-
Notifications
You must be signed in to change notification settings - Fork 0
/
driverlib.rs
152 lines (133 loc) · 4.07 KB
/
driverlib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
mod driverwrapper {
#[link(name = "driverwrapper")]
extern "C" {
pub(super) fn init_system();
pub(super) fn uart_avail_host() -> bool;
pub(super) fn uart_avail_board() -> bool;
pub(super) fn uart_readb_host() -> i32;
pub(super) fn uart_readb_board() -> i32;
pub(super) fn uart_writeb_host(data: u8);
pub(super) fn uart_writeb_board(data: u8);
pub(super) fn eeprom_read(data: *mut u32, address: u32, length: u32);
pub(super) fn eeprom_write(data: *const u32, address: u32, length: u32);
pub(super) fn read_sw_1() -> bool;
pub(super) fn get_temp_samples(data: *mut u32);
pub(super) fn sleep_us(us: u32);
pub(super) fn start_delay_timer_us(us: u32);
pub(super) fn wait_delay_timer();
pub(super) fn get_remaining_us_delay_timer() -> u32;
pub(super) fn get_tick_timer() -> u64;
}
}
const EEPROM_SIZE: u32 = 0x800; // 2K
/// Set up the system. This should be called after Board::new().
pub fn init_system() {
unsafe {
driverwrapper::init_system();
}
}
/// Check if the host has sent a byte.
pub fn uart_avail_host() -> bool {
unsafe { driverwrapper::uart_avail_host() }
}
/// Check if the board has sent a byte.
pub fn uart_avail_board() -> bool {
unsafe { driverwrapper::uart_avail_board() }
}
/// Read a byte from the host.
pub fn uart_readb_host() -> u8 {
// return as u8
let ret: i32 = unsafe { driverwrapper::uart_readb_host() };
ret as u8
}
// Read bytes from the host into an array. Only reads data.len() bytes
pub fn uart_read_host(data: &mut [u8]) {
for byte in data {
*byte = uart_readb_host();
}
}
/// Read a byte from the board.
pub fn uart_readb_board() -> u8 {
let ret: i32 = unsafe { driverwrapper::uart_readb_board() };
ret as u8
}
// Read bytes from the board
pub fn uart_read_board(data: &mut [u8]){
for byte in data {
*byte = uart_readb_board();
}
}
/// Write a byte to the host.
pub fn uart_writeb_host(data: u8) {
unsafe {
driverwrapper::uart_writeb_host(data);
}
}
// Write bytes to the host
pub fn uart_write_host(data: & [u8]){
for byte in data{
uart_writeb_host(*byte);
}
}
/// Write a byte to the board.
pub fn uart_writeb_board(data: u8) {
unsafe {
driverwrapper::uart_writeb_board(data);
}
}
// Write bytes to the board
pub fn uart_write_board(data: &[u8]) {
for byte in data {
uart_writeb_board(*byte);
}
}
/// Read from the EEPROM. Address must be a multiple of 4.
pub fn eeprom_read(data: &mut [u32], address: u32) {
if data.len() == 0 {
return;
}
assert!(address + data.len() as u32 * 4 <= EEPROM_SIZE);
unsafe {
driverwrapper::eeprom_read(data.as_mut_ptr(), address, data.len() as u32 * 4);
}
}
/// Write to the EEPROM. Address must be a multiple of 4.
pub fn eeprom_write(data: &[u32], address: u32) {
if data.len() == 0 {
return;
}
assert!(address + data.len() as u32 * 4 <= EEPROM_SIZE);
unsafe {
driverwrapper::eeprom_write(data.as_ptr(), address, data.len() as u32 * 4);
}
}
/// Check if switch 1 is pressed. Returns true if pressed.
pub fn read_sw_1() -> bool {
unsafe { driverwrapper::read_sw_1() }
}
pub fn get_temp_samples(samples: &mut [u32; 8]) {
unsafe {
driverwrapper::get_temp_samples(samples.as_mut_ptr())
}
}
/// Waits for approximately the number of microseconds provided.
pub fn sleep_us(us: u32) {
assert_ne!(us, 0);
unsafe { driverwrapper::sleep_us(us) }
}
/// Sets up the delay timer to trigger after the microseconds provided
pub fn start_delay_timer_us(us: u32) {
unsafe { driverwrapper::start_delay_timer_us(us) }
}
/// Waits for delay timer to be completed
pub fn wait_delay_timer() {
unsafe { driverwrapper::wait_delay_timer() }
}
/// Gets remaining time on delay timer
pub fn get_remaining_us_delay_timer() -> u32 {
unsafe { driverwrapper::get_remaining_us_delay_timer() }
}
/// Returns counter from PIOSC from startup
pub fn get_tick_timer() -> u64 {
unsafe { driverwrapper::get_tick_timer() }
}