Appiko
sensepi_store_config.c
1 /*
2  * sensepi_store_config.c : Support file to store configs into NVMC memory
3  * Copyright (C) 2019 Appiko
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
19 
20 #include "sensepi_store_config.h"
21 
22 #include "hal_nop_delay.h"
23 #include "hal_nvmc.h"
24 
25 #include "nrf_util.h"
26 #include "nrf_assert.h"
27 #include "common_util.h"
28 
29 #include "log.h"
30 
32 #define LAST_APP_PAGE_ADDR 0x27000
33 
34 #define CONFIG_SIZE_TO_POINTER 5
35 
36 #define NO_OF_CONFIGS 204
37 
38 #define WORD_SIZE 4
39 
40 #define LAST_CONFIG_END_ADDR (LAST_APP_PAGE_ADDR+NO_OF_CONFIGS*CONFIG_SIZE_TO_POINTER\
41  * WORD_SIZE)
42 
44 #define CONFIG_FW_VER_LOC LAST_CONFIG_END_ADDR+0x2
45 
46 #define MEM_RESET_VALUE 0xFFFFFFFF
47 
48 #define MEM_FULL LAST_CONFIG_END_ADDR
49 
58 static uint32_t get_next_location (void);
59 
65 static void clear_all_config (void);
66 
70 static void update_fw_ver (void);
71 
72 static uint32_t get_next_location (void)
73 {
74  log_printf("%s\n",__func__);
75  uint32_t * p_mem_loc = (uint32_t *) LAST_APP_PAGE_ADDR;
76  while(p_mem_loc < (uint32_t *)LAST_CONFIG_END_ADDR)
77  {
78  if(*(p_mem_loc) == MEM_RESET_VALUE)
79  {
80  return (uint32_t)p_mem_loc;
81  }
82  p_mem_loc += CONFIG_SIZE_TO_POINTER;
83  }
84  return MEM_FULL;
85 
86 }
87 
89 {
90  log_printf("%s\n",__func__);
91  if(get_next_location () == LAST_APP_PAGE_ADDR)
92  {
93  return true;
94  }
95  else
96  {
97  return false;
98  }
99 }
100 
101 void sensepi_store_config_write (sensepi_config_t* latest_config)
102 {
103  log_printf("%s\n",__func__);
104  uint32_t * p_mem_loc = (uint32_t *) get_next_location();
105  if(p_mem_loc == (uint32_t *)MEM_FULL)
106  {
107  clear_all_config ();
108  p_mem_loc = (uint32_t *) get_next_location();
109  }
110  hal_nvmc_write_data(p_mem_loc, latest_config, sizeof(sensepi_config_t));
111 
112 }
113 
115 {
116  log_printf("%s\n",__func__);
117  uint32_t * p_mem_loc = (uint32_t*)get_next_location();
118  if(p_mem_loc != (uint32_t*)LAST_APP_PAGE_ADDR)
119  {
120  p_mem_loc -= CONFIG_SIZE_TO_POINTER;
121  }
122  return (sensepi_config_t*) p_mem_loc;
123 }
124 
125 static void clear_all_config (void)
126 {
127  log_printf("%s\n",__func__);
128  hal_nvmc_erase_page (LAST_APP_PAGE_ADDR);
129 }
130 
132 {
133  log_printf("%s\n",__func__);
134  uint32_t * p_mem_loc = (uint32_t *) CONFIG_FW_VER_LOC;
135  uint32_t local_major_num = *p_mem_loc/10000;
136  if(*p_mem_loc == MEM_RESET_VALUE)
137  {
138  update_fw_ver ();
139  }
140  else if(local_major_num != (FW_VER/10000))
141  {
142  clear_all_config ();
143  update_fw_ver ();
144  }
145 }
146 
147 static void update_fw_ver () // make it as hal_nvmc_write
148 {
149  log_printf("%s\n",__func__);
150  uint32_t * p_mem_loc = (uint32_t *) CONFIG_FW_VER_LOC;
151  uint32_t local_fw_ver = FW_VER;
152  hal_nvmc_write_data (p_mem_loc, &local_fw_ver, sizeof(uint32_t));
153 
154 }
bool sensepi_store_config_is_memory_empty(void)
void sensepi_store_config_check_fw_ver()
Function to check the major number of firmware if latest major number \ firmware version is greater t...
sensepi_config_t * sensepi_store_config_get_last_config()
void sensepi_store_config_write(sensepi_config_t *latest_config)
Function to write the sensepi_config_t at address location received from get_next_location().