-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #577 from bittide/add-deviceIdentifierWb
Add `readDnaPortE2Wb`
- Loading branch information
Showing
15 changed files
with
282 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
-- SPDX-FileCopyrightText: 2024 Google LLC | ||
-- | ||
-- SPDX-License-Identifier: Apache-2.0 | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE NumericUnderscores #-} | ||
{-# OPTIONS_GHC -fplugin=Protocols.Plugin #-} | ||
|
||
module Wishbone.DnaPortE2 where | ||
|
||
import Clash.Explicit.Prelude | ||
import Clash.Prelude(withClockResetEnable) | ||
|
||
import Clash.Cores.UART(uart, ValidBaud) | ||
import Clash.Cores.Xilinx.Unisim.DnaPortE2 | ||
import Clash.Explicit.Testbench | ||
import Clash.Xilinx.ClockGen | ||
import Data.Char | ||
import Data.Maybe | ||
import Language.Haskell.TH | ||
import Numeric | ||
import Project.FilePath | ||
import Protocols | ||
import System.FilePath | ||
import Test.Tasty | ||
import Test.Tasty.HUnit | ||
import Test.Tasty.TH | ||
import VexRiscv | ||
|
||
import Bittide.DoubleBufferedRam | ||
import Bittide.Instances.Domains | ||
import Bittide.ProcessingElement | ||
import Bittide.ProcessingElement.Util | ||
import Bittide.SharedTypes | ||
import Bittide.Wishbone | ||
import Clash.Cores.UART.Extra(MaxBaudRate) | ||
|
||
import qualified Prelude as P | ||
|
||
-- | Test whether we can read the DNA from the DNA port peripheral. | ||
case_dna_port_self_test :: Assertion | ||
case_dna_port_self_test = assertBool msg (receivedDna == simDna2) | ||
where | ||
msg = "Received dna " <> showHex receivedDna "" <> " not equal to expected dna " <> showHex simDna2 "" | ||
receivedDna = parseResult simResult | ||
baud = SNat @(MaxBaudRate Basic50) | ||
clk = clockGen | ||
rst = resetGen | ||
ena = enableGen | ||
simResult = fmap (chr . fromIntegral) $ catMaybes $ sampleN 100_000 uartStream | ||
(uartStream, _, _) = withClockResetEnable (clockGen @Basic50) rst ena $ uart baud uartTx (pure Nothing) | ||
uartTx = dut baud (clockToDiffClock clk) rst (pure 0) | ||
|
||
-- | A simple instance containing just VexRisc with UART and the DNA peripheral which | ||
-- runs the `dna_port_e2_test` binary from `firmware-binaries`. | ||
dut :: | ||
forall dom baud . | ||
(KnownDomain dom, ValidBaud dom baud) => | ||
SNat baud -> | ||
"SYSCLK_300" ::: DiffClock Ext300 -> | ||
"CPU_RESET" ::: Reset dom -> | ||
"USB_UART_TX" ::: Signal dom Bit -> | ||
"USB_UART_RX" ::: Signal dom Bit | ||
dut baud diffClk rst_in usbUartTx = usbUartRx | ||
where | ||
(_, usbUartRx) = go ((usbUartTx, pure $ JtagIn low low low), pure ()) | ||
|
||
go = | ||
toSignals $ withClockResetEnable clk200 rst200 enableGen $ | ||
circuit $ \(uartRx, jtag) -> do | ||
[uartBus, dnaWb] <- processingElement @dom peConfig -< jtag | ||
(uartTx, _uartStatus) <- uartWb d256 d16 baud -< (uartBus, uartRx) | ||
readDnaPortE2Wb simDna2 -< dnaWb | ||
idC -< uartTx | ||
|
||
(clk200 :: Clock dom, pllLock :: Reset dom) = clockWizardDifferential diffClk noReset | ||
rst200 = resetSynchronizer clk200 (unsafeOrReset rst_in pllLock) | ||
|
||
(iMem, dMem) = $(do | ||
root <- runIO $ findParentContaining "cabal.project" | ||
let | ||
elfDir = root </> firmwareBinariesDir "riscv32imc-unknown-none-elf" Release | ||
elfPath = elfDir </> "dna_port_e2_test" | ||
|
||
memBlobsFromElf BigEndian (Nothing, Nothing) elfPath Nothing) | ||
|
||
peConfig = | ||
PeConfig (0b00 :> 0b01 :> 0b10 :> 0b11 :> Nil) | ||
(Reloadable $ Blob iMem) | ||
(Reloadable $ Blob dMem) | ||
|
||
parseResult :: String -> BitVector 96 | ||
parseResult = pack . (read :: String -> Unsigned 96) . P.head . lines | ||
|
||
tests :: TestTree | ||
tests = $(testGroupGenerator) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# SPDX-FileCopyrightText: 2024 Google LLC | ||
# | ||
# SPDX-License-Identifier: CC0-1.0 | ||
|
||
[package] | ||
name = "dna_port_e2_test" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license = "Apache-2.0" | ||
authors = ["Google LLC"] | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
riscv-rt = "0.11.0" | ||
bittide-sys = { path = "../../../firmware-support/bittide-sys"} | ||
ufmt = "0.2.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-FileCopyrightText: 2024 Google LLC | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::env; | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
/// Put the linker script somewhere the linker can find it. | ||
fn main() { | ||
let out_dir = env::var("OUT_DIR").expect("No out dir"); | ||
let dest_path = Path::new(&out_dir).join("memory.x"); | ||
fs::write(dest_path, include_bytes!("memory.x")).expect("Could not write file"); | ||
|
||
if env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "riscv32" { | ||
println!("cargo:rustc-link-arg=-Tmemory.x"); | ||
println!("cargo:rustc-link-arg=-Tlink.x"); // linker script from riscv-rt | ||
} | ||
println!("cargo:rustc-link-search={out_dir}"); | ||
|
||
println!("cargo:rerun-if-changed=memory.x"); | ||
println!("cargo:rerun-if-changed=build.rs"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
SPDX-FileCopyrightText: 2024 Google LLC | ||
SPDX-License-Identifier: CC0-1.0 | ||
*/ | ||
|
||
MEMORY | ||
{ | ||
IMEM : ORIGIN = 0x80000000, LENGTH = 64K | ||
DMEM : ORIGIN = 0x40000000, LENGTH = 32K | ||
} | ||
|
||
REGION_ALIAS("REGION_TEXT", IMEM); | ||
REGION_ALIAS("REGION_RODATA", DMEM); | ||
REGION_ALIAS("REGION_DATA", DMEM); | ||
REGION_ALIAS("REGION_BSS", DMEM); | ||
REGION_ALIAS("REGION_HEAP", DMEM); | ||
REGION_ALIAS("REGION_STACK", DMEM); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// SPDX-FileCopyrightText: 2024 Google LLC | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
#![no_std] | ||
#![cfg_attr(not(test), no_main)] | ||
|
||
use ufmt::uwriteln; | ||
|
||
use bittide_sys::dna_port_e2::{dna_to_u128, DnaValue}; | ||
use bittide_sys::uart::Uart; | ||
#[cfg(not(test))] | ||
use riscv_rt::entry; | ||
|
||
const DNA_ADDR: *const DnaValue = 0xC000_0000 as *const DnaValue; | ||
|
||
#[cfg_attr(not(test), entry)] | ||
fn main() -> ! { | ||
// Initialize peripherals. | ||
let mut uart = unsafe { Uart::new(0x8000_0000 as *mut u8) }; | ||
let dna = dna_to_u128(unsafe { *DNA_ADDR }); | ||
uwriteln!(uart, "{}", dna).unwrap(); | ||
loop { | ||
continue; | ||
} | ||
} | ||
|
||
#[panic_handler] | ||
fn panic_handler(_info: &core::panic::PanicInfo) -> ! { | ||
loop { | ||
continue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// SPDX-FileCopyrightText: 2024 Google LLC | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
/// The `DnaValue` type is a unsigned 96-bit integer. We represent it as a 12-byte array | ||
/// because Rust does not have a built-in 96-bit integer type. | ||
pub type DnaValue = [u8; 12]; | ||
|
||
/// Convert a `DnaValue` to a `u128` integer. | ||
pub fn dna_to_u128(dna: DnaValue) -> u128 { | ||
let mut u128_array = [0u8; 16]; | ||
u128_array[..12].copy_from_slice(&dna); | ||
u128::from_le_bytes(u128_array) | ||
} |
Oops, something went wrong.