If you’re still interested, I have an update…
As noted above, writing data to the SD card on the CubeCell platform, using the current SPI & SD libraries, was very hit and miss—it would work sometimes, but most often not. Reading was actually OK, it was the writing before reading in the ReadWrite example that was ultimately causing the failures I observed. This had all the hallmarks of a [processor] timing problem—I saw something similar back when the CubeCells first came out.
Working my way through the relevant code, I have discovered that inserting a very small delay in the [CubeCell] SPI library transfer
function (on my Mac, under the Arduino IDE, that’s ~Documents/Arduino/hardware/CubeCell/CubeCell/cores/asr650x/SPI/SPI.cpp
, around line 183) solves this problem in my operating environment. If I insert a delay of 2 ms (1 ms gets you there more often than not, but not always) inside the if
block at line 193 everything then seems to work reliably.
uint8_t SPIClass::transfer(uint8_t data)
{
uint32 rxdata;
uint32 timeout = 0;
if(_spi_num == 0)
{
while (SPI_1_SpiUartGetRxBufferSize() == 0) {
timeout++;
if (timeout > spi_TIMEOUT) {
delay(2);
break;
}
}
.
.
.
Note that there is a separate block of code, that follows the above, to handle SPI_2. Presumably, if you’re using the second SPI bus [on the CubeCell Plus] you would need to add a similar delay there, but I haven’t actually run any of these more recent tests on the CubeCell Plus (the CubeCell V2—same processor but restricted configuration capability—doesn’t identify pins for the second SPI bus and I haven’t done anything to see whether or not a second SPI bus might be ‘available’ regardless).
The delay doesn’t seem to cause any problems in my current environment but I will raise a report on GitHub in the hope that someone with a deeper understanding of ASR6502 behaviour, or the relevant lower-level code, will be able fix the underlying problem.
I have been running tests on a CubeCell V2, so only using the one SPI bus for both SD card writing and LoRa, and the two are working well together. I’m only [LoRa] transmitting, and not trying to write to the SD card at the same time, so blocking is not an issue in this environment.
EDIT: Sometimes it pays to sleep on a problem… I was troubled by the fact that just increasing the value of spi_TIMEOUT
in the above code snippet didn’t have the same effect as the delay but I realised that I had incorrectly equated the timeout setting (which was 500) to milliseconds, when it is actually simply the number of cycles through the relevant loop. Looking through the various implementations of the SPI library, even this looks like a bit of a hack but if the CubeCell version was originally developed for the ASR6501 processor, and the ASR6502 is faster, it is logical that this timeout might need to be tuned to a given processor. I still reckon that there must be a more elegant solution here, but given the code we’re working with it seems that the solution to our current problem would simply be to increase the value of spi_TIMEOUT
sufficiently—3500 seems to be the lower limit with the CubeCell V2 module that I’m using, so maybe using a value of 4000 would provide a bit of headroom without having an unacceptable hit on performance.
spi_TIMEOUT
is set right near the top of the SPI.cpp
file, at line 26:
#define spi_TIMEOUT 4000
So, ignore my suggestion to insert any delays and just increase the value of spi_TIMEOUT
—this will apply no mater which SPI bus is being used.