From 6fa2a6ebd87529a27e55d0bfa22a0f9e7fddcbe9 Mon Sep 17 00:00:00 2001 From: Robert Jordens Date: Thu, 24 Nov 2016 15:53:14 +0100 Subject: [PATCH] phaser: move ad9154 spi/jesd api to rust --- artiq/runtime.rs/libksupport/ad9154.rs | 75 +++++++++++++++++++++++++ artiq/runtime.rs/libksupport/api.rs | 31 ++++++----- artiq/runtime.rs/libksupport/lib.rs | 2 + artiq/runtime/Makefile | 2 +- artiq/runtime/ad9154.c | 77 -------------------------- artiq/runtime/ad9154.h | 19 ------- 6 files changed, 96 insertions(+), 110 deletions(-) create mode 100644 artiq/runtime.rs/libksupport/ad9154.rs delete mode 100644 artiq/runtime/ad9154.c delete mode 100644 artiq/runtime/ad9154.h diff --git a/artiq/runtime.rs/libksupport/ad9154.rs b/artiq/runtime.rs/libksupport/ad9154.rs new file mode 100644 index 000000000..ab7026258 --- /dev/null +++ b/artiq/runtime.rs/libksupport/ad9154.rs @@ -0,0 +1,75 @@ +use board::csr; + +pub extern fn init() { + unsafe { + csr::ad9154::spi_offline_write(1); + csr::ad9154::spi_cs_polarity_write(0); + csr::ad9154::spi_clk_polarity_write(0); + csr::ad9154::spi_clk_phase_write(0); + csr::ad9154::spi_lsb_first_write(0); + csr::ad9154::spi_half_duplex_write(0); + csr::ad9154::spi_clk_div_write_write(16); + csr::ad9154::spi_clk_div_read_write(16); + csr::ad9154::spi_xfer_len_write_write(24); + csr::ad9154::spi_xfer_len_read_write(0); + csr::ad9154::spi_cs_write(csr::CONFIG_AD9154_DAC_CS); + csr::ad9154::spi_offline_write(0); + } +} + +const AD9_READ: u16 = 1 << 15; + +pub extern fn dac_write(addr: u16, data: u8) { + unsafe { + csr::ad9154::spi_data_write_write( + ((addr as u32) << 16) | ((data as u32) << 8)); + while csr::ad9154::spi_pending_read() != 0 {} + while csr::ad9154::spi_active_read() != 0 {} + } +} + +pub extern fn dac_read(addr: u16) -> u8 { + unsafe { + dac_write(AD9_READ | addr, 0); + csr::ad9154::spi_data_read_read() as u8 + } +} + +pub extern fn clk_write(addr: u16, data: u8) { + unsafe { + csr::ad9154::spi_cs_write(csr::CONFIG_AD9154_CLK_CS); + dac_write(addr, data); + csr::ad9154::spi_cs_write(csr::CONFIG_AD9154_DAC_CS); + } +} + +pub extern fn clk_read(addr: u16) -> u8 { + unsafe { + clk_write(AD9_READ | addr, 0); + csr::ad9154::spi_data_read_read() as u8 + } +} + +pub extern fn jesd_enable(en: u32) { + unsafe { + csr::ad9154::jesd_control_enable_write(en); + } +} + +pub extern fn jesd_ready() { + unsafe { + csr::ad9154::jesd_control_ready_read(); + } +} + +pub extern fn jesd_prbs(p: u32) { + unsafe { + csr::ad9154::jesd_control_prbs_config_write(p); + } +} + +pub extern fn jesd_stpl(en: u32) { + unsafe { + csr::ad9154::jesd_control_stpl_enable_write(en); + } +} diff --git a/artiq/runtime.rs/libksupport/api.rs b/artiq/runtime.rs/libksupport/api.rs index f7dbf41c0..d27e8964f 100644 --- a/artiq/runtime.rs/libksupport/api.rs +++ b/artiq/runtime.rs/libksupport/api.rs @@ -117,17 +117,22 @@ static mut API: &'static [(&'static str, *const ())] = &[ #[cfg(has_i2c)] api!(i2c_read = ::i2c::read), -// #if (defined CONFIG_AD9154_CS) - api!(ad9154_init), - api!(ad9154_write), - api!(ad9154_read), - - api!(ad9516_write), - api!(ad9516_read), - - api!(ad9154_jesd_enable), - api!(ad9154_jesd_ready), - api!(ad9154_jesd_prbs), - api!(ad9154_jesd_stpl), -// #endif + #[cfg(has_ad9154)] + api!(ad9154_init = ::ad9154::init), + #[cfg(has_ad9154)] + api!(ad9154_write = ::ad9154::dac_write), + #[cfg(has_ad9154)] + api!(ad9154_read = ::ad9154::dac_read), + #[cfg(has_ad9154)] + api!(ad9516_write = ::ad9154::clk_write), + #[cfg(has_ad9154)] + api!(ad9516_read = ::ad9154::clk_read), + #[cfg(has_ad9154)] + api!(ad9154_jesd_enable = ::ad9154::jesd_enable), + #[cfg(has_ad9154)] + api!(ad9154_jesd_ready = ::ad9154::jesd_ready), + #[cfg(has_ad9154)] + api!(ad9154_jesd_prbs = ::ad9154::jesd_prbs), + #[cfg(has_ad9154)] + api!(ad9154_jesd_stpl = ::ad9154::jesd_stpl), ]; diff --git a/artiq/runtime.rs/libksupport/lib.rs b/artiq/runtime.rs/libksupport/lib.rs index 08673e759..66aecbe98 100644 --- a/artiq/runtime.rs/libksupport/lib.rs +++ b/artiq/runtime.rs/libksupport/lib.rs @@ -52,6 +52,8 @@ macro_rules! artiq_raise { mod rtio; #[cfg(has_i2c)] mod i2c; +#[cfg(has_ad9154)] +mod ad9154; use core::{mem, ptr, slice, str}; use std::io::Cursor; diff --git a/artiq/runtime/Makefile b/artiq/runtime/Makefile index 853154c8a..a8387466b 100644 --- a/artiq/runtime/Makefile +++ b/artiq/runtime/Makefile @@ -4,7 +4,7 @@ include $(MISOC_DIRECTORY)/software/common.mak PYTHON ?= python3.5 OBJECTS := flash_storage.o main.o -OBJECTS_KSUPPORT := ksupport_glue.o artiq_personality.o ad9154.o +OBJECTS_KSUPPORT := ksupport_glue.o artiq_personality.o RUSTOUT_DIRECTORY := cargo/or1k-unknown-none/debug CORE_IO_COMMIT := d40c593f42fafbac1ff3d827f6df96338b5b7d8b diff --git a/artiq/runtime/ad9154.c b/artiq/runtime/ad9154.c deleted file mode 100644 index 5bc5e49e2..000000000 --- a/artiq/runtime/ad9154.c +++ /dev/null @@ -1,77 +0,0 @@ -#include - -#include -#include - -#include "artiq_personality.h" -#include "ad9154.h" - -#ifdef CONFIG_AD9154_DAC_CS - -void ad9154_init(void) -{ - ad9154_spi_offline_write(1); - ad9154_spi_cs_polarity_write(0); - ad9154_spi_clk_polarity_write(0); - ad9154_spi_clk_phase_write(0); - ad9154_spi_lsb_first_write(0); - ad9154_spi_half_duplex_write(0); - ad9154_spi_clk_div_write_write(16); - ad9154_spi_clk_div_read_write(16); - ad9154_spi_xfer_len_write_write(24); - ad9154_spi_xfer_len_read_write(0); - ad9154_spi_cs_write(CONFIG_AD9154_DAC_CS); - ad9154_spi_offline_write(0); -} - -#define AD9_READ (1 << 15) -#define AD9_XFER(w) ((w) << 13) - -void ad9154_write(uint16_t addr, uint8_t data) -{ - ad9154_spi_data_write_write( - ((AD9_XFER(0) | addr) << 16) | (data << 8)); - while (ad9154_spi_pending_read()); - while (ad9154_spi_active_read()); -} - -uint8_t ad9154_read(uint16_t addr) -{ - ad9154_write(AD9_READ | addr, 0); - return ad9154_spi_data_read_read(); -} - -void ad9516_write(uint16_t addr, uint8_t data) -{ - ad9154_spi_cs_write(CONFIG_AD9154_CLK_CS); - ad9154_write(addr, data); - ad9154_spi_cs_write(CONFIG_AD9154_DAC_CS); -} - -uint8_t ad9516_read(uint16_t addr) -{ - ad9516_write(AD9_READ | addr, 0); - return ad9154_spi_data_read_read(); -} - -void ad9154_jesd_enable(int en) -{ - ad9154_jesd_control_enable_write(en); -} - -int ad9154_jesd_ready(void) -{ - return ad9154_jesd_control_ready_read(); -} - -void ad9154_jesd_prbs(int p) -{ - ad9154_jesd_control_prbs_config_write(p); -} - -void ad9154_jesd_stpl(int en) -{ - ad9154_jesd_control_stpl_enable_write(en); -} - -#endif /* CONFIG_AD9154_DAC_CS */ diff --git a/artiq/runtime/ad9154.h b/artiq/runtime/ad9154.h deleted file mode 100644 index d6fa93d85..000000000 --- a/artiq/runtime/ad9154.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef __AD9154_H -#define __AD9154_H - -#ifdef CONFIG_AD9154_DAC_CS - -void ad9154_init(void); -void ad9154_write(uint16_t addr, uint8_t data); -uint8_t ad9154_read(uint16_t addr); - -void ad9516_write(uint16_t addr, uint8_t data); -uint8_t ad9516_read(uint16_t addr); - -void ad9154_jesd_enable(int en); -int ad9154_jesd_ready(void); -void ad9154_jesd_prbs(int p); -void ad9154_jesd_stpl(int en); - -#endif -#endif