fix formatting, fix bitfield offsets

This commit is contained in:
hanatonikkou 2026-04-28 20:01:34 +02:00
parent 4bb8b55777
commit d69a03393b
6 changed files with 238 additions and 220 deletions

View file

@ -377,9 +377,13 @@ impl ClockManagerImpl {
// check the clock muxes to select the right source for clktop // check the clock muxes to select the right source for clktop
let mux0 = sysctrl.r(utra::sysctrl::SFR_CGUSEL0) & 0x1; let mux0 = sysctrl.r(utra::sysctrl::SFR_CGUSEL0) & 0x1;
let mux1 = sysctrl.r(utra::sysctrl::SFR_CGUSEL1) & 0x1; let mux1 = sysctrl.r(utra::sysctrl::SFR_CGUSEL1) & 0x1;
let clktop = if mux0 == 1 { pll0_freq } let clktop = if mux0 == 1 {
else if mux1 == 1 { XTAL0 } pll0_freq
else { OSC }; } else if mux1 == 1 {
XTAL0
} else {
OSC
};
let fclk_fd = sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0) & 0xFF; let fclk_fd = sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0) & 0xFF;
let aclk_fd = sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_1) & 0xFF; let aclk_fd = sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_1) & 0xFF;
let hclk_fd = sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_2) & 0xFF; let hclk_fd = sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_2) & 0xFF;
@ -455,9 +459,13 @@ impl ClockManagerImpl {
// check the clock muxes to select the right source for clktop // check the clock muxes to select the right source for clktop
let mux0 = self.sysctrl.r(utra::sysctrl::SFR_CGUSEL0) & 0x1; let mux0 = self.sysctrl.r(utra::sysctrl::SFR_CGUSEL0) & 0x1;
let mux1 = self.sysctrl.r(utra::sysctrl::SFR_CGUSEL1) & 0x1; let mux1 = self.sysctrl.r(utra::sysctrl::SFR_CGUSEL1) & 0x1;
let clktop = if mux0 == 1 { pll0_freq } let clktop = if mux0 == 1 {
else if mux1 == 1 { XTAL0 } pll0_freq
else { OSC }; } else if mux1 == 1 {
XTAL0
} else {
OSC
};
let fclk_fd = self.sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0) & 0xFF; let fclk_fd = self.sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0) & 0xFF;
let aclk_fd = self.sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_1) & 0xFF; let aclk_fd = self.sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_1) & 0xFF;
let hclk_fd = self.sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_2) & 0xFF; let hclk_fd = self.sysctrl.r(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_2) & 0xFF;

View file

@ -1,25 +1,20 @@
#[allow(unused)] // time::Duration is needed in send_read but the compiler doesn't realize it
use std::time::{Duration, Instant};
use arbitrary_int::{Number, u5}; use arbitrary_int::{Number, u5};
use bao1x_api::bio::*; use bao1x_api::bio::*;
use bao1x_api::bio_code; use bao1x_api::bio_code;
use bao1x_api::bio_resources::*; use bao1x_api::bio_resources::*;
use bao1x_hal::bio::{Bio, CoreCsr}; use bao1x_hal::bio::{Bio, CoreCsr};
use bao1x_hal::clocks::ClockOp; use bao1x_hal::clocks::ClockOp;
use num_traits::cast::{ToPrimitive}; use num_traits::cast::ToPrimitive;
#[allow(unused)] // time::Duration is needed in send_read but the compiler doesn't realize it
use std::time::{Duration, Instant};
use utralib::generated::utra::bio_bdma; use utralib::generated::utra::bio_bdma;
use xous_api_susres::api::Opcode as SusresOp; use xous_api_susres::api::Opcode as SusresOp;
mod protocol; mod protocol;
use protocol::*; use protocol::*;
pub use protocol::{ pub use protocol::{
Address, Address, Brightness, Command, Part207LedDriver, SpecCmdData, SpecialCommand101, SpecialCommand110,
Brightness,
Command,
Part207LedDriver,
SpecialCommand101,
SpecialCommand110,
SpecCmdData,
bitflags, bitflags,
}; };
@ -29,7 +24,7 @@ pub use protocol::{
* Dali stands for Digitally Addressable Lighting Interface and is a standard for controlling building * Dali stands for Digitally Addressable Lighting Interface and is a standard for controlling building
* lighting. The naming convention is a bit confusing: * lighting. The naming convention is a bit confusing:
* *
* Application controller A bus master, used to control lighting: that's what this library is form * Application controller A bus master, used to control lighting: that's what this library is for
* Control gear A device directly controlling lights, e.g. an LED driver * Control gear A device directly controlling lights, e.g. an LED driver
* Control device An input device, e.g. a light sensor or a dimmer * Control device An input device, e.g. a light sensor or a dimmer
* *
@ -53,8 +48,8 @@ pub use protocol::{
* *
* Limitations of this library: * Limitations of this library:
* 1) It's not been tested with actual devices. * 1) It's not been tested with actual devices.
* 2) Not all device types are implemented. To add a new device type, implement an enum with trait DataByte, * 2) Not all device types are implemented. To add a new device type, implement an enum with trait
* equivalent to Extension207LedDriver. * DataByte, equivalent to Extension207LedDriver.
* 3) DALI-2 adds support for control devices such as sensors and dimmers, which are not supported. * 3) DALI-2 adds support for control devices such as sensors and dimmers, which are not supported.
* 4) Only synchronous functions at this point. * 4) Only synchronous functions at this point.
* *
@ -110,8 +105,8 @@ impl From<ResourceError> for DaliSetupError {
#[derive(PartialEq)] #[derive(PartialEq)]
pub enum InvertTx { pub enum InvertTx {
// A level shifter for interfacing with the bus may invert the signal (e.g. a transistor pulling the line // A level shifter for interfacing with the bus may invert the signal (e.g. a transistor pulling the
// low on a high pin ) or not (e.g. an opto-isolator). // line low on a high pin ) or not (e.g. an opto-isolator).
True, True,
False, False,
} }
@ -126,13 +121,10 @@ pub struct DaliConfig {
#[allow(unused)] // never used, says the compiler #[allow(unused)] // never used, says the compiler
impl DaliConfig { impl DaliConfig {
pub fn new(rx_pin: u5, tx_pin: u5) -> Result<Self, DaliSetupError> { pub fn new(rx_pin: u5, tx_pin: u5) -> Result<Self, DaliSetupError> {
if rx_pin == tx_pin { return Err(DaliSetupError::TxRxPinsIdentical); } if rx_pin == tx_pin {
Ok(Self { return Err(DaliSetupError::TxRxPinsIdentical);
rx_pin, }
tx_pin, Ok(Self { rx_pin, tx_pin, io_mode: IoConfigMode::Overwrite, inverts: InvertTx::False })
io_mode: IoConfigMode::Overwrite,
inverts: InvertTx::False,
})
} }
} }
@ -176,15 +168,14 @@ impl Drop for Dali {
} }
impl Dali { impl Dali {
pub fn new(config: DaliConfig) -> Result<Dali, DaliSetupError> pub fn new(config: DaliConfig) -> Result<Dali, DaliSetupError> {
{
let rx_pin = config.rx_pin; let rx_pin = config.rx_pin;
let tx_pin = config.tx_pin; let tx_pin = config.tx_pin;
// Lower fclk speed to allow longer quantum intervals, any freqency below 157MHz works // Lower fclk speed to allow longer quantum intervals, any freqency below 157MHz works
let requested_frequency = 140_000_000; let requested_frequency = 140_000_000;
let xns = xous_names::XousNames::new().unwrap(); let xns = xous_names::XousNames::new().unwrap();
let conn = xns.request_connection_blocking(xous_api_susres::api::SERVER_NAME_SUSRES) let conn = xns
.request_connection_blocking(xous_api_susres::api::SERVER_NAME_SUSRES)
.expect("Can't connect to Susres server"); .expect("Can't connect to Susres server");
let result = xous::send_message( let result = xous::send_message(
conn, conn,
@ -194,11 +185,14 @@ impl Dali {
requested_frequency as usize, requested_frequency as usize,
0, 0,
0, 0,
)); ),
);
match result { match result {
Ok(xous::Result::Scalar2(ok, _freq)) => { Ok(xous::Result::Scalar2(ok, _freq)) => {
if ok == 0 { return Err(DaliSetupError::CantChangeFclk); } if ok == 0 {
}, return Err(DaliSetupError::CantChangeFclk);
}
}
_ => return Err(DaliSetupError::CantChangeFclk), _ => return Err(DaliSetupError::CantChangeFclk),
}; };
@ -207,9 +201,8 @@ impl Dali {
let resource_grant = bio_ss.claim_resources(&Dali::resource_spec())?; let resource_grant = bio_ss.claim_resources(&Dali::resource_spec())?;
log::debug!("granted to Dali: {:?}", resource_grant); log::debug!("granted to Dali: {:?}", resource_grant);
// configure cores // configure cores
let config_rx = CoreConfig { let config_rx =
clock_mode: bao1x_api::bio::ClockMode::ExternalPin(BioPin::new(rx_pin.as_u8())) CoreConfig { clock_mode: bao1x_api::bio::ClockMode::ExternalPin(BioPin::new(rx_pin.as_u8())) };
};
let config_tx = CoreConfig { clock_mode: bao1x_api::bio::ClockMode::TargetFreqFrac(2400) }; let config_tx = CoreConfig { clock_mode: bao1x_api::bio::ClockMode::TargetFreqFrac(2400) };
let rx_kernel = dali_rx_kernel(); let rx_kernel = dali_rx_kernel();
let tx_kernel = dali_tx_kernel(); let tx_kernel = dali_tx_kernel();
@ -219,7 +212,9 @@ impl Dali {
bio_ss.claim_dynamic_pin(rx_pin.as_u8(), &Dali::resource_spec().claimer)?; bio_ss.claim_dynamic_pin(rx_pin.as_u8(), &Dali::resource_spec().claimer)?;
bio_ss.claim_dynamic_pin(tx_pin.as_u8(), &Dali::resource_spec().claimer)?; bio_ss.claim_dynamic_pin(tx_pin.as_u8(), &Dali::resource_spec().claimer)?;
let mut io_config = IoConfig::default(); let mut io_config = IoConfig::default();
if config.inverts == InvertTx::True { io_config.o_inv = 1 << tx_pin.as_u32(); } if config.inverts == InvertTx::True {
io_config.o_inv = 1 << tx_pin.as_u32();
}
io_config.mapped = (1 << rx_pin.as_u32()) | (1 << tx_pin.as_u32()); io_config.mapped = (1 << rx_pin.as_u32()) | (1 << tx_pin.as_u32());
io_config.mode = config.io_mode; io_config.mode = config.io_mode;
bio_ss.setup_io_config(io_config).unwrap(); bio_ss.setup_io_config(io_config).unwrap();
@ -263,8 +258,11 @@ impl Dali {
} }
} }
fn send_sync(&mut self, address_byte: impl AddrByte, data_byte: impl DataByte) -> fn send_sync(
Result<(), DaliOpError> { &mut self,
address_byte: impl AddrByte,
data_byte: impl DataByte,
) -> Result<(), DaliOpError> {
self.empty_read_queue(); self.empty_read_queue();
let cmd = ForwardFrame::new(address_byte, data_byte).to_bits(); let cmd = ForwardFrame::new(address_byte, data_byte).to_bits();
log::debug!("sending: {:08b}_{:08b}", cmd >> 8, cmd & 0xff); log::debug!("sending: {:08b}_{:08b}", cmd >> 8, cmd & 0xff);
@ -278,95 +276,111 @@ impl Dali {
} }
} }
fn send_read(&mut self, address_byte: impl AddrByte, data_byte: impl DataByte) -> fn send_read(
Result<BackwardFrame, DaliOpError> { &mut self,
let _ = self.send_sync(address_byte, data_byte)?; address_byte: impl AddrByte,
let now = Instant::now(); data_byte: impl DataByte,
// wait time ) -> Result<BackwardFrame, DaliOpError> {
// (last bit transition -> value pushed onto FIFO) -- let _ = self.send_sync(address_byte, data_byte)?;
// value pushed onto FIFO -> end of stop bits 1250us let now = Instant::now();
// end of stop bits -> max. var. delay for backward frames 9166us // wait time
while self.rx.csr.rf(bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1) == 0 && // (last bit transition -> value pushed onto FIFO) --
now.elapsed().as_micros() < 10416 {} // value pushed onto FIFO -> end of stop bits 1250us
// due to min. delay for backward frames and their length, it's unlikely a transmission finishes // end of stop bits -> max. var. delay for backward frames 9166us
// before the end of this timeout while self.rx.csr.rf(bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1) == 0
let read = if self.rx.csr.rf(bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1) != 0 { && now.elapsed().as_micros() < 10416
// message received -> read {}
self.rx.csr.r(bio_bdma::SFR_RXF1) & 0x0000_00ff // due to min. delay for backward frames and their length, it's unlikely a transmission finishes
} else if self.rx.csr.rf(bio_bdma::SFR_EVENT_STATUS_SFR_EVENT_STATUS) & 0b11 == 0 { // before the end of this timeout
// line busy, external source -> wait for message let read = if self.rx.csr.rf(bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1) != 0 {
while self.rx.csr.rf(bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1) == 0 {}; // message received -> read
self.rx.csr.r(bio_bdma::SFR_RXF1) & 0x0000_00ff self.rx.csr.r(bio_bdma::SFR_RXF1) & 0x0000_00ff
} else { } else if self.rx.csr.rf(bio_bdma::SFR_EVENT_STATUS_SFR_EVENT_STATUS) & 0b11 == 0 {
// line idle -> timeout // line busy, external source -> wait for message
// line busy, tx core is sending -> timeout while self.rx.csr.rf(bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1) == 0 {}
0 self.rx.csr.r(bio_bdma::SFR_RXF1) & 0x0000_00ff
}; } else {
if read == 0 { // line idle -> timeout
log::debug!("received: --timeout--"); // line busy, tx core is sending -> timeout
} else { 0
log::debug!("received: {:08b}_{:08b}_{:08b}_{:08b}", };
(read >> 24), (read >> 16) & 0xff, (read >> 8) & 0xff, read & 0xff if read == 0 {
); log::debug!("received: --timeout--");
} } else {
return Ok(BackwardFrame(read as u8)) log::debug!(
"received: {:08b}_{:08b}_{:08b}_{:08b}",
(read >> 24),
(read >> 16) & 0xff,
(read >> 8) & 0xff,
read & 0xff
);
}
return Ok(BackwardFrame(read as u8));
} }
pub fn set_brightness(&mut self, address: Address, brightness: Brightness) -> pub fn set_brightness(&mut self, address: Address, brightness: Brightness) -> Result<(), DaliOpError> {
Result<(), DaliOpError> { _ = self.send_sync(address, brightness)?;
_ = self.send_sync(address, brightness)?; Ok(())
Ok(())
} }
pub fn send_command(&mut self, address: Address, command: impl StdCommand + DataByte) -> pub fn send_command(
Result<(), DaliOpError> { &mut self,
_ = self.send_sync(address, command)?; address: Address,
Ok(()) command: impl StdCommand + DataByte,
) -> Result<(), DaliOpError> {
_ = self.send_sync(address, command)?;
Ok(())
} }
pub fn query_command_value(&mut self, address: Address, command: impl StdCommand + DataByte) -> pub fn query_command_value(
Result<u8, DaliOpError> { &mut self,
let response = self.send_read(address, command)?; address: Address,
Ok(response.0) command: impl StdCommand + DataByte,
) -> Result<u8, DaliOpError> {
let response = self.send_read(address, command)?;
Ok(response.0)
} }
pub fn query_command_bool(&mut self, address: Address, command: impl StdCommand + DataByte) -> pub fn query_command_bool(
Result<bool, DaliOpError> { &mut self,
let response = self.send_read(address, command)?; address: Address,
if response.0 != 0 { command: impl StdCommand + DataByte,
Ok(true) ) -> Result<bool, DaliOpError> {
} else { let response = self.send_read(address, command)?;
Ok(false) if response.0 != 0 { Ok(true) } else { Ok(false) }
}
} }
pub fn send_special_command<Trait: SpecialCommand + AddrByte> pub fn send_special_command<Trait: SpecialCommand + AddrByte>(
(&mut self, command: Trait, data: SpecCmdData) -> Result<(), DaliOpError> { &mut self,
let data_byte = command.match_data_byte(data)?; command: Trait,
_ = self.send_sync(command, data_byte)?; data: SpecCmdData,
Ok(()) ) -> Result<(), DaliOpError> {
let data_byte = command.match_data_byte(data)?;
_ = self.send_sync(command, data_byte)?;
Ok(())
} }
pub fn query_special_command_value<Trait: SpecialCommand + AddrByte> pub fn query_special_command_value<Trait: SpecialCommand + AddrByte>(
(&mut self, command: Trait, data: SpecCmdData) -> Result<u8, DaliOpError> { &mut self,
let data_byte = command.match_data_byte(data)?; command: Trait,
let response = self.send_read(command, data_byte)?; data: SpecCmdData,
Ok(response.0) ) -> Result<u8, DaliOpError> {
let data_byte = command.match_data_byte(data)?;
let response = self.send_read(command, data_byte)?;
Ok(response.0)
} }
pub fn query_special_command_bool<Trait: SpecialCommand + AddrByte> pub fn query_special_command_bool<Trait: SpecialCommand + AddrByte>(
(&mut self, command: Trait, data: SpecCmdData) -> Result<bool, DaliOpError> { &mut self,
let data_byte = command.match_data_byte(data)?; command: Trait,
let response = self.send_read(command, data_byte)?; data: SpecCmdData,
if response.0 != 0 { ) -> Result<bool, DaliOpError> {
Ok(true) let data_byte = command.match_data_byte(data)?;
} else { let response = self.send_read(command, data_byte)?;
Ok(false) if response.0 != 0 { Ok(true) } else { Ok(false) }
}
} }
} }
#[rustfmt::skip] #[rustfmt::skip]
bio_code!(dali_rx_kernel, DALI_RX_START, DALI_RX_END, bio_code!(dali_rx_kernel, DALI_RX_START, DALI_RX_END,
/* DALI receiver /* DALI receiver

View file

@ -1,10 +1,12 @@
use arbitrary_int::{Number, u4, u6}; use arbitrary_int::{Number, u4, u6};
use super::DaliOpError; use super::DaliOpError;
/* Overview: /* Overview:
* *
* Two-layer structure allows user-facing functions of form (address, command) or (command, data) that work * Two-layer structure allows user-facing functions of form (address, command) or (command, data) that work
* with all possible input combinations while preventing misformed frames. Alternative would be nested enums. * with all possible input combinations while preventing misformed frames. Alternative would be nested
* enums.
* *
* User-facing structures: * User-facing structures:
* Address byte: * Address byte:
@ -41,6 +43,7 @@ pub enum Address {
GroupAddress(u4), GroupAddress(u4),
} }
#[rustfmt::skip]
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub enum Command { pub enum Command {
// Arc power control commands // Arc power control commands
@ -182,7 +185,7 @@ pub enum Command {
QueryManufacturerSpecificMode = 166, // * (part of Dali-2) QueryManufacturerSpecificMode = 166, // * (part of Dali-2)
QueryNextDeviceType = 167, // * QueryNextDeviceType = 167, // *
QueryExtendedFadeTime = 168, // * QueryExtendedFadeTime = 168, // *
QueryControlGearFailure = 169, // * QueryControlGearFailure = 169, // *
// Reserved 170-175 // Reserved 170-175
QueryLevelScene0 = 176, // 0xb0 QueryLevelScene0 = 176, // 0xb0
QueryLevelScene1 = 177, QueryLevelScene1 = 177,
@ -210,6 +213,7 @@ pub enum Command {
// Extended Commands 224-255 // Extended Commands 224-255
} }
#[rustfmt::skip]
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub enum Part207LedDriver { pub enum Part207LedDriver {
ReferenceSystemPower = 224, ReferenceSystemPower = 224,
@ -239,6 +243,7 @@ pub enum Part207LedDriver {
QueryExtendedVersionNumber = 255, QueryExtendedVersionNumber = 255,
} }
#[rustfmt::skip]
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub enum SpecialCommand101 { pub enum SpecialCommand101 {
// Recommended to send Initialize and Randomize twice. // Recommended to send Initialize and Randomize twice.
@ -264,6 +269,7 @@ pub enum SpecialCommand101 {
// Reserved 15 // Reserved 15
} }
#[rustfmt::skip]
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub enum SpecialCommand110 { pub enum SpecialCommand110 {
EnableDeviceType = 0, // #272 - maybe selects function of Extended***Set-commands? EnableDeviceType = 0, // #272 - maybe selects function of Extended***Set-commands?
@ -297,7 +303,7 @@ pub enum GroupBit {
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub enum CommandBit { pub enum CommandBit {
DirectArcPowerCommand = 0, DirectArcPowerCommand = 0,
OtherCommand= 1, OtherCommand = 1,
} }
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
@ -312,9 +318,7 @@ pub struct BackwardFrame(pub u8);
// Trait for ForwardFrame constructor // Trait for ForwardFrame constructor
pub trait AddrByte { pub trait AddrByte {
fn to_bits(&self) -> u8; fn to_bits(&self) -> u8;
fn set_group_bit(&self) -> GroupBit { fn set_group_bit(&self) -> GroupBit { GroupBit::GroupAddress }
GroupBit::GroupAddress
}
} }
impl AddrByte for Address { impl AddrByte for Address {
@ -325,6 +329,7 @@ impl AddrByte for Address {
Address::GroupAddress(grp) => grp.as_u8() << 1, Address::GroupAddress(grp) => grp.as_u8() << 1,
} }
} }
fn set_group_bit(&self) -> GroupBit { fn set_group_bit(&self) -> GroupBit {
match self { match self {
Address::ShortAddress(_) => GroupBit::ShortAddress, Address::ShortAddress(_) => GroupBit::ShortAddress,
@ -350,36 +355,34 @@ impl AddrByte for SpecialCommand110 {
// Trait for ForwardFrame constructor // Trait for ForwardFrame constructor
pub trait DataByte { pub trait DataByte {
fn to_bits(&self) -> u8; fn to_bits(&self) -> u8;
fn set_command_bit(&self) -> CommandBit { fn set_command_bit(&self) -> CommandBit { CommandBit::OtherCommand }
CommandBit::OtherCommand
}
} }
impl DataByte for Brightness { impl DataByte for Brightness {
fn to_bits(&self) -> u8 { self.0 } fn to_bits(&self) -> u8 { self.0 }
fn set_command_bit(&self) -> CommandBit {
CommandBit::DirectArcPowerCommand fn set_command_bit(&self) -> CommandBit { CommandBit::DirectArcPowerCommand }
} }
impl DataByte for SpCmdData {
fn to_bits(&self) -> u8 { self.0 }
}
impl DataByte for Command {
fn to_bits(&self) -> u8 { *self as u8 }
}
impl DataByte for Part207LedDriver {
fn to_bits(&self) -> u8 { *self as u8 }
} }
impl DataByte for SpCmdData { fn to_bits(&self) -> u8 { self.0 } }
impl DataByte for Command { fn to_bits(&self) -> u8 { *self as u8 } }
impl DataByte for Part207LedDriver { fn to_bits(&self) -> u8 { *self as u8 } }
impl ForwardFrame { impl ForwardFrame {
pub fn new (address: impl AddrByte, data: impl DataByte) -> Self { pub fn new(address: impl AddrByte, data: impl DataByte) -> Self {
let group_bit = address.set_group_bit(); let group_bit = address.set_group_bit();
let address_bits = address.to_bits(); let address_bits = address.to_bits();
let command_bit = data.set_command_bit(); let command_bit = data.set_command_bit();
let address_byte = (group_bit as u8) << 7 | address_bits | command_bit as u8; let address_byte = (group_bit as u8) << 7 | address_bits | command_bit as u8;
Self { Self { address_byte, data_byte: data.to_bits() }
address_byte,
data_byte: data.to_bits(),
}
} }
pub fn to_bits (&self) -> u16 { pub fn to_bits(&self) -> u16 { (self.address_byte as u16) << 8 | self.data_byte as u16 }
(self.address_byte as u16) << 8 | self.data_byte as u16
}
} }
// User-facing trait to send special commands // User-facing trait to send special commands
@ -390,47 +393,38 @@ pub trait SpecialCommand {
impl SpecialCommand for SpecialCommand101 { impl SpecialCommand for SpecialCommand101 {
fn match_data_byte(&self, data: SpecCmdData) -> Result<SpCmdData, DaliOpError> { fn match_data_byte(&self, data: SpecCmdData) -> Result<SpCmdData, DaliOpError> {
match self { match self {
SpecialCommand101::Terminate | SpecialCommand101::Terminate
SpecialCommand101::Randomize | | SpecialCommand101::Randomize
SpecialCommand101::Compare | | SpecialCommand101::Compare
SpecialCommand101::Withdraw | | SpecialCommand101::Withdraw
SpecialCommand101::QueryShortAddress | | SpecialCommand101::QueryShortAddress
SpecialCommand101::PhysicalSelection | | SpecialCommand101::PhysicalSelection
SpecialCommand101::Ping => { | SpecialCommand101::Ping => match data {
match data { SpecCmdData::None => Ok(SpCmdData(0)),
SpecCmdData::None => Ok(SpCmdData(0)), _ => Err(DaliOpError::WrongDataByteForCommand),
_ => Err(DaliOpError::WrongDataByteForCommand),
}
}, },
SpecialCommand101::ProgramShortAddress | SpecialCommand101::ProgramShortAddress | SpecialCommand101::VerifyShortAddress => match data {
SpecialCommand101::VerifyShortAddress => { SpecCmdData::Address(addr) => {
match data { let byte = addr.as_u8() << 1 | 0b0000_0001;
SpecCmdData::Address(addr) => { Ok(SpCmdData(byte))
let byte = addr.as_u8() << 1 | 0b0000_0001;
Ok(SpCmdData(byte))
}
_ => Err(DaliOpError::WrongDataByteForCommand),
} }
_ => Err(DaliOpError::WrongDataByteForCommand),
}, },
SpecialCommand101::Initialize => { SpecialCommand101::Initialize => match data {
match data { SpecCmdData::InitializeBroadcast => Ok(SpCmdData(0b0000_0000)),
SpecCmdData::InitializeBroadcast => Ok(SpCmdData(0b0000_0000)), SpecCmdData::InitializeShortAddress(addr) => {
SpecCmdData::InitializeShortAddress(addr) => { let byte = addr.as_u8() << 1 | 0b0000_0001;
let byte = addr.as_u8() << 1 | 0b0000_0001; Ok(SpCmdData(byte))
Ok(SpCmdData(byte))
},
SpecCmdData::InitializeDeviceWithoutShortAddress => Ok(SpCmdData(0b1111_1111)),
_ => Err(DaliOpError::WrongDataByteForCommand),
} }
SpecCmdData::InitializeDeviceWithoutShortAddress => Ok(SpCmdData(0b1111_1111)),
_ => Err(DaliOpError::WrongDataByteForCommand),
}, },
SpecialCommand101::DataTransferRegister0 | SpecialCommand101::DataTransferRegister0
SpecialCommand101::SearchAddressH | | SpecialCommand101::SearchAddressH
SpecialCommand101::SearchAddressM | | SpecialCommand101::SearchAddressM
SpecialCommand101::SearchAddressL => { | SpecialCommand101::SearchAddressL => match data {
match data { SpecCmdData::Data(data) => Ok(SpCmdData(data)),
SpecCmdData::Data(data) => Ok(SpCmdData(data)), _ => Err(DaliOpError::WrongDataByteForCommand),
_ => Err(DaliOpError::WrongDataByteForCommand),
}
}, },
} }
} }
@ -452,66 +446,67 @@ impl StdCommand for Command {}
impl StdCommand for Part207LedDriver {} impl StdCommand for Part207LedDriver {}
// Information to decode backward frames // Information to decode backward frames
#[rustfmt::skip]
pub mod bitflags { pub mod bitflags {
pub mod std { pub mod std {
pub mod status_info { pub mod status_info {
pub const STATUS_OF_CONTROL_GEAR: u8 = 1; pub const STATUS_OF_CONTROL_GEAR: u8 = 0x01;
pub const LAMP_FAILURE: u8 = 2; pub const LAMP_FAILURE: u8 = 0x02;
pub const LAMP_ARC_POWER_ON: u8 = 3; pub const LAMP_ARC_POWER_ON: u8 = 0x04;
pub const QUERY_LIMIT_ERROR: u8 = 4; pub const QUERY_LIMIT_ERROR: u8 = 0x08;
pub const FADE_RUNNING: u8 = 5; pub const FADE_RUNNING: u8 = 0x10;
pub const QUERY_RESET_STATE: u8 = 6; pub const QUERY_RESET_STATE: u8 = 0x20;
pub const QUERY_MISSING_SHORT_ADDRESS: u8 = 7; pub const QUERY_MISSING_SHORT_ADDRESS: u8 = 0x40;
pub const QUERY_POWER_FAILURE: u8 = 8; pub const QUERY_POWER_FAILURE: u8 = 0x80;
} }
pub mod lamp_failure { pub mod lamp_failure {
pub const SHORT_CIRCUIT: u8 = 1; pub const SHORT_CIRCUIT: u8 = 0x01;
pub const OPEN_CIRCUIT: u8 = 2; pub const OPEN_CIRCUIT: u8 = 0x02;
pub const LOAD_DECREASE: u8 = 3; pub const LOAD_DECREASE: u8 = 0x04;
pub const LOAD_INCREASE: u8 = 4; pub const LOAD_INCREASE: u8 = 0x08;
pub const CURRENT_PROTECTOR_ACTIVE: u8 = 5; pub const CURRENT_PROTECTOR_ACTIVE: u8 = 0x10;
} }
} }
pub mod part_207_led_driver { pub mod part_207_led_driver {
pub mod gear_type { pub mod gear_type {
pub const LED_POWER_SUPPLY_INTEGRATED: u8 = 1; pub const LED_POWER_SUPPLY_INTEGRATED: u8 = 0x01;
pub const LED_MODULE_INTEGRATED: u8 = 2; pub const LED_MODULE_INTEGRATED: u8 = 0x02;
pub const AC_SUPPLY_POSSIBLE: u8 = 3; pub const AC_SUPPLY_POSSIBLE: u8 = 0x04;
pub const DC_SUPPLY_POSSIBLE: u8 = 4; pub const DC_SUPPLY_POSSIBLE: u8 = 0x08;
// Bits 4-7 unused // Bits 4-7 unused
} }
pub mod possible_operating_mode { pub mod possible_operating_mode {
pub const PWM_MODE_IS_POSSIBLE: u8 = 1; pub const PWM_MODE_IS_POSSIBLE: u8 = 0x01;
pub const AM_MODE_IS_POSSIBLE: u8 = 2; pub const AM_MODE_IS_POSSIBLE: u8 = 0x02;
pub const OUTPUT_IS_CURRENT_CONTROLLED: u8 = 3; pub const OUTPUT_IS_CURRENT_CONTROLLED: u8 = 0x04;
pub const HIGH_CURRENT_PULSE_MODE: u8 = 4; pub const HIGH_CURRENT_PULSE_MODE: u8 = 0x08;
// Bits 4-7 unused // Bits 4-7 unused
} }
pub mod features { pub mod features {
pub const SHORT_CIRCUIT_DETECTION_CAN_BE_QUERIED: u8 = 1; pub const SHORT_CIRCUIT_DETECTION_CAN_BE_QUERIED: u8 = 0x01;
pub const OPEN_CIRCUIT_DETECTION_CAN_BE_QUERIED: u8 = 2; pub const OPEN_CIRCUIT_DETECTION_CAN_BE_QUERIED: u8 = 0x02;
pub const DETECTION_OF_LOAD_DECREASE_CAN_BE_QUERIED: u8 = 3; pub const DETECTION_OF_LOAD_DECREASE_CAN_BE_QUERIED: u8 = 0x04;
pub const DETECTION_OF_LOAD_INCREASE_CAN_BE_QUERIED: u8 = 4; pub const DETECTION_OF_LOAD_INCREASE_CAN_BE_QUERIED: u8 = 0x08;
pub const CURRENT_PROTECTOR_IS_IMPLEMENTED_AND_CAN_BE_QUERIED: u8 = 5; pub const CURRENT_PROTECTOR_IS_IMPLEMENTED_AND_CAN_BE_QUERIED: u8 = 0x10;
pub const THERMAL_SHUTDOWN: u8 = 6; pub const THERMAL_SHUTDOWN: u8 = 0x20;
pub const LIGHT_LEVEL_REDUCTION_DUE_TO_OVERTEMPERATURE: u8 = 7; pub const LIGHT_LEVEL_REDUCTION_DUE_TO_OVERTEMPERATURE: u8 = 0x40;
pub const PHYSICAL_SELECTION_IS_SUPPORTED: u8 = 8; pub const PHYSICAL_SELECTION_IS_SUPPORTED: u8 = 0x80;
} }
pub mod failure_modes { pub mod failure_modes {
pub const SHORT_CIRCUIT_DETECTION_CAN_BE_QUERIED: u8 = 1; pub const SHORT_CIRCUIT_DETECTION_CAN_BE_QUERIED: u8 = 0x01;
pub const OPEN_CIRCUIT_DETECTION_CAN_BE_QUERIED: u8 = 2; pub const OPEN_CIRCUIT_DETECTION_CAN_BE_QUERIED: u8 = 0x02;
pub const DETECTION_OF_LOAD_DECREASE_CAN_BE_QUERIED: u8 = 3; pub const DETECTION_OF_LOAD_DECREASE_CAN_BE_QUERIED: u8 = 0x04;
pub const DETECTION_OF_LOAD_INCREASE_CAN_BE_QUERIED: u8 = 4; pub const DETECTION_OF_LOAD_INCREASE_CAN_BE_QUERIED: u8 = 0x08;
pub const CURRENT_PROTECTOR_IS_IMPLEMENTED_AND_CAN_BE_QUERIED: u8 = 5; pub const CURRENT_PROTECTOR_IS_IMPLEMENTED_AND_CAN_BE_QUERIED: u8 = 0x10;
pub const THERMAL_SHUTDOWN: u8 = 6; pub const THERMAL_SHUTDOWN: u8 = 0x20;
// Bit 7-8 unused // Bit 6-7 unused
} }
pub mod operating_mode { pub mod operating_mode {
pub const PWM_MODE_ACTIVE: u8 = 1; pub const PWM_MODE_ACTIVE: u8 = 0x01;
pub const AM_MODE_ACTIVE: u8 = 2; pub const AM_MODE_ACTIVE: u8 = 0x02;
pub const OUTPUT_IS_CURRENT_CONTROLLED: u8 = 3; pub const OUTPUT_IS_CURRENT_CONTROLLED: u8 = 0x04;
pub const HIGH_CURRENT_PULSE_MODE_ACTIVE: u8 = 4; pub const HIGH_CURRENT_PULSE_MODE_ACTIVE: u8 = 0x08;
pub const NON_LOGARITHMIC_DIMMING_CURVE_ACTIVE: u8 = 5; pub const NON_LOGARITHMIC_DIMMING_CURVE_ACTIVE: u8 = 0x10;
// Bits 5-7 unused // Bits 5-7 unused
} }
} }

View file

@ -38,9 +38,9 @@ pub mod av_trng;
pub mod c; pub mod c;
#[cfg(feature = "captouch")] #[cfg(feature = "captouch")]
pub mod captouch; pub mod captouch;
#[cfg(feature = "dali")]
pub mod dali;
#[cfg(feature = "mac-demo")] #[cfg(feature = "mac-demo")]
pub mod mac_demo; pub mod mac_demo;
#[cfg(feature = "ws2812")] #[cfg(feature = "ws2812")]
pub mod ws2812; pub mod ws2812;
#[cfg(feature = "dali")]
pub mod dali;

View file

@ -2,8 +2,8 @@ pub mod api;
pub mod trng; pub mod trng;
use bao1x_api::*; use bao1x_api::*;
use bao1x_hal::udma::{AdcExtChannel, AdcSource};
use bao1x_hal::clocks::ClockOp; use bao1x_hal::clocks::ClockOp;
use bao1x_hal::udma::{AdcExtChannel, AdcSource};
use num_traits::*; use num_traits::*;
use xous::{Message, send_message}; use xous::{Message, send_message};
use xous_api_susres::api::Opcode as SusresOp; use xous_api_susres::api::Opcode as SusresOp;

View file

@ -1,13 +1,12 @@
use core::sync::atomic::{AtomicU32, Ordering}; use core::sync::atomic::{AtomicU32, Ordering};
use bao1x_api::divide_by_fd;
use bao1x_hal::clocks::{ClockOp, fd_from_frequency};
use num_traits::*; use num_traits::*;
use utralib::*; use utralib::*;
use xous::{CID, Message, msg_blocking_scalar_unpack, msg_scalar_unpack, send_message, sender::Sender}; use xous::{CID, Message, msg_blocking_scalar_unpack, msg_scalar_unpack, send_message, sender::Sender};
use xous_api_susres::*; use xous_api_susres::*;
use xous_ipc::Buffer; use xous_ipc::Buffer;
use bao1x_api::divide_by_fd;
use bao1x_hal::clocks::{fd_from_frequency,ClockOp};
static TIMEOUT_TIME: AtomicU32 = AtomicU32::new(5000); static TIMEOUT_TIME: AtomicU32 = AtomicU32::new(5000);
@ -406,7 +405,9 @@ fn susres_service() {
let requested_freq = arg2 as u32; let requested_freq = arg2 as u32;
let fclk_fd = fd_from_frequency(requested_freq, clk_mgr.clktop); let fclk_fd = fd_from_frequency(requested_freq, clk_mgr.clktop);
let achievable_freq = divide_by_fd(fclk_fd, clk_mgr.clktop); let achievable_freq = divide_by_fd(fclk_fd, clk_mgr.clktop);
if (1.0 - (achievable_freq as f32 / requested_freq as f32)).abs() < MAX_DEVIATION { if (1.0 - (achievable_freq as f32 / requested_freq as f32)).abs()
< MAX_DEVIATION
{
clk_mgr.set_fclk_fd(fclk_fd as u8); clk_mgr.set_fclk_fd(fclk_fd as u8);
clk_mgr.update_clocks(); clk_mgr.update_clocks();
log::info!("Changed fclk to {}", clk_mgr.fclk); log::info!("Changed fclk to {}", clk_mgr.fclk);