Basic project setup (#1)

Reviewed-on: #1
Co-authored-by: Andreas Tsouchlos <an.tsouchlos@gmail.com>
Co-committed-by: Andreas Tsouchlos <an.tsouchlos@gmail.com>
This commit is contained in:
Andreas Tsouchlos 2024-11-01 23:49:53 +00:00 committed by an.tsouchlos
parent bc6bf765fa
commit 05dd72f55c
10 changed files with 10865 additions and 9 deletions

15
.cargo/config.toml Normal file
View File

@ -0,0 +1,15 @@
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
# replace nRF82810_xxAA with your chip as listed in `probe-rs chip list`
runner = "probe-rs run --chip nRF52832_xxAA"
[alias]
rr = "run --release"
f = "flash --chip nRF52810_xxAA --reset-halt"
fr = "flash --chip nRF52810_xxAA --release --reset-halt"
[build]
target = "thumbv7em-none-eabi"
[env]
DEFMT_LOG = "trace"

9
.gitignore vendored
View File

@ -1,8 +1,3 @@
# ---> esp-idf
# gitignore template for esp-idf, the official development framework for ESP32
# https://github.com/espressif/esp-idf
build/
sdkconfig
sdkconfig.old
# Added by cargo
/target

1163
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

25
Cargo.toml Normal file
View File

@ -0,0 +1,25 @@
[package]
name = "commonsense-sw"
version = "0.1.0"
edition = "2021"
[dependencies]
atomic-pool = "1.0.1"
cortex-m = { version = "0.7.7", features = ["inline-asm"] }
cortex-m-rt = "0.7.3"
defmt = "0.3.8"
defmt-rtt = "0.4.1"
embassy-executor = { version = "0.6.1", features = ["task-arena-size-8192", "arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "integrated-timers"] }
embassy-futures = "0.1.1"
embassy-nrf = { version = "0.2.0", features = ["defmt", "nrf52832", "time-driver-rtc1", "gpiote", "unstable-pac", "time"] }
embassy-sync = { version = "0.6.0", features = ["defmt"] }
embassy-time = { version = "0.3.2", features = ["defmt", "defmt-timestamp-uptime"] }
embedded-storage = "0.3.1"
embedded-storage-async = "0.4.1"
fixed = "1.28.0"
futures = { version = "0.3.31", default-features = false }
heapless = "0.8.0"
nrf-softdevice = { version = "0.1.0", features = ["defmt", "ble-peripheral", "ble-central", "critical-section-impl", "nrf52832", "s132"] }
nrf-softdevice-s132 = "0.1.2"
panic-probe = { version = "0.3.2", features = ["print-defmt"] }
static_cell = "2.1.0"

4
Dockerfile Normal file
View File

@ -0,0 +1,4 @@
FROM rust:latest
RUN rustup target add thumbv7em-none-eabi
RUN curl --proto '=https' --tlsv1.2 -LsSf https://github.com/probe-rs/probe-rs/releases/latest/download/probe-rs-tools-installer.sh | sh

View File

@ -1,3 +1,68 @@
# CommonSense_SW
# CommonSense
Firmware for the CommonSense project.
Firmware for the CommonSense project.
## Compile and Run
The firmware depends on the S132 softdevice. Before running the firmware, the
softdevice has to flashed to the chip.
### Flashing the Softdevice
1. Erase the chip
```bash
$ probe-rs erase --chip nRF52832_xxAA
```
2. Flash the softdevice
```bash
$ probe-rs download --verify --binary-format hex --chip nRF52832_xxAA s132_nrf52_7.3.0_softdevice.hex
```
### Compile and Run
1. Install the required tools. Consult `Dockerfile` for specifics
2. Build the firmware
```bash
$ cargo build --release
```
3. Flash / run the firmware
```bash
$ cargo fr
$ # OR
$ cargo run --release
```
### Compile and Run using Docker
1. Build the docker image
```bash
$ docker build . -t commonsense
```
2. Build the firmware
```bash
$ docker run --rm -v $PWD:$PWD -w $PWD -u $(id -u):$(id -g) commonsense cargo build --release
```
3. Flash / run the firmware
```bash
$ docker run --privileged -v /dev/bus/usb/:/dev/bus/usb --rm -v $PWD:$PWD -w $PWD -u $(id -u):$(id -g) commonsense cargo fr
$ # OR
$ docker run --privileged -v /dev/bus/usb/:/dev/bus/usb --rm -v $PWD:$PWD -w $PWD -u $(id -u):$(id -g) commonsense cargo run --release
```
## Notes
- If flashing fails after being successful the first time, take a look at this:
https://users.rust-lang.org/t/probe-rs-fails-to-work-after-first-time-use-successful/103234
- This may be useful for unit testing: https://crates.io/crates/embedded-test
- Currently this project is being developed on the nrf52-dk board with the
nrf52832 chip. What mature enough, it will be ported to the nrf52810 to run
on the actual hardware. Locations to modify:
- `memory.x` - consult the
[nrf-softdevice](https://github.com/embassy-rs/nrf-softdevice)
documentation for more information
- `Cargo.toml` - change the `nrf52832` feature to `nrf52810`
- `.cargo/config.toml` - change the `nrf52832` chip to `nrf52810`
- TODO: Find out how to properly set up the license (the softdevice is
proprietary)
- If the device locks up, use the
[nrf-recover](https://docs.rs/crate/nrf-recover/latest) utility

35
build.rs Normal file
View File

@ -0,0 +1,35 @@
//! 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=-Tdefmt.x");
}

13
memory.x Normal file
View File

@ -0,0 +1,13 @@
/*MEMORY
{
FLASH : ORIGIN = 0x00000000, LENGTH = 256K
RAM : ORIGIN = 0x20000000, LENGTH = 24K
}*/
MEMORY
{
/* NOTE 1 K = 1 KiBi = 1024 bytes */
/* NRF52832 with Softdevice S132 7.x and 6.x */
FLASH : ORIGIN = 0x00026000, LENGTH = 512K - 152K
RAM : ORIGIN = 0x20000000 + 30K, LENGTH = 64K - 30K
}

File diff suppressed because it is too large Load Diff

25
src/main.rs Normal file
View File

@ -0,0 +1,25 @@
#![no_std]
#![no_main]
use defmt::info;
use embassy_executor::Spawner;
use embassy_nrf::gpio::{Level, Output, OutputDrive};
use embassy_time::Timer;
use {defmt_rtt as _, panic_probe as _};
use nrf_softdevice::{raw, Softdevice};
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_nrf::init(Default::default());
let mut led = Output::new(p.P0_18, Level::Low, OutputDrive::Standard);
info!("Starting blinky");
loop {
led.set_high();
Timer::after_millis(300).await;
led.set_low();
Timer::after_millis(300).await;
}
}