tcc.h 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495
  1. /**
  2. * \file
  3. *
  4. * \brief SAM TCC - Timer Counter for Control Applications Driver
  5. *
  6. * Copyright (C) 2013-2016 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #ifndef TCC_H_INCLUDED
  47. #define TCC_H_INCLUDED
  48. /**
  49. * \defgroup asfdoc_sam0_tcc_group SAM Timer Counter for Control Applications (TCC) Driver
  50. *
  51. * This driver for Atmel&reg; | SMART ARM&reg;-based microcontrollers provides an interface for the configuration
  52. * and management of the TCC module within the device, for waveform
  53. * generation and timing operations. It also provides extended options for
  54. * control applications.
  55. *
  56. * The following driver API modes are covered
  57. * by this manual:
  58. *
  59. * - Polled APIs
  60. * \if TCC_CALLBACK_MODE
  61. * - Callback APIs
  62. * \endif
  63. *
  64. * The following peripheral is used by this module:
  65. * - TCC (Timer/Counter for Control Applications)
  66. *
  67. * The following devices can use this module:
  68. * - Atmel | SMART SAM D21
  69. * - Atmel | SMART SAM R21
  70. * - Atmel | SMART SAM D10/D11
  71. * - Atmel | SMART SAM L21/L22
  72. * - Atmel | SMART SAM DA1
  73. * - Atmel | SMART SAM C20/C21
  74. * - Atmel | SMART SAM HA1
  75. * - Atmel | SMART SAM R30
  76. *
  77. * The outline of this documentation is as follows:
  78. * - \ref asfdoc_sam0_tcc_prerequisites
  79. * - \ref asfdoc_sam0_tcc_module_overview
  80. * - \ref asfdoc_sam0_tcc_special_considerations
  81. * - \ref asfdoc_sam0_tcc_extra_info
  82. * - \ref asfdoc_sam0_tcc_examples
  83. * - \ref asfdoc_sam0_tcc_api_overview
  84. *
  85. * \section asfdoc_sam0_tcc_prerequisites Prerequisites
  86. *
  87. * There are no prerequisites for this module.
  88. *
  89. * \section asfdoc_sam0_tcc_module_overview Module Overview
  90. *
  91. * The Timer/Counter for Control Applications (TCC) module provides a set of
  92. * timing and counting related functionality, such as the generation of periodic
  93. * waveforms, the capturing of a periodic waveform's frequency/duty cycle,
  94. * software timekeeping for periodic operations, waveform extension control,
  95. * fault detection etc.
  96. *
  97. * The counter size of the TCC modules can be 16- or 24-bit depending on
  98. * the TCC instance.
  99. * Refer \ref asfdoc_sam0_tcc_special_considerations_tcc_d21 and
  100. * \ref asfdoc_sam0_tcc_special_considerations_tcc_d11 for details on TCC instances.
  101. *
  102. * The TCC module for the SAM includes the following functions:
  103. *
  104. * - Generation of PWM signals
  105. * - Generation of timestamps for events
  106. * - General time counting
  107. * - Waveform period capture
  108. * - Waveform frequency capture
  109. * - Additional control for generated waveform outputs
  110. * - Fault protection for waveform generation
  111. *
  112. * \ref asfdoc_sam0_tcc_block_diagram "The diagram below" shows the overview
  113. * of the TCC Module.
  114. *
  115. * \anchor asfdoc_sam0_tcc_block_diagram
  116. * \image html overview.svg "Overview of the TCC Module"
  117. *
  118. * \subsection asfdoc_sam0_tcc_module_overview_parts Functional Description
  119. * The TCC module consists of following sections:
  120. * - Base Counter
  121. * - Compare/Capture channels, with waveform generation
  122. * - Waveform extension control and fault detection
  123. * - Interface to the event system, DMAC, and the interrupt system
  124. *
  125. * The base counter can be configured to either count a prescaled generic
  126. * clock or events from the event system.(TCEx, with event action configured
  127. * to counting).
  128. * The counter value can be used by compare/capture channels which can be
  129. * set up either in compare mode or capture mode.
  130. *
  131. * In capture mode, the counter value is stored when a configurable event
  132. * occurs. This mode can be used to generate timestamps used in event capture,
  133. * or it can be used for the measurement of a periodic input signal's
  134. * frequency/duty cycle.
  135. *
  136. * In compare mode, the counter value is compared against one or more of the
  137. * configured channels' compare values. When the counter value coincides with a
  138. * compare value an action can be taken automatically by the module, such as
  139. * generating an output event or toggling a pin when used for frequency or PWM
  140. * signal generation.
  141. *
  142. * \note The connection of events between modules requires the use of the
  143. * \ref asfdoc_sam0_events_group "SAM Event System Driver (EVENTS)"
  144. * to route output event of one module to the the input event of another.
  145. * For more information on event routing, refer to the event driver
  146. * documentation.
  147. *
  148. * In compare mode, when output signal is generated, extended waveform controls
  149. * are available, to arrange the compare outputs into specific formats.
  150. * The Output matrix can change the channel output routing. Pattern generation
  151. * unit can overwrite the output signal line to specific state.
  152. * The Fault protection feature of the TCC supports recoverable and
  153. * non-recoverable faults.
  154. *
  155. * \subsection asfdoc_sam0_tcc_module_overview_tc Base Timer/Counter
  156. *
  157. * \subsubsection asfdoc_sam0_tcc_module_overview_tc_size Timer/Counter Size
  158. * Each TCC has a counter size of either 16- or 24-bits. The size of the
  159. * counter determines the maximum value it can count to before an overflow
  160. * occurs.
  161. * \ref asfdoc_sam0_tcc_count_size_vs_top "The table below" shows the
  162. * maximum values for each of the possible counter sizes.
  163. *
  164. * \anchor asfdoc_sam0_tcc_count_size_vs_top
  165. * <table>
  166. * <caption>Timer Counter Sizes and Their Maximum Count Values</caption>
  167. * <tr>
  168. * <th>Counter size</th>
  169. * <th>Max. (hexadecimal)</th>
  170. * <th>Max. (decimal)</th>
  171. * </tr>
  172. * <tr>
  173. * <td>16-bit</td>
  174. * <td>0xFFFF</td>
  175. * <td>65,535</td>
  176. * </tr>
  177. * <tr>
  178. * <td>24-bit</td>
  179. * <td>0xFFFFFF</td>
  180. * <td>16,777,215</td>
  181. * </tr>
  182. * </table>
  183. *
  184. * The period/top value of the counter can be set, to define counting period.
  185. * This will allow the counter to overflow when the counter value reaches the
  186. * period/top value.
  187. *
  188. * \subsubsection asfdoc_sam0_tcc_module_overview_tc_clk Timer/Counter Clock and Prescaler
  189. * TCC is clocked asynchronously to the system clock by a GCLK
  190. * (Generic Clock) channel. The GCLK channel can be connected to any of the GCLK
  191. * generators. The GCLK generators are configured to use one of the available
  192. * clock sources in the system such as internal oscillator, external crystals,
  193. * etc. See the \ref asfdoc_sam0_system_clock_group "Generic Clock driver" for
  194. * more information.
  195. *
  196. * Each TCC module in the SAM has its own individual clock prescaler, which
  197. * can be used to divide the input clock frequency used by the counter. This
  198. * prescaler only scales the clock used to provide clock pulses for the counter
  199. * to count, and does not affect the digital register interface portion of
  200. * the module, thus the timer registers will be synchronized to the raw GCLK
  201. * frequency input to the module.
  202. *
  203. * As a result of this, when selecting a GCLK frequency and timer prescaler
  204. * value, the user application should consider both the timer resolution
  205. * required and the synchronization frequency to avoid lengthy
  206. * synchronization times of the module if a very slow GCLK frequency is fed
  207. * into the TCC module. It is preferable to use a higher module GCLK frequency
  208. * as the input to the timer, and prescale this down as much as possible to
  209. * obtain a suitable counter frequency in latency-sensitive applications.
  210. *
  211. * \subsubsection asfdoc_sam0_tcc_module_overview_tc_ctrl Timer/Counter Control Inputs (Events)
  212. *
  213. * The TCC can take several actions on the occurrence of an input event.
  214. * The event actions are listed
  215. * in \ref asfdoc_sam0_tcc_module_event_act "events action settings".
  216. *
  217. * \anchor asfdoc_sam0_tcc_module_event_act
  218. * <table>
  219. * <caption>TCC Module Event Actions</caption>
  220. * <tr>
  221. * <th>Event action</th>
  222. * <th>Description</th>
  223. * <th>Applied event</th>
  224. * </tr>
  225. * <tr>
  226. * <td>TCC_EVENT_ACTION_OFF</td>
  227. * <td>No action on the event input</td>
  228. * <td>All</td>
  229. * </tr>
  230. * <tr>
  231. * <td>TCC_EVENT_ACTION_RETRIGGER</td>
  232. * <td>Re-trigger Counter on event</td>
  233. * <td>All</td>
  234. * </tr>
  235. * <tr>
  236. * <td>TCC_EVENT_ACTION_NON_RECOVERABLE_FAULT</td>
  237. * <td>Generate Non-Recoverable Fault on event</td>
  238. * <td>All</td>
  239. * </tr>
  240. * <tr>
  241. * <td>TCC_EVENT_ACTION_START</td>
  242. * <td>Counter start on event</td>
  243. * <td>EV0</td>
  244. * </tr>
  245. * <tr>
  246. * <td>TCC_EVENT_ACTION_DIR_CONTROL</td>
  247. * <td>Counter direction control</td>
  248. * <td>EV0</td>
  249. * </tr>
  250. * <tr>
  251. * <td>TCC_EVENT_ACTION_DECREMENT</td>
  252. * <td>Counter decrement on event</td>
  253. * <td>EV0</td>
  254. * </tr>
  255. * <tr>
  256. * <td>TCC_EVENT_ACTION_PERIOD_PULSE_WIDTH_CAPTURE</td>
  257. * <td>Capture pulse period and pulse width</td>
  258. * <td>EV0</td>
  259. * </tr>
  260. * <tr>
  261. * <td>TCC_EVENT_ACTION_PULSE_WIDTH_PERIOD_CAPTURE</td>
  262. * <td>Capture pulse width and pulse period</td>
  263. * <td>EV0</td>
  264. * </tr>
  265. * <tr>
  266. * <td>TCC_EVENT_ACTION_STOP</td>
  267. * <td>Counter stop on event</td>
  268. * <td>EV1</td>
  269. * </tr>
  270. * <tr>
  271. * <td>TCC_EVENT_ACTION_COUNT_EVENT</td>
  272. * <td>Counter count on event</td>
  273. * <td>EV1</td>
  274. * </tr>
  275. * <tr>
  276. * <td>TCC_EVENT_ACTION_INCREMENT</td>
  277. * <td>Counter increment on event</td>
  278. * <td>EV1</td>
  279. * </tr>
  280. * <tr>
  281. * <td>TCC_EVENT_ACTION_COUNT_DURING_ACTIVE</td>
  282. * <td>Counter count during active state of asynchronous event</td>
  283. * <td>EV1</td>
  284. * </tr>
  285. * </table>
  286. *
  287. * \subsubsection asfdoc_sam0_tcc_module_overview_tc_reload Timer/Counter Reloading
  288. *
  289. * The TCC also has a configurable reload action, used when a
  290. * re-trigger event occurs. Examples of a re-trigger event could be the counter
  291. * reaching the maximum value when counting up, or when an event from the event
  292. * system makes the counter to re-trigger. The reload action determines if the
  293. * prescaler should be reset, and on which clock. The counter will
  294. * always be reloaded with the value it is set to start counting. The user
  295. * can choose between three different reload actions, described in
  296. * \ref asfdoc_sam0_tcc_module_reload_act "the table below".
  297. *
  298. * \anchor asfdoc_sam0_tcc_module_reload_act
  299. * <table>
  300. * <caption>TCC Module Reload Actions</caption>
  301. * <tr>
  302. * <th>Reload action</th>
  303. * <th>Description</th>
  304. * </tr>
  305. * <tr>
  306. * <td>TCC_RELOAD_ACTION_GCLK</td>
  307. * <td>Reload TCC counter value on next GCLK cycle. Leave prescaler
  308. * as-is.</td>
  309. * </tr>
  310. * <tr>
  311. * <td>TCC_RELOAD_ACTION_PRESC</td>
  312. * <td>Reloads TCC counter value on next prescaler clock. Leave prescaler
  313. * as-is.</td>
  314. * </tr>
  315. * <tr>
  316. * <td>TCC_RELOAD_ACTION_RESYNC</td>
  317. * <td>Reload TCC counter value on next GCLK cycle. Clear prescaler to
  318. * zero.</td>
  319. * </tr>
  320. * </table>
  321. *
  322. * The reload action to use will depend on the specific application being
  323. * implemented. One example is when an external trigger for a reload occurs; if
  324. * the TCC uses the prescaler, the counter in the prescaler should not have a
  325. * value between zero and the division factor. The counter in the TCC module
  326. * and the counter in the prescaler should both start at zero.
  327. * If the counter is set to re-trigger when it reaches the maximum value,
  328. * this is not the right option to use. In such a case it would be better if
  329. * the prescaler is left unaltered when the re-trigger happens, letting the
  330. * counter reset on the next GCLK cycle.
  331. *
  332. * \subsubsection asfdoc_sam0_tcc_module_overview_tc_oneshot One-shot Mode
  333. *
  334. * The TCC module can be configured in one-shot mode. When configured in this
  335. * manner, starting the timer will cause it to count until the next overflow
  336. * or underflow condition before automatically halting, waiting to be manually
  337. * triggered by the user application software or an event from the event
  338. * system.
  339. *
  340. * \subsection asfdoc_sam0_tcc_module_overview_capt Capture Operations
  341. *
  342. * In capture operations, any event from the event system or a pin change can
  343. * trigger a capture of the counter value. This captured counter value can be
  344. * used as timestamps for the events, or it can be used in frequency and pulse
  345. * width capture.
  346. *
  347. * \subsubsection asfdoc_sam0_tcc_module_overview_capt_ev Capture Operations - Event
  348. *
  349. * Event capture is a simple use of the capture functionality,
  350. * designed to create timestamps for specific events. When the input event
  351. * appears, the current counter value is copied into the corresponding
  352. * compare/capture register, which can then be read by the user application.
  353. *
  354. * Note that when performing any capture operation, there is a risk that the
  355. * counter reaches its top value (MAX) when counting up, or the bottom value
  356. * (zero) when counting down, before the capture event occurs. This can distort
  357. * the result, making event timestamps to appear shorter than they really are.
  358. * In this case, the user application should check for timer overflow when
  359. * reading a capture result in order to detect this situation and perform an
  360. * appropriate adjustment.
  361. *
  362. * Before checking for a new capture, \ref TCC_STATUS_COUNT_OVERFLOW
  363. * should be checked. The response to an overflow error is left to the user
  364. * application, however, it may be necessary to clear both the overflow
  365. * flag and the capture flag upon each capture reading.
  366. *
  367. * \subsubsection asfdoc_sam0_tcc_module_overview_capt_pulse Capture Operations - Pulse Width
  368. *
  369. * Pulse Width Capture mode makes it possible to measure the pulse width and
  370. * period of PWM signals. This mode uses two capture channels of the counter.
  371. * There are two modes for pulse width capture;
  372. * Pulse Width Period (PWP) and Period Pulse Width (PPW). In PWP mode, capture
  373. * channel 0 is used for storing the pulse width and capture channel 1 stores
  374. * the observed period. While in PPW mode, the roles of the two capture channels
  375. * are reversed.
  376. *
  377. * As in the above example it is necessary to poll on interrupt flags to see
  378. * if a new capture has happened and check that a capture overflow error has
  379. * not occurred.
  380. *
  381. * Refer to \ref asfdoc_sam0_tcc_module_overview_tc_ctrl to set up the input
  382. * event to perform pulse width capture.
  383. *
  384. * \subsection asfdoc_sam0_tcc_module_overview_mc Compare Match Operation
  385. *
  386. * In compare match operation, Compare/Capture registers are compared
  387. * with the counter value. When the timer's count value matches the value of a
  388. * compare channel, a user defined action can be taken.
  389. *
  390. * \subsubsection asfdoc_sam0_tcc_module_overview_mc_timer Basic Timer
  391. *
  392. * A Basic Timer is a simple application where compare match operation is used
  393. * to determine when a specific period has elapsed. In Basic Timer operations,
  394. * one or more values in the module's Compare/Capture registers are used to
  395. * specify the time (in terms of the number of prescaled GCLK cycles, or
  396. * input events) at which
  397. * an action should be taken by the microcontroller. This can be an Interrupt
  398. * Service Routine (ISR), event generation via the event system, or a software
  399. * flag that is polled from the user application.
  400. *
  401. * \subsubsection asfdoc_sam0_tcc_module_overview_mc_wave Waveform Generation
  402. *
  403. * Waveform generation enables the TCC module to generate square waves, or, if
  404. * combined with an external passive low-pass filter, analog waveforms.
  405. *
  406. * \subsubsection asfdoc_sam0_tcc_module_overview_mc_wave_pwm Waveform Generation - PWM
  407. *
  408. * Pulse width modulation is a form of waveform generation and a signalling
  409. * technique that can be useful in many applications. When PWM mode is used,
  410. * a digital pulse train with a configurable frequency and duty cycle can be
  411. * generated by the TCC module and output to a GPIO pin of the device.
  412. *
  413. * Often PWM is used to communicate a control or information parameter to an
  414. * external circuit or component. Differing impedances of the source generator
  415. * and sink receiver circuits is less of an issue when using PWM compared to
  416. * using an analog voltage value, as noise will not generally affect the
  417. * signal's integrity to a meaningful extent.
  418. *
  419. * \ref asfdoc_sam0_tcc_module_pwm_single_diag "The figure below" illustrates
  420. * operations and different states of the counter and its output when using
  421. * the timer in Normal PWM mode (Single Slope). As can be seen, the TOP/PERIOD
  422. * value is
  423. * unchanged and is set to MAX. The compare match value is changed at several
  424. * points to illustrate the resulting waveform output changes. The PWM output is
  425. * set to normal (i.e. non-inverted) output mode.
  426. *
  427. * \anchor asfdoc_sam0_tcc_module_pwm_single_diag
  428. * \image html pwm_single_ex.svg "Example Of PWM In Single-Slope Mode, and Different Counter Operations"
  429. *
  430. * Several PWM modes are supported by the TCC module, refer to
  431. * datasheet for the details on PWM waveform generation.
  432. *
  433. * \subsubsection asfdoc_sam0_tcc_module_overview_mc_wave_freq Waveform Generation - Frequency
  434. *
  435. * Normal Frequency Generation is in many ways identical to PWM generation.
  436. * However, only in Frequency Generation, a toggle occurs on the output when a
  437. * match on a compare channels occurs.
  438. *
  439. * When the Match Frequency Generation is used, the timer value is reset on
  440. * match condition, resulting in a variable frequency square wave with a
  441. * fixed 50% duty cycle.
  442. *
  443. * \subsection asfdoc_sam0_tcc_module_overview_ext Waveform Extended Controls
  444. *
  445. * \subsubsection asfdoc_sam0_tcc_module_overview_ext_pat Pattern Generation
  446. *
  447. * Pattern insertion allows the TCC module to change the actual pin output level
  448. * without modifying the compare/match settings.
  449. *
  450. * \anchor asfdoc_sam0_tcc_module_pattern_gen
  451. * <table>
  452. * <caption>TCC Module Output Pattern Generation</caption>
  453. * <tr>
  454. * <th>Pattern</th>
  455. * <th>Description</th>
  456. * </tr>
  457. * <tr>
  458. * <td>TCC_OUTPUT_PATTERN_DISABLE</td>
  459. * <td>Pattern disabled, generate output as is</td>
  460. * </tr>
  461. * <tr>
  462. * <td>TCC_OUTPUT_PATTERN_0</td>
  463. * <td>Generate pattern 0 on output (keep the output LOW)</td>
  464. * </tr>
  465. * <tr>
  466. * <td>TCC_OUTPUT_PATTERN_1</td>
  467. * <td>Generate pattern 1 on output (keep the output HIGH)</td>
  468. * </tr>
  469. * </table>
  470. *
  471. * \subsubsection asfdoc_sam0_tcc_module_overview_ext_r_fault Recoverable Faults
  472. *
  473. * The recoverable faults can trigger one or several of following fault actions:
  474. * -# *Halt* action: The recoverable faults can halt the TCC timer/counter,
  475. * so that the final output wave is kept at a defined state. When the fault
  476. * state is removed it is possible to recover the counter and waveform
  477. * generation. The halt action is defined as:
  478. * \anchor asfdoc_sam0_tcc_module_fault_halt_action
  479. * <table>
  480. * <caption>TCC Module Recoverable Fault Halt Actions</caption>
  481. * <tr>
  482. * <th>Action</th>
  483. * <th>Description</th>
  484. * </tr>
  485. * <tr>
  486. * <td>TCC_FAULT_HALT_ACTION_DISABLE</td>
  487. * <td>Halt action is disabled</td>
  488. * </tr>
  489. * <tr>
  490. * <td>TCC_FAULT_HALT_ACTION_HW_HALT</td>
  491. * <td>The timer/counter is halted as long as the corresponding fault is
  492. * present</td>
  493. * </tr>
  494. * <tr>
  495. * <td>TCC_FAULT_HALT_ACTION_SW_HALT</td>
  496. * <td>The timer/counter is halted until the corresponding fault is removed
  497. * and fault state cleared by software</td>
  498. * </tr>
  499. * <tr>
  500. * <td>TCC_FAULT_HALT_ACTION_NON_RECOVERABLE</td>
  501. * <td>Force all the TCC output pins to a pre-defined level, as what
  502. * Non-Recoverable Fault do</td>
  503. * </tr>
  504. * </table>
  505. * -# *Restart* action: When enabled, the recoverable faults can restart the TCC
  506. * timer/counter.
  507. * -# *Keep* action: When enabled, the recoverable faults can keep the
  508. * corresponding channel output to zero when the fault condition is present.
  509. * -# *Capture* action: When the recoverable fault occurs, the capture action can
  510. * time stamps the corresponding fault. The following capture mode is
  511. * supported:
  512. * \anchor asfdoc_sam0_tcc_module_fault_capt_action
  513. * <table>
  514. * <caption>TCC Module Recoverable Fault Capture Actions</caption>
  515. * <tr>
  516. * <th>Action</th>
  517. * <th>Description</th>
  518. * </tr>
  519. * <tr>
  520. * <td>TCC_FAULT_CAPTURE_DISABLE</td>
  521. * <td>Capture action is disabled</td>
  522. * </tr>
  523. * <tr>
  524. * <td>TCC_FAULT_CAPTURE_EACH</td>
  525. * <td>Equivalent to standard capture operation, on each fault occurrence
  526. * the time stamp is captured</td>
  527. * </tr>
  528. * <tr>
  529. * <td>TCC_FAULT_CAPTURE_MINIMUM</td>
  530. * <td>Get the minimum time stamped value in all time stamps</td>
  531. * </tr>
  532. * <tr>
  533. * <td>TCC_FAULT_CAPTURE_MAXIMUM</td>
  534. * <td>Get the maximum time stamped value in all time stamps</td>
  535. * </tr>
  536. * <tr>
  537. * <td>TCC_FAULT_CAPTURE_SMALLER</td>
  538. * <td>Time stamp the fault input if the value is smaller than last one</td>
  539. * </tr>
  540. * <tr>
  541. * <td>TCC_FAULT_CAPTURE_BIGGER</td>
  542. * <td>Time stamp the fault input if the value is bigger than last one</td>
  543. * </tr>
  544. * <tr>
  545. * <td>TCC_FAULT_CAPTURE_CHANGE</td>
  546. * <td>Time stamp the fault input if the time stamps changes its increment
  547. * direction</td>
  548. * </tr>
  549. * </table>
  550. *
  551. * In TCC module, only the first two compare channels (CC0 and CC1) can work
  552. * with recoverable fault inputs. The corresponding event inputs (TCCx MC0
  553. * and TCCx MC1) are then used as fault inputs respectively.
  554. * The faults are called Fault A and Fault B.
  555. *
  556. * The recoverable fault can be filtered or effected by corresponding channel
  557. * output. On fault condition there are many other settings that can be chosen.
  558. * Refer to data sheet for more details about the recoverable fault
  559. * operations.
  560. *
  561. * \subsubsection asfdoc_sam0_tcc_module_overview_ext_n_fault Non-Recoverable Faults
  562. *
  563. * The non-recoverable faults force all the TCC output pins to a pre-defined
  564. * level (can be forced to 0 or 1). The input control signal of non-recoverable
  565. * fault is from timer/counter event (TCCx EV0 and TCCx EV1).
  566. * To enable non-recoverable fault,
  567. * corresponding TCEx event action must be set to non-recoverable fault action
  568. * (\ref TCC_EVENT_ACTION_NON_RECOVERABLE_FAULT).
  569. * Refer to \ref asfdoc_sam0_tcc_module_overview_tc_ctrl to see the available
  570. * event input action.
  571. *
  572. * \subsection asfdoc_sam0_tcc_module_overview_buffering Double and Circular Buffering
  573. *
  574. * The pattern, period, and the compare channels registers are double buffered.
  575. * For these options there are effective registers (PATT, PER, and CCx) and
  576. * buffer registers (PATTB, PERB, and CCx). When writing to the buffer
  577. * registers, the values are buffered and will be committed to effective
  578. * registers on UPDATE condition.
  579. *
  580. * Usually the buffered value is cleared after it is committed, but there is also
  581. * an option to circular the register buffers. The period (PER) and four lowest
  582. * compare channels register (CCx, x is 0 ~ 3) support this function. When
  583. * circular buffer is used, on UPDATE the previous period or compare values are
  584. * copied back into the corresponding period buffer and compare buffers.
  585. * This way, the register value and its buffer register value is actually
  586. * switched on UPDATE condition, and will be switched back on next UPDATE
  587. * condition.
  588. *
  589. * For input capture, the buffer register (CCBx) and the corresponding capture
  590. * channel register (CCx) act like a FIFO. When regular register (CCx) is empty
  591. * or read, any content in the buffer register is passed to regular one.
  592. *
  593. * In TCC module driver, when the double buffering write is enabled, any
  594. * write through \ref tcc_set_top_value(), \ref tcc_set_compare_value(), and
  595. * \ref tcc_set_pattern() will be done to the corresponding buffer register.
  596. * Then the value in the buffer register will be transferred to the regular
  597. * register on the next UPDATE condition or by a force UPDATE using
  598. * \ref tcc_force_double_buffer_update().
  599. *
  600. * \subsection asfdoc_sam0_tcc_module_overview_sleep Sleep Mode
  601. *
  602. * TCC modules can be configured to operate in any sleep mode, with its "run
  603. * in standby" function enabled. It can wake up the device using interrupts or
  604. * perform internal actions with the help of the Event System.
  605. *
  606. * \section asfdoc_sam0_tcc_special_considerations Special Considerations
  607. *
  608. * \subsection asfdoc_sam0_tcc_special_considerations_specific_features Driver Feature Macro Definition
  609. * \ref asfdoc_sam0_tcc_feature_table "The table below" shows some specific features
  610. * of the TCC Module.
  611. *
  612. * \anchor asfdoc_sam0_tcc_feature_table
  613. * <table>
  614. * <caption>TCC Module Specific Features</caption>
  615. * <tr>
  616. * <th>Driver Feature Macro</th>
  617. * <th>Supported devices</th>
  618. * </tr>
  619. * <tr>
  620. * <td>FEATURE_TCC_GENERATE_DMA_TRIGGER</td>
  621. * <td>SAM L21/L22/R30</td>
  622. * </tr>
  623. * </table>
  624. *
  625. * \note The specific features are only available in the driver when the
  626. * selected device supports those features.
  627. *
  628. * \subsection asfdoc_sam0_tcc_special_considerations_tcc_feature Module Features
  629. *
  630. * The features of TCC, such as timer/counter size, number of compare capture
  631. * channels, and number of outputs, are dependent on the TCC module instance being
  632. * used.
  633. *
  634. * \subsubsection asfdoc_sam0_tcc_special_considerations_tcc_d21 SAM TCC Feature List
  635. * For SAM D21/R21/L21/L22/DA1/C21/R30, the TCC features are:
  636. * \anchor asfdoc_sam0_tcc_features_d21
  637. * <table>
  638. * <caption>TCC module features for SAM D21/R21/L21/L22/DA1/C21/R30</caption>
  639. * <tr>
  640. * <th>TCC#</th>
  641. * <th>Match/Capture channels</th>
  642. * <th>Wave outputs</th>
  643. * <th>Counter size [bits]</th>
  644. * <th>Fault</th>
  645. * <th>Dithering</th>
  646. * <th>Output matrix</th>
  647. * <th>Dead-Time insertion</th>
  648. * <th>SWAP</th>
  649. * <th>Pattern</th>
  650. * </tr>
  651. * <tr>
  652. * <td>0</td>
  653. * <td>4</td>
  654. * <td>8</td>
  655. * <td>24</td>
  656. * <td>Y</td>
  657. * <td>Y</td>
  658. * <td>Y</td>
  659. * <td>Y</td>
  660. * <td>Y</td>
  661. * <td>Y</td>
  662. * </tr>
  663. * <tr>
  664. * <td>1</td>
  665. * <td>2</td>
  666. * <td>4</td>
  667. * <td>24</td>
  668. * <td>Y</td>
  669. * <td>Y</td>
  670. * <td></td>
  671. * <td></td>
  672. * <td></td>
  673. * <td>Y</td>
  674. * </tr>
  675. * <tr>
  676. * <td>2</td>
  677. * <td>2</td>
  678. * <td>2</td>
  679. * <td>16</td>
  680. * <td>Y</td>
  681. * <td></td>
  682. * <td></td>
  683. * <td></td>
  684. * <td></td>
  685. * <td></td>
  686. * </tr>
  687. * </table>
  688. *
  689. * \subsubsection asfdoc_sam0_tcc_special_considerations_tcc_d11 SAM D10/D11 TCC Feature List
  690. * For SAM D10/D11, the TCC features are:
  691. * \anchor asfdoc_sam0_tcc_features_d11
  692. * <table>
  693. * <caption>TCC Module Features For SAM D10/D11</caption>
  694. * <tr>
  695. * <th>TCC#</th>
  696. * <th>Match/Capture channels</th>
  697. * <th>Wave outputs</th>
  698. * <th>Counter size [bits]</th>
  699. * <th>Fault</th>
  700. * <th>Dithering</th>
  701. * <th>Output matrix</th>
  702. * <th>Dead-Time insertion</th>
  703. * <th>SWAP</th>
  704. * <th>Pattern</th>
  705. * </tr>
  706. * <tr>
  707. * <td>0</td>
  708. * <td>4</td>
  709. * <td>8</td>
  710. * <td>24</td>
  711. * <td>Y</td>
  712. * <td>Y</td>
  713. * <td>Y</td>
  714. * <td>Y</td>
  715. * <td>Y</td>
  716. * <td>Y</td>
  717. * </tr>
  718. * </table>
  719. *
  720. * \subsection asfdoc_sam0_tcc_special_considerations_tcc_pin Channels vs. Pinouts
  721. *
  722. * As the TCC module may have more waveform output pins than the number of
  723. * compare/capture channels, the free pins (with number higher than number of
  724. * channels) will reuse the waveform generated by channels subsequently. E.g.,
  725. * if the number of channels is four and the number of wave output pins is eight, channel
  726. * 0 output will be available on out pin 0 and 4, channel 1 output
  727. * on wave out pin 1 and 5, and so on.
  728. *
  729. * \section asfdoc_sam0_tcc_extra_info Extra Information
  730. *
  731. * For extra information, see \ref asfdoc_sam0_tcc_extra. This includes:
  732. * - \ref asfdoc_sam0_tcc_extra_acronyms
  733. * - \ref asfdoc_sam0_tcc_extra_dependencies
  734. * - \ref asfdoc_sam0_tcc_extra_errata
  735. * - \ref asfdoc_sam0_tcc_extra_history
  736. *
  737. *
  738. * \section asfdoc_sam0_tcc_examples Examples
  739. *
  740. * For a list of examples related to this driver, see
  741. * \ref asfdoc_sam0_tcc_exqsg.
  742. *
  743. * \section asfdoc_sam0_tcc_api_overview API Overview
  744. * @{
  745. */
  746. #include <compiler.h>
  747. #include <clock.h>
  748. #include <gclk.h>
  749. #include <pinmux.h>
  750. /** Maximum number of channels supported by the driver
  751. * (Channel index from 0 to \c TCC_NUM_CHANNELS - 1).
  752. */
  753. #define TCC_NUM_CHANNELS 4
  754. /** Maximum number of wave outputs lines supported by the driver
  755. * (Output line index from 0 to \c TCC_NUM_WAVE_OUTPUTS - 1).
  756. */
  757. #define TCC_NUM_WAVE_OUTPUTS 8
  758. /** Maximum number of (recoverable) faults supported by the driver. */
  759. #define TCC_NUM_FAULTS 2
  760. #if TCC_ASYNC == true
  761. # include <system_interrupt.h>
  762. #endif
  763. /**
  764. * \name Driver Feature Definition
  765. * Define port features set according to different device family.
  766. * @{
  767. */
  768. #if (SAML21) || (SAML22) || (SAMR30) || defined(__DOXYGEN__)
  769. /** Generate DMA triggers */
  770. # define FEATURE_TCC_GENERATE_DMA_TRIGGER
  771. #endif
  772. /*@}*/
  773. #ifdef __cplusplus
  774. extern "C" {
  775. #endif
  776. /** Generates a table enum list entry for a given type
  777. and index (e.g. "TCC_CALLBACK_MC_CHANNEL_0,"). */
  778. #define _TCC_ENUM(n, type) TCC_##type##_##n,
  779. /** Generates table enum list entries for all channels of a
  780. given type and channel number on TCC module. */
  781. #define _TCC_CHANNEL_ENUM_LIST(type) \
  782. MREPEAT(TCC_NUM_CHANNELS, _TCC_ENUM, type##_CHANNEL)
  783. /** Generates table enum list entries for all output of a
  784. given type and waveform output number on TCC module. */
  785. #define _TCC_WO_ENUM_LIST(type) \
  786. MREPEAT(TCC_NUM_WAVE_OUTPUTS, _TCC_ENUM, type)
  787. #if TCC_ASYNC == true
  788. /** Enum for the possible callback types for the TCC module. */
  789. enum tcc_callback {
  790. /** Callback for TCC overflow */
  791. TCC_CALLBACK_OVERFLOW,
  792. /** Callback for TCC Retrigger */
  793. TCC_CALLBACK_RETRIGGER,
  794. /** Callback for TCC counter event */
  795. TCC_CALLBACK_COUNTER_EVENT,
  796. /** Callback for capture overflow error */
  797. TCC_CALLBACK_ERROR,
  798. /** Callback for Recoverable Fault A */
  799. TCC_CALLBACK_FAULTA,
  800. /** Callback for Recoverable Fault B */
  801. TCC_CALLBACK_FAULTB,
  802. /** Callback for Non-Recoverable Fault 0 */
  803. TCC_CALLBACK_FAULT0,
  804. /** Callback for Non-Recoverable Fault 1 */
  805. TCC_CALLBACK_FAULT1,
  806. # if defined(__DOXYGEN__)
  807. /** Channel callback type table for TCC
  808. *
  809. * Each TCC module may contain several callback types for channels; each
  810. * channel will have its own callback type in the table, with the channel
  811. * index number substituted for "n" in the channel callback type
  812. * (e.g. \c TCC_MATCH_CAPTURE_CHANNEL_0).
  813. */
  814. TCC_CALLBACK_CHANNEL_n = n,
  815. # else
  816. /** Callbacks for Match/Capture channels, e.g., TCC_CALLBACK_CHANNEL_0 */
  817. _TCC_CHANNEL_ENUM_LIST(CALLBACK)
  818. # endif
  819. # if !defined(__DOXYGEN__)
  820. /** Number of available callbacks */
  821. TCC_CALLBACK_N
  822. # endif
  823. };
  824. #endif /* #if TCC_ASYNC == true */
  825. /**
  826. * \name Module Status Flags
  827. *
  828. * TCC status flags, returned by \ref tcc_get_status() and cleared by
  829. * \ref tcc_clear_status().
  830. *
  831. * @{
  832. */
  833. /** Timer channel \c ch (0 ~ 3) has matched against its compare value,
  834. * or has captured a new value.
  835. */
  836. #define TCC_STATUS_CHANNEL_MATCH_CAPTURE(ch) (1UL << (ch))
  837. /** Timer channel \c ch (0 ~ 3) match/compare output state. */
  838. #define TCC_STATUS_CHANNEL_OUTPUT(ch) (1UL << ((ch)+8))
  839. /** A Non-Recoverable Fault \c x (0 ~ 1) has occurred. */
  840. #define TCC_STATUS_NON_RECOVERABLE_FAULT_OCCUR(x) (1UL << ((x)+16))
  841. /** A Recoverable Fault \c n (0 ~ 1 representing A ~ B) has occured. */
  842. #define TCC_STATUS_RECOVERABLE_FAULT_OCCUR(n) (1UL << ((n)+18))
  843. /** The Non-Recoverable Fault \c x (0 ~ 1) input is present. */
  844. #define TCC_STATUS_NON_RECOVERABLE_FAULT_PRESENT(x) (1UL << ((x)+20))
  845. /** A Recoverable Fault \c n (0 ~ 1 representing A ~ B) is present. */
  846. #define TCC_STATUS_RECOVERABLE_FAULT_PRESENT(n) (1UL << ((n)+22))
  847. /** Timer registers synchronization has completed, and the synchronized count
  848. * value may be read.
  849. */
  850. #define TCC_STATUS_SYNC_READY (1UL << 23)
  851. /** A new value was captured before the previous value was read, resulting in
  852. * lost data.
  853. */
  854. #define TCC_STATUS_CAPTURE_OVERFLOW (1UL << 24)
  855. /** A counter event occurred. */
  856. #define TCC_STATUS_COUNTER_EVENT (1UL << 25)
  857. /** A counter retrigger occurred. */
  858. #define TCC_STATUS_COUNTER_RETRIGGERED (1UL << 26)
  859. /** The timer count value has overflowed from its maximum value to its minimum
  860. * when counting upward, or from its minimum value to its maximum when
  861. * counting downward.
  862. */
  863. #define TCC_STATUS_COUNT_OVERFLOW (1UL << 27)
  864. /** Ramp period cycle index.
  865. * In ramp operation, each two period cycles are marked as cycle A and B,
  866. * the index 0 represents cycle A and 1 represents cycle B. */
  867. #define TCC_STATUS_RAMP_CYCLE_INDEX (1UL << 28)
  868. /** The counter has been stopped (due to disable, stop command, or one-shot). */
  869. #define TCC_STATUS_STOPPED (1UL << 29)
  870. /** @} */
  871. /**
  872. * \brief Index of the match capture channels
  873. *
  874. * This enum is used to specify which capture/match channel to do
  875. * operations on.
  876. */
  877. enum tcc_match_capture_channel {
  878. # if defined(__DOXYGEN__)
  879. /** Match capture channel index table for TCC
  880. *
  881. * Each TCC module may contain several match capture channels; each channel
  882. * will have its own index in the table, with the index number substituted
  883. * for "n" in the index name (e.g. \c TCC_MATCH_CAPTURE_CHANNEL_0).
  884. */
  885. TCC_MATCH_CAPTURE_CHANNEL_n = n,
  886. # else
  887. /** Indexes of match capture channels, e.g., TCC_MATCH_CAPTURE_CHANNEL_0 */
  888. _TCC_CHANNEL_ENUM_LIST(MATCH_CAPTURE)
  889. # endif
  890. # if !defined(__DOXYGEN__)
  891. /** Number of supported channels */
  892. TCC_MATCH_CAPTURE_CHANNEL_N
  893. # endif
  894. };
  895. /**
  896. * \brief Index of the wave outputs
  897. *
  898. * This enum is used to specify which wave output to do
  899. * operations on.
  900. */
  901. enum tcc_wave_output {
  902. # if defined(__DOXYGEN__)
  903. /** Waveform output index table for TCC
  904. *
  905. * Each TCC module may contain several wave outputs; each output
  906. * will have its own index in the table, with the index number substituted
  907. * for "n" in the index name (e.g. \c TCC_WAVE_OUTPUT_0).
  908. */
  909. TCC_WAVE_OUTPUT_n = n,
  910. # else
  911. /** Indexes of match capture channels, e.g., TCC_WAVEFORM_OUTPUT_0 */
  912. _TCC_WO_ENUM_LIST(WAVE_OUTPUT)
  913. # endif
  914. # if !defined(__DOXYGEN__)
  915. /** Number of supported channels */
  916. TCC_WAVE_OUTPUT_N
  917. # endif
  918. };
  919. /**
  920. * \brief TCC wave generation mode enum
  921. *
  922. * This enum is used to specify the waveform generation mode.
  923. *
  924. */
  925. enum tcc_wave_generation {
  926. /** Normal Frequency: Top is the PER register, output toggled on each
  927. * compare match */
  928. TCC_WAVE_GENERATION_NORMAL_FREQ = 0,
  929. /** Match Frequency: Top is CC0 register, output toggles on each update
  930. * condition */
  931. TCC_WAVE_GENERATION_MATCH_FREQ = 1,
  932. /** Single-Slope PWM: Top is the PER register, CCx controls duty cycle
  933. * (output active when count is greater than CCx) */
  934. TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM = 2,
  935. /** Double-slope (count up and down), non centre-aligned: Top is the PER
  936. * register, CC[x] controls duty cycle while counting up and CC[x+N/2]
  937. * controls it while counting down */
  938. TCC_WAVE_GENERATION_DOUBLE_SLOPE_CRITICAL = 4,
  939. /** Double-slope (count up and down), interrupt/event at Bottom (Top is the
  940. * PER register, output active when count is greater than CCx) */
  941. TCC_WAVE_GENERATION_DOUBLE_SLOPE_BOTTOM = 5,
  942. /** Double-slope (count up and down), interrupt/event at Bottom and Top: (Top is the
  943. * PER register, output active when count is lower than CCx) */
  944. TCC_WAVE_GENERATION_DOUBLE_SLOPE_BOTH = 6,
  945. /** Double-slope (count up and down), interrupt/event at Top (Top is the
  946. * PER register, output active when count is greater than CCx) */
  947. TCC_WAVE_GENERATION_DOUBLE_SLOPE_TOP = 7,
  948. };
  949. /**
  950. * \brief Polarity of TCC wave generation on channels
  951. *
  952. * Specifies whether the wave output needs to be inverted or not.
  953. */
  954. enum tcc_wave_polarity {
  955. /** Wave output is not inverted */
  956. TCC_WAVE_POLARITY_0,
  957. /** Wave output is inverted */
  958. TCC_WAVE_POLARITY_1
  959. };
  960. /**
  961. * \brief TCC pattern generator for outputs
  962. *
  963. * Used when disabling output pattern or when selecting a specific pattern.
  964. */
  965. enum tcc_output_pattern {
  966. /** SWAP output pattern is not used */
  967. TCC_OUTPUT_PATTERN_DISABLE,
  968. /** Pattern 0 is applied to SWAP output */
  969. TCC_OUTPUT_PATTERN_0,
  970. /** Pattern 1 is applied to SWAP output */
  971. TCC_OUTPUT_PATTERN_1
  972. };
  973. /**
  974. * \brief Ramp Operations which are supported in single-slope PWM generation
  975. *
  976. * Ramp operations which are supported in single-slope PWM generation.
  977. */
  978. enum tcc_ramp {
  979. /** Default timer/counter PWM operation */
  980. TCC_RAMP_RAMP1 = 0,
  981. /** Uses a single channel (CC0) to control both CC0/CC1 compare outputs.
  982. * In cycle A, the channel 0 output is disabled, and
  983. * in cycle B, the channel 1 output is disabled. */
  984. TCC_RAMP_RAMP2A,
  985. /** Uses channels CC0 and CC1 to control compare outputs.
  986. * In cycle A, the channel 0 output is disabled, and
  987. * in cycle B, the channel 1 output is disabled.*/
  988. TCC_RAMP_RAMP2
  989. };
  990. /**
  991. * \brief Ramp Index for TCC wave generation
  992. *
  993. * In ramp operation, each two period cycles are marked as cycle A and B,
  994. * the index 0 represents cycle A and 1 represents cycle B.
  995. */
  996. enum tcc_ramp_index {
  997. /** Default, cycle index toggles. */
  998. TCC_RAMP_INDEX_DEFAULT,
  999. /** Force next cycle to be cycle B (set to 1) */
  1000. TCC_RAMP_INDEX_FORCE_B,
  1001. /** Force next cycle to be cycle A (clear to 0) */
  1002. TCC_RAMP_INDEX_FORCE_A,
  1003. /** Force next cycle keeping the same as current */
  1004. TCC_RAMP_INDEX_FORCE_KEEP
  1005. };
  1006. /**
  1007. * \brief TCC output inversion
  1008. *
  1009. * Used when enabling or disabling output inversion.
  1010. */
  1011. enum tcc_output_invertion {
  1012. /** Output inversion not to be enabled */
  1013. TCC_OUTPUT_INVERTION_DISABLE,
  1014. /** Invert the output from WO[x] */
  1015. TCC_OUTPUT_INVERTION_ENABLE
  1016. };
  1017. /**
  1018. * \brief TCC Counter reload action enum
  1019. *
  1020. * This enum specify how the counter is reloaded and whether the prescaler
  1021. * should be restarted.
  1022. */
  1023. enum tcc_reload_action {
  1024. /** The counter is reloaded/reset on the next GCLK and starts
  1025. * counting on the prescaler clock
  1026. */
  1027. TCC_RELOAD_ACTION_GCLK,
  1028. /** The counter is reloaded/reset on the next prescaler clock
  1029. */
  1030. TCC_RELOAD_ACTION_PRESC,
  1031. /** The counter is reloaded/reset on the next GCLK, and the
  1032. * prescaler is restarted as well
  1033. */
  1034. TCC_RELOAD_ACTION_RESYNC
  1035. };
  1036. /**
  1037. * \brief TCC clock prescaler values
  1038. *
  1039. * This enum is used to choose the clock prescaler
  1040. * configuration. The prescaler divides the clock frequency of the TCC
  1041. * module to operate TCC at a slower clock rate.
  1042. */
  1043. enum tcc_clock_prescaler {
  1044. /** Divide clock by 1 */
  1045. TCC_CLOCK_PRESCALER_DIV1,
  1046. /** Divide clock by 2 */
  1047. TCC_CLOCK_PRESCALER_DIV2,
  1048. /** Divide clock by 4 */
  1049. TCC_CLOCK_PRESCALER_DIV4,
  1050. /** Divide clock by 8 */
  1051. TCC_CLOCK_PRESCALER_DIV8,
  1052. /** Divide clock by 16 */
  1053. TCC_CLOCK_PRESCALER_DIV16,
  1054. /** Divide clock by 64 */
  1055. TCC_CLOCK_PRESCALER_DIV64,
  1056. /** Divide clock by 256 */
  1057. TCC_CLOCK_PRESCALER_DIV256,
  1058. /** Divide clock by 1024 */
  1059. TCC_CLOCK_PRESCALER_DIV1024
  1060. };
  1061. /**
  1062. * \brief TCC module count direction
  1063. *
  1064. * Used when selecting the Timer/Counter count direction.
  1065. */
  1066. enum tcc_count_direction {
  1067. /** Timer should count upward */
  1068. TCC_COUNT_DIRECTION_UP,
  1069. /** Timer should count downward */
  1070. TCC_COUNT_DIRECTION_DOWN,
  1071. };
  1072. #ifdef FEATURE_TCC_GENERATE_DMA_TRIGGER
  1073. /**
  1074. * \brief TCC module counter overflow DMA request mode
  1075. *
  1076. * Used when selecting the Timer/Counter overflow DMA request mode.
  1077. */
  1078. enum tcc_count_overflow_dma_trigger_mode {
  1079. /** TCC generates a DMA request on each cycle when an update condition
  1080. * is detected
  1081. */
  1082. TCC_COUNT_OVERFLOW_DMA_TRIGGER_MODE_CONTINUE,
  1083. /** When an update condition is detected, the TCC generates a DMA trigger
  1084. * on the cycle following the DMA One-Shot Command written to the Control
  1085. * B register
  1086. */
  1087. TCC_COUNT_OVERFLOW_DMA_TRIGGER_MODE_ONE_SHOT,
  1088. };
  1089. #endif
  1090. /**
  1091. * \brief Action to perform when the TCC module is triggered by events
  1092. *
  1093. * Event action to perform when the module is triggered by events.
  1094. */
  1095. enum tcc_event_action {
  1096. /** No event action */
  1097. TCC_EVENT_ACTION_OFF,
  1098. /** Stop counting, the counter will maintain its current value, waveforms
  1099. * are set to a defined Non-Recoverable State output
  1100. * (\ref tcc_non_recoverable_state_output). */
  1101. TCC_EVENT_ACTION_STOP,
  1102. /** Re-trigger counter on event, may generate an event if the re-trigger
  1103. * event output is enabled.
  1104. * \note When re-trigger event action is enabled, enabling the counter
  1105. * will not start until the next incoming event appears. */
  1106. TCC_EVENT_ACTION_RETRIGGER,
  1107. /** Start counter when previously stopped.
  1108. * Start counting on the event rising edge. Further events will not
  1109. * restart the counter;
  1110. * the counter keeps on counting using prescaled GCLK_TCCx, until it
  1111. * reaches TOP or Zero
  1112. * depending on the direction. */
  1113. TCC_EVENT_ACTION_START,
  1114. /** Count events; i.e. Increment or decrement depending on count
  1115. * direction. */
  1116. TCC_EVENT_ACTION_COUNT_EVENT,
  1117. /** The event source must be an asynchronous event, input value will
  1118. * overrides the direction settings (input low: counting up, input high:
  1119. * counting down). */
  1120. TCC_EVENT_ACTION_DIR_CONTROL,
  1121. /** Increment the counter on event, irrespective of count direction */
  1122. TCC_EVENT_ACTION_INCREMENT,
  1123. /** Decrement the counter on event, irrespective of count direction */
  1124. TCC_EVENT_ACTION_DECREMENT,
  1125. /** Count during active state of asynchronous event. In this case,
  1126. * depending on the count direction, the count will be incremented
  1127. * or decremented on each prescaled GCLK_TCCx, as long as the input
  1128. * event remains active. */
  1129. TCC_EVENT_ACTION_COUNT_DURING_ACTIVE,
  1130. /** Store period in capture register 0, pulse width in capture
  1131. * register 1
  1132. */
  1133. TCC_EVENT_ACTION_PERIOD_PULSE_WIDTH_CAPTURE,
  1134. /** Store pulse width in capture register 0, period in capture
  1135. * register 1
  1136. */
  1137. TCC_EVENT_ACTION_PULSE_WIDTH_PERIOD_CAPTURE,
  1138. /** Generate Non-Recoverable Fault on event */
  1139. TCC_EVENT_ACTION_NON_RECOVERABLE_FAULT,
  1140. };
  1141. /**
  1142. * \brief Action to be performed when the TCC module is triggered by event0
  1143. *
  1144. * Event action to perform when the module is triggered by event0.
  1145. */
  1146. enum tcc_event0_action {
  1147. /** No event action */
  1148. TCC_EVENT0_ACTION_OFF = TCC_EVENT_ACTION_OFF,
  1149. /** Re-trigger Counter on event */
  1150. TCC_EVENT0_ACTION_RETRIGGER = TCC_EVENT_ACTION_RETRIGGER,
  1151. /** Count events (increment or decrement, depending on count direction)
  1152. */
  1153. TCC_EVENT0_ACTION_COUNT_EVENT = TCC_EVENT_ACTION_COUNT_EVENT,
  1154. /** Start counter on event */
  1155. TCC_EVENT0_ACTION_START = TCC_EVENT_ACTION_START,
  1156. /** Increment counter on event */
  1157. TCC_EVENT0_ACTION_INCREMENT = TCC_EVENT_ACTION_INCREMENT,
  1158. /** Count during active state of asynchronous event */
  1159. TCC_EVENT0_ACTION_COUNT_DURING_ACTIVE = TCC_EVENT_ACTION_COUNT_DURING_ACTIVE,
  1160. /** Generate Non-Recoverable Fault on event */
  1161. TCC_EVENT0_ACTION_NON_RECOVERABLE_FAULT = TCC_EVENT_ACTION_NON_RECOVERABLE_FAULT
  1162. };
  1163. /**
  1164. * \brief Action to perform when the TCC module is triggered by event1
  1165. *
  1166. * Event action to perform when the module is triggered by event1.
  1167. */
  1168. enum tcc_event1_action {
  1169. /** No event action */
  1170. TCC_EVENT1_ACTION_OFF = TCC_EVENT_ACTION_OFF,
  1171. /** Re-trigger Counter on event */
  1172. TCC_EVENT1_ACTION_RETRIGGER = TCC_EVENT_ACTION_RETRIGGER,
  1173. /** The event source must be an asynchronous event, and the input value
  1174. * will override the direction settings.
  1175. * If TCEINVx is 0 and input event is LOW: counter will count up.
  1176. * If TCEINVx is 0 and input event is HIGH: counter will count down.
  1177. */
  1178. TCC_EVENT1_ACTION_DIR_CONTROL = TCC_EVENT_ACTION_DIR_CONTROL,
  1179. /** Stop counter on event */
  1180. TCC_EVENT1_ACTION_STOP = TCC_EVENT_ACTION_STOP,
  1181. /** Decrement on event */
  1182. TCC_EVENT1_ACTION_DECREMENT = TCC_EVENT_ACTION_DECREMENT,
  1183. /** Store period in capture register 0, pulse width in capture
  1184. * register 1
  1185. */
  1186. TCC_EVENT1_ACTION_PERIOD_PULSE_WIDTH_CAPTURE = TCC_EVENT_ACTION_PERIOD_PULSE_WIDTH_CAPTURE,
  1187. /** Store pulse width in capture register 0, period in capture
  1188. * register 1
  1189. */
  1190. TCC_EVENT1_ACTION_PULSE_WIDTH_PERIOD_CAPTURE = TCC_EVENT_ACTION_PULSE_WIDTH_PERIOD_CAPTURE,
  1191. /** Generate Non-Recoverable Fault on event */
  1192. TCC_EVENT1_ACTION_NON_RECOVERABLE_FAULT = TCC_EVENT_ACTION_NON_RECOVERABLE_FAULT
  1193. };
  1194. /**
  1195. * \brief On which part of the counter cycle the counter event output is generated
  1196. *
  1197. * This enum is used to define the point at which the counter event is generated.
  1198. */
  1199. enum tcc_event_generation_selection {
  1200. /** Counter Event is generated when a new counter cycle starts */
  1201. TCC_EVENT_GENERATION_SELECTION_START,
  1202. /** Counter Event is generated when a counter cycle ends */
  1203. TCC_EVENT_GENERATION_SELECTION_END,
  1204. /** Counter Event is generated when a counter cycle ends, except for the
  1205. * first and last cycles */
  1206. TCC_EVENT_GENERATION_SELECTION_BETWEEN,
  1207. /** Counter Event is generated when a new counter cycle starts or ends */
  1208. TCC_EVENT_GENERATION_SELECTION_BOUNDARY
  1209. };
  1210. /**
  1211. * \brief TCC channel operation modes
  1212. *
  1213. * To set a timer channel either in compare or in capture mode.
  1214. */
  1215. enum tcc_channel_function {
  1216. /** TCC channel performs compare operation */
  1217. TCC_CHANNEL_FUNCTION_COMPARE,
  1218. /** TCC channel performs capture operation */
  1219. TCC_CHANNEL_FUNCTION_CAPTURE
  1220. };
  1221. /**
  1222. * \brief TCC (recoverable) fault Halt action
  1223. */
  1224. enum tcc_fault_halt_action {
  1225. /** Halt action disabled. */
  1226. TCC_FAULT_HALT_ACTION_DISABLE,
  1227. /** Hardware halt action, counter is halted until restart */
  1228. TCC_FAULT_HALT_ACTION_HW_HALT,
  1229. /** Software halt action, counter is halted until fault bit cleared */
  1230. TCC_FAULT_HALT_ACTION_SW_HALT,
  1231. /** Non-Recoverable fault, force output to pre-defined level */
  1232. TCC_FAULT_HALT_ACTION_NON_RECOVERABLE
  1233. };
  1234. /**
  1235. * \brief TCC (recoverable) fault Capture action
  1236. */
  1237. enum tcc_fault_capture_action {
  1238. /** Capture disabled */
  1239. TCC_FAULT_CAPTURE_DISABLE,
  1240. /** Capture on Fault, each value is captured */
  1241. TCC_FAULT_CAPTURE_EACH,
  1242. /** Capture the minimum detection, but notify on smaller ones */
  1243. TCC_FAULT_CAPTURE_MINIMUM,
  1244. /** Capture the maximum detection, but notify on bigger ones */
  1245. TCC_FAULT_CAPTURE_MAXIMUM,
  1246. /** Capture if the value is smaller than last, notify event or interrupt
  1247. * if previous stamp is confirmed to be "local minimum" (not bigger than
  1248. * current stamp). */
  1249. TCC_FAULT_CAPTURE_SMALLER,
  1250. /** Capture if the value is bigger than last, notify event or interrupt
  1251. * if previous stamp is confirmed to be "local maximum" (not smaller than
  1252. * current stamp). */
  1253. TCC_FAULT_CAPTURE_BIGGER,
  1254. /** Capture if the time stamps changes its increment direction */
  1255. TCC_FAULT_CAPTURE_CHANGE
  1256. };
  1257. /**
  1258. * \brief Capture Channel triggered by TCC (recoverable) fault
  1259. */
  1260. enum tcc_fault_capture_channel {
  1261. /** Recoverable fault triggers channel 0 capture operation */
  1262. TCC_FAULT_CAPTURE_CHANNEL_0,
  1263. /** Recoverable fault triggers channel 1 capture operation */
  1264. TCC_FAULT_CAPTURE_CHANNEL_1,
  1265. /** Recoverable fault triggers channel 2 capture operation */
  1266. TCC_FAULT_CAPTURE_CHANNEL_2,
  1267. /** Recoverable fault triggers channel 3 capture operation */
  1268. TCC_FAULT_CAPTURE_CHANNEL_3
  1269. };
  1270. /**
  1271. * \brief TCC (recoverable) fault Input Source
  1272. */
  1273. enum tcc_fault_source {
  1274. /** Fault input is disabled */
  1275. TCC_FAULT_SOURCE_DISABLE,
  1276. /** Match Capture Event x (x=0,1) input */
  1277. TCC_FAULT_SOURCE_ENABLE,
  1278. /** Inverted MCEx (x=0,1) event input */
  1279. TCC_FAULT_SOURCE_INVERT,
  1280. /** Alternate fault (A or B) state at the end of the previous period */
  1281. TCC_FAULT_SOURCE_ALTFAULT
  1282. };
  1283. /**
  1284. * \brief TCC (recoverable) fault Input Blanking Start Point
  1285. */
  1286. enum tcc_fault_blanking {
  1287. /** No blanking */
  1288. TCC_FAULT_BLANKING_DISABLE,
  1289. /** Blanking applied from rising edge of the output waveform */
  1290. TCC_FAULT_BLANKING_RISING_EDGE,
  1291. /** Blanking applied from falling edge of the output waveform */
  1292. TCC_FAULT_BLANKING_FALLING_EDGE,
  1293. /** Blanking applied from each toggle of the output waveform */
  1294. TCC_FAULT_BLANKING_BOTH_EDGE
  1295. };
  1296. /**
  1297. * \brief TCC (recoverable) fault Input Qualification Action
  1298. */
  1299. enum tcc_fault_qualification {
  1300. /** The input is not disabled on compare condition */
  1301. TCC_FAULT_QUALIFICATION_DISABLE,
  1302. /** The input is disabled when match output signal is at inactive level */
  1303. TCC_FAULT_QUALIFICATION_BY_OUTPUT
  1304. };
  1305. /**
  1306. * \brief TCC (recoverable) fault Output Keep Action
  1307. */
  1308. enum tcc_fault_keep {
  1309. /** Disable keeping, wave output released as soon as fault is released */
  1310. TCC_FAULT_KEEP_DISABLE,
  1311. /** Keep wave output until end of TCC cycle */
  1312. TCC_FAULT_KEEP_TILL_END
  1313. };
  1314. /**
  1315. * \brief TCC Non-recoverable State Outupt
  1316. */
  1317. enum tcc_fault_state_output {
  1318. /** Non-recoverable fault output is tri-stated */
  1319. TCC_FAULT_STATE_OUTPUT_OFF,
  1320. /** Non-recoverable fault force output 0 */
  1321. TCC_FAULT_STATE_OUTPUT_0,
  1322. /** Non-recoverable fault force output 1 */
  1323. TCC_FAULT_STATE_OUTPUT_1
  1324. };
  1325. /**
  1326. * \brief TCC (recoverable) fault Restart Action
  1327. */
  1328. enum tcc_fault_restart {
  1329. /** Restart Action disabled */
  1330. TCC_FAULT_RESTART_DISABLE,
  1331. /** Restart Action enabled */
  1332. TCC_FAULT_RESTART_ENABLE
  1333. };
  1334. /**
  1335. * \brief Configuration struct for TCC module recoverable fault
  1336. */
  1337. struct tcc_recoverable_fault_config {
  1338. /** Fault filter value applied on MCEx event input line (0x0 ~ 0xF).
  1339. * Must be 0 when MCEx event is used as synchronous event.
  1340. * Apply to both recoverable and non-recoverable fault. */
  1341. uint8_t filter_value;
  1342. /** Fault blanking value (0 ~ 255), disable input source for several TCC
  1343. * clocks after the detection of the waveform edge */
  1344. uint8_t blanking_cycles;
  1345. /** Set to \c true to enable restart action */
  1346. bool restart;
  1347. /** Set to \c true to enable keep action (keep until end of TCC cycle) */
  1348. bool keep;
  1349. /** Set to \c true to enable input qualification
  1350. * (disable input when output is inactive) */
  1351. bool qualification;
  1352. /** Specifies if the event input generates recoverable Fault.
  1353. * The event system channel connected to MCEx event input must be
  1354. * configured as asynchronous.
  1355. */
  1356. enum tcc_fault_source source;
  1357. /** Fault Blanking Start Point for recoverable Fault */
  1358. enum tcc_fault_blanking blanking;
  1359. /** Halt action for recoverable Fault */
  1360. enum tcc_fault_halt_action halt_action;
  1361. /** Capture action for recoverable Fault */
  1362. enum tcc_fault_capture_action capture_action;
  1363. /** Channel triggered by recoverable Fault */
  1364. enum tcc_fault_capture_channel capture_channel;
  1365. };
  1366. /**
  1367. * \brief Configuration struct for TCC module non-recoverable fault
  1368. */
  1369. struct tcc_non_recoverable_fault_config {
  1370. /** Fault filter value applied on TCEx event input line (0x0 ~ 0xF).
  1371. * Must be 0 when TCEx event is used as synchronous event. */
  1372. uint8_t filter_value;
  1373. /** Output */
  1374. enum tcc_fault_state_output output;
  1375. };
  1376. /**
  1377. * \brief TCC input event enable/disable/configure structure
  1378. *
  1379. * For configuring an input event.
  1380. */
  1381. struct tcc_input_event_config {
  1382. /** Event action on incoming event */
  1383. enum tcc_event_action action;
  1384. /** Modify event action */
  1385. bool modify_action;
  1386. /** Invert incoming event input line */
  1387. bool invert;
  1388. };
  1389. /**
  1390. * \brief TCC output event enable/disable/configure structure
  1391. *
  1392. * Structure used for configuring an output event.
  1393. */
  1394. struct tcc_output_event_config {
  1395. /** It decides which part of the counter cycle the counter event output
  1396. * is generated */
  1397. enum tcc_event_generation_selection generation_selection;
  1398. /** A switch to allow enable/disable of events, without modifying the
  1399. * event output configuration
  1400. */
  1401. bool modify_generation_selection;
  1402. };
  1403. /**
  1404. * \brief TCC event enable/disable structure
  1405. *
  1406. * Event flags for the \ref tcc_enable_events() and \ref tcc_disable_events().
  1407. */
  1408. struct tcc_events {
  1409. /** Input events configuration */
  1410. struct tcc_input_event_config input_config[2];
  1411. /** Output event configuration */
  1412. struct tcc_output_event_config output_config;
  1413. /** Perform the configured event action when an incoming event is
  1414. * signalled */
  1415. bool on_input_event_perform_action[2];
  1416. /** Perform the configured event action when an incoming channel event is
  1417. * signalled */
  1418. bool on_event_perform_channel_action[TCC_NUM_CHANNELS];
  1419. /** Generate an output event on a channel capture/match.
  1420. * Specify which channels will generate events */
  1421. bool generate_event_on_channel[TCC_NUM_CHANNELS];
  1422. /** Generate an output event on counter overflow/underflow */
  1423. bool generate_event_on_counter_overflow;
  1424. /** Generate an output event on counter retrigger */
  1425. bool generate_event_on_counter_retrigger;
  1426. /** Generate an output event on counter boundary.
  1427. * See \ref tcc_event_output_action. */
  1428. bool generate_event_on_counter_event;
  1429. };
  1430. /**
  1431. * \brief Configuration struct for the TCC module base counter
  1432. *
  1433. * Structure for configuring a TCC as a counter.
  1434. */
  1435. struct tcc_counter_config {
  1436. /** Value to initialize the count register */
  1437. uint32_t count;
  1438. /** Period/top and period/top buffer values for counter */
  1439. uint32_t period;
  1440. /** When \c true, the counter will be stopped on the next hardware or
  1441. * software re-trigger event or overflow/underflow
  1442. */
  1443. bool oneshot;
  1444. #ifdef FEATURE_TCC_GENERATE_DMA_TRIGGER
  1445. /** Counter overflow trigger a DMA request mode */
  1446. enum tcc_count_overflow_dma_trigger_mode dma_trigger_mode;
  1447. #endif
  1448. /** Specifies the direction for the TCC to count */
  1449. enum tcc_count_direction direction;
  1450. /** GCLK generator used to clock the peripheral */
  1451. enum gclk_generator clock_source;
  1452. /** Specifies the prescaler value for GCLK_TCC */
  1453. enum tcc_clock_prescaler clock_prescaler;
  1454. /** Specifies the reload or reset time of the counter and prescaler
  1455. * resynchronization on a re-trigger event for the TCC
  1456. */
  1457. enum tcc_reload_action reload_action;
  1458. };
  1459. /**
  1460. * \brief Configuration struct for the TCC module capture
  1461. *
  1462. * Structure used when configuring TCC channels in capture mode.
  1463. */
  1464. struct tcc_capture_config {
  1465. /** Channel functions selection (capture/match) */
  1466. enum tcc_channel_function channel_function[TCC_NUM_CHANNELS];
  1467. };
  1468. /**
  1469. * \brief Configuration struct for the TCC module match/wave generation
  1470. *
  1471. * The structure, which helps to configure a TCC channel for compare
  1472. * operation and wave generation.
  1473. */
  1474. struct tcc_match_wave_config {
  1475. /** Channel functions selection (capture/match) */
  1476. enum tcc_channel_function channel_function[TCC_NUM_CHANNELS];
  1477. /** Specifies polarity for match output waveform generation */
  1478. enum tcc_wave_polarity wave_polarity[TCC_NUM_CHANNELS];
  1479. /** Specifies which waveform generation mode to use */
  1480. enum tcc_wave_generation wave_generation;
  1481. /** Specifies Ramp mode for waveform generation */
  1482. enum tcc_ramp wave_ramp;
  1483. /** Value to be used for compare match on each channel */
  1484. uint32_t match[TCC_NUM_CHANNELS];
  1485. };
  1486. /**
  1487. * \brief Configuration struct for the TCC module waveform extension
  1488. *
  1489. * This structure is used to specify the waveform extension features for TCC.
  1490. */
  1491. struct tcc_wave_extension_config {
  1492. /** Configuration for recoverable faults */
  1493. struct tcc_recoverable_fault_config
  1494. recoverable_fault[TCC_NUM_FAULTS];
  1495. /** Configuration for non-recoverable faults */
  1496. struct tcc_non_recoverable_fault_config
  1497. non_recoverable_fault[TCC_NUM_WAVE_OUTPUTS];
  1498. /** Invert waveform final outputs lines */
  1499. bool invert[TCC_NUM_WAVE_OUTPUTS];
  1500. };
  1501. /**
  1502. * \brief Configuration struct for the TCC module output pins
  1503. *
  1504. * Structure which is used when taking wave output from TCC.
  1505. */
  1506. struct tcc_pins_config {
  1507. /** Specifies pin output for each channel */
  1508. uint32_t wave_out_pin[TCC_NUM_WAVE_OUTPUTS];
  1509. /** Specifies MUX setting for each output channel pin */
  1510. uint32_t wave_out_pin_mux[TCC_NUM_WAVE_OUTPUTS];
  1511. /** When \c true, PWM output pin for the given channel is enabled */
  1512. bool enable_wave_out_pin[TCC_NUM_WAVE_OUTPUTS];
  1513. };
  1514. /**
  1515. * \brief TCC configuration structure
  1516. *
  1517. * Configuration struct for a TCC instance. This structure should be
  1518. * initialized by the \ref tcc_get_config_defaults function before being
  1519. * modified by the user application.
  1520. */
  1521. struct tcc_config {
  1522. /** Structure for configuring TCC base timer/counter */
  1523. struct tcc_counter_config counter;
  1524. /** TCC match/capture configurations */
  1525. union {
  1526. /** Helps to configure a TCC channel in capture mode */
  1527. struct tcc_capture_config capture;
  1528. /** For configuring a TCC channel in compare mode */
  1529. struct tcc_match_wave_config compare;
  1530. /** Serves the same purpose as compare. Used as an alias for
  1531. * compare,
  1532. * when a TCC channel is configured for wave generation */
  1533. struct tcc_match_wave_config wave;
  1534. };
  1535. /** Structure for configuring TCC waveform extension */
  1536. struct tcc_wave_extension_config wave_ext;
  1537. /** Structure for configuring TCC output pins */
  1538. struct tcc_pins_config pins;
  1539. /** Set to \c true to enable double buffering write. When enabled any write
  1540. * through \ref tcc_set_top_value(), \ref tcc_set_compare_value() and
  1541. * \ref tcc_set_pattern() will direct to the buffer register as buffered
  1542. * value, and the buffered value will be committed to effective register
  1543. * on UPDATE condition, if update is not locked.
  1544. *
  1545. * \note The init values in \ref tcc_config for \ref tcc_init are always
  1546. * filled to effective registers, no matter if double buffering is
  1547. * enabled or not.
  1548. */
  1549. bool double_buffering_enabled;
  1550. /** When \c true the module is enabled during standby */
  1551. bool run_in_standby;
  1552. };
  1553. #if TCC_ASYNC == true
  1554. /* Forward Declaration for the device instance. */
  1555. struct tcc_module;
  1556. /** Type definition for the TCC callback function. */
  1557. typedef void (*tcc_callback_t)(struct tcc_module *const module);
  1558. #endif
  1559. /**
  1560. * \brief TCC software device instance structure
  1561. *
  1562. * TCC software instance structure, used to retain software state information
  1563. * of an associated hardware module instance.
  1564. *
  1565. * \note The fields of this structure should not be altered by the user
  1566. * application; they are reserved only for module-internal use.
  1567. */
  1568. struct tcc_module {
  1569. /** Hardware module pointer of the associated Timer/Counter peripheral. */
  1570. Tcc *hw;
  1571. # if TCC_ASYNC == true
  1572. /** Array of callbacks */
  1573. tcc_callback_t callback[TCC_CALLBACK_N];
  1574. /** Bit mask for callbacks registered */
  1575. uint32_t register_callback_mask;
  1576. /** Bit mask for callbacks enabled */
  1577. uint32_t enable_callback_mask;
  1578. # endif
  1579. /** Set to \c true to write to buffered registers */
  1580. bool double_buffering_enabled;
  1581. };
  1582. #if !defined(__DOXYGEN__)
  1583. uint8_t _tcc_get_inst_index(
  1584. Tcc *const hw);
  1585. #endif
  1586. /**
  1587. * \name Driver Initialization and Configuration
  1588. * @{
  1589. */
  1590. /**
  1591. * \brief Determines if the hardware module is currently synchronizing to the bus
  1592. *
  1593. * Checks to see if the underlying hardware peripheral module is currently
  1594. * synchronizing across multiple clock domains to the hardware bus. This
  1595. * function can be used to delay further operations on a module until such time
  1596. * that it is ready, to prevent blocking delays for synchronization in the
  1597. * user application.
  1598. *
  1599. * \param[in] module_inst Pointer to the software module instance struct
  1600. *
  1601. * \return Synchronization status of the underlying hardware module.
  1602. *
  1603. * \retval false If the module has completed synchronization
  1604. * \retval true If the module synchronization is ongoing
  1605. */
  1606. static inline bool tcc_is_syncing(
  1607. const struct tcc_module *const module_inst)
  1608. {
  1609. /* Sanity check arguments */
  1610. Assert(module_inst);
  1611. Assert(module_inst->hw);
  1612. return (module_inst->hw->SYNCBUSY.reg > 0);
  1613. }
  1614. void tcc_get_config_defaults(
  1615. struct tcc_config *const config,
  1616. Tcc *const hw);
  1617. enum status_code tcc_init(
  1618. struct tcc_module *const module_inst,
  1619. Tcc *const hw,
  1620. const struct tcc_config *const config);
  1621. /** @} */
  1622. /**
  1623. * \name Event Management
  1624. * @{
  1625. */
  1626. enum status_code tcc_enable_events(
  1627. struct tcc_module *const module_inst,
  1628. struct tcc_events *const events);
  1629. void tcc_disable_events(
  1630. struct tcc_module *const module_inst,
  1631. struct tcc_events *const events);
  1632. /** @} */
  1633. /**
  1634. * \name Enable/Disable/Reset
  1635. * @{
  1636. */
  1637. /**
  1638. * \brief Enable the TCC module
  1639. *
  1640. * Enables a TCC module that has been previously initialized. The counter will
  1641. * start when the counter is enabled.
  1642. *
  1643. * \note When the counter is configured to re-trigger on an event, the counter
  1644. * will not start until the next incoming event appears. Then it
  1645. * restarts on any following event.
  1646. *
  1647. * \param[in] module_inst Pointer to the software module instance struct
  1648. */
  1649. static inline void tcc_enable(
  1650. const struct tcc_module *const module_inst)
  1651. {
  1652. /* Sanity check arguments */
  1653. Assert(module_inst);
  1654. Assert(module_inst->hw);
  1655. /* Get a pointer to the module's hardware instance */
  1656. Tcc *const tcc_module = module_inst->hw;
  1657. while (tcc_module->SYNCBUSY.reg & TCC_SYNCBUSY_ENABLE) {
  1658. /* Wait for sync */
  1659. }
  1660. /* Enable the TCC module */
  1661. tcc_module->CTRLA.reg |= TCC_CTRLA_ENABLE;
  1662. }
  1663. /**
  1664. * \brief Disables the TCC module
  1665. *
  1666. * Disables a TCC module and stops the counter.
  1667. *
  1668. * \param[in] module_inst Pointer to the software module instance struct
  1669. */
  1670. static inline void tcc_disable(
  1671. const struct tcc_module *const module_inst)
  1672. {
  1673. /* Sanity check arguments */
  1674. Assert(module_inst);
  1675. Assert(module_inst->hw);
  1676. /* Get a pointer to the module's hardware instance */
  1677. Tcc *const tcc_module = module_inst->hw;
  1678. while (tcc_module->SYNCBUSY.reg & TCC_SYNCBUSY_ENABLE) {
  1679. /* Wait for sync */
  1680. }
  1681. /* Disbale interrupt */
  1682. tcc_module->INTENCLR.reg = TCC_INTENCLR_MASK;
  1683. /* Clear interrupt flag */
  1684. tcc_module->INTFLAG.reg = TCC_INTFLAG_MASK;
  1685. /* Disable the TCC module */
  1686. tcc_module->CTRLA.reg &= ~TC_CTRLA_ENABLE;
  1687. }
  1688. /**
  1689. * \brief Resets the TCC module
  1690. *
  1691. * Resets the TCC module, restoring all hardware module registers to their
  1692. * default values and disabling the module. The TCC module will not be
  1693. * accessible while the reset is being performed.
  1694. *
  1695. * \note When resetting a 32-bit counter only the master TCC module's instance
  1696. * structure should be passed to the function.
  1697. *
  1698. * \param[in] module_inst Pointer to the software module instance struct
  1699. *
  1700. */
  1701. static inline void tcc_reset(
  1702. const struct tcc_module *const module_inst)
  1703. {
  1704. /* Sanity check arguments */
  1705. Assert(module_inst);
  1706. Assert(module_inst->hw);
  1707. /* Get a pointer to the module hardware instance */
  1708. Tcc *const tcc_module = module_inst->hw;
  1709. /* Disable this module if it is running */
  1710. if (tcc_module->CTRLA.reg & TCC_CTRLA_ENABLE) {
  1711. tcc_disable(module_inst);
  1712. while (tcc_is_syncing(module_inst)) {
  1713. /* wait while module is disabling */
  1714. }
  1715. }
  1716. /* Reset this TC module */
  1717. tcc_module->CTRLA.reg |= TCC_CTRLA_SWRST;
  1718. }
  1719. /** @} */
  1720. /**
  1721. * \name Set/Toggle Count Direction
  1722. * @{
  1723. */
  1724. /**
  1725. * \brief Sets the TCC module count direction
  1726. *
  1727. * Sets the count direction of an initialized TCC module. The
  1728. * specified TCC module can remain running or stopped.
  1729. *
  1730. * \param[in] module_inst Pointer to the software module instance struct
  1731. * \param[in] dir New timer count direction to set
  1732. */
  1733. static inline void tcc_set_count_direction(
  1734. const struct tcc_module *const module_inst,
  1735. enum tcc_count_direction dir)
  1736. {
  1737. /* Sanity check arguments */
  1738. Assert(module_inst);
  1739. Assert(module_inst->hw);
  1740. /* Get a pointer to the module's hardware instance */
  1741. Tcc *const tcc_module = module_inst->hw;
  1742. while (tcc_module->SYNCBUSY.reg & TCC_SYNCBUSY_CTRLB) {
  1743. /* Wait for sync */
  1744. }
  1745. /* Set count direction */
  1746. if (TCC_COUNT_DIRECTION_DOWN == dir) {
  1747. tcc_module->CTRLBSET.reg = TCC_CTRLBSET_DIR;
  1748. return;
  1749. }
  1750. tcc_module->CTRLBCLR.reg = TCC_CTRLBCLR_DIR;
  1751. }
  1752. /**
  1753. * \brief Toggles the TCC module count direction
  1754. *
  1755. * Toggles the count direction of an initialized TCC module. The
  1756. * specified TCC module can remain running or stopped.
  1757. *
  1758. * \param[in] module_inst Pointer to the software module instance struct
  1759. */
  1760. static inline void tcc_toggle_count_direction(
  1761. const struct tcc_module *const module_inst)
  1762. {
  1763. /* Sanity check arguments */
  1764. Assert(module_inst);
  1765. Assert(module_inst->hw);
  1766. /* Get a pointer to the module's hardware instance */
  1767. Tcc *const tcc_module = module_inst->hw;
  1768. while (tcc_module->SYNCBUSY.reg & TCC_SYNCBUSY_CTRLB) {
  1769. /* Wait for sync */
  1770. }
  1771. bool dir_value_1 = tcc_module->CTRLBSET.bit.DIR;
  1772. if (dir_value_1) {
  1773. tcc_module->CTRLBCLR.reg = TCC_CTRLBCLR_DIR;
  1774. } else {
  1775. tcc_module->CTRLBSET.reg = TCC_CTRLBSET_DIR;
  1776. }
  1777. }
  1778. /** @} */
  1779. /**
  1780. * \name Get/Set Count Value
  1781. * @{
  1782. */
  1783. uint32_t tcc_get_count_value(
  1784. const struct tcc_module *const module_inst);
  1785. enum status_code tcc_set_count_value(
  1786. const struct tcc_module *const module_inst,
  1787. const uint32_t count);
  1788. /** @} */
  1789. /**
  1790. * \name Stop/Restart Counter
  1791. * @{
  1792. */
  1793. /**
  1794. * \brief Stops the counter
  1795. *
  1796. * This function will stop the counter. When the counter is stopped
  1797. * the value in the count register is set to 0 if the counter was
  1798. * counting up, or maximum or the top value if the counter was counting
  1799. * down.
  1800. *
  1801. * \param[in] module_inst Pointer to the software module instance struct
  1802. */
  1803. static inline void tcc_stop_counter(
  1804. const struct tcc_module *const module_inst)
  1805. {
  1806. /* Sanity check arguments */
  1807. Assert(module_inst);
  1808. Assert(module_inst->hw);
  1809. /* Get a pointer to the module's hardware instance */
  1810. Tcc *const tcc_module = module_inst->hw;
  1811. uint32_t last_cmd;
  1812. /* Wait until last command is done */
  1813. do {
  1814. while (tcc_module->SYNCBUSY.reg & TCC_SYNCBUSY_CTRLB) {
  1815. /* Wait for sync */
  1816. }
  1817. last_cmd = tcc_module->CTRLBSET.reg & TCC_CTRLBSET_CMD_Msk;
  1818. if (last_cmd == TCC_CTRLBSET_CMD_NONE) {
  1819. break;
  1820. } else if (last_cmd == TCC_CTRLBSET_CMD_STOP) {
  1821. /* Command have been issued */
  1822. return;
  1823. } else if (last_cmd == TCC_CTRLBSET_CMD_RETRIGGER) {
  1824. /* Cancel RETRIGGER command and issue STOP */
  1825. tcc_module->CTRLBCLR.reg = TCC_CTRLBCLR_CMD_Msk;
  1826. }
  1827. } while (1);
  1828. /* Write command to execute */
  1829. tcc_module->CTRLBSET.reg = TCC_CTRLBSET_CMD_STOP;
  1830. }
  1831. /**
  1832. * \brief Starts the counter from beginning
  1833. *
  1834. * Restarts an initialized TCC module's counter.
  1835. *
  1836. * \param[in] module_inst Pointer to the software module instance struct
  1837. */
  1838. static inline void tcc_restart_counter(
  1839. const struct tcc_module *const module_inst)
  1840. {
  1841. /* Sanity check arguments */
  1842. Assert(module_inst);
  1843. Assert(module_inst->hw);
  1844. /* Get a pointer to the module's hardware instance */
  1845. Tcc *const tcc_module = module_inst->hw;
  1846. uint32_t last_cmd;
  1847. /* Wait until last command is done */
  1848. do {
  1849. while (tcc_module->SYNCBUSY.reg & TCC_SYNCBUSY_CTRLB) {
  1850. /* Wait for sync */
  1851. }
  1852. last_cmd = tcc_module->CTRLBSET.reg & TCC_CTRLBSET_CMD_Msk;
  1853. if (last_cmd == TCC_CTRLBSET_CMD_NONE) {
  1854. break;
  1855. } else if (last_cmd == TCC_CTRLBSET_CMD_RETRIGGER) {
  1856. /* Command have been issued */
  1857. return;
  1858. } else if (last_cmd == TCC_CTRLBSET_CMD_STOP) {
  1859. /* Cancel STOP command and issue RETRIGGER */
  1860. tcc_module->CTRLBCLR.reg = TCC_CTRLBCLR_CMD_Msk;
  1861. }
  1862. } while (1);
  1863. /* Write command to execute */
  1864. tcc_module->CTRLBSET.reg = TCC_CTRLBSET_CMD_RETRIGGER;
  1865. }
  1866. /** @} */
  1867. #ifdef FEATURE_TCC_GENERATE_DMA_TRIGGER
  1868. /**
  1869. * \name Generate TCC DMA Triggers Command
  1870. * @{
  1871. */
  1872. /**
  1873. * \brief TCC DMA Trigger.
  1874. *
  1875. * TCC DMA trigger command.
  1876. *
  1877. * \param[in] module_inst Pointer to the software module instance struct
  1878. */
  1879. static inline void tcc_dma_trigger_command(
  1880. const struct tcc_module *const module_inst)
  1881. {
  1882. /* Sanity check arguments */
  1883. Assert(module_inst);
  1884. Assert(module_inst->hw);
  1885. /* Get a pointer to the module's hardware instance */
  1886. Tcc *const tcc_module = module_inst->hw;
  1887. while (tcc_module->SYNCBUSY.reg & TCC_SYNCBUSY_CTRLB) {
  1888. /* Wait for sync */
  1889. }
  1890. /* Make certain that there are no conflicting commands in the register */
  1891. tcc_module->CTRLBCLR.reg = TCC_CTRLBCLR_CMD_NONE;
  1892. while (tcc_module->SYNCBUSY.reg & TCC_SYNCBUSY_CTRLB) {
  1893. /* Wait for sync */
  1894. }
  1895. #if !(SAML21 || SAML22 || SAMR30)
  1896. /* Write command to execute */
  1897. tcc_module->CTRLBSET.reg = TCC_CTRLBSET_CMD_DMATRG;
  1898. #endif
  1899. #if (SAML21XXXB) || (SAML22) || (SAMR30)
  1900. /* Write command to execute */
  1901. tcc_module->CTRLBSET.reg = TCC_CTRLBSET_CMD_DMAOS;
  1902. #endif
  1903. }
  1904. /** @} */
  1905. #endif
  1906. /**
  1907. * \name Get/Set Compare/Capture Register
  1908. * @{
  1909. */
  1910. uint32_t tcc_get_capture_value(
  1911. const struct tcc_module *const module_inst,
  1912. const enum tcc_match_capture_channel channel_index);
  1913. enum status_code tcc_set_compare_value(
  1914. const struct tcc_module *const module_inst,
  1915. const enum tcc_match_capture_channel channel_index,
  1916. const uint32_t compare);
  1917. /** @} */
  1918. /**
  1919. * \name Set Top Value
  1920. * @{
  1921. */
  1922. enum status_code tcc_set_top_value(
  1923. const struct tcc_module *const module_inst,
  1924. const uint32_t top_value);
  1925. /** @} */
  1926. /**
  1927. * \name Set Output Pattern
  1928. * @{
  1929. */
  1930. enum status_code tcc_set_pattern(
  1931. const struct tcc_module *const module_inst,
  1932. const uint32_t line_index,
  1933. const enum tcc_output_pattern pattern);
  1934. /** @} */
  1935. /**
  1936. * \name Set Ramp Index
  1937. * @{
  1938. */
  1939. /**
  1940. * \brief Sets the TCC module ramp index on next cycle
  1941. *
  1942. * In RAMP2 and RAMP2A operation, we can force either cycle A or cycle B at
  1943. * the output, on the next clock cycle.
  1944. * When ramp index command is disabled, cycle A and cycle B will appear at
  1945. * the output, on alternate clock cycles.
  1946. * See \ref tcc_ramp.
  1947. *
  1948. * \param[in] module_inst Pointer to the software module instance struct
  1949. * \param[in] ramp_index Ramp index (\ref tcc_ramp_index) of the next cycle
  1950. */
  1951. static inline void tcc_set_ramp_index(
  1952. const struct tcc_module *const module_inst,
  1953. const enum tcc_ramp_index ramp_index)
  1954. {
  1955. /* Sanity check arguments */
  1956. Assert(module_inst);
  1957. Assert(module_inst->hw);
  1958. /* Get a pointer to the module's hardware instance */
  1959. Tcc *const tcc_module = module_inst->hw;
  1960. uint32_t last_cmd;
  1961. /* Wait until last command is done */
  1962. do {
  1963. while (tcc_module->SYNCBUSY.reg & TCC_SYNCBUSY_CTRLB) {
  1964. /* Wait for sync */
  1965. }
  1966. if (TCC_RAMP_INDEX_DEFAULT == ramp_index) {
  1967. /* Cancel pending command */
  1968. tcc_module->CTRLBCLR.reg = TCC_CTRLBSET_IDXCMD_HOLD;
  1969. return;
  1970. }
  1971. last_cmd = tcc_module->CTRLBSET.reg & TCC_CTRLBSET_IDXCMD_Msk;
  1972. if (last_cmd == TCC_CTRLBSET_IDXCMD_DISABLE) {
  1973. break;
  1974. } else if (last_cmd == TCC_CTRLBSET_IDXCMD(ramp_index)) {
  1975. /* Command have been issued */
  1976. return;
  1977. }
  1978. } while (1);
  1979. /* Write command to execute */
  1980. tcc_module->CTRLBSET.reg = TCC_CTRLBSET_IDXCMD(ramp_index);
  1981. }
  1982. /** @} */
  1983. /**
  1984. * \name Status Management
  1985. * @{
  1986. */
  1987. /**
  1988. * \brief Checks if the timer/counter is running
  1989. *
  1990. * \param[in] module_inst Pointer to the TCC software instance struct
  1991. *
  1992. * \return Status which indicates whether the module is running.
  1993. *
  1994. * \retval true The timer/counter is running
  1995. * \retval false The timer/counter is stopped
  1996. */
  1997. static inline bool tcc_is_running(
  1998. struct tcc_module *const module_inst)
  1999. {
  2000. /* Sanity check arguments */
  2001. Assert(module_inst);
  2002. Assert(module_inst->hw);
  2003. return !module_inst->hw->STATUS.bit.STOP;
  2004. }
  2005. uint32_t tcc_get_status(
  2006. struct tcc_module *const module_inst);
  2007. void tcc_clear_status(
  2008. struct tcc_module *const module_inst,
  2009. const uint32_t status_flags);
  2010. /** @} */
  2011. /**
  2012. * \name Double Buffering Management
  2013. * @{
  2014. */
  2015. /**
  2016. * \brief Enable TCC double buffering write
  2017. *
  2018. * When double buffering write is enabled, the following function will write
  2019. * values to buffered registers instead of effective ones (buffered):
  2020. * - PERB: through \ref tcc_set_top_value()
  2021. * - CCBx(x is 0~3): through \ref tcc_set_compare_value()
  2022. * - PATTB: through \ref tcc_set_pattern()
  2023. *
  2024. * Then, on UPDATE condition the buffered registers are committed to regular ones
  2025. * to take effect.
  2026. *
  2027. * \param[in] module_inst Pointer to the TCC software instance struct
  2028. */
  2029. static inline void tcc_enable_double_buffering(
  2030. struct tcc_module *const module_inst)
  2031. {
  2032. /* Sanity check arguments */
  2033. Assert(module_inst);
  2034. module_inst->double_buffering_enabled = true;
  2035. }
  2036. /**
  2037. * \brief Disable TCC double buffering Write
  2038. *
  2039. * When double buffering write is disabled, following function will write values
  2040. * to effective registers (not buffered):
  2041. * - PER: through \ref tcc_set_top_value()
  2042. * - CCx(x is 0~3): through \ref tcc_set_compare_value()
  2043. * - PATT: through \ref tcc_set_pattern()
  2044. *
  2045. * \note This function does not lock double buffer update, which means on next
  2046. * UPDATE condition the last written buffered values will be committed to
  2047. * take effect. Invoke \ref tcc_lock_double_buffer_update() before this
  2048. * function to disable double buffering update, if this change is not
  2049. * expected.
  2050. *
  2051. * \param[in] module_inst Pointer to the TCC software instance struct
  2052. */
  2053. static inline void tcc_disable_double_buffering(
  2054. struct tcc_module *const module_inst)
  2055. {
  2056. /* Sanity check arguments */
  2057. Assert(module_inst);
  2058. Assert(module_inst->hw);
  2059. module_inst->double_buffering_enabled = false;
  2060. }
  2061. /**
  2062. * \brief Lock the TCC double buffered registers updates
  2063. *
  2064. * Locks the double buffered registers so they will not be updated through
  2065. * their buffered values on UPDATE conditions.
  2066. *
  2067. * \param[in] module_inst Pointer to the TCC software instance struct
  2068. *
  2069. */
  2070. static inline void tcc_lock_double_buffer_update(
  2071. struct tcc_module *const module_inst)
  2072. {
  2073. /* Sanity check arguments */
  2074. Assert(module_inst);
  2075. Assert(module_inst->hw);
  2076. while (module_inst->hw->SYNCBUSY.reg & TCC_SYNCBUSY_CTRLB) {
  2077. /* Wait for sync */
  2078. }
  2079. module_inst->hw->CTRLBSET.reg = TCC_CTRLBSET_LUPD;
  2080. }
  2081. /**
  2082. * \brief Unlock the TCC double buffered registers updates
  2083. *
  2084. * Unlock the double buffered registers so they will be updated through
  2085. * their buffered values on UPDATE conditions.
  2086. *
  2087. * \param[in] module_inst Pointer to the TCC software instance struct
  2088. *
  2089. */
  2090. static inline void tcc_unlock_double_buffer_update(
  2091. struct tcc_module *const module_inst)
  2092. {
  2093. /* Sanity check arguments */
  2094. Assert(module_inst);
  2095. Assert(module_inst->hw);
  2096. while (module_inst->hw->SYNCBUSY.reg & TCC_SYNCBUSY_CTRLB) {
  2097. /* Wait for sync */
  2098. }
  2099. module_inst->hw->CTRLBCLR.reg = TCC_CTRLBCLR_LUPD;
  2100. }
  2101. /**
  2102. * \brief Force the TCC double buffered registers to update once
  2103. *
  2104. * \param[in] module_inst Pointer to the TCC software instance struct
  2105. *
  2106. */
  2107. static inline void tcc_force_double_buffer_update(
  2108. struct tcc_module *const module_inst)
  2109. {
  2110. /* Sanity check arguments */
  2111. Assert(module_inst);
  2112. Assert(module_inst->hw);
  2113. /* Get a pointer to the module's hardware instance */
  2114. Tcc *const tcc_module = module_inst->hw;
  2115. uint32_t last_cmd;
  2116. /* Wait until last command is done */
  2117. do {
  2118. while (tcc_module->SYNCBUSY.reg & TCC_SYNCBUSY_CTRLB) {
  2119. /* Wait for sync */
  2120. }
  2121. last_cmd = tcc_module->CTRLBSET.reg & TCC_CTRLBSET_CMD_Msk;
  2122. if (last_cmd == TCC_CTRLBSET_CMD_NONE) {
  2123. break;
  2124. } else if (last_cmd == TCC_CTRLBSET_CMD_UPDATE) {
  2125. /* Command have been issued */
  2126. return;
  2127. }
  2128. } while (1);
  2129. /* Write command to execute */
  2130. tcc_module->CTRLBSET.reg = TCC_CTRLBSET_CMD_UPDATE;
  2131. }
  2132. /**
  2133. * \brief Enable Circular option for double buffered Top/Period Values
  2134. *
  2135. * Enable circular option for the double buffered top/period values.
  2136. * On each UPDATE condition, the contents of PERB and PER are switched, meaning
  2137. * that the contents of PERB are transferred to PER and the contents of PER are
  2138. * transferred to PERB.
  2139. *
  2140. * \param[in] module_inst Pointer to the TCC software instance struct
  2141. */
  2142. static inline void tcc_enable_circular_buffer_top(
  2143. struct tcc_module *const module_inst)
  2144. {
  2145. /* Sanity check arguments */
  2146. Assert(module_inst);
  2147. Assert(module_inst->hw);
  2148. module_inst->hw->WAVE.reg |= TCC_WAVE_CIPEREN;
  2149. }
  2150. /**
  2151. * \brief Disable Circular option for double buffered Top/Period Values
  2152. *
  2153. * Stop circularing the double buffered top/period values.
  2154. *
  2155. * \param[in] module_inst Pointer to the TCC software instance struct
  2156. */
  2157. static inline void tcc_disable_circular_buffer_top(
  2158. struct tcc_module *const module_inst)
  2159. {
  2160. /* Sanity check arguments */
  2161. Assert(module_inst);
  2162. Assert(module_inst->hw);
  2163. module_inst->hw->WAVE.reg &= ~TCC_WAVE_CIPEREN;
  2164. }
  2165. enum status_code tcc_set_double_buffer_top_values(
  2166. const struct tcc_module *const module_inst,
  2167. const uint32_t top_value, const uint32_t top_buffer_value);
  2168. enum status_code tcc_enable_circular_buffer_compare(
  2169. struct tcc_module *const module_inst,
  2170. enum tcc_match_capture_channel channel_index);
  2171. enum status_code tcc_disable_circular_buffer_compare(
  2172. struct tcc_module *const module_inst,
  2173. enum tcc_match_capture_channel channel_index);
  2174. enum status_code tcc_set_double_buffer_compare_values(
  2175. struct tcc_module *const module_inst,
  2176. enum tcc_match_capture_channel channel_index,
  2177. const uint32_t compare,
  2178. const uint32_t compare_buffer);
  2179. /** @} */
  2180. /** @} */
  2181. #ifdef __cplusplus
  2182. }
  2183. #endif
  2184. /**
  2185. * \page asfdoc_sam0_tcc_extra Extra Information for TCC Driver
  2186. *
  2187. * \section asfdoc_sam0_tcc_extra_acronyms Acronyms
  2188. * The table below presents the acronyms used in this module:
  2189. *
  2190. * <table>
  2191. * <tr>
  2192. * <th>Acronym</th>
  2193. * <th>Description</th>
  2194. * </tr>
  2195. * <tr>
  2196. * <td>DMA</td>
  2197. * <td>Direct Memory Access</td>
  2198. * </tr>
  2199. * <tr>
  2200. * <td>TCC</td>
  2201. * <td>Timer Counter for Control Applications</td>
  2202. * </tr>
  2203. * <tr>
  2204. * <td>PWM</td>
  2205. * <td>Pulse Width Modulation</td>
  2206. * </tr>
  2207. * <tr>
  2208. * <td>PWP</td>
  2209. * <td>Pulse Width Period</td>
  2210. * </tr>
  2211. * <tr>
  2212. * <td>PPW</td>
  2213. * <td>Period Pulse Width</td>
  2214. * </tr>
  2215. * </table>
  2216. *
  2217. *
  2218. * \section asfdoc_sam0_tcc_extra_dependencies Dependencies
  2219. * This driver has the following dependencies:
  2220. *
  2221. * - \ref asfdoc_sam0_system_pinmux_group "System Pin Multiplexer Driver"
  2222. *
  2223. *
  2224. * \section asfdoc_sam0_tcc_extra_errata Errata
  2225. * There are no errata related to this driver.
  2226. *
  2227. *
  2228. * \section asfdoc_sam0_tcc_extra_history Module History
  2229. * An overview of the module history is presented in the table below, with
  2230. * details on the enhancements and fixes made to the module since its first
  2231. * release. The current version of this corresponds to the newest version in
  2232. * the table.
  2233. *
  2234. * <table>
  2235. * <tr>
  2236. * <th>Changelog</th>
  2237. * </tr>
  2238. * <tr>
  2239. * <td>Add double buffering functionality</td>
  2240. * </tr>
  2241. * <tr>
  2242. * <td>Add fault handling functionality</td>
  2243. * </tr>
  2244. * <tr>
  2245. * <td>Initial Release</td>
  2246. * </tr>
  2247. * </table>
  2248. */
  2249. /**
  2250. * \page asfdoc_sam0_tcc_exqsg Examples for TCC Driver
  2251. *
  2252. * This is a list of the available Quick Start guides (QSGs) and example
  2253. * applications for \ref asfdoc_sam0_tcc_group. QSGs are simple examples with
  2254. * step-by-step instructions to configure and use this driver in a selection of
  2255. * use cases. Note that QSGs can be compiled as a standalone application or be
  2256. * added to the user application.
  2257. *
  2258. * - \subpage asfdoc_sam0_tcc_basic_use_case
  2259. * - \subpage asfdoc_sam0_tcc_buffering_use_case
  2260. * \if TCC_CALLBACK_MODE
  2261. * - \subpage asfdoc_sam0_tcc_timer_use_case
  2262. * - \subpage asfdoc_sam0_tcc_callback_use_case
  2263. * - \subpage asfdoc_sam0_tcc_faultx_use_case
  2264. * - \subpage asfdoc_sam0_tcc_faultn_use_case
  2265. * \endif
  2266. * - \subpage asfdoc_sam0_tcc_dma_use_case
  2267. *
  2268. * \page asfdoc_sam0_tcc_document_revision_history Document Revision History
  2269. *
  2270. * <table>
  2271. * <tr>
  2272. * <th>Doc. Rev.</th>
  2273. * <th>Date</th>
  2274. * <th>Comments</th>
  2275. * </tr>
  2276. * <tr>
  2277. * <td>42256C</td>
  2278. * <td>12/2015</td>
  2279. * <td>Added support for SAM L21/L22, SAM DA1, and SAM C20/C21</td>
  2280. * </tr>
  2281. * <tr>
  2282. * <td>42256B</td>
  2283. * <td>12/2014</td>
  2284. * <td>Added fault handling functionality.
  2285. * Added double buffering functionality with use case.
  2286. * Added timer use case.
  2287. * Added SAM R21/D10/D11 support.</td>
  2288. * </tr>
  2289. * <tr>
  2290. * <td>42256A</td>
  2291. * <td>01/2014</td>
  2292. * <td>Initial release</td>
  2293. * </tr>
  2294. * </table>
  2295. */
  2296. #endif /* TCC_H_INCLUDED */