aboutsummaryrefslogtreecommitdiff
path: root/quantum
diff options
context:
space:
mode:
authorJames Churchill <pelrun@gmail.com>2019-04-06 02:20:43 +1000
committerMechMerlin <30334081+mechmerlin@users.noreply.github.com>2019-04-05 09:20:43 -0700
commit4c1760883e2e0ed710348f02bc791786ed0c1b04 (patch)
treec7045c751b600af2f73f4dc19a0ce5fe3de20cd2 /quantum
parentdad66cad40131bbc5b7c85bdf107c664a721ba07 (diff)
downloadqmk_firmware-4c1760883e2e0ed710348f02bc791786ed0c1b04.tar.gz
qmk_firmware-4c1760883e2e0ed710348f02bc791786ed0c1b04.zip
Update ps2avrgb template to use standard matrix/i2c code (#4957)
* Update ps2avrgb template to use standard matrix/i2c code * Default to an 8x11 matrix in the ps2avrgb template
Diffstat (limited to 'quantum')
-rw-r--r--quantum/template/avr/template.c30
-rw-r--r--quantum/template/ps2avrgb/config.h13
-rw-r--r--quantum/template/ps2avrgb/i2c.c106
-rw-r--r--quantum/template/ps2avrgb/i2c.h27
-rw-r--r--quantum/template/ps2avrgb/matrix.c112
-rw-r--r--quantum/template/ps2avrgb/rules.mk4
-rw-r--r--quantum/template/ps2avrgb/template.c77
7 files changed, 99 insertions, 270 deletions
diff --git a/quantum/template/avr/template.c b/quantum/template/avr/template.c
index 1e4ce26cd..86dc69abc 100644
--- a/quantum/template/avr/template.c
+++ b/quantum/template/avr/template.c
@@ -15,29 +15,37 @@
15 */ 15 */
16#include "%KEYBOARD%.h" 16#include "%KEYBOARD%.h"
17 17
18// Optional override functions below.
19// You can leave any or all of these undefined.
20// These are only required if you want to perform custom actions.
21
22/*
23
18void matrix_init_kb(void) { 24void matrix_init_kb(void) {
19 // put your keyboard start-up code here 25 // put your keyboard start-up code here
20 // runs once when the firmware starts up 26 // runs once when the firmware starts up
21 27
22 matrix_init_user(); 28 matrix_init_user();
23} 29}
24 30
25void matrix_scan_kb(void) { 31void matrix_scan_kb(void) {
26 // put your looping keyboard code here 32 // put your looping keyboard code here
27 // runs every cycle (a lot) 33 // runs every cycle (a lot)
28 34
29 matrix_scan_user(); 35 matrix_scan_user();
30} 36}
31 37
32bool process_record_kb(uint16_t keycode, keyrecord_t *record) { 38bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
33 // put your per-action keyboard code here 39 // put your per-action keyboard code here
34 // runs for every action, just before processing by the firmware 40 // runs for every action, just before processing by the firmware
35 41
36 return process_record_user(keycode, record); 42 return process_record_user(keycode, record);
37} 43}
38 44
39void led_set_kb(uint8_t usb_led) { 45void led_set_kb(uint8_t usb_led) {
40 // put your keyboard LED indicator (ex: Caps Lock LED) toggling code here 46 // put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
41 47
42 led_set_user(usb_led); 48 led_set_user(usb_led);
43} 49}
50
51*/
diff --git a/quantum/template/ps2avrgb/config.h b/quantum/template/ps2avrgb/config.h
index 01cdf932e..8d9a993cf 100644
--- a/quantum/template/ps2avrgb/config.h
+++ b/quantum/template/ps2avrgb/config.h
@@ -21,17 +21,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
21 21
22#define VENDOR_ID 0x20A0 22#define VENDOR_ID 0x20A0
23#define PRODUCT_ID 0x422D 23#define PRODUCT_ID 0x422D
24#define DEVICE_VER 0x0001
24#define MANUFACTURER You 25#define MANUFACTURER You
25#define PRODUCT %KEYBOARD% 26#define PRODUCT %KEYBOARD%
27#define DESCRIPTION A custom keyboard
26 28
27#define RGBLED_NUM 16 29#define RGBLED_NUM 16
28 30
29#define MATRIX_ROWS 2 31#define MATRIX_ROWS 8
30#define MATRIX_COLS 3 32#define MATRIX_COLS 11
31 33
32#define MATRIX_ROW_PINS { D0, D5 } 34#define MATRIX_ROW_PINS { B0, B1, B2, B3, B4, B5, B6, B7 }
33#define MATRIX_COL_PINS { F1, F0, B0 } 35#define MATRIX_COL_PINS { A0, A1, A2, A3, A4, A5, A6, A7, C7, C6, C5 }
34#define UNUSED_PINS 36// #define MATRIX_COL_PINS { A0, A1, A2, A3, A4, A5, A6, A7, C7, C6, C5, C4, C3, C2, C1, C0, D7 }
37#define UNUSED_PINS {}
35 38
36#define DIODE_DIRECTION COL2ROW 39#define DIODE_DIRECTION COL2ROW
37#define DEBOUNCING_DELAY 5 40#define DEBOUNCING_DELAY 5
diff --git a/quantum/template/ps2avrgb/i2c.c b/quantum/template/ps2avrgb/i2c.c
deleted file mode 100644
index e8c4455ad..000000000
--- a/quantum/template/ps2avrgb/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/quantum/template/ps2avrgb/i2c.h b/quantum/template/ps2avrgb/i2c.h
deleted file mode 100644
index 7ce50cdb5..000000000
--- a/quantum/template/ps2avrgb/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/quantum/template/ps2avrgb/matrix.c b/quantum/template/ps2avrgb/matrix.c
deleted file mode 100644
index 245813dfd..000000000
--- a/quantum/template/ps2avrgb/matrix.c
+++ /dev/null
@@ -1,112 +0,0 @@
1/*
2Copyright 2017 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/delay.h>
20
21#include "matrix.h"
22
23#ifndef DEBOUNCE
24# define DEBOUNCE 5
25#endif
26
27static uint8_t debouncing = DEBOUNCE;
28
29static matrix_row_t matrix[MATRIX_ROWS];
30static matrix_row_t matrix_debouncing[MATRIX_ROWS];
31
32void matrix_set_row_status(uint8_t row);
33uint8_t bit_reverse(uint8_t x);
34
35void matrix_init(void) {
36 // all outputs for rows high
37 DDRB = 0xFF;
38 PORTB = 0xFF;
39 // all inputs for columns
40 DDRA = 0x00;
41 DDRC &= ~(0x111111<<2);
42 DDRD &= ~(1<<PIND7);
43 // all columns are pulled-up
44 PORTA = 0xFF;
45 PORTC |= (0b111111<<2);
46 PORTD |= (1<<PIND7);
47
48 // initialize matrix state: all keys off
49 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
50 matrix[row] = 0x00;
51 matrix_debouncing[row] = 0x00;
52 }
53
54 matrix_init_quantum();
55}
56
57uint8_t matrix_scan(void) {
58 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
59 matrix_set_row_status(row);
60 _delay_us(5);
61
62 matrix_row_t cols = (
63 // cols 0..7, PORTA 0 -> 7
64 (~PINA) & 0xFF
65 ) | (
66 // cols 8..13, PORTC 7 -> 0
67 bit_reverse((~PINC) & 0xFF) << 8
68 ) | (
69 // col 14, PORTD 7
70 ((~PIND) & (1 << PIND7)) << 7
71 );
72
73 if (matrix_debouncing[row] != cols) {
74 matrix_debouncing[row] = cols;
75 debouncing = DEBOUNCE;
76 }
77 }
78
79 if (debouncing) {
80 if (--debouncing) {
81 _delay_ms(1);
82 } else {
83 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
84 matrix[i] = matrix_debouncing[i];
85 }
86 }
87 }
88
89 matrix_scan_quantum();
90
91 return 1;
92}
93
94// declarations
95void matrix_set_row_status(uint8_t row) {
96 DDRB = (1 << row);
97 PORTB = ~(1 << row);
98}
99
100uint8_t bit_reverse(uint8_t x) {
101 x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
102 x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
103 x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
104 return x;
105}
106
107inline matrix_row_t matrix_get_row(uint8_t row) {
108 return matrix[row];
109}
110
111void matrix_print(void) {
112}
diff --git a/quantum/template/ps2avrgb/rules.mk b/quantum/template/ps2avrgb/rules.mk
index bcd7dff99..191a13844 100644
--- a/quantum/template/ps2avrgb/rules.mk
+++ b/quantum/template/ps2avrgb/rules.mk
@@ -42,9 +42,7 @@ RGBLIGHT_CUSTOM_DRIVER = yes
42 42
43OPT_DEFS = -DDEBUG_LEVEL=0 43OPT_DEFS = -DDEBUG_LEVEL=0
44 44
45# custom matrix setup 45SRC += i2c_master.c
46CUSTOM_MATRIX = yes
47SRC = matrix.c i2c.c
48 46
49# programming options 47# programming options
50PROGRAM_CMD = ./util/atmega32a_program.py $(TARGET).hex 48PROGRAM_CMD = ./util/atmega32a_program.py $(TARGET).hex
diff --git a/quantum/template/ps2avrgb/template.c b/quantum/template/ps2avrgb/template.c
index 08156c562..3f920de48 100644
--- a/quantum/template/ps2avrgb/template.c
+++ b/quantum/template/ps2avrgb/template.c
@@ -13,13 +13,78 @@
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
16#include "%KEYBOARD%.h" 17#include "%KEYBOARD%.h"
17 18
18#include <avr/pgmspace.h> 19#ifdef RGBLIGHT_ENABLE
19#include "action_layer.h" 20
20#include "i2c.h" 21#include <string.h>
21#include "quantum.h" 22#include "i2c_master.h"
23#include "rgblight.h"
24
25extern rgblight_config_t rgblight_config;
26
27void matrix_init_kb(void) {
28 i2c_init();
29 // call user level keymaps, if any
30 matrix_init_user();
31}
32
33// custom RGB driver
34void rgblight_set(void) {
35 if (!rgblight_config.enable) {
36 memset(led, 0, 3 * RGBLED_NUM);
37 }
38
39 i2c_transmit(0xb0, (uint8_t*)led, 3 * RGBLED_NUM, 100);
40}
41
42bool rgb_init = false;
43
44void matrix_scan_kb(void) {
45 // if LEDs were previously on before poweroff, turn them back on
46 if (rgb_init == false && rgblight_config.enable) {
47 i2c_transmit(0xb0, (uint8_t*)led, 3 * RGBLED_NUM, 100);
48 rgb_init = true;
49 }
50
51 rgblight_task();
52 matrix_scan_user();
53}
54
55#endif
22 56
23__attribute__ ((weak)) 57// Optional override functions below.
24void matrix_scan_user(void) { 58// You can leave any or all of these undefined.
59// These are only required if you want to perform custom actions.
60
61/*
62
63void matrix_init_kb(void) {
64 // put your keyboard start-up code here
65 // runs once when the firmware starts up
66
67 matrix_init_user();
25} 68}
69
70void matrix_scan_kb(void) {
71 // put your looping keyboard code here
72 // runs every cycle (a lot)
73
74 matrix_scan_user();
75}
76
77bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
78 // put your per-action keyboard code here
79 // runs for every action, just before processing by the firmware
80
81 return process_record_user(keycode, record);
82}
83
84void led_set_kb(uint8_t usb_led) {
85 // put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
86
87 led_set_user(usb_led);
88}
89
90*/