// MAIN TASK INITIALIZATIOON result_t Task_SmartPHTApp_Init(void) { result_t result; adc_init(); rtc_initialize(); rtc_set(&statusBarData.time); RESULT_CHECK( ioexp_initialize(), result); RESULT_CHECK( touch_initialize(), result); battery_init(); battery_enable_usb_charger(bTrue); battery_enable_ac_charger(bTrue); install_event_handlers(); gui_set_current_view(&view_dashboard); // Initialize USB initCDC(); // setup the CDC state machine usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS); // initialize USB. TODO: Remove magic with macro usb_start(); //start the USB peripheral EnableUsbPerifInterrupts(USB_TRN + USB_SOF + USB_UERR + USB_URST); EnableUsbGlobalInterrupt(); // Only enables global USB interrupt. Chip interrupts must be enabled by the user (PIC18) sensor_init(); reset_minmax(); reset_alarms(); // Reset waveforms wfrm_clear(&wfrmPressure); wfrm_clear(&wfrmHumidity); wfrm_clear(&wfrmTemperature); // Set plot scale lineplot_set_sample_per(&lineplot, &supportedSamplingPeriods[sliderSampPeriodData.value]); // Default sensor checkable_set_checked(&btnSelInterface0, bTrue); return RV_OK; }
int main(void) #endif { BYTE RecvdByte; initCDC(); // setup the CDC state machine SetupBoard(); //setup the hardware, customize for your hardware usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS); // initialize USB. TODO: Remove magic with macro usb_start(); //start the USB peripheral // PIC18 INTERRUPTS // It is the users resposibility to set up high, low or legacy mode // interrupt operation. The following macros for high and low interrupt // setup have been removed: //#define EnableUsbHighPriInterrupt() do { RCONbits.IPEN = 1; IPR2bits.USBIP = 1; INTCONbits.GIEH = 1;} while(0) // JTR new //#define EnableUsbLowPriInterrupt() do { RCONbits.IPEN = 1; IPR2bits.USBIP = 0; INTCONbits.GIEL = 1;} while(0) // JTR new // By default, the interrupt mode will be LEGACY (ISR Vector 0x08) // (Same as high priority vector wise but the operation (latency) is // not the same. Consult the data sheet for details.) // If a priority mode is enabled then this affects ALL other interrupt // sources therefore it does not belong to the usb stack to be // doing this. It is a global, user application choice. #if defined USB_INTERRUPTS // See the prj_usb_config.h file. EnableUsbPerifInterrupts(USB_TRN + USB_SOF + USB_UERR + USB_URST); #if defined __18CXX //turn on interrupts for PIC18 INTCONbits.PEIE = 1; INTCONbits.GIE = 1; #endif EnableUsbGlobalInterrupt(); // Only enables global USB interrupt. Chip interrupts must be enabled by the user (PIC18) #endif // Wait for USB to connect do { #ifndef USB_INTERRUPTS usb_handler(); #endif } while (usb_device_state < CONFIGURED_STATE); usb_register_sof_handler(CDCFlushOnTimeout); // Register our CDC timeout handler after device configured // Main echo loop do { // If USB_INTERRUPT is not defined each loop should have at least one additional call to the usb handler to allow for control transfers. #ifndef USB_INTERRUPTS usb_handler(); #endif // Receive and send method 1 // The CDC module will call usb_handler each time a BULK CDC packet is sent or received. // If there is a byte ready will return with the number of bytes available and received byte in RecvdByte if (poll_getc_cdc(&RecvdByte)) putc_cdc(RecvdByte+1); // // Receive and send method 2 // Same as poll_getc_cdc except that byte is NOT removed from queue. // This function will wait for a byte and return and remove it from the queue when it arrives. if (peek_getc_cdc(&RecvdByte)) { RecvdByte = getc_cdc(); putc_cdc(RecvdByte+1); } // Receive and send method 3 // If there is a byte ready will return with the number of bytes available and received byte in RecvdByte // use CDC_Flush_In_Now(); when it has to be sent immediately and not wait for a timeout condition. if (poll_getc_cdc(&RecvdByte)) { putc_cdc(RecvdByte+1); // CDC_Flush_In_Now(); } } while (1); } //end main
void Initialize(void) { #if defined (BUSPIRATEV2) unsigned char i; #endif volatile unsigned long delay = 0xffff; // volatile unsigned long delay = 0xffff; // TBLPAG = 0; // we need to be in page 0 (somehow this isn't set) #if defined (BUSPIRATEV2) || defined (BUSPIRATEV1A) CLKDIVbits.RCDIV0 = 0; //clock divider to 0 AD1PCFG = 0xFFFF; // Default all pins to digital #elif defined (BUSPIRATEV4) INTCON1bits.NSTDIS = 1; CLKDIV = 0x0000; // Set PLL prescaler (1:1) BP_LEDUSB_DIR = 0; // output CORCONbits.PSV = 1; // JTR PIC24 fixup ?? PSV not being initialized. May have been done by c_init though. PSVPAG = 0; // OSCCONbits.SOSCEN = 0; AD1PCFGL = 0x7FD8; //BUSPIRATEV4 has five analog pins b0, b1, b2, b5, b15 AD1PCFGH = 0x2; // usb_register_sof_handler(0); #endif OSCCONbits.SOSCEN = 0; while (delay--); //set pin configuration using peripheral pin select #if defined (BUSPIRATEV2) || defined (BUSPIRATEV1A) BP_TERM_RX = BP_TERM_RX_RP; //Inputs UART1 RX RPINR18bits.U1RXR=4; BP_TERM_TX_RP = BP_TERM_TX; // Outputs UART1 TX RPOR1bits.RP3R=U1TX_IO; #elif defined (BUSPIRATEV4) && defined (BPV4_DEBUG) BP_TERM_RX = BP_TERM_RX_RP; //Inputs UART1 RX RPINR18bits.U1RXR=11;//AUX2 BP_TERM_TX_RP = BP_TERM_TX; // Outputs UART1 TX RPOR1bits.RP2R=U1TX_IO;//AUX1 #endif //put startup values in config (do first) bpConfig.termSpeed = 8; //default PC side port speed, startup in 115200, or saved state (later).... bpConfig.displayMode = HEX; bpInit(); //put startup values in config (do first)clean up, exit in HI-Z #if defined (BUSPIRATEV2) || defined (BUSPIRATEV1A) InitializeUART1(); //init the PC side serial port #endif #if defined (BUSPIRATEV4) && !defined (BPV4_DEBUG) initCDC(); usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS); usb_start(); #endif #if defined (BUSPIRATEV4) && defined (BPV4_DEBUG) InitializeUART1(); //init the PC side serial port #endif #if defined (BUSPIRATEV2) //find the Bus Pirate revision //pullup on, do it now so it can settle during the next operations CNPU1bits.CN6PUE = 1; CNPU1bits.CN7PUE = 1; #endif //#ifndef BUSPIRATEV4 // Get the chip type and revision bpConfig.dev_type = bpReadFlash(DEV_ADDR_UPPER, DEV_ADDR_TYPE); bpConfig.dev_rev = bpReadFlash(DEV_ADDR_UPPER, DEV_ADDR_REV); //#endif #if defined (BUSPIRATEV2) //now check the revision //Version | RB3 | RB2 //2go, 3a | 1 | 1 //v3b | 1 | 0 //v3.5 | 0 | 0 i=PORTB; //get settings i=i>>2; //remove unused i&=(~0b11111100); //clear others if (i==0b11) { bpConfig.HWversion = 'a'; } else if(i==0b10){ bpConfig.HWversion = 'b'; }else if(i==0){ bpConfig.HWversion = '5'; } //pullup off CNPU1bits.CN6PUE = 0; CNPU1bits.CN7PUE = 0; #else bpConfig.HWversion = 0; #endif bpConfig.quiet = 0; // turn output on (default) modeConfig.numbits = 8; #ifdef BP_USE_BASIC initpgmspace(); #endif TBLPAG = 0; // we need to be in page 0 (somehow this isn't set) #ifndef BUSPIRATEV4 bpWBR; //send a line feed versionInfo(); //prints hardware and firmware version info (base.c) #endif }
void initialize_board(void) { volatile unsigned long delay = 0xFFFF; #ifdef BUSPIRATEV3 /* Set clock divider to 0. */ CLKDIVbits.RCDIV0 = 0; /* All pins are digital. */ AD1PCFG = 0xFFFF; #elif defined(BUSPIRATEV4) /* Disable nested interrupts. */ INTCON1bits.NSTDIS = ON; /* Set PLL prescaler to 1:1 ratio. */ CLKDIV = 0x0000; BP_LEDUSB_DIR = OUTPUT; /* Initialize program space visibility. */ CORCONbits.PSV = ON; /* Map top part of the firmware. */ PSVPAG = 0; /* Disable secondary oscillator. */ OSCCONbits.SOSCEN = OFF; /* Set pins B0, B1, B2, B5, and B15 as analog, rest are digital. */ AD1PCFGL = 0x7FD8; /* Only enable Vbg/2 channel for input scan. */ AD1PCFGH = 0x0002; #endif /* BUSPIRATEV3 || BUSPIRATEV4 */ /* Disable secondary oscillator. */ OSCCONbits.SOSCEN = OFF; /* Wait a bit. */ while (delay--) { } /* Set up the UART port pins. */ #ifdef BUSPIRATEV3 BP_TERM_RX = BP_TERM_RX_RP; BP_TERM_TX_RP = BP_TERM_TX; #endif /* BUSPIRATEV3 */ /* Set the UART port speed to 115200 bps. */ bus_pirate_configuration.terminal_speed = 8; /* Default to print numbers in hexadecimal format. */ bus_pirate_configuration.display_mode = HEX; /* Start from a known, clear state. */ bp_reset_board_state(); #ifdef BUSPIRATEV3 /* Initialize the internal UART port. */ user_serial_initialise(); #endif /* BUSPIRATEV3 */ #if defined(BUSPIRATEV4) /* Initialize the USB-based serial port. */ initCDC(); usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS); usb_start(); #endif /* BUSPIRATEV4 */ #ifdef BUSPIRATEV3 /* Turn pull-ups ON. */ CNPU1bits.CN6PUE = ON; CNPU1bits.CN7PUE = ON; #endif /* BUSPIRATEV3 */ /* Read device type and revision. */ bus_pirate_configuration.device_type = bp_read_from_flash(DEV_ADDR_UPPER, DEV_ADDR_TYPE); bus_pirate_configuration.device_revision = bp_read_from_flash(DEV_ADDR_UPPER, DEV_ADDR_REV); #ifdef BUSPIRATEV3 /* Get the revision identifier. */ bus_pirate_configuration.hardware_version = BPV3_HARDWARE_VERSION_TABLE[(PORTB >> 2) & 0b00000011]; /* Turn pull-ups OFF. */ CNPU1bits.CN6PUE = OFF; CNPU1bits.CN7PUE = OFF; #else bus_pirate_configuration.hardware_version = 0; #endif /* BUSPIRATEV3 */ bus_pirate_configuration.quiet = OFF; mode_configuration.numbits = 8; #ifdef BP_ENABLE_BASIC_SUPPORT bp_basic_initialize(); #endif /* BP_ENABLE_BASIC_SUPPORT */ /* Move to page #0. */ TBLPAG = 0; #ifndef BUSPIRATEV4 bpBR; print_version_info(); #endif /* !BUSPIRATEV4 */ }
void main(void) { static BYTE ledtrig; BYTE OutByte; SetupBoard(); //setup the hardware, USB SetupRC5(); usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS); // TODO: Remove magic with macro initCDC(); // JTR this function has been highly modified It no longer sets up CDC endpoints. usb_start(); //usbbufflush(); //flush USB input buffer system ledtrig = 1; //only shut LED off once // Never ending loop services each task in small increments while (1) { do { // if (!TestGlobalUsbInterruptEnabled()) //JTR3 added usb_handler(); ////service USB tasks Guaranteed one pass in polling mode even when usb_device_state == CONFIGURED_STATE if ((usb_device_state < DEFAULT_STATE)) { // JTR2 no suspendControl available yet || (USBSuspendControl==1) ){ LedOff(); } else if (usb_device_state < CONFIGURED_STATE) { LedOff(); } else if ((ledtrig == 1) && (usb_device_state == CONFIGURED_STATE)) { // LedOn(); ledtrig = 0; } } while(usb_device_state < CONFIGURED_STATE); //TRISB &= 0x7f; //TRISB |= 0x40; //LATB |= 0x40; //mode = IRWIDGET; //irWidgetservice(); #ifdef UARTONLY //mode = USB_UART; Usb2UartService(); // Never returns #endif // Test for commands: 0, 1, 2 (Entry to SUMP MODE.) // Do not remove from input buffer, just take a PEEK. // SUMPLogicCommand will remove from input buffer // (Standardized coding) if (peek_getc_cdc(&OutByte)) { switch (OutByte) { case 0: //SUMP clear case 1: //SUMP run case 2: //SUMP ID mode = IR_SUMP; //put IR Toy in IR_SUMP mode irSUMPservice(); // Fully self contained, does not return until exited via 0x00 command. cdc_In_len = 0; mode = IR_DECODER; SetupRC5(); break; case 'r': //IRMAN decoder mode case 'R': OutByte = getc_cdc(); // now ok to remove byte from the USB buffer SetupRC5(); mode = IR_DECODER; putc_cdc('O'); putc_cdc('K'); CDC_Flush_In_Now(); break; case 'S': //IRs Sampling Mode case 's': OutByte = getc_cdc(); // now ok to remove byte from the USB buffer mode = IR_S; irsService(); // Fully self contained, does not return until exited via 0x00 command. cdc_In_len = 0; mode = IR_DECODER; SetupRC5(); break; case 'U': case 'u': OutByte = getc_cdc(); // now ok to remove byte from the USB buffer mode = USB_UART; Usb2UartService(); break; case 'P': case 'p':// IR Widget mode OutByte = getc_cdc(); // now ok to remove byte from the USB buffer mode = IRWIDGET; GetPulseFreq(); // Never returns //GetPulseTime(); break; case 'T': case 't'://self test OutByte = getc_cdc(); // now ok to remove byte from the USB buffer SelfTest(); //run the self-test break; case 'V': case 'v':// Acquire Version OutByte = getc_cdc(); // now ok to remove byte from the USB buffer GetUsbIrToyVersion(); break; case '$'://bootloader jump OutByte = getc_cdc(); // now ok to remove byte from the USB buffer BootloaderJump(); break; default: OutByte = getc_cdc(); }//switch(OutByte } // if peek OutByte == 1 ProcessIR(); //increment IR decoder state machine }//end while(1) }//end main
void main(void) { unsigned char c,x,y,led; unsigned long l; // Default all pins to digital ADCON1 |= 0x0F; // Disable USB external transceiver to allow RB1/RB2 UCFGbits.UTRDIS = 0; // set pin to output TRISBbits.TRISB0 = 0; TRISBbits.TRISB1 = 0; TRISBbits.TRISB2 = 0; TRISBbits.TRISB3 = 0; LED0 = 1; LED1 = LED2 = LED3 = 0; // TODO: Remove magic with macro usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS); usb_start(); initCDC(); usbbufflush(); do { if (!TestUsbInterruptEnabled()) USBDeviceTasks(); switch(usb_device_state){ case DETACHED_STATE: LED1 = 0; LED2 = 0; LED3 = 0; case ATTACHED_STATE: LED1 = 0; LED2 = 0; LED3 = 1; case POWERED_STATE: LED1 = 0; LED2 = 1; LED3 = 0; case DEFAULT_STATE: LED1 = 0; LED2 = 1; LED3 = 1; case ADR_PENDING_STATE: LED1 = 1; LED2 = 0; LED3 = 0; case ADDRESS_STATE: LED1 = 1; LED2 = 0; LED3 = 1; case CONFIGURED_STATE: LED1 = 1; LED2 = 1; LED3 = 0; } } while (usb_device_state < CONFIGURED_STATE); while(1); #if 0 // Initiate Osc with 8MHz (only relevant if you use the internal oscillator) //OSCCON = 0x70; initCDC(); // TODO: Remove magic with macro usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS); usb_start(); #if defined (USB_INTERRUPTS) //EnableUsbInterrupt(USB_TRN + USB_SOF + USB_UERR + USB_URST); EnableUsbInterrupt(USB_STALL + USB_IDLE + USB_TRN + USB_ACTIV + USB_SOF + USB_UERR + USB_URST); EnableUsbInterrupts(); #endif usbbufflush(); //flush USB input buffer system led = UCFGbits.UTRDIS; LED0=0; LED1=0; LED2=0; LED3=0; while(1){ LED1 = LED1 ? 0 : 1; for(l = 0; l < 10; ++l); } LED0=1; LED1=1; LED2=1; LED3=1; delay_ms(500); #if 0 while(1) { LED0 = 1; delay_ms(250); LED0 = 0; delay_ms(250); } #endif LED0 = 0; LED1 = 0; LED2 = 0; LED3 = 0; delay_ms(500); while(1); do { LED2=1; LED3 = 0; #ifndef USB_INTERRUPTS //service USB tasks //Guaranteed one pass in polling mode //even when usb_device_state == CONFIGURED_STATE if (!TestUsbInterruptEnabled()) { USBDeviceTasks(); LED3 = 1; } #endif if ((usb_device_state < DEFAULT_STATE)) { // JTR2 no suspendControl available yet || (USBSuspendControl==1) ){ LED0=1; LED1=0; } else if (usb_device_state < CONFIGURED_STATE) { LED0=1; LED1=1; }else if((usb_device_state == CONFIGURED_STATE)) { LED0=0; LED1=1; } LED2=0; delay_ms(1000); } while (usb_device_state < CONFIGURED_STATE); LED0 = 0; LED1 = 0; LED2 = 1; #if 1 while(1) { LED0 = led; led = led ? 0 : 1; #ifndef USB_INTERRUPTS //service USB tasks //Guaranteed one pass in polling mode //even when usb_device_state == CONFIGURED_STATE if (!TestUsbInterruptEnabled()) USBDeviceTasks(); #endif usbbufservice(); //service USB buffer system if (usbbufgetbyte(&c) == 1) { WaitInReady(); cdc_In_buffer[0] = c; //answer OK putUnsignedCharArrayUsbUsart(cdc_In_buffer, 1); } } #endif // Disable USB external transceiver to allow RB1/RB2 UCFGbits.UTRDIS = 0; // set pin to output TRISBbits.TRISB0 = 0; TRISBbits.TRISB1 = 0; TRISBbits.TRISB2 = 0; // start a loop blinking the led while(1) { LED0 = 1; delay_ms(250); LED0 = 0; delay_ms(250); } #endif }
void main(void){ u8 i,cmd, param[9],c; u16 temp; // TODO to be removed later on long l; signed int x, y, z; signed char x8, y8, z8; static unsigned char ledtrig; initCDC(); // JTR this function has been highly modified It no longer sets up CDC endpoints. init(); //setup the crystal, pins mma_init(); usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS); // TODO: Remove magic with macro usb_start(); usbbufflush(); //flush USB input buffer system i = mma_read( MMA_I2CAD ); //if((i&~0b10000000)!=0x1d)while(1); mma_write( MMA_I2CAD, 0x1d | MMA_I2CDIS_bit ); // disable I2C i = mma_read( MMA_CTL1 ); mma_write( MMA_CTL1, i |= MMA_DFBW_bit ); // high output data rate mma_write( MMA_MCTL, 0b00000101 ); // 2g range; measurement mode //mma_calibrate_offset( 0, 0, 0 ); //LEDs //PWM on cathode TRISAbits.TRISA0=0; LATAbits.LATA0=1; //enable cathode //setup LEDs LATB=0; TRISB=0; ledtrig=1; //only shut LED off once // Never ending loop services each task in small increments while (1) { do { if (!TestUsbInterruptEnabled()) //JTR3 added USBDeviceTasks(); ////service USB tasks Guaranteed one pass in polling mode even when usb_device_state == CONFIGURED_STATE if ((usb_device_state < DEFAULT_STATE)) { // JTR2 no suspendControl available yet || (USBSuspendControl==1) ){ //LedOff(); } else if (usb_device_state < CONFIGURED_STATE) { //LedOn(); }else if((ledtrig==1) && (usb_device_state == CONFIGURED_STATE)){ //LedOff(); ledtrig=0; } } while (usb_device_state < CONFIGURED_STATE); usbbufservice();//load any USB data into byte buffer if(usbbufgetbyte(&inByte) == 1){ switch(inByte){//switch on the command case 0x00: WaitInReady(); //it's always ready, but this could be done better param[0]='B'; param[1]='B'; param[2]='I'; param[3]='O'; param[4]='1'; putUnsignedCharArrayUsbUsart(param,5); break; case 0x01: mma_get_average8( 4, &x8, &y8, &z8 ); param[0]=x8; param[1]=y8; param[2]=z8; WaitInReady(); //it's always ready, but this could be done better putUnsignedCharArrayUsbUsart(param,3); break; default: //error break; } }//if data }//while //#endif }//end main