M052LBN EVM - SPI 테스트
M052LBN는 저렴한 MCU임에도 2채널의 SPI를 제공한다. FIFO있고 SPI속도도 빠르다.
M052LBN SPI 블록도
M052LBN SPI 초기화 함수
void SPI0_Init(void)
{
SYS_UnlockReg();
CLK_EnableModuleClock(SPI0_MODULE);
/* Set multi function pin for SPI1 */
//SYS->P0_MFP = SYS_MFP_P04_SPISS1 | SYS_MFP_P05_MOSI_1 | SYS_MFP_P06_MISO_1 | SYS_MFP_P07_SPICLK1;
SYS->P1_MFP = SYS_MFP_P15_MOSI_0 | SYS_MFP_P16_MISO_0 | SYS_MFP_P17_SPICLK0;
/* Select HXT as the clock source of SPI1 */
CLK_SetModuleClock(SPI0_MODULE, CLK_CLKSEL1_SPI0_S_HCLK, MODULE_NoMsk);
/* Enable SPI1 peripheral clock */
CLK_EnableModuleClock(SPI0_MODULE);
/* Lock protected registers */
SYS_LockReg();
/* Configure SPI1 as a master, MSB first, 8-bit transaction, SPI Mode-0 timing, clock is 6MHz */
SPI_Open(SPI0, SPI_MASTER, SPI_MODE_0, 8, 6000000);
/* Enable the automatic hardware slave select function. Select the SS0 pin and configure as low-active. */
SPI_EnableAutoSS(SPI0, SPI_SS, SPI_SS_ACTIVE_LOW);
}
SPI_CNTRL 레지스터
M052LBN SPI 송수신 함수
unsigned char SPI0_WriteReadByte(unsigned char Data)
{
SPI_WRITE_TX0(SPI0, Data);
/* Trigger SPI transfer */
SPI_TRIGGER(SPI0);
/* Check busy state */
while(SPI_IS_BUSY(SPI0));
return SPI_READ_RX0(SPI0);
}