-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboot_protocol.cpp
211 lines (189 loc) · 6.71 KB
/
boot_protocol.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <flash.h>
#include <usart.h>
#include <iopins.h>
#include <Timers.h>
#include <binary_stream.h>
#include <dispatcher.h>
#include <sys_tick.h>
#include <watchdog.h>
#include <array>
#include "bootloader.h"
#include "boot_protocol.h"
using namespace Mcucpp::Modbus;
using namespace Bootloader;
BootloaderProtocol::BootloaderProtocol(BootloaderApp &bootloader)
: _bootloader{bootloader}
{
}
bool BootloaderProtocol::Init()
{
Clock::HsiClock::Enable();
BootDeviceClock::SelectClockSource(Clock::UsartClockSrc::Hsi);
rtuTransport.SetStaticBuffers(&rxChunk, &txChunk);
BootDevice::Init(115200, BootDevice::Default | BootDevice::TwoStopBits);
BootDevice::SelectTxRxPins<TxPin, RxPin>();
BootDevice::SetRxTimeout(20);
RxPin::SetPullUp(RxPin::PullMode::PullUp);
BootDevice::SelectDePin<DePin>();
modbus.SetAddress(BootModbusAddr);
modbus.OnWriteHoldingRegs = [this](uint16_t s, uint16_t c, DataBuffer &b) { return WriteHoldingRegisters(s, c, b); };
modbus.OnReadHoldingRegs = [this](uint16_t s, uint16_t c, DataBuffer &b) { return ReadHoldingRegisters(s, c, b); };
modbus.OnReadInputRegisters = [this](uint16_t s, uint16_t c, DataBuffer &b) { return ReadInputRegisters(s, c, b); };
if (!rtuTransport.StartListen())
{
return false;
}
return true;
}
uint16_t BootloaderProtocol::GetPageMapItem(uint16_t index)
{
PageProps prop = (PageProps)(index % PageMapEntrySize);
uint16_t page = index / PageMapEntrySize;
if (page >= Flash::PageCount())
{
return 0;
}
if (prop == PageProps::AddressLo)
{
return (uint16_t)(Flash::PageAddress(page) & 0xffff);
}
if (prop == PageProps::AddressHi)
{
return (uint16_t)((Flash::PageAddress(page) >> 16) & 0xffff);
}
if (prop == PageProps::SizeLo)
{
return (uint16_t)(Flash::PageSize(page) & 0xffff);
}
if (prop == PageProps::SizeHi)
{
return (uint16_t)((Flash::PageSize(page) >> 16) & 0xffff);
}
return 0xffff;
}
ModbusError BootloaderProtocol::ReadInputRegisters(uint16_t start, uint16_t count, DataBuffer &buffer)
{
uint16_t end = std::min<uint16_t>(start + count, Flash::PageCount() * PageMapEntrySize);
for (uint16_t reg = start; reg < end; reg++)
{
buffer.WriteU16Be(GetPageMapItem(reg));
}
return ModbusError::NoError;
}
ModbusError BootloaderProtocol::ReadHoldingRegisters(uint16_t start, uint16_t count, DataBuffer &buffer)
{
#if defined(_DEBUG) && _DEBUG
cout << "RD: " << setw(5) << start << setw(5) << count << "\r\n";
#endif
uint16_t bootDataEnd = std::min<uint16_t>(start + count, sizeof(BootData) / 2);
uint16_t *bootDataPtr = reinterpret_cast<uint16_t *>(&_bootloader.GetBootData());
for (uint16_t reg = start; reg < bootDataEnd; reg++)
{
buffer.WriteU16Be(bootDataPtr[reg]);
}
return ModbusError::NoError;
}
ModbusError BootloaderProtocol::WriteHoldingRegisters(uint16_t start, uint16_t count, DataBuffer &buffer)
{
uint16_t endReg = start + count;
#if defined(_DEBUG) && _DEBUG
cout << setw(5) << start << setw(5) << count;
if (count < 20)
{
cout << "{";
for (auto d : buffer)
cout << setw(5) << d;
cout << "}";
}
cout << "\r\n";
#endif
if ((start >= CommandAddress && start < CommandAddress + CommandParamsSize) || (endReg >= CommandAddress && endReg < CommandAddress + CommandParamsSize))
{
uint16_t startInRange = (uint16_t)std::max<int>(start - CommandAddress, 0);
uint16_t countInRange = std::min<uint16_t>(count, CommandParamsSize - startInRange);
return WriteCommand(startInRange, countInRange, buffer);
}
if ((start >= PageBufferAddr && start < PageBufferAddr + PageBuffer.size()) || (endReg >= PageBufferAddr && endReg < PageBufferAddr + PageBuffer.size()))
{
uint16_t startInRange = (uint16_t)std::max<int>(start - PageBufferAddr, 0);
uint16_t countInRange = std::min<uint16_t>(count, PageBuffer.size() - startInRange);
return WriteBuffer(startInRange, countInRange, buffer);
}
return ModbusError::IllegalAddress;
}
ModbusError BootloaderProtocol::WriteBuffer(uint16_t start, uint16_t count, DataBuffer &buffer)
{
uint16_t endReg = std::min<uint16_t>(start + count, PageBuffer.size());
for (uint16_t reg = start; reg < endReg; reg++)
{
PageBuffer[reg] = buffer.ReadU16Be();
}
return ModbusError::NoError;
}
ModbusError BootloaderProtocol::WriteCommand(uint16_t start, uint16_t count, DataBuffer &buffer)
{
uint16_t endReg = std::min<uint16_t>(start + count, CommandParamsSize);
for (uint16_t reg = start; reg < endReg; reg++)
{
uint16_t value = buffer.ReadU16Be();
CommandLayout field = (CommandLayout)reg;
if (field == CommandLayout::Page)
{
if (value >= _bootloader.BootStartBootPage())
{
return ModbusError::IllegalValue;
}
_commandData.page = value;
}
if (field == CommandLayout::Offset)
{
if (value >= Flash::PageSize(_commandData.page))
{
return ModbusError::IllegalValue;
}
_commandData.offset = value;
}
if (field == CommandLayout::Length)
{
if (value > Flash::PageSize(_commandData.page) - _commandData.offset)
{
return ModbusError::IllegalValue;
}
_commandData.length = value;
}
if (field == CommandLayout::Command)
{
if (!ExecuteCommand(static_cast<BootCommand>(value)))
{
return ModbusError::NotAcknowledge;
}
}
}
return ModbusError::NoError;
}
bool BootloaderProtocol::ExecuteCommand(BootCommand command)
{
_bootloader.GetBootData().error = BootError::Success;
switch (command)
{
case BootCommand::PageErase:
return _bootloader.EraseFlash(_commandData.page);
case BootCommand::PageWrite:
return _bootloader.WriteFlash(PageBuffer.data(), _commandData.page, _commandData.length, _commandData.offset);
case BootCommand::RunApplication:
return _bootloader.RunApplication();
case BootCommand::Reset:
_bootloader.Reset();
break;
case BootCommand::Activate:
_bootloader.Connected();
break;
case BootCommand::PageRead:
case BootCommand::None:
break;
default:
_bootloader.GetBootData().error = BootError::WrongCommand;
return false;
}
return true;
}