void matchuart_init(uint8_t num, const char *pat, void (*cb)(void)) { if( match_num != UINT8_MAX ) { // unregister previous event uart_register_rx_event(match_num, NULL); } match_cb = cb; match_pat = pat; match_cur = pat; match_num = num; uart_register_rx_event(num, uart_event); }
int main(void) { struct callout c1, c2, c3, c4; uint8_t old_prio; #ifdef HOST_VERSION hostsim_uart_init(); hostsim_ittimer_add(timer_interrupt, 1 * 1000 * 1000); /* 1ms period */ hostsim_ittimer_enable(100); /* 100 us */ #else uart_init(); fdevopen(uart0_dev_send, uart0_dev_recv); timer_init(); timer0_register_OV_intr(timer_interrupt); #endif uart_register_rx_event(0, dump_stats); callout_mgr_init(&global_cm, get_time_ms); sei(); printf("f1 every %d ms, high prio (200)\n", C1_TIME_MS); printf("f2 every %d ms, low prio (50)\n", C2_TIME_MS); printf("f3 every %d ms, med prio (100), the function lasts 600ms\n", C3_TIME_MS); printf("f4 only once after %d ms, very high prio (250), " "stop task f2\n", C4_TIME_MS); printf("type s to dump stats\n"); callout_init(&c1, f1, NULL, 200); callout_init(&c2, f2, NULL, 50); callout_init(&c3, f3, NULL, 100); callout_init(&c4, supp, &c2, 250); callout_schedule(&global_cm, &c1, C1_TIME_MS); callout_schedule(&global_cm, &c2, C2_TIME_MS); callout_schedule(&global_cm, &c3, C3_TIME_MS); callout_schedule(&global_cm, &c4, C4_TIME_MS); while (get_time_ms() < 2900) ; old_prio = callout_mgr_set_prio(&global_cm, 150); ts_printf("set prio 150\n"); while (get_time_ms() < 3100) ; ts_printf("set prio 0\n"); callout_mgr_restore_prio(&global_cm, old_prio); while (get_time_ms() < 5000) ; callout_stop(&global_cm, &c1); callout_stop(&global_cm, &c3); callout_stop(&global_cm, &c4); callout_dump_stats(&global_cm); wait_ms(10); #ifdef HOST_VERSION hostsim_uart_exit(); #endif return 0; }
int main(void) { #ifndef HOST_VERSION /* brake */ BRAKE_DDR(); BRAKE_OFF(); /* CPLD reset on PG3 */ DDRG |= 1<<3; PORTG &= ~(1<<3); /* implicit */ /* LEDS */ DDRJ |= 0x0c; DDRL = 0xc0; LED1_OFF(); LED2_OFF(); LED3_OFF(); LED4_OFF(); #endif memset(&gen, 0, sizeof(gen)); memset(&mainboard, 0, sizeof(mainboard)); mainboard.flags = DO_ENCODERS | DO_CS | DO_RS | DO_POS | DO_POWER | DO_BD | DO_ERRBLOCKING; ballboard.lcob = I2C_COB_NONE; ballboard.rcob = I2C_COB_NONE; /* UART */ uart_init(); uart_register_rx_event(CMDLINE_UART, emergency); #ifndef HOST_VERSION #if CMDLINE_UART == 3 fdevopen(uart3_dev_send, uart3_dev_recv); #elif CMDLINE_UART == 1 fdevopen(uart1_dev_send, uart1_dev_recv); #endif /* check eeprom to avoid to run the bad program */ if (eeprom_read_byte(EEPROM_MAGIC_ADDRESS) != EEPROM_MAGIC_MAINBOARD) { int c; sei(); printf_P(PSTR("Bad eeprom value ('f' to force)\r\n")); c = uart_recv(CMDLINE_UART); if (c == 'f') eeprom_write_byte(EEPROM_MAGIC_ADDRESS, EEPROM_MAGIC_MAINBOARD); wait_ms(100); bootloader(); } #endif /* ! HOST_VERSION */ /* LOGS */ error_register_emerg(mylog); error_register_error(mylog); error_register_warning(mylog); error_register_notice(mylog); error_register_debug(mylog); #ifndef HOST_VERSION /* SPI + ENCODERS */ encoders_spi_init(); /* this will also init spi hardware */ /* I2C */ i2c_init(I2C_MODE_MASTER, I2C_MAINBOARD_ADDR); i2c_protocol_init(); i2c_register_recv_event(i2c_recvevent); i2c_register_send_event(i2c_sendevent); /* TIMER */ timer_init(); timer0_register_OV_intr(main_timer_interrupt); /* PWM */ PWM_NG_TIMER_16BITS_INIT(1, TIMER_16_MODE_PWM_10, TIMER1_PRESCALER_DIV_1); PWM_NG_TIMER_16BITS_INIT(4, TIMER_16_MODE_PWM_10, TIMER4_PRESCALER_DIV_1); PWM_NG_INIT16(&gen.pwm1_4A, 4, A, 10, PWM_NG_MODE_SIGNED, &PORTD, 4); PWM_NG_INIT16(&gen.pwm2_4B, 4, B, 10, PWM_NG_MODE_SIGNED | PWM_NG_MODE_SIGN_INVERTED, &PORTD, 5); PWM_NG_INIT16(&gen.pwm3_1A, 1, A, 10, PWM_NG_MODE_SIGNED, &PORTD, 6); PWM_NG_INIT16(&gen.pwm4_1B, 1, B, 10, PWM_NG_MODE_SIGNED, &PORTD, 7); /* servos */ PWM_NG_TIMER_16BITS_INIT(3, TIMER_16_MODE_PWM_10, TIMER1_PRESCALER_DIV_256); PWM_NG_INIT16(&gen.servo1, 3, C, 10, PWM_NG_MODE_NORMAL, NULL, 0); PWM_NG_TIMER_16BITS_INIT(5, TIMER_16_MODE_PWM_10, TIMER1_PRESCALER_DIV_256); PWM_NG_INIT16(&gen.servo2, 5, A, 10, PWM_NG_MODE_NORMAL, NULL, 0); PWM_NG_INIT16(&gen.servo3, 5, B, 10, PWM_NG_MODE_NORMAL, NULL, 0); PWM_NG_INIT16(&gen.servo4, 5, C, 10, PWM_NG_MODE_NORMAL, NULL, 0); support_balls_deploy(); /* init pwm for servos */ #endif /* !HOST_VERSION */ /* SCHEDULER */ scheduler_init(); #ifdef HOST_VERSION hostsim_init(); robotsim_init(); #endif #ifndef HOST_VERSION scheduler_add_periodical_event_priority(do_led_blink, NULL, 100000L / SCHEDULER_UNIT, LED_PRIO); #endif /* !HOST_VERSION */ /* all cs management */ microb_cs_init(); /* TIME */ time_init(TIME_PRIO); /* sensors, will also init hardware adc */ sensor_init(); #ifndef HOST_VERSION /* start i2c slave polling */ scheduler_add_periodical_event_priority(i2c_poll_slaves, NULL, 8000L / SCHEDULER_UNIT, I2C_POLL_PRIO); #endif /* !HOST_VERSION */ /* strat */ gen.logs[0] = E_USER_STRAT; gen.log_level = 5; /* strat-related event */ scheduler_add_periodical_event_priority(strat_event, NULL, 25000L / SCHEDULER_UNIT, STRAT_PRIO); #ifndef HOST_VERSION /* eeprom time monitor */ scheduler_add_periodical_event_priority(do_time_monitor, NULL, 1000000L / SCHEDULER_UNIT, EEPROM_TIME_PRIO); #endif /* !HOST_VERSION */ sei(); strat_db_init(); printf_P(PSTR("\r\n")); printf_P(PSTR("Respect et robustesse.\r\n")); #ifndef HOST_VERSION { uint16_t seconds; seconds = eeprom_read_word(EEPROM_TIME_ADDRESS); printf_P(PSTR("Running since %d mn %d\r\n"), seconds/60, seconds%60); } #endif #ifdef HOST_VERSION strat_reset_pos(400, COLOR_Y(400), COLOR_A(-90)); #endif cmdline_interact(); return 0; }