@@ -364,6 +364,17 @@ void ArduinoSPI::configSpiSettings(arduino::SPISettings const & settings)
364
364
configSpi (settings);
365
365
}
366
366
367
+
368
+ #define ONE_BIT 1
369
+ #define TWO_BITS 3
370
+
371
+ #define CPHA_POS 0
372
+ #define CPOL_POS 1
373
+ #define BRDV_POS 2
374
+
375
+ #define BIT_ORDER_POS 12
376
+
377
+
367
378
void ArduinoSPI::configSpi (arduino::SPISettings const & settings)
368
379
{
369
380
auto [clk_phase, clk_polarity, bit_order] = toFspSpiConfig (settings);
@@ -374,23 +385,35 @@ void ArduinoSPI::configSpi(arduino::SPISettings const & settings)
374
385
uint32_t spcmd0 = _spi_ctrl.p_regs ->SPCMD [0 ];
375
386
376
387
/* Configure CPHA setting. */
377
- spcmd0 |= (uint32_t ) clk_phase;
388
+ uint32_t mask = (ONE_BIT << CPHA_POS);
389
+ spcmd0 &= ~(uint32_t )mask;
390
+ spcmd0 |= (clk_phase << CPHA_POS);
378
391
379
392
/* Configure CPOL setting. */
380
- spcmd0 |= (uint32_t ) clk_polarity << 1 ;
393
+ mask = (ONE_BIT << CPOL_POS);
394
+ spcmd0 &= ~(uint32_t )mask;
395
+ spcmd0 |= ((uint32_t ) clk_polarity << CPOL_POS);
381
396
382
397
/* Configure Bit Order (MSB,LSB) */
383
- spcmd0 |= (uint32_t ) bit_order << 12 ;
398
+ mask = (ONE_BIT << BIT_ORDER_POS);
399
+ spcmd0 &= ~(uint32_t )mask;
400
+ spcmd0 |= ((uint32_t ) bit_order << BIT_ORDER_POS);
384
401
385
402
/* Configure the Bit Rate Division Setting */
386
- spcmd0 &= ~(((uint32_t ) 3 ) << 2 );
387
- spcmd0 |= (uint32_t ) spck_div.brdv << 2 ;
403
+ mask = (TWO_BITS << BRDV_POS);
404
+ spcmd0 &= ~(uint32_t )mask;
405
+ spcmd0 |= ((uint32_t ) spck_div.brdv << BRDV_POS);
388
406
389
407
/* Update settings. */
390
408
_spi_ctrl.p_regs ->SPCMD [0 ] = (uint16_t ) spcmd0;
391
409
_spi_ctrl.p_regs ->SPBR = (uint8_t ) spck_div.spbr ;
392
410
}
393
411
412
+ #define SCI_CKPH_POS 7
413
+ #define SCI_CKPOL_POS 6
414
+ #define SCI_DIRECTION_POS 3
415
+ #define SCI_CK_SEL_POS 0
416
+
394
417
void ArduinoSPI::configSpiSci (arduino::SPISettings const & settings)
395
418
{
396
419
auto [clk_phase, clk_polarity, bit_order] = toFspSpiConfig (settings);
@@ -403,16 +426,24 @@ void ArduinoSPI::configSpiSci(arduino::SPISettings const & settings)
403
426
uint32_t smr = R_SCI0_SMR_CM_Msk;
404
427
405
428
/* Configure CPHA setting. */
406
- spmr |= (uint32_t ) clk_phase << 7 ;
429
+ uint32_t mask = (ONE_BIT << SCI_CKPH_POS);
430
+ spmr &= ~(uint32_t )mask; // reset bit 0
431
+ spmr |= ((uint32_t ) clk_phase << SCI_CKPH_POS);
407
432
408
433
/* Configure CPOL setting. */
409
- spmr |= (uint32_t ) clk_polarity << 6 ;
434
+ mask = (ONE_BIT << SCI_CKPOL_POS);
435
+ spmr &= ~(uint32_t )mask; // reset bit 0
436
+ spmr |= ((uint32_t ) clk_polarity << SCI_CKPOL_POS);
410
437
411
438
/* Configure Bit Order (MSB,LSB) */
412
- scmr |= (uint32_t ) bit_order << 3 ;
439
+ mask = (ONE_BIT << SCI_DIRECTION_POS);
440
+ scmr &= ~(uint32_t )mask; // reset bit 0
441
+ scmr |= ((uint32_t ) bit_order << SCI_DIRECTION_POS);
413
442
414
443
/* Select the baud rate generator clock divider. */
415
- smr |= (uint32_t ) clk_div.cks ;
444
+ mask = (TWO_BITS << SCI_CK_SEL_POS);
445
+ smr &= ~(uint32_t )mask; // reset bit 0
446
+ smr |= ((uint32_t ) clk_div.cks << SCI_CK_SEL_POS);
416
447
417
448
/* Update settings. */
418
449
_spi_sci_ctrl.p_reg ->SMR = (uint8_t ) smr;
0 commit comments