usart.h 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief SAM SERCOM USART Driver
  6. *
  7. * Copyright (C) 2012-2016 Atmel Corporation. All rights reserved.
  8. *
  9. * \asf_license_start
  10. *
  11. * \page License
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. *
  23. * 3. The name of Atmel may not be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * 4. This software may only be redistributed and used in connection with an
  27. * Atmel microcontroller product.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  30. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  32. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  33. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  35. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  37. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  38. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. * POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. * \asf_license_stop
  42. *
  43. */
  44. /*
  45. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  46. */
  47. #ifndef USART_H_INCLUDED
  48. #define USART_H_INCLUDED
  49. /**
  50. * \defgroup asfdoc_sam0_sercom_usart_group SAM Serial USART (SERCOM USART) Driver
  51. *
  52. * This driver for Atmel&reg; | SMART ARM&reg;-based microcontrollers provides
  53. * an interface for the configuration and management of the SERCOM module in
  54. * its USART mode to transfer or receive USART data frames. The following driver
  55. * API modes are covered by this manual:
  56. *
  57. * - Polled APIs
  58. * \if USART_CALLBACK_MODE
  59. * - Callback APIs
  60. * \endif
  61. *
  62. * The following peripheral is used by this module:
  63. * - SERCOM (Serial Communication Interface)
  64. *
  65. * The following devices can use this module:
  66. * - Atmel | SMART SAM D20/D21
  67. * - Atmel | SMART SAM R21
  68. * - Atmel | SMART SAM D09/D10/D11
  69. * - Atmel | SMART SAM D10/D11
  70. * - Atmel | SMART SAM L21/L22
  71. * - Atmel | SMART SAM DA1
  72. * - Atmel | SMART SAM C20/C21
  73. * - Atmel | SMART SAM HA1
  74. *
  75. * The outline of this documentation is as follows:
  76. * - \ref asfdoc_sam0_sercom_usart_prerequisites
  77. * - \ref asfdoc_sam0_sercom_usart_overview
  78. * - \ref asfdoc_sam0_sercom_usart_special_considerations
  79. * - \ref asfdoc_sam0_sercom_usart_extra_info
  80. * - \ref asfdoc_sam0_sercom_usart_examples
  81. * - \ref asfdoc_sam0_sercom_usart_api_overview
  82. *
  83. * \section asfdoc_sam0_sercom_usart_prerequisites Prerequisites
  84. *
  85. * To use the USART you need to have a GCLK generator enabled and running
  86. * that can be used as the SERCOM clock source. This can either be configured
  87. * in conf_clocks.h or by using the system clock driver.
  88. *
  89. * \section asfdoc_sam0_sercom_usart_overview Module Overview
  90. *
  91. * This driver will use one (or more) SERCOM interface(s) in the system
  92. * and configure it to run as a USART interface in either synchronous
  93. * or asynchronous mode.
  94. *
  95. * \subsection asfdoc_sam0_sercom_usart_features Driver Feature Macro Definition
  96. * <table>
  97. * <tr>
  98. * <th>Driver Feature Macro</th>
  99. * <th>Supported devices</th>
  100. * </tr>
  101. * <tr>
  102. * <td>FEATURE_USART_SYNC_SCHEME_V2</td>
  103. * <td>SAM D21/R21/D09/D10/D11/L21/L22/DA1/C20/C21</td>
  104. * </tr>
  105. * <tr>
  106. * <td>FEATURE_USART_OVER_SAMPLE</td>
  107. * <td>SAM D21/R21/D09/D10/D11/L21/L22/DA1/C20/C21</td>
  108. * </tr>
  109. * <tr>
  110. * <td>FEATURE_USART_HARDWARE_FLOW_CONTROL</td>
  111. * <td>SAM D21/R21/D09/D10/D11/L21/L22/DA1/C20/C21</td>
  112. * </tr>
  113. * <tr>
  114. * <td>FEATURE_USART_IRDA</td>
  115. * <td>SAM D21/R21/D09/D10/D11/L21/L22/DA1/C20/C21</td>
  116. * </tr>
  117. * <tr>
  118. * <td>FEATURE_USART_LIN_SLAVE</td>
  119. * <td>SAM D21/R21/D09/D10/D11/L21/L22/DA1/C20/C21</td>
  120. * </tr>
  121. * <tr>
  122. * <td>FEATURE_USART_COLLISION_DECTION</td>
  123. * <td>SAM D21/R21/D09/D10/D11/L21/L22/DA1/C20/C21</td>
  124. * </tr>
  125. * <tr>
  126. * <td>FEATURE_USART_START_FRAME_DECTION</td>
  127. * <td>SAM D21/R21/D09/D10/D11/L21/L22/DA1/C20/C21</td>
  128. * </tr>
  129. * <tr>
  130. * <td>FEATURE_USART_IMMEDIATE_BUFFER_OVERFLOW_NOTIFICATION</td>
  131. * <td>SAM D21/R21/D09/D10/D11/L21/L22/DA1/C20/C21</td>
  132. * </tr>
  133. * <tr>
  134. * <td>FEATURE_USART_RS485</td>
  135. * <td>SAM C20/C21</td>
  136. * </tr>
  137. * <tr>
  138. * <td>FEATURE_USART_LIN_MASTER</td>
  139. * <td>SAM L22/C20/C21</td>
  140. * </tr>
  141. * </table>
  142. * \note The specific features are only available in the driver when the
  143. * selected device supports those features.
  144. *
  145. * \subsection asfdoc_sam0_sercom_usart_overview_frame_format Frame Format
  146. *
  147. * Communication is based on frames, where the frame format can be customized
  148. * to accommodate a wide range of standards. A frame consists of a start bit,
  149. * a number of data bits, an optional parity bit for error detection as well
  150. * as a configurable length stop bit(s) - see
  151. * \ref asfdoc_sam0_sercom_usart_frame_diagram "the figure below".
  152. * \ref asfdoc_sam0_sercom_usart_frame_params "The table below" shows the
  153. * available parameters you can change in a frame.
  154. *
  155. * \anchor asfdoc_sam0_sercom_usart_frame_params
  156. * <table>
  157. * <caption>USART Frame Parameters</caption>
  158. * <tr>
  159. * <th>Parameter</th>
  160. * <th>Options</th>
  161. * </tr>
  162. * <tr>
  163. * <td>Start bit</td>
  164. * <td>1</td>
  165. * </tr>
  166. * <tr>
  167. * <td>Data bits</td>
  168. * <td>5, 6, 7, 8, 9</td>
  169. * </tr>
  170. * <tr>
  171. * <td>Parity bit</td>
  172. * <td>None, Even, Odd</td>
  173. * </tr>
  174. * <tr>
  175. * <td>Stop bits</td>
  176. * <td>1, 2</td>
  177. * </tr>
  178. * </table>
  179. *
  180. * \anchor asfdoc_sam0_sercom_usart_frame_diagram
  181. * \image html usart_frame.svg "USART Frame Overview" width=100%
  182. *
  183. * \subsection asfdoc_sam0_sercom_usart_overview_sync Synchronous Mode
  184. *
  185. * In synchronous mode a dedicated clock line is provided; either by the USART
  186. * itself if in master mode, or by an external master if in slave mode.
  187. * Maximum transmission speed is the same as the GCLK clocking the USART
  188. * peripheral when in slave mode, and the GCLK divided by two if in
  189. * master mode. In synchronous mode the interface needs three lines to
  190. * communicate:
  191. * - TX (Transmit pin)
  192. * - RX (Receive pin)
  193. * - XCK (Clock pin)
  194. *
  195. * \subsubsection asfdoc_sam0_sercom_usart_overview_sync_sampling Data Sampling
  196. * In synchronous mode the data is sampled on either the rising or falling edge
  197. * of the clock signal. This is configured by setting the clock polarity in the
  198. * configuration struct.
  199. *
  200. * \subsection asfdoc_sam0_sercom_usart_overview_async Asynchronous Mode
  201. *
  202. * In asynchronous mode no dedicated clock line is used, and the communication
  203. * is based on matching the clock speed on the transmitter and receiver. The
  204. * clock is generated from the internal SERCOM baudrate generator, and the
  205. * frames are synchronized by using the frame start bits. Maximum transmission
  206. * speed is limited to the SERCOM GCLK divided by 16.
  207. * In asynchronous mode the interface only needs two lines to communicate:
  208. * - TX (Transmit pin)
  209. * - RX (Receive pin)
  210. *
  211. * \subsubsection asfdoc_sam0_sercom_usart_overview_async_clock_matching Transmitter/receiver Clock Matching
  212. *
  213. * For successful transmit and receive using the asynchronous mode the receiver
  214. * and transmitter clocks needs to be closely matched. When receiving a frame
  215. * that does not match the selected baudrate closely enough the receiver will
  216. * be unable to synchronize the frame(s), and garbage transmissions will
  217. * result.
  218. *
  219. * \subsection asfdoc_sam0_sercom_usart_parity Parity
  220. * Parity can be enabled to detect if a transmission was in error. This is done
  221. * by counting the number of "1" bits in the frame. When using even parity the
  222. * parity bit will be set if the total number of "1"s in the frame are an even
  223. * number. If using odd parity the parity bit will be set if the total number
  224. * of "1"s are odd.
  225. *
  226. * When receiving a character the receiver will count the number of "1"s in the
  227. * frame and give an error if the received frame and parity bit disagree.
  228. *
  229. * \subsection asfdoc_sam0_sercom_usart_overview_pin_configuration GPIO Configuration
  230. *
  231. * The SERCOM module has four internal pads; the RX pin can be placed freely on
  232. * any one of the four pads, and the TX and XCK pins have two predefined
  233. * positions that can be selected as a pair. The pads can then be routed to an
  234. * external GPIO pin using the normal pin multiplexing scheme on the SAM.
  235. *
  236. * \section asfdoc_sam0_sercom_usart_special_considerations Special Considerations
  237. *
  238. * \if USART_CALLBACK_MODE
  239. * Never execute large portions of code in the callbacks. These
  240. * are run from the interrupt routine, and thus having long callbacks will
  241. * keep the processor in the interrupt handler for an equally long time.
  242. * A common way to handle this is to use global flags signaling the
  243. * main application that an interrupt event has happened, and only do the
  244. * minimal needed processing in the callback.
  245. * \else
  246. * No special considerations.
  247. * \endif
  248. *
  249. * \section asfdoc_sam0_sercom_usart_extra_info Extra Information
  250. *
  251. * For extra information, see \ref asfdoc_sam0_sercom_usart_extra. This includes:
  252. * - \ref asfdoc_sam0_sercom_usart_extra_acronyms
  253. * - \ref asfdoc_sam0_sercom_usart_extra_dependencies
  254. * - \ref asfdoc_sam0_sercom_usart_extra_errata
  255. * - \ref asfdoc_sam0_sercom_usart_extra_history
  256. *
  257. * \section asfdoc_sam0_sercom_usart_examples Examples
  258. *
  259. * For a list of examples related to this driver, see
  260. * \ref asfdoc_sam0_sercom_usart_exqsg.
  261. *
  262. * \section asfdoc_sam0_sercom_usart_api_overview API Overview
  263. * @{
  264. */
  265. #include <compiler.h>
  266. #include <sercom.h>
  267. #include <pinmux.h>
  268. #if USART_CALLBACK_MODE == true
  269. # include <sercom_interrupt.h>
  270. #endif
  271. #ifdef __cplusplus
  272. extern "C" {
  273. #endif
  274. /**
  275. * \name Driver Feature Definition
  276. * Define SERCOM USART features set according to different device family.
  277. * @{
  278. */
  279. #if (SAMD21) || (SAMR21) || (SAMD09) || (SAMD10) || (SAMD11) || \
  280. (SAML21) || (SAML22) || (SAMDA1) || (SAMC20) || (SAMC21) || \
  281. (SAMR30) || (SAMHA1) || defined(__DOXYGEN__)
  282. /** USART sync scheme version 2. */
  283. # define FEATURE_USART_SYNC_SCHEME_V2
  284. /** USART oversampling. */
  285. # define FEATURE_USART_OVER_SAMPLE
  286. /** USART hardware control flow. */
  287. # define FEATURE_USART_HARDWARE_FLOW_CONTROL
  288. /** IrDA mode. */
  289. # define FEATURE_USART_IRDA
  290. /** LIN slave mode. */
  291. # define FEATURE_USART_LIN_SLAVE
  292. /** USART collision detection. */
  293. # define FEATURE_USART_COLLISION_DECTION
  294. /** USART start frame detection. */
  295. # define FEATURE_USART_START_FRAME_DECTION
  296. /** USART start buffer overflow notification. */
  297. # define FEATURE_USART_IMMEDIATE_BUFFER_OVERFLOW_NOTIFICATION
  298. #endif
  299. #if (SAML22) || defined(__DOXYGEN__)
  300. /** ISO7816 for smart card interfacing. */
  301. #define FEATURE_USART_ISO7816
  302. #endif
  303. #if (SAMC20) || (SAMC21) || defined(__DOXYGEN__)
  304. /** LIN master mode. */
  305. #define FEATURE_USART_LIN_MASTER
  306. #endif
  307. #if (SAML22) || (SAMC20) || (SAMC21) || defined(__DOXYGEN__)
  308. /** RS485 mode. */
  309. # define FEATURE_USART_RS485
  310. #endif
  311. /*@}*/
  312. #ifdef FEATURE_USART_LIN_MASTER
  313. /**
  314. * \brief LIN node type
  315. *
  316. * LIN node type.
  317. */
  318. enum lin_node_type {
  319. /** LIN master mode */
  320. LIN_MASTER_NODE = SERCOM_USART_CTRLA_FORM(0x02),
  321. /** LIN slave mode */
  322. LIN_SLAVE_NODE = SERCOM_USART_CTRLA_FORM(0x04),
  323. /** Neither LIN master nor LIN slave mode */
  324. LIN_INVALID_MODE = SERCOM_USART_CTRLA_FORM(0x00),
  325. };
  326. /**
  327. * \brief LIN master command enum
  328. *
  329. * LIN master command enum.
  330. */
  331. enum lin_master_cmd {
  332. /** LIN master software control transmission command */
  333. LIN_MASTER_SOFTWARE_CONTROL_TRANSMIT_CMD = SERCOM_USART_CTRLB_LINCMD(0x01),
  334. /** LIN master automatically transmission command */
  335. LIN_MASTER_AUTO_TRANSMIT_CMD = SERCOM_USART_CTRLB_LINCMD(0x02),
  336. };
  337. /**
  338. * \brief LIN master header delay
  339. *
  340. * LIN master header delay between break and sync transmission,
  341. * and between the sync and identifier (ID) fields.
  342. * This field is only valid when using automatically transmission command
  343. */
  344. enum lin_master_header_delay {
  345. /** Delay between break and sync transmission is 1 bit time.
  346. Delay between sync and ID transmission is 1 bit time. */
  347. LIN_MASTER_HEADER_DELAY_0 = SERCOM_USART_CTRLC_HDRDLY(0x0),
  348. /** Delay between break and sync transmission is 4 bit time.
  349. Delay between sync and ID transmission is 4 bit time. */
  350. LIN_MASTER_HEADER_DELAY_1 = SERCOM_USART_CTRLC_HDRDLY(0x01),
  351. /** Delay between break and sync transmission is 8 bit time.
  352. Delay between sync and ID transmission is 4 bit time. */
  353. LIN_MASTER_HEADER_DELAY_2 = SERCOM_USART_CTRLC_HDRDLY(0x02),
  354. /** Delay between break and sync transmission is 14 bit time.
  355. Delay between sync and ID transmission is 4 bit time. */
  356. LIN_MASTER_HEADER_DELAY_3 = SERCOM_USART_CTRLC_HDRDLY(0x03),
  357. };
  358. /**
  359. * \brief LIN master break length
  360. *
  361. * Length of the break field transmitted when in LIN master mode
  362. */
  363. enum lin_master_break_length {
  364. /** Break field transmission is 13 bit times */
  365. LIN_MASTER_BREAK_LENGTH_13_BIT = SERCOM_USART_CTRLC_BRKLEN(0x0),
  366. /** Break field transmission is 17 bit times */
  367. LIN_MASTER_BREAK_LENGTH_17_BIT = SERCOM_USART_CTRLC_BRKLEN(0x1),
  368. /** Break field transmission is 21 bit times */
  369. LIN_MASTER_BREAK_LENGTH_21_BIT = SERCOM_USART_CTRLC_BRKLEN(0x2),
  370. /** Break field transmission is 26 bit times */
  371. LIN_MASTER_BREAK_LENGTH_26_BIT = SERCOM_USART_CTRLC_BRKLEN(0x3),
  372. };
  373. #endif
  374. #ifdef FEATURE_USART_ISO7816
  375. /**
  376. * \brief ISO7816 protocol type
  377. *
  378. * ISO7816 protocol type.
  379. */
  380. enum iso7816_protocol_type {
  381. /** ISO7816 protocol type 0 */
  382. ISO7816_PROTOCOL_T_0 = SERCOM_USART_CTRLA_CMODE,
  383. /** ISO7816 protocol type 1 */
  384. ISO7816_PROTOCOL_T_1 = (0x0ul << SERCOM_USART_CTRLA_CMODE_Pos),
  385. };
  386. /**
  387. * \brief ISO7816 guard time
  388. *
  389. * The value of ISO7816 guard time.
  390. */
  391. enum iso7816_guard_time {
  392. /** The guard time is 2-bit times */
  393. ISO7816_GUARD_TIME_2_BIT = 2,
  394. /** The guard time is 3-bit times */
  395. ISO7816_GUARD_TIME_3_BIT,
  396. /** The guard time is 4-bit times */
  397. ISO7816_GUARD_TIME_4_BIT,
  398. /** The guard time is 5-bit times */
  399. ISO7816_GUARD_TIME_5_BIT,
  400. /** The guard time is 6-bit times */
  401. ISO7816_GUARD_TIME_6_BIT,
  402. /** The guard time is 7-bit times */
  403. ISO7816_GUARD_TIME_7_BIT,
  404. };
  405. /**
  406. * \brief ISO7816 receive NACK inhibit
  407. *
  408. * The value of ISO7816 receive NACK inhibit.
  409. */
  410. enum iso7816_inhibit_nack {
  411. /** The NACK is generated */
  412. ISO7816_INHIBIT_NACK_DISABLE = (0x0ul << SERCOM_USART_CTRLC_INACK_Pos),
  413. /** The NACK is not generated */
  414. ISO7816_INHIBIT_NACK_ENABLE = SERCOM_USART_CTRLC_INACK,
  415. };
  416. /**
  417. * \brief ISO7816 disable successive receive NACK
  418. *
  419. * The value of ISO7816 disable successive receive NACK.
  420. */
  421. enum iso7816_successive_recv_nack {
  422. /** The successive receive NACK is enable. */
  423. ISO7816_SUCCESSIVE_RECV_NACK_DISABLE = (0x0ul << SERCOM_USART_CTRLC_INACK_Pos),
  424. /** The successive receive NACK is disable. */
  425. ISO7816_SUCCESSIVE_RECV_NACK_ENABLE = SERCOM_USART_CTRLC_DSNACK,
  426. };
  427. /**
  428. * \brief ISO7816 configuration struct
  429. *
  430. * ISO7816 configuration structure.
  431. */
  432. struct iso7816_config_t {
  433. /* ISO7816 mode enable */
  434. bool enabled;
  435. /** ISO7816 protocol type */
  436. enum iso7816_protocol_type protocol_t;
  437. /** Enable inverse transmission and reception */
  438. bool enable_inverse;
  439. /** Guard time, which lasts two bit times */
  440. enum iso7816_guard_time guard_time;
  441. /**
  442. * Inhibit Non Acknowledge:
  443. * - 0: the NACK is generated;
  444. * - 1: the NACK is not generated.
  445. */
  446. enum iso7816_inhibit_nack inhibit_nack;
  447. /**
  448. * Disable successive NACKs.
  449. * - 0: NACK is sent on the ISO line as soon as a parity error occurs
  450. * in the received character. Successive parity errors are counted up to
  451. * the value in the max_iterations field. These parity errors generate
  452. * a NACK on the ISO line. As soon as this value is reached, no additional
  453. * NACK is sent on the ISO line. The ITERATION flag is asserted.
  454. */
  455. enum iso7816_successive_recv_nack successive_recv_nack;
  456. /* Max number of repetitions */
  457. uint32_t max_iterations;
  458. };
  459. #endif
  460. #ifndef PINMUX_DEFAULT
  461. /** Default pinmux */
  462. # define PINMUX_DEFAULT 0
  463. #endif
  464. #ifndef PINMUX_UNUSED
  465. /** Unused pinmux */
  466. # define PINMUX_UNUSED 0xFFFFFFFF
  467. #endif
  468. #ifndef USART_TIMEOUT
  469. /** USART timeout value */
  470. # define USART_TIMEOUT 0xFFFF
  471. #endif
  472. #if USART_CALLBACK_MODE == true
  473. /**
  474. * \brief USART callback enum
  475. *
  476. * Callbacks for the Asynchronous USART driver.
  477. */
  478. enum usart_callback {
  479. /** Callback for buffer transmitted */
  480. USART_CALLBACK_BUFFER_TRANSMITTED,
  481. /** Callback for buffer received */
  482. USART_CALLBACK_BUFFER_RECEIVED,
  483. /** Callback for error */
  484. USART_CALLBACK_ERROR,
  485. #ifdef FEATURE_USART_LIN_SLAVE
  486. /** Callback for break character is received */
  487. USART_CALLBACK_BREAK_RECEIVED,
  488. #endif
  489. #ifdef FEATURE_USART_HARDWARE_FLOW_CONTROL
  490. /** Callback for a change is detected on the CTS pin */
  491. USART_CALLBACK_CTS_INPUT_CHANGE,
  492. #endif
  493. #ifdef FEATURE_USART_START_FRAME_DECTION
  494. /** Callback for a start condition is detected on the RxD line */
  495. USART_CALLBACK_START_RECEIVED,
  496. #endif
  497. # if !defined(__DOXYGEN__)
  498. /** Number of available callbacks */
  499. USART_CALLBACK_N,
  500. # endif
  501. };
  502. #endif
  503. /**
  504. * \brief USART Data Order enum
  505. *
  506. * The data order decides which MSB or LSB is shifted out first when data is
  507. * transferred.
  508. */
  509. enum usart_dataorder {
  510. /** The MSB will be shifted out first during transmission,
  511. * and shifted in first during reception */
  512. USART_DATAORDER_MSB = 0,
  513. /** The LSB will be shifted out first during transmission,
  514. * and shifted in first during reception */
  515. USART_DATAORDER_LSB = SERCOM_USART_CTRLA_DORD,
  516. };
  517. /**
  518. * \brief USART Transfer mode enum
  519. *
  520. * Select USART transfer mode.
  521. */
  522. enum usart_transfer_mode {
  523. /** Transfer of data is done synchronously */
  524. USART_TRANSFER_SYNCHRONOUSLY = (SERCOM_USART_CTRLA_CMODE),
  525. /** Transfer of data is done asynchronously */
  526. USART_TRANSFER_ASYNCHRONOUSLY = (0x0ul << SERCOM_USART_CTRLA_CMODE_Pos),
  527. };
  528. /**
  529. * \brief USART Parity enum
  530. *
  531. * Select parity USART parity mode.
  532. */
  533. enum usart_parity {
  534. /** For odd parity checking, the parity bit will be set if number of
  535. * ones being transferred is even */
  536. USART_PARITY_ODD = SERCOM_USART_CTRLB_PMODE,
  537. /** For even parity checking, the parity bit will be set if number of
  538. * ones being received is odd */
  539. USART_PARITY_EVEN = 0,
  540. /** No parity checking will be executed, and there will be no parity bit
  541. * in the received frame */
  542. USART_PARITY_NONE = 0xFF,
  543. };
  544. /**
  545. * \brief USART signal MUX settings
  546. *
  547. * Set the functionality of the SERCOM pins.
  548. *
  549. * See \ref asfdoc_sam0_sercom_usart_mux_settings for a description of the
  550. * various MUX setting options.
  551. */
  552. enum usart_signal_mux_settings {
  553. #ifdef FEATURE_USART_HARDWARE_FLOW_CONTROL
  554. /** MUX setting RX_0_TX_0_XCK_1 */
  555. USART_RX_0_TX_0_XCK_1 = (SERCOM_USART_CTRLA_RXPO(0) | SERCOM_USART_CTRLA_TXPO(0)),
  556. /** MUX setting RX_0_TX_2_XCK_3 */
  557. USART_RX_0_TX_2_XCK_3 = (SERCOM_USART_CTRLA_RXPO(0) | SERCOM_USART_CTRLA_TXPO(1)),
  558. /** MUX setting USART_RX_0_TX_0_RTS_2_CTS_3 */
  559. USART_RX_0_TX_0_RTS_2_CTS_3 = (SERCOM_USART_CTRLA_RXPO(0) | SERCOM_USART_CTRLA_TXPO(2)),
  560. /** MUX setting RX_1_TX_0_XCK_1 */
  561. USART_RX_1_TX_0_XCK_1 = (SERCOM_USART_CTRLA_RXPO(1) | SERCOM_USART_CTRLA_TXPO(0)),
  562. /** MUX setting RX_1_TX_2_XCK_3 */
  563. USART_RX_1_TX_2_XCK_3 = (SERCOM_USART_CTRLA_RXPO(1) | SERCOM_USART_CTRLA_TXPO(1)),
  564. /** MUX setting USART_RX_1_TX_0_RTS_2_CTS_3 */
  565. USART_RX_1_TX_0_RTS_2_CTS_3 = (SERCOM_USART_CTRLA_RXPO(1) | SERCOM_USART_CTRLA_TXPO(2)),
  566. /** MUX setting RX_2_TX_0_XCK_1 */
  567. USART_RX_2_TX_0_XCK_1 = (SERCOM_USART_CTRLA_RXPO(2) | SERCOM_USART_CTRLA_TXPO(0)),
  568. /** MUX setting RX_2_TX_2_XCK_3 */
  569. USART_RX_2_TX_2_XCK_3 = (SERCOM_USART_CTRLA_RXPO(2) | SERCOM_USART_CTRLA_TXPO(1)),
  570. /** MUX setting USART_RX_2_TX_0_RTS_2_CTS_3 */
  571. USART_RX_2_TX_0_RTS_2_CTS_3 = (SERCOM_USART_CTRLA_RXPO(2) | SERCOM_USART_CTRLA_TXPO(2)),
  572. /** MUX setting RX_3_TX_0_XCK_1 */
  573. USART_RX_3_TX_0_XCK_1 = (SERCOM_USART_CTRLA_RXPO(3) | SERCOM_USART_CTRLA_TXPO(0)),
  574. /** MUX setting RX_3_TX_2_XCK_3 */
  575. USART_RX_3_TX_2_XCK_3 = (SERCOM_USART_CTRLA_RXPO(3) | SERCOM_USART_CTRLA_TXPO(1)),
  576. /** MUX setting USART_RX_3_TX_0_RTS_2_CTS_3 */
  577. USART_RX_3_TX_0_RTS_2_CTS_3 = (SERCOM_USART_CTRLA_RXPO(3) | SERCOM_USART_CTRLA_TXPO(2)),
  578. #ifdef FEATURE_USART_RS485
  579. /** MUX setting USART_RX_0_TX_0_XCK_1_TE_2 */
  580. USART_RX_0_TX_0_XCK_1_TE_2 = (SERCOM_USART_CTRLA_RXPO(0) | SERCOM_USART_CTRLA_TXPO(3)),
  581. /** MUX setting USART_RX_1_TX_0_XCK_1_TE_2 */
  582. USART_RX_1_TX_0_XCK_1_TE_2 = (SERCOM_USART_CTRLA_RXPO(1) | SERCOM_USART_CTRLA_TXPO(3)),
  583. /** MUX setting USART_RX_2_TX_0_XCK_1_TE_2 */
  584. USART_RX_2_TX_0_XCK_1_TE_2 = (SERCOM_USART_CTRLA_RXPO(2) | SERCOM_USART_CTRLA_TXPO(3)),
  585. /** MUX setting USART_RX_3_TX_0_XCK_1_TE_2 */
  586. USART_RX_3_TX_0_XCK_1_TE_2 = (SERCOM_USART_CTRLA_RXPO(3) | SERCOM_USART_CTRLA_TXPO(3)),
  587. #endif
  588. #else
  589. /** MUX setting RX_0_TX_0_XCK_1 */
  590. USART_RX_0_TX_0_XCK_1 = (SERCOM_USART_CTRLA_RXPO(0)),
  591. /** MUX setting RX_0_TX_2_XCK_3 */
  592. USART_RX_0_TX_2_XCK_3 = (SERCOM_USART_CTRLA_RXPO(0) | SERCOM_USART_CTRLA_TXPO),
  593. /** MUX setting RX_1_TX_0_XCK_1 */
  594. USART_RX_1_TX_0_XCK_1 = (SERCOM_USART_CTRLA_RXPO(1)),
  595. /** MUX setting RX_1_TX_2_XCK_3 */
  596. USART_RX_1_TX_2_XCK_3 = (SERCOM_USART_CTRLA_RXPO(1) | SERCOM_USART_CTRLA_TXPO),
  597. /** MUX setting RX_2_TX_0_XCK_1 */
  598. USART_RX_2_TX_0_XCK_1 = (SERCOM_USART_CTRLA_RXPO(2)),
  599. /** MUX setting RX_2_TX_2_XCK_3 */
  600. USART_RX_2_TX_2_XCK_3 = (SERCOM_USART_CTRLA_RXPO(2) | SERCOM_USART_CTRLA_TXPO),
  601. /** MUX setting RX_3_TX_0_XCK_1 */
  602. USART_RX_3_TX_0_XCK_1 = (SERCOM_USART_CTRLA_RXPO(3)),
  603. /** MUX setting RX_3_TX_2_XCK_3 */
  604. USART_RX_3_TX_2_XCK_3 = (SERCOM_USART_CTRLA_RXPO(3) | SERCOM_USART_CTRLA_TXPO),
  605. #endif
  606. };
  607. /**
  608. * \brief USART Stop Bits enum
  609. *
  610. * Number of stop bits for a frame.
  611. */
  612. enum usart_stopbits {
  613. /** Each transferred frame contains one stop bit */
  614. USART_STOPBITS_1 = 0,
  615. /** Each transferred frame contains two stop bits */
  616. USART_STOPBITS_2 = SERCOM_USART_CTRLB_SBMODE,
  617. };
  618. /**
  619. * \brief USART Character Size
  620. *
  621. * Number of bits for the character sent in a frame.
  622. */
  623. enum usart_character_size {
  624. /** The char being sent in a frame is five bits long */
  625. USART_CHARACTER_SIZE_5BIT = SERCOM_USART_CTRLB_CHSIZE(5),
  626. /** The char being sent in a frame is six bits long */
  627. USART_CHARACTER_SIZE_6BIT = SERCOM_USART_CTRLB_CHSIZE(6),
  628. /** The char being sent in a frame is seven bits long */
  629. USART_CHARACTER_SIZE_7BIT = SERCOM_USART_CTRLB_CHSIZE(7),
  630. /** The char being sent in a frame is eight bits long */
  631. USART_CHARACTER_SIZE_8BIT = SERCOM_USART_CTRLB_CHSIZE(0),
  632. /** The char being sent in a frame is nine bits long */
  633. USART_CHARACTER_SIZE_9BIT = SERCOM_USART_CTRLB_CHSIZE(1),
  634. };
  635. #ifdef FEATURE_USART_OVER_SAMPLE
  636. /**
  637. * \brief USART Sample Rate
  638. *
  639. * The value of sample rate and baudrate generation mode.
  640. */
  641. enum usart_sample_rate {
  642. /** 16x over-sampling using arithmetic baudrate generation */
  643. USART_SAMPLE_RATE_16X_ARITHMETIC = SERCOM_USART_CTRLA_SAMPR(0),
  644. /** 16x over-sampling using fractional baudrate generation */
  645. USART_SAMPLE_RATE_16X_FRACTIONAL = SERCOM_USART_CTRLA_SAMPR(1),
  646. /** 8x over-sampling using arithmetic baudrate generation */
  647. USART_SAMPLE_RATE_8X_ARITHMETIC = SERCOM_USART_CTRLA_SAMPR(2),
  648. /** 8x over-sampling using fractional baudrate generation */
  649. USART_SAMPLE_RATE_8X_FRACTIONAL = SERCOM_USART_CTRLA_SAMPR(3),
  650. /** 3x over-sampling using arithmetic baudrate generation */
  651. USART_SAMPLE_RATE_3X_ARITHMETIC = SERCOM_USART_CTRLA_SAMPR(4),
  652. };
  653. /**
  654. * \brief USART Sample Adjustment
  655. *
  656. * The value of sample number used for majority voting.
  657. */
  658. enum usart_sample_adjustment {
  659. /** The first, middle and last sample number used for majority voting is 7-8-9 */
  660. USART_SAMPLE_ADJUSTMENT_7_8_9 = SERCOM_USART_CTRLA_SAMPA(0),
  661. /** The first, middle and last sample number used for majority voting is 9-10-11 */
  662. USART_SAMPLE_ADJUSTMENT_9_10_11 = SERCOM_USART_CTRLA_SAMPA(1),
  663. /** The first, middle and last sample number used for majority voting is 11-12-13 */
  664. USART_SAMPLE_ADJUSTMENT_11_12_13 = SERCOM_USART_CTRLA_SAMPA(2),
  665. /** The first, middle and last sample number used for majority voting is 13-14-15 */
  666. USART_SAMPLE_ADJUSTMENT_13_14_15 = SERCOM_USART_CTRLA_SAMPA(3),
  667. };
  668. #endif
  669. #ifdef FEATURE_USART_RS485
  670. /**
  671. * \brief RS485 Guard Time
  672. *
  673. * The value of RS485 guard time.
  674. */
  675. enum rs485_guard_time {
  676. /** The guard time is 0-bit time */
  677. RS485_GUARD_TIME_0_BIT = 0,
  678. /** The guard time is 1-bit time */
  679. RS485_GUARD_TIME_1_BIT,
  680. /** The guard time is 2-bit times */
  681. RS485_GUARD_TIME_2_BIT,
  682. /** The guard time is 3-bit times */
  683. RS485_GUARD_TIME_3_BIT,
  684. /** The guard time is 4-bit times */
  685. RS485_GUARD_TIME_4_BIT,
  686. /** The guard time is 5-bit times */
  687. RS485_GUARD_TIME_5_BIT,
  688. /** The guard time is 6-bit times */
  689. RS485_GUARD_TIME_6_BIT,
  690. /** The guard time is 7-bit times */
  691. RS485_GUARD_TIME_7_BIT,
  692. };
  693. #endif
  694. /**
  695. * \brief USART Transceiver
  696. *
  697. * Select Receiver or Transmitter.
  698. */
  699. enum usart_transceiver_type {
  700. /** The parameter is for the Receiver */
  701. USART_TRANSCEIVER_RX,
  702. /** The parameter is for the Transmitter */
  703. USART_TRANSCEIVER_TX,
  704. };
  705. /**
  706. * \brief USART configuration struct
  707. *
  708. * Configuration options for USART.
  709. */
  710. struct usart_config {
  711. /** USART bit order (MSB or LSB first) */
  712. enum usart_dataorder data_order;
  713. /** USART in asynchronous or synchronous mode */
  714. enum usart_transfer_mode transfer_mode;
  715. /** USART parity */
  716. enum usart_parity parity;
  717. /** Number of stop bits */
  718. enum usart_stopbits stopbits;
  719. /** USART character size */
  720. enum usart_character_size character_size;
  721. /** USART pin out */
  722. enum usart_signal_mux_settings mux_setting;
  723. #ifdef FEATURE_USART_OVER_SAMPLE
  724. /** USART sample rate */
  725. enum usart_sample_rate sample_rate;
  726. /** USART sample adjustment */
  727. enum usart_sample_adjustment sample_adjustment;
  728. #endif
  729. #ifdef FEATURE_USART_IMMEDIATE_BUFFER_OVERFLOW_NOTIFICATION
  730. /** Controls when the buffer overflow status bit is asserted when a buffer overflow occurs */
  731. bool immediate_buffer_overflow_notification;
  732. #endif
  733. #ifdef FEATURE_USART_IRDA
  734. /** Enable IrDA encoding format */
  735. bool encoding_format_enable;
  736. /** The minimum pulse length required for a pulse to be accepted by the IrDA receiver */
  737. uint8_t receive_pulse_length;
  738. #endif
  739. #ifdef FEATURE_USART_LIN_SLAVE
  740. /** Enable LIN Slave Support */
  741. bool lin_slave_enable;
  742. #endif
  743. #ifdef FEATURE_USART_LIN_MASTER
  744. /** LIN node type */
  745. enum lin_node_type lin_node;
  746. /** LIN master header delay */
  747. enum lin_master_header_delay lin_header_delay;
  748. /** LIN Master Break Length */
  749. enum lin_master_break_length lin_break_length;
  750. #endif
  751. #ifdef FEATURE_USART_START_FRAME_DECTION
  752. /** Enable start of frame dection */
  753. bool start_frame_detection_enable;
  754. #endif
  755. #ifdef FEATURE_USART_ISO7816
  756. /** Enable ISO7816 for smart card interfacing */
  757. struct iso7816_config_t iso7816_config;
  758. #endif
  759. #ifdef FEATURE_USART_RS485
  760. /** RS485 guard time */
  761. enum rs485_guard_time rs485_guard_time;
  762. #endif
  763. #ifdef FEATURE_USART_COLLISION_DECTION
  764. /** Enable collision dection */
  765. bool collision_detection_enable;
  766. #endif
  767. /** USART baudrate */
  768. uint32_t baudrate;
  769. /** Enable receiver */
  770. bool receiver_enable;
  771. /** Enable transmitter */
  772. bool transmitter_enable;
  773. /** USART Clock Polarity.
  774. * If true, data changes on falling XCK edge and
  775. * is sampled at rising edge.
  776. * If false, data changes on rising XCK edge and
  777. * is sampled at falling edge.
  778. * */
  779. bool clock_polarity_inverted;
  780. /** States whether to use the external clock applied to the XCK pin.
  781. * In synchronous mode the shift register will act directly on the XCK clock.
  782. * In asynchronous mode the XCK will be the input to the USART hardware module.
  783. */
  784. bool use_external_clock;
  785. /** External clock frequency in synchronous mode.
  786. * This must be set if \c use_external_clock is true. */
  787. uint32_t ext_clock_freq;
  788. /** If true the USART will be kept running in Standby sleep mode */
  789. bool run_in_standby;
  790. /** GCLK generator source */
  791. enum gclk_generator generator_source;
  792. /** PAD0 pinmux.
  793. *
  794. * If current USARTx has several alternative multiplexing I/O pins for PAD0, then
  795. * only one peripheral multiplexing I/O can be enabled for current USARTx PAD0
  796. * function. Make sure that no other alternative multiplexing I/O is associated
  797. * with the same USARTx PAD0.
  798. */
  799. uint32_t pinmux_pad0;
  800. /** PAD1 pinmux.
  801. *
  802. * If current USARTx has several alternative multiplexing I/O pins for PAD1, then
  803. * only one peripheral multiplexing I/O can be enabled for current USARTx PAD1
  804. * function. Make sure that no other alternative multiplexing I/O is associated
  805. * with the same USARTx PAD1.
  806. */
  807. uint32_t pinmux_pad1;
  808. /** PAD2 pinmux.
  809. *
  810. * If current USARTx has several alternative multiplexing I/O pins for PAD2, then
  811. * only one peripheral multiplexing I/O can be enabled for current USARTx PAD2
  812. * function. Make sure that no other alternative multiplexing I/O is associated
  813. * with the same USARTx PAD2.
  814. */
  815. uint32_t pinmux_pad2;
  816. /** PAD3 pinmux.
  817. *
  818. * If current USARTx has several alternative multiplexing I/O pins for PAD3, then
  819. * only one peripheral multiplexing I/O can be enabled for current USARTx PAD3
  820. * function. Make sure that no other alternative multiplexing I/O is associated
  821. * with the same USARTx PAD3.
  822. */
  823. uint32_t pinmux_pad3;
  824. };
  825. #if USART_CALLBACK_MODE == true
  826. /**
  827. * \brief USART module instance
  828. *
  829. * Forward Declaration for the device instance.
  830. */
  831. struct usart_module;
  832. /**
  833. * \brief USART callback type
  834. *
  835. * Type of the callback functions.
  836. */
  837. typedef void (*usart_callback_t)(struct usart_module *const module);
  838. #endif
  839. /**
  840. * \brief SERCOM USART driver software device instance structure.
  841. *
  842. * SERCOM USART driver software instance structure, used to retain software
  843. * state information of an associated hardware module instance.
  844. *
  845. * \note The fields of this structure should not be altered by the user
  846. * application; they are reserved for module-internal use only.
  847. */
  848. struct usart_module {
  849. #if !defined(__DOXYGEN__)
  850. /** Pointer to the hardware instance */
  851. Sercom *hw;
  852. /** Module lock */
  853. volatile bool locked;
  854. /** Character size of the data being transferred */
  855. enum usart_character_size character_size;
  856. /** Receiver enabled */
  857. bool receiver_enabled;
  858. /** Transmitter enabled */
  859. bool transmitter_enabled;
  860. #ifdef FEATURE_USART_LIN_SLAVE
  861. /** LIN Slave Support enabled */
  862. bool lin_slave_enabled;
  863. #endif
  864. #ifdef FEATURE_USART_START_FRAME_DECTION
  865. /** Start of frame dection enabled */
  866. bool start_frame_detection_enabled;
  867. #endif
  868. #ifdef FEATURE_USART_ISO7816
  869. /** ISO7816 mode enable */
  870. bool iso7816_mode_enabled;
  871. #endif
  872. # if USART_CALLBACK_MODE == true
  873. /** Array to store callback function pointers in */
  874. usart_callback_t callback[USART_CALLBACK_N];
  875. /** Buffer pointer to where the next received character will be put */
  876. volatile uint8_t *rx_buffer_ptr;
  877. /** Buffer pointer to where the next character will be transmitted from
  878. **/
  879. volatile uint8_t *tx_buffer_ptr;
  880. /** Remaining characters to receive */
  881. volatile uint16_t remaining_rx_buffer_length;
  882. /** Remaining characters to transmit */
  883. volatile uint16_t remaining_tx_buffer_length;
  884. /** Bit mask for callbacks registered */
  885. uint8_t callback_reg_mask;
  886. /** Bit mask for callbacks enabled */
  887. uint8_t callback_enable_mask;
  888. /** Holds the status of the ongoing or last read operation */
  889. volatile enum status_code rx_status;
  890. /** Holds the status of the ongoing or last write operation */
  891. volatile enum status_code tx_status;
  892. # endif
  893. #endif
  894. };
  895. /**
  896. * \name Lock/Unlock
  897. * @{
  898. */
  899. /**
  900. * \brief Attempt to get lock on driver instance
  901. *
  902. * This function checks the instance's lock, which indicates whether or not it
  903. * is currently in use, and sets the lock if it was not already set.
  904. *
  905. * The purpose of this is to enable exclusive access to driver instances, so
  906. * that, e.g., transactions by different services will not interfere with each
  907. * other.
  908. *
  909. * \param[in,out] module Pointer to the driver instance to lock
  910. *
  911. * \retval STATUS_OK If the module was locked
  912. * \retval STATUS_BUSY If the module was already locked
  913. */
  914. static inline enum status_code usart_lock(
  915. struct usart_module *const module)
  916. {
  917. enum status_code status;
  918. system_interrupt_enter_critical_section();
  919. if (module->locked) {
  920. status = STATUS_BUSY;
  921. } else {
  922. module->locked = true;
  923. status = STATUS_OK;
  924. }
  925. system_interrupt_leave_critical_section();
  926. return status;
  927. }
  928. /**
  929. * \brief Unlock driver instance
  930. *
  931. * This function clears the instance lock, indicating that it is available for
  932. * use.
  933. *
  934. * \param[in,out] module Pointer to the driver instance to lock
  935. *
  936. */
  937. static inline void usart_unlock(struct usart_module *const module)
  938. {
  939. module->locked = false;
  940. }
  941. /** @} */
  942. /**
  943. * \brief Check if peripheral is busy syncing registers across clock domains
  944. *
  945. * Return peripheral synchronization status. If doing a non-blocking
  946. * implementation this function can be used to check the sync state and hold of
  947. * any new actions until sync is complete. If this function is not run; the
  948. * functions will block until the sync has completed.
  949. *
  950. * \param[in] module Pointer to peripheral module
  951. *
  952. * \return Peripheral sync status.
  953. *
  954. * \retval true Peripheral is busy syncing
  955. * \retval false Peripheral is not busy syncing and can be read/written without
  956. * stalling the bus
  957. */
  958. static inline bool usart_is_syncing(
  959. const struct usart_module *const module)
  960. {
  961. /* Sanity check arguments */
  962. Assert(module);
  963. Assert(module->hw);
  964. SercomUsart *const usart_hw = &(module->hw->USART);
  965. #ifdef FEATURE_USART_SYNC_SCHEME_V2
  966. return (usart_hw->SYNCBUSY.reg);
  967. #else
  968. return (usart_hw->STATUS.reg & SERCOM_USART_STATUS_SYNCBUSY);
  969. #endif
  970. }
  971. #if !defined (__DOXYGEN__)
  972. /**
  973. * \internal
  974. * Waits until synchronization is complete
  975. */
  976. static inline void _usart_wait_for_sync(
  977. const struct usart_module *const module)
  978. {
  979. /* Sanity check */
  980. Assert(module);
  981. while (usart_is_syncing(module)) {
  982. /* Wait until the synchronization is complete */
  983. }
  984. }
  985. #endif
  986. /**
  987. * \brief Initializes the device to predefined defaults
  988. *
  989. * Initialize the USART device to predefined defaults:
  990. * - 8-bit asynchronous USART
  991. * - No parity
  992. * - One stop bit
  993. * - 9600 baud
  994. * - Transmitter enabled
  995. * - Receiver enabled
  996. * - GCLK generator 0 as clock source
  997. * - Default pin configuration
  998. *
  999. * The configuration struct will be updated with the default
  1000. * configuration.
  1001. *
  1002. * \param[in,out] config Pointer to configuration struct
  1003. */
  1004. static inline void usart_get_config_defaults(
  1005. struct usart_config *const config)
  1006. {
  1007. /* Sanity check arguments */
  1008. Assert(config);
  1009. /* Set default config in the config struct */
  1010. config->data_order = USART_DATAORDER_LSB;
  1011. config->transfer_mode = USART_TRANSFER_ASYNCHRONOUSLY;
  1012. config->parity = USART_PARITY_NONE;
  1013. config->stopbits = USART_STOPBITS_1;
  1014. config->character_size = USART_CHARACTER_SIZE_8BIT;
  1015. config->baudrate = 9600;
  1016. config->receiver_enable = true;
  1017. config->transmitter_enable = true;
  1018. config->clock_polarity_inverted = false;
  1019. config->use_external_clock = false;
  1020. config->ext_clock_freq = 0;
  1021. config->mux_setting = USART_RX_1_TX_2_XCK_3;
  1022. config->run_in_standby = false;
  1023. config->generator_source = GCLK_GENERATOR_0;
  1024. config->pinmux_pad0 = PINMUX_DEFAULT;
  1025. config->pinmux_pad1 = PINMUX_DEFAULT;
  1026. config->pinmux_pad2 = PINMUX_DEFAULT;
  1027. config->pinmux_pad3 = PINMUX_DEFAULT;
  1028. #ifdef FEATURE_USART_OVER_SAMPLE
  1029. config->sample_adjustment = USART_SAMPLE_ADJUSTMENT_7_8_9;
  1030. config->sample_rate = USART_SAMPLE_RATE_16X_ARITHMETIC;
  1031. #endif
  1032. #ifdef FEATURE_USART_LIN_SLAVE
  1033. config->lin_slave_enable = false;
  1034. #endif
  1035. #ifdef FEATURE_USART_LIN_MASTER
  1036. config->lin_node = LIN_INVALID_MODE;
  1037. config->lin_header_delay = LIN_MASTER_HEADER_DELAY_0;
  1038. config->lin_break_length = LIN_MASTER_BREAK_LENGTH_13_BIT;
  1039. #endif
  1040. #ifdef FEATURE_USART_IMMEDIATE_BUFFER_OVERFLOW_NOTIFICATION
  1041. config->immediate_buffer_overflow_notification = false;
  1042. #endif
  1043. #ifdef FEATURE_USART_START_FRAME_DECTION
  1044. config->start_frame_detection_enable = false;
  1045. #endif
  1046. #ifdef FEATURE_USART_IRDA
  1047. config->encoding_format_enable = false;
  1048. config->receive_pulse_length = 19;
  1049. #endif
  1050. #ifdef FEATURE_USART_ISO7816
  1051. config->iso7816_config.enabled = false;
  1052. config->iso7816_config.guard_time = ISO7816_GUARD_TIME_2_BIT;
  1053. config->iso7816_config.protocol_t = ISO7816_PROTOCOL_T_0;
  1054. config->iso7816_config.enable_inverse = false;
  1055. config->iso7816_config.inhibit_nack = ISO7816_INHIBIT_NACK_DISABLE;
  1056. config->iso7816_config.successive_recv_nack = ISO7816_SUCCESSIVE_RECV_NACK_DISABLE;
  1057. config->iso7816_config.max_iterations = 7;
  1058. #endif
  1059. #ifdef FEATURE_USART_COLLISION_DECTION
  1060. config->collision_detection_enable = false;
  1061. #endif
  1062. #ifdef FEATURE_USART_RS485
  1063. config->rs485_guard_time = RS485_GUARD_TIME_0_BIT;
  1064. #endif
  1065. }
  1066. enum status_code usart_init(
  1067. struct usart_module *const module,
  1068. Sercom *const hw,
  1069. const struct usart_config *const config);
  1070. /**
  1071. * \brief Enable the module
  1072. *
  1073. * Enables the USART module.
  1074. *
  1075. * \param[in] module Pointer to USART software instance struct
  1076. */
  1077. static inline void usart_enable(
  1078. const struct usart_module *const module)
  1079. {
  1080. /* Sanity check arguments */
  1081. Assert(module);
  1082. Assert(module->hw);
  1083. /* Get a pointer to the hardware module instance */
  1084. SercomUsart *const usart_hw = &(module->hw->USART);
  1085. #if USART_CALLBACK_MODE == true
  1086. /* Enable Global interrupt for module */
  1087. system_interrupt_enable(_sercom_get_interrupt_vector(module->hw));
  1088. #endif
  1089. /* Wait until synchronization is complete */
  1090. _usart_wait_for_sync(module);
  1091. /* Enable USART module */
  1092. usart_hw->CTRLA.reg |= SERCOM_USART_CTRLA_ENABLE;
  1093. }
  1094. /**
  1095. * \brief Disable module
  1096. *
  1097. * Disables the USART module.
  1098. *
  1099. * \param[in] module Pointer to USART software instance struct
  1100. */
  1101. static inline void usart_disable(
  1102. const struct usart_module *const module)
  1103. {
  1104. /* Sanity check arguments */
  1105. Assert(module);
  1106. Assert(module->hw);
  1107. /* Get a pointer to the hardware module instance */
  1108. SercomUsart *const usart_hw = &(module->hw->USART);
  1109. #if USART_CALLBACK_MODE == true
  1110. /* Disable Global interrupt for module */
  1111. system_interrupt_disable(_sercom_get_interrupt_vector(module->hw));
  1112. #endif
  1113. /* Wait until synchronization is complete */
  1114. _usart_wait_for_sync(module);
  1115. /* Disable USART module */
  1116. usart_hw->CTRLA.reg &= ~SERCOM_USART_CTRLA_ENABLE;
  1117. }
  1118. /**
  1119. * \brief Resets the USART module
  1120. *
  1121. * Disables and resets the USART module.
  1122. *
  1123. * \param[in] module Pointer to the USART software instance struct
  1124. */
  1125. static inline void usart_reset(
  1126. const struct usart_module *const module)
  1127. {
  1128. /* Sanity check arguments */
  1129. Assert(module);
  1130. Assert(module->hw);
  1131. /* Get a pointer to the hardware module instance */
  1132. SercomUsart *const usart_hw = &(module->hw->USART);
  1133. usart_disable(module);
  1134. /* Wait until synchronization is complete */
  1135. _usart_wait_for_sync(module);
  1136. /* Reset module */
  1137. usart_hw->CTRLA.reg = SERCOM_USART_CTRLA_SWRST;
  1138. }
  1139. /**
  1140. * \name Writing and Reading
  1141. * @{
  1142. */
  1143. enum status_code usart_write_wait(
  1144. struct usart_module *const module,
  1145. const uint16_t tx_data);
  1146. enum status_code usart_read_wait(
  1147. struct usart_module *const module,
  1148. uint16_t *const rx_data);
  1149. enum status_code usart_write_buffer_wait(
  1150. struct usart_module *const module,
  1151. const uint8_t *tx_data,
  1152. uint16_t length);
  1153. enum status_code usart_read_buffer_wait(
  1154. struct usart_module *const module,
  1155. uint8_t *rx_data,
  1156. uint16_t length);
  1157. /** @} */
  1158. /**
  1159. * \name Enabling/Disabling Receiver and Transmitter
  1160. * @{
  1161. */
  1162. /**
  1163. * \brief Enable Transceiver
  1164. *
  1165. * Enable the given transceiver. Either RX or TX.
  1166. *
  1167. * \param[in] module Pointer to USART software instance struct
  1168. * \param[in] transceiver_type Transceiver type
  1169. */
  1170. static inline void usart_enable_transceiver(
  1171. struct usart_module *const module,
  1172. enum usart_transceiver_type transceiver_type)
  1173. {
  1174. /* Sanity check arguments */
  1175. Assert(module);
  1176. Assert(module->hw);
  1177. /* Get a pointer to the hardware module instance */
  1178. SercomUsart *const usart_hw = &(module->hw->USART);
  1179. /* Wait until synchronization is complete */
  1180. _usart_wait_for_sync(module);
  1181. switch (transceiver_type) {
  1182. case USART_TRANSCEIVER_RX:
  1183. /* Enable RX */
  1184. usart_hw->CTRLB.reg |= SERCOM_USART_CTRLB_RXEN;
  1185. module->receiver_enabled = true;
  1186. break;
  1187. case USART_TRANSCEIVER_TX:
  1188. /* Enable TX */
  1189. usart_hw->CTRLB.reg |= SERCOM_USART_CTRLB_TXEN;
  1190. module->transmitter_enabled = true;
  1191. break;
  1192. }
  1193. _usart_wait_for_sync(module);
  1194. }
  1195. /**
  1196. * \brief Disable Transceiver
  1197. *
  1198. * Disable the given transceiver (RX or TX).
  1199. *
  1200. * \param[in] module Pointer to USART software instance struct
  1201. * \param[in] transceiver_type Transceiver type
  1202. */
  1203. static inline void usart_disable_transceiver(
  1204. struct usart_module *const module,
  1205. enum usart_transceiver_type transceiver_type)
  1206. {
  1207. /* Sanity check arguments */
  1208. Assert(module);
  1209. Assert(module->hw);
  1210. /* Get a pointer to the hardware module instance */
  1211. SercomUsart *const usart_hw = &(module->hw->USART);
  1212. /* Wait until synchronization is complete */
  1213. _usart_wait_for_sync(module);
  1214. switch (transceiver_type) {
  1215. case USART_TRANSCEIVER_RX:
  1216. /* Disable RX */
  1217. usart_hw->CTRLB.reg &= ~SERCOM_USART_CTRLB_RXEN;
  1218. module->receiver_enabled = false;
  1219. break;
  1220. case USART_TRANSCEIVER_TX:
  1221. /* Disable TX */
  1222. usart_hw->CTRLB.reg &= ~SERCOM_USART_CTRLB_TXEN;
  1223. module->transmitter_enabled = false;
  1224. break;
  1225. }
  1226. }
  1227. /** @} */
  1228. #ifdef FEATURE_USART_LIN_MASTER
  1229. /**
  1230. * \name LIN Master Command and Status
  1231. * @{
  1232. */
  1233. /**
  1234. * \brief Sending LIN command.
  1235. *
  1236. * Sending LIN command.
  1237. *
  1238. * \param[in] module Pointer to USART software instance struct
  1239. * \param[in] cmd Cammand type
  1240. */
  1241. static inline void lin_master_send_cmd(
  1242. struct usart_module *const module,
  1243. enum lin_master_cmd cmd)
  1244. {
  1245. SercomUsart *const usart_hw = &(module->hw->USART);
  1246. _usart_wait_for_sync(module);
  1247. usart_hw->CTRLB.reg |= cmd;
  1248. }
  1249. /**
  1250. * \brief Get LIN transmission status
  1251. *
  1252. * Get LIN transmission status.
  1253. *
  1254. * \param[in] module Pointer to USART software instance struct
  1255. *
  1256. * \return Status of LIN master transmission.
  1257. * \retval true Data transmission completed
  1258. * \retval false Transmission is ongoing
  1259. */
  1260. static inline bool lin_master_transmission_status(struct usart_module *const module)
  1261. {
  1262. SercomUsart *const usart_hw = &(module->hw->USART);
  1263. return ((usart_hw->STATUS.reg & SERCOM_USART_STATUS_TXE)? true:false);
  1264. }
  1265. /** @} */
  1266. #endif
  1267. #ifdef __cplusplus
  1268. }
  1269. #endif
  1270. /** @} */
  1271. /**
  1272. * \page asfdoc_sam0_sercom_usart_extra Extra Information for SERCOM USART Driver
  1273. *
  1274. * \section asfdoc_sam0_sercom_usart_extra_acronyms Acronyms
  1275. *
  1276. * Below is a table listing the acronyms used in this module, along with their
  1277. * intended meanings.
  1278. *
  1279. * <table>
  1280. * <tr>
  1281. * <th>Acronym</th>
  1282. * <th>Description</th>
  1283. * </tr>
  1284. * <tr>
  1285. * <td>SERCOM</td>
  1286. * <td>Serial Communication Interface</td>
  1287. * </tr>
  1288. * <tr>
  1289. * <td>USART</td>
  1290. * <td>Universal Synchronous and Asynchronous Serial Receiver and Transmitter</td>
  1291. * </tr>
  1292. * <tr>
  1293. * <td>LSB</td>
  1294. * <td>Least Significant Bit</td>
  1295. * </tr>
  1296. * <tr>
  1297. * <td>MSB</td>
  1298. * <td>Most Significant Bit</td>
  1299. * </tr>
  1300. * <tr>
  1301. * <td>DMA</td>
  1302. * <td>Direct Memory Access</td>
  1303. * </tr>
  1304. * </table>
  1305. *
  1306. *
  1307. * \section asfdoc_sam0_sercom_usart_extra_dependencies Dependencies
  1308. * This driver has the following dependencies:
  1309. *
  1310. * - \ref asfdoc_sam0_system_pinmux_group "System Pin Multiplexer Driver"
  1311. * - \ref asfdoc_sam0_system_clock_group "System clock configuration"
  1312. *
  1313. *
  1314. * \section asfdoc_sam0_sercom_usart_extra_errata Errata
  1315. * There are no errata related to this driver.
  1316. *
  1317. *
  1318. * \section asfdoc_sam0_sercom_usart_extra_history Module History
  1319. * An overview of the module history is presented in the table below, with
  1320. * details on the enhancements and fixes made to the module since its first
  1321. * release. The current version of this corresponds to the newest version in
  1322. * the table.
  1323. *
  1324. * <table>
  1325. * <tr>
  1326. * <th>Changelog</th>
  1327. * </tr>
  1328. * <tr>
  1329. * <td>Added new feature as below:
  1330. * \li ISO7816
  1331. * </tr>
  1332. * <tr>
  1333. * <td>Added new features as below:
  1334. * \li LIN master
  1335. * \li RS485
  1336. * </tr>
  1337. * <tr>
  1338. * <td>Added new features as below:
  1339. * \li Oversample
  1340. * \li Buffer overflow notification
  1341. * \li Irda
  1342. * \li Lin slave
  1343. * \li Start frame detection
  1344. * \li Hardware flow control
  1345. * \li Collision detection
  1346. * \li DMA support </td>
  1347. * </tr>
  1348. * <tr>
  1349. * <td>\li Added new \c transmitter_enable and \c receiver_enable Boolean
  1350. * values to \c struct usart_config
  1351. * \li Altered \c usart_write_* and usart_read_* functions to abort with
  1352. * an error code if the relevant transceiver is not enabled
  1353. * \li Fixed \c usart_write_buffer_wait() and \c usart_read_buffer_wait()
  1354. * not aborting correctly when a timeout condition occurs</td>
  1355. * </tr>
  1356. * <tr>
  1357. * <td>Initial Release</td>
  1358. * </tr>
  1359. * </table>
  1360. */
  1361. /**
  1362. * \page asfdoc_sam0_sercom_usart_exqsg Examples for SERCOM USART Driver
  1363. *
  1364. * This is a list of the available Quick Start guides (QSGs) and example
  1365. * applications for \ref asfdoc_sam0_sercom_usart_group. QSGs are simple examples with
  1366. * step-by-step instructions to configure and use this driver in a selection of
  1367. * use cases. Note that a QSG can be compiled as a standalone application or be
  1368. * added to the user application.
  1369. *
  1370. * - \subpage asfdoc_sam0_sercom_usart_basic_use_case
  1371. * \if USART_CALLBACK_MODE
  1372. * - \subpage asfdoc_sam0_sercom_usart_callback_use_case
  1373. * \endif
  1374. * - \subpage asfdoc_sam0_sercom_usart_dma_use_case
  1375. * - \subpage asfdoc_sam0_sercom_usart_lin_use_case
  1376. */
  1377. /**
  1378. * \page asfdoc_sam0_sercom_usart_mux_settings SERCOM USART MUX Settings
  1379. *
  1380. * The following lists the possible internal SERCOM module pad function
  1381. * assignments, for the four SERCOM pads when in USART mode. Note that this is
  1382. * in addition to the physical GPIO pin MUX of the device, and can be used in
  1383. * conjunction to optimize the serial data pin-out.
  1384. *
  1385. * When TX and RX are connected to the same pin, the USART will operate in
  1386. * half-duplex mode if both one transmitter and several receivers are enabled.
  1387. *
  1388. * \note When RX and XCK are connected to the same pin, the receiver must not
  1389. * be enabled if the USART is configured to use an external clock.
  1390. *
  1391. *
  1392. * <table>
  1393. * <tr>
  1394. * <th>MUX/Pad</th>
  1395. * <th>PAD 0</th>
  1396. * <th>PAD 1</th>
  1397. * <th>PAD 2</th>
  1398. * <th>PAD 3</th>
  1399. * </tr>
  1400. * <tr>
  1401. * <td>RX_0_TX_0_XCK_1</td>
  1402. * <td>TX / RX</td>
  1403. * <td>XCK</td>
  1404. * <td>-</td>
  1405. * <td>-</td>
  1406. * </tr>
  1407. * <tr>
  1408. * <td>RX_0_TX_2_XCK_3</td>
  1409. * <td>RX</td>
  1410. * <td>-</td>
  1411. * <td>TX</td>
  1412. * <td>XCK</td>
  1413. * </tr>
  1414. * <tr>
  1415. * <td>RX_1_TX_0_XCK_1</td>
  1416. * <td>TX</td>
  1417. * <td>RX / XCK</td>
  1418. * <td>-</td>
  1419. * <td>-</td>
  1420. * </tr>
  1421. * <tr>
  1422. * <td>RX_1_TX_2_XCK_3</td>
  1423. * <td>-</td>
  1424. * <td>RX</td>
  1425. * <td>TX</td>
  1426. * <td>XCK</td>
  1427. * </tr>
  1428. * <tr>
  1429. * <td>RX_2_TX_0_XCK_1</td>
  1430. * <td>TX</td>
  1431. * <td>XCK</td>
  1432. * <td>RX</td>
  1433. * <td>-</td>
  1434. * </tr>
  1435. * <tr>
  1436. * <td>RX_2_TX_2_XCK_3</td>
  1437. * <td>-</td>
  1438. * <td>-</td>
  1439. * <td>TX / RX</td>
  1440. * <td>XCK</td>
  1441. * </tr>
  1442. * <tr>
  1443. * <td>RX_3_TX_0_XCK_1</td>
  1444. * <td>TX</td>
  1445. * <td>XCK</td>
  1446. * <td>-</td>
  1447. * <td>RX</td>
  1448. * </tr>
  1449. * <tr>
  1450. * <td>RX_3_TX_2_XCK_3</td>
  1451. * <td>-</td>
  1452. * <td>-</td>
  1453. * <td>TX</td>
  1454. * <td>RX / XCK</td>
  1455. * </tr>
  1456. * </table>
  1457. *
  1458. * \page asfdoc_sam0_sercom_usart_document_revision_history Document Revision History
  1459. *
  1460. * <table>
  1461. * <tr>
  1462. * <th>Doc. Rev.</td>
  1463. * <th>Date</td>
  1464. * <th>Comments</td>
  1465. * </tr>
  1466. * <tr>
  1467. * <td>42118F</td>
  1468. * <td>12/2015</td>
  1469. * <td>Added support for SAM L21/L22, SAM DA1, SAM D09, SAMR30 and SAM C20/C21</td>
  1470. * </tr>
  1471. * <tr>
  1472. * <td>42118E</td>
  1473. * <td>12/2014</td>
  1474. * <td>Added support for SAM R21 and SAM D10/D11</td>
  1475. * </tr>
  1476. * <tr>
  1477. * <td>42118D</td>
  1478. * <td>01/2014</td>
  1479. * <td>Added support for SAM D21</td>
  1480. * </tr>
  1481. * <tr>
  1482. * <td>42118C</td>
  1483. * <td>10/2013</td>
  1484. * <td>Replaced the pad multiplexing documentation with a condensed table</td>
  1485. * </tr>
  1486. * <tr>
  1487. * <td>42118B</td>
  1488. * <td>06/2013</td>
  1489. * <td>Corrected documentation typos</td>
  1490. * </tr>
  1491. * <tr>
  1492. * <td>42118A</td>
  1493. * <td>06/2013</td>
  1494. * <td>Initial release</td>
  1495. * </tr>
  1496. * </table>
  1497. */
  1498. #endif /* USART_H_INCLUDED */