MK20D classic SPI example??

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

MK20D classic SPI example??

492 Views
terence_kong
Contributor II

trying to get the SPI work on K20 in order to access a flash memory.

I have checked on the scope that the SCLK did toggle during a byte send but the CS always held low.

And idea why the CS not toggling? 

Is there any sample program that shows how to setup the SPI on K20?


/*******************************************************************************
* scSSP0Init - Initialize SSP0 module( we are using SSP0 module in SPI frame format
*******************************************************************************/
void scSSP0Init(void)
{

SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK; //Enable PORTD
SIM_SCGC6 |= SIM_SCGC6_DSPI0_MASK; //Turn on clock to SPI0 module
/* configure GPIO for SPI0 function */
PORTD_PCR1 = PORT_PCR_MUX(2); //SPI0_CLK
PORTD_PCR2 = PORT_PCR_MUX(2); //SPI0_SO
PORTD_PCR3 = PORT_PCR_MUX(2); //SPI0_SI
PORTD_PCR4 = PORT_PCR_MUX(2); //SPI0_CS1
GPIOD_PDDR |= (1<<SPI_DFLASH_CS_PIN); //Set PTD4 as output
GPIOD_PSOR |= (1<<SPI_DFLASH_CS_PIN); //Set High iniitializlly

SPI0_MCR |= SPI_MCR_MDIS_MASK | SPI_MCR_HALT_MASK; // Allows external logic to disable DSPI clock, halt DSPI transfer
//SPI0_MCR |= SPI_MCR_DIS_TXF_MASK | SPI_MCR_DIS_RXF_MASK; // Disable RX and TX DSPIFIFO as simplified double buffered SPI
SPI0_CTAR0 = 0x38010008; //8 bit per frame, 50/50 duty, BR scaler = 256, BR div = 3, ~130kbps
SPI0_MCR |= SPI_MCR_MSTR_MASK; //Master mmode
//SPI0_TCR = (uint32_t)0x00UL; //Clear transfer count
// SPI0_RSER = (uint32_t)0x00UL; //Disable Interrupt and DMA
//SPI0_SR = SPI_SR_EOQF_MASK | SPI_SR_TCF_MASK | SPI_SR_RFOF_MASK | SPI_SR_TFUF_MASK | SPI_SR_TFFF_MASK | SPI_SR_RFDF_MASK; //Clear Status Register
SPI0_MCR &= ~(SPI_MCR_MDIS_MASK | SPI_MCR_HALT_MASK); // Enable DSPI clock, start DSPI transfer
//SPI0_MCR |= SPI_MCR_DIS_TXF_MASK | SPI_MCR_DIS_RXF_MASK; // Disable RX and TX DSPIFIFO as simplified double buffered SPI
}


/*******************************************************************************
* SPI Receive Byte, receive one byte only, returns Data byte
*******************************************************************************/
unsigned char SSP0ReceiveByte( void )
{
unsigned char rxData= 0xff;
SPI0_SR |= SPI_SR_TCF_MASK; //clear transfer
//SPI0_PUSHR = 0xFF;
// Wait until the Busy bit is cleared
if (!SSP0WaitReady())
{
return rxData;
}
rxData = SPI0_POPR;
return rxData;
}

/*******************************************************************************
* SSP0 SendByte, send one byte to EEPROM
*******************************************************************************/
void SSP0SendByte(unsigned char SP0Byte)
{
SPI0_SR |= SPI_SR_TCF_MASK; //clear transfer
SPI0_PUSHR = SP0Byte; //PUSH TX FIFO
// Wait until the Busy bit is cleared
if (!SSP0WaitReady())
{
return;
}
}

Labels (1)
Tags (1)
0 Kudos
1 Reply

487 Views
nxf56274
NXP Employee
NXP Employee

Hi,

You configure the PTD4 as CS. So you can't configure PDDR and PSOR except you configure the pin as gpio. You can download the sample in https://mcuxpresso.nxp.com/en/select.

K20 has same content as k60. So k20 uses k60's sample. You can refer it.

Have a great day,
TIC

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

- We are following threads for 7 days after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos