EE445M RTOS
Taken at the University of Texas Spring 2015
uart Class Reference

#include <uartpp.hpp>

Inheritance diagram for uart:
Inheritance graph
Collaboration diagram for uart:
Collaboration graph

Public Member Functions

 uart ()
 
 uart (memory_address_t uart_channel, memory_address_t uart_interrupt, uint32_t uart_baud_rate=115200)
 
virtual void start (void)
 
virtual void stop (void)
 
virtual uint32_t ack (void)
 
void send_char (const char)
 
void send_string (const char *)
 
void send_newline (void)
 
char get_char (void)
 
char * get_string (const uint32_t length)
 
void printf (const char *pcString,...)
 
void atomic_printf (const char *pcString,...)
 
- Public Member Functions inherited from interruptable
 interruptable ()
 
- Public Member Functions inherited from critical
uint32_t StartCritical (void)
 
void EndCritical (uint32_t primask)
 

Public Attributes

char buffer [UART_DEFAULT_MAX_GET_STRING_LENGTH]
 

Static Public Attributes

static bool LAST_WAS_CR
 

Private Member Functions

void vprintf (const char *pcString, va_list vaArgP)
 

Private Attributes

uint32_t baud_rate
 
memory_address_t channel
 
memory_address_t interrupt
 

Additional Inherited Members

- Static Public Member Functions inherited from critical
static uint32_t static_StartCritical (void)
 
static void static_EndCritical (uint32_t primask)
 

Detailed Description

Definition at line 28 of file uartpp.hpp.

Constructor & Destructor Documentation

uart::uart ( )

Definition at line 21 of file uartpp.cpp.

21 {}
uart::uart ( memory_address_t  uart_channel,
memory_address_t  uart_interrupt,
uint32_t  uart_baud_rate = 115200 
)

Definition at line 24 of file uartpp.cpp.

References baud_rate, channel, interrupt, printf(), and start().

25  {
26 
27  baud_rate = uart_baud_rate;
28  channel = uart_channel;
29  interrupt = uart_interrupt;
30 
31  SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0 +
32  (uart_channel - UART0_BASE) / 0x1000);
33 
34  /* todo: parametrize */
35  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
36  GPIOPinConfigure(GPIO_PA0_U0RX);
37  GPIOPinConfigure(GPIO_PA1_U0TX);
38  GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
39 
40  UARTConfigSetExpClk(channel, SysCtlClockGet(), baud_rate,
41  (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
42  UART_CONFIG_PAR_NONE));
43 
44  start();
45 
46  printf("\n\rWelcome to RRTOS v0\n\r");
47 }
virtual void start(void)
Definition: uartpp.cpp:408
memory_address_t interrupt
Definition: uartpp.hpp:32
void printf(const char *pcString,...)
Definition: uartpp.cpp:360
uint32_t baud_rate
Definition: uartpp.hpp:30
memory_address_t channel
Definition: uartpp.hpp:31

Here is the call graph for this function:

Member Function Documentation

uint32_t uart::ack ( void  )
virtual

Acknowledge interrupt.

Implements interruptable.

Definition at line 386 of file uartpp.cpp.

References channel.

Referenced by get_string(), and UART0_Handler().

386  {
387 
388  uint32_t ui32Status;
389  ui32Status = UARTIntStatus(channel, true);
390  UARTIntClear(channel, ui32Status);
391  return ui32Status;
392 }
memory_address_t channel
Definition: uartpp.hpp:31

Here is the caller graph for this function:

void uart::atomic_printf ( const char *  pcString,
  ... 
)

Printf atomically over uart.

Definition at line 348 of file uartpp.cpp.

References critical::EndCritical(), critical::StartCritical(), and vprintf().

Referenced by shell::backspace(), can_handler(), shell::execute_command(), shell::print_ps1(), thread_uart_update(), and shell::type().

348  {
349 
350  va_list vaArgP;
351  va_start(vaArgP, pcString);
352 
353  uint32_t ui32Status = StartCritical();
354  vprintf(pcString, vaArgP);
355  EndCritical(ui32Status);
356 
357  va_end(vaArgP);
358 }
void EndCritical(uint32_t primask)
Definition: criticalpp.hpp:14
uint32_t StartCritical(void)
Definition: criticalpp.hpp:9
void vprintf(const char *pcString, va_list vaArgP)
Definition: uartpp.cpp:122

Here is the call graph for this function:

Here is the caller graph for this function:

char uart::get_char ( void  )

Receive a char.

Definition at line 381 of file uartpp.cpp.

References channel.

Referenced by get_string(), and UART0_Handler().

381  {
382 
383  return UARTCharGet(channel) & 0xFF;
384 }
memory_address_t channel
Definition: uartpp.hpp:31

Here is the caller graph for this function:

char * uart::get_string ( const uint32_t  length)

Receive a string of LENGTH characters.

Definition at line 394 of file uartpp.cpp.

References ack(), buffer, channel, and get_char().

394  {
395 
396  uint32_t remaining_chars = (uint32_t) length;
397 
398  ack();
399 
400  while(UARTCharsAvail(channel) && (remaining_chars > 0)) {
401  buffer[remaining_chars-- - length] = get_char();
402  }
403  buffer[length] = 0;
404 
405  return buffer;
406 }
char get_char(void)
Definition: uartpp.cpp:381
char buffer[UART_DEFAULT_MAX_GET_STRING_LENGTH]
Definition: uartpp.hpp:40
virtual uint32_t ack(void)
Definition: uartpp.cpp:386
memory_address_t channel
Definition: uartpp.hpp:31

Here is the call graph for this function:

void uart::printf ( const char *  pcString,
  ... 
)

Printf over uart.

Definition at line 360 of file uartpp.cpp.

References vprintf().

Referenced by can_handler(), can_transmitter(), ping_handler(), timer_updater(), and uart().

360  {
361 
362  va_list vaArgP;
363  va_start(vaArgP, pcString);
364 
365  vprintf(pcString, vaArgP);
366 
367  va_end(vaArgP);
368 }
void vprintf(const char *pcString, va_list vaArgP)
Definition: uartpp.cpp:122

Here is the call graph for this function:

Here is the caller graph for this function:

void uart::send_char ( const char  ch)

Send a char.

Definition at line 371 of file uartpp.cpp.

References channel.

Referenced by send_string().

371  {
372 
373  UARTCharPut(channel, ch);
374 }
memory_address_t channel
Definition: uartpp.hpp:31

Here is the caller graph for this function:

void uart::send_newline ( void  )

Send a newline and carriage return;

Definition at line 376 of file uartpp.cpp.

References send_string().

376  {
377 
378  send_string("\r\n");
379 }
void send_string(const char *)
Definition: uartpp.cpp:49

Here is the call graph for this function:

void uart::send_string ( const char *  str)

Send a string.

Definition at line 49 of file uartpp.cpp.

References channel, send_char(), and ustrlen().

Referenced by send_newline().

49  {
50 
51  uint32_t len = ustrlen(str);
52  char* ptr = (char*)str;
53 
54  while(len--) {
55  while(!UARTSpaceAvail(channel)) {}
56  send_char(*(ptr++));
57  }
58 }
uint32_t ustrlen(const char *s)
Definition: uartpp.cpp:14
void send_char(const char)
Definition: uartpp.cpp:371
memory_address_t channel
Definition: uartpp.hpp:31

Here is the call graph for this function:

Here is the caller graph for this function:

void uart::start ( void  )
virtual

Enable the uart.

Implements interruptable.

Definition at line 408 of file uartpp.cpp.

References channel, and interrupt.

Referenced by uart().

408  {
409 
410  IntEnable(interrupt);
411  UARTIntEnable(channel, UART_INT_RX | UART_INT_RT);
412 }
memory_address_t interrupt
Definition: uartpp.hpp:32
memory_address_t channel
Definition: uartpp.hpp:31

Here is the caller graph for this function:

void uart::stop ( void  )
virtual

Disable the uart.

Implements interruptable.

Definition at line 414 of file uartpp.cpp.

References channel, and interrupt.

414  {
415 
416  IntDisable(interrupt);
417  UARTIntDisable(channel, UART_INT_RX | UART_INT_RT);
418 }
memory_address_t interrupt
Definition: uartpp.hpp:32
memory_address_t channel
Definition: uartpp.hpp:31
void uart::vprintf ( const char *  pcString,
va_list  vaArgP 
)
private

Definition at line 122 of file uartpp.cpp.

References channel, g_pcHex, and UARTwrite().

Referenced by atomic_printf(), and printf().

122  {
123  uint32_t ui32Idx, ui32Value, ui32Pos, ui32Count, ui32Base, ui32Neg;
124  char *pcStr, pcBuf[16], cFill;
125 
126  // Loop while there are more characters in the string.
127  while(*pcString)
128  {
129  // Find the first non-% character, or the end of the string.
130  for(ui32Idx = 0;
131  (pcString[ui32Idx] != '%') && (pcString[ui32Idx] != '\0');
132  ui32Idx++) {}
133 
134  // Write this portion of the string.
135  UARTwrite(channel, pcString, ui32Idx);
136 
137  // Skip the portion of the string that was written.
138  pcString += ui32Idx;
139 
140  // See if the next character is a %.
141  if(*pcString == '%') {
142 
143  // Skip the %.
144  pcString++;
145 
146  // Set the digit count to zero, and the fill character to space
147  // (in other words, to the defaults).
148  ui32Count = 0;
149  cFill = ' ';
150 
151  // It may be necessary to get back here to process more characters.
152  // Goto's aren't pretty, but effective. I feel extremely dirty for
153  // using not one but two of the beasts.
154 again:
155 
156  // Determine how to handle the next character.
157  switch(*pcString++) {
158 
159  // Handle the digit characters.
160  case '0':
161  case '1':
162  case '2':
163  case '3':
164  case '4':
165  case '5':
166  case '6':
167  case '7':
168  case '8':
169  case '9':
170  // If this is a zero, and it is the first digit, then the
171  // fill character is a zero instead of a space.
172  if((pcString[-1] == '0') && (ui32Count == 0)) {
173  cFill = '0';
174  }
175 
176  // Update the digit count.
177  ui32Count *= 10;
178  ui32Count += pcString[-1] - '0';
179 
180  // Get the next character.
181  goto again;
182 
183  // Handle the %c command.
184  case 'c':
185  // Get the value from the varargs.
186  ui32Value = va_arg(vaArgP, uint32_t);
187 
188  // Print out the character.
189  UARTwrite(channel, (char *)&ui32Value, 1);
190 
191  // This command has been handled.
192  break;
193 
194  // Handle the %d and %i commands.
195  case 'd':
196  case 'i':
197  // Get the value from the varargs.
198  ui32Value = va_arg(vaArgP, uint32_t);
199 
200  // Reset the buffer position.
201  ui32Pos = 0;
202 
203  // If the value is negative, make it positive and indicate
204  // that a minus sign is needed.
205  if((int32_t)ui32Value < 0) {
206 
207  // Make the value positive.
208  ui32Value = -(int32_t)ui32Value;
209  // Indicate that the value is negative.
210  ui32Neg = 1;
211  } else {
212  // Indicate that the value is positive so that a minus
213  // sign isn't inserted.
214  ui32Neg = 0;
215  }
216 
217  // Set the base to 10.
218  ui32Base = 10;
219 
220  // Convert the value to ASCII.
221  goto convert;
222 
223  // Handle the %s command.
224  case 's':
225  // Get the string pointer from the varargs.
226  pcStr = va_arg(vaArgP, char *);
227 
228  // Determine the length of the string.
229  for(ui32Idx = 0; pcStr[ui32Idx] != '\0'; ui32Idx++) {}
230 
231  // Write the string.
232  UARTwrite(channel, pcStr, ui32Idx);
233 
234  // Write any required padding spaces
235  if(ui32Count > ui32Idx) {
236  ui32Count -= ui32Idx;
237  while(ui32Count--) {
238  UARTwrite(channel, " ", 1);
239  }
240  }
241  break;
242 
243  // Handle the %u command.
244  case 'u':
245  // Get the value from the varargs.
246  ui32Value = va_arg(vaArgP, uint32_t);
247 
248  // Reset the buffer position.
249  ui32Pos = 0;
250 
251  // Set the base to 10.
252  ui32Base = 10;
253 
254  // Indicate that the value is positive so that a minus sign
255  // isn't inserted.
256  ui32Neg = 0;
257 
258  // Convert the value to ASCII.
259  goto convert;
260 
261  // Handle the %x and %X commands. Note that they are treated
262  // identically; in other words, %X will use lower case letters
263  // for a-f instead of the upper case letters it should use. We
264  // also alias %p to %x.
265  case 'x':
266  case 'X':
267  case 'p':
268  // Get the value from the varargs.
269  ui32Value = va_arg(vaArgP, uint32_t);
270 
271  // Reset the buffer position.
272  ui32Pos = 0;
273 
274  // Set the base to 16.
275  ui32Base = 16;
276 
277  // Indicate that the value is positive so that a minus sign
278  // isn't inserted.
279  ui32Neg = 0;
280 
281  // Determine the number of digits in the string version of
282  // the value.
283 convert:
284  for(ui32Idx = 1;
285  (((ui32Idx * ui32Base) <= ui32Value) &&
286  (((ui32Idx * ui32Base) / ui32Base) == ui32Idx));
287  ui32Idx *= ui32Base, ui32Count--) {}
288 
289  // If the value is negative, reduce the count of padding
290  // characters needed.
291  if(ui32Neg) {
292  ui32Count--;
293  }
294 
295  // If the value is negative and the value is padded with
296  // zeros, then place the minus sign before the padding.
297  if(ui32Neg && (cFill == '0')) {
298  // Place the minus sign in the output buffer.
299  pcBuf[ui32Pos++] = '-';
300 
301  // The minus sign has been placed, so turn off the
302  // negative flag.
303  ui32Neg = 0;
304  }
305 
306  // Provide additional padding at the beginning of the
307  // string conversion if needed.
308  if((ui32Count > 1) && (ui32Count < 16)) {
309  for(ui32Count--; ui32Count; ui32Count--) {
310  pcBuf[ui32Pos++] = cFill;
311  }
312  }
313 
314  // If the value is negative, then place the minus sign
315  // before the number.
316  if(ui32Neg) {
317  // Place the minus sign in the output buffer.
318  pcBuf[ui32Pos++] = '-';
319  }
320 
321  // Convert the value into a string.
322  for(; ui32Idx; ui32Idx /= ui32Base) {
323  pcBuf[ui32Pos++] =
324  g_pcHex[(ui32Value / ui32Idx) % ui32Base];
325  }
326 
327  // Write the string.
328  UARTwrite(channel, pcBuf, ui32Pos);
329 
330  break;
331 
332  // Handle the %% command.
333  case '%':
334  // Simply write a single %.
335  UARTwrite(channel, pcString - 1, 1);
336  break;
337 
338  // Handle all other commands.
339  default:
340  // Indicate an error.
341  UARTwrite(channel, "ERROR", 5);
342  break;
343  }
344  }
345  }
346 }
int UARTwrite(memory_address_t channel, const char *pcBuf, uint32_t ui32Len)
Definition: uartpp.cpp:64
memory_address_t channel
Definition: uartpp.hpp:31
static const char *const g_pcHex
Definition: uartpp.cpp:61

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

uint32_t uart::baud_rate
private

Definition at line 30 of file uartpp.hpp.

Referenced by uart().

Definition at line 40 of file uartpp.hpp.

Referenced by get_string().

memory_address_t uart::channel
private

Definition at line 31 of file uartpp.hpp.

Referenced by ack(), get_char(), get_string(), send_char(), send_string(), start(), stop(), uart(), and vprintf().

memory_address_t uart::interrupt
private

Definition at line 32 of file uartpp.hpp.

Referenced by start(), stop(), and uart().

bool uart::LAST_WAS_CR
static

Flag for proper handling of newlines input from terminal.

Definition at line 38 of file uartpp.hpp.

Referenced by UART0_Handler().


The documentation for this class was generated from the following files: