コード例 #1
0
ファイル: main.c プロジェクト: alkyl1978/stm32samples
int main(){
	uint32_t Old_timer = 0;

	// RCC clocking: 8MHz oscillator -> 72MHz system
	rcc_clock_setup_in_hse_8mhz_out_72mhz();

	GPIO_init();

	usb_disconnect(); // turn off USB while initializing all
	steppers_init();

	// USB
	usbd_dev = USB_init();

	// SysTick is a system timer with 1ms period
	SysTick_init();

	// wait a little and then turn on USB pullup
//	for (i = 0; i < 0x800000; i++)
//		__asm__("nop");

	usb_connect(); // turn on USB

	while(1){
		usbd_poll(usbd_dev);
		if(usbdatalen){ // there's something in USB buffer
			usbdatalen = parce_incoming_buf(usbdatabuf, usbdatalen);
		}
		//check_and_parce_UART(USART1); // also check data in UART buffers
		if(Timer - Old_timer > 999){ // one-second cycle
			Old_timer += 1000;
		}else if(Timer < Old_timer){ // Timer overflow
			Old_timer = 0;
		}
	}
}
コード例 #2
0
ファイル: main.c プロジェクト: eddyem/IR-controller
int main(){
	//int i;
	uint32_t Shtr_blink_timer = 0, Old_timer = 0, lastTRDread = 0, lastTmon = 0, OW_timer = 0;
	int oldusbdatalen = 0;
	//SPI_read_status SPI_stat;

	// RCC clocking: 8MHz oscillator -> 72MHz system
	rcc_clock_setup_in_hse_8mhz_out_72mhz();

	// turn off SWJ/JTAG
	AFIO_MAPR = AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_OFF;

	// GPIO
	GPIO_init();
	usb_disconnect(); // turn off USB while initializing all

	// init USART3 (master) & USART1 (slave)
	UART_init(USART3);
	UART_init(USART1);

	// USB
	usbd_dev = USB_init();

	// SysTick is a system timer with 1mc period
	SysTick_init();

	// instead of SPI1 we use those pins to control shutter and system state
	// SPI2 used for working with external ADC
	switch_SPI(SPI2); // init SPI2
	SPI_init();

	// wait a little and then turn on USB pullup
//	for (i = 0; i < 0x800000; i++)
//		__asm__("nop");

	// init ADC
	ADC_init();
	ADC_calibrate_and_start();

	steppers_init();

	usb_connect(); // turn on USB
	shutter_init();

	read_stored_data(); // copy stored data into RAM

	init_ow_dmatimer();
	//OW_send_read_seq();

	LED_STATUS_OK(); // All initialized - light up LED
	while(1){
		init_on_poweron_proc();
		usbd_poll(usbd_dev);
		if(oldusbdatalen != usbdatalen){ // there's something in USB buffer
			usbdatalen = parce_incoming_buf(usbdatabuf, usbdatalen, usb_send);
			oldusbdatalen = usbdatalen;
		}
		check_and_parce_UART(USART3); // check data in master UART buffers
		check_and_parce_UART(USART1); // also check data in slave UART buffers
		if(ad7794_on){
			if(Timer != lastTRDread){ // run this not more than once in 1ms
				lastTRDread = Timer;
				read_next_TRD();
			}
		}
		OW_process(); // process 1-wire commands
		// scan 1-wire each 1 second
		if(OW_scan && (Timer - OW_timer > 999 || Timer < OW_timer)){
			OW_timer = Timer;
			scan_onewire();
		}
		process_stepper_motors(); // check flags of motors' timers
		process_shutter(); // shutter state machine

		if(Timer - Shtr_blink_timer > 500 ||  Timer < Shtr_blink_timer){
			Shtr_blink_timer = Timer;
			// shutter LED will be blinking until init occurs
			if(Shutter_State == SHUTTER_NOTREADY)
				gpio_toggle(LED_SHUTTER_PORT, LED_SHUTTER_PIN);
		}

		if(Timer - Old_timer > 999){ // one-second cycle
			Old_timer += 1000;
			// init shutter if error occurs
			if(Shutter_State == SHUTTER_NOTREADY){
				shutter_init();
			}
		}else if(Timer < Old_timer){ // Timer overflow
			Old_timer = 0;
			tOVRFL++; // this is an overflow counter - for workinkg in long-long time interval
		}

		if((Timer - lastTmon > 9999) || (Timer < lastTmon)){ // run constant monitoring of ADC values each 10 seconds
			lastTmon += 10000;
			if(ADC_monitoring){
				print_time(lastsendfun);
				print_int_ad_vals(lastsendfun);
				print_ad_vals(lastsendfun);
			}
		}
	}
}
コード例 #3
0
ファイル: main.c プロジェクト: chaos-inkl/firecake-pump
int main(void) {
    uart_init();
    servos_init();
    steppers_init();
    pumps_init();

    *idle_ddr &= ~idle_mask;
    *idle_port |= idle_mask;

    *setup_ddr &= ~setup_mask;
    *setup_port |= setup_mask;

    *ready_ddr |= ready_mask;
    *ready_port &= ~(ready_mask);


    sei();

    uart_puts("PumpControl 0.1 ready\n\r");


    uint8_t active_pump = 0;

    while(1) {
        pumps_run();

        // Setup is active low
        if(!(*setup_pin & setup_mask)) {
            for(uint8_t i = 0; i < PumpCount; i++) {
                if(pump_states[i] != PUMP_SETUP) {
                     pump_enter_setup(i);
                }
            }
        }

        // We only activate extruders if we are idle
        if(*idle_pin & idle_mask) {
            /*
             * Check if we need a new active pump
             * if full -> nice we've got a fresh one
             * if dispense -> still some stuff left in this one
             */
            if(pump_states[active_pump] != PUMP_FULL &&
                pump_states[active_pump] != PUMP_DISPENSE) {
                for(uint8_t i = 0; i < PumpCount; i++) {
                    if(pump_states[i] == PUMP_FULL) {
                        active_pump = i;
                        break;
                    }
                }
            }

            // We've selected a new active pump, now we need to start it
            if(pump_states[active_pump] == PUMP_FULL) {
                uart_debug_pump(active_pump, "is new acitve pump");
                pump_enter_dispense(active_pump);
            }
        }

        /*
         * We are ready if we picked a new active pump and set it to dispense,
         * or if the old one still has stuff left in it and is in dispense.
         */
        if(pump_states[active_pump] == PUMP_DISPENSE) {
            *ready_port |= ready_mask;
        }
        else {
            *ready_port &= ~(ready_mask);
        }


        /*
         * If we are idle, try to fill all pumps
         * If the active pump is also refilled, a other full one will be chosen
         * in the next iteration.
         */
        if(!(*idle_pin & idle_mask)) {
            for(uint8_t i = 0; i < PumpCount; i++) {
                if(pump_states[i] == PUMP_DISPENSE) {
                    uart_debug("Idle state detected, refilling");
                    pump_enter_fill(i);
                }
            }
        }
    }

}