| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664 |
- #include <stdio.h>
- #include <stddef.h>
- #include <string.h>
- #include "FreeRTOS.h"
- #include "chip.h"
- #include "board.h"
- #include "serial.h"
- #include "sysinfo.h"
- #define UART485_RBR_THR_DLL 0x000
- #define UART485_DLH_IER 0x004
- #define UART485_IIR_FCR 0x008
- #define UART485_RBR 0x000
- #define UART485_THR 0x000
- #define UART485_DLL 0x000
- #define UART485_DLH 0x004
- #define UART485_IER 0x004
- #define UART485_IIR 0x008
- #define UART485_FCR 0x008
- #define UART485_LCR 0x00C
- #define UART485_MCR 0x010
- #define UART485_LSR 0x014
- #define UART485_MSR 0x018
- #define UART485_SCR 0x01C
- #define UART485_LPDLL 0x020
- #define UART485_LPDLH 0x024
- #define UART485_RESERVED 0x028
- #define UART485_SRBR 0x030
- #define UART485_STHR 0x06C
- #define UART485_FAR 0x070
- #define UART485_TFR 0x074
- #define UART485_RFW 0x078
- #define UART485_USR 0x07C
- #define UART485_TFL 0x080
- #define UART485_RFL 0x084
- #define UART485_SRR 0x088
- #define UART485_SRTS 0x08C
- #define UART485_SBCR 0x090
- #define UART485_SDMAM 0x094
- #define UART485_SFE 0x098
- #define UART485_SRT 0x09C
- #define UART485_STET 0x0A0
- #define UART485_HTX 0x0A4
- #define UART485_DMASA 0x0A8
- #define UART485_TCR 0x0AC
- #define UART485_DE_EN 0x0B0
- #define UART485_RE_EN 0x0B4
- #define UART485_DET 0x0B8
- #define UART485_TAT 0x0BC
- #define UART485_DLF 0x0C0
- #define UART485_RAR 0x0C4
- #define UART485_TAR 0x0C8
- #define UART485_LCR_EXT 0x0CC
- #define UART485_CPR 0x0F4
- #define UART485_UCV 0x0F8
- #define UART485_CTR 0x0FC
- #define UART485_FCR_ENABLE_FIFO (1 << 0) /* Enable the FIFO */
- #define UART485_FCR_CLEAR_RCVR (1 << 1) /* Clear the RCVR FIFO */
- #define UART485_FCR_CLEAR_XMIT (1 << 2) /* Clear the XMIT FIFO */
- #define UART485_LCR_SPAR (1 << 5)
- #define UART485_LSR_RFE (1 << 7)
- #define UART485_LSR_TEMT (1 << 6)
- #define UART485_LSR_THRE (1 << 5)
- #define UART485_LSR_BI (1 << 4)
- #define UART485_LSR_FE (1 << 3)
- #define UART485_LSR_PE (1 << 2)
- #define UART485_LSR_OE (1 << 1)
- #define UART485_LSR_DR (1 << 0)
- #define UART485_IIR_IID_MASK (0xf << 0)
- #define UART485_IIR_IID_MODEM_STATUS 0x0
- #define UART485_IIR_IID_NO_INT_PENDING 0x1
- #define UART485_IIR_IID_THR_EMPTY 0x2
- #define UART485_IIR_IID_REV_DATA_AVAIL 0x4
- #define UART485_IIR_IID_REV_LINE_STATUS 0x6
- #define UART485_IIR_IID_BUSY_DETECT 0x7
- #define UART485_IIR_IID_CHAR_TIMEOUT 0xc
- #define CSIZE 0x0003
- #define CS8 0x0000
- #define CS6 0x0001
- #define CS7 0x0002
- #define CS5 0x0003
- #define CSTOPB 0x0004
- #define PARENB 0x0010
- #define PARODD 0x0020
- #define CMSPAR 0x0100 /* mark or space (stick) parity */
- #define CRTSCTS 0x0200 /* flow control */
- //#define UART_CLK(x) 24000000
- #define UART_CLK(x) ulClkGetRate(x)
- #define UART_BUF_SIZE 4096
- #define UART_ISR_PASS_LIMIT 16
- #define UART_BLOCK_TIMEOUT pdMS_TO_TICKS(100)
- #define UART_NO_BLOCK ( ( TickType_t ) 0 )
- static uint32_t ulUartBase[UART_NUM] = {REGS_UART0_BASE, REGS_UART1_BASE, REGS_UART2_BASE, REGS_UART3_BASE};
- static UartPort_t *pxUartPort[UART_NUM] = {NULL};
- #define uart_circ_empty(circ) ((circ)->head == (circ)->tail)
- #define uart_circ_clear(circ) ((circ)->head = (circ)->tail = 0)
- #define uart_circ_chars_pending(circ) \
- (CIRC_CNT((circ)->head, (circ)->tail, UART_XMIT_SIZE))
- #define uart_circ_chars_free(circ) \
- (CIRC_SPACE((circ)->head, (circ)->tail, UART_XMIT_SIZE))
- static void vUart485ClearFifos(UartPort_t *uap)
- {
- writel(UART485_FCR_ENABLE_FIFO, uap->regbase + UART485_FCR);
- writel(UART485_FCR_ENABLE_FIFO | UART485_FCR_CLEAR_RCVR
- | UART485_FCR_CLEAR_XMIT, uap->regbase + UART485_FCR);
- writel(0, uap->regbase + UART485_FCR);
- }
- static void vUart485ForceIdle(UartPort_t *uap)
- {
- vUart485ClearFifos(uap);
- //set fifo
- writel((2 << 6) | (3 << 4) | 7, uap->regbase + UART485_FCR);
- }
- static void vUart485CheckLcr(UartPort_t *uap, unsigned int value)
- {
- int tries = 1000;
- /* Make sure LCR write wasn't ignored */
- while (tries--) {
- unsigned int lcr = readl(uap->regbase + UART485_LCR);
- if ((value & ~UART485_LCR_SPAR) == (lcr & ~UART485_LCR_SPAR))
- return;
- vUart485ForceIdle(uap);
- writel(value, uap->regbase + UART485_LCR);
- }
- }
- static void vUart485StopTx(UartPort_t *uap)
- {
- writel(readl(uap->regbase + UART485_IER) & ~(1 << 1), uap->regbase + UART485_IER);
- }
- static void vUart485StartTx(UartPort_t *uap)
- {
- writel(readl(uap->regbase + UART485_IER) | (1 << 1), uap->regbase + UART485_IER);
- }
- static int iUart485RxChars(UartPort_t *uap)
- {
- uint32_t lsr;
- unsigned int ch, max_count = 256;
- int fifotaken = 0;
- while (max_count--) {
- lsr = readl(uap->regbase + UART485_LSR);
- if (!(lsr & UART485_LSR_DR))
- break;
- /* Take chars from the FIFO and update status */
- ch = readl(uap->regbase + UART485_RBR);
- fifotaken++;
- /* discard when rx buf is full */
- if (CIRC_SPACE_TO_END(uap->rxbuf.head, uap->rxbuf.tail, UART_BUF_SIZE) > 0) {
- *(uap->rxbuf.buf + uap->rxbuf.head) = ch;
- uap->rxbuf.head = (uap->rxbuf.head + 1) & (UART_BUF_SIZE - 1);
- } else {
- //printf("uart%d rx buf is full, discard a data.\n", uap->id);
- }
- }
- if (fifotaken > 0)
- xSemaphoreGiveFromISR(uap->xRev, NULL);
- return fifotaken;
- }
- /* Returns true if tx interrupts have to be (kept) enabled */
- static BaseType_t xUart485TxChars(UartPort_t *uap, int from_irq)
- {
- struct circ_buf *xmit = &uap->txbuf;
- int count = uap->fifosize >> 1;
- if (uart_circ_empty(xmit)) {
- vUart485StopTx(uap);
- return pdFALSE;
- }
- do {
- if (from_irq && count-- == 0)
- break;
- writel(xmit->buf[xmit->tail], uap->regbase + UART485_THR);
- xmit->tail = (xmit->tail + 1) & (UART_BUF_SIZE - 1);
- } while (!uart_circ_empty(xmit));
- if (from_irq)
- xSemaphoreGiveFromISR(uap->xSend, NULL);
- return pdTRUE;
- }
- static void vUart485IntHandler(void *param)
- {
- UartPort_t *uap = param;
- uint32_t iir;
- uint32_t lsr;
- iir = readl(uap->regbase + UART485_IIR);
- switch (iir & UART485_IIR_IID_MASK) {
- case UART485_IIR_IID_THR_EMPTY:
- xUart485TxChars(uap, 1);
- break;
- case UART485_IIR_IID_REV_DATA_AVAIL:
- case UART485_IIR_IID_CHAR_TIMEOUT:
- iUart485RxChars(uap);
- break;
- case UART485_IIR_IID_BUSY_DETECT:
- iir = readl(uap->regbase + UART485_USR); //clear busy interrupt
- break;
- case UART485_IIR_IID_REV_LINE_STATUS:
- lsr = readl(uap->regbase + UART485_LSR);
- printf("Error lsr=0x%x\n", lsr);
- break;
- }
- }
- UartPort_t *xUartOpen(uint32_t id)
- {
- int irq;
- if (id >= UART_NUM) {
- TRACE_INFO("Wrong input uart id.\n");
- return NULL;
- }
- if (pxUartPort[id] != NULL) {
- if (id != UART_DEBUG_PORT)
- TRACE_ERROR("Uart %d is already in use.\n", id);
- return NULL;
- }
- UartPort_t *uap = (UartPort_t *)pvPortMalloc(sizeof(UartPort_t));
- if (uap == NULL) {
- if (id != UART_DEBUG_PORT)
- TRACE_ERROR("Out of memory for uart%d.\n", id);
- return NULL;
- }
- memset(uap, 0, sizeof(*uap));
- uap->rxbuf.buf = (char*)pvPortMalloc(UART_BUF_SIZE);
- if (uap->rxbuf.buf == NULL) {
- if (id != UART_DEBUG_PORT)
- TRACE_ERROR("Out of memory for uart%d.\n", id);
- goto err;
- }
- uap->txbuf.buf = (char*)pvPortMalloc(UART_BUF_SIZE);
- if (uap->txbuf.buf == NULL) {
- if (id != UART_DEBUG_PORT)
- TRACE_ERROR("Out of memory for uart%d.\n", id);
- goto err;
- }
- uap->xMutex = xSemaphoreCreateRecursiveMutex();
- configASSERT(uap->xMutex);
- uap->xRev = xSemaphoreCreateBinary();
- configASSERT(uap->xRev);
- uap->xSend = xSemaphoreCreateBinary();
- configASSERT(uap->xSend);
- uap->id = id;
- uap->regbase = ulUartBase[id];
- if (uap->id == UART_ID3)
- uap->fifosize = 64;
- else
- uap->fifosize = 128;
- irq = UART0_IRQn + id;
- request_irq(irq, 0, vUart485IntHandler, uap);
- return pxUartPort[id] = uap;
- err:
- if (uap->txbuf.buf)
- vPortFree(uap->txbuf.buf);
- if (uap->rxbuf.buf)
- vPortFree(uap->rxbuf.buf);
- vPortFree(uap);
- return NULL;
- };
- void vUartInit(UartPort_t *uap, uint32_t baud, uint32_t flags)
- {
- unsigned int lcr_h, quot, cr;
- int count = 0;
- /* soft reset */
- sys_soft_reset(softreset_uart0 + uap->id - UART_ID0);
- //set modem
- writel(0, uap->regbase + UART485_MCR);
- // baud = clk/(16*U_DLL)
- //set baud rate
- writel(readl(uap->regbase + UART485_LCR) | (1 << 7), uap->regbase + UART485_LCR);
- writel(UART_CLK(CLK_UART0 + uap->id - UART_ID0) / (16 * baud), uap->regbase + UART485_DLL);
- writel((UART_CLK(CLK_UART0 + uap->id - UART_ID0) / baud) & 0xf, uap->regbase + UART485_DLF);
- writel(0, uap->regbase + UART485_DLH);
- writel(readl(uap->regbase + UART485_LCR) & ~(1 << 7), uap->regbase + UART485_LCR);
- cr = 0;
- //校验
- if (flags & PARENB) {
- cr |= (1 << 3);
- if (!(flags & PARODD))
- cr |= (1 << 4);
- if (flags & CMSPAR)
- cr |= (1 << 5);
- }
- //停止位
- if (flags & CSTOPB)
- cr |= (1 << 2);
- else
- cr &= ~(1 << 2);
- //数据位
- cr &= ~(3 << 0);
- switch (flags & CSIZE) {
- case CS5:
- break;
- case CS6:
- cr |= 1;
- break;
- case CS7:
- cr |= 2;
- break;
- default: // CS8
- cr |= 3;
- break;
- }
- vUart485CheckLcr(uap, cr);
- //set fifo
- writel((2 << 6) | (3 << 4) | 7, uap->regbase + UART485_FCR);
- //enable rx and rls interrupt
- writel((1 << 2) | 1, uap->regbase + UART485_IER);
- //设置通信模式全双工还是半双工
- //485mode disable
- writel(0, uap->regbase + UART485_TCR);
- }
- void vUartClose(UartPort_t *uap)
- {
- if (uap == NULL) return;
- vSemaphoreDelete(uap->xSend);
- vSemaphoreDelete(uap->xRev);
- vSemaphoreDelete(uap->xMutex);
- vPortFree(uap->rxbuf.buf);
- uap->rxbuf.tail = uap->rxbuf.head = 0;
- vPortFree(uap->txbuf.buf);
- uap->rxbuf.tail = uap->rxbuf.head = 0;
- vPortFree(uap);
- pxUartPort[uap->id] = NULL;
- }
- int iUartWrite(UartPort_t *uap, uint8_t *buf, size_t len, TickType_t xBlockTime)
- {
- int c, ret = 0;
- TickType_t timeout = xBlockTime;
- TickType_t starttime = xTaskGetTickCount();
- TickType_t sptime;
- while (len) {
- xSemaphoreTakeRecursive(uap->xMutex, portMAX_DELAY);
- c = CIRC_SPACE_TO_END(uap->txbuf.head, uap->txbuf.tail, UART_BUF_SIZE);
- if (len < c)
- c = len;
- if (c <= 0) {
- xSemaphoreGiveRecursive(uap->xMutex);
- sptime = xTaskGetTickCount() - starttime;
- if (timeout > sptime) {
- xSemaphoreTake(uap->xSend, timeout - sptime);
- continue;
- } else {
- break;
- }
- }
- memcpy(uap->txbuf.buf + uap->txbuf.head, buf, c);
- uap->txbuf.head = (uap->txbuf.head + c) & (UART_BUF_SIZE - 1);
- buf += c;
- len -= c;
- ret += c;
- xSemaphoreGiveRecursive(uap->xMutex);
- }
- vUart485StartTx(uap);
- return ret;
- }
- int iUartReadNoBlock(UartPort_t *uap, uint8_t *buf, size_t len)
- {
- int c;
- xSemaphoreTakeRecursive(uap->xMutex, portMAX_DELAY);
- c = CIRC_CNT_TO_END(uap->rxbuf.head, uap->rxbuf.tail, UART_BUF_SIZE);
- if (len < c)
- c = len;
- if (c <= 0) {
- xSemaphoreGiveRecursive(uap->xMutex);
- }
- memcpy(buf, uap->rxbuf.buf + uap->rxbuf.tail, c);
- uap->rxbuf.tail = (uap->rxbuf.tail + c) & (UART_BUF_SIZE - 1);
- xSemaphoreGiveRecursive(uap->xMutex);
- return c;
- }
- int iUartRead(UartPort_t *uap, uint8_t *buf, size_t len, TickType_t xBlockTime)
- {
- int c, ret = 0;
- TickType_t timeout = xBlockTime;
- TickType_t starttime = xTaskGetTickCount();
- TickType_t sptime;
- while (len) {
- xSemaphoreTakeRecursive(uap->xMutex, portMAX_DELAY);
- c = CIRC_CNT_TO_END(uap->rxbuf.head, uap->rxbuf.tail, UART_BUF_SIZE);
- if (len < c)
- c = len;
- if (c <= 0) {
- xSemaphoreGiveRecursive(uap->xMutex);
- sptime = xTaskGetTickCount() - starttime;
- if (timeout > sptime) {
- xSemaphoreTake(uap->xRev, timeout - sptime);
- continue;
- } else {
- break;
- }
- }
- memcpy(buf, uap->rxbuf.buf + uap->rxbuf.tail, c);
- uap->rxbuf.tail = (uap->rxbuf.tail + c) & (UART_BUF_SIZE - 1);
- buf += c;
- len -= c;
- ret += c;
- xSemaphoreGiveRecursive(uap->xMutex);
- }
- return ret;
- }
- void vDebugConsoleInitialise(void)
- {
- UartPort_t *uap = xUartOpen(UART_DEBUG_PORT);
- if (uap != NULL) {
- vUartInit(uap, 115200, 0);
- //vUartInit(uap, 62500, 0);
- }
- }
- /* retarget printf function */
- int32_t putchar(int32_t ch)
- {
- //while (!(readl(ulUartBase[UART_DEBUG_PORT] + UART485_USR) & (1<<1)));
- while(!((readl(ulUartBase[UART_DEBUG_PORT] + UART485_LSR)&(1<<6))?1:0));
- writel(ch, ulUartBase[UART_DEBUG_PORT] + UART485_THR);
- return ch;
- }
- xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
- {
- return pxUartPort[UART_DEBUG_PORT];
- }
- signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
- {
- if (iUartWrite(pxPort, (uint8_t *)&cOutChar, 1, xBlockTime) == 1)
- return pdPASS;
- return pdFAIL;
- }
- void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
- {
- signed char *pxNext;
- /* NOTE: This implementation does not handle the queue being full as no
- block time is used! */
- /* The port handle is not required as this driver only supports UART0. */
- ( void ) pxPort;
- ( void ) usStringLength;
- /* Send each character in the string, one at a time. */
- pxNext = ( signed char * ) pcString;
- while( *pxNext )
- {
- xSerialPutChar( pxPort, *pxNext, UART_NO_BLOCK );
- pxNext++;
- }
- }
- signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
- {
- if (iUartRead(pxPort, (uint8_t *)pcRxedChar, 1, xBlockTime) == 1)
- return pdPASS;
- return pdFAIL;
- }
- #define UUP_PACKET_SIZE 128
- #define UUP_MAX_FRAME_LEN (UUP_PACKET_SIZE + 16)
- #define UUP_RX_FRAME_NUM 16
- static unsigned char uup_rx_buf[UUP_RX_FRAME_NUM][UUP_MAX_FRAME_LEN];
- static unsigned char *uup_rx_ptr;
- static int uup_rx_rev_len = 0;
- static int uup_rx_head = 0;
- static int uup_rx_tail = 0;
- static int uup_rx_state = 0;
- static int uup_rx_data_len = 0;
- static void uart_tx_demo_thread(void *param)
- {
- UartPort_t *uap = param;
- uint8_t uarttx[32] = "hello, i am amt630h\n";
- //printf("### uart_tx_demo_thread\n");
- for (;;) {
- iUartWrite(uap, uarttx, strlen((char*)uarttx), pdMS_TO_TICKS(100));
- vTaskDelay(1000);
- }
- }
- static void uart_rx_demo_thread(void *param)
- {
- UartPort_t *uap = xUartOpen(UART_MCU_PORT);
- uint8_t uartrx[16];
- int len;
- int i;
- if (!uap) {
- printf("open uart %d fail.\n", UART_MCU_PORT);
- vTaskDelete(NULL);
- return;
- }
- vUartInit(uap, 115200, 0);
- if (xTaskCreate(uart_tx_demo_thread, "uartsend", configMINIMAL_STACK_SIZE, uap,
- configMAX_PRIORITIES / 3, NULL) != pdPASS) {
- printf("create uart tx demo task fail.\n");
- vTaskDelete(NULL);
- return;
- }
- for (;;) {
- len = iUartRead(uap, uartrx, 16, pdMS_TO_TICKS(100));
- for (i = 0; i < len; i++) {
- switch (uup_rx_state) {
- case 0:
- if (uartrx[i] == 0x55) {
- uup_rx_state++;
- uup_rx_rev_len = 0;
- uup_rx_ptr = &uup_rx_buf[uup_rx_head][0];
- }
- break;
- case 1:
- if (uartrx[i] == 0x81)
- uup_rx_state++;
- else
- uup_rx_state = 0;
- *uup_rx_ptr++ = uartrx[i];
- break;
- case 2:
- if (uartrx[i] == 0xc6)
- uup_rx_state++;
- else
- uup_rx_state = 0;
- *uup_rx_ptr++ = uartrx[i];
- break;
- case 3:
- uup_rx_data_len = uartrx[i];
- if((uup_rx_data_len <= 0) || (uup_rx_data_len > UUP_PACKET_SIZE)) {
- printf("Invalid uup_rx_data_len %d\n", uup_rx_data_len);
- uup_rx_state = 0;
- } else {
- uup_rx_state++;
- *uup_rx_ptr++ = uartrx[i];
- }
- break;
- case 4:
- *uup_rx_ptr++ = uartrx[i];
- if (++uup_rx_rev_len == uup_rx_data_len)
- uup_rx_state++;
- break;
- case 5:
- *uup_rx_ptr++ = uartrx[i];
- uup_rx_head = (uup_rx_head + 1) % UUP_RX_FRAME_NUM;
- uup_rx_state = 0;
- break;
- }
- }
- if (uup_rx_tail != uup_rx_head) {
- unsigned char *buf;
- unsigned char checksum = 0;
- buf = &uup_rx_buf[uup_rx_tail][0];
- len = buf[2];
- for (i = 0; i < len + 3; i++)
- checksum ^= buf[i];
- if (checksum == buf[len + 3]) {
- if (buf[3] == 0) {
- SysInfo *sysinfo = GetSysInfo();
- printf("receive uart update cmd, updating...\n");
- #if 0
- sysinfo->update_media_type = UPDATE_MEDIA_UART;
- sysinfo->update_status = UPDATE_STATUS_START;
- #endif
- SaveSysInfo();
- wdt_cpu_reboot();
- }
- } else {
- printf("rev frame checksum err.\n");
- }
- uup_rx_tail = (uup_rx_tail + 1) % UUP_RX_FRAME_NUM;
- }
- }
- }
- int uart_rx_demo(void)
- {
- /* Create a task to process uart rx data */
- if (xTaskCreate(uart_rx_demo_thread, "uartdemo", configMINIMAL_STACK_SIZE, NULL,
- configMAX_PRIORITIES / 3, NULL) != pdPASS) {
- printf("create uart rx demo task fail.\n");
- return -1;
- }
- return 0;
- }
|