How can I use MKM34 in SPI slave mode?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How can I use MKM34 in SPI slave mode?

1,597 Views
redsire
Contributor II

I want to use MKM34Z256VLL7 microcontroller in SPI slave mode, I configurated it but it doesn't work.

SPI0 IRQ interrupt is not working. and datas are not comming in receive buffer.

 

"

#include "MKM34Z7.h"

void SPI_Slave_Init(void) {

SIM->SCGC4 |= SIM_SCGC4_SPI0_MASK;
SIM->SCGC5 |= SIM_SCGC5_PORTD_MASK;


PORTD->PCR[2] = PORT_PCR_MUX(2); // SCK
PORTD->PCR[3] = PORT_PCR_MUX(2); // MOSI
PORTD->PCR[4] = PORT_PCR_MUX(2); // MISO
PORTD->PCR[5] = PORT_PCR_MUX(2); // SS


SPI0->C1 = SPI_C1_SPE_MASK; 
SPI0->C1 &= ~SPI_C1_MSTR_MASK; 
SPI0->C2 = 0; 


NVIC_SetPriority(SPI0_SPI1_IRQn, 2);
NVIC_EnableIRQ(SPI0_SPI1_IRQn);
SPI0->C1 |= SPI_C1_SPIE_MASK;
}

static volatile uint8_t receivedData;
static volatile uint8_t dataToSend = 0x55;

void SPI0_SPI1_IRQHandler(void) {
if (SPI0->S & SPI_S_SPRF_MASK) { 
receivedData = SPI0->DL; 
SPI0->DL = dataToSend;
}
}"

Tags (1)
0 Kudos
Reply
4 Replies

1,501 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

I suppose that your code is correct.

void SPI0_SPI1_IRQHandler(void) {
if (SPI0->S & SPI_S_SPRF_MASK) {
receivedData = SPI0->DL;
SPI0->DL = dataToSend;
}

The SPI0->DL is both input and output data register.

Regarding your issue, if you do not read the DL, just write the DL register, what is the result?

 

Hope it can help you

BR

XiangJun Rong

0 Kudos
Reply

1,495 Views
redsire
Contributor II
I really appreciate your help, also thank you for your quick reply.

I want the master to request data from the slave and the slave to send data to the master on request.

"The SPI0->DL is both input and output data register.
Regarding your issue, if you do not read the DL, just write the DL register, what is the result?"
In your said if I try to just write the DL register is there just wouldn't it be transmitting data from slave to master without request?
0 Kudos
Reply

1,565 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

When you use the SPI of KM34 as slave, as you know that the SPI master drives the SCK and /CS signal and launch the spi transfer, pls sure you have connected the master and slave:

SPI master      SPI slave

/CS                   /CS

SCK                   SCK

MOSI                MOSI

MISO                 MISO

after the master launch the spi transfer, the slave spi can work and enter interrupt.

Pls check if you connect the spi master and slave and if the spi master has launched the data transfer.

Hope it can help you

BR

Xiangjun Rong

1,528 Views
redsire
Contributor II
I have received the data in the receive buffer, but it transmits all incoming data, but I can't figure it out.
0 Kudos
Reply