|
|
|
@ -45,7 +45,6 @@ THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include "yaMBSiavr.h"
|
|
|
|
#include "yaMBSiavr.h"
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include "aBusIO.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
volatile unsigned char BusState = 0;
|
|
|
|
volatile unsigned char BusState = 0;
|
|
|
|
volatile uint16_t modbusTimer = 0;
|
|
|
|
volatile uint16_t modbusTimer = 0;
|
|
|
|
@ -180,26 +179,27 @@ void modbusTickTimer(void)
|
|
|
|
|
|
|
|
|
|
|
|
ISR(UART_RECEIVE_INTERRUPT)
|
|
|
|
ISR(UART_RECEIVE_INTERRUPT)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
unsigned char data;
|
|
|
|
unsigned char data;
|
|
|
|
data = UART_DATA;
|
|
|
|
data = UART_DATA;
|
|
|
|
modbusTimer=0; //reset timer
|
|
|
|
modbusTimer=0; //reset timer
|
|
|
|
if (!(BusState & (1<<ReceiveCompleted)) && !(BusState & (1<<TransmitRequested)) && !(BusState & (1<<Transmitting)) && (BusState & (1<<Receiving)) && !(BusState & (1<<BusTimedOut)))
|
|
|
|
if (!(BusState & (1<<ReceiveCompleted)) && !(BusState & (1<<TransmitRequested)) && !(BusState & (1<<Transmitting)) && (BusState & (1<<Receiving)) && !(BusState & (1<<BusTimedOut)))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (DataPos>MaxFrameIndex) modbusReset();
|
|
|
|
if (DataPos>MaxFrameIndex) modbusReset();
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
rxbuffer[DataPos]=data;
|
|
|
|
rxbuffer[DataPos]=data;
|
|
|
|
DataPos++; //TODO: maybe prevent this from exceeding 255?
|
|
|
|
DataPos++; //TODO: maybe prevent this from exceeding 255?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
} else
|
|
|
|
if (!(BusState & (1<<ReceiveCompleted)) && !(BusState & (1<<TransmitRequested)) && !(BusState & (1<<Transmitting)) && !(BusState & (1<<Receiving)) && (BusState & (1<<BusTimedOut)))
|
|
|
|
if (!(BusState & (1<<ReceiveCompleted)) && !(BusState & (1<<TransmitRequested)) && !(BusState & (1<<Transmitting)) && !(BusState & (1<<Receiving)) && (BusState & (1<<BusTimedOut)))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
rxbuffer[0]=data;
|
|
|
|
rxbuffer[0]=data;
|
|
|
|
BusState=((1<<Receiving)|(1<<TimerActive));
|
|
|
|
BusState=((1<<Receiving)|(1<<TimerActive));
|
|
|
|
DataPos=1;
|
|
|
|
DataPos=1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//TODO
|
|
|
|
ISR(UART_TRANSMIT_INTERRUPT)
|
|
|
|
ISR(UART_TRANSMIT_INTERRUPT)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
BusState&=~(1<<TransmitRequested);
|
|
|
|
BusState&=~(1<<TransmitRequested);
|
|
|
|
@ -219,6 +219,7 @@ ISR(UART_TRANSMIT_COMPLETE_INTERRUPT)
|
|
|
|
modbusReset();
|
|
|
|
modbusReset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//TODO
|
|
|
|
void modbusInit(void)
|
|
|
|
void modbusInit(void)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
UBRRH = (unsigned char)((UBRR) >> 8);
|
|
|
|
UBRRH = (unsigned char)((UBRR) >> 8);
|
|
|
|
@ -242,6 +243,7 @@ void modbusInit(void)
|
|
|
|
* Arguments: - packtop: Position of the last byte containing data.
|
|
|
|
* Arguments: - packtop: Position of the last byte containing data.
|
|
|
|
* modbusSendException is a good usage example.
|
|
|
|
* modbusSendException is a good usage example.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
//TODO
|
|
|
|
void modbusSendMessage(unsigned char packtop)
|
|
|
|
void modbusSendMessage(unsigned char packtop)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
PacketTopIndex=packtop+2;
|
|
|
|
PacketTopIndex=packtop+2;
|
|
|
|
|