Skip to content

patch full duplex SPI transfers... #154

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

Merged
merged 1 commit into from
Apr 15, 2020
Merged

patch full duplex SPI transfers... #154

merged 1 commit into from
Apr 15, 2020

Conversation

oclyke
Copy link
Contributor

@oclyke oclyke commented Apr 14, 2020

...after migration to AmbiqSuite SDK 2.4.2 HAL

Copied functionality from 2.2.0 version of HAL. Will ultimately want to fix this. Tested with this sketch:

#include "SPI.h"
#define CS_PIN 13
SPISettings MySPISettings(14000000, MSBFIRST, SPI_MODE0);

/////////////////////////////////////////////
// Set TX and RX values for various data types
uint8_t tx_val_8 = 0xAA;
uint8_t rx_val_8 = 0x00;

uint16_t tx_val_16 = 0xABBA;
uint16_t rx_val_16 = 0x00;

const uint8_t msg_len = 8;
uint8_t tx_msg[msg_len] = {'H', 'e', 'l', 'l', 'o', '!', '!', 0};
uint8_t rx_msg[msg_len] = {0, 0, 0, 0, 0, 0, 0, 0};

uint8_t single_buff[msg_len] = {'t', 'e', 's', 't', ' ', 'p', 'a', 't'};
uint8_t single_buff_copy[msg_len] = {0, 0, 0, 0, 0, 0, 0, 0};

/////////////////////////////////////////////
// Main test
void setup() {
  Serial.begin(115200);
  SPI.begin();
  pinMode(CS_PIN, OUTPUT);
  digitalWrite(CS_PIN, HIGH);
  memcpy(single_buff_copy, single_buff, msg_len); // copy single buff for comparison after the fact

  delay(100);
  Serial.println("\n\nApollo3 SPI Full Duplex Test Sketch\n");

  /////////////////////////////////////////////
  // SPI Transaction with tests of each data type transfer (connect MOSI to MISO)
  SPI.beginTransaction(MySPISettings);
  digitalWrite(CS_PIN, LOW);

  rx_val_8 = SPI.transfer(tx_val_8);            // test 8-bit fullduplex  (Arduino API)
  rx_val_16 = SPI.transfer16(tx_val_16);        // test 16-bit fullduplex (Arduino API)
  SPI.transferOutIn(tx_msg, rx_msg, msg_len);   // test buffer fullduplex (API extension - shows HW capability)
  SPI.transfer(single_buff, msg_len);           // test out/in in-place   (Arduino API)

  digitalWrite(CS_PIN, HIGH);
  SPI.endTransaction();

  /////////////////////////////////////////////
  // Check if received values match sent values
  Serial.print("SPI.transfer(uint8_t); RESULT: "); test_8bit_match();
  Serial.print("SPI.transfer16(uint16_t); RESULT: "); test_16bit_match();
  Serial.print("SPI.transferOutIn(void*, void*, size_t); RESULT: "); test_msg_match(tx_msg, rx_msg, msg_len);
  Serial.print("SPI.transfer(void*, size_t); RESULT: "); test_msg_match(single_buff_copy, single_buff, msg_len);

  Serial.println();
}

void loop() {
}

void test_8bit_match( void ){
  if(rx_val_8 != tx_val_8){
    Serial.printf("Error: failed fullduplex\n\tSent 0x%02X, received 0x%02X\n", tx_val_8, rx_val_8);
  }else{
    Serial.printf("Passed!\n");
  }
}

void test_16bit_match( void ){
  if(rx_val_16 != tx_val_16){
    Serial.printf("Error: failed fullduplex\n\tSent 0x%04X, received 0x%04X\n", tx_val_16, rx_val_16);
  }else{
    Serial.printf("Passed!\n");
  }
}

void test_msg_match( uint8_t* tx, uint8_t* rx, size_t len ){
  bool msg_match = true;
  for(uint8_t ix = 0; ix < len; ix++){
    if(tx[ix] != rx[ix]){
      msg_match = false;
      break;
    }
  }
  if(!msg_match){
    Serial.printf("Error: failed fullduplex\n\tSent: { ");
    for(uint8_t ix = 0; ix < len; ix++){
      if(ix != 0){
        Serial.print(", ");
      }
      Serial.print((char)tx[ix]);
    }
    Serial.print("}, recieved { ");
    for(uint8_t ix = 0; ix < len; ix++){
      if(ix != 0){
        Serial.print(", ");
      }
      Serial.print((char)rx[ix]);
    }
    Serial.print("}\n");
  }else{
    Serial.printf("Passed!\n");
  }
}

….2 HAL

Copied functionality from 2.2.0 version of HAL. Will ultimately want to fix this. Tested with this sketch:

```
#include "SPI.h"
#define CS_PIN 13
SPISettings MySPISettings(14000000, MSBFIRST, SPI_MODE0);

/////////////////////////////////////////////
// Set TX and RX values for various data types
uint8_t tx_val_8 = 0xAA;
uint8_t rx_val_8 = 0x00;

uint16_t tx_val_16 = 0xABBA;
uint16_t rx_val_16 = 0x00;

const uint8_t msg_len = 8;
uint8_t tx_msg[msg_len] = {'H', 'e', 'l', 'l', 'o', '!', '!', 0};
uint8_t rx_msg[msg_len] = {0, 0, 0, 0, 0, 0, 0, 0};

uint8_t single_buff[msg_len] = {'t', 'e', 's', 't', ' ', 'p', 'a', 't'};
uint8_t single_buff_copy[msg_len] = {0, 0, 0, 0, 0, 0, 0, 0};

/////////////////////////////////////////////
// Main test
void setup() {
  Serial.begin(115200);
  SPI.begin();
  pinMode(CS_PIN, OUTPUT);
  digitalWrite(CS_PIN, HIGH);
  memcpy(single_buff_copy, single_buff, msg_len); // copy single buff for comparison after the fact

  delay(100);
  Serial.println("\n\nApollo3 SPI Full Duplex Test Sketch\n");

  /////////////////////////////////////////////
  // SPI Transaction with tests of each data type transfer (connect MOSI to MISO)
  SPI.beginTransaction(MySPISettings);
  digitalWrite(CS_PIN, LOW);

  rx_val_8 = SPI.transfer(tx_val_8);            // test 8-bit fullduplex  (Arduino API)
  rx_val_16 = SPI.transfer16(tx_val_16);        // test 16-bit fullduplex (Arduino API)
  SPI.transferOutIn(tx_msg, rx_msg, msg_len);   // test buffer fullduplex (API extension - shows HW capability)
  SPI.transfer(single_buff, msg_len);           // test out/in in-place   (Arduino API)

  digitalWrite(CS_PIN, HIGH);
  SPI.endTransaction();

  /////////////////////////////////////////////
  // Check if received values match sent values
  Serial.print("SPI.transfer(uint8_t); RESULT: "); test_8bit_match();
  Serial.print("SPI.transfer16(uint16_t); RESULT: "); test_16bit_match();
  Serial.print("SPI.transferOutIn(void*, void*, size_t); RESULT: "); test_msg_match(tx_msg, rx_msg, msg_len);
  Serial.print("SPI.transfer(void*, size_t); RESULT: "); test_msg_match(single_buff_copy, single_buff, msg_len);

  Serial.println();
}

void loop() {
}

void test_8bit_match( void ){
  if(rx_val_8 != tx_val_8){
    Serial.printf("Error: failed fullduplex\n\tSent 0x%02X, received 0x%02X\n", tx_val_8, rx_val_8);
  }else{
    Serial.printf("Passed!\n");
  }
}

void test_16bit_match( void ){
  if(rx_val_16 != tx_val_16){
    Serial.printf("Error: failed fullduplex\n\tSent 0x%04X, received 0x%04X\n", tx_val_16, rx_val_16);
  }else{
    Serial.printf("Passed!\n");
  }
}

void test_msg_match( uint8_t* tx, uint8_t* rx, size_t len ){
  bool msg_match = true;
  for(uint8_t ix = 0; ix < len; ix++){
    if(tx[ix] != rx[ix]){
      msg_match = false;
      break;
    }
  }
  if(!msg_match){
    Serial.printf("Error: failed fullduplex\n\tSent: { ");
    for(uint8_t ix = 0; ix < len; ix++){
      if(ix != 0){
        Serial.print(", ");
      }
      Serial.print((char)tx[ix]);
    }
    Serial.print("}, recieved { ");
    for(uint8_t ix = 0; ix < len; ix++){
      if(ix != 0){
        Serial.print(", ");
      }
      Serial.print((char)rx[ix]);
    }
    Serial.print("}\n");
  }else{
    Serial.printf("Passed!\n");
  }
}
```
@oclyke oclyke requested a review from Wenn0101 April 14, 2020 20:59
@oclyke oclyke mentioned this pull request Apr 14, 2020
@oclyke oclyke merged commit d322a7f into master Apr 15, 2020
@oclyke oclyke deleted the patch-spi-fullduplex branch April 15, 2020 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant