Skip to content

Instantly share code, notes, and snippets.

@luojia65
Last active November 16, 2025 03:33
Show Gist options
  • Select an option

  • Save luojia65/c21544191d026296cd2ccc00dc3ddd1e to your computer and use it in GitHub Desktop.

Select an option

Save luojia65/c21544191d026296cd2ccc00dc3ddd1e to your computer and use it in GitHub Desktop.
Complex blnky
use std::{env, path::PathBuf};
fn main() {
let out = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let ld = &out.join("artinchip-rt.ld");
std::fs::write(ld, LINKER_SCRIPT).unwrap();
println!("cargo:rustc-link-arg=-T{}", ld.display());
println!("cargo:rustc-link-search={}", out.display());
}
const LINKER_SCRIPT: &[u8] = b"OUTPUT_ARCH(riscv)
ENTRY(_start)
SECTIONS {
. = 0x30044000 - 0x8;
.head : ALIGN(4) {
KEEP(*(.head.pbp))
}
. = 0x30044000;
.text : ALIGN(4) {
*(.text.entry)
*(.text .text.*)
}
.rodata : ALIGN(4) {
srodata = .;
*(.rodata .rodata.*)
*(.srodata .srodata.*)
. = ALIGN(4);
erodata = .;
}
.data : ALIGN(4) {
sdata = .;
*(.data .data.*)
*(.sdata .sdata.*)
. = ALIGN(4);
edata = .;
}
sidata = LOADADDR(.data);
.bss (NOLOAD) : ALIGN(4) {
*(.bss.uninit)
sbss = .;
*(.bss .bss.*)
*(.sbss .sbss.*)
ebss = .;
}
/DISCARD/ : {
*(.eh_frame)
}
}";
#![no_std]
#![no_main]
use artinchip_hal::{cmu, gpio, prelude::*};
use artinchip_rt::{Peripherals, pbp_entry};
use panic_halt as _;
use riscv::asm::delay;
#[pbp_entry]
fn pbp_main(_boot_param: u32, _priv_data: &[u8]) {
let p = Peripherals::take();
let mut pa5 = p.gpioa.pa5.into_pull_up_output();
let pa10 = p.gpioa.pa10.into_function::<8>();
let pa11 = p.gpioa.pa11.into_function::<8>();
let data = 0x1111aaaa;
let mut i = 0;
loop {
if data & (1 << i) != 0 {
pa5.toggle().ok();
delay(1_000_000);
pa5.toggle().ok();
delay(1_000_000);
} else {
pa5.toggle().ok();
delay(500_000);
pa5.toggle().ok();
delay(500_000);
}
i += 1;
if i == 32 {
i = 0;
}
}
// let one = |pa5: &mut artinchip_hal::gpio::Output<'_>| {
// for _ in 0..4 {
// pa5.toggle().ok();
// delay(1_000_000);
// }
// };
// let zero = |pa5: &mut artinchip_hal::gpio::Output<'_>| {
// for _ in 0..4 {
// pa5.toggle().ok();
// delay(500_000);
// }
// };
// let data = 0x5555aaaa;
// for i in 0..31 {
// let bit = ((data >> i) & 0b1) != 0;
// if bit {
// one(&mut pa5);
// } else {
// zero(&mut pa5);
// }
// }
// delay(3_000_000);
// let cmu = unsafe { &*(0x18020000 as *const cmu::RegisterBlock) };
// // unsafe {
// // // 1.2GHz / (24 + 1) = 48MHz
// // cmu.clock_uart0.modify(|v| {
// // v.enable_module_reset()
// // .enable_module_clk()
// // .set_module_clk_div(24)
// // });
// // cmu.clock_uart0.modify(|v| v.enable_bus_clk());
// // cmu.clock_uart0.modify(|v| v.disable_module_reset());
// // }
// // let data: u32 = unsafe { core::mem::transmute(cmu.clock_uart0.read()) };
// // let gpio = unsafe { &*(0x18700000 as *const gpio::RegisterBlock) };
// // let gpioa = &gpio.groups[0];
// loop {
// for i in 0..31 {
// let bit = ((data >> i) & 0b1) != 0;
// if bit {
// one(&mut pa5);
// } else {
// zero(&mut pa5);
// }
// }
// delay(3_000_000);
// }
// // unsafe {
// // // pa0 = 0x325
// // gpioa.pin_config[0].modify(|v| {
// // v.set_pin_pull(gpio::PinPull::PullUp)
// // .set_drive_strength(gpio::PinDriveStrength::Level2)
// // .set_pin_func(5)
// // });
// // // pa1 = 0x325
// // gpioa.pin_config[1].modify(|v| {
// // v.set_pin_pull(gpio::PinPull::PullUp)
// // .set_drive_strength(gpio::PinDriveStrength::Level2)
// // .set_pin_func(5)
// // });
// // }
// let uart0 = unsafe { &*(0x18710000 as *const uart16550::Uart16550<u32>) };
// // 48MHz / 417 ≈ 115200 Bd * (1 - 0.08%)
// uart0.write_divisor(417);
// // 115200 8N1
// let lcr = uart0.lcr().read();
// uart0.lcr().write(
// lcr.set_char_len(uart16550::CharLen::EIGHT)
// .set_one_stop_bit(true)
// .set_parity(uart16550::PARITY::NONE),
// );
// uart0.write(b"Hello World from Rust Artinchip HAL!\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment