Loading...
Searching...
No Matches
usbcomm.c
1/* ===================================================================== *
2 * UART HANDLING *
3 * ===================================================================== */
4
5#include "usbcomm.h"
6
7extern MessageBufferHandle_t xUsbTxBuff;
8extern MessageBufferHandle_t xUsbRxBuff;
9
10uint8_t usbRxBuff[USB_RX_SIZE];
11uint8_t usbRxBuffIdx = 0;
12
13/* =============================================================================== */
18void vUsbTransmit(void *argument) {
19 const TickType_t timeout = portMAX_DELAY;
20 uint8_t rxData[100];
21
22 UART *usb = DeviceHandle_getHandle("USB").device;
23
24 for (;;) {
25 // Read byte from UART Tx buffer, skip loop if empty
26 if (!xMessageBufferReceive(xUsbTxBuff, (void *)rxData, 100, timeout))
27 continue;
28
29 usb->print(usb, (char *)rxData);
30 }
31}
32
33/* =============================================================================== */
49void vUsbReceive(void *argument) {
50 const TickType_t timeout = portMAX_DELAY;
51 uint8_t rxData;
52
53 UART *usb = DeviceHandle_getHandle("USB").device;
54 Shell *shell = argument;
55
56 for (;;) {
57 // Read byte from UART Rx buffer, skip loop if empty
58 if (!xStreamBufferReceive(xUsbRxBuff, (void *)&rxData, 1, timeout))
59 continue;
60
61 // Send byte back for display
62 usb->send(usb, rxData);
63
64 // Process command and reset buffer on <Enter> input
65 if (rxData == CARRIAGE_RETURN) {
66 usb->print(usb, "\n"); // Send newline back for display
67 usbRxBuff[usbRxBuffIdx - 1] = '\0'; // Replace carriage return with null terminator
68 shell->runTask(shell, usbRxBuff); // Run shell program as task
69 usbRxBuffIdx = 0; // Reset buffer
70 }
71
72 // Clear terminal on <Ctrl-c> input
73 else if (rxData == SIGINT) {
74 shell->clear(shell);
75 usbRxBuffIdx = 0;
76 }
77
78 // Erase character and move cursor backwards on <BS> input
79 else if (rxData == BACKSPACE) {
80 usb->print(usb, " \b");
81 if (usbRxBuffIdx)
82 usbRxBuffIdx -= 2;
83 }
84 }
85}
86
87/* =============================================================================== */
96void USART6_IRQHandler() {
97 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
98
99 // Read in data from USART6
100 while ((USART6->SR & USART_SR_RXNE) == 0);
101 uint8_t rxData = USART6->DR & 0xFF;
102
103 usbRxBuff[usbRxBuffIdx++] = rxData;
104 usbRxBuffIdx %= USB_RX_SIZE;
105
106 xStreamBufferSendFromISR(xUsbRxBuff, (void *)&rxData, 1, &xHigherPriorityTaskWoken);
107 portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
108}
Struct definition for UART interface.
Definition uart.h:53
Definition shell.h:34