wm_gpio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. u32 cpu_sr = 0;
  140. cpu_sr = tls_os_set_critical();
  141. // reg_en = tls_reg_read32(HR_GPIO_DATA_EN + offset);
  142. // tls_reg_write32(HR_GPIO_DATA_EN + offset, reg_en | (1 << pin));
  143. reg = tls_reg_read32(HR_GPIO_DATA + offset);
  144. // tls_reg_write32(HR_GPIO_DATA_EN + offset, reg_en);
  145. tls_os_release_critical(cpu_sr);
  146. if(reg & (0x1 << pin))
  147. return 1;
  148. else
  149. return 0;
  150. }
  151. /**
  152. * @brief This function is used to modify gpio status
  153. *
  154. * @param[in] gpio_pin gpio pin num
  155. * @param[in] value power level
  156. * 0: low power level
  157. * 1: high power level
  158. *
  159. * @return None
  160. *
  161. * @note None
  162. */
  163. __attribute__((section (".ram_run"))) void tls_gpio_write(enum tls_io_name gpio_pin, u8 value)
  164. {
  165. u32 cpu_sr = 0;
  166. u32 reg;
  167. u32 reg_en;
  168. u8 pin;
  169. u16 offset;
  170. if (gpio_pin >= WM_IO_PB_00)
  171. {
  172. pin = gpio_pin - WM_IO_PB_00;
  173. offset = TLS_IO_AB_OFFSET;
  174. }
  175. else
  176. {
  177. pin = gpio_pin;
  178. offset = 0;
  179. }
  180. cpu_sr = tls_os_set_critical();
  181. // reg_en = tls_reg_read32(HR_GPIO_DATA_EN + offset);
  182. // tls_reg_write32(HR_GPIO_DATA_EN + offset, reg_en | (1 << pin));
  183. reg = tls_reg_read32(HR_GPIO_DATA + offset);
  184. if(value)
  185. tls_reg_write32(HR_GPIO_DATA + offset, reg | (1 << pin)); /* write high */
  186. else
  187. tls_reg_write32(HR_GPIO_DATA + offset, reg & (~(1 << pin)));/* write low */
  188. // tls_reg_write32(HR_GPIO_DATA_EN + offset, reg_en);
  189. tls_os_release_critical(cpu_sr);
  190. }
  191. //add by hyj, 2022-05-20
  192. // 以极限速度输出io脉冲
  193. __attribute__((section (".ram_run"))) void tls_gpio_pulse(enum tls_io_name gpio_pin,u8* level,u16 len,u16 delay)
  194. {
  195. u32 cpu_sr = 0;
  196. u32 reg;
  197. u32 reg_en;
  198. u8 pin;
  199. u16 offset;
  200. u16 i;
  201. volatile u32 del=delay;
  202. if (gpio_pin >= WM_IO_PB_00)
  203. {
  204. pin = gpio_pin - WM_IO_PB_00;
  205. offset = TLS_IO_AB_OFFSET;
  206. }
  207. else
  208. {
  209. pin = gpio_pin;
  210. offset = 0;
  211. }
  212. cpu_sr = tls_os_set_critical();
  213. int GPIO_DATA = HR_GPIO_DATA + offset;
  214. // reg_en = tls_reg_read32(HR_GPIO_DATA_EN + offset);
  215. // tls_reg_write32(HR_GPIO_DATA_EN + offset, reg_en | (1 << pin));
  216. reg = tls_reg_read32(GPIO_DATA);
  217. u32 reg_high = reg | (1 << pin);
  218. u32 reg_low = reg & (~(1 << pin));
  219. for(i=0; i<len; i++)
  220. {
  221. if(level[i/8]&(0x80>>(i%8)))
  222. tls_reg_write32(GPIO_DATA, reg_high); /* write high */
  223. else
  224. tls_reg_write32(GPIO_DATA, reg_low); /* write low */
  225. del = delay;
  226. while(del--);
  227. }
  228. // tls_reg_write32(HR_GPIO_DATA_EN + offset, reg_en);
  229. tls_os_release_critical(cpu_sr);
  230. }
  231. /**
  232. * @brief This function is used to config gpio interrupt
  233. *
  234. * @param[in] gpio_pin gpio pin num
  235. * @param[in] mode interrupt trigger type
  236. *
  237. * @return None
  238. *
  239. * @note None
  240. */
  241. void tls_gpio_irq_enable(enum tls_io_name gpio_pin, enum tls_gpio_irq_trig mode)
  242. {
  243. u32 reg;
  244. u8 pin;
  245. u16 offset;
  246. u8 vec_no;
  247. if (gpio_pin >= WM_IO_PB_00)
  248. {
  249. pin = gpio_pin - WM_IO_PB_00;
  250. offset = TLS_IO_AB_OFFSET;
  251. vec_no = GPIOB_IRQn;
  252. }
  253. else
  254. {
  255. pin = gpio_pin;
  256. offset = 0;
  257. vec_no = GPIOA_IRQn;
  258. }
  259. // TLS_DBGPRT_INFO("\r\ntls_gpio_int_enable gpio pin =%d,mode==%d\r\n",gpio_pin,mode);
  260. switch(mode)
  261. {
  262. case WM_GPIO_IRQ_TRIG_RISING_EDGE:
  263. reg = tls_reg_read32(HR_GPIO_IS + offset);
  264. reg &= (~(0x1 << pin));
  265. // TLS_DBGPRT_INFO("\r\nrising edge is ret=%x\r\n",reg);
  266. tls_reg_write32(HR_GPIO_IS + offset, reg); /* 0 edge trigger */
  267. reg = tls_reg_read32(HR_GPIO_IBE + offset);
  268. reg &= (~(0x1 << pin));
  269. // TLS_DBGPRT_INFO("\r\nrising edge ibe ret=%x\r\n",reg);
  270. tls_reg_write32(HR_GPIO_IBE + offset, reg); /* 0 edge trigger */
  271. reg = tls_reg_read32(HR_GPIO_IEV + offset);
  272. reg |= (0x1 << pin);
  273. // TLS_DBGPRT_INFO("\r\nrising edge iev ret=%x\r\n",reg);
  274. tls_reg_write32(HR_GPIO_IEV + offset, reg); /* 1 rising edge trigger */
  275. break;
  276. case WM_GPIO_IRQ_TRIG_FALLING_EDGE:
  277. reg = tls_reg_read32(HR_GPIO_IS + offset);
  278. reg &= (~(0x1 << pin));
  279. // TLS_DBGPRT_INFO("\falling edge is ret=%x\n",reg);
  280. tls_reg_write32(HR_GPIO_IS + offset, reg); /* 0 edge trigger */
  281. reg = tls_reg_read32(HR_GPIO_IBE + offset);
  282. reg &= (~(0x1 << pin));
  283. // TLS_DBGPRT_INFO("\falling edge ibe ret=%x\n",reg);
  284. tls_reg_write32(HR_GPIO_IBE + offset, reg); /* 0 edge trigger */
  285. reg = tls_reg_read32(HR_GPIO_IEV + offset);
  286. reg &= (~(0x1 << pin));
  287. // TLS_DBGPRT_INFO("\falling edge iev ret=%x\n",reg);
  288. tls_reg_write32(HR_GPIO_IEV + offset, reg); /* 0 falling edge trigger */
  289. break;
  290. case WM_GPIO_IRQ_TRIG_DOUBLE_EDGE:
  291. reg = tls_reg_read32(HR_GPIO_IS + offset);
  292. tls_reg_write32(HR_GPIO_IS + offset, reg & (~(0x1 << pin))); /* 0 edge trigger */
  293. reg = tls_reg_read32(HR_GPIO_IBE + offset);
  294. tls_reg_write32(HR_GPIO_IBE + offset, reg | (0x1 << pin)); /* 1 double edge trigger */
  295. break;
  296. case WM_GPIO_IRQ_TRIG_HIGH_LEVEL:
  297. reg = tls_reg_read32(HR_GPIO_IS + offset);
  298. tls_reg_write32(HR_GPIO_IS + offset, reg | (0x1 << pin)); /* 1 level trigger */
  299. reg = tls_reg_read32(HR_GPIO_IEV + offset);
  300. tls_reg_write32(HR_GPIO_IEV + offset, reg | (0x1 << pin)); /* 1 high level trigger */
  301. break;
  302. case WM_GPIO_IRQ_TRIG_LOW_LEVEL:
  303. reg = tls_reg_read32(HR_GPIO_IS + offset);
  304. tls_reg_write32(HR_GPIO_IS + offset, reg | (0x1 << pin)); /* 1 level trigger */
  305. reg = tls_reg_read32(HR_GPIO_IEV + offset);
  306. tls_reg_write32(HR_GPIO_IEV + offset, reg & (~(0x1 << pin))); /* 0 low level trigger */
  307. break;
  308. }
  309. reg = tls_reg_read32(HR_GPIO_IE + offset);
  310. reg |= (0x1 << pin);
  311. // TLS_DBGPRT_INFO("\nie ret=%x\n",reg);
  312. tls_reg_write32(HR_GPIO_IE + offset, reg); /* enable interrupt */
  313. tls_irq_enable(vec_no);
  314. }
  315. /**
  316. * @brief This function is used to disable gpio interrupt
  317. *
  318. * @param[in] gpio_pin gpio pin num
  319. *
  320. * @return None
  321. *
  322. * @note None
  323. */
  324. void tls_gpio_irq_disable(enum tls_io_name gpio_pin)
  325. {
  326. u32 reg;
  327. u8 pin;
  328. u16 offset;
  329. if (gpio_pin >= WM_IO_PB_00)
  330. {
  331. pin = gpio_pin - WM_IO_PB_00;
  332. offset = TLS_IO_AB_OFFSET;
  333. reg = tls_reg_read32(HR_GPIO_IE + offset);
  334. tls_reg_write32(HR_GPIO_IE + offset, reg & (~(0x1 << pin))); /* disable interrupt */
  335. reg = reg&(~(0x1 << pin));
  336. if (reg == 0)
  337. {
  338. tls_irq_disable(GPIOB_IRQn);
  339. }
  340. }
  341. else
  342. {
  343. pin = gpio_pin;
  344. offset = 0;
  345. reg = tls_reg_read32(HR_GPIO_IE + offset);
  346. tls_reg_write32(HR_GPIO_IE + offset, reg & (~(0x1 << pin))); /* disable interrupt */
  347. reg = (reg&(~(0x1 << pin)))&0xFFFF;
  348. if (reg == 0)
  349. {
  350. tls_irq_disable(GPIOA_IRQn);
  351. }
  352. }
  353. }
  354. /**
  355. * @brief This function is used to get gpio interrupt status
  356. *
  357. * @param[in] gpio_pin gpio pin num
  358. *
  359. * @retval 0 no interrupt happened
  360. * @retval 1 interrupt happened
  361. *
  362. * @note None
  363. */
  364. u8 tls_get_gpio_irq_status(enum tls_io_name gpio_pin)
  365. {
  366. u32 reg;
  367. u8 pin;
  368. u16 offset;
  369. if (gpio_pin >= WM_IO_PB_00)
  370. {
  371. pin = gpio_pin - WM_IO_PB_00;
  372. offset = TLS_IO_AB_OFFSET;
  373. }
  374. else
  375. {
  376. pin = gpio_pin;
  377. offset = 0;
  378. }
  379. reg = tls_reg_read32(HR_GPIO_RIS + offset);
  380. if(reg & (0x1 << pin))
  381. return 1;
  382. else
  383. return 0;
  384. }
  385. /**
  386. * @brief This function is used to clear gpio interrupt flag
  387. *
  388. * @param[in] gpio_pin gpio pin num
  389. *
  390. * @return None
  391. *
  392. * @note None
  393. */
  394. void tls_clr_gpio_irq_status(enum tls_io_name gpio_pin)
  395. {
  396. u8 pin;
  397. u16 offset;
  398. if (gpio_pin >= WM_IO_PB_00)
  399. {
  400. pin = gpio_pin - WM_IO_PB_00;
  401. offset = TLS_IO_AB_OFFSET;
  402. }
  403. else
  404. {
  405. pin = gpio_pin;
  406. offset = 0;
  407. }
  408. tls_reg_write32(HR_GPIO_IC + offset, (0x1 << pin)); /* 1 clear interrupt status */
  409. }
  410. /**
  411. * @brief This function is used to register gpio interrupt
  412. *
  413. * @param[in] gpio_pin gpio pin num
  414. * @param[in] callback the gpio interrupt call back function
  415. * @param[in] arg parammeter for the callback
  416. *
  417. * @return None
  418. *
  419. * @note
  420. * gpio callback function is called in interrupt,
  421. * so can not operate the critical data in the callback fuuction,
  422. * recommendation to send messages to other tasks to operate it.
  423. */
  424. void tls_gpio_isr_register(enum tls_io_name gpio_pin,
  425. tls_gpio_irq_callback callback,
  426. void *arg)
  427. {
  428. gpio_context[gpio_pin].callback = callback;
  429. gpio_context[gpio_pin].arg = arg;
  430. }