mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/sta-core.git
synced 2025-06-13 01:55:59 +00:00
Implemented SPI for Arduino and a few fixes
This commit is contained in:
parent
53335281d7
commit
ec83b7026e
@ -31,8 +31,8 @@ namespace sta
|
|||||||
*/
|
*/
|
||||||
enum class SPIClkPolarity
|
enum class SPIClkPolarity
|
||||||
{
|
{
|
||||||
LOW, /**< Low idle clock */
|
SPI_LOW, /**< Low idle clock */
|
||||||
HIGH /**< High idle clock */
|
SPI_HIGH /**< High idle clock */
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
namespace sta
|
namespace sta
|
||||||
{
|
{
|
||||||
extern BasePrintable * Debug;
|
extern Printable * Debug;
|
||||||
} // namespace sta
|
} // namespace sta
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
namespace sta
|
namespace sta
|
||||||
{
|
{
|
||||||
class PrintableSerial: public sta::BasePrintable
|
class PrintableSerial: public Printable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
|
@ -25,7 +25,7 @@ namespace sta
|
|||||||
*
|
*
|
||||||
* @ingroup sta_core
|
* @ingroup sta_core
|
||||||
*/
|
*/
|
||||||
class PrintableUART : public BasePrintable
|
class PrintableUART : public Printable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
#ifndef STA_CORE_ARDUINO_HAL_HPP
|
#ifndef STA_CORE_ARDUINO_HAL_HPP
|
||||||
#define STA_CORE_ARDUINO_HAL_HPP
|
#define STA_CORE_ARDUINO_HAL_HPP
|
||||||
|
|
||||||
#include<Arduino.h>
|
#include <Arduino.h>
|
||||||
#include<Wire.h>
|
#include <Wire.h>
|
||||||
#include<SPI.h>
|
#include <SPI.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @defgroup sta_core_arduino Arduino
|
* @defgroup sta_core_arduino Arduino
|
||||||
@ -11,4 +11,10 @@
|
|||||||
* @brief Modules implemented for the Arduino platform.
|
* @brief Modules implemented for the Arduino platform.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
namespace hal
|
||||||
|
{
|
||||||
|
using SPISettings = SPISettings;
|
||||||
|
} // namespace hal
|
||||||
|
|
||||||
|
|
||||||
#endif // STA_CORE_ARDUINO_HAL_HPP
|
#endif // STA_CORE_ARDUINO_HAL_HPP
|
@ -12,11 +12,11 @@ namespace sta
|
|||||||
{
|
{
|
||||||
case SPIMode::MODE_0:
|
case SPIMode::MODE_0:
|
||||||
case SPIMode::MODE_1:
|
case SPIMode::MODE_1:
|
||||||
return SPIClkPolarity::LOW;
|
return SPIClkPolarity::SPI_LOW;
|
||||||
|
|
||||||
case SPIMode::MODE_2:
|
case SPIMode::MODE_2:
|
||||||
case SPIMode::MODE_3:
|
case SPIMode::MODE_3:
|
||||||
return SPIClkPolarity::HIGH;
|
return SPIClkPolarity::SPI_HIGH;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// Unreachable case
|
// Unreachable case
|
||||||
@ -46,7 +46,7 @@ namespace sta
|
|||||||
|
|
||||||
SPIMode getSPIMode(SPIClkPolarity polarity, SPIClkPhase phase)
|
SPIMode getSPIMode(SPIClkPolarity polarity, SPIClkPhase phase)
|
||||||
{
|
{
|
||||||
if (polarity == SPIClkPolarity::LOW)
|
if (polarity == SPIClkPolarity::SPI_LOW)
|
||||||
{
|
{
|
||||||
if (phase == SPIClkPhase::EDGE_1)
|
if (phase == SPIClkPhase::EDGE_1)
|
||||||
{
|
{
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
namespace sta
|
namespace sta
|
||||||
{
|
{
|
||||||
PrintableSerial::PrintableSerial(unsigned long baud)
|
PrintableSerial::PrintableSerial(unsigned long baud)
|
||||||
: BasePrintable()
|
: Printable{}
|
||||||
{
|
{
|
||||||
Serial.begin(baud);
|
Serial.begin(baud);
|
||||||
}
|
}
|
||||||
|
@ -15,28 +15,28 @@ namespace sta
|
|||||||
|
|
||||||
void ArduinoI2C::transfer(uint8_t value)
|
void ArduinoI2C::transfer(uint8_t value)
|
||||||
{
|
{
|
||||||
Wire.beginTransmission(address_);
|
Wire.beginTransmission((uint8_t)address_);
|
||||||
Wire.write(value);
|
Wire.write(value);
|
||||||
Wire.endTransmission(true);
|
Wire.endTransmission(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArduinoI2C::transfer16(uint16_t value)
|
void ArduinoI2C::transfer16(uint16_t value)
|
||||||
{
|
{
|
||||||
Wire.beginTransmission(address_);
|
Wire.beginTransmission((uint8_t)address_);
|
||||||
Wire.write(value);
|
Wire.write(value);
|
||||||
Wire.endTransmission(true);
|
Wire.endTransmission(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArduinoI2C::transfer(const uint8_t * buffer, size_t size)
|
void ArduinoI2C::transfer(const uint8_t * buffer, size_t size)
|
||||||
{
|
{
|
||||||
Wire.beginTransmission(address_);
|
Wire.beginTransmission((uint8_t)address_);
|
||||||
Wire.write(buffer, size);
|
Wire.write(buffer, size);
|
||||||
Wire.endTransmission();
|
Wire.endTransmission();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArduinoI2C::receive(uint8_t * buffer, size_t size)
|
void ArduinoI2C::receive(uint8_t * buffer, size_t size)
|
||||||
{
|
{
|
||||||
Wire.requestFrom(address_, size);
|
Wire.requestFrom((uint8_t)address_, (uint8_t)size);
|
||||||
size_t index = 0;
|
size_t index = 0;
|
||||||
|
|
||||||
while (index < size)
|
while (index < size)
|
||||||
@ -54,7 +54,7 @@ namespace sta
|
|||||||
uint8_t *buffer = new uint8_t[count];
|
uint8_t *buffer = new uint8_t[count];
|
||||||
memset(buffer, value, count);
|
memset(buffer, value, count);
|
||||||
|
|
||||||
Wire.beginTransmission(address_);
|
Wire.beginTransmission((uint8_t)address_);
|
||||||
Wire.write(buffer, count);
|
Wire.write(buffer, count);
|
||||||
Wire.endTransmission();
|
Wire.endTransmission();
|
||||||
|
|
||||||
|
@ -1,51 +1,102 @@
|
|||||||
#include <sta/devices/arduino/bus/spi.hpp>
|
#include <sta/devices/arduino/bus/spi.hpp>
|
||||||
|
|
||||||
|
#include <sta/lang.hpp>
|
||||||
|
|
||||||
|
#include <sta/devices/arduino/hal.hpp>
|
||||||
|
|
||||||
namespace sta
|
namespace sta
|
||||||
{
|
{
|
||||||
ArduinoSPI::ArduinoSPI(const SPISettings & settings, Mutex * mutex /* = nullptr */)
|
ArduinoSPI::ArduinoSPI(const SPISettings & settings, Mutex * mutex /* = nullptr */)
|
||||||
: SPI{ settings, mutex }
|
: SPI{ settings, mutex }
|
||||||
{
|
{
|
||||||
|
SPIClass::begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArduinoSPI::transfer(uint8_t value)
|
void ArduinoSPI::transfer(uint8_t value)
|
||||||
{
|
{
|
||||||
|
SPIClass::transfer(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArduinoSPI::transfer16(uint16_t value)
|
void ArduinoSPI::transfer16(uint16_t value)
|
||||||
{
|
{
|
||||||
|
SPIClass::transfer16(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArduinoSPI::transfer(const uint8_t * buffer, size_t size)
|
void ArduinoSPI::transfer(const uint8_t * buffer, size_t size)
|
||||||
{
|
{
|
||||||
|
// We have to create a temporary buffer here because the buffer argument in SPIClass::transfer is not const.
|
||||||
|
uint8_t temp_buffer[size];
|
||||||
|
memcpy(temp_buffer, buffer, size);
|
||||||
|
|
||||||
|
SPIClass::transfer(temp_buffer, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArduinoSPI::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size)
|
void ArduinoSPI::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size)
|
||||||
{
|
{
|
||||||
|
// Copy txBuffer content into rxBuffer, SPIClass::transfer will store received values in rxBuffer.
|
||||||
|
memcpy(rxBuffer, txBuffer, size);
|
||||||
|
|
||||||
|
SPIClass::transfer(rxBuffer, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArduinoSPI::receive(uint8_t * buffer, size_t size)
|
void ArduinoSPI::receive(uint8_t * buffer, size_t size)
|
||||||
{
|
{
|
||||||
|
SPIClass::transfer(buffer, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArduinoSPI::fill(uint8_t value, size_t count)
|
void ArduinoSPI::fill(uint8_t value, size_t count)
|
||||||
{
|
{
|
||||||
|
uint8_t temp_buffer[count];
|
||||||
|
memset(temp_buffer, value, count);
|
||||||
|
|
||||||
|
SPIClass::transfer(temp_buffer, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArduinoSPI::acquire()
|
void ArduinoSPI::acquire()
|
||||||
{
|
{
|
||||||
|
const SPISettings & set = settings();
|
||||||
|
|
||||||
|
uint8_t dataMode;
|
||||||
|
switch (set.mode)
|
||||||
|
{
|
||||||
|
case SPIMode::MODE_0:
|
||||||
|
dataMode = SPI_MODE0;
|
||||||
|
break;
|
||||||
|
case SPIMode::MODE_1:
|
||||||
|
dataMode = SPI_MODE1;
|
||||||
|
break;
|
||||||
|
case SPIMode::MODE_2:
|
||||||
|
dataMode = SPI_MODE2;
|
||||||
|
break;
|
||||||
|
case SPIMode::MODE_3:
|
||||||
|
dataMode = SPI_MODE3;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
STA_UNREACHABLE();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t bitOrder;
|
||||||
|
switch (set.bitOrder)
|
||||||
|
{
|
||||||
|
case SPIBitOrder::MSB:
|
||||||
|
bitOrder = MSBFIRST;
|
||||||
|
break;
|
||||||
|
case SPIBitOrder::LSB:
|
||||||
|
bitOrder = LSBFIRST;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
STA_UNREACHABLE();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
hal::SPISettings halSettings(set.clkSpeed, bitOrder, dataMode);
|
||||||
|
|
||||||
|
SPIClass::beginTransaction(halSettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArduinoSPI::release()
|
void ArduinoSPI::release()
|
||||||
{
|
{
|
||||||
|
SPIClass::endTransaction();
|
||||||
}
|
}
|
||||||
} // namespace sta
|
} // namespace sta
|
||||||
|
@ -20,7 +20,7 @@ namespace sta
|
|||||||
SPISettings settings;
|
SPISettings settings;
|
||||||
|
|
||||||
settings.mode = getSPIMode(
|
settings.mode = getSPIMode(
|
||||||
(handle->Init.CLKPolarity == SPI_POLARITY_LOW) ? SPIClkPolarity::LOW : SPIClkPolarity::HIGH,
|
(handle->Init.CLKPolarity == SPI_POLARITY_LOW) ? SPIClkPolarity::SPI_LOW : SPIClkPolarity::SPI_HIGH,
|
||||||
(handle->Init.CLKPhase == SPI_PHASE_1EDGE) ? SPIClkPhase::EDGE_1 : SPIClkPhase::EDGE_2
|
(handle->Init.CLKPhase == SPI_PHASE_1EDGE) ? SPIClkPhase::EDGE_1 : SPIClkPhase::EDGE_2
|
||||||
);
|
);
|
||||||
settings.dataSize = (handle->Init.DataSize == SPI_DATASIZE_8BIT) ? SPIDataSize::SIZE_8 : SPIDataSize::SIZE_16;
|
settings.dataSize = (handle->Init.DataSize == SPI_DATASIZE_8BIT) ? SPIDataSize::SIZE_8 : SPIDataSize::SIZE_16;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user