@@ -227,74 +227,76 @@ uint16_t ArduinoSPI::transfer16(uint16_t data)
227227
228228void ArduinoSPI::transfer (void *buf, size_t count)
229229{
230- if (buf != NULL ) {
231- if (_is_sci) {
232- _spi_cb_event[_cb_event_idx] = SPI_EVENT_TRANSFER_ABORTED;
230+ if (NULL == buf) {
231+ return ;
232+ }
233+
234+ if (_is_sci) {
235+ _spi_cb_event[_cb_event_idx] = SPI_EVENT_TRANSFER_ABORTED;
233236
234- _write_then_read (&_spi_sci_ctrl, buf, buf, count, SPI_BIT_WIDTH_8_BITS);
237+ _write_then_read (&_spi_sci_ctrl, buf, buf, count, SPI_BIT_WIDTH_8_BITS);
235238
236- for (auto const start = millis ();
237- (SPI_EVENT_TRANSFER_COMPLETE != _spi_cb_event[_cb_event_idx]) && (millis () - start < 1000 ); )
238- {
239- __NOP ();
240- }
241- if (SPI_EVENT_TRANSFER_ABORTED == _spi_cb_event[_cb_event_idx])
242- {
243- end ();
244- }
239+ for (auto const start = millis ();
240+ (SPI_EVENT_TRANSFER_COMPLETE != _spi_cb_event[_cb_event_idx]) && (millis () - start < 1000 ); )
241+ {
242+ __NOP ();
245243 }
246- else {
247- uint32_t *buffer32 = (uint32_t *) buf;
248- size_t index_rx = 0 ;
249- size_t index_tx = 0 ;
250- size_t const n32 = count / 4U ;
251- uint8_t const bytes_remaining = (uint8_t ) (count & 3U );
252-
253- if (n32 != 0U ) {
254- _spi_ctrl.p_regs ->SPCR_b .SPE = 0 ; /* disable SPI unit */
255- _spi_ctrl.p_regs ->SPDCR = R_SPI0_SPDCR_SPLW_Msk; /* SPI word access */
256- _spi_ctrl.p_regs ->SPCMD_b [0 ].SPB = 2 ; /* spi bit width = 32 */
257- _spi_ctrl.p_regs ->SPCR_b .SPE = 1 ; /* enable SPI unit */
258-
259- while ((index_tx < 2U ) && (index_tx < n32)) {
260- if (_spi_ctrl.p_regs ->SPSR_b .SPTEF ) {
261- _spi_ctrl.p_regs ->SPDR = buffer32[index_tx];
262- index_tx++;
263- }
244+ if (SPI_EVENT_TRANSFER_ABORTED == _spi_cb_event[_cb_event_idx])
245+ {
246+ end ();
247+ }
248+ }
249+ else {
250+ uint32_t *buffer32 = (uint32_t *) buf;
251+ size_t index_rx = 0 ;
252+ size_t index_tx = 0 ;
253+ size_t const n32 = count / 4U ;
254+ uint8_t const bytes_remaining = (uint8_t ) (count & 3U );
255+
256+ if (n32 != 0U ) {
257+ _spi_ctrl.p_regs ->SPCR_b .SPE = 0 ; /* disable SPI unit */
258+ _spi_ctrl.p_regs ->SPDCR = R_SPI0_SPDCR_SPLW_Msk; /* SPI word access */
259+ _spi_ctrl.p_regs ->SPCMD_b [0 ].SPB = 2 ; /* spi bit width = 32 */
260+ _spi_ctrl.p_regs ->SPCR_b .SPE = 1 ; /* enable SPI unit */
261+
262+ while ((index_tx < 2U ) && (index_tx < n32)) {
263+ if (_spi_ctrl.p_regs ->SPSR_b .SPTEF ) {
264+ _spi_ctrl.p_regs ->SPDR = buffer32[index_tx];
265+ index_tx++;
264266 }
267+ }
265268
266- while (index_tx < n32) {
267- if (_spi_ctrl.p_regs ->SPSR_b .SPRF ) {
268- uint32_t tmp = _spi_ctrl.p_regs ->SPDR ;
269- _spi_ctrl.p_regs ->SPDR = buffer32[index_tx];
270- buffer32[index_rx] = tmp;
271- index_rx++;
272- index_tx++;
273- }
269+ while (index_tx < n32) {
270+ if (_spi_ctrl.p_regs ->SPSR_b .SPRF ) {
271+ uint32_t tmp = _spi_ctrl.p_regs ->SPDR ;
272+ _spi_ctrl.p_regs ->SPDR = buffer32[index_tx];
273+ buffer32[index_rx] = tmp;
274+ index_rx++;
275+ index_tx++;
274276 }
277+ }
275278
276- while (index_rx < n32) { /* collect the last word received */
277- if (_spi_ctrl.p_regs ->SPSR_b .SPRF ) {
278- uint32_t tmp = _spi_ctrl.p_regs ->SPDR ;
279- buffer32[index_rx] = tmp;
280- index_rx++;
281- }
279+ while (index_rx < n32) { /* collect the last word received */
280+ if (_spi_ctrl.p_regs ->SPSR_b .SPRF ) {
281+ uint32_t tmp = _spi_ctrl.p_regs ->SPDR ;
282+ buffer32[index_rx] = tmp;
283+ index_rx++;
282284 }
283-
284- _spi_ctrl.p_regs ->SPCR_b .SPE = 0 ; /* disable SPI unit */
285- _spi_ctrl.p_regs ->SPDCR = R_SPI0_SPDCR_SPBYT_Msk; /* SPI byte access */
286- _spi_ctrl.p_regs ->SPCMD_b [0 ].SPB = 7 ; /* spi bit width = 8 */
287- _spi_ctrl.p_regs ->SPCR_b .SPE = 1 ; /* enable SPI unit */
288285 }
289286
290- /* send the remaining bytes with 8-bit transfers */
291- uint8_t *buffer = (uint8_t *) &buffer32[index_rx];
287+ _spi_ctrl.p_regs ->SPCR_b .SPE = 0 ; /* disable SPI unit */
288+ _spi_ctrl.p_regs ->SPDCR = R_SPI0_SPDCR_SPBYT_Msk; /* SPI byte access */
289+ _spi_ctrl.p_regs ->SPCMD_b [0 ].SPB = 7 ; /* spi bit width = 8 */
290+ _spi_ctrl.p_regs ->SPCR_b .SPE = 1 ; /* enable SPI unit */
291+ }
292292
293- for (uint8_t index = 0 ; index < bytes_remaining; index++) {
294- _spi_ctrl.p_regs ->SPDR_BY = buffer[index];
295- while (0U == _spi_ctrl.p_regs ->SPSR_b .SPRF ) {}
296- buffer[index] = _spi_ctrl.p_regs ->SPDR_BY ;
297- }
293+ /* send the remaining bytes with 8-bit transfers */
294+ uint8_t *buffer = (uint8_t *) &buffer32[index_rx];
295+
296+ for (uint8_t index = 0 ; index < bytes_remaining; index++) {
297+ _spi_ctrl.p_regs ->SPDR_BY = buffer[index];
298+ while (0U == _spi_ctrl.p_regs ->SPSR_b .SPRF ) {}
299+ buffer[index] = _spi_ctrl.p_regs ->SPDR_BY ;
298300 }
299301 }
300302}
0 commit comments