nu_pwm.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. /**************************************************************************//**
  2. * @file pwm.c
  3. * @brief N9H30 series PWM driver source file
  4. *
  5. * @note
  6. * SPDX-License-Identifier: Apache-2.0
  7. * Copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #include "N9H30.h"
  10. #include "nu_sys.h"
  11. #include "nu_pwm.h"
  12. /** @addtogroup N9H30_Device_Driver N9H30 Device Driver
  13. @{
  14. */
  15. /** @addtogroup N9H30_PWM_Driver PWM Driver
  16. @{
  17. */
  18. /** @addtogroup N9H30_PWM_EXPORTED_FUNCTIONS PWM Exported Functions
  19. @{
  20. */
  21. //Internal function definition
  22. /// @cond HIDDEN_SYMBOLS
  23. void pwmISR(PVOID pvParam);
  24. static INT pwmInitGPIO(const INT nTimerIdentity, const INT nValue);
  25. static INT pwmInitTimer(const INT nTimerIdentity);
  26. static INT pwmStartTimer(const INT nTimerIdentity);
  27. static INT pwmStopTimer(const INT nTimerIdentity, const INT nMethod);
  28. // Register operation
  29. static INT pwmSetCP(const INT nTimerIdentity, const INT nValue);
  30. static INT pwmSetDZI(const INT nTimerIdentity, const INT nValue);
  31. static INT pwmSetCSR(const INT nTimerIdentity, const INT nValue);
  32. static INT pwmSetDZGenerator(const INT nTimerIdentity, const INT nStatus);
  33. static INT pwmSetTimerState(const INT nTimerIdentity, const INT nStatus);
  34. static INT pwmSetInverter(const INT nTimerIdentity, const INT nStatus);
  35. static INT pwmSetMode(const INT nTimerIdentity, const INT nStatus);
  36. static INT pwmSetCNR(const INT nTimerIdentity, const INT nValue);
  37. static INT pwmSetCMR(const INT nTimerIdentity, const INT nValue);
  38. static UINT pwmGetPDR(const INT nTimerIdentity);
  39. static INT pwmSetPIER(const INT nTimerIdentity, const INT value);
  40. static INT pwmCleanPIIR(const INT nTimerIdentity);
  41. //Global variable
  42. static BOOL bPWMIRQFlag = FALSE; //IRQ enable flag, set after PWM IRQ enable
  43. static BOOL bPWMTimerOpenStatus[PWM_TIMER_NUM]; //timer flag which set after open(for disable IRQ decision)
  44. static BOOL bPWMTimerStartStatus[PWM_TIMER_NUM]; //timer flag which set after Start count(to avoid incorrectly stop procedure)
  45. static BOOL bPWMTimerMode[PWM_TIMER_NUM]; //PWM timer toggle/one shot mode
  46. static BOOL volatile bPWMIntFlag[PWM_TIMER_NUM]; //interrupt flag which set by ISR
  47. /// @endcond /* HIDDEN_SYMBOLS */
  48. /**
  49. * @brief The init function of PWM device driver
  50. */
  51. INT pwmInit(void)
  52. {
  53. UINT temp;
  54. // Enable PWM clock
  55. temp = inpw(REG_CLK_PCLKEN1);
  56. temp = temp | 0x8000000;
  57. outpw(REG_CLK_PCLKEN1, temp);
  58. sysInstallISR(IRQ_LEVEL_1, PWM_IRQn, (PVOID)pwmISR);
  59. sysSetLocalInterrupt(ENABLE_IRQ); // Enable CPSR I bit
  60. return 0;
  61. }
  62. /**
  63. * @brief The exit function of PWM device driver
  64. */
  65. INT pwmExit(void)
  66. {
  67. return 0;
  68. }
  69. /**
  70. * @brief The open function of PWM device driver
  71. * @param[in] nTimerIdentity PWM Timer channel identity
  72. * @retval Successful PWM successfully opened
  73. * @retval pwmTimerBusy PWM timer already open
  74. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  75. */
  76. INT pwmOpen(const INT nTimerIdentity)
  77. {
  78. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  79. {
  80. return pwmInvalidTimerChannel;// nTimerIdentity value error
  81. }
  82. if (bPWMTimerOpenStatus[nTimerIdentity] == TRUE)
  83. {
  84. return pwmTimerBusy;
  85. }
  86. if (bPWMIRQFlag == FALSE)
  87. {
  88. sysEnableInterrupt(PWM_IRQn);
  89. bPWMIRQFlag = TRUE;
  90. }
  91. bPWMTimerOpenStatus[nTimerIdentity] = TRUE;
  92. // Set PWM timer default value(CSR->PPR->PCR->CMR->CNR)
  93. pwmInitTimer(nTimerIdentity);
  94. //Enable PIER
  95. pwmSetPIER(nTimerIdentity, PWM_ENABLE);
  96. //Reset PIIR
  97. pwmCleanPIIR(nTimerIdentity);
  98. //Reset PWM timer start count flag
  99. bPWMTimerStartStatus[nTimerIdentity] = FALSE;
  100. return Successful;
  101. }
  102. /**
  103. * @brief The close function of PWM device driver
  104. * @param[in] nTimerIdentity PWM Timer channel identity
  105. * @retval Successful PWM successfully closed
  106. * @retval pwmTimerNotOpen PWM timer not open
  107. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  108. */
  109. INT pwmClose(const INT nTimerIdentity)
  110. {
  111. INT nLoop;
  112. BOOL uAllTimerClose = TRUE;
  113. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  114. {
  115. return pwmInvalidTimerChannel;// nTimerIdentity value error
  116. }
  117. if (bPWMTimerOpenStatus[nTimerIdentity] == FALSE)
  118. {
  119. return pwmTimerNotOpen;
  120. }
  121. bPWMTimerOpenStatus[nTimerIdentity] = FALSE;
  122. //Check if all timer stop, IRQ can be disable
  123. for (nLoop = PWM_TIMER_MIN; nLoop < PWM_TIMER_NUM; nLoop++)
  124. {
  125. if (bPWMTimerOpenStatus[nLoop] == TRUE)
  126. {
  127. uAllTimerClose = FALSE;
  128. }
  129. }
  130. //All timer stop, disable IRQs
  131. if (uAllTimerClose == TRUE)
  132. {
  133. sysDisableInterrupt(PWM_IRQn);
  134. bPWMIRQFlag = FALSE;
  135. }
  136. pwmSetPIER(nTimerIdentity, PWM_DISABLE);
  137. pwmCleanPIIR(nTimerIdentity);
  138. return Successful;
  139. }
  140. /**
  141. * @brief The read function of PWM device driver
  142. * @param[in] nTimerIdentity PWM Timer channel identity
  143. * @param[out] pucStatusValue The point of typePWMSTATUS
  144. * @param[in] uLength The length of typePWMSTATUS
  145. * @retval Successful Read PWM value successfully
  146. * @retval pwmTimerNotOpen PWM timer not open
  147. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  148. * @retval pwmInvalidStructLength Struct length error(struct type error)
  149. */
  150. INT pwmRead(const INT nTimerIdentity, PUCHAR pucStatusValue, const UINT uLength)
  151. {
  152. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  153. {
  154. return pwmInvalidTimerChannel;// nTimerIdentity value error
  155. }
  156. if (bPWMTimerOpenStatus[nTimerIdentity] == FALSE)
  157. {
  158. return pwmTimerNotOpen;
  159. }
  160. if (uLength != sizeof(typePWMSTATUS))
  161. {
  162. return pwmInvalidStructLength;// Struct length error(struct type error)
  163. }
  164. if (sizeof(*((typePWMSTATUS *)pucStatusValue)) != sizeof(typePWMSTATUS))
  165. {
  166. return pwmInvalidStructLength;// Struct length error(struct type error)
  167. }
  168. ((typePWMSTATUS *)pucStatusValue)->PDR = pwmGetPDR(nTimerIdentity);
  169. if (bPWMIntFlag[nTimerIdentity] == TRUE)
  170. {
  171. bPWMIntFlag[nTimerIdentity] = FALSE;
  172. ((typePWMSTATUS *)pucStatusValue)->InterruptFlag = TRUE;
  173. }
  174. else
  175. {
  176. ((typePWMSTATUS *)pucStatusValue)->InterruptFlag = FALSE;
  177. }
  178. return Successful;
  179. }
  180. /**
  181. * @brief The write function of PWM device driver
  182. * @param[in] nTimerIdentity PWM Timer channel identity
  183. * @param[in] pucCNRCMRValue The value of CNR and CMR
  184. * @param[in] uLength For future usage
  185. * @retval Successful Write PWM setting successfully
  186. * @retval pwmTimerNotOpen PWM timer not open
  187. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  188. */
  189. INT pwmWrite(const INT nTimerIdentity, PUCHAR pucCNRCMRValue, const UINT uLength)
  190. {
  191. typePWMVALUE pwmvalue;
  192. INT nStatus;
  193. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  194. {
  195. return pwmInvalidTimerChannel;// nTimerIdentity value error
  196. }
  197. if (bPWMTimerOpenStatus[nTimerIdentity] == FALSE)
  198. {
  199. return pwmTimerNotOpen;
  200. }
  201. if (uLength != sizeof(typePWMVALUE))
  202. {
  203. return pwmInvalidStructLength;// Struct length error(struct type error)
  204. }
  205. pwmvalue.value = ((typePWMVALUE *)pucCNRCMRValue)->value;
  206. nStatus = pwmSetCNR(nTimerIdentity, pwmvalue.field.cnr);
  207. if (nStatus != Successful)
  208. {
  209. return nStatus;
  210. }
  211. nStatus = pwmSetCMR(nTimerIdentity, pwmvalue.field.cmr);
  212. if (nStatus != Successful)
  213. {
  214. return nStatus;
  215. }
  216. return Successful;
  217. }
  218. /**
  219. * @brief The ioctl function of PWM device driver
  220. * @param[in] nTimerIdentity PWM Timer channel identity
  221. * @param[in] uCommand Ioctl command which indicates different operation
  222. * @param[in] uIndication Not use in PWM
  223. * @param[in] uValue The value which use with uCommand
  224. * @retval Successful PWM ioctl execute successfully
  225. * @retval pwmTimerNotOpen PWM timer not open
  226. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  227. * @retval pwmInvalidIoctlCommand Ioctl command error
  228. * @retval Others Error according to different uCommand
  229. */
  230. INT pwmIoctl(const INT nTimerIdentity, const UINT uCommand, const UINT uIndication, UINT uValue)
  231. {
  232. INT nStatus;
  233. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  234. {
  235. return pwmInvalidTimerChannel;// nTimerIdentity value error
  236. }
  237. if (bPWMTimerOpenStatus[nTimerIdentity] == FALSE)
  238. {
  239. return pwmTimerNotOpen;
  240. }
  241. switch (uCommand)
  242. {
  243. case START_PWMTIMER:
  244. {
  245. nStatus = pwmStartTimer(nTimerIdentity);
  246. break;
  247. }
  248. case STOP_PWMTIMER:
  249. {
  250. // default stop method is 2
  251. nStatus = pwmStopTimer(nTimerIdentity, PWM_STOP_METHOD2);
  252. break;
  253. }
  254. case SET_CSR:
  255. {
  256. nStatus = pwmSetCSR(nTimerIdentity, uValue);
  257. break;
  258. }
  259. case SET_CP:
  260. {
  261. nStatus = pwmSetCP(nTimerIdentity, uValue);
  262. break;
  263. }
  264. case SET_DZI:
  265. {
  266. nStatus = pwmSetDZI(nTimerIdentity, uValue);
  267. break;
  268. }
  269. case SET_INVERTER:
  270. {
  271. nStatus = pwmSetInverter(nTimerIdentity, uValue);
  272. break;
  273. }
  274. case SET_MODE:
  275. {
  276. nStatus = pwmSetMode(nTimerIdentity, uValue);
  277. break;
  278. }
  279. case ENABLE_DZ_GENERATOR:
  280. {
  281. nStatus = pwmSetDZGenerator(nTimerIdentity, PWM_ENABLE);
  282. break;
  283. }
  284. case DISABLE_DZ_GENERATOR:
  285. {
  286. nStatus = pwmSetDZGenerator(nTimerIdentity, PWM_DISABLE);
  287. break;
  288. }
  289. case ENABLE_PWMGPIOOUTPUT:
  290. {
  291. nStatus = pwmInitGPIO(nTimerIdentity, uValue);
  292. break;
  293. }
  294. default:
  295. {
  296. return pwmInvalidIoctlCommand;
  297. }
  298. }
  299. return nStatus;
  300. }
  301. /// @cond HIDDEN_SYMBOLS
  302. /**
  303. * @brief The interrupt service routines of PWM
  304. * @param[in] pvParam IRQ Parameter(not use in PWM)
  305. */
  306. VOID pwmISR(PVOID pvParam)
  307. {
  308. INT i;
  309. UINT32 uRegisterValue = 0;
  310. uRegisterValue = inpw(REG_PWM_PIIR);// Get PIIR value
  311. for (i = 0; i < PWM_TIMER_NUM ; i++)
  312. {
  313. if (uRegisterValue & (1 << i))
  314. {
  315. bPWMIntFlag[i] = 1;
  316. outpw(REG_PWM_PIIR, (1 << i));
  317. }
  318. }
  319. }
  320. /**
  321. * @brief This function set corresponding GPIO as PWM function according to the
  322. * parameter nTimerIdentity
  323. * @param[in] nTimerIdentity Timer channel number
  324. * @retval Successful PWM init GPIO successfully
  325. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  326. * @retval pwmInvalidPin PWM output pin setting error
  327. */
  328. static INT pwmInitGPIO(const INT nTimerIdentity, const INT nValue)
  329. {
  330. UINT temp = 0;
  331. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  332. {
  333. return pwmInvalidTimerChannel;// Timer_num value error
  334. }
  335. if (nTimerIdentity == PWM_TIMER0)
  336. {
  337. if (nValue == PWM0_GPA12)
  338. {
  339. temp = inpw(REG_SYS_GPA_MFPH);
  340. temp = (temp & ~0x000F0000) | 0xD0000;
  341. outpw(REG_SYS_GPA_MFPH, temp);
  342. }
  343. else if (nValue == PWM0_GPB2)
  344. {
  345. temp = inpw(REG_SYS_GPB_MFPL);
  346. temp = (temp & ~0xF00) | 0xD00;
  347. outpw(REG_SYS_GPB_MFPL, temp);
  348. }
  349. else
  350. return pwmInvalidPin;
  351. }
  352. else if (nTimerIdentity == PWM_TIMER1)
  353. {
  354. if (nValue == PWM1_GPA13)
  355. {
  356. temp = inpw(REG_SYS_GPA_MFPH);
  357. temp = (temp & ~0x00F00000) | 0xD00000;
  358. outpw(REG_SYS_GPA_MFPH, temp);
  359. }
  360. else if (nValue == PWM1_GPB3)
  361. {
  362. temp = inpw(REG_SYS_GPB_MFPL);
  363. temp = (temp & ~0xF000) | 0xD000;
  364. outpw(REG_SYS_GPB_MFPL, temp);
  365. }
  366. else
  367. return pwmInvalidPin;
  368. }
  369. else if (nTimerIdentity == PWM_TIMER2)
  370. {
  371. if (nValue == PWM2_GPA14)
  372. {
  373. temp = inpw(REG_SYS_GPA_MFPH);
  374. temp = (temp & ~0x0F000000) | 0xD000000;
  375. outpw(REG_SYS_GPA_MFPH, temp);
  376. }
  377. else if (nValue == PWM2_GPH2)
  378. {
  379. temp = inpw(REG_SYS_GPH_MFPL);
  380. temp = (temp & ~0xF00) | 0xD00;
  381. outpw(REG_SYS_GPH_MFPL, temp);
  382. }
  383. else
  384. return pwmInvalidPin;
  385. }
  386. else
  387. {
  388. if (nValue == PWM3_GPA15)
  389. {
  390. temp = inpw(REG_SYS_GPA_MFPH);
  391. temp = (temp & ~0xF0000000) | 0xD0000000;
  392. outpw(REG_SYS_GPA_MFPH, temp);
  393. }
  394. else if (nValue == PWM3_GPH3)
  395. {
  396. temp = inpw(REG_SYS_GPH_MFPL);
  397. temp = (temp & ~0xF000) | 0xD000;
  398. outpw(REG_SYS_GPH_MFPL, temp);
  399. }
  400. else
  401. return pwmInvalidPin;
  402. }
  403. return Successful;
  404. }
  405. /**
  406. * @brief This function initiates PWM timer n and set the default setting to CSR,
  407. * PPR, PCR, CNR, CMR
  408. * @param[in] nTimerIdentity Timer channel number
  409. * @retval Successful PWM init timer successfully
  410. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  411. */
  412. static INT pwmInitTimer(const INT nTimerIdentity)
  413. {
  414. typePPR PWMPPR;
  415. INT nStatus;
  416. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  417. {
  418. return pwmInvalidTimerChannel;// nTimerIdentity value error
  419. }
  420. //Set CSR
  421. nStatus = pwmSetCSR(nTimerIdentity, DEFAULT_CSR);
  422. if (nStatus != Successful)
  423. {
  424. return nStatus;
  425. }
  426. //Set PPR
  427. PWMPPR.value = (UINT)inpw(REG_PWM_PPR);
  428. switch (nTimerIdentity)
  429. {
  430. case PWM_TIMER0:
  431. {
  432. if (PWMPPR.field.cp0 == 0)
  433. {
  434. pwmSetCP(nTimerIdentity, DEFAULT_CP);
  435. }
  436. break;
  437. }
  438. case PWM_TIMER1:
  439. {
  440. if (PWMPPR.field.cp0 == 0)
  441. {
  442. pwmSetCP(nTimerIdentity, DEFAULT_CP);
  443. }
  444. break;
  445. }
  446. case PWM_TIMER2:
  447. {
  448. if (PWMPPR.field.cp1 == 0)
  449. {
  450. pwmSetCP(nTimerIdentity, DEFAULT_CP);
  451. }
  452. break;
  453. }
  454. case PWM_TIMER3:
  455. {
  456. if (PWMPPR.field.cp1 == 0)
  457. {
  458. pwmSetCP(nTimerIdentity, DEFAULT_CP);
  459. }
  460. break;
  461. }
  462. }
  463. //Set PCR
  464. nStatus = pwmSetMode(nTimerIdentity, DEFAULT_MODE);
  465. if (nStatus != Successful)
  466. {
  467. return nStatus;
  468. }
  469. bPWMTimerMode[nTimerIdentity] = DEFAULT_MODE;
  470. //Set CMR
  471. nStatus = pwmSetCMR(nTimerIdentity, DEFAULT_CMR);
  472. if (nStatus != Successful)
  473. {
  474. return nStatus;
  475. }
  476. //Set CNR
  477. nStatus = pwmSetCNR(nTimerIdentity, DEFAULT_CNR);
  478. if (nStatus != Successful)
  479. {
  480. return nStatus;
  481. }
  482. return Successful;
  483. }
  484. /**
  485. * @brief This function starts PWM timer according to the parameter
  486. * @param[in] nTimerIdentity Timer channel number
  487. * @retval Successful PWM start timer successfully
  488. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  489. */
  490. static INT pwmStartTimer(const INT nTimerIdentity)
  491. {
  492. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  493. {
  494. return pwmInvalidTimerChannel;// Timer_num value error
  495. }
  496. pwmSetTimerState(nTimerIdentity, PWM_ENABLE);
  497. if (bPWMTimerMode[nTimerIdentity] == PWM_TOGGLE)
  498. {
  499. bPWMTimerStartStatus[nTimerIdentity] = TRUE;
  500. }
  501. return Successful;
  502. }
  503. /**
  504. * @brief This function stops PWM timer n using method 1, 2, or 3 according to the
  505. * parameter nTimerIdentity and nStatus
  506. * @param[in] nTimerIdentity Timer channel number
  507. * @param[in] nMethod Stop PWM timer method
  508. * @retval Successful PWM stop timer successfully
  509. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  510. * @retval pwmInvalidStopMethod Stop method error
  511. */
  512. static INT pwmStopTimer(const INT nTimerIdentity, INT nMethod)
  513. {
  514. typeCNR PWMCNR;
  515. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  516. {
  517. // Timer_num value error
  518. return pwmInvalidTimerChannel;
  519. }
  520. //Can't stop before open PWM timer
  521. if (bPWMTimerOpenStatus[nTimerIdentity] == FALSE)
  522. {
  523. return Successful;
  524. }
  525. // one shot mode didn't need stop procedure
  526. if (bPWMTimerMode[nTimerIdentity] == PWM_ONESHOT)
  527. {
  528. return Successful;
  529. }
  530. // Timer stop already, no need to stop again
  531. if (bPWMTimerStartStatus[nTimerIdentity] == FALSE)
  532. {
  533. return Successful;
  534. }
  535. // Set CNR as 0
  536. PWMCNR.field.cnr = 0;
  537. outpw(REG_PWM_CNR0 + (PWM_OFFSET * nTimerIdentity), PWMCNR.value);
  538. switch (nMethod)
  539. {
  540. case PWM_STOP_METHOD1:
  541. {
  542. while (1)
  543. {
  544. if (pwmGetPDR(nTimerIdentity) == 0) // Wait PDR reach to 0
  545. {
  546. pwmSetTimerState(nTimerIdentity, PWM_DISABLE);// Disable pwm timer
  547. bPWMIntFlag[nTimerIdentity] = FALSE;
  548. bPWMTimerStartStatus[nTimerIdentity] = FALSE;
  549. break;
  550. }
  551. }
  552. break;
  553. }
  554. case PWM_STOP_METHOD2:
  555. {
  556. while (1)
  557. {
  558. if (bPWMIntFlag[nTimerIdentity] == TRUE) // Wait interrupt happen
  559. {
  560. pwmSetTimerState(nTimerIdentity, PWM_DISABLE);// Disable pwm timer
  561. bPWMIntFlag[nTimerIdentity] = FALSE;
  562. bPWMTimerStartStatus[nTimerIdentity] = FALSE;
  563. break;
  564. }
  565. }
  566. break;
  567. }
  568. /*case PWM_STOP_METHOD3:
  569. {
  570. pwmSetPCRState(nTimerIdentity, PWM_DISABLE);// Disable pwm timer
  571. bPWMIntFlag[nTimerIdentity] = FALSE;
  572. bPWMTimerStartStatus[nTimerIdentity] = FALSE;
  573. break;
  574. }*/
  575. default:
  576. {
  577. return pwmInvalidStopMethod;// Stop method value error
  578. }
  579. }
  580. return Successful;
  581. }
  582. /**
  583. * @brief This function set CPn value according to the parameter nTimerIdentity and nValue
  584. * @param[in] nTimerIdentity Timer channel number
  585. * @param[in] nValue The value which want to set in CSRn
  586. * @retval Successful Set CPn successfully
  587. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  588. * @retval pwmInvalidCPValue PWM_PPR CPn value out of range
  589. */
  590. static INT pwmSetCP(const INT nTimerIdentity, const INT nValue)
  591. {
  592. typePPR PWMPPR;
  593. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  594. {
  595. return pwmInvalidTimerChannel;// Timer_num value error
  596. }
  597. if (nValue < CP_MIN || nValue > CP_MAX)
  598. {
  599. return pwmInvalidCPValue;// CP value error
  600. }
  601. PWMPPR.value = (UINT)inpw(REG_PWM_PPR);
  602. switch (nTimerIdentity)
  603. {
  604. case PWM_TIMER0:
  605. {
  606. PWMPPR.field.cp0 = nValue;
  607. break;
  608. }
  609. case PWM_TIMER1:
  610. {
  611. PWMPPR.field.cp0 = nValue;
  612. break;
  613. }
  614. case PWM_TIMER2:
  615. {
  616. PWMPPR.field.cp1 = nValue;
  617. break;
  618. }
  619. case PWM_TIMER3:
  620. {
  621. PWMPPR.field.cp1 = nValue;
  622. break;
  623. }
  624. }
  625. outpw(REG_PWM_PPR, PWMPPR.value);
  626. return Successful;
  627. }
  628. /**
  629. * @brief This function set DZIn value according to the parameter nTimerIdentity and nValue
  630. * @param[in] nTimerIdentity Timer channel number
  631. * @param[in] nValue The value which want to set in DZIn
  632. * @retval Successful Set DZIn successfully
  633. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  634. * @retval pwmInvalidDZIValue PWM_PPR DZIn value out of range
  635. */
  636. static INT pwmSetDZI(const INT nTimerIdentity, const INT nValue)
  637. {
  638. typePPR PWMPPR;
  639. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  640. {
  641. return pwmInvalidTimerChannel;// Timer_num value error
  642. }
  643. if (nValue < DZI_MIN || nValue > DZI_MAX)
  644. {
  645. return pwmInvalidDZIValue;// CSR value error
  646. }
  647. PWMPPR.value = (UINT)inpw(REG_PWM_PPR);
  648. switch (nTimerIdentity)
  649. {
  650. case PWM_TIMER0:
  651. {
  652. PWMPPR.field.dzi0 = nValue;
  653. break;
  654. }
  655. case PWM_TIMER1:
  656. {
  657. PWMPPR.field.dzi0 = nValue;
  658. break;
  659. }
  660. case PWM_TIMER2:
  661. {
  662. PWMPPR.field.dzi1 = nValue;
  663. break;
  664. }
  665. case PWM_TIMER3:
  666. {
  667. PWMPPR.field.dzi1 = nValue;
  668. break;
  669. }
  670. }
  671. outpw(REG_PWM_PPR, PWMPPR.value);
  672. return Successful;
  673. }
  674. /**
  675. * @brief This function set CSRn value according to the parameter nTimerIdentity and nValue
  676. * @param[in] nTimerIdentity Timer channel number
  677. * @param[in] nValue The value which want to set in CSRn
  678. * @retval Successful Set CSRn successfully
  679. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  680. */
  681. static INT pwmSetCSR(const INT nTimerIdentity, const INT nValue)
  682. {
  683. typeCSR PWMCSR;
  684. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  685. {
  686. return pwmInvalidTimerChannel;// Timer_num value error
  687. }
  688. if (nValue < CSR_MIN || nValue > CSR_MAX)
  689. {
  690. return pwmInvalidCSRValue;// CSR value error
  691. }
  692. PWMCSR.value = (UINT)inpw(REG_PWM_CSR);
  693. switch (nTimerIdentity)
  694. {
  695. case PWM_TIMER0:
  696. {
  697. PWMCSR.field.csr0 = nValue;
  698. break;
  699. }
  700. case PWM_TIMER1:
  701. {
  702. PWMCSR.field.csr1 = nValue;
  703. break;
  704. }
  705. case PWM_TIMER2:
  706. {
  707. PWMCSR.field.csr2 = nValue;
  708. break;
  709. }
  710. case PWM_TIMER3:
  711. {
  712. PWMCSR.field.csr3 = nValue;
  713. break;
  714. }
  715. }
  716. outpw(REG_PWM_CSR, PWMCSR.value);
  717. return Successful;
  718. }
  719. /**
  720. * @brief This function enable/disable PWM channel n dead zone function according to the
  721. * parameter nTimerIdentity and nStatus
  722. * @param[in] nTimerIdentity Timer channel number
  723. * @param[in] nStatus PWMDZG_ENABLE/PWMDZG_DISABLE
  724. * @retval Successful Set dead zone successfully
  725. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  726. * @retval pwmInvalidDZGStatus PWM Dead-Zone Generator enable/disable status error
  727. */
  728. static INT pwmSetDZGenerator(const INT nTimerIdentity, INT nStatus)
  729. {
  730. typePCR PWMPCR;
  731. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  732. {
  733. return pwmInvalidTimerChannel;// Timer_num value error
  734. }
  735. if (nStatus != PWMDZG_ENABLE && nStatus != PWMDZG_DISABLE)
  736. {
  737. return pwmInvalidDZGStatus;// PCR inverter value error
  738. }
  739. PWMPCR.value = (UINT)inpw(REG_PWM_PCR);
  740. switch (nTimerIdentity)
  741. {
  742. case PWM_TIMER0:
  743. {
  744. PWMPCR.field.grpup0_dzen = nStatus;
  745. break;
  746. }
  747. case PWM_TIMER1:
  748. {
  749. PWMPCR.field.grpup0_dzen = nStatus;
  750. break;
  751. }
  752. case PWM_TIMER2:
  753. {
  754. PWMPCR.field.grpup1_dzen = nStatus;
  755. break;
  756. }
  757. case PWM_TIMER3:
  758. {
  759. PWMPCR.field.grpup1_dzen = nStatus;
  760. break;
  761. }
  762. }
  763. outpw(REG_PWM_PCR, PWMPCR.value);
  764. return Successful;
  765. }
  766. /**
  767. * @brief This function set PWM channel n enable/disable according to the
  768. * parameter nTimerIdentity and nStatus
  769. * @param[in] nTimerIdentity Timer channel number
  770. * @param[in] nStatus PWM_ENABLE/PWMDISABLE
  771. * @retval Successful Set channel enable/disable successfully
  772. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  773. */
  774. static INT pwmSetTimerState(const INT nTimerIdentity, INT nStatus)
  775. {
  776. typePCR PWMPCR;
  777. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  778. {
  779. return pwmInvalidTimerChannel;// Timer_num value error
  780. }
  781. if (nStatus != PWM_ENABLE && nStatus != PWM_DISABLE)
  782. {
  783. return pwmInvalidTimerStatus;
  784. }
  785. PWMPCR.value = (UINT)inpw(REG_PWM_PCR);
  786. switch (nTimerIdentity)
  787. {
  788. case PWM_TIMER0:
  789. {
  790. PWMPCR.field.ch0_en = nStatus;
  791. break;
  792. }
  793. case PWM_TIMER1:
  794. {
  795. PWMPCR.field.ch1_en = nStatus;
  796. break;
  797. }
  798. case PWM_TIMER2:
  799. {
  800. PWMPCR.field.ch2_en = nStatus;
  801. break;
  802. }
  803. case PWM_TIMER3:
  804. {
  805. PWMPCR.field.ch3_en = nStatus;
  806. break;
  807. }
  808. }
  809. outpw(REG_PWM_PCR, PWMPCR.value);
  810. return Successful;
  811. }
  812. /**
  813. * @brief This function set PWM channel n inverter on/off according to the
  814. * parameter nTimerIdentity and nStatus
  815. * @param[in] nTimerIdentity Timer channel number
  816. * @param[in] nStatus PWM_ENABLE/PWM_DISABLE
  817. * @retval Successful Set inverter successfully
  818. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  819. * @retval pwmInvalidInverterValue Inverter value error
  820. */
  821. static INT pwmSetInverter(const INT nTimerIdentity, INT nStatus)
  822. {
  823. typePCR PWMPCR;
  824. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  825. {
  826. return pwmInvalidTimerChannel;// Timer_num value error
  827. }
  828. if (nStatus != PWM_INVON && nStatus != PWM_INVOFF)
  829. {
  830. return pwmInvalidInverterValue;// PCR inverter value error
  831. }
  832. PWMPCR.value = (UINT)inpw(REG_PWM_PCR);
  833. switch (nTimerIdentity)
  834. {
  835. case PWM_TIMER0:
  836. {
  837. PWMPCR.field.ch0_inverter = nStatus;
  838. break;
  839. }
  840. case PWM_TIMER1:
  841. {
  842. PWMPCR.field.ch1_inverter = nStatus;
  843. break;
  844. }
  845. case PWM_TIMER2:
  846. {
  847. PWMPCR.field.ch2_inverter = nStatus;
  848. break;
  849. }
  850. case PWM_TIMER3:
  851. {
  852. PWMPCR.field.ch3_inverter = nStatus;
  853. break;
  854. }
  855. }
  856. outpw(REG_PWM_PCR, PWMPCR.value);
  857. return Successful;
  858. }
  859. /**
  860. * @brief This function set PWM channel n toggle/one shot mode according to the
  861. * parameter nTimerIdentity and nStatus
  862. * @param[in] nTimerIdentity Timer channel number
  863. * @param[in] nStatus PWM_TOGGLE/PWM_ONESHOT
  864. * @retval Successful Set operation mode successfully
  865. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  866. * @retval pwmInvalidModeStatus Operating mode error
  867. */
  868. static INT pwmSetMode(const INT nTimerIdentity, INT nStatus)
  869. {
  870. typePCR PWMPCR;
  871. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  872. {
  873. return pwmInvalidTimerChannel;// Timer_num value error
  874. }
  875. if (nStatus != PWM_TOGGLE && nStatus != PWM_ONESHOT)
  876. {
  877. return pwmInvalidModeStatus;// PCR inverter value error
  878. }
  879. PWMPCR.value = (UINT)inpw(REG_PWM_PCR);
  880. switch (nTimerIdentity)
  881. {
  882. case PWM_TIMER0:
  883. {
  884. PWMPCR.field.ch0_mode = nStatus;
  885. break;
  886. }
  887. case PWM_TIMER1:
  888. {
  889. PWMPCR.field.ch1_mode = nStatus;
  890. break;
  891. }
  892. case PWM_TIMER2:
  893. {
  894. PWMPCR.field.ch2_mode = nStatus;
  895. break;
  896. }
  897. case PWM_TIMER3:
  898. {
  899. PWMPCR.field.ch3_mode = nStatus;
  900. break;
  901. }
  902. }
  903. outpw(REG_PWM_PCR, PWMPCR.value);
  904. bPWMTimerMode[nTimerIdentity] = nStatus;
  905. return Successful;
  906. }
  907. /**
  908. * @brief This function set PWM_CNRn value according to the parameter nTimerIdentity and nValue
  909. * @param[in] nTimerIdentity Timer channel number
  910. * @param[in] nValue CNR value
  911. * @retval Successful Set CNR successfully
  912. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  913. * @retval pwmInvalidCNRValue Invalid CNR value
  914. */
  915. static INT pwmSetCNR(const INT nTimerIdentity, INT nValue)
  916. {
  917. typeCNR PWMCNR;
  918. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  919. {
  920. return pwmInvalidTimerChannel;// Timer_num value error
  921. }
  922. if (nValue < CNR_MIN || nValue > CNR_MAX)
  923. {
  924. return pwmInvalidCNRValue;// PCR inverter value error
  925. }
  926. PWMCNR.field.cnr = nValue;
  927. outpw(REG_PWM_CNR0 + (PWM_OFFSET * nTimerIdentity), PWMCNR.value);
  928. return Successful;
  929. }
  930. /**
  931. * @brief This function set PWM_CMRn value according to the parameter nTimerIdentity and nValue
  932. * @param[in] nTimerIdentity Timer channel number
  933. * @param[in] nValue CMR value
  934. * @retval Successful Set CMR successfully
  935. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  936. * @retval pwmInvalidCMRValue Invalid CMR value
  937. */
  938. static INT pwmSetCMR(const INT nTimerIdentity, INT nValue)
  939. {
  940. typeCMR PWMCMR;
  941. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  942. {
  943. return pwmInvalidTimerChannel;// Timer_num value error
  944. }
  945. if (nValue < CMR_MIN || nValue > CMR_MAX)
  946. {
  947. return pwmInvalidCMRValue;// CMR value error
  948. }
  949. PWMCMR.field.cmr = nValue;
  950. outpw(REG_PWM_CMR0 + (PWM_OFFSET * nTimerIdentity), PWMCMR.value);
  951. return Successful;
  952. }
  953. /**
  954. * @brief This function return the PDR value of PWM timer n
  955. * @param[in] nTimerIdentity Timer channel number
  956. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  957. * @retval Others Current PDR value
  958. */
  959. static UINT pwmGetPDR(const INT nTimerIdentity)
  960. {
  961. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  962. {
  963. return pwmInvalidTimerChannel;// Timer_num value error
  964. }
  965. else
  966. {
  967. return (UINT)inpw(REG_PWM_PDR0 + (PWM_OFFSET * nTimerIdentity)); // Return PDR value
  968. }
  969. }
  970. /**
  971. * @brief This function set the PIERn bit of PWM timer n as 1 or 0 according to the
  972. * parameter nTimerIdentity and nValue
  973. * @param[in] nTimerIdentity Timer channel number
  974. * @param[in] nValue PWM_ENABLE/PWM_DISABLE
  975. * @retval Successful Set PIER successfully
  976. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  977. */
  978. static INT pwmSetPIER(const INT nTimerIdentity, INT nValue)
  979. {
  980. UINT uRegisterValue = 0;;
  981. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  982. {
  983. return pwmInvalidTimerChannel;// Timer_num value error
  984. }
  985. else
  986. {
  987. uRegisterValue = (UINT)inpw(REG_PWM_PIER);
  988. if (nValue == PWM_ENABLE)
  989. {
  990. uRegisterValue = uRegisterValue | (1 << nTimerIdentity); // Set PIER
  991. }
  992. else
  993. {
  994. uRegisterValue = uRegisterValue & (0 << nTimerIdentity); // Clear PIER
  995. }
  996. outpw(REG_PWM_PIER, uRegisterValue);// Write value to PIER
  997. return Successful;
  998. }
  999. }
  1000. /**
  1001. * @brief This function clear PIIRn bit according to the parameter nTimerIdentity
  1002. * @param[in] nTimerIdentity Timer channel number
  1003. * @retval Successful Clear PIIR successfully
  1004. * @retval pwmInvalidTimerChannel PWM Timer channel number error
  1005. */
  1006. static INT pwmCleanPIIR(const INT nTimerIdentity)
  1007. {
  1008. UINT uRegisterValue = 0;
  1009. if (nTimerIdentity < PWM_TIMER_MIN || nTimerIdentity > PWM_TIMER_MAX)
  1010. {
  1011. return pwmInvalidTimerChannel;// nTimerIdentity value error
  1012. }
  1013. uRegisterValue = (UINT)inpw(REG_PWM_PIIR);
  1014. uRegisterValue = uRegisterValue & ~(1 << nTimerIdentity);
  1015. outpw(REG_PWM_PIIR, uRegisterValue);
  1016. return Successful;
  1017. }
  1018. /// @endcond /* HIDDEN_SYMBOLS */
  1019. /*@}*/ /* end of group N9H30_PWM_EXPORTED_FUNCTIONS */
  1020. /*@}*/ /* end of group N9H30_PWM_Driver */
  1021. /*@}*/ /* end of group N9H30_Device_Driver */
  1022. /*** (C) COPYRIGHT 2018 Nuvoton Technology Corp. ***/