- LPC1752通过SPI与FM25L04B进行通讯。
图 1 LPC1752 与 FM25L04B 硬件连线图
- 初始化LPC1752的SSP0模块
按着如下步骤进行初始化的操作:
- 在PCONP寄存器置位PCSSP0,允许SSP0模块的电源输出;
- 在PCLK_SSP1寄存器中选择PCLK_SEL0,设置SSP0的时钟为CCLK/4;对SSP0的时钟进行分频,设置为CCLK/4/10;
- 对LPC1752的管脚进行设置;
- 设置 SPI 为主模式, FM25L04 工作在 SPI 模式 0。
初始化代码:
void LPC17xx_SPI_Init(void)
{
volatile uint32_t dummy;
dummy = dummy; /* avoid warning */
LPC_SC->PCONP |= (1 << 21); /* Enable power to SSPI0 block */
/* PCLK_SSP0=CCLK */
LPC_SC->PCLKSEL1 &= ~(3<<10); /* PCLKSP0 = CCLK/4 (25MHz) */
LPC_SC->PCLKSEL1 |= (1<<10); /* PCLKSP0 = CCLK (100MHz) */
LPC17xx_SPI_SetSpeed (SPI_SPEED_10MHz);
LPC_GPIO0->FIODIR |= (1<<16); /* P0.16 CS is output */
/* P0.15 SCK, P0.17 MISO, P0.18 MOSI are SSP pins. */
LPC_PINCON->PINSEL0 &= ~( (2UL<<30) ); /* P0.15 cleared */
LPC_PINCON->PINSEL1 &= ~( (2<<2) | (2<<4) ); /* P0.17, P0.18 cleared */
LPC_PINCON->PINSEL0 |= ( (2UL<<30) ); /* P0.15 SCK0 */
LPC_PINCON->PINSEL1 |= ( (2<<2) | (2<<4) ); /* P0.17 MISO0 P0.18 MOSI0 */
LPC_SSP0->CR0 = ( 7<<0 ); /* 8Bit, CPOL=0, CPHA=0 */
LPC_SSP0->CR1 = ( 1<<1 ); /* SSP0 enable, master */
/* wait for busy gone */
while( LPC_SSP0->SR & ( 1 << SSPSR_BSY ) );
/* drain SPI RX FIFO */
while( LPC_SSP0->SR & ( 1 << SSPSR_RNE ) )
{
dummy = LPC_SSP0->DR;
}
}
- 读取FM25L04B
FM25L04B的读时序
uint8_t FM25L04B_Read(uint32_t Dst, uint8_t* RcvBufPt, uint32_t NByte)
{
uint8_t addr_a8;
uint32_t i = 0;
// if ((Dst+NByte > MAX_ADDR)||(NByte == 0)) return (ERROR);
addr_a8 = (Dst & 0xffffff00)>>5;
SPI_FLASH_WP_UNLOCK();
SPI_FLASH_CS_LOW();
FM25L04B_ReadWriteByte(READ_CMD|addr_a8);
FM25L04B_ReadWriteByte(Dst & 0x00ff);
for (i = 0; i < NByte; i++)
{
RcvBufPt[i] = FM25L04B_ReadWriteByte(0xFF);
}
SPI_FLASH_CS_HIGH();
SPI_FLASH_WP_LOCK();
return 0;
}
- 写FM25L04B
FM25L04B的写时序
uint8_t FM25L04B_Write(uint32_t Dst, uint8_t* SndbufPt,uint32_t NByte)
{
uint8_t addr_a8;
uint8_t temp = 0,StatRgVal = 0;
uint32_t i = 0;
// if (( (Dst+NByte-1 > MAX_ADDR)||(NByte == 0) ))
// {
// return (ERROR); /* ?????? */
// }
addr_a8 = (Dst & 0xffffff00)>>5;
SPI_FLASH_WP_UNLOCK();
SPI_FLASH_CS_LOW();
FM25L04B_ReadWriteByte(RDSR_CMD);
temp = FM25L04B_ReadWriteByte(0xFF);
SPI_FLASH_CS_HIGH();
for(i = 0; i < NByte; i++)
{
SPI_FLASH_CS_LOW();
FM25L04B_ReadWriteByte(WREN_CMD);
SPI_FLASH_CS_HIGH();
SPI_FLASH_CS_LOW();
FM25L04B_ReadWriteByte(WRITE_CMD|addr_a8);
FM25L04B_ReadWriteByte((Dst+i)& 0x00ff);
FM25L04B_ReadWriteByte(SndbufPt[i]);
SPI_FLASH_CS_HIGH();
do
{
SPI_FLASH_CS_LOW();
FM25L04B_ReadWriteByte(RDSR_CMD);
StatRgVal = FM25L04B_ReadWriteByte(0xFF);
SPI_FLASH_CS_HIGH();
}
while (StatRgVal == 0x03 );
}
SPI_FLASH_CS_LOW();
FM25L04B_ReadWriteByte(WREN_CMD);
SPI_FLASH_CS_HIGH();
SPI_FLASH_CS_LOW();
FM25L04B_ReadWriteByte(WRSR_CMD);
FM25L04B_ReadWriteByte(temp);
SPI_FLASH_CS_HIGH();
SPI_FLASH_WP_LOCK();
return 0;
}
- 测试代码
uint8_t Recv_buf[16];
uint8_t Send_buf[16] = {0x01,0x02,0x05,0xe4};
int main(void)
{
SystemInit();
LPC17xx_SPI_Init();
FM25L04B_Read(0x0,Recv_buf, sizeof(Recv_buf));
FM25L04B_Write(0x0,Send_buf, sizeof(Send_buf));
memset(Recv_buf,0,sizeof(Recv_buf));
FM25L04B_Read(0x0,Recv_buf, sizeof(Recv_buf));
while(1) ;
}
时间: 2024-10-11 03:31:44