Example #1
0
/*****************************************************************************
* Function: Init
*
* Description:
*****************************************************************************/
void AlternateFunctionPin::Init(void)
{
    assert_param(IS_GPIO_ALL_PERIPH(this->port));
    assert_param(IS_GET_GPIO_PIN(this->pin));
    assert_param(IS_GPIO_PIN_SOURCE(this->pin_source));
    assert_param(IS_GPIO_AF(this->alternate_function));
    assert_param(IS_RCC_AHB1_CLOCK_PERIPH(this->clock));

    // Peripheral clock enable.
    RCC_AHB1PeriphClockCmd(this->clock, ENABLE);

    // Configure pin in alternate function mode.
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = this->pin;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(this->port, &GPIO_InitStructure);

    GPIO_PinAFConfig(this->port, this->pin_source, this->alternate_function);

} // AlternateFunctionPin::Init()
Example #2
0
STATIC mp_obj_t pin_obj_init_helper(const pin_obj_t *self, uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
    // parse args
    mp_arg_val_t vals[PIN_INIT_NUM_ARGS];
    mp_arg_parse_all(n_args, args, kw_args, PIN_INIT_NUM_ARGS, pin_init_args, vals);

    // get io mode
    uint mode = vals[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 = vals[1].u_int;
    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)
    mp_int_t af = vals[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
    switch (self->port) {
        #ifdef __GPIOA_CLK_ENABLE
        case PORT_A: __GPIOA_CLK_ENABLE(); break;
        #endif
        #ifdef __GPIOB_CLK_ENABLE
        case PORT_B: __GPIOB_CLK_ENABLE(); break;
        #endif
        #ifdef __GPIOC_CLK_ENABLE
        case PORT_C: __GPIOC_CLK_ENABLE(); break;
        #endif
        #ifdef __GPIOD_CLK_ENABLE
        case PORT_D: __GPIOD_CLK_ENABLE(); break;
        #endif
        #ifdef __GPIOE_CLK_ENABLE
        case PORT_E: __GPIOE_CLK_ENABLE(); break;
        #endif
        #ifdef __GPIOF_CLK_ENABLE
        case PORT_F: __GPIOF_CLK_ENABLE(); break;
        #endif
        #ifdef __GPIOG_CLK_ENABLE
        case PORT_G: __GPIOG_CLK_ENABLE(); break;
        #endif
        #ifdef __GPIOH_CLK_ENABLE
        case PORT_H: __GPIOH_CLK_ENABLE(); break;
        #endif
        #ifdef __GPIOI_CLK_ENABLE
        case PORT_I: __GPIOI_CLK_ENABLE(); break;
        #endif
        #ifdef __GPIOJ_CLK_ENABLE
        case PORT_J: __GPIOJ_CLK_ENABLE(); break;
        #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;
}