29 #if defined(CONFIG_TWI_I2C_AVAILABLE) && defined(CONFIG_TWI_I2C_ENABLE) 35 #define SSD1306_TWI_FREQ 400000 36 #define MAX_RETRIES 64 40 static uint8_t ssd1306_twi_start(
void)
43 uint8_t iters = MAX_RETRIES;
46 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
47 while ( (TWCR & (1<<TWINT)) == 0 );
53 }
while (twst == TW_MT_ARB_LOST);
54 if ((twst != TW_START) && (twst != TW_REP_START))
61 static uint8_t ssd1306_twi_send(uint8_t data)
64 uint8_t iters = MAX_RETRIES;
68 TWCR = (1<<TWINT) | (1<<TWEN);
69 while ( (TWCR & (1<<TWINT)) == 0 );
71 if ((twsr == TW_MT_SLA_ACK) || (twsr == TW_MT_DATA_ACK))
75 if (twsr == TW_MT_ARB_LOST)
84 }
while (twsr != TW_MT_ARB_LOST);
89 static void ssd1306_twi_stop(
void)
91 TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);
94 static void ssd1306_i2cStart_Twi(
void)
98 if (ssd1306_twi_start() != 0)
103 }
while (ssd1306_twi_send(s_sa << 1) == TW_MT_ARB_LOST);
106 static void ssd1306_i2cStop_Twi(
void)
113 #if defined(__AVR_ATmega328P__) 115 DDRC &= ~(1<<PINC4); PORTC |= (1<<PINC4);
116 DDRC &= ~(1<<PINC5); PORTC |= (1<<PINC5);
121 TWBR = ((F_CPU / SSD1306_TWI_FREQ) - 16) / 2 / (1);
122 TWCR = (1 << TWEN) | (1 << TWEA);
125 static void ssd1306_i2cSendByte_Twi(uint8_t data)
129 if (ssd1306_twi_send(data) != TW_MT_ARB_LOST)
133 if (ssd1306_twi_start() != 0)
138 if (ssd1306_twi_send(s_sa << 1) != TW_MT_ARB_LOST)
146 static void ssd1306_i2cSendBytes_Twi(
const uint8_t *buffer, uint16_t size)
150 ssd1306_i2cSendByte_Twi(*buffer);
156 static void ssd1306_i2cClose_Twi()
void ssd1306_i2cInit_Twi(uint8_t sa)
void(* send)(uint8_t data)
void ssd1306_i2cConfigure_Twi(uint8_t arg)
void(* close)(void)
deinitializes internal resources, allocated for interface.
ssd1306_interface_t ssd1306_intf
void(* send_buffer)(const uint8_t *buffer, uint16_t size)
Sends bytes to SSD1306 device.