EE445M RTOS
Taken at the University of Texas Spring 2015
move-main.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset: 4; */
2 /* Created by Hershal Bhave and Eric Crosson on <2015-03-15 Sun> */
3 /* Revision History: Look in Git FGT */
4 
5 #include "adcpp.hpp"
6 #include "blinker.hpp"
7 #include "uartpp.hpp"
8 #include "shellpp.hpp"
9 #include "semaphorepp.hpp"
10 #include "motorpp.hpp"
11 #include "drivepp.hpp"
12 #include "canpp.hpp"
13 #include "ctlsysctl.hpp"
14 #include "switchpp.hpp"
15 
16 #include "libos/os.h"
17 #include "libschedule/schedule.h"
18 
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <stdbool.h>
22 
23 #include "inc/hw_memmap.h"
24 
25 #include "driverlib/adc.h"
26 #include "driverlib/sysctl.h"
27 #include "driverlib/gpio.h"
28 #include "driverlib/interrupt.h"
29 #include "driverlib/uart.h"
30 #include "driverlib/pwm.h"
31 
32 #define thread(x) \
33  do { \
34  x; \
35  os_surrender_context(); \
36  } while(true)
37 
41 
42 uint16_t sens_ir_left;
44 uint16_t sens_ir_right;
46 uint16_t sens_ping_front;
47 
51 
55 
56 #define can_data_length 10
57 const uint32_t can_msg_id = 1;
58 const bool can_sender = false;
61 
62 #define UART0_RX_BUFFER_SIZE 8
65 
66 uint32_t blink_count_red = 0;
67 uint32_t blink_count_green = 0;
68 uint32_t blink_count_blue = 0;
69 
73 
74 #define BLINK_RED_WAIT_FOR_SEM 1
75 #define BLINK_BLUE_WAIT_FOR_SEM 1
76 #define BLINK_GREEN_WAIT_FOR_SEM 1
77 
79 
80  while(1) {
81 #if BLINK_RED_WAIT_FOR_SEM == 1
82  if (sem_blink_red.guard()) {
83 #endif
84  blink.toggle(PIN_RED);
86 #if BLINK_RED_WAIT_FOR_SEM == 1
87  }
88 #endif
89  os_surrender_context();
90  }
91 }
92 
94 
95  while(1) {
96 #if BLINK_BLUE_WAIT_FOR_SEM == 1
97  if (sem_blink_blue.guard()) {
98 #endif
99  blink.toggle(PIN_BLUE);
101 #if BLINK_BLUE_WAIT_FOR_SEM == 1
102  }
103 #endif
104  os_surrender_context();
105  }
106 }
107 
109 
110  while(1) {
111 #if BLINK_GREEN_WAIT_FOR_SEM == 1
112  if (sem_blink_green.guard()) {
113 #endif
114  blink.toggle(PIN_GREEN);
116 #if BLINK_GREEN_WAIT_FOR_SEM == 1
117  }
118 #endif
119  os_surrender_context();
120  }
121 }
122 
124 
125  thread (
126  uart0.atomic_printf("%x %x %x\n\r", blink_count_red,
128  );
129 }
130 
131 extern "C" void __cxa_pure_virtual() { while (1) {} }
132 
133 extern "C" void UART0_Handler(void) {
134 
135  if(!(uart0.ack() & (UART_INT_RX | UART_INT_RT))) {
136  return;
137  }
138 
139  while(UARTCharsAvail(UART0_BASE)) {
140  char recv = uart0.get_char();
141 
142  /* Regardless of newline received, our convention is to
143  * mark end-of-lines in a buffer with the CR character. */
144  switch(recv) {
145  case '\n':
146  if (uart::LAST_WAS_CR) {
147  uart::LAST_WAS_CR = false;
148  continue;
149  }
150  break;
151  case '\r':
152  uart::LAST_WAS_CR = true;
153  break;
154  case 0x1b:
155  recv = '\r';
156  break;
157  default: break;
158  }
159  UART0_RX_BUFFER.notify((const int8_t) recv);
160  blink.blink(PIN_RED);
161  }
162 }
163 
164 extern "C" void CAN0_Handler(void) {
165 
166  uint32_t message_id = can0.ack();
167 
168  switch(message_id) {
169  case 1:
170  can0.recv_sem.post();
171  break;
172  default:
173  can0.error_tx();
174  break;
175  }
176 }
177 
178 extern "C" void GPIOPortE_Handler() {
179 
180  switch0.ack();
181  switch0.debounce();
182 }
183 
185 
186  const uint32_t counter_max = SysCtlClockGet()/25;
187  uint32_t pins, counter;
188  percent_t left_speed, right_speed;
189 
190  while(1) {
191  if(sem_switch.guard() && switch0.debounced_data) {
192  counter = 0;
193 
194  if(switch0.debounced_data & GPIO_PIN_1) {
195  left_speed = 100;
196  right_speed = 0;
197  } else if(switch0.debounced_data & GPIO_PIN_2) {
198  left_speed = 0;
199  right_speed = 100;
200  }
201 
202  motor0.reverse();
203  motor1.reverse();
204  while (++counter < counter_max) { }
205  motor0.reverse();
206  motor1.reverse();
207  drive0.reset_history();
208  }
209  os_surrender_context();
210  }
211 }
212 
213 void can_handler(void) {
214 
215  uint16_t can_msg_id;
216 
217  while(1) {
218  if(can0.recv_sem.guard()) {
219 
220  can0.get(can_data);
221  blink.toggle(PIN_BLUE);
222 
223  can_msg_id = (can_data[1] << 8) | (can_data[0]);
224 
225  if (can_msg_id == 0) {
226  sens_ir_left = (can_data[3] << 8) | (can_data[2]);
227  sens_ir_left_front = (can_data[5] << 8) | (can_data[4]);
228  sens_ir_right = (can_data[7] << 8) | (can_data[4]);
229  } else {
230  sens_ir_right_front = (can_data[3] << 8) | (can_data[2]);
231  sens_ping_front = (can_data[5] << 8) | (can_data[4]);
232  }
233 
234  uart0.atomic_printf(" \r");
235  uart0.atomic_printf("l: %u lf: %u r: %u rf: %u pf: %u\r",
239 
240  /* uart0.atomic_printf("Received CAN data: %0X %0X %0X %0X %0X %0X %0X %0X \n", */
241  /* can_data[0], can_data[1], can_data[2], can_data[3], */
242  /* can_data[4], can_data[5], can_data[6], can_data[7]); */
243  }
244  os_surrender_context();
245  }
246 }
247 
249 
250  while(1) {
251  if(UART0_RX_SEM.guard()) {
252 
253  bool ok;
254  char recv = UART0_RX_BUFFER.get(ok);
255 
256  if(ok) { shell0.accept(recv); }
257  }
258  os_surrender_context();
259  }
260 }
261 
263 void motor_control(void) {
264 
265  while(1) {
266  if(motor_start.guard()) {
267  drive0.start();
268  }
269  if(motor_stop.guard()) {
270  drive0.stop();
271  }
272  os_surrender_context();
273  }
274 }
275 
276 extern "C" void Timer1A_Handler() {
277 
278  switch0.end_debounce();
279 }
280 
281 extern "C" void Timer0A_Handler() {
282 
283  countdown_timer.ack();
284  drive0.stop();
285 }
286 
287 void driver(void) {
288 
289  while(1) {
292  os_surrender_context();
293  }
294 }
295 
296 int main(void) {
297 
299  IntMasterDisable();
300 
301  blink = blinker(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
302 
303  sem_blink_red = semaphore();
304  sem_blink_green = semaphore();
305  sem_blink_blue = semaphore();
306 
308  uart0 = uart(UART0_BASE, INT_UART0);
309 
310  can0 = can(CAN0_BASE, INT_CAN0, can_sender, can_data_length);
311 
312  motor_start = semaphore();
313  motor_stop = semaphore();
314  shell0 = shell(&uart0, &motor_start, &motor_stop);
315  motor0 = motor(GPIO_PORTA_BASE, GPIO_PIN_6, PWM0_BASE, PWM_GEN_0, PWM_OUT_0);
316  motor1 = motor(GPIO_PORTA_BASE, GPIO_PIN_7, PWM0_BASE, PWM_GEN_0, PWM_OUT_1);
317  drive0 = drive(&motor0, &motor1, 50);
318  countdown_timer = timer(0, TIMER_BOTH, TIMER_CFG_ONE_SHOT, SysCtlClockGet()*180,
319  TIMER_TIMA_TIMEOUT, true);
320  switch0 = lswitch(GPIO_PORTE_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3,
321  &sem_switch, 1, TIMER_A, GPIO_BOTH_EDGES,
322  INT_GPIOE_TM4C123, true);
323 
324  os_threading_init();
325  schedule(motor_control, 200);
326  schedule(thread_blink_red, 200);
327  schedule(thread_blink_blue, 200);
328  schedule(thread_blink_green, 200);
329  schedule(shell_handler, 200);
330  /* schedule(thread_uart_update, 200); */
331  schedule(switch_responder, 200);
332  schedule(driver, 200);
333  schedule(can_handler, 200);
334  os_launch();
335 }
336 
337 /* Local Variables: */
338 /* firestarter: (compile "make -k -j32 -C ~/workspace/ee445m-labs/build/") */
339 /* End: */
can can0
Definition: move-main.cpp:59
static semaphore UART0_RX_SEM
Definition: move-main.cpp:63
Definition: uartpp.hpp:28
bool guard(void)
Definition: semaphorepp.cpp:57
void thread_blink_red()
Definition: move-main.cpp:78
void atomic_printf(const char *pcString,...)
Definition: uartpp.cpp:348
semaphore sem_blink_red
Definition: move-main.cpp:70
semaphore sem_blink_green
Definition: move-main.cpp:72
void Timer0A_Handler()
Definition: move-main.cpp:281
void post(void)
Definition: semaphorepp.cpp:45
const pin_t PIN_BLUE
Definition: blinker.hpp:20
#define thread(x)
Definition: move-main.cpp:32
virtual uint32_t ack(void)
Definition: canpp.cpp:146
void UART0_Handler(void)
Definition: move-main.cpp:133
uint16_t sens_ir_left
Definition: move-main.cpp:42
semaphore recv_sem
Definition: canpp.hpp:69
void notify(const T data)
Definition: bufferpp.hpp:48
char get_char(void)
Definition: uartpp.cpp:381
void steer(uint32_t left_sens, uint32_t left_front_sens, uint32_t right_sens, uint32_t right_front_sens, uint32_t front_sens)
Definition: drivepp.cpp:50
uint16_t sens_ir_right_front
Definition: move-main.cpp:45
void can_handler(void)
Definition: move-main.cpp:213
void thread_uart_update()
Definition: move-main.cpp:123
void thread_blink_blue()
Definition: move-main.cpp:93
void switch_responder()
Definition: move-main.cpp:184
int32_t percent_t
Definition: motorpp.hpp:15
drive drive0
Definition: move-main.cpp:54
motor motor0
Definition: move-main.cpp:53
const uint32_t can_msg_id
Definition: move-main.cpp:57
uint16_t sens_ir_left_front
Definition: move-main.cpp:43
T get(bool &ok)
Definition: bufferpp.hpp:76
const bool can_sender
Definition: move-main.cpp:58
uint32_t end_debounce(void)
Definition: switchpp.cpp:74
uint32_t debounced_data
Definition: switchpp.hpp:40
uint8_t can_data[10]
Definition: move-main.cpp:60
blinker blink
Definition: move-main.cpp:38
void error_tx(void)
Definition: canpp.cpp:97
virtual uint32_t ack(void)
Definition: switchpp.cpp:83
uint32_t blink_count_blue
Definition: move-main.cpp:68
uint32_t blink_count_green
Definition: move-main.cpp:67
static bool LAST_WAS_CR
Definition: uartpp.hpp:38
void __cxa_pure_virtual()
Definition: move-main.cpp:131
void Timer1A_Handler()
Definition: move-main.cpp:276
semaphore motor_start
Definition: move-main.cpp:52
static buffer< char, 8 > UART0_RX_BUFFER
Definition: move-main.cpp:64
const pin_t PIN_RED
Definition: blinker.hpp:18
void motor_control(void)
Definition: move-main.cpp:263
#define can_data_length
Definition: move-main.cpp:56
Definition: canpp.hpp:21
void CAN0_Handler(void)
Definition: move-main.cpp:164
void reverse(void)
Definition: motorpp.cpp:136
virtual uint32_t ack()
Definition: timerpp.cpp:74
lswitch switch0
Definition: move-main.cpp:48
void accept(char ch)
Definition: shellpp.cpp:174
void driver(void)
Definition: move-main.cpp:287
void GPIOPortE_Handler()
Definition: move-main.cpp:178
motor motor1
Definition: move-main.cpp:53
semaphore sem_blink_blue
Definition: move-main.cpp:71
static void set_clock(void)
Definition: ctlsysctl.hpp:109
virtual uint32_t ack(void)
Definition: uartpp.cpp:386
virtual void blink(pin_t pin)
Definition: blinker.cpp:38
shell shell0
Definition: move-main.cpp:40
void thread_blink_green()
Definition: move-main.cpp:108
void stop(void)
Definition: drivepp.cpp:20
uint16_t sens_ping_front
Definition: move-main.cpp:46
void start(void)
Definition: drivepp.cpp:26
void reset_history()
Definition: drivepp.cpp:122
virtual void toggle(pin_t pin)
Definition: blinker.cpp:34
timer countdown_timer
Definition: move-main.cpp:50
uint32_t blink_count_red
Definition: move-main.cpp:66
const pin_t PIN_GREEN
Definition: blinker.hpp:19
semaphore sem_switch
Definition: move-main.cpp:49
void debounce(void)
Definition: switchpp.cpp:65
semaphore motor_stop
Definition: move-main.cpp:52
int main(void)
Definition: move-main.cpp:296
void shell_handler()
Definition: move-main.cpp:248
uart uart0
Definition: move-main.cpp:39
void get(uint8_t *data)
Definition: canpp.cpp:102
uint16_t sens_ir_right
Definition: move-main.cpp:44