#define SPI_CS_LOW (GPIOA->BRR |=GPIO_Pin_5)
#define SPI_CS_HIGH (GPIOA->BSRR |=GPIO_Pin_5)
#define SPI_SCK_LOW (GPIOA->BRR |=GPIO_Pin_6)
#define SPI_SCK_HIGH (GPIOA->BSRR |=GPIO_Pin_6)
#define SPI_MISO_LOW (GPIOA->BRR |=GPIO_Pin_4)
#define SPI_MISO_HIGH (GPIOA->BSRR|=GPIO_Pin_4)
#define SPI_MISO_READ (GPIOA->IDR &GPIO_Pin_4)
#define SPI_MOSI_LOW (GPIOA->BRR |=GPIO_Pin_7)
#define SPI_MOSI_HIGH (GPIOA->BSRR|=GPIO_Pin_7)
void Init_45DB041(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//CS
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
//SCK
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOA,&GPIO_InitStruct);
//MISO
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_4;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;//加上拉电阻,其它没加
GPIO_Init(GPIOA,&GPIO_InitStruct);
//MOSI
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_7;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOA,&GPIO_InitStruct);
SPI_CS_HIGH;
SPI_SCK_HIGH;
}
void read_arry_byte(unsigned long address,unsigned char *d)//Á¬Ðø×Ö½Ú¶Á
{
unsigned char j=0,k=0,n;
__disable_irq();
n=strlen((const char*)d);//ÇóÊýÁ¿
SPI_SCK_HIGH;
delay_us(100);
SPI_CS_LOW;
//SPI_MISO_HIGH;
delay_us(100);
for(k=0;k<32;k++)//ËÍÃüÁî
{
SPI_SCK_LOW;
delay_us(100);
if((address&0x80000000)==0x80000000)
{ SPI_MOSI_HIGH;}
else
{ SPI_MOSI_LOW;}
delay_us(100);
SPI_SCK_HIGH;
address<<=1;
delay_us(100);
}
//SPI_SCK_HIGH;
//delay_us(100);
for(j=0;j<n;j++)//¶ÁÊý¾Ý
{
for(k=0;k<8;k++)
{
d[j]<<=1;
SPI_SCK_LOW;
delay_us(100);
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4))
{d[j]|=0x01;}
else
{d[j]&=0xFE;}
SPI_SCK_HIGH;
delay_us(100);
}
}
SPI_CS_HIGH;
SPI_SCK_LOW;
delay_us(100);
}
void write_page(unsigned long address,unsigned char *data)//Á¬Ðø×Ö½Úд
{
unsigned char j=0,k=0,n;
__disable_irq();
n=strlen((const char*)data);//ÇóÊýÁ¿
SPI_SCK_HIGH;
delay_us(100);
SPI_CS_LOW;
delay_us(100);
for(k=0;k<32;k++)//ËÍÃüÁî
{
SPI_SCK_LOW;
delay_us(100);
if((address&0x80000000)==0x80000000)
{ SPI_MOSI_HIGH;}
else
{ SPI_MOSI_LOW;}
delay_us(100);
SPI_SCK_HIGH;
address<<=1;
delay_us(100);
}
// SPI_SCK_HIGH;
// delay_us(100);
for(j=0;j<n;j++)//ËÍÊý¾Ý
{
for(k=0;k<8;k++)
{
SPI_SCK_LOW;
delay_us(100);
if((data[j]&0x80)==0x80)
{ SPI_MOSI_HIGH;}
else
{ SPI_MOSI_LOW;}
delay_us(100);
SPI_SCK_HIGH;
data[j]<<=1;
delay_us(100);
}
}
SPI_CS_HIGH;
SPI_SCK_LOW;
delay_us(100);
}
unsigned char Read_state(void)
{
unsigned char k=0,i=0xD7;
__disable_irq();
SPI_SCK_HIGH;
delay_us(100);
SPI_CS_LOW;
// SPI_MISO_HIGH;
delay_us(100);
for(k=0;k<8;k++)//ËÍÃüÁî
{
SPI_SCK_LOW;
delay_us(100);
if((i&0x80)==0x80)
{ SPI_MOSI_HIGH;}
else
{ SPI_MOSI_LOW;}
delay_us(100);
SPI_SCK_HIGH;
i<<=1;
delay_us(100);
}
i=0;
// SPI_SCK_HIGH;
// delay_us(100);
for(k=0;k<8;k++)//¶ÁÊý¾Ý
{
i<<=1;
SPI_SCK_LOW;
delay_us(100);
if(SPI_MISO_READ)
{i|=0x01;}
else
{i&=0xFE;}
SPI_SCK_HIGH;
delay_us(100);
}
SPI_CS_HIGH;
SPI_SCK_LOW;
delay_us(100);
return i;
}
void erase_45(void)
{
unsigned char j=0,i=0;
unsigned char erease_order[4]={0xc7,0x94,0x80,0x9a};//²Á³ýÃüÁî
__disable_irq();
SPI_SCK_HIGH;
delay_us(100);
SPI_CS_LOW;
delay_us(100);
while(j<4)
{
for(i=0;i<8;i++)
{
SPI_SCK_LOW;
delay_us(100);
if((erease_order[j]&0x80)==0x80)
{ SPI_MOSI_HIGH;}
else
{ SPI_MOSI_LOW; }
delay_us(100);
SPI_SCK_HIGH;
erease_order[j]<<=1;
delay_us(100);
}
j++;
}
SPI_CS_HIGH;
SPI_SCK_LOW;
delay_us(100);
}
void main()
{
u8 IC_Serial[4];//´æ´¢IC¿¨ÐòÁкÅ
u8 IC_ID[4];//¶ÁÈ¡IC¶Á¿¨Æ÷É豸IDºÅ
Init_45DB041();
IC_Serial[0]=0xDD;IC_Serial[1]=0xFF;IC_Serial[2]=0xFD;IC_Serial[3]=0xDD;
write_page(0x82000800,IC_Serial);
delay_ms(500);delay_ms(500);delay_ms(500);delay_ms(500); delay_ms(500);//
while((Read_state()&0x80)==0) delay_ms(5);
__enable_irq();
read_arry_byte(0x03000800,IC_ID);
__enable_irq();
sprintf((char*)p,"DC32(20,120,'%X-%X-%X-%X',15);\r\n",IC_ID[0],IC_ID[1],IC_ID[2],IC_ID[3]);//
atk_8266_send_cmd(p,"OK",200);
} |