MartyBrown
Member
Hi!
I'm trying to establish a spi connection between a teensy 3.6 as master and a teensy 4 as a slave. I only would like to receive 16Bit of Data on the Teensy 4 side.
Below is my Code on the Teensy 3.6 side and the code on the Teensy 4 side.
The pin-connections are: Pin10->Pin10, Pin11->Pin11, Pin12->Pin12 and Pin13->Pin13.
It doesn't seem like the DMA-Interrupt even gets triggered.
Perhabs anyone of you can spot a mistake
Thank you in advance!
Code of Master:
Code of Slave:
I'm trying to establish a spi connection between a teensy 3.6 as master and a teensy 4 as a slave. I only would like to receive 16Bit of Data on the Teensy 4 side.
Below is my Code on the Teensy 3.6 side and the code on the Teensy 4 side.
The pin-connections are: Pin10->Pin10, Pin11->Pin11, Pin12->Pin12 and Pin13->Pin13.
It doesn't seem like the DMA-Interrupt even gets triggered.
Perhabs anyone of you can spot a mistake
Thank you in advance!
Code of Master:
Code:
#include <SPI.h>
#define CS 10
// set up the speed, mode and endianness of each device
SPISettings SPI_Settings(1000000, MSBFIRST, SPI_MODE0);
void setup() {
// set the Slave Select Pins as outputs:
pinMode(CS, OUTPUT);
digitalWrite(CS, HIGH);
// initialize SPI:
SPI.begin();
}
uint16_t val = 1235; //send anything
void loop() {
SPI.beginTransaction(SPI_Settings);
digitalWrite(CS, LOW);
SPI.transfer16(val);
digitalWrite (CS, HIGH);
SPI.endTransaction();
delay(500);
}
Code of Slave:
Code:
//Inspired by Paul Stoffregen's SPI Lib and manitou48's spidma2.ino
#include <DMAChannel.h>
#include <SPI.h>
#define PRREG(x) Serial.print(#x" 0x"); Serial.println(x,BIN)
#define SAMPLES 10
DMAMEM static uint8_t rx_buffer[SAMPLES];
DMAChannel rx(false);
//volatile int ticks = 0;
void DMAChanneltransferCount() {
if (!(rx.TCD->BITER & DMA_TCD_BITER_ELINK)) {
rx.TCD->BITER = SAMPLES & 0x7fff;
} else {
rx.TCD->BITER = (rx.TCD->BITER & 0xFE00) | (SAMPLES & 0x1ff);
}
rx.TCD->CITER = rx.TCD->BITER;
}
void rxISR() {
Serial.println("RX Interrupt");
rx.clearInterrupt();
rx.clearComplete();
DMAChanneltransferCount();
//ticks++;
rx.enable();
asm volatile ("dsb");
}
bool initSPISlaveDMA() {
rx.disable();
rx.source((uint8_t &) LPSPI4_RDR);
rx.disableOnCompletion();
rx.triggerAtHardwareEvent(DMAMUX_SOURCE_LPSPI4_RX);
//rx.TCD->CSR = DMA_TCD_CSR_INTHALF | DMA_TCD_CSR_INTMAJOR;
rx.attachInterrupt(rxISR); //doesnt get called?
rx.interruptAtCompletion(); //TCD->CSR |= DMA_TCD_CSR_INTMAJOR;
//rx.TCD->ATTR_SRC = 0; //8Bit Modus
rx.destinationBuffer(rx_buffer, SAMPLES + 1);
//rx.TCD->DLASTSGA = -SAMPLES;
//LPSPI4_TCR = (LPSPI4_TCR & ~(LPSPI_TCR_FRAMESZ(31))) | LPSPI_TCR_FRAMESZ(15);
//LPSPI4_TCR = LPSPI_TCR_FRAMESZ(7);
LPSPI4_TCR = (LPSPI4_TCR & 0xfffff000) | LPSPI_TCR_FRAMESZ(15) | LPSPI_TCR_CONT; //16Bit Modus
LPSPI4_FCR = 0;
//LPSPI4_IER = LPSPI_IER_RDIE; //Receive Data Inerruopt enable
//LPSPI4_DER = LPSPI_IER_RDIE;
LPSPI4_DER = LPSPI_DER_RDDE; //RX DMA Request Enable
LPSPI4_SR = 0x3f00; // clear out all of the other status...
LPSPI4_CR |= LPSPI_CR_MEN; //SPI-Modul einschalten!
rx.enable();
return 1;
}
bool initSPISlave()
{
LPSPI4_CR &= ~LPSPI_CR_MEN; //Module disable
CCM_CBCMR &= ~CCM_CCGR1_LPSPI4(CCM_CCGR_ON); //Clock ausschalten
//return 1;
//FAST-IO
uint32_t fastio = IOMUXC_PAD_DSE(6) | IOMUXC_PAD_SPEED(1);
IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_01 = fastio;
IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_02 = fastio;
IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_03 = fastio;
CCM_CBCMR |= CCM_CCGR1_LPSPI4(CCM_CCGR_ON); //Clock reaktivieren
//LPSPI Muxing:
IOMUXC_LPSPI4_SCK_SELECT_INPUT = 0;
IOMUXC_LPSPI4_SDI_SELECT_INPUT = 0;
IOMUXC_LPSPI4_SDO_SELECT_INPUT = 0;
IOMUXC_LPSPI4_PCS0_SELECT_INPUT = 0;
//Pad Muxing:
IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_03 = 3; //SCK
IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_01 = 3; //SDI (MISO)
IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_02 = 3; //SDO (MOSI)
IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_00 = 3; //LPSPI4 PCS0 (CS)
//Master-Logic Reset:
LPSPI4_CR = LPSPI_CR_RST; //Master Logic reset! (Control Register => Software Reset)
/*
LPSPI4_CFGR1 &= ~LPSPI_CFGR1_MASTER; //Master Modus disable
LPSPI4_CFGR1 |= LPSPI_CFGR1_NOSTALL; //prevent stall from RX
*/
//FIFO Watermark for 16Bit?
LPSPI4_FCR = LPSPI_FCR_RXWATER(15);
LPSPI4_CR |= LPSPI_CR_RTF | LPSPI_CR_RRF | LPSPI_CR_MEN | LPSPI_CR_DBGEN | LPSPI_CR_DOZEN; //enable!
return 1;
}
void setup()
{
Serial.begin(115200);
SPI.begin();
/*
pinMode(10, INPUT);
pinMode(11, INPUT);
pinMode(12, OUTPUT);
pinMode(13, INPUT);
*/
rx.begin(true);
while (!Serial);
Serial.println("init SPI!");
if (initSPISlave()) {
Serial.println("SPI SLAVE init!");
}
if (initSPISlaveDMA()) {
Serial.println("DMA Channel init!");
}
Serial.println(SPI.pinIsChipSelect(10));
Serial.println(SPI.pinIsMOSI(11));
Serial.println(SPI.pinIsMISO(12));
Serial.println(SPI.pinIsSCK(13));
rx_buffer[0] = 0;
//Print Registers:
/*
PRREG(LPSPI4_CR);
PRREG(LPSPI4_SR);
PRREG(LPSPI4_DER);
PRREG(LPSPI4_CFGR0);
PRREG(LPSPI4_CFGR1);
PRREG(LPSPI4_CCR);
PRREG(LPSPI4_FCR);
PRREG(LPSPI_FSR_RXCOUNT(4));
PRREG(LPSPI4_TCR);
PRREG(LPSPI_RSR_RXEMPTY);
PRREG(LPSPI4_RDR);
*/
}
void loop()
{
Serial.println(rx_buffer[0]); //always Zero
//PRREG(LPSPI_FSR_RXCOUNT(4));
//PRREG(LPSPI4_SR);
delay(1000);
}
Last edited by a moderator: