31 #if defined(CONFIG_SOFTWARE_I2C_AVAILABLE) && defined(CONFIG_SOFTWARE_I2C_ENABLE) 33 #include <util/delay_basic.h> 45 #if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) 48 #define PORT_REG PORTB 49 #elif defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) 53 #define PORT_REG PORTA 57 #define PORT_REG PORTC 62 #warning "F_CPU is not defined, there can be I2C issues" 65 #define CPU_CYCLE_NS (1000000000/F_CPU) 67 #define DELAY_LOOP_CYCLES 4 68 #define ssd1306_delay(x) _delay_loop_2(x) 73 #define SSD1306_I2C_START_STOP_DELAY 600 74 #define SSD1306_I2C_RISE_TIME 300 75 #define SSD1306_I2C_FALL_TIME 300 76 #define SSD1306_I2C_DATA_HOLD_TIME 300 77 #define SSD1306_I2C_IDLE_TIME 1300 78 #define SSD1306_I2C_CLOCK 2500 81 #define I2C_START_STOP_DELAY ((SSD1306_I2C_START_STOP_DELAY/CPU_CYCLE_NS + DELAY_LOOP_CYCLES/2)/DELAY_LOOP_CYCLES) 83 #define I2C_RISE_TIME ((SSD1306_I2C_RISE_TIME/CPU_CYCLE_NS)/DELAY_LOOP_CYCLES) 85 #define I2C_DATA_HOLD_TIME ((SSD1306_I2C_DATA_HOLD_TIME/CPU_CYCLE_NS + DELAY_LOOP_CYCLES/2)/DELAY_LOOP_CYCLES) 87 #define I2C_IDLE_TIME (((SSD1306_I2C_IDLE_TIME/CPU_CYCLE_NS) + DELAY_LOOP_CYCLES/2)/DELAY_LOOP_CYCLES) 89 #define I2C_HALF_CLOCK (((SSD1306_I2C_CLOCK - SSD1306_I2C_FALL_TIME - SSD1306_I2C_RISE_TIME - SSD1306_I2C_FALL_TIME)/CPU_CYCLE_NS/2 \ 95 #define DIGITAL_WRITE_HIGH(DREG, PREG, BIT) { DREG &= ~BIT; PREG |= BIT; } 99 #define DIGITAL_WRITE_LOW(DREG, PREG, BIT) { DREG |= BIT; PREG &= ~BIT; } 101 static uint8_t oldSREG;
102 static uint8_t interruptsOff = 0;
108 static void ssd1306_i2cSendByte_Embedded(uint8_t data)
114 DIGITAL_WRITE_HIGH(DDR_REG, PORT_REG, s_sda)
116 DIGITAL_WRITE_LOW(DDR_REG, PORT_REG, s_sda);
118 ssd1306_delay(I2C_RISE_TIME);
120 DIGITAL_WRITE_HIGH(DDR_REG, PORT_REG, s_scl);
121 ssd1306_delay(I2C_HALF_CLOCK);
123 DIGITAL_WRITE_LOW(DDR_REG, PORT_REG, s_scl);
124 ssd1306_delay(I2C_HALF_CLOCK);
127 DIGITAL_WRITE_HIGH(DDR_REG, PORT_REG, s_sda);
128 ssd1306_delay(I2C_RISE_TIME);
129 DIGITAL_WRITE_HIGH(DDR_REG, PORT_REG, s_scl);
130 ssd1306_delay(I2C_HALF_CLOCK);
131 DIGITAL_WRITE_LOW(DDR_REG, PORT_REG, s_scl);
132 ssd1306_delay(I2C_HALF_CLOCK);
135 static void ssd1306_i2cSendBytes_Embedded(
const uint8_t *buffer, uint16_t size)
139 ssd1306_i2cSendByte_Embedded(*buffer);
147 static void ssd1306_i2cStart_Embedded(
void)
152 DIGITAL_WRITE_LOW(DDR_REG, PORT_REG, s_sda);
153 ssd1306_delay(I2C_START_STOP_DELAY);
154 DIGITAL_WRITE_LOW(DDR_REG, PORT_REG, s_scl);
155 ssd1306_delay(I2C_HALF_CLOCK);
156 ssd1306_i2cSendByte_Embedded((s_sa << 1) | 0);
159 static void ssd1306_i2cStop_Embedded(
void)
161 DIGITAL_WRITE_LOW(DDR_REG, PORT_REG, s_sda);
162 ssd1306_delay(I2C_RISE_TIME);
163 DIGITAL_WRITE_HIGH(DDR_REG, PORT_REG, s_scl);
164 ssd1306_delay(I2C_START_STOP_DELAY);
165 DIGITAL_WRITE_HIGH(DDR_REG, PORT_REG, s_sda);
166 ssd1306_delay(I2C_IDLE_TIME);
174 static void ssd1306_i2cClose_Embedded()
180 if (scl>=0) s_scl = (1<<scl);
181 if (sda>=0) s_sda = (1<<sda);
void(* send)(uint8_t data)
#define SSD1306_SDA
SDA, Pin 4 on SSD1306 Board.
void(* close)(void)
deinitializes internal resources, allocated for interface.
ssd1306_interface_t ssd1306_intf
#define SSD1306_SCL
SCL, Pin 3 on SSD1306 Board.
void ssd1306_i2cInit_Embedded(int8_t scl, int8_t sda, uint8_t sa)
void(* send_buffer)(const uint8_t *buffer, uint16_t size)
Sends bytes to SSD1306 device.