Compare commits

...

18 Commits

Author SHA1 Message Date
315a0ae7c3 feat(ufmt): add feature 2025-08-16 22:16:00 +04:00
b98aad071f refactor!: add debug func (temp) + remove some delays + refactor bitbang 2025-08-16 22:15:51 +04:00
fee9ff945f feat(ringbuf): stabilize data streaming for ringbuffer 2025-08-16 22:14:57 +04:00
3e0b1a8451 refactor: move implementation to mod.rs 2025-08-10 21:32:20 +04:00
2e1bb877c6 fix: rename feature 2025-08-10 21:29:11 +04:00
6da908db14 my skill issue... 2025-08-10 21:27:11 +04:00
3d11f1382e fix: import ring buffer after fix Cargo.toml of ring-buffer crate 2025-08-10 21:22:25 +04:00
c77d02a7f7 i hate this 2025-08-10 21:18:19 +04:00
89270cb956 fix: add version 2025-08-10 21:04:11 +04:00
b78e62b351 fix: ring-buffer is optional 2025-08-10 21:01:08 +04:00
2efee051ff feat: add RingBuffer as crate feature 2025-08-10 20:57:55 +04:00
ca54784e64 feat(encode): add CRC calculation 2025-08-10 20:57:28 +04:00
71d952dce5 feat(impl): remove RingBuffer implementation 2025-08-10 20:56:14 +04:00
68c1ff13eb feat: remove complex type casting 2025-06-01 18:20:24 +04:00
e915deb1d5 fix: send only bytes written to the buffer instead of the entire buffer 2025-05-27 21:27:11 +04:00
f069c3b4b2 feat(writer): reduce byte transactions (from N to len of buffer) 2025-05-23 17:32:08 +04:00
f43340db30 fix: add re-export 2025-05-22 22:01:35 +04:00
b3aa8ea46a feat(sync): remove synchronization method calls 2025-05-22 21:58:45 +04:00
4 changed files with 206 additions and 234 deletions

View File

@@ -3,6 +3,14 @@ name = "soft-serial"
version = "0.1.0"
edition = "2021"
[dependencies.ufmt]
version = "*"
optional = true
[dependencies.ring-buffer]
git = "https://gitea.doryan04.ru/doryan/ring-buffer"
optional = true
[dependencies.arduino-hal]
git = "https://github.com/rahix/avr-hal"
rev = "3e362624547462928a219c40f9ea8e3a64f21e5f"
@@ -12,6 +20,8 @@ git = "https://gitea.doryan04.ru/TheEmbeddedRust/static-pins"
[features]
sparkfun-promicro = ["arduino-hal/sparkfun-promicro", "static-pins/sparkfun-promicro"]
ring-buf = ["dep:ring-buffer"]
ufmt = ["dep:ufmt"]
[dependencies.avr-device]
version = "0.5.4"

View File

@@ -4,6 +4,7 @@ use core::marker::PhantomData;
use arduino_hal::{
delay_us,
hal::port::{PB4, PB5, PE6},
port::{
mode::{Input, PullUp},
Pin, PinOps,
@@ -13,6 +14,7 @@ use arduino_hal::{
use avr_device::asm::delay_cycles;
use static_pins::StaticPinOps;
use ufmt::derive::uDebug;
mod structures;
@@ -24,11 +26,18 @@ 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 = 16;
const FIRST_ENTRY_READING: u32 = 30;
const LSB: u8 = 0x01;
const MSB: u8 = 0x80;
const READING_ADJUST: u32 = 10;
const TX_DELAY_CYCLES: u32 = 22;
const MSB: u16 = 0x0800;
const PACKET_MASK: u16 = 0x0FFF;
#[inline(always)]
fn crc_calculate(value: u8) -> u8 {
((value >> 6) | (value >> 4) | (value >> 2) | value) & 0x0F
}
#[cfg(feature = "ufmt")]
#[derive(uDebug)]
pub enum PollError {
NotFound,
NotReady,
@@ -38,40 +47,81 @@ 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();
}
impl<P> HalfDuplexSerial<P>
where
P: PinOps + StaticPinOps,
{
#[inline]
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();
}
Self {
_pin: PhantomData {},
}
}
pub fn poll(&self) -> PollResult {
PE6::set_low();
P::into_output();
delay_cycles(1);
delay_cycles(2);
P::into_pull_up_input();
delay_us(SERIAL_DELAY);
delay_us(SERIAL_DELAY + FIRST_HALF_SERIAL_DELAY);
if P::is_low() {
PE6::set_high();
return PollResult::Err(PollError::NotFound);
}
delay_us(SERIAL_DELAY);
if P::is_high() {
PE6::set_high();
return PollResult::Err(PollError::NotReady);
}
while P::is_low() {}
PE6::set_high();
PollResult::Ok(())
}
pub fn response(&self) {
PE6::set_low();
P::into_output_high();
delay_us(FIRST_HALF_SERIAL_DELAY);
@@ -80,23 +130,42 @@ where
delay_us(SERIAL_DELAY);
P::set_high();
P::into_pull_up_input();
PE6::set_high();
}
#[inline(never)]
#[inline]
pub fn sync_transmitter(&self) {
if cfg!(debug_assertions) {
PB5::set_low();
}
P::into_output();
delay_us(SERIAL_DELAY);
P::set_high();
if cfg!(debug_assertions) {
PB5::set_high();
}
}
#[inline(never)]
#[inline]
pub fn sync_reciever(&self) {
if cfg!(debug_assertions) {
PB5::set_low();
}
while P::is_high() {}
while P::is_low() {}
if cfg!(debug_assertions) {
PB5::set_high();
}
}
#[inline]
@@ -105,99 +174,95 @@ where
}
}
pub trait SoftSerialWriter<P, T>
pub trait SoftSerialWriter<P, T>: SoftSerialByteWriter<P>
where
P: PinOps + StaticPinOps,
{
fn write_byte(&self, data: u8);
fn write_bytes(&self, transmit_data: T);
}
pub trait SoftSerialReader<P, T>
pub trait SoftSerialReader<P, T>: SoftSerialByteReader<P>
where
P: PinOps + StaticPinOps,
{
fn read_byte(&self) -> ReadByteResult;
fn read_bytes(&self, recieve_data: T);
fn read_bytes(&self, recieve_data: T) -> Result<(), CorruptedData>;
}
#[inline(always)]
pub(crate) fn _priv_read_byte<P: PinOps + StaticPinOps>() -> ReadByteResult {
let (mut data, mut reciever_parity_bit) = (0, 0);
pub trait SoftSerialByteWriter<P>
where
P: PinOps + StaticPinOps,
{
#[inline(never)]
fn write_byte(&self, transmit_data: u8) {
let mut data = ((transmit_data as u16) << 4) | (crc_calculate(transmit_data) as u16);
delay_cycles(FIRST_ENTRY_READING);
for _ in 0..8 {
delay_us(FIRST_HALF_SERIAL_DELAY);
for _ in 0..(u8::BITS + 4) {
debug_pb4_low();
data <<= 1;
if data & MSB == 0 {
P::set_high();
} else {
P::set_low();
}
if P::is_low() {
data |= 1;
reciever_parity_bit ^= 1;
} else {
data |= 0;
reciever_parity_bit ^= 0;
data = (data << 1) & PACKET_MASK;
delay_us(SERIAL_DELAY);
debug_pb4_high();
}
delay_cycles(READING_ADJUST);
delay_us(SECOND_HALF_SERIAL_DELAY);
}
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_cycles(TX_DELAY_CYCLES);
}
delay_us(SERIAL_DELAY);
}
pub trait SoftSerialByteReader<P>
where
P: PinOps + StaticPinOps,
{
#[inline(never)]
fn read_byte(&self) -> ReadByteResult {
let mut packet = 0u16;
for _ in 0..(u8::BITS + 4) {
debug_pb4_low();
delay_us(FIRST_HALF_SERIAL_DELAY);
delay_cycles(READING_ADJUST);
if P::is_low() {
packet = (packet << 1) | 1;
} else {
packet <<= 1;
}
delay_us(SECOND_HALF_SERIAL_DELAY);
debug_pb4_high();
}
let data = (packet >> 4) as u8;
let received_crc = (packet & 0x000F) as u8;
let calculated_crc = crc_calculate(data);
if received_crc != calculated_crc {
return Err((received_crc, calculated_crc));
}
Ok(data)
}
}
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,
{
#[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.write_byte(*byte);
self.sync_transmitter();
}
}
@@ -207,17 +272,11 @@ impl<P> SoftSerialReader<P, &mut [u8]> for HalfDuplexSerial<P>
where
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]) -> Result<(), CorruptedData> {
for byte in recieve_data {
if let Ok(data) = <Self as SoftSerialReader<P, &mut [u8]>>::read_byte(self) {
*byte = data;
}
*byte = self.read_byte()?;
self.sync_reciever();
}
Ok(())
}
}

View File

@@ -1 +1,52 @@
pub mod ring_buffer;
use arduino_hal::port::PinOps;
use static_pins::StaticPinOps;
use crate::{
HalfDuplexSerial, SoftSerialByteReader, SoftSerialByteWriter, SoftSerialReader,
SoftSerialWriter,
};
#[cfg(feature = "ring-buf")]
pub mod ring_buf {
use crate::CorruptedData;
use super::*;
use ring_buffer::RingBuffer;
impl<const N: usize, P> SoftSerialWriter<P, &mut RingBuffer<N>> for HalfDuplexSerial<P>
where
P: PinOps + StaticPinOps,
{
#[inline(always)]
fn write_bytes(&self, transmit_data: &mut RingBuffer<N>) {
self.write_byte(transmit_data.len() as u8);
for byte in transmit_data {
self.sync_transmitter();
self.write_byte(byte);
}
self.sync_transmitter();
}
}
impl<const N: usize, P> SoftSerialReader<P, &mut RingBuffer<N>> for HalfDuplexSerial<P>
where
P: PinOps + StaticPinOps,
{
#[inline(always)]
fn read_bytes(&self, recieve_data: &mut RingBuffer<N>) -> Result<(), CorruptedData> {
let len = self.read_byte()?;
for _ in 0..len {
self.sync_reciever();
recieve_data.push(self.read_byte()?);
}
self.sync_reciever();
Ok(())
}
}
}

View File

@@ -1,148 +0,0 @@
// 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(());
}
}
}
}
}