int usb_configure(usb_attr_t * attr) { if (attr == NULL) { return 0; } if (attr->use_present_pin) { if (!IS_GPIO_ALL_PERIPH(attr->present_port->GPIOx) || !IS_GPIO_PIN_SOURCE(attr->present_pin)) { return 0; } gpio_set_mode(attr->present_port, attr->present_pin, GPIO_INPUT_PU); } preempt_prio = attr->preempt_prio; sub_prio = attr->sub_prio; // USB Device Initialize USBD_Init(&USB_OTG_dev, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_CDC_cb, &USR_cb); usb_ready = 1; return 1; }
/** * @brief Reads the specified GPIO output data port. * @param GPIOx: where x can be (A, B or C) to select the GPIO peripheral. * @retval GPIO output data port value. */ uint32_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx) { /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(GPIOx)); return ((uint32_t)GPIOx->ODR); }
void gpio_toggle_bit(gpio_dev* dev, uint8_t pin) { /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(dev->GPIOx)); assert_param(IS_GPIO_PIN_SOURCE(pin)); dev->GPIOx->ODR ^= BIT(pin); }
/** * @brief Writes data to the specified GPIO data port. * @param GPIOx: where x can be (A, B or C) to select the GPIO peripheral. * @param PortVal: specifies the value to be written to the port output data * register. * @retval None */ void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal) { /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(GPIOx)); GPIOx->ODR = PortVal; }
/***************************************************************************** * Function: Init * * Description: *****************************************************************************/ void OutputPin::Init ( pin_level_t pin_level // Starting level (high/low) of pin. ) { assert_param(IS_GPIO_ALL_PERIPH(this->port)); assert_param(IS_GET_GPIO_PIN(this->pin)); assert_param(IS_RCC_AHB1_CLOCK_PERIPH(this->clock)); // Peripheral clock enable. RCC_AHB1PeriphClockCmd(this->clock, ENABLE); // Configure pin in output pushpull mode. GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = this->pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; 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); // Initialize pin level and field. if (pin_level == HIGH) { WriteHigh(); } else { WriteLow(); } } // OutputPin::Init()
void LCD_Init( uint8_t fourBitMode, GPIO_TypeDef* gpio, uint16_t rs, uint16_t rw, uint16_t enable, uint16_t d0, uint16_t d1, uint16_t d2, uint16_t d3, uint16_t d4, uint16_t d5, uint16_t d6, uint16_t d7) { uint32_t periph; assert_param(IS_GPIO_ALL_PERIPH(gpio)); assert_param(IS_GPIO_PIN(rs)); assert_param(IS_GPIO_PIN(rw)); assert_param(IS_GPIO_PIN(enable)); assert_param(IS_GPIO_PIN(d0)); assert_param(IS_GPIO_PIN(d1)); assert_param(IS_GPIO_PIN(d2)); assert_param(IS_GPIO_PIN(d3)); if(!fourBitMode) { assert_param(IS_GPIO_PIN(d4)); assert_param(IS_GPIO_PIN(d5)); assert_param(IS_GPIO_PIN(d6)); assert_param(IS_GPIO_PIN(d7)); } Lcd_Conf.gpio = gpio; Lcd_Conf.rs = rs; Lcd_Conf.rw = rw; Lcd_Conf.enable = enable; Lcd_Conf.data[0] = d0; Lcd_Conf.data[1] = d1; Lcd_Conf.data[2] = d2; Lcd_Conf.data[3] = d3; Lcd_Conf.data[4] = d4; Lcd_Conf.data[5] = d5; Lcd_Conf.data[6] = d6; Lcd_Conf.data[7] = d7; if (gpio == GPIOA) periph = RCC_AHB1Periph_GPIOA; else if (gpio == GPIOB) periph = RCC_AHB1Periph_GPIOB; else if (gpio == GPIOC) periph = RCC_AHB1Periph_GPIOC; else if (gpio == GPIOD) periph = RCC_AHB1Periph_GPIOD; else if (gpio == GPIOE) periph = RCC_AHB1Periph_GPIOE; else return; RCC_AHB1PeriphClockCmd(periph, ENABLE); set_output(1); if(fourBitMode) Lcd_Conf.dispFunc = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS; else Lcd_Conf.dispFunc = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS; }
/** * @brief De-initializes the GPIOx peripheral registers to their default reset values. * @note By default, The GPIO pins are configured in input floating mode (except JTAG pins). * @param GPIOx: where x can be (A..K) to select the GPIO peripheral for STM32F429X device or * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices. * @retval None */ void GPIO_DeInit(GPIO_TypeDef* GPIOx) { /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(GPIOx)); if (GPIOx == GPIOA) { RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA, ENABLE); RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA, DISABLE); } else if (GPIOx == GPIOB) { RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOB, ENABLE); RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOB, DISABLE); } else if (GPIOx == GPIOC) { RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOC, ENABLE); RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOC, DISABLE); } else if (GPIOx == GPIOD) { RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOD, ENABLE); RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOD, DISABLE); } else if (GPIOx == GPIOE) { RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOE, ENABLE); RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOE, DISABLE); } else if (GPIOx == GPIOF) { RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOF, ENABLE); RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOF, DISABLE); } else if (GPIOx == GPIOG) { RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOG, ENABLE); RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOG, DISABLE); } else if (GPIOx == GPIOH) { RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOH, ENABLE); RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOH, DISABLE); } #if defined (STM32F40XX) || defined (STM32F427X) else { if (GPIOx == GPIOI) { RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOI, ENABLE); RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOI, DISABLE); } } #endif /* STM32F40XX || STM32F427X */ #if defined (STM32F429X) else if (GPIOx == GPIOI)
/** * @brief Clears the selected data port bits. * @param GPIOx: where x can be (A, B or C) to select the GPIO peripheral. * @param GPIO_Pin: specifies the port bits to be written. * This parameter can be any combination of GPIO_Pin_x where x can be (0..7). * @note This functions uses GPIOx_CLR register to allow atomic read/modify * accesses. In this way, there is no risk of an IRQ occurring between * the read and the modify access. * @retval None */ void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint32_t GPIO_Pin) { /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(GPIOx)); assert_param(IS_GPIO_PIN(GPIO_Pin)); GPIOx->BRR = 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; } }
void gpio_init(gpio_dev* dev) { /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(dev->GPIOx)); GPIO_DeInit(dev->GPIOx); /* Enable the GPIO Clock */ //LASER_PWM_PATCH dev->clkcmd(dev->clk | RCC_APB2Periph_AFIO, ENABLE); }
void configurePin(GPIO_TypeDef* port, uint16_t pinNum, GPIOMode_TypeDef mode) { /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(GPIOx)); if (port == GPIOA) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //| RCC_APB2Periph_AFIO RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, DISABLE); } else if (port == GPIOB) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, DISABLE); } else if (port == GPIOC) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC, DISABLE); } else if (port == GPIOD) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD, DISABLE); } else if (port == GPIOE) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE); RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOE, DISABLE); } else if (port == GPIOF) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE); RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOF, DISABLE); } else { if (port == GPIOG) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG, ENABLE); RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOG, DISABLE); } } GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = pinNum; GPIO_InitStructure.GPIO_Mode = mode; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(port, &GPIO_InitStructure); }
static u8 GetGpioPinNumber(u16 gpio_pin) { u8 i = 0; //参数检查 assert_param(IS_GPIO_ALL_PERIPH(gpio_pin)); for(i = 0; i < 16; i ++) { if(gpio_pin == GPIO_Pin[i]) { return i; } } return 0; //不会执行到这里 }
void gpio_write_bit(gpio_dev* dev, uint8_t pin, uint8_t val) { /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(dev->GPIOx)); assert_param(IS_GPIO_PIN_SOURCE(pin)); if (val) { dev->GPIOx->BSRR = BIT(pin); } else { dev->GPIOx->BRR = BIT(pin); } }
/** * @brief Deinitializes the GPIOx peripheral registers to their default reset values. * @param GPIOx: where x can be (A..C) to select the GPIO peripheral. * @retval None */ void GPIO_DeInit(GPIO_TypeDef* GPIOx) { /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(GPIOx)); GPIOx->CRL = 0x00004444; GPIOx->CRH = 0x00004444; GPIOx->IDR = 0x00000000; GPIOx->ODR = 0x00000000; GPIOx->BSR = 0x00000000; GPIOx->BRR = 0x00000000; GPIO_DBG->PCTRACECR = 0x00000000; GPIO_DBG->DBGCR = 0x00000010; }
/** * @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_set_mode(gpio_dev* dev, uint8_t pin, gpio_pin_mode mode) { /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(dev->GPIOx)); assert_param(IS_GPIO_PIN_SOURCE(pin)); GPIO_InitTypeDef config; /* Enable the GPIO Clock */ dev->clkcmd(dev->clk, ENABLE); /* Configure the pin */ GPIO_StructInit(&config); config.GPIO_Speed = GPIO_DEFAULT_SPEED; switch(mode) { case GPIO_OUTPUT_PP: config.GPIO_Mode = GPIO_Mode_Out_PP; break; case GPIO_OUTPUT_OD: config.GPIO_Mode = GPIO_Mode_Out_OD; break; case GPIO_INPUT_FLOATING: config.GPIO_Mode = GPIO_Mode_IN_FLOATING; break; case GPIO_INPUT_ANALOG: config.GPIO_Mode = GPIO_Mode_AIN; break; case GPIO_INPUT_PU: config.GPIO_Mode = GPIO_Mode_IPU; break; case GPIO_INPUT_PD: config.GPIO_Mode = GPIO_Mode_IPD; break; case GPIO_AF_OUTPUT_PP: config.GPIO_Mode = GPIO_Mode_AF_PP; break; case GPIO_AF_OUTPUT_OD: config.GPIO_Mode = GPIO_Mode_AF_OD; break; default: errno_r = EINVAL; return; } config.GPIO_Pin = BIT(pin); GPIO_Init(dev->GPIOx, &config); }
/** * @brief Sets or clears the selected data port bit. * @param GPIOx: where x can be (A, B or C) to select the GPIO peripheral. * @param GPIO_Pin: specifies the port bit to be written. * This parameter can be one of GPIO_Pin_x where x can be (0..7). * @param BitVal: specifies the value to be written to the selected bit. * This parameter can be one of the BitAction enum values: * @arg Bit_RESET: to clear the port pin * @arg Bit_SET: to set the port pin * @retval None */ void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint32_t GPIO_Pin, BitAction BitVal) { /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(GPIOx)); assert_param(IS_GET_GPIO_PIN(GPIO_Pin)); assert_param(IS_GPIO_BIT_ACTION(BitVal)); if (BitVal != Bit_RESET) { GPIOx->BSR = GPIO_Pin; } else { GPIOx->BRR = GPIO_Pin ; } }
/** * @brief Reads the specified input port pin. * @param GPIOx: where x can be (A, B or C) to select the GPIO peripheral. * @param GPIO_Pin: specifies the port bit to read. * This parameter can be GPIO_Pin_x where x can be (0..7). * @retval The input port pin value. */ uint32_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint32_t GPIO_Pin) { uint32_t bitstatus = 0x00; /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(GPIOx)); assert_param(IS_GET_GPIO_PIN(GPIO_Pin)); if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)Bit_RESET) { bitstatus = (uint32_t)Bit_SET; } else { bitstatus = (uint32_t)Bit_RESET; } return bitstatus; }
/***************************************************************************** * Function: Init * * Description: *****************************************************************************/ void InputPin::Init(void) { assert_param(IS_GPIO_ALL_PERIPH(this->port)); assert_param(IS_GET_GPIO_PIN(this->pin)); assert_param(IS_RCC_AHB1_CLOCK_PERIPH(this->clock)); // Peripheral clock enable. RCC_AHB1PeriphClockCmd(this->clock, ENABLE); // Configure pin in input pushpull mode. GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = this->pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; 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); } // InputPin::Init()
uint8_t gpio_read_bit(gpio_dev* dev, uint8_t pin) { uint8_t bitstatus = 0x00; /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(dev->GPIOx)); assert_param(IS_GPIO_PIN_SOURCE(pin)); if ((dev->GPIOx->IDR & BIT(pin)) != (uint32_t)Bit_RESET) { bitstatus = (uint8_t)Bit_SET; } else { bitstatus = (uint8_t)Bit_RESET; } return bitstatus; }
/** * @brief Deinitializes the GPIOx peripheral registers to their default reset * values. ��ʼ��GPIOx�Ĵ�����ȱʡֵ,ͨ������RCC_AHBRSTR * By default, The GPIO pins are configured in input floating mode * (except JTAG pins). ȱʡ�£�GPIO ���ó���������ģʽ������JTAG) * @param GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral. ���� GPIOx:x����Ϊ(A, B, C, D, E or H) * @retval None û�з���ֵ */ void GPIO_DeInit(GPIO_TypeDef* GPIOx) { /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(GPIOx)); /*������Ժ����ڼ�麯������,�������������assert_failed����main.c)���û����ã���������Դ�ļ�����������ȷ������*/ /*IS_GPIO_ALL_PERIPH()���ڼ������Ƿ�ϸ���1����0*/ if(GPIOx == GPIOA) { RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA, DISABLE); /*����GPIOx,ͨ������RCC_AHBRSTR*/ } else if(GPIOx == GPIOB) { RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, ENABLE); RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, DISABLE); } else if(GPIOx == GPIOC) { RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOC, ENABLE); RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOC, DISABLE); } else if(GPIOx == GPIOD) { RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOD, ENABLE); RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOD, DISABLE); } else if(GPIOx == GPIOE) { RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOE, ENABLE); RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOE, DISABLE); } else { if(GPIOx == GPIOH) { RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOH, ENABLE); RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOH, DISABLE); } } }
/***************************************************************************** * 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()
afio_exti_port gpio_exti_port(gpio_dev* dev) { /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(dev->GPIOx)); return dev->exti_port; }
/* GPIOx: where x can be (A..H) to select the GPIO peripheral. pinNumber: GPIO_Pin_0 ... GPIO_Pin_15, GPIO_Pin_All GPIO_AF: stm32l1xx_gpio.c, search GPIO_AFSelection. pinType: GPIO_OType_PP, GPIO_OType_OD - sets the pin type to push / pull or open drain pinPull: GPIO_PuPd_NOPULL, GPIO_PuPd_UP, GPIO_PuPd_DOWN pinSpeed: GPIO_Speed_400KHz, GPIO_Speed_2MHz, GPIO_Speed_10MHz, GPIO_Speed_40MHz gpio_pinSetup_AF(GPIOA, GPIO_Pin_2, GPIO_AF_TIM9, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_Speed_2MHz); */ void gpio_pinSetup_AF(GPIO_TypeDef* GPIOx, uint32_t pinNumber, uint8_t GPIO_AF, GPIOOType_TypeDef pinType, GPIOPuPd_TypeDef pinPull, GPIOSpeed_TypeDef pinSpeed) { GPIO_InitTypeDef GPIO_setup; uint32_t pinSource = 0; /* Check the parameters */ assert_param(IS_GPIO_ALL_PERIPH(GPIOx)); if(GPIOx == GPIOA) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); } else if(GPIOx == GPIOB) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); } else if(GPIOx == GPIOC) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); } else if(GPIOx == GPIOD) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE); } else if(GPIOx == GPIOE) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE); } else if(GPIOx == GPIOF) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE); } else if(GPIOx == GPIOG) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOG, ENABLE); } else { if(GPIOx == GPIOH) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOH, ENABLE); } } switch(pinNumber) { case GPIO_Pin_0: pinSource = GPIO_PinSource0; break; case GPIO_Pin_1: pinSource = GPIO_PinSource1; break; case GPIO_Pin_2: pinSource = GPIO_PinSource2; break; case GPIO_Pin_3: pinSource = GPIO_PinSource3; break; case GPIO_Pin_4: pinSource = GPIO_PinSource4; break; case GPIO_Pin_5: pinSource = GPIO_PinSource5; break; case GPIO_Pin_6: pinSource = GPIO_PinSource6; break; case GPIO_Pin_7: pinSource = GPIO_PinSource7; break; case GPIO_Pin_8: pinSource = GPIO_PinSource8; break; case GPIO_Pin_9: pinSource = GPIO_PinSource9; break; case GPIO_Pin_10: pinSource = GPIO_PinSource10; break; case GPIO_Pin_11: pinSource = GPIO_PinSource11; break; case GPIO_Pin_12: pinSource = GPIO_PinSource12; break; case GPIO_Pin_13: pinSource = GPIO_PinSource13; break; case GPIO_Pin_14: pinSource = GPIO_PinSource14; break; case GPIO_Pin_15: pinSource = GPIO_PinSource15; break; } GPIO_PinAFConfig(GPIOx, pinSource, GPIO_AF); GPIO_setup.GPIO_Pin = pinNumber; GPIO_setup.GPIO_Mode = GPIO_Mode_AF; GPIO_setup.GPIO_OType = pinType; GPIO_setup.GPIO_PuPd = pinPull; GPIO_setup.GPIO_Speed = pinSpeed; GPIO_Init(GPIOx, &GPIO_setup); }