1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//! This [RP2040](rp2040_hal) project provides contact detection with a
//! highly-conductivity/highly-capacitive surface (such as brain tissue) for an autopsy saw. For
//! more information, check out [the repo](https://github.com/jessicarod7/aps490_pfpu2_mini).
//!
//! ## Crate features
//!
//! - `triple_status`: Enables the use of 3 LEDs to provide system status. This is the main user
//! interface for the tool, and is enabled by default.
//! - `rgba_status`: Alternate configuration which uses a single common-anode RGB LED. This is the design which appears in
//! [our schematic](https://github.com/jessicarod7/aps490_retraction_fsm/blob/hardware/aps490_detection/aps490_detection-schematic.pdf).
//! - `trace_avg_samples`: Logs the average voltage difference measured, 250 samples at a time. See
//! [`buffer::Buffers::trace_avg_samples`].
//! - `trace_indiv_samples` Logs information on every sample recorded. Very noisy! See
//! [`interrupt::AlignedAverages::trace_high_index`] and [`interrupt::trace_indiv_samples`]
//! - `disable_switch`: Starts the SysTick timer to check the disable switch status. Never tested
//! this feature, and I'm pretty sure my implementation will cause the system to panic due to poor
//! synchronization. This functionality should be redesigned before enabling the feature.
//!
//! <div class="warning">Features <code>triple_status</code> and <code>rgba_status</code> are
//! mutually exclusive.</div>
//!
//! ## Demo
//!
//! The following is a simplified (including [`Rgba`](components::Rgba)-only lights) implementation of the [binary crate](https://docs.rs/crate/aps490_pfpu2_mini/latest/source/src/main.rs)
//! used on our proof-of-concept.
//!
//! ```no_run
//! #![no_std]
//! #![no_main]
//!
//! use aps490_pfpu2_mini::{
//! buffer::{create_avg_buffer, Buffers},
//! components::{LedControl, Rgba, StatusLed, StatusLedBase},
//! interrupt::{DISABLE_SWITCH, READINGS_FIFO, SIGNAL_GEN, STATUS_LEDS},
//! };
//! use cortex_m::peripheral::syst::SystClkSource;
//! use defmt::{debug, warn};
//! #[allow(unused_imports)]
//! use defmt_rtt as _;
//! use embedded_hal::pwm::SetDutyCycle;
//! #[allow(unused_imports)]
//! use panic_probe as _;
//! use rp2040_hal::{
//! adc::{Adc, AdcPin},
//! clocks::init_clocks_and_plls,
//! dma::{single_buffer, DMAExt, SingleChannel},
//! entry,
//! gpio::Pins,
//! pac,
//! prelude::*,
//! pwm::Slices,
//! Sio, Watchdog,
//! };
//!
//! #[link_section = ".boot2"]
//! #[used]
//! pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080;
//! pub const XOSC_FREQ_HZ: u32 = 12_000_000;
//! const SYS_CLOCK_FREQ: u32 = 24_000_000;
//! pub static SIGNAL_GEN_FREQ_HZ: f32 = 100_000.0;
//!
//! #[entry]
//! fn main() -> ! {
//! let mut pac = pac::Peripherals::take().unwrap();
//! let core = pac::CorePeripherals::take().unwrap();
//! let mut watchdog = Watchdog::new(pac.WATCHDOG);
//! let sio = Sio::new(pac.SIO);
//!
//! let mut clocks = init_clocks_and_plls(
//! XOSC_FREQ_HZ,
//! pac.XOSC,
//! pac.CLOCKS,
//! pac.PLL_SYS,
//! pac.PLL_USB,
//! &mut pac.RESETS,
//! &mut watchdog,
//! )
//! .ok()
//! .unwrap();
//! let mut sysclk_rescale = clocks.system_clock.freq().to_Hz() as f32 / SYS_CLOCK_FREQ as f32;
//!
//! // Setup status LEDs
//! let pins = Pins::new(
//! pac.IO_BANK0,
//! pac.PADS_BANK0,
//! sio.gpio_bank0,
//! &mut pac.RESETS,
//! );
//! critical_section::with(|cs| {
//! STATUS_LEDS.replace(cs, Rgba::init(pins.gpio6, pins.gpio7, pins.gpio8));
//! });
//!
//! // Initialize and start signal generator
//! let mut pwm_slices = Slices::new(pac.PWM, &mut pac.RESETS);
//! pwm_slices.pwm3.set_top(
//! ((clocks.system_clock.freq().to_Hz() as f32 / (SIGNAL_GEN_FREQ_HZ * sysclk_rescale))
//! - 1.0) as u16,
//! );
//! pwm_slices.pwm3.enable();
//! let mut signal_gen = pwm_slices.pwm3.channel_a;
//! signal_gen.output_to(pins.gpio22);
//! signal_gen.set_duty_cycle_percent(50).unwrap();
//! critical_section::with(|cs| SIGNAL_GEN.replace(cs, Some(signal_gen)));
//!
//! // Setup ADC pins, DMA, buffers
//! let mut adc = Adc::new(pac.ADC, &mut pac.RESETS);
//! let mut adc_pin0 = AdcPin::new(pins.gpio26.into_floating_input()).unwrap();
//! let mut dma = pac.DMA.split(&mut pac.RESETS);
//! Buffers::init();
//!
//! // Setup first transfer
//! let avg_buffer = create_avg_buffer().unwrap();
//! let mut readings_fifo = adc
//! .build_fifo()
//! .set_channel(&mut adc_pin0)
//! .clock_divider(
//! ((clocks.system_clock.freq().to_Hz() as f32
//! / (2.0 * (SIGNAL_GEN_FREQ_HZ * sysclk_rescale)))
//! - 1.0) as u16,
//! 0,
//! )
//! .shift_8bit()
//! .enable_dma()
//! .start_paused();
//! dma.ch0.enable_irq0();
//! let adc_dma_transfer =
//! single_buffer::Config::new(dma.ch0, readings_fifo.dma_read_target(), avg_buffer);
//! debug!("critical_section: transfer readings FIFO to mutex");
//! critical_section::with(|cs| READINGS_FIFO.replace(cs, Some(adc_dma_transfer.start())));
//! readings_fifo.resume();
//!
//! // Configure and enable SysTick for disable switch
//! let disable_switch = pins.gpio9.into_pull_down_input();
//! disable_switch.set_schmitt_enabled(true); // Debouncing
//! debug!("critical_section: init disable switch");
//! critical_section::with(|cs| DISABLE_SWITCH.replace(cs, Some(disable_switch)));
//!
//! let mut syst = core.SYST;
//! syst.set_clock_source(SystClkSource::Core); // 1 us per tick
//! syst.set_reload(20_000);
//! syst.clear_current();
//! #[cfg(feature = "disable_switch")]
//! syst.enable_interrupt();
//!
//! // Begin normal system operation
//! critical_section::with(|cs| {
//! #[cfg(feature = "rgba_status")]
//! StatusLedBase::<Rgba>::set_normal(cs, Some("System initialization complete"));
//! #[cfg(feature = "triple_status")]
//! StatusLedBase::<Triple>::set_normal(cs, Some("System initialization complete"));
//! });
//! unsafe { pac::NVIC::unmask(pac::Interrupt::DMA_IRQ_0) }
//! loop {
//! // All functionality in interrupts
//! cortex_m::asm::wfi();
//! }
//! }
//! ```
// Copyright 2024 Jessica Rodriguez
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
compile_error!;