Skip to content

Commit f8bc545

Browse files
authored
Merge pull request #43 from ABOSTM/CHECK_VARIANT
Add CheckVariant sketch for non regression test purpose
2 parents 25dbfe9 + 1fefdfc commit f8bc545

File tree

4 files changed

+933
-0
lines changed

4 files changed

+933
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
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+
*/
6+
7+
#include "utils.h"
8+
9+
#define PORTx(pn) (char)('A' + STM_PORT(pn))
10+
#define PINx(pn) STM_PIN(pn)
11+
12+
/*
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
39+
pname: Pin of type PinName (PY_n)
40+
asPN: true display as a PinName, false as a pin number (PYn)
41+
val: display value or not
42+
ln: carriage return or not
43+
*/
44+
void printPinName(PinName pname, bool asPN, bool val, bool ln) {
45+
Serial.print("P");
46+
Serial.print(PORTx(pname));
47+
if (asPN) {
48+
Serial.print("_");
49+
}
50+
Serial.print(PINx(pname));
51+
if (val) {
52+
Serial.print(" (");
53+
Serial.print(asPN ? (uint32_t)pname : pinNametoDigitalPin(pname));
54+
Serial.print(")");
55+
}
56+
if (ln) {
57+
Serial.println();
58+
}
59+
}
60+
61+
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+
74+
Serial.begin(115200);
75+
while (!Serial) {
76+
; // wait for serial port to connect. Needed for native USB port only
77+
}
78+
pinMode(LED_BUILTIN, OUTPUT);
79+
}
80+
81+
void loop() {
82+
83+
bool testPassed = true;
84+
String testStatus;
85+
uint32_t blinkDelay;
86+
87+
Serial.println("#####");
88+
Serial.printf("NUM_DIGITAL_PINS = %i\n", NUM_DIGITAL_PINS);
89+
Serial.printf("NUM_ANALOG_INPUTS = %i\n", NUM_ANALOG_INPUTS);
90+
91+
/* Run the different tests */
92+
if (!checkDigitalPins()) {
93+
testPassed = false;
94+
}
95+
if (!checkAnalogPins()) {
96+
testPassed = false;
97+
}
98+
if (!checkIPInstance()) {
99+
testPassed = false;
100+
}
101+
102+
/* Display test result */
103+
if (testPassed) {
104+
testStatus = "PASSED";
105+
blinkDelay = 1000;
106+
} else {
107+
testStatus = "FAILED";
108+
blinkDelay = 200;
109+
}
110+
Serial.println("");
111+
Serial.println("########################################");
112+
Serial.printf("#### Test %s\n", testStatus.c_str());
113+
Serial.println("########################################");
114+
115+
/* Blink Led forever, slowly for test passed, and quickly for test failed */
116+
while (1) {
117+
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
118+
delay(blinkDelay);
119+
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
120+
delay(blinkDelay);
121+
}
122+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Check Analog pins
3+
Return true in case of test passed
4+
Return false in case of test failed
5+
*/
6+
bool checkAnalogPins(void) {
7+
bool testPassed = true;
8+
9+
Serial.println("#####");
10+
Serial.println("Checking analog pins definition...");
11+
12+
for ( uint32_t i = 0; i < (NUM_ANALOG_INPUTS); i++) {
13+
uint8_t res = 0;
14+
// i is the index in the analogInputPin array
15+
uint32_t pinnum_array = analogInputPin[i];
16+
uint32_t pinnum_aTD_i = analogInputToDigitalPin(i);
17+
uint32_t pinnum_aTD_Ax = analogInputToDigitalPin(A0 + i);
18+
19+
PinName pn_dTpn_Ax = digitalPinToPinName(A0 + i);
20+
PinName pn_aTpn_Ax = analogInputToPinName(A0 + i);
21+
22+
23+
if ((pinnum_array != pinnum_aTD_i) || (pinnum_array != pinnum_aTD_Ax) || (pinnum_aTD_i == NC)) {
24+
res = 1;
25+
}
26+
if (!digitalpinIsAnalogInput(pinnum_array) || !digitalpinIsAnalogInput(pinnum_aTD_i) || !digitalpinIsAnalogInput(pinnum_aTD_Ax)) {
27+
res |= 2;
28+
}
29+
if ((pn_dTpn_Ax != pn_aTpn_Ax) || (pinnum_aTD_i == NC)) {
30+
res |= 4;
31+
}
32+
if (digitalPinToAnalogInput(pinnum_aTD_i) != i) {
33+
34+
res |= 8;
35+
}
36+
37+
if (res) {
38+
Serial.printf("A%i defined as %i with pin name: ", i, A0 + i);
39+
printPinName(pn_aTpn_Ax, true, true, false);
40+
Serial.print(" and pin number: ");
41+
printPinName(pn_aTpn_Ax, false, true, false);
42+
Serial.printf(" --> %i\n", res);
43+
Serial.printf(" --> digitalPinToAnalogInput(%i) = %i\n", pinnum_aTD_i, digitalPinToAnalogInput(pinnum_aTD_i));
44+
testPassed = false;
45+
}
46+
}
47+
Serial.println("End check analog pins");
48+
return testPassed;
49+
}
Lines changed: 170 additions & 0 deletions
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)