]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - drivers/avr/i2c_master.c
Add user-overridable callback for cancelling UCIS input (#5564)
[qmk_firmware.git] / drivers / avr / i2c_master.c
index 0db949db4aba25ac3ff6a32cc3b9a477e69c45f9..0acc246426460815776263780d0d2f960f36a6c8 100755 (executable)
@@ -1,3 +1,18 @@
+/*  Copyright (C) 2019 Elia Ritterbusch
+ +
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 /* Library made by: g4lvanix
  * Github repository: https://github.com/g4lvanix/I2C-master-lib
  */
 void i2c_init(void) {
   TWSR = 0; /* no prescaler */
   TWBR = (uint8_t)TWBR_val;
+
+  #ifdef __AVR_ATmega32A__
+  // set pull-up resistors on I2C bus pins
+  PORTC |= 0b11;
+
+  // enable TWI (two-wire interface)
+  TWCR |= (1 << TWEN);
+
+  // enable TWI interrupt and slave address ACK
+  TWCR |= (1 << TWIE);
+  TWCR |= (1 << TWEA);
+  #endif
 }
 
 i2c_status_t i2c_start(uint8_t address, uint16_t timeout) {
@@ -109,7 +136,7 @@ int16_t i2c_read_nack(uint16_t timeout) {
   return TWDR;
 }
 
-i2c_status_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout) {
+i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout) {
   i2c_status_t status = i2c_start(address | I2C_WRITE, timeout);
 
   for (uint16_t i = 0; i < length && status >= 0; i++) {
@@ -143,7 +170,7 @@ i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16
   return (status < 0) ? status : I2C_STATUS_SUCCESS;
 }
 
-i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) {
+i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout) {
   i2c_status_t status = i2c_start(devaddr | 0x00, timeout);
   if (status >= 0) {
     status = i2c_write(regaddr, timeout);