From a298d07b9a11720b191296c451120267142e949a Mon Sep 17 00:00:00 2001 From: skarg Date: Sun, 18 Jul 2010 19:08:44 +0000 Subject: [PATCH] Refactored test and bacnet into respective files. --- bacnet-stack/ports/bdk-atxx4-mstp/Makefile | 4 +- bacnet-stack/ports/bdk-atxx4-mstp/bacnet.c | 154 +++++++++++++++++++ bacnet-stack/ports/bdk-atxx4-mstp/bacnet.h | 41 +++++ bacnet-stack/ports/bdk-atxx4-mstp/main.c | 169 +-------------------- bacnet-stack/ports/bdk-atxx4-mstp/test.c | 83 ++++++++++ bacnet-stack/ports/bdk-atxx4-mstp/test.h | 41 +++++ 6 files changed, 324 insertions(+), 168 deletions(-) create mode 100644 bacnet-stack/ports/bdk-atxx4-mstp/bacnet.c create mode 100644 bacnet-stack/ports/bdk-atxx4-mstp/bacnet.h create mode 100644 bacnet-stack/ports/bdk-atxx4-mstp/test.c create mode 100644 bacnet-stack/ports/bdk-atxx4-mstp/test.h diff --git a/bacnet-stack/ports/bdk-atxx4-mstp/Makefile b/bacnet-stack/ports/bdk-atxx4-mstp/Makefile index 093ad1d9..dbd9aaea 100644 --- a/bacnet-stack/ports/bdk-atxx4-mstp/Makefile +++ b/bacnet-stack/ports/bdk-atxx4-mstp/Makefile @@ -61,6 +61,8 @@ CSRC = main.c \ eeprom.c \ seeprom.c \ dlmstp.c \ + test.c \ + bacnet.c \ device.c \ ai.c \ av.c \ @@ -156,7 +158,7 @@ endif ## BACnet options BFLAGS = -DBACDL_MSTP -BFLAGS += -DMAX_APDU=128 +BFLAGS += -DMAX_APDU=256 BFLAGS += -DBIG_ENDIAN=0 BFLAGS += -DMAX_TSM_TRANSACTIONS=0 #BFLAGS += -DCRC_USE_TABLE diff --git a/bacnet-stack/ports/bdk-atxx4-mstp/bacnet.c b/bacnet-stack/ports/bdk-atxx4-mstp/bacnet.c new file mode 100644 index 00000000..ba1687ec --- /dev/null +++ b/bacnet-stack/ports/bdk-atxx4-mstp/bacnet.c @@ -0,0 +1,154 @@ +/************************************************************************** +* +* Copyright (C) 2010 Steve Karg +* +* 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 +#include +/* hardware layer includes */ +#include "hardware.h" +#include "timer.h" +#include "seeprom.h" +#include "nvdata.h" +#include "rs485.h" +#include "input.h" +/* BACnet Stack includes */ +#include "datalink.h" +#include "npdu.h" +#include "handlers.h" +#include "client.h" +#include "txbuf.h" +#include "dcc.h" +#include "iam.h" +#include "device.h" +#include "ai.h" +#include "av.h" +#include "bi.h" +#include "bo.h" + +/* MAC Address of MS/TP */ +static uint8_t MSTP_MAC_Address; +/* timer for device communications control */ +static struct itimer DCC_Timer; +#define DCC_CYCLE_SECONDS 1 + +static bool seeprom_version_test( + void) +{ + uint16_t version = 0; + uint16_t id = 0; + bool status = false; + + seeprom_bytes_read(NV_SEEPROM_TYPE_0, (uint8_t *) & id, 2); + seeprom_bytes_read(NV_SEEPROM_VERSION_0, (uint8_t *) & version, 2); + + if ((id == SEEPROM_ID) && (version == SEEPROM_VERSION)) { + status = true; + } else { + version = SEEPROM_VERSION; + id = SEEPROM_ID; + seeprom_bytes_write(NV_SEEPROM_TYPE_0, (uint8_t *) & id, 2); + seeprom_bytes_write(NV_SEEPROM_VERSION_0, (uint8_t *) & version, 2); + } + + return status; +} + +void bacnet_init( + void) +{ + MSTP_MAC_Address = input_address(); + dlmstp_set_mac_address(MSTP_MAC_Address); + dlmstp_init(NULL); + + if (!seeprom_version_test()) { + /* invalid version data */ + } + /* initialize objects */ + Device_Init(); + + /* set up our confirmed service unrecognized service handler - required! */ + apdu_set_unrecognized_service_handler_handler + (handler_unrecognized_service); + /* we need to handle who-is to support dynamic device binding */ + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_HAS, handler_who_has); + /* Set the handlers for any confirmed services that we support. */ + /* We must implement read property - it's required! */ + apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY, + handler_read_property); + apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROP_MULTIPLE, + handler_read_property_multiple); + apdu_set_confirmed_handler(SERVICE_CONFIRMED_REINITIALIZE_DEVICE, + handler_reinitialize_device); + apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY, + handler_write_property); + /* handle communication so we can shutup when asked */ + apdu_set_confirmed_handler(SERVICE_CONFIRMED_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]); +} + +static uint8_t PDUBuffer[MAX_MPDU]; +void bacnet_task( + void) +{ + uint8_t mstp_mac_address; + uint16_t pdu_len; + BACNET_ADDRESS src; /* source address */ + uint8_t value; + bool button_value; + uint8_t i; + BACNET_BINARY_PV binary_value = BINARY_INACTIVE; + + + mstp_mac_address = input_address(); + if (MSTP_MAC_Address != mstp_mac_address) { + /* address changed! */ + MSTP_MAC_Address = mstp_mac_address; + dlmstp_set_mac_address(MSTP_MAC_Address); + Send_I_Am(&Handler_Transmit_Buffer[0]); + } + /* handle the inputs */ + value = adc_result(7); + Analog_Input_Present_Value_Set(0, value); + for (i = 0; i < 5; i++) { + button_value = input_button_value(i); + if (button_value) { + binary_value = BINARY_ACTIVE; + } + Binary_Input_Present_Value_Set(i, binary_value); + } + /* handle the communication timer */ + if (timer_interval_expired(&DCC_Timer)) { + timer_interval_reset(&DCC_Timer); + dcc_timer_seconds(DCC_CYCLE_SECONDS); + } + /* handle the messaging */ + pdu_len = datalink_receive(&src, &PDUBuffer[0], sizeof(PDUBuffer), 0); + if (pdu_len) { + npdu_handler(&src, &PDUBuffer[0], pdu_len); + } +} diff --git a/bacnet-stack/ports/bdk-atxx4-mstp/bacnet.h b/bacnet-stack/ports/bdk-atxx4-mstp/bacnet.h new file mode 100644 index 00000000..ee352604 --- /dev/null +++ b/bacnet-stack/ports/bdk-atxx4-mstp/bacnet.h @@ -0,0 +1,41 @@ +/************************************************************************** +* +* Copyright (C) 2010 Steve Karg +* +* 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 BACNET_H +#define BACNET_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + + void bacnet_init( + void); + void bacnet_task( + void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif diff --git a/bacnet-stack/ports/bdk-atxx4-mstp/main.c b/bacnet-stack/ports/bdk-atxx4-mstp/main.c index f004da5d..f1350ab7 100644 --- a/bacnet-stack/ports/bdk-atxx4-mstp/main.c +++ b/bacnet-stack/ports/bdk-atxx4-mstp/main.c @@ -25,7 +25,6 @@ #include #include -#include #include "hardware.h" #include "init.h" #include "stack.h" @@ -35,181 +34,17 @@ #include "adc.h" #include "nvdata.h" #include "timer.h" -#include "dcc.h" #include "rs485.h" #include "serial.h" -#include "datalink.h" -#include "npdu.h" -#include "handlers.h" -#include "client.h" -#include "txbuf.h" -#include "iam.h" -#include "device.h" -#include "ai.h" -#include "av.h" -#include "bi.h" -#include "bo.h" +#include "bacnet.h" +#include "test.h" /* local version override */ const char *BACnet_Version = "1.0"; -/* MAC Address of MS/TP */ -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: http://www.avrfreaks.net/wiki/index.php/Documentation:AVR_GCC/IarToAvrgcc*/ -bool seeprom_version_test( - void) -{ - uint16_t version = 0; - uint16_t id = 0; - bool status = false; - - seeprom_bytes_read(NV_SEEPROM_TYPE_0, (uint8_t *) & id, 2); - seeprom_bytes_read(NV_SEEPROM_VERSION_0, (uint8_t *) & version, 2); - - if ((id == SEEPROM_ID) && (version == SEEPROM_VERSION)) { - status = true; - } else { - version = SEEPROM_VERSION; - id = SEEPROM_ID; - seeprom_bytes_write(NV_SEEPROM_TYPE_0, (uint8_t *) & id, 2); - seeprom_bytes_write(NV_SEEPROM_VERSION_0, (uint8_t *) & version, 2); - } - - return status; -} - -static void bacnet_init( - void) -{ - MSTP_MAC_Address = input_address(); - dlmstp_set_mac_address(MSTP_MAC_Address); - dlmstp_init(NULL); - - if (!seeprom_version_test()) { - /* invalid version data */ - } - /* initialize objects */ - Device_Init(); - - /* set up our confirmed service unrecognized service handler - required! */ - apdu_set_unrecognized_service_handler_handler - (handler_unrecognized_service); - /* we need to handle who-is to support dynamic device binding */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_HAS, handler_who_has); - /* Set the handlers for any confirmed services that we support. */ - /* We must implement read property - it's required! */ - apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY, - handler_read_property); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROP_MULTIPLE, - handler_read_property_multiple); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_REINITIALIZE_DEVICE, - handler_reinitialize_device); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY, - handler_write_property); - /* handle communication so we can shutup when asked */ - apdu_set_confirmed_handler(SERVICE_CONFIRMED_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]); -} - -static uint8_t PDUBuffer[MAX_MPDU]; -static void bacnet_task( - void) -{ - uint8_t mstp_mac_address; - uint16_t pdu_len; - BACNET_ADDRESS src; /* source address */ - uint8_t value; - bool button_value; - uint8_t i; - BACNET_BINARY_PV binary_value = BINARY_INACTIVE; - - - mstp_mac_address = input_address(); - if (MSTP_MAC_Address != mstp_mac_address) { - /* address changed! */ - MSTP_MAC_Address = mstp_mac_address; - dlmstp_set_mac_address(MSTP_MAC_Address); - Send_I_Am(&Handler_Transmit_Buffer[0]); - } - /* handle the inputs */ - value = adc_result(7); - Analog_Input_Present_Value_Set(0, value); - for (i = 0; i < 5; i++) { - button_value = input_button_value(i); - if (button_value) { - binary_value = BINARY_ACTIVE; - } - Binary_Input_Present_Value_Set(i, binary_value); - } - /* handle the communication timer */ - if (timer_interval_expired(&DCC_Timer)) { - timer_interval_reset(&DCC_Timer); - dcc_timer_seconds(DCC_CYCLE_SECONDS); - } - /* handle the messaging */ - pdu_len = datalink_receive(&src, &PDUBuffer[0], sizeof(PDUBuffer), 0); - if (pdu_len) { - npdu_handler(&src, &PDUBuffer[0], pdu_len); - } -} - -void test_init( - void) -{ - timer_interval_start_seconds(&Test_Timer, 1); -} - -void test_task( - void) -{ - char buffer[32] = "BACnet: 0000000\r\n"; - uint8_t nbytes = 17; - uint8_t data_register = 0; - - if (timer_interval_expired(&Test_Timer)) { - timer_interval_reset(&Test_Timer); - buffer[8] = (MSTP_MAC_Address & BIT0) ? '1' : '0'; - buffer[9] = (MSTP_MAC_Address & BIT1) ? '1' : '0'; - buffer[10] = (MSTP_MAC_Address & BIT2) ? '1' : '0'; - buffer[11] = (MSTP_MAC_Address & BIT3) ? '1' : '0'; - buffer[12] = (MSTP_MAC_Address & BIT4) ? '1' : '0'; - buffer[13] = (MSTP_MAC_Address & BIT5) ? '1' : '0'; - buffer[14] = (MSTP_MAC_Address & BIT6) ? '1' : '0'; - serial_bytes_send((uint8_t *) buffer, nbytes); - } - if (serial_byte_get(&data_register)) { - /* echo the character */ - serial_byte_send(data_register); - if (data_register == '0') { - Binary_Output_Present_Value_Set(0, 0, BINARY_INACTIVE); - Binary_Output_Present_Value_Set(1, 0, BINARY_INACTIVE); - } - if (data_register == '1') { - Binary_Output_Present_Value_Set(0, 0, BINARY_ACTIVE); - Binary_Output_Present_Value_Set(1, 0, BINARY_ACTIVE); - } - if (data_register == '2') { - Binary_Output_Present_Value_Set(0, 0, BINARY_NULL); - Binary_Output_Present_Value_Set(1, 0, BINARY_NULL); - } - serial_byte_send('\r'); - serial_byte_send('\n'); - serial_byte_transmit_complete(); - } -} - int main( void) { diff --git a/bacnet-stack/ports/bdk-atxx4-mstp/test.c b/bacnet-stack/ports/bdk-atxx4-mstp/test.c new file mode 100644 index 00000000..e1192cff --- /dev/null +++ b/bacnet-stack/ports/bdk-atxx4-mstp/test.c @@ -0,0 +1,83 @@ +/************************************************************************** +* +* Copyright (C) 2010 Steve Karg +* +* 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 +#include +#include +#include "hardware.h" +#include "timer.h" +#include "serial.h" +#include "input.h" +#include "bo.h" + +/* timer for test task */ +static struct itimer Test_Timer; +/* MAC Address of MS/TP */ +static uint8_t MSTP_MAC_Address; + +void test_init( + void) +{ + timer_interval_start_seconds(&Test_Timer, 1); +} + +void test_task( + void) +{ + char buffer[32] = "BACnet: 0000000\r\n"; + uint8_t nbytes = 17; + uint8_t data_register = 0; + + if (timer_interval_expired(&Test_Timer)) { + timer_interval_reset(&Test_Timer); + MSTP_MAC_Address = input_address(); + buffer[8] = (MSTP_MAC_Address & BIT0) ? '1' : '0'; + buffer[9] = (MSTP_MAC_Address & BIT1) ? '1' : '0'; + buffer[10] = (MSTP_MAC_Address & BIT2) ? '1' : '0'; + buffer[11] = (MSTP_MAC_Address & BIT3) ? '1' : '0'; + buffer[12] = (MSTP_MAC_Address & BIT4) ? '1' : '0'; + buffer[13] = (MSTP_MAC_Address & BIT5) ? '1' : '0'; + buffer[14] = (MSTP_MAC_Address & BIT6) ? '1' : '0'; + serial_bytes_send((uint8_t *) buffer, nbytes); + } + if (serial_byte_get(&data_register)) { + /* echo the character */ + serial_byte_send(data_register); + if (data_register == '0') { + Binary_Output_Present_Value_Set(0, 0, BINARY_INACTIVE); + Binary_Output_Present_Value_Set(1, 0, BINARY_INACTIVE); + } + if (data_register == '1') { + Binary_Output_Present_Value_Set(0, 0, BINARY_ACTIVE); + Binary_Output_Present_Value_Set(1, 0, BINARY_ACTIVE); + } + if (data_register == '2') { + Binary_Output_Present_Value_Set(0, 0, BINARY_NULL); + Binary_Output_Present_Value_Set(1, 0, BINARY_NULL); + } + serial_byte_send('\r'); + serial_byte_send('\n'); + serial_byte_transmit_complete(); + } +} diff --git a/bacnet-stack/ports/bdk-atxx4-mstp/test.h b/bacnet-stack/ports/bdk-atxx4-mstp/test.h new file mode 100644 index 00000000..3dccc856 --- /dev/null +++ b/bacnet-stack/ports/bdk-atxx4-mstp/test.h @@ -0,0 +1,41 @@ +/************************************************************************** +* +* Copyright (C) 2010 Steve Karg +* +* 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 TEST_H +#define TEST_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + + void test_init( + void); + void test_task( + void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif