Skip to content

Instantly share code, notes, and snippets.

@chemicstry
Created July 11, 2022 23:46
Show Gist options
  • Select an option

  • Save chemicstry/a26c4e98a1d86d29a003e79a10e1439f to your computer and use it in GitHub Desktop.

Select an option

Save chemicstry/a26c4e98a1d86d29a003e79a10e1439f to your computer and use it in GitHub Desktop.
use core::mem::MaybeUninit;
use rtt_target::{rtt::{RttHeader, RttChannel}, set_print_channel, ChannelMode, UpChannel};
const UP_CHANNELS: usize = 1;
const DOWN_CHANNELS: usize = 0;
const TERMINAL_CHANNEL_SIZE: usize = 512;
#[repr(C)]
pub struct RttControlBlock {
header: RttHeader,
up_channels: [RttChannel; UP_CHANNELS],
down_channels: [RttChannel; DOWN_CHANNELS],
}
#[used]
#[no_mangle]
#[link_section = ".rtt_block"]
#[export_name = "_SEGGER_RTT"]
pub static mut CONTROL_BLOCK: MaybeUninit<RttControlBlock> = MaybeUninit::uninit();
#[link_section = ".rtt_block"]
static mut TERMINAL_CHANNEL: MaybeUninit<[u8; TERMINAL_CHANNEL_SIZE]> = MaybeUninit::uninit();
pub fn init_rtt() {
unsafe {
let id = core::slice::from_raw_parts(CONTROL_BLOCK.as_mut_ptr() as *mut u8, 16);
let cb = &mut *CONTROL_BLOCK.as_mut_ptr();
// Only initialize if not initialized
if id != b"SEGGER RTT\0\0\0\0\0\0" {
core::ptr::write_bytes(CONTROL_BLOCK.as_mut_ptr(), 0, core::mem::size_of::<RttControlBlock>());
cb.up_channels[0].init("Terminal\0".as_bytes().as_ptr(), ChannelMode::NoBlockSkip, TERMINAL_CHANNEL.as_mut_ptr());
cb.header.init(cb.up_channels.len(), cb.down_channels.len());
}
let terminal_channel = UpChannel::new(&mut cb.up_channels[0]);
set_print_channel(terminal_channel);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment