Skip to content

Commit 217c96d

Browse files
pdscompsuchmememanyskill
authored andcommitted
Adding log-tail capability to dsp-to-serial
1 parent 5989834 commit 217c96d

2 files changed

Lines changed: 71 additions & 9 deletions

File tree

dsp-to-serial/src/main.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
use serialport::{SerialPort, TTYPort};
66

77
use crate::{
8-
communication_handler::CommunicationHandler, kbuf::{kbuf_use_new_buf}, msgbox::MsgboxEndpoint, sharespace::{sharespace_mmap}
8+
communication_handler::CommunicationHandler, kbuf::{kbuf_use_new_buf}, msgbox::MsgboxEndpoint, sharespace::{sharespace_mmap, mmap_log_buffer}
99
};
1010

1111
mod error;
@@ -15,6 +15,42 @@ mod sharespace;
1515
mod util;
1616
mod communication_handler;
1717

18+
fn log_tail_thread() {
19+
let log_buffer = mmap_log_buffer();
20+
21+
println!("Log buffer mapped at: {:p}", log_buffer.as_ptr());
22+
23+
let mut read_ptr = 4;
24+
25+
loop {
26+
let write_ptr = u32::from_le_bytes(log_buffer[0..4].try_into().unwrap()) as usize;
27+
28+
if write_ptr == read_ptr {
29+
std::thread::sleep(Duration::from_millis(100));
30+
continue;
31+
}
32+
33+
let process_buffer = |buffer: &[u8]| {
34+
for message in buffer.split(|&b| b == 0) {
35+
if !message.is_empty() {
36+
println!("DSP_log: {}", String::from_utf8_lossy(message).trim_end());
37+
}
38+
}
39+
};
40+
41+
if write_ptr > read_ptr {
42+
process_buffer(&log_buffer[read_ptr..write_ptr]);
43+
} else { // write_ptr < read_ptr, wrapped around
44+
process_buffer(&log_buffer[read_ptr..]);
45+
process_buffer(&log_buffer[4..write_ptr]);
46+
}
47+
48+
read_ptr = write_ptr;
49+
50+
std::thread::sleep(Duration::from_millis(100));
51+
}
52+
}
53+
1854
fn read_dsp(msgbox : &mut MsgboxEndpoint, handler : &mut CommunicationHandler, port: &mut TTYPort) {
1955
if !msgbox.msgbox_has_signal()
2056
{
@@ -72,6 +108,9 @@ fn write_dsp(msgbox : &mut MsgboxEndpoint, handler : &mut CommunicationHandler,
72108

73109
fn main() {
74110
println!("Hello, world!");
111+
112+
std::thread::spawn(log_tail_thread);
113+
75114
let mmap = sharespace_mmap();
76115
println!("Got sharespace mmap!");
77116
let kbuf = kbuf_use_new_buf(mmap.dsp_sharespace.arm_write_addr).unwrap();

dsp-to-serial/src/sharespace.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::os::fd::{AsRawFd, OwnedFd};
22

3-
use memmap2::{MmapMut, MmapOptions};
3+
use memmap2::{Mmap, MmapMut, MmapOptions};
44
use nix::{errno::Errno, fcntl::{open, OFlag}, ioctl_readwrite_bad, sys::stat::Mode};
55

66
use crate::util::wrap_ioctl_negative_invalid;
77

88
#[repr(C)]
99
#[derive(Default, Debug)]
10-
struct DebugMessage {
10+
pub struct DebugMessage {
1111
pub sys_cnt: u32,
1212
pub log_head_addr: u32,
1313
pub log_end_addr: u32,
@@ -33,16 +33,17 @@ pub struct DspSharespace {
3333
pub debug_msg: DebugMessage,
3434
}
3535

36-
#[derive(Debug)]
37-
enum ChooseShareSpace {
36+
#[derive(Debug, Clone, Copy)]
37+
pub enum ChooseShareSpace {
3838
ChooseDspWriteSpace = 0,
3939
ChooseArmWriteSpace = 1,
40+
ChooseLogSpace = 2,
4041
}
4142

4243
ioctl_readwrite_bad!(read_debug_message, 0x01, DspSharespace);
4344
ioctl_readwrite_bad!(write_debug_message, 0x03, DspSharespace);
4445

45-
fn choose_sharespace(
46+
pub fn choose_sharespace(
4647
fd: &OwnedFd,
4748
msg: &mut DspSharespace,
4849
choose: ChooseShareSpace,
@@ -53,7 +54,15 @@ fn choose_sharespace(
5354
msg.mmap_phy_addr = match choose {
5455
ChooseShareSpace::ChooseDspWriteSpace => msg.dsp_write_addr,
5556
ChooseShareSpace::ChooseArmWriteSpace => msg.arm_write_addr,
57+
ChooseShareSpace::ChooseLogSpace => msg.dsp_log_addr,
5658
};
59+
60+
msg.mmap_phy_size = match choose {
61+
ChooseShareSpace::ChooseDspWriteSpace => msg.dsp_write_size,
62+
ChooseShareSpace::ChooseArmWriteSpace => msg.arm_write_size,
63+
ChooseShareSpace::ChooseLogSpace => msg.dsp_log_size,
64+
};
65+
5766

5867
println!("Init sharespace {:?} to 0x{:x}", choose, msg.mmap_phy_addr);
5968

@@ -62,16 +71,30 @@ fn choose_sharespace(
6271
Ok(())
6372
}
6473

65-
fn sharespace_open() -> Result<OwnedFd, Errno> {
74+
pub fn sharespace_open() -> Result<OwnedFd, Errno> {
6675
open(
6776
"/dev/dsp_debug",
6877
OFlag::O_RDWR | OFlag::O_SYNC | OFlag::O_NONBLOCK,
6978
Mode::empty(),
7079
)
7180
}
7281

82+
pub fn mmap_log_buffer() -> Mmap {
83+
let mut dsp_sharespace = DspSharespace::default();
84+
let fd = sharespace_open().unwrap();
85+
86+
choose_sharespace(
87+
&fd,
88+
&mut dsp_sharespace,
89+
ChooseShareSpace::ChooseLogSpace,
90+
)
91+
.unwrap();
92+
93+
unsafe { MmapOptions::new().len(dsp_sharespace.dsp_log_size as usize).map(&fd).unwrap() }
94+
}
95+
7396
pub struct Sharespace {
74-
fd: OwnedFd,
97+
pub fd: OwnedFd,
7598
pub dsp_sharespace: DspSharespace,
7699
pub write_buffer: MmapMut, // ARM buffer - pu8ArmBuf
77100
}
@@ -87,7 +110,7 @@ pub fn sharespace_mmap() -> Sharespace {
87110
)
88111
.unwrap();
89112

90-
let write_buffer = unsafe { MmapOptions::new().len(0x1000).map_mut(&fd).unwrap() };
113+
let write_buffer = unsafe { MmapOptions::new().len(dsp_sharespace.mmap_phy_size as usize).map_mut(&fd).unwrap() };
91114

92115
Sharespace {
93116
fd,

0 commit comments

Comments
 (0)