drv_gpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-12-27 iysheng first version
  9. * 2021-01-01 iysheng support exti interrupt
  10. * 2021-09-07 FuC Suit for V85XX
  11. * 2021-09-09 ZhuXW Add GPIO interrupt
  12. * 2021-09-12 ZhuXW Suit for V85XXP
  13. */
  14. #include <board.h>
  15. #include "drv_gpio.h"
  16. #ifdef RT_USING_PIN
  17. #if defined(GPIOF)
  18. #define __V85XXP_PORT_MAX 6u
  19. #elif defined(GPIOE)
  20. #define __V85XXP_PORT_MAX 5u
  21. #elif defined(GPIOD)
  22. #define __V85XXP_PORT_MAX 4u
  23. #elif defined(GPIOC)
  24. #define __V85XXP_PORT_MAX 3u
  25. #elif defined(GPIOB)
  26. #define __V85XXP_PORT_MAX 2u
  27. #elif defined(GPIOA)
  28. #define __V85XXP_PORT_MAX 1u
  29. #else
  30. #define __V85XXP_PORT_MAX 0u
  31. #error Unsupported V85XXP GPIO peripheral.
  32. #endif
  33. #define PIN_V85XXPPORT_MAX __V85XXP_PORT_MAX
  34. #define PIN_V85XXPPORT_A 0u
  35. static const struct pin_irq_map pin_irq_map[] =
  36. {
  37. #if defined(SOC_SERIES_V85XXP)
  38. {GPIO_Pin_0, PMU_IRQn},
  39. {GPIO_Pin_1, PMU_IRQn},
  40. {GPIO_Pin_2, PMU_IRQn},
  41. {GPIO_Pin_3, PMU_IRQn},
  42. {GPIO_Pin_4, PMU_IRQn},
  43. {GPIO_Pin_5, PMU_IRQn},
  44. {GPIO_Pin_6, PMU_IRQn},
  45. {GPIO_Pin_7, PMU_IRQn},
  46. {GPIO_Pin_8, PMU_IRQn},
  47. {GPIO_Pin_9, PMU_IRQn},
  48. {GPIO_Pin_10, PMU_IRQn},
  49. {GPIO_Pin_11, PMU_IRQn},
  50. {GPIO_Pin_12, PMU_IRQn},
  51. {GPIO_Pin_13, PMU_IRQn},
  52. {GPIO_Pin_14, PMU_IRQn},
  53. {GPIO_Pin_15, PMU_IRQn},
  54. #else
  55. #error "Unsupported soc series"
  56. #endif
  57. };
  58. static struct rt_pin_irq_hdr pin_irq_hdr_tab[] =
  59. {
  60. {-1, 0, RT_NULL, RT_NULL},
  61. {-1, 0, RT_NULL, RT_NULL},
  62. {-1, 0, RT_NULL, RT_NULL},
  63. {-1, 0, RT_NULL, RT_NULL},
  64. {-1, 0, RT_NULL, RT_NULL},
  65. {-1, 0, RT_NULL, RT_NULL},
  66. {-1, 0, RT_NULL, RT_NULL},
  67. {-1, 0, RT_NULL, RT_NULL},
  68. {-1, 0, RT_NULL, RT_NULL},
  69. {-1, 0, RT_NULL, RT_NULL},
  70. {-1, 0, RT_NULL, RT_NULL},
  71. {-1, 0, RT_NULL, RT_NULL},
  72. {-1, 0, RT_NULL, RT_NULL},
  73. {-1, 0, RT_NULL, RT_NULL},
  74. {-1, 0, RT_NULL, RT_NULL},
  75. {-1, 0, RT_NULL, RT_NULL},
  76. };
  77. static uint32_t pin_irq_enable_mask = 0;
  78. #define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
  79. static rt_base_t V85XXP_pin_get(const char *name)
  80. {
  81. rt_base_t pin = 0;
  82. int hw_port_num, hw_pin_num = 0;
  83. int i, name_len;
  84. name_len = rt_strlen(name);
  85. if ((name_len < 4) || (name_len >= 6))
  86. {
  87. return -RT_EINVAL;
  88. }
  89. if ((name[0] != 'P') || (name[2] != '.'))
  90. {
  91. return -RT_EINVAL;
  92. }
  93. if ((name[1] >= 'A') && (name[1] <= 'F'))
  94. {
  95. hw_port_num = (int)(name[1] - 'A');
  96. }
  97. else
  98. {
  99. return -RT_EINVAL;
  100. }
  101. for (i = 3; i < name_len; i++)
  102. {
  103. hw_pin_num *= 10;
  104. hw_pin_num += name[i] - '0';
  105. }
  106. pin = PIN_NUM(hw_port_num, hw_pin_num);
  107. return pin;
  108. }
  109. static void V85XXP_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value)
  110. {
  111. GPIO_Type *gpio_port;
  112. uint16_t gpio_pin;
  113. if (PIN_PORT(pin) == PIN_V85XXPPORT_A)
  114. {
  115. gpio_pin = PIN_V85XXPPIN(pin);
  116. GPIOA_WriteBit(GPIOA, gpio_pin, (BitState)value);
  117. }
  118. else if (PIN_PORT(pin) < PIN_V85XXPPORT_MAX)
  119. {
  120. gpio_port = PIN_V85XXPPORT(pin);
  121. gpio_pin = PIN_V85XXPPIN(pin);
  122. GPIOBToF_WriteBit(gpio_port, gpio_pin, (BitState)value);
  123. }
  124. }
  125. static int V85XXP_pin_read(rt_device_t dev, rt_base_t pin)
  126. {
  127. GPIO_Type *gpio_port;
  128. uint16_t gpio_pin;
  129. int value = PIN_LOW;
  130. if (PIN_PORT(pin) == PIN_V85XXPPORT_A)
  131. {
  132. gpio_pin = PIN_V85XXPPIN(pin);
  133. value = GPIOA_ReadInputDataBit(GPIOA, gpio_pin);
  134. }
  135. else if (PIN_PORT(pin) < PIN_V85XXPPORT_MAX)
  136. {
  137. gpio_port = PIN_V85XXPPORT(pin);
  138. gpio_pin = PIN_V85XXPPIN(pin);
  139. value = GPIOBToF_ReadInputDataBit(gpio_port, gpio_pin);
  140. }
  141. return value;
  142. }
  143. static void V85XXP_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
  144. {
  145. GPIO_InitType GPIO_InitStruct = {0};
  146. if (PIN_PORT(pin) >= PIN_V85XXPPORT_MAX)
  147. {
  148. return;
  149. }
  150. /* Configure GPIO_InitStructure */
  151. GPIO_InitStruct.GPIO_Pin = PIN_V85XXPPIN(pin);
  152. GPIO_InitStruct.GPIO_Mode = GPIO_MODE_INPUT;
  153. switch (mode)
  154. {
  155. case PIN_MODE_OUTPUT:
  156. GPIO_InitStruct.GPIO_Mode = GPIO_MODE_OUTPUT_CMOS;
  157. break;
  158. case PIN_MODE_INPUT:
  159. GPIO_InitStruct.GPIO_Mode = GPIO_MODE_INPUT;
  160. break;
  161. case PIN_MODE_INPUT_PULLUP:
  162. GPIO_InitStruct.GPIO_Mode = GPIO_MODE_INOUT_CMOS;
  163. break;
  164. case PIN_MODE_INPUT_PULLDOWN:
  165. GPIO_InitStruct.GPIO_Mode = GPIO_MODE_INOUT_OD;
  166. break;
  167. case PIN_MODE_OUTPUT_OD:
  168. GPIO_InitStruct.GPIO_Mode = GPIO_MODE_INOUT_OD;
  169. break;
  170. default:
  171. break;
  172. }
  173. if (PIN_PORT(pin) == PIN_V85XXPPORT_A)
  174. {
  175. GPIOA_Init(GPIOA, &GPIO_InitStruct);
  176. }
  177. else if (PIN_PORT(pin) < PIN_V85XXPPORT_MAX)
  178. {
  179. GPIOBToF_Init(PIN_V85XXPPORT(pin), &GPIO_InitStruct);
  180. }
  181. }
  182. rt_inline rt_int32_t bit2bitno(rt_uint32_t bit)
  183. {
  184. int i;
  185. for (i = 0; i < 32; i++)
  186. {
  187. if ((0x01 << i) == bit)
  188. {
  189. return i;
  190. }
  191. }
  192. return -1;
  193. }
  194. static rt_err_t V85XXP_pin_attach_irq(struct rt_device *device, rt_int32_t pin,
  195. rt_uint32_t mode, void (*hdr)(void *args), void *args)
  196. {
  197. rt_base_t level;
  198. rt_int32_t irqindex = -1;
  199. if (PIN_PORT(pin) > PIN_V85XXPPORT_A)
  200. {
  201. return -RT_ENOSYS;
  202. }
  203. irqindex = bit2bitno(PIN_V85XXPPIN(pin));
  204. if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
  205. {
  206. return -RT_ENOSYS;
  207. }
  208. level = rt_hw_interrupt_disable();
  209. if (pin_irq_hdr_tab[irqindex].pin == pin &&
  210. pin_irq_hdr_tab[irqindex].hdr == hdr &&
  211. pin_irq_hdr_tab[irqindex].mode == mode &&
  212. pin_irq_hdr_tab[irqindex].args == args)
  213. {
  214. rt_hw_interrupt_enable(level);
  215. return RT_EOK;
  216. }
  217. if (pin_irq_hdr_tab[irqindex].pin != -1)
  218. {
  219. rt_hw_interrupt_enable(level);
  220. return -RT_EBUSY;
  221. }
  222. pin_irq_hdr_tab[irqindex].pin = pin;
  223. pin_irq_hdr_tab[irqindex].hdr = hdr;
  224. pin_irq_hdr_tab[irqindex].mode = mode;
  225. pin_irq_hdr_tab[irqindex].args = args;
  226. rt_hw_interrupt_enable(level);
  227. return RT_EOK;
  228. }
  229. static rt_err_t V85XXP_pin_detach_irq(struct rt_device *device, rt_int32_t pin)
  230. {
  231. rt_base_t level;
  232. rt_int32_t irqindex = -1;
  233. if (PIN_PORT(pin) > PIN_V85XXPPORT_A)
  234. {
  235. return -RT_ENOSYS;
  236. }
  237. irqindex = bit2bitno(PIN_V85XXPPIN(pin));
  238. if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
  239. {
  240. return -RT_ENOSYS;
  241. }
  242. level = rt_hw_interrupt_disable();
  243. if (pin_irq_hdr_tab[irqindex].pin == -1)
  244. {
  245. rt_hw_interrupt_enable(level);
  246. return RT_EOK;
  247. }
  248. pin_irq_hdr_tab[irqindex].pin = -1;
  249. pin_irq_hdr_tab[irqindex].hdr = RT_NULL;
  250. pin_irq_hdr_tab[irqindex].mode = 0;
  251. pin_irq_hdr_tab[irqindex].args = RT_NULL;
  252. rt_hw_interrupt_enable(level);
  253. return RT_EOK;
  254. }
  255. static rt_err_t V85XXP_pin_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled)
  256. {
  257. const struct pin_irq_map *irqmap;
  258. rt_base_t level;
  259. rt_int32_t irqindex = -1;
  260. GPIO_InitType GPIO_InitStruct = {0};
  261. if (PIN_PORT(pin) > PIN_V85XXPPORT_A)
  262. {
  263. return -RT_ENOSYS;
  264. }
  265. GPIO_InitStruct.GPIO_Pin = PIN_V85XXPPIN(pin);
  266. if (enabled == PIN_IRQ_ENABLE)
  267. {
  268. irqindex = bit2bitno(PIN_V85XXPPIN(pin));
  269. if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
  270. {
  271. return -RT_ENOSYS;
  272. }
  273. level = rt_hw_interrupt_disable();
  274. if (pin_irq_hdr_tab[irqindex].pin == -1)
  275. {
  276. rt_hw_interrupt_enable(level);
  277. return -RT_ENOSYS;
  278. }
  279. GPIO_InitStruct.GPIO_Mode = GPIO_MODE_INPUT;
  280. GPIO_InitStruct.GPIO_Pin = PIN_V85XXPPIN(pin);
  281. GPIOA_Init(GPIOA, &GPIO_InitStruct);
  282. irqmap = &pin_irq_map[irqindex];
  283. switch (pin_irq_hdr_tab[irqindex].mode)
  284. {
  285. case PIN_IRQ_MODE_RISING:
  286. PMU_WakeUpPinConfig(PIN_V85XXPPIN(pin), IOA_RISING);
  287. break;
  288. case PIN_IRQ_MODE_FALLING:
  289. PMU_WakeUpPinConfig(PIN_V85XXPPIN(pin), IOA_FALLING);
  290. break;
  291. case PIN_IRQ_MODE_RISING_FALLING:
  292. PMU_WakeUpPinConfig(PIN_V85XXPPIN(pin), IOA_EDGEBOTH);
  293. break;
  294. case PIN_IRQ_MODE_HIGH_LEVEL:
  295. PMU_WakeUpPinConfig(PIN_V85XXPPIN(pin), IOA_HIGH);
  296. break;
  297. case PIN_IRQ_MODE_LOW_LEVEL:
  298. PMU_WakeUpPinConfig(PIN_V85XXPPIN(pin), IOA_LOW);
  299. break;
  300. default:
  301. break;
  302. }
  303. PMU_INTConfig(PMU_INT_IOAEN, ENABLE);
  304. NVIC_SetPriority(irqmap->irqno, 0);
  305. NVIC_EnableIRQ(irqmap->irqno);
  306. pin_irq_enable_mask |= irqmap->pinbit;
  307. rt_hw_interrupt_enable(level);
  308. }
  309. else if (enabled == PIN_IRQ_DISABLE)
  310. {
  311. level = rt_hw_interrupt_disable();
  312. PMU_INTConfig(PMU_INT_IOAEN, DISABLE);
  313. NVIC_DisableIRQ(irqmap->irqno);
  314. rt_hw_interrupt_enable(level);
  315. }
  316. else
  317. {
  318. return -RT_ENOSYS;
  319. }
  320. return RT_EOK;
  321. }
  322. const static struct rt_pin_ops _V85XXP_pin_ops =
  323. {
  324. V85XXP_pin_mode,
  325. V85XXP_pin_write,
  326. V85XXP_pin_read,
  327. V85XXP_pin_attach_irq,
  328. V85XXP_pin_detach_irq,
  329. V85XXP_pin_irq_enable,
  330. V85XXP_pin_get,
  331. };
  332. rt_inline void pin_irq_hdr(int irqno)
  333. {
  334. if (pin_irq_hdr_tab[irqno].hdr)
  335. {
  336. pin_irq_hdr_tab[irqno].hdr(pin_irq_hdr_tab[irqno].args);
  337. }
  338. }
  339. void V85XXP_pin_exti_irqhandler()
  340. {
  341. rt_base_t intsts=0;
  342. int i=0;
  343. intsts = PMU_GetIOAAllINTStatus();
  344. for(i=0; i<16; i++)
  345. {
  346. if((1<<i) & intsts)
  347. {
  348. PMU_ClearIOAINTStatus(1<<i);
  349. pin_irq_hdr(bit2bitno(1<<i));
  350. return;
  351. }
  352. }
  353. }
  354. void PMU_IRQHandler()
  355. {
  356. rt_interrupt_enter();
  357. V85XXP_pin_exti_irqhandler();
  358. rt_interrupt_leave();
  359. }
  360. int rt_hw_pin_init(void)
  361. {
  362. GPIO_InitType GPIO_InitStruct;
  363. GPIO_InitStruct.GPIO_Mode = GPIO_MODE_INPUT;
  364. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_All;
  365. #if defined(GPIOF)
  366. GPIOBToF_Init(GPIOF, &GPIO_InitStruct);
  367. #endif
  368. #if defined(GPIOE)
  369. GPIOBToF_Init(GPIOE, &GPIO_InitStruct);
  370. #endif
  371. #if defined(GPIOD)
  372. GPIOBToF_Init(GPIOD, &GPIO_InitStruct);
  373. #endif
  374. #if defined(GPIOC)
  375. GPIOBToF_Init(GPIOC, &GPIO_InitStruct);
  376. #endif
  377. #if defined(GPIOB)
  378. GPIOBToF_Init(GPIOB, &GPIO_InitStruct);
  379. #endif
  380. #if defined(GPIOA)
  381. GPIOA_Init(GPIOA, &GPIO_InitStruct);
  382. #endif
  383. return rt_device_pin_register("pin", &_V85XXP_pin_ops, RT_NULL);
  384. }
  385. INIT_BOARD_EXPORT(rt_hw_pin_init);
  386. #endif /* RT_USING_PIN */