feat(buf): implement SoftSerial traits for HalfDuplexSerial with RingBuffer
This commit is contained in:
parent
7cbbe955bb
commit
b72be2fc3b
154
src/lib.rs
154
src/lib.rs
|
@ -14,6 +14,8 @@ use avr_device::asm::delay_cycles;
|
||||||
|
|
||||||
use static_pins::StaticPinOps;
|
use static_pins::StaticPinOps;
|
||||||
|
|
||||||
|
mod structures;
|
||||||
|
|
||||||
pub type PollResult = Result<(), PollError>;
|
pub type PollResult = Result<(), PollError>;
|
||||||
pub type ReadByteResult = Result<u8, CorruptedData>;
|
pub type ReadByteResult = Result<u8, CorruptedData>;
|
||||||
pub type CorruptedData = (u8, u8);
|
pub type CorruptedData = (u8, u8);
|
||||||
|
@ -107,100 +109,112 @@ pub trait SoftSerialWriter<P, T>
|
||||||
where
|
where
|
||||||
P: PinOps + StaticPinOps,
|
P: PinOps + StaticPinOps,
|
||||||
{
|
{
|
||||||
#[inline(never)]
|
fn write_byte(&self, data: u8);
|
||||||
fn write_byte(&self, data: u8) {
|
|
||||||
let (mut data, mut parity_bit) = (data, 0);
|
|
||||||
|
|
||||||
for _ in 0..8 {
|
|
||||||
if data & MSB == 0 {
|
|
||||||
P::set_high();
|
|
||||||
parity_bit ^= 0;
|
|
||||||
} else {
|
|
||||||
P::set_low();
|
|
||||||
parity_bit ^= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
delay_us(SERIAL_DELAY);
|
|
||||||
|
|
||||||
data <<= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Hamming code and CRC are very weightful and slow, so I use simple parity check
|
|
||||||
|
|
||||||
if parity_bit == 0 {
|
|
||||||
P::set_high();
|
|
||||||
} else {
|
|
||||||
P::set_low();
|
|
||||||
}
|
|
||||||
|
|
||||||
delay_us(SERIAL_DELAY);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_bytes(&self, transmit_data: T);
|
fn write_bytes(&self, transmit_data: T);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<P> SoftSerialWriter<P, &[u8]> for HalfDuplexSerial<P>
|
|
||||||
where
|
|
||||||
P: PinOps + StaticPinOps,
|
|
||||||
{
|
|
||||||
fn write_bytes(&self, transmit_data: &[u8]) {
|
|
||||||
for byte in transmit_data {
|
|
||||||
self.write_byte(*byte);
|
|
||||||
self.sync_transmitter();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait SoftSerialReader<P, T>
|
pub trait SoftSerialReader<P, T>
|
||||||
where
|
where
|
||||||
P: PinOps + StaticPinOps,
|
P: PinOps + StaticPinOps,
|
||||||
{
|
{
|
||||||
#[inline(never)]
|
fn read_byte(&self) -> ReadByteResult;
|
||||||
fn read_byte(&self) -> ReadByteResult {
|
fn read_bytes(&self, recieve_data: T);
|
||||||
let (mut data, mut reciever_parity_bit) = (0, 0);
|
}
|
||||||
|
|
||||||
delay_cycles(FIRST_ENTRY_READING);
|
#[inline(always)]
|
||||||
for _ in 0..8 {
|
pub(crate) fn _priv_read_byte<P: PinOps + StaticPinOps>() -> ReadByteResult {
|
||||||
delay_us(FIRST_HALF_SERIAL_DELAY);
|
let (mut data, mut reciever_parity_bit) = (0, 0);
|
||||||
|
|
||||||
data <<= 1;
|
|
||||||
|
|
||||||
if P::is_low() {
|
|
||||||
data |= 1;
|
|
||||||
reciever_parity_bit ^= 1;
|
|
||||||
} else {
|
|
||||||
data |= 0;
|
|
||||||
reciever_parity_bit ^= 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
delay_cycles(READING_ADJUST);
|
|
||||||
delay_us(SECOND_HALF_SERIAL_DELAY);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
delay_cycles(FIRST_ENTRY_READING);
|
||||||
|
for _ in 0..8 {
|
||||||
delay_us(FIRST_HALF_SERIAL_DELAY);
|
delay_us(FIRST_HALF_SERIAL_DELAY);
|
||||||
|
|
||||||
let transmitter_parity_bit = (P::read() >> P::PIN_NUM) & LSB;
|
data <<= 1;
|
||||||
|
|
||||||
|
if P::is_low() {
|
||||||
|
data |= 1;
|
||||||
|
reciever_parity_bit ^= 1;
|
||||||
|
} else {
|
||||||
|
data |= 0;
|
||||||
|
reciever_parity_bit ^= 0;
|
||||||
|
}
|
||||||
|
|
||||||
delay_cycles(READING_ADJUST);
|
delay_cycles(READING_ADJUST);
|
||||||
delay_us(SECOND_HALF_SERIAL_DELAY);
|
delay_us(SECOND_HALF_SERIAL_DELAY);
|
||||||
|
|
||||||
if reciever_parity_bit == transmitter_parity_bit {
|
|
||||||
return Err((data, reciever_parity_bit));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(data)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_bytes(&self, recieve_data: T);
|
delay_us(FIRST_HALF_SERIAL_DELAY);
|
||||||
|
|
||||||
|
let transmitter_parity_bit = (P::read() >> P::PIN_NUM) & LSB;
|
||||||
|
|
||||||
|
delay_cycles(READING_ADJUST);
|
||||||
|
delay_us(SECOND_HALF_SERIAL_DELAY);
|
||||||
|
|
||||||
|
if reciever_parity_bit == transmitter_parity_bit {
|
||||||
|
return Err((data, reciever_parity_bit));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub(crate) fn _priv_write_bytes<P: PinOps + StaticPinOps>(data: u8) {
|
||||||
|
let (mut data, mut parity_bit) = (data, 0);
|
||||||
|
|
||||||
|
for _ in 0..8 {
|
||||||
|
if data & MSB == 0 {
|
||||||
|
P::set_high();
|
||||||
|
parity_bit ^= 0;
|
||||||
|
} else {
|
||||||
|
P::set_low();
|
||||||
|
parity_bit ^= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
delay_us(SERIAL_DELAY);
|
||||||
|
|
||||||
|
data <<= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hamming code and CRC are very weightful and slow, so I use simple parity check
|
||||||
|
|
||||||
|
if parity_bit == 0 {
|
||||||
|
P::set_high();
|
||||||
|
} else {
|
||||||
|
P::set_low();
|
||||||
|
}
|
||||||
|
|
||||||
|
delay_us(SERIAL_DELAY);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<P> SoftSerialWriter<P, &[u8]> for HalfDuplexSerial<P>
|
||||||
|
where
|
||||||
|
P: PinOps + StaticPinOps,
|
||||||
|
{
|
||||||
|
#[inline(never)]
|
||||||
|
fn write_byte(&self, data: u8) {
|
||||||
|
_priv_write_bytes::<P>(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_bytes(&self, transmit_data: &[u8]) {
|
||||||
|
for byte in transmit_data {
|
||||||
|
<Self as SoftSerialWriter<P, &[u8]>>::write_byte(self, *byte);
|
||||||
|
self.sync_transmitter();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<P> SoftSerialReader<P, &mut [u8]> for HalfDuplexSerial<P>
|
impl<P> SoftSerialReader<P, &mut [u8]> for HalfDuplexSerial<P>
|
||||||
where
|
where
|
||||||
P: PinOps + StaticPinOps,
|
P: PinOps + StaticPinOps,
|
||||||
{
|
{
|
||||||
|
#[inline(never)]
|
||||||
|
fn read_byte(&self) -> ReadByteResult {
|
||||||
|
_priv_read_byte::<P>()
|
||||||
|
}
|
||||||
|
|
||||||
fn read_bytes(&self, recieve_data: &mut [u8]) {
|
fn read_bytes(&self, recieve_data: &mut [u8]) {
|
||||||
for byte in recieve_data {
|
for byte in recieve_data {
|
||||||
if let Ok(data) = self.read_byte() {
|
if let Ok(data) = <Self as SoftSerialReader<P, &mut [u8]>>::read_byte(self) {
|
||||||
*byte = data;
|
*byte = data;
|
||||||
}
|
}
|
||||||
self.sync_reciever();
|
self.sync_reciever();
|
||||||
|
|
Loading…
Reference in New Issue