/************************************************************************** * * Copyright (C) 2007 Steve Karg * Portions of the AT91SAM7S startup code were developed by James P Lynch. * * 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. * *********************************************************************/ /* hardware specific */ #include "AT91SAM7S256.h" #include "board.h" #include "timer.h" /* standard libraries */ #include #include #include #include /* BACnet */ #include "rs485.h" #include "datalink.h" #include "npdu.h" #include "apdu.h" #include "dcc.h" #include "iam.h" #include "handlers.h" #include "device.h" #include "dcc.h" #include "iam.h" #include "txbuf.h" // ******************************************************* // FIXME: use header files? External References // ******************************************************* extern void LowLevelInit(void); extern unsigned enableIRQ(void); extern unsigned enableFIQ(void); // ******************************************************* // FIXME: use header files? Global Variables // ******************************************************* unsigned int FiqCount = 0; static unsigned long LED_Timer_1 = 0; static unsigned long LED_Timer_2 = 0; static unsigned long LED_Timer_3 = 0; static unsigned long LED_Timer_4 = 1000; static unsigned long DCC_Timer = 1000; static void millisecond_timer(void) { while (Timer_Milliseconds) { Timer_Milliseconds--; if (LED_Timer_1) { LED_Timer_1--; } if (LED_Timer_2) { LED_Timer_2--; } if (LED_Timer_3) { LED_Timer_3--; } if (LED_Timer_4) { LED_Timer_4--; } if (DCC_Timer) { DCC_Timer--; } } /* note: MS/TP silence timer is updated in ISR */ } static void init(void) { /* Initialize the Parallel I/O Controller A Peripheral Clock */ volatile AT91PS_PMC pPMC = AT91C_BASE_PMC; pPMC->PMC_PCER = pPMC->PMC_PCSR | (1<PIO_PER = LED_MASK | SW1_MASK; // PIO Output Enable Register // sets pins P0 - P3 to outputs pPIO->PIO_OER = LED_MASK; // PIO Set Output Data Register // turns off the four LEDs pPIO->PIO_SODR = LED_MASK; // Select PA19 (pushbutton) to be FIQ function (Peripheral B) pPIO->PIO_BSR = SW1_MASK; // Set up the AIC registers for FIQ (pushbutton SW1) volatile AT91PS_AIC pAIC = AT91C_BASE_AIC; // Disable FIQ interrupt in // AIC Interrupt Disable Command Register pAIC->AIC_IDCR = (1<AIC_SMR[AT91C_ID_FIQ] = (AT91C_AIC_SRCTYPE_INT_EDGE_TRIGGERED); // Clear the FIQ interrupt in // AIC Interrupt Clear Command Register pAIC->AIC_ICCR = (1<AIC_IDCR = (0<AIC_IECR = (1<PIO_ODSR & LED1) == LED1) && (LED1_Off_Enabled)) { LED1_Off_Enabled = false; /* wait */ LED_Timer_1 = 20; } if (!LED_Timer_1) { /* turn off */ pPIO->PIO_SODR = LED1; LED1_Off_Enabled = true; } /* USART Rx turns the LED on, we turn it off */ if (((pPIO->PIO_ODSR & LED2) == LED2) && (LED2_Off_Enabled)) { LED2_Off_Enabled = false; /* wait */ LED_Timer_2 = 20; } if (!LED_Timer_2) { /* turn off */ pPIO->PIO_SODR = LED2; LED2_Off_Enabled = true; } /* switch or NPDU turns on the LED, we turn it off */ if (((pPIO->PIO_ODSR & LED3) == LED3) && (LED3_Off_Enabled)) { LED3_Off_Enabled = false; /* wait */ LED_Timer_3 = 500; } if (!LED_Timer_3) { /* turn LED3 (DS3) off */ pPIO->PIO_SODR = LED3; LED3_Off_Enabled = true; } /* Blink LED every second */ if (!LED_Timer_4) { if ((pPIO->PIO_ODSR & LED4) == LED4) { /* turn on */ pPIO->PIO_CODR = LED4; } else { /* turn off */ pPIO->PIO_SODR = LED4; } /* wait */ LED_Timer_4 = 1000; } // count # of times through the idle loop IdleCount++; /* BACnet handling */ pdu_len = datalink_receive(&src, &pdu[0], MAX_MPDU, 0); if (pdu_len) { pPIO->PIO_CODR = LED3; #ifndef DLMSTP_TEST npdu_handler(&src, &pdu[0], pdu_len); #endif } } }