NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io


So to summarize, I am trying to understand how to structure embedded code, which uses a HAL crate. For example https://github.com/rp-rs/rp-hal/blob/main/rp2040-hal/examples/spi.rs shows initialization of spi, and writing to it. Usually minimum initialization is sth like:

- singleton `pac::Peripherals::take()`
- `hal::clocks::init_clocks_and_plls(pac.clock1, pac.clock2, ...)`
- setup of `hal::gpio::Pins`
- setting correct mode for pins for spi function
- creating spi "object" `hal::Spi::<_, _, 8>::new(pac.SPI0)`
- and finally initializing `spi.init(pac.clock1, mode)`

Then one could write to it with `let x = spi.send(0xff)`;

However, if one wanted to have a couple of different items, i guess one would need to split this into rust modules.

I would then want to have something resembling:

`src/main.rs`

```
#![no_std]
use rp2040_hal;
mod disp;
mod flash;
fn main() -> ! {
let p = rp2040_hal::pac::Peripherals::take();
(...) // all the other init from above, clocks ect

// idea A
let d = MyDisp_or_Flash::new(spi<generic_specialized_paramA, generic_specialized_paramB>); // but this is a bit difficult
let e = MyDisp_or_Flash::new(different_spi)
e.write(0x55);
d.write(0x55);
// idea B?
}
```

and

`src/disp/mod.rs`

```
pub struct MyDisp_or_Flash {
// either spi: Spi<?, ?, ?> // or sth else
// or display:
}
impl MyDisp_or_Flash {
pub fn new(

/* spi: Spi<?, ?, ?> or passing &pins/&clocks/&peripherals stucts*/

) -> Self {
// initialize spi, then "keep it" inside the class
// so it wouldn't be destroyed after new() returns

// in a second module this spi is used to initialize mpidsi object
// let di = display_interface_spi::SPIInterface::new(spi, 1, 2);
// where 1, 2 are pins - so not important in this example
let mut delay = embedded_hal::blocking::delay::new(10);
let mut display = Builder::ili9486(di).init(&mut delay, Some(reset));

// what should i return to be able to use display and/or spi
// for example calling from parent modulename.write([1,2,3]),
// how could I contain them within this module/struct?

}

pub fn write(&self, data: u8)
{
self.spi.write(data);

// or in case of display we could want to clear it

self.display.clear(Rgb666::BLACK)?;
}

}
```
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.