Skip to content

Commit 0d2d3e9

Browse files
committed
Add full duplex capability to SPI
Also fixed the no-return error within 'transfer' and added last-resort definitions of 'SS' 'MOSI' 'MISO' and 'CLK' to ensure out-of-the-box compatibility with the SD examples.
1 parent b26285a commit 0d2d3e9

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

cores/arduino/ard_sup/Arduino.h

+2
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,6 @@ extern "C"
8383

8484
#include "variant.h"
8585

86+
#include "ap3_post_variant.h"
87+
8688
#endif // _ARDUINO_H_
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef _AP3_POST_VARIANT_H_
2+
#define _AP3_POST_VARIANT_H_
3+
4+
// Some fail-unsafe defines for SD card library compilation.
5+
// These ideally would be defined in a variant header, however
6+
// if they are not these definitions may suffice
7+
#ifndef SS
8+
#define SS 50
9+
#endif // SS
10+
#ifndef MOSI
11+
#define MOSI 50
12+
#endif // SS
13+
#ifndef MISO
14+
#define MISO 50
15+
#endif // SS
16+
#ifndef CLK
17+
#define CLK 50
18+
#endif // SS
19+
20+
21+
22+
23+
#endif // _AP3_POST_VARIANT_H_

libraries/SPI/src/SPI.cpp

+15-7
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,9 @@ void SPIClass::setDataMode(uint8_t mode)
225225

226226
byte SPIClass::transfer(uint8_t data)
227227
{
228-
_transfer(&data, NULL, 1);
228+
uint8_t rxval = 0x00;
229+
_transfer(&data, &rxval, 1);
230+
return rxval;
229231
}
230232

231233
uint16_t SPIClass::transfer16(uint16_t data)
@@ -289,7 +291,7 @@ void SPIClass::_transfer(void *buf_out, void *buf_in, size_t count)
289291
// Determine direction
290292
if ((buf_out != NULL) && (buf_in != NULL))
291293
{
292-
iomTransfer.eDirection = AM_HAL_IOM_TX; // AM_HAL_IOM_FULLDUPLEX - Note: Ambiq SDK says that FULLDUPLEX is not yet supported // todo:
294+
iomTransfer.eDirection = AM_HAL_IOM_FULLDUPLEX;
293295
}
294296
else if (buf_out != NULL)
295297
{
@@ -300,12 +302,18 @@ void SPIClass::_transfer(void *buf_out, void *buf_in, size_t count)
300302
iomTransfer.eDirection = AM_HAL_IOM_RX;
301303
}
302304

303-
uint32_t retVal32 = am_hal_iom_blocking_transfer(_handle, &iomTransfer);
304-
if (retVal32 != 0)
305-
{
306-
// Serial.printf("got an error on _transfer: %d\n", retVal32);
307-
return /*retVal32*/;
305+
uint32_t retVal32 = 0;
306+
if( iomTransfer.eDirection == AM_HAL_IOM_FULLDUPLEX ){
307+
retVal32 = am_hal_iom_spi_blocking_fullduplex(_handle, &iomTransfer);
308+
}else{
309+
retVal32 = am_hal_iom_blocking_transfer(_handle, &iomTransfer);
308310
}
311+
312+
// if (retVal32 != 0)
313+
// {
314+
// Serial.printf("got an error on _transfer: %d\n", retVal32);
315+
// return retVal32;
316+
// }
309317
}
310318

311319
void SPIClass::attachInterrupt()

0 commit comments

Comments
 (0)