@@ -160,7 +160,7 @@ void SPIClass::begin(void) {
160160void SPIClass::beginSlave (void ) {
161161 spi_init (_currentSetting->spi_d );
162162 configure_gpios (_currentSetting->spi_d , 0 );
163- uint32 flags = ((_currentSetting->bitOrder == MSBFIRST ? SPI_FRAME_MSB : SPI_FRAME_LSB) | _currentSetting->dataSize | SPI_SW_SLAVE );
163+ uint32 flags = ((_currentSetting->bitOrder == MSBFIRST ? SPI_FRAME_MSB : SPI_FRAME_LSB) | _currentSetting->dataSize | SPI_RX_ONLY );
164164 #ifdef SPI_DEBUG
165165 Serial.print (" spi_slave_enable(" ); Serial.print (_currentSetting->dataMode ); Serial.print (" ," ); Serial.print (flags); Serial.println (" )" );
166166 #endif
@@ -306,10 +306,10 @@ void SPIClass::endTransaction(void)
306306 * I/O
307307 */
308308
309- uint8 SPIClass::read (void )
309+ uint16 SPIClass::read (void )
310310{
311311 while ( spi_is_rx_nonempty (_currentSetting->spi_d )==0 ) ;
312- return (uint8 )spi_rx_reg (_currentSetting->spi_d );
312+ return (uint16 )spi_rx_reg (_currentSetting->spi_d );
313313}
314314
315315void SPIClass::read (uint8 *buf, uint32 len)
@@ -321,7 +321,7 @@ void SPIClass::read(uint8 *buf, uint32 len)
321321 while ( rxed < len) {
322322 regs->DR = 0x00FF ; // " write the data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
323323 while ( (regs->SR & SPI_SR_RXNE)==0 ) ; // wait till data is available in the Rx register
324- buf[rxed++] = (uint8) (regs->DR ); // read and store the received byte
324+ * buf++ = (regs->DR ); // read and store the received byte
325325 }
326326}
327327
@@ -332,11 +332,21 @@ void SPIClass::write(uint16 data)
332332 * by taking the Tx code from transfer(byte)
333333 * This almost doubles the speed of this function.
334334 */
335- spi_tx_reg (_currentSetting->spi_d , data); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
336- while (spi_is_tx_empty (_currentSetting->spi_d ) == 0 ); // "5. Wait until TXE=1 ..."
335+ spi_tx_reg (_currentSetting->spi_d , data); // write the data to be transmitted into the SPI_DR register (this clears the TXE flag)
337336 while (spi_is_busy (_currentSetting->spi_d ) != 0 ); // "... and then wait until BSY=0 before disabling the SPI."
338337}
339338
339+ void SPIClass::write (uint16 data, uint32 n)
340+ {
341+ // Added by stevstrong: Repeatedly send same data by the specified number of times
342+ spi_reg_map * regs = _currentSetting->spi_d ->regs ;
343+ while ( (n--)>0 ) {
344+ regs->DR = data; // write the data to be transmitted into the SPI_DR register (this clears the TXE flag)
345+ while ( (regs->SR & SPI_SR_TXE)==0 ) ; // wait till Tx empty
346+ }
347+ while ( (regs->SR & SPI_SR_BSY) != 0 ); // wait until BSY=0 before returning
348+ }
349+
340350void SPIClass::write (const void *data, uint32 length)
341351{
342352 spi_tx (_currentSetting->spi_d , data, length); // data can be array of bytes or words
@@ -346,18 +356,18 @@ void SPIClass::write(const void *data, uint32 length)
346356 uint16 b = spi_rx_reg (_currentSetting->spi_d ); // dummy read, needed, don't remove!
347357}
348358
349- uint16_t SPIClass::transfer16 ( uint16_t wr_data ) const {
350- spi_tx_reg (_currentSetting->spi_d , wr_data ); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
359+ uint8 SPIClass::transfer (uint8 byte ) const {
360+ spi_tx_reg (_currentSetting->spi_d , byte ); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
351361 while (spi_is_tx_empty (_currentSetting->spi_d ) == 0 ); // "5. Wait until TXE=1 ..."
352362 while (spi_is_busy (_currentSetting->spi_d ) != 0 ); // "... and then wait until BSY=0 before disabling the SPI."
353- return (uint16 )spi_rx_reg (_currentSetting->spi_d ); // "... and read the last received data."
363+ return (uint8 )spi_rx_reg (_currentSetting->spi_d ); // "... and read the last received data."
354364}
355365
356- uint8 SPIClass::transfer (uint8 byte ) const {
357- spi_tx_reg (_currentSetting->spi_d , byte ); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
366+ uint16_t SPIClass::transfer16 ( uint16_t wr_data ) const {
367+ spi_tx_reg (_currentSetting->spi_d , wr_data ); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
358368 while (spi_is_tx_empty (_currentSetting->spi_d ) == 0 ); // "5. Wait until TXE=1 ..."
359369 while (spi_is_busy (_currentSetting->spi_d ) != 0 ); // "... and then wait until BSY=0 before disabling the SPI."
360- return (uint8 )spi_rx_reg (_currentSetting->spi_d ); // "... and read the last received data."
370+ return (uint16 )spi_rx_reg (_currentSetting->spi_d ); // "... and read the last received data."
361371}
362372
363373/* Roger Clark and Victor Perez, 2015
@@ -419,10 +429,10 @@ uint8 SPIClass::dmaTransfer(void * transmitBuf, void * receiveBuf, uint16 length
419429* Still in progress.
420430* 2016 - stevstrong - reworked to automatically detect bit size from SPI setting
421431*/
422- uint8 SPIClass::dmaSend (void * transmitBuf, uint16 length)
432+ uint8 SPIClass::dmaSend (void * transmitBuf, uint16 length, bool minc )
423433{
424434 if (length == 0 ) return 0 ;
425- uint32 flags = (DMA_MINC_MODE | DMA_FROM_MEM | DMA_TRNS_CMPLT);
435+ uint32 flags = ( ( DMA_MINC_MODE*minc) | DMA_FROM_MEM | DMA_TRNS_CMPLT);
426436 uint8 b = 0 ;
427437// dma1_ch3_Active=true;
428438 dma_init (_currentSetting->spiDmaDev );
@@ -438,7 +448,7 @@ uint8 SPIClass::dmaSend(void * transmitBuf, uint16 length)
438448
439449// while (dma1_ch3_Active);
440450 uint32_t m = millis ();
441- while ((dma_get_isr_bits (_currentSetting->spiDmaDev , _currentSetting->spiTxDmaChannel ) & 0x2 )==0 ) {// Avoid interrupts and just loop waiting for the flag to be set.
451+ while ((dma_get_isr_bits (_currentSetting->spiDmaDev , _currentSetting->spiTxDmaChannel ) & DMA_ISR_TCIF1 )==0 ) {// Avoid interrupts and just loop waiting for the flag to be set.
442452 if ((millis () - m) > DMA_TIMEOUT) { b = 2 ; break ; }
443453 }
444454 dma_clear_isr_bits (_currentSetting->spiDmaDev , _currentSetting->spiTxDmaChannel );
0 commit comments