@@ -259,14 +259,14 @@ void ArduinoSPI::transfer(void *buf, size_t count)
259
259
}
260
260
else
261
261
{
262
- if (buf) {
262
+ if (buf != NULL ) {
263
263
uint32_t *buffer32 = (uint32_t *) buf;
264
264
size_t ir = 0 ;
265
265
size_t it = 0 ;
266
- size_t n32 = count / 4U ;
267
- count &= 3U ;
266
+ size_t const n32 = count / 4U ;
267
+ uint8_t const bytes_remaining = ( uint8_t ) ( count & 3U ) ;
268
268
269
- if (n32) {
269
+ if (n32 != 0U ) {
270
270
_spi_ctrl.p_regs ->SPCR_b .SPE = 0 ; /* disable SPI unit */
271
271
_spi_ctrl.p_regs ->SPDCR = R_SPI0_SPDCR_SPLW_Msk; /* SPI word access */
272
272
_spi_ctrl.p_regs ->SPCMD_b [0 ].SPB = 2 ; /* spi bit width = 32 */
@@ -303,14 +303,13 @@ void ArduinoSPI::transfer(void *buf, size_t count)
303
303
_spi_ctrl.p_regs ->SPCR_b .SPE = 1 ; /* enable SPI unit */
304
304
}
305
305
306
+ /* send the remaining bytes with 8-bit transfers */
306
307
uint8_t *buffer = (uint8_t *) &buffer32[ir];
307
308
308
- /* send the remaining bytes with 8-bit transfers */
309
- for (; count > 0U ; count--) {
310
- _spi_ctrl.p_regs ->SPDR_BY = buffer[0 ];
309
+ for (uint8_t index = 0 ; index < bytes_remaining; index++) {
310
+ _spi_ctrl.p_regs ->SPDR_BY = buffer[index];
311
311
while (0U == _spi_ctrl.p_regs ->SPSR_b .SPRF ) {}
312
- buffer[0 ] = _spi_ctrl.p_regs ->SPDR_BY ;
313
- buffer++;
312
+ buffer[index] = _spi_ctrl.p_regs ->SPDR_BY ;
314
313
}
315
314
}
316
315
}
@@ -335,7 +334,7 @@ void ArduinoSPI::transfer(void *buf, void *rxbuf, size_t count)
335
334
}
336
335
else {
337
336
size_t n32 = count / 4U ;
338
- if (n32) {
337
+ if (n32 != 0U ) {
339
338
_spi_ctrl.p_regs ->SPCR_b .SPE = 0 ; /* disable SPI unit */
340
339
_spi_ctrl.p_regs ->SPDCR = R_SPI0_SPDCR_SPLW_Msk; /* SPI word access */
341
340
_spi_ctrl.p_regs ->SPCMD_b [0 ].SPB = 2 ; /* spi bit width = 32 */
@@ -348,16 +347,16 @@ void ArduinoSPI::transfer(void *buf, void *rxbuf, size_t count)
348
347
349
348
while ((it < 2U ) && (it < n32)) {
350
349
if (_spi_ctrl.p_regs ->SPSR_b .SPTEF ) {
351
- _spi_ctrl.p_regs ->SPDR = (buf) ? tx32[it] : 0xFFFFFFFF ;
350
+ _spi_ctrl.p_regs ->SPDR = (buf != NULL ) ? tx32[it] : 0xFFFFFFFF ;
352
351
it++;
353
352
}
354
353
}
355
354
356
355
while (it < n32) {
357
356
if (_spi_ctrl.p_regs ->SPSR_b .SPRF ) {
358
357
uint32_t tmp = _spi_ctrl.p_regs ->SPDR ;
359
- _spi_ctrl.p_regs ->SPDR = (buf) ? tx32[it] : 0xFFFFFFFF ;
360
- if (rxbuf) {
358
+ _spi_ctrl.p_regs ->SPDR = (buf != NULL ) ? tx32[it] : 0xFFFFFFFF ;
359
+ if (rxbuf != NULL ) {
361
360
rx32[ir] = tmp;
362
361
}
363
362
ir++;
@@ -368,7 +367,7 @@ void ArduinoSPI::transfer(void *buf, void *rxbuf, size_t count)
368
367
while (ir < n32) { /* collect the last word received */
369
368
if (_spi_ctrl.p_regs ->SPSR_b .SPRF ) {
370
369
uint32_t tmp = _spi_ctrl.p_regs ->SPDR ;
371
- if (rxbuf) {
370
+ if (rxbuf != NULL ) {
372
371
rx32[ir] = tmp;
373
372
}
374
373
ir++;
@@ -386,10 +385,10 @@ void ArduinoSPI::transfer(void *buf, void *rxbuf, size_t count)
386
385
uint8_t *rx = (uint8_t *) rxbuf;
387
386
const uint8_t * tx = (const uint8_t *) buf;
388
387
for (size_t i = 4U * n32; i < count; i++) {
389
- _spi_ctrl.p_regs ->SPDR_BY = (buf) ? tx[i] : 0xFF ;
388
+ _spi_ctrl.p_regs ->SPDR_BY = (buf != NULL ) ? tx[i] : 0xFF ;
390
389
while (0U == _spi_ctrl.p_regs ->SPSR_b .SPRF ) {}
391
390
uint8_t tmp = _spi_ctrl.p_regs ->SPDR_BY ;
392
- if (rxbuf) {
391
+ if (rxbuf != NULL ) {
393
392
rx[i] = tmp;
394
393
}
395
394
}
0 commit comments