Skip to content

True 16 bit transfer, and new loop friendly 16 bit transfer #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions libraries/SPI/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
* Copyright (c) 2014 by Paul Stoffregen <[email protected]> (Transaction API)
* Copyright (c) 2014 by Matthijs Kooijman <[email protected]> (SPISettings AVR)
* Copyright (c) 2014 by Andrew J. Kroll <[email protected]> (atomicity fixes)
* Copyright (c) 2024 by Mitchell C. Nelson, PhD <[email protected]>
* SPI Master library for arduino.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
* or the GNU Lesser General Public License version 2.1, both as
* published by the Free Software Foundation.
*
*
*/

/**************************************************************************************
Expand Down Expand Up @@ -203,7 +206,102 @@ uint8_t ArduinoSPI::transfer(uint8_t data)
return rxbuf;
}

/* This provides true 16 bit transfers
*/
uint16_t ArduinoSPI::transfer16(uint16_t data)
{
uint16_t rxbuf;

if (_is_sci) {
_spi_cb_event[_cb_event_idx] = SPI_EVENT_TRANSFER_ABORTED;

_write_then_read(&_spi_sci_ctrl, &data, &rxbuf, 1, SPI_BIT_WIDTH_16_BITS);

for (auto const start = millis();
(SPI_EVENT_TRANSFER_COMPLETE != _spi_cb_event[_cb_event_idx]) && (millis() - start < 1000); )
{
__NOP();
}
if (SPI_EVENT_TRANSFER_ABORTED == _spi_cb_event[_cb_event_idx])
{
end();
return 0;
}
}
else
{
_spi_ctrl.p_regs->SPCR_b.SPE = 0; /* disable SPI unit */
_spi_ctrl.p_regs->SPDCR = R_SPI0_SPDCR_SPLW_Msk; /* SPI word access */
_spi_ctrl.p_regs->SPCMD_b[0].SPB = 0x0F; /* spi bit width = 16 */
_spi_ctrl.p_regs->SPCR_b.SPE = 1; /* enable SPI unit */

while (!_spi_ctrl.p_regs->SPSR_b.SPTEF){}

_spi_ctrl.p_regs->SPDR = data;

while (!_spi_ctrl.p_regs->SPSR_b.SPRF){}

rxbuf = _spi_ctrl.p_regs->SPDR;


_spi_ctrl.p_regs->SPCR_b.SPE = 0; /* disable SPI unit */
_spi_ctrl.p_regs->SPDCR = R_SPI0_SPDCR_SPBYT_Msk; /* SPI byte access */
_spi_ctrl.p_regs->SPCMD_b[0].SPB = 7; /* spi bit width = 8 */
_spi_ctrl.p_regs->SPCR_b.SPE = 1; /* enable SPI unit */
}

return rxbuf;
}

/* Use these three functions when you need to loop over a device
such as an ADC or DAC where you need to pulse a pin between
each access. Call transfer16_setup() before the loop, then
call tansfer16_transfer() inside the loop, and then when you
are done you may choose to call transfer16_cleanup() to restore
the default 8 bit setup.
*/

uint16_t ArduinoSPI::transfer16_setup()
{
if (!_is_sci) {
_spi_ctrl.p_regs->SPCR_b.SPE = 0; /* disable SPI unit */
_spi_ctrl.p_regs->SPDCR = R_SPI0_SPDCR_SPLW_Msk; /* SPI word access */
_spi_ctrl.p_regs->SPCMD_b[0].SPB = 0x0F; /* spi bit width = 16 */
_spi_ctrl.p_regs->SPCR_b.SPE = 1; /* enable SPI unit */
}
}

uint16_t ArduinoSPI::transfer16_transfer(uint16_t data)
{
uint16_t rxbuf;
if (_is_sci) {
return transfer16(data);
}
else {
while (!_spi_ctrl.p_regs->SPSR_b.SPTEF){}

_spi_ctrl.p_regs->SPDR = data;

while (!_spi_ctrl.p_regs->SPSR_b.SPRF){}

rxbuf = _spi_ctrl.p_regs->SPDR;
}
}

uint16_t ArduinoSPI::transfer16_cleanup()
{
_spi_ctrl.p_regs->SPCR_b.SPE = 0; /* disable SPI unit */
_spi_ctrl.p_regs->SPDCR = R_SPI0_SPDCR_SPBYT_Msk; /* SPI byte access */
_spi_ctrl.p_regs->SPCMD_b[0].SPB = 7; /* spi bit width = 8 */
_spi_ctrl.p_regs->SPCR_b.SPE = 1; /* enable SPI unit */
}


/* This is the original 16 bit transfer as two bytes, with an
extra 1.4 usecs between the two bytes.
*/

uint16_t ArduinoSPI::transfer16_asbytes(uint16_t data)
{
union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } t;
t.val = data;
Expand Down
4 changes: 4 additions & 0 deletions libraries/SPI/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class ArduinoSPI : public SPIClass

virtual uint8_t transfer(uint8_t data);
virtual uint16_t transfer16(uint16_t data);
virtual uint16_t transfer16_setup();
virtual uint16_t transfer16_transfer(uint16_t data);
virtual uint16_t transfer16_cleanup();
virtual uint16_t transfer16_asbytes(uint16_t data);
virtual void transfer(void *buf, size_t count);

// Transaction Functions
Expand Down
Loading