void GCInit_Help(double defaultMinRatio, double defaultMaxRatio, int defaultMinRatioSize, int defaultMaxRatioSize) { MinHeapByte = Min(MinHeapByte, MaxHeapByte); init_double(&MinRatio, defaultMinRatio); init_double(&MaxRatio, defaultMaxRatio); init_int(&MinRatioSize, defaultMinRatioSize); init_int(&MaxRatioSize, defaultMaxRatioSize); fromSpace = Heap_Alloc(MinHeapByte, MaxHeapByte); toSpace = Heap_Alloc(MinHeapByte, MaxHeapByte); Heap_Resize(fromSpace, (MinHeapByte + MaxHeapByte) / 2, 1); Heap_Resize(toSpace, (MinHeapByte + MaxHeapByte) / 2, 1); }
void hardware_init(){ init_timer1(); init_timer0(); init_external(); init_int(); LS1=0xFF; }
/** Genera e inizializza una nuova struttura di tipo inotify_t \return da notare che, se ci sono stati errori, lo si potrà sapere analizzando il valore di new_inotify_t().instance == -1 */ inotify_t new_inotify_t(void) { inotify_t r; init_int(&r.watch_fds); init_str(&r.files); r.instance = inotify_init(); return r; }
void memobj_init(void) { unsigned long min_bytes, max_bytes; unsigned long mem_bytes = GetPhysicalPages() * 0.90 * TILT_PAGESIZE; unsigned long cache_bytes = GetBcacheSize() * 2; min_bytes = 2048 * 1024; min_bytes = Max(min_bytes, cache_bytes); max_bytes = (unsigned long)INT_MAX; max_bytes = Min(max_bytes, mem_bytes); max_bytes = Min(max_bytes, rlimit(RLIMIT_DATA)); max_bytes = Min(max_bytes, rlimit(RLIMIT_AS)); #ifdef RLIMIT_VMEM max_bytes = Min(max_bytes, rlimit(RLIMIT_VMEM)); #endif init_int(&MinHeapByte, min_bytes); init_int(&MaxHeapByte, 0.40 * max_bytes); assert(MinHeapByte <= MaxHeapByte); #ifdef sparc assert(TILT_PAGESIZE == sysconf(_SC_PAGESIZE)); #else assert(TILT_PAGESIZE == sysconf(_SC_PAGE_SIZE)); #endif StackInitialize(); HeapInitialize(); GuardStackletSize = TILT_PAGESIZE / kilobyte; stackletOffset = (GuardStackletSize + MLStackletSize + CStackletSize) * kilobyte; primaryStackletOffset = 0; replicaStackletOffset = stackletOffset; /* So we don't pay mmap for first thread - general case? XXXX */ { int i; Stacklet_t *temp[5]; for (i=0; i<5; i++) temp[i] = Stacklet_Alloc(NULL); for (i=0; i<5; i++) Stacklet_Dealloc(temp[i]); } }
int main(int argc,char ** argv){ int * visitado,otimo; Tour pop[TAMPOP], * melhor; struct tms before,after; if(argc != 3){ printf("USO : %s 'instancia' 'otimo' \n\n",argv[0]); return 1; } otimo = atoi(argv[2]); times(&before); read(argv[1],d,n); visitado = new_int(n); init_int(visitado,n); pop[0] = rand_tour(); pop[1] = rand_tour(); createSet(); LinKernighan(pop[0]); LinKernighan(pop[1]); pop[2] = new_tour(); pop[2]->c = new_int(n); pop[2]->pos = new_int(n); pop[3] = new_tour(); pop[3]->c = new_int(n); pop[3]->pos = new_int(n); PMX(pop[2],pop[3],pop[0],pop[1]); LinKernighan(pop[2]); LinKernighan(pop[3]); printf("PAI 1 : \n"); print_tour(pop[0]); printf("PAI 2 : \n"); print_tour(pop[1]); printf("FILHO 1 : \n"); print_tour(pop[2]); printf("FILHO 2 : \n"); print_tour(pop[3]); melhor = &pop[0]; //print_tour(melhor); times(&after); double tempo = (double)(after.tms_utime - before.tms_utime)/(double)100; double gap = ((double)((*(melhor))->cost-otimo)/(double) otimo) * 100.0; printf("%s\t%.2lfs\t%d\t%d\t%.3lf\n",argv[1],tempo,otimo,(*(melhor))->cost,gap); //printf("System time: %ld seconds\n", after.tms_stime - before.tms_stime); return 0; }
void GCInit(void) { init_int(&minOffRequest, 1 * TILT_PAGESIZE); init_int(&maxOffRequest, 8 * TILT_PAGESIZE); init_int(&minOnRequest, 1 * TILT_PAGESIZE); init_int(©PageSize, TILT_PAGESIZE / 2); init_int(©CheckSize, TILT_PAGESIZE / 2); init_int(©ChunkSize, 256); minOffRequest = RoundUp(minOffRequest, TILT_PAGESIZE); minOnRequest = RoundUp(minOnRequest, TILT_PAGESIZE); reset_statistic(&minorSurvivalStatistic); reset_statistic(&heapSizeStatistic); reset_statistic(&majorSurvivalStatistic); switch (collector_type) { case Semispace: GCFun = GC_Semi; GCReleaseFun = GCRelease_Semi; GCPollFun = NULL; GCInit_Semi(); break; case Generational: GCFun = GC_Gen; GCReleaseFun = GCRelease_Gen; GCPollFun = NULL; GCInit_Gen(); break; case SemispaceParallel: GCFun = GC_SemiPara; GCReleaseFun = GCRelease_SemiPara; GCPollFun = GCPoll_SemiPara; GCInit_SemiPara(); break; case GenerationalParallel: GCFun = GC_GenPara; GCReleaseFun = GCRelease_GenPara; GCPollFun = GCPoll_GenPara; GCInit_GenPara(); break; case SemispaceConcurrent: GCFun = GC_SemiConc; GCReleaseFun = GCRelease_SemiConc; GCPollFun = GCPoll_SemiConc; GCInit_SemiConc(); break; case GenerationalConcurrent: GCFun = GC_GenConc; GCReleaseFun = GCRelease_GenConc; GCPollFun = GCPoll_GenConc; GCInit_GenConc(); break; default: DIE("bad collector type"); } if (forceMirrorArray) mirrorArray = 1; }
void Display_rcinput(void) { // Re-enable interrupts. High speed mode may have left them off init_int(); while(BUTTON1 != 0) { if (BUTTON4 == 0) { CenterSticks(); } if (BUTTON3 == 0) { SetFailsafe(); } RxGetChannels(); LCD_Display_Text(19,(const unsigned char*)Verdana8,0,0); LCD_Display_Text(32,(const unsigned char*)Verdana8,0,10); LCD_Display_Text(20,(const unsigned char*)Verdana8,0,20); LCD_Display_Text(35,(const unsigned char*)Verdana8,0,30); LCD_Display_Text(109,(const unsigned char*)Verdana8,70,0); LCD_Display_Text(110,(const unsigned char*)Verdana8,70,10); LCD_Display_Text(111,(const unsigned char*)Verdana8,70,20); LCD_Display_Text(112,(const unsigned char*)Verdana8,70,30); mugui_lcd_puts(itoa(MonopolarThrottle,pBuffer,10),(const unsigned char*)Verdana8,37,0); mugui_lcd_puts(itoa(RCinputs[AILERON],pBuffer,10),(const unsigned char*)Verdana8,37,10); mugui_lcd_puts(itoa(RCinputs[ELEVATOR],pBuffer,10),(const unsigned char*)Verdana8,37,20); mugui_lcd_puts(itoa(RCinputs[RUDDER],pBuffer,10),(const unsigned char*)Verdana8,37,30); mugui_lcd_puts(itoa(RCinputs[GEAR],pBuffer,10),(const unsigned char*)Verdana8,100,0); mugui_lcd_puts(itoa(RCinputs[AUX1],pBuffer,10),(const unsigned char*)Verdana8,100,10); mugui_lcd_puts(itoa(RCinputs[AUX2],pBuffer,10),(const unsigned char*)Verdana8,100,20); mugui_lcd_puts(itoa(RCinputs[AUX3],pBuffer,10),(const unsigned char*)Verdana8,100,30); // Print bottom text and markers LCD_Display_Text(12, (const unsigned char*)Wingdings, 0, 57); // Left LCD_Display_Text(21, (const unsigned char*)Verdana8, 40, 55); // Failsafe LCD_Display_Text(9, (const unsigned char*)Wingdings, 80, 59); // Down LCD_Display_Text(60, (const unsigned char*)Verdana8, 100, 55); // Cal. LCD_Display_Text(9, (const unsigned char*)Wingdings, 119, 59); // Down // Update buffer write_buffer(buffer); clear_buffer(buffer); } }
/* Initialize all kernel subsystems and run system */ int main(void) { printa("In main %x\n", (unsigned)main); init_int(); init_page_alloc(); init_pipes(); init_scheduler(); add_task(&init_systems); while (1) { schedule(); } return 0; }
/* * init_config() - Get the system parameter values from the * configuration file and create run-time variables. */ static void init_config() { /* * Determine which memory integral to use. */ MEMINT = init_int(MEMORY_INTEGRAL, 1, TRUE); /* * Initialize variables needed for multitasking and pacct sbus. */ init_pacct_sbu(); return; }
int main(int argc, char** argv) { stack_int pile; init_int(&pile); push_int(&pile,12); push_int(&pile,24); if(empty_int(&pile) == 0) { printf("Top : %d \n",top_int(&pile)); pop_int(&pile); printf("Top : %d \n",top_int(&pile)); } return (EXIT_SUCCESS); }
void GCInit_GenConc(void) { int cache_size = GetBcacheSize(); double nurseryFraction = 1.2 * NumProc; if (ordering == DefaultOrder) ordering = StackOrder; if (ordering == HybridOrder) grayAsReplica = 1; GCInit_Help(0.2, 0.8, 512, 50 * 1024); if (relaxed) { reducedTenuredSize = Heap_GetSize(fromSpace); expandedTenuredSize = reducedToExpanded(reducedTenuredSize, CollectionRate, doAgressive ? 2 : 1); } else { expandedTenuredSize = Heap_GetSize(fromSpace); reducedTenuredSize = expandedToReduced(expandedTenuredSize, CollectionRate, doAgressive ? 2 : 1); Heap_Resize(fromSpace, reducedTenuredSize, 0); Heap_Resize(toSpace, reducedTenuredSize, 0); } init_int(&NurseryByte, (int)(nurseryFraction * cache_size)); assert(MinHeapByte >= 1.2*NurseryByte); reducedNurserySize = NurseryByte; expandedNurserySize = reducedToExpanded(reducedNurserySize, CollectionRate, doAgressive ? 2 : 1); maximumNurserySize = 2 * expandedNurserySize; nursery = Heap_Alloc(maximumNurserySize, maximumNurserySize); Heap_Resize(nursery, reducedNurserySize, 0); gc_large_init(); workStack = SharedStack_Alloc(1, 100, 16 * 1024, 12 * 1024, 96 * 1024, 16 * 1024, 2048, 256 * 1024); barriers = createBarriers(NumProc, 14); arraySegmentSize = 2 * 1024; mirrorGlobal = 1; mirrorArray = 1; pauseWarningThreshold = 10.0; addOldStackletOnUnderflow = 1; }
void menu_rc_setup(uint8_t section) { int8_t *value_ptr; menu_range_t range; uint8_t text_link; uint8_t i = 0; uint8_t mult = 1; // Multiplier uint8_t offset; // Index into channel structure uint8_t items; // Items in group // If submenu item has changed, reset submenu positions if (menu_flag) { sub_top = RCSTART; menu_flag = 0; } while(button != BACK) { // Get menu offsets and load values from eeprom // 1 = RC, 2 = Failsafe, 3 = General switch(section) { case 1: // RC setup menu offset = 0; items = RCITEMS; value_ptr = &Config.RxModeIn; mult = 1; break; case 2: // Failsafe menu offset = RCITEMS; items = FSITEMS; value_ptr = &Config.FailsafeType; mult = 1; break; case 3: // General menu offset = RCITEMS + FSITEMS; items = GENERALITEMS; value_ptr = &Config.MixMode; mult = 1; break; default: offset = 0; items = RCITEMS; value_ptr = &Config.RxModeIn; mult = 1; break; } // Save pre-edited values int8_t temp_type = Config.MixMode; int8_t temp_flapchan = Config.FlapChan; int8_t temp_RxModeIn = Config.RxModeIn; // Print menu print_menu_items(sub_top + offset, RCSTART + offset, value_ptr, mult, (const unsigned char*)rc_menu_ranges[section - 1], 0, RCOFFSET, (const unsigned char*)RCMenuText[section - 1], cursor); // Handle menu changes update_menu(items, RCSTART, offset, button, &cursor, &sub_top, &menu_temp); range = get_menu_range ((const unsigned char*)rc_menu_ranges[section - 1], (menu_temp - RCSTART - offset)); if (button == ENTER) { text_link = pgm_read_byte(&RCMenuText[section - 1][menu_temp - RCSTART - offset]); do_menu_item(menu_temp, value_ptr + (menu_temp - RCSTART - offset), mult, range, 0, text_link, false, 0); } if (button == ENTER) { // Update Ch5. mixer with source from Config.FlapChan if in Aeroplane mode and source changed if ((Config.MixMode == AEROPLANE) && (Config.FlapChan != temp_flapchan)) { Config.Channel[CH5].source_a = Config.FlapChan; } // Reset serial in channel masks every time the input type is changed if (temp_RxModeIn != Config.RxModeIn) { Xtreme_Chanmask = 0; Xtreme_RSS = 0; Spektrum_Chanmask_0 = 0; Spektrum_Chanmask_1 = 0; Spektrum_frameloss = 0; SBUS_Flags = 0; // Clear channel data for (i = 0; i < MAX_RC_CHANNELS; i++) { RxChannel[i] = 0; // Unused Spektrum channels set to NULL if (Config.RxModeOut == SPEKTRUM) { ExtChannel[i] = 0xFFFF; } // Unused channels set to mid-way else if (Config.RxModeOut == SBUS) { ExtChannel[i] = 0x400; } // Xtreme doesn't care else { ExtChannel[i] = 0; } } } // If model type has changed, reload preset if ((section == 3) && (temp_type != Config.MixMode)) { switch(Config.MixMode) // Load selected mix { case AEROPLANE: get_preset_mix(AEROPLANE_MIX); break; case FWING: get_preset_mix(FLYING_WING_MIX); break; case MANUAL: // Clear all channel info memset(&Config.Channel[0].value,0,(sizeof(channel_t) * MAX_OUTPUTS)); // Preset important settings for (i = 0; i < MAX_OUTPUTS; i++) { Config.Channel[i].source_a = i; // Set to mirror the inputs Config.Channel[i].source_a_volume = 100; Config.Channel[i].source_b = NOCHAN; Config.Channel[i].output_b = UNUSED; Config.Channel[i].output_c = UNUSED; } break; default: break; } } init_int(); // In case RC type has changed, reinitialise interrupts init_uart(); // and UART UpdateLimits(); // Update I-term limits and triggers based on percentages #ifdef KK21 // Update MPU6050 LPF writeI2Cbyte(MPU60X0_DEFAULT_ADDRESS, MPU60X0_RA_CONFIG, (6 - Config.MPU6050_LPF)); #endif // Update channel sequence for (i = 0; i < MAX_RC_CHANNELS; i++) { if (Config.TxSeq == FUTABASEQ) { Config.ChannelOrder[i] = pgm_read_byte(&FUTABA[i]); } else { Config.ChannelOrder[i] = pgm_read_byte(&JR[i]); } } Save_Config_to_EEPROM(); // Save value and return Wait_BUTTON4(); // Wait for user's finger off the button } } }
void menu_rc_setup(uint8_t section) { static uint8_t rc_top = RCSTART; int8_t *value_ptr; menu_range_t range; uint8_t text_link; uint8_t i = 0; uint8_t mult = 1; // Multiplier uint8_t offset; // Index into channel structure uint8_t items; // Items in group uint16_t temp16_1; // If submenu item has changed, reset submenu positions if (menu_flag) { rc_top = RCSTART; menu_flag = 0; } while(button != BACK) { // Get menu offsets and load values from eeprom // 1 = RC, 2 = Failsafe, 3 = General, 4 = Battery switch(section) { case 1: // RC setup menu offset = 0; items = RCITEMS; value_ptr = &Config.RxMode; mult = 1; break; case 2: // Failsafe menu offset = RCITEMS; items = FSITEMS; value_ptr = &Config.FailsafeType; mult = 1; break; case 3: // General menu offset = RCITEMS + FSITEMS; items = GENERALITEMS; value_ptr = &Config.MixMode; mult = 1; break; case 4: // Battery menu offset = RCITEMS + FSITEMS + GENERALITEMS; items = BATTITEMS; value_ptr = &Config.BatteryType; mult = 4; break; default: offset = 0; items = RCITEMS; value_ptr = &Config.RxMode; mult = 1; break; } // Save pre-edited values int8_t temp_type = Config.MixMode; int8_t temp_cells = Config.BatteryCells; int8_t temp_minvoltage = Config.MinVoltage; int8_t temp_flapchan = Config.FlapChan; // Print menu print_menu_items(rc_top + offset, RCSTART + offset, value_ptr, mult, (prog_uchar*)rc_menu_ranges[section - 1], 0, RCOFFSET, (prog_uchar*)RCMenuText[section - 1], cursor); // Handle menu changes update_menu(items, RCSTART, offset, button, &cursor, &rc_top, &menu_temp); range = get_menu_range ((prog_uchar*)rc_menu_ranges[section - 1], (menu_temp - RCSTART - offset)); if (button == ENTER) { text_link = pgm_read_byte(&RCMenuText[section - 1][menu_temp - RCSTART - offset]); do_menu_item(menu_temp, value_ptr + (menu_temp - RCSTART - offset), mult, range, 0, text_link, false, 0); } if (button == ENTER) { // Update Ch7. mixer with source from Config.FlapChan if in Aeroplane mode and source changed if ((Config.MixMode == AEROPLANE) && (Config.FlapChan != temp_flapchan)) { Config.Channel[CH7].source_a = Config.FlapChan; } // See if cell number or min_volts has changed if ((temp_cells != Config.BatteryCells) || (temp_minvoltage != Config.MinVoltage)) { // Recalculate if more cells temp16_1 = Config.MinVoltage; temp16_1 = temp16_1 * Config.BatteryCells; temp16_1 = temp16_1 / 10; Config.PowerTrigger = (int8_t)temp16_1; } // If model type has changed, reload preset if ((section == 3) && (temp_type != Config.MixMode)) { switch(Config.MixMode) // Load selected mix { case AEROPLANE: get_preset_mix(AEROPLANE_MIX); break; case FWING: get_preset_mix(FLYING_WING_MIX); break; case CAMSTAB: get_preset_mix(CAM_STAB); break; case MANUAL: // Clear all channel info memset(&Config.Channel[0].value,0,(sizeof(channel_t) * PSUEDO_OUTPUTS)); // Preset important settings for (i = 0; i < PSUEDO_OUTPUTS; i++) { Config.Channel[i].source_a = NOCHAN; Config.Channel[i].source_b = NOCHAN; Config.Channel[i].output_b = UNUSED; Config.Channel[i].output_c = UNUSED; Config.Channel[i].output_d = UNUSED; } break; default: break; } } init_int(); // In case RC type has changed, reinitialise interrupts init_uart(); // and UART UpdateIMUvalues(); // Update IMU variables UpdateLimits(); // Update I-term limits and triggers based on percentages #ifdef KK21 // Update MPU6050 LPF writeI2Cbyte(MPU60X0_DEFAULT_ADDRESS, MPU60X0_RA_CONFIG, Config.MPU6050_LPF); #endif // Update channel sequence for (i = 0; i < MAX_RC_CHANNELS; i++) { if (Config.TxSeq == FUTABASEQ) { Config.ChannelOrder[i] = (uint8_t)pgm_read_byte(&FUTABA[i]); } else { Config.ChannelOrder[i] = (uint8_t)pgm_read_byte(&JR[i]); } } Save_Config_to_EEPROM(); // Save value and return } } }
void init(void) { uint8_t i; bool updated; //*********************************************************** // I/O setup //*********************************************************** // Set port directions DDRA = 0x30; // Port A DDRB = 0x0A; // Port B DDRC = 0xFC; // Port C DDRD = 0xF2; // Port D // Hold all PWM outputs low to stop glitches // M5 and M6 are on PortA for KK2.1 MOTORS = 0; M5 = 0; M6 = 0; // Preset I/O pins LED1 = 0; // LED1 off LVA = 0; // LVA alarm OFF LCD_SCL = 1; // GLCD clock high // Set/clear pull-ups (1 = set, 0 = clear) PINB = 0xF5; // Set PB pull-ups PIND = 0x0C; // Set PD pull-ups (Don't pull up RX yet) //*********************************************************** // Spektrum receiver binding. Must be done immediately on power-up // // 3 low pulses: DSM2 1024/22ms // 5 low pulses: DSM2 2048/11ms // 7 low pulses: DSMX 1024/22ms // 9 low pulses: DSMX 2048/11ms //*********************************************************** PIND = 0x0C; // Release RX pull up on PD0 _delay_ms(63); // Pause while satellite wakes up // and pull-ups have time to rise. // Tweak until bind pulses about 68ms after power-up // Bind as master if any single button pressed. // NB: Have to wait until the button pull-ups rise before testing for a button press. // Button 1 if ((PINB & 0xf0) == 0x70) { DDRD = 0xF3; // Switch PD0 to output bind_master(3); } // Button 2 if ((PINB & 0xf0) == 0xb0) { DDRD = 0xF3; // Switch PD0 to output bind_master(5); } // Button 3 if ((PINB & 0xf0) == 0xd0) { DDRD = 0xF3; // Switch PD0 to output bind_master(7); } // Button 4 if ((PINB & 0xf0) == 0xE0) { DDRD = 0xF3; // Switch PD0 to output bind_master(9); } DDRD = 0xF2; // Reset Port D directions PIND = 0x0D; // Set PD pull-ups (now pull up RX as well) //*********************************************************** // Timers //*********************************************************** // Timer0 (8bit) - run @ 20MHz / 1024 = 19.531kHz or 51.2us - max 13.1ms // Slow timer to extend Timer 1 TCCR0A = 0; // Normal operation TCCR0B = 0x05; // Clk / 1024 = 19.531kHz or 51.2us - max 13.1ms TIMSK0 |= (1 << TOIE0); // Enable interrupts TCNT0 = 0; // Reset counter // Timer1 (16bit) - run @ 2.5MHz (400ns) - max 26.2ms // Used to measure Rx Signals & control ESC/servo output rate TCCR1A = 0; TCCR1B |= (1 << CS11); // Clk/8 = 2.5MHz // Timer2 8bit - run @ 20MHz / 1024 = 19.531kHz or 51.2us - max 13.1ms // Used to time arm/disarm intervals TCCR2A = 0; TCCR2B = 0x07; // Clk/1024 = 19.531kHz TIMSK2 = 0; TIFR2 = 0; TCNT2 = 0; // Reset counter //*********************************************************** // Interrupts and pin function setup //*********************************************************** // Pin change interrupt enables PCINT1, PCINT2 and PCINT3 (Throttle, AUX and CPPM input) PCICR = 0x0A; // PCINT8 to PCINT15 (PCINT1 group - AUX) // PCINT24 to PCINT31 (PCINT3 group - THR) PCIFR = 0x0F; // Clear PCIF0 interrupt flag // Clear PCIF1 interrupt flag // Clear PCIF2 interrupt flag // Clear PCIF3 interrupt flag // External interrupts INT0 (Elevator) and INT1 (Aileron) and INT2 (Rudder) EICRA = 0x15; // Any change INT0 // Any change INT1 // Any change INT2 EIFR = 0x07; // Clear INT0 interrupt flag (Elevator) // Clear INT1 interrupt flag (Aileron) // Clear INT2 interrupt flag (Rudder/CPPM) //*********************************************************** // Start up //*********************************************************** // Preset important flags Interrupted = false; // Load EEPROM settings updated = Initial_EEPROM_Config_Load(); // Config now contains valid values //*********************************************************** // RX channel defaults for when no RC connected // Not doing this can result in the FC trying (unsuccessfully) to arm // and makes entry into the menus very hard //*********************************************************** for (i = 0; i < MAX_RC_CHANNELS; i++) { RxChannel[i] = 3750; } RxChannel[THROTTLE] = 2500; // Min throttle //*********************************************************** // GLCD initialisation //*********************************************************** // Initialise the GLCD st7565_init(); // Make sure the LCD is blank without clearing buffer (and so no logo) clear_screen(); //*********************************************************** // ESC calibration //*********************************************************** // Calibrate ESCs if ONLY buttons 1 and 4 pressed if ((PINB & 0xf0) == 0x60) { // Display calibrating message st7565_command(CMD_SET_COM_NORMAL); // For text (not for logo) clear_buffer(buffer); LCD_Display_Text(59,(const unsigned char*)Verdana14,10,25); write_buffer(buffer); clear_buffer(buffer); // For each output for (i = 0; i < MAX_OUTPUTS; i++) { // Check for motor marker if (Config.Channel[i].Motor_marker == MOTOR) { // Set output to maximum pulse width ServoOut[i] = MOTOR_100; } else { ServoOut[i] = SERVO_CENTER; } } // Output HIGH pulse (1.9ms) until buttons released while ((PINB & 0xf0) == 0x60) { // Pass address of ServoOut array and select all outputs output_servo_ppm_asm(&ServoOut[0], 0xFF); // Loop rate = 20ms (50Hz) _delay_ms(20); } // Output LOW pulse (1.1ms) after buttons released // For each output for (i = 0; i < MAX_OUTPUTS; i++) { // Check for motor marker if (Config.Channel[i].Motor_marker == MOTOR) { // Set output to maximum pulse width ServoOut[i] = MOTOR_0; } } // Loop forever here while(1) { // Pass address of ServoOut array and select all outputs output_servo_ppm_asm(&ServoOut[0], 0xFF); // Loop rate = 20ms (50Hz) _delay_ms(20); } } //*********************************************************** // Reset EEPROM settings //*********************************************************** // This delay prevents the GLCD flashing up a ghost image of old data _delay_ms(300); // Reload default eeprom settings if middle two buttons are pressed if ((PINB & 0xf0) == 0x90) { // Display reset message st7565_command(CMD_SET_COM_NORMAL); // For text (not for logo) clear_buffer(buffer); LCD_Display_Text(262,(const unsigned char*)Verdana14,40,25); // "Reset" write_buffer(buffer); clear_buffer(buffer); // Reset EEPROM settings Set_EEPROM_Default_Config(); Save_Config_to_EEPROM(); // Set contrast to the default value st7565_set_brightness(Config.Contrast); _delay_ms(500); // Save is now too fast to show the "Reset" text long enough } // Display message in place of logo when updating eeprom structure if (updated) { st7565_command(CMD_SET_COM_NORMAL); // For text (not for logo) clear_buffer(buffer); LCD_Display_Text(259,(const unsigned char*)Verdana14,30,13); // "Updating" LCD_Display_Text(260,(const unsigned char*)Verdana14,33,37); // "settings" write_buffer(buffer); clear_buffer(buffer); _delay_ms(1000); } else { // Write logo from buffer write_buffer(buffer); _delay_ms(1000); } clear_buffer(buffer); write_buffer(buffer); st7565_init(); // Seems necessary for KK2 mini //*********************************************************** // i2c init //*********************************************************** i2c_init(); init_i2c_gyros(); init_i2c_accs(); //*********************************************************** // Remaining init tasks //*********************************************************** // Display "Hold steady" message clear_buffer(buffer); st7565_command(CMD_SET_COM_NORMAL); // For text (not for logo) LCD_Display_Text(263,(const unsigned char*)Verdana14,18,25); // "Hold steady" write_buffer(buffer); clear_buffer(buffer); // Do startup tasks Init_ADC(); init_int(); // Initialise interrupts based on RC input mode init_uart(); // Initialise UART // Initial gyro calibration if (!CalibrateGyrosSlow()) { clear_buffer(buffer); LCD_Display_Text(61,(const unsigned char*)Verdana14,25,25); // "Cal. failed" write_buffer(buffer); _delay_ms(1000); // Reset cli(); wdt_enable(WDTO_15MS); // Watchdog on, 15ms while(1); // Wait for reboot } // Update voltage detection SystemVoltage = GetVbat(); // Check power-up battery voltage UpdateLimits(); // Update travel and trigger limits // Disarm on start-up if Armed setting is ARMABLE if (Config.ArmMode == ARMABLE) { General_error |= (1 << DISARMED); // Set disarmed bit } // Check to see that throttle is low if RC detected if (Interrupted) { RxGetChannels(); if (MonopolarThrottle > THROTTLEIDLE) // THROTTLEIDLE = 50 { General_error |= (1 << THROTTLE_HIGH); // Set throttle high error bit } } // Reset IMU reset_IMU(); // Beep that init is complete LVA = 1; _delay_ms(25); LVA = 0; #ifdef ERROR_LOG // Log reboot add_log(REBOOT); #endif } // init()
void init(void) { //*********************************************************** // I/O setup //*********************************************************** // Set port directions // KK2.0 and KK2.1 are different #ifdef KK21 DDRA = 0x30; // Port A DDRC = 0xFC; // Port C #else DDRA = 0x00; // Port A DDRC = 0xFF; // Port C #endif DDRB = 0x0A; // Port B DDRD = 0xF2; // Port D // Hold all PWM outputs low to stop glitches // M5 and M6 are on PortA for KK2.1 MOTORS = 0; M5 = 0; M6 = 0; // Preset I/O pins LED1 = 0; // LED1 off LVA = 0; // LVA alarm OFF LCD_SCL = 1; // GLCD clock high // Set/clear pull-ups (1 = set, 0 = clear) PINB = 0xF5; // Set PB pull-ups PIND = 0x0C; // Set PD pull-ups (Don't pull up RX yet) //*********************************************************** // Spektrum receiver binding //*********************************************************** _delay_ms(63); // Pause while satellite wakes up // and pull-ups have time to rise. // Tweak until bind pulses about 68ms after power-up // Bind as master if ONLY button 4 pressed if ((PINB & 0xf0) == 0xE0) { DDRD = 0xF3; // Switch PD0 to output bind_master(); } DDRD = 0xF2; // Reset Port D directions // Set/clear pull-ups (1 = set, 0 = clear) PIND = 0x0D; // Set PD pull-ups (now pull up RX as well) //*********************************************************** // Timers //*********************************************************** // Timer0 (8bit) - run @ 20MHz (50ns) - max 12.8us // Fast timer for small, precise interval timing TCCR0A = 0; // Normal operation TCCR0B = (1 << CS00); // Clk / 1 = 20MHz = 50ns TIMSK0 = 0; // No interrupts // Timer1 (16bit) - run @ 2.5MHz (400ns) - max 26.2ms // Used to measure Rx Signals & control ESC/servo output rate TCCR1A = 0; TCCR1B = (1 << CS11); // Clk/8 = 2.5MHz // Timer2 8bit - run @ 20MHz / 1024 = 19.531kHz or 51.2us - max 13.1ms // Used to time arm/disarm intervals TCCR2A = 0; TCCR2B = 0x07; // Clk/1024 = 19.531kHz TIMSK2 = 0; TIFR2 = 0; TCNT2 = 0; // Reset counter //*********************************************************** // Interrupts and pin function setup //*********************************************************** // Pin change interrupt enables PCINT1, PCINT2 and PCINT3 (Throttle, AUX and CPPM input) PCICR = 0x0A; // PCINT8 to PCINT15 (PCINT1 group - AUX) // PCINT24 to PCINT31 (PCINT3 group - THR) PCIFR = 0x0F; // Clear PCIF0 interrupt flag // Clear PCIF1 interrupt flag // Clear PCIF2 interrupt flag // Clear PCIF3 interrupt flag // External interrupts INT0 (Elevator) and INT1 (Aileron) and INT2 (Rudder) EICRA = 0x15; // Any change INT0 // Any change INT1 // Any change INT2 EIFR = 0x07; // Clear INT0 interrupt flag (Elevator) // Clear INT1 interrupt flag (Aileron) // Clear INT2 interrupt flag (Rudder/CPPM) //*********************************************************** // i2c init for KK2.1 //*********************************************************** #ifdef KK21 i2c_init(); init_i2c_gyros(); init_i2c_accs(); #endif //*********************************************************** // Start up //*********************************************************** // Preset important flags Interrupted = false; Main_flags |= (1 << FirstTimeIMU); Main_flags |= (1 << FirstTimeFlightMode); // Initialise the GLCD st7565_init(); st7565_command(CMD_DISPLAY_ON); st7565_command(CMD_SET_ALLPTS_NORMAL); st7565_set_brightness(0x26); st7565_command(CMD_SET_COM_REVERSE); // For logo // Make sure the LCD is blank clear_screen(); // This delay prevents the GLCD flashing up a ghost image of old data _delay_ms(300); // Reload default eeprom settings if middle two buttons are pressed (or all, for older users) if (((PINB & 0xf0) == 0x90) || ((PINB & 0xf0) == 0x00)) { // Display reset message st7565_command(CMD_SET_COM_NORMAL); // For text (not for logo) clear_buffer(buffer); LCD_Display_Text(1,(prog_uchar*)Verdana14,40,25); write_buffer(buffer,1); clear_buffer(buffer); Set_EEPROM_Default_Config(); Save_Config_to_EEPROM(); } // Load "Config" global data structure else { Initial_EEPROM_Config_Load(); } // Now set contrast to the previously saved value st7565_set_brightness((uint8_t)Config.Contrast); #ifdef KK21 // Write logo from buffer write_buffer(buffer,0); _delay_ms(500); #endif #ifndef KK21 // Display "Hold steady" message for KK2.0 st7565_command(CMD_SET_COM_NORMAL); // For text (not for logo) clear_buffer(buffer); LCD_Display_Text(2,(prog_uchar*)Verdana14,18,25); write_buffer(buffer,1); clear_buffer(buffer); #endif // Do startup tasks UpdateLimits(); // Update travel limts UpdateIMUvalues(); // Update IMU factors Init_ADC(); init_int(); // Intialise interrupts based on RC input mode // Initialise UART init_uart(); // Initial gyro calibration CalibrateGyrosSlow(); // Check to see that gyros are stable ReadGyros(); if ((gyroADC[ROLL] > GYROS_STABLE) || (gyroADC[ROLL] < -GYROS_STABLE) || (gyroADC[PITCH] > GYROS_STABLE) || (gyroADC[PITCH] < -GYROS_STABLE) || (gyroADC[YAW] > GYROS_STABLE) || (gyroADC[YAW] < -GYROS_STABLE)) { General_error |= (1 << SENSOR_ERROR); // Set sensor error bit } // Check to see that throttle is low if in serial mode. // Don't bother if in CamStab mode _delay_ms(100); if ( ( (Config.RxMode == CPPM_MODE) || (Config.RxMode == XTREME) || (Config.RxMode == SBUS) || (Config.RxMode == SPEKTRUM) ) && (Config.CamStab == OFF) ) { RxGetChannels(); if (RCinputs[THROTTLE] > -900) { General_error |= (1 << THROTTLE_HIGH); // Set throttle high error bit } } // Flash LED LED1 = 1; _delay_ms(150); LED1 = 0; // Beep that all sensors have been handled menu_beep(1); // Set text display mode back to normal st7565_command(CMD_SET_COM_NORMAL); // For text (not for logo) } // init()