AnsweredAssumed Answered

S9KEAZN8AMFK SPI0 Master mode can't change speed.

Question asked by 忠尧 赵 on Jul 3, 2016
Latest reply on Jul 4, 2016 by Kerry Zhou


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.

Outcomes