pwm.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  1. //*****************************************************************************
  2. //
  3. // pwm.c - API for the PWM modules
  4. //
  5. // Copyright (c) 2005-2011 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Texas Instruments (TI) is supplying this software for use solely and
  9. // exclusively on TI's microcontroller products. The software is owned by
  10. // TI and/or its suppliers, and is protected under applicable copyright
  11. // laws. You may not combine this software with "viral" open-source
  12. // software in order to form a larger program.
  13. //
  14. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  15. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  16. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  18. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  19. // DAMAGES, FOR ANY REASON WHATSOEVER.
  20. //
  21. // This is part of revision 8264 of the Stellaris Peripheral Driver Library.
  22. //
  23. //*****************************************************************************
  24. //*****************************************************************************
  25. //
  26. //! \addtogroup pwm_api
  27. //! @{
  28. //
  29. //*****************************************************************************
  30. #include "inc/hw_ints.h"
  31. #include "inc/hw_memmap.h"
  32. #include "inc/hw_pwm.h"
  33. #include "inc/hw_sysctl.h"
  34. #include "inc/hw_types.h"
  35. #include "driverlib/debug.h"
  36. #include "driverlib/interrupt.h"
  37. #include "driverlib/pwm.h"
  38. //*****************************************************************************
  39. //
  40. // Misc macros for manipulating the encoded generator and output defines used
  41. // by the API.
  42. //
  43. //*****************************************************************************
  44. #define PWM_GEN_BADDR(_mod_, _gen_) \
  45. ((_mod_) + (_gen_))
  46. #define PWM_GEN_EXT_BADDR(_mod_, _gen_) \
  47. ((_mod_) + PWM_GEN_EXT_0 + \
  48. ((_gen_) - PWM_GEN_0) * 2)
  49. #define PWM_OUT_BADDR(_mod_, _out_) \
  50. ((_mod_) + ((_out_) & 0xFFFFFFC0))
  51. #define PWM_IS_OUTPUT_ODD(_out_) \
  52. ((_out_) & 0x00000001)
  53. //*****************************************************************************
  54. //
  55. //! \internal
  56. //! Checks a PWM generator number.
  57. //!
  58. //! \param ulGen is the generator number.
  59. //!
  60. //! This function determines if a PWM generator number is valid.
  61. //!
  62. //! \return Returnes \b true if the generator number is valid and \b false
  63. //! otherwise.
  64. //
  65. //*****************************************************************************
  66. #ifdef DEBUG
  67. static tBoolean
  68. PWMGenValid(unsigned long ulGen)
  69. {
  70. return((ulGen == PWM_GEN_0) || (ulGen == PWM_GEN_1) ||
  71. (ulGen == PWM_GEN_2) || (ulGen == PWM_GEN_3));
  72. }
  73. #endif
  74. //*****************************************************************************
  75. //
  76. //! \internal
  77. //! Checks a PWM output number.
  78. //!
  79. //! \param ulPWMOut is the output number.
  80. //!
  81. //! This function determines if a PWM output number is valid.
  82. //!
  83. //! \return Returns \b true if the output number is valid and \b false
  84. //! otherwise.
  85. //
  86. //*****************************************************************************
  87. #ifdef DEBUG
  88. static tBoolean
  89. PWMOutValid(unsigned long ulPWMOut)
  90. {
  91. return((ulPWMOut == PWM_OUT_0) || (ulPWMOut == PWM_OUT_1) ||
  92. (ulPWMOut == PWM_OUT_2) || (ulPWMOut == PWM_OUT_3) ||
  93. (ulPWMOut == PWM_OUT_4) || (ulPWMOut == PWM_OUT_5) ||
  94. (ulPWMOut == PWM_OUT_6) || (ulPWMOut == PWM_OUT_7));
  95. }
  96. #endif
  97. //*****************************************************************************
  98. //
  99. //! Configures a PWM generator.
  100. //!
  101. //! \param ulBase is the base address of the PWM module.
  102. //! \param ulGen is the PWM generator to configure. This parameter must be one
  103. //! of \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2, or \b PWM_GEN_3.
  104. //! \param ulConfig is the configuration for the PWM generator.
  105. //!
  106. //! This function is used to set the mode of operation for a PWM generator.
  107. //! The counting mode, synchronization mode, and debug behavior are all
  108. //! configured. After configuration, the generator is left in the disabled
  109. //! state.
  110. //!
  111. //! A PWM generator can count in two different modes: count down mode or count
  112. //! up/down mode. In count down mode, it counts from a value down to zero,
  113. //! and then resets to the preset value, producing left-aligned PWM
  114. //! signals (that is, the rising edge of the two PWM signals produced by the
  115. //! generator occur at the same time). In count up/down mode, it counts up
  116. //! from zero to the preset value, counts back down to zero, and then repeats
  117. //! the process, producing center-aligned PWM signals (that is,
  118. //! the middle of the high/low period of the PWM signals produced by the
  119. //! generator occurs at the same time).
  120. //!
  121. //! When the PWM generator parameters (period and pulse width) are modified,
  122. //! their effect on the output PWM signals can be delayed. In synchronous
  123. //! mode, the parameter updates are not applied until a synchronization event
  124. //! occurs. This mode allows multiple parameters to be modified and take
  125. //! effect simultaneously, instead of one at a time. Additionally, parameters
  126. //! to multiple PWM generators in synchronous mode can be updated
  127. //! simultaneously, allowing them to be treated as if they were a unified
  128. //! generator. In non-synchronous mode, the parameter updates are not delayed
  129. //! until a synchronization event. In either mode, the parameter updates only
  130. //! occur when the counter is at zero to help prevent oddly formed PWM signals
  131. //! during the update (that is, a PWM pulse that is too short or too long).
  132. //!
  133. //! The PWM generator can either pause or continue running when the processor
  134. //! is stopped via the debugger. If configured to pause, it continues to
  135. //! count until it reaches zero, at which point it pauses until the
  136. //! processor is restarted. If configured to continue running, it keeps
  137. //! counting as if nothing had happened.
  138. //!
  139. //! The \e ulConfig parameter contains the desired configuration. It is the
  140. //! logical OR of the following:
  141. //!
  142. //! - \b PWM_GEN_MODE_DOWN or \b PWM_GEN_MODE_UP_DOWN to specify the counting
  143. //! mode
  144. //! - \b PWM_GEN_MODE_SYNC or \b PWM_GEN_MODE_NO_SYNC to specify the counter
  145. //! load and comparator update synchronization mode
  146. //! - \b PWM_GEN_MODE_DBG_RUN or \b PWM_GEN_MODE_DBG_STOP to specify the debug
  147. //! behavior
  148. //! - \b PWM_GEN_MODE_GEN_NO_SYNC, \b PWM_GEN_MODE_GEN_SYNC_LOCAL, or
  149. //! \b PWM_GEN_MODE_GEN_SYNC_GLOBAL to specify the update synchronization
  150. //! mode for generator counting mode changes
  151. //! - \b PWM_GEN_MODE_DB_NO_SYNC, \b PWM_GEN_MODE_DB_SYNC_LOCAL, or
  152. //! \b PWM_GEN_MODE_DB_SYNC_GLOBAL to specify the deadband parameter
  153. //! synchronization mode
  154. //! - \b PWM_GEN_MODE_FAULT_LATCHED or \b PWM_GEN_MODE_FAULT_UNLATCHED to
  155. //! specify whether fault conditions are latched or not
  156. //! - \b PWM_GEN_MODE_FAULT_MINPER or \b PWM_GEN_MODE_FAULT_NO_MINPER to
  157. //! specify whether minimum fault period support is required
  158. //! - \b PWM_GEN_MODE_FAULT_EXT or \b PWM_GEN_MODE_FAULT_LEGACY to specify
  159. //! whether extended fault source selection support is enabled or not
  160. //!
  161. //! Setting \b PWM_GEN_MODE_FAULT_MINPER allows an application to set the
  162. //! minimum duration of a PWM fault signal. Faults are signaled for at
  163. //! least this time even if the external fault pin deasserts earlier. Care
  164. //! should be taken when using this mode because during the fault signal
  165. //! period, the fault interrupt from the PWM generator remains asserted. The
  166. //! fault interrupt handler may, therefore, reenter immediately if it exits
  167. //! prior to expiration of the fault timer.
  168. //!
  169. //! \note Changes to the counter mode affect the period of the PWM signals
  170. //! produced. PWMGenPeriodSet() and PWMPulseWidthSet() should be called after
  171. //! any changes to the counter mode of a generator.
  172. //!
  173. //! \return None.
  174. //
  175. //*****************************************************************************
  176. void
  177. PWMGenConfigure(unsigned long ulBase, unsigned long ulGen,
  178. unsigned long ulConfig)
  179. {
  180. //
  181. // Check the arguments.
  182. //
  183. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  184. ASSERT(PWMGenValid(ulGen));
  185. //
  186. // Compute the generator's base address.
  187. //
  188. ulGen = PWM_GEN_BADDR(ulBase, ulGen);
  189. //
  190. // Change the global configuration of the generator.
  191. //
  192. HWREG(ulGen + PWM_O_X_CTL) = ((HWREG(ulGen + PWM_O_X_CTL) &
  193. ~(PWM_X_CTL_MODE | PWM_X_CTL_DEBUG |
  194. PWM_X_CTL_LATCH | PWM_X_CTL_MINFLTPER |
  195. PWM_X_CTL_FLTSRC | PWM_X_CTL_DBFALLUPD_M |
  196. PWM_X_CTL_DBRISEUPD_M |
  197. PWM_X_CTL_DBCTLUPD_M |
  198. PWM_X_CTL_GENBUPD_M |
  199. PWM_X_CTL_GENAUPD_M |
  200. PWM_X_CTL_LOADUPD | PWM_X_CTL_CMPAUPD |
  201. PWM_X_CTL_CMPBUPD)) | ulConfig);
  202. //
  203. // Set the individual PWM generator controls.
  204. //
  205. if(ulConfig & PWM_X_CTL_MODE)
  206. {
  207. //
  208. // In up/down count mode, set the signal high on up count comparison
  209. // and low on down count comparison (that is, center align the
  210. // signals).
  211. //
  212. HWREG(ulGen + PWM_O_X_GENA) = (PWM_X_GENA_ACTCMPAU_ONE |
  213. PWM_X_GENA_ACTCMPAD_ZERO);
  214. HWREG(ulGen + PWM_O_X_GENB) = (PWM_X_GENB_ACTCMPBU_ONE |
  215. PWM_X_GENB_ACTCMPBD_ZERO);
  216. }
  217. else
  218. {
  219. //
  220. // In down count mode, set the signal high on load and low on count
  221. // comparison (that is, left align the signals).
  222. //
  223. HWREG(ulGen + PWM_O_X_GENA) = (PWM_X_GENA_ACTLOAD_ONE |
  224. PWM_X_GENA_ACTCMPAD_ZERO);
  225. HWREG(ulGen + PWM_O_X_GENB) = (PWM_X_GENB_ACTLOAD_ONE |
  226. PWM_X_GENB_ACTCMPBD_ZERO);
  227. }
  228. }
  229. //*****************************************************************************
  230. //
  231. //! Sets the period of a PWM generator.
  232. //!
  233. //! \param ulBase is the base address of the PWM module.
  234. //! \param ulGen is the PWM generator to be modified. This parameter must be
  235. //! one of \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2, or \b PWM_GEN_3.
  236. //! \param ulPeriod specifies the period of PWM generator output, measured
  237. //! in clock ticks.
  238. //!
  239. //! This function sets the period of the specified PWM generator block, where
  240. //! the period of the generator block is defined as the number of PWM clock
  241. //! ticks between pulses on the generator block zero signal.
  242. //!
  243. //! \note Any subsequent calls made to this function before an update occurs
  244. //! cause the previous values to be overwritten.
  245. //!
  246. //! \return None.
  247. //
  248. //*****************************************************************************
  249. void
  250. PWMGenPeriodSet(unsigned long ulBase, unsigned long ulGen,
  251. unsigned long ulPeriod)
  252. {
  253. //
  254. // Check the arguments.
  255. //
  256. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  257. ASSERT(PWMGenValid(ulGen));
  258. //
  259. // Compute the generator's base address.
  260. //
  261. ulGen = PWM_GEN_BADDR(ulBase, ulGen);
  262. //
  263. // Set the reload register based on the mode.
  264. //
  265. if(HWREG(ulGen + PWM_O_X_CTL) & PWM_X_CTL_MODE)
  266. {
  267. //
  268. // In up/down count mode, set the reload register to half the requested
  269. // period.
  270. //
  271. ASSERT((ulPeriod / 2) < 65536);
  272. HWREG(ulGen + PWM_O_X_LOAD) = ulPeriod / 2;
  273. }
  274. else
  275. {
  276. //
  277. // In down count mode, set the reload register to the requested period
  278. // minus one.
  279. //
  280. ASSERT((ulPeriod <= 65536) && (ulPeriod != 0));
  281. HWREG(ulGen + PWM_O_X_LOAD) = ulPeriod - 1;
  282. }
  283. }
  284. //*****************************************************************************
  285. //
  286. //! Gets the period of a PWM generator block.
  287. //!
  288. //! \param ulBase is the base address of the PWM module.
  289. //! \param ulGen is the PWM generator to query. This parameter must be one of
  290. //! \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2, or \b PWM_GEN_3.
  291. //!
  292. //! This function gets the period of the specified PWM generator block. The
  293. //! period of the generator block is defined as the number of PWM clock ticks
  294. //! between pulses on the generator block zero signal.
  295. //!
  296. //! If the update of the counter for the specified PWM generator has yet
  297. //! to be completed, the value returned may not be the active period. The
  298. //! value returned is the programmed period, measured in PWM clock ticks.
  299. //!
  300. //! \return Returns the programmed period of the specified generator block
  301. //! in PWM clock ticks.
  302. //
  303. //*****************************************************************************
  304. unsigned long
  305. PWMGenPeriodGet(unsigned long ulBase, unsigned long ulGen)
  306. {
  307. //
  308. // Check the arguments.
  309. //
  310. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  311. ASSERT(PWMGenValid(ulGen));
  312. //
  313. // Compute the generator's base address.
  314. //
  315. ulGen = PWM_GEN_BADDR(ulBase, ulGen);
  316. //
  317. // Figure out the counter mode.
  318. //
  319. if(HWREG(ulGen + PWM_O_X_CTL) & PWM_X_CTL_MODE)
  320. {
  321. //
  322. // The period is twice the reload register value.
  323. //
  324. return(HWREG(ulGen + PWM_O_X_LOAD) * 2);
  325. }
  326. else
  327. {
  328. //
  329. // The period is the reload register value plus one.
  330. //
  331. return(HWREG(ulGen + PWM_O_X_LOAD) + 1);
  332. }
  333. }
  334. //*****************************************************************************
  335. //
  336. //! Enables the timer/counter for a PWM generator block.
  337. //!
  338. //! \param ulBase is the base address of the PWM module.
  339. //! \param ulGen is the PWM generator to be enabled. This parameter must be
  340. //! one of \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2, or \b PWM_GEN_3.
  341. //!
  342. //! This function allows the PWM clock to drive the timer/counter for the
  343. //! specified generator block.
  344. //!
  345. //! \return None.
  346. //
  347. //*****************************************************************************
  348. void
  349. PWMGenEnable(unsigned long ulBase, unsigned long ulGen)
  350. {
  351. //
  352. // Check the arguments.
  353. //
  354. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  355. ASSERT(PWMGenValid(ulGen));
  356. //
  357. // Enable the PWM generator.
  358. //
  359. HWREG(PWM_GEN_BADDR(ulBase, ulGen) + PWM_O_X_CTL) |= PWM_X_CTL_ENABLE;
  360. }
  361. //*****************************************************************************
  362. //
  363. //! Disables the timer/counter for a PWM generator block.
  364. //!
  365. //! \param ulBase is the base address of the PWM module.
  366. //! \param ulGen is the PWM generator to be disabled. This parameter must be
  367. //! one of \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2, or \b PWM_GEN_3.
  368. //!
  369. //! This function blocks the PWM clock from driving the timer/counter for the
  370. //! specified generator block.
  371. //!
  372. //! \return None.
  373. //
  374. //*****************************************************************************
  375. void
  376. PWMGenDisable(unsigned long ulBase, unsigned long ulGen)
  377. {
  378. //
  379. // Check the arguments.
  380. //
  381. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  382. ASSERT(PWMGenValid(ulGen));
  383. //
  384. // Disable the PWM generator.
  385. //
  386. HWREG(PWM_GEN_BADDR(ulBase, + ulGen) + PWM_O_X_CTL) &= ~(PWM_X_CTL_ENABLE);
  387. }
  388. //*****************************************************************************
  389. //
  390. //! Sets the pulse width for the specified PWM output.
  391. //!
  392. //! \param ulBase is the base address of the PWM module.
  393. //! \param ulPWMOut is the PWM output to modify. This parameter must be one
  394. //! of \b PWM_OUT_0, \b PWM_OUT_1, \b PWM_OUT_2, \b PWM_OUT_3, \b PWM_OUT_4,
  395. //! \b PWM_OUT_5, \b PWM_OUT_6, or \b PWM_OUT_7.
  396. //! \param ulWidth specifies the width of the positive portion of the pulse.
  397. //!
  398. //! This function sets the pulse width for the specified PWM output, where the
  399. //! pulse width is defined as the number of PWM clock ticks.
  400. //!
  401. //! \note Any subsequent calls made to this function before an update occurs
  402. //! cause the previous values to be overwritten.
  403. //!
  404. //! \return None.
  405. //
  406. //*****************************************************************************
  407. void
  408. PWMPulseWidthSet(unsigned long ulBase, unsigned long ulPWMOut,
  409. unsigned long ulWidth)
  410. {
  411. unsigned long ulGenBase, ulReg;
  412. //
  413. // Check the arguments.
  414. //
  415. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  416. ASSERT(PWMOutValid(ulPWMOut));
  417. //
  418. // Compute the generator's base address.
  419. //
  420. ulGenBase = PWM_OUT_BADDR(ulBase, ulPWMOut);
  421. //
  422. // If the counter is in up/down count mode, divide the width by two.
  423. //
  424. if(HWREG(ulGenBase + PWM_O_X_CTL) & PWM_X_CTL_MODE)
  425. {
  426. ulWidth /= 2;
  427. }
  428. //
  429. // Get the period.
  430. //
  431. ulReg = HWREG(ulGenBase + PWM_O_X_LOAD);
  432. //
  433. // Make sure the width is not too large.
  434. //
  435. ASSERT(ulWidth < ulReg);
  436. //
  437. // Compute the compare value.
  438. //
  439. ulReg = ulReg - ulWidth;
  440. //
  441. // Write to the appropriate registers.
  442. //
  443. if(PWM_IS_OUTPUT_ODD(ulPWMOut))
  444. {
  445. HWREG(ulGenBase + PWM_O_X_CMPB) = ulReg;
  446. }
  447. else
  448. {
  449. HWREG(ulGenBase + PWM_O_X_CMPA) = ulReg;
  450. }
  451. }
  452. //*****************************************************************************
  453. //
  454. //! Gets the pulse width of a PWM output.
  455. //!
  456. //! \param ulBase is the base address of the PWM module.
  457. //! \param ulPWMOut is the PWM output to query. This parameter must be one of
  458. //! \b PWM_OUT_0, \b PWM_OUT_1, \b PWM_OUT_2, \b PWM_OUT_3, \b PWM_OUT_4,
  459. //! \b PWM_OUT_5, \b PWM_OUT_6, or \b PWM_OUT_7.
  460. //!
  461. //! This function gets the currently programmed pulse width for the specified
  462. //! PWM output. If the update of the comparator for the specified output has
  463. //! yet to be completed, the value returned may not be the active pulse width.
  464. //! The value returned is the programmed pulse width, measured in PWM clock
  465. //! ticks.
  466. //!
  467. //! \return Returns the width of the pulse in PWM clock ticks.
  468. //
  469. //*****************************************************************************
  470. unsigned long
  471. PWMPulseWidthGet(unsigned long ulBase, unsigned long ulPWMOut)
  472. {
  473. unsigned long ulGenBase, ulReg, ulLoad;
  474. //
  475. // Check the arguments.
  476. //
  477. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  478. ASSERT(PWMOutValid(ulPWMOut));
  479. //
  480. // Compute the generator's base address.
  481. //
  482. ulGenBase = PWM_OUT_BADDR(ulBase, ulPWMOut);
  483. //
  484. // Then compute the pulse width. If mode is UpDown, set
  485. // width = (load - compare) * 2. Otherwise, set width = load - compare.
  486. //
  487. ulLoad = HWREG(ulGenBase + PWM_O_X_LOAD);
  488. if(PWM_IS_OUTPUT_ODD(ulPWMOut))
  489. {
  490. ulReg = HWREG(ulGenBase + PWM_O_X_CMPB);
  491. }
  492. else
  493. {
  494. ulReg = HWREG(ulGenBase + PWM_O_X_CMPA);
  495. }
  496. ulReg = ulLoad - ulReg;
  497. //
  498. // If in up/down count mode, double the pulse width.
  499. //
  500. if(HWREG(ulGenBase + PWM_O_X_CTL) & PWM_X_CTL_MODE)
  501. {
  502. ulReg = ulReg * 2;
  503. }
  504. //
  505. // Return the pulse width.
  506. //
  507. return(ulReg);
  508. }
  509. //*****************************************************************************
  510. //
  511. //! Enables the PWM dead band output and sets the dead band delays.
  512. //!
  513. //! \param ulBase is the base address of the PWM module.
  514. //! \param ulGen is the PWM generator to modify. This parameter must be one
  515. //! of \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2, or \b PWM_GEN_3.
  516. //! \param usRise specifies the width of delay from the rising edge.
  517. //! \param usFall specifies the width of delay from the falling edge.
  518. //!
  519. //! This function sets the dead bands for the specified PWM generator,
  520. //! where the dead bands are defined as the number of \b PWM clock ticks
  521. //! from the rising or falling edge of the generator's \b OutA signal.
  522. //! Note that this function causes the coupling of \b OutB to \b OutA.
  523. //!
  524. //! \return None.
  525. //
  526. //****************************************************************************
  527. void
  528. PWMDeadBandEnable(unsigned long ulBase, unsigned long ulGen,
  529. unsigned short usRise, unsigned short usFall)
  530. {
  531. //
  532. // Check the arguments.
  533. //
  534. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  535. ASSERT(PWMGenValid(ulGen));
  536. ASSERT(usRise < 4096);
  537. ASSERT(usFall < 4096);
  538. //
  539. // Compute the generator's base address.
  540. //
  541. ulGen = PWM_GEN_BADDR(ulBase, ulGen);
  542. //
  543. // Write the dead band delay values.
  544. //
  545. HWREG(ulGen + PWM_O_X_DBRISE) = usRise;
  546. HWREG(ulGen + PWM_O_X_DBFALL) = usFall;
  547. //
  548. // Enable the deadband functionality.
  549. //
  550. HWREG(ulGen + PWM_O_X_DBCTL) |= PWM_X_DBCTL_ENABLE;
  551. }
  552. //*****************************************************************************
  553. //
  554. //! Disables the PWM dead band output.
  555. //!
  556. //! \param ulBase is the base address of the PWM module.
  557. //! \param ulGen is the PWM generator to modify. This parameter must be one of
  558. //! \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2, or \b PWM_GEN_3.
  559. //!
  560. //! This function disables the dead band mode for the specified PWM generator.
  561. //! Doing so decouples the \b OutA and \b OutB signals.
  562. //!
  563. //! \return None.
  564. //
  565. //*****************************************************************************
  566. void
  567. PWMDeadBandDisable(unsigned long ulBase, unsigned long ulGen)
  568. {
  569. //
  570. // Check the arguments.
  571. //
  572. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  573. ASSERT(PWMGenValid(ulGen));
  574. //
  575. // Disable the deadband functionality.
  576. //
  577. HWREG(PWM_GEN_BADDR(ulBase, ulGen) + PWM_O_X_DBCTL) &=
  578. ~(PWM_X_DBCTL_ENABLE);
  579. }
  580. //*****************************************************************************
  581. //
  582. //! Synchronizes all pending updates.
  583. //!
  584. //! \param ulBase is the base address of the PWM module.
  585. //! \param ulGenBits are the PWM generator blocks to be updated. This
  586. //! parameter must be the logical OR of any of \b PWM_GEN_0_BIT,
  587. //! \b PWM_GEN_1_BIT, \b PWM_GEN_2_BIT, or \b PWM_GEN_3_BIT.
  588. //!
  589. //! For the selected PWM generators, this function causes all queued updates to
  590. //! the period or pulse width to be applied the next time the corresponding
  591. //! counter becomes zero.
  592. //!
  593. //! \return None.
  594. //
  595. //*****************************************************************************
  596. void
  597. PWMSyncUpdate(unsigned long ulBase, unsigned long ulGenBits)
  598. {
  599. //
  600. // Check the arguments.
  601. //
  602. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  603. ASSERT(!(ulGenBits & ~(PWM_GEN_0_BIT | PWM_GEN_1_BIT | PWM_GEN_2_BIT |
  604. PWM_GEN_3_BIT)));
  605. //
  606. // Synchronize pending PWM register changes.
  607. //
  608. HWREG(ulBase + PWM_O_CTL) = ulGenBits;
  609. }
  610. //*****************************************************************************
  611. //
  612. //! Synchronizes the counters in one or multiple PWM generator blocks.
  613. //!
  614. //! \param ulBase is the base address of the PWM module.
  615. //! \param ulGenBits are the PWM generator blocks to be synchronized. This
  616. //! parameter must be the logical OR of any of \b PWM_GEN_0_BIT,
  617. //! \b PWM_GEN_1_BIT, \b PWM_GEN_2_BIT, or \b PWM_GEN_3_BIT.
  618. //!
  619. //! For the selected PWM module, this function synchronizes the time base
  620. //! of the generator blocks by causing the specified generator counters to be
  621. //! reset to zero.
  622. //!
  623. //! \return None.
  624. //
  625. //*****************************************************************************
  626. void
  627. PWMSyncTimeBase(unsigned long ulBase, unsigned long ulGenBits)
  628. {
  629. //
  630. // Check the arguments.
  631. //
  632. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  633. ASSERT(!(ulGenBits & ~(PWM_GEN_0_BIT | PWM_GEN_1_BIT | PWM_GEN_2_BIT |
  634. PWM_GEN_3_BIT)));
  635. //
  636. // Synchronize the counters in the specified generators by writing to the
  637. // module's synchronization register.
  638. //
  639. HWREG(ulBase + PWM_O_SYNC) = ulGenBits;
  640. }
  641. //*****************************************************************************
  642. //
  643. //! Enables or disables PWM outputs.
  644. //!
  645. //! \param ulBase is the base address of the PWM module.
  646. //! \param ulPWMOutBits are the PWM outputs to be modified. This parameter
  647. //! must be the logical OR of any of \b PWM_OUT_0_BIT, \b PWM_OUT_1_BIT,
  648. //! \b PWM_OUT_2_BIT, \b PWM_OUT_3_BIT, \b PWM_OUT_4_BIT, \b PWM_OUT_5_BIT,
  649. //! \b PWM_OUT_6_BIT, or \b PWM_OUT_7_BIT.
  650. //! \param bEnable determines if the signal is enabled or disabled.
  651. //!
  652. //! This function enables or disables the selected PWM outputs. The
  653. //! outputs are selected using the parameter \e ulPWMOutBits. The parameter
  654. //! \e bEnable determines the state of the selected outputs. If \e bEnable is
  655. //! \b true, then the selected PWM outputs are enabled, or placed in the active
  656. //! state. If \e bEnable is \b false, then the selected outputs are disabled
  657. //! or placed in the inactive state.
  658. //!
  659. //! \return None.
  660. //
  661. //*****************************************************************************
  662. void
  663. PWMOutputState(unsigned long ulBase, unsigned long ulPWMOutBits,
  664. tBoolean bEnable)
  665. {
  666. //
  667. // Check the arguments.
  668. //
  669. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  670. ASSERT(!(ulPWMOutBits & ~(PWM_OUT_0_BIT | PWM_OUT_1_BIT | PWM_OUT_2_BIT |
  671. PWM_OUT_3_BIT | PWM_OUT_4_BIT | PWM_OUT_5_BIT |
  672. PWM_OUT_6_BIT | PWM_OUT_7_BIT)));
  673. //
  674. // Read the module's ENABLE output control register and set or clear the
  675. // requested bits.
  676. //
  677. if(bEnable == true)
  678. {
  679. HWREG(ulBase + PWM_O_ENABLE) |= ulPWMOutBits;
  680. }
  681. else
  682. {
  683. HWREG(ulBase + PWM_O_ENABLE) &= ~(ulPWMOutBits);
  684. }
  685. }
  686. //*****************************************************************************
  687. //
  688. //! Selects the inversion mode for PWM outputs.
  689. //!
  690. //! \param ulBase is the base address of the PWM module.
  691. //! \param ulPWMOutBits are the PWM outputs to be modified. This parameter
  692. //! must be the logical OR of any of \b PWM_OUT_0_BIT, \b PWM_OUT_1_BIT,
  693. //! \b PWM_OUT_2_BIT, \b PWM_OUT_3_BIT, \b PWM_OUT_4_BIT, \b PWM_OUT_5_BIT,
  694. //! \b PWM_OUT_6_BIT, or \b PWM_OUT_7_BIT.
  695. //! \param bInvert determines if the signal is inverted or passed through.
  696. //!
  697. //! This function is used to select the inversion mode for the selected PWM
  698. //! outputs. The outputs are selected using the parameter \e ulPWMOutBits.
  699. //! The parameter \e bInvert determines the inversion mode for the selected
  700. //! outputs. If \e bInvert is \b true, this function causes the specified
  701. //! PWM output signals to be inverted or made active low. If \e bInvert is
  702. //! \b false, the specified outputs are passed through as is or made active
  703. //! high.
  704. //!
  705. //! \return None.
  706. //
  707. //*****************************************************************************
  708. void
  709. PWMOutputInvert(unsigned long ulBase, unsigned long ulPWMOutBits,
  710. tBoolean bInvert)
  711. {
  712. //
  713. // Check the arguments.
  714. //
  715. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  716. ASSERT(!(ulPWMOutBits & ~(PWM_OUT_0_BIT | PWM_OUT_1_BIT | PWM_OUT_2_BIT |
  717. PWM_OUT_3_BIT | PWM_OUT_4_BIT | PWM_OUT_5_BIT |
  718. PWM_OUT_6_BIT | PWM_OUT_7_BIT)));
  719. //
  720. // Read the module's INVERT output control register and set or clear the
  721. // requested bits.
  722. //
  723. if(bInvert == true)
  724. {
  725. HWREG(ulBase + PWM_O_INVERT) |= ulPWMOutBits;
  726. }
  727. else
  728. {
  729. HWREG(ulBase + PWM_O_INVERT) &= ~(ulPWMOutBits);
  730. }
  731. }
  732. //*****************************************************************************
  733. //
  734. //! Specifies the level of PWM outputs suppressed in response to a fault
  735. //! condition.
  736. //!
  737. //! \param ulBase is the base address of the PWM module.
  738. //! \param ulPWMOutBits are the PWM outputs to be modified. This parameter
  739. //! must be the logical OR of any of \b PWM_OUT_0_BIT, \b PWM_OUT_1_BIT,
  740. //! \b PWM_OUT_2_BIT, \b PWM_OUT_3_BIT, \b PWM_OUT_4_BIT, \b PWM_OUT_5_BIT,
  741. //! \b PWM_OUT_6_BIT, or \b PWM_OUT_7_BIT.
  742. //! \param bDriveHigh determines if the signal is driven high or low during an
  743. //! active fault condition.
  744. //!
  745. //! This function determines whether a PWM output pin that is suppressed in
  746. //! response to a fault condition is driven high or low. The affected outputs
  747. //! are selected using the parameter \e ulPWMOutBits. The parameter
  748. //! \e bDriveHigh determines the output level for the pins identified by
  749. //! \e ulPWMOutBits. If \e bDriveHigh is \b true then the selected outputs are
  750. //! driven high when a fault is detected. If it is \e false, the pins are
  751. //! driven low.
  752. //!
  753. //! In a fault condition, pins which have not been configured to be suppressed
  754. //! via a call to PWMOutputFault() are unaffected by this function.
  755. //!
  756. //! \note This function is available only on devices which support extended
  757. //! PWM fault handling.
  758. //!
  759. //! \return None.
  760. //
  761. //*****************************************************************************
  762. void
  763. PWMOutputFaultLevel(unsigned long ulBase, unsigned long ulPWMOutBits,
  764. tBoolean bDriveHigh)
  765. {
  766. //
  767. // Check the arguments.
  768. //
  769. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  770. ASSERT(!(ulPWMOutBits & ~(PWM_OUT_0_BIT | PWM_OUT_1_BIT | PWM_OUT_2_BIT |
  771. PWM_OUT_3_BIT | PWM_OUT_4_BIT | PWM_OUT_5_BIT |
  772. PWM_OUT_6_BIT | PWM_OUT_7_BIT)));
  773. //
  774. // Read the module's FAULT output control register and set or clear the
  775. // requested bits.
  776. //
  777. if(bDriveHigh == true)
  778. {
  779. HWREG(ulBase + PWM_O_FAULTVAL) |= ulPWMOutBits;
  780. }
  781. else
  782. {
  783. HWREG(ulBase + PWM_O_FAULTVAL) &= ~(ulPWMOutBits);
  784. }
  785. }
  786. //*****************************************************************************
  787. //
  788. //! Specifies the state of PWM outputs in response to a fault condition.
  789. //!
  790. //! \param ulBase is the base address of the PWM module.
  791. //! \param ulPWMOutBits are the PWM outputs to be modified. This parameter
  792. //! must be the logical OR of any of \b PWM_OUT_0_BIT, \b PWM_OUT_1_BIT,
  793. //! \b PWM_OUT_2_BIT, \b PWM_OUT_3_BIT, \b PWM_OUT_4_BIT, \b PWM_OUT_5_BIT,
  794. //! \b PWM_OUT_6_BIT, or \b PWM_OUT_7_BIT.
  795. //! \param bFaultSuppress determines if the signal is suppressed or passed
  796. //! through during an active fault condition.
  797. //!
  798. //! This function sets the fault handling characteristics of the selected PWM
  799. //! outputs. The outputs are selected using the parameter \e ulPWMOutBits.
  800. //! The parameter \e bFaultSuppress determines the fault handling
  801. //! characteristics for the selected outputs. If \e bFaultSuppress is \b true,
  802. //! then the selected outputs are made inactive. If \e bFaultSuppress is
  803. //! \b false, then the selected outputs are unaffected by the detected fault.
  804. //!
  805. //! On devices supporting extended PWM fault handling, the state the affected
  806. //! output pins are driven to can be configured with PWMOutputFaultLevel(). If
  807. //! not configured, or if the device does not support extended PWM fault
  808. //! handling, affected outputs are driven low on a fault condition.
  809. //!
  810. //! \return None.
  811. //
  812. //*****************************************************************************
  813. void
  814. PWMOutputFault(unsigned long ulBase, unsigned long ulPWMOutBits,
  815. tBoolean bFaultSuppress)
  816. {
  817. //
  818. // Check the arguments.
  819. //
  820. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  821. ASSERT(!(ulPWMOutBits & ~(PWM_OUT_0_BIT | PWM_OUT_1_BIT | PWM_OUT_2_BIT |
  822. PWM_OUT_3_BIT | PWM_OUT_4_BIT | PWM_OUT_5_BIT |
  823. PWM_OUT_6_BIT | PWM_OUT_7_BIT)));
  824. //
  825. // Read the module's FAULT output control register and set or clear the
  826. // requested bits.
  827. //
  828. if(bFaultSuppress == true)
  829. {
  830. HWREG(ulBase + PWM_O_FAULT) |= ulPWMOutBits;
  831. }
  832. else
  833. {
  834. HWREG(ulBase + PWM_O_FAULT) &= ~(ulPWMOutBits);
  835. }
  836. }
  837. //*****************************************************************************
  838. //
  839. //! Gets the PWM generator interrupt number.
  840. //!
  841. //! \param ulBase is the base address of the PWM module.
  842. //! \param ulGen is the PWM generator in question. This parameter must be one
  843. //! of \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2, or \b PWM_GEN_3.
  844. //!
  845. //! This function returns the interrupt number of the corresponding PWM
  846. //! generator.
  847. //!
  848. //! \return Returns the interrupt number.
  849. //
  850. //*****************************************************************************
  851. static unsigned long
  852. PWMGenIntGet(unsigned long ulBase, unsigned long ulGen)
  853. {
  854. //
  855. // Determine the generator and PWM module in question.
  856. //
  857. switch(ulBase + ulGen)
  858. {
  859. //
  860. // The first PWM generator in the first PWM module.
  861. //
  862. case PWM0_BASE + PWM_GEN_0:
  863. {
  864. return(INT_PWM0_0);
  865. }
  866. //
  867. // The second PWM generator in the first PWM module.
  868. //
  869. case PWM0_BASE + PWM_GEN_1:
  870. {
  871. return(INT_PWM0_1);
  872. }
  873. //
  874. // The third PWM generator in the first PWM module.
  875. //
  876. case PWM0_BASE + PWM_GEN_2:
  877. {
  878. return(INT_PWM0_2);
  879. }
  880. //
  881. // The fourth PWM generator in the first PWM module.
  882. //
  883. case PWM0_BASE + PWM_GEN_3:
  884. {
  885. return(INT_PWM0_3);
  886. }
  887. //
  888. // The first PWM generator in the second PWM module.
  889. //
  890. case PWM1_BASE + PWM_GEN_0:
  891. {
  892. return(INT_PWM1_0);
  893. }
  894. //
  895. // The first PWM generator in the second PWM module.
  896. //
  897. case PWM1_BASE + PWM_GEN_1:
  898. {
  899. return(INT_PWM1_1);
  900. }
  901. //
  902. // The first PWM generator in the second PWM module.
  903. //
  904. case PWM1_BASE + PWM_GEN_2:
  905. {
  906. return(INT_PWM1_2);
  907. }
  908. //
  909. // The first PWM generator in the second PWM module.
  910. //
  911. case PWM1_BASE + PWM_GEN_3:
  912. {
  913. return(INT_PWM1_3);
  914. }
  915. //
  916. // An unknown PWM module/generator was specified.
  917. //
  918. default:
  919. {
  920. return(0);
  921. }
  922. }
  923. }
  924. //*****************************************************************************
  925. //
  926. //! Registers an interrupt handler for the specified PWM generator block.
  927. //!
  928. //! \param ulBase is the base address of the PWM module.
  929. //! \param ulGen is the PWM generator in question. This parameter must be one
  930. //! of \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2, or \b PWM_GEN_3.
  931. //! \param pfnIntHandler is a pointer to the function to be called when the PWM
  932. //! generator interrupt occurs.
  933. //!
  934. //! This function ensures that the interrupt handler specified by
  935. //! \e pfnIntHandler is called when an interrupt is detected for the specified
  936. //! PWM generator block. This function also enables the corresponding
  937. //! PWM generator interrupt in the interrupt controller; individual generator
  938. //! interrupts and interrupt sources must be enabled with PWMIntEnable() and
  939. //! PWMGenIntTrigEnable().
  940. //!
  941. //! \sa IntRegister() for important information about registering interrupt
  942. //! handlers.
  943. //!
  944. //! \return None.
  945. //
  946. //*****************************************************************************
  947. void
  948. PWMGenIntRegister(unsigned long ulBase, unsigned long ulGen,
  949. void (*pfnIntHandler)(void))
  950. {
  951. unsigned long ulInt;
  952. //
  953. // Check the arguments.
  954. //
  955. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  956. ASSERT(PWMGenValid(ulGen));
  957. //
  958. // Get the interrupt number associated with the specified generator.
  959. //
  960. ulInt = PWMGenIntGet(ulBase, ulGen);
  961. //
  962. // Register the interrupt handler.
  963. //
  964. IntRegister(ulInt, pfnIntHandler);
  965. //
  966. // Enable the PWMx interrupt.
  967. //
  968. IntEnable(ulInt);
  969. }
  970. //*****************************************************************************
  971. //
  972. //! Removes an interrupt handler for the specified PWM generator block.
  973. //!
  974. //! \param ulBase is the base address of the PWM module.
  975. //! \param ulGen is the PWM generator in question. This parameter must be one
  976. //! of \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2, or \b PWM_GEN_3.
  977. //!
  978. //! This function unregisters the interrupt handler for the specified
  979. //! PWM generator block. This function also disables the corresponding
  980. //! PWM generator interrupt in the interrupt controller; individual generator
  981. //! interrupts and interrupt sources must be disabled with PWMIntDisable() and
  982. //! PWMGenIntTrigDisable().
  983. //!
  984. //! \sa IntRegister() for important information about registering interrupt
  985. //! handlers.
  986. //!
  987. //! \return None.
  988. //
  989. //*****************************************************************************
  990. void
  991. PWMGenIntUnregister(unsigned long ulBase, unsigned long ulGen)
  992. {
  993. unsigned long ulInt;
  994. //
  995. // Check the arguments.
  996. //
  997. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  998. ASSERT(PWMGenValid(ulGen));
  999. //
  1000. // Get the interrupt number associated with the specified generator.
  1001. //
  1002. ulInt = PWMGenIntGet(ulBase, ulGen);
  1003. //
  1004. // Disable the PWMx interrupt.
  1005. //
  1006. IntDisable(ulInt);
  1007. //
  1008. // Unregister the interrupt handler.
  1009. //
  1010. IntUnregister(ulInt);
  1011. }
  1012. //*****************************************************************************
  1013. //
  1014. //! Gets the PWM fault interrupt number.
  1015. //!
  1016. //! \param ulBase is the base address of the PWM module.
  1017. //!
  1018. //! This function returns the fault interrupt number of the corresponding
  1019. //! PWM module.
  1020. //!
  1021. //! \return Returns the interrupt number.
  1022. //
  1023. //*****************************************************************************
  1024. static unsigned long
  1025. PWMFaultIntGet(unsigned long ulBase)
  1026. {
  1027. //
  1028. // Return the fault interrupt number.
  1029. //
  1030. return((ulBase == PWM0_BASE) ? INT_PWM0_FAULT : INT_PWM1_FAULT);
  1031. }
  1032. //*****************************************************************************
  1033. //
  1034. //! Registers an interrupt handler for a fault condition detected in a PWM
  1035. //! module.
  1036. //!
  1037. //! \param ulBase is the base address of the PWM module.
  1038. //! \param pfnIntHandler is a pointer to the function to be called when the PWM
  1039. //! fault interrupt occurs.
  1040. //!
  1041. //! This function ensures that the interrupt handler specified by
  1042. //! \e pfnIntHandler is called when a fault interrupt is detected for the
  1043. //! selected PWM module. This function also enables the PWM fault
  1044. //! interrupt in the NVIC; the PWM fault interrupt must also be enabled at the
  1045. //! module level using PWMIntEnable().
  1046. //!
  1047. //! \sa IntRegister() for important information about registering interrupt
  1048. //! handlers.
  1049. //!
  1050. //! \return None.
  1051. //
  1052. //*****************************************************************************
  1053. void
  1054. PWMFaultIntRegister(unsigned long ulBase, void (*pfnIntHandler)(void))
  1055. {
  1056. unsigned long ulInt;
  1057. //
  1058. // Check the arguments.
  1059. //
  1060. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  1061. //
  1062. // Get the interrupt number associated with the specified module.
  1063. //
  1064. ulInt = PWMFaultIntGet(ulBase);
  1065. //
  1066. // Register the interrupt handler, returning an error if one occurs.
  1067. //
  1068. IntRegister(ulInt, pfnIntHandler);
  1069. //
  1070. // Enable the PWM fault interrupt.
  1071. //
  1072. IntEnable(ulInt);
  1073. }
  1074. //*****************************************************************************
  1075. //
  1076. //! Removes the PWM fault condition interrupt handler.
  1077. //!
  1078. //! \param ulBase is the base address of the PWM module.
  1079. //!
  1080. //! This function removes the interrupt handler for a PWM fault interrupt
  1081. //! from the selected PWM module. This function also disables the PWM
  1082. //! fault interrupt in the NVIC; the PWM fault interrupt must also be disabled
  1083. //! at the module level using PWMIntDisable().
  1084. //!
  1085. //! \sa IntRegister() for important information about registering interrupt
  1086. //! handlers.
  1087. //!
  1088. //! \return None.
  1089. //
  1090. //*****************************************************************************
  1091. void
  1092. PWMFaultIntUnregister(unsigned long ulBase)
  1093. {
  1094. unsigned long ulInt;
  1095. //
  1096. // Check the arguments.
  1097. //
  1098. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  1099. //
  1100. // Get the interrupt number associated with the specified module.
  1101. //
  1102. ulInt = PWMFaultIntGet(ulBase);
  1103. //
  1104. // Disable the PWM fault interrupt.
  1105. //
  1106. IntDisable(ulInt);
  1107. //
  1108. // Unregister the interrupt handler, returning an error if one occurs.
  1109. //
  1110. IntUnregister(ulInt);
  1111. }
  1112. //*****************************************************************************
  1113. //
  1114. //! Enables interrupts and triggers for the specified PWM generator block.
  1115. //!
  1116. //! \param ulBase is the base address of the PWM module.
  1117. //! \param ulGen is the PWM generator to have interrupts and triggers enabled.
  1118. //! This parameter must be one of \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2, or
  1119. //! \b PWM_GEN_3.
  1120. //! \param ulIntTrig specifies the interrupts and triggers to be enabled.
  1121. //!
  1122. //! This function unmasks the specified interrupt(s) and trigger(s) by setting
  1123. //! the specified bits of the interrupt/trigger enable register for the
  1124. //! specified PWM generator. The \e ulIntTrig parameter is the logical OR of
  1125. //! \b PWM_INT_CNT_ZERO, \b PWM_INT_CNT_LOAD, \b PWM_INT_CNT_AU,
  1126. //! \b PWM_INT_CNT_AD, \b PWM_INT_CNT_BU, \b PWM_INT_CNT_BD,
  1127. //! \b PWM_TR_CNT_ZERO, \b PWM_TR_CNT_LOAD, \b PWM_TR_CNT_AU, \b PWM_TR_CNT_AD,
  1128. //! \b PWM_TR_CNT_BU, or \b PWM_TR_CNT_BD.
  1129. //!
  1130. //! \return None.
  1131. //
  1132. //*****************************************************************************
  1133. void
  1134. PWMGenIntTrigEnable(unsigned long ulBase, unsigned long ulGen,
  1135. unsigned long ulIntTrig)
  1136. {
  1137. //
  1138. // Check the arguments.
  1139. //
  1140. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  1141. ASSERT(PWMGenValid(ulGen));
  1142. ASSERT((ulIntTrig & ~(PWM_INT_CNT_ZERO | PWM_INT_CNT_LOAD |
  1143. PWM_INT_CNT_AU | PWM_INT_CNT_AD | PWM_INT_CNT_BU |
  1144. PWM_INT_CNT_BD | PWM_TR_CNT_ZERO | PWM_TR_CNT_LOAD |
  1145. PWM_TR_CNT_AU | PWM_TR_CNT_AD | PWM_TR_CNT_BU |
  1146. PWM_TR_CNT_BD)) == 0);
  1147. //
  1148. // Enable the specified interrupts/triggers.
  1149. //
  1150. HWREG(PWM_GEN_BADDR(ulBase, ulGen) + PWM_O_X_INTEN) |= ulIntTrig;
  1151. }
  1152. //*****************************************************************************
  1153. //
  1154. //! Disables interrupts for the specified PWM generator block.
  1155. //!
  1156. //! \param ulBase is the base address of the PWM module.
  1157. //! \param ulGen is the PWM generator to have interrupts and triggers disabled.
  1158. //! This parameter must be one of \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2, or
  1159. //! \b PWM_GEN_3.
  1160. //! \param ulIntTrig specifies the interrupts and triggers to be disabled.
  1161. //!
  1162. //! This function masks the specified interrupt(s) and trigger(s) by clearing
  1163. //! the specified bits of the interrupt/trigger enable register for the
  1164. //! specified PWM generator. The \e ulIntTrig parameter is the logical OR of
  1165. //! \b PWM_INT_CNT_ZERO, \b PWM_INT_CNT_LOAD, \b PWM_INT_CNT_AU,
  1166. //! \b PWM_INT_CNT_AD, \b PWM_INT_CNT_BU, \b PWM_INT_CNT_BD,
  1167. //! \b PWM_TR_CNT_ZERO, \b PWM_TR_CNT_LOAD, \b PWM_TR_CNT_AU, \b PWM_TR_CNT_AD,
  1168. //! \b PWM_TR_CNT_BU, or \b PWM_TR_CNT_BD.
  1169. //!
  1170. //! \return None.
  1171. //
  1172. //*****************************************************************************
  1173. void
  1174. PWMGenIntTrigDisable(unsigned long ulBase, unsigned long ulGen,
  1175. unsigned long ulIntTrig)
  1176. {
  1177. //
  1178. // Check the arguments.
  1179. //
  1180. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  1181. ASSERT(PWMGenValid(ulGen));
  1182. ASSERT((ulIntTrig & ~(PWM_INT_CNT_ZERO | PWM_INT_CNT_LOAD |
  1183. PWM_INT_CNT_AU | PWM_INT_CNT_AD | PWM_INT_CNT_BU |
  1184. PWM_INT_CNT_BD | PWM_TR_CNT_ZERO | PWM_TR_CNT_LOAD |
  1185. PWM_TR_CNT_AU | PWM_TR_CNT_AD | PWM_TR_CNT_BU |
  1186. PWM_TR_CNT_BD)) == 0);
  1187. //
  1188. // Disable the specified interrupts/triggers.
  1189. //
  1190. HWREG(PWM_GEN_BADDR(ulBase, ulGen) + PWM_O_X_INTEN) &= ~(ulIntTrig);
  1191. }
  1192. //*****************************************************************************
  1193. //
  1194. //! Gets interrupt status for the specified PWM generator block.
  1195. //!
  1196. //! \param ulBase is the base address of the PWM module.
  1197. //! \param ulGen is the PWM generator to query. This parameter must be one of
  1198. //! \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2, or \b PWM_GEN_3.
  1199. //! \param bMasked specifies whether masked or raw interrupt status is
  1200. //! returned.
  1201. //!
  1202. //! If \e bMasked is set as \b true, then the masked interrupt status is
  1203. //! returned; otherwise, the raw interrupt status is returned.
  1204. //!
  1205. //! \return Returns the contents of the interrupt status register or the
  1206. //! contents of the raw interrupt status register for the specified
  1207. //! PWM generator.
  1208. //
  1209. //*****************************************************************************
  1210. unsigned long
  1211. PWMGenIntStatus(unsigned long ulBase, unsigned long ulGen, tBoolean bMasked)
  1212. {
  1213. //
  1214. // Check the arguments.
  1215. //
  1216. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  1217. ASSERT(PWMGenValid(ulGen));
  1218. //
  1219. // Compute the generator's base address.
  1220. //
  1221. ulGen = PWM_GEN_BADDR(ulBase, ulGen);
  1222. //
  1223. // Read and return the specified generator's raw or enabled interrupt
  1224. // status.
  1225. //
  1226. if(bMasked == true)
  1227. {
  1228. return(HWREG(ulGen + PWM_O_X_ISC));
  1229. }
  1230. else
  1231. {
  1232. return(HWREG(ulGen + PWM_O_X_RIS));
  1233. }
  1234. }
  1235. //*****************************************************************************
  1236. //
  1237. //! Clears the specified interrupt(s) for the specified PWM generator block.
  1238. //!
  1239. //! \param ulBase is the base address of the PWM module.
  1240. //! \param ulGen is the PWM generator to query. This parameter must be one of
  1241. //! \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2, or \b PWM_GEN_3.
  1242. //! \param ulInts specifies the interrupts to be cleared.
  1243. //!
  1244. //! This funciton clears the specified interrupt(s) by writing a 1 to the
  1245. //! specified bits of the interrupt status register for the specified PWM
  1246. //! generator. The \e ulInts parameter is the logical OR of
  1247. //! \b PWM_INT_CNT_ZERO, \b PWM_INT_CNT_LOAD, \b PWM_INT_CNT_AU,
  1248. //! \b PWM_INT_CNT_AD, \b PWM_INT_CNT_BU, or \b PWM_INT_CNT_BD.
  1249. //!
  1250. //! \note Because there is a write buffer in the Cortex-M processor, it may
  1251. //! take several clock cycles before the interrupt source is actually cleared.
  1252. //! Therefore, it is recommended that the interrupt source be cleared early in
  1253. //! the interrupt handler (as opposed to the very last action) to avoid
  1254. //! returning from the interrupt handler before the interrupt source is
  1255. //! actually cleared. Failure to do so may result in the interrupt handler
  1256. //! being immediately reentered (because the interrupt controller still sees
  1257. //! the interrupt source asserted).
  1258. //!
  1259. //! \return None.
  1260. //
  1261. //*****************************************************************************
  1262. void
  1263. PWMGenIntClear(unsigned long ulBase, unsigned long ulGen, unsigned long ulInts)
  1264. {
  1265. //
  1266. // Check the arguments.
  1267. //
  1268. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  1269. ASSERT(PWMGenValid(ulGen));
  1270. ASSERT((ulInts & ~(PWM_INT_CNT_ZERO | PWM_INT_CNT_LOAD | PWM_INT_CNT_AU |
  1271. PWM_INT_CNT_AD | PWM_INT_CNT_BU | PWM_INT_CNT_BD)) ==
  1272. 0);
  1273. //
  1274. // Clear the requested interrupts by writing ones to the specified bit
  1275. // of the module's interrupt enable register.
  1276. //
  1277. HWREG(PWM_GEN_BADDR(ulBase, ulGen) + PWM_O_X_ISC) = ulInts;
  1278. }
  1279. //*****************************************************************************
  1280. //
  1281. //! Enables generator and fault interrupts for a PWM module.
  1282. //!
  1283. //! \param ulBase is the base address of the PWM module.
  1284. //! \param ulGenFault contains the interrupts to be enabled. This parameter
  1285. //! must be a logical OR of any of \b PWM_INT_GEN_0, \b PWM_INT_GEN_1,
  1286. //! \b PWM_INT_GEN_2, \b PWM_INT_GEN_3, \b PWM_INT_FAULT0, \b PWM_INT_FAULT1,
  1287. //! \b PWM_INT_FAULT2, or \b PWM_INT_FAULT3.
  1288. //!
  1289. //! This function unmasks the specified interrupt(s) by setting the specified
  1290. //! bits of the interrupt enable register for the selected PWM module.
  1291. //!
  1292. //! \return None.
  1293. //
  1294. //*****************************************************************************
  1295. void
  1296. PWMIntEnable(unsigned long ulBase, unsigned long ulGenFault)
  1297. {
  1298. //
  1299. // Check the arguments.
  1300. //
  1301. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  1302. ASSERT((ulGenFault & ~(PWM_INT_GEN_0 | PWM_INT_GEN_1 | PWM_INT_GEN_2 |
  1303. PWM_INT_GEN_3 | PWM_INT_FAULT0 | PWM_INT_FAULT1 |
  1304. PWM_INT_FAULT2 | PWM_INT_FAULT3)) == 0);
  1305. //
  1306. // Read the module's interrupt enable register and enable interrupts
  1307. // for the specified PWM generators.
  1308. //
  1309. HWREG(ulBase + PWM_O_INTEN) |= ulGenFault;
  1310. }
  1311. //*****************************************************************************
  1312. //
  1313. //! Disables generator and fault interrupts for a PWM module.
  1314. //!
  1315. //! \param ulBase is the base address of the PWM module.
  1316. //! \param ulGenFault contains the interrupts to be disabled. This parameter
  1317. //! must be a logical OR of any of \b PWM_INT_GEN_0, \b PWM_INT_GEN_1,
  1318. //! \b PWM_INT_GEN_2, \b PWM_INT_GEN_3, \b PWM_INT_FAULT0, \b PWM_INT_FAULT1,
  1319. //! \b PWM_INT_FAULT2, or \b PWM_INT_FAULT3.
  1320. //!
  1321. //! This function masks the specified interrupt(s) by clearing the specified
  1322. //! bits of the interrupt enable register for the selected PWM module.
  1323. //!
  1324. //! \return None.
  1325. //
  1326. //*****************************************************************************
  1327. void
  1328. PWMIntDisable(unsigned long ulBase, unsigned long ulGenFault)
  1329. {
  1330. //
  1331. // Check the arguments.
  1332. //
  1333. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  1334. ASSERT((ulGenFault & ~(PWM_INT_GEN_0 | PWM_INT_GEN_1 | PWM_INT_GEN_2 |
  1335. PWM_INT_GEN_3 | PWM_INT_FAULT0 | PWM_INT_FAULT1 |
  1336. PWM_INT_FAULT2 | PWM_INT_FAULT3)) == 0);
  1337. //
  1338. // Read the module's interrupt enable register and disable interrupts
  1339. // for the specified PWM generators.
  1340. //
  1341. HWREG(ulBase + PWM_O_INTEN) &= ~(ulGenFault);
  1342. }
  1343. //*****************************************************************************
  1344. //
  1345. //! Clears the fault interrupt for a PWM module.
  1346. //!
  1347. //! \param ulBase is the base address of the PWM module.
  1348. //!
  1349. //! This function clears the fault interrupt by writing to the appropriate bit
  1350. //! of the interrupt status register for the selected PWM module.
  1351. //!
  1352. //! This function clears only the FAULT0 interrupt and is retained for
  1353. //! backwards compatibility. It is recommended that PWMFaultIntClearExt() be
  1354. //! used instead because it supports all fault interrupts supported on devices
  1355. //! with and without extended PWM fault handling support.
  1356. //!
  1357. //! \note Because there is a write buffer in the Cortex-M processor, it may
  1358. //! take several clock cycles before the interrupt source is actually cleared.
  1359. //! Therefore, it is recommended that the interrupt source be cleared early in
  1360. //! the interrupt handler (as opposed to the very last action) to avoid
  1361. //! returning from the interrupt handler before the interrupt source is
  1362. //! actually cleared. Failure to do so may result in the interrupt handler
  1363. //! being immediately reentered (because the interrupt controller still sees
  1364. //! the interrupt source asserted).
  1365. //!
  1366. //! \return None.
  1367. //
  1368. //*****************************************************************************
  1369. void
  1370. PWMFaultIntClear(unsigned long ulBase)
  1371. {
  1372. //
  1373. // Check the arguments.
  1374. //
  1375. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  1376. //
  1377. // Write the only writeable bit in the module's interrupt register.
  1378. //
  1379. HWREG(ulBase + PWM_O_ISC) = PWM_ISC_INTFAULT0;
  1380. }
  1381. //*****************************************************************************
  1382. //
  1383. //! Gets the interrupt status for a PWM module.
  1384. //!
  1385. //! \param ulBase is the base address of the PWM module.
  1386. //! \param bMasked specifies whether masked or raw interrupt status is
  1387. //! returned.
  1388. //!
  1389. //! If \e bMasked is set as \b true, then the masked interrupt status is
  1390. //! returned; otherwise, the raw interrupt status is returned.
  1391. //!
  1392. //! \return The current interrupt status, enumerated as a bit field of
  1393. //! \b PWM_INT_GEN_0, \b PWM_INT_GEN_1, \b PWM_INT_GEN_2, \b PWM_INT_GEN_3,
  1394. //! \b PWM_INT_FAULT0, \b PWM_INT_FAULT1, \b PWM_INT_FAULT2, and
  1395. //! \b PWM_INT_FAULT3.
  1396. //!
  1397. //*****************************************************************************
  1398. unsigned long
  1399. PWMIntStatus(unsigned long ulBase, tBoolean bMasked)
  1400. {
  1401. //
  1402. // Check the arguments.
  1403. //
  1404. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  1405. //
  1406. // Read and return either the module's raw or enabled interrupt status.
  1407. //
  1408. if(bMasked == true)
  1409. {
  1410. return(HWREG(ulBase + PWM_O_ISC));
  1411. }
  1412. else
  1413. {
  1414. return(HWREG(ulBase + PWM_O_RIS));
  1415. }
  1416. }
  1417. //*****************************************************************************
  1418. //
  1419. //! Clears the fault interrupt for a PWM module.
  1420. //!
  1421. //! \param ulBase is the base address of the PWM module.
  1422. //! \param ulFaultInts specifies the fault interrupts to clear.
  1423. //!
  1424. //! This function clears one or more fault interrupts by writing to the
  1425. //! appropriate bit of the PWM interrupt status register. The parameter
  1426. //! \e ulFaultInts must be the logical OR of any of \b PWM_INT_FAULT0,
  1427. //! \b PWM_INT_FAULT1, \b PWM_INT_FAULT2, or \b PWM_INT_FAULT3.
  1428. //!
  1429. //! When running on a device supporting extended PWM fault handling, the fault
  1430. //! interrupts are derived by performing a logical OR of each of the configured
  1431. //! fault trigger signals for a given generator. Therefore, these interrupts
  1432. //! are not directly related to the four possible FAULTn inputs to the device
  1433. //! but indicate that a fault has been signaled to one of the four possible PWM
  1434. //! generators. On a device without extended PWM fault handling, the interrupt
  1435. //! is directly related to the state of the single FAULT pin.
  1436. //!
  1437. //! \note Because there is a write buffer in the Cortex-M processor, it may
  1438. //! take several clock cycles before the interrupt source is actually cleared.
  1439. //! Therefore, it is recommended that the interrupt source be cleared early in
  1440. //! the interrupt handler (as opposed to the very last action) to avoid
  1441. //! returning from the interrupt handler before the interrupt source is
  1442. //! actually cleared. Failure to do so may result in the interrupt handler
  1443. //! being immediately reentered (because the interrupt controller still sees
  1444. //! the interrupt source asserted).
  1445. //!
  1446. //! \return None.
  1447. //
  1448. //*****************************************************************************
  1449. void
  1450. PWMFaultIntClearExt(unsigned long ulBase, unsigned long ulFaultInts)
  1451. {
  1452. //
  1453. // Check the arguments.
  1454. //
  1455. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  1456. ASSERT((ulFaultInts & ~(PWM_INT_FAULT0 | PWM_INT_FAULT1 |
  1457. PWM_INT_FAULT2 | PWM_INT_FAULT3)) == 0);
  1458. //
  1459. // Clear the supplied fault bits.
  1460. //
  1461. HWREG(ulBase + PWM_O_ISC) = ulFaultInts;
  1462. }
  1463. //*****************************************************************************
  1464. //
  1465. //! Configures the minimum fault period and fault pin senses for a given
  1466. //! PWM generator.
  1467. //!
  1468. //! \param ulBase is the base address of the PWM module.
  1469. //! \param ulGen is the PWM generator for which fault configuration is being
  1470. //! set. This function must be one of \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2,
  1471. //! or \b PWM_GEN_3.
  1472. //! \param ulMinFaultPeriod is the minimum fault active period expressed in
  1473. //! PWM clock cycles.
  1474. //! \param ulFaultSenses indicates which sense of each FAULT input should be
  1475. //! considered the ``asserted'' state. Valid values are logical OR
  1476. //! combinations of \b PWM_FAULTn_SENSE_HIGH and \b PWM_FAULTn_SENSE_LOW.
  1477. //!
  1478. //! This function configures the minimum fault period for a given generator
  1479. //! along with the sense of each of the 4 possible fault inputs. The minimum
  1480. //! fault period is expressed in PWM clock cycles and takes effect only if
  1481. //! PWMGenConfigure() is called with flag \b PWM_GEN_MODE_FAULT_PER set in the
  1482. //! \e ulConfig parameter. When a fault input is asserted, the minimum fault
  1483. //! period timer ensures that it remains asserted for at least the number of
  1484. //! clock cycles specified.
  1485. //!
  1486. //! \note This function is only available on devices supporting extended PWM
  1487. //! fault handling.
  1488. //!
  1489. //! \return None.
  1490. //
  1491. //*****************************************************************************
  1492. void
  1493. PWMGenFaultConfigure(unsigned long ulBase, unsigned long ulGen,
  1494. unsigned long ulMinFaultPeriod,
  1495. unsigned long ulFaultSenses)
  1496. {
  1497. //
  1498. // Check the arguments.
  1499. //
  1500. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  1501. ASSERT(PWMGenValid(ulGen));
  1502. ASSERT(ulMinFaultPeriod < PWM_X_MINFLTPER_M);
  1503. ASSERT((ulFaultSenses & ~(PWM_FAULT0_SENSE_HIGH | PWM_FAULT0_SENSE_LOW |
  1504. PWM_FAULT1_SENSE_HIGH | PWM_FAULT1_SENSE_LOW |
  1505. PWM_FAULT2_SENSE_HIGH | PWM_FAULT2_SENSE_LOW |
  1506. PWM_FAULT3_SENSE_HIGH | PWM_FAULT3_SENSE_LOW)) ==
  1507. 0);
  1508. //
  1509. // Write the minimum fault period.
  1510. //
  1511. HWREG(PWM_GEN_BADDR(ulBase, ulGen) + PWM_O_X_MINFLTPER) = ulMinFaultPeriod;
  1512. //
  1513. // Write the fault senses.
  1514. //
  1515. HWREG(PWM_GEN_EXT_BADDR(ulBase, ulGen) + PWM_O_X_FLTSEN) = ulFaultSenses;
  1516. }
  1517. //*****************************************************************************
  1518. //
  1519. //! Configures the set of fault triggers for a given PWM generator.
  1520. //!
  1521. //! \param ulBase is the base address of the PWM module.
  1522. //! \param ulGen is the PWM generator for which fault triggers are being set.
  1523. //! This parameter must be one of \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2, or
  1524. //! \b PWM_GEN_3.
  1525. //! \param ulGroup indicates the subset of possible faults that are to be
  1526. //! configured. This parameter must be \b PWM_FAULT_GROUP_0 or
  1527. //! \b PWM_FAULT_GROUP_1.
  1528. //! \param ulFaultTriggers defines the set of inputs that are to contribute
  1529. //! towards generation of the fault signal to the given PWM generator. For
  1530. //! \b PWM_FAULT_GROUP_0, this is the logical OR of \b PWM_FAULT_FAULT0,
  1531. //! \b PWM_FAULT_FAULT1, \b PWM_FAULT_FAULT2, or \b PWM_FAULT_FAULT3. For
  1532. //! \b PWM_FAULT_GROUP_1, this is the logical OR of \b PWM_FAULT_DCMP0,
  1533. //! \b PWM_FAULT_DCMP1, \b PWM_FAULT_DCMP2, \b PWM_FAULT_DCMP3,
  1534. //! \b PWM_FAULT_DCMP4, \b PWM_FAULT_DCMP5, \b PWM_FAULT_DCMP6, or
  1535. //! \b PWM_FAULT_DCMP7.
  1536. //!
  1537. //! This function allows selection of the set of fault inputs that is combined
  1538. //! to generate a fault condition to a given PWM generator. By default, all
  1539. //! generators use only FAULT0 (for backwards compatibility) but if
  1540. //! PWMGenConfigure() is called with flag \b PWM_GEN_MODE_FAULT_SRC in the
  1541. //! \e ulConfig parameter, extended fault handling is enabled and this function
  1542. //! must be called to configure the fault triggers.
  1543. //!
  1544. //! The fault signal to the PWM generator is generated by ORing together each
  1545. //! of the signals specified in the \e ulFaultTriggers parameter after having
  1546. //! adjusted the sense of each FAULTn input based on the configuration
  1547. //! previously set using a call to PWMGenFaultConfigure().
  1548. //!
  1549. //! \note This function is only available on devices supporting extended PWM
  1550. //! fault handling.
  1551. //!
  1552. //! \return None.
  1553. //
  1554. //*****************************************************************************
  1555. void
  1556. PWMGenFaultTriggerSet(unsigned long ulBase, unsigned long ulGen,
  1557. unsigned long ulGroup, unsigned long ulFaultTriggers)
  1558. {
  1559. //
  1560. // Check for valid parameters.
  1561. //
  1562. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  1563. ASSERT(PWMGenValid(ulGen));
  1564. ASSERT((ulGroup == PWM_FAULT_GROUP_0) || (ulGroup == PWM_FAULT_GROUP_1));
  1565. ASSERT((ulGroup == PWM_FAULT_GROUP_0) &&
  1566. ((ulFaultTriggers & ~(PWM_FAULT_FAULT0 | PWM_FAULT_FAULT1 |
  1567. PWM_FAULT_FAULT2 | PWM_FAULT_FAULT3)) == 0));
  1568. ASSERT((ulGroup == PWM_FAULT_GROUP_1) &&
  1569. ((ulFaultTriggers & ~(PWM_FAULT_DCMP0 | PWM_FAULT_DCMP1 |
  1570. PWM_FAULT_DCMP2 | PWM_FAULT_DCMP3 |
  1571. PWM_FAULT_DCMP4 | PWM_FAULT_DCMP5 |
  1572. PWM_FAULT_DCMP6 | PWM_FAULT_DCMP7)) == 0));
  1573. //
  1574. // Write the fault triggers to the appropriate register.
  1575. //
  1576. if(ulGroup == PWM_FAULT_GROUP_0)
  1577. {
  1578. HWREG(PWM_GEN_BADDR(ulBase, ulGen) + PWM_O_X_FLTSRC0) =
  1579. ulFaultTriggers;
  1580. }
  1581. else
  1582. {
  1583. HWREG(PWM_GEN_BADDR(ulBase, ulGen) + PWM_O_X_FLTSRC1) =
  1584. ulFaultTriggers;
  1585. }
  1586. }
  1587. //*****************************************************************************
  1588. //
  1589. //! Returns the set of fault triggers currently configured for a given PWM
  1590. //! generator.
  1591. //!
  1592. //! \param ulBase is the base address of the PWM module.
  1593. //! \param ulGen is the PWM generator for which fault triggers are being
  1594. //! queried. This parameter must be one of \b PWM_GEN_0, \b PWM_GEN_1,
  1595. //! \b PWM_GEN_2, or \b PWM_GEN_3.
  1596. //! \param ulGroup indicates the subset of faults that are being queried. This
  1597. //! parameter must be \b PWM_FAULT_GROUP_0 or \b PWM_FAULT_GROUP_1.
  1598. //!
  1599. //! This function allows an application to query the current set of inputs that
  1600. //! contribute to the generation of a fault condition to a given PWM generator.
  1601. //!
  1602. //! \note This function is only available on devices supporting extended PWM
  1603. //! fault handling.
  1604. //!
  1605. //! \return Returns the current fault triggers configured for the fault group
  1606. //! provided. For \b PWM_FAULT_GROUP_0, the returned value is a logical OR of
  1607. //! \b PWM_FAULT_FAULT0, \b PWM_FAULT_FAULT1, \b PWM_FAULT_FAULT2, or
  1608. //! \b PWM_FAULT_FAULT3. For \b PWM_FAULT_GROUP_1, the return value is the
  1609. //! logical OR of \b PWM_FAULT_DCMP0, \b PWM_FAULT_DCMP1,
  1610. //! \b PWM_FAULT_DCMP2, \b PWM_FAULT_DCMP3, \b PWM_FAULT_DCMP4,
  1611. //! \b PWM_FAULT_DCMP5, \b PWM_FAULT_DCMP6, or \b PWM_FAULT_DCMP7.
  1612. //
  1613. //*****************************************************************************
  1614. unsigned long
  1615. PWMGenFaultTriggerGet(unsigned long ulBase, unsigned long ulGen,
  1616. unsigned long ulGroup)
  1617. {
  1618. //
  1619. // Check for valid parameters.
  1620. //
  1621. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  1622. ASSERT(PWMGenValid(ulGen));
  1623. ASSERT((ulGroup == PWM_FAULT_GROUP_0) || (ulGroup == PWM_FAULT_GROUP_1));
  1624. //
  1625. // Return the current fault triggers.
  1626. //
  1627. if(ulGroup == PWM_FAULT_GROUP_0)
  1628. {
  1629. return(HWREG(PWM_GEN_BADDR(ulBase, ulGen) + PWM_O_X_FLTSRC0));
  1630. }
  1631. else
  1632. {
  1633. return(HWREG(PWM_GEN_BADDR(ulBase, ulGen) + PWM_O_X_FLTSRC1));
  1634. }
  1635. }
  1636. //*****************************************************************************
  1637. //
  1638. //! Returns the current state of the fault triggers for a given PWM generator.
  1639. //!
  1640. //! \param ulBase is the base address of the PWM module.
  1641. //! \param ulGen is the PWM generator for which fault trigger states are being
  1642. //! queried. This parameter must be one of \b PWM_GEN_0, \b PWM_GEN_1,
  1643. //! \b PWM_GEN_2, or \b PWM_GEN_3.
  1644. //! \param ulGroup indicates the subset of faults that are being queried. This
  1645. //! parameter must be \b PWM_FAULT_GROUP_0 or \b PWM_FAULT_GROUP_1.
  1646. //!
  1647. //! This function allows an application to query the current state of each of
  1648. //! the fault trigger inputs to a given PWM generator. The current state of
  1649. //! each fault trigger input is returned unless PWMGenConfigure() has
  1650. //! previously been called with flag \b PWM_GEN_MODE_LATCH_FAULT in the
  1651. //! \e ulConfig parameter, in which case the returned status is the latched
  1652. //! fault trigger status.
  1653. //!
  1654. //! If latched faults are configured, the application must call
  1655. //! PWMGenFaultClear() to clear each trigger.
  1656. //!
  1657. //! \note This function is only available on devices supporting extended PWM
  1658. //! fault handling.
  1659. //!
  1660. //! \return Returns the current state of the fault triggers for the given PWM
  1661. //! generator. A set bit indicates that the associated trigger is active. For
  1662. //! \b PWM_FAULT_GROUP_0, the returned value is a logical OR of
  1663. //! \b PWM_FAULT_FAULT0, \b PWM_FAULT_FAULT1, \b PWM_FAULT_FAULT2, or
  1664. //! \b PWM_FAULT_FAULT3. For \b PWM_FAULT_GROUP_1, the return value is the
  1665. //! logical OR of \b PWM_FAULT_DCMP0, \b PWM_FAULT_DCMP1,
  1666. //! \b PWM_FAULT_DCMP2, \b PWM_FAULT_DCMP3, \b PWM_FAULT_DCMP4,
  1667. //! \b PWM_FAULT_DCMP5, \b PWM_FAULT_DCMP6, or \b PWM_FAULT_DCMP7.
  1668. //
  1669. //*****************************************************************************
  1670. unsigned long
  1671. PWMGenFaultStatus(unsigned long ulBase, unsigned long ulGen,
  1672. unsigned long ulGroup)
  1673. {
  1674. //
  1675. // Check for valid parameters.
  1676. //
  1677. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  1678. ASSERT(PWMGenValid(ulGen));
  1679. ASSERT((ulGroup == PWM_FAULT_GROUP_0) || (ulGroup == PWM_FAULT_GROUP_1));
  1680. //
  1681. // Return the current fault status.
  1682. //
  1683. if(ulGroup == PWM_FAULT_GROUP_0)
  1684. {
  1685. return(HWREG(PWM_GEN_EXT_BADDR(ulBase, ulGen) + PWM_O_X_FLTSTAT0));
  1686. }
  1687. else
  1688. {
  1689. return(HWREG(PWM_GEN_EXT_BADDR(ulBase, ulGen) + PWM_O_X_FLTSTAT1));
  1690. }
  1691. }
  1692. //*****************************************************************************
  1693. //
  1694. //! Clears one or more latched fault triggers for a given PWM generator.
  1695. //!
  1696. //! \param ulBase is the base address of the PWM module.
  1697. //! \param ulGen is the PWM generator for which fault trigger states are being
  1698. //! queried. This parameter must be one of \b PWM_GEN_0, \b PWM_GEN_1,
  1699. //! \b PWM_GEN_2, or \b PWM_GEN_3.
  1700. //! \param ulGroup indicates the subset of faults that are being queried. This
  1701. //! parameter must be \b PWM_FAULT_GROUP_0 or \b PWM_FAULT_GROUP_1.
  1702. //! \param ulFaultTriggers is the set of fault triggers which are to be
  1703. //! cleared.
  1704. //!
  1705. //! This function allows an application to clear the fault triggers for a given
  1706. //! PWM generator. This function is only required if PWMGenConfigure() has
  1707. //! previously been called with flag \b PWM_GEN_MODE_LATCH_FAULT in parameter
  1708. //! \e ulConfig.
  1709. //!
  1710. //! \note This function is only available on devices supporting extended PWM
  1711. //! fault handling.
  1712. //!
  1713. //! \return None.
  1714. //
  1715. //*****************************************************************************
  1716. void
  1717. PWMGenFaultClear(unsigned long ulBase, unsigned long ulGen,
  1718. unsigned long ulGroup, unsigned long ulFaultTriggers)
  1719. {
  1720. //
  1721. // Check for valid parameters.
  1722. //
  1723. ASSERT((ulBase == PWM0_BASE) || (ulBase == PWM1_BASE));
  1724. ASSERT(PWMGenValid(ulGen));
  1725. ASSERT((ulGroup == PWM_FAULT_GROUP_0) || (ulGroup == PWM_FAULT_GROUP_1));
  1726. ASSERT((ulGroup == PWM_FAULT_GROUP_0) &&
  1727. ((ulFaultTriggers & ~(PWM_FAULT_FAULT0 | PWM_FAULT_FAULT1 |
  1728. PWM_FAULT_FAULT2 | PWM_FAULT_FAULT3)) == 0));
  1729. ASSERT((ulGroup == PWM_FAULT_GROUP_1) &&
  1730. ((ulFaultTriggers & ~(PWM_FAULT_DCMP0 | PWM_FAULT_DCMP1 |
  1731. PWM_FAULT_DCMP2 | PWM_FAULT_DCMP3 |
  1732. PWM_FAULT_DCMP4 | PWM_FAULT_DCMP5 |
  1733. PWM_FAULT_DCMP6 | PWM_FAULT_DCMP7)) == 0));
  1734. //
  1735. // Clear the given faults.
  1736. //
  1737. if(ulGroup == PWM_FAULT_GROUP_0)
  1738. {
  1739. HWREG(PWM_GEN_EXT_BADDR(ulBase, ulGen) + PWM_O_X_FLTSTAT0) =
  1740. ulFaultTriggers;
  1741. }
  1742. else
  1743. {
  1744. HWREG(PWM_GEN_EXT_BADDR(ulBase, ulGen) + PWM_O_X_FLTSTAT1) =
  1745. ulFaultTriggers;
  1746. }
  1747. }
  1748. //*****************************************************************************
  1749. //
  1750. // Close the Doxygen group.
  1751. //! @}
  1752. //
  1753. //*****************************************************************************