]> mj.ucw.cz Git - home-hw.git/commitdiff
test-display: IR test
authorMartin Mares <mj@ucw.cz>
Sun, 6 Feb 2022 19:37:15 +0000 (20:37 +0100)
committerMartin Mares <mj@ucw.cz>
Sun, 6 Feb 2022 19:37:15 +0000 (20:37 +0100)
test-display/config.h
test-display/main.c

index 312c7beeca7f3bfd15953990be38c0d02ffe7fbf..fe39e28caa5bf33ab643ffbcdd5e1ce978ff10e1 100644 (file)
@@ -12,3 +12,7 @@
 
 #define DEBUG_USART USART1
 #define DEBUG_LED_BLUEPILL
+
+// Testing of IR receiver
+
+#undef IR_TEST
index 8fd56363b7125ca5b9f14518bc73ccd65b842b86..a00c20a985b3537053a6e6a2061afc7481ffb7ed 100644 (file)
@@ -55,6 +55,9 @@ static void gpio_init(void)
        // PB7 = SDA for display controller
        // PB6 = SCL for display controller
        gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO6 | GPIO7);
+
+       // PB8 = SFH5110 output (5V tolerant)
+       gpio_set_mode(GPIOC, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO8);
 }
 
 static void usart_init(void)
@@ -118,7 +121,7 @@ static byte ctrl = 0x56;
 
 static void display_update(void)
 {
-       debug_puts("Display pdate\n");
+       debug_puts("Display update\n");
        byte cmds[4];
        cmds[0] = 0;
        cmds[1] = ctrl;
@@ -390,14 +393,101 @@ static void usb_init(void)
        usb_event_pending = 1;
 }
 
+/*** Testing of IR receiver ***/
+
+#ifdef IR_TEST
+
+static u16 get_bit(void)
+{
+#if 1
+       return !gpio_get(GPIOB, GPIO8);
+#else
+       int x = 0;
+       x += !gpio_get(GPIOB, GPIO8);
+       x += !gpio_get(GPIOB, GPIO8);
+       x += !gpio_get(GPIOB, GPIO8);
+       x += !gpio_get(GPIOB, GPIO8);
+       return x >= 2;
+#endif
+}
+
+#define MAX_SAMPLES 1024
+u32 samples[MAX_SAMPLES];
+
+static void ir_test_loop(void)
+{
+       debug_puts("\n\n### Infrared Remote Control receiver ###\n\n");
+
+       systick_set_reload(0xffffff);
+       systick_counter_enable();
+       systick_interrupt_disable();
+       systick_clear();
+
+       for (;;) {
+               gpio_set(GPIOC, GPIO13);
+
+               if (get_bit()) {
+                       debug_puts("Waiting for silence\n");
+                       while (get_bit())
+                               ;
+               }
+               debug_puts("Ready...");
+
+               u16 last = 0;
+               uint nsamp = 0;
+               u32 start;
+
+               do {
+                       start = systick_get_value();
+                       last = get_bit();
+               } while (!last);
+
+               gpio_clear(GPIOC, GPIO13);
+
+               for (;;) {
+                       u32 now;
+                       u16 curr;
+                       uint len;
+                       do {
+                               now = systick_get_value();
+                               len = (start - now) & 0xffffff;
+                               if (len > 5000000) {
+                                       samples[nsamp++] = len;
+                                       goto timeout;
+                               }
+                               curr = get_bit();
+                       } while (curr == last);
+                       samples[nsamp++] = len;
+                       if (nsamp >= MAX_SAMPLES)
+                               break;
+                       start = now;
+                       last = curr;
+               }
+
+       timeout:
+               for (uint i=0; i<nsamp; i++) {
+                       debug_putc(i ? ' ' : '\n');
+                       debug_printf("%u", (unsigned int)((samples[i] + CPU_CLOCK_MHZ - 1) / CPU_CLOCK_MHZ));   // in μs
+               }
+               debug_putc('\n');
+       }
+}
+
+#endif
+
 /*** Main ***/
 
 int main(void)
 {
        clock_init();
        gpio_init();
-       tick_init();
        usart_init();
+
+#ifdef IR_TEST
+       ir_test_loop();
+#endif
+
+       tick_init();
        desig_get_unique_id_as_dfu(usb_serial_number);
 
        debug_printf("Hello, world!\n");