Beispiel #1
0
int main(void)
{
	/* Low level initialization. */
	extern uint32_t vector_table __asm__("vector_table"); /* Defined by the linker */
	SCB_VTOR = (uint32_t) &vector_table;
	rcc_clock_setup_in_hse_8mhz_out_72mhz();

	/* Enable clock for Port B used by the LED and USB pull-up transistor */
	rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
	rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);

	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO8);
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO9);
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO10);

	gpio_set(DBGO, DBG_R);
	gpio_clear(DBGO, DBG_G);
	gpio_set(DBGO, DBG_B);

	/* Setup GPIOB Pin 1 for the LED */
	gpio_set(GPIOB, GPIO1);
	gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO1);

	rsm500_usb_init();

	systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB);
	systick_set_reload(8999999);
	systick_interrupt_enable();
	systick_counter_enable();

	cprintf("RSM-500 spectrometer ready\n");
	while (1) {
		console_poll();
	}

	return 0;
}
Beispiel #2
0
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;
		}
	}
}
Beispiel #3
0
/**
 * Setup the system clock to 72MHz.
 */
void clock_init(void)
{
     rcc_clock_setup_in_hse_8mhz_out_72mhz();
}
Beispiel #4
0
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);
			}
		}
	}
}
Beispiel #5
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
	}
}
static void clock_setup(void)
{
	rcc_clock_setup_in_hse_8mhz_out_72mhz();
}
Beispiel #7
0
int main(void) {
	//rcc_clock_setup_in_hsi_out_48mhz();
	rcc_clock_setup_in_hse_8mhz_out_72mhz();
    rcc_periph_clock_enable(RCC_GPIOB);
    gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO10);
    do_blink();

    GPIO_BRR(GPIOB) = GPIO10;

	struct UjClass* mainClass = NULL;
	struct UjClass* objectClass;
	UInt32 threadH;

	UInt8 ret = ujInit(&objectClass);
	if(ret != UJ_ERR_NONE){
	    GPIO_BSRR(GPIOB) = GPIO10;
	    while (true) {}
	}

	ret = ujRegisterNativeClass(&nativeCls_UC, objectClass, NULL);
	if(ret != UJ_ERR_NONE){
		GPIO_BSRR(GPIOB) = GPIO10;
		while (true) {}
	}

	ret = ujLoadClass(0, &mainClass);
	if(ret != UJ_ERR_NONE){
		GPIO_BSRR(GPIOB) = GPIO10;
		while (true) {}
	}

	ret = ujInitAllClasses();
	if(ret != UJ_ERR_NONE){
		GPIO_BSRR(GPIOB) = GPIO10;
		while (true) {}
	}

	//now classes are loaded, time to call the entry point

	threadH = ujThreadCreate(0);
	if(!threadH) {
		GPIO_BSRR(GPIOB) = GPIO10;
		while (true) {}
	}

	UInt8 h = ujThreadGoto(threadH, mainClass, "main", "()V");
	if(h == UJ_ERR_METHOD_NONEXISTENT) {
		GPIO_BSRR(GPIOB) = GPIO10;
		while (true) {}
	}

	while(ujCanRun()) {
		h = ujInstr();
		if(h != UJ_ERR_NONE) {
			GPIO_BSRR(GPIOB) = GPIO10;
			while (true) {}
		}
	}


	GPIO_BSRR(GPIOB) = GPIO10;
	while (true) {}
}
Beispiel #8
0
int platform_init(void)
{
	uint32_t data;
	rcc_clock_setup_in_hse_8mhz_out_72mhz();

	/* Enable peripherals */
	rcc_periph_clock_enable(RCC_USB);
	rcc_periph_clock_enable(RCC_GPIOA);
	rcc_periph_clock_enable(RCC_GPIOB);
	rcc_periph_clock_enable(RCC_AFIO);
	rcc_periph_clock_enable(RCC_CRC);

	/* Unmap JTAG Pins so we can reuse as GPIO */
        data = AFIO_MAPR;
        data &= ~AFIO_MAPR_SWJ_MASK;
        data |= AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_OFF;
        AFIO_MAPR = data;
	/* Setup JTAG GPIO ports */
	gpio_set_mode(TMS_PORT, GPIO_MODE_OUTPUT_10_MHZ,
			GPIO_CNF_OUTPUT_PUSHPULL, TMS_PIN);
	gpio_set_mode(TCK_PORT, GPIO_MODE_OUTPUT_10_MHZ,
			GPIO_CNF_OUTPUT_PUSHPULL, TCK_PIN);
	gpio_set_mode(TDI_PORT, GPIO_MODE_OUTPUT_10_MHZ,
			GPIO_CNF_OUTPUT_PUSHPULL, TDI_PIN);

	gpio_set_mode(TDO_PORT, GPIO_MODE_INPUT,
			GPIO_CNF_INPUT_FLOAT, TDO_PIN);

        gpio_set(NRST_PORT,NRST_PIN);
	gpio_set_mode(NRST_PORT, GPIO_MODE_INPUT,
			GPIO_CNF_INPUT_PULL_UPDOWN, NRST_PIN);

	gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ,
			GPIO_CNF_OUTPUT_PUSHPULL, led_idle_run);

        /* Remap TIM2 TIM2_REMAP[1]
         * TIM2_CH1_ETR -> PA15 (TDI, set as output above)
         * TIM2_CH2     -> PB3  (TDO)
         */
        data = AFIO_MAPR;
        data &= ~AFIO_MAPR_TIM2_REMAP_FULL_REMAP;
        data |=  AFIO_MAPR_TIM2_REMAP_PARTIAL_REMAP1;
        AFIO_MAPR = data;

	/* Setup heartbeat timer */
	systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8);
	systick_set_reload(900000);	/* Interrupt us at 10 Hz */
	SCB_SHPR(11) &= ~((15 << 4) & 0xff);
	SCB_SHPR(11) |= ((14 << 4) & 0xff);
	systick_interrupt_enable();
	systick_counter_enable();

	usbuart_init();

	SCB_VTOR = 0x2000;	// Relocate interrupt vector table here

	cdcacm_init();

	// Set recovery point
	if (setjmp(fatal_error_jmpbuf)) {
		return 0; // Do nothing on failure
	}

	jtag_scan(NULL);

	return 0;
}
/*--------------------------------------------------------------------*/
void clock_setup(void)
{
/* Setup the clock to 72MHz from the 8MHz external crystal */
	rcc_clock_setup_in_hse_8mhz_out_72mhz();
}
Beispiel #10
0
void
Board_FY20AP::setup(void)
{

	/* configure for 8MHz crystal on the FY20AP board */
	rcc_clock_setup_in_hse_8mhz_out_72mhz();

	/*
	 * Pinouts:
	 *
	 * 			OUT			IN
	 * 	NULL	NULL	RUD	ELE	AIL	SWITCH	RUD	ELE	AIL
	 * 	------------------------------------------------------------------------
	 *   A	n/c	PA10	PB6	PB7	PB9	PA0	PA1	PA3	PA4
	 *   B	Boot0	PA9	Vcc	Vcc	Vcc	Vcc	Vcc	Vcc	Vcc
	 *   C	PC8	GND	GND	GND	GND	GND	GND	GND	GND
	 *
	 * Useful pin options
	 * ------------------
	 *
	 * PA0 - TIM2_CH1, ADC12_IN0
	 * PA1 - TIM2_CH2, ADC12_IN1
	 * PA3 - TIM2_CH4, ADC12_IN3, USART2_RX
	 * PA4 - ADC12_IN4
	 *
	 * PA9  - USART1_TX, TIM1_CH2
	 * PA10 - USART1_RX, TIM1_CH3
	 *
	 * PB6 - TIM4_CH1, I2C1_SCL, USART1_TX
	 * PB7 - TIM4_CH2, I2C1_SDA, USART1_RX
	 * PB9 - TIM4_CH4
	 *
	 * PC8 - TIM3_CH3
	 *
	 * PA15 - blue LED
	 * PC12 - red LED
	 */

	/* turn on required clocks */
	rcc_peripheral_enable_clock(&RCC_APB1ENR,
				    RCC_APB1ENR_I2C1EN);
	rcc_peripheral_enable_clock(&RCC_APB2ENR,
				    RCC_APB2ENR_IOPAEN |
				    RCC_APB2ENR_IOPBEN |
				    RCC_APB2ENR_IOPCEN |
				    RCC_APB2ENR_AFIOEN |
				    RCC_APB2ENR_USART1EN);

	/* configure LED GPIOs */
	AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON;
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO15);
	gpio_set(GPIOA, GPIO15);
	gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO12);
	gpio_set(GPIOC, GPIO12);

	/* configure misc GPIOs as pulled-up inputs */
	gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO0 | GPIO1 | GPIO3 | GPIO4);
	gpio_set(GPIOA, GPIO0 | GPIO1 | GPIO3 | GPIO4);
	gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO9);
	gpio_set(GPIOB, GPIO9);
	gpio_set_mode(GPIOC, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO8);
	gpio_set(GPIOC, GPIO8);

	/* configure USART GPIOs */
	gpio_set_mode(GPIO_BANK_USART1_TX, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
	gpio_set_mode(GPIO_BANK_USART1_RX, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX);

	/* configure I2C GPIOs */
	gpio_set_mode(GPIO_BANK_I2C1_SCL,  GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO_I2C1_SCL);
	gpio_set_mode(GPIO_BANK_I2C1_SDA,  GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO_I2C1_SDA);

}
Beispiel #11
0
int main(void) {
  uint32_t i;

  rcc_periph_clock_enable(BOARD_RCC_LED);
  LED_ENABLE();
  LED_BUSY();

/* Setup clock accordingly */
#ifdef GD32F103
  rcc_clock_setup_in_hse_12mhz_out_120mhz();
#else
#ifdef STM32F0
  rcc_clock_setup_in_hsi48_out_48mhz();
  rcc_periph_clock_enable(RCC_SYSCFG_COMP);
  SYSCFG_CFGR1 |= SYSCFG_CFGR1_PA11_PA12_RMP;
  rcc_periph_clock_enable(RCC_CRS);
  crs_autotrim_usb_enable();
  rcc_set_usbclk_source(RCC_HSI48);
#else
  rcc_clock_setup_in_hse_8mhz_out_72mhz();
#endif /* STM32F0 */
#endif /* GD32F103 */

  rcc_periph_clock_enable(RCC_GPIOA); /* For USB */

/* STM32F0x2 has internal pullup and does not need AFIO */
#ifndef STM32F0
  rcc_periph_clock_enable(BOARD_RCC_USB_PULLUP);
  rcc_periph_clock_enable(RCC_AFIO); /* For SPI */
#endif /* STM32F0 */

#if BOARD_USE_DEBUG_PINS_AS_GPIO
  gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_OFF, AFIO_MAPR_TIM2_REMAP_FULL_REMAP);
#endif

/* Setup GPIO to pull up the D+ high. (STM32F0x2 has internal pullup.) */
#ifndef STM32F0
  gpio_set_mode(BOARD_PORT_USB_PULLUP, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, BOARD_PIN_USB_PULLUP);
#if BOARD_USB_HIGH_IS_PULLUP
  gpio_set(BOARD_PORT_USB_PULLUP, BOARD_PIN_USB_PULLUP);
#else
  gpio_clear(BOARD_PORT_USB_PULLUP, BOARD_PIN_USB_PULLUP);
#endif /* BOARD_USB_HIGH_IS_PULLUP */
#endif /* STM32F0 */

  usbcdc_init();
  spi_setup(SPI_DEFAULT_CLOCK);

  /* The loop. */
  while (true) {
    /* Wait and blink if USB is not ready. */
    LED_IDLE();
    while (!usb_ready) {
      LED_DISABLE();
      for (i = 0; i < rcc_ahb_frequency / 150; i ++) {
        asm("nop");
      }
      LED_ENABLE();
      for (i = 0; i < rcc_ahb_frequency / 150; i ++) {
        asm("nop");
      }
    }

    /* Actual thing */
    /* TODO: we are blocked here, hence no knowledge about USB bet reset. */
    handle_command(usbcdc_getc());
  }

  return 0;
}
/*--------------------------------------------------------------------*/
void hardware_setup(void)
{
/* Setup the clock to 72MHz from the 8MHz external crystal */

	rcc_clock_setup_in_hse_8mhz_out_72mhz();

/* Enable GPIOA, GPIOB and GPIOC clocks.
   APB2 (High Speed Advanced Peripheral Bus) peripheral clock enable register (RCC_APB2ENR)
   Set RCC_APB2ENR_IOPBEN for port B, RCC_APB2ENR_IOPAEN for port A and RCC_APB2ENR_IOPAEN
   for Alternate Function clock */
	rcc_periph_clock_enable(RCC_GPIOA);
	rcc_periph_clock_enable(RCC_GPIOB);
	rcc_periph_clock_enable(RCC_GPIOC);
	rcc_periph_clock_enable(RCC_AFIO);

/* Digital Test output PC0 */
	gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
		      GPIO_CNF_OUTPUT_PUSHPULL, GPIO0);

/* ----------------- Timer 2 Interrupt and DAC control*/

/* Enable TIM2 clock. */
	rcc_periph_clock_enable(RCC_TIM2);
/* Enable TIM2 interrupt. */
	nvic_enable_irq(NVIC_TIM2_IRQ);
	timer_reset(TIM2);
/* Timer global mode:
 * - No divider
 * - Alignment edge
 * - Direction up
 */
	timer_set_mode(TIM2, TIM_CR1_CKD_CK_INT,
		       TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
/* Continous mode. */
	timer_continuous_mode(TIM2);
	timer_set_period(TIM2, 1000);
/* Disable outputs. */
	timer_disable_oc_output(TIM2, TIM_OC1 | TIM_OC2 | TIM_OC3 | TIM_OC4);
/* Configure global mode of output channel 1, disabling the output. */
	timer_disable_oc_clear(TIM2, TIM_OC1);
	timer_disable_oc_preload(TIM2, TIM_OC1);
	timer_set_oc_slow_mode(TIM2, TIM_OC1);
	timer_set_oc_mode(TIM2, TIM_OC1, TIM_OCM_FROZEN);
/* Set the capture compare value for OC1. */
	timer_set_oc_value(TIM2, TIM_OC1, 1000);
/* ARR reload disable. */
	timer_disable_preload(TIM2);
/* Counter enable. */
	timer_enable_counter(TIM2);
/* Enable commutation interrupt. */
	timer_enable_irq(TIM2, TIM_DIER_CC1IE);

/* Set port PA4 for DAC1 to 'alternate function'. Output driver mode is ignored. */
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
		      GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO4);

/* Enable the DAC clock on APB1 */
	rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_DACEN);

/* Setup the DAC, software trigger source. Assume the DAC has
woken up by the time the first interrupt occurs */
	dac_trigger_enable(CHANNEL_D);
	dac_set_trigger_source(DAC_CR_TSEL1_SW | DAC_CR_TSEL2_SW);
	dac_enable(CHANNEL_D);
	dac_load_data_buffer_dual(0, 0, RIGHT8);

}
Beispiel #13
0
void hardware_setup(void)
{
/* Set the clock to 72MHz from the 8MHz external crystal */

	rcc_clock_setup_in_hse_8mhz_out_72mhz();

/* Enable GPIOA, GPIOB and GPIOC clocks.
   APB2 (High Speed Advanced Peripheral Bus) peripheral clock enable register (RCC_APB2ENR)
   Set RCC_APB2ENR_IOPBEN for port B, RCC_APB2ENR_IOPAEN for port A and RCC_APB2ENR_IOPAEN
   for Alternate Function clock */
    rcc_periph_clock_enable(RCC_GPIOA);
	rcc_periph_clock_enable(RCC_GPIOB);
	rcc_periph_clock_enable(RCC_GPIOC);
	rcc_periph_clock_enable(RCC_AFIO);

/* Set ports PA8 (TIM1_CH1), PA9 (TIM1_CH2), PB13 (TIM1_CH1N), PB14 (TIM1_CH2N)
for PWM, to 'alternate function output push-pull'. */
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
		      GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO8 | GPIO9);
	gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
		      GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO13 | GPIO14);

/* ------------------ Timer 1 PWM */
/* Enable TIM1 clock. */
	rcc_periph_clock_enable(RCC_TIM1);

/* Reset TIM1 peripheral. */
	timer_reset(TIM1);

/* Set Timer global mode:
 * - No division
 * - Alignment centre mode 1 (up/down counting, interrupt on downcount only)
 * - Direction up (when centre mode is set it is read only, changes by hardware)
 */
	timer_set_mode(TIM1, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_CENTER_1, TIM_CR1_DIR_UP);

/* Set Timer output compare mode:
 * - Channel 1
 * - PWM mode 2 (output low when CNT < CCR1, high otherwise)
 */
	timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_PWM2);
	timer_enable_oc_output(TIM1, TIM_OC1);
	timer_enable_oc_output(TIM1, TIM_OC1N);
	timer_set_oc_mode(TIM1, TIM_OC2, TIM_OCM_PWM2);
	timer_enable_oc_output(TIM1, TIM_OC2);
	timer_enable_oc_output(TIM1, TIM_OC2N);
	timer_enable_break_main_output(TIM1);
/* Set the polarity of OCN to be high to match that of the OC, for switching
the low MOSFET through an inverting level shifter */
    timer_set_oc_polarity_high(TIM1, TIM_OC2N);

/* The ARR (auto-preload register) sets the PWM period to 62.5kHz from the
72 MHz clock.*/
	timer_enable_preload(TIM1);
	timer_set_period(TIM1, PERIOD);

/* The CCR1 (capture/compare register 1) sets the PWM duty cycle to default 50% */
	timer_enable_oc_preload(TIM1, TIM_OC1);
	timer_set_oc_value(TIM1, TIM_OC1, (PERIOD*20)/100);
	timer_enable_oc_preload(TIM1, TIM_OC2);
	timer_set_oc_value(TIM1, TIM_OC2, (PERIOD*50)/100);

/* Force an update to load the shadow registers */
	timer_generate_event(TIM1, TIM_EGR_UG);

/* Start the Counter. */
	timer_enable_counter(TIM1);
}