soft-serial/src/lib.rs

283 lines
5.7 KiB
Rust
Raw Normal View History

2025-04-22 21:44:19 +03:00
#![no_std]
2025-04-21 20:36:22 +03:00
use core::marker::PhantomData;
use arduino_hal::{
delay_us,
hal::port::{PB4, PB5, PE6},
2025-04-21 20:36:22 +03:00
port::{
mode::{Input, PullUp},
Pin, PinOps,
},
};
use avr_device::asm::delay_cycles;
use static_pins::StaticPinOps;
use ufmt::derive::uDebug;
2025-04-21 20:36:22 +03:00
mod structures;
2025-04-21 20:36:22 +03:00
pub type PollResult = Result<(), PollError>;
pub type ReadByteResult = Result<u8, CorruptedData>;
pub type CorruptedData = (u8, u8);
const SERIAL_DELAY: u32 = 4;
const FIRST_HALF_SERIAL_DELAY: u32 = SERIAL_DELAY / 2;
const SECOND_HALF_SERIAL_DELAY: u32 = SERIAL_DELAY - FIRST_HALF_SERIAL_DELAY;
const READING_ADJUST: u32 = 10;
const TX_DELAY_CYCLES: u32 = 22;
const MSB: u16 = 0x0800;
const PACKET_MASK: u16 = 0x0FFF;
2025-08-10 19:57:28 +03:00
#[inline(always)]
fn crc_calculate(value: u8) -> u8 {
((value >> 6) | (value >> 4) | (value >> 2) | value) & 0x0F
}
2025-04-21 20:36:22 +03:00
#[cfg(feature = "ufmt")]
#[derive(uDebug)]
2025-04-21 20:36:22 +03:00
pub enum PollError {
NotFound,
NotReady,
}
pub struct HalfDuplexSerial<P> {
_pin: PhantomData<Pin<Input<PullUp>, P>>,
}
#[cfg(debug_assertions)]
#[inline(always)]
fn debug_pb4_low() {
PB4::set_low();
}
#[cfg(debug_assertions)]
#[inline(always)]
fn debug_pb4_high() {
PB4::set_high();
}
#[allow(dead_code)]
#[cfg(debug_assertions)]
#[inline(always)]
fn debug_pb4_pulse_positive() {
debug_pb4_low();
debug_pb4_high();
}
#[allow(dead_code)]
#[cfg(debug_assertions)]
#[inline(always)]
fn debug_pb4_pulse_negative() {
debug_pb4_high();
debug_pb4_low();
}
2025-04-21 20:36:22 +03:00
impl<P> HalfDuplexSerial<P>
where
P: PinOps + StaticPinOps,
{
pub fn new(_pin: Pin<Input<PullUp>, P>) -> Self {
if cfg!(debug_assertions) {
PB4::into_output_high();
PB5::into_output_high();
PE6::into_output_high();
}
2025-04-21 20:36:22 +03:00
Self {
_pin: PhantomData {},
}
}
pub fn poll(&self) -> PollResult {
PE6::set_low();
2025-04-21 20:36:22 +03:00
P::into_output();
delay_cycles(2);
2025-04-21 20:36:22 +03:00
P::into_pull_up_input();
delay_us(SERIAL_DELAY + FIRST_HALF_SERIAL_DELAY);
2025-04-21 20:36:22 +03:00
if P::is_low() {
PE6::set_high();
2025-04-21 20:36:22 +03:00
return PollResult::Err(PollError::NotFound);
}
delay_us(SERIAL_DELAY);
if P::is_high() {
PE6::set_high();
2025-04-21 20:36:22 +03:00
return PollResult::Err(PollError::NotReady);
}
while P::is_low() {}
PE6::set_high();
2025-04-21 20:36:22 +03:00
PollResult::Ok(())
}
pub fn response(&self) {
PE6::set_low();
2025-04-21 20:36:22 +03:00
P::into_output_high();
delay_us(FIRST_HALF_SERIAL_DELAY);
P::set_low();
delay_us(SERIAL_DELAY);
P::set_high();
2025-04-21 20:36:22 +03:00
P::into_pull_up_input();
PE6::set_high();
2025-04-21 20:36:22 +03:00
}
#[inline]
pub fn sync_transmitter(&self) {
if cfg!(debug_assertions) {
PB5::set_low();
}
2025-04-21 20:36:22 +03:00
P::into_output();
2025-04-30 22:03:42 +03:00
delay_us(SERIAL_DELAY);
2025-04-21 20:36:22 +03:00
P::set_high();
if cfg!(debug_assertions) {
PB5::set_high();
}
2025-04-21 20:36:22 +03:00
}
#[inline]
pub fn sync_reciever(&self) {
if cfg!(debug_assertions) {
PB5::set_low();
}
2025-04-21 20:36:22 +03:00
while P::is_high() {}
while P::is_low() {}
if cfg!(debug_assertions) {
PB5::set_high();
}
2025-04-21 20:36:22 +03:00
}
#[inline]
pub fn reset(&self) {
P::into_pull_up_input();
}
}
2025-06-01 17:20:24 +03:00
pub trait SoftSerialWriter<P, T>: SoftSerialByteWriter<P>
where
P: PinOps + StaticPinOps,
{
fn write_bytes(&self, transmit_data: T);
}
2025-06-01 17:20:24 +03:00
pub trait SoftSerialReader<P, T>: SoftSerialByteReader<P>
where
P: PinOps + StaticPinOps,
{
fn read_bytes(&self, recieve_data: T) -> Result<(), CorruptedData>;
}
2025-06-01 17:20:24 +03:00
pub trait SoftSerialByteWriter<P>
where
P: PinOps + StaticPinOps,
{
#[inline(never)]
fn write_byte(&self, transmit_data: u8) {
2025-08-10 19:57:28 +03:00
let mut data = ((transmit_data as u16) << 4) | (crc_calculate(transmit_data) as u16);
2025-06-01 17:20:24 +03:00
2025-08-10 19:57:28 +03:00
for _ in 0..(u8::BITS + 4) {
debug_pb4_low();
2025-06-01 17:20:24 +03:00
if data & MSB == 0 {
P::set_high();
} else {
P::set_low();
}
data = (data << 1) & PACKET_MASK;
2025-06-01 17:20:24 +03:00
delay_us(SERIAL_DELAY);
2025-04-21 20:36:22 +03:00
debug_pb4_high();
2025-06-01 17:20:24 +03:00
}
P::set_high();
delay_cycles(TX_DELAY_CYCLES);
}
2025-06-01 17:20:24 +03:00
}
2025-06-01 17:20:24 +03:00
pub trait SoftSerialByteReader<P>
where
P: PinOps + StaticPinOps,
{
#[inline(never)]
fn read_byte(&self) -> ReadByteResult {
2025-08-10 19:57:28 +03:00
let mut packet = 0u16;
2025-04-21 20:36:22 +03:00
2025-08-10 19:57:28 +03:00
for _ in 0..(u8::BITS + 4) {
debug_pb4_low();
delay_us(FIRST_HALF_SERIAL_DELAY);
delay_cycles(READING_ADJUST);
2025-06-01 17:20:24 +03:00
if P::is_low() {
packet = (packet << 1) | 1;
2025-06-01 17:20:24 +03:00
} else {
packet <<= 1;
2025-06-01 17:20:24 +03:00
}
2025-06-01 17:20:24 +03:00
delay_us(SECOND_HALF_SERIAL_DELAY);
debug_pb4_high();
2025-04-21 20:36:22 +03:00
}
let data = (packet >> 4) as u8;
2025-08-10 19:57:28 +03:00
let received_crc = (packet & 0x000F) as u8;
let calculated_crc = crc_calculate(data);
2025-08-10 19:57:28 +03:00
if received_crc != calculated_crc {
return Err((received_crc, calculated_crc));
2025-06-01 17:20:24 +03:00
}
2025-06-01 17:20:24 +03:00
Ok(data)
}
}
2025-06-01 17:20:24 +03:00
impl<P: PinOps + StaticPinOps> SoftSerialByteWriter<P> for HalfDuplexSerial<P> {}
impl<P: PinOps + StaticPinOps> SoftSerialByteReader<P> for HalfDuplexSerial<P> {}
impl<P> SoftSerialWriter<P, &[u8]> for HalfDuplexSerial<P>
where
P: PinOps + StaticPinOps,
{
fn write_bytes(&self, transmit_data: &[u8]) {
for byte in transmit_data {
2025-06-01 17:20:24 +03:00
self.write_byte(*byte);
self.sync_transmitter();
}
}
}
impl<P> SoftSerialReader<P, &mut [u8]> for HalfDuplexSerial<P>
where
P: PinOps + StaticPinOps,
{
fn read_bytes(&self, recieve_data: &mut [u8]) -> Result<(), CorruptedData> {
for byte in recieve_data {
*byte = self.read_byte()?;
self.sync_reciever();
}
Ok(())
}
}