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

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)