Skip to content
Snippets Groups Projects
Commit e63eed4c authored by Samuel Tardieu's avatar Samuel Tardieu
Browse files

Ignore malformed messages

parent f4579f24
No related branches found
No related tags found
No related merge requests found
...@@ -57,7 +57,7 @@ fn handle_command(command: &[u8], response: &mut Deque<u8, MESSAGE_SIZE>) { ...@@ -57,7 +57,7 @@ fn handle_command(command: &[u8], response: &mut Deque<u8, MESSAGE_SIZE>) {
[WHO_AM_I, ..] => { [WHO_AM_I, ..] => {
let _ = response.push_back(0x57); let _ = response.push_back(0x57);
} }
&[MAX_MOTOR_PERCENTAGE, p, ..] => { &[MAX_MOTOR_PERCENTAGE, p] => {
if (1..=100).contains(&p) { if (1..=100).contains(&p) {
state.max_motor_percentage = p; state.max_motor_percentage = p;
} else { } else {
...@@ -67,7 +67,7 @@ fn handle_command(command: &[u8], response: &mut Deque<u8, MESSAGE_SIZE>) { ...@@ -67,7 +67,7 @@ fn handle_command(command: &[u8], response: &mut Deque<u8, MESSAGE_SIZE>) {
[MAX_MOTOR_PERCENTAGE] => { [MAX_MOTOR_PERCENTAGE] => {
let _ = response.push_back(state.max_motor_percentage); let _ = response.push_back(state.max_motor_percentage);
} }
[MOTOR_SPEED, ms @ ..] if ms.len() >= 2 => { [MOTOR_SPEED, ms @ ..] if ms.len() == 2 => {
let (max_pwm, mmp) = ( let (max_pwm, mmp) = (
state.motors.max_pwm() as i32, state.motors.max_pwm() as i32,
i32::from(state.max_motor_percentage), i32::from(state.max_motor_percentage),
...@@ -93,12 +93,12 @@ fn handle_command(command: &[u8], response: &mut Deque<u8, MESSAGE_SIZE>) { ...@@ -93,12 +93,12 @@ fn handle_command(command: &[u8], response: &mut Deque<u8, MESSAGE_SIZE>) {
} }
} }
} }
[MOTOR_SPEED, ..] => { [MOTOR_SPEED] => {
for &s in &state.motor_speed { for &s in &state.motor_speed {
let _ = response.push_back(s); let _ = response.push_back(s);
} }
} }
[ENCODER_TICKS, ..] => { [ENCODER_TICKS] => {
let (left, right) = state.encoders.ticks(); let (left, right) = state.encoders.ticks();
let (left, right) = (left.to_le_bytes(), right.to_le_bytes()); let (left, right) = (left.to_le_bytes(), right.to_le_bytes());
let _ = response.push_back(left[0]); let _ = response.push_back(left[0]);
...@@ -106,8 +106,8 @@ fn handle_command(command: &[u8], response: &mut Deque<u8, MESSAGE_SIZE>) { ...@@ -106,8 +106,8 @@ fn handle_command(command: &[u8], response: &mut Deque<u8, MESSAGE_SIZE>) {
let _ = response.push_back(right[0]); let _ = response.push_back(right[0]);
let _ = response.push_back(right[1]); let _ = response.push_back(right[1]);
} }
&[idx, ..] => { &[_, ..] => {
defmt::warn!("unknown register {:#04x}", idx); defmt::warn!("unknown command or args {:#04x}", command);
} }
&[] => { &[] => {
defmt::warn!("received empty command"); defmt::warn!("received empty command");
......
...@@ -110,9 +110,15 @@ impl I2C2 { ...@@ -110,9 +110,15 @@ impl I2C2 {
let af_num = SclPin::af_num(&pb10); let af_num = SclPin::af_num(&pb10);
assert_eq!(af_num, SdaPin::af_num(&pb11)); assert_eq!(af_num, SdaPin::af_num(&pb11));
let mut pb10 = Flex::new(pb10); let mut pb10 = Flex::new(pb10);
pb10.set_as_af_unchecked(af_num, AfType::output(OutputType::OpenDrain, Speed::Low)); pb10.set_as_af_unchecked(
af_num,
AfType::output(OutputType::OpenDrain, Speed::VeryHigh),
);
let mut pb11 = Flex::new(pb11); let mut pb11 = Flex::new(pb11);
pb11.set_as_af_unchecked(af_num, AfType::output(OutputType::OpenDrain, Speed::Low)); pb11.set_as_af_unchecked(
af_num,
AfType::output(OutputType::OpenDrain, Speed::VeryHigh),
);
// Turn I2C2 clock on // Turn I2C2 clock on
pac::RCC.apb1enr().modify(|w| w.set_i2c2en(true)); pac::RCC.apb1enr().modify(|w| w.set_i2c2en(true));
...@@ -131,6 +137,7 @@ impl I2C2 { ...@@ -131,6 +137,7 @@ impl I2C2 {
// Enable I2C2 // Enable I2C2
pac::I2C2.cr1().write(|w| { pac::I2C2.cr1().write(|w| {
w.set_pe(true); w.set_pe(true);
w.set_nostretch(false);
}); });
// Set ack generation // Set ack generation
pac::I2C2.cr1().modify(|w| { pac::I2C2.cr1().modify(|w| {
...@@ -162,11 +169,19 @@ unsafe fn I2C2_EV() { ...@@ -162,11 +169,19 @@ unsafe fn I2C2_EV() {
pac::I2C2.cr1().read().0, pac::I2C2.cr1().read().0,
pac::I2C2.cr2().read().0 pac::I2C2.cr2().read().0
); );
if sr1.ovr() {
defmt::error!("overrun detected");
pac::I2C2.sr1().modify(|w| w.set_ovr(false));
}
let state = unsafe { STATE.as_mut().unwrap() }; let state = unsafe { STATE.as_mut().unwrap() };
if sr1.rxne() { if sr1.rxne() {
defmt::trace!("I2C2_EV RXNE"); defmt::trace!("I2C2_EV RXNE");
let b = pac::I2C2.dr().read().0 as u8; let b = pac::I2C2.dr().read().0 as u8;
defmt::trace!("received byte {:#04x}", &b); defmt::trace!(
"received byte {:#04x} - sr1 is now {:#06x}",
&b,
pac::I2C2.sr1().read().0
);
if state.incoming.push(b).is_err() { if state.incoming.push(b).is_err() {
defmt::error!("I2C byte has overfilled the buffer"); defmt::error!("I2C byte has overfilled the buffer");
} }
...@@ -215,5 +230,8 @@ unsafe fn I2C2_EV() { ...@@ -215,5 +230,8 @@ unsafe fn I2C2_EV() {
} else { } else {
state.set_write_in_progress(); state.set_write_in_progress();
} }
// Read DR to avoid overrun
pac::I2C2.dr().read();
defmt::trace!("sr1 is now {:#06x}", pac::I2C2.sr1().read().0);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment