]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - keyboards/lets_split/i2c.c
Added OLED SSD1306 support to I2C
[qmk_firmware.git] / keyboards / lets_split / i2c.c
index 084c890c405fa6f7ebfc1be7a699e3d120e5c482..038f37a4be71428c0f0248bb22a94cb93c143ab0 100644 (file)
@@ -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