84 #if DEBUG_DBPRINT == 1
96 #define TO_HEX(i) (i <= 9 ? '0' + i : 'A' - 10 + i)
97 #define TO_DEC(i) (i <= 9 ? '0' + i : '?')
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"
110 USART_TypeDef* dbpointer;
114 volatile bool dataReceived =
false;
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);
144 void dbprint_INIT (USART_TypeDef* pointer, uint8_t location,
bool vcom,
bool interrupts)
166 USART_InitAsync_TypeDef config = USART_INITASYNC_DEFAULT;
169 CMU_ClockEnable(cmuClock_GPIO,
true);
173 if (dbpointer == USART0)
175 CMU_ClockEnable(cmuClock_USART0,
true);
177 else if (dbpointer == USART1)
179 CMU_ClockEnable(cmuClock_USART1,
true);
186 GPIO_PinModeSet(gpioPortA, 9, gpioModePushPull, 1);
187 GPIO_PinOutSet(gpioPortA, 9);
192 if (dbpointer == USART0)
197 GPIO_PinModeSet(gpioPortE, 11, gpioModeInput, 0);
198 GPIO_PinModeSet(gpioPortE, 10, gpioModePushPull, 1);
201 GPIO_PinModeSet(gpioPortC, 10, gpioModeInput, 0);
205 GPIO_PinModeSet(gpioPortE, 12, gpioModeInput, 0);
206 GPIO_PinModeSet(gpioPortE, 13, gpioModePushPull, 1);
209 GPIO_PinModeSet(gpioPortB, 8, gpioModeInput, 0);
210 GPIO_PinModeSet(gpioPortB, 7, gpioModePushPull, 1);
214 GPIO_PinModeSet(gpioPortC, 1, gpioModeInput, 0);
215 GPIO_PinModeSet(gpioPortC, 0, gpioModePushPull, 1);
221 else if (dbpointer == USART1)
226 GPIO_PinModeSet(gpioPortC, 1, gpioModeInput, 0);
227 GPIO_PinModeSet(gpioPortC, 0, gpioModePushPull, 1);
231 GPIO_PinModeSet(gpioPortD, 6, gpioModeInput, 0);
232 GPIO_PinModeSet(gpioPortD, 7, gpioModePushPull, 1);
235 GPIO_PinModeSet(gpioPortA, 0, gpioModeInput, 0);
236 GPIO_PinModeSet(gpioPortF, 2, gpioModePushPull, 1);
239 GPIO_PinModeSet(gpioPortC, 2, gpioModeInput, 0);
240 GPIO_PinModeSet(gpioPortC, 1, gpioModePushPull, 1);
249 USART_InitAsync(dbpointer, &config);
255 dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC0;
258 dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC1;
261 dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC2;
264 dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC3;
267 dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC4;
270 dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC5;
273 dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC6;
276 dbpointer->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_DEFAULT;
286 USART_IntEnable(dbpointer, USART_IEN_RXDATAV);
291 USART_IntEnable(dbpointer, USART_IEN_TXC);
293 if (dbpointer == USART0)
296 NVIC_EnableIRQ(USART0_RX_IRQn);
297 NVIC_EnableIRQ(USART0_TX_IRQn);
299 else if (dbpointer == USART1)
302 NVIC_EnableIRQ(USART1_RX_IRQn);
303 NVIC_EnableIRQ(USART1_TX_IRQn);
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");
316 USART_IntSet(dbpointer, USART_IFS_TXC);
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");
340 USART_Tx(dbpointer,
'\a');
354 USART_Tx(dbpointer,
'\f');
373 for (uint32_t i = 0; message[i] != 0; i++)
375 USART_Tx(dbpointer, message[i]);
396 USART_Tx(dbpointer,
'\r');
399 USART_Tx(dbpointer,
'\n');
481 USART_Tx(dbpointer,
'\r');
484 USART_Tx(dbpointer,
'\n');
499 void dbinfo (
char *message)
517 void dbwarn (
char *message)
535 void dbcrit (
char *message)
563 void dbinfoInt (
char *message1, int32_t value,
char *message2)
594 void dbwarnInt (
char *message1, int32_t value,
char *message2)
625 void dbcritInt (
char *message1, int32_t value,
char *message2)
655 void dbinfoInt_hex (
char *message1, int32_t value,
char *message2)
686 void dbwarnInt_hex (
char *message1, int32_t value,
char *message2)
717 void dbcritInt_hex (
char *message1, int32_t value,
char *message2)
744 uint32_t negativeValue = (~value) + 1;
749 uint32_to_charDec(decchar, negativeValue);
754 uint32_to_charDec(decchar, value);
775 USART_Tx(dbpointer,
'\r');
778 USART_Tx(dbpointer,
'\n');
793 uint32_to_charHex(hexchar, value,
true);
812 USART_Tx(dbpointer,
'\r');
815 USART_Tx(dbpointer,
'\n');
828 return (USART_Rx(dbpointer));
851 return (charDec_to_uint32(value));
872 char localBuffer = USART_Rx(dbpointer);
875 if (localBuffer ==
'\r')
883 buf[i] = localBuffer;
907 return (dataReceived);
1022 buf[i] = rx_buffer[i];
1029 dataReceived =
false;
1033 dbcrit(
"No received data available!");
1057 static void uint32_to_charHex (
char *buf, uint32_t value,
bool spacing)
1060 if (value <= 0xFFFF)
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) );
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));
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) );
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) );
1118 static void uint32_to_charDec (
char *buf, uint32_t value)
1128 char backwardsBuf[10];
1130 uint32_t calcval = value;
1132 uint8_t lengthCounter = 0;
1138 uint32_t rem = calcval % 10;
1139 backwardsBuf[length] = TO_DEC(rem);
1142 calcval = calcval - rem;
1143 calcval = calcval / 10;
1147 lengthCounter = length;
1150 for (uint8_t i = 0; i < length; i++)
1152 buf[i] = backwardsBuf[lengthCounter-1];
1180 static uint32_t charDec_to_uint32 (
char *buf)
1189 uint8_t
byte = *buf++;
1192 if ( (value <= 0xFFFFFFF) && ((
byte -
'0') <= 0xF) )
1196 value = (value * 10) + (
byte -
'0');
1275 void USART0_RX_IRQHandler(
void)
1278 static uint32_t i = 0;
1281 uint32_t flags = USART_IntGet(dbpointer);
1282 USART_IntClear(dbpointer, flags);
1285 rx_buffer[i++] = USART_Rx(dbpointer);
1288 if ( (rx_buffer[i - 1] ==
'\r') || (rx_buffer[i - 1] ==
'\f') )
1290 dataReceived =
true;
1291 rx_buffer[i - 1] =
'\0';
1298 dataReceived =
true;
1299 rx_buffer[i] =
'\0';
1315 void USART0_TX_IRQHandler(
void)
1318 static uint32_t i = 0;
1321 uint32_t flags = USART_IntGet(dbpointer);
1322 USART_IntClear(dbpointer, flags);
1325 if (flags & USART_IF_TXC)
1332 USART_Tx(dbpointer, tx_buffer[i++]);
1352 USART0_RX_IRQHandler();
1366 USART0_TX_IRQHandler();