_hal_uart_dma.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /**************************************************************************************************
  2. Filename: _hal_uart_dma.c
  3. Revised: $Date: 2009-03-31 13:13:12 -0700 (Tue, 31 Mar 2009) $
  4. Revision: $Revision: 19614 $
  5. Description: This file contains the interface to the H/W UART driver by DMA.
  6. Copyright 2006-2009 Texas Instruments Incorporated. All rights reserved.
  7. IMPORTANT: Your use of this Software is limited to those specific rights
  8. granted under the terms of a software license agreement between the user
  9. who downloaded the software, his/her employer (which must be your employer)
  10. and Texas Instruments Incorporated (the "License"). You may not use this
  11. Software unless you agree to abide by the terms of the License. The License
  12. limits your use, and you acknowledge, that the Software may not be modified,
  13. copied or distributed unless embedded on a Texas Instruments microcontroller
  14. or used solely and exclusively in conjunction with a Texas Instruments radio
  15. frequency transceiver, which is integrated into your product. Other than for
  16. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  17. works of, modify, distribute, perform, display or sell this Software and/or
  18. its documentation for any purpose.
  19. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  20. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  21. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  22. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  23. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  24. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  25. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  26. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  27. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  28. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  29. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  30. Should you have any questions regarding your right to use this Software,
  31. contact Texas Instruments Incorporated at www.TI.com.
  32. **************************************************************************************************/
  33. /*********************************************************************
  34. * INCLUDES
  35. */
  36. #include "hal_types.h"
  37. #include "hal_assert.h"
  38. #include "hal_board.h"
  39. #include "hal_defs.h"
  40. #include "hal_dma.h"
  41. #include "hal_mcu.h"
  42. #include "hal_uart.h"
  43. #if defined MT_TASK
  44. #include "mt_uart.h"
  45. #endif
  46. #include "osal.h"
  47. /*********************************************************************
  48. * MACROS
  49. */
  50. //#define HAL_UART_ASSERT(expr) HAL_ASSERT((expr))
  51. #define HAL_UART_ASSERT(expr)
  52. #if defined HAL_BOARD_CC2430EB || defined HAL_BOARD_CC2430DB || defined HAL_BOARD_CC2430BB
  53. #define HAL_UART_DMA_NEW_RX_BYTE(IDX) (DMA_PAD == LO_UINT16(dmaCfg.rxBuf[(IDX)]))
  54. #define HAL_UART_DMA_GET_RX_BYTE(IDX) (HI_UINT16(dmaCfg.rxBuf[(IDX)]))
  55. #define HAL_UART_DMA_CLR_RX_BYTE(IDX) (dmaCfg.rxBuf[(IDX)] = BUILD_UINT16((DMA_PAD ^ 0xFF), 0))
  56. #else
  57. #define HAL_UART_DMA_NEW_RX_BYTE(IDX) (DMA_PAD == HI_UINT16(dmaCfg.rxBuf[(IDX)]))
  58. #define HAL_UART_DMA_GET_RX_BYTE(IDX) (LO_UINT16(dmaCfg.rxBuf[(IDX)]))
  59. #define HAL_UART_DMA_CLR_RX_BYTE(IDX) (dmaCfg.rxBuf[(IDX)] = BUILD_UINT16(0, (DMA_PAD ^ 0xFF)))
  60. #endif
  61. /*********************************************************************
  62. * CONSTANTS
  63. */
  64. // UxCSR - USART Control and Status Register.
  65. #define CSR_MODE 0x80
  66. #define CSR_RE 0x40
  67. #define CSR_SLAVE 0x20
  68. #define CSR_FE 0x10
  69. #define CSR_ERR 0x08
  70. #define CSR_RX_BYTE 0x04
  71. #define CSR_TX_BYTE 0x02
  72. #define CSR_ACTIVE 0x01
  73. // UxUCR - USART UART Control Register.
  74. #define UCR_FLUSH 0x80
  75. #define UCR_FLOW 0x40
  76. #define UCR_D9 0x20
  77. #define UCR_BIT9 0x10
  78. #define UCR_PARITY 0x08
  79. #define UCR_SPB 0x04
  80. #define UCR_STOP 0x02
  81. #define UCR_START 0x01
  82. #define UTX0IE 0x04
  83. #define UTX1IE 0x08
  84. #define P2DIR_PRIPO 0xC0
  85. // Incompatible redefinitions between the 2 UART driver sub-modules:
  86. #undef PxSEL
  87. #undef UxCSR
  88. #undef UxUCR
  89. #undef UxDBUF
  90. #undef UxBAUD
  91. #undef UxGCR
  92. #undef URXxIE
  93. #undef UTXxIE
  94. #undef UTXxIF
  95. #if (HAL_UART_DMA == 1)
  96. #define PxOUT P0
  97. #define PxDIR P0DIR
  98. #define PxSEL P0SEL
  99. #define UxCSR U0CSR
  100. #define UxUCR U0UCR
  101. #define UxDBUF U0DBUF
  102. #define UxBAUD U0BAUD
  103. #define UxGCR U0GCR
  104. #define URXxIE URX0IE
  105. #define UTXxIE UTX0IE
  106. #define UTXxIF UTX0IF
  107. #define UxRX_TX 0x0C
  108. #define HAL_UART_PERCFG_BIT 0x01
  109. #define HAL_UART_Px_CTS 0x10 // Peripheral I/O Select for CTS.
  110. #define HAL_UART_Px_RTS 0x20 // Peripheral I/O Select for RTS.
  111. #else
  112. #define PxOUT P1
  113. #define PxDIR P1DIR
  114. #define PxSEL P1SEL
  115. #define UxCSR U1CSR
  116. #define UxUCR U1UCR
  117. #define UxDBUF U1DBUF
  118. #define UxBAUD U1BAUD
  119. #define UxGCR U1GCR
  120. #define URXxIE URX1IE
  121. #define UTXxIE UTX1IE
  122. #define UTXxIF UTX1IF
  123. #define UxRX_TX 0xC0
  124. #define HAL_UART_PERCFG_BIT 0x02
  125. #define HAL_UART_Px_CTS 0x10 // Peripheral I/O Select for CTS.
  126. #define HAL_UART_Px_RTS 0x20 // Peripheral I/O Select for RTS.
  127. #endif
  128. // The timeout tick is at 32-kHz, so multiply msecs by 33.
  129. #define HAL_UART_MSECS_TO_TICKS 33
  130. #if defined MT_TASK
  131. #define HAL_UART_DMA_TX_MAX MT_UART_DEFAULT_MAX_TX_BUFF
  132. #define HAL_UART_DMA_RX_MAX MT_UART_DEFAULT_MAX_RX_BUFF
  133. #define HAL_UART_DMA_HIGH MT_UART_DEFAULT_THRESHOLD
  134. #define HAL_UART_DMA_IDLE (MT_UART_DEFAULT_IDLE_TIMEOUT * HAL_UART_MSECS_TO_TICKS)
  135. #else
  136. #if !defined HAL_UART_DMA_RX_MAX
  137. #define HAL_UART_DMA_RX_MAX 128
  138. #endif
  139. #if !defined HAL_UART_DMA_TX_MAX
  140. #define HAL_UART_DMA_TX_MAX HAL_UART_DMA_RX_MAX
  141. #endif
  142. #if !defined HAL_UART_DMA_HIGH
  143. #define HAL_UART_DMA_HIGH (HAL_UART_DMA_RX_MAX / 2 - 16)
  144. #endif
  145. #if !defined HAL_UART_DMA_IDLE
  146. #define HAL_UART_DMA_IDLE (6 * HAL_UART_MSECS_TO_TICKS)
  147. #endif
  148. #endif
  149. #if !defined HAL_UART_DMA_FULL
  150. #define HAL_UART_DMA_FULL (HAL_UART_DMA_RX_MAX - 16)
  151. #endif
  152. #if defined HAL_BOARD_CC2430EB || defined HAL_BOARD_CC2430DB || defined HAL_BOARD_CC2430BB
  153. #define HAL_DMA_U0DBUF 0xDFC1
  154. #define HAL_DMA_U1DBUF 0xDFF9
  155. #else /* CC2530 */
  156. #define HAL_DMA_U0DBUF 0x70C1
  157. #define HAL_DMA_U1DBUF 0x70F9
  158. #endif
  159. #if (HAL_UART_DMA == 1)
  160. #define DMATRIG_RX HAL_DMA_TRIG_URX0
  161. #define DMATRIG_TX HAL_DMA_TRIG_UTX0
  162. #define DMA_UDBUF HAL_DMA_U0DBUF
  163. #define DMA_PAD U0BAUD
  164. #else
  165. #define DMATRIG_RX HAL_DMA_TRIG_URX1
  166. #define DMATRIG_TX HAL_DMA_TRIG_UTX1
  167. #define DMA_UDBUF HAL_DMA_U1DBUF
  168. #define DMA_PAD U1BAUD
  169. #endif
  170. /*********************************************************************
  171. * TYPEDEFS
  172. */
  173. typedef struct
  174. {
  175. uint16 rxBuf[HAL_UART_DMA_RX_MAX];
  176. #if HAL_UART_DMA_RX_MAX < 256
  177. uint8 rxHead;
  178. uint8 rxTail;
  179. #else
  180. uint16 rxHead;
  181. uint16 rxTail;
  182. #endif
  183. uint8 rxTick;
  184. uint8 rxShdw;
  185. uint8 txBuf[2][HAL_UART_DMA_TX_MAX];
  186. #if HAL_UART_DMA_TX_MAX < 256
  187. uint8 txIdx[2];
  188. #else
  189. uint16 txIdx[2];
  190. #endif
  191. volatile uint8 txSel;
  192. uint8 txMT;
  193. uint8 txTick; // 1-character time in 32kHz ticks according to baud rate,
  194. // to be used in calculating time lapse since DMA ISR
  195. // to allow delay margin before start firing DMA, so that
  196. // DMA does not overwrite UART DBUF of previous packet
  197. volatile uint8 txShdw; // Sleep Timer LSB shadow.
  198. volatile uint8 txShdwValid; // TX shadow value is valid
  199. uint8 txDMAPending; // UART TX DMA is pending
  200. halUARTCBack_t uartCB;
  201. } uartDMACfg_t;
  202. /*********************************************************************
  203. * GLOBAL VARIABLES
  204. */
  205. /*********************************************************************
  206. * GLOBAL FUNCTIONS
  207. */
  208. void HalUARTIsrDMA(void);
  209. /*********************************************************************
  210. * LOCAL VARIABLES
  211. */
  212. static uartDMACfg_t dmaCfg;
  213. /*********************************************************************
  214. * LOCAL FUNCTIONS
  215. */
  216. static uint16 findTail(void);
  217. // Invoked by functions in hal_uart.c when this file is included.
  218. static void HalUARTInitDMA(void);
  219. static void HalUARTOpenDMA(halUARTCfg_t *config);
  220. static uint16 HalUARTReadDMA(uint8 *buf, uint16 len);
  221. static uint16 HalUARTWriteDMA(uint8 *buf, uint16 len);
  222. static void HalUARTPollDMA(void);
  223. static uint16 HalUARTRxAvailDMA(void);
  224. static void HalUARTSuspendDMA(void);
  225. static void HalUARTResumeDMA(void);
  226. /*****************************************************************************
  227. * @fn findTail
  228. *
  229. * @brief Find the rxBuf index where the DMA RX engine is working.
  230. *
  231. * @param None.
  232. *
  233. * @return Index of tail of rxBuf.
  234. *****************************************************************************/
  235. static uint16 findTail(void)
  236. {
  237. uint16 idx = dmaCfg.rxHead;
  238. do
  239. {
  240. if (!HAL_UART_DMA_NEW_RX_BYTE(idx))
  241. {
  242. break;
  243. }
  244. if (++idx >= HAL_UART_DMA_RX_MAX)
  245. {
  246. idx = 0;
  247. }
  248. } while (idx != dmaCfg.rxHead);
  249. return idx;
  250. }
  251. /******************************************************************************
  252. * @fn HalUARTInitDMA
  253. *
  254. * @brief Initialize the UART
  255. *
  256. * @param none
  257. *
  258. * @return none
  259. *****************************************************************************/
  260. static void HalUARTInitDMA(void)
  261. {
  262. halDMADesc_t *ch;
  263. P2DIR &= ~P2DIR_PRIPO;
  264. P2DIR |= HAL_UART_PRIPO;
  265. #if (HAL_UART_DMA == 1)
  266. PERCFG &= ~HAL_UART_PERCFG_BIT; // Set UART0 I/O to Alt. 1 location on P0.
  267. #else
  268. PERCFG |= HAL_UART_PERCFG_BIT; // Set UART1 I/O to Alt. 2 location on P1.
  269. #endif
  270. PxSEL |= UxRX_TX; // Enable Tx and Rx peripheral functions on pins.
  271. ADCCFG &= ~UxRX_TX; // Make sure ADC doesnt use this.
  272. UxCSR = CSR_MODE; // Mode is UART Mode.
  273. UxUCR = UCR_FLUSH; // Flush it.
  274. // Setup Tx by DMA.
  275. ch = HAL_DMA_GET_DESC1234( HAL_DMA_CH_TX );
  276. // The start address of the destination.
  277. HAL_DMA_SET_DEST( ch, DMA_UDBUF );
  278. // Using the length field to determine how many bytes to transfer.
  279. HAL_DMA_SET_VLEN( ch, HAL_DMA_VLEN_USE_LEN );
  280. // One byte is transferred each time.
  281. HAL_DMA_SET_WORD_SIZE( ch, HAL_DMA_WORDSIZE_BYTE );
  282. // The bytes are transferred 1-by-1 on Tx Complete trigger.
  283. HAL_DMA_SET_TRIG_MODE( ch, HAL_DMA_TMODE_SINGLE );
  284. HAL_DMA_SET_TRIG_SRC( ch, DMATRIG_TX );
  285. // The source address is incremented by 1 byte after each transfer.
  286. HAL_DMA_SET_SRC_INC( ch, HAL_DMA_SRCINC_1 );
  287. // The destination address is constant - the Tx Data Buffer.
  288. HAL_DMA_SET_DST_INC( ch, HAL_DMA_DSTINC_0 );
  289. // The DMA Tx done is serviced by ISR in order to maintain full thruput.
  290. HAL_DMA_SET_IRQ( ch, HAL_DMA_IRQMASK_ENABLE );
  291. // Xfer all 8 bits of a byte xfer.
  292. HAL_DMA_SET_M8( ch, HAL_DMA_M8_USE_8_BITS );
  293. // DMA has highest priority for memory access.
  294. HAL_DMA_SET_PRIORITY( ch, HAL_DMA_PRI_HIGH );
  295. // Setup Rx by DMA.
  296. ch = HAL_DMA_GET_DESC1234( HAL_DMA_CH_RX );
  297. // The start address of the source.
  298. HAL_DMA_SET_SOURCE( ch, DMA_UDBUF );
  299. // Using the length field to determine how many bytes to transfer.
  300. HAL_DMA_SET_VLEN( ch, HAL_DMA_VLEN_USE_LEN );
  301. /* The trick is to cfg DMA to xfer 2 bytes for every 1 byte of Rx.
  302. * The byte after the Rx Data Buffer is the Baud Cfg Register,
  303. * which always has a known value. So init Rx buffer to inverse of that
  304. * known value. DMA word xfer will flip the bytes, so every valid Rx byte
  305. * in the Rx buffer will be preceded by a DMA_PAD char equal to the
  306. * Baud Cfg Register value.
  307. */
  308. HAL_DMA_SET_WORD_SIZE( ch, HAL_DMA_WORDSIZE_WORD );
  309. // The bytes are transferred 1-by-1 on Rx Complete trigger.
  310. HAL_DMA_SET_TRIG_MODE( ch, HAL_DMA_TMODE_SINGLE_REPEATED );
  311. HAL_DMA_SET_TRIG_SRC( ch, DMATRIG_RX );
  312. // The source address is constant - the Rx Data Buffer.
  313. HAL_DMA_SET_SRC_INC( ch, HAL_DMA_SRCINC_0 );
  314. // The destination address is incremented by 1 word after each transfer.
  315. HAL_DMA_SET_DST_INC( ch, HAL_DMA_DSTINC_1 );
  316. HAL_DMA_SET_DEST( ch, dmaCfg.rxBuf );
  317. HAL_DMA_SET_LEN( ch, HAL_UART_DMA_RX_MAX );
  318. // The DMA is to be polled and shall not issue an IRQ upon completion.
  319. HAL_DMA_SET_IRQ( ch, HAL_DMA_IRQMASK_DISABLE );
  320. // Xfer all 8 bits of a byte xfer.
  321. HAL_DMA_SET_M8( ch, HAL_DMA_M8_USE_8_BITS );
  322. // DMA has highest priority for memory access.
  323. HAL_DMA_SET_PRIORITY( ch, HAL_DMA_PRI_HIGH );
  324. }
  325. /******************************************************************************
  326. * @fn HalUARTOpenDMA
  327. *
  328. * @brief Open a port according tp the configuration specified by parameter.
  329. *
  330. * @param config - contains configuration information
  331. *
  332. * @return none
  333. *****************************************************************************/
  334. static void HalUARTOpenDMA(halUARTCfg_t *config)
  335. {
  336. dmaCfg.uartCB = config->callBackFunc;
  337. // Only supporting subset of baudrate for code size - other is possible.
  338. HAL_UART_ASSERT((config->baudRate == HAL_UART_BR_9600) ||
  339. (config->baudRate == HAL_UART_BR_19200) ||
  340. (config->baudRate == HAL_UART_BR_38400) ||
  341. (config->baudRate == HAL_UART_BR_57600) ||
  342. (config->baudRate == HAL_UART_BR_115200));
  343. if (config->baudRate == HAL_UART_BR_57600 ||
  344. config->baudRate == HAL_UART_BR_115200)
  345. {
  346. UxBAUD = 216;
  347. }
  348. else
  349. {
  350. UxBAUD = 59;
  351. }
  352. switch (config->baudRate)
  353. {
  354. case HAL_UART_BR_9600:
  355. UxGCR = 8;
  356. dmaCfg.txTick = 35; // (32768Hz / (9600bps / 10 bits))
  357. // 10 bits include start and stop bits.
  358. break;
  359. case HAL_UART_BR_19200:
  360. UxGCR = 9;
  361. dmaCfg.txTick = 18;
  362. break;
  363. case HAL_UART_BR_38400:
  364. UxGCR = 10;
  365. dmaCfg.txTick = 9;
  366. break;
  367. case HAL_UART_BR_57600:
  368. UxGCR = 10;
  369. dmaCfg.txTick = 6;
  370. break;
  371. default:
  372. // HAL_UART_BR_115200
  373. UxGCR = 11;
  374. dmaCfg.txTick = 3;
  375. break;
  376. }
  377. // 8 bits/char; no parity; 1 stop bit; stop bit hi.
  378. if (config->flowControl)
  379. {
  380. UxUCR = UCR_FLOW | UCR_STOP;
  381. PxSEL |= HAL_UART_Px_CTS;
  382. // DMA Rx is always on (self-resetting). So flow must be controlled by the S/W polling the Rx
  383. // buffer level. Start by allowing flow.
  384. PxOUT &= ~HAL_UART_Px_RTS;
  385. PxDIR |= HAL_UART_Px_RTS;
  386. }
  387. else
  388. {
  389. UxUCR = UCR_STOP;
  390. }
  391. dmaCfg.rxBuf[0] = *(volatile uint8 *)DMA_UDBUF; // Clear the DMA Rx trigger.
  392. HAL_DMA_CLEAR_IRQ(HAL_DMA_CH_RX);
  393. HAL_DMA_ARM_CH(HAL_DMA_CH_RX);
  394. osal_memset(dmaCfg.rxBuf, (DMA_PAD ^ 0xFF), HAL_UART_DMA_RX_MAX*2);
  395. UxCSR |= CSR_RE;
  396. UxDBUF = 0; // Prime the DMA-ISR pump.
  397. // Initialize that TX DMA is not pending
  398. dmaCfg.txDMAPending = FALSE;
  399. dmaCfg.txShdwValid = FALSE;
  400. }
  401. /*****************************************************************************
  402. * @fn HalUARTReadDMA
  403. *
  404. * @brief Read a buffer from the UART
  405. *
  406. * @param buf - valid data buffer at least 'len' bytes in size
  407. * len - max length number of bytes to copy to 'buf'
  408. *
  409. * @return length of buffer that was read
  410. *****************************************************************************/
  411. static uint16 HalUARTReadDMA(uint8 *buf, uint16 len)
  412. {
  413. uint16 cnt;
  414. for (cnt = 0; cnt < len; cnt++)
  415. {
  416. if (!HAL_UART_DMA_NEW_RX_BYTE(dmaCfg.rxHead))
  417. {
  418. break;
  419. }
  420. *buf++ = HAL_UART_DMA_GET_RX_BYTE(dmaCfg.rxHead);
  421. HAL_UART_DMA_CLR_RX_BYTE(dmaCfg.rxHead);
  422. if (++(dmaCfg.rxHead) >= HAL_UART_DMA_RX_MAX)
  423. {
  424. dmaCfg.rxHead = 0;
  425. }
  426. }
  427. PxOUT &= ~HAL_UART_Px_RTS; // Re-enable the flow on any read.
  428. return cnt;
  429. }
  430. /******************************************************************************
  431. * @fn HalUARTWriteDMA
  432. *
  433. * @brief Write a buffer to the UART.
  434. *
  435. * @param buf - pointer to the buffer that will be written, not freed
  436. * len - length of
  437. *
  438. * @return length of the buffer that was sent
  439. *****************************************************************************/
  440. static uint16 HalUARTWriteDMA(uint8 *buf, uint16 len)
  441. {
  442. uint16 cnt;
  443. halIntState_t his;
  444. uint8 txIdx, txSel;
  445. // Enforce all or none.
  446. if ((len + dmaCfg.txIdx[dmaCfg.txSel]) > HAL_UART_DMA_TX_MAX)
  447. {
  448. return 0;
  449. }
  450. HAL_ENTER_CRITICAL_SECTION(his);
  451. txSel = dmaCfg.txSel;
  452. txIdx = dmaCfg.txIdx[txSel];
  453. HAL_EXIT_CRITICAL_SECTION(his);
  454. for (cnt = 0; cnt < len; cnt++)
  455. {
  456. dmaCfg.txBuf[txSel][txIdx++] = buf[cnt];
  457. }
  458. HAL_ENTER_CRITICAL_SECTION(his);
  459. if (txSel != dmaCfg.txSel)
  460. {
  461. HAL_EXIT_CRITICAL_SECTION(his);
  462. txSel = dmaCfg.txSel;
  463. txIdx = dmaCfg.txIdx[txSel];
  464. for (cnt = 0; cnt < len; cnt++)
  465. {
  466. dmaCfg.txBuf[txSel][txIdx++] = buf[cnt];
  467. }
  468. HAL_ENTER_CRITICAL_SECTION(his);
  469. }
  470. dmaCfg.txIdx[txSel] = txIdx;
  471. if (dmaCfg.txIdx[(txSel ^ 1)] == 0)
  472. {
  473. // TX DMA is expected to be fired
  474. dmaCfg.txDMAPending = TRUE;
  475. }
  476. HAL_EXIT_CRITICAL_SECTION(his);
  477. return cnt;
  478. }
  479. /******************************************************************************
  480. * @fn HalUARTPollDMA
  481. *
  482. * @brief Poll a USART module implemented by DMA.
  483. *
  484. * @param none
  485. *
  486. * @return none
  487. *****************************************************************************/
  488. static void HalUARTPollDMA(void)
  489. {
  490. uint16 cnt = 0;
  491. uint8 evt = 0;
  492. if (HAL_UART_DMA_NEW_RX_BYTE(dmaCfg.rxHead))
  493. {
  494. uint16 tail = findTail();
  495. // If the DMA has transferred in more Rx bytes, reset the Rx idle timer.
  496. if (dmaCfg.rxTail != tail)
  497. {
  498. dmaCfg.rxTail = tail;
  499. // Re-sync the shadow on any 1st byte(s) received.
  500. if (dmaCfg.rxTick == 0)
  501. {
  502. dmaCfg.rxShdw = ST0;
  503. }
  504. dmaCfg.rxTick = HAL_UART_DMA_IDLE;
  505. }
  506. else if (dmaCfg.rxTick)
  507. {
  508. // Use the LSB of the sleep timer (ST0 must be read first anyway).
  509. uint8 decr = ST0 - dmaCfg.rxShdw;
  510. if (dmaCfg.rxTick > decr)
  511. {
  512. dmaCfg.rxTick -= decr;
  513. dmaCfg.rxShdw = ST0;
  514. }
  515. else
  516. {
  517. dmaCfg.rxTick = 0;
  518. }
  519. }
  520. cnt = HalUARTRxAvailDMA();
  521. }
  522. else
  523. {
  524. dmaCfg.rxTick = 0;
  525. }
  526. if (cnt >= HAL_UART_DMA_FULL)
  527. {
  528. evt = HAL_UART_RX_FULL;
  529. }
  530. else if (cnt >= HAL_UART_DMA_HIGH)
  531. {
  532. evt = HAL_UART_RX_ABOUT_FULL;
  533. PxOUT |= HAL_UART_Px_RTS;
  534. }
  535. else if (cnt && !dmaCfg.rxTick)
  536. {
  537. evt = HAL_UART_RX_TIMEOUT;
  538. }
  539. if (dmaCfg.txMT)
  540. {
  541. dmaCfg.txMT = FALSE;
  542. evt |= HAL_UART_TX_EMPTY;
  543. }
  544. if (dmaCfg.txShdwValid)
  545. {
  546. uint8 decr = ST0;
  547. decr -= dmaCfg.txShdw;
  548. if (decr > dmaCfg.txTick)
  549. {
  550. // No protection for txShdwValid is required
  551. // because while the shadow was valid, DMA ISR cannot be triggered
  552. // to cause concurrent access to this variable.
  553. dmaCfg.txShdwValid = FALSE;
  554. }
  555. }
  556. if (dmaCfg.txDMAPending && !dmaCfg.txShdwValid)
  557. {
  558. // UART TX DMA is expected to be fired and enough time has lapsed since last DMA ISR
  559. // to know that DBUF can be overwritten
  560. halDMADesc_t *ch = HAL_DMA_GET_DESC1234(HAL_DMA_CH_TX);
  561. halIntState_t intState;
  562. // Clear the DMA pending flag
  563. dmaCfg.txDMAPending = FALSE;
  564. HAL_DMA_SET_SOURCE(ch, dmaCfg.txBuf[dmaCfg.txSel]);
  565. HAL_DMA_SET_LEN(ch, dmaCfg.txIdx[dmaCfg.txSel]);
  566. dmaCfg.txSel ^= 1;
  567. HAL_ENTER_CRITICAL_SECTION(intState);
  568. HAL_DMA_ARM_CH(HAL_DMA_CH_TX);
  569. do
  570. {
  571. asm("NOP");
  572. } while (!HAL_DMA_CH_ARMED(HAL_DMA_CH_TX));
  573. HAL_DMA_CLEAR_IRQ(HAL_DMA_CH_TX);
  574. HAL_DMA_MAN_TRIGGER(HAL_DMA_CH_TX);
  575. HAL_EXIT_CRITICAL_SECTION(intState);
  576. }
  577. if (evt && (dmaCfg.uartCB != NULL))
  578. {
  579. dmaCfg.uartCB(HAL_UART_DMA-1, evt);
  580. }
  581. }
  582. /**************************************************************************************************
  583. * @fn HalUARTRxAvailDMA()
  584. *
  585. * @brief Calculate Rx Buffer length - the number of bytes in the buffer.
  586. *
  587. * @param none
  588. *
  589. * @return length of current Rx Buffer
  590. **************************************************************************************************/
  591. static uint16 HalUARTRxAvailDMA(void)
  592. {
  593. uint16 cnt = 0;
  594. if (HAL_UART_DMA_NEW_RX_BYTE(dmaCfg.rxHead))
  595. {
  596. uint16 idx;
  597. for (idx = 0; idx < HAL_UART_DMA_RX_MAX; idx++)
  598. {
  599. if (HAL_UART_DMA_NEW_RX_BYTE(idx))
  600. {
  601. cnt++;
  602. }
  603. }
  604. }
  605. return cnt;
  606. }
  607. /******************************************************************************
  608. * @fn HalUARTSuspendDMA
  609. *
  610. * @brief Suspend UART hardware before entering PM mode 1, 2 or 3.
  611. *
  612. * @param None
  613. *
  614. * @return None
  615. *****************************************************************************/
  616. static void HalUARTSuspendDMA( void )
  617. {
  618. UxCSR &= ~CSR_RE;
  619. }
  620. /******************************************************************************
  621. * @fn HalUARTResumeDMA
  622. *
  623. * @brief Resume UART hardware after exiting PM mode 1, 2 or 3.
  624. *
  625. * @param None
  626. *
  627. * @return None
  628. *****************************************************************************/
  629. static void HalUARTResumeDMA( void )
  630. {
  631. UxUCR |= UCR_FLUSH;
  632. UxCSR |= CSR_RE;
  633. }
  634. /******************************************************************************
  635. * @fn HalUARTIsrDMA
  636. *
  637. * @brief Handle the Tx done DMA ISR.
  638. *
  639. * @param none
  640. *
  641. * @return none
  642. *****************************************************************************/
  643. void HalUARTIsrDMA(void);
  644. void HalUARTIsrDMA(void)
  645. {
  646. HAL_DMA_CLEAR_IRQ(HAL_DMA_CH_TX);
  647. // Indicate that the other buffer is free now.
  648. dmaCfg.txIdx[(dmaCfg.txSel ^ 1)] = 0;
  649. dmaCfg.txMT = TRUE;
  650. // Set TX shadow
  651. dmaCfg.txShdw = ST0;
  652. dmaCfg.txShdwValid = TRUE;
  653. // If there is more Tx data ready to go, re-start the DMA immediately on it.
  654. if (dmaCfg.txIdx[dmaCfg.txSel])
  655. {
  656. // UART TX DMA is expected to be fired
  657. dmaCfg.txDMAPending = TRUE;
  658. }
  659. }
  660. /******************************************************************************
  661. ******************************************************************************/