program dsPIC33EP_NMRABusXpressnet; // ****************************************************************************** // // * Copyright: // (c) Mustangpeak Software 2012. // // The contents of this file are subject to the GNU GPL v3 licence/ you maynot use // this file except in compliance with the License. You may obtain a copy of the // License at http://www.gnu.org/licenses/gpl.html // // * Revision History: // 2012-02-01: Created // 2012-12-16 updated // // * Description: // // ***************************************************************************** uses NMRAnetXpressnet, NMRAnetStateMachine, NMRAnetDefines, NMRAnetAppCallbacks, MCU_Setup_dsPIC33EP64GP502, NMRAnetXpressnetDefines, _25AAxxxx, NMRAnetBufferPools, NMRAnetNode, // dsPIC33_Traps, NodeIDs; {$I Options.inc} const STATE_GATHER_COMMANDSTATIONS = 0; STATE_VERIFY_COMMANDSTATIONS = 1; STATE_GATHER_PROGRAMMERS = 2; STATE_VERIFY_PROGRAMMERS = 3; STATE_INITIALIAZE = 4; STATE_RUN = 5; var MainLoopState: Byte; var // EEPROM SPI PINS CS_Bank_0 : sbit at LATB6_bit; CS_Bank_0_Direction : sbit at TRISB6_bit; EE_PROM_Hold : sbit at LATB5_bit; EEPROM_Hold_Direction : sbit at TRISB5_bit; var Xpressnet_RS485_Select : sbit at LATA0_bit; Xpressnet_RS485_Select_Direction : sbit at TRISA0_bit; // TrapFlagPin: sbit at LATB4_bit; // ***************************************************************************** // INTERRUPTS // ***************************************************************************** procedure INTERRUPT_Timer_525ms(); iv IVT_ADDR_T1INTERRUPT; begin T1IF_bit := 0; XpressnetStateMachine_525ms_TimeTick; end; procedure INTERRUPT_Timer_100ms(); iv IVT_ADDR_T2INTERRUPT; // Called once every 100m var i: Integer; begin T2IF_bit := 0; // Clear the Flag for i := 0 to Nodes.AllocatedCount - 1 do NMRAnetStateMachine_100ms_Timer(Nodes.AllocatedList[i]); NMRAnetBufferPools_100ms_TimeTick; end; procedure INTERRUPT_Timer_120us(); iv IVT_ADDR_T3INTERRUPT; ics ICS_AUTO; // Xpressnet Timeout begin T3IF_bit := 0; end; {$IFDEF XPRESSNET_UART_ENABLE} procedure INTERRUPT_XPRESSNET_UART_RX(); iv IVT_ADDR_U2RXINTERRUPT; begin U2RXIF_Bit := 0; // Clear the interrupt flag to receive the next byte while (URXDA_U2STA_bit = 1) and (XpressnetStateMachineInfo.RS485.XPressNetData.StateMachineIndex < STATE_RS485_FULL) do begin XpressnetStateMachine_UART_RX_StateMachine(@XpressnetStateMachineInfo, U2RXREG); end end; {$ENDIF} // ******************* // MAIN LOOP // ******************* var Buffer: TCANBuffer; ActiveNode: PNMRAnetNode; j: Integer; begin SR := SR and $FF1F; // Bug in silicon? Clear the IPL bits (CPU Priority is lowest possible) TRISB4_bit := 0; LATB4_bit := 0; _25AAxxxx_Initialize; NMRAnetStateMachine_Initialize(MUSTANGPEAK_ID_0_HI, MUSTANGPEAK_XPRESSNET_ID_0_LO); MCU_Setup_Initialize; XpressnetStateMachine_Initialize; AppCallback_Configuration_Zeroize(False); // Enabled Perphrials MCU_Enable_CAN; TON_T1CON_bit := 1; // Turn on TON_T2CON_bit := 1; // Turn on UART1_Write_Text('Starting'+LF); MainLoopState := 0; while (TRUE) do begin ActiveNode := NMRAnetNode_NextNode; if ActiveNode <> PNMRAnetNode( nil) then begin if UART1_Data_Ready then begin case UART1_Read of 'Z', 'z' : begin _25AAxxxx_Erase(EEPROM_BANK_0); end; 'a' : begin if IPL_0_bit = 0 then UART1_Write_Text('IPL_0_bit = 0' + LF) else UART1_Write_Text('IPL_0_bit = 1' + LF); if IPL_1_bit = 0 then UART1_Write_Text('IPL_1_bit = 0' + LF) else UART1_Write_Text('IPL_1_bit = 1' + LF); if IPL_2_bit = 0 then UART1_Write_Text('IPL_2_bit = 0' + LF) else UART1_Write_Text('IPL_2_bit = 1' + LF); end; end; end; NMRAnetStateMachine_Process(ActiveNode); XpressnetStateMachine_Process(@Nodes.RawList[0]); // May want this outside of Permited block, look at the statemachine end end; end.