zigbee cc2530操作flash,以cc2530读flash_id为例子;
void InitIO()
{
CLKCONCMD &= ~0x40; //设置系统时钟源为32MHZ晶振
while(CLKCONSTA & 0x40); //等待晶振稳定为32M
CLKCONCMD &= ~0x47; //设置系统主时钟频率为32MHZ
UartInitial();
// CS P1_3
P1DIR |= 1<<3;
P1INP |= 1<<3;
P1SEL &= ~(1<<3);
// IC_SCK P1_5
P1DIR |= 1<<5;
P1INP |= 1<<5;
P1SEL &= ~(1<<5);
// IC_MOSI P1_6
P1DIR |= 1<<6;
P1INP |= 1<<6;
P1SEL &= ~(1<<6);
// IC_MISO P1_7
P1DIR |= 1<<7;
P1INP |= 1<<7;
P1SEL &= ~(1<<7);
P1DIR |= 1<<2;
P1INP |= 1<<2;
P1SEL &= ~(1<<2);
SPI_CS = 1;
SPI_SCK = 1;
}
操作写一个字节数据
/************************************************
函数名称 : Write_SPI_Byte
功 能 : SPI写读一个字节
参 数 : Byte ---- 数据
返 回 值 : Byte ---- 数据
*************************************************/
uint8_t Write_SPI_Byte( uint8_t Byte )
{
uint8_t i;
SPI_SCK = 1;
for(i = 0;i < 8;i++)
{
SPI_SCK = 0;
Delay_1us(WAIT_TIME); // 空等待
#if 0
SPI_MOSI = ((Byte & 0x80) >> 7);
#else
if(Byte & 0x80)
{
SPI_MOSI = 1;
}
else
{
SPI_MOSI = 0;
}
#endif
Byte <<= 1;
Delay_1us(WAIT_TIME); // 空等待
SPI_SCK = 1;
Delay_1us(WAIT_TIME); // 空等待
//Byte |= SPI_MISO;
}
return Byte;
}
操作读flash数据
/************************************************
函数名称 : Read_SPI_Byte
功 能 : SPI只读一个字节
参 数 : 无
返 回 值 : temp ---- 数据
*************************************************/
uint8_t Read_SPI_Byte(void)
{
uint8_t i;
uint8_t temp = 0;
SPI_SCK = 1;
for(i = 0;i < 8;i++)
{
SPI_SCK = 0;
Delay_1us(WAIT_TIME); // 空等待
temp <<= 1;
#if 1
temp |= SPI_MISO;
#else
if(SPI_MISO)
{
temp++;
}
#endif
SPI_SCK = 1;
Delay_1us(WAIT_TIME); // 空等待
}
return temp;
}
测试以读取flash_id为例子,需要写0x90然后读两个字节
uint16_t read_flash_id(uint8_t command)
{
uint16_t id = 0;
uint8_t data[2] = {0};
SPI_CS = 1;
SPI_CS = 0;
Write_SPI_Byte(command);
Write_SPI_Byte(0xFF);
Write_SPI_Byte(0xFF);
Write_SPI_Byte(0x00);
for(int i = 0; i < 2 ; i++)
{
data[i] = Read_SPI_Byte();
//data++;
}
SPI_CS = 1;
return data[0] >> 8 + data[1];
}
然后读指令我门用刚才提到的0x90:
void main()
{
InitIO();
Delay_1s(1);
SPI_CS = 0;
read_flash_id(0x90);
}
最后通过逻辑分析仪抓取flash接口可以看到操作ok!