Compare commits
2 Commits
14bb705e94
...
b72be2fc3b
Author | SHA1 | Date | |
---|---|---|---|
|
b72be2fc3b | ||
|
7cbbe955bb |
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();
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
pub mod ring_buffer;
|
|
@ -0,0 +1,148 @@
|
||||||
|
// Thanks to Low Byte Productions, I like this channel.
|
||||||
|
// Youtube: https://www.youtube.com/watch?v=uIJnATS9j_0
|
||||||
|
|
||||||
|
use arduino_hal::port::PinOps;
|
||||||
|
use static_pins::StaticPinOps;
|
||||||
|
|
||||||
|
use crate::{HalfDuplexSerial, SoftSerialReader, SoftSerialWriter};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct RingBuffer<const N: usize> {
|
||||||
|
buf: [u8; N],
|
||||||
|
mask: usize,
|
||||||
|
head: usize,
|
||||||
|
tail: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const N: usize> RingBuffer<N> {
|
||||||
|
#[inline]
|
||||||
|
pub const fn new() -> Self {
|
||||||
|
if N.is_power_of_two() {
|
||||||
|
RingBuffer {
|
||||||
|
buf: [0; N],
|
||||||
|
mask: N - 1,
|
||||||
|
head: 0,
|
||||||
|
tail: 0,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
panic!("Buffer capacity isn't power of two");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn get_buffer(&self) -> &[u8] {
|
||||||
|
&self.buf
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.head == self.tail
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn is_full(&self) -> bool {
|
||||||
|
(self.head + 1) & self.mask == self.tail
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn capacity(&self) -> usize {
|
||||||
|
N
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.head.overflowing_sub(self.tail).0 & self.mask
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn clear(&mut self) {
|
||||||
|
self.buf = [0; N];
|
||||||
|
self.head = 0;
|
||||||
|
self.tail = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn push(&mut self, value: u8) -> Option<()> {
|
||||||
|
let (head, tail) = (self.head, self.tail);
|
||||||
|
let next_head = (head + 1) & self.mask;
|
||||||
|
|
||||||
|
if next_head == tail {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.buf[head] = value;
|
||||||
|
self.head = next_head;
|
||||||
|
|
||||||
|
Some(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn pop(&mut self) -> Option<u8> {
|
||||||
|
let (head, mut tail) = (self.head, self.tail);
|
||||||
|
|
||||||
|
if head == tail {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let value = self.buf[tail];
|
||||||
|
tail = (tail + 1) & self.mask;
|
||||||
|
self.tail = tail;
|
||||||
|
|
||||||
|
Some(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const N: usize> Iterator for RingBuffer<N> {
|
||||||
|
type Item = u8;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
self.pop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const N: usize, P> SoftSerialWriter<P, &mut RingBuffer<N>> for HalfDuplexSerial<P>
|
||||||
|
where
|
||||||
|
P: PinOps + StaticPinOps,
|
||||||
|
{
|
||||||
|
#[inline(never)]
|
||||||
|
fn write_byte(&self, data: u8) {
|
||||||
|
crate::_priv_write_bytes::<P>(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_bytes(&self, transmit_data: &mut RingBuffer<N>) {
|
||||||
|
self.sync_transmitter();
|
||||||
|
<Self as SoftSerialWriter<P, &mut RingBuffer<N>>>::write_byte(self, N as u8);
|
||||||
|
|
||||||
|
for _ in 0..N {
|
||||||
|
let byte = transmit_data.pop().unwrap_or(0);
|
||||||
|
self.sync_transmitter();
|
||||||
|
<Self as SoftSerialWriter<P, &mut RingBuffer<N>>>::write_byte(self, byte);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const N: usize, P> SoftSerialReader<P, &mut RingBuffer<N>> for HalfDuplexSerial<P>
|
||||||
|
where
|
||||||
|
P: PinOps + StaticPinOps,
|
||||||
|
{
|
||||||
|
#[inline(never)]
|
||||||
|
fn read_byte(&self) -> crate::ReadByteResult {
|
||||||
|
crate::_priv_read_byte::<P>()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_bytes(&self, recieve_data: &mut RingBuffer<N>) {
|
||||||
|
self.sync_reciever();
|
||||||
|
let byte = <Self as SoftSerialReader<P, &mut RingBuffer<N>>>::read_byte(self);
|
||||||
|
|
||||||
|
if let Ok(len) = byte {
|
||||||
|
for _ in 0..len {
|
||||||
|
self.sync_reciever();
|
||||||
|
|
||||||
|
if let Ok(byte) = <Self as SoftSerialReader<P, &mut RingBuffer<N>>>::read_byte(self)
|
||||||
|
{
|
||||||
|
recieve_data.push(byte).unwrap_or(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue