123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- #include "drv_gpio.h"
- #include "pin.h"
- #include "stm32f4xx_gpio.h"
- #define PIN_NUM(port, no) (((((port)&0xFu) << 4) | ((no)&0xFu)))
- #define PIN_PORT(pin) ((uint8_t)(((pin) >> 4) & 0xFu))
- #define PIN_NO(pin) ((uint8_t)((pin)&0xFu))
- #define PIN_STPORT(pin) ((GPIO_TypeDef *)(GPIOA_BASE + (0x400u * PIN_PORT(pin))))
- #define PIN_STPIN(pin) ((uint16_t)(1u << PIN_NO(pin)))
- #define PIN_STPORT_MAX 16
- #define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
- rt_size_t rt_strlen(const char *s)
- {
- const char *sc;
- for (sc = s; *sc != '\0'; ++sc) /* nothing */
- ;
- return sc - s;
- }
- rt_base_t stm32_pin_get(const char *name)
- {
- rt_base_t pin = 0;
- int hw_port_num, hw_pin_num = 0;
- int i, name_len;
- name_len = rt_strlen(name);
- if ((name_len < 4) || (name_len >= 6))
- {
- return -RT_EINVAL;
- }
- if ((name[0] != 'P') || (name[2] != '.'))
- {
- return -RT_EINVAL;
- }
- if ((name[1] >= 'A') && (name[1] <= 'Z'))
- {
- hw_port_num = (int)(name[1] - 'A');
- }
- else
- {
- return -RT_EINVAL;
- }
- for (i = 3; i < name_len; i++)
- {
- hw_pin_num *= 10;
- hw_pin_num += name[i] - '0';
- }
- pin = PIN_NUM(hw_port_num, hw_pin_num);
- return pin;
- }
- void rt_pin_write(rt_base_t pin, rt_base_t value)
- {
- GPIO_TypeDef *gpio_port;
- uint16_t gpio_pin;
- if (PIN_PORT(pin) < PIN_STPORT_MAX)
- {
- gpio_port = PIN_STPORT(pin);
- gpio_pin = PIN_STPIN(pin);
- GPIO_WriteBit(gpio_port, gpio_pin, value);
- }
- }
- int rt_pin_read(rt_base_t pin)
- {
- GPIO_TypeDef *gpio_port;
- uint16_t gpio_pin;
- int value = PIN_LOW;
- if (PIN_PORT(pin) < PIN_STPORT_MAX)
- {
- gpio_port = PIN_STPORT(pin);
- gpio_pin = PIN_STPIN(pin);
- value = GPIO_ReadInputDataBit(gpio_port, gpio_pin);
- }
- return value;
- }
- void rt_pin_mode(rt_base_t pin, rt_base_t mode)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- if (PIN_PORT(pin) >= PIN_STPORT_MAX)
- {
- return;
- }
- /* Configure GPIO_InitStructure */
- GPIO_InitStruct.GPIO_Pin = PIN_STPIN(pin);
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStruct.GPIO_Speed = GPIO_Fast_Speed;
- GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
- if (mode == PIN_MODE_OUTPUT)
- {
- /* output setting */
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
- }
- else if (mode == PIN_MODE_INPUT)
- {
- /* input setting: not GPIO_OType. */
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
- GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
- }
- else if (mode == PIN_MODE_INPUT_PULLUP)
- {
- /* input setting: GPIO_OType up. */
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
- GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
- }
- else if (mode == PIN_MODE_INPUT_PULLDOWN)
- {
- /* input setting: GPIO_OType down. */
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
- GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN;
- }
- else if (mode == PIN_MODE_OUTPUT_OD)
- {
- /* output setting: od. */
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
- GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
- }
- GPIO_Init(PIN_STPORT(pin), &GPIO_InitStruct);
- }
- rt_inline rt_int32_t bit2bitno(rt_uint32_t bit)
- {
- int i;
- for (i = 0; i < 32; i++)
- {
- if ((0x01 << i) == bit)
- {
- return i;
- }
- }
- return -1;
- }
- int rt_hw_pin_init(void)
- {
- #if defined(RCC_GPIOA_CLK_ENABLE)
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
- #endif
- #if defined(RCC_GPIOB_CLK_ENABLE)
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
- #endif
- #if defined(RCC_GPIOC_CLK_ENABLE)
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
- #endif
- #if defined(RCC_GPIOD_CLK_ENABLE)
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
- #endif
- #if defined(RCC_GPIOE_CLK_ENABLE)
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
- #endif
- #if defined(RCC_GPIOF_CLK_ENABLE)
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
- #endif
- #if defined(RCC_GPIOG_CLK_ENABLE)
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
- #endif
- #if defined(RCC_GPIOH_CLK_ENABLE)
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH, ENABLE);
- #endif
- #if defined(RCC_GPIOI_CLK_ENABLE)
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOI, ENABLE);
- #endif
- return 0;
- }
|