mss_uart.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  1. /*******************************************************************************
  2. * (c) Copyright 2011-2013 Microsemi SoC Products Group. All rights reserved.
  3. *
  4. * SmartFusion2 Microcontroller Subsystem MMUART bare metal software driver
  5. * implementation.
  6. *
  7. * SVN $Revision: 5610 $
  8. * SVN $Date: 2013-04-05 18:49:30 +0530 (Fri, 05 Apr 2013) $
  9. */
  10. #include "mss_uart.h"
  11. #include "mss_uart_regs.h"
  12. #include "../../CMSIS/mss_assert.h"
  13. #include "../../CMSIS/hw_reg_io.h"
  14. #include "../../CMSIS/system_m2sxxx.h"
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /*******************************************************************************
  19. * Defines
  20. */
  21. #define TX_COMPLETE 0u
  22. #define TX_FIFO_SIZE 16u
  23. #define FCR_TRIG_LEVEL_MASK 0xC0u
  24. #define IIRF_MASK 0x0Fu
  25. #define INVALID_INTERRUPT 0u
  26. #define INVALID_IRQ_HANDLER ((mss_uart_irq_handler_t) 0)
  27. #define NULL_HANDLER ((mss_uart_irq_handler_t) 0)
  28. #define MSS_UART_DATA_READY ((uint8_t) 0x01)
  29. #define SYNC_ASYNC_MODE_MASK (0x7u)
  30. /*******************************************************************************
  31. * Possible values for Interrupt Identification Register Field.
  32. */
  33. #define IIRF_MODEM_STATUS 0x00u
  34. #define IIRF_THRE 0x02u
  35. #define IIRF_MMI 0x03u
  36. #define IIRF_RX_DATA 0x04u
  37. #define IIRF_RX_LINE_STATUS 0x06u
  38. #define IIRF_DATA_TIMEOUT 0x0Cu
  39. /*******************************************************************************
  40. * Receiver error status mask.
  41. */
  42. #define STATUS_ERROR_MASK ( MSS_UART_OVERUN_ERROR | MSS_UART_PARITY_ERROR | \
  43. MSS_UART_FRAMING_ERROR | MSS_UART_BREAK_ERROR | \
  44. MSS_UART_FIFO_ERROR)
  45. /*******************************************************************************
  46. * Cortex-M3 interrupt handler functions implemented as part of the MSS UART
  47. * driver.
  48. */
  49. #if defined(__GNUC__)
  50. __attribute__((__interrupt__)) void UART0_IRQHandler(void);
  51. #else
  52. void UART0_IRQHandler(void);
  53. #endif
  54. #if defined(__GNUC__)
  55. __attribute__((__interrupt__)) void UART1_IRQHandler(void);
  56. #else
  57. void UART1_IRQHandler(void);
  58. #endif
  59. /*******************************************************************************
  60. * Local functions.
  61. */
  62. static void global_init(mss_uart_instance_t * this_uart, uint32_t baud_rate,
  63. uint8_t line_config);
  64. static void MSS_UART_isr(mss_uart_instance_t * this_uart);
  65. static void default_tx_handler(mss_uart_instance_t * this_uart);
  66. static void config_baud_divisors
  67. (
  68. mss_uart_instance_t * this_uart,
  69. uint32_t baudrate
  70. );
  71. /*******************************************************************************
  72. * Instance definitions
  73. */
  74. mss_uart_instance_t g_mss_uart0;
  75. mss_uart_instance_t g_mss_uart1;
  76. /*******************************************************************************
  77. * Public Functions
  78. *******************************************************************************/
  79. /***************************************************************************//**
  80. * See mss_uart.h for details of how to use this function.
  81. */
  82. void
  83. MSS_UART_init
  84. (
  85. mss_uart_instance_t* this_uart,
  86. uint32_t baud_rate,
  87. uint8_t line_config
  88. )
  89. {
  90. /* The driver expects g_mss_uart0 and g_mss_uart1 to be the only
  91. * mss_uart_instance_t instances used to identify UART0 and UART1. */
  92. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  93. /* Perform generic initialization */
  94. global_init(this_uart, baud_rate, line_config);
  95. /* Disable LIN mode */
  96. clear_bit_reg8(&this_uart->hw_reg->MM0, ELIN);
  97. /* Disable IrDA mode */
  98. clear_bit_reg8(&this_uart->hw_reg->MM1, EIRD);
  99. /* Disable SmartCard Mode */
  100. clear_bit_reg8(&this_uart->hw_reg->MM2, EERR);
  101. /* set default tx handler for automated TX using interrupt in USART mode */
  102. this_uart->tx_handler = default_tx_handler;
  103. }
  104. /***************************************************************************//**
  105. * See mss_uart.h for details of how to use this function.
  106. */
  107. void MSS_UART_lin_init
  108. (
  109. mss_uart_instance_t* this_uart,
  110. uint32_t baud_rate,
  111. uint8_t line_config
  112. )
  113. {
  114. /* The driver expects g_mss_uart0 and g_mss_uart1 to be the only
  115. * mss_uart_instance_t instances used to identify UART0 and UART1. */
  116. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  117. /* Perform generic initialization */
  118. global_init(this_uart, baud_rate, line_config);
  119. /* Enable LIN mode */
  120. set_bit_reg8(&this_uart->hw_reg->MM0, ELIN);
  121. /* Disable IrDA mode */
  122. clear_bit_reg8(&this_uart->hw_reg->MM1, EIRD);
  123. /* Disable SmartCard Mode */
  124. clear_bit_reg8(&this_uart->hw_reg->MM2, EERR);
  125. }
  126. /***************************************************************************//**
  127. * See mss_uart.h for details of how to use this function.
  128. */
  129. void
  130. MSS_UART_irda_init
  131. (
  132. mss_uart_instance_t* this_uart,
  133. uint32_t baud_rate,
  134. uint8_t line_config,
  135. mss_uart_rzi_polarity_t rxpol,
  136. mss_uart_rzi_polarity_t txpol,
  137. mss_uart_rzi_pulsewidth_t pw
  138. )
  139. {
  140. /* The driver expects g_mss_uart0 and g_mss_uart1 to be the only
  141. * mss_uart_instance_t instances used to identify UART0 and UART1. */
  142. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  143. /* Perform generic initialization */
  144. global_init(this_uart, baud_rate, line_config);
  145. /* Enable LIN mode */
  146. clear_bit_reg8(&this_uart->hw_reg->MM0, ELIN);
  147. /* Disable IrDA mode */
  148. set_bit_reg8(&this_uart->hw_reg->MM1, EIRD);
  149. ((rxpol == MSS_UART_ACTIVE_LOW) ? clear_bit_reg8(&this_uart->hw_reg->MM1,EIRX) :
  150. set_bit_reg8(&this_uart->hw_reg->MM1,EIRX));
  151. ((txpol == MSS_UART_ACTIVE_LOW) ? clear_bit_reg8(&this_uart->hw_reg->MM1,EITX) :
  152. set_bit_reg8(&this_uart->hw_reg->MM1,EITX));
  153. ((pw == MSS_UART_3_BY_16) ? clear_bit_reg8(&this_uart->hw_reg->MM1,EITP) :
  154. set_bit_reg8(&this_uart->hw_reg->MM1,EITP));
  155. /* Disable SmartCard Mode */
  156. clear_bit_reg8(&this_uart->hw_reg->MM2, EERR);
  157. }
  158. /***************************************************************************//**
  159. * See mss_uart.h for details of how to use this function.
  160. */
  161. void
  162. MSS_UART_smartcard_init
  163. (
  164. mss_uart_instance_t* this_uart,
  165. uint32_t baud_rate,
  166. uint8_t line_config
  167. )
  168. {
  169. /* The driver expects g_mss_uart0 and g_mss_uart1 to be the only
  170. * mss_uart_instance_t instances used to identify UART0 and UART1. */
  171. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  172. /* Perform generic initialization */
  173. global_init(this_uart, baud_rate, line_config);
  174. /* Disable LIN mode */
  175. clear_bit_reg8(&this_uart->hw_reg->MM0, ELIN);
  176. /* Disable IrDA mode */
  177. clear_bit_reg8(&this_uart->hw_reg->MM1, EIRD);
  178. /* Enable SmartCard Mode : Only when data is 8-bit and 2 stop bits*/
  179. if( ( MSS_UART_DATA_8_BITS | MSS_UART_TWO_STOP_BITS) ==
  180. (line_config & (MSS_UART_DATA_8_BITS | MSS_UART_TWO_STOP_BITS)))
  181. {
  182. set_bit_reg8(&this_uart->hw_reg->MM2, EERR);
  183. /* Enable single wire half-duplex mode */
  184. set_bit_reg8(&this_uart->hw_reg->MM2,ESWM);
  185. }
  186. }
  187. /***************************************************************************//**
  188. * See mss_uart.h for details of how to use this function.
  189. */
  190. void
  191. MSS_UART_polled_tx
  192. (
  193. mss_uart_instance_t * this_uart,
  194. const uint8_t * pbuff,
  195. uint32_t tx_size
  196. )
  197. {
  198. uint32_t char_idx = 0u;
  199. uint32_t size_sent;
  200. uint8_t status;
  201. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  202. ASSERT(pbuff != ( (uint8_t *)0));
  203. ASSERT(tx_size > 0u);
  204. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
  205. (pbuff != ((uint8_t *)0)) && (tx_size > 0u))
  206. {
  207. /* Remain in this loop until the entire input buffer
  208. * has been transferred to the UART.
  209. */
  210. do {
  211. /* Read the Line Status Register and update the sticky record */
  212. status = this_uart->hw_reg->LSR;
  213. this_uart->status |= status;
  214. /* Check if TX FIFO is empty. */
  215. if(status & MSS_UART_THRE)
  216. {
  217. uint32_t fill_size = TX_FIFO_SIZE;
  218. /* Calculate the number of bytes to transmit. */
  219. if(tx_size < TX_FIFO_SIZE)
  220. {
  221. fill_size = tx_size;
  222. }
  223. /* Fill the TX FIFO with the calculated the number of bytes. */
  224. for(size_sent = 0u; size_sent < fill_size; ++size_sent)
  225. {
  226. /* Send next character in the buffer. */
  227. this_uart->hw_reg->THR = pbuff[char_idx];
  228. char_idx++;
  229. }
  230. /* Calculate the number of untransmitted bytes remaining. */
  231. tx_size -= size_sent;
  232. }
  233. } while(tx_size);
  234. }
  235. }
  236. /***************************************************************************//**
  237. * See mss_uart.h for details of how to use this function.
  238. */
  239. void
  240. MSS_UART_polled_tx_string
  241. (
  242. mss_uart_instance_t * this_uart,
  243. const uint8_t * p_sz_string
  244. )
  245. {
  246. uint32_t char_idx = 0u;
  247. uint32_t fill_size;
  248. uint8_t data_byte;
  249. uint8_t status;
  250. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  251. ASSERT(p_sz_string != ((uint8_t *)0));
  252. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
  253. (p_sz_string != ((uint8_t *)0)))
  254. {
  255. /* Get the first data byte from the input buffer */
  256. data_byte = p_sz_string[char_idx];
  257. /* First check for the NULL terminator byte.
  258. * Then remain in this loop until the entire string in the input buffer
  259. * has been transferred to the UART.
  260. */
  261. while(0u != data_byte)
  262. {
  263. /* Wait until TX FIFO is empty. */
  264. do {
  265. status = this_uart->hw_reg->LSR;
  266. this_uart->status |= status;
  267. } while (0u == (status & MSS_UART_THRE));
  268. /* Send bytes from the input buffer until the TX FIFO is full
  269. * or we reach the NULL terminator byte.
  270. */
  271. fill_size = 0u;
  272. while((0u != data_byte) && (fill_size < TX_FIFO_SIZE))
  273. {
  274. /* Send the data byte */
  275. this_uart->hw_reg->THR = data_byte;
  276. ++fill_size;
  277. char_idx++;
  278. /* Get the next data byte from the input buffer */
  279. data_byte = p_sz_string[char_idx];
  280. }
  281. }
  282. }
  283. }
  284. /***************************************************************************//**
  285. * See mss_uart.h for details of how to use this function.
  286. */
  287. void
  288. MSS_UART_irq_tx
  289. (
  290. mss_uart_instance_t * this_uart,
  291. const uint8_t * pbuff,
  292. uint32_t tx_size
  293. )
  294. {
  295. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  296. ASSERT(pbuff != ((uint8_t *)0));
  297. ASSERT(tx_size > 0u);
  298. if((tx_size > 0u) && ( pbuff != ((uint8_t *)0)) &&
  299. ((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)))
  300. {
  301. /*Initialise the transmit info for the UART instance with the arguments.*/
  302. this_uart->tx_buffer = pbuff;
  303. this_uart->tx_buff_size = tx_size;
  304. this_uart->tx_idx = (uint16_t)0;
  305. /* Clear any previously pended interrupts */
  306. NVIC_ClearPendingIRQ(this_uart->irqn);
  307. /* assign default handler for data transfer */
  308. this_uart->tx_handler = default_tx_handler;
  309. /* enables TX interrupt */
  310. set_bit_reg8(&this_uart->hw_reg->IER,ETBEI);
  311. /* Enable UART instance interrupt in Cortex-M3 NVIC. */
  312. NVIC_EnableIRQ(this_uart->irqn);
  313. }
  314. }
  315. /***************************************************************************//**
  316. * See mss_uart.h for details of how to use this function.
  317. */
  318. int8_t
  319. MSS_UART_tx_complete
  320. (
  321. mss_uart_instance_t * this_uart
  322. )
  323. {
  324. int8_t ret_value = 0;
  325. uint8_t status = 0u;
  326. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  327. if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
  328. {
  329. /* Read the Line Status Register and update the sticky record. */
  330. status = this_uart->hw_reg->LSR;
  331. this_uart->status |= status;
  332. if((TX_COMPLETE == this_uart->tx_buff_size) &&
  333. ((status & MSS_UART_TEMT) != 0u))
  334. {
  335. ret_value = (int8_t)1;
  336. }
  337. }
  338. return ret_value;
  339. }
  340. /***************************************************************************//**
  341. * See mss_uart.h for details of how to use this function.
  342. */
  343. size_t
  344. MSS_UART_get_rx
  345. (
  346. mss_uart_instance_t * this_uart,
  347. uint8_t * rx_buff,
  348. size_t buff_size
  349. )
  350. {
  351. size_t rx_size = 0u;
  352. uint8_t status = 0u;
  353. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  354. ASSERT(rx_buff != ((uint8_t *)0));
  355. ASSERT(buff_size > 0u);
  356. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
  357. (rx_buff != ((uint8_t *)0)) && (buff_size > 0u))
  358. {
  359. status = this_uart->hw_reg->LSR;
  360. this_uart->status |= status;
  361. while(((status & MSS_UART_DATA_READY) != 0u) &&
  362. (rx_size < buff_size))
  363. {
  364. rx_buff[rx_size] = this_uart->hw_reg->RBR;
  365. ++rx_size;
  366. status = this_uart->hw_reg->LSR;
  367. this_uart->status |= status;
  368. }
  369. }
  370. return rx_size;
  371. }
  372. /***************************************************************************//**
  373. * See mss_uart.h for details of how to use this function.
  374. */
  375. void
  376. MSS_UART_enable_irq
  377. (
  378. mss_uart_instance_t * this_uart,
  379. mss_uart_irq_t irq_mask
  380. )
  381. {
  382. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  383. ASSERT(MSS_UART_INVALID_IRQ > irq_mask);
  384. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
  385. (MSS_UART_INVALID_IRQ > irq_mask))
  386. {
  387. /* Clear any previously pended interrupts */
  388. NVIC_ClearPendingIRQ(this_uart->irqn);
  389. /* irq_mask encoding: 1- enable
  390. * bit 0 - Receive Data Available Interrupt
  391. * bit 1 - Transmitter Holding Register Empty Interrupt
  392. * bit 2 - Receiver Line Status Interrupt
  393. * bit 3 - Modem Status Interrupt
  394. */
  395. this_uart->hw_reg->IER |= (uint8_t)irq_mask & IIRF_MASK;
  396. /*
  397. * bit 4 - Receiver time-out interrupt
  398. * bit 5 - NACK / ERR signal interrupt
  399. * bit 6 - PID parity error interrupt
  400. * bit 7 - LIN break detection interrupt
  401. * bit 8 - LIN Sync detection interrupt
  402. */
  403. this_uart->hw_reg->IEM |= (uint8_t)(((uint32_t)irq_mask & ~((uint32_t)IIRF_MASK)) >> 4u);
  404. /* Enable UART instance interrupt in Cortex-M3 NVIC. */
  405. NVIC_EnableIRQ(this_uart->irqn);
  406. }
  407. }
  408. /***************************************************************************//**
  409. * See mss_uart.h for details of how to use this function.
  410. */
  411. void
  412. MSS_UART_disable_irq
  413. (
  414. mss_uart_instance_t * this_uart,
  415. mss_uart_irq_t irq_mask
  416. )
  417. {
  418. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  419. if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
  420. {
  421. /* irq_mask encoding: 1 - disable
  422. * bit 0 - Receive Data Available Interrupt
  423. * bit 1 - Transmitter Holding Register Empty Interrupt
  424. * bit 2 - Receiver Line Status Interrupt
  425. * bit 3 - Modem Status Interrupt
  426. */
  427. this_uart->hw_reg->IER &= ((uint8_t)(~((uint32_t)irq_mask & (uint32_t)IIRF_MASK)));
  428. /*
  429. * bit 4 - Receiver time-out interrupt
  430. * bit 5 - NACK / ERR signal interrupt
  431. * bit 6 - PID parity error interrupt
  432. * bit 7 - LIN break detection interrupt
  433. * bit 8 - LIN Sync detection interrupt
  434. */
  435. this_uart->hw_reg->IEM |= (uint8_t)(~(((uint32_t)irq_mask & ~((uint32_t)IIRF_MASK)) >> 8u));
  436. /* Clear any previously pended interrupts */
  437. NVIC_ClearPendingIRQ(this_uart->irqn);
  438. if(irq_mask == IIRF_MASK)
  439. {
  440. /* Disable UART instance interrupt in Cortex-M3 NVIC. */
  441. NVIC_DisableIRQ(this_uart->irqn);
  442. }
  443. }
  444. }
  445. /***************************************************************************//**
  446. * See mss_uart.h for details of how to use this function.
  447. */
  448. void
  449. MSS_UART_set_rx_handler
  450. (
  451. mss_uart_instance_t * this_uart,
  452. mss_uart_irq_handler_t handler,
  453. mss_uart_rx_trig_level_t trigger_level
  454. )
  455. {
  456. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  457. ASSERT(handler != INVALID_IRQ_HANDLER );
  458. ASSERT(trigger_level < MSS_UART_FIFO_INVALID_TRIG_LEVEL);
  459. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
  460. (handler != INVALID_IRQ_HANDLER) &&
  461. (trigger_level < MSS_UART_FIFO_INVALID_TRIG_LEVEL))
  462. {
  463. this_uart->rx_handler = handler;
  464. /* Set the receive interrupt trigger level. */
  465. this_uart->hw_reg->FCR = (this_uart->hw_reg->FCR &
  466. (uint8_t)(~((uint8_t)FCR_TRIG_LEVEL_MASK))) |
  467. (uint8_t)trigger_level;
  468. /* Clear any previously pended interrupts */
  469. NVIC_ClearPendingIRQ(this_uart->irqn);
  470. /* Enable receive interrupt. */
  471. set_bit_reg8(&this_uart->hw_reg->IER,ERBFI);
  472. /* Enable UART instance interrupt in Cortex-M3 NVIC. */
  473. NVIC_EnableIRQ(this_uart->irqn);
  474. }
  475. }
  476. /***************************************************************************//**
  477. * See mss_uart.h for details of how to use this function.
  478. */
  479. void
  480. MSS_UART_set_loopback
  481. (
  482. mss_uart_instance_t * this_uart,
  483. mss_uart_loopback_t loopback
  484. )
  485. {
  486. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  487. ASSERT(MSS_UART_INVALID_LOOPBACK > loopback);
  488. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) ||
  489. (MSS_UART_INVALID_LOOPBACK > loopback))
  490. {
  491. switch(loopback)
  492. {
  493. case MSS_UART_LOCAL_LOOPBACK_OFF:
  494. /* Disable local loopback */
  495. clear_bit_reg8(&this_uart->hw_reg->MCR,LOOP);
  496. break;
  497. case MSS_UART_LOCAL_LOOPBACK_ON:
  498. /* Enable local loopback */
  499. set_bit_reg8(&this_uart->hw_reg->MCR,LOOP);
  500. break;
  501. case MSS_UART_REMOTE_LOOPBACK_OFF:
  502. case MSS_UART_AUTO_ECHO_OFF:
  503. /* Disable remote loopback & automatic echo*/
  504. this_uart->hw_reg->MCR &= ~RLOOP_MASK;
  505. break;
  506. case MSS_UART_REMOTE_LOOPBACK_ON:
  507. /* Enable remote loopback */
  508. this_uart->hw_reg->MCR |= (1u << RLOOP);
  509. break;
  510. case MSS_UART_AUTO_ECHO_ON:
  511. /* Enable automatic echo */
  512. this_uart->hw_reg->MCR |= (1u << ECHO);
  513. break;
  514. case MSS_UART_INVALID_LOOPBACK:
  515. /* Fall through to default. */
  516. default:
  517. ASSERT(0);
  518. break;
  519. }
  520. }
  521. }
  522. /***************************************************************************//**
  523. * UART0 interrupt service routine.
  524. * UART0_IRQHandler is included within the Cortex-M3 vector table as part of the
  525. * Fusion 2 CMSIS.
  526. */
  527. #if defined(__GNUC__)
  528. __attribute__((__interrupt__)) void UART0_IRQHandler(void)
  529. #else
  530. void UART0_IRQHandler(void)
  531. #endif
  532. {
  533. MSS_UART_isr(&g_mss_uart0);
  534. }
  535. /***************************************************************************//**
  536. * UART1 interrupt service routine.
  537. * UART2_IRQHandler is included within the Cortex-M3 vector table as part of the
  538. * Fusion 2 CMSIS.
  539. */
  540. #if defined(__GNUC__)
  541. __attribute__((__interrupt__)) void UART1_IRQHandler(void)
  542. #else
  543. void UART1_IRQHandler(void)
  544. #endif
  545. {
  546. MSS_UART_isr(&g_mss_uart1);
  547. }
  548. /***************************************************************************//**
  549. * See mss_uart.h for details of how to use this function.
  550. */
  551. void
  552. MSS_UART_set_rxstatus_handler
  553. (
  554. mss_uart_instance_t * this_uart,
  555. mss_uart_irq_handler_t handler
  556. )
  557. {
  558. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  559. ASSERT(handler != INVALID_IRQ_HANDLER);
  560. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
  561. (handler != INVALID_IRQ_HANDLER))
  562. {
  563. this_uart->linests_handler = handler;
  564. /* Clear any previously pended interrupts */
  565. NVIC_ClearPendingIRQ(this_uart->irqn);
  566. /* Enable receiver line status interrupt. */
  567. set_bit_reg8(&this_uart->hw_reg->IER,ELSI);
  568. /* Enable UART instance interrupt in Cortex-M3 NVIC. */
  569. NVIC_EnableIRQ(this_uart->irqn);
  570. }
  571. }
  572. /***************************************************************************//**
  573. * See mss_uart.h for details of how to use this function.
  574. */
  575. void
  576. MSS_UART_set_tx_handler
  577. (
  578. mss_uart_instance_t * this_uart,
  579. mss_uart_irq_handler_t handler
  580. )
  581. {
  582. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  583. ASSERT(handler != INVALID_IRQ_HANDLER);
  584. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
  585. (handler != INVALID_IRQ_HANDLER))
  586. {
  587. this_uart->tx_handler = handler;
  588. /* Make TX buffer info invalid */
  589. this_uart->tx_buffer = (const uint8_t *)0;
  590. this_uart->tx_buff_size = 0u;
  591. /* Clear any previously pended interrupts */
  592. NVIC_ClearPendingIRQ(this_uart->irqn);
  593. /* Enable transmitter holding register Empty interrupt. */
  594. set_bit_reg8(&this_uart->hw_reg->IER,ETBEI);
  595. /* Enable UART instance interrupt in Cortex-M3 NVIC. */
  596. NVIC_EnableIRQ(this_uart->irqn);
  597. }
  598. }
  599. /***************************************************************************//**
  600. * See mss_uart.h for details of how to use this function.
  601. */
  602. void
  603. MSS_UART_set_modemstatus_handler
  604. (
  605. mss_uart_instance_t * this_uart,
  606. mss_uart_irq_handler_t handler
  607. )
  608. {
  609. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  610. ASSERT(handler != INVALID_IRQ_HANDLER);
  611. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
  612. (handler != INVALID_IRQ_HANDLER))
  613. {
  614. this_uart->modemsts_handler = handler;
  615. /* Clear any previously pended interrupts */
  616. NVIC_ClearPendingIRQ(this_uart->irqn);
  617. /* Enable modem status interrupt. */
  618. set_bit_reg8(&this_uart->hw_reg->IER,EDSSI);
  619. /* Enable UART instance interrupt in Cortex-M3 NVIC. */
  620. NVIC_EnableIRQ(this_uart->irqn);
  621. }
  622. }
  623. /***************************************************************************//**
  624. * See mss_uart.h for details of how to use this function.
  625. */
  626. size_t
  627. MSS_UART_fill_tx_fifo
  628. (
  629. mss_uart_instance_t * this_uart,
  630. const uint8_t * tx_buffer,
  631. size_t tx_size
  632. )
  633. {
  634. uint8_t status = 0u;
  635. size_t size_sent = 0u;
  636. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  637. ASSERT(tx_buffer != ( (uint8_t *)0));
  638. ASSERT(tx_size > 0);
  639. /* Fill the UART's Tx FIFO until the FIFO is full or the complete input
  640. * buffer has been written. */
  641. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
  642. (tx_buffer != ((uint8_t *)0)) &&
  643. (tx_size > 0u))
  644. {
  645. status = this_uart->hw_reg->LSR;
  646. this_uart->status |= status;
  647. if(status & MSS_UART_THRE)
  648. {
  649. uint32_t fill_size = TX_FIFO_SIZE;
  650. if(tx_size < TX_FIFO_SIZE)
  651. {
  652. fill_size = tx_size;
  653. }
  654. /* Fill up FIFO */
  655. for(size_sent = 0u; size_sent < fill_size; ++size_sent)
  656. {
  657. /* Send next character in the buffer. */
  658. this_uart->hw_reg->THR = tx_buffer[size_sent];
  659. }
  660. }
  661. }
  662. return size_sent;
  663. }
  664. /***************************************************************************//**
  665. * See mss_uart.h for details of how to use this function.
  666. */
  667. uint8_t
  668. MSS_UART_get_rx_status
  669. (
  670. mss_uart_instance_t * this_uart
  671. )
  672. {
  673. uint8_t status = MSS_UART_INVALID_PARAM;
  674. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  675. if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
  676. {
  677. /*
  678. * Extract UART receive error status.
  679. * Bit 1 - Overflow error status
  680. * Bit 2 - Parity error status
  681. * Bit 3 - Frame error status
  682. * Bit 4 - Break interrupt indicator
  683. * Bit 7 - FIFO data error status
  684. */
  685. this_uart->status |= (this_uart->hw_reg->LSR);
  686. status = (this_uart->status & STATUS_ERROR_MASK);
  687. /* Clear the sticky status after reading */
  688. this_uart->status = 0u;
  689. }
  690. return status;
  691. }
  692. /***************************************************************************//**
  693. * See mss_uart.h for details of how to use this function.
  694. */
  695. uint8_t
  696. MSS_UART_get_modem_status
  697. (
  698. mss_uart_instance_t * this_uart
  699. )
  700. {
  701. uint8_t status = MSS_UART_INVALID_PARAM;
  702. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  703. if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
  704. {
  705. /*
  706. * Extract UART modem status and place in lower bits of "status".
  707. * Bit 0 - Delta Clear to Send Indicator
  708. * Bit 1 - Delta Clear to Receive Indicator
  709. * Bit 2 - Trailing edge of Ring Indicator detector
  710. * Bit 3 - Delta Data Carrier Detect indicator
  711. * Bit 4 - Clear To Send
  712. * Bit 5 - Data Set Ready
  713. * Bit 6 - Ring Indicator
  714. * Bit 7 - Data Carrier Detect
  715. */
  716. status = this_uart->hw_reg->MSR;
  717. }
  718. return status;
  719. }
  720. /***************************************************************************//**
  721. * MSS_UART_get_tx_status.
  722. * See mss_uart.h for details of how to use this function.
  723. */
  724. uint8_t
  725. MSS_UART_get_tx_status
  726. (
  727. mss_uart_instance_t * this_uart
  728. )
  729. {
  730. uint8_t status = MSS_UART_TX_BUSY;
  731. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  732. if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
  733. {
  734. /* Read the Line Status Register and update the sticky record. */
  735. status = this_uart->hw_reg->LSR;
  736. this_uart->status |= status;
  737. /*
  738. * Extract the transmit status bits from the UART's Line Status Register.
  739. * Bit 5 - Transmitter Holding Register/FIFO Empty (THRE) status. (If = 1, TX FIFO is empty)
  740. * Bit 6 - Transmitter Empty (TEMT) status. (If = 1, both TX FIFO and shift register are empty)
  741. */
  742. status &= (MSS_UART_THRE | MSS_UART_TEMT);
  743. }
  744. return status;
  745. }
  746. /***************************************************************************//**
  747. * See mss_uart.h for details of how to use this function.
  748. */
  749. void
  750. MSS_UART_set_break
  751. (
  752. mss_uart_instance_t * this_uart
  753. )
  754. {
  755. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  756. if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
  757. {
  758. /* set break charecter on Tx line */
  759. set_bit_reg8(&this_uart->hw_reg->LCR,SB);
  760. }
  761. }
  762. /***************************************************************************//**
  763. * See mss_uart.h for details of how to use this function.
  764. */
  765. void
  766. MSS_UART_clear_break
  767. (
  768. mss_uart_instance_t * this_uart
  769. )
  770. {
  771. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  772. if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
  773. {
  774. /* remove break charecter from Tx line */
  775. clear_bit_reg8(&this_uart->hw_reg->LCR,SB);
  776. }
  777. }
  778. /***************************************************************************//**
  779. * See mss_uart.h for details of how to use this function.
  780. */
  781. void
  782. MSS_UART_set_pidpei_handler
  783. (
  784. mss_uart_instance_t * this_uart,
  785. mss_uart_irq_handler_t handler
  786. )
  787. {
  788. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  789. ASSERT(handler != INVALID_IRQ_HANDLER);
  790. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
  791. (handler != INVALID_IRQ_HANDLER))
  792. {
  793. this_uart->pid_pei_handler = handler;
  794. /* Clear any previously pended interrupts */
  795. NVIC_ClearPendingIRQ( this_uart->irqn );
  796. /* Enable PID parity error interrupt. */
  797. set_bit_reg8(&this_uart->hw_reg->IEM,EPID_PEI);
  798. /* Enable UART instance interrupt in Cortex-M3 NVIC. */
  799. NVIC_EnableIRQ(this_uart->irqn);
  800. }
  801. }
  802. /***************************************************************************//**
  803. * See mss_uart.h for details of how to use this function.
  804. */
  805. void
  806. MSS_UART_set_linbreak_handler
  807. (
  808. mss_uart_instance_t * this_uart,
  809. mss_uart_irq_handler_t handler
  810. )
  811. {
  812. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  813. ASSERT(handler != INVALID_IRQ_HANDLER);
  814. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
  815. (handler != INVALID_IRQ_HANDLER))
  816. {
  817. this_uart->break_handler = handler;
  818. /* Clear any previously pended interrupts */
  819. NVIC_ClearPendingIRQ( this_uart->irqn );
  820. /* Enable LIN break detection interrupt. */
  821. set_bit_reg8(&this_uart->hw_reg->IEM,ELINBI);
  822. /* Enable UART instance interrupt in Cortex-M3 NVIC. */
  823. NVIC_EnableIRQ(this_uart->irqn);
  824. }
  825. }
  826. /***************************************************************************//**
  827. * See mss_uart.h for details of how to use this function.
  828. */
  829. void
  830. MSS_UART_set_linsync_handler
  831. (
  832. mss_uart_instance_t * this_uart,
  833. mss_uart_irq_handler_t handler
  834. )
  835. {
  836. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  837. ASSERT(handler != INVALID_IRQ_HANDLER);
  838. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
  839. (handler != INVALID_IRQ_HANDLER))
  840. {
  841. this_uart->sync_handler = handler;
  842. /* Clear any previously pended interrupts */
  843. NVIC_ClearPendingIRQ( this_uart->irqn );
  844. /* Enable LIN sync detection interrupt. */
  845. set_bit_reg8(&this_uart->hw_reg->IEM,ELINSI);
  846. /* Enable UART instance interrupt in Cortex-M3 NVIC. */
  847. NVIC_EnableIRQ(this_uart->irqn);
  848. }
  849. }
  850. /***************************************************************************//**
  851. * See mss_uart.h for details of how to use this function.
  852. */
  853. void
  854. MSS_UART_set_nack_handler
  855. (
  856. mss_uart_instance_t * this_uart,
  857. mss_uart_irq_handler_t handler
  858. )
  859. {
  860. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  861. ASSERT(handler != INVALID_IRQ_HANDLER);
  862. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
  863. (handler != INVALID_IRQ_HANDLER))
  864. {
  865. this_uart->nack_handler = handler;
  866. /* Clear any previously pended interrupts */
  867. NVIC_ClearPendingIRQ( this_uart->irqn );
  868. /* Enable LIN sync detection interrupt. */
  869. set_bit_reg8(&this_uart->hw_reg->IEM,ENACKI);
  870. /* Enable UART instance interrupt in Cortex-M3 NVIC. */
  871. NVIC_EnableIRQ(this_uart->irqn);
  872. }
  873. }
  874. /***************************************************************************//**
  875. * See mss_uart.h for details of how to use this function.
  876. */
  877. void
  878. MSS_UART_set_rx_timeout_handler
  879. (
  880. mss_uart_instance_t * this_uart,
  881. mss_uart_irq_handler_t handler
  882. )
  883. {
  884. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  885. ASSERT(handler != INVALID_IRQ_HANDLER);
  886. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
  887. (handler != INVALID_IRQ_HANDLER))
  888. {
  889. this_uart->rto_handler = handler;
  890. /* Clear any previously pended interrupts */
  891. NVIC_ClearPendingIRQ( this_uart->irqn );
  892. /* Enable receiver timeout interrupt. */
  893. set_bit_reg8(&this_uart->hw_reg->IEM,ERTOI);
  894. /* Enable UART instance interrupt in Cortex-M3 NVIC. */
  895. NVIC_EnableIRQ(this_uart->irqn);
  896. }
  897. }
  898. /***************************************************************************//**
  899. * See mss_uart.h for details of how to use this function.
  900. */
  901. void
  902. MSS_UART_enable_half_duplex
  903. (
  904. mss_uart_instance_t * this_uart
  905. )
  906. {
  907. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  908. if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
  909. {
  910. /* enable single wire half-duplex mode */
  911. set_bit_reg8(&this_uart->hw_reg->MM2,ESWM);
  912. }
  913. }
  914. /***************************************************************************//**
  915. * See mss_uart.h for details of how to use this function.
  916. */
  917. void
  918. MSS_UART_disable_half_duplex
  919. (
  920. mss_uart_instance_t * this_uart
  921. )
  922. {
  923. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  924. if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
  925. {
  926. /* enable single wire half-duplex mode */
  927. clear_bit_reg8(&this_uart->hw_reg->MM2,ESWM);
  928. }
  929. }
  930. /***************************************************************************//**
  931. * See mss_uart.h for details of how to use this function.
  932. */
  933. void
  934. MSS_UART_set_rx_endian
  935. (
  936. mss_uart_instance_t * this_uart,
  937. mss_uart_endian_t endian
  938. )
  939. {
  940. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  941. ASSERT(MSS_UART_INVALID_ENDIAN > endian);
  942. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
  943. (MSS_UART_INVALID_ENDIAN > endian))
  944. {
  945. /* Configure MSB first / LSB first for receiver */
  946. ((MSS_UART_LITTLEEND == endian) ? (clear_bit_reg8(&this_uart->hw_reg->MM1,E_MSB_RX)) :
  947. (set_bit_reg8(&this_uart->hw_reg->MM1,E_MSB_RX)));
  948. }
  949. }
  950. /***************************************************************************//**
  951. * See mss_uart.h for details of how to use this function.
  952. */
  953. void
  954. MSS_UART_set_tx_endian
  955. (
  956. mss_uart_instance_t * this_uart,
  957. mss_uart_endian_t endian
  958. )
  959. {
  960. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  961. ASSERT(MSS_UART_INVALID_ENDIAN > endian);
  962. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
  963. (MSS_UART_INVALID_ENDIAN > endian))
  964. {
  965. /* Configure MSB first / LSB first for transmitter */
  966. ((MSS_UART_LITTLEEND == endian) ? (clear_bit_reg8(&this_uart->hw_reg->MM1,E_MSB_TX)) :
  967. (set_bit_reg8(&this_uart->hw_reg->MM1,E_MSB_TX)) ) ;
  968. }
  969. }
  970. /***************************************************************************//**
  971. * See mss_uart.h for details of how to use this function.
  972. */
  973. void
  974. MSS_UART_set_filter_length
  975. (
  976. mss_uart_instance_t * this_uart,
  977. mss_uart_filter_length_t length
  978. )
  979. {
  980. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  981. ASSERT(MSS_UART_INVALID_FILTER_LENGTH > length);
  982. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
  983. (MSS_UART_INVALID_FILTER_LENGTH > length))
  984. {
  985. /* Configure glitch filter length */
  986. this_uart->hw_reg->GFR = (uint8_t)length;
  987. }
  988. }
  989. /***************************************************************************//**
  990. * See mss_uart.h for details of how to use this function.
  991. */
  992. void
  993. MSS_UART_enable_afm
  994. (
  995. mss_uart_instance_t * this_uart
  996. )
  997. {
  998. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  999. if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
  1000. {
  1001. /* Disable RX FIFO till address flag with correct address is received */
  1002. set_bit_reg8(&this_uart->hw_reg->MM2,EAFM);
  1003. }
  1004. }
  1005. /***************************************************************************//**
  1006. * See mss_uart.h for details of how to use this function.
  1007. */
  1008. void
  1009. MSS_UART_disable_afm
  1010. (
  1011. mss_uart_instance_t * this_uart
  1012. )
  1013. {
  1014. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  1015. if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
  1016. {
  1017. /* Enable RX FIFO irrespective of address flag and
  1018. correct address is received */
  1019. clear_bit_reg8(&this_uart->hw_reg->MM2,EAFM);
  1020. }
  1021. }
  1022. /***************************************************************************//**
  1023. * See mss_uart.h for details of how to use this function.
  1024. */
  1025. void
  1026. MSS_UART_enable_afclear
  1027. (
  1028. mss_uart_instance_t * this_uart
  1029. )
  1030. {
  1031. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  1032. if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
  1033. {
  1034. /* Enable address flag clearing */
  1035. /* Disable RX FIFO till another address flag with
  1036. correct address is received */
  1037. set_bit_reg8(&this_uart->hw_reg->MM2,EAFC);
  1038. }
  1039. }
  1040. /***************************************************************************//**
  1041. * See mss_uart.h for details of how to use this function.
  1042. */
  1043. void
  1044. MSS_UART_disable_afclear
  1045. (
  1046. mss_uart_instance_t * this_uart
  1047. )
  1048. {
  1049. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  1050. if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
  1051. {
  1052. /* Disable address flag clearing */
  1053. clear_bit_reg8(&this_uart->hw_reg->MM2,EAFC);
  1054. }
  1055. }
  1056. /***************************************************************************//**
  1057. * See mss_uart.h for details of how to use this function.
  1058. */
  1059. void
  1060. MSS_UART_enable_rx_timeout
  1061. (
  1062. mss_uart_instance_t * this_uart,
  1063. uint8_t timeout
  1064. )
  1065. {
  1066. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  1067. if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
  1068. {
  1069. /* Load the receive timeout value */
  1070. this_uart->hw_reg->RTO = timeout;
  1071. /*Enable receiver time-out */
  1072. set_bit_reg8(&this_uart->hw_reg->MM0,ERTO);
  1073. }
  1074. }
  1075. /***************************************************************************//**
  1076. * See mss_uart.h for details of how to use this function.
  1077. */
  1078. void
  1079. MSS_UART_disable_rx_timeout
  1080. (
  1081. mss_uart_instance_t * this_uart
  1082. )
  1083. {
  1084. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  1085. if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
  1086. {
  1087. /*Disable receiver time-out */
  1088. clear_bit_reg8(&this_uart->hw_reg->MM0,ERTO);
  1089. }
  1090. }
  1091. /***************************************************************************//**
  1092. * See mss_uart.h for details of how to use this function.
  1093. */
  1094. void
  1095. MSS_UART_enable_tx_time_guard
  1096. (
  1097. mss_uart_instance_t * this_uart,
  1098. uint8_t timeguard
  1099. )
  1100. {
  1101. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  1102. if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
  1103. {
  1104. /* Load the transmitter time guard value */
  1105. this_uart->hw_reg->TTG = timeguard;
  1106. /*Enable transmitter time guard */
  1107. set_bit_reg8(&this_uart->hw_reg->MM0,ETTG);
  1108. }
  1109. }
  1110. /***************************************************************************//**
  1111. * See mss_uart.h for details of how to use this function.
  1112. */
  1113. void
  1114. MSS_UART_disable_tx_time_guard
  1115. (
  1116. mss_uart_instance_t * this_uart
  1117. )
  1118. {
  1119. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  1120. if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
  1121. {
  1122. /*Disable transmitter time guard */
  1123. clear_bit_reg8(&this_uart->hw_reg->MM0,ETTG);
  1124. }
  1125. }
  1126. /***************************************************************************//**
  1127. * See mss_uart.h for details of how to use this function.
  1128. */
  1129. void
  1130. MSS_UART_set_address
  1131. (
  1132. mss_uart_instance_t * this_uart,
  1133. uint8_t address
  1134. )
  1135. {
  1136. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  1137. if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
  1138. {
  1139. this_uart->hw_reg->ADR = address;
  1140. }
  1141. }
  1142. /***************************************************************************//**
  1143. * See mss_uart.h for details of how to use this function.
  1144. */
  1145. void
  1146. MSS_UART_set_ready_mode
  1147. (
  1148. mss_uart_instance_t * this_uart,
  1149. mss_uart_ready_mode_t mode
  1150. )
  1151. {
  1152. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  1153. ASSERT(MSS_UART_INVALID_READY_MODE > mode);
  1154. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
  1155. (MSS_UART_INVALID_READY_MODE > mode ) )
  1156. {
  1157. /* Configure mode 0 or mode 1 for TXRDY and RXRDY */
  1158. ((MSS_UART_READY_MODE0 == mode) ? clear_bit_reg8(&this_uart->hw_reg->FCR,RDYMODE) :
  1159. set_bit_reg8(&this_uart->hw_reg->FCR,RDYMODE) );
  1160. }
  1161. }
  1162. /***************************************************************************//**
  1163. * Configure baud divisors using fractional baud rate if possible.
  1164. */
  1165. static void
  1166. config_baud_divisors
  1167. (
  1168. mss_uart_instance_t * this_uart,
  1169. uint32_t baudrate
  1170. )
  1171. {
  1172. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  1173. if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
  1174. {
  1175. uint32_t baud_value;
  1176. uint32_t baud_value_by_64;
  1177. uint32_t baud_value_by_128;
  1178. uint32_t fractional_baud_value;
  1179. uint32_t pclk_freq;
  1180. this_uart->baudrate = baudrate;
  1181. /* Force the value of the CMSIS global variables holding the various system
  1182. * clock frequencies to be updated. */
  1183. SystemCoreClockUpdate();
  1184. if(this_uart == &g_mss_uart0)
  1185. {
  1186. pclk_freq = g_FrequencyPCLK0;
  1187. }
  1188. else
  1189. {
  1190. pclk_freq = g_FrequencyPCLK1;
  1191. }
  1192. /*
  1193. * Compute baud value based on requested baud rate and PCLK frequency.
  1194. * The baud value is computed using the following equation:
  1195. * baud_value = PCLK_Frequency / (baud_rate * 16)
  1196. */
  1197. baud_value_by_128 = (8u * pclk_freq) / baudrate;
  1198. baud_value_by_64 = baud_value_by_128 / 2u;
  1199. baud_value = baud_value_by_64 / 64u;
  1200. fractional_baud_value = baud_value_by_64 - (baud_value * 64u);
  1201. fractional_baud_value += (baud_value_by_128 - (baud_value * 128u)) - (fractional_baud_value * 2u);
  1202. /* Assert if integer baud value fits in 16-bit. */
  1203. ASSERT(baud_value <= UINT16_MAX);
  1204. if(baud_value <= (uint32_t)UINT16_MAX)
  1205. {
  1206. if(baud_value > 1u)
  1207. {
  1208. /*
  1209. * Use Frational baud rate divisors
  1210. */
  1211. /* set divisor latch */
  1212. set_bit_reg8(&this_uart->hw_reg->LCR,DLAB);
  1213. /* msb of baud value */
  1214. this_uart->hw_reg->DMR = (uint8_t)(baud_value >> 8);
  1215. /* lsb of baud value */
  1216. this_uart->hw_reg->DLR = (uint8_t)baud_value;
  1217. /* reset divisor latch */
  1218. clear_bit_reg8(&this_uart->hw_reg->LCR,DLAB);
  1219. /* Enable Fractional baud rate */
  1220. set_bit_reg8(&this_uart->hw_reg->MM0,EFBR);
  1221. /* Load the fractional baud rate register */
  1222. ASSERT(fractional_baud_value <= (uint32_t)UINT8_MAX);
  1223. this_uart->hw_reg->DFR = (uint8_t)fractional_baud_value;
  1224. }
  1225. else
  1226. {
  1227. /*
  1228. * Do NOT use Frational baud rate divisors.
  1229. */
  1230. /* set divisor latch */
  1231. set_bit_reg8(&this_uart->hw_reg->LCR,DLAB);
  1232. /* msb of baud value */
  1233. this_uart->hw_reg->DMR = (uint8_t)(baud_value >> 8u);
  1234. /* lsb of baud value */
  1235. this_uart->hw_reg->DLR = (uint8_t)baud_value;
  1236. /* reset divisor latch */
  1237. clear_bit_reg8(&this_uart->hw_reg->LCR,DLAB);
  1238. /* Disable Fractional baud rate */
  1239. clear_bit_reg8(&this_uart->hw_reg->MM0,EFBR);
  1240. }
  1241. }
  1242. }
  1243. }
  1244. /***************************************************************************//**
  1245. * See mss_uart.h for details of how to use this function.
  1246. */
  1247. void
  1248. MSS_UART_set_usart_mode
  1249. (
  1250. mss_uart_instance_t * this_uart,
  1251. mss_uart_usart_mode_t mode
  1252. )
  1253. {
  1254. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  1255. ASSERT(MSS_UART_INVALID_SYNC_MODE > mode);
  1256. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
  1257. (MSS_UART_INVALID_SYNC_MODE > mode))
  1258. {
  1259. /* Nothing to do for the baudrate: operates at PCLK / 2 + glitch filter length */
  1260. /* Clear the ESYN bits 2:0 */
  1261. this_uart->hw_reg->MM0 &= ~SYNC_ASYNC_MODE_MASK;
  1262. this_uart->hw_reg->MM0 |= (uint8_t)mode;
  1263. }
  1264. }
  1265. /*******************************************************************************
  1266. * Local Functions
  1267. *******************************************************************************/
  1268. /*******************************************************************************
  1269. * Global initialization for all modes
  1270. */
  1271. static void global_init
  1272. (
  1273. mss_uart_instance_t * this_uart,
  1274. uint32_t baud_rate,
  1275. uint8_t line_config
  1276. )
  1277. {
  1278. /* The driver expects g_mss_uart0 and g_mss_uart1 to be the only
  1279. * mss_uart_instance_t instances used to identify UART0 and UART1. */
  1280. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  1281. if(this_uart == &g_mss_uart0)
  1282. {
  1283. this_uart->hw_reg = UART0;
  1284. this_uart->irqn = UART0_IRQn;
  1285. /* reset UART0 */
  1286. SYSREG->SOFT_RST_CR |= SYSREG_MMUART0_SOFTRESET_MASK;
  1287. /* Clear any previously pended UART0 interrupt */
  1288. NVIC_ClearPendingIRQ(UART0_IRQn);
  1289. /* Take UART0 out of reset. */
  1290. SYSREG->SOFT_RST_CR &= ~SYSREG_MMUART0_SOFTRESET_MASK;
  1291. }
  1292. else
  1293. {
  1294. this_uart->hw_reg = UART1;
  1295. this_uart->irqn = UART1_IRQn;
  1296. /* Reset UART1 */
  1297. SYSREG->SOFT_RST_CR |= SYSREG_MMUART1_SOFTRESET_MASK;
  1298. /* Clear any previously pended UART1 interrupt */
  1299. NVIC_ClearPendingIRQ(UART1_IRQn);
  1300. /* Take UART1 out of reset. */
  1301. SYSREG->SOFT_RST_CR &= ~SYSREG_MMUART1_SOFTRESET_MASK;
  1302. }
  1303. /* disable interrupts */
  1304. this_uart->hw_reg->IER = 0u;
  1305. /* FIFO configuration */
  1306. this_uart->hw_reg->FCR = (uint8_t)MSS_UART_FIFO_SINGLE_BYTE;
  1307. /* clear receiver FIFO */
  1308. set_bit_reg8(&this_uart->hw_reg->FCR,CLEAR_RX_FIFO);
  1309. /* clear transmitter FIFO */
  1310. set_bit_reg8(&this_uart->hw_reg->FCR,CLEAR_TX_FIFO);
  1311. /* set default READY mode : Mode 0*/
  1312. /* enable RXRDYN and TXRDYN pins. The earlier FCR write to set the TX FIFO
  1313. * trigger level inadvertently disabled the FCR_RXRDY_TXRDYN_EN bit. */
  1314. set_bit_reg8(&this_uart->hw_reg->FCR,RXRDY_TXRDYN_EN);
  1315. /* disable loopback : local * remote */
  1316. clear_bit_reg8(&this_uart->hw_reg->MCR,LOOP);
  1317. clear_bit_reg8(&this_uart->hw_reg->MCR,RLOOP);
  1318. /* set default TX endian */
  1319. clear_bit_reg8(&this_uart->hw_reg->MM1,E_MSB_TX);
  1320. /* set default RX endian */
  1321. clear_bit_reg8(&this_uart->hw_reg->MM1,E_MSB_RX);
  1322. /* default AFM : disabled */
  1323. clear_bit_reg8(&this_uart->hw_reg->MM2,EAFM);
  1324. /* disable TX time gaurd */
  1325. clear_bit_reg8(&this_uart->hw_reg->MM0,ETTG);
  1326. /* set default RX timeout */
  1327. clear_bit_reg8(&this_uart->hw_reg->MM0,ERTO);
  1328. /* disable fractional baud-rate */
  1329. clear_bit_reg8(&this_uart->hw_reg->MM0,EFBR);
  1330. /* disable single wire mode */
  1331. clear_bit_reg8(&this_uart->hw_reg->MM2,ESWM);
  1332. /* set filter to minimum value */
  1333. this_uart->hw_reg->GFR = 0u;
  1334. /* set default TX time gaurd */
  1335. this_uart->hw_reg->TTG = 0u;
  1336. /* set default RX timeout */
  1337. this_uart->hw_reg->RTO = 0u;
  1338. /*
  1339. * Configure baud rate divisors. This uses the frational baud rate divisor
  1340. * where possible to provide the most accurate baud rat possible.
  1341. */
  1342. config_baud_divisors(this_uart, baud_rate);
  1343. /* set the line control register (bit length, stop bits, parity) */
  1344. this_uart->hw_reg->LCR = line_config;
  1345. /* Instance setup */
  1346. this_uart->baudrate = baud_rate;
  1347. this_uart->lineconfig = line_config;
  1348. this_uart->tx_buff_size = TX_COMPLETE;
  1349. this_uart->tx_buffer = (const uint8_t *)0;
  1350. this_uart->tx_idx = 0u;
  1351. /* Default handlers for MSS UART interrupts */
  1352. this_uart->rx_handler = NULL_HANDLER;
  1353. this_uart->tx_handler = NULL_HANDLER;
  1354. this_uart->linests_handler = NULL_HANDLER;
  1355. this_uart->modemsts_handler = NULL_HANDLER;
  1356. this_uart->rto_handler = NULL_HANDLER;
  1357. this_uart->nack_handler = NULL_HANDLER;
  1358. this_uart->pid_pei_handler = NULL_HANDLER;
  1359. this_uart->break_handler = NULL_HANDLER;
  1360. this_uart->sync_handler = NULL_HANDLER;
  1361. /* Initialize the sticky status */
  1362. this_uart->status = 0u;
  1363. }
  1364. /***************************************************************************//**
  1365. * Interrupt service routine triggered by any MSS UART interrupt. This routine
  1366. * will call the handler function appropriate to the interrupt from the
  1367. * handlers previously registered with the driver through calls to the
  1368. * MSS_UART_set_*_handler() functions, or it will call the default_tx_handler()
  1369. * function in response to transmit interrupts if MSS_UART_irq_tx() is used to
  1370. * transmit data.
  1371. */
  1372. static void
  1373. MSS_UART_isr
  1374. (
  1375. mss_uart_instance_t * this_uart
  1376. )
  1377. {
  1378. uint8_t iirf;
  1379. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  1380. if((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1))
  1381. {
  1382. iirf = this_uart->hw_reg->IIR & IIRF_MASK;
  1383. switch (iirf)
  1384. {
  1385. case IIRF_MODEM_STATUS: /* Modem status interrupt */
  1386. {
  1387. ASSERT(NULL_HANDLER != this_uart->modemsts_handler);
  1388. if(NULL_HANDLER != this_uart->modemsts_handler)
  1389. {
  1390. (*(this_uart->modemsts_handler))(this_uart);
  1391. }
  1392. }
  1393. break;
  1394. case IIRF_THRE: /* Transmitter Holding Register Empty */
  1395. {
  1396. ASSERT(NULL_HANDLER != this_uart->tx_handler);
  1397. if(NULL_HANDLER != this_uart->tx_handler)
  1398. {
  1399. (*(this_uart->tx_handler))(this_uart);
  1400. }
  1401. }
  1402. break;
  1403. case IIRF_RX_DATA: /* Received Data Available */
  1404. case IIRF_DATA_TIMEOUT: /* Received Data Timed-out */
  1405. {
  1406. ASSERT(NULL_HANDLER != this_uart->rx_handler);
  1407. if(NULL_HANDLER != this_uart->rx_handler)
  1408. {
  1409. (*(this_uart->rx_handler))(this_uart);
  1410. }
  1411. }
  1412. break;
  1413. case IIRF_RX_LINE_STATUS: /* Line Status Interrupt */
  1414. {
  1415. ASSERT(NULL_HANDLER != this_uart->linests_handler);
  1416. if(NULL_HANDLER != this_uart->linests_handler)
  1417. {
  1418. (*(this_uart->linests_handler))(this_uart);
  1419. }
  1420. }
  1421. break;
  1422. case IIRF_MMI:
  1423. {
  1424. /* Identify multimode interrupts and handle */
  1425. /* Receiver time-out interrupt */
  1426. if(read_bit_reg8(&this_uart->hw_reg->IIM,ERTOI))
  1427. {
  1428. ASSERT(NULL_HANDLER != this_uart->rto_handler);
  1429. if(NULL_HANDLER != this_uart->rto_handler)
  1430. {
  1431. (*(this_uart->rto_handler))(this_uart);
  1432. }
  1433. }
  1434. /* NACK interrupt */
  1435. if(read_bit_reg8(&this_uart->hw_reg->IIM,ENACKI))
  1436. {
  1437. ASSERT(NULL_HANDLER != this_uart->nack_handler);
  1438. if(NULL_HANDLER != this_uart->nack_handler)
  1439. {
  1440. (*(this_uart->nack_handler))(this_uart);
  1441. }
  1442. }
  1443. /* PID parity error interrupt */
  1444. if(read_bit_reg8(&this_uart->hw_reg->IIM,EPID_PEI))
  1445. {
  1446. ASSERT(NULL_HANDLER != this_uart->pid_pei_handler);
  1447. if(NULL_HANDLER != this_uart->pid_pei_handler)
  1448. {
  1449. (*(this_uart->pid_pei_handler))(this_uart);
  1450. }
  1451. }
  1452. /* LIN break detection interrupt */
  1453. if(read_bit_reg8(&this_uart->hw_reg->IIM,ELINBI))
  1454. {
  1455. ASSERT(NULL_HANDLER != this_uart->break_handler);
  1456. if(NULL_HANDLER != this_uart->break_handler)
  1457. {
  1458. (*(this_uart->break_handler))(this_uart);
  1459. }
  1460. }
  1461. /* LIN Sync detection interrupt */
  1462. if(read_bit_reg8(&this_uart->hw_reg->IIM,ELINSI))
  1463. {
  1464. ASSERT(NULL_HANDLER != this_uart->sync_handler);
  1465. if(NULL_HANDLER != this_uart->sync_handler)
  1466. {
  1467. (*(this_uart->sync_handler))(this_uart);
  1468. }
  1469. }
  1470. break;
  1471. }
  1472. default:
  1473. {
  1474. ASSERT(INVALID_INTERRUPT);
  1475. }
  1476. break;
  1477. }
  1478. }
  1479. }
  1480. /***************************************************************************//**
  1481. * See mss_uart.h for details of how to use this function.
  1482. */
  1483. static void
  1484. default_tx_handler
  1485. (
  1486. mss_uart_instance_t * this_uart
  1487. )
  1488. {
  1489. uint8_t status;
  1490. ASSERT((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1));
  1491. ASSERT(( (uint8_t *)0 ) != this_uart->tx_buffer);
  1492. ASSERT(0u < this_uart->tx_buff_size);
  1493. if(((this_uart == &g_mss_uart0) || (this_uart == &g_mss_uart1)) &&
  1494. (((uint8_t *)0 ) != this_uart->tx_buffer) &&
  1495. (0u < this_uart->tx_buff_size))
  1496. {
  1497. /* Read the Line Status Register and update the sticky record. */
  1498. status = this_uart->hw_reg->LSR;
  1499. this_uart->status |= status;
  1500. /*
  1501. * This function should only be called as a result of a THRE interrupt.
  1502. * Verify that this is true before proceeding to transmit data.
  1503. */
  1504. if(status & MSS_UART_THRE)
  1505. {
  1506. uint32_t i;
  1507. uint32_t fill_size = TX_FIFO_SIZE;
  1508. uint32_t tx_remain = this_uart->tx_buff_size - this_uart->tx_idx;
  1509. /* Calculate the number of bytes to transmit. */
  1510. if(tx_remain < TX_FIFO_SIZE)
  1511. {
  1512. fill_size = tx_remain;
  1513. }
  1514. /* Fill the TX FIFO with the calculated the number of bytes. */
  1515. for(i = 0u; i < fill_size; ++i)
  1516. {
  1517. /* Send next character in the buffer. */
  1518. this_uart->hw_reg->THR = this_uart->tx_buffer[this_uart->tx_idx];
  1519. ++this_uart->tx_idx;
  1520. }
  1521. }
  1522. /* Flag Tx as complete if all data has been pushed into the Tx FIFO. */
  1523. if(this_uart->tx_idx == this_uart->tx_buff_size)
  1524. {
  1525. this_uart->tx_buff_size = TX_COMPLETE;
  1526. /* disables TX interrupt */
  1527. clear_bit_reg8(&this_uart->hw_reg->IER,ETBEI);
  1528. }
  1529. }
  1530. }
  1531. #ifdef __cplusplus
  1532. }
  1533. #endif