Skip to content

Commit

Permalink
Merge pull request #15 from hydrausb3/v1.1.3
Browse files Browse the repository at this point in the history
V1.1.3
  • Loading branch information
kauwua authored Sep 6, 2024
2 parents 9041c32 + ecf7f47 commit 4e75bd7
Show file tree
Hide file tree
Showing 17 changed files with 366 additions and 58 deletions.
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ add_library(wch-ch56x-lib INTERFACE)

# With HSPIDeviceScheduled
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/interrupt_queue/interrupt_queue.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/logging/log_printf.c
Expand All @@ -10,7 +11,6 @@ target_sources(wch-ch56x-lib INTERFACE
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/logging/logging.c
${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/fifo.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
Expand All @@ -28,6 +28,7 @@ target_link_libraries(wch-ch56x-lib INTERFACE wch-ch56x-bsp lwrb nanoprintf)
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/interrupt_queue/interrupt_queue.c
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/logging/log_printf.c
Expand All @@ -36,7 +37,6 @@ target_sources(wch-ch56x-lib-scheduled INTERFACE
${CMAKE_CURRENT_LIST_DIR}/wch-ch56x-lib/logging/logging.c
${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/fifo.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
Expand Down
5 changes: 3 additions & 2 deletions src/wch-ch56x-lib/HSPIDeviceScheduled/hspi_scheduled.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
#include "wch-ch56x-lib/logging/logging.h"
#include "wch-ch56x-lib/memory/fifo.h"
#include "wch-ch56x-lib/memory/pool.h"
#include "wch-ch56x-lib/utils/critical_section.h"
#include <stdint.h>

__attribute__((aligned(16))) uint8_t* hspi_rx_buffer_0;
Expand Down Expand Up @@ -251,7 +252,7 @@ bool _hspi_send(uint8_t* data)
if (hspi_task_args == NULL)
return false;

bsp_disable_interrupt();
BSP_ENTER_CRITICAL();
if (R8_HSPI_TX_SC & RB_HSPI_TX_TOG)
{
R32_HSPI_TX_ADDR1 = (vuint32_t)hspi_task_args->args.buffer;
Expand All @@ -277,7 +278,7 @@ bool _hspi_send(uint8_t* data)
// tx1_addr[1], tx1_addr[2], tx1_addr[3], tx1_addr[4]);
}
hspi_transmission_finished = false;
bsp_enable_interrupt();
BSP_EXIT_CRITICAL();

HSPI_DMA_Tx();

Expand Down
2 changes: 1 addition & 1 deletion src/wch-ch56x-lib/USBDevice/usb30.c
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ usb30_ep_in_handler(uint8_t endp_num)
}
}

if (nump == 0 && *usb30_get_tx_endpoint_remaining_length(endp_num) == 0)
if (*usb30_get_tx_endpoint_remaining_length(endp_num) == 0)
{
LOG_IF(LOG_LEVEL_DEBUG, LOG_ID_USB3,
"Finished IN transfer on ep %d, remaining length %d\r\n", endp_num,
Expand Down
12 changes: 7 additions & 5 deletions src/wch-ch56x-lib/USBDevice/usb_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ USB3.0/USB2.0 management in the back.
#pragma GCC diagnostic pop
#pragma GCC diagnostic pop
#pragma GCC diagnostic pop

#include "wch-ch56x-lib/logging/logging.h"
#include "wch-ch56x-lib/USBDevice/usb_descriptors.h"
#include "wch-ch56x-lib/USBDevice/usb_endpoints.h"
#include "wch-ch56x-lib/USBDevice/usb_types.h"
#include "wch-ch56x-lib/utils/critical_section.h"
#include <stdint.h>

#ifdef __cplusplus
Expand Down Expand Up @@ -136,14 +139,14 @@ void usb_device_set_endpoint_mask(usb_device_t* usb_device, uint32_t endpoint_ma
__attribute__((always_inline)) inline static bool
endp_tx_set_new_buffer(usb_device_t* usb_device, uint8_t endp_num, uint8_t* const ptr, uint16_t size)
{
bsp_disable_interrupt();
volatile USB_ENDPOINT* ep = &usb_device->endpoints.tx[endp_num];
BSP_ENTER_CRITICAL();

if (size > ep->max_packet_size_with_burst || ptr == 0)
{
bsp_enable_interrupt();
BSP_EXIT_CRITICAL();
return false;
}

ep->buffer = ptr;

if (endp_num == 0)
Expand All @@ -152,12 +155,11 @@ endp_tx_set_new_buffer(usb_device_t* usb_device, uint8_t endp_num, uint8_t* cons
memcpy(usb_device->endpoints.rx[0].buffer, ptr, size);
}

bsp_enable_interrupt();
if (usb_device->speed == USB30_SUPERSPEED)
usb3_endpoints_backend_handled.usb3_endp_tx_ready(endp_num, size);
else
usb2_endpoints_backend_handled.usb2_endp_tx_ready(endp_num, size);

BSP_EXIT_CRITICAL();
return true;
}

Expand Down
6 changes: 4 additions & 2 deletions src/wch-ch56x-lib/logging/log_printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
*******************************************************************************/

#include "wch-ch56x-lib/logging/log_printf.h"
#include "wch-ch56x-lib/logging/logging_definitions.h"
#include "wch-ch56x-lib/logging/nanoprintf_impl.h"
#include "wch-ch56x-lib/utils/critical_section.h"

// Add option in pre-processor compiler option
// CH56x_DEBUG_LOG_BASIC_TIMESTAMP=1
Expand Down Expand Up @@ -113,7 +115,7 @@ void _vlog_printf(const char* fmt, va_list args)
#endif
#endif // ifndef CH56x_DEBUG_LOG_BASIC_TIMESTAMP

bsp_disable_interrupt(); // Enter Critical Section
BSP_ENTER_CRITICAL(); // Enter Critical Section
#ifdef CH56x_DEBUG_LOG_BASIC_TIMESTAMP
print_size1 = npf_snprintf(log_printf_buff, sizeof(log_printf_buff),
"0x%08X ", (uint32_t)(delta));
Expand All @@ -137,7 +139,7 @@ void _vlog_printf(const char* fmt, va_list args)
}
UART1_SendString((uint8_t*)log_printf_buff, print_size2);
}
bsp_enable_interrupt(); // Exit Critical Section
BSP_EXIT_CRITICAL(); // Exit Critical Section
bsp_uled_off();
}

Expand Down
6 changes: 3 additions & 3 deletions src/wch-ch56x-lib/logging/log_serdes.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ limitations under the License.
*******************************************************************************/

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

// Add option in pre-processor compiler option
Expand Down Expand Up @@ -116,7 +116,7 @@ void vlog_serdes(const char* fmt, va_list args)
#endif
#endif // ifndef CH56x_DEBUG_LOG_BASIC_TIMESTAMP

bsp_disable_interrupt(); // Enter Critical Section
BSP_ENTER_CRITICAL(); // Enter Critical Section
#ifdef CH56x_DEBUG_LOG_BASIC_TIMESTAMP
print_size1 = npf_snprintf(serdes_buffer, sizeof(serdes_buffer), "0x%08X ",
(uint32_t)(delta));
Expand All @@ -140,7 +140,7 @@ void vlog_serdes(const char* fmt, va_list args)
}
serdes_send((uint8_t*)serdes_buffer, print_size2, 0);
}
bsp_enable_interrupt(); // Exit Critical Section
BSP_EXIT_CRITICAL(); // Exit Critical Section
}

void log_serdes(const char* fmt, ...)
Expand Down
10 changes: 5 additions & 5 deletions src/wch-ch56x-lib/logging/log_to_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ limitations under the License.
*******************************************************************************/

#include "wch-ch56x-lib/logging/log_to_buffer.h"
#include "CH56x_common.h"
#include "wch-ch56x-lib/logging/nanoprintf_impl.h"
#include "wch-ch56x-lib/utils/critical_section.h"
#include <stdarg.h>
#include "lwrb/lwrb.h"

Expand Down Expand Up @@ -146,9 +146,9 @@ void vlog_to_buffer(const char* fmt, va_list args)
print_size2 += print_size1;
if (print_size2 > 0)
{
bsp_disable_interrupt();
BSP_ENTER_CRITICAL();
lwrb_write(&lwrb_buffer, temp_buffer, print_size2);
bsp_enable_interrupt();
BSP_EXIT_CRITICAL();
}
}

Expand Down Expand Up @@ -180,7 +180,7 @@ void log_to_buffer_dump_to_uart(void)

void _write_to_buffer(char* buffer, size_t size)
{
bsp_disable_interrupt();
BSP_ENTER_CRITICAL();
lwrb_write(&lwrb_buffer, buffer, size);
bsp_enable_interrupt();
BSP_EXIT_CRITICAL();
}
15 changes: 13 additions & 2 deletions src/wch-ch56x-lib/memory/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static pool_t ram_pool;
void pool_init(void)
{
uint32_t i;

RAM_ALLOC_ENTER_CRITICAL();
ram_pool.pool_size = POOL_BLOCK_NUM;
ram_pool.block_size = POOL_BLOCK_SIZE;
ram_pool.blocks_used = 0;
Expand All @@ -45,6 +45,7 @@ void pool_init(void)
{
ram_pool.blocks[i] = 0;
}
RAM_ALLOC_EXIT_CRITICAL();
}

/**
Expand All @@ -61,13 +62,17 @@ void* pool_alloc_blocks(uint8_t num_blocks)
{
uint32_t i, j;
uint8_t space_found;
void* ret = NULL;

RAM_ALLOC_ENTER_CRITICAL();
if (num_blocks == 0)
{
RAM_ALLOC_EXIT_CRITICAL();
return 0;
}
if (num_blocks > POOL_BLOCK_NUM)
{
RAM_ALLOC_EXIT_CRITICAL();
return 0;
}

Expand Down Expand Up @@ -96,10 +101,13 @@ void* pool_alloc_blocks(uint8_t num_blocks)
ram_pool.blocks[i + j] = num_blocks;
}
ram_pool.blocks_used += num_blocks;
return ram_pool.pool + (ram_pool.block_size * i);
ret = ram_pool.pool + (ram_pool.block_size * i);
RAM_ALLOC_EXIT_CRITICAL();
return ret;
}
}
}
RAM_ALLOC_EXIT_CRITICAL();
return 0;
}

Expand Down Expand Up @@ -131,8 +139,10 @@ void pool_free(void* ptr)
{
uint8_t block_index, num_blocks, i;

RAM_ALLOC_ENTER_CRITICAL();
if (ptr == 0)
{
RAM_ALLOC_EXIT_CRITICAL();
return;
}

Expand All @@ -144,6 +154,7 @@ void pool_free(void* ptr)
ram_pool.blocks[block_index + i] = 0;
}
ram_pool.blocks_used -= num_blocks;
RAM_ALLOC_EXIT_CRITICAL();
}

uint8_t pool_stats_free() { return ram_pool.pool_size - ram_pool.blocks_used; }
Expand Down
9 changes: 9 additions & 0 deletions src/wch-ch56x-lib/memory/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ limitations under the License.
#ifndef _ALLOC_H_
#define _ALLOC_H_

#include "wch-ch56x-lib/utils/critical_section.h"
#include <stdint.h>

/**
Expand All @@ -32,6 +33,14 @@ You must pass the following defines to your compiler
extern "C" {
#endif

#ifndef RAM_ALLOC_ENTER_CRITICAL
#define RAM_ALLOC_ENTER_CRITICAL() BSP_ENTER_CRITICAL()
#endif

#ifndef RAM_ALLOC_EXIT_CRITICAL
#define RAM_ALLOC_EXIT_CRITICAL() BSP_EXIT_CRITICAL()
#endif

typedef struct pool
{
uint8_t* pool;
Expand Down
33 changes: 18 additions & 15 deletions src/wch-ch56x-lib/memory/fifo.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ limitations under the License.
#include <string.h>

#include "wch-ch56x-lib/logging/logging.h"
#include "wch-ch56x-lib/utils/critical_section.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -90,11 +91,13 @@ typedef struct hydra_fifo_t
.rd_idx = 0, \
.wr_idx = 0 }

/**
* @brief Set this pointer to disable/enable interrupts. Not necessary with
* single-producer/single-consumer.
*/
extern void (*hydra_fifo_disable_interrupt)(bool);
#ifndef HYDRA_FIFO_ENTER_CRITICAL
#define HYDRA_FIFO_ENTER_CRITICAL() BSP_ENTER_CRITICAL()
#endif

#ifndef HYDRA_FIFO_EXIT_CRITICAL
#define HYDRA_FIFO_EXIT_CRITICAL() BSP_EXIT_CRITICAL()
#endif

#define ABS(x) x < 0 ? -x : x
#define MIN(x, y) x < y ? x : y
Expand Down Expand Up @@ -262,14 +265,14 @@ _fifo_advance_write(hydra_fifo_t* fifo, uint16_t wr_idx, uint16_t offset)
__attribute__((always_inline)) static inline uint16_t
fifo_read(hydra_fifo_t* fifo, void* buffer)
{
hydra_fifo_disable_interrupt(true);
HYDRA_FIFO_ENTER_CRITICAL();
uint16_t rd_idx = fifo->rd_idx;
uint16_t wr_idx = fifo->wr_idx;
uint16_t count_read =
_fifo_read_n(fifo, buffer, _fifo_get_count(rd_idx, wr_idx, fifo->size),
rd_idx, wr_idx);
_fifo_advance_read(fifo, rd_idx, count_read);
hydra_fifo_disable_interrupt(false);
HYDRA_FIFO_EXIT_CRITICAL();
return count_read;
}

Expand All @@ -284,12 +287,12 @@ fifo_read(hydra_fifo_t* fifo, void* buffer)
__attribute__((always_inline)) static inline uint16_t
fifo_read_n(hydra_fifo_t* fifo, void* buffer, uint16_t n)
{
hydra_fifo_disable_interrupt(true);
HYDRA_FIFO_ENTER_CRITICAL();
uint16_t rd_idx = fifo->rd_idx;
uint16_t wr_idx = fifo->wr_idx;
uint16_t count_read = _fifo_read_n(fifo, buffer, n, rd_idx, wr_idx);
_fifo_advance_read(fifo, rd_idx, count_read);
hydra_fifo_disable_interrupt(false);
HYDRA_FIFO_EXIT_CRITICAL();
return count_read;
}

Expand All @@ -305,11 +308,11 @@ fifo_read_n(hydra_fifo_t* fifo, void* buffer, uint16_t n)
__attribute__((always_inline)) static inline uint16_t
fifo_peek_n(hydra_fifo_t* fifo, void* buffer, uint16_t n)
{
hydra_fifo_disable_interrupt(true);
HYDRA_FIFO_ENTER_CRITICAL();
uint16_t rd_idx = fifo->rd_idx;
uint16_t wr_idx = fifo->wr_idx;
uint16_t count_read = _fifo_read_n(fifo, buffer, n, rd_idx, wr_idx);
hydra_fifo_disable_interrupt(false);
HYDRA_FIFO_EXIT_CRITICAL();
return count_read;
}

Expand All @@ -324,12 +327,12 @@ fifo_peek_n(hydra_fifo_t* fifo, void* buffer, uint16_t n)
__attribute__((always_inline)) static inline uint16_t
fifo_write(hydra_fifo_t* fifo, void* buffer, uint16_t n)
{
hydra_fifo_disable_interrupt(true);
HYDRA_FIFO_ENTER_CRITICAL();
uint16_t rd_idx = fifo->rd_idx;
uint16_t wr_idx = fifo->wr_idx;
uint16_t count_written = _fifo_write_n(fifo, buffer, n, rd_idx, wr_idx);
_fifo_advance_write(fifo, wr_idx, count_written);
hydra_fifo_disable_interrupt(false);
HYDRA_FIFO_EXIT_CRITICAL();
return count_written;
}

Expand All @@ -351,11 +354,11 @@ fifo_count(hydra_fifo_t* fifo)
__attribute__((always_inline)) static inline void
fifo_clean(hydra_fifo_t* fifo)
{
hydra_fifo_disable_interrupt(true);
HYDRA_FIFO_ENTER_CRITICAL();
fifo->rd_idx = 0;
fifo->wr_idx = 0;
memset(fifo->buffer, 0, fifo->size * fifo->type_size);
hydra_fifo_disable_interrupt(false);
HYDRA_FIFO_EXIT_CRITICAL();
}

#ifdef __cplusplus
Expand Down
Loading

0 comments on commit 4e75bd7

Please sign in to comment.