S9KEAZN8AMFK SPI0 Master mode can't change speed.

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

S9KEAZN8AMFK SPI0 Master mode can't change speed.

747 Views
54anson
Contributor I


Hi,

   I am using S9KEAZN8AMFK as SPI master device , and the spi mode configured as mode 2. MCU clock speed is set to 48Mhz then the maximum spi clock speed is 12Mhz. I have use the processor expert generate code. I want send 4bytes data then read back 4bytes data. I try many different spi speed but it won't work. when i use digital oscilloscope capture the signal ,I found the signal Chip select toggled every byte send. and the clock pin will stop output when chip select pin toggle high. So that the slave device can not receive the whole  command.

   but it is very strange, when I set Spi speed to 1Mhz, It works very well. but 1Mhz spi speed is to slow for me, I want increase the spi speed, please help me.

  here is the spi initial code generated by the processor expert:

LDD_TDeviceData* SM1_Init(LDD_TUserData *UserDataPtr)

{

  /* Allocate LDD device structure */

  SM1_TDeviceDataPtr DeviceDataPrv;

  /* {Default RTOS Adapter} Driver memory allocation: Dynamic allocation is simulated by a pointer to the static object */

  DeviceDataPrv = &DeviceDataPrv__DEFAULT_RTOS_ALLOC;

  DeviceDataPrv->UserData = UserDataPtr; /* Store the RTOS device structure */

  /* Interrupt vector(s) allocation */

  /* {Default RTOS Adapter} Set interrupt vector: IVT is static, ISR parameter is passed by the global variable */

  INT_SPI0__DEFAULT_RTOS_ISRPARAM = DeviceDataPrv;

  DeviceDataPrv->ErrFlag = 0x00U;      /* Clear error flags */

  /* Clear the receive counters and pointer */

  DeviceDataPrv->InpRecvDataNum = 0x00U; /* Clear the counter of received characters */

  DeviceDataPrv->InpDataNumReq = 0x00U; /* Clear the counter of characters to receive by ReceiveBlock() */

  DeviceDataPrv->InpDataPtr = NULL;    /* Clear the buffer pointer for received characters */

  /* Clear the transmit counters and pointer */

  DeviceDataPrv->OutSentDataNum = 0x00U; /* Clear the counter of sent characters */

  DeviceDataPrv->OutDataNumReq = 0x00U; /* Clear the counter of characters to be send by SendBlock() */

  DeviceDataPrv->OutDataPtr = NULL;    /* Clear the buffer pointer for data to be transmitted */

  /* SIM_SCGC: SPI0=1 */

  SIM_SCGC |= SIM_SCGC_SPI0_MASK;

  /* Interrupt vector(s) priority setting */

  /* NVIC_IPR2: PRI_10=0x80 */

  NVIC_IPR2 = (uint32_t)((NVIC_IPR2 & (uint32_t)~(uint32_t)(

               NVIC_IP_PRI_10(0x7F)

              )) | (uint32_t)(

               NVIC_IP_PRI_10(0x80)

              ));

  /* NVIC_ISER: SETENA|=0x0400 */

  NVIC_ISER |= NVIC_ISER_SETENA(0x0400);

  /* SIM_PINSEL: SPI0PS=0 */

  SIM_PINSEL &= (uint32_t)~(uint32_t)(SIM_PINSEL_SPI0PS_MASK);

  /* SIM_PINSEL: SPI0PS=0 */

  SIM_PINSEL &= (uint32_t)~(uint32_t)(SIM_PINSEL_SPI0PS_MASK);

  /* SIM_PINSEL: SPI0PS=0 */

  SIM_PINSEL &= (uint32_t)~(uint32_t)(SIM_PINSEL_SPI0PS_MASK);

  /* SIM_PINSEL: SPI0PS=0 */

  SIM_PINSEL &= (uint32_t)~(uint32_t)(SIM_PINSEL_SPI0PS_MASK);

  /* SPI0_C1: SPIE=0,SPE=0,SPTIE=0,MSTR=1,CPOL=0,CPHA=1,SSOE=1,LSBFE=0 */

  SPI0_C1 = (SPI_C1_MSTR_MASK | SPI_C1_CPHA_MASK | SPI_C1_SSOE_MASK); /* Set configuration register */

  /* SPI0_C2: SPMIE=0,??=0,??=0,MODFEN=1,BIDIROE=0,??=0,SPISWAI=0,SPC0=0 */

  SPI0_C2 = SPI_C2_MODFEN_MASK;        /* Set configuration register */

  /* SPI0_BR: ??=0,SPPR=2,SPR=2 */

  SPI0_BR = (SPI_BR_SPPR(0x02) | SPI_BR_SPR(0x02)); /* Set baud rate register */

  /* SPI0_C1: SPE=1 */

  SPI0_C1 |= SPI_C1_SPE_MASK;          /* Enable SPI module */

  /* Registration of the device structure */

  PE_LDD_RegisterDeviceStructure(PE_LDD_COMPONENT_SM1_ID,DeviceDataPrv);

  return ((LDD_TDeviceData *)DeviceDataPrv); /* Return pointer to the data data structure */

}

when I try change SPI_BR_SPPR or SPI_BR_SPR to change the SPI speed , It doesn't work anymore.

only setting SPI0_BR = (SPI_BR_SPPR(0x02) | SPI_BR_SPR(0x02));  could work.

Labels (1)
0 Kudos
1 Reply

471 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Customer,

    Answer your two questions.

1. CS pin pull higher after each byte send

  The SPI module really have this character, so, if you want the CS pin pull up after your whole command, I suggest you choos an GPIO pin to control the CS, it is will be flexible.

Just don't use the SPI module's CS pin, choose another GPIO pin, when you want to send data, pull down the pin, after the whole command is sent, then pull up the CS signal.

2.  frequency can't be changed.

  This should be caused by the PE code, as you know, PE code combine a lot of code, although it easy to user, but the code is complicated to understand and not very brief.

  So, if you want to use very higher SPI baud, I suggest you control the register directly, then it will save a lot of code execution time.

  You can refer to this document:

External flash control via kinetis M series SPI module

  The control method is nearly the same.

  Please try to write the code by yourself.

Wish it helps you!

If you still have question, please kindly let me know!


Have a great day,
Jingjing

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

0 Kudos