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