/* * serial.c * * Created on: 07.06.2019 * Author: harry */ #include #include "usart.h" #include "fifo.h" #include "serial.h" static uart_rx_buf_t EditBuffer; static uint8_t tx_buffer[TX_BUFFER_SIZE]; static uart_rx_buf_t *TargetBuffer; static volatile uint8_t uart_busy; static volatile uint8_t line_complete; static fifo_t tx_fifo; static uint8_t buffer_p; static uint8_t rxbuf; static UART_HandleTypeDef *hcomport; /******************************************************************** * Uart API-Fuctions ********************************************************************/ void serial_init(UART_HandleTypeDef *huart, uart_rx_buf_t *LineBuffer) { hcomport = huart; fifo_init(&tx_fifo, (uint8_t *) &tx_buffer, TX_BUFFER_SIZE); line_complete = FALSE; EditBuffer[0] = 0; buffer_p = 0; TargetBuffer = LineBuffer; HAL_UART_Receive_IT(hcomport, &rxbuf, 1); } uint8_t uart_isLineAvailable(void) { if (line_complete) { line_complete = FALSE; return TRUE; } else return FALSE; } void uart_putc(char c) { static uint8_t ci; while (!fifo_put(&tx_fifo, c)) ; if (uart_busy == FALSE) { uart_busy = TRUE; fifo_get(&tx_fifo, &ci); HAL_UART_Transmit_IT(hcomport, (uint8_t *) &ci, 1); } } void uart_puts(char *s) { for (uint8_t i = 0; i < strlen(s); i++) uart_putc(s[i]); } /******************************************************************** * Uart Callback * called, when a line is completed by typing and the line is * copied to the TargetBuffer ********************************************************************/ __weak void uart_LineReceivedCallback(uart_rx_buf_t *line) { UNUSED (line); } /******************************************************************** * Uart Iterrupt-Handler ********************************************************************/ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if (huart == hcomport) { if (rxbuf >= ' ') { if (buffer_p < (UART_RX_BUFFER_SIZE-1)) { EditBuffer[buffer_p++] = rxbuf; EditBuffer[buffer_p] = '\0'; uart_putc(rxbuf); } } else { switch (rxbuf) { case '\r': // got CR uart_putc(rxbuf); EditBuffer[buffer_p] = 0; buffer_p = 0; uart_puts("\r\n"); strcpy((char*)TargetBuffer, (char*)EditBuffer); line_complete = TRUE; uart_LineReceivedCallback(&EditBuffer); break; case '\b': // backspace if (buffer_p) { uart_puts("\b \b"); buffer_p--; } else uart_putc(0x07); // beep break; case '\e': // escape buffer_p = 0; // send singele escape break; default: break; } } HAL_UART_Receive_IT(hcomport, &rxbuf, 1); } } void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) { static uint8_t c; if (huart == hcomport) { if (fifo_get(&tx_fifo, &c)) { HAL_UART_Transmit_IT(hcomport, (uint8_t *) &c, 1); } else uart_busy = FALSE; } }