humpback-dds/src/main.rs

130 lines
2.9 KiB
Rust
Raw Normal View History

2020-08-07 13:36:00 +08:00
#![no_main]
#![no_std]
use panic_semihosting as _;
use stm32h7xx_hal::hal::digital::v2::{
InputPin,
OutputPin,
};
use stm32h7xx_hal::{pac, prelude::*, spi};
use cortex_m;
use cortex_m::asm::nop;
use cortex_m_rt::entry;
use cortex_m_semihosting::hprintln;
use core::ptr;
use nb::block;
2020-08-11 00:07:07 +08:00
use firmware;
use firmware::{
CPLD,
attenuator::Attenuator,
2020-08-12 11:50:24 +08:00
config_register::{
ConfigRegister,
CFGMask,
},
2020-08-17 12:15:11 +08:00
dds::DDS,
2020-08-11 00:07:07 +08:00
};
2020-08-07 13:36:00 +08:00
#[entry]
fn main() -> ! {
let cp = cortex_m::Peripherals::take().unwrap();
let dp = pac::Peripherals::take().unwrap();
let pwr = dp.PWR.constrain();
let vos = pwr.freeze();
let rcc = dp.RCC.constrain();
let ccdr = rcc
.sys_ck(400.mhz())
.pll1_q_ck(48.mhz())
.freeze(vos, &dp.SYSCFG);
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() {
Ok(true) => hprintln!("FPGA is ready."),
Ok(_) => hprintln!("FPGA is in reset state."),
Err(_) => hprintln!("Error: Cannot read C_DONE"),
}.unwrap();
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-21 14:18:33 +08:00
// let mut sclk = gpioa.pa5.into_push_pull_output();
// let mut mosi = gpiob.pb5.into_push_pull_output();
// let mut miso = gpioa.pa6.into_pull_up_input();
2020-08-09 13:42:18 +08:00
2020-08-21 14:18:33 +08:00
let (mut cs0, mut cs1, mut 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-09 13:42:18 +08:00
let mut spi = dp.SPI1.spi(
(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-21 14:18:33 +08:00
// debug led
let mut yellow = gpioe.pe1.into_push_pull_output();
yellow.set_high().unwrap();
2020-08-07 13:36:00 +08:00
2020-08-21 14:18:33 +08:00
let mut switch = CPLD::new(spi, (cs0, cs1, cs2));
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);
loop {
// let mut counter = config.get_configuration(CFGMask::RF_SW);
// hprintln!("{}", counter);
// config.set_configurations(&mut [
// (CFGMask::RF_SW, ((counter + 1)%16) as u32)
// ]).unwrap();
hprintln!("{:?}", att.set_attenuation([33.0, -1.0, 24.0, 12.0]).unwrap()).unwrap();
hprintln!("{:?}", att.set_attenuation([0.0, 0.0, 0.0, 0.0]).unwrap()).unwrap();
}
2020-08-17 12:15:11 +08:00
2020-08-21 14:18:33 +08:00
/*
cs0.set_low().unwrap();
cs1.set_low().unwrap();
cs2.set_low().unwrap();
let mut arr: [u8; 4] = [0x12, 0x34, 0x56, 0x78];
2020-08-07 13:36:00 +08:00
loop {
2020-08-21 14:18:33 +08:00
arr[0] = 0x12;
arr[1] = 0x34;
arr[2] = 0x56;
arr[3] = 0x78;
cs1.set_high().unwrap();
hprintln!("{:?}", spi.transfer(&mut arr).unwrap()).unwrap();
cs1.set_low().unwrap();
2020-08-07 13:36:00 +08:00
}
2020-08-21 14:18:33 +08:00
*/
2020-08-07 13:36:00 +08:00
}