Skip to content

Commit

Permalink
Change time tipes in spi functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilchmela committed Sep 16, 2024
1 parent 421e58e commit c746094
Show file tree
Hide file tree
Showing 19 changed files with 95 additions and 89 deletions.
6 changes: 3 additions & 3 deletions Sts1CobcSw/CobcSoftware/SpiStartupTestAndSupervisorThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,18 @@ class SpiStartupTestAndSupervisorThread : public RODOS::StaticThread<stackSize>
TIME_LOOP(0, value_of(supervisionPeriod))
{
auto timeoutHappened = false;
if(CurrentRodosTime() > RodosTime(framEpsSpi.TransferEnd()))
if(CurrentRodosTime() > framEpsSpi.TransferEnd())
{
DEBUG_PRINT("FRAM/EPS SPI timeout occurred\n");
timeoutHappened = true;
}
if(CurrentRodosTime() > RodosTime(flash::spi.TransferEnd()))
if(CurrentRodosTime() > flash::spi.TransferEnd())
{
DEBUG_PRINT("Flash SPI timeout occurred\n");
timeoutHappened = true;
persistentVariables.template Increment<"nFlashErrors">();
}
if(CurrentRodosTime() > RodosTime(rf::spi.TransferEnd()))
if(CurrentRodosTime() > rf::spi.TransferEnd())
{
DEBUG_PRINT("RF SPI timeout occurred\n");
timeoutHappened = true;
Expand Down
5 changes: 3 additions & 2 deletions Sts1CobcSw/FileSystem/FileSystem.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <Sts1CobcSw/FileSystem/FileSystem.hpp>
#include <Sts1CobcSw/Periphery/Flash.hpp>
#include <Sts1CobcSw/Serial/Byte.hpp>
#include <Sts1CobcSw/Utility/Time.hpp>

#include <rodos_no_using_namespace.h>

Expand Down Expand Up @@ -64,9 +65,9 @@ lfs_config const lfsConfig{.context = nullptr,

// TODO: Test with real HW
// max. 3.5 ms acc. W25Q01JV datasheet
constexpr auto pageProgramTimeout = 5 * RODOS::MILLISECONDS;
constexpr auto pageProgramTimeout = 5 * ms;
// max. 400 ms acc. W25Q01JV datasheet (lfs_config.block_size = flash::sectorSize)
constexpr auto blockEraseTimeout = 500 * RODOS::MILLISECONDS;
constexpr auto blockEraseTimeout = 500 * ms;


// --- Public function definitions ---
Expand Down
5 changes: 3 additions & 2 deletions Sts1CobcSw/FileSystem/LfsFlash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <Sts1CobcSw/FileSystem/LfsStorageDevice.hpp> // IWYU pragma: associated
#include <Sts1CobcSw/Periphery/Flash.hpp>
#include <Sts1CobcSw/Serial/Byte.hpp>
#include <Sts1CobcSw/Utility/Time.hpp>

#include <rodos_no_using_namespace.h>

Expand Down Expand Up @@ -59,9 +60,9 @@ lfs_config const lfsConfig = lfs_config{.context = nullptr,

// TODO: Test with real HW
// max. 3.5 ms acc. W25Q01JV datasheet
constexpr auto pageProgramTimeout = 5 * RODOS::MILLISECONDS;
constexpr auto pageProgramTimeout = 5 * ms;
// max. 400 ms acc. W25Q01JV datasheet (lfs_config.block_size = flash::sectorSize)
constexpr auto blockEraseTimeout = 500 * RODOS::MILLISECONDS;
constexpr auto blockEraseTimeout = 500 * ms;


auto Initialize() -> void
Expand Down
3 changes: 2 additions & 1 deletion Sts1CobcSw/FramSections/PersistentVariables.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <Sts1CobcSw/FramSections/Section.hpp>
#include <Sts1CobcSw/FramSections/Subsections.hpp>
#include <Sts1CobcSw/Periphery/Fram.hpp>
#include <Sts1CobcSw/Utility/Time.hpp>

#include <rodos/api/rodos-semaphore.h>
#include <rodos/api/timemodel.h>
Expand Down Expand Up @@ -57,7 +58,7 @@ class PersistentVariables
static constexpr auto subsections2 = Subsections<parentSection2, PersistentVariableInfos...>();

// With a baud rate of 48 MHz we can read 6000 bytes in 1 ms, which should be more than enough
static constexpr auto spiTimeout = 1 * RODOS::MILLISECONDS;
static constexpr auto spiTimeout = 1 * ms;

static RODOS::Semaphore semaphore;
};
Expand Down
9 changes: 5 additions & 4 deletions Sts1CobcSw/Hal/Spi.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <Sts1CobcSw/Hal/IoNames.hpp>
#include <Sts1CobcSw/Hal/Spi.hpp>
#include <Sts1CobcSw/Utility/Time.hpp>

#include <rodos_no_using_namespace.h>

Expand All @@ -12,7 +13,7 @@ Spi::Spi(RODOS::SPI_IDX spiIndex,
RODOS::GPIO_PIN mosiPin)
: spi_(spiIndex, sckPin, misoPin, mosiPin, spiNssDummyPin)
{
transferEnd_.put(RODOS::END_OF_TIME);
transferEnd_.put(value_of(endOfTime));
}


Expand All @@ -21,15 +22,15 @@ auto Initialize(Spi * spi, std::uint32_t baudRate) -> void
// spi.init() only returns -1 if the SPI_IDX is out of range. Since we can check that statically
// we do not need to report that error at runtime.
spi->spi_.init(baudRate, /*slave=*/false, /*tiMode=*/false);
spi->transferEnd_.put(RODOS::END_OF_TIME);
spi->transferEnd_.put(value_of(endOfTime));
}


auto Spi::TransferEnd() const -> std::int64_t
auto Spi::TransferEnd() const -> RodosTime
{
std::int64_t transferEnd = 0;
this->transferEnd_.get(transferEnd);
return transferEnd;
return RodosTime(transferEnd);
}


Expand Down
8 changes: 5 additions & 3 deletions Sts1CobcSw/Hal/Spi.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once


#include <Sts1CobcSw/Utility/Time.hpp>

#include <rodos_no_using_namespace.h>

#include <cstddef>
Expand All @@ -22,12 +24,12 @@ class Spi
friend auto Initialize(Spi * spi, std::uint32_t baudRate) -> void;

template<typename T, std::size_t extent>
friend auto WriteTo(Spi * spi, std::span<T const, extent> data, std::int64_t timeout) -> void;
friend auto WriteTo(Spi * spi, std::span<T const, extent> data, Duration timeout) -> void;

template<typename T, std::size_t extent>
friend auto ReadFrom(Spi * spi, std::span<T, extent> data, std::int64_t timeout) -> void;
friend auto ReadFrom(Spi * spi, std::span<T, extent> data, Duration timeout) -> void;

auto TransferEnd() const -> std::int64_t;
auto TransferEnd() const -> RodosTime;
auto BaudRate() -> std::int32_t;


Expand Down
13 changes: 7 additions & 6 deletions Sts1CobcSw/Hal/Spi.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,34 @@


#include <Sts1CobcSw/Hal/Spi.hpp>
#include <Sts1CobcSw/Utility/Time.hpp>

#include <rodos_no_using_namespace.h>


namespace sts1cobcsw::hal
{
template<typename T, std::size_t extent>
auto WriteTo(Spi * spi, std::span<T const, extent> data, std::int64_t timeout) -> void
auto WriteTo(Spi * spi, std::span<T const, extent> data, Duration timeout) -> void
{
// spi.write() only returns -1 or the given buffer length. It only returns -1 if the SPI is not
// initialized, which we can check/ensure statically. Therefore, we do not need to check the
// return value at runtime.
spi->transferEnd_.put(RODOS::NOW() + timeout);
spi->transferEnd_.put(value_of(CurrentRodosTime() + timeout));
spi->spi_.write(data.data(), data.size_bytes());
spi->transferEnd_.put(RODOS::END_OF_TIME);
spi->transferEnd_.put(value_of(endOfTime));
}


template<typename T, std::size_t extent>
auto ReadFrom(Spi * spi, std::span<T, extent> data, std::int64_t timeout) -> void
auto ReadFrom(Spi * spi, std::span<T, extent> data, Duration timeout) -> void
{
// spi.read() only returns -1 or the given buffer length. It only returns -1 if the SPI is not
// initialized, which we can check/ensure statically. Therefore, we do not need to check the
// return value at runtime.
spi->transferEnd_.put(RODOS::NOW() + timeout);
spi->transferEnd_.put(value_of(CurrentRodosTime() + timeout));
spi->spi_.read(data.data(), data.size_bytes());
spi->transferEnd_.put(RODOS::END_OF_TIME);
spi->transferEnd_.put(value_of(endOfTime));
}


Expand Down
7 changes: 4 additions & 3 deletions Sts1CobcSw/Periphery/Eps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <Sts1CobcSw/Serial/Serial.hpp>
#include <Sts1CobcSw/Utility/FlatArray.hpp>
#include <Sts1CobcSw/Utility/Span.hpp>
#include <Sts1CobcSw/Utility/Time.hpp>

#include <rodos_no_using_namespace.h>

Expand Down Expand Up @@ -88,7 +89,7 @@ enum class ResetType

// --- Private globals ---

constexpr auto spiTimeout = 1 * RODOS::MILLISECONDS;
constexpr auto spiTimeout = 1 * ms;

auto adc4CsGpioPin = hal::GpioPin(hal::epsAdc4CsPin);
auto adc5CsGpioPin = hal::GpioPin(hal::epsAdc5CsPin);
Expand Down Expand Up @@ -226,8 +227,8 @@ auto ReadAdc(hal::GpioPin * adcCsPin) -> AdcValues
// According to the datasheet at most 514 conversions are done after a conversion command
// (depends on averaging and channels). This takes 514 * (t_acq + t_conv) + wakeup = 514 * (0.6
// + 3.5) us + 65 us = 2172.4 us.
static constexpr auto conversionTime = 3 * RODOS::MILLISECONDS;
RODOS::AT(RODOS::NOW() + conversionTime);
static constexpr auto conversionTime = 3 * ms;
SuspendFor(conversionTime);

// Resolution is 12 bit, sent like this: [0 0 0 0 MSB x x x], [x x x x x x x LSB]
auto adcData = Buffer<AdcValues>{};
Expand Down
25 changes: 13 additions & 12 deletions Sts1CobcSw/Periphery/Flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <Sts1CobcSw/Serial/Byte.hpp>
#include <Sts1CobcSw/Serial/Serial.hpp>
#include <Sts1CobcSw/Utility/Span.hpp>
#include <Sts1CobcSw/Utility/Time.hpp>

#include <rodos_no_using_namespace.h>

Expand Down Expand Up @@ -40,7 +41,7 @@ hal::Spi spi =

// Baud rate = 48 MHz, largest data transfer = 1 page = 256 bytes -> spiTimeout = 1 ms is enough for
// all transfers
constexpr auto spiTimeout = 1 * RODOS::MILLISECONDS;
constexpr auto spiTimeout = 1 * ms;
constexpr auto endianness = std::endian::big;

// Instructions according to section 7.3 in W25Q01JV datasheet
Expand Down Expand Up @@ -68,13 +69,13 @@ auto DisableWriting() -> void;
auto IsBusy() -> bool;

template<std::size_t extent>
auto Write(std::span<Byte const, extent> data, std::int64_t timeout) -> void;
auto Write(std::span<Byte const, extent> data, Duration timeout) -> void;

template<std::size_t extent>
auto Read(std::span<Byte, extent> data, std::int64_t timeout) -> void;
auto Read(std::span<Byte, extent> data, Duration timeout) -> void;

template<std::size_t size>
auto Read(std::int64_t timeout) -> std::array<Byte, size>;
auto Read(Duration timeout) -> std::array<Byte, size>;

template<SimpleInstruction const & instruction>
requires(instruction.answerLength > 0)
Expand Down Expand Up @@ -169,17 +170,17 @@ auto EraseSector(std::uint32_t address) -> void
}


auto WaitWhileBusy(std::int64_t timeout) -> Result<void>
auto WaitWhileBusy(Duration timeout) -> Result<void>
{
auto const pollingCycleTime = 1 * RODOS::MILLISECONDS;
auto const reactivationTime = RODOS::NOW() + timeout;
auto const pollingCycleTime = 1 * ms;
auto const reactivationTime = CurrentRodosTime() + timeout;
while(IsBusy())
{
if(RODOS::NOW() >= reactivationTime)
if(CurrentRodosTime() >= reactivationTime)
{
return ErrorCode::timeout;
}
RODOS::AT(RODOS::NOW() + pollingCycleTime);
SuspendFor(pollingCycleTime);
}
return outcome_v2::success();
}
Expand Down Expand Up @@ -225,21 +226,21 @@ auto IsBusy() -> bool


template<std::size_t extent>
inline auto Write(std::span<Byte const, extent> data, std::int64_t timeout) -> void
inline auto Write(std::span<Byte const, extent> data, Duration timeout) -> void
{
hal::WriteTo(&spi, data, timeout);
}


template<std::size_t extent>
inline auto Read(std::span<Byte, extent> data, std::int64_t timeout) -> void
inline auto Read(std::span<Byte, extent> data, Duration timeout) -> void
{
hal::ReadFrom(&spi, data, timeout);
}


template<std::size_t size>
inline auto Read(std::int64_t timeout) -> std::array<Byte, size>
inline auto Read(Duration timeout) -> std::array<Byte, size>
{
auto answer = std::array<Byte, size>{};
hal::ReadFrom(&spi, Span(&answer), timeout);
Expand Down
3 changes: 2 additions & 1 deletion Sts1CobcSw/Periphery/Flash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <Sts1CobcSw/Hal/Spi.hpp>
#include <Sts1CobcSw/Outcome/Outcome.hpp>
#include <Sts1CobcSw/Serial/Byte.hpp>
#include <Sts1CobcSw/Utility/Time.hpp>

#include <array>
#include <cstddef>
Expand Down Expand Up @@ -60,6 +61,6 @@ auto Initialize() -> void;
[[nodiscard]] auto ReadPage(std::uint32_t address) -> Page;
auto ProgramPage(std::uint32_t address, PageSpan data) -> void;
auto EraseSector(std::uint32_t address) -> void;
[[nodiscard]] auto WaitWhileBusy(std::int64_t timeout) -> Result<void>;
[[nodiscard]] auto WaitWhileBusy(Duration timeout) -> Result<void>;
auto ActualBaudRate() -> std::int32_t;
}
7 changes: 4 additions & 3 deletions Sts1CobcSw/Periphery/FlashMock.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Sts1CobcSw/Periphery/FlashMock.hpp>
#include <Sts1CobcSw/Utility/Time.hpp>

#include <strong_type/type.hpp>

Expand Down Expand Up @@ -56,7 +57,7 @@ auto EraseSector(std::uint32_t address) -> void
}


auto WaitWhileBusy(std::int64_t timeout) -> Result<void>
auto WaitWhileBusy(Duration timeout) -> Result<void>
{
return doWaitWhileBusy(timeout);
}
Expand Down Expand Up @@ -106,7 +107,7 @@ auto SetDoEraseSector(void (*doEraseSectorFunction)(std::uint32_t address)) -> v
}


auto SetDoWaitWhileBusy(Result<void> (*doWaitWhileBusyFunction)(std::int64_t timeout)) -> void
auto SetDoWaitWhileBusy(Result<void> (*doWaitWhileBusyFunction)(Duration timeout)) -> void
{
doWaitWhileBusy = doWaitWhileBusyFunction;
}
Expand Down Expand Up @@ -169,7 +170,7 @@ auto DoEraseSector([[maybe_unused]] std::uint32_t address) -> void
}


auto DoWaitWhileBusy([[maybe_unused]] std::int64_t timeout) -> Result<void>
auto DoWaitWhileBusy([[maybe_unused]] Duration timeout) -> Result<void>
{
return outcome_v2::success();
}
Expand Down
5 changes: 3 additions & 2 deletions Sts1CobcSw/Periphery/FlashMock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <Sts1CobcSw/Outcome/Outcome.hpp>
#include <Sts1CobcSw/Periphery/Flash.hpp>
#include <Sts1CobcSw/Serial/Byte.hpp>
#include <Sts1CobcSw/Utility/Time.hpp>

#include <array>
#include <cstddef>
Expand All @@ -21,7 +22,7 @@ auto SetDoReadStatusRegister(Byte (*doReadStatusRegisterFunction)(std::int8_t re
auto SetDoReadPage(Page (*doReadPageFunction)(std::uint32_t address)) -> void;
auto SetDoProgramPage(void (*doProgramPageFunction)(std::uint32_t address, PageSpan data)) -> void;
auto SetDoEraseSector(void (*doEraseSectorFunction)(std::uint32_t address)) -> void;
auto SetDoWaitWhileBusyFunction(Result<void> (*doWaitWhileBusy)(std::int64_t timeout)) -> void;
auto SetDoWaitWhileBusyFunction(Result<void> (*doWaitWhileBusy)(Duration timeout)) -> void;
auto SetDoActualBaudRate(std::int32_t (*doActualBaudRateFunction)()) -> void;

namespace empty
Expand All @@ -35,7 +36,7 @@ auto DoReadStatusRegister(std::int8_t registerNo) -> Byte;
auto DoReadPage(std::uint32_t address) -> Page;
auto DoProgramPage(std::uint32_t address, PageSpan data) -> void;
auto DoEraseSector(std::uint32_t address) -> void;
auto DoWaitWhileBusy(std::int64_t timeout) -> Result<void>;
auto DoWaitWhileBusy(Duration timeout) -> Result<void>;
auto DoActualBaudRate() -> std::int32_t;
}
}
7 changes: 4 additions & 3 deletions Sts1CobcSw/Periphery/Fram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <Sts1CobcSw/Periphery/FramEpsSpi.hpp>
#include <Sts1CobcSw/Serial/Serial.hpp>
#include <Sts1CobcSw/Utility/Span.hpp>
#include <Sts1CobcSw/Utility/Time.hpp>

#include <rodos_no_using_namespace.h>

Expand All @@ -23,7 +24,7 @@ EdacVariable<bool> framIsWorking(true);

// --- Private globals ---

constexpr auto spiTimeout = 1 * RODOS::MILLISECONDS;
constexpr auto spiTimeout = 1 * ms;
constexpr auto endianness = std::endian::big;

// Command opcodes according to section 4.1 in CY15B108QN-40SXI datasheet. I couldn't use an enum
Expand Down Expand Up @@ -76,7 +77,7 @@ auto ActualBaudRate() -> std::int32_t

namespace internal
{
auto WriteTo(Address address, void const * data, std::size_t nBytes, std::int64_t timeout) -> void
auto WriteTo(Address address, void const * data, std::size_t nBytes, Duration timeout) -> void
{
SetWriteEnableLatch();
csGpioPin.Reset();
Expand All @@ -89,7 +90,7 @@ auto WriteTo(Address address, void const * data, std::size_t nBytes, std::int64_
}


auto ReadFrom(Address address, void * data, std::size_t nBytes, std::int64_t timeout) -> void
auto ReadFrom(Address address, void * data, std::size_t nBytes, Duration timeout) -> void
{
csGpioPin.Reset();
hal::WriteTo(&framEpsSpi, Span(opcode::readData), spiTimeout);
Expand Down
Loading

0 comments on commit c746094

Please sign in to comment.