First project for the rpi pico (w) using rust
This commit is contained in:
commit
64a41176f6
8
.cargo/config.toml
Normal file
8
.cargo/config.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
|
||||
runner = "probe-run --chip RP2040"
|
||||
|
||||
[build]
|
||||
target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+
|
||||
|
||||
[env]
|
||||
DEFMT_LOG = "debug"
|
2
.gdbinit
Normal file
2
.gdbinit
Normal file
|
@ -0,0 +1,2 @@
|
|||
file target/thumbv6m-none-eabi/debug/rp
|
||||
target remote :1337
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target
|
1761
Cargo.lock
generated
Normal file
1761
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
67
Cargo.toml
Normal file
67
Cargo.toml
Normal file
|
@ -0,0 +1,67 @@
|
|||
[package]
|
||||
name = "rp"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
cortex-m = { version = "0.7", features = ["inline-asm"] }
|
||||
cortex-m-rt = "0.7"
|
||||
defmt = "0.3"
|
||||
defmt-rtt = "0.4"
|
||||
embassy-executor = { git = "https://github.com/embassy-rs/embassy", features = [
|
||||
"arch-cortex-m",
|
||||
"executor-thread",
|
||||
"executor-interrupt",
|
||||
"defmt",
|
||||
"nightly",
|
||||
"integrated-timers",
|
||||
] }
|
||||
embassy-rp = { git = "https://github.com/embassy-rs/embassy", features = [
|
||||
"defmt",
|
||||
"unstable-traits",
|
||||
"nightly",
|
||||
"unstable-pac",
|
||||
"time-driver",
|
||||
"critical-section-impl",
|
||||
] }
|
||||
embassy-time = { git = "https://github.com/embassy-rs/embassy", features = [
|
||||
"defmt",
|
||||
"unstable-traits",
|
||||
"defmt-timestamp-uptime",
|
||||
"nightly",
|
||||
] }
|
||||
embassy-usb = { git = "https://github.com/embassy-rs/embassy", features = [
|
||||
"defmt",
|
||||
] }
|
||||
embassy-net = { git = "https://github.com/embassy-rs/embassy", features = [
|
||||
"tcp",
|
||||
"dhcpv4",
|
||||
"nightly",
|
||||
"medium-ethernet",
|
||||
"defmt",
|
||||
] }
|
||||
embassy-sync = { git = "https://github.com/embassy-rs/embassy", features = [
|
||||
"defmt",
|
||||
] }
|
||||
embassy-futures = { git = "https://github.com/embassy-rs/embassy", features = [
|
||||
"defmt",
|
||||
] }
|
||||
cyw43 = { git = "https://github.com/embassy-rs/embassy", features = ["defmt"] }
|
||||
cyw43-pio = { git = "https://github.com/embassy-rs/embassy", features = [
|
||||
"defmt",
|
||||
] }
|
||||
panic-probe = { version = "0.3", features = ["print-defmt"] }
|
||||
log = "0.4.17"
|
||||
static_cell = { version = "1.1.0", features = ["nightly"] }
|
||||
heapless = { version = "0.7.16", features = ["defmt"] }
|
||||
embedded-io = { version = "0.4.0", features = ["async", "defmt"] }
|
||||
crc16 = "0.4.0"
|
||||
dsmr5 = "0.2.2"
|
||||
|
||||
[build-dependencies]
|
||||
dotenvy = "0.15.7"
|
||||
|
||||
[profile.release]
|
||||
debug = true
|
45
build.rs
Normal file
45
build.rs
Normal file
|
@ -0,0 +1,45 @@
|
|||
//! This build script copies the `memory.x` file from the crate root into
|
||||
//! a directory where the linker can always find it at build time.
|
||||
//! For many projects this is optional, as the linker always searches the
|
||||
//! project root directory -- wherever `Cargo.toml` is. However, if you
|
||||
//! are using a workspace or have a more complicated build setup, this
|
||||
//! build script becomes required. Additionally, by requesting that
|
||||
//! Cargo re-run the build script whenever `memory.x` is changed,
|
||||
//! updating `memory.x` ensures a rebuild of the application with the
|
||||
//! new memory settings.
|
||||
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
// Put `memory.x` in our output directory and ensure it's
|
||||
// on the linker search path.
|
||||
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
|
||||
File::create(out.join("memory.x"))
|
||||
.unwrap()
|
||||
.write_all(include_bytes!("memory.x"))
|
||||
.unwrap();
|
||||
println!("cargo:rustc-link-search={}", out.display());
|
||||
|
||||
// By default, Cargo will re-run a build script whenever
|
||||
// any file in the project changes. By specifying `memory.x`
|
||||
// here, we ensure the build script is only re-run when
|
||||
// `memory.x` is changed.
|
||||
println!("cargo:rerun-if-changed=memory.x");
|
||||
|
||||
println!("cargo:rustc-link-arg-bins=--nmagic");
|
||||
println!("cargo:rustc-link-arg-bins=-Tlink.x");
|
||||
println!("cargo:rustc-link-arg-bins=-Tlink-rp.x");
|
||||
println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
|
||||
|
||||
if let Ok(dotenv_path) = dotenvy::dotenv() {
|
||||
println!("cargo:rerun-if-changed={}", dotenv_path.display());
|
||||
|
||||
for env_var in dotenvy::dotenv_iter().unwrap() {
|
||||
let (key, value) = env_var.unwrap();
|
||||
println!("cargo:rustc-env={key}={value}");
|
||||
}
|
||||
}
|
||||
}
|
BIN
firmware/43439A0.bin
Normal file
BIN
firmware/43439A0.bin
Normal file
Binary file not shown.
BIN
firmware/43439A0_clm.bin
Normal file
BIN
firmware/43439A0_clm.bin
Normal file
Binary file not shown.
5
memory.x
Normal file
5
memory.x
Normal file
|
@ -0,0 +1,5 @@
|
|||
MEMORY {
|
||||
BOOT2 : ORIGIN = 0x10000000, LENGTH = 0x100
|
||||
FLASH : ORIGIN = 0x10000100, LENGTH = 1024K - 0x100
|
||||
RAM : ORIGIN = 0x20000000, LENGTH = 256K
|
||||
}
|
41
sample.txt
Normal file
41
sample.txt
Normal file
|
@ -0,0 +1,41 @@
|
|||
/ISK5\2M550T-1011
|
||||
|
||||
1-3:0.2.8(50)
|
||||
0-0:1.0.0(181106140429W)
|
||||
0-0:96.1.1(4530303334303036383130353136343136)
|
||||
1-0:1.8.1(003808.351*kWh)
|
||||
1-0:1.8.2(002948.827*kWh)
|
||||
1-0:2.8.1(001285.951*kWh)
|
||||
1-0:2.8.2(002876.514*kWh)
|
||||
0-0:96.14.0(0002)
|
||||
1-0:1.7.0(00.000*kW)
|
||||
1-0:2.7.0(00.498*kW)
|
||||
0-0:96.7.21(00006)
|
||||
0-0:96.7.9(00003)
|
||||
1-0:99.97.0(1)(0-0:96.7.19)(180529135630S)(0000002451*s)
|
||||
1-0:32.32.0(00003)
|
||||
1-0:52.32.0(00002)
|
||||
1-0:72.32.0(00002)
|
||||
1-0:32.36.0(00001)
|
||||
1-0:52.36.0(00001)
|
||||
1-0:72.36.0(00001)
|
||||
0-0:96.13.0()
|
||||
1-0:32.7.0(236.0*V)
|
||||
1-0:52.7.0(232.6*V)
|
||||
1-0:72.7.0(235.1*V)
|
||||
1-0:31.7.0(002*A)
|
||||
1-0:51.7.0(000*A)
|
||||
1-0:71.7.0(000*A)
|
||||
1-0:21.7.0(00.000*kW)
|
||||
1-0:41.7.0(00.033*kW)
|
||||
1-0:61.7.0(00.132*kW)
|
||||
1-0:22.7.0(00.676*kW)
|
||||
1-0:42.7.0(00.000*kW)
|
||||
1-0:62.7.0(00.000*kW)
|
||||
0-1:24.1.0(003)
|
||||
0-1:96.1.0(4730303339303031373030343630313137)
|
||||
0-1:24.2.1(181106140010W)(01569.646*m3)
|
||||
!1F28
|
||||
|
||||
|
||||
|
336
src/main.rs
Normal file
336
src/main.rs
Normal file
|
@ -0,0 +1,336 @@
|
|||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use cyw43_pio::PioSpi;
|
||||
use defmt::{debug, info, warn, Format};
|
||||
use dsmr5::Readout;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_futures::select::{select, Either};
|
||||
use embassy_net::{tcp::TcpSocket, Ipv4Address, Ipv4Cidr, Stack, StackResources};
|
||||
use embassy_rp::{
|
||||
bind_interrupts, gpio,
|
||||
peripherals::{DMA_CH0, PIN_23, PIN_25, PIO0, UART0},
|
||||
pio::Pio,
|
||||
uart::{self, BufferedUartRx, Parity},
|
||||
};
|
||||
|
||||
use embedded_io::asynch::Read;
|
||||
|
||||
use embassy_sync::{
|
||||
blocking_mutex::raw::NoopRawMutex,
|
||||
channel::{Channel, Sender},
|
||||
};
|
||||
use gpio::{Level, Output};
|
||||
use heapless::Vec;
|
||||
use static_cell::make_static;
|
||||
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
bind_interrupts!(struct Irqs {
|
||||
UART0_IRQ => uart::BufferedInterruptHandler<UART0>;
|
||||
});
|
||||
|
||||
#[derive(Format)]
|
||||
struct Test {
|
||||
counter: u32,
|
||||
}
|
||||
|
||||
// #[embassy_executor::task]
|
||||
// async fn usb_task(mut device: UsbDevice<'static, Driver<'static, peripherals::USB>>) -> ! {
|
||||
// device.run().await
|
||||
// }
|
||||
|
||||
// #[embassy_executor::task]
|
||||
// async fn echo_task(
|
||||
// mut class: CdcAcmClass<'static, Driver<'static, peripherals::USB>>,
|
||||
// sender: Sender<'static, NoopRawMutex, Message, 1>,
|
||||
// ) -> ! {
|
||||
// loop {
|
||||
// class.wait_connection().await;
|
||||
// info!("Connected");
|
||||
// let _ = echo(&mut class, &sender).await;
|
||||
// info!("Disconnected");
|
||||
// }
|
||||
// }
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn wifi_task(
|
||||
runner: cyw43::Runner<
|
||||
'static,
|
||||
Output<'static, PIN_23>,
|
||||
PioSpi<'static, PIN_25, PIO0, 0, DMA_CH0>,
|
||||
>,
|
||||
) -> ! {
|
||||
runner.run().await
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn net_task(stack: &'static Stack<cyw43::NetDriver<'static>>) -> ! {
|
||||
stack.run().await
|
||||
}
|
||||
|
||||
async fn get_readout(rx: &mut BufferedUartRx<'static, UART0>) -> Readout {
|
||||
let mut buffer: Vec<u8, 2048> = Vec::new();
|
||||
buffer.push(b'/').unwrap();
|
||||
|
||||
let mut byte = [0; 1];
|
||||
debug!("Waiting for next telegram...");
|
||||
loop {
|
||||
rx.read_exact(&mut byte).await.unwrap();
|
||||
|
||||
if byte[0] == b'/' {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
debug!("Start of telegram detected");
|
||||
|
||||
loop {
|
||||
rx.read(&mut byte).await.unwrap();
|
||||
buffer.push(byte[0]).unwrap();
|
||||
|
||||
if byte[0] == b'!' {
|
||||
debug!("Start of CRC detected");
|
||||
|
||||
let mut crc = [0; 4];
|
||||
rx.read_exact(&mut crc).await.unwrap();
|
||||
|
||||
buffer.extend_from_slice(&crc).unwrap();
|
||||
|
||||
debug!("Received telegram");
|
||||
|
||||
// Fill the rest of the buffer with zeroes
|
||||
buffer.resize(2048, 0).unwrap();
|
||||
|
||||
return Readout {
|
||||
buffer: buffer.into_array().unwrap(),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn uart_rx_task(
|
||||
mut rx: BufferedUartRx<'static, UART0>,
|
||||
sender: Sender<'static, NoopRawMutex, Readout, 1>,
|
||||
) {
|
||||
info!("Wating for serial data");
|
||||
loop {
|
||||
let readout = get_readout(&mut rx).await;
|
||||
match sender.try_send(readout) {
|
||||
Ok(_) => {}
|
||||
Err(_) => warn!("Queue is full!"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(spawner: Spawner) {
|
||||
info!("Hello world!");
|
||||
let p = embassy_rp::init(Default::default());
|
||||
|
||||
let channel = make_static!(Channel::<NoopRawMutex, Readout, 1>::new());
|
||||
|
||||
// === UART ===
|
||||
let mut config = uart::Config::default();
|
||||
config.parity = Parity::ParityNone;
|
||||
// config.invert_rx = true;
|
||||
|
||||
let buf = make_static!([0u8; 2048]);
|
||||
let rx = BufferedUartRx::new_with_rts(p.UART0, Irqs, p.PIN_1, p.PIN_3, buf, config);
|
||||
|
||||
spawner.spawn(uart_rx_task(rx, channel.sender())).unwrap();
|
||||
|
||||
// === USB SERIAL ===
|
||||
// let driver = Driver::new(p.USB, Irqs);
|
||||
//
|
||||
// let mut config = Config::new(0xc0de, 0xcafe);
|
||||
// config.manufacturer = Some("huizinga.dev");
|
||||
// config.product = Some("Raspberry Pi Pico");
|
||||
// config.serial_number = Some("123456789");
|
||||
// config.max_power = 0;
|
||||
// config.max_packet_size_0 = 64;
|
||||
//
|
||||
// // Needed for windows compatiblilty
|
||||
// config.device_class = 0xEF;
|
||||
// config.device_sub_class = 0x02;
|
||||
// config.device_protocol = 0x01;
|
||||
// config.composite_with_iads = true;
|
||||
//
|
||||
// let mut builder = Builder::new(
|
||||
// driver,
|
||||
// config,
|
||||
// singleton!([0; 256]),
|
||||
// singleton!([0; 256]),
|
||||
// singleton!([0; 256]),
|
||||
// singleton!([0; 64]),
|
||||
// );
|
||||
//
|
||||
// let class = CdcAcmClass::new(&mut builder, singleton!(State::new()), 64);
|
||||
// let usb = builder.build();
|
||||
//
|
||||
// spawner.spawn(usb_task(usb)).unwrap();
|
||||
// spawner.spawn(echo_task(class, channel.sender())).unwrap();
|
||||
|
||||
// === WIFI ===
|
||||
// To make flashing faster for development, you may want to flash the firmwares independently
|
||||
// at hardcoded addresses, instead of baking them into the program with `include_bytes!`:
|
||||
// probe-rs-cli download 43439A0.bin --format bin --chip RP2040 --base-address 0x10100000
|
||||
// probe-rs-cli download 43439A0_clm.bin --format bin --chip RP2040 --base-address 0x10140000
|
||||
let fw = unsafe { core::slice::from_raw_parts(0x10100000 as *const u8, 224190) };
|
||||
let clm = unsafe { core::slice::from_raw_parts(0x10140000 as *const u8, 4752) };
|
||||
// let fw = include_bytes!("../firmware/43439A0.bin");
|
||||
// let clm = include_bytes!("../firmware/43439A0_clm.bin");
|
||||
|
||||
let pwr = Output::new(p.PIN_23, Level::Low);
|
||||
let cs = Output::new(p.PIN_25, Level::High);
|
||||
|
||||
let mut pio = Pio::new(p.PIO0);
|
||||
let spi = PioSpi::new(
|
||||
&mut pio.common,
|
||||
pio.sm0,
|
||||
pio.irq0,
|
||||
cs,
|
||||
p.PIN_24,
|
||||
p.PIN_29,
|
||||
p.DMA_CH0,
|
||||
);
|
||||
|
||||
let state = make_static!(cyw43::State::new());
|
||||
let (net_device, mut control, runner) = cyw43::new(state, pwr, spi, fw).await;
|
||||
spawner.spawn(wifi_task(runner)).unwrap();
|
||||
|
||||
control.init(clm).await;
|
||||
control
|
||||
.set_power_management(cyw43::PowerManagementMode::PowerSave)
|
||||
.await;
|
||||
|
||||
let ip = Ipv4Address::new(10, 0, 0, 77);
|
||||
|
||||
// let config = embassy_net::Config::Dhcp(Default::default());
|
||||
let config = embassy_net::Config::Static(embassy_net::StaticConfig {
|
||||
address: Ipv4Cidr::new(ip, 24),
|
||||
dns_servers: Vec::new(),
|
||||
gateway: Some(Ipv4Address::new(10, 0, 0, 1)),
|
||||
});
|
||||
|
||||
let seed = 0x51ac_3101_6468_8cdf;
|
||||
let stack = make_static!(Stack::new(
|
||||
net_device,
|
||||
config,
|
||||
make_static!(StackResources::<2>::new()),
|
||||
seed,
|
||||
));
|
||||
|
||||
spawner.spawn(net_task(stack)).unwrap();
|
||||
|
||||
// Connect to wifi
|
||||
loop {
|
||||
match control
|
||||
.join_wpa2(env!("WIFI_NETWORK"), env!("WIFI_PASSWORD"))
|
||||
.await
|
||||
{
|
||||
Ok(_) => break,
|
||||
Err(err) => {
|
||||
info!("Failed to join with status = {}", err.status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut rx_buffer = [0; 4096];
|
||||
let mut tx_buffer = [0; 4096];
|
||||
|
||||
let receiver = channel.receiver();
|
||||
|
||||
loop {
|
||||
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
||||
// socket.set_timeout(Some(Duration::from_secs(10)));
|
||||
|
||||
control.gpio_set(0, false).await;
|
||||
let port = 1234;
|
||||
info!("Listening on {}:{}...", ip, port);
|
||||
|
||||
if let Err(e) = socket.accept(port).await {
|
||||
warn!("Accept error: {:?}", e);
|
||||
continue;
|
||||
}
|
||||
|
||||
info!("Received connection from {:?}", socket.remote_endpoint());
|
||||
control.gpio_set(0, true).await;
|
||||
|
||||
loop {
|
||||
match select(socket.read(&mut [0; 64]), receiver.recv()).await {
|
||||
Either::First(Ok(0)) => {
|
||||
warn!("Read OEF");
|
||||
break;
|
||||
}
|
||||
Either::First(Ok(_)) => {
|
||||
// Received something over the socket, currently does nothing
|
||||
}
|
||||
Either::First(Err(e)) => {
|
||||
warn!("Read error: {:?}", e);
|
||||
}
|
||||
Either::Second(readout) => {
|
||||
let telegram = readout.to_telegram().unwrap();
|
||||
|
||||
debug!("checksum: {}", telegram.checksum);
|
||||
debug!("prefix: {}", telegram.prefix);
|
||||
debug!("identification: {}", telegram.identification);
|
||||
|
||||
let state = dsmr5::Result::<dsmr5::state::State>::from(&telegram).unwrap();
|
||||
debug!("datetime: {}", state.datetime.unwrap().year);
|
||||
debug!("meterreadings[0]: {}", state.meterreadings[0].to.unwrap());
|
||||
debug!("meterreadings[1]: {}", state.meterreadings[1].to.unwrap());
|
||||
|
||||
debug!("slave:");
|
||||
debug!("\tdevice_type: {}", state.slaves[0].device_type.unwrap());
|
||||
debug!(
|
||||
"\tmeter_reading: {}",
|
||||
state.slaves[0].meter_reading.as_ref().unwrap().1
|
||||
);
|
||||
|
||||
}
|
||||
// Either::Second(message) => match socket.write(&message).await {
|
||||
// Ok(_) => {}
|
||||
// Err(e) => {
|
||||
// warn!("Write error: {:?}", e);
|
||||
// break;
|
||||
// }
|
||||
// },
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// struct Disconnected;
|
||||
//
|
||||
// impl From<EndpointError> for Disconnected {
|
||||
// fn from(value: EndpointError) -> Self {
|
||||
// match value {
|
||||
// EndpointError::BufferOverflow => panic!("Buffer overflow"),
|
||||
// EndpointError::Disabled => Self {},
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /// Echo back received packets
|
||||
// async fn echo<'d>(
|
||||
// class: &mut CdcAcmClass<'d, Driver<'d, peripherals::USB>>,
|
||||
// sender: &Sender<'static, NoopRawMutex, Message, 1>,
|
||||
// ) -> Result<(), Disconnected> {
|
||||
// let mut buf = [0; 64];
|
||||
// loop {
|
||||
// let n = class.read_packet(&mut buf).await?;
|
||||
// let data = from_utf8_mut(&mut buf[..n]).unwrap();
|
||||
//
|
||||
// data.make_ascii_uppercase();
|
||||
//
|
||||
// sender
|
||||
// .send(Message {
|
||||
// data: buf,
|
||||
// length: n,
|
||||
// })
|
||||
// .await;
|
||||
// }
|
||||
// }
|
Loading…
Reference in New Issue
Block a user