-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.cpp
128 lines (97 loc) · 3.44 KB
/
memory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "memory.h"
#include "settings.h"
#include "GPIOoutput.h"
#include "hardware/flash.h"
#include "hardware/sync.h"
#include <cstring> // memcpy
uint8_t UNIQUE_ID;
void flashInit()
{
// test if we already stored data in the flash memory
flash_get_unique_id(&UNIQUE_ID);
uint8_t *firstFlashByte = (uint8_t *)(XIP_BASE + MY_FLASH_OFFSET);
if (UNIQUE_ID != *firstFlashByte) // no data in the flash memory, write for the first time
{
struct memory tempData[256];
tempData[0].rxFrequency = F_MIN; // fScanMin
tempData[0].txFrequency = F_MAX; // fScanMax
for (size_t i = 1; i <= MEMORIES; i++)
{
tempData[i].isUsed = false;
}
auto interruptState = save_and_disable_interrupts();
// write unique id
flash_range_erase(MY_FLASH_OFFSET, FLASH_SECTOR_SIZE); // erase 1 sector = 4096 bytes
flash_range_program(MY_FLASH_OFFSET, &UNIQUE_ID, FLASH_PAGE_SIZE); // write 1 page = 256 bytes
// write flash data
flash_range_erase(MY_FLASH_DATA, FLASH_SECTOR_SIZE); // erase 1 sector = 4096 bytes
flash_range_program(MY_FLASH_DATA, (uint8_t *)tempData, 4096);
restore_interrupts(interruptState);
}
}
uint32_t scanMin()
{
return flashData[0].rxFrequency;
}
uint32_t scanMax()
{
return flashData[0].txFrequency;
}
void saveScanMin(uint32_t frequency)
{
Piezo::getInstance()->beepWriteOK();
struct memory tempData[256];
memcpy(tempData, flashData, 4096);
tempData[0].rxFrequency = frequency;
auto interruptState = save_and_disable_interrupts();
flash_range_erase(MY_FLASH_DATA, FLASH_SECTOR_SIZE); // erase 1 sector = 4096 bytes
flash_range_program(MY_FLASH_DATA, (uint8_t *)tempData, 4096);
restore_interrupts(interruptState);
}
void saveScanMax(uint32_t frequency)
{
Piezo::getInstance()->beepWriteOK();
struct memory tempData[256];
memcpy(tempData, flashData, 4096);
tempData[0].txFrequency = frequency;
auto interruptState = save_and_disable_interrupts();
flash_range_erase(MY_FLASH_DATA, FLASH_SECTOR_SIZE); // erase 1 sector = 4096 bytes
flash_range_program(MY_FLASH_DATA, (uint8_t *)tempData, 4096);
restore_interrupts(interruptState);
}
void saveMemory(const size_t memoryIndex, const memory &m)
{
if (memoryIndex < 1 || memoryIndex > MEMORIES)
{
Piezo::getInstance()->beepError();
}
else
{
Piezo::getInstance()->beepWriteOK();
struct memory tempData[256];
memcpy(tempData, flashData, 4096);
tempData[memoryIndex] = m;
auto interruptState = save_and_disable_interrupts();
flash_range_erase(MY_FLASH_DATA, FLASH_SECTOR_SIZE); // erase 1 sector = 4096 bytes
flash_range_program(MY_FLASH_DATA, (uint8_t *)tempData, 4096);
restore_interrupts(interruptState);
}
}
void deleteMemory(const size_t memoryIndex)
{
if (memoryIndex < 1 || memoryIndex > MEMORIES)
{
Piezo::getInstance()->beepError();
}
else
{
Piezo::getInstance()->beepWriteOK();
struct memory tempData[256];
memcpy(tempData, flashData, 4096);
tempData[memoryIndex].isUsed = false;
auto interruptState = save_and_disable_interrupts();
flash_range_erase(MY_FLASH_DATA, FLASH_SECTOR_SIZE); // erase 1 sector = 4096 bytes
flash_range_program(MY_FLASH_DATA, (uint8_t *)tempData, 4096);
restore_interrupts(interruptState);
}
}