Working on device brightness control
Signed-off-by: fly <merspieler@alwaysdata.com>
This commit is contained in:
parent
bd216e1999
commit
d62dd32360
3 changed files with 95 additions and 136 deletions
|
@ -1,90 +0,0 @@
|
|||
#![deny(unsafe_code)]
|
||||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
use panic_halt as _;
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
use stm32f1xx_hal::{adc, pac, prelude::*, timer::{Channel, Tim2NoRemap}};
|
||||
|
||||
use stm32f1xx_hal::usb::{Peripheral, UsbBus};
|
||||
use usb_device::prelude::*;
|
||||
use usbd_serial::{SerialPort, USB_CLASS_CDC};
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
// ====================== general setup =================
|
||||
// Acquire peripherals
|
||||
let p = pac::Peripherals::take().unwrap();
|
||||
let mut flash = p.FLASH.constrain();
|
||||
let rcc = p.RCC.constrain();
|
||||
|
||||
// Setup GPIOA
|
||||
let mut gpioa = p.GPIOA.split();
|
||||
|
||||
// configure clock
|
||||
let clocks = rcc
|
||||
.cfgr
|
||||
.use_hse(16.MHz())
|
||||
.sysclk(48.MHz())
|
||||
.pclk1(24.MHz())
|
||||
.freeze(&mut flash.acr);
|
||||
|
||||
// ====================== ADC setup =================
|
||||
// Configure ADC clocks
|
||||
// Default value is the slowest possible ADC clock: PCLK2 / 8. Meanwhile ADC
|
||||
// clock is configurable. So its frequency may be tweaked to meet certain
|
||||
// practical needs. User specified value is be approximated using supported
|
||||
// prescaler values 2/4/6/8.
|
||||
// let clocks = rcc.cfgr.adcclk(2.MHz()).freeze(&mut flash.acr);
|
||||
|
||||
// Setup ADC
|
||||
let mut adc1 = adc::Adc::adc1(p.ADC1, clocks);
|
||||
|
||||
// Configure pa1 as an analog input
|
||||
let mut ch0 = gpioa.pa1.into_analog(&mut gpioa.crl);
|
||||
|
||||
// ====================== PWM setup =================
|
||||
let mut afio = p.AFIO.constrain();
|
||||
let c1 = gpioa.pa0.into_alternate_push_pull(&mut gpioa.crl);
|
||||
let mut pwm = p.TIM2.pwm_hz::<Tim2NoRemap, _, _>(c1, &mut afio.mapr, 1.kHz(), &clocks);
|
||||
pwm.enable(Channel::C1);
|
||||
let pwm_max = pwm.get_max_duty() as f32;
|
||||
|
||||
// ====================== USB setup =================
|
||||
assert!(clocks.usbclk_valid());
|
||||
let mut usb_dp = gpioa.pa12.into_push_pull_output(&mut gpioa.crh);
|
||||
let usb = Peripheral {
|
||||
usb: p.USB,
|
||||
pin_dm: gpioa.pa11,
|
||||
pin_dp: usb_dp.into_floating_input(&mut gpioa.crh),
|
||||
};
|
||||
let usb_bus = UsbBus::new(usb);
|
||||
|
||||
let mut serial = SerialPort::new(&usb_bus);
|
||||
|
||||
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
|
||||
.manufacturer("FLC Meow!")
|
||||
.product("Serial port")
|
||||
.serial_number("01189998819991197253")
|
||||
.device_class(USB_CLASS_CDC)
|
||||
.build();
|
||||
|
||||
loop {
|
||||
let data: u16 = adc1.read(&mut ch0).unwrap();
|
||||
pwm.set_duty(Channel::C1, (pwm_max * ((0xffff as f32) / (data as f32))) as u16);
|
||||
|
||||
let mut buf = ryu::Buffer::new();
|
||||
let ret = buf.format((0xffff as f32) / (data as f32)).as_bytes();
|
||||
|
||||
let mut write_offset = 0;
|
||||
while write_offset < 8 {
|
||||
match serial.write(&ret[write_offset..8]) {
|
||||
Ok(len) if len > 0 => {
|
||||
write_offset += len;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
#![deny(unsafe_code)]
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use panic_halt as _;
|
||||
|
||||
use nb::block;
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
use stm32f1xx_hal::{pac, prelude::*, timer::Timer};
|
||||
|
||||
#[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 mut gpioa = dp.GPIOA.split();
|
||||
|
||||
// Configure gpio C pin 13 as a push-pull output. The `crh` register is passed to the function
|
||||
// in order to configure the port. For pins 0-7, crl should be passed instead.
|
||||
let mut led = gpioa.pa0.into_push_pull_output(&mut gpioa.crl);
|
||||
// Configure the syst timer to trigger an update every second
|
||||
let mut timer = Timer::syst(cp.SYST, &clocks).counter_hz();
|
||||
timer.start(1.Hz()).unwrap();
|
||||
|
||||
// Wait for the timer to trigger an update and change the state of the LED
|
||||
loop {
|
||||
block!(timer.wait()).unwrap();
|
||||
led.set_high();
|
||||
block!(timer.wait()).unwrap();
|
||||
led.set_low();
|
||||
}
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
/home/fly/hw-stuff/112VU/firmware/src/brightness.rs
|
95
firmware/src/main.rs
Normal file
95
firmware/src/main.rs
Normal file
|
@ -0,0 +1,95 @@
|
|||
#![deny(unsafe_code)]
|
||||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
use panic_halt as _;
|
||||
|
||||
use nb::block;
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
use stm32f1xx_hal::{adc, pac, prelude::*, timer::{Channel, Tim2NoRemap}};
|
||||
|
||||
use stm32f1xx_hal::usb::{Peripheral, UsbBus};
|
||||
use usb_device::prelude::*;
|
||||
use usbd_serial::{SerialPort, USB_CLASS_CDC};
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
// ====================== general setup =================
|
||||
// Acquire peripherals
|
||||
let cp = cortex_m::Peripherals::take().unwrap();
|
||||
let p = pac::Peripherals::take().unwrap();
|
||||
let mut flash = p.FLASH.constrain();
|
||||
let rcc = p.RCC.constrain();
|
||||
|
||||
// Setup GPIOA
|
||||
let mut gpioa = p.GPIOA.split();
|
||||
|
||||
// configure clock
|
||||
let clocks = rcc
|
||||
.cfgr
|
||||
.use_hse(16.MHz())
|
||||
.sysclk(48.MHz())
|
||||
.pclk1(24.MHz())
|
||||
.freeze(&mut flash.acr);
|
||||
|
||||
// ====================== ADC setup =================
|
||||
// Configure ADC clocks
|
||||
// Default value is the slowest possible ADC clock: PCLK2 / 8. Meanwhile ADC
|
||||
// clock is configurable. So its frequency may be tweaked to meet certain
|
||||
// practical needs. User specified value is be approximated using supported
|
||||
// prescaler values 2/4/6/8.
|
||||
// let clocks = rcc.cfgr.adcclk(2.MHz()).freeze(&mut flash.acr);
|
||||
|
||||
// Setup ADC
|
||||
let mut adc1 = adc::Adc::adc1(p.ADC1, clocks);
|
||||
|
||||
// Configure pa1 as an analog input
|
||||
let mut ch0 = gpioa.pa1.into_analog(&mut gpioa.crl);
|
||||
|
||||
// ====================== PWM setup =================
|
||||
let mut afio = p.AFIO.constrain();
|
||||
let c1 = gpioa.pa0.into_alternate_push_pull(&mut gpioa.crl);
|
||||
let mut pwm = p.TIM2.pwm_hz::<Tim2NoRemap, _, _>(c1, &mut afio.mapr, 1.kHz(), &clocks);
|
||||
pwm.enable(Channel::C1);
|
||||
let pwm_max = pwm.get_max_duty() as u16;
|
||||
|
||||
// ====================== USB setup =================
|
||||
assert!(clocks.usbclk_valid());
|
||||
let mut usb_dp = gpioa.pa12.into_push_pull_output(&mut gpioa.crh);
|
||||
let usb = Peripheral {
|
||||
usb: p.USB,
|
||||
pin_dm: gpioa.pa11,
|
||||
pin_dp: usb_dp.into_floating_input(&mut gpioa.crh),
|
||||
};
|
||||
let usb_bus = UsbBus::new(usb);
|
||||
|
||||
let mut serial = SerialPort::new(&usb_bus);
|
||||
|
||||
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
|
||||
.manufacturer("FLC Meow!")
|
||||
.product("Serial port")
|
||||
.serial_number("01189998819991197253")
|
||||
.device_class(USB_CLASS_CDC)
|
||||
.build();
|
||||
|
||||
loop {
|
||||
let data: u16 = adc1.read(&mut ch0).unwrap();
|
||||
let mut pwm_val = data as f32 / 0xfff as f32;
|
||||
let pwm_duty = (pwm_max as f32 * pwm_val as f32) as u16;
|
||||
pwm.set_duty(Channel::C1, pwm_duty);
|
||||
|
||||
let mut buf = ryu::Buffer::new();
|
||||
let ret = buf.format(pwm_val).as_bytes();
|
||||
|
||||
// let mut write_offset = 0;
|
||||
// while write_offset < 8 {
|
||||
// match serial.write(&ret[write_offset..8]) {
|
||||
// Ok(len) if len > 0 => {
|
||||
// write_offset += len;
|
||||
// }
|
||||
// _ => {}
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue