Skip to content

Commit

Permalink
add hw id read
Browse files Browse the repository at this point in the history
  • Loading branch information
xythobuz committed Apr 16, 2024
1 parent bbd5b07 commit 717f8b1
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 15 deletions.
1 change: 1 addition & 0 deletions firmware/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ target_sources(dispensy PUBLIC
${CMAKE_CURRENT_LIST_DIR}/src/buttons.c
${CMAKE_CURRENT_LIST_DIR}/src/lcd.c
${CMAKE_CURRENT_LIST_DIR}/src/ring.c
${CMAKE_CURRENT_LIST_DIR}/src/hw_id.c

${CMAKE_CURRENT_LIST_DIR}/pico-ssd1306/ssd1306.c
)
Expand Down
34 changes: 34 additions & 0 deletions firmware/include/hw_id.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* hw_id.h
*
* Copyright (c) 2022 - 2024 Thomas Buck ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* See <http://www.gnu.org/licenses/>.
*/

#ifndef __HW_ID_H__
#define __HW_ID_H__

#include "pico/stdlib.h"

enum hw_type {
HW_TYPE_MAINBOARD = 0,
};

void hw_id_init(void);
void hw_id_read(void);

enum hw_type hw_type(void);
uint hw_id(void);

#endif // __HW_ID_H__
2 changes: 2 additions & 0 deletions firmware/include/lcd.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#define FONT_WIDTH 5

void lcd_init(void);

void lcd_splash_version(void);
void lcd_bye(void);

#endif // __LCD_H__
11 changes: 9 additions & 2 deletions firmware/src/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
#include "log.h"
#include "util.h"
#include "usb_cdc.h"
#include "lcd.h"
#include "main.h"
#include "hw_id.h"
#include "console.h"

#define CNSL_BUFF_SIZE 64
Expand Down Expand Up @@ -81,12 +80,20 @@ static void cnsl_interpret(const char *line) {
println("");
println(" reset - reset back into this firmware");
println(" \\x18 - reset to bootloader");
println(" type - print hardware type");
println(" id - print hardware ID");
println("");
println("Press Enter with no input to repeat last command.");
println("Use repeat to continuously execute last command.");
println("Stop this by calling repeat again.");
} else if (strcmp(line, "reset") == 0) {
reset_to_main();
} else if (strcmp(line, "type") == 0) {
hw_id_read();
debug("HW Type: %d", hw_type());
} else if (strcmp(line, "id") == 0) {
hw_id_read();
debug("HW ID: %d", hw_id());
} else {
println("unknown command \"%s\"", line);
}
Expand Down
82 changes: 82 additions & 0 deletions firmware/src/hw_id.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* hw_id.c
*
* Copyright (c) 2022 - 2024 Thomas Buck ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* See <http://www.gnu.org/licenses/>.
*/

#include "hardware/watchdog.h"
#include "config.h"
#include "log.h"
#include "hw_id.h"

#define SR_WIDTH 8

#define SR_GPIO_LOAD 20
#define SR_GPIO_CLK 21
#define SR_GPIO_DATA 22

static bool states[SR_WIDTH] = {0};

void hw_id_init(void) {
gpio_init(SR_GPIO_LOAD);
gpio_set_dir(SR_GPIO_LOAD, GPIO_OUT);
gpio_put(SR_GPIO_LOAD, true);

gpio_init(SR_GPIO_CLK);
gpio_set_dir(SR_GPIO_CLK, GPIO_OUT);
gpio_put(SR_GPIO_CLK, true);

gpio_init(SR_GPIO_DATA);
gpio_set_dir(SR_GPIO_DATA, GPIO_IN);
}

void hw_id_read(void) {
gpio_put(SR_GPIO_LOAD, false);
watchdog_update();
sleep_us(5);
gpio_put(SR_GPIO_LOAD, true);
watchdog_update();
sleep_us(5);

for (uint i = 0; i < SR_WIDTH; i++) {
gpio_put(SR_GPIO_CLK, false);
watchdog_update();
sleep_us(5);

states[i] = gpio_get(SR_GPIO_DATA);

gpio_put(SR_GPIO_CLK, true);
watchdog_update();
sleep_us(5);

//debug("bit %d is %s", i, states[i] ? "true" : "false");
}
}

enum hw_type hw_type(void) {
uint val = 0;
for (uint i = 4; i < 8; i++) {
val |= states[i] ? (1 << (i - 4)) : 0;
}
return val;
}

uint hw_id(void) {
uint val = 0;
for (uint i = 0; i < 4; i++) {
val |= (!states[i]) ? (1 << (4 - i - 1)) : 0;
}
return val;
}
25 changes: 19 additions & 6 deletions firmware/src/lcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
* See <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <string.h>

#include "hardware/i2c.h"
#include "ssd1306.h"
#include "git.h"

#include "config.h"
#include "hw_id.h"
#include "log.h"
#include "lcd.h"

Expand Down Expand Up @@ -69,20 +71,31 @@ void lcd_splash_version(void) {

if (git_IsPopulated()) {
const char *hash = git_CommitSHA1();
char short_hash[6 + 7 + 1] = {0};
char short_hash[6 + 7 + 6 + 1] = {0};
memcpy(short_hash, "Hash: ", 6);
memcpy(short_hash + 6, hash, 7);
ssd1306_draw_string(&lcd, 0, FONT_HEIGHT * 2 + 1 + (FONT_HEIGHT + 1) * 2, 1,
short_hash);

if (git_AnyUncommittedChanges()) {
ssd1306_draw_string(&lcd, 0, FONT_HEIGHT * 2 + 1 + (FONT_HEIGHT + 1) * 3, 1,
"Repo has changes!");
memcpy(short_hash + 6 + 7, " dirty", 6);
}
ssd1306_draw_string(&lcd, 0, FONT_HEIGHT * 2 + 1 + (FONT_HEIGHT + 1) * 2, 1,
short_hash);
} else {
ssd1306_draw_string(&lcd, 0, FONT_HEIGHT * 2 + 1 + (FONT_HEIGHT + 1) * 2, 1,
"No Git Repo");
}

char hw_id_str[42] = {0};
snprintf(hw_id_str, sizeof(hw_id_str) - 1,
"HW type=%X id=%X", hw_type(), hw_id());
ssd1306_draw_string(&lcd, 0, FONT_HEIGHT * 2 + 1 + (FONT_HEIGHT + 1) * 3, 1,
hw_id_str);

ssd1306_show(&lcd);
}

void lcd_bye(void) {
ssd1306_clear(&lcd);
ssd1306_draw_string(&lcd, 6, 5, 3, " Boot-");
ssd1306_draw_string(&lcd, 8, LCD_HEIGHT / 2 + 5, 3, "loader");
ssd1306_show(&lcd);
}
11 changes: 8 additions & 3 deletions firmware/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "usb.h"
#include "buttons.h"
#include "lcd.h"
#include "hw_id.h"
#include "main.h"

void main_loop_hw(void) {
Expand All @@ -35,7 +36,8 @@ int main(void) {
watchdog_enable(WATCHDOG_PERIOD_MS, 1);

// detect hardware type
// TODO
hw_id_init();
hw_id_read();

// required for debug console
cnsl_init();
Expand All @@ -46,15 +48,18 @@ int main(void) {
debug("reset by watchdog");
}

buttons_init();
debug("HW Type: %d", hw_type());
debug("HW ID: %d", hw_id());

//buttons_init();
lcd_init();
lcd_splash_version();

debug("go");

while (1) {
main_loop_hw();
buttons_run();
//buttons_run();
cnsl_run();
}

Expand Down
6 changes: 2 additions & 4 deletions firmware/src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "hardware/watchdog.h"

#include "config.h"
#include "lcd.h"
#include "log.h"
#include "util.h"

Expand All @@ -33,11 +34,8 @@ bool str_startswith(const char *str, const char *start) {
}

void reset_to_bootloader(void) {
#ifdef PICO_DEFAULT_LED_PIN
reset_usb_boot(1 << PICO_DEFAULT_LED_PIN, 0);
#else // ! PICO_DEFAULT_LED_PIN
lcd_bye();
reset_usb_boot(0, 0);
#endif // PICO_DEFAULT_LED_PIN
}

void reset_to_main(void) {
Expand Down

0 comments on commit 717f8b1

Please sign in to comment.