Master Thesis  V1.0
Research and Design of Sensor Node for NMSD Treatment
dbprint.c
Go to the documentation of this file.
1 /***************************************************************************/
82 #include "debug_dbprint.h" /* Enable or disable printing to UART for debugging */
83 
84 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
85 
86 
87 #include <stdint.h> /* (u)intXX_t */
88 #include <stdbool.h> /* "bool", "true", "false" */
89 #include "em_cmu.h" /* Clock Management Unit */
90 #include "em_gpio.h" /* General Purpose IO (GPIO) peripheral API */
91 #include "em_usart.h" /* Universal synchr./asynchr. receiver/transmitter (USART/UART) Peripheral API */
92 
93 
94 /* Local definitions */
95 /* Macro definitions that return a character when given a value */
96 #define TO_HEX(i) (i <= 9 ? '0' + i : 'A' - 10 + i) /* "?:" = ternary operator (return ['0' + i] if [i <= 9] = true, ['A' - 10 + i] if false) */
97 #define TO_DEC(i) (i <= 9 ? '0' + i : '?') /* return "?" if out of range */
98 
99 /* ANSI colors */
100 #define COLOR_RED "\x1b[31m"
101 #define COLOR_GREEN "\x1b[32m"
102 #define COLOR_BLUE "\x1b[34m"
103 #define COLOR_CYAN "\x1b[36m"
104 #define COLOR_MAGENTA "\x1b[35m"
105 #define COLOR_YELLOW "\x1b[33m"
106 #define COLOR_RESET "\x1b[0m"
107 
108 
110 USART_TypeDef* dbpointer;
111 
112 /* Local variables to store data */
113 /* -> Volatile because it's modified by an interrupt service routine (@RAM) */
114 volatile bool dataReceived = false; /* true if there is a line of data received */
115 volatile char rx_buffer[DBPRINT_BUFFER_SIZE];
116 volatile char tx_buffer[DBPRINT_BUFFER_SIZE];
117 
118 
119 /* Local prototypes */
120 static void uint32_to_charHex (char *buf, uint32_t value, bool spacing);
121 static void uint32_to_charDec (char *buf, uint32_t value);
122 static uint32_t charDec_to_uint32 (char *buf);
123 //static uint32_t charHex_to_uint32 (char *buf); // Unused but kept here just in case
124 
125 
126 /**************************************************************************/
144 void dbprint_INIT (USART_TypeDef* pointer, uint8_t location, bool vcom, bool interrupts)
145 {
146  /* Store the pointer in the global variable */
147  dbpointer = pointer;
148 
149  /*
150  * USART_INITASYNC_DEFAULT:
151  * config.enable = usartEnable // Specifies whether TX and/or RX is enabled when initialization is completed
152  * // (Enable RX/TX when initialization is complete).
153  * config.refFreq = 0 // USART/UART reference clock assumed when configuring baud rate setup
154  * // (0 = Use current configured reference clock for configuring baud rate).
155  * config.baudrate = 115200 // Desired baudrate (115200 bits/s).
156  * config.oversampling = usartOVS16 // Oversampling used (16x oversampling).
157  * config.databits = usartDatabits8 // Number of data bits in frame (8 data bits).
158  * config.parity = usartNoParity // Parity mode to use (no parity).
159  * config.stopbits = usartStopbits1 // Number of stop bits to use (1 stop bit).
160  * config.mvdis = false // Majority Vote Disable for 16x, 8x and 6x oversampling modes (Do not disable majority vote).
161  * config.prsRxEnable = false // Enable USART Rx via PRS (Not USART PRS input mode).
162  * config.prsRxCh = 0 // Select PRS channel for USART Rx. (Only valid if prsRxEnable is true - PRS channel 0).
163  * config.autoCsEnable = false // Auto CS enabling (Auto CS functionality enable/disable switch - disabled).
164  */
165 
166  USART_InitAsync_TypeDef config = USART_INITASYNC_DEFAULT;
167 
168  /* Enable oscillator to GPIO*/
169  CMU_ClockEnable(cmuClock_GPIO, true);
170 
171 
172  /* Enable oscillator to USARTx modules */
173  if (dbpointer == USART0)
174  {
175  CMU_ClockEnable(cmuClock_USART0, true);
176  }
177  else if (dbpointer == USART1)
178  {
179  CMU_ClockEnable(cmuClock_USART1, true);
180  }
181 
182 
183  /* Set PA9 (EFM_BC_EN) high if necessary to enable the isolation switch */
184  if (vcom)
185  {
186  GPIO_PinModeSet(gpioPortA, 9, gpioModePushPull, 1);
187  GPIO_PinOutSet(gpioPortA, 9);
188  }
189 
190 
191  /* Set pin modes for UART TX and RX pins */
192  if (dbpointer == USART0)
193  {
194  switch (location)
195  {
196  case 0:
197  GPIO_PinModeSet(gpioPortE, 11, gpioModeInput, 0); /* RX */
198  GPIO_PinModeSet(gpioPortE, 10, gpioModePushPull, 1); /* TX */
199  break;
200  case 2:
201  GPIO_PinModeSet(gpioPortC, 10, gpioModeInput, 0); /* RX */
202  /* No TX pin in this mode */
203  break;
204  case 3:
205  GPIO_PinModeSet(gpioPortE, 12, gpioModeInput, 0); /* RX */
206  GPIO_PinModeSet(gpioPortE, 13, gpioModePushPull, 1); /* TX */
207  break;
208  case 4:
209  GPIO_PinModeSet(gpioPortB, 8, gpioModeInput, 0); /* RX */
210  GPIO_PinModeSet(gpioPortB, 7, gpioModePushPull, 1); /* TX */
211  break;
212  case 5:
213  case 6:
214  GPIO_PinModeSet(gpioPortC, 1, gpioModeInput, 0); /* RX */
215  GPIO_PinModeSet(gpioPortC, 0, gpioModePushPull, 1); /* TX */
216  break;
217  /* default: */
218  /* No default */
219  }
220  }
221  else if (dbpointer == USART1)
222  {
223  switch (location)
224  {
225  case 0:
226  GPIO_PinModeSet(gpioPortC, 1, gpioModeInput, 0); /* RX */
227  GPIO_PinModeSet(gpioPortC, 0, gpioModePushPull, 1); /* TX */
228  break;
229  case 2:
230  case 3:
231  GPIO_PinModeSet(gpioPortD, 6, gpioModeInput, 0); /* RX */
232  GPIO_PinModeSet(gpioPortD, 7, gpioModePushPull, 1); /* TX */
233  break;
234  case 4:
235  GPIO_PinModeSet(gpioPortA, 0, gpioModeInput, 0); /* RX */
236  GPIO_PinModeSet(gpioPortF, 2, gpioModePushPull, 1); /* TX */
237  break;
238  case 5:
239  GPIO_PinModeSet(gpioPortC, 2, gpioModeInput, 0); /* RX */
240  GPIO_PinModeSet(gpioPortC, 1, gpioModePushPull, 1); /* TX */
241  break;
242  /* default: */
243  /* No default */
244  }
245  }
246 
247 
248  /* Initialize USART asynchronous mode */
249  USART_InitAsync(dbpointer, &config);
250 
251  /* Route pins */
252  switch (location)
253  {
254  case 0:
255  dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC0;
256  break;
257  case 1:
258  dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC1;
259  break;
260  case 2:
261  dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC2;
262  break;
263  case 3:
264  dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC3;
265  break;
266  case 4:
267  dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC4;
268  break;
269  case 5:
270  dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC5;
271  break;
272  case 6:
273  dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC6;
274  break;
275  default:
276  dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_DEFAULT;
277  }
278 
279  /* Enable interrupts if necessary and print welcome string (and make an alert sound in the console) */
280  if (interrupts)
281  {
282  /* Initialize USART interrupts */
283 
284  /* RX Data Valid Interrupt Enable
285  * Set when data is available in the receive buffer. Cleared when the receive buffer is empty. */
286  USART_IntEnable(dbpointer, USART_IEN_RXDATAV);
287 
288  /* TX Complete Interrupt Enable
289  * Set when a transmission has completed and no more data is available in the transmit buffer.
290  * Cleared when a new transmission starts. */
291  USART_IntEnable(dbpointer, USART_IEN_TXC);
292 
293  if (dbpointer == USART0)
294  {
295  /* Enable USART interrupts */
296  NVIC_EnableIRQ(USART0_RX_IRQn);
297  NVIC_EnableIRQ(USART0_TX_IRQn);
298  }
299  else if (dbpointer == USART1)
300  {
301  /* Enable USART interrupts */
302  NVIC_EnableIRQ(USART1_RX_IRQn);
303  NVIC_EnableIRQ(USART1_TX_IRQn);
304  }
305 
306  /* Print welcome string */
307  dbprint(COLOR_RESET);
308  dbprintln("\a\r\f### UART initialized (interrupt mode) ###");
309  dbinfo("This is an info message.");
310  dbwarn("This is a warning message.");
311  dbcrit("This is a critical error message.");
312  dbprintln("### Start executing programmed code ###\n");
313 
314  /* Set TX Complete Interrupt Flag (transmission has completed and no more data
315  * is available in the transmit buffer) */
316  USART_IntSet(dbpointer, USART_IFS_TXC);
317  }
318  /* Print welcome string (and make an alert sound in the console) if not in interrupt mode */
319  else
320  {
321  dbprint(COLOR_RESET);
322  dbprintln("\a\r\f### UART initialized (no interrupts) ###");
323  dbinfo("This is an info message.");
324  dbwarn("This is a warning message.");
325  dbcrit("This is a critical error message.");
326  dbprintln("### Start executing programmed code ###\n");
327  }
328 }
329 
330 
331 /**************************************************************************/
338 void dbAlert (void)
339 {
340  USART_Tx(dbpointer, '\a');
341 }
342 
343 
344 /**************************************************************************/
352 void dbClear (void)
353 {
354  USART_Tx(dbpointer, '\f');
355 }
356 
357 
358 /**************************************************************************/
369 void dbprint (char *message)
370 {
371  /* "message[i] != 0" makes "uint32_t length = strlen(message)"
372  * not necessary (given string MUST be terminated by NULL for this to work) */
373  for (uint32_t i = 0; message[i] != 0; i++)
374  {
375  USART_Tx(dbpointer, message[i]);
376  }
377 }
378 
379 
380 /**************************************************************************/
391 void dbprintln (char *message)
392 {
393  dbprint(message);
394 
395  /* Carriage return */
396  USART_Tx(dbpointer, '\r');
397 
398  /* Line feed (new line) */
399  USART_Tx(dbpointer, '\n');
400 }
401 
402 
403 /**************************************************************************/
417 void dbprint_color (char *message, dbprint_color_t color)
418 {
419  switch (color)
420  {
421  case DEFAULT_COLOR:
422  dbprint(COLOR_RESET);
423  dbprint(message);
424  break;
425  case RED:
426  dbprint(COLOR_RED);
427  dbprint(message);
428  dbprint(COLOR_RESET);
429  break;
430  case GREEN:
431  dbprint(COLOR_GREEN);
432  dbprint(message);
433  dbprint(COLOR_RESET);
434  break;
435  case BLUE:
436  dbprint(COLOR_BLUE);
437  dbprint(message);
438  dbprint(COLOR_RESET);
439  break;
440  case CYAN:
441  dbprint(COLOR_CYAN);
442  dbprint(message);
443  dbprint(COLOR_RESET);
444  break;
445  case MAGENTA:
446  dbprint(COLOR_MAGENTA);
447  dbprint(message);
448  dbprint(COLOR_RESET);
449  break;
450  case YELLOW:
451  dbprint(COLOR_YELLOW);
452  dbprint(message);
453  dbprint(COLOR_RESET);
454  break;
455  default:
456  dbprint(COLOR_RESET);
457  dbprint(message);
458  }
459 }
460 
461 
462 /**************************************************************************/
476 void dbprintln_color (char *message, dbprint_color_t color)
477 {
478  dbprint_color(message, color);
479 
480  /* Carriage return */
481  USART_Tx(dbpointer, '\r');
482 
483  /* Line feed (new line) */
484  USART_Tx(dbpointer, '\n');
485 }
486 
487 
488 /**************************************************************************/
499 void dbinfo (char *message)
500 {
501  dbprint("INFO: ");
502  dbprintln(message);
503 }
504 
505 
506 /**************************************************************************/
517 void dbwarn (char *message)
518 {
519  dbprint_color("WARN: ", YELLOW);
520  dbprintln_color(message, YELLOW);
521 }
522 
523 
524 /**************************************************************************/
535 void dbcrit (char *message)
536 {
537  dbprint_color("CRIT: ", RED);
538  dbprintln_color(message, RED);
539 }
540 
541 
542 /**************************************************************************/
563 void dbinfoInt (char *message1, int32_t value, char *message2)
564 {
565  dbprint("INFO: ");
566  dbprint(message1);
567  dbprintInt(value);
568  dbprintln(message2);
569 }
570 
571 
572 /**************************************************************************/
594 void dbwarnInt (char *message1, int32_t value, char *message2)
595 {
596  dbprint_color("WARN: ", YELLOW);
597  dbprint_color(message1, YELLOW);
598  dbprintInt(value);
599  dbprintln_color(message2, YELLOW);
600 }
601 
602 
603 /**************************************************************************/
625 void dbcritInt (char *message1, int32_t value, char *message2)
626 {
627  dbprint_color("CRIT: ", RED);
628  dbprint_color(message1, RED);
629  dbprintInt(value);
630  dbprintln_color(message2, RED);
631 }
632 
633 
634 /**************************************************************************/
655 void dbinfoInt_hex (char *message1, int32_t value, char *message2)
656 {
657  dbprint("INFO: ");
658  dbprint(message1);
659  dbprintInt_hex(value);
660  dbprintln(message2);
661 }
662 
663 
664 /**************************************************************************/
686 void dbwarnInt_hex (char *message1, int32_t value, char *message2)
687 {
688  dbprint_color("WARN: ", YELLOW);
689  dbprint_color(message1, YELLOW);
690  dbprintInt_hex(value);
691  dbprintln_color(message2, YELLOW);
692 }
693 
694 
695 /**************************************************************************/
717 void dbcritInt_hex (char *message1, int32_t value, char *message2)
718 {
719  dbprint_color("CRIT: ", RED);
720  dbprint_color(message1, RED);
721  dbprintInt_hex(value);
722  dbprintln_color(message2, RED);
723 }
724 
725 
726 /**************************************************************************/
734 void dbprintInt (int32_t value)
735 {
736  /* Buffer to put the char array in (Needs to be 10) */
737  char decchar[10];
738 
739  /* Convert a negative number to a positive one and print the "-" */
740  if (value < 0)
741  {
742  /* Negative of value = flip all bits, +1
743  * bitwise logic: "~" = "NOT" */
744  uint32_t negativeValue = (~value) + 1;
745 
746  dbprint("-");
747 
748  /* Convert the value */
749  uint32_to_charDec(decchar, negativeValue);
750  }
751  else
752  {
753  /* Convert the value */
754  uint32_to_charDec(decchar, value);
755  }
756 
757  /* Print the buffer */
758  dbprint(decchar);
759 }
760 
761 
762 /**************************************************************************/
770 void dbprintlnInt (int32_t value)
771 {
772  dbprintInt(value);
773 
774  /* Carriage return */
775  USART_Tx(dbpointer, '\r');
776 
777  /* Line feed (new line) */
778  USART_Tx(dbpointer, '\n');
779 }
780 
781 
782 /**************************************************************************/
790 void dbprintInt_hex (int32_t value)
791 {
792  char hexchar[9]; /* Needs to be 9 */
793  uint32_to_charHex(hexchar, value, true); /* true: add spacing between eight HEX chars */
794  dbprint("0x");
795  dbprint(hexchar);
796 }
797 
798 
799 /**************************************************************************/
807 void dbprintlnInt_hex (int32_t value)
808 {
809  dbprintInt_hex(value);
810 
811  /* Carriage return */
812  USART_Tx(dbpointer, '\r');
813 
814  /* Line feed (new line) */
815  USART_Tx(dbpointer, '\n');
816 }
817 
818 
819 /**************************************************************************/
826 char dbReadChar (void)
827 {
828  return (USART_Rx(dbpointer));
829 }
830 
831 
832 /**************************************************************************/
844 uint8_t dbReadInt (void)
845 {
846  /* Method expects a char array ending with a null termination character */
847  char value[2];
848  value[0]= dbReadChar();
849  value[1] = '\0';
850 
851  return (charDec_to_uint32(value));
852 }
853 
854 
855 /**************************************************************************/
868 void dbReadLine (char *buf)
869 {
870  for (uint32_t i = 0; i < DBPRINT_BUFFER_SIZE - 1 ; i++ )
871  {
872  char localBuffer = USART_Rx(dbpointer);
873 
874  /* Check if a CR character is received */
875  if (localBuffer == '\r')
876  {
877  /* End with a null termination character, expected by the dbprintln method */
878  buf[i] = '\0';
879  break;
880  }
881  else
882  {
883  buf[i] = localBuffer;
884  }
885  }
886 }
887 
888 
889 /**************************************************************************/
905 bool dbGet_RXstatus (void)
906 {
907  return (dataReceived);
908 }
909 
910 
911 // TODO: Needs fixing (but probably won't ever be used):
912 //**************************************************************************//**
913 // * @brief
914 // * Set the value of the TX buffer and start transmitting it using interrupts.
915 // *
916 // * @details
917 // * The `"TX Complete Interrupt Flag"` also gets set at the end of the
918 // * function (transmission has completed and no more data is available
919 // * in the transmit buffer).
920 // *
921 // * @note
922 // * If the input is not a string (ex.: `"Hello world!"`) but a char array,
923 // * the input message (array) needs to end with NULL (`"\0"`)!
924 // *
925 // * @note
926 // * The index of the RX buffer gets reset in the TX handler when all the
927 // * characters in the buffer are send.
928 // *
929 // * @attention
930 // * Interrupt functionality has to be enabled on initialization for this
931 // * function to work correctly.
932 // *
933 // * @param[in] message
934 // * The string to put in the TX buffer.
935 // *****************************************************************************/
936 //void dbSet_TXbuffer (char *message)
937 //{
938 // uint32_t i;
939 //
940 // /* Copy data to the TX buffer */
941 // for (i = 0; message[i] != 0 && i < DBPRINT_BUFFER_SIZE-1; i++)
942 // {
943 // tx_buffer[i] = message[i];
944 // }
945 //
946 // /* Set TX Complete Interrupt Flag (transmission has completed and no more data
947 // * is available in the transmit buffer) */
948 // USART_IntSet(dbpointer, USART_IFS_TXC);
949 //
950 // TODO: Perhaps some functionality needs to be added to send numbers and
951 // that might need functionality to disable interrupts. For reference
952 // some old code that could be put in a main.c file is put below.
953 // /* Data is ready to retransmit (notified by the RX handler) */
954 // if (dbprint_rxdata)
955 // {
956 // uint32_t i;
957 //
958 // /* RX Data Valid Interrupt Enable
959 // * Set when data is available in the receive buffer. Cleared when the receive buffer is empty.
960 // *
961 // * TX Complete Interrupt Enable
962 // * Set when a transmission has completed and no more data is available in the transmit buffer.
963 // * Cleared when a new transmission starts.
964 // */
965 //
966 // /* Disable "RX Data Valid Interrupt Enable" and "TX Complete Interrupt Enable" interrupts */
967 // USART_IntDisable(dbpointer, USART_IEN_RXDATAV);
968 // USART_IntDisable(dbpointer, USART_IEN_TXC);
969 //
970 // /* Copy data from the RX buffer to the TX buffer */
971 // for (i = 0; dbprint_rx_buffer[i] != 0 && i < DBPRINT_BUFFER_SIZE-3; i++)
972 // {
973 // dbprint_tx_buffer[i] = dbprint_rx_buffer[i];
974 // }
975 //
976 // /* Add "new line" characters */
977 // dbprint_tx_buffer[i++] = '\r';
978 // dbprint_tx_buffer[i++] = '\n';
979 // dbprint_tx_buffer[i] = '\0';
980 //
981 // /* Reset "notification" variable */
982 // dbprint_rxdata = false;
983 //
984 // /* Enable "RX Data Valid Interrupt" and "TX Complete Interrupt" interrupts */
985 // USART_IntEnable(dbpointer, USART_IEN_RXDATAV);
986 // USART_IntEnable(dbpointer, USART_IEN_TXC);
987 //
988 // /* Set TX Complete Interrupt Flag (transmission has completed and no more data
989 // * is available in the transmit buffer) */
990 // USART_IntSet(dbpointer, USART_IFS_TXC);
991 // }
992 //
993 //}
994 
995 
996 /**************************************************************************/
1013 void dbGet_RXbuffer (char *buf)
1014 {
1015  if (dataReceived)
1016  {
1017  uint32_t i;
1018 
1019  /* Copy data from the RX buffer to the given buffer */
1020  for (i = 0; rx_buffer[i] != 0 && i < DBPRINT_BUFFER_SIZE-1; i++)
1021  {
1022  buf[i] = rx_buffer[i];
1023  }
1024 
1025  /* Add NULL termination character */
1026  buf[i++] = '\0';
1027 
1028  /* Reset "notification" variable */
1029  dataReceived = false;
1030  }
1031  else
1032  {
1033  dbcrit("No received data available!");
1034  }
1035 }
1036 
1037 
1038 /**************************************************************************/
1057 static void uint32_to_charHex (char *buf, uint32_t value, bool spacing)
1058 {
1059  /* 4 nibble HEX representation */
1060  if (value <= 0xFFFF)
1061  {
1062  /* Only get necessary nibble by ANDing with a mask and
1063  * shifting one nibble (4 bits) per position */
1064  buf[0] = TO_HEX(((value & 0xF000) >> 12));
1065  buf[1] = TO_HEX(((value & 0x0F00) >> 8 ));
1066  buf[2] = TO_HEX(((value & 0x00F0) >> 4 ));
1067  buf[3] = TO_HEX( (value & 0x000F) );
1068  buf[4] = '\0'; /* NULL termination character */
1069  }
1070 
1071  /* 8 nibble HEX representation */
1072  else
1073  {
1074  /* Only get necessary nibble by ANDing with a mask and
1075  * shifting one nibble (4 bits) per position */
1076  buf[0] = TO_HEX(((value & 0xF0000000) >> 28));
1077  buf[1] = TO_HEX(((value & 0x0F000000) >> 24));
1078  buf[2] = TO_HEX(((value & 0x00F00000) >> 20));
1079  buf[3] = TO_HEX(((value & 0x000F0000) >> 16));
1080 
1081  /* Add spacing if necessary */
1082  if (spacing)
1083  {
1084  buf[4] = ' '; /* Spacing */
1085  buf[5] = TO_HEX(((value & 0x0000F000) >> 12));
1086  buf[6] = TO_HEX(((value & 0x00000F00) >> 8 ));
1087  buf[7] = TO_HEX(((value & 0x000000F0) >> 4 ));
1088  buf[8] = TO_HEX( (value & 0x0000000F) );
1089  buf[9] = '\0'; /* NULL termination character */
1090  }
1091  else
1092  {
1093  buf[4] = TO_HEX(((value & 0x0000F000) >> 12));
1094  buf[5] = TO_HEX(((value & 0x00000F00) >> 8 ));
1095  buf[6] = TO_HEX(((value & 0x000000F0) >> 4 ));
1096  buf[7] = TO_HEX( (value & 0x0000000F) );
1097  buf[8] = '\0'; /* NULL termination character */
1098  }
1099  }
1100 }
1101 
1102 
1103 /**************************************************************************/
1118 static void uint32_to_charDec (char *buf, uint32_t value)
1119 {
1120  if (value == 0)
1121  {
1122  buf[0] = '0';
1123  buf[1] = '\0'; /* NULL termination character */
1124  }
1125  else
1126  {
1127  /* MAX uint32_t value = FFFFFFFFh = 4294967295d (10 decimal chars) */
1128  char backwardsBuf[10];
1129 
1130  uint32_t calcval = value;
1131  uint8_t length = 0;
1132  uint8_t lengthCounter = 0;
1133 
1134 
1135  /* Loop until the value is zero (separate characters 0-9) and calculate length */
1136  while (calcval)
1137  {
1138  uint32_t rem = calcval % 10;
1139  backwardsBuf[length] = TO_DEC(rem); /* Convert to ASCII character */
1140  length++;
1141 
1142  calcval = calcval - rem;
1143  calcval = calcval / 10;
1144  }
1145 
1146  /* Backwards counter */
1147  lengthCounter = length;
1148 
1149  /* Reverse the characters in the buffer for the final string */
1150  for (uint8_t i = 0; i < length; i++)
1151  {
1152  buf[i] = backwardsBuf[lengthCounter-1];
1153  lengthCounter--;
1154  }
1155 
1156  /* Add NULL termination character */
1157  buf[length] = '\0';
1158  }
1159 }
1160 
1161 
1162 /**************************************************************************/
1180 static uint32_t charDec_to_uint32 (char *buf)
1181 {
1182  /* Value to eventually return */
1183  uint32_t value = 0;
1184 
1185  /* Loop until buffer is empty */
1186  while (*buf)
1187  {
1188  /* Get current character, increment afterwards */
1189  uint8_t byte = *buf++;
1190 
1191  /* Check if the next value we need to add can fit in a uint32_t */
1192  if ( (value <= 0xFFFFFFF) && ((byte - '0') <= 0xF) )
1193  {
1194  /* Convert the ASCII (decimal) character to the representing decimal value
1195  * and add it to the value (which is multiplied by 10 for each position) */
1196  value = (value * 10) + (byte - '0');
1197  }
1198  else
1199  {
1200  /* Given buffer can't fit in uint32_t */
1201  return (0);
1202  }
1203  }
1204 
1205  return (value);
1206 }
1207 
1208 
1209 // Unused but kept here just in case:
1210 //**************************************************************************//**
1211 // * @brief
1212 // * Convert a string (char array) in hexadecimal notation to a `uint32_t` value.
1213 // *
1214 // * @note
1215 // * If the input is not a string (ex.: `"00120561"`) but a char array,
1216 // * the input buffer (array) needs to end with NULL (`"\0"`)!@n
1217 // * The input string can't have the prefix `0x`.
1218 // *
1219 // * @note
1220 // * This is a static method because it's only internally used in this file
1221 // * and called by other methods if necessary.
1222 // *
1223 // * @param[in] buf
1224 // * The hexadecimal string to convert to a `uint32_t` value.
1225 // *
1226 // * @return
1227 // * The resulting `uint32_t` value.
1228 // *****************************************************************************/
1229 //static uint32_t charHex_to_uint32 (char *buf)
1230 //{
1231 // /* Value to eventually return */
1232 // uint32_t value = 0;
1233 //
1234 // /* Loop until buffer is empty */
1235 // while (*buf)
1236 // {
1237 // /* Get current character, increment afterwards */
1238 // uint8_t byte = *buf++;
1239 //
1240 // /* Convert the hex character to the 4-bit equivalent
1241 // * number using the ASCII table indexes */
1242 // if (byte >= '0' && byte <= '9') byte = byte - '0';
1243 // else if (byte >= 'a' && byte <='f') byte = byte - 'a' + 10;
1244 // else if (byte >= 'A' && byte <='F') byte = byte - 'A' + 10;
1245 //
1246 // /* Check if the next byte we need to add can fit in a uint32_t */
1247 // if ( (value <= 0xFFFFFFF) && (byte <= 0xF) )
1248 // {
1249 // /* Shift one nibble (4 bits) to make space for a new digit
1250 // * and add the 4 bits (ANDing with a mask, 0xF = 0b1111) */
1251 // value = (value << 4) | (byte & 0xF);
1252 // }
1253 // else
1254 // {
1255 // /* Given buffer can't fit in uint32_t */
1256 // return (0);
1257 // }
1258 // }
1259 //
1260 // return (value);
1261 //}
1262 
1263 
1264 /**************************************************************************/
1275 void USART0_RX_IRQHandler(void)
1276 {
1277  /* "static" so it keeps its value between invocations */
1278  static uint32_t i = 0;
1279 
1280  /* Get and clear the pending USART interrupt flags */
1281  uint32_t flags = USART_IntGet(dbpointer);
1282  USART_IntClear(dbpointer, flags);
1283 
1284  /* Store incoming data into dbprint_rx_buffer */
1285  rx_buffer[i++] = USART_Rx(dbpointer);
1286 
1287  /* Set dbprint_rxdata when a special character is received (~ full line received) */
1288  if ( (rx_buffer[i - 1] == '\r') || (rx_buffer[i - 1] == '\f') )
1289  {
1290  dataReceived = true;
1291  rx_buffer[i - 1] = '\0'; /* Overwrite CR or LF character */
1292  i = 0;
1293  }
1294 
1295  /* Set dbprint_rxdata when the buffer is full */
1296  if (i >= (DBPRINT_BUFFER_SIZE - 2))
1297  {
1298  dataReceived = true;
1299  rx_buffer[i] = '\0'; /* Do not overwrite last character */
1300  i = 0;
1301  }
1302 }
1303 
1304 
1305 /**************************************************************************/
1315 void USART0_TX_IRQHandler(void)
1316 {
1317  /* "static" so it keeps its value between invocations */
1318  static uint32_t i = 0;
1319 
1320  /* Get and clear the pending USART interrupt flags */
1321  uint32_t flags = USART_IntGet(dbpointer);
1322  USART_IntClear(dbpointer, flags);
1323 
1324  /* Mask flags AND "TX Complete Interrupt Flag" */
1325  if (flags & USART_IF_TXC)
1326  {
1327  /* Index is smaller than the maximum buffer size and
1328  * the current item to print is not "NULL" (\0) */
1329  if ( (i < DBPRINT_BUFFER_SIZE) && (tx_buffer[i] != '\0') )
1330  {
1331  /* Transmit byte at current index and increment index */
1332  USART_Tx(dbpointer, tx_buffer[i++]);
1333  }
1334  else
1335  {
1336  i = 0; /* No more data to send */
1337  }
1338  }
1339 }
1340 
1341 
1342 /**************************************************************************/
1349 void USART1_RX_IRQHandler(void)
1350 {
1351  /* Call other handler */
1352  USART0_RX_IRQHandler();
1353 }
1354 
1355 
1356 /**************************************************************************/
1363 void USART1_TX_IRQHandler(void)
1364 {
1365  /* Call other handler */
1366  USART0_TX_IRQHandler();
1367 }
1368 
1369 #endif /* DEBUG_DBPRINT */
dbClear
void dbClear(void)
dbprint_INIT
void dbprint_INIT(USART_TypeDef *pointer, uint8_t location, bool vcom, bool interrupts)
MAGENTA
Definition: dbprint.h:58
dbprint_color
void dbprint_color(char *message, dbprint_color_t color)
dbprintln_color
void dbprintln_color(char *message, dbprint_color_t color)
dbGet_RXbuffer
void dbGet_RXbuffer(char *buf)
dbprintInt_hex
void dbprintInt_hex(int32_t value)
dbinfoInt
void dbinfoInt(char *message1, int32_t value, char *message2)
dbGet_RXstatus
bool dbGet_RXstatus(void)
dbprint
void dbprint(char *message)
dbprintInt
void dbprintInt(int32_t value)
DEFAULT_COLOR
Definition: dbprint.h:60
dbcritInt
void dbcritInt(char *message1, int32_t value, char *message2)
BLUE
Definition: dbprint.h:56
dbwarnInt
void dbwarnInt(char *message1, int32_t value, char *message2)
dbReadChar
char dbReadChar(void)
dbcritInt_hex
void dbcritInt_hex(char *message1, int32_t value, char *message2)
dbprintlnInt
void dbprintlnInt(int32_t value)
dbprint_color_t
enum dbprint_colors dbprint_color_t
dbinfoInt_hex
void dbinfoInt_hex(char *message1, int32_t value, char *message2)
dbwarn
void dbwarn(char *message)
USART1_TX_IRQHandler
void USART1_TX_IRQHandler(void)
UART1 TX IRQ Handler.
Definition: uart.c:250
RED
Definition: dbprint.h:54
dbReadInt
uint8_t dbReadInt(void)
dbwarnInt_hex
void dbwarnInt_hex(char *message1, int32_t value, char *message2)
DBPRINT_BUFFER_SIZE
#define DBPRINT_BUFFER_SIZE
Definition: dbprint.h:48
dbprintlnInt_hex
void dbprintlnInt_hex(int32_t value)
dbAlert
void dbAlert(void)
dbReadLine
void dbReadLine(char *buf)
CYAN
Definition: dbprint.h:57
dbcrit
void dbcrit(char *message)
GREEN
Definition: dbprint.h:55
YELLOW
Definition: dbprint.h:59
dbinfo
void dbinfo(char *message)
dbprintln
void dbprintln(char *message)
USART1_RX_IRQHandler
void USART1_RX_IRQHandler(void)
UART1 RX IRQ Handler.
Definition: uart.c:223
debug_dbprint.h
Enable or disable printing to UART with dbprint.