|
| 1 | +/* Peripheral Manager test |
| 2 | + * |
| 3 | + * This test is using Serial to check if the peripheral manager is able to |
| 4 | + * attach and detach peripherals correctly on shared pins. |
| 5 | + * Make sure that the peripheral names contain only letters, numbers and underscores. |
| 6 | + * |
| 7 | + * This test skips the following peripherals: |
| 8 | + * - USB: USB is not able to be detached |
| 9 | + * - SDMMC: SDMMC requires a card to be mounted before the pins are attached |
| 10 | + */ |
| 11 | + |
| 12 | +#if SOC_I2S_SUPPORTED |
| 13 | +#include "ESP_I2S.h" |
| 14 | +#endif |
| 15 | + |
| 16 | +#if SOC_I2C_SUPPORTED |
| 17 | +#include "Wire.h" |
| 18 | +#endif |
| 19 | + |
| 20 | +#if SOC_GPSPI_SUPPORTED |
| 21 | +#include "SPI.h" |
| 22 | +#endif |
| 23 | + |
| 24 | +#define ETH_PHY_TYPE ETH_PHY_LAN8720 |
| 25 | +#define ETH_PHY_ADDR 0 |
| 26 | +#define ETH_PHY_MDC 23 |
| 27 | +#define ETH_PHY_MDIO 18 |
| 28 | +#define ETH_PHY_POWER -1 |
| 29 | +#define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN |
| 30 | + |
| 31 | +#include "ETH.h" |
| 32 | + |
| 33 | +/* Definitions */ |
| 34 | + |
| 35 | +#define UART1_RX_DEFAULT 4 |
| 36 | +#define UART1_TX_DEFAULT 5 |
| 37 | + |
| 38 | +#define ADC1_DEFAULT A4 |
| 39 | + |
| 40 | +#if CONFIG_IDF_TARGET_ESP32 |
| 41 | +# define ADC2_DEFAULT A5 |
| 42 | +#else |
| 43 | +# define ADC2_DEFAULT A3 |
| 44 | +#endif |
| 45 | + |
| 46 | +#if CONFIG_IDF_TARGET_ESP32 |
| 47 | +# define TOUCH1_DEFAULT T0 |
| 48 | +# define TOUCH2_DEFAULT T2 |
| 49 | +#else |
| 50 | +# define TOUCH1_DEFAULT T4 |
| 51 | +# define TOUCH2_DEFAULT T5 |
| 52 | +#endif |
| 53 | + |
| 54 | +/* Global variables */ |
| 55 | + |
| 56 | +bool test_executed = false; |
| 57 | +String current_test; |
| 58 | +int8_t uart1_rx_pin; |
| 59 | +int8_t uart1_tx_pin; |
| 60 | + |
| 61 | +/* Callback functions */ |
| 62 | + |
| 63 | +void onReceive_cb(void) { |
| 64 | + // This is a callback function that will be activated on UART RX events |
| 65 | + size_t available = Serial1.available(); |
| 66 | + while (available --) { |
| 67 | + Serial.print((char)Serial1.read()); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// This function is called by before each test is run |
| 72 | +void setup_test(String test_name, int8_t rx_pin = UART1_RX_DEFAULT, int8_t tx_pin = UART1_TX_DEFAULT) { |
| 73 | + log_v("Setting up %s test", test_name.c_str()); |
| 74 | + |
| 75 | + current_test = test_name; |
| 76 | + uart1_rx_pin = rx_pin; |
| 77 | + uart1_tx_pin = tx_pin; |
| 78 | + test_executed = false; |
| 79 | + |
| 80 | + pinMode(uart1_rx_pin, INPUT_PULLUP); |
| 81 | + pinMode(uart1_tx_pin, OUTPUT); |
| 82 | + Serial1.setPins(uart1_rx_pin, uart1_tx_pin); |
| 83 | + uart_internal_loopback(1, uart1_rx_pin); |
| 84 | + delay(100); |
| 85 | + log_v("Running %s test", test_name.c_str()); |
| 86 | +} |
| 87 | + |
| 88 | +// This function is called after each test is run |
| 89 | +void teardown_test(void) { |
| 90 | + log_v("Tearing down %s test", current_test.c_str()); |
| 91 | + if (test_executed) { |
| 92 | + pinMode(uart1_rx_pin, INPUT_PULLUP); |
| 93 | + pinMode(uart1_tx_pin, OUTPUT); |
| 94 | + Serial1.print(current_test); |
| 95 | + Serial1.println(" test: This should not be printed"); |
| 96 | + Serial1.flush(); |
| 97 | + |
| 98 | + Serial1.setPins(uart1_rx_pin, uart1_tx_pin); |
| 99 | + uart_internal_loopback(1, uart1_rx_pin); |
| 100 | + delay(100); |
| 101 | + } |
| 102 | + |
| 103 | + Serial1.print(current_test); |
| 104 | + Serial1.println(" test: This should be printed"); |
| 105 | + Serial1.flush(); |
| 106 | + |
| 107 | + log_v("Finished %s test", current_test.c_str()); |
| 108 | +} |
| 109 | + |
| 110 | +/* Test functions */ |
| 111 | +/* These functions must only init the peripheral on the same pins and update "current_test" */ |
| 112 | + |
| 113 | +void gpio_test(void) { |
| 114 | + setup_test("GPIO"); |
| 115 | + test_executed = true; |
| 116 | + pinMode(uart1_rx_pin, INPUT); |
| 117 | + pinMode(uart1_tx_pin, OUTPUT); |
| 118 | + digitalRead(uart1_rx_pin); |
| 119 | + digitalWrite(uart1_tx_pin, HIGH); |
| 120 | + teardown_test(); |
| 121 | +} |
| 122 | + |
| 123 | +void sigmadelta_test(void) { |
| 124 | + setup_test("SigmaDelta"); |
| 125 | +#if SOC_SDM_SUPPORTED |
| 126 | + test_executed = true; |
| 127 | + if (!sigmaDeltaAttach(uart1_rx_pin, 312500)) { |
| 128 | + Serial.println("SigmaDelta init failed"); |
| 129 | + } |
| 130 | + if (!sigmaDeltaAttach(uart1_tx_pin, 312500)) { |
| 131 | + Serial.println("SigmaDelta init failed"); |
| 132 | + } |
| 133 | +#endif |
| 134 | + teardown_test(); |
| 135 | +} |
| 136 | + |
| 137 | +void adc_oneshot_test(void) { |
| 138 | +#if !SOC_ADC_SUPPORTED |
| 139 | + setup_test("ADC_Oneshot"); |
| 140 | +#else |
| 141 | + setup_test("ADC_Oneshot", ADC1_DEFAULT, ADC2_DEFAULT); |
| 142 | + test_executed = true; |
| 143 | + analogReadResolution(12); |
| 144 | + pinMode(ADC1_DEFAULT, INPUT); |
| 145 | + pinMode(ADC2_DEFAULT, INPUT); |
| 146 | + analogRead(ADC1_DEFAULT); |
| 147 | + analogRead(ADC2_DEFAULT); |
| 148 | +#endif |
| 149 | + teardown_test(); |
| 150 | +} |
| 151 | + |
| 152 | +#if SOC_ADC_SUPPORTED |
| 153 | +volatile bool adc_coversion_done = false; |
| 154 | +void ARDUINO_ISR_ATTR adcComplete() { |
| 155 | + adc_coversion_done = true; |
| 156 | +} |
| 157 | +#endif |
| 158 | + |
| 159 | +void adc_continuous_test(void) { |
| 160 | +#if !SOC_ADC_SUPPORTED |
| 161 | + setup_test("ADC_Continuous"); |
| 162 | +#else |
| 163 | + setup_test("ADC_Continuous", ADC1_DEFAULT, ADC2_DEFAULT); |
| 164 | + test_executed = true; |
| 165 | + uint8_t adc_pins[] = {ADC1_DEFAULT, ADC2_DEFAULT}; |
| 166 | + uint8_t adc_pins_count = 2; |
| 167 | + adc_continuos_data_t * result = NULL; |
| 168 | + |
| 169 | + analogContinuousSetWidth(12); |
| 170 | + analogContinuousSetAtten(ADC_11db); |
| 171 | + |
| 172 | + analogContinuous(adc_pins, adc_pins_count, 6, 20000, &adcComplete); |
| 173 | + analogContinuousStart(); |
| 174 | + |
| 175 | + while (adc_coversion_done == false) { |
| 176 | + delay(1); |
| 177 | + } |
| 178 | + |
| 179 | + if (!analogContinuousRead(&result, 0)) { |
| 180 | + Serial.println("ADC continuous read failed"); |
| 181 | + } |
| 182 | + |
| 183 | + analogContinuousStop(); |
| 184 | +#endif |
| 185 | + teardown_test(); |
| 186 | +} |
| 187 | + |
| 188 | +void dac_test(void) { |
| 189 | +#if !SOC_DAC_SUPPORTED |
| 190 | + setup_test("DAC"); |
| 191 | +#else |
| 192 | + setup_test("DAC", DAC1, DAC2); |
| 193 | + test_executed = true; |
| 194 | + dacWrite(DAC1, 255); |
| 195 | + dacWrite(DAC2, 255); |
| 196 | +#endif |
| 197 | + teardown_test(); |
| 198 | +} |
| 199 | + |
| 200 | +void ledc_test(void) { |
| 201 | + setup_test("LEDC"); |
| 202 | +#if SOC_LEDC_SUPPORTED |
| 203 | + test_executed = true; |
| 204 | + if (!ledcAttach(uart1_rx_pin, 5000, 12)) { |
| 205 | + Serial.println("LEDC init failed"); |
| 206 | + } |
| 207 | + if (!ledcAttach(uart1_tx_pin, 5000, 12)) { |
| 208 | + Serial.println("LEDC init failed"); |
| 209 | + } |
| 210 | +#endif |
| 211 | + teardown_test(); |
| 212 | +} |
| 213 | + |
| 214 | +void rmt_test(void) { |
| 215 | + setup_test("RMT"); |
| 216 | +#if SOC_RMT_SUPPORTED |
| 217 | + test_executed = true; |
| 218 | + if (!rmtInit(uart1_rx_pin, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000)) { |
| 219 | + Serial.println("RMT init failed"); |
| 220 | + } |
| 221 | + if (!rmtInit(uart1_tx_pin, RMT_RX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000)) { |
| 222 | + Serial.println("RMT init failed"); |
| 223 | + } |
| 224 | +#endif |
| 225 | + teardown_test(); |
| 226 | +} |
| 227 | + |
| 228 | +void i2s_test(void) { |
| 229 | + setup_test("I2S"); |
| 230 | +#if SOC_I2S_SUPPORTED |
| 231 | + test_executed = true; |
| 232 | + I2SClass i2s; |
| 233 | + |
| 234 | + i2s.setPins(uart1_rx_pin, uart1_tx_pin, -1); |
| 235 | + i2s.setTimeout(1000); |
| 236 | + if (!i2s.begin(I2S_MODE_STD, 16000, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO)) { |
| 237 | + Serial.println("I2S init failed"); |
| 238 | + } |
| 239 | +#endif |
| 240 | + teardown_test(); |
| 241 | +} |
| 242 | + |
| 243 | +void i2c_test(void) { |
| 244 | + setup_test("I2C"); |
| 245 | +#if SOC_I2C_SUPPORTED |
| 246 | + test_executed = true; |
| 247 | + if (!Wire.begin(uart1_rx_pin, uart1_tx_pin)) { |
| 248 | + Serial.println("I2C init failed"); |
| 249 | + } |
| 250 | +#endif |
| 251 | + teardown_test(); |
| 252 | +} |
| 253 | + |
| 254 | +void spi_test(void) { |
| 255 | + setup_test("SPI"); |
| 256 | +#if SOC_GPSPI_SUPPORTED |
| 257 | + test_executed = true; |
| 258 | + SPI.begin(uart1_rx_pin, uart1_tx_pin, -1, -1); |
| 259 | +#endif |
| 260 | + teardown_test(); |
| 261 | +} |
| 262 | + |
| 263 | +void touch_test(void) { |
| 264 | +#if !SOC_TOUCH_SENSOR_SUPPORTED |
| 265 | + setup_test("Touch"); |
| 266 | +#else |
| 267 | + setup_test("Touch", TOUCH1_DEFAULT, TOUCH2_DEFAULT); |
| 268 | + test_executed = true; |
| 269 | + touchRead(TOUCH1_DEFAULT); |
| 270 | + touchRead(TOUCH2_DEFAULT); |
| 271 | +#endif |
| 272 | + teardown_test(); |
| 273 | +} |
| 274 | + |
| 275 | +void eth_test(void) { |
| 276 | + setup_test("ETH"); |
| 277 | + test_executed = true; |
| 278 | + ETH.begin(); |
| 279 | + teardown_test(); |
| 280 | +} |
| 281 | + |
| 282 | +/* Main functions */ |
| 283 | + |
| 284 | +void setup() { |
| 285 | + Serial.begin(115200); |
| 286 | + while(!Serial) { delay(10); } |
| 287 | + |
| 288 | + Serial1.setPins(UART1_RX_DEFAULT, UART1_TX_DEFAULT); |
| 289 | + Serial1.begin(115200); |
| 290 | + while(!Serial1) { delay(10); } |
| 291 | + Serial1.onReceive(onReceive_cb); |
| 292 | + uart_internal_loopback(1, uart1_rx_pin); |
| 293 | + |
| 294 | + gpio_test(); |
| 295 | + sigmadelta_test(); |
| 296 | + ledc_test(); |
| 297 | + rmt_test(); |
| 298 | + i2s_test(); |
| 299 | + i2c_test(); |
| 300 | + spi_test(); |
| 301 | + adc_oneshot_test(); |
| 302 | + adc_continuous_test(); |
| 303 | + dac_test(); |
| 304 | + touch_test(); |
| 305 | + eth_test(); |
| 306 | +} |
| 307 | + |
| 308 | +void loop() {} |
0 commit comments