Appiko
sensebe_store_config.c
1 /*
2  * sensebe_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 #include "sensebe_store_config.h"
20 
21 #include "hal_nop_delay.h"
22 #include "hal_nvmc.h"
23 
24 #include "nrf_util.h"
25 #include "nrf_assert.h"
26 #include "common_util.h"
27 
28 #include "log.h"
29 
31 #define LAST_APP_PAGE_ADDR 0x27000
32 
33 #define CONFIG_SIZE_TO_POINTER 6
34 
35 #define NO_OF_CONFIGS 165
36 
37 #define WORD_SIZE 4
38 
39 #define LAST_CONFIG_END_ADDR (LAST_APP_PAGE_ADDR+NO_OF_CONFIGS*CONFIG_SIZE_TO_POINTER\
40  * WORD_SIZE)
41 
43 #define CONFIG_FW_VER_LOC LAST_CONFIG_END_ADDR+CONFIG_SIZE_TO_POINTER+0x2
44 
45 #define MEM_RESET_VALUE 0xFFFFFFFF
46 
47 #define MEM_FULL LAST_CONFIG_END_ADDR
48 
57 static uint32_t get_next_location (void);
58 
64 static void clear_all_config (void);
65 
69 static void update_fw_ver (void);
70 
71 static uint32_t get_next_location (void)
72 {
73  log_printf("%s\n",__func__);
74  uint32_t * p_mem_loc = (uint32_t *) LAST_APP_PAGE_ADDR;
75  while(p_mem_loc < (uint32_t *)LAST_CONFIG_END_ADDR)
76  {
77  if(*(p_mem_loc) == MEM_RESET_VALUE)
78  {
79  return (uint32_t)p_mem_loc;
80  }
81  p_mem_loc += CONFIG_SIZE_TO_POINTER;
82  }
83  return MEM_FULL;
84 
85 }
86 
88 {
89  log_printf("%s\n",__func__);
90  if(get_next_location () == LAST_APP_PAGE_ADDR)
91  {
92  return true;
93  }
94  else
95  {
96  return false;
97  }
98 }
99 
100 void sensebe_store_config_write (sensebe_config_t* latest_config)
101 {
102  log_printf("%s\n",__func__);
103  uint32_t * p_mem_loc = (uint32_t *) get_next_location();
104  if(p_mem_loc == (uint32_t *)MEM_FULL)
105  {
106  clear_all_config ();
107  p_mem_loc = (uint32_t *) get_next_location();
108  }
109  hal_nvmc_write_data(p_mem_loc, latest_config, sizeof(sensebe_config_t));
110 
111 }
112 
114 {
115  log_printf("%s\n",__func__);
116  uint32_t * p_mem_loc = (uint32_t*)get_next_location();
117  if(p_mem_loc != (uint32_t*)LAST_APP_PAGE_ADDR)
118  {
119  p_mem_loc -= CONFIG_SIZE_TO_POINTER;
120  }
121  return (sensebe_config_t*) p_mem_loc;
122 }
123 
124 static void clear_all_config (void)
125 {
126  log_printf("%s\n",__func__);
127  hal_nvmc_erase_page (LAST_APP_PAGE_ADDR);
128 }
129 
131 {
132  log_printf("%s\n",__func__);
133  uint32_t * p_mem_loc = (uint32_t *) CONFIG_FW_VER_LOC;
134  uint32_t local_major_num = *p_mem_loc/10000;
135  if(*p_mem_loc == MEM_RESET_VALUE)
136  {
137  update_fw_ver ();
138  }
139  else if(local_major_num != (FW_VER/10000))
140  {
141  clear_all_config ();
142  update_fw_ver ();
143  }
144 }
145 
146 static void update_fw_ver () // make it as hal_nvmc_write
147 {
148  log_printf("%s\n",__func__);
149  uint32_t * p_mem_loc = (uint32_t *) CONFIG_FW_VER_LOC;
150  uint32_t local_fw_ver = FW_VER;
151  hal_nvmc_write_data (p_mem_loc, &local_fw_ver, sizeof(uint32_t));
152 
153 }
void sensebe_store_config_write(sensebe_config_t *latest_config)
Function to write the sensebe_config_t at address location received from get_next_location().
bool sensebe_store_config_is_memory_empty(void)
sensebe_config_t * sensebe_store_config_get_last_config()
void sensebe_store_config_check_fw_ver()
Function to check the major number of firmware if latest major number \ firmware version is greater t...