Skip to content

Commit 791a345

Browse files
committed
STM32 Spi state as MODM_FLAGS8
1 parent 173a426 commit 791a345

File tree

5 files changed

+32
-29
lines changed

5 files changed

+32
-29
lines changed

src/modm/platform/spi/stm32/spi_base.hpp.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ public:
139139
QuarterFull = SPI_CR2_FRXTH,
140140
};
141141
%% endif
142+
enum State : uint8_t {
143+
// Word = Bit0, // 0: Byte (uint8_t) transaction, 1: Word (uint16_t) transaction
144+
// AutoIncr = Bit1, // 0: Always send first data of tx, 1: Send consecutive data of tx
145+
Idle = Bit2, // 1: Transaction is Running
146+
LowByte = Bit3, // 1: Low byte has been transmitted
147+
ByteHigh = Bit4, // 1: High byte of word has been transmitted
148+
};
149+
MODM_FLAGS8(State);
142150
};
143151

144152
} // namespace platform

src/modm/platform/spi/stm32/spi_master.cpp.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
// Bit0: single transfer state
2121
// Bit1: block transfer state
22-
uint8_t
22+
modm::platform::SpiBase::State_t
2323
modm::platform::SpiMaster{{ id }}::state(0);
2424

2525
std::size_t

src/modm/platform/spi/stm32/spi_master.hpp.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace platform
3939
*/
4040
class SpiMaster{{ id }} : public modm::SpiMaster
4141
{
42-
static uint8_t state;
42+
static SpiBase::State_t state;
4343
static std::size_t index;
4444

4545
static uint8_t count;
@@ -99,7 +99,7 @@ public:
9999

100100
// initialize the Spi
101101
SpiHal{{ id }}::initialize(prescaler);
102-
state = 0;
102+
state = SpiBase::State(0);
103103
}
104104

105105
static modm_always_inline void

src/modm/platform/spi/stm32/spi_master_dma_impl.hpp.in

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ modm::platform::SpiMaster{{ id }}_Dma<DmaChannelRx, DmaChannelTx>::transmit(T da
6161
// there are only two states encoded into 1 bit (LSB of state):
6262
// 1. waiting to start, and
6363
// 2. waiting to finish.
64-
// LSB != Bit0 ?
65-
if ( !(state & Bit0) )
64+
65+
if (!state.all(SpiBase::LowByte))
6666
{
6767
// disable DMA for single transfer
6868
SpiHal{{ id }}::disableInterrupt(SpiBase::Interrupt::TxDmaEnable |
@@ -76,7 +76,7 @@ modm::platform::SpiMaster{{ id }}_Dma<DmaChannelRx, DmaChannelTx>::transmit(T da
7676
SpiHal{{ id }}::write(data);
7777

7878
// set LSB = Bit0
79-
state |= Bit0;
79+
state.set(SpiBase::LowByte);
8080
}
8181

8282
if (!SpiHal{{ id }}::isReceiveRegisterNotEmpty())
@@ -85,7 +85,7 @@ modm::platform::SpiMaster{{ id }}_Dma<DmaChannelRx, DmaChannelTx>::transmit(T da
8585
SpiHal{{ id }}::read(data);
8686

8787
// transfer finished
88-
state &= ~Bit0;
88+
state.reset(SpiBase::LowByte);
8989
return {modm::rf::Stop, data};
9090
}
9191

@@ -101,12 +101,11 @@ modm::platform::SpiMaster{{ id }}_Dma<DmaChannelRx, DmaChannelTx>::transmit(cons
101101
// 1. initialize index, and
102102
// 2. wait for transfer to finish.
103103

104-
// we are only interested in Bit1
105-
switch(state & Bit1)
104+
switch(int(state.all(SpiBase::Idle)))
106105
{
107106
case 0:
108107
// we will only visit this state once
109-
state |= Bit1;
108+
state.set(SpiBase::Idle);
110109
dmaError = false;
111110

112111
SpiHal{{ id }}::enableInterrupt(SpiBase::Interrupt::TxDmaEnable |
@@ -145,8 +144,8 @@ modm::platform::SpiMaster{{ id }}_Dma<DmaChannelRx, DmaChannelTx>::transmit(cons
145144

146145
SpiHal{{ id }}::disableInterrupt(SpiBase::Interrupt::TxDmaEnable |
147146
SpiBase::Interrupt::RxDmaEnable);
148-
// clear the state
149-
state &= ~Bit1;
147+
148+
state.reset(SpiBase::Idle);
150149
return {modm::rf::Stop};
151150
}
152151
}
@@ -164,11 +163,11 @@ modm::platform::SpiMaster{{ id }}_Dma<DmaChannelRx, DmaChannelTx>::transmit(cons
164163
// 2. wait for transfer to finish.
165164

166165
// we are only interested in Bit1
167-
switch(state & Bit1)
166+
switch(int(state.all(SpiBase::Idle)))
168167
{
169168
case 0:
170169
// we will only visit this state once
171-
state |= Bit1;
170+
state.set(SpiBase::Idle);
172171
dmaError = false;
173172

174173
SpiHal{{ id }}::enableInterrupt(SpiBase::Interrupt::TxDmaEnable |
@@ -219,8 +218,8 @@ modm::platform::SpiMaster{{ id }}_Dma<DmaChannelRx, DmaChannelTx>::transmit(cons
219218

220219
SpiHal{{ id }}::disableInterrupt(SpiBase::Interrupt::TxDmaEnable |
221220
SpiBase::Interrupt::RxDmaEnable);
222-
// clear the state
223-
state &= ~Bit1;
221+
222+
state.reset(SpiBase::Idle);
224223
return {modm::rf::Stop};
225224
}
226225
}

src/modm/platform/spi/stm32/spi_master_impl.hpp.in

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ modm::platform::SpiMaster{{ id }}::transmit(T data)
2828
// 1. waiting to start, and
2929
// 2. waiting to finish.
3030

31-
// LSB != Bit0 ?
32-
if ( !(state & Bit0) )
31+
if (!state.all(SpiBase::LowByte))
3332
{
3433
// wait for previous transfer to finish
3534
if (!SpiHal{{ id }}::isTransmitRegisterEmpty())
@@ -38,8 +37,7 @@ modm::platform::SpiMaster{{ id }}::transmit(T data)
3837
// start transfer by copying data into register
3938
SpiHal{{ id }}::write(data);
4039

41-
// set LSB = Bit0
42-
state |= Bit0;
40+
state.set(SpiBase::LowByte);
4341
}
4442

4543
if (!SpiHal{{ id }}::isReceiveRegisterNotEmpty())
@@ -48,7 +46,7 @@ modm::platform::SpiMaster{{ id }}::transmit(T data)
4846
SpiHal{{ id }}::read(data);
4947

5048
// transfer finished
51-
state &= ~Bit0;
49+
state.reset(SpiBase::LowByte);
5250
return {modm::rf::Stop, data};
5351
}
5452

@@ -63,11 +61,11 @@ modm::platform::SpiMaster{{ id }}::transmit(const T *tx, const std::size_t repea
6361
// 2. wait for transfer to finish.
6462

6563
// we are only interested in Bit1
66-
switch(state & Bit1)
64+
switch(int(state.all(SpiBase::Idle)))
6765
{
6866
case 0:
6967
// we will only visit this state once
70-
state |= Bit1;
68+
state.set(SpiBase::Idle);
7169

7270
// initialize index and check range
7371
index = 0;
@@ -86,8 +84,7 @@ modm::platform::SpiMaster{{ id }}::transmit(const T *tx, const std::size_t repea
8684
index++;
8785
}
8886

89-
// clear the state
90-
state &= ~Bit1;
87+
state.reset(SpiBase::Idle);
9188
return {modm::rf::Stop};
9289
}
9390
}
@@ -102,12 +99,11 @@ modm::platform::SpiMaster{{ id }}::transmit(const T * tx, T * rx, const std::siz
10299
// 1. initialize index, and
103100
// 2. wait for transfer to finish.
104101

105-
// we are only interested in Bit1
106-
switch(state & Bit1)
102+
switch(int(state.all(SpiBase::Idle)))
107103
{
108104
case 0:
109105
// we will only visit this state once
110-
state |= Bit1;
106+
state.set(SpiBase::Idle);
111107

112108
// initialize index and check range
113109
index = 0;
@@ -131,7 +127,7 @@ modm::platform::SpiMaster{{ id }}::transmit(const T * tx, T * rx, const std::siz
131127
}
132128

133129
// clear the state
134-
state &= ~Bit1;
130+
state.reset(SpiBase::Idle);
135131
return {modm::rf::Stop};
136132
}
137133
}

0 commit comments

Comments
 (0)