Subversion Repositories EDIS_Ignition

Rev

Rev 9 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9 mjames 1
/*
2
 * nvram.c
3
 *
4
 *  Created on: 4 Jun 2017
5
 *  Converted to use two 1K pages of STM32L1 Block Erasable Flash memory
6
 *   instead of 4k of genuine word erasable NVRAM
7
 *    on 1 Mar 2023
8
 *      Author: Mike
9
 */
10
 
11
/* Includes ------------------------------------------------------------------*/
12
#include "stm32f1xx_hal.h"
13
 
14
#include "nvram.h"
15
 
16
// erase page size on STM32F103 is 1kbytes 
17
#define NVRAM_PAGESIZE (1024 / sizeof(nvram_info_t))
18
 
19
// decided to allocate 2 pages of Flash as NVRAM
20
#define NVRAM_WORDS (2 * NVRAM_PAGESIZE)
21
 
10 mjames 22
// Flash hardware erases to all 0
23
nvram_info_t const HARDWARE_ERASED = {.u16 = (uint16_t)~0U};
9 mjames 24
// Marked as erased change to all 1
10 mjames 25
nvram_info_t const MARKED_ERASED = {.u16 = (uint16_t)0U};
9 mjames 26
 
27
// allocate the memory for storing the NVRAM data
10 mjames 28
nvram_info_t NVRAM_Base[NVRAM_WORDS] __attribute__((section(".nvram"))) = { [0 ... NVRAM_WORDS-1].u16 = (uint16_t)~0U}; // set by linker
9 mjames 29
 
30
static void
31
WriteNVRAM(nvram_info_t *Address, nvram_info_t data)
32
{
33
  HAL_FLASH_Unlock();
34
  HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)Address, data.u16);
35
  HAL_FLASH_Lock();
36
}
37
 
38
static void
39
EraseNVRAM(nvram_info_t *Address)
40
{
10 mjames 41
  FLASH_EraseInitTypeDef erase = {.TypeErase = FLASH_TYPEERASE_PAGES, .PageAddress = (uint32_t)Address, .NbPages = 1};
9 mjames 42
  uint32_t err;
43
  HAL_FLASH_Unlock();
44
  HAL_FLASHEx_Erase(&erase, &err);
45
  HAL_FLASH_Lock();
46
}
47
 
48
// returns true if page crossed , used to trigger erasure of
49
// the other page after copying out all data from other page to new page
50
 
51
static uint8_t _write_nvram_data(nvram_info_t data, int *pIndex)
52
{
53
  int base = *pIndex;
54
  uint8_t pageCrossed = 0;
55
 
10 mjames 56
  // search for correct data 
9 mjames 57
  for (int ptr = 0; ptr < NVRAM_WORDS; ptr++)
58
  {
59
    int index = (ptr + base) % NVRAM_WORDS;
60
    // erase the entry just found
61
    if (NVRAM_Base[index].data.tag == data.data.tag)
62
    {
10 mjames 63
      // is this the same data as we are writing ? if so return immediately
64
      if(NVRAM_Base[index].u16 == data.u16)
65
        return 0;
66
      // different data
9 mjames 67
      // erase previous data
68
      WriteNVRAM(&NVRAM_Base[index], MARKED_ERASED);
10 mjames 69
      base = index+1; // look at next element 
9 mjames 70
      break;
71
    }
72
  }
10 mjames 73
  // search forward for next hardware erased element, use it
74
  for (int offset = 0; offset < NVRAM_WORDS; offset++)
9 mjames 75
  {
76
    int index = (base + offset) % NVRAM_WORDS;
77
 
78
    if (NVRAM_Base[index].u16 == HARDWARE_ERASED.u16)
79
    {
80
      // if we cross a page by incrementing into it need action
81
      if (index % NVRAM_PAGESIZE == 0)
82
        pageCrossed = 1;
83
 
84
      WriteNVRAM(&NVRAM_Base[index], data);
85
      // now check to see if it actually wrote correctly
86
      if (NVRAM_Base[index].u16 != data.u16)
87
      {
10 mjames 88
        WriteNVRAM(&NVRAM_Base[index], MARKED_ERASED); // Set to all erased if the data did not write properly
9 mjames 89
        continue;
90
      }
91
      // record where the data was written
92
      *pIndex = index;
93
      return pageCrossed;
94
    }
95
  }
96
  // if it gets here, failure.
97
  return 0;
98
}
99
 
100
void write_nvram_data(nvram_info_t data)
101
{
102
  int index = 0;
103
  // try the easy case - returns 0 , no further action
104
  if (!_write_nvram_data(data, &index))
105
    return;
106
  // Returns 1, crossed page boundary with write of new data. 
107
  // Now have the index pointing into the page where we need to copy the data to
108
  // for any data in the other page, copy it to the new page
109
  int oldPageBase = index >= NVRAM_PAGESIZE ? 0 : NVRAM_PAGESIZE;
110
  for (int i = oldPageBase; i < oldPageBase + NVRAM_WORDS; i++)
111
  {
112
    if (NVRAM_Base[i].u16 != HARDWARE_ERASED.u16 && NVRAM_Base[i].u16 != MARKED_ERASED.u16)
113
    {
114
      int base = i;
115
      // copy data forwards
116
      _write_nvram_data(NVRAM_Base[i], &base);
117
    }
118
    // erase the page we just copied out of
119
  }
10 mjames 120
  EraseNVRAM(&NVRAM_Base[oldPageBase]);
9 mjames 121
}
122
 
123
nvram_info_t *find_nvram_data(uint8_t searchTag)
124
{
125
  for (int ptr = 0; ptr < NVRAM_WORDS; ptr++)
126
  {
127
    if (NVRAM_Base[ptr].data.tag == searchTag)
128
      return &NVRAM_Base[ptr];
129
  }
130
  return NULL;
131
}