2022-04-30 18:40:22 +02:00
|
|
|
#include "SpiCookie.h"
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-05-11 11:11:39 +02:00
|
|
|
SpiCookie::SpiCookie(address_t spiAddress, gpioId_t chipSelect, const size_t maxSize,
|
2022-02-02 10:29:30 +01:00
|
|
|
spi::SpiModes spiMode, uint32_t spiSpeed)
|
2022-05-11 11:11:39 +02:00
|
|
|
: SpiCookie(spi::SpiComIfModes::REGULAR, spiAddress, chipSelect, maxSize, spiMode, spiSpeed,
|
|
|
|
nullptr, nullptr) {}
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-05-11 11:11:39 +02:00
|
|
|
SpiCookie::SpiCookie(address_t spiAddress, const size_t maxSize, spi::SpiModes spiMode,
|
|
|
|
uint32_t spiSpeed)
|
|
|
|
: SpiCookie(spiAddress, gpio::NO_GPIO, maxSize, spiMode, spiSpeed) {}
|
|
|
|
|
|
|
|
SpiCookie::SpiCookie(address_t spiAddress, gpioId_t chipSelect, const size_t maxSize,
|
|
|
|
spi::SpiModes spiMode, uint32_t spiSpeed,
|
2022-02-02 10:29:30 +01:00
|
|
|
spi::send_callback_function_t callback, void* args)
|
2022-05-11 11:11:39 +02:00
|
|
|
: SpiCookie(spi::SpiComIfModes::CALLBACK, spiAddress, chipSelect, maxSize, spiMode, spiSpeed,
|
|
|
|
callback, args) {}
|
2021-07-13 19:19:25 +02:00
|
|
|
|
|
|
|
SpiCookie::SpiCookie(spi::SpiComIfModes comIfMode, address_t spiAddress, gpioId_t chipSelect,
|
2022-05-11 11:11:39 +02:00
|
|
|
const size_t maxSize, spi::SpiModes spiMode, uint32_t spiSpeed,
|
|
|
|
spi::send_callback_function_t callback, void* args)
|
2022-02-02 10:29:30 +01:00
|
|
|
: spiAddress(spiAddress),
|
|
|
|
chipSelectPin(chipSelect),
|
|
|
|
comIfMode(comIfMode),
|
|
|
|
maxSize(maxSize),
|
|
|
|
spiMode(spiMode),
|
|
|
|
spiSpeed(spiSpeed),
|
|
|
|
sendCallback(callback),
|
|
|
|
callbackArgs(args) {}
|
|
|
|
|
|
|
|
spi::SpiComIfModes SpiCookie::getComIfMode() const { return this->comIfMode; }
|
2021-07-13 19:19:25 +02:00
|
|
|
|
|
|
|
void SpiCookie::getSpiParameters(spi::SpiModes& spiMode, uint32_t& spiSpeed,
|
2022-02-02 10:29:30 +01:00
|
|
|
UncommonParameters* parameters) const {
|
|
|
|
spiMode = this->spiMode;
|
|
|
|
spiSpeed = this->spiSpeed;
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
if (parameters != nullptr) {
|
|
|
|
parameters->threeWireSpi = uncommonParameters.threeWireSpi;
|
|
|
|
parameters->lsbFirst = uncommonParameters.lsbFirst;
|
|
|
|
parameters->noCs = uncommonParameters.noCs;
|
|
|
|
parameters->bitsPerWord = uncommonParameters.bitsPerWord;
|
|
|
|
parameters->csHigh = uncommonParameters.csHigh;
|
|
|
|
}
|
2021-07-13 19:19:25 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
gpioId_t SpiCookie::getChipSelectPin() const { return chipSelectPin; }
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
size_t SpiCookie::getMaxBufferSize() const { return maxSize; }
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
address_t SpiCookie::getSpiAddress() const { return spiAddress; }
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
void SpiCookie::setThreeWireSpi(bool enable) { uncommonParameters.threeWireSpi = enable; }
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
void SpiCookie::setLsbFirst(bool enable) { uncommonParameters.lsbFirst = enable; }
|
|
|
|
|
|
|
|
void SpiCookie::setNoCs(bool enable) { uncommonParameters.noCs = enable; }
|
2021-07-13 19:19:25 +02:00
|
|
|
|
|
|
|
void SpiCookie::setBitsPerWord(uint8_t bitsPerWord) {
|
2022-02-02 10:29:30 +01:00
|
|
|
uncommonParameters.bitsPerWord = bitsPerWord;
|
2021-07-13 19:19:25 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
void SpiCookie::setCsHigh(bool enable) { uncommonParameters.csHigh = enable; }
|
2021-07-13 19:19:25 +02:00
|
|
|
|
|
|
|
void SpiCookie::activateCsDeselect(bool deselectCs, uint16_t delayUsecs) {
|
2022-02-02 10:29:30 +01:00
|
|
|
spiTransferStruct.cs_change = deselectCs;
|
|
|
|
spiTransferStruct.delay_usecs = delayUsecs;
|
2021-07-13 19:19:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpiCookie::assignReadBuffer(uint8_t* rx) {
|
2022-02-02 10:29:30 +01:00
|
|
|
if (rx != nullptr) {
|
|
|
|
spiTransferStruct.rx_buf = reinterpret_cast<__u64>(rx);
|
|
|
|
}
|
2021-07-13 19:19:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpiCookie::assignWriteBuffer(const uint8_t* tx) {
|
2022-02-02 10:29:30 +01:00
|
|
|
if (tx != nullptr) {
|
|
|
|
spiTransferStruct.tx_buf = reinterpret_cast<__u64>(tx);
|
|
|
|
}
|
2021-07-13 19:19:25 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
void SpiCookie::setCallbackMode(spi::send_callback_function_t callback, void* args) {
|
|
|
|
this->comIfMode = spi::SpiComIfModes::CALLBACK;
|
|
|
|
this->sendCallback = callback;
|
|
|
|
this->callbackArgs = args;
|
2021-07-13 19:19:25 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
void SpiCookie::setCallbackArgs(void* args) { this->callbackArgs = args; }
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
spi_ioc_transfer* SpiCookie::getTransferStructHandle() { return &spiTransferStruct; }
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
void SpiCookie::setFullOrHalfDuplex(bool halfDuplex) { this->halfDuplex = halfDuplex; }
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
bool SpiCookie::isFullDuplex() const { return not this->halfDuplex; }
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
void SpiCookie::setTransferSize(size_t transferSize) { spiTransferStruct.len = transferSize; }
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
size_t SpiCookie::getCurrentTransferSize() const { return spiTransferStruct.len; }
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
void SpiCookie::setSpiSpeed(uint32_t newSpeed) { this->spiSpeed = newSpeed; }
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
void SpiCookie::setSpiMode(spi::SpiModes newMode) { this->spiMode = newMode; }
|
2021-07-13 19:19:25 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
void SpiCookie::getCallback(spi::send_callback_function_t* callback, void** args) {
|
|
|
|
*callback = this->sendCallback;
|
|
|
|
*args = this->callbackArgs;
|
2021-07-13 19:19:25 +02:00
|
|
|
}
|
2022-05-14 11:33:43 +02:00
|
|
|
|
|
|
|
void SpiCookie::setCsLockManual(bool enable) { manualCsLock = enable; }
|
|
|
|
|
|
|
|
bool SpiCookie::getCsLockManual() const { return manualCsLock; }
|
|
|
|
|
|
|
|
void SpiCookie::getMutexParams(MutexIF::TimeoutType& csTimeoutType, dur_millis_t& csTimeout) const {
|
|
|
|
csTimeoutType = this->csTimeoutType;
|
|
|
|
csTimeout = this->csTimeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpiCookie::setMutexParams(MutexIF::TimeoutType csTimeoutType, dur_millis_t csTimeout) {
|
|
|
|
this->csTimeoutType = csTimeoutType;
|
|
|
|
this->csTimeout = csTimeout;
|
|
|
|
}
|