Skip to content

Commit 0f002a3

Browse files
authored
Merge pull request #122 from fpistm/genericSerial
Make Serialx instance generic
2 parents dd3e773 + 29bdd7c commit 0f002a3

File tree

43 files changed

+676
-979
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+676
-979
lines changed

boards.txt

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
menu.pnum=Board part number
44

5-
menu.xserial=Extra serial instance
5+
menu.xserial=Serial interface
66
menu.usb=USB interface
77

88
menu.opt=Optimize
@@ -328,37 +328,37 @@ Disco.menu.upload_method.STLink.upload.tool=stlink_upload
328328

329329
################################################################################
330330
# Serialx activation
331-
Nucleo_144.menu.xserial.none=None
332-
Nucleo_144.menu.xserial.SerialAll=All
333-
Nucleo_144.menu.xserial.SerialAll.build.xSerial=-DENABLE_SERIAL1 -DENABLE_SERIAL2
334-
Nucleo_144.menu.xserial.Serial1=Serial1 (if available)
335-
Nucleo_144.menu.xserial.Serial1.build.xSerial=-DENABLE_SERIAL1
336-
Nucleo_144.menu.xserial.Serial2=Serial2 (if available)
337-
Nucleo_144.menu.xserial.Serial2.build.xSerial=-DENABLE_SERIAL2
338-
339-
Nucleo_64.menu.xserial.none=None
340-
Nucleo_64.menu.xserial.SerialAll=All
341-
Nucleo_64.menu.xserial.SerialAll.build.xSerial=-DENABLE_SERIAL1 -DENABLE_SERIAL2
342-
Nucleo_64.menu.xserial.Serial1=Serial1 (if available)
343-
Nucleo_64.menu.xserial.Serial1.build.xSerial=-DENABLE_SERIAL1
344-
Nucleo_64.menu.xserial.Serial2=Serial2 (if available)
345-
Nucleo_64.menu.xserial.Serial2.build.xSerial=-DENABLE_SERIAL2
346-
347-
Nucleo_32.menu.xserial.none=None
348-
Nucleo_32.menu.xserial.SerialAll=All
349-
Nucleo_32.menu.xserial.SerialAll.build.xSerial=-DENABLE_SERIAL1 -DENABLE_SERIAL2
350-
Nucleo_32.menu.xserial.Serial1=Serial1 (if available)
351-
Nucleo_32.menu.xserial.Serial1.build.xSerial=-DENABLE_SERIAL1
352-
Nucleo_32.menu.xserial.Serial2=Serial2 (if available)
353-
Nucleo_32.menu.xserial.Serial2.build.xSerial=-DENABLE_SERIAL2
354-
355-
Disco.menu.xserial.none=None
356-
Disco.menu.xserial.SerialAll=All
357-
Disco.menu.xserial.SerialAll.build.xSerial=-DENABLE_SERIAL1 -DENABLE_SERIAL2
358-
Disco.menu.xserial.Serial1=Serial1 (if available)
359-
Disco.menu.xserial.Serial1.build.xSerial=-DENABLE_SERIAL1
360-
Disco.menu.xserial.Serial2=Serial2 (if available)
361-
Disco.menu.xserial.Serial2.build.xSerial=-DENABLE_SERIAL2
331+
Nucleo_144.menu.xserial.generic=Generic Serial
332+
Nucleo_144.menu.xserial.none=No Serial
333+
Nucleo_144.menu.xserial.none.build.xSerial=-DNO_HWSERIAL
334+
Nucleo_144.menu.xserial.all=All (up to 10)
335+
Nucleo_144.menu.xserial.all.build.xSerial=-DALL_HWSERIAL
336+
Nucleo_144.menu.xserial.third=Enable first third (USART1 to 3 if available)
337+
Nucleo_144.menu.xserial.third.build.xSerial=-DFIRST_THIRD_HWSERIAL
338+
339+
Nucleo_64.menu.xserial.generic=Generic Serial
340+
Nucleo_64.menu.xserial.none=No Serial
341+
Nucleo_64.menu.xserial.none.build.xSerial=-DNO_HWSERIAL
342+
Nucleo_64.menu.xserial.all=All (up to 10)
343+
Nucleo_64.menu.xserial.all.build.xSerial=-DALL_HWSERIAL
344+
Nucleo_64.menu.xserial.third=Enable first third (USART1 to 3 if available)
345+
Nucleo_64.menu.xserial.third.build.xSerial=-DFIRST_THIRD_HWSERIAL
346+
347+
Nucleo_32.menu.xserial.generic=Generic Serial
348+
Nucleo_32.menu.xserial.none=No Serial
349+
Nucleo_32.menu.xserial.none.build.xSerial=-DNO_HWSERIAL
350+
Nucleo_32.menu.xserial.all=All (up to 10)
351+
Nucleo_32.menu.xserial.all.build.xSerial=-DALL_HWSERIAL
352+
Nucleo_32.menu.xserial.third=Enable first third (USART1 to 3 if available)
353+
Nucleo_32.menu.xserial.third.build.xSerial=-DFIRST_THIRD_HWSERIAL
354+
355+
Disco.menu.xserial.generic=Generic Serial
356+
Disco.menu.xserial.none=No Serial
357+
Disco.menu.xserial.none.build.xSerial=-DNO_HWSERIAL
358+
Disco.menu.xserial.all=All (up to 10)
359+
Disco.menu.xserial.all.build.xSerial=-DALL_HWSERIAL
360+
Disco.menu.xserial.third=Enable first third (USART1 to 3 if available)
361+
Disco.menu.xserial.third.build.xSerial=-DFIRST_THIRD_HWSERIAL
362362

363363
# USB connectivity
364364
Nucleo_144.menu.usb.none=None

cores/arduino/HardwareSerial.cpp

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,143 @@
3030
#include "Arduino.h"
3131
#include "HardwareSerial.h"
3232

33+
#if !defined(NO_HWSERIAL)
34+
#if defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3) ||\
35+
defined(HAVE_HWSERIAL4) || defined(HAVE_HWSERIAL5) || defined(HAVE_HWSERIAL6) ||\
36+
defined(HAVE_HWSERIAL7) || defined(HAVE_HWSERIAL8) || defined(HAVE_HWSERIAL8) ||\
37+
defined(HAVE_HWSERIAL10)
38+
// SerialEvent functions are weak, so when the user doesn't define them,
39+
// the linker just sets their address to 0 (which is checked below).
40+
#if defined(HAVE_HWSERIAL1)
41+
HardwareSerial Serial1(USART1);
42+
void serialEvent1() __attribute__((weak));
43+
#endif
44+
45+
#if defined(HAVE_HWSERIAL2)
46+
HardwareSerial Serial2(USART2);
47+
void serialEvent2() __attribute__((weak));
48+
#endif
49+
50+
#if defined(HAVE_HWSERIAL3)
51+
HardwareSerial Serial3(USART3);
52+
void serialEvent3() __attribute__((weak));
53+
#endif
54+
55+
#if defined(HAVE_HWSERIAL4)
56+
#if defined(USART4)
57+
HardwareSerial Serial4(USART4);
58+
#else
59+
HardwareSerial Serial4(UART4);
60+
#endif
61+
void serialEvent4() __attribute__((weak));
62+
#endif
63+
64+
#if defined(HAVE_HWSERIAL5)
65+
#if defined(USART5)
66+
HardwareSerial Serial5(USART5);
67+
#else
68+
HardwareSerial Serial5(UART5);
69+
#endif
70+
void serialEvent5() __attribute__((weak));
71+
#endif
72+
73+
#if defined(HAVE_HWSERIAL6)
74+
HardwareSerial Serial6(USART6);
75+
void serialEvent6() __attribute__((weak));
76+
#endif
77+
78+
#if defined(HAVE_HWSERIAL7)
79+
#if defined(USART7)
80+
HardwareSerial Serial7(USART7);
81+
#else
82+
HardwareSerial Serial7(UART7);
83+
#endif
84+
void serialEvent7() __attribute__((weak));
85+
#endif
86+
87+
#if defined(HAVE_HWSERIAL8)
88+
#if defined(USART8)
89+
HardwareSerial Serial8(USART8);
90+
#else
91+
HardwareSerial Serial8(UART8);
92+
#endif
93+
void serialEvent8() __attribute__((weak));
94+
#endif
95+
96+
#if defined(HAVE_HWSERIAL9)
97+
HardwareSerial Serial9(UART9);
98+
void serialEvent9() __attribute__((weak));
99+
#endif
100+
101+
#if defined(HAVE_HWSERIAL10)
102+
HardwareSerial Serial10(UART10);
103+
void serialEvent10() __attribute__((weak));
104+
#endif
105+
106+
void serialEventRun(void)
107+
{
108+
#if defined(HAVE_HWSERIAL1)
109+
if (serialEvent1 && Serial1.available()) serialEvent1();
110+
#endif
111+
#if defined(HAVE_HWSERIAL2)
112+
if (serialEvent2 && Serial2.available()) serialEvent2();
113+
#endif
114+
#if defined(HAVE_HWSERIAL3)
115+
if (serialEvent3 && Serial3.available()) serialEvent3();
116+
#endif
117+
#if defined(HAVE_HWSERIAL4)
118+
if (serialEvent4 && Serial4.available()) serialEvent4();
119+
#endif
120+
#if defined(HAVE_HWSERIAL5)
121+
if (serialEvent5 && Serial5.available()) serialEvent5();
122+
#endif
123+
#if defined(HAVE_HWSERIAL6)
124+
if (serialEvent6 && Serial6.available()) serialEvent6();
125+
#endif
126+
#if defined(HAVE_HWSERIAL7)
127+
if (serialEvent7 && Serial7.available()) serialEvent7();
128+
#endif
129+
#if defined(HAVE_HWSERIAL8)
130+
if (serialEvent8 && Serial8.available()) serialEvent8();
131+
#endif
132+
#if defined(HAVE_HWSERIAL9)
133+
if (serialEvent9 && Serial9.available()) serialEvent9();
134+
#endif
135+
#if defined(HAVE_HWSERIAL10)
136+
if (serialEventl10 && Serial10.available()) serialEvent10();
137+
#endif
138+
}
33139

34140
// Constructors ////////////////////////////////////////////////////////////////
35141
HardwareSerial::HardwareSerial(PinName _rx, PinName _tx)
36142
{
37143
_serial.pin_rx = _rx;
38144
_serial.pin_tx = _tx;
145+
init();
146+
}
147+
148+
HardwareSerial::HardwareSerial(void* peripheral)
149+
{
150+
// If Serial is defined in variant set
151+
// the Rx/Tx pins for com port if defined
152+
#if defined(Serial) && defined(PIN_SERIAL_RX) && defined(PIN_SERIAL_TX)
153+
if (this == &Serial)
154+
{
155+
setRx(PIN_SERIAL_RX);
156+
setTx(PIN_SERIAL_TX);
157+
}
158+
else
159+
#endif
160+
// else get the pins of the first peripheral occurence in PinMap
161+
{
162+
_serial.pin_rx = pinmap_pin(peripheral, PinMap_UART_RX);
163+
_serial.pin_tx = pinmap_pin(peripheral, PinMap_UART_TX);
164+
}
165+
init();
166+
}
167+
168+
void HardwareSerial::init(void)
169+
{
39170
_serial.rx_buff = _rx_buffer;
40171
_serial.rx_head = 0;
41172
_serial.rx_tail = 0;
@@ -87,7 +218,7 @@ void HardwareSerial::begin(unsigned long baud, byte config)
87218

88219
_serial.baudrate = (uint32_t)baud;
89220

90-
// Manage databits
221+
// Manage databitshardware/arduino/avr/cores/arduino/HardwareSerial.cpp
91222
switch(config & 0x07) {
92223
case 0x02:
93224
databits = 6;
@@ -224,3 +355,21 @@ size_t HardwareSerial::write(uint8_t c)
224355

225356
return 1;
226357
}
358+
359+
void HardwareSerial::setRx(uint32_t _rx) {
360+
_serial.pin_rx = digitalPinToPinName(_rx);
361+
}
362+
363+
void HardwareSerial::setTx(uint32_t _tx) {
364+
_serial.pin_tx = digitalPinToPinName(_tx);
365+
}
366+
367+
void HardwareSerial::setRx(PinName _rx) {
368+
_serial.pin_rx = _rx;
369+
}
370+
371+
void HardwareSerial::setTx(PinName _tx){
372+
_serial.pin_tx = _tx;
373+
}
374+
#endif // HAVE_HWSERIALx
375+
#endif // !NO_HWSERIAL

cores/arduino/HardwareSerial.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class HardwareSerial : public Stream
102102

103103
public:
104104
HardwareSerial(PinName _rx, PinName _tx);
105+
HardwareSerial(void* peripheral);
105106
void begin(unsigned long baud) { begin(baud, SERIAL_8N1); }
106107
void begin(unsigned long, uint8_t);
107108
void end();
@@ -118,11 +119,29 @@ class HardwareSerial : public Stream
118119
using Print::write; // pull in write(str) and write(buf, size) from Print
119120
operator bool() { return true; }
120121

122+
void setRx(uint32_t _rx);
123+
void setTx(uint32_t _tx);
124+
void setRx(PinName _rx);
125+
void setTx(PinName _tx);
126+
121127
// Interrupt handlers
122128
static void _rx_complete_irq(serial_t* obj);
123129
static int _tx_complete_irq(serial_t* obj);
130+
private:
131+
void init(void);
124132
};
125133

134+
extern HardwareSerial Serial1;
135+
extern HardwareSerial Serial2;
136+
extern HardwareSerial Serial3;
137+
extern HardwareSerial Serial4;
138+
extern HardwareSerial Serial5;
139+
extern HardwareSerial Serial6;
140+
extern HardwareSerial Serial7;
141+
extern HardwareSerial Serial8;
142+
extern HardwareSerial Serial9;
143+
extern HardwareSerial Serial10;
144+
126145
extern void serialEventRun(void) __attribute__((weak));
127146

128147
#endif

cores/arduino/stm32/core_callback.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#ifndef __CALLBACK_H
4040
#define __CALLBACK_H
4141

42-
#include "Arduino.h"
42+
#include "variant.h"
4343

4444
#ifdef __cplusplus
4545
extern "C" {

cores/arduino/stm32/pinmap.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@ void* pinmap_peripheral(PinName pin, const PinMap* map) {
3636
return peripheral;
3737
}
3838

39+
PinName pinmap_find_pin(void* peripheral, const PinMap* map) {
40+
while (map->peripheral != NP) {
41+
if (map->peripheral == peripheral)
42+
return map->pin;
43+
map++;
44+
}
45+
return NC;
46+
}
47+
48+
PinName pinmap_pin(void* peripheral, const PinMap* map) {
49+
PinName pin = NC;
50+
51+
if (peripheral != NP) {
52+
pin = pinmap_find_pin(peripheral, map);
53+
}
54+
// else error("pinmap not found for pin");
55+
return pin;
56+
}
57+
3958
uint32_t pinmap_find_function(PinName pin, const PinMap* map) {
4059
while (map->pin != NC) {
4160
if (map->pin == pin)

cores/arduino/stm32/pinmap.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ void pin_function(PinName pin, int function);
4141

4242
PinName pin_pinName(const PinMap* map);
4343

44-
void* pinmap_peripheral(PinName pin, const PinMap* map);
45-
uint32_t pinmap_function(PinName pin, const PinMap* map);
4644
void* pinmap_find_peripheral(PinName pin, const PinMap* map);
45+
void* pinmap_peripheral(PinName pin, const PinMap* map);
46+
PinName pinmap_find_pin(void* peripheral, const PinMap* map);
47+
PinName pinmap_pin(void* peripheral, const PinMap* map);
4748
uint32_t pinmap_find_function(PinName pin, const PinMap* map);
49+
uint32_t pinmap_function(PinName pin, const PinMap* map);
4850
void* pinmap_merge_peripheral(void* a, void* b);
4951

5052
#ifdef __cplusplus

cores/arduino/stm32/timer.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,8 @@
4646
/** @addtogroup STM32F4xx_System_Private_Includes
4747
* @{
4848
*/
49-
#include "stm32_def.h"
5049
#include "timer.h"
51-
#include "digital_io.h"
52-
#include "clock.h"
53-
#include "analog.h"
54-
#include "variant.h"
50+
#include "board.h"
5551

5652
#ifdef __cplusplus
5753
extern "C" {

cores/arduino/stm32/uart.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@
5454
extern "C" {
5555
#endif
5656

57+
// if DEBUG_UART is not defined assume this is the one
58+
// linked to PIN_SERIAL_TX
59+
#if !defined(DEBUG_UART)
60+
#if defined(PIN_SERIAL_TX)
61+
#define DEBUG_UART pinmap_peripheral(digitalPinToPinName(PIN_SERIAL_TX), PinMap_UART_TX)
62+
#else
63+
#define DEBUG_UART NP
64+
#endif
65+
#endif
5766
// @brief uart caracteristics
5867
#if defined(STM32F4xx)
5968
#define UART_NUM (10)
@@ -398,11 +407,11 @@ size_t uart_write(serial_t *obj, uint8_t data, uint16_t size)
398407
size_t uart_debug_write(uint8_t *data, uint32_t size)
399408
{
400409
uint8_t index = 0;
410+
USART_TypeDef* dbg_uart = DEBUG_UART;
401411
uint32_t tickstart = HAL_GetTick();
402-
403412
for(index = 0; index < UART_NUM; index++) {
404413
if(uart_handlers[index] != NULL) {
405-
if(DEBUG_UART == uart_handlers[index]->Instance) {
414+
if(dbg_uart == uart_handlers[index]->Instance) {
406415
break;
407416
}
408417
}

0 commit comments

Comments
 (0)