diff options
| -rw-r--r-- | keyboards/exclusive/e6v2/bmc/bmc.c | 4 | ||||
| -rw-r--r-- | keyboards/exclusive/e6v2/bmc/i2c.c | 106 | ||||
| -rw-r--r-- | keyboards/exclusive/e6v2/bmc/i2c.h | 24 | ||||
| -rw-r--r-- | keyboards/exclusive/e6v2/bmc/rules.mk | 2 |
4 files changed, 133 insertions, 3 deletions
diff --git a/keyboards/exclusive/e6v2/bmc/bmc.c b/keyboards/exclusive/e6v2/bmc/bmc.c index 257b68b8b..e3dd244c5 100644 --- a/keyboards/exclusive/e6v2/bmc/bmc.c +++ b/keyboards/exclusive/e6v2/bmc/bmc.c | |||
| @@ -15,7 +15,7 @@ | |||
| 15 | */ | 15 | */ |
| 16 | #include "bmc.h" | 16 | #include "bmc.h" |
| 17 | #include "rgblight.h" | 17 | #include "rgblight.h" |
| 18 | #include "i2c_master.h" | 18 | #include "i2c.h" |
| 19 | 19 | ||
| 20 | void matrix_init_kb(void) { | 20 | void matrix_init_kb(void) { |
| 21 | // put your keyboard start-up code here | 21 | // put your keyboard start-up code here |
| @@ -57,7 +57,7 @@ void rgblight_set(void) { | |||
| 57 | } | 57 | } |
| 58 | 58 | ||
| 59 | i2c_init(); | 59 | i2c_init(); |
| 60 | i2c_transmit(0xb0, (uint8_t*)led, 3 * RGBLED_NUM, 100); | 60 | i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM); |
| 61 | } | 61 | } |
| 62 | #endif | 62 | #endif |
| 63 | 63 | ||
diff --git a/keyboards/exclusive/e6v2/bmc/i2c.c b/keyboards/exclusive/e6v2/bmc/i2c.c new file mode 100644 index 000000000..801242e0d --- /dev/null +++ b/keyboards/exclusive/e6v2/bmc/i2c.c | |||
| @@ -0,0 +1,106 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com> | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 2 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | // Please do not modify this file | ||
| 19 | |||
| 20 | #include <avr/io.h> | ||
| 21 | #include <util/twi.h> | ||
| 22 | |||
| 23 | #include "i2c.h" | ||
| 24 | |||
| 25 | void i2c_set_bitrate(uint16_t bitrate_khz) { | ||
| 26 | uint8_t bitrate_div = ((F_CPU / 1000l) / bitrate_khz); | ||
| 27 | if (bitrate_div >= 16) { | ||
| 28 | bitrate_div = (bitrate_div - 16) / 2; | ||
| 29 | } | ||
| 30 | TWBR = bitrate_div; | ||
| 31 | } | ||
| 32 | |||
| 33 | void i2c_init(void) { | ||
| 34 | // set pull-up resistors on I2C bus pins | ||
| 35 | PORTC |= 0b11; | ||
| 36 | |||
| 37 | i2c_set_bitrate(400); | ||
| 38 | |||
| 39 | // enable TWI (two-wire interface) | ||
| 40 | TWCR |= (1 << TWEN); | ||
| 41 | |||
| 42 | // enable TWI interrupt and slave address ACK | ||
| 43 | TWCR |= (1 << TWIE); | ||
| 44 | TWCR |= (1 << TWEA); | ||
| 45 | } | ||
| 46 | |||
| 47 | uint8_t i2c_start(uint8_t address) { | ||
| 48 | // reset TWI control register | ||
| 49 | TWCR = 0; | ||
| 50 | |||
| 51 | // begin transmission and wait for it to end | ||
| 52 | TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); | ||
| 53 | while (!(TWCR & (1<<TWINT))); | ||
| 54 | |||
| 55 | // check if the start condition was successfully transmitted | ||
| 56 | if ((TWSR & 0xF8) != TW_START) { | ||
| 57 | return 1; | ||
| 58 | } | ||
| 59 | |||
| 60 | // transmit address and wait | ||
| 61 | TWDR = address; | ||
| 62 | TWCR = (1<<TWINT) | (1<<TWEN); | ||
| 63 | while (!(TWCR & (1<<TWINT))); | ||
| 64 | |||
| 65 | // check if the device has acknowledged the READ / WRITE mode | ||
| 66 | uint8_t twst = TW_STATUS & 0xF8; | ||
| 67 | if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) { | ||
| 68 | return 1; | ||
| 69 | } | ||
| 70 | |||
| 71 | return 0; | ||
| 72 | } | ||
| 73 | |||
| 74 | void i2c_stop(void) { | ||
| 75 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO); | ||
| 76 | } | ||
| 77 | |||
| 78 | uint8_t i2c_write(uint8_t data) { | ||
| 79 | TWDR = data; | ||
| 80 | |||
| 81 | // transmit data and wait | ||
| 82 | TWCR = (1<<TWINT) | (1<<TWEN); | ||
| 83 | while (!(TWCR & (1<<TWINT))); | ||
| 84 | |||
| 85 | if ((TWSR & 0xF8) != TW_MT_DATA_ACK) { | ||
| 86 | return 1; | ||
| 87 | } | ||
| 88 | |||
| 89 | return 0; | ||
| 90 | } | ||
| 91 | |||
| 92 | uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length) { | ||
| 93 | if (i2c_start(address)) { | ||
| 94 | return 1; | ||
| 95 | } | ||
| 96 | |||
| 97 | for (uint16_t i = 0; i < length; i++) { | ||
| 98 | if (i2c_write(data[i])) { | ||
| 99 | return 1; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | i2c_stop(); | ||
| 104 | |||
| 105 | return 0; | ||
| 106 | } \ No newline at end of file | ||
diff --git a/keyboards/exclusive/e6v2/bmc/i2c.h b/keyboards/exclusive/e6v2/bmc/i2c.h new file mode 100644 index 000000000..5407d7bee --- /dev/null +++ b/keyboards/exclusive/e6v2/bmc/i2c.h | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com> | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 2 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | // Please do not modify this file | ||
| 19 | |||
| 20 | #pragma once | ||
| 21 | |||
| 22 | void i2c_init(void); | ||
| 23 | void i2c_set_bitrate(uint16_t bitrate_khz); | ||
| 24 | uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length); \ No newline at end of file | ||
diff --git a/keyboards/exclusive/e6v2/bmc/rules.mk b/keyboards/exclusive/e6v2/bmc/rules.mk index 59f98e6f4..7c3e16b15 100644 --- a/keyboards/exclusive/e6v2/bmc/rules.mk +++ b/keyboards/exclusive/e6v2/bmc/rules.mk | |||
| @@ -85,6 +85,6 @@ AUDIO_ENABLE = no # Audio output on port C6 | |||
| 85 | FAUXCLICKY_ENABLE = no # Use buzzer to emulate clicky switches | 85 | FAUXCLICKY_ENABLE = no # Use buzzer to emulate clicky switches |
| 86 | HD44780_ENABLE = no # Enable support for HD44780 based LCDs (+400) | 86 | HD44780_ENABLE = no # Enable support for HD44780 based LCDs (+400) |
| 87 | 87 | ||
| 88 | SRC += i2c_master.c | 88 | SRC += i2c.c |
| 89 | 89 | ||
| 90 | PROGRAM_CMD = ./util/atmega32a_program.py $(TARGET).hex | 90 | PROGRAM_CMD = ./util/atmega32a_program.py $(TARGET).hex |
