Added I2C support for raspi & first rework of debugging

This commit is contained in:
Dario 2023-06-23 15:50:54 +01:00
parent 3cf2173433
commit 6b4acfd27b
70 changed files with 985 additions and 772 deletions

View File

@ -12,9 +12,9 @@
*/
#include <sta/can/filter.hpp>
#include <sta/can/headers.hpp>
#include <sta/can/iter.hpp>
#include <sta/bus/can/filter.hpp>
#include <sta/bus/can/headers.hpp>
#include <sta/bus/can/iter.hpp>
namespace sta

View File

@ -5,7 +5,7 @@
#ifndef STA_CORE_CAN_FILTER_HPP
#define STA_CORE_CAN_FILTER_HPP
#include <sta/can/id.hpp>
#include <sta/bus/can/id.hpp>
#include <cstdint>

View File

@ -5,7 +5,7 @@
#ifndef STA_CORE_CAN_HEADERS_HPP
#define STA_CORE_CAN_HEADERS_HPP
#include <sta/can/id.hpp>
#include <sta/bus/can/id.hpp>
#include <cstdint>

View File

@ -5,8 +5,8 @@
#ifndef STA_CORE_CAN_SUBSCRIBABLE_HPP
#define STA_CORE_CAN_SUBSCRIBABLE_HPP
#include <sta/can/filter.hpp>
#include <sta/can/headers.hpp>
#include <sta/bus/can/filter.hpp>
#include <sta/bus/can/headers.hpp>
namespace sta
@ -96,7 +96,7 @@ namespace sta
} // namespace sta
#include <sta/can/subscribable.tpp>
#include <sta/bus/can/subscribable.tpp>
#endif // STA_CORE_CAN_SUBSCRIBABLE_HPP
#endif // STA_CORE_CAN_SUBSCRIBABLE_HPP

View File

@ -1,33 +1,21 @@
/**
* @file
* @brief SPI bus peripheral device.
*/
#ifndef STA_CORE_SPI_DEVICE_HPP
#define STA_CORE_SPI_DEVICE_HPP
#include <sta/gpio_pin.hpp>
#include <sta/spi/spi.hpp>
#include <cstddef>
#include <cstdint>
#ifndef STA_CORE_BUS_SERIAL_DEVICE_HPP
#define STA_CORE_BUS_SERIAL_DEVICE_HPP
#include <sta/bus/interface.hpp>
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

View File

@ -0,0 +1,37 @@
#ifndef STA_CORE_I2C_DEVICE_HPP
#define STA_CORE_I2C_DEVICE_HPP
#include <sta/bus/i2c/i2c.hpp>
#include <sta/bus/device.hpp>
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

View File

@ -0,0 +1,33 @@
#ifndef STA_CORE_I2C_I2C_HPP
#define STA_CORE_I2C_I2C_HPP
#include <sta/bus/interface.hpp>
#include <sta/mutex.hpp>
#include <cstddef>
#include <cstdint>
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

View File

@ -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 <sta/mutex.hpp>
#include <sta/spi/settings.hpp>
#include <cstddef>
#include <cstdint>
#include <cstddef>
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

View File

@ -0,0 +1,51 @@
/**
* @file
* @brief SPI bus peripheral device.
*/
#ifndef STA_CORE_SPI_DEVICE_HPP
#define STA_CORE_SPI_DEVICE_HPP
#include <sta/bus/device.hpp>
#include <sta/bus/spi/spi.hpp>
#include <sta/gpio_pin.hpp>
#include <cstddef>
#include <cstdint>
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

View File

@ -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 <cstdint>
namespace sta
{
/**

View File

@ -0,0 +1,47 @@
/**
* @file
* @brief SPI bus software interface.
*/
#ifndef STA_CORE_SPI_SPI_HPP
#define STA_CORE_SPI_SPI_HPP
#include <sta/bus/interface.hpp>
#include <sta/bus/spi/settings.hpp>
#include <sta/mutex.hpp>
#include <cstddef>
#include <cstdint>
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

View File

@ -0,0 +1,24 @@
#ifndef STA_CORE_UART_SETTINGS_HPP
#define STA_CORE_UART_SETTINGS_HPP
#include <stdint.h>
namespace sta
{
enum class UARTMode
{
RX,
TX,
RX_TX
};
/**
* @brief %UART settings.
*/
struct UARTSettings
{
UARTMode mode;
};
} // namespace sta
#endif // STA_CORE_UART_SETTINGS_HPP

View File

@ -0,0 +1,25 @@
#ifndef STA_CORE_UART_UART_HPP
#define STA_CORE_UART_UART_HPP
#include <sta/bus/interface.hpp>
#include <sta/bus/uart/settings.hpp>
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

View File

@ -2,7 +2,7 @@
#define STA_CONFIG
// Use the raspberry pi for this project.
#include <sta/raspi/mcu/common.hpp>
#include <sta/devices/raspi/mcu/common.hpp>
// #define DEBUG
#define STA_PRINTF_USE_STDLIB

View File

@ -0,0 +1,52 @@
#ifndef STA_RASPI_I2C_HPP
#define STA_RASPI_I2C_HPP
#include <sta/config.hpp>
#ifdef STA_PLATFORM_RASPI
#include <sta/bus/i2c/i2c.hpp>
#include <sta/bus/i2c/device.hpp>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
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

View File

@ -4,8 +4,8 @@
#include <sta/config.hpp>
#ifdef STA_PLATFORM_RASPI
#include <sta/spi/device.hpp>
#include <sta/spi/spi.hpp>
#include <sta/bus/spi/device.hpp>
#include <sta/bus/spi/spi.hpp>
namespace sta
{

View File

View File

@ -4,7 +4,7 @@
// Only enable module on Raspi platform w/ HAL GPIO module enabled
#include <sta/config.hpp>
#ifdef STA_PLATFORM_RASPI
# include <sta/raspi/hal.hpp>
# include <sta/devices/raspi/hal.hpp>
# define STA_RASPI_GPIO_ENABLED
#endif // STA_PLATFORM_RASPI

View File

@ -0,0 +1,43 @@
/**
* @file delay.hpp
* @author <your name> (<you>@<your_domain>.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 <sta/config.hpp>
#if defined(STA_PLATFORM_YOUR_DEVICE) || defined(DOXYGEN)
#include <cstdint>
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

View File

@ -1,27 +0,0 @@
#ifndef STA_I2C_HPP
#define STA_I2C_HPP
#include <cstdint>
#include <sta/mutex.hpp>
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

View File

@ -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.
*

View File

@ -5,8 +5,6 @@
#ifndef STA_CORE_PRINTABLE_HPP
#define STA_CORE_PRINTABLE_HPP
#include <sta/printf.hpp>
#include <cstddef>
#include <cstdint>
@ -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

View File

@ -5,7 +5,8 @@
#ifndef STA_CORE_PRINTABLE_UART_HPP
#define STA_CORE_PRINTABLE_UART_HPP
#include <sta/uart.hpp>
#include <sta/bus/uart/uart.hpp>
#include <sta/printable.hpp>
#include <cstddef>
#include <cstdint>
@ -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_;
};

View File

@ -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.
*

77
src/bus/device.cpp Normal file
View File

@ -0,0 +1,77 @@
#include <sta/bus/device.hpp>
#include <sta/assert.hpp>
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

23
src/bus/i2c/device.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <sta/bus/i2c/device.hpp>
#include <sta/assert.hpp>
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

11
src/bus/i2c/i2c.cpp Normal file
View File

@ -0,0 +1,11 @@
#include <sta/bus/i2c/i2c.hpp>
namespace sta
{
I2c::I2c(Mutex * mutex=nullptr)
: Interface{mutex}
{
}
} // namespace sta

29
src/bus/interface.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <sta/bus/interface.hpp>
#include <sta/assert.hpp>
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

29
src/bus/spi/device.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <sta/bus/spi/device.hpp>
#include <sta/assert.hpp>
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

View File

@ -1,4 +1,4 @@
#include <sta/spi/settings.hpp>
#include <sta/bus/spi/settings.hpp>
#include <sta/assert.hpp>
#include <sta/lang.hpp>

17
src/bus/spi/spi.cpp Normal file
View File

@ -0,0 +1,17 @@
#include <sta/bus/spi/spi.hpp>
#include <cstdio>
namespace sta
{
SPI::SPI(const SPISettings & settings, Mutex * mutex /* = nullptr */)
: Interface{mutex}, settings_{settings}
{
}
const SPISettings & SPI::settings()
{
return settings_;
}
} // namespace sta

View File

15
src/bus/uart/uart.cpp Normal file
View File

@ -0,0 +1,15 @@
#include <sta/bus/uart/uart.hpp>
namespace sta
{
UART::UART(const UARTSettings & settings, Mutex * mutex)
: Interface{mutex}, settings_{settings}
{
}
const UARTSettings & UART::settings()
{
return settings_;
}
} // namespace sta

View File

@ -0,0 +1,129 @@
#include <sta/devices/raspi/bus/i2c.hpp>
#ifdef STA_PLATFORM_RASPI
#include <sta/assert.hpp>
#include <cstdlib>
#include <string.h>
#include <iostream>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
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

View File

@ -1,9 +1,9 @@
#include <sta/raspi/spi.hpp>
#include <sta/devices/raspi/bus/spi.hpp>
#ifdef STA_PLATFORM_RASPI
#include <sta/raspi/gpio_pin.hpp>
#include <sta/spi/settings.hpp>
#include <sta/devices/raspi/gpio_pin.hpp>
#include <sta/bus/spi/settings.hpp>
#include <sta/assert.hpp>
#include <sta/endian.hpp>
#include <sta/lang.hpp>
@ -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_);
}

View File

@ -1,7 +1,7 @@
#include <sta/raspi/delay.hpp>
#include <sta/devices/raspi/delay.hpp>
#ifdef STA_PLATFORM_RASPI
#include <sta/raspi/hal.hpp>
#include <sta/devices/raspi/hal.hpp>
#include <sta/assert.hpp>
#include <sta/lang.hpp>

View File

@ -1,4 +1,4 @@
#include <sta/stm32/uart.hpp>
#include <sta/devices/stm32/uart.hpp>
#ifdef STA_STM32_UART_ENABLED
#include <sta/assert.hpp>

View File

@ -0,0 +1,36 @@
/**
* @file delay.cpp
* @author <your name> (<you>@<your_domain>.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 <sta/lang.hpp> import if no longer needed.
*/
#define STA_PLATFORM_YOUR_DEVICE
#include <sta/devices/template/delay.hpp>
#ifdef STA_PLATFORM_YOUR_DEVICE
#include <sta/lang.hpp>
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

View File

@ -1,22 +0,0 @@
#include <sta/i2c.hpp>
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();
}
}
}

View File

@ -2,136 +2,184 @@
#include <cinttypes>
#include <cstring>
#include <cstdio>
#include <sta/assert.hpp>
#include <sta/lang.hpp>
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("<invalid_base>");
}
}
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<uint32_t>(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("<bin_value_too_big>");
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("<hex_value_too_big>");
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

View File

@ -7,194 +7,19 @@
#include <cstring>
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<const uint8_t *>(str), length);
intf_->transfer(reinterpret_cast<const uint8_t *>(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("<invalid_base>");
}
}
void PrintableUART::printDec(uintmax_t num, const char * fmt)
{
char buffer[64];
snprintf(buffer, sizeof(buffer), fmt, static_cast<uint32_t>(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("<bin_value_too_big>");
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("<hex_value_too_big>");
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

View File

@ -1,88 +0,0 @@
#include <sta/spi/device.hpp>
#include <sta/assert.hpp>
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

View File

@ -1,27 +0,0 @@
#include <sta/spi/spi.hpp>
#include <cstdio>
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

View File

@ -6,7 +6,6 @@
#include <cstring>
namespace sta
{
void UART::write(uint8_t value)