-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFirmwareMain.cpp
280 lines (221 loc) · 7.19 KB
/
FirmwareMain.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// SPDX-License-Identifier: GPL-2.0-only
/*
* Digital Voice Modem - Modem Firmware
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2015,2016,2017,2018 Jonathan Naylor, G4KLX
* Copyright (C) 2016 Mathis Schmieder, DB9MAT
* Copyright (C) 2016 Colin Durbridge, G4EML
* Copyright (C) 2018,2024 Bryan Biedenkapp, N2PLL
*
*/
#include "Globals.h"
// ---------------------------------------------------------------------------
// Globals Variables
// ---------------------------------------------------------------------------
DVM_STATE m_modemState = STATE_IDLE;
bool m_dmrEnable = true;
bool m_p25Enable = true;
bool m_nxdnEnable = true;
bool m_dcBlockerEnable = true;
bool m_cosLockoutEnable = false;
bool m_duplex = true;
bool m_tx = false;
bool m_dcd = false;
/* DMR BS */
dmr::DMRIdleRX dmrIdleRX;
dmr::DMRRX dmrRX;
dmr::DMRTX dmrTX;
/* DMR MS-DMO */
dmr::DMRDMORX dmrDMORX;
dmr::DMRDMOTX dmrDMOTX;
/* P25 */
p25::P25RX p25RX;
p25::P25TX p25TX;
/* NXDN */
nxdn::NXDNRX nxdnRX;
nxdn::NXDNTX nxdnTX;
/* Calibration */
dmr::CalDMR calDMR;
p25::CalP25 calP25;
nxdn::CalNXDN calNXDN;
CalRSSI calRSSI;
/* CW */
CWIdTX cwIdTX;
/* RS232 and Air Interface I/O */
SerialPort serial;
IO io;
// ---------------------------------------------------------------------------
// Global Functions
// ---------------------------------------------------------------------------
void setup()
{
io.init();
serial.start();
}
void loop()
{
serial.process();
io.process();
// The following is for transmitting
if (m_dmrEnable && m_modemState == STATE_DMR) {
if (m_duplex)
dmrTX.process();
else
dmrDMOTX.process();
}
if (m_p25Enable && m_modemState == STATE_P25)
p25TX.process();
if (m_nxdnEnable && m_modemState == STATE_NXDN)
nxdnTX.process();
if (m_modemState == STATE_DMR_DMO_CAL_1K || m_modemState == STATE_DMR_CAL_1K ||
m_modemState == STATE_DMR_LF_CAL || m_modemState == STATE_DMR_CAL)
calDMR.process();
if (m_modemState == STATE_P25_CAL_1K || m_modemState == STATE_P25_CAL)
calP25.process();
if (m_modemState == STATE_NXDN_CAL)
calNXDN.process();
if (m_modemState == STATE_CW || m_modemState == STATE_IDLE)
cwIdTX.process();
}
#if defined(__SAM3X8E__) && defined(ARDUINO_SAM_DUE)
/*
main.cpp - Main loop for Arduino sketches
Copyright (c) 2005-2013 Arduino Team. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define ARDUINO_MAIN
#include "Arduino.h"
/*
* Cortex-M3 Systick IT handler
*/
/*
extern void SysTick_Handler (void)
{
// Increment tick count each ms
TimeTick_Increment();
}
*/
// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
void initVariant() { }
// ---------------------------------------------------------------------------
// Firmware Entry Point
// ---------------------------------------------------------------------------
int main(void)
{
// Initialize watchdog
watchdogSetup();
init();
initVariant();
delay(1);
#if defined(SAM3XE_OVERCLOCK)
#define SYS_BOARD_PLLAR (CKGR_PLLAR_ONE | CKGR_PLLAR_MULA(18UL) | CKGR_PLLAR_PLLACOUNT(0x3fUL) | CKGR_PLLAR_DIVA(1UL))
#define SYS_BOARD_MCKR ( PMC_MCKR_PRES_CLK_2 | PMC_MCKR_CSS_PLLA_CLK)
// Set FWS according to SYS_BOARD_MCKR configuration
EFC0->EEFC_FMR = EEFC_FMR_FWS(4); //4 waitstate flash access
EFC1->EEFC_FMR = EEFC_FMR_FWS(4);
// Initialize PLLA to 114MHz
PMC->CKGR_PLLAR = SYS_BOARD_PLLAR;
while (!(PMC->PMC_SR & PMC_SR_LOCKA)) {}
PMC->PMC_MCKR = SYS_BOARD_MCKR;
while (!(PMC->PMC_SR & PMC_SR_MCKRDY)) {}
SystemCoreClockUpdate();
#endif
#if defined(USBCON)
USBDevice.attach();
#endif
setup();
for (;;)
{
loop();
if (serialEventRun)
serialEventRun();
}
return 0;
}
extern "C" void __cxa_pure_virtual() { while (true); }
#endif // defined(__SAM3X8E__) && defined(ARDUINO_SAM_DUE)
#if defined(STM32F4XX) || defined(STM32F7XX)
#if defined(STM32F4XX)
#include <stm32f4xx_flash.h>
#endif
#if defined(STM32F7XX)
#include <stm32f7xx_flash.h>
#endif
#define STM32_CNF_PAGE_ADDR (uint32_t)0x08010000
#define STM32_CNF_PAGE ((uint32_t *)0x08010000)
#define STM32_CNF_SECTOR FLASH_Sector_4
#define STM32_CNF_PAGE_24 24U
// --------------------------------------------------------------------------
// Firmware Entry Point
// --------------------------------------------------------------------------
void jumpToBootLoader()
{
// Disable RCC, set it to default (after reset) settings Internal clock, no PLL, etc.
RCC_DeInit();
ADC_DeInit();
DAC_DeInit();
TIM_DeInit(TIM2);
USART_DeInit(USART1);
USART_DeInit(UART5);
// Disable Systick timer
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;
// Clear Interrupt Enable Register & Interrupt Pending Register
for (uint8_t i = 0; i < sizeof(NVIC->ICER) / sizeof(NVIC->ICER[0]); i++) {
NVIC->ICER[i] = 0xFFFFFFFF;
NVIC->ICPR[i] = 0xFFFFFFFF;
}
SYSCFG->MEMRMP = 0x01;
#if defined(STM32F4XX)
volatile uint32_t addr = 0x1FFF0000;
#elif defined(STM32F7XX)
volatile uint32_t addr = 0x1FF00000;
#endif
void (*SysMemBootJump)(void);
SysMemBootJump = (void (*)(void))(*((uint32_t *)(addr + 4)));
__set_MSP(*(uint32_t *)addr);
SysMemBootJump();
}
int main()
{
// does the configuration page contain the request bootloader flag?
if ((STM32_CNF_PAGE[STM32_CNF_PAGE_24] != 0xFFFFFFFFU) && (STM32_CNF_PAGE[STM32_CNF_PAGE_24] != 0x00U)) {
uint8_t bootloadMode = (STM32_CNF_PAGE[STM32_CNF_PAGE_24] >> 8) & 0xFFU;
if ((bootloadMode & 0x20U) == 0x20U) {
// we unfortunately need to discard the configuration area entirely for bootloader mode...
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_WRPERR);
#if defined(STM32F4XX) || defined(STM32F7XX)
if (FLASH_EraseSector(STM32_CNF_SECTOR, VoltageRange_3) != FLASH_COMPLETE) {
FLASH_Lock();
return RSN_FAILED_ERASE_FLASH;
}
#elif defined(STM32F10X_MD)
if (FLASH_ErasePage(STM32_CNF_PAGE_ADDR) != FLASH_COMPLETE) {
FLASH_Lock();
return RSN_FAILED_ERASE_FLASH;
}
#endif
jumpToBootLoader();
}
}
setup();
for (;;)
loop();
}
#endif // defined(STM32F4XX)