core_gpio.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "user.h"
  22. typedef struct
  23. {
  24. volatile GPIO_TypeDef *RegBase;
  25. const int32_t IrqLine;
  26. uint16_t ODBitMap;
  27. }GPIO_ResourceStruct;
  28. typedef struct
  29. {
  30. CBFuncEx_t CB;
  31. void *pParam;
  32. }EXTI_CBStruct;
  33. typedef struct
  34. {
  35. EXTI_CBStruct ExtiCB[GPIO_MAX];
  36. }GPIO_CtrlStruct;
  37. static GPIO_CtrlStruct prvGPIO;
  38. static GPIO_ResourceStruct prvGPIO_Resource[6] =
  39. {
  40. {
  41. GPIOA,
  42. EXTI0_IRQn,
  43. 0,
  44. },
  45. {
  46. GPIOB,
  47. EXTI1_IRQn,
  48. 0,
  49. },
  50. {
  51. GPIOC,
  52. EXTI2_IRQn,
  53. 0,
  54. },
  55. {
  56. GPIOD,
  57. EXTI3_IRQn,
  58. 0,
  59. },
  60. {
  61. GPIOE,
  62. EXTI4_IRQn,
  63. 0,
  64. },
  65. {
  66. GPIOF,
  67. EXTI5_IRQn,
  68. 0,
  69. },
  70. };
  71. static int32_t GPIO_IrqDummyCB(void *pData, void *pParam)
  72. {
  73. // DBG("%d", pData);
  74. return 0;
  75. }
  76. static void __FUNC_IN_RAM__ GPIO_IrqHandle(int32_t IrqLine, void *pData)
  77. {
  78. volatile uint32_t Port = (uint32_t)pData;
  79. volatile uint32_t Sn, i, Pin;
  80. CBFuncEx_t CB;
  81. if (GPIO->INTP_TYPE_STA[Port].INTP_STA)
  82. {
  83. Sn = GPIO->INTP_TYPE_STA[Port].INTP_STA;
  84. GPIO->INTP_TYPE_STA[Port].INTP_STA = 0xffff;
  85. Port = (Port << 4);
  86. for(i = 0; i < 16; i++)
  87. {
  88. if (Sn & (1 << i))
  89. {
  90. Pin = Port+i;
  91. //DBG("%d,%x,%x", Pin, prvGPIO.ExtiCB[Pin].CB, prvGPIO.ExtiCB[Pin].pParam);
  92. prvGPIO.ExtiCB[Pin].CB((void *)Pin, prvGPIO.ExtiCB[Pin].pParam);
  93. }
  94. }
  95. }
  96. ISR_Clear(IrqLine);
  97. }
  98. void GPIO_GlobalInit(CBFuncEx_t Fun)
  99. {
  100. uint32_t i;
  101. if (Fun)
  102. {
  103. for(i = 0; i < GPIO_MAX; i++)
  104. {
  105. prvGPIO.ExtiCB[i].CB = Fun;
  106. }
  107. }
  108. else
  109. {
  110. for(i = 0; i < GPIO_MAX; i++)
  111. {
  112. prvGPIO.ExtiCB[i].CB = GPIO_IrqDummyCB;
  113. }
  114. }
  115. for(i = 0; i < 6; i++)
  116. {
  117. GPIO->INTP_TYPE_STA[i].INTP_TYPE = 0;
  118. GPIO->INTP_TYPE_STA[i].INTP_STA = 0xffff;
  119. #ifdef __BUILD_OS__
  120. ISR_SetPriority(prvGPIO_Resource[i].IrqLine, IRQ_MAX_PRIORITY + 1);
  121. #else
  122. ISR_SetPriority(prvGPIO_Resource[i].IrqLine, 3);
  123. #endif
  124. ISR_SetHandler(prvGPIO_Resource[i].IrqLine, GPIO_IrqHandle, (void *)i);
  125. ISR_OnOff(prvGPIO_Resource[i].IrqLine, 1);
  126. }
  127. }
  128. void __FUNC_IN_RAM__ GPIO_Config(uint32_t Pin, uint8_t IsInput, uint8_t InitValue)
  129. {
  130. uint8_t Port = (Pin >> 4);
  131. uint8_t orgPin = Pin;
  132. Pin = 1 << (Pin & 0x0000000f);
  133. GPIO_Iomux(orgPin, 1);
  134. if (IsInput)
  135. {
  136. prvGPIO_Resource[Port].RegBase->OEN |= Pin;
  137. }
  138. else
  139. {
  140. prvGPIO_Resource[Port].RegBase->BSRR |= InitValue?Pin:(Pin << 16);
  141. prvGPIO_Resource[Port].RegBase->OEN &= ~Pin;
  142. }
  143. prvGPIO_Resource[Port].ODBitMap &= ~Pin;
  144. // DBG("%d, %x, %x, %x",Port, Pin, prvGPIO_Resource[Port].RegBase->OEN,prvGPIO_Resource[Port].RegBase->IODR);
  145. }
  146. void __FUNC_IN_RAM__ GPIO_ODConfig(uint32_t Pin, uint8_t InitValue)
  147. {
  148. uint8_t Port = (Pin >> 4);
  149. uint8_t orgPin = Pin;
  150. Pin = 1 << (Pin & 0x0000000f);
  151. GPIO_Iomux(orgPin, 1);
  152. if (InitValue)
  153. {
  154. prvGPIO_Resource[Port].RegBase->OEN |= Pin;
  155. prvGPIO_Resource[Port].RegBase->PUE |= Pin;
  156. }
  157. else
  158. {
  159. prvGPIO_Resource[Port].RegBase->PUE &= ~Pin;
  160. prvGPIO_Resource[Port].RegBase->BSRR |= (Pin << 16);
  161. prvGPIO_Resource[Port].RegBase->OEN &= ~Pin;
  162. }
  163. prvGPIO_Resource[Port].ODBitMap |= Pin;
  164. // DBG("%d, %x, %x, %x",Port, Pin, prvGPIO_Resource[Port].RegBase->OEN, prvGPIO_Resource[Port].RegBase->IODR);
  165. }
  166. void __FUNC_IN_RAM__ GPIO_PullConfig(uint32_t Pin, uint8_t IsPull, uint8_t IsUp)
  167. {
  168. uint8_t Port = (Pin >> 4);
  169. Pin = 1 << (Pin & 0x0000000f);
  170. if (IsPull && IsUp)
  171. {
  172. prvGPIO_Resource[Port].RegBase->PUE |= Pin;
  173. }
  174. else
  175. {
  176. prvGPIO_Resource[Port].RegBase->PUE &= ~Pin;
  177. }
  178. }
  179. void GPIO_ExtiConfig(uint32_t Pin, uint8_t IsLevel, uint8_t IsRiseHigh, uint8_t IsFallLow)
  180. {
  181. uint8_t Port = (Pin >> 4);
  182. uint32_t Type = 0;
  183. uint32_t Mask = ~(0x03 << ((Pin & 0x0000000f) * 2));
  184. if (!IsLevel)
  185. {
  186. if (IsRiseHigh && IsFallLow)
  187. {
  188. Type = 0x03 << ((Pin & 0x0000000f) * 2);
  189. }
  190. else if (IsFallLow)
  191. {
  192. Type = 0x02 << ((Pin & 0x0000000f) * 2);
  193. }
  194. else if (IsRiseHigh)
  195. {
  196. Type = 0x01 << ((Pin & 0x0000000f) * 2);
  197. }
  198. }
  199. GPIO->INTP_TYPE_STA[Port].INTP_TYPE = (GPIO->INTP_TYPE_STA[Port].INTP_TYPE & Mask) | Type;
  200. uint32_t Sn = Pin / 32;
  201. uint32_t Pos = 1 << (Pin % 32);
  202. if (!IsLevel && (IsRiseHigh || IsFallLow))
  203. {
  204. switch(Sn)
  205. {
  206. case 0:
  207. GPIO->WAKE_P0_EN |= Pos;
  208. break;
  209. case 1:
  210. GPIO->WAKE_P1_EN |= Pos;
  211. break;
  212. case 2:
  213. GPIO->WAKE_P2_EN |= Pos;
  214. break;
  215. }
  216. }
  217. else
  218. {
  219. switch(Sn)
  220. {
  221. case 0:
  222. GPIO->WAKE_P0_EN &= ~Pos;
  223. break;
  224. case 1:
  225. GPIO->WAKE_P1_EN &= ~Pos;
  226. break;
  227. case 2:
  228. GPIO->WAKE_P2_EN &= ~Pos;
  229. break;
  230. }
  231. }
  232. }
  233. void GPIO_ExtiSetCB(uint32_t Pin, CBFuncEx_t CB, void *pParam)
  234. {
  235. if (CB)
  236. {
  237. prvGPIO.ExtiCB[Pin].CB = CB;
  238. }
  239. else
  240. {
  241. prvGPIO.ExtiCB[Pin].CB = GPIO_IrqDummyCB;
  242. }
  243. prvGPIO.ExtiCB[Pin].pParam = pParam;
  244. }
  245. void __FUNC_IN_RAM__ GPIO_Iomux(uint32_t Pin, uint32_t Function)
  246. {
  247. uint8_t Port = (Pin >> 4);
  248. uint32_t Mask = ~(0x03 << ((Pin & 0x0000000f) * 2));
  249. Function = Function << ((Pin & 0x0000000f) * 2);
  250. GPIO->ALT[Port] = (GPIO->ALT[Port] & Mask) | Function;
  251. }
  252. void __FUNC_IN_RAM__ GPIO_Output(uint32_t Pin, uint8_t Level)
  253. {
  254. uint8_t Port = (Pin >> 4);
  255. Pin = 1 << (Pin & 0x0000000f);
  256. if (prvGPIO_Resource[Port].ODBitMap & Pin)
  257. {
  258. if (Level)
  259. {
  260. prvGPIO_Resource[Port].RegBase->PUE |= Pin;
  261. prvGPIO_Resource[Port].RegBase->OEN |= Pin;
  262. }
  263. else
  264. {
  265. prvGPIO_Resource[Port].RegBase->PUE &= ~Pin;
  266. prvGPIO_Resource[Port].RegBase->BSRR |= (Pin << 16);
  267. prvGPIO_Resource[Port].RegBase->OEN &= ~Pin;
  268. }
  269. }
  270. else
  271. {
  272. prvGPIO_Resource[Port].RegBase->BSRR |= Level?Pin:(Pin << 16);
  273. }
  274. // DBG("%d, %x, %x, %x",Port, Pin, prvGPIO_Resource[Port].RegBase->OEN, prvGPIO_Resource[Port].RegBase->IODR);
  275. // DBG("%d, %x, %x, %x",Port, Pin, prvGPIO_Resource[Port].RegBase, prvGPIO_Resource[Port].RegBase->IODR);
  276. }
  277. uint8_t __FUNC_IN_RAM__ GPIO_Input(uint32_t Pin)
  278. {
  279. uint8_t Port = (Pin >> 4);
  280. Pin = 1 << (Pin & 0x0000000f);
  281. return (prvGPIO_Resource[Port].RegBase->IODR & (Pin << 16))?1:0;
  282. }
  283. void __FUNC_IN_RAM__ GPIO_Toggle(uint32_t Pin)
  284. {
  285. uint8_t Port = (Pin >> 4);
  286. Pin = 1 << (Pin & 0x0000000f);
  287. prvGPIO_Resource[Port].RegBase->IODR ^= Pin;
  288. }
  289. void __FUNC_IN_RAM__ GPIO_OutputMulti(uint32_t Port, uint32_t Pins, uint32_t Level)
  290. {
  291. prvGPIO_Resource[Port].RegBase->BSRR |= Level?Pins:(Pins << 16);
  292. }
  293. uint32_t __FUNC_IN_RAM__ GPIO_InputMulti(uint32_t Port)
  294. {
  295. return (prvGPIO_Resource[Port].RegBase->IODR >> 16);
  296. }
  297. void __FUNC_IN_RAM__ GPIO_ToggleMulti(uint32_t Port, uint32_t Pins)
  298. {
  299. prvGPIO_Resource[Port].RegBase->IODR ^= (Pins);
  300. }
  301. void __FUNC_IN_RAM__ GPIO_OutPulse(uint32_t Pin, uint8_t *Data, uint16_t BitLen, uint16_t Delay)
  302. {
  303. int i, j;
  304. uint8_t table[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
  305. uint8_t Port = (Pin >> 4);
  306. Pin = 1 << (Pin & 0x0000000f);
  307. __disable_irq();
  308. if (Delay)
  309. {
  310. for(i = 0;i < BitLen; i++)
  311. {
  312. j = Delay;
  313. prvGPIO_Resource[Port].RegBase->BSRR |= (Data[i >> 3] & (table[i & 0x07]))?Pin:(Pin << 16);
  314. while(j > 0) {j--;}
  315. }
  316. }
  317. else
  318. {
  319. for(i = 0;i < BitLen; i++)
  320. {
  321. prvGPIO_Resource[Port].RegBase->BSRR |= (Data[i >> 3] & (table[i & 0x07]))?Pin:(Pin << 16);
  322. }
  323. }
  324. __enable_irq();
  325. }