21 #ifndef ARM_CORTEX_STM32_COMMON_SPI_HPP_INCLUDED 22 #define ARM_CORTEX_STM32_COMMON_SPI_HPP_INCLUDED 24 #include <arch/rcc.hpp> 25 #include <arch/nvic.hpp> 26 #include <arch/reg/spi.hpp> 27 #include <type_traits> 31 template<
unsigned _spi_no,
typename system_clock_type >
36 static constexpr
unsigned spi_no = _spi_no;
38 using irq = irq::spi<spi_no>;
42 static constexpr
unsigned clk_freq = (spi_no == 1 ? system_clock_type::pclk2_freq : system_clock_type::pclk1_freq );
46 template<
unsigned value>
47 struct data_size_impl {
48 static_assert(value == 8 || value == 16,
"invalid data_size (supported values: 8, 16)");
49 using type =
regval<
typename SPIx::CR1::DFF,
51 value == 16 ? 1 : 0xff )
83 regval< typename SPIx::CR1::BIDIMODE, 0 >,
93 regval< typename SPIx::CR1::BIDIMODE, 1 >,
105 template<freq_t value>
108 value >= clk_freq / 2 ? 0 :
109 value >= clk_freq / 4 ? 1 :
110 value >= clk_freq / 8 ? 2 :
111 value >= clk_freq / 16 ? 3 :
112 value >= clk_freq / 32 ? 4 :
113 value >= clk_freq / 64 ? 5 :
114 value >= clk_freq / 128 ? 6 :
119 template<
unsigned value>
120 using data_size =
typename std::enable_if<
121 value == 8 || value == 16,
132 template<
typename... Tp >
134 reglist< Tp... >::template strict_reset_to<
144 template<
typename... Tp >
148 configure<
typename SPIx::CR1::SPE, Tp... >();
152 SPIx::CRCPR::reset();
156 SPIx::CR1::SPE::set();
159 SPIx::CR1::SPE::clear();
163 while(SPIx::SR::TXE::test() ==
false);
166 while(SPIx::SR::RXNE::test() ==
false);
169 while(SPIx::SR::BSY::test());
172 static void send(uint16_t data) {
173 SPIx::DR::store(data);
182 static typename SPIx::DR::value_type
receive(
void) {
183 return SPIx::DR::load();
201 #endif // ARM_CORTEX_STM32_COMMON_SPI_HPP_INCLUDED static void enable(void)
Definition: spi.hpp:155
rcc_spi_clock_resources< spi_no > resources
Definition: spi.hpp:40
static constexpr unsigned spi_no
Definition: spi.hpp:36
static void wait_receive_not_empty(void)
Definition: spi.hpp:165
regval< typename SPIx::CR1::BR,(value==0 ? 0 :value >=clk_freq/2 ? 0 :value >=clk_freq/4 ? 1 :value >=clk_freq/8 ? 2 :value >=clk_freq/16 ? 3 :value >=clk_freq/32 ? 4 :value >=clk_freq/64 ? 5 :value >=clk_freq/128 ? 6 :7) > max_frequency
Maximum frequency (hz, 0=maximum available)
Definition: spi.hpp:116
static void wait_not_busy(void)
Definition: spi.hpp:168
static void send_blocking(uint16_t data)
Definition: spi.hpp:176
static void configure(void)
Configure SPI register using Tp type traits.
Definition: spi.hpp:133
static void send(uint16_t data)
Definition: spi.hpp:172
static constexpr unsigned clk_freq
Definition: spi.hpp:42
SPI< spi_no > SPIx
Definition: spi.hpp:37
static SPIx::DR::value_type receive(void)
NOTE: return value depends on the data frame format (CR1::DFF, 8 or 16 bit)
Definition: spi.hpp:182
static unsigned char writeread_blocking(uint16_t data)
Definition: spi.hpp:191
static void disable(void)
Definition: spi.hpp:158
static void wait_transmit_empty(void)
Definition: spi.hpp:162
static void reset_crc(void)
Definition: spi.hpp:151
static SPIx::DR::value_type receive_blocking(void)
Definition: spi.hpp:186
static void reconfigure(void)
Disable SPI, configure SPI register using Tp type traits, and enable SPI.
Definition: spi.hpp:145
typename std::enable_if< value==8||value==16, regval< typename SPIx::CR1::DFF, value==16 ? 1 :0 > >::type data_size
8 bit or 16 bit data size
Definition: spi.hpp:123
irq::spi< spi_no > irq
Definition: spi.hpp:38