Skip to content
Snippets Groups Projects
Commit 47725e95 authored by Samuel Tardieu's avatar Samuel Tardieu
Browse files

Move chip identification to its own crate

parent 9b4b41f8
No related branches found
No related tags found
No related merge requests found
...@@ -53,6 +53,13 @@ version = "1.0.0" ...@@ -53,6 +53,13 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "common"
version = "0.1.0"
dependencies = [
"embassy-stm32",
]
[[package]] [[package]]
name = "cortex-m" name = "cortex-m"
version = "0.7.7" version = "0.7.7"
...@@ -131,6 +138,7 @@ dependencies = [ ...@@ -131,6 +138,7 @@ dependencies = [
name = "dc-motor-driver-hat" name = "dc-motor-driver-hat"
version = "0.2.0" version = "0.2.0"
dependencies = [ dependencies = [
"common",
"cortex-m", "cortex-m",
"cortex-m-rt", "cortex-m-rt",
"critical-section", "critical-section",
......
[workspace] [workspace]
members = ["controller", "i2c2-target"] members = [ "common","controller", "i2c2-target"]
exclude = ["pid"] exclude = ["pid"]
resolver = "2" resolver = "2"
......
[package]
name = "common"
version = "0.1.0"
edition = "2021"
[dependencies]
embassy-stm32 = { git = "https://github.com/embassy-rs/embassy", features = ["unstable-pac"] }
#![no_std]
use embassy_stm32::pac;
pub fn mcu_kind() -> (&'static str, &'static str) {
let dbgmcu = pac::DBGMCU.idcode().read();
match (dbgmcu.dev_id(), dbgmcu.rev_id()) {
(0x412, 0x1000) => ("low-density", "A"),
(0x412, _) => ("low-density", "?"),
(0x410, 0x0000) => ("medium-density", "A"),
(0x410, 0x2000) => ("medium-density", "B"),
(0x410, 0x2001) => ("medium-density", "Z"),
(0x410, 0x2003) => ("medium-density", "1/2/3/X/Y"),
(0x414, 0x1000) => ("high-density", "1/A"),
(0x414, 0x1001) => ("high-density", "Z"),
(0x414, 0x1003) => ("high-density", "1/2/3/X/Y"),
(0x414, _) => ("high-density", "?"),
(0x430, 0x1000) => ("XL-density", "1/A"),
(0x430, _) => ("XL-density", "?"),
(0x418, 0x1000) => ("connectivity", "A"),
(0x418, 0x1001) => ("connectivity", "Z"),
(0x418, _) => ("connectivity", "?"),
(_, _) => ("?", "?"),
}
}
pub fn flash_size() -> usize {
usize::from(unsafe { *(0x1FFF_F7E0 as *const u16) })
}
pub fn identity() -> Option<(u32, u32, &'static str)> {
let pid = 0xe00f_ffe0 as *const [u32; 3];
let pid = unsafe { *pid };
(pid[2] & 8 != 0).then(|| {
let identity_code = ((pid[1] & 0xf0) >> 4) | ((pid[2] & 0x7) << 4);
let continuation_code = unsafe { *(0xe00f_ffd0 as *const u32) };
let device = match (identity_code, continuation_code) {
(0x20, 0x00) => "STM32",
(0x3b, 0x04) => "APM32",
_ => "?",
};
(identity_code, continuation_code, device)
})
}
pub fn device_id() -> &'static [u8; 8] {
unsafe { &*(0x1fff_f7e8 as *const [u8; 8]) }
}
...@@ -5,6 +5,7 @@ authors = ["Samuel Tardieu <sam@rfc1149.net>"] ...@@ -5,6 +5,7 @@ authors = ["Samuel Tardieu <sam@rfc1149.net>"]
edition = "2021" edition = "2021"
[dependencies] [dependencies]
common = { version = "0.1.0", path = "../common" }
cortex-m = { version = "0.7.7", features = ["critical-section-single-core", "inline-asm"] } cortex-m = { version = "0.7.7", features = ["critical-section-single-core", "inline-asm"] }
cortex-m-rt = "0.7.3" cortex-m-rt = "0.7.3"
critical-section = "1.1.2" critical-section = "1.1.2"
......
use embassy_stm32::pac;
pub fn identify() { pub fn identify() {
let dbgmcu = pac::DBGMCU.idcode().read(); let (density, rev) = common::mcu_kind();
let (density, rev) = match (dbgmcu.dev_id(), dbgmcu.rev_id()) { let flash_size = common::flash_size();
(0x412, 0x1000) => ("low-density", "A"),
(0x412, _) => ("low-density", "?"),
(0x410, 0x0000) => ("medium-density", "A"),
(0x410, 0x2000) => ("medium-density", "B"),
(0x410, 0x2001) => ("medium-density", "Z"),
(0x410, 0x2003) => ("medium-density", "1/2/3/X/Y"),
(0x414, 0x1000) => ("high-density", "1/A"),
(0x414, 0x1001) => ("high-density", "Z"),
(0x414, 0x1003) => ("high-density", "1/2/3/X/Y"),
(0x414, _) => ("high-density", "?"),
(0x430, 0x1000) => ("XL-density", "1/A"),
(0x430, _) => ("XL-density", "?"),
(0x418, 0x1000) => ("connectivity", "A"),
(0x418, 0x1001) => ("connectivity", "Z"),
(0x418, _) => ("connectivity", "?"),
(_, _) => ("?", "?"),
};
let flash_size = unsafe { *(0x1FFF_F7E0 as *const u16) };
defmt::info!( defmt::info!(
"Device type: {}, revision: {} – flash size: {}kB", "Device type: {}, revision: {} – flash size: {}kB",
density, density,
...@@ -28,15 +8,18 @@ pub fn identify() { ...@@ -28,15 +8,18 @@ pub fn identify() {
flash_size flash_size
); );
let pid = 0xe00f_ffe0 as *const [u32; 3]; if let Some((identity_code, continuation_code, device)) = common::identity() {
let pid = unsafe { *pid }; defmt::info!(
let used = pid[2] & 8 != 0; "device = {} (identity code = {:#04x}, continuation code = {:#04x})",
let identity_code = ((pid[1] & 0xf0) >> 4) | ((pid[2] & 0x7) << 4); device,
let continuation_code = unsafe { *(0xe00f_ffd0 as *const u32) }; identity_code,
defmt::info!( continuation_code
"used = {}, identity_code = {:#04x}, continuation_code = {:#04x}", );
used, } else {
identity_code, defmt::info!("no identity and continuation codes");
continuation_code }
); }
pub fn device_id() -> &'static [u8; 8] {
unsafe { &*(0x1fff_f7e8 as *const [u8; 8]) }
} }
use crate::{encoders::Encoders, tb6612fng::Tb6612fng}; use crate::{chip, encoders::Encoders, tb6612fng::Tb6612fng};
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_stm32::{peripherals::WWDG, time::hz}; use embassy_stm32::{peripherals::WWDG, time::hz};
use futures::FutureExt; use futures::FutureExt;
...@@ -156,8 +156,7 @@ fn process_command( ...@@ -156,8 +156,7 @@ fn process_command(
cortex_m::peripheral::SCB::sys_reset(); cortex_m::peripheral::SCB::sys_reset();
} }
[CMD_DEVICE_ID] => { [CMD_DEVICE_ID] => {
let dev_id = unsafe { &*(0x1fff_f7e8 as *const [u8; 8]) }; for &b in chip::device_id() {
for &b in dev_id {
response.push(b).unwrap(); response.push(b).unwrap();
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment