Compare commits
3 Commits
46837a68e4
...
6991e5611e
Author | SHA1 | Date | |
---|---|---|---|
|
6991e5611e | ||
|
caa1ff9f24 | ||
|
6defd8963f |
97
src/lib.rs
97
src/lib.rs
|
@ -46,49 +46,8 @@ where
|
||||||
_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();
|
||||||
|
@ -110,7 +69,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);
|
||||||
|
@ -123,21 +82,32 @@ 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 / 2);
|
||||||
|
|
||||||
P::set_high();
|
P::set_high();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[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();
|
||||||
|
while P::is_low() {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait SoftSerialWriter<P, T>
|
||||||
|
where
|
||||||
|
P: PinOps + StaticPinOps,
|
||||||
|
{
|
||||||
#[inline(never)]
|
#[inline(never)]
|
||||||
fn write_byte(&self, data: u8) {
|
fn write_byte(&self, data: u8) {
|
||||||
let (mut data, mut parity_bit) = (data, 0);
|
let (mut data, mut parity_bit) = (data, 0);
|
||||||
|
@ -167,6 +137,25 @@ where
|
||||||
delay_us(SERIAL_DELAY);
|
delay_us(SERIAL_DELAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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>
|
||||||
|
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);
|
||||||
|
@ -202,4 +191,20 @@ where
|
||||||
|
|
||||||
Ok(data)
|
Ok(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn read_bytes(&self, recieve_data: T);
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue