mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/sta-core.git
synced 2025-06-12 01:25:59 +00:00
Added SPI implementation based on Linux library
This commit is contained in:
parent
d2806f1651
commit
9894fff86e
@ -31,10 +31,18 @@ namespace sta
|
|||||||
|
|
||||||
void setState(GpioPinState state) override;
|
void setState(GpioPinState state) override;
|
||||||
|
|
||||||
|
static RaspiGpioPin DUMMY_GPIO;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint8_t pin_;
|
uint8_t pin_;
|
||||||
GpioMode mode_;
|
GpioMode mode_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class DummyGpioPin : RaspiGpioPin {
|
||||||
|
DummyGpioPin();
|
||||||
|
|
||||||
|
void setState(GpioPinState state) override;
|
||||||
|
}
|
||||||
} // namespace sta
|
} // namespace sta
|
||||||
|
|
||||||
#endif // STA_RASPI_GPIO_ENABLED
|
#endif // STA_RASPI_GPIO_ENABLED
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
#define STA_CORE_RASPI_HAL_HPP
|
#define STA_CORE_RASPI_HAL_HPP
|
||||||
|
|
||||||
#include <wiringPi.h>
|
#include <wiringPi.h>
|
||||||
#include <wiringPiSPI.h>
|
|
||||||
#include <wiringPiI2C.h>
|
#include <wiringPiI2C.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
|
||||||
#endif //STA_CORE_RASPI_HAL_HPP
|
#endif //STA_CORE_RASPI_HAL_HPP
|
||||||
|
@ -1,19 +1,50 @@
|
|||||||
#ifndef STA_CORE_RASPI_SPI_HPP
|
#ifndef STA_CORE_RASPI_SPI_HPP
|
||||||
#define STA_CORE_RASPI_SPI_HPP
|
#define STA_CORE_RASPI_SPI_HPP
|
||||||
|
|
||||||
|
#include <sta/config.hpp>
|
||||||
|
#ifdef STA_PLATFORM_RASPI
|
||||||
|
|
||||||
#include <sta/spi/device.hpp>
|
#include <sta/spi/device.hpp>
|
||||||
|
#include <sta/spi/spi.hpp>
|
||||||
|
|
||||||
namespace sta
|
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
|
class RaspiSPIDevice : SPIDevice
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
RaspiSPIDevice(RaspiSPI * intf);
|
||||||
|
|
||||||
|
void select() override;
|
||||||
|
|
||||||
|
void deselect() override;
|
||||||
};
|
};
|
||||||
} // namespace sta
|
} // namespace sta
|
||||||
|
|
||||||
|
#endif // STA_PLATFORM_RASPI
|
||||||
|
|
||||||
#endif // STA_CORE_RASPI_HPP
|
#endif // STA_CORE_RASPI_HPP
|
@ -15,6 +15,22 @@ namespace sta
|
|||||||
{
|
{
|
||||||
digitalWrite(pin_, state == GpioPinState::GPIO_LOW ? LOW : HIGH);
|
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
|
} // namespace sta
|
||||||
|
|
||||||
|
|
||||||
|
188
src/raspi/spi.cpp
Normal file
188
src/raspi/spi.cpp
Normal file
@ -0,0 +1,188 @@
|
|||||||
|
#include <sta/raspi/spi.hpp>
|
||||||
|
#ifdef STA_PLATFORM_RASPI
|
||||||
|
|
||||||
|
|
||||||
|
#include <sta/raspi/gpio_pin.hpp>
|
||||||
|
#include <sta/assert.hpp>
|
||||||
|
#include <sta/endian.hpp>
|
||||||
|
#include <sta/lang.hpp>
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
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
|
Loading…
x
Reference in New Issue
Block a user