Skip to content

Commit

Permalink
Merge pull request #18 from hydrausb3/improve_stress_resistance
Browse files Browse the repository at this point in the history
Improve stress resistance
  • Loading branch information
kauwua authored Feb 24, 2025
2 parents 579f903 + 8e879c4 commit 3b056b5
Show file tree
Hide file tree
Showing 89 changed files with 2,820 additions and 292 deletions.
7 changes: 7 additions & 0 deletions BREAKING_CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
v1.1.4:

* `USBDevice` folder renamed to `usb`
* `SerDesDevice` folder renamed to `serdes`
* `HSPIDeviceScheduled` folder renamed to `hspi_scheduled`
* `HSPIDevice` folder renamed to `hspi`
* Renamed `LOG_OUTPUT` option `printf` to `uart`
2 changes: 1 addition & 1 deletion BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ Some logging filters might activate too much logs for the USB controllers to wor
In this case, the `LOG` macro will bypass all filters and levels, allowing you to log just what you need.

- Log methods
- `LOG_OUTPUT=printf`. Logs are written directly to the UART
- `LOG_OUTPUT=uart`. Logs are written directly to the UART
- `LOG_OUTPUT=buffer`. Logs are stored in a buffer, and flushed to the UART when calling `LOG_DUMP()`
- `LOG_OUTPUT=serdes`. Logs are directly sent using Serdes. Might be used to share logs from one board to the other. Not very well tested.
- Log levels
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This library was built alongside [Hydradancer](https://github.com/HydraDancer/hy

Reliable drivers were needed for this project and many features were missing from the WCH examples or untested.

This library provides USB3, USB2, HSPI and SerDes drivers that have been tested using the benchmark/integrity tests in `tests/`. It is based on `wch-ch56x-bsp` but its goal is to provide a higher level library. As Hydradancer was using several peripherals at a time (USB3/HSPI, USB2/HSPI), an interrupt queue was implemented to avoid missing interrupts for use with `HSPIDeviceScheduled` along with a static memory pool. While the tests in this repository are simple enough to do the processing inside the interrupt handlers, Hydradancer was missing interrupts and deferring interrupts to user mode was required.
This library provides USB3, USB2, HSPI and SerDes drivers that have been tested using the benchmark/integrity tests in `tests/`. It is based on `wch-ch56x-bsp` but its goal is to provide a higher level library. As Hydradancer was using several peripherals at a time (USB3/HSPI, USB2/HSPI), an interrupt queue was implemented to avoid missing interrupts for use with `hspi_scheduled` along with a static memory pool. While the tests in this repository are simple enough to do the processing inside the interrupt handlers, Hydradancer was missing interrupts and deferring interrupts to user mode was required.

# Using this library in your project

Expand Down
28 changes: 14 additions & 14 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
add_library(wch-ch56x-lib INTERFACE)

# With HSPIDeviceScheduled
# With hspi_scheduled
target_sources(wch-ch56x-lib INTERFACE
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/utils/critical_section.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/HSPIDevice/hspi.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/hspi/hspi.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/interrupt_queue/interrupt_queue.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/logging/log_printf.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/logging/log_serdes.c
Expand All @@ -12,24 +12,24 @@ target_sources(wch-ch56x-lib INTERFACE
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/logging/nanoprintf_impl.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/memory/alloc.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/memory/ramx_alloc.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/SerDesDevice/serdes.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/USBDevice/usb_device.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/USBDevice/usb_endpoints.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/USBDevice/usb20.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/USBDevice/usb30.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/serdes/serdes.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/usb/usb_device.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/usb/usb_endpoints.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/usb/usb20.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/usb/usb30.c
)

target_include_directories(wch-ch56x-lib INTERFACE ${CMAKE_CURRENT_LIST_DIR})

target_link_libraries(wch-ch56x-lib INTERFACE wch-ch56x-bsp lwrb nanoprintf)

# Without HSPIDeviceScheduled
# Without hspi_scheduled

add_library(wch-ch56x-lib-scheduled INTERFACE)

target_sources(wch-ch56x-lib-scheduled INTERFACE
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/utils/critical_section.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/HSPIDeviceScheduled/hspi_scheduled.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/hspi_scheduled/hspi_scheduled.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/interrupt_queue/interrupt_queue.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/logging/log_printf.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/logging/log_serdes.c
Expand All @@ -38,11 +38,11 @@ target_sources(wch-ch56x-lib-scheduled INTERFACE
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/logging/nanoprintf_impl.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/memory/alloc.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/memory/ramx_alloc.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/SerDesDevice/serdes.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/USBDevice/usb_device.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/USBDevice/usb_endpoints.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/USBDevice/usb20.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/USBDevice/usb30.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/serdes/serdes.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/usb/usb_device.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/usb/usb_endpoints.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/usb/usb20.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/usb/usb30.c
)

target_include_directories(wch-ch56x-lib-scheduled INTERFACE ${CMAKE_CURRENT_LIST_DIR})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
********************************************************************************/

#include "wch-ch56x-lib/HSPIDevice/hspi.h"
#include "wch-ch56x-lib/hspi/hspi.h"
#include "wch-ch56x-lib/logging/logging.h"
#include <stdint.h>

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
*******************************************************************************/

#include "wch-ch56x-lib/HSPIDeviceScheduled/hspi_scheduled.h"
#include "wch-ch56x-lib/hspi_scheduled/hspi_scheduled.h"
#include "wch-ch56x-lib/interrupt_queue/interrupt_queue.h"
#include "wch-ch56x-lib/logging/log_to_buffer.h"
#include "wch-ch56x-lib/logging/logging.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ limitations under the License.
*******************************************************************************/

/**
HSPIDeviceScheduled uses an interrupt queue to process RX and TX interrupts.
hspi_scheduled uses an interrupt queue to process RX and TX interrupts.
It uses a statically allocated pool to save call arguments for use when the
function is scheduled.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/wch-ch56x-lib/logging/log_serdes.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ limitations under the License.

#include "wch-ch56x-lib/logging/log_serdes.h"
#include "wch-ch56x-lib/logging/nanoprintf_impl.h"
#include "wch-ch56x-lib/SerDesDevice/serdes.h"
#include "wch-ch56x-lib/serdes/serdes.h"
#include "wch-ch56x-lib/utils/critical_section.h"
#include <stdarg.h>

Expand Down
41 changes: 11 additions & 30 deletions src/wch-ch56x-lib/memory/fifo.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,23 @@ _fifo_read_n(hydra_fifo_t* fifo, void* buffer, uint16_t n, uint16_t rd_idx,
{
uint16_t linear_count = MIN(n, wr_idx - rd_idx);
uint16_t linear_bytes = linear_count * fifo->type_size;
memcpy(buffer, fifo->buffer + rd_idx * fifo->type_size, linear_bytes);
memcpy(buffer, fifo->buffer + (rd_idx + 1) * fifo->type_size, linear_bytes);
LOG_IF_LEVEL(LOG_LEVEL_TRACE,
"read rd_idx=%d wr_idx=%d linear_count=%d\r\n", rd_idx, wr_idx,
linear_count);
return linear_count;
}

uint16_t linear_count = MIN(n, fifo->size - rd_idx);
uint16_t linear_count = MIN(n, fifo->size - 1 - rd_idx);
uint16_t linear_bytes = linear_count * fifo->type_size;
memcpy(buffer, fifo->buffer + rd_idx * fifo->type_size, linear_bytes);
memcpy(buffer, fifo->buffer + (rd_idx + 1) * fifo->type_size, linear_bytes);
LOG_IF_LEVEL(LOG_LEVEL_TRACE, "read rd_idx=%d wr_idx=%d linear_count=%d\r\n",
rd_idx, wr_idx, linear_count);

if (n > linear_count)
{
uint16_t wrap_count = MIN(wr_idx, n - linear_count);
uint16_t wrap_bytes = wr_idx * fifo->type_size;
uint16_t wrap_count = MIN(wr_idx + 1, n - linear_count);
uint16_t wrap_bytes = wrap_count * fifo->type_size;
memcpy((uint8_t*)buffer + linear_bytes, fifo->buffer, wrap_bytes);
LOG_IF_LEVEL(LOG_LEVEL_TRACE,
"read rd_idx=%d wr_idx=%d linear_count=%d, wrap_count=%d\r\n",
Expand Down Expand Up @@ -191,23 +191,22 @@ _fifo_write_n(hydra_fifo_t* fifo, void* buffer, uint16_t n, uint16_t rd_idx,
{
uint16_t linear_count = MIN(free_space, n);
uint16_t linear_bytes = linear_count * fifo->type_size;
memcpy(fifo->buffer + wr_idx * fifo->type_size, buffer, linear_bytes);
memcpy(fifo->buffer + (wr_idx + 1) * fifo->type_size, buffer, linear_bytes);
LOG_IF_LEVEL(LOG_LEVEL_TRACE,
"write rd_idx=%d wr_idx=%d linear_count=%d\r\n", rd_idx,
wr_idx, linear_count);
return linear_count;
}

uint16_t linear_count = MIN(fifo->size - wr_idx, n);
uint16_t linear_count = MIN(fifo->size - 1 - wr_idx, n);
uint16_t linear_bytes = linear_count * fifo->type_size;
memcpy(fifo->buffer + wr_idx * fifo->type_size, buffer, linear_bytes);
memcpy(fifo->buffer + (wr_idx + 1) * fifo->type_size, buffer, linear_bytes);

if (n > linear_count)
{
uint16_t wrap_count = MIN(rd_idx, n - linear_count);
uint16_t wrap_bytes = wrap_count * fifo->type_size;
memcpy(fifo->buffer + wr_idx * fifo->type_size + linear_bytes,
(uint8_t*)buffer + linear_bytes, wrap_bytes);
memcpy(fifo->buffer, (uint8_t*)buffer + linear_bytes, wrap_bytes);
LOG_IF_LEVEL(LOG_LEVEL_TRACE,
"write rd_idx=%d wr_idx=%d linear_count=%d, wrap_count=%d\r\n",
rd_idx, wr_idx, linear_count, wrap_count);
Expand All @@ -227,33 +226,15 @@ _fifo_advance_read(hydra_fifo_t* fifo, uint16_t rd_idx, uint16_t offset);
__attribute__((always_inline)) static inline void
_fifo_advance_read(hydra_fifo_t* fifo, uint16_t rd_idx, uint16_t offset)
{
uint16_t remaining = fifo->size - rd_idx;
LOG_IF_LEVEL(LOG_LEVEL_TRACE, "advance_read rd_idx=%d offset=%d\r\n", rd_idx,
offset);

if (offset < remaining)
{
fifo->rd_idx += offset;
return;
}
fifo->rd_idx = offset - remaining;
fifo->rd_idx = (fifo->rd_idx + offset) % fifo->size;
}

__attribute__((always_inline)) static inline void
_fifo_advance_write(hydra_fifo_t* fifo, uint16_t wr_idx, uint16_t offset);
__attribute__((always_inline)) static inline void
_fifo_advance_write(hydra_fifo_t* fifo, uint16_t wr_idx, uint16_t offset)
{
uint16_t remaining = fifo->size - wr_idx;
LOG_IF_LEVEL(LOG_LEVEL_TRACE, "advance_write wr_idx=%d offset=%d\r\n", wr_idx,
offset);

if (offset < remaining)
{
fifo->wr_idx += offset;
return;
}
fifo->wr_idx = offset - remaining;
fifo->wr_idx = (fifo->wr_idx + offset) % fifo->size;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.
*******************************************************************************/

#include "wch-ch56x-lib/SerDesDevice/serdes.h"
#include "wch-ch56x-lib/serdes/serdes.h"
#include "wch-ch56x-lib/logging/logging.h"

volatile uint32_t SDS_RX_LEN0 = 0;
Expand Down
File renamed without changes.
Loading

0 comments on commit 3b056b5

Please sign in to comment.