humpback-dds/src/main.rs

211 lines
4.4 KiB
Rust
Raw Normal View History

2020-08-07 13:36:00 +08:00
#![no_main]
#![no_std]
2020-09-14 17:33:50 +08:00
#[macro_use]
extern crate log;
use log::{trace, debug, info, warn};
2020-08-07 13:36:00 +08:00
use stm32h7xx_hal::hal::digital::v2::{
InputPin,
OutputPin,
};
use stm32h7xx_hal::{pac, prelude::*, spi};
use cortex_m;
use cortex_m_rt::entry;
2020-08-11 00:07:07 +08:00
use firmware;
use firmware::{
attenuator::Attenuator,
2020-08-12 11:50:24 +08:00
config_register::{
ConfigRegister,
CFGMask,
2020-08-23 17:17:09 +08:00
StatusMask,
2020-08-12 11:50:24 +08:00
},
2020-08-26 13:18:50 +08:00
dds::{
DDS,
DDSCFRMask,
},
2020-08-31 09:31:56 +08:00
cpld::{
CPLD,
}
2020-08-11 00:07:07 +08:00
};
2020-08-07 13:36:00 +08:00
2020-09-14 17:33:50 +08:00
#[path = "../examples/util/logger.rs"]
mod logger;
2020-08-07 13:36:00 +08:00
#[entry]
fn main() -> ! {
2020-09-14 17:33:50 +08:00
let mut cp = cortex_m::Peripherals::take().unwrap();
2020-08-07 13:36:00 +08:00
let dp = pac::Peripherals::take().unwrap();
let pwr = dp.PWR.constrain();
let vos = pwr.freeze();
let rcc = dp.RCC.constrain();
let ccdr = rcc
2020-09-14 17:33:50 +08:00
.use_hse(8.mhz())
2020-08-07 13:36:00 +08:00
.sys_ck(400.mhz())
.pll1_q_ck(48.mhz())
2020-09-14 17:33:50 +08:00
.pll1_r_ck(400.mhz())
2020-08-07 13:36:00 +08:00
.freeze(vos, &dp.SYSCFG);
2020-09-14 17:33:50 +08:00
unsafe {
logger::enable_itm(&dp.DBGMCU, &mut cp.DCB, &mut cp.ITM);
}
logger::init();
2020-08-07 13:36:00 +08:00
let mut delay = cp.SYST.delay(ccdr.clocks);
let gpioa = dp.GPIOA.split(ccdr.peripheral.GPIOA);
let gpiob = dp.GPIOB.split(ccdr.peripheral.GPIOB);
2020-08-09 18:46:06 +08:00
let gpioc = dp.GPIOC.split(ccdr.peripheral.GPIOC);
2020-08-07 13:36:00 +08:00
let gpiod = dp.GPIOD.split(ccdr.peripheral.GPIOD);
2020-08-21 14:18:33 +08:00
let gpioe = dp.GPIOE.split(ccdr.peripheral.GPIOE);
2020-08-07 13:36:00 +08:00
let gpiof = dp.GPIOF.split(ccdr.peripheral.GPIOF);
// Setup CDONE for checking
let fpga_cdone = gpiod.pd15.into_pull_up_input();
match fpga_cdone.is_high() {
2020-09-14 17:33:50 +08:00
Ok(true) => info!("FPGA is ready."),
Ok(_) => info!("FPGA is in reset state."),
Err(_) => info!("Error: Cannot read C_DONE"),
};
2020-08-07 13:36:00 +08:00
2020-08-09 13:42:18 +08:00
/*
* Using SPI1, AF5
* SCLK -> PA5
* MOSI -> PB5
* MISO -> PA6
* CS -> 0: PB12, 1: PA15, 2: PC7
*/
let sclk = gpioa.pa5.into_alternate_af5();
let mosi = gpiob.pb5.into_alternate_af5();
let miso = gpioa.pa6.into_alternate_af5();
2020-08-24 10:57:37 +08:00
2020-08-09 13:42:18 +08:00
2020-08-24 17:03:44 +08:00
let (cs0, cs1, cs2) = (
2020-08-11 00:07:07 +08:00
gpiob.pb12.into_push_pull_output(),
gpioa.pa15.into_push_pull_output(),
gpioc.pc7.into_push_pull_output(),
);
2020-08-24 10:57:37 +08:00
/*
* I/O_Update -> PB15
2020-08-24 10:57:37 +08:00
*/
2020-08-25 12:20:24 +08:00
let io_update = gpiob.pb15.into_push_pull_output();
2020-08-24 10:57:37 +08:00
2020-08-24 17:03:44 +08:00
let spi = dp.SPI1.spi(
2020-08-09 13:42:18 +08:00
(sclk, miso, mosi),
spi::MODE_0,
2020-08-21 14:18:33 +08:00
3.mhz(),
2020-08-09 13:42:18 +08:00
ccdr.peripheral.SPI1,
&ccdr.clocks,
);
2020-08-25 12:20:24 +08:00
2020-08-26 13:18:50 +08:00
let switch = CPLD::new(spi, (cs0, cs1, cs2), io_update);
2020-08-11 00:07:07 +08:00
let parts = switch.split();
2020-08-11 16:51:17 +08:00
let mut config = ConfigRegister::new(parts.spi1);
2020-08-21 14:18:33 +08:00
let mut att = Attenuator::new(parts.spi2);
2020-08-26 16:49:37 +08:00
let mut dds0 = DDS::new(parts.spi4, 25_000_000);
2020-08-24 10:57:37 +08:00
2020-08-24 17:03:44 +08:00
// Reset all DDS, set CLK_SEL to 0
config.set_configurations(&mut [
(CFGMask::RST, 1),
(CFGMask::IO_RST, 1),
(CFGMask::IO_UPDATE, 0)
]).unwrap();
config.set_configurations(&mut [
(CFGMask::IO_RST, 0),
(CFGMask::RST, 0),
2020-08-26 13:18:50 +08:00
(CFGMask::RF_SW, 13),
2020-08-26 16:49:37 +08:00
(CFGMask::DIV, 3)
2020-08-24 17:03:44 +08:00
]).unwrap();
2020-08-26 13:18:50 +08:00
dds0.init().unwrap();
dds0.set_configurations(&mut [
(DDSCFRMask::PDCLK_ENABLE, 0),
(DDSCFRMask::READ_EFFECTIVE_FTW, 1),
2020-08-24 17:03:44 +08:00
]).unwrap();
2020-08-27 17:09:35 +08:00
dds0.enable_pll(1_000_000_000).unwrap();
2020-08-24 17:03:44 +08:00
2020-08-27 17:09:35 +08:00
// Attenuator
att.set_attenuation([
0.0, 31.5, 24.0, 0.0
]).unwrap();
dds0.set_single_tone_profile(1, 10_000_000, 0.0, 0.5).unwrap();
config.set_configurations(&mut [
(CFGMask::PROFILE, 1),
]).unwrap();
// Setup RAM configuration
dds0.set_configurations(&mut [
(DDSCFRMask::RAM_ENABLE, 1),
(DDSCFRMask::RAM_PLAYBACK_DST, 2),
]).unwrap();
// Configure RAM profile 0
dds0.write_register(0x0E, &mut [
0x00, // Open
0x09, 0xC4, // Address step rate (2500)
0xFF, 0xC0, // End at address 1023
0x00, 0x00, // Start at address 0
0x04, // Recirculate mode
]).unwrap();
2020-09-14 17:33:50 +08:00
debug!("{:#X?}", dds0.read_register(0x0E, &mut[
2020-08-27 17:09:35 +08:00
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
2020-09-14 17:33:50 +08:00
]).unwrap());
2020-08-24 17:03:44 +08:00
2020-08-27 17:09:35 +08:00
// Choose profile 0
config.set_configurations(&mut [
(CFGMask::PROFILE, 0),
]).unwrap();
2020-08-24 17:03:44 +08:00
2020-08-27 17:09:35 +08:00
// Set RAM to be amplitudes, disable RAM momentarily
dds0.set_configurations(&mut [
(DDSCFRMask::RAM_PLAYBACK_DST, 0),
(DDSCFRMask::RAM_ENABLE, 0),
]).unwrap();
2020-08-24 17:03:44 +08:00
2020-08-27 17:09:35 +08:00
let mut ram_data: [u8; ((1024 * 4) + 1)] = [0; (1024 * 4) + 1];
ram_data[0] = 0x16;
for index in 0..1024 {
if index % 2 == 1 {
ram_data[(index * 4) + 1] = 0x3F;
ram_data[(index * 4) + 2] = 0xFF;
} else {
ram_data[(index * 4) + 1] = 0x00;
ram_data[(index * 4) + 2] = 0x00;
}
// ram_data[(index * 4) + 1] = ((index >> 2) & 0xFF) as u8;
// ram_data[(index * 4) + 2] = ((index & 0x03) << 6) as u8;
}
dds0.transfer(&mut ram_data).unwrap();
2020-08-17 12:15:11 +08:00
2020-08-27 17:09:35 +08:00
config.set_configurations(&mut [
(CFGMask::PROFILE, 1),
]).unwrap();
2020-08-24 17:03:44 +08:00
2020-08-27 17:09:35 +08:00
config.set_configurations(&mut [
(CFGMask::PROFILE, 0),
2020-08-24 10:57:37 +08:00
]).unwrap();
2020-08-27 17:09:35 +08:00
dds0.set_configurations(&mut [
(DDSCFRMask::RAM_ENABLE, 1),
]).unwrap();
2020-08-23 17:17:09 +08:00
2020-08-24 10:57:37 +08:00
loop {}
2020-08-07 13:36:00 +08:00
}