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