Browse Source

add hw id read

Thomas Buck 1 week ago
parent
commit
717f8b1abc

+ 1
- 0
firmware/CMakeLists.txt View File

@@ -40,6 +40,7 @@ target_sources(dispensy PUBLIC
40 40
     ${CMAKE_CURRENT_LIST_DIR}/src/buttons.c
41 41
     ${CMAKE_CURRENT_LIST_DIR}/src/lcd.c
42 42
     ${CMAKE_CURRENT_LIST_DIR}/src/ring.c
43
+    ${CMAKE_CURRENT_LIST_DIR}/src/hw_id.c
43 44
 
44 45
     ${CMAKE_CURRENT_LIST_DIR}/pico-ssd1306/ssd1306.c
45 46
 )

+ 34
- 0
firmware/include/hw_id.h View File

@@ -0,0 +1,34 @@
1
+/*
2
+ * hw_id.h
3
+ *
4
+ * Copyright (c) 2022 - 2024 Thomas Buck (thomas@xythobuz.de)
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * See <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+#ifndef __HW_ID_H__
20
+#define __HW_ID_H__
21
+
22
+#include "pico/stdlib.h"
23
+
24
+enum hw_type {
25
+    HW_TYPE_MAINBOARD = 0,
26
+};
27
+
28
+void hw_id_init(void);
29
+void hw_id_read(void);
30
+
31
+enum hw_type hw_type(void);
32
+uint hw_id(void);
33
+
34
+#endif // __HW_ID_H__

+ 2
- 0
firmware/include/lcd.h View File

@@ -26,6 +26,8 @@
26 26
 #define FONT_WIDTH 5
27 27
 
28 28
 void lcd_init(void);
29
+
29 30
 void lcd_splash_version(void);
31
+void lcd_bye(void);
30 32
 
31 33
 #endif // __LCD_H__

+ 9
- 2
firmware/src/console.c View File

@@ -25,8 +25,7 @@
25 25
 #include "log.h"
26 26
 #include "util.h"
27 27
 #include "usb_cdc.h"
28
-#include "lcd.h"
29
-#include "main.h"
28
+#include "hw_id.h"
30 29
 #include "console.h"
31 30
 
32 31
 #define CNSL_BUFF_SIZE 64
@@ -81,12 +80,20 @@ static void cnsl_interpret(const char *line) {
81 80
         println("");
82 81
         println("  reset - reset back into this firmware");
83 82
         println("   \\x18 - reset to bootloader");
83
+        println("   type - print hardware type");
84
+        println("     id - print hardware ID");
84 85
         println("");
85 86
         println("Press Enter with no input to repeat last command.");
86 87
         println("Use repeat to continuously execute last command.");
87 88
         println("Stop this by calling repeat again.");
88 89
     } else if (strcmp(line, "reset") == 0) {
89 90
         reset_to_main();
91
+    } else if (strcmp(line, "type") == 0) {
92
+        hw_id_read();
93
+        debug("HW Type: %d", hw_type());
94
+    } else if (strcmp(line, "id") == 0) {
95
+        hw_id_read();
96
+        debug("HW ID: %d", hw_id());
90 97
     } else {
91 98
         println("unknown command \"%s\"", line);
92 99
     }

+ 82
- 0
firmware/src/hw_id.c View File

@@ -0,0 +1,82 @@
1
+/*
2
+ * hw_id.c
3
+ *
4
+ * Copyright (c) 2022 - 2024 Thomas Buck (thomas@xythobuz.de)
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * See <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+#include "hardware/watchdog.h"
20
+#include "config.h"
21
+#include "log.h"
22
+#include "hw_id.h"
23
+
24
+#define SR_WIDTH 8
25
+
26
+#define SR_GPIO_LOAD 20
27
+#define SR_GPIO_CLK 21
28
+#define SR_GPIO_DATA 22
29
+
30
+static bool states[SR_WIDTH] = {0};
31
+
32
+void hw_id_init(void) {
33
+    gpio_init(SR_GPIO_LOAD);
34
+    gpio_set_dir(SR_GPIO_LOAD, GPIO_OUT);
35
+    gpio_put(SR_GPIO_LOAD, true);
36
+
37
+    gpio_init(SR_GPIO_CLK);
38
+    gpio_set_dir(SR_GPIO_CLK, GPIO_OUT);
39
+    gpio_put(SR_GPIO_CLK, true);
40
+
41
+    gpio_init(SR_GPIO_DATA);
42
+    gpio_set_dir(SR_GPIO_DATA, GPIO_IN);
43
+}
44
+
45
+void hw_id_read(void) {
46
+    gpio_put(SR_GPIO_LOAD, false);
47
+    watchdog_update();
48
+    sleep_us(5);
49
+    gpio_put(SR_GPIO_LOAD, true);
50
+    watchdog_update();
51
+    sleep_us(5);
52
+
53
+    for (uint i = 0; i < SR_WIDTH; i++) {
54
+        gpio_put(SR_GPIO_CLK, false);
55
+        watchdog_update();
56
+        sleep_us(5);
57
+
58
+        states[i] = gpio_get(SR_GPIO_DATA);
59
+
60
+        gpio_put(SR_GPIO_CLK, true);
61
+        watchdog_update();
62
+        sleep_us(5);
63
+
64
+        //debug("bit %d is %s", i, states[i] ? "true" : "false");
65
+    }
66
+}
67
+
68
+enum hw_type hw_type(void) {
69
+    uint val = 0;
70
+    for (uint i = 4; i < 8; i++) {
71
+        val |= states[i] ? (1 << (i - 4)) : 0;
72
+    }
73
+    return val;
74
+}
75
+
76
+uint hw_id(void) {
77
+    uint val = 0;
78
+    for (uint i = 0; i < 4; i++) {
79
+        val |= (!states[i]) ? (1 << (4 - i - 1)) : 0;
80
+    }
81
+    return val;
82
+}

+ 19
- 6
firmware/src/lcd.c View File

@@ -16,6 +16,7 @@
16 16
  * See <http://www.gnu.org/licenses/>.
17 17
  */
18 18
 
19
+#include <stdio.h>
19 20
 #include <string.h>
20 21
 
21 22
 #include "hardware/i2c.h"
@@ -23,6 +24,7 @@
23 24
 #include "git.h"
24 25
 
25 26
 #include "config.h"
27
+#include "hw_id.h"
26 28
 #include "log.h"
27 29
 #include "lcd.h"
28 30
 
@@ -69,20 +71,31 @@ void lcd_splash_version(void) {
69 71
 
70 72
     if (git_IsPopulated()) {
71 73
         const char *hash = git_CommitSHA1();
72
-        char short_hash[6 + 7 + 1] = {0};
74
+        char short_hash[6 + 7 + 6 + 1] = {0};
73 75
         memcpy(short_hash, "Hash: ", 6);
74 76
         memcpy(short_hash + 6, hash, 7);
75
-        ssd1306_draw_string(&lcd, 0, FONT_HEIGHT * 2 + 1 + (FONT_HEIGHT + 1) * 2, 1,
76
-                            short_hash);
77
-
78 77
         if (git_AnyUncommittedChanges()) {
79
-            ssd1306_draw_string(&lcd, 0, FONT_HEIGHT * 2 + 1 + (FONT_HEIGHT + 1) * 3, 1,
80
-                                "Repo has changes!");
78
+            memcpy(short_hash + 6 + 7, " dirty", 6);
81 79
         }
80
+        ssd1306_draw_string(&lcd, 0, FONT_HEIGHT * 2 + 1 + (FONT_HEIGHT + 1) * 2, 1,
81
+                            short_hash);
82 82
     } else {
83 83
         ssd1306_draw_string(&lcd, 0, FONT_HEIGHT * 2 + 1 + (FONT_HEIGHT + 1) * 2, 1,
84 84
                             "No Git Repo");
85 85
     }
86 86
 
87
+    char hw_id_str[42] = {0};
88
+    snprintf(hw_id_str, sizeof(hw_id_str) - 1,
89
+             "HW type=%X id=%X", hw_type(), hw_id());
90
+    ssd1306_draw_string(&lcd, 0, FONT_HEIGHT * 2 + 1 + (FONT_HEIGHT + 1) * 3, 1,
91
+                        hw_id_str);
92
+
93
+    ssd1306_show(&lcd);
94
+}
95
+
96
+void lcd_bye(void) {
97
+    ssd1306_clear(&lcd);
98
+    ssd1306_draw_string(&lcd, 6, 5, 3, " Boot-");
99
+    ssd1306_draw_string(&lcd, 8, LCD_HEIGHT / 2 + 5, 3, "loader");
87 100
     ssd1306_show(&lcd);
88 101
 }

+ 8
- 3
firmware/src/main.c View File

@@ -24,6 +24,7 @@
24 24
 #include "usb.h"
25 25
 #include "buttons.h"
26 26
 #include "lcd.h"
27
+#include "hw_id.h"
27 28
 #include "main.h"
28 29
 
29 30
 void main_loop_hw(void) {
@@ -35,7 +36,8 @@ int main(void) {
35 36
     watchdog_enable(WATCHDOG_PERIOD_MS, 1);
36 37
 
37 38
     // detect hardware type
38
-    // TODO
39
+    hw_id_init();
40
+    hw_id_read();
39 41
 
40 42
     // required for debug console
41 43
     cnsl_init();
@@ -46,7 +48,10 @@ int main(void) {
46 48
         debug("reset by watchdog");
47 49
     }
48 50
 
49
-    buttons_init();
51
+    debug("HW Type: %d", hw_type());
52
+    debug("HW ID: %d", hw_id());
53
+
54
+    //buttons_init();
50 55
     lcd_init();
51 56
     lcd_splash_version();
52 57
 
@@ -54,7 +59,7 @@ int main(void) {
54 59
 
55 60
     while (1) {
56 61
         main_loop_hw();
57
-        buttons_run();
62
+        //buttons_run();
58 63
         cnsl_run();
59 64
     }
60 65
 

+ 2
- 4
firmware/src/util.c View File

@@ -21,6 +21,7 @@
21 21
 #include "hardware/watchdog.h"
22 22
 
23 23
 #include "config.h"
24
+#include "lcd.h"
24 25
 #include "log.h"
25 26
 #include "util.h"
26 27
 
@@ -33,11 +34,8 @@ bool str_startswith(const char *str, const char *start) {
33 34
 }
34 35
 
35 36
 void reset_to_bootloader(void) {
36
-#ifdef PICO_DEFAULT_LED_PIN
37
-    reset_usb_boot(1 << PICO_DEFAULT_LED_PIN, 0);
38
-#else // ! PICO_DEFAULT_LED_PIN
37
+    lcd_bye();
39 38
     reset_usb_boot(0, 0);
40
-#endif // PICO_DEFAULT_LED_PIN
41 39
 }
42 40
 
43 41
 void reset_to_main(void) {

Loading…
Cancel
Save