mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/sta-core.git
synced 2025-09-29 05:17:33 +00:00
Added I2C support for raspi & first rework of debugging
This commit is contained in:
77
src/bus/device.cpp
Normal file
77
src/bus/device.cpp
Normal 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
23
src/bus/i2c/device.cpp
Normal 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
11
src/bus/i2c/i2c.cpp
Normal 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
29
src/bus/interface.cpp
Normal 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
29
src/bus/spi/device.cpp
Normal 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
|
72
src/bus/spi/settings.cpp
Normal file
72
src/bus/spi/settings.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <sta/bus/spi/settings.hpp>
|
||||
|
||||
#include <sta/assert.hpp>
|
||||
#include <sta/lang.hpp>
|
||||
|
||||
|
||||
namespace sta
|
||||
{
|
||||
SPIClkPolarity getSPIClkPolarity(SPIMode mode)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
case SPIMode::MODE_0:
|
||||
case SPIMode::MODE_1:
|
||||
return SPIClkPolarity::LOW;
|
||||
|
||||
case SPIMode::MODE_2:
|
||||
case SPIMode::MODE_3:
|
||||
return SPIClkPolarity::HIGH;
|
||||
|
||||
default:
|
||||
// Unreachable case
|
||||
STA_ASSERT_MSG(false, "Case for SPIMode enum not handled");
|
||||
STA_UNREACHABLE();
|
||||
}
|
||||
}
|
||||
|
||||
SPIClkPhase getSPIClkPhase(SPIMode mode)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
case SPIMode::MODE_0:
|
||||
case SPIMode::MODE_2:
|
||||
return SPIClkPhase::EDGE_1;
|
||||
|
||||
case SPIMode::MODE_1:
|
||||
case SPIMode::MODE_3:
|
||||
return SPIClkPhase::EDGE_2;
|
||||
|
||||
default:
|
||||
// Unreachable case
|
||||
STA_ASSERT_MSG(false, "Case for SPIMode enum not handled");
|
||||
STA_UNREACHABLE();
|
||||
}
|
||||
}
|
||||
|
||||
SPIMode getSPIMode(SPIClkPolarity polarity, SPIClkPhase phase)
|
||||
{
|
||||
if (polarity == SPIClkPolarity::LOW)
|
||||
{
|
||||
if (phase == SPIClkPhase::EDGE_1)
|
||||
{
|
||||
return SPIMode::MODE_0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return SPIMode::MODE_1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (phase == SPIClkPhase::EDGE_1)
|
||||
{
|
||||
return SPIMode::MODE_2;
|
||||
}
|
||||
else
|
||||
{
|
||||
return SPIMode::MODE_3;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace sta
|
17
src/bus/spi/spi.cpp
Normal file
17
src/bus/spi/spi.cpp
Normal 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
|
0
src/bus/uart/settings.cpp
Normal file
0
src/bus/uart/settings.cpp
Normal file
15
src/bus/uart/uart.cpp
Normal file
15
src/bus/uart/uart.cpp
Normal 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
|
Reference in New Issue
Block a user