Skip to content

Commit

Permalink
Improve debug code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonan CM committed Nov 25, 2013
1 parent c2c4620 commit e027e79
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 321 deletions.
25 changes: 25 additions & 0 deletions debug.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "debug.h"

#ifndef NDEBUG

#include <p30fxxxx.h>

void dbgmsg_uart1(char *c)
{
for (; *c != 0; ++c)
{
while (U1STAbits.UTXBF);
U1TXREG = *c;
}
}

void dbgmsg_uart2(char *c)
{
for (; *c != 0; ++c)
{
while (U2STAbits.UTXBF);
U2TXREG = *c;
}
}

#endif
18 changes: 18 additions & 0 deletions debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef DEBUG_H
#define DEBUG_H

#define NDEBUG

#ifndef NDEBUG
void dbgmsg_uart1(char *c);
#else
#define dbgmsg_uart1(x) /* Define empty macro */
#endif

#ifndef NDEBUG
void dbgmsg_uart2(char *c);
#else
#define dbgmsg_uart2(x) /* Define empty macro */
#endif

#endif
13 changes: 3 additions & 10 deletions gpcore/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ _FOSC(CSW_FSCM_OFF & XT_PLL16); // Fosc = 16x 7.37 MHz, Fcy = 29.50 MHz
_FWDT(WDT_OFF); // Turn off the watchdog timer
_FBORPOR(MCLR_EN & PWRT_OFF); // Enable reset pin and turn off the power-up timers.

#define NDEBUG

#include "hostcom.h"
#include "../types.h"
#include "../macros.h"
#include "shell.h"
#include "../mcuicom.h"
#include "mctlcom.h"

#include "../debug.h"

int main(void)
{
hostcom_setup();
Expand All @@ -23,14 +23,7 @@ int main(void)
// Code for debugging. Send a message over RS232 notifying that the UART 1
// and the UART 2 of the GPMCU are ready and working fine.
#ifndef NDEBUG
{
char *c = "UART 1 GPMCU ready\n";
for (; *c != 0; ++c)
{
while (U1STAbits.UTXBF);
U1TXREG = *c;
}
}
mcuicom_send("UART 1 GPMCU ready\n", STRLEN("UART 1 GPMCU ready\n"));
hostcom_send("UART 2 GPMCU ready\n", STRLEN("UART 2 GPMCU ready\n"));
#endif

Expand Down
35 changes: 20 additions & 15 deletions gpcore/mctlcom.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#include "mctlcom.h"

#include <string.h> // memcpy

#include "../clock.h"

#define T2PRESCALER 64 /* Timer 2 prescale value 1:64 */

bool_t mctlcom_timeout = false;

// debug
#include "../macros.h"
#include "hostcom.h"
#include "../debug.h"
#ifndef NDEBUG
#include <stdio.h>
#include <string.h>
#endif

inline void mctlcom_setup(void)
{
Expand Down Expand Up @@ -40,10 +39,11 @@ inline void mctlcom_start_timer(unsigned int timeout)
// Set Timer 2 period
PR2 = (((timeout / 1000.0) * FCY) / T2PRESCALER);

// debug
#ifndef NDEBUG
char buf[64];
snprintf(buf, 64, "PR2 = %u\n", PR2);
hostcom_send(buf, strlen(buf));
dbgmsg_uart2(buf, strlen(buf));
#endif

// Start Timer 2
T2CONbits.TON = 1;
Expand All @@ -60,8 +60,10 @@ void __attribute__((interrupt, auto_psv)) _T2Interrupt(void)
mctlcom_timeout = true;
// Clear interrupt flag
IFS0bits.T2IF = 0;
// debug
hostcom_send("_T2Interrupt\n", STRLEN("_T2Interrupt\n"));

#ifndef NDEBUG
dbgmsg_uart2("_T2Interrupt\n");
#endif
}

int mctlcom_get_response(char *response, int size, unsigned int *timeout)
Expand All @@ -72,8 +74,9 @@ int mctlcom_get_response(char *response, int size, unsigned int *timeout)
// If timeout was requested, start timer
if (*timeout != 0)
{
// debug
hostcom_send("start timer\n", STRLEN("start timer\n"));
#ifndef NDEBUG
dbgmsg_uart2("start timer\n");
#endif

mctlcom_start_timer(*timeout);
}
Expand All @@ -87,8 +90,9 @@ int mctlcom_get_response(char *response, int size, unsigned int *timeout)
mctlcom_timeout = false;
mctlcom_stop_timer();

// debug
hostcom_send("timeout occurred\n", STRLEN("timeout occurred\n"));
#ifndef NDEBUG
dbgmsg_uart2("timeout occurred\n");
#endif
}
// Otherwise, if a command has been fully received, get it and extract the response code
// in order to return it to the callee
Expand All @@ -97,8 +101,9 @@ int mctlcom_get_response(char *response, int size, unsigned int *timeout)
copied = mcuicom_read_cmd(response, size, &full);
*timeout = false;

// debug
hostcom_send("response received\n", STRLEN("response received\n"));
#ifndef NDEBUG
dbgmsg_uart2("response received\n");
#endif
}

return copied;
Expand Down
Loading

0 comments on commit e027e79

Please sign in to comment.