mapp_uart1.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. //==================================================================
  2. //
  3. // Filename: app_uart2.c
  4. // Description:
  5. //
  6. //------------------------------------------------------------------
  7. //
  8. // version:
  9. // data: 18:24, 05.Seb.2019, written by dwd
  10. // Description:
  11. //
  12. //------------------------------------------------------------------
  13. //==================================================================
  14. /*------------------------------------------------------------------------------
  15. * I N C L U D E
  16. *------------------------------------------------------------------------------*/
  17. #include <string.h>
  18. #include "stm32f0xx.h"
  19. #include "general.h"
  20. #include "main.h"
  21. #include "mapp_uart1.h"
  22. #include "mapp_pwr.h"
  23. #include "mapp_arm.h"
  24. #include "mapp_gpio.h"
  25. /*------------------------------------------------------------------------------
  26. * D E F I N E
  27. *------------------------------------------------------------------------------*/
  28. /*------------------------------------------------------------------------------
  29. * S T R U C T
  30. *------------------------------------------------------------------------------*/
  31. /*------------------------------------------------------------------------------
  32. * S T A T I C - V A R I A B L E S
  33. *------------------------------------------------------------------------------*/
  34. T_COM_UART1 s_tUart1Cfg = {0}; /* Com for DVD */
  35. T_COM_UART1 s_tUart2Cfg = {0};
  36. /*------------------------------------------------------------------------------
  37. * G L O B A L
  38. *------------------------------------------------------------------------------*/
  39. /*------------------------------------------------------------------------------
  40. * P R O T O T Y P E
  41. *------------------------------------------------------------------------------*/
  42. static void s_Uart1VarInit(void);
  43. static BOOL s_Uart1RxbufEmpty(void);
  44. static void s_Uart2VarInit(void);
  45. static BOOL s_Uart2RxbufEmpty(void);
  46. //static BOOL s_Uart1TxbufEmpty(void);
  47. /*----------------------------------------------------------------------
  48. // Function uses : Uart1AndUart2CfgInit
  49. // Input parameter :
  50. // Output parameter :
  51. // Use Function : for DVD communication
  52. // Reserve date : 18:24, 05.Seb.2019, written by dwd
  53. ----------------------------------------------------------------------*/
  54. //void Uart1CfgInit(void)
  55. //{
  56. // GPIO_InitTypeDef GPIO_InitStructure;
  57. // USART_InitTypeDef USART_InitStructure = {0};
  58. // /* Init variables ------------------------------------------------*/
  59. // s_Uart1VarInit();
  60. // /* Enable peripheral clocks ------------------------------------------------*/
  61. // /* Enable USART Clock */
  62. // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);
  63. // RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  64. //
  65. //
  66. // GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_0); //uart1——tx stm32f030C8
  67. // GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_0);
  68. //
  69. // /* Configure pins as AF pushpull */
  70. // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;//TXRX
  71. // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  72. // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  73. // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  74. // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  75. // GPIO_Init(GPIOA, &GPIO_InitStructure);
  76. // /* USART1 configuration ------------------------------------------------------*/
  77. // USART_InitStructure.USART_BaudRate = 115200;
  78. // USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  79. // USART_InitStructure.USART_StopBits = USART_StopBits_1;
  80. // USART_InitStructure.USART_Parity = USART_Parity_No;
  81. // USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  82. // USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  83. // USART_Init(USART1, &USART_InitStructure);
  84. //
  85. // /* Enable the USART */
  86. // USART_Cmd(USART1, ENABLE);
  87. // /* Enable USART3 Receive and Transmit interrupts */
  88. // USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  89. // USART_ITConfig(USART1, USART_IT_TC, ENABLE);
  90. // USART_ClearFlag(USART1, USART_FLAG_TC);
  91. //
  92. //}
  93. void Uart1CfgInit(void)
  94. {
  95. GPIO_InitTypeDef GPIO_InitStructure;
  96. USART_InitTypeDef USART_InitStructure = {0};
  97. /* Init variables ------------------------------------------------*/
  98. s_Uart1VarInit();
  99. /* Enable peripheral clocks ------------------------------------------------*/
  100. /* Enable USART Clock */
  101. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); // 应该是 GPIOB
  102. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  103. /* Configure pins as AF pushpull */
  104. GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_1); // uart1——tx -> PB6
  105. GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_1); // uart1——rx -> PB7
  106. /* Configure pins as AF pushpull */
  107. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // TX RX
  108. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  109. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  110. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  111. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  112. GPIO_Init(GPIOB, &GPIO_InitStructure); // 应该是 GPIOB
  113. /* USART1 configuration ------------------------------------------------------*/
  114. USART_InitStructure.USART_BaudRate = 115200;
  115. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  116. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  117. USART_InitStructure.USART_Parity = USART_Parity_No;
  118. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  119. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  120. USART_Init(USART1, &USART_InitStructure);
  121. /* Enable the USART */
  122. USART_Cmd(USART1, ENABLE);
  123. /* Enable USART1 Receive and Transmit interrupts */
  124. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  125. USART_ITConfig(USART1, USART_IT_TC, ENABLE);
  126. USART_ClearFlag(USART1, USART_FLAG_TC);
  127. }
  128. void Uart2CfgInit(void)
  129. {
  130. GPIO_InitTypeDef GPIO_InitStructure;
  131. USART_InitTypeDef USART_InitStructure = {0};
  132. /* Init variables ------------------------------------------------*/
  133. s_Uart2VarInit();
  134. /* Enable peripheral clocks ------------------------------------------------*/
  135. /* Enable USART Clock */
  136. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
  137. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  138. GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1); //uart2 stm32f030C8
  139. GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
  140. /* Configure pins as AF pushpull */
  141. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;//TXRX
  142. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  143. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  144. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  145. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  146. GPIO_Init(GPIOA, &GPIO_InitStructure);
  147. USART_InitStructure.USART_BaudRate = 115200;
  148. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  149. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  150. USART_InitStructure.USART_Parity = USART_Parity_No;
  151. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  152. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  153. USART_Init(USART2, &USART_InitStructure);
  154. /* Enable the USART */
  155. USART_Cmd(USART2, ENABLE);
  156. /* Enable USART3 Receive and Transmit interrupts */
  157. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  158. USART_ITConfig(USART2, USART_IT_TC, ENABLE);
  159. USART_ClearFlag(USART2, USART_FLAG_TC);
  160. }
  161. /*----------------------------------------------------------------------
  162. // Function uses : UartISRHandler
  163. // Input parameter :
  164. // Output parameter :
  165. // Use Function :
  166. // Reserve date : 18:24, 05.Seb.2019, written by dwd
  167. ----------------------------------------------------------------------*/
  168. void Uart1ISRHandler(void)
  169. {
  170. T_COM_UART1 *pbuf;
  171. U08 rdata = 0;
  172. static U08 rstep = 0;
  173. pbuf = &s_tUart1Cfg;
  174. //------------------------------------------------------------------
  175. // for Tx compelete interrupt routine
  176. // 19:50pm, 11.Oct.2012, modified by Sirius
  177. //------------------------------------------------------------------
  178. if (USART_GetFlagStatus(USART1, USART_FLAG_TC) != RESET) {
  179. if (pbuf->TxCtr > 0) { /* See if buffer is empty */
  180. pbuf->TxCtr--; /* No, decrement character count */
  181. if (pbuf->TxDeque >= UART1_TX_BUF_SIZE) { /* Avoid bounding offset */
  182. pbuf->TxDeque = 0;
  183. }
  184. USART_SendData(USART1, pbuf->BufTx[pbuf->TxDeque++]); /* Get character from buffer */
  185. } else {
  186. pbuf->TxDeque = pbuf ->TxEnque;
  187. }
  188. USART_ClearFlag(USART1, USART_FLAG_TC);
  189. }
  190. //------------------------------------------------------------------
  191. // for Rx not empty interrupt routine
  192. // 19:51pm, 11.Oct.2012, modified by Sirius
  193. //------------------------------------------------------------------
  194. if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET) {
  195. rdata = USART_ReceiveData(USART1);
  196. // USART2->DR = rdata; //发送到串口2
  197. #ifdef WDFJKEY_MAC_ADD
  198. switch (rstep) {
  199. case 0:
  200. if (rdata == 0xFE) {
  201. rstep = 1;
  202. pbuf->BufRx[pbuf->RxInPag][0] = rdata;
  203. // gtArm.ledtestcnt = 10;
  204. }
  205. break;
  206. case 1:
  207. if (rdata < (UART1_RX_BUF_SIZE-2)&&(rdata!=0)) {
  208. pbuf->RxLen = rdata;
  209. pbuf->BufRx[pbuf->RxInPag][1] = rdata; //Lenght
  210. pbuf->RxCnt = 2;
  211. pbuf->RxLen += 2;
  212. rstep = 2;
  213. } else {
  214. rstep = 0;
  215. }
  216. break;
  217. case 2:
  218. pbuf->BufRx[pbuf->RxInPag][pbuf->RxCnt] = rdata;
  219. pbuf->RxCnt ++;
  220. if (pbuf->RxCnt >= pbuf->RxLen) {
  221. pbuf->RxInPag ++;
  222. if (pbuf->RxInPag >= UART1_RX_PAGE) {
  223. pbuf->RxInPag = 0; /* Wrap in pointer if page buffer full */
  224. }
  225. rstep = 0;
  226. }
  227. break;
  228. default:
  229. rstep = 0;
  230. break;
  231. }
  232. #else
  233. switch (rstep) {
  234. case 0:
  235. if (rdata == '$') {
  236. rstep = 1;
  237. // gtArm.ledtestcnt = 10;
  238. }
  239. pbuf->RxCnt = 1;
  240. break;
  241. case 1:
  242. if (rdata == '#') {
  243. // gtArm.ledtestcnt = 30;
  244. pbuf->BufRx[pbuf->RxInPag][pbuf->RxCnt] = '#';
  245. pbuf->BufRx[pbuf->RxInPag][0] = pbuf->RxCnt; //Lenght
  246. pbuf->RxInPag++;
  247. if (pbuf->RxInPag >= UART1_RX_PAGE) {
  248. pbuf->RxInPag = 0; /* Wrap in pointer if page buffer full */
  249. }
  250. rstep = 0;
  251. } else if (rdata == '$') {
  252. pbuf->RxCnt = 1;
  253. } else {
  254. pbuf->BufRx[pbuf->RxInPag][pbuf->RxCnt] = rdata;
  255. pbuf->RxCnt++;
  256. }
  257. break;
  258. default:
  259. rstep = 0;
  260. break;
  261. }
  262. #endif
  263. // USART_ClearFlag(USART1, USART_FLAG_RXNE);
  264. // USART_SendData(USART2, rdata); //发送到串口2
  265. // USART_ClearFlag(USART2, USART_FLAG_ORE);
  266. }
  267. }
  268. /*----------------------------------------------------------------------
  269. // Function uses : UartISRHandler
  270. // Input parameter :
  271. // Output parameter :
  272. // Use Function :
  273. // Reserve date : 09:35, 2.Apr.2023, written by dwd
  274. ----------------------------------------------------------------------*/
  275. void Uart2ISRHandler(void)
  276. {
  277. T_COM_UART1 *pbuf;
  278. U08 rdata = 0;
  279. static U08 rstep = 0;
  280. pbuf = &s_tUart2Cfg;
  281. //------------------------------------------------------------------
  282. // for Tx compelete interrupt routine
  283. // 19:50pm, 11.Oct.2012, modified by Sirius
  284. //------------------------------------------------------------------
  285. if (USART_GetFlagStatus(USART2, USART_FLAG_TC) != RESET) {
  286. if (pbuf->TxCtr > 0) { /* See if buffer is empty */
  287. pbuf->TxCtr--; /* No, decrement character count */
  288. if (pbuf->TxDeque >= UART1_TX_BUF_SIZE) { /* Avoid bounding offset */
  289. pbuf->TxDeque = 0;
  290. }
  291. USART_SendData(USART2, pbuf->BufTx[pbuf->TxDeque++]); /* Get character from buffer */
  292. } else {
  293. pbuf->TxDeque = pbuf ->TxEnque;
  294. }
  295. USART_ClearFlag(USART2, USART_FLAG_TC);
  296. }
  297. //------------------------------------------------------------------
  298. // for Rx not empty interrupt routine
  299. // 19:51pm, 11.Oct.2012, modified by Sirius
  300. //------------------------------------------------------------------
  301. if (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) != RESET) {
  302. rdata = USART_ReceiveData(USART1);
  303. switch (rstep) {
  304. case 0:
  305. if (rdata == '$') {
  306. rstep = 1;
  307. // gtArm.ledtestcnt = 10;
  308. }
  309. pbuf->RxCnt = 1;
  310. break;
  311. case 1:
  312. if (rdata == HI_BYTE( gtArm.FJKeymac)) {
  313. rstep = 2 ;
  314. } else {
  315. rstep = 0;
  316. }
  317. break;
  318. case 2:
  319. if (rdata == LO_BYTE( gtArm.FJKeymac)) {
  320. rstep = 3;
  321. } else {
  322. rstep = 0;
  323. }
  324. break;
  325. case 3:
  326. if (rdata == '#') {
  327. // gtArm.ledtestcnt = 30;
  328. pbuf->BufRx[pbuf->RxInPag][pbuf->RxCnt] = '#';
  329. pbuf->BufRx[pbuf->RxInPag][0] = pbuf->RxCnt; //Lenght
  330. pbuf->RxInPag++;
  331. if (pbuf->RxInPag >= UART1_RX_PAGE) {
  332. pbuf->RxInPag = 0; /* Wrap in pointer if page buffer full */
  333. }
  334. rstep = 0;
  335. } else if (rdata == '$') {
  336. pbuf->RxCnt = 1;
  337. } else {
  338. pbuf->BufRx[pbuf->RxInPag][pbuf->RxCnt] = rdata;
  339. pbuf->RxCnt++;
  340. }
  341. break;
  342. default:
  343. rstep = 0;
  344. break;
  345. }
  346. USART_ClearFlag(USART2, USART_FLAG_RXNE);
  347. USART_SendData(USART1, rdata); //发送到串口1
  348. USART_ClearFlag(USART1, USART_FLAG_ORE);
  349. }
  350. }
  351. /*----------------------------------------------------------------------
  352. // Function name : Uart1CfgDisable
  353. // Input parameter :
  354. // Output parameter :
  355. // Use Function :
  356. // Reserve date : 18:24, 05.Seb.2019, written by dwd
  357. ----------------------------------------------------------------------*/
  358. void Uart1CfgDisable(void)
  359. {
  360. /* Enable the USART */
  361. USART_ITConfig(USART1,USART_IT_RXNE,DISABLE);
  362. USART_ITConfig(USART1,USART_IT_TC,DISABLE);
  363. USART_ClearFlag(USART1, USART_FLAG_TC);
  364. USART_Cmd(USART1, DISABLE);
  365. }
  366. void Uart2CfgDisable(void)
  367. {
  368. /* Enable the USART */
  369. USART_ITConfig(USART2,USART_IT_RXNE,DISABLE);
  370. USART_ITConfig(USART2,USART_IT_TC,DISABLE);
  371. USART_ClearFlag(USART2, USART_FLAG_TC);
  372. USART_Cmd(USART2, DISABLE);
  373. }
  374. /*------------------------------------------------------------------------------
  375. - Name: Uart1GetChar
  376. - DESCRIPTION: - To obtain a character from the communications channel. The function
  377. will wait for a character to be received on the serial channel
  378. or until the function times out.
  379. -
  380. - Input: 'to' is the amount of time (in clock ticks) that the calling function is
  381. willing to wait for a character to arrive. If you specify a timeout of 0,
  382. the function will wait forever for a character to arrive.
  383. - Output:
  384. -
  385. - modification history
  386. - --------------------
  387. - 18:24, 05.Seb.2019, written by dwd
  388. * --------------------
  389. -----------------------------------------------------------------------------*/
  390. void Uart1GetChar(U08 *rbuf ,U16 to, U08 *err)
  391. {
  392. U08 i,len;
  393. T_COM_UART1 *pbuf;
  394. pbuf = &s_tUart1Cfg; /* Obtain pointer to communications channel */
  395. if (!s_Uart1RxbufEmpty()) {
  396. *rbuf++ = pbuf->BufRx[pbuf->RxOutPag][0]; /* Copy data type */
  397. len = pbuf->BufRx[pbuf->RxOutPag][1];
  398. if (len > (UART1_RX_BUF_SIZE-2) ) {
  399. len = UART1_RX_BUF_SIZE -2;
  400. }
  401. *rbuf++ = len; /* Copy data length */
  402. len ++;
  403. for (i = 0; i < len; i++) { /* Copy data to buffer */
  404. *rbuf++ = pbuf->BufRx[pbuf->RxOutPag][i + 2];
  405. }
  406. pbuf->RxOutPag++;
  407. if (pbuf->RxOutPag >= UART1_RX_PAGE) { /* Ready for next page */
  408. pbuf->RxOutPag = 0;
  409. }
  410. *err = COM_NO_ERR;
  411. } else { /* See if characters received within timeout*/
  412. *err = COM_RX_EMPTY; /* No, return error code */
  413. }
  414. }
  415. void Uart2GetChar(U08 *rbuf ,U16 to, U08 *err)
  416. {
  417. U08 i,len;
  418. T_COM_UART1 *pbuf;
  419. pbuf = &s_tUart2Cfg; /* Obtain pointer to communications channel */
  420. if (!s_Uart2RxbufEmpty()) {
  421. //*rbuf++ = pbuf->BufRx[pbuf->RxOutPag][0]; /* Copy data type */
  422. len = pbuf->BufRx[pbuf->RxOutPag][0];
  423. if (len > (UART1_RX_BUF_SIZE-2) ) {
  424. len = UART1_RX_BUF_SIZE -2;
  425. }
  426. *rbuf++ = len; /* Copy data length */
  427. len ++;
  428. for (i = 0; i < len; i++) { /* Copy data to buffer */
  429. *rbuf++ = pbuf->BufRx[pbuf->RxOutPag][i + 1];
  430. }
  431. pbuf->RxOutPag++;
  432. if (pbuf->RxOutPag >= UART1_RX_PAGE) { /* Ready for next page */
  433. pbuf->RxOutPag = 0;
  434. }
  435. *err = COM_NO_ERR;
  436. } else { /* See if characters received within timeout*/
  437. *err = COM_RX_EMPTY; /* No, return error code */
  438. }
  439. }
  440. U08 Uart1PutChar(U08 *c, U08 len)
  441. {
  442. BOOL newTras;
  443. T_COM_UART1 *pbuf;
  444. pbuf = &s_tUart1Cfg; /* Obtain pointer to communications channel */
  445. if (len == 0) {
  446. return (COM_TX_EMPTY);
  447. }
  448. //if (len > s_Uart1TxbufLeft()) { /* Txbuffer left is not enough for this transmit */
  449. // return (COM_TX_FULL);
  450. // }
  451. newTras = s_Uart1RxbufEmpty();
  452. while (len--) {
  453. pbuf->TxCtr++; /* No, increment character count */
  454. if (pbuf->TxEnque >= UART1_TX_BUF_SIZE) { /* Avoid bounding offset */
  455. pbuf->TxEnque = 0;
  456. }
  457. pbuf->BufTx[pbuf->TxEnque++] = *c++; /* Put character into buffer */
  458. }
  459. /* Tx first character to wake up Usart new transmit process */
  460. if (newTras) {
  461. if (pbuf->TxCtr){
  462. pbuf->TxCtr--; /* No, decrement character count */
  463. }
  464. if (pbuf->TxDeque >= UART1_TX_BUF_SIZE) { /* Avoid bounding offset */
  465. pbuf->TxDeque = 0;
  466. }
  467. USART_SendData(USART1, pbuf->BufTx[pbuf->TxDeque++]); /* Get character from buffer */
  468. USART_ClearFlag(USART1, USART_FLAG_ORE); /* Clear TC flag in status register */
  469. }
  470. return (COM_NO_ERR);
  471. }
  472. U08 Uart2PutChar(U08 *c, U08 len)
  473. {
  474. BOOL newTras;
  475. T_COM_UART1 *pbuf;
  476. pbuf = &s_tUart2Cfg; /* Obtain pointer to communications channel */
  477. if (len == 0) {
  478. return (COM_TX_EMPTY);
  479. }
  480. //if (len > s_Uart1TxbufLeft()) { /* Txbuffer left is not enough for this transmit */
  481. // return (COM_TX_FULL);
  482. // }
  483. newTras = s_Uart2RxbufEmpty();
  484. while (len--) {
  485. pbuf->TxCtr++; /* No, increment character count */
  486. if (pbuf->TxEnque >= UART1_TX_BUF_SIZE) { /* Avoid bounding offset */
  487. pbuf->TxEnque = 0;
  488. }
  489. pbuf->BufTx[pbuf->TxEnque++] = *c++; /* Put character into buffer */
  490. }
  491. /* Tx first character to wake up Usart new transmit process */
  492. if (newTras) {
  493. if (pbuf->TxCtr){
  494. pbuf->TxCtr--; /* No, decrement character count */
  495. }
  496. if (pbuf->TxDeque >= UART1_TX_BUF_SIZE) { /* Avoid bounding offset */
  497. pbuf->TxDeque = 0;
  498. }
  499. USART_SendData(USART2, pbuf->BufTx[pbuf->TxDeque++]); /* Get character from buffer */
  500. USART_ClearFlag(USART2, USART_FLAG_ORE); /* Clear TC flag in status register */
  501. }
  502. return (COM_NO_ERR);
  503. }
  504. /*------------------------------------------------------------------------------
  505. - Name: Uart1VarInit
  506. - DESCRIPTION: - Initialize the ring buffer for COMM.
  507. Must call this function before calling any other functions.
  508. -
  509. - Input:
  510. - Output:
  511. -
  512. - modification history
  513. - --------------------
  514. - 18:24, 05.Seb.2019, written by dwd
  515. * --------------------
  516. -----------------------------------------------------------------------------*/
  517. static void s_Uart1VarInit(void)
  518. {
  519. T_COM_UART1 *pbuf;
  520. pbuf = &s_tUart1Cfg; /* Obtain pointer to communications channel */
  521. pbuf->RxInPag = 0; /* Initialize the ring buffer for COMM port */
  522. pbuf->RxOutPag = 0;
  523. pbuf->RxLen = 0;
  524. pbuf->RxCnt = 0;
  525. }
  526. static void s_Uart2VarInit(void)
  527. {
  528. T_COM_UART1 *pbuf;
  529. pbuf = &s_tUart2Cfg; /* Obtain pointer to communications channel */
  530. pbuf->RxInPag = 0; /* Initialize the ring buffer for COMM port */
  531. pbuf->RxOutPag = 0;
  532. pbuf->RxLen = 0;
  533. pbuf->RxCnt = 0;
  534. }
  535. /*=======================================================
  536. - Name: s_Uart1RxbufEmpty
  537. - DESCRIPTION: - Judge whether rxbuf data valid.
  538. -
  539. - Input:
  540. - Output:
  541. -
  542. - modification history
  543. - --------------------
  544. - 18:24, 05.Seb.2019, written by dwd
  545. * --------------------
  546. =======================================================*/
  547. static BOOL s_Uart1RxbufEmpty(void)
  548. {
  549. return (BOOL)(s_tUart1Cfg.RxInPag == s_tUart1Cfg.RxOutPag);
  550. }
  551. static BOOL s_Uart2RxbufEmpty(void)
  552. {
  553. return (BOOL)(s_tUart2Cfg.RxInPag == s_tUart2Cfg.RxOutPag);
  554. }
  555. /*------------------------------- E O F ----------------------------------------*/