fsl_llwu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN)
  36. void LLWU_SetExternalWakeupPinMode(LLWU_Type *base, uint32_t pinIndex, llwu_external_pin_mode_t pinMode)
  37. {
  38. #if (defined(FSL_FEATURE_LLWU_REG_BITWIDTH) && (FSL_FEATURE_LLWU_REG_BITWIDTH == 32))
  39. volatile uint32_t *regBase;
  40. uint32_t regOffset;
  41. uint32_t reg;
  42. switch (pinIndex >> 4U)
  43. {
  44. case 0U:
  45. regBase = &base->PE1;
  46. break;
  47. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 16))
  48. case 1U:
  49. regBase = &base->PE2;
  50. break;
  51. #endif
  52. default:
  53. regBase = NULL;
  54. break;
  55. }
  56. #else
  57. volatile uint8_t *regBase;
  58. uint8_t regOffset;
  59. uint8_t reg;
  60. switch (pinIndex >> 2U)
  61. {
  62. case 0U:
  63. regBase = &base->PE1;
  64. break;
  65. case 1U:
  66. regBase = &base->PE2;
  67. break;
  68. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 8))
  69. case 2U:
  70. regBase = &base->PE3;
  71. break;
  72. #endif
  73. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 12))
  74. case 3U:
  75. regBase = &base->PE4;
  76. break;
  77. #endif
  78. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 16))
  79. case 4U:
  80. regBase = &base->PE5;
  81. break;
  82. #endif
  83. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 20))
  84. case 5U:
  85. regBase = &base->PE6;
  86. break;
  87. #endif
  88. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 24))
  89. case 6U:
  90. regBase = &base->PE7;
  91. break;
  92. #endif
  93. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 28))
  94. case 7U:
  95. regBase = &base->PE8;
  96. break;
  97. #endif
  98. default:
  99. regBase = NULL;
  100. break;
  101. }
  102. #endif /* FSL_FEATURE_LLWU_REG_BITWIDTH == 32 */
  103. if (regBase)
  104. {
  105. reg = *regBase;
  106. #if (defined(FSL_FEATURE_LLWU_REG_BITWIDTH) && (FSL_FEATURE_LLWU_REG_BITWIDTH == 32))
  107. regOffset = ((pinIndex & 0x0FU) << 1U);
  108. #else
  109. regOffset = ((pinIndex & 0x03U) << 1U);
  110. #endif
  111. reg &= ~(0x3U << regOffset);
  112. reg |= ((uint32_t)pinMode << regOffset);
  113. *regBase = reg;
  114. }
  115. }
  116. bool LLWU_GetExternalWakeupPinFlag(LLWU_Type *base, uint32_t pinIndex)
  117. {
  118. #if (defined(FSL_FEATURE_LLWU_REG_BITWIDTH) && (FSL_FEATURE_LLWU_REG_BITWIDTH == 32))
  119. return (bool)(base->PF & (1U << pinIndex));
  120. #else
  121. volatile uint8_t *regBase;
  122. switch (pinIndex >> 3U)
  123. {
  124. #if (defined(FSL_FEATURE_LLWU_HAS_PF) && FSL_FEATURE_LLWU_HAS_PF)
  125. case 0U:
  126. regBase = &base->PF1;
  127. break;
  128. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 8))
  129. case 1U:
  130. regBase = &base->PF2;
  131. break;
  132. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  133. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 16))
  134. case 2U:
  135. regBase = &base->PF3;
  136. break;
  137. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  138. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 24))
  139. case 3U:
  140. regBase = &base->PF4;
  141. break;
  142. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  143. #else
  144. case 0U:
  145. regBase = &base->F1;
  146. break;
  147. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 8))
  148. case 1U:
  149. regBase = &base->F2;
  150. break;
  151. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  152. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 16))
  153. case 2U:
  154. regBase = &base->F3;
  155. break;
  156. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  157. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 24))
  158. case 3U:
  159. regBase = &base->F4;
  160. break;
  161. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  162. #endif /* FSL_FEATURE_LLWU_HAS_PF */
  163. default:
  164. regBase = NULL;
  165. break;
  166. }
  167. if (regBase)
  168. {
  169. return (bool)(*regBase & (1U << pinIndex % 8));
  170. }
  171. else
  172. {
  173. return false;
  174. }
  175. #endif /* FSL_FEATURE_LLWU_REG_BITWIDTH */
  176. }
  177. void LLWU_ClearExternalWakeupPinFlag(LLWU_Type *base, uint32_t pinIndex)
  178. {
  179. #if (defined(FSL_FEATURE_LLWU_REG_BITWIDTH) && (FSL_FEATURE_LLWU_REG_BITWIDTH == 32))
  180. base->PF = (1U << pinIndex);
  181. #else
  182. volatile uint8_t *regBase;
  183. switch (pinIndex >> 3U)
  184. {
  185. #if (defined(FSL_FEATURE_LLWU_HAS_PF) && FSL_FEATURE_LLWU_HAS_PF)
  186. case 0U:
  187. regBase = &base->PF1;
  188. break;
  189. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 8))
  190. case 1U:
  191. regBase = &base->PF2;
  192. break;
  193. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  194. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 16))
  195. case 2U:
  196. regBase = &base->PF3;
  197. break;
  198. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  199. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 24))
  200. case 3U:
  201. regBase = &base->PF4;
  202. break;
  203. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  204. #else
  205. case 0U:
  206. regBase = &base->F1;
  207. break;
  208. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 8))
  209. case 1U:
  210. regBase = &base->F2;
  211. break;
  212. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  213. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 16))
  214. case 2U:
  215. regBase = &base->F3;
  216. break;
  217. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  218. #if (defined(FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN) && (FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN > 24))
  219. case 3U:
  220. regBase = &base->F4;
  221. break;
  222. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  223. #endif /* FSL_FEATURE_LLWU_HAS_PF */
  224. default:
  225. regBase = NULL;
  226. break;
  227. }
  228. if (regBase)
  229. {
  230. *regBase = (1U << pinIndex % 8U);
  231. }
  232. #endif /* FSL_FEATURE_LLWU_REG_BITWIDTH */
  233. }
  234. #endif /* FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN */
  235. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && FSL_FEATURE_LLWU_HAS_PIN_FILTER)
  236. void LLWU_SetPinFilterMode(LLWU_Type *base, uint32_t filterIndex, llwu_external_pin_filter_mode_t filterMode)
  237. {
  238. #if (defined(FSL_FEATURE_LLWU_REG_BITWIDTH) && (FSL_FEATURE_LLWU_REG_BITWIDTH == 32))
  239. base->FILT = ((base->FILT) & ~((LLWU_FILT_FILTSEL1_MASK | LLWU_FILT_FILTE1_MASK) << ((filterIndex - 1U) * 8U))) |
  240. ((LLWU_FILT_FILTSEL1(filterMode.pinIndex) | LLWU_FILT_FILTE1(filterMode.filterMode))
  241. << ((filterIndex - 1U) * 8U)) |
  242. LLWU_FILT_FILTF1_MASK /* W1C to clear the FILTF flag bit. */
  243. ;
  244. #else
  245. volatile uint8_t *regBase;
  246. switch (filterIndex)
  247. {
  248. case 1U:
  249. regBase = &base->FILT1;
  250. break;
  251. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 1))
  252. case 2U:
  253. regBase = &base->FILT2;
  254. break;
  255. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER */
  256. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 2))
  257. case 3U:
  258. regBase = &base->FILT3;
  259. break;
  260. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER */
  261. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 3))
  262. case 4U:
  263. regBase = &base->FILT4;
  264. break;
  265. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER */
  266. default:
  267. regBase = NULL;
  268. break;
  269. }
  270. if (NULL != regBase)
  271. {
  272. *regBase = (*regBase & ~(LLWU_FILT1_FILTSEL_MASK | LLWU_FILT1_FILTE_MASK)) |
  273. LLWU_FILT1_FILTSEL(filterMode.pinIndex) | LLWU_FILT1_FILTE(filterMode.filterMode) |
  274. LLWU_FILT1_FILTF_MASK /* W1C to clear the FILTF flag bit. */
  275. ;
  276. }
  277. #endif /* FSL_FEATURE_LLWU_REG_BITWIDTH */
  278. }
  279. bool LLWU_GetPinFilterFlag(LLWU_Type *base, uint32_t filterIndex)
  280. {
  281. #if (defined(FSL_FEATURE_LLWU_REG_BITWIDTH) && (FSL_FEATURE_LLWU_REG_BITWIDTH == 32))
  282. return (bool)(base->FILT & (1U << (filterIndex * 8U - 1)));
  283. #else
  284. bool status = false;
  285. switch (filterIndex)
  286. {
  287. case 1:
  288. status = (base->FILT1 & LLWU_FILT1_FILTF_MASK);
  289. break;
  290. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 1))
  291. case 2:
  292. status = (base->FILT2 & LLWU_FILT2_FILTF_MASK);
  293. break;
  294. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER */
  295. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 2))
  296. case 3:
  297. status = (base->FILT3 & LLWU_FILT3_FILTF_MASK);
  298. break;
  299. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER */
  300. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 3))
  301. case 4:
  302. status = (base->FILT4 & LLWU_FILT4_FILTF_MASK);
  303. break;
  304. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER */
  305. default:
  306. break;
  307. }
  308. return status;
  309. #endif /* FSL_FEATURE_LLWU_REG_BITWIDTH */
  310. }
  311. void LLWU_ClearPinFilterFlag(LLWU_Type *base, uint32_t filterIndex)
  312. {
  313. #if (defined(FSL_FEATURE_LLWU_REG_BITWIDTH) && (FSL_FEATURE_LLWU_REG_BITWIDTH == 32))
  314. uint32_t reg;
  315. reg = base->FILT;
  316. switch (filterIndex)
  317. {
  318. case 1:
  319. reg |= LLWU_FILT_FILTF1_MASK;
  320. break;
  321. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 1))
  322. case 2:
  323. reg |= LLWU_FILT_FILTF2_MASK;
  324. break;
  325. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER > 1 */
  326. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 2))
  327. case 3:
  328. reg |= LLWU_FILT_FILTF3_MASK;
  329. break;
  330. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER > 2 */
  331. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 3))
  332. case 4:
  333. reg |= LLWU_FILT_FILTF4_MASK;
  334. break;
  335. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER > 3 */
  336. default:
  337. break;
  338. }
  339. base->FILT = reg;
  340. #else
  341. volatile uint8_t *regBase;
  342. uint8_t reg;
  343. switch (filterIndex)
  344. {
  345. case 1:
  346. regBase = &base->FILT1;
  347. break;
  348. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 1))
  349. case 2:
  350. regBase = &base->FILT2;
  351. break;
  352. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER */
  353. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 2))
  354. case 3:
  355. regBase = &base->FILT3;
  356. break;
  357. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER */
  358. #if (defined(FSL_FEATURE_LLWU_HAS_PIN_FILTER) && (FSL_FEATURE_LLWU_HAS_PIN_FILTER > 3))
  359. case 4:
  360. regBase = &base->FILT4;
  361. break;
  362. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER */
  363. default:
  364. regBase = NULL;
  365. break;
  366. }
  367. if (regBase)
  368. {
  369. reg = *regBase;
  370. reg |= LLWU_FILT1_FILTF_MASK;
  371. *regBase = reg;
  372. }
  373. #endif /* FSL_FEATURE_LLWU_REG_BITWIDTH */
  374. }
  375. #endif /* FSL_FEATURE_LLWU_HAS_PIN_FILTER */
  376. #if (defined(FSL_FEATURE_LLWU_HAS_RESET_ENABLE) && FSL_FEATURE_LLWU_HAS_RESET_ENABLE)
  377. void LLWU_SetResetPinMode(LLWU_Type *base, bool pinEnable, bool enableInLowLeakageMode)
  378. {
  379. uint8_t reg;
  380. reg = base->RST;
  381. reg &= ~(LLWU_RST_LLRSTE_MASK | LLWU_RST_RSTFILT_MASK);
  382. reg |=
  383. (((uint32_t)pinEnable << LLWU_RST_LLRSTE_SHIFT) | ((uint32_t)enableInLowLeakageMode << LLWU_RST_RSTFILT_SHIFT));
  384. base->RST = reg;
  385. }
  386. #endif /* FSL_FEATURE_LLWU_HAS_RESET_ENABLE */