new SensorTile firmware, fixed LED problem

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
shaoziyang
Posts: 104
Joined: Sun Apr 17, 2016 1:55 pm

new SensorTile firmware, fixed LED problem

Postby shaoziyang » Tue Jan 10, 2017 3:34 pm

By default, micropython doesn't support GPIOG, so LED in SensorTile is not blinky.

I modify pin.c and led.c, add GPIOG support.

in pin.c, modify fuction pin_obj_init_helper

Code: Select all

// init(mode, pull=None, af=-1, *, value, alt)
STATIC mp_obj_t pin_obj_init_helper(const pin_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
    static const mp_arg_t allowed_args[] = {
        { MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT },
        { MP_QSTR_pull, MP_ARG_OBJ, {.u_obj = mp_const_none}},
        { MP_QSTR_af, MP_ARG_INT, {.u_int = -1}}, // legacy
        { MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
        { MP_QSTR_alt, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1}},
    };

    // parse args
    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
    mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

    // get io mode
    uint mode = args[0].u_int;
    if (!IS_GPIO_MODE(mode)) {
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid pin mode: %d", mode));
    }

    // get pull mode
    uint pull = GPIO_NOPULL;
    if (args[1].u_obj != mp_const_none) {
        pull = mp_obj_get_int(args[1].u_obj);
    }
    if (!IS_GPIO_PULL(pull)) {
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid pin pull: %d", pull));
    }

    // get af (alternate function); alt-arg overrides af-arg
    mp_int_t af = args[4].u_int;
    if (af == -1) {
        af = args[2].u_int;
    }
    if ((mode == GPIO_MODE_AF_PP || mode == GPIO_MODE_AF_OD) && !IS_GPIO_AF(af)) {
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid pin af: %d", af));
    }

    // enable the peripheral clock for the port of this pin
    mp_hal_gpio_clock_enable(self->gpio);

    // if given, set the pin value before initialising to prevent glitches
    if (args[3].u_obj != MP_OBJ_NULL) {
        mp_hal_pin_write(self, mp_obj_is_true(args[3].u_obj));
    }

#if defined(STM32L476xx)
    __HAL_RCC_SYSCFG_CLK_ENABLE();
    __HAL_RCC_PWR_CLK_ENABLE();
    __HAL_RCC_GPIOG_CLK_ENABLE();
    HAL_PWREx_EnableVddIO2();
#endif

    // configure the GPIO as requested
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.Pin = self->pin_mask;
    GPIO_InitStructure.Mode = mode;
    GPIO_InitStructure.Pull = pull;
    GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
    GPIO_InitStructure.Alternate = af;
    HAL_GPIO_Init(self->gpio, &GPIO_InitStructure);

    return mp_const_none;
}


in LED.c, modify function led_init()

Code: Select all

void led_init(void) {
    /* Turn off LEDs and initialize */

#if defined(STM32L476xx)
    __HAL_RCC_SYSCFG_CLK_ENABLE();
    __HAL_RCC_PWR_CLK_ENABLE();
    __HAL_RCC_GPIOG_CLK_ENABLE();
    HAL_PWREx_EnableVddIO2();
#endif

    for (int led = 0; led < NUM_LEDS; led++) {
        const pin_obj_t *led_pin = pyb_led_obj[led].led_pin;
        mp_hal_gpio_clock_enable(led_pin->gpio);
        MICROPY_HW_LED_OFF(led_pin);
        mp_hal_pin_output(led_pin);
    }
}

User avatar
dhylands
Posts: 2109
Joined: Mon Jan 06, 2014 6:08 pm
Location: Shuswap, BC, Canada
Contact:

Re: new SensorTile firmware, fixed LED problem

Postby dhylands » Tue Jan 10, 2017 5:46 pm

I'd recommend that you put together a PR in github.

led.c is already calling mp_hal_gpio_clock_enabled here:
https://github.com/micropython/micropyt ... /led.c#L73

It feels like this problem would affect pins as well as leds, so having that code in led.c seems wrong to me.

This function: https://github.com/micropython/micropyt ... c#L85-L132 already has code for enabling the clock.
Perhaps it needs to be expanded?

I personally don't like processor specific #ifdefs if something more generic can be used.

shaoziyang
Posts: 104
Joined: Sun Apr 17, 2016 1:55 pm

Re: new SensorTile firmware, fixed LED problem

Postby shaoziyang » Wed Jan 11, 2017 2:25 am

dhylands wrote:I'd recommend that you put together a PR in github.

I am not familiar with git, it is too complex. Can you give me some advise?

This function: https://github.com/micropython/micropyt ... c#L85-L132 already has code for enabling the clock.
Perhaps it needs to be expanded?

I personally don't like processor specific #ifdefs if something more generic can be used.

unlike other GPIO port, GPIOG need VddIO2, only use GPIOG_CLK_ENABLE is not work. so we must add these code.

User avatar
dhylands
Posts: 2109
Joined: Mon Jan 06, 2014 6:08 pm
Location: Shuswap, BC, Canada
Contact:

Re: new SensorTile firmware, fixed LED problem

Postby dhylands » Wed Jan 11, 2017 6:29 am

Could you check if changing mp_hal_gpio_clock_enable function in stmhal/mphalport.c like this:

Code: Select all

    #if defined(GPIOG) && defined(__GPIOG_CLK_ENABLE)
    } else if (gpio == GPIOG) {
        #if defined(STM32L476xx) || defined(STM32L486xx)
        // According to AN4555 (Getting started with STM32L4 series hardware
        // development, Port G pins 2 thu 15 are powered using VddIO2 for the
        // STM32L4x6 MCUs.
        HAL_PWREx_EnableVddIO2();
        #endif
        __GPIOG_CLK_ENABLE();
    #endif
works.

The PWR clock is already enabled in system_stm32.c, and I couldn't see anywhere that needed SYSCFG.

If that works, I'd be happy to create a PR.

shaoziyang
Posts: 104
Joined: Sun Apr 17, 2016 1:55 pm

Re: new SensorTile firmware, fixed LED problem

Postby shaoziyang » Wed Jan 11, 2017 7:43 am

dhylands wrote:Could you check if changing mp_hal_gpio_clock_enable function in stmhal/mphalport.c like this:

Code: Select all

    #if defined(GPIOG) && defined(__GPIOG_CLK_ENABLE)
    } else if (gpio == GPIOG) {
        #if defined(STM32L476xx) || defined(STM32L486xx)
        // According to AN4555 (Getting started with STM32L4 series hardware
        // development, Port G pins 2 thu 15 are powered using VddIO2 for the
        // STM32L4x6 MCUs.
        HAL_PWREx_EnableVddIO2();
        #endif
        __GPIOG_CLK_ENABLE();
    #endif
works.

The PWR clock is already enabled in system_stm32.c, and I couldn't see anywhere that needed SYSCFG.

If that works, I'd be happy to create a PR.


Yes, it works, I just test it in my SensorTile kit.

And PORTG must add to pins.cvs too. I have upload SensorTile board file at:
https://github.com/shaoziyang/MicroPyth ... SensorTile

User avatar
dhylands
Posts: 2109
Joined: Mon Jan 06, 2014 6:08 pm
Location: Shuswap, BC, Canada
Contact:

Re: new SensorTile firmware, fixed LED problem

Postby dhylands » Wed Jan 11, 2017 10:31 pm

I filed a PR for the first issue (since it isn't really SensorTile specific):
https://github.com/micropython/micropython/pull/2782

I'll look at the other files later.


Return to “Other Boards”

Who is online

Users browsing this forum: No registered users and 1 guest