Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NonVolatileMemory.cpp « Hardware « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2e19daabcf0acb98c3b54c27dd1a25fc2803ff10 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * NonVolatileMemory.cpp
 *
 *  Created on: 24 Aug 2020
 *      Author: David
 */

#include "NonVolatileMemory.h"

#if SAM4E || SAM4S || SAME70
# include <Cache.h>
# include <Flash.h>
# include <RTOSIface/RTOSIface.h>
#endif

NonVolatileMemory::NonVolatileMemory() noexcept : state(NvmState::notRead)
{
}

void NonVolatileMemory::EnsureRead() noexcept
{
	if (state == NvmState::notRead)
	{
#if SAME5x
		memcpyu32(reinterpret_cast<uint32_t*>(&buffer), reinterpret_cast<const uint32_t *>(SEEPROM_ADDR), sizeof(buffer)/sizeof(uint32_t));
#elif defined(__LPC17xx__)
# error		//TODO
#elif SAM4E || SAM4S || SAME70
		const bool cacheEnabled = Cache::Disable();
		Flash::ReadUserSignature(reinterpret_cast<uint32_t*>(&buffer), sizeof(buffer)/sizeof(uint32_t));
		if (cacheEnabled)
		{
			Cache::Enable();
		}
#else
# error Unsupported processor
#endif
		if (buffer.magic != NVM::MagicValue)
		{
//			debugPrintf("Invalid user area\n");
			memset(&buffer, 0xFF, sizeof(buffer));
			buffer.magic = NVM::MagicValue;
			state = NvmState::eraseAndWriteNeeded;
		}
		else
		{
			state = NvmState::clean;
//			debugPrintf("user area valid\n");
		}
	}
}

void NonVolatileMemory::EnsureWritten() noexcept
{
#if SAME5x
	if (state >= NvmState::writeNeeded)
	{
		// No need to erase on the SAME5x because the EEPROM emulation manages it
        while (NVMCTRL->SEESTAT.bit.BUSY) { }
        memcpyu32(reinterpret_cast<uint32_t*>(SEEPROM_ADDR), reinterpret_cast<const uint32_t*>(&buffer), sizeof(buffer)/sizeof(uint32_t));
		state = NvmState::clean;
        while (NVMCTRL->SEESTAT.bit.BUSY) { }
	}
#else
	if (state == NvmState::eraseAndWriteNeeded)
	{
		// Erase the page
# if SAM4E || SAM4S || SAME70
		Flash::EraseUserSignature();
# elif defined(__LPC17xx__)
		LPC_EraseSoftwareResetDataSlots();	// erase the last flash sector
# endif
		state = NvmState::writeNeeded;
	}

	if (state == NvmState::writeNeeded)
	{
# if SAM4E || SAM4S || SAME70
		const bool cacheEnabled = Cache::Disable();
		Flash::WriteUserSignature(reinterpret_cast<const uint32_t*>(&buffer));
		if (cacheEnabled)
		{
			Cache::Enable();
		}
# elif defined(__LPC17xx__)
		LPC_WriteSoftwareResetData(slot, buffer, sizeof(buffer));
# else
#  error Unsupported processor
# endif
		state = NvmState::clean;
	}
#endif
}

SoftwareResetData* NonVolatileMemory::GetLastWrittenResetData(unsigned int &slot) noexcept
{
	EnsureRead();
	for (unsigned int i = NumberOfResetDataSlots; i != 0; )
	{
		--i;
		if (buffer.resetData[i].IsValid())
		{
			slot = i;
			return &buffer.resetData[i];
		}
	}
	return nullptr;
}

SoftwareResetData* NonVolatileMemory::AllocateResetDataSlot() noexcept
{
	EnsureRead();
	for (unsigned int i = 0; i < NumberOfResetDataSlots; ++i)
	{
		if (buffer.resetData[i].IsVacant())
		{
			if (state == NvmState::clean)			// need this test because state may already be EraseAndWriteNeeded after EnsureRead
			{
				state = NvmState::writeNeeded;		// assume the caller will write to the allocated slot
			}
			return &buffer.resetData[i];
		}
	}

	// All slots are full, so clear them out and start again
	for (unsigned int i = 0; i < NumberOfResetDataSlots; ++i)
	{
		buffer.resetData[i].Clear();
	}
	state = NvmState::eraseAndWriteNeeded;
	return &buffer.resetData[0];
}

int8_t NonVolatileMemory::GetThermistorLowCalibration(unsigned int inputNumber) noexcept
{
	return GetThermistorCalibration(inputNumber, buffer.thermistorLowCalibration);
}

int8_t NonVolatileMemory::GetThermistorHighCalibration(unsigned int inputNumber) noexcept
{
	return GetThermistorCalibration(inputNumber, buffer.thermistorHighCalibration);
}

void NonVolatileMemory::SetThermistorLowCalibration(unsigned int inputNumber, int8_t val) noexcept
{
	SetThermistorCalibration(inputNumber, val, buffer.thermistorLowCalibration);
}

void NonVolatileMemory::SetThermistorHighCalibration(unsigned int inputNumber, int8_t val) noexcept
{
	SetThermistorCalibration(inputNumber, val, buffer.thermistorHighCalibration);
}

int8_t NonVolatileMemory::GetThermistorCalibration(unsigned int inputNumber, uint8_t *calibArray) noexcept
{
	EnsureRead();
	return (inputNumber >= MaxCalibratedThermistors || calibArray[inputNumber] == 0xFF) ? 0 : (int)calibArray[inputNumber] - (int)0x7F;
}

void NonVolatileMemory::SetThermistorCalibration(unsigned int inputNumber, int8_t val, uint8_t *calibArray) noexcept
{
	if (inputNumber < MaxCalibratedThermistors)
	{
		EnsureRead();
		const uint8_t oldVal = calibArray[inputNumber];
		const uint8_t newVal = val + 0x7F;
		if (oldVal != newVal)
		{
			// If we are only changing 1 bits to 0 then we don't need to erase
			calibArray[inputNumber] = newVal;
			if ((newVal & ~oldVal) != 0)
			{
				state = NvmState::eraseAndWriteNeeded;
			}
			else if (state == NvmState::clean)
			{
				state = NvmState::writeNeeded;
			}
		}
	}
}

// End