2012-10-13 09:39 AM
hi all,i am working on external flash memory and stm32f100c8 communication via SPI,but in my code there is a wrong what i missed.perhaps you may see..
/*---------------------------------*/#include ''stm32f10x.h''#include ''stm32f10x_spi.h''#include ''sFlash.h'' //sst25vf016b drivers/*-------------------------------------------------------------*/#define FLASH_WriteAddress 0x700000 //flash memory address#define FLASH_ReadAddress FLASH_WriteAddress#define FLASH_SectorToErase FLASH_WriteAddress#define BufferSize (countof(Tx_Buffer)-1)#define countof(a) (sizeof(a) / sizeof(*(a)))uint8_t Tx_Buffer[] = ''HELLO'';//data to given memory addressuint8_t Rx_Buffer[BufferSize];//read from given memory address/*---------------------------------*//*---------------------------------*//*function prototypes*/void RCC_Configuration(void);void GPIO_Configuration(void);void SPI_Configuration(void);/*---------------------------------*//*---------------------------------*/void RCC_Configuration(void){ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); }/*---------------------------------*//*---------------------------------*/void GPIO_Configuration(void){ GPIO_InitTypeDef GPIO_InitStructure; //sck-5 //mosi 7 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //out pushpull GPIO_Init(GPIOA, &GPIO_InitStructure); //nss 4 chip select GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //out pushpull GPIO_Init(GPIOA, &GPIO_InitStructure); //miso 6 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//In floating GPIO_Init(GPIOA, &GPIO_InitStructure);}/*---------------------------------*//*---------------------------------*/void SPI_Configuration(void){ SPI_InitTypeDef SPI_InitStructure; GPIO_ResetBits(GPIOA,GPIO_Pin_5); sFLASH_CS_HIGH(); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); SPI_Cmd(SPI1, ENABLE); }/*---------------------------------*//*---------------------------------*/int main(){ RCC_Configuration(); GPIO_Configuration(); SPI_Configuration(); /*erase chip*/ sFLASH_EraseBulk(); /*write tx buffer to Address with size*/ sFLASH_WriteBuffer(Tx_Buffer, FLASH_WriteAddress, BufferSize); /*read buffer from given memory address*/ sFLASH_ReadBuffer(Rx_Buffer, FLASH_ReadAddress, BufferSize); while(1);}when i write ''hello'' in TX,i see a strange char ''ÿ
''(dec 255-hex 0xFF) in RX buffer.But normally should ''hello''..thanks.2012-10-13 11:05 AM
So it sounds like it reads back blank/erased memory.
Perhaps you need to disable block/write protection, or check the data sheet if there is some magic settings you need to send to initialize or unlock. Check things like the chip ID, make sure that works, and is correct. Check the status register, see if that looks valid, and check the block and write protection settings reported.2012-10-14 03:21 AM
thanks for suggestions clive,i will try them
2012-10-17 10:22 AM
hi clive,i tried suggestions,i read the Flash ID it has no problem but this time i can only read ''H'' of ''HELLO'' th others are ''FF'' i mean
''
ÿ.
i guess i am writing only ''H'' of ''HELLO'' to memory.do you have any idea why is that..here is my updated code..:#define FLASH_WriteAddress 0x700000 //flash memory address#define FLASH_ReadAddress FLASH_WriteAddress#define FLASH_SectorToErase FLASH_WriteAddress#define sFLASH_ID 0x00BF2541 //flash ID for sst25 16mbit family/*-------------------------------------------------------------*//*-------------------------------------------------------------*/#define BufferSize (countof(Tx_Buffer)-1)#define countof(a) (sizeof(a) / sizeof(*(a)))/*-------------------------------------------------------------*//*-------------------------------------------------------------*/uint8_t Tx_Buffer[] = ''HELLO''; //data to given memory addressuint8_t Rx_Buffer[BufferSize]; //read from given memory address__IO uint32_t FlashID=0;/*---------------------------------*//*---------------------------------*//*function prototypes*/void RCC_Configuration(void);void GPIO_Configuration(void);void SPI_Configuration(void);void EWSR(void); //enable write status registervoid WRSR(uint8_t byte);/*---------------------------------*//*---------------------------------*/void RCC_Configuration(void){ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);}/*---------------------------------*//*---------------------------------*/void GPIO_Configuration(void){ GPIO_InitTypeDef GPIO_InitStructure; //sck pin 5-mosi pin 7 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //out pushpull GPIO_Init(GPIOA, &GPIO_InitStructure); //nss pin(CS) 4 chip select GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //out pushpull GPIO_Init(GPIOA, &GPIO_InitStructure); //miso pin 6 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//In floating GPIO_Init(GPIOA, &GPIO_InitStructure); //led GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //out pushpull GPIO_Init(GPIOB, &GPIO_InitStructure);}/*---------------------------------*//*---------------------------------*/void SPI_Configuration(void){ SPI_InitTypeDef SPI_InitStructure; sFLASH_CS_HIGH(); //Deselect flash-chip select pin high SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); SPI_Cmd(SPI1, ENABLE); }/*---------------------------------*//*---------------------------------*/int main(){ long int i; RCC_Configuration(); GPIO_Configuration(); SPI_Configuration(); FlashID = sFLASH_ReadID(); //get Flash ID if(FlashID==sFLASH_ID){ //if flash ID matches do operation GPIO_SetBits(GPIOB, GPIO_Pin_14); //blink led until op.complete EWSR(); WRSR(0x00); /*erase sector chip*/ sFLASH_EraseSector(FLASH_SectorToErase); /*write tx buffer to Address with size*/ sFLASH_WriteBuffer(Tx_Buffer, FLASH_WriteAddress, BufferSize); /*read buffer from given memory address*/ sFLASH_ReadBuffer(Rx_Buffer, FLASH_ReadAddress, BufferSize); for(i=0;i<10000000;i++); GPIO_ResetBits(GPIOB, GPIO_Pin_14); //led off } while(1);}2012-10-17 10:27 AM
to be migrated, sourceId: 25556:697285D7-A9CA-445D-B16C-F23BF0E3B1A3
2012-10-17 12:17 PM
Absent HW I can't really assist you.
2015-06-24 06:06 AM
You can not write multiple adress witout
Auto Address Increment (AAI) Word-Program Sequence This is the code /************************************************************************/ /* PROCEDURE: WRDI */ /************************************************************************/ void WRDI() { sFLASH_CS_LOW(); /* enable device */ YYCM_SpiPut(0x04); /* send WRDI command */ sFLASH_CS_HIGH(); /* disable device */ } /************************************************************************/ /* PROCEDURE: Auto_Add_IncA */ //------Dikkat Sadece 2 nin katlari seklinde yazabiliyor AUTO increment // Auto Address Increment (AAI) Word-Program Sequence with Software End-of-Write Detection /************************************************************************/ void sFLASH_WritePage_AutoINC(uint8_t* pBuffer,unsigned long Dst,uint16_t NumByteToWrite) { //EBSY(); /* Enable SO as RY/BY# output if in AAI */ sFLASH_WriteEnable(); sFLASH_CS_LOW(); /* enable device */ YYCM_SpiPut(0xAD); /* send AAI command */ YYCM_SpiPut(((Dst & 0xFFFFFF) >> 16)); /* send 3 address bytes */ YYCM_SpiPut(((Dst & 0xFFFF) >> 8)); YYCM_SpiPut(Dst & 0xFF); YYCM_SpiPut(*pBuffer); /* send 1st byte to be programmed */ pBuffer++; YYCM_SpiPut(*pBuffer); /* send 2nd byte to be programmed */ pBuffer++; sFLASH_CS_HIGH(); /* disable device */ NumByteToWrite=NumByteToWrite-2; while (NumByteToWrite) //!!!Dikkat Bu Sekilde Calismiyor Otomatik Aderss artt1rma vs yok!!! { sFLASH_CS_LOW(); /* enable device */ YYCM_SpiPut(0xAD); /* send AAI command */ YYCM_SpiPut(*pBuffer); /* send 1st byte to be programmed */ pBuffer++; YYCM_SpiPut(*pBuffer); /* send 2nd byte to be programmed */ pBuffer++; sFLASH_CS_HIGH(); /* disable device */ NumByteToWrite=NumByteToWrite-2; } WRDI(); /* Exit AAI before executing DBSY */ //DBSY(); /* disable SO as RY/BY# output if in AAI */ sFLASH_WaitForWriteEnd(); }