Advertisement
Guest User

Untitled

a guest
May 24th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. void i2c_init(void) {
  2. rcc_periph_clock_enable(RCC_I2C1);
  3. rcc_periph_clock_enable(RCC_GPIOB);
  4. /* rcc_periph_clock_enable(RCC_AFIO); */
  5.  
  6. gpio_set_mode(GPIOB,
  7. GPIO_MODE_OUTPUT_50_MHZ,
  8. GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN,
  9. GPIO6 | GPIO7);
  10.  
  11. i2c_peripheral_disable(I2C1);
  12.  
  13. i2c_reset(I2C1);
  14. i2c_set_standard_mode(I2C1);
  15.  
  16. // APB1 is running at 36MHz
  17. i2c_set_clock_frequency(I2C1, I2C_CR2_FREQ_36MHZ);
  18.  
  19. // 36MHz / (100kHz * 2)
  20. i2c_set_ccr(I2C1, 180);
  21.  
  22. // standard mode, freq MHz + 1
  23. i2c_set_trise(I2C1, 37);
  24.  
  25. i2c_set_dutycycle(I2C1, I2C_CCR_DUTY_DIV2);
  26.  
  27. /* nvic_enable_irq(NVIC_I2C1_EV_IRQ); */
  28. /* nvic_enable_irq(NVIC_I2C1_ER_IRQ); */
  29. /* i2c_enable_interrupt(I2C1, I2C_CR2_ITERREN | I2C_CR2_ITEVTEN); */
  30.  
  31. i2c_peripheral_enable(I2C1);
  32.  
  33. printf("i2c: intialized\n");
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement