void FA_NAKED( init_hw ) ( void ) { // Initialize ports (ATmega32U4) DDRE = DDE ; PORTE = PEPU ; DDRF = DDF ; PORTF = PFPU ; // #endif // Initialize timers SetTMPS( 0, 64 ) ; // Set T0 prescaler to / 64 for query & us delay SetTMPS( 1, 1024 ) ; // Set T1 prescaler to / 1024 for ms delay EICRA = _B1(ISC01) | _B1(ISC00) ; // Need INT0 on rising edges Delay_1024( T0DEL200MS ) ; // Allow the stick to boot for ( ;; ) // Forever.. { Flash_LED_12MS() ; // Flash LED and wait Delay_1024( T0DEL200MS ) ; // Try to read a data packet, QueryFFP( 0, 126 ) ; // don't know how long - let it time out Flash_LED_12MS() ; // Analyze clock count if ( ! ~sw_clkcnt ) sw_clkcnt = 0 ; if ( sw_clkcnt == (16 + 1) // FFP/PP in 3-bit mode || sw_clkcnt == (48 + 1) ) // 3DPP in 1-bit mode { if ( InitFFPro() ) // found FFP/PP { break ; // break forever } } dis3DP_INT() ; } dis3DP_INT() ; // Disable INT cli() ; // Disable interrupts sw_buttons = BUTMSK ; // All buttons released RET() ; // naked also means no ret.. }
int FA_NORETURN( main ) ( void ) { uint8_t sw_sendrep ; // Initialize.. init_hw() ; // hardware. Note: defined as naked ! sw_reportsz = // report size (sw_id == SW_ID_3DP ? SW_REPSZ_3DP : SW_REPSZ_FFP) + ADDED_REPORT_DATA_SIZE; usb_init() ; // USB stack sei() ; // Enable interrupts SetTMPS( 1, 64 ) ; // Set T1 prescaler to /64 SetTMPS( 0, 1024 ) ; // Set T0 prescaler to / 1024 for delay sw_sendrep = sw_repchg() ; // Init send report flag, saved report WaitMs(1000); FfbInitMidi(); wdt_enable( WDTO_500MS ) ; // Unleash watchdog uint8_t ffb_init_sent = 0, ffb_test_sent = 0, ffb_pause_sent = 0; for ( ;; ) // Forever.. { wdt_reset() ; // Calm watchdog if ( usb_configuration ) { if ( TMexp( 0 ) ) // Time to read the stick { SetTMPS( 0, 64 ) ; // Set T0 prescaler to / 64 for query getdata() ; // read 3DP data SetTMPS( 0, 1024 ) ; // Set T0 prescaler to / 1024 for delay ResetTM( 0, READ_DEL ) ; // Read the additional controls // ???? TODO: read pedals and trims sw_report[sw_reportsz-1] = 5; // ???? test a rudder position if ( sw_repchg() ) // Report changed, sw_sendrep = TRUE ; // need to send it } if ( TMexp( 1 ) ) // Idle timed out { ResetTM( 1, IDLE_DEL ) ; if ( ! --idle_cnt && idle_rate )// Idle counter expired, rate not indef. sw_sendrep = TRUE ; // need to send current report } if ( sw_sendrep && ! usb_IN_busy() ) { LED_on() ; // ???? Test FFB: when certain buttons are pressed // -------0 -------1 -------2 -------3 -------4 -------5 // XXXXXXXX YYYYYYXX HHHHYYYY BBRRRRRR TBBBBBBB 00TTTTTT // 76543210 54321098 32109876 21543210 09876543 654321 if (sw_report[3] & 0b01000000) // Button 1 { if (ffb_init_sent == 0) { ffb_init_sent = 1; FfbInitMidi(); } } else ffb_init_sent = 0; if (sw_report[3] & 0b10000000) // button 2 { if (ffb_test_sent == 0) { ffb_test_sent = 1; FfbTest(); } } else ffb_test_sent = 0; if (sw_report[4] & 0b00000001) // Button 3 { if (ffb_pause_sent == 0) { ffb_pause_sent = 1; FfbSendDisable(); } } else ffb_pause_sent = 0; usb_send_IN( sw_report, sw_reportsz ) ; sw_sendrep = FALSE ; // reset send report flag idle_cnt = idle_rate ; // reset idle counter ResetTM( 1, IDLE_DEL ) ; // reset idle timer LED_off() ; } } else { ResetTM( 1, IDLE_DEL ) ; // reset idle timer ResetTM( 0, READ_DEL ) ; // reset SW timer } } }