Corrected bug in ATmega168 port demo project, which required some code reduction to fit using GCC compiler.

This commit is contained in:
skarg
2008-03-14 22:15:49 +00:00
parent 01438b7aa6
commit c8677814a6
6 changed files with 89 additions and 108 deletions
+3 -3
View File
@@ -48,9 +48,9 @@ CORESRC = \
$(BACNET_CORE)/abort.c \ $(BACNET_CORE)/abort.c \
$(BACNET_CORE)/reject.c \ $(BACNET_CORE)/reject.c \
$(BACNET_CORE)/bacerror.c \ $(BACNET_CORE)/bacerror.c \
$(BACNET_CORE)/bacapp.c \ $(BACNET_CORE)/bacapp.c
$(BACNET_CORE)/version.c
# $(BACNET_CORE)/version.c
# $(BACNET_CORE)/bacprop.c \ # $(BACNET_CORE)/bacprop.c \
# $(BACNET_CORE)/bactext.c \ # $(BACNET_CORE)/bactext.c \
# $(BACNET_CORE)/datetime.c \ # $(BACNET_CORE)/datetime.c \
@@ -98,6 +98,7 @@ BFLAGS += -DBIG_ENDIAN=0
BFLAGS += -DMAX_TSM_TRANSACTIONS=0 BFLAGS += -DMAX_TSM_TRANSACTIONS=0
#BFLAGS += -DCRC_USE_TABLE #BFLAGS += -DCRC_USE_TABLE
BFLAGS += -DBACAPP_REAL BFLAGS += -DBACAPP_REAL
BFLAGS += -DMAX_ANALOG_VALUES=10
CFLAGS = $(COMMON) CFLAGS = $(COMMON)
# dead code removal # dead code removal
CFLAGS += -ffunction-sections -fdata-sections CFLAGS += -ffunction-sections -fdata-sections
@@ -162,7 +163,6 @@ size: ${TARGET_ELF}
## Clean target ## Clean target
.PHONY: clean .PHONY: clean
clean: clean:
touch Makefile
-rm -rf $(OBJECTS) $(TARGET_ELF) dep/* -rm -rf $(OBJECTS) $(TARGET_ELF) dep/*
-rm -rf $(LIBRARY) $(COREOBJ) $(LIBRARY:.a=.lst) -rm -rf $(LIBRARY) $(COREOBJ) $(LIBRARY:.a=.lst)
-rm -rf $(TARGET).hex $(TARGET).eep $(TARGET).lst $(TARGET).map -rm -rf $(TARGET).hex $(TARGET).eep $(TARGET).lst $(TARGET).map
+7 -12
View File
@@ -34,12 +34,11 @@
#include "config.h" /* the custom stuff */ #include "config.h" /* the custom stuff */
#include "wp.h" #include "wp.h"
#define MAX_ANALOG_VALUES 9 #if (MAX_ANALOG_VALUES > 10)
#if (MAX_ANALOG_VALUES > 9)
#error Modify the Analog_Value_Name to handle multiple digits #error Modify the Analog_Value_Name to handle multiple digits
#endif #endif
float Present_Value[MAX_ANALOG_VALUES]; float AV_Present_Value[MAX_ANALOG_VALUES];
/* we simply have 0-n object instances. Yours might be */ /* we simply have 0-n object instances. Yours might be */
/* more complex, and then you need validate that the */ /* more complex, and then you need validate that the */
@@ -83,14 +82,11 @@ unsigned Analog_Value_Instance_To_Index(
char *Analog_Value_Name( char *Analog_Value_Name(
uint32_t object_instance) uint32_t object_instance)
{ {
static char text_string[5] = "AV-0"; /* okay for single thread */ static char text_string[5] = "AV-"; /* okay for single thread */
if (object_instance < MAX_ANALOG_VALUES) { text_string[3] = '0' + (uint8_t) object_instance;
text_string[3] = '0' + (uint8_t) object_instance;
return text_string;
}
return NULL; return text_string;
} }
/* return apdu len, or -1 on error */ /* return apdu len, or -1 on error */
@@ -114,7 +110,6 @@ int Analog_Value_Encode_Property_APDU(
object_instance); object_instance);
break; break;
case PROP_OBJECT_NAME: case PROP_OBJECT_NAME:
case PROP_DESCRIPTION:
characterstring_init_ansi(&char_string, characterstring_init_ansi(&char_string,
Analog_Value_Name(object_instance)); Analog_Value_Name(object_instance));
apdu_len = apdu_len =
@@ -127,7 +122,7 @@ int Analog_Value_Encode_Property_APDU(
case PROP_PRESENT_VALUE: case PROP_PRESENT_VALUE:
object_index = Analog_Value_Instance_To_Index(object_instance); object_index = Analog_Value_Instance_To_Index(object_instance);
apdu_len = apdu_len =
encode_application_real(&apdu[0], Present_Value[object_index]); encode_application_real(&apdu[0], AV_Present_Value[object_index]);
break; break;
case PROP_STATUS_FLAGS: case PROP_STATUS_FLAGS:
bitstring_init(&bit_string); bitstring_init(&bit_string);
@@ -184,7 +179,7 @@ bool Analog_Value_Write_Property(
if (value.tag == BACNET_APPLICATION_TAG_REAL) { if (value.tag == BACNET_APPLICATION_TAG_REAL) {
object_index = object_index =
Analog_Value_Instance_To_Index(wp_data->object_instance); Analog_Value_Instance_To_Index(wp_data->object_instance);
Present_Value[object_index] = value.type.Real; AV_Present_Value[object_index] = value.type.Real;
status = true; status = true;
} else { } else {
*error_class = ERROR_CLASS_PROPERTY; *error_class = ERROR_CLASS_PROPERTY;
File diff suppressed because one or more lines are too long
+12 -19
View File
@@ -222,7 +222,6 @@ int Device_Encode_Property_APDU(
Object_Instance_Number); Object_Instance_Number);
break; break;
case PROP_OBJECT_NAME: case PROP_OBJECT_NAME:
case PROP_DESCRIPTION:
characterstring_init_ansi(&char_string, Object_Name); characterstring_init_ansi(&char_string, Object_Name);
apdu_len = apdu_len =
encode_application_character_string(&apdu[0], &char_string); encode_application_character_string(&apdu[0], &char_string);
@@ -249,7 +248,7 @@ int Device_Encode_Property_APDU(
encode_application_character_string(&apdu[0], &char_string); encode_application_character_string(&apdu[0], &char_string);
break; break;
case PROP_FIRMWARE_REVISION: case PROP_FIRMWARE_REVISION:
characterstring_init_ansi(&char_string, BACnet_Version); characterstring_init_ansi(&char_string, BACNET_VERSION_TEXT);
apdu_len = apdu_len =
encode_application_character_string(&apdu[0], &char_string); encode_application_character_string(&apdu[0], &char_string);
break; break;
@@ -304,24 +303,16 @@ int Device_Encode_Property_APDU(
/* your maximum APDU size. */ /* your maximum APDU size. */
else if (array_index == BACNET_ARRAY_ALL) { else if (array_index == BACNET_ARRAY_ALL) {
for (i = 1; i <= count; i++) { for (i = 1; i <= count; i++) {
if (Device_Object_List_Identifier(i, &object_type, Device_Object_List_Identifier(i, &object_type, &instance);
&instance)) { len =
len = encode_application_object_id(&apdu[apdu_len],
encode_application_object_id(&apdu[apdu_len], object_type, instance);
object_type, instance); apdu_len += len;
apdu_len += len; /* assume next one is the same size as this one */
/* assume next one is the same size as this one */ /* can we all fit into the APDU? */
/* can we all fit into the APDU? */ if ((apdu_len + len) >= MAX_APDU) {
if ((apdu_len + len) >= MAX_APDU) {
*error_class = ERROR_CLASS_SERVICES;
*error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT;
apdu_len = -1;
break;
}
} else {
/* error: internal error? */
*error_class = ERROR_CLASS_SERVICES; *error_class = ERROR_CLASS_SERVICES;
*error_code = ERROR_CODE_OTHER; *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT;
apdu_len = -1; apdu_len = -1;
break; break;
} }
@@ -367,10 +358,12 @@ int Device_Encode_Property_APDU(
apdu_len = apdu_len =
encode_application_unsigned(&apdu[0], dlmstp_max_master()); encode_application_unsigned(&apdu[0], dlmstp_max_master());
break; break;
#if 0
case 9600: case 9600:
apdu_len = apdu_len =
encode_application_unsigned(&apdu[0], RS485_Get_Baud_Rate()); encode_application_unsigned(&apdu[0], RS485_Get_Baud_Rate());
break; break;
#endif
default: default:
*error_class = ERROR_CLASS_PROPERTY; *error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_UNKNOWN_PROPERTY; *error_code = ERROR_CODE_UNKNOWN_PROPERTY;
+38 -43
View File
@@ -49,33 +49,31 @@
or sets the error, and returns -1 */ or sets the error, and returns -1 */
int Encode_Property_APDU( int Encode_Property_APDU(
uint8_t * apdu, uint8_t * apdu,
BACNET_OBJECT_TYPE object_type, BACNET_READ_PROPERTY_DATA *rp_data,
uint32_t object_instance,
BACNET_PROPERTY_ID property,
int32_t array_index,
BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CLASS * error_class,
BACNET_ERROR_CODE * error_code) BACNET_ERROR_CODE * error_code)
{ {
int apdu_len = -1; int apdu_len = -1;
/* initialize the default return values */
*error_class = ERROR_CLASS_OBJECT;
*error_code = ERROR_CODE_UNKNOWN_OBJECT;
/* handle each object type */ /* handle each object type */
switch (object_type) { switch (rp_data->object_type) {
case OBJECT_DEVICE: case OBJECT_DEVICE:
if (Device_Valid_Object_Instance_Number(object_instance)) { if (Device_Valid_Object_Instance_Number(rp_data->object_instance)) {
apdu_len = apdu_len =
Device_Encode_Property_APDU(&apdu[0], property, Device_Encode_Property_APDU(&apdu[0],
array_index, error_class, error_code); rp_data->object_property,
rp_data->array_index,
error_class, error_code);
} }
break; break;
case OBJECT_ANALOG_VALUE: case OBJECT_ANALOG_VALUE:
if (Analog_Value_Valid_Instance(object_instance)) { if (Analog_Value_Valid_Instance(rp_data->object_instance)) {
apdu_len = apdu_len =
Analog_Value_Encode_Property_APDU(&apdu[0], Analog_Value_Encode_Property_APDU(&apdu[0],
object_instance, property, array_index, error_class, rp_data->object_instance,
error_code); rp_data->object_property,
rp_data->array_index,
error_class, error_code);
} }
break; break;
default: default:
@@ -99,51 +97,48 @@ void handler_read_property(
int property_len = 0; int property_len = 0;
int pdu_len = 0; int pdu_len = 0;
BACNET_NPDU_DATA npdu_data; BACNET_NPDU_DATA npdu_data;
bool error = false;
int bytes_sent = 0; int bytes_sent = 0;
BACNET_ERROR_CLASS error_class = ERROR_CLASS_OBJECT; BACNET_ERROR_CLASS error_class = ERROR_CLASS_OBJECT;
BACNET_ERROR_CODE error_code = ERROR_CODE_UNKNOWN_OBJECT; BACNET_ERROR_CODE error_code = ERROR_CODE_UNKNOWN_OBJECT;
BACNET_ADDRESS my_address; BACNET_ADDRESS my_address;
len = rp_decode_service_request(service_request, service_len, &data);
/* encode the NPDU portion of the packet */ /* encode the NPDU portion of the packet */
datalink_get_my_address(&my_address); datalink_get_my_address(&my_address);
npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL); npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL);
pdu_len = pdu_len =
npdu_encode_pdu(&Handler_Transmit_Buffer[0], src, &my_address, npdu_encode_pdu(&Handler_Transmit_Buffer[0], src, &my_address,
&npdu_data); &npdu_data);
if (len < 0) { if (service_data->segmented_message) {
/* bad decoding - send an abort */
len =
abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, ABORT_REASON_OTHER, true);
} else if (service_data->segmented_message) {
/* we don't support segmentation - send an abort */ /* we don't support segmentation - send an abort */
len = len =
abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, service_data->invoke_id, ABORT_REASON_SEGMENTATION_NOT_SUPPORTED,
true); true);
} else { goto RP_ABORT;
/* most cases will be error */
error = true;
ack_len =
rp_ack_encode_apdu_init(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, &data);
/* FIXME: add buffer len as passed into function or use smart buffer */
property_len =
Encode_Property_APDU(&Handler_Transmit_Buffer[pdu_len + ack_len],
data.object_type, data.object_instance, data.object_property,
data.array_index, &error_class, &error_code);
if (len >= 0) {
len =
rp_ack_encode_apdu_object_property_end(&Handler_Transmit_Buffer
[pdu_len + property_len + ack_len]);
len += ack_len + property_len;
error = false;
}
} }
if (error) { len = rp_decode_service_request(service_request, service_len, &data);
switch (len) { if (len < 0) {
/* bad decoding - send an abort */
len =
abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, ABORT_REASON_OTHER, true);
goto RP_ABORT;
}
/* most cases will be error */
ack_len =
rp_ack_encode_apdu_init(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, &data);
/* FIXME: add buffer len as passed into function or use smart buffer */
property_len =
Encode_Property_APDU(&Handler_Transmit_Buffer[pdu_len + ack_len],
&data, &error_class, &error_code);
if (property_len >= 0) {
len =
rp_ack_encode_apdu_object_property_end(&Handler_Transmit_Buffer
[pdu_len + property_len + ack_len]);
len += ack_len + property_len;
} else {
switch (property_len) {
/* BACnet APDU too small to fit data, so proper response is Abort */ /* BACnet APDU too small to fit data, so proper response is Abort */
case -2: case -2:
len = len =
@@ -151,7 +146,6 @@ void handler_read_property(
service_data->invoke_id, service_data->invoke_id,
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true);
break; break;
case -1:
default: default:
len = len =
bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
@@ -160,6 +154,7 @@ void handler_read_property(
break; break;
} }
} }
RP_ABORT:
pdu_len += len; pdu_len += len;
bytes_sent = bytes_sent =
datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0], datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0],
+28 -30
View File
@@ -34,10 +34,19 @@
#include "iam.h" #include "iam.h"
#include "device.h" #include "device.h"
const char *BACnet_Version = "1.0";
/* 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*/
#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_GREEN_INIT() BIT_SET(DDRD, DDD4)
#define LED_GREEN_ON() BIT_CLEAR(PORTD, PD4)
#define LED_GREEN_OFF() BIT_SET(PORTD, PD4)
/* dummy function */ /* dummy function */
bool dcc_communication_enabled( bool dcc_communication_enabled(
void) void)
@@ -81,43 +90,24 @@ static void init(
BIT_CLEAR(MCUSR, WDRF); BIT_CLEAR(MCUSR, WDRF);
WDTCSR = 0; WDTCSR = 0;
/* Configure USART */ /* Configure Specialized Hardware */
RS485_Initialize(); RS485_Initialize();
/* configure one LED for NPDU indication */
/* default: off, output */
LED_NPDU_OFF();
LED_NPDU_INIT();
/* Configure Software active LED */
LED_GREEN_INIT();
LED_GREEN_ON();
/* Configure Timer0 for millisecond timer */ /* Configure Timer0 for millisecond timer */
Timer_Initialize(); Timer_Initialize();
/* Set the LED ports OFF */
BIT_SET(PORTD, PD4);
BIT_SET(PORTD, PD5);
/* Configure the LED ports as outputs */
BIT_SET(DDRD, DDD4);
BIT_SET(DDRD, DDD5);
/* Enable global interrupts */ /* Enable global interrupts */
__enable_interrupt(); __enable_interrupt();
} }
static uint8_t NPDU_Timer;
static void NDPU_Timers(
void)
{
if (NPDU_Timer) {
NPDU_Timer--;
if (NPDU_Timer == 0) {
BIT_SET(PORTD, PD5);
}
}
}
static void NPDU_LED_On(
void)
{
BIT_CLEAR(PORTD, PD5);
NPDU_Timer = 20;
}
static void task_milliseconds( static void task_milliseconds(
void) void)
{ {
@@ -125,7 +115,6 @@ static void task_milliseconds(
Timer_Milliseconds--; Timer_Milliseconds--;
/* add other millisecond timer tasks here */ /* add other millisecond timer tasks here */
RS485_LED_Timers(); RS485_LED_Timers();
NDPU_Timers();
} }
} }
@@ -152,6 +141,13 @@ static void input_switch_read(
} }
} }
static void Analog_Value_Task(void)
{
extern float AV_Present_Value[MAX_ANALOG_VALUES];
AV_Present_Value[0] = 3.14159F;
}
static uint8_t PDUBuffer[MAX_MPDU]; static uint8_t PDUBuffer[MAX_MPDU];
int main( int main(
void) void)
@@ -169,12 +165,14 @@ int main(
for (;;) { for (;;) {
input_switch_read(); input_switch_read();
task_milliseconds(); task_milliseconds();
Analog_Value_Task();
/* other tasks */ /* other tasks */
/* BACnet handling */ /* BACnet handling */
pdu_len = datalink_receive(&src, &PDUBuffer[0], sizeof(PDUBuffer), 0); pdu_len = datalink_receive(&src, &PDUBuffer[0], sizeof(PDUBuffer), 0);
if (pdu_len) { if (pdu_len) {
LED_NPDU_ON();
npdu_handler(&src, &PDUBuffer[0], pdu_len); npdu_handler(&src, &PDUBuffer[0], pdu_len);
NPDU_LED_On(); LED_NPDU_OFF();
} }
} }
} }