-
Notifications
You must be signed in to change notification settings - Fork 8
/
ad9833-midi-player-bp.rs
215 lines (196 loc) · 5.5 KB
/
ad9833-midi-player-bp.rs
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! This plays the final part of Beethoven's ninth symphony given by
//! its MIDI tones using an AD9833 waveform generator / direct digital synthesizer.
//!
//! You can see a video of this running here:
//! https://blog.eldruin.com/ad983x-waveform-generator-dds-driver-in-rust/
//!
//! This example is runs on the STM32F103 "Bluepill" board using SPI1.
//!
//! ```
//! BP <-> AD9833 <-> Amplifier
//! GND <-> VSS <-> GND
//! 3.3V <-> VDD <-> VCC
//! PA4 <-> FSYNC
//! PA5 <-> CLK
//! PA7 <-> DAT
//! OUT <-> IN
//! ```
//!
//! You will need an amplifier like the PAM8403 or similar and a speaker.
//!
//! Run with:
//! `cargo embed --example ad9833-midi-player-bp --release`,
#![deny(unsafe_code)]
#![no_std]
#![no_main]
use ad983x::{Ad983x, FrequencyRegister, MODE};
use cortex_m_rt::entry;
use panic_rtt_target as _;
use rtt_target::{rprintln, rtt_init_print};
use stm32f1xx_hal::{delay::Delay, pac, prelude::*, spi::Spi};
#[entry]
fn main() -> ! {
rtt_init_print!();
rprintln!("AD9833 example");
let cp = cortex_m::Peripherals::take().unwrap();
let dp = pac::Peripherals::take().unwrap();
let mut flash = dp.FLASH.constrain();
let rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.freeze(&mut flash.acr);
let mut delay = Delay::new(cp.SYST, clocks);
let mut afio = dp.AFIO.constrain();
let mut gpioa = dp.GPIOA.split();
// SPI1
let sck = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl);
let miso = gpioa.pa6;
let mosi = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl);
let mut cs = gpioa.pa4.into_push_pull_output(&mut gpioa.crl);
let spi = Spi::spi1(
dp.SPI1,
(sck, miso, mosi),
&mut afio.mapr,
MODE,
1_u32.mhz(),
clocks,
);
let mut gpioc = dp.GPIOC.split();
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
cs.set_high();
let mut synth = Ad983x::new_ad9833(spi, cs);
synth.reset().unwrap();
synth.enable().unwrap();
let mut current_register = FrequencyRegister::F0;
let mut table = MidiTable::default();
loop {
// Blink LED 0 to check that everything is actually running.
// If the LED 0 does not blink, something went wrong.
led.set_high();
delay.delay_ms(50_u16);
led.set_low();
delay.delay_ms(25_u16);
let midi_number = table.next().unwrap_or(0);
let midi_number = f64::from(midi_number);
let frequency_hz = libm::pow(2.0, (midi_number - 69.0) / 12.0) * 440.0;
let mclk_hz = 25_000_000.0;
let synth_value = frequency_hz * f64::from(1 << 28) / mclk_hz;
// To ensure a smooth transition, set the frequency in the frequency
// register that is not currently in use, then switch to it.
let opposite = get_opposite(current_register);
synth.set_frequency(opposite, synth_value as u32).unwrap();
synth.select_frequency(opposite).unwrap();
current_register = opposite;
}
}
fn get_opposite(register: FrequencyRegister) -> FrequencyRegister {
match register {
FrequencyRegister::F0 => FrequencyRegister::F1,
FrequencyRegister::F1 => FrequencyRegister::F0,
}
}
#[derive(Debug, Default)]
struct MidiTable {
position: usize,
duration_counter: usize,
}
impl Iterator for MidiTable {
type Item = u32;
fn next(&mut self) -> Option<Self::Item> {
let mut silence = None;
let (_, note_duration, silence_duration) = Self::NOTES[self.position];
let total_duration = note_duration + silence_duration;
let is_in_silence =
self.duration_counter >= note_duration && self.duration_counter < total_duration;
if is_in_silence {
self.duration_counter += 1;
silence = Some(0);
} else if self.duration_counter >= total_duration {
self.position = (self.position + 1) % Self::NOTES.len();
self.duration_counter = 1;
} else {
self.duration_counter += 1;
}
let tone = Some(Self::NOTES[self.position].0);
silence.or(tone)
}
}
impl MidiTable {
const NOTES: [(u32, usize, usize); 62] = [
(76, 4, 1),
(76, 4, 1),
(77, 4, 1),
(79, 4, 1),
//
(79, 4, 1),
(77, 4, 1),
(76, 4, 1),
(74, 4, 1),
//
(72, 4, 1),
(72, 4, 1),
(74, 4, 1),
(76, 4, 1),
//
(76, 4, 4),
(74, 2, 1),
(74, 6, 4),
//
(76, 4, 1),
(76, 4, 1),
(77, 4, 1),
(79, 4, 1),
//
(79, 4, 1),
(77, 4, 1),
(76, 4, 1),
(74, 4, 1),
//
(72, 4, 1),
(72, 4, 1),
(74, 4, 1),
(76, 4, 1),
//
(74, 4, 4),
(72, 2, 1),
(72, 6, 4),
//
(74, 4, 1),
(74, 4, 1),
(76, 4, 1),
(72, 4, 1),
//
(74, 4, 1),
(76, 2, 1),
(77, 2, 1),
(76, 4, 1),
(72, 4, 1),
//
(74, 4, 1),
(76, 2, 1),
(77, 2, 1),
(76, 4, 1),
(74, 4, 1),
//
(72, 4, 1),
(74, 4, 1),
(67, 6, 2),
//
(76, 4, 1),
(76, 4, 1),
(77, 4, 1),
(79, 4, 1),
//
(79, 4, 1),
(77, 4, 1),
(76, 4, 1),
(74, 4, 1),
//
(72, 4, 1),
(72, 4, 1),
(74, 4, 1),
(76, 4, 1),
//
(74, 6, 2),
(72, 2, 1),
(72, 6, 10),
];
}