void GPIO_Init(GPIO_TypeDef* GPIOx, uint8_t GPIO_Pin, GPIO_Mode_TypeDef GPIO_Mode) { /*----------------------*/ /* Check the parameters */ /*----------------------*/ assert_param(IS_GPIO_MODE(GPIO_Mode)); assert_param(IS_GPIO_PIN(GPIO_Pin)); /* Reset corresponding bit to GPIO_Pin in CR2 register */ GPIOx->CR2 &= (uint8_t)(~(GPIO_Pin)); /*-----------------------------*/ /* Input/Output mode selection */ /*-----------------------------*/ if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x80) != (uint8_t)0x00) /* Output mode */ { if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x10) != (uint8_t)0x00) /* High level */ { GPIOx->ODR |= GPIO_Pin; } else /* Low level */ { GPIOx->ODR &= (uint8_t)(~(GPIO_Pin)); } /* Set Output mode */ GPIOx->DDR |= GPIO_Pin; } else /* Input mode */ { /* Set Input mode */ GPIOx->DDR &= (uint8_t)(~(GPIO_Pin)); } /*------------------------------------------------------------------------*/ /* Pull-Up/Float (Input) or Push-Pull/Open-Drain (Output) modes selection */ /*------------------------------------------------------------------------*/ if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x40) != (uint8_t)0x00) /* Pull-Up or Push-Pull */ { GPIOx->CR1 |= GPIO_Pin; } else /* Float or Open-Drain */ { GPIOx->CR1 &= (uint8_t)(~(GPIO_Pin)); } /*-----------------------------------------------------*/ /* Interrupt (Input) or Slope (Output) modes selection */ /*-----------------------------------------------------*/ if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x20) != (uint8_t)0x00) /* Interrupt or Slow slope */ { GPIOx->CR2 |= GPIO_Pin; } else /* No external interrupt or No slope control */ { GPIOx->CR2 &= (uint8_t)(~(GPIO_Pin)); } }
/** * @brief Initializes the GPIOx peripheral according to the specified * parameters in the GPIO_InitStruct. * @param GPIOx: where x can be (A, B or C) to select the GPIO peripheral. * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that * contains the configuration information for the specified GPIO * peripheral. * GPIO_Pin: selects the pin to be configured: GPIO_Pin_0 -> GPIO_Pin_7 * GPIO_Mode: selects the mode of the pin: * - GPIO Analog Mode: GPIO_Mode_AN * - GPIO Output Mode PP: GPIO_Mode_OUT_PP * - GPIO Input Mode NOPULL: GPIO_Mode_IN * - GPIO Output Mode OD: GPIO_Mode_OUT_OD * - GPIO Input Mode PuPd: GPIO_Mode_IN_PUD * - GPIO Alternate function Mode PP: GPIO_Mode_AF_PP * - GPIO Alternate function Mode SPI SCLK PP: GPIO_Mode_AF_PP_SPI * - GPIO Alternate function Mode OD: GPIO_Mode_AF_OD * @retval None */ void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct) { uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00; uint32_t tmpreg = 0x00, pinmask = 0x00; /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(GPIOx)); assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode)); assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin)); /*---------------------------- GPIO Mode Configuration -----------------------*/ currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F); /*---------------------------- GPIO CRL Configuration ------------------------*/ /* Configure the four low port pins */ if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x0F)) != 0x00) { tmpreg = GPIOx->CRL; for (pinpos = 0x00; pinpos < 0x04; pinpos++) { pos = ((uint32_t)0x01) << pinpos; /* Get the port pins position */ currentpin = (GPIO_InitStruct->GPIO_Pin) & pos; if (currentpin == pos) { pos = pinpos << 2; /* Clear the corresponding low control register bits */ pinmask = ((uint32_t)0x0F) << pos; tmpreg &= ~pinmask; /* Write the mode configuration in the corresponding bits */ tmpreg |= (currentmode << pos); } } GPIOx->CRL = tmpreg; } /*---------------------------- GPIO CRH Configuration ------------------------*/ /* Configure the four high port pins */ if (GPIO_InitStruct->GPIO_Pin > 0x0F) { tmpreg = GPIOx->CRH; for (pinpos = 0x00; pinpos < 0x04; pinpos++) { pos = (((uint32_t)0x01) << (pinpos + 0x04)); /* Get the port pins position */ currentpin = ((GPIO_InitStruct->GPIO_Pin) & pos); if (currentpin == pos) { pos = pinpos << 2; /* Clear the corresponding high control register bits */ pinmask = ((uint32_t)0x0F) << pos; tmpreg &= ~pinmask; /* Write the mode configuration in the corresponding bits */ tmpreg |= (currentmode << pos); } } GPIOx->CRH = tmpreg; } }
/** * @brief Initializes the GPIOx peripheral according to the specified * parameters in the GPIO_InitStruct. ��ʼ��GPIOx ����ͨ������GPIOx(����mode\speed\PuPd\otype) * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral. ���� GPIOx(A, B, C, D, E or H) * @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that * contains the configuration information for the specified GPIO * peripheral. * GPIO_Pin: selects the pin to be configured: GPIO_Pin_0 -> GPIO_Pin_15 * GPIO_Mode: selects the mode of the pin: * - Input mode: GPIO_Mode_IN * - Output mode: GPIO_Mode_OUT * - Alternate Function mode: GPIO_Mode_AF * - Analog mode: GPIO_Mode_AN * GPIO_Speed: selects the speed of the pin if configured in Output: * - Very Low: GPIO_Speed_400KHz * - Low: GPIO_Speed_2MHz * - Medium: GPIO_Speed_10MHz * - High: GPIO_Speed_40MHz * GPIO_OType: selects the Output type (if the selected mode is output): * - Push-pull: GPIO_OType_PP * - Open Drain: GPIO_OType_OD * GPIO_PuPd: configures the Pull-up/Pull-down resistor on the pin: * - pull-up: GPIO_PuPd_UP * - pull-down: GPIO_PuPd_DOWN * - Neither pull-up nor Pull-down: GPIO_PuPd_NOPULL ����Ϊ1��GPIO_TypeDef* GPIOx������GPIOx)�������������ļĴ����� 2��GPIO_InitTypeDef* GPIO_InitStruct ������д�û���Ҫ�����͡��ٶȡ����ģʽ�����������ȣ� * @retval None */ void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct) { uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00; /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(GPIOx)); assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin)); assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode)); assert_param(IS_GPIO_PUPD(GPIO_InitStruct->GPIO_PuPd)); /* -------------------------Configure the port pins---------------- */ /*-- GPIO Mode Configuration --*/ for (pinpos = 0x00; pinpos < 0x10; pinpos++) { pos = ((uint32_t)0x01) << pinpos; /* Get the port pins position */ currentpin = (GPIO_InitStruct->GPIO_Pin) & pos; if (currentpin == pos) { GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (pinpos * 2));/*һ��pinռ2λ����˱����ƶ�pinpos * 2,�����*/ GPIOx->MODER |= (((uint32_t)GPIO_InitStruct->GPIO_Mode) << (pinpos * 2));/*һ��pinռ2λ����˱����ƶ�pinpos * 2,�ٸ�ֵ*/ if ((GPIO_InitStruct->GPIO_Mode == GPIO_Mode_OUT) || (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_AF)) { /*��ѡ�����ģʽ����AFģʽ,�����ٶȡ�������͡�*/ /* Check Speed mode parameters */ assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed)); /* Speed mode configuration */ GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (pinpos * 2));/*һ��pinռ2λ����˱����ƶ�pinpos * 2,�����*/ GPIOx->OSPEEDR |= ((uint32_t)(GPIO_InitStruct->GPIO_Speed) << (pinpos * 2));/*һ��pinռ2λ����˱����ƶ�pinpos * 2,�ٸ�ֵ*/ /*Check Output mode parameters */ assert_param(IS_GPIO_OTYPE(GPIO_InitStruct->GPIO_OType)); /* Output mode configuration */ GPIOx->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)pinpos)) ;/*һ��pinռ1λ����˱����ƶ�pinpos ,�����*/ GPIOx->OTYPER |= (uint16_t)(((uint16_t)GPIO_InitStruct->GPIO_OType) << ((uint16_t)pinpos));/*һ��pinռ1λ����˱����ƶ�pinpos ,�ٸ�ֵ*/ } /*������*/ /* Pull-up Pull down resistor configuration */ GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)pinpos * 2));/*һ��pinռ2λ����˱����ƶ�pinpos * 2,�����*/ GPIOx->PUPDR |= (((uint32_t)GPIO_InitStruct->GPIO_PuPd) << (pinpos * 2));/*һ��pinռ2λ����˱����ƶ�pinpos*2 ,�ٸ�ֵ*/ } } }
void GPIO_Init(GPIO_InitTypeDef* GPIO_InitStruct) { int pin_reg = 0,mode_reg; /* Check the parameters */ assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode)); assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin)); /* set ADDR_GPIO_PIN_MUX_CTRL Register*/ pin_reg = NST_RD_PWM_REG(ADDR_GPIO_PIN_MUX_CTRL); pin_reg |= GPIO_InitStruct->GPIO_Pin; /* set SWPORTA_DDR Register*/ mode_reg = NST_RD_GPIO_REG(SWPORTA_DDR); if(GPIO_InitStruct->GPIO_Mode){ mode_reg |= GPIO_InitStruct->GPIO_Pin; } else { mode_reg &= ~GPIO_InitStruct->GPIO_Pin; } NST_WR_PWM_REG(ADDR_GPIO_PIN_MUX_CTRL, pin_reg); NST_WR_GPIO_REG(SWPORTA_DDR, mode_reg); }
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; }