Skip to content

Commit 3597f49

Browse files
committed
feat: add LED and flash it on error
1 parent 2e2eddf commit 3597f49

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

src/main.cpp

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#define PIN_WRLOCK_IN (15)
3232
#define PIN_SDA (16)
3333
#define PIN_SCL (17)
34+
#define PIN_LED PICO_DEFAULT_LED_PIN
3435

3536
#define I2C_INST i2c0
3637

@@ -44,21 +45,31 @@ static char board_id[PICO_UNIQUE_BOARD_ID_SIZE_BYTES * 2 + 1];
4445
static DeviceInfoBlock data;
4546

4647
// EEPROM peripheral.
47-
static AT24CM02 eeprom{I2C_INST, false};
48+
static AT24CM02 eeprom{I2C_INST, true};
4849

4950
static volatile uint32_t edgecount;
5051
static uint32_t last_edgecount;
5152
static volatile ::absolute_time_t last_edge_time;
5253

54+
[[noreturn]] static void Panic() noexcept {
55+
while (1) {
56+
::gpio_xor_mask(1U << PIN_LED);
57+
::sleep_ms(250);
58+
}
59+
}
60+
5361
static void StoreDeviceInfo() noexcept {
54-
// TODO: error handling
55-
eeprom.Write(kDeviceInfoAddr, reinterpret_cast<const uint8_t*>(&data),
56-
sizeof(data));
62+
if (!eeprom.Write(kDeviceInfoAddr, reinterpret_cast<const uint8_t*>(&data),
63+
sizeof(data))) {
64+
Panic();
65+
}
5766
}
5867

5968
static void LoadDeviceInfo() noexcept {
60-
// TODO: error handling
61-
eeprom.Read(kDeviceInfoAddr, reinterpret_cast<uint8_t*>(&data), sizeof(data));
69+
if (!eeprom.Read(kDeviceInfoAddr, reinterpret_cast<uint8_t*>(&data),
70+
sizeof(data))) {
71+
Panic();
72+
}
6273
}
6374

6475
// Validates data. If it was invalid, updates the data and writes it back to the
@@ -71,14 +82,18 @@ static void ValidateDeviceInfo() noexcept {
7182
}
7283

7384
static void StoreEdgeCount(uint32_t ec) noexcept {
74-
// TODO: error handling
75-
eeprom.Write(kEdgeCountAddr, reinterpret_cast<const uint8_t*>(&ec),
76-
sizeof(ec));
85+
if (!eeprom.Write(kEdgeCountAddr, reinterpret_cast<const uint8_t*>(&ec),
86+
sizeof(ec))) {
87+
Panic();
88+
}
7789
}
7890

7991
static void LoadEdgeCount() noexcept {
8092
uint32_t ec;
81-
eeprom.Read(kEdgeCountAddr, reinterpret_cast<uint8_t*>(&ec), sizeof(ec));
93+
if (!eeprom.Read(kEdgeCountAddr, reinterpret_cast<uint8_t*>(&ec),
94+
sizeof(ec))) {
95+
Panic();
96+
}
8297
edgecount = ec;
8398
}
8499

@@ -153,6 +168,10 @@ static void HandleSerialMessage(std::string_view message) noexcept {
153168
int main(void) {
154169
::stdio_init_all();
155170

171+
::gpio_init(PIN_LED);
172+
::gpio_set_dir(PIN_LED, GPIO_OUT);
173+
::gpio_put(PIN_LED, 1);
174+
156175
::gpio_init(PIN_WRLOCK_OUT);
157176
::gpio_set_dir(PIN_WRLOCK_OUT, GPIO_OUT);
158177
::gpio_put(PIN_WRLOCK_OUT, 1);

0 commit comments

Comments
 (0)