AnsweredAssumed Answered

how to use spi read 24bit data on use s32k144

Question asked by jinshuai xu on Apr 17, 2019
Latest reply on Apr 29, 2019 by jinshuai xu

hello:

      l need to use SPI to read/write sensor chip recently. SPI control show in below.

 

    SO  l set SPI  TCR Frame Size is 16bits. my  SPI init and read function is below. 

void LPSPI0_init_master(void) {
PCC->PCCn[PCC_LPSPI0_INDEX] = 0; /* Disable clocks to modify PCS ( default) */
//PCC->PCCn[PCC_LPSPI1_INDEX] = 0xC6000000; /* Enable PCS=SPLL_DIV2 (40 MHz func'l clock) */
PCC->PCCn[PCC_LPSPI0_INDEX] = 0xC1000000; /* Enable PCS=SOSC_DIV2 (16 MHz func'l clock) */
//clock source select 001 select soscdiv2_clk

LPSPI0->CR = 0x00000000; /* Disable module for configuration */
LPSPI0->IER = 0x00000000; /* Interrupts not used */
LPSPI0->DER = 0x00000000; /* DMA not used */
LPSPI0->CFGR0 = 0x00000000; /* Defaults: */
/* RDM0=0: rec'd data to FIFO as normal */
/* CIRFIFO=0; Circular FIFO is disabled */
/* HRSEL, HRPOL, HREN=0: Host request disabled */
LPSPI0->CFGR1 = 0x00000001; /* Configurations: master mode*/
/* PCSCFG=0: PCS[3:2] are enabled */
/* OUTCFG=0: Output data retains last value when CS negated */
/* PINCFG=0: SIN is input, SOUT is output */
/* MATCFG=0: Match disabled */
/* PCSPOL=0: PCS is active low */
/* NOSTALL=0: Stall if Tx FIFO empty or Rx FIFO full */
/* AUTOPCS=0: does not apply for master mode */
/* SAMPLE=0: input data sampled on SCK edge */
/* MASTER=1: Master mode */

LPSPI0->TCR = 0x4800000F;   //16 bits, //PRESCALE=1: Functional clock divided by 2*1 = 2 */16m/2=8M 1/8M=125ns
//LPSPI0->TCR = 0x5100000F; /* Transmit cmd: PCS3, 16 bits, prescale func'l clk by 4, etc*/
/* CPOL=0: SCK inactive state is low */
/* CPHA=1: Change data on SCK lead'g, capture on trail'g edge*/
/* PRESCALE=2: Functional clock divided by 2**2 = 4 */
/* PCS=1: Transfer using PCS3 */
/* LSBF=0: Data is transfered MSB first */
/* BYSW=0: Byte swap disabled */
/* CONT, CONTC=0: Continuous transfer disabled */
/* RXMSK=0: Normal transfer: rx data stored in rx FIFO */
/* TXMSK=0: Normal transfer: data loaded from tx FIFO */
/* WIDTH=0: Single bit transfer */
/* FRAMESZ=15: # bits in frame = 15+1=16 */
LPSPI0->CCR = 0x04090800; // /* SCKDIV=0: SCK divider =0+2 = 2 2*125ns=250ns (0.25 usec: 4 MHz baud rate) */
//LPSPI1->CCR = 0x04090808; /* Clock dividers based on prescaled func'l clk of 100 nsec */
/* SCKPCS=4: SCK to PCS delay = 4+1 = 5 (500 nsec) */
/* PCSSCK=4: PCS to SCK delay = 9+1 = 10 (1 usec) */
/* DBT=8: Delay between Transfers = 8+2 = 10 (1 usec) */
/* SCKDIV=8: SCK divider =8+2 = 10 (1 usec: 1 MHz baud rate) */
LPSPI0->FCR = 0x00000003; /* RXWATER=0: Rx flags set when Rx FIFO >0 */
/* TXWATER=3: Tx flags set when Tx FIFO <= 3 */
LPSPI0->CR = 0x00000009; /* Enable module for operation */
/* DBGEN=1: module enabled in debug mode */
/* DOZEN=0: module enabled in Doze mode */
/* RST=0: Master logic not reset */
/* MEN=1: Module is enabled */
}

 

void LPSPI0_Register_Read_TDC1000(uint8_t *TDC1000_commond,uint16_t *TDC1000_Read_data,uint8_t TDC1000_byte) {
uint8_t temp1;
uint16_t TDC1000_write_Buf[10];

for (temp1 = 0; temp1 < TDC1000_byte; temp1++) {
TDC1000_write_Buf[temp1] = 0xff + (TDC1000_commond[temp1] &(~0x40))*256;

SELECT_PCS0();//pcs select
//while((LPSPI0->SR & 0x01000000)>>24==1);//busy wait
LPSPI0_transmit_16bits(TDC1000_write_Buf[temp1]);//send command and data
while((LPSPI0->SR & LPSPI_SR_TCF_MASK)>>LPSPI_SR_TCF_SHIFT==0);//wait until send success
LPSPI0->SR |= LPSPI_SR_TCF_MASK;//All transfers have completed flag
TDC1000_Read_data[temp1]=LPSPI0_receive_16bits();
TDC1000_Read_data[temp1]=TDC1000_Read_data[temp1]&0xff;
}
}

 

 

now  l want read 8bits data sometime, read 24bits sometime,how l set read function. set as below????

 

 

void LPSPI0_Register_Read_TDC1000(uint8_t *TDC1000_commond,uint16_t *TDC1000_Read_data,uint8_t TDC1000_byte) {
uint8_t temp1;
uint16_t TDC1000_write_Buf[10];

for (temp1 = 0; temp1 < TDC1000_byte; temp1++) {
TDC1000_write_Buf[temp1] = 0xff + (TDC1000_commond[temp1] &(~0x40))*256;

SELECT_PCS0();//pcs select
LPSPI0_transmit_16bits(TDC1000_write_Buf[temp1]);//send command and data
while((LPSPI0->SR & LPSPI_SR_TCF_MASK)>>LPSPI_SR_TCF_SHIFT==0);//wait until send success
LPSPI0->SR |= LPSPI_SR_TCF_MASK;//All transfers have completed flag
TDC1000_Read_data[temp1]=LPSPI0_receive_16bits();
TDC1000_Read_data[temp1]=TDC1000_Read_data[temp1]&0xff;

TDC1000_Read1_data[temp1]=LPSPI0_receive_16bits();
TDC1000_Read1_data[temp1]=TDC1000_Read1_data[temp1]&0xff;
}
}

 

 

who can help me ?

thank you.......

Outcomes