Converted to standard indent style.

This commit is contained in:
skarg
2006-10-07 14:13:34 +00:00
parent 19c5a20205
commit 5dd8c6da91
37 changed files with 873 additions and 896 deletions
+191 -202
View File
@@ -42,7 +42,7 @@ extern volatile struct mstp_port_struct_t MSTP_Port;
uint32_t RS485_Baud_Rate = 9600;
/* the ISR and other use this for status and control */
COMSTAT RS485_Comstat;
COMSTAT RS485_Comstat;
/*#pragma udata MSTPPortData */
/* the buffer for receiving characters */
@@ -59,53 +59,55 @@ volatile uint8_t RS485_Tx_Buffer[MAX_MPDU];
*****************************************************************************/
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) */
{
uint16_t i = 0; /* loop counter */
uint8_t turnaround_time;
uint16_t nbytes)
{ /* number of bytes of data (up to 501) */
uint16_t i = 0; /* loop counter */
uint8_t turnaround_time;
if (!buffer)
return;
/* bounds check */
if (nbytes >= sizeof(RS485_Tx_Buffer))
return;
/* buffer is full. Wait for ISR to transmit. */
while (RS485_Comstat.Tx_Bytes) {
};
/* wait 40 bit times since reception */
if (RS485_Baud_Rate == 9600)
turnaround_time = 4;
else if (RS485_Baud_Rate == 19200)
turnaround_time = 2;
else
turnaround_time = 1;
while (mstp_port->SilenceTimer < turnaround_time) {
};
RS485_Comstat.TxHead = 0;
memcpy((void *) &RS485_Tx_Buffer[0], (void *) buffer, nbytes);
/*for (i = 0; i < nbytes; i++) { */
/* /* put the data into the buffer */ */
/* RS485_Tx_Buffer[i] = *buffer; */
/* buffer++; */
/*} */
RS485_Comstat.Tx_Bytes = nbytes;
/* disable the receiver */
PIE3bits.RC2IE = 0;
RCSTA2bits.CREN = 0;
/* enable the transceiver */
RS485_TX_ENABLE = 1;
RS485_RX_DISABLE = 1;
/* enable the transmitter */
TXSTA2bits.TXEN = 1;
PIE3bits.TX2IE = 1;
/* per MSTP spec, sort of */
mstp_port->SilenceTimer = 0;
if (!buffer)
return;
/* bounds check */
if (nbytes >= sizeof(RS485_Tx_Buffer))
return;
/* buffer is full. Wait for ISR to transmit. */
while (RS485_Comstat.Tx_Bytes) {};
/* wait 40 bit times since reception */
if (RS485_Baud_Rate == 9600)
turnaround_time = 4;
else if (RS485_Baud_Rate == 19200)
turnaround_time = 2;
else
turnaround_time = 1;
while (mstp_port->SilenceTimer < turnaround_time) {};
RS485_Comstat.TxHead = 0;
memcpy((void *)&RS485_Tx_Buffer[0], (void *)buffer, nbytes);
/*for (i = 0; i < nbytes; i++) { */
/* /* put the data into the buffer */ */
/* RS485_Tx_Buffer[i] = *buffer; */
/* buffer++; */
/*} */
RS485_Comstat.Tx_Bytes = nbytes;
/* disable the receiver */
PIE3bits.RC2IE = 0;
RCSTA2bits.CREN = 0;
/* enable the transceiver */
RS485_TX_ENABLE = 1;
RS485_RX_DISABLE = 1;
/* enable the transmitter */
TXSTA2bits.TXEN = 1;
PIE3bits.TX2IE = 1;
/* per MSTP spec, sort of */
mstp_port->SilenceTimer = 0;
return;
}
/****************************************************************************
@@ -114,30 +116,30 @@ void RS485_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, /* port
* ALGORITHM: none
* NOTES: none
*****************************************************************************/
uint8_t RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port)
uint8_t RS485_Check_UART_Data(volatile struct mstp_port_struct_t *
mstp_port)
{
/* check for data */
if (RS485_Comstat.Rx_Bytes)
{
mstp_port->DataRegister = RS485_Rx_Buffer[RS485_Comstat.RxTail];
if (RS485_Comstat.RxTail >= (sizeof(RS485_Rx_Buffer)-1))
RS485_Comstat.RxTail = 0;
else
RS485_Comstat.RxTail++;
RS485_Comstat.Rx_Bytes--;
/* errors? let the state machine know */
if (RS485_Comstat.Rx_Bufferoverrun)
{
RS485_Comstat.Rx_Bufferoverrun = FALSE;
mstp_port->ReceiveError = TRUE;
}
/* We read a good byte */
else
mstp_port->DataAvailable = TRUE;
if (RS485_Comstat.Rx_Bytes) {
mstp_port->DataRegister = RS485_Rx_Buffer[RS485_Comstat.RxTail];
if (RS485_Comstat.RxTail >= (sizeof(RS485_Rx_Buffer) - 1))
RS485_Comstat.RxTail = 0;
else
RS485_Comstat.RxTail++;
RS485_Comstat.Rx_Bytes--;
/* errors? let the state machine know */
if (RS485_Comstat.Rx_Bufferoverrun) {
RS485_Comstat.Rx_Bufferoverrun = FALSE;
mstp_port->ReceiveError = TRUE;
}
/* We read a good byte */
else
mstp_port->DataAvailable = TRUE;
}
return RS485_Comstat.Rx_Bytes;
return RS485_Comstat.Rx_Bytes;
}
/* *************************************************************************
DESCRIPTION: Receives RS485 data stream
@@ -149,31 +151,26 @@ uint8_t RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port)
*************************************************************************** */
void RS485_Interrupt_Rx(void)
{
char dummy;
char dummy;
if ((RCSTA2bits.FERR) || (RCSTA2bits.OERR))
{
/* Clear the error */
RCSTA2bits.CREN = 0;
RCSTA2bits.CREN = 1;
RS485_Comstat.Rx_Bufferoverrun = TRUE;
dummy = RCREG2;
}
else if (RS485_Comstat.Rx_Bytes < sizeof(RS485_Rx_Buffer))
{
RS485_Rx_Buffer[RS485_Comstat.RxHead] = RCREG2;
if (RS485_Comstat.RxHead >= (sizeof(RS485_Rx_Buffer)-1))
RS485_Comstat.RxHead = 0;
else
RS485_Comstat.RxHead++;
RS485_Comstat.Rx_Bytes++;
}
else
{
RS485_Comstat.Rx_Bufferoverrun = TRUE;
dummy = RCREG2;
(void)dummy;
}
if ((RCSTA2bits.FERR) || (RCSTA2bits.OERR)) {
/* Clear the error */
RCSTA2bits.CREN = 0;
RCSTA2bits.CREN = 1;
RS485_Comstat.Rx_Bufferoverrun = TRUE;
dummy = RCREG2;
} else if (RS485_Comstat.Rx_Bytes < sizeof(RS485_Rx_Buffer)) {
RS485_Rx_Buffer[RS485_Comstat.RxHead] = RCREG2;
if (RS485_Comstat.RxHead >= (sizeof(RS485_Rx_Buffer) - 1))
RS485_Comstat.RxHead = 0;
else
RS485_Comstat.RxHead++;
RS485_Comstat.Rx_Bytes++;
} else {
RS485_Comstat.Rx_Bufferoverrun = TRUE;
dummy = RCREG2;
(void) dummy;
}
}
/* *************************************************************************
@@ -187,28 +184,25 @@ void RS485_Interrupt_Rx(void)
*************************************************************************** */
void RS485_Interrupt_Tx(void)
{
if (RS485_Comstat.Tx_Bytes)
{
/* Get the data byte */
TXREG2 = RS485_Tx_Buffer[RS485_Comstat.TxHead];
/* point to the next byte */
RS485_Comstat.TxHead++;
/* reduce the buffer size */
RS485_Comstat.Tx_Bytes--;
}
else
{
/* wait for the USART to be empty */
while (!TXSTA2bits.TRMT);
/* disable this interrupt */
PIE3bits.TX2IE = 0;
/* enable the receiver */
RS485_TX_ENABLE = 0;
RS485_RX_DISABLE = 0;
/* FIXME: might not be necessary */
PIE3bits.RC2IE = 1;
RCSTA2bits.CREN = 1;
}
if (RS485_Comstat.Tx_Bytes) {
/* Get the data byte */
TXREG2 = RS485_Tx_Buffer[RS485_Comstat.TxHead];
/* point to the next byte */
RS485_Comstat.TxHead++;
/* reduce the buffer size */
RS485_Comstat.Tx_Bytes--;
} else {
/* wait for the USART to be empty */
while (!TXSTA2bits.TRMT);
/* disable this interrupt */
PIE3bits.TX2IE = 0;
/* enable the receiver */
RS485_TX_ENABLE = 0;
RS485_RX_DISABLE = 0;
/* FIXME: might not be necessary */
PIE3bits.RC2IE = 1;
RCSTA2bits.CREN = 1;
}
}
/****************************************************************************
@@ -243,11 +237,9 @@ void RS485_Set_Baud_Rate(uint32_t baud)
else
RS485_Baud_Rate = 115200;
I2C_Write_Block(
EEPROM_DEVICE_ADDRESS,
(char *)&RS485_Baud_Rate,
sizeof(RS485_Baud_Rate),
EEPROM_MSTP_BAUD_RATE_ADDR);
I2C_Write_Block(EEPROM_DEVICE_ADDRESS,
(char *) &RS485_Baud_Rate,
sizeof(RS485_Baud_Rate), EEPROM_MSTP_BAUD_RATE_ADDR);
}
/****************************************************************************
@@ -260,75 +252,74 @@ void RS485_Set_Baud_Rate(uint32_t baud)
void RS485_Initialize_Port(void)
{
/* Reset USART registers to POR state */
TXSTA2 = 0;
RCSTA2 = 0;
/* configure USART for receiving */
/* since the TX will handle setting up for transmit */
RCSTA2bits.CREN = 1;
/* Interrupt on receipt */
PIE3bits.RC2IE = 1;
/* enable the transmitter, disable its interrupt */
TXSTA2bits.TXEN = 1;
PIE3bits.TX2IE = 0;
/* 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)
{
case 19200:
SPBRG2 = 64;
TXSTA2bits.BRGH = 1;
break;
case 38400:
SPBRG2 = 32;
TXSTA2bits.BRGH = 1;
break;
case 57600:
SPBRG2 = 21;
TXSTA2bits.BRGH = 1;
break;
case 76800:
SPBRG2 = 3;
TXSTA2bits.BRGH = 0;
break;
case 115200:
SPBRG2 = 10;
TXSTA2bits.BRGH = 1;
break;
case 9600:
SPBRG2 = 129;
TXSTA2bits.BRGH = 1;
break;
default:
SPBRG2 = 129;
TXSTA2bits.BRGH = 1;
RS485_Set_Baud_Rate(9600);
break;
}
/* select async mode */
TXSTA2bits.SYNC = 0;
/* enable transmitter */
TXSTA2bits.TXEN = 1;
/* serial port enable */
RCSTA2bits.SPEN = 1;
/* since we are using RS485,
we need to explicitly say
transmit enable or not */
RS485_RX_DISABLE = 0;
RS485_TX_ENABLE = 0;
/* Reset USART registers to POR state */
TXSTA2 = 0;
RCSTA2 = 0;
/* configure USART for receiving */
/* since the TX will handle setting up for transmit */
RCSTA2bits.CREN = 1;
/* Interrupt on receipt */
PIE3bits.RC2IE = 1;
/* enable the transmitter, disable its interrupt */
TXSTA2bits.TXEN = 1;
PIE3bits.TX2IE = 0;
/* 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) {
case 19200:
SPBRG2 = 64;
TXSTA2bits.BRGH = 1;
break;
case 38400:
SPBRG2 = 32;
TXSTA2bits.BRGH = 1;
break;
case 57600:
SPBRG2 = 21;
TXSTA2bits.BRGH = 1;
break;
case 76800:
SPBRG2 = 3;
TXSTA2bits.BRGH = 0;
break;
case 115200:
SPBRG2 = 10;
TXSTA2bits.BRGH = 1;
break;
case 9600:
SPBRG2 = 129;
TXSTA2bits.BRGH = 1;
break;
default:
SPBRG2 = 129;
TXSTA2bits.BRGH = 1;
RS485_Set_Baud_Rate(9600);
break;
}
/* select async mode */
TXSTA2bits.SYNC = 0;
/* enable transmitter */
TXSTA2bits.TXEN = 1;
/* serial port enable */
RCSTA2bits.SPEN = 1;
/* since we are using RS485,
we need to explicitly say
transmit enable or not */
RS485_RX_DISABLE = 0;
RS485_TX_ENABLE = 0;
}
/****************************************************************************
@@ -339,9 +330,9 @@ void RS485_Initialize_Port(void)
*****************************************************************************/
void RS485_Disable_Port(void)
{
RCSTA2 &= 0x4F; /* Disable the receiver */
TXSTA2bits.TXEN = 0; /* and transmitter */
PIE3 &= 0xCF; /* Disable both interrupts */
RCSTA2 &= 0x4F; /* Disable the receiver */
TXSTA2bits.TXEN = 0; /* and transmitter */
PIE3 &= 0xCF; /* Disable both interrupts */
}
void RS485_Reinit(void)
@@ -357,20 +348,18 @@ void RS485_Reinit(void)
*****************************************************************************/
void RS485_Initialize(void)
{
/* Init the Rs485 buffers */
RS485_Comstat.RxHead = 0;
RS485_Comstat.RxTail = 0;
RS485_Comstat.Rx_Bytes = 0;
RS485_Comstat.Rx_Bufferoverrun = FALSE;
RS485_Comstat.TxHead = 0;
RS485_Comstat.TxTail = 0;
RS485_Comstat.Tx_Bytes = 0;
/* Init the Rs485 buffers */
RS485_Comstat.RxHead = 0;
RS485_Comstat.RxTail = 0;
RS485_Comstat.Rx_Bytes = 0;
RS485_Comstat.Rx_Bufferoverrun = FALSE;
RS485_Comstat.TxHead = 0;
RS485_Comstat.TxTail = 0;
RS485_Comstat.Tx_Bytes = 0;
I2C_Read_Block(
EEPROM_DEVICE_ADDRESS,
(char *)&RS485_Baud_Rate,
sizeof(RS485_Baud_Rate),
EEPROM_MSTP_BAUD_RATE_ADDR);
I2C_Read_Block(EEPROM_DEVICE_ADDRESS,
(char *) &RS485_Baud_Rate,
sizeof(RS485_Baud_Rate), EEPROM_MSTP_BAUD_RATE_ADDR);
RS485_Initialize_Port();
RS485_Initialize_Port();
}