Subversion Repositories EDIS_Ignition

Rev

Rev 10 | Go to most recent revision | Details | 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
 
22
// NVRAM hardware erases to all 0x
23
nvram_info_t const HARDWARE_ERASED = {.u16 = 0UL};
24
// Marked as erased change to all 1
25
nvram_info_t const MARKED_ERASED = {.u16 = ~0UL};
26
 
27
// allocate the memory for storing the NVRAM data
28
nvram_info_t NVRAM_Base[NVRAM_WORDS] __attribute__((section(".nvram"))); // set by linker
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
{
41
  FLASH_EraseInitTypeDef erase = {.TypeErase = FLASH_TYPEERASE_PAGES, .PageAddress = Address, .NbPages = 1};
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
 
56
  /* search blank */
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
    {
63
      // erase previous data
64
      WriteNVRAM(&NVRAM_Base[index], MARKED_ERASED);
65
      base = ptr;
66
      break;
67
    }
68
  }
69
  // search forward for next erased or empty element, use it
70
  for (int offset = 1; offset < NVRAM_WORDS + 1; offset++)
71
  {
72
    int index = (base + offset) % NVRAM_WORDS;
73
 
74
    if (NVRAM_Base[index].u16 == HARDWARE_ERASED.u16)
75
    {
76
      // if we cross a page by incrementing into it need action
77
      if (index % NVRAM_PAGESIZE == 0)
78
        pageCrossed = 1;
79
 
80
      WriteNVRAM(&NVRAM_Base[index], data);
81
      // now check to see if it actually wrote correctly
82
      if (NVRAM_Base[index].u16 != data.u16)
83
      {
84
        WriteNVRAM(&NVRAM_Base[index], MARKED_ERASED); // Set to all 1 if the data did not write properly
85
        continue;
86
      }
87
      // record where the data was written
88
      *pIndex = index;
89
      return pageCrossed;
90
    }
91
  }
92
  // if it gets here, failure.
93
  return 0;
94
}
95
 
96
void write_nvram_data(nvram_info_t data)
97
{
98
  int index = 0;
99
  // try the easy case - returns 0 , no further action
100
  if (!_write_nvram_data(data, &index))
101
    return;
102
  // Returns 1, crossed page boundary with write of new data. 
103
  // Now have the index pointing into the page where we need to copy the data to
104
  // for any data in the other page, copy it to the new page
105
  int oldPageBase = index >= NVRAM_PAGESIZE ? 0 : NVRAM_PAGESIZE;
106
  for (int i = oldPageBase; i < oldPageBase + NVRAM_WORDS; i++)
107
  {
108
    if (NVRAM_Base[i].u16 != HARDWARE_ERASED.u16 && NVRAM_Base[i].u16 != MARKED_ERASED.u16)
109
    {
110
      int base = i;
111
      // copy data forwards
112
      _write_nvram_data(NVRAM_Base[i], &base);
113
    }
114
    // erase the page we just copied out of
115
    EraseNVRAM(&NVRAM_Base[oldPageBase]);
116
  }
117
}
118
 
119
nvram_info_t *find_nvram_data(uint8_t searchTag)
120
{
121
  for (int ptr = 0; ptr < NVRAM_WORDS; ptr++)
122
  {
123
    if (NVRAM_Base[ptr].data.tag == searchTag)
124
      return &NVRAM_Base[ptr];
125
  }
126
  return NULL;
127
}