Skip to content
This repository was archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
switch from generic duration to defined type (#98)
Browse files Browse the repository at this point in the history
* switch to duration type from time.rs

* fixes for duration type

* export streaming_iter trait for now

* bumped versions of cortex crates

* unused marked

* clean up

* removed streaming iter dep

* cleanup

* add deny warnings

* removed generic duration

* add deny warnings

* fixed clippy errors

* added default constructor
  • Loading branch information
teamplayer3 authored Dec 20, 2021
1 parent 5d37215 commit aaeefc9
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 117 deletions.
1 change: 0 additions & 1 deletion examples/basic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ embedded-time = "0.12.0"

# TODO: if new embedded-hal version releases, this can be changed to crates.io
embedded-hal = {version = "0.2.6", git = "https://github.com/rust-embedded/embedded-hal/", branch = "v0.2.x"}
streaming-iterator = "0.1.5"

[dependencies.uavcan]
path = "../../uavcan"
Expand Down
26 changes: 16 additions & 10 deletions examples/basic/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
#![deny(warnings)]

use embedded_hal::can::ExtendedId;
use embedded_time::duration::Milliseconds;
use embedded_time::Clock;
use streaming_iterator::StreamingIterator;
use uavcan::session::StdVecSessionManager;
use uavcan::time::StdClock;
use uavcan::transport::can::{Can, CanFrame as UavcanFrame, CanMetadata};
use uavcan::{transfer::Transfer, types::TransferId, Node, Priority, Subscription, TransferKind};
use uavcan::{
session::StdVecSessionManager,
time::StdClock,
transfer::Transfer,
transport::can::{Can, CanFrame as UavcanFrame, CanMetadata},
types::TransferId,
Node, Priority, StreamingIterator, Subscription, TransferKind,
};

use arrayvec::ArrayVec;
use socketcan::{CANFrame, CANSocket};

fn main() {
let clock = StdClock::new();
let mut session_manager = StdVecSessionManager::<CanMetadata, Milliseconds, StdClock>::new();
let mut session_manager = StdVecSessionManager::<CanMetadata, StdClock>::new();
session_manager
.subscribe(Subscription::new(
TransferKind::Message,
Expand Down Expand Up @@ -71,7 +75,7 @@ fn main() {
for byte in xfer.payload {
print!("0x{:02x} ", byte);
}
println!("");
println!();
}
TransferKind::Request => {
println!("Request Received!");
Expand Down Expand Up @@ -107,8 +111,10 @@ fn main() {

let mut frame_iter = node.transmit(&transfer).unwrap();
while let Some(frame) = frame_iter.next() {
sock.write_frame(&CANFrame::new(frame.id, &frame.payload, false, false).unwrap())
.unwrap();
sock.write_frame(
&CANFrame::new(frame.id.as_raw(), &frame.payload, false, false).unwrap(),
)
.unwrap();

//print!("Can frame {}: ", i);
//for byte in &frame.payload {
Expand Down
4 changes: 2 additions & 2 deletions examples/embedded/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ edition = "2021"

[dependencies]

cortex-m = "0.6.0"
cortex-m-rt = "0.6.10"
cortex-m = "0.7.1"
cortex-m-rt = "0.7.1"
stm32g4xx-hal = {version = "0.0.0", git = "https://github.com/stm32-rs/stm32g4xx-hal", features = ["rt", "stm32g431"] }

alloc-cortex-m = "0.4.1"
Expand Down
6 changes: 4 additions & 2 deletions examples/embedded/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ extern crate alloc;
pub struct MyAllocator(Mutex<RefCell<Tlsf<'static, u8, u8, 8, 8>>>);

impl MyAllocator {
pub const INIT: MyAllocator = MyAllocator(Mutex::new(RefCell::new(Tlsf::INIT)));
pub const fn init() -> Self {
Self(Mutex::new(RefCell::new(Tlsf::INIT)))
}

pub unsafe fn init(&self, pool: *mut u8, len: usize) -> usize {
pub unsafe fn set_pool(&self, pool: *mut u8, len: usize) -> usize {
cortex_m::interrupt::free(|cs| {
let mut tlsf = self.0.borrow(cs).borrow_mut();

Expand Down
1 change: 1 addition & 0 deletions examples/embedded/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ defmt::timestamp!("{=usize}", {
});

/// Terminates the application and makes `probe-run` exit with exit-code = 0
#[allow(unused)]
pub fn exit() -> ! {
loop {
cortex_m::asm::bkpt();
Expand Down
33 changes: 12 additions & 21 deletions examples/embedded/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
#![no_main]
#![no_std]
#![deny(warnings)]
// to use the global allocator
#![feature(alloc_error_handler)]

#[cfg(test)]
#[macro_use]
extern crate std;

#[cfg(feature = "logging")]
mod logging;
#[cfg(feature = "logging")]
use defmt::info;

mod allocator;
mod clock;
mod logging;
mod util;


#[cfg(not(feature = "logging"))]
use panic_halt as _;

use core::{
alloc::Layout,
mem::MaybeUninit,
Expand All @@ -29,10 +22,9 @@ use core::{
use allocator::MyAllocator;
use clock::StmClock;

use cortex_m as _;
use cortex_m_rt::entry;

use embedded_time::{duration::Milliseconds, Clock};
use embedded_time::Clock;
use hal::{
delay::SYSTDelayExt,
fdcan::{
Expand All @@ -48,30 +40,29 @@ use hal::{
prelude::*,
rcc::{Config, PLLSrc, PllConfig, Rcc, RccExt, SysClockSrc},
stm32::{Peripherals, FDCAN1},
timer::MonoTimer,
};
use stm32g4xx_hal as hal;

use uavcan::{
session::HeapSessionManager,
transfer::Transfer,
transport::can::{Can, CanMetadata},
Node, Priority, Subscription, TransferKind,
Node, Priority, StreamingIterator, Subscription, TransferKind,
};

use util::insert_u8_array_in_u32_array;

static mut POOL: MaybeUninit<[u8; 1024]> = MaybeUninit::uninit();

#[global_allocator]
static ALLOCATOR: MyAllocator = MyAllocator::INIT;
static ALLOCATOR: MyAllocator = MyAllocator::init();

#[entry]
fn main() -> ! {
// init heap
let cursor = unsafe { POOL.as_mut_ptr() } as *mut u8;
let size = 1024;
unsafe { ALLOCATOR.init(cursor, size) };
unsafe { ALLOCATOR.set_pool(cursor, size) };

// define peripherals of the board
let dp = Peripherals::take().unwrap();
Expand Down Expand Up @@ -119,12 +110,10 @@ fn main() -> ! {
can.into_normal()
};

let measure_clock = MonoTimer::new(cp.DWT, cp.DCB, &rcc.clocks);

// init clock
let clock = StmClock::new(dp.TIM7, &rcc.clocks);

let mut session_manager = HeapSessionManager::<CanMetadata, Milliseconds, StmClock>::new();
let mut session_manager = HeapSessionManager::<CanMetadata, StmClock>::new();
session_manager
.subscribe(Subscription::new(
TransferKind::Message,
Expand Down Expand Up @@ -175,15 +164,16 @@ fn main() -> ! {
}

pub fn publish(
node: &mut Node<HeapSessionManager<CanMetadata, Milliseconds<u32>, StmClock>, Can, StmClock>,
node: &mut Node<HeapSessionManager<CanMetadata, StmClock>, Can, StmClock>,
transfer: Transfer<StmClock>,
can: &mut FdCan<FDCAN1, NormalOperationMode>,
) {
for frame in node.transmit(&transfer).unwrap() {
let mut iter = node.transmit(&transfer).unwrap();
while let Some(frame) = iter.next() {
let header = TxFrameHeader {
bit_rate_switching: false,
frame_format: hal::fdcan::frame::FrameFormat::Standard,
id: Id::Extended(ExtendedId::new(frame.id).unwrap()),
id: Id::Extended(ExtendedId::new(frame.id.as_raw()).unwrap()),
len: frame.payload.len() as u8,
marker: None,
};
Expand All @@ -210,6 +200,7 @@ fn config_rcc(rcc: Rcc) -> Rcc {
)
}

#[allow(clippy::empty_loop)]
#[alloc_error_handler]
fn oom(_: Layout) -> ! {
loop {}
Expand Down
16 changes: 8 additions & 8 deletions examples/embedded/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
pub fn insert_u8_array_in_u32_array(u8_array: &[u8], u32_array: &mut [u32]) {
let u32_final_len = (u8_array.len() + 3) / 4;
// only iterate over n - 1
for i in 0..u32_final_len - 1 {
let index = i * 4;
for (index, item) in u32_array.iter_mut().enumerate().take(u32_final_len - 1) {
let index = index * 4;
let val = slice_into_u32(&u8_array[index..index + 4]);
u32_array[i] = val;
*item = val;
}
let remaining = match u8_array.len() % 4 {
0 => 4,
Expand All @@ -25,16 +25,16 @@ pub fn insert_u8_array_in_u32_array(u8_array: &[u8], u32_array: &mut [u32]) {
fn slice_into_u32(slice: &[u8]) -> u32 {
// TODO nicer algorithm
match slice.len() {
1 => return (slice[0] as u32) << 24,
2 => return (slice[0] as u32) << 24 | (slice[1] as u32) << 16,
3 => return (slice[0] as u32) << 24 | (slice[1] as u32) << 16 | (slice[2] as u32) << 8,
1 => (slice[0] as u32) << 24,
2 => (slice[0] as u32) << 24 | (slice[1] as u32) << 16,
3 => (slice[0] as u32) << 24 | (slice[1] as u32) << 16 | (slice[2] as u32) << 8,
4 => {
return (slice[0] as u32) << 24
(slice[0] as u32) << 24
| (slice[1] as u32) << 16
| (slice[2] as u32) << 8
| (slice[3] as u32)
}
_ => return 0,
_ => 0,
}
}

Expand Down
9 changes: 3 additions & 6 deletions uavcan/src/crc16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Crc16 {
/// Process the current crc sum further with the supplied data.
pub fn digest<T: ?Sized + AsRef<[u8]>>(&mut self, data: &T) {
for n in data.as_ref().iter().copied() {
let index = ((self.0 >> u16::from(Self::BITS - 8)) as u8 ^ n) as usize;
let index = ((self.0 >> (Self::BITS - 8)) as u8 ^ n) as usize;
self.0 = (self.0 << 8) ^ NO_REF_16_1021[index];
}
}
Expand All @@ -62,15 +62,12 @@ impl Crc16 {
pub fn get_crc(&self) -> u16 {
let final_xor = 0x0000;

let sum = (self.0 ^ final_xor) & Crc16::mask();

sum
(self.0 ^ final_xor) & Crc16::mask()
}

const fn mask() -> u16 {
let high_bit = 1 << (Self::BITS - 1);
let mask = ((high_bit - 1) << 1) | 1;
mask
((high_bit - 1) << 1) | 1
}
}

Expand Down
20 changes: 14 additions & 6 deletions uavcan/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//! this. I can see issues with this running into issues in multi-threaded
//! environments, but I'll get to those when I get to them.
#![no_std]
#![deny(warnings)]
#![feature(generic_associated_types)]
#![feature(test)]

Expand All @@ -45,10 +46,12 @@ pub mod transfer;
pub mod transport;
pub mod types;

use embedded_time::fixed_point::FixedPoint;
pub use node::Node;
use time::Duration;
pub use transfer::TransferKind;

pub use streaming_iterator::StreamingIterator;

mod internal;
mod node;
pub mod session;
Expand Down Expand Up @@ -107,15 +110,20 @@ pub enum Priority {
}

/// Simple subscription type to
pub struct Subscription<D: embedded_time::duration::Duration + FixedPoint> {
pub struct Subscription {
transfer_kind: TransferKind,
port_id: PortId,
extent: usize,
timeout: D,
timeout: Duration,
}

impl<D: embedded_time::duration::Duration + FixedPoint> Subscription<D> {
pub fn new(transfer_kind: TransferKind, port_id: PortId, extent: usize, timeout: D) -> Self {
impl Subscription {
pub fn new(
transfer_kind: TransferKind,
port_id: PortId,
extent: usize,
timeout: Duration,
) -> Self {
Self {
transfer_kind,
port_id,
Expand All @@ -125,7 +133,7 @@ impl<D: embedded_time::duration::Duration + FixedPoint> Subscription<D> {
}
}

impl<D: embedded_time::duration::Duration + FixedPoint> PartialEq for Subscription<D> {
impl PartialEq for Subscription {
fn eq(&self, other: &Self) -> bool {
self.transfer_kind == other.transfer_kind && self.port_id == other.port_id
}
Expand Down
Loading

0 comments on commit aaeefc9

Please sign in to comment.