aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--keyboards/bfake/bfake.c53
-rw-r--r--keyboards/bfake/i2c.c106
-rw-r--r--keyboards/bfake/i2c.h27
-rw-r--r--keyboards/bfake/rules.mk19
-rw-r--r--keyboards/jc65/v32a/i2c.c106
-rw-r--r--keyboards/jc65/v32a/i2c.h27
-rw-r--r--keyboards/jc65/v32a/rules.mk19
-rw-r--r--keyboards/jc65/v32a/v32a.c56
-rw-r--r--keyboards/jj50/i2c.c104
-rw-r--r--keyboards/jj50/i2c.h25
-rw-r--r--keyboards/jj50/jj50.c67
-rw-r--r--keyboards/jj50/rules.mk24
-rw-r--r--keyboards/mechmini/v1/i2c.c104
-rw-r--r--keyboards/mechmini/v1/i2c.h22
-rw-r--r--keyboards/mechmini/v1/rules.mk19
-rw-r--r--keyboards/mechmini/v1/v1.c60
-rw-r--r--keyboards/mehkee96/i2c.c106
-rw-r--r--keyboards/mehkee96/i2c.h27
-rw-r--r--keyboards/mehkee96/mehkee96.c65
-rw-r--r--keyboards/mehkee96/rules.mk4
-rw-r--r--keyboards/mt40/i2c.c104
-rw-r--r--keyboards/mt40/i2c.h25
-rw-r--r--keyboards/mt40/mt40.c29
-rw-r--r--keyboards/mt40/rules.mk4
-rw-r--r--keyboards/percent/canoe/canoe.c92
-rw-r--r--keyboards/percent/canoe/i2c.c106
-rw-r--r--keyboards/percent/canoe/i2c.h27
-rw-r--r--keyboards/percent/canoe/rules.mk21
-rw-r--r--keyboards/percent/skog/i2c.c106
-rw-r--r--keyboards/percent/skog/i2c.h25
-rw-r--r--keyboards/percent/skog/rules.mk19
-rw-r--r--keyboards/percent/skog/skog.c66
-rw-r--r--keyboards/winkeyless/bmini/bmini.c26
-rw-r--r--keyboards/winkeyless/bmini/i2c.c106
-rw-r--r--keyboards/winkeyless/bmini/i2c.h27
-rw-r--r--keyboards/winkeyless/bmini/rules.mk19
-rw-r--r--keyboards/winkeyless/bminiex/bminiex.c61
-rw-r--r--keyboards/winkeyless/bminiex/i2c.c106
-rw-r--r--keyboards/winkeyless/bminiex/i2c.h25
-rw-r--r--keyboards/winkeyless/bminiex/rules.mk19
-rw-r--r--keyboards/ymd75/i2c.c104
-rw-r--r--keyboards/ymd75/i2c.h25
-rw-r--r--keyboards/ymd75/rules.mk20
-rw-r--r--keyboards/ymd75/ymd75.c67
-rw-r--r--keyboards/ymd96/i2c.c104
-rw-r--r--keyboards/ymd96/i2c.h25
-rw-r--r--keyboards/ymd96/rules.mk19
-rw-r--r--keyboards/ymd96/ymd96.c66
-rw-r--r--keyboards/ymdk_np21/i2c.c104
-rw-r--r--keyboards/ymdk_np21/i2c.h25
-rw-r--r--keyboards/ymdk_np21/rules.mk20
-rw-r--r--keyboards/ymdk_np21/ymdk_np21.c45
52 files changed, 138 insertions, 2539 deletions
diff --git a/keyboards/bfake/bfake.c b/keyboards/bfake/bfake.c
index 2160706fe..f7d006576 100644
--- a/keyboards/bfake/bfake.c
+++ b/keyboards/bfake/bfake.c
@@ -16,51 +16,34 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/ 16*/
17 17
18#include "bfake.h" 18#include "bfake.h"
19#ifdef BACKLIGHT_ENABLE
20#include "backlight.h"
21#endif
22#ifdef RGBLIGHT_ENABLE
23#include "rgblight.h"
24#endif
25
26#include <avr/pgmspace.h>
27
28#include "action_layer.h"
29#include "i2c.h"
30#include "quantum.h"
31
32#ifdef RGBLIGHT_ENABLE
33extern rgblight_config_t rgblight_config;
34
35void rgblight_set(void) {
36 if (!rgblight_config.enable) {
37 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
38 led[i].r = 0;
39 led[i].g = 0;
40 led[i].b = 0;
41 }
42 }
43
44 i2c_init();
45 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
46}
47#endif
48 19
49__attribute__ ((weak)) 20__attribute__ ((weak))
50void matrix_scan_user(void) { 21void matrix_scan_user(void) {}
51}
52 22
53void backlight_init_ports(void) { 23void backlight_init_ports(void) {
54 DDRD |= (1<<0 | 1<<1 | 1<<4 | 1<<6); 24 setPinOutput(D0);
55 PORTD &= ~(1<<0 | 1<<1 | 1<<4 | 1<<6); 25 setPinOutput(D1);
26 setPinOutput(D4);
27 setPinOutput(D6);
28
29 writePinLow(D0);
30 writePinLow(D1);
31 writePinLow(D4);
32 writePinLow(D6);
56} 33}
57 34
58void backlight_set(uint8_t level) { 35void backlight_set(uint8_t level) {
59 if (level == 0) { 36 if (level == 0) {
60 // Turn out the lights 37 // Turn out the lights
61 PORTD &= ~(1<<0 | 1<<1 | 1<<4 | 1<<6); 38 writePinLow(D0);
39 writePinLow(D1);
40 writePinLow(D4);
41 writePinLow(D6);
62 } else { 42 } else {
63 // Turn on the lights 43 // Turn on the lights
64 PORTD |= (1<<0 | 1<<1 | 1<<4 | 1<<6); 44 writePinHigh(D0);
45 writePinHigh(D1);
46 writePinHigh(D4);
47 writePinHigh(D6);
65 } 48 }
66} 49}
diff --git a/keyboards/bfake/i2c.c b/keyboards/bfake/i2c.c
deleted file mode 100644
index a4f952135..000000000
--- a/keyboards/bfake/i2c.c
+++ /dev/null
@@ -1,106 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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
25void 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
33void 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
47uint8_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
74void i2c_stop(void) {
75 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
76}
77
78uint8_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
92uint8_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}
diff --git a/keyboards/bfake/i2c.h b/keyboards/bfake/i2c.h
deleted file mode 100644
index 93a69c94d..000000000
--- a/keyboards/bfake/i2c.h
+++ /dev/null
@@ -1,27 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18// Please do not modify this file
19
20#ifndef __I2C_H__
21#define __I2C_H__
22
23void i2c_init(void);
24void i2c_set_bitrate(uint16_t bitrate_khz);
25uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length);
26
27#endif
diff --git a/keyboards/bfake/rules.mk b/keyboards/bfake/rules.mk
index b5b3b852c..5963c9810 100644
--- a/keyboards/bfake/rules.mk
+++ b/keyboards/bfake/rules.mk
@@ -1,18 +1,3 @@
1# Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com>
2#
3# This program is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16# MCU name 1# MCU name
17MCU = atmega32a 2MCU = atmega32a
18 3
@@ -34,10 +19,10 @@ CONSOLE_ENABLE = yes
34COMMAND_ENABLE = yes 19COMMAND_ENABLE = yes
35BACKLIGHT_ENABLE = no 20BACKLIGHT_ENABLE = no
36RGBLIGHT_ENABLE = no 21RGBLIGHT_ENABLE = no
37RGBLIGHT_CUSTOM_DRIVER = yes 22WS2812_DRIVER = i2c
38 23
39OPT_DEFS = -DDEBUG_LEVEL=0 24OPT_DEFS = -DDEBUG_LEVEL=0
40 25
41# custom matrix setup 26# custom matrix setup
42CUSTOM_MATRIX = yes 27CUSTOM_MATRIX = yes
43SRC = matrix.c i2c.c 28SRC = matrix.c
diff --git a/keyboards/jc65/v32a/i2c.c b/keyboards/jc65/v32a/i2c.c
deleted file mode 100644
index a4f952135..000000000
--- a/keyboards/jc65/v32a/i2c.c
+++ /dev/null
@@ -1,106 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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
25void 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
33void 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
47uint8_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
74void i2c_stop(void) {
75 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
76}
77
78uint8_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
92uint8_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}
diff --git a/keyboards/jc65/v32a/i2c.h b/keyboards/jc65/v32a/i2c.h
deleted file mode 100644
index 93a69c94d..000000000
--- a/keyboards/jc65/v32a/i2c.h
+++ /dev/null
@@ -1,27 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18// Please do not modify this file
19
20#ifndef __I2C_H__
21#define __I2C_H__
22
23void i2c_init(void);
24void i2c_set_bitrate(uint16_t bitrate_khz);
25uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length);
26
27#endif
diff --git a/keyboards/jc65/v32a/rules.mk b/keyboards/jc65/v32a/rules.mk
index ee6efb3fa..18e7f1de9 100644
--- a/keyboards/jc65/v32a/rules.mk
+++ b/keyboards/jc65/v32a/rules.mk
@@ -1,18 +1,3 @@
1# Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com>
2#
3# This program is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16# MCU name 1# MCU name
17MCU = atmega32a 2MCU = atmega32a
18 3
@@ -34,10 +19,10 @@ CONSOLE_ENABLE = yes
34COMMAND_ENABLE = yes 19COMMAND_ENABLE = yes
35BACKLIGHT_ENABLE = yes 20BACKLIGHT_ENABLE = yes
36RGBLIGHT_ENABLE = yes 21RGBLIGHT_ENABLE = yes
37RGBLIGHT_CUSTOM_DRIVER = yes 22WS2812_DRIVER = i2c
38 23
39OPT_DEFS = -DDEBUG_LEVEL=0 24OPT_DEFS = -DDEBUG_LEVEL=0
40 25
41# custom matrix setup 26# custom matrix setup
42CUSTOM_MATRIX = yes 27CUSTOM_MATRIX = yes
43SRC = matrix.c i2c.c 28SRC = matrix.c
diff --git a/keyboards/jc65/v32a/v32a.c b/keyboards/jc65/v32a/v32a.c
index 8176ade0a..9b1e07274 100644
--- a/keyboards/jc65/v32a/v32a.c
+++ b/keyboards/jc65/v32a/v32a.c
@@ -16,56 +16,36 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/ 16*/
17 17
18#include "v32a.h" 18#include "v32a.h"
19#ifdef BACKLIGHT_ENABLE
20#include "backlight.h"
21#endif
22#ifdef RGBLIGHT_ENABLE
23#include "rgblight.h"
24#endif
25
26#include <avr/pgmspace.h>
27
28#include "action_layer.h"
29#include "i2c.h"
30#include "quantum.h"
31
32#ifdef RGBLIGHT_ENABLE
33extern rgblight_config_t rgblight_config;
34
35void rgblight_set(void) {
36 if (!rgblight_config.enable) {
37 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
38 led[i].r = 0;
39 led[i].g = 0;
40 led[i].b = 0;
41 }
42 }
43
44 i2c_init();
45 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
46}
47#endif
48 19
49__attribute__ ((weak)) 20__attribute__ ((weak))
50void matrix_scan_user(void) { 21void matrix_scan_user(void) {}
51#ifdef RGBLIGHT_ENABLE
52 rgblight_task();
53#endif
54}
55 22
56#ifdef BACKLIGHT_ENABLE 23#ifdef BACKLIGHT_ENABLE
57void backlight_init_ports(void) { 24void backlight_init_ports(void) {
58 DDRD |= (1<<0 | 1<<1 | 1<<4 | 1<<6); 25 setPinOutput(D0);
59 PORTD &= ~(1<<0 | 1<<1 | 1<<4 | 1<<6); 26 setPinOutput(D1);
27 setPinOutput(D4);
28 setPinOutput(D6);
29
30 writePinLow(D0);
31 writePinLow(D1);
32 writePinLow(D4);
33 writePinLow(D6);
60} 34}
61 35
62void backlight_set(uint8_t level) { 36void backlight_set(uint8_t level) {
63 if (level == 0) { 37 if (level == 0) {
64 // Turn out the lights 38 // Turn out the lights
65 PORTD &= ~(1<<0 | 1<<1 | 1<<4 | 1<<6); 39 writePinLow(D0);
40 writePinLow(D1);
41 writePinLow(D4);
42 writePinLow(D6);
66 } else { 43 } else {
67 // Turn on the lights 44 // Turn on the lights
68 PORTD |= (1<<0 | 1<<1 | 1<<4 | 1<<6); 45 writePinHigh(D0);
46 writePinHigh(D1);
47 writePinHigh(D4);
48 writePinHigh(D6);
69 } 49 }
70} 50}
71#endif 51#endif
diff --git a/keyboards/jj50/i2c.c b/keyboards/jj50/i2c.c
deleted file mode 100644
index c27f3e3d1..000000000
--- a/keyboards/jj50/i2c.c
+++ /dev/null
@@ -1,104 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include <avr/io.h>
19#include <util/twi.h>
20
21#include "i2c.h"
22
23void i2c_set_bitrate(uint16_t bitrate_khz) {
24 uint8_t bitrate_div = ((F_CPU / 1000l) / bitrate_khz);
25 if (bitrate_div >= 16) {
26 bitrate_div = (bitrate_div - 16) / 2;
27 }
28 TWBR = bitrate_div;
29}
30
31void i2c_init(void) {
32 // set pull-up resistors on I2C bus pins
33 PORTC |= 0b11;
34
35 i2c_set_bitrate(400);
36
37 // enable TWI (two-wire interface)
38 TWCR |= (1 << TWEN);
39
40 // enable TWI interrupt and slave address ACK
41 TWCR |= (1 << TWIE);
42 TWCR |= (1 << TWEA);
43}
44
45uint8_t i2c_start(uint8_t address) {
46 // reset TWI control register
47 TWCR = 0;
48
49 // begin transmission and wait for it to end
50 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
51 while (!(TWCR & (1<<TWINT)));
52
53 // check if the start condition was successfully transmitted
54 if ((TWSR & 0xF8) != TW_START) {
55 return 1;
56 }
57
58 // transmit address and wait
59 TWDR = address;
60 TWCR = (1<<TWINT) | (1<<TWEN);
61 while (!(TWCR & (1<<TWINT)));
62
63 // check if the device has acknowledged the READ / WRITE mode
64 uint8_t twst = TW_STATUS & 0xF8;
65 if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) {
66 return 1;
67 }
68
69 return 0;
70}
71
72void i2c_stop(void) {
73 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
74}
75
76uint8_t i2c_write(uint8_t data) {
77 TWDR = data;
78
79 // transmit data and wait
80 TWCR = (1<<TWINT) | (1<<TWEN);
81 while (!(TWCR & (1<<TWINT)));
82
83 if ((TWSR & 0xF8) != TW_MT_DATA_ACK) {
84 return 1;
85 }
86
87 return 0;
88}
89
90uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length) {
91 if (i2c_start(address)) {
92 return 1;
93 }
94
95 for (uint16_t i = 0; i < length; i++) {
96 if (i2c_write(data[i])) {
97 return 1;
98 }
99 }
100
101 i2c_stop();
102
103 return 0;
104}
diff --git a/keyboards/jj50/i2c.h b/keyboards/jj50/i2c.h
deleted file mode 100644
index 27c9d3d05..000000000
--- a/keyboards/jj50/i2c.h
+++ /dev/null
@@ -1,25 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef __I2C_H__
19#define __I2C_H__
20
21void i2c_init(void);
22void i2c_set_bitrate(uint16_t bitrate_khz);
23uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length);
24
25#endif
diff --git a/keyboards/jj50/jj50.c b/keyboards/jj50/jj50.c
index d4a70f68d..7c3cee95d 100644
--- a/keyboards/jj50/jj50.c
+++ b/keyboards/jj50/jj50.c
@@ -17,24 +17,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18#include "jj50.h" 18#include "jj50.h"
19 19
20#include <avr/pgmspace.h>
21
22#include "action_layer.h"
23#include "i2c.h"
24#include "quantum.h"
25
26#include "backlight.h" 20#include "backlight.h"
27#include "backlight_custom.h" 21#include "backlight_custom.h"
28 22
29// for keyboard subdirectory level init functions 23void matrix_init_kb(void) { matrix_init_user(); }
30// @Override 24
31void matrix_init_kb(void) { 25__attribute__ ((weak))
32 // call user level keymaps, if any 26void matrix_init_user(void) {}
33 matrix_init_user(); 27
34} 28void matrix_scan_kb(void) { matrix_scan_user(); }
29
30__attribute__ ((weak))
31void matrix_scan_user(void) {}
35 32
36#ifdef BACKLIGHT_ENABLE 33#ifdef BACKLIGHT_ENABLE
37/// Overrides functions in `quantum.c`
38void backlight_init_ports(void) { 34void backlight_init_ports(void) {
39 b_led_init_ports(); 35 b_led_init_ports();
40} 36}
@@ -47,48 +43,3 @@ void backlight_set(uint8_t level) {
47 b_led_set(level); 43 b_led_set(level);
48} 44}
49#endif 45#endif
50
51#ifdef RGBLIGHT_ENABLE
52extern rgblight_config_t rgblight_config;
53
54// custom RGB driver
55void rgblight_set(void) {
56 if (!rgblight_config.enable) {
57 for (uint8_t i=0; i<RGBLED_NUM; i++) {
58 led[i].r = 0;
59 led[i].g = 0;
60 led[i].b = 0;
61 }
62 }
63
64 i2c_init();
65 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
66}
67
68bool rgb_init = false;
69
70void matrix_scan_kb(void) {
71 // if LEDs were previously on before poweroff, turn them back on
72 if (rgb_init == false && rgblight_config.enable) {
73 i2c_init();
74 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
75 rgb_init = true;
76 }
77
78 rgblight_task();
79#else
80 void matrix_scan_kb(void) {
81#endif
82 matrix_scan_user();
83 /* Nothing else for now. */
84 }
85
86 __attribute__((weak)) // overridable
87 void matrix_init_user(void) {
88
89 }
90
91 __attribute__((weak)) // overridable
92 void matrix_scan_user(void) {
93
94 }
diff --git a/keyboards/jj50/rules.mk b/keyboards/jj50/rules.mk
index b23b4becd..40473e6ec 100644
--- a/keyboards/jj50/rules.mk
+++ b/keyboards/jj50/rules.mk
@@ -1,19 +1,3 @@
1# Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com>
2# Modified 2018 Wayne Jones (WarmCatUK) <waynekjones@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# MCU name 1# MCU name
18MCU = atmega32a 2MCU = atmega32a
19 3
@@ -35,22 +19,18 @@ CONSOLE_ENABLE = no
35COMMAND_ENABLE = yes 19COMMAND_ENABLE = yes
36BACKLIGHT_ENABLE = yes 20BACKLIGHT_ENABLE = yes
37RGBLIGHT_ENABLE = yes 21RGBLIGHT_ENABLE = yes
38RGBLIGHT_CUSTOM_DRIVER = yes 22WS2812_DRIVER = i2c
39NKRO_ENABLE = no 23NKRO_ENABLE = no
40# Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work 24# Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work
41 25
42
43DISABLE_WS2812 = no
44
45KEY_LOCK_ENABLE = yes 26KEY_LOCK_ENABLE = yes
46# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE 27# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE
47SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend 28SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend
48 29
49
50#OPT_DEFS = -DDEBUG_LEVEL=0 30#OPT_DEFS = -DDEBUG_LEVEL=0
51 31
52# custom matrix setup 32# custom matrix setup
53CUSTOM_MATRIX = yes 33CUSTOM_MATRIX = yes
54SRC = matrix.c i2c.c backlight.c 34SRC = matrix.c backlight.c
55 35
56LAYOUTS = ortho_5x12 36LAYOUTS = ortho_5x12
diff --git a/keyboards/mechmini/v1/i2c.c b/keyboards/mechmini/v1/i2c.c
deleted file mode 100644
index c27f3e3d1..000000000
--- a/keyboards/mechmini/v1/i2c.c
+++ /dev/null
@@ -1,104 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include <avr/io.h>
19#include <util/twi.h>
20
21#include "i2c.h"
22
23void i2c_set_bitrate(uint16_t bitrate_khz) {
24 uint8_t bitrate_div = ((F_CPU / 1000l) / bitrate_khz);
25 if (bitrate_div >= 16) {
26 bitrate_div = (bitrate_div - 16) / 2;
27 }
28 TWBR = bitrate_div;
29}
30
31void i2c_init(void) {
32 // set pull-up resistors on I2C bus pins
33 PORTC |= 0b11;
34
35 i2c_set_bitrate(400);
36
37 // enable TWI (two-wire interface)
38 TWCR |= (1 << TWEN);
39
40 // enable TWI interrupt and slave address ACK
41 TWCR |= (1 << TWIE);
42 TWCR |= (1 << TWEA);
43}
44
45uint8_t i2c_start(uint8_t address) {
46 // reset TWI control register
47 TWCR = 0;
48
49 // begin transmission and wait for it to end
50 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
51 while (!(TWCR & (1<<TWINT)));
52
53 // check if the start condition was successfully transmitted
54 if ((TWSR & 0xF8) != TW_START) {
55 return 1;
56 }
57
58 // transmit address and wait
59 TWDR = address;
60 TWCR = (1<<TWINT) | (1<<TWEN);
61 while (!(TWCR & (1<<TWINT)));
62
63 // check if the device has acknowledged the READ / WRITE mode
64 uint8_t twst = TW_STATUS & 0xF8;
65 if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) {
66 return 1;
67 }
68
69 return 0;
70}
71
72void i2c_stop(void) {
73 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
74}
75
76uint8_t i2c_write(uint8_t data) {
77 TWDR = data;
78
79 // transmit data and wait
80 TWCR = (1<<TWINT) | (1<<TWEN);
81 while (!(TWCR & (1<<TWINT)));
82
83 if ((TWSR & 0xF8) != TW_MT_DATA_ACK) {
84 return 1;
85 }
86
87 return 0;
88}
89
90uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length) {
91 if (i2c_start(address)) {
92 return 1;
93 }
94
95 for (uint16_t i = 0; i < length; i++) {
96 if (i2c_write(data[i])) {
97 return 1;
98 }
99 }
100
101 i2c_stop();
102
103 return 0;
104}
diff --git a/keyboards/mechmini/v1/i2c.h b/keyboards/mechmini/v1/i2c.h
deleted file mode 100644
index 194a82cab..000000000
--- a/keyboards/mechmini/v1/i2c.h
+++ /dev/null
@@ -1,22 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3This program is free software: you can redistribute it and/or modify
4it under the terms of the GNU General Public License as published by
5the Free Software Foundation, either version 2 of the License, or
6(at your option) any later version.
7This program is distributed in the hope that it will be useful,
8but WITHOUT ANY WARRANTY; without even the implied warranty of
9MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10GNU General Public License for more details.
11You should have received a copy of the GNU General Public License
12along with this program. If not, see <http://www.gnu.org/licenses/>.
13*/
14
15#ifndef __I2C_H__
16#define __I2C_H__
17
18void i2c_init(void);
19void i2c_set_bitrate(uint16_t bitrate_khz);
20uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length);
21
22#endif
diff --git a/keyboards/mechmini/v1/rules.mk b/keyboards/mechmini/v1/rules.mk
index 3510ca6f3..ee023b43b 100644
--- a/keyboards/mechmini/v1/rules.mk
+++ b/keyboards/mechmini/v1/rules.mk
@@ -1,18 +1,3 @@
1# Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com>
2#
3# This program is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16# MCU name 1# MCU name
17MCU = atmega32a 2MCU = atmega32a
18 3
@@ -34,10 +19,10 @@ CONSOLE_ENABLE = yes
34COMMAND_ENABLE = yes 19COMMAND_ENABLE = yes
35BACKLIGHT_ENABLE = no 20BACKLIGHT_ENABLE = no
36RGBLIGHT_ENABLE = yes 21RGBLIGHT_ENABLE = yes
37RGBLIGHT_CUSTOM_DRIVER = yes 22WS2812_DRIVER = i2c
38 23
39OPT_DEFS = -DDEBUG_LEVEL=0 24OPT_DEFS = -DDEBUG_LEVEL=0
40 25
41# custom matrix setup 26# custom matrix setup
42CUSTOM_MATRIX = yes 27CUSTOM_MATRIX = yes
43SRC = matrix.c i2c.c 28SRC = matrix.c
diff --git a/keyboards/mechmini/v1/v1.c b/keyboards/mechmini/v1/v1.c
index 508d60c78..2c910d965 100644
--- a/keyboards/mechmini/v1/v1.c
+++ b/keyboards/mechmini/v1/v1.c
@@ -16,61 +16,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/ 16*/
17 17
18#include "v1.h" 18#include "v1.h"
19#include <avr/pgmspace.h>
20#include "action_layer.h"
21#include "i2c.h"
22#include "quantum.h"
23#include "rgblight.h"
24 19
25// for keyboard subdirectory level init functions 20void matrix_init_kb(void) { matrix_init_user(); }
26// @Override
27void matrix_init_kb(void) {
28 // call user level keymaps, if any
29 matrix_init_user();
30}
31 21
32#ifdef RGBLIGHT_ENABLE 22__attribute__ ((weak))
33extern rgblight_config_t rgblight_config; 23void matrix_init_user(void) {}
34 24
35// custom RGB driver 25void matrix_scan_kb(void) { matrix_scan_user(); }
36void rgblight_set(void) {
37 if (!rgblight_config.enable) {
38 for (uint8_t i=0; i<RGBLED_NUM; i++) {
39 led[i].r = 0;
40 led[i].g = 0;
41 led[i].b = 0;
42 }
43 }
44 26
45 i2c_init(); 27__attribute__ ((weak))
46 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM); 28void matrix_scan_user(void) {}
47}
48
49bool rgb_init = false;
50
51void matrix_scan_kb(void) {
52 // if LEDs were previously on before poweroff, turn them back on
53 if (rgb_init == false && rgblight_config.enable) {
54 i2c_init();
55 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
56 rgb_init = true;
57 }
58
59 rgblight_task();
60#else
61void matrix_scan_kb(void) {
62#endif
63 matrix_scan_user();
64 /* Nothing else for now. */
65}
66
67__attribute__((weak)) // overridable
68void matrix_init_user(void) {
69
70}
71
72
73__attribute__((weak)) // overridable
74void matrix_scan_user(void) {
75
76}
diff --git a/keyboards/mehkee96/i2c.c b/keyboards/mehkee96/i2c.c
deleted file mode 100644
index a4f952135..000000000
--- a/keyboards/mehkee96/i2c.c
+++ /dev/null
@@ -1,106 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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
25void 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
33void 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
47uint8_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
74void i2c_stop(void) {
75 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
76}
77
78uint8_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
92uint8_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}
diff --git a/keyboards/mehkee96/i2c.h b/keyboards/mehkee96/i2c.h
deleted file mode 100644
index 7ce50cdb5..000000000
--- a/keyboards/mehkee96/i2c.h
+++ /dev/null
@@ -1,27 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18// Please do not modify this file
19
20#ifndef __I2C_H__
21#define __I2C_H__
22
23void i2c_init(void);
24void i2c_set_bitrate(uint16_t bitrate_khz);
25uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length);
26
27#endif
diff --git a/keyboards/mehkee96/mehkee96.c b/keyboards/mehkee96/mehkee96.c
index 604fad803..46427ec93 100644
--- a/keyboards/mehkee96/mehkee96.c
+++ b/keyboards/mehkee96/mehkee96.c
@@ -16,66 +16,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/ 16*/
17 17
18#include "mehkee96.h" 18#include "mehkee96.h"
19#include "rgblight.h"
20 19
21#include <avr/pgmspace.h> 20void matrix_init_kb(void) { matrix_init_user(); }
22 21
23#include "action_layer.h" 22__attribute__ ((weak))
24#include "i2c.h" 23void matrix_init_user(void) {}
25#include "quantum.h"
26 24
27// for keyboard subdirectory level init functions 25void matrix_scan_kb(void) { matrix_scan_user(); }
28// @Override
29void matrix_init_kb(void) {
30 // call user level keymaps, if any
31 matrix_init_user();
32}
33 26
34#ifdef RGBLIGHT_ENABLE 27__attribute__ ((weak))
35extern rgblight_config_t rgblight_config; 28void matrix_scan_user(void) {}
36
37// custom RGB driver
38void rgblight_set(void) {
39 if (!rgblight_config.enable) {
40 for (uint8_t i=0; i<RGBLED_NUM; i++) {
41 led[i].r = 0;
42 led[i].g = 0;
43 led[i].b = 0;
44 }
45 }
46
47 i2c_init();
48 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
49}
50
51bool rgb_init = false;
52
53void matrix_scan_kb(void) {
54 // if LEDs were previously on before poweroff, turn them back on
55 if (rgb_init == false && rgblight_config.enable) {
56 i2c_init();
57 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
58 rgb_init = true;
59 }
60
61#ifdef RGBLIGHT_ANIMATION
62 rgblight_task();
63#endif
64
65#else
66void matrix_scan_kb(void) {
67#endif
68 matrix_scan_user();
69 /* Nothing else for now. */
70}
71
72__attribute__((weak)) // overridable
73void matrix_init_user(void) {
74
75}
76
77
78__attribute__((weak)) // overridable
79void matrix_scan_user(void) {
80
81}
diff --git a/keyboards/mehkee96/rules.mk b/keyboards/mehkee96/rules.mk
index 244dd2141..1cbd02948 100644
--- a/keyboards/mehkee96/rules.mk
+++ b/keyboards/mehkee96/rules.mk
@@ -21,10 +21,10 @@ CONSOLE_ENABLE = yes # Console for debug(+400)
21COMMAND_ENABLE = yes # Commands for debug and configuration 21COMMAND_ENABLE = yes # Commands for debug and configuration
22BACKLIGHT_ENABLE = no # Enable keyboard backlight functionality 22BACKLIGHT_ENABLE = no # Enable keyboard backlight functionality
23RGBLIGHT_ENABLE = yes 23RGBLIGHT_ENABLE = yes
24RGBLIGHT_CUSTOM_DRIVER = yes 24WS2812_DRIVER = i2c
25 25
26OPT_DEFS = -DDEBUG_LEVEL=0 26OPT_DEFS = -DDEBUG_LEVEL=0
27 27
28# custom matrix setup 28# custom matrix setup
29CUSTOM_MATRIX = yes 29CUSTOM_MATRIX = yes
30SRC = matrix.c i2c.c 30SRC = matrix.c
diff --git a/keyboards/mt40/i2c.c b/keyboards/mt40/i2c.c
deleted file mode 100644
index c27f3e3d1..000000000
--- a/keyboards/mt40/i2c.c
+++ /dev/null
@@ -1,104 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include <avr/io.h>
19#include <util/twi.h>
20
21#include "i2c.h"
22
23void i2c_set_bitrate(uint16_t bitrate_khz) {
24 uint8_t bitrate_div = ((F_CPU / 1000l) / bitrate_khz);
25 if (bitrate_div >= 16) {
26 bitrate_div = (bitrate_div - 16) / 2;
27 }
28 TWBR = bitrate_div;
29}
30
31void i2c_init(void) {
32 // set pull-up resistors on I2C bus pins
33 PORTC |= 0b11;
34
35 i2c_set_bitrate(400);
36
37 // enable TWI (two-wire interface)
38 TWCR |= (1 << TWEN);
39
40 // enable TWI interrupt and slave address ACK
41 TWCR |= (1 << TWIE);
42 TWCR |= (1 << TWEA);
43}
44
45uint8_t i2c_start(uint8_t address) {
46 // reset TWI control register
47 TWCR = 0;
48
49 // begin transmission and wait for it to end
50 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
51 while (!(TWCR & (1<<TWINT)));
52
53 // check if the start condition was successfully transmitted
54 if ((TWSR & 0xF8) != TW_START) {
55 return 1;
56 }
57
58 // transmit address and wait
59 TWDR = address;
60 TWCR = (1<<TWINT) | (1<<TWEN);
61 while (!(TWCR & (1<<TWINT)));
62
63 // check if the device has acknowledged the READ / WRITE mode
64 uint8_t twst = TW_STATUS & 0xF8;
65 if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) {
66 return 1;
67 }
68
69 return 0;
70}
71
72void i2c_stop(void) {
73 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
74}
75
76uint8_t i2c_write(uint8_t data) {
77 TWDR = data;
78
79 // transmit data and wait
80 TWCR = (1<<TWINT) | (1<<TWEN);
81 while (!(TWCR & (1<<TWINT)));
82
83 if ((TWSR & 0xF8) != TW_MT_DATA_ACK) {
84 return 1;
85 }
86
87 return 0;
88}
89
90uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length) {
91 if (i2c_start(address)) {
92 return 1;
93 }
94
95 for (uint16_t i = 0; i < length; i++) {
96 if (i2c_write(data[i])) {
97 return 1;
98 }
99 }
100
101 i2c_stop();
102
103 return 0;
104}
diff --git a/keyboards/mt40/i2c.h b/keyboards/mt40/i2c.h
deleted file mode 100644
index 27c9d3d05..000000000
--- a/keyboards/mt40/i2c.h
+++ /dev/null
@@ -1,25 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef __I2C_H__
19#define __I2C_H__
20
21void i2c_init(void);
22void i2c_set_bitrate(uint16_t bitrate_khz);
23uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length);
24
25#endif
diff --git a/keyboards/mt40/mt40.c b/keyboards/mt40/mt40.c
index 555689f87..dd079e7d8 100644
--- a/keyboards/mt40/mt40.c
+++ b/keyboards/mt40/mt40.c
@@ -13,32 +13,5 @@
13 * You should have received a copy of the GNU General Public License 13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */ 15 */
16#include "mt40.h"
17#include "rgblight.h"
18
19#include <avr/pgmspace.h>
20
21#include "action_layer.h"
22#include "i2c.h"
23#include "quantum.h"
24
25extern rgblight_config_t rgblight_config;
26 16
27void rgblight_set(void) { 17#include "mt40.h"
28 if (!rgblight_config.enable) {
29 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
30 led[i].r = 0;
31 led[i].g = 0;
32 led[i].b = 0;
33 }
34 }
35
36 i2c_init();
37 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
38}
39
40__attribute__ ((weak))
41void matrix_scan_kb(void) {
42 rgblight_task();
43 matrix_init_user();
44}
diff --git a/keyboards/mt40/rules.mk b/keyboards/mt40/rules.mk
index 19410bc6e..0cbf90c67 100644
--- a/keyboards/mt40/rules.mk
+++ b/keyboards/mt40/rules.mk
@@ -24,14 +24,14 @@ AUDIO_ENABLE ?= no # Audio output on port C6
24UNICODE_ENABLE ?= no # Unicode 24UNICODE_ENABLE ?= no # Unicode
25BLUETOOTH_ENABLE ?= no # Enable Bluetooth with the Adafruit EZ-Key HID 25BLUETOOTH_ENABLE ?= no # Enable Bluetooth with the Adafruit EZ-Key HID
26RGBLIGHT_ENABLE ?= yes # Enable WS2812 RGB underlight. 26RGBLIGHT_ENABLE ?= yes # Enable WS2812 RGB underlight.
27RGBLIGHT_CUSTOM_DRIVER = yes 27WS2812_DRIVER = i2c
28TAP_DANCE_ENABLE = no 28TAP_DANCE_ENABLE = no
29 29
30OPT_DEFS = -DDEBUG_LEVEL=0 30OPT_DEFS = -DDEBUG_LEVEL=0
31 31
32# custom matrix setup 32# custom matrix setup
33CUSTOM_MATRIX = yes 33CUSTOM_MATRIX = yes
34SRC = matrix.c i2c.c 34SRC = matrix.c
35 35
36LAYOUTS = planck_mit 36LAYOUTS = planck_mit
37LAYOUTS_HAS_RGB = no 37LAYOUTS_HAS_RGB = no
diff --git a/keyboards/percent/canoe/canoe.c b/keyboards/percent/canoe/canoe.c
index a7427e152..e59b0dd7b 100644
--- a/keyboards/percent/canoe/canoe.c
+++ b/keyboards/percent/canoe/canoe.c
@@ -16,81 +16,43 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/ 16*/
17 17
18#include "canoe.h" 18#include "canoe.h"
19#ifdef BACKLIGHT_ENABLE 19
20#include "backlight.h" 20void matrix_init_kb(void) { matrix_init_user(); }
21#endif 21
22#ifdef RGBLIGHT_ENABLE 22__attribute__ ((weak))
23#include "i2c.h" 23void matrix_init_user(void) {}
24#include "rgblight.h" 24
25#endif 25void matrix_scan_kb(void) { matrix_scan_user(); }
26
27__attribute__ ((weak))
28void matrix_scan_user(void) {}
26 29
27#ifdef BACKLIGHT_ENABLE 30#ifdef BACKLIGHT_ENABLE
28void backlight_set(uint8_t level) { 31void backlight_set(uint8_t level) {
29 if (level == 0) { 32 if (level == 0) {
30 // Turn out the lights 33 // Turn out the lights
31 PORTD &= ~(1<<0 | 1<<1 | 1<<4 | 1<<6); 34 writePinLow(D0);
35 writePinLow(D1);
36 writePinLow(D4);
37 writePinLow(D6);
32 } else { 38 } else {
33 // Turn on the lights 39 // Turn on the lights
34 PORTD |= (1<<0 | 1<<1 | 1<<4 | 1<<6); 40 writePinHigh(D0);
41 writePinHigh(D1);
42 writePinHigh(D4);
43 writePinHigh(D6);
35 } 44 }
36} 45}
37 46
38void backlight_init_ports(void) { 47void backlight_init_ports(void) {
39 DDRD |= (1<<0 | 1<<1 | 1<<4 | 1<<6); 48 setPinOutput(D0);
40 PORTD &= ~(1<<0 | 1<<1 | 1<<4 | 1<<6); 49 setPinOutput(D1);
41} 50 setPinOutput(D4);
42 51 setPinOutput(D6);
43#endif 52
44 53 writePinLow(D0);
45// for keyboard subdirectory level init functions 54 writePinLow(D1);
46// @Override 55 writePinLow(D4);
47void matrix_init_kb(void) { 56 writePinLow(D6);
48 // call user level keymaps, if any
49 matrix_init_user();
50}
51
52#ifdef RGBLIGHT_ENABLE
53extern rgblight_config_t rgblight_config;
54
55// custom RGB driver
56void rgblight_set(void) {
57 if (!rgblight_config.enable) {
58 for (uint8_t i=0; i<RGBLED_NUM; i++) {
59 led[i].r = 0;
60 led[i].g = 0;
61 led[i].b = 0;
62 }
63 }
64
65 i2c_init();
66 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
67} 57}
68
69bool rgb_init = false;
70
71void matrix_scan_kb(void) {
72 // if LEDs were previously on before poweroff, turn them back on
73 if (rgb_init == false && rgblight_config.enable) {
74 i2c_init();
75 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
76 rgb_init = true;
77 }
78
79 rgblight_task();
80#else
81void matrix_scan_kb(void) {
82#endif 58#endif
83 matrix_scan_user();
84 /* Nothing else for now. */
85}
86
87__attribute__((weak)) // overridable
88void matrix_init_user(void) {
89
90}
91
92
93__attribute__((weak)) // overridable
94void matrix_scan_user(void) {
95
96}
diff --git a/keyboards/percent/canoe/i2c.c b/keyboards/percent/canoe/i2c.c
deleted file mode 100644
index a4f952135..000000000
--- a/keyboards/percent/canoe/i2c.c
+++ /dev/null
@@ -1,106 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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
25void 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
33void 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
47uint8_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
74void i2c_stop(void) {
75 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
76}
77
78uint8_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
92uint8_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}
diff --git a/keyboards/percent/canoe/i2c.h b/keyboards/percent/canoe/i2c.h
deleted file mode 100644
index 93a69c94d..000000000
--- a/keyboards/percent/canoe/i2c.h
+++ /dev/null
@@ -1,27 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18// Please do not modify this file
19
20#ifndef __I2C_H__
21#define __I2C_H__
22
23void i2c_init(void);
24void i2c_set_bitrate(uint16_t bitrate_khz);
25uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length);
26
27#endif
diff --git a/keyboards/percent/canoe/rules.mk b/keyboards/percent/canoe/rules.mk
index e3269b94d..bb14269a2 100644
--- a/keyboards/percent/canoe/rules.mk
+++ b/keyboards/percent/canoe/rules.mk
@@ -1,18 +1,3 @@
1# Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com>
2#
3# This program is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16# MCU name 1# MCU name
17MCU = atmega32a 2MCU = atmega32a
18 3
@@ -34,12 +19,12 @@ CONSOLE_ENABLE = yes
34COMMAND_ENABLE = yes 19COMMAND_ENABLE = yes
35BACKLIGHT_ENABLE = no 20BACKLIGHT_ENABLE = no
36RGBLIGHT_ENABLE = yes 21RGBLIGHT_ENABLE = yes
37RGBLIGHT_CUSTOM_DRIVER = yes 22WS2812_DRIVER = i2c
38 23
39OPT_DEFS = -DDEBUG_LEVEL=0 24OPT_DEFS = -DDEBUG_LEVEL=0
40 25
41# custom matrix setup 26# custom matrix setup
42CUSTOM_MATRIX = yes 27CUSTOM_MATRIX = yes
43SRC = matrix.c i2c.c 28SRC = matrix.c
44 29
45LAYOUTS = 65_ansi_blocker \ No newline at end of file 30LAYOUTS = 65_ansi_blocker
diff --git a/keyboards/percent/skog/i2c.c b/keyboards/percent/skog/i2c.c
deleted file mode 100644
index a4f952135..000000000
--- a/keyboards/percent/skog/i2c.c
+++ /dev/null
@@ -1,106 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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
25void 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
33void 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
47uint8_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
74void i2c_stop(void) {
75 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
76}
77
78uint8_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
92uint8_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}
diff --git a/keyboards/percent/skog/i2c.h b/keyboards/percent/skog/i2c.h
deleted file mode 100644
index ada8cc7bf..000000000
--- a/keyboards/percent/skog/i2c.h
+++ /dev/null
@@ -1,25 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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
22void i2c_init(void);
23void i2c_set_bitrate(uint16_t bitrate_khz);
24uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length);
25
diff --git a/keyboards/percent/skog/rules.mk b/keyboards/percent/skog/rules.mk
index b8439906b..2b0c1154d 100644
--- a/keyboards/percent/skog/rules.mk
+++ b/keyboards/percent/skog/rules.mk
@@ -1,18 +1,3 @@
1# Copyright 2018 Jumail Mundekkat / MxBlue
2#
3# This program is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16# MCU name 1# MCU name
17MCU = atmega32a 2MCU = atmega32a
18 3
@@ -34,9 +19,9 @@ CONSOLE_ENABLE = yes
34COMMAND_ENABLE = yes 19COMMAND_ENABLE = yes
35BACKLIGHT_ENABLE = yes 20BACKLIGHT_ENABLE = yes
36RGBLIGHT_ENABLE = yes 21RGBLIGHT_ENABLE = yes
22WS2812_DRIVER = i2c
37 23
38BACKLIGHT_CUSTOM_DRIVER = yes 24BACKLIGHT_CUSTOM_DRIVER = yes
39RGBLIGHT_CUSTOM_DRIVER = yes
40 25
41OPT_DEFS = -DDEBUG_LEVEL=0 26OPT_DEFS = -DDEBUG_LEVEL=0
42 27
@@ -45,4 +30,4 @@ SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend
45 30
46# custom matrix setup 31# custom matrix setup
47CUSTOM_MATRIX = yes 32CUSTOM_MATRIX = yes
48SRC = matrix.c i2c.c backlight.c 33SRC = matrix.c backlight.c
diff --git a/keyboards/percent/skog/skog.c b/keyboards/percent/skog/skog.c
index 3d1eb0e1a..8678d483a 100644
--- a/keyboards/percent/skog/skog.c
+++ b/keyboards/percent/skog/skog.c
@@ -19,23 +19,18 @@ ps2avrGB support code by Kenneth A. (bminiex/.[ch])
19 19
20#include "skog.h" 20#include "skog.h"
21 21
22#include "rgblight.h" 22#include "backlight.h"
23#include "backlight_custom.h"
23 24
24#include <avr/pgmspace.h> 25void matrix_init_kb(void) { matrix_init_user(); }
25 26
26#include "action_layer.h" 27__attribute__ ((weak))
27#include "i2c.h" 28void matrix_init_user(void) {}
28#include "quantum.h"
29 29
30#include "backlight.h" 30void matrix_scan_kb(void) { matrix_scan_user(); }
31#include "backlight_custom.h"
32 31
33// for keyboard subdirectory level init functions 32__attribute__ ((weak))
34// @Override 33void matrix_scan_user(void) {}
35void matrix_init_kb(void) {
36 // call user level keymaps, if any
37 matrix_init_user();
38}
39 34
40#ifdef BACKLIGHT_ENABLE 35#ifdef BACKLIGHT_ENABLE
41/// Overrides functions in `quantum.c` 36/// Overrides functions in `quantum.c`
@@ -51,48 +46,3 @@ void backlight_set(uint8_t level) {
51 b_led_set(level); 46 b_led_set(level);
52} 47}
53#endif 48#endif
54
55#ifdef RGBLIGHT_ENABLE
56extern rgblight_config_t rgblight_config;
57
58// custom RGB driver
59void rgblight_set(void) {
60 if (!rgblight_config.enable) {
61 for (uint8_t i=0; i<RGBLED_NUM; i++) {
62 led[i].r = 0;
63 led[i].g = 0;
64 led[i].b = 0;
65 }
66 }
67
68 i2c_init();
69 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
70}
71
72bool rgb_init = false;
73
74void matrix_scan_kb(void) {
75 // if LEDs were previously on before poweroff, turn them back on
76 if (rgb_init == false && rgblight_config.enable) {
77 i2c_init();
78 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
79 rgb_init = true;
80 }
81
82 rgblight_task();
83#else
84void matrix_scan_kb(void) {
85#endif
86 matrix_scan_user();
87 /* Nothing else for now. */
88}
89
90__attribute__((weak)) // overridable
91void matrix_init_user(void) {
92
93}
94
95__attribute__((weak)) // overridable
96void matrix_scan_user(void) {
97
98}
diff --git a/keyboards/winkeyless/bmini/bmini.c b/keyboards/winkeyless/bmini/bmini.c
index 35800358c..87a31d052 100644
--- a/keyboards/winkeyless/bmini/bmini.c
+++ b/keyboards/winkeyless/bmini/bmini.c
@@ -16,30 +16,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/ 16*/
17 17
18#include "bmini.h" 18#include "bmini.h"
19#include "rgblight.h"
20
21#include <avr/pgmspace.h>
22
23#include "action_layer.h"
24#include "i2c.h"
25#include "quantum.h"
26
27extern rgblight_config_t rgblight_config;
28
29void rgblight_set(void) {
30 if (!rgblight_config.enable) {
31 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
32 led[i].r = 0;
33 led[i].g = 0;
34 led[i].b = 0;
35 }
36 }
37
38 i2c_init();
39 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
40}
41 19
42__attribute__ ((weak)) 20__attribute__ ((weak))
43void matrix_scan_user(void) { 21void matrix_scan_user(void) {}
44 rgblight_task();
45}
diff --git a/keyboards/winkeyless/bmini/i2c.c b/keyboards/winkeyless/bmini/i2c.c
deleted file mode 100644
index a4f952135..000000000
--- a/keyboards/winkeyless/bmini/i2c.c
+++ /dev/null
@@ -1,106 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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
25void 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
33void 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
47uint8_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
74void i2c_stop(void) {
75 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
76}
77
78uint8_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
92uint8_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}
diff --git a/keyboards/winkeyless/bmini/i2c.h b/keyboards/winkeyless/bmini/i2c.h
deleted file mode 100644
index 93a69c94d..000000000
--- a/keyboards/winkeyless/bmini/i2c.h
+++ /dev/null
@@ -1,27 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18// Please do not modify this file
19
20#ifndef __I2C_H__
21#define __I2C_H__
22
23void i2c_init(void);
24void i2c_set_bitrate(uint16_t bitrate_khz);
25uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length);
26
27#endif
diff --git a/keyboards/winkeyless/bmini/rules.mk b/keyboards/winkeyless/bmini/rules.mk
index 8cfc81056..7822bd830 100644
--- a/keyboards/winkeyless/bmini/rules.mk
+++ b/keyboards/winkeyless/bmini/rules.mk
@@ -1,18 +1,3 @@
1# Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com>
2#
3# This program is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16# MCU name 1# MCU name
17MCU = atmega32a 2MCU = atmega32a
18 3
@@ -34,10 +19,10 @@ CONSOLE_ENABLE = yes
34COMMAND_ENABLE = yes 19COMMAND_ENABLE = yes
35BACKLIGHT_ENABLE = no 20BACKLIGHT_ENABLE = no
36RGBLIGHT_ENABLE = yes 21RGBLIGHT_ENABLE = yes
37RGBLIGHT_CUSTOM_DRIVER = yes 22WS2812_DRIVER = i2c
38 23
39OPT_DEFS = -DDEBUG_LEVEL=0 24OPT_DEFS = -DDEBUG_LEVEL=0
40 25
41# custom matrix setup 26# custom matrix setup
42CUSTOM_MATRIX = yes 27CUSTOM_MATRIX = yes
43SRC = matrix.c i2c.c 28SRC = matrix.c
diff --git a/keyboards/winkeyless/bminiex/bminiex.c b/keyboards/winkeyless/bminiex/bminiex.c
index d9b05aba5..11315b921 100644
--- a/keyboards/winkeyless/bminiex/bminiex.c
+++ b/keyboards/winkeyless/bminiex/bminiex.c
@@ -16,24 +16,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/ 16*/
17 17
18#include "bminiex.h" 18#include "bminiex.h"
19#include "rgblight.h"
20
21#include <avr/pgmspace.h>
22
23#include "action_layer.h"
24#include "i2c.h"
25#include "quantum.h"
26 19
27#include "backlight.h" 20#include "backlight.h"
28#include "backlight_custom.h" 21#include "backlight_custom.h"
29 22
30// for keyboard subdirectory level init functions
31// @Override
32void matrix_init_kb(void) {
33 // call user level keymaps, if any
34 matrix_init_user();
35}
36
37#ifdef BACKLIGHT_ENABLE 23#ifdef BACKLIGHT_ENABLE
38/// Overrides functions in `quantum.c` 24/// Overrides functions in `quantum.c`
39void backlight_init_ports(void) { 25void backlight_init_ports(void) {
@@ -48,50 +34,3 @@ void backlight_set(uint8_t level) {
48 b_led_set(level); 34 b_led_set(level);
49} 35}
50#endif 36#endif
51
52#ifdef RGBLIGHT_ENABLE
53extern rgblight_config_t rgblight_config;
54
55// custom RGB driver
56void rgblight_set(void) {
57 if (!rgblight_config.enable) {
58 for (uint8_t i=0; i<RGBLED_NUM; i++) {
59 led[i].r = 0;
60 led[i].g = 0;
61 led[i].b = 0;
62 }
63 }
64
65 i2c_init();
66 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
67}
68
69bool rgb_init = false;
70
71void matrix_scan_kb(void) {
72 // if LEDs were previously on before poweroff, turn them back on
73 if (rgb_init == false && rgblight_config.enable) {
74 i2c_init();
75 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
76 rgb_init = true;
77 }
78
79 rgblight_task();
80#else
81void matrix_scan_kb(void) {
82#endif
83 matrix_scan_user();
84 /* Nothing else for now. */
85}
86
87__attribute__((weak)) // overridable
88void matrix_init_user(void) {
89
90}
91
92__attribute__((weak)) // overridable
93void matrix_scan_user(void) {
94
95}
96
97
diff --git a/keyboards/winkeyless/bminiex/i2c.c b/keyboards/winkeyless/bminiex/i2c.c
deleted file mode 100644
index a4f952135..000000000
--- a/keyboards/winkeyless/bminiex/i2c.c
+++ /dev/null
@@ -1,106 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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
25void 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
33void 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
47uint8_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
74void i2c_stop(void) {
75 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
76}
77
78uint8_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
92uint8_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}
diff --git a/keyboards/winkeyless/bminiex/i2c.h b/keyboards/winkeyless/bminiex/i2c.h
deleted file mode 100644
index ada8cc7bf..000000000
--- a/keyboards/winkeyless/bminiex/i2c.h
+++ /dev/null
@@ -1,25 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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
22void i2c_init(void);
23void i2c_set_bitrate(uint16_t bitrate_khz);
24uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length);
25
diff --git a/keyboards/winkeyless/bminiex/rules.mk b/keyboards/winkeyless/bminiex/rules.mk
index 24e434879..39a77bda1 100644
--- a/keyboards/winkeyless/bminiex/rules.mk
+++ b/keyboards/winkeyless/bminiex/rules.mk
@@ -1,18 +1,3 @@
1# Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com>
2#
3# This program is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16# MCU name 1# MCU name
17MCU = atmega32a 2MCU = atmega32a
18 3
@@ -36,7 +21,7 @@ COMMAND_ENABLE = no
36BACKLIGHT_ENABLE = yes 21BACKLIGHT_ENABLE = yes
37BACKLIGHT_CUSTOM_DRIVER = yes 22BACKLIGHT_CUSTOM_DRIVER = yes
38RGBLIGHT_ENABLE = yes 23RGBLIGHT_ENABLE = yes
39RGBLIGHT_CUSTOM_DRIVER = yes 24WS2812_DRIVER = i2c
40TAP_DANCE_ENABLE = no 25TAP_DANCE_ENABLE = no
41 26
42# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE 27# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE
@@ -46,4 +31,4 @@ OPT_DEFS = -DDEBUG_LEVEL=0
46 31
47# custom matrix setup 32# custom matrix setup
48CUSTOM_MATRIX = yes 33CUSTOM_MATRIX = yes
49SRC = matrix.c i2c.c backlight.c 34SRC = matrix.c backlight.c
diff --git a/keyboards/ymd75/i2c.c b/keyboards/ymd75/i2c.c
deleted file mode 100644
index c27f3e3d1..000000000
--- a/keyboards/ymd75/i2c.c
+++ /dev/null
@@ -1,104 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include <avr/io.h>
19#include <util/twi.h>
20
21#include "i2c.h"
22
23void i2c_set_bitrate(uint16_t bitrate_khz) {
24 uint8_t bitrate_div = ((F_CPU / 1000l) / bitrate_khz);
25 if (bitrate_div >= 16) {
26 bitrate_div = (bitrate_div - 16) / 2;
27 }
28 TWBR = bitrate_div;
29}
30
31void i2c_init(void) {
32 // set pull-up resistors on I2C bus pins
33 PORTC |= 0b11;
34
35 i2c_set_bitrate(400);
36
37 // enable TWI (two-wire interface)
38 TWCR |= (1 << TWEN);
39
40 // enable TWI interrupt and slave address ACK
41 TWCR |= (1 << TWIE);
42 TWCR |= (1 << TWEA);
43}
44
45uint8_t i2c_start(uint8_t address) {
46 // reset TWI control register
47 TWCR = 0;
48
49 // begin transmission and wait for it to end
50 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
51 while (!(TWCR & (1<<TWINT)));
52
53 // check if the start condition was successfully transmitted
54 if ((TWSR & 0xF8) != TW_START) {
55 return 1;
56 }
57
58 // transmit address and wait
59 TWDR = address;
60 TWCR = (1<<TWINT) | (1<<TWEN);
61 while (!(TWCR & (1<<TWINT)));
62
63 // check if the device has acknowledged the READ / WRITE mode
64 uint8_t twst = TW_STATUS & 0xF8;
65 if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) {
66 return 1;
67 }
68
69 return 0;
70}
71
72void i2c_stop(void) {
73 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
74}
75
76uint8_t i2c_write(uint8_t data) {
77 TWDR = data;
78
79 // transmit data and wait
80 TWCR = (1<<TWINT) | (1<<TWEN);
81 while (!(TWCR & (1<<TWINT)));
82
83 if ((TWSR & 0xF8) != TW_MT_DATA_ACK) {
84 return 1;
85 }
86
87 return 0;
88}
89
90uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length) {
91 if (i2c_start(address)) {
92 return 1;
93 }
94
95 for (uint16_t i = 0; i < length; i++) {
96 if (i2c_write(data[i])) {
97 return 1;
98 }
99 }
100
101 i2c_stop();
102
103 return 0;
104}
diff --git a/keyboards/ymd75/i2c.h b/keyboards/ymd75/i2c.h
deleted file mode 100644
index 27c9d3d05..000000000
--- a/keyboards/ymd75/i2c.h
+++ /dev/null
@@ -1,25 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef __I2C_H__
19#define __I2C_H__
20
21void i2c_init(void);
22void i2c_set_bitrate(uint16_t bitrate_khz);
23uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length);
24
25#endif
diff --git a/keyboards/ymd75/rules.mk b/keyboards/ymd75/rules.mk
index 7bee20173..0c1c9110c 100644
--- a/keyboards/ymd75/rules.mk
+++ b/keyboards/ymd75/rules.mk
@@ -1,19 +1,3 @@
1# Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com>
2# Modified 2018 Wayne Jones (WarmCatUK) <waynekjones@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# MCU name 1# MCU name
18MCU = atmega32a 2MCU = atmega32a
19 3
@@ -35,7 +19,7 @@ CONSOLE_ENABLE = no
35COMMAND_ENABLE = yes 19COMMAND_ENABLE = yes
36BACKLIGHT_ENABLE = yes 20BACKLIGHT_ENABLE = yes
37RGBLIGHT_ENABLE = yes 21RGBLIGHT_ENABLE = yes
38RGBLIGHT_CUSTOM_DRIVER = yes 22WS2812_DRIVER = i2c
39NKRO_ENABLE = no 23NKRO_ENABLE = no
40# Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work 24# Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work
41 25
@@ -51,4 +35,4 @@ SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend
51 35
52# custom matrix setup 36# custom matrix setup
53CUSTOM_MATRIX = yes 37CUSTOM_MATRIX = yes
54SRC = matrix.c i2c.c backlight.c 38SRC = matrix.c backlight.c
diff --git a/keyboards/ymd75/ymd75.c b/keyboards/ymd75/ymd75.c
index 2259dfa5f..74f7c6a49 100644
--- a/keyboards/ymd75/ymd75.c
+++ b/keyboards/ymd75/ymd75.c
@@ -16,23 +16,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/ 16*/
17 17
18#include "ymd75.h" 18#include "ymd75.h"
19//#include "rgblight.h"
20
21#include <avr/pgmspace.h>
22
23#include "action_layer.h"
24#include "i2c.h"
25#include "quantum.h"
26 19
27#include "backlight.h" 20#include "backlight.h"
28#include "backlight_custom.h" 21#include "backlight_custom.h"
29 22
30// for keyboard subdirectory level init functions 23void matrix_init_kb(void) { matrix_init_user(); }
31// @Override 24
32void matrix_init_kb(void) { 25__attribute__ ((weak))
33 // call user level keymaps, if any 26void matrix_init_user(void) {}
34 matrix_init_user(); 27
35} 28void matrix_scan_kb(void) { matrix_scan_user(); }
29
30__attribute__ ((weak))
31void matrix_scan_user(void) {}
36 32
37#ifdef BACKLIGHT_ENABLE 33#ifdef BACKLIGHT_ENABLE
38/// Overrides functions in `quantum.c` 34/// Overrides functions in `quantum.c`
@@ -48,48 +44,3 @@ void backlight_set(uint8_t level) {
48 b_led_set(level); 44 b_led_set(level);
49} 45}
50#endif 46#endif
51
52#ifdef RGBLIGHT_ENABLE
53extern rgblight_config_t rgblight_config;
54
55// custom RGB driver
56void rgblight_set(void) {
57 if (!rgblight_config.enable) {
58 for (uint8_t i=0; i<RGBLED_NUM; i++) {
59 led[i].r = 0;
60 led[i].g = 0;
61 led[i].b = 0;
62 }
63 }
64
65 i2c_init();
66 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
67}
68
69bool rgb_init = false;
70
71void matrix_scan_kb(void) {
72 // if LEDs were previously on before poweroff, turn them back on
73 if (rgb_init == false && rgblight_config.enable) {
74 i2c_init();
75 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
76 rgb_init = true;
77 }
78
79 rgblight_task();
80#else
81 void matrix_scan_kb(void) {
82#endif
83 matrix_scan_user();
84 /* Nothing else for now. */
85 }
86
87 __attribute__((weak)) // overridable
88 void matrix_init_user(void) {
89
90 }
91
92 __attribute__((weak)) // overridable
93 void matrix_scan_user(void) {
94
95 }
diff --git a/keyboards/ymd96/i2c.c b/keyboards/ymd96/i2c.c
deleted file mode 100644
index c27f3e3d1..000000000
--- a/keyboards/ymd96/i2c.c
+++ /dev/null
@@ -1,104 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include <avr/io.h>
19#include <util/twi.h>
20
21#include "i2c.h"
22
23void i2c_set_bitrate(uint16_t bitrate_khz) {
24 uint8_t bitrate_div = ((F_CPU / 1000l) / bitrate_khz);
25 if (bitrate_div >= 16) {
26 bitrate_div = (bitrate_div - 16) / 2;
27 }
28 TWBR = bitrate_div;
29}
30
31void i2c_init(void) {
32 // set pull-up resistors on I2C bus pins
33 PORTC |= 0b11;
34
35 i2c_set_bitrate(400);
36
37 // enable TWI (two-wire interface)
38 TWCR |= (1 << TWEN);
39
40 // enable TWI interrupt and slave address ACK
41 TWCR |= (1 << TWIE);
42 TWCR |= (1 << TWEA);
43}
44
45uint8_t i2c_start(uint8_t address) {
46 // reset TWI control register
47 TWCR = 0;
48
49 // begin transmission and wait for it to end
50 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
51 while (!(TWCR & (1<<TWINT)));
52
53 // check if the start condition was successfully transmitted
54 if ((TWSR & 0xF8) != TW_START) {
55 return 1;
56 }
57
58 // transmit address and wait
59 TWDR = address;
60 TWCR = (1<<TWINT) | (1<<TWEN);
61 while (!(TWCR & (1<<TWINT)));
62
63 // check if the device has acknowledged the READ / WRITE mode
64 uint8_t twst = TW_STATUS & 0xF8;
65 if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) {
66 return 1;
67 }
68
69 return 0;
70}
71
72void i2c_stop(void) {
73 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
74}
75
76uint8_t i2c_write(uint8_t data) {
77 TWDR = data;
78
79 // transmit data and wait
80 TWCR = (1<<TWINT) | (1<<TWEN);
81 while (!(TWCR & (1<<TWINT)));
82
83 if ((TWSR & 0xF8) != TW_MT_DATA_ACK) {
84 return 1;
85 }
86
87 return 0;
88}
89
90uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length) {
91 if (i2c_start(address)) {
92 return 1;
93 }
94
95 for (uint16_t i = 0; i < length; i++) {
96 if (i2c_write(data[i])) {
97 return 1;
98 }
99 }
100
101 i2c_stop();
102
103 return 0;
104}
diff --git a/keyboards/ymd96/i2c.h b/keyboards/ymd96/i2c.h
deleted file mode 100644
index 27c9d3d05..000000000
--- a/keyboards/ymd96/i2c.h
+++ /dev/null
@@ -1,25 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef __I2C_H__
19#define __I2C_H__
20
21void i2c_init(void);
22void i2c_set_bitrate(uint16_t bitrate_khz);
23uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length);
24
25#endif
diff --git a/keyboards/ymd96/rules.mk b/keyboards/ymd96/rules.mk
index 96441e1b3..19d9fd81a 100644
--- a/keyboards/ymd96/rules.mk
+++ b/keyboards/ymd96/rules.mk
@@ -1,18 +1,3 @@
1# Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com>
2#
3# This program is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16# MCU name 1# MCU name
17MCU = atmega32a 2MCU = atmega32a
18 3
@@ -37,7 +22,7 @@ BACKLIGHT_ENABLE = yes
37BACKLIGHT_CUSTOM_DRIVER = yes 22BACKLIGHT_CUSTOM_DRIVER = yes
38 23
39RGBLIGHT_ENABLE = yes 24RGBLIGHT_ENABLE = yes
40RGBLIGHT_CUSTOM_DRIVER = yes 25WS2812_DRIVER = i2c
41 26
42KEY_LOCK_ENABLE = yes 27KEY_LOCK_ENABLE = yes
43 28
@@ -48,4 +33,4 @@ OPT_DEFS = -DDEBUG_LEVEL=0
48 33
49# custom matrix setup 34# custom matrix setup
50CUSTOM_MATRIX = yes 35CUSTOM_MATRIX = yes
51SRC = matrix.c i2c.c backlight.c 36SRC = matrix.c backlight.c
diff --git a/keyboards/ymd96/ymd96.c b/keyboards/ymd96/ymd96.c
index b0bf6128f..546a4c6e3 100644
--- a/keyboards/ymd96/ymd96.c
+++ b/keyboards/ymd96/ymd96.c
@@ -18,22 +18,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18
19#include "ymd96.h" 19#include "ymd96.h"
20 20
21#include <avr/pgmspace.h> 21#include "backlight.h"
22#include "backlight_custom.h"
22 23
23#include "action_layer.h" 24void matrix_init_kb(void) { matrix_init_user(); }
24#include "quantum.h"
25 25
26#include "i2c.h" 26__attribute__ ((weak))
27void matrix_init_user(void) {}
27 28
28#include "backlight.h" 29void matrix_scan_kb(void) { matrix_scan_user(); }
29#include "backlight_custom.h"
30 30
31// for keyboard subdirectory level init functions 31__attribute__ ((weak))
32// @Override 32void matrix_scan_user(void) {}
33void matrix_init_kb(void) {
34 // call user level keymaps, if any
35 matrix_init_user();
36}
37 33
38#ifdef BACKLIGHT_ENABLE 34#ifdef BACKLIGHT_ENABLE
39/// Overrides functions in `quantum.c` 35/// Overrides functions in `quantum.c`
@@ -49,49 +45,3 @@ void backlight_set(uint8_t level) {
49 b_led_set(level); 45 b_led_set(level);
50} 46}
51#endif 47#endif
52
53#ifdef RGBLIGHT_ENABLE
54extern rgblight_config_t rgblight_config;
55
56// custom RGB driver
57void rgblight_set(void) {
58 if (!rgblight_config.enable) {
59 for (uint8_t i=0; i<RGBLED_NUM; i++) {
60 led[i].r = 0;
61 led[i].g = 0;
62 led[i].b = 0;
63 }
64 }
65
66 i2c_init();
67 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
68}
69
70bool rgb_init = false;
71
72void matrix_scan_kb(void) {
73 // if LEDs were previously on before poweroff, turn them back on
74 if (rgb_init == false && rgblight_config.enable) {
75 i2c_init();
76 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
77 rgb_init = true;
78 }
79
80 rgblight_task();
81#else
82void matrix_scan_kb(void) {
83#endif
84 matrix_scan_user();
85 /* Nothing else for now. */
86}
87
88__attribute__((weak)) // overridable
89void matrix_init_user(void) {
90
91}
92
93
94__attribute__((weak)) // overridable
95void matrix_scan_user(void) {
96
97}
diff --git a/keyboards/ymdk_np21/i2c.c b/keyboards/ymdk_np21/i2c.c
deleted file mode 100644
index c27f3e3d1..000000000
--- a/keyboards/ymdk_np21/i2c.c
+++ /dev/null
@@ -1,104 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include <avr/io.h>
19#include <util/twi.h>
20
21#include "i2c.h"
22
23void i2c_set_bitrate(uint16_t bitrate_khz) {
24 uint8_t bitrate_div = ((F_CPU / 1000l) / bitrate_khz);
25 if (bitrate_div >= 16) {
26 bitrate_div = (bitrate_div - 16) / 2;
27 }
28 TWBR = bitrate_div;
29}
30
31void i2c_init(void) {
32 // set pull-up resistors on I2C bus pins
33 PORTC |= 0b11;
34
35 i2c_set_bitrate(400);
36
37 // enable TWI (two-wire interface)
38 TWCR |= (1 << TWEN);
39
40 // enable TWI interrupt and slave address ACK
41 TWCR |= (1 << TWIE);
42 TWCR |= (1 << TWEA);
43}
44
45uint8_t i2c_start(uint8_t address) {
46 // reset TWI control register
47 TWCR = 0;
48
49 // begin transmission and wait for it to end
50 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
51 while (!(TWCR & (1<<TWINT)));
52
53 // check if the start condition was successfully transmitted
54 if ((TWSR & 0xF8) != TW_START) {
55 return 1;
56 }
57
58 // transmit address and wait
59 TWDR = address;
60 TWCR = (1<<TWINT) | (1<<TWEN);
61 while (!(TWCR & (1<<TWINT)));
62
63 // check if the device has acknowledged the READ / WRITE mode
64 uint8_t twst = TW_STATUS & 0xF8;
65 if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) {
66 return 1;
67 }
68
69 return 0;
70}
71
72void i2c_stop(void) {
73 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
74}
75
76uint8_t i2c_write(uint8_t data) {
77 TWDR = data;
78
79 // transmit data and wait
80 TWCR = (1<<TWINT) | (1<<TWEN);
81 while (!(TWCR & (1<<TWINT)));
82
83 if ((TWSR & 0xF8) != TW_MT_DATA_ACK) {
84 return 1;
85 }
86
87 return 0;
88}
89
90uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length) {
91 if (i2c_start(address)) {
92 return 1;
93 }
94
95 for (uint16_t i = 0; i < length; i++) {
96 if (i2c_write(data[i])) {
97 return 1;
98 }
99 }
100
101 i2c_stop();
102
103 return 0;
104}
diff --git a/keyboards/ymdk_np21/i2c.h b/keyboards/ymdk_np21/i2c.h
deleted file mode 100644
index 27c9d3d05..000000000
--- a/keyboards/ymdk_np21/i2c.h
+++ /dev/null
@@ -1,25 +0,0 @@
1/*
2Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef __I2C_H__
19#define __I2C_H__
20
21void i2c_init(void);
22void i2c_set_bitrate(uint16_t bitrate_khz);
23uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length);
24
25#endif
diff --git a/keyboards/ymdk_np21/rules.mk b/keyboards/ymdk_np21/rules.mk
index 634b9c69c..2bab3043e 100644
--- a/keyboards/ymdk_np21/rules.mk
+++ b/keyboards/ymdk_np21/rules.mk
@@ -1,18 +1,3 @@
1# Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com>
2#
3# This program is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16# MCU name 1# MCU name
17MCU = atmega32a 2MCU = atmega32a
18 3
@@ -37,8 +22,7 @@ BACKLIGHT_ENABLE = yes
37BACKLIGHT_CUSTOM_DRIVER = yes 22BACKLIGHT_CUSTOM_DRIVER = yes
38 23
39RGBLIGHT_ENABLE = yes 24RGBLIGHT_ENABLE = yes
40RGBLIGHT_CUSTOM_DRIVER = yes 25WS2812_DRIVER = i2c
41# DISABLE_WS2812 = no
42 26
43KEY_LOCK_ENABLE = yes 27KEY_LOCK_ENABLE = yes
44 28
@@ -49,4 +33,4 @@ OPT_DEFS = -DDEBUG_LEVEL=0
49 33
50# custom matrix setup 34# custom matrix setup
51CUSTOM_MATRIX = yes 35CUSTOM_MATRIX = yes
52SRC = matrix.c i2c.c backlight.c 36SRC = matrix.c backlight.c
diff --git a/keyboards/ymdk_np21/ymdk_np21.c b/keyboards/ymdk_np21/ymdk_np21.c
index c1b11b404..f2b3d46da 100644
--- a/keyboards/ymdk_np21/ymdk_np21.c
+++ b/keyboards/ymdk_np21/ymdk_np21.c
@@ -18,25 +18,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18
19#include "ymdk_np21.h" 19#include "ymdk_np21.h"
20 20
21#include <avr/pgmspace.h>
22
23#include "action_layer.h"
24#include "quantum.h"
25
26#include "i2c.h"
27
28#include "backlight.h" 21#include "backlight.h"
29#include "backlight_custom.h" 22#include "backlight_custom.h"
30 23
31extern rgblight_config_t rgblight_config;
32
33// for keyboard subdirectory level init functions
34// @Override
35void matrix_init_kb(void) {
36 // call user level keymaps, if any
37 matrix_init_user();
38}
39
40#ifdef BACKLIGHT_ENABLE 24#ifdef BACKLIGHT_ENABLE
41/// Overrides functions in `quantum.c` 25/// Overrides functions in `quantum.c`
42void backlight_init_ports(void) { 26void backlight_init_ports(void) {
@@ -51,32 +35,3 @@ void backlight_set(uint8_t level) {
51 b_led_set(level); 35 b_led_set(level);
52} 36}
53#endif 37#endif
54
55// custom RGB driver
56void rgblight_set(void) {
57 if (!rgblight_config.enable) {
58 for (uint8_t i=0; i<RGBLED_NUM; i++) {
59 led[i].r = 0;
60 led[i].g = 0;
61 led[i].b = 0;
62 }
63 }
64
65 i2c_init();
66 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
67}
68
69bool rgb_init = false;
70void matrix_scan_kb(void) {
71 // if LEDs were previously on before poweroff, turn them back on
72 if (rgb_init == false && rgblight_config.enable) {
73 i2c_init();
74 i2c_send(0xb0, (uint8_t*)led, 3 * RGBLED_NUM);
75 rgb_init = true;
76 }
77
78 rgblight_task();
79 /* Nothing else for now. */
80
81 matrix_scan_user();
82}