| |
| |
| |
| |
| |
| |
|
|
| #include "dma_uart.h" |
| #include "FreeRTOS.h" |
| #include "task.h" |
| #include "semphr.h" |
| #include "dump_hex.h" |
| #include "itu.h" |
| #include <stdio.h> |
| #include <string.h> |
|
|
| #define DMAUART_RxInterrupt 0x01 |
| #define DMAUART_TxInterrupt 0x02 |
| #define DMAUART_BufInterrupt 0x04 |
| #define DMAUART_Overflow 0x08 |
| #define DMAUART_CtsDetect 0x10 |
| #define DMAUART_RxValid 0x20 |
| #define DMAUART_TxReady 0x40 |
| #define DMAUART_RxNeedAddr 0x80 |
|
|
| #define DMAUART_RxIRQ_EN 0x81 |
| #define DMAUART_TxIRQ_EN 0x82 |
| #define DMAUART_BufReq_EN 0x84 |
| #define DMAUART_RxIRQ_DIS 0x01 |
| #define DMAUART_TxIRQ_DIS 0x02 |
| #define DMAUART_BufReq_DIS 0x04 |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| void DmaUART::EnableIRQ(bool enable) |
| { |
| printf("Uart IRQ %sable!\n", enable ? "en" : "dis"); |
| if (enable) { |
| uart->ictrl = ( DMAUART_RxIRQ_EN | DMAUART_BufReq_EN ); |
| ioWrite8(ITU_IRQ_HIGH_EN, ioRead8(ITU_IRQ_HIGH_EN) | (1 << my_irq)); |
| } else { |
| uart->ictrl = ( DMAUART_RxIRQ_DIS | DMAUART_BufReq_DIS ); |
| } |
| } |
|
|
| void DmaUART::EnableLoopback(bool enable) |
| { |
| if(enable) { |
| uart->flowctrl |= DMAUART_LOOPBACK; |
| } else { |
| uart->flowctrl &= ~DMAUART_LOOPBACK; |
| } |
| } |
|
|
| void DmaUART::FlowControl(bool enable) |
| { |
| if(enable) { |
| uart->flowctrl |= DMAUART_HWFLOWCTRL; |
| } else { |
| uart->flowctrl &= ~DMAUART_HWFLOWCTRL; |
| } |
| } |
|
|
| void DmaUART::EnableSlip(bool enable) |
| { |
| slipMode = enable; |
| if(enable) { |
| uart->flowctrl |= DMAUART_SLIPENABLE; |
| } else { |
| uart->flowctrl &= ~DMAUART_SLIPENABLE; |
| } |
| } |
|
|
| void DmaUART::ModuleCtrl(uint8_t mode) |
| { |
| printf("Setting module control to (%b) %d ", uart->flowctrl, mode); |
| uart->flowctrl = (uart->flowctrl & 0x8F) | (mode << 4); |
| printf("%b\n", uart->flowctrl); |
| } |
|
|
| void DmaUART::ClearRxBuffer(void) |
| { |
| |
| uart->flowctrl = uart->flowctrl | DMAUART_RESET; |
| xQueueGenericReset(rx_bufs, pdFALSE); |
| uart->flowctrl = uart->flowctrl | DMAUART_RESET; |
| cmd_buffer_reset(packets); |
| } |
|
|
| int DmaUART::Read(uint8_t *buffer, int bufsize) |
| { |
| |
| command_buf_t *buf; |
| if (ReceivePacket(&buf, 0) == pdTRUE) { |
| |
| int max = buf->size > bufsize ? bufsize : buf->size; |
| memcpy(buffer, buf->data, max); |
| FreeBuffer(buf); |
| return max; |
| } |
| return 0; |
| } |
|
|
| void DmaUART::SetBaudRate(int bps) |
| { |
| int twice = (CLOCK_FREQ * 2) / bps; |
| uint16_t rate = ((twice + 1) >> 1) - 1; |
| uint32_t actual = (CLOCK_FREQ * 2) / (2 * (rate + 1)); |
| printf("SetBaudRate to %d bps => %d => actual = %d\n", bps, rate, actual); |
| uart->rate_h = rate >> 8; |
| uart->rate_l = rate & 0xFF; |
| } |
|
|
| void hex(uint8_t h); |
|
|
| uint8_t DmaUART::DmaUartInterrupt(void *context) |
| { |
| DmaUART *u = (DmaUART *) context; |
| uint8_t uart_intr_status = 0; |
|
|
| BaseType_t HPTaskAwoken = pdFALSE; |
| |
|
|
| while (1) { |
| |
| |
| uart_intr_status = u->uart->status & (DMAUART_RxInterrupt | DMAUART_TxInterrupt | DMAUART_BufInterrupt); |
| |
| |
| if (uart_intr_status == 0) { |
| break; |
| } |
|
|
| |
| if (uart_intr_status & DMAUART_BufInterrupt) { |
| command_buf_t *rxb = NULL; |
| |
| if (cmd_buffer_get_free_isr(u->packets, &rxb, &HPTaskAwoken) == pdTRUE) { |
| |
| xQueueSendFromISR(u->rx_bufs, &rxb, &HPTaskAwoken); |
| |
| |
| u->uart->rx_addr = rxb->data; |
| u->uart->ictrl = DMAUART_RxIRQ_EN; |
| } else { |
| ioWrite8(UART_DATA, '^'); |
| u->uart->ictrl = DMAUART_BufReq_DIS; |
| } |
| } |
|
|
| if (uart_intr_status & DMAUART_TxInterrupt) { |
| |
|
|
| if (u->current_tx_buf) { |
| cmd_buffer_free_isr(u->packets, u->current_tx_buf, &HPTaskAwoken); |
| u->uart->ictrl = DMAUART_BufReq_EN; |
| u->current_tx_buf = NULL; |
| } else if (cmd_buffer_get_tx_isr(u->packets, &(u->current_tx_buf), &HPTaskAwoken) == pdTRUE) { |
| u->uart->tx_addr = u->current_tx_buf->data; |
| u->uart->length = (uint16_t)u->current_tx_buf->size; |
| u->uart->tx_push = 1; |
| |
| |
| |
| |
| |
| |
| |
| } else { |
| u->uart->ictrl = DMAUART_TxIRQ_DIS; |
| } |
| } |
| |
| |
| if (uart_intr_status & DMAUART_RxInterrupt) { |
| command_buf_t *rxb = NULL; |
|
|
| if (xQueueReceiveFromISR(u->rx_bufs, &rxb, &HPTaskAwoken) == pdTRUE) { |
| |
| rxb->size = u->uart->length; |
|
|
| if (u->isr_rx_callback(u->packets, rxb, &HPTaskAwoken) != pdTRUE) { |
| |
| cmd_buffer_free_isr(u->packets, rxb, &HPTaskAwoken); |
| u->uart->ictrl = DMAUART_BufReq_EN; |
| } |
| } else { |
| ioWrite8(UART_DATA, '!'); |
| } |
| u->uart->rx_pop = 1; |
| } |
| } |
| |
| return HPTaskAwoken; |
| } |
|
|
| void DmaUART :: SetReceiveCallback(isr_receive_callback_t cb) |
| { |
| isr_rx_callback = cb; |
| } |
|
|
| void DmaUART :: ResetReceiveCallback(void) |
| { |
| isr_rx_callback = cmd_buffer_received_isr; |
| } |
|
|
|
|
| |
| BaseType_t DmaUART :: SendSlipPacket(const uint8_t *data, int len) |
| { |
| if (len > CMD_BUF_SIZE) { |
| return pdFALSE; |
| } |
| command_buf_t *buf; |
| BaseType_t success = GetBuffer(&buf, 100); |
| if (success == pdTRUE) { |
| memcpy(buf->data, data, len); |
| buf->size = len; |
| success = TransmitPacket(buf); |
| } |
| return success; |
| } |
|
|
| |
| BaseType_t DmaUART :: TransmitPacket(command_buf_t *buf, uint16_t *ms) |
| { |
| if (cmd_buffer_transmit(packets, buf) == pdTRUE) { |
| if (txDebug) { |
| printf("Transmit packet %d: (%d bytes)\n", buf->bufnr, buf->size); |
| dump_hex_relative(buf->data, (buf->size < 64) ? buf->size : 64); |
| } |
| |
| if (ms) { |
| *ms = getMsTimer(); |
| } |
| uart->ictrl = DMAUART_TxIRQ_EN; |
| return pdTRUE; |
| } |
|
|
| printf("Transmit packet failed; transmit queue full\n"); |
| cmd_buffer_free(packets, buf); |
| return pdFALSE; |
| } |
|
|
| BaseType_t DmaUART :: ReceivePacket(command_buf_t **buf, TickType_t ticks) |
| { |
| return cmd_buffer_received(packets, buf, ticks); |
| } |
|
|
| BaseType_t DmaUART :: FreeBuffer(command_buf_t *buf) |
| { |
| BaseType_t ret = cmd_buffer_free(packets, buf); |
|
|
| |
| uart->ictrl = DMAUART_BufReq_EN; |
|
|
| return ret; |
| } |
|
|
| BaseType_t DmaUART :: GetBuffer(command_buf_t **buf, TickType_t ticks) |
| { |
| BaseType_t ret = cmd_buffer_get(packets, buf, ticks); |
| |
| if (ret == pdTRUE) { |
| (*buf)->size = 0; |
| (*buf)->dropped = 0; |
| } |
| return ret; |
| } |
|
|
| |
| |
| |
| |
|
|