From a784285600d7704fe1a50c02b79baa067365c9af Mon Sep 17 00:00:00 2001 From: Alberto Nidasio <alberto.nidasio@skywarder.eu> Date: Wed, 15 Mar 2023 09:31:13 +0100 Subject: [PATCH] [SPI] Changed uint16_t functions name and removed SPISignalGenerator --- CMakeLists.txt | 15 - cmake/boardcore.cmake | 1 - src/shared/drivers/spi/SPI.h | 20 +- src/shared/drivers/spi/SPIBus.h | 27 +- src/shared/drivers/spi/SPIBusInterface.h | 10 +- src/shared/drivers/spi/SPIDriver.h | 1 - src/shared/drivers/spi/SPISignalGenerator.h | 273 ------------------ src/shared/drivers/spi/SPISlaveBus.h | 271 ----------------- src/shared/drivers/spi/SPITransaction.cpp | 20 +- src/shared/drivers/spi/SPITransaction.h | 10 +- src/shared/sensors/MAX31855/MAX31855.cpp | 4 +- src/tests/drivers/spi/test-spi-as-slave.cpp | 64 ---- .../drivers/spi/test-spi-signal-generator.cpp | 80 ----- src/tests/drivers/spi/test-spi-slave-bus.cpp | 109 ------- src/tests/drivers/spi/test-spi.cpp | 10 +- 15 files changed, 52 insertions(+), 863 deletions(-) delete mode 100644 src/shared/drivers/spi/SPISignalGenerator.h delete mode 100644 src/shared/drivers/spi/SPISlaveBus.h delete mode 100644 src/tests/drivers/spi/test-spi-as-slave.cpp delete mode 100644 src/tests/drivers/spi/test-spi-signal-generator.cpp delete mode 100644 src/tests/drivers/spi/test-spi-slave-bus.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a511e5de..c5d677191 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -214,15 +214,6 @@ sbs_target(test-pwm stm32f429zi_stm32f4discovery) add_executable(test-spi src/tests/drivers/spi/test-spi.cpp) sbs_target(test-spi stm32f429zi_stm32f4discovery) -add_executable(test-spi-as-slave src/tests/drivers/spi/test-spi-as-slave.cpp) -sbs_target(test-spi-as-slave stm32f429zi_stm32f4discovery) - -add_executable(test-spi-signal-generator src/tests/drivers/spi/test-spi-signal-generator.cpp) -sbs_target(test-spi-signal-generator stm32f429zi_stm32f4discovery) - -add_executable(test-spi-slave-bus src/tests/drivers/spi/test-spi-slave-bus.cpp) -sbs_target(test-spi-slave-bus stm32f429zi_stm32f4discovery) - add_executable(test-timer-utils src/tests/drivers/timer/test-timer-utils.cpp) sbs_target(test-timer-utils stm32f429zi_stm32f4discovery) @@ -322,12 +313,6 @@ sbs_target(test-ads1118 stm32f407vg_stm32f4discovery) add_executable(test-ads131m04 src/tests/sensors/ADS131M04/test-ads131m04.cpp) sbs_target(test-ads131m04 stm32f429zi_stm32f4discovery) -add_executable(test-ads131m04highfreq src/tests/sensors/ADS131M04/test-ads131m04highfreq.cpp) -sbs_target(test-ads131m04highfreq stm32f429zi_stm32f4discovery) - -add_executable(test-ads131m04highfreq-with-logger src/tests/sensors/ADS131M04/test-ads131m04highfreq-with-logger.cpp) -sbs_target(test-ads131m04highfreq-with-logger stm32f429zi_stm32f4discovery) - add_executable(test-analog-pressure-sensors src/tests/sensors/analog/test-analog-pressure-sensors.cpp) sbs_target(test-analog-pressure-sensors stm32f429zi_stm32f4discovery) diff --git a/cmake/boardcore.cmake b/cmake/boardcore.cmake index 5a91d68a0..caea138a3 100644 --- a/cmake/boardcore.cmake +++ b/cmake/boardcore.cmake @@ -81,7 +81,6 @@ foreach(OPT_BOARD ${BOARDS}) # Sensors ${SBS_BASE}/src/shared/sensors/ADS1118/ADS1118.cpp ${SBS_BASE}/src/shared/sensors/ADS131M04/ADS131M04.cpp - ${SBS_BASE}/src/shared/sensors/ADS131M04/ADS131M04HighFreq.cpp ${SBS_BASE}/src/shared/sensors/BME280/BME280.cpp ${SBS_BASE}/src/shared/sensors/BMP280/BMP280.cpp ${SBS_BASE}/src/shared/sensors/BMX160/BMX160.cpp diff --git a/src/shared/drivers/spi/SPI.h b/src/shared/drivers/spi/SPI.h index 2097264ec..e146c323c 100644 --- a/src/shared/drivers/spi/SPI.h +++ b/src/shared/drivers/spi/SPI.h @@ -183,7 +183,7 @@ public: * @param data Buffer to be filled with received data. * @param size Size of the buffer in bytes. */ - void read(uint16_t *data, size_t nBytes); + void read16(uint16_t *data, size_t nBytes); /** * @brief Writes a single byte to the bus. @@ -197,7 +197,7 @@ public: * * @param data Half word to write. */ - void write(uint16_t data); + void write16(uint16_t data); /** * @brief Writes multiple bytes to the bus. @@ -213,7 +213,7 @@ public: * @param data Buffer containing data to write. * @param size Size of the buffer in bytes. */ - void write(const uint16_t *data, size_t nBytes); + void write16(const uint16_t *data, size_t nBytes); /** * @brief Full duplex transmission of one byte on the bus. @@ -229,7 +229,7 @@ public: * @param data Half word to write. * @return Half word read from the bus. */ - uint16_t transfer(uint16_t data); + uint16_t transfer16(uint16_t data); /** * @brief Full duplex transmission of multiple bytes on the bus. @@ -245,7 +245,7 @@ public: * @param data Buffer containing data to trasfer. * @param size Size of the buffer in bytes. */ - void transfer(uint16_t *data, size_t nBytes); + void transfer16(uint16_t *data, size_t nBytes); private: SPIType *spi; @@ -344,7 +344,7 @@ inline void SPI::read(uint8_t *data, size_t nBytes) data[i] = read(); } -inline void SPI::read(uint16_t *data, size_t nBytes) +inline void SPI::read16(uint16_t *data, size_t nBytes) { // Set 16 bit frame format set16BitFrameFormat(); @@ -372,7 +372,7 @@ inline void SPI::read(uint16_t *data, size_t nBytes) inline void SPI::write(uint8_t data) { transfer(data); } -inline void SPI::write(uint16_t data) { transfer(data); } +inline void SPI::write16(uint16_t data) { transfer(data); } inline void SPI::write(const uint8_t *data, size_t nBytes) { @@ -380,7 +380,7 @@ inline void SPI::write(const uint8_t *data, size_t nBytes) transfer(data[i]); } -inline void SPI::write(const uint16_t *data, size_t nBytes) +inline void SPI::write16(const uint16_t *data, size_t nBytes) { // Set 16 bit frame format set16BitFrameFormat(); @@ -423,7 +423,7 @@ inline uint8_t SPI::transfer(uint8_t data) return static_cast<uint8_t>(spi->DR); } -inline uint16_t SPI::transfer(uint16_t data) +inline uint16_t SPI::transfer16(uint16_t data) { // Set 16 bit frame format set16BitFrameFormat(); @@ -452,7 +452,7 @@ inline void SPI::transfer(uint8_t *data, size_t nBytes) data[i] = transfer(data[i]); } -inline void SPI::transfer(uint16_t *data, size_t nBytes) +inline void SPI::transfer16(uint16_t *data, size_t nBytes) { // Set 16 bit frame format set16BitFrameFormat(); diff --git a/src/shared/drivers/spi/SPIBus.h b/src/shared/drivers/spi/SPIBus.h index e5865b051..a10af7e29 100644 --- a/src/shared/drivers/spi/SPIBus.h +++ b/src/shared/drivers/spi/SPIBus.h @@ -101,7 +101,7 @@ public: * @param data Buffer to be filled with received data. * @param size Size of the buffer. */ - void read(uint16_t* data, size_t size) override; + void read16(uint16_t* data, size_t size) override; /** * @brief Writes a single byte to the bus. @@ -115,7 +115,7 @@ public: * * @param data Half word to write. */ - void write(uint16_t data) override; + void write16(uint16_t data) override; /** * @brief Writes multiple bytes to the bus. @@ -131,7 +131,7 @@ public: * @param data Buffer containing data to write. * @param size Size of the buffer. */ - void write(uint16_t* data, size_t size) override; + void write16(uint16_t* data, size_t size) override; /** * @brief Full duplex transmission of one byte on the bus. @@ -147,7 +147,7 @@ public: * @param data Half word to write. * @return Half word read from the bus. */ - uint16_t transfer(uint16_t data) override; + uint16_t transfer16(uint16_t data) override; /** * @brief Full duplex transmission of multiple bytes on the bus. @@ -163,7 +163,7 @@ public: * @param data Buffer containing data to trasfer. * @param size Size of the buffer. */ - void transfer(uint16_t* data, size_t size) override; + void transfer16(uint16_t* data, size_t size) override; private: SPI spi; @@ -233,31 +233,34 @@ inline uint16_t SPIBus::read16() { return spi.read16(); } inline void SPIBus::read(uint8_t* data, size_t size) { spi.read(data, size); } -inline void SPIBus::read(uint16_t* data, size_t size) { spi.read(data, size); } +inline void SPIBus::read16(uint16_t* data, size_t size) +{ + spi.read16(data, size); +} inline void SPIBus::write(uint8_t data) { spi.write(data); } -inline void SPIBus::write(uint16_t data) { spi.write(data); } +inline void SPIBus::write16(uint16_t data) { spi.write(data); } inline void SPIBus::write(uint8_t* data, size_t size) { spi.write(data, size); } -inline void SPIBus::write(uint16_t* data, size_t size) +inline void SPIBus::write16(uint16_t* data, size_t size) { - spi.write(data, size); + spi.write16(data, size); } inline uint8_t SPIBus::transfer(uint8_t data) { return spi.transfer(data); } -inline uint16_t SPIBus::transfer(uint16_t data) { return spi.transfer(data); } +inline uint16_t SPIBus::transfer16(uint16_t data) { return spi.transfer(data); } inline void SPIBus::transfer(uint8_t* data, size_t size) { spi.transfer(data, size); } -inline void SPIBus::transfer(uint16_t* data, size_t size) +inline void SPIBus::transfer16(uint16_t* data, size_t size) { - spi.transfer(data, size); + spi.transfer16(data, size); } } // namespace Boardcore diff --git a/src/shared/drivers/spi/SPIBusInterface.h b/src/shared/drivers/spi/SPIBusInterface.h index 1cbdd05ff..12fde48f2 100644 --- a/src/shared/drivers/spi/SPIBusInterface.h +++ b/src/shared/drivers/spi/SPIBusInterface.h @@ -148,7 +148,7 @@ public: * @param data Buffer to be filled with received data. * @param size Size of the buffer. */ - virtual void read(uint16_t* data, size_t size) = 0; + virtual void read16(uint16_t* data, size_t size) = 0; /** * @brief Writes a single byte to the bus. @@ -162,7 +162,7 @@ public: * * @param data Half word to write. */ - virtual void write(uint16_t data) = 0; + virtual void write16(uint16_t data) = 0; /** * @brief Writes multiple bytes to the bus. @@ -178,7 +178,7 @@ public: * @param data Buffer containing data to write. * @param size Size of the buffer. */ - virtual void write(uint16_t* data, size_t size) = 0; + virtual void write16(uint16_t* data, size_t size) = 0; /** * @brief Full duplex transmission of one byte on the bus. @@ -194,7 +194,7 @@ public: * @param data Half word to write. * @return Half word read from the bus. */ - virtual uint16_t transfer(uint16_t data) = 0; + virtual uint16_t transfer16(uint16_t data) = 0; /** * @brief Full duplex transmission of multiple bytes on the bus. @@ -210,7 +210,7 @@ public: * @param data Buffer containing data to trasfer. * @param size Size of the buffer. */ - virtual void transfer(uint16_t* data, size_t size) = 0; + virtual void transfer16(uint16_t* data, size_t size) = 0; }; /** diff --git a/src/shared/drivers/spi/SPIDriver.h b/src/shared/drivers/spi/SPIDriver.h index 836f9782e..7bdb64ef3 100644 --- a/src/shared/drivers/spi/SPIDriver.h +++ b/src/shared/drivers/spi/SPIDriver.h @@ -24,5 +24,4 @@ #include "SPIBus.h" #include "SPIBusInterface.h" -#include "SPISlaveBus.h" #include "SPITransaction.h" diff --git a/src/shared/drivers/spi/SPISignalGenerator.h b/src/shared/drivers/spi/SPISignalGenerator.h deleted file mode 100644 index e3d7e31ee..000000000 --- a/src/shared/drivers/spi/SPISignalGenerator.h +++ /dev/null @@ -1,273 +0,0 @@ -/* Copyright (c) 2021 Skyward Experimental Rocketry - * Author: Alberto Nidasio - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#pragma once - -#include <drivers/timer/GeneralPurposeTimer.h> -#include <drivers/timer/TimerUtils.h> - -#include "SPIBusInterface.h" - -namespace Boardcore -{ - -/** - * @brief Generates SPI clock and chip select signal through two timers chained - * together. - * - * To configure which timer to use, change the following parameters: - * - chainChannel: Specify which channel of the master timer is used to chain - * the slave timer; - * - csChannel: Specify which channel of the master timer to use for CS signal - * generation; - * - sckChannel: Specify which channel of the slave timer to use for SCK signal - * generation. - * - * The chainChannel and CSChannel should be different. If they are equal the CS - * signal generated by the master timer will be reversed (high when active). - * - * GPIO must be already configured. - * - * This driver does not support the complementary timers channels. - */ -class SPISignalGenerator -{ -public: - /** - * @brief Create a SPISignalGenerator object. This does not configure the - * timers. - * - * @param nBytes Number of bytes for the sck signal. - * @param transactionFrequency Frequency of the SPI transactions. - * @param spiFrequency SPI clock signal frequency. - * @param spiMode SPI mode, this affects the clock polarity and phase. - * @param chainChannel Master timer channel used to chain the slave timer. - * @param csChannel Master timer channel used for CS signal generation. - * @param sckChannel Slave timer channel used for SCK signal generation. - * @param masterTimer Master timer which generates the CS signal. - * @param slaveTimer Slave timer which generates the SCK signal. - * @param slaveTriggerSource Trigger source form the slave timer. This - * depends on the timers combination. - */ - SPISignalGenerator( - size_t nBytes, int transactionFrequency, int spiFrequency = 1e6, - SPI::Mode spiMode = SPI::Mode::MODE_0, - TimerUtils::Channel chainChannel = TimerUtils::Channel::CHANNEL_2, - TimerUtils::Channel csChannel = TimerUtils::Channel::CHANNEL_2, - TimerUtils::Channel sckChannel = TimerUtils::Channel::CHANNEL_4, - TIM_TypeDef *masterTimer = TIM1, TIM_TypeDef *slaveTimer = TIM3, - TimerUtils::TriggerSource slaveTriggerSource = - TimerUtils::TriggerSource::ITR0); - - /** - * @brief Sets up the two timers. - */ - void configure(); - - /** - * @brief Enables SPI signal generation. - */ - void enable(); - - /** - * @brief Disables SPI signal generation. - */ - void disable(); - - /** - * @brief Generates a single spi transaction with the specified number of - * bytes. - * - * Configures the timers to generate the specified number of bytes, enables - * master timer one one-pulse mode and enables them. The master timer will - * be disabled automatically, but the slave timer will not. - * - * @param nBytes Size of the transaction in bytes. - */ - void generateSingleTransaction(size_t nBytes); - -private: - int nBytes; ///< SPI Clock pulses (divided by 8). - int transactionFrequency; ///< Frequency of the transactions are generated. - int spiFrequency; ///< SPI Clock frequency. - SPI::Mode spiMode; - TimerUtils::Channel chainChannel; - TimerUtils::Channel csChannel; - TimerUtils::Channel sckChannel; - GP16bitTimer masterTimer; ///< Master timer for CS generation. - GP16bitTimer slaveTimer; ///< Slave timer for SCK generation. - TimerUtils::TriggerSource slaveTriggerSource; -}; - -inline SPISignalGenerator::SPISignalGenerator( - size_t nBytes, int transactionFrequency, int spiFrequency, - SPI::Mode spiMode, TimerUtils::Channel chainChannel, - TimerUtils::Channel csChannel, TimerUtils::Channel sckChannel, - TIM_TypeDef *masterTimer, TIM_TypeDef *slaveTimer, - TimerUtils::TriggerSource slaveTriggerSource) - : nBytes(nBytes), transactionFrequency(transactionFrequency), - spiFrequency(spiFrequency), spiMode(spiMode), chainChannel(chainChannel), - csChannel(csChannel), sckChannel(sckChannel), masterTimer(masterTimer), - slaveTimer(slaveTimer), slaveTriggerSource(slaveTriggerSource) -{ -} - -inline void SPISignalGenerator::configure() -{ - // Configure master timer - { - masterTimer.reset(); - - // Ensures that the CS output will be high until the timers are enabled - // masterTimer.setCounter(UINT16_MAX); - - // Connect the specified channel to the trigger output - switch (chainChannel) - { - case TimerUtils::Channel::CHANNEL_1: - masterTimer.setMasterMode( - TimerUtils::MasterMode::OC1REF_OUTPUT); - break; - case TimerUtils::Channel::CHANNEL_2: - masterTimer.setMasterMode( - TimerUtils::MasterMode::OC2REF_OUTPUT); - break; - case TimerUtils::Channel::CHANNEL_3: - masterTimer.setMasterMode( - TimerUtils::MasterMode::OC3REF_OUTPUT); - break; - case TimerUtils::Channel::CHANNEL_4: - masterTimer.setMasterMode( - TimerUtils::MasterMode::OC4REF_OUTPUT); - break; - } - - // Set the prescaler and auto realod value - uint16_t autoReloadRegister = spiFrequency * 4 / transactionFrequency; - masterTimer.setPrescaler(TimerUtils::computePrescalerValue( - masterTimer.getTimer(), spiFrequency * 4)); - masterTimer.setAutoReloadRegister(autoReloadRegister); - - // Set channels capture/compare register - uint16_t ccRegister = nBytes * 8 * 4; - ccRegister = autoReloadRegister - ccRegister + 1; - - // Set chain channel - masterTimer.setCaptureCompareRegister(chainChannel, ccRegister); - masterTimer.setOutputCompareMode( - chainChannel, TimerUtils::OutputCompareMode::PWM_MODE_2); - - // Set CS channel if different - if (chainChannel != csChannel) - { - masterTimer.setCaptureCompareRegister(csChannel, ccRegister); - masterTimer.setOutputCompareMode( - csChannel, TimerUtils::OutputCompareMode::PWM_MODE_1); - - // Enable CS capture/compare output - masterTimer.enableCaptureCompareOutput(csChannel); - } - else - { - // If chain channel and cs channel are the same, the cs output - // must be from the complementary output - masterTimer.setCaptureCompareComplementaryPolarity( - chainChannel, TimerUtils::OutputComparePolarity::ACTIVE_LOW); - masterTimer.enableCaptureCompareComplementaryOutput(chainChannel); - } - - // Update the registers - masterTimer.generateUpdate(); - } - - // Configure slave timer - { - slaveTimer.reset(); - - // Set slaveTimer in gated mode - slaveTimer.setSlaveMode(TimerUtils::SlaveMode::GATED_MODE); - - // Set ITR1 as internal trigger source - slaveTimer.setTriggerSource(slaveTriggerSource); - - // Set the prescaler and auto realod value - slaveTimer.setPrescaler(TimerUtils::computePrescalerValue( - slaveTimer.getTimer(), spiFrequency * 4)); - slaveTimer.setAutoReloadRegister(1); - - // Set SCK capture/compare register - slaveTimer.setCaptureCompareRegister(sckChannel, 1); - - // Set channel 1 to toggle mode - slaveTimer.setOutputCompareMode(sckChannel, - TimerUtils::OutputCompareMode::TOGGLE); - if (spiMode >= SPI::Mode::MODE_2) - { - slaveTimer.setCaptureComparePolarity( - sckChannel, TimerUtils::OutputComparePolarity::ACTIVE_LOW); - } - - // Enable capture/compare output - slaveTimer.enableCaptureCompareOutput(sckChannel); - - // Update the register - slaveTimer.generateUpdate(); - } -} - -inline void SPISignalGenerator::enable() -{ - slaveTimer.enable(); - masterTimer.enable(); -} - -inline void SPISignalGenerator::disable() -{ - masterTimer.disable(); - slaveTimer.disable(); -} - -inline void SPISignalGenerator::generateSingleTransaction(size_t nBytes) -{ - // Change the number of bytes to generate - size_t backupNBytes = this->nBytes; - this->nBytes = nBytes; - - // Change the period to generate a transaction right away - int backupTransactionFrequency = transactionFrequency; - transactionFrequency = spiFrequency * 4 / (nBytes * 8 * 4); - - // Configure the timers - configure(); - - // Enable one pulse mode - masterTimer.enableOnePulseMode(); - - // Reset nBytes - this->nBytes = backupNBytes; - transactionFrequency = backupTransactionFrequency; - - // Start the signal generation - enable(); -} - -} // namespace Boardcore diff --git a/src/shared/drivers/spi/SPISlaveBus.h b/src/shared/drivers/spi/SPISlaveBus.h deleted file mode 100644 index fcd274c25..000000000 --- a/src/shared/drivers/spi/SPISlaveBus.h +++ /dev/null @@ -1,271 +0,0 @@ -/* Copyright (c) 2021 Skyward Experimental Rocketry - * Author: Alberto Nidasio - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#pragma once - -#include "SPIBusInterface.h" -#include "SPISignalGenerator.h" - -namespace Boardcore -{ - -/** - * @brief This implementation of SPIBusInterface uses the spi peripheral in - * slave mode and the spi signal generator to act as an spi master. - */ -class SPISlaveBus : public SPIBusInterface -{ -public: - SPISlaveBus(SPIType* spi, SPISignalGenerator signalGenerator); - - ///< Delete copy/move contructors/operators. - SPISlaveBus(const SPISlaveBus&) = delete; - SPISlaveBus& operator=(const SPISlaveBus&) = delete; - SPISlaveBus(SPISlaveBus&&) = delete; - SPISlaveBus& operator=(SPISlaveBus&&) = delete; - - /** - * @brief Configures and enables the bus with the provided configuration. - * - * Since this implementation is not syncronized, if configure() is called on - * an already in use bus nothing will be done. - * - * Use SyncedSPIBus if you need to synchronize access to the bus. - */ - void configure(SPIBusConfig config) override; - - /** - * @brief See SPIBusInterface::select(). - */ - void select(GpioType& cs) override; - - /** - * @brief See SPIBusInterface::deselect(). - */ - void deselect(GpioType& cs) override; - - // Read, write and transfer operations - - /** - * @brief Reads a single byte from the bus. - * - * @return Byte read from the bus. - */ - uint8_t read() override; - - /** - * @brief Reads a single half word from the bus. - * - * @return Half word read from the bus. - */ - uint16_t read16() override; - - /** - * @brief Reads multiple bytes from the bus - * - * @param data Buffer to be filled with received data. - * @param size Size of the buffer in bytes. - */ - void read(uint8_t* data, size_t size) override; - - /** - * @brief Reads multiple half words from the bus - * - * @param data Buffer to be filled with received data. - * @param size Size of the buffer in bytes. - */ - void read(uint16_t* data, size_t size) override; - - /** - * @brief Writes a single byte to the bus. - * - * @param data Byte to write. - */ - void write(uint8_t data) override; - - /** - * @brief Writes a single half word to the bus. - * - * @param data Half word to write. - */ - void write(uint16_t data) override; - - /** - * @brief Writes multiple bytes to the bus. - * - * @param data Buffer containing data to write. - * @param size Size of the buffer in bytes. - */ - void write(uint8_t* data, size_t size) override; - - /** - * @brief Writes multiple half words to the bus. - * - * @param data Buffer containing data to write. - * @param size Size of the buffer in bytes. - */ - void write(uint16_t* data, size_t size) override; - - /** - * @brief Full duplex transmission of one byte on the bus. - * - * @param data Byte to write. - * @return Byte read from the bus. - */ - uint8_t transfer(uint8_t data) override; - - /** - * @brief Full duplex transmission of one half word on the bus. - * - * @param data Half word to write. - * @return Half word read from the bus. - */ - uint16_t transfer(uint16_t data) override; - - /** - * @brief Full duplex transmission of multiple bytes on the bus. - * - * @param data Buffer containing data to trasfer. - * @param size Size of the buffer in bytes. - */ - void transfer(uint8_t* data, size_t size) override; - - /** - * @brief Full duplex transmission of multiple half words on the bus. - * - * @param data Buffer containing data to trasfer. - * @param size Size of the buffer in bytes. - */ - void transfer(uint16_t* data, size_t size) override; - -private: - SPI spi; - SPISignalGenerator signalGenerator; - SPIBusConfig config{}; -}; - -inline SPISlaveBus::SPISlaveBus(SPIType* spi, - SPISignalGenerator signalGenerator) - : spi(spi), signalGenerator(signalGenerator) -{ -} - -inline void SPISlaveBus::configure(SPIBusConfig newConfig) -{ - // Save the new configuration - config = newConfig; - - // Wait until the peripheral is done before changing configuration - spi.waitPeripheral(); - - // Disable the peripheral - spi.disable(); - - // Configure clock polarity and phase - spi.setMode(config.mode); - - // Configure bit order - spi.setBitOrder(config.bitOrder); - - // Enable the peripheral - spi.enable(); -} - -inline void SPISlaveBus::select(GpioType& cs) {} - -inline void SPISlaveBus::deselect(GpioType& cs) {} - -// Read, write and transfer operations - -inline uint8_t SPISlaveBus::read() -{ - signalGenerator.generateSingleTransaction(1); - return spi.read(); -} - -inline uint16_t SPISlaveBus::read16() -{ - signalGenerator.generateSingleTransaction(2); - return spi.read16(); -} - -inline void SPISlaveBus::read(uint8_t* data, size_t size) -{ - signalGenerator.generateSingleTransaction(size); - spi.read(data, size); -} - -inline void SPISlaveBus::read(uint16_t* data, size_t size) -{ - signalGenerator.generateSingleTransaction(size); - spi.read(data, size); -} - -inline void SPISlaveBus::write(uint8_t data) -{ - signalGenerator.generateSingleTransaction(1); - spi.write(data); -} - -inline void SPISlaveBus::write(uint16_t data) -{ - signalGenerator.generateSingleTransaction(2); - spi.write(data); -} - -inline void SPISlaveBus::write(uint8_t* data, size_t size) -{ - signalGenerator.generateSingleTransaction(size); - spi.write(data, size); -} - -inline void SPISlaveBus::write(uint16_t* data, size_t size) -{ - signalGenerator.generateSingleTransaction(size); - spi.write(data, size); -} - -inline uint8_t SPISlaveBus::transfer(uint8_t data) -{ - signalGenerator.generateSingleTransaction(1); - return spi.transfer(data); -} - -inline uint16_t SPISlaveBus::transfer(uint16_t data) -{ - signalGenerator.generateSingleTransaction(2); - return spi.transfer(data); -} - -inline void SPISlaveBus::transfer(uint8_t* data, size_t size) -{ - signalGenerator.generateSingleTransaction(size); - spi.transfer(data, size); -} - -inline void SPISlaveBus::transfer(uint16_t* data, size_t size) -{ - signalGenerator.generateSingleTransaction(size); - spi.transfer(data, size); -} - -} // namespace Boardcore diff --git a/src/shared/drivers/spi/SPITransaction.cpp b/src/shared/drivers/spi/SPITransaction.cpp index faa330d96..e16ad6326 100644 --- a/src/shared/drivers/spi/SPITransaction.cpp +++ b/src/shared/drivers/spi/SPITransaction.cpp @@ -64,10 +64,10 @@ void SPITransaction::read(uint8_t* data, size_t size) bus.deselect(cs); } -void SPITransaction::read(uint16_t* data, size_t size) +void SPITransaction::read16(uint16_t* data, size_t size) { bus.select(cs); - bus.read(data, size); + bus.read16(data, size); bus.deselect(cs); } @@ -78,10 +78,10 @@ void SPITransaction::write(uint8_t data) bus.deselect(cs); } -void SPITransaction::write(uint16_t data) +void SPITransaction::write16(uint16_t data) { bus.select(cs); - bus.write(data); + bus.write16(data); bus.deselect(cs); } @@ -92,10 +92,10 @@ void SPITransaction::write(uint8_t* data, size_t size) bus.deselect(cs); } -void SPITransaction::write(uint16_t* data, size_t size) +void SPITransaction::write16(uint16_t* data, size_t size) { bus.select(cs); - bus.write(data, size); + bus.write16(data, size); bus.deselect(cs); } @@ -107,10 +107,10 @@ uint8_t SPITransaction::transfer(uint8_t data) return data; } -uint16_t SPITransaction::transfer(uint16_t data) +uint16_t SPITransaction::transfer16(uint16_t data) { bus.select(cs); - data = bus.transfer(data); + data = bus.transfer16(data); bus.deselect(cs); return data; } @@ -122,10 +122,10 @@ void SPITransaction::transfer(uint8_t* data, size_t size) bus.deselect(cs); } -void SPITransaction::transfer(uint16_t* data, size_t size) +void SPITransaction::transfer16(uint16_t* data, size_t size) { bus.select(cs); - bus.transfer(data, size); + bus.transfer16(data, size); bus.deselect(cs); } diff --git a/src/shared/drivers/spi/SPITransaction.h b/src/shared/drivers/spi/SPITransaction.h index 6c98cafbe..abd9ac33e 100644 --- a/src/shared/drivers/spi/SPITransaction.h +++ b/src/shared/drivers/spi/SPITransaction.h @@ -126,7 +126,7 @@ public: * @param data Buffer to be filled with received data. * @param size Size of the buffer in bytes. */ - void read(uint16_t *data, size_t size); + void read16(uint16_t *data, size_t size); /** * @brief Writes a single byte to the bus. @@ -140,7 +140,7 @@ public: * * @param data Half word to write. */ - void write(uint16_t data); + void write16(uint16_t data); /** * @brief Writes multiple bytes to the bus. @@ -156,7 +156,7 @@ public: * @param data Buffer containing data to write. * @param size Size of the buffer in bytes. */ - void write(uint16_t *data, size_t size); + void write16(uint16_t *data, size_t size); /** * @brief Full duplex transmission of one byte on the bus. @@ -172,7 +172,7 @@ public: * @param data Half word to write. * @return Half word read from the bus. */ - uint16_t transfer(uint16_t data); + uint16_t transfer16(uint16_t data); /** * @brief Full duplex transmission of multiple bytes on the bus. @@ -188,7 +188,7 @@ public: * @param data Buffer containing data to trasfer. * @param size Size of the buffer in bytes. */ - void transfer(uint16_t *data, size_t size); + void transfer16(uint16_t *data, size_t size); // Read, write and transfer operations with registers diff --git a/src/shared/sensors/MAX31855/MAX31855.cpp b/src/shared/sensors/MAX31855/MAX31855.cpp index ee52dd7ab..c55d08065 100644 --- a/src/shared/sensors/MAX31855/MAX31855.cpp +++ b/src/shared/sensors/MAX31855/MAX31855.cpp @@ -51,7 +51,7 @@ bool MAX31855::checkConnection() { SPITransaction spi{slave}; - spi.read(sample, sizeof(sample)); + spi.read16(sample, sizeof(sample)); } // Bits D0, D1 and D2 go high if thermocouple is open or shorted either to @@ -90,7 +90,7 @@ TemperatureData MAX31855::readInternalTemperature() { SPITransaction spi{slave}; - spi.read(sample, sizeof(sample)); + spi.read16(sample, sizeof(sample)); } TemperatureData result{}; diff --git a/src/tests/drivers/spi/test-spi-as-slave.cpp b/src/tests/drivers/spi/test-spi-as-slave.cpp deleted file mode 100644 index 525f9e5d8..000000000 --- a/src/tests/drivers/spi/test-spi-as-slave.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/* Copyright (c) 2020 Skyward Experimental Rocketry - * Author: Alberto Nidasio - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include <drivers/spi/SPI.h> -#include <miosix.h> - -using namespace miosix; -using namespace Boardcore; - -GpioPin sckPin = GpioPin(GPIOE_BASE, 4); -GpioPin misoPin = GpioPin(GPIOE_BASE, 2); -GpioPin mosiPin = GpioPin(GPIOE_BASE, 5); -GpioPin csPin = GpioPin(GPIOE_BASE, 6); - -uint16_t counter = 0; - -int main() -{ - // Setup gpio pins - csPin.mode(Mode::ALTERNATE); - csPin.alternateFunction(5); - sckPin.mode(Mode::ALTERNATE); - sckPin.alternateFunction(5); - misoPin.mode(Mode::ALTERNATE); - misoPin.alternateFunction(5); - mosiPin.mode(Mode::ALTERNATE); - mosiPin.alternateFunction(5); - - // Setup spi as a slave - SPI spi(SPI4); - spi.reset(); - spi.enable(); - spi.set16BitFrameFormat(); - - printf("Slave started\n"); - - while (true) - { - spi.getSpi()->DR = counter; - counter++; - // Wait until Tx buffer is empty and until the peripheral is still busy - while ((spi.getSpi()->SR & SPI_SR_TXE) == 0) - ; - } -} diff --git a/src/tests/drivers/spi/test-spi-signal-generator.cpp b/src/tests/drivers/spi/test-spi-signal-generator.cpp deleted file mode 100644 index c89af2e3d..000000000 --- a/src/tests/drivers/spi/test-spi-signal-generator.cpp +++ /dev/null @@ -1,80 +0,0 @@ -/* Copyright (c) 2021 Skyward Experimental Rocketry - * Author: Alberto Nidasio - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include <drivers/spi/SPISignalGenerator.h> -#include <miosix.h> - -using namespace miosix; -using namespace Boardcore; -using namespace TimerUtils; - -/** - * Pin list: - * PB4 - TIM3_CH1 - CS signal - * PE5 - TIM9_CH1 - SCK signal - * - * We'll use two timers to generate clock and chip select signals for the SPI - * peripheral. - */ - -GpioPin csPin = GpioPin(GPIOA_BASE, 11); -GpioPin sckPin = GpioPin(GPIOB_BASE, 1); - -void setupGPIOs(); - -int main() -{ - setupGPIOs(); - - SPISignalGenerator spiSignalGenerator{2, - 1000, - 1000000, - SPI::Mode::MODE_0, - TimerUtils::Channel::CHANNEL_1, - TimerUtils::Channel::CHANNEL_4, - TimerUtils::Channel::CHANNEL_4}; - spiSignalGenerator.configure(); - - Thread::sleep(1000); - - spiSignalGenerator.generateSingleTransaction(8); - - Thread::sleep(1000); - spiSignalGenerator.configure(); - spiSignalGenerator.enable(); - - Thread::sleep(5 * 1000); - - spiSignalGenerator.disable(); - - while (true) - Thread::sleep(10000); -} - -void setupGPIOs() -{ - csPin.mode(Mode::ALTERNATE); - csPin.alternateFunction(1); - - sckPin.mode(Mode::ALTERNATE); - sckPin.alternateFunction(2); -} diff --git a/src/tests/drivers/spi/test-spi-slave-bus.cpp b/src/tests/drivers/spi/test-spi-slave-bus.cpp deleted file mode 100644 index e75d047e0..000000000 --- a/src/tests/drivers/spi/test-spi-slave-bus.cpp +++ /dev/null @@ -1,109 +0,0 @@ -/* Copyright (c) 2020 Skyward Experimental Rocketry - * Author: Alberto Nidasio - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include <drivers/spi/SPIDriver.h> -#include <miosix.h> - -using namespace miosix; -using namespace Boardcore; - -GpioPin sckPin = GpioPin(GPIOE_BASE, 4); -GpioPin misoPin = GpioPin(GPIOE_BASE, 2); -GpioPin mosiPin = GpioPin(GPIOE_BASE, 5); -GpioPin csPin = GpioPin(GPIOE_BASE, 6); - -GpioPin timerCsPin = GpioPin(GPIOA_BASE, 11); -GpioPin timerSckPin = GpioPin(GPIOB_BASE, 1); - -int main() -{ - // Setup gpio pins - csPin.mode(Mode::ALTERNATE); - csPin.alternateFunction(5); - sckPin.mode(Mode::ALTERNATE); - sckPin.alternateFunction(5); - misoPin.mode(Mode::ALTERNATE); - misoPin.alternateFunction(5); - mosiPin.mode(Mode::ALTERNATE); - mosiPin.alternateFunction(5); - - timerCsPin.mode(Mode::ALTERNATE); - timerCsPin.alternateFunction(1); - timerSckPin.mode(Mode::ALTERNATE); - timerSckPin.alternateFunction(2); - - // Setup spi as a slave - SPISignalGenerator spiSignalGenerator{2, - 100, - 1000000, - SPI::Mode::MODE_0, - TimerUtils::Channel::CHANNEL_1, - TimerUtils::Channel::CHANNEL_4, - TimerUtils::Channel::CHANNEL_4}; - SPISlaveBus bus(SPI4, spiSignalGenerator); - SPISlave spiSlave(bus, csPin, {}); - spiSlave.config.clockDivider = SPI::ClockDivider::DIV_64; - - SPITransaction transaction(spiSlave); - - uint8_t buffer8[6]; - uint16_t buffer16[6]; - - transaction.read(); - delayMs(1); - transaction.read16(); - delayMs(1); - transaction.read(buffer8, sizeof(buffer8)); - delayMs(1); - transaction.read(buffer16, sizeof(buffer16)); - delayMs(1); - transaction.write((uint8_t)0xAB); - delayMs(1); - transaction.write((uint16_t)0xABCD); - delayMs(1); - buffer8[0] = 0x01; - buffer8[1] = 0x23; - buffer8[2] = 0x45; - buffer8[3] = 0x67; - buffer8[4] = 0x89; - buffer8[5] = 0xAB; - transaction.write(buffer8, sizeof(buffer8)); - delayMs(1); - buffer16[0] = 0x0101; - buffer16[1] = 0x2323; - buffer16[2] = 0x4545; - buffer16[3] = 0x6767; - buffer16[4] = 0x8989; - buffer16[5] = 0xABAB; - transaction.write(buffer16, sizeof(buffer16)); - delayMs(1); - transaction.transfer((uint8_t)0xAB); - delayMs(1); - transaction.transfer((uint16_t)0xABCD); - delayMs(1); - transaction.transfer(buffer8, sizeof(buffer8)); - delayMs(1); - transaction.transfer(buffer16, sizeof(buffer16)); - - while (true) - delayMs(1000); -} diff --git a/src/tests/drivers/spi/test-spi.cpp b/src/tests/drivers/spi/test-spi.cpp index 0d52bd427..813cc1a1f 100644 --- a/src/tests/drivers/spi/test-spi.cpp +++ b/src/tests/drivers/spi/test-spi.cpp @@ -62,11 +62,11 @@ int main() delayMs(1); transaction.read(buffer8, 6); delayMs(1); - transaction.read(buffer16, 6); + transaction.read16(buffer16, 6); delayMs(1); transaction.write((uint8_t)0xAB); delayMs(1); - transaction.write((uint16_t)0xABCD); + transaction.write16((uint16_t)0xABCD); delayMs(1); buffer8[0] = 0x01; buffer8[1] = 0x23; @@ -82,15 +82,15 @@ int main() buffer16[3] = 0x6767; buffer16[4] = 0x8989; buffer16[5] = 0xABAB; - transaction.write(buffer16, 6); + transaction.write16(buffer16, 6); delayMs(1); transaction.transfer((uint8_t)0xAB); delayMs(1); - transaction.transfer((uint16_t)0xABCD); + transaction.transfer16((uint16_t)0xABCD); delayMs(1); transaction.transfer(buffer8, 6); delayMs(1); - transaction.transfer(buffer16, 6); + transaction.transfer16(buffer16, 6); while (true) delayMs(1000); -- GitLab