cancel
Showing results for 
Search instead for 
Did you mean: 

using flash as eeprom

ezrab
Associate II
Posted on October 19, 2010 at 15:14

hi.

i found out that it is possiable to use the flash as eeprom but the example i found was very long and complicated is there asimple way to do that?

thanks.
16 REPLIES 16
ezrab
Associate II
Posted on October 19, 2010 at 15:49

i tried that code but it does not work why?

#include ''stm32F10x.h''

#define CR_PG_Reset              ((uint32_t)0x00001FFE)

void setsystemclock(void);

void uartfunction(void);

uint8_t TxBuffer[] = ''Buffer Send from USART3 to USARTz using Flags'';

uint8_t TxCounter = 0, RxCounter = 0;

/* EEPROM start address in Flash */

#define EEPROM_START_ADDRESS    ((uint32_t)0x08010000) /* EEPROM emulation start address:

                                                  after 64KByte of used Flash memory */

#define FLASH_KEY1               ((uint32_t)0x45670123)

#define FLASH_KEY2               ((uint32_t)0xCDEF89AB)

int main(void)

{

char byte_temp=0;

int Address=0;

    setsystemclock(); //set system clock to 24Mhz

    RCC->APB2ENR =RCC_APB2RSTR_IOPCRST | RCC_APB2RSTR_AFIORST;//port C clock and alternative clock enable. page 79

    RCC->APB1ENR =RCC_APB1ENR_USART3EN ;

      GPIOC->CRH=GPIO_CRH_MODE10 | GPIO_CRH_CNF10_1 |  GPIO_CRH_CNF11_0 ;//PC11 in input floating mode for RX. PC10 Alternative Output TX.

AFIO->MAPR=AFIO_MAPR_USART3_REMAP_0 ;//port remap as uart //PC10 TX PC11 RX

    USART3->CR1 =USART_CR1_RE | USART_CR1_TE; //Receiver enable, Transmitter enable, USART disable

    USART3->BRR=0x9C4;//9600 at 24Mhz

    USART3->CR1 |= USART_CR1_UE;; //Uart Enable;

    //flash configuration

    FLASH->KEYR = FLASH_KEY1;

    FLASH->KEYR = FLASH_KEY2;

    FLASH->CR |= FLASH_CR_PG; //flash programing

    Address=EEPROM_START_ADDRESS +1;

    *(__IO uint16_t*)Address = '6';

    /* Wait for last operation to be completed */

    while((FLASH->SR&FLASH_SR_BSY) >0);

      /* if the program operation is completed, disable the PG Bit */

      FLASH->CR &= CR_PG_Reset;

  while(1)

  {

   if (((USART3->SR)&USART_SR_RXNE)>0)

    {

    byte_temp= USART3->DR;

     if (((USART3->SR)&USART_SR_TXE)>0)

       USART3->DR = (*(__IO uint16_t*)(Address - 2));;

    }

 /*   while(TxCounter < 30)

  {

     USART3->DR =('5');

      while(((USART3->SR)&USART_SR_TXE)==0);// Loop until USARTy DR register is empty

      TxCounter++;

  }

  TxCounter=0;*/

  }

}

void setsystemclock(void)

{

      RCC->CR |= RCC_CR_HSEON;

    // Wait until it's ready

    while ((RCC->CR & RCC_CR_HSERDY) == 0)

        ;

    // Select PREDIV1 as PLL source and sett PLL mul to 3 (set bit 0)

    // for 8*3 = 24 MHz

    RCC->CFGR |= RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL_0;

    // Start PLL

    RCC->CR |= RCC_CR_PLLON;

    // Wait until it's ready

    while ((RCC->CR & RCC_CR_PLLRDY) == 0)

        ;

    // Select PLL as system clock

    RCC->CFGR |= RCC_CFGR_SW_PLL;

    // Here we can check if PLL is used, and maybe disable HSI

    // Disable HSI

    RCC->CR &= ~RCC_CR_HSION;

    RCC->CFGR|=RCC_CFGR_MCO_2; //sys clock output

}

thanks,

STM32 hobbyist
Associate III
Posted on October 19, 2010 at 16:32

Hi,

Have you already referred to

the AN2594 ''EEPROM emulation in in STM32F10x microcontrollers ''

?

Hope this can help you

M3allem

ezrab
Associate II
Posted on October 19, 2010 at 16:36

hi

thanks for your replay i did saw the

AN2594 and tryied to work accordanly but it is very complicatied with many many function one leading to the other .

i am trying  to  understand the simple way to write one byte to the flash and read it when nessery thats it.

thnaks.

STM32 hobbyist
Associate III
Posted on October 19, 2010 at 16:50

Yes, writing a byte into the flash as an EEPROM is not simple as you imagine. There is a complete procedure to do that. You should also keep in mind that the number of write cycles is limited (I think 10000 cycles) and you should not abuse of writing usage.

B.R.

M3allem

Posted on October 19, 2010 at 17:17

i tried that code but it does not work why?

 

 

    Address=EEPROM_START_ADDRESS +1;

 

    *(__IO uint16_t*)Address = '6';

You can't write WORDs to ODD addresses

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ezrab
Associate II
Posted on October 19, 2010 at 17:32

i changed the

 Address=EEPROM_START_ADDRESS +2;

    *(__IO uint16_t*)Address = 5;

but i still get to the void HardFault_Handler(void) routine.

i know it not simple but i still dont know how to do it without endless routines.

thanks.

Posted on October 19, 2010 at 21:21

This seem relatively serviceable using Keil, and writes to the flash, although I don't have a USART3 serial port. I've fixed the PLL code as I've previously suggested. If you want to locate faulting instructions, step through the code, you don't have much of it. Also you will need to work on erasing.

#include ''stm32F10x.h''

#define CR_PG_Reset              ((uint32_t)0x00001FFE)

void setsystemclock(void);

/* EEPROM start address in Flash */

#define EEPROM_START_ADDRESS    ((uint32_t)0x08010000) /* EEPROM emulation start address:

                                                  after 64KByte of used Flash memory */

#define FLASH_KEY1               ((uint32_t)0x45670123)

#define FLASH_KEY2               ((uint32_t)0xCDEF89AB)

int main(void)

{

  char byte_temp=0;

  int Address=0;

  setsystemclock(); //set system clock to 24Mhz

  RCC->APB2ENR |= RCC_APB2RSTR_IOPCRST | RCC_APB2RSTR_AFIORST;//port C clock and alternative clock enable. page 79

  RCC->APB1ENR |= RCC_APB1ENR_USART3EN ;

  GPIOC->CRH=GPIO_CRH_MODE10 | GPIO_CRH_CNF10_1 |  GPIO_CRH_CNF11_0 ;//PC11 in input floating mode for RX. PC10 Alternative Output TX.

  AFIO->MAPR=AFIO_MAPR_USART3_REMAP_0 ;//port remap as uart //PC10 TX PC11 RX

  USART3->CR1 =USART_CR1_RE | USART_CR1_TE; //Receiver enable, Transmitter enable, USART disable

  USART3->BRR = 0x9C4;//9600 at 24Mhz

  USART3->CR1 |= USART_CR1_UE; //Uart Enable;

  //flash configuration

  FLASH->KEYR = FLASH_KEY1;

  FLASH->KEYR = FLASH_KEY2;

  FLASH->CR |= FLASH_CR_PG; //flash programing

  Address = EEPROM_START_ADDRESS + 2;

  *(__IO uint16_t*)Address = '6';

  /* Wait for last operation to be completed */

  while((FLASH->SR&FLASH_SR_BSY) >0);

  /* if the program operation is completed, disable the PG Bit */

  FLASH->CR &= CR_PG_Reset;

  while(1)

  {

   if (((USART3->SR) & USART_SR_RXNE)>0)

    {

    byte_temp += USART3->DR;

     if (((USART3->SR) & USART_SR_TXE)>0)

       USART3->DR = (*(__IO uint16_t*)Address);

    }

  }

}

void setsystemclock(void)

{

  RCC->CR |= RCC_CR_HSEON;

  // Wait until it's ready

  while ((RCC->CR & RCC_CR_HSERDY) == 0)

        ;

  // Select PREDIV1 as PLL source and sett PLL mul to 3 (set bit 0)

  // for 8*3 = 24 MHz

  RCC->CFGR |= RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL_0;

  // Start PLL

  RCC->CR |= RCC_CR_PLLON;

  // Wait until it's ready

  while ((RCC->CR & RCC_CR_PLLRDY) == 0)

        ;

  // Select PLL as system clock

  RCC->CFGR = (RCC->CFGR & ~RCC_CFGR_SW) | RCC_CFGR_SW_PLL;

  // Wait until PLL is used as system clock source

  while ((RCC->CFGR & RCC_CFGR_SWS) != 0x08)

        ;

  // Here we can check if PLL is used, and maybe disable HSI

  // Disable HSI

  RCC->CR &= ~RCC_CR_HSION;

  RCC->CFGR|=RCC_CFGR_MCO_2; //sys clock output

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ezrab
Associate II
Posted on October 20, 2010 at 08:05

i have done astep by step run and the program fails when it gets to the line:

 while((FLASH->SR&FLASH_SR_BSY) >0);

and then it gets to hard fault.

i also tried the example from ST but that didnt work either.

thanks.

ezrab
Associate II
Posted on October 20, 2010 at 08:28

when i run the program if it does not get to hardfualt i get that the stack is 100% full for some reason.