Compare commits
No commits in common. "315a0ae7c38a72b9613b0d6a36af5c9decefb941" and "3e0b1a845112e990acde7957e12e7abc2aa0d653" have entirely different histories.
315a0ae7c3
...
3e0b1a8451
|
@ -3,10 +3,6 @@ name = "soft-serial"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies.ufmt]
|
|
||||||
version = "*"
|
|
||||||
optional = true
|
|
||||||
|
|
||||||
[dependencies.ring-buffer]
|
[dependencies.ring-buffer]
|
||||||
git = "https://gitea.doryan04.ru/doryan/ring-buffer"
|
git = "https://gitea.doryan04.ru/doryan/ring-buffer"
|
||||||
optional = true
|
optional = true
|
||||||
|
@ -21,7 +17,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-buf = ["dep:ring-buffer"]
|
ring-buf = ["dep:ring-buffer"]
|
||||||
ufmt = ["dep:ufmt"]
|
|
||||||
|
|
||||||
[dependencies.avr-device]
|
[dependencies.avr-device]
|
||||||
version = "0.5.4"
|
version = "0.5.4"
|
||||||
|
|
113
src/lib.rs
113
src/lib.rs
|
@ -4,7 +4,6 @@ use core::marker::PhantomData;
|
||||||
|
|
||||||
use arduino_hal::{
|
use arduino_hal::{
|
||||||
delay_us,
|
delay_us,
|
||||||
hal::port::{PB4, PB5, PE6},
|
|
||||||
port::{
|
port::{
|
||||||
mode::{Input, PullUp},
|
mode::{Input, PullUp},
|
||||||
Pin, PinOps,
|
Pin, PinOps,
|
||||||
|
@ -14,7 +13,6 @@ use arduino_hal::{
|
||||||
use avr_device::asm::delay_cycles;
|
use avr_device::asm::delay_cycles;
|
||||||
|
|
||||||
use static_pins::StaticPinOps;
|
use static_pins::StaticPinOps;
|
||||||
use ufmt::derive::uDebug;
|
|
||||||
|
|
||||||
mod structures;
|
mod structures;
|
||||||
|
|
||||||
|
@ -26,18 +24,15 @@ const SERIAL_DELAY: u32 = 4;
|
||||||
const FIRST_HALF_SERIAL_DELAY: u32 = SERIAL_DELAY / 2;
|
const FIRST_HALF_SERIAL_DELAY: u32 = SERIAL_DELAY / 2;
|
||||||
const SECOND_HALF_SERIAL_DELAY: u32 = SERIAL_DELAY - FIRST_HALF_SERIAL_DELAY;
|
const SECOND_HALF_SERIAL_DELAY: u32 = SERIAL_DELAY - FIRST_HALF_SERIAL_DELAY;
|
||||||
|
|
||||||
const READING_ADJUST: u32 = 10;
|
const READING_ADJUST: u32 = 16;
|
||||||
const TX_DELAY_CYCLES: u32 = 22;
|
const FIRST_ENTRY_READING: u32 = 30;
|
||||||
const MSB: u16 = 0x0800;
|
const MSB: u16 = 0x0200;
|
||||||
const PACKET_MASK: u16 = 0x0FFF;
|
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn crc_calculate(value: u8) -> u8 {
|
fn crc_calculate(value: u8) -> u8 {
|
||||||
((value >> 6) | (value >> 4) | (value >> 2) | value) & 0x0F
|
((value >> 6) | (value >> 4) | (value >> 2) | value) & 0x0F
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "ufmt")]
|
|
||||||
#[derive(uDebug)]
|
|
||||||
pub enum PollError {
|
pub enum PollError {
|
||||||
NotFound,
|
NotFound,
|
||||||
NotReady,
|
NotReady,
|
||||||
|
@ -47,81 +42,40 @@ pub struct HalfDuplexSerial<P> {
|
||||||
_pin: PhantomData<Pin<Input<PullUp>, 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>
|
impl<P> HalfDuplexSerial<P>
|
||||||
where
|
where
|
||||||
P: PinOps + StaticPinOps,
|
P: PinOps + StaticPinOps,
|
||||||
{
|
{
|
||||||
|
#[inline]
|
||||||
pub fn new(_pin: Pin<Input<PullUp>, P>) -> Self {
|
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 {
|
Self {
|
||||||
_pin: PhantomData {},
|
_pin: PhantomData {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn poll(&self) -> PollResult {
|
pub fn poll(&self) -> PollResult {
|
||||||
PE6::set_low();
|
|
||||||
P::into_output();
|
P::into_output();
|
||||||
|
delay_cycles(4);
|
||||||
delay_cycles(2);
|
|
||||||
|
|
||||||
P::into_pull_up_input();
|
P::into_pull_up_input();
|
||||||
|
|
||||||
delay_us(SERIAL_DELAY + FIRST_HALF_SERIAL_DELAY);
|
delay_us(SERIAL_DELAY);
|
||||||
|
|
||||||
if P::is_low() {
|
if P::is_low() {
|
||||||
PE6::set_high();
|
|
||||||
return PollResult::Err(PollError::NotFound);
|
return PollResult::Err(PollError::NotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
delay_us(SERIAL_DELAY);
|
delay_us(SERIAL_DELAY);
|
||||||
|
|
||||||
if P::is_high() {
|
if P::is_high() {
|
||||||
PE6::set_high();
|
|
||||||
return PollResult::Err(PollError::NotReady);
|
return PollResult::Err(PollError::NotReady);
|
||||||
}
|
}
|
||||||
|
|
||||||
while P::is_low() {}
|
while P::is_low() {}
|
||||||
|
|
||||||
PE6::set_high();
|
|
||||||
|
|
||||||
PollResult::Ok(())
|
PollResult::Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn response(&self) {
|
pub fn response(&self) {
|
||||||
PE6::set_low();
|
|
||||||
|
|
||||||
P::into_output_high();
|
P::into_output_high();
|
||||||
|
|
||||||
delay_us(FIRST_HALF_SERIAL_DELAY);
|
delay_us(FIRST_HALF_SERIAL_DELAY);
|
||||||
|
@ -130,42 +84,23 @@ where
|
||||||
|
|
||||||
delay_us(SERIAL_DELAY);
|
delay_us(SERIAL_DELAY);
|
||||||
|
|
||||||
P::set_high();
|
|
||||||
P::into_pull_up_input();
|
P::into_pull_up_input();
|
||||||
|
|
||||||
PE6::set_high();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline(never)]
|
||||||
pub fn sync_transmitter(&self) {
|
pub fn sync_transmitter(&self) {
|
||||||
if cfg!(debug_assertions) {
|
|
||||||
PB5::set_low();
|
|
||||||
}
|
|
||||||
|
|
||||||
P::into_output();
|
P::into_output();
|
||||||
|
|
||||||
delay_us(SERIAL_DELAY);
|
delay_us(SERIAL_DELAY);
|
||||||
|
|
||||||
P::set_high();
|
P::set_high();
|
||||||
|
|
||||||
if cfg!(debug_assertions) {
|
|
||||||
PB5::set_high();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline(never)]
|
||||||
pub fn sync_reciever(&self) {
|
pub fn sync_reciever(&self) {
|
||||||
if cfg!(debug_assertions) {
|
|
||||||
PB5::set_low();
|
|
||||||
}
|
|
||||||
|
|
||||||
while P::is_high() {}
|
while P::is_high() {}
|
||||||
|
|
||||||
while P::is_low() {}
|
while P::is_low() {}
|
||||||
|
|
||||||
if cfg!(debug_assertions) {
|
|
||||||
PB5::set_high();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -185,7 +120,7 @@ pub trait SoftSerialReader<P, T>: SoftSerialByteReader<P>
|
||||||
where
|
where
|
||||||
P: PinOps + StaticPinOps,
|
P: PinOps + StaticPinOps,
|
||||||
{
|
{
|
||||||
fn read_bytes(&self, recieve_data: T) -> Result<(), CorruptedData>;
|
fn read_bytes(&self, recieve_data: T);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait SoftSerialByteWriter<P>
|
pub trait SoftSerialByteWriter<P>
|
||||||
|
@ -197,22 +132,16 @@ where
|
||||||
let mut data = ((transmit_data as u16) << 4) | (crc_calculate(transmit_data) as u16);
|
let mut data = ((transmit_data as u16) << 4) | (crc_calculate(transmit_data) as u16);
|
||||||
|
|
||||||
for _ in 0..(u8::BITS + 4) {
|
for _ in 0..(u8::BITS + 4) {
|
||||||
debug_pb4_low();
|
|
||||||
|
|
||||||
if data & MSB == 0 {
|
if data & MSB == 0 {
|
||||||
P::set_high();
|
P::set_high();
|
||||||
} else {
|
} else {
|
||||||
P::set_low();
|
P::set_low();
|
||||||
}
|
}
|
||||||
|
|
||||||
data = (data << 1) & PACKET_MASK;
|
|
||||||
delay_us(SERIAL_DELAY);
|
delay_us(SERIAL_DELAY);
|
||||||
|
|
||||||
debug_pb4_high();
|
data <<= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
P::set_high();
|
|
||||||
delay_cycles(TX_DELAY_CYCLES);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -224,25 +153,24 @@ where
|
||||||
fn read_byte(&self) -> ReadByteResult {
|
fn read_byte(&self) -> ReadByteResult {
|
||||||
let mut packet = 0u16;
|
let mut packet = 0u16;
|
||||||
|
|
||||||
|
delay_cycles(FIRST_ENTRY_READING);
|
||||||
for _ in 0..(u8::BITS + 4) {
|
for _ in 0..(u8::BITS + 4) {
|
||||||
debug_pb4_low();
|
|
||||||
|
|
||||||
delay_us(FIRST_HALF_SERIAL_DELAY);
|
delay_us(FIRST_HALF_SERIAL_DELAY);
|
||||||
delay_cycles(READING_ADJUST);
|
|
||||||
|
packet <<= 1;
|
||||||
|
|
||||||
if P::is_low() {
|
if P::is_low() {
|
||||||
packet = (packet << 1) | 1;
|
packet |= 1;
|
||||||
} else {
|
} else {
|
||||||
packet <<= 1;
|
packet |= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
delay_cycles(READING_ADJUST);
|
||||||
delay_us(SECOND_HALF_SERIAL_DELAY);
|
delay_us(SECOND_HALF_SERIAL_DELAY);
|
||||||
|
|
||||||
debug_pb4_high();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let data = (packet >> 4) as u8;
|
|
||||||
let received_crc = (packet & 0x000F) as u8;
|
let received_crc = (packet & 0x000F) as u8;
|
||||||
|
let data = packet.overflowing_shr(4).0 as u8;
|
||||||
let calculated_crc = crc_calculate(data);
|
let calculated_crc = crc_calculate(data);
|
||||||
|
|
||||||
if received_crc != calculated_crc {
|
if received_crc != calculated_crc {
|
||||||
|
@ -272,11 +200,12 @@ impl<P> SoftSerialReader<P, &mut [u8]> for HalfDuplexSerial<P>
|
||||||
where
|
where
|
||||||
P: PinOps + StaticPinOps,
|
P: PinOps + StaticPinOps,
|
||||||
{
|
{
|
||||||
fn read_bytes(&self, recieve_data: &mut [u8]) -> Result<(), CorruptedData> {
|
fn read_bytes(&self, recieve_data: &mut [u8]) {
|
||||||
for byte in recieve_data {
|
for byte in recieve_data {
|
||||||
*byte = self.read_byte()?;
|
if let Ok(data) = self.read_byte() {
|
||||||
|
*byte = data;
|
||||||
|
}
|
||||||
self.sync_reciever();
|
self.sync_reciever();
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,8 +9,6 @@ use crate::{
|
||||||
|
|
||||||
#[cfg(feature = "ring-buf")]
|
#[cfg(feature = "ring-buf")]
|
||||||
pub mod ring_buf {
|
pub mod ring_buf {
|
||||||
use crate::CorruptedData;
|
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use ring_buffer::RingBuffer;
|
use ring_buffer::RingBuffer;
|
||||||
|
|
||||||
|
@ -18,7 +16,7 @@ pub mod ring_buf {
|
||||||
where
|
where
|
||||||
P: PinOps + StaticPinOps,
|
P: PinOps + StaticPinOps,
|
||||||
{
|
{
|
||||||
#[inline(always)]
|
#[inline(never)]
|
||||||
fn write_bytes(&self, transmit_data: &mut RingBuffer<N>) {
|
fn write_bytes(&self, transmit_data: &mut RingBuffer<N>) {
|
||||||
self.write_byte(transmit_data.len() as u8);
|
self.write_byte(transmit_data.len() as u8);
|
||||||
|
|
||||||
|
@ -26,8 +24,6 @@ pub mod ring_buf {
|
||||||
self.sync_transmitter();
|
self.sync_transmitter();
|
||||||
self.write_byte(byte);
|
self.write_byte(byte);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.sync_transmitter();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,18 +31,19 @@ pub mod ring_buf {
|
||||||
where
|
where
|
||||||
P: PinOps + StaticPinOps,
|
P: PinOps + StaticPinOps,
|
||||||
{
|
{
|
||||||
#[inline(always)]
|
#[inline(never)]
|
||||||
fn read_bytes(&self, recieve_data: &mut RingBuffer<N>) -> Result<(), CorruptedData> {
|
fn read_bytes(&self, recieve_data: &mut RingBuffer<N>) {
|
||||||
let len = self.read_byte()?;
|
let byte = self.read_byte();
|
||||||
|
|
||||||
|
if let Ok(len) = byte {
|
||||||
for _ in 0..len {
|
for _ in 0..len {
|
||||||
self.sync_reciever();
|
self.sync_reciever();
|
||||||
recieve_data.push(self.read_byte()?);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.sync_reciever();
|
if let Ok(byte) = self.read_byte() {
|
||||||
|
recieve_data.push(byte);
|
||||||
Ok(())
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue