Skip to content

Commit 1fefdfc

Browse files
committed
CheckVariant: Add check of IP instances (Wire, , Serial, SPI)
Signed-off-by: Alexandre Bourdiol <alexandre.bourdiol@st.com>
1 parent d40478a commit 1fefdfc

File tree

2 files changed

+217
-4
lines changed

2 files changed

+217
-4
lines changed

examples/NonReg/CheckVariant/CheckVariant.ino

+47-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,45 @@
11
/*
2-
This sketch check, digital and analog pins defined in the variant
3-
*/
2+
This sketch check:
3+
- digital and analog pins defined in the variant
4+
- peripheral instances associated to wire (I2C), serial (UART) and SPI
5+
*/
46

57
#include "utils.h"
68

79
#define PORTx(pn) (char)('A' + STM_PORT(pn))
810
#define PINx(pn) STM_PIN(pn)
911

1012
/*
13+
Initial check of Serial to be sure we can further print on serial
14+
Returns true in case of test passed
15+
Returns false in case of test failed
16+
*/
17+
bool checkSerial(void) {
18+
bool testPassed = true;
19+
USART_TypeDef *uart_rx = (USART_TypeDef *)pinmap_peripheral(digitalPinToPinName(PIN_SERIAL_RX), PinMap_UART_RX);
20+
USART_TypeDef *uart_tx = (USART_TypeDef *)pinmap_peripheral(digitalPinToPinName(PIN_SERIAL_TX), PinMap_UART_TX);
21+
if (uart_rx == NP) {
22+
/* PIN_SERIAL_RX (%d) doesn't match a valid UART peripheral */
23+
testPassed = false;
24+
}
25+
if (uart_tx == NP) {
26+
/* PIN_SERIAL_TX doesn't match a valid UART peripheral */
27+
testPassed = false;
28+
}
29+
if (uart_rx != uart_tx) {
30+
/* PIN_SERIAL_RX (%d) doesn't match PIN_SERIAL_TX peripheral */
31+
testPassed = false;
32+
}
33+
34+
return testPassed;
35+
}
36+
37+
/*
38+
Prints Pin name
1139
pname: Pin of type PinName (PY_n)
1240
asPN: true display as a PinName, false as a pin number (PYn)
13-
val: display value or not
14-
ln: carriage return or not
41+
val: display value or not
42+
ln: carriage return or not
1543
*/
1644
void printPinName(PinName pname, bool asPN, bool val, bool ln) {
1745
Serial.print("P");
@@ -31,6 +59,18 @@ void printPinName(PinName pname, bool asPN, bool val, bool ln) {
3159
}
3260

3361
void setup() {
62+
/* Check first whether Serial is valid and we can further print on Serial */
63+
if (!checkSerial()) {
64+
uint32_t blinkDelay = 200;
65+
while (1) {
66+
/* blink led quickly and forever in case of error */
67+
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
68+
delay(blinkDelay);
69+
digitalWrite(LED_BUILTIN, LOW); // turn the LED off
70+
delay(blinkDelay);
71+
}
72+
}
73+
3474
Serial.begin(115200);
3575
while (!Serial) {
3676
; // wait for serial port to connect. Needed for native USB port only
@@ -55,6 +95,9 @@ void loop() {
5595
if (!checkAnalogPins()) {
5696
testPassed = false;
5797
}
98+
if (!checkIPInstance()) {
99+
testPassed = false;
100+
}
58101

59102
/* Display test result */
60103
if (testPassed) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
Checks Wire instances (I2C)
3+
Returns true in case of test passed
4+
Returns false in case of test failed
5+
*/
6+
bool checkI2CInstance(void) {
7+
bool testPassed = true;
8+
9+
#if defined(PIN_WIRE_SDA) && defined(PIN_WIRE_SCL)
10+
I2C_TypeDef *i2c_sda = (I2C_TypeDef *)pinmap_peripheral(digitalPinToPinName(PIN_WIRE_SDA), PinMap_I2C_SDA);
11+
I2C_TypeDef *i2c_scl = (I2C_TypeDef *)pinmap_peripheral(digitalPinToPinName(PIN_WIRE_SCL), PinMap_I2C_SCL);
12+
if (i2c_sda == NP) {
13+
Serial.printf("PIN_WIRE_SDA (%d) doesn't match a valid I2C peripheral\n", PIN_WIRE_SDA);
14+
testPassed = false;
15+
}
16+
if (i2c_scl == NP) {
17+
Serial.printf("PIN_WIRE_SCL (%d) doesn't match a valid I2C peripheral\n", PIN_WIRE_SCL);
18+
testPassed = false;
19+
}
20+
if (i2c_sda != i2c_scl) {
21+
Serial.printf("PIN_WIRE_SCL (%d) doesn't match PIN_WIRE_SDA (%d) peripheral\n", PIN_WIRE_SCL, PIN_WIRE_SDA);
22+
testPassed = false;
23+
}
24+
#endif
25+
return testPassed;
26+
}
27+
28+
#if defined(SERIAL_UART_INSTANCE)
29+
/*
30+
Returns USART_TypeDef corresponding to SERIAL_UART_INSTANCE
31+
*/
32+
USART_TypeDef* getSerialInstance(void) {
33+
#if SERIAL_UART_INSTANCE == 0 || SERIAL_UART_INSTANCE == 101
34+
return LPUART1;
35+
#elif SERIAL_UART_INSTANCE == 1
36+
return USART1;
37+
#elif SERIAL_UART_INSTANCE == 2
38+
return USART2;
39+
#elif SERIAL_UART_INSTANCE == 3
40+
return USART3;
41+
#elif SERIAL_UART_INSTANCE == 4
42+
return USART4;
43+
#elif SERIAL_UART_INSTANCE == 5
44+
return USART5;
45+
#elif SERIAL_UART_INSTANCE == 6
46+
return USART6;
47+
#elif SERIAL_UART_INSTANCE == 7
48+
return USART7;
49+
#elif SERIAL_UART_INSTANCE == 8
50+
return USART8;
51+
#elif SERIAL_UART_INSTANCE == 9
52+
return USART9;
53+
#elif SERIAL_UART_INSTANCE == 10
54+
return USART10;
55+
#else
56+
return NULL;
57+
#endif
58+
}
59+
#endif /* SERIAL_UART_INSTANCE */
60+
61+
/*
62+
Checks Serial instances (UART)
63+
Returns true in case of test passed
64+
Returns false in case of test failed
65+
*/
66+
bool checkSerialInstance(void) {
67+
bool testPassed = true;
68+
69+
#if defined(PIN_SERIAL_RX) && defined(PIN_SERIAL_TX)
70+
USART_TypeDef *uart_rx = (USART_TypeDef *)pinmap_peripheral(digitalPinToPinName(PIN_SERIAL_RX), PinMap_UART_RX);
71+
USART_TypeDef *uart_tx = (USART_TypeDef *)pinmap_peripheral(digitalPinToPinName(PIN_SERIAL_TX), PinMap_UART_TX);
72+
if (uart_rx == NP) {
73+
Serial.printf("PIN_SERIAL_RX (%d) doesn't match a valid UART peripheral\n", PIN_SERIAL_RX);
74+
testPassed = false;
75+
}
76+
if (uart_tx == NP) {
77+
Serial.printf("PIN_SERIAL_TX (%d) doesn't match a valid UART peripheral\n", PIN_SERIAL_TX);
78+
testPassed = false;
79+
}
80+
if (uart_rx != uart_tx) {
81+
Serial.printf("PIN_SERIAL_RX (%d) doesn't match PIN_SERIAL_TX (%d) peripheral\n", PIN_SERIAL_RX, PIN_SERIAL_TX);
82+
testPassed = false;
83+
}
84+
85+
#if defined(SERIAL_UART_INSTANCE)
86+
USART_TypeDef* usart = getSerialInstance();
87+
if (usart != uart_tx) {
88+
Serial.printf("SERIAL_UART_INSTANCE (%d) doesn't match PIN_SERIAL_TX (%d) peripheral\n", SERIAL_UART_INSTANCE, PIN_SERIAL_TX);
89+
testPassed = false;
90+
}
91+
#endif
92+
93+
#endif /* PIN_SERIAL_RX && PIN_SERIAL_TX */
94+
95+
return testPassed;
96+
}
97+
98+
/*
99+
Checks SPI instances
100+
Returns true in case of test passed
101+
Returns false in case of test failed
102+
*/
103+
bool checkSPIInstance(void) {
104+
bool testPassed = true;
105+
106+
#if defined(PIN_SPI_MOSI) && defined(PIN_SPI_MISO)
107+
SPI_TypeDef *spi_mosi = (SPI_TypeDef *)pinmap_peripheral(digitalPinToPinName(PIN_SPI_MOSI), PinMap_SPI_MOSI);
108+
SPI_TypeDef *spi_miso = (SPI_TypeDef *)pinmap_peripheral(digitalPinToPinName(PIN_SPI_MISO), PinMap_SPI_MISO);
109+
if (spi_mosi == NP) {
110+
Serial.printf("PIN_SPI_MOSI (%d) doesn't match a valid SPI peripheral\n", PIN_SPI_MOSI);
111+
testPassed = false;
112+
}
113+
if (spi_miso == NP) {
114+
Serial.printf("PIN_SPI_MISO (%d) doesn't match a valid SPI peripheral\n", PIN_SPI_MISO);
115+
testPassed = false;
116+
}
117+
if (spi_mosi != spi_miso) {
118+
Serial.printf("PIN_SPI_MOSI (%d) doesn't match PIN_SPI_MISO (%d) peripheral\n", PIN_SPI_MOSI, PIN_SPI_MISO);
119+
testPassed = false;
120+
}
121+
122+
#if defined(PIN_SPI_SCK)
123+
SPI_TypeDef *spi_sck = (SPI_TypeDef *)pinmap_peripheral(digitalPinToPinName(PIN_SPI_SCK), PinMap_SPI_SCLK);
124+
if (spi_sck == NP) {
125+
Serial.printf("PIN_SPI_SCK (%d) doesn't match a valid SPI peripheral\n", PIN_SPI_SCK);
126+
testPassed = false;
127+
}
128+
if (spi_sck != spi_mosi) {
129+
Serial.printf("PIN_SPI_SCK (%d) doesn't match PIN_SPI_MISO (%d) peripheral\n", PIN_SPI_SCK, PIN_SPI_MISO);
130+
testPassed = false;
131+
}
132+
#endif
133+
134+
#endif /* PIN_SPI_MOSI && PIN_SPI_MISO */
135+
136+
#if defined(PIN_SPI_SS)
137+
/* PIN_SPI_SS may be used as software chip select (and not hardware) any pin is valid
138+
thus not possible to check this pin agianst PinMap_SPI_SSEL */
139+
if (!digitalPinIsValid(PIN_SPI_SS)) {
140+
Serial.printf("PIN_SPI_SS (%d) doesn't match PIN_SPI_MISO (%d) peripheral\n", PIN_SPI_SS, PIN_SPI_MISO);
141+
testPassed = false;
142+
}
143+
#endif
144+
145+
return testPassed;
146+
}
147+
148+
/*
149+
Check IP instances (I2C, UART, SPI)
150+
Return true in case of test passed
151+
Return false in case of test failed
152+
*/
153+
bool checkIPInstance(void) {
154+
bool testPassed = true;
155+
156+
Serial.println("#####");
157+
Serial.println("Checking IP instances (I2C, UART, SPI)...");
158+
159+
if (!checkI2CInstance()) {
160+
testPassed = false;
161+
}
162+
if (!checkSerialInstance()) {
163+
testPassed = false;
164+
}
165+
if (!checkSPIInstance()) {
166+
testPassed = false;
167+
}
168+
Serial.println("End check IP instances (I2C, UART, SPI)");
169+
return testPassed;
170+
}

0 commit comments

Comments
 (0)