Esempio n. 1
0
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);
}
Esempio n. 2
0
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);
}
Esempio n. 3
0
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
}
Esempio n. 4
0
void i2c_setup(void)
{
	rcc_periph_clock_enable(RCC_I2C1);
	rcc_periph_clock_enable(RCC_GPIOB);

	//clock from 48 MHz
	RCC_CFGR3 |= RCC_CFGR3_I2C1SW;

	//i2c_reset(I2C_BUS);
	RCC_APB1RSTR |= RCC_APB1RSTR_I2C1RST;
	RCC_APB1RSTR &= ~RCC_APB1RSTR_I2C1RST;

	gpio_mode_setup(I2C_PIN_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, I2C_PINS);
	gpio_set_af(I2C_PIN_PORT, I2C_PIN_AF, I2C_PINS);

	//i2c_peripheral_disable(I2C_BUS);
	I2C_CR1(I2C_BUS) &= ~I2C_CR1_PE;

	//configure ANFOFF DNF[3:0] in CR1
	//i2c_enable_analog_filter(I2C_BUS);
	I2C_CR1(I2C_BUS) &= ~I2C_CR1_ANFOFF;

	//i2c_set_digital_filter(I2C_BUS, I2C_CR1_DNF_DISABLED); //default is good

	//Configure PRESC[3:0] SDADEL[3:0] SCLDEL[3:0] SCLH[7:0] SCLL[7:0]
	// in TIMINGR
	//i2c_100khz_i2cclk8mhz(I2C_BUS); //let's assume this is good :)

	//I don't think the timing matters when we're slave
	I2C_TIMINGR(I2C_BUS) = 0;

	//configure No-Stretch CR1 (only relevant in slave mode)
	//i2c_enable_stretching(I2C_BUS);

	//addressing mode
	//i2c_set_7bit_addr_mode(I2C_BUS); //default is good

	//slave address
	I2C_OAR1(I2C_BUS) = I2C_OAR1_OA1EN + (I2C_SLAVE_ADDRESS << 1);

	//enable all interrupts
	I2C_CR1(I2C_BUS) |= 0xfe;

	//i2c_peripheral_enable(I2C_BUS);
	I2C_CR1(I2C_BUS) |= I2C_CR1_PE;

	nvic_enable_irq(NVIC_I2C1_IRQ);
}
Esempio n. 5
0
void init_i2c(const i2c_config *ip)
{
    // Enable periph clock.
    // Configure GPIO.
    // Reset I²C.

    uint32_t base = ip->i_base_address;

    enum rcc_periph_clken clken;
    switch (base) {
    case I2C1:
        clken = RCC_I2C1;
        break;
    case I2C2:
        clken = RCC_I2C2;
        break;
    default:
        assert(false && "unknown I2C base address");
    }
    rcc_periph_clock_enable(clken);

    gpio_init_pins(ip->i_pins, (&ip->i_pins)[1] - ip->i_pins);

    uint32_t i2c_freq_mhz = rcc_apb1_frequency / 1000000;
    uint32_t ccr = rcc_apb1_frequency / I2C_BAUD / 2 + 1;
    if (ccr < 4)
        ccr = 4;

    i2c_peripheral_disable(base);
    i2c_set_clock_frequency(base, i2c_freq_mhz);
    i2c_set_trise(base, i2c_freq_mhz + 1);
    i2c_set_ccr(base, ccr);
    I2C_CR1(base) = 0;
    I2C_OAR1(base) = ip->i_own_address;
    i2c_peripheral_enable(base);
}
Esempio n. 6
0
void i2c_set_own_10bit_slave_address(uint32_t i2c, uint16_t slave)
{
	I2C_OAR1(i2c) = (uint16_t)(I2C_OAR1_OA1MODE | slave);
}
Esempio n. 7
0
void i2c_set_own_7bit_slave_address(uint32_t i2c, uint8_t slave)
{
	I2C_OAR1(i2c) = (uint16_t)(slave << 1);
	I2C_OAR1(i2c) &= ~I2C_OAR1_OA1MODE;
	I2C_OAR1(i2c) |= (1 << 14); /* Datasheet: always keep 1 by software. */
}
Esempio n. 8
0
void i2c_set_own_10bit_slave_address(u32 i2c, u16 slave)
{
	I2C_OAR1(i2c) = (u16)(I2C_OAR1_ADDMODE | slave);
}
Esempio n. 9
0
void i2c_set_own_7bit_slave_address(u32 i2c, u8 slave)
{
	I2C_OAR1(i2c) = (u16)(slave << 1);
	I2C_OAR1(i2c) &= ~I2C_OAR1_ADDMODE;
	I2C_OAR1(i2c) |= (1 << 14); /* Datasheet: always keep 1 by software. */
}
Esempio n. 10
0
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
}