Embedded System Design 2 - Project
dbprint.h File Reference

Homebrew println/printf replacement "DeBugPrint". More...

#include <stdint.h>
#include <stdbool.h>
#include "em_usart.h"

Go to the source code of this file.

Macros

#define DBPRINT_BUFFER_SIZE   80
 

Typedefs

typedef enum dbprint_colors dbprint_color_t
 

Enumerations

enum  dbprint_colors {
  RED, GREEN, BLUE, CYAN,
  MAGENTA, YELLOW, DEFAULT_COLOR
}
 

Functions

void dbprint_INIT (USART_TypeDef *pointer, uint8_t location, bool vcom, bool interrupts)
 Initialize USARTx. More...
 
void dbAlert (void)
 Sound an alert in the terminal. More...
 
void dbClear (void)
 Clear the terminal. More...
 
void dbprint (char *message)
 Print a string (char array) to USARTx. More...
 
void dbprintln (char *message)
 Print a string (char array) to USARTx and go to the next line. More...
 
void dbprintInt (int32_t value)
 Print a number in decimal notation to USARTx. More...
 
void dbprintlnInt (int32_t value)
 Print a number in decimal notation to USARTx and go to the next line. More...
 
void dbprintInt_hex (int32_t value)
 Print a number in hexadecimal notation to USARTx. More...
 
void dbprintlnInt_hex (int32_t value)
 Print a number in hexadecimal notation to USARTx and go to the next line. More...
 
void dbprint_color (char *message, dbprint_color_t color)
 Print a string (char array) to USARTx in a given color. More...
 
void dbprintln_color (char *message, dbprint_color_t color)
 Print a string (char array) to USARTx in a given color and go to the next line. More...
 
void dbinfo (char *message)
 Print an info string (char array) to USARTx and go to the next line. More...
 
void dbwarn (char *message)
 Print a warning string (char array) in yellow to USARTx and go to the next line. More...
 
void dbcrit (char *message)
 Print a critical error string (char array) in red to USARTx and go to the next line. More...
 
void dbinfoInt (char *message1, int32_t value, char *message2)
 Print an info value surrounded by two strings (char array) to USARTx. More...
 
void dbwarnInt (char *message1, int32_t value, char *message2)
 Print a warning value surrounded by two strings (char array) to USARTx. More...
 
void dbcritInt (char *message1, int32_t value, char *message2)
 Print a critical value surrounded by two strings (char array) to USARTx. More...
 
void dbinfoInt_hex (char *message1, int32_t value, char *message2)
 Print an info value surrounded by two strings (char array) to USARTx. More...
 
void dbwarnInt_hex (char *message1, int32_t value, char *message2)
 Print a warning value surrounded by two strings (char array) to USARTx. More...
 
void dbcritInt_hex (char *message1, int32_t value, char *message2)
 Print a critical value surrounded by two strings (char array) to USARTx. More...
 
char dbReadChar (void)
 Read a character from USARTx. More...
 
uint8_t dbReadInt (void)
 Read a decimal character from USARTx and convert it to a uint8_t value. More...
 
void dbReadLine (char *buf)
 Read a string (char array) from USARTx. More...
 
bool dbGet_RXstatus (void)
 Check if data was received using interrupts in the RX buffer. More...
 
void dbGet_RXbuffer (char *buf)
 Get the value of the RX buffer and clear the dataReceived flag. More...
 

Detailed Description

Homebrew println/printf replacement "DeBugPrint".

Version
6.2
Author
Brecht Van Eeckhoudt

License

Copyright (C) 2019 - Brecht Van Eeckhoudt

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

A copy of the GNU General Public License can be found in the LICENSE file along with this source code.


Some methods also use code obtained from examples from Silicon Labs' GitHub. These sections are licensed under the Silabs License Agreement. See the file "Silabs_License_Agreement.txt" for details. Before using this software for any purpose, you must agree to the terms of that agreement.

Definition in file dbprint.h.

Macro Definition Documentation

◆ DBPRINT_BUFFER_SIZE

#define DBPRINT_BUFFER_SIZE   80

Public definition to configure the buffer size.

Definition at line 48 of file dbprint.h.

Typedef Documentation

◆ dbprint_color_t

Enum type for the color selection.

Enumeration Type Documentation

◆ dbprint_colors

Enum type for the color selection.

Enumerator
RED 
GREEN 
BLUE 
CYAN 
MAGENTA 
YELLOW 
DEFAULT_COLOR 

Definition at line 52 of file dbprint.h.

53 {
54  RED,
55  GREEN,
56  BLUE,
57  CYAN,
58  MAGENTA,
59  YELLOW,
Definition: dbprint.h:57
Definition: dbprint.h:54
Definition: dbprint.h:55
Definition: dbprint.h:59
Definition: dbprint.h:56
enum dbprint_colors dbprint_color_t

Function Documentation

◆ dbAlert()

void dbAlert ( void  )

Sound an alert in the terminal.

Print the bell (alert) character to USARTx.

Definition at line 342 of file dbprint.c.

343 {
344  USART_Tx(dbpointer, '\a');
345 }
USART_TypeDef * dbpointer
Definition: dbprint.c:114

◆ dbClear()

void dbClear ( void  )

Clear the terminal.

Print the form feed character to USARTx. Accessing old data is still possible by scrolling up in the serial port program.

Definition at line 356 of file dbprint.c.

357 {
358  USART_Tx(dbpointer, '\f');
359 }
USART_TypeDef * dbpointer
Definition: dbprint.c:114

◆ dbcrit()

void dbcrit ( char *  message)

Print a critical error string (char array) in red to USARTx and go to the next line.

Note
If the input is not a string (ex.: "Hello world!") but a char array, the input message (array) needs to end with NULL ("\0")!
Parameters
[in]messageThe string to print to USARTx.

Definition at line 539 of file dbprint.c.

540 {
541  dbprint_color("CRIT: ", RED);
542  dbprintln_color(message, RED);
543 }
Definition: dbprint.h:54
void dbprintln_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color and go to the next line.
Definition: dbprint.c:480
void dbprint_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color.
Definition: dbprint.c:421

◆ dbcritInt()

void dbcritInt ( char *  message1,
int32_t  value,
char *  message2 
)

Print a critical value surrounded by two strings (char array) to USARTx.

"CRIT: " gets added in front, the decimal notation is used and the function advances to the next line. The value is in the color white, the rest is red.

Note
If the input is not a string (ex.: "Hello world!") but a char array, the input message (array) needs to end with NULL ("\0")!
Parameters
[in]message1The first part of the string to print to USARTx.
[in]valueThe value to print between the two string parts.
[in]message2The second part of the string to print to USARTx.

Definition at line 629 of file dbprint.c.

630 {
631  dbprint_color("CRIT: ", RED);
632  dbprint_color(message1, RED);
633  dbprintInt(value);
634  dbprintln_color(message2, RED);
635 }
Definition: dbprint.h:54
void dbprintInt(int32_t value)
Print a number in decimal notation to USARTx.
Definition: dbprint.c:738
void dbprintln_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color and go to the next line.
Definition: dbprint.c:480
void dbprint_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color.
Definition: dbprint.c:421

◆ dbcritInt_hex()

void dbcritInt_hex ( char *  message1,
int32_t  value,
char *  message2 
)

Print a critical value surrounded by two strings (char array) to USARTx.

"CRIT: " gets added in front, the hexadecimal notation is used and the function advances to the next line. The value is in the color white, the rest is red.

Note
If the input is not a string (ex.: "Hello world!") but a char array, the input message (array) needs to end with NULL ("\0")!
Parameters
[in]message1The first part of the string to print to USARTx.
[in]valueThe value to print between the two string parts.
[in]message2The second part of the string to print to USARTx.

Definition at line 721 of file dbprint.c.

722 {
723  dbprint_color("CRIT: ", RED);
724  dbprint_color(message1, RED);
725  dbprintInt_hex(value);
726  dbprintln_color(message2, RED);
727 }
Definition: dbprint.h:54
void dbprintInt_hex(int32_t value)
Print a number in hexadecimal notation to USARTx.
Definition: dbprint.c:794
void dbprintln_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color and go to the next line.
Definition: dbprint.c:480
void dbprint_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color.
Definition: dbprint.c:421

◆ dbGet_RXbuffer()

void dbGet_RXbuffer ( char *  buf)

Get the value of the RX buffer and clear the dataReceived flag.

Note
The index of the RX buffer gets reset in the RX handler when a CR character is received or the buffer is filled.
Attention
Interrupt functionality has to be enabled on initialization for this function to work correctly.
Parameters
[in]bufThe buffer to put the resulting string in.
This needs to have a length of DBPRINT_BUFFER_SIZE for the function to work properly: char buf[DBPRINT_BUFFER_SIZE];!

Definition at line 1017 of file dbprint.c.

1018 {
1019  if (dataReceived)
1020  {
1021  uint32_t i;
1022 
1023  /* Copy data from the RX buffer to the given buffer */
1024  for (i = 0; rx_buffer[i] != 0 && i < DBPRINT_BUFFER_SIZE-1; i++)
1025  {
1026  buf[i] = rx_buffer[i];
1027  }
1028 
1029  /* Add NULL termination character */
1030  buf[i++] = '\0';
1031 
1032  /* Reset "notification" variable */
1033  dataReceived = false;
1034  }
1035  else
1036  {
1037  dbcrit("No received data available!");
1038  }
1039 }
volatile bool dataReceived
Definition: dbprint.c:118
volatile char rx_buffer[80]
Definition: dbprint.c:119
#define DBPRINT_BUFFER_SIZE
Definition: dbprint.h:48
void dbcrit(char *message)
Print a critical error string (char array) in red to USARTx and go to the next line.
Definition: dbprint.c:539

◆ dbGet_RXstatus()

bool dbGet_RXstatus ( void  )

Check if data was received using interrupts in the RX buffer.

Note
The index of the RX buffer gets reset in the RX handler when a CR character is received or the buffer is filled.
Attention
Interrupt functionality has to be enabled on initialization for this function to work correctly.
Returns
  • true - Data is received in the RX buffer.
  • false - No data is received.

Definition at line 909 of file dbprint.c.

910 {
911  return (dataReceived);
912 }
volatile bool dataReceived
Definition: dbprint.c:118

◆ dbinfo()

void dbinfo ( char *  message)

Print an info string (char array) to USARTx and go to the next line.

Note
If the input is not a string (ex.: "Hello world!") but a char array, the input message (array) needs to end with NULL ("\0")!
Parameters
[in]messageThe string to print to USARTx.

Definition at line 503 of file dbprint.c.

504 {
505  dbprint("INFO: ");
506  dbprintln(message);
507 }
void dbprint(char *message)
Print a string (char array) to USARTx.
Definition: dbprint.c:373
void dbprintln(char *message)
Print a string (char array) to USARTx and go to the next line.
Definition: dbprint.c:395

◆ dbinfoInt()

void dbinfoInt ( char *  message1,
int32_t  value,
char *  message2 
)

Print an info value surrounded by two strings (char array) to USARTx.

"INFO: " gets added in front, the decimal notation is used and the function advances to the next line.

Note
If the input is not a string (ex.: "Hello world!") but a char array, the input message (array) needs to end with NULL ("\0")!
Parameters
[in]message1The first part of the string to print to USARTx.
[in]valueThe value to print between the two string parts.
[in]message2The second part of the string to print to USARTx.

Definition at line 567 of file dbprint.c.

568 {
569  dbprint("INFO: ");
570  dbprint(message1);
571  dbprintInt(value);
572  dbprintln(message2);
573 }
void dbprint(char *message)
Print a string (char array) to USARTx.
Definition: dbprint.c:373
void dbprintln(char *message)
Print a string (char array) to USARTx and go to the next line.
Definition: dbprint.c:395
void dbprintInt(int32_t value)
Print a number in decimal notation to USARTx.
Definition: dbprint.c:738

◆ dbinfoInt_hex()

void dbinfoInt_hex ( char *  message1,
int32_t  value,
char *  message2 
)

Print an info value surrounded by two strings (char array) to USARTx.

"INFO: " gets added in front, the hexadecimal notation is used and the function advances to the next line.

Note
If the input is not a string (ex.: "Hello world!") but a char array, the input message (array) needs to end with NULL ("\0")!
Parameters
[in]message1The first part of the string to print to USARTx.
[in]valueThe value to print between the two string parts.
[in]message2The second part of the string to print to USARTx.

Definition at line 659 of file dbprint.c.

660 {
661  dbprint("INFO: ");
662  dbprint(message1);
663  dbprintInt_hex(value);
664  dbprintln(message2);
665 }
void dbprint(char *message)
Print a string (char array) to USARTx.
Definition: dbprint.c:373
void dbprintln(char *message)
Print a string (char array) to USARTx and go to the next line.
Definition: dbprint.c:395
void dbprintInt_hex(int32_t value)
Print a number in hexadecimal notation to USARTx.
Definition: dbprint.c:794

◆ dbprint()

void dbprint ( char *  message)

Print a string (char array) to USARTx.

Note
If the input is not a string (ex.: "Hello world!") but a char array, the input message (array) needs to end with NULL ("\0")!
Parameters
[in]messageThe string to print to USARTx.

Definition at line 373 of file dbprint.c.

374 {
375  /* "message[i] != 0" makes "uint32_t length = strlen(message)"
376  * not necessary (given string MUST be terminated by NULL for this to work) */
377  for (uint32_t i = 0; message[i] != 0; i++)
378  {
379  USART_Tx(dbpointer, message[i]);
380  }
381 }
USART_TypeDef * dbpointer
Definition: dbprint.c:114

◆ dbprint_color()

void dbprint_color ( char *  message,
dbprint_color_t  color 
)

Print a string (char array) to USARTx in a given color.

Note
If the input is not a string (ex.: "Hello world!") but a char array, the input message (array) needs to end with NULL ("\0")!
Parameters
[in]messageThe string to print to USARTx.
[in]colorThe color to print the text in.

Definition at line 421 of file dbprint.c.

422 {
423  switch (color)
424  {
425  case DEFAULT_COLOR:
427  dbprint(message);
428  break;
429  case RED:
431  dbprint(message);
433  break;
434  case GREEN:
436  dbprint(message);
438  break;
439  case BLUE:
441  dbprint(message);
443  break;
444  case CYAN:
446  dbprint(message);
448  break;
449  case MAGENTA:
451  dbprint(message);
453  break;
454  case YELLOW:
456  dbprint(message);
458  break;
459  default:
461  dbprint(message);
462  }
463 }
Definition: dbprint.h:57
#define COLOR_GREEN
Definition: dbprint.c:105
void dbprint(char *message)
Print a string (char array) to USARTx.
Definition: dbprint.c:373
#define COLOR_YELLOW
Definition: dbprint.c:109
Definition: dbprint.h:54
#define COLOR_MAGENTA
Definition: dbprint.c:108
#define COLOR_RED
Definition: dbprint.c:104
Definition: dbprint.h:55
Definition: dbprint.h:59
Definition: dbprint.h:56
#define COLOR_BLUE
Definition: dbprint.c:106
#define COLOR_CYAN
Definition: dbprint.c:107
#define COLOR_RESET
Definition: dbprint.c:110

◆ dbprint_INIT()

void dbprint_INIT ( USART_TypeDef *  pointer,
uint8_t  location,
bool  vcom,
bool  interrupts 
)

Initialize USARTx.

Parameters
[in]pointerPointer to USARTx.
[in]locationLocation for pin routing.
[in]vcom
  • true - Isolation switch enabled by setting PA9 high so the Virtual COM port (CDC) can be used.
  • false - Isolation switch disabled on the Happy Gecko board.
[in]interrupts
  • true - Enable interrupt functionality.
  • false - No interrupt functionality is initialized.

Definition at line 148 of file dbprint.c.

149 {
150  /* Store the pointer in the global variable */
151  dbpointer = pointer;
152 
153  /*
154  * USART_INITASYNC_DEFAULT:
155  * config.enable = usartEnable // Specifies whether TX and/or RX is enabled when initialization is completed
156  * // (Enable RX/TX when initialization is complete).
157  * config.refFreq = 0 // USART/UART reference clock assumed when configuring baud rate setup
158  * // (0 = Use current configured reference clock for configuring baud rate).
159  * config.baudrate = 115200 // Desired baudrate (115200 bits/s).
160  * config.oversampling = usartOVS16 // Oversampling used (16x oversampling).
161  * config.databits = usartDatabits8 // Number of data bits in frame (8 data bits).
162  * config.parity = usartNoParity // Parity mode to use (no parity).
163  * config.stopbits = usartStopbits1 // Number of stop bits to use (1 stop bit).
164  * config.mvdis = false // Majority Vote Disable for 16x, 8x and 6x oversampling modes (Do not disable majority vote).
165  * config.prsRxEnable = false // Enable USART Rx via PRS (Not USART PRS input mode).
166  * config.prsRxCh = 0 // Select PRS channel for USART Rx. (Only valid if prsRxEnable is true - PRS channel 0).
167  * config.autoCsEnable = false // Auto CS enabling (Auto CS functionality enable/disable switch - disabled).
168  */
169 
170  USART_InitAsync_TypeDef config = USART_INITASYNC_DEFAULT;
171 
172  /* Enable oscillator to GPIO*/
173  CMU_ClockEnable(cmuClock_GPIO, true);
174 
175 
176  /* Enable oscillator to USARTx modules */
177  if (dbpointer == USART0)
178  {
179  CMU_ClockEnable(cmuClock_USART0, true);
180  }
181  else if (dbpointer == USART1)
182  {
183  CMU_ClockEnable(cmuClock_USART1, true);
184  }
185 
186 
187  /* Set PA9 (EFM_BC_EN) high if necessary to enable the isolation switch */
188  if (vcom)
189  {
190  GPIO_PinModeSet(gpioPortA, 9, gpioModePushPull, 1);
191  GPIO_PinOutSet(gpioPortA, 9);
192  }
193 
194 
195  /* Set pin modes for UART TX and RX pins */
196  if (dbpointer == USART0)
197  {
198  switch (location)
199  {
200  case 0:
201  GPIO_PinModeSet(gpioPortE, 11, gpioModeInput, 0); /* RX */
202  GPIO_PinModeSet(gpioPortE, 10, gpioModePushPull, 1); /* TX */
203  break;
204  case 2:
205  GPIO_PinModeSet(gpioPortC, 10, gpioModeInput, 0); /* RX */
206  /* No TX pin in this mode */
207  break;
208  case 3:
209  GPIO_PinModeSet(gpioPortE, 12, gpioModeInput, 0); /* RX */
210  GPIO_PinModeSet(gpioPortE, 13, gpioModePushPull, 1); /* TX */
211  break;
212  case 4:
213  GPIO_PinModeSet(gpioPortB, 8, gpioModeInput, 0); /* RX */
214  GPIO_PinModeSet(gpioPortB, 7, gpioModePushPull, 1); /* TX */
215  break;
216  case 5:
217  case 6:
218  GPIO_PinModeSet(gpioPortC, 1, gpioModeInput, 0); /* RX */
219  GPIO_PinModeSet(gpioPortC, 0, gpioModePushPull, 1); /* TX */
220  break;
221  /* default: */
222  /* No default */
223  }
224  }
225  else if (dbpointer == USART1)
226  {
227  switch (location)
228  {
229  case 0:
230  GPIO_PinModeSet(gpioPortC, 1, gpioModeInput, 0); /* RX */
231  GPIO_PinModeSet(gpioPortC, 0, gpioModePushPull, 1); /* TX */
232  break;
233  case 2:
234  case 3:
235  GPIO_PinModeSet(gpioPortD, 6, gpioModeInput, 0); /* RX */
236  GPIO_PinModeSet(gpioPortD, 7, gpioModePushPull, 1); /* TX */
237  break;
238  case 4:
239  GPIO_PinModeSet(gpioPortA, 0, gpioModeInput, 0); /* RX */
240  GPIO_PinModeSet(gpioPortF, 2, gpioModePushPull, 1); /* TX */
241  break;
242  case 5:
243  GPIO_PinModeSet(gpioPortC, 2, gpioModeInput, 0); /* RX */
244  GPIO_PinModeSet(gpioPortC, 1, gpioModePushPull, 1); /* TX */
245  break;
246  /* default: */
247  /* No default */
248  }
249  }
250 
251 
252  /* Initialize USART asynchronous mode */
253  USART_InitAsync(dbpointer, &config);
254 
255  /* Route pins */
256  switch (location)
257  {
258  case 0:
259  dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC0;
260  break;
261  case 1:
262  dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC1;
263  break;
264  case 2:
265  dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC2;
266  break;
267  case 3:
268  dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC3;
269  break;
270  case 4:
271  dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC4;
272  break;
273  case 5:
274  dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC5;
275  break;
276  case 6:
277  dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC6;
278  break;
279  default:
280  dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_DEFAULT;
281  }
282 
283  /* Enable interrupts if necessary and print welcome string (and make an alert sound in the console) */
284  if (interrupts)
285  {
286  /* Initialize USART interrupts */
287 
288  /* RX Data Valid Interrupt Enable
289  * Set when data is available in the receive buffer. Cleared when the receive buffer is empty. */
290  USART_IntEnable(dbpointer, USART_IEN_RXDATAV);
291 
292  /* TX Complete Interrupt Enable
293  * Set when a transmission has completed and no more data is available in the transmit buffer.
294  * Cleared when a new transmission starts. */
295  USART_IntEnable(dbpointer, USART_IEN_TXC);
296 
297  if (dbpointer == USART0)
298  {
299  /* Enable USART interrupts */
300  NVIC_EnableIRQ(USART0_RX_IRQn);
301  NVIC_EnableIRQ(USART0_TX_IRQn);
302  }
303  else if (dbpointer == USART1)
304  {
305  /* Enable USART interrupts */
306  NVIC_EnableIRQ(USART1_RX_IRQn);
307  NVIC_EnableIRQ(USART1_TX_IRQn);
308  }
309 
310  /* Print welcome string */
312  dbprintln("\a\r\f### UART initialized (interrupt mode) ###");
313  dbinfo("This is an info message.");
314  dbwarn("This is a warning message.");
315  dbcrit("This is a critical error message.");
316  dbprintln("### Start executing programmed code ###\n");
317 
318  /* Set TX Complete Interrupt Flag (transmission has completed and no more data
319  * is available in the transmit buffer) */
320  USART_IntSet(dbpointer, USART_IFS_TXC);
321  }
322  /* Print welcome string (and make an alert sound in the console) if not in interrupt mode */
323  else
324  {
326  dbprintln("\a\r\f### UART initialized (no interrupts) ###");
327  dbinfo("This is an info message.");
328  dbwarn("This is a warning message.");
329  dbcrit("This is a critical error message.");
330  dbprintln("### Start executing programmed code ###\n");
331  }
332 }
void dbprint(char *message)
Print a string (char array) to USARTx.
Definition: dbprint.c:373
void dbprintln(char *message)
Print a string (char array) to USARTx and go to the next line.
Definition: dbprint.c:395
void dbwarn(char *message)
Print a warning string (char array) in yellow to USARTx and go to the next line.
Definition: dbprint.c:521
void dbinfo(char *message)
Print an info string (char array) to USARTx and go to the next line.
Definition: dbprint.c:503
USART_TypeDef * dbpointer
Definition: dbprint.c:114
#define COLOR_RESET
Definition: dbprint.c:110
void dbcrit(char *message)
Print a critical error string (char array) in red to USARTx and go to the next line.
Definition: dbprint.c:539

◆ dbprintInt()

void dbprintInt ( int32_t  value)

Print a number in decimal notation to USARTx.

Parameters
[in]valueThe number to print to USARTx.
This can be of type uint32_t or int32_t.

Definition at line 738 of file dbprint.c.

739 {
740  /* Buffer to put the char array in (Needs to be 10) */
741  char decchar[10];
742 
743  /* Convert a negative number to a positive one and print the "-" */
744  if (value < 0)
745  {
746  /* Negative of value = flip all bits, +1
747  * bitwise logic: "~" = "NOT" */
748  uint32_t negativeValue = (~value) + 1;
749 
750  dbprint("-");
751 
752  /* Convert the value */
753  uint32_to_charDec(decchar, negativeValue);
754  }
755  else
756  {
757  /* Convert the value */
758  uint32_to_charDec(decchar, value);
759  }
760 
761  /* Print the buffer */
762  dbprint(decchar);
763 }
void dbprint(char *message)
Print a string (char array) to USARTx.
Definition: dbprint.c:373
static void uint32_to_charDec(char *buf, uint32_t value)
Convert a uint32_t value to a decimal char array (string).
Definition: dbprint.c:1122

◆ dbprintInt_hex()

void dbprintInt_hex ( int32_t  value)

Print a number in hexadecimal notation to USARTx.

Parameters
[in]valueThe number to print to USARTx.
This can be of type uint32_t or int32_t.

Definition at line 794 of file dbprint.c.

795 {
796  char hexchar[9]; /* Needs to be 9 */
797  uint32_to_charHex(hexchar, value, true); /* true: add spacing between eight HEX chars */
798  dbprint("0x");
799  dbprint(hexchar);
800 }
void dbprint(char *message)
Print a string (char array) to USARTx.
Definition: dbprint.c:373
static void uint32_to_charHex(char *buf, uint32_t value, bool spacing)
Convert a uint32_t value to a hexadecimal char array (string).
Definition: dbprint.c:1061

◆ dbprintln()

void dbprintln ( char *  message)

Print a string (char array) to USARTx and go to the next line.

Note
If the input is not a string (ex.: "Hello world!") but a char array, the input message (array) needs to end with NULL ("\0")!
Parameters
[in]messageThe string to print to USARTx.

Definition at line 395 of file dbprint.c.

396 {
397  dbprint(message);
398 
399  /* Carriage return */
400  USART_Tx(dbpointer, '\r');
401 
402  /* Line feed (new line) */
403  USART_Tx(dbpointer, '\n');
404 }
void dbprint(char *message)
Print a string (char array) to USARTx.
Definition: dbprint.c:373
USART_TypeDef * dbpointer
Definition: dbprint.c:114

◆ dbprintln_color()

void dbprintln_color ( char *  message,
dbprint_color_t  color 
)

Print a string (char array) to USARTx in a given color and go to the next line.

Note
If the input is not a string (ex.: "Hello world!") but a char array, the input message (array) needs to end with NULL ("\0")!
Parameters
[in]messageThe string to print to USARTx.
[in]colorThe color to print the text in.

Definition at line 480 of file dbprint.c.

481 {
482  dbprint_color(message, color);
483 
484  /* Carriage return */
485  USART_Tx(dbpointer, '\r');
486 
487  /* Line feed (new line) */
488  USART_Tx(dbpointer, '\n');
489 }
USART_TypeDef * dbpointer
Definition: dbprint.c:114
void dbprint_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color.
Definition: dbprint.c:421

◆ dbprintlnInt()

void dbprintlnInt ( int32_t  value)

Print a number in decimal notation to USARTx and go to the next line.

Parameters
[in]valueThe number to print to USARTx.
This can be of type uint32_t or int32_t.

Definition at line 774 of file dbprint.c.

775 {
776  dbprintInt(value);
777 
778  /* Carriage return */
779  USART_Tx(dbpointer, '\r');
780 
781  /* Line feed (new line) */
782  USART_Tx(dbpointer, '\n');
783 }
USART_TypeDef * dbpointer
Definition: dbprint.c:114
void dbprintInt(int32_t value)
Print a number in decimal notation to USARTx.
Definition: dbprint.c:738

◆ dbprintlnInt_hex()

void dbprintlnInt_hex ( int32_t  value)

Print a number in hexadecimal notation to USARTx and go to the next line.

Parameters
[in]valueThe number to print to USARTx.
This can be of type uint32_t or int32_t.

Definition at line 811 of file dbprint.c.

812 {
813  dbprintInt_hex(value);
814 
815  /* Carriage return */
816  USART_Tx(dbpointer, '\r');
817 
818  /* Line feed (new line) */
819  USART_Tx(dbpointer, '\n');
820 }
USART_TypeDef * dbpointer
Definition: dbprint.c:114
void dbprintInt_hex(int32_t value)
Print a number in hexadecimal notation to USARTx.
Definition: dbprint.c:794

◆ dbReadChar()

char dbReadChar ( void  )

Read a character from USARTx.

Returns
The character read from USARTx.

Definition at line 830 of file dbprint.c.

831 {
832  return (USART_Rx(dbpointer));
833 }
USART_TypeDef * dbpointer
Definition: dbprint.c:114

◆ dbReadInt()

uint8_t dbReadInt ( void  )

Read a decimal character from USARTx and convert it to a uint8_t value.

Note
Specific methods exist to read uint16_t and uint32_t values:
  • uint16_t USART_RxDouble(USART_TypeDef *usart);
  • uint32_t USART_RxDoubleExt(USART_TypeDef *usart);
Returns
The converted uint8_t value.

Definition at line 848 of file dbprint.c.

849 {
850  /* Method expects a char array ending with a null termination character */
851  char value[2];
852  value[0]= dbReadChar();
853  value[1] = '\0';
854 
855  return (charDec_to_uint32(value));
856 }
static uint32_t charDec_to_uint32(char *buf)
Convert a string (char array) in decimal notation to a uint32_t value.
Definition: dbprint.c:1184
char dbReadChar(void)
Read a character from USARTx.
Definition: dbprint.c:830

◆ dbReadLine()

void dbReadLine ( char *  buf)

Read a string (char array) from USARTx.

Note
The reading stops when a "CR" (Carriage Return, ENTER) character is received or the maximum length (DBPRINT_BUFFER_SIZE) is reached.
Parameters
[in]bufThe buffer to put the resulting string in.
This needs to have a length of DBPRINT_BUFFER_SIZE for the function to work properly: char buf[DBPRINT_BUFFER_SIZE];!

Definition at line 872 of file dbprint.c.

873 {
874  for (uint32_t i = 0; i < DBPRINT_BUFFER_SIZE - 1 ; i++ )
875  {
876  char localBuffer = USART_Rx(dbpointer);
877 
878  /* Check if a CR character is received */
879  if (localBuffer == '\r')
880  {
881  /* End with a null termination character, expected by the dbprintln method */
882  buf[i] = '\0';
883  break;
884  }
885  else
886  {
887  buf[i] = localBuffer;
888  }
889  }
890 }
USART_TypeDef * dbpointer
Definition: dbprint.c:114
#define DBPRINT_BUFFER_SIZE
Definition: dbprint.h:48

◆ dbwarn()

void dbwarn ( char *  message)

Print a warning string (char array) in yellow to USARTx and go to the next line.

Note
If the input is not a string (ex.: "Hello world!") but a char array, the input message (array) needs to end with NULL ("\0")!
Parameters
[in]messageThe string to print to USARTx.

Definition at line 521 of file dbprint.c.

522 {
523  dbprint_color("WARN: ", YELLOW);
524  dbprintln_color(message, YELLOW);
525 }
Definition: dbprint.h:59
void dbprintln_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color and go to the next line.
Definition: dbprint.c:480
void dbprint_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color.
Definition: dbprint.c:421

◆ dbwarnInt()

void dbwarnInt ( char *  message1,
int32_t  value,
char *  message2 
)

Print a warning value surrounded by two strings (char array) to USARTx.

"WARN: " gets added in front, the decimal notation is used and the function advances to the next line. The value is in the color white, the rest is yellow.

Note
If the input is not a string (ex.: "Hello world!") but a char array, the input message (array) needs to end with NULL ("\0")!
Parameters
[in]message1The first part of the string to print to USARTx.
[in]valueThe value to print between the two string parts.
[in]message2The second part of the string to print to USARTx.

Definition at line 598 of file dbprint.c.

599 {
600  dbprint_color("WARN: ", YELLOW);
601  dbprint_color(message1, YELLOW);
602  dbprintInt(value);
603  dbprintln_color(message2, YELLOW);
604 }
Definition: dbprint.h:59
void dbprintInt(int32_t value)
Print a number in decimal notation to USARTx.
Definition: dbprint.c:738
void dbprintln_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color and go to the next line.
Definition: dbprint.c:480
void dbprint_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color.
Definition: dbprint.c:421

◆ dbwarnInt_hex()

void dbwarnInt_hex ( char *  message1,
int32_t  value,
char *  message2 
)

Print a warning value surrounded by two strings (char array) to USARTx.

"WARN: " gets added in front, the hexadecimal notation is used and the function advances to the next line. The value is in the color white, the rest is yellow.

Note
If the input is not a string (ex.: "Hello world!") but a char array, the input message (array) needs to end with NULL ("\0")!
Parameters
[in]message1The first part of the string to print to USARTx.
[in]valueThe value to print between the two string parts.
[in]message2The second part of the string to print to USARTx.

Definition at line 690 of file dbprint.c.

691 {
692  dbprint_color("WARN: ", YELLOW);
693  dbprint_color(message1, YELLOW);
694  dbprintInt_hex(value);
695  dbprintln_color(message2, YELLOW);
696 }
Definition: dbprint.h:59
void dbprintInt_hex(int32_t value)
Print a number in hexadecimal notation to USARTx.
Definition: dbprint.c:794
void dbprintln_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color and go to the next line.
Definition: dbprint.c:480
void dbprint_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color.
Definition: dbprint.c:421