Skip to content

Commit 8499f96

Browse files
committed
- first draft for a much fast SPI class
1 parent 89ab55b commit 8499f96

File tree

1 file changed

+102
-2
lines changed

1 file changed

+102
-2
lines changed

libraries/SPI/SPI.cpp

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ void ArduinoSPI::begin()
147147

148148
configSpiSettings(DEFAULT_SPI_SETTINGS);
149149

150+
#if 0
150151
/* Configure the Interrupt Controller. */
151152
if (_is_sci)
152153
{
@@ -180,6 +181,7 @@ void ArduinoSPI::begin()
180181
init_ok = false;
181182
}
182183
}
184+
#endif
183185

184186
_is_initialized = init_ok;
185187
}
@@ -197,7 +199,8 @@ void ArduinoSPI::end()
197199
uint8_t ArduinoSPI::transfer(uint8_t data)
198200
{
199201
uint8_t rxbuf;
200-
_spi_cb_event[_cb_event_idx] = SPI_EVENT_TRANSFER_ABORTED;
202+
#if 0
203+
_spi_cb_event[_cb_event_idx] = SPI_EVENT_TRANSFER_ABORTED;
201204
if (_is_sci) {
202205
_write_then_read(&_spi_sci_ctrl, &data, &rxbuf, 1, SPI_BIT_WIDTH_8_BITS);
203206
} else {
@@ -214,6 +217,16 @@ uint8_t ArduinoSPI::transfer(uint8_t data)
214217
end();
215218
return 0;
216219
}
220+
#endif
221+
222+
#if 1
223+
_spi_ctrl.p_regs->SPDR_BY = data;
224+
// while (0 == _spi_ctrl.p_regs->SPSR_b.SPTEF) {}
225+
// while (_spi_ctrl.p_regs->SPSR_b.IDLNF) {}
226+
while (0 == _spi_ctrl.p_regs->SPSR_b.SPRF) {}
227+
rxbuf = _spi_ctrl.p_regs->SPDR_BY;
228+
#endif
229+
217230
return rxbuf;
218231
}
219232

@@ -234,6 +247,7 @@ uint16_t ArduinoSPI::transfer16(uint16_t data)
234247

235248
void ArduinoSPI::transfer(void *buf, size_t count)
236249
{
250+
#if 0
237251
_spi_cb_event[_cb_event_idx] = SPI_EVENT_TRANSFER_ABORTED;
238252

239253
if (_is_sci) {
@@ -251,6 +265,19 @@ void ArduinoSPI::transfer(void *buf, size_t count)
251265
{
252266
end();
253267
}
268+
#endif
269+
270+
#if 1
271+
uint8_t *buffer = (uint8_t *) buf;
272+
273+
for(size_t index = 0; index < count; index++)
274+
{
275+
_spi_ctrl.p_regs->SPDR_BY = buffer[index];
276+
while (0 == _spi_ctrl.p_regs->SPSR_b.SPRF) {}
277+
buffer[index] = _spi_ctrl.p_regs->SPDR_BY;
278+
}
279+
while (_spi_ctrl.p_regs->SPSR_b.IDLNF) {}
280+
#endif
254281
}
255282

256283
void ArduinoSPI::beginTransaction(arduino::SPISettings settings)
@@ -366,6 +393,7 @@ void ArduinoSPI::configSpiSettings(arduino::SPISettings const & settings)
366393

367394
void ArduinoSPI::configSpi(arduino::SPISettings const & settings)
368395
{
396+
#if 0
369397
auto [clk_phase, clk_polarity, bit_order] = toFspSpiConfig(settings);
370398

371399
rspck_div_setting_t spck_div = _spi_ext_cfg.spck_div;
@@ -383,12 +411,84 @@ void ArduinoSPI::configSpi(arduino::SPISettings const & settings)
383411
spcmd0 |= (uint32_t) bit_order << 12;
384412

385413
/* Configure the Bit Rate Division Setting */
386-
spcmd0 &= ~(((uint32_t) 3) << 2);
414+
spcmd0 &= !(((uint32_t)0xFF) << 2);
387415
spcmd0 |= (uint32_t) spck_div.brdv << 2;
388416

389417
/* Update settings. */
390418
_spi_ctrl.p_regs->SPCMD[0] = (uint16_t) spcmd0;
391419
_spi_ctrl.p_regs->SPBR = (uint8_t) spck_div.spbr;
420+
#endif
421+
422+
#if 1
423+
/** SPI base register access macro. */
424+
#define SPI_REG(channel) ((R_SPI0_Type *) ((uint32_t) R_SPI0 + \
425+
((uint32_t) R_SPI1 - (uint32_t) R_SPI0) * \
426+
(channel)))
427+
428+
_spi_ctrl.p_cfg = &_spi_cfg;
429+
_spi_ctrl.p_callback = _spi_cfg.p_callback;
430+
_spi_ctrl.p_context = _spi_cfg.p_context;
431+
_spi_ctrl.p_callback_memory = NULL;
432+
_spi_ctrl.p_regs = SPI_REG(_spi_ctrl.p_cfg->channel);
433+
434+
auto [clk_phase, clk_polarity, bit_order] = toFspSpiConfig(settings);
435+
436+
rspck_div_setting_t spck_div = _spi_ext_cfg.spck_div;
437+
R_SPI_CalculateBitrate(settings.getClockFreq(), &spck_div);
438+
439+
uint32_t spcmd0 = 0;
440+
uint32_t spcr = 0;
441+
uint32_t sslp = 0;
442+
uint32_t sppcr = 0;
443+
uint32_t spcr2 = 0;
444+
uint32_t spckd = 0;
445+
uint32_t sslnd = 0;
446+
uint32_t spnd = 0;
447+
448+
spcmd0 |= (uint32_t) clk_phase; /* Configure CPHA setting. */
449+
spcmd0 |= (uint32_t) clk_polarity << 1; /* Configure CPOL setting. */
450+
spcmd0 |= (uint32_t) spck_div.brdv << 2; /* Configure the Bit Rate Division Setting */
451+
spcmd0 |= (uint32_t) SPI_BIT_WIDTH_8_BITS << 8; /* Configure 8 bit data width */
452+
spcmd0 |= (uint32_t) bit_order << 12; /* Configure Bit Order (MSB,LSB) */
453+
454+
/* TXMD = 0 -> full duplex, SPxIE = 0 -> no interrupts */
455+
spcr |= R_SPI0_SPCR_SPMS_Msk; /* configure 3-Wire Mode */
456+
if(SPI_MODE_MASTER == _spi_cfg.operating_mode)
457+
{
458+
spcr |= R_SPI0_SPCR_MSTR_Msk;
459+
spcr2 |= R_SPI0_SPCR2_SCKASE_Msk;
460+
}
461+
462+
/* Configure SSLn polarity setting. */
463+
sslp |= (uint32_t) _spi_ext_cfg.ssl_polarity << _spi_ext_cfg.ssl_select;
464+
465+
/* set MOSI idle value to low */
466+
sppcr |= R_SPI0_SPPCR_MOIFE_Msk;
467+
468+
/* Power up the SPI module. */
469+
R_BSP_MODULE_START(FSP_IP_SPI, _spi_cfg.channel);
470+
471+
/* Write registers */
472+
_spi_ctrl.p_regs->SPCR = (uint8_t) spcr;
473+
_spi_ctrl.p_regs->SSLP = (uint8_t) sslp;
474+
_spi_ctrl.p_regs->SPPCR = (uint8_t) sppcr;
475+
_spi_ctrl.p_regs->SPCKD = (uint8_t) spckd;
476+
_spi_ctrl.p_regs->SSLND = (uint8_t) sslnd;
477+
_spi_ctrl.p_regs->SPND = (uint8_t) spnd;
478+
_spi_ctrl.p_regs->SPCR2 = (uint8_t) spcr2;
479+
480+
_spi_ctrl.p_regs->SPCMD[0] = (uint16_t) spcmd0;
481+
_spi_ctrl.p_regs->SPBR = (uint8_t) spck_div.spbr;
482+
483+
_spi_ctrl.p_regs->SPDCR_b.SPBYT = 1; /* SPI byte access */
484+
485+
_spi_ctrl.p_regs->SPSR; /* read to clear OVRF */
486+
_spi_ctrl.p_regs->SPSR = 0; /* clear status register */
487+
488+
_spi_ctrl.p_regs->SPCR_b.SPE = 1; /* enable SPI unit */
489+
490+
_spi_ctrl.open = (0x52535049ULL); /* "SPI" in ASCII, used to determine if channel is open. */
491+
#endif
392492
}
393493

394494
void ArduinoSPI::configSpiSci(arduino::SPISettings const & settings)

0 commit comments

Comments
 (0)