Skip to content

Commit

Permalink
Minor SPI refactors (#90)
Browse files Browse the repository at this point in the history
* chore: SPI make method names consistent, remove obsolete commentary
* test(spim0): print on uDMA UART instead of APB UART 0
  • Loading branch information
hegza authored Nov 5, 2024
1 parent 506b385 commit 64e8549
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
13 changes: 4 additions & 9 deletions examples/headsail-bsp/src/sysctrl/udma/spim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,12 @@ impl<'u> UdmaSpim<'u, Enabled> {
.write(|w| unsafe { w.bits(buf.len() as u32) });

// Dispatch transmission
spim.spim_tx_cfg().write(
|w| w.en().set_bit(), // If we want "continuous mode". In continuous mode, uDMA reloads the address and transmits it again
//.continous().set_bit()
);
spim.spim_tx_cfg().write(|w| w.en().set_bit());

// Poll until finished (prevents `buf` leakage)
while spim.spim_tx_saddr().read().bits() != 0 {}
}

pub fn write_rx(&mut self, buf: &[u8]) {
let spim = &self.0;

Expand All @@ -75,10 +73,7 @@ impl<'u> UdmaSpim<'u, Enabled> {
.write(|w| unsafe { w.bits(buf.len() as u32) });

// Dispatch transmission
spim.spim_rx_cfg().write(
|w| w.en().set_bit(), // If we want "continuous mode". In continuous mode, uDMA reloads the address and transmits it again
//.continous().set_bit()
);
spim.spim_rx_cfg().write(|w| w.en().set_bit());

// Poll until finished (prevents `buf` leakage)
while spim.spim_rx_saddr().read().bits() != 0 {}
Expand Down Expand Up @@ -178,7 +173,7 @@ impl<'u> UdmaSpim<'u, Enabled> {
/// spim.eot();
///
/// ```
pub fn receive(&mut self, data: &[u8]) {
pub fn receive_data(&mut self, data: &[u8]) {
let mut cmd_data: [u8; 12] = [0; 12];

cmd_data[0..4].copy_from_slice(
Expand Down
25 changes: 16 additions & 9 deletions examples/sysctrl/hello-sysctrl/examples/spim0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,29 @@
#![no_main]

use core::arch::asm;
use headsail_bsp::apb_uart::ApbUart0;
use headsail_bsp::{pac::Sysctrl, rt::entry, sysctrl::udma::Udma};
use headsail_bsp::{
pac::Sysctrl,
rt::entry,
sysctrl::{soc_ctrl, udma::Udma},
ufmt,
};
use hello_sysctrl::{print_example_name, sprintln};

#[entry]
fn main() -> ! {
// These lines are necessary to initialize uDMA UART prints for sprint-macro
soc_ctrl::periph_clk_div_set(0);
hello_sysctrl::UdmaUart::init();
print_example_name!();

let sysctrl = unsafe { Sysctrl::steal() };
let udma = Udma(sysctrl.udma());

// Split uDMA into sub-drivers for each peripheral
let udma_periphs = udma.split();

let (soc_freq, baud) = (30_000_000, 115_200);
let mut uart = ApbUart0::init(soc_freq, baud);

let mut spim = udma_periphs.spim.enable();
uart.write_str("SPI enabled!\n\r");
sprintln!("SPI enabled!");

let tx_data: [u8; 8] = [0x01, 0x42, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08];
let rx_data: [u8; 8] = [0; 8];
Expand All @@ -32,16 +39,16 @@ fn main() -> ! {
// Send 8 bytes
spim.send_data(&tx_data);
spim.write_eot_keep_cs();
uart.write_str("Data sent!\n\r");
sprintln!("Data sent!\n\r");

for _ in 0..10_000 {
unsafe { asm!("nop") }
}

// Receive 8 bytes
spim.receive(&rx_data);
spim.receive_data(&rx_data);
spim.write_eot();
uart.write_str("Data received!\n\r");
sprintln!("Data received!\n\r");

loop {
continue;
Expand Down

0 comments on commit 64e8549

Please sign in to comment.