fsl_llwu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_llwu.h"
  35. /* Component ID definition, used by tools. */
  36. #ifndef FSL_COMPONENT_ID
  37. #define FSL_COMPONENT_ID "platform.drivers.llwu"
  38. #endif
  39. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN)
  40. void LLWU_SetExternalWakeupPinMode(LLWU_Type *base, uint32_t pinIndex, llwu_external_pin_mode_t pinMode)
  41. {
  42. #if (defined(FSL_FEATURE_LLWU_REG_BITWIDTH) && (FSL_FEATURE_LLWU_REG_BITWIDTH == 32))
  43. volatile uint32_t *regBase;
  44. uint32_t regOffset;
  45. uint32_t reg;
  46. switch (pinIndex >> 4U)
  47. {
  48. case 0U:
  49. regBase = &base->PE1;
  50. break;
  51. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 16))
  52. case 1U:
  53. regBase = &base->PE2;
  54. break;
  55. #endif
  56. default:
  57. regBase = NULL;
  58. break;
  59. }
  60. #else
  61. volatile uint8_t *regBase;
  62. uint8_t regOffset;
  63. uint8_t reg;
  64. switch (pinIndex >> 2U)
  65. {
  66. case 0U:
  67. regBase = &base->PE1;
  68. break;
  69. case 1U:
  70. regBase = &base->PE2;
  71. break;
  72. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 8))
  73. case 2U:
  74. regBase = &base->PE3;
  75. break;
  76. #endif
  77. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 12))
  78. case 3U:
  79. regBase = &base->PE4;
  80. break;
  81. #endif
  82. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 16))
  83. case 4U:
  84. regBase = &base->PE5;
  85. break;
  86. #endif
  87. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 20))
  88. case 5U:
  89. regBase = &base->PE6;
  90. break;
  91. #endif
  92. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 24))
  93. case 6U:
  94. regBase = &base->PE7;
  95. break;
  96. #endif
  97. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 28))
  98. case 7U:
  99. regBase = &base->PE8;
  100. break;
  101. #endif
  102. default:
  103. regBase = NULL;
  104. break;
  105. }
  106. #endif /* FSL_FEATURE_LLWU_REG_BITWIDTH == 32 */
  107. if (regBase)
  108. {
  109. reg = *regBase;
  110. #if (defined(FSL_FEATURE_LLWU_REG_BITWIDTH) && (FSL_FEATURE_LLWU_REG_BITWIDTH == 32))
  111. regOffset = ((pinIndex & 0x0FU) << 1U);
  112. #else
  113. regOffset = ((pinIndex & 0x03U) << 1U);
  114. #endif
  115. reg &= ~(0x3U << regOffset);
  116. reg |= ((uint32_t)pinMode << regOffset);
  117. *regBase = reg;
  118. }
  119. }
  120. bool LLWU_GetExternalWakeupPinFlag(LLWU_Type *base, uint32_t pinIndex)
  121. {
  122. #if (defined(FSL_FEATURE_LLWU_REG_BITWIDTH) && (FSL_FEATURE_LLWU_REG_BITWIDTH == 32))
  123. return (bool)(base->PF & (1U << pinIndex));
  124. #else
  125. volatile uint8_t *regBase;
  126. switch (pinIndex >> 3U)
  127. {
  128. #if (defined(FSL_FEATURE_LLWU_HAS_PF) && FSL_FEATURE_LLWU_HAS_PF)
  129. case 0U:
  130. regBase = &base->PF1;
  131. break;
  132. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 8))
  133. case 1U:
  134. regBase = &base->PF2;
  135. break;
  136. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  137. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 16))
  138. case 2U:
  139. regBase = &base->PF3;
  140. break;
  141. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  142. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 24))
  143. case 3U:
  144. regBase = &base->PF4;
  145. break;
  146. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  147. #else
  148. case 0U:
  149. regBase = &base->F1;
  150. break;
  151. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 8))
  152. case 1U:
  153. regBase = &base->F2;
  154. break;
  155. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  156. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 16))
  157. case 2U:
  158. regBase = &base->F3;
  159. break;
  160. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  161. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 24))
  162. case 3U:
  163. regBase = &base->F4;
  164. break;
  165. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  166. #endif /* FSL_FEATURE_LLWU_HAS_PF */
  167. default:
  168. regBase = NULL;
  169. break;
  170. }
  171. if (regBase)
  172. {
  173. return (bool)(*regBase & (1U << pinIndex % 8));
  174. }
  175. else
  176. {
  177. return false;
  178. }
  179. #endif /* FSL_FEATURE_LLWU_REG_BITWIDTH */
  180. }
  181. void LLWU_ClearExternalWakeupPinFlag(LLWU_Type *base, uint32_t pinIndex)
  182. {
  183. #if (defined(FSL_FEATURE_LLWU_REG_BITWIDTH) && (FSL_FEATURE_LLWU_REG_BITWIDTH == 32))
  184. base->PF = (1U << pinIndex);
  185. #else
  186. volatile uint8_t *regBase;
  187. switch (pinIndex >> 3U)
  188. {
  189. #if (defined(FSL_FEATURE_LLWU_HAS_PF) && FSL_FEATURE_LLWU_HAS_PF)
  190. case 0U:
  191. regBase = &base->PF1;
  192. break;
  193. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 8))
  194. case 1U:
  195. regBase = &base->PF2;
  196. break;
  197. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  198. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 16))
  199. case 2U:
  200. regBase = &base->PF3;
  201. break;
  202. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  203. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 24))
  204. case 3U:
  205. regBase = &base->PF4;
  206. break;
  207. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  208. #else
  209. case 0U:
  210. regBase = &base->F1;
  211. break;
  212. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 8))
  213. case 1U:
  214. regBase = &base->F2;
  215. break;
  216. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  217. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 16))
  218. case 2U:
  219. regBase = &base->F3;
  220. break;
  221. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  222. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 24))
  223. case 3U:
  224. regBase = &base->F4;
  225. break;
  226. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  227. #endif /* FSL_FEATURE_LLWU_HAS_PF */
  228. default:
  229. regBase = NULL;
  230. break;
  231. }
  232. if (regBase)
  233. {
  234. *regBase = (1U << pinIndex % 8U);
  235. }
  236. #endif /* FSL_FEATURE_LLWU_REG_BITWIDTH */
  237. }
  238. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  239. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && FSL_FEATURE_LLWU_HAS_PIN_FILTER)
  240. void LLWU_SetPinFilterMode(LLWU_Type *base, uint32_t filterIndex, llwu_external_pin_filter_mode_t filterMode)
  241. {
  242. #if (defined(FSL_FEATURE_LLWU_REG_BITWIDTH) && (FSL_FEATURE_LLWU_REG_BITWIDTH == 32))
  243. base->FILT = ((base->FILT) & ~((LLWU_FILT_FILTSEL1_MASK | LLWU_FILT_FILTE1_MASK) << ((filterIndex - 1U) * 8U))) |
  244. ((LLWU_FILT_FILTSEL1(filterMode.pinIndex) | LLWU_FILT_FILTE1(filterMode.filterMode))
  245. << ((filterIndex - 1U) * 8U)) |
  246. LLWU_FILT_FILTF1_MASK /* W1C to clear the FILTF flag bit. */
  247. ;
  248. #else
  249. volatile uint8_t *regBase;
  250. switch (filterIndex)
  251. {
  252. case 1U:
  253. regBase = &base->FILT1;
  254. break;
  255. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 1))
  256. case 2U:
  257. regBase = &base->FILT2;
  258. break;
  259. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER */
  260. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 2))
  261. case 3U:
  262. regBase = &base->FILT3;
  263. break;
  264. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER */
  265. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 3))
  266. case 4U:
  267. regBase = &base->FILT4;
  268. break;
  269. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER */
  270. default:
  271. regBase = NULL;
  272. break;
  273. }
  274. if (NULL != regBase)
  275. {
  276. *regBase = (*regBase & ~(LLWU_FILT1_FILTSEL_MASK | LLWU_FILT1_FILTE_MASK)) |
  277. LLWU_FILT1_FILTSEL(filterMode.pinIndex) | LLWU_FILT1_FILTE(filterMode.filterMode) |
  278. LLWU_FILT1_FILTF_MASK /* W1C to clear the FILTF flag bit. */
  279. ;
  280. }
  281. #endif /* FSL_FEATURE_LLWU_REG_BITWIDTH */
  282. }
  283. bool LLWU_GetPinFilterFlag(LLWU_Type *base, uint32_t filterIndex)
  284. {
  285. #if (defined(FSL_FEATURE_LLWU_REG_BITWIDTH) && (FSL_FEATURE_LLWU_REG_BITWIDTH == 32))
  286. return (bool)(base->FILT & (1U << (filterIndex * 8U - 1)));
  287. #else
  288. bool status = false;
  289. switch (filterIndex)
  290. {
  291. case 1:
  292. status = (base->FILT1 & LLWU_FILT1_FILTF_MASK);
  293. break;
  294. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 1))
  295. case 2:
  296. status = (base->FILT2 & LLWU_FILT2_FILTF_MASK);
  297. break;
  298. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER */
  299. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 2))
  300. case 3:
  301. status = (base->FILT3 & LLWU_FILT3_FILTF_MASK);
  302. break;
  303. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER */
  304. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 3))
  305. case 4:
  306. status = (base->FILT4 & LLWU_FILT4_FILTF_MASK);
  307. break;
  308. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER */
  309. default:
  310. break;
  311. }
  312. return status;
  313. #endif /* FSL_FEATURE_LLWU_REG_BITWIDTH */
  314. }
  315. void LLWU_ClearPinFilterFlag(LLWU_Type *base, uint32_t filterIndex)
  316. {
  317. #if (defined(FSL_FEATURE_LLWU_REG_BITWIDTH) && (FSL_FEATURE_LLWU_REG_BITWIDTH == 32))
  318. uint32_t reg;
  319. reg = base->FILT;
  320. switch (filterIndex)
  321. {
  322. case 1:
  323. reg |= LLWU_FILT_FILTF1_MASK;
  324. break;
  325. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 1))
  326. case 2:
  327. reg |= LLWU_FILT_FILTF2_MASK;
  328. break;
  329. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER > 1 */
  330. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 2))
  331. case 3:
  332. reg |= LLWU_FILT_FILTF3_MASK;
  333. break;
  334. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER > 2 */
  335. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 3))
  336. case 4:
  337. reg |= LLWU_FILT_FILTF4_MASK;
  338. break;
  339. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER > 3 */
  340. default:
  341. break;
  342. }
  343. base->FILT = reg;
  344. #else
  345. volatile uint8_t *regBase;
  346. uint8_t reg;
  347. switch (filterIndex)
  348. {
  349. case 1:
  350. regBase = &base->FILT1;
  351. break;
  352. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 1))
  353. case 2:
  354. regBase = &base->FILT2;
  355. break;
  356. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER */
  357. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 2))
  358. case 3:
  359. regBase = &base->FILT3;
  360. break;
  361. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER */
  362. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 3))
  363. case 4:
  364. regBase = &base->FILT4;
  365. break;
  366. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER */
  367. default:
  368. regBase = NULL;
  369. break;
  370. }
  371. if (regBase)
  372. {
  373. reg = *regBase;
  374. reg |= LLWU_FILT1_FILTF_MASK;
  375. *regBase = reg;
  376. }
  377. #endif /* FSL_FEATURE_LLWU_REG_BITWIDTH */
  378. }
  379. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER */
  380. #if (defined(FSL_FEATURE_LLWU_HAS_RESET_ENABLE) && FSL_FEATURE_LLWU_HAS_RESET_ENABLE)
  381. void LLWU_SetResetPinMode(LLWU_Type *base, bool pinEnable, bool enableInLowLeakageMode)
  382. {
  383. uint8_t reg;
  384. reg = base->RST;
  385. reg &= ~(LLWU_RST_LLRSTE_MASK | LLWU_RST_RSTFILT_MASK);
  386. reg |=
  387. (((uint32_t)pinEnable << LLWU_RST_LLRSTE_SHIFT) | ((uint32_t)enableInLowLeakageMode << LLWU_RST_RSTFILT_SHIFT));
  388. base->RST = reg;
  389. }
  390. #endif /* FSL_FEATURE_LLWU_HAS_RESET_ENABLE */