uart.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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. uint32_t lsr;
  181. iir = readl(uap->regbase + UART485_IIR);
  182. switch (iir & UART485_IIR_IID_MASK) {
  183. case UART485_IIR_IID_THR_EMPTY:
  184. xUart485TxChars(uap, 1);
  185. break;
  186. case UART485_IIR_IID_REV_DATA_AVAIL:
  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. case UART485_IIR_IID_REV_LINE_STATUS:
  194. lsr = readl(uap->regbase + UART485_LSR);
  195. printf("Error lsr=0x%x\n", lsr);
  196. break;
  197. }
  198. }
  199. UartPort_t *xUartOpen(uint32_t id)
  200. {
  201. int irq;
  202. if (id >= UART_NUM) {
  203. TRACE_INFO("Wrong input uart id.\n");
  204. return NULL;
  205. }
  206. if (pxUartPort[id] != NULL) {
  207. if (id != UART_DEBUG_PORT)
  208. TRACE_ERROR("Uart %d is already in use.\n", id);
  209. return NULL;
  210. }
  211. UartPort_t *uap = (UartPort_t *)pvPortMalloc(sizeof(UartPort_t));
  212. if (uap == NULL) {
  213. if (id != UART_DEBUG_PORT)
  214. TRACE_ERROR("Out of memory for uart%d.\n", id);
  215. return NULL;
  216. }
  217. memset(uap, 0, sizeof(*uap));
  218. uap->rxbuf.buf = (char*)pvPortMalloc(UART_BUF_SIZE);
  219. if (uap->rxbuf.buf == NULL) {
  220. if (id != UART_DEBUG_PORT)
  221. TRACE_ERROR("Out of memory for uart%d.\n", id);
  222. goto err;
  223. }
  224. uap->txbuf.buf = (char*)pvPortMalloc(UART_BUF_SIZE);
  225. if (uap->txbuf.buf == NULL) {
  226. if (id != UART_DEBUG_PORT)
  227. TRACE_ERROR("Out of memory for uart%d.\n", id);
  228. goto err;
  229. }
  230. uap->xMutex = xSemaphoreCreateRecursiveMutex();
  231. configASSERT(uap->xMutex);
  232. uap->xRev = xSemaphoreCreateBinary();
  233. configASSERT(uap->xRev);
  234. uap->xSend = xSemaphoreCreateBinary();
  235. configASSERT(uap->xSend);
  236. uap->id = id;
  237. uap->regbase = ulUartBase[id];
  238. if (uap->id == UART_ID3)
  239. uap->fifosize = 64;
  240. else
  241. uap->fifosize = 128;
  242. irq = UART0_IRQn + id;
  243. request_irq(irq, 0, vUart485IntHandler, uap);
  244. return pxUartPort[id] = uap;
  245. err:
  246. if (uap->txbuf.buf)
  247. vPortFree(uap->txbuf.buf);
  248. if (uap->rxbuf.buf)
  249. vPortFree(uap->rxbuf.buf);
  250. vPortFree(uap);
  251. return NULL;
  252. };
  253. void vUartInit(UartPort_t *uap, uint32_t baud, uint32_t flags)
  254. {
  255. unsigned int lcr_h, quot, cr;
  256. int count = 0;
  257. /* soft reset */
  258. sys_soft_reset(softreset_uart0 + uap->id - UART_ID0);
  259. //set modem
  260. writel(0, uap->regbase + UART485_MCR);
  261. // baud = clk/(16*U_DLL)
  262. //set baud rate
  263. writel(readl(uap->regbase + UART485_LCR) | (1 << 7), uap->regbase + UART485_LCR);
  264. writel(UART_CLK(CLK_UART0 + uap->id - UART_ID0) / (16 * baud), uap->regbase + UART485_DLL);
  265. writel((UART_CLK(CLK_UART0 + uap->id - UART_ID0) / baud) & 0xf, uap->regbase + UART485_DLF);
  266. writel(0, uap->regbase + UART485_DLH);
  267. writel(readl(uap->regbase + UART485_LCR) & ~(1 << 7), uap->regbase + UART485_LCR);
  268. cr = 0;
  269. //校验
  270. if (flags & PARENB) {
  271. cr |= (1 << 3);
  272. if (!(flags & PARODD))
  273. cr |= (1 << 4);
  274. if (flags & CMSPAR)
  275. cr |= (1 << 5);
  276. }
  277. //停止位
  278. if (flags & CSTOPB)
  279. cr |= (1 << 2);
  280. else
  281. cr &= ~(1 << 2);
  282. //数据位
  283. cr &= ~(3 << 0);
  284. switch (flags & CSIZE) {
  285. case CS5:
  286. break;
  287. case CS6:
  288. cr |= 1;
  289. break;
  290. case CS7:
  291. cr |= 2;
  292. break;
  293. default: // CS8
  294. cr |= 3;
  295. break;
  296. }
  297. vUart485CheckLcr(uap, cr);
  298. //set fifo
  299. writel((2 << 6) | (3 << 4) | 7, uap->regbase + UART485_FCR);
  300. //enable rx and rls interrupt
  301. writel((1 << 2) | 1, uap->regbase + UART485_IER);
  302. //设置通信模式全双工还是半双工
  303. //485mode disable
  304. writel(0, uap->regbase + UART485_TCR);
  305. }
  306. void vUartClose(UartPort_t *uap)
  307. {
  308. if (uap == NULL) return;
  309. vSemaphoreDelete(uap->xSend);
  310. vSemaphoreDelete(uap->xRev);
  311. vSemaphoreDelete(uap->xMutex);
  312. vPortFree(uap->rxbuf.buf);
  313. uap->rxbuf.tail = uap->rxbuf.head = 0;
  314. vPortFree(uap->txbuf.buf);
  315. uap->rxbuf.tail = uap->rxbuf.head = 0;
  316. vPortFree(uap);
  317. pxUartPort[uap->id] = NULL;
  318. }
  319. int iUartWrite(UartPort_t *uap, uint8_t *buf, size_t len, TickType_t xBlockTime)
  320. {
  321. int c, ret = 0;
  322. TickType_t timeout = xBlockTime;
  323. TickType_t starttime = xTaskGetTickCount();
  324. TickType_t sptime;
  325. while (len) {
  326. xSemaphoreTakeRecursive(uap->xMutex, portMAX_DELAY);
  327. c = CIRC_SPACE_TO_END(uap->txbuf.head, uap->txbuf.tail, UART_BUF_SIZE);
  328. if (len < c)
  329. c = len;
  330. if (c <= 0) {
  331. xSemaphoreGiveRecursive(uap->xMutex);
  332. sptime = xTaskGetTickCount() - starttime;
  333. if (timeout > sptime) {
  334. xSemaphoreTake(uap->xSend, timeout - sptime);
  335. continue;
  336. } else {
  337. break;
  338. }
  339. }
  340. memcpy(uap->txbuf.buf + uap->txbuf.head, buf, c);
  341. uap->txbuf.head = (uap->txbuf.head + c) & (UART_BUF_SIZE - 1);
  342. buf += c;
  343. len -= c;
  344. ret += c;
  345. xSemaphoreGiveRecursive(uap->xMutex);
  346. }
  347. vUart485StartTx(uap);
  348. return ret;
  349. }
  350. int iUartReadNoBlock(UartPort_t *uap, uint8_t *buf, size_t len)
  351. {
  352. int c;
  353. xSemaphoreTakeRecursive(uap->xMutex, portMAX_DELAY);
  354. c = CIRC_CNT_TO_END(uap->rxbuf.head, uap->rxbuf.tail, UART_BUF_SIZE);
  355. if (len < c)
  356. c = len;
  357. if (c <= 0) {
  358. xSemaphoreGiveRecursive(uap->xMutex);
  359. }
  360. memcpy(buf, uap->rxbuf.buf + uap->rxbuf.tail, c);
  361. uap->rxbuf.tail = (uap->rxbuf.tail + c) & (UART_BUF_SIZE - 1);
  362. xSemaphoreGiveRecursive(uap->xMutex);
  363. return c;
  364. }
  365. int iUartRead(UartPort_t *uap, uint8_t *buf, size_t len, TickType_t xBlockTime)
  366. {
  367. int c, ret = 0;
  368. TickType_t timeout = xBlockTime;
  369. TickType_t starttime = xTaskGetTickCount();
  370. TickType_t sptime;
  371. while (len) {
  372. xSemaphoreTakeRecursive(uap->xMutex, portMAX_DELAY);
  373. c = CIRC_CNT_TO_END(uap->rxbuf.head, uap->rxbuf.tail, UART_BUF_SIZE);
  374. if (len < c)
  375. c = len;
  376. if (c <= 0) {
  377. xSemaphoreGiveRecursive(uap->xMutex);
  378. sptime = xTaskGetTickCount() - starttime;
  379. if (timeout > sptime) {
  380. xSemaphoreTake(uap->xRev, timeout - sptime);
  381. continue;
  382. } else {
  383. break;
  384. }
  385. }
  386. memcpy(buf, uap->rxbuf.buf + uap->rxbuf.tail, c);
  387. uap->rxbuf.tail = (uap->rxbuf.tail + c) & (UART_BUF_SIZE - 1);
  388. buf += c;
  389. len -= c;
  390. ret += c;
  391. xSemaphoreGiveRecursive(uap->xMutex);
  392. }
  393. return ret;
  394. }
  395. void vDebugConsoleInitialise(void)
  396. {
  397. UartPort_t *uap = xUartOpen(UART_DEBUG_PORT);
  398. if (uap != NULL) {
  399. vUartInit(uap, 115200, 0);
  400. //vUartInit(uap, 62500, 0);
  401. }
  402. }
  403. /* retarget printf function */
  404. int32_t putchar(int32_t ch)
  405. {
  406. //while (!(readl(ulUartBase[UART_DEBUG_PORT] + UART485_USR) & (1<<1)));
  407. while(!((readl(ulUartBase[UART_DEBUG_PORT] + UART485_LSR)&(1<<6))?1:0));
  408. writel(ch, ulUartBase[UART_DEBUG_PORT] + UART485_THR);
  409. return ch;
  410. }
  411. xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
  412. {
  413. return pxUartPort[UART_DEBUG_PORT];
  414. }
  415. signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
  416. {
  417. if (iUartWrite(pxPort, (uint8_t *)&cOutChar, 1, xBlockTime) == 1)
  418. return pdPASS;
  419. return pdFAIL;
  420. }
  421. void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
  422. {
  423. signed char *pxNext;
  424. /* NOTE: This implementation does not handle the queue being full as no
  425. block time is used! */
  426. /* The port handle is not required as this driver only supports UART0. */
  427. ( void ) pxPort;
  428. ( void ) usStringLength;
  429. /* Send each character in the string, one at a time. */
  430. pxNext = ( signed char * ) pcString;
  431. while( *pxNext )
  432. {
  433. xSerialPutChar( pxPort, *pxNext, UART_NO_BLOCK );
  434. pxNext++;
  435. }
  436. }
  437. signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
  438. {
  439. if (iUartRead(pxPort, (uint8_t *)pcRxedChar, 1, xBlockTime) == 1)
  440. return pdPASS;
  441. return pdFAIL;
  442. }
  443. #define UUP_PACKET_SIZE 128
  444. #define UUP_MAX_FRAME_LEN (UUP_PACKET_SIZE + 16)
  445. #define UUP_RX_FRAME_NUM 16
  446. static unsigned char uup_rx_buf[UUP_RX_FRAME_NUM][UUP_MAX_FRAME_LEN];
  447. static unsigned char *uup_rx_ptr;
  448. static int uup_rx_rev_len = 0;
  449. static int uup_rx_head = 0;
  450. static int uup_rx_tail = 0;
  451. static int uup_rx_state = 0;
  452. static int uup_rx_data_len = 0;
  453. static void uart_tx_demo_thread(void *param)
  454. {
  455. UartPort_t *uap = param;
  456. uint8_t uarttx[32] = "hello, i am amt630h\n";
  457. //printf("### uart_tx_demo_thread\n");
  458. for (;;) {
  459. iUartWrite(uap, uarttx, strlen((char*)uarttx), pdMS_TO_TICKS(100));
  460. vTaskDelay(1000);
  461. }
  462. }
  463. static void uart_rx_demo_thread(void *param)
  464. {
  465. UartPort_t *uap = xUartOpen(UART_MCU_PORT);
  466. uint8_t uartrx[16];
  467. int len;
  468. int i;
  469. if (!uap) {
  470. printf("open uart %d fail.\n", UART_MCU_PORT);
  471. vTaskDelete(NULL);
  472. return;
  473. }
  474. vUartInit(uap, 115200, 0);
  475. if (xTaskCreate(uart_tx_demo_thread, "uartsend", configMINIMAL_STACK_SIZE, uap,
  476. configMAX_PRIORITIES / 3, NULL) != pdPASS) {
  477. printf("create uart tx demo task fail.\n");
  478. vTaskDelete(NULL);
  479. return;
  480. }
  481. for (;;) {
  482. len = iUartRead(uap, uartrx, 16, pdMS_TO_TICKS(100));
  483. for (i = 0; i < len; i++) {
  484. switch (uup_rx_state) {
  485. case 0:
  486. if (uartrx[i] == 0x55) {
  487. uup_rx_state++;
  488. uup_rx_rev_len = 0;
  489. uup_rx_ptr = &uup_rx_buf[uup_rx_head][0];
  490. }
  491. break;
  492. case 1:
  493. if (uartrx[i] == 0x81)
  494. uup_rx_state++;
  495. else
  496. uup_rx_state = 0;
  497. *uup_rx_ptr++ = uartrx[i];
  498. break;
  499. case 2:
  500. if (uartrx[i] == 0xc6)
  501. uup_rx_state++;
  502. else
  503. uup_rx_state = 0;
  504. *uup_rx_ptr++ = uartrx[i];
  505. break;
  506. case 3:
  507. uup_rx_data_len = uartrx[i];
  508. if((uup_rx_data_len <= 0) || (uup_rx_data_len > UUP_PACKET_SIZE)) {
  509. printf("Invalid uup_rx_data_len %d\n", uup_rx_data_len);
  510. uup_rx_state = 0;
  511. } else {
  512. uup_rx_state++;
  513. *uup_rx_ptr++ = uartrx[i];
  514. }
  515. break;
  516. case 4:
  517. *uup_rx_ptr++ = uartrx[i];
  518. if (++uup_rx_rev_len == uup_rx_data_len)
  519. uup_rx_state++;
  520. break;
  521. case 5:
  522. *uup_rx_ptr++ = uartrx[i];
  523. uup_rx_head = (uup_rx_head + 1) % UUP_RX_FRAME_NUM;
  524. uup_rx_state = 0;
  525. break;
  526. }
  527. }
  528. if (uup_rx_tail != uup_rx_head) {
  529. unsigned char *buf;
  530. unsigned char checksum = 0;
  531. buf = &uup_rx_buf[uup_rx_tail][0];
  532. len = buf[2];
  533. for (i = 0; i < len + 3; i++)
  534. checksum ^= buf[i];
  535. if (checksum == buf[len + 3]) {
  536. if (buf[3] == 0) {
  537. SysInfo *sysinfo = GetSysInfo();
  538. printf("receive uart update cmd, updating...\n");
  539. #if 0
  540. sysinfo->update_media_type = UPDATE_MEDIA_UART;
  541. sysinfo->update_status = UPDATE_STATUS_START;
  542. #endif
  543. SaveSysInfo();
  544. wdt_cpu_reboot();
  545. }
  546. } else {
  547. printf("rev frame checksum err.\n");
  548. }
  549. uup_rx_tail = (uup_rx_tail + 1) % UUP_RX_FRAME_NUM;
  550. }
  551. }
  552. }
  553. int uart_rx_demo(void)
  554. {
  555. /* Create a task to process uart rx data */
  556. if (xTaskCreate(uart_rx_demo_thread, "uartdemo", configMINIMAL_STACK_SIZE, NULL,
  557. configMAX_PRIORITIES / 3, NULL) != pdPASS) {
  558. printf("create uart rx demo task fail.\n");
  559. return -1;
  560. }
  561. return 0;
  562. }