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 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 I2C2 event interrupt --------------------------------*/ nvic_set_priority(NVIC_I2C2_EV_IRQ, 0); nvic_enable_irq(NVIC_I2C2_EV_IRQ); /* Configure and enable I2C2 err interrupt ----------------------------------*/ nvic_set_priority(NVIC_I2C2_ER_IRQ, 1); nvic_enable_irq(NVIC_I2C2_ER_IRQ); /* Enable peripheral clocks -------------------------------------------------*/ /* Enable I2C2 clock */ rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_I2C2EN); /* 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 event_i2c_abuse_test(void) { if (i2c_idle(&I2C_ABUSE_PORT)) { LED_ON(5); // green = idle LED_OFF(4); } else { LED_ON(4); // red = busy LED_OFF(5); } // Wait for I2C transaction object to be released by the I2C driver before changing anything if ((i2c_abuse_test_counter < 12) && (i2c_abuse_test_counter > 3)) { if ((i2c_test2.status == I2CTransFailed) || (i2c_test2.status == I2CTransSuccess)) { //i2c_test2.slave_addr = 0x90; i2c_test2.type = I2CTransRx; i2c_test2.slave_addr = 0x92; i2c_test2.len_r = 2; i2c_submit(&I2C_ABUSE_PORT,&i2c_test2); } } if ((i2c_test1.status == I2CTransFailed) || (i2c_test1.status == I2CTransSuccess)) { if (i2c_abuse_test_counter < 16) { i2c_abuse_test_counter++; } else { // wait until ready: if (i2c_idle(&I2C_ABUSE_PORT)) { i2c_abuse_test_counter = 1; i2c_setbitrate(&I2C_ABUSE_PORT, i2c_abuse_test_bitrate); i2c_abuse_test_bitrate += 17000; if (i2c_abuse_test_bitrate > 410000) { i2c_abuse_test_bitrate -= 410000; } } } if (i2c_abuse_test_counter < 16) { RunOnceEvery(100,LED_TOGGLE(I2C_ABUSE_LED)); i2c_abuse_send_transaction( i2c_abuse_test_counter ); } } }
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 and enable I2C3 event interrupt --------------------------------*/ nvic_set_priority(NVIC_I2C3_EV_IRQ, NVIC_I2C3_IRQ_PRIO); nvic_enable_irq(NVIC_I2C3_EV_IRQ); /* Configure and enable I2C3 err interrupt ----------------------------------*/ nvic_set_priority(NVIC_I2C3_ER_IRQ, NVIC_I2C3_IRQ_PRIO+1); nvic_enable_irq(NVIC_I2C3_ER_IRQ); /* Enable peripheral clocks -------------------------------------------------*/ /* Enable I2C3 clock */ rcc_periph_clock_enable(RCC_I2C3); /* Enable GPIO clock */ 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); 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 i2c1_hw_init(void) { i2c1.reg_addr = (void *)I2C1; i2c1.init_struct = NULL; i2c1.errors = &i2c1_errors; i2c1.watchdog = -1; /* 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 :( ) */ //rcc_periph_reset_pulse(RST_I2C1); /* Configure and enable I2C1 event interrupt --------------------------------*/ nvic_set_priority(NVIC_I2C1_EV_IRQ, NVIC_I2C1_IRQ_PRIO); nvic_enable_irq(NVIC_I2C1_EV_IRQ); /* Configure and enable I2C1 err interrupt ----------------------------------*/ nvic_set_priority(NVIC_I2C1_ER_IRQ, NVIC_I2C1_IRQ_PRIO+1); nvic_enable_irq(NVIC_I2C1_ER_IRQ); /* Enable peripheral clocks -------------------------------------------------*/ /* Enable I2C1 clock */ rcc_periph_clock_enable(RCC_I2C1); /* setup gpio clock and pins */ i2c_setup_gpio(I2C1); rcc_periph_reset_pulse(RST_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_enable_interrupt(I2C1, I2C_CR2_ITERREN); i2c_setbitrate(&i2c1, I2C1_CLOCK_SPEED); #endif }
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 and enable I2C1 event interrupt --------------------------------*/ nvic_set_priority(NVIC_I2C1_EV_IRQ, NVIC_I2C1_IRQ_PRIO); nvic_enable_irq(NVIC_I2C1_EV_IRQ); /* Configure and enable I2C1 err interrupt ----------------------------------*/ nvic_set_priority(NVIC_I2C1_ER_IRQ, NVIC_I2C1_IRQ_PRIO+1); nvic_enable_irq(NVIC_I2C1_ER_IRQ); /* Enable peripheral clocks -------------------------------------------------*/ /* Enable I2C1 clock */ rcc_periph_clock_enable(RCC_I2C1); /* 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 }