void Usb2UartService(void) { BYTE tempout; Usb2UartSetup(); do { usb_handler(); #ifdef USE_HNDSHK_POL_INV if (Hold_pol) { RTSpin = cls.RTS; DTRpin = cls.DTR; } else { RTSpin = ~cls.RTS; DTRpin = ~cls.DTR; } #endif if (TxIf) { if (poll_getc_cdc(&tempout)) { TXREG = tempout; } } if (RCSTA & 0x06) // error handling { RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; } } while (1); }
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 Task_SmartPHTApp(void) { message_t msg; adc_voltages_t *v; sensor_data_t *c; byte_t comRecvByte; if (knl_mailbox_pend(&taskMailbox, &msg) == RV_OK) { switch (msg.MsgId) { case MSG_ID_SYS_VOLTAGE_UPDATE: v = (adc_voltages_t*) msg.Param0; //tiny_printf("SYS: Vbat=%dmV, Vcc=%dmV, Vdd=%dmV, Vtft=%dmV\n", v->vbat, v->vcc, v->vdd, v->vtft); // if(v->vdd < 3000) // tty_output("SYS: system voltage is low!\n"); // if(v->vbat < 3300) // tty_output("SYS: BATTERY LOW! replace or battery can be damaged\n"); break; case MSG_ID_SYS_BATTERY_STAT: // if(msg.Param0 == BAT_CHARGE_COMPLETE) // tty_output("SYS: battery charge complete.\n"); // if(msg.Param0 == BAT_CHARGING) // tty_output("SYS: charging...\n"); // if(msg.Param0 == BAT_SHUTDOWN) // tty_output("SYS: charger shtdn.\n"); // break; break; case MSG_ID_USB_STATE_CHANGED: handle_usb_change(msg.Param0); break; case MSG_ID_SENSOR_UPDATE: c = (sensor_data_t*) msg.Param0; filter(&dashboardData.pressure, c->compensated.hp_pressure); filter(&dashboardData.humidity, c->compensated.sht_humidity); filter(&dashboardData.temperature, c->compensated.sht_temperature); update_min_max(&dashboardData.pressure); update_min_max(&dashboardData.humidity); update_min_max(&dashboardData.temperature); check_alarms(); // update waveforms wfrm_put_sample(&wfrmPressure, dashboardData.pressure.val); wfrm_put_sample(&wfrmHumidity, dashboardData.humidity.val); wfrm_put_sample(&wfrmTemperature, dashboardData.temperature.val); // Write to file write_samples_to_file(c, (rtc_time_t*) msg.Param1); gui_invalidate(&dashb); gui_invalidate(&lineplot); break; case MSG_ID_MEDIA_DETECTED: //tty_output("SDCARD: inserted."); //if(msg.Param0) tty_output(" Card is write protected!"); //tty_output("\n"); sd_register(); f_close(&samplesFile); // Mount filesystem f_mount(0, &filesystem); // Try open file; sampleFileIsOpen = try_open_file(); // Change statusbar toolbar_set_status(&statusbar, STATUS_SDCARD_PRESENT); break; case MSG_ID_MEDIA_DISAPPEARED: //tty_output("SDCARD: removed\n"); // Dismount file system f_mount(0, NULL); // Change statusbar toolbar_clear_status(&statusbar, STATUS_SDCARD_PRESENT); break; case MSG_ID_WDG_PRESSED: handle_button_press_event(msg.Param0); break; case MSG_ID_WDG_DELAYED_OP: handle_delayed_operation_event(msg.Param0); break; case MSG_ID_WDG_CHECKED: handle_button_checked_event(msg.Param0); break; case MSG_ID_WDG_INVALIDATE: // GUI needs to be redrawn gui_draw(); break; case MSG_ID_LPM_REQUEST: enter_sleep_mode(); break; case MSG_ID_TOUCH_EVENT: // // Draw cursor // if(msg.Param0 == TOUCH_EVENT_PANEL_PRESSED) // { // uint16_t i, j; // i = msg.Param1; // j = msg.Param2; // // if(i < 12) i = 12; // else if(i > 467) i = 467; // if(j < 12) j = 12; // else if(j > 260) j = 260; // // gfx_set_color(COLOR_WHITE); // gfx_draw_circle(i, j , 10); // } if(!darkenScreen) { set_brightness(lightenBrightness); } darkenScreen = bTrue; // no break, gui must get this msg too default: gui_msg(&msg); break; } } // COMMAND PARSER if(usb_device_state == CONFIGURED_STATE) if (poll_getc_cdc(&comRecvByte)) { putc_cdc(comRecvByte); // echo CDC_Flush_In_Now(); cmd_parse_char(comRecvByte); } }