void MyMIWI_Init(void) { // Configure Pins for MRF24J40MB mPORTESetBits(RST_MIWI); mPORTESetPinsDigitalOut(RST_MIWI); mPORTBSetBits(MIWI_WAKE); mPORTBSetPinsDigitalOut(MIWI_WAKE); // Configure the INT3 controller for MIWI // Set RD10/INT3 as input mPORTDSetPinsDigitalIn(BIT_10); // Clear corresponding bits in INTCON for falling edge trigger INTCONCLR = _INTCON_INT3EP_MASK; // Set up interrupt prioirty and sub-priority INTSetVectorPriority(INT_EXTERNAL_3_VECTOR, My_INT_EXTERNAL_3_PRIORITY); INTSetVectorSubPriority(INT_EXTERNAL_3_VECTOR, My_INT_EXTERNAL_3_SUB_PRIORITY); // Clear the interrupt flags INTClearFlag(INT_INT3); // Enable INT3 INTEnable(INT_INT3, INT_ENABLED); // WARNING : Change in file MRF24J40.c in Microchip Application Library // the line : void __ISR(_EXTERNAL_1_VECTOR, ipl4) _INT1Interrupt(void) // by : void __ISR(_EXTERNAL_3_VECTOR, ipl4) _INT3Interrupt(void) }
void MyCyclone_Init(void) { // Configure Reset Pin = GPIO_2[10] = RD7 mPORTDClearBits(RST_FPGA); mPORTDSetPinsDigitalOut(RST_FPGA); // Do a Reset mPORTDSetBits(RST_FPGA); mPORTDClearBits(RST_FPGA); // Do Interrupts Initialization // Set RD8/INT1 and RD9/INT2 as inputs mPORTDSetPinsDigitalIn(BIT_8 | BIT_9); // Clear corresponding bits in INTCON for falling edge trigger INTCONCLR = _INTCON_INT1EP_MASK | _INTCON_INT2EP_MASK; // Set up interrupt prioirty and sub-priority INTSetVectorPriority(INT_EXTERNAL_1_VECTOR, My_INT_EXTERNAL_1_PRIORITY); INTSetVectorSubPriority(INT_EXTERNAL_1_VECTOR, My_INT_EXTERNAL_1_SUB_PRIORITY); INTSetVectorPriority(INT_EXTERNAL_2_VECTOR, My_INT_EXTERNAL_2_PRIORITY); INTSetVectorSubPriority(INT_EXTERNAL_2_VECTOR, My_INT_EXTERNAL_2_SUB_PRIORITY); // Clear the interrupt flags INTClearFlag(INT_INT1); INTClearFlag(INT_INT2); // Enable INT1 & INT2 INTEnable(INT_INT1, INT_ENABLED); INTEnable(INT_INT2, INT_ENABLED); // Enable KEY0 and KEY1 interrupts and IOs of the MyExpansionBoard_IO_v2 MyCyclone_Write(CYCLONE_CONFIG,CYCLONE_ENABLE_INT_KEY0 | CYCLONE_ENABLE_INT_KEY1 | CYCLONE_ENABLE_IO_AB | CYCLONE_ENABLE_IO_CD); }
int main() { i=0; lcdini(); DDPCON=0x0000; mPORTDSetPinsDigitalIn(BIT_7|BIT_13|BIT_6); //Set Portd pins DigitalIn mPORTASetPinsDigitalOut(BIT_0|BIT_1|BIT_2|BIT_3|BIT_4|BIT_5|BIT_6); mPORTAClearBits(BIT_0|BIT_1|BIT_2|BIT_3|BIT_4|BIT_5|BIT_6); mPORTASetPinsDigitalIn(BIT_7); char x; while(1) { switch(i) //Switch case to select function { case 0: mode(); break; case 1: batteryv(); break; case 2: temps(); break; } if(i==3) { lcdcmd(0x01); break; } } }
//Function to initialize buttons BTN1, BTN2, and BTN3 void initButtons() { speeds = 1; int x; for(x = 0; x<4; x++) { samples[x] = 0x00; waitingFor[x] = 1; } mPORTGSetPinsDigitalIn(BIT_6 | BIT_7); mPORTDSetPinsDigitalIn(BIT_13 ); mPORTFSetPinsDigitalIn(BIT_4 | BIT_13); }
void InitializeIO(void) { //Initializing CSN and CE pins as output mPORTFSetPinsDigitalOut(BIT_0 | BIT_1); mPORTFClearBits(BIT_0 | BIT_1); //Initializing IRQ pin as input mPORTDSetPinsDigitalIn(BIT_13); mPORTDClearBits(BIT_13); //Setting CSN bit active LATFbits.LATF1 = 1; //Initializing LEDs mPORTDSetPinsDigitalOut(BIT_1 | BIT_2); mPORTDClearBits(BIT_1 | BIT_2); //Initializing MOSI/SDO1 and SCK as output mPORTDSetPinsDigitalOut(BIT_10); mPORTDSetPinsDigitalOut(BIT_0); //Initializing MISO/SDI1 as input mPORTCSetPinsDigitalIn(BIT_4); }
void setupHardware() { SYSTEMConfig(SYS_CLOCK, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); mPORTDSetPinsDigitalOut(BIT_1); //yellow LED mPORTGSetPinsDigitalOut(BIT_6); //green LED //A1 PIR Input (CN4 module, RB2) //A2 STB current consumption (AN3) //D0 IR Output mPORTDSetPinsDigitalOut(BIT_3); //D1 STB IRF control mPORTDSetPinsDigitalIn(BIT_4); //D2 BUT OTG mPORTDSetPinsDigitalOut(BIT_5); //D3 SHD RED LED Command OFF acqusition notificaiton mPORTDSetPinsDigitalOut(BIT_6); //D4 SHD GREEN LED Command ON acqusition notificaiton //D5 IR input (RD7) //D6 SHD BUT1 //D7 SHD BUT2 mPORTBSetPinsDigitalOut(BIT_14); //D9 monitor relay control DDPCONbits.JTAGEN = 0; initLEDs(); initUART(); initADC(); setupCNModuleAnd_IR_PIR_Input(); //setupCNModuleAndPIRInput(); configureRTC(); setupIRTransmit(); switchMonitorOff(); switchSTBOff(); INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); INTEnableInterrupts(); T2CONbits.ON = 1; }
void setupCNModuleAnd_IR_PIR_Input() { //T2 used to sample IR Input signals setupTimer2(); //IR INPUT mPORTDSetPinsDigitalIn(BIT_7); //D5 IR Input (CN16 module, RD7) //PIR input mPORTBSetPinsDigitalIn(BIT_2); //A1 PIR Input (CN4 module, RB2) // setup the change notice options //(ensure that CN continues working in sleep mode) mCNOpen(CN_OFF | CN_IDLE_CON, CN16_ENABLE | CN4_ENABLE, CN16_PULLUP_ENABLE | CN4_PULLUP_ENABLE); // read port(s) to clear mismatch mPORTDReadBits(BIT_7); mPORTBReadBits(BIT_2); mPORTBReadBits(BIT_4); // configure interrupts and clear change notice interrupt flag ConfigIntCN(CHANGE_INT_ON | CHANGE_INT_PRI_3); mCNClearIntFlag(); // Clear interrupt flag }
/**************************************************************************** Function: static void InitializeBoard(void) Description: This routine initializes the hardware. It is a generic initialization routine for many of the Microchip development boards, using definitions in HardwareProfile.h to determine specific initialization. Precondition: None Parameters: None - None Returns: None Remarks: None ***************************************************************************/ static void InitializeBoard(void) { // WiFi Module hardware Initialization handled by Library // Enable multi-vectored interrupts INTEnableSystemMultiVectoredInt(); // Enable optimal performance SYSTEMConfigPerformance(GetSystemClock()); mOSCSetPBDIV(OSC_PB_DIV_1); // Use 1:1 CPU Core:Peripheral clocks // Disable JTAG port so we get our I/O pins back, but first // wait 50ms so if you want to reprogram the part with // JTAG, you'll still have a tiny window before JTAG goes away. // The PIC32 Starter Kit debuggers use JTAG and therefore must not // disable JTAG. DelayMs(50); DDPCONbits.JTAGEN = 0; // LEDs LEDS_OFF(); mPORTESetPinsDigitalOut(BIT_5 | BIT_6 | BIT_7); // Switches mPORTDSetPinsDigitalIn(BIT_4 | BIT_5 | BIT_6); ConfigCNPullups(CN13_PULLUP_ENABLE | CN14_PULLUP_ENABLE | CN15_PULLUP_ENABLE); // LCD mPORTESetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2 | BIT_3); //Configure LCD SPI pins mPORTFSetPinsDigitalOut(BIT_8); mPORTDSetPinsDigitalOut(BIT_15); //SPI Flash mPORTDSetPinsDigitalOut(BIT_14); //UART mPORTFSetPinsDigitalOut(BIT_5); mPORTFSetPinsDigitalIn(BIT_4); //MiWi #if defined(MRF24J40) || defined(MRF49XA) PHY_CS = 1; mPORTDSetPinsDigitalOut(BIT_9); PHY_RESETn = 1; mPORTDSetPinsDigitalOut(BIT_11); #endif #if defined(MRF49XA) nFSEL_TRIS = 0; FINT_TRIS = 1; nFSEL = 1; #elif defined(MRF24J40) PHY_WAKE = 1; mPORTBSetPinsDigitalOut(BIT_9); #else Data_nCS_TRIS = 0; Config_nCS_TRIS = 0; Data_nCS = 1; Config_nCS = 1; IRQ1_INT_TRIS = 1; IRQ0_INT_TRIS = 1; #endif /* Set the Port Directions of SDO, SDI, Clock & Slave Select Signal */ /* Set SCK port pin to output */ mPORTDSetPinsDigitalOut(BIT_10); /* Set SDO port pin to output */ mPORTDSetPinsDigitalOut(BIT_0); /* Set SDI port pin to input */ mPORTCSetPinsDigitalIn(BIT_4); /* Set INT1, INT2 port pins to input */ mPORTESetPinsDigitalIn(BIT_8 | BIT_9); /* Clear SPI1CON register */ SPI1CONCLR = 0xFFFFFFFF; #ifdef HARDWARE_SPI unsigned int pbFreq; /* Enable SPI1, Set to Master Mode & Set CKE bit : Serial output data changes on transition from active clock state to Idle clock state */ SPI1CON = 0x00008120; /* Peripheral Bus Frequency = System Clock / PB Divider */ pbFreq = (DWORD) CLOCK_FREQ / (1 << mOSCGetPBDIV()); /* PB Frequency can be maximum 40 MHz */ if (pbFreq > (2 * MAX_SPI_CLK_FREQ_FOR_P2P)) { { unsigned int SPI_Clk_Freq; unsigned char SPI_Brg1 = 1; //For the SPI1 /* Continue the loop till you find SPI Baud Rate Register Value */ while (1) { /* SPI Clock Calculation as per PIC32 Manual */ SPI_Clk_Freq = pbFreq / (2 * (SPI_Brg1 + 1)); if (SPI_Clk_Freq <= MAX_SPI_CLK_FREQ_FOR_P2P) { break; } SPI_Brg1++; } mSpiChnSetBrg(1, SPI_Brg1); } } else { /* Set SPI1 Baud Rate */ mSpiChnSetBrg(1, 0); } #endif /* Set the Interrupt Priority */ mINT2SetIntPriority(4); #if defined(MRF89XA) mINT1SetIntPriority(4); #endif /* Set Interrupt Subpriority Bits for INT2 */ mINT2SetIntSubPriority(2); #if defined(MRF89XA) mINT2SetIntSubPriority(1); #endif /* Set INT2 to falling edge */ mINT2SetEdgeMode(0); #if defined(MRF89XA) mINT1SetEdgeMode(1); mINT2SetEdgeMode(1); #endif /* Enable INT2 */ mINT2IntEnable(1); #if defined(MRF89XA) mINT2IntEnable(1); #endif /* Enable Multi Vectored Interrupts */ // INTEnableSystemMultiVectoredInt(); #if defined(MRF89XA) PHY_IRQ1 = 0; PHY_IRQ0 = 0; PHY_RESETn_TRIS = 1; #else RFIF = 0; if (RF_INT_PIN == 0) { RFIF = 1; } #endif // Initialize the EEPROM XEEInit(); // UART Initialization #if defined(STACK_USE_UART) UARTTX_TRIS = 0; UARTRX_TRIS = 1; UMODE = 0x8000; // Set UARTEN. Note: this must be done before setting UTXEN USTA = 0x00001400; // RXEN set, TXEN set #define CLOSEST_UBRG_VALUE ((GetPeripheralClock()+8ul*BAUD_RATE)/16/BAUD_RATE-1) #define BAUD_ACTUAL (GetPeripheralClock()/16/(CLOSEST_UBRG_VALUE+1)) #define BAUD_ERROR ((BAUD_ACTUAL > BAUD_RATE) ? BAUD_ACTUAL-BAUD_RATE : BAUD_RATE-BAUD_ACTUAL) #define BAUD_ERROR_PRECENT ((BAUD_ERROR*100+BAUD_RATE/2)/BAUD_RATE) #if (BAUD_ERROR_PRECENT > 3) #warning UART frequency error is worse than 3% #elif (BAUD_ERROR_PRECENT > 2) #warning UART frequency error is worse than 2% #endif UBRG = CLOSEST_UBRG_VALUE; #endif }
main() { // Disable JTAG (on RA0 and RA1 ) mJTAGPortEnable( DEBUG_JTAGPORT_OFF ); // Configure the device for maximum performance but do not change the PBDIV // Given the options, this function will change the flash wait states, RAM // wait state and enable prefetch cache but will not change the PBDIV. // The PBDIV value is already set via the pragma FPBDIV option above.. SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); initializeUART(); initializeADC(); initializeLCD(); initializeRPG(); /* Initialize SD card */ setup_SDSPI(); SD_setStart(); /* Fill tempBuffer[] with int 0 to 63 * Write it to the current block. * Empty tempBuffer[] to all 0. * Read from the current block to make sure that it returns the right value. */ fillTempBuffer(); testSDReadWrite(tempBuffer); curr_read_block = curr_block; ConfigTimer1(); // Enable Timer1 for second counts configureInterrupts(); // T2CON = 0x8030; // TMR1 on, prescale 1:256 PB mPORTASetPinsDigitalOut( LED_MASK ); // LEDs = output mPORTDSetPinsDigitalIn( PB_MASK_D ); // PBs on D = input curr_state = READY; // enable interrupts INTEnableInterrupts(); int i = 0; while( 1 ) { if (getPrintToUARTFlag() == 1){ LCDMenuControl(); //mPORTAToggleBits( LED_MASK ); convertAndPrintIntegerToString("i => ", i++); convertAndPrintIntegerToString("timeElapse => ", timeElapsed); convertAndPrintIntegerToString("timeElapsedLEDSample => ", timeElapsedLEDSample); convertAndPrintIntegerToString("timeElapsedLEDTurnedOff => ", timeElapsedLEDTurnedOff); convertAndPrintIntegerToString("sampleLEDNow => ", sampleLEDNow); convertAndPrintIntegerToString(" ADC Value => ", getChannel5Value()); printShadowDetect(); printLightLevel(); drawLightDetectedBar(); controlPowerRelay(); switch(curr_state) { case READY : WriteString("State => READY "); break; case SLEEP : WriteString("State => SLEEP "); break; case HIBERNATE : WriteString("State => HIBERNATE"); break; case BUSY : WriteString("State => BUSY "); break; } WriteString("\r"); setPrintToUARTFlag(0); } if (NEW_BYTE_RECEIVED == 1){ curr_state = READY; NEW_BYTE_RECEIVED = 0; //mPORTAToggleBits( LED_MASK ); char tempArray[] = "g"; tempArray[0] = characterByteReceived; WriteString(tempArray); if(curr_state = HIBERNATE) { addByteToBuffer(characterByteReceived); } else { PutCharacter(characterByteReceived); } } if(bufferIndex == 512) { SDWriteBlock(currBlock); currBlock++; bufferIndex = 0; } if((curr_state == READY) && (timeElapsed >= SLEEP_TIMEOUT) && (timeElapsed < HIBERNATE_TIMEOUT)) { curr_state = SLEEP; } else if((curr_state == SLEEP) && (timeElapsed >= HIBERNATE_TIMEOUT)) { curr_state = HIBERNATE; timeElapsed = 0; } if (transmitDataFromSDCard == 1) { transmitDataFromSDCard = 0; forwardDataToPrinter(); } } // main (while) loop return 0; } // main
//******************************** //******************************** //********** INITIALISE ********** //******************************** //******************************** void initialise (void) { BYTE data; //##### GENERAL NOTE ABOUT PIC32'S ##### //Try and use the peripheral libraries instead of special function registers for everything (literally everything!) to avoid //bugs that can be caused by the pipeline and interrupts. //--------------------------------- //----- CONFIGURE PERFORMANCE ----- //--------------------------------- //----- SETUP EVERYTHING FOR OPTIMUM PERFORMANCE ----- SYSTEMConfigPerformance(80000000ul); //Note this sets peripheral bus to '1' max speed (regardless of configuration bit setting) //Use PBCLK divider of 1:1 to calculate UART baud, timer tick etc //----- SET PERIPHERAL BUS DIVISOR ----- //To minimize dynamic power the PB divisor should be chosen to run the peripherals at the lowest frequency that provides acceptable system performance mOSCSetPBDIV(OSC_PB_DIV_2); //OSC_PB_DIV_1, OSC_PB_DIV_2, OSC_PB_DIV_4, OSC_PB_DIV_8, //----- SETUP INTERRUPTS ----- INTEnableSystemMultiVectoredInt(); //------------------------- //----- SETUP IO PINS ----- //------------------------- //(Device will powerup with all IO pins as inputs) //----- TURN OFF THE JTAG PORT ----- //(JTAG is on by default) //mJTAGPortEnable(0); //Must be on for Microchip Multimedia Development board #define PORTA_IO 0xc2ff //Setup the IO pin type (0 = output, 1 = input) mPORTAWrite(0xc033); //Set initial ouput pin states mPORTASetPinsDigitalIn(PORTA_IO); //(Sets high bits as input) mPORTASetPinsDigitalOut(~PORTA_IO); //(Sets high bits as output) #define PORTB_IO 0xfbff //Setup the IO pin type (0 = output, 1 = input) mPORTBWrite(0x6d13); //Set initial ouput pin states mPORTBSetPinsDigitalIn(PORTB_IO); //(Sets high bits as input) mPORTBSetPinsDigitalOut(~PORTB_IO); //(Sets high bits as output) mPORTBSetPinsDigitalIn(BIT_0 | BIT_1 | BIT_3 | BIT_4 | BIT_15); //Joystick inputs #define PORTC_IO 0xf01e //Setup the IO pin type (0 = output, 1 = input) mPORTCWrite(0x3018); //Set initial ouput pin states mPORTCSetPinsDigitalIn(PORTC_IO); //(Sets high bits as input) mPORTCSetPinsDigitalOut(~PORTC_IO); //(Sets high bits as output) #define PORTD_IO 0x7bfe //Setup the IO pin type (0 = output, 1 = input) mPORTDWrite(0xbdaf); //Set initial ouput pin states mPORTDSetPinsDigitalIn(PORTD_IO); //(Sets high bits as input) mPORTDSetPinsDigitalOut(~PORTD_IO); //(Sets high bits as output) mPORTDSetPinsDigitalOut(BIT_2 | BIT_1); //LED's 2 and 3 mPORTDSetPinsDigitalIn(BIT_9); #define PORTE_IO 0x03ff //Setup the IO pin type (0 = output, 1 = input) mPORTEWrite(0x02a2); //Set initial ouput pin states mPORTESetPinsDigitalIn(PORTE_IO); //(Sets high bits as input) mPORTESetPinsDigitalOut(~PORTE_IO); //(Sets high bits as output) #define PORTF_IO 0x111f //Setup the IO pin type (0 = output, 1 = input) mPORTFWrite(0x0039); //Set initial ouput pin states mPORTFSetPinsDigitalIn(PORTF_IO); //(Sets high bits as input) mPORTFSetPinsDigitalOut(~PORTF_IO); //(Sets high bits as output) #define PORTG_IO 0xd3cf //Setup the IO pin type (0 = output, 1 = input) mPORTGWrite(0xf203); //Set initial ouput pin states mPORTGSetPinsDigitalIn(PORTG_IO); //(Sets high bits as input) mPORTGSetPinsDigitalOut(~PORTG_IO); //(Sets high bits as output) //Read pins using: // mPORTAReadBits(BIT_0); //Write pins using: // mPORTAClearBits(BIT_0); // mPORTASetBits(BIT_0); // mPORTAToggleBits(BIT_0); //----- INPUT CHANGE NOTIFICATION CONFIGURATION ----- //EnableCN0(); ConfigCNPullups(CN2_PULLUP_ENABLE | CN3_PULLUP_ENABLE | CN5_PULLUP_ENABLE | CN6_PULLUP_ENABLE | CN12_PULLUP_ENABLE); //Joystick pins //----- SETUP THE A TO D PINS ----- ENABLE_ALL_DIG; //--------------------- //----- SETUP USB ----- //--------------------- //The USB specifications require that USB peripheral devices must never source current onto the Vbus pin. Additionally, USB peripherals should not source //current on D+ or D- when the host/hub is not actively powering the Vbus line. When designing a self powered (as opposed to bus powered) USB peripheral //device, the firmware should make sure not to turn on the USB module and D+ or D- pull up resistor unless Vbus is actively powered. Therefore, the //firmware needs some means to detect when Vbus is being powered by the host. A 5V tolerant I/O pin can be connected to Vbus (through a resistor), and //can be used to detect when Vbus is high (host actively powering), or low (host is shut down or otherwise not supplying power). The USB firmware //can then periodically poll this I/O pin to know when it is okay to turn on the USB module/D+/D- pull up resistor. When designing a purely bus powered //peripheral device, it is not possible to source current on D+ or D- when the host is not actively providing power on Vbus. Therefore, implementing this //bus sense feature is optional. This firmware can be made to use this bus sense feature by making sure "USE_USB_BUS_SENSE_IO" has been defined in the //HardwareProfile.h file. // #if defined(USE_USB_BUS_SENSE_IO) // tris_usb_bus_sense = INPUT_PIN; // See HardwareProfile.h // #endif //If the host PC sends a GetStatus (device) request, the firmware must respond and let the host know if the USB peripheral device is currently bus powered //or self powered. See chapter 9 in the official USB specifications for details regarding this request. If the peripheral device is capable of being both //self and bus powered, it should not return a hard coded value for this request. Instead, firmware should check if it is currently self or bus powered, and //respond accordingly. If the hardware has been configured like demonstrated on the PICDEM FS USB Demo Board, an I/O pin can be polled to determine the //currently selected power source. On the PICDEM FS USB Demo Board, "RA2" is used for this purpose. If using this feature, make sure "USE_SELF_POWER_SENSE_IO" //has been defined in HardwareProfile.h, and that an appropriate I/O pin has been mapped to it in HardwareProfile.h. // #if defined(USE_SELF_POWER_SENSE_IO) // tris_self_power = INPUT_PIN; // See HardwareProfile.h // #endif //Enable the USB port now - we will check to see if Vbus is powered at the end of init and disable it if not. //USBDeviceInit(); //usb_device.c. Initializes USB module SFRs and firmware variables to known states. //------------------------ //----- SETUP TIMERS ----- //------------------------ //(INCLUDE THE USAGE OF ALL TIMERS HERE EVEN IF NOT SETUP HERE SO THIS IS THE ONE POINT OF //REFERENCE TO KNOW WHICH TIMERS ARE IN USE AND FOR WHAT). //----- SETUP TIMER 1 ----- //Used for: Available //OpenTimer1((T1_ON | T1_IDLE_CON | T1_GATE_OFF | T1_PS_1_4 | T1_SOURCE_INT), 20000); //----- SETUP TIMER 2 ----- //Used for: //OpenTimer2((T2_ON | T2_IDLE_CON | T2_GATE_OFF | T2_PS_1_1 | T2_SOURCE_INT), 0xffff); //0xffff = 305Hz //----- SETUP TIMER 3 ----- //Used for: //OpenTimer3((T3_ON | T3_IDLE_CON | T3_GATE_OFF | T3_PS_1_1 | T3_SOURCE_INT), PIEZO_TIMER_PERIOD); //----- SETUP TIMER 4 ----- //Used for: //OpenTimer4((T4_ON | T4_IDLE_CON | T4_GATE_OFF | T4_PS_1_1 | T4_SOURCE_INT), 20000); //----- SETUP TIMER 5 ----- //Used for: Heartbeat OpenTimer5((T5_ON | T5_IDLE_CON | T5_GATE_OFF | T5_PS_1_1 | T5_SOURCE_INT), 40000); //1mS with 80MHz osc and PB_DIV_2 ConfigIntTimer5(T5_INT_ON | T5_INT_PRIOR_7); //1=lowest priority to 7=highest priority. ISR function must specify same value //--------------------------------- //----- SETUP EVAL BOARD CPLD ----- //--------------------------------- //Graphics bus width = 16 mPORTGSetPinsDigitalOut(BIT_14); mPORTGSetBits(BIT_14); //SPI source select = SPI3 (not used) mPORTGSetPinsDigitalOut(BIT_12); mPORTGClearBits(BIT_12); //SPI peripheral destination select = Expansion Slot (not used) mPORTASetPinsDigitalOut(BIT_7 | BIT_6); mPORTASetBits(BIT_7); mPORTAClearBits(BIT_6); //-------------------------------------- //----- PARALLEL MASTER PORT SETUP ----- //-------------------------------------- PMMODE = 0; PMAEN = 0; PMCON = 0; PMMODE = 0x0610; PMCONbits.PTRDEN = 1; //Enable RD line PMCONbits.PTWREN = 1; //Enable WR line PMCONbits.PMPEN = 1; //Enable PMP //------------------------------ //----- INITIALISE DISPLAY ----- //------------------------------ display_initialise(); display_test(); //LOAD OUR GLOBAL HTML STYLES FILE READY FOR DISPLAY HTML PAGES BYTE dummy_styles_count; DWORD file_size; if (display_html_setup_read_file(global_css, 0, &file_size)) { dummy_styles_count = 0; display_html_read_styles(&file_size, &dummy_styles_count, 1); //1 = this is global styles file } }
int main() { // Initialize Sockets and IP address containers // SOCKET serverSock, clientSock = INVALID_SOCKET; IP_ADDR curr_ip, ip; // Initialize buffer length variables // int rlen, sent, bytesSent; // Initialize the Send/Recv buffers // char rbfr[10]; // Socket struct descriptor // struct sockaddr_in addr; int addrlen = sizeof(struct sockaddr_in); // System clock containers // unsigned int sys_clk, pb_clk; // Initialize LED Variables: // Setup the LEDs on the PIC32 board // RD0, RD1 and RD2 as outputs // mPORTDSetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2 ); mPORTDClearBits(BIT_0 | BIT_1 | BIT_2); // Clear previous LED status. // Setup the switches on the PIC32 board as inputs // mPORTDSetPinsDigitalIn(BIT_6 | BIT_7 | BIT_13); // RD6, RD7, RD13 as inputs // Setup the system clock to use CPU frequency // sys_clk = GetSystemClock(); pb_clk = SYSTEMConfigWaitStatesAndPB(sys_clk); // interrupts enabled INTEnableSystemMultiVectoredInt(); // system clock enabled SystemTickInit(sys_clk, TICKS_PER_SECOND); // Initialize TCP/IP // TCPIPSetDefaultAddr(DEFAULT_IP_ADDR, DEFAULT_IP_MASK, DEFAULT_IP_GATEWAY, DEFAULT_MAC_ADDR); if (!TCPIPInit(sys_clk)) return -1; DHCPInit(); // Port to bind socket to // addr.sin_port = 6653; addr.sin_addr.S_un.S_addr = IP_ADDR_ANY; // Initialize TCP server socket // if((serverSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == SOCKET_ERROR) return -1; // Ensure we bound to the socket. End Program if bind fails // if(bind(serverSock, (struct sockaddr*) &addr, addrlen ) == SOCKET_ERROR) return -1; // Listen to up to five clients on server socket // listen(serverSock, 5); // We store our desired transfer paragraph // char myStr[] = "TCP/IP (Transmission Control Protocol/Internet Protocol) is " "the basic communication language or protocol of the Internet. " "It can also be used as a communications protocol in a private " "network (either an intranet or an extranet). When you are set up " "with direct access to the Internet, your computer is provided " "with a copy of the TCP/IP program just as every other computer " "that you may send messages to or get information from also has " "a copy of TCP/IP. TCP/IP is a two-layer program. The higher " "layer, Transmission Control Protocol, manages the assembling " "of a message or file into smaller packets that are transmitted " "over the Internet and received by a TCP layer that reassembles " "the packets into the original message. The lower layer, " "Internet Protocol, handles the address part of each packet so " "that it gets to the right destination. Each gateway computer on " "the network checks this address to see where to forward the " "message. Even though some packets from the same message are " "routed differently than others, they'll be reassembled at the " "destination.\0"; // Chunk up our data // // Copy our string into our buffer // int tlen = strlen(myStr); char tbfr1[tlen1+1]; // Loop forever // while(1) { // Refresh TCIP and DHCP // TCPIPProcess(); DHCPTask(); // Get the machines IP address and save to variable // ip.Val = TCPIPGetIPAddr(); // DHCP server change IP address? // if(curr_ip.Val != ip.Val) curr_ip.Val = ip.Val; // TCP Server Code // if(clientSock == INVALID_SOCKET) { // Start listening for incoming connections // clientSock = accept(serverSock, (struct sockaddr*) &addr, &addrlen); // Upon connection to a client blink LEDS. // if(clientSock != INVALID_SOCKET) { setsockopt(clientSock, SOL_SOCKET, TCP_NODELAY, (char*)&tlen, sizeof(int)); mPORTDSetBits(BIT_0); // LED1=1 DelayMsec(50); mPORTDClearBits(BIT_0); // LED1=0 mPORTDSetBits(BIT_1); // LED2=1 DelayMsec(50); mPORTDClearBits(BIT_1); // LED2=0 mPORTDSetBits(BIT_2); // LED3=1 DelayMsec(50); mPORTDClearBits(BIT_2); // LED3=0 } } else { // We are connected to a client already. We start // by receiving the message being sent by the client // rlen = recvfrom(clientSock, rbfr, sizeof(rbfr), 0, NULL, NULL); // Check to see if socket is still alive // if(rlen > 0) { // If the received message first byte is '02' it signifies // a start of message // if (rbfr[0]==2) { //mPORTDSetBits(BIT_0); // LED1=1 // Check to see if message begins with // '0271' to see if the message is a a global reset // if(rbfr[1]==71) { mPORTDSetBits(BIT_0); // LED1=1 DelayMsec(50); mPORTDClearBits(BIT_0); // LED1=0 } } // If the received message starts with a second byte is // '84' it signifies a initiate transfer // if(rbfr[1]==84){ mPORTDSetBits(BIT_2); // LED3=1 bytesSent = 0; //sent = 0; while (bytesSent < tlen){ memcpy(tbfr1, myStr+bytesSent, tlen1); if (bytesSent > 1049){ tbfr1[tlen-bytesSent+1] = '\0'; send(clientSock, tbfr1, tlen-bytesSent+1, 0); } else{ tbfr1[tlen1] = '\0'; // Loop until we send the full message // send(clientSock, tbfr1, tlen1+1, 0); } bytesSent += tlen1; DelayMsec(50); } mPORTDClearBits(BIT_2); // LED3=0 } mPORTDClearBits(BIT_0); // LED1=0 } // The client has closed the socket so we close as well // else if(rlen < 0) { closesocket(clientSock); clientSock = SOCKET_ERROR; } } } }
/******************************************************************************* * Function: BoardInit(void) * PreCondition:None * Input: None * Output: None * Overview: SPI pins and SFR, Maintenance Tasks Timer, External Interrupts, * and other board issues initialization. * Note: This routine needs to be called before initialising MiWi stack * or invoking other function that operates on MiWi stack. ******************************************************************************/ void BoardInit(void){ #if defined(__PIC32MX__) // RADIO INTERFACES & SPI INIT -------------------------------------------// #if defined HARDWARE_SPI /* Peripheral Bus Frequency = System Clock / PB Divider */ unsigned int pbFreq; pbFreq = (DWORD) CLOCK_FREQ/(1 << mOSCGetPBDIV()); unsigned int SPI_Clk_Freq; unsigned char SPI_Brg; #endif #if defined MRF24J40 PHY_CS_TRIS = OUTPUT_PIN; PHY_CS = 1; PHY_RESETn_TRIS = OUTPUT_PIN; PHY_RESETn = 1; MRF24J40_INT_TRIS = INPUT_PIN; SDI_TRIS = INPUT_PIN; SDO_TRIS = OUTPUT_PIN; SCK_TRIS = OUTPUT_PIN; SPI_SDO = 0; SPI_SCK = 0; PHY_WAKE_TRIS = OUTPUT_PIN; PHY_WAKE = 1; MRF24J40_PWR_TRIS = OUTPUT_PIN; MRF24J40_PWR = 1; SPICONCLR = 0xFFFFFFFF; // Clear SPIxCON register #ifdef HARDWARE_SPI /* Enable SPI, Set to Master Mode & Set CKE bit : Serial output * data changes on transition from active clock state to Idle * clock state */ SPICON = 0x00008120; /* PB Frequency can be maximum 40 MHz */ if(pbFreq > (2 * MAX_SPI_CLK_FREQ_FOR_P2P)){ SPI_Brg = 1; /* Continue the loop till you find SPI Baud Rate Reg Value */ while(1){ /* SPI Clock Calculation as per PIC32 Manual */ SPI_Clk_Freq = pbFreq / (2 * (SPI_Brg + 1)); if(SPI_Clk_Freq <= MAX_SPI_CLK_FREQ_FOR_P2P){ break; } SPI_Brg++; } #if defined MRF24J40_IN_SPI1 mSpiChnSetBrg (1, SPI_Brg); #elif defined MRF24J40_IN_SPI2 mSpiChnSetBrg (2, SPI_Brg); #elif defined MRF24J40_IN_SPI3 mSpiChnSetBrg (1A, SPI_Brg); #elif defined MRF24J40_IN_SPI4 mSpiChnSetBrg (3A, SPI_Brg); #endif } else{ #if defined MRF24J40_IN_SPI1 mSpiChnSetBrg (1, 0); #elif defined MRF24J40_IN_SPI2 mSpiChnSetBrg (2, 0); #elif defined MRF24J40_IN_SPI3 mSpiChnSetBrg (1A, 0); #elif defined MRF24J40_IN_SPI4 mSpiChnSetBrg (3A, SPI_Brg); #endif } #endif #endif #if defined(MRF49XA_1) // pruebas de funcionamiento /* MRF49XA_1_PHY_CS_TRIS = OUTPUT_PIN; MRF49XA_1_PHY_CS = 0; MRF49XA_1_PHY_CS = 1; MRF49XA_1_PHY_RESETn_TRIS = OUTPUT_PIN; MRF49XA_1_PHY_RESETn = 0; MRF49XA_1_PHY_RESETn = 1; MRF49XA_1_INT_TRIS = OUTPUT_PIN; MRF49XA_1_INT_PIN = 0; MRF49XA_1_INT_PIN = 1; MRF49XA_1_SDI_TRIS = OUTPUT_PIN; MRF49XA_1_SPI_SDI = 0; MRF49XA_1_SPI_SDI = 1; MRF49XA_1_SDO_TRIS = OUTPUT_PIN; MRF49XA_1_SPI_SDO = 0; MRF49XA_1_SPI_SDO = 1; MRF49XA_1_SCK_TRIS = OUTPUT_PIN; MRF49XA_1_SPI_SCK = 0; MRF49XA_1_SPI_SCK = 1; MRF49XA_1_nFSEL_TRIS = OUTPUT_PIN; MRF49XA_1_nFSEL = 0; MRF49XA_1_nFSEL = 1; MRF49XA_1_FINT_TRIS = OUTPUT_PIN; MRF49XA_1_FINT = 0; MRF49XA_1_FINT = 1; */ // configuration. Juan: Added; Agus: Modified to a standard way MRF49XA_1_PHY_CS_TRIS = OUTPUT_PIN; MRF49XA_1_PHY_CS = 1; MRF49XA_1_PHY_RESETn_TRIS = OUTPUT_PIN; MRF49XA_1_PHY_RESETn = 1; MRF49XA_1_INT_TRIS = INPUT_PIN; MRF49XA_1_SDI_TRIS = INPUT_PIN; MRF49XA_1_SDO_TRIS = OUTPUT_PIN; MRF49XA_1_SCK_TRIS = OUTPUT_PIN; MRF49XA_1_SPI_SDO = 0; MRF49XA_1_SPI_SCK = 0; MRF49XA_1_nFSEL_TRIS = OUTPUT_PIN; MRF49XA_1_FINT_TRIS = INPUT_PIN; MRF49XA_1_nFSEL = 1; // nFSEL inactive #ifdef cNGD_PLATFORM // MRF49XA_1_PWR_TRIS = OUTPUT_PIN; // MRF49XA_1_PWR = 1; #endif MRF49XA_1_SPICONCLR = 0xFFFFFFFF; //Clear SPIxCON register #ifdef HARDWARE_SPI /* Enable SPI1, Set to Master Mode & Set CKE bit : Serial output * data changes on transition from active clock state to Idle * clock state */ MRF49XA_1_SPICON = 0x00008120; /* PB Frequency can be maximum 40 MHz */ if(pbFreq > (2 * MAX_SPI_CLK_FREQ_FOR_P2P)){ SPI_Brg = 1; /* Continue the loop till you find SPI Baud Rate Reg Value */ while(1){ /* SPI Clock Calculation as per PIC32 Manual */ SPI_Clk_Freq = pbFreq / (2 * (SPI_Brg + 1)); if(SPI_Clk_Freq <= MAX_SPI_CLK_FREQ_FOR_P2P){ break; } SPI_Brg++; } #if defined MRF49XA_1_IN_SPI1 mSpiChnSetBrg (1, SPI_Brg); #elif defined MRF49XA_1_IN_SPI2 mSpiChnSetBrg (2, SPI_Brg); #elif defined MRF49XA_1_IN_SPI3 mSpiChnSetBrg (1A, SPI_Brg); #endif } else{ #if defined MRF49XA_1_IN_SPI1 mSpiChnSetBrg (1, 0); #elif defined MRF49XA_1_IN_SPI2 mSpiChnSetBrg (2, 0); #elif defined MRF49XA_1_IN_SPI3 mSpiChnSetBrg (1A, 0); #endif } #endif #endif #if defined(MRF49XA_2) MRF49XA_2_PHY_CS_TRIS = OUTPUT_PIN; MRF49XA_2_PHY_CS = 1; MRF49XA_2_PHY_RESETn_TRIS = OUTPUT_PIN; MRF49XA_2_PHY_RESETn = 1; MRF49XA_2_INT_TRIS = 1; MRF49XA_2_SDI_TRIS = INPUT_PIN; MRF49XA_2_SDO_TRIS = OUTPUT_PIN; MRF49XA_2_SCK_TRIS = OUTPUT_PIN; MRF49XA_2_SPI_SDO = 0; MRF49XA_2_SPI_SCK = 0; MRF49XA_2_nFSEL_TRIS = OUTPUT_PIN; MRF49XA_2_FINT_TRIS = INPUT_PIN; MRF49XA_2_nFSEL = 1; // nFSEL inactive #ifdef cNGD_PLATFORM MRF49XA_2_PWR_TRIS = OUTPUT_PIN; MRF49XA_2_PWR = 1; #endif MRF49XA_2_SPICONCLR = 0xFFFFFFFF; // Clear SPIxCON register #ifdef HARDWARE_SPI /* Enable SPI1, Set to Master Mode & Set CKE bit : Serial output * data changes on transition from active clock state to Idle * clock state */ MRF49XA_2_SPICON = 0x00008120; /* PB Frequency can be maximum 40 MHz */ if(pbFreq > (2 * MAX_SPI_CLK_FREQ_FOR_P2P)){ SPI_Brg = 1; /* Continue the loop till you find SPI Baud Rate Reg Value */ while(1){ /* SPI Clock Calculation as per PIC32 Manual */ SPI_Clk_Freq = pbFreq / (2 * (SPI_Brg + 1)); if(SPI_Clk_Freq <= MAX_SPI_CLK_FREQ_FOR_P2P){ break; } SPI_Brg++; } #if defined MRF49XA_2_IN_SPI1 mSpiChnSetBrg (1, SPI_Brg); #elif defined MRF49XA_2_IN_SPI2 mSpiChnSetBrg (2, SPI_Brg); #elif defined MRF49XA_2_IN_SPI3 mSpiChnSetBrg (1A, SPI_Brg); #endif } else{ #if defined MRF49XA_2_IN_SPI1 mSpiChnSetBrg (1, 0); #elif defined MRF49XA_2_IN_SPI2 mSpiChnSetBrg (2, 0); #elif defined MRF49XA_2_IN_SPI3 mSpiChnSetBrg (1A, 0); #endif } #endif #endif #if defined MRF89XA Data_nCS_TRIS = 0; Config_nCS_TRIS = 0; Data_nCS = 1; Config_nCS = 1; PHY_IRQ1_TRIS = 1; //... REVIEW... #endif // SPI & EXTERNAL INTERRUPTS PINS AND CONFIGURATION ----------------------// /* Set the SPI Port Directions (SDO, SDI, SCK) for every SPI module.*/ #if defined SPI1_IN_USE SDI1_TRIS = INPUT_PIN; //DIGITAL IN SDO1_TRIS = OUTPUT_PIN; //DIGITAL OUT SCK1_TRIS = OUTPUT_PIN; //DIGITAL OUT #endif #if defined SPI2_IN_USE SDI2_TRIS = INPUT_PIN; //DIGITAL IN SDO2_TRIS = OUTPUT_PIN; //DIGITAL OUT SCK2_TRIS = OUTPUT_PIN; //DIGITAL OUT #endif #if defined SPI3_IN_USE SDI3_TRIS = INPUT_PIN; //DIGITAL IN SDO3_TRIS = OUTPUT_PIN; //DIGITAL OUT SCK3_TRIS = OUTPUT_PIN; //DIGITAL OUT #endif #if defined SPI4_IN_USE SDI4_TRIS = INPUT_PIN; //DIGITAL IN SDO4_TRIS = OUTPUT_PIN; //DIGITAL OUT SCK4_TRIS = OUTPUT_PIN; //DIGITAL OUT #endif /* Set the external interrups Pin Directions and Priority*/ #if defined INT1_IN_USE INT1_TRIS = INPUT_PIN; // DIGITAL IN mINT1SetIntPriority(4); mINT1SetIntSubPriority(2); mINT1SetEdgeMode(0); //0: Falling Edge. // Enable INT1 mINT1IntEnable(1); #endif #if defined INT2_IN_USE INT2_TRIS = INPUT_PIN; // DIGITAL IN mINT2SetIntPriority(4); mINT2SetIntSubPriority(2); mINT2SetEdgeMode(0); //0: Falling Edge. /* Enable INT2 */ mINT2IntEnable(1); #endif #if defined INT3_IN_USE INT3_TRIS = INPUT_PIN; // DIGITAL IN mINT3SetIntPriority(4); mINT3SetIntSubPriority(2); mINT3SetEdgeMode(0); //0: Falling Edge. /* Enable INT3 */ mINT3IntEnable(1); #endif #if defined INT4_IN_USE INT4_TRIS = INPUT_PIN; // DIGITAL IN mINT4SetIntPriority(4); mINT4SetIntSubPriority(2); mINT4SetEdgeMode(0); //0: Falling Edge. /* Enable INT4 */ mINT4IntEnable(1); #endif // LEDs #ifdef cNGD_PLATFORM mJTAGPortEnable(0); //Needed due to multiplexed pins LED1_TRIS = OUTPUT_PIN; LED2_TRIS = OUTPUT_PIN; LED3_TRIS = OUTPUT_PIN; LED1 = 0; LED2 = 0; LED3 = 0; #endif // TIMER 1 FOR TIME_SYNC -------------------------------------------------// #if defined(ENABLE_TIME_SYNC) //TIMER 1 MAY BE USED FOR SLEEP MODE AND/OR FOR STACKS MAINTENANCE. IT //NEEDS ADAPTATION BEFORE ENABLING TIME_SYNC WITH TIMER 1 TOO! T1CON = 0; T1CON = 0x0012; T1CONSET = 0x8000; PR1 = 0xFFFF; IFS0bits.T1IF = 0; mT1IntEnable(1); mT1SetIntPriority(4); while(T1CONbits.TWIP); TMR1 = 0; #endif // TIMER 1 FOR NODE STACKS AUTO-MAINTENANCE ------------------------------// #if defined NODE_DOES_MAINTENANCE_TASKS T1CON = 0x0070; //Disable timer, PBCLK source, PS=256 TMR1 = 0x0000; //Reset count PR1 = MAINTENANCE_PERIOD; //Set period. IPC1SET = 0x00000005; //Set Priority level 1, Subpriority level 1 IFS0CLR = 0x00000010; //Clear T1IF IEC0SET = 0x00000010; //Set T1IE //Timer will be triggered after initialization. #endif //************************************* TODO // IOPORT CN - For waking up the node manually. --------------------------// mPORTDSetPinsDigitalIn(BIT_5); // CN14 CNCON = 0x8000; //Module enabled. CNEN = 0x00004000; //Enable CN14 CNPUE = 0x00004000; //Enable CN14 weak pull-up. ReadBUTTONS(); //Clear PORT mismatch condition. IFS1CLR = 0x00000001; //Clear the CN interrupt flag status bit IPC6SET = 0x00180000; //Set CN priority 6, subpriority 0. //It will be enabled only during sleep mode time interval //------------------------------------------------------------------------// // Lo modifico en el wifi config #if defined(ENABLE_NVM) //REVIEW //EE_nCS_TRIS = 0;//FERNANDO, CUIDADO NO SE SI LA PILA REALMENTE FUNCIONA CON FLASH MEMORY //EE_nCS = 1; #endif // INTERRUPTION FLAGS AND EXT_INT PIN FINAL SETTINGS ---------------------// #if defined MRF49XA_1 MRF49XA_1_IF = 0; if(MRF49XA_1_INT_PIN == 0){ MRF49XA_1_IF = 1; } #endif #if defined MRF49XA_2 MRF49XA_2_IF = 0; if(MRF49XA_2_INT_PIN == 0){ MRF49XA_2_IF = 1; } #endif #if defined MRF89XA PHY_IRQ1 = 0; #endif #if defined MRF24J40 MRF24J40_IF = 0; if(MRF24J40_INT_PIN == 0){ MRF24J40_IF = 1; } #endif #else //Not PIC32. #error "Unknown target board." #endif }
/******************************************************************************* * Function: BoardInit(void) * PreCondition:None * Input: None * Output: None * Overview: SPI pins and SFR, Maintenance Tasks Timer, External Interrupts, * and other board issues initialization. * Note: This routine needs to be called before initialising MiWi stack * or invoking other function that operates on MiWi stack. ******************************************************************************/ void BoardInit(void){ #if defined(__PIC32MX__) // RADIO INTERFACES & SPI INIT -------------------------------------------// #if defined HARDWARE_SPI /* Peripheral Bus Frequency = System Clock / PB Divider */ unsigned int pbFreq; pbFreq = (DWORD) CLOCK_FREQ/(1 << mOSCGetPBDIV()); unsigned int SPI_Clk_Freq; unsigned char SPI_Brg; #endif #if defined MRF24J40 PHY_CS_TRIS = 0; PHY_CS = 1; PHY_RESETn_TRIS = 0; PHY_RESETn = 1; MRF24J40_INT_TRIS = 1; SDI_TRIS = 1; SDO_TRIS = 0; SCK_TRIS = 0; SPI_SDO = 0; SPI_SCK = 0; PHY_WAKE_TRIS = 0; PHY_WAKE = 1; SPICONCLR = 0xFFFFFFFF; // Clear SPIxCON register #ifdef HARDWARE_SPI /* Enable SPI, Set to Master Mode & Set CKE bit : Serial output * data changes on transition from active clock state to Idle * clock state */ SPICON = 0x00008120; /* PB Frequency can be maximum 40 MHz */ if(pbFreq > (2 * MAX_SPI_CLK_FREQ_FOR_P2P)){ SPI_Brg = 1; /* Continue the loop till you find SPI Baud Rate Reg Value */ while(1){ /* SPI Clock Calculation as per PIC32 Manual */ SPI_Clk_Freq = pbFreq / (2 * (SPI_Brg + 1)); if(SPI_Clk_Freq <= MAX_SPI_CLK_FREQ_FOR_P2P){ break; } SPI_Brg++; } #if defined MRF24J40_IN_SPI1 mSpiChnSetBrg (1, SPI_Brg); #elif defined MRF24J40_IN_SPI2 mSpiChnSetBrg (2, SPI_Brg); #elif defined MRF24J40_IN_SPI3 mSpiChnSetBrg (1A, SPI_Brg); #elif defined MRF24J40_IN_SPI4 mSpiChnSetBrg (3A, SPI_Brg); #endif } else{ #if defined MRF24J40_IN_SPI1 mSpiChnSetBrg (1, 0); #elif defined MRF24J40_IN_SPI2 mSpiChnSetBrg (2, 0); #elif defined MRF24J40_IN_SPI3 mSpiChnSetBrg (1A, 0); #elif defined MRF24J40_IN_SPI4 mSpiChnSetBrg (3A, SPI_Brg); #endif } #endif #endif #if defined(MRF49XA_1) //Configuration for Guilja's Expansion Board, Connection SLot 1 --// mPORTESetPinsDigitalOut(BIT_1); //nCS mPORTBSetPinsDigitalIn(BIT_2); //FINT //Juan: Added. //----------------------------------------------------------------// MRF49XA_1_PHY_CS_TRIS = 0; MRF49XA_1_PHY_CS = 1; MRF49XA_1_PHY_RESETn_TRIS = 0; MRF49XA_1_PHY_RESETn = 1; MRF49XA_1_INT_TRIS = 1; MRF49XA_1_SDI_TRIS = 1; MRF49XA_1_SDO_TRIS = 0; MRF49XA_1_SCK_TRIS = 0; MRF49XA_1_SPI_SDO = 0; MRF49XA_1_SPI_SCK = 0; MRF49XA_1_nFSEL_TRIS = 0; MRF49XA_1_FINT_TRIS = 1; MRF49XA_1_nFSEL = 1; // nFSEL inactive MRF49XA_1_SPICONCLR = 0xFFFFFFFF; //Clear SPIxCON register #ifdef HARDWARE_SPI /* Enable SPI1, Set to Master Mode & Set CKE bit : Serial output * data changes on transition from active clock state to Idle * clock state */ MRF49XA_1_SPICON = 0x00008120; /* PB Frequency can be maximum 40 MHz */ if(pbFreq > (2 * MAX_SPI_CLK_FREQ_FOR_P2P)){ SPI_Brg = 1; /* Continue the loop till you find SPI Baud Rate Reg Value */ while(1){ /* SPI Clock Calculation as per PIC32 Manual */ SPI_Clk_Freq = pbFreq / (2 * (SPI_Brg + 1)); if(SPI_Clk_Freq <= MAX_SPI_CLK_FREQ_FOR_P2P){ break; } SPI_Brg++; } #if defined MRF49XA_1_IN_SPI1 mSpiChnSetBrg (1, SPI_Brg); #elif defined MRF49XA_1_IN_SPI2 mSpiChnSetBrg (2, SPI_Brg); #elif defined MRF49XA_1_IN_SPI3 mSpiChnSetBrg (1A, SPI_Brg); #endif } else{ #if defined MRF49XA_1_IN_SPI1 mSpiChnSetBrg (1, 0); #elif defined MRF49XA_1_IN_SPI2 mSpiChnSetBrg (2, 0); #elif defined MRF49XA_1_IN_SPI3 mSpiChnSetBrg (1A, 0); #endif } #endif #endif #if defined(MRF49XA_2) MRF49XA_2_PHY_CS_TRIS = 0; MRF49XA_2_PHY_CS = 1; MRF49XA_2_PHY_RESETn_TRIS = 0; MRF49XA_2_PHY_RESETn = 1; MRF49XA_2_INT_TRIS = 1; MRF49XA_2_SDI_TRIS = 1; MRF49XA_2_SDO_TRIS = 0; MRF49XA_2_SCK_TRIS = 0; MRF49XA_2_SPI_SDO = 0; MRF49XA_2_SPI_SCK = 0; MRF49XA_2_nFSEL_TRIS = 0; MRF49XA_2_FINT_TRIS = 1; MRF49XA_2_nFSEL = 1; // nFSEL inactive MRF49XA_2_SPICONCLR = 0xFFFFFFFF; // Clear SPIxCON register #ifdef HARDWARE_SPI /* Enable SPI1, Set to Master Mode & Set CKE bit : Serial output * data changes on transition from active clock state to Idle * clock state */ MRF49XA_2_SPICON = 0x00008120; /* PB Frequency can be maximum 40 MHz */ if(pbFreq > (2 * MAX_SPI_CLK_FREQ_FOR_P2P)){ SPI_Brg = 1; /* Continue the loop till you find SPI Baud Rate Reg Value */ while(1){ /* SPI Clock Calculation as per PIC32 Manual */ SPI_Clk_Freq = pbFreq / (2 * (SPI_Brg + 1)); if(SPI_Clk_Freq <= MAX_SPI_CLK_FREQ_FOR_P2P){ break; } SPI_Brg++; } #if defined MRF49XA_2_IN_SPI1 mSpiChnSetBrg (1, SPI_Brg); #elif defined MRF49XA_2_IN_SPI2 mSpiChnSetBrg (2, SPI_Brg); #elif defined MRF49XA_2_IN_SPI3 mSpiChnSetBrg (1A, SPI_Brg); #endif } else{ #if defined MRF49XA_2_IN_SPI1 mSpiChnSetBrg (1, 0); #elif defined MRF49XA_2_IN_SPI2 mSpiChnSetBrg (2, 0); #elif defined MRF49XA_2_IN_SPI3 mSpiChnSetBrg (1A, 0); #endif } #endif #endif // SPI & EXTERNAL INTERRUPTS PINS AND CONFIGURATION ----------------------// #if (defined __32MX675F256L__ || defined ____32MX675F512__) /* Set the SPI Port Directions (SDO, SDI, SCK) for every SPI module.*/ #if defined MRF49XA_1_IN_SPI1 || defined MRF49XA_2_IN_SPI1 || \ defined MRF89XA_IN_SPI1 || defined MRF24J40_IN_SPI1 || \ defined MRF24WB0M_IN_SPI1 mPORTDSetPinsDigitalOut(BIT_0); //SDO1 mPORTDSetPinsDigitalOut(BIT_10); //SCK1 mPORTCSetPinsDigitalIn(BIT_4); //SDI1 #endif #if defined MRF49XA_1_IN_SPI2 || defined MRF49XA_2_IN_SPI2 || \ defined MRF89XA_IN_SPI2 || defined MRF24J40_IN_SPI2 || \ defined MRF24WB0M mPORTGSetPinsDigitalOut(BIT_8); //SDO2 mPORTGSetPinsDigitalOut(BIT_6); //SCK2 mPORTGSetPinsDigitalIn(BIT_7); //SDI2 #endif #if defined MRF49XA_1_IN_SPI3 || defined MRF49XA_2_IN_SPI3 || \ defined MRF89XA_IN_SPI3 || defined MRF24J40_IN_SPI3 || \ defined MRF24WB0M mPORTFSetPinsDigitalOut(BIT_8); //SDO3 mPORTDSetPinsDigitalOut(BIT_15); //SCK3 mPORTFSetPinsDigitalIn(BIT_2); //SDI3 #endif // #if defined MRF24WB0M_IN_SPI4 // mPORTFSetPinsDigitalOut(BIT_5); //SDO4 // mPORTFSetPinsDigitalOut(BIT_13); //SCK4 // mPORTFSetPinsDigitalIn(BIT_4); //SDI4 // #endif #endif #endif // TIMER 1 FOR TIME_SYNC -------------------------------------------------// #if defined(ENABLE_TIME_SYNC) //TIMER 1 MAY BE USED FOR SLEEP MODE AND/OR FOR STACKS MAINTENANCE. IT //NEEDS ADAPTATION BEFORE ENABLING TIME_SYNC WITH TIMER 1 TOO! T1CON = 0; T1CON = 0x0012; T1CONSET = 0x8000; PR1 = 0xFFFF; IFS0bits.T1IF = 0; mT1IntEnable(1); mT1SetIntPriority(4); while(T1CONbits.TWIP); TMR1 = 0; #endif // TIMER 1 FOR NODE STACKS AUTO-MAINTENANCE ------------------------------// #if defined NODE_DOES_MAINTENANCE_TASKS T1CON = 0x0070; //Disable timer, PBCLK source, PS=256 TMR1 = 0x0000; //Reset count PR1 = MAINTENANCE_PERIOD; //Set period. IPC1SET = 0x00000005; //Set Priority level 1, Subpriority level 1 IFS0CLR = 0x00000010; //Clear T1IF IEC0SET = 0x00000010; //Set T1IE //Timer will be triggered after initialization. #endif // IOPORT CN - For waking up the node manually. --------------------------// mPORTDSetPinsDigitalIn(BIT_5); // CN14 CNCON = 0x8000; //Module enabled. CNEN = 0x00004000; //Enable CN14 CNPUE = 0x00004000; //Enable CN14 weak pull-up. ReadBUTTONS(); //Clear PORT mismatch condition. IFS1CLR = 0x00000001; //Clear the CN interrupt flag status bit IPC6SET = 0x00180000; //Set CN priority 6, subpriority 0. //It will be enabled only during sleep mode time interval //------------------------------------------------------------------------// // Lo modifico en el wifi config #if defined(ENABLE_NVM) //REVIEW //EE_nCS_TRIS = 0;//FERNANDO, CUIDADO NO SE SI LA PILA REALMENTE FUNCIONA CON FLASH MEMORY //EE_nCS = 1; #endif // INTERRUPTION FLAGS AND EXT_INT PIN FINAL SETTINGS ---------------------// #if defined MRF49XA_1 MRF49XA_1_IF = 0; if(MRF49XA_1_INT_PIN == 0){ MRF49XA_1_IF = 1; } #endif #if defined MRF49XA_2 MRF49XA_2_IF = 0; if(MRF49XA_2_INT_PIN == 0){ MRF49XA_2_IF = 1; } #endif #if defined MRF89XA PHY_IRQ1 = 0; #endif #if defined MRF24J40 MRF24J40_IF = 0; if(MRF24J40_INT_PIN == 0){ MRF24J40_IF = 1; } #endif }