From 8f74049209abd800b365a31d1d90dd63ea873de1 Mon Sep 17 00:00:00 2001
From: Samuel Tardieu <sam@rfc1149.net>
Date: Wed, 17 Jul 2024 01:05:18 +0200
Subject: [PATCH] Add RESET command

---
 README.org                      | 4 ++++
 controller/python/controller.py | 5 +++++
 controller/src/logic/mod.rs     | 5 +++++
 3 files changed, 14 insertions(+)

diff --git a/README.org b/README.org
index dee21c9..d42e2a7 100644
--- a/README.org
+++ b/README.org
@@ -125,6 +125,10 @@ Multibyte values are exchanged in little endian format.
   duration in 10th of seconds on 1 byte. This cannot be used if the
   motor is moving.
 
+** [IMPLEMENTED] 0xE0 Reset (W)
+
+- Reset the device. Used mainly for testing.
+
 ** 0xF0-0xF7 Unique device ID (R)
 
 - Unique device ID as found in addresses 0x1FFFF7E8-0x1FFFF7EF
diff --git a/controller/python/controller.py b/controller/python/controller.py
index 29c9b8f..abe1040 100644
--- a/controller/python/controller.py
+++ b/controller/python/controller.py
@@ -22,6 +22,7 @@ class Controller:
     MOTOR_SPEED = 0x30
     ENCODER_TICKS = 0x32
     STATUS = 0x36
+    RESET = 0xE0
 
     def __init__(self, i2cbus=8):
         import smbus
@@ -161,3 +162,7 @@ class Controller:
         """Return the PWM frequency in Hz."""
         a, b, c = self._read(self.PWM_FREQUENCY, 3, "BBB")
         return a | (b << 8) | (c << 16)
+
+    def reset(self):
+        """Reset the device. Used mainly for testing."""
+        self._write(self.RESET, [])
diff --git a/controller/src/logic/mod.rs b/controller/src/logic/mod.rs
index 3ed0269..5de199d 100644
--- a/controller/src/logic/mod.rs
+++ b/controller/src/logic/mod.rs
@@ -60,6 +60,7 @@ const CMD_MOTOR_SHUTDOWN_TIMEOUT: u8 = 0x28;
 const CMD_MOTOR_SPEED: u8 = 0x30;
 const CMD_ENCODER_TICKS: u8 = 0x32;
 const CMD_STATUS: u8 = 0x36;
+const CMD_RESET: u8 = 0xe0;
 
 include!(concat!(env!("OUT_DIR"), "/version.rs"));
 
@@ -153,6 +154,10 @@ fn process_command(
                 .push_back(u8::from(motor_control::is_moving()))
                 .unwrap();
         }
+        [CMD_RESET] => {
+            defmt::info!("resetting device after receiving the RESET command");
+            cortex_m::peripheral::SCB::sys_reset();
+        }
         _ => {
             defmt::warn!("unknown command or args {:#04x}", command);
             return false;
-- 
GitLab