-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSerialSTM.cpp
338 lines (283 loc) · 9.41 KB
/
SerialSTM.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// 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) 2016 Jim McLaughlin, KI6ZUM
* Copyright (C) 2016,2017,2018 Andy Uribe, CA6JAU
* Copyright (c) 2017 Jonathan Naylor, G4KLX
* Copyright (C) 2018,2022,2024 Bryan Biedenkapp, N2PLL
*
*/
#include "Globals.h"
#include "SerialPort.h"
#include "STM_UART.h"
#if defined(STM32F4XX) || defined(STM32F7XX)
#if defined(STM32F4XX)
#include <stm32f4xx_flash.h>
#endif
#if defined(STM32F7XX)
#include <stm32f7xx_flash.h>
#endif
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
#define STM32_CNF_PAGE_ADDR (uint32_t)0x08010000
#define STM32_CNF_PAGE ((uint32_t *)0x08010000)
#define STM32_CNF_SECTOR FLASH_Sector_4
/*
Pin definitions:
- Host communication:
USART1 - TXD PA9 - RXD PA10 (STM32F4 Pi Board (MMDVM-Pi board), STM32F4 POG Board, STM32F4 DVM1 Board)
- Serial repeater:
UART5 - TXD PC12 - RXD PD2 (STM32F4 Pi Board (MMDVM-Pi board), STM32F4 POG Board)
*/
// ---------------------------------------------------------------------------
// Global Functions and Variables
// ---------------------------------------------------------------------------
extern "C" {
void USART1_IRQHandler();
void UART5_IRQHandler();
}
#if defined(STM32F4_PI) || defined(STM32F4_POG) || defined(STM32F4_DVMV1) || defined(STM32F4_EDA_405) || defined(STM32F4_EDA_446)
// ---------------------------------------------------------------------------
// UART1
// ---------------------------------------------------------------------------
static STM_UART m_USART1;
/**
* @brief Helper to handle the USART1 IRQ.
*/
void USART1_IRQHandler()
{
m_USART1.handleIRQ();
}
/**
* @brief Initializes the USART1.
* @param speed Port speed.
*/
void InitUSART1(int speed)
{
// USART1 - TXD PA9 - RXD PA10 - pins on mmdvm pi board
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
// USART IRQ init
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
// Configure USART as alternate function
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; // Tx | Rx
GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure USART baud rate
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate = speed;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
m_USART1.init(USART1);
}
#endif
// ---------------------------------------------------------------------------
// UART5
// ---------------------------------------------------------------------------
static STM_UART m_USART5;
/**
* @brief Helper to handle the USART5 IRQ.
*/
void UART5_IRQHandler()
{
m_USART5.handleIRQ();
}
/**
* @brief Initializes the USART5.
* @param speed Port speed.
*/
void InitUART5(int speed)
{
// UART5 - TXD PC12 - RXD PD2
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5);
// USART IRQ init
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
// Configure USART as alternate function
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; // Tx
GPIO_InitStructure.GPIO_Speed = GPIO_Fast_Speed;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // Rx
GPIO_Init(GPIOD, &GPIO_InitStructure);
// Configure USART baud rate
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate = speed;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(UART5, &USART_InitStructure);
USART_Cmd(UART5, ENABLE);
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);
m_USART5.init(UART5);
}
// ---------------------------------------------------------------------------
// Private Class Members
// ---------------------------------------------------------------------------
/* Reads data from the modem flash parititon. */
void SerialPort::flashRead()
{
uint8_t reply[249U];
reply[0U] = DVM_SHORT_FRAME_START;
reply[1U] = 249U;
reply[2U] = CMD_FLSH_READ;
::memcpy(reply + 3U, (void*)STM32_CNF_PAGE, 246U);
writeInt(1U, reply, 249U);
}
/* Writes data to the modem flash partition. */
uint8_t SerialPort::flashWrite(const uint8_t* data, uint8_t length)
{
if (length > 249U) {
return RSN_FLASH_WRITE_TOO_BIG;
}
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
// write data to the user flash area
uint32_t address = STM32_CNF_PAGE_ADDR;
uint8_t i = 0U;
while (i < length) {
uint32_t word =
(data[i + 3] << 24) +
(data[i + 2] << 16) +
(data[i + 1] << 8) +
(data[i + 0] << 0);
if (FLASH_ProgramWord(address, word) == FLASH_COMPLETE) {
address += 4;
i += 4;
}
else {
FLASH_Lock();
return RSN_FAILED_WRITE_FLASH;
}
}
FLASH_Lock();
return RSN_OK;
}
/* */
void SerialPort::beginInt(uint8_t n, int speed)
{
switch (n) {
case 1U:
#if defined(STM32F4_PI) || defined(STM32F4_POG) || defined(STM32F4_DVMV1) || defined(STM32F4_EDA_405) || defined(STM32F4_EDA_446)
InitUSART1(speed);
#endif
break;
case 3U:
InitUART5(speed);
break;
default:
break;
}
}
/* */
int SerialPort::availableInt(uint8_t n)
{
switch (n) {
case 1U:
#if defined(STM32F4_PI) || defined(STM32F4_POG) || defined(STM32F4_DVMV1) || defined(STM32F4_EDA_405) || defined(STM32F4_EDA_446)
return m_USART1.available();
#endif
case 3U:
return m_USART5.available();
default:
return 0;
}
}
/* */
int SerialPort::availableForWriteInt(uint8_t n)
{
switch (n) {
case 1U:
#if defined(STM32F4_PI) || defined(STM32F4_POG) || defined(STM32F4_DVMV1) || defined(STM32F4_EDA_405) || defined(STM32F4_EDA_446)
return m_USART1.availableForWrite();
#endif
case 3U:
return m_USART5.availableForWrite();
default:
return 0;
}
}
/* */
uint8_t SerialPort::readInt(uint8_t n)
{
switch (n) {
case 1U:
#if defined(STM32F4_PI) || defined(STM32F4_POG) || defined(STM32F4_DVMV1) || defined(STM32F4_EDA_405) || defined(STM32F4_EDA_446)
return m_USART1.read();
#endif
case 3U:
return m_USART5.read();
default:
return 0U;
}
}
/* */
void SerialPort::writeInt(uint8_t n, const uint8_t* data, uint16_t length, bool flush)
{
switch (n) {
case 1U:
#if defined(STM32F4_PI) || defined(STM32F4_POG) || defined(STM32F4_DVMV1) || defined(STM32F4_EDA_405) || defined(STM32F4_EDA_446)
m_USART1.write(data, length);
if (flush)
m_USART1.flush();
#endif
break;
case 3U:
m_USART5.write(data, length);
if (flush)
m_USART5.flush();
break;
default:
break;
}
}
#endif // defined(STM32F4XX)