[FreeScale]/KINETIS2013. 10. 25. 22:30

[FRDM-KL25Z] Kinetis - SPI 테스트

[FRDM-KL25Z] Kinetis - SPI 테스트


KL25Z SPI 테스트를 위한 자료 정리



SPI Bus clock

Bus clock Up to 50 MHz Up to 4 MHz MCGOUTCLK clock divider


KL25Z SPI 초기화 함수

void SPI0_Init(void)

{

SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK;      //Turn on clock to D module  

SIM_SCGC4 |= SIM_SCGC4_SPI0_MASK;       //Enable SPI0 clock  


_SPI0_IO_INIT();

/*

PORTD_PCR0 = PORT_PCR_MUX(0x2);    //Set PTD0 to mux 2 [SPI0_PCS0]  

PORTD_PCR1 = PORT_PCR_MUX(0x2);    //Set PTD1 to mux 2 [SPI0_SCK]  

PORTD_PCR2 = PORT_PCR_MUX(0x2);    //Set PTD2 to mux 2 [SPI0_MOSI]  

PORTD_PCR3 = PORT_PCR_MUX(0x2);    //Set PTD3 to mux 2 [SPIO_MISO]  

*/

//SPI_C1_CPOL_MASK

//SPI_C1_CPHA_MASK

SPI0_C1 = SPI_C1_MSTR_MASK;// | SPI_C1_SSOE_MASK;   //Set SPI0 to Master & SS pin to auto SS  


//SPI0_C2 = SPI_C2_MODFEN_MASK;   //Master SS pin acts as slave select output   


    //Set baud rate prescale

SPI0_BR = (SPI_BR_SPPR(0x01) | SPI_BR_SPR(0x02));


//SPI Mode

Cbi(SPI0_C1, SPI_C1_CPOL_MASK);

Cbi(SPI0_C1, SPI_C1_CPHA_MASK);

    //Enable SPI0  

Sbi(SPI0_C1, SPI_C1_SPE_MASK);

}




KL25Z SPI 데이터 송수신 함수

unsigned char SPI0_WriteReadByte(unsigned char Data)

{

//SPTEF - 1 이면 데이터 전송

while(!(SPI_S_SPTEF_MASK & SPI0_S))    

{        

asm("nop");        //While buffer is not empty do nothing    

}    


SPI0_D = Data;     //Write char to SPI     


//SPRF - 1 이면 데이터 수신

while(!(SPI_S_SPRF_MASK & SPI0_S))    

{        

asm("nop");        //While buffer is not empty do nothing    

}  


return SPI0_D;

}






Posted by nexp

댓글을 달아 주세요