microsoft / jacdac-stm32x0

Jacdac implementation for STM32F0 and similar
MIT License
11 stars 10 forks source link

Instance ID and Name in stm32g0 firmware #49

Closed brian-inksmith closed 1 year ago

brian-inksmith commented 1 year ago

Hello!

We're looking to implement instances in our firmware for modules that have multiple sensors, similar to how it is for the button simulator below. Our use case is for relays and for light sensors.

image image

We've implemented the following code for our jacdac breakout board:

board.h

#define PIN_LED NO_PIN
#define PIN_LED_GND NO_PIN

// RGB LED sink connected to these pins
#define PIN_LED_R PA_10
#define PIN_LED_G PA_9
#define PIN_LED_B PA_8

// these values calibrate to ~60lux for each of ff0000, 00ff00, 0000ff
#define LED_R_MULT 250
#define LED_G_MULT 150
#define LED_B_MULT 42
#define RGB_LED_PERIOD 600

#define PIN_BL_LED PIN_LED_B
#define PIN_BL_PERIOD 300

#define PIN_PWR NO_PIN
#define PIN_P0 NO_PIN
#define PIN_P1 NO_PIN

// #define PIN_SDA PA_10
// #define PIN_SCL PA_9
// #define I2C_AF LL_GPIO_AF_4

#define BOARD_STARTUP_CODE LL_SYSCFG_EnablePinRemap(LL_SYSCFG_PIN_RMP_PA11 | LL_SYSCFG_PIN_RMP_PA12);

#define UART_PIN PB_6
#define UART_PIN_AF LL_GPIO_AF_0
#define USART_IDX 1

#define JD_INSTANCE_NAME 1

And out BreakoutBoard.c file is the following:


#include "jdprofile.h"
#include "jacdac/dist/c/relay.h"

FIRMWARE_IDENTIFIER(0x3412171b, "Breakout Board+Servo+Pump");

const servo_params_t servo_0 = {
    .pin = PA_6,
    .fixed = 0,
    .min_angle = -90 << 16,
    .min_pulse = 600,
    .max_angle = 90 << 16,
    .max_pulse = 2500,
    .power_pin = PB_7
};

const servo_params_t servo_1 = {
    .pin = PA_7,
    .fixed = 0,
    .min_angle = -90 << 16,
    .min_pulse = 600,
    .max_angle = 90 << 16,
    .max_pulse = 2500,
    .power_pin = PC_15
};

const servo_params_t servo_2 = {
    .pin = PA_4,
    .fixed = 0,
    .min_angle = -90 << 16,
    .min_pulse = 600,
    .max_angle = 90 << 16,
    .max_pulse = 2500,
    .power_pin = PC_15
    //NO_PIN
};    

const relay_params_t hi_power = {
    .relay_variant = JD_RELAY_VARIANT_ELECTROMECHANICAL,
    .max_switching_current = 20,
    .pin_relay_drive = PA_0,
    .pin_relay_feedback = NO_PIN,
    .pin_relay_led = NO_PIN,
    .drive_active_lo = true,
    .led_active_lo = false,
    .initial_state = true
};

void app_init_services() {
    // high power mode when we get there

    servo_init(&servo_0);
    servo_init(&servo_1);
    servo_init(&servo_2);
    relay_service_init(&hi_power);
}

const char *app_get_instance_name(int service_idx) {
    switch (service_idx) {
    case 1:
        return "S1";
    case 2:
        return "S2";
    case 3:
        return "S3";
    case 4:
        return "P1";
    }
    return NULL;
}

The above code does not work once we compile it (no errors upon compile) and load it to the module. Any thoughts on what else is necessary? Thanks in advance!

mmoskal commented 1 year ago

It works for me. I updated our servo-dual example to use this, and got the resulting screenshot.

Screen Shot 2023-05-15 at 12 36 01 PM
mmoskal commented 1 year ago

(see the commit above or here https://github.com/microsoft/jacdac-msr-modules/commit/7e9e119c551eb72f16153715adcbf44aebd7c4e5 )

mmoskal commented 1 year ago

oh, and most of that commit is just formatting

brian-inksmith commented 1 year ago

Thanks Michal, I will take a look and get back soon.

brian-inksmith commented 1 year ago

Looks like it was the submodules not updating. Using the same as you worked. Thank you Michal!