summaryrefslogtreecommitdiff
path: root/keyboards/lets_split/i2c.c
diff options
context:
space:
mode:
authorishtob <ishtob@gmail.com>2017-03-22 15:04:11 -0400
committerGitHub <noreply@github.com>2017-03-22 15:04:11 -0400
commit65317a3a020c0b0261849fb4c40d3aae175ac337 (patch)
tree9066b264cec4c11286b46fe1bc837b09c5f1c30d /keyboards/lets_split/i2c.c
parent423c68c210d73f6bb05c3e9f4a994833b12a524e (diff)
Added OLED SSD1306 support to I2C
Diffstat (limited to 'keyboards/lets_split/i2c.c')
-rw-r--r--keyboards/lets_split/i2c.c94
1 files changed, 93 insertions, 1 deletions
diff --git a/keyboards/lets_split/i2c.c b/keyboards/lets_split/i2c.c
index 084c890c40..038f37a4be 100644
--- a/keyboards/lets_split/i2c.c
+++ b/keyboards/lets_split/i2c.c
@@ -48,7 +48,7 @@ void i2c_master_init(void) {
// returns: 0 => success
// 1 => error
uint8_t i2c_master_start(uint8_t address) {
- TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTA);
+ TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
i2c_delay();
@@ -56,6 +56,7 @@ uint8_t i2c_master_start(uint8_t address) {
if ( (TW_STATUS != TW_START) && (TW_STATUS != TW_REP_START))
return 1;
+ // send device address
TWDR = address;
TWCR = (1<<TWINT) | (1<<TWEN);
@@ -159,4 +160,95 @@ ISR(TWI_vect) {
// Reset everything, so we are ready for the next TWI interrupt
TWCR |= (1<<TWIE) | (1<<TWINT) | (ack<<TWEA) | (1<<TWEN);
}
+
+// from SSD1306
+/*
+void i2c_start_wait(unsigned char address)
+{
+ uint8_t twst;
+
+
+ while ( 1 )
+ {
+ // send START condition
+ TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
+
+ // wait until transmission completed
+ while(!(TWCR & (1<<TWINT)));
+
+ // check value of TWI Status Register. Mask prescaler bits.
+ twst = TW_STATUS & 0xF8;
+ if ( (twst != TW_START) && (twst != TW_REP_START)) continue;
+
+ // send device address
+ TWDR = address;
+ TWCR = (1<<TWINT) | (1<<TWEN);
+
+ // wail until transmission completed
+ while(!(TWCR & (1<<TWINT)));
+
+ // check value of TWI Status Register. Mask prescaler bits.
+ twst = TW_STATUS & 0xF8;
+ if ( (twst == TW_MT_SLA_NACK )||(twst ==TW_MR_DATA_NACK) )
+ {
+ // device busy, send stop condition to terminate write operation
+ TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
+
+ // wait until stop condition is executed and bus released
+ while(TWCR & (1<<TWSTO));
+
+ continue;
+ }
+ //if( twst != TW_MT_SLA_ACK) return 1;
+ break;
+ }
+
+}// i2c_start_wait
+*/
+/*************************************************************************
+ Issues a repeated start condition and sends address and transfer direction
+
+ Input: address and transfer direction of I2C device
+
+ Return: 0 device accessible
+ 1 failed to access device
+*************************************************************************/
+/*
+unsigned char i2c_rep_start(unsigned char address)
+{
+ return i2c_master_start( address );
+
+}/* i2c_rep_start */
+/*************************************************************************
+ Read one byte from the I2C device, request more data from device
+
+ Return: byte read from I2C device
+*************************************************************************/
+/*
+unsigned char i2c_readAck(void)
+{
+ TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
+ while(!(TWCR & (1<<TWINT)));
+
+ return TWDR;
+
+}/* i2c_readAck */
+
+
+/*************************************************************************
+ Read one byte from the I2C device, read is followed by a stop condition
+
+ Return: byte read from I2C device
+*************************************************************************
+unsigned char i2c_readNak(void)
+{
+ TWCR = (1<<TWINT) | (1<<TWEN);
+ while(!(TWCR & (1<<TWINT)));
+
+ return TWDR;
+
+}/* i2c_readNak */
+
+
+
#endif