pre uart exchange

This commit is contained in:
2026-03-04 16:32:51 +01:00
parent b665cb5def
commit 4f3fbff258
46 changed files with 2820 additions and 3186 deletions

View File

@@ -1,5 +1,6 @@
#include <zephyr/fs/littlefs.h>
#include <zephyr/drivers/flash.h>
#include <zephyr/storage/flash_map.h>
#include <zephyr/dfu/flash_img.h>
#include <zephyr/dfu/mcuboot.h>
#include <zephyr/pm/device.h>
@@ -416,4 +417,79 @@ int flash_write_firmware_block(const uint8_t *buffer, size_t length, bool is_las
return rc;
}
return 0;
}
size_t fs_get_external_flash_page_size(void) {
const struct flash_area *fa;
const struct device *dev;
struct flash_pages_info info;
int rc;
rc = flash_area_open(STORAGE_PARTITION_ID, &fa);
if (rc != 0) {
LOG_ERR("Failed to open flash area for page size retrieval");
return 256; // Fallback to a common page size
}
dev = flash_area_get_device(fa);
if (dev == NULL) {
flash_area_close(fa);
LOG_ERR("Failed to get flash device for page size retrieval");
return 256; // Fallback to a common page size
}
rc = flash_get_page_info_by_offs(dev, fa->fa_off, &info);
flash_area_close(fa);
if (rc != 0) {
LOG_ERR("Failed to get flash page info: %d", rc);
return 256; // Fallback to a common page size
}
return info.size;
}
size_t fs_get_fw_slot_size(void) {
const struct flash_area *fa;
int rc;
rc = flash_area_open(SLOT1_ID, &fa);
if (rc != 0) {
LOG_ERR("Failed to open flash area for slot size retrieval");
return 0;
}
size_t slot_size = fa->fa_size;
flash_area_close(fa);
return slot_size;
}
size_t fs_get_internal_flash_page_size(void) {
const struct flash_area *fa;
const struct device *dev;
struct flash_pages_info info;
int rc;
rc = flash_area_open(SLOT1_ID, &fa);
if (rc != 0) {
LOG_ERR("Failed to open flash area for page size retrieval");
return 256; // Fallback to a common page size
}
dev = flash_area_get_device(fa);
if (dev == NULL) {
flash_area_close(fa);
LOG_ERR("Failed to get flash device for page size retrieval");
return 256; // Fallback to a common page size
}
rc = flash_get_page_info_by_offs(dev, fa->fa_off, &info);
flash_area_close(fa);
if (rc != 0) {
LOG_ERR("Failed to get flash page info: %d", rc);
return 256; // Fallback to a common page size
}
return info.size;
}

View File

@@ -3,6 +3,8 @@
#include <zephyr/fs/fs.h>
#define MAX_PATH_LEN 32U
typedef struct slot_info_t {
size_t start_addr;
size_t size;
@@ -182,4 +184,21 @@ int flash_init_firmware_upload(void);
*/
int flash_write_firmware_block(const uint8_t *buffer, size_t length, bool is_last_block);
/**
* @brief Gets the page size of the internal flash, which is needed for proper write operations
* @return Page size in bytes
*/
size_t fs_get_internal_flash_page_size(void);
/**
* @brief Gets the size of the firmware slot, which is needed for proper write operations
* @return Size in bytes
*/
size_t fs_get_fw_slot_size(void);
/**
* @brief Gets the page size of the external flash, which is needed for proper write operations
* @return Page size in bytes
*/
size_t fs_get_external_flash_page_size(void);
#endif // FS_H

File diff suppressed because it is too large Load Diff

View File

@@ -8,44 +8,42 @@
typedef enum {
PS_WAIT_SYNC = 0,
PS_READ_HEADER,
PS_READ_PAYLOAD,
PS_READ_PAYLOAD_CRC,
PS_READ_FRAME_TYPE,
PS_READ_REQ,
PS_READ_REQ_DATA,
} protocol_state_t;
typedef enum {
CMD_INVALID = 0,
CMD_GET_PROTOCOL_VERSION = 0x00,
CMD_GET_FIRMWARE_STATUS = 0x01,
CMD_GET_FLASH_STATUS = 0x02,
CMD_CONFIRM_FIRMWARE = 0x03,
CMD_REBOOT = 0x04,
CMD_LIST_DIR = 0x10,
CMD_CHECK_FILE_CRC = 0x11,
CMD_MKDIR = 0x12,
CMD_RM = 0x13,
CMD_PUT_FILE_START = 0x14,
CMD_PUT_FILE_CHUNK = 0x15,
CMD_PUT_FILE_END = 0x16,
CMD_PUT_FW_START = 0x17,
CMD_STAT = 0x18,
CMD_RENAME = 0x19,
CMD_RM_R = 0x1A,
CMD_GET_FILE = 0x1B,
CMD_GET_TAG_BLOB = 0x20,
CMD_SET_TAG_BLOB_START = 0x21,
CMD_SET_TAG_BLOB_CHUNK = 0x22,
CMD_SET_TAG_BLOB_END = 0x23,
CMD_PUT_FILE = 0x20,
CMD_PUT_FW = 0x21,
CMD_GET_FILE = 0x22,
} protocol_cmd_t;
typedef enum {
FRAME_REQ = 0x01,
FRAME_REQ_DATA = 0x02,
FRAME_RESP_ACK = 0x10,
FRAME_RESP_DATA = 0x11,
FRAME_RESP_STREAM_START = 0x12,
FRAME_RESP_STREAM_CHUNK = 0x13,
// FRAME_RESP_STREAM_CHUNK = 0x13,
FRAME_RESP_STREAM_END = 0x14,
FRAME_RESP_ERROR = 0x7F,
FRAME_RESP_LIST_START = 0x15,
FRAME_RESP_LIST_CHUNK = 0x16,
FRAME_RESP_LIST_END = 0x17,
FRAME_RESP_ERROR = 0xFF,
} protocol_frame_type_t;
typedef enum {
@@ -72,6 +70,13 @@ typedef enum {
P_ERR_INTERNAL = 0x32,
} protocol_error_t;
typedef enum
{
FW_STATUS_CONFIRMED = 0x00,
FW_STATUS_PENDING = 0x01,
FW_STATUS_TESTING = 0x02,
} firmware_status_t;
void protocol_thread_entry(void *p1, void *p2, void *p3);
#endif // PROTOCOL_H

1195
firmware/src/protocol_old.c Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -6,6 +6,8 @@
#include <io.h>
#define RX_RING_BUF_SIZE 1024
LOG_MODULE_REGISTER(usb, LOG_LEVEL_INF);
K_SEM_DEFINE(usb_rx_sem, 0, 1);
@@ -13,9 +15,8 @@ K_SEM_DEFINE(usb_tx_sem, 0, 1);
#define UART_NODE DT_ALIAS(usb_uart)
const struct device *cdc_dev = DEVICE_DT_GET(UART_NODE);
static volatile bool rx_interrupt_enabled = false;
/* NEU: Ringbuffer für stabilen asynchronen USB-Empfang */
#define RX_RING_BUF_SIZE 5*1024 /* 8 KB Ringpuffer für eingehende USB-Daten */
RING_BUF_DECLARE(rx_ringbuf, RX_RING_BUF_SIZE);
static void cdc_acm_irq_cb(const struct device *dev, void *user_data)
@@ -36,7 +37,6 @@ static void cdc_acm_irq_cb(const struct device *dev, void *user_data)
und der USB-Stack den Host drosselt (NAK). */
uart_irq_rx_disable(dev);
} else {
/* Nur so viele Daten lesen, wie Platz im Ringpuffer ist */
int to_read = MIN(sizeof(buffer), space);
int len = uart_fifo_read(dev, buffer, to_read);
@@ -68,23 +68,15 @@ bool usb_wait_for_data(k_timeout_t timeout)
return (k_sem_take(&usb_rx_sem, timeout) == 0);
}
int usb_read_char(uint8_t *c)
bool usb_read_byte(uint8_t *c)
{
int ret = ring_buf_get(&rx_ringbuf, c, 1);
if (ret > 0 && device_is_ready(cdc_dev)) {
/* Platz geschaffen -> Empfang wieder aktivieren */
uart_irq_rx_enable(cdc_dev);
}
return ret;
return (ret > 0);
}
int usb_read_buffer(uint8_t *buf, size_t max_len)
{
int ret = ring_buf_get(&rx_ringbuf, buf, max_len);
if (ret > 0 && device_is_ready(cdc_dev)) {
/* Platz geschaffen -> Empfang wieder aktivieren */
uart_irq_rx_enable(cdc_dev);
}
return ret;
}
@@ -95,7 +87,7 @@ void usb_resume_rx(void)
}
}
void usb_write_char(uint8_t c)
void usb_write_byte(uint8_t c)
{
if (!device_is_ready(cdc_dev)) {
return;
@@ -103,11 +95,11 @@ void usb_write_char(uint8_t c)
uart_poll_out(cdc_dev, c);
}
void usb_write_buffer(const uint8_t *buf, size_t len)
int usb_write_buffer(const uint8_t *buf, size_t len)
{
if (!device_is_ready(cdc_dev))
{
return;
return -ENODEV;
}
size_t written;
@@ -126,10 +118,11 @@ void usb_write_buffer(const uint8_t *buf, size_t len)
if (k_sem_take(&usb_tx_sem, K_MSEC(100)) != 0)
{
LOG_WRN("USB TX timeout - consumer not reading?");
return;
return -ETIMEDOUT;
}
}
}
return 0;
}
void usb_flush_rx(void)

View File

@@ -17,9 +17,9 @@ bool usb_wait_for_data(k_timeout_t timeout);
/**
* @brief Reads a single character from the USB RX FIFO
* @param c Pointer to store the read character
* @return 1 if a character was read, 0 if no data was available
* @return true if a character was read, false if no data was available
*/
int usb_read_char(uint8_t *c);
bool usb_read_byte(uint8_t *c);
/**
* @brief Reads a block of data from the USB RX FIFO
@@ -36,16 +36,16 @@ void usb_resume_rx(void);
/**
* @brief Writes a single character to the USB TX FIFO
* @param c Character to write
* @param c Character to write
*/
void usb_write_char(uint8_t c);
void usb_write_byte(uint8_t c);
/**
* @brief Writes a block of data to the USB TX FIFO
* @param buf Buffer containing the data to write
* @param len Number of bytes to write
*/
void usb_write_buffer(const uint8_t *buf, size_t len);
int usb_write_buffer(const uint8_t *buf, size_t len);
/**
* @brief Flushes the USB RX FIFO