uart.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. #include <stdio.h>
  2. #include <stddef.h>
  3. #include <string.h>
  4. #include "FreeRTOS.h"
  5. #include "chip.h"
  6. #include "board.h"
  7. #include "serial.h"
  8. #include "sysinfo.h"
  9. #define UART485_RBR_THR_DLL 0x000
  10. #define UART485_DLH_IER 0x004
  11. #define UART485_IIR_FCR 0x008
  12. #define UART485_RBR 0x000
  13. #define UART485_THR 0x000
  14. #define UART485_DLL 0x000
  15. #define UART485_DLH 0x004
  16. #define UART485_IER 0x004
  17. #define UART485_IIR 0x008
  18. #define UART485_FCR 0x008
  19. #define UART485_LCR 0x00C
  20. #define UART485_MCR 0x010
  21. #define UART485_LSR 0x014
  22. #define UART485_MSR 0x018
  23. #define UART485_SCR 0x01C
  24. #define UART485_LPDLL 0x020
  25. #define UART485_LPDLH 0x024
  26. #define UART485_RESERVED 0x028
  27. #define UART485_SRBR 0x030
  28. #define UART485_STHR 0x06C
  29. #define UART485_FAR 0x070
  30. #define UART485_TFR 0x074
  31. #define UART485_RFW 0x078
  32. #define UART485_USR 0x07C
  33. #define UART485_TFL 0x080
  34. #define UART485_RFL 0x084
  35. #define UART485_SRR 0x088
  36. #define UART485_SRTS 0x08C
  37. #define UART485_SBCR 0x090
  38. #define UART485_SDMAM 0x094
  39. #define UART485_SFE 0x098
  40. #define UART485_SRT 0x09C
  41. #define UART485_STET 0x0A0
  42. #define UART485_HTX 0x0A4
  43. #define UART485_DMASA 0x0A8
  44. #define UART485_TCR 0x0AC
  45. #define UART485_DE_EN 0x0B0
  46. #define UART485_RE_EN 0x0B4
  47. #define UART485_DET 0x0B8
  48. #define UART485_TAT 0x0BC
  49. #define UART485_DLF 0x0C0
  50. #define UART485_RAR 0x0C4
  51. #define UART485_TAR 0x0C8
  52. #define UART485_LCR_EXT 0x0CC
  53. #define UART485_CPR 0x0F4
  54. #define UART485_UCV 0x0F8
  55. #define UART485_CTR 0x0FC
  56. #define UART485_FCR_ENABLE_FIFO (1 << 0) /* Enable the FIFO */
  57. #define UART485_FCR_CLEAR_RCVR (1 << 1) /* Clear the RCVR FIFO */
  58. #define UART485_FCR_CLEAR_XMIT (1 << 2) /* Clear the XMIT FIFO */
  59. #define UART485_LCR_SPAR (1 << 5)
  60. #define UART485_LSR_RFE (1 << 7)
  61. #define UART485_LSR_TEMT (1 << 6)
  62. #define UART485_LSR_THRE (1 << 5)
  63. #define UART485_LSR_BI (1 << 4)
  64. #define UART485_LSR_FE (1 << 3)
  65. #define UART485_LSR_PE (1 << 2)
  66. #define UART485_LSR_OE (1 << 1)
  67. #define UART485_LSR_DR (1 << 0)
  68. #define UART485_IIR_IID_MASK (0xf << 0)
  69. #define UART485_IIR_IID_MODEM_STATUS 0x0
  70. #define UART485_IIR_IID_NO_INT_PENDING 0x1
  71. #define UART485_IIR_IID_THR_EMPTY 0x2
  72. #define UART485_IIR_IID_REV_DATA_AVAIL 0x4
  73. #define UART485_IIR_IID_REV_LINE_STATUS 0x6
  74. #define UART485_IIR_IID_BUSY_DETECT 0x7
  75. #define UART485_IIR_IID_CHAR_TIMEOUT 0xc
  76. #define CSIZE 0x0003
  77. #define CS8 0x0000
  78. #define CS6 0x0001
  79. #define CS7 0x0002
  80. #define CS5 0x0003
  81. #define CSTOPB 0x0004
  82. #define PARENB 0x0010
  83. #define PARODD 0x0020
  84. #define CMSPAR 0x0100 /* mark or space (stick) parity */
  85. #define CRTSCTS 0x0200 /* flow control */
  86. //#define UART_CLK(x) 24000000
  87. #define UART_CLK(x) ulClkGetRate(x)
  88. #define UART_BUF_SIZE 4096
  89. #define UART_ISR_PASS_LIMIT 16
  90. #define UART_BLOCK_TIMEOUT pdMS_TO_TICKS(100)
  91. #define UART_NO_BLOCK ( ( TickType_t ) 0 )
  92. static uint32_t ulUartBase[UART_NUM] = {REGS_UART0_BASE, REGS_UART1_BASE, REGS_UART2_BASE, REGS_UART3_BASE};
  93. static UartPort_t *pxUartPort[UART_NUM] = {NULL};
  94. #define uart_circ_empty(circ) ((circ)->head == (circ)->tail)
  95. #define uart_circ_clear(circ) ((circ)->head = (circ)->tail = 0)
  96. #define uart_circ_chars_pending(circ) \
  97. (CIRC_CNT((circ)->head, (circ)->tail, UART_XMIT_SIZE))
  98. #define uart_circ_chars_free(circ) \
  99. (CIRC_SPACE((circ)->head, (circ)->tail, UART_XMIT_SIZE))
  100. static void vUart485ClearFifos(UartPort_t *uap)
  101. {
  102. writel(UART485_FCR_ENABLE_FIFO, uap->regbase + UART485_FCR);
  103. writel(UART485_FCR_ENABLE_FIFO | UART485_FCR_CLEAR_RCVR
  104. | UART485_FCR_CLEAR_XMIT, uap->regbase + UART485_FCR);
  105. writel(0, uap->regbase + UART485_FCR);
  106. }
  107. static void vUart485ForceIdle(UartPort_t *uap)
  108. {
  109. vUart485ClearFifos(uap);
  110. //set fifo
  111. writel((2 << 6) | (3 << 4) | 7, uap->regbase + UART485_FCR);
  112. }
  113. static void vUart485CheckLcr(UartPort_t *uap, unsigned int value)
  114. {
  115. int tries = 1000;
  116. /* Make sure LCR write wasn't ignored */
  117. while (tries--) {
  118. unsigned int lcr = readl(uap->regbase + UART485_LCR);
  119. if ((value & ~UART485_LCR_SPAR) == (lcr & ~UART485_LCR_SPAR))
  120. return;
  121. vUart485ForceIdle(uap);
  122. writel(value, uap->regbase + UART485_LCR);
  123. }
  124. }
  125. static void vUart485StopTx(UartPort_t *uap)
  126. {
  127. writel(readl(uap->regbase + UART485_IER) & ~(1 << 1), uap->regbase + UART485_IER);
  128. }
  129. static void vUart485StartTx(UartPort_t *uap)
  130. {
  131. writel(readl(uap->regbase + UART485_IER) | (1 << 1), uap->regbase + UART485_IER);
  132. }
  133. static int iUart485RxChars(UartPort_t *uap)
  134. {
  135. uint32_t lsr;
  136. unsigned int ch, max_count = 256;
  137. int fifotaken = 0;
  138. while (max_count--) {
  139. lsr = readl(uap->regbase + UART485_LSR);
  140. if (!(lsr & UART485_LSR_DR))
  141. break;
  142. /* Take chars from the FIFO and update status */
  143. ch = readl(uap->regbase + UART485_RBR);
  144. fifotaken++;
  145. /* discard when rx buf is full */
  146. if (CIRC_SPACE_TO_END(uap->rxbuf.head, uap->rxbuf.tail, UART_BUF_SIZE) > 0) {
  147. *(uap->rxbuf.buf + uap->rxbuf.head) = ch;
  148. uap->rxbuf.head = (uap->rxbuf.head + 1) & (UART_BUF_SIZE - 1);
  149. } else {
  150. //printf("uart%d rx buf is full, discard a data.\n", uap->id);
  151. }
  152. }
  153. if (fifotaken > 0)
  154. xSemaphoreGiveFromISR(uap->xRev, NULL);
  155. return fifotaken;
  156. }
  157. /* Returns true if tx interrupts have to be (kept) enabled */
  158. static BaseType_t xUart485TxChars(UartPort_t *uap, int from_irq)
  159. {
  160. struct circ_buf *xmit = &uap->txbuf;
  161. int count = uap->fifosize >> 1;
  162. if (uart_circ_empty(xmit)) {
  163. vUart485StopTx(uap);
  164. return pdFALSE;
  165. }
  166. do {
  167. if (from_irq && count-- == 0)
  168. break;
  169. writel(xmit->buf[xmit->tail], uap->regbase + UART485_THR);
  170. xmit->tail = (xmit->tail + 1) & (UART_BUF_SIZE - 1);
  171. } while (!uart_circ_empty(xmit));
  172. if (from_irq)
  173. xSemaphoreGiveFromISR(uap->xSend, NULL);
  174. return pdTRUE;
  175. }
  176. static void vUart485IntHandler(void *param)
  177. {
  178. UartPort_t *uap = param;
  179. uint32_t iir;
  180. iir = readl(uap->regbase + UART485_IIR);
  181. switch (iir & UART485_IIR_IID_MASK) {
  182. case UART485_IIR_IID_THR_EMPTY:
  183. xUart485TxChars(uap, 1);
  184. break;
  185. case UART485_IIR_IID_REV_DATA_AVAIL:
  186. case UART485_IIR_IID_REV_LINE_STATUS:
  187. case UART485_IIR_IID_CHAR_TIMEOUT:
  188. iUart485RxChars(uap);
  189. break;
  190. case UART485_IIR_IID_BUSY_DETECT:
  191. iir = readl(uap->regbase + UART485_USR); //clear busy interrupt
  192. break;
  193. }
  194. }
  195. UartPort_t *xUartOpen(uint32_t id)
  196. {
  197. int irq;
  198. if (id >= UART_NUM) {
  199. TRACE_INFO("Wrong input uart id.\n");
  200. return NULL;
  201. }
  202. if (pxUartPort[id] != NULL) {
  203. if (id != UART_DEBUG_PORT)
  204. TRACE_ERROR("Uart %d is already in use.\n", id);
  205. return NULL;
  206. }
  207. UartPort_t *uap = (UartPort_t *)pvPortMalloc(sizeof(UartPort_t));
  208. if (uap == NULL) {
  209. if (id != UART_DEBUG_PORT)
  210. TRACE_ERROR("Out of memory for uart%d.\n", id);
  211. return NULL;
  212. }
  213. memset(uap, 0, sizeof(*uap));
  214. uap->rxbuf.buf = (char*)pvPortMalloc(UART_BUF_SIZE);
  215. if (uap->rxbuf.buf == NULL) {
  216. if (id != UART_DEBUG_PORT)
  217. TRACE_ERROR("Out of memory for uart%d.\n", id);
  218. goto err;
  219. }
  220. uap->txbuf.buf = (char*)pvPortMalloc(UART_BUF_SIZE);
  221. if (uap->txbuf.buf == NULL) {
  222. if (id != UART_DEBUG_PORT)
  223. TRACE_ERROR("Out of memory for uart%d.\n", id);
  224. goto err;
  225. }
  226. uap->xMutex = xSemaphoreCreateRecursiveMutex();
  227. configASSERT(uap->xMutex);
  228. uap->xRev = xSemaphoreCreateBinary();
  229. configASSERT(uap->xRev);
  230. uap->xSend = xSemaphoreCreateBinary();
  231. configASSERT(uap->xSend);
  232. uap->id = id;
  233. uap->regbase = ulUartBase[id];
  234. if (uap->id == UART_ID3)
  235. uap->fifosize = 64;
  236. else
  237. uap->fifosize = 128;
  238. irq = UART0_IRQn + id;
  239. request_irq(irq, 0, vUart485IntHandler, uap);
  240. return pxUartPort[id] = uap;
  241. err:
  242. if (uap->txbuf.buf)
  243. vPortFree(uap->txbuf.buf);
  244. if (uap->rxbuf.buf)
  245. vPortFree(uap->rxbuf.buf);
  246. vPortFree(uap);
  247. return NULL;
  248. };
  249. void vUartInit(UartPort_t *uap, uint32_t baud, uint32_t flags)
  250. {
  251. unsigned int lcr_h, quot, cr;
  252. int count = 0;
  253. /* soft reset */
  254. sys_soft_reset(softreset_uart0 + uap->id - UART_ID0);
  255. //set modem
  256. writel(0, uap->regbase + UART485_MCR);
  257. // baud = clk/(16*U_DLL)
  258. //set baud rate
  259. writel(readl(uap->regbase + UART485_LCR) | (1 << 7), uap->regbase + UART485_LCR);
  260. writel(UART_CLK(CLK_UART0 + uap->id - UART_ID0) / (16 * baud), uap->regbase + UART485_DLL);
  261. writel((UART_CLK(CLK_UART0 + uap->id - UART_ID0) / baud) & 0xf, uap->regbase + UART485_DLF);
  262. writel(0, uap->regbase + UART485_DLH);
  263. writel(readl(uap->regbase + UART485_LCR) & ~(1 << 7), uap->regbase + UART485_LCR);
  264. cr = 0;
  265. //校验
  266. if (flags & PARENB) {
  267. cr |= (1 << 3);
  268. if (!(flags & PARODD))
  269. cr |= (1 << 4);
  270. if (flags & CMSPAR)
  271. cr |= (1 << 5);
  272. }
  273. //停止位
  274. if (flags & CSTOPB)
  275. cr |= (1 << 2);
  276. else
  277. cr &= ~(1 << 2);
  278. //数据位
  279. cr &= ~(3 << 0);
  280. switch (flags & CSIZE) {
  281. case CS5:
  282. break;
  283. case CS6:
  284. cr |= 1;
  285. break;
  286. case CS7:
  287. cr |= 2;
  288. break;
  289. default: // CS8
  290. cr |= 3;
  291. break;
  292. }
  293. vUart485CheckLcr(uap, cr);
  294. //set fifo
  295. writel((2 << 6) | (3 << 4) | 7, uap->regbase + UART485_FCR);
  296. //enable rx and err interrupt
  297. writel((1 << 4) | 1, uap->regbase + UART485_IER);
  298. //设置通信模式全双工还是半双工
  299. //485mode disable
  300. writel(0, uap->regbase + UART485_TCR);
  301. }
  302. void vUartClose(UartPort_t *uap)
  303. {
  304. if (uap == NULL) return;
  305. vSemaphoreDelete(uap->xSend);
  306. vSemaphoreDelete(uap->xRev);
  307. vSemaphoreDelete(uap->xMutex);
  308. vPortFree(uap->rxbuf.buf);
  309. uap->rxbuf.tail = uap->rxbuf.head = 0;
  310. vPortFree(uap->txbuf.buf);
  311. uap->rxbuf.tail = uap->rxbuf.head = 0;
  312. vPortFree(uap);
  313. pxUartPort[uap->id] = NULL;
  314. }
  315. int iUartWrite(UartPort_t *uap, uint8_t *buf, size_t len, TickType_t xBlockTime)
  316. {
  317. int c, ret = 0;
  318. TickType_t timeout = xBlockTime;
  319. TickType_t starttime = xTaskGetTickCount();
  320. TickType_t sptime;
  321. while (len) {
  322. xSemaphoreTakeRecursive(uap->xMutex, portMAX_DELAY);
  323. c = CIRC_SPACE_TO_END(uap->txbuf.head, uap->txbuf.tail, UART_BUF_SIZE);
  324. if (len < c)
  325. c = len;
  326. if (c <= 0) {
  327. xSemaphoreGiveRecursive(uap->xMutex);
  328. sptime = xTaskGetTickCount() - starttime;
  329. if (timeout > sptime) {
  330. xSemaphoreTake(uap->xSend, timeout - sptime);
  331. continue;
  332. } else {
  333. break;
  334. }
  335. }
  336. memcpy(uap->txbuf.buf + uap->txbuf.head, buf, c);
  337. uap->txbuf.head = (uap->txbuf.head + c) & (UART_BUF_SIZE - 1);
  338. buf += c;
  339. len -= c;
  340. ret += c;
  341. xSemaphoreGiveRecursive(uap->xMutex);
  342. }
  343. vUart485StartTx(uap);
  344. return ret;
  345. }
  346. int iUartReadNoBlock(UartPort_t *uap, uint8_t *buf, size_t len)
  347. {
  348. int c;
  349. xSemaphoreTakeRecursive(uap->xMutex, portMAX_DELAY);
  350. c = CIRC_CNT_TO_END(uap->rxbuf.head, uap->rxbuf.tail, UART_BUF_SIZE);
  351. if (len < c)
  352. c = len;
  353. if (c <= 0) {
  354. xSemaphoreGiveRecursive(uap->xMutex);
  355. }
  356. memcpy(buf, uap->rxbuf.buf + uap->rxbuf.tail, c);
  357. uap->rxbuf.tail = (uap->rxbuf.tail + c) & (UART_BUF_SIZE - 1);
  358. xSemaphoreGiveRecursive(uap->xMutex);
  359. return c;
  360. }
  361. int iUartRead(UartPort_t *uap, uint8_t *buf, size_t len, TickType_t xBlockTime)
  362. {
  363. int c, ret = 0;
  364. TickType_t timeout = xBlockTime;
  365. TickType_t starttime = xTaskGetTickCount();
  366. TickType_t sptime;
  367. while (len) {
  368. xSemaphoreTakeRecursive(uap->xMutex, portMAX_DELAY);
  369. c = CIRC_CNT_TO_END(uap->rxbuf.head, uap->rxbuf.tail, UART_BUF_SIZE);
  370. if (len < c)
  371. c = len;
  372. if (c <= 0) {
  373. xSemaphoreGiveRecursive(uap->xMutex);
  374. sptime = xTaskGetTickCount() - starttime;
  375. if (timeout > sptime) {
  376. xSemaphoreTake(uap->xRev, timeout - sptime);
  377. continue;
  378. } else {
  379. break;
  380. }
  381. }
  382. memcpy(buf, uap->rxbuf.buf + uap->rxbuf.tail, c);
  383. uap->rxbuf.tail = (uap->rxbuf.tail + c) & (UART_BUF_SIZE - 1);
  384. buf += c;
  385. len -= c;
  386. ret += c;
  387. xSemaphoreGiveRecursive(uap->xMutex);
  388. }
  389. return ret;
  390. }
  391. void vDebugConsoleInitialise(void)
  392. {
  393. UartPort_t *uap = xUartOpen(UART_DEBUG_PORT);
  394. if (uap != NULL) {
  395. vUartInit(uap, 115200, 0);
  396. //vUartInit(uap, 62500, 0);
  397. }
  398. }
  399. /* retarget printf function */
  400. int32_t putchar(int32_t ch)
  401. {
  402. //while (!(readl(ulUartBase[UART_DEBUG_PORT] + UART485_USR) & (1<<1)));
  403. while(!((readl(ulUartBase[UART_DEBUG_PORT] + UART485_LSR)&(1<<6))?1:0));
  404. writel(ch, ulUartBase[UART_DEBUG_PORT] + UART485_THR);
  405. return ch;
  406. }
  407. xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
  408. {
  409. return pxUartPort[UART_DEBUG_PORT];
  410. }
  411. signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
  412. {
  413. if (iUartWrite(pxPort, (uint8_t *)&cOutChar, 1, xBlockTime) == 1)
  414. return pdPASS;
  415. return pdFAIL;
  416. }
  417. void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
  418. {
  419. signed char *pxNext;
  420. /* NOTE: This implementation does not handle the queue being full as no
  421. block time is used! */
  422. /* The port handle is not required as this driver only supports UART0. */
  423. ( void ) pxPort;
  424. ( void ) usStringLength;
  425. /* Send each character in the string, one at a time. */
  426. pxNext = ( signed char * ) pcString;
  427. while( *pxNext )
  428. {
  429. xSerialPutChar( pxPort, *pxNext, UART_NO_BLOCK );
  430. pxNext++;
  431. }
  432. }
  433. signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
  434. {
  435. if (iUartRead(pxPort, (uint8_t *)pcRxedChar, 1, xBlockTime) == 1)
  436. return pdPASS;
  437. return pdFAIL;
  438. }
  439. #define UUP_PACKET_SIZE 128
  440. #define UUP_MAX_FRAME_LEN (UUP_PACKET_SIZE + 16)
  441. #define UUP_RX_FRAME_NUM 16
  442. static unsigned char uup_rx_buf[UUP_RX_FRAME_NUM][UUP_MAX_FRAME_LEN];
  443. static unsigned char *uup_rx_ptr;
  444. static int uup_rx_rev_len = 0;
  445. static int uup_rx_head = 0;
  446. static int uup_rx_tail = 0;
  447. static int uup_rx_state = 0;
  448. static int uup_rx_data_len = 0;
  449. static void uart_tx_demo_thread(void *param)
  450. {
  451. UartPort_t *uap = param;
  452. uint8_t uarttx[32] = "hello, i am amt630h\n";
  453. //printf("### uart_tx_demo_thread\n");
  454. for (;;) {
  455. iUartWrite(uap, uarttx, strlen((char*)uarttx), pdMS_TO_TICKS(100));
  456. vTaskDelay(1000);
  457. }
  458. }
  459. static void uart_rx_demo_thread(void *param)
  460. {
  461. UartPort_t *uap = xUartOpen(UART_MCU_PORT);
  462. uint8_t uartrx[16];
  463. int len;
  464. int i;
  465. if (!uap) {
  466. printf("open uart %d fail.\n", UART_MCU_PORT);
  467. vTaskDelete(NULL);
  468. return;
  469. }
  470. vUartInit(uap, 115200, 0);
  471. if (xTaskCreate(uart_tx_demo_thread, "uartsend", configMINIMAL_STACK_SIZE, uap,
  472. configMAX_PRIORITIES / 3, NULL) != pdPASS) {
  473. printf("create uart tx demo task fail.\n");
  474. vTaskDelete(NULL);
  475. return;
  476. }
  477. for (;;) {
  478. len = iUartRead(uap, uartrx, 16, pdMS_TO_TICKS(100));
  479. for (i = 0; i < len; i++) {
  480. switch (uup_rx_state) {
  481. case 0:
  482. if (uartrx[i] == 0x55) {
  483. uup_rx_state++;
  484. uup_rx_rev_len = 0;
  485. uup_rx_ptr = &uup_rx_buf[uup_rx_head][0];
  486. }
  487. break;
  488. case 1:
  489. if (uartrx[i] == 0x81)
  490. uup_rx_state++;
  491. else
  492. uup_rx_state = 0;
  493. *uup_rx_ptr++ = uartrx[i];
  494. break;
  495. case 2:
  496. if (uartrx[i] == 0xc6)
  497. uup_rx_state++;
  498. else
  499. uup_rx_state = 0;
  500. *uup_rx_ptr++ = uartrx[i];
  501. break;
  502. case 3:
  503. uup_rx_data_len = uartrx[i];
  504. if((uup_rx_data_len <= 0) || (uup_rx_data_len > UUP_PACKET_SIZE)) {
  505. printf("Invalid uup_rx_data_len %d\n", uup_rx_data_len);
  506. uup_rx_state = 0;
  507. } else {
  508. uup_rx_state++;
  509. *uup_rx_ptr++ = uartrx[i];
  510. }
  511. break;
  512. case 4:
  513. *uup_rx_ptr++ = uartrx[i];
  514. if (++uup_rx_rev_len == uup_rx_data_len)
  515. uup_rx_state++;
  516. break;
  517. case 5:
  518. *uup_rx_ptr++ = uartrx[i];
  519. uup_rx_head = (uup_rx_head + 1) % UUP_RX_FRAME_NUM;
  520. uup_rx_state = 0;
  521. break;
  522. }
  523. }
  524. if (uup_rx_tail != uup_rx_head) {
  525. unsigned char *buf;
  526. unsigned char checksum = 0;
  527. buf = &uup_rx_buf[uup_rx_tail][0];
  528. len = buf[2];
  529. for (i = 0; i < len + 3; i++)
  530. checksum ^= buf[i];
  531. if (checksum == buf[len + 3]) {
  532. if (buf[3] == 0) {
  533. SysInfo *sysinfo = GetSysInfo();
  534. printf("receive uart update cmd, updating...\n");
  535. #if 0
  536. sysinfo->update_media_type = UPDATE_MEDIA_UART;
  537. sysinfo->update_status = UPDATE_STATUS_START;
  538. #endif
  539. SaveSysInfo();
  540. wdt_cpu_reboot();
  541. }
  542. } else {
  543. printf("rev frame checksum err.\n");
  544. }
  545. uup_rx_tail = (uup_rx_tail + 1) % UUP_RX_FRAME_NUM;
  546. }
  547. }
  548. }
  549. int uart_rx_demo(void)
  550. {
  551. /* Create a task to process uart rx data */
  552. if (xTaskCreate(uart_rx_demo_thread, "uartdemo", configMINIMAL_STACK_SIZE, NULL,
  553. configMAX_PRIORITIES / 3, NULL) != pdPASS) {
  554. printf("create uart rx demo task fail.\n");
  555. return -1;
  556. }
  557. return 0;
  558. }