_hal_uart_dma.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /**************************************************************************************************
  2. Filename: _hal_uart_dma.c
  3. Revised: $Date: 2010-04-01 18:14:33 -0700 (Thu, 01 Apr 2010) $
  4. Revision: $Revision: 22068 $
  5. Description: This file contains the interface to the H/W UART driver by DMA.
  6. Copyright 2006-2010 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. #if defined POWER_SAVING
  48. #include "OSAL_PwrMgr.h"
  49. #endif
  50. #include "ZComDef.h"
  51. #include "ZGlobals.h"
  52. #include "ZMac.h"
  53. #include "znp_app.h"
  54. /*********************************************************************
  55. * MACROS
  56. */
  57. //#define HAL_UART_ASSERT(expr) HAL_ASSERT((expr))
  58. #define HAL_UART_ASSERT(expr)
  59. #if defined HAL_BOARD_CC2430EB || defined HAL_BOARD_CC2430DB || defined HAL_BOARD_CC2430BB
  60. #define HAL_UART_DMA_NEW_RX_BYTE(IDX) (DMA_PAD == LO_UINT16(dmaCfg.rxBuf[(IDX)]))
  61. #define HAL_UART_DMA_GET_RX_BYTE(IDX) (HI_UINT16(dmaCfg.rxBuf[(IDX)]))
  62. #define HAL_UART_DMA_CLR_RX_BYTE(IDX) (dmaCfg.rxBuf[(IDX)] = BUILD_UINT16((DMA_PAD ^ 0xFF), 0))
  63. #else
  64. #define HAL_UART_DMA_NEW_RX_BYTE(IDX) (DMA_PAD == HI_UINT16(dmaCfg.rxBuf[(IDX)]))
  65. #define HAL_UART_DMA_GET_RX_BYTE(IDX) (LO_UINT16(dmaCfg.rxBuf[(IDX)]))
  66. #define HAL_UART_DMA_CLR_RX_BYTE(IDX) (dmaCfg.rxBuf[(IDX)] = BUILD_UINT16(0, (DMA_PAD ^ 0xFF)))
  67. #endif
  68. /*********************************************************************
  69. * CONSTANTS
  70. */
  71. // UxCSR - USART Control and Status Register.
  72. #define CSR_MODE 0x80
  73. #define CSR_RE 0x40
  74. #define CSR_SLAVE 0x20
  75. #define CSR_FE 0x10
  76. #define CSR_ERR 0x08
  77. #define CSR_RX_BYTE 0x04
  78. #define CSR_TX_BYTE 0x02
  79. #define CSR_ACTIVE 0x01
  80. // UxUCR - USART UART Control Register.
  81. #define UCR_FLUSH 0x80
  82. #define UCR_FLOW 0x40
  83. #define UCR_D9 0x20
  84. #define UCR_BIT9 0x10
  85. #define UCR_PARITY 0x08
  86. #define UCR_SPB 0x04
  87. #define UCR_STOP 0x02
  88. #define UCR_START 0x01
  89. #define UTX0IE 0x04
  90. #define UTX1IE 0x08
  91. #define P2DIR_PRIPO 0xC0
  92. // Incompatible redefinitions result with more than one UART driver sub-module.
  93. #undef PxOUT
  94. #undef PxDIR
  95. #undef PxSEL
  96. #undef UxCSR
  97. #undef UxUCR
  98. #undef UxDBUF
  99. #undef UxBAUD
  100. #undef UxGCR
  101. #undef URXxIE
  102. #undef URXxIF
  103. #undef UTXxIE
  104. #undef UTXxIF
  105. #undef HAL_UART_PERCFG_BIT
  106. #undef HAL_UART_Px_RTS
  107. #undef HAL_UART_Px_CTS
  108. #undef HAL_UART_Px_RX_TX
  109. #if (HAL_UART_DMA == 1)
  110. #define PxOUT P0
  111. #define PxIN P0
  112. #define PxDIR P0DIR
  113. #define PxSEL P0SEL
  114. #define UxCSR U0CSR
  115. #define UxUCR U0UCR
  116. #define UxDBUF U0DBUF
  117. #define UxBAUD U0BAUD
  118. #define UxGCR U0GCR
  119. #define URXxIE URX0IE
  120. #define URXxIF URX0IF
  121. #define UTXxIE UTX0IE
  122. #define UTXxIF UTX0IF
  123. #else
  124. #define PxOUT P1
  125. #define PxIN P1
  126. #define PxDIR P1DIR
  127. #define PxSEL P1SEL
  128. #define UxCSR U1CSR
  129. #define UxUCR U1UCR
  130. #define UxDBUF U1DBUF
  131. #define UxBAUD U1BAUD
  132. #define UxGCR U1GCR
  133. #define URXxIE URX1IE
  134. #define URXxIF URX1IF
  135. #define UTXxIE UTX1IE
  136. #define UTXxIF UTX1IF
  137. #endif
  138. #if (HAL_UART_DMA == 1)
  139. #define HAL_UART_PERCFG_BIT 0x01 // USART0 on P0, Alt-1; so clear this bit.
  140. #define HAL_UART_Px_RX_TX 0x0C // Peripheral I/O Select for Rx/Tx.
  141. #define HAL_UART_Px_RTS 0x20 // Peripheral I/O Select for RTS.
  142. #define HAL_UART_Px_CTS 0x10 // Peripheral I/O Select for CTS.
  143. #else
  144. #define HAL_UART_PERCFG_BIT 0x02 // USART1 on P1, Alt-2; so set this bit.
  145. #define HAL_UART_Px_RTS 0x20 // Peripheral I/O Select for RTS.
  146. #define HAL_UART_Px_CTS 0x10 // Peripheral I/O Select for CTS.
  147. #define HAL_UART_Px_RX_TX 0xC0 // Peripheral I/O Select for Rx/Tx.
  148. #endif
  149. // The timeout tick is at 32-kHz, so multiply msecs by 33.
  150. #define HAL_UART_MSECS_TO_TICKS 33
  151. #if defined MT_TASK
  152. #define HAL_UART_DMA_TX_MAX MT_UART_DEFAULT_MAX_TX_BUFF
  153. #define HAL_UART_DMA_RX_MAX MT_UART_DEFAULT_MAX_RX_BUFF
  154. #define HAL_UART_DMA_HIGH MT_UART_DEFAULT_THRESHOLD
  155. #define HAL_UART_DMA_IDLE (MT_UART_DEFAULT_IDLE_TIMEOUT * HAL_UART_MSECS_TO_TICKS)
  156. #else
  157. #if !defined HAL_UART_DMA_RX_MAX
  158. #define HAL_UART_DMA_RX_MAX 256
  159. #endif
  160. #if !defined HAL_UART_DMA_TX_MAX
  161. #define HAL_UART_DMA_TX_MAX HAL_UART_DMA_RX_MAX
  162. #endif
  163. #if !defined HAL_UART_DMA_HIGH
  164. #define HAL_UART_DMA_HIGH (HAL_UART_DMA_RX_MAX / 2 - 16)
  165. #endif
  166. #if !defined HAL_UART_DMA_IDLE
  167. #define HAL_UART_DMA_IDLE (1 * HAL_UART_MSECS_TO_TICKS)
  168. #endif
  169. #endif
  170. #if !defined HAL_UART_DMA_FULL
  171. #define HAL_UART_DMA_FULL (HAL_UART_DMA_RX_MAX - 16)
  172. #endif
  173. #if defined HAL_BOARD_CC2430EB || defined HAL_BOARD_CC2430DB || defined HAL_BOARD_CC2430BB
  174. #define HAL_DMA_U0DBUF 0xDFC1
  175. #define HAL_DMA_U1DBUF 0xDFF9
  176. #else /* CC2530 */
  177. #define HAL_DMA_U0DBUF 0x70C1
  178. #define HAL_DMA_U1DBUF 0x70F9
  179. #endif
  180. #if (HAL_UART_DMA == 1)
  181. #define DMATRIG_RX HAL_DMA_TRIG_URX0
  182. #define DMATRIG_TX HAL_DMA_TRIG_UTX0
  183. #define DMA_UDBUF HAL_DMA_U0DBUF
  184. #define DMA_PAD U0BAUD
  185. #else
  186. #define DMATRIG_RX HAL_DMA_TRIG_URX1
  187. #define DMATRIG_TX HAL_DMA_TRIG_UTX1
  188. #define DMA_UDBUF HAL_DMA_U1DBUF
  189. #define DMA_PAD U1BAUD
  190. #endif
  191. /*********************************************************************
  192. * TYPEDEFS
  193. */
  194. #if HAL_UART_DMA_RX_MAX <= 256
  195. typedef uint8 rxIdx_t;
  196. #else
  197. typedef uint16 rxIdx_t;
  198. #endif
  199. #if HAL_UART_DMA_TX_MAX <= 256
  200. typedef uint8 txIdx_t;
  201. #else
  202. typedef uint16 txIdx_t;
  203. #endif
  204. typedef struct
  205. {
  206. uint16 rxBuf[HAL_UART_DMA_RX_MAX];
  207. rxIdx_t rxHead;
  208. rxIdx_t rxTail;
  209. uint8 rxTick;
  210. uint8 rxShdw;
  211. uint8 txBuf[2][HAL_UART_DMA_TX_MAX];
  212. txIdx_t txIdx[2];
  213. volatile uint8 txSel;
  214. uint8 txMT;
  215. uint8 txTick; // 1-character time in 32kHz ticks according to baud rate,
  216. // to be used in calculating time lapse since DMA ISR
  217. // to allow delay margin before start firing DMA, so that
  218. // DMA does not overwrite UART DBUF of previous packet
  219. volatile uint8 txShdw; // Sleep Timer LSB shadow.
  220. volatile uint8 txShdwValid; // TX shadow value is valid
  221. uint8 txDMAPending; // UART TX DMA is pending
  222. halUARTCBack_t uartCB;
  223. } uartDMACfg_t;
  224. /*********************************************************************
  225. * GLOBAL VARIABLES
  226. */
  227. /*********************************************************************
  228. * GLOBAL FUNCTIONS
  229. */
  230. void HalUARTIsrDMA(void);
  231. /*********************************************************************
  232. * LOCAL VARIABLES
  233. */
  234. static uartDMACfg_t dmaCfg;
  235. /*********************************************************************
  236. * LOCAL FUNCTIONS
  237. */
  238. static rxIdx_t findTail(void);
  239. // Invoked by functions in hal_uart.c when this file is included.
  240. static void HalUARTInitDMA(void);
  241. static void HalUARTOpenDMA(halUARTCfg_t *config);
  242. static uint16 HalUARTReadDMA(uint8 *buf, uint16 len);
  243. static uint16 HalUARTWriteDMA(uint8 *buf, uint16 len);
  244. static void HalUARTPollDMA(void);
  245. static uint16 HalUARTRxAvailDMA(void);
  246. static void HalUARTSuspendDMA(void);
  247. static void HalUARTResumeDMA(void);
  248. /*****************************************************************************
  249. * @fn findTail
  250. *
  251. * @brief Find the rxBuf index where the DMA RX engine is working.
  252. *
  253. * @param None.
  254. *
  255. * @return Index of tail of rxBuf.
  256. *****************************************************************************/
  257. static rxIdx_t findTail(void)
  258. {
  259. rxIdx_t idx = dmaCfg.rxHead;
  260. do
  261. {
  262. if (!HAL_UART_DMA_NEW_RX_BYTE(idx))
  263. {
  264. break;
  265. }
  266. #if HAL_UART_DMA_RX_MAX == 256
  267. idx++;
  268. #else
  269. if (++idx >= HAL_UART_DMA_RX_MAX)
  270. {
  271. idx = 0;
  272. }
  273. #endif
  274. } while (idx != dmaCfg.rxHead);
  275. return idx;
  276. }
  277. /******************************************************************************
  278. * @fn HalUARTInitDMA
  279. *
  280. * @brief Initialize the UART
  281. *
  282. * @param none
  283. *
  284. * @return none
  285. *****************************************************************************/
  286. static void HalUARTInitDMA(void)
  287. {
  288. halDMADesc_t *ch;
  289. P2DIR &= ~P2DIR_PRIPO;
  290. P2DIR |= HAL_UART_PRIPO;
  291. #if (HAL_UART_DMA == 1)
  292. PERCFG &= ~HAL_UART_PERCFG_BIT; // Set UART0 I/O to Alt. 1 location on P0.
  293. #else
  294. PERCFG |= HAL_UART_PERCFG_BIT; // Set UART1 I/O to Alt. 2 location on P1.
  295. #endif
  296. PxSEL |= HAL_UART_Px_RX_TX; // Enable Tx and Rx on P1.
  297. ADCCFG &= ~HAL_UART_Px_RX_TX; // Make sure ADC doesnt use this.
  298. UxCSR = CSR_MODE; // Mode is UART Mode.
  299. UxUCR = UCR_FLUSH; // Flush it.
  300. // Setup Tx by DMA.
  301. ch = HAL_DMA_GET_DESC1234( HAL_DMA_CH_TX );
  302. // The start address of the destination.
  303. HAL_DMA_SET_DEST( ch, DMA_UDBUF );
  304. // Using the length field to determine how many bytes to transfer.
  305. HAL_DMA_SET_VLEN( ch, HAL_DMA_VLEN_USE_LEN );
  306. // One byte is transferred each time.
  307. HAL_DMA_SET_WORD_SIZE( ch, HAL_DMA_WORDSIZE_BYTE );
  308. // The bytes are transferred 1-by-1 on Tx Complete trigger.
  309. HAL_DMA_SET_TRIG_MODE( ch, HAL_DMA_TMODE_SINGLE );
  310. HAL_DMA_SET_TRIG_SRC( ch, DMATRIG_TX );
  311. // The source address is incremented by 1 byte after each transfer.
  312. HAL_DMA_SET_SRC_INC( ch, HAL_DMA_SRCINC_1 );
  313. // The destination address is constant - the Tx Data Buffer.
  314. HAL_DMA_SET_DST_INC( ch, HAL_DMA_DSTINC_0 );
  315. // The DMA Tx done is serviced by ISR in order to maintain full thruput.
  316. HAL_DMA_SET_IRQ( ch, HAL_DMA_IRQMASK_ENABLE );
  317. // Xfer all 8 bits of a byte xfer.
  318. HAL_DMA_SET_M8( ch, HAL_DMA_M8_USE_8_BITS );
  319. // DMA has highest priority for memory access.
  320. HAL_DMA_SET_PRIORITY( ch, HAL_DMA_PRI_HIGH );
  321. // Setup Rx by DMA.
  322. ch = HAL_DMA_GET_DESC1234( HAL_DMA_CH_RX );
  323. // The start address of the source.
  324. HAL_DMA_SET_SOURCE( ch, DMA_UDBUF );
  325. // Using the length field to determine how many bytes to transfer.
  326. HAL_DMA_SET_VLEN( ch, HAL_DMA_VLEN_USE_LEN );
  327. /* The trick is to cfg DMA to xfer 2 bytes for every 1 byte of Rx.
  328. * The byte after the Rx Data Buffer is the Baud Cfg Register,
  329. * which always has a known value. So init Rx buffer to inverse of that
  330. * known value. DMA word xfer will flip the bytes, so every valid Rx byte
  331. * in the Rx buffer will be preceded by a DMA_PAD char equal to the
  332. * Baud Cfg Register value.
  333. */
  334. HAL_DMA_SET_WORD_SIZE( ch, HAL_DMA_WORDSIZE_WORD );
  335. // The bytes are transferred 1-by-1 on Rx Complete trigger.
  336. HAL_DMA_SET_TRIG_MODE( ch, HAL_DMA_TMODE_SINGLE_REPEATED );
  337. HAL_DMA_SET_TRIG_SRC( ch, DMATRIG_RX );
  338. // The source address is constant - the Rx Data Buffer.
  339. HAL_DMA_SET_SRC_INC( ch, HAL_DMA_SRCINC_0 );
  340. // The destination address is incremented by 1 word after each transfer.
  341. HAL_DMA_SET_DST_INC( ch, HAL_DMA_DSTINC_1 );
  342. HAL_DMA_SET_DEST( ch, dmaCfg.rxBuf );
  343. HAL_DMA_SET_LEN( ch, HAL_UART_DMA_RX_MAX );
  344. // The DMA is to be polled and shall not issue an IRQ upon completion.
  345. HAL_DMA_SET_IRQ( ch, HAL_DMA_IRQMASK_DISABLE );
  346. // Xfer all 8 bits of a byte xfer.
  347. HAL_DMA_SET_M8( ch, HAL_DMA_M8_USE_8_BITS );
  348. // DMA has highest priority for memory access.
  349. HAL_DMA_SET_PRIORITY( ch, HAL_DMA_PRI_HIGH );
  350. }
  351. /******************************************************************************
  352. * @fn HalUARTOpenDMA
  353. *
  354. * @brief Open a port according tp the configuration specified by parameter.
  355. *
  356. * @param config - contains configuration information
  357. *
  358. * @return none
  359. *****************************************************************************/
  360. static void HalUARTOpenDMA(halUARTCfg_t *config)
  361. {
  362. dmaCfg.uartCB = config->callBackFunc;
  363. // Only supporting subset of baudrate for code size - other is possible.
  364. HAL_UART_ASSERT((config->baudRate == HAL_UART_BR_9600) ||
  365. (config->baudRate == HAL_UART_BR_19200) ||
  366. (config->baudRate == HAL_UART_BR_38400) ||
  367. (config->baudRate == HAL_UART_BR_57600) ||
  368. (config->baudRate == HAL_UART_BR_115200));
  369. if (config->baudRate == HAL_UART_BR_57600 ||
  370. config->baudRate == HAL_UART_BR_115200)
  371. {
  372. UxBAUD = 216;
  373. }
  374. else
  375. {
  376. UxBAUD = 59;
  377. }
  378. switch (config->baudRate)
  379. {
  380. case HAL_UART_BR_9600:
  381. UxGCR = 8;
  382. dmaCfg.txTick = 35; // (32768Hz / (9600bps / 10 bits))
  383. // 10 bits include start and stop bits.
  384. break;
  385. case HAL_UART_BR_19200:
  386. UxGCR = 9;
  387. dmaCfg.txTick = 18;
  388. break;
  389. case HAL_UART_BR_38400:
  390. UxGCR = 10;
  391. dmaCfg.txTick = 9;
  392. break;
  393. case HAL_UART_BR_57600:
  394. UxGCR = 10;
  395. dmaCfg.txTick = 6;
  396. break;
  397. default:
  398. // HAL_UART_BR_115200
  399. UxGCR = 11;
  400. dmaCfg.txTick = 3;
  401. break;
  402. }
  403. // 8 bits/char; no parity; 1 stop bit; stop bit hi.
  404. if (config->flowControl)
  405. {
  406. UxUCR = UCR_FLOW | UCR_STOP;
  407. PxSEL |= HAL_UART_Px_CTS;
  408. // DMA Rx is always on (self-resetting). So flow must be controlled by the S/W polling the Rx
  409. // buffer level. Start by allowing flow.
  410. PxOUT &= ~HAL_UART_Px_RTS;
  411. PxDIR |= HAL_UART_Px_RTS;
  412. // Trigger an interrupt on CTS input (enabled in HalUARTSuspendDMA).
  413. P0IFG &= ~HAL_UART_Px_CTS;
  414. P0IEN &= ~HAL_UART_Px_CTS;
  415. P0IE = 1;
  416. }
  417. else
  418. {
  419. UxUCR = UCR_STOP;
  420. }
  421. dmaCfg.rxBuf[0] = *(volatile uint8 *)DMA_UDBUF; // Clear the DMA Rx trigger.
  422. HAL_DMA_CLEAR_IRQ(HAL_DMA_CH_RX);
  423. HAL_DMA_ARM_CH(HAL_DMA_CH_RX);
  424. osal_memset(dmaCfg.rxBuf, (DMA_PAD ^ 0xFF), HAL_UART_DMA_RX_MAX*2);
  425. UxCSR |= CSR_RE;
  426. // Initialize that TX DMA is not pending
  427. dmaCfg.txDMAPending = FALSE;
  428. dmaCfg.txShdwValid = FALSE;
  429. }
  430. /*****************************************************************************
  431. * @fn HalUARTReadDMA
  432. *
  433. * @brief Read a buffer from the UART
  434. *
  435. * @param buf - valid data buffer at least 'len' bytes in size
  436. * len - max length number of bytes to copy to 'buf'
  437. *
  438. * @return length of buffer that was read
  439. *****************************************************************************/
  440. static uint16 HalUARTReadDMA(uint8 *buf, uint16 len)
  441. {
  442. uint16 cnt;
  443. for (cnt = 0; cnt < len; cnt++)
  444. {
  445. if (!HAL_UART_DMA_NEW_RX_BYTE(dmaCfg.rxHead))
  446. {
  447. break;
  448. }
  449. *buf++ = HAL_UART_DMA_GET_RX_BYTE(dmaCfg.rxHead);
  450. HAL_UART_DMA_CLR_RX_BYTE(dmaCfg.rxHead);
  451. #if HAL_UART_DMA_RX_MAX == 256
  452. (dmaCfg.rxHead)++;
  453. #else
  454. if (++(dmaCfg.rxHead) >= HAL_UART_DMA_RX_MAX)
  455. {
  456. dmaCfg.rxHead = 0;
  457. }
  458. #endif
  459. }
  460. PxOUT &= ~HAL_UART_Px_RTS; // Re-enable the flow on any read.
  461. return cnt;
  462. }
  463. /******************************************************************************
  464. * @fn HalUARTWriteDMA
  465. *
  466. * @brief Write a buffer to the UART.
  467. *
  468. * @param buf - pointer to the buffer that will be written, not freed
  469. * len - length of
  470. *
  471. * @return length of the buffer that was sent
  472. *****************************************************************************/
  473. static uint16 HalUARTWriteDMA(uint8 *buf, uint16 len)
  474. {
  475. uint16 cnt;
  476. halIntState_t his;
  477. uint8 txSel;
  478. txIdx_t txIdx;
  479. // Enforce all or none.
  480. if ((len + dmaCfg.txIdx[dmaCfg.txSel]) > HAL_UART_DMA_TX_MAX)
  481. {
  482. return 0;
  483. }
  484. HAL_ENTER_CRITICAL_SECTION(his);
  485. txSel = dmaCfg.txSel;
  486. txIdx = dmaCfg.txIdx[txSel];
  487. HAL_EXIT_CRITICAL_SECTION(his);
  488. for (cnt = 0; cnt < len; cnt++)
  489. {
  490. dmaCfg.txBuf[txSel][txIdx++] = buf[cnt];
  491. }
  492. HAL_ENTER_CRITICAL_SECTION(his);
  493. if (txSel != dmaCfg.txSel)
  494. {
  495. HAL_EXIT_CRITICAL_SECTION(his);
  496. txSel = dmaCfg.txSel;
  497. txIdx = dmaCfg.txIdx[txSel];
  498. for (cnt = 0; cnt < len; cnt++)
  499. {
  500. dmaCfg.txBuf[txSel][txIdx++] = buf[cnt];
  501. }
  502. HAL_ENTER_CRITICAL_SECTION(his);
  503. }
  504. dmaCfg.txIdx[txSel] = txIdx;
  505. if (dmaCfg.txIdx[(txSel ^ 1)] == 0)
  506. {
  507. // TX DMA is expected to be fired
  508. dmaCfg.txDMAPending = TRUE;
  509. }
  510. HAL_EXIT_CRITICAL_SECTION(his);
  511. return cnt;
  512. }
  513. /******************************************************************************
  514. * @fn HalUARTPollDMA
  515. *
  516. * @brief Poll a USART module implemented by DMA.
  517. *
  518. * @param none
  519. *
  520. * @return none
  521. *****************************************************************************/
  522. static void HalUARTPollDMA(void)
  523. {
  524. uint16 cnt = 0;
  525. uint8 evt = 0;
  526. if (HAL_UART_DMA_NEW_RX_BYTE(dmaCfg.rxHead))
  527. {
  528. rxIdx_t tail = findTail();
  529. // If the DMA has transferred in more Rx bytes, reset the Rx idle timer.
  530. if (dmaCfg.rxTail != tail)
  531. {
  532. dmaCfg.rxTail = tail;
  533. // Re-sync the shadow on any 1st byte(s) received.
  534. if (dmaCfg.rxTick == 0)
  535. {
  536. dmaCfg.rxShdw = ST0;
  537. }
  538. dmaCfg.rxTick = HAL_UART_DMA_IDLE;
  539. }
  540. else if (dmaCfg.rxTick)
  541. {
  542. // Use the LSB of the sleep timer (ST0 must be read first anyway).
  543. uint8 decr = ST0 - dmaCfg.rxShdw;
  544. if (dmaCfg.rxTick > decr)
  545. {
  546. dmaCfg.rxTick -= decr;
  547. dmaCfg.rxShdw = ST0;
  548. }
  549. else
  550. {
  551. dmaCfg.rxTick = 0;
  552. }
  553. }
  554. cnt = HalUARTRxAvailDMA();
  555. }
  556. else
  557. {
  558. dmaCfg.rxTick = 0;
  559. }
  560. if (cnt >= HAL_UART_DMA_FULL)
  561. {
  562. evt = HAL_UART_RX_FULL;
  563. }
  564. else if (cnt >= HAL_UART_DMA_HIGH)
  565. {
  566. evt = HAL_UART_RX_ABOUT_FULL;
  567. PxOUT |= HAL_UART_Px_RTS; // Disable Rx flow.
  568. }
  569. else if (cnt && !dmaCfg.rxTick)
  570. {
  571. evt = HAL_UART_RX_TIMEOUT;
  572. }
  573. if (dmaCfg.txMT)
  574. {
  575. dmaCfg.txMT = FALSE;
  576. evt |= HAL_UART_TX_EMPTY;
  577. }
  578. if (dmaCfg.txShdwValid)
  579. {
  580. uint8 decr = ST0;
  581. decr -= dmaCfg.txShdw;
  582. if (decr > dmaCfg.txTick)
  583. {
  584. // No protection for txShdwValid is required
  585. // because while the shadow was valid, DMA ISR cannot be triggered
  586. // to cause concurrent access to this variable.
  587. dmaCfg.txShdwValid = FALSE;
  588. }
  589. }
  590. if (dmaCfg.txDMAPending && !dmaCfg.txShdwValid)
  591. {
  592. // UART TX DMA is expected to be fired and enough time has lapsed since last DMA ISR
  593. // to know that DBUF can be overwritten
  594. halDMADesc_t *ch = HAL_DMA_GET_DESC1234(HAL_DMA_CH_TX);
  595. halIntState_t intState;
  596. // Clear the DMA pending flag
  597. dmaCfg.txDMAPending = FALSE;
  598. HAL_DMA_SET_SOURCE(ch, dmaCfg.txBuf[dmaCfg.txSel]);
  599. HAL_DMA_SET_LEN(ch, dmaCfg.txIdx[dmaCfg.txSel]);
  600. dmaCfg.txSel ^= 1;
  601. HAL_ENTER_CRITICAL_SECTION(intState);
  602. HAL_DMA_ARM_CH(HAL_DMA_CH_TX);
  603. do
  604. {
  605. asm("NOP");
  606. } while (!HAL_DMA_CH_ARMED(HAL_DMA_CH_TX));
  607. HAL_DMA_CLEAR_IRQ(HAL_DMA_CH_TX);
  608. HAL_DMA_MAN_TRIGGER(HAL_DMA_CH_TX);
  609. HAL_EXIT_CRITICAL_SECTION(intState);
  610. }
  611. else
  612. {
  613. halIntState_t his;
  614. HAL_ENTER_CRITICAL_SECTION(his);
  615. if ((dmaCfg.txIdx[dmaCfg.txSel] != 0) && !HAL_DMA_CH_ARMED(HAL_DMA_CH_TX)
  616. && !HAL_DMA_CHECK_IRQ(HAL_DMA_CH_TX))
  617. {
  618. HAL_EXIT_CRITICAL_SECTION(his);
  619. HalUARTIsrDMA();
  620. }
  621. else
  622. {
  623. HAL_EXIT_CRITICAL_SECTION(his);
  624. }
  625. }
  626. if (evt && (dmaCfg.uartCB != NULL))
  627. {
  628. dmaCfg.uartCB(HAL_UART_DMA-1, evt);
  629. }
  630. #if defined POWER_SAVING
  631. /* A simple ZAP application sending a unicast at 2-Hz was seen to bog down to < 1-Hz OTA unicast
  632. * when the ZNP was configured to be a ZED (i.e. POWER_SAVING was enabled). So adding this delay
  633. * of only 10 msecs before re-enabling CONSERVE showed that the problem was fixed while still
  634. * allowing the ZNP to enter sleep.
  635. */
  636. static uint8 znpUartActiveShdw;
  637. // If a ZED and no ongoing Rx/Tx and the UART master is not asserting CTS.
  638. if (ZG_DEVICE_ENDDEVICE_TYPE && !(UxCSR & CSR_ACTIVE) && (PxIN & HAL_UART_Px_CTS))
  639. {
  640. if (znpUartActiveShdw)
  641. {
  642. uint8 rxOnIdle;
  643. (void)ZMacGetReq(ZMacRxOnIdle, &rxOnIdle);
  644. if (!rxOnIdle)
  645. {
  646. znpUartActiveShdw = FALSE;
  647. if (ZSuccess != osal_start_timerEx(znpTaskId, ZNP_PWRMGR_CONSERVE_EVENT,
  648. ZNP_PWRMGR_CONSERVE_DELAY))
  649. {
  650. (void)osal_set_event(znpTaskId, ZNP_PWRMGR_CONSERVE_EVENT);
  651. }
  652. }
  653. }
  654. }
  655. else if (!znpUartActiveShdw)
  656. {
  657. znpUartActiveShdw = TRUE;
  658. (void)osal_stop_timerEx(znpTaskId, ZNP_PWRMGR_CONSERVE_EVENT);
  659. (void)osal_clear_event(znpTaskId, ZNP_PWRMGR_CONSERVE_EVENT);
  660. (void)osal_pwrmgr_task_state(znpTaskId, PWRMGR_HOLD);
  661. }
  662. #endif
  663. }
  664. /**************************************************************************************************
  665. * @fn HalUARTRxAvailDMA()
  666. *
  667. * @brief Calculate Rx Buffer length - the number of bytes in the buffer.
  668. *
  669. * @param none
  670. *
  671. * @return length of current Rx Buffer
  672. **************************************************************************************************/
  673. static uint16 HalUARTRxAvailDMA(void)
  674. {
  675. uint16 cnt = 0;
  676. if (HAL_UART_DMA_NEW_RX_BYTE(dmaCfg.rxHead))
  677. {
  678. uint16 idx;
  679. for (idx = 0; idx < HAL_UART_DMA_RX_MAX; idx++)
  680. {
  681. if (HAL_UART_DMA_NEW_RX_BYTE(idx))
  682. {
  683. cnt++;
  684. }
  685. }
  686. }
  687. return cnt;
  688. }
  689. /******************************************************************************
  690. * @fn HalUARTSuspendDMA
  691. *
  692. * @brief Suspend UART hardware before entering PM mode 1, 2 or 3.
  693. *
  694. * @param None
  695. *
  696. * @return None
  697. *****************************************************************************/
  698. static void HalUARTSuspendDMA( void )
  699. {
  700. PxOUT |= HAL_UART_Px_RTS; // Disable Rx flow.
  701. UxCSR &= ~CSR_RE;
  702. P0IEN |= HAL_UART_Px_CTS; // Enable the CTS ISR.
  703. }
  704. /******************************************************************************
  705. * @fn HalUARTResumeDMA
  706. *
  707. * @brief Resume UART hardware after exiting PM mode 1, 2 or 3.
  708. *
  709. * @param None
  710. *
  711. * @return None
  712. *****************************************************************************/
  713. static void HalUARTResumeDMA( void )
  714. {
  715. P0IEN &= ~HAL_UART_Px_CTS; // Disable the CTS ISR.
  716. UxUCR |= UCR_FLUSH;
  717. UxCSR |= CSR_RE;
  718. PxOUT &= ~HAL_UART_Px_RTS; // Re-enable Rx flow.
  719. }
  720. /******************************************************************************
  721. * @fn HalUARTIsrDMA
  722. *
  723. * @brief Handle the Tx done DMA ISR.
  724. *
  725. * @param none
  726. *
  727. * @return none
  728. *****************************************************************************/
  729. void HalUARTIsrDMA(void);
  730. void HalUARTIsrDMA(void)
  731. {
  732. HAL_DMA_CLEAR_IRQ(HAL_DMA_CH_TX);
  733. // Indicate that the other buffer is free now.
  734. dmaCfg.txIdx[(dmaCfg.txSel ^ 1)] = 0;
  735. dmaCfg.txMT = TRUE;
  736. // Set TX shadow
  737. dmaCfg.txShdw = ST0;
  738. dmaCfg.txShdwValid = TRUE;
  739. // If there is more Tx data ready to go, re-start the DMA immediately on it.
  740. if (dmaCfg.txIdx[dmaCfg.txSel])
  741. {
  742. // UART TX DMA is expected to be fired
  743. dmaCfg.txDMAPending = TRUE;
  744. }
  745. }
  746. /******************************************************************************
  747. ******************************************************************************/