F6: USB glue cleanup and fixes (#666)
* USB-CDC: accepting the next data packet only if we process previous data * USB-CDC: use USB FS packet size * HAL-console: puts method * Check: print assertion data * FuriHal: rx stream free space aware CDC confirmation. * Bootloader: pull down USB lines, leave the rest to the firmware or bootloader * F6: cleanup and move USB code to usb-glue folder, add USB suspend/resume events to VCP, cleanup target.mk, fix missing motd message in cli when using minicom. * F5: cleanup the rest of USB glue code, adjust LPM and Power info data in descriptor. Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
@@ -66,4 +66,8 @@ void furi_hal_console_printf(const char format[], ...) {
|
||||
va_end(args);
|
||||
furi_hal_console_tx((const uint8_t*)string_get_cstr(string), string_size(string));
|
||||
string_clear(string);
|
||||
}
|
||||
|
||||
void furi_hal_console_puts(const char *data) {
|
||||
furi_hal_console_tx((const uint8_t*)data, strlen(data));
|
||||
}
|
@@ -11,8 +11,16 @@ void furi_hal_console_init();
|
||||
|
||||
void furi_hal_console_tx(const uint8_t* buffer, size_t buffer_size);
|
||||
|
||||
/**
|
||||
* Printf-like plain uart interface
|
||||
* @warning Will not work in ISR context
|
||||
* @param format
|
||||
* @param ...
|
||||
*/
|
||||
void furi_hal_console_printf(const char format[], ...);
|
||||
|
||||
void furi_hal_console_puts(const char* data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@@ -1,15 +1,20 @@
|
||||
#include <furi-hal-vcp.h>
|
||||
#include <usbd_cdc_if.h>
|
||||
#include <furi-hal-vcp_i.h>
|
||||
|
||||
#include <furi.h>
|
||||
#include <usbd_cdc_if.h>
|
||||
#include <stream_buffer.h>
|
||||
|
||||
#define FURI_HAL_VCP_RX_BUFFER_SIZE 600
|
||||
#define FURI_HAL_VCP_RX_BUFFER_SIZE (APP_RX_DATA_SIZE * 5)
|
||||
|
||||
extern USBD_HandleTypeDef hUsbDeviceFS;
|
||||
|
||||
typedef struct {
|
||||
volatile bool connected;
|
||||
|
||||
StreamBufferHandle_t rx_stream;
|
||||
volatile bool rx_stream_full;
|
||||
|
||||
osSemaphoreId_t tx_semaphore;
|
||||
volatile bool alive;
|
||||
volatile bool underrun;
|
||||
} FuriHalVcp;
|
||||
|
||||
static FuriHalVcp* furi_hal_vcp = NULL;
|
||||
@@ -17,66 +22,31 @@ static FuriHalVcp* furi_hal_vcp = NULL;
|
||||
static const uint8_t ascii_soh = 0x01;
|
||||
static const uint8_t ascii_eot = 0x04;
|
||||
|
||||
void _furi_hal_vcp_init();
|
||||
void _furi_hal_vcp_deinit();
|
||||
void _furi_hal_vcp_control_line(uint8_t state);
|
||||
void _furi_hal_vcp_rx_callback(const uint8_t* buffer, size_t size);
|
||||
void _furi_hal_vcp_tx_complete(size_t size);
|
||||
|
||||
void furi_hal_vcp_init() {
|
||||
furi_hal_vcp = furi_alloc(sizeof(FuriHalVcp));
|
||||
furi_hal_vcp->connected = false;
|
||||
|
||||
furi_hal_vcp->rx_stream = xStreamBufferCreate(FURI_HAL_VCP_RX_BUFFER_SIZE, 1);
|
||||
furi_hal_vcp->rx_stream_full = false;
|
||||
|
||||
furi_hal_vcp->tx_semaphore = osSemaphoreNew(1, 1, NULL);
|
||||
furi_hal_vcp->alive = false;
|
||||
furi_hal_vcp->underrun = false;
|
||||
|
||||
FURI_LOG_I("FuriHalVcp", "Init OK");
|
||||
}
|
||||
|
||||
void _furi_hal_vcp_init() {
|
||||
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
||||
}
|
||||
|
||||
void _furi_hal_vcp_deinit() {
|
||||
furi_hal_vcp->alive = false;
|
||||
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
||||
}
|
||||
|
||||
void _furi_hal_vcp_control_line(uint8_t state) {
|
||||
// bit 0: DTR state, bit 1: RTS state
|
||||
// bool dtr = state & 0b01;
|
||||
bool dtr = state & 0b1;
|
||||
|
||||
if (dtr) {
|
||||
if (!furi_hal_vcp->alive) {
|
||||
furi_hal_vcp->alive = true;
|
||||
_furi_hal_vcp_rx_callback(&ascii_soh, 1); // SOH
|
||||
}
|
||||
} else {
|
||||
if (furi_hal_vcp->alive) {
|
||||
_furi_hal_vcp_rx_callback(&ascii_eot, 1); // EOT
|
||||
furi_hal_vcp->alive = false;
|
||||
}
|
||||
}
|
||||
|
||||
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
||||
}
|
||||
|
||||
void _furi_hal_vcp_rx_callback(const uint8_t* buffer, size_t size) {
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
size_t ret = xStreamBufferSendFromISR(furi_hal_vcp->rx_stream, buffer, size, &xHigherPriorityTaskWoken);
|
||||
if (ret != size) {
|
||||
furi_hal_vcp->underrun = true;
|
||||
}
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
}
|
||||
|
||||
void _furi_hal_vcp_tx_complete(size_t size) {
|
||||
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
||||
}
|
||||
|
||||
size_t furi_hal_vcp_rx(uint8_t* buffer, size_t size) {
|
||||
furi_assert(furi_hal_vcp);
|
||||
return xStreamBufferReceive(furi_hal_vcp->rx_stream, buffer, size, portMAX_DELAY);
|
||||
|
||||
size_t received = xStreamBufferReceive(furi_hal_vcp->rx_stream, buffer, size, portMAX_DELAY);
|
||||
|
||||
if(furi_hal_vcp->rx_stream_full
|
||||
&&xStreamBufferSpacesAvailable(furi_hal_vcp->rx_stream) >= APP_RX_DATA_SIZE) {
|
||||
furi_hal_vcp->rx_stream_full = false;
|
||||
// data accepted, start waiting for next packet
|
||||
USBD_CDC_ReceivePacket(&hUsbDeviceFS);
|
||||
}
|
||||
|
||||
return received;
|
||||
}
|
||||
|
||||
size_t furi_hal_vcp_rx_with_timeout(uint8_t* buffer, size_t size, uint32_t timeout) {
|
||||
@@ -87,8 +57,10 @@ size_t furi_hal_vcp_rx_with_timeout(uint8_t* buffer, size_t size, uint32_t timeo
|
||||
void furi_hal_vcp_tx(const uint8_t* buffer, size_t size) {
|
||||
furi_assert(furi_hal_vcp);
|
||||
|
||||
while (size > 0 && furi_hal_vcp->alive) {
|
||||
while (size > 0 && furi_hal_vcp->connected) {
|
||||
furi_check(osSemaphoreAcquire(furi_hal_vcp->tx_semaphore, osWaitForever) == osOK);
|
||||
if (!furi_hal_vcp->connected)
|
||||
break;
|
||||
|
||||
size_t batch_size = size;
|
||||
if (batch_size > APP_TX_DATA_SIZE) {
|
||||
@@ -99,8 +71,58 @@ void furi_hal_vcp_tx(const uint8_t* buffer, size_t size) {
|
||||
size -= batch_size;
|
||||
buffer += batch_size;
|
||||
} else {
|
||||
// Shouldn't be there
|
||||
osDelay(100);
|
||||
FURI_LOG_E("FuriHalVcp", "CDC_Transmit_FS failed");
|
||||
osDelay(50);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void furi_hal_vcp_on_usb_resume() {
|
||||
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
||||
}
|
||||
|
||||
void furi_hal_vcp_on_usb_suspend() {
|
||||
if (furi_hal_vcp->connected) {
|
||||
furi_hal_vcp->connected = false;
|
||||
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
||||
}
|
||||
}
|
||||
|
||||
void furi_hal_vcp_on_cdc_control_line(uint8_t state) {
|
||||
// bit 0: DTR state, bit 1: RTS state
|
||||
// bool dtr = state & 0b01;
|
||||
bool dtr = state & 0b1;
|
||||
|
||||
if (dtr) {
|
||||
if (!furi_hal_vcp->connected) {
|
||||
furi_hal_vcp->connected = true;
|
||||
furi_hal_vcp_on_cdc_rx(&ascii_soh, 1); // SOH
|
||||
}
|
||||
} else {
|
||||
if (furi_hal_vcp->connected) {
|
||||
furi_hal_vcp_on_cdc_rx(&ascii_eot, 1); // EOT
|
||||
furi_hal_vcp->connected = false;
|
||||
}
|
||||
}
|
||||
|
||||
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
||||
}
|
||||
|
||||
void furi_hal_vcp_on_cdc_rx(const uint8_t* buffer, size_t size) {
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
size_t ret = xStreamBufferSendFromISR(furi_hal_vcp->rx_stream, buffer, size, &xHigherPriorityTaskWoken);
|
||||
furi_check(ret == size);
|
||||
|
||||
if (xStreamBufferSpacesAvailable(furi_hal_vcp->rx_stream) >= APP_RX_DATA_SIZE) {
|
||||
USBD_CDC_ReceivePacket(&hUsbDeviceFS);
|
||||
} else {
|
||||
furi_hal_vcp->rx_stream_full = true;
|
||||
}
|
||||
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
}
|
||||
|
||||
void furi_hal_vcp_on_cdc_tx_complete(size_t size) {
|
||||
osSemaphoreRelease(furi_hal_vcp->tx_semaphore);
|
||||
}
|
||||
|
||||
|
13
firmware/targets/f6/furi-hal/furi-hal-vcp_i.h
Normal file
13
firmware/targets/f6/furi-hal/furi-hal-vcp_i.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <furi-hal-vcp.h>
|
||||
|
||||
void furi_hal_vcp_on_usb_resume();
|
||||
|
||||
void furi_hal_vcp_on_usb_suspend();
|
||||
|
||||
void furi_hal_vcp_on_cdc_control_line(uint8_t state);
|
||||
|
||||
void furi_hal_vcp_on_cdc_rx(const uint8_t* buffer, size_t size);
|
||||
|
||||
void furi_hal_vcp_on_cdc_tx_complete(size_t size);
|
Reference in New Issue
Block a user