switched inside controller back to pico (micro center run)
This commit is contained in:
parent
6b80c49a59
commit
542be263df
9 changed files with 2123 additions and 746 deletions
|
@ -1,16 +1,10 @@
|
|||
[target.xtensa-esp32-none-elf]
|
||||
runner = "espflash flash --monitor"
|
||||
|
||||
|
||||
[env]
|
||||
ESP_LOG="info"
|
||||
#[target.'cfg(all(target_arch = "arm", target_os = "none"))']
|
||||
#runner = "probe-rs run --chip RP2040"
|
||||
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
|
||||
runner = "elf2uf2-rs --deploy --serial --verbose"
|
||||
|
||||
[build]
|
||||
rustflags = [
|
||||
"-C", "link-arg=-nostartfiles",
|
||||
]
|
||||
target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+
|
||||
|
||||
target = "xtensa-esp32-none-elf"
|
||||
|
||||
[unstable]
|
||||
build-std = ["alloc", "core"]
|
||||
[env]
|
||||
DEFMT_LOG = "debug"
|
||||
|
|
2255
inside/Cargo.lock
generated
2255
inside/Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -1,52 +1,71 @@
|
|||
[package]
|
||||
edition = "2021"
|
||||
name = "inside"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "MIT OR Apache-2.0"
|
||||
|
||||
|
||||
[dependencies]
|
||||
blocking-network-stack = { git = "https://github.com/bjoernQ/blocking-network-stack.git", rev = "1c581661d78e0cf0f17b936297179b993fb149d7" }
|
||||
esp-backtrace = { version = "0.14.2", features = [
|
||||
"esp32",
|
||||
"exception-handler",
|
||||
"panic-handler",
|
||||
"println",
|
||||
] }
|
||||
esp-hal = { version = "0.21.0", features = [ "esp32" ] }
|
||||
esp-println = { version = "0.12.0", features = ["esp32", "log"] }
|
||||
log = { version = "0.4.22" }
|
||||
esp-alloc = { version = "0.5.0" }
|
||||
embedded-io = "0.6.1"
|
||||
embedded-hal = "1"
|
||||
esp-wifi = { version = "0.10.1", features = [
|
||||
"esp32",
|
||||
"phy-enable-usb",
|
||||
"utils",
|
||||
"wifi",
|
||||
] }
|
||||
heapless = { version = "0.8.0", default-features = false }
|
||||
smoltcp = { version = "0.11.0", default-features = false, features = [
|
||||
"medium-ethernet",
|
||||
"proto-dhcpv4",
|
||||
"proto-igmp",
|
||||
"proto-ipv4",
|
||||
"socket-dhcpv4",
|
||||
"socket-icmp",
|
||||
"socket-raw",
|
||||
"socket-tcp",
|
||||
"socket-udp",
|
||||
] }
|
||||
embassy-executor = { version = "0.6.0", git="https://github.com/embassy-rs/embassy", features = ["task-arena-size-98304", "arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "integrated-timers"] }
|
||||
embassy-time = { version = "0.3.2", git="https://github.com/embassy-rs/embassy", features = ["defmt", "defmt-timestamp-uptime"] }
|
||||
embassy-rp = { version = "0.2.0", git="https://github.com/embassy-rs/embassy", features = ["defmt", "unstable-pac", "time-driver", "critical-section-impl", "rp2040"] }
|
||||
embassy-usb = { version = "0.3.0", git="https://github.com/embassy-rs/embassy", features = ["defmt"] }
|
||||
embassy-net = { version = "0.4.0", git="https://github.com/embassy-rs/embassy", features = ["defmt", "tcp", "udp", "raw", "dhcpv4", "medium-ethernet", "dns"] }
|
||||
embassy-net-wiznet = { version = "0.1.0", git="https://github.com/embassy-rs/embassy", features = ["defmt"] }
|
||||
embassy-futures = { version = "0.1.0", git="https://github.com/embassy-rs/embassy"}
|
||||
embassy-usb-logger = { version = "0.2.0", git="https://github.com/embassy-rs/embassy" }
|
||||
cyw43 = { version = "0.2.0", git="https://github.com/embassy-rs/embassy", features = ["defmt", "firmware-logs", "bluetooth"] }
|
||||
cyw43-pio = { version = "0.2.0", git="https://github.com/embassy-rs/embassy", features = ["defmt"] }
|
||||
|
||||
defmt = "0.3"
|
||||
defmt-rtt = "0.4"
|
||||
fixed = "1.23.1"
|
||||
fixed-macro = "1.2"
|
||||
|
||||
# for web request example
|
||||
reqwless = { version = "0.12.0", features = ["defmt",]}
|
||||
serde = { version = "1.0.203", default-features = false, features = ["derive"] }
|
||||
serde-json-core = "0.5.1"
|
||||
|
||||
# for assign resources example
|
||||
assign-resources = { git = "https://github.com/adamgreig/assign-resources", rev = "94ad10e2729afdf0fd5a77cd12e68409a982f58a" }
|
||||
|
||||
#cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] }
|
||||
cortex-m = { version = "0.7.6", features = ["inline-asm"] }
|
||||
cortex-m-rt = "0.7.0"
|
||||
critical-section = "1.1"
|
||||
panic-probe = { version = "0.3", features = ["print-defmt"] }
|
||||
display-interface-spi = "0.4.1"
|
||||
embedded-graphics = "0.7.1"
|
||||
st7789 = "0.6.1"
|
||||
display-interface = "0.4.1"
|
||||
byte-slice-cast = { version = "1.2.0", default-features = false }
|
||||
smart-leds = "0.3.0"
|
||||
heapless = "0.8"
|
||||
usbd-hid = "0.8.1"
|
||||
|
||||
embedded-hal-1 = { package = "embedded-hal", version = "1.0" }
|
||||
embedded-hal-async = "1.0"
|
||||
embedded-hal-bus = { version = "0.1", features = ["async"] }
|
||||
embedded-io-async = { version = "0.6.1", features = ["defmt-03"] }
|
||||
embedded-storage = { version = "0.3" }
|
||||
static_cell = "2.1"
|
||||
portable-atomic = { version = "1.5", features = ["critical-section"] }
|
||||
log = "0.4"
|
||||
pio-proc = "0.2"
|
||||
pio = "0.2.1"
|
||||
rand = { version = "0.8.5", default-features = false }
|
||||
embedded-sdmmc = "0.7.0"
|
||||
|
||||
bt-hci = { version = "0.1.0", default-features = false, features = ["defmt"] }
|
||||
hex = { version = "0.4.3", default-features=false}
|
||||
|
||||
[profile.dev]
|
||||
# Rust debug is too slow.
|
||||
# For debug builds always builds with some optimization
|
||||
opt-level = "s"
|
||||
|
||||
[profile.release]
|
||||
codegen-units = 1 # LLVM can perform better optimizations using a single thread
|
||||
debug = 2
|
||||
debug-assertions = false
|
||||
incremental = false
|
||||
lto = 'fat'
|
||||
opt-level = 's'
|
||||
overflow-checks = false
|
||||
lto = true
|
||||
opt-level = 'z'
|
||||
|
||||
[profile.dev]
|
||||
debug = 2
|
||||
lto = true
|
||||
opt-level = "z"
|
||||
|
|
|
@ -1,5 +1,36 @@
|
|||
fn main() {
|
||||
println!("cargo:rustc-link-arg-bins=-Tlinkall.x");
|
||||
//! 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.
|
||||
|
||||
println!("cargo:rustc-link-arg-bins=-Trom_functions.x");
|
||||
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");
|
||||
}
|
||||
|
|
17
inside/memory.x
Normal file
17
inside/memory.x
Normal file
|
@ -0,0 +1,17 @@
|
|||
MEMORY {
|
||||
BOOT2 : ORIGIN = 0x10000000, LENGTH = 0x100
|
||||
FLASH : ORIGIN = 0x10000100, LENGTH = 2048K - 0x100
|
||||
|
||||
/* Pick one of the two options for RAM layout */
|
||||
|
||||
/* OPTION A: Use all RAM banks as one big block */
|
||||
/* Reasonable, unless you are doing something */
|
||||
/* really particular with DMA or other concurrent */
|
||||
/* access that would benefit from striping */
|
||||
RAM : ORIGIN = 0x20000000, LENGTH = 264K
|
||||
|
||||
/* OPTION B: Keep the unstriped sections separate */
|
||||
/* RAM: ORIGIN = 0x20000000, LENGTH = 256K */
|
||||
/* SCRATCH_A: ORIGIN = 0x20040000, LENGTH = 4K */
|
||||
/* SCRATCH_B: ORIGIN = 0x20041000, LENGTH = 4K */
|
||||
}
|
|
@ -1,2 +0,0 @@
|
|||
[toolchain]
|
||||
channel = "esp"
|
|
@ -1,69 +0,0 @@
|
|||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(const_trait_impl)]
|
||||
|
||||
use core::sync::atomic::AtomicU32;
|
||||
|
||||
use blocking_network_stack::Socket;
|
||||
use embedded_hal::pwm::SetDutyCycle;
|
||||
use embedded_io::Write;
|
||||
use esp_hal::{delay::Delay, gpio::GpioPin, ledc::{channel::Channel, LowSpeed}, };
|
||||
use esp_println::println;
|
||||
use esp_wifi::wifi::{WifiDevice, WifiStaDevice};
|
||||
|
||||
|
||||
//pub fn handle<T,U,const V: u8>(slice: &[u8], motor: PwmPin<T,U, V,true>) {
|
||||
pub fn handle(slice: &[u8], motor: &mut Channel<LowSpeed, GpioPin<15>>, socket: &mut Socket<WifiDevice<WifiStaDevice>>) {
|
||||
println!("dat: {:?}", slice);
|
||||
let mut command = slice.trim_ascii().split(|c| *c == b' ');
|
||||
|
||||
// hate hate hate
|
||||
static LAST_BADGE_LOW: AtomicU32 = AtomicU32::new(0);
|
||||
static LAST_BADGE_HIG: AtomicU32 = AtomicU32::new(0);
|
||||
|
||||
match char::from_u32(command.next().unwrap()[0] as u32).unwrap() {
|
||||
'D' => {
|
||||
let hex = command.next().unwrap();
|
||||
println!("{hex:#?}");
|
||||
let buf: [u8;2] = hex::FromHex::from_hex(hex).unwrap();
|
||||
let power: u16 = u16::from_be_bytes(buf);
|
||||
println!("{power}");
|
||||
motor.set_duty_cycle(power).unwrap();
|
||||
},
|
||||
'O' => {
|
||||
open_door(motor);
|
||||
}
|
||||
'B' => {
|
||||
let buf: [u8;8] = hex::FromHex::from_hex(command.next().unwrap()).unwrap();
|
||||
let card: u64 = u64::from_ne_bytes(buf);
|
||||
println!("card {card:#16x}");
|
||||
|
||||
LAST_BADGE_LOW.store(card as u32, core::sync::atomic::Ordering::SeqCst);
|
||||
LAST_BADGE_HIG.store((card >> 32) as u32, core::sync::atomic::Ordering::SeqCst);
|
||||
|
||||
open_door(motor);
|
||||
},
|
||||
'G' => {
|
||||
|
||||
let mut last_badge: u64 = LAST_BADGE_LOW.load(core::sync::atomic::Ordering::SeqCst) as u64;
|
||||
last_badge += (LAST_BADGE_HIG.load(core::sync::atomic::Ordering::SeqCst) as u64) << 32;
|
||||
|
||||
let _ = socket.write_fmt(
|
||||
format_args!("B{last_badge}\r\n")
|
||||
);
|
||||
},
|
||||
_ => {}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub fn open_door(motor: &mut Channel<LowSpeed, GpioPin<15>>) {
|
||||
let delay = Delay::new();
|
||||
let _ = motor.set_duty_cycle(0x533);
|
||||
delay.delay_millis(3000);
|
||||
//Timer::after_millis(3000).await;
|
||||
let _ = motor.set_duty_cycle(0x4c8);
|
||||
delay.delay_millis(750);
|
||||
let _ = motor.set_duty_cycle(0x4CD);
|
||||
}
|
|
@ -1,204 +1,213 @@
|
|||
//! This example uses the RP Pico W board Wifi chip (cyw43).
|
||||
//! Creates an Access point Wifi network and creates a TCP endpoint on port 1234.
|
||||
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(const_trait_impl)]
|
||||
#![allow(async_fn_in_trait)]
|
||||
|
||||
use embedded_io::{Read, ReadReady, Write};
|
||||
use embedded_hal::pwm::SetDutyCycle;
|
||||
use esp_backtrace as _;
|
||||
use esp_hal::{gpio::Io, ledc::{channel, timer, LSGlobalClkSource, Ledc, LowSpeed}, prelude::*, reset::software_reset, rng::Rng, time::{self, Duration}};
|
||||
use esp_println::{print, println};
|
||||
use esp_wifi::wifi::{utils::create_network_interface, AccessPointInfo, ClientConfiguration, Configuration, WifiError, WifiStaDevice};
|
||||
use smoltcp::iface::SocketStorage;
|
||||
use blocking_network_stack::{ipv4, Stack};
|
||||
use inside::handle;
|
||||
use core::array;
|
||||
use core::fmt::Formatter;
|
||||
use core::panic::PanicInfo;
|
||||
use core::str::from_utf8;
|
||||
|
||||
extern crate alloc;
|
||||
use bt_hci::cmd::info;
|
||||
use cyw43_pio::PioSpi;
|
||||
use embassy_rp::pwm::{self, Pwm};
|
||||
use fixed::FixedU16;
|
||||
use log::*;
|
||||
//use embassy_rp::i2c::InterruptHandler;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_net::tcp::TcpSocket;
|
||||
use embassy_net::{Config, StackResources};
|
||||
use embassy_rp::bind_interrupts;
|
||||
use embassy_rp::clocks::RoscRng;
|
||||
use embassy_rp::gpio::{Level, Output};
|
||||
use embassy_rp::peripherals::{DMA_CH0, PIO0, USB};
|
||||
use embassy_rp::pio::{InterruptHandler, Pio};
|
||||
use embassy_rp::usb::Driver;
|
||||
use embassy_time::{Duration, Timer};
|
||||
use embedded_io_async::Write;
|
||||
use rand::RngCore;
|
||||
use reqwless::response;
|
||||
use static_cell::StaticCell;
|
||||
use defmt_rtt as _;
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
#[allow(unused)]
|
||||
bind_interrupts!(struct Irqs {
|
||||
PIO0_IRQ_0 => InterruptHandler<PIO0>;
|
||||
USBCTRL_IRQ => embassy_rp::usb::InterruptHandler<USB>;
|
||||
});
|
||||
|
||||
let mut config = esp_hal::Config::default();
|
||||
config.cpu_clock = CpuClock::max();
|
||||
let peripherals = esp_hal::init(config);
|
||||
let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
|
||||
let pin = io.pins.gpio15;
|
||||
#[embassy_executor::task]
|
||||
async fn logger_task(driver: Driver<'static, USB>) {
|
||||
embassy_usb_logger::run!(1024, log::LevelFilter::Debug, driver);
|
||||
}
|
||||
|
||||
let mut ledc = Ledc::new(peripherals.LEDC);
|
||||
ledc.set_global_slow_clock(LSGlobalClkSource::APBClk);
|
||||
let mut lstimer0 = ledc.get_timer::<LowSpeed>(timer::Number::Timer0);
|
||||
lstimer0
|
||||
.configure(timer::config::Config {
|
||||
duty: timer::config::Duty::Duty14Bit,
|
||||
clock_source: timer::LSClockSource::APBClk,
|
||||
frequency: 50.Hz(),
|
||||
})
|
||||
.unwrap();
|
||||
#[embassy_executor::task]
|
||||
async fn cyw43_task(runner: cyw43::Runner<'static, Output<'static>, PioSpi<'static, PIO0, 0, DMA_CH0>>) -> ! {
|
||||
runner.run().await
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn net_task(mut runner: embassy_net::Runner<'static, cyw43::NetDriver<'static>>) -> ! {
|
||||
runner.run().await
|
||||
}
|
||||
|
||||
#[panic_handler]
|
||||
fn panic( info: &PanicInfo) -> ! {
|
||||
error!("{}", info);
|
||||
loop { }
|
||||
}
|
||||
|
||||
fn run_motor(pwm_c: &mut pwm::Config, pwm: &mut Pwm, pulse: u16) {
|
||||
pwm_c.compare_b = pulse;
|
||||
pwm.set_config(&pwm_c);
|
||||
}
|
||||
|
||||
async fn open_door(mut pwm_c: &mut pwm::Config, mut pwm: &mut Pwm<'_>) {
|
||||
run_motor(&mut pwm_c, &mut pwm, 0x13DF); // up
|
||||
Timer::after_millis(3000).await;
|
||||
run_motor(&mut pwm_c, &mut pwm, 0x123C); // down
|
||||
Timer::after_millis(750).await;
|
||||
run_motor(&mut pwm_c, &mut pwm, 4687); // stop
|
||||
}
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(spawner: Spawner) {
|
||||
|
||||
|
||||
let mut motor = ledc.get_channel(channel::Number::Channel0, pin);
|
||||
let p = embassy_rp::init(Default::default());
|
||||
let mut rng = RoscRng;
|
||||
|
||||
motor
|
||||
.configure(channel::config::Config {
|
||||
timer: &lstimer0,
|
||||
duty_pct: 0,
|
||||
pin_config: channel::config::PinConfig::PushPull,
|
||||
})
|
||||
.unwrap();
|
||||
let driver = Driver::new(p.USB, Irqs);
|
||||
spawner.spawn(logger_task(driver)).unwrap();
|
||||
|
||||
println!("max duty {}", motor.max_duty_cycle());
|
||||
motor.set_duty_cycle(1229).unwrap();
|
||||
let mut c: pwm::Config = Default::default();
|
||||
c.divider = 40.into();
|
||||
c.top = 62_500; // 20ms
|
||||
c.compare_b = 4687; // 1.5ms
|
||||
let mut pwm = Pwm::new_output_b(p.PWM_SLICE1, p.PIN_3, c.clone());
|
||||
|
||||
esp_alloc::heap_allocator!(72 * 1024);
|
||||
let fw = include_bytes!("../../cyw43-firmware/43439A0.bin");
|
||||
let clm = include_bytes!("../../cyw43-firmware/43439A0_clm.bin");
|
||||
|
||||
esp_println::logger::init_logger_from_env();
|
||||
// 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 download 43439A0.bin --binary-format bin --chip RP2040 --base-address 0x10100000
|
||||
// probe-rs download 43439A0_clm.bin --binary-format bin --chip RP2040 --base-address 0x10140000
|
||||
//let fw = unsafe { core::slice::from_raw_parts(0x10100000 as *const u8, 230321) };
|
||||
//let clm = unsafe { core::slice::from_raw_parts(0x10140000 as *const u8, 4752) };
|
||||
|
||||
let timg0 = esp_hal::timer::timg::TimerGroup::new(peripherals.TIMG0);
|
||||
let mut rng = Rng::new(peripherals.RNG);
|
||||
let init = esp_wifi::init(
|
||||
esp_wifi::EspWifiInitFor::Wifi,
|
||||
timg0.timer0,
|
||||
rng.clone(),
|
||||
peripherals.RADIO_CLK,
|
||||
)
|
||||
.unwrap();
|
||||
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, Irqs);
|
||||
let spi = PioSpi::new(&mut pio.common, pio.sm0, pio.irq0, cs, p.PIN_24, p.PIN_29, p.DMA_CH0);
|
||||
|
||||
let mut wifi = peripherals.WIFI;
|
||||
let mut socket_set_entries: [SocketStorage; 3] = Default::default();
|
||||
let (iface, device, mut controller, socket_set) =
|
||||
create_network_interface(&init, &mut wifi, WifiStaDevice, &mut socket_set_entries).unwrap();
|
||||
static STATE: StaticCell<cyw43::State> = StaticCell::new();
|
||||
let state = STATE.init(cyw43::State::new());
|
||||
let (net_device, mut control, runner) = cyw43::new(state, pwr, spi, fw).await;
|
||||
defmt::unwrap!(spawner.spawn(cyw43_task(runner)));
|
||||
|
||||
let now = || time::now().duration_since_epoch().to_millis();
|
||||
let mut stack = Stack::new(iface, device, socket_set, now, rng.random());
|
||||
control.init(clm).await;
|
||||
control
|
||||
.set_power_management(cyw43::PowerManagementMode::PowerSave)
|
||||
.await;
|
||||
|
||||
let client_config = Configuration::Client(ClientConfiguration {
|
||||
ssid: "🐟".try_into().unwrap(),
|
||||
password: "ilcougars1234".try_into().unwrap(),
|
||||
..Default::default()
|
||||
// Use a link-local address for communication without DHCP server
|
||||
let config = Config::ipv4_static(embassy_net::StaticConfigV4 {
|
||||
address: embassy_net::Ipv4Cidr::new(embassy_net::Ipv4Address::new(169, 254, 1, 1), 16),
|
||||
dns_servers: heapless::Vec::new(),
|
||||
gateway: None,
|
||||
});
|
||||
let res = controller.set_configuration(&client_config);
|
||||
log::info!("{:?}",res);
|
||||
|
||||
controller.start().unwrap();
|
||||
// Generate random seed
|
||||
let seed = rng.next_u64();
|
||||
|
||||
let res: Result<(heapless::Vec<AccessPointInfo, 10>, usize), WifiError> = controller.scan_n();
|
||||
if let Ok((res, _count)) = res {
|
||||
for ap in res {
|
||||
log::info!("{:?}", ap);
|
||||
}
|
||||
}
|
||||
// Init network stack
|
||||
static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new();
|
||||
let (stack, runner) = embassy_net::new(net_device, config, RESOURCES.init(StackResources::new()), seed);
|
||||
|
||||
defmt::unwrap!(spawner.spawn(net_task(runner)));
|
||||
|
||||
//control.start_ap_open("door409", 5).await;
|
||||
control.start_ap_wpa2("door409", "babelite", 5).await;
|
||||
|
||||
let mut rx_buffer = [0; 4096];
|
||||
let mut tx_buffer = [0; 4096];
|
||||
let mut buf = [0; 4096];
|
||||
|
||||
log::info!("connecting: {:?}", controller.connect());
|
||||
loop {
|
||||
match controller.is_connected() {
|
||||
Ok(true) => break,
|
||||
Ok(false) => {}
|
||||
Err(err) => {
|
||||
log::error!("{:?}", err);
|
||||
log::info!("connecting: {:?}", controller.connect());
|
||||
}
|
||||
}
|
||||
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
||||
socket.set_timeout(Some(Duration::from_secs(3)));
|
||||
|
||||
let mut c: pwm::Config = Default::default();
|
||||
c.divider = 40.into();
|
||||
c.top = 62_500; // 20ms
|
||||
// 3125 counts per ms
|
||||
c.compare_b = 4687; // 1.5ms
|
||||
pwm.set_config(&c);
|
||||
|
||||
control.gpio_set(0, false).await;
|
||||
info!("Listening on TCP:1234...");
|
||||
if let Err(e) = socket.accept(1234).await {
|
||||
warn!("accept error: {:?}", e);
|
||||
continue;
|
||||
}
|
||||
|
||||
log::info!("the wifi device is connected");
|
||||
info!("Received connection from {:?}", socket.remote_endpoint());
|
||||
control.gpio_set(0, true).await;
|
||||
|
||||
stack
|
||||
.set_iface_configuration(&ipv4::Configuration::Client(
|
||||
ipv4::ClientConfiguration::Fixed(
|
||||
ipv4::ClientSettings {
|
||||
ip: ipv4::Ipv4Addr::new(169,254,2,2),
|
||||
subnet: ipv4::Subnet {
|
||||
gateway: ipv4::Ipv4Addr::new(169, 254, 2, 1),
|
||||
mask: ipv4::Mask(16),
|
||||
loop {
|
||||
let mut n = match socket.read(&mut buf).await {
|
||||
Ok(0) => {
|
||||
warn!("read EOF");
|
||||
break;
|
||||
}
|
||||
Ok(n) => n,
|
||||
Err(e) => {
|
||||
warn!("read error: {:?}", e);
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
info!("rxd {}", from_utf8(&buf[..n]).unwrap());
|
||||
Timer::after_millis(30).await;
|
||||
|
||||
let mut segs = buf[..n].trim_ascii().split(|c| *c == ' ' as u8);
|
||||
|
||||
match char::from_u32(segs.next().unwrap()[0] as u32).unwrap() {
|
||||
'D' => {
|
||||
let pwr: [u8;2] = hex::FromHex::from_hex(segs.next().unwrap()).unwrap();
|
||||
info!("power {pwr:?}");
|
||||
|
||||
let pwr = u16::from_be_bytes(pwr);
|
||||
c.compare_b = pwr;
|
||||
info!("power {pwr:?}");
|
||||
pwm.set_config(&c);
|
||||
},
|
||||
dns: None,
|
||||
secondary_dns: None,
|
||||
'B' => {
|
||||
let buf: [u8;8] = hex::FromHex::from_hex(segs.next().unwrap()).unwrap();
|
||||
let card: u64 = u64::from_ne_bytes(buf);
|
||||
info!("card {card:#16x}");
|
||||
|
||||
open_door(&mut c, &mut pwm).await;
|
||||
},
|
||||
),
|
||||
))
|
||||
.unwrap();
|
||||
_ => {}
|
||||
|
||||
let mut rx_buffer = [0u8; 1536];
|
||||
let mut tx_buffer = [0u8; 1536];
|
||||
let mut socket = stack.get_socket(&mut rx_buffer, &mut tx_buffer);
|
||||
|
||||
socket.listen(4595).unwrap();
|
||||
|
||||
loop {
|
||||
loop {
|
||||
match controller.is_connected() {
|
||||
Ok(true) => break,
|
||||
Ok(false) => {}
|
||||
Err(err) => {
|
||||
log::error!("{:?}", err);
|
||||
log::info!("connecting: {:?}", controller.connect());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
socket.work();
|
||||
//let mut response: [u8;2] = [0;2];
|
||||
|
||||
if !socket.is_open() {
|
||||
socket.listen(4595).unwrap();
|
||||
}
|
||||
//let _ = bus.read_async(0xC0u16, &mut response).await;
|
||||
//let _ = hex::encode_to_slice(response, &mut buf);
|
||||
|
||||
if socket.is_connected() {
|
||||
let mut time_out = false;
|
||||
let deadline = time::now() + Duration::secs(3);
|
||||
let mut buffer = [0u8; 1024];
|
||||
let mut pos = 0;
|
||||
log::info!("connected");
|
||||
'read: loop {
|
||||
while !socket.read_ready().unwrap_or(true) {
|
||||
if time::now() > deadline {
|
||||
println!("Timeout");
|
||||
time_out = true;
|
||||
break 'read;
|
||||
}
|
||||
}
|
||||
if let Ok(len) = socket.read(&mut buffer[pos..]) {
|
||||
let to_print = unsafe { core::str::from_utf8_unchecked(&buffer[..(pos + len)]) };
|
||||
|
||||
println!("recv {}", to_print);
|
||||
if to_print.contains("\r\n") {
|
||||
print!("{}", to_print);
|
||||
println!();
|
||||
pos += len;
|
||||
match socket.write_all(&buf[..n]).await {
|
||||
Ok(()) => {}
|
||||
Err(e) => {
|
||||
warn!("write error: {:?}", e);
|
||||
break;
|
||||
}
|
||||
|
||||
pos += len;
|
||||
|
||||
if time::now() > deadline {
|
||||
println!("Timeout");
|
||||
time_out = true;
|
||||
break;
|
||||
};
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
handle(&buffer[..pos], &mut motor, &mut socket);
|
||||
|
||||
if !time_out {
|
||||
let _ = socket.write_all(
|
||||
b"test\r\n"
|
||||
);
|
||||
|
||||
socket.flush().unwrap();
|
||||
}
|
||||
while time::now() < deadline && socket.is_connected() {}
|
||||
socket.close();
|
||||
log::info!("ending transaction");
|
||||
// open: 0x0533
|
||||
// close: 0x04c8
|
||||
motor.set_duty_cycle(1229).unwrap();
|
||||
}
|
||||
|
||||
//let deadline = time::now() + Duration::secs(5);
|
||||
//while time::now() < deadline {
|
||||
// socket.work();
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -113,6 +113,8 @@ async fn main(spawner: Spawner) {
|
|||
let mut tx = [0; 2048];
|
||||
let mut uart = BufferedUart::new(p.UART1, Irqs, p.PIN_20, p.PIN_21, &mut tx, &mut rx, config);
|
||||
|
||||
let button = Input::new(p.PIN_22, embassy_rp::gpio::Pull::Up);
|
||||
|
||||
let mut rng = RoscRng;
|
||||
|
||||
let driver = Driver::new(p.USB, Irqs);
|
||||
|
@ -160,7 +162,17 @@ async fn main(spawner: Spawner) {
|
|||
|
||||
defmt::unwrap!(spawner.spawn(net_task(runner)));
|
||||
|
||||
control.start_ap_wpa2("🐟", "ilcougars1234", 5).await;
|
||||
loop {
|
||||
match control
|
||||
.join("door409", JoinOptions::new("babelite".as_bytes()))
|
||||
.await
|
||||
{
|
||||
Ok(_) => break,
|
||||
Err(err) => {
|
||||
info!("join failed with status={}", err.status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut rx_buffer = [0; 4096];
|
||||
let mut tx_buffer = [0; 4096];
|
||||
|
@ -205,6 +217,7 @@ async fn main(spawner: Spawner) {
|
|||
|
||||
loop {
|
||||
let card = CHANNEL.receive().await;
|
||||
//join(CHANNEL.receive().await, button.wait_for_falling_edge())
|
||||
|
||||
let num = (RoscRng::next_u32(&mut rng) % 5) + 1;
|
||||
uart.write_all(b"AT+PLAYFILE=/").await.unwrap();
|
||||
|
@ -218,7 +231,7 @@ async fn main(spawner: Spawner) {
|
|||
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
||||
socket.set_timeout(Some(Duration::from_secs(1)));
|
||||
info!("attempting conn to inside");
|
||||
if let Err(e) = socket.connect(IpEndpoint::new(Ipv4Address::new(169, 254, 2, 2).into_address(), 4595)).await {
|
||||
if let Err(e) = socket.connect(IpEndpoint::new(Ipv4Address::new(169, 254, 1, 1).into_address(), 1234)).await {
|
||||
warn!("connect error: {:?}", e);
|
||||
continue;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue