Skip to content

Commit

Permalink
Merge pull request #32 from xrobot-org/dev
Browse files Browse the repository at this point in the history
自定义printf
  • Loading branch information
Jiu-xiao authored Aug 23, 2023
2 parents 721a4ba + 76c641a commit b4b6f01
Show file tree
Hide file tree
Showing 26 changed files with 279 additions and 66 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@
[submodule "lib/MiniFlashDB"]
path = lib/MiniFlashDB
url = https://github.com/Jiu-xiao/MiniFlashDB.git
[submodule "lib/printf"]
path = lib/printf
url = https://github.com/xrobot-org/printf.git
2 changes: 1 addition & 1 deletion hw/bsp/Webots/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ int main() {
robot_init();
while (1) {
wb_robot_step(1);
poll(NULL, 0, 2);
poll(NULL, 0, 1);
}
}
47 changes: 44 additions & 3 deletions hw/bsp/atom/drivers/bsp_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,28 @@ bsp_status_t bsp_spi_register_callback(bsp_spi_t spi, bsp_spi_callback_t type,
return BSP_OK;
}

bsp_status_t bsp_spi_transmit(bsp_spi_t spi, uint8_t *data, size_t size,
bsp_status_t bsp_spi_transmit_receive(bsp_spi_t spi, uint8_t *recv_data,
const uint8_t *trans_data, size_t size,
bool block) {
if (block) {
return HAL_SPI_TransmitReceive(bsp_spi_get_handle(spi),
(uint8_t *)trans_data, recv_data, size,
10) != HAL_OK;
} else {
return HAL_SPI_TransmitReceive_DMA(bsp_spi_get_handle(spi),
(uint8_t *)trans_data, recv_data,
size) != HAL_OK;
}
}

bsp_status_t bsp_spi_transmit(bsp_spi_t spi, const uint8_t *data, size_t size,
bool block) {
if (block) {
return HAL_SPI_Transmit(bsp_spi_get_handle(spi), data, size, 10) != HAL_OK;
return HAL_SPI_Transmit(bsp_spi_get_handle(spi), (uint8_t *)data, size,
10) != HAL_OK;
} else {
return HAL_SPI_Transmit_DMA(bsp_spi_get_handle(spi), data, size) != HAL_OK;
return HAL_SPI_Transmit_DMA(bsp_spi_get_handle(spi), (uint8_t *)data,
size) != HAL_OK;
}
}

Expand All @@ -80,3 +96,28 @@ bsp_status_t bsp_spi_receive(bsp_spi_t spi, uint8_t *buff, size_t size,
return HAL_SPI_Receive_DMA(bsp_spi_get_handle(spi), buff, size) != HAL_OK;
}
}

uint8_t bsp_spi_mem_read_byte(bsp_spi_t spi, uint8_t reg) {
uint8_t tmp[2] = {reg | 0x80, 0x00};
bsp_spi_transmit_receive(spi, tmp, tmp, 2u, true);
return tmp[1];
}

bsp_status_t bsp_spi_mem_write_byte(bsp_spi_t spi, uint8_t reg, uint8_t data) {
uint8_t tmp[2] = {reg & 0x7f, data};
return bsp_spi_transmit(spi, tmp, 2u, true);
}

bsp_status_t bsp_spi_mem_read(bsp_spi_t spi, uint8_t reg, uint8_t *buff,
size_t size, bool block) {
reg = reg | 0x80;
bsp_spi_transmit(spi, &reg, 1u, true);
return bsp_spi_receive(spi, buff, size, block);
}

bsp_status_t bsp_spi_mem_write(bsp_spi_t spi, uint8_t reg, const uint8_t *buff,
size_t size, bool block) {
reg = reg & 0x7f;
bsp_spi_transmit(spi, &reg, 1u, true);
return bsp_spi_transmit(spi, buff, size, block);
}
11 changes: 10 additions & 1 deletion hw/bsp/atom/drivers/bsp_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@ typedef enum {
bsp_status_t bsp_spi_register_callback(bsp_spi_t spi, bsp_spi_callback_t type,
void (*callback)(void *),
void *callback_arg);
bsp_status_t bsp_spi_transmit(bsp_spi_t spi, uint8_t *data, size_t size,
bsp_status_t bsp_spi_transmit_receive(bsp_spi_t spi, uint8_t *recv_data,
const uint8_t *trans_data, size_t size,
bool block);
bsp_status_t bsp_spi_transmit(bsp_spi_t spi, const uint8_t *data, size_t size,
bool block);
bsp_status_t bsp_spi_receive(bsp_spi_t spi, uint8_t *buff, size_t size,
bool block);
uint8_t bsp_spi_mem_read_byte(bsp_spi_t spi, uint8_t reg);
bsp_status_t bsp_spi_mem_write_byte(bsp_spi_t spi, uint8_t reg, uint8_t data);
bsp_status_t bsp_spi_mem_read(bsp_spi_t spi, uint8_t reg, uint8_t *buff,
size_t size, bool block);
bsp_status_t bsp_spi_mem_write(bsp_spi_t spi, uint8_t reg, const uint8_t *buff,
size_t size, bool block);

#ifdef __cplusplus
}
Expand Down
6 changes: 1 addition & 5 deletions hw/bsp/atom/drivers/bsp_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ static void uart_tx_cb(void *arg) {
}

bsp_status_t bsp_usb_transmit(const uint8_t *buffer, uint32_t len) {
xSemaphoreTake(sem, UINT32_MAX);

bsp_uart_transmit(BSP_UART_MCU, (uint8_t *)(buffer), len, false);

xSemaphoreTake(sem, 10);
return BSP_OK;
}

Expand Down Expand Up @@ -82,8 +80,6 @@ void bsp_usb_init() {
bsp_uart_receive(BSP_UART_MCU, usb_rx_buff, sizeof(usb_rx_buff), false);

sem = xSemaphoreCreateBinary();

xSemaphoreGive(sem);
}

void bsp_usb_update() {}
47 changes: 44 additions & 3 deletions hw/bsp/c-mini/drivers/bsp_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,28 @@ bsp_status_t bsp_spi_register_callback(bsp_spi_t spi, bsp_spi_callback_t type,
return BSP_OK;
}

bsp_status_t bsp_spi_transmit(bsp_spi_t spi, uint8_t *data, size_t size,
bsp_status_t bsp_spi_transmit_receive(bsp_spi_t spi, uint8_t *recv_data,
const uint8_t *trans_data, size_t size,
bool block) {
if (block) {
return HAL_SPI_TransmitReceive(bsp_spi_get_handle(spi),
(uint8_t *)trans_data, recv_data, size,
10) != HAL_OK;
} else {
return HAL_SPI_TransmitReceive_DMA(bsp_spi_get_handle(spi),
(uint8_t *)trans_data, recv_data,
size) != HAL_OK;
}
}

bsp_status_t bsp_spi_transmit(bsp_spi_t spi, const uint8_t *data, size_t size,
bool block) {
if (block) {
return HAL_SPI_Transmit(bsp_spi_get_handle(spi), data, size, 10) != HAL_OK;
return HAL_SPI_Transmit(bsp_spi_get_handle(spi), (uint8_t *)data, size,
10) != HAL_OK;
} else {
return HAL_SPI_Transmit_DMA(bsp_spi_get_handle(spi), data, size) != HAL_OK;
return HAL_SPI_Transmit_DMA(bsp_spi_get_handle(spi), (uint8_t *)data,
size) != HAL_OK;
}
}

Expand All @@ -80,3 +96,28 @@ bsp_status_t bsp_spi_receive(bsp_spi_t spi, uint8_t *buff, size_t size,
return HAL_SPI_Receive_DMA(bsp_spi_get_handle(spi), buff, size) != HAL_OK;
}
}

uint8_t bsp_spi_mem_read_byte(bsp_spi_t spi, uint8_t reg) {
uint8_t tmp[2] = {reg | 0x80, 0x00};
bsp_spi_transmit_receive(spi, tmp, tmp, 2u, true);
return tmp[1];
}

bsp_status_t bsp_spi_mem_write_byte(bsp_spi_t spi, uint8_t reg, uint8_t data) {
uint8_t tmp[2] = {reg & 0x7f, data};
return bsp_spi_transmit(spi, tmp, 2u, true);
}

bsp_status_t bsp_spi_mem_read(bsp_spi_t spi, uint8_t reg, uint8_t *buff,
size_t size, bool block) {
reg = reg | 0x80;
bsp_spi_transmit(spi, &reg, 1u, true);
return bsp_spi_receive(spi, buff, size, block);
}

bsp_status_t bsp_spi_mem_write(bsp_spi_t spi, uint8_t reg, const uint8_t *buff,
size_t size, bool block) {
reg = reg & 0x7f;
bsp_spi_transmit(spi, &reg, 1u, true);
return bsp_spi_transmit(spi, buff, size, block);
}
11 changes: 10 additions & 1 deletion hw/bsp/c-mini/drivers/bsp_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,19 @@ typedef enum {
bsp_status_t bsp_spi_register_callback(bsp_spi_t spi, bsp_spi_callback_t type,
void (*callback)(void *),
void *callback_arg);
bsp_status_t bsp_spi_transmit(bsp_spi_t spi, uint8_t *data, size_t size,
bsp_status_t bsp_spi_transmit_receive(bsp_spi_t spi, uint8_t *recv_data,
const uint8_t *trans_data, size_t size,
bool block);
bsp_status_t bsp_spi_transmit(bsp_spi_t spi, const uint8_t *data, size_t size,
bool block);
bsp_status_t bsp_spi_receive(bsp_spi_t spi, uint8_t *buff, size_t size,
bool block);
uint8_t bsp_spi_mem_read_byte(bsp_spi_t spi, uint8_t reg);
bsp_status_t bsp_spi_mem_write_byte(bsp_spi_t spi, uint8_t reg, uint8_t data);
bsp_status_t bsp_spi_mem_read(bsp_spi_t spi, uint8_t reg, uint8_t *buff,
size_t size, bool block);
bsp_status_t bsp_spi_mem_write(bsp_spi_t spi, uint8_t reg, const uint8_t *buff,
size_t size, bool block);

#ifdef __cplusplus
}
Expand Down
47 changes: 44 additions & 3 deletions hw/bsp/node_imu/drivers/bsp_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,28 @@ bsp_status_t bsp_spi_register_callback(bsp_spi_t spi, bsp_spi_callback_t type,
return BSP_OK;
}

bsp_status_t bsp_spi_transmit(bsp_spi_t spi, uint8_t *data, size_t size,
bsp_status_t bsp_spi_transmit_receive(bsp_spi_t spi, uint8_t *recv_data,
const uint8_t *trans_data, size_t size,
bool block) {
if (block) {
return HAL_SPI_TransmitReceive(bsp_spi_get_handle(spi),
(uint8_t *)trans_data, recv_data, size,
10) != HAL_OK;
} else {
return HAL_SPI_TransmitReceive_DMA(bsp_spi_get_handle(spi),
(uint8_t *)trans_data, recv_data,
size) != HAL_OK;
}
}

bsp_status_t bsp_spi_transmit(bsp_spi_t spi, const uint8_t *data, size_t size,
bool block) {
if (block) {
return HAL_SPI_Transmit(bsp_spi_get_handle(spi), data, size, 10) != HAL_OK;
return HAL_SPI_Transmit(bsp_spi_get_handle(spi), (uint8_t *)data, size,
10) != HAL_OK;
} else {
return HAL_SPI_Transmit_DMA(bsp_spi_get_handle(spi), data, size) != HAL_OK;
return HAL_SPI_Transmit_DMA(bsp_spi_get_handle(spi), (uint8_t *)data,
size) != HAL_OK;
}
}

Expand All @@ -80,3 +96,28 @@ bsp_status_t bsp_spi_receive(bsp_spi_t spi, uint8_t *buff, size_t size,
return HAL_SPI_Receive_DMA(bsp_spi_get_handle(spi), buff, size) != HAL_OK;
}
}

uint8_t bsp_spi_mem_read_byte(bsp_spi_t spi, uint8_t reg) {
uint8_t tmp[2] = {reg | 0x80, 0x00};
bsp_spi_transmit_receive(spi, tmp, tmp, 2u, true);
return tmp[1];
}

bsp_status_t bsp_spi_mem_write_byte(bsp_spi_t spi, uint8_t reg, uint8_t data) {
uint8_t tmp[2] = {reg & 0x7f, data};
return bsp_spi_transmit(spi, tmp, 2u, true);
}

bsp_status_t bsp_spi_mem_read(bsp_spi_t spi, uint8_t reg, uint8_t *buff,
size_t size, bool block) {
reg = reg | 0x80;
bsp_spi_transmit(spi, &reg, 1u, true);
return bsp_spi_receive(spi, buff, size, block);
}

bsp_status_t bsp_spi_mem_write(bsp_spi_t spi, uint8_t reg, const uint8_t *buff,
size_t size, bool block) {
reg = reg & 0x7f;
bsp_spi_transmit(spi, &reg, 1u, true);
return bsp_spi_transmit(spi, buff, size, block);
}
11 changes: 10 additions & 1 deletion hw/bsp/node_imu/drivers/bsp_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@ typedef enum {
bsp_status_t bsp_spi_register_callback(bsp_spi_t spi, bsp_spi_callback_t type,
void (*callback)(void *),
void *callback_arg);
bsp_status_t bsp_spi_transmit(bsp_spi_t spi, uint8_t *data, size_t size,
bsp_status_t bsp_spi_transmit_receive(bsp_spi_t spi, uint8_t *recv_data,
const uint8_t *trans_data, size_t size,
bool block);
bsp_status_t bsp_spi_transmit(bsp_spi_t spi, const uint8_t *data, size_t size,
bool block);
bsp_status_t bsp_spi_receive(bsp_spi_t spi, uint8_t *buff, size_t size,
bool block);
uint8_t bsp_spi_mem_read_byte(bsp_spi_t spi, uint8_t reg);
bsp_status_t bsp_spi_mem_write_byte(bsp_spi_t spi, uint8_t reg, uint8_t data);
bsp_status_t bsp_spi_mem_read(bsp_spi_t spi, uint8_t reg, uint8_t *buff,
size_t size, bool block);
bsp_status_t bsp_spi_mem_write(bsp_spi_t spi, uint8_t reg, const uint8_t *buff,
size_t size, bool block);

#ifdef __cplusplus
}
Expand Down
47 changes: 44 additions & 3 deletions hw/bsp/rm-c/drivers/bsp_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,28 @@ bsp_status_t bsp_spi_register_callback(bsp_spi_t spi, bsp_spi_callback_t type,
return BSP_OK;
}

bsp_status_t bsp_spi_transmit(bsp_spi_t spi, uint8_t *data, size_t size,
bsp_status_t bsp_spi_transmit_receive(bsp_spi_t spi, uint8_t *recv_data,
const uint8_t *trans_data, size_t size,
bool block) {
if (block) {
return HAL_SPI_TransmitReceive(bsp_spi_get_handle(spi),
(uint8_t *)trans_data, recv_data, size,
10) != HAL_OK;
} else {
return HAL_SPI_TransmitReceive_DMA(bsp_spi_get_handle(spi),
(uint8_t *)trans_data, recv_data,
size) != HAL_OK;
}
}

bsp_status_t bsp_spi_transmit(bsp_spi_t spi, const uint8_t *data, size_t size,
bool block) {
if (block) {
return HAL_SPI_Transmit(bsp_spi_get_handle(spi), data, size, 10) != HAL_OK;
return HAL_SPI_Transmit(bsp_spi_get_handle(spi), (uint8_t *)data, size,
10) != HAL_OK;
} else {
return HAL_SPI_Transmit_DMA(bsp_spi_get_handle(spi), data, size) != HAL_OK;
return HAL_SPI_Transmit_DMA(bsp_spi_get_handle(spi), (uint8_t *)data,
size) != HAL_OK;
}
}

Expand All @@ -81,3 +97,28 @@ bsp_status_t bsp_spi_receive(bsp_spi_t spi, uint8_t *buff, size_t size,
return HAL_SPI_Receive_DMA(bsp_spi_get_handle(spi), buff, size) != HAL_OK;
}
}

uint8_t bsp_spi_mem_read_byte(bsp_spi_t spi, uint8_t reg) {
uint8_t tmp[2] = {reg | 0x80, 0x00};
bsp_spi_transmit_receive(spi, tmp, tmp, 2u, true);
return tmp[1];
}

bsp_status_t bsp_spi_mem_write_byte(bsp_spi_t spi, uint8_t reg, uint8_t data) {
uint8_t tmp[2] = {reg & 0x7f, data};
return bsp_spi_transmit(spi, tmp, 2u, true);
}

bsp_status_t bsp_spi_mem_read(bsp_spi_t spi, uint8_t reg, uint8_t *buff,
size_t size, bool block) {
reg = reg | 0x80;
bsp_spi_transmit(spi, &reg, 1u, true);
return bsp_spi_receive(spi, buff, size, block);
}

bsp_status_t bsp_spi_mem_write(bsp_spi_t spi, uint8_t reg, const uint8_t *buff,
size_t size, bool block) {
reg = reg & 0x7f;
bsp_spi_transmit(spi, &reg, 1u, true);
return bsp_spi_transmit(spi, buff, size, block);
}
11 changes: 10 additions & 1 deletion hw/bsp/rm-c/drivers/bsp_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,19 @@ typedef enum {
bsp_status_t bsp_spi_register_callback(bsp_spi_t spi, bsp_spi_callback_t type,
void (*callback)(void *),
void *callback_arg);
bsp_status_t bsp_spi_transmit(bsp_spi_t spi, uint8_t *data, size_t size,
bsp_status_t bsp_spi_transmit_receive(bsp_spi_t spi, uint8_t *recv_data,
const uint8_t *trans_data, size_t size,
bool block);
bsp_status_t bsp_spi_transmit(bsp_spi_t spi, const uint8_t *data, size_t size,
bool block);
bsp_status_t bsp_spi_receive(bsp_spi_t spi, uint8_t *buff, size_t size,
bool block);
uint8_t bsp_spi_mem_read_byte(bsp_spi_t spi, uint8_t reg);
bsp_status_t bsp_spi_mem_write_byte(bsp_spi_t spi, uint8_t reg, uint8_t data);
bsp_status_t bsp_spi_mem_read(bsp_spi_t spi, uint8_t reg, uint8_t *buff,
size_t size, bool block);
bsp_status_t bsp_spi_mem_write(bsp_spi_t spi, uint8_t reg, const uint8_t *buff,
size_t size, bool block);

#ifdef __cplusplus
}
Expand Down
17 changes: 17 additions & 0 deletions hw/mcu/default/custom_printf.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
set(SUPPORT_EXPONENTIAL_SPECIFIERS 0)
set(MSVC_STYLE_INTEGER_SPECIFIERS 0)
set(SUPPORT_WRITEBACK_SPECIFIER 0)
set(BUILD_STATIC_LIBRARY 1)

add_subdirectory(${LIB_DIR}/printf printf.o)

add_compile_options(
-fno-builtin-printf
-fno-builtin-sprintf
-fno-builtin-vsprintf
-fno-builtin-snprintf
-fno-builtin-vsnprintf
-fno-builtin-vprintf
)

target_compile_options(printf PRIVATE -w)
4 changes: 3 additions & 1 deletion hw/mcu/gd/cmake/mcu_gd32f4.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
include(${MCU_DIR}/default/custom_printf.cmake)

set(CPU_FLAGS "-mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb")

set(GENERAL_FLAGS
"-Wall -Wextra -Wstack-usage=1024 --specs=nano.specs --specs=nosys.specs -u _printf_float -fno-builtin -ffunction-sections -fdata-sections"
"-Wall -Wextra -Wstack-usage=1024 --specs=nano.specs --specs=nosys.specs -fno-builtin -ffunction-sections -fdata-sections"
)

set(CMAKE_C_FLAGS
Expand Down
Loading

0 comments on commit b4b6f01

Please sign in to comment.