My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /**********************************************************************
  2. * $Id$ lpc17xx_pwm.c 2011-03-31
  3. *//**
  4. * @file lpc17xx_pwm.c
  5. * @brief Contains all functions support for PWM firmware library on LPC17xx
  6. * @version 2.1
  7. * @date 31. Mar. 2011
  8. * @author NXP MCU SW Application Team
  9. *
  10. * Copyright(C) 2011, NXP Semiconductor
  11. * All rights reserved.
  12. *
  13. ***********************************************************************
  14. * Software that is described herein is for illustrative purposes only
  15. * which provides customers with programming information regarding the
  16. * products. This software is supplied "AS IS" without any warranties.
  17. * NXP Semiconductors assumes no responsibility or liability for the
  18. * use of the software, conveys no license or title under any patent,
  19. * copyright, or mask work right to the product. NXP Semiconductors
  20. * reserves the right to make changes in the software without
  21. * notification. NXP Semiconductors also make no representation or
  22. * warranty that such application will be suitable for the specified
  23. * use without further testing or modification.
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors'
  26. * relevant copyright in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. **********************************************************************/
  31. /* Peripheral group ----------------------------------------------------------- */
  32. /** @addtogroup PWM
  33. * @{
  34. */
  35. /* Includes ------------------------------------------------------------------- */
  36. #include "lpc17xx_pwm.h"
  37. #include "lpc17xx_clkpwr.h"
  38. /* If this source file built with example, the LPC17xx FW library configuration
  39. * file in each example directory ("lpc17xx_libcfg.h") must be included,
  40. * otherwise the default FW library configuration file must be included instead
  41. */
  42. #ifdef __BUILD_WITH_EXAMPLE__
  43. #include "lpc17xx_libcfg.h"
  44. #else
  45. #include "lpc17xx_libcfg_default.h"
  46. #endif /* __BUILD_WITH_EXAMPLE__ */
  47. #ifdef _PWM
  48. /* Public Functions ----------------------------------------------------------- */
  49. /** @addtogroup PWM_Public_Functions
  50. * @{
  51. */
  52. /*********************************************************************//**
  53. * @brief Check whether specified interrupt flag in PWM is set or not
  54. * @param[in] PWMx: PWM peripheral, should be LPC_PWM1
  55. * @param[in] IntFlag: PWM interrupt flag, should be:
  56. * - PWM_INTSTAT_MR0: Interrupt flag for PWM match channel 0
  57. * - PWM_INTSTAT_MR1: Interrupt flag for PWM match channel 1
  58. * - PWM_INTSTAT_MR2: Interrupt flag for PWM match channel 2
  59. * - PWM_INTSTAT_MR3: Interrupt flag for PWM match channel 3
  60. * - PWM_INTSTAT_MR4: Interrupt flag for PWM match channel 4
  61. * - PWM_INTSTAT_MR5: Interrupt flag for PWM match channel 5
  62. * - PWM_INTSTAT_MR6: Interrupt flag for PWM match channel 6
  63. * - PWM_INTSTAT_CAP0: Interrupt flag for capture input 0
  64. * - PWM_INTSTAT_CAP1: Interrupt flag for capture input 1
  65. * @return New State of PWM interrupt flag (SET or RESET)
  66. **********************************************************************/
  67. IntStatus PWM_GetIntStatus(LPC_PWM_TypeDef *PWMx, uint32_t IntFlag)
  68. {
  69. CHECK_PARAM(PARAM_PWMx(PWMx));
  70. CHECK_PARAM(PARAM_PWM_INTSTAT(IntFlag));
  71. return ((PWMx->IR & IntFlag) ? SET : RESET);
  72. }
  73. /*********************************************************************//**
  74. * @brief Clear specified PWM Interrupt pending
  75. * @param[in] PWMx: PWM peripheral, should be LPC_PWM1
  76. * @param[in] IntFlag: PWM interrupt flag, should be:
  77. * - PWM_INTSTAT_MR0: Interrupt flag for PWM match channel 0
  78. * - PWM_INTSTAT_MR1: Interrupt flag for PWM match channel 1
  79. * - PWM_INTSTAT_MR2: Interrupt flag for PWM match channel 2
  80. * - PWM_INTSTAT_MR3: Interrupt flag for PWM match channel 3
  81. * - PWM_INTSTAT_MR4: Interrupt flag for PWM match channel 4
  82. * - PWM_INTSTAT_MR5: Interrupt flag for PWM match channel 5
  83. * - PWM_INTSTAT_MR6: Interrupt flag for PWM match channel 6
  84. * - PWM_INTSTAT_CAP0: Interrupt flag for capture input 0
  85. * - PWM_INTSTAT_CAP1: Interrupt flag for capture input 1
  86. * @return None
  87. **********************************************************************/
  88. void PWM_ClearIntPending(LPC_PWM_TypeDef *PWMx, uint32_t IntFlag)
  89. {
  90. CHECK_PARAM(PARAM_PWMx(PWMx));
  91. CHECK_PARAM(PARAM_PWM_INTSTAT(IntFlag));
  92. PWMx->IR = IntFlag;
  93. }
  94. /*****************************************************************************//**
  95. * @brief Fills each PWM_InitStruct member with its default value:
  96. * - If PWMCounterMode = PWM_MODE_TIMER:
  97. * + PrescaleOption = PWM_TIMER_PRESCALE_USVAL
  98. * + PrescaleValue = 1
  99. * - If PWMCounterMode = PWM_MODE_COUNTER:
  100. * + CountInputSelect = PWM_COUNTER_PCAP1_0
  101. * + CounterOption = PWM_COUNTER_RISING
  102. * @param[in] PWMTimerCounterMode Timer or Counter mode, should be:
  103. * - PWM_MODE_TIMER: Counter of PWM peripheral is in Timer mode
  104. * - PWM_MODE_COUNTER: Counter of PWM peripheral is in Counter mode
  105. * @param[in] PWM_InitStruct Pointer to structure (PWM_TIMERCFG_Type or
  106. * PWM_COUNTERCFG_Type) which will be initialized.
  107. * @return None
  108. * Note: PWM_InitStruct pointer will be assigned to corresponding structure
  109. * (PWM_TIMERCFG_Type or PWM_COUNTERCFG_Type) due to PWMTimerCounterMode.
  110. *******************************************************************************/
  111. void PWM_ConfigStructInit(uint8_t PWMTimerCounterMode, void *PWM_InitStruct)
  112. {
  113. PWM_TIMERCFG_Type *pTimeCfg;
  114. PWM_COUNTERCFG_Type *pCounterCfg;
  115. CHECK_PARAM(PARAM_PWM_TC_MODE(PWMTimerCounterMode));
  116. pTimeCfg = (PWM_TIMERCFG_Type *) PWM_InitStruct;
  117. pCounterCfg = (PWM_COUNTERCFG_Type *) PWM_InitStruct;
  118. if (PWMTimerCounterMode == PWM_MODE_TIMER )
  119. {
  120. pTimeCfg->PrescaleOption = PWM_TIMER_PRESCALE_USVAL;
  121. pTimeCfg->PrescaleValue = 1;
  122. }
  123. else if (PWMTimerCounterMode == PWM_MODE_COUNTER)
  124. {
  125. pCounterCfg->CountInputSelect = PWM_COUNTER_PCAP1_0;
  126. pCounterCfg->CounterOption = PWM_COUNTER_RISING;
  127. }
  128. }
  129. /*********************************************************************//**
  130. * @brief Initializes the PWMx peripheral corresponding to the specified
  131. * parameters in the PWM_ConfigStruct.
  132. * @param[in] PWMx PWM peripheral, should be LPC_PWM1
  133. * @param[in] PWMTimerCounterMode Timer or Counter mode, should be:
  134. * - PWM_MODE_TIMER: Counter of PWM peripheral is in Timer mode
  135. * - PWM_MODE_COUNTER: Counter of PWM peripheral is in Counter mode
  136. * @param[in] PWM_ConfigStruct Pointer to structure (PWM_TIMERCFG_Type or
  137. * PWM_COUNTERCFG_Type) which will be initialized.
  138. * @return None
  139. * Note: PWM_ConfigStruct pointer will be assigned to corresponding structure
  140. * (PWM_TIMERCFG_Type or PWM_COUNTERCFG_Type) due to PWMTimerCounterMode.
  141. **********************************************************************/
  142. void PWM_Init(LPC_PWM_TypeDef *PWMx, uint32_t PWMTimerCounterMode, void *PWM_ConfigStruct)
  143. {
  144. PWM_TIMERCFG_Type *pTimeCfg;
  145. PWM_COUNTERCFG_Type *pCounterCfg;
  146. uint64_t clkdlycnt;
  147. CHECK_PARAM(PARAM_PWMx(PWMx));
  148. CHECK_PARAM(PARAM_PWM_TC_MODE(PWMTimerCounterMode));
  149. pTimeCfg = (PWM_TIMERCFG_Type *)PWM_ConfigStruct;
  150. pCounterCfg = (PWM_COUNTERCFG_Type *)PWM_ConfigStruct;
  151. CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCPWM1, ENABLE);
  152. CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_PWM1, CLKPWR_PCLKSEL_CCLK_DIV_4);
  153. // Get peripheral clock of PWM1
  154. clkdlycnt = (uint64_t) CLKPWR_GetPCLK (CLKPWR_PCLKSEL_PWM1);
  155. // Clear all interrupts pending
  156. PWMx->IR = 0xFF & PWM_IR_BITMASK;
  157. PWMx->TCR = 0x00;
  158. PWMx->CTCR = 0x00;
  159. PWMx->MCR = 0x00;
  160. PWMx->CCR = 0x00;
  161. PWMx->PCR = 0x00;
  162. PWMx->LER = 0x00;
  163. if (PWMTimerCounterMode == PWM_MODE_TIMER)
  164. {
  165. CHECK_PARAM(PARAM_PWM_TIMER_PRESCALE(pTimeCfg->PrescaleOption));
  166. /* Absolute prescale value */
  167. if (pTimeCfg->PrescaleOption == PWM_TIMER_PRESCALE_TICKVAL)
  168. {
  169. PWMx->PR = pTimeCfg->PrescaleValue - 1;
  170. }
  171. /* uSecond prescale value */
  172. else
  173. {
  174. clkdlycnt = (clkdlycnt * pTimeCfg->PrescaleValue) / 1000000;
  175. PWMx->PR = ((uint32_t) clkdlycnt) - 1;
  176. }
  177. }
  178. else if (PWMTimerCounterMode == PWM_MODE_COUNTER)
  179. {
  180. CHECK_PARAM(PARAM_PWM_COUNTER_INPUTSEL(pCounterCfg->CountInputSelect));
  181. CHECK_PARAM(PARAM_PWM_COUNTER_EDGE(pCounterCfg->CounterOption));
  182. PWMx->CTCR |= (PWM_CTCR_MODE((uint32_t)pCounterCfg->CounterOption)) \
  183. | (PWM_CTCR_SELECT_INPUT((uint32_t)pCounterCfg->CountInputSelect));
  184. }
  185. }
  186. /*********************************************************************//**
  187. * @brief De-initializes the PWM peripheral registers to their
  188. * default reset values.
  189. * @param[in] PWMx PWM peripheral selected, should be LPC_PWM1
  190. * @return None
  191. **********************************************************************/
  192. void PWM_DeInit (LPC_PWM_TypeDef *PWMx)
  193. {
  194. CHECK_PARAM(PARAM_PWMx(PWMx));
  195. // Disable PWM control (timer, counter and PWM)
  196. PWMx->TCR = 0x00;
  197. CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCPWM1, DISABLE);
  198. }
  199. /*********************************************************************//**
  200. * @brief Enable/Disable PWM peripheral
  201. * @param[in] PWMx PWM peripheral selected, should be LPC_PWM1
  202. * @param[in] NewState New State of this function, should be:
  203. * - ENABLE: Enable PWM peripheral
  204. * - DISABLE: Disable PWM peripheral
  205. * @return None
  206. **********************************************************************/
  207. void PWM_Cmd(LPC_PWM_TypeDef *PWMx, FunctionalState NewState)
  208. {
  209. CHECK_PARAM(PARAM_PWMx(PWMx));
  210. CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));
  211. if (NewState == ENABLE)
  212. {
  213. PWMx->TCR |= PWM_TCR_PWM_ENABLE;
  214. }
  215. else
  216. {
  217. PWMx->TCR &= (~PWM_TCR_PWM_ENABLE) & PWM_TCR_BITMASK;
  218. }
  219. }
  220. /*********************************************************************//**
  221. * @brief Enable/Disable Counter in PWM peripheral
  222. * @param[in] PWMx PWM peripheral selected, should be LPC_PWM1
  223. * @param[in] NewState New State of this function, should be:
  224. * - ENABLE: Enable Counter in PWM peripheral
  225. * - DISABLE: Disable Counter in PWM peripheral
  226. * @return None
  227. **********************************************************************/
  228. void PWM_CounterCmd(LPC_PWM_TypeDef *PWMx, FunctionalState NewState)
  229. {
  230. CHECK_PARAM(PARAM_PWMx(PWMx));
  231. CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));
  232. if (NewState == ENABLE)
  233. {
  234. PWMx->TCR |= PWM_TCR_COUNTER_ENABLE;
  235. }
  236. else
  237. {
  238. PWMx->TCR &= (~PWM_TCR_COUNTER_ENABLE) & PWM_TCR_BITMASK;
  239. }
  240. }
  241. /*********************************************************************//**
  242. * @brief Reset Counter in PWM peripheral
  243. * @param[in] PWMx PWM peripheral selected, should be LPC_PWM1
  244. * @return None
  245. **********************************************************************/
  246. void PWM_ResetCounter(LPC_PWM_TypeDef *PWMx)
  247. {
  248. CHECK_PARAM(PARAM_PWMx(PWMx));
  249. PWMx->TCR |= PWM_TCR_COUNTER_RESET;
  250. PWMx->TCR &= (~PWM_TCR_COUNTER_RESET) & PWM_TCR_BITMASK;
  251. }
  252. /*********************************************************************//**
  253. * @brief Configures match for PWM peripheral
  254. * @param[in] PWMx PWM peripheral selected, should be LPC_PWM1
  255. * @param[in] PWM_MatchConfigStruct Pointer to a PWM_MATCHCFG_Type structure
  256. * that contains the configuration information for the
  257. * specified PWM match function.
  258. * @return None
  259. **********************************************************************/
  260. void PWM_ConfigMatch(LPC_PWM_TypeDef *PWMx, PWM_MATCHCFG_Type *PWM_MatchConfigStruct)
  261. {
  262. CHECK_PARAM(PARAM_PWMx(PWMx));
  263. CHECK_PARAM(PARAM_PWM1_MATCH_CHANNEL(PWM_MatchConfigStruct->MatchChannel));
  264. CHECK_PARAM(PARAM_FUNCTIONALSTATE(PWM_MatchConfigStruct->IntOnMatch));
  265. CHECK_PARAM(PARAM_FUNCTIONALSTATE(PWM_MatchConfigStruct->ResetOnMatch));
  266. CHECK_PARAM(PARAM_FUNCTIONALSTATE(PWM_MatchConfigStruct->StopOnMatch));
  267. //interrupt on MRn
  268. if (PWM_MatchConfigStruct->IntOnMatch == ENABLE)
  269. {
  270. PWMx->MCR |= PWM_MCR_INT_ON_MATCH(PWM_MatchConfigStruct->MatchChannel);
  271. }
  272. else
  273. {
  274. PWMx->MCR &= (~PWM_MCR_INT_ON_MATCH(PWM_MatchConfigStruct->MatchChannel)) \
  275. & PWM_MCR_BITMASK;
  276. }
  277. //reset on MRn
  278. if (PWM_MatchConfigStruct->ResetOnMatch == ENABLE)
  279. {
  280. PWMx->MCR |= PWM_MCR_RESET_ON_MATCH(PWM_MatchConfigStruct->MatchChannel);
  281. }
  282. else
  283. {
  284. PWMx->MCR &= (~PWM_MCR_RESET_ON_MATCH(PWM_MatchConfigStruct->MatchChannel)) \
  285. & PWM_MCR_BITMASK;
  286. }
  287. //stop on MRn
  288. if (PWM_MatchConfigStruct->StopOnMatch == ENABLE)
  289. {
  290. PWMx->MCR |= PWM_MCR_STOP_ON_MATCH(PWM_MatchConfigStruct->MatchChannel);
  291. }
  292. else
  293. {
  294. PWMx->MCR &= (~PWM_MCR_STOP_ON_MATCH(PWM_MatchConfigStruct->MatchChannel)) \
  295. & PWM_MCR_BITMASK;
  296. }
  297. }
  298. /*********************************************************************//**
  299. * @brief Configures capture input for PWM peripheral
  300. * @param[in] PWMx PWM peripheral selected, should be LPC_PWM1
  301. * @param[in] PWM_CaptureConfigStruct Pointer to a PWM_CAPTURECFG_Type structure
  302. * that contains the configuration information for the
  303. * specified PWM capture input function.
  304. * @return None
  305. **********************************************************************/
  306. void PWM_ConfigCapture(LPC_PWM_TypeDef *PWMx, PWM_CAPTURECFG_Type *PWM_CaptureConfigStruct)
  307. {
  308. CHECK_PARAM(PARAM_PWMx(PWMx));
  309. CHECK_PARAM(PARAM_PWM1_CAPTURE_CHANNEL(PWM_CaptureConfigStruct->CaptureChannel));
  310. CHECK_PARAM(PARAM_FUNCTIONALSTATE(PWM_CaptureConfigStruct->FallingEdge));
  311. CHECK_PARAM(PARAM_FUNCTIONALSTATE(PWM_CaptureConfigStruct->IntOnCaption));
  312. CHECK_PARAM(PARAM_FUNCTIONALSTATE(PWM_CaptureConfigStruct->RisingEdge));
  313. if (PWM_CaptureConfigStruct->RisingEdge == ENABLE)
  314. {
  315. PWMx->CCR |= PWM_CCR_CAP_RISING(PWM_CaptureConfigStruct->CaptureChannel);
  316. }
  317. else
  318. {
  319. PWMx->CCR &= (~PWM_CCR_CAP_RISING(PWM_CaptureConfigStruct->CaptureChannel)) \
  320. & PWM_CCR_BITMASK;
  321. }
  322. if (PWM_CaptureConfigStruct->FallingEdge == ENABLE)
  323. {
  324. PWMx->CCR |= PWM_CCR_CAP_FALLING(PWM_CaptureConfigStruct->CaptureChannel);
  325. }
  326. else
  327. {
  328. PWMx->CCR &= (~PWM_CCR_CAP_FALLING(PWM_CaptureConfigStruct->CaptureChannel)) \
  329. & PWM_CCR_BITMASK;
  330. }
  331. if (PWM_CaptureConfigStruct->IntOnCaption == ENABLE)
  332. {
  333. PWMx->CCR |= PWM_CCR_INT_ON_CAP(PWM_CaptureConfigStruct->CaptureChannel);
  334. }
  335. else
  336. {
  337. PWMx->CCR &= (~PWM_CCR_INT_ON_CAP(PWM_CaptureConfigStruct->CaptureChannel)) \
  338. & PWM_CCR_BITMASK;
  339. }
  340. }
  341. /*********************************************************************//**
  342. * @brief Read value of capture register PWM peripheral
  343. * @param[in] PWMx PWM peripheral selected, should be LPC_PWM1
  344. * @param[in] CaptureChannel: capture channel number, should be in
  345. * range 0 to 1
  346. * @return Value of capture register
  347. **********************************************************************/
  348. uint32_t PWM_GetCaptureValue(LPC_PWM_TypeDef *PWMx, uint8_t CaptureChannel)
  349. {
  350. CHECK_PARAM(PARAM_PWMx(PWMx));
  351. CHECK_PARAM(PARAM_PWM1_CAPTURE_CHANNEL(CaptureChannel));
  352. switch (CaptureChannel)
  353. {
  354. case 0:
  355. return PWMx->CR0;
  356. case 1:
  357. return PWMx->CR1;
  358. default:
  359. return (0);
  360. }
  361. }
  362. /********************************************************************//**
  363. * @brief Update value for each PWM channel with update type option
  364. * @param[in] PWMx PWM peripheral selected, should be LPC_PWM1
  365. * @param[in] MatchChannel Match channel
  366. * @param[in] MatchValue Match value
  367. * @param[in] UpdateType Type of Update, should be:
  368. * - PWM_MATCH_UPDATE_NOW: The update value will be updated for
  369. * this channel immediately
  370. * - PWM_MATCH_UPDATE_NEXT_RST: The update value will be updated for
  371. * this channel on next reset by a PWM Match event.
  372. * @return None
  373. *********************************************************************/
  374. void PWM_MatchUpdate(LPC_PWM_TypeDef *PWMx, uint8_t MatchChannel, \
  375. uint32_t MatchValue, uint8_t UpdateType)
  376. {
  377. CHECK_PARAM(PARAM_PWMx(PWMx));
  378. CHECK_PARAM(PARAM_PWM1_MATCH_CHANNEL(MatchChannel));
  379. CHECK_PARAM(PARAM_PWM_MATCH_UPDATE(UpdateType));
  380. switch (MatchChannel)
  381. {
  382. case 0:
  383. PWMx->MR0 = MatchValue;
  384. break;
  385. case 1:
  386. PWMx->MR1 = MatchValue;
  387. break;
  388. case 2:
  389. PWMx->MR2 = MatchValue;
  390. break;
  391. case 3:
  392. PWMx->MR3 = MatchValue;
  393. break;
  394. case 4:
  395. PWMx->MR4 = MatchValue;
  396. break;
  397. case 5:
  398. PWMx->MR5 = MatchValue;
  399. break;
  400. case 6:
  401. PWMx->MR6 = MatchValue;
  402. break;
  403. }
  404. // Write Latch register
  405. PWMx->LER |= PWM_LER_EN_MATCHn_LATCH(MatchChannel);
  406. // In case of update now
  407. if (UpdateType == PWM_MATCH_UPDATE_NOW)
  408. {
  409. PWMx->TCR |= PWM_TCR_COUNTER_RESET;
  410. PWMx->TCR &= (~PWM_TCR_COUNTER_RESET) & PWM_TCR_BITMASK;
  411. }
  412. }
  413. /********************************************************************//**
  414. * @brief Update value for multi PWM channel with update type option
  415. * at the same time
  416. * @param[in] PWMx PWM peripheral selected, should be LPC_PWM1
  417. * @param[in] MatchStruct Structure that contents match value of 7 pwm channels
  418. * @param[in] UpdateType Type of Update, should be:
  419. * - PWM_MATCH_UPDATE_NOW: The update value will be updated for
  420. * this channel immediately
  421. * - PWM_MATCH_UPDATE_NEXT_RST: The update value will be updated for
  422. * this channel on next reset by a PWM Match event.
  423. * @return None
  424. *********************************************************************/
  425. void PWM_MultiMatchUpdate(LPC_PWM_TypeDef *PWMx, PWM_Match_T *MatchStruct , uint8_t UpdateType)
  426. {
  427. uint8_t LatchValue = 0;
  428. uint8_t i;
  429. CHECK_PARAM(PARAM_PWMx(PWMx));
  430. CHECK_PARAM(PARAM_PWM_MATCH_UPDATE(UpdateType));
  431. //Update match value
  432. for(i=0;i<7;i++)
  433. {
  434. if(MatchStruct[i].Status == SET)
  435. {
  436. if(i<4)
  437. *((volatile unsigned int *)(&(PWMx->MR0) + i)) = MatchStruct[i].Matchvalue;
  438. else
  439. {
  440. *((volatile unsigned int *)(&(PWMx->MR4) + (i-4))) = MatchStruct[i].Matchvalue;
  441. }
  442. LatchValue |=(1<<i);
  443. }
  444. }
  445. //set update for multi-channel at the same time
  446. PWMx->LER = LatchValue;
  447. // In case of update now
  448. if (UpdateType == PWM_MATCH_UPDATE_NOW)
  449. {
  450. PWMx->TCR |= PWM_TCR_COUNTER_RESET;
  451. PWMx->TCR &= (~PWM_TCR_COUNTER_RESET) & PWM_TCR_BITMASK;
  452. }
  453. }
  454. /********************************************************************//**
  455. * @brief Configure Edge mode for each PWM channel
  456. * @param[in] PWMx PWM peripheral selected, should be LPC_PWM1
  457. * @param[in] PWMChannel PWM channel, should be in range from 2 to 6
  458. * @param[in] ModeOption PWM mode option, should be:
  459. * - PWM_CHANNEL_SINGLE_EDGE: Single Edge mode
  460. * - PWM_CHANNEL_DUAL_EDGE: Dual Edge mode
  461. * @return None
  462. * Note: PWM Channel 1 can not be selected for mode option
  463. *********************************************************************/
  464. void PWM_ChannelConfig(LPC_PWM_TypeDef *PWMx, uint8_t PWMChannel, uint8_t ModeOption)
  465. {
  466. CHECK_PARAM(PARAM_PWMx(PWMx));
  467. CHECK_PARAM(PARAM_PWM1_EDGE_MODE_CHANNEL(PWMChannel));
  468. CHECK_PARAM(PARAM_PWM_CHANNEL_EDGE(ModeOption));
  469. // Single edge mode
  470. if (ModeOption == PWM_CHANNEL_SINGLE_EDGE)
  471. {
  472. PWMx->PCR &= (~PWM_PCR_PWMSELn(PWMChannel)) & PWM_PCR_BITMASK;
  473. }
  474. // Double edge mode
  475. else if (PWM_CHANNEL_DUAL_EDGE)
  476. {
  477. PWMx->PCR |= PWM_PCR_PWMSELn(PWMChannel);
  478. }
  479. }
  480. /********************************************************************//**
  481. * @brief Enable/Disable PWM channel output
  482. * @param[in] PWMx PWM peripheral selected, should be LPC_PWM1
  483. * @param[in] PWMChannel PWM channel, should be in range from 1 to 6
  484. * @param[in] NewState New State of this function, should be:
  485. * - ENABLE: Enable this PWM channel output
  486. * - DISABLE: Disable this PWM channel output
  487. * @return None
  488. *********************************************************************/
  489. void PWM_ChannelCmd(LPC_PWM_TypeDef *PWMx, uint8_t PWMChannel, FunctionalState NewState)
  490. {
  491. CHECK_PARAM(PARAM_PWMx(PWMx));
  492. CHECK_PARAM(PARAM_PWM1_CHANNEL(PWMChannel));
  493. if (NewState == ENABLE)
  494. {
  495. PWMx->PCR |= PWM_PCR_PWMENAn(PWMChannel);
  496. }
  497. else
  498. {
  499. PWMx->PCR &= (~PWM_PCR_PWMENAn(PWMChannel)) & PWM_PCR_BITMASK;
  500. }
  501. }
  502. /**
  503. * @}
  504. */
  505. #endif /* _PWM */
  506. /**
  507. * @}
  508. */
  509. /* --------------------------------- End Of File ------------------------------ */