Compare commits

..

18 Commits

Author SHA1 Message Date
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
b72be2fc3b feat(buf): implement SoftSerial traits for HalfDuplexSerial with RingBuffer 2025-05-22 21:52:03 +04:00
7cbbe955bb feat(buf): add ringbuffer structure 2025-05-22 21:51:13 +04:00
14bb705e94 feat: remove redundant loop 2025-04-30 23:03:55 +04:00
096fa7853f fix: revert sync timing 2025-04-30 23:03:42 +04:00
6991e5611e feat: remove SoftSerial trait 2025-04-30 18:08:12 +04:00
caa1ff9f24 feat: move sync and handshake methods to struct implementation 2025-04-30 18:07:36 +04:00
6defd8963f feat(writer/reader): move write and read methods to SoftSerialWriter and SoftSerialReader traits + impl these traits
a
2025-04-30 18:06:46 +04:00
46837a68e4 fix(check): change bool expression for check condition 2025-04-25 23:11:33 +04:00
698851cbfd fix(read): prevent redundant data left bit shifting
fix(parity_bit):
2025-04-25 23:10:09 +04:00
788060bf38 feat: rename crate dependency 2025-04-25 16:35:08 +04:00
a4657e202f feat(feature): add sparkfun-promicro feature 2025-04-25 13:46:42 +04:00
aa554f1fe5 feat: remove avr specs 2025-04-25 12:25:19 +04:00
b5e0708b09 feat: remove interrupt configuration in constructor 2025-04-24 22:40:41 +04:00
20 changed files with 223 additions and 714 deletions

View File

@@ -6,10 +6,12 @@ edition = "2021"
[dependencies.arduino-hal] [dependencies.arduino-hal]
git = "https://github.com/rahix/avr-hal" git = "https://github.com/rahix/avr-hal"
rev = "3e362624547462928a219c40f9ea8e3a64f21e5f" rev = "3e362624547462928a219c40f9ea8e3a64f21e5f"
features = ["sparkfun-promicro"]
[dependencies.static_pins] [dependencies.static-pins]
git = "https://gitea.doryan04.ru/TheEmbeddedRust/static_pins" git = "https://gitea.doryan04.ru/TheEmbeddedRust/static-pins"
[features]
sparkfun-promicro = ["arduino-hal/sparkfun-promicro", "static-pins/sparkfun-promicro"]
[dependencies.avr-device] [dependencies.avr-device]
version = "0.5.4" version = "0.5.4"

View File

@@ -1,41 +0,0 @@
{
"arch": "avr",
"atomic-cas": false,
"cpu": "atmega1280",
"crt-objects-fallback": "false",
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
"eh-frame-header": false,
"exe-suffix": ".elf",
"late-link-args": {
"gnu-cc": [
"-lgcc"
],
"gnu-lld-cc": [
"-lgcc"
]
},
"linker": "avr-gcc",
"linker-flavor": "gnu-cc",
"llvm-target": "avr-unknown-unknown",
"max-atomic-width": 8,
"metadata": {
"description": null,
"host_tools": null,
"std": null,
"tier": null
},
"no-default-libraries": false,
"pre-link-args": {
"gnu-cc": [
"-mmcu=atmega1280",
"-Wl,--as-needed,--print-memory-usage"
],
"gnu-lld-cc": [
"-mmcu=atmega1280",
"-Wl,--as-needed,--print-memory-usage"
]
},
"relocation-model": "static",
"target-c-int-width": "16",
"target-pointer-width": "16"
}

View File

@@ -1,41 +0,0 @@
{
"arch": "avr",
"atomic-cas": false,
"cpu": "atmega1284p",
"crt-objects-fallback": "false",
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
"eh-frame-header": false,
"exe-suffix": ".elf",
"late-link-args": {
"gnu-cc": [
"-lgcc"
],
"gnu-lld-cc": [
"-lgcc"
]
},
"linker": "avr-gcc",
"linker-flavor": "gnu-cc",
"llvm-target": "avr-unknown-unknown",
"max-atomic-width": 8,
"metadata": {
"description": null,
"host_tools": null,
"std": null,
"tier": null
},
"no-default-libraries": false,
"pre-link-args": {
"gnu-cc": [
"-mmcu=atmega1284p",
"-Wl,--as-needed,--print-memory-usage"
],
"gnu-lld-cc": [
"-mmcu=atmega1284p",
"-Wl,--as-needed,--print-memory-usage"
]
},
"relocation-model": "static",
"target-c-int-width": "16",
"target-pointer-width": "16"
}

View File

@@ -1,41 +0,0 @@
{
"arch": "avr",
"atomic-cas": false,
"cpu": "atmega128a",
"crt-objects-fallback": "false",
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
"eh-frame-header": false,
"exe-suffix": ".elf",
"late-link-args": {
"gnu-cc": [
"-lgcc"
],
"gnu-lld-cc": [
"-lgcc"
]
},
"linker": "avr-gcc",
"linker-flavor": "gnu-cc",
"llvm-target": "avr-unknown-unknown",
"max-atomic-width": 8,
"metadata": {
"description": null,
"host_tools": null,
"std": null,
"tier": null
},
"no-default-libraries": false,
"pre-link-args": {
"gnu-cc": [
"-mmcu=atmega128a",
"-Wl,--as-needed,--print-memory-usage"
],
"gnu-lld-cc": [
"-mmcu=atmega128a",
"-Wl,--as-needed,--print-memory-usage"
]
},
"relocation-model": "static",
"target-c-int-width": "16",
"target-pointer-width": "16"
}

View File

@@ -1,41 +0,0 @@
{
"arch": "avr",
"atomic-cas": false,
"cpu": "atmega164pa",
"crt-objects-fallback": "false",
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
"eh-frame-header": false,
"exe-suffix": ".elf",
"late-link-args": {
"gnu-cc": [
"-lgcc"
],
"gnu-lld-cc": [
"-lgcc"
]
},
"linker": "avr-gcc",
"linker-flavor": "gnu-cc",
"llvm-target": "avr-unknown-unknown",
"max-atomic-width": 8,
"metadata": {
"description": null,
"host_tools": null,
"std": null,
"tier": null
},
"no-default-libraries": false,
"pre-link-args": {
"gnu-cc": [
"-mmcu=atmega164pa",
"-Wl,--as-needed,--print-memory-usage"
],
"gnu-lld-cc": [
"-mmcu=atmega164pa",
"-Wl,--as-needed,--print-memory-usage"
]
},
"relocation-model": "static",
"target-c-int-width": "16",
"target-pointer-width": "16"
}

View File

@@ -1,41 +0,0 @@
{
"arch": "avr",
"atomic-cas": false,
"cpu": "atmega168",
"crt-objects-fallback": "false",
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
"eh-frame-header": false,
"exe-suffix": ".elf",
"late-link-args": {
"gnu-cc": [
"-lgcc"
],
"gnu-lld-cc": [
"-lgcc"
]
},
"linker": "avr-gcc",
"linker-flavor": "gnu-cc",
"llvm-target": "avr-unknown-unknown",
"max-atomic-width": 8,
"metadata": {
"description": null,
"host_tools": null,
"std": null,
"tier": null
},
"no-default-libraries": false,
"pre-link-args": {
"gnu-cc": [
"-mmcu=atmega168",
"-Wl,--as-needed,--print-memory-usage"
],
"gnu-lld-cc": [
"-mmcu=atmega168",
"-Wl,--as-needed,--print-memory-usage"
]
},
"relocation-model": "static",
"target-c-int-width": "16",
"target-pointer-width": "16"
}

View File

@@ -1,41 +0,0 @@
{
"arch": "avr",
"atomic-cas": false,
"cpu": "atmega2560",
"crt-objects-fallback": "false",
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
"eh-frame-header": false,
"exe-suffix": ".elf",
"late-link-args": {
"gnu-cc": [
"-lgcc"
],
"gnu-lld-cc": [
"-lgcc"
]
},
"linker": "avr-gcc",
"linker-flavor": "gnu-cc",
"llvm-target": "avr-unknown-unknown",
"max-atomic-width": 8,
"metadata": {
"description": null,
"host_tools": null,
"std": null,
"tier": null
},
"no-default-libraries": false,
"pre-link-args": {
"gnu-cc": [
"-mmcu=atmega2560",
"-Wl,--as-needed,--print-memory-usage"
],
"gnu-lld-cc": [
"-mmcu=atmega2560",
"-Wl,--as-needed,--print-memory-usage"
]
},
"relocation-model": "static",
"target-c-int-width": "16",
"target-pointer-width": "16"
}

View File

@@ -1,41 +0,0 @@
{
"arch": "avr",
"atomic-cas": false,
"cpu": "atmega328",
"crt-objects-fallback": "false",
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
"eh-frame-header": false,
"exe-suffix": ".elf",
"late-link-args": {
"gnu-cc": [
"-lgcc"
],
"gnu-lld-cc": [
"-lgcc"
]
},
"linker": "avr-gcc",
"linker-flavor": "gnu-cc",
"llvm-target": "avr-unknown-unknown",
"max-atomic-width": 8,
"metadata": {
"description": null,
"host_tools": null,
"std": null,
"tier": null
},
"no-default-libraries": false,
"pre-link-args": {
"gnu-cc": [
"-mmcu=atmega328",
"-Wl,--as-needed,--print-memory-usage"
],
"gnu-lld-cc": [
"-mmcu=atmega328",
"-Wl,--as-needed,--print-memory-usage"
]
},
"relocation-model": "static",
"target-c-int-width": "16",
"target-pointer-width": "16"
}

View File

@@ -1,41 +0,0 @@
{
"arch": "avr",
"atomic-cas": false,
"cpu": "atmega328p",
"crt-objects-fallback": "false",
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
"eh-frame-header": false,
"exe-suffix": ".elf",
"late-link-args": {
"gnu-cc": [
"-lgcc"
],
"gnu-lld-cc": [
"-lgcc"
]
},
"linker": "avr-gcc",
"linker-flavor": "gnu-cc",
"llvm-target": "avr-unknown-unknown",
"max-atomic-width": 8,
"metadata": {
"description": null,
"host_tools": null,
"std": null,
"tier": null
},
"no-default-libraries": false,
"pre-link-args": {
"gnu-cc": [
"-mmcu=atmega328p",
"-Wl,--as-needed,--print-memory-usage"
],
"gnu-lld-cc": [
"-mmcu=atmega328p",
"-Wl,--as-needed,--print-memory-usage"
]
},
"relocation-model": "static",
"target-c-int-width": "16",
"target-pointer-width": "16"
}

View File

@@ -1,41 +0,0 @@
{
"arch": "avr",
"atomic-cas": false,
"cpu": "atmega32a",
"crt-objects-fallback": "false",
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
"eh-frame-header": false,
"exe-suffix": ".elf",
"late-link-args": {
"gnu-cc": [
"-lgcc"
],
"gnu-lld-cc": [
"-lgcc"
]
},
"linker": "avr-gcc",
"linker-flavor": "gnu-cc",
"llvm-target": "avr-unknown-unknown",
"max-atomic-width": 8,
"metadata": {
"description": null,
"host_tools": null,
"std": null,
"tier": null
},
"no-default-libraries": false,
"pre-link-args": {
"gnu-cc": [
"-mmcu=atmega32a",
"-Wl,--as-needed,--print-memory-usage"
],
"gnu-lld-cc": [
"-mmcu=atmega32a",
"-Wl,--as-needed,--print-memory-usage"
]
},
"relocation-model": "static",
"target-c-int-width": "16",
"target-pointer-width": "16"
}

View File

@@ -1,41 +0,0 @@
{
"arch": "avr",
"atomic-cas": false,
"cpu": "atmega32u4",
"crt-objects-fallback": "false",
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
"eh-frame-header": false,
"exe-suffix": ".elf",
"late-link-args": {
"gnu-cc": [
"-lgcc"
],
"gnu-lld-cc": [
"-lgcc"
]
},
"linker": "avr-gcc",
"linker-flavor": "gnu-cc",
"llvm-target": "avr-unknown-unknown",
"max-atomic-width": 8,
"metadata": {
"description": null,
"host_tools": null,
"std": null,
"tier": null
},
"no-default-libraries": false,
"pre-link-args": {
"gnu-cc": [
"-mmcu=atmega32u4",
"-Wl,--as-needed,--print-memory-usage"
],
"gnu-lld-cc": [
"-mmcu=atmega32u4",
"-Wl,--as-needed,--print-memory-usage"
]
},
"relocation-model": "static",
"target-c-int-width": "16",
"target-pointer-width": "16"
}

View File

@@ -1,41 +0,0 @@
{
"arch": "avr",
"atomic-cas": false,
"cpu": "atmega48p",
"crt-objects-fallback": "false",
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
"eh-frame-header": false,
"exe-suffix": ".elf",
"late-link-args": {
"gnu-cc": [
"-lgcc"
],
"gnu-lld-cc": [
"-lgcc"
]
},
"linker": "avr-gcc",
"linker-flavor": "gnu-cc",
"llvm-target": "avr-unknown-unknown",
"max-atomic-width": 8,
"metadata": {
"description": null,
"host_tools": null,
"std": null,
"tier": null
},
"no-default-libraries": false,
"pre-link-args": {
"gnu-cc": [
"-mmcu=atmega48p",
"-Wl,--as-needed,--print-memory-usage"
],
"gnu-lld-cc": [
"-mmcu=atmega48p",
"-Wl,--as-needed,--print-memory-usage"
]
},
"relocation-model": "static",
"target-c-int-width": "16",
"target-pointer-width": "16"
}

View File

@@ -1,41 +0,0 @@
{
"arch": "avr",
"atomic-cas": false,
"cpu": "atmega8",
"crt-objects-fallback": "false",
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
"eh-frame-header": false,
"exe-suffix": ".elf",
"late-link-args": {
"gnu-cc": [
"-lgcc"
],
"gnu-lld-cc": [
"-lgcc"
]
},
"linker": "avr-gcc",
"linker-flavor": "gnu-cc",
"llvm-target": "avr-unknown-unknown",
"max-atomic-width": 8,
"metadata": {
"description": null,
"host_tools": null,
"std": null,
"tier": null
},
"no-default-libraries": false,
"pre-link-args": {
"gnu-cc": [
"-mmcu=atmega8",
"-Wl,--as-needed,--print-memory-usage"
],
"gnu-lld-cc": [
"-mmcu=atmega8",
"-Wl,--as-needed,--print-memory-usage"
]
},
"relocation-model": "static",
"target-c-int-width": "16",
"target-pointer-width": "16"
}

View File

@@ -1,41 +0,0 @@
{
"arch": "avr",
"atomic-cas": false,
"cpu": "attiny167",
"crt-objects-fallback": "false",
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
"eh-frame-header": false,
"exe-suffix": ".elf",
"late-link-args": {
"gnu-cc": [
"-lgcc"
],
"gnu-lld-cc": [
"-lgcc"
]
},
"linker": "avr-gcc",
"linker-flavor": "gnu-cc",
"llvm-target": "avr-unknown-unknown",
"max-atomic-width": 8,
"metadata": {
"description": null,
"host_tools": null,
"std": null,
"tier": null
},
"no-default-libraries": false,
"pre-link-args": {
"gnu-cc": [
"-mmcu=attiny167",
"-Wl,--as-needed,--print-memory-usage"
],
"gnu-lld-cc": [
"-mmcu=attiny167",
"-Wl,--as-needed,--print-memory-usage"
]
},
"relocation-model": "static",
"target-c-int-width": "16",
"target-pointer-width": "16"
}

View File

@@ -1,41 +0,0 @@
{
"arch": "avr",
"atomic-cas": false,
"cpu": "attiny2313",
"crt-objects-fallback": "false",
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
"eh-frame-header": false,
"exe-suffix": ".elf",
"late-link-args": {
"gnu-cc": [
"-lgcc"
],
"gnu-lld-cc": [
"-lgcc"
]
},
"linker": "avr-gcc",
"linker-flavor": "gnu-cc",
"llvm-target": "avr-unknown-unknown",
"max-atomic-width": 8,
"metadata": {
"description": null,
"host_tools": null,
"std": null,
"tier": null
},
"no-default-libraries": false,
"pre-link-args": {
"gnu-cc": [
"-mmcu=attiny2313",
"-Wl,--as-needed,--print-memory-usage"
],
"gnu-lld-cc": [
"-mmcu=attiny2313",
"-Wl,--as-needed,--print-memory-usage"
]
},
"relocation-model": "static",
"target-c-int-width": "16",
"target-pointer-width": "16"
}

View File

@@ -1,41 +0,0 @@
{
"arch": "avr",
"atomic-cas": false,
"cpu": "attiny85",
"crt-objects-fallback": "false",
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
"eh-frame-header": false,
"exe-suffix": ".elf",
"late-link-args": {
"gnu-cc": [
"-lgcc"
],
"gnu-lld-cc": [
"-lgcc"
]
},
"linker": "avr-gcc",
"linker-flavor": "gnu-cc",
"llvm-target": "avr-unknown-unknown",
"max-atomic-width": 8,
"metadata": {
"description": null,
"host_tools": null,
"std": null,
"tier": null
},
"no-default-libraries": false,
"pre-link-args": {
"gnu-cc": [
"-mmcu=attiny85",
"-Wl,--as-needed,--print-memory-usage"
],
"gnu-lld-cc": [
"-mmcu=attiny85",
"-Wl,--as-needed,--print-memory-usage"
]
},
"relocation-model": "static",
"target-c-int-width": "16",
"target-pointer-width": "16"
}

View File

@@ -1,41 +0,0 @@
{
"arch": "avr",
"atomic-cas": false,
"cpu": "attiny88",
"crt-objects-fallback": "false",
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
"eh-frame-header": false,
"exe-suffix": ".elf",
"late-link-args": {
"gnu-cc": [
"-lgcc"
],
"gnu-lld-cc": [
"-lgcc"
]
},
"linker": "avr-gcc",
"linker-flavor": "gnu-cc",
"llvm-target": "avr-unknown-unknown",
"max-atomic-width": 8,
"metadata": {
"description": null,
"host_tools": null,
"std": null,
"tier": null
},
"no-default-libraries": false,
"pre-link-args": {
"gnu-cc": [
"-mmcu=attiny88",
"-Wl,--as-needed,--print-memory-usage"
],
"gnu-lld-cc": [
"-mmcu=attiny88",
"-Wl,--as-needed,--print-memory-usage"
]
},
"relocation-model": "static",
"target-c-int-width": "16",
"target-pointer-width": "16"
}

View File

@@ -4,7 +4,6 @@ use core::marker::PhantomData;
use arduino_hal::{ use arduino_hal::{
delay_us, delay_us,
pac::EXINT,
port::{ port::{
mode::{Input, PullUp}, mode::{Input, PullUp},
Pin, PinOps, Pin, PinOps,
@@ -15,6 +14,10 @@ use avr_device::asm::delay_cycles;
use static_pins::StaticPinOps; use static_pins::StaticPinOps;
mod structures;
pub use structures::ring_buffer::*;
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);
@@ -42,57 +45,13 @@ where
P: PinOps + StaticPinOps, P: PinOps + StaticPinOps,
{ {
#[inline] #[inline]
pub fn new(_pin: Pin<Input<PullUp>, P>, exint: &EXINT) -> Self { pub fn new(_pin: Pin<Input<PullUp>, P>) -> Self {
exint.eimsk.write(|w| w.bits(1 << 1));
exint.eicra.write(|w| unsafe { w.bits(!(1 << 3 | 1 << 2)) });
Self { Self {
_pin: PhantomData {}, _pin: PhantomData {},
} }
} }
}
pub trait SoftSerial<P> pub fn poll(&self) -> PollResult {
where
P: PinOps + StaticPinOps,
{
fn poll(&self) -> PollResult;
fn response(&self);
fn sync_reciever(&self);
fn sync_transmitter(&self);
fn write_byte(&self, data: u8);
fn read_byte(&self) -> ReadByteResult;
#[inline]
fn finish_write(&self) {
P::into_pull_up_input();
while P::is_low() {}
}
fn write_bytes(&self, transmit_data: &[u8]) {
for byte in transmit_data {
self.write_byte(*byte);
self.sync_transmitter();
}
}
fn read_bytes(&self, recieve_buffer: &mut [u8]) {
for byte in recieve_buffer {
if let Ok(data) = self.read_byte() {
*byte = data;
}
self.sync_reciever();
}
}
}
impl<P> SoftSerial<P> for HalfDuplexSerial<P>
where
P: PinOps + StaticPinOps,
{
fn poll(&self) -> PollResult {
P::into_output(); P::into_output();
delay_cycles(1); delay_cycles(1);
P::into_pull_up_input(); P::into_pull_up_input();
@@ -114,7 +73,7 @@ where
PollResult::Ok(()) PollResult::Ok(())
} }
fn response(&self) { pub fn response(&self) {
P::into_output_high(); P::into_output_high();
delay_us(FIRST_HALF_SERIAL_DELAY); delay_us(FIRST_HALF_SERIAL_DELAY);
@@ -127,7 +86,7 @@ where
} }
#[inline(never)] #[inline(never)]
fn sync_transmitter(&self) { pub fn sync_transmitter(&self) {
P::into_output(); P::into_output();
delay_us(SERIAL_DELAY); delay_us(SERIAL_DELAY);
@@ -136,15 +95,39 @@ where
} }
#[inline(never)] #[inline(never)]
fn sync_reciever(&self) { pub fn sync_reciever(&self) {
while P::is_high() {} while P::is_high() {}
while P::is_low() {} while P::is_low() {}
} }
#[inline]
pub fn reset(&self) {
P::into_pull_up_input();
}
}
pub trait SoftSerialWriter<P, T>: SoftSerialByteWriter<P>
where
P: PinOps + StaticPinOps,
{
fn write_bytes(&self, transmit_data: T);
}
pub trait SoftSerialReader<P, T>: SoftSerialByteReader<P>
where
P: PinOps + StaticPinOps,
{
fn read_bytes(&self, recieve_data: T);
}
pub trait SoftSerialByteWriter<P>
where
P: PinOps + StaticPinOps,
{
#[inline(never)] #[inline(never)]
fn write_byte(&self, data: u8) { fn write_byte(&self, transmit_data: u8) {
let (mut data, mut parity_bit) = (data, 0); let (mut data, mut parity_bit) = (transmit_data, 0);
for _ in 0..8 { for _ in 0..8 {
if data & MSB == 0 { if data & MSB == 0 {
@@ -170,7 +153,12 @@ where
delay_us(SERIAL_DELAY); delay_us(SERIAL_DELAY);
} }
}
pub trait SoftSerialByteReader<P>
where
P: PinOps + StaticPinOps,
{
#[inline(never)] #[inline(never)]
fn read_byte(&self) -> ReadByteResult { fn read_byte(&self) -> ReadByteResult {
let (mut data, mut reciever_parity_bit) = (0, 0); let (mut data, mut reciever_parity_bit) = (0, 0);
@@ -179,6 +167,8 @@ where
for _ in 0..8 { for _ in 0..8 {
delay_us(FIRST_HALF_SERIAL_DELAY); delay_us(FIRST_HALF_SERIAL_DELAY);
data <<= 1;
if P::is_low() { if P::is_low() {
data |= 1; data |= 1;
reciever_parity_bit ^= 1; reciever_parity_bit ^= 1;
@@ -189,8 +179,6 @@ where
delay_cycles(READING_ADJUST); delay_cycles(READING_ADJUST);
delay_us(SECOND_HALF_SERIAL_DELAY); delay_us(SECOND_HALF_SERIAL_DELAY);
data <<= 1;
} }
delay_us(FIRST_HALF_SERIAL_DELAY); delay_us(FIRST_HALF_SERIAL_DELAY);
@@ -200,10 +188,39 @@ where
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 { if reciever_parity_bit == transmitter_parity_bit {
return Err((data, reciever_parity_bit)); return Err((data, reciever_parity_bit));
} }
Ok(data) 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,
{
fn write_bytes(&self, transmit_data: &[u8]) {
for byte in transmit_data {
self.write_byte(*byte);
self.sync_transmitter();
}
}
}
impl<P> SoftSerialReader<P, &mut [u8]> for HalfDuplexSerial<P>
where
P: PinOps + StaticPinOps,
{
fn read_bytes(&self, recieve_data: &mut [u8]) {
for byte in recieve_data {
if let Ok(data) = self.read_byte() {
*byte = data;
}
self.sync_reciever();
}
}
}

1
src/structures/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod ring_buffer;

View File

@@ -0,0 +1,145 @@
// 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, SoftSerialByteReader, SoftSerialByteWriter, 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> 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>
where
P: PinOps + StaticPinOps,
{
#[inline(never)]
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);
}
}
}
impl<const N: usize, P> SoftSerialReader<P, &mut RingBuffer<N>> for HalfDuplexSerial<P>
where
P: PinOps + StaticPinOps,
{
#[inline(never)]
fn read_bytes(&self, recieve_data: &mut RingBuffer<N>) {
let byte = self.read_byte();
if let Ok(len) = byte {
for _ in 0..len {
self.sync_reciever();
if let Ok(byte) = self.read_byte() {
recieve_data.push(byte).unwrap_or(());
}
}
}
}
}