55 * Copyright (c) 2012-2017, Niklas Hauser
66 * Copyright (c) 2013, Kevin Läufer
77 * Copyright (c) 2014, Sascha Schade
8+ * Copyright (c) 2021, Thomas Sommer
89 *
910 * This file is part of the modm project.
1011 *
@@ -24,6 +25,9 @@ modm::platform::SpiMaster{{ id }}::state(0);
2425uint8_t
2526modm::platform::SpiMaster{{ id }}::count(0);
2627
28+ std::size_t
29+ modm::platform::SpiMaster{{ id }}::index(0);
30+
2731void *
2832modm::platform::SpiMaster{{ id }}::context(nullptr);
2933
@@ -61,85 +65,4 @@ modm::platform::SpiMaster{{ id }}::release(void *ctx)
6165 context = nullptr;
6266 }
6367 return count;
64- }
65- // ----------------------------------------------------------------------------
66-
67- modm::ResumableResult<uint8_t>
68- modm::platform::SpiMaster{{ id }}::transfer(uint8_t data)
69- {
70- // this is a manually implemented "fast resumable function"
71- // there is no context or nesting protection, since we don't need it.
72- // there are only two states encoded into 1 bit (LSB of state):
73- // 1. waiting to start, and
74- // 2. waiting to finish.
75-
76- // LSB != Bit0 ?
77- if ( !(state & Bit0) )
78- {
79- // wait for previous transfer to finish
80- if (!SpiHal{{ id }}::isTransmitRegisterEmpty())
81- return {modm::rf::Running};
82-
83- // start transfer by copying data into register
84- SpiHal{{ id }}::write(data);
85-
86- // set LSB = Bit0
87- state |= Bit0;
88- }
89-
90- if (!SpiHal{{ id }}::isReceiveRegisterNotEmpty())
91- return {modm::rf::Running};
92-
93- SpiHal{{ id }}::read(data);
94-
95- // transfer finished
96- state &= ~Bit0;
97- return {modm::rf::Stop, data};
98- }
99-
100- modm::ResumableResult<void>
101- modm::platform::SpiMaster{{ id }}::transfer(
102- const uint8_t * tx, uint8_t * rx, std::size_t length)
103- {
104- // this is a manually implemented "fast resumable function"
105- // there is no context or nesting protection, since we don't need it.
106- // there are only two states encoded into 1 bit (Bit1 of state):
107- // 1. initialize index, and
108- // 2. wait for 1-byte transfer to finish.
109-
110- // we need to globally remember which byte we are currently transferring
111- static std::size_t index = 0;
112-
113- // we are only interested in Bit1
114- switch(state & Bit1)
115- {
116- case 0:
117- // we will only visit this state once
118- state |= Bit1;
119-
120- // initialize index and check range
121- index = 0;
122- while (index < length)
123- {
124- default:
125- {
126- // if tx == 0, we use a dummy byte 0x00
127- // else we copy it from the array
128- // call the resumable function
129- modm::ResumableResult<uint8_t> result = transfer(tx ? tx[index] : 0);
130-
131- // if the resumable function is still running, so are we
132- if (result.getState() > modm::rf::NestingError)
133- return {modm::rf::Running};
134-
135- // if rx != 0, we copy the result into the array
136- if (rx) rx[index] = result.getResult();
137- }
138- index++;
139- }
140-
141- // clear the state
142- state &= ~Bit1;
143- return {modm::rf::Stop};
144- }
145- }
68+ }
0 commit comments