EE445M RTOS
Taken at the University of Texas Spring 2015
pingpp.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset: 4; -*- */
2 /* Created by Hershal Bhave and Eric Crosson 2015-04 */
3 /* Revision history: Look in Git FGT */
4 #include "pingpp.hpp"
5 
6 #include "delay.hpp"
7 #include "ctlsysctl.hpp"
8 
9 #include "driverlib/interrupt.c"
10 
12 
14  timer_t ping_timer_id, subtimer_t ping_timer_subtimer) {
15 
17 
18  base = ping_base;
19  pin = ping_pin;
20  sig = blinker(base);
21 
22  sem = semaphore();
24 
25  switch(ping_timer_subtimer) {
26  case TIMER_A:
27  case TIMER_BOTH:
28  timer_interrupt = TIMER_TIMA_TIMEOUT;
29  break;
30  case TIMER_B:
31  timer_interrupt = TIMER_TIMB_TIMEOUT;
32  break;
33  default:
34  while (1) {}
35  }
36 
37  tim = timer(ping_timer_id, ping_timer_subtimer, TIMER_CFG_ONE_SHOT_UP, 0x0fffffe,
39 
41 }
42 
45 void ping::sample() {
46 
47  if (status == ping_not_active) {
48  uint32_t intstatus = StartCritical();
49 
50  /* Disable interrupts in SIG */
52  IntDisable(ctlsys::periph_to_int(base));
53 
54  /* Set Ping))) SIG to output */
55  GPIOPinTypeGPIOOutput(base, pin);
56  sig.turn_on(pin);
57  /* Set SIG high for 5usec */
58  delay::count(1);
59  sig.turn_off(pin);
60 
61  /* Set Ping))) SIG to input */
62  GPIOPinTypeGPIOInput(base, pin);
63  GPIOIntTypeSet(base, pin, GPIO_BOTH_EDGES);
64  delay::count(200);
65 
66  /* Enable interupts on SIG */
68  IntEnable(ctlsys::periph_to_int(base));
69 
70  EndCritical(intstatus);
71  }
72 }
73 
74 void ping::start() {
75 
76  /* start the chain */
77  sem.post();
78 }
79 
80 void ping::stop() {
81 
82  sem.reset();
83 }
84 
85 uint32_t ping::handle_timer() {
86 
87  tim.ack();
88 
89  if (status == ping_not_active) {
90  /* TODO: remove when done debugging */
91  GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1);
92  } else if (status == ping_signal) {
93  /* TODO: remove when done debugging */
94  GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);
95  } else if (status == ping_response) {
96  /* TODO: remove when done debugging */
97  GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, GPIO_PIN_3);
98  } else if (status == ping_sample_delay) {
100  }
101 
102  /* either a timeout happened or the sensor delay period has been
103  achieved */
104  sem.post();
105 }
106 
107 uint32_t ping::handle_gpio() {
108 
109  GPIOIntClear(base, pin);
110 
111  status = ping_status_t(int(status) + 1);
112 
113  if (status == ping_signal) {
114  tim.reload();
116  tim.start();
117  } else if (status == ping_response) {
121  tim.load(SysCtlClockGet()/25);
122  tim.start();
123  } else {
124  /* TODO: do something here? */
125  }
126 
127  return 0xDEADBEEF;
128 }
129 
131 
132  return &sem;
133 }
134 
135 int32_t ping::average() {
136 
137  int32_t i, value;
138  value = 0;
139  for (i=0; i<buf.len; ++i) {
140  value += buf.buf[i];
141  }
142  value /= buf.len;
143  return value;
144 }
145 
146 int32_t ping::distance() {
147 
148  /* The speed of sound is 340 m/s or 29 microseconds per centimeter. */
149  /* The ping travels out and back, so to find the distance of the */
150  /* object we take half of the distance travelled. */
151 
152  /* bus clock is 25/2 ns */
153  /* num bus clocks is in average() */
154  /* time taken for round trip is average()*25/2 in ns */
155 
156  return (average() * 25) / (2492);
157 }
158 
159 
160 /* Local Variables: */
161 /* firestarter: (compile "make -k -j32 -C ~/workspace/ee445m-labs/build/") */
162 /* End: */
semaphore * get_sem(void)
Definition: pingpp.cpp:130
void EndCritical(uint32_t primask)
Definition: criticalpp.hpp:14
void load(uint32_t load_value)
Definition: timerpp.cpp:47
uint32_t memory_address_t
Definition: adcpp.hpp:16
circularbuffer< int32_t, 5 > buf
Definition: pingpp.hpp:66
void reload(void)
Definition: timerpp.cpp:42
uint8_t timer_t
Definition: timerpp.hpp:20
uint32_t timer_interrupt
Definition: pingpp.hpp:34
memory_address_t pin
Definition: pingpp.hpp:36
void post(void)
Definition: semaphorepp.cpp:45
uint32_t StartCritical(void)
Definition: criticalpp.hpp:9
enum ping_status ping_status_t
static void gpio_int_enable(uint32_t base, uint32_t pin, bool clear_int=false)
Definition: ctlsysctl.hpp:99
uint32_t timer_signal_value
Definition: pingpp.hpp:38
T buf[N]
Definition: bufferpp.hpp:115
uint32_t len
Definition: bufferpp.hpp:113
ping()
Definition: pingpp.cpp:11
virtual void stop(void)
Definition: pingpp.cpp:80
int32_t average(void)
Definition: pingpp.cpp:135
static void count(uint32_t cycles)
Definition: delay.hpp:15
static void gpio_int_disable(uint32_t base, uint32_t pin)
Definition: ctlsysctl.hpp:93
void add(const T ch)
void sample(void)
Definition: pingpp.cpp:45
semaphore sem
Definition: pingpp.hpp:30
timer tim
Definition: pingpp.hpp:33
virtual uint32_t ack()
Definition: timerpp.cpp:74
uint32_t timer_response_value
Definition: pingpp.hpp:39
virtual void start()
Definition: timerpp.cpp:62
virtual void turn_off(pin_t pin)
Definition: blinker.cpp:30
void reset(void)
Definition: semaphorepp.cpp:28
uint32_t subtimer_t
Definition: timerpp.hpp:21
int32_t distance(void)
Definition: pingpp.cpp:146
uint32_t handle_timer(void)
Definition: pingpp.cpp:85
virtual void turn_on(pin_t pin)
Definition: blinker.cpp:26
memory_address_t base
Definition: pingpp.hpp:35
static uint32_t periph_to_int(uint32_t periph)
Definition: ctlsysctl.hpp:73
static void enable_periph(uint32_t sys_periph)
Definition: ctlsysctl.hpp:24
virtual void start(void)
Definition: pingpp.cpp:74
virtual uint32_t get()
Definition: timerpp.cpp:90
ping_status_t status
Definition: pingpp.hpp:32
blinker sig
Definition: pingpp.hpp:31
uint32_t handle_gpio(void)
Definition: pingpp.cpp:107