Converted timer to a more useful and easily used timer library.
This commit is contained in:
@@ -55,6 +55,7 @@ CSRC = main.c \
|
|||||||
serial.c \
|
serial.c \
|
||||||
rs485.c \
|
rs485.c \
|
||||||
timer2.c \
|
timer2.c \
|
||||||
|
timer.c \
|
||||||
led.c \
|
led.c \
|
||||||
eeprom.c \
|
eeprom.c \
|
||||||
seeprom.c \
|
seeprom.c \
|
||||||
|
|||||||
@@ -459,10 +459,10 @@ static void MSTP_Receive_Frame_FSM(
|
|||||||
for the beginning of a frame. */
|
for the beginning of a frame. */
|
||||||
if (rs485_receive_error()) {
|
if (rs485_receive_error()) {
|
||||||
/* EatAnError */
|
/* EatAnError */
|
||||||
timer_reset(TIMER_SILENCE);
|
rs485_silence_time_reset();
|
||||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||||
} else if (rs485_byte_available(&DataRegister)) {
|
} else if (rs485_byte_available(&DataRegister)) {
|
||||||
timer_reset(TIMER_SILENCE);
|
rs485_silence_time_reset();
|
||||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||||
if (DataRegister == 0x55) {
|
if (DataRegister == 0x55) {
|
||||||
/* Preamble1 */
|
/* Preamble1 */
|
||||||
@@ -474,19 +474,19 @@ static void MSTP_Receive_Frame_FSM(
|
|||||||
case MSTP_RECEIVE_STATE_PREAMBLE:
|
case MSTP_RECEIVE_STATE_PREAMBLE:
|
||||||
/* In the PREAMBLE state, the node waits for the
|
/* In the PREAMBLE state, the node waits for the
|
||||||
second octet of the preamble. */
|
second octet of the preamble. */
|
||||||
if (timer_elapsed_milliseconds(TIMER_SILENCE, Tframe_abort)) {
|
if (rs485_silence_time_elapsed(Tframe_abort)) {
|
||||||
/* Timeout */
|
/* Timeout */
|
||||||
/* a correct preamble has not been received */
|
/* a correct preamble has not been received */
|
||||||
/* wait for the start of a frame. */
|
/* wait for the start of a frame. */
|
||||||
Receive_State = MSTP_RECEIVE_STATE_IDLE;
|
Receive_State = MSTP_RECEIVE_STATE_IDLE;
|
||||||
} else if (rs485_receive_error()) {
|
} else if (rs485_receive_error()) {
|
||||||
/* Error */
|
/* Error */
|
||||||
timer_reset(TIMER_SILENCE);
|
rs485_silence_time_reset();
|
||||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||||
/* wait for the start of a frame. */
|
/* wait for the start of a frame. */
|
||||||
Receive_State = MSTP_RECEIVE_STATE_IDLE;
|
Receive_State = MSTP_RECEIVE_STATE_IDLE;
|
||||||
} else if (rs485_byte_available(&DataRegister)) {
|
} else if (rs485_byte_available(&DataRegister)) {
|
||||||
timer_reset(TIMER_SILENCE);
|
rs485_silence_time_reset();
|
||||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||||
if (DataRegister == 0xFF) {
|
if (DataRegister == 0xFF) {
|
||||||
/* Preamble2 */
|
/* Preamble2 */
|
||||||
@@ -508,7 +508,7 @@ static void MSTP_Receive_Frame_FSM(
|
|||||||
case MSTP_RECEIVE_STATE_HEADER:
|
case MSTP_RECEIVE_STATE_HEADER:
|
||||||
/* In the HEADER state, the node waits
|
/* In the HEADER state, the node waits
|
||||||
for the fixed message header. */
|
for the fixed message header. */
|
||||||
if (timer_elapsed_milliseconds(TIMER_SILENCE, Tframe_abort)) {
|
if (rs485_silence_time_elapsed(Tframe_abort)) {
|
||||||
/* Timeout */
|
/* Timeout */
|
||||||
/* indicate that an error has occurred
|
/* indicate that an error has occurred
|
||||||
during the reception of a frame */
|
during the reception of a frame */
|
||||||
@@ -517,7 +517,7 @@ static void MSTP_Receive_Frame_FSM(
|
|||||||
Receive_State = MSTP_RECEIVE_STATE_IDLE;
|
Receive_State = MSTP_RECEIVE_STATE_IDLE;
|
||||||
} else if (rs485_receive_error()) {
|
} else if (rs485_receive_error()) {
|
||||||
/* Error */
|
/* Error */
|
||||||
timer_reset(TIMER_SILENCE);
|
rs485_silence_time_reset();
|
||||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||||
/* indicate that an error has occurred
|
/* indicate that an error has occurred
|
||||||
during the reception of a frame */
|
during the reception of a frame */
|
||||||
@@ -525,7 +525,7 @@ static void MSTP_Receive_Frame_FSM(
|
|||||||
/* wait for the start of a frame. */
|
/* wait for the start of a frame. */
|
||||||
Receive_State = MSTP_RECEIVE_STATE_IDLE;
|
Receive_State = MSTP_RECEIVE_STATE_IDLE;
|
||||||
} else if (rs485_byte_available(&DataRegister)) {
|
} else if (rs485_byte_available(&DataRegister)) {
|
||||||
timer_reset(TIMER_SILENCE);
|
rs485_silence_time_reset();
|
||||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||||
if (Index == 0) {
|
if (Index == 0) {
|
||||||
/* FrameType */
|
/* FrameType */
|
||||||
@@ -610,7 +610,7 @@ static void MSTP_Receive_Frame_FSM(
|
|||||||
case MSTP_RECEIVE_STATE_DATA:
|
case MSTP_RECEIVE_STATE_DATA:
|
||||||
/* In the DATA state, the node waits
|
/* In the DATA state, the node waits
|
||||||
for the data portion of a frame. */
|
for the data portion of a frame. */
|
||||||
if (timer_elapsed_milliseconds(TIMER_SILENCE, Tframe_abort)) {
|
if (rs485_silence_time_elapsed(Tframe_abort)) {
|
||||||
/* Timeout */
|
/* Timeout */
|
||||||
/* indicate that an error has occurred
|
/* indicate that an error has occurred
|
||||||
during the reception of a frame */
|
during the reception of a frame */
|
||||||
@@ -619,7 +619,7 @@ static void MSTP_Receive_Frame_FSM(
|
|||||||
Receive_State = MSTP_RECEIVE_STATE_IDLE;
|
Receive_State = MSTP_RECEIVE_STATE_IDLE;
|
||||||
} else if (rs485_receive_error()) {
|
} else if (rs485_receive_error()) {
|
||||||
/* Error */
|
/* Error */
|
||||||
timer_reset(TIMER_SILENCE);
|
rs485_silence_time_reset();
|
||||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||||
/* indicate that an error has occurred during
|
/* indicate that an error has occurred during
|
||||||
the reception of a frame */
|
the reception of a frame */
|
||||||
@@ -627,7 +627,7 @@ static void MSTP_Receive_Frame_FSM(
|
|||||||
/* wait for the start of the next frame. */
|
/* wait for the start of the next frame. */
|
||||||
Receive_State = MSTP_RECEIVE_STATE_IDLE;
|
Receive_State = MSTP_RECEIVE_STATE_IDLE;
|
||||||
} else if (rs485_byte_available(&DataRegister)) {
|
} else if (rs485_byte_available(&DataRegister)) {
|
||||||
timer_reset(TIMER_SILENCE);
|
rs485_silence_time_reset();
|
||||||
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
INCREMENT_AND_LIMIT_UINT8(EventCount);
|
||||||
if (Index < DataLength) {
|
if (Index < DataLength) {
|
||||||
/* DataOctet */
|
/* DataOctet */
|
||||||
@@ -736,7 +736,7 @@ static bool MSTP_Master_Node_FSM(
|
|||||||
break;
|
break;
|
||||||
case MSTP_MASTER_STATE_IDLE:
|
case MSTP_MASTER_STATE_IDLE:
|
||||||
/* In the IDLE state, the node waits for a frame. */
|
/* In the IDLE state, the node waits for a frame. */
|
||||||
if (timer_elapsed_milliseconds(TIMER_SILENCE, Tno_token)) {
|
if (rs485_silence_time_elapsed(Tno_token)) {
|
||||||
/* LostToken */
|
/* LostToken */
|
||||||
/* assume that the token has been lost */
|
/* assume that the token has been lost */
|
||||||
EventCount = 0; /* Addendum 135-2004d-8 */
|
EventCount = 0; /* Addendum 135-2004d-8 */
|
||||||
@@ -842,7 +842,7 @@ static bool MSTP_Master_Node_FSM(
|
|||||||
case MSTP_MASTER_STATE_WAIT_FOR_REPLY:
|
case MSTP_MASTER_STATE_WAIT_FOR_REPLY:
|
||||||
/* In the WAIT_FOR_REPLY state, the node waits for */
|
/* In the WAIT_FOR_REPLY state, the node waits for */
|
||||||
/* a reply from another node. */
|
/* a reply from another node. */
|
||||||
if (timer_elapsed_milliseconds(TIMER_SILENCE, Treply_timeout)) {
|
if (rs485_silence_time_elapsed(Treply_timeout)) {
|
||||||
/* ReplyTimeout */
|
/* ReplyTimeout */
|
||||||
/* assume that the request has failed */
|
/* assume that the request has failed */
|
||||||
FrameCount = Nmax_info_frames;
|
FrameCount = Nmax_info_frames;
|
||||||
@@ -979,7 +979,7 @@ static bool MSTP_Master_Node_FSM(
|
|||||||
/* The PASS_TOKEN state listens for a successor to begin using */
|
/* The PASS_TOKEN state listens for a successor to begin using */
|
||||||
/* the token that this node has just attempted to pass. */
|
/* the token that this node has just attempted to pass. */
|
||||||
case MSTP_MASTER_STATE_PASS_TOKEN:
|
case MSTP_MASTER_STATE_PASS_TOKEN:
|
||||||
if (timer_elapsed_milliseconds(TIMER_SILENCE, Tusage_timeout)) {
|
if (rs485_silence_time_elapsed(Tusage_timeout)) {
|
||||||
if (RetryCount < Nretry_token) {
|
if (RetryCount < Nretry_token) {
|
||||||
/* RetrySendToken */
|
/* RetrySendToken */
|
||||||
RetryCount++;
|
RetryCount++;
|
||||||
@@ -1022,11 +1022,11 @@ static bool MSTP_Master_Node_FSM(
|
|||||||
whether or not this node may create a token. */
|
whether or not this node may create a token. */
|
||||||
case MSTP_MASTER_STATE_NO_TOKEN:
|
case MSTP_MASTER_STATE_NO_TOKEN:
|
||||||
my_timeout = Tno_token + (Tslot * This_Station);
|
my_timeout = Tno_token + (Tslot * This_Station);
|
||||||
if (timer_elapsed_milliseconds(TIMER_SILENCE, my_timeout)) {
|
if (rs485_silence_time_elapsed(my_timeout)) {
|
||||||
ns_timeout = Tno_token + (Tslot * (This_Station + 1));
|
ns_timeout = Tno_token + (Tslot * (This_Station + 1));
|
||||||
if (timer_elapsed_milliseconds(TIMER_SILENCE, ns_timeout)) {
|
if (rs485_silence_time_elapsed(ns_timeout)) {
|
||||||
/* should never get here unless timer resolution is bad */
|
/* should never get here unless timer resolution is bad */
|
||||||
timer_reset(TIMER_SILENCE);
|
rs485_silence_time_reset();
|
||||||
Master_State = MSTP_MASTER_STATE_IDLE;
|
Master_State = MSTP_MASTER_STATE_IDLE;
|
||||||
} else {
|
} else {
|
||||||
/* GenerateToken */
|
/* GenerateToken */
|
||||||
@@ -1084,8 +1084,7 @@ static bool MSTP_Master_Node_FSM(
|
|||||||
transition_now = true;
|
transition_now = true;
|
||||||
}
|
}
|
||||||
MSTP_Flag.ReceivedValidFrame = false;
|
MSTP_Flag.ReceivedValidFrame = false;
|
||||||
} else if ((timer_elapsed_milliseconds(TIMER_SILENCE,
|
} else if ((rs485_silence_time_elapsed(Tusage_timeout)) ||
|
||||||
Tusage_timeout)) ||
|
|
||||||
(MSTP_Flag.ReceivedInvalidFrame == true)) {
|
(MSTP_Flag.ReceivedInvalidFrame == true)) {
|
||||||
if (MSTP_Flag.SoleMaster == true) {
|
if (MSTP_Flag.SoleMaster == true) {
|
||||||
/* SoleMaster */
|
/* SoleMaster */
|
||||||
|
|||||||
@@ -52,15 +52,4 @@
|
|||||||
#define SEEPROM_PAGE_SIZE 16
|
#define SEEPROM_PAGE_SIZE 16
|
||||||
#define SEEPROM_WORD_ADDRESS_16BIT 0
|
#define SEEPROM_WORD_ADDRESS_16BIT 0
|
||||||
|
|
||||||
/* reserve the millisecond timer indexes as needed for each module */
|
|
||||||
#define TIMER_SILENCE 0
|
|
||||||
#define TIMER_DEBOUNCE 1
|
|
||||||
#define TIMER_LED_1 2
|
|
||||||
#define TIMER_LED_2 3
|
|
||||||
#define TIMER_LED_3 4
|
|
||||||
#define TIMER_LED_4 5
|
|
||||||
#define TIMER_DCC 6
|
|
||||||
#define TIMER_TEST 7
|
|
||||||
#define MAX_MILLISECOND_TIMERS 8
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -29,6 +29,7 @@
|
|||||||
|
|
||||||
static uint8_t Address_Switch;
|
static uint8_t Address_Switch;
|
||||||
static uint8_t Buttons;
|
static uint8_t Buttons;
|
||||||
|
static struct itimer Debounce_Timer;
|
||||||
|
|
||||||
#define BDK_V1_HACK 0
|
#define BDK_V1_HACK 0
|
||||||
|
|
||||||
@@ -75,8 +76,8 @@ void input_task(
|
|||||||
static uint8_t old_buttons = 0;
|
static uint8_t old_buttons = 0;
|
||||||
|
|
||||||
/* only check the inputs every debounce time */
|
/* only check the inputs every debounce time */
|
||||||
if (timer_elapsed_milliseconds(TIMER_DEBOUNCE, 30)) {
|
if (timer_interval_expired(&Debounce_Timer)) {
|
||||||
timer_reset(TIMER_DEBOUNCE);
|
timer_interval_reset(&Debounce_Timer);
|
||||||
/* pins used are PA6, PA5, PA4, PA3, PA2, PA1, PA0 */
|
/* pins used are PA6, PA5, PA4, PA3, PA2, PA1, PA0 */
|
||||||
#if BDK_V1_HACK
|
#if BDK_V1_HACK
|
||||||
/* version 1 BDK - workaround */
|
/* version 1 BDK - workaround */
|
||||||
@@ -161,5 +162,5 @@ void input_init(
|
|||||||
BIT_CLEAR(DDRB, DDB2);
|
BIT_CLEAR(DDRB, DDB2);
|
||||||
BIT_CLEAR(DDRB, DDB3);
|
BIT_CLEAR(DDRB, DDB3);
|
||||||
BIT_CLEAR(DDRB, DDB4);
|
BIT_CLEAR(DDRB, DDB4);
|
||||||
timer_reset(TIMER_DEBOUNCE);
|
timer_interval_start(&Debounce_Timer, 30);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,10 +26,7 @@
|
|||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
#include "led.h"
|
#include "led.h"
|
||||||
|
|
||||||
static uint32_t Off_Delay_Milliseconds_1;
|
static struct itimer Off_Delay_Timer[MAX_LEDS];
|
||||||
static uint32_t Off_Delay_Milliseconds_2;
|
|
||||||
static uint32_t Off_Delay_Milliseconds_3;
|
|
||||||
static uint32_t Off_Delay_Milliseconds_4;
|
|
||||||
|
|
||||||
/*************************************************************************
|
/*************************************************************************
|
||||||
* Description: Turn on an LED
|
* Description: Turn on an LED
|
||||||
@@ -55,6 +52,9 @@ void led_on(
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (index < MAX_LEDS) {
|
||||||
|
timer_interval_no_expire(&Off_Delay_Timer[index]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*************************************************************************
|
/*************************************************************************
|
||||||
@@ -81,6 +81,9 @@ void led_off(
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (index < MAX_LEDS) {
|
||||||
|
timer_interval_no_expire(&Off_Delay_Timer[index]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*************************************************************************
|
/*************************************************************************
|
||||||
@@ -135,25 +138,8 @@ void led_off_delay(
|
|||||||
uint8_t index,
|
uint8_t index,
|
||||||
uint32_t delay_ms)
|
uint32_t delay_ms)
|
||||||
{
|
{
|
||||||
switch (index) {
|
if (index < MAX_LEDS) {
|
||||||
case LED_1:
|
timer_interval_start(&Off_Delay_Timer[index], delay_ms);
|
||||||
Off_Delay_Milliseconds_1 = delay_ms;
|
|
||||||
timer_reset(TIMER_LED_1);
|
|
||||||
break;
|
|
||||||
case LED_2:
|
|
||||||
Off_Delay_Milliseconds_2 = delay_ms;
|
|
||||||
timer_reset(TIMER_LED_2);
|
|
||||||
break;
|
|
||||||
case LED_3:
|
|
||||||
Off_Delay_Milliseconds_3 = delay_ms;
|
|
||||||
timer_reset(TIMER_LED_3);
|
|
||||||
break;
|
|
||||||
case LED_4:
|
|
||||||
Off_Delay_Milliseconds_4 = delay_ms;
|
|
||||||
timer_reset(TIMER_LED_4);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,28 +151,12 @@ void led_off_delay(
|
|||||||
void led_task(
|
void led_task(
|
||||||
void)
|
void)
|
||||||
{
|
{
|
||||||
if (Off_Delay_Milliseconds_1) {
|
uint8_t i; /* loop counter */
|
||||||
if (timer_elapsed_milliseconds(TIMER_LED_1, Off_Delay_Milliseconds_1)) {
|
|
||||||
Off_Delay_Milliseconds_1 = 0;
|
for (i = 0; i < MAX_LEDS; i++) {
|
||||||
led_off(LED_1);
|
if (timer_interval_expired(&Off_Delay_Timer[i])) {
|
||||||
}
|
timer_interval_no_expire(&Off_Delay_Timer[i]);
|
||||||
}
|
led_off(i);
|
||||||
if (Off_Delay_Milliseconds_2) {
|
|
||||||
if (timer_elapsed_milliseconds(TIMER_LED_2, Off_Delay_Milliseconds_2)) {
|
|
||||||
Off_Delay_Milliseconds_2 = 0;
|
|
||||||
led_off(LED_2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (Off_Delay_Milliseconds_3) {
|
|
||||||
if (timer_elapsed_milliseconds(TIMER_LED_3, Off_Delay_Milliseconds_3)) {
|
|
||||||
Off_Delay_Milliseconds_3 = 0;
|
|
||||||
led_off(LED_3);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (Off_Delay_Milliseconds_4) {
|
|
||||||
if (timer_elapsed_milliseconds(TIMER_LED_4, Off_Delay_Milliseconds_4)) {
|
|
||||||
Off_Delay_Milliseconds_4 = 0;
|
|
||||||
led_off(LED_4);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -199,13 +169,14 @@ void led_task(
|
|||||||
void led_init(
|
void led_init(
|
||||||
void)
|
void)
|
||||||
{
|
{
|
||||||
|
uint8_t i; /* loop counter */
|
||||||
|
|
||||||
/* configure the port pins as outputs */
|
/* configure the port pins as outputs */
|
||||||
BIT_SET(DDRC, DDC7);
|
BIT_SET(DDRC, DDC7);
|
||||||
BIT_SET(DDRC, DDC6);
|
BIT_SET(DDRC, DDC6);
|
||||||
BIT_SET(DDRD, DDD7);
|
BIT_SET(DDRD, DDD7);
|
||||||
BIT_SET(DDRD, DDD6);
|
BIT_SET(DDRD, DDD6);
|
||||||
led_off(LED_1);
|
for (i = 0; i < MAX_LEDS; i++) {
|
||||||
led_off(LED_2);
|
led_off(i);
|
||||||
led_off(LED_3);
|
}
|
||||||
led_off(LED_4);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,6 +54,11 @@
|
|||||||
const char *BACnet_Version = "1.0";
|
const char *BACnet_Version = "1.0";
|
||||||
/* MAC Address of MS/TP */
|
/* MAC Address of MS/TP */
|
||||||
static uint8_t MSTP_MAC_Address;
|
static uint8_t MSTP_MAC_Address;
|
||||||
|
/* timer for device communications control */
|
||||||
|
static struct itimer DCC_Timer;
|
||||||
|
#define DCC_CYCLE_SECONDS 1
|
||||||
|
/* timer for test task */
|
||||||
|
static struct itimer Test_Timer;
|
||||||
|
|
||||||
/* For porting to IAR, see:
|
/* For porting to IAR, see:
|
||||||
http://www.avrfreaks.net/wiki/index.php/Documentation:AVR_GCC/IarToAvrgcc*/
|
http://www.avrfreaks.net/wiki/index.php/Documentation:AVR_GCC/IarToAvrgcc*/
|
||||||
@@ -114,7 +119,9 @@ static void bacnet_init(
|
|||||||
/* handle communication so we can shutup when asked */
|
/* handle communication so we can shutup when asked */
|
||||||
apdu_set_confirmed_handler(SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL,
|
apdu_set_confirmed_handler(SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL,
|
||||||
handler_device_communication_control);
|
handler_device_communication_control);
|
||||||
|
/* start the cyclic 1 second timer for DCC */
|
||||||
|
timer_interval_start_seconds(&DCC_Timer, DCC_CYCLE_SECONDS);
|
||||||
|
/* Hello World! */
|
||||||
Send_I_Am(&Handler_Transmit_Buffer[0]);
|
Send_I_Am(&Handler_Transmit_Buffer[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,9 +156,9 @@ static void bacnet_task(
|
|||||||
Binary_Input_Present_Value_Set(i, binary_value);
|
Binary_Input_Present_Value_Set(i, binary_value);
|
||||||
}
|
}
|
||||||
/* handle the communication timer */
|
/* handle the communication timer */
|
||||||
if (timer_elapsed_seconds(TIMER_DCC, 1)) {
|
if (timer_interval_expired(&DCC_Timer)) {
|
||||||
timer_reset(TIMER_DCC);
|
timer_interval_reset(&DCC_Timer);
|
||||||
dcc_timer_seconds(1);
|
dcc_timer_seconds(DCC_CYCLE_SECONDS);
|
||||||
}
|
}
|
||||||
/* handle the messaging */
|
/* handle the messaging */
|
||||||
pdu_len = datalink_receive(&src, &PDUBuffer[0], sizeof(PDUBuffer), 0);
|
pdu_len = datalink_receive(&src, &PDUBuffer[0], sizeof(PDUBuffer), 0);
|
||||||
@@ -163,9 +170,7 @@ static void bacnet_task(
|
|||||||
void test_init(
|
void test_init(
|
||||||
void)
|
void)
|
||||||
{
|
{
|
||||||
timer_reset(TIMER_LED_3);
|
timer_interval_start_seconds(&Test_Timer, 1);
|
||||||
timer_reset(TIMER_LED_4);
|
|
||||||
timer_reset(TIMER_TEST);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_task(
|
void test_task(
|
||||||
@@ -175,8 +180,8 @@ void test_task(
|
|||||||
uint8_t nbytes = 17;
|
uint8_t nbytes = 17;
|
||||||
uint8_t data_register = 0;
|
uint8_t data_register = 0;
|
||||||
|
|
||||||
if (timer_elapsed_seconds(TIMER_TEST, 1)) {
|
if (timer_interval_expired(&Test_Timer)) {
|
||||||
timer_reset(TIMER_TEST);
|
timer_interval_reset(&Test_Timer);
|
||||||
buffer[8] = (MSTP_MAC_Address & BIT0) ? '1' : '0';
|
buffer[8] = (MSTP_MAC_Address & BIT0) ? '1' : '0';
|
||||||
buffer[9] = (MSTP_MAC_Address & BIT1) ? '1' : '0';
|
buffer[9] = (MSTP_MAC_Address & BIT1) ? '1' : '0';
|
||||||
buffer[10] = (MSTP_MAC_Address & BIT2) ? '1' : '0';
|
buffer[10] = (MSTP_MAC_Address & BIT2) ? '1' : '0';
|
||||||
|
|||||||
@@ -49,6 +49,18 @@ static uint32_t Baud_Rate = 9600;
|
|||||||
static uint8_t Receive_Buffer_Data[128];
|
static uint8_t Receive_Buffer_Data[128];
|
||||||
static FIFO_BUFFER Receive_Buffer;
|
static FIFO_BUFFER Receive_Buffer;
|
||||||
|
|
||||||
|
static struct etimer Silence_Timer;
|
||||||
|
|
||||||
|
bool rs485_silence_time_elapsed(uint32_t milliseconds)
|
||||||
|
{
|
||||||
|
return timer_elapsed_milliseconds(&Silence_Timer, milliseconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
void rs485_silence_time_reset(void)
|
||||||
|
{
|
||||||
|
timer_elapsed_start(&Silence_Timer);
|
||||||
|
}
|
||||||
|
|
||||||
static void rs485_rts_init(
|
static void rs485_rts_init(
|
||||||
void)
|
void)
|
||||||
{
|
{
|
||||||
@@ -82,7 +94,7 @@ void rs485_turnaround_delay(
|
|||||||
/* wait a minimum 40 bit times since reception */
|
/* wait a minimum 40 bit times since reception */
|
||||||
/* at least 1 ms for errors: rounding, clock tick */
|
/* at least 1 ms for errors: rounding, clock tick */
|
||||||
turnaround_time = 1 + ((Tturnaround * 1000UL) / Baud_Rate);
|
turnaround_time = 1 + ((Tturnaround * 1000UL) / Baud_Rate);
|
||||||
while (!timer_elapsed_milliseconds(TIMER_SILENCE, turnaround_time)) {
|
while (!timer_elapsed_milliseconds(&Silence_Timer, turnaround_time)) {
|
||||||
/* do nothing - wait for timer to increment */
|
/* do nothing - wait for timer to increment */
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -95,7 +107,7 @@ ISR(USART0_RX_vect)
|
|||||||
/* data is available */
|
/* data is available */
|
||||||
data_byte = UDR0;
|
data_byte = UDR0;
|
||||||
(void) FIFO_Put(&Receive_Buffer, data_byte);
|
(void) FIFO_Put(&Receive_Buffer, data_byte);
|
||||||
timer_reset(TIMER_SILENCE);
|
timer_elapsed_start(&Silence_Timer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,7 +156,7 @@ void rs485_bytes_send(
|
|||||||
}
|
}
|
||||||
/* Clear the Transmit Complete flag by writing a one to it. */
|
/* Clear the Transmit Complete flag by writing a one to it. */
|
||||||
BIT_SET(UCSR0A, TXC0);
|
BIT_SET(UCSR0A, TXC0);
|
||||||
timer_reset(TIMER_SILENCE);
|
timer_elapsed_start(&Silence_Timer);
|
||||||
led_off_delay(LED_2, 1);
|
led_off_delay(LED_2, 1);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
@@ -249,6 +261,7 @@ void rs485_init(
|
|||||||
{
|
{
|
||||||
FIFO_Init(&Receive_Buffer, &Receive_Buffer_Data[0],
|
FIFO_Init(&Receive_Buffer, &Receive_Buffer_Data[0],
|
||||||
(unsigned) sizeof(Receive_Buffer_Data));
|
(unsigned) sizeof(Receive_Buffer_Data));
|
||||||
|
timer_elapsed_start(&Silence_Timer);
|
||||||
rs485_rts_init();
|
rs485_rts_init();
|
||||||
rs485_usart_init();
|
rs485_usart_init();
|
||||||
rs485_init_nvdata();
|
rs485_init_nvdata();
|
||||||
|
|||||||
@@ -60,6 +60,8 @@ extern "C" {
|
|||||||
|
|
||||||
void rs485_turnaround_delay(
|
void rs485_turnaround_delay(
|
||||||
void);
|
void);
|
||||||
|
void rs485_silence_time_reset(void);
|
||||||
|
bool rs485_silence_time_elapsed(uint32_t milliseconds);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,262 @@
|
|||||||
|
/**************************************************************************
|
||||||
|
*
|
||||||
|
* Copyright (C) 2009 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.
|
||||||
|
*********************************************************************/
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "timer.h"
|
||||||
|
|
||||||
|
/* generic elapsed timer handling */
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Description: Sets the start time for an elapsed timer
|
||||||
|
* Returns: the value of the start timer
|
||||||
|
* Notes: none
|
||||||
|
*************************************************************************/
|
||||||
|
void timer_elapsed_start(
|
||||||
|
struct etimer *t)
|
||||||
|
{
|
||||||
|
uint32_t now = timer_milliseconds();
|
||||||
|
|
||||||
|
if (t) {
|
||||||
|
t->start = now;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Description: Gets the amount of elapsed time in milliseconds
|
||||||
|
* Returns: elapsed time in milliseconds
|
||||||
|
* Notes: none
|
||||||
|
*************************************************************************/
|
||||||
|
uint32_t timer_elapsed_time(
|
||||||
|
struct etimer *t)
|
||||||
|
{
|
||||||
|
uint32_t now = timer_milliseconds();
|
||||||
|
uint32_t delta = 0;
|
||||||
|
|
||||||
|
if (t) {
|
||||||
|
delta = now - t->start;
|
||||||
|
}
|
||||||
|
|
||||||
|
return delta;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Description: Sets the start time with an offset
|
||||||
|
* Returns: elapsed time in milliseconds
|
||||||
|
* Notes: none
|
||||||
|
*************************************************************************/
|
||||||
|
void timer_elapsed_start_offset(
|
||||||
|
struct etimer *t,
|
||||||
|
uint32_t offset)
|
||||||
|
{
|
||||||
|
uint32_t now = timer_milliseconds();
|
||||||
|
|
||||||
|
if (t) {
|
||||||
|
t->start = now + offset;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Description: Tests to see if time has elapsed
|
||||||
|
* Returns: true if time has elapsed
|
||||||
|
* Notes: none
|
||||||
|
*************************************************************************/
|
||||||
|
bool timer_elapsed_milliseconds(
|
||||||
|
struct etimer *t,
|
||||||
|
uint32_t milliseconds)
|
||||||
|
{
|
||||||
|
return (timer_elapsed_time(t) >= milliseconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Description: Tests to see if time has elapsed
|
||||||
|
* Returns: true if time has elapsed
|
||||||
|
* Notes: none
|
||||||
|
*************************************************************************/
|
||||||
|
bool timer_elapsed_seconds(
|
||||||
|
struct etimer *t,
|
||||||
|
uint32_t seconds)
|
||||||
|
{
|
||||||
|
uint32_t milliseconds = seconds;
|
||||||
|
|
||||||
|
milliseconds *= 1000L;
|
||||||
|
|
||||||
|
return timer_elapsed_milliseconds(t, milliseconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Description: Tests to see if time has elapsed
|
||||||
|
* Returns: true if time has elapsed
|
||||||
|
* Notes: none
|
||||||
|
*************************************************************************/
|
||||||
|
bool timer_elapsed_minutes(
|
||||||
|
struct etimer *t,
|
||||||
|
uint32_t minutes)
|
||||||
|
{
|
||||||
|
uint32_t milliseconds = minutes;
|
||||||
|
|
||||||
|
milliseconds *= 1000L;
|
||||||
|
milliseconds *= 60L;
|
||||||
|
|
||||||
|
return timer_elapsed_milliseconds(t, milliseconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Description: Starts an interval timer
|
||||||
|
* Returns: nothing
|
||||||
|
* Notes: none
|
||||||
|
*************************************************************************/
|
||||||
|
void timer_interval_start(
|
||||||
|
struct itimer *t,
|
||||||
|
uint32_t interval)
|
||||||
|
{
|
||||||
|
if (t) {
|
||||||
|
t->start = timer_milliseconds();
|
||||||
|
t->interval = interval;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Description: Starts an interval timer
|
||||||
|
* Returns: nothing
|
||||||
|
* Notes: none
|
||||||
|
*************************************************************************/
|
||||||
|
void timer_interval_start_seconds(
|
||||||
|
struct itimer *t,
|
||||||
|
uint32_t seconds)
|
||||||
|
{
|
||||||
|
uint32_t interval = seconds;
|
||||||
|
|
||||||
|
interval *= 1000L;
|
||||||
|
timer_interval_start(t, interval);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Description: Starts an interval timer
|
||||||
|
* Returns: nothing
|
||||||
|
* Notes: none
|
||||||
|
*************************************************************************/
|
||||||
|
void timer_interval_start_minutes(
|
||||||
|
struct itimer *t,
|
||||||
|
uint32_t minutes)
|
||||||
|
{
|
||||||
|
uint32_t interval = minutes;
|
||||||
|
|
||||||
|
interval *= 1000L;
|
||||||
|
interval *= 60L;
|
||||||
|
timer_interval_start(t, interval);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Description: Determines the amount of time that has elapsed
|
||||||
|
* Returns: elapsed milliseconds
|
||||||
|
* Notes: none
|
||||||
|
*************************************************************************/
|
||||||
|
uint32_t timer_interval_elapsed(
|
||||||
|
struct itimer *t)
|
||||||
|
{
|
||||||
|
uint32_t now = timer_milliseconds();
|
||||||
|
uint32_t delta = 0;
|
||||||
|
|
||||||
|
if (t) {
|
||||||
|
delta = now - t->start;
|
||||||
|
}
|
||||||
|
|
||||||
|
return delta;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Description: Determines the amount of time that has elapsed
|
||||||
|
* Returns: elapsed milliseconds
|
||||||
|
* Notes: none
|
||||||
|
*************************************************************************/
|
||||||
|
uint32_t timer_interval(
|
||||||
|
struct itimer *t)
|
||||||
|
{
|
||||||
|
uint32_t interval = 0;
|
||||||
|
|
||||||
|
if (t) {
|
||||||
|
interval = t->interval;
|
||||||
|
}
|
||||||
|
|
||||||
|
return interval;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Description: Tests to see if time has elapsed
|
||||||
|
* Returns: true if time has elapsed
|
||||||
|
* Notes: none
|
||||||
|
*************************************************************************/
|
||||||
|
bool timer_interval_expired(
|
||||||
|
struct itimer *t)
|
||||||
|
{
|
||||||
|
bool expired = false;
|
||||||
|
|
||||||
|
if (t) {
|
||||||
|
if (t->interval) {
|
||||||
|
expired = timer_interval_elapsed(t) >= t->interval;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return expired;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Description: Sets the interval value to zero so it never expires
|
||||||
|
* Returns: nothing
|
||||||
|
* Notes: none
|
||||||
|
*************************************************************************/
|
||||||
|
void timer_interval_no_expire(
|
||||||
|
struct itimer *t)
|
||||||
|
{
|
||||||
|
if (t) {
|
||||||
|
t->interval = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Description: Adds another interval to the start time. Used for cyclic
|
||||||
|
* timers that won't lose ticks.
|
||||||
|
* Returns: nothing
|
||||||
|
* Notes: none
|
||||||
|
*************************************************************************/
|
||||||
|
void timer_interval_reset(
|
||||||
|
struct itimer *t)
|
||||||
|
{
|
||||||
|
if (t) {
|
||||||
|
t->start += t->interval;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Description: Restarts the timer with the same interval
|
||||||
|
* Returns: nothing
|
||||||
|
* Notes: none
|
||||||
|
*************************************************************************/
|
||||||
|
void timer_interval_restart(
|
||||||
|
struct itimer *t)
|
||||||
|
{
|
||||||
|
if (t) {
|
||||||
|
t->start = timer_milliseconds();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,28 +30,69 @@
|
|||||||
|
|
||||||
/* Timer Module */
|
/* Timer Module */
|
||||||
|
|
||||||
|
/* elapsed timer structure */
|
||||||
|
struct etimer {
|
||||||
|
uint32_t start;
|
||||||
|
};
|
||||||
|
/* interval timer structure */
|
||||||
|
struct itimer {
|
||||||
|
uint32_t start;
|
||||||
|
uint32_t interval;
|
||||||
|
};
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif /* __cplusplus */
|
#endif /* __cplusplus */
|
||||||
|
|
||||||
|
/* these 3 functions are created in the hardware specific module */
|
||||||
void timer_init(
|
void timer_init(
|
||||||
void);
|
void);
|
||||||
unsigned long timer_milliseconds(
|
uint32_t timer_milliseconds(void);
|
||||||
unsigned index);
|
uint32_t timer_milliseconds_set(
|
||||||
|
uint32_t value);
|
||||||
|
|
||||||
|
/* these functions are in the generic timer.c module */
|
||||||
|
|
||||||
|
/* elapsed timer */
|
||||||
|
void timer_elapsed_start(
|
||||||
|
struct etimer *t);
|
||||||
|
void timer_elapsed_start_offset(
|
||||||
|
struct etimer *t,
|
||||||
|
uint32_t offset);
|
||||||
|
uint32_t timer_elapsed_time(
|
||||||
|
struct etimer *t);
|
||||||
bool timer_elapsed_milliseconds(
|
bool timer_elapsed_milliseconds(
|
||||||
unsigned index,
|
struct etimer *t,
|
||||||
unsigned long value);
|
uint32_t value);
|
||||||
bool timer_elapsed_seconds(
|
bool timer_elapsed_seconds(
|
||||||
unsigned index,
|
struct etimer *t,
|
||||||
unsigned long value);
|
uint32_t value);
|
||||||
bool timer_elapsed_minutes(
|
bool timer_elapsed_minutes(
|
||||||
unsigned index,
|
struct etimer *t,
|
||||||
unsigned long seconds);
|
uint32_t seconds);
|
||||||
unsigned long timer_milliseconds_set(
|
|
||||||
unsigned index,
|
/* interval timer */
|
||||||
unsigned long value);
|
void timer_interval_start(
|
||||||
unsigned long timer_reset(
|
struct itimer *t,
|
||||||
unsigned index);
|
uint32_t interval);
|
||||||
|
void timer_interval_start_seconds(
|
||||||
|
struct itimer *t,
|
||||||
|
uint32_t interval);
|
||||||
|
void timer_interval_start_minutes(
|
||||||
|
struct itimer *t,
|
||||||
|
uint32_t interval);
|
||||||
|
bool timer_interval_expired(
|
||||||
|
struct itimer *t);
|
||||||
|
uint32_t timer_interval(
|
||||||
|
struct itimer *t);
|
||||||
|
uint32_t timer_interval_elapsed(
|
||||||
|
struct itimer *t);
|
||||||
|
void timer_interval_no_expire(
|
||||||
|
struct itimer *t);
|
||||||
|
void timer_interval_reset(
|
||||||
|
struct itimer *t);
|
||||||
|
void timer_interval_restart(
|
||||||
|
struct itimer *t);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,11 +26,6 @@
|
|||||||
#include "hardware.h"
|
#include "hardware.h"
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
|
|
||||||
/* define various timers in timer.h file */
|
|
||||||
#ifndef MAX_MILLISECOND_TIMERS
|
|
||||||
#define MAX_MILLISECOND_TIMERS 2
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Timer2 Prescaling: 1, 8, 32, 64, 128, 256, or 1024 */
|
/* Timer2 Prescaling: 1, 8, 32, 64, 128, 256, or 1024 */
|
||||||
#define TIMER2_PRESCALER 128
|
#define TIMER2_PRESCALER 128
|
||||||
/* Count: Timer counts up to 0xFF and then signals overflow */
|
/* Count: Timer counts up to 0xFF and then signals overflow */
|
||||||
@@ -40,24 +35,8 @@
|
|||||||
#endif
|
#endif
|
||||||
#define TIMER2_COUNT (0xFF-TIMER2_TICKS)
|
#define TIMER2_COUNT (0xFF-TIMER2_TICKS)
|
||||||
|
|
||||||
/* counter for the various timers */
|
/* counter for the the timer which wraps every 49.7 days */
|
||||||
static volatile unsigned long Millisecond_Counter[MAX_MILLISECOND_TIMERS];
|
static volatile uint32_t Millisecond_Counter;
|
||||||
|
|
||||||
/*************************************************************************
|
|
||||||
* Description: Timer Interrupt Handler
|
|
||||||
* Returns: none
|
|
||||||
* Notes: Global interupts must be enabled
|
|
||||||
*************************************************************************/
|
|
||||||
static inline void timer_interrupt_handler(
|
|
||||||
void)
|
|
||||||
{
|
|
||||||
unsigned i; /* loop counter */
|
|
||||||
|
|
||||||
/* increment the tick count */
|
|
||||||
for (i = 0; i < MAX_MILLISECOND_TIMERS; i++) {
|
|
||||||
Millisecond_Counter[i]++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*************************************************************************
|
/*************************************************************************
|
||||||
* Description: Timer Interrupt Service Routine - Timer Overflowed!
|
* Description: Timer Interrupt Service Routine - Timer Overflowed!
|
||||||
@@ -68,7 +47,7 @@ ISR(TIMER2_OVF_vect)
|
|||||||
{
|
{
|
||||||
/* Set the counter for the next interrupt */
|
/* Set the counter for the next interrupt */
|
||||||
TCNT2 = TIMER2_COUNT;
|
TCNT2 = TIMER2_COUNT;
|
||||||
timer_interrupt_handler();
|
Millisecond_Counter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*************************************************************************
|
/*************************************************************************
|
||||||
@@ -76,20 +55,17 @@ ISR(TIMER2_OVF_vect)
|
|||||||
* Returns: none
|
* Returns: none
|
||||||
* Notes: none
|
* Notes: none
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
unsigned long timer_milliseconds_set(
|
uint32_t timer_milliseconds_set(
|
||||||
unsigned index,
|
uint32_t value)
|
||||||
unsigned long value)
|
|
||||||
{
|
{
|
||||||
uint8_t sreg = 0; /* holds interrupts pending */
|
uint8_t sreg = 0; /* holds interrupts pending */
|
||||||
unsigned long old_value = 0; /* return value */
|
uint32_t old_value = 0; /* return value */
|
||||||
|
|
||||||
if (index < MAX_MILLISECOND_TIMERS) {
|
sreg = SREG;
|
||||||
sreg = SREG;
|
__disable_interrupt();
|
||||||
__disable_interrupt();
|
old_value = Millisecond_Counter;
|
||||||
old_value = Millisecond_Counter[index];
|
Millisecond_Counter = value;
|
||||||
Millisecond_Counter[index] = value;
|
SREG = sreg;
|
||||||
SREG = sreg;
|
|
||||||
}
|
|
||||||
|
|
||||||
return old_value;
|
return old_value;
|
||||||
}
|
}
|
||||||
@@ -99,76 +75,25 @@ unsigned long timer_milliseconds_set(
|
|||||||
* Returns: none
|
* Returns: none
|
||||||
* Notes: none
|
* Notes: none
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
unsigned long timer_milliseconds(
|
uint32_t timer_milliseconds(void)
|
||||||
unsigned index)
|
|
||||||
{
|
{
|
||||||
unsigned long timer_value = 0; /* return value */
|
uint32_t timer_value = 0; /* return value */
|
||||||
uint8_t sreg = 0; /* holds interrupts pending */
|
uint8_t sreg = 0; /* holds interrupts pending */
|
||||||
|
|
||||||
if (index < MAX_MILLISECOND_TIMERS) {
|
sreg = SREG;
|
||||||
sreg = SREG;
|
__disable_interrupt();
|
||||||
__disable_interrupt();
|
timer_value = Millisecond_Counter;
|
||||||
timer_value = Millisecond_Counter[index];
|
SREG = sreg;
|
||||||
SREG = sreg;
|
|
||||||
}
|
|
||||||
|
|
||||||
return timer_value;
|
return timer_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*************************************************************************
|
|
||||||
* Description: compares the current time count with a value
|
|
||||||
* Returns: true if the time has elapsed
|
|
||||||
* Notes: none
|
|
||||||
*************************************************************************/
|
|
||||||
bool timer_elapsed_milliseconds(
|
|
||||||
unsigned index,
|
|
||||||
unsigned long value)
|
|
||||||
{
|
|
||||||
return (timer_milliseconds(index) >= value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*************************************************************************
|
|
||||||
* Description: compares the current time count with a value
|
|
||||||
* Returns: true if the time has elapsed
|
|
||||||
* Notes: none
|
|
||||||
*************************************************************************/
|
|
||||||
bool timer_elapsed_seconds(
|
|
||||||
unsigned index,
|
|
||||||
unsigned long seconds)
|
|
||||||
{
|
|
||||||
return ((timer_milliseconds(index) / 1000UL) >= seconds);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*************************************************************************
|
|
||||||
* Description: compares the current time count with a value
|
|
||||||
* Returns: true if the time has elapsed
|
|
||||||
* Notes: none
|
|
||||||
*************************************************************************/
|
|
||||||
bool timer_elapsed_minutes(
|
|
||||||
unsigned index,
|
|
||||||
unsigned long minutes)
|
|
||||||
{
|
|
||||||
return ((timer_milliseconds(index) / (1000UL * 60UL)) >= minutes);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*************************************************************************
|
|
||||||
* Description: Sets the timer counter to zero.
|
|
||||||
* Returns: none
|
|
||||||
* Notes: none
|
|
||||||
*************************************************************************/
|
|
||||||
unsigned long timer_reset(
|
|
||||||
unsigned index)
|
|
||||||
{
|
|
||||||
return timer_milliseconds_set(index, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*************************************************************************
|
/*************************************************************************
|
||||||
* Description: Initialization for Timer
|
* Description: Initialization for Timer
|
||||||
* Returns: none
|
* Returns: none
|
||||||
* Notes: none
|
* Notes: none
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
static void timer2_init(
|
void timer_init(void)
|
||||||
void)
|
|
||||||
{
|
{
|
||||||
/* Normal Operation */
|
/* Normal Operation */
|
||||||
TCCR2A = 0;
|
TCCR2A = 0;
|
||||||
@@ -210,14 +135,3 @@ static void timer2_init(
|
|||||||
/* Clear the Power Reduction Timer/Counter0 */
|
/* Clear the Power Reduction Timer/Counter0 */
|
||||||
BIT_CLEAR(PRR, PRTIM2);
|
BIT_CLEAR(PRR, PRTIM2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*************************************************************************
|
|
||||||
* Description: Initialization for Timer
|
|
||||||
* Returns: none
|
|
||||||
* Notes: none
|
|
||||||
*************************************************************************/
|
|
||||||
void timer_init(
|
|
||||||
void)
|
|
||||||
{
|
|
||||||
timer2_init();
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user