-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
243 lines (202 loc) · 7.02 KB
/
lib.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//! Impl of the UTP platform for the TI TM4C.
//!
//! TODO!
#![doc(test(attr(deny(rust_2018_idioms, warnings))))]
#![doc(html_logo_url = "")] // TODO!
#![no_std]
#[cfg(not(all(target_arch = "arm", target_os = "none")))]
compile_error!(
"
This crate only builds for `thumbv7em-none-eabihf`!
Please either pass `--target thumbv7em-none-eabihf` to `cargo` or
use one of the aliases (like `cargo r` to run) defined in `.cargo/config`.
");
pub const PANIC_DELIM: &str = "++++++++++";
pub const END_DELIM: &str = "==========";
extern crate tm4c123x_hal as hal;
use core::fmt::Write;
use hal::gpio::{AlternateFunction, AF1, PushPull};
use hal::gpio::gpioa::{PA1, PA0};
use hal::prelude::*;
use lc3_baseline_sim::interp::{Interpreter, MachineState, OwnedOrRef, PeripheralInterruptFlags};
use lc3_baseline_sim::sim::Simulator;
use lc3_device_support::{
memory::PartialMemory,
rpc::{
encoding::{Cobs, PostcardDecode, PostcardEncode},
transport::uart_simple::UartTransport,
},
util::Fifo,
};
use lc3_traits::control::rpc::{
Device, RequestMessage, ResponseMessage, SimpleEventFutureSharedState, EventFuture,
};
use lc3_traits::control::Control;
use lc3_traits::peripherals::stubs::{ClockStub, GpioStub, PwmStub};
use lc3_traits::peripherals::{
stubs::{/*PeripheralsStub,*/ InputStub, OutputStub},
PeripheralSet,
};
// use hal::{gpio::*, gpio::gpioe::*};
use lc3_tm4c::peripherals_tm4c::{
// gpio::{
// required_components as GpioComponents,
// physical_pins as Tm4cGpio,
// // GpioShim exists but it's not used for anything and doesn't impl Gpio?
// },
adc::{required_components as AdcComponents, AdcShim as Tm4cAdc},
// clock::{required_components as ClockComponents, Tm4cClock},
// pwm::{required_components as PwmComponents, PwmShim as Tm4cPwm},
timers::{required_components as TimerComponents, TimersShim as Tm4cTimers},
};
// Unforuntately, this type alias is incomplete.
// use lc3_tm4c::peripherals_tm4c::Peripheralstm4c;
pub static FLAGS: PeripheralInterruptFlags = PeripheralInterruptFlags::new();
// type Tm4cPeripheralSet<'int> = PeripheralSet<
// 'int,
// Tm4cGpio<'int>,
// Tm4cAdc,
// Tm4cPwm,
// Tm4cTimers<'int>,
// Tm4cClock,
// InputStub,
// OutputStub,
// >;
type PortA = tm4c123x::GPIO_PORTA;
type Uart0 = hal::serial::UART0;
type PowerControl = hal::sysctl::PowerControl;
type Clocks = hal::sysctl::Clocks;
pub fn setup(
state: &SimpleEventFutureSharedState,
) -> (impl Control<EventFuture = EventFuture<'_, SimpleEventFutureSharedState>> + '_, (PortA, Uart0, PowerControl, Clocks)) {
let p = hal::Peripherals::take().unwrap();
let mut sc = p.SYSCTL.constrain();
sc.clock_setup.oscillator = hal::sysctl::Oscillator::Main(
hal::sysctl::CrystalFrequency::_16mhz,
hal::sysctl::SystemClock::UsePll(hal::sysctl::PllOutputFrequency::_80_00mhz),
);
let clocks = sc.clock_setup.freeze();
// Peripheral Init:
let peripheral_set = {
// let portf = p.GPIO_PORTF;
// let portb = p.GPIO_PORTB;
// let gpio = Tm4cGpio::new(
// &sc.power_control,
// GpioComponents {
// portf,
// portb,
// },
// );
let gpio = GpioStub;
let adc0 = p.ADC0;
let adc1 = p.ADC1;
let porte = p.GPIO_PORTE;
let adc = Tm4cAdc::new(&sc.power_control, AdcComponents { adc0, adc1, porte });
// let portb = unsafe { hal::Peripherals::steal() }.GPIO_PORTB;
// let portd = p.GPIO_PORTD;
// let pwm0 = p.PWM0;
// let pwm1 = p.PWM1;
// Note: This will spin forever if you make the mistake of using an `lm4f` (which
// does not have PWM...).
// Perhaps we should have a `feature` for this at the very least?
// let pwm = Tm4cPwm::new(
// PwmComponents {
// portb,
// portd,
// pwm0,
// pwm1,
// },
// &sc.power_control,
// );
let pwm = PwmStub;
let timer0 = p.TIMER0;
let timer1 = p.TIMER1;
let timers = Tm4cTimers::new(&sc.power_control, TimerComponents { timer0, timer1 });
// let timer = p.TIMER2;
// let clock = Tm4cClock::new(
// ClockComponents {
// timer,
// },
// &sc.power_control,
// );
let clock = ClockStub;
PeripheralSet::new(gpio, adc, pwm, timers, clock, InputStub, OutputStub)
};
static mut MEMORY: PartialMemory = PartialMemory::new();
let interp: Interpreter<'static, _, _> = Interpreter::new(
// SAFETY: we have exclusive access!
unsafe {
&mut MEMORY
},
peripheral_set,
OwnedOrRef::Ref(&FLAGS),
[0; 8],
0x200,
MachineState::Running,
);
let sim = Simulator::new_with_state(interp, state);
let aux = (p.GPIO_PORTA, p.UART0, sc.power_control, clocks);
(sim, aux)
}
pub type Serial0 = hal::serial::Serial<
tm4c123x::UART0,
PA1<AlternateFunction<AF1, PushPull>>,
PA0<AlternateFunction<AF1, PushPull>>,
(),
(),
>;
pub fn setup_uart(porta: PortA, u0: Uart0, pc: &PowerControl, clocks: &Clocks) -> Serial0 {
let mut porta = porta.split(pc);
// Activate UART
hal::serial::Serial::uart0(
u0,
porta
.pa1
.into_af_push_pull::<hal::gpio::AF1>(&mut porta.control),
porta
.pa0
.into_af_push_pull::<hal::gpio::AF1>(&mut porta.control),
(),
(),
1_500_000_u32.bps(),
// hal::serial::NewlineMode::SwapLFtoCRLF,
hal::serial::NewlineMode::Binary,
&clocks,
pc,
)
}
// TODO: do away with this once we update `core`.
struct MutRefWrite<'w, W: embedded_hal::serial::Write<u8>>(&'w mut W);
impl<'w, W: embedded_hal::serial::Write<u8>> embedded_hal::serial::Write<u8> for MutRefWrite<'w, W> {
type Error = W::Error;
fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
self.0.write(word)
}
fn flush(&mut self) -> nb::Result<(), Self::Error> {
self.0.flush()
}
}
pub fn run<C: Control>(sim: &mut C, uart: Serial0) -> !
where
<C as Control>::EventFuture: Unpin,
{
let func: &dyn Fn() -> Cobs<Fifo<u8>> = &|| Cobs::try_new(Fifo::new()).unwrap();
let enc = PostcardEncode::<ResponseMessage, _, _>::new(func);
let dec = PostcardDecode::<RequestMessage, Cobs<Fifo<u8>>>::new();
let (tx, rx) = uart.split();
let mut tx = panic_write::PanicHandler::new_with_hook(tx, |w, panic_info| {
// TODO: multi-plexing friendly hook!
writeln!(w, "\n{}", PANIC_DELIM).unwrap();
writeln!(w, "{panic_info}").unwrap();
writeln!(w, "{}", PANIC_DELIM).unwrap();
});
let tx = MutRefWrite(&mut *tx);
let mut device = Device::<UartTransport<_, _>, _, RequestMessage, ResponseMessage, _, _>::new(
enc,
dec,
UartTransport::new(rx, tx),
);
loop {
let _ = device.step(sim);
}
}