Kinda working serial example
Signed-off-by: fly <merspieler@alwaysdata.com>
This commit is contained in:
parent
60bf8222e6
commit
9a1a7834dc
4 changed files with 103 additions and 40 deletions
20
firmware/Cargo.lock
generated
20
firmware/Cargo.lock
generated
|
@ -73,6 +73,15 @@ dependencies = [
|
|||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cortex-m-semihosting"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c23234600452033cc77e4b761e740e02d2c4168e11dbf36ab14a0f58973592b0"
|
||||
dependencies = [
|
||||
"cortex-m",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "embedded-dma"
|
||||
version = "0.2.0"
|
||||
|
@ -101,6 +110,7 @@ dependencies = [
|
|||
"embedded-hal",
|
||||
"nb 1.1.0",
|
||||
"panic-halt",
|
||||
"panic-semihosting",
|
||||
"ryu",
|
||||
"stm32f1xx-hal",
|
||||
"usb-device",
|
||||
|
@ -153,6 +163,16 @@ version = "0.2.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "de96540e0ebde571dc55c73d60ef407c653844e6f9a1e2fdbd40c07b9252d812"
|
||||
|
||||
[[package]]
|
||||
name = "panic-semihosting"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ee8a3e1233d9073d76a870223512ce4eeea43c067a94a445c13bd6d792d7b1ab"
|
||||
dependencies = [
|
||||
"cortex-m",
|
||||
"cortex-m-semihosting",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.78"
|
||||
|
|
|
@ -16,6 +16,7 @@ panic-halt = "0.2.0"
|
|||
usb-device = "0.2.9"
|
||||
usbd-serial = "0.1.1"
|
||||
ryu = "1.0.16"
|
||||
panic-semihosting = "0.6.0"
|
||||
|
||||
[dependencies.stm32f1xx-hal]
|
||||
version = "0.10.0"
|
||||
|
|
12
firmware/openocd.cfg
Normal file
12
firmware/openocd.cfg
Normal file
|
@ -0,0 +1,12 @@
|
|||
# Sample OpenOCD configuration for the STM32F3DISCOVERY development board
|
||||
|
||||
# Depending on the hardware revision you got you'll have to pick ONE of these
|
||||
# interfaces. At any time only one interface should be commented out.
|
||||
|
||||
# Revision C (newer revision)
|
||||
source [find interface/jlink.cfg]
|
||||
|
||||
# Revision A and B (older revisions)
|
||||
# source [find interface/stlink-v2.cfg]
|
||||
|
||||
source [find target/stm32f1x.cfg]
|
|
@ -7,12 +7,15 @@ use panic_halt as _;
|
|||
use nb::block;
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
use cortex_m::asm::delay;
|
||||
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};
|
||||
|
||||
use core::f32::RADIX;
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
// ====================== general setup =================
|
||||
|
@ -32,15 +35,30 @@ fn main() -> ! {
|
|||
.sysclk(48.MHz())
|
||||
.pclk1(24.MHz())
|
||||
.freeze(&mut flash.acr);
|
||||
|
||||
// ====================== USB setup =================
|
||||
assert!(clocks.usbclk_valid());
|
||||
let mut usb_dp = gpioa.pa12.into_push_pull_output(&mut gpioa.crh);
|
||||
usb_dp.set_low();
|
||||
delay(clocks.sysclk().raw() / 100);
|
||||
|
||||
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();
|
||||
|
||||
// ====================== 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);
|
||||
|
||||
|
@ -52,44 +70,56 @@ fn main() -> ! {
|
|||
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();
|
||||
let pwm_max = pwm.get_max_duty() as u16; //48000 in our case
|
||||
|
||||
let mut pwm_debug = 0;
|
||||
let mut isInit = false;
|
||||
loop {
|
||||
// Poll USB
|
||||
if !usb_dev.poll(&mut [&mut serial]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Read serial data and set PWM
|
||||
let mut buf = [0u8; 64];
|
||||
|
||||
match serial.read(&mut buf) {
|
||||
Ok(count) => {
|
||||
isInit = true;
|
||||
let tmpBuf = format!("{:?}", &buf);
|
||||
let pwm_duty = tmpBuf.chars().map(|c| c.to_digit(RADIX).unwrap()).sum::<u32>();
|
||||
// TODO get value and set PWM
|
||||
pwm.set_duty(Channel::C1, pwm_duty);
|
||||
}
|
||||
_ => {
|
||||
if !isInit {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get data from pots
|
||||
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 outBuf = ryu::Buffer::new();
|
||||
let mut ret = outBuf.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;
|
||||
// }
|
||||
// _ => {}
|
||||
// }
|
||||
// }
|
||||
// Write serial data
|
||||
// NOTE: Make sure not to ever run in here before a successful read!
|
||||
if isInit {
|
||||
while !ret.is_empty() {
|
||||
match serial.write(ret) {
|
||||
Ok(count) => {
|
||||
ret = &ret[count..];
|
||||
}
|
||||
_ => {continue;},
|
||||
}
|
||||
}
|
||||
serial.write(b"\n");
|
||||
}
|
||||
|
||||
// Sleep so we don't send that quickly
|
||||
delay(clocks.sysclk().raw() / 10);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue