diff --git a/bacnet-stack/ports/atmega168/bacnet.ewp b/bacnet-stack/ports/atmega168/bacnet.ewp index 5d995186..9716296a 100644 --- a/bacnet-stack/ports/atmega168/bacnet.ewp +++ b/bacnet-stack/ports/atmega168/bacnet.ewp @@ -63,7 +63,7 @@ SCCStackSize - 0xff + 0x200 SCExtCStack @@ -239,7 +239,7 @@ CCDefines BACDL_MSTP - MAX_APDU=128 + MAX_APDU=50 BIG_ENDIAN=0 MAX_TSM_TRANSACTIONS=0 BACAPP_REAL @@ -247,6 +247,7 @@ BACAPP_ENUMERATED BACAPP_CHARACTER_STRING BACAPP_OBJECT_ID + WRITE_PROPERTY CCPreprocFile @@ -1953,6 +1954,9 @@ $PROJ_DIR$\avr035.h + + $PROJ_DIR$\..\..\src\bacapp.c + $PROJ_DIR$\..\..\src\bacdcode.c @@ -2031,6 +2035,9 @@ $PROJ_DIR$\..\..\src\whois.c + + $PROJ_DIR$\..\..\src\wp.c + diff --git a/bacnet-stack/ports/atmega168/hardware.h b/bacnet-stack/ports/atmega168/hardware.h index a402590c..19bf3364 100644 --- a/bacnet-stack/ports/atmega168/hardware.h +++ b/bacnet-stack/ports/atmega168/hardware.h @@ -43,6 +43,9 @@ #define LED_NPDU_INIT() BIT_SET(DDRD, DDD5) #define LED_NPDU_ON() BIT_CLEAR(PORTD, PD5) #define LED_NPDU_OFF() BIT_SET(PORTD, PD5) +/* #define LED_NPDU PORTD_Bit5 */ +/* #define LED_NPDU_OFF() {LED_NPDU = false;} */ +/* #define LED_NPDU_ON() {LED_NPDU = true;} */ #define LED_GREEN_INIT() BIT_SET(DDRD, DDD4) #define LED_GREEN_ON() BIT_CLEAR(PORTD, PD4) diff --git a/bacnet-stack/ports/atmega168/iar2gcc.h b/bacnet-stack/ports/atmega168/iar2gcc.h index e3779651..3a051408 100644 --- a/bacnet-stack/ports/atmega168/iar2gcc.h +++ b/bacnet-stack/ports/atmega168/iar2gcc.h @@ -42,6 +42,7 @@ #if defined(__IAR_SYSTEMS_ICC__) || defined(__IAR_SYSTEMS_ASM__) #include #include +/* BitValue is used alot in GCC examples */ #define _BV(bit_num) (1 << (bit_num)) /* inline function */ @@ -52,13 +53,11 @@ static inline void _delay_us( __delay_cycles(F_CPU / 1000000UL); } while (microseconds--); } +#endif -#else -/* GCC */ +/* Input/Output Registers */ +#if defined(__GNUC__) #include -#include -#include -#include typedef struct { unsigned char bit0:1; @@ -182,18 +181,46 @@ typedef struct { #define GPIOR2_Bit6 GPIO_BITREG(GPIOR2,6) #define GPIOR2_Bit7 GPIO_BITREG(GPIOR2,7) - /* FIXME: intrinsic routines: map to assembler for size/speed */ -#define __multiply_unsigned(x,y) ((x)*(y)) +#endif - - /* memory location */ -#define __eeprom __attribute__((section (".eeprom"))) -#define __flash __attribute__((progmem)) - /* __root means to not optimize or strip */ -#define __root +/* Global Interrupts */ +#if defined(__GNUC__) #define __enable_interrupt() sei() #define __disable_interrupt() cli() +#endif +/* Interrupts */ +#if defined(__ICCAVR__) +#define PRAGMA(x) _Pragma( #x ) +#define ISR(vec) PRAGMA( vector=vec ) __interrupt void handler_##vec(void) +#endif +#if defined(__GNUC__) +#include +#endif + +/* Flash */ +#if defined(__ICCAVR__) +#define FLASH_DECLARE(x) __flash x +#endif +#if defined(__GNUC__) +#define FLASH_DECLARE(x) x __attribute__((__progmem__)) +#endif + +/* EEPROM */ +#if defined(__ICCAVR__) +#define EEPROM_DECLARE(x) __eeprom x +#endif +#if defined(__GNUC__) +#include +#define EEPROM_DECLARE(x) x __attribute__((section (".eeprom"))) +#endif + +/* IAR intrinsic routines */ +#if defined(__GNUC__) + /* FIXME: intrinsic routines: map to assembler for size/speed */ + #define __multiply_unsigned(x,y) ((x)*(y)) + /* FIXME: __root means to not optimize or strip */ + #define __root #endif #endif diff --git a/bacnet-stack/ports/atmega168/main.c b/bacnet-stack/ports/atmega168/main.c index 01cb0ac0..c6e4598b 100644 --- a/bacnet-stack/ports/atmega168/main.c +++ b/bacnet-stack/ports/atmega168/main.c @@ -138,6 +138,63 @@ static void input_switch_read( } } +/* stack checking */ +#if STACK_CHECK_ENABLED +#if defined(__GNUC__) +extern uint8_t _end; +extern uint8_t __stack; +#define STACK_CANARY (0xC5) +void StackPaint(void) __attribute__ ((naked)) __attribute__ ((section (".init1"))); + +void StackPaint(void) +{ +#if 0 + uint8_t *p = &_end; + + while(p <= &__stack) + { + *p = STACK_CANARY; + p++; + } +#else + __asm volatile (" ldi r30,lo8(_end)\n" + " ldi r31,hi8(_end)\n" + " ldi r24,lo8(0xc5)\n" /* STACK_CANARY = 0xc5 */ + " ldi r25,hi8(__stack)\n" + " rjmp .cmp\n" + ".loop:\n" + " st Z+,r24\n" + ".cmp:\n" + " cpi r30,lo8(__stack)\n" + " cpc r31,r25\n" + " brlo .loop\n" + " breq .loop"::); +#endif +} + +static uint16_t StackCount(void) +{ + const uint8_t *p = &_end; + uint16_t c = 0; + + while(*p == STACK_CANARY && p <= &__stack) + { + p++; + c++; + } + + return c; +} + +static void Analog_Value_Task(void) +{ + extern float AV_Present_Value[MAX_ANALOG_VALUES]; + + AV_Present_Value[0] = (float)StackCount(); +} +#endif +#endif + static uint8_t PDUBuffer[MAX_MPDU]; int main( void) @@ -159,6 +216,9 @@ int main( /* BACnet handling */ pdu_len = datalink_receive(&src, &PDUBuffer[0], sizeof(PDUBuffer), 0); if (pdu_len) { +#if STACK_CHECK_ENABLED + Analog_Value_Task(); +#endif LED_NPDU_ON(); npdu_handler(&src, &PDUBuffer[0], pdu_len); LED_NPDU_OFF(); diff --git a/bacnet-stack/ports/atmega168/rs485.c b/bacnet-stack/ports/atmega168/rs485.c index 5946ff15..40bc9e2a 100644 --- a/bacnet-stack/ports/atmega168/rs485.c +++ b/bacnet-stack/ports/atmega168/rs485.c @@ -237,17 +237,18 @@ void RS485_Send_Data( while (!BIT_CHECK(UCSR0A, UDRE0)) { /* do nothing - wait until Tx buffer is empty */ } + /* Send the data byte */ UDR0 = *buffer; buffer++; nbytes--; } - /* Clear the Transmit Complete flag by writing a one to it. */ - BIT_SET(UCSR0A, TXC0); /* was the frame sent? */ while (!BIT_CHECK(UCSR0A, TXC0)) { /* do nothing - wait until the entire frame in the Transmit Shift Register has been shifted out */ } + /* Clear the Transmit Complete flag by writing a one to it. */ + BIT_SET(UCSR0A, TXC0); /* per MSTP spec, sort of */ Timer_Silence_Reset(); } diff --git a/bacnet-stack/ports/atmega168/timer.c b/bacnet-stack/ports/atmega168/timer.c index 51fcff4b..40c60b12 100644 --- a/bacnet-stack/ports/atmega168/timer.c +++ b/bacnet-stack/ports/atmega168/timer.c @@ -71,13 +71,7 @@ void Timer_Initialize( /* Timer interupt */ /* note: Global interupts must be enabled - sei() */ /* Timer Overflowed! Increment the time. */ -#if defined(__IAR_SYSTEMS_ICC__) || defined(__IAR_SYSTEMS_ASM__) -#pragma vector=TIMER0_OVF_vect -__interrupt void myTIMER0_OVF_vect( - ) -#else ISR(TIMER0_OVF_vect) -#endif { /* Set the counter for the next interrupt */ TCNT0 = TIMER_COUNT;