#include "main.h" #include "MP45DT02.h" #include "lcd.h" #define SAMPLE_AMOUNT 256 struct TestResult { unsigned int lo_present:1; unsigned int hi_present:1; }; int MP45DT02_run_test(void) { DISPLAY_CLEAR; display_write_data_seq("MP45DT02 Mic"); uint16_t sample_buffer[SAMPLE_AMOUNT]; for (size_t t = 0; t < 5; t++) { // gather samples register unsigned int reset_value = (GPIO_PIN_10 << 16); for (size_t i = 0; i < SAMPLE_AMOUNT; i++) { sample_buffer[i] = 0; for (size_t j = 0; j < 16; j++) { for (int k = 0; k < 16; k++) { GPIOB->BSRR = reset_value; asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); GPIOB->BSRR = GPIO_PIN_10; } sample_buffer[i] <<= 1; sample_buffer[i] |= (GPIOC->IDR & (1 << 3)) >> 3; } } struct TestResult res = {}; // look for zeros for (size_t i = 0; i < SAMPLE_AMOUNT / 2; i++) if (~((uint32_t *) sample_buffer)[i]) { res.lo_present = 1; break; } // look for ones for (size_t i = 0; i < SAMPLE_AMOUNT / 2; i++) if (((uint32_t *) sample_buffer)[i]) { res.hi_present = 1; break; } DISPLAY_CLEAR; display_write_data_seq("MP45DT02 Mic"); if (t) { DISPLAY_SET_CURSOR(1, 14); display_write_data_byte('T'); display_write_data_byte('0' + t + 1); } DISPLAY_SET_CURSOR(1, 4); if (res.lo_present & res.hi_present) { display_write_data_seq("OK"); return 0; } else if (res.lo_present) { display_write_data_seq("ALWAYS LO"); } else if (res.hi_present) { display_write_data_seq("ALWAYS HI"); } } return 1; }