-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathCheckVariant.ino
131 lines (117 loc) · 3.5 KB
/
CheckVariant.ino
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
/*
This sketch check:
- digital and analog pins defined in the variant
- peripheral instances associated to wire (I2C), serial (UART) and SPI
*/
#include "utils.h"
#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION <= 0x01090000)
#error "This sketch is not compatible with core version prior to 2.0.0"
#endif
#define PORTx(pn) (char)('A' + STM_PORT(pn))
#define PINx(pn) STM_PIN(pn)
#ifndef LED_BUILTIN
#define LED_BUILTIN PNUM_NOT_DEFINED
#endif
/*
Initial check of Serial to be sure we can further print on serial
Returns true in case of test passed
Returns false in case of test failed
*/
bool checkSerial(void) {
bool testPassed = true;
#if defined(PinMap_UART_RX) && defined(PinMap_UART_TX)
USART_TypeDef *uart_rx = (USART_TypeDef *)pinmap_peripheral(digitalPinToPinName(PIN_SERIAL_RX), PinMap_UART_RX);
USART_TypeDef *uart_tx = (USART_TypeDef *)pinmap_peripheral(digitalPinToPinName(PIN_SERIAL_TX), PinMap_UART_TX);
if (uart_rx == NP) {
/* PIN_SERIAL_RX (%d) doesn't match a valid UART peripheral */
testPassed = false;
}
if (uart_tx == NP) {
/* PIN_SERIAL_TX doesn't match a valid UART peripheral */
testPassed = false;
}
if (uart_rx != uart_tx) {
/* PIN_SERIAL_RX (%d) doesn't match PIN_SERIAL_TX peripheral */
testPassed = false;
}
#endif
return testPassed;
}
/*
Prints Pin name
pname: Pin of type PinName (PY_n)
asPN: true display as a PinName, false as a pin number (PYn)
val: display value or not
ln: carriage return or not
*/
void printPinName(PinName pname, bool asPN, bool val, bool ln) {
Serial.print("P");
Serial.print(PORTx(pname));
if (asPN) {
Serial.print("_");
}
Serial.print(PINx(pname));
if (val) {
Serial.print(" (");
Serial.print(asPN ? (uint32_t)pname : pinNametoDigitalPin(pname));
Serial.print(")");
}
if (ln) {
Serial.println();
}
}
void setup() {
/* Check first whether Serial is valid and we can further print on Serial */
if (!checkSerial()) {
uint32_t blinkDelay = 200;
while (1) {
/* blink led quickly and forever in case of error */
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
delay(blinkDelay);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off
delay(blinkDelay);
}
}
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
bool testPassed = true;
String testStatus;
uint32_t blinkDelay;
Serial.println("#####");
Serial.printf("NUM_DIGITAL_PINS = %i\n", NUM_DIGITAL_PINS);
Serial.printf("NUM_ANALOG_INPUTS = %i\n", NUM_ANALOG_INPUTS);
/* Run the different tests */
if (!checkDigitalPins()) {
testPassed = false;
}
if (!checkAnalogPins()) {
testPassed = false;
}
if (!checkIPInstance()) {
testPassed = false;
}
/* Display test result */
if (testPassed) {
testStatus = "PASSED";
blinkDelay = 1000;
} else {
testStatus = "FAILED";
blinkDelay = 200;
}
Serial.println("");
Serial.println("########################################");
Serial.printf("#### Test %s\n", testStatus.c_str());
Serial.println("########################################");
/* Blink Led forever, slowly for test passed, and quickly for test failed */
while (1) {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(blinkDelay);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(blinkDelay);
}
}