DeBugPrint (dbprint)
Homebrew minimal low-level println/printf replacement
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
7.0
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 344 of file dbprint.c.

345 {
346  USART_Tx(dbpointer, '\a');
347 }
USART_TypeDef * dbpointer
Definition: dbprint.c:116

◆ 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 358 of file dbprint.c.

359 {
360  USART_Tx(dbpointer, '\f');
361 }
USART_TypeDef * dbpointer
Definition: dbprint.c:116

◆ 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 541 of file dbprint.c.

542 {
543  dbprint_color("CRIT: ", RED);
544  dbprintln_color(message, RED);
545 }
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:482
void dbprint_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color.
Definition: dbprint.c:423

◆ 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 631 of file dbprint.c.

632 {
633  dbprint_color("CRIT: ", RED);
634  dbprint_color(message1, RED);
635  dbprintInt(value);
636  dbprintln_color(message2, RED);
637 }
Definition: dbprint.h:54
void dbprintInt(int32_t value)
Print a number in decimal notation to USARTx.
Definition: dbprint.c:740
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:482
void dbprint_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color.
Definition: dbprint.c:423

◆ 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 723 of file dbprint.c.

724 {
725  dbprint_color("CRIT: ", RED);
726  dbprint_color(message1, RED);
727  dbprintInt_hex(value);
728  dbprintln_color(message2, RED);
729 }
Definition: dbprint.h:54
void dbprintInt_hex(int32_t value)
Print a number in hexadecimal notation to USARTx.
Definition: dbprint.c:796
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:482
void dbprint_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color.
Definition: dbprint.c:423

◆ 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 1019 of file dbprint.c.

1020 {
1021  if (dataReceived)
1022  {
1023  uint32_t i;
1024 
1025  /* Copy data from the RX buffer to the given buffer */
1026  for (i = 0; rx_buffer[i] != 0 && i < DBPRINT_BUFFER_SIZE-1; i++)
1027  {
1028  buf[i] = rx_buffer[i];
1029  }
1030 
1031  /* Add NULL termination character */
1032  buf[i++] = '\0';
1033 
1034  /* Reset "notification" variable */
1035  dataReceived = false;
1036  }
1037  else
1038  {
1039  dbcrit("No received data available!");
1040  }
1041 }
volatile bool dataReceived
Definition: dbprint.c:120
volatile char rx_buffer[80]
Definition: dbprint.c:121
#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:541

◆ 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 911 of file dbprint.c.

912 {
913  return (dataReceived);
914 }
volatile bool dataReceived
Definition: dbprint.c:120

◆ 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 505 of file dbprint.c.

506 {
507  dbprint("INFO: ");
508  dbprintln(message);
509 }
void dbprint(char *message)
Print a string (char array) to USARTx.
Definition: dbprint.c:375
void dbprintln(char *message)
Print a string (char array) to USARTx and go to the next line.
Definition: dbprint.c:397

◆ 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 569 of file dbprint.c.

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

◆ 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 661 of file dbprint.c.

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

◆ 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 375 of file dbprint.c.

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

◆ 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 423 of file dbprint.c.

424 {
425  switch (color)
426  {
427  case DEFAULT_COLOR:
429  dbprint(message);
430  break;
431  case RED:
433  dbprint(message);
435  break;
436  case GREEN:
438  dbprint(message);
440  break;
441  case BLUE:
443  dbprint(message);
445  break;
446  case CYAN:
448  dbprint(message);
450  break;
451  case MAGENTA:
453  dbprint(message);
455  break;
456  case YELLOW:
458  dbprint(message);
460  break;
461  default:
463  dbprint(message);
464  }
465 }
Definition: dbprint.h:57
#define COLOR_GREEN
Definition: dbprint.c:107
void dbprint(char *message)
Print a string (char array) to USARTx.
Definition: dbprint.c:375
#define COLOR_YELLOW
Definition: dbprint.c:111
Definition: dbprint.h:54
#define COLOR_MAGENTA
Definition: dbprint.c:110
#define COLOR_RED
Definition: dbprint.c:106
Definition: dbprint.h:55
Definition: dbprint.h:59
Definition: dbprint.h:56
#define COLOR_BLUE
Definition: dbprint.c:108
#define COLOR_CYAN
Definition: dbprint.c:109
#define COLOR_RESET
Definition: dbprint.c:112

◆ 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 150 of file dbprint.c.

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

◆ 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 740 of file dbprint.c.

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

◆ 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 796 of file dbprint.c.

797 {
798  char hexchar[9]; /* Needs to be 9 */
799  uint32_to_charHex(hexchar, value, true); /* true: add spacing between eight HEX chars */
800  dbprint("0x");
801  dbprint(hexchar);
802 }
void dbprint(char *message)
Print a string (char array) to USARTx.
Definition: dbprint.c:375
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:1063

◆ 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 397 of file dbprint.c.

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

◆ 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 482 of file dbprint.c.

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

◆ 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 776 of file dbprint.c.

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

◆ 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 813 of file dbprint.c.

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

◆ dbReadChar()

char dbReadChar ( void  )

Read a character from USARTx.

Returns
The character read from USARTx.

Definition at line 832 of file dbprint.c.

833 {
834  return (USART_Rx(dbpointer));
835 }
USART_TypeDef * dbpointer
Definition: dbprint.c:116

◆ 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 850 of file dbprint.c.

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

◆ 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 874 of file dbprint.c.

875 {
876  for (uint32_t i = 0; i < DBPRINT_BUFFER_SIZE - 1 ; i++ )
877  {
878  char localBuffer = USART_Rx(dbpointer);
879 
880  /* Check if a CR character is received */
881  if (localBuffer == '\r')
882  {
883  /* End with a null termination character, expected by the dbprintln method */
884  buf[i] = '\0';
885  break;
886  }
887  else
888  {
889  buf[i] = localBuffer;
890  }
891  }
892 }
USART_TypeDef * dbpointer
Definition: dbprint.c:116
#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 523 of file dbprint.c.

524 {
525  dbprint_color("WARN: ", YELLOW);
526  dbprintln_color(message, YELLOW);
527 }
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:482
void dbprint_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color.
Definition: dbprint.c:423

◆ 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 600 of file dbprint.c.

601 {
602  dbprint_color("WARN: ", YELLOW);
603  dbprint_color(message1, YELLOW);
604  dbprintInt(value);
605  dbprintln_color(message2, YELLOW);
606 }
Definition: dbprint.h:59
void dbprintInt(int32_t value)
Print a number in decimal notation to USARTx.
Definition: dbprint.c:740
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:482
void dbprint_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color.
Definition: dbprint.c:423

◆ 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 692 of file dbprint.c.

693 {
694  dbprint_color("WARN: ", YELLOW);
695  dbprint_color(message1, YELLOW);
696  dbprintInt_hex(value);
697  dbprintln_color(message2, YELLOW);
698 }
Definition: dbprint.h:59
void dbprintInt_hex(int32_t value)
Print a number in hexadecimal notation to USARTx.
Definition: dbprint.c:796
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:482
void dbprint_color(char *message, dbprint_color_t color)
Print a string (char array) to USARTx in a given color.
Definition: dbprint.c:423