From b2a92ea87e0e198f6dc5d26db74263f9c5016892 Mon Sep 17 00:00:00 2001 From: dvdb97 Date: Tue, 9 May 2023 18:17:57 +0100 Subject: [PATCH 01/22] Added hal.hpp for raspi --- include/sta/raspi/hal.hpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 include/sta/raspi/hal.hpp diff --git a/include/sta/raspi/hal.hpp b/include/sta/raspi/hal.hpp new file mode 100644 index 0000000..900715e --- /dev/null +++ b/include/sta/raspi/hal.hpp @@ -0,0 +1,13 @@ +#ifndef STA_CORE_RASPI_HAL_HPP +#define STA_CORE_RASPI_HAL_HPP + +namespace sta { + +B +A +A +D +D +} + +#endif //STA_CORE_RASPI_HAL_HPP From d2806f1651a87584dbc7f05460457d07ebd1d3c5 Mon Sep 17 00:00:00 2001 From: dvdb97 Date: Tue, 9 May 2023 21:24:55 +0100 Subject: [PATCH 02/22] Added gpio pin implementation for raspi --- include/sta/gpio_pin.hpp | 4 ++-- include/sta/raspi/gpio_pin.hpp | 42 ++++++++++++++++++++++++++++++++++ include/sta/raspi/hal.hpp | 11 +++------ include/sta/raspi/spi.hpp | 19 +++++++++++++++ src/raspi/gpio_pin.cpp | 21 +++++++++++++++++ 5 files changed, 87 insertions(+), 10 deletions(-) create mode 100644 include/sta/raspi/gpio_pin.hpp create mode 100644 include/sta/raspi/spi.hpp create mode 100644 src/raspi/gpio_pin.cpp diff --git a/include/sta/gpio_pin.hpp b/include/sta/gpio_pin.hpp index 82ce7ec..c3ef231 100644 --- a/include/sta/gpio_pin.hpp +++ b/include/sta/gpio_pin.hpp @@ -21,8 +21,8 @@ namespace sta */ enum class GpioPinState { - LOW, - HIGH + GPIO_LOW, + GPIO_HIGH }; /** diff --git a/include/sta/raspi/gpio_pin.hpp b/include/sta/raspi/gpio_pin.hpp new file mode 100644 index 0000000..6230b1c --- /dev/null +++ b/include/sta/raspi/gpio_pin.hpp @@ -0,0 +1,42 @@ +#ifndef STA_CORE_RASPI_GPIO_PIN_HPP +#define STA_CORE_RASPI_GPIO_PIN_HPP + +// Only enable module on Raspi platform w/ HAL GPIO module enabled +#include +#ifdef STA_PLATFORM_RASPI +# include +# define STA_RASPI_GPIO_ENABLED +#endif // STA_PLATFORM_RASPI + +#if defined(STA_RASPI_GPIO_ENABLED) || defined(DOXYGEN) + +#include +#include + +namespace sta +{ + class RaspiGpioPin : GpioPin + { + public: + enum class GpioMode { + GPIO_OUTPUT, + GPIO_INPUT + }; + + /** + * @param pin Pin index + * @param mode The mode of the GPIO pin. Either INPUT or OUTPUT + */ + RaspiGpioPin(uint8_t pin, GpioMode mode); + + void setState(GpioPinState state) override; + + private: + uint8_t pin_; + GpioMode mode_; + }; +} // namespace sta + +#endif // STA_RASPI_GPIO_ENABLED + +#endif // STA_CORE_RASPI_GPIO_PIN_HPP \ No newline at end of file diff --git a/include/sta/raspi/hal.hpp b/include/sta/raspi/hal.hpp index 900715e..f3e008b 100644 --- a/include/sta/raspi/hal.hpp +++ b/include/sta/raspi/hal.hpp @@ -1,13 +1,8 @@ #ifndef STA_CORE_RASPI_HAL_HPP #define STA_CORE_RASPI_HAL_HPP -namespace sta { - -B -A -A -D -D -} +#include +#include +#include #endif //STA_CORE_RASPI_HAL_HPP diff --git a/include/sta/raspi/spi.hpp b/include/sta/raspi/spi.hpp new file mode 100644 index 0000000..9758b19 --- /dev/null +++ b/include/sta/raspi/spi.hpp @@ -0,0 +1,19 @@ +#ifndef STA_CORE_RASPI_SPI_HPP +#define STA_CORE_RASPI_SPI_HPP + +#include + +namespace sta +{ + class RaspiSPI : SPI + { + + }; + + class RaspiSPIDevice : SPIDevice + { + + }; +} // namespace sta + +#endif // STA_CORE_RASPI_HPP \ No newline at end of file diff --git a/src/raspi/gpio_pin.cpp b/src/raspi/gpio_pin.cpp new file mode 100644 index 0000000..a15fa43 --- /dev/null +++ b/src/raspi/gpio_pin.cpp @@ -0,0 +1,21 @@ +#include +#ifdef STA_RASPI_GPIO_ENABLED + +#include +#include + +namespace sta +{ + RaspiGpioPin::RaspiGpioPin(uint8_t pin, GpioMode mode) : pin_{pin}, mode_{mode} + { + pinMode(pin, mode == GpioMode::GPIO_INPUT ? INPUT : OUTPUT); + } + + void RaspiGpioPin::setState(GpioPinState state) + { + digitalWrite(pin_, state == GpioPinState::GPIO_LOW ? LOW : HIGH); + } +} // namespace sta + + +#endif // STA_ARDUINO_GPIO_ENABLED \ No newline at end of file From 9894fff86ef280e98551af7ab012ecbf7d8fddb7 Mon Sep 17 00:00:00 2001 From: dvdb97 Date: Tue, 16 May 2023 00:05:07 +0200 Subject: [PATCH 03/22] Added SPI implementation based on Linux library --- include/sta/raspi/gpio_pin.hpp | 8 ++ include/sta/raspi/hal.hpp | 2 +- include/sta/raspi/spi.hpp | 33 +++++- src/raspi/gpio_pin.cpp | 16 +++ src/raspi/spi.cpp | 188 +++++++++++++++++++++++++++++++++ 5 files changed, 245 insertions(+), 2 deletions(-) create mode 100644 src/raspi/spi.cpp diff --git a/include/sta/raspi/gpio_pin.hpp b/include/sta/raspi/gpio_pin.hpp index 6230b1c..64d5ee6 100644 --- a/include/sta/raspi/gpio_pin.hpp +++ b/include/sta/raspi/gpio_pin.hpp @@ -31,10 +31,18 @@ namespace sta void setState(GpioPinState state) override; + static RaspiGpioPin DUMMY_GPIO; + private: uint8_t pin_; GpioMode mode_; }; + + class DummyGpioPin : RaspiGpioPin { + DummyGpioPin(); + + void setState(GpioPinState state) override; + } } // namespace sta #endif // STA_RASPI_GPIO_ENABLED diff --git a/include/sta/raspi/hal.hpp b/include/sta/raspi/hal.hpp index f3e008b..5b6173a 100644 --- a/include/sta/raspi/hal.hpp +++ b/include/sta/raspi/hal.hpp @@ -2,7 +2,7 @@ #define STA_CORE_RASPI_HAL_HPP #include -#include #include +#include #endif //STA_CORE_RASPI_HAL_HPP diff --git a/include/sta/raspi/spi.hpp b/include/sta/raspi/spi.hpp index 9758b19..cc8e741 100644 --- a/include/sta/raspi/spi.hpp +++ b/include/sta/raspi/spi.hpp @@ -1,19 +1,50 @@ #ifndef STA_CORE_RASPI_SPI_HPP #define STA_CORE_RASPI_SPI_HPP +#include +#ifdef STA_PLATFORM_RASPI + #include +#include namespace sta { - class RaspiSPI : SPI + class RaspiSPI : public SPI { + public: + RaspiSPI(const char * dev, const SPISettings & settings, Mutex * mutex = nullptr); + ~RaspiSPI(); + void transfer(uint8_t value) override; + void transfer16(uint16_t value) override; + void transfer(const uint8_t * buffer, size_t size) override; + void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) override; + void receive(uint8_t * buffer, size_t size) override; + + void fill(uint8_t value, size_t count) override; + + void acquire() override; + void release() override; + + private: + char * spidev_; + int spifd_; + bool open_; + + void update_setting(int setting, int value); }; class RaspiSPIDevice : SPIDevice { + public: + RaspiSPIDevice(RaspiSPI * intf); + void select() override; + + void deselect() override; }; } // namespace sta +#endif // STA_PLATFORM_RASPI + #endif // STA_CORE_RASPI_HPP \ No newline at end of file diff --git a/src/raspi/gpio_pin.cpp b/src/raspi/gpio_pin.cpp index a15fa43..01773d0 100644 --- a/src/raspi/gpio_pin.cpp +++ b/src/raspi/gpio_pin.cpp @@ -15,6 +15,22 @@ namespace sta { digitalWrite(pin_, state == GpioPinState::GPIO_LOW ? LOW : HIGH); } + + DummyGpioPin::DummyGpioPin() : RaspiGpioPin { -1 , GpioMode::GPIO_INPUT } + { + + } + + DummyGpioPin::setState(GpioPinState state) + { + // If this gets called, something is wrong! + STA_UNREACHABLE(); + } + + static DummyGpioPin dummyGpio; + + + RaspiGpioPin * RaspiGpioPin::DUMMY_GPIO = &dummyGpio; } // namespace sta diff --git a/src/raspi/spi.cpp b/src/raspi/spi.cpp new file mode 100644 index 0000000..135adb1 --- /dev/null +++ b/src/raspi/spi.cpp @@ -0,0 +1,188 @@ +#include +#ifdef STA_PLATFORM_RASPI + + +#include +#include +#include +#include + +#include +#include + +namespace sta +{ + + RaspiSPI::RaspiSPI(const char * dev, const SPISettings & settings, Mutex * mutex = nullptr) + : SPI(settings, mutex) + { + // Check validity of parameters. + STA_ASSERT(dev != nullptr); + STA_ASSERT(mutex != nullptr); + + open_ = false; + spidev_ = (char *)malloc(strlen(dev)+1); + + // Check if memory allocation was successful. + STA_ASSERT(spidev_ != nullptr); + + strcpy(spidev_ , dev); + } + + RaspiSPI::~RaspiSPI() + { + if (spidev_ != NULL ) { + free(spidev_); + spidev_ = NULL; + } + + if (open_) { + close(spifd_); + } + } + + void RaspiSPI::transfer(uint8_t value) + { + STA_ASSERT(open_); + + struct spi_ioc_transfer spi_message[1]; + memset(spi_message, 0, sizeof(spi_message)); + spi_message[0].tx_buf = (unsigned long)&value; + spi_message[0].len = 1; + + int result = ioctl(m_spifd, SPI_IOC_MESSAGE(1), spi_message); + + STA_ASSERT(result == 0); + } + + void RaspiSPI::transfer16(uint16_t value) + { + STA_ASSERT(open_); + + struct spi_ioc_transfer spi_message[1]; + memset(spi_message, 0, sizeof(spi_message)); + spi_message[0].tx_buf = (unsigned long)&value; + spi_message[0].len = 1; + + int result = ioctl(m_spifd, SPI_IOC_MESSAGE(1), spi_message); + + STA_ASSERT(result == 0); + } + + void RaspiSPI::transfer(const uint8_t * buffer, size_t size) + { + STA_ASSERT(open_); + STA_ASSERT(buffer != nullptr); + STA_ASSERT(size != 0); + + struct spi_ioc_transfer spi_message[1]; + memset(spi_message, 0, sizeof(spi_message)); + spi_message[0].tx_buf = (unsigned long)buffer; + spi_message[0].len = size; + + int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); + + STA_ASSERT(result == 0); + } + + void RaspiSPI::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) + { + STA_ASSERT(txBuffer != nullptr); + STA_ASSERT(rxBuffer != nullptr); + STA_ASSERT(size != 0); + + struct spi_ioc_transfer spi_message[1]; + memset(spi_message, 0, sizeof(spi_message)); + + spi_message[0].rx_buf = (unsigned long)rxBuffer; + spi_message[0].tx_buf = (unsigned long)txBuffer; + spi_message[0].len = size; + + int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); + + STA_ASSERT(result == 0); + } + + void RaspiSPI::receive(uint8_t * buffer, size_t size) + { + STA_ASSERT(buffer != nullptr); + + struct spi_ioc_transfer spi_message[1]; + memset(spi_message, 0, sizeof(spi_message)); + + spi_message[0].rx_buf = (unsigned long)buffer; + spi_message[0].len = size; + return ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); + } + + void RaspiSPI::fill(uint8_t value, size_t count) + { + STA_ASSERT(count != 0); + + uint8_t * buffer = new uint8_t[count]; + memset(buffer, value, count); + + struct spi_ioc_transfer spi_message[1]; + memset(spi_message, 0, sizeof(spi_message)); + spi_message[0].tx_buf = (unsigned long)buffer; + spi_message[0].len = count; + + int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); + + delete[] buffer; + } + + void RaspiSPI::update_setting(int setting, int value) + { + STA_ASSERT(open_); + + int result = ioctl(spifd_, setting, value); + STA_ASSERT(result >= 0); + } + + void RaspiSPI::acquire() + { + SPI::acquire(); + + STA_ASSERT(spidev_ != nullptr); + + /* open spidev device */ + if (open_ == true) + return; + + spifd_ = open(spidev_, O_RDWR); + STA_ASSERT(spifd_ < 0); + + open_ = true; + + update_setting(SPI_IOC_WR_MODE, &m_spiconfig.mode); + update_setting(SPI_IOC_RD_MODE, &m_spiconfig.mode); + update_setting(SPI_IOC_WR_BITS_PER_WORD, &m_spiconfig.bits_per_word); + update_setting(SPI_IOC_RD_BITS_PER_WORD, &m_spiconfig.bits_per_word); + uppdate_setting(SPI_IOC_WR_MAX_SPEED_HZ, &m_spiconfig.speed); + update_setting(SPI_IOC_RD_MAX_SPEED_HZ, &m_spiconfig.speed); + } + + void RaspiSPI::release() + { + SPI::release(); + } + + RaspiSPIDevice::RaspiSPIDevice(RaspiSPI * intf) : SPIDevice { intf, RaspiGpioPin::DUMMY_GPIO } + { + + } + + RaspiSPIDevice::select() + { + // do nothing since the raspi SPI API doesn't require GPIO interaction. + } + + RaspiSPIDevice::deselect() + { + // do nothing since the raspi SPI API doesn't require GPIO interaction. + } +} // namespace sta + + +#endif // STA_PlATFORM_RASPI From 21cb870014b9df2ea51e8759ef0d9f9e91728fb8 Mon Sep 17 00:00:00 2001 From: dvdb97 Date: Tue, 16 May 2023 08:31:39 +0200 Subject: [PATCH 04/22] first attempst to handle endianness --- src/raspi/spi.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/raspi/spi.cpp b/src/raspi/spi.cpp index 135adb1..fa214e2 100644 --- a/src/raspi/spi.cpp +++ b/src/raspi/spi.cpp @@ -10,6 +10,11 @@ #include #include + +#define SYSTEM_ENDIANNESS __cpp_lib_endian + +// TODO: Check out "cpu_to_leXX()" and "leXX()_to_cpu()" on Linux! + namespace sta { From 0bc924bba247a334ae363e10cb20a5bdefc5f159 Mon Sep 17 00:00:00 2001 From: Dario Date: Tue, 16 May 2023 14:58:09 +0100 Subject: [PATCH 05/22] Updated debug printing for raspi + added persistent_open setting for raspi spi --- include/sta/debug_printf.hpp | 49 ++++++++ include/sta/printable.hpp | 206 +++++++++++++++++++++++++++++++ include/sta/printable_uart.hpp | 14 --- include/sta/raspi/gpio_pin.hpp | 8 +- include/sta/raspi/hal.hpp | 2 +- include/sta/raspi/mcu/common.hpp | 13 ++ include/sta/raspi/spi.hpp | 16 ++- include/sta/spi/device.hpp | 2 +- src/assert.cpp | 8 +- src/printable.cpp | 117 ++++++++++++++++++ src/printable_uart.cpp | 11 -- src/raspi/gpio_pin.cpp | 6 +- src/raspi/spi.cpp | 164 +++++++++++++++++------- src/spi/device.cpp | 6 +- 14 files changed, 529 insertions(+), 93 deletions(-) create mode 100644 include/sta/debug_printf.hpp create mode 100644 include/sta/printable.hpp create mode 100644 include/sta/raspi/mcu/common.hpp create mode 100644 src/printable.cpp diff --git a/include/sta/debug_printf.hpp b/include/sta/debug_printf.hpp new file mode 100644 index 0000000..42f85a6 --- /dev/null +++ b/include/sta/debug_printf.hpp @@ -0,0 +1,49 @@ +#ifndef STA_CORE_DEBUG_PRINTF_HPP +#define STA_CORE_DEBUG_PRINTF_HPP + +#include + +// Determine if module should be enabled +// Condition 1: STA_DEBUG_PRINTF is defined +// Condition 2: +// STA_DEBUG_PRINTF_FORCE is defined +// or +// DEBUG is defined but not NDEBUG +#ifdef STA_DEBUG_PRINTF +# ifdef STA_DEBUG_PRINTF_FORCE +# define STA_DEBUG_SERIAL_ENABLED +# else // !STA_DEBUG_PRINTF_FORCE +# if defined(DEBUG) && !defined(NDEBUG) +# define STA_DEBUG_PRINTF_ENABLED +# endif // DEBUG && !NDEBUG +# endif // !STA_DEBUG_PRINTF_FORCE +#endif // STA_DEBUG_PRINTF + +#if defined(STA_DEBUG_PRINTF_ENABLED) || defined(DOXYGEN) + +#include + +/** + * @brief Print debug output. + * + * @param ... See @ref sta::PrintableUART::print + * + * @ingroup sta_core_debug + */ +# define STA_DEBUG_PRINT(...) sta::print(__VA_ARGS__) +/** + * @brief Print debug output followed by new-line to UART. + * + * @param ... See @ref sta::PrintableUART::println + * + * @ingroup sta_core_debug + */ +# define STA_DEBUG_PRINTLN(...) sta::println(__VA_ARGS__) + + +#else // !STA_DEBUG_PRINTF_ENABLED +# define STA_DEBUG_PRINT(...) ((void)0) +# define STA_DEBUG_PRINTLN(...) ((void)0) +#endif // !STA_DEBUG_PRINTF_ENABLED + +#endif // STA_CORE_DEBUG_PRINTF_HPP \ No newline at end of file diff --git a/include/sta/printable.hpp b/include/sta/printable.hpp new file mode 100644 index 0000000..e94f253 --- /dev/null +++ b/include/sta/printable.hpp @@ -0,0 +1,206 @@ +/** + * @file + * @brief Printable UART interface definition. + */ +#ifndef STA_CORE_PRINTABLE_HPP +#define STA_CORE_PRINTABLE_HPP + +#include + +#include +#include + + +namespace sta +{ + /** + * @brief Integer representation. + * + * @ingroup sta_core + */ + enum class IntegerBase + { + DEC, /**< Decimal */ + BIN, /**< Binary */ + HEX /**< Hexadecimal */ + }; + + /** + * @brief Print single character. + * + * @param c Character to print + */ + void print(char c); + + /** + * @brief Print boolean value. + * + * @param b Boolean value + */ + void print(bool b); + + /** + * @brief Print floating point value. + * + * @param d Floating point value + */ + void print(double d); + + /** + * @brief Print integer in selected base. + * + * @param num 8-bit unsigned integer + * @param base Integer base + */ + void print(uint8_t num, IntegerBase base = IntegerBase::DEC); + + /** + * @brief Print integer in selected base. + * + * @param num 16-bit unsigned integer + * @param base Integer base + */ + void print(uint16_t num, IntegerBase base = IntegerBase::DEC); + + /** + * @brief Print integer in selected base. + * + * @param num 32-bit unsigned integer + * @param base Integer base + */ + void print(uint32_t num, IntegerBase base = IntegerBase::DEC); + + /** + * @brief Print integer in selected base. + * + * @param num Integer + * @param base Integer base + */ + void print(size_t num, IntegerBase base = IntegerBase::DEC); + + /** + * @brief Print c-string. + * + * @param str Null terminated string + */ + void print(const char * str); + + /** + * @brief Print string. + * + * @param str String buffer + * @param length String length + */ + void print(const char * str, size_t length); + + /** + * @brief Print new-line. + */ + void println(); + + /** + * @brief Print single character followed by a new-line. + * + * @param c Character to print + */ + void println(char c); + + /** + * @brief Print boolean value followed by a new-line. + * + * @param b Boolean value + */ + void println(bool b); + + /** + * @brief Print floating point value followed by a new-line. + * + * @param d Floating point value + */ + void println(double d); + + /** + * @brief Print integer in selected base followed by a new-line. + * + * @param num 8-bit unsigned integer + * @param base Integer base + */ + void println(uint8_t num, IntegerBase base = IntegerBase::DEC); + + /** + * @brief Print integer in selected base followed by a new-line. + * + * @param num 16-bit unsigned integer + * @param base Integer base + */ + void println(uint16_t num, IntegerBase base = IntegerBase::DEC); + + /** + * @brief Print integer in selected base followed by a new-line. + * + * @param num 32-bit unsigned integer + * @param base Integer base + */ + void println(uint32_t num, IntegerBase base = IntegerBase::DEC); + + /** + * @brief Print integer in selected base followed by a new-line. + * + * @param num Integer + * @param base Integer base + */ + void println(size_t num, IntegerBase base = IntegerBase::DEC); + + /** + * @brief Print c-string followed by a new-line. + * + * @param str Null terminated string + */ + void println(const char * str); + + /** + * @brief Print string followed by a new-line. + * + * @param str String buffer + * @param length String length + */ + void println(const char * str, size_t length); + + /** + * @brief Print unsigned integer in selected base. + * + * @param value Unsigned integer value + * @param base Integer base + * @param fmt printf format string for base 10 + * @param size Size of value in bytes + */ + void printBase(uintmax_t value, IntegerBase base, const char * fmt, size_t size); + + /** + * @brief Print unsigned integer in base 10. + * + * @param value Unsigned integer value + * @param fmt printf format string + */ + void printDec(uintmax_t value, const char * fmt); + + /** + * @brief Print unsigned integer in base 2. + * + * @param value Unsigned integer value + * @param digits Number of digits to print + */ + void printBin(uintmax_t value, size_t digits); + + /** + * @brief Print unsigned integer in base 16. + * + * @param value Unsigned integer value + * @param digits Number of digits to print + */ + void printHex(uintmax_t value, size_t digits); + +} // namespace sta + + +#endif // STA_CORE_PRINTABLE_HPP \ No newline at end of file diff --git a/include/sta/printable_uart.hpp b/include/sta/printable_uart.hpp index d5473fc..9dfb3ee 100644 --- a/include/sta/printable_uart.hpp +++ b/include/sta/printable_uart.hpp @@ -77,13 +77,6 @@ namespace sta * @param base Integer base */ void print(uint32_t num, IntegerBase base = IntegerBase::DEC); - /** - * @brief Print integer in selected base. - * - * @param num Integer - * @param base Integer base - */ - void print(size_t num, IntegerBase base = IntegerBase::DEC); /** * @brief Print c-string. * @@ -142,13 +135,6 @@ namespace sta * @param base Integer base */ void println(uint32_t num, IntegerBase base = IntegerBase::DEC); - /** - * @brief Print integer in selected base followed by a new-line. - * - * @param num Integer - * @param base Integer base - */ - void println(size_t num, IntegerBase base = IntegerBase::DEC); /** * @brief Print c-string followed by a new-line. * diff --git a/include/sta/raspi/gpio_pin.hpp b/include/sta/raspi/gpio_pin.hpp index 64d5ee6..4174bbe 100644 --- a/include/sta/raspi/gpio_pin.hpp +++ b/include/sta/raspi/gpio_pin.hpp @@ -15,7 +15,7 @@ namespace sta { - class RaspiGpioPin : GpioPin + class RaspiGpioPin : public GpioPin { public: enum class GpioMode { @@ -31,18 +31,18 @@ namespace sta void setState(GpioPinState state) override; - static RaspiGpioPin DUMMY_GPIO; + static RaspiGpioPin * DUMMY_GPIO; private: uint8_t pin_; GpioMode mode_; }; - class DummyGpioPin : RaspiGpioPin { + class DummyGpioPin : public RaspiGpioPin { DummyGpioPin(); void setState(GpioPinState state) override; - } + }; } // namespace sta #endif // STA_RASPI_GPIO_ENABLED diff --git a/include/sta/raspi/hal.hpp b/include/sta/raspi/hal.hpp index 5b6173a..112597c 100644 --- a/include/sta/raspi/hal.hpp +++ b/include/sta/raspi/hal.hpp @@ -2,7 +2,7 @@ #define STA_CORE_RASPI_HAL_HPP #include -#include +#include #include #endif //STA_CORE_RASPI_HAL_HPP diff --git a/include/sta/raspi/mcu/common.hpp b/include/sta/raspi/mcu/common.hpp new file mode 100644 index 0000000..89ce60f --- /dev/null +++ b/include/sta/raspi/mcu/common.hpp @@ -0,0 +1,13 @@ +/** + * @file + * @brief Common configuration for Raspberry Pi MCUs + */ +#ifndef STA_CORE_RASPI_MCU_COMMON_HPP +#define STA_CORE_RASPI_MCU_COMMON_HPP + +#define STA_MCU_LITTLE_ENDIAN + +// Enable STM32 platform +#define STA_PLATFORM_RASPI + +#endif // STA_CORE_RASPI_MCU_COMMON_HPP \ No newline at end of file diff --git a/include/sta/raspi/spi.hpp b/include/sta/raspi/spi.hpp index cc8e741..c081aeb 100644 --- a/include/sta/raspi/spi.hpp +++ b/include/sta/raspi/spi.hpp @@ -9,10 +9,15 @@ namespace sta { + enum class SPINode { + DEV_0_0, + DEV_0_1 + }; + class RaspiSPI : public SPI { public: - RaspiSPI(const char * dev, const SPISettings & settings, Mutex * mutex = nullptr); + RaspiSPI(SPINode node, const SPISettings & settings, Mutex * mutex = nullptr, bool persistent_open = true); ~RaspiSPI(); void transfer(uint8_t value) override; @@ -30,18 +35,17 @@ namespace sta char * spidev_; int spifd_; bool open_; + const bool persistent_open_; void update_setting(int setting, int value); + int get_setting(int setting, void * rslt_ptr); + void update_settings(); }; - class RaspiSPIDevice : SPIDevice + class RaspiSPIDevice : public SPIDevice { public: RaspiSPIDevice(RaspiSPI * intf); - - void select() override; - - void deselect() override; }; } // namespace sta diff --git a/include/sta/spi/device.hpp b/include/sta/spi/device.hpp index 7821bff..657b6b1 100644 --- a/include/sta/spi/device.hpp +++ b/include/sta/spi/device.hpp @@ -93,7 +93,7 @@ namespace sta * * @return SPI settings */ - const SpiSettings & settings() const; + const SPISettings & settings() const; /** diff --git a/src/assert.cpp b/src/assert.cpp index 5ba23b2..3c02c58 100644 --- a/src/assert.cpp +++ b/src/assert.cpp @@ -1,7 +1,13 @@ #include #ifdef STA_ASSERT_ENABLED -#include +// TODO: This will probably destroy some stuff when working with stm32! +#ifdef STA_PRINTF_USE_STDLIB +# include +#else +# include +#endif + #include diff --git a/src/printable.cpp b/src/printable.cpp new file mode 100644 index 0000000..61d094c --- /dev/null +++ b/src/printable.cpp @@ -0,0 +1,117 @@ +#include + +#include +#include + +namespace sta +{ + void print(char c) + { + print(&c, 1); + } + + void print(bool b) + { + print(b ? "true" : "false"); + } + + void print(double d) + { + char buffer[64]; + snprintf(buffer, sizeof(buffer), "%f", d); + print(buffer); + } + + void print(uint8_t num, IntegerBase base /* = IntegerBase::DEC */) + { + printBase(num, base, "%" PRIu8, sizeof(num)); + } + + void print(uint16_t num, IntegerBase base /* = IntegerBase::DEC */) + { + printBase(num, base, "%" PRIu16, sizeof(num)); + } + + void print(uint32_t num, IntegerBase base /* = IntegerBase::DEC */) + { + printBase(num, base, "%" PRIu32, sizeof(num)); + } + + void print(const char * str) + { + print(str, strlen(str)); + } + + void println() + { + print("\r\n", 2); + } + + void println(char c) + { + print(&c, 1); + println(); + } + + void println(bool b) + { + print(b); + println(); + } + + void println(double d) + { + print(d); + println(); + } + + void println(uint8_t num, IntegerBase base /* = IntegerBase::DEC */) + { + print(num, base); + println(); + } + + void println(uint16_t num, IntegerBase base /* = IntegerBase::DEC */) + { + print(num, base); + println(); + } + + void println(uint32_t num, IntegerBase base /* = IntegerBase::DEC */) + { + print(num, base); + println(); + } + + void println(const char * str) + { + println(str, strlen(str)); + } + + void println(const char * str, size_t length) + { + print(str, length); + println(); + } + + void printBase(uintmax_t value, IntegerBase base, const char * fmt, size_t size) + { + + } + + void printDec(uintmax_t value, const char * fmt) + { + + } + + void printBin(uintmax_t value, size_t digits) + { + + } + + void printHex(uintmax_t value, size_t digits) + { + + } + +} // namespace sta \ No newline at end of file diff --git a/src/printable_uart.cpp b/src/printable_uart.cpp index 4462ac9..fbc48cf 100644 --- a/src/printable_uart.cpp +++ b/src/printable_uart.cpp @@ -49,11 +49,6 @@ namespace sta printBase(num, base, "%" PRIu32, sizeof(num)); } - void PrintableUART::print(size_t num, IntegerBase base /* = IntegerBase::DEC */) - { - printBase(num, base, "%z", sizeof(num)); - } - void PrintableUART::print(const char * str) { print(str, strlen(str)); @@ -106,12 +101,6 @@ namespace sta println(); } - void PrintableUART::println(size_t num, IntegerBase base /* = IntegerBase::DEC */) - { - print(num, base); - println(); - } - void PrintableUART::println(const char * str) { println(str, strlen(str)); diff --git a/src/raspi/gpio_pin.cpp b/src/raspi/gpio_pin.cpp index 01773d0..476e067 100644 --- a/src/raspi/gpio_pin.cpp +++ b/src/raspi/gpio_pin.cpp @@ -21,15 +21,13 @@ namespace sta } - DummyGpioPin::setState(GpioPinState state) + void DummyGpioPin::setState(GpioPinState state) { - // If this gets called, something is wrong! - STA_UNREACHABLE(); + // Don't do anything. } static DummyGpioPin dummyGpio; - RaspiGpioPin * RaspiGpioPin::DUMMY_GPIO = &dummyGpio; } // namespace sta diff --git a/src/raspi/spi.cpp b/src/raspi/spi.cpp index fa214e2..12baaec 100644 --- a/src/raspi/spi.cpp +++ b/src/raspi/spi.cpp @@ -1,8 +1,9 @@ #include + #ifdef STA_PLATFORM_RASPI - #include +#include #include #include #include @@ -10,28 +11,36 @@ #include #include +// Imports needed for SPI handling on Linux. +#include +#include +#include +#include -#define SYSTEM_ENDIANNESS __cpp_lib_endian +#define STA_DEBUG_IOCTL_WRITE(result) STA_ASSERT_MSG(result >= 0, "ioctl writing failed!") +#define STA_DEBUG_IOCTL_READ(result) STA_ASSERT_MSG(result >= 0, "ioctl reading failed!") +#define STA_DEBUG_IOCTL_SEND(result) STA_ASSERT_MSG(result >= 0, "ioctl sending spi message failed!") + +#define STA_DEBUG_FD_OPEN() STA_ASSERT_MSG(open_, "File descriptor wasn't opened!") +#define STA_DEBUG_NULLPTR(ptr) STA_ASSERT_MSG(ptr != nullptr, "Pointer is nullptr!") -// TODO: Check out "cpu_to_leXX()" and "leXX()_to_cpu()" on Linux! namespace sta { - RaspiSPI::RaspiSPI(const char * dev, const SPISettings & settings, Mutex * mutex = nullptr) - : SPI(settings, mutex) + RaspiSPI::RaspiSPI(SPINode node, const SPISettings & settings, Mutex * mutex, bool persistent_open = true) + : SPI(settings, mutex), open_{ false }, persistent_open_{ persistent_open } { // Check validity of parameters. - STA_ASSERT(dev != nullptr); - STA_ASSERT(mutex != nullptr); + STA_DEBUG_NULLPTR(mutex); open_ = false; - spidev_ = (char *)malloc(strlen(dev)+1); + + // Safer version of malloc + strcpy + spidev_ = strdup(node == SPINode::DEV_0_0 ? "/dev/spidev0.0" : "/dev/spidev0.1"); // Check if memory allocation was successful. - STA_ASSERT(spidev_ != nullptr); - - strcpy(spidev_ , dev); + STA_DEBUG_NULLPTR(spidev_); } RaspiSPI::~RaspiSPI() @@ -48,37 +57,37 @@ namespace sta void RaspiSPI::transfer(uint8_t value) { - STA_ASSERT(open_); + STA_DEBUG_FD_OPEN(); struct spi_ioc_transfer spi_message[1]; memset(spi_message, 0, sizeof(spi_message)); spi_message[0].tx_buf = (unsigned long)&value; spi_message[0].len = 1; - int result = ioctl(m_spifd, SPI_IOC_MESSAGE(1), spi_message); + int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); - STA_ASSERT(result == 0); + STA_DEBUG_IOCTL_SEND(result); } void RaspiSPI::transfer16(uint16_t value) { - STA_ASSERT(open_); + STA_DEBUG_FD_OPEN(); struct spi_ioc_transfer spi_message[1]; memset(spi_message, 0, sizeof(spi_message)); spi_message[0].tx_buf = (unsigned long)&value; spi_message[0].len = 1; - int result = ioctl(m_spifd, SPI_IOC_MESSAGE(1), spi_message); + int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); - STA_ASSERT(result == 0); + STA_DEBUG_IOCTL_SEND(result); } void RaspiSPI::transfer(const uint8_t * buffer, size_t size) { - STA_ASSERT(open_); - STA_ASSERT(buffer != nullptr); - STA_ASSERT(size != 0); + STA_DEBUG_FD_OPEN(); + STA_DEBUG_NULLPTR(buffer); + STA_ASSERT_MSG(size != 0, "Buffer size cannot be 0!"); struct spi_ioc_transfer spi_message[1]; memset(spi_message, 0, sizeof(spi_message)); @@ -87,14 +96,15 @@ namespace sta int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); - STA_ASSERT(result == 0); + STA_DEBUG_IOCTL_SEND(result); } void RaspiSPI::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) { - STA_ASSERT(txBuffer != nullptr); - STA_ASSERT(rxBuffer != nullptr); - STA_ASSERT(size != 0); + STA_DEBUG_FD_OPEN(); + STA_DEBUG_NULLPTR(txBuffer); + STA_DEBUG_NULLPTR(rxBuffer); + STA_ASSERT_MSG(size != 0, "Buffer size cannot be 0!"); struct spi_ioc_transfer spi_message[1]; memset(spi_message, 0, sizeof(spi_message)); @@ -105,24 +115,30 @@ namespace sta int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); - STA_ASSERT(result == 0); + STA_DEBUG_IOCTL_SEND(result); } void RaspiSPI::receive(uint8_t * buffer, size_t size) { - STA_ASSERT(buffer != nullptr); + STA_DEBUG_FD_OPEN(); + STA_DEBUG_NULLPTR(buffer); + STA_ASSERT_MSG(size != 0, "Buffer size cannot be 0!"); struct spi_ioc_transfer spi_message[1]; memset(spi_message, 0, sizeof(spi_message)); spi_message[0].rx_buf = (unsigned long)buffer; spi_message[0].len = size; - return ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); + + int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); + + STA_DEBUG_IOCTL_SEND(result); } void RaspiSPI::fill(uint8_t value, size_t count) { - STA_ASSERT(count != 0); + STA_DEBUG_FD_OPEN(); + STA_ASSERT_MSG(count != 0, "Buffer size cannot be 0!"); uint8_t * buffer = new uint8_t[count]; memset(buffer, value, count); @@ -134,15 +150,78 @@ namespace sta int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); + STA_DEBUG_IOCTL_SEND(result); + delete[] buffer; } void RaspiSPI::update_setting(int setting, int value) { - STA_ASSERT(open_); - + STA_DEBUG_FD_OPEN(); int result = ioctl(spifd_, setting, value); - STA_ASSERT(result >= 0); + STA_DEBUG_IOCTL_WRITE(result); + } + + int RaspiSPI::get_setting(int setting, void * rslt_ptr) + { + STA_DEBUG_FD_OPEN(); + int result = ioctl(spifd_, setting, rslt_ptr); + STA_DEBUG_IOCTL_READ(result); + } + + void RaspiSPI::update_settings() + { + SPISettings settings = this->settings(); + int mode; + + switch (settings.mode) + { + case SPIMode::MODE_0: + mode = SPI_MODE_0; + break; + case SPIMode::MODE_1: + mode = SPI_MODE_1; + break; + case SPIMode::MODE_2: + mode = SPI_MODE_2; + break; + case SPIMode::MODE_3: + mode = SPI_MODE_3; + break; + default: + STA_ASSERT_MSG(false, "Case for SPIMode enum not handled"); + STA_UNREACHABLE(); + } + + // Set the spi mode. + update_setting(SPI_IOC_WR_MODE, mode); + + // Set the word size. According to the documentation "the value zero signifies eight bits". + update_setting(SPI_IOC_WR_BITS_PER_WORD, settings.dataSize == SPIDataSize::SIZE_8 ? 0 : 16); + + // Set the bit order. According to the documentation zero means MSB first, everything else means LSB first. + update_setting(SPI_IOC_WR_LSB_FIRST, settings.bitOrder == SPIBitOrder::MSB ? 0 : 1); + + // Set the maximum clock speed. + update_setting(SPI_IOC_WR_MAX_SPEED_HZ, settings.clkSpeed); + + #if defined(DEBUG) + uint8_t r_mode = 0; + get_setting(SPI_IOC_RD_MODE, &r_mode); + STA_ASSERT_MSG(r_mode == mode, "The mode wasn't set correctly!"); + + uint8_t r_dataSize = 0; + get_setting(SPI_IOC_WR_BITS_PER_WORD, &r_dataSize); + STA_ASSERT_MSG(r_dataSize == (settings.dataSize == SPIDataSize::SIZE_8 ? 0 : 16), "The data size wasn't set correctly!"); + + uint8_t r_bitOrder = 0; + get_setting(SPI_IOC_RD_LSB_FIRST, &r_bitOrder); + STA_ASSERT_MSG(r_bitOrder == (settings.bitOrder == SPIBitOrder::MSB ? 0 : 1), "The data size wasn't set correctly!"); + + uint32_t r_clk_Speed; + get_setting(SPI_IOC_RD_MAX_SPEED_HZ, &r_clk_Speed); + STA_ASSERT_MSG(r_clk_Speed == settings.clkSpeed, "The clock speed wasn't set correctly!"); + #endif // STA_DEBUG } void RaspiSPI::acquire() @@ -156,20 +235,19 @@ namespace sta return; spifd_ = open(spidev_, O_RDWR); - STA_ASSERT(spifd_ < 0); + STA_ASSERT(spifd_ >= 0); open_ = true; - update_setting(SPI_IOC_WR_MODE, &m_spiconfig.mode); - update_setting(SPI_IOC_RD_MODE, &m_spiconfig.mode); - update_setting(SPI_IOC_WR_BITS_PER_WORD, &m_spiconfig.bits_per_word); - update_setting(SPI_IOC_RD_BITS_PER_WORD, &m_spiconfig.bits_per_word); - uppdate_setting(SPI_IOC_WR_MAX_SPEED_HZ, &m_spiconfig.speed); - update_setting(SPI_IOC_RD_MAX_SPEED_HZ, &m_spiconfig.speed); + update_settings(); } void RaspiSPI::release() { + if (!persistent_open_) { + close(spifd_); + } + SPI::release(); } @@ -177,16 +255,6 @@ namespace sta { } - - RaspiSPIDevice::select() - { - // do nothing since the raspi SPI API doesn't require GPIO interaction. - } - - RaspiSPIDevice::deselect() - { - // do nothing since the raspi SPI API doesn't require GPIO interaction. - } } // namespace sta diff --git a/src/spi/device.cpp b/src/spi/device.cpp index cced053..86a5210 100644 --- a/src/spi/device.cpp +++ b/src/spi/device.cpp @@ -70,7 +70,7 @@ namespace sta } - const SpiSettings & SPIDevice::settings() const + const SPISettings & SPIDevice::settings() const { return intf_->settings(); } @@ -78,11 +78,11 @@ namespace sta void SPIDevice::select() { - csPin_->setState(GpioPinState::LOW); + csPin_->setState(GpioPinState::GPIO_LOW); } void SPIDevice::deselect() { - csPin_->setState(GpioPinState::HIGH); + csPin_->setState(GpioPinState::GPIO_HIGH); } } // namespace sta From 38bdbbe52634cddf66c6a4343635967d1a0f910b Mon Sep 17 00:00:00 2001 From: Dario Date: Tue, 16 May 2023 21:15:53 +0100 Subject: [PATCH 06/22] Added some buxfixes to spi code --- include/sta/config.hpp | 11 +++++ include/sta/printable.hpp | 16 ------- include/sta/raspi/delay.hpp | 44 ++++++++++++++++++ include/sta/raspi/gpio_pin.hpp | 6 --- include/sta/raspi/spi.hpp | 8 +++- include/sta/spi/spi.hpp | 3 +- src/printable.cpp | 38 +++++++++++---- src/raspi/delay.cpp | 23 +++++++++ src/raspi/gpio_pin.cpp | 13 ++---- src/raspi/spi.cpp | 85 ++++++++++++++++++++++++---------- src/spi/spi.cpp | 3 +- src/stm32/i2c.cpp | 4 ++ 12 files changed, 186 insertions(+), 68 deletions(-) create mode 100644 include/sta/config.hpp create mode 100644 include/sta/raspi/delay.hpp create mode 100644 src/raspi/delay.cpp diff --git a/include/sta/config.hpp b/include/sta/config.hpp new file mode 100644 index 0000000..deed5f1 --- /dev/null +++ b/include/sta/config.hpp @@ -0,0 +1,11 @@ +#ifndef STA_CONFIG +#define STA_CONFIG + +// Use the raspberry pi for this project. +#include + +// #define DEBUG +#define STA_PRINTF_USE_STDLIB +#define STA_DEBUG_PRINTF + +#endif \ No newline at end of file diff --git a/include/sta/printable.hpp b/include/sta/printable.hpp index e94f253..4597a41 100644 --- a/include/sta/printable.hpp +++ b/include/sta/printable.hpp @@ -70,14 +70,6 @@ namespace sta */ void print(uint32_t num, IntegerBase base = IntegerBase::DEC); - /** - * @brief Print integer in selected base. - * - * @param num Integer - * @param base Integer base - */ - void print(size_t num, IntegerBase base = IntegerBase::DEC); - /** * @brief Print c-string. * @@ -143,14 +135,6 @@ namespace sta */ void println(uint32_t num, IntegerBase base = IntegerBase::DEC); - /** - * @brief Print integer in selected base followed by a new-line. - * - * @param num Integer - * @param base Integer base - */ - void println(size_t num, IntegerBase base = IntegerBase::DEC); - /** * @brief Print c-string followed by a new-line. * diff --git a/include/sta/raspi/delay.hpp b/include/sta/raspi/delay.hpp new file mode 100644 index 0000000..cbf85dc --- /dev/null +++ b/include/sta/raspi/delay.hpp @@ -0,0 +1,44 @@ +/** + * @file + * @brief Delay functions. + * + * Configuration: + * * STA_RASPI_DELAY_US_TIM: 1 MHz TIM instance used by sta::delayUs + * + * NOTE: TIM time base must be started before use of sta::delayUs by calling sta::initHAL. + * When using startup system task this is handled automatically. + */ +#ifndef STA_CORE_RASPI_DELAY_HPP +#define STA_CORE_RASPI_DELAY_HPP + + +// Only enable module on RASPI platform +#include + + +#if defined(STA_PLATFORM_RASPI) || defined(DOXYGEN) + +#include + + +namespace sta +{ + /** + * @brief Millisecond delay. + * + * @param ms Milliseconds + */ + void delayMs(uint32_t ms); + + /** + * @brief Microsecond delay. + * + * @param us Microseconds + */ + void delayUs(uint32_t us); +} // namespace sta + + +#endif // STA_PLATFORM_RASPI + +#endif // STA_CORE_RASPI_DELAY_HPP diff --git a/include/sta/raspi/gpio_pin.hpp b/include/sta/raspi/gpio_pin.hpp index 4174bbe..10aa951 100644 --- a/include/sta/raspi/gpio_pin.hpp +++ b/include/sta/raspi/gpio_pin.hpp @@ -37,12 +37,6 @@ namespace sta uint8_t pin_; GpioMode mode_; }; - - class DummyGpioPin : public RaspiGpioPin { - DummyGpioPin(); - - void setState(GpioPinState state) override; - }; } // namespace sta #endif // STA_RASPI_GPIO_ENABLED diff --git a/include/sta/raspi/spi.hpp b/include/sta/raspi/spi.hpp index c081aeb..484debc 100644 --- a/include/sta/raspi/spi.hpp +++ b/include/sta/raspi/spi.hpp @@ -36,9 +36,15 @@ namespace sta int spifd_; bool open_; const bool persistent_open_; + SPISettings settings_; + + uint8_t mode_; + uint8_t dataSize_; + uint8_t bitOrder_; + uint32_t clkSpeed_; void update_setting(int setting, int value); - int get_setting(int setting, void * rslt_ptr); + void get_setting(int setting, void * rslt_ptr); void update_settings(); }; diff --git a/include/sta/spi/spi.hpp b/include/sta/spi/spi.hpp index 5186cbe..bb80419 100644 --- a/include/sta/spi/spi.hpp +++ b/include/sta/spi/spi.hpp @@ -81,8 +81,7 @@ namespace sta * * @return %SPI settings */ - const SPISettings & settings() const; - + const SPISettings & settings(); /** * @brief Acquire usage rights to use the interface. diff --git a/src/printable.cpp b/src/printable.cpp index 61d094c..3bc8325 100644 --- a/src/printable.cpp +++ b/src/printable.cpp @@ -2,6 +2,8 @@ #include #include +#include +#include namespace sta { @@ -22,17 +24,17 @@ namespace sta print(buffer); } - void print(uint8_t num, IntegerBase base /* = IntegerBase::DEC */) + void print(uint8_t num, IntegerBase base) { printBase(num, base, "%" PRIu8, sizeof(num)); } - void print(uint16_t num, IntegerBase base /* = IntegerBase::DEC */) + void print(uint16_t num, IntegerBase base) { printBase(num, base, "%" PRIu16, sizeof(num)); } - void print(uint32_t num, IntegerBase base /* = IntegerBase::DEC */) + void print(uint32_t num, IntegerBase base) { printBase(num, base, "%" PRIu32, sizeof(num)); } @@ -42,6 +44,10 @@ namespace sta print(str, strlen(str)); } + void print(const char * str, size_t length) { + print(str, length); + } + void println() { print("\r\n", 2); @@ -65,19 +71,19 @@ namespace sta println(); } - void println(uint8_t num, IntegerBase base /* = IntegerBase::DEC */) + void println(uint8_t num, IntegerBase base) { print(num, base); println(); } - void println(uint16_t num, IntegerBase base /* = IntegerBase::DEC */) + void println(uint16_t num, IntegerBase base) { print(num, base); println(); } - void println(uint32_t num, IntegerBase base /* = IntegerBase::DEC */) + void println(uint32_t num, IntegerBase base) { print(num, base); println(); @@ -96,12 +102,26 @@ namespace sta void printBase(uintmax_t value, IntegerBase base, const char * fmt, size_t size) { - + switch (base) + { + case IntegerBase::DEC: + printDec(value, fmt); + break; + case IntegerBase::HEX: + printHex(value, size); + break; + case IntegerBase::BIN: + printBin(value, size); + break; + default: + STA_ASSERT_MSG(false, "Case for IntegerBase enum not handled"); + STA_UNREACHABLE(); + } } void printDec(uintmax_t value, const char * fmt) { - + printf(fmt, value); } void printBin(uintmax_t value, size_t digits) @@ -111,7 +131,7 @@ namespace sta void printHex(uintmax_t value, size_t digits) { - + printf("%x", value); } } // namespace sta \ No newline at end of file diff --git a/src/raspi/delay.cpp b/src/raspi/delay.cpp new file mode 100644 index 0000000..0cbb495 --- /dev/null +++ b/src/raspi/delay.cpp @@ -0,0 +1,23 @@ +#include +#ifdef STA_PLATFORM_RASPI + +#include + +#include +#include + + +namespace sta +{ + void delayMs(uint32_t ms) + { + delay(ms); + } + + void delayUs(uint32_t us) + { + delayMicroseconds(us); + } +} // namespace sta + +#endif // STA_PLATFORM_RASPI diff --git a/src/raspi/gpio_pin.cpp b/src/raspi/gpio_pin.cpp index 476e067..a1b8898 100644 --- a/src/raspi/gpio_pin.cpp +++ b/src/raspi/gpio_pin.cpp @@ -16,15 +16,12 @@ namespace sta digitalWrite(pin_, state == GpioPinState::GPIO_LOW ? LOW : HIGH); } - DummyGpioPin::DummyGpioPin() : RaspiGpioPin { -1 , GpioMode::GPIO_INPUT } - { + class DummyGpioPin : public RaspiGpioPin { + public: + DummyGpioPin() : RaspiGpioPin { 0, GpioMode::GPIO_INPUT } {} - } - - void DummyGpioPin::setState(GpioPinState state) - { - // Don't do anything. - } + void setState(GpioPinState state) override {} + }; static DummyGpioPin dummyGpio; diff --git a/src/raspi/spi.cpp b/src/raspi/spi.cpp index 12baaec..600b1cc 100644 --- a/src/raspi/spi.cpp +++ b/src/raspi/spi.cpp @@ -10,12 +10,14 @@ #include #include +#include // Imports needed for SPI handling on Linux. #include #include #include #include +#include #define STA_DEBUG_IOCTL_WRITE(result) STA_ASSERT_MSG(result >= 0, "ioctl writing failed!") #define STA_DEBUG_IOCTL_READ(result) STA_ASSERT_MSG(result >= 0, "ioctl reading failed!") @@ -28,8 +30,8 @@ namespace sta { - RaspiSPI::RaspiSPI(SPINode node, const SPISettings & settings, Mutex * mutex, bool persistent_open = true) - : SPI(settings, mutex), open_{ false }, persistent_open_{ persistent_open } + RaspiSPI::RaspiSPI(SPINode node, const SPISettings & settings, Mutex * mutex, bool persistent_open) + : SPI(settings, mutex), open_{ false }, persistent_open_{ persistent_open }, settings_{ settings } { // Check validity of parameters. STA_DEBUG_NULLPTR(mutex); @@ -60,6 +62,8 @@ namespace sta STA_DEBUG_FD_OPEN(); struct spi_ioc_transfer spi_message[1]; + + // According to the documentation, spi_message should be zero intialized. memset(spi_message, 0, sizeof(spi_message)); spi_message[0].tx_buf = (unsigned long)&value; spi_message[0].len = 1; @@ -74,6 +78,8 @@ namespace sta STA_DEBUG_FD_OPEN(); struct spi_ioc_transfer spi_message[1]; + + // According to the documentation, spi_message should be zero intialized. memset(spi_message, 0, sizeof(spi_message)); spi_message[0].tx_buf = (unsigned long)&value; spi_message[0].len = 1; @@ -90,10 +96,18 @@ namespace sta STA_ASSERT_MSG(size != 0, "Buffer size cannot be 0!"); struct spi_ioc_transfer spi_message[1]; + + // According to the documentation, spi_message should be zero intialized. memset(spi_message, 0, sizeof(spi_message)); spi_message[0].tx_buf = (unsigned long)buffer; spi_message[0].len = size; + printf("Sending "); + for (int i = 0; i < size; i++) { + printf("%x, ", buffer[i]); + } + printf("\n"); + int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); STA_DEBUG_IOCTL_SEND(result); @@ -107,6 +121,8 @@ namespace sta STA_ASSERT_MSG(size != 0, "Buffer size cannot be 0!"); struct spi_ioc_transfer spi_message[1]; + + // According to the documentation, spi_message should be zero intialized. memset(spi_message, 0, sizeof(spi_message)); spi_message[0].rx_buf = (unsigned long)rxBuffer; @@ -115,6 +131,10 @@ namespace sta int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); + if (result == -1) { + printf("Sending failed with error '%s'! \n", strerror(errno)); + } + STA_DEBUG_IOCTL_SEND(result); } @@ -125,6 +145,8 @@ namespace sta STA_ASSERT_MSG(size != 0, "Buffer size cannot be 0!"); struct spi_ioc_transfer spi_message[1]; + + // According to the documentation, spi_message should be zero intialized. memset(spi_message, 0, sizeof(spi_message)); spi_message[0].rx_buf = (unsigned long)buffer; @@ -132,6 +154,12 @@ namespace sta int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); + printf("Receiving "); + for (int i = 0; i < size; i++) { + printf("%x, ", buffer[i]); + } + printf("\n"); + STA_DEBUG_IOCTL_SEND(result); } @@ -157,70 +185,75 @@ namespace sta void RaspiSPI::update_setting(int setting, int value) { - STA_DEBUG_FD_OPEN(); - int result = ioctl(spifd_, setting, value); - STA_DEBUG_IOCTL_WRITE(result); + } - int RaspiSPI::get_setting(int setting, void * rslt_ptr) + void RaspiSPI::get_setting(int setting, void * rslt_ptr) { STA_DEBUG_FD_OPEN(); int result = ioctl(spifd_, setting, rslt_ptr); - STA_DEBUG_IOCTL_READ(result); + STA_DEBUG_IOCTL_READ(result); } void RaspiSPI::update_settings() { - SPISettings settings = this->settings(); - int mode; + STA_DEBUG_FD_OPEN(); - switch (settings.mode) + switch (settings_.mode) { case SPIMode::MODE_0: - mode = SPI_MODE_0; + mode_ = SPI_MODE_0; break; case SPIMode::MODE_1: - mode = SPI_MODE_1; + mode_ = SPI_MODE_1; break; case SPIMode::MODE_2: - mode = SPI_MODE_2; + mode_ = SPI_MODE_2; break; case SPIMode::MODE_3: - mode = SPI_MODE_3; + mode_ = SPI_MODE_3; break; default: STA_ASSERT_MSG(false, "Case for SPIMode enum not handled"); STA_UNREACHABLE(); } + + dataSize_ = settings_.dataSize == SPIDataSize::SIZE_8 ? 0 : 16; + bitOrder_ = settings_.bitOrder == SPIBitOrder::MSB ? 0 : 1; + clkSpeed_ = settings_.clkSpeed; // Set the spi mode. - update_setting(SPI_IOC_WR_MODE, mode); + int result = ioctl(spifd_, SPI_IOC_WR_MODE, &mode_); + STA_DEBUG_IOCTL_WRITE(result); // Set the word size. According to the documentation "the value zero signifies eight bits". - update_setting(SPI_IOC_WR_BITS_PER_WORD, settings.dataSize == SPIDataSize::SIZE_8 ? 0 : 16); + result = ioctl(spifd_, SPI_IOC_WR_BITS_PER_WORD, &dataSize_); + STA_DEBUG_IOCTL_WRITE(result); // Set the bit order. According to the documentation zero means MSB first, everything else means LSB first. - update_setting(SPI_IOC_WR_LSB_FIRST, settings.bitOrder == SPIBitOrder::MSB ? 0 : 1); + result = ioctl(spifd_, SPI_IOC_WR_LSB_FIRST, &bitOrder_); + STA_DEBUG_IOCTL_WRITE(result); // Set the maximum clock speed. - update_setting(SPI_IOC_WR_MAX_SPEED_HZ, settings.clkSpeed); + result = ioctl(spifd_, SPI_IOC_WR_MAX_SPEED_HZ, &clkSpeed_); + STA_DEBUG_IOCTL_WRITE(result); #if defined(DEBUG) uint8_t r_mode = 0; get_setting(SPI_IOC_RD_MODE, &r_mode); - STA_ASSERT_MSG(r_mode == mode, "The mode wasn't set correctly!"); + STA_ASSERT_MSG(r_mode == mode_, "The mode wasn't set correctly!"); uint8_t r_dataSize = 0; get_setting(SPI_IOC_WR_BITS_PER_WORD, &r_dataSize); - STA_ASSERT_MSG(r_dataSize == (settings.dataSize == SPIDataSize::SIZE_8 ? 0 : 16), "The data size wasn't set correctly!"); + STA_ASSERT_MSG(r_dataSize == (settings_.dataSize == SPIDataSize::SIZE_8 ? 8 : 16), "The data size wasn't set correctly!"); uint8_t r_bitOrder = 0; get_setting(SPI_IOC_RD_LSB_FIRST, &r_bitOrder); - STA_ASSERT_MSG(r_bitOrder == (settings.bitOrder == SPIBitOrder::MSB ? 0 : 1), "The data size wasn't set correctly!"); + STA_ASSERT_MSG(r_bitOrder == (settings_.bitOrder == SPIBitOrder::MSB ? 0 : 1), "The data size wasn't set correctly!"); uint32_t r_clk_Speed; get_setting(SPI_IOC_RD_MAX_SPEED_HZ, &r_clk_Speed); - STA_ASSERT_MSG(r_clk_Speed == settings.clkSpeed, "The clock speed wasn't set correctly!"); + STA_ASSERT_MSG(r_clk_Speed == settings_.clkSpeed, "The clock speed wasn't set correctly!"); #endif // STA_DEBUG } @@ -235,16 +268,18 @@ namespace sta return; spifd_ = open(spidev_, O_RDWR); - STA_ASSERT(spifd_ >= 0); - open_ = true; + STA_ASSERT(spifd_ >= 0); + update_settings(); } void RaspiSPI::release() { - if (!persistent_open_) { + STA_ASSERT_MSG(open_, "'release' was called despite the device being closed! This has to be a bug!"); + + if (!persistent_open_ && open_) { close(spifd_); } diff --git a/src/spi/spi.cpp b/src/spi/spi.cpp index 56d38db..621086f 100644 --- a/src/spi/spi.cpp +++ b/src/spi/spi.cpp @@ -1,5 +1,6 @@ #include +#include namespace sta { @@ -7,7 +8,7 @@ namespace sta : settings_{settings}, mutex_{mutex} {} - const SPISettings & SPI::settings() const + const SPISettings & SPI::settings() { return settings_; } diff --git a/src/stm32/i2c.cpp b/src/stm32/i2c.cpp index 289bfc1..ce4bdbd 100644 --- a/src/stm32/i2c.cpp +++ b/src/stm32/i2c.cpp @@ -1,5 +1,7 @@ #include +#if false + namespace sta { STM32I2cDevice::STM32I2cDevice(I2C_HandleTypeDef* i2cHandle, uint16_t address, Mutex* mutex, bool master, bool blocking) : I2cDevice(address, mutex, master, blocking) { @@ -51,3 +53,5 @@ namespace sta { return res == HAL_OK; } } + +#endif // false \ No newline at end of file From 4eb1053ffd465e068d50953c0ad527b3c543bf65 Mon Sep 17 00:00:00 2001 From: Dario Date: Tue, 16 May 2023 22:19:08 +0100 Subject: [PATCH 07/22] Modified GpioPin to offer PinState reading --- include/sta/gpio_pin.hpp | 7 +++++++ include/sta/raspi/gpio_pin.hpp | 2 ++ src/raspi/gpio_pin.cpp | 15 +++++++++++++-- src/raspi/spi.cpp | 2 +- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/include/sta/gpio_pin.hpp b/include/sta/gpio_pin.hpp index c3ef231..9a26f4c 100644 --- a/include/sta/gpio_pin.hpp +++ b/include/sta/gpio_pin.hpp @@ -37,6 +37,13 @@ namespace sta * @param state Output state */ virtual void setState(GpioPinState state) = 0; + + /** + * @brief Get pin input state. + * + * @param state Input state + */ + virtual GpioPinState getState() = 0; }; diff --git a/include/sta/raspi/gpio_pin.hpp b/include/sta/raspi/gpio_pin.hpp index 10aa951..6f64fc7 100644 --- a/include/sta/raspi/gpio_pin.hpp +++ b/include/sta/raspi/gpio_pin.hpp @@ -31,6 +31,8 @@ namespace sta void setState(GpioPinState state) override; + GpioPinState getState() override; + static RaspiGpioPin * DUMMY_GPIO; private: diff --git a/src/raspi/gpio_pin.cpp b/src/raspi/gpio_pin.cpp index a1b8898..af3e210 100644 --- a/src/raspi/gpio_pin.cpp +++ b/src/raspi/gpio_pin.cpp @@ -6,7 +6,7 @@ namespace sta { - RaspiGpioPin::RaspiGpioPin(uint8_t pin, GpioMode mode) : pin_{pin}, mode_{mode} + RaspiGpioPin::RaspiGpioPin(uint8_t pin, GpioMode mode) : pin_{ pin }, mode_{ mode } { pinMode(pin, mode == GpioMode::GPIO_INPUT ? INPUT : OUTPUT); } @@ -16,11 +16,22 @@ namespace sta digitalWrite(pin_, state == GpioPinState::GPIO_LOW ? LOW : HIGH); } - class DummyGpioPin : public RaspiGpioPin { + GpioPinState RaspiGpioPin::getState() + { + return digitalRead(pin_) == LOW ? GpioPinState::GPIO_LOW : GpioPinState::GPIO_HIGH; + } + + class DummyGpioPin : public RaspiGpioPin + { public: DummyGpioPin() : RaspiGpioPin { 0, GpioMode::GPIO_INPUT } {} void setState(GpioPinState state) override {} + + GpioPinState getState() override { + // This should really never be called since the code might rely on the returned value to be accurate. + STA_UNREACHABLE(); + } }; static DummyGpioPin dummyGpio; diff --git a/src/raspi/spi.cpp b/src/raspi/spi.cpp index 600b1cc..1b60e6b 100644 --- a/src/raspi/spi.cpp +++ b/src/raspi/spi.cpp @@ -133,7 +133,7 @@ namespace sta if (result == -1) { printf("Sending failed with error '%s'! \n", strerror(errno)); - } + } STA_DEBUG_IOCTL_SEND(result); } From 3cf2173433000fb8d5bd8dfbce7a3101b272d6b0 Mon Sep 17 00:00:00 2001 From: Dario Date: Tue, 23 May 2023 21:05:31 +0100 Subject: [PATCH 08/22] Added fixes to the SPI implementation, removed debugging --- include/sta/raspi/gpio_pin.hpp | 10 ++++---- src/raspi/spi.cpp | 42 +++++++++++++++++++++++----------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/include/sta/raspi/gpio_pin.hpp b/include/sta/raspi/gpio_pin.hpp index 6f64fc7..2e5e456 100644 --- a/include/sta/raspi/gpio_pin.hpp +++ b/include/sta/raspi/gpio_pin.hpp @@ -15,14 +15,14 @@ namespace sta { + enum class GpioMode { + GPIO_OUTPUT, + GPIO_INPUT + }; + class RaspiGpioPin : public GpioPin { public: - enum class GpioMode { - GPIO_OUTPUT, - GPIO_INPUT - }; - /** * @param pin Pin index * @param mode The mode of the GPIO pin. Either INPUT or OUTPUT diff --git a/src/raspi/spi.cpp b/src/raspi/spi.cpp index 1b60e6b..0d9d92f 100644 --- a/src/raspi/spi.cpp +++ b/src/raspi/spi.cpp @@ -68,8 +68,15 @@ namespace sta spi_message[0].tx_buf = (unsigned long)&value; spi_message[0].len = 1; + // For some reasons, this line makes the SPI interface work for the BMP388. + spi_message[0].cs_change = 1; + int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); + if (result < 0) { + printf("Sending failed with error '%s'! \n", strerror(errno)); + } + STA_DEBUG_IOCTL_SEND(result); } @@ -83,6 +90,7 @@ namespace sta memset(spi_message, 0, sizeof(spi_message)); spi_message[0].tx_buf = (unsigned long)&value; spi_message[0].len = 1; + spi_message[0].cs_change = 1; int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); @@ -102,14 +110,12 @@ namespace sta spi_message[0].tx_buf = (unsigned long)buffer; spi_message[0].len = size; - printf("Sending "); - for (int i = 0; i < size; i++) { - printf("%x, ", buffer[i]); - } - printf("\n"); - int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); + if (result < 0) { + printf("Sending failed with error '%s'! \n", strerror(errno)); + } + STA_DEBUG_IOCTL_SEND(result); } @@ -131,7 +137,7 @@ namespace sta int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); - if (result == -1) { + if (result < 0) { printf("Sending failed with error '%s'! \n", strerror(errno)); } @@ -154,12 +160,6 @@ namespace sta int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); - printf("Receiving "); - for (int i = 0; i < size; i++) { - printf("%x, ", buffer[i]); - } - printf("\n"); - STA_DEBUG_IOCTL_SEND(result); } @@ -226,18 +226,34 @@ namespace sta int result = ioctl(spifd_, SPI_IOC_WR_MODE, &mode_); STA_DEBUG_IOCTL_WRITE(result); + if (result < 0) { + printf("Update mode failed with error '%s'! \n", strerror(errno)); + } + // Set the word size. According to the documentation "the value zero signifies eight bits". result = ioctl(spifd_, SPI_IOC_WR_BITS_PER_WORD, &dataSize_); STA_DEBUG_IOCTL_WRITE(result); + if (result < 0) { + printf("Update dataSize failed with error '%s'! \n", strerror(errno)); + } + // Set the bit order. According to the documentation zero means MSB first, everything else means LSB first. result = ioctl(spifd_, SPI_IOC_WR_LSB_FIRST, &bitOrder_); STA_DEBUG_IOCTL_WRITE(result); + if (result < 0) { + printf("Update endianness failed with error '%s'! \n", strerror(errno)); + } + // Set the maximum clock speed. result = ioctl(spifd_, SPI_IOC_WR_MAX_SPEED_HZ, &clkSpeed_); STA_DEBUG_IOCTL_WRITE(result); + if (result < 0) { + printf("Update clock speed failed with error '%s'! \n", strerror(errno)); + } + #if defined(DEBUG) uint8_t r_mode = 0; get_setting(SPI_IOC_RD_MODE, &r_mode); From 6b4acfd27b001d455ecfbec41eedb99c84bdaa0d Mon Sep 17 00:00:00 2001 From: Dario Date: Fri, 23 Jun 2023 15:50:54 +0100 Subject: [PATCH 09/22] Added I2C support for raspi & first rework of debugging --- include/sta/{ => bus}/can/controller.hpp | 6 +- include/sta/{ => bus}/can/filter.hpp | 2 +- include/sta/{ => bus}/can/headers.hpp | 2 +- include/sta/{ => bus}/can/id.hpp | 0 include/sta/{ => bus}/can/iter.hpp | 0 include/sta/{ => bus}/can/subscribable.hpp | 8 +- include/sta/{ => bus}/can/subscribable.tpp | 0 include/sta/{spi => bus}/device.hpp | 63 ++-- include/sta/bus/i2c/device.hpp | 37 +++ include/sta/bus/i2c/i2c.hpp | 33 ++ .../sta/{spi/spi.hpp => bus/interface.hpp} | 48 +-- include/sta/bus/spi/device.hpp | 51 ++++ include/sta/{ => bus}/spi/settings.hpp | 3 - include/sta/bus/spi/spi.hpp | 47 +++ include/sta/bus/uart/settings.hpp | 24 ++ include/sta/bus/uart/uart.hpp | 25 ++ include/sta/config.hpp | 2 +- .../{ => devices}/arduino/not_implemented.hpp | 0 include/sta/devices/raspi/bus/i2c.hpp | 52 ++++ .../sta/{raspi => devices/raspi/bus}/spi.hpp | 4 +- include/sta/devices/raspi/can.hpp | 0 include/sta/{ => devices}/raspi/delay.hpp | 0 include/sta/{ => devices}/raspi/gpio_pin.hpp | 2 +- include/sta/{ => devices}/raspi/hal.hpp | 0 .../sta/{ => devices}/raspi/mcu/common.hpp | 0 include/sta/{ => devices}/stm32/can.hpp | 0 include/sta/{ => devices}/stm32/clocks.hpp | 0 include/sta/{ => devices}/stm32/delay.hpp | 0 include/sta/{ => devices}/stm32/gpio_pin.hpp | 0 include/sta/{ => devices}/stm32/hal.hpp | 0 include/sta/{ => devices}/stm32/i2c.hpp | 0 include/sta/{ => devices}/stm32/init.hpp | 0 .../{ => devices}/stm32/mcu/STM32F411xE.hpp | 0 .../{ => devices}/stm32/mcu/STM32F413xx.hpp | 0 .../sta/{ => devices}/stm32/mcu/common.hpp | 0 include/sta/{ => devices}/stm32/spi.hpp | 0 include/sta/{ => devices}/stm32/uart.hpp | 0 include/sta/devices/template/delay.hpp | 43 +++ include/sta/i2c.hpp | 27 -- include/sta/lang.hpp | 8 + include/sta/printable.hpp | 283 +++++++++--------- include/sta/printable_uart.hpp | 156 +--------- include/sta/uart.hpp | 10 +- src/bus/device.cpp | 77 +++++ src/bus/i2c/device.cpp | 23 ++ src/bus/i2c/i2c.cpp | 11 + src/bus/interface.cpp | 29 ++ src/bus/spi/device.cpp | 29 ++ src/{ => bus}/spi/settings.cpp | 2 +- src/bus/spi/spi.cpp | 17 ++ src/bus/uart/settings.cpp | 0 src/bus/uart/uart.cpp | 15 + src/devices/raspi/bus/i2c/i2c.cpp | 129 ++++++++ src/{raspi => devices/raspi/bus/spi}/spi.cpp | 12 +- src/{ => devices}/raspi/delay.cpp | 4 +- src/{ => devices}/raspi/gpio_pin.cpp | 0 src/{ => devices}/stm32/can.cpp | 0 src/{ => devices}/stm32/delay.cpp | 0 src/{ => devices}/stm32/gpio_pin.cpp | 0 src/{ => devices}/stm32/i2c.cpp | 0 src/{ => devices}/stm32/init.cpp | 0 src/{ => devices}/stm32/spi.cpp | 0 src/{ => devices}/stm32/uart.cpp | 2 +- src/devices/template/delay.cpp | 36 +++ src/i2c.cpp | 22 -- src/printable.cpp | 116 ++++--- src/printable_uart.cpp | 181 +---------- src/spi/device.cpp | 88 ------ src/spi/spi.cpp | 27 -- src/uart.cpp | 1 - 70 files changed, 985 insertions(+), 772 deletions(-) rename include/sta/{ => bus}/can/controller.hpp (96%) rename include/sta/{ => bus}/can/filter.hpp (96%) rename include/sta/{ => bus}/can/headers.hpp (96%) rename include/sta/{ => bus}/can/id.hpp (100%) rename include/sta/{ => bus}/can/iter.hpp (100%) rename include/sta/{ => bus}/can/subscribable.hpp (93%) rename include/sta/{ => bus}/can/subscribable.tpp (100%) rename include/sta/{spi => bus}/device.hpp (69%) create mode 100644 include/sta/bus/i2c/device.hpp create mode 100644 include/sta/bus/i2c/i2c.hpp rename include/sta/{spi/spi.hpp => bus/interface.hpp} (69%) create mode 100644 include/sta/bus/spi/device.hpp rename include/sta/{ => bus}/spi/settings.hpp (99%) create mode 100644 include/sta/bus/spi/spi.hpp create mode 100644 include/sta/bus/uart/settings.hpp create mode 100644 include/sta/bus/uart/uart.hpp rename include/sta/{ => devices}/arduino/not_implemented.hpp (100%) create mode 100644 include/sta/devices/raspi/bus/i2c.hpp rename include/sta/{raspi => devices/raspi/bus}/spi.hpp (95%) create mode 100644 include/sta/devices/raspi/can.hpp rename include/sta/{ => devices}/raspi/delay.hpp (100%) rename include/sta/{ => devices}/raspi/gpio_pin.hpp (96%) rename include/sta/{ => devices}/raspi/hal.hpp (100%) rename include/sta/{ => devices}/raspi/mcu/common.hpp (100%) rename include/sta/{ => devices}/stm32/can.hpp (100%) rename include/sta/{ => devices}/stm32/clocks.hpp (100%) rename include/sta/{ => devices}/stm32/delay.hpp (100%) rename include/sta/{ => devices}/stm32/gpio_pin.hpp (100%) rename include/sta/{ => devices}/stm32/hal.hpp (100%) rename include/sta/{ => devices}/stm32/i2c.hpp (100%) rename include/sta/{ => devices}/stm32/init.hpp (100%) rename include/sta/{ => devices}/stm32/mcu/STM32F411xE.hpp (100%) rename include/sta/{ => devices}/stm32/mcu/STM32F413xx.hpp (100%) rename include/sta/{ => devices}/stm32/mcu/common.hpp (100%) rename include/sta/{ => devices}/stm32/spi.hpp (100%) rename include/sta/{ => devices}/stm32/uart.hpp (100%) create mode 100644 include/sta/devices/template/delay.hpp delete mode 100644 include/sta/i2c.hpp create mode 100644 src/bus/device.cpp create mode 100644 src/bus/i2c/device.cpp create mode 100644 src/bus/i2c/i2c.cpp create mode 100644 src/bus/interface.cpp create mode 100644 src/bus/spi/device.cpp rename src/{ => bus}/spi/settings.cpp (97%) create mode 100644 src/bus/spi/spi.cpp create mode 100644 src/bus/uart/settings.cpp create mode 100644 src/bus/uart/uart.cpp create mode 100644 src/devices/raspi/bus/i2c/i2c.cpp rename src/{raspi => devices/raspi/bus/spi}/spi.cpp (97%) rename src/{ => devices}/raspi/delay.cpp (78%) rename src/{ => devices}/raspi/gpio_pin.cpp (100%) rename src/{ => devices}/stm32/can.cpp (100%) rename src/{ => devices}/stm32/delay.cpp (100%) rename src/{ => devices}/stm32/gpio_pin.cpp (100%) rename src/{ => devices}/stm32/i2c.cpp (100%) rename src/{ => devices}/stm32/init.cpp (100%) rename src/{ => devices}/stm32/spi.cpp (100%) rename src/{ => devices}/stm32/uart.cpp (92%) create mode 100644 src/devices/template/delay.cpp delete mode 100644 src/i2c.cpp delete mode 100644 src/spi/device.cpp delete mode 100644 src/spi/spi.cpp diff --git a/include/sta/can/controller.hpp b/include/sta/bus/can/controller.hpp similarity index 96% rename from include/sta/can/controller.hpp rename to include/sta/bus/can/controller.hpp index fbadb59..0c6f212 100644 --- a/include/sta/can/controller.hpp +++ b/include/sta/bus/can/controller.hpp @@ -12,9 +12,9 @@ */ -#include -#include -#include +#include +#include +#include namespace sta diff --git a/include/sta/can/filter.hpp b/include/sta/bus/can/filter.hpp similarity index 96% rename from include/sta/can/filter.hpp rename to include/sta/bus/can/filter.hpp index 0071139..1499eea 100644 --- a/include/sta/can/filter.hpp +++ b/include/sta/bus/can/filter.hpp @@ -5,7 +5,7 @@ #ifndef STA_CORE_CAN_FILTER_HPP #define STA_CORE_CAN_FILTER_HPP -#include +#include #include diff --git a/include/sta/can/headers.hpp b/include/sta/bus/can/headers.hpp similarity index 96% rename from include/sta/can/headers.hpp rename to include/sta/bus/can/headers.hpp index 24536c5..0214434 100644 --- a/include/sta/can/headers.hpp +++ b/include/sta/bus/can/headers.hpp @@ -5,7 +5,7 @@ #ifndef STA_CORE_CAN_HEADERS_HPP #define STA_CORE_CAN_HEADERS_HPP -#include +#include #include diff --git a/include/sta/can/id.hpp b/include/sta/bus/can/id.hpp similarity index 100% rename from include/sta/can/id.hpp rename to include/sta/bus/can/id.hpp diff --git a/include/sta/can/iter.hpp b/include/sta/bus/can/iter.hpp similarity index 100% rename from include/sta/can/iter.hpp rename to include/sta/bus/can/iter.hpp diff --git a/include/sta/can/subscribable.hpp b/include/sta/bus/can/subscribable.hpp similarity index 93% rename from include/sta/can/subscribable.hpp rename to include/sta/bus/can/subscribable.hpp index 1d84080..2f5d600 100644 --- a/include/sta/can/subscribable.hpp +++ b/include/sta/bus/can/subscribable.hpp @@ -5,8 +5,8 @@ #ifndef STA_CORE_CAN_SUBSCRIBABLE_HPP #define STA_CORE_CAN_SUBSCRIBABLE_HPP -#include -#include +#include +#include namespace sta @@ -96,7 +96,7 @@ namespace sta } // namespace sta -#include +#include -#endif // STA_CORE_CAN_SUBSCRIBABLE_HPP +#endif // STA_CORE_CAN_SUBSCRIBABLE_HPP \ No newline at end of file diff --git a/include/sta/can/subscribable.tpp b/include/sta/bus/can/subscribable.tpp similarity index 100% rename from include/sta/can/subscribable.tpp rename to include/sta/bus/can/subscribable.tpp diff --git a/include/sta/spi/device.hpp b/include/sta/bus/device.hpp similarity index 69% rename from include/sta/spi/device.hpp rename to include/sta/bus/device.hpp index 657b6b1..55ea4a4 100644 --- a/include/sta/spi/device.hpp +++ b/include/sta/bus/device.hpp @@ -1,33 +1,21 @@ -/** - * @file - * @brief SPI bus peripheral device. - */ -#ifndef STA_CORE_SPI_DEVICE_HPP -#define STA_CORE_SPI_DEVICE_HPP - -#include -#include - -#include -#include +#ifndef STA_CORE_BUS_SERIAL_DEVICE_HPP +#define STA_CORE_BUS_SERIAL_DEVICE_HPP +#include namespace sta { /** - * @brief Peripheral device connected via SPI. - * - * @ingroup sta_core_spi - */ - class SPIDevice + * @brief Abstract device for serial communication. + */ + class Device { public: - /** + /** * @param intf %SPI hardware interface * @param csPin Chip select pin */ - SPIDevice(SPI * intf, GpioPin * csPin); - + Device(Interface * intf); /** * @brief Start transmission with device. @@ -35,6 +23,7 @@ namespace sta * Must be called before any I/O operations. */ void beginTransmission(); + /** * @brief End transmission with device. * @@ -42,19 +31,20 @@ namespace sta */ void endTransmission(); - /** * @brief Send single byte of data. * * @param value 8-bit value */ void transfer(uint8_t value); + /** * @brief Send two bytes of data. * * @param value 16-bit value */ void transfer16(uint16_t value); + /** * @brief Send data from buffer. * @@ -62,6 +52,7 @@ namespace sta * @param size Number of bytes to transfer */ void transfer(const uint8_t * buffer, size_t size); + /** * @brief Send and receive data simultaneously. * @@ -70,6 +61,7 @@ namespace sta * @param size Number of bytes to transfer */ void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size); + /** * @brief Read incoming data to buffer. * @@ -78,7 +70,6 @@ namespace sta */ void receive(uint8_t * buffer, size_t size); - /** * @brief Send byte value repeatedly. * @@ -87,29 +78,21 @@ namespace sta */ void fill(uint8_t value, size_t count); + protected: + /** + * @brief Activate device.. + */ + virtual void select() = 0; /** - * @brief Get %SPI interface settings. - * - * @return SPI settings + * @brief Deactivate device.. */ - const SPISettings & settings() const; - - - /** - * @brief Activate device via CS pin. - */ - void select(); - /** - * @brief Deactivate device via CS pin. - */ - void deselect(); + virtual void deselect() = 0; private: - SPI * intf_; /**< %SPI hardware interface */ - GpioPin * csPin_; /**< Chip select pin */ + Interface * intf_; + bool selected_ = false; }; } // namespace sta - -#endif // STA_CORE_SPI_DEVICE_HPP +#endif // STA_CORE_BUS_SERIAL_DEVICE_HPP \ No newline at end of file diff --git a/include/sta/bus/i2c/device.hpp b/include/sta/bus/i2c/device.hpp new file mode 100644 index 0000000..2a96b4d --- /dev/null +++ b/include/sta/bus/i2c/device.hpp @@ -0,0 +1,37 @@ +#ifndef STA_CORE_I2C_DEVICE_HPP +#define STA_CORE_I2C_DEVICE_HPP + +#include +#include + +namespace sta +{ + /** + * @brief Peripheral device connected via I2c. + * + * @ingroup sta_core_i2c + */ + class I2cDevice : public Device + { + public: + /** + * @param intf %I2C hardware interface + * @param csPin The peripheral's address. + */ + I2cDevice(I2c * intf, int addr); + + protected: + void select() override; + + void deselect() override; + + private: + int addr_; /**< device address */ + }; + +} // namespace sta + + + + +#endif // STA_CORE_I2C_DEVICE_HPP \ No newline at end of file diff --git a/include/sta/bus/i2c/i2c.hpp b/include/sta/bus/i2c/i2c.hpp new file mode 100644 index 0000000..8d3123a --- /dev/null +++ b/include/sta/bus/i2c/i2c.hpp @@ -0,0 +1,33 @@ +#ifndef STA_CORE_I2C_I2C_HPP +#define STA_CORE_I2C_I2C_HPP + +#include +#include + +#include +#include + +namespace sta +{ + /** + * @brief Interface class for %I2C hardware. + * + * Represents a single %I2C bus that can be shared by multiple devices. + * + * @ingroup sta_core_i2c + */ + class I2c : public Interface + { + public: + I2c(Mutex * mutex=nullptr); + + /** + * @brief Address selection for the I2C bus. + * + * @param address The address to select. + */ + virtual void selectAddress(uint16_t address) = 0; + }; +} // namespace sta + +#endif // STA_CORE_I2C_I2C_HPP \ No newline at end of file diff --git a/include/sta/spi/spi.hpp b/include/sta/bus/interface.hpp similarity index 69% rename from include/sta/spi/spi.hpp rename to include/sta/bus/interface.hpp index bb80419..a8581d8 100644 --- a/include/sta/spi/spi.hpp +++ b/include/sta/bus/interface.hpp @@ -1,35 +1,20 @@ -/** - * @file - * @brief SPI bus software interface. - */ -#ifndef STA_CORE_SPI_SPI_HPP -#define STA_CORE_SPI_SPI_HPP +#ifndef STA_CORE_BUS_SERIAL_INTERFACE_HPP +#define STA_CORE_BUS_SERIAL_INTERFACE_HPP #include -#include -#include #include - +#include namespace sta { /** - * @brief Interface class for %SPI hardware. - * - * Represents a single %SPI bus that can be shared by multiple devices. - * - * @ingroup sta_core_spi - */ - class SPI + * @brief Abstract interface for serial communication. + */ + class Interface { public: - /** - * @param settings %SPI bus settings - * @param mutex Mutex object for managing shared access. Pass nullptr for no access control - */ - SPI(const SPISettings & settings, Mutex * mutex = nullptr); - + Interface(Mutex * mutex); /** * @brief Send single byte of data. @@ -66,7 +51,6 @@ namespace sta */ virtual void receive(uint8_t * buffer, size_t size) = 0; - /** * @brief Send byte value repeatedly. * @@ -75,14 +59,6 @@ namespace sta */ virtual void fill(uint8_t value, size_t count) = 0; - - /** - * @brief Get %SPI interface settings. - * - * @return %SPI settings - */ - const SPISettings & settings(); - /** * @brief Acquire usage rights to use the interface. * @@ -96,11 +72,15 @@ namespace sta */ virtual void release(); + /** + * @returns true if the interface has been aquired. + */ + bool isAquired(); private: - SPISettings settings_; /**< %SPI settings */ - Mutex * mutex_; /**< Mutex object */ + Mutex * mutex_; + bool aquired_ = false; }; } // namespace sta -#endif // STA_CORE_SPI_SPI_HPP +#endif // STA_CORE_BUS_SERIAL_INTERFACE_HPP \ No newline at end of file diff --git a/include/sta/bus/spi/device.hpp b/include/sta/bus/spi/device.hpp new file mode 100644 index 0000000..5f22b87 --- /dev/null +++ b/include/sta/bus/spi/device.hpp @@ -0,0 +1,51 @@ +/** + * @file + * @brief SPI bus peripheral device. + */ +#ifndef STA_CORE_SPI_DEVICE_HPP +#define STA_CORE_SPI_DEVICE_HPP + +#include +#include +#include + +#include +#include + + +namespace sta +{ + /** + * @brief Peripheral device connected via SPI. + * + * @ingroup sta_core_spi + */ + class SPIDevice : public Device + { + public: + /** + * @param intf %SPI hardware interface + * @param csPin Chip select pin + */ + SPIDevice(SPI * intf, GpioPin * csPin); + + /** + * @brief Get %SPI interface settings. + * + * @return SPI settings + */ + const SPISettings & settings() const; + + protected: + void select() override; + + void deselect() override; + + private: + SPI * intf_; /**< %SPI hardware interface */ + GpioPin * csPin_; /**< Chip select pin */ + }; +} // namespace sta + + +#endif // STA_CORE_SPI_DEVICE_HPP diff --git a/include/sta/spi/settings.hpp b/include/sta/bus/spi/settings.hpp similarity index 99% rename from include/sta/spi/settings.hpp rename to include/sta/bus/spi/settings.hpp index 35b7433..dea4618 100644 --- a/include/sta/spi/settings.hpp +++ b/include/sta/bus/spi/settings.hpp @@ -5,17 +5,14 @@ #ifndef STA_CORE_SPI_SETTINGS_HPP #define STA_CORE_SPI_SETTINGS_HPP - /** * @defgroup sta_core_spi SPI * @ingroup sta_core * @brief SPI interface. */ - #include - namespace sta { /** diff --git a/include/sta/bus/spi/spi.hpp b/include/sta/bus/spi/spi.hpp new file mode 100644 index 0000000..fdf978f --- /dev/null +++ b/include/sta/bus/spi/spi.hpp @@ -0,0 +1,47 @@ +/** + * @file + * @brief SPI bus software interface. + */ +#ifndef STA_CORE_SPI_SPI_HPP +#define STA_CORE_SPI_SPI_HPP + +#include +#include +#include + +#include +#include + + +namespace sta +{ + /** + * @brief Interface class for %SPI hardware. + * + * Represents a single %SPI bus that can be shared by multiple devices. + * + * @ingroup sta_core_spi + */ + class SPI : public Interface + { + public: + /** + * @param settings %SPI bus settings + * @param mutex Mutex object for managing shared access. Pass nullptr for no access control + */ + SPI(const SPISettings & settings, Mutex * mutex = nullptr); + + /** + * @brief Get %SPI interface settings. + * + * @return %SPI settings + */ + const SPISettings & settings(); + + private: + SPISettings settings_; /**< %SPI settings */ + }; +} // namespace sta + + +#endif // STA_CORE_SPI_SPI_HPP diff --git a/include/sta/bus/uart/settings.hpp b/include/sta/bus/uart/settings.hpp new file mode 100644 index 0000000..c3bbe2b --- /dev/null +++ b/include/sta/bus/uart/settings.hpp @@ -0,0 +1,24 @@ +#ifndef STA_CORE_UART_SETTINGS_HPP +#define STA_CORE_UART_SETTINGS_HPP + +#include + +namespace sta +{ + enum class UARTMode + { + RX, + TX, + RX_TX + }; + + /** + * @brief %UART settings. + */ + struct UARTSettings + { + UARTMode mode; + }; +} // namespace sta + +#endif // STA_CORE_UART_SETTINGS_HPP diff --git a/include/sta/bus/uart/uart.hpp b/include/sta/bus/uart/uart.hpp new file mode 100644 index 0000000..fafd153 --- /dev/null +++ b/include/sta/bus/uart/uart.hpp @@ -0,0 +1,25 @@ +#ifndef STA_CORE_UART_UART_HPP +#define STA_CORE_UART_UART_HPP + +#include +#include + +namespace sta +{ + class UART : public Interface + { + public: + UART(const UARTSettings & settings, Mutex * mutex=nullptr); + + /** + * @brief Get %UART interface settings. + * + * @return %UART settings + */ + const UARTSettings & settings(); + private: + UARTSettings settings_; /**< %UART settings */ + }; +} // namespace sta + +#endif // STA_CORE_UART_UART_HPP \ No newline at end of file diff --git a/include/sta/config.hpp b/include/sta/config.hpp index deed5f1..f2f4489 100644 --- a/include/sta/config.hpp +++ b/include/sta/config.hpp @@ -2,7 +2,7 @@ #define STA_CONFIG // Use the raspberry pi for this project. -#include +#include // #define DEBUG #define STA_PRINTF_USE_STDLIB diff --git a/include/sta/arduino/not_implemented.hpp b/include/sta/devices/arduino/not_implemented.hpp similarity index 100% rename from include/sta/arduino/not_implemented.hpp rename to include/sta/devices/arduino/not_implemented.hpp diff --git a/include/sta/devices/raspi/bus/i2c.hpp b/include/sta/devices/raspi/bus/i2c.hpp new file mode 100644 index 0000000..597b4bd --- /dev/null +++ b/include/sta/devices/raspi/bus/i2c.hpp @@ -0,0 +1,52 @@ +#ifndef STA_RASPI_I2C_HPP +#define STA_RASPI_I2C_HPP + +#include +#ifdef STA_PLATFORM_RASPI + +#include +#include + +#include +#include + +namespace sta +{ + enum class I2cNode { + DEV_1, + DEV_2 + }; + + class RaspiI2c : public I2c + { + public: + RaspiI2c(I2cNode node, Mutex * mutex=nullptr, bool persistent_open=false); + ~RaspiI2c(); + + void transfer(uint8_t value) override; + void transfer16(uint16_t value) override; + void transfer(const uint8_t * buffer, size_t size) override; + void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) override; + void receive(uint8_t * buffer, size_t size) override; + + void fill(uint8_t value, size_t count) override; + + void selectAddress(uint16_t address) override; + void acquire() override; + void release() override; + private: + char * i2cdev_; + int i2cfd_; + bool open_ = false; + const bool persistent_open_; + }; + + class RaspiI2cDevice : public I2cDevice + { + RaspiI2cDevice(I2c * intf, uint16_t address_10bit, Mutex* mutex=nullptr, bool master=false, bool blocking=true); + }; +} // namespace sta + +#endif // STA_PLATFORM_RASPI + +#endif // STA_I2C_HPP \ No newline at end of file diff --git a/include/sta/raspi/spi.hpp b/include/sta/devices/raspi/bus/spi.hpp similarity index 95% rename from include/sta/raspi/spi.hpp rename to include/sta/devices/raspi/bus/spi.hpp index 484debc..266a0cc 100644 --- a/include/sta/raspi/spi.hpp +++ b/include/sta/devices/raspi/bus/spi.hpp @@ -4,8 +4,8 @@ #include #ifdef STA_PLATFORM_RASPI -#include -#include +#include +#include namespace sta { diff --git a/include/sta/devices/raspi/can.hpp b/include/sta/devices/raspi/can.hpp new file mode 100644 index 0000000..e69de29 diff --git a/include/sta/raspi/delay.hpp b/include/sta/devices/raspi/delay.hpp similarity index 100% rename from include/sta/raspi/delay.hpp rename to include/sta/devices/raspi/delay.hpp diff --git a/include/sta/raspi/gpio_pin.hpp b/include/sta/devices/raspi/gpio_pin.hpp similarity index 96% rename from include/sta/raspi/gpio_pin.hpp rename to include/sta/devices/raspi/gpio_pin.hpp index 2e5e456..22af920 100644 --- a/include/sta/raspi/gpio_pin.hpp +++ b/include/sta/devices/raspi/gpio_pin.hpp @@ -4,7 +4,7 @@ // Only enable module on Raspi platform w/ HAL GPIO module enabled #include #ifdef STA_PLATFORM_RASPI -# include +# include # define STA_RASPI_GPIO_ENABLED #endif // STA_PLATFORM_RASPI diff --git a/include/sta/raspi/hal.hpp b/include/sta/devices/raspi/hal.hpp similarity index 100% rename from include/sta/raspi/hal.hpp rename to include/sta/devices/raspi/hal.hpp diff --git a/include/sta/raspi/mcu/common.hpp b/include/sta/devices/raspi/mcu/common.hpp similarity index 100% rename from include/sta/raspi/mcu/common.hpp rename to include/sta/devices/raspi/mcu/common.hpp diff --git a/include/sta/stm32/can.hpp b/include/sta/devices/stm32/can.hpp similarity index 100% rename from include/sta/stm32/can.hpp rename to include/sta/devices/stm32/can.hpp diff --git a/include/sta/stm32/clocks.hpp b/include/sta/devices/stm32/clocks.hpp similarity index 100% rename from include/sta/stm32/clocks.hpp rename to include/sta/devices/stm32/clocks.hpp diff --git a/include/sta/stm32/delay.hpp b/include/sta/devices/stm32/delay.hpp similarity index 100% rename from include/sta/stm32/delay.hpp rename to include/sta/devices/stm32/delay.hpp diff --git a/include/sta/stm32/gpio_pin.hpp b/include/sta/devices/stm32/gpio_pin.hpp similarity index 100% rename from include/sta/stm32/gpio_pin.hpp rename to include/sta/devices/stm32/gpio_pin.hpp diff --git a/include/sta/stm32/hal.hpp b/include/sta/devices/stm32/hal.hpp similarity index 100% rename from include/sta/stm32/hal.hpp rename to include/sta/devices/stm32/hal.hpp diff --git a/include/sta/stm32/i2c.hpp b/include/sta/devices/stm32/i2c.hpp similarity index 100% rename from include/sta/stm32/i2c.hpp rename to include/sta/devices/stm32/i2c.hpp diff --git a/include/sta/stm32/init.hpp b/include/sta/devices/stm32/init.hpp similarity index 100% rename from include/sta/stm32/init.hpp rename to include/sta/devices/stm32/init.hpp diff --git a/include/sta/stm32/mcu/STM32F411xE.hpp b/include/sta/devices/stm32/mcu/STM32F411xE.hpp similarity index 100% rename from include/sta/stm32/mcu/STM32F411xE.hpp rename to include/sta/devices/stm32/mcu/STM32F411xE.hpp diff --git a/include/sta/stm32/mcu/STM32F413xx.hpp b/include/sta/devices/stm32/mcu/STM32F413xx.hpp similarity index 100% rename from include/sta/stm32/mcu/STM32F413xx.hpp rename to include/sta/devices/stm32/mcu/STM32F413xx.hpp diff --git a/include/sta/stm32/mcu/common.hpp b/include/sta/devices/stm32/mcu/common.hpp similarity index 100% rename from include/sta/stm32/mcu/common.hpp rename to include/sta/devices/stm32/mcu/common.hpp diff --git a/include/sta/stm32/spi.hpp b/include/sta/devices/stm32/spi.hpp similarity index 100% rename from include/sta/stm32/spi.hpp rename to include/sta/devices/stm32/spi.hpp diff --git a/include/sta/stm32/uart.hpp b/include/sta/devices/stm32/uart.hpp similarity index 100% rename from include/sta/stm32/uart.hpp rename to include/sta/devices/stm32/uart.hpp diff --git a/include/sta/devices/template/delay.hpp b/include/sta/devices/template/delay.hpp new file mode 100644 index 0000000..a87bd66 --- /dev/null +++ b/include/sta/devices/template/delay.hpp @@ -0,0 +1,43 @@ +/** + * @file delay.hpp + * @author (@.com) + * @brief + * @version 0.1 + * @date 2023-06-13 + * + * @copyright Copyright (c) 2023 + * + * How to modify this file: + * - Ctrl + F and replace "YOUR_DEVICE" with the appropriate name. + */ +#ifndef STA_CORE_YOUR_DEVICE_DELAY_HPP +#define STA_CORE_YOUR_DEVICE_DELAY_HPP + +// Only enable module on YOUR_DEVICE platform +#include + + +#if defined(STA_PLATFORM_YOUR_DEVICE) || defined(DOXYGEN) + +#include + +namespace sta +{ + /** + * @brief Millisecond delay. + * + * @param ms Milliseconds + */ + void delayMs(uint32_t ms); + + /** + * @brief Microsecond delay. + * + * @param us Microseconds + */ + void delayUs(uint32_t us); +} // namespace sta + +#endif // STA_PLATFORM_YOUR_DEVICE + +#endif // STA_CORE_YOUR_DEVICE_DELAY_HPP \ No newline at end of file diff --git a/include/sta/i2c.hpp b/include/sta/i2c.hpp deleted file mode 100644 index db467dd..0000000 --- a/include/sta/i2c.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef STA_I2C_HPP -#define STA_I2C_HPP - -#include -#include - -namespace sta { - class I2cDevice { - protected: - uint16_t address; - Mutex* mutex; - bool master; - bool blocking; - public: - I2cDevice(uint16_t address_10bit, Mutex* mutex=nullptr, bool master=false, bool blocking=true); - - virtual bool transmit(uint8_t* data, uint16_t size) = 0; - - virtual bool receive(uint8_t* data, uint16_t size) = 0; - - virtual void acquire(); - - virtual void release(); - }; -} - -#endif // STA_I2C_HPP diff --git a/include/sta/lang.hpp b/include/sta/lang.hpp index 35591b7..0c68348 100644 --- a/include/sta/lang.hpp +++ b/include/sta/lang.hpp @@ -95,6 +95,14 @@ # define STA_UNREACHABLE() __builtin_unreachable() #endif // !STA_UNREACHABLE +/** + * @brief A macro for marking code as not implemented. Causes a program to + * crash with the appropriate error message if the code is executed. + */ +#ifndef STA_NOT_IMPLEMENTED +# define STA_NOT_IMPLEMENTED() throw "myFunction is not implemented yet."; +#endif // !STA_NOT_IMPLEMENTED + /** * @brief Silencing compiler warnings for intended switch case fallthrough. * diff --git a/include/sta/printable.hpp b/include/sta/printable.hpp index 4597a41..7925c8c 100644 --- a/include/sta/printable.hpp +++ b/include/sta/printable.hpp @@ -5,8 +5,6 @@ #ifndef STA_CORE_PRINTABLE_HPP #define STA_CORE_PRINTABLE_HPP -#include - #include #include @@ -25,165 +23,168 @@ namespace sta HEX /**< Hexadecimal */ }; - /** - * @brief Print single character. - * - * @param c Character to print - */ - void print(char c); + class Printable + { + /** + * @brief Print single character. + * + * @param c Character to print + */ + void print(char c); - /** - * @brief Print boolean value. - * - * @param b Boolean value - */ - void print(bool b); + /** + * @brief Print boolean value. + * + * @param b Boolean value + */ + void print(bool b); - /** - * @brief Print floating point value. - * - * @param d Floating point value - */ - void print(double d); + /** + * @brief Print floating point value. + * + * @param d Floating point value + */ + void print(double d); - /** - * @brief Print integer in selected base. - * - * @param num 8-bit unsigned integer - * @param base Integer base - */ - void print(uint8_t num, IntegerBase base = IntegerBase::DEC); + /** + * @brief Print integer in selected base. + * + * @param num 8-bit unsigned integer + * @param base Integer base + */ + void print(uint8_t num, IntegerBase base = IntegerBase::DEC); - /** - * @brief Print integer in selected base. - * - * @param num 16-bit unsigned integer - * @param base Integer base - */ - void print(uint16_t num, IntegerBase base = IntegerBase::DEC); + /** + * @brief Print integer in selected base. + * + * @param num 16-bit unsigned integer + * @param base Integer base + */ + void print(uint16_t num, IntegerBase base = IntegerBase::DEC); - /** - * @brief Print integer in selected base. - * - * @param num 32-bit unsigned integer - * @param base Integer base - */ - void print(uint32_t num, IntegerBase base = IntegerBase::DEC); + /** + * @brief Print integer in selected base. + * + * @param num 32-bit unsigned integer + * @param base Integer base + */ + void print(uint32_t num, IntegerBase base = IntegerBase::DEC); - /** - * @brief Print c-string. - * - * @param str Null terminated string - */ - void print(const char * str); + /** + * @brief Print c-string. + * + * @param str Null terminated string + */ + void print(const char * str); - /** - * @brief Print string. - * - * @param str String buffer - * @param length String length - */ - void print(const char * str, size_t length); + /** + * @brief Print string. + * + * @param str String buffer + * @param length String length + */ + virtual void print(const char * str, size_t length) = 0; - /** - * @brief Print new-line. - */ - void println(); + /** + * @brief Print new-line. + */ + void println(); - /** - * @brief Print single character followed by a new-line. - * - * @param c Character to print - */ - void println(char c); + /** + * @brief Print single character followed by a new-line. + * + * @param c Character to print + */ + void println(char c); - /** - * @brief Print boolean value followed by a new-line. - * - * @param b Boolean value - */ - void println(bool b); + /** + * @brief Print boolean value followed by a new-line. + * + * @param b Boolean value + */ + void println(bool b); - /** - * @brief Print floating point value followed by a new-line. - * - * @param d Floating point value - */ - void println(double d); + /** + * @brief Print floating point value followed by a new-line. + * + * @param d Floating point value + */ + void println(double d); - /** - * @brief Print integer in selected base followed by a new-line. - * - * @param num 8-bit unsigned integer - * @param base Integer base - */ - void println(uint8_t num, IntegerBase base = IntegerBase::DEC); + /** + * @brief Print integer in selected base followed by a new-line. + * + * @param num 8-bit unsigned integer + * @param base Integer base + */ + void println(uint8_t num, IntegerBase base = IntegerBase::DEC); - /** - * @brief Print integer in selected base followed by a new-line. - * - * @param num 16-bit unsigned integer - * @param base Integer base - */ - void println(uint16_t num, IntegerBase base = IntegerBase::DEC); + /** + * @brief Print integer in selected base followed by a new-line. + * + * @param num 16-bit unsigned integer + * @param base Integer base + */ + void println(uint16_t num, IntegerBase base = IntegerBase::DEC); - /** - * @brief Print integer in selected base followed by a new-line. - * - * @param num 32-bit unsigned integer - * @param base Integer base - */ - void println(uint32_t num, IntegerBase base = IntegerBase::DEC); + /** + * @brief Print integer in selected base followed by a new-line. + * + * @param num 32-bit unsigned integer + * @param base Integer base + */ + void println(uint32_t num, IntegerBase base = IntegerBase::DEC); - /** - * @brief Print c-string followed by a new-line. - * - * @param str Null terminated string - */ - void println(const char * str); + /** + * @brief Print c-string followed by a new-line. + * + * @param str Null terminated string + */ + void println(const char * str); - /** - * @brief Print string followed by a new-line. - * - * @param str String buffer - * @param length String length - */ - void println(const char * str, size_t length); + /** + * @brief Print string followed by a new-line. + * + * @param str String buffer + * @param length String length + */ + void println(const char * str, size_t length); - /** - * @brief Print unsigned integer in selected base. - * - * @param value Unsigned integer value - * @param base Integer base - * @param fmt printf format string for base 10 - * @param size Size of value in bytes - */ - void printBase(uintmax_t value, IntegerBase base, const char * fmt, size_t size); + private: + /** + * @brief Print unsigned integer in selected base. + * + * @param value Unsigned integer value + * @param base Integer base + * @param fmt printf format string for base 10 + * @param size Size of value in bytes + */ + void printBase(uintmax_t value, IntegerBase base, const char * fmt, size_t size); - /** - * @brief Print unsigned integer in base 10. - * - * @param value Unsigned integer value - * @param fmt printf format string - */ - void printDec(uintmax_t value, const char * fmt); + /** + * @brief Print unsigned integer in base 10. + * + * @param value Unsigned integer value + * @param fmt printf format string + */ + void printDec(uintmax_t value, const char * fmt); - /** - * @brief Print unsigned integer in base 2. - * - * @param value Unsigned integer value - * @param digits Number of digits to print - */ - void printBin(uintmax_t value, size_t digits); - - /** - * @brief Print unsigned integer in base 16. - * - * @param value Unsigned integer value - * @param digits Number of digits to print - */ - void printHex(uintmax_t value, size_t digits); + /** + * @brief Print unsigned integer in base 2. + * + * @param value Unsigned integer value + * @param digits Number of digits to print + */ + void printBin(uintmax_t value, size_t digits); + /** + * @brief Print unsigned integer in base 16. + * + * @param value Unsigned integer value + * @param digits Number of digits to print + */ + void printHex(uintmax_t value, size_t digits); + }; } // namespace sta diff --git a/include/sta/printable_uart.hpp b/include/sta/printable_uart.hpp index 9dfb3ee..7e78b0e 100644 --- a/include/sta/printable_uart.hpp +++ b/include/sta/printable_uart.hpp @@ -5,7 +5,8 @@ #ifndef STA_CORE_PRINTABLE_UART_HPP #define STA_CORE_PRINTABLE_UART_HPP -#include +#include +#include #include #include @@ -13,24 +14,12 @@ namespace sta { - /** - * @brief Integer representation. - * - * @ingroup sta_core - */ - enum class IntegerBase - { - DEC, /**< Decimal */ - BIN, /**< Binary */ - HEX /**< Hexadecimal */ - }; - /** * @brief Printable interface for UART. * * @ingroup sta_core */ - class PrintableUART + class PrintableUART : public Printable { public: /** @@ -38,149 +27,14 @@ namespace sta */ PrintableUART(UART * intf); - /** - * @brief Print single character. - * - * @param c Character to print - */ - void print(char c); - /** - * @brief Print boolean value. - * - * @param b Boolean value - */ - void print(bool b); - /** - * @brief Print floating point value. - * - * @param d Floating point value - */ - void print(double d); - /** - * @brief Print integer in selected base. - * - * @param num 8-bit unsigned integer - * @param base Integer base - */ - void print(uint8_t num, IntegerBase base = IntegerBase::DEC); - /** - * @brief Print integer in selected base. - * - * @param num 16-bit unsigned integer - * @param base Integer base - */ - void print(uint16_t num, IntegerBase base = IntegerBase::DEC); - /** - * @brief Print integer in selected base. - * - * @param num 32-bit unsigned integer - * @param base Integer base - */ - void print(uint32_t num, IntegerBase base = IntegerBase::DEC); - /** - * @brief Print c-string. - * - * @param str Null terminated string - */ - void print(const char * str); /** * @brief Print string. * * @param str String buffer * @param length String length */ - void print(const char * str, size_t length); - - - /** - * @brief Print new-line. - */ - void println(); - /** - * @brief Print single character followed by a new-line. - * - * @param c Character to print - */ - void println(char c); - /** - * @brief Print boolean value followed by a new-line. - * - * @param b Boolean value - */ - void println(bool b); - /** - * @brief Print floating point value followed by a new-line. - * - * @param d Floating point value - */ - void println(double d); - /** - * @brief Print integer in selected base followed by a new-line. - * - * @param num 8-bit unsigned integer - * @param base Integer base - */ - void println(uint8_t num, IntegerBase base = IntegerBase::DEC); - /** - * @brief Print integer in selected base followed by a new-line. - * - * @param num 16-bit unsigned integer - * @param base Integer base - */ - void println(uint16_t num, IntegerBase base = IntegerBase::DEC); - /** - * @brief Print integer in selected base followed by a new-line. - * - * @param num 32-bit unsigned integer - * @param base Integer base - */ - void println(uint32_t num, IntegerBase base = IntegerBase::DEC); - /** - * @brief Print c-string followed by a new-line. - * - * @param str Null terminated string - */ - void println(const char * str); - /** - * @brief Print string followed by a new-line. - * - * @param str String buffer - * @param length String length - */ - void println(const char * str, size_t length); - - private: - /** - * @brief Print unsigned integer in selected base. - * - * @param value Unsigned integer value - * @param base Integer base - * @param fmt printf format string for base 10 - * @param size Size of value in bytes - */ - void printBase(uintmax_t value, IntegerBase base, const char * fmt, size_t size); - /** - * @brief Print unsigned integer in base 10. - * - * @param value Unsigned integer value - * @param fmt printf format string - */ - void printDec(uintmax_t value, const char * fmt); - /** - * @brief Print unsigned integer in base 2. - * - * @param value Unsigned integer value - * @param digits Number of digits to print - */ - void printBin(uintmax_t value, size_t digits); - /** - * @brief Print unsigned integer in base 16. - * - * @param value Unsigned integer value - * @param digits Number of digits to print - */ - void printHex(uintmax_t value, size_t digits); - + void print(const char * str, size_t length) override; + private: UART * intf_; }; diff --git a/include/sta/uart.hpp b/include/sta/uart.hpp index 9667623..400f871 100644 --- a/include/sta/uart.hpp +++ b/include/sta/uart.hpp @@ -19,10 +19,10 @@ namespace sta { /** - * @brief Interface for %UART. - * - * @ingroup sta_core_uart - */ + * @brief Interface for %UART. + * + * @ingroup sta_core_uart + */ class UART { public: @@ -40,12 +40,14 @@ namespace sta * @param value Unsigned integer value */ void write(uint8_t value); + /** * @brief Write unsigned integer to %UART. * * @param value Unsigned integer value */ void write(uint16_t value); + /** * @brief Write unsigned integer to %UART. * diff --git a/src/bus/device.cpp b/src/bus/device.cpp new file mode 100644 index 0000000..fb2365f --- /dev/null +++ b/src/bus/device.cpp @@ -0,0 +1,77 @@ +#include +#include + +namespace sta +{ + Device::Device(Interface * intf) + : intf_{intf} + { + STA_ASSERT(intf != nullptr); + } + + void Device::beginTransmission() + { + intf_->acquire(); + select(); + selected_ = true; + } + + void Device::endTransmission() + { + deselect(); + selected_ = false; + intf_->release(); + } + + void Device::transfer(uint8_t value) + { + STA_ASSERT(intf_->isAquired()); + STA_ASSERT(selected_); + + intf_->transfer(value); + } + + void Device::transfer16(uint16_t value) + { + STA_ASSERT(intf_->isAquired()); + STA_ASSERT(selected_); + + intf_->transfer16(value); + } + + void Device::transfer(const uint8_t * buffer, size_t size) + { + STA_ASSERT(intf_->isAquired()); + STA_ASSERT(selected_); + STA_ASSERT(buffer != nullptr); + + intf_->transfer(buffer, size); + } + + void Device::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) + { + STA_ASSERT(intf_->isAquired()); + STA_ASSERT(selected_); + STA_ASSERT(txBuffer != nullptr); + STA_ASSERT(rxBuffer != nullptr); + + intf_->transfer(txBuffer, rxBuffer, size); + } + + void Device::receive(uint8_t * buffer, size_t size) + { + STA_ASSERT(intf_->isAquired()); + STA_ASSERT(selected_); + STA_ASSERT(buffer != nullptr); + + intf_->receive(buffer, size); + } + + void Device::fill(uint8_t value, size_t count) + { + STA_ASSERT(intf_->isAquired()); + STA_ASSERT(selected_); + + intf_->fill(value, count); + } +} // namespace sta diff --git a/src/bus/i2c/device.cpp b/src/bus/i2c/device.cpp new file mode 100644 index 0000000..dc54795 --- /dev/null +++ b/src/bus/i2c/device.cpp @@ -0,0 +1,23 @@ +#include + +#include + + +namespace sta +{ + I2cDevice::I2cDevice(I2c * intf, int addr) + : Device{intf}, addr_{addr} + { + STA_ASSERT(intf != nullptr); + } + + void I2cDevice::select() + { + // TODO: Implement address selection here? + } + + void I2cDevice::deselect() + { + // TODO: Implement address deselection here? + } +} // namespace sta diff --git a/src/bus/i2c/i2c.cpp b/src/bus/i2c/i2c.cpp new file mode 100644 index 0000000..db7d91f --- /dev/null +++ b/src/bus/i2c/i2c.cpp @@ -0,0 +1,11 @@ +#include + + +namespace sta +{ + I2c::I2c(Mutex * mutex=nullptr) + : Interface{mutex} + { + + } +} // namespace sta \ No newline at end of file diff --git a/src/bus/interface.cpp b/src/bus/interface.cpp new file mode 100644 index 0000000..a727dee --- /dev/null +++ b/src/bus/interface.cpp @@ -0,0 +1,29 @@ +#include + +#include + +namespace sta +{ + Interface::Interface(Mutex * mutex) + : mutex_{mutex} + { + STA_ASSERT(mutex != nullptr); + } + + void Interface::acquire() + { + if (mutex_ != nullptr) + mutex_->acquire(); + } + + void Interface::release() + { + if (mutex_ != nullptr) + mutex_->release(); + } + + bool Interface::isAquired() + { + return aquired_; + } +} // namespace sta diff --git a/src/bus/spi/device.cpp b/src/bus/spi/device.cpp new file mode 100644 index 0000000..29c25b2 --- /dev/null +++ b/src/bus/spi/device.cpp @@ -0,0 +1,29 @@ +#include + +#include + + +namespace sta +{ + SPIDevice::SPIDevice(SPI * intf, GpioPin * csPin) + : Device{intf}, intf_{intf}, csPin_{csPin} + { + STA_ASSERT(intf != nullptr); + STA_ASSERT(csPin != nullptr); + } + + const SPISettings & SPIDevice::settings() const + { + return intf_->settings(); + } + + void SPIDevice::select() + { + csPin_->setState(GpioPinState::GPIO_LOW); + } + + void SPIDevice::deselect() + { + csPin_->setState(GpioPinState::GPIO_HIGH); + } +} // namespace sta diff --git a/src/spi/settings.cpp b/src/bus/spi/settings.cpp similarity index 97% rename from src/spi/settings.cpp rename to src/bus/spi/settings.cpp index 7d76aa0..d0c9246 100644 --- a/src/spi/settings.cpp +++ b/src/bus/spi/settings.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/src/bus/spi/spi.cpp b/src/bus/spi/spi.cpp new file mode 100644 index 0000000..012bcf3 --- /dev/null +++ b/src/bus/spi/spi.cpp @@ -0,0 +1,17 @@ +#include + +#include + +namespace sta +{ + SPI::SPI(const SPISettings & settings, Mutex * mutex /* = nullptr */) + : Interface{mutex}, settings_{settings} + { + + } + + const SPISettings & SPI::settings() + { + return settings_; + } +} // namespace sta diff --git a/src/bus/uart/settings.cpp b/src/bus/uart/settings.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/bus/uart/uart.cpp b/src/bus/uart/uart.cpp new file mode 100644 index 0000000..6225576 --- /dev/null +++ b/src/bus/uart/uart.cpp @@ -0,0 +1,15 @@ +#include + +namespace sta +{ + UART::UART(const UARTSettings & settings, Mutex * mutex) + : Interface{mutex}, settings_{settings} + { + + } + + const UARTSettings & UART::settings() + { + return settings_; + } +} // namespace sta diff --git a/src/devices/raspi/bus/i2c/i2c.cpp b/src/devices/raspi/bus/i2c/i2c.cpp new file mode 100644 index 0000000..7be071f --- /dev/null +++ b/src/devices/raspi/bus/i2c/i2c.cpp @@ -0,0 +1,129 @@ +#include + +#ifdef STA_PLATFORM_RASPI + +#include + +#include +#include +#include + +#include +#include +#include +#include + + +namespace sta +{ + RaspiI2c::RaspiI2c(I2cNode node, Mutex * mutex, bool persistent_open) + : I2c{mutex}, persistent_open_{persistent_open} + { + // Safer version of malloc + strcpy + i2cdev_ = strdup(node == I2cNode::DEV_1 ? "/dev/i2c-1" : "/dev/i2c-2"); + + STA_ASSERT(i2cdev_ != nullptr); + } + + RaspiI2c::~RaspiI2c() + { + if (i2cdev_ != NULL ) { + free(i2cdev_); + i2cdev_ = NULL; + } + + if (open_) { + close(i2cfd_); + } + } + + void RaspiI2c::transfer(uint8_t value) + { + STA_ASSERT(open_); + + write(i2cfd_, &value, 1); + } + + void RaspiI2c::transfer16(uint16_t value) + { + STA_ASSERT(open_); + + write(i2cfd_, &value, 2); + } + + void RaspiI2c::transfer(const uint8_t * buffer, size_t size) + { + STA_ASSERT(open_); + + write(i2cfd_, buffer, size); + } + + void RaspiI2c::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) + { + STA_ASSERT(open_); + + // TODO: Is this even possible in i2c? + } + + void RaspiI2c::receive(uint8_t * buffer, size_t size) + { + STA_ASSERT(open_); + + if (read(i2cfd_, buffer, size) <= 0) + { + printf("Error while reading i2c bus."); + } + } + + void RaspiI2c::fill(uint8_t value, size_t count) + { + STA_ASSERT(open_); + + // Initialize a buffer of size count and fill it with the value. + uint8_t *buffer = new uint8_t[count]; + memset(buffer, value, count); + + write(i2cfd_, buffer, count); + + delete [] buffer; + } + + void RaspiI2c::selectAddress(uint16_t address) + { + if (ioctl(i2cfd_, I2C_SLAVE, address) < 0) + { + printf("Failed to send the slave address."); + } + } + + void RaspiI2c::acquire() + { + I2c::acquire(); + + if (open_) { + return; + } + + i2cfd_ = open(i2cdev_, O_RDWR); + open_ = true; + + STA_ASSERT(i2cfd_ >= 0); + } + + void RaspiI2c::release() + { + if (!persistent_open_ && open_) { + close(i2cfd_); + } + + I2c::release(); + } + + RaspiI2cDevice::RaspiI2cDevice(I2c * intf, uint16_t address_10bit, Mutex* mutex, bool master, bool blocking) + : I2cDevice { intf, address_10bit } + { + + } +} // namespace sta + +#endif // STA_PLATFORM_RASPI \ No newline at end of file diff --git a/src/raspi/spi.cpp b/src/devices/raspi/bus/spi/spi.cpp similarity index 97% rename from src/raspi/spi.cpp rename to src/devices/raspi/bus/spi/spi.cpp index 0d9d92f..74f71f1 100644 --- a/src/raspi/spi.cpp +++ b/src/devices/raspi/bus/spi/spi.cpp @@ -1,9 +1,9 @@ -#include +#include #ifdef STA_PLATFORM_RASPI -#include -#include +#include +#include #include #include #include @@ -94,6 +94,10 @@ namespace sta int result = ioctl(spifd_, SPI_IOC_MESSAGE(1), spi_message); + if (result < 0) { + printf("Sending failed with error '%s'! \n", strerror(errno)); + } + STA_DEBUG_IOCTL_SEND(result); } @@ -293,8 +297,6 @@ namespace sta void RaspiSPI::release() { - STA_ASSERT_MSG(open_, "'release' was called despite the device being closed! This has to be a bug!"); - if (!persistent_open_ && open_) { close(spifd_); } diff --git a/src/raspi/delay.cpp b/src/devices/raspi/delay.cpp similarity index 78% rename from src/raspi/delay.cpp rename to src/devices/raspi/delay.cpp index 0cbb495..3bbc5f1 100644 --- a/src/raspi/delay.cpp +++ b/src/devices/raspi/delay.cpp @@ -1,7 +1,7 @@ -#include +#include #ifdef STA_PLATFORM_RASPI -#include +#include #include #include diff --git a/src/raspi/gpio_pin.cpp b/src/devices/raspi/gpio_pin.cpp similarity index 100% rename from src/raspi/gpio_pin.cpp rename to src/devices/raspi/gpio_pin.cpp diff --git a/src/stm32/can.cpp b/src/devices/stm32/can.cpp similarity index 100% rename from src/stm32/can.cpp rename to src/devices/stm32/can.cpp diff --git a/src/stm32/delay.cpp b/src/devices/stm32/delay.cpp similarity index 100% rename from src/stm32/delay.cpp rename to src/devices/stm32/delay.cpp diff --git a/src/stm32/gpio_pin.cpp b/src/devices/stm32/gpio_pin.cpp similarity index 100% rename from src/stm32/gpio_pin.cpp rename to src/devices/stm32/gpio_pin.cpp diff --git a/src/stm32/i2c.cpp b/src/devices/stm32/i2c.cpp similarity index 100% rename from src/stm32/i2c.cpp rename to src/devices/stm32/i2c.cpp diff --git a/src/stm32/init.cpp b/src/devices/stm32/init.cpp similarity index 100% rename from src/stm32/init.cpp rename to src/devices/stm32/init.cpp diff --git a/src/stm32/spi.cpp b/src/devices/stm32/spi.cpp similarity index 100% rename from src/stm32/spi.cpp rename to src/devices/stm32/spi.cpp diff --git a/src/stm32/uart.cpp b/src/devices/stm32/uart.cpp similarity index 92% rename from src/stm32/uart.cpp rename to src/devices/stm32/uart.cpp index 094b34e..d377cd5 100644 --- a/src/stm32/uart.cpp +++ b/src/devices/stm32/uart.cpp @@ -1,4 +1,4 @@ -#include +#include #ifdef STA_STM32_UART_ENABLED #include diff --git a/src/devices/template/delay.cpp b/src/devices/template/delay.cpp new file mode 100644 index 0000000..18edb34 --- /dev/null +++ b/src/devices/template/delay.cpp @@ -0,0 +1,36 @@ +/** + * @file delay.cpp + * @author (@.com) + * @brief + * @version 0.1 + * @date 2023-06-13 + * + * @copyright Copyright (c) 2023 + * + * How to modify this file: + * - Ctrl + F and replace "YOUR_DEVICE" with the appropriate name. + * - Implement the functions delayMs and delayUs. + * - Remove the import if no longer needed. + */ + +#define STA_PLATFORM_YOUR_DEVICE + +#include +#ifdef STA_PLATFORM_YOUR_DEVICE + +#include + +namespace sta +{ + void delayMs(uint32_t ms) + { + STA_NOT_IMPLEMENTED(); + } + + void delayUs(uint32_t us) + { + STA_NOT_IMPLEMENTED(); + } +} // namespace sta + +#endif // STA_PLATFORM_YOUR_DEVICE \ No newline at end of file diff --git a/src/i2c.cpp b/src/i2c.cpp deleted file mode 100644 index 0f32c26..0000000 --- a/src/i2c.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include - -namespace sta { - I2cDevice::I2cDevice(uint16_t address_7bit, Mutex* mutex, bool master, bool blocking) { - this->address = address_7bit << 1; - this->mutex = mutex; - this->master = master; - this->blocking = blocking; - } - - void I2cDevice::acquire() { - if (this->mutex != nullptr) { - mutex->acquire(); - } - } - - void I2cDevice::release() { - if (this->mutex != nullptr) { - mutex->release(); - } - } -} diff --git a/src/printable.cpp b/src/printable.cpp index 3bc8325..4031032 100644 --- a/src/printable.cpp +++ b/src/printable.cpp @@ -2,136 +2,184 @@ #include #include +#include + #include #include namespace sta { - void print(char c) + void Printable::print(char c) { print(&c, 1); } - void print(bool b) + void Printable::print(bool b) { print(b ? "true" : "false"); } - void print(double d) + void Printable::print(double d) { char buffer[64]; snprintf(buffer, sizeof(buffer), "%f", d); print(buffer); } - void print(uint8_t num, IntegerBase base) + void Printable::print(uint8_t num, IntegerBase base) { printBase(num, base, "%" PRIu8, sizeof(num)); } - void print(uint16_t num, IntegerBase base) + void Printable::print(uint16_t num, IntegerBase base) { printBase(num, base, "%" PRIu16, sizeof(num)); } - void print(uint32_t num, IntegerBase base) + void Printable::print(uint32_t num, IntegerBase base) { printBase(num, base, "%" PRIu32, sizeof(num)); } - void print(const char * str) + void Printable::print(const char * str) { print(str, strlen(str)); } - - void print(const char * str, size_t length) { - print(str, length); - } - - void println() + + void Printable::println() { print("\r\n", 2); } - void println(char c) + void Printable::println(char c) { print(&c, 1); println(); } - void println(bool b) + void Printable::println(bool b) { print(b); println(); } - void println(double d) + void Printable::println(double d) { print(d); println(); } - void println(uint8_t num, IntegerBase base) + void Printable::println(uint8_t num, IntegerBase base) { print(num, base); println(); } - void println(uint16_t num, IntegerBase base) + void Printable::println(uint16_t num, IntegerBase base) { print(num, base); println(); } - void println(uint32_t num, IntegerBase base) + void Printable::println(uint32_t num, IntegerBase base) { print(num, base); println(); } - void println(const char * str) + void Printable::println(const char * str) { println(str, strlen(str)); } - void println(const char * str, size_t length) + void Printable::println(const char * str, size_t length) { print(str, length); println(); } - void printBase(uintmax_t value, IntegerBase base, const char * fmt, size_t size) + void Printable::printBase(uintmax_t num, IntegerBase base, const char * fmt, size_t size) { switch (base) { case IntegerBase::DEC: - printDec(value, fmt); - break; - case IntegerBase::HEX: - printHex(value, size); + printDec(num, fmt); break; + case IntegerBase::BIN: - printBin(value, size); + // Digits in base 2 = size in bytes * 8 + printBin(num, size * 8); break; + + case IntegerBase::HEX: + // Digits in base 16 = size in bytes * 2 + printHex(num, size * 2); + break; + default: - STA_ASSERT_MSG(false, "Case for IntegerBase enum not handled"); - STA_UNREACHABLE(); + print(""); } } - void printDec(uintmax_t value, const char * fmt) + void Printable::printDec(uintmax_t num, const char * fmt) { - printf(fmt, value); + char buffer[64]; + snprintf(buffer, sizeof(buffer), fmt, static_cast(num)); + print(buffer); } - void printBin(uintmax_t value, size_t digits) + void Printable::printBin(uintmax_t value, size_t digits) { + // Need 8 digits for every byte + char buffer[sizeof(value) * 8]; + // Check bounds + if (digits > sizeof(buffer)) + { + print(""); + return; + } + // Nothing to do + if (digits == 0) + return; + + for (size_t i = 0; i < digits; ++i) + { + // Convert bit to '0' or '1' + // First digit in buffer is MSB in value, so shift from high to low + buffer[i] = '0' + ((value >> (digits - 1 - i)) & 0x1); + } + + print(buffer, digits); } - void printHex(uintmax_t value, size_t digits) + void Printable::printHex(uintmax_t value, size_t digits) { - printf("%x", value); + // Need 2 digits for every byte + char buffer[sizeof(value) * 2]; + + // Check bounds + if (digits > sizeof(buffer)) + { + print(""); + return; + } + // Nothing to do + if (digits == 0) + return; + + for (size_t i = 0; i < digits; ++i) + { + // Convert 4 bits to hex + // First digit in buffer is 4 MSBs in value, so shift from high to low + uint8_t hex = ((value >> ((digits - 1 - i) * 4)) & 0xF); + if (hex > 9) + buffer[i] = 'A' + (hex - 10); + else + buffer[i] = '0' + hex; + } + + print(buffer, digits); } } // namespace sta \ No newline at end of file diff --git a/src/printable_uart.cpp b/src/printable_uart.cpp index fbc48cf..5a6ae2e 100644 --- a/src/printable_uart.cpp +++ b/src/printable_uart.cpp @@ -7,194 +7,19 @@ #include - namespace sta { + PrintableUART::PrintableUART(UART * intf) : intf_{intf} { STA_ASSERT(intf != nullptr); - } - - - void PrintableUART::print(char c) - { - print(&c, 1); - } - - void PrintableUART::print(bool b) - { - print(b ? "true" : "false"); - } - - void PrintableUART::print(double d) - { - char buffer[64]; - snprintf(buffer, sizeof(buffer), "%f", d); - print(buffer); - } - - void PrintableUART::print(uint8_t num, IntegerBase base /* = IntegerBase::DEC */) - { - printBase(num, base, "%" PRIu8, sizeof(num)); - } - - void PrintableUART::print(uint16_t num, IntegerBase base /* = IntegerBase::DEC */) - { - printBase(num, base, "%" PRIu16, sizeof(num)); - } - - void PrintableUART::print(uint32_t num, IntegerBase base /* = IntegerBase::DEC */) - { - printBase(num, base, "%" PRIu32, sizeof(num)); - } - - void PrintableUART::print(const char * str) - { - print(str, strlen(str)); + STA_ASSERT(intf->settings().mode == UARTMode::RX || intf->settings().mode == UARTMode::RX_TX); } void PrintableUART::print(const char * str, size_t length) { - intf_->write(reinterpret_cast(str), length); + intf_->transfer(reinterpret_cast(str), length); } - - void PrintableUART::println() - { - print("\r\n", 2); - } - - void PrintableUART::println(char c) - { - print(&c, 1); - println(); - } - - void PrintableUART::println(bool b) - { - print(b); - println(); - } - - void PrintableUART::println(double d) - { - print(d); - println(); - } - - void PrintableUART::println(uint8_t num, IntegerBase base /* = IntegerBase::DEC */) - { - print(num, base); - println(); - } - - void PrintableUART::println(uint16_t num, IntegerBase base /* = IntegerBase::DEC */) - { - print(num, base); - println(); - } - - void PrintableUART::println(uint32_t num, IntegerBase base /* = IntegerBase::DEC */) - { - print(num, base); - println(); - } - - void PrintableUART::println(const char * str) - { - println(str, strlen(str)); - } - - void PrintableUART::println(const char * str, size_t length) - { - print(str, length); - println(); - } - - - - void PrintableUART::printBase(uintmax_t num, IntegerBase base, const char * fmt, size_t size) - { - switch (base) - { - case IntegerBase::DEC: - printDec(num, fmt); - break; - - case IntegerBase::BIN: - // Digits in base 2 = size in bytes * 8 - printBin(num, size * 8); - break; - - case IntegerBase::HEX: - // Digits in base 16 = size in bytes * 2 - printHex(num, size * 2); - break; - - default: - print(""); - } - } - - void PrintableUART::printDec(uintmax_t num, const char * fmt) - { - char buffer[64]; - snprintf(buffer, sizeof(buffer), fmt, static_cast(num)); - print(buffer); - } - - void PrintableUART::printBin(uintmax_t value, size_t digits) - { - // Need 8 digits for every byte - char buffer[sizeof(value) * 8]; - - // Check bounds - if (digits > sizeof(buffer)) - { - print(""); - return; - } - // Nothing to do - if (digits == 0) - return; - - for (size_t i = 0; i < digits; ++i) - { - // Convert bit to '0' or '1' - // First digit in buffer is MSB in value, so shift from high to low - buffer[i] = '0' + ((value >> (digits - 1 - i)) & 0x1); - } - - print(buffer, digits); - } - - void PrintableUART::printHex(uintmax_t value, size_t digits) - { - // Need 2 digits for every byte - char buffer[sizeof(value) * 2]; - - // Check bounds - if (digits > sizeof(buffer)) - { - print(""); - return; - } - // Nothing to do - if (digits == 0) - return; - - for (size_t i = 0; i < digits; ++i) - { - // Convert 4 bits to hex - // First digit in buffer is 4 MSBs in value, so shift from high to low - uint8_t hex = ((value >> ((digits - 1 - i) * 4)) & 0xF); - if (hex > 9) - buffer[i] = 'A' + (hex - 10); - else - buffer[i] = '0' + hex; - } - - print(buffer, digits); - } } // namespace sta diff --git a/src/spi/device.cpp b/src/spi/device.cpp deleted file mode 100644 index 86a5210..0000000 --- a/src/spi/device.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include - -#include - - -namespace sta -{ - SPIDevice::SPIDevice(SPI * intf, GpioPin * csPin) - : intf_{intf}, csPin_{csPin} - { - STA_ASSERT(intf != nullptr); - STA_ASSERT(csPin != nullptr); - } - - void SPIDevice::beginTransmission() - { - // Acquire SPI access and activate device - intf_->acquire(); - select(); - } - - void SPIDevice::endTransmission() - { - // Deactivate device and release SPI access - deselect(); - intf_->release(); - } - - - // Forward I/O operations to SPI interface - - void SPIDevice::transfer(uint8_t data) - { - intf_->transfer(data); - } - - void SPIDevice::transfer16(uint16_t data) - { - intf_->transfer16(data); - } - - void SPIDevice::transfer(const uint8_t * buffer, size_t size) - { - STA_ASSERT(buffer != nullptr); - - intf_->transfer(buffer, size); - } - - void SPIDevice::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) - { - STA_ASSERT(txBuffer != nullptr); - STA_ASSERT(rxBuffer != nullptr); - STA_ASSERT(size != 0); - - intf_->transfer(txBuffer, rxBuffer, size); - } - - void SPIDevice::receive(uint8_t * buffer, size_t size) - { - STA_ASSERT(buffer != nullptr); - - intf_->receive(buffer, size); - } - - void SPIDevice::fill(uint8_t value, size_t count) - { - STA_ASSERT(count != 0); - - intf_->fill(value, count); - } - - - const SPISettings & SPIDevice::settings() const - { - return intf_->settings(); - } - - - void SPIDevice::select() - { - csPin_->setState(GpioPinState::GPIO_LOW); - } - - void SPIDevice::deselect() - { - csPin_->setState(GpioPinState::GPIO_HIGH); - } -} // namespace sta diff --git a/src/spi/spi.cpp b/src/spi/spi.cpp deleted file mode 100644 index 621086f..0000000 --- a/src/spi/spi.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include - -#include - -namespace sta -{ - SPI::SPI(const SPISettings & settings, Mutex * mutex /* = nullptr */) - : settings_{settings}, mutex_{mutex} - {} - - const SPISettings & SPI::settings() - { - return settings_; - } - - void SPI::acquire() - { - if (mutex_ != nullptr) - mutex_->acquire(); - } - - void SPI::release() - { - if (mutex_ != nullptr) - mutex_->release(); - } -} // namespace sta diff --git a/src/uart.cpp b/src/uart.cpp index 87eadef..dd0f69f 100644 --- a/src/uart.cpp +++ b/src/uart.cpp @@ -6,7 +6,6 @@ #include - namespace sta { void UART::write(uint8_t value) From e7ddbbf365f061abbc940588ecb5b42cc43d7ec3 Mon Sep 17 00:00:00 2001 From: dvdb97 Date: Sat, 24 Jun 2023 15:44:56 +0200 Subject: [PATCH 10/22] Fixed broken imports after dir structure rework & added stm32 i2c --- include/sta/bus/i2c/device.hpp | 13 +- include/sta/bus/i2c/i2c.hpp | 20 ++- include/sta/bus/interface.hpp | 2 +- include/sta/config.hpp | 11 -- include/sta/devices/stm32/bus/i2c.hpp | 44 +++++++ include/sta/devices/stm32/can.hpp | 2 +- include/sta/devices/stm32/clocks.hpp | 2 +- include/sta/devices/stm32/gpio_pin.hpp | 2 +- include/sta/devices/stm32/i2c.hpp | 39 ------ include/sta/devices/stm32/mcu/STM32F411xE.hpp | 2 +- include/sta/devices/stm32/spi.hpp | 2 +- include/sta/devices/stm32/uart.hpp | 2 +- include/sta/lang.hpp | 2 +- src/bus/i2c/device.cpp | 13 +- src/bus/i2c/i2c.cpp | 11 +- src/can/id.cpp | 2 +- src/can/iter.cpp | 2 +- src/devices/raspi/gpio_pin.cpp | 4 +- src/devices/stm32/bus/i2c.cpp | 122 ++++++++++++++++++ src/devices/stm32/can.cpp | 2 +- src/devices/stm32/delay.cpp | 6 +- src/devices/stm32/gpio_pin.cpp | 4 +- src/devices/stm32/i2c.cpp | 57 -------- src/devices/stm32/init.cpp | 2 +- src/devices/stm32/spi.cpp | 2 +- src/devices/template/delay.cpp | 4 +- 26 files changed, 224 insertions(+), 150 deletions(-) delete mode 100644 include/sta/config.hpp create mode 100644 include/sta/devices/stm32/bus/i2c.hpp delete mode 100644 include/sta/devices/stm32/i2c.hpp create mode 100644 src/devices/stm32/bus/i2c.cpp delete mode 100644 src/devices/stm32/i2c.cpp diff --git a/include/sta/bus/i2c/device.hpp b/include/sta/bus/i2c/device.hpp index 2a96b4d..88485b3 100644 --- a/include/sta/bus/i2c/device.hpp +++ b/include/sta/bus/i2c/device.hpp @@ -11,22 +11,25 @@ namespace sta * * @ingroup sta_core_i2c */ - class I2cDevice : public Device + class I2CDevice : public Device { public: /** * @param intf %I2C hardware interface * @param csPin The peripheral's address. */ - I2cDevice(I2c * intf, int addr); + I2CDevice(I2C * intf, int address, bool master=false, bool blocking=true); protected: void select() override; void deselect() override; - + private: - int addr_; /**< device address */ + I2C * intf_; + int address_; /**< device address */ + int master_; + int blocking_; }; } // namespace sta @@ -34,4 +37,4 @@ namespace sta -#endif // STA_CORE_I2C_DEVICE_HPP \ No newline at end of file +#endif // STA_CORE_I2C_DEVICE_HPP diff --git a/include/sta/bus/i2c/i2c.hpp b/include/sta/bus/i2c/i2c.hpp index 8d3123a..e2e37c4 100644 --- a/include/sta/bus/i2c/i2c.hpp +++ b/include/sta/bus/i2c/i2c.hpp @@ -16,18 +16,24 @@ namespace sta * * @ingroup sta_core_i2c */ - class I2c : public Interface + class I2C : public Interface { public: - I2c(Mutex * mutex=nullptr); + I2C(Mutex * mutex=nullptr); /** - * @brief Address selection for the I2C bus. - * - * @param address The address to select. + * @brief Specify the mode of communication via the bus. + * + * @param master + * @param blocking */ - virtual void selectAddress(uint16_t address) = 0; + void setSettings(uint16_t address, bool master, bool blocking); + + protected: + uint16_t address_; + bool master_; + bool blocking_; }; } // namespace sta -#endif // STA_CORE_I2C_I2C_HPP \ No newline at end of file +#endif // STA_CORE_I2C_I2C_HPP diff --git a/include/sta/bus/interface.hpp b/include/sta/bus/interface.hpp index a8581d8..a26151a 100644 --- a/include/sta/bus/interface.hpp +++ b/include/sta/bus/interface.hpp @@ -83,4 +83,4 @@ namespace sta } // namespace sta -#endif // STA_CORE_BUS_SERIAL_INTERFACE_HPP \ No newline at end of file +#endif // STA_CORE_BUS_SERIAL_INTERFACE_HPP diff --git a/include/sta/config.hpp b/include/sta/config.hpp deleted file mode 100644 index f2f4489..0000000 --- a/include/sta/config.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef STA_CONFIG -#define STA_CONFIG - -// Use the raspberry pi for this project. -#include - -// #define DEBUG -#define STA_PRINTF_USE_STDLIB -#define STA_DEBUG_PRINTF - -#endif \ No newline at end of file diff --git a/include/sta/devices/stm32/bus/i2c.hpp b/include/sta/devices/stm32/bus/i2c.hpp new file mode 100644 index 0000000..be939d2 --- /dev/null +++ b/include/sta/devices/stm32/bus/i2c.hpp @@ -0,0 +1,44 @@ +#ifndef STA_CORE_STM32_I2C_HPP +#define STA_CORE_STM32_I2C_HPP + +#include +#ifdef STA_PLATFORM_STM32 +# include +# ifdef HAL_I2C_MODULE_ENABLED +# define STA_STM32_I2C_ENABLED +# endif // HAL_SPI_MODULE_ENABLED +#endif // STA_PLATFORM_STM32 + +#ifdef STA_STM32_I2C_ENABLED + +#include +#include + +namespace sta +{ + class STM32I2C : public I2C + { + public: + STM32I2C(I2C_HandleTypeDef * handle, Mutex * mutex=nullptr); + + void transfer(uint8_t value) override; + void transfer16(uint16_t value) override; + void transfer(const uint8_t * buffer, size_t size) override; + void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) override; + void receive(uint8_t * buffer, size_t size) override; + + void fill(uint8_t value, size_t count) override; + private: + I2C_HandleTypeDef * handle_; + const uint32_t timeout_ = HAL_MAX_DELAY; + }; + + class STM32I2CDevice : public I2CDevice + { + STM32I2CDevice(); + }; +} + +#endif // STA_STM32_I2C_ENABLED + +#endif // STA_CORE_STM32_I2C_HPP diff --git a/include/sta/devices/stm32/can.hpp b/include/sta/devices/stm32/can.hpp index 2bf910c..8eb5d70 100644 --- a/include/sta/devices/stm32/can.hpp +++ b/include/sta/devices/stm32/can.hpp @@ -19,7 +19,7 @@ // Only enable module on STM32 platform w/ HAL CAN module enabled #include #ifdef STA_PLATFORM_STM32 -# include +# include # ifdef HAL_CAN_MODULE_ENABLED # define STA_STM32_CAN_ENABLED # endif // HAL_CAN_MODULE_ENABLED diff --git a/include/sta/devices/stm32/clocks.hpp b/include/sta/devices/stm32/clocks.hpp index a556fb5..df4604e 100644 --- a/include/sta/devices/stm32/clocks.hpp +++ b/include/sta/devices/stm32/clocks.hpp @@ -12,7 +12,7 @@ #if defined(STA_PLATFORM_STM32) || defined(DOXYGEN) -#include +#include /** diff --git a/include/sta/devices/stm32/gpio_pin.hpp b/include/sta/devices/stm32/gpio_pin.hpp index f6f355e..ee87f2d 100644 --- a/include/sta/devices/stm32/gpio_pin.hpp +++ b/include/sta/devices/stm32/gpio_pin.hpp @@ -9,7 +9,7 @@ // Only enable module on STM32 platform w/ HAL GPIO module enabled #include #ifdef STA_PLATFORM_STM32 -# include +# include # ifdef HAL_GPIO_MODULE_ENABLED # define STA_STM32_GPIO_ENABLED # endif // HAL_GPIO_MODULE_ENABLED diff --git a/include/sta/devices/stm32/i2c.hpp b/include/sta/devices/stm32/i2c.hpp deleted file mode 100644 index 4804e1a..0000000 --- a/include/sta/devices/stm32/i2c.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef STA_STM32_I2C_HPP -#define STA_STM32_I2C_HPP - -#include -#ifdef STA_PLATFORM_STM32 -# include -# ifdef HAL_I2C_MODULE_ENABLED -# define STA_STM32_I2C_ENABLED -# endif // HAL_SPI_MODULE_ENABLED -#endif // STA_PLATFORM_STM32 - -#ifdef STA_STM32_I2C_ENABLED - -#include - -namespace sta { - class STM32I2cDevice : public I2cDevice { - private: - I2C_HandleTypeDef* i2cHandle; - const uint32_t timeout = HAL_MAX_DELAY; - - public: - STM32I2cDevice( - I2C_HandleTypeDef* i2cHandle, - uint16_t address, - Mutex* mutex=nullptr, - bool master=false, - bool blocking=true - ); - - bool transmit(uint8_t* data, uint16_t size) override; - bool receive(uint8_t* data, uint16_t size) override; - - bool deviceReady(); - }; -} - -#endif // STA_STM32_I2C_ENABLED -#endif // STA_STM32_I2C_HPP diff --git a/include/sta/devices/stm32/mcu/STM32F411xE.hpp b/include/sta/devices/stm32/mcu/STM32F411xE.hpp index 838408d..cdca7c2 100644 --- a/include/sta/devices/stm32/mcu/STM32F411xE.hpp +++ b/include/sta/devices/stm32/mcu/STM32F411xE.hpp @@ -11,7 +11,7 @@ #endif // !STM32F411xE -#include +#include // Peripheral clock mappings diff --git a/include/sta/devices/stm32/spi.hpp b/include/sta/devices/stm32/spi.hpp index 0e477bf..385e1e9 100644 --- a/include/sta/devices/stm32/spi.hpp +++ b/include/sta/devices/stm32/spi.hpp @@ -9,7 +9,7 @@ // Only enable module on STM32 platform w/ HAL SPI module enabled #include #ifdef STA_PLATFORM_STM32 -# include +# include # ifndef HAL_GPIO_MODULE_ENABLED # error "STM32 GPIO module required!" # endif // !HAL_GPIO_MODULE_ENABLED diff --git a/include/sta/devices/stm32/uart.hpp b/include/sta/devices/stm32/uart.hpp index fb1a10e..1b79738 100644 --- a/include/sta/devices/stm32/uart.hpp +++ b/include/sta/devices/stm32/uart.hpp @@ -9,7 +9,7 @@ // Only enable module on STM32 platform w/ HAL UART module enabled #include #ifdef STA_PLATFORM_STM32 -# include +# include # ifdef HAL_UART_MODULE_ENABLED # define STA_STM32_UART_ENABLED # endif // HAL_UART_MODULE_ENABLED diff --git a/include/sta/lang.hpp b/include/sta/lang.hpp index 0c68348..037823f 100644 --- a/include/sta/lang.hpp +++ b/include/sta/lang.hpp @@ -100,7 +100,7 @@ * crash with the appropriate error message if the code is executed. */ #ifndef STA_NOT_IMPLEMENTED -# define STA_NOT_IMPLEMENTED() throw "myFunction is not implemented yet."; +# define STA_NOT_IMPLEMENTED() // throw "myFunction is not implemented yet."; #endif // !STA_NOT_IMPLEMENTED /** diff --git a/src/bus/i2c/device.cpp b/src/bus/i2c/device.cpp index dc54795..0235b7c 100644 --- a/src/bus/i2c/device.cpp +++ b/src/bus/i2c/device.cpp @@ -5,19 +5,20 @@ namespace sta { - I2cDevice::I2cDevice(I2c * intf, int addr) - : Device{intf}, addr_{addr} + I2CDevice::I2CDevice(I2C * intf, int address, bool master, bool blocking) + : Device{intf}, intf_{intf}, address_{address}, master_{master}, blocking_{blocking} { STA_ASSERT(intf != nullptr); } - void I2cDevice::select() + void I2CDevice::select() { - // TODO: Implement address selection here? + // Initialize the interface to match the device's settings. + intf_->setSettings(address_, master_, blocking_); } - void I2cDevice::deselect() + void I2CDevice::deselect() { - // TODO: Implement address deselection here? + // Nothing to do here. } } // namespace sta diff --git a/src/bus/i2c/i2c.cpp b/src/bus/i2c/i2c.cpp index db7d91f..289bbda 100644 --- a/src/bus/i2c/i2c.cpp +++ b/src/bus/i2c/i2c.cpp @@ -3,9 +3,16 @@ namespace sta { - I2c::I2c(Mutex * mutex=nullptr) + I2C::I2C(Mutex * mutex) : Interface{mutex} { } -} // namespace sta \ No newline at end of file + + void I2C::setSettings(uint16_t address, bool master, bool blocking) + { + address_ = address; + master_ = master; + blocking_ = blocking; + } +} // namespace sta diff --git a/src/can/id.cpp b/src/can/id.cpp index 8ba0fd9..26c8070 100644 --- a/src/can/id.cpp +++ b/src/can/id.cpp @@ -1,4 +1,4 @@ -#include +#include namespace sta diff --git a/src/can/iter.cpp b/src/can/iter.cpp index b7a7b06..b5dff60 100644 --- a/src/can/iter.cpp +++ b/src/can/iter.cpp @@ -1,4 +1,4 @@ -#include +#include #include diff --git a/src/devices/raspi/gpio_pin.cpp b/src/devices/raspi/gpio_pin.cpp index af3e210..fbfb512 100644 --- a/src/devices/raspi/gpio_pin.cpp +++ b/src/devices/raspi/gpio_pin.cpp @@ -1,4 +1,4 @@ -#include +#include #ifdef STA_RASPI_GPIO_ENABLED #include @@ -40,4 +40,4 @@ namespace sta } // namespace sta -#endif // STA_ARDUINO_GPIO_ENABLED \ No newline at end of file +#endif // STA_ARDUINO_GPIO_ENABLED diff --git a/src/devices/stm32/bus/i2c.cpp b/src/devices/stm32/bus/i2c.cpp new file mode 100644 index 0000000..6cbd5e4 --- /dev/null +++ b/src/devices/stm32/bus/i2c.cpp @@ -0,0 +1,122 @@ +#include + +#include +#include + +namespace sta +{ + STM32I2C::STM32I2C(I2C_HandleTypeDef * handle, Mutex * mutex) + : I2C{mutex}, handle_{handle} + { + STA_ASSERT(handle != nullptr); + } + + void STM32I2C::transfer(uint8_t value) + { + HAL_StatusTypeDef res; + + if (master_) + { + res = HAL_I2C_Master_Transmit(handle_, address_, &value, 1, timeout_); + } else { + res = HAL_I2C_Slave_Transmit(handle_, &value, 1, timeout_); + } + + STA_ASSERT(res == HAL_OK); + } + + void STM32I2C::transfer16(uint16_t value) + { + HAL_StatusTypeDef res; + + if (blocking_) + { + if (master_) + { + res = HAL_I2C_Master_Transmit(handle_, address_, reinterpret_cast(&value), 2, timeout_); + } else { + res = HAL_I2C_Slave_Transmit(handle_, reinterpret_cast(&value), 2, timeout_); + } + } else { + if (master_) + { + res = HAL_I2C_Slave_Transmit(handle_, reinterpret_cast(&value), 2, timeout_); + } else { + res = HAL_I2C_Slave_Transmit_IT(handle_, reinterpret_cast(&value), 2); + } + } + + STA_ASSERT(res == HAL_OK); + } + + void STM32I2C::transfer(const uint8_t * buffer, size_t size) + { + HAL_StatusTypeDef res; + + /* + * It's undecided if we want to change the parameter for this function. Since the transmission + * doesn't take a const buffer as an argument, we are using this fix by creating a temporary buffer. + */ + uint8_t * temp_buffer = new uint8_t[size]; + memcpy(temp_buffer, buffer, size); + + if (blocking_) + { + if (master_) + { + res = HAL_I2C_Master_Transmit(handle_, address_, temp_buffer, size, timeout_); + } else { + res = HAL_I2C_Slave_Transmit(handle_, temp_buffer, size, timeout_); + } + } else { + if (master_) { + res = HAL_I2C_Master_Transmit_IT(handle_, address_, temp_buffer, size); + } else { + res = HAL_I2C_Slave_Transmit_IT(handle_, temp_buffer, size); + } + } + + delete [] temp_buffer; + + STA_ASSERT(res == HAL_OK); + } + + void STM32I2C::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) + { + // TODO: Is this even something necessary for I2C? + } + + void STM32I2C::receive(uint8_t * buffer, size_t size) + { + HAL_StatusTypeDef res; + + if (blocking_) { + if (!master_) { + res = HAL_I2C_Master_Receive(handle_, address_, buffer, size, timeout_); + } else { + res = HAL_I2C_Slave_Receive(handle_, buffer, size, timeout_); + } + } else { + if (master_) { + res = HAL_I2C_Master_Receive_IT(handle_, address_, buffer, size); + } else { + res = HAL_I2C_Slave_Receive_IT(handle_, buffer, size); + } + } + + STA_ASSERT(res == HAL_OK); + } + + void STM32I2C::fill(uint8_t value, size_t count) + { + // Initialize a buffer of size count and fill it with the value. + uint8_t *buffer = new uint8_t[count]; + memset(buffer, value, count); + + // Transfer the buffer via the bus. + transfer(buffer, count); + + delete [] buffer; + } + +} // namespace sta diff --git a/src/devices/stm32/can.cpp b/src/devices/stm32/can.cpp index 5c39fc7..1355b0e 100644 --- a/src/devices/stm32/can.cpp +++ b/src/devices/stm32/can.cpp @@ -1,4 +1,4 @@ -#include +#include #ifdef STA_STM32_CAN_ENABLED #include diff --git a/src/devices/stm32/delay.cpp b/src/devices/stm32/delay.cpp index a3b9a98..a9d4c51 100644 --- a/src/devices/stm32/delay.cpp +++ b/src/devices/stm32/delay.cpp @@ -1,8 +1,8 @@ -#include +#include #ifdef STA_PLATFORM_STM32 -#include -#include +#include +#include #include #include diff --git a/src/devices/stm32/gpio_pin.cpp b/src/devices/stm32/gpio_pin.cpp index c0d6a67..9318605 100644 --- a/src/devices/stm32/gpio_pin.cpp +++ b/src/devices/stm32/gpio_pin.cpp @@ -1,4 +1,4 @@ -#include +#include #ifdef STA_STM32_GPIO_ENABLED #include @@ -15,7 +15,7 @@ namespace sta void STM32GpioPin::setState(GpioPinState state) { - HAL_GPIO_WritePin(port_, pin_, (state == GpioPinState::LOW) ? GPIO_PIN_RESET : GPIO_PIN_SET); + HAL_GPIO_WritePin(port_, pin_, (state == GpioPinState::GPIO_LOW) ? GPIO_PIN_RESET : GPIO_PIN_SET); } GPIO_TypeDef * STM32GpioPin::getPort() const diff --git a/src/devices/stm32/i2c.cpp b/src/devices/stm32/i2c.cpp deleted file mode 100644 index ce4bdbd..0000000 --- a/src/devices/stm32/i2c.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include - -#if false - -namespace sta { - STM32I2cDevice::STM32I2cDevice(I2C_HandleTypeDef* i2cHandle, uint16_t address, Mutex* mutex, bool master, bool blocking) - : I2cDevice(address, mutex, master, blocking) { - this->master = master; - } - - bool STM32I2cDevice::transmit(uint8_t* data, uint16_t size) { - HAL_StatusTypeDef res; - - if (this->blocking) { - if (!this->master) { - res = HAL_I2C_Master_Transmit(i2cHandle, address, data, size, this->timeout); - } else { - res = HAL_I2C_Slave_Transmit(i2cHandle , data, size, this->timeout); - } - } else { - if (!this->master) { - res = HAL_I2C_Master_Transmit_IT(i2cHandle, address, data, size); - } else { - res = HAL_I2C_Slave_Transmit_IT(i2cHandle , data, size); - } - } - - return res == HAL_OK; - } - - bool STM32I2cDevice::receive(uint8_t* data, uint16_t size) { - HAL_StatusTypeDef res; - - if (this->blocking) { - if (!this->master) { - res = HAL_I2C_Master_Receive(i2cHandle, address, data, size, this->timeout); - } else { - res = HAL_I2C_Slave_Receive(i2cHandle , data, size, this->timeout); - } - } else { - if (!this->master) { - res = HAL_I2C_Master_Receive_IT(i2cHandle, address, data, size); - } else { - res = HAL_I2C_Slave_Receive_IT(i2cHandle , data, size); - } - } - - return res == HAL_OK; - } - - bool STM32I2cDevice::deviceReady() { - HAL_StatusTypeDef res = HAL_I2C_IsDeviceReady(this->i2cHandle, this->address, 8, this->timeout); - return res == HAL_OK; - } -} - -#endif // false \ No newline at end of file diff --git a/src/devices/stm32/init.cpp b/src/devices/stm32/init.cpp index fed0f3c..843c6d0 100644 --- a/src/devices/stm32/init.cpp +++ b/src/devices/stm32/init.cpp @@ -1,4 +1,4 @@ -#include +#include #include diff --git a/src/devices/stm32/spi.cpp b/src/devices/stm32/spi.cpp index ec5af2a..2a2676a 100644 --- a/src/devices/stm32/spi.cpp +++ b/src/devices/stm32/spi.cpp @@ -1,4 +1,4 @@ -#include +#include #ifdef STA_STM32_SPI_ENABLED #include diff --git a/src/devices/template/delay.cpp b/src/devices/template/delay.cpp index 18edb34..3b19826 100644 --- a/src/devices/template/delay.cpp +++ b/src/devices/template/delay.cpp @@ -13,8 +13,6 @@ * - Remove the import if no longer needed. */ -#define STA_PLATFORM_YOUR_DEVICE - #include #ifdef STA_PLATFORM_YOUR_DEVICE @@ -33,4 +31,4 @@ namespace sta } } // namespace sta -#endif // STA_PLATFORM_YOUR_DEVICE \ No newline at end of file +#endif // STA_PLATFORM_YOUR_DEVICE From 8c5f0a3f291b3260c45ccefb4d8662af112cd958 Mon Sep 17 00:00:00 2001 From: dvdb97 Date: Sun, 2 Jul 2023 11:56:11 +0200 Subject: [PATCH 11/22] Added first implementations for Arduino --- include/sta/devices/arduino/bus/i2c.hpp | 36 ++++++++++ include/sta/devices/arduino/bus/spi.hpp | 0 include/sta/devices/arduino/delay.hpp | 28 ++++++++ include/sta/devices/arduino/gpio_pin.hpp | 44 ++++++++++++ include/sta/devices/arduino/hal.hpp | 8 +++ include/sta/devices/arduino/mcu/common.hpp | 7 ++ include/sta/devices/arduino/mcu/uno_r3.hpp | 6 ++ .../sta/devices/arduino/not_implemented.hpp | 12 ---- src/devices/arduino/bus/i2c.cpp | 68 +++++++++++++++++++ src/devices/arduino/bus/spi.cpp | 0 src/devices/arduino/delay.cpp | 20 ++++++ src/devices/arduino/gpio_pin.cpp | 28 ++++++++ src/devices/raspi/bus/{i2c => }/i2c.cpp | 0 src/devices/raspi/bus/{spi => }/spi.cpp | 0 src/devices/stm32/{ => bus}/spi.cpp | 0 15 files changed, 245 insertions(+), 12 deletions(-) create mode 100644 include/sta/devices/arduino/bus/i2c.hpp create mode 100644 include/sta/devices/arduino/bus/spi.hpp create mode 100644 include/sta/devices/arduino/delay.hpp create mode 100644 include/sta/devices/arduino/gpio_pin.hpp create mode 100644 include/sta/devices/arduino/hal.hpp create mode 100644 include/sta/devices/arduino/mcu/common.hpp create mode 100644 include/sta/devices/arduino/mcu/uno_r3.hpp delete mode 100644 include/sta/devices/arduino/not_implemented.hpp create mode 100644 src/devices/arduino/bus/i2c.cpp create mode 100644 src/devices/arduino/bus/spi.cpp create mode 100644 src/devices/arduino/delay.cpp create mode 100644 src/devices/arduino/gpio_pin.cpp rename src/devices/raspi/bus/{i2c => }/i2c.cpp (100%) rename src/devices/raspi/bus/{spi => }/spi.cpp (100%) rename src/devices/stm32/{ => bus}/spi.cpp (100%) diff --git a/include/sta/devices/arduino/bus/i2c.hpp b/include/sta/devices/arduino/bus/i2c.hpp new file mode 100644 index 0000000..ff9a4e9 --- /dev/null +++ b/include/sta/devices/arduino/bus/i2c.hpp @@ -0,0 +1,36 @@ +#ifndef STA_CORE_ARDUINO_I2C_HPP +#define STA_CORE_ARDUINO_I2C_HPP + +#include +#ifdef STA_PLATFORM_ARDUINO + +#include +#include + +namespace sta +{ + class ArduinoI2C : public I2C + { + public: + ArduinoI2C(Mutex * mutex=nullptr, uint16_t address); + + void transfer16(uint16_t value) override; + void transfer(const uint8_t * buffer, size_t size) override; + void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) override; + void receive(uint8_t * buffer, size_t size) override; + void fill(uint8_t value, size_t count) override; + + void acquire(); + void release(); + }; + + class ArduinoI2CDevice : public I2CDevice + { + public: + ArduinoI2CDevice(I2C * intf, int address, bool master=false, bool blocking=true); + }; +} // namespace sta + +#endif // STA_PLATFORM_ARDUINO + +#endif // STA_CORE_ARDUINO_I2C_HPP \ No newline at end of file diff --git a/include/sta/devices/arduino/bus/spi.hpp b/include/sta/devices/arduino/bus/spi.hpp new file mode 100644 index 0000000..e69de29 diff --git a/include/sta/devices/arduino/delay.hpp b/include/sta/devices/arduino/delay.hpp new file mode 100644 index 0000000..bcc6ebb --- /dev/null +++ b/include/sta/devices/arduino/delay.hpp @@ -0,0 +1,28 @@ +#ifndef STA_CORE_ARDUINO_DELAY_HPP +#define STA_CORE_ARDUINO_DELAY_HPP + +// Only enable module on Arduino platform +#include + +#if defined(STA_PLATFORM_ARDUINO) || defined(DOXYGEN) + +namespace sta +{ + /** + * @brief Millisecond delay. + * + * @param ms Milliseconds + */ + void delayMs(uint32_t ms); + + /** + * @brief Microsecond delay. + * + * @param us Microseconds + */ + void delayUs(uint32_t us); +} // namespace sta + +#endif // STA_PLATFORM_ARDUINO + +#endif // STA_CORE_ARDUINO_DELAY_HPP \ No newline at end of file diff --git a/include/sta/devices/arduino/gpio_pin.hpp b/include/sta/devices/arduino/gpio_pin.hpp new file mode 100644 index 0000000..dd03289 --- /dev/null +++ b/include/sta/devices/arduino/gpio_pin.hpp @@ -0,0 +1,44 @@ +/** + * @file + * @brief Wrapper for Arduino GPIO pins. + */ +#ifndef STA_CORE_ARDUINO_GPIO_PIN_HPP +#define STA_CORE_ARDUINO_GPIO_PIN_HPP + +// Only enable module on Arduino platform w/ HAL GPIO module enabled +#include + +#if defined(STA_PLATFORM_ARDUINO) || defined(DOXYGEN) + +#include +#include + +namespace sta +{ + class ArduinoGpioPin : public GpioPin + { + public: + /** + * @param port GPIO port + * @param pin Pin index + */ + ArduinoGpioPin(uint16_t pin); + + void setState(GpioPinState state) override; + + GpioPinState getState() override; + + /** + * @brief Get pin index for pin. + * + * @return Pin index + */ + uint16_t getPin() const; + private: + uint16_t pin_; /**< GPIO pin */ + }; +} // namespace sta + +#endif // STA_ARDUINO_GPIO_ENABLED + +#endif // STA_CORE_ARDUINO_GPIO_PIN_HPP \ No newline at end of file diff --git a/include/sta/devices/arduino/hal.hpp b/include/sta/devices/arduino/hal.hpp new file mode 100644 index 0000000..1693555 --- /dev/null +++ b/include/sta/devices/arduino/hal.hpp @@ -0,0 +1,8 @@ +#ifndef STA_CORE_ARDUINO_HAL_HPP +#define STA_CORE_ARDUINO_HAL_HPP + +#include +#include +#include + +#endif // STA_CORE_ARDUINO_HAL_HPP \ No newline at end of file diff --git a/include/sta/devices/arduino/mcu/common.hpp b/include/sta/devices/arduino/mcu/common.hpp new file mode 100644 index 0000000..3a79c10 --- /dev/null +++ b/include/sta/devices/arduino/mcu/common.hpp @@ -0,0 +1,7 @@ +#ifndef STA_CORE_ARDUINO_MCU_COMMON_HPP +#define STA_CORE_ARDUINO_MCU_COMMON_HPP + +// Enable Arduino platform +#define STA_PLATFORM_ARDUINO + +#endif // STA_CORE_ARDUINO_MCU_COMMON_HPP \ No newline at end of file diff --git a/include/sta/devices/arduino/mcu/uno_r3.hpp b/include/sta/devices/arduino/mcu/uno_r3.hpp new file mode 100644 index 0000000..a367296 --- /dev/null +++ b/include/sta/devices/arduino/mcu/uno_r3.hpp @@ -0,0 +1,6 @@ +#ifndef STA_CORE_ARDUINO_MCU_UNO_R3_HPP +#define STA_CORE_ARDUINO_MCU_UNO_R3_HPP + +#include + +#endif // STA_CORE_ARDUINO_MCU_UNO_R3_HPPs \ No newline at end of file diff --git a/include/sta/devices/arduino/not_implemented.hpp b/include/sta/devices/arduino/not_implemented.hpp deleted file mode 100644 index 5c8d8b3..0000000 --- a/include/sta/devices/arduino/not_implemented.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef STA_CORE_ARDUINO_NOT_IMPLEMENTED_HPP -#define STA_CORE_ARDUINO_NOT_IMPLEMENTED_HPP - - -/** - * @defgroup sta_core_arduino Arduino - * @ingroup sta_core_platforms - * @brief Modules implemented for the Arduino platform. - */ - - -#endif // STA_CORE_ARDUINO_NOT_IMPLEMENTED_HPP \ No newline at end of file diff --git a/src/devices/arduino/bus/i2c.cpp b/src/devices/arduino/bus/i2c.cpp new file mode 100644 index 0000000..c52503a --- /dev/null +++ b/src/devices/arduino/bus/i2c.cpp @@ -0,0 +1,68 @@ +#include + +#ifdef STA_PLATFORM_ARDUINO + +#include + + +namespace sta +{ + ArduinoI2C::ArduinoI2C(Mutex * mutex, uint16_t address) + : I2C{mutex} + { + Wire.begin(address); + } + + void ArduinoI2C::transfer16(uint16_t value) + { + Wire.write(value); + } + + void ArduinoI2C::transfer(const uint8_t * buffer, size_t size) + { + Wire.write(buffer, size); + } + + void ArduinoI2C::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) + { + // NOT REALLY A THING HERE, IS IT? + } + + void ArduinoI2C::receive(uint8_t * buffer, size_t size) + { + + } + + void ArduinoI2C::fill(uint8_t value, size_t count) + { + // Initialize a buffer of size count and fill it with the value. + uint8_t *buffer = new uint8_t[count]; + memset(buffer, value, count); + + Serial.write(buffer, count); + + delete [] buffer; + } + + void ArduinoI2C::acquire() + { + I2C::acquire(); + + Wire.beginTransmission(address_); + } + + void ArduinoI2C::release() + { + Wire.endTransmission(); + + I2C::release(); + } + + ArduinoI2CDevice::ArduinoI2CDevice(I2C * intf, int address, bool master, bool blocking) + : I2CDevice{intf, address, master, blocking} + { + + } +} // namespace sta + +#endif // STA_PLATFORM_ARDUINO \ No newline at end of file diff --git a/src/devices/arduino/bus/spi.cpp b/src/devices/arduino/bus/spi.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/devices/arduino/delay.cpp b/src/devices/arduino/delay.cpp new file mode 100644 index 0000000..b3a39f1 --- /dev/null +++ b/src/devices/arduino/delay.cpp @@ -0,0 +1,20 @@ +#include + +#ifdef STA_PLATFORM_ARDUINO + +#include + +namespace sta +{ + void delayMs(uint32_t ms) + { + delay(ms); + } + + void delayUs(uint32_t us) + { + delayMicroseconds(us); + } +} // namespace sta + +#endif // STA_PLATFORM_ARDUINO \ No newline at end of file diff --git a/src/devices/arduino/gpio_pin.cpp b/src/devices/arduino/gpio_pin.cpp new file mode 100644 index 0000000..1d3b232 --- /dev/null +++ b/src/devices/arduino/gpio_pin.cpp @@ -0,0 +1,28 @@ +#include + +#ifdef STA_PLATFORM_ARDUINO + +#include + +namespace sta +{ + ArduinoGpioPin::ArduinoGpioPin(uint16_t pin) : pin_{pin} { } + + void ArduinoGpioPin::setState(GpioPinState state) + { + digitalWrite(pin_, (state == GpioPinState::GPIO_LOW) ? LOW : HIGH); + } + + GpioPinState ArduinoGpioPin::getState() + { + return digitalRead(pin_) == HIGH ? GpioPinState::GPIO_HIGH : GpioPinState::GPIO_LOW; + } + + uint16_t ArduinoGpioPin::getPin() const + { + return pin_; + } + +} // namespace sta + +#endif // STA_PLATFORM_ARDUINO \ No newline at end of file diff --git a/src/devices/raspi/bus/i2c/i2c.cpp b/src/devices/raspi/bus/i2c.cpp similarity index 100% rename from src/devices/raspi/bus/i2c/i2c.cpp rename to src/devices/raspi/bus/i2c.cpp diff --git a/src/devices/raspi/bus/spi/spi.cpp b/src/devices/raspi/bus/spi.cpp similarity index 100% rename from src/devices/raspi/bus/spi/spi.cpp rename to src/devices/raspi/bus/spi.cpp diff --git a/src/devices/stm32/spi.cpp b/src/devices/stm32/bus/spi.cpp similarity index 100% rename from src/devices/stm32/spi.cpp rename to src/devices/stm32/bus/spi.cpp From 96d94edc52b7984a07507a4941b19c94f2df3022 Mon Sep 17 00:00:00 2001 From: dvdb97 Date: Wed, 12 Jul 2023 13:33:34 +0200 Subject: [PATCH 12/22] First reworks for debugging --- include/sta/bus/uart/settings.hpp | 2 + include/sta/bus/uart/uart.hpp | 4 +- include/sta/debugging.hpp | 40 +++++++++++++ include/sta/devices/stm32/bus/i2c.hpp | 3 +- include/sta/devices/stm32/{ => bus}/spi.hpp | 0 include/sta/devices/stm32/{ => bus}/uart.hpp | 11 +++- include/sta/printable.hpp | 3 +- include/sta/uart.hpp | 61 -------------------- src/bus/uart/uart.cpp | 4 +- src/debug_serial.cpp | 4 +- src/devices/stm32/bus/i2c.cpp | 6 ++ src/devices/stm32/{ => bus}/spi.cpp | 2 +- src/devices/stm32/bus/uart.cpp | 58 +++++++++++++++++++ src/devices/stm32/uart.cpp | 25 -------- src/uart.cpp | 28 --------- 15 files changed, 125 insertions(+), 126 deletions(-) create mode 100644 include/sta/debugging.hpp rename include/sta/devices/stm32/{ => bus}/spi.hpp (100%) rename include/sta/devices/stm32/{ => bus}/uart.hpp (69%) delete mode 100644 include/sta/uart.hpp rename src/devices/stm32/{ => bus}/spi.cpp (99%) create mode 100644 src/devices/stm32/bus/uart.cpp delete mode 100644 src/devices/stm32/uart.cpp delete mode 100644 src/uart.cpp diff --git a/include/sta/bus/uart/settings.hpp b/include/sta/bus/uart/settings.hpp index c3bbe2b..390774c 100644 --- a/include/sta/bus/uart/settings.hpp +++ b/include/sta/bus/uart/settings.hpp @@ -5,6 +5,8 @@ namespace sta { + + enum class UARTMode { RX, diff --git a/include/sta/bus/uart/uart.hpp b/include/sta/bus/uart/uart.hpp index fafd153..aee3232 100644 --- a/include/sta/bus/uart/uart.hpp +++ b/include/sta/bus/uart/uart.hpp @@ -9,7 +9,7 @@ namespace sta class UART : public Interface { public: - UART(const UARTSettings & settings, Mutex * mutex=nullptr); + UART(Mutex * mutex=nullptr); /** * @brief Get %UART interface settings. @@ -22,4 +22,4 @@ namespace sta }; } // namespace sta -#endif // STA_CORE_UART_UART_HPP \ No newline at end of file +#endif // STA_CORE_UART_UART_HPP diff --git a/include/sta/debugging.hpp b/include/sta/debugging.hpp new file mode 100644 index 0000000..e5604ed --- /dev/null +++ b/include/sta/debugging.hpp @@ -0,0 +1,40 @@ +#ifndef STA_CORE_DEBUGGING_HPP +#define STA_CORE_DEBUGGING_HPP + +#include +#ifdef STA_DEBUGGING_ENABLED + +#include + +namespace sta +{ + extern Printable * Debug; +} // namespace sta + + +/** + * @brief Debug print message. + * + * @param ... See @ref sta::PrintableUART::print + * + * @ingroup sta_core_debug + */ +# define STA_DEBUG_PRINT(...) sta::Debug->print(__VA_ARGS__) + +/** + * @brief Debug print message followed by new-line to UART. + * + * @param ... See @ref sta::PrintableUART::println + * + * @ingroup sta_core_debug + */ +# define STA_DEBUG_PRINTLN(...) sta::Debug->println(__VA_ARGS__) + +#else // !STA_DEBUGGING_ENABLED + +# define STA_DEBUG_PRINT(...) ((void)0) +# define STA_DEBUG_PRINTLN(...) ((void)0) + +#endif // STA_DEBUGGING_ENABLED + +#endif // STA_CORE_DEBUGGING_HPP diff --git a/include/sta/devices/stm32/bus/i2c.hpp b/include/sta/devices/stm32/bus/i2c.hpp index be939d2..b2b34d1 100644 --- a/include/sta/devices/stm32/bus/i2c.hpp +++ b/include/sta/devices/stm32/bus/i2c.hpp @@ -35,7 +35,8 @@ namespace sta class STM32I2CDevice : public I2CDevice { - STM32I2CDevice(); + public: + STM32I2CDevice(STM32I2C * intf, int address, bool master=true, bool blocking=true); }; } diff --git a/include/sta/devices/stm32/spi.hpp b/include/sta/devices/stm32/bus/spi.hpp similarity index 100% rename from include/sta/devices/stm32/spi.hpp rename to include/sta/devices/stm32/bus/spi.hpp diff --git a/include/sta/devices/stm32/uart.hpp b/include/sta/devices/stm32/bus/uart.hpp similarity index 69% rename from include/sta/devices/stm32/uart.hpp rename to include/sta/devices/stm32/bus/uart.hpp index 1b79738..f50477d 100644 --- a/include/sta/devices/stm32/uart.hpp +++ b/include/sta/devices/stm32/bus/uart.hpp @@ -18,7 +18,7 @@ #if defined(STA_STM32_UART_ENABLED) || defined(DOXYGEN) -#include +#include /** @@ -41,10 +41,15 @@ namespace sta /** * @param handle STM32 HAL handle */ - STM32UART(UART_HandleTypeDef * handle); + STM32UART(UART_HandleTypeDef * handle, Mutex * mutex); - void write(const uint8_t * buffer, size_t size) override; + void transfer(uint8_t value) override; + void transfer16(uint16_t value) override; + void transfer(const uint8_t * buffer, size_t size) override; + void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) override; + void receive(uint8_t * buffer, size_t size) override; + void fill(uint8_t value, size_t count) override; private: UART_HandleTypeDef * handle_; /**< STM32 HAL handle */ }; diff --git a/include/sta/printable.hpp b/include/sta/printable.hpp index 7925c8c..77117a3 100644 --- a/include/sta/printable.hpp +++ b/include/sta/printable.hpp @@ -25,6 +25,7 @@ namespace sta class Printable { + public: /** * @brief Print single character. * @@ -188,4 +189,4 @@ namespace sta } // namespace sta -#endif // STA_CORE_PRINTABLE_HPP \ No newline at end of file +#endif // STA_CORE_PRINTABLE_HPP diff --git a/include/sta/uart.hpp b/include/sta/uart.hpp deleted file mode 100644 index 400f871..0000000 --- a/include/sta/uart.hpp +++ /dev/null @@ -1,61 +0,0 @@ -/** - * @file - * @brief UART interface definition. - */ -#ifndef STA_CORE_UART_HPP -#define STA_CORE_UART_HPP - -#include -#include - - -/** - * @defgroup sta_core_uart UART - * @ingroup sta_core - * @brief UART interface. - */ - - -namespace sta -{ - /** - * @brief Interface for %UART. - * - * @ingroup sta_core_uart - */ - class UART - { - public: - /** - * @brief Write buffer to %UART. - * - * @param buffer Source buffer - * @param size Number of bytes in buffer - */ - virtual void write(const uint8_t * buffer, size_t size) = 0; - - /** - * @brief Write unsigned integer to %UART. - * - * @param value Unsigned integer value - */ - void write(uint8_t value); - - /** - * @brief Write unsigned integer to %UART. - * - * @param value Unsigned integer value - */ - void write(uint16_t value); - - /** - * @brief Write unsigned integer to %UART. - * - * @param value Unsigned integer value - */ - void write(uint32_t value); - }; -} // namespace sta - - -#endif // STA_CORE_UART_HPP diff --git a/src/bus/uart/uart.cpp b/src/bus/uart/uart.cpp index 6225576..d2e2767 100644 --- a/src/bus/uart/uart.cpp +++ b/src/bus/uart/uart.cpp @@ -2,8 +2,8 @@ namespace sta { - UART::UART(const UARTSettings & settings, Mutex * mutex) - : Interface{mutex}, settings_{settings} + UART::UART(Mutex * mutex) + : Interface{mutex} { } diff --git a/src/debug_serial.cpp b/src/debug_serial.cpp index 80a0db5..b6ff7fe 100644 --- a/src/debug_serial.cpp +++ b/src/debug_serial.cpp @@ -4,9 +4,9 @@ #ifdef STA_PLATFORM_STM32 -#include +#include -#include +// #include // Set platform UART alias using PlatformUART = sta::STM32UART; diff --git a/src/devices/stm32/bus/i2c.cpp b/src/devices/stm32/bus/i2c.cpp index 6cbd5e4..83f4dda 100644 --- a/src/devices/stm32/bus/i2c.cpp +++ b/src/devices/stm32/bus/i2c.cpp @@ -119,4 +119,10 @@ namespace sta delete [] buffer; } + STM32I2CDevice::STM32I2CDevice(STM32I2C * intf, int address, bool master, bool blocking) + : I2CDevice(intf, address, master, blocking) + { + + } + } // namespace sta diff --git a/src/devices/stm32/spi.cpp b/src/devices/stm32/bus/spi.cpp similarity index 99% rename from src/devices/stm32/spi.cpp rename to src/devices/stm32/bus/spi.cpp index 2a2676a..7cfa8b5 100644 --- a/src/devices/stm32/spi.cpp +++ b/src/devices/stm32/bus/spi.cpp @@ -1,4 +1,4 @@ -#include +#include #ifdef STA_STM32_SPI_ENABLED #include diff --git a/src/devices/stm32/bus/uart.cpp b/src/devices/stm32/bus/uart.cpp new file mode 100644 index 0000000..2d36ef3 --- /dev/null +++ b/src/devices/stm32/bus/uart.cpp @@ -0,0 +1,58 @@ +#include +#ifdef STA_STM32_UART_ENABLED + +#include +#include + +namespace sta +{ + STM32UART::STM32UART(UART_HandleTypeDef * handle, Mutex * mutex) + : UART{mutex}, handle_{handle} + { + STA_ASSERT(handle != nullptr); + } + + void STM32UART::transfer(uint8_t value) + { + HAL_UART_Transmit(handle_, &value, 1, HAL_MAX_DELAY); + } + + void STM32UART::transfer16(uint16_t value) + { + HAL_UART_Transmit(handle_, reinterpret_cast(&value), 2, HAL_MAX_DELAY); + } + + void STM32UART::transfer(const uint8_t * buffer, size_t size) + { + STA_ASSERT(buffer != nullptr); + + HAL_UART_Transmit(handle_, const_cast(buffer), size, HAL_MAX_DELAY); + } + + void STM32UART::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) + { + // IS THIS A THING HERE? + } + + void STM32UART::receive(uint8_t * buffer, size_t size) + { + STA_ASSERT(buffer != nullptr); + + HAL_UART_Receive(handle_, buffer, size, HAL_MAX_DELAY); + } + + void STM32UART::fill(uint8_t value, size_t count) + { + // Initialize a buffer of size count and fill it with the value. + uint8_t *buffer = new uint8_t[count]; + memset(buffer, value, count); + + // Transfer the buffer via the bus. + transfer(buffer, count); + + delete [] buffer; + } +} // namespace sta + + +#endif // STA_STM32_UART_ENABLED diff --git a/src/devices/stm32/uart.cpp b/src/devices/stm32/uart.cpp deleted file mode 100644 index d377cd5..0000000 --- a/src/devices/stm32/uart.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include -#ifdef STA_STM32_UART_ENABLED - -#include - - -namespace sta -{ - STM32UART::STM32UART(UART_HandleTypeDef * handle) - : handle_{handle} - { - STA_ASSERT(handle != nullptr); - } - - - void STM32UART::write(const uint8_t * buffer, size_t size) - { - STA_ASSERT(buffer != nullptr); - - HAL_UART_Transmit(handle_, const_cast(buffer), size, HAL_MAX_DELAY); - } -} // namespace sta - - -#endif // STA_STM32_UART_ENABLED diff --git a/src/uart.cpp b/src/uart.cpp deleted file mode 100644 index dd0f69f..0000000 --- a/src/uart.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include - -#include - -#include -#include - - -namespace sta -{ - void UART::write(uint8_t value) - { - // TODO Handle endian-ness - write(&value, 1); - } - - void UART::write(uint16_t value) - { - // TODO Handle endian-ness - write(reinterpret_cast(&value), sizeof(value)); - } - - void UART::write(uint32_t value) - { - // TODO Handle endian-ness - write(reinterpret_cast(&value), sizeof(value)); - } -} // namespace sta From 6da6666559306d34a46c52819120a762677b7252 Mon Sep 17 00:00:00 2001 From: Dario Date: Wed, 12 Jul 2023 14:05:51 +0100 Subject: [PATCH 13/22] Big assert / debug clean up --- include/sta/config.hpp | 11 +++ include/sta/{ => debug}/assert.hpp | 0 .../sta/{debugging.hpp => debug/debug.hpp} | 2 +- .../sta/{ => debug/printing}/printable.hpp | 0 .../sta/debug/printing/printable_printf.hpp | 28 +++++++ .../{ => debug/printing}/printable_uart.hpp | 2 +- include/sta/debug_printf.hpp | 49 ------------ include/sta/debug_serial.hpp | 79 ------------------- include/sta/devices/raspi/bus/i2c.hpp | 17 ++-- src/bus/device.cpp | 2 +- src/bus/i2c/device.cpp | 2 +- src/bus/interface.cpp | 2 +- src/bus/spi/device.cpp | 2 +- src/bus/spi/settings.cpp | 2 +- src/can/iter.cpp | 2 +- src/{ => debug}/assert.cpp | 12 +-- src/{ => debug/printing}/printable.cpp | 4 +- src/debug/printing/printable_printf.cpp | 20 +++++ src/{ => debug/printing}/printable_uart.cpp | 4 +- src/debug_serial.cpp | 30 ------- src/devices/raspi/bus/i2c.cpp | 42 ++++------ src/devices/raspi/bus/spi.cpp | 2 +- src/devices/raspi/delay.cpp | 2 +- src/devices/raspi/gpio_pin.cpp | 2 +- src/devices/stm32/bus/i2c.cpp | 6 +- src/devices/stm32/init.cpp | 2 +- 26 files changed, 108 insertions(+), 218 deletions(-) create mode 100644 include/sta/config.hpp rename include/sta/{ => debug}/assert.hpp (100%) rename include/sta/{debugging.hpp => debug/debug.hpp} (94%) rename include/sta/{ => debug/printing}/printable.hpp (100%) create mode 100644 include/sta/debug/printing/printable_printf.hpp rename include/sta/{ => debug/printing}/printable_uart.hpp (94%) delete mode 100644 include/sta/debug_printf.hpp delete mode 100644 include/sta/debug_serial.hpp rename src/{ => debug}/assert.cpp (63%) rename src/{ => debug/printing}/printable.cpp (98%) create mode 100644 src/debug/printing/printable_printf.cpp rename src/{ => debug/printing}/printable_uart.cpp (85%) delete mode 100644 src/debug_serial.cpp diff --git a/include/sta/config.hpp b/include/sta/config.hpp new file mode 100644 index 0000000..3b22342 --- /dev/null +++ b/include/sta/config.hpp @@ -0,0 +1,11 @@ +#ifndef STA_CONFIG_HPP +#define STA_CONFIG_HPP + +#include + +#define STA_DEBUGGING_ENABLED +#define STA_PRINTF_USE_STDLIB + +#define STA_ASSERT_FORCE + +#endif // STA_CONFIG_HPP \ No newline at end of file diff --git a/include/sta/assert.hpp b/include/sta/debug/assert.hpp similarity index 100% rename from include/sta/assert.hpp rename to include/sta/debug/assert.hpp diff --git a/include/sta/debugging.hpp b/include/sta/debug/debug.hpp similarity index 94% rename from include/sta/debugging.hpp rename to include/sta/debug/debug.hpp index e5604ed..0396ea1 100644 --- a/include/sta/debugging.hpp +++ b/include/sta/debug/debug.hpp @@ -4,7 +4,7 @@ #include #ifdef STA_DEBUGGING_ENABLED -#include +#include namespace sta { diff --git a/include/sta/printable.hpp b/include/sta/debug/printing/printable.hpp similarity index 100% rename from include/sta/printable.hpp rename to include/sta/debug/printing/printable.hpp diff --git a/include/sta/debug/printing/printable_printf.hpp b/include/sta/debug/printing/printable_printf.hpp new file mode 100644 index 0000000..0d51f04 --- /dev/null +++ b/include/sta/debug/printing/printable_printf.hpp @@ -0,0 +1,28 @@ +#ifndef STA_CORE_PRINTABLE_PRINTF_HPP +#define STA_CORE_PRINTABLE_PRINTF_HPP + +#include + +namespace sta +{ + class PrintablePrintf : public Printable + { + public: + /** + * @brief Construct a new Printable Printf object + */ + PrintablePrintf(); + + /** + * @brief Print string. + * + * @param str String buffer + * @param length String length + */ + void print(const char * str, size_t length) override; + }; +} // namespace sta + + + +#endif // STA_CORE_PRINTABLE_PRINTF_HPP \ No newline at end of file diff --git a/include/sta/printable_uart.hpp b/include/sta/debug/printing/printable_uart.hpp similarity index 94% rename from include/sta/printable_uart.hpp rename to include/sta/debug/printing/printable_uart.hpp index 7e78b0e..93f92c5 100644 --- a/include/sta/printable_uart.hpp +++ b/include/sta/debug/printing/printable_uart.hpp @@ -6,7 +6,7 @@ #define STA_CORE_PRINTABLE_UART_HPP #include -#include +#include #include #include diff --git a/include/sta/debug_printf.hpp b/include/sta/debug_printf.hpp deleted file mode 100644 index 42f85a6..0000000 --- a/include/sta/debug_printf.hpp +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef STA_CORE_DEBUG_PRINTF_HPP -#define STA_CORE_DEBUG_PRINTF_HPP - -#include - -// Determine if module should be enabled -// Condition 1: STA_DEBUG_PRINTF is defined -// Condition 2: -// STA_DEBUG_PRINTF_FORCE is defined -// or -// DEBUG is defined but not NDEBUG -#ifdef STA_DEBUG_PRINTF -# ifdef STA_DEBUG_PRINTF_FORCE -# define STA_DEBUG_SERIAL_ENABLED -# else // !STA_DEBUG_PRINTF_FORCE -# if defined(DEBUG) && !defined(NDEBUG) -# define STA_DEBUG_PRINTF_ENABLED -# endif // DEBUG && !NDEBUG -# endif // !STA_DEBUG_PRINTF_FORCE -#endif // STA_DEBUG_PRINTF - -#if defined(STA_DEBUG_PRINTF_ENABLED) || defined(DOXYGEN) - -#include - -/** - * @brief Print debug output. - * - * @param ... See @ref sta::PrintableUART::print - * - * @ingroup sta_core_debug - */ -# define STA_DEBUG_PRINT(...) sta::print(__VA_ARGS__) -/** - * @brief Print debug output followed by new-line to UART. - * - * @param ... See @ref sta::PrintableUART::println - * - * @ingroup sta_core_debug - */ -# define STA_DEBUG_PRINTLN(...) sta::println(__VA_ARGS__) - - -#else // !STA_DEBUG_PRINTF_ENABLED -# define STA_DEBUG_PRINT(...) ((void)0) -# define STA_DEBUG_PRINTLN(...) ((void)0) -#endif // !STA_DEBUG_PRINTF_ENABLED - -#endif // STA_CORE_DEBUG_PRINTF_HPP \ No newline at end of file diff --git a/include/sta/debug_serial.hpp b/include/sta/debug_serial.hpp deleted file mode 100644 index ee3bd11..0000000 --- a/include/sta/debug_serial.hpp +++ /dev/null @@ -1,79 +0,0 @@ -/** - * @file - * @brief Debug output via UART. - * - * Configuration: - * * STA_DEBUG_SERIAL_UART: UART interface for output - * * STA_DEBUG_SERIAL_FORCE: Ignore debug defines and always enable output - * * DEBUG: Enables output when defined - * * NDEBUG: Disables output when defined (overrides DEBUG) - */ -#ifndef STA_CORE_DEBUG_SERIAL_HPP -#define STA_CORE_DEBUG_SERIAL_HPP - - -#include - -// Determine if module should be enabled -// Condition 1: STA_DEBUG_SERIAL_UART is defined -// Condition 2: -// STA_DEBUG_SERIAL_FORCE is defined -// or -// DEBUG is defined but not NDEBUG -#ifdef STA_DEBUG_SERIAL_UART -# ifdef STA_DEBUG_SERIAL_FORCE -# define STA_DEBUG_SERIAL_ENABLED -# else // !STA_DEBUG_SERIAL_FORCE -# if defined(DEBUG) && !defined(NDEBUG) -# define STA_DEBUG_SERIAL_ENABLED -# endif // DEBUG && !NDEBUG -# endif // !STA_DEBUG_SERIAL_FORCE -#endif // STA_DEBUG_SERIAL_UART - - -#if defined(STA_DEBUG_SERIAL_ENABLED) || defined(DOXYGEN) - -#include - - -/** - * @defgroup sta_core_debug Debug Serial - * @ingroup sta_core - * @brief Debug serial output. - */ - - -namespace sta -{ - /** - * @brief %UART print object for debug serial output. - * - * @ingroup sta_core_debug - */ - extern PrintableUART DebugSerial; -} // namespace sta - - -/** - * @brief Print debug output to UART. - * - * @param ... See @ref sta::PrintableUART::print - * - * @ingroup sta_core_debug - */ -# define STA_DEBUG_PRINT(...) sta::DebugSerial.print(__VA_ARGS__) -/** - * @brief Print debug output followed by new-line to UART. - * - * @param ... See @ref sta::PrintableUART::println - * - * @ingroup sta_core_debug - */ -# define STA_DEBUG_PRINTLN(...) sta::DebugSerial.println(__VA_ARGS__) -#else // !STA_DEBUG_SERIAL_ENABLED -# define STA_DEBUG_PRINT(...) ((void)0) -# define STA_DEBUG_PRINTLN(...) ((void)0) -#endif // !STA_DEBUG_SERIAL_ENABLED - - -#endif // STA_CORE_DEBUG_SERIAL_HPP diff --git a/include/sta/devices/raspi/bus/i2c.hpp b/include/sta/devices/raspi/bus/i2c.hpp index 597b4bd..d2573ac 100644 --- a/include/sta/devices/raspi/bus/i2c.hpp +++ b/include/sta/devices/raspi/bus/i2c.hpp @@ -12,16 +12,16 @@ namespace sta { - enum class I2cNode { + enum class I2CNode { DEV_1, DEV_2 }; - class RaspiI2c : public I2c + class RaspiI2C : public I2C { public: - RaspiI2c(I2cNode node, Mutex * mutex=nullptr, bool persistent_open=false); - ~RaspiI2c(); + RaspiI2C(I2CNode node, Mutex * mutex=nullptr, bool persistent_open=false); + ~RaspiI2C(); void transfer(uint8_t value) override; void transfer16(uint16_t value) override; @@ -29,11 +29,10 @@ namespace sta void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) override; void receive(uint8_t * buffer, size_t size) override; - void fill(uint8_t value, size_t count) override; - - void selectAddress(uint16_t address) override; void acquire() override; void release() override; + + void fill(uint8_t value, size_t count) override; private: char * i2cdev_; int i2cfd_; @@ -41,9 +40,9 @@ namespace sta const bool persistent_open_; }; - class RaspiI2cDevice : public I2cDevice + class RaspiI2CDevice : public I2CDevice { - RaspiI2cDevice(I2c * intf, uint16_t address_10bit, Mutex* mutex=nullptr, bool master=false, bool blocking=true); + RaspiI2CDevice(RaspiI2C * intf, uint16_t address_10bit, Mutex* mutex=nullptr, bool master=false, bool blocking=true); }; } // namespace sta diff --git a/src/bus/device.cpp b/src/bus/device.cpp index fb2365f..7696143 100644 --- a/src/bus/device.cpp +++ b/src/bus/device.cpp @@ -1,5 +1,5 @@ #include -#include +#include namespace sta { diff --git a/src/bus/i2c/device.cpp b/src/bus/i2c/device.cpp index 0235b7c..9d152ac 100644 --- a/src/bus/i2c/device.cpp +++ b/src/bus/i2c/device.cpp @@ -1,6 +1,6 @@ #include -#include +#include namespace sta diff --git a/src/bus/interface.cpp b/src/bus/interface.cpp index a727dee..627e863 100644 --- a/src/bus/interface.cpp +++ b/src/bus/interface.cpp @@ -1,6 +1,6 @@ #include -#include +#include namespace sta { diff --git a/src/bus/spi/device.cpp b/src/bus/spi/device.cpp index 29c25b2..cca3fa1 100644 --- a/src/bus/spi/device.cpp +++ b/src/bus/spi/device.cpp @@ -1,6 +1,6 @@ #include -#include +#include namespace sta diff --git a/src/bus/spi/settings.cpp b/src/bus/spi/settings.cpp index d0c9246..9fbf8e4 100644 --- a/src/bus/spi/settings.cpp +++ b/src/bus/spi/settings.cpp @@ -1,6 +1,6 @@ #include -#include +#include #include diff --git a/src/can/iter.cpp b/src/can/iter.cpp index b5dff60..5288bef 100644 --- a/src/can/iter.cpp +++ b/src/can/iter.cpp @@ -1,6 +1,6 @@ #include -#include +#include namespace sta diff --git a/src/assert.cpp b/src/debug/assert.cpp similarity index 63% rename from src/assert.cpp rename to src/debug/assert.cpp index 3c02c58..2f819ab 100644 --- a/src/assert.cpp +++ b/src/debug/assert.cpp @@ -1,22 +1,16 @@ -#include +#include #ifdef STA_ASSERT_ENABLED -// TODO: This will probably destroy some stuff when working with stm32! -#ifdef STA_PRINTF_USE_STDLIB -# include -#else -# include -#endif - +#include #include +#include namespace sta { STA_WEAK void assert_failed(const char * expr, const char * file, uint32_t line) { - // printf("%s:%d: Assertion failed: %s", file, line, expr) STA_DEBUG_PRINT(file); STA_DEBUG_PRINT(':'); STA_DEBUG_PRINT(line); diff --git a/src/printable.cpp b/src/debug/printing/printable.cpp similarity index 98% rename from src/printable.cpp rename to src/debug/printing/printable.cpp index 4031032..df6dcba 100644 --- a/src/printable.cpp +++ b/src/debug/printing/printable.cpp @@ -1,10 +1,10 @@ -#include +#include #include #include #include -#include +#include #include namespace sta diff --git a/src/debug/printing/printable_printf.cpp b/src/debug/printing/printable_printf.cpp new file mode 100644 index 0000000..07357f5 --- /dev/null +++ b/src/debug/printing/printable_printf.cpp @@ -0,0 +1,20 @@ +#include +#include + +#include + +namespace sta +{ + PrintablePrintf::PrintablePrintf() + { + + } + + void PrintablePrintf::print(const char * str, size_t length) + { + STA_ASSERT(str != nullptr); + STA_ASSERT(length > 0); + + printf("%.*s", length, str); + } +} // namespace sta diff --git a/src/printable_uart.cpp b/src/debug/printing/printable_uart.cpp similarity index 85% rename from src/printable_uart.cpp rename to src/debug/printing/printable_uart.cpp index 5a6ae2e..76a63f8 100644 --- a/src/printable_uart.cpp +++ b/src/debug/printing/printable_uart.cpp @@ -1,6 +1,6 @@ -#include +#include -#include +#include #include #include diff --git a/src/debug_serial.cpp b/src/debug_serial.cpp deleted file mode 100644 index b6ff7fe..0000000 --- a/src/debug_serial.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include -#ifdef STA_DEBUG_SERIAL_ENABLED - - -#ifdef STA_PLATFORM_STM32 - -#include - -// #include - -// Set platform UART alias -using PlatformUART = sta::STM32UART; - -#endif // STA_PLATFORM_STM32 - - -namespace -{ - // Create platform specific serial interface - PlatformUART platformDebugSerial(&STA_DEBUG_SERIAL_UART); -} - -namespace sta -{ - // Create debug serial object using platform specific serial interface - PrintableUART DebugSerial(&platformDebugSerial); -} // namespace sta - - -#endif // STA_DEBUG_SERIAL_ENABLED diff --git a/src/devices/raspi/bus/i2c.cpp b/src/devices/raspi/bus/i2c.cpp index 7be071f..be270c6 100644 --- a/src/devices/raspi/bus/i2c.cpp +++ b/src/devices/raspi/bus/i2c.cpp @@ -2,7 +2,7 @@ #ifdef STA_PLATFORM_RASPI -#include +#include #include #include @@ -16,16 +16,16 @@ namespace sta { - RaspiI2c::RaspiI2c(I2cNode node, Mutex * mutex, bool persistent_open) - : I2c{mutex}, persistent_open_{persistent_open} + RaspiI2C::RaspiI2C(I2CNode node, Mutex * mutex, bool persistent_open) + : I2C{mutex}, persistent_open_{persistent_open} { // Safer version of malloc + strcpy - i2cdev_ = strdup(node == I2cNode::DEV_1 ? "/dev/i2c-1" : "/dev/i2c-2"); + i2cdev_ = strdup(node == I2CNode::DEV_1 ? "/dev/i2c-1" : "/dev/i2c-2"); STA_ASSERT(i2cdev_ != nullptr); } - RaspiI2c::~RaspiI2c() + RaspiI2C::~RaspiI2C() { if (i2cdev_ != NULL ) { free(i2cdev_); @@ -37,35 +37,35 @@ namespace sta } } - void RaspiI2c::transfer(uint8_t value) + void RaspiI2C::transfer(uint8_t value) { STA_ASSERT(open_); write(i2cfd_, &value, 1); } - void RaspiI2c::transfer16(uint16_t value) + void RaspiI2C::transfer16(uint16_t value) { STA_ASSERT(open_); write(i2cfd_, &value, 2); } - void RaspiI2c::transfer(const uint8_t * buffer, size_t size) + void RaspiI2C::transfer(const uint8_t * buffer, size_t size) { STA_ASSERT(open_); write(i2cfd_, buffer, size); } - void RaspiI2c::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) + void RaspiI2C::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) { STA_ASSERT(open_); // TODO: Is this even possible in i2c? } - void RaspiI2c::receive(uint8_t * buffer, size_t size) + void RaspiI2C::receive(uint8_t * buffer, size_t size) { STA_ASSERT(open_); @@ -75,7 +75,7 @@ namespace sta } } - void RaspiI2c::fill(uint8_t value, size_t count) + void RaspiI2C::fill(uint8_t value, size_t count) { STA_ASSERT(open_); @@ -88,17 +88,9 @@ namespace sta delete [] buffer; } - void RaspiI2c::selectAddress(uint16_t address) + void RaspiI2C::acquire() { - if (ioctl(i2cfd_, I2C_SLAVE, address) < 0) - { - printf("Failed to send the slave address."); - } - } - - void RaspiI2c::acquire() - { - I2c::acquire(); + I2C::acquire(); if (open_) { return; @@ -110,17 +102,17 @@ namespace sta STA_ASSERT(i2cfd_ >= 0); } - void RaspiI2c::release() + void RaspiI2C::release() { if (!persistent_open_ && open_) { close(i2cfd_); } - I2c::release(); + I2C::release(); } - RaspiI2cDevice::RaspiI2cDevice(I2c * intf, uint16_t address_10bit, Mutex* mutex, bool master, bool blocking) - : I2cDevice { intf, address_10bit } + RaspiI2CDevice::RaspiI2CDevice(RaspiI2C * intf, uint16_t address_10bit, Mutex* mutex, bool master, bool blocking) + : I2CDevice { intf, address_10bit } { } diff --git a/src/devices/raspi/bus/spi.cpp b/src/devices/raspi/bus/spi.cpp index 74f71f1..b7eb57b 100644 --- a/src/devices/raspi/bus/spi.cpp +++ b/src/devices/raspi/bus/spi.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include #include #include diff --git a/src/devices/raspi/delay.cpp b/src/devices/raspi/delay.cpp index 3bbc5f1..3eb9641 100644 --- a/src/devices/raspi/delay.cpp +++ b/src/devices/raspi/delay.cpp @@ -3,7 +3,7 @@ #include -#include +#include #include diff --git a/src/devices/raspi/gpio_pin.cpp b/src/devices/raspi/gpio_pin.cpp index fbfb512..e845997 100644 --- a/src/devices/raspi/gpio_pin.cpp +++ b/src/devices/raspi/gpio_pin.cpp @@ -1,7 +1,7 @@ #include #ifdef STA_RASPI_GPIO_ENABLED -#include +#include #include namespace sta diff --git a/src/devices/stm32/bus/i2c.cpp b/src/devices/stm32/bus/i2c.cpp index 83f4dda..9577eec 100644 --- a/src/devices/stm32/bus/i2c.cpp +++ b/src/devices/stm32/bus/i2c.cpp @@ -1,8 +1,10 @@ #include -#include +#include #include +#ifdef STA_PLATFORM_STM32 + namespace sta { STM32I2C::STM32I2C(I2C_HandleTypeDef * handle, Mutex * mutex) @@ -126,3 +128,5 @@ namespace sta } } // namespace sta + +#endif // STA_PLATFORM_STM32 diff --git a/src/devices/stm32/init.cpp b/src/devices/stm32/init.cpp index 843c6d0..0f06016 100644 --- a/src/devices/stm32/init.cpp +++ b/src/devices/stm32/init.cpp @@ -1,6 +1,6 @@ #include -#include +#include #ifdef STA_STM32_DELAY_US_TIM From 5e04b2cfcb2790f957a7599f5df3214069edc2b7 Mon Sep 17 00:00:00 2001 From: Dario Date: Sat, 15 Jul 2023 16:11:07 +0100 Subject: [PATCH 14/22] Added printable prinf and template printable --- .../sta/debug/printing/printable_printf.hpp | 2 -- .../sta/devices/template/custom_printable.hpp | 34 +++++++++++++++++++ src/devices/template/custom_printable.cpp | 18 ++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 include/sta/devices/template/custom_printable.hpp create mode 100644 src/devices/template/custom_printable.cpp diff --git a/include/sta/debug/printing/printable_printf.hpp b/include/sta/debug/printing/printable_printf.hpp index 0d51f04..2ef9ed6 100644 --- a/include/sta/debug/printing/printable_printf.hpp +++ b/include/sta/debug/printing/printable_printf.hpp @@ -23,6 +23,4 @@ namespace sta }; } // namespace sta - - #endif // STA_CORE_PRINTABLE_PRINTF_HPP \ No newline at end of file diff --git a/include/sta/devices/template/custom_printable.hpp b/include/sta/devices/template/custom_printable.hpp new file mode 100644 index 0000000..594ba31 --- /dev/null +++ b/include/sta/devices/template/custom_printable.hpp @@ -0,0 +1,34 @@ +#ifndef STA_CORE_YOUR_DEVICE_CUSTOM_PRINTABLE_HPP +#define STA_CORE_YOUR_DEVICE_CUSTOM_PRINTABLE_HPP + +#include + +#if defined(STA_PLATFORM_YOUR_DEVICE) || defined(DOXYGEN) + +#include + +namespace sta +{ + class CustomPrintable : public Printable + { + public: + /** + * @brief Constructor for your custom printable. + * + * Delete if not needed. + */ + CustomPrintable(/* YOUR ARGMENTS */); + + /** + * @brief Print string. + * + * @param str String buffer + * @param length String length + */ + void print(const char * str, size_t length) override; + }; +} // namespace sta + +#endif // STA_PLATFORM_YOUR_DEVICE + +#endif // STA_CORE_YOUR_DEVICE_CUSTOM_PRINTABLE_HPP \ No newline at end of file diff --git a/src/devices/template/custom_printable.cpp b/src/devices/template/custom_printable.cpp new file mode 100644 index 0000000..590859a --- /dev/null +++ b/src/devices/template/custom_printable.cpp @@ -0,0 +1,18 @@ +#include + +#ifdef STA_PLATFORM_YOUR_DEVICE + +#include + +namespace sta +{ + void CustomPrintable::print(const char * str, size_t length) + { + STA_ASSERT(str != nullptr); + STA_ASSERT(length > 0); + + // YOUR CODE HERE + } +} // namespace sta + +#endif // STA_PLATFORM_YOUR_DEVICE From 2265131b3f8697283ec48580ddc64a577a36fa91 Mon Sep 17 00:00:00 2001 From: Dario Date: Sat, 15 Jul 2023 16:41:29 +0100 Subject: [PATCH 15/22] added skelettons for stm32 dac and adc --- include/sta/devices/stm32/adc.hpp | 23 +++++++++++++++++++++++ include/sta/devices/stm32/dac.hpp | 17 +++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 include/sta/devices/stm32/adc.hpp create mode 100644 include/sta/devices/stm32/dac.hpp diff --git a/include/sta/devices/stm32/adc.hpp b/include/sta/devices/stm32/adc.hpp new file mode 100644 index 0000000..b77555f --- /dev/null +++ b/include/sta/devices/stm32/adc.hpp @@ -0,0 +1,23 @@ +#ifndef STA_CORE_STM32_ADC_HPP +#define STA_CORE_STM32_ADC_HPP + +#include +#ifdef STA_PLATFORM_STM32 + +#include + +namespace sta +{ + class STM32ADC + { + public: + STM32ADC(); + + private: + + }; +} // namespace sta + +#endif // STA_PLATFORM_STM32 + +#endif // STA_CORE_STM32_ADC_HPP diff --git a/include/sta/devices/stm32/dac.hpp b/include/sta/devices/stm32/dac.hpp new file mode 100644 index 0000000..e85ec5b --- /dev/null +++ b/include/sta/devices/stm32/dac.hpp @@ -0,0 +1,17 @@ +#ifndef STA_CORE_STM32_DAC_HPP +#define STA_CORE_STM32_DAC_HPP + +#include +#ifdef STA_PLATFORM_STM32 + +namespace sta +{ + class DAC + { + + }; +} // namespace sta + +#endif // STA_PLATFORM_STM32 + +#endif // STA_CORE_STM32_DAC_HPP \ No newline at end of file From 0d02d57cbbbc3c295d1ddc433304ff1edcf24e64 Mon Sep 17 00:00:00 2001 From: dvdb97 Date: Sun, 16 Jul 2023 12:02:45 +0200 Subject: [PATCH 16/22] Small fixes for smt32 support --- include/sta/bus/uart/uart.hpp | 2 +- include/sta/config.hpp | 4 ++-- include/sta/devices/stm32/bus/spi.hpp | 13 +++++-------- include/sta/devices/stm32/bus/uart.hpp | 2 +- include/sta/devices/stm32/can.hpp | 2 +- include/sta/devices/stm32/gpio_pin.hpp | 2 ++ src/bus/uart/uart.cpp | 4 ++-- src/devices/stm32/bus/spi.cpp | 10 ++++------ src/devices/stm32/bus/uart.cpp | 6 +++--- src/devices/stm32/can.cpp | 2 +- src/devices/stm32/delay.cpp | 2 +- src/devices/stm32/gpio_pin.cpp | 7 ++++++- 12 files changed, 29 insertions(+), 27 deletions(-) diff --git a/include/sta/bus/uart/uart.hpp b/include/sta/bus/uart/uart.hpp index aee3232..21bff52 100644 --- a/include/sta/bus/uart/uart.hpp +++ b/include/sta/bus/uart/uart.hpp @@ -9,7 +9,7 @@ namespace sta class UART : public Interface { public: - UART(Mutex * mutex=nullptr); + UART(UARTSettings & settings, Mutex * mutex=nullptr); /** * @brief Get %UART interface settings. diff --git a/include/sta/config.hpp b/include/sta/config.hpp index 3b22342..b9d1958 100644 --- a/include/sta/config.hpp +++ b/include/sta/config.hpp @@ -1,11 +1,11 @@ #ifndef STA_CONFIG_HPP #define STA_CONFIG_HPP -#include +#include #define STA_DEBUGGING_ENABLED #define STA_PRINTF_USE_STDLIB -#define STA_ASSERT_FORCE +// #define STA_ASSERT_FORCE #endif // STA_CONFIG_HPP \ No newline at end of file diff --git a/include/sta/devices/stm32/bus/spi.hpp b/include/sta/devices/stm32/bus/spi.hpp index 385e1e9..9ed1ee0 100644 --- a/include/sta/devices/stm32/bus/spi.hpp +++ b/include/sta/devices/stm32/bus/spi.hpp @@ -21,11 +21,11 @@ #if defined(STA_STM32_SPI_ENABLED) || defined(DOXYGEN) -#include -#include +#include +#include -#include -#include +#include +#include /** @@ -92,10 +92,7 @@ namespace sta * @param intf %SPI interface * @param csPin Device CS pin */ - STM32SPIDevice(STM32SPI * intf, STM32GpioPin csPin); - - private: - STM32GpioPin csPin_; /**< Device CS pin */ + STM32SPIDevice(STM32SPI * intf, STM32GpioPin * csPin); }; diff --git a/include/sta/devices/stm32/bus/uart.hpp b/include/sta/devices/stm32/bus/uart.hpp index f50477d..1f70911 100644 --- a/include/sta/devices/stm32/bus/uart.hpp +++ b/include/sta/devices/stm32/bus/uart.hpp @@ -41,7 +41,7 @@ namespace sta /** * @param handle STM32 HAL handle */ - STM32UART(UART_HandleTypeDef * handle, Mutex * mutex); + STM32UART(UART_HandleTypeDef * handle, UARTSettings & settings, Mutex * mutex); void transfer(uint8_t value) override; void transfer16(uint16_t value) override; diff --git a/include/sta/devices/stm32/can.hpp b/include/sta/devices/stm32/can.hpp index 8eb5d70..95a96c5 100644 --- a/include/sta/devices/stm32/can.hpp +++ b/include/sta/devices/stm32/can.hpp @@ -28,7 +28,7 @@ #if defined(STA_STM32_CAN_ENABLED) || defined(DOXYGEN) -#include +#include namespace sta diff --git a/include/sta/devices/stm32/gpio_pin.hpp b/include/sta/devices/stm32/gpio_pin.hpp index ee87f2d..205b297 100644 --- a/include/sta/devices/stm32/gpio_pin.hpp +++ b/include/sta/devices/stm32/gpio_pin.hpp @@ -50,6 +50,8 @@ namespace sta void setState(GpioPinState state) override; + GpioPinState getState() override; + /** * @brief Get GPIO port for pin. * diff --git a/src/bus/uart/uart.cpp b/src/bus/uart/uart.cpp index d2e2767..1b86d1e 100644 --- a/src/bus/uart/uart.cpp +++ b/src/bus/uart/uart.cpp @@ -2,8 +2,8 @@ namespace sta { - UART::UART(Mutex * mutex) - : Interface{mutex} + UART::UART(UARTSettings & settings, Mutex * mutex) + : Interface{mutex}, settings_{settings} { } diff --git a/src/devices/stm32/bus/spi.cpp b/src/devices/stm32/bus/spi.cpp index 7cfa8b5..becfaac 100644 --- a/src/devices/stm32/bus/spi.cpp +++ b/src/devices/stm32/bus/spi.cpp @@ -1,7 +1,7 @@ #include #ifdef STA_STM32_SPI_ENABLED -#include +#include #include #include @@ -66,7 +66,7 @@ namespace sta } - STM32SPI::STM32SPI(SPI_HandleTypeDef * handle, uint32_t pclkFreq, Mutex * mutex = nullptr) + STM32SPI::STM32SPI(SPI_HandleTypeDef * handle, uint32_t pclkFreq, Mutex * mutex) : SPI(getSPISettings(handle, pclkFreq), mutex), handle_{handle} { STA_ASSERT(handle != nullptr); @@ -157,10 +157,8 @@ namespace sta } } - - - STM32SPIDevice::STM32SPIDevice(STM32SPI * intf, STM32GpioPin csPin) - : SPIDevice(intf, &csPin_), csPin_{csPin} + STM32SPIDevice::STM32SPIDevice(STM32SPI * intf, STM32GpioPin * csPin) + : SPIDevice(intf, csPin) {} } // namespace sta diff --git a/src/devices/stm32/bus/uart.cpp b/src/devices/stm32/bus/uart.cpp index 2d36ef3..699e9eb 100644 --- a/src/devices/stm32/bus/uart.cpp +++ b/src/devices/stm32/bus/uart.cpp @@ -1,13 +1,13 @@ #include #ifdef STA_STM32_UART_ENABLED -#include +#include #include namespace sta { - STM32UART::STM32UART(UART_HandleTypeDef * handle, Mutex * mutex) - : UART{mutex}, handle_{handle} + STM32UART::STM32UART(UART_HandleTypeDef * handle, UARTSettings & settings, Mutex * mutex) + : UART{settings, mutex}, handle_{handle} { STA_ASSERT(handle != nullptr); } diff --git a/src/devices/stm32/can.cpp b/src/devices/stm32/can.cpp index 1355b0e..0c485ea 100644 --- a/src/devices/stm32/can.cpp +++ b/src/devices/stm32/can.cpp @@ -1,7 +1,7 @@ #include #ifdef STA_STM32_CAN_ENABLED -#include +#include #include diff --git a/src/devices/stm32/delay.cpp b/src/devices/stm32/delay.cpp index a9d4c51..a7bfd3c 100644 --- a/src/devices/stm32/delay.cpp +++ b/src/devices/stm32/delay.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include #include diff --git a/src/devices/stm32/gpio_pin.cpp b/src/devices/stm32/gpio_pin.cpp index 9318605..416d3ab 100644 --- a/src/devices/stm32/gpio_pin.cpp +++ b/src/devices/stm32/gpio_pin.cpp @@ -1,7 +1,7 @@ #include #ifdef STA_STM32_GPIO_ENABLED -#include +#include #include @@ -18,6 +18,11 @@ namespace sta HAL_GPIO_WritePin(port_, pin_, (state == GpioPinState::GPIO_LOW) ? GPIO_PIN_RESET : GPIO_PIN_SET); } + GpioPinState STM32GpioPin::getState() + { + return HAL_GPIO_ReadPin(port_, pin_) == GPIO_PIN_RESET ? GpioPinState::GPIO_LOW : GpioPinState::GPIO_HIGH; + } + GPIO_TypeDef * STM32GpioPin::getPort() const { return port_; From 266cc46a09afe2be3fb61d98d3faf12e9a96c0d8 Mon Sep 17 00:00:00 2001 From: dvdb97 Date: Sun, 16 Jul 2023 21:17:14 +0200 Subject: [PATCH 17/22] Added ADC implementation for STM32 --- include/sta/devices/stm32/adc.hpp | 20 +++++++++++++++++-- src/devices/stm32/adc.cpp | 33 +++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/devices/stm32/adc.cpp diff --git a/include/sta/devices/stm32/adc.hpp b/include/sta/devices/stm32/adc.hpp index b77555f..b1411be 100644 --- a/include/sta/devices/stm32/adc.hpp +++ b/include/sta/devices/stm32/adc.hpp @@ -11,10 +11,26 @@ namespace sta class STM32ADC { public: - STM32ADC(); + /** + * @param handle A handle to a STM32 ADC. + */ + STM32ADC(ADC_HandleTypeDef * handle); + /** + * @brief Starts conversion of the incoming analog signal. + */ + void start(); + + /** + * @brief + * + * @param timeout + */ + void poll(uint32_t timeout); + + uint32_t getValue(); private: - + ADC_HandleTypeDef * handle_; }; } // namespace sta diff --git a/src/devices/stm32/adc.cpp b/src/devices/stm32/adc.cpp new file mode 100644 index 0000000..01c9eb7 --- /dev/null +++ b/src/devices/stm32/adc.cpp @@ -0,0 +1,33 @@ +#include + +#ifdef STA_PLATFORM_STM32 + +#include + +namespace sta +{ + STM32ADC::STM32ADC(ADC_HandleTypeDef * handle) + : handle_{handle} + { + STA_ASSERT(handle != nullptr); + } + + void STM32ADC::start() + { + HAL_ADC_Start(handle_); + } + + void STM32ADC::poll(uint32_t timeout) + { + HAL_StatusTypeDef res = HAL_ADC_PollForConversion(handle_, timeout); + + STA_ASSERT(res == HAL_OK); + } + + uint32_t STM32ADC::getValue() + { + return HAL_ADC_GetValue(handle_); + } +} // namespace sta + +#endif // STA_PLATFORM_STM32 \ No newline at end of file From 8ffba482deb91de69c408c78d78b22989c03e537 Mon Sep 17 00:00:00 2001 From: dario Date: Thu, 10 Aug 2023 22:54:18 +0200 Subject: [PATCH 18/22] Fixes to make I2C work for SMT32 --- include/sta/bus/interface.hpp | 4 +- include/sta/config.hpp | 2 +- include/sta/devices/stm32/adc.hpp | 9 +++- include/sta/devices/stm32/bus/i2c.hpp | 2 +- src/bus/device.cpp | 12 ++--- src/bus/interface.cpp | 8 ++-- src/devices/stm32/adc.cpp | 4 +- src/devices/stm32/bus/i2c.cpp | 8 ++-- src/devices/stm32/delay.cpp | 68 ++++++++++++++------------- 9 files changed, 64 insertions(+), 53 deletions(-) diff --git a/include/sta/bus/interface.hpp b/include/sta/bus/interface.hpp index a26151a..6e53b9e 100644 --- a/include/sta/bus/interface.hpp +++ b/include/sta/bus/interface.hpp @@ -75,10 +75,10 @@ namespace sta /** * @returns true if the interface has been aquired. */ - bool isAquired(); + bool isAcquired(); private: Mutex * mutex_; - bool aquired_ = false; + bool acquired_ = false; }; } // namespace sta diff --git a/include/sta/config.hpp b/include/sta/config.hpp index b9d1958..f798ba8 100644 --- a/include/sta/config.hpp +++ b/include/sta/config.hpp @@ -8,4 +8,4 @@ // #define STA_ASSERT_FORCE -#endif // STA_CONFIG_HPP \ No newline at end of file +#endif // STA_CONFIG_HPP diff --git a/include/sta/devices/stm32/adc.hpp b/include/sta/devices/stm32/adc.hpp index b1411be..c04e2c6 100644 --- a/include/sta/devices/stm32/adc.hpp +++ b/include/sta/devices/stm32/adc.hpp @@ -3,8 +3,13 @@ #include #ifdef STA_PLATFORM_STM32 +# include +# ifdef HAL_ADC_MODULE_ENABLED +# define STA_STM32_ADC_ENABLED +# endif +#endif -#include +#if defined(STA_STM32_ADC_ENABLED) || defined(DOXYGEN) namespace sta { @@ -34,6 +39,6 @@ namespace sta }; } // namespace sta -#endif // STA_PLATFORM_STM32 +#endif // STA_STM32_ADC_ENABLED #endif // STA_CORE_STM32_ADC_HPP diff --git a/include/sta/devices/stm32/bus/i2c.hpp b/include/sta/devices/stm32/bus/i2c.hpp index b2b34d1..ce71294 100644 --- a/include/sta/devices/stm32/bus/i2c.hpp +++ b/include/sta/devices/stm32/bus/i2c.hpp @@ -30,7 +30,7 @@ namespace sta void fill(uint8_t value, size_t count) override; private: I2C_HandleTypeDef * handle_; - const uint32_t timeout_ = HAL_MAX_DELAY; + const uint32_t timeout_ = 1000; }; class STM32I2CDevice : public I2CDevice diff --git a/src/bus/device.cpp b/src/bus/device.cpp index 7696143..e42646a 100644 --- a/src/bus/device.cpp +++ b/src/bus/device.cpp @@ -25,7 +25,7 @@ namespace sta void Device::transfer(uint8_t value) { - STA_ASSERT(intf_->isAquired()); + STA_ASSERT(intf_->isAcquired()); STA_ASSERT(selected_); intf_->transfer(value); @@ -33,7 +33,7 @@ namespace sta void Device::transfer16(uint16_t value) { - STA_ASSERT(intf_->isAquired()); + STA_ASSERT(intf_->isAcquired()); STA_ASSERT(selected_); intf_->transfer16(value); @@ -41,7 +41,7 @@ namespace sta void Device::transfer(const uint8_t * buffer, size_t size) { - STA_ASSERT(intf_->isAquired()); + STA_ASSERT(intf_->isAcquired()); STA_ASSERT(selected_); STA_ASSERT(buffer != nullptr); @@ -50,7 +50,7 @@ namespace sta void Device::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) { - STA_ASSERT(intf_->isAquired()); + STA_ASSERT(intf_->isAcquired()); STA_ASSERT(selected_); STA_ASSERT(txBuffer != nullptr); STA_ASSERT(rxBuffer != nullptr); @@ -60,7 +60,7 @@ namespace sta void Device::receive(uint8_t * buffer, size_t size) { - STA_ASSERT(intf_->isAquired()); + STA_ASSERT(intf_->isAcquired()); STA_ASSERT(selected_); STA_ASSERT(buffer != nullptr); @@ -69,7 +69,7 @@ namespace sta void Device::fill(uint8_t value, size_t count) { - STA_ASSERT(intf_->isAquired()); + STA_ASSERT(intf_->isAcquired()); STA_ASSERT(selected_); intf_->fill(value, count); diff --git a/src/bus/interface.cpp b/src/bus/interface.cpp index 627e863..b7ff0a7 100644 --- a/src/bus/interface.cpp +++ b/src/bus/interface.cpp @@ -5,7 +5,7 @@ namespace sta { Interface::Interface(Mutex * mutex) - : mutex_{mutex} + : mutex_{mutex}, acquired_{false} { STA_ASSERT(mutex != nullptr); } @@ -14,16 +14,18 @@ namespace sta { if (mutex_ != nullptr) mutex_->acquire(); + acquired_ = true; } void Interface::release() { if (mutex_ != nullptr) + acquired_ = false; mutex_->release(); } - bool Interface::isAquired() + bool Interface::isAcquired() { - return aquired_; + return acquired_; } } // namespace sta diff --git a/src/devices/stm32/adc.cpp b/src/devices/stm32/adc.cpp index 01c9eb7..5c5d663 100644 --- a/src/devices/stm32/adc.cpp +++ b/src/devices/stm32/adc.cpp @@ -1,6 +1,6 @@ #include -#ifdef STA_PLATFORM_STM32 +#ifdef STA_STM32_ADC_ENABLED #include @@ -30,4 +30,4 @@ namespace sta } } // namespace sta -#endif // STA_PLATFORM_STM32 \ No newline at end of file +#endif // STA_STM32_ADC_ENABLED diff --git a/src/devices/stm32/bus/i2c.cpp b/src/devices/stm32/bus/i2c.cpp index 9577eec..5eae138 100644 --- a/src/devices/stm32/bus/i2c.cpp +++ b/src/devices/stm32/bus/i2c.cpp @@ -3,7 +3,7 @@ #include #include -#ifdef STA_PLATFORM_STM32 +#ifdef STA_STM32_I2C_ENABLED namespace sta { @@ -42,7 +42,7 @@ namespace sta } else { if (master_) { - res = HAL_I2C_Slave_Transmit(handle_, reinterpret_cast(&value), 2, timeout_); + res = HAL_I2C_Slave_Transmit_IT(handle_, reinterpret_cast(&value), 2); } else { res = HAL_I2C_Slave_Transmit_IT(handle_, reinterpret_cast(&value), 2); } @@ -93,7 +93,7 @@ namespace sta HAL_StatusTypeDef res; if (blocking_) { - if (!master_) { + if (master_) { res = HAL_I2C_Master_Receive(handle_, address_, buffer, size, timeout_); } else { res = HAL_I2C_Slave_Receive(handle_, buffer, size, timeout_); @@ -129,4 +129,4 @@ namespace sta } // namespace sta -#endif // STA_PLATFORM_STM32 +#endif // STA_STM32_I2C_ENABLED diff --git a/src/devices/stm32/delay.cpp b/src/devices/stm32/delay.cpp index a7bfd3c..8f51d73 100644 --- a/src/devices/stm32/delay.cpp +++ b/src/devices/stm32/delay.cpp @@ -1,4 +1,5 @@ #include + #ifdef STA_PLATFORM_STM32 #include @@ -29,44 +30,47 @@ namespace sta { uint32_t gDelayUsMul = 1; + bool isValidDelayUsTIM() + { + // Get PCLK multiplier for TIM clock + uint32_t pclkMul = 1; + switch (STA_STM32_DELAY_US_TIM.Init.ClockDivision) + { + case TIM_CLOCKDIVISION_DIV1: + pclkMul = 1; + break; + case TIM_CLOCKDIVISION_DIV2: + pclkMul = 2; + break; + case TIM_CLOCKDIVISION_DIV4: + pclkMul = 4; + break; + default: + STA_ASSERT(false); + STA_UNREACHABLE(); + } + + // Calculate TIM clock frequency + uint32_t clkFreq = pclkMul * STA_STM32_GET_HANDLE_PCLK_FREQ_FN(STA_STM32_DELAY_US_TIM)(); + // Calculate update frequency based on prescaler value + uint32_t prescaler = (STA_STM32_DELAY_US_TIM.Init.Prescaler) ? STA_STM32_DELAY_US_TIM.Init.Prescaler : 1; + uint32_t updateFreq = clkFreq / prescaler; + + gDelayUsMul = updateFreq / 1000000; + + // TIM must have at least microsecond precision (>= 1 MHz frequency) + return (updateFreq >= 1000000); + } + void delayUs(uint32_t us) { + // Check if the specified timer is usable for microsecond delays. + STA_ASSERT(isValidDelayUsTIM()); + __HAL_TIM_SET_COUNTER(&STA_STM32_DELAY_US_TIM, 0); while (__HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM) < us * gDelayUsMul); } - - bool isValidDelayUsTIM() - { - // Get PCLK multiplier for TIM clock - uint32_t pclkMul = 1; - switch (STA_STM32_DELAY_US_TIM.Init.ClockDivision) - { - case TIM_CLOCKDIVISION_DIV1: - pclkMul = 1; - break; - case TIM_CLOCKDIVISION_DIV2: - pclkMul = 2; - break; - case TIM_CLOCKDIVISION_DIV4: - pclkMul = 4; - break; - default: - STA_ASSERT(false); - STA_UNREACHABLE(); - } - - // Calculate TIM clock frequency - uint32_t clkFreq = pclkMul * STA_STM32_GET_HANDLE_PCLK_FREQ_FN(STA_STM32_DELAY_US_TIM)(); - // Calculate update frequency based on prescaler value - uint32_t prescaler = (STA_STM32_DELAY_US_TIM.Init.Prescaler) ? STA_STM32_DELAY_US_TIM.Init.Prescaler : 1; - uint32_t updateFreq = clkFreq / prescaler; - - gDelayUsMul = updateFreq / 1000000; - - // TIM must have at least microsecond precision (>= 1 MHz frequency) - return (updateFreq >= 1000000); - } } // namespace sta #endif // STA_STM32_DELAY_US_TIM From 017c07a07747760a0ea392e8e0a4a02f7337b56e Mon Sep 17 00:00:00 2001 From: Dario Date: Mon, 14 Aug 2023 21:04:23 +0100 Subject: [PATCH 19/22] Added changes for Raspi I2C --- include/sta/bus/i2c/device.hpp | 2 +- include/sta/bus/interface.hpp | 2 +- include/sta/devices/raspi/bus/i2c.hpp | 3 ++- src/bus/interface.cpp | 10 ++++----- src/devices/raspi/bus/i2c.cpp | 30 +++++++++++++++++++++------ 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/include/sta/bus/i2c/device.hpp b/include/sta/bus/i2c/device.hpp index 88485b3..71552fc 100644 --- a/include/sta/bus/i2c/device.hpp +++ b/include/sta/bus/i2c/device.hpp @@ -18,7 +18,7 @@ namespace sta * @param intf %I2C hardware interface * @param csPin The peripheral's address. */ - I2CDevice(I2C * intf, int address, bool master=false, bool blocking=true); + I2CDevice(I2C * intf, int address, bool master=true, bool blocking=true); protected: void select() override; diff --git a/include/sta/bus/interface.hpp b/include/sta/bus/interface.hpp index a26151a..f5bbd9b 100644 --- a/include/sta/bus/interface.hpp +++ b/include/sta/bus/interface.hpp @@ -78,7 +78,7 @@ namespace sta bool isAquired(); private: Mutex * mutex_; - bool aquired_ = false; + bool acquired_ = false; }; } // namespace sta diff --git a/include/sta/devices/raspi/bus/i2c.hpp b/include/sta/devices/raspi/bus/i2c.hpp index d2573ac..b16449c 100644 --- a/include/sta/devices/raspi/bus/i2c.hpp +++ b/include/sta/devices/raspi/bus/i2c.hpp @@ -42,7 +42,8 @@ namespace sta class RaspiI2CDevice : public I2CDevice { - RaspiI2CDevice(RaspiI2C * intf, uint16_t address_10bit, Mutex* mutex=nullptr, bool master=false, bool blocking=true); + public: + RaspiI2CDevice(RaspiI2C * intf, uint16_t address_10bit, bool master=true, bool blocking=true); }; } // namespace sta diff --git a/src/bus/interface.cpp b/src/bus/interface.cpp index 627e863..d32292d 100644 --- a/src/bus/interface.cpp +++ b/src/bus/interface.cpp @@ -12,18 +12,18 @@ namespace sta void Interface::acquire() { - if (mutex_ != nullptr) - mutex_->acquire(); + mutex_->acquire(); + acquired_ = true; } void Interface::release() { - if (mutex_ != nullptr) - mutex_->release(); + mutex_->release(); + acquired_ = false; } bool Interface::isAquired() { - return aquired_; + return acquired_; } } // namespace sta diff --git a/src/devices/raspi/bus/i2c.cpp b/src/devices/raspi/bus/i2c.cpp index be270c6..689d7df 100644 --- a/src/devices/raspi/bus/i2c.cpp +++ b/src/devices/raspi/bus/i2c.cpp @@ -3,6 +3,7 @@ #ifdef STA_PLATFORM_RASPI #include +#include #include #include @@ -11,6 +12,7 @@ #include #include #include +#include #include @@ -41,21 +43,33 @@ namespace sta { STA_ASSERT(open_); - write(i2cfd_, &value, 1); + ssize_t n_out = write(i2cfd_, &value, 1); + + if (n_out < 0) + { + STA_DEBUG_PRINT("Transfer of single byte failed: "); + STA_DEBUG_PRINTLN(strerror(errno)); + } + + STA_ASSERT(n_out == 1); } void RaspiI2C::transfer16(uint16_t value) { STA_ASSERT(open_); - write(i2cfd_, &value, 2); + ssize_t n_out = write(i2cfd_, &value, 2); + + STA_ASSERT(n_out == 2); } void RaspiI2C::transfer(const uint8_t * buffer, size_t size) { STA_ASSERT(open_); - write(i2cfd_, buffer, size); + ssize_t n_out = write(i2cfd_, buffer, size); + + STA_ASSERT(n_out == size); } void RaspiI2C::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) @@ -83,7 +97,9 @@ namespace sta uint8_t *buffer = new uint8_t[count]; memset(buffer, value, count); - write(i2cfd_, buffer, count); + ssize_t n_out = write(i2cfd_, buffer, count); + + STA_ASSERT(n_out == count); delete [] buffer; } @@ -100,6 +116,8 @@ namespace sta open_ = true; STA_ASSERT(i2cfd_ >= 0); + + STA_DEBUG_PRINTLN("Successfully opened file"); } void RaspiI2C::release() @@ -111,8 +129,8 @@ namespace sta I2C::release(); } - RaspiI2CDevice::RaspiI2CDevice(RaspiI2C * intf, uint16_t address_10bit, Mutex* mutex, bool master, bool blocking) - : I2CDevice { intf, address_10bit } + RaspiI2CDevice::RaspiI2CDevice(RaspiI2C * intf, uint16_t address_10bit, bool master, bool blocking) + : I2CDevice { intf, address_10bit, master, blocking } { } From 558a574793f78088b5ff41f5177b980f938cd230 Mon Sep 17 00:00:00 2001 From: Dario Date: Tue, 15 Aug 2023 17:01:32 +0100 Subject: [PATCH 20/22] fixes for raspi i2c support --- include/sta/bus/i2c/device.hpp | 1 - include/sta/config.hpp | 3 ++- include/sta/devices/raspi/bus/i2c.hpp | 1 + src/devices/raspi/bus/i2c.cpp | 32 +++++++++++++++++---------- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/include/sta/bus/i2c/device.hpp b/include/sta/bus/i2c/device.hpp index 71552fc..5894b5b 100644 --- a/include/sta/bus/i2c/device.hpp +++ b/include/sta/bus/i2c/device.hpp @@ -22,7 +22,6 @@ namespace sta protected: void select() override; - void deselect() override; private: diff --git a/include/sta/config.hpp b/include/sta/config.hpp index f798ba8..725478f 100644 --- a/include/sta/config.hpp +++ b/include/sta/config.hpp @@ -1,7 +1,8 @@ #ifndef STA_CONFIG_HPP #define STA_CONFIG_HPP -#include +// #include +#include #define STA_DEBUGGING_ENABLED #define STA_PRINTF_USE_STDLIB diff --git a/include/sta/devices/raspi/bus/i2c.hpp b/include/sta/devices/raspi/bus/i2c.hpp index b16449c..d2ba9ef 100644 --- a/include/sta/devices/raspi/bus/i2c.hpp +++ b/include/sta/devices/raspi/bus/i2c.hpp @@ -29,6 +29,7 @@ namespace sta void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) override; void receive(uint8_t * buffer, size_t size) override; + void selectAddress(); void acquire() override; void release() override; diff --git a/src/devices/raspi/bus/i2c.cpp b/src/devices/raspi/bus/i2c.cpp index 689d7df..393a0e4 100644 --- a/src/devices/raspi/bus/i2c.cpp +++ b/src/devices/raspi/bus/i2c.cpp @@ -43,14 +43,9 @@ namespace sta { STA_ASSERT(open_); + selectAddress(); ssize_t n_out = write(i2cfd_, &value, 1); - if (n_out < 0) - { - STA_DEBUG_PRINT("Transfer of single byte failed: "); - STA_DEBUG_PRINTLN(strerror(errno)); - } - STA_ASSERT(n_out == 1); } @@ -58,6 +53,7 @@ namespace sta { STA_ASSERT(open_); + selectAddress(); ssize_t n_out = write(i2cfd_, &value, 2); STA_ASSERT(n_out == 2); @@ -67,6 +63,7 @@ namespace sta { STA_ASSERT(open_); + selectAddress(); ssize_t n_out = write(i2cfd_, buffer, size); STA_ASSERT(n_out == size); @@ -83,16 +80,18 @@ namespace sta { STA_ASSERT(open_); - if (read(i2cfd_, buffer, size) <= 0) - { - printf("Error while reading i2c bus."); - } + selectAddress(); + ssize_t n_in = read(i2cfd_, buffer, size); + + STA_ASSERT(n_in >= 0); } void RaspiI2C::fill(uint8_t value, size_t count) { STA_ASSERT(open_); + selectAddress(); + // Initialize a buffer of size count and fill it with the value. uint8_t *buffer = new uint8_t[count]; memset(buffer, value, count); @@ -104,6 +103,14 @@ namespace sta delete [] buffer; } + void RaspiI2C::selectAddress() + { + // Select the slave with the given address. + int rslt = ioctl(i2cfd_, I2C_SLAVE, address_); + + STA_ASSERT(rslt != -1); + } + void RaspiI2C::acquire() { I2C::acquire(); @@ -116,14 +123,13 @@ namespace sta open_ = true; STA_ASSERT(i2cfd_ >= 0); - - STA_DEBUG_PRINTLN("Successfully opened file"); } void RaspiI2C::release() { if (!persistent_open_ && open_) { close(i2cfd_); + open_ = false; } I2C::release(); @@ -134,6 +140,8 @@ namespace sta { } + + } // namespace sta #endif // STA_PLATFORM_RASPI \ No newline at end of file From 4c157bbe52a039ea8f159eef7e8d177d01d66156 Mon Sep 17 00:00:00 2001 From: dario Date: Sun, 20 Aug 2023 17:38:48 +0200 Subject: [PATCH 21/22] Moved simultaneous reading and writing into SPI; removed it from I2C and UART --- include/sta/bus/device.hpp | 11 +---------- include/sta/bus/interface.hpp | 8 -------- include/sta/bus/spi/device.hpp | 9 +++++++++ include/sta/bus/spi/spi.hpp | 9 +++++++++ include/sta/devices/raspi/bus/i2c.hpp | 3 +-- include/sta/devices/stm32/bus/i2c.hpp | 1 - include/sta/devices/stm32/bus/uart.hpp | 1 - src/bus/device.cpp | 10 ---------- src/bus/spi/device.cpp | 9 +++++++++ src/devices/raspi/bus/i2c.cpp | 9 +-------- src/devices/stm32/bus/i2c.cpp | 5 ----- src/devices/stm32/bus/uart.cpp | 5 ----- 12 files changed, 30 insertions(+), 50 deletions(-) diff --git a/include/sta/bus/device.hpp b/include/sta/bus/device.hpp index 55ea4a4..c6dc208 100644 --- a/include/sta/bus/device.hpp +++ b/include/sta/bus/device.hpp @@ -53,15 +53,6 @@ namespace sta */ void transfer(const uint8_t * buffer, size_t size); - /** - * @brief Send and receive data simultaneously. - * - * @param txBuffer Send buffer - * @param rxBuffer Receive buffer - * @param size Number of bytes to transfer - */ - void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size); - /** * @brief Read incoming data to buffer. * @@ -95,4 +86,4 @@ namespace sta }; } // namespace sta -#endif // STA_CORE_BUS_SERIAL_DEVICE_HPP \ No newline at end of file +#endif // STA_CORE_BUS_SERIAL_DEVICE_HPP diff --git a/include/sta/bus/interface.hpp b/include/sta/bus/interface.hpp index 6e53b9e..e4a0cd8 100644 --- a/include/sta/bus/interface.hpp +++ b/include/sta/bus/interface.hpp @@ -35,14 +35,6 @@ namespace sta * @param size Number of bytes to transfer */ virtual void transfer(const uint8_t * buffer, size_t size) = 0; - /** - * @brief Send and receive data simultaneously. - * - * @param txBuffer Send buffer - * @param rxBuffer Receive buffer - * @param size Number of bytes to transfer - */ - virtual void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) = 0; /** * @brief Read incoming data to buffer. * diff --git a/include/sta/bus/spi/device.hpp b/include/sta/bus/spi/device.hpp index 5f22b87..adeed46 100644 --- a/include/sta/bus/spi/device.hpp +++ b/include/sta/bus/spi/device.hpp @@ -29,6 +29,15 @@ namespace sta */ SPIDevice(SPI * intf, GpioPin * csPin); + /** + * @brief Send and receive data simultaneously. + * + * @param txBuffer Send buffer + * @param rxBuffer Receive buffer + * @param size Number of bytes to transfer + */ + void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size); + /** * @brief Get %SPI interface settings. * diff --git a/include/sta/bus/spi/spi.hpp b/include/sta/bus/spi/spi.hpp index fdf978f..2d0dc14 100644 --- a/include/sta/bus/spi/spi.hpp +++ b/include/sta/bus/spi/spi.hpp @@ -31,6 +31,15 @@ namespace sta */ SPI(const SPISettings & settings, Mutex * mutex = nullptr); + /** + * @brief Send and receive data simultaneously. + * + * @param txBuffer Send buffer + * @param rxBuffer Receive buffer + * @param size Number of bytes to transfer + */ + virtual void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) = 0; + /** * @brief Get %SPI interface settings. * diff --git a/include/sta/devices/raspi/bus/i2c.hpp b/include/sta/devices/raspi/bus/i2c.hpp index d2573ac..c4c8ee7 100644 --- a/include/sta/devices/raspi/bus/i2c.hpp +++ b/include/sta/devices/raspi/bus/i2c.hpp @@ -26,7 +26,6 @@ namespace sta void transfer(uint8_t value) override; void transfer16(uint16_t value) override; void transfer(const uint8_t * buffer, size_t size) override; - void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) override; void receive(uint8_t * buffer, size_t size) override; void acquire() override; @@ -48,4 +47,4 @@ namespace sta #endif // STA_PLATFORM_RASPI -#endif // STA_I2C_HPP \ No newline at end of file +#endif // STA_I2C_HPP diff --git a/include/sta/devices/stm32/bus/i2c.hpp b/include/sta/devices/stm32/bus/i2c.hpp index ce71294..c195709 100644 --- a/include/sta/devices/stm32/bus/i2c.hpp +++ b/include/sta/devices/stm32/bus/i2c.hpp @@ -24,7 +24,6 @@ namespace sta void transfer(uint8_t value) override; void transfer16(uint16_t value) override; void transfer(const uint8_t * buffer, size_t size) override; - void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) override; void receive(uint8_t * buffer, size_t size) override; void fill(uint8_t value, size_t count) override; diff --git a/include/sta/devices/stm32/bus/uart.hpp b/include/sta/devices/stm32/bus/uart.hpp index 1f70911..5982c8b 100644 --- a/include/sta/devices/stm32/bus/uart.hpp +++ b/include/sta/devices/stm32/bus/uart.hpp @@ -46,7 +46,6 @@ namespace sta void transfer(uint8_t value) override; void transfer16(uint16_t value) override; void transfer(const uint8_t * buffer, size_t size) override; - void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) override; void receive(uint8_t * buffer, size_t size) override; void fill(uint8_t value, size_t count) override; diff --git a/src/bus/device.cpp b/src/bus/device.cpp index e42646a..85713d0 100644 --- a/src/bus/device.cpp +++ b/src/bus/device.cpp @@ -48,16 +48,6 @@ namespace sta intf_->transfer(buffer, size); } - void Device::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) - { - STA_ASSERT(intf_->isAcquired()); - STA_ASSERT(selected_); - STA_ASSERT(txBuffer != nullptr); - STA_ASSERT(rxBuffer != nullptr); - - intf_->transfer(txBuffer, rxBuffer, size); - } - void Device::receive(uint8_t * buffer, size_t size) { STA_ASSERT(intf_->isAcquired()); diff --git a/src/bus/spi/device.cpp b/src/bus/spi/device.cpp index cca3fa1..60b273a 100644 --- a/src/bus/spi/device.cpp +++ b/src/bus/spi/device.cpp @@ -12,6 +12,15 @@ namespace sta STA_ASSERT(csPin != nullptr); } + void SPIDevice::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) + { + STA_ASSERT(intf_->isAcquired()); + STA_ASSERT(txBuffer != nullptr); + STA_ASSERT(rxBuffer != nullptr); + + intf_->transfer(txBuffer, rxBuffer, size); + } + const SPISettings & SPIDevice::settings() const { return intf_->settings(); diff --git a/src/devices/raspi/bus/i2c.cpp b/src/devices/raspi/bus/i2c.cpp index be270c6..bb23bf2 100644 --- a/src/devices/raspi/bus/i2c.cpp +++ b/src/devices/raspi/bus/i2c.cpp @@ -58,13 +58,6 @@ namespace sta write(i2cfd_, buffer, size); } - void RaspiI2C::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) - { - STA_ASSERT(open_); - - // TODO: Is this even possible in i2c? - } - void RaspiI2C::receive(uint8_t * buffer, size_t size) { STA_ASSERT(open_); @@ -118,4 +111,4 @@ namespace sta } } // namespace sta -#endif // STA_PLATFORM_RASPI \ No newline at end of file +#endif // STA_PLATFORM_RASPI diff --git a/src/devices/stm32/bus/i2c.cpp b/src/devices/stm32/bus/i2c.cpp index 5eae138..6478f8a 100644 --- a/src/devices/stm32/bus/i2c.cpp +++ b/src/devices/stm32/bus/i2c.cpp @@ -83,11 +83,6 @@ namespace sta STA_ASSERT(res == HAL_OK); } - void STM32I2C::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) - { - // TODO: Is this even something necessary for I2C? - } - void STM32I2C::receive(uint8_t * buffer, size_t size) { HAL_StatusTypeDef res; diff --git a/src/devices/stm32/bus/uart.cpp b/src/devices/stm32/bus/uart.cpp index 699e9eb..300f49f 100644 --- a/src/devices/stm32/bus/uart.cpp +++ b/src/devices/stm32/bus/uart.cpp @@ -29,11 +29,6 @@ namespace sta HAL_UART_Transmit(handle_, const_cast(buffer), size, HAL_MAX_DELAY); } - void STM32UART::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) - { - // IS THIS A THING HERE? - } - void STM32UART::receive(uint8_t * buffer, size_t size) { STA_ASSERT(buffer != nullptr); From 34c92626a1e2dcd320f84be545f71ca59fc39808 Mon Sep 17 00:00:00 2001 From: dario Date: Sun, 20 Aug 2023 17:55:40 +0200 Subject: [PATCH 22/22] Last clean-up; removed config.hpp --- include/sta/bus/i2c/device.hpp | 6 +++--- include/sta/bus/i2c/i2c.hpp | 5 +++-- include/sta/bus/uart/settings.hpp | 2 -- include/sta/bus/uart/uart.hpp | 4 ++++ include/sta/config.hpp | 12 ------------ 5 files changed, 10 insertions(+), 19 deletions(-) delete mode 100644 include/sta/config.hpp diff --git a/include/sta/bus/i2c/device.hpp b/include/sta/bus/i2c/device.hpp index 5894b5b..ec6fb7b 100644 --- a/include/sta/bus/i2c/device.hpp +++ b/include/sta/bus/i2c/device.hpp @@ -26,9 +26,9 @@ namespace sta private: I2C * intf_; - int address_; /**< device address */ - int master_; - int blocking_; + int address_; /**< device address */ + int master_; /**< is the mcu the master? */ + int blocking_; /**< blocking or non-blocking transmits / receives */ }; } // namespace sta diff --git a/include/sta/bus/i2c/i2c.hpp b/include/sta/bus/i2c/i2c.hpp index e2e37c4..1af5571 100644 --- a/include/sta/bus/i2c/i2c.hpp +++ b/include/sta/bus/i2c/i2c.hpp @@ -24,8 +24,9 @@ namespace sta /** * @brief Specify the mode of communication via the bus. * - * @param master - * @param blocking + * @param address The peripheral's address to communicate with. + * @param master Whether the mcu is a master or slave. + * @param blocking Whether to use blocking or non-blocking transmits / receives. */ void setSettings(uint16_t address, bool master, bool blocking); diff --git a/include/sta/bus/uart/settings.hpp b/include/sta/bus/uart/settings.hpp index 390774c..c3bbe2b 100644 --- a/include/sta/bus/uart/settings.hpp +++ b/include/sta/bus/uart/settings.hpp @@ -5,8 +5,6 @@ namespace sta { - - enum class UARTMode { RX, diff --git a/include/sta/bus/uart/uart.hpp b/include/sta/bus/uart/uart.hpp index 21bff52..0460c3e 100644 --- a/include/sta/bus/uart/uart.hpp +++ b/include/sta/bus/uart/uart.hpp @@ -9,6 +9,10 @@ namespace sta class UART : public Interface { public: + /** + * @param settings %UART bus settings + * @param mutex Mutex object for managing shared access. Pass nullptr for no access control + */ UART(UARTSettings & settings, Mutex * mutex=nullptr); /** diff --git a/include/sta/config.hpp b/include/sta/config.hpp deleted file mode 100644 index 725478f..0000000 --- a/include/sta/config.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef STA_CONFIG_HPP -#define STA_CONFIG_HPP - -// #include -#include - -#define STA_DEBUGGING_ENABLED -#define STA_PRINTF_USE_STDLIB - -// #define STA_ASSERT_FORCE - -#endif // STA_CONFIG_HPP