wm_gpio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /**
  2. * @file wm_gpio.c
  3. *
  4. * @brief GPIO Driver Module
  5. *
  6. * @author dave
  7. *
  8. * Copyright (c) 2014 Winner Microelectronics Co., Ltd.
  9. */
  10. #include "wm_gpio.h"
  11. #include "wm_regs.h"
  12. #include "wm_irq.h"
  13. #include "wm_osal.h"
  14. #include "tls_common.h"
  15. struct gpio_irq_context{
  16. tls_gpio_irq_callback callback;
  17. void *arg;
  18. };
  19. static struct gpio_irq_context gpio_context[WM_IO_PB_31 - WM_IO_PA_00 + 1] = {{0,0}};
  20. ATTRIBUTE_ISR void GPIOA_IRQHandler(void)
  21. {
  22. u8 i = 0;
  23. u8 found = 0;
  24. u32 reg = 0;
  25. csi_kernel_intrpt_enter();
  26. reg = tls_reg_read32(HR_GPIO_MIS);
  27. for (i = 0; i <= WM_IO_PA_15; i++)
  28. {
  29. if (reg & BIT(i))
  30. {
  31. found = 1;
  32. break;
  33. }
  34. }
  35. if (found)
  36. {
  37. if (NULL != gpio_context[i].callback)
  38. gpio_context[i].callback(gpio_context[i].arg);
  39. }
  40. csi_kernel_intrpt_exit();
  41. }
  42. ATTRIBUTE_ISR void GPIOB_IRQHandler(void)
  43. {
  44. u8 i = 0;
  45. u8 found = 0;
  46. u32 reg = 0;
  47. csi_kernel_intrpt_enter();
  48. reg = tls_reg_read32(HR_GPIO_MIS + TLS_IO_AB_OFFSET);
  49. for (i = WM_IO_PB_00; i <= WM_IO_PB_31; i++)
  50. {
  51. if (reg & BIT(i - WM_IO_PB_00))
  52. {
  53. found = 1;
  54. break;
  55. }
  56. }
  57. if (found)
  58. {
  59. if (NULL != gpio_context[i].callback)
  60. gpio_context[i].callback(gpio_context[i].arg);
  61. }
  62. csi_kernel_intrpt_exit();
  63. }
  64. /**
  65. * @brief This function is used to config gpio function
  66. *
  67. * @param[in] gpio_pin gpio pin num
  68. * @param[in] dir gpio direction
  69. * @param[in] attr gpio attribute
  70. *
  71. * @return None
  72. *
  73. * @note
  74. */
  75. void tls_gpio_cfg(enum tls_io_name gpio_pin, enum tls_gpio_dir dir, enum tls_gpio_attr attr)
  76. {
  77. u8 pin;
  78. u16 offset;
  79. if (gpio_pin >= WM_IO_PB_00)
  80. {
  81. pin = gpio_pin - WM_IO_PB_00;
  82. offset = TLS_IO_AB_OFFSET;
  83. }
  84. else
  85. {
  86. pin = gpio_pin;
  87. offset = 0;
  88. }
  89. /* enable gpio function */
  90. tls_io_cfg_set(gpio_pin, WM_IO_OPT5_GPIO);
  91. /* gpio direction */
  92. if (WM_GPIO_DIR_OUTPUT == dir)
  93. tls_reg_write32(HR_GPIO_DIR + offset, tls_reg_read32(HR_GPIO_DIR + offset) | BIT(pin)); /* 1 set output */
  94. else if (WM_GPIO_DIR_INPUT == dir)
  95. tls_reg_write32(HR_GPIO_DIR + offset, tls_reg_read32(HR_GPIO_DIR + offset) & (~BIT(pin))); /* 0 set input */
  96. /* gpio attribute */
  97. if (WM_GPIO_ATTR_FLOATING == attr)
  98. {
  99. tls_reg_write32(HR_GPIO_PULLUP_EN + offset, tls_reg_read32(HR_GPIO_PULLUP_EN + offset) | BIT(pin)); /* 1 disable pullup */
  100. tls_reg_write32(HR_GPIO_PULLDOWN_EN + offset, tls_reg_read32(HR_GPIO_PULLDOWN_EN + offset)&(~BIT(pin))); /* 1 disable pulldown */
  101. }
  102. if (WM_GPIO_ATTR_PULLHIGH == attr)
  103. {
  104. tls_reg_write32(HR_GPIO_PULLUP_EN + offset, tls_reg_read32(HR_GPIO_PULLUP_EN + offset) & (~BIT(pin))); /* 0 enable pullup */
  105. tls_reg_write32(HR_GPIO_PULLDOWN_EN + offset, tls_reg_read32(HR_GPIO_PULLDOWN_EN + offset) &(~BIT(pin))); /* 0 disable pulldown */
  106. }
  107. if (WM_GPIO_ATTR_PULLLOW == attr)
  108. {
  109. tls_reg_write32(HR_GPIO_PULLUP_EN + offset, tls_reg_read32(HR_GPIO_PULLUP_EN + offset) | BIT(pin)); /* 0 disable pullup */
  110. tls_reg_write32(HR_GPIO_PULLDOWN_EN + offset, tls_reg_read32(HR_GPIO_PULLDOWN_EN + offset) | BIT(pin)); /* 1 disable pulldown */
  111. }
  112. }
  113. /**
  114. * @brief This function is used to read gpio status
  115. *
  116. * @param[in] gpio_pin gpio pin num
  117. *
  118. * @retval 0 power level is low
  119. * @retval 1 power level is high
  120. *
  121. * @note None
  122. */
  123. __attribute__((section (".ram_run"))) u8 tls_gpio_read(enum tls_io_name gpio_pin)
  124. {
  125. u32 reg_en;
  126. u32 reg;
  127. u8 pin;
  128. u16 offset;
  129. if (gpio_pin >= WM_IO_PB_00)
  130. {
  131. pin = gpio_pin - WM_IO_PB_00;
  132. offset = TLS_IO_AB_OFFSET;
  133. }
  134. else
  135. {
  136. pin = gpio_pin;
  137. offset = 0;
  138. }
  139. // reg_en = tls_reg_read32(HR_GPIO_DATA_EN + offset);
  140. // tls_reg_write32(HR_GPIO_DATA_EN + offset, reg_en | (1 << pin));
  141. reg = tls_reg_read32(HR_GPIO_DATA + offset);
  142. // tls_reg_write32(HR_GPIO_DATA_EN + offset, reg_en);
  143. if(reg & (0x1 << pin))
  144. return 1;
  145. else
  146. return 0;
  147. }
  148. /**
  149. * @brief This function is used to modify gpio status
  150. *
  151. * @param[in] gpio_pin gpio pin num
  152. * @param[in] value power level
  153. * 0: low power level
  154. * 1: high power level
  155. *
  156. * @return None
  157. *
  158. * @note None
  159. */
  160. __attribute__((section (".ram_run"))) void tls_gpio_write(enum tls_io_name gpio_pin, u8 value)
  161. {
  162. u32 cpu_sr = 0;
  163. u32 reg;
  164. u32 reg_en;
  165. u8 pin;
  166. u16 offset;
  167. if (gpio_pin >= WM_IO_PB_00)
  168. {
  169. pin = gpio_pin - WM_IO_PB_00;
  170. offset = TLS_IO_AB_OFFSET;
  171. }
  172. else
  173. {
  174. pin = gpio_pin;
  175. offset = 0;
  176. }
  177. // cpu_sr = tls_os_set_critical();
  178. // reg_en = tls_reg_read32(HR_GPIO_DATA_EN + offset);
  179. // tls_reg_write32(HR_GPIO_DATA_EN + offset, reg_en | (1 << pin));
  180. reg = tls_reg_read32(HR_GPIO_DATA + offset);
  181. if(value)
  182. tls_reg_write32(HR_GPIO_DATA + offset, reg | (1 << pin)); /* write high */
  183. else
  184. tls_reg_write32(HR_GPIO_DATA + offset, reg & (~(1 << pin)));/* write low */
  185. // tls_reg_write32(HR_GPIO_DATA_EN + offset, reg_en);
  186. // tls_os_release_critical(cpu_sr);
  187. }
  188. //add by hyj, 2022-05-20
  189. // 以极限速度输出io脉冲
  190. __attribute__((section (".ram_run"))) void tls_gpio_pulse(enum tls_io_name gpio_pin,u8* level,u16 len,u16 delay)
  191. {
  192. u32 cpu_sr = 0;
  193. u32 reg;
  194. u32 reg_en;
  195. u8 pin;
  196. u16 offset;
  197. u16 i;
  198. volatile u32 del=delay;
  199. if (gpio_pin >= WM_IO_PB_00)
  200. {
  201. pin = gpio_pin - WM_IO_PB_00;
  202. offset = TLS_IO_AB_OFFSET;
  203. }
  204. else
  205. {
  206. pin = gpio_pin;
  207. offset = 0;
  208. }
  209. cpu_sr = tls_os_set_critical();
  210. int GPIO_DATA = HR_GPIO_DATA + offset;
  211. // reg_en = tls_reg_read32(HR_GPIO_DATA_EN + offset);
  212. // tls_reg_write32(HR_GPIO_DATA_EN + offset, reg_en | (1 << pin));
  213. reg = tls_reg_read32(GPIO_DATA);
  214. u32 reg_high = reg | (1 << pin);
  215. u32 reg_low = reg & (~(1 << pin));
  216. for(i=0; i<len; i++)
  217. {
  218. if(level[i/8]&(0x80>>(i%8)))
  219. tls_reg_write32(GPIO_DATA, reg_high); /* write high */
  220. else
  221. tls_reg_write32(GPIO_DATA, reg_low); /* write low */
  222. del = delay;
  223. while(del--);
  224. }
  225. // tls_reg_write32(HR_GPIO_DATA_EN + offset, reg_en);
  226. tls_os_release_critical(cpu_sr);
  227. }
  228. /**
  229. * @brief This function is used to config gpio interrupt
  230. *
  231. * @param[in] gpio_pin gpio pin num
  232. * @param[in] mode interrupt trigger type
  233. *
  234. * @return None
  235. *
  236. * @note None
  237. */
  238. void tls_gpio_irq_enable(enum tls_io_name gpio_pin, enum tls_gpio_irq_trig mode)
  239. {
  240. u32 reg;
  241. u8 pin;
  242. u16 offset;
  243. u8 vec_no;
  244. if (gpio_pin >= WM_IO_PB_00)
  245. {
  246. pin = gpio_pin - WM_IO_PB_00;
  247. offset = TLS_IO_AB_OFFSET;
  248. vec_no = GPIOB_IRQn;
  249. }
  250. else
  251. {
  252. pin = gpio_pin;
  253. offset = 0;
  254. vec_no = GPIOA_IRQn;
  255. }
  256. // TLS_DBGPRT_INFO("\r\ntls_gpio_int_enable gpio pin =%d,mode==%d\r\n",gpio_pin,mode);
  257. switch(mode)
  258. {
  259. case WM_GPIO_IRQ_TRIG_RISING_EDGE:
  260. reg = tls_reg_read32(HR_GPIO_IS + offset);
  261. reg &= (~(0x1 << pin));
  262. // TLS_DBGPRT_INFO("\r\nrising edge is ret=%x\r\n",reg);
  263. tls_reg_write32(HR_GPIO_IS + offset, reg); /* 0 edge trigger */
  264. reg = tls_reg_read32(HR_GPIO_IBE + offset);
  265. reg &= (~(0x1 << pin));
  266. // TLS_DBGPRT_INFO("\r\nrising edge ibe ret=%x\r\n",reg);
  267. tls_reg_write32(HR_GPIO_IBE + offset, reg); /* 0 edge trigger */
  268. reg = tls_reg_read32(HR_GPIO_IEV + offset);
  269. reg |= (0x1 << pin);
  270. // TLS_DBGPRT_INFO("\r\nrising edge iev ret=%x\r\n",reg);
  271. tls_reg_write32(HR_GPIO_IEV + offset, reg); /* 1 rising edge trigger */
  272. break;
  273. case WM_GPIO_IRQ_TRIG_FALLING_EDGE:
  274. reg = tls_reg_read32(HR_GPIO_IS + offset);
  275. reg &= (~(0x1 << pin));
  276. // TLS_DBGPRT_INFO("\falling edge is ret=%x\n",reg);
  277. tls_reg_write32(HR_GPIO_IS + offset, reg); /* 0 edge trigger */
  278. reg = tls_reg_read32(HR_GPIO_IBE + offset);
  279. reg &= (~(0x1 << pin));
  280. // TLS_DBGPRT_INFO("\falling edge ibe ret=%x\n",reg);
  281. tls_reg_write32(HR_GPIO_IBE + offset, reg); /* 0 edge trigger */
  282. reg = tls_reg_read32(HR_GPIO_IEV + offset);
  283. reg &= (~(0x1 << pin));
  284. // TLS_DBGPRT_INFO("\falling edge iev ret=%x\n",reg);
  285. tls_reg_write32(HR_GPIO_IEV + offset, reg); /* 0 falling edge trigger */
  286. break;
  287. case WM_GPIO_IRQ_TRIG_DOUBLE_EDGE:
  288. reg = tls_reg_read32(HR_GPIO_IS + offset);
  289. tls_reg_write32(HR_GPIO_IS + offset, reg & (~(0x1 << pin))); /* 0 edge trigger */
  290. reg = tls_reg_read32(HR_GPIO_IBE + offset);
  291. tls_reg_write32(HR_GPIO_IBE + offset, reg | (0x1 << pin)); /* 1 double edge trigger */
  292. break;
  293. case WM_GPIO_IRQ_TRIG_HIGH_LEVEL:
  294. reg = tls_reg_read32(HR_GPIO_IS + offset);
  295. tls_reg_write32(HR_GPIO_IS + offset, reg | (0x1 << pin)); /* 1 level trigger */
  296. reg = tls_reg_read32(HR_GPIO_IEV + offset);
  297. tls_reg_write32(HR_GPIO_IEV + offset, reg | (0x1 << pin)); /* 1 high level trigger */
  298. break;
  299. case WM_GPIO_IRQ_TRIG_LOW_LEVEL:
  300. reg = tls_reg_read32(HR_GPIO_IS + offset);
  301. tls_reg_write32(HR_GPIO_IS + offset, reg | (0x1 << pin)); /* 1 level trigger */
  302. reg = tls_reg_read32(HR_GPIO_IEV + offset);
  303. tls_reg_write32(HR_GPIO_IEV + offset, reg & (~(0x1 << pin))); /* 0 low level trigger */
  304. break;
  305. }
  306. reg = tls_reg_read32(HR_GPIO_IE + offset);
  307. reg |= (0x1 << pin);
  308. // TLS_DBGPRT_INFO("\nie ret=%x\n",reg);
  309. tls_reg_write32(HR_GPIO_IE + offset, reg); /* enable interrupt */
  310. tls_irq_enable(vec_no);
  311. }
  312. /**
  313. * @brief This function is used to disable gpio interrupt
  314. *
  315. * @param[in] gpio_pin gpio pin num
  316. *
  317. * @return None
  318. *
  319. * @note None
  320. */
  321. void tls_gpio_irq_disable(enum tls_io_name gpio_pin)
  322. {
  323. u32 reg;
  324. u8 pin;
  325. u16 offset;
  326. if (gpio_pin >= WM_IO_PB_00)
  327. {
  328. pin = gpio_pin - WM_IO_PB_00;
  329. offset = TLS_IO_AB_OFFSET;
  330. reg = tls_reg_read32(HR_GPIO_IE + offset);
  331. tls_reg_write32(HR_GPIO_IE + offset, reg & (~(0x1 << pin))); /* disable interrupt */
  332. // tls_irq_disable(GPIOB_IRQn);
  333. }
  334. else
  335. {
  336. pin = gpio_pin;
  337. offset = 0;
  338. reg = tls_reg_read32(HR_GPIO_IE + offset);
  339. tls_reg_write32(HR_GPIO_IE + offset, reg & (~(0x1 << pin))); /* disable interrupt */
  340. // tls_irq_disable(GPIOA_IRQn);
  341. }
  342. }
  343. /**
  344. * @brief This function is used to get gpio interrupt status
  345. *
  346. * @param[in] gpio_pin gpio pin num
  347. *
  348. * @retval 0 no interrupt happened
  349. * @retval 1 interrupt happened
  350. *
  351. * @note None
  352. */
  353. u8 tls_get_gpio_irq_status(enum tls_io_name gpio_pin)
  354. {
  355. u32 reg;
  356. u8 pin;
  357. u16 offset;
  358. if (gpio_pin >= WM_IO_PB_00)
  359. {
  360. pin = gpio_pin - WM_IO_PB_00;
  361. offset = TLS_IO_AB_OFFSET;
  362. }
  363. else
  364. {
  365. pin = gpio_pin;
  366. offset = 0;
  367. }
  368. reg = tls_reg_read32(HR_GPIO_RIS + offset);
  369. if(reg & (0x1 << pin))
  370. return 1;
  371. else
  372. return 0;
  373. }
  374. /**
  375. * @brief This function is used to clear gpio interrupt flag
  376. *
  377. * @param[in] gpio_pin gpio pin num
  378. *
  379. * @return None
  380. *
  381. * @note None
  382. */
  383. void tls_clr_gpio_irq_status(enum tls_io_name gpio_pin)
  384. {
  385. u8 pin;
  386. u16 offset;
  387. if (gpio_pin >= WM_IO_PB_00)
  388. {
  389. pin = gpio_pin - WM_IO_PB_00;
  390. offset = TLS_IO_AB_OFFSET;
  391. }
  392. else
  393. {
  394. pin = gpio_pin;
  395. offset = 0;
  396. }
  397. tls_reg_write32(HR_GPIO_IC + offset, (0x1 << pin)); /* 1 clear interrupt status */
  398. }
  399. /**
  400. * @brief This function is used to register gpio interrupt
  401. *
  402. * @param[in] gpio_pin gpio pin num
  403. * @param[in] callback the gpio interrupt call back function
  404. * @param[in] arg parammeter for the callback
  405. *
  406. * @return None
  407. *
  408. * @note
  409. * gpio callback function is called in interrupt,
  410. * so can not operate the critical data in the callback fuuction,
  411. * recommendation to send messages to other tasks to operate it.
  412. */
  413. void tls_gpio_isr_register(enum tls_io_name gpio_pin,
  414. tls_gpio_irq_callback callback,
  415. void *arg)
  416. {
  417. gpio_context[gpio_pin].callback = callback;
  418. gpio_context[gpio_pin].arg = arg;
  419. }