static void gpio_setup(void) { /* Set GPIO12-15 (in GPIO port D) to 'output push-pull'. */ gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12 | GPIO13 | GPIO14 | GPIO15); /* Set */ gpio_mode_setup(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO0); gpio_clear(GPIOC, GPIO0); // OTG_FS gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11 | GPIO12); gpio_set_af(GPIOA, GPIO_AF10, GPIO11 | GPIO12); // OTG_HS gpio_mode_setup(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO15 | GPIO14); gpio_set_af(GPIOB, GPIO_AF12, GPIO14 | GPIO15); // USART TX gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO6 | GPIO7); gpio_set_af(GPIOC, GPIO_AF8, GPIO6 | GPIO7); // button gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO0); }
static void stmpe811_i2c_init() { rcc_periph_clock_enable(RCC_GPIOA); rcc_periph_clock_enable(RCC_GPIOC); rcc_periph_clock_enable(RCC_I2C3); gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO8); gpio_set_output_options(GPIOA, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO8); gpio_set_af(GPIOA, GPIO_AF4, GPIO8); gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9); gpio_set_output_options(GPIOC, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO9); gpio_set_af(GPIOC, GPIO_AF4, GPIO9); i2c_peripheral_disable(I2C3); /* disable i2c during setup */ i2c_reset(I2C3); i2c_set_fast_mode(I2C3); i2c_set_clock_frequency(I2C3, I2C_CR2_FREQ_42MHZ); i2c_set_ccr(I2C3, 35); i2c_set_trise(I2C3, 43); i2c_peripheral_enable(I2C3); /* finally enable i2c */ i2c_set_own_7bit_slave_address(I2C3, 0x00); }
static void usart_setup(void) { /* Enable the USART2 interrupt. */ nvic_enable_irq(NVIC_USART2_IRQ); /* Setup GPIO pins for USART2 transmit. */ gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2); /* Setup GPIO pins for USART2 receive. */ gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO3); gpio_set_output_options(GPIOA, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, GPIO3); /* Setup USART2 TX and RX pin as alternate function. */ gpio_set_af(GPIOA, GPIO_AF7, GPIO2); gpio_set_af(GPIOA, GPIO_AF7, GPIO3); /* Setup USART2 parameters. */ usart_set_baudrate(USART2, 38400); usart_set_databits(USART2, 8); usart_set_stopbits(USART2, USART_STOPBITS_1); usart_set_mode(USART2, USART_MODE_TX_RX); usart_set_parity(USART2, USART_PARITY_NONE); usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); /* Enable USART2 Receive interrupt. */ usart_enable_rx_interrupt(USART2); /* Finally enable the USART. */ usart_enable(USART2); }
void i2c_config_gpios(const i2c_dev *dev) { gpio_set_modef(sda_port(dev), dev->sda_pin, GPIO_MODE_AF, GPIO_MODEF_TYPE_OD); gpio_set_modef(scl_port(dev), dev->scl_pin, GPIO_MODE_AF, GPIO_MODEF_TYPE_OD); gpio_set_af(sda_port(dev), dev->sda_pin, GPIO_AF_4); gpio_set_af(scl_port(dev), dev->scl_pin, GPIO_AF_4); }
void usart_setup(void) { /* Enable all required USART modules */ rcc_periph_clock_enable(USART_RCC_PORT); rcc_periph_clock_enable(USART_RCC_ID); /* Setup GPIO pins for USART transmit. */ gpio_mode_setup(USART_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, USART_RX); gpio_mode_setup(USART_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, USART_TX); // gpio_set_output_options(USART_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, USART_TX); /* Setup USART TX pin as alternate function. */ gpio_set_af(USART_PORT, GPIO_AF7, USART_RX); gpio_set_af(USART_PORT, GPIO_AF7, USART_TX); /* Setup USART parameters. */ usart_set_baudrate(USART_ID, USART_BAUD_RATE); usart_set_databits(USART_ID, 8); usart_set_stopbits(USART_ID, USART_STOPBITS_1); usart_set_mode(USART_ID, USART_MODE_TX_RX); usart_set_parity(USART_ID, USART_PARITY_NONE); usart_set_flow_control(USART_ID, USART_FLOWCONTROL_NONE); /* Finally enable the USART. */ usart_enable(USART_ID); }
/* * Set up the GPIO pins and peripheral clocks for the SDIO * system. The code should probably take an option card detect * pin, at the moment it uses the one used by the Embest board. */ static void stm32_sdio_rcc_init(void) { /* Enable clocks for SDIO and DMA2 */ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_SDIOEN); #ifdef WITH_DMA2 rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_DMA2EN); #endif /* Setup GPIO Pins for SDIO: PC8 - PC11 - DAT0 thru DAT3 PC12 - CLK PD2 - CMD */ gpio_set_output_options(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_25MHZ, GPIO12 ); // All SDIO lines are push-pull, 25Mhz gpio_set_output_options(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_25MHZ, GPIO8 | GPIO9 | GPIO10 | GPIO11 ); // All SDIO lines are push-pull, 25Mhz gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO8 | GPIO9 | GPIO10 | GPIO11); // D0 - D3 enable pullups (bi-directional) gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO12); // CLK line no pullup gpio_set_af(GPIOC, GPIO_AF12, GPIO8 | GPIO9 | GPIO10 | GPIO11 | GPIO12); gpio_set_output_options(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_25MHZ, GPIO2); gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12 | GPIO15); gpio_mode_setup(GPIOD, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO2); gpio_set_af(GPIOD, GPIO_AF12, GPIO2); #ifdef SDIO_HAS_CARD_DETECT /* SDIO Card Detect pin on the Embest Baseboard */ /* PB15 as a hacked Card Detect (active LOW for card present) */ gpio_mode_setup(GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO15); #endif }
void i2c_config_gpios(const i2c_dev *dev) { /* Untested... */ gpio_set_modef(sda_port(dev), dev->sda_pin, GPIO_MODE_AF, I2C_MODEF_FLAGS); gpio_set_modef(scl_port(dev), dev->scl_pin, GPIO_MODE_AF, I2C_MODEF_FLAGS); gpio_set_af(sda_port(dev), dev->sda_pin, GPIO_AF_I2C); gpio_set_af(scl_port(dev), dev->scl_pin, GPIO_AF_I2C); }
static void platform_init_pwm() { gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO6); gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO7); gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO8); gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9); gpio_set_af(GPIOC, GPIO_AF2, GPIO6); gpio_set_af(GPIOC, GPIO_AF2, GPIO7); gpio_set_af(GPIOC, GPIO_AF2, GPIO8); gpio_set_af(GPIOC, GPIO_AF2, GPIO9); timer_reset(TIM3); timer_disable_oc_output(TIM3, TIM_OC1); timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); /* 72ish Hz, close to 60 */ timer_set_period(TIM3, 65535); timer_set_prescaler(TIM3, 16); timer_disable_preload(TIM3); timer_continuous_mode(TIM3); /* Setup output compare registers */ timer_ic_set_input(TIM3, TIM_IC1, TIM_IC_OUT); timer_disable_oc_clear(TIM3, TIM_OC1); timer_disable_oc_preload(TIM3, TIM_OC1); timer_set_oc_slow_mode(TIM3, TIM_OC1); timer_set_oc_mode(TIM3, TIM_OC1, TIM_OCM_PWM1); timer_set_oc_value(TIM3, TIM_OC1, 0); timer_set_oc_polarity_high(TIM3, TIM_OC1); timer_enable_oc_output(TIM3, TIM_OC1); timer_ic_set_input(TIM3, TIM_IC2, TIM_IC_OUT); timer_disable_oc_clear(TIM3, TIM_OC2); timer_disable_oc_preload(TIM3, TIM_OC2); timer_set_oc_slow_mode(TIM3, TIM_OC2); timer_set_oc_mode(TIM3, TIM_OC2, TIM_OCM_PWM1); timer_set_oc_value(TIM3, TIM_OC2, 0); timer_set_oc_polarity_high(TIM3, TIM_OC2); timer_enable_oc_output(TIM3, TIM_OC2); timer_ic_set_input(TIM3, TIM_IC3, TIM_IC_OUT); timer_disable_oc_clear(TIM3, TIM_OC3); timer_disable_oc_preload(TIM3, TIM_OC3); timer_set_oc_slow_mode(TIM3, TIM_OC3); timer_set_oc_mode(TIM3, TIM_OC3, TIM_OCM_PWM1); timer_set_oc_value(TIM3, TIM_OC3, 0); timer_set_oc_polarity_high(TIM3, TIM_OC3); timer_enable_oc_output(TIM3, TIM_OC3); timer_ic_set_input(TIM3, TIM_IC4, TIM_IC_OUT); timer_disable_oc_clear(TIM3, TIM_OC4); timer_disable_oc_preload(TIM3, TIM_OC4); timer_set_oc_slow_mode(TIM3, TIM_OC4); timer_set_oc_mode(TIM3, TIM_OC4, TIM_OCM_PWM1); timer_set_oc_value(TIM3, TIM_OC4, 0); timer_set_oc_polarity_high(TIM3, TIM_OC4); timer_enable_oc_output(TIM3, TIM_OC4); timer_enable_counter(TIM3); }
void i2c3_hw_init(void) { i2c3.reg_addr = (void *)I2C3; i2c3.init_struct = NULL; i2c3.errors = &i2c3_errors; i2c3_watchdog_counter = 0; /* zeros error counter */ ZEROS_ERR_COUNTER(i2c3_errors); /* reset peripheral to default state ( sometimes not achieved on reset :( ) */ //i2c_reset(I2C3); /* Configure priority grouping 0 bits for pre-emption priority and 4 bits for sub-priority. */ scb_set_priority_grouping(SCB_AIRCR_PRIGROUP_NOGROUP_SUB16); /* Configure and enable I2C3 event interrupt --------------------------------*/ nvic_set_priority(NVIC_I2C3_EV_IRQ, 0); nvic_enable_irq(NVIC_I2C3_EV_IRQ); /* Configure and enable I2C3 err interrupt ----------------------------------*/ nvic_set_priority(NVIC_I2C3_ER_IRQ, 1); nvic_enable_irq(NVIC_I2C3_ER_IRQ); /* Enable peripheral clocks -------------------------------------------------*/ /* Enable I2C3 clock */ rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_I2C3EN); /* Enable GPIO clock */ gpio_enable_clock(I2C3_GPIO_SCL_PORT); gpio_mode_setup(I2C3_GPIO_SCL_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, I2C3_GPIO_SCL); gpio_set_output_options(I2C3_GPIO_SCL_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, I2C3_GPIO_SCL); gpio_set_af(I2C3_GPIO_SCL_PORT, GPIO_AF4, I2C3_GPIO_SCL); gpio_enable_clock(I2C3_GPIO_SDA_PORT); gpio_mode_setup(I2C3_GPIO_SDA_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, I2C3_GPIO_SDA); gpio_set_output_options(I2C3_GPIO_SDA_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, I2C3_GPIO_SDA); gpio_set_af(I2C3_GPIO_SDA_PORT, GPIO_AF4, I2C3_GPIO_SDA); i2c_reset(I2C3); // enable peripheral i2c_peripheral_enable(I2C3); /* * XXX: there is a function to do that already in libopencm3 but I am not * sure if it is correct, using direct register instead (esden) */ //i2c_set_own_7bit_slave_address(I2C3, 0); I2C_OAR1(I2C3) = 0 | 0x4000; // enable error interrupts I2C_CR2(I2C1) |= I2C_CR2_ITERREN; i2c_setbitrate(&i2c3, I2C3_CLOCK_SPEED); }
void usart_gpio_setup(void) { /* Setup GPIO pins for USART2 transmit/receive. */ gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO2); gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO3); /* Setup USART2 TX/RX pins as alternate function. */ gpio_set_af(GPIOA, GPIO_AF7, GPIO2); gpio_set_af(GPIOA, GPIO_AF7, GPIO3); }
void candle_can_init(void) { rx_callback = 0; memset(led_status, 0, sizeof(led_status)); clear_tx_queue(0); clear_tx_queue(1); // enable led outputs rcc_periph_clock_enable(RCC_GPIOD); gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12 | GPIO13 | GPIO14 | GPIO15); // enable can1 peripheral rcc_periph_clock_enable(RCC_GPIOD); rcc_periph_clock_enable(RCC_CAN1); gpio_mode_setup(GPIOD, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO0 | GPIO1); gpio_set_output_options(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, GPIO0 | GPIO1); gpio_set_af(GPIOD, GPIO_AF9, GPIO0 | GPIO1); reset_can(CAN1); init_can(CAN1); // enable can1 transceiver rcc_periph_clock_enable(RCC_GPIOC); gpio_mode_setup(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO6); gpio_clear(GPIOC, GPIO6); // enable can2 peripheral rcc_periph_clock_enable(RCC_GPIOB); rcc_periph_clock_enable(RCC_CAN2); gpio_mode_setup(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO12 | GPIO13); gpio_set_output_options(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, GPIO12 | GPIO13); gpio_set_af(GPIOB, GPIO_AF9, GPIO12 | GPIO13); reset_can(CAN2); init_can(CAN2); // enable can2 transceiver rcc_periph_clock_enable(RCC_GPIOD); gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO11); gpio_clear(GPIOD, GPIO11); // init filter banks CAN_FMR(CAN1) |= CAN_FMR_FINIT; // switch filter banks to init mode // configure usage of 14 filter banks for can1 and 14 banks for can2 CAN_FMR(CAN1) &= ~CAN_FMR_CAN2SB_MASK; CAN_FMR(CAN1) |= (14<<CAN_FMR_CAN2SB_SHIFT); can_filter_id_mask_32bit_init(CAN1, 0, 0, 0, 0, 1); // set a catch-all filter for CAN1 fifo 0 can_filter_id_mask_32bit_init(CAN2, 14, 0, 0, 0, 1); // set a catch-all filter for CAN2 fifo 0 CAN_FMR(CAN1) &= ~CAN_FMR_FINIT; // switch filter banks to active mode }
/* Initialize the SPI peripheral. */ void init_spi(void){ rcc_periph_clock_enable(RCC_SPI3); rcc_periph_clock_enable(RCC_GPIOB); gpio_mode_setup(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO3 | GPIO4 | GPIO5); gpio_set_af(GPIOB, GPIO_AF6, GPIO3 | GPIO4 | GPIO5); gpio_set_output_options(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, GPIO3 | GPIO5); }
static void usb_setup(void) { /* USB pins */ gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9 | GPIO11 | GPIO12); gpio_set_af(GPIOA, GPIO_AF10, GPIO9 | GPIO11 | GPIO12); }
void bluetooth_init(void) { rcc_periph_clock_enable(RCC_B_WAKE_SW_PORT); rcc_periph_clock_enable(RCC_B_WAKE_HW_PORT); rcc_periph_clock_enable(RCC_B_CMD_PORT); gpio_mode_setup(B_WAKE_SW_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, B_WAKE_SW_PIN); gpio_mode_setup(B_WAKE_HW_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, B_WAKE_HW_PIN); gpio_mode_setup(B_CMD_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, B_CMD_PIN); //uart rcc_periph_clock_enable(RCC_B_USART); gpio_mode_setup(B_USART_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, B_USART_TX_PIN|B_USART_RX_PIN); gpio_set_af(B_USART_PORT, GPIO_AF1, B_USART_TX_PIN|B_USART_RX_PIN); usart_set_baudrate(B_USART, 115200);//9600 ); usart_set_databits(B_USART, 8); usart_set_stopbits(B_USART, USART_CR2_STOP_1_0BIT); usart_set_mode(B_USART, USART_MODE_TX_RX); usart_set_parity(B_USART, USART_PARITY_NONE); usart_set_flow_control(B_USART, USART_FLOWCONTROL_NONE); usart_enable(B_USART); }
static void gpio_setup(void){ rcc_periph_clock_enable(RCC_GPIOD); gpio_mode_setup(GPIOD, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO2); gpio_set_af(GPIOD, GPIO_AF2, GPIO2); }
void usart_setup(void) { /* Setup clock */ /* Enable GPIOA clock for USART. */ rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); /* Enable clocks for USART2. */ rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); /* Setup GPIO pins for USART2 transmit. */ gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2); /* Setup USART2 TX pin as alternate function. */ gpio_set_af(GPIOA, GPIO_AF7, GPIO2); /* Setup USART2 parameters. */ usart_set_baudrate(USART2, 115200); usart_set_databits(USART2, 8); usart_set_stopbits(USART2, USART_STOPBITS_1); usart_set_mode(USART2, USART_MODE_TX); usart_set_parity(USART2, USART_PARITY_NONE); usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); /* Finally enable the USART. */ usart_enable(USART2); }
/* Function to init EEPROM driver and I2C peripheral */ void eeprom_init(void) { /* Enable GPIOB clock. */ rcc_periph_clock_enable(RCC_GPIOB); /* set I2C1_SCL and I2C1_SDA, external pull-up resistors */ gpio_mode_setup(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO6 | GPIO7); /* Open Drain, Speed 100 MHz */ gpio_set_output_options(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_100MHZ, GPIO6 | GPIO7); /* Alternate Function: I2C1 */ gpio_set_af(GPIOB, GPIO_AF4, GPIO6 | GPIO7); /* Enable I2C1 clock. */ rcc_periph_clock_enable(RCC_I2C1); /* Enable I2C1 interrupt. */ nvic_enable_irq(NVIC_I2C1_EV_IRQ); /* reset I2C1 */ i2c_reset(I2C1); /* standard mode */ i2c_set_standard_mode(I2C1); /* clock and bus frequencies */ i2c_set_clock_frequency(I2C1, I2C_CR2_FREQ_2MHZ); i2c_set_ccr(I2C1, 20); /* enable error event interrupt only */ i2c_enable_interrupt(I2C1, I2C_CR2_ITERREN); /* enable I2C */ i2c_peripheral_enable(I2C1); }
void spi_setup(void) { rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_SPI1EN); /* For spi signal pins */ rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPAEN); /* For spi mode select on the l3gd20 */ rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPEEN); /* Setup GPIOE3 pin for spi mode l3gd20 select. */ gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO3); /* Start with spi communication disabled */ gpio_set(GPIOE, GPIO3); /* Setup GPIO pins for AF5 for SPI1 signals. */ gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO5 | GPIO6 | GPIO7); gpio_set_af(GPIOA, GPIO_AF5, GPIO5 | GPIO6 | GPIO7); //spi initialization; spi_set_master_mode(SPI1); spi_set_baudrate_prescaler(SPI1, SPI_CR1_BR_FPCLK_DIV_64); spi_set_clock_polarity_0(SPI1); spi_set_clock_phase_0(SPI1); spi_set_full_duplex_mode(SPI1); spi_set_unidirectional_mode(SPI1); /* bidirectional but in 3-wire */ spi_set_data_size(SPI1, SPI_CR2_DS_8BIT); spi_enable_software_slave_management(SPI1); spi_send_msb_first(SPI1); spi_set_nss_high(SPI1); //spi_enable_ss_output(SPI1); spi_fifo_reception_threshold_8bit(SPI1); SPI_I2SCFGR(SPI1) &= ~SPI_I2SCFGR_I2SMOD; spi_enable(SPI1); }
void enable_test_trigger(trigger_type trig, unsigned int rpm) { if (trig != FORD_TFI) { return; } timeval_t t = time_from_rpm_diff(rpm, 45); /* Set up TIM5 as 32bit clock */ rcc_periph_clock_enable(RCC_TIM5); gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO0); gpio_set_af(GPIOA, GPIO_AF2, GPIO0); timer_reset(TIM5); timer_disable_oc_output(TIM5, TIM_OC1); timer_set_mode(TIM5, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); timer_set_period(TIM5, (unsigned int)t); timer_set_prescaler(TIM5, 0); timer_disable_preload(TIM5); timer_continuous_mode(TIM5); /* Setup output compare registers */ timer_ic_set_input(TIM5, TIM_IC1, TIM_IC_OUT); timer_disable_oc_clear(TIM5, TIM_OC1); timer_disable_oc_preload(TIM5, TIM_OC1); timer_set_oc_slow_mode(TIM5, TIM_OC1); timer_set_oc_mode(TIM5, TIM_OC1, TIM_OCM_TOGGLE); timer_set_oc_value(TIM5, TIM_OC1, t); timer_set_oc_polarity_high(TIM5, TIM_OC1); timer_enable_oc_output(TIM5, TIM_OC1); timer_enable_counter(TIM5); }
void mew_bluetooth_init(void) { gpio_mode_setup(MEW_BLUETOOTH_POWER_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, MEW_BLUETOOTH_POWER_PIN); gpio_set_output_options(MEW_BLUETOOTH_POWER_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, MEW_BLUETOOTH_POWER_PIN); gpio_clear(MEW_BLUETOOTH_POWER_PORT, MEW_BLUETOOTH_POWER_PIN); gpio_mode_setup(MEW_BLUETOOTH_RESET_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, MEW_BLUETOOTH_RESET_PIN); gpio_set_output_options(MEW_BLUETOOTH_RESET_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, MEW_BLUETOOTH_RESET_PIN); gpio_set(MEW_BLUETOOTH_RESET_PORT, MEW_BLUETOOTH_RESET_PIN); gpio_mode_setup(MEW_BLUETOOTH_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, MEW_BLUETOOTH_PIN); gpio_set_af(MEW_BLUETOOTH_PORT, MEW_BLUETOOTH_PORT_AF, MEW_BLUETOOTH_PIN); usart_disable(MEW_BLUETOOTH_USART); usart_set_baudrate(MEW_BLUETOOTH_USART, MEW_BLUETOOTH_SPEED); usart_set_databits(MEW_BLUETOOTH_USART, 8); usart_set_stopbits(MEW_BLUETOOTH_USART, USART_STOPBITS_1); usart_set_mode(MEW_BLUETOOTH_USART, USART_MODE_TX_RX); usart_set_parity(MEW_BLUETOOTH_USART, USART_PARITY_NONE); usart_set_flow_control(MEW_BLUETOOTH_USART, USART_FLOWCONTROL_NONE); usart_enable_rx_interrupt(MEW_BLUETOOTH_USART); usart_disable_tx_interrupt(MEW_BLUETOOTH_USART); usart_enable_tx_dma(MEW_BLUETOOTH_USART); usart_enable(MEW_BLUETOOTH_USART); nvic_enable_irq(MEW_BLUETOOTH_IRQ); nvic_enable_irq(MEW_BLUETOOTH_DMA_NVIC_TX); memset(_mew_bt_buffer, 0, MEW_BT_RECEIVE_BUFFER_SIZE); }
void timer_setup(void) { /* Enable timer clock. */ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_TIM1EN); /* Reset timer. */ timer_reset(TIM1); /* Configure prescaler. */ timer_set_prescaler(TIM1, 160); /* Configure PE11 (AF1: TIM1_CH2) (SYNC_IN). */ rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPEEN); gpio_mode_setup(GPIOE, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11); gpio_set_af(GPIOE, GPIO_AF1, GPIO11); /* Configure input capture. */ timer_ic_disable(TIM1, TIM_IC2); timer_ic_set_input(TIM1, TIM_IC2, TIM_IC_IN_TI2); timer_ic_set_polarity(TIM1, TIM_IC2, TIM_IC_RISING); timer_ic_set_prescaler(TIM1, TIM_IC2, TIM_IC_PSC_OFF); timer_ic_set_filter(TIM1, TIM_IC2, TIM_IC_OFF); timer_ic_enable(TIM1, TIM_IC2); /* Enable counter. */ timer_enable_counter(TIM1); /* Enable IRQs */ nvic_enable_irq(NVIC_TIM1_UP_TIM10_IRQ); timer_enable_irq(TIM1, TIM_DIER_UIE); nvic_enable_irq(NVIC_TIM1_CC_IRQ); timer_enable_irq(TIM1, TIM_DIER_CC2IE); }
int main(void) { /* Check the force bootloader pin*/ rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); if(!gpio_get(GPIOA, GPIO0)) dfu_jump_app_if_valid(); dfu_protect_enable(); /* Set up clock*/ rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]); systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8); systick_set_reload(2100000); systick_interrupt_enable(); systick_counter_enable(); /* Handle LED */ rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN); gpio_clear(GPIOA, GPIO4); gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO4); /* Set up USB*/ rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); rcc_peripheral_enable_clock(&RCC_AHB2ENR, RCC_AHB2ENR_OTGFSEN); /* Set up USB Pins and alternate function*/ gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9 | GPIO10 | GPIO11 | GPIO12); gpio_set_af(GPIOA, GPIO_AF10, GPIO9 | GPIO10| GPIO11 | GPIO12); dfu_init(&stm32f107_usb_driver); dfu_main(); }
void touch_init(void) { gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO0 | GPIO1 | GPIO2 | GPIO3 | GPIO9 | GPIO10 | GPIO11 | GPIO12); gpio_set_af(GPIOA, GPIO_AF3, GPIO0 | GPIO1 | GPIO2 | GPIO3| GPIO9 | GPIO10 | GPIO11 | GPIO12); // gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO0 | GPIO1 | GPIO2 | GPIO3); // gpio_set_af(GPIOA, GPIO_AF3, GPIO0 | GPIO1 | GPIO2 | GPIO3); rcc_periph_clock_enable(RCC_TSC); TSC_CR |= 0b1111 << TSC_CR_CTPH_SHIFT; //charge transfer pulse high TSC_CR |= 0b1111 << TSC_CR_CTPL_SHIFT; //charge transfer pulse low TSC_CR |= 0b1111111 << TSC_CR_SSD_SHIFT; //spread spectrum deviation TSC_CR |= 0b110 << TSC_CR_MCV_SHIFT; //max count value TSC_CR |= 0b000 << TSC_CR_PGPSC_SHIFT; //clock prescaler // TSC_IOSCR |= TSC_IOSCR_G1(4) | TSC_IOSCR_G2(4) | TSC_IOSCR_G3(3); //sampling capacitors selection per group // TSC_IOCCR |= TSC_IOCCR_G1(3) | TSC_IOCCR_G2(3) | TSC_IOCCR_G3(2); //channel selection per grou TSC_IOSCR |= TSC_IOSCR_G1(4) | TSC_IOSCR_G4(4); //sampling capacitors selection per group //TSC_IOGCSR |= TSC_IOGCSR_GxE(1) | TSC_IOGCSR_GxE(2) | TSC_IOGCSR_GxE(3); //enable aquision on the channels TSC_IOGCSR |= TSC_IOGCSR_GxE(1) | TSC_IOGCSR_GxE(4); //enable aquision on the channels //statuses: TSC_IOGCSR_GxS(1) | TSC_IOGCSR_GxS(2) | TSC_IOGCSR_GxS(3); TSC_CR |= TSC_CR_TSCE; //enable touch sensing controller //get some history for(unsigned int i=0; i<100; i++) { touch_read(); } }
/** * Initializes the clocks, the UART peripheral, and the RX and TX buffer * structures. */ static void comm_init(void) { os_char_buffer_init(&bsp_rx_buffer, rx_buffer_mem, BSP_RX_BUFFER_SIZE, NULL); os_char_buffer_init(&bsp_tx_buffer, tx_buffer_mem, BSP_TX_BUFFER_SIZE, enable_usart1_tx_interrupt); rcc_periph_clock_enable(RCC_GPIOA); rcc_periph_clock_enable(RCC_USART1); gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO9 | GPIO10); gpio_set_af(GPIOA, GPIO_AF1, GPIO9 | GPIO10); usart_set_baudrate(USART1, 115200); usart_set_databits(USART1, 8); usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); usart_set_mode(USART1, USART_MODE_TX_RX); usart_set_parity(USART1, USART_PARITY_NONE); usart_set_stopbits(USART1, USART_CR2_STOP_1_0BIT); usart_enable_rx_interrupt(USART1); usart_enable(USART1); nvic_set_priority(NVIC_USART1_IRQ, 0); nvic_enable_irq(NVIC_USART1_IRQ); }
void platform_main(int arg) { rcc_clock_setup_hse(&clock_72mhz); rcc_periph_clock_enable(RCC_GPIOA); rcc_periph_clock_enable(RCC_GPIOB); // CAN pin gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIOA_CAN_RX | GPIOA_CAN_TX); gpio_set_output_options(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, GPIOA_CAN_RX | GPIOA_CAN_TX); gpio_set_af(GPIOA, GPIO_AF9, GPIOA_CAN_RX | GPIOA_CAN_TX); // enable CAN transceiver gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIOA_CAN_DIS); gpio_set_output_options(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, GPIOA_CAN_DIS); gpio_clear(GPIOA, GPIOA_CAN_DIS); // LED on gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIOA_LED); gpio_set_output_options(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, GPIOA_LED); gpio_set(GPIOA, GPIOA_LED); // configure timeout of 10000 milliseconds timeout_timer_init(72000000, 10000); can_interface_init(); bootloader_main(arg); reboot_system(BOOT_ARG_START_BOOTLOADER); }
// setup screen and pad void interface_setup() { // SPI gpio_mode_setup(INTERFACE_SPI_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, INTERFACE_SPI_CLK_PIN | INTERFACE_SPI_MISO_PIN | INTERFACE_SPI_MOSI_PIN ); gpio_set_af(INTERFACE_SPI_PORT, GPIO_AF5, INTERFACE_SPI_CLK_PIN | INTERFACE_SPI_MISO_PIN | INTERFACE_SPI_MOSI_PIN); gpio_mode_setup(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO0 | GPIO1); gpio_mode_setup(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO4 | GPIO5); rcc_periph_clock_enable(RCC_SPI1); spi_init_master(SPI1, SPI_CR1_BAUDRATE_FPCLK_DIV_4, SPI_CR1_CPOL_CLK_TO_0_WHEN_IDLE, SPI_CR1_CPHA_CLK_TRANSITION_1, SPI_CR1_DFF_8BIT, SPI_CR1_MSBFIRST); spi_enable_ss_output(SPI1); spi_enable(SPI1); // OLED ssd1306_setup(); // PAD pad_setup(); nvic_set_priority(NVIC_EXTI0_IRQ,0x00); /* Enable EXTI0 interrupt. */ nvic_enable_irq(NVIC_EXTI0_IRQ); gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO0); /* Configure the EXTI subsystem. */ exti_select_source(EXTI0, GPIOA); exti_set_trigger(EXTI0, EXTI_TRIGGER_FALLING); exti_enable_request(EXTI0); }
void radio_init() { rcc_periph_clock_enable(R_RCC_SPI); rcc_periph_clock_enable(R_RCC_GPIO); gpio_mode_setup(R_SPI_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, R_SPI_PINS); gpio_set_af(R_SPI_PORT, R_SPI_AFn, R_SPI_PINS); gpio_mode_setup(R_CS_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, R_CS_PIN); // Reset and enable the SPI periph spi_reset(R_SPI); spi_init_master(R_SPI, SPI_CR1_BAUDRATE_FPCLK_DIV_64, SPI_CR1_CPOL_CLK_TO_0_WHEN_IDLE, SPI_CR1_CPHA_CLK_TRANSITION_1, SPI_CR1_CRCL_8BIT, SPI_CR1_MSBFIRST); // Trigger an RXNE event when we have 8 bits (one byte) in the buffer spi_fifo_reception_threshold_8bit(R_SPI); // NSS must be set high for the peripheral to function spi_enable_software_slave_management(R_SPI); spi_set_nss_high(R_SPI); gpio_set(R_CS_PORT, R_CS_PIN); // Enable spi_enable(R_SPI); radio_sleep(); }
void target_console_init(void) { /* Enable UART clock */ rcc_periph_clock_enable(CONSOLE_USART_CLOCK); /* Setup GPIO pins for UART2 */ gpio_mode_setup(CONSOLE_USART_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, CONSOLE_USART_GPIO_PINS); gpio_set_af(CONSOLE_USART_GPIO_PORT, CONSOLE_USART_GPIO_AF, CONSOLE_USART_GPIO_PINS); }
void button_init(void) { rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO0); gpio_set_af(GPIOA, GPIO_AF0, GPIO0); }
static void platform_init_eventtimer() { /* Set up TIM2 as 32bit clock */ timer_reset(TIM2); timer_set_mode(TIM2, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); timer_set_period(TIM2, 0xFFFFFFFF); timer_set_prescaler(TIM2, 0); timer_disable_preload(TIM2); timer_continuous_mode(TIM2); /* Setup output compare registers */ timer_disable_oc_output(TIM2, TIM_OC1); timer_disable_oc_output(TIM2, TIM_OC2); timer_disable_oc_output(TIM2, TIM_OC3); timer_disable_oc_output(TIM2, TIM_OC4); 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); /* Setup input captures for CH2-4 Triggers */ gpio_mode_setup(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO3); gpio_set_af(GPIOB, GPIO_AF1, GPIO3); timer_ic_set_input(TIM2, TIM_IC2, TIM_IC_IN_TI2); timer_ic_set_filter(TIM2, TIM_IC2, TIM_IC_CK_INT_N_2); timer_ic_set_polarity(TIM2, TIM_IC2, TIM_IC_FALLING); timer_ic_enable(TIM2, TIM_IC2); gpio_mode_setup(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO10); gpio_set_af(GPIOB, GPIO_AF1, GPIO10); timer_ic_set_input(TIM2, TIM_IC3, TIM_IC_IN_TI3); timer_ic_set_filter(TIM2, TIM_IC3, TIM_IC_CK_INT_N_2); timer_ic_set_polarity(TIM2, TIM_IC3, TIM_IC_FALLING); timer_ic_enable(TIM2, TIM_IC3); gpio_mode_setup(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11); gpio_set_af(GPIOB, GPIO_AF1, GPIO11); timer_ic_set_input(TIM2, TIM_IC4, TIM_IC_IN_TI4); timer_ic_set_filter(TIM2, TIM_IC4, TIM_IC_CK_INT_N_2); timer_ic_set_polarity(TIM2, TIM_IC4, TIM_IC_FALLING); timer_ic_enable(TIM2, TIM_IC4); timer_enable_counter(TIM2); timer_enable_irq(TIM2, TIM_DIER_CC2IE); timer_enable_irq(TIM2, TIM_DIER_CC3IE); timer_enable_irq(TIM2, TIM_DIER_CC4IE); nvic_enable_irq(NVIC_TIM2_IRQ); nvic_set_priority(NVIC_TIM2_IRQ, 0); }