Skip to content

Commit 31d6a5b

Browse files
committed
feat(spi): improve inline documentation and fix build errors
1 parent 3fbd0c1 commit 31d6a5b

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

examples/lpc55s69/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ publish = false
88

99
[dependencies]
1010
embassy-nxp = { version = "0.1.0", path = "../../embassy-nxp", features = ["lpc55-core0", "rt", "defmt", "time-driver-rtc"] }
11+
nxp-pac = { version = "0.1.0", git = "https://github.com/i509VCB/nxp-pac", rev = "b736e3038254d593024aaa1a5a7b1f95a5728538", features = ["metadata"]}
1112
embassy-executor = { version = "0.9.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "executor-interrupt"] }
1213
embassy-sync = { version = "0.7.2", path = "../../embassy-sync", features = ["defmt"] }
1314
embassy-time = { version = "0.5.0", path = "../../embassy-time", features = ["defmt", "tick-hz-32_768"] }

examples/lpc55s69/src/bin/spi_master_nxp-pac.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
// Location should be embassy/examples/lpc55s69/src/bin/spi_master_test.rs
21
#![no_std]
32
#![no_main]
43

54
use cortex_m::asm::nop;
65
use defmt::*;
76
use embassy_executor::Spawner;
8-
//use embassy_nxp::pac::*;
97
use {defmt_rtt as _, panic_halt as _};
108
use nxp_pac::*;
119

1210
fn init() {
1311
info!("Init");
14-
// SPI 7 at FLEXCOMM 7 Setup
15-
// CLOCK ENABLING
12+
// SPI 7 at FLEXCOMM 7 Setup (spi instance is the same as the flexcomm instance)
13+
// Enable iocon and flexcomm
1614
SYSCON.ahbclkctrl0().modify(|w| {
1715
w.set_iocon(true);
1816
} );
1917
SYSCON.ahbclkctrl1().modify(|w| {
2018
w.set_fc(7, true);
2119
} );
2220

23-
// RST FLEXCOMM 7
21+
// Reset Flexcomm 7
2422
SYSCON.presetctrl1().modify(|w| {
2523
w.set_fc_rst(7, syscon::vals::FcRst::ASSERTED);
2624
});
@@ -29,14 +27,18 @@ fn init() {
2927
});
3028

3129
// CLK SEL
30+
// Select Main Clock (ENUM_0X2 is FRO 12Mhz)
3231
SYSCON.fcclksel(7).modify(|w|
3332
w.set_sel(syscon::vals::FcclkselSel::ENUM_0X2)
3433
);
34+
// Set flexcomm to SPI
3535
FLEXCOMM7.pselid().modify(|w|{
3636
w.set_persel(flexcomm::vals::Persel::SPI);
3737
});
3838

3939
// IOCON Setup
40+
// All pins are configured according to the standard settings in the SPI config documentation
41+
// SSEL1 at func 1 iocon
4042
IOCON.pio1(20).modify(|w|{
4143
w.set_func(iocon::vals::PioFunc::ALT1);
4244
w.set_digimode(iocon::vals::PioDigimode::DIGITAL);
@@ -45,6 +47,7 @@ fn init() {
4547
w.set_invert(false);
4648
w.set_od(iocon::vals::PioOd::NORMAL);
4749
});
50+
// MOSI at func 7 iocon
4851
IOCON.pio0(20).modify(|w|{
4952
w.set_func(iocon::vals::PioFunc::ALT7);
5053
w.set_digimode(iocon::vals::PioDigimode::DIGITAL);
@@ -53,6 +56,7 @@ fn init() {
5356
w.set_invert(false);
5457
w.set_od(iocon::vals::PioOd::NORMAL);
5558
});
59+
// MISO at func 7 iocon
5660
IOCON.pio0(19).modify(|w|{
5761
w.set_func(iocon::vals::PioFunc::ALT7);
5862
w.set_digimode(iocon::vals::PioDigimode::DIGITAL);
@@ -61,6 +65,7 @@ fn init() {
6165
w.set_invert(false);
6266
w.set_od(iocon::vals::PioOd::NORMAL);
6367
});
68+
// SCKL at func 7 iocon
6469
IOCON.pio0(21).modify(|w|{
6570
w.set_func(iocon::vals::PioFunc::ALT7);
6671
w.set_digimode(iocon::vals::PioDigimode::DIGITAL);
@@ -70,16 +75,18 @@ fn init() {
7075
w.set_od(iocon::vals::PioOd::NORMAL);
7176
});
7277

73-
// INTERFACE CLK Setup
78+
// FlexcommFRG divider (div is 0xFF by default in the documentation meaning it can be divided by either 1 or 2)
7479
SYSCON.flexfrgctrl(7).modify(|w|{
7580
w.set_div(0xFF);
7681
w.set_mult(0);
7782
});
83+
// SPI clock divider can go from 1 to 255
7884
SPI7.div().modify(|w|{
7985
w.set_divval(0);
8086
});
87+
// Final Clock is 12 MHz
8188

82-
//SPI MASTER CONFIG
89+
// SPI Master config using ssel_1
8390
SPI7.cfg().modify(|w|{
8491
w.set_enable(true);
8592
w.set_master(spi::vals::Master::MASTER_MODE);
@@ -89,6 +96,7 @@ fn init() {
8996
w.set_loop_(false);
9097
w.set_spol1(spi::vals::Spol1::LOW);
9198
});
99+
// FIFO Config disabling DMA and enabling TX and RX
92100
SPI7.fifocfg().modify(|w| {
93101
w.set_dmatx(false);
94102
w.set_dmarx(false);
@@ -97,17 +105,18 @@ fn init() {
97105
//w.set_emptytx(true);
98106
//w.set_emptyrx(true);
99107
});
108+
// Disabling RX is recommended in the documentation if you are not expecting to receive data
100109
SPI7.fifowr().write(|w|{
101110
w.set_rxignore(spi::vals::Rxignore::IGNORE);
102111
});
103112

104113
loop {
105-
SPI7.fifowr().write(|w|w.set_txssel1_n(spi::vals::Txssel1N::ASSERTED));
114+
SPI7.fifowr().write(|w|w.set_txssel1_n(spi::vals::Txssel1N::ASSERTED)); // Assert the SSEL you are transferring data to
106115
for _ in 0..100_000 {
107116
nop();}
108117
SPI7.fifowr().write(|w|{
109-
w.set_txdata(0x04);
110-
w.set_len(8);
118+
w.set_txdata(0x04); // Data to be transfered
119+
w.set_len(8); // !! IMPORTANT !! If length isn't specified data won't be shifted out
111120
});
112121
for _ in 0..100_000 {
113122
nop();}
@@ -116,8 +125,8 @@ fn init() {
116125
info!("Tx level: {}", fifostat.txlvl());
117126
info!("Tx empty? {}", fifostat.txempty());
118127
SPI7.fifowr().write(|w|{
119-
w.set_eot(true);
120-
w.set_txssel1_n(spi::vals::Txssel1N::NOT_ASSERTED);
128+
w.set_eot(true); // Mark end of transfer
129+
w.set_txssel1_n(spi::vals::Txssel1N::NOT_ASSERTED); // Deassert our SSEL
121130
});
122131
for _ in 0..100_000 {
123132
nop();}

0 commit comments

Comments
 (0)