mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/sta-core.git
synced 2025-06-12 01:25:59 +00:00
Add SPI settings
This commit is contained in:
parent
ae460b69f6
commit
3c8fce14f6
71
include/sta/spi_settings.hpp
Normal file
71
include/sta/spi_settings.hpp
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
#ifndef STA_SPI_SETTINGS_HPP
|
||||||
|
#define STA_SPI_SETTINGS_HPP
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
|
||||||
|
namespace sta
|
||||||
|
{
|
||||||
|
enum class SpiClkPolarity
|
||||||
|
{
|
||||||
|
LOW,
|
||||||
|
HIGH
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class SpiClkPhase
|
||||||
|
{
|
||||||
|
EDGE_1,
|
||||||
|
EDGE_2
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class SpiMode
|
||||||
|
{
|
||||||
|
MODE_0,
|
||||||
|
MODE_1,
|
||||||
|
MODE_2,
|
||||||
|
MODE_3
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class SpiDataSize
|
||||||
|
{
|
||||||
|
BIT_8,
|
||||||
|
BIT_16
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class SpiBitOrder
|
||||||
|
{
|
||||||
|
MSB,
|
||||||
|
LSB
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class SpiBaudRatePrescaler
|
||||||
|
{
|
||||||
|
BRP_2,
|
||||||
|
BRP_4,
|
||||||
|
BRP_8,
|
||||||
|
BRP_16,
|
||||||
|
BRP_32,
|
||||||
|
BRP_64,
|
||||||
|
BRP_128,
|
||||||
|
BRP_256
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct SpiSettings
|
||||||
|
{
|
||||||
|
SpiMode mode;
|
||||||
|
SpiDataSize dataSize;
|
||||||
|
SpiBitOrder bitOrder;
|
||||||
|
|
||||||
|
uint32_t clkSpeed;
|
||||||
|
SpiBaudRatePrescaler baudRatePrescaler; /**< Subject to change */
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
SpiClkPolarity getSpiClkPolarity(SpiMode mode);
|
||||||
|
SpiClkPhase getSpiClkPhase(SpiMode mode);
|
||||||
|
SpiMode getSpiMode(SpiClkPolarity polarity, SpiClkPhase phase);
|
||||||
|
} // namespace sta
|
||||||
|
|
||||||
|
|
||||||
|
#endif // STA_SPI_SETTINGS_HPP
|
72
src/spi_settings.cpp
Normal file
72
src/spi_settings.cpp
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
#include <sta/spi_settings.hpp>
|
||||||
|
|
||||||
|
#include <sta/assert.hpp>
|
||||||
|
#include <sta/lang.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
namespace sta
|
||||||
|
{
|
||||||
|
SpiClkPolarity getSpiClkPolarity(SpiMode mode)
|
||||||
|
{
|
||||||
|
switch (mode)
|
||||||
|
{
|
||||||
|
case SpiMode::MODE_0:
|
||||||
|
case SpiMode::MODE_1:
|
||||||
|
return SpiClkPolarity::LOW;
|
||||||
|
|
||||||
|
case SpiMode::MODE_2:
|
||||||
|
case SpiMode::MODE_3:
|
||||||
|
return SpiClkPolarity::HIGH;
|
||||||
|
|
||||||
|
default:
|
||||||
|
// Unreachable case
|
||||||
|
STA_ASSERT_MSG(false, "Case for SpiMode enum not handled");
|
||||||
|
STA_UNREACHABLE();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiClkPhase getSpiClkPhase(SpiMode mode)
|
||||||
|
{
|
||||||
|
switch (mode)
|
||||||
|
{
|
||||||
|
case SpiMode::MODE_0:
|
||||||
|
case SpiMode::MODE_2:
|
||||||
|
return SpiClkPhase::EDGE_1;
|
||||||
|
|
||||||
|
case SpiMode::MODE_1:
|
||||||
|
case SpiMode::MODE_3:
|
||||||
|
return SpiClkPhase::EDGE_2;
|
||||||
|
|
||||||
|
default:
|
||||||
|
// Unreachable case
|
||||||
|
STA_ASSERT_MSG(false, "Case for SpiMode enum not handled");
|
||||||
|
STA_UNREACHABLE();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiMode getSpiMode(SpiClkPolarity polarity, SpiClkPhase phase)
|
||||||
|
{
|
||||||
|
if (polarity == SpiClkPolarity::LOW)
|
||||||
|
{
|
||||||
|
if (phase == SpiClkPhase::EDGE_1)
|
||||||
|
{
|
||||||
|
return SpiMode::MODE_0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return SpiMode::MODE_1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (phase == SpiClkPhase::EDGE_1)
|
||||||
|
{
|
||||||
|
return SpiMode::MODE_2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return SpiMode::MODE_3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} // namespace sta
|
Loading…
x
Reference in New Issue
Block a user