USART_LPC18xx.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629
  1. /* --------------------------------------------------------------------------
  2. * Copyright (c) 2013-2016 ARM Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * $Date: 02. March 2016
  19. * $Revision: V2.9
  20. *
  21. * Driver: Driver_USART0, Driver_USART1, Driver_USART2, Driver_USART3
  22. * Configured: via RTE_Device.h configuration file
  23. * Project: USART Driver for NXP LPC18xx
  24. * --------------------------------------------------------------------------
  25. * Use the following configuration settings in the middleware component
  26. * to connect to this driver.
  27. *
  28. * Configuration Setting Value UART Interface
  29. * --------------------- ----- --------------
  30. * Connect to hardware via Driver_USART# = 0 use USART0
  31. * Connect to hardware via Driver_USART# = 1 use UART1
  32. * Connect to hardware via Driver_USART# = 2 use USART2
  33. * Connect to hardware via Driver_USART# = 3 use USART3
  34. * -------------------------------------------------------------------------- */
  35. /* History:
  36. * Version 2.9
  37. * - Driver update to work with GPDMA_LPC18xx ver.: 1.3
  38. * Version 2.8
  39. * - Corrected PowerControl function for conditional Power full (driver must be initialized)
  40. * Version 2.7
  41. * - PowerControl for Power OFF and Uninitialize functions made unconditional.
  42. * - Corrected status bit-field handling, to prevent race conditions.
  43. * Version 2.6
  44. * - Corrected disabling of receive DMA channel when aborting
  45. * Receive (ARM_USART_ABORT_RECEIVE) or Transfer (ARM_USART_ABORT_TRANSFER)
  46. * Version 2.5
  47. * - fract_div_lookup_table moved from USART_LPC18xx.h to USART_LPC18xx.c
  48. * Version 2.4
  49. * - Improved baudrate calculation
  50. * Version 2.3
  51. * - Corrected RX Time-Out handling
  52. * - Corrected USART clock configuration
  53. * - Updated USART_Control function
  54. * - Updated USART_Send function
  55. * - GPDMA initialization and uninitialization
  56. * Version 2.2
  57. * - Corrected modem lines handling
  58. * Version 2.1
  59. * - Added DMA support
  60. * - Other Improvements (status checking, USART_Control, ...)
  61. * Version 2.0
  62. * - Updated to CMSIS Driver API V2.00
  63. * Version 1.1
  64. * - Based on API V1.10 (namespace prefix ARM_ added)
  65. * Version 1.0
  66. * - Initial release
  67. */
  68. #include "USART_LPC18xx.h"
  69. #include "RTE_Device.h"
  70. #include "RTE_Components.h"
  71. #define ARM_USART_DRV_VERSION ARM_DRIVER_VERSION_MAJOR_MINOR(2,9)
  72. #if ((!RTE_USART0) && (!RTE_UART1) && (!RTE_USART2) && (!RTE_USART3))
  73. #error "USART not enabled in RTE_Device.h!"
  74. #endif
  75. // Driver Version
  76. static const ARM_DRIVER_VERSION usart_driver_version = { ARM_USART_API_VERSION, ARM_USART_DRV_VERSION };
  77. // Trigger level definitions
  78. // Can be user defined by C preprocessor
  79. #ifndef USART0_TRIG_LVL
  80. #define USART0_TRIG_LVL USART_TRIG_LVL_1
  81. #endif
  82. #ifndef USART1_TRIG_LVL
  83. #define USART1_TRIG_LVL USART_TRIG_LVL_1
  84. #endif
  85. #ifndef USART2_TRIG_LVL
  86. #define USART2_TRIG_LVL USART_TRIG_LVL_1
  87. #endif
  88. #ifndef USART3_TRIG_LVL
  89. #define USART3_TRIG_LVL USART_TRIG_LVL_1
  90. #endif
  91. // SmartCard oversampling ratio
  92. #ifndef USART0_SC_OVERSAMPLING_RATIO
  93. #define USART0_SC_OVERSAMPLING_RATIO 372
  94. #endif
  95. #ifndef USART2_SC_OVERSAMPLING_RATIO
  96. #define USART2_SC_OVERSAMPLING_RATIO 372
  97. #endif
  98. #ifndef USART3_SC_OVERSAMPLING_RATIO
  99. #define USART3_SC_OVERSAMPLING_RATIO 372
  100. #endif
  101. // Fractional divider lookup table
  102. static const FRACT_DIVIDER fract_div_lookup_table[] = {
  103. {(1 << 12), 0},
  104. FRACT_DIV(1, 15),
  105. FRACT_DIV(1, 14),
  106. FRACT_DIV(1, 13),
  107. FRACT_DIV(1, 12),
  108. FRACT_DIV(1, 11),
  109. FRACT_DIV(1, 10),
  110. FRACT_DIV(1, 9),
  111. FRACT_DIV(1, 8),
  112. FRACT_DIV(2, 15),
  113. FRACT_DIV(1, 7),
  114. FRACT_DIV(2, 13),
  115. FRACT_DIV(1, 6),
  116. FRACT_DIV(2, 11),
  117. FRACT_DIV(1, 5),
  118. FRACT_DIV(3, 14),
  119. FRACT_DIV(2, 9),
  120. FRACT_DIV(3, 13),
  121. FRACT_DIV(1, 4),
  122. FRACT_DIV(4, 15),
  123. FRACT_DIV(3, 11),
  124. FRACT_DIV(2, 7),
  125. FRACT_DIV(3, 10),
  126. FRACT_DIV(4, 13),
  127. FRACT_DIV(1, 3),
  128. FRACT_DIV(5, 14),
  129. FRACT_DIV(4, 11),
  130. FRACT_DIV(3, 8),
  131. FRACT_DIV(5, 13),
  132. FRACT_DIV(2, 5),
  133. FRACT_DIV(5, 12),
  134. FRACT_DIV(3, 7),
  135. FRACT_DIV(4, 9),
  136. FRACT_DIV(5, 11),
  137. FRACT_DIV(6, 13),
  138. FRACT_DIV(7, 15),
  139. FRACT_DIV(1, 2),
  140. FRACT_DIV(8, 15),
  141. FRACT_DIV(7, 13),
  142. FRACT_DIV(6, 11),
  143. FRACT_DIV(5, 9),
  144. FRACT_DIV(4, 7),
  145. FRACT_DIV(7, 12),
  146. FRACT_DIV(3, 5),
  147. FRACT_DIV(8, 13),
  148. FRACT_DIV(5, 8),
  149. FRACT_DIV(7, 11),
  150. FRACT_DIV(9, 14),
  151. FRACT_DIV(2, 3),
  152. FRACT_DIV(9, 13),
  153. FRACT_DIV(7, 10),
  154. FRACT_DIV(5, 7),
  155. FRACT_DIV(8, 11),
  156. FRACT_DIV(11, 15),
  157. FRACT_DIV(3, 4),
  158. FRACT_DIV(10, 13),
  159. FRACT_DIV(7, 9),
  160. FRACT_DIV(11, 14),
  161. FRACT_DIV(4, 5),
  162. FRACT_DIV(9, 11),
  163. FRACT_DIV(5, 6),
  164. FRACT_DIV(11, 13),
  165. FRACT_DIV(6, 7),
  166. FRACT_DIV(13, 15),
  167. FRACT_DIV(7, 8),
  168. FRACT_DIV(8, 9),
  169. FRACT_DIV(9, 10),
  170. FRACT_DIV(10, 11),
  171. FRACT_DIV(11, 12),
  172. FRACT_DIV(12, 13),
  173. FRACT_DIV(13, 14),
  174. FRACT_DIV(14, 15)
  175. };
  176. // Fractional divider lookup table size
  177. #define FRACT_DIV_LOOKUP_TABLE_SZ (sizeof(fract_div_lookup_table) / sizeof(fract_div_lookup_table[0]))
  178. // USART0
  179. #if (RTE_USART0)
  180. static USART_INFO USART0_Info = {0};
  181. static PIN_ID USART0_pin_tx = { RTE_USART0_TX_PORT, RTE_USART0_TX_BIT, RTE_USART0_TX_FUNC };
  182. static PIN_ID USART0_pin_rx = { RTE_USART0_RX_PORT, RTE_USART0_RX_BIT, RTE_USART0_RX_FUNC };
  183. #if (RTE_USART0_UCLK_PIN_EN == 1)
  184. static PIN_ID USART0_pin_clk = { RTE_USART0_UCLK_PORT, RTE_USART0_UCLK_BIT, RTE_USART0_UCLK_FUNC };
  185. #endif
  186. #if (RTE_USART0_DMA_TX_EN == 1)
  187. void USART0_GPDMA_Tx_Event (uint32_t event);
  188. static USART_DMA USART0_DMA_Tx = {RTE_USART0_DMA_TX_CH,
  189. RTE_USART0_DMA_TX_PERI,
  190. RTE_USART0_DMA_TX_PERI_SEL,
  191. USART0_GPDMA_Tx_Event};
  192. #endif
  193. #if (RTE_USART0_DMA_RX_EN == 1)
  194. void USART0_GPDMA_Rx_Event (uint32_t event);
  195. static USART_DMA USART0_DMA_Rx = {RTE_USART0_DMA_RX_CH,
  196. RTE_USART0_DMA_RX_PERI,
  197. RTE_USART0_DMA_RX_PERI_SEL,
  198. USART0_GPDMA_Rx_Event};
  199. #endif
  200. static const USART_RESOURCES USART0_Resources = {
  201. { // Capabilities
  202. 1, // supports UART (Asynchronous) mode
  203. #if (RTE_USART0_UCLK_PIN_EN == 1)
  204. 1, // supports Synchronous Master mode
  205. 1, // supports Synchronous Slave mode
  206. #else
  207. 0, // supports Synchronous Master mode
  208. 0, // supports Synchronous Slave mode
  209. #endif
  210. 1, // supports UART Single-wire mode
  211. 0, // supports UART IrDA mode
  212. 1, // supports UART Smart Card mode
  213. #if (RTE_USART0_UCLK_PIN_EN == 1)
  214. 1, // Smart Card Clock generator
  215. #else
  216. 0,
  217. #endif
  218. 0, // RTS Flow Control available
  219. 0, // CTS Flow Control available
  220. 0, // Transmit completed event: \ref ARM_USART_EVENT_TX_COMPLETE
  221. #if ((RTE_USART0_DMA_RX_EN == 1) || (USART0_TRIG_LVL == USART_TRIG_LVL_1))
  222. 0, // Signal receive character timeout event: \ref ARM_USART_EVENT_RX_TIMEOUT
  223. #else
  224. 1, // Signal receive character timeout event: \ref ARM_USART_EVENT_RX_TIMEOUT
  225. #endif
  226. 0, // RTS Line: 0=not available, 1=available
  227. 0, // CTS Line: 0=not available, 1=available
  228. 0, // DTR Line: 0=not available, 1=available
  229. 0, // DSR Line: 0=not available, 1=available
  230. 0, // DCD Line: 0=not available, 1=available
  231. 0, // RI Line: 0=not available, 1=available
  232. 0, // Signal CTS change event: \ref ARM_USART_EVENT_CTS
  233. 0, // Signal DSR change event: \ref ARM_USART_EVENT_DSR
  234. 0, // Signal DCD change event: \ref ARM_USART_EVENT_DCD
  235. 0, // Signal RI change event: \ref ARM_USART_EVENT_RI
  236. },
  237. LPC_USART0,
  238. NULL,
  239. { // USART Pin Configuration
  240. &USART0_pin_tx,
  241. &USART0_pin_rx,
  242. #if (RTE_USART0_UCLK_PIN_EN == 1)
  243. &USART0_pin_clk,
  244. #else
  245. NULL,
  246. #endif
  247. NULL, NULL, NULL, NULL, NULL, NULL,
  248. },
  249. { // USART Clocks Configuration
  250. &LPC_CCU1->CLK_M3_USART0_CFG,
  251. &LPC_CCU1->CLK_M3_USART0_STAT,
  252. &LPC_CCU2->CLK_APB0_USART0_CFG,
  253. &LPC_CCU2->CLK_APB0_USART0_STAT,
  254. &LPC_CGU->BASE_UART0_CLK,
  255. },
  256. { // USART Reset Configuration
  257. (1 << 12),
  258. &LPC_RGU->RESET_CTRL1,
  259. &LPC_RGU->RESET_ACTIVE_STATUS1,
  260. },
  261. USART0_IRQn,
  262. USART0_TRIG_LVL,
  263. #if (RTE_USART0_DMA_TX_EN == 1)
  264. &USART0_DMA_Tx,
  265. #else
  266. NULL,
  267. #endif
  268. #if (RTE_USART0_DMA_RX_EN == 1)
  269. &USART0_DMA_Rx,
  270. #else
  271. NULL,
  272. #endif
  273. &USART0_Info,
  274. USART0_SC_OVERSAMPLING_RATIO
  275. };
  276. #endif
  277. // UART1
  278. #if (RTE_UART1)
  279. static USART_INFO USART1_Info = {0};
  280. static PIN_ID USART1_pin_tx = { RTE_UART1_TX_PORT, RTE_UART1_TX_BIT, RTE_UART1_TX_FUNC };
  281. static PIN_ID USART1_pin_rx = { RTE_UART1_RX_PORT, RTE_UART1_RX_BIT, RTE_UART1_RX_FUNC };
  282. #if (RTE_UART1_CTS_PIN_EN == 1)
  283. static PIN_ID USART1_pin_cts = { RTE_UART1_CTS_PORT, RTE_UART1_CTS_BIT, RTE_UART1_CTS_FUNC };
  284. #endif
  285. #if (RTE_UART1_RTS_PIN_EN == 1)
  286. static PIN_ID USART1_pin_rts = { RTE_UART1_RTS_PORT, RTE_UART1_RTS_BIT, RTE_UART1_RTS_FUNC };
  287. #endif
  288. #if (RTE_UART1_DCD_PIN_EN == 1)
  289. static PIN_ID USART1_pin_dcd = { RTE_UART1_DCD_PORT, RTE_UART1_DCD_BIT, RTE_UART1_DCD_FUNC };
  290. #endif
  291. #if (RTE_UART1_DSR_PIN_EN == 1)
  292. static PIN_ID USART1_pin_dsr = { RTE_UART1_DSR_PORT, RTE_UART1_DSR_BIT, RTE_UART1_DSR_FUNC };
  293. #endif
  294. #if (RTE_UART1_DTR_PIN_EN == 1)
  295. static PIN_ID USART1_pin_dtr = { RTE_UART1_DTR_PORT, RTE_UART1_DTR_BIT, RTE_UART1_DTR_FUNC };
  296. #endif
  297. #if (RTE_UART1_RI_PIN_EN == 1)
  298. static PIN_ID USART1_pin_ri = { RTE_UART1_RI_PORT, RTE_UART1_RI_BIT, RTE_UART1_RI_FUNC };
  299. #endif
  300. #if (RTE_UART1_DMA_TX_EN == 1)
  301. void USART1_GPDMA_Tx_Event (uint32_t event);
  302. static USART_DMA USART1_DMA_Tx = {RTE_UART1_DMA_TX_CH,
  303. RTE_UART1_DMA_TX_PERI,
  304. RTE_UART1_DMA_TX_PERI_SEL,
  305. USART1_GPDMA_Tx_Event};
  306. #endif
  307. #if (RTE_UART1_DMA_RX_EN == 1)
  308. void USART1_GPDMA_Rx_Event (uint32_t event);
  309. static USART_DMA USART1_DMA_Rx = {RTE_UART1_DMA_RX_CH,
  310. RTE_UART1_DMA_RX_PERI,
  311. RTE_UART1_DMA_RX_PERI_SEL,
  312. USART1_GPDMA_Rx_Event};
  313. #endif
  314. static const USART_RESOURCES USART1_Resources = {
  315. { // Capabilities
  316. 1, // supports UART (Asynchronous) mode
  317. 0, // supports Synchronous Master mode
  318. 0, // supports Synchronous Slave mode
  319. 0, // supports UART Single-wire mode
  320. 0, // supports UART IrDA mode
  321. 0, // supports UART Smart Card mode
  322. 0, // Smart Card Clock generator
  323. #if (RTE_UART1_RTS_PIN_EN == 1)
  324. 1, // RTS Flow Control available
  325. #else
  326. 0, // RTS Flow Control available
  327. #endif
  328. #if (RTE_UART1_CTS_PIN_EN == 1)
  329. 1, // CTS Flow Control available
  330. #else
  331. 0, // CTS Flow Control available
  332. #endif
  333. 0, // Transmit completed event: \ref ARM_USART_EVENT_TX_COMPLETE
  334. #if ((RTE_UART1_DMA_RX_EN == 1) || (USART1_TRIG_LVL == USART_TRIG_LVL_1))
  335. 0, // Signal receive character timeout event: \ref ARM_USART_EVENT_RX_TIMEOUT
  336. #else
  337. 1, // Signal receive character timeout event: \ref ARM_USART_EVENT_RX_TIMEOUT
  338. #endif
  339. #if (RTE_UART1_RTS_PIN_EN == 1)
  340. 1, // RTS Line: 0=not available, 1=available
  341. #else
  342. 0,
  343. #endif
  344. #if (RTE_UART1_CTS_PIN_EN == 1)
  345. 1, // CTS Line: 0=not available, 1=available
  346. #else
  347. 0,
  348. #endif
  349. #if (RTE_UART1_DTR_PIN_EN == 1)
  350. 1, // DTR Line: 0=not available, 1=available
  351. #else
  352. 0,
  353. #endif
  354. #if (RTE_UART1_DSR_PIN_EN == 1)
  355. 1, // DSR Line: 0=not available, 1=available
  356. #else
  357. 0,
  358. #endif
  359. #if (RTE_UART1_DCD_PIN_EN == 1)
  360. 1, // DCD Line: 0=not available, 1=available
  361. #else
  362. 0,
  363. #endif
  364. #if (RTE_UART1_RI_PIN_EN == 1)
  365. 1, // RI Line: 0=not available, 1=available
  366. #else
  367. 0,
  368. #endif
  369. #if (RTE_UART1_CTS_PIN_EN == 1)
  370. 1, // Signal CTS change event: \ref ARM_USART_EVENT_CTS
  371. #else
  372. 0,
  373. #endif
  374. #if (RTE_UART1_DSR_PIN_EN == 1)
  375. 1, // Signal DSR change event: \ref ARM_USART_EVENT_DSR
  376. #else
  377. 0,
  378. #endif
  379. #if (RTE_UART1_DCD_PIN_EN == 1)
  380. 1, // Signal DCD change event: \ref ARM_USART_EVENT_DCD
  381. #else
  382. 0,
  383. #endif
  384. #if (RTE_UART1_RI_PIN_EN == 1)
  385. 1, // Signal RI change event: \ref ARM_USART_EVENT_RI
  386. #else
  387. 0,
  388. #endif
  389. },
  390. (LPC_USARTn_Type *)LPC_UART1,
  391. LPC_UART1,
  392. { // USART Pin Configuration
  393. &USART1_pin_tx,
  394. &USART1_pin_rx,
  395. NULL,
  396. #if (RTE_UART1_CTS_PIN_EN == 1)
  397. &USART1_pin_cts,
  398. #else
  399. NULL,
  400. #endif
  401. #if (RTE_UART1_RTS_PIN_EN == 1)
  402. &USART1_pin_rts,
  403. #else
  404. NULL,
  405. #endif
  406. #if (RTE_UART1_DCD_PIN_EN == 1)
  407. &USART1_pin_dcd,
  408. #else
  409. NULL,
  410. #endif
  411. #if (RTE_UART1_DSR_PIN_EN == 1)
  412. &USART1_pin_dsr,
  413. #else
  414. NULL,
  415. #endif
  416. #if (RTE_UART1_DTR_PIN_EN == 1)
  417. &USART1_pin_dtr,
  418. #else
  419. NULL,
  420. #endif
  421. #if (RTE_UART1_RI_PIN_EN == 1)
  422. &USART1_pin_ri,
  423. #else
  424. NULL,
  425. #endif
  426. },
  427. { // USART Clocks Configuration
  428. &LPC_CCU1->CLK_M3_UART1_CFG,
  429. &LPC_CCU1->CLK_M3_UART1_STAT,
  430. &LPC_CCU2->CLK_APB0_UART1_CFG,
  431. &LPC_CCU2->CLK_APB0_UART1_STAT,
  432. &LPC_CGU->BASE_UART1_CLK,
  433. },
  434. { // USART Reset Configuration
  435. (1 << 13),
  436. &LPC_RGU->RESET_CTRL1,
  437. &LPC_RGU->RESET_ACTIVE_STATUS1,
  438. },
  439. UART1_IRQn,
  440. USART1_TRIG_LVL,
  441. #if (RTE_UART1_DMA_TX_EN == 1)
  442. &USART1_DMA_Tx,
  443. #else
  444. NULL,
  445. #endif
  446. #if (RTE_UART1_DMA_RX_EN == 1)
  447. &USART1_DMA_Rx,
  448. #else
  449. NULL,
  450. #endif
  451. &USART1_Info,
  452. 0
  453. };
  454. #endif
  455. // USART2
  456. #if (RTE_USART2)
  457. static USART_INFO USART2_Info = {0};
  458. static PIN_ID USART2_pin_tx = { RTE_USART2_TX_PORT, RTE_USART2_TX_BIT, RTE_USART2_TX_FUNC };
  459. static PIN_ID USART2_pin_rx = { RTE_USART2_RX_PORT, RTE_USART2_RX_BIT, RTE_USART2_RX_FUNC };
  460. #if (RTE_USART2_UCLK_PIN_EN == 1)
  461. static PIN_ID USART2_pin_clk = { RTE_USART2_UCLK_PORT, RTE_USART2_UCLK_BIT, RTE_USART2_UCLK_FUNC };
  462. #endif
  463. #if (RTE_USART2_DMA_TX_EN == 1)
  464. void USART2_GPDMA_Tx_Event (uint32_t event);
  465. static USART_DMA USART2_DMA_Tx = {RTE_USART2_DMA_TX_CH,
  466. RTE_USART2_DMA_TX_PERI,
  467. RTE_USART2_DMA_TX_PERI_SEL,
  468. USART2_GPDMA_Tx_Event};
  469. #endif
  470. #if (RTE_USART2_DMA_RX_EN == 1)
  471. void USART2_GPDMA_Rx_Event (uint32_t event);
  472. static USART_DMA USART2_DMA_Rx = {RTE_USART2_DMA_RX_CH,
  473. RTE_USART2_DMA_RX_PERI,
  474. RTE_USART2_DMA_RX_PERI_SEL,
  475. USART2_GPDMA_Rx_Event};
  476. #endif
  477. static const USART_RESOURCES USART2_Resources = {
  478. { // Capabilities
  479. 1, // supports UART (Asynchronous) mode
  480. #if (RTE_USART1_UCLK_PIN_EN == 1)
  481. 1, // supports Synchronous Master mode
  482. 1, // supports Synchronous Slave mode
  483. #else
  484. 0, // supports Synchronous Master mode
  485. 0, // supports Synchronous Slave mode
  486. #endif
  487. 1, // supports UART Single-wire mode
  488. 0, // supports UART IrDA mode
  489. 1, // supports UART Smart Card mode
  490. #if (RTE_USART2_UCLK_PIN_EN == 1)
  491. 1, // Smart Card Clock generator
  492. #else
  493. 0,
  494. #endif
  495. 0, // RTS Flow Control available
  496. 0, // CTS Flow Control available
  497. 0, // Transmit completed event: \ref ARM_USART_EVENT_TX_COMPLETE
  498. #if ((RTE_USART2_DMA_RX_EN == 1) || (USART2_TRIG_LVL == USART_TRIG_LVL_1))
  499. 0, // Signal receive character timeout event: \ref ARM_USART_EVENT_RX_TIMEOUT
  500. #else
  501. 1, // Signal receive character timeout event: \ref ARM_USART_EVENT_RX_TIMEOUT
  502. #endif
  503. 0, // RTS Line: 0=not available, 1=available
  504. 0, // CTS Line: 0=not available, 1=available
  505. 0, // DTR Line: 0=not available, 1=available
  506. 0, // DSR Line: 0=not available, 1=available
  507. 0, // DCD Line: 0=not available, 1=available
  508. 0, // RI Line: 0=not available, 1=available
  509. 0, // Signal CTS change event: \ref ARM_USART_EVENT_CTS
  510. 0, // Signal DSR change event: \ref ARM_USART_EVENT_DSR
  511. 0, // Signal DCD change event: \ref ARM_USART_EVENT_DCD
  512. 0, // Signal RI change event: \ref ARM_USART_EVENT_RI
  513. },
  514. LPC_USART2,
  515. NULL,
  516. { // USART Pin Configuration
  517. &USART2_pin_tx,
  518. &USART2_pin_rx,
  519. #if (RTE_USART2_UCLK_PIN_EN == 1)
  520. &USART2_pin_clk,
  521. #else
  522. NULL,
  523. #endif
  524. NULL, NULL, NULL, NULL, NULL, NULL,
  525. },
  526. { // USART Clocks Configuration
  527. &LPC_CCU1->CLK_M3_USART2_CFG,
  528. &LPC_CCU1->CLK_M3_USART2_STAT,
  529. &LPC_CCU2->CLK_APB2_USART2_CFG,
  530. &LPC_CCU2->CLK_APB2_USART2_STAT,
  531. &LPC_CGU->BASE_UART2_CLK,
  532. },
  533. { // USART Reset Configuration
  534. (1 << 14),
  535. &LPC_RGU->RESET_CTRL1,
  536. &LPC_RGU->RESET_ACTIVE_STATUS1,
  537. },
  538. USART2_IRQn,
  539. USART2_TRIG_LVL,
  540. #if (RTE_USART2_DMA_TX_EN == 1)
  541. &USART2_DMA_Tx,
  542. #else
  543. NULL,
  544. #endif
  545. #if (RTE_USART2_DMA_RX_EN == 1)
  546. &USART2_DMA_Rx,
  547. #else
  548. NULL,
  549. #endif
  550. &USART2_Info,
  551. USART2_SC_OVERSAMPLING_RATIO
  552. };
  553. #endif
  554. // USART3
  555. #if (RTE_USART3)
  556. static USART_INFO USART3_Info = {0};
  557. static PIN_ID USART3_pin_tx = { RTE_USART3_TX_PORT, RTE_USART3_TX_BIT, RTE_USART3_TX_FUNC };
  558. static PIN_ID USART3_pin_rx = { RTE_USART3_RX_PORT, RTE_USART3_RX_BIT, RTE_USART3_RX_FUNC };
  559. #if (RTE_USART3_UCLK_PIN_EN == 1)
  560. static PIN_ID USART3_pin_clk = { RTE_USART3_UCLK_PORT, RTE_USART3_UCLK_BIT, RTE_USART3_UCLK_FUNC };
  561. #endif
  562. #if (RTE_USART3_DMA_TX_EN == 1)
  563. void USART3_GPDMA_Tx_Event (uint32_t event);
  564. static USART_DMA USART3_DMA_Tx = {RTE_USART3_DMA_TX_CH,
  565. RTE_USART3_DMA_TX_PERI,
  566. RTE_USART3_DMA_TX_PERI_SEL,
  567. USART3_GPDMA_Tx_Event};
  568. #endif
  569. #if (RTE_USART3_DMA_RX_EN == 1)
  570. void USART3_GPDMA_Rx_Event (uint32_t event);
  571. static USART_DMA USART3_DMA_Rx = {RTE_USART3_DMA_RX_CH,
  572. RTE_USART3_DMA_RX_PERI,
  573. RTE_USART3_DMA_RX_PERI_SEL,
  574. USART3_GPDMA_Rx_Event};
  575. #endif
  576. static const USART_RESOURCES USART3_Resources = {
  577. { // Capabilities
  578. 1, // supports UART (Asynchronous) mode
  579. #if (RTE_USART3_UCLK_PIN_EN == 1)
  580. 1, // supports Synchronous Master mode
  581. 1, // supports Synchronous Slave mode
  582. #else
  583. 0, // supports Synchronous Master mode
  584. 0, // supports Synchronous Slave mode
  585. #endif
  586. 1, // supports UART Single-wire mode
  587. 1, // supports UART IrDA mode
  588. 1, // supports UART Smart Card mode
  589. #if (RTE_USART3_UCLK_PIN_EN == 1)
  590. 1, // Smart Card Clock generator
  591. #else
  592. 0,
  593. #endif
  594. 0, // RTS Flow Control available
  595. 0, // CTS Flow Control available
  596. 0, // Transmit completed event: \ref ARM_USART_EVENT_TX_COMPLETE
  597. #if ((RTE_USART3_DMA_RX_EN == 1) || (USART3_TRIG_LVL == USART_TRIG_LVL_1))
  598. 0, // Signal receive character timeout event: \ref ARM_USART_EVENT_RX_TIMEOUT
  599. #else
  600. 1, // Signal receive character timeout event: \ref ARM_USART_EVENT_RX_TIMEOUT
  601. #endif
  602. 0, // RTS Line: 0=not available, 1=available
  603. 0, // CTS Line: 0=not available, 1=available
  604. 0, // DTR Line: 0=not available, 1=available
  605. 0, // DSR Line: 0=not available, 1=available
  606. 0, // DCD Line: 0=not available, 1=available
  607. 0, // RI Line: 0=not available, 1=available
  608. 0, // Signal CTS change event: \ref ARM_USART_EVENT_CTS
  609. 0, // Signal DSR change event: \ref ARM_USART_EVENT_DSR
  610. 0, // Signal DCD change event: \ref ARM_USART_EVENT_DCD
  611. 0, // Signal RI change event: \ref ARM_USART_EVENT_RI
  612. },
  613. LPC_USART3,
  614. NULL,
  615. { // USART Pin Configuration
  616. &USART3_pin_tx,
  617. &USART3_pin_rx,
  618. #if (RTE_USART3_UCLK_PIN_EN == 1)
  619. &USART3_pin_clk,
  620. #else
  621. NULL,
  622. #endif
  623. NULL, NULL, NULL, NULL, NULL, NULL,
  624. },
  625. { // USART Clocks Configuration
  626. &LPC_CCU1->CLK_M3_USART3_CFG,
  627. &LPC_CCU1->CLK_M3_USART3_STAT,
  628. &LPC_CCU2->CLK_APB2_USART3_CFG,
  629. &LPC_CCU2->CLK_APB2_USART3_STAT,
  630. &LPC_CGU->BASE_UART3_CLK,
  631. },
  632. { // USART Reset Configuration
  633. (1 << 15),
  634. &LPC_RGU->RESET_CTRL1,
  635. &LPC_RGU->RESET_ACTIVE_STATUS1,
  636. },
  637. USART3_IRQn,
  638. USART3_TRIG_LVL,
  639. #if (RTE_USART3_DMA_TX_EN == 1)
  640. &USART3_DMA_Tx,
  641. #else
  642. NULL,
  643. #endif
  644. #if (RTE_USART3_DMA_RX_EN == 1)
  645. &USART3_DMA_Rx,
  646. #else
  647. NULL,
  648. #endif
  649. &USART3_Info,
  650. USART3_SC_OVERSAMPLING_RATIO
  651. };
  652. #endif
  653. // Extern Function
  654. extern uint32_t GetClockFreq (uint32_t clk_src);
  655. // Local Function
  656. /**
  657. \fn int32_t USART_SetBaudrate (uint32_t baudrate,
  658. USART_RESOURCES *usart)
  659. \brief Set baudrate dividers
  660. \param[in] baudrate Usart baudrate
  661. \param[in] usart Pointer to USART resources)
  662. \returns
  663. - \b 0: function succeeded
  664. - \b -1: function failed
  665. */
  666. int32_t USART_SetBaudrate (uint32_t baudrate,
  667. USART_RESOURCES *usart) {
  668. uint8_t add, mul, add_mul_best, oversampling_fract_best;
  669. uint16_t latch_div_best, oversampling, oversampling_best;
  670. uint32_t i, j, pclk, div, tmp_div, latch_div, delta, delta_best, val;
  671. pclk = GetClockFreq ((*usart->clk.base_clk >> 24) & 0x1FU);
  672. // Calculate fixed point divider (12 LSBs are fractional part)
  673. div = (uint32_t)(((uint64_t)pclk << FRACT_BITS) / (uint64_t)baudrate);
  674. delta_best = 0xFFFFFFFFU;
  675. oversampling_fract_best = 0U;
  676. // SmartCard mode
  677. if (usart->info->mode == ARM_USART_MODE_SMART_CARD) {
  678. oversampling_best = usart->sc_oversamp;
  679. for (i = 0; i < FRACT_DIV_LOOKUP_TABLE_SZ; i++) {
  680. // Calculate latch divider (latch_div = div / (fract_div * oversampling(16)))
  681. latch_div = ((div / fract_div_lookup_table[i].val) / oversampling_best);
  682. if (latch_div > 65535U) { continue; }
  683. for (j = 0U; j < 2U; j++) {
  684. // Which latch divider value is more appropriate:
  685. // latch_div or latch_div + 1 (rounded up)
  686. if (latch_div < 3U) { latch_div++; continue; }
  687. // Calculate actual divider (temp_div = latch_div * fract_div * oversampling(16))
  688. tmp_div = (latch_div * fract_div_lookup_table[i].val) / oversampling_best;
  689. // Calculate delta
  690. if (div > tmp_div) { delta = div - tmp_div; }
  691. else { delta = tmp_div - div; }
  692. // Check if delta is better than best delta
  693. if (delta < delta_best) {
  694. delta_best = delta;
  695. add_mul_best = fract_div_lookup_table[i].add_mul;
  696. latch_div_best = latch_div;
  697. }
  698. latch_div++;
  699. }
  700. }
  701. } else {
  702. // Oversampling is fixed to 16
  703. // divider = oversampling * latch divider * fractional divider = 16 * latch_div * fract_div
  704. if (div >= FIXED_OVERSAMPLING_DIVIDER_LIMIT) {
  705. latch_div = div >> (FRACT_BITS + 4U);
  706. if ((div == (latch_div << (FRACT_BITS + 4U))) && ((latch_div >> 4) <= 0xFFFFU)) {
  707. // Fractional part of divider is 0
  708. delta_best = 0U;
  709. add_mul_best = 0U;
  710. latch_div_best = latch_div;
  711. oversampling_best = 16U;
  712. } else {
  713. // Divider larger than 48, can be accomplished with configurable
  714. // latch and fractional divider, and fixed oversampling to 16
  715. for (i = 0U; i < FRACT_DIV_LOOKUP_TABLE_SZ; i++) {
  716. // Calculate latch divider (latch_div = div / (fract_div * oversampling(16)))
  717. latch_div = ((div / fract_div_lookup_table[i].val) >> 4);
  718. if (latch_div > 65535U) { continue; }
  719. for (j = 0U; j < 2U; j++) {
  720. // Which latch divider value is more appropriate:
  721. // latch_div or latch_div + 1 (rounded up)
  722. if (latch_div < 3U) { latch_div++; continue; }
  723. // Calculate actual divider (temp_div = latch_div * fract_div * oversampling(16))
  724. tmp_div = (latch_div * fract_div_lookup_table[i].val) << 4;
  725. // Calculate delta
  726. if (div > tmp_div) { delta = div - tmp_div; }
  727. else { delta = tmp_div - div; }
  728. // Check if delta is better than best delta
  729. if (delta < delta_best) {
  730. delta_best = delta;
  731. add_mul_best = fract_div_lookup_table[i].add_mul;
  732. latch_div_best = latch_div;
  733. oversampling_best = 16U;
  734. }
  735. latch_div++;
  736. }
  737. }
  738. }
  739. } else {
  740. // Check if oversampling register is available
  741. if (usart->uart_reg != NULL) {return - 1; }
  742. if (div > INTEGER_OVERSAMPLING_DIVIDER_LIMIT) {
  743. // Oversampling ratio is integer value
  744. // Set oversampling
  745. if (div > (48U << 12)) { oversampling = 15U; }
  746. else if (div > (45U << 12)) { oversampling = 14U; }
  747. else if (div > (42U << 12)) { oversampling = 13U; }
  748. else if (div > (38U << 12)) { oversampling = 12U; }
  749. else if (div > (35U << 12)) { oversampling = 11U; }
  750. else if (div > (32U << 12)) { oversampling = 10U; }
  751. else if (div > (29U << 12)) { oversampling = 9U; }
  752. else if (div > (26U << 12)) { oversampling = 8U; }
  753. else if (div > (23U << 12)) { oversampling = 7U; }
  754. else if (div > (19U << 12)) { oversampling = 6U; }
  755. else if (div > (16U << 12)) { oversampling = 5U; }
  756. else { oversampling = 4U; }
  757. // Check if divider is integer value
  758. tmp_div = (div / oversampling);
  759. if ((tmp_div & FRACT_MASK) == 0U) {
  760. // Fractional part of divider is 0
  761. delta_best = 0U;
  762. add_mul_best = 0U;
  763. latch_div_best = tmp_div >> FRACT_BITS;
  764. oversampling_best = oversampling;
  765. } else {
  766. // Fractional part of divider is not 0
  767. latch_div = 3U;
  768. for (i = 0U; i < FRACT_DIV_LOOKUP_TABLE_SZ; i++) {
  769. // Calculate actual divider (temp_div = latch_div * fract_div * oversampling)
  770. tmp_div = latch_div * fract_div_lookup_table[i].val * oversampling;
  771. // Calculate delta
  772. if (div > tmp_div) { delta = div - tmp_div; }
  773. else { delta = tmp_div - div; }
  774. // Check if delta is better than best delta
  775. if (delta < delta_best) {
  776. delta_best = delta;
  777. add_mul_best = fract_div_lookup_table[i].add_mul;
  778. latch_div_best = latch_div;
  779. oversampling_best = oversampling;
  780. }
  781. }
  782. }
  783. //tmp_div = latch_div_best * fract_best * oversampling_best;
  784. add = add_mul_best & 0x0FU;
  785. mul = add_mul_best >> 4;
  786. tmp_div = ((latch_div_best * (mul + add) * oversampling_best) << 12) / mul;
  787. if ((tmp_div & FRACT_MASK) == 0U) {
  788. // If best possible divider is integer value, make sure
  789. // fractional divider is 0 and max oversampling is used
  790. oversampling = 16U;
  791. do {
  792. if (((tmp_div / oversampling) & FRACT_MASK) == 0U) {
  793. // Fractional part of divider is 0
  794. tmp_div /= oversampling;
  795. add_mul_best = 0U;
  796. latch_div_best = tmp_div >> FRACT_BITS;
  797. oversampling_best = oversampling;
  798. break;
  799. }
  800. oversampling--;
  801. } while (oversampling >= 4U);
  802. }
  803. } else {
  804. // Oversampling ratio can be fractional,
  805. // latch divider is 1 and fractional divider is not used
  806. // Oversampling step
  807. val = (125U << FRACT_BITS) / 1000U;
  808. oversampling = 13U << FRACT_BITS;
  809. do {
  810. // Calculate delta
  811. if (div > oversampling) { delta = div - oversampling; }
  812. else { delta = oversampling - div; }
  813. // Check if delta is better than best delta
  814. if (delta < delta_best) {
  815. delta_best = delta;
  816. add_mul_best = 0U;
  817. latch_div_best = 1U;
  818. oversampling_best = oversampling;
  819. }
  820. oversampling -= val;
  821. } while (oversampling >= (4U << FRACT_BITS));
  822. oversampling_fract_best = ((oversampling_best & FRACT_MASK) << 3) >> FRACT_BITS;
  823. oversampling_best = oversampling_best >> FRACT_BITS;
  824. }
  825. }
  826. }
  827. if (((delta_best * 100U) / div) > USART_MAX_BAUDRATE_ERROR) { return -1; }
  828. usart->reg->LCR |= USART_LCR_DLAB;
  829. usart->reg->DLM = ((latch_div_best >> 8) & 0xFFU) << USART_DLM_DLMSB_POS;
  830. usart->reg->DLL = (latch_div_best & USART_DLL_DLLSB_MSK) << USART_DLL_DLLSB_POS;
  831. // Reset DLAB bit
  832. usart->reg->LCR &= (~USART_LCR_DLAB);
  833. usart->reg->FDR = ((add_mul_best & USART_FDR_MULVAL_MSK) |
  834. (add_mul_best & USART_FDR_DIVADDVAL_MSK));
  835. // Check if oversampling register is available
  836. if (usart->uart_reg == NULL) {
  837. usart->reg->OSR = ((oversampling_best - 1) << USART_OSR_OSINT_POS) |
  838. ( oversampling_fract_best << USART_OSR_OSFRAC_POS);
  839. }
  840. usart->info->baudrate = baudrate;
  841. return 0;
  842. }
  843. /**
  844. \fn uint32_t USART_RxLineIntHandler (USART_RESOURCES *usart)
  845. \brief Receive line interrupt handler
  846. \param[in] usart Pointer to USART resources
  847. \return Rx Line event mask
  848. */
  849. static uint32_t USART_RxLineIntHandler (USART_RESOURCES *usart) {
  850. uint32_t lsr, event;
  851. event = 0U;
  852. lsr = usart->reg->LSR & USART_LSR_LINE_INT;
  853. // OverRun error
  854. if (lsr & USART_LSR_OE) {
  855. usart->info->rx_status.rx_overflow = 1U;
  856. event |= ARM_USART_EVENT_RX_OVERFLOW;
  857. // Sync Slave mode: If Transmitter enabled, signal TX underflow
  858. if (usart->info->mode == ARM_USART_MODE_SYNCHRONOUS_SLAVE) {
  859. if (usart->info->xfer.send_active != 0U) {
  860. event |= ARM_USART_EVENT_TX_UNDERFLOW;
  861. }
  862. }
  863. }
  864. // Parity error
  865. if (lsr & USART_LSR_PE) {
  866. usart->info->rx_status.rx_parity_error = 1U;
  867. event |= ARM_USART_EVENT_RX_PARITY_ERROR;
  868. }
  869. // Break detected
  870. if (lsr & USART_LSR_BI) {
  871. usart->info->rx_status.rx_break = 1U;
  872. event |= ARM_USART_EVENT_RX_BREAK;
  873. }
  874. // Framing error
  875. else {
  876. if(lsr & USART_LSR_FE) {
  877. usart->info->rx_status.rx_framing_error = 1U;
  878. event |= ARM_USART_EVENT_RX_FRAMING_ERROR;
  879. }
  880. }
  881. return event;
  882. }
  883. // Function Prototypes
  884. static int32_t USART_Receive (void *data,
  885. uint32_t num,
  886. USART_RESOURCES *usart);
  887. // USART Driver functions
  888. /**
  889. \fn ARM_DRIVER_VERSION USARTx_GetVersion (void)
  890. \brief Get driver version.
  891. \return \ref ARM_DRIVER_VERSION
  892. */
  893. static ARM_DRIVER_VERSION USARTx_GetVersion (void) {
  894. return usart_driver_version;
  895. }
  896. /**
  897. \fn ARM_USART_CAPABILITIES USART_GetCapabilities (USART_RESOURCES *usart)
  898. \brief Get driver capabilities
  899. \param[in] usart Pointer to USART resources
  900. \return \ref ARM_USART_CAPABILITIES
  901. */
  902. static ARM_USART_CAPABILITIES USART_GetCapabilities (USART_RESOURCES *usart) {
  903. return usart->capabilities;
  904. }
  905. /**
  906. \fn int32_t USART_Initialize (ARM_USART_SignalEvent_t cb_event
  907. USART_RESOURCES *usart)
  908. \brief Initialize USART Interface.
  909. \param[in] cb_event Pointer to \ref ARM_USART_SignalEvent
  910. \param[in] usart Pointer to USART resources
  911. \return \ref execution_status
  912. */
  913. static int32_t USART_Initialize (ARM_USART_SignalEvent_t cb_event,
  914. USART_RESOURCES *usart) {
  915. if (usart->info->flags & USART_FLAG_INITIALIZED) {
  916. // Driver is already initialized
  917. return ARM_DRIVER_OK;
  918. }
  919. // Initialize USART Run-time Resources
  920. usart->info->cb_event = cb_event;
  921. usart->info->rx_status.rx_busy = 0U;
  922. usart->info->rx_status.rx_overflow = 0U;
  923. usart->info->rx_status.rx_break = 0U;
  924. usart->info->rx_status.rx_framing_error = 0U;
  925. usart->info->rx_status.rx_parity_error = 0U;
  926. usart->info->xfer.send_active = 0U;
  927. usart->info->xfer.tx_def_val = 0U;
  928. // Configure CTS pin
  929. if (usart->capabilities.cts) {
  930. SCU_PinConfigure(usart->pins.cts->port, usart->pins.cts->num,
  931. SCU_PIN_CFG_INPUT_FILTER_DIS | SCU_PIN_CFG_INPUT_BUFFER_EN |
  932. SCU_PIN_CFG_MODE(usart->pins.cts->config_val));
  933. }
  934. // Configure RTS pin
  935. if (usart->capabilities.rts) {
  936. SCU_PinConfigure(usart->pins.rts->port, usart->pins.rts->num,
  937. SCU_PIN_CFG_MODE(usart->pins.rts->config_val));
  938. }
  939. // Configure DCD pin
  940. if (usart->capabilities.dcd) {
  941. SCU_PinConfigure(usart->pins.dcd->port, usart->pins.dcd->num,
  942. SCU_PIN_CFG_INPUT_FILTER_DIS | SCU_PIN_CFG_INPUT_BUFFER_EN |
  943. SCU_PIN_CFG_MODE(usart->pins.dcd->config_val));
  944. }
  945. // Configure DSR pin
  946. if (usart->capabilities.dsr) {
  947. SCU_PinConfigure(usart->pins.dsr->port, usart->pins.dsr->num,
  948. SCU_PIN_CFG_INPUT_FILTER_DIS | SCU_PIN_CFG_INPUT_BUFFER_EN |
  949. SCU_PIN_CFG_MODE(usart->pins.dsr->config_val));
  950. }
  951. // Configure DTR pin
  952. if (usart->capabilities.dtr) {
  953. SCU_PinConfigure(usart->pins.dtr->port, usart->pins.dtr->num,
  954. SCU_PIN_CFG_MODE(usart->pins.dtr->config_val));
  955. }
  956. // Configure RI pin
  957. if (usart->capabilities.ri) {
  958. SCU_PinConfigure(usart->pins.ri->port, usart->pins.ri->num,
  959. SCU_PIN_CFG_INPUT_FILTER_DIS | SCU_PIN_CFG_INPUT_BUFFER_EN |
  960. SCU_PIN_CFG_MODE(usart->pins.ri->config_val));
  961. }
  962. // DMA Initialize
  963. if (usart->dma_tx || usart->dma_rx) { GPDMA_Initialize (); }
  964. usart->info->flags = USART_FLAG_INITIALIZED;
  965. return ARM_DRIVER_OK;
  966. }
  967. /**
  968. \fn int32_t USART_Uninitialize (USART_RESOURCES *usart)
  969. \brief De-initialize USART Interface.
  970. \param[in] usart Pointer to USART resources
  971. \return \ref execution_status
  972. */
  973. static int32_t USART_Uninitialize (USART_RESOURCES *usart) {
  974. // Reset TX pin configuration
  975. SCU_PinConfigure(usart->pins.tx->port, usart->pins.tx->num , 0U);
  976. // Reset RX pin configuration
  977. SCU_PinConfigure(usart->pins.rx->port, usart->pins.rx->num , 0U);
  978. // Reset CLK pin configuration
  979. if (usart->pins.clk) {
  980. SCU_PinConfigure(usart->pins.clk->port, usart->pins.clk->num, 0U);
  981. }
  982. // Reset CTS pin configuration
  983. if (usart->capabilities.cts) {
  984. SCU_PinConfigure(usart->pins.cts->port, usart->pins.cts->num, 0U);
  985. }
  986. // Reset RTS pin configuration
  987. if (usart->capabilities.rts) {
  988. SCU_PinConfigure(usart->pins.rts->port, usart->pins.rts->num, 0U);
  989. }
  990. // Configure DCD pin configuration
  991. if (usart->capabilities.dcd) {
  992. SCU_PinConfigure(usart->pins.dcd->port, usart->pins.dcd->num, 0U);
  993. }
  994. // Reset DSR pin configuration
  995. if (usart->capabilities.dsr) {
  996. SCU_PinConfigure(usart->pins.dsr->port, usart->pins.dsr->num, 0U);
  997. }
  998. // Reset DTR pin configuration
  999. if (usart->capabilities.dtr) {
  1000. SCU_PinConfigure(usart->pins.dtr->port, usart->pins.dtr->num, 0U);
  1001. }
  1002. // Reset RI pin configuration
  1003. if (usart->capabilities.ri) {
  1004. SCU_PinConfigure(usart->pins.ri->port, usart->pins.ri->num, 0U);
  1005. }
  1006. // DMA Uninitialize
  1007. if (usart->dma_tx || usart->dma_rx) { GPDMA_Uninitialize (); }
  1008. // Reset USART status flags
  1009. usart->info->flags = 0U;
  1010. return ARM_DRIVER_OK;
  1011. }
  1012. /**
  1013. \fn int32_t USART_PowerControl (ARM_POWER_STATE state)
  1014. \brief Control USART Interface Power.
  1015. \param[in] state Power state
  1016. \param[in] usart Pointer to USART resources
  1017. \return \ref execution_status
  1018. */
  1019. static int32_t USART_PowerControl (ARM_POWER_STATE state,
  1020. USART_RESOURCES *usart) {
  1021. uint32_t val;
  1022. switch (state) {
  1023. case ARM_POWER_OFF:
  1024. // Disable USART IRQ
  1025. NVIC_DisableIRQ(usart->irq_num);
  1026. // If DMA mode - disable TX DMA channel
  1027. if ((usart->dma_tx) && (usart->info->xfer.send_active != 0U)) {
  1028. GPDMA_ChannelDisable (usart->dma_tx->channel);
  1029. }
  1030. // If DMA mode - disable DMA channel
  1031. if ((usart->dma_rx) && (usart->info->rx_status.rx_busy)) {
  1032. GPDMA_ChannelDisable (usart->dma_rx->channel);
  1033. }
  1034. // Reset USART peripheral
  1035. *usart->rst.reg_cfg = usart->rst.reg_cfg_val;
  1036. while ((*(usart->rst.reg_stat) & usart->rst.reg_cfg_val) == 0U);
  1037. // Disable USART peripheral clock
  1038. *usart->clk.peri_cfg &= ~1U;
  1039. while (*usart->clk.peri_cfg & 1U);
  1040. // Disable USART register interface clock
  1041. *usart->clk.reg_cfg &= ~1U;
  1042. while (*usart->clk.reg_cfg & 1U);
  1043. // Clear pending USART interrupts in NVIC
  1044. NVIC_ClearPendingIRQ(usart->irq_num);
  1045. // Clear driver variables
  1046. usart->info->rx_status.rx_busy = 0U;
  1047. usart->info->rx_status.rx_overflow = 0U;
  1048. usart->info->rx_status.rx_break = 0U;
  1049. usart->info->rx_status.rx_framing_error = 0U;
  1050. usart->info->rx_status.rx_parity_error = 0U;
  1051. usart->info->xfer.send_active = 0U;
  1052. usart->info->flags &= ~USART_FLAG_POWERED;
  1053. break;
  1054. case ARM_POWER_LOW:
  1055. return ARM_DRIVER_ERROR_UNSUPPORTED;
  1056. case ARM_POWER_FULL:
  1057. if ((usart->info->flags & USART_FLAG_INITIALIZED) == 0U) { return ARM_DRIVER_ERROR; }
  1058. if ((usart->info->flags & USART_FLAG_POWERED) != 0U) { return ARM_DRIVER_OK; }
  1059. // Connect USART base clock to PLL1
  1060. *usart->clk.base_clk = (1U << 11) |
  1061. (0x09U << 24) ;
  1062. // Enable USART register interface clock
  1063. *usart->clk.reg_cfg |= CCU_CLK_CFG_AUTO | CCU_CLK_CFG_RUN;
  1064. while ((*usart->clk.reg_cfg & CCU_CLK_CFG_RUN) == 0U);
  1065. // Enable USART peripheral clock
  1066. *usart->clk.peri_cfg |= CCU_CLK_CFG_AUTO | CCU_CLK_CFG_RUN;
  1067. while (( *usart->clk.peri_cfg & CCU_CLK_CFG_RUN) == 0U);
  1068. // Reset USART peripheral
  1069. *usart->rst.reg_cfg = usart->rst.reg_cfg_val;
  1070. while ((*(usart->rst.reg_stat) & usart->rst.reg_cfg_val) == 0U);
  1071. // Disable transmitter
  1072. usart->reg->TER &= ~USART_TER_TXEN;
  1073. // Disable receiver
  1074. usart->reg->RS485CTRL |= USART_RS485CTRL_RXDIS;
  1075. // Disable interrupts
  1076. usart->reg->IER = 0U;
  1077. // Configure FIFO Control register
  1078. // Set trigger level
  1079. val = (usart->trig_lvl & USART_FCR_RXTRIGLVL_MSK) | USART_FCR_FIFOEN;
  1080. if (usart->dma_rx || usart->dma_tx) {
  1081. val |= USART_FCR_DMAMODE;
  1082. }
  1083. usart->reg->FCR = val;
  1084. #if (RTE_UART1)
  1085. // Enable modem lines status interrupts (only UART1)
  1086. if (usart->uart_reg) {
  1087. if (usart->capabilities.cts || usart->capabilities.dcd ||
  1088. usart->capabilities.dsr || usart->capabilities.ri) {
  1089. usart->uart_reg->IER |= UART_IER_MSIE;
  1090. }
  1091. }
  1092. #endif
  1093. // Clear driver variables
  1094. usart->info->rx_status.rx_busy = 0U;
  1095. usart->info->rx_status.rx_overflow = 0U;
  1096. usart->info->rx_status.rx_break = 0U;
  1097. usart->info->rx_status.rx_framing_error = 0U;
  1098. usart->info->rx_status.rx_parity_error = 0U;
  1099. usart->info->mode = 0U;
  1100. usart->info->flags = 0U;
  1101. usart->info->xfer.send_active = 0U;
  1102. usart->info->flags = USART_FLAG_POWERED | USART_FLAG_INITIALIZED;
  1103. // Clear and Enable USART IRQ
  1104. NVIC_ClearPendingIRQ(usart->irq_num);
  1105. NVIC_EnableIRQ(usart->irq_num);
  1106. break;
  1107. default: return ARM_DRIVER_ERROR_UNSUPPORTED;
  1108. }
  1109. return ARM_DRIVER_OK;
  1110. }
  1111. /**
  1112. \fn int32_t USART_Send (const void *data,
  1113. uint32_t num,
  1114. USART_RESOURCES *usart)
  1115. \brief Start sending data to USART transmitter.
  1116. \param[in] data Pointer to buffer with data to send to USART transmitter
  1117. \param[in] num Number of data items to send
  1118. \param[in] usart Pointer to USART resources
  1119. \return \ref execution_status
  1120. */
  1121. static int32_t USART_Send (const void *data,
  1122. uint32_t num,
  1123. USART_RESOURCES *usart) {
  1124. int32_t stat, source_inc, val;
  1125. if ((data == NULL) || (num == 0U)) {
  1126. // Invalid parameters
  1127. return ARM_DRIVER_ERROR_PARAMETER;
  1128. }
  1129. if ((usart->info->flags & USART_FLAG_CONFIGURED) == 0U) {
  1130. // USART is not configured (mode not selected)
  1131. return ARM_DRIVER_ERROR;
  1132. }
  1133. if (usart->info->xfer.send_active != 0U) {
  1134. // Send is not completed yet
  1135. return ARM_DRIVER_ERROR_BUSY;
  1136. }
  1137. // Set Send active flag
  1138. usart->info->xfer.send_active = 1U;
  1139. // For DMA mode: source increment
  1140. source_inc = GPDMA_CH_CONTROL_SI;
  1141. // Synchronous mode
  1142. if ((usart->info->mode == ARM_USART_MODE_SYNCHRONOUS_MASTER) ||
  1143. (usart->info->mode == ARM_USART_MODE_SYNCHRONOUS_SLAVE )) {
  1144. if (usart->info->xfer.sync_mode == 0U) {
  1145. usart->info->xfer.sync_mode = USART_SYNC_MODE_TX;
  1146. // Start dummy reads
  1147. stat = USART_Receive (&usart->info->xfer.rx_dump_val, num, usart);
  1148. if (stat != ARM_DRIVER_OK) { return ARM_DRIVER_ERROR; }
  1149. } else {
  1150. if (usart->info->xfer.sync_mode == USART_SYNC_MODE_RX) {
  1151. // Dummy DMA writes (do not increment source address)
  1152. source_inc = 0U;
  1153. }
  1154. }
  1155. }
  1156. // Save transmit buffer info
  1157. usart->info->xfer.tx_buf = (uint8_t *)data;
  1158. usart->info->xfer.tx_num = num;
  1159. usart->info->xfer.tx_cnt = 0U;
  1160. // DMA mode
  1161. if (usart->dma_tx) {
  1162. // Configure DMA mux
  1163. GPDMA_PeripheralSelect (usart->dma_tx->peripheral, usart->dma_tx->peripheral_sel);
  1164. // Configure DMA channel
  1165. stat = GPDMA_ChannelConfigure (usart->dma_tx->channel,
  1166. (uint32_t)data,
  1167. (uint32_t)(&(usart->reg->THR)),
  1168. num,
  1169. GPDMA_CH_CONTROL_SBSIZE(GPDMA_BSIZE_1) |
  1170. GPDMA_CH_CONTROL_DBSIZE(GPDMA_BSIZE_1) |
  1171. GPDMA_CH_CONTROL_SWIDTH(GPDMA_WIDTH_BYTE) |
  1172. GPDMA_CH_CONTROL_DWIDTH(GPDMA_WIDTH_BYTE) |
  1173. GPDMA_CH_CONTROL_S |
  1174. GPDMA_CH_CONTROL_D |
  1175. GPDMA_CH_CONTROL_I |
  1176. source_inc,
  1177. GPDMA_CH_CONFIG_DEST_PERI(usart->dma_tx->peripheral) |
  1178. GPDMA_CH_CONFIG_FLOWCNTRL(GPDMA_TRANSFER_M2P_CTRL_DMA) |
  1179. GPDMA_CH_CONFIG_IE |
  1180. GPDMA_CH_CONFIG_ITC |
  1181. GPDMA_CH_CONFIG_E,
  1182. usart->dma_tx->cb_event);
  1183. if (stat == -1) { return ARM_DRIVER_ERROR; }
  1184. // Interrupt mode
  1185. } else {
  1186. // Fill TX FIFO
  1187. if (usart->reg->LSR & USART_LSR_THRE) {
  1188. val = 16U;
  1189. while ((val--) && (usart->info->xfer.tx_cnt != usart->info->xfer.tx_num)) {
  1190. usart->reg->THR = usart->info->xfer.tx_buf[usart->info->xfer.tx_cnt++];
  1191. }
  1192. }
  1193. // Enable transmit holding register empty interrupt
  1194. usart->reg->IER |= USART_IER_THREIE;
  1195. }
  1196. return ARM_DRIVER_OK;
  1197. }
  1198. /**
  1199. \fn int32_t USART_Receive (void *data,
  1200. uint32_t num,
  1201. USART_RESOURCES *usart)
  1202. \brief Start receiving data from USART receiver.
  1203. \param[out] data Pointer to buffer for data to receive from USART receiver
  1204. \param[in] num Number of data items to receive
  1205. \param[in] usart Pointer to USART resources
  1206. \return \ref execution_status
  1207. */
  1208. static int32_t USART_Receive (void *data,
  1209. uint32_t num,
  1210. USART_RESOURCES *usart) {
  1211. int32_t stat, dest_inc;
  1212. if ((data == NULL) || (num == 0U)) {
  1213. // Invalid parameters
  1214. return ARM_DRIVER_ERROR_PARAMETER;
  1215. }
  1216. if ((usart->info->flags & USART_FLAG_CONFIGURED) == 0U) {
  1217. // USART is not configured (mode not selected)
  1218. return ARM_DRIVER_ERROR;
  1219. }
  1220. // Check if receiver is busy
  1221. if (usart->info->rx_status.rx_busy == 1U) {
  1222. return ARM_DRIVER_ERROR_BUSY;
  1223. }
  1224. // Set RX busy flag
  1225. usart->info->rx_status.rx_busy = 1U;
  1226. dest_inc = GPDMA_CH_CONTROL_DI;
  1227. // Save number of data to be received
  1228. usart->info->xfer.rx_num = num;
  1229. // Clear RX statuses
  1230. usart->info->rx_status.rx_break = 0U;
  1231. usart->info->rx_status.rx_framing_error = 0U;
  1232. usart->info->rx_status.rx_overflow = 0U;
  1233. usart->info->rx_status.rx_parity_error = 0U;
  1234. // Save receive buffer info
  1235. usart->info->xfer.rx_buf = (uint8_t *)data;
  1236. usart->info->xfer.rx_cnt = 0U;
  1237. // Synchronous mode
  1238. if ((usart->info->mode == ARM_USART_MODE_SYNCHRONOUS_MASTER) ||
  1239. (usart->info->mode == ARM_USART_MODE_SYNCHRONOUS_SLAVE )) {
  1240. if (usart->info->xfer.sync_mode == USART_SYNC_MODE_TX) {
  1241. // Dummy DMA reads (do not increment destination address)
  1242. dest_inc = 0U;
  1243. }
  1244. }
  1245. // DMA mode
  1246. if (usart->dma_rx) {
  1247. GPDMA_PeripheralSelect (usart->dma_rx->peripheral, usart->dma_rx->peripheral_sel);
  1248. stat = GPDMA_ChannelConfigure (usart->dma_rx->channel,
  1249. (uint32_t)&usart->reg->RBR,
  1250. (uint32_t)data,
  1251. num,
  1252. GPDMA_CH_CONTROL_SBSIZE(GPDMA_BSIZE_1) |
  1253. GPDMA_CH_CONTROL_DBSIZE(GPDMA_BSIZE_1) |
  1254. GPDMA_CH_CONTROL_SWIDTH(GPDMA_WIDTH_BYTE) |
  1255. GPDMA_CH_CONTROL_DWIDTH(GPDMA_WIDTH_BYTE) |
  1256. GPDMA_CH_CONTROL_S |
  1257. GPDMA_CH_CONTROL_D |
  1258. GPDMA_CH_CONTROL_I |
  1259. dest_inc,
  1260. GPDMA_CH_CONFIG_SRC_PERI(usart->dma_rx->peripheral) |
  1261. GPDMA_CH_CONFIG_FLOWCNTRL(GPDMA_TRANSFER_P2M_CTRL_DMA) |
  1262. GPDMA_CH_CONFIG_IE |
  1263. GPDMA_CH_CONFIG_ITC |
  1264. GPDMA_CH_CONFIG_E,
  1265. usart->dma_rx->cb_event);
  1266. if (stat == -1) { return ARM_DRIVER_ERROR; }
  1267. // Interrupt mode
  1268. } else {
  1269. // Enable receive data available interrupt
  1270. usart->reg->IER |= USART_IER_RBRIE;
  1271. }
  1272. // Synchronous mode
  1273. if ((usart->info->mode == ARM_USART_MODE_SYNCHRONOUS_MASTER) ||
  1274. (usart->info->mode == ARM_USART_MODE_SYNCHRONOUS_SLAVE )) {
  1275. if (usart->info->xfer.sync_mode == 0U) {
  1276. usart->info->xfer.sync_mode = USART_SYNC_MODE_RX;
  1277. // Send dummy data
  1278. stat = USART_Send (&usart->info->xfer.tx_def_val, num, usart);
  1279. if (stat != ARM_DRIVER_OK) { return ARM_DRIVER_ERROR; }
  1280. }
  1281. }
  1282. return ARM_DRIVER_OK;
  1283. }
  1284. /**
  1285. \fn int32_t USART_Transfer (const void *data_out,
  1286. void *data_in,
  1287. uint32_t num,
  1288. USART_RESOURCES *usart)
  1289. \brief Start sending/receiving data to/from USART transmitter/receiver.
  1290. \param[in] data_out Pointer to buffer with data to send to USART transmitter
  1291. \param[out] data_in Pointer to buffer for data to receive from USART receiver
  1292. \param[in] num Number of data items to transfer
  1293. \param[in] usart Pointer to USART resources
  1294. \return \ref execution_status
  1295. */
  1296. static int32_t USART_Transfer (const void *data_out,
  1297. void *data_in,
  1298. uint32_t num,
  1299. USART_RESOURCES *usart) {
  1300. int32_t status;
  1301. if ((data_out == NULL) || (data_in == NULL) || (num == 0U)) {
  1302. // Invalid parameters
  1303. return ARM_DRIVER_ERROR_PARAMETER;
  1304. }
  1305. if ((usart->info->flags & USART_FLAG_CONFIGURED) == 0U) {
  1306. // USART is not configured
  1307. return ARM_DRIVER_ERROR;
  1308. }
  1309. if ((usart->info->mode == ARM_USART_MODE_SYNCHRONOUS_MASTER) ||
  1310. (usart->info->mode == ARM_USART_MODE_SYNCHRONOUS_SLAVE )) {
  1311. // Set xfer mode
  1312. usart->info->xfer.sync_mode = USART_SYNC_MODE_TX_RX;
  1313. // Receive
  1314. status = USART_Receive (data_in, num, usart);
  1315. if (status != ARM_DRIVER_OK) { return status; }
  1316. // Send
  1317. status = USART_Send (data_out, num, usart);
  1318. if (status != ARM_DRIVER_OK) { return status; }
  1319. } else {
  1320. // Only in synchronous mode
  1321. return ARM_DRIVER_ERROR;
  1322. }
  1323. return ARM_DRIVER_OK;
  1324. }
  1325. /**
  1326. \fn uint32_t USART_GetTxCount (USART_RESOURCES *usart)
  1327. \brief Get transmitted data count.
  1328. \param[in] usart Pointer to USART resources
  1329. \return number of data items transmitted
  1330. */
  1331. static uint32_t USART_GetTxCount (USART_RESOURCES *usart) {
  1332. uint32_t cnt;
  1333. if (usart->dma_tx) {
  1334. cnt = GPDMA_ChannelGetCount (usart->dma_tx->channel);
  1335. } else {
  1336. cnt = usart->info->xfer.tx_cnt;
  1337. }
  1338. return cnt;
  1339. }
  1340. /**
  1341. \fn uint32_t USART_GetRxCount (USART_RESOURCES *usart)
  1342. \brief Get received data count.
  1343. \param[in] usart Pointer to USART resources
  1344. \return number of data items received
  1345. */
  1346. static uint32_t USART_GetRxCount (USART_RESOURCES *usart) {
  1347. uint32_t cnt;
  1348. if (usart->dma_rx) {
  1349. cnt = GPDMA_ChannelGetCount (usart->dma_rx->channel);
  1350. } else {
  1351. cnt = usart->info->xfer.rx_cnt;
  1352. }
  1353. return cnt;
  1354. }
  1355. /**
  1356. \fn int32_t USART_Control (uint32_t control,
  1357. uint32_t arg,
  1358. USART_RESOURCES *usart)
  1359. \brief Control USART Interface.
  1360. \param[in] control Operation
  1361. \param[in] arg Argument of operation (optional)
  1362. \param[in] usart Pointer to USART resources
  1363. \return common \ref execution_status and driver specific \ref usart_execution_status
  1364. */
  1365. static int32_t USART_Control (uint32_t control,
  1366. uint32_t arg,
  1367. USART_RESOURCES *usart) {
  1368. uint32_t val, mode;
  1369. uint32_t syncctrl, hden, icr, scictrl, lcr, mcr;
  1370. if ((usart->info->flags & USART_FLAG_POWERED) == 0U) {
  1371. // USART not powered
  1372. return ARM_DRIVER_ERROR;
  1373. }
  1374. syncctrl = 0U;
  1375. hden = 0U;
  1376. icr = 0U;
  1377. scictrl = 0U;
  1378. lcr = 0U;
  1379. switch (control & ARM_USART_CONTROL_Msk) {
  1380. // Control TX
  1381. case ARM_USART_CONTROL_TX:
  1382. // Check if TX line available
  1383. if (usart->pins.tx == NULL) { return ARM_DRIVER_ERROR; }
  1384. if (arg) {
  1385. if (usart->info->mode != ARM_USART_MODE_SMART_CARD) {
  1386. // USART TX pin function selected
  1387. SCU_PinConfigure(usart->pins.tx->port, usart->pins.tx->num, SCU_PIN_CFG_INPUT_FILTER_DIS |
  1388. SCU_PIN_CFG_MODE(usart->pins.tx->config_val));
  1389. }
  1390. usart->info->flags |= USART_FLAG_TX_ENABLED;
  1391. usart->reg->TER |= USART_TER_TXEN;
  1392. } else {
  1393. usart->info->flags &= ~USART_FLAG_TX_ENABLED;
  1394. usart->reg->TER &= ~USART_TER_TXEN;
  1395. if (usart->info->mode != ARM_USART_MODE_SMART_CARD) {
  1396. // GPIO pin function selected
  1397. SCU_PinConfigure(usart->pins.tx->port, usart->pins.tx->num, SCU_PIN_CFG_INPUT_FILTER_DIS |
  1398. SCU_PIN_CFG_MODE(SCU_CFG_MODE_FUNC0));
  1399. }
  1400. }
  1401. return ARM_DRIVER_OK;
  1402. // Control RX
  1403. case ARM_USART_CONTROL_RX:
  1404. if (usart->pins.rx == NULL) { return ARM_DRIVER_ERROR; }
  1405. // RX Line interrupt enable (overrun, framing, parity error, break)
  1406. if (arg) {
  1407. if ((usart->info->mode != ARM_USART_MODE_SMART_CARD) &&
  1408. (usart->info->mode != ARM_USART_MODE_SINGLE_WIRE )) {
  1409. // USART RX pin function selected
  1410. SCU_PinConfigure(usart->pins.rx->port, usart->pins.rx->num,
  1411. SCU_PIN_CFG_INPUT_BUFFER_EN | SCU_PIN_CFG_INPUT_FILTER_DIS |
  1412. SCU_PIN_CFG_MODE(usart->pins.rx->config_val));
  1413. }
  1414. usart->info->flags |= USART_FLAG_RX_ENABLED;
  1415. usart->reg->RS485CTRL &= ~USART_RS485CTRL_RXDIS;
  1416. usart->reg->IER |= USART_IER_RXIE;
  1417. } else {
  1418. usart->info->flags &= ~USART_FLAG_RX_ENABLED;
  1419. usart->reg->RS485CTRL |= USART_RS485CTRL_RXDIS;
  1420. usart->reg->IER &= ~USART_IER_RXIE;
  1421. if ((usart->info->mode != ARM_USART_MODE_SMART_CARD) &&
  1422. (usart->info->mode != ARM_USART_MODE_SINGLE_WIRE )) {
  1423. // GPIO pin function selected
  1424. SCU_PinConfigure(usart->pins.rx->port, usart->pins.rx->num, SCU_PIN_CFG_INPUT_FILTER_DIS |
  1425. SCU_PIN_CFG_MODE(SCU_CFG_MODE_FUNC0));
  1426. }
  1427. }
  1428. return ARM_DRIVER_OK;
  1429. // Control break
  1430. case ARM_USART_CONTROL_BREAK:
  1431. if (arg) {
  1432. if (usart->info->xfer.send_active != 0U) { return ARM_DRIVER_ERROR_BUSY; }
  1433. usart->reg->LCR |= USART_LCR_BC;
  1434. // Set Send active flag
  1435. usart->info->xfer.send_active = 1U;
  1436. }
  1437. else {
  1438. usart->reg->LCR &= ~USART_LCR_BC;
  1439. // Clear Send active flag
  1440. usart->info->xfer.send_active = 0U;
  1441. }
  1442. return ARM_DRIVER_OK;
  1443. // Abort Send
  1444. case ARM_USART_ABORT_SEND:
  1445. // Disable transmit holding register empty interrupt
  1446. usart->reg->IER &= ~USART_IER_THREIE;
  1447. // Set trigger level
  1448. val = (usart->trig_lvl & USART_FCR_RXTRIGLVL_MSK) | USART_FCR_FIFOEN;
  1449. if (usart->dma_rx || usart->dma_tx) {
  1450. val |= USART_FCR_DMAMODE;
  1451. }
  1452. // Transmit FIFO reset
  1453. val |= USART_FCR_TXFIFORES;
  1454. usart->reg->FCR = val;
  1455. // If DMA mode - disable DMA channel
  1456. if ((usart->dma_tx) && (usart->info->xfer.send_active != 0U)) {
  1457. GPDMA_ChannelDisable (usart->dma_tx->channel);
  1458. }
  1459. // Clear Send active flag
  1460. usart->info->xfer.send_active = 0U;
  1461. return ARM_DRIVER_OK;
  1462. // Abort receive
  1463. case ARM_USART_ABORT_RECEIVE:
  1464. // Disable receive data available interrupt
  1465. usart->reg->IER &= ~USART_IER_RBRIE;
  1466. // Set trigger level
  1467. val = (usart->trig_lvl & USART_FCR_RXTRIGLVL_MSK) |
  1468. USART_FCR_FIFOEN;
  1469. if (usart->dma_rx || usart->dma_tx) {
  1470. val |= USART_FCR_DMAMODE;
  1471. }
  1472. // Receive FIFO reset
  1473. val |= USART_FCR_RXFIFORES;
  1474. usart->reg->FCR = val;
  1475. // If DMA mode - disable DMA channel
  1476. if ((usart->dma_rx) && (usart->info->rx_status.rx_busy)) {
  1477. GPDMA_ChannelDisable (usart->dma_rx->channel);
  1478. }
  1479. // Clear RX busy status
  1480. usart->info->rx_status.rx_busy = 0U;
  1481. return ARM_DRIVER_OK;
  1482. // Abort transfer
  1483. case ARM_USART_ABORT_TRANSFER:
  1484. // Disable transmit holding register empty and
  1485. // receive data available interrupts
  1486. usart->reg->IER &= ~(USART_IER_THREIE | USART_IER_RBRIE);
  1487. // If DMA mode - disable DMA channel
  1488. if ((usart->dma_tx) && (usart->info->xfer.send_active != 0U)) {
  1489. GPDMA_ChannelDisable (usart->dma_tx->channel);
  1490. }
  1491. if ((usart->dma_rx) && (usart->info->rx_status.rx_busy)) {
  1492. GPDMA_ChannelDisable (usart->dma_rx->channel);
  1493. }
  1494. // Set trigger level
  1495. val = (usart->trig_lvl & USART_FCR_RXTRIGLVL_MSK) | USART_FCR_FIFOEN;
  1496. if (usart->dma_rx || usart->dma_tx) {
  1497. val |= USART_FCR_DMAMODE;
  1498. }
  1499. // Transmit and receive FIFO reset
  1500. val |= USART_FCR_TXFIFORES | USART_FCR_RXFIFORES;
  1501. usart->reg->FCR = val;
  1502. // Clear busy statuses
  1503. usart->info->rx_status.rx_busy = 0U;
  1504. usart->info->xfer.send_active = 0U;
  1505. return ARM_DRIVER_OK;
  1506. default: break;
  1507. }
  1508. switch (control & ARM_USART_CONTROL_Msk) {
  1509. case ARM_USART_MODE_ASYNCHRONOUS:
  1510. mode = ARM_USART_MODE_ASYNCHRONOUS;
  1511. break;
  1512. case ARM_USART_MODE_SYNCHRONOUS_MASTER:
  1513. if (usart->capabilities.synchronous_master) {
  1514. // Enable synchronous master (SCLK out) mode
  1515. syncctrl = USART_SYNCCTRL_SYNC | USART_SYNCCTRL_CSRC;
  1516. } else { return ARM_USART_ERROR_MODE; }
  1517. mode = ARM_USART_MODE_SYNCHRONOUS_MASTER;
  1518. break;
  1519. case ARM_USART_MODE_SYNCHRONOUS_SLAVE:
  1520. if (usart->capabilities.synchronous_slave) {
  1521. // Enable synchronous slave (SCLK in) mode
  1522. syncctrl = USART_SYNCCTRL_SYNC;
  1523. } else { return ARM_USART_ERROR_MODE; }
  1524. mode = ARM_USART_MODE_SYNCHRONOUS_SLAVE;
  1525. break;
  1526. case ARM_USART_MODE_SINGLE_WIRE:
  1527. // Enable Half duplex
  1528. hden = USART_HDEN_HDEN;
  1529. mode = ARM_USART_MODE_SINGLE_WIRE;
  1530. break;
  1531. case ARM_USART_MODE_IRDA:
  1532. if (usart->capabilities.irda) {
  1533. // Enable IrDA mode
  1534. icr = USART_ICR_IRDAEN;
  1535. } else { return ARM_USART_ERROR_MODE; }
  1536. mode = ARM_USART_MODE_IRDA;
  1537. break;
  1538. case ARM_USART_MODE_SMART_CARD:
  1539. if (usart->capabilities.smart_card) {
  1540. // Enable Smart card mode
  1541. scictrl = USART_SCICTRL_SCIEN;
  1542. } else { return ARM_USART_ERROR_MODE; }
  1543. mode = ARM_USART_MODE_SMART_CARD;
  1544. break;
  1545. // Default TX value
  1546. case ARM_USART_SET_DEFAULT_TX_VALUE:
  1547. usart->info->xfer.tx_def_val = arg;
  1548. return ARM_DRIVER_OK;
  1549. // IrDA pulse
  1550. case ARM_USART_SET_IRDA_PULSE:
  1551. if (usart->capabilities.irda) {
  1552. if (arg == 0U) {
  1553. usart->reg->ICR &= ~(USART_ICR_FIXPULSEEN);
  1554. } else {
  1555. val = 1000000000U / (GetClockFreq (((*usart->clk.base_clk >> 24) & 0x1FU)));
  1556. icr = usart->reg->ICR & ~USART_ICR_PULSEDIV_MSK;
  1557. if (arg <= (2U * val)) { icr |= (0U << USART_ICR_PULSEDIV_POS); }
  1558. else if (arg <= (4U * val)) { icr |= (1U << USART_ICR_PULSEDIV_POS); }
  1559. else if (arg <= (8U * val)) { icr |= (2U << USART_ICR_PULSEDIV_POS); }
  1560. else if (arg <= (16U * val)) { icr |= (3U << USART_ICR_PULSEDIV_POS); }
  1561. else if (arg <= (32U * val)) { icr |= (4U << USART_ICR_PULSEDIV_POS); }
  1562. else if (arg <= (64U * val)) { icr |= (5U << USART_ICR_PULSEDIV_POS); }
  1563. else if (arg <= (128U * val)) { icr |= (6U << USART_ICR_PULSEDIV_POS); }
  1564. else if (arg <= (256U * val)) { icr |= (7U << USART_ICR_PULSEDIV_POS); }
  1565. else { return ARM_DRIVER_ERROR; }
  1566. usart->reg->ICR = icr | USART_ICR_FIXPULSEEN;
  1567. }
  1568. } else { return ARM_DRIVER_ERROR; }
  1569. return ARM_DRIVER_OK;
  1570. // SmartCard guard time
  1571. case ARM_USART_SET_SMART_CARD_GUARD_TIME:
  1572. if (usart->capabilities.smart_card) {
  1573. if (arg > 0xFF) { return ARM_DRIVER_ERROR; }
  1574. usart->reg->SCICTRL &= ~USART_SCICTRL_GUARDTIME_MSK;
  1575. usart->reg->SCICTRL |= (arg << USART_SCICTRL_GUARDTIME_POS);
  1576. } else { return ARM_DRIVER_ERROR; }
  1577. return ARM_DRIVER_OK;
  1578. // SmartCard clock
  1579. case ARM_USART_SET_SMART_CARD_CLOCK:
  1580. if (usart->capabilities.smart_card == 0U) { return ARM_DRIVER_ERROR; }
  1581. if (arg == 0U) { return ARM_DRIVER_OK; }
  1582. if (usart->capabilities.smart_card_clock) {
  1583. if ((usart->info->baudrate * usart->sc_oversamp) != arg) {
  1584. return ARM_DRIVER_ERROR;
  1585. }
  1586. } else { return ARM_DRIVER_ERROR; }
  1587. return ARM_DRIVER_OK;
  1588. // SmartCard NACK
  1589. case ARM_USART_CONTROL_SMART_CARD_NACK:
  1590. if (usart->capabilities.smart_card) {
  1591. if (arg) { usart->reg->SCICTRL &= ~USART_SCICTRL_NACKDIS; }
  1592. else { usart->reg->SCICTRL |= USART_SCICTRL_NACKDIS; }
  1593. } else return ARM_DRIVER_ERROR;
  1594. return ARM_DRIVER_OK;
  1595. // Unsupported command
  1596. default: return ARM_DRIVER_ERROR_UNSUPPORTED;
  1597. }
  1598. // Check if Receiver/Transmitter is busy
  1599. if ( usart->info->rx_status.rx_busy ||
  1600. (usart->info->xfer.send_active != 0U)) {
  1601. return ARM_DRIVER_ERROR_BUSY;
  1602. }
  1603. // USART Data bits
  1604. switch (control & ARM_USART_DATA_BITS_Msk) {
  1605. case ARM_USART_DATA_BITS_5: lcr |= (0U << USART_LCR_WLS_POS); break;
  1606. case ARM_USART_DATA_BITS_6: lcr |= (1U << USART_LCR_WLS_POS); break;
  1607. case ARM_USART_DATA_BITS_7: lcr |= (2U << USART_LCR_WLS_POS); break;
  1608. case ARM_USART_DATA_BITS_8: lcr |= (3U << USART_LCR_WLS_POS); break;
  1609. default: return ARM_USART_ERROR_DATA_BITS;
  1610. }
  1611. // USART Parity
  1612. switch (control & ARM_USART_PARITY_Msk) {
  1613. case ARM_USART_PARITY_NONE: break;
  1614. case ARM_USART_PARITY_EVEN: lcr |= (1U << USART_LCR_PS_POS) |
  1615. USART_LCR_PE; break;
  1616. case ARM_USART_PARITY_ODD: lcr |= USART_LCR_PE; break;
  1617. default: return (ARM_USART_ERROR_PARITY);
  1618. }
  1619. // USART Stop bits
  1620. switch (control & ARM_USART_STOP_BITS_Msk) {
  1621. case ARM_USART_STOP_BITS_1: break;
  1622. case ARM_USART_STOP_BITS_2: lcr |= USART_LCR_SBS; break;
  1623. default: return ARM_USART_ERROR_STOP_BITS;
  1624. }
  1625. // USART Flow control (RTS and CTS lines are only available on USART1)
  1626. if (usart->uart_reg != NULL) {
  1627. mcr = usart->uart_reg->MCR & ~(UART_MCR_RTSEN | UART_MCR_CTSEN);
  1628. switch (control & ARM_USART_FLOW_CONTROL_Msk) {
  1629. case ARM_USART_FLOW_CONTROL_NONE:
  1630. break;
  1631. case ARM_USART_FLOW_CONTROL_RTS:
  1632. if (usart->capabilities.flow_control_rts) {
  1633. mcr |= UART_MCR_RTSEN;
  1634. }
  1635. else { return ARM_USART_ERROR_FLOW_CONTROL; }
  1636. break;
  1637. case ARM_USART_FLOW_CONTROL_CTS:
  1638. if (usart->capabilities.flow_control_cts) {
  1639. mcr |= UART_MCR_CTSEN;
  1640. } else {
  1641. return ARM_USART_ERROR_FLOW_CONTROL;
  1642. }
  1643. break;
  1644. case ARM_USART_FLOW_CONTROL_RTS_CTS:
  1645. if (usart->capabilities.flow_control_rts &&
  1646. usart->capabilities.flow_control_cts) {
  1647. mcr |= (UART_MCR_RTSEN | UART_MCR_CTSEN);
  1648. } else {
  1649. return ARM_USART_ERROR_FLOW_CONTROL;
  1650. }
  1651. break;
  1652. default: { return ARM_USART_ERROR_FLOW_CONTROL; }
  1653. }
  1654. }
  1655. // Clock setting for synchronous mode
  1656. if ((mode == ARM_USART_MODE_SYNCHRONOUS_MASTER) ||
  1657. (mode == ARM_USART_MODE_SYNCHRONOUS_SLAVE )) {
  1658. // Only CPOL0 - CPHA1 combination available
  1659. // USART clock polarity
  1660. if ((control & ARM_USART_CPOL_Msk) != ARM_USART_CPOL0) {
  1661. return ARM_USART_ERROR_CPOL;
  1662. }
  1663. // USART clock phase
  1664. if ((control & ARM_USART_CPHA_Msk) != ARM_USART_CPHA1) {
  1665. return ARM_USART_ERROR_CPHA;
  1666. }
  1667. }
  1668. // USART Baudrate
  1669. if (USART_SetBaudrate (arg, usart) == -1) {
  1670. return ARM_USART_ERROR_BAUDRATE;
  1671. }
  1672. // Configuration is OK - Mode is valid
  1673. usart->info->mode = mode;
  1674. // Configure TX pin regarding mode and transmitter state
  1675. val = SCU_PIN_CFG_INPUT_FILTER_DIS;
  1676. switch (usart->info->mode) {
  1677. case ARM_USART_MODE_SMART_CARD:
  1678. // Pin function = USART TX
  1679. val |= SCU_PIN_CFG_MODE(usart->pins.tx->config_val);
  1680. break;
  1681. default:
  1682. // Synchronous master/slave, asynchronous, single-wire and IrDA mode
  1683. if (usart->info->flags & USART_FLAG_TX_ENABLED) {
  1684. // Pin function = USART TX
  1685. val |= SCU_PIN_CFG_MODE(usart->pins.tx->config_val);
  1686. } else {
  1687. // Pin function = GPIO
  1688. val |= SCU_PIN_CFG_MODE(SCU_CFG_MODE_FUNC0);
  1689. }
  1690. }
  1691. SCU_PinConfigure(usart->pins.tx->port, usart->pins.tx->num, val);
  1692. // Configure RX pin regarding mode and receiver state
  1693. val = SCU_PIN_CFG_INPUT_FILTER_DIS;
  1694. switch (usart->info->mode) {
  1695. case ARM_USART_MODE_SINGLE_WIRE:
  1696. case ARM_USART_MODE_SMART_CARD:
  1697. // Pin function = GPIO
  1698. val |= SCU_PIN_CFG_MODE(SCU_CFG_MODE_FUNC0);
  1699. break;
  1700. default:
  1701. // Synchronous master/slave, asynchronous and IrDA mode
  1702. if (usart->info->flags & USART_FLAG_RX_ENABLED) {
  1703. // Pin function = USART RX
  1704. val |= SCU_PIN_CFG_INPUT_BUFFER_EN |
  1705. SCU_PIN_CFG_MODE(usart->pins.rx->config_val);
  1706. } else {
  1707. // Pin function = GPIO
  1708. val |= SCU_PIN_CFG_MODE(SCU_CFG_MODE_FUNC0);
  1709. }
  1710. break;
  1711. }
  1712. SCU_PinConfigure(usart->pins.rx->port, usart->pins.rx->num, val);
  1713. // Configure CLK pin regarding mode
  1714. if (usart->pins.clk) {
  1715. val = SCU_PIN_CFG_INPUT_FILTER_DIS;
  1716. switch (usart->info->mode) {
  1717. case ARM_USART_MODE_SMART_CARD:
  1718. case ARM_USART_MODE_SYNCHRONOUS_MASTER:
  1719. // Pin function = USART UCLK (output)
  1720. val |= SCU_PIN_CFG_MODE(usart->pins.clk->config_val);
  1721. break;
  1722. case ARM_USART_MODE_SYNCHRONOUS_SLAVE:
  1723. // Pin function = USART UCLK (input)
  1724. val |= SCU_PIN_CFG_INPUT_BUFFER_EN |
  1725. SCU_PIN_CFG_MODE(usart->pins.clk->config_val);
  1726. break;
  1727. default:
  1728. // Asynchronous, Single-wire and IrDA mode
  1729. // Pin function = GPIO
  1730. val |= SCU_PIN_CFG_INPUT_BUFFER_EN |
  1731. SCU_PIN_CFG_MODE(SCU_CFG_MODE_FUNC0);
  1732. }
  1733. SCU_PinConfigure(usart->pins.clk->port, usart->pins.clk->num, val);
  1734. }
  1735. // Configure SYNCCRTL register (only in synchronous mode)
  1736. if (usart->capabilities.synchronous_master ||
  1737. usart->capabilities.synchronous_slave) {
  1738. usart->reg->SYNCCTRL = USART_SYNCCTRL_FES |
  1739. USART_SYNCCTRL_SSSDIS |
  1740. syncctrl;
  1741. }
  1742. // Configure HDEN register (only in single wire mode)
  1743. if (usart->capabilities.single_wire) {
  1744. usart->reg->HDEN = hden;
  1745. }
  1746. // Configure ICR register (only in IrDA mode)
  1747. if (usart->capabilities.irda) {
  1748. usart->reg->ICR = (usart->reg->ICR & ~USART_ICR_IRDAEN) | icr;
  1749. }
  1750. // Configure SCICTRL register (only in Smart Card mode)
  1751. if (usart->capabilities.smart_card) {
  1752. usart->reg->SCICTRL = (usart->reg->SCICTRL & ~USART_SCICTRL_SCIEN) |
  1753. scictrl;
  1754. }
  1755. // Configure MCR register (modem line for USART1)
  1756. if (usart->uart_reg) {
  1757. usart->uart_reg->MCR = ((usart->uart_reg->MCR & ~(UART_MCR_RTSEN |
  1758. UART_MCR_CTSEN))) | mcr;
  1759. }
  1760. // Configure Line control register
  1761. usart->reg->LCR = ((usart->reg->LCR & (USART_LCR_BC | USART_LCR_DLAB)) | lcr);
  1762. // Set configured flag
  1763. usart->info->flags |= USART_FLAG_CONFIGURED;
  1764. return ARM_DRIVER_OK;
  1765. }
  1766. /**
  1767. \fn ARM_USART_STATUS USART_GetStatus (USART_RESOURCES *usart)
  1768. \brief Get USART status.
  1769. \param[in] usart Pointer to USART resources
  1770. \return USART status \ref ARM_USART_STATUS
  1771. */
  1772. static ARM_USART_STATUS USART_GetStatus (USART_RESOURCES *usart) {
  1773. ARM_USART_STATUS stat;
  1774. stat.tx_busy = (usart->reg->LSR & USART_LSR_TEMT ? (0U) : (1U));
  1775. stat.rx_busy = usart->info->rx_status.rx_busy;
  1776. stat.tx_underflow = 0U;
  1777. stat.rx_overflow = usart->info->rx_status.rx_overflow;
  1778. stat.rx_break = usart->info->rx_status.rx_break;
  1779. stat.rx_framing_error = usart->info->rx_status.rx_framing_error;
  1780. stat.rx_parity_error = usart->info->rx_status.rx_parity_error;
  1781. return stat;
  1782. }
  1783. /**
  1784. \fn int32_t USART_SetModemControl (ARM_USART_MODEM_CONTROL control,
  1785. USART_RESOURCES *usart)
  1786. \brief Set USART Modem Control line state.
  1787. \param[in] control \ref ARM_USART_MODEM_CONTROL
  1788. \param[in] usart Pointer to USART resources
  1789. \return \ref execution_status
  1790. */
  1791. static int32_t USART_SetModemControl (ARM_USART_MODEM_CONTROL control,
  1792. USART_RESOURCES *usart) {
  1793. if ((usart->info->flags & USART_FLAG_CONFIGURED) == 0U) {
  1794. // USART is not configured
  1795. return ARM_DRIVER_ERROR;
  1796. }
  1797. // Only UART1 supports modem lines
  1798. if (usart->uart_reg == NULL) { return ARM_DRIVER_ERROR_UNSUPPORTED; }
  1799. if (control == ARM_USART_RTS_CLEAR) {
  1800. if (usart->capabilities.rts) { usart->uart_reg->MCR &= ~UART_MCR_RTSCTRL; }
  1801. else { return ARM_DRIVER_ERROR_UNSUPPORTED; }
  1802. }
  1803. if (control == ARM_USART_RTS_SET) {
  1804. if (usart->capabilities.rts) { usart->uart_reg->MCR |= UART_MCR_RTSCTRL; }
  1805. else {return ARM_DRIVER_ERROR_UNSUPPORTED; }
  1806. }
  1807. if (control == ARM_USART_DTR_CLEAR) {
  1808. if (usart->capabilities.dtr) { usart->uart_reg->MCR &= ~UART_MCR_DTRCTRL; }
  1809. else { return ARM_DRIVER_ERROR_UNSUPPORTED; }
  1810. }
  1811. if (control == ARM_USART_DTR_SET) {
  1812. if (usart->capabilities.dtr) { usart->uart_reg->MCR |= UART_MCR_DTRCTRL; }
  1813. else { return ARM_DRIVER_ERROR_UNSUPPORTED; }
  1814. }
  1815. return ARM_DRIVER_OK;
  1816. }
  1817. /**
  1818. \fn ARM_USART_MODEM_STATUS USART_GetModemStatus (USART_RESOURCES *usart)
  1819. \brief Get USART Modem Status lines state.
  1820. \param[in] usart Pointer to USART resources
  1821. \return modem status \ref ARM_USART_MODEM_STATUS
  1822. */
  1823. static ARM_USART_MODEM_STATUS USART_GetModemStatus (USART_RESOURCES *usart) {
  1824. ARM_USART_MODEM_STATUS modem_status;
  1825. uint32_t msr;
  1826. if (usart->uart_reg &&
  1827. (usart->info->flags & USART_FLAG_CONFIGURED)) {
  1828. msr = usart->uart_reg->MSR;
  1829. if (usart->capabilities.cts) { modem_status.cts = (msr & UART_MSR_CTS ? (1U) : (0U)); }
  1830. else { modem_status.cts = 0U; }
  1831. if (usart->capabilities.dsr) { modem_status.dsr = (msr & UART_MSR_DSR ? (1U) : (0U)); }
  1832. else { modem_status.dsr = 0U; }
  1833. if (usart->capabilities.ri ) { modem_status.ri = (msr & UART_MSR_RI ? (1U) : (0U)); }
  1834. else { modem_status.ri = 0U; }
  1835. if (usart->capabilities.dcd) { modem_status.dcd = (msr & UART_MSR_DCD ? (1U) : (0U)); }
  1836. else { modem_status.dcd = 0U; }
  1837. } else {
  1838. modem_status.cts = 0U;
  1839. modem_status.dsr = 0U;
  1840. modem_status.ri = 0U;
  1841. modem_status.dcd = 0U;
  1842. }
  1843. return modem_status;
  1844. }
  1845. /**
  1846. \fn void USART_IRQHandler (UART_RESOURCES *usart)
  1847. \brief USART Interrupt handler.
  1848. \param[in] usart Pointer to USART resources
  1849. */
  1850. static void USART_IRQHandler (USART_RESOURCES *usart) {
  1851. uint32_t iir, event, val, i;
  1852. event = 0U;
  1853. iir = usart->reg->IIR;
  1854. if ((iir & USART_IIR_INTSTATUS) == 0U) {
  1855. // Transmit holding register empty
  1856. if ((iir & USART_IIR_INTID_MSK) == USART_IIR_INTID_THRE) {
  1857. val = 16U;
  1858. while ((val --) && (usart->info->xfer.tx_num != usart->info->xfer.tx_cnt)) {
  1859. if (((usart->info->mode == ARM_USART_MODE_SYNCHRONOUS_MASTER) ||
  1860. (usart->info->mode == ARM_USART_MODE_SYNCHRONOUS_SLAVE )) &&
  1861. (usart->info->xfer.sync_mode == USART_SYNC_MODE_RX)) {
  1862. // Dummy write in synchronous receive only mode
  1863. usart->reg->THR = usart->info->xfer.tx_def_val;
  1864. } else {
  1865. // Write data to Tx FIFO
  1866. usart->reg->THR = usart->info->xfer.tx_buf[usart->info->xfer.tx_cnt];
  1867. }
  1868. usart->info->xfer.tx_cnt++;
  1869. }
  1870. // Check if all data is transmitted
  1871. if (usart->info->xfer.tx_num == usart->info->xfer.tx_cnt) {
  1872. // Disable THRE interrupt
  1873. usart->reg->IER &= ~USART_IER_THREIE;
  1874. // Clear TX busy flag
  1875. usart->info->xfer.send_active = 0U;
  1876. // Set send complete event
  1877. if ((usart->info->mode == ARM_USART_MODE_SYNCHRONOUS_MASTER) ||
  1878. (usart->info->mode == ARM_USART_MODE_SYNCHRONOUS_SLAVE )) {
  1879. if ((usart->info->xfer.sync_mode == USART_SYNC_MODE_TX) &&
  1880. ((usart->info->flags & USART_FLAG_RX_ENABLED) == 0U)) {
  1881. event |= ARM_USART_EVENT_SEND_COMPLETE;
  1882. }
  1883. } else {
  1884. event |= ARM_USART_EVENT_SEND_COMPLETE;
  1885. }
  1886. }
  1887. }
  1888. // Receive line status
  1889. if ((iir & USART_IIR_INTID_MSK) == USART_IIR_INTID_RLS) {
  1890. event |= USART_RxLineIntHandler(usart);
  1891. }
  1892. // Receive data available and Character time-out indicator interrupt
  1893. if (((iir & USART_IIR_INTID_MSK) == USART_IIR_INTID_RDA) ||
  1894. ((iir & USART_IIR_INTID_MSK) == USART_IIR_INTID_CTI)) {
  1895. switch (usart->trig_lvl) {
  1896. case USART_TRIG_LVL_1: i = 1U; break;
  1897. case USART_TRIG_LVL_4: i = 3U; break;
  1898. case USART_TRIG_LVL_8: i = 7U; break;
  1899. case USART_TRIG_LVL_14: i = 13U; break;
  1900. }
  1901. // Get available data from RX FIFO
  1902. while ((usart->reg->LSR & USART_LSR_RDR) && (i--)) {
  1903. // Check RX line interrupt for errors
  1904. event |= USART_RxLineIntHandler (usart);
  1905. if (((usart->info->mode == ARM_USART_MODE_SYNCHRONOUS_MASTER) ||
  1906. (usart->info->mode == ARM_USART_MODE_SYNCHRONOUS_SLAVE )) &&
  1907. (usart->info->xfer.sync_mode == USART_SYNC_MODE_TX)) {
  1908. // Dummy read in synchronous transmit only mode
  1909. usart->reg->RBR;
  1910. } else {
  1911. // Read data from RX FIFO into receive buffer
  1912. usart->info->xfer.rx_buf[usart->info->xfer.rx_cnt] = usart->reg->RBR;
  1913. }
  1914. usart->info->xfer.rx_cnt++;
  1915. // Check if requested amount of data is received
  1916. if (usart->info->xfer.rx_cnt == usart->info->xfer.rx_num) {
  1917. // Disable RDA interrupt
  1918. usart->reg->IER &= ~USART_IER_RBRIE;
  1919. // Clear RX busy flag and set receive transfer complete event
  1920. usart->info->rx_status.rx_busy = 0U;
  1921. if ((usart->info->mode == ARM_USART_MODE_SYNCHRONOUS_MASTER) ||
  1922. (usart->info->mode == ARM_USART_MODE_SYNCHRONOUS_SLAVE )) {
  1923. val = usart->info->xfer.sync_mode;
  1924. usart->info->xfer.sync_mode = 0U;
  1925. switch (val) {
  1926. case USART_SYNC_MODE_TX:
  1927. event |= ARM_USART_EVENT_SEND_COMPLETE;
  1928. break;
  1929. case USART_SYNC_MODE_RX:
  1930. event |= ARM_USART_EVENT_RECEIVE_COMPLETE;
  1931. break;
  1932. case USART_SYNC_MODE_TX_RX:
  1933. event |= ARM_USART_EVENT_TRANSFER_COMPLETE;
  1934. break;
  1935. default: break;
  1936. }
  1937. } else {
  1938. event |= ARM_USART_EVENT_RECEIVE_COMPLETE;
  1939. }
  1940. break;
  1941. }
  1942. }
  1943. }
  1944. // Character time-out indicator
  1945. if ((iir & USART_IIR_INTID_MSK) == USART_IIR_INTID_CTI) {
  1946. if ((usart->info->mode != ARM_USART_MODE_SYNCHRONOUS_MASTER) &&
  1947. (usart->info->mode != ARM_USART_MODE_SYNCHRONOUS_SLAVE )) {
  1948. // Signal RX Time-out event, if not all requested data received
  1949. if (usart->info->xfer.rx_cnt != usart->info->xfer.rx_num) {
  1950. event |= ARM_USART_EVENT_RX_TIMEOUT;
  1951. }
  1952. }
  1953. }
  1954. // Modem interrupt (UART1 only)
  1955. #if (RTE_UART1)
  1956. if (usart->uart_reg) {
  1957. if ((iir & USART_IIR_INTID_MSK) == UART_IIR_INTID_MS) {
  1958. // Save modem status register
  1959. val = usart->uart_reg->MSR;
  1960. // CTS state changed
  1961. if ((usart->capabilities.cts) && (val & UART_MSR_DCTS)) {
  1962. event |= ARM_USART_EVENT_CTS;
  1963. }
  1964. // DSR state changed
  1965. if ((usart->capabilities.dsr) && (val & UART_MSR_DDSR)) {
  1966. event |= ARM_USART_EVENT_DSR;
  1967. }
  1968. // Ring indicator
  1969. if ((usart->capabilities.ri) && (val & UART_MSR_TERI)) {
  1970. event |= ARM_USART_EVENT_RI;
  1971. }
  1972. // DCD state changed
  1973. if ((usart->capabilities.dcd) && (val & UART_MSR_DDCD)) {
  1974. event |= ARM_USART_EVENT_DCD;
  1975. }
  1976. }
  1977. }
  1978. #endif
  1979. }
  1980. if ((usart->info->cb_event != NULL) && (event != 0U)) {
  1981. usart->info->cb_event (event);
  1982. }
  1983. }
  1984. #if (((RTE_USART0) && (RTE_USART0_DMA_TX_EN == 1)) || \
  1985. ((RTE_UART1) && (RTE_UART1_DMA_TX_EN == 1)) || \
  1986. ((RTE_USART2) && (RTE_USART2_DMA_TX_EN == 1)) || \
  1987. ((RTE_USART3) && (RTE_USART3_DMA_TX_EN == 1)))
  1988. /**
  1989. \fn void USART_GPDMA_Tx_Event (uint32_t event, USART_RESOURCES *usart)
  1990. \brief UART Interrupt handler.
  1991. \param[in] usart Pointer to USART resources
  1992. \param[in] event GPDMA_EVENT_TERMINAL_COUNT_REQUEST / GPDMA_EVENT_ERROR
  1993. */
  1994. static void USART_GPDMA_Tx_Event (uint32_t event, USART_RESOURCES *usart) {
  1995. switch (event) {
  1996. case GPDMA_EVENT_TERMINAL_COUNT_REQUEST:
  1997. usart->info->xfer.tx_cnt = usart->info->xfer.tx_num;
  1998. // Clear TX busy flag
  1999. usart->info->xfer.send_active = 0U;
  2000. // Set Send Complete event for asynchronous transfers
  2001. if ((usart->info->mode != ARM_USART_MODE_SYNCHRONOUS_MASTER) &&
  2002. (usart->info->mode != ARM_USART_MODE_SYNCHRONOUS_SLAVE )) {
  2003. if (usart->info->cb_event) {
  2004. usart->info->cb_event (ARM_USART_EVENT_SEND_COMPLETE);
  2005. }
  2006. }
  2007. break;
  2008. case GPDMA_EVENT_ERROR:
  2009. default:
  2010. break;
  2011. }
  2012. }
  2013. #endif
  2014. #if (((RTE_USART0) && (RTE_USART0_DMA_RX_EN == 1)) || \
  2015. ((RTE_UART1) && (RTE_UART1_DMA_RX_EN == 1)) || \
  2016. ((RTE_USART2) && (RTE_USART2_DMA_RX_EN == 1)) || \
  2017. ((RTE_USART3) && (RTE_USART3_DMA_RX_EN == 1)))
  2018. /**
  2019. \fn void USART_GPDMA_Rx_Event (uint32_t event, USART_RESOURCES *usart)
  2020. \brief UART Interrupt handler.
  2021. \param[in] event GPDMA_EVENT_TERMINAL_COUNT_REQUEST / GPDMA_EVENT_ERROR
  2022. \param[in] usart Pointer to USART resources
  2023. */
  2024. static void USART_GPDMA_Rx_Event (uint32_t event, USART_RESOURCES *usart) {
  2025. uint32_t val, evt;
  2026. evt = 0U;
  2027. switch (event) {
  2028. case GPDMA_EVENT_TERMINAL_COUNT_REQUEST:
  2029. usart->info->xfer.rx_cnt = usart->info->xfer.rx_num;
  2030. usart->info->rx_status.rx_busy = 0U;
  2031. if ((usart->info->mode == ARM_USART_MODE_SYNCHRONOUS_MASTER) ||
  2032. (usart->info->mode == ARM_USART_MODE_SYNCHRONOUS_SLAVE )) {
  2033. val = usart->info->xfer.sync_mode;
  2034. usart->info->xfer.sync_mode = 0U;
  2035. switch (val) {
  2036. case USART_SYNC_MODE_TX:
  2037. evt |= ARM_USART_EVENT_SEND_COMPLETE;
  2038. break;
  2039. case USART_SYNC_MODE_RX:
  2040. evt |= ARM_USART_EVENT_RECEIVE_COMPLETE;
  2041. break;
  2042. case USART_SYNC_MODE_TX_RX:
  2043. evt |= ARM_USART_EVENT_TRANSFER_COMPLETE;
  2044. break;
  2045. default: break;
  2046. }
  2047. } else {
  2048. evt |= ARM_USART_EVENT_RECEIVE_COMPLETE;
  2049. }
  2050. if ((usart->info->cb_event != NULL) && (evt != 0U)) {
  2051. usart->info->cb_event (evt);
  2052. }
  2053. break;
  2054. case GPDMA_EVENT_ERROR:
  2055. default:
  2056. break;
  2057. }
  2058. }
  2059. #endif
  2060. #if (RTE_USART0)
  2061. // USART0 Driver Wrapper functions
  2062. static ARM_USART_CAPABILITIES USART0_GetCapabilities (void) {
  2063. return USART_GetCapabilities (&USART0_Resources);
  2064. }
  2065. static int32_t USART0_Initialize (ARM_USART_SignalEvent_t cb_event) {
  2066. return USART_Initialize (cb_event, &USART0_Resources);
  2067. }
  2068. static int32_t USART0_Uninitialize (void) {
  2069. return USART_Uninitialize(&USART0_Resources);
  2070. }
  2071. static int32_t USART0_PowerControl (ARM_POWER_STATE state) {
  2072. return USART_PowerControl (state, &USART0_Resources);
  2073. }
  2074. static int32_t USART0_Send (const void *data, uint32_t num) {
  2075. return USART_Send (data, num, &USART0_Resources);
  2076. }
  2077. static int32_t USART0_Receive (void *data, uint32_t num) {
  2078. return USART_Receive (data, num, &USART0_Resources);
  2079. }
  2080. static int32_t USART0_Transfer (const void *data_out,
  2081. void *data_in,
  2082. uint32_t num) {
  2083. return USART_Transfer (data_out, data_in, num, &USART0_Resources);
  2084. }
  2085. static uint32_t USART0_GetTxCount (void) {
  2086. return USART_GetTxCount (&USART0_Resources);
  2087. }
  2088. static uint32_t USART0_GetRxCount (void) {
  2089. return USART_GetRxCount (&USART0_Resources);
  2090. }
  2091. static int32_t USART0_Control (uint32_t control, uint32_t arg) {
  2092. return USART_Control (control, arg, &USART0_Resources);
  2093. }
  2094. static ARM_USART_STATUS USART0_GetStatus (void) {
  2095. return USART_GetStatus (&USART0_Resources);
  2096. }
  2097. static int32_t USART0_SetModemControl (ARM_USART_MODEM_CONTROL control) {
  2098. return USART_SetModemControl (control, &USART0_Resources);
  2099. }
  2100. static ARM_USART_MODEM_STATUS USART0_GetModemStatus (void) {
  2101. return USART_GetModemStatus (&USART0_Resources);
  2102. }
  2103. void UART0_IRQHandler (void) {
  2104. USART_IRQHandler (&USART0_Resources);
  2105. }
  2106. #if (RTE_USART0_DMA_TX_EN == 1)
  2107. void USART0_GPDMA_Tx_Event (uint32_t event) {
  2108. USART_GPDMA_Tx_Event(event, &USART0_Resources);
  2109. }
  2110. #endif
  2111. #if (RTE_USART0_DMA_RX_EN == 1)
  2112. void USART0_GPDMA_Rx_Event (uint32_t event) {
  2113. USART_GPDMA_Rx_Event(event, &USART0_Resources);
  2114. }
  2115. #endif
  2116. // USART0 Driver Control Block
  2117. ARM_DRIVER_USART Driver_USART0 = {
  2118. USARTx_GetVersion,
  2119. USART0_GetCapabilities,
  2120. USART0_Initialize,
  2121. USART0_Uninitialize,
  2122. USART0_PowerControl,
  2123. USART0_Send,
  2124. USART0_Receive,
  2125. USART0_Transfer,
  2126. USART0_GetTxCount,
  2127. USART0_GetRxCount,
  2128. USART0_Control,
  2129. USART0_GetStatus,
  2130. USART0_SetModemControl,
  2131. USART0_GetModemStatus
  2132. };
  2133. #endif
  2134. #if (RTE_UART1)
  2135. // USART1 Driver Wrapper functions
  2136. static ARM_USART_CAPABILITIES USART1_GetCapabilities (void) {
  2137. return USART_GetCapabilities (&USART1_Resources);
  2138. }
  2139. static int32_t USART1_Initialize (ARM_USART_SignalEvent_t cb_event) {
  2140. return USART_Initialize (cb_event, &USART1_Resources);
  2141. }
  2142. static int32_t USART1_Uninitialize (void) {
  2143. return USART_Uninitialize(&USART1_Resources);
  2144. }
  2145. static int32_t USART1_PowerControl (ARM_POWER_STATE state) {
  2146. return USART_PowerControl (state, &USART1_Resources);
  2147. }
  2148. static int32_t USART1_Send (const void *data, uint32_t num) {
  2149. return USART_Send (data, num, &USART1_Resources);
  2150. }
  2151. static int32_t USART1_Receive (void *data, uint32_t num) {
  2152. return USART_Receive (data, num, &USART1_Resources);
  2153. }
  2154. static int32_t USART1_Transfer (const void *data_out,
  2155. void *data_in,
  2156. uint32_t num) {
  2157. return USART_Transfer (data_out, data_in, num, &USART1_Resources);
  2158. }
  2159. static uint32_t USART1_GetTxCount (void) {
  2160. return USART_GetTxCount (&USART1_Resources);
  2161. }
  2162. static uint32_t USART1_GetRxCount (void) {
  2163. return USART_GetRxCount (&USART1_Resources);
  2164. }
  2165. static int32_t USART1_Control (uint32_t control, uint32_t arg) {
  2166. return USART_Control (control, arg, &USART1_Resources);
  2167. }
  2168. static ARM_USART_STATUS USART1_GetStatus (void) {
  2169. return USART_GetStatus (&USART1_Resources);
  2170. }
  2171. static int32_t USART1_SetModemControl (ARM_USART_MODEM_CONTROL control) {
  2172. return USART_SetModemControl (control, &USART1_Resources);
  2173. }
  2174. static ARM_USART_MODEM_STATUS USART1_GetModemStatus (void) {
  2175. return USART_GetModemStatus (&USART1_Resources);
  2176. }
  2177. void UART1_IRQHandler (void) {
  2178. USART_IRQHandler (&USART1_Resources);
  2179. }
  2180. #if (RTE_UART1_DMA_TX_EN == 1)
  2181. void USART1_GPDMA_Tx_Event (uint32_t event) {
  2182. USART_GPDMA_Tx_Event(event, &USART1_Resources);
  2183. }
  2184. #endif
  2185. #if (RTE_UART1_DMA_RX_EN == 1)
  2186. void USART1_GPDMA_Rx_Event (uint32_t event) {
  2187. USART_GPDMA_Rx_Event(event, &USART1_Resources);
  2188. }
  2189. #endif
  2190. // USART1 Driver Control Block
  2191. ARM_DRIVER_USART Driver_USART1 = {
  2192. USARTx_GetVersion,
  2193. USART1_GetCapabilities,
  2194. USART1_Initialize,
  2195. USART1_Uninitialize,
  2196. USART1_PowerControl,
  2197. USART1_Send,
  2198. USART1_Receive,
  2199. USART1_Transfer,
  2200. USART1_GetTxCount,
  2201. USART1_GetRxCount,
  2202. USART1_Control,
  2203. USART1_GetStatus,
  2204. USART1_SetModemControl,
  2205. USART1_GetModemStatus
  2206. };
  2207. #endif
  2208. #if (RTE_USART2)
  2209. // USART2 Driver Wrapper functions
  2210. static ARM_USART_CAPABILITIES USART2_GetCapabilities (void) {
  2211. return USART_GetCapabilities (&USART2_Resources);
  2212. }
  2213. static int32_t USART2_Initialize (ARM_USART_SignalEvent_t cb_event) {
  2214. return USART_Initialize (cb_event, &USART2_Resources);
  2215. }
  2216. static int32_t USART2_Uninitialize (void) {
  2217. return USART_Uninitialize(&USART2_Resources);
  2218. }
  2219. static int32_t USART2_PowerControl (ARM_POWER_STATE state) {
  2220. return USART_PowerControl (state, &USART2_Resources);
  2221. }
  2222. static int32_t USART2_Send (const void *data, uint32_t num) {
  2223. return USART_Send (data, num, &USART2_Resources);
  2224. }
  2225. static int32_t USART2_Receive (void *data, uint32_t num) {
  2226. return USART_Receive (data, num, &USART2_Resources);
  2227. }
  2228. static int32_t USART2_Transfer (const void *data_out,
  2229. void *data_in,
  2230. uint32_t num) {
  2231. return USART_Transfer (data_out, data_in, num, &USART2_Resources);
  2232. }
  2233. static uint32_t USART2_GetTxCount (void) {
  2234. return USART_GetTxCount (&USART2_Resources);
  2235. }
  2236. static uint32_t USART2_GetRxCount (void) {
  2237. return USART_GetRxCount (&USART2_Resources);
  2238. }
  2239. static int32_t USART2_Control (uint32_t control, uint32_t arg) {
  2240. return USART_Control (control, arg, &USART2_Resources);
  2241. }
  2242. static ARM_USART_STATUS USART2_GetStatus (void) {
  2243. return USART_GetStatus (&USART2_Resources);
  2244. }
  2245. static int32_t USART2_SetModemControl (ARM_USART_MODEM_CONTROL control) {
  2246. return USART_SetModemControl (control, &USART2_Resources);
  2247. }
  2248. static ARM_USART_MODEM_STATUS USART2_GetModemStatus (void) {
  2249. return USART_GetModemStatus (&USART2_Resources);
  2250. }
  2251. void UART2_IRQHandler (void) {
  2252. USART_IRQHandler (&USART2_Resources);
  2253. }
  2254. #if (RTE_USART2_DMA_TX_EN == 1)
  2255. void USART2_GPDMA_Tx_Event (uint32_t event) {
  2256. USART_GPDMA_Tx_Event(event, &USART2_Resources);
  2257. }
  2258. #endif
  2259. #if (RTE_USART2_DMA_RX_EN == 1)
  2260. void USART2_GPDMA_Rx_Event (uint32_t event) {
  2261. USART_GPDMA_Rx_Event(event, &USART2_Resources);
  2262. }
  2263. #endif
  2264. // USART2 Driver Control Block
  2265. ARM_DRIVER_USART Driver_USART2 = {
  2266. USARTx_GetVersion,
  2267. USART2_GetCapabilities,
  2268. USART2_Initialize,
  2269. USART2_Uninitialize,
  2270. USART2_PowerControl,
  2271. USART2_Send,
  2272. USART2_Receive,
  2273. USART2_Transfer,
  2274. USART2_GetTxCount,
  2275. USART2_GetRxCount,
  2276. USART2_Control,
  2277. USART2_GetStatus,
  2278. USART2_SetModemControl,
  2279. USART2_GetModemStatus
  2280. };
  2281. #endif
  2282. #if (RTE_USART3)
  2283. // USART3 Driver Wrapper functions
  2284. static ARM_USART_CAPABILITIES USART3_GetCapabilities (void) {
  2285. return USART_GetCapabilities (&USART3_Resources);
  2286. }
  2287. static int32_t USART3_Initialize (ARM_USART_SignalEvent_t cb_event) {
  2288. return USART_Initialize (cb_event, &USART3_Resources);
  2289. }
  2290. static int32_t USART3_Uninitialize (void) {
  2291. return USART_Uninitialize(&USART3_Resources);
  2292. }
  2293. static int32_t USART3_PowerControl (ARM_POWER_STATE state) {
  2294. return USART_PowerControl (state, &USART3_Resources);
  2295. }
  2296. static int32_t USART3_Send (const void *data, uint32_t num) {
  2297. return USART_Send (data, num, &USART3_Resources);
  2298. }
  2299. static int32_t USART3_Receive (void *data, uint32_t num) {
  2300. return USART_Receive (data, num, &USART3_Resources);
  2301. }
  2302. static int32_t USART3_Transfer (const void *data_out,
  2303. void *data_in,
  2304. uint32_t num) {
  2305. return USART_Transfer (data_out, data_in, num, &USART3_Resources);
  2306. }
  2307. static uint32_t USART3_GetTxCount (void) {
  2308. return USART_GetTxCount (&USART3_Resources);
  2309. }
  2310. static uint32_t USART3_GetRxCount (void) {
  2311. return USART_GetRxCount (&USART3_Resources);
  2312. }
  2313. static int32_t USART3_Control (uint32_t control, uint32_t arg) {
  2314. return USART_Control (control, arg, &USART3_Resources);
  2315. }
  2316. static ARM_USART_STATUS USART3_GetStatus (void) {
  2317. return USART_GetStatus (&USART3_Resources);
  2318. }
  2319. static int32_t USART3_SetModemControl (ARM_USART_MODEM_CONTROL control) {
  2320. return USART_SetModemControl (control, &USART3_Resources);
  2321. }
  2322. static ARM_USART_MODEM_STATUS USART3_GetModemStatus (void) {
  2323. return USART_GetModemStatus (&USART3_Resources);
  2324. }
  2325. void UART3_IRQHandler (void) {
  2326. USART_IRQHandler (&USART3_Resources);
  2327. }
  2328. #if (RTE_USART3_DMA_TX_EN == 1)
  2329. void USART3_GPDMA_Tx_Event (uint32_t event) {
  2330. USART_GPDMA_Tx_Event(event, &USART3_Resources);
  2331. }
  2332. #endif
  2333. #if (RTE_USART3_DMA_RX_EN == 1)
  2334. void USART3_GPDMA_Rx_Event (uint32_t event) {
  2335. USART_GPDMA_Rx_Event(event, &USART3_Resources);
  2336. }
  2337. #endif
  2338. // USART3 Driver Control Block
  2339. ARM_DRIVER_USART Driver_USART3 = {
  2340. USARTx_GetVersion,
  2341. USART3_GetCapabilities,
  2342. USART3_Initialize,
  2343. USART3_Uninitialize,
  2344. USART3_PowerControl,
  2345. USART3_Send,
  2346. USART3_Receive,
  2347. USART3_Transfer,
  2348. USART3_GetTxCount,
  2349. USART3_GetRxCount,
  2350. USART3_Control,
  2351. USART3_GetStatus,
  2352. USART3_SetModemControl,
  2353. USART3_GetModemStatus
  2354. };
  2355. #endif