Skip to content

Commit

Permalink
Merge branch 'feat/cli-commands'
Browse files Browse the repository at this point in the history
  • Loading branch information
ckrenslehner committed Dec 27, 2024
2 parents 5c03572 + 947587b commit 0bd5a97
Show file tree
Hide file tree
Showing 18 changed files with 1,910 additions and 733 deletions.
23 changes: 22 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ The justfile needs `just` and `nu` to be installed.

## Status
Working functionality:
- Discover ST-Link probes
- Connect to a target
- Write hex/bin file to target
- Save memory to file
- Mass erase memory
- Upgrade BLE stack
- Reset
- Get general device information

Functionality to be added:
- Enable readout protection
- Disable readout protection
- And much more.. PRs are more than welcome! 😊
- Read memory
- As bytes, half words, words
- As struct (needs to implement bytemuck::Pod + bytemuck::Zeroable)
- Firmware Update Service (only STM32WB5x/35xx)
- Read installed versions of FUS and BLE stack
- Delete BLE stack firmware
- Upgrade BLE stack firmware

Functionality to be added:
- There are still many functions in `STM32CubeProgrammer_API.chm` which are not yet implemented.
- PRs are more than welcome! 😊

## Platform support
- Windows: Tested
Expand Down
17 changes: 17 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
# Nushell needs to be installeds
set shell := ["nu", "-c"]

shebang := if os() == 'windows' {
'nu.exe'
} else {
'/usr/bin/env nu'
}

# List all the recipes
default:
just -l

sample-env-file:
#!{{shebang}}
let content = 'STM32_CUBE_PROGRAMMER_DIR = "<PATH TO STM32_CUBE_PROGRAMMER ROOT DIR>"
STM32_CUBE_PROGRAMMER_DOWNLOAD_HEX_PATH = "<PATH TO HEX FILE>"
STM32_CUBE_PROGRAMMER_DOWNLOAD_BIN_PATH = "<PATH TO BIN FILE>"
STM32_CUBE_PROGRAMMER_DOWNLOAD_BIN_START_ADDRESS = "<START ADDRESS e.g. 0x08000000>"
STM32_CUBE_PROGRAMMER_BLE_STACK_PATH = "<PATH TO BLE STACK BIN FILE>"
STM32_CUBE_PROGRAMMER_BLE_STACK_START_ADDRESS = "<START ADDRESS e.g. 0x080CE000>"'

echo $content | save .env

# Run all tests or a specific test with a specific verbosity
# The log level maps to the `log` crate log levels: trace, debug, info, warn, error
test name="" log_level="trace":
Expand Down
5 changes: 5 additions & 0 deletions stm32cubeprogrammer-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ name = "stm32cubeprogrammer-cli"
version = "0.1.0"
edition = "2021"

authors = ["Christian Krenslehner"]
description = "CLI for the STM32CubeProgrammer API"
license = "MIT"
repository = "https://github.com/ckrenslehner/stm32cubeprogrammer-rs"

[dependencies]
clap = { version = "4.5.22", features = ["derive"] }
clap-verbosity-flag = "3.0.1"
Expand Down
90 changes: 90 additions & 0 deletions stm32cubeprogrammer-cli/src/display_handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use std::borrow::Cow;

use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use indicatif_log_bridge::LogWrapper;

/// Display handler which wraps a progress bar and a logger
#[derive(Debug)]
pub struct DisplayHandler {
_multi: MultiProgress,
progress_bar: indicatif::ProgressBar,
message: Cow<'static, str>,
}

impl DisplayHandler {
/// Create a new display handler which combines a progress bar and a logger
pub fn new(logger: env_logger::Logger) -> Self {
let multi = MultiProgress::new();

// Installs new global logger
LogWrapper::new(multi.clone(), logger).try_init().unwrap();

let progress_bar = multi.add(ProgressBar::new(0));
progress_bar.set_style(
ProgressStyle::default_bar()
.template("{msg} - {percent}% - [{wide_bar:.cyan/blue}]")
.unwrap()
.progress_chars("#>-"),
);

Self {
_multi: multi,
progress_bar,
message: Cow::Owned(String::new()),
}
}

pub fn set_message(&mut self, message: impl Into<Cow<'static, str>>) {
self.message = message.into();
}
}

/// Implement the display callback trait for the display handler
/// This allows showing progress and log messages from CubeProgrammer API
impl stm32cubeprogrammer::DisplayCallback for DisplayHandler {
fn init_progressbar(&self) {
self.progress_bar.set_message(self.message.clone());
self.progress_bar.set_length(0);
self.progress_bar.set_position(0);
}

fn log_message(&self, message_type: stm32cubeprogrammer::LogMessageType, message: &str) {
if message.is_empty() || message == "\n" || message == "\r\n" {
return;
}

match message_type {
stm32cubeprogrammer::LogMessageType::Normal => log::info!("{}", message),
stm32cubeprogrammer::LogMessageType::Info => log::info!("{}", message),
stm32cubeprogrammer::LogMessageType::GreenInfo => log::info!("{}", message),
stm32cubeprogrammer::LogMessageType::GreenInfoNoPopup => log::info!("{}", message),

stm32cubeprogrammer::LogMessageType::Warning => log::warn!("{}", message),
stm32cubeprogrammer::LogMessageType::WarningNoPopup => log::warn!("{}", message),

stm32cubeprogrammer::LogMessageType::Error => log::error!("{}", message),
stm32cubeprogrammer::LogMessageType::ErrorNoPopup => log::error!("{}", message),

stm32cubeprogrammer::LogMessageType::Verbosity1 => log::info!("{}", message),
stm32cubeprogrammer::LogMessageType::Verbosity2 => log::debug!("{}", message),
stm32cubeprogrammer::LogMessageType::Verbosity3 => log::trace!("{}", message),

_ => {}
}
}

fn update_progressbar(&self, current_number: u64, total_number: u64) {
if current_number == total_number {
self.progress_bar.finish();
return;
}

if let Some(current_length) = self.progress_bar.length() {
if current_length != total_number {
self.progress_bar.set_length(total_number);
}
}

self.progress_bar.set_position(current_number);
}
}
Loading

0 comments on commit 0bd5a97

Please sign in to comment.