-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved driver to own module and finished it.
Added leds task. Started on measure task.
- Loading branch information
1 parent
f40a14b
commit b10c2a4
Showing
7 changed files
with
188 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,67 @@ | ||
use core::pin::pin; | ||
|
||
use embassy_futures::select::{select3, Either3}; | ||
use embassy_stm32::{ | ||
peripherals::TIM1, | ||
time::Hertz, | ||
timer::{complementary_pwm::ComplementaryPwm, Channel}, | ||
}; | ||
use futures::StreamExt; | ||
|
||
use crate::modbus; | ||
|
||
#[embassy_executor::task] | ||
pub async fn driver(mut driver_pwm: ComplementaryPwm<'static, TIM1>) -> ! { | ||
let mut enable_listener = pin!(modbus::ENABLE.get_listener().await); | ||
let mut frequency_listener = pin!(modbus::DRIVE_FREQUENCY.get_listener().await); | ||
let mut dutycycle_listener = pin!(modbus::COIL_POWER_DUTYCYCLE.get_listener().await); | ||
|
||
// We want the timer to be able to trigger the ADC. | ||
// For that we set the MMS2 to `0101: Compare - OC2REFC signal is used as trigger output (TRGO2)` | ||
// The adc can then trigger on the TRGO2 signal | ||
unsafe { &*stm32g0::stm32g030::TIM1::PTR } | ||
.cr2 | ||
.modify(|_, w| w.mms2().variant(0b0101)); | ||
|
||
driver_pwm.set_dead_time(0); | ||
|
||
modbus::ENABLE.write(false).await; | ||
modbus::DRIVE_FREQUENCY.write(40_000).await; | ||
modbus::COIL_POWER_DUTYCYCLE.write(1.0).await; | ||
|
||
loop { | ||
let result = select3( | ||
enable_listener.next(), | ||
frequency_listener.next(), | ||
dutycycle_listener.next(), | ||
) | ||
.await; | ||
|
||
match result { | ||
Either3::First(Some(enable)) => { | ||
if enable { | ||
driver_pwm.enable(Channel::Ch2); | ||
} else { | ||
driver_pwm.disable(Channel::Ch2); | ||
} | ||
|
||
modbus::LED_GREEN.write(enable).await; | ||
} | ||
Either3::Second(Some(frequency)) => { | ||
driver_pwm.set_freq(Hertz(frequency)); | ||
let dutycycle = modbus::COIL_POWER_DUTYCYCLE.read().await; | ||
driver_pwm.set_duty( | ||
Channel::Ch2, | ||
(driver_pwm.get_max_duty() as f32 * dutycycle.clamp(0.0, 1.0)) as u16, | ||
); | ||
} | ||
Either3::Third(Some(dutycycle)) => { | ||
driver_pwm.set_duty( | ||
Channel::Ch2, | ||
(driver_pwm.get_max_duty() as f32 * dutycycle.clamp(0.0, 1.0)) as u16, | ||
); | ||
} | ||
_ => defmt::unreachable!(), | ||
} | ||
} | ||
} |
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,31 @@ | ||
use core::pin::pin; | ||
|
||
use crate::modbus; | ||
use embassy_futures::select::{select, Either}; | ||
use embassy_stm32::{ | ||
gpio::OutputOpenDrain, | ||
peripherals::{PA1, PA2}, | ||
}; | ||
use futures::StreamExt; | ||
|
||
#[embassy_executor::task] | ||
pub async fn leds( | ||
mut led_g: OutputOpenDrain<'static, PA2>, | ||
mut led_r: OutputOpenDrain<'static, PA1>, | ||
) -> ! { | ||
let mut led_g_listener = pin!(modbus::LED_GREEN.get_listener().await); | ||
let mut led_r_listener = pin!(modbus::LED_RED.get_listener().await); | ||
|
||
modbus::LED_GREEN.write(false).await; | ||
modbus::LED_RED.write(false).await; | ||
|
||
loop { | ||
let result = select(led_g_listener.next(), led_r_listener.next()).await; | ||
|
||
match result { | ||
Either::First(Some(value)) => led_g.set_level(value.into()), | ||
Either::Second(Some(value)) => led_r.set_level(value.into()), | ||
_ => defmt::unreachable!(), | ||
} | ||
} | ||
} |
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,48 @@ | ||
use embassy_stm32::{dma::TransferOptions, pac::ADC1, peripherals}; | ||
use embassy_time::Duration; | ||
|
||
use crate::modbus; | ||
|
||
#[embassy_executor::task] | ||
pub async fn measure( | ||
_measure_adc: peripherals::ADC1, | ||
_measure_pin: peripherals::PA0, | ||
mut measure_dma: peripherals::DMA1_CH3, | ||
) -> ! { | ||
// What we need from the ADC is not implemented in embassy, so we'll have to control the registers ourselves. | ||
// We take the peripherals nonetheless so we know we've got exclusive access | ||
|
||
modbus::COIL_MEASURE_FREQUENCY.write(10).await; | ||
|
||
let mut adc_sample_buffer = [0u16; 64]; | ||
|
||
// TODO: Configure the ADC and the pin | ||
|
||
loop { | ||
embassy_time::Timer::after(Duration::from_hz( | ||
modbus::COIL_MEASURE_FREQUENCY.read().await.clamp(1, 1000) as u64, | ||
)) | ||
.await; | ||
|
||
if !modbus::ENABLE.read().await { | ||
continue; | ||
} | ||
|
||
// TODO: Make the ADC start on the next TRGO2 (either rising edge or falling edge, needs figuring out) | ||
|
||
let dma_transfer = unsafe { | ||
embassy_stm32::dma::Transfer::new_read::<u16>( | ||
&mut measure_dma, | ||
5, // ADC request | ||
ADC1.dr().as_ptr().cast(), // The result register of the adc | ||
&mut adc_sample_buffer, | ||
TransferOptions::default(), | ||
) | ||
}; | ||
|
||
dma_transfer.await; | ||
|
||
// TODO: Do ADC cleanup | ||
// TODO: Take the samples and do the calculations | ||
} | ||
} |
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