n32g45x_tsc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_tsc.c
  29. * @author Nations
  30. * @version v1.0.2
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include "n32g45x.h"
  35. #include "n32g45x_tsc.h"
  36. /**
  37. * @brief Init TSC config for hardware detect mode.
  38. * @param TSC_Def Pointer of TSC register.
  39. * @param CtrlCfg configurations.
  40. */
  41. TSC_ErrorTypeDef TSC_Init(TSC_Module* TSC_Def, TSC_InitType* CtrlCfg)
  42. {
  43. uint32_t tempreg,timeout;
  44. assert_param(IS_TSC_FILTER(CtrlCfg->TSC_FilterCount));
  45. assert_param(IS_TSC_DET_PERIOD(CtrlCfg->TSC_DetPeriod));
  46. if(TSC_Def != TSC)
  47. return TSC_ERROR_PARAMETER;
  48. /* waiting tsc hw for idle status.*/
  49. timeout = 0;
  50. do
  51. {
  52. __TSC_HW_DISABLE();
  53. if(++timeout > TSC_TIMEOUT)
  54. return TSC_ERROR_HW_MODE;
  55. }while (__TSC_GET_HW_MODE());
  56. /*TSC_CTRL config*/
  57. tempreg = 0;
  58. if(CtrlCfg->TSC_DetIntEnable)
  59. tempreg |= TSC_IT_DET_ENABLE;
  60. if(CtrlCfg->TSC_GreatEnable)
  61. tempreg |= TSC_DET_TYPE_GREAT;
  62. if(CtrlCfg->TSC_LessEnable)
  63. tempreg |= TSC_DET_TYPE_LESS;
  64. tempreg |= CtrlCfg->TSC_FilterCount;
  65. tempreg |= CtrlCfg->TSC_DetPeriod;
  66. TSC_Def->CTRL = tempreg;
  67. return TSC_ERROR_OK;
  68. }
  69. /**
  70. * @brief Config the clock source of TSC
  71. * @param TSC_ClkSource specifies the clock source of TSC
  72. * This parameter can be one of the following values:
  73. * @arg TSC_CLK_SRC_LSI: TSC clock source is LSI(default)
  74. * @arg TSC_CLK_SRC_LSE: TSC clock source is LSE,and LSE is oscillator
  75. * @arg TSC_CLK_SRC_LSE_BYPASS: TSC clock source is LSE,and LSE is extennal clock
  76. * @retval TSC error code
  77. */
  78. TSC_ErrorTypeDef TSC_ClockConfig(uint32_t TSC_ClkSource)
  79. {
  80. uint32_t timeout;
  81. /*Enable PWR peripheral Clock*/
  82. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_PWR,ENABLE);
  83. if(TSC_CLK_SRC_LSI == TSC_ClkSource)
  84. {
  85. /*enable LSI clock*/
  86. RCC_EnableLsi(ENABLE);
  87. /*Wait LSI stable*/
  88. timeout = 0;
  89. while(RCC_GetFlagStatus(RCC_FLAG_LSIRD) == RESET)
  90. {
  91. if(++timeout >TSC_TIMEOUT)
  92. return TSC_ERROR_CLOCK;
  93. }
  94. }
  95. else if((TSC_CLK_SRC_LSE_BYPASS==TSC_ClkSource)||(TSC_CLK_SRC_LSE==TSC_ClkSource))
  96. {
  97. if(RCC_GetFlagStatus(RCC_FLAG_LSERD)==RESET)
  98. {
  99. // Set bit 8 of PWR_CTRL1.Open PWR DBP.
  100. PWR_BackupAccessEnable(ENABLE);
  101. RCC_ConfigLse(TSC_ClkSource);
  102. timeout = 0;
  103. while(RCC_GetFlagStatus(RCC_FLAG_LSERD) == RESET)
  104. {
  105. if(++timeout >TSC_TIMEOUT)
  106. return TSC_ERROR_CLOCK;
  107. }
  108. }
  109. }
  110. else
  111. return TSC_ERROR_PARAMETER;
  112. /*Enable TSC clk*/
  113. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TSC,ENABLE);
  114. return TSC_ERROR_OK;
  115. }
  116. /**
  117. * @brief Configure internal charge resistor for some channels
  118. * @param TSC_Def Pointer of TSC register.
  119. * @param res: internal resistor selecte
  120. * This parameter can be one of the following values:
  121. * @arg TSC_RESR_CHN_RESIST_0: 1M OHM
  122. * @arg TSC_RESR_CHN_RESIST_1: 882K OHM
  123. * @arg TSC_RESR_CHN_RESIST_2: 756K OHM
  124. * @arg TSC_RESR_CHN_RESIST_3: 630K OHM
  125. * @arg TSC_RESR_CHN_RESIST_4: 504K OHM
  126. * @arg TSC_RESR_CHN_RESIST_5: 378K OHM
  127. * @arg TSC_RESR_CHN_RESIST_6: 252K OHM
  128. * @arg TSC_RESR_CHN_RESIST_7: 126K OHM
  129. * @param Channels: channels to be configed, as TSC_CHNEN defined
  130. * This parameter:bit[0:23] used,bit[24:31] must be 0
  131. * bitx: TSC channel x
  132. * @return: none
  133. */
  134. TSC_ErrorTypeDef TSC_ConfigInternalResistor(TSC_Module* TSC_Def,uint32_t Channels, uint32_t res )
  135. {
  136. uint32_t i,chn,timeout,*pReg,nPos;
  137. assert_param(IS_TSC_CHN(Channels));
  138. assert_param(IS_TSC_RESISTOR_VALUE(res));
  139. if(TSC_Def != TSC)
  140. return TSC_ERROR_PARAMETER;
  141. /*Check charge resistor value */
  142. if(res > TSC_RESR_CHN_RESIST_125K)
  143. return TSC_ERROR_PARAMETER;
  144. /* waiting tsc hw for idle status.*/
  145. timeout = 0;
  146. do
  147. {
  148. __TSC_HW_DISABLE();
  149. if(++timeout > TSC_TIMEOUT)
  150. return TSC_ERROR_HW_MODE;
  151. }while (__TSC_GET_HW_MODE());
  152. /* Mask invalie bits*/
  153. chn = Channels & TSC_CHNEN_CHN_SEL_MASK;
  154. /* Set resistance for each channel one by one*/
  155. for (i = 0; i<MAX_TSC_HW_CHN; i++)
  156. {
  157. if (chn & 0x00000001)
  158. {
  159. pReg = (uint32_t *)(&(TSC_Def->RESR0));
  160. pReg += (i/8);
  161. nPos = (i & 0x7UL)*4;
  162. MODIFY_REG(*pReg,TSC_RESR_CHN_RESIST_MASK<<nPos,res<<nPos);
  163. }
  164. chn >>= 1;
  165. }
  166. return TSC_ERROR_OK;
  167. }
  168. /**
  169. * @brief Configure threshold value for some channels
  170. * @param TSC_Def Pointer of TSC register.
  171. * @param Channels: channels to be configed, as TSC_CHNEN defined
  172. * This parameter:bit[0:23] used,bit[24:31] must be 0
  173. * bitx: TSC channel x
  174. * @param base: base value of threshold, 0-MAX_TSC_THRESHOLD_BASE
  175. * @param delta: delta value of threshold,0-MAX_TSC_THRESHOLD_DELRA
  176. * @return: None
  177. */
  178. TSC_ErrorTypeDef TSC_ConfigThreshold( TSC_Module* TSC_Def, uint32_t Channels, uint32_t base, uint32_t delta)
  179. {
  180. uint32_t i, chn,timeout,*pReg;
  181. assert_param(IS_TSC_CHN(Channels));
  182. assert_param(IS_TSC_THRESHOLD_BASE(base));
  183. assert_param(IS_TSC_THRESHOLD_DELTA(delta));
  184. if(TSC_Def != TSC)
  185. return TSC_ERROR_PARAMETER;
  186. /*Check the base and delta value*/
  187. if( (base>MAX_TSC_THRESHOLD_BASE)||(delta>MAX_TSC_THRESHOLD_DELTA))
  188. return TSC_ERROR_PARAMETER;
  189. /* waiting tsc hw for idle status.*/
  190. timeout = 0;
  191. do
  192. {
  193. __TSC_HW_DISABLE();
  194. if(++timeout > TSC_TIMEOUT)
  195. return TSC_ERROR_HW_MODE;
  196. }while (__TSC_GET_HW_MODE());
  197. pReg = (uint32_t *)(&(TSC_Def->THRHD0));
  198. /*Mask invalie bits*/
  199. chn = Channels & TSC_CHNEN_CHN_SEL_MASK;
  200. /* Set the base and delta for each channnel one by one*/
  201. for (i = 0; i<MAX_TSC_HW_CHN; i++)
  202. {
  203. if (chn & 0x00000001)
  204. {
  205. pReg[i] = (base<<TSC_THRHD_BASE_SHIFT)|(delta<<TSC_THRHD_DELTA_SHIFT);
  206. }
  207. chn >>= 1;
  208. }
  209. return TSC_ERROR_OK;
  210. }
  211. /**
  212. * @brief Get parameters of one channel.
  213. * @param TSC_Def Pointer of TSC register.
  214. * @param ChnCfg: Pointer of TSC_ChnCfg structure.
  215. * @param Channels: channels to be configed, as TSC_CHNEN defined
  216. * This parameter:bit[0:23] used,bit[24:31] must be 0
  217. * bitx: TSC channel x
  218. * @return: None
  219. */
  220. TSC_ErrorTypeDef TSC_GetChannelCfg( TSC_Module* TSC_Def, TSC_ChnCfg* ChnCfg, uint32_t Channels)
  221. {
  222. uint32_t i,chn, *pReg;
  223. if(TSC_Def != TSC)
  224. return TSC_ERROR_PARAMETER;
  225. /*Check channel number*/
  226. if(!(IS_TSC_CHN(Channels)))
  227. return TSC_ERROR_PARAMETER;
  228. chn = Channels & TSC_CHNEN_CHN_SEL_MASK;
  229. for (i = 0; i<MAX_TSC_HW_CHN; i++)
  230. {
  231. if (chn & 0x00000001)
  232. {
  233. pReg = (uint32_t *)(&(TSC->THRHD0));
  234. pReg += i;
  235. ChnCfg->TSC_Base = (uint16_t)(((*pReg) & TSC_THRHD_BASE_MASK) >> TSC_THRHD_BASE_SHIFT);
  236. ChnCfg->TSC_Delta = (uint8_t)(((*pReg) & TSC_THRHD_DELTA_MASK)>> TSC_THRHD_DELTA_SHIFT);
  237. pReg = (uint32_t *)(&(TSC->RESR0));
  238. pReg += (i/8);
  239. ChnCfg->TSC_ResisValue = (uint8_t)(((*pReg) >> ((i & 0x7UL)*4)) & TSC_RESR_CHN_RESIST_MASK);
  240. break;
  241. }
  242. chn >>= 1;
  243. }
  244. return TSC_ERROR_OK;
  245. }
  246. /**
  247. * @brief Get TSC status value.
  248. * @param TSC_Def Pointer of TSC register.
  249. * @param type TSC status type.
  250. */
  251. uint32_t TSC_GetStatus(TSC_Module* TSC_Def, uint32_t type)
  252. {
  253. uint32_t value = 0;
  254. if(TSC_Def != TSC)
  255. return 0;
  256. switch (type)
  257. {
  258. case TSC_GET_STS_CNTVALUE:
  259. value = __TSC_GET_CHN_CNT();
  260. break;
  261. case TSC_GET_STS_LESS_DET:
  262. value = __TSC_GET_HW_DET_TYPE(TSC_FLAG_LESS_DET);
  263. break;
  264. case TSC_GET_STS_GREAT_DET:
  265. value = __TSC_GET_HW_DET_TYPE(TSC_FLAG_GREAT_DET);
  266. break;
  267. case TSC_GET_STS_CHN_NUM:
  268. value = __TSC_GET_CHN_NUMBER();
  269. break;
  270. case TSC_GET_STS_DET_ST:
  271. value = __TSC_GET_HW_MODE();
  272. break;
  273. default:
  274. break;
  275. }
  276. return value;
  277. }
  278. /**
  279. * @brief Enable/Disable hardware detection.
  280. * @param TSC_Def Pointer of TSC register.
  281. * @param Channels: channels to be configed, as TSC_CHNEN defined
  282. * This parameter:bit[0:23] used,bit[24:31] must be 0
  283. * bitx: TSC channel x
  284. * @param Cmd ENABLE:Enable hardware detection,DISALBE:Disable hardware detection.
  285. * @note You can only output one channel at a time.
  286. */
  287. TSC_ErrorTypeDef TSC_Cmd(TSC_Module* TSC_Def, uint32_t Channels, FunctionalState Cmd)
  288. {
  289. uint32_t timeout;
  290. if(TSC_Def != TSC)
  291. return TSC_ERROR_PARAMETER;
  292. if (Cmd != DISABLE)
  293. {
  294. // enable tsc channel
  295. Channels &= TSC_CHNEN_CHN_SEL_MASK;
  296. __TSC_CHN_CONFIG(Channels );
  297. /* Enable the TSC */
  298. __TSC_HW_ENABLE();
  299. }
  300. else
  301. {
  302. /* Disable the TSC */
  303. timeout = 0;
  304. do
  305. {
  306. __TSC_HW_DISABLE();
  307. if(++timeout > TSC_TIMEOUT)
  308. return TSC_ERROR_HW_MODE;
  309. }while (__TSC_GET_HW_MODE());
  310. __TSC_CHN_CONFIG(0);
  311. }
  312. return TSC_ERROR_OK;
  313. }
  314. /**
  315. * @brief Toggle channels to output to TIMER2/TIMER4 by software mode.
  316. * @param TSC_Def Pointer of TSC register.
  317. * @param Channels: channels to be configed, as TSC_CHNEN defined
  318. * This parameter:bit[0:23] used,bit[24:31] must be 0
  319. * bitx: TSC channel x
  320. * @param TIMx Select timer.
  321. * @param Cmd ENABLE:Enable hardware detection,DISALBE:Disable hardware detection.
  322. * @note It can only output to TIMER2/TIMER4 by software mode.Other channels are not valid.
  323. */
  324. TSC_ErrorTypeDef TSC_SW_SwtichChn(TSC_Module* TSC_Def, uint32_t Channel, TIM_Module* TIMx, FunctionalState Cmd)
  325. {
  326. uint32_t i, timeout;
  327. if(TSC_Def != TSC)
  328. return TSC_ERROR_PARAMETER;
  329. if ((TIMx != TIM2) && (TIMx != TIM4))
  330. return TSC_ERROR_PARAMETER;
  331. /* Disable the TSC HW MODE */
  332. timeout = 0;
  333. do
  334. {
  335. __TSC_HW_DISABLE();
  336. if(++timeout > TSC_TIMEOUT)
  337. return TSC_ERROR_HW_MODE;
  338. }while (__TSC_GET_HW_MODE());
  339. if (Cmd == DISABLE) // Close output by software mode
  340. {
  341. __TSC_OUT_CONFIG(TSC_OUT_PIN);
  342. __TSC_SW_DISABLE();
  343. }
  344. else
  345. {
  346. for (i = 0; i < MAX_TSC_HW_CHN; i++)
  347. {
  348. if (Channel & 0x00000001)
  349. {
  350. __TSC_SW_CHN_NUM_CONFIG(i);
  351. break;
  352. }
  353. Channel >>= 1;
  354. }
  355. // Select to output to specified TIMER.
  356. if (TIMx == TIM4)
  357. {
  358. __TSC_OUT_CONFIG(TSC_OUT_TIM4_ETR);
  359. }
  360. else
  361. {
  362. __TSC_OUT_CONFIG(TSC_OUT_TIM2_ETR);
  363. }
  364. __TSC_SW_ENABLE();
  365. }
  366. // delay time for tsc channel stabilize output
  367. for (i = 0; i < 2000; i++)
  368. {
  369. }
  370. return TSC_ERROR_OK;
  371. }
  372. /**
  373. * @brief Configure analog signal parameters.
  374. * @param TSC_Def Pointer of TSC register.
  375. * @param AnaoCfg Pointer of analog parameter structure.
  376. */
  377. TSC_ErrorTypeDef TSC_SetAnaoCfg(TSC_Module* TSC_Def, TSC_AnaoCfg* AnaoCfg)
  378. {
  379. if(TSC_Def != TSC)
  380. return TSC_ERROR_PARAMETER;
  381. if(AnaoCfg == 0)
  382. return TSC_ERROR_PARAMETER;
  383. assert_param(IS_TSC_PAD_OPTION(AnaoCfg->TSC_AnaoptrResisOption));
  384. assert_param(IS_TSC_PAD_SPEED(AnaoCfg->TSC_AnaoptrSpeedOption));
  385. __TSC_PAD_OPT_CONFIG(AnaoCfg->TSC_AnaoptrResisOption);
  386. __TSC_PAD_SPEED_CONFIG(AnaoCfg->TSC_AnaoptrSpeedOption);
  387. return TSC_ERROR_OK;
  388. }
  389. /**
  390. * @brief Configure channel parameters by channel or operation.Support configure several channels at the same time.
  391. * @param TSC_Def Pointer of TSC register.
  392. * @param ChnCfg Channel parameters.
  393. * @param Channels: channels to be configed, as TSC_CHNEN defined
  394. * This parameter:bit[0:23] used,bit[24:31] must be 0
  395. * bitx: TSC channel x
  396. */
  397. TSC_ErrorTypeDef TSC_SetChannelCfg(TSC_Module* TSC_Def, TSC_ChnCfg* ChnCfg, uint32_t Channels)
  398. {
  399. TSC_ErrorTypeDef err;
  400. if(TSC_Def != TSC)
  401. return TSC_ERROR_PARAMETER;
  402. if(0 == ChnCfg)
  403. return TSC_ERROR_PARAMETER;
  404. // Set resistance
  405. err = TSC_ConfigInternalResistor(TSC_Def, Channels, ChnCfg->TSC_ResisValue);
  406. if(err != TSC_ERROR_OK)
  407. return err;
  408. // Set the threshold of base and delta.
  409. err = TSC_ConfigThreshold(TSC_Def, Channels, ChnCfg->TSC_Base, ChnCfg->TSC_Delta);
  410. return err;
  411. }