ogulcan123
Member level 5
- Joined
- Apr 23, 2011
- Messages
- 81
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,286
- Location
- Istanbul, Turkey
- Activity points
- 2,024
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 [I] /* Initialize SPI1 for SPI Flash */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); SPI_InitTypeDef SPI_InitStruct; SPI_InitTypeDef SPI_InitStructure; SPI_Init(SPI1, &SPI_InitStruct); /* Set SPI1 MOSI pin (PB5) as output */ GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Set SPI1 MISO pin (PB4) as input */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); SPI_Cmd(SPI1, ENABLE); DMA_InitTypeDef DMA_InitStruct; DMA_Init(DMA1_Stream0, &DMA_InitStruct); FLASH_Unlock();[/I]
unsigned char SPI_EEPROM_JEDEC_CHECK (SPI_TypeDef* SPIx)
{
unsigned char tmp=0;
PIN_OFF(CS_Pin);
SPI_SendByte(SPIx, JEDEC_ID_CMD);
if (SPI_ReadByte(SPIx,0)!=0xBF) tmp++;
if (SPI_ReadByte(SPIx,0)!=0x25) tmp++;
if (SPI_ReadByte(SPIx,0)!=0x41) tmp++;
PIN_ON(CS_Pin);
if (tmp) return JEDEC_ERROR_MISMATCH; else return SUCCESS;
}
void SPI_EEPROM_SEND_CMD (SPI_TypeDef* SPIx, unsigned char CMD)
{
PIN_OFF(CS_Pin);
SPI_SendByte(SPIx, CMD);
PIN_ON(CS_Pin);
}
unsigned char SPI_EEPROM_FULL_ERASE (SPI_TypeDef* SPIx, unsigned long capacity)
{
SPI_EEPROM_WRITE_REG(SPIx,WRITE_STATUS_CMD, 0);
SPI_EEPROM_SEND_CMD(SPIx, WRITE_ENABLE_CMD);
PIN_OFF(CS_Pin);
SPI_SendByte(SPIx, FULLERASE_CMD);
delay_ms(CHIP_ERASE_TIME_ms);
PIN_ON(CS_Pin);
return SPI_EEPROM_BLANK_CHECK(SPIx);
}
unsigned long SPI_EEPROM_WRITE_DATA (SPI_TypeDef* SPIx, unsigned long start_adrs, unsigned long page_size)
{
unsigned long cnt, tmp;
unsigned int dat;
SPI_EEPROM_SEND_CMD(SPIx, EWSR_CMD);
SPI_EEPROM_WRITE_REG(SPIx,WRITE_STATUS_CMD, 0);
SPI_EEPROM_SEND_CMD(SPIx, ENABLE_SO_CMD);
SPI_EEPROM_SEND_CMD(SPIx, WRITE_ENABLE_CMD);
PIN_OFF(CS_Pin);
SPI_SendByte(SPIx, AAI_PROG_CMD);
SPI_SendByte(SPIx, (0xFF0000&start_adrs)>>16);
SPI_SendByte(SPIx, (0x00FF00&start_adrs)>>8);
SPI_SendByte(SPIx, (0x0000FF&start_adrs)>>0);
for (cnt=0; cnt!=page_size; cnt++)
{
// SPI_SendByte(SPIx, (0xFF00&flower12[cnt])>>8);
// SPI_SendByte(SPIx, (0x00FF&flower12[cnt])>>0);
PIN_ON(CS_Pin);
PIN_OFF(CS_Pin);
while (SPI_ReadByte(SPIx,0)!=0xFF);
PIN_ON(CS_Pin);
PIN_OFF(CS_Pin);
SPI_SendByte(SPIx, AAI_PROG_CMD);
}
PIN_ON(CS_Pin);
SPI_EEPROM_SEND_CMD(SPIx, WRITE_DISABLE_CMD);
SPI_EEPROM_SEND_CMD(SPIx, DISABLE_SO_CMD);
tmp=0;
/*
PIN_OFF(CS_Pin);
SPI_SendByte(SPIx, FASTREAD_CMD);
SPI_SendByte(SPIx, (0xFF0000&start_adrs)>>16);
SPI_SendByte(SPIx, (0x00FF00&start_adrs)>>8);
SPI_SendByte(SPIx, (0x0000FF&start_adrs)>>0);
for (cnt=0; cnt!=page_size; cnt++)
{
//dat=(SPI_ReadByte(SPIx,0)<<8)|(SPI_ReadByte(SPIx,0)<<0);
dat=(SPI_ReadByte(SPIx,0)<<0)|(SPI_ReadByte(SPIx,0)<<8);
if (dat!=flower1[cnt]) tmp++;
//if (SPI_ReadByte(SPIx,0)!=0xFF) tmp++;
}
PIN_ON(CS_Pin);
*/
return tmp;
}
unsigned char SPI_EEPROM_STATUS_READ (SPI_TypeDef* SPIx)
{
unsigned char tmp;
PIN_OFF(CS_Pin);
SPI_SendByte(SPIx, READ_STATUS_CMD);
tmp=SPI_ReadByte(SPIx, 0);
PIN_ON(CS_Pin);
return tmp;
}
void SPI_EEPROM_WRITE_REG (SPI_TypeDef* SPIx, unsigned char REG, unsigned char DAT)
{
PIN_OFF(CS_Pin);
SPI_SendByte(SPIx, REG);
SPI_SendByte(SPIx, DAT);
PIN_ON(CS_Pin);
}
unsigned long SPI_EEPROM_BLANK_CHECK (SPI_TypeDef* SPIx)
{
unsigned long cnt, tmp;
tmp=0;
PIN_OFF(CS_Pin);
SPI_SendByte(SPIx, FASTREAD_CMD);
SPI_SendByte(SPIx, 0);
SPI_SendByte(SPIx, 0);
SPI_SendByte(SPIx, 0);
for (cnt=0; cnt!=LASTADDRESS; cnt++) if (SPI_ReadByte(SPIx,0)!=0xFF) tmp++;
PIN_ON(CS_Pin);
if (tmp<10) tmp=0;
return tmp;
}
void SPI_EEPROM_WRITE_BYTEPAGE (SPI_TypeDef* SPIx, unsigned long start_adrs, unsigned long page_size, unsigned char * data)
{
unsigned long cnt;
SPI_EEPROM_SEND_CMD(SPIx, EWSR_CMD);
SPI_EEPROM_WRITE_REG(SPIx,WRITE_STATUS_CMD, 0);
SPI_EEPROM_SEND_CMD(SPIx, ENABLE_SO_CMD);
SPI_EEPROM_SEND_CMD(SPIx, WRITE_ENABLE_CMD);
PIN_OFF(CS_Pin);
SPI_SendByte(SPIx, AAI_PROG_CMD);
SPI_SendByte(SPIx, (0xFF0000&start_adrs)>>16);
SPI_SendByte(SPIx, (0x00FF00&start_adrs)>>8);
SPI_SendByte(SPIx, (0x0000FF&start_adrs)>>0);
for (cnt=0; cnt!=(page_size>>1); cnt++)
{
SPI_SendByte(SPIx, *data++);
SPI_SendByte(SPIx, *data++);
PIN_ON(CS_Pin);
PIN_OFF(CS_Pin);
while (SPI_ReadByte(SPIx,0)!=0xFF);
PIN_ON(CS_Pin);
PIN_OFF(CS_Pin);
SPI_SendByte(SPIx, AAI_PROG_CMD);
}
PIN_ON(CS_Pin);
SPI_EEPROM_SEND_CMD(SPIx, WRITE_DISABLE_CMD);
SPI_EEPROM_SEND_CMD(SPIx, DISABLE_SO_CMD);
}
void SPI_EEPROM_READ_STRING (SPI_TypeDef* SPIx, unsigned long start_adrs,
unsigned char * data, unsigned long String_Len)
{
unsigned long cnt;
PIN_OFF(CS_Pin);
SPI_SendByte(SPIx, FASTREAD_CMD);
SPI_SendByte(SPIx, (0xFF0000&start_adrs)>>16);
SPI_SendByte(SPIx, (0x00FF00&start_adrs)>>8);
SPI_SendByte(SPIx, (0x0000FF&start_adrs)>>0);
for (cnt=0;cnt!=String_Len;cnt++) *data++=SPI_ReadByte(SPIx,0);
PIN_ON(CS_Pin);
}
I'm actually using micro SD card for data logs so that in an emergency situation, the logs can be read plugging into a PC.If the memory you are using only has (a minimum) 100,000 erase/write cycles, then why not use a removable device such as an SD card or USB drive.
/* Initialize SPI1 for SPI Flash */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); // SPI1 is located in APB2 bus
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master; // The device is master
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // The Winbond W25Q64FVSSIG is 8 bits
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; // SPI_CPOL_Low OR SPI_CPOL_High
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge; // Choose clock phase: SPI_CPHA_1Edge OR SPI_CPHA_2Edge
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft; // Slave select: SPI_NSS_Soft OR SPI_NSS_Hard
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStruct.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStruct);
/* Set SPI1 MOSI pin (PB5) as output */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; // SPI speed is 42 Mbit/s, 8 bits data size => Less than 42/8=5.25
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Set SPI1 MISO pin (PB4) as input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
SPI_Cmd(SPI1, ENABLE);
DMA_Init(DMA1_Stream0, &DMA_InitStruct);
i_spi=0;
for(spi_address=0x40013021;spi_address<=0x400133FF;spi_address++)
{
FLASH_Unlock();
status = FLASH_ProgramByte(spi_address, i_spi);
FLASH_Lock();
SPI_buffer = *(uint8_t*) spi_address;
if(SPI_buffer!=0){
TRACE_INFO("\r\n**SPI** 0x%08x W:%d R:%d", spi_address, i_spi, SPI_buffer);
if (status==8) {
TRACE_INFO(" OK");
}
else {
TRACE_INFO(" FAILED!");
}
}
i_spi++;
i_spi = i_spi%256;
}
you need to map PB3 as SPI1_SCK as well. You mention NSS - that will need to be mapped also.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?