changed c++ comments to c comments using comment.sh script.

This commit is contained in:
skarg
2006-02-19 01:33:30 +00:00
parent dee63d45bc
commit b686fa7ca7
34 changed files with 975 additions and 1030 deletions
+11 -11
View File
@@ -115,13 +115,13 @@
#define SHIFTREG_CLK PORTCbits.RC1
#define SHIFTREG_DATA PORTCbits.RC0
#define NO_ANALOGS 0x06 // None
#define ALL_ANALOG 0x00 // RA0 RA1 RA2 RA3 RA5 RE0 RE1 RE2 Ref=Vdd
#define ANALOG_RA3_REF 0x01 // RA0 RA1 RA2 RA5 RE0 RE1 RE2 Ref=RA3
#define A_ANALOG 0x02 // RA0 RA1 RA2 RA3 RA5 Ref=Vdd
#define A_ANALOG_RA3_REF 0x03 // RA0 RA1 RA2 RA5 Ref=RA3
#define RA0_RA1_RA3_ANALOG 0x04 // RA0 RA1 RA3 Ref=Vdd
#define RA0_RA1_ANALOG_RA3_REF 0x05 // RA0 RA1 Ref=RA3
#define NO_ANALOGS 0x06 /* None */
#define ALL_ANALOG 0x00 /* RA0 RA1 RA2 RA3 RA5 RE0 RE1 RE2 Ref=Vdd */
#define ANALOG_RA3_REF 0x01 /* RA0 RA1 RA2 RA5 RE0 RE1 RE2 Ref=RA3 */
#define A_ANALOG 0x02 /* RA0 RA1 RA2 RA3 RA5 Ref=Vdd */
#define A_ANALOG_RA3_REF 0x03 /* RA0 RA1 RA2 RA5 Ref=RA3 */
#define RA0_RA1_RA3_ANALOG 0x04 /* RA0 RA1 RA3 Ref=Vdd */
#define RA0_RA1_ANALOG_RA3_REF 0x05 /* RA0 RA1 Ref=RA3 */
#define ANALOG_RA3_RA2_REF 0x08
#define ANALOG_NOT_RE1_RE2 0x09
@@ -132,7 +132,7 @@
#define RA0_ANALOG 0x0E
#define RA0_ANALOG_RA3_RA2_REF 0x0F
// Constants used for SETUP_ADC() are:
/* Constants used for SETUP_ADC() are: */
#define ADC_OFF 0
#define ADC_START 4
#define ADC_CLOCK_DIV_2 1
@@ -242,9 +242,9 @@
#define USART_TRANSMIT(x) TXREG = (x)
#define USART_RECEIVE() RCREG
#define USART_RX_FRAME_ERROR() rcstabits.FERR
// combine the sequence correctly
#define USART_RX_SETUP() PIE1bits.RCIE = 1; RCSTAbits.CREN = 1
/* combine the sequence correctly */
#define USART_RX_SETUP() PIE1bits.RCIE = 1; RCSTAbits.CREN = 1
#define USART_TX_SETUP() PIE1bits.TXIE = 1; TXSTAbits.TXEN = 1
#endif /* HARDWARE_H */
#endif /* HARDWARE_H */
+48 -49
View File
@@ -29,10 +29,10 @@
#include <stdint.h>
#include "hardware.h"
// define this to enable ICD
//#define USE_ICD
/* define this to enable ICD */
/*#define USE_ICD */
// Configuration Bits
/* Configuration Bits */
#pragma config OSC = HS
#pragma config PWRT = ON
#pragma config BOR = ON, BORV = 42
@@ -59,12 +59,12 @@
#pragma config EBTRB = OFF
#ifdef USE_ICD
#pragma config WDT = OFF, WDTPS = 128
#pragma config DEBUG = ON
#pragma config WDT = OFF, WDTPS = 128
#pragma config DEBUG = ON
#else
#pragma config WDT = ON, WDTPS = 128
#pragma config DEBUG = OFF
#endif /* USE_ICD */
#pragma config WDT = ON, WDTPS = 128
#pragma config DEBUG = OFF
#endif /* USE_ICD */
#pragma romdata
@@ -76,56 +76,55 @@
*****************************************************************************/
void init_hardware(void)
{
// If the processor gets a power on reset then we can do something.
// We should not get a reset unless there has been
// some kind of power line disturbance.
if (RCONbits.POR)
{
//do something special!
}
/* If the processor gets a power on reset then we can do something. */
/* We should not get a reset unless there has been */
/* some kind of power line disturbance. */
if (RCONbits.POR) {
/*do something special! */
}
GLOBAL_INT_DISABLE();
GLOBAL_INT_DISABLE();
/* Setup PORT A */
TRISA=PORT_A_TRIS_MASK;
/* Setup PORT A */
TRISA = PORT_A_TRIS_MASK;
/* PORT A can have analog inputs or digital IO */
ADCON1 = NO_ANALOGS;
/* PORT A can have analog inputs or digital IO */
ADCON1 = NO_ANALOGS;
/* Setup PORT B */
TRISB=PORT_B_TRIS_MASK;
/* Setup PORT B */
TRISB = PORT_B_TRIS_MASK;
/* Setup PORT C */
TRISC=PORT_C_TRIS_MASK;
/* Setup PORT C */
TRISC = PORT_C_TRIS_MASK;
/* setup zero cross interrupt to trigger on a low to high edge */
INTCON2bits.INTEDG0 = 1;
/* setup zero cross interrupt to trigger on a low to high edge */
INTCON2bits.INTEDG0 = 1;
// setup ABUS
//ABUS_LED_OFF();
//SSPADD = ABUS_DEFAULT_ADDR;
//SSPCON1 = (ABUS_SLAVE_MASK | ABUS_CLK_ENABLE | ABUS_MODE_SETUP);
//ABUS_Clear_SSPBUF();
/* setup ABUS */
/*ABUS_LED_OFF(); */
/*SSPADD = ABUS_DEFAULT_ADDR; */
/*SSPCON1 = (ABUS_SLAVE_MASK | ABUS_CLK_ENABLE | ABUS_MODE_SETUP); */
/*ABUS_Clear_SSPBUF(); */
// setup timer2 to reset every 1ms
CloseTimer2();
PR2 = 250;
OpenTimer2(T2_PS_1_4 & T2_POST_1_5 & 0x7F);
/* setup timer2 to reset every 1ms */
CloseTimer2();
PR2 = 250;
OpenTimer2(T2_PS_1_4 & T2_POST_1_5 & 0x7F);
// Setup our interrupt priorities ---------> all low priority
RCONbits.IPEN = 1;
IPR1 = 0;
IPR2 = 0;
INTCON2bits.TMR0IP = 0;
INTCON2bits.RBIP = 0;
INTCON3 = 0;
/* Setup our interrupt priorities ---------> all low priority */
RCONbits.IPEN = 1;
IPR1 = 0;
IPR2 = 0;
INTCON2bits.TMR0IP = 0;
INTCON2bits.RBIP = 0;
INTCON3 = 0;
/* Enable interrupts */
TIMER2_INT_ENABLE();
PERIPHERAL_INT_ENABLE();
GLOBAL_INT_ENABLE();
/* Enable interrupts */
TIMER2_INT_ENABLE();
PERIPHERAL_INT_ENABLE();
GLOBAL_INT_ENABLE();
// Turn on the Zero cross interrupt
INTCONbits.INT0F = 0;
INTCONbits.INT0E = 1;
/* Turn on the Zero cross interrupt */
INTCONbits.INT0F = 0;
INTCONbits.INT0E = 1;
}
+82 -98
View File
@@ -31,7 +31,7 @@
#include "hardware.h"
#include "timer.h"
// interrupt service routines
/* interrupt service routines */
extern void RS485_Receive_Interrupt(void);
extern void RS485_Transmit_Interrupt(void);
@@ -45,76 +45,65 @@ extern void RS485_Transmit_Interrupt(void);
#pragma interruptlow InterruptHandlerLow save=PROD, section(".tmpdata")
void InterruptHandlerLow(void)
{
// check for timer0 interrupt
if((INTCONbits.TMR0IF) && (INTCONbits.TMR0IE))
{
// clear interrupt flag
INTCONbits.TMR0IF = 0;
// call interrupt handler
}
//check for timer1 interrupt
if ((PIR1bits.TMR1IF) && (PIE1bits.TMR1IE))
{
PIR1bits.TMR1IF = 0;
// call interrupt handler
}
//check for timer2 interrupt
if ((PIR1bits.TMR2IF) && (PIE1bits.TMR2IE))
{
PIR1bits.TMR2IF = 0;
Timer_Millisecond_Interrupt();
}
//check for timer3 interrupt
if ((PIR2bits.TMR3IF) && (PIE2bits.TMR3IE))
{
PIR2bits.TMR3IF = 0;
// call interrupt handler
}
//check for compare 1 int
if ((PIR1bits.CCP1IF) && (PIE1bits.CCP1IE))
{
PIR1bits.CCP1IF = 0;
// call interrupt handler
}
//check for compare 2 int
if ((PIR2bits.CCP2IF) && (PIE2bits.CCP2IE))
{
PIR2bits.CCP2IF = 0;
// call interrupt handler
}
//check for USART Rx int
if ((PIR2bits.EEIF) && (PIE2bits.EEIE))
{
PIR2bits.EEIF = 0; //clear interrupt flag
EECON1bits.WREN = 0; // disable writes
// call interrupt handler
}
//check for USART Tx int
if ((PIR1bits.TXIF) && (PIE1bits.TXIE))
{
// call interrupt handler
RS485_Transmit_Interrupt();
}
//check for USART Rx int
if ((PIR1bits.RCIF) && (PIE1bits.RCIE))
{
// call interrupt handler
RS485_Receive_Interrupt();
}
//check for AD int
if ((PIR1bits.ADIF) && (PIE1bits.ADIE))
{
// call interrupt handler
PIR1bits.ADIF = 0;
}
//check for I2C receive int (MSSP int)
if ((PIR1bits.SSPIF) && (PIE1bits.SSPIE))
{
PIR1bits.SSPIF = 0;
// call interrupt handler
}
/* check for timer0 interrupt */
if ((INTCONbits.TMR0IF) && (INTCONbits.TMR0IE)) {
/* clear interrupt flag */
INTCONbits.TMR0IF = 0;
/* call interrupt handler */
}
/*check for timer1 interrupt */
if ((PIR1bits.TMR1IF) && (PIE1bits.TMR1IE)) {
PIR1bits.TMR1IF = 0;
/* call interrupt handler */
}
/*check for timer2 interrupt */
if ((PIR1bits.TMR2IF) && (PIE1bits.TMR2IE)) {
PIR1bits.TMR2IF = 0;
Timer_Millisecond_Interrupt();
}
/*check for timer3 interrupt */
if ((PIR2bits.TMR3IF) && (PIE2bits.TMR3IE)) {
PIR2bits.TMR3IF = 0;
/* call interrupt handler */
}
/*check for compare 1 int */
if ((PIR1bits.CCP1IF) && (PIE1bits.CCP1IE)) {
PIR1bits.CCP1IF = 0;
/* call interrupt handler */
}
/*check for compare 2 int */
if ((PIR2bits.CCP2IF) && (PIE2bits.CCP2IE)) {
PIR2bits.CCP2IF = 0;
/* call interrupt handler */
}
/*check for USART Rx int */
if ((PIR2bits.EEIF) && (PIE2bits.EEIE)) {
PIR2bits.EEIF = 0; /*clear interrupt flag */
EECON1bits.WREN = 0; /* disable writes */
/* call interrupt handler */
}
/*check for USART Tx int */
if ((PIR1bits.TXIF) && (PIE1bits.TXIE)) {
/* call interrupt handler */
RS485_Transmit_Interrupt();
}
/*check for USART Rx int */
if ((PIR1bits.RCIF) && (PIE1bits.RCIE)) {
/* call interrupt handler */
RS485_Receive_Interrupt();
}
/*check for AD int */
if ((PIR1bits.ADIF) && (PIE1bits.ADIE)) {
/* call interrupt handler */
PIR1bits.ADIF = 0;
}
/*check for I2C receive int (MSSP int) */
if ((PIR1bits.SSPIF) && (PIE1bits.SSPIE)) {
PIR1bits.SSPIF = 0;
/* call interrupt handler */
}
return;
return;
}
/****************************************************************************
@@ -128,26 +117,24 @@ void InterruptHandlerLow(void)
#pragma interrupt InterruptHandlerHigh
void InterruptHandlerHigh(void)
{
//check for external int
if ((INTCONbits.INT0IF) && (INTCONbits.INT0IE))
{
/* Test to ensure that we are not getting a false trigger on the
falling edge. Only trigger on Rising edge. */
if (ZERO_CROSS)
{
// timer used to determine when power is gone (no zero crosses)
// Power_Timeout = 30;
// if (ABUS_Current_Status.Zerox_Fail)
// {
// ABUS_Flags.SendStatus = TRUE;
// ABUS_Current_Status.Zerox_Fail = FALSE;
// }
// if we get here it means power is good
// System_Flags.PowerFail = FALSE;
/*check for external int */
if ((INTCONbits.INT0IF) && (INTCONbits.INT0IE)) {
/* Test to ensure that we are not getting a false trigger on the
falling edge. Only trigger on Rising edge. */
if (ZERO_CROSS) {
/* timer used to determine when power is gone (no zero crosses) */
/* Power_Timeout = 30; */
/* if (ABUS_Current_Status.Zerox_Fail) */
/* { */
/* ABUS_Flags.SendStatus = TRUE; */
/* ABUS_Current_Status.Zerox_Fail = FALSE; */
/* } */
/* if we get here it means power is good */
/* System_Flags.PowerFail = FALSE; */
}
INTCONbits.INT0IF = 0;
}
INTCONbits.INT0IF = 0;
}
return;
return;
}
/****************************************************************************
@@ -160,10 +147,9 @@ void InterruptHandlerHigh(void)
#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh(void)
{
_asm
goto InterruptHandlerHigh //jump to interrupt routine
_endasm
}
_asm goto InterruptHandlerHigh /*jump to interrupt routine */
_endasm}
#pragma code
/****************************************************************************
@@ -176,9 +162,7 @@ void InterruptVectorHigh(void)
#pragma code InterruptVectorLow = 0x18
void InterruptVectorLow(void)
{
_asm
goto InterruptHandlerLow //jump to interrupt routine
_endasm
}
#pragma code
_asm goto InterruptHandlerLow /*jump to interrupt routine */
_endasm}
#pragma code
+26 -28
View File
@@ -34,8 +34,8 @@
#include "timer.h"
#include "hardware.h"
volatile struct mstp_port_struct_t MSTP_Port; // port data
static uint8_t MSTP_MAC_Address = 0x05; // local MAC address
volatile struct mstp_port_struct_t MSTP_Port; /* port data */
static uint8_t MSTP_MAC_Address = 0x05; /* local MAC address */
/****************************************************************************
* DESCRIPTION: Handles our calling our module level milisecond counters
@@ -46,40 +46,38 @@ static uint8_t MSTP_MAC_Address = 0x05; // local MAC address
*****************************************************************************/
static void Check_Timer_Milliseconds(void)
{
// We might have missed some so keep doing it until we have got them all
while (Milliseconds)
{
MSTP_Millisecond_Timer(&MSTP_Port);
Milliseconds--;
}
/* We might have missed some so keep doing it until we have got them all */
while (Milliseconds) {
MSTP_Millisecond_Timer(&MSTP_Port);
Milliseconds--;
}
}
void main(void)
{
init_hardware();
RS485_Initialize();
MSTP_Init(&MSTP_Port,MSTP_MAC_Address);
init_hardware();
RS485_Initialize();
MSTP_Init(&MSTP_Port, MSTP_MAC_Address);
// loop forever
for (;;)
{
WATCHDOG_TIMER();
/* loop forever */
for (;;) {
WATCHDOG_TIMER();
// input
Check_Timer_Milliseconds();
// note: also called by RS-485 Receive ISR
RS485_Check_UART_Data(&MSTP_Port);
MSTP_Receive_Frame_FSM(&MSTP_Port);
/* input */
Check_Timer_Milliseconds();
/* note: also called by RS-485 Receive ISR */
RS485_Check_UART_Data(&MSTP_Port);
MSTP_Receive_Frame_FSM(&MSTP_Port);
// process
/* process */
// output
RS485_Process_Tx_Message();
MSTP_Master_Node_FSM(&MSTP_Port);
}
return;
/* output */
RS485_Process_Tx_Message();
MSTP_Master_Node_FSM(&MSTP_Port);
}
return;
}
+222 -243
View File
@@ -23,63 +23,60 @@
*
*********************************************************************/
// The module handles sending data out the RS-485 port
// and handles receiving data from the RS-485 port.
// Customize this file for your specific hardware
/* The module handles sending data out the RS-485 port */
/* and handles receiving data from the RS-485 port. */
/* Customize this file for your specific hardware */
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include "hardware.h"
#include "mstp.h"
// public port info
/* public port info */
extern volatile struct mstp_port_struct_t MSTP_Port;
static uint32_t RS485_Baud_Rate = 9600;
// UART transmission buffer and index
/* UART transmission buffer and index */
static volatile uint8_t RS485_Tx_Buffer[MAX_MPDU];
static volatile uint8_t RS485_Tx_Index = 0;
static volatile uint8_t RS485_Tx_Length = 0;
static volatile char RS485_Tx_Postdrive_Delay = 0;
static struct
{
unsigned TransmitStart:1; // TRUE if we are requested to transmit
unsigned TransmitComplete:1; // TRUE if we are finished transmitting frame
static struct {
unsigned TransmitStart:1; /* TRUE if we are requested to transmit */
unsigned TransmitComplete:1; /* TRUE if we are finished transmitting frame */
} RS485_Flags;
// Duplicate of the RCSTA reg used due to the double buffering of the
// fifo. Reading the RCREG reg will cause the second RCSTA reg to be
// loaded if there is one. */
struct _rcstabits
{
unsigned char RX9D:1;
unsigned char OERR:1;
unsigned char FERR:1;
unsigned char ADDEN:1;
unsigned char CREN:1;
unsigned char SREN:1;
unsigned char RX9:1;
unsigned char SPEN:1;
/* Duplicate of the RCSTA reg used due to the double buffering of the */
/* fifo. Reading the RCREG reg will cause the second RCSTA reg to be */
/* loaded if there is one. */ */
struct _rcstabits {
unsigned char RX9D:1;
unsigned char OERR:1;
unsigned char FERR:1;
unsigned char ADDEN:1;
unsigned char CREN:1;
unsigned char SREN:1;
unsigned char RX9:1;
unsigned char SPEN:1;
};
volatile static enum
{
RS485_STATE_IDLE = 0,
RS485_STATE_RX_DATA = 1,
RS485_STATE_RX_CHECKSUM = 2,
RS485_STATE_RX_PROCESS = 3,
RS485_STATE_TX_DATA = 4,
RS485_STATE_WAIT_FOR_ACK = 5,
RS485_STATE_WAIT_COMPLETE = 6,
RS485_STATE_TX_GLOBAL_ACK = 7,
RS485_STATE_TX_POSTDRIVE_DELAY = 8,
RS485_STATE_ERROR = 9,
RS485_STATE_RX_TEST = 10,
RS485_STATE_RX_TEST_EEPROM = 11,
RS485_STATE_RX_TEST_DELAY = 12,
RS485_STATE_TX_TEST_WAIT = 13,
RS485_STATE_TX_TEST = 14
volatile static enum {
RS485_STATE_IDLE = 0,
RS485_STATE_RX_DATA = 1,
RS485_STATE_RX_CHECKSUM = 2,
RS485_STATE_RX_PROCESS = 3,
RS485_STATE_TX_DATA = 4,
RS485_STATE_WAIT_FOR_ACK = 5,
RS485_STATE_WAIT_COMPLETE = 6,
RS485_STATE_TX_GLOBAL_ACK = 7,
RS485_STATE_TX_POSTDRIVE_DELAY = 8,
RS485_STATE_ERROR = 9,
RS485_STATE_RX_TEST = 10,
RS485_STATE_RX_TEST_EEPROM = 11,
RS485_STATE_RX_TEST_DELAY = 12,
RS485_STATE_TX_TEST_WAIT = 13,
RS485_STATE_TX_TEST = 14
} RS485_State;
/****************************************************************************
@@ -88,45 +85,42 @@ volatile static enum
* ALGORITHM: none
* NOTES: none
*****************************************************************************/
void RS485_Send_Frame(
volatile struct mstp_port_struct_t *mstp_port, // port specific data
uint8_t *buffer, // frame to send (up to 501 bytes of data)
uint16_t nbytes) // number of bytes of data (up to 501)
{
// do we check for tx buffer in-use?
// Or do we just stop the in-progress transmission?
// Drop any transmission in progress, but don't worry about
// cleaning up the hardware - the start routine will handle that
void RS485_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, /* port specific data */
uint8_t * buffer, /* frame to send (up to 501 bytes of data) */
uint16_t nbytes)
{ /* number of bytes of data (up to 501) */
/* do we check for tx buffer in-use? */
/* Or do we just stop the in-progress transmission? */
/* Drop any transmission in progress, but don't worry about */
/* cleaning up the hardware - the start routine will handle that */
// Disable the interrupt since it depends on the global transmit buffer.
USART_TX_INT_DISABLE();
switch (RS485_State)
{
/* Disable the interrupt since it depends on the global transmit buffer. */
USART_TX_INT_DISABLE();
switch (RS485_State) {
case RS485_STATE_TX_DATA:
case RS485_STATE_WAIT_FOR_ACK:
case RS485_STATE_WAIT_COMPLETE:
case RS485_STATE_TX_GLOBAL_ACK:
RS485_State = RS485_STATE_IDLE;
break;
}
// load the frame
RS485_Tx_Length = 0;
while (buffer && nbytes)
{
RS485_Tx_Buffer[RS485_Tx_Length] = *buffer;
buffer++;
nbytes--;
RS485_Tx_Length++;
// check bounds - should this error be indicated somehow?
// perhaps not send the message?
if (RS485_Tx_Length >= MAX_MPDU)
break;
}
// signal the task to start sending when it is ready
RS485_Flags.TransmitStart = TRUE;
RS485_State = RS485_STATE_IDLE;
break;
}
return;
/* load the frame */
RS485_Tx_Length = 0;
while (buffer && nbytes) {
RS485_Tx_Buffer[RS485_Tx_Length] = *buffer;
buffer++;
nbytes--;
RS485_Tx_Length++;
/* check bounds - should this error be indicated somehow? */
/* perhaps not send the message? */
if (RS485_Tx_Length >= MAX_MPDU)
break;
}
/* signal the task to start sending when it is ready */
RS485_Flags.TransmitStart = TRUE;
return;
}
/****************************************************************************
@@ -137,55 +131,49 @@ void RS485_Send_Frame(
*****************************************************************************/
void RS485_Transmit_Interrupt(void)
{
uint8_t data; // data byte to send
switch (RS485_State)
{
uint8_t data; /* data byte to send */
switch (RS485_State) {
case RS485_STATE_TX_DATA:
RS485_Tx_Index++;
if (RS485_Tx_Index < RS485_Tx_Length)
{
data = RS485_Tx_Buffer[RS485_Tx_Index];
USART_TRANSMIT(data);
MSTP_Port.SilenceTimer = 0;
}
else
{
// wait until the last bit is sent
while (!USART_TX_EMPTY());
RS485_TRANSMIT_DISABLE();
// wait 2 characters after sending (min=15 bit times)
RS485_Tx_Postdrive_Delay = 2;
RS485_State = RS485_STATE_TX_POSTDRIVE_DELAY;
USART_TRANSMIT(0);
}
break;
RS485_Tx_Index++;
if (RS485_Tx_Index < RS485_Tx_Length) {
data = RS485_Tx_Buffer[RS485_Tx_Index];
USART_TRANSMIT(data);
MSTP_Port.SilenceTimer = 0;
} else {
/* wait until the last bit is sent */
while (!USART_TX_EMPTY());
RS485_TRANSMIT_DISABLE();
/* wait 2 characters after sending (min=15 bit times) */
RS485_Tx_Postdrive_Delay = 2;
RS485_State = RS485_STATE_TX_POSTDRIVE_DELAY;
USART_TRANSMIT(0);
}
break;
case RS485_STATE_TX_POSTDRIVE_DELAY:
// after the message is sent, we wait a certain
// number of character times to get a delay
if (RS485_Tx_Postdrive_Delay)
{
RS485_Tx_Postdrive_Delay--;
if (RS485_Tx_Postdrive_Delay == 0)
RS485_State = RS485_STATE_WAIT_COMPLETE;
USART_TRANSMIT(0);
}
else
RS485_State = RS485_STATE_WAIT_COMPLETE;
break;
/* after the message is sent, we wait a certain */
/* number of character times to get a delay */
if (RS485_Tx_Postdrive_Delay) {
RS485_Tx_Postdrive_Delay--;
if (RS485_Tx_Postdrive_Delay == 0)
RS485_State = RS485_STATE_WAIT_COMPLETE;
USART_TRANSMIT(0);
} else
RS485_State = RS485_STATE_WAIT_COMPLETE;
break;
case RS485_STATE_WAIT_COMPLETE:
// wait until the last delay bit is shifted
while (!USART_TX_EMPTY());
USART_TX_INT_DISABLE();
RS485_Flags.TransmitComplete = TRUE;
RS485_State = RS485_STATE_IDLE;
USART_RX_SETUP();
break;
/* wait until the last delay bit is shifted */
while (!USART_TX_EMPTY());
USART_TX_INT_DISABLE();
RS485_Flags.TransmitComplete = TRUE;
RS485_State = RS485_STATE_IDLE;
USART_RX_SETUP();
break;
default:
break;
}
return;
break;
}
return;
}
/****************************************************************************
@@ -196,28 +184,27 @@ void RS485_Transmit_Interrupt(void)
*****************************************************************************/
void RS485_Process_Tx_Message(void)
{
if (RS485_Flags.TransmitComplete)
RS485_Flags.TransmitComplete = FALSE;
// start a new transmisstion if we are ready
if (RS485_Flags.TransmitStart && (RS485_State == RS485_STATE_IDLE))
{
// Disable the receiver
USART_RX_INT_DISABLE();
USART_CONTINUOUS_RX_DISABLE();
// Enable the transmit line driver and interrupts
RS485_TRANSMIT_ENABLE();
RS485_State = RS485_STATE_TX_DATA;
// Configure the ISR handler for an outgoing message
RS485_Tx_Index = 0;
// update the flags for beginning a send
RS485_Flags.TransmitComplete = FALSE;
RS485_Flags.TransmitStart = FALSE;
// send the first byte
USART_TRANSMIT(RS485_Tx_Buffer[0]);
USART_TX_SETUP();
}
return;
if (RS485_Flags.TransmitComplete)
RS485_Flags.TransmitComplete = FALSE;
/* start a new transmisstion if we are ready */
if (RS485_Flags.TransmitStart && (RS485_State == RS485_STATE_IDLE)) {
/* Disable the receiver */
USART_RX_INT_DISABLE();
USART_CONTINUOUS_RX_DISABLE();
/* Enable the transmit line driver and interrupts */
RS485_TRANSMIT_ENABLE();
RS485_State = RS485_STATE_TX_DATA;
/* Configure the ISR handler for an outgoing message */
RS485_Tx_Index = 0;
/* update the flags for beginning a send */
RS485_Flags.TransmitComplete = FALSE;
RS485_Flags.TransmitStart = FALSE;
/* send the first byte */
USART_TRANSMIT(RS485_Tx_Buffer[0]);
USART_TX_SETUP();
}
return;
}
/****************************************************************************
@@ -228,40 +215,36 @@ void RS485_Process_Tx_Message(void)
*****************************************************************************/
void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port)
{
struct _rcstabits rcstabits; // reading it more than once gets wrong data
struct _rcstabits rcstabits; /* reading it more than once gets wrong data */
// check for data
if (USART_RX_COMPLETE())
{
// Read the data and the Rx status reg
rcstabits = USART_RX_STATUS();
mstp_port->DataRegister = USART_RECEIVE();
/* check for data */
if (USART_RX_COMPLETE()) {
/* Read the data and the Rx status reg */
rcstabits = USART_RX_STATUS();
mstp_port->DataRegister = USART_RECEIVE();
// Check for buffer overrun error
if (rcstabits.OERR)
{
// clear the error
USART_CONTINUOUS_RX_DISABLE();
USART_CONTINUOUS_RX_ENABLE();
// let the state machine know
mstp_port->ReceiveError = TRUE;
/* Check for buffer overrun error */
if (rcstabits.OERR) {
/* clear the error */
USART_CONTINUOUS_RX_DISABLE();
USART_CONTINUOUS_RX_ENABLE();
/* let the state machine know */
mstp_port->ReceiveError = TRUE;
}
/* Check for framing errors */
else if (USART_RX_FRAME_ERROR()) {
/* let the state machine know */
mstp_port->FramingError = TRUE;
mstp_port->ReceiveError = TRUE;
}
/* We read a good byte */
else {
/* state machine will clear this */
mstp_port->DataAvailable = TRUE;
}
}
// Check for framing errors
else if (USART_RX_FRAME_ERROR())
{
// let the state machine know
mstp_port->FramingError = TRUE;
mstp_port->ReceiveError = TRUE;
}
// We read a good byte
else
{
// state machine will clear this
mstp_port->DataAvailable = TRUE;
}
}
return;
return;
}
/****************************************************************************
@@ -272,17 +255,16 @@ void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port)
*****************************************************************************/
void RS485_Receive_Interrupt(void)
{
// get as many bytes as we can get
for (;;)
{
RS485_Check_UART_Data(&MSTP_Port);
if (MSTP_Port.ReceiveError || MSTP_Port.DataAvailable)
MSTP_Receive_Frame_FSM(&MSTP_Port);
else
break;
}
/* get as many bytes as we can get */
for (;;) {
RS485_Check_UART_Data(&MSTP_Port);
if (MSTP_Port.ReceiveError || MSTP_Port.DataAvailable)
MSTP_Receive_Frame_FSM(&MSTP_Port);
else
break;
}
return;
return;
}
/****************************************************************************
@@ -293,7 +275,7 @@ void RS485_Receive_Interrupt(void)
*****************************************************************************/
uint32_t RS485_Get_Baud_Rate(void)
{
return RS485_Baud_Rate;
return RS485_Baud_Rate;
}
/****************************************************************************
@@ -304,70 +286,69 @@ uint32_t RS485_Get_Baud_Rate(void)
*****************************************************************************/
void RS485_Set_Baud_Rate(uint32_t baud)
{
if (baud < 19200)
RS485_Baud_Rate = 9600;
else if (baud < 38400)
RS485_Baud_Rate = 19200;
else if (baud < 57600)
RS485_Baud_Rate = 38400;
else if (baud < 57600)
RS485_Baud_Rate = 57600;
else if (baud < 115200)
RS485_Baud_Rate = 76800;
else
RS485_Baud_Rate = 115200;
if (baud < 19200)
RS485_Baud_Rate = 9600;
else if (baud < 38400)
RS485_Baud_Rate = 19200;
else if (baud < 57600)
RS485_Baud_Rate = 38400;
else if (baud < 57600)
RS485_Baud_Rate = 57600;
else if (baud < 115200)
RS485_Baud_Rate = 76800;
else
RS485_Baud_Rate = 115200;
}
void RS485_Initialize_Baud(void)
{
// setup USART Baud Rate Generator
// see BAUD RATES FOR ASYNCHRONOUS MODE in Data Book
/* Fosc=20MHz
BRGH=1 BRGH=0
Rate SPBRG Rate SPBRG
------- ----- ------- -----
9615 129 9469 32
/* setup USART Baud Rate Generator */
/* see BAUD RATES FOR ASYNCHRONOUS MODE in Data Book */
/* Fosc=20MHz
BRGH=1 BRGH=0
Rate SPBRG Rate SPBRG
------- ----- ------- -----
9615 129 9469 32
19230 64 19530 15
37878 32 78130 3
56818 21 104200 2
113630 10 312500 0
250000 4
625000 1
1250000 0
*/
switch (RS485_Baud_Rate)
{
113630 10 312500 0
250000 4
625000 1
1250000 0
*/
switch (RS485_Baud_Rate) {
case 19200:
SPBRG = 64;
TXSTAbits.BRGH=1;
break;
SPBRG = 64;
TXSTAbits.BRGH = 1;
break;
case 38400:
SPBRG = 32;
TXSTAbits.BRGH=1;
break;
SPBRG = 32;
TXSTAbits.BRGH = 1;
break;
case 57600:
SPBRG = 21;
TXSTAbits.BRGH=1;
break;
SPBRG = 21;
TXSTAbits.BRGH = 1;
break;
case 76800:
SPBRG = 3;
TXSTAbits.BRGH=0;
break;
SPBRG = 3;
TXSTAbits.BRGH = 0;
break;
case 115200:
SPBRG = 10;
TXSTAbits.BRGH=1;
break;
SPBRG = 10;
TXSTAbits.BRGH = 1;
break;
case 9600:
default:
SPBRG = 129;
TXSTAbits.BRGH=1;
break;
}
/* select async mode */
TXSTAbits.SYNC=0;
/* serial port enable */
RCSTAbits.SPEN=1;
SPBRG = 129;
TXSTAbits.BRGH = 1;
break;
}
/* select async mode */
TXSTAbits.SYNC = 0;
/* serial port enable */
RCSTAbits.SPEN = 1;
}
/****************************************************************************
@@ -379,17 +360,15 @@ void RS485_Initialize_Baud(void)
*****************************************************************************/
void RS485_Initialize(void)
{
RS485_Initialize_Baud();
/* configure interrupts */
USART_TX_INT_DISABLE();
USART_RX_INT_ENABLE();
// configure USART for receiving
// since the TX will handle setting up for transmit
USART_CONTINUOUS_RX_ENABLE();
/* since we are using RS485,
we need to explicitly say
transmit enable or not */
RS485_TRANSMIT_DISABLE();
RS485_Initialize_Baud();
/* configure interrupts */
USART_TX_INT_DISABLE();
USART_RX_INT_ENABLE();
/* configure USART for receiving */
/* since the TX will handle setting up for transmit */
USART_CONTINUOUS_RX_ENABLE();
/* since we are using RS485,
we need to explicitly say
transmit enable or not */
RS485_TRANSMIT_DISABLE();
}
+14 -14
View File
@@ -1,28 +1,28 @@
#ifndef STDBOOL_H
#define STDBOOL_H
// C99 Boolean types for compilers without C99 support
/* C99 Boolean types for compilers without C99 support */
#ifndef __cplusplus
typedef int _Bool;
#ifndef bool
#define bool _Bool
#endif
#ifndef true
#define true 1
#endif
#ifndef false
#define false 0
#endif
#define __bool_true_false_are_defined 1
typedef int _Bool;
#ifndef bool
#define bool _Bool
#endif
#ifndef true
#define true 1
#endif
#ifndef false
#define false 0
#endif
#define __bool_true_false_are_defined 1
#endif
#ifndef FALSE
#define FALSE 0
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#define TRUE 1
#endif
#endif
+11 -11
View File
@@ -1,18 +1,18 @@
// Defines the standard integer types that are used in code
/* Defines the standard integer types that are used in code */
#ifndef STDINT_H
#define STDINT_H 1
#include <stddef.h>
typedef unsigned char uint8_t; // 1 byte 0 to 255
typedef signed char int8_t; // 1 byte -127 to 127
typedef unsigned short uint16_t; // 2 bytes 0 to 65535
typedef signed short int16_t; // 2 bytes -32767 to 32767
//typedef unsigned short long uint24_t; // 3 bytes 0 to 16777215
typedef unsigned long uint32_t; // 4 bytes 0 to 4294967295
typedef signed long int32_t; // 4 bytes -2147483647 to 2147483647
// typedef signed long long int64_t;
// typedef unsigned long long uint64_t;
typedef unsigned char uint8_t; /* 1 byte 0 to 255 */
typedef signed char int8_t; /* 1 byte -127 to 127 */
typedef unsigned short uint16_t; /* 2 bytes 0 to 65535 */
typedef signed short int16_t; /* 2 bytes -32767 to 32767 */
/*typedef unsigned short long uint24_t; // 3 bytes 0 to 16777215 */
typedef unsigned long uint32_t; /* 4 bytes 0 to 4294967295 */
typedef signed long int32_t; /* 4 bytes -2147483647 to 2147483647 */
/* typedef signed long long int64_t; */
/* typedef unsigned long long uint64_t; */
#endif // STDINT_H
#endif /* STDINT_H */
+5 -5
View File
@@ -25,7 +25,7 @@
*********************************************************************/
#include <stdint.h>
volatile uint8_t Milliseconds; // used for timing stuff - counts up to 0xFF.
volatile uint8_t Milliseconds; /* used for timing stuff - counts up to 0xFF. */
/****************************************************************************
* DESCRIPTION: Timer is set to go off every 1ms. We increment the counter,
@@ -37,9 +37,9 @@ volatile uint8_t Milliseconds; // used for timing stuff - counts up to 0xFF.
*****************************************************************************/
void Timer_Millisecond_Interrupt(void)
{
// Global Milisecond timer
if (Milliseconds < 0xFF)
Milliseconds++;
/* Global Milisecond timer */
if (Milliseconds < 0xFF)
Milliseconds++;
return;
return;
}
+1 -1
View File
@@ -26,7 +26,7 @@
#ifndef TIMER_H
#define TIMER_H
// used for timing stuff - counts up to 0xFF.
/* used for timing stuff - counts up to 0xFF. */
extern volatile uint8_t Milliseconds;
void Timer_Millisecond_Interrupt(void);