Skip to content

Commit 33d8348

Browse files
committed
- changes as requested for the pull-request
1 parent 7df2144 commit 33d8348

File tree

1 file changed

+38
-55
lines changed

1 file changed

+38
-55
lines changed

libraries/SPI/SPI.cpp

Lines changed: 38 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -164,25 +164,8 @@ void ArduinoSPI::begin()
164164
init_ok = false;
165165
}
166166
}
167-
else
168-
{
169-
#if 0
170-
/* not using FSP for SPI anymore and no interrupts */
171-
SpiMasterIrqReq_t irq_req
172-
{
173-
.ctrl = &_spi_ctrl,
174-
.cfg = &_spi_cfg,
175-
.hw_channel = (uint8_t)_channel,
176-
};
177-
init_ok &= IRQManager::getInstance().addPeripheral(IRQ_SPI_MASTER, &irq_req);
178167

179-
if (FSP_SUCCESS == _open(&_spi_ctrl, &_spi_cfg)) {
180-
init_ok &= true;
181-
} else {
182-
init_ok = false;
183-
}
184-
#endif
185-
}
168+
/* not using FSP for SPI anymore and no interrupts */
186169

187170
_is_initialized = init_ok;
188171
}
@@ -229,17 +212,17 @@ uint8_t ArduinoSPI::transfer(uint8_t data)
229212

230213
uint16_t ArduinoSPI::transfer16(uint16_t data)
231214
{
232-
union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } t;
233-
t.val = data;
215+
union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } t;
216+
t.val = data;
234217

235-
if (_settings.getBitOrder() == LSBFIRST) {
236-
t.lsb = transfer(t.lsb);
237-
t.msb = transfer(t.msb);
238-
} else {
239-
t.msb = transfer(t.msb);
240-
t.lsb = transfer(t.lsb);
241-
}
242-
return t.val;
218+
if (_settings.getBitOrder() == LSBFIRST) {
219+
t.lsb = transfer(t.lsb);
220+
t.msb = transfer(t.msb);
221+
} else {
222+
t.msb = transfer(t.msb);
223+
t.lsb = transfer(t.lsb);
224+
}
225+
return t.val;
243226
}
244227

245228
void ArduinoSPI::transfer(void *buf, size_t count)
@@ -263,8 +246,8 @@ void ArduinoSPI::transfer(void *buf, size_t count)
263246
{
264247
if (buf != NULL) {
265248
uint32_t *buffer32 = (uint32_t *) buf;
266-
size_t ir = 0;
267-
size_t it = 0;
249+
size_t index_rx = 0;
250+
size_t index_tx = 0;
268251
size_t const n32 = count / 4U;
269252
uint8_t const bytes_remaining = (uint8_t) (count & 3U);
270253

@@ -274,28 +257,28 @@ void ArduinoSPI::transfer(void *buf, size_t count)
274257
_spi_ctrl.p_regs->SPCMD_b[0].SPB = 2; /* spi bit width = 32 */
275258
_spi_ctrl.p_regs->SPCR_b.SPE = 1; /* enable SPI unit */
276259

277-
while ((it < 2U) && (it < n32)) {
260+
while ((index_tx < 2U) && (index_tx < n32)) {
278261
if (_spi_ctrl.p_regs->SPSR_b.SPTEF) {
279-
_spi_ctrl.p_regs->SPDR = buffer32[it];
280-
it++;
262+
_spi_ctrl.p_regs->SPDR = buffer32[index_tx];
263+
index_tx++;
281264
}
282265
}
283266

284-
while (it < n32) {
267+
while (index_tx < n32) {
285268
if (_spi_ctrl.p_regs->SPSR_b.SPRF) {
286269
uint32_t tmp = _spi_ctrl.p_regs->SPDR;
287-
_spi_ctrl.p_regs->SPDR = buffer32[it];
288-
buffer32[ir] = tmp;
289-
ir++;
290-
it++;
270+
_spi_ctrl.p_regs->SPDR = buffer32[index_tx];
271+
buffer32[index_rx] = tmp;
272+
index_rx++;
273+
index_tx++;
291274
}
292275
}
293276

294-
while (ir < n32) { /* collect the last word received */
277+
while (index_rx < n32) { /* collect the last word received */
295278
if (_spi_ctrl.p_regs->SPSR_b.SPRF) {
296279
uint32_t tmp = _spi_ctrl.p_regs->SPDR;
297-
buffer32[ir] = tmp;
298-
ir++;
280+
buffer32[index_rx] = tmp;
281+
index_rx++;
299282
}
300283
}
301284

@@ -306,7 +289,7 @@ void ArduinoSPI::transfer(void *buf, size_t count)
306289
}
307290

308291
/* send the remaining bytes with 8-bit transfers */
309-
uint8_t *buffer = (uint8_t *) &buffer32[ir];
292+
uint8_t *buffer = (uint8_t *) &buffer32[index_rx];
310293

311294
for (uint8_t index = 0; index < bytes_remaining; index++) {
312295
_spi_ctrl.p_regs->SPDR_BY = buffer[index];
@@ -344,35 +327,35 @@ void ArduinoSPI::transfer(void *buf, void *rxbuf, size_t count)
344327

345328
const uint32_t* tx32 = (const uint32_t *) buf;
346329
uint32_t* rx32 = (uint32_t *) rxbuf;
347-
size_t ir = 0;
348-
size_t it = 0;
330+
size_t index_rx = 0;
331+
size_t index_tx = 0;
349332

350-
while ((it < 2U) && (it < n32)) {
333+
while ((index_tx < 2U) && (index_tx < n32)) {
351334
if (_spi_ctrl.p_regs->SPSR_b.SPTEF) {
352-
_spi_ctrl.p_regs->SPDR = (buf != NULL) ? tx32[it] : 0xFFFFFFFF;
353-
it++;
335+
_spi_ctrl.p_regs->SPDR = (buf != NULL) ? tx32[index_tx] : 0xFFFFFFFF;
336+
index_tx++;
354337
}
355338
}
356339

357-
while (it < n32) {
340+
while (index_tx < n32) {
358341
if (_spi_ctrl.p_regs->SPSR_b.SPRF) {
359342
uint32_t tmp = _spi_ctrl.p_regs->SPDR;
360-
_spi_ctrl.p_regs->SPDR = (buf != NULL) ? tx32[it] : 0xFFFFFFFF;
343+
_spi_ctrl.p_regs->SPDR = (buf != NULL) ? tx32[index_tx] : 0xFFFFFFFF;
361344
if (rxbuf != NULL) {
362-
rx32[ir] = tmp;
345+
rx32[index_rx] = tmp;
363346
}
364-
ir++;
365-
it++;
347+
index_rx++;
348+
index_tx++;
366349
}
367350
}
368351

369-
while (ir < n32) { /* collect the last word received */
352+
while (index_rx < n32) { /* collect the last word received */
370353
if (_spi_ctrl.p_regs->SPSR_b.SPRF) {
371354
uint32_t tmp = _spi_ctrl.p_regs->SPDR;
372355
if (rxbuf != NULL) {
373-
rx32[ir] = tmp;
356+
rx32[index_rx] = tmp;
374357
}
375-
ir++;
358+
index_rx++;
376359
}
377360
}
378361

0 commit comments

Comments
 (0)