diff --git a/Chapter07/error_handling/cstdlib_support/retarget.cpp b/Chapter07/error_handling/cstdlib_support/retarget.cpp index 60db0bf..f2dd14c 100644 --- a/Chapter07/error_handling/cstdlib_support/retarget.cpp +++ b/Chapter07/error_handling/cstdlib_support/retarget.cpp @@ -4,6 +4,8 @@ #include #include +#include + namespace { hal::uart *uart_stdio; @@ -26,7 +28,7 @@ extern "C" int _write(int fd, char * ptr, int len) { if(fd == STDOUT_FILENO || fd == STDERR_FILENO) { - uart_stdio->write_array(ptr, len); + uart_stdio->write(std::span(ptr, len)); } return len; diff --git a/Chapter07/error_handling/hal/uart/inc/uart.hpp b/Chapter07/error_handling/hal/uart/inc/uart.hpp index 534c3df..a7145dd 100644 --- a/Chapter07/error_handling/hal/uart/inc/uart.hpp +++ b/Chapter07/error_handling/hal/uart/inc/uart.hpp @@ -10,11 +10,6 @@ class uart { public: virtual void init(std::uint32_t baudrate) = 0; - virtual void write_array(const char * ptr, std::size_t len) = 0; - - virtual void write(std::span data) - { - write_array(data.data(), data.size()); - } + virtual void write(std::span data) = 0; }; }; // namespace hal diff --git a/Chapter07/error_handling/hal/uart/inc/uart_stm32.hpp b/Chapter07/error_handling/hal/uart/inc/uart_stm32.hpp index 0653582..f30c529 100644 --- a/Chapter07/error_handling/hal/uart/inc/uart_stm32.hpp +++ b/Chapter07/error_handling/hal/uart/inc/uart_stm32.hpp @@ -18,7 +18,7 @@ class uart_stm32 : public uart void init(std::uint32_t baudrate = c_baudrate_default) override; - void write_array(const char * ptr, std::size_t len) override; + void write(std::span data) override; private: UART_HandleTypeDef huart_; diff --git a/Chapter07/error_handling/hal/uart/src/uart_stm32.cpp b/Chapter07/error_handling/hal/uart/src/uart_stm32.cpp index 59633c6..50b0c7a 100644 --- a/Chapter07/error_handling/hal/uart/src/uart_stm32.cpp +++ b/Chapter07/error_handling/hal/uart/src/uart_stm32.cpp @@ -22,10 +22,10 @@ void hal::uart_stm32::init(std::uint32_t baudrate) HAL_UART_Init(&huart_); } -void hal::uart_stm32::write_array(const char * ptr, std::size_t len) +void hal::uart_stm32::write(std::span data) { // we must cast away costness due to ST HAL's API - char * data_ptr = const_cast(ptr); - HAL_UART_Transmit(&huart_, reinterpret_cast(data_ptr), len, + char * data_ptr = const_cast(data.data()); + HAL_UART_Transmit(&huart_, reinterpret_cast(data_ptr), data.size(), HAL_MAX_DELAY); } \ No newline at end of file diff --git a/Chapter09/type_safety/CMakeLists.txt b/Chapter09/type_safety/CMakeLists.txt index 1b10db1..f133d89 100644 --- a/Chapter09/type_safety/CMakeLists.txt +++ b/Chapter09/type_safety/CMakeLists.txt @@ -11,7 +11,7 @@ set(CMAKE_SIZE "arm-none-eabi-size") set(RENODE "renode" CACHE STRING "Path to Renode executable") set(MAIN_CPP_PATH "${CMAKE_SOURCE_DIR}/app/src/") -set(MAIN_CPP_FILE_NAME "main_integer_promotion.cpp" CACHE STRING "main file") +set(MAIN_CPP_FILE_NAME "main_usual_arithmetic_conversion.cpp" CACHE STRING "main file") list(APPEND LIB_SPECS "-specs=nosys.specs") list(APPEND LIB_SPECS "-specs=nano.specs") diff --git a/Chapter09/type_safety/cstdlib_support/retarget.cpp b/Chapter09/type_safety/cstdlib_support/retarget.cpp index 60db0bf..f2dd14c 100644 --- a/Chapter09/type_safety/cstdlib_support/retarget.cpp +++ b/Chapter09/type_safety/cstdlib_support/retarget.cpp @@ -4,6 +4,8 @@ #include #include +#include + namespace { hal::uart *uart_stdio; @@ -26,7 +28,7 @@ extern "C" int _write(int fd, char * ptr, int len) { if(fd == STDOUT_FILENO || fd == STDERR_FILENO) { - uart_stdio->write_array(ptr, len); + uart_stdio->write(std::span(ptr, len)); } return len; diff --git a/Chapter09/type_safety/hal/uart/inc/uart.hpp b/Chapter09/type_safety/hal/uart/inc/uart.hpp index 534c3df..a7145dd 100644 --- a/Chapter09/type_safety/hal/uart/inc/uart.hpp +++ b/Chapter09/type_safety/hal/uart/inc/uart.hpp @@ -10,11 +10,6 @@ class uart { public: virtual void init(std::uint32_t baudrate) = 0; - virtual void write_array(const char * ptr, std::size_t len) = 0; - - virtual void write(std::span data) - { - write_array(data.data(), data.size()); - } + virtual void write(std::span data) = 0; }; }; // namespace hal diff --git a/Chapter09/type_safety/hal/uart/inc/uart_stm32.hpp b/Chapter09/type_safety/hal/uart/inc/uart_stm32.hpp index 0653582..f30c529 100644 --- a/Chapter09/type_safety/hal/uart/inc/uart_stm32.hpp +++ b/Chapter09/type_safety/hal/uart/inc/uart_stm32.hpp @@ -18,7 +18,7 @@ class uart_stm32 : public uart void init(std::uint32_t baudrate = c_baudrate_default) override; - void write_array(const char * ptr, std::size_t len) override; + void write(std::span data) override; private: UART_HandleTypeDef huart_; diff --git a/Chapter09/type_safety/hal/uart/src/uart_stm32.cpp b/Chapter09/type_safety/hal/uart/src/uart_stm32.cpp index 59633c6..50b0c7a 100644 --- a/Chapter09/type_safety/hal/uart/src/uart_stm32.cpp +++ b/Chapter09/type_safety/hal/uart/src/uart_stm32.cpp @@ -22,10 +22,10 @@ void hal::uart_stm32::init(std::uint32_t baudrate) HAL_UART_Init(&huart_); } -void hal::uart_stm32::write_array(const char * ptr, std::size_t len) +void hal::uart_stm32::write(std::span data) { // we must cast away costness due to ST HAL's API - char * data_ptr = const_cast(ptr); - HAL_UART_Transmit(&huart_, reinterpret_cast(data_ptr), len, + char * data_ptr = const_cast(data.data()); + HAL_UART_Transmit(&huart_, reinterpret_cast(data_ptr), data.size(), HAL_MAX_DELAY); } \ No newline at end of file diff --git a/Chapter10/lambdas/cstdlib_support/retarget.cpp b/Chapter10/lambdas/cstdlib_support/retarget.cpp index 60db0bf..f2dd14c 100644 --- a/Chapter10/lambdas/cstdlib_support/retarget.cpp +++ b/Chapter10/lambdas/cstdlib_support/retarget.cpp @@ -4,6 +4,8 @@ #include #include +#include + namespace { hal::uart *uart_stdio; @@ -26,7 +28,7 @@ extern "C" int _write(int fd, char * ptr, int len) { if(fd == STDOUT_FILENO || fd == STDERR_FILENO) { - uart_stdio->write_array(ptr, len); + uart_stdio->write(std::span(ptr, len)); } return len; diff --git a/Chapter10/lambdas/hal/uart/inc/uart.hpp b/Chapter10/lambdas/hal/uart/inc/uart.hpp index 534c3df..a7145dd 100644 --- a/Chapter10/lambdas/hal/uart/inc/uart.hpp +++ b/Chapter10/lambdas/hal/uart/inc/uart.hpp @@ -10,11 +10,6 @@ class uart { public: virtual void init(std::uint32_t baudrate) = 0; - virtual void write_array(const char * ptr, std::size_t len) = 0; - - virtual void write(std::span data) - { - write_array(data.data(), data.size()); - } + virtual void write(std::span data) = 0; }; }; // namespace hal diff --git a/Chapter10/lambdas/hal/uart/inc/uart_stm32.hpp b/Chapter10/lambdas/hal/uart/inc/uart_stm32.hpp index 0653582..f30c529 100644 --- a/Chapter10/lambdas/hal/uart/inc/uart_stm32.hpp +++ b/Chapter10/lambdas/hal/uart/inc/uart_stm32.hpp @@ -18,7 +18,7 @@ class uart_stm32 : public uart void init(std::uint32_t baudrate = c_baudrate_default) override; - void write_array(const char * ptr, std::size_t len) override; + void write(std::span data) override; private: UART_HandleTypeDef huart_; diff --git a/Chapter10/lambdas/hal/uart/src/uart_stm32.cpp b/Chapter10/lambdas/hal/uart/src/uart_stm32.cpp index 59633c6..50b0c7a 100644 --- a/Chapter10/lambdas/hal/uart/src/uart_stm32.cpp +++ b/Chapter10/lambdas/hal/uart/src/uart_stm32.cpp @@ -22,10 +22,10 @@ void hal::uart_stm32::init(std::uint32_t baudrate) HAL_UART_Init(&huart_); } -void hal::uart_stm32::write_array(const char * ptr, std::size_t len) +void hal::uart_stm32::write(std::span data) { // we must cast away costness due to ST HAL's API - char * data_ptr = const_cast(ptr); - HAL_UART_Transmit(&huart_, reinterpret_cast(data_ptr), len, + char * data_ptr = const_cast(data.data()); + HAL_UART_Transmit(&huart_, reinterpret_cast(data_ptr), data.size(), HAL_MAX_DELAY); } \ No newline at end of file diff --git a/Chapter11/compile_time/cstdlib_support/retarget.cpp b/Chapter11/compile_time/cstdlib_support/retarget.cpp index 60db0bf..f2dd14c 100644 --- a/Chapter11/compile_time/cstdlib_support/retarget.cpp +++ b/Chapter11/compile_time/cstdlib_support/retarget.cpp @@ -4,6 +4,8 @@ #include #include +#include + namespace { hal::uart *uart_stdio; @@ -26,7 +28,7 @@ extern "C" int _write(int fd, char * ptr, int len) { if(fd == STDOUT_FILENO || fd == STDERR_FILENO) { - uart_stdio->write_array(ptr, len); + uart_stdio->write(std::span(ptr, len)); } return len; diff --git a/Chapter11/compile_time/hal/uart/inc/uart.hpp b/Chapter11/compile_time/hal/uart/inc/uart.hpp index 534c3df..a7145dd 100644 --- a/Chapter11/compile_time/hal/uart/inc/uart.hpp +++ b/Chapter11/compile_time/hal/uart/inc/uart.hpp @@ -10,11 +10,6 @@ class uart { public: virtual void init(std::uint32_t baudrate) = 0; - virtual void write_array(const char * ptr, std::size_t len) = 0; - - virtual void write(std::span data) - { - write_array(data.data(), data.size()); - } + virtual void write(std::span data) = 0; }; }; // namespace hal diff --git a/Chapter11/compile_time/hal/uart/inc/uart_stm32.hpp b/Chapter11/compile_time/hal/uart/inc/uart_stm32.hpp index 0653582..f30c529 100644 --- a/Chapter11/compile_time/hal/uart/inc/uart_stm32.hpp +++ b/Chapter11/compile_time/hal/uart/inc/uart_stm32.hpp @@ -18,7 +18,7 @@ class uart_stm32 : public uart void init(std::uint32_t baudrate = c_baudrate_default) override; - void write_array(const char * ptr, std::size_t len) override; + void write(std::span data) override; private: UART_HandleTypeDef huart_; diff --git a/Chapter11/compile_time/hal/uart/src/uart_stm32.cpp b/Chapter11/compile_time/hal/uart/src/uart_stm32.cpp index 59633c6..50b0c7a 100644 --- a/Chapter11/compile_time/hal/uart/src/uart_stm32.cpp +++ b/Chapter11/compile_time/hal/uart/src/uart_stm32.cpp @@ -22,10 +22,10 @@ void hal::uart_stm32::init(std::uint32_t baudrate) HAL_UART_Init(&huart_); } -void hal::uart_stm32::write_array(const char * ptr, std::size_t len) +void hal::uart_stm32::write(std::span data) { // we must cast away costness due to ST HAL's API - char * data_ptr = const_cast(ptr); - HAL_UART_Transmit(&huart_, reinterpret_cast(data_ptr), len, + char * data_ptr = const_cast(data.data()); + HAL_UART_Transmit(&huart_, reinterpret_cast(data_ptr), data.size(), HAL_MAX_DELAY); } \ No newline at end of file diff --git a/Chapter12/cpp_hal/app/src/main_basic_reg.cpp b/Chapter12/cpp_hal/app/src/main_basic_reg.cpp index aac2808..95be8e7 100644 --- a/Chapter12/cpp_hal/app/src/main_basic_reg.cpp +++ b/Chapter12/cpp_hal/app/src/main_basic_reg.cpp @@ -51,8 +51,6 @@ int main() // RCC->CR = 0xDEADBEEF; rcc::write(0xDEADBEEF); - auto val = rcc::read(); - printf("val = %ld\r\n", val); print_reg(&(RCC->CR)); diff --git a/Chapter12/cpp_hal/cstdlib_support/retarget.cpp b/Chapter12/cpp_hal/cstdlib_support/retarget.cpp index 60db0bf..f2dd14c 100644 --- a/Chapter12/cpp_hal/cstdlib_support/retarget.cpp +++ b/Chapter12/cpp_hal/cstdlib_support/retarget.cpp @@ -4,6 +4,8 @@ #include #include +#include + namespace { hal::uart *uart_stdio; @@ -26,7 +28,7 @@ extern "C" int _write(int fd, char * ptr, int len) { if(fd == STDOUT_FILENO || fd == STDERR_FILENO) { - uart_stdio->write_array(ptr, len); + uart_stdio->write(std::span(ptr, len)); } return len; diff --git a/Chapter12/cpp_hal/hal/uart/inc/uart.hpp b/Chapter12/cpp_hal/hal/uart/inc/uart.hpp index 534c3df..a7145dd 100644 --- a/Chapter12/cpp_hal/hal/uart/inc/uart.hpp +++ b/Chapter12/cpp_hal/hal/uart/inc/uart.hpp @@ -10,11 +10,6 @@ class uart { public: virtual void init(std::uint32_t baudrate) = 0; - virtual void write_array(const char * ptr, std::size_t len) = 0; - - virtual void write(std::span data) - { - write_array(data.data(), data.size()); - } + virtual void write(std::span data) = 0; }; }; // namespace hal diff --git a/Chapter12/cpp_hal/hal/uart/inc/uart_stm32.hpp b/Chapter12/cpp_hal/hal/uart/inc/uart_stm32.hpp index 0653582..f30c529 100644 --- a/Chapter12/cpp_hal/hal/uart/inc/uart_stm32.hpp +++ b/Chapter12/cpp_hal/hal/uart/inc/uart_stm32.hpp @@ -18,7 +18,7 @@ class uart_stm32 : public uart void init(std::uint32_t baudrate = c_baudrate_default) override; - void write_array(const char * ptr, std::size_t len) override; + void write(std::span data) override; private: UART_HandleTypeDef huart_; diff --git a/Chapter12/cpp_hal/hal/uart/src/uart_stm32.cpp b/Chapter12/cpp_hal/hal/uart/src/uart_stm32.cpp index 59633c6..50b0c7a 100644 --- a/Chapter12/cpp_hal/hal/uart/src/uart_stm32.cpp +++ b/Chapter12/cpp_hal/hal/uart/src/uart_stm32.cpp @@ -22,10 +22,10 @@ void hal::uart_stm32::init(std::uint32_t baudrate) HAL_UART_Init(&huart_); } -void hal::uart_stm32::write_array(const char * ptr, std::size_t len) +void hal::uart_stm32::write(std::span data) { // we must cast away costness due to ST HAL's API - char * data_ptr = const_cast(ptr); - HAL_UART_Transmit(&huart_, reinterpret_cast(data_ptr), len, + char * data_ptr = const_cast(data.data()); + HAL_UART_Transmit(&huart_, reinterpret_cast(data_ptr), data.size(), HAL_MAX_DELAY); } \ No newline at end of file