21
21
Modified 14 August 2012 by Alarus
22
22
Modified 3 December 2013 by Matthijs Kooijman
23
23
Modified 1 may 2023 by TempersLee
24
- Modified 28 July 2024 by Maxint R&D
24
+ Modified 13 October 2023 by Maxint R&D, latest update 6 May 2025
25
25
*/
26
26
27
-
28
27
#include < stdio.h>
29
28
#include " Arduino.h"
30
29
#include " HardwareSerial.h"
@@ -36,10 +35,22 @@ HardwareSerial::HardwareSerial(void *peripheral)
36
35
{
37
36
setHandler (peripheral);
38
37
38
+ #if defined(PIN_SERIAL_RX) && defined(PIN_SERIAL_TX)
39
+ // if SERIAL_UART_INSTANCES is defined and has value 1, then PIN_SERIAL_RX and PIN_SERIAL_TX are rx/tx pins of the selected SERIAL_UART_INSTANCE
39
40
setRx (PIN_SERIAL_RX);
40
-
41
41
setTx (PIN_SERIAL_TX);
42
-
42
+ #endif
43
+
44
+ #if defined(USART2) && defined(PIN_SERIAL_RX2) && defined(PIN_SERIAL_TX2)
45
+ // if SERIAL_UART_INSTANCES is defined and is 2 or higher, multiple instances can be used simultaneously
46
+ // TODO: get pin number from pinmap PinMap_UART_TX and PinMap_UART_RX
47
+ if (peripheral==USART2)
48
+ {
49
+ setRx (PIN_SERIAL_RX2);
50
+ setTx (PIN_SERIAL_TX2);
51
+ }
52
+ #endif
53
+
43
54
init (_serial.pin_rx , _serial.pin_tx );
44
55
}
45
56
@@ -60,12 +71,13 @@ void HardwareSerial::init(PinName _rx, PinName _tx, PinName _rts, PinName _cts)
60
71
61
72
62
73
// Interrupt handler for filling rx buffer /////////////////////////////////////
63
- #if (OPT_USART1_INT==1)
64
-
65
- #if defined(USART1)
74
+ #if (OPT_USART_INT==1)
66
75
#ifdef __cplusplus
67
76
extern " C" {
68
77
#endif
78
+
79
+ #if defined(USART1)
80
+ #if defined(HAVE_HWSERIAL1)
69
81
void USART1_IRQHandler (void ) __attribute__((interrupt(" WCH-Interrupt-fast" )));
70
82
void USART1_IRQHandler (void ) {
71
83
USART_ClearITPendingBit (USART1, USART_IT_RXNE);
@@ -77,13 +89,29 @@ void HardwareSerial::init(PinName _rx, PinName _tx, PinName _rts, PinName _cts)
77
89
obj->_rx_buffer_head ++;
78
90
obj->_rx_buffer_head %= SERIAL_RX_BUFFER_SIZE;
79
91
}
80
- #ifdef __cplusplus
81
- }
82
- #endif
92
+ #endif
83
93
#endif
84
94
95
+ #if defined(USART2)
96
+ #if defined(HAVE_HWSERIAL2)
97
+ void USART2_IRQHandler (void ) __attribute__((interrupt(" WCH-Interrupt-fast" )));
98
+ void USART2_IRQHandler (void ) {
99
+ USART_ClearITPendingBit (USART2, USART_IT_RXNE);
100
+ // Use the proper serial object to fill the RX buffer. Perhaps we should use uart_handlers[] as defined in uart.c
101
+ // Second Serial is most often Serial2, initialized below as HardwareSerial Serial2(USART2); DEBUG_UART may give issues.
102
+ // TODO? get_serial_obj(uart_handlers[UART2_INDEX]);
103
+ HardwareSerial *obj=&Serial2;
104
+ obj->_rx_buffer [obj->_rx_buffer_head ] = USART_ReceiveData (USART2); // maybe we should use uart_getc()?
105
+ obj->_rx_buffer_head ++;
106
+ obj->_rx_buffer_head %= SERIAL_RX_BUFFER_SIZE;
107
+ }
108
+ #endif
85
109
#endif
86
110
111
+ #ifdef __cplusplus
112
+ }
113
+ #endif
114
+ #endif // if(OPT_USART_INT==1)
87
115
88
116
// Public Methods //////////////////////////////////////////////////////////////
89
117
void HardwareSerial::begin (unsigned long baud, byte config)
@@ -166,14 +194,19 @@ void HardwareSerial::begin(unsigned long baud, byte config)
166
194
}
167
195
uart_init (&_serial, (uint32_t )baud, databits, parity, stopbits);
168
196
169
- #if (OPT_USART1_INT ==1)
197
+ #if (OPT_USART_INT ==1)
170
198
// MMOLE 240619: Enable interrupt handler for filling rx buffer
171
199
#if defined(USART1)
172
200
USART_ITConfig (USART1, USART_IT_RXNE, ENABLE);
173
201
NVIC_SetPriority (USART1_IRQn, UART_IRQ_PRIO);
174
202
NVIC_EnableIRQ (USART1_IRQn);
175
203
#endif
176
- // MMOLE TODO: I only have CH32V003; only tested USART1, how about others?
204
+ // MMOLE TODO: I only have CH32V003/CH32X033; only tested USART1 and USART2, how about others?
205
+ #if defined(USART2)
206
+ USART_ITConfig (USART2, USART_IT_RXNE, ENABLE);
207
+ NVIC_SetPriority (USART2_IRQn, UART_IRQ_PRIO);
208
+ NVIC_EnableIRQ (USART2_IRQn);
209
+ #endif
177
210
#endif
178
211
}
179
212
@@ -185,8 +218,8 @@ void HardwareSerial::end()
185
218
186
219
uart_deinit (&_serial);
187
220
188
- #if (OPT_USART1_INT ==1)
189
- // MMOLE TODO: disable interrupt handler
221
+ #if (OPT_USART_INT ==1)
222
+ // MMOLE TODO: disable interrupt handlers
190
223
#endif
191
224
}
192
225
@@ -234,15 +267,25 @@ int HardwareSerial::read(void)
234
267
235
268
size_t HardwareSerial::write (const uint8_t *buffer, size_t size)
236
269
{
237
-
270
+ #if OPT_PR180 // PR180: HardwareSerial: use correct UART HW for TX
271
+ for (size_t i = 0 ; i < size; i++) {
272
+ write (buffer[i]);
273
+ }
274
+ return size;
275
+ #else
238
276
return uart_debug_write ((uint8_t *)buffer, size);
277
+ #endif
239
278
}
240
279
241
280
242
281
size_t HardwareSerial::write (uint8_t c)
243
282
{
283
+ #if OPT_PR180 // PR180: HardwareSerial: use correct UART HW for TX
284
+ return uart_putc (&_serial, c);
285
+ #else
244
286
uint8_t buff = c;
245
287
return write (&buff, 1 );
288
+ #endif
246
289
}
247
290
248
291
void HardwareSerial::setRx (uint32_t _rx)
0 commit comments