Skip to content

Add CheckVariant sketch for non regression test purpose #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions examples/NonReg/CheckVariant/CheckVariant.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
This sketch check:
- digital and analog pins defined in the variant
- peripheral instances associated to wire (I2C), serial (UART) and SPI
*/

#include "utils.h"

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

/*
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;
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;
}

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);
}
}
49 changes: 49 additions & 0 deletions examples/NonReg/CheckVariant/analogPinsTest.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Check Analog pins
Return true in case of test passed
Return false in case of test failed
*/
bool checkAnalogPins(void) {
bool testPassed = true;

Serial.println("#####");
Serial.println("Checking analog pins definition...");

for ( uint32_t i = 0; i < (NUM_ANALOG_INPUTS); i++) {
uint8_t res = 0;
// i is the index in the analogInputPin array
uint32_t pinnum_array = analogInputPin[i];
uint32_t pinnum_aTD_i = analogInputToDigitalPin(i);
uint32_t pinnum_aTD_Ax = analogInputToDigitalPin(A0 + i);

PinName pn_dTpn_Ax = digitalPinToPinName(A0 + i);
PinName pn_aTpn_Ax = analogInputToPinName(A0 + i);


if ((pinnum_array != pinnum_aTD_i) || (pinnum_array != pinnum_aTD_Ax) || (pinnum_aTD_i == NC)) {
res = 1;
}
if (!digitalpinIsAnalogInput(pinnum_array) || !digitalpinIsAnalogInput(pinnum_aTD_i) || !digitalpinIsAnalogInput(pinnum_aTD_Ax)) {
res |= 2;
}
if ((pn_dTpn_Ax != pn_aTpn_Ax) || (pinnum_aTD_i == NC)) {
res |= 4;
}
if (digitalPinToAnalogInput(pinnum_aTD_i) != i) {

res |= 8;
}

if (res) {
Serial.printf("A%i defined as %i with pin name: ", i, A0 + i);
printPinName(pn_aTpn_Ax, true, true, false);
Serial.print(" and pin number: ");
printPinName(pn_aTpn_Ax, false, true, false);
Serial.printf(" --> %i\n", res);
Serial.printf(" --> digitalPinToAnalogInput(%i) = %i\n", pinnum_aTD_i, digitalPinToAnalogInput(pinnum_aTD_i));
testPassed = false;
}
}
Serial.println("End check analog pins");
return testPassed;
}
170 changes: 170 additions & 0 deletions examples/NonReg/CheckVariant/checkIPInstanceTest.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
Checks Wire instances (I2C)
Returns true in case of test passed
Returns false in case of test failed
*/
bool checkI2CInstance(void) {
bool testPassed = true;

#if defined(PIN_WIRE_SDA) && defined(PIN_WIRE_SCL)
I2C_TypeDef *i2c_sda = (I2C_TypeDef *)pinmap_peripheral(digitalPinToPinName(PIN_WIRE_SDA), PinMap_I2C_SDA);
I2C_TypeDef *i2c_scl = (I2C_TypeDef *)pinmap_peripheral(digitalPinToPinName(PIN_WIRE_SCL), PinMap_I2C_SCL);
if (i2c_sda == NP) {
Serial.printf("PIN_WIRE_SDA (%d) doesn't match a valid I2C peripheral\n", PIN_WIRE_SDA);
testPassed = false;
}
if (i2c_scl == NP) {
Serial.printf("PIN_WIRE_SCL (%d) doesn't match a valid I2C peripheral\n", PIN_WIRE_SCL);
testPassed = false;
}
if (i2c_sda != i2c_scl) {
Serial.printf("PIN_WIRE_SCL (%d) doesn't match PIN_WIRE_SDA (%d) peripheral\n", PIN_WIRE_SCL, PIN_WIRE_SDA);
testPassed = false;
}
#endif
return testPassed;
}

#if defined(SERIAL_UART_INSTANCE)
/*
Returns USART_TypeDef corresponding to SERIAL_UART_INSTANCE
*/
USART_TypeDef* getSerialInstance(void) {
#if SERIAL_UART_INSTANCE == 0 || SERIAL_UART_INSTANCE == 101
return LPUART1;
#elif SERIAL_UART_INSTANCE == 1
return USART1;
#elif SERIAL_UART_INSTANCE == 2
return USART2;
#elif SERIAL_UART_INSTANCE == 3
return USART3;
#elif SERIAL_UART_INSTANCE == 4
return USART4;
#elif SERIAL_UART_INSTANCE == 5
return USART5;
#elif SERIAL_UART_INSTANCE == 6
return USART6;
#elif SERIAL_UART_INSTANCE == 7
return USART7;
#elif SERIAL_UART_INSTANCE == 8
return USART8;
#elif SERIAL_UART_INSTANCE == 9
return USART9;
#elif SERIAL_UART_INSTANCE == 10
return USART10;
#else
return NULL;
#endif
}
#endif /* SERIAL_UART_INSTANCE */

/*
Checks Serial instances (UART)
Returns true in case of test passed
Returns false in case of test failed
*/
bool checkSerialInstance(void) {
bool testPassed = true;

#if defined(PIN_SERIAL_RX) && defined(PIN_SERIAL_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) {
Serial.printf("PIN_SERIAL_RX (%d) doesn't match a valid UART peripheral\n", PIN_SERIAL_RX);
testPassed = false;
}
if (uart_tx == NP) {
Serial.printf("PIN_SERIAL_TX (%d) doesn't match a valid UART peripheral\n", PIN_SERIAL_TX);
testPassed = false;
}
if (uart_rx != uart_tx) {
Serial.printf("PIN_SERIAL_RX (%d) doesn't match PIN_SERIAL_TX (%d) peripheral\n", PIN_SERIAL_RX, PIN_SERIAL_TX);
testPassed = false;
}

#if defined(SERIAL_UART_INSTANCE)
USART_TypeDef* usart = getSerialInstance();
if (usart != uart_tx) {
Serial.printf("SERIAL_UART_INSTANCE (%d) doesn't match PIN_SERIAL_TX (%d) peripheral\n", SERIAL_UART_INSTANCE, PIN_SERIAL_TX);
testPassed = false;
}
#endif

#endif /* PIN_SERIAL_RX && PIN_SERIAL_TX */

return testPassed;
}

/*
Checks SPI instances
Returns true in case of test passed
Returns false in case of test failed
*/
bool checkSPIInstance(void) {
bool testPassed = true;

#if defined(PIN_SPI_MOSI) && defined(PIN_SPI_MISO)
SPI_TypeDef *spi_mosi = (SPI_TypeDef *)pinmap_peripheral(digitalPinToPinName(PIN_SPI_MOSI), PinMap_SPI_MOSI);
SPI_TypeDef *spi_miso = (SPI_TypeDef *)pinmap_peripheral(digitalPinToPinName(PIN_SPI_MISO), PinMap_SPI_MISO);
if (spi_mosi == NP) {
Serial.printf("PIN_SPI_MOSI (%d) doesn't match a valid SPI peripheral\n", PIN_SPI_MOSI);
testPassed = false;
}
if (spi_miso == NP) {
Serial.printf("PIN_SPI_MISO (%d) doesn't match a valid SPI peripheral\n", PIN_SPI_MISO);
testPassed = false;
}
if (spi_mosi != spi_miso) {
Serial.printf("PIN_SPI_MOSI (%d) doesn't match PIN_SPI_MISO (%d) peripheral\n", PIN_SPI_MOSI, PIN_SPI_MISO);
testPassed = false;
}

#if defined(PIN_SPI_SCK)
SPI_TypeDef *spi_sck = (SPI_TypeDef *)pinmap_peripheral(digitalPinToPinName(PIN_SPI_SCK), PinMap_SPI_SCLK);
if (spi_sck == NP) {
Serial.printf("PIN_SPI_SCK (%d) doesn't match a valid SPI peripheral\n", PIN_SPI_SCK);
testPassed = false;
}
if (spi_sck != spi_mosi) {
Serial.printf("PIN_SPI_SCK (%d) doesn't match PIN_SPI_MISO (%d) peripheral\n", PIN_SPI_SCK, PIN_SPI_MISO);
testPassed = false;
}
#endif

#endif /* PIN_SPI_MOSI && PIN_SPI_MISO */

#if defined(PIN_SPI_SS)
/* PIN_SPI_SS may be used as software chip select (and not hardware) any pin is valid
thus not possible to check this pin agianst PinMap_SPI_SSEL */
if (!digitalPinIsValid(PIN_SPI_SS)) {
Serial.printf("PIN_SPI_SS (%d) doesn't match PIN_SPI_MISO (%d) peripheral\n", PIN_SPI_SS, PIN_SPI_MISO);
testPassed = false;
}
#endif

return testPassed;
}

/*
Check IP instances (I2C, UART, SPI)
Return true in case of test passed
Return false in case of test failed
*/
bool checkIPInstance(void) {
bool testPassed = true;

Serial.println("#####");
Serial.println("Checking IP instances (I2C, UART, SPI)...");

if (!checkI2CInstance()) {
testPassed = false;
}
if (!checkSerialInstance()) {
testPassed = false;
}
if (!checkSPIInstance()) {
testPassed = false;
}
Serial.println("End check IP instances (I2C, UART, SPI)");
return testPassed;
}
Loading