/*---------------------------------------------------------------------------*/ void watchdog_init(void) { iwdg_set_period_ms(0xfff); iwdg_reset(); printf("watchdog_init\n"); }
/*--------------------------------------------------------------------*/ int main(void) { hardware_setup(); uint32_t i=0; for (; i<10; i++) { uint32_t d=0; for (; d<2000000; d++) __asm__("nop"); gpio_toggle(GPIOB, GPIO8); iwdg_reset(); } gpio_set(GPIOB, GPIO9); while (1) { } return 0; }
int main(void){ uint8_t *string; // string from UART2 & pointer to last full GPS answer uint8_t lastGPSans[UART_BUF_DATA_SIZE] = {0}; int i; rcc_clock_setup_in_hse_8mhz_out_72mhz(); // init systick (1ms) systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8); // Systyck: 72/8=9MHz systick_set_reload(STK_RVR_DEFAULT_VAL); // 9000 pulses: 1kHz systick_interrupt_enable(); systick_counter_enable(); GPIO_init(); /* // if PC11 connected to usb 1.5kOhm pull-up through transistor rcc_periph_clock_enable(RCC_GPIOC); gpio_set(GPIOC, GPIO11); gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO11); */ usb_disconnect(); // turn off USB while initializing all usbkeybrd_setup(); UART_init(USART2); // init GPS UART #ifdef ULTRASONIC tim2_init(); // ultrasonic timer #endif //tim4_init(); // beeper timer /* for (i = 0; i < 0x80000; i++) __asm__("nop"); */ usb_connect(); // turn on USB GPS_send_start_seq(); init_adc_sensor(); // time (in milliseconds from MCU start) for trigger, adc & power LED status; power LED blink interval // blink time: (1000ms - powerLEDblink) - LED ON // GPSstatus_tm - timer for blinking by GPS LED if there's no GPS after timer is good // powerLEDblink - LED blinking time (depends on power level) uint32_t usbkbrdtm = 0, trigrtm = 0, powerLEDtm = 0, GPSstatus_tm = 0, powerLEDblink = 1; // istriggered == 1 after ANY trigger's event (set it to 1 at start to prevent false events) // GPSLEDblink - GPS LED blinking uint8_t istriggered = 1, GPSLEDblink = 0; iwdg_set_period_ms(50); // set watchdog timeout to 50ms iwdg_start(); while(1){ if(Timer == 500) // turn off PPS LED after 500ms gpio_set(LEDS_Y_PORT, LEDS_Y2_PIN); poll_usbkeybrd(); if(usbkbrdtm != msctr){ // process USB not frequently than once per 1ms process_usbkbrd(); usbkbrdtm = msctr; } #ifdef ULTRASONIC poll_ultrasonic(); #endif poll_ADC(); if((string = check_UART2())){ memcpy(lastGPSans, string, UART_BUF_DATA_SIZE); GPS_parse_answer(string); } /* if(msctr - trigrtm > 3000){ trigrtm = msctr; for(i = 0; i < 3; ++i){ // IR or Laser P("ADC"); put_char_to_buf('0' + i); P(" val: "); print_int(ADC_value[i]); newline(); } }*/ if(istriggered){ // there was any trigger event if(msctr - trigrtm > TRIGGER_DELAY || trigrtm > msctr){ // turn off LED & beeper istriggered = 0; gpio_set(LEDS_Y_PORT, LEDS_Y1_PIN); gpio_set(BEEPER_PORT, BEEPER_PIN); trigger_ms = DIDNT_TRIGGERED; adc_ms[0] = DIDNT_TRIGGERED; adc_ms[1] = DIDNT_TRIGGERED; #ifdef ULTRASONIC ultrasonic_ms = DIDNT_TRIGGERED; #endif } }else{ if(trigger_ms != DIDNT_TRIGGERED){ // Control Button pressed trigrtm = msctr; istriggered = 1; P("Button time: "); print_time(&trigger_time, trigger_ms); if(*lastGPSans){ P("GPS last message: "); send_msg((char*)lastGPSans); newline(); } } for(i = 0; i < 2; ++i){ // IR or Laser uint32_t adcms = adc_ms[i]; if(adcms == DIDNT_TRIGGERED) continue; int32_t timediff = Timer - adcms; if(timediff < 0) timediff += 1000; // pause for noice removal if(timediff > ADC_NOICE_TIMEOUT && !istriggered){ trigrtm = msctr; istriggered = 1; if(i == 0) P("Infrared"); else P("Laser"); /* P(" trig val: "); print_int(ADC_trig_val[i]);*/ put_char_to_buf(' '); //P(" time: "); print_time(&adc_time[i], adcms); } } #ifdef ULTRASONIC if(ultrasonic_ms != DIDNT_TRIGGERED && !istriggered){ trigrtm = msctr; istriggered = 1; P("Ultrasonic time: "); print_time(&ultrasonic_time, ultrasonic_ms); } #endif if(istriggered){ // turn on Y1 LED gpio_clear(LEDS_Y_PORT, LEDS_Y1_PIN); //beep(); // turn on beeper gpio_clear(BEEPER_PORT, BEEPER_PIN); } } // check 12V power level (once per 1ms) if(powerLEDtm != msctr){ uint16_t _12V = ADC_value[2]; if(_12V < GOOD_POWER_LEVEL){ // insufficient power? - blink LED R2 // calculate blink time only if there's [was] too low level if(_12V < POWER_ALRM_LEVEL || powerLEDblink){ powerLEDblink = GOOD_POWER_LEVEL - _12V; // critical level: power LED is almost OFF if(_12V < POWER_CRITICAL_LEVEL) powerLEDblink = 990; //if(powerLEDblink > 990) powerLEDblink = 990; // shadow LED not more than 0.99s } }else{ // power restored - LED R2 shines if(powerLEDblink){ gpio_clear(LEDS_R_PORT, LEDS_R2_PIN); powerLEDblink = 0; } powerLEDtm = msctr; } if(powerLEDblink){ if(GPIO_ODR(LEDS_R_PORT) & LEDS_R2_PIN){ // LED is OFF if(msctr - powerLEDtm > powerLEDblink || msctr < powerLEDtm){ // turn LED ON powerLEDtm = msctr; gpio_clear(LEDS_R_PORT, LEDS_R2_PIN); } }else{ if(msctr - powerLEDtm > (1000 - powerLEDblink) || msctr < powerLEDtm){ // turn LED OFF powerLEDtm = msctr; gpio_set(LEDS_R_PORT, LEDS_R2_PIN); } } } } // check GPS status to turn on/off GPS LED if(current_time.H < 24){ // timer OK if((GPS_status != GPS_VALID) || need_sync){ GPSLEDblink = 1; }else{ GPSLEDblink = 0; if((GPIO_ODR(LEDS_G_PORT) & LEDS_G1_PIN) == 0) gpio_clear(LEDS_G_PORT, LEDS_G1_PIN); // turn ON G1 LED } if(GPSLEDblink){ if(msctr - GPSstatus_tm > 500 || msctr < GPSstatus_tm){ GPSstatus_tm = msctr; if(GPIO_ODR(LEDS_G_PORT) & LEDS_G1_PIN){ // LED is OFF gpio_clear(LEDS_G_PORT, LEDS_G1_PIN); }else{ gpio_set(LEDS_G_PORT, LEDS_G1_PIN); } } } }else{ // something bad with timer - turn OFF G1 LED if(!(GPIO_ODR(LEDS_G_PORT) & LEDS_G1_PIN)){ gpio_set(LEDS_G_PORT, LEDS_G1_PIN); } } iwdg_reset(); // reset watchdog } }
/** Clear (reset) the independent watchdog timer. */ void watchdog_clear(void) { iwdg_reset(); }
/** Setup and enable the independent watchdog timer. * \param period_ms Desired watchdog period in milliseconds. Note that the LSI * RC oscillator which drives the IWDG has a very loose timing spec. Actual * period may vary up to +88% -32% from this value. */ void watchdog_enable(uint32_t period_ms) { iwdg_set_period_ms(period_ms); iwdg_start(); iwdg_reset(); }
/*---------------------------------------------------------------------------*/ void watchdog_periodic(void) { iwdg_reset(); // printf("watchdog_periodic\r\n"); }
int main(void) { // Disable, configure, and start the watchdog timer iwdg_reset(); iwdg_set_period_ms(8000); iwdg_start(); // Start and configure all hardware peripherals enable_interrupts(); led_init(); //radio_init(); gps_init(); eeprom_init(); //radio_enable(); // Set the radio shift and baud rate //_radio_dac_write(RADIO_COARSE, RADIO_CENTER_FREQ); //_radio_dac_write(RADIO_FINE, 0); //radio_set_shift(RADIO_SHIFT_425); //radio_set_baud(RADIO_BAUD_50); // Radio chatter for(uint8_t i = 0; i < 5; i++) { //radio_chatter(); iwdg_reset(); } int32_t lat = 0, lon = 0, alt = 0; uint8_t hour = 0, minute = 0, second = 0, lock = 0, sats = 0; while(true) { led_set(LED_GREEN, 1); // Get the current system tick and increment uint32_t tick; eeprom_read_dword(ticks_addr, &tick); tick += 1; // Check that we're in airborne <1g mode if( gps_check_nav() != 0x06 ) led_set(LED_RED, 1); // Get information from the GPS gps_check_lock(&lock, &sats); if( lock == 0x02 || lock == 0x03 || lock == 0x04 ) { gps_get_position(&lat, &lon, &alt); gps_get_time(&hour, &minute, &second); } led_set(LED_GREEN, 0); // Format the telemetry string & transmit double lat_fmt = (double)lat / 10000000.0; double lon_fmt = (double)lon / 10000000.0; alt /= 1000; sprintf(s, "$$" CALLSIGN ",%lu,%02u:%02u:%02u,%02.7f,%03.7f,%ld,%u,%x", tick, hour, minute, second, lat_fmt, lon_fmt, alt, sats, lock); //radio_chatter(); //radio_transmit_sentence(s); //radio_chatter(); led_set(LED_RED, 0); eeprom_write_dword(ticks_addr, tick); iwdg_reset(); //_delay_ms(500); } return 0; }
int main(void) { if (DFU_AVAILABLE) { DFU_maybe_jump_to_bootloader(); } clock_setup(); tick_setup(1000); gpio_setup(); led_num(0); if (CDC_AVAILABLE) { console_setup(DEFAULT_BAUDRATE); } if (SEMIHOSTING) { initialise_monitor_handles(); } else if (VCDC_AVAILABLE) { retarget(STDOUT_FILENO, VIRTUAL_USART); retarget(STDERR_FILENO, VIRTUAL_USART); } else if (CDC_AVAILABLE) { retarget(STDOUT_FILENO, CONSOLE_USART); retarget(STDERR_FILENO, CONSOLE_USART); } led_num(1); { char serial[USB_SERIAL_NUM_LENGTH+1]; desig_get_unique_id_as_string(serial, USB_SERIAL_NUM_LENGTH+1); cmp_set_usb_serial_number(serial); } usbd_device* usbd_dev = cmp_usb_setup(); DAP_app_setup(usbd_dev, &on_dfu_request); if (CDC_AVAILABLE) { cdc_uart_app_setup(usbd_dev, &on_usb_activity, &on_usb_activity); } if (VCDC_AVAILABLE) { vcdc_app_setup(usbd_dev, &on_usb_activity, &on_usb_activity); } if (DFU_AVAILABLE) { dfu_setup(usbd_dev, &on_dfu_request); } tick_start(); /* Enable the watchdog to enable DFU recovery from bad firmware images */ iwdg_set_period_ms(1000); iwdg_start(); while (1) { iwdg_reset(); usbd_poll(usbd_dev); if (CDC_AVAILABLE) { cdc_uart_app_update(); } if (VCDC_AVAILABLE) { vcdc_app_update(); } // Handle DAP bool dap_active = DAP_app_update(); if (dap_active) { usb_timer = 1000; } else if (do_reset_to_dfu && DFU_AVAILABLE) { /* Blink 3 times to indicate reset */ int x; for (x=0; x < 3; x++) { iwdg_reset(); led_num(7); wait_ms(150); led_num(0); wait_ms(150); iwdg_reset(); } DFU_reset_and_jump_to_bootloader(); } if (usb_timer > 0) { usb_timer--; LED_ACTIVITY_OUT(1); } else { LED_ACTIVITY_OUT(0); } } return 0; }
static void handle_data(void){ #ifdef USE_STTERM unsigned char tmp_c; #endif uint32_t tmp=0; char crc=0; #ifdef USE_STTERM if(stlinky_rx_ready()){ scanf("%c", &tmp_c); INPUT[k] = tmp_c; gpio_toggle(GPIOD, GPIO15); #else if(usart_has_data()){ INPUT[k] = usart_read(); #endif k++; if(k==1 && INPUT[0]!=0x53){ // 'S' #ifdef DEBUG printf("ERROR AT START-BYTE %d\r\n",INPUT[0]); #endif k=0; } if(k==10 && INPUT[9]!=0x45){ // 'E' #ifdef DEBUG printf("ERROR AT END-BYTE %d\r\n",INPUT[8]); #endif k=0; } if(k==11){ #ifdef DEBUG printf("Package: "); for(tmp=0;tmp<10;tmp++) printf("%d ",INPUT[tmp]); printf("\n"); #endif for(tmp=0;tmp<8;tmp++) KEYS[tmp] = INPUT[tmp+1]; crc = getCRC(KEYS,8); if(crc==INPUT[10]){ #ifdef DEBUG printf("INPUT OK!\r\n"); #endif if(usb_ready == 3) usb_send_packet(KEYS, 8); }else{ #ifdef DEBUG printf("ERROR AT CRC-BYTE %d vs %d\r\n",INPUT[10],crc); #endif } k=0; } } } void tim3_isr(void){ if(timer_get_flag(TIM3, TIM_SR_UIF)){ handle_data(); timer_clear_flag(TIM3, TIM_SR_UIF); } } void tim4_isr(void){ if(timer_get_flag(TIM4, TIM_SR_UIF)){ if(usb_ready==3){ gpio_toggle(GPIOD,GPIO14); KEYS[0]=0; KEYS[1]=0; KEYS[3]=0; KEYS[4]=0; KEYS[5]=0; KEYS[6]=0; KEYS[7]=0; KEYS[2]=0x04; usb_send_packet(KEYS, 8); KEYS[2]=0x00; usb_send_packet(KEYS, 8); KEYS[2]=0x05; usb_send_packet(KEYS, 8); KEYS[2]=0x00; usb_send_packet(KEYS, 8); } timer_clear_flag(TIM4, TIM_SR_UIF); } } void tim5_isr(void){ if(timer_get_flag(TIM5, TIM_SR_UIF)){ gpio_toggle(GPIOD, GPIO15); timer_clear_flag(TIM5, TIM_SR_UIF); } } int main(void) { iwdg_reset(); iwdg_set_period_ms(5); iwdg_start(); rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]); rcc_periph_clock_enable(RCC_GPIOD); systick_setup(); gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12 | GPIO13 | GPIO14 | GPIO15); gpio_clear(GPIOD, GPIO12 | GPIO13 | GPIO14 | GPIO15); iwdg_set_period_ms(500); iwdg_reset(); msleep(400); iwdg_set_period_ms(5); iwdg_reset(); gpio_set(GPIOD, GPIO15); // msleep(100000); /* SLEEEEEEEEEEEEEEEEEEEEEEP */ #ifdef USE_STTERM stlinky_init(); #else usart_setup(); #endif timer2_setup(100); usb_setup(); //timer4_setup(1); timer3_setup(100); timer5_setup(2); // gpio_set(GPIOD, GPIO12 | GPIO13 | GPIO14 | GPIO15); while(1) { iwdg_reset(); } return 0; }