flipperzero-firmware/firmware/targets/f4/api-hal/api-hal-vcp.c
DrZlo13 8f9b2513ff
[FL-140] Core api dynamic records (#296)
* SYSTEM: tickless mode with deep sleep.
* Move FreeRTOS ticks to lptim2
* API: move all sumbodules init routines to one place. Timebase: working lptim2 at tick source.
* API Timebase: lp-timer routines, timer access safe zones prediction and synchronization. FreeRTOS: adjust configuration for tickless mode.
* NFC: support for tickless mode.
* API Timebase: improve tick error handling in IRQ. Apploader: use insomnia mode to run applications.
* BLE: prevent sleep while core2 starting
* HAL: nap while in insomnia mode
* init records work
* try to implement record delete
* tests and flapp
* flapp subsystem
* new core functions to get app stat, simplify core code
* fix thread termination
* add strdup to core
* fix tests
* Refactoring: remove all unusued parts, update API usage, aggreagate API sources and headers, new record storage
* Refactoring: update furi record api usage, cleanup code
* Fix broken merge for freertos apps
* Core, Target: fix compilation warnings
* Drop firmware target local
* HAL Timebase, Power, Clock: semaphore guarded access to clock and power modes, better sleep mode.
* SD-Filesystem: wait for all deps to arrive before adding widget. Core, BLE: disable debug dump to serial.
* delete old app example-ipc
* delete old app fatfs list
* fix strobe app, add input header
* delete old display driver
* comment old app qr-code
* fix sd-card test, add forced widget update
* remove unused new core test
* increase heap to 128k
* comment and assert old core tests
* fix syntax

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-01-20 19:09:26 +03:00

93 lines
2.5 KiB
C

#include <api-hal-vcp.h>
#include <usbd_cdc_if.h>
#include <furi.h>
#include <stream_buffer.h>
#define API_HAL_VCP_RX_BUFFER_SIZE 600
typedef struct {
StreamBufferHandle_t rx_stream;
osSemaphoreId_t tx_semaphore;
volatile bool alive;
volatile bool underrun;
} ApiHalVcp;
ApiHalVcp api_hal_vcp;
static const uint8_t ascii_soh = 0x01;
static const uint8_t ascii_eot = 0x04;
void _api_hal_vcp_init();
void _api_hal_vcp_deinit();
void _api_hal_vcp_control_line(uint8_t state);
void _api_hal_vcp_rx_callback(const uint8_t* buffer, size_t size);
void _api_hal_vcp_tx_complete(size_t size);
void api_hal_vcp_init() {
api_hal_vcp.rx_stream = xStreamBufferCreate(API_HAL_VCP_RX_BUFFER_SIZE, 1);
api_hal_vcp.tx_semaphore = osSemaphoreNew(1, 1, NULL);
api_hal_vcp.alive = false;
api_hal_vcp.underrun = false;
}
void _api_hal_vcp_init() {
osSemaphoreRelease(api_hal_vcp.tx_semaphore);
}
void _api_hal_vcp_deinit() {
api_hal_vcp.alive = false;
osSemaphoreRelease(api_hal_vcp.tx_semaphore);
}
void _api_hal_vcp_control_line(uint8_t state) {
// bit 0: DTR state, bit 1: RTS state
// bool dtr = state & 0b01;
bool rts = state & 0b10;
if (rts) {
api_hal_vcp.alive = true;
_api_hal_vcp_rx_callback(&ascii_soh, 1); // SOH
} else {
api_hal_vcp.alive = false;
_api_hal_vcp_rx_callback(&ascii_eot, 1); // EOT
}
osSemaphoreRelease(api_hal_vcp.tx_semaphore);
}
void _api_hal_vcp_rx_callback(const uint8_t* buffer, size_t size) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
size_t ret = xStreamBufferSendFromISR(api_hal_vcp.rx_stream, buffer, size, &xHigherPriorityTaskWoken);
if (ret != size) {
api_hal_vcp.underrun = true;
}
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
void _api_hal_vcp_tx_complete(size_t size) {
osSemaphoreRelease(api_hal_vcp.tx_semaphore);
}
size_t api_hal_vcp_rx(uint8_t* buffer, size_t size) {
return xStreamBufferReceive(api_hal_vcp.rx_stream, buffer, size, portMAX_DELAY);
}
void api_hal_vcp_tx(uint8_t* buffer, size_t size) {
while (size > 0 && api_hal_vcp.alive) {
furi_check(osSemaphoreAcquire(api_hal_vcp.tx_semaphore, osWaitForever) == osOK);
size_t batch_size = size;
if (batch_size > APP_TX_DATA_SIZE) {
batch_size = APP_TX_DATA_SIZE;
}
if (CDC_Transmit_FS(buffer, batch_size) == USBD_OK) {
size -= batch_size;
buffer += batch_size;
} else {
// Shouldn't be there
osDelay(100);
}
}
}