Compare commits
No commits in common. "2efee051fff91e0173916ae9a24f1b0f8bc621ad" and "68c1ff13ebd285d79e5e8adf8469bcaa368d2d3e" have entirely different histories.
2efee051ff
...
68c1ff13eb
|
@ -3,9 +3,6 @@ name = "soft-serial"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies.ring-buffer]
|
|
||||||
git = "https://gitea.doryan04.ru/doryan/ring-buffer"
|
|
||||||
|
|
||||||
[dependencies.arduino-hal]
|
[dependencies.arduino-hal]
|
||||||
git = "https://github.com/rahix/avr-hal"
|
git = "https://github.com/rahix/avr-hal"
|
||||||
rev = "3e362624547462928a219c40f9ea8e3a64f21e5f"
|
rev = "3e362624547462928a219c40f9ea8e3a64f21e5f"
|
||||||
|
@ -15,7 +12,6 @@ git = "https://gitea.doryan04.ru/TheEmbeddedRust/static-pins"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
sparkfun-promicro = ["arduino-hal/sparkfun-promicro", "static-pins/sparkfun-promicro"]
|
sparkfun-promicro = ["arduino-hal/sparkfun-promicro", "static-pins/sparkfun-promicro"]
|
||||||
ring-buffer = ["dep:ring-buffer"]
|
|
||||||
|
|
||||||
[dependencies.avr-device]
|
[dependencies.avr-device]
|
||||||
version = "0.5.4"
|
version = "0.5.4"
|
||||||
|
|
51
src/lib.rs
51
src/lib.rs
|
@ -28,12 +28,8 @@ const SECOND_HALF_SERIAL_DELAY: u32 = SERIAL_DELAY - FIRST_HALF_SERIAL_DELAY;
|
||||||
|
|
||||||
const READING_ADJUST: u32 = 16;
|
const READING_ADJUST: u32 = 16;
|
||||||
const FIRST_ENTRY_READING: u32 = 30;
|
const FIRST_ENTRY_READING: u32 = 30;
|
||||||
const MSB: u16 = 0x0200;
|
const LSB: u8 = 0x01;
|
||||||
|
const MSB: u8 = 0x80;
|
||||||
#[inline(always)]
|
|
||||||
fn crc_calculate(value: u8) -> u8 {
|
|
||||||
((value >> 6) | (value >> 4) | (value >> 2) | value) & 0x0F
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum PollError {
|
pub enum PollError {
|
||||||
NotFound,
|
NotFound,
|
||||||
|
@ -57,7 +53,7 @@ where
|
||||||
|
|
||||||
pub fn poll(&self) -> PollResult {
|
pub fn poll(&self) -> PollResult {
|
||||||
P::into_output();
|
P::into_output();
|
||||||
delay_cycles(4);
|
delay_cycles(1);
|
||||||
P::into_pull_up_input();
|
P::into_pull_up_input();
|
||||||
|
|
||||||
delay_us(SERIAL_DELAY);
|
delay_us(SERIAL_DELAY);
|
||||||
|
@ -131,19 +127,31 @@ where
|
||||||
{
|
{
|
||||||
#[inline(never)]
|
#[inline(never)]
|
||||||
fn write_byte(&self, transmit_data: u8) {
|
fn write_byte(&self, transmit_data: u8) {
|
||||||
let mut data = ((transmit_data as u16) << 4) | (crc_calculate(transmit_data) as u16);
|
let (mut data, mut parity_bit) = (transmit_data, 0);
|
||||||
|
|
||||||
for _ in 0..(u8::BITS + 4) {
|
for _ in 0..8 {
|
||||||
if data & MSB == 0 {
|
if data & MSB == 0 {
|
||||||
P::set_high();
|
P::set_high();
|
||||||
|
parity_bit ^= 0;
|
||||||
} else {
|
} else {
|
||||||
P::set_low();
|
P::set_low();
|
||||||
|
parity_bit ^= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
delay_us(SERIAL_DELAY);
|
delay_us(SERIAL_DELAY);
|
||||||
|
|
||||||
data <<= 1;
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,30 +161,35 @@ where
|
||||||
{
|
{
|
||||||
#[inline(never)]
|
#[inline(never)]
|
||||||
fn read_byte(&self) -> ReadByteResult {
|
fn read_byte(&self) -> ReadByteResult {
|
||||||
let mut packet = 0u16;
|
let (mut data, mut reciever_parity_bit) = (0, 0);
|
||||||
|
|
||||||
delay_cycles(FIRST_ENTRY_READING);
|
delay_cycles(FIRST_ENTRY_READING);
|
||||||
for _ in 0..(u8::BITS + 4) {
|
for _ in 0..8 {
|
||||||
delay_us(FIRST_HALF_SERIAL_DELAY);
|
delay_us(FIRST_HALF_SERIAL_DELAY);
|
||||||
|
|
||||||
packet <<= 1;
|
data <<= 1;
|
||||||
|
|
||||||
if P::is_low() {
|
if P::is_low() {
|
||||||
packet |= 1;
|
data |= 1;
|
||||||
|
reciever_parity_bit ^= 1;
|
||||||
} else {
|
} else {
|
||||||
packet |= 0;
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
let received_crc = (packet & 0x000F) as u8;
|
delay_us(FIRST_HALF_SERIAL_DELAY);
|
||||||
let data = packet.overflowing_shr(4).0 as u8;
|
|
||||||
let calculated_crc = crc_calculate(data);
|
|
||||||
|
|
||||||
if received_crc != calculated_crc {
|
let transmitter_parity_bit = (P::read() >> P::PIN_NUM) & LSB;
|
||||||
return Err((received_crc, calculated_crc));
|
|
||||||
|
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)
|
Ok(data)
|
||||||
|
|
|
@ -9,7 +9,106 @@ use crate::{
|
||||||
SoftSerialWriter,
|
SoftSerialWriter,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(feature = "ring-buffer")]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct RingBuffer<const N: usize> {
|
||||||
|
buf: [u8; N],
|
||||||
|
mask: usize,
|
||||||
|
head: usize,
|
||||||
|
tail: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const N: usize> Default for RingBuffer<N> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const N: usize> RingBuffer<N> {
|
||||||
|
#[inline]
|
||||||
|
pub const fn new() -> Self {
|
||||||
|
if N.is_power_of_two() {
|
||||||
|
Self {
|
||||||
|
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>
|
impl<const N: usize, P> SoftSerialWriter<P, &mut RingBuffer<N>> for HalfDuplexSerial<P>
|
||||||
where
|
where
|
||||||
P: PinOps + StaticPinOps,
|
P: PinOps + StaticPinOps,
|
||||||
|
@ -25,7 +124,6 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "ring-buffer")]
|
|
||||||
impl<const N: usize, P> SoftSerialReader<P, &mut RingBuffer<N>> for HalfDuplexSerial<P>
|
impl<const N: usize, P> SoftSerialReader<P, &mut RingBuffer<N>> for HalfDuplexSerial<P>
|
||||||
where
|
where
|
||||||
P: PinOps + StaticPinOps,
|
P: PinOps + StaticPinOps,
|
||||||
|
@ -39,7 +137,7 @@ where
|
||||||
self.sync_reciever();
|
self.sync_reciever();
|
||||||
|
|
||||||
if let Ok(byte) = self.read_byte() {
|
if let Ok(byte) = self.read_byte() {
|
||||||
recieve_data.push(byte);
|
recieve_data.push(byte).unwrap_or(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue