Skip to content

Commit

Permalink
Re-enable 'mctlcom' timer with some fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonan CM committed Nov 22, 2013
1 parent a6cd5f8 commit c2c4620
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
2 changes: 2 additions & 0 deletions gpcore/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ _FBORPOR(MCLR_EN & PWRT_OFF); // Enable reset pin and turn off the power-up ti
#include "../macros.h"
#include "shell.h"
#include "../mcuicom.h"
#include "mctlcom.h"

int main(void)
{
hostcom_setup();
mcuicom_setup();
mctlcom_setup();

// 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.
Expand Down
50 changes: 30 additions & 20 deletions gpcore/mctlcom.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@

#include "../clock.h"

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

bool_t mctlcom_timeout = false;

// debug
#include "../macros.h"
#include "hostcom.h"
#include <stdio.h>

/*
void mctlcom_setup(void)
inline void mctlcom_setup(void)
{
*/
/*****************************************************************
* Set up Timer 2 to be used for communication timeout *
*****************************************************************/
/*
// Clear the Timer 2 interrupt flag

IFS0bits.T2IF = 0;
Expand All @@ -30,23 +28,28 @@ void mctlcom_setup(void)

// Set Timer 2 prescaler (0=1:1, 1=1:8, 2=1:64, 3=1:256)

T2CONbits.TCKPS = 1;
T2CONbits.TCKPS = 2;

// Set Timer 2 interrupt priority to 1 (default: 2)

IPC1bits.T2IP = 1;
//IPC1bits.T2IP = 1;
}

void mctlcom_start_timer(int timeout)
inline void mctlcom_start_timer(unsigned int timeout)
{
// Set Timer 2 period
PR2 = ((timeout * FCY) / T2PRESCALER);
PR2 = (((timeout / 1000.0) * FCY) / T2PRESCALER);

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

// Start Timer 2
T2CONbits.TON = 1;
}

void mctlcom_stop_timer(void)
inline void mctlcom_stop_timer(void)
{
// Stop Timer 2
T2CONbits.TON = 0;
Expand All @@ -57,39 +60,46 @@ void __attribute__((interrupt, auto_psv)) _T2Interrupt(void)
mctlcom_timeout = true;
// Clear interrupt flag
IFS0bits.T2IF = 0;
// debug
hostcom_send("_T2Interrupt\n", STRLEN("_T2Interrupt\n"));
}
*/

int mctlcom_get_response(char *response, int size, unsigned int *timeout)
{
const int cmd_buf_size = 128;
char cmd[cmd_buf_size];
bool_t full;
int copied, retcode = 0;
int copied = 0;

// If timeout was requested, start timer
/*
if (*timeout != 0)
{
// debug
hostcom_send("start timer\n", STRLEN("start timer\n"));

mctlcom_start_timer(*timeout);
*/
}

// Wait until the receive buffer has at least one full command or a timeout occurs
while (!mcuicom_cmd_available() && !mctlcom_timeout);
copied = mcuicom_read_cmd(response, size, &full);

// If a timeout occurred, reset the timeout flag and stop the timer
if (mctlcom_timeout)
{
mctlcom_timeout = false;
//mctlcom_stop_timer();
mctlcom_stop_timer();

// debug
hostcom_send("timeout occurred\n", STRLEN("timeout occurred\n"));
}
// Otherwise, if a command has been fully received, get it and extract the response code
// in order to return it to the callee
else
{
memcpy(response, cmd, copied);
copied = mcuicom_read_cmd(response, size, &full);
*timeout = false;

// debug
hostcom_send("response received\n", STRLEN("response received\n"));
}

return retcode;
return copied;
}
6 changes: 3 additions & 3 deletions gpcore/mctlcom.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
#include "../mcuicom.h"

/**
* mctlcom timeout in seconds
* mctlcom timeout in milliseconds
* Default value: 0.100 seconds (100 ms)
* Recommended range (at 29.50 MHz with 1:64 prescaler): 1 ms ~ 100 ms
*/
#define MCTLCOM_TIMEOUT 0.100
#define MCTLCOM_TIMEOUT 100

//void mctlcom_setup(void);
inline void mctlcom_setup(void);
int mctlcom_get_response(char *response, int size, unsigned int *timeout);

#endif
3 changes: 2 additions & 1 deletion gpcore/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -1755,7 +1755,8 @@ inline void hostcmd_pa(void)
mcuicom_send(buf, 3);
// Get response (motor steps) and re-send it to the host PC
size = mctlcom_get_response(buf, size, &timeout);
hostcom_send(buf, strlen(buf));
if (!timeout)
hostcom_send(buf, size);
}
else
{
Expand Down

0 comments on commit c2c4620

Please sign in to comment.