i2c.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #pragma once
  2. #include <stdint.h>
  3. #ifndef F_CPU
  4. #define F_CPU 16000000UL
  5. #endif
  6. #define I2C_READ 1
  7. #define I2C_WRITE 0
  8. #define I2C_ACK 1
  9. #define I2C_NACK 0
  10. #define SLAVE_BUFFER_SIZE 0x10
  11. // i2c SCL clock frequency 400kHz
  12. #define SCL_CLOCK 400000L
  13. extern volatile uint8_t i2c_slave_buffer[SLAVE_BUFFER_SIZE];
  14. void i2c_master_init(void);
  15. uint8_t i2c_master_start(uint8_t address);
  16. void i2c_master_stop(void);
  17. uint8_t i2c_master_write(uint8_t data);
  18. uint8_t i2c_master_read(int);
  19. void i2c_reset_state(void);
  20. void i2c_slave_init(uint8_t address);
  21. static inline unsigned char i2c_start_read(unsigned char addr) {
  22. return i2c_master_start((addr << 1) | I2C_READ);
  23. }
  24. static inline unsigned char i2c_start_write(unsigned char addr) {
  25. return i2c_master_start((addr << 1) | I2C_WRITE);
  26. }
  27. // from SSD1306 scrips
  28. extern unsigned char i2c_rep_start(unsigned char addr);
  29. extern void i2c_start_wait(unsigned char addr);
  30. extern unsigned char i2c_readAck(void);
  31. extern unsigned char i2c_readNak(void);
  32. extern unsigned char i2c_read(unsigned char ack);
  33. #define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak();