-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made it possible to share the ADC (#49)
* feat: added heapless as dependency and example added the heapless crate as a dependency for allocating and sharing the adc between multiple structs, and also created an example on how this can be done. * feat: enable adc sharing in the bsc now the adc is allocated in the bsc inside of an arc, which allows it to be cloned and shared between multiple structs, so that both the light sensor array and the future battery sensor will be able to use it. * fix: fixed the no_bsc_arc example fixed the example for the usage of `Arc`, the problem was that we didn't create the memory block for the allocation before doing it, so the program had a runtime error * fix: fixed arc allocation in the bsc same fix as in the arc example, the memory block for the adc wasn't being defined and so, the alloc function failed, solved that. * fix: add the allow static mut refs flag in rust 2024 this will be disallowed due to causing undefined behaviour, in this case we're using rust 2021 and also the `heapless` crate does it this way, so i added a flag to prevent the compiler from giving an error/warning
- Loading branch information
Showing
4 changed files
with
126 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//! This is an example on how to use the `Arc` type from the `heapless` crate to allocate something | ||
//! in the memory and then create pointers to that location in order to use it in a memory | ||
//! safe way (except for the part in which we define the block of memory, that requires using unsafe) | ||
#![no_std] | ||
#![cfg_attr(not(doc), no_main)] | ||
#![allow(static_mut_refs)] | ||
use core::cell::RefCell; | ||
|
||
use stm32f1xx_hal::{ | ||
pac::ADC1, | ||
adc::Adc, | ||
}; | ||
|
||
use panic_halt as _; | ||
|
||
use mightybuga_bsc as board; | ||
|
||
use cortex_m_rt::entry; | ||
use board::hal::{pac, prelude::*}; | ||
|
||
use heapless::{ | ||
arc_pool, | ||
pool::arc::ArcBlock, | ||
}; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
// Get access to the core peripherals from the cortex-m crate | ||
let _cp = cortex_m::Peripherals::take().unwrap(); | ||
// Get access to the device specific peripherals from the peripheral access crate | ||
let dp = pac::Peripherals::take().unwrap(); | ||
|
||
// Take ownership over the raw flash and rcc devices and convert them into the corresponding | ||
// HAL structs | ||
let mut flash = dp.FLASH.constrain(); | ||
let rcc = dp.RCC.constrain(); | ||
|
||
// Freeze the configuration of all the clocks in the system and store the frozen frequencies in | ||
// `clocks` | ||
let clocks = rcc.cfgr.freeze(&mut flash.acr); | ||
|
||
// Acquire the GPIOC peripheral | ||
let _gpioc = dp.GPIOC.split(); | ||
let mut gpioa = dp.GPIOA.split(); | ||
|
||
// This macro generates all the necessary code implementations to allocate a refcell containing | ||
// the adc1 into a block in the memory | ||
arc_pool!(P: RefCell<Adc<ADC1>>); | ||
|
||
// Generate the memory block in which the refcell<adc1> will be allocated, we need to do | ||
// this in an unsafe way | ||
let arc_block: &'static mut ArcBlock<RefCell<Adc<ADC1>>> = unsafe { | ||
static mut B: ArcBlock<RefCell<Adc<ADC1>>> = ArcBlock::new(); | ||
&mut B | ||
}; | ||
|
||
// Tell the singleton to use this block in the memory for managing the refcell<adc1> | ||
P.manage(arc_block); | ||
|
||
// Allocate the adc1 inside of a refcell in the memory block we created earlier, the `alloc` | ||
// method doesn't have an unwrap for its Result, so we use a simple match statement to do that. | ||
let arc = match P.alloc(RefCell::new(Adc::adc1(dp.ADC1, clocks))) { | ||
Ok(adc_arc) => adc_arc, | ||
Err(_) => { | ||
panic!("Couldn't get the Arc"); | ||
}, | ||
}; | ||
|
||
// Clone the pointer to the refcell<adc1> | ||
let arc2 = arc.clone(); | ||
|
||
// Use any of those pointers to read using the adc | ||
let _x: u16 = arc2.borrow_mut().read(&mut gpioa.pa0.into_analog(&mut gpioa.crl)).unwrap(); | ||
let _y: u16 = arc.borrow_mut().read(&mut gpioa.pa2.into_analog(&mut gpioa.crl)).unwrap(); | ||
|
||
// Drop the pointers and the original instance of the refcell<adc1> | ||
drop(arc2); | ||
drop(arc); | ||
|
||
loop { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters