Converted SilenceTimer on MS/TP datalink to be a function so that it can be atomic on 8-bit microcontrollers.

This commit is contained in:
skarg
2007-08-19 12:30:51 +00:00
parent 53f2fc3f35
commit eabe6dee96
17 changed files with 242 additions and 121 deletions
+4 -8
View File
@@ -33,6 +33,7 @@
#include "npdu.h"
/* This file has been customized for use with the AT91SAM7S-EK */
#include "board.h"
#include "timer.h"
/* Number of MS/TP Packets Rx/Tx */
uint16_t MSTP_Packets = 0;
@@ -46,13 +47,6 @@ static volatile struct mstp_port_struct_t MSTP_Port;
static uint8_t TxBuffer[MAX_MPDU];
static uint8_t RxBuffer[MAX_MPDU];
#define INCREMENT_AND_LIMIT_UINT16(x) {if (x < 0xFFFF) x++;}
volatile uint16_t *dlmstp_millisecond_timer_address(void)
{
return &(MSTP_Port.SilenceTimer);
}
void dlmstp_copy_bacnet_address(BACNET_ADDRESS * dest, BACNET_ADDRESS * src)
{
int i = 0;
@@ -83,8 +77,10 @@ bool dlmstp_init(char *ifname)
MSTP_Port.InputBufferSize = sizeof(RxBuffer);
MSTP_Port.OutputBuffer = &TxBuffer[0];
MSTP_Port.OutputBufferSize = sizeof(TxBuffer);
MSTP_Init.SilenceTimer = Timer_Silence;
MSTP_Init.SilenceTimerReset = Timer_Silence_Reset;
MSTP_Init(&MSTP_Port);
return true;
}
+18 -6
View File
@@ -45,11 +45,12 @@
#include "board.h"
#include "dlmstp.h"
// global variable counts interrupts
/* global variable counts interrupts */
volatile unsigned long Timer_Milliseconds;
volatile uint16_t *pTimer_MSTP_Silence;
/* MS/TP Silence Timer */
static volatile uint16_t SilenceTime;
void Timer0_Setup(int milliseconds) {
static void Timer0_Setup(int milliseconds) {
// TC Block Control Register TC_BCR (read/write)
//
// |------------------------------------------------------------------|------|
@@ -296,7 +297,7 @@ void Timer0_Setup(int milliseconds) {
// Modified by Steve Karg
// simplified and changed to a millisecond count-up timer
// *****************************************************************************
void Timer0IrqHandler (void) {
static void Timer0IrqHandler (void) {
volatile AT91PS_TC pTC = AT91C_BASE_TC0; // pointer to timer channel 0 register structure
unsigned int dummy; // temporary
@@ -305,10 +306,21 @@ void Timer0IrqHandler (void) {
dummy = pTC->TC_SR;
// increment the tick count
Timer_Milliseconds++;
if ((*pTimer_MSTP_Silence) < 0xFFFF)
(*pTimer_MSTP_Silence)++;
if (SilenceTime < 0xFFFF)
SilenceTime++;
}
uint16_t Timer_Silence(void)
{
return SilenceTime;
}
void Timer_Silence_Reset(void)
{
SilenceTime = 0;
}
// *****************************************************************************
//
// Timer 0 Initialization
+44
View File
@@ -0,0 +1,44 @@
/**************************************************************************
*
* Copyright (C) 2007 Steve Karg <skarg@users.sourceforge.net>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*********************************************************************/
#ifndef TIMER_H
#define TIMER_H
#include <stdint.h>
extern volatile unsigned long Timer_Milliseconds;
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
void TimerInit(void);
uint16_t Timer_Silence(void);
void Timer_Silence_Reset(void);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
+1 -4
View File
@@ -75,7 +75,7 @@ void init(void)
RS485_Set_Baud_Rate(38400);
/* Configure Timer0 for millisecond timer */
timer_initialize();
Timer_Initialize();
/* Enable global interrupts */
sei();
@@ -86,9 +86,6 @@ void task_milliseconds(void)
while (Timer_Milliseconds) {
Timer_Milliseconds--;
/* add other millisecond timer tasks here */
#if defined(BACDL_MSTP)
dlmstp_millisecond_timer();
#endif
}
}
+2 -2
View File
@@ -128,7 +128,7 @@ void RS485_Send_Frame(
if (!turnaround_time) {
turnaround_time = 1;
}
while (mstp_port->SilenceTimer < turnaround_time) {
while (mstp_port->SilenceTimer() < turnaround_time) {
/* do nothing - wait for timer to increment */
};
}
@@ -146,7 +146,7 @@ void RS485_Send_Frame(
}
/* per MSTP spec */
if (mstp_port) {
mstp_port->SilenceTimer = 0;
mstp_port->SilenceTimerReset();
}
return;
+30 -1
View File
@@ -37,9 +37,11 @@
#define TIMER_COUNT (0xFF-TIMER_TICKS)
/* Global variable millisecond timer - used by main.c for timers task */
volatile uint8_t Timer_Milliseconds = 0;
/* MS/TP Silence Timer */
static volatile uint16_t SilenceTime;
/* Configure the Timer */
void timer_initialize(void)
void Timer_Initialize(void)
{
/* Normal Operation */
TCCR1A = 0;
@@ -76,4 +78,31 @@ ISR(TIMER0_OVF_vect)
/* Update the global timer */
if (Timer_Milliseconds < 0xFF)
Timer_Milliseconds++;
if (SilenceTime < 0xFFFF)
SilenceTime++;
}
/* Public access to the Silence Timer */
uint16_t Timer_Silence(void)
{
uint16_t timer;
uint8_t sreg;
sreg = SREG;
cli();
timer = SilenceTime;
SREG = sreg;
return timer;
}
/* Public reset of the Silence Timer */
void Timer_Silence_Reset(void)
{
uint8_t sreg;
sreg = SREG;
cli();
SilenceTime = 0;
SREG = sreg;
}
+11 -1
View File
@@ -27,6 +27,16 @@
extern volatile uint8_t Timer_Milliseconds;
void timer_initialize(void);
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
void Timer_Initialize(void);
uint16_t Timer_Silence(void);
void Timer_Silence_Reset(void);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
+13 -2
View File
@@ -58,12 +58,21 @@ volatile struct mstp_port_struct_t MSTP_Port;
/* buffers needed by mstp port struct */
static uint8_t TxBuffer[MAX_MPDU];
static uint8_t RxBuffer[MAX_MPDU];
/* Timer that indicates line silence - and functions */
static uint16_t SilenceTime;
#define INCREMENT_AND_LIMIT_UINT16(x) {if (x < 0xFFFF) x++;}
static uint16_t Timer_Silence(void)
{
return SilenceTime;
}
static void Timer_Silence_Reset(void)
{
SilenceTime = 0;
}
void dlmstp_millisecond_timer(void)
{
INCREMENT_AND_LIMIT_UINT16(MSTP_Port.SilenceTimer);
INCREMENT_AND_LIMIT_UINT16(SilenceTime);
}
static void *dlmstp_milliseconds_task(void *pArg)
@@ -590,6 +599,8 @@ bool dlmstp_init(char *ifname)
MSTP_Port.InputBufferSize = sizeof(RxBuffer);
MSTP_Port.OutputBuffer = &TxBuffer[0];
MSTP_Port.OutputBufferSize = sizeof(TxBuffer);
MSTP_Port.SilenceTimer = Timer_Silence;
MSTP_Port.SilenceTimerReset = Timer_Silence_Reset;
MSTP_Init(&MSTP_Port);
#if 0
/* FIXME: implement your data storage */
+2 -2
View File
@@ -157,7 +157,7 @@ void RS485_Send_Frame(
turnaround_time = 2;
else
turnaround_time = 1;
while (mstp_port->SilenceTimer < turnaround_time) {
while (mstp_port->SilenceTimer() < turnaround_time) {
/* do nothing - wait for timer to increment */
sched_yield();
};
@@ -173,7 +173,7 @@ void RS485_Send_Frame(
/* per MSTP spec, sort of */
if (mstp_port) {
mstp_port->SilenceTimer = 0;
mstp_port->SilenceTimerReset();
}
return;
+13 -2
View File
@@ -53,12 +53,21 @@ volatile struct mstp_port_struct_t MSTP_Port;
/* buffers needed by mstp port struct */
static uint8_t TxBuffer[MAX_MPDU];
static uint8_t RxBuffer[MAX_MPDU];
/* Timer that indicates line silence - and functions */
static uint16_t SilenceTime;
#define INCREMENT_AND_LIMIT_UINT16(x) {if (x < 0xFFFF) x++;}
static uint16_t Timer_Silence(void)
{
return SilenceTime;
}
static void Timer_Silence_Reset(void)
{
SilenceTime = 0;
}
void dlmstp_millisecond_timer(void)
{
INCREMENT_AND_LIMIT_UINT16(MSTP_Port.SilenceTimer);
INCREMENT_AND_LIMIT_UINT16(SilenceTime);
}
void dlmstp_reinit(void)
@@ -435,6 +444,8 @@ bool dlmstp_init(char *ifname)
MSTP_Port.InputBufferSize = sizeof(RxBuffer);
MSTP_Port.OutputBuffer = &TxBuffer[0];
MSTP_Port.OutputBufferSize = sizeof(TxBuffer);
MSTP_Init.SilenceTimer = Timer_Silence;
MSTP_Init.SilenceTimerReset = Timer_Silence_Reset;
MSTP_Init(&MSTP_Port);
#if 0
uint8_t data;
+2 -4
View File
@@ -279,7 +279,6 @@ void RS485_Send_Frame(
{
DWORD dwWritten = 0;
#if 0
if (mstp_port) {
uint32_t baud;
uint8_t turnaround_time;
@@ -291,16 +290,15 @@ void RS485_Send_Frame(
turnaround_time = 2;
else
turnaround_time = 1;
while (mstp_port->SilenceTimer < turnaround_time) {
while (mstp_port->SilenceTimer() < turnaround_time) {
/* do nothing - wait for timer to increment */
};
}
#endif
WriteFile(RS485_Handle, buffer, nbytes, &dwWritten, NULL);
/* per MSTP spec, reset SilenceTimer after each byte is sent */
if (mstp_port) {
mstp_port->SilenceTimer = 0;
mstp_port->SilenceTimerReset();
}
return;