|
| 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