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); }
static void gpio_enable_clock_escs(int8_t escIndex) { int i = 0; if (escIndex == -1) { for (i = 0; i < ESC_COUNT; i++) { gpio_enable_clock(escHardware[i].periph); } } else { gpio_enable_clock(escHardware[escIndex].periph); } }
// Configure all GPIOs static void init_all_gpio(int8_t escIndex) { // Programmer RX gpio_enable_clock(S1W_RX_PERIPH); gpio_set_mode(S1W_RX_GPIO, S1W_RX_PIN, Mode_IPU); // Programmer TX gpio_enable_clock(S1W_TX_PERIPH); gpio_set_mode(S1W_TX_GPIO, S1W_TX_PIN, Mode_Out_PP); // Escs gpio_enable_clock_escs(escIndex); // Escs pins start in input mode, pullup is always on gpio_set_mode_escs(escIndex, Mode_IPU); }
void i2c2_hw_init(void) { i2c2.reg_addr = (void *)I2C2; i2c2.init_struct = NULL; i2c2.errors = &i2c2_errors; i2c2_watchdog_counter = 0; /* zeros error counter */ ZEROS_ERR_COUNTER(i2c2_errors); /* reset peripheral to default state ( sometimes not achieved on reset :( ) */ //i2c_reset(I2C2); /* Configure and enable I2C2 event interrupt --------------------------------*/ nvic_set_priority(NVIC_I2C2_EV_IRQ, NVIC_I2C2_IRQ_PRIO); nvic_enable_irq(NVIC_I2C2_EV_IRQ); /* Configure and enable I2C2 err interrupt ----------------------------------*/ nvic_set_priority(NVIC_I2C2_ER_IRQ, NVIC_I2C2_IRQ_PRIO+1); nvic_enable_irq(NVIC_I2C2_ER_IRQ); /* Enable peripheral clocks -------------------------------------------------*/ /* Enable I2C2 clock */ rcc_periph_clock_enable(RCC_I2C2); /* Enable GPIO clock */ gpio_enable_clock(I2C2_GPIO_PORT); #if defined(STM32F1) gpio_set_mode(I2C2_GPIO_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, I2C2_GPIO_SCL | I2C2_GPIO_SDA); #elif defined(STM32F4) gpio_mode_setup(I2C2_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, I2C2_GPIO_SCL | I2C2_GPIO_SDA); gpio_set_output_options(I2C2_GPIO_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, I2C2_GPIO_SCL | I2C2_GPIO_SDA); gpio_set_af(I2C2_GPIO_PORT, GPIO_AF4, I2C2_GPIO_SCL | I2C2_GPIO_SDA); #endif i2c_reset(I2C2); // enable peripheral i2c_peripheral_enable(I2C2); /* * 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(I2C2, 0); I2C_OAR1(I2C2) = 0 | 0x4000; // enable error interrupts I2C_CR2(I2C1) |= I2C_CR2_ITERREN; i2c_setbitrate(&i2c2, I2C2_CLOCK_SPEED); }
void ppm_arch_init ( void ) { /* timer clock enable */ rcc_peripheral_enable_clock(PPM_RCC, PPM_PERIPHERAL); /* GPIO clock enable */ gpio_enable_clock(PPM_GPIO_PORT); /* timer gpio configuration */ gpio_setup_pin_af(PPM_GPIO_PORT, PPM_GPIO_PIN, PPM_GPIO_AF, FALSE); /* Time Base configuration */ timer_reset(PPM_TIMER); timer_set_mode(PPM_TIMER, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); timer_set_period(PPM_TIMER, 0xFFFF); timer_set_prescaler(PPM_TIMER, (PPM_TIMER_CLK / (RC_PPM_TICKS_PER_USEC*ONE_MHZ_CLK)) - 1); /* TIM configuration: Input Capture mode --------------------- The Rising edge is used as active edge ------------------------------------------------------------ */ #if defined PPM_PULSE_TYPE && PPM_PULSE_TYPE == PPM_PULSE_TYPE_POSITIVE timer_ic_set_polarity(PPM_TIMER, PPM_CHANNEL, TIM_IC_RISING); #elif defined PPM_PULSE_TYPE && PPM_PULSE_TYPE == PPM_PULSE_TYPE_NEGATIVE timer_ic_set_polarity(PPM_TIMER, PPM_CHANNEL, TIM_IC_FALLING); #else #error "Unknown PPM_PULSE_TYPE" #endif timer_ic_set_input(PPM_TIMER, PPM_CHANNEL, PPM_TIMER_INPUT); timer_ic_set_prescaler(PPM_TIMER, PPM_CHANNEL, TIM_IC_PSC_OFF); timer_ic_set_filter(PPM_TIMER, PPM_CHANNEL, TIM_IC_OFF); /* Enable timer Interrupt(s). */ nvic_set_priority(PPM_IRQ, 2); nvic_enable_irq(PPM_IRQ); #ifdef PPM_IRQ2 nvic_set_priority(PPM_IRQ2, 2); nvic_enable_irq(PPM_IRQ2); #endif /* Enable the Capture/Compare and Update interrupt requests. */ timer_enable_irq(PPM_TIMER, (PPM_CC_IE | TIM_DIER_UIE)); /* Enable capture channel. */ timer_ic_enable(PPM_TIMER, PPM_CHANNEL); /* TIM enable counter */ timer_enable_counter(PPM_TIMER); ppm_last_pulse_time = 0; ppm_cur_pulse = RADIO_CONTROL_NB_CHANNEL; timer_rollover_cnt = 0; }
static void i2c_setup_gpio(uint32_t i2c) { switch (i2c) { #if USE_I2C1 case I2C1: gpio_enable_clock(I2C1_GPIO_PORT); gpio_mode_setup(I2C1_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, I2C1_GPIO_SCL | I2C1_GPIO_SDA); gpio_set_output_options(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, I2C1_GPIO_SCL | I2C1_GPIO_SDA); gpio_set_af(I2C1_GPIO_PORT, GPIO_AF4, I2C1_GPIO_SCL | I2C1_GPIO_SDA); break; #endif #if USE_I2C2 case I2C2: gpio_enable_clock(I2C2_GPIO_PORT); gpio_mode_setup(I2C2_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, I2C2_GPIO_SCL | I2C2_GPIO_SDA); gpio_set_output_options(I2C2_GPIO_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, I2C2_GPIO_SCL | I2C2_GPIO_SDA); gpio_set_af(I2C2_GPIO_PORT, GPIO_AF4, I2C2_GPIO_SCL | I2C2_GPIO_SDA); break; #endif #if USE_I2C3 case I2C3: gpio_enable_clock(I2C3_GPIO_PORT_SCL); gpio_mode_setup(I2C3_GPIO_PORT_SCL, GPIO_MODE_AF, GPIO_PUPD_NONE, I2C3_GPIO_SCL); gpio_set_output_options(I2C3_GPIO_PORT_SCL, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, I2C3_GPIO_SCL); gpio_set_af(I2C3_GPIO_PORT_SCL, GPIO_AF4, I2C3_GPIO_SCL); gpio_enable_clock(I2C3_GPIO_PORT_SDA); gpio_mode_setup(I2C3_GPIO_PORT_SDA, GPIO_MODE_AF, GPIO_PUPD_NONE, I2C3_GPIO_SDA); gpio_set_output_options(I2C3_GPIO_PORT_SDA, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, I2C3_GPIO_SDA); gpio_set_af(I2C3_GPIO_PORT_SDA, GPIO_AF4, I2C3_GPIO_SDA); break; #endif default: break; } }
static void ledInitDebug(void) { #if defined(STM32F3DISCOVERY) GPIO_DeInit(GPIOE); gpio_enable_clock(RCC_AHBPeriph_GPIOE); gpio_set_mode(GPIOE, GPIO_Pin_8|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_14|GPIO_Pin_15, Mode_Out_PP); // Inverted LEDs ledSetState(GPIOE, GPIO_Pin_8|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_14|GPIO_Pin_15, Bit_RESET); #endif return; }
void gpio_setup_pin_af(uint32_t port, uint16_t pin, uint32_t af, bool_t is_output) { gpio_enable_clock(port); /* remap alternate function if needed */ if (af) { rcc_periph_clock_enable(RCC_AFIO); AFIO_MAPR |= af; } if (is_output) gpio_set_mode(port, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, pin); else gpio_set_mode(port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, pin); }
static void i2c_setup_gpio(uint32_t i2c) { switch (i2c) { #if USE_I2C1 case I2C1: gpio_enable_clock(I2C1_GPIO_PORT); gpio_set_mode(I2C1_GPIO_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, I2C1_GPIO_SCL | I2C1_GPIO_SDA); break; #endif #if USE_I2C2 case I2C2: gpio_enable_clock(I2C2_GPIO_PORT); gpio_set_mode(I2C2_GPIO_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, I2C2_GPIO_SCL | I2C2_GPIO_SDA); break; #endif default: break; } }
void gpio_setup_pin_af(uint32_t port, uint16_t pin, uint8_t af, bool_t is_output __attribute__ ((unused))) { gpio_enable_clock(port); gpio_mode_setup(port, GPIO_MODE_AF, GPIO_PUPD_NONE, pin); gpio_set_af(port, af, pin); }
/* * uart_init(tx pin, rx pin, baudrate); * * Initialize a UART that will talk on the tx/rx pin pair at a given baudrate * Curently only 8n1 format, no-hw flow control, only. * * Returns channel unumber (0 - MAX_UART_CHANNELS) or -1 on error */ int uart_init(enum GPIO_PORT_PIN tx, enum GPIO_PORT_PIN rx, int baudrate) { uint32_t my_uart = uart_pin_map(tx, USART); int i; if (uart_pin_map(rx, USART) != my_uart) { /* Both pins are not connected to same serial port */ return -1; } if (nxt_channel >= MAX_UART_CHANNELS) { /* Need more channel configured */ return -2; } if (my_uart == 0) { /* neither pin connects to a USART? */ return -3; } /* Enable Clock for the USART/UART involved */ rcc_peripheral_enable_clock((uint32_t *)uart_pin_map(tx, APB_REG), uart_pin_map(tx, APB_ENA)); /* Enable Clock for the GPIOs we are using */ gpio_enable_clock(tx); gpio_enable_clock(rx); /* GPIO pins */ /* Both AF Mode */ gpio_mode_setup(gpio_base(tx), GPIO_MODE_AF, GPIO_PUPD_NONE, gpio_bit(tx)); gpio_mode_setup(gpio_base(rx), GPIO_MODE_AF, GPIO_PUPD_NONE, gpio_bit(rx)); gpio_set_af(gpio_base(tx), uart_pin_map(tx, AF), gpio_bit(tx)); gpio_set_af(gpio_base(rx), uart_pin_map(rx, AF), gpio_bit(rx)); /* Transmit pin set to an output */ gpio_set_output_options(gpio_base(tx), GPIO_OTYPE_PP, GPIO_OSPEED_25MHZ, gpio_bit(tx)); /* Set up UART parameters */ usart_set_baudrate(my_uart, baudrate); usart_set_databits(my_uart, 8); usart_set_stopbits(my_uart, USART_STOPBITS_1); usart_set_mode(my_uart, USART_MODE_TX_RX); usart_set_parity(my_uart, USART_PARITY_NONE); usart_set_flow_control(my_uart, USART_FLOWCONTROL_NONE); usart_enable(my_uart); nxt_recv_ndx[nxt_channel] = cur_recv_ndx[nxt_channel] = 0; nxt_xmit_ndx[nxt_channel] = cur_xmit_ndx[nxt_channel] = 0; /* * This was done to try to get it to run under GDB with the Black * Magic debug probe but it didn't have any effect (interrupts * are still masked) */ nvic_set_priority(uart_pin_map(tx, IRQ), 0); // highest priority nvic_enable_irq(uart_pin_map(tx, IRQ)); USART_DR(my_uart) = 0; usart_enable_rx_interrupt(my_uart); /* Now create two mappings, channel => usart, and usart => channel */ channel_to_uart_map[nxt_channel] = my_uart; for (i = 0; i < NUARTS; i++) { if (UART_MAP[i] == my_uart) { uart_to_channel_map[i] = nxt_channel; break; } } nxt_channel++; return (nxt_channel - 1); }
void i2c1_hw_init(void) { i2c1.reg_addr = (void *)I2C1; i2c1.init_struct = NULL; i2c1.errors = &i2c1_errors; i2c1_watchdog_counter = 0; /* zeros error counter */ ZEROS_ERR_COUNTER(i2c1_errors); // Extra #ifdef I2C_DEBUG_LED LED_INIT(); #else /* reset peripheral to default state ( sometimes not achieved on reset :( ) */ //i2c_reset(I2C1); /* 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 I2C1 event interrupt --------------------------------*/ nvic_set_priority(NVIC_I2C1_EV_IRQ, 0); nvic_enable_irq(NVIC_I2C1_EV_IRQ); /* Configure and enable I2C1 err interrupt ----------------------------------*/ nvic_set_priority(NVIC_I2C1_ER_IRQ, 1); nvic_enable_irq(NVIC_I2C1_ER_IRQ); /* Enable peripheral clocks -------------------------------------------------*/ /* Enable I2C1 clock */ rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_I2C1EN); /* Enable GPIO clock */ gpio_enable_clock(I2C1_GPIO_PORT); #if defined(STM32F1) gpio_set_mode(I2C1_GPIO_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, I2C1_GPIO_SCL | I2C1_GPIO_SDA); #elif defined(STM32F4) gpio_mode_setup(I2C1_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, I2C1_GPIO_SCL | I2C1_GPIO_SDA); gpio_set_output_options(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, I2C1_GPIO_SCL | I2C1_GPIO_SDA); gpio_set_af(I2C1_GPIO_PORT, GPIO_AF4, I2C1_GPIO_SCL | I2C1_GPIO_SDA); #endif i2c_reset(I2C1); // enable peripheral i2c_peripheral_enable(I2C1); /* * 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(I2C1, 0); I2C_OAR1(I2C1) = 0 | 0x4000; // enable error interrupts I2C_CR2(I2C1) |= I2C_CR2_ITERREN; i2c_setbitrate(&i2c1, I2C1_CLOCK_SPEED); #endif }
void gpio_setup_input(uint32_t port, uint16_t gpios) { gpio_enable_clock(port); gpio_set_mode(port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, gpios); }
void gpio_setup_output(uint32_t port, uint16_t gpios) { gpio_enable_clock(port); gpio_set_mode(port, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, gpios); }
void gpio_setup_input_pulldown(uint32_t port, uint16_t gpios) { gpio_enable_clock(port); gpio_clear(port, gpios); gpio_set_mode(port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, gpios); }
void gpio_setup_input(uint32_t port, uint16_t gpios) { gpio_enable_clock(port); gpio_mode_setup(port, GPIO_MODE_INPUT, GPIO_PUPD_NONE, gpios); }
void gpio_setup_pin_analog(uint32_t port, uint16_t pin) { gpio_enable_clock(port); gpio_set_mode(port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, pin); }
void gpio_setup_pin_analog(uint32_t port, uint16_t pin) { gpio_enable_clock(port); gpio_mode_setup(port, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, pin); }
void gpio_setup_input_pulldown(uint32_t port, uint16_t gpios) { gpio_enable_clock(port); gpio_mode_setup(port, GPIO_MODE_INPUT, GPIO_PUPD_PULLDOWN, gpios); }