Loading...
Searching...
No Matches
usbcomm.c
1/* ===================================================================== *
2 * UART HANDLING *
3 * ===================================================================== */
4
5#include "usbcomm.h"
6
7#include "FreeRTOS.h"
8#include "message_buffer.h"
9#include "stdint.h"
10#include "stm32f439xx.h"
11
12#include "devicelist.h"
13#include "shell.h"
14#include "uart.h"
15
16extern MessageBufferHandle_t xUsbTxBuff;
17extern MessageBufferHandle_t xUsbRxBuff;
18
19uint8_t usbRxBuff[USB_RX_SIZE];
20uint8_t usbRxBuffIdx = 0;
21
22/* =============================================================================== */
27void vUsbTransmit(void *argument) {
28 const TickType_t timeout = portMAX_DELAY;
29 uint8_t rxData[100];
30
31 UART_t *usb = DeviceList_getDeviceHandle(DEVICE_UART_USB).device;
32
33 for (;;) {
34 // Read byte from UART Tx buffer, skip loop if empty
35 if (!xMessageBufferReceive(xUsbTxBuff, (void *)rxData, 100, timeout))
36 continue;
37
38 usb->print(usb, (char *)rxData);
39 }
40}
41
42/* =============================================================================== */
58void vUsbReceive(void *argument) {
59 const TickType_t timeout = portMAX_DELAY;
60 uint8_t rxData;
61
62 UART_t *usb = DeviceList_getDeviceHandle(DEVICE_UART_USB).device;
63 Shell *shell = argument;
64
65 for (;;) {
66 // Read byte from UART Rx buffer, skip loop if empty
67 if (!xStreamBufferReceive(xUsbRxBuff, (void *)&rxData, 1, timeout))
68 continue;
69
70 // Send byte back for display
71 usb->send(usb, rxData);
72
73 // Process command and reset buffer on <Enter> input
74 if (rxData == CARRIAGE_RETURN) {
75 usb->print(usb, "\n"); // Send newline back for display
76 usbRxBuff[usbRxBuffIdx - 1] = '\0'; // Replace carriage return with null terminator
77 shell->runTask(shell, usbRxBuff); // Run shell program as task
78 usbRxBuffIdx = 0; // Reset buffer
79 }
80
81 // Clear terminal on <Ctrl-c> input
82 else if (rxData == SIGINT) {
83 shell->clear(shell);
84 usbRxBuffIdx = 0;
85 }
86
87 // Erase character and move cursor backwards on <BS> input
88 else if (rxData == BACKSPACE) {
89 usb->print(usb, " \b");
90 if (usbRxBuffIdx)
91 usbRxBuffIdx -= 2;
92 }
93 }
94}
95
96/* =============================================================================== */
105void USART6_IRQHandler() {
106 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
107
108 // Read in data from USART6
109 while ((USART6->SR & USART_SR_RXNE) == 0);
110 uint8_t rxData = USART6->DR & 0xFF;
111
112 usbRxBuff[usbRxBuffIdx++] = rxData;
113 usbRxBuffIdx %= USB_RX_SIZE;
114
115 xStreamBufferSendFromISR(xUsbRxBuff, (void *)&rxData, 1, &xHigherPriorityTaskWoken);
116 portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
117}
DeviceHandle_t DeviceList_getDeviceHandle(DeviceKey)
Retrieve device handle from list by key.
Definition devicelist.c:36
#define USART_SR_RXNE
bool(* clear)(struct Shell *)
Definition shell.h:53
void(* runTask)(struct Shell *, uint8_t *)
Definition shell.h:52
Struct definition for shell interface.
Definition shell.h:48
void(* print)(struct UART *, char *)
UART print string method.
Definition uart.h:61
void(* send)(struct UART *, uint8_t)
UART send method.
Definition uart.h:59
Struct definition for UART interface.
Definition uart.h:52
CMSIS STM32F439xx Device Peripheral Access Layer Header File.