aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormarksard <38324387+marksard@users.noreply.github.com>2018-09-04 08:34:16 +0900
committerDrashna Jaelre <drashna@live.com>2018-09-03 16:34:16 -0700
commit35efcc9f398a1a2493b482dd1bd7c859f93ef450 (patch)
tree004b44e13067b8793ad9523340ce0539977bf8b9
parentfa1ee47cf2293d06693b86e8dd188d9fbc9338c4 (diff)
downloadqmk_firmware-35efcc9f398a1a2493b482dd1bd7c859f93ef450.tar.gz
qmk_firmware-35efcc9f398a1a2493b482dd1bd7c859f93ef450.zip
Keyboard: Improvement of crkbd communication functions (based on helix-keyboard) (#3798)
* improvement of crkbd communication functions (based on helix-keyboard) * Removed unnecessary code. * Changed read restriction from #define to #pragma once. * Changed from sizeof to defined size. * moved lib folder to crkbdroot. removed warning of ws2812.d
-rw-r--r--keyboards/crkbd/config.h13
-rw-r--r--keyboards/crkbd/crkbd.h5
-rw-r--r--keyboards/crkbd/i2c.c2
-rw-r--r--keyboards/crkbd/i2c.h7
-rw-r--r--keyboards/crkbd/keymaps/default/keymap.c2
-rw-r--r--keyboards/crkbd/keymaps/default/rules.mk14
-rw-r--r--keyboards/crkbd/keymaps/like_jis/keymap.c2
-rw-r--r--keyboards/crkbd/keymaps/like_jis/rules.mk14
-rw-r--r--keyboards/crkbd/lib/host_led_state_reader.c (renamed from keyboards/crkbd/keymaps/lib/host_led_state_reader.c)0
-rw-r--r--keyboards/crkbd/lib/keylogger.c (renamed from keyboards/crkbd/keymaps/lib/keylogger.c)0
-rw-r--r--keyboards/crkbd/lib/layer_state_reader.c (renamed from keyboards/crkbd/keymaps/lib/layer_state_reader.c)0
-rw-r--r--keyboards/crkbd/lib/logo_reader.c (renamed from keyboards/crkbd/keymaps/lib/logo_reader.c)0
-rw-r--r--keyboards/crkbd/lib/mode_icon_reader.c (renamed from keyboards/crkbd/keymaps/lib/mode_icon_reader.c)0
-rw-r--r--keyboards/crkbd/lib/rgb_state_reader.c (renamed from keyboards/crkbd/keymaps/lib/rgb_state_reader.c)0
-rw-r--r--keyboards/crkbd/lib/timelogger.c (renamed from keyboards/crkbd/keymaps/lib/timelogger.c)0
-rw-r--r--keyboards/crkbd/pro_micro.h6
-rw-r--r--keyboards/crkbd/rev1/config.h8
-rw-r--r--keyboards/crkbd/rev1/matrix.c59
-rw-r--r--keyboards/crkbd/rev1/rev1.h5
-rw-r--r--keyboards/crkbd/rev1/rules.mk5
-rw-r--r--keyboards/crkbd/rev1/serial_config.h10
-rw-r--r--keyboards/crkbd/rev1/serial_config_simpleapi.h5
-rw-r--r--keyboards/crkbd/rev1/split_scomm.c73
-rw-r--r--keyboards/crkbd/rev1/split_scomm.h21
-rw-r--r--keyboards/crkbd/rev1/split_util.c (renamed from keyboards/crkbd/split_util.c)3
-rw-r--r--keyboards/crkbd/rev1/split_util.h (renamed from keyboards/crkbd/split_util.h)5
-rw-r--r--keyboards/crkbd/rules.mk10
-rw-r--r--keyboards/crkbd/serial.c452
-rw-r--r--keyboards/crkbd/serial.h83
-rw-r--r--keyboards/crkbd/ssd1306.c1
-rw-r--r--keyboards/crkbd/ssd1306.h8
31 files changed, 580 insertions, 233 deletions
diff --git a/keyboards/crkbd/config.h b/keyboards/crkbd/config.h
index c910d8f24..64fee7545 100644
--- a/keyboards/crkbd/config.h
+++ b/keyboards/crkbd/config.h
@@ -16,9 +16,16 @@ You should have received a copy of the GNU General Public License
16along with this program. If not, see <http://www.gnu.org/licenses/>. 16along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/ 17*/
18 18
19#ifndef CONFIG_H 19#pragma once
20#define CONFIG_H
21 20
22#include "config_common.h" 21#include "config_common.h"
22#include <serial_config.h>
23 23
24#endif 24#ifdef USE_Link_Time_Optimization
25 // LTO has issues with macros (action_get_macro) and "functions" (fn_actions),
26 // so just disable them
27 #define NO_ACTION_MACRO
28 #define NO_ACTION_FUNCTION
29
30 #define DISABLE_LEADER
31#endif // USE_Link_Time_Optimization
diff --git a/keyboards/crkbd/crkbd.h b/keyboards/crkbd/crkbd.h
index 889bcb9ae..73f2a3f07 100644
--- a/keyboards/crkbd/crkbd.h
+++ b/keyboards/crkbd/crkbd.h
@@ -1,8 +1,5 @@
1#ifndef CRKBD_H 1#pragma once
2#define CRKBD_H
3 2
4#ifdef KEYBOARD_crkbd_rev1 3#ifdef KEYBOARD_crkbd_rev1
5 #include "rev1.h" 4 #include "rev1.h"
6#endif 5#endif
7
8#endif
diff --git a/keyboards/crkbd/i2c.c b/keyboards/crkbd/i2c.c
index 084c890c4..4bee5c639 100644
--- a/keyboards/crkbd/i2c.c
+++ b/keyboards/crkbd/i2c.c
@@ -34,7 +34,7 @@ void i2c_delay(void) {
34 // _delay_us(100); 34 // _delay_us(100);
35} 35}
36 36
37// Setup twi to run at 100kHz 37// Setup twi to run at 100kHz or 400kHz (see ./i2c.h SCL_CLOCK)
38void i2c_master_init(void) { 38void i2c_master_init(void) {
39 // no prescaler 39 // no prescaler
40 TWSR = 0; 40 TWSR = 0;
diff --git a/keyboards/crkbd/i2c.h b/keyboards/crkbd/i2c.h
index c15b6bc50..710662c7a 100644
--- a/keyboards/crkbd/i2c.h
+++ b/keyboards/crkbd/i2c.h
@@ -1,5 +1,4 @@
1#ifndef I2C_H 1#pragma once
2#define I2C_H
3 2
4#include <stdint.h> 3#include <stdint.h>
5 4
@@ -15,7 +14,7 @@
15 14
16#define SLAVE_BUFFER_SIZE 0x10 15#define SLAVE_BUFFER_SIZE 0x10
17 16
18// i2c SCL clock frequency 17// i2c SCL clock frequency 400kHz
19#define SCL_CLOCK 400000L 18#define SCL_CLOCK 400000L
20 19
21extern volatile uint8_t i2c_slave_buffer[SLAVE_BUFFER_SIZE]; 20extern volatile uint8_t i2c_slave_buffer[SLAVE_BUFFER_SIZE];
@@ -45,5 +44,3 @@ extern unsigned char i2c_readNak(void);
45extern unsigned char i2c_read(unsigned char ack); 44extern unsigned char i2c_read(unsigned char ack);
46 45
47#define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak(); 46#define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak();
48
49#endif
diff --git a/keyboards/crkbd/keymaps/default/keymap.c b/keyboards/crkbd/keymaps/default/keymap.c
index 87661d345..e92fbdebf 100644
--- a/keyboards/crkbd/keymaps/default/keymap.c
+++ b/keyboards/crkbd/keymaps/default/keymap.c
@@ -5,7 +5,6 @@
5 #include "split_util.h" 5 #include "split_util.h"
6#endif 6#endif
7#ifdef SSD1306OLED 7#ifdef SSD1306OLED
8 #include "LUFA/Drivers/Peripheral/TWI.h"
9 #include "ssd1306.h" 8 #include "ssd1306.h"
10#endif 9#endif
11 10
@@ -130,7 +129,6 @@ void matrix_init_user(void) {
130 #endif 129 #endif
131 //SSD1306 OLED init, make sure to add #define SSD1306OLED in config.h 130 //SSD1306 OLED init, make sure to add #define SSD1306OLED in config.h
132 #ifdef SSD1306OLED 131 #ifdef SSD1306OLED
133 TWI_Init(TWI_BIT_PRESCALE_1, TWI_BITLENGTH_FROM_FREQ(1, 800000));
134 iota_gfx_init(!has_usb()); // turns on the display 132 iota_gfx_init(!has_usb()); // turns on the display
135 #endif 133 #endif
136} 134}
diff --git a/keyboards/crkbd/keymaps/default/rules.mk b/keyboards/crkbd/keymaps/default/rules.mk
index 6570e2f5c..0edf1181f 100644
--- a/keyboards/crkbd/keymaps/default/rules.mk
+++ b/keyboards/crkbd/keymaps/default/rules.mk
@@ -21,10 +21,10 @@ SWAP_HANDS_ENABLE = no # Enable one-hand typing
21SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend 21SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend
22 22
23# If you want to change the display of OLED, you need to change here 23# If you want to change the display of OLED, you need to change here
24SRC += ../lib/rgb_state_reader.c \ 24SRC += ./lib/rgb_state_reader.c \
25 ../lib/layer_state_reader.c \ 25 ./lib/layer_state_reader.c \
26 ../lib/logo_reader.c \ 26 ./lib/logo_reader.c \
27 ../lib/keylogger.c \ 27 ./lib/keylogger.c \
28 # ../lib/mode_icon_reader.c \ 28 # ./lib/mode_icon_reader.c \
29 # ../lib/host_led_state_reader.c \ 29 # ./lib/host_led_state_reader.c \
30 # ../lib/timelogger.c \ 30 # ./lib/timelogger.c \
diff --git a/keyboards/crkbd/keymaps/like_jis/keymap.c b/keyboards/crkbd/keymaps/like_jis/keymap.c
index 31b5230b1..90e5b7ec1 100644
--- a/keyboards/crkbd/keymaps/like_jis/keymap.c
+++ b/keyboards/crkbd/keymaps/like_jis/keymap.c
@@ -5,7 +5,6 @@
5 #include "split_util.h" 5 #include "split_util.h"
6#endif 6#endif
7#ifdef SSD1306OLED 7#ifdef SSD1306OLED
8 #include "LUFA/Drivers/Peripheral/TWI.h"
9 #include "ssd1306.h" 8 #include "ssd1306.h"
10#endif 9#endif
11 10
@@ -124,7 +123,6 @@ void matrix_init_user(void) {
124 #endif 123 #endif
125 //SSD1306 OLED init, make sure to add #define SSD1306OLED in config.h 124 //SSD1306 OLED init, make sure to add #define SSD1306OLED in config.h
126 #ifdef SSD1306OLED 125 #ifdef SSD1306OLED
127 TWI_Init(TWI_BIT_PRESCALE_1, TWI_BITLENGTH_FROM_FREQ(1, 800000));
128 iota_gfx_init(!has_usb()); // turns on the display 126 iota_gfx_init(!has_usb()); // turns on the display
129 #endif 127 #endif
130} 128}
diff --git a/keyboards/crkbd/keymaps/like_jis/rules.mk b/keyboards/crkbd/keymaps/like_jis/rules.mk
index 6570e2f5c..0edf1181f 100644
--- a/keyboards/crkbd/keymaps/like_jis/rules.mk
+++ b/keyboards/crkbd/keymaps/like_jis/rules.mk
@@ -21,10 +21,10 @@ SWAP_HANDS_ENABLE = no # Enable one-hand typing
21SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend 21SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend
22 22
23# If you want to change the display of OLED, you need to change here 23# If you want to change the display of OLED, you need to change here
24SRC += ../lib/rgb_state_reader.c \ 24SRC += ./lib/rgb_state_reader.c \
25 ../lib/layer_state_reader.c \ 25 ./lib/layer_state_reader.c \
26 ../lib/logo_reader.c \ 26 ./lib/logo_reader.c \
27 ../lib/keylogger.c \ 27 ./lib/keylogger.c \
28 # ../lib/mode_icon_reader.c \ 28 # ./lib/mode_icon_reader.c \
29 # ../lib/host_led_state_reader.c \ 29 # ./lib/host_led_state_reader.c \
30 # ../lib/timelogger.c \ 30 # ./lib/timelogger.c \
diff --git a/keyboards/crkbd/keymaps/lib/host_led_state_reader.c b/keyboards/crkbd/lib/host_led_state_reader.c
index 41ac55dc2..41ac55dc2 100644
--- a/keyboards/crkbd/keymaps/lib/host_led_state_reader.c
+++ b/keyboards/crkbd/lib/host_led_state_reader.c
diff --git a/keyboards/crkbd/keymaps/lib/keylogger.c b/keyboards/crkbd/lib/keylogger.c
index 8f2a8ce3c..8f2a8ce3c 100644
--- a/keyboards/crkbd/keymaps/lib/keylogger.c
+++ b/keyboards/crkbd/lib/keylogger.c
diff --git a/keyboards/crkbd/keymaps/lib/layer_state_reader.c b/keyboards/crkbd/lib/layer_state_reader.c
index eddb71337..eddb71337 100644
--- a/keyboards/crkbd/keymaps/lib/layer_state_reader.c
+++ b/keyboards/crkbd/lib/layer_state_reader.c
diff --git a/keyboards/crkbd/keymaps/lib/logo_reader.c b/keyboards/crkbd/lib/logo_reader.c
index 1bc1503a6..1bc1503a6 100644
--- a/keyboards/crkbd/keymaps/lib/logo_reader.c
+++ b/keyboards/crkbd/lib/logo_reader.c
diff --git a/keyboards/crkbd/keymaps/lib/mode_icon_reader.c b/keyboards/crkbd/lib/mode_icon_reader.c
index 04c226506..04c226506 100644
--- a/keyboards/crkbd/keymaps/lib/mode_icon_reader.c
+++ b/keyboards/crkbd/lib/mode_icon_reader.c
diff --git a/keyboards/crkbd/keymaps/lib/rgb_state_reader.c b/keyboards/crkbd/lib/rgb_state_reader.c
index e0efe2e52..e0efe2e52 100644
--- a/keyboards/crkbd/keymaps/lib/rgb_state_reader.c
+++ b/keyboards/crkbd/lib/rgb_state_reader.c
diff --git a/keyboards/crkbd/keymaps/lib/timelogger.c b/keyboards/crkbd/lib/timelogger.c
index 69828a3a0..69828a3a0 100644
--- a/keyboards/crkbd/keymaps/lib/timelogger.c
+++ b/keyboards/crkbd/lib/timelogger.c
diff --git a/keyboards/crkbd/pro_micro.h b/keyboards/crkbd/pro_micro.h
index f9e7ed75d..366633372 100644
--- a/keyboards/crkbd/pro_micro.h
+++ b/keyboards/crkbd/pro_micro.h
@@ -21,9 +21,7 @@
21 21
22 $Id: wiring.h 249 2007-02-03 16:52:51Z mellis $ 22 $Id: wiring.h 249 2007-02-03 16:52:51Z mellis $
23*/ 23*/
24 24#pragma once
25#ifndef Pins_Arduino_h
26#define Pins_Arduino_h
27 25
28#include <avr/pgmspace.h> 26#include <avr/pgmspace.h>
29 27
@@ -358,5 +356,3 @@ const uint8_t PROGMEM analog_pin_to_channel_PGM[] = {
358#define SERIAL_PORT_USBVIRTUAL Serial 356#define SERIAL_PORT_USBVIRTUAL Serial
359#define SERIAL_PORT_HARDWARE Serial1 357#define SERIAL_PORT_HARDWARE Serial1
360#define SERIAL_PORT_HARDWARE_OPEN Serial1 358#define SERIAL_PORT_HARDWARE_OPEN Serial1
361
362#endif /* Pins_Arduino_h */
diff --git a/keyboards/crkbd/rev1/config.h b/keyboards/crkbd/rev1/config.h
index 6321136a7..efce13a49 100644
--- a/keyboards/crkbd/rev1/config.h
+++ b/keyboards/crkbd/rev1/config.h
@@ -16,10 +16,7 @@ You should have received a copy of the GNU General Public License
16along with this program. If not, see <http://www.gnu.org/licenses/>. 16along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/ 17*/
18 18
19#ifndef REV1_CONFIG_H 19#pragma once
20#define REV1_CONFIG_H
21
22#include "../config.h"
23 20
24/* USB Device descriptor parameter */ 21/* USB Device descriptor parameter */
25#define VENDOR_ID 0xFEED 22#define VENDOR_ID 0xFEED
@@ -82,6 +79,3 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
82//#define NO_ACTION_ONESHOT 79//#define NO_ACTION_ONESHOT
83//#define NO_ACTION_MACRO 80//#define NO_ACTION_MACRO
84//#define NO_ACTION_FUNCTION 81//#define NO_ACTION_FUNCTION
85
86
87#endif
diff --git a/keyboards/crkbd/rev1/matrix.c b/keyboards/crkbd/rev1/matrix.c
index 117ff8d37..718cc5744 100644
--- a/keyboards/crkbd/rev1/matrix.c
+++ b/keyboards/crkbd/rev1/matrix.c
@@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */ 20 */
21#include <stdint.h> 21#include <stdint.h>
22#include <stdbool.h> 22#include <stdbool.h>
23#include <string.h>
23#include <avr/io.h> 24#include <avr/io.h>
24#include <avr/wdt.h> 25#include <avr/wdt.h>
25#include <avr/interrupt.h> 26#include <avr/interrupt.h>
@@ -30,12 +31,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
30#include "matrix.h" 31#include "matrix.h"
31#include "split_util.h" 32#include "split_util.h"
32#include "pro_micro.h" 33#include "pro_micro.h"
33#include "config.h"
34 34
35#ifdef USE_MATRIX_I2C 35#ifdef USE_MATRIX_I2C
36# include "i2c.h" 36# include "i2c.h"
37#else // USE_SERIAL 37#else // USE_SERIAL
38# include "serial.h" 38# include "split_scomm.h"
39#endif 39#endif
40 40
41#ifndef DEBOUNCE 41#ifndef DEBOUNCE
@@ -103,6 +103,8 @@ void matrix_init(void)
103 init_cols(); 103 init_cols();
104 104
105 TX_RX_LED_INIT; 105 TX_RX_LED_INIT;
106 TXLED0;
107 RXLED0;
106 108
107 // initialize matrix state: all keys off 109 // initialize matrix state: all keys off
108 for (uint8_t i=0; i < MATRIX_ROWS; i++) { 110 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
@@ -179,17 +181,20 @@ i2c_error: // the cable is disconnceted, or something else went wrong
179 181
180#else // USE_SERIAL 182#else // USE_SERIAL
181 183
182int serial_transaction(void) { 184int serial_transaction(int master_changed) {
183 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; 185 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
186#ifdef SERIAL_USE_MULTI_TRANSACTION
187 int ret=serial_update_buffers(master_changed);
188#else
184 int ret=serial_update_buffers(); 189 int ret=serial_update_buffers();
190#endif
185 if (ret ) { 191 if (ret ) {
186 if(ret==2)RXLED1; 192 if(ret==2) RXLED1;
187 return 1; 193 return 1;
188 } 194 }
189RXLED0; 195 RXLED0;
190 for (int i = 0; i < ROWS_PER_HAND; ++i) { 196 memcpy(&matrix[slaveOffset],
191 matrix[slaveOffset+i] = serial_slave_buffer[i]; 197 (void *)serial_slave_buffer, SERIAL_SLAVE_BUFFER_LENGTH);
192 }
193 return 0; 198 return 0;
194} 199}
195#endif 200#endif
@@ -200,19 +205,9 @@ uint8_t matrix_scan(void)
200 matrix_master_scan(); 205 matrix_master_scan();
201 }else{ 206 }else{
202 matrix_slave_scan(); 207 matrix_slave_scan();
203 208 int offset = (isLeftHand) ? ROWS_PER_HAND : 0;
204// if(serial_slave_DATA_CORRUPT()){ 209 memcpy(&matrix[offset],
205// TXLED0; 210 (void *)serial_master_buffer, SERIAL_MASTER_BUFFER_LENGTH);
206 int offset = (isLeftHand) ? ROWS_PER_HAND : 0;
207
208 for (int i = 0; i < ROWS_PER_HAND; ++i) {
209 matrix[offset+i] = serial_master_buffer[i];
210 }
211
212// }else{
213// TXLED1;
214// }
215
216 matrix_scan_quantum(); 211 matrix_scan_quantum();
217 } 212 }
218 return 1; 213 return 1;
@@ -222,6 +217,7 @@ uint8_t matrix_scan(void)
222uint8_t matrix_master_scan(void) { 217uint8_t matrix_master_scan(void) {
223 218
224 int ret = _matrix_scan(); 219 int ret = _matrix_scan();
220 int mchanged = 1;
225 221
226 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND; 222 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
227 223
@@ -231,15 +227,18 @@ uint8_t matrix_master_scan(void) {
231// i2c_slave_buffer[i] = matrix[offset+i]; 227// i2c_slave_buffer[i] = matrix[offset+i];
232// } 228// }
233#else // USE_SERIAL 229#else // USE_SERIAL
234 for (int i = 0; i < ROWS_PER_HAND; ++i) { 230 #ifdef SERIAL_USE_MULTI_TRANSACTION
235 serial_master_buffer[i] = matrix[offset+i]; 231 mchanged = memcmp((void *)serial_master_buffer,
236 } 232 &matrix[offset], SERIAL_MASTER_BUFFER_LENGTH);
233 #endif
234 memcpy((void *)serial_master_buffer,
235 &matrix[offset], SERIAL_MASTER_BUFFER_LENGTH);
237#endif 236#endif
238 237
239#ifdef USE_MATRIX_I2C 238#ifdef USE_MATRIX_I2C
240 if( i2c_transaction() ) { 239 if( i2c_transaction() ) {
241#else // USE_SERIAL 240#else // USE_SERIAL
242 if( serial_transaction() ) { 241 if( serial_transaction(mchanged) ) {
243#endif 242#endif
244 // turn on the indicator led when halves are disconnected 243 // turn on the indicator led when halves are disconnected
245 TXLED1; 244 TXLED1;
@@ -273,9 +272,19 @@ void matrix_slave_scan(void) {
273 i2c_slave_buffer[i] = matrix[offset+i]; 272 i2c_slave_buffer[i] = matrix[offset+i];
274 } 273 }
275#else // USE_SERIAL 274#else // USE_SERIAL
275 #ifdef SERIAL_USE_MULTI_TRANSACTION
276 int change = 0;
277 #endif
276 for (int i = 0; i < ROWS_PER_HAND; ++i) { 278 for (int i = 0; i < ROWS_PER_HAND; ++i) {
279 #ifdef SERIAL_USE_MULTI_TRANSACTION
280 if( serial_slave_buffer[i] != matrix[offset+i] )
281 change = 1;
282 #endif
277 serial_slave_buffer[i] = matrix[offset+i]; 283 serial_slave_buffer[i] = matrix[offset+i];
278 } 284 }
285 #ifdef SERIAL_USE_MULTI_TRANSACTION
286 slave_buffer_change_count += change;
287 #endif
279#endif 288#endif
280} 289}
281 290
diff --git a/keyboards/crkbd/rev1/rev1.h b/keyboards/crkbd/rev1/rev1.h
index d02c51f30..cdd61d2bf 100644
--- a/keyboards/crkbd/rev1/rev1.h
+++ b/keyboards/crkbd/rev1/rev1.h
@@ -1,5 +1,4 @@
1#ifndef REV1_H 1#pragma once
2#define REV1_CONFIG_H
3 2
4#include "../crkbd.h" 3#include "../crkbd.h"
5 4
@@ -49,5 +48,3 @@
49 KC_##L20, KC_##L21, KC_##L22, KC_##L23, KC_##L24, KC_##L25, KC_##R20, KC_##R21, KC_##R22, KC_##R23, KC_##R24, KC_##R25, \ 48 KC_##L20, KC_##L21, KC_##L22, KC_##L23, KC_##L24, KC_##L25, KC_##R20, KC_##R21, KC_##R22, KC_##R23, KC_##R24, KC_##R25, \
50 KC_##L30, KC_##L31, KC_##L32, KC_##R30, KC_##R31, KC_##R32 \ 49 KC_##L30, KC_##L31, KC_##L32, KC_##R30, KC_##R31, KC_##R32 \
51 ) 50 )
52
53#endif
diff --git a/keyboards/crkbd/rev1/rules.mk b/keyboards/crkbd/rev1/rules.mk
index 7af7ffdb8..6028b5a5b 100644
--- a/keyboards/crkbd/rev1/rules.mk
+++ b/keyboards/crkbd/rev1/rules.mk
@@ -1,2 +1,3 @@
1SRC += rev1/matrix.c \ 1SRC += rev1/matrix.c
2 ws2812.c 2SRC += rev1/split_util.c
3SRC += rev1/split_scomm.c
diff --git a/keyboards/crkbd/rev1/serial_config.h b/keyboards/crkbd/rev1/serial_config.h
new file mode 100644
index 000000000..671ed821d
--- /dev/null
+++ b/keyboards/crkbd/rev1/serial_config.h
@@ -0,0 +1,10 @@
1#pragma once
2
3/* Soft Serial defines */
4#define SERIAL_PIN_DDR DDRD
5#define SERIAL_PIN_PORT PORTD
6#define SERIAL_PIN_INPUT PIND
7#define SERIAL_PIN_MASK _BV(PD2)
8#define SERIAL_PIN_INTERRUPT INT2_vect
9
10#define SERIAL_USE_MULTI_TRANSACTION
diff --git a/keyboards/crkbd/rev1/serial_config_simpleapi.h b/keyboards/crkbd/rev1/serial_config_simpleapi.h
new file mode 100644
index 000000000..0e1dd9e4a
--- /dev/null
+++ b/keyboards/crkbd/rev1/serial_config_simpleapi.h
@@ -0,0 +1,5 @@
1#pragma once
2
3#undef SERIAL_USE_MULTI_TRANSACTION
4#define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2
5#define SERIAL_MASTER_BUFFER_LENGTH MATRIX_ROWS/2
diff --git a/keyboards/crkbd/rev1/split_scomm.c b/keyboards/crkbd/rev1/split_scomm.c
new file mode 100644
index 000000000..9719eb22e
--- /dev/null
+++ b/keyboards/crkbd/rev1/split_scomm.c
@@ -0,0 +1,73 @@
1#ifdef USE_SERIAL
2#ifdef SERIAL_USE_MULTI_TRANSACTION
3/* --- USE flexible API (using multi-type transaction function) --- */
4
5#include <stdbool.h>
6#include <stdint.h>
7#include <stddef.h>
8#include <split_scomm.h>
9#include "serial.h"
10#ifdef SERIAL_DEBUG_MODE
11#include <avr/io.h>
12#endif
13
14uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
15uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0};
16uint8_t volatile status_com = 0;
17uint8_t volatile status1 = 0;
18uint8_t slave_buffer_change_count = 0;
19uint8_t s_change_old = 0xff;
20
21SSTD_t transactions[] = {
22#define GET_SLAVE_STATUS 0
23 /* master buffer not changed, only recive slave_buffer_change_count */
24 { (uint8_t *)&status_com,
25 0, NULL,
26 sizeof(slave_buffer_change_count), &slave_buffer_change_count,
27 },
28#define PUT_MASTER_GET_SLAVE_STATUS 1
29 /* master buffer changed need send, and recive slave_buffer_change_count */
30 { (uint8_t *)&status_com,
31 sizeof(serial_master_buffer), (uint8_t *)serial_master_buffer,
32 sizeof(slave_buffer_change_count), &slave_buffer_change_count,
33 },
34#define GET_SLAVE_BUFFER 2
35 /* recive serial_slave_buffer */
36 { (uint8_t *)&status1,
37 0, NULL,
38 sizeof(serial_slave_buffer), (uint8_t *)serial_slave_buffer
39 }
40};
41
42void serial_master_init(void)
43{
44 soft_serial_initiator_init(transactions);
45}
46
47void serial_slave_init(void)
48{
49 soft_serial_target_init(transactions);
50}
51
52// 0 => no error
53// 1 => slave did not respond
54// 2 => checksum error
55int serial_update_buffers(int master_update)
56{
57 int status;
58 static int need_retry = 0;
59 if( s_change_old != slave_buffer_change_count ) {
60 status = soft_serial_transaction(GET_SLAVE_BUFFER);
61 if( status == TRANSACTION_END )
62 s_change_old = slave_buffer_change_count;
63 }
64 if( !master_update && !need_retry)
65 status = soft_serial_transaction(GET_SLAVE_STATUS);
66 else
67 status = soft_serial_transaction(PUT_MASTER_GET_SLAVE_STATUS);
68 need_retry = ( status == TRANSACTION_END ) ? 0 : 1;
69 return status;
70}
71
72#endif // SERIAL_USE_MULTI_TRANSACTION
73#endif /* USE_SERIAL */
diff --git a/keyboards/crkbd/rev1/split_scomm.h b/keyboards/crkbd/rev1/split_scomm.h
new file mode 100644
index 000000000..16887eb74
--- /dev/null
+++ b/keyboards/crkbd/rev1/split_scomm.h
@@ -0,0 +1,21 @@
1#pragma once
2
3#ifndef SERIAL_USE_MULTI_TRANSACTION
4/* --- USE Simple API (OLD API, compatible with let's split serial.c) --- */
5#include "serial.h"
6
7#else
8/* --- USE flexible API (using multi-type transaction function) --- */
9// Buffers for master - slave communication
10#define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2
11#define SERIAL_MASTER_BUFFER_LENGTH MATRIX_ROWS/2
12
13extern volatile uint8_t serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH];
14extern volatile uint8_t serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH];
15extern uint8_t slave_buffer_change_count;
16
17void serial_master_init(void);
18void serial_slave_init(void);
19int serial_update_buffers(int master_changed);
20
21#endif
diff --git a/keyboards/crkbd/split_util.c b/keyboards/crkbd/rev1/split_util.c
index 8bc064174..e1ff8b437 100644
--- a/keyboards/crkbd/split_util.c
+++ b/keyboards/crkbd/rev1/split_util.c
@@ -7,12 +7,11 @@
7#include "split_util.h" 7#include "split_util.h"
8#include "matrix.h" 8#include "matrix.h"
9#include "keyboard.h" 9#include "keyboard.h"
10#include "config.h"
11 10
12#ifdef USE_MATRIX_I2C 11#ifdef USE_MATRIX_I2C
13# include "i2c.h" 12# include "i2c.h"
14#else 13#else
15# include "serial.h" 14# include "split_scomm.h"
16#endif 15#endif
17 16
18volatile bool isLeftHand = true; 17volatile bool isLeftHand = true;
diff --git a/keyboards/crkbd/split_util.h b/keyboards/crkbd/rev1/split_util.h
index 687ca19bd..f59304756 100644
--- a/keyboards/crkbd/split_util.h
+++ b/keyboards/crkbd/rev1/split_util.h
@@ -1,5 +1,4 @@
1#ifndef SPLIT_KEYBOARD_UTIL_H 1#pragma once
2#define SPLIT_KEYBOARD_UTIL_H
3 2
4#include <stdbool.h> 3#include <stdbool.h>
5#include "eeconfig.h" 4#include "eeconfig.h"
@@ -15,5 +14,3 @@ void split_keyboard_setup(void);
15bool has_usb(void); 14bool has_usb(void);
16 15
17void matrix_master_OLED_init (void); 16void matrix_master_OLED_init (void);
18
19#endif
diff --git a/keyboards/crkbd/rules.mk b/keyboards/crkbd/rules.mk
index d88daebf3..6396b115d 100644
--- a/keyboards/crkbd/rules.mk
+++ b/keyboards/crkbd/rules.mk
@@ -1,7 +1,9 @@
1SRC += i2c.c \ 1SRC += i2c.c
2 serial.c \ 2SRC += serial.c
3 split_util.c \ 3SRC += ssd1306.c
4 ssd1306.c 4
5# if firmware size over limit, try this option
6# CFLAGS += -flto
5 7
6# MCU name 8# MCU name
7#MCU = at90usb1287 9#MCU = at90usb1287
diff --git a/keyboards/crkbd/serial.c b/keyboards/crkbd/serial.c
index e918ab6ee..11ceff0b3 100644
--- a/keyboards/crkbd/serial.c
+++ b/keyboards/crkbd/serial.c
@@ -9,36 +9,128 @@
9#include <avr/io.h> 9#include <avr/io.h>
10#include <avr/interrupt.h> 10#include <avr/interrupt.h>
11#include <util/delay.h> 11#include <util/delay.h>
12#include <stddef.h>
12#include <stdbool.h> 13#include <stdbool.h>
13#include "serial.h" 14#include "serial.h"
15//#include <pro_micro.h>
14 16
15#ifdef USE_SERIAL 17#ifdef USE_SERIAL
16 18
17// Serial pulse period in microseconds. Its probably a bad idea to lower this 19#ifndef SERIAL_USE_MULTI_TRANSACTION
18// value. 20/* --- USE Simple API (OLD API, compatible with let's split serial.c) */
19#define SERIAL_DELAY 24 21 #if SERIAL_SLAVE_BUFFER_LENGTH > 0
22 uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
23 #endif
24 #if SERIAL_MASTER_BUFFER_LENGTH > 0
25 uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0};
26 #endif
27 uint8_t volatile status0 = 0;
28
29SSTD_t transactions[] = {
30 { (uint8_t *)&status0,
31 #if SERIAL_MASTER_BUFFER_LENGTH > 0
32 sizeof(serial_master_buffer), (uint8_t *)serial_master_buffer,
33 #else
34 0, (uint8_t *)NULL,
35 #endif
36 #if SERIAL_SLAVE_BUFFER_LENGTH > 0
37 sizeof(serial_slave_buffer), (uint8_t *)serial_slave_buffer
38 #else
39 0, (uint8_t *)NULL,
40 #endif
41 }
42};
43
44void serial_master_init(void)
45{ soft_serial_initiator_init(transactions); }
20 46
21uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0}; 47void serial_slave_init(void)
22uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0}; 48{ soft_serial_target_init(transactions); }
23 49
24#define SLAVE_DATA_CORRUPT (1<<0) 50// 0 => no error
25volatile uint8_t status = 0; 51// 1 => slave did not respond
52// 2 => checksum error
53int serial_update_buffers()
54{ return soft_serial_transaction(); }
55
56#endif // Simple API (OLD API, compatible with let's split serial.c)
57
58#define ALWAYS_INLINE __attribute__((always_inline))
59#define NO_INLINE __attribute__((noinline))
60#define _delay_sub_us(x) __builtin_avr_delay_cycles(x)
61
62// Serial pulse period in microseconds.
63#define TID_SEND_ADJUST 14
64
65#define SELECT_SERIAL_SPEED 1
66#if SELECT_SERIAL_SPEED == 0
67 // Very High speed
68 #define SERIAL_DELAY 4 // micro sec
69 #define READ_WRITE_START_ADJUST 33 // cycles
70 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
71#elif SELECT_SERIAL_SPEED == 1
72 // High speed
73 #define SERIAL_DELAY 6 // micro sec
74 #define READ_WRITE_START_ADJUST 30 // cycles
75 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
76#elif SELECT_SERIAL_SPEED == 2
77 // Middle speed
78 #define SERIAL_DELAY 12 // micro sec
79 #define READ_WRITE_START_ADJUST 30 // cycles
80 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
81#elif SELECT_SERIAL_SPEED == 3
82 // Low speed
83 #define SERIAL_DELAY 24 // micro sec
84 #define READ_WRITE_START_ADJUST 30 // cycles
85 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
86#elif SELECT_SERIAL_SPEED == 4
87 // Very Low speed
88 #define SERIAL_DELAY 50 // micro sec
89 #define READ_WRITE_START_ADJUST 30 // cycles
90 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
91#else
92#error Illegal Serial Speed
93#endif
94
95
96#define SERIAL_DELAY_HALF1 (SERIAL_DELAY/2)
97#define SERIAL_DELAY_HALF2 (SERIAL_DELAY - SERIAL_DELAY/2)
98
99#define SLAVE_INT_WIDTH_US 1
100#ifndef SERIAL_USE_MULTI_TRANSACTION
101 #define SLAVE_INT_RESPONSE_TIME SERIAL_DELAY
102#else
103 #define SLAVE_INT_ACK_WIDTH_UNIT 2
104 #define SLAVE_INT_ACK_WIDTH 4
105#endif
106
107static SSTD_t *Transaction_table = NULL;
26 108
27inline static 109inline static
28void serial_delay(void) { 110void serial_delay(void) {
29 _delay_us(SERIAL_DELAY); 111 _delay_us(SERIAL_DELAY);
30} 112}
31void serial_delay_short(void) { 113
32 _delay_us(SERIAL_DELAY-1); 114inline static
115void serial_delay_half1(void) {
116 _delay_us(SERIAL_DELAY_HALF1);
117}
118
119inline static
120void serial_delay_half2(void) {
121 _delay_us(SERIAL_DELAY_HALF2);
33} 122}
123
124inline static void serial_output(void) ALWAYS_INLINE;
34inline static 125inline static
35void serial_output(void) { 126void serial_output(void) {
36 SERIAL_PIN_DDR |= SERIAL_PIN_MASK; 127 SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
37} 128}
38 129
39// make the serial pin an input with pull-up resistor 130// make the serial pin an input with pull-up resistor
131inline static void serial_input_with_pullup(void) ALWAYS_INLINE;
40inline static 132inline static
41void serial_input(void) { 133void serial_input_with_pullup(void) {
42 SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK; 134 SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
43 SERIAL_PIN_PORT |= SERIAL_PIN_MASK; 135 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
44} 136}
@@ -48,191 +140,305 @@ uint8_t serial_read_pin(void) {
48 return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK); 140 return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
49} 141}
50 142
143inline static void serial_low(void) ALWAYS_INLINE;
51inline static 144inline static
52void serial_low(void) { 145void serial_low(void) {
53 SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK; 146 SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
54} 147}
55 148
149inline static void serial_high(void) ALWAYS_INLINE;
56inline static 150inline static
57void serial_high(void) { 151void serial_high(void) {
58 SERIAL_PIN_PORT |= SERIAL_PIN_MASK; 152 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
59} 153}
60 154
61void serial_master_init(void) { 155void soft_serial_initiator_init(SSTD_t *sstd_table)
62 serial_output(); 156{
63 serial_high(); 157 Transaction_table = sstd_table;
158 serial_output();
159 serial_high();
64} 160}
65 161
66void serial_slave_init(void) { 162void soft_serial_target_init(SSTD_t *sstd_table)
67 serial_input(); 163{
68 164 Transaction_table = sstd_table;
69#ifndef USE_SERIAL_PD2 165 serial_input_with_pullup();
70 // Enable INT0 166
71 EIMSK |= _BV(INT0); 167#if SERIAL_PIN_MASK == _BV(PD0)
72 // Trigger on falling edge of INT0 168 // Enable INT0
73 EICRA &= ~(_BV(ISC00) | _BV(ISC01)); 169 EIMSK |= _BV(INT0);
170 // Trigger on falling edge of INT0
171 EICRA &= ~(_BV(ISC00) | _BV(ISC01));
172#elif SERIAL_PIN_MASK == _BV(PD2)
173 // Enable INT2
174 EIMSK |= _BV(INT2);
175 // Trigger on falling edge of INT2
176 EICRA &= ~(_BV(ISC20) | _BV(ISC21));
74#else 177#else
75 // Enable INT2 178 #error unknown SERIAL_PIN_MASK value
76 EIMSK |= _BV(INT2);
77 // Trigger on falling edge of INT2
78 EICRA &= ~(_BV(ISC20) | _BV(ISC21));
79#endif 179#endif
80} 180}
81 181
82// Used by the master to synchronize timing with the slave. 182// Used by the sender to synchronize timing with the reciver.
183static void sync_recv(void) NO_INLINE;
83static 184static
84void sync_recv(void) { 185void sync_recv(void) {
85 serial_input(); 186 for (uint8_t i = 0; i < SERIAL_DELAY*5 && serial_read_pin(); i++ ) {
86 // This shouldn't hang if the slave disconnects because the 187 }
87 // serial line will float to high if the slave does disconnect. 188 // This shouldn't hang if the target disconnects because the
189 // serial line will float to high if the target does disconnect.
88 while (!serial_read_pin()); 190 while (!serial_read_pin());
89 //serial_delay();
90 _delay_us(SERIAL_DELAY-5);
91} 191}
92 192
93// Used by the slave to send a synchronization signal to the master. 193// Used by the reciver to send a synchronization signal to the sender.
194static void sync_send(void)NO_INLINE;
94static 195static
95void sync_send(void) { 196void sync_send(void) {
96 serial_output();
97
98 serial_low(); 197 serial_low();
99 serial_delay(); 198 serial_delay();
100
101 serial_high(); 199 serial_high();
102} 200}
103 201
104// Reads a byte from the serial line 202// Reads a byte from the serial line
105static 203static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) NO_INLINE;
106uint8_t serial_read_byte(void) { 204static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) {
107 uint8_t byte = 0; 205 uint8_t byte, i, p, pb;
108 serial_input(); 206
109 for ( uint8_t i = 0; i < 8; ++i) { 207 _delay_sub_us(READ_WRITE_START_ADJUST);
110 byte = (byte << 1) | serial_read_pin(); 208 for( i = 0, byte = 0, p = 0; i < bit; i++ ) {
111 serial_delay(); 209 serial_delay_half1(); // read the middle of pulses
112 _delay_us(1); 210 if( serial_read_pin() ) {
211 byte = (byte << 1) | 1; p ^= 1;
212 } else {
213 byte = (byte << 1) | 0; p ^= 0;
214 }
215 _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
216 serial_delay_half2();
113 } 217 }
218 /* recive parity bit */
219 serial_delay_half1(); // read the middle of pulses
220 pb = serial_read_pin();
221 _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
222 serial_delay_half2();
223
224 *pterrcount += (p != pb)? 1 : 0;
114 225
115 return byte; 226 return byte;
116} 227}
117 228
118// Sends a byte with MSB ordering 229// Sends a byte with MSB ordering
119static 230void serial_write_chunk(uint8_t data, uint8_t bit) NO_INLINE;
120void serial_write_byte(uint8_t data) { 231void serial_write_chunk(uint8_t data, uint8_t bit) {
121 uint8_t b = 8; 232 uint8_t b, p;
122 serial_output(); 233 for( p = 0, b = 1<<(bit-1); b ; b >>= 1) {
123 while( b-- ) { 234 if(data & b) {
124 if(data & (1 << b)) { 235 serial_high(); p ^= 1;
125 serial_high(); 236 } else {
126 } else { 237 serial_low(); p ^= 0;
127 serial_low(); 238 }
239 serial_delay();
128 } 240 }
241 /* send parity bit */
242 if(p & 1) { serial_high(); }
243 else { serial_low(); }
129 serial_delay(); 244 serial_delay();
130 }
131}
132 245
133// interrupt handle to be used by the slave device 246 serial_low(); // sync_send() / senc_recv() need raise edge
134ISR(SERIAL_PIN_INTERRUPT) { 247}
135 sync_send();
136 248
137 uint8_t checksum = 0; 249static void serial_send_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
138 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) { 250static
139 serial_write_byte(serial_slave_buffer[i]); 251void serial_send_packet(uint8_t *buffer, uint8_t size) {
252 for (uint8_t i = 0; i < size; ++i) {
253 uint8_t data;
254 data = buffer[i];
140 sync_send(); 255 sync_send();
141 checksum += serial_slave_buffer[i]; 256 serial_write_chunk(data,8);
142 } 257 }
143 serial_write_byte(checksum); 258}
144 sync_send();
145 259
146 // wait for the sync to finish sending 260static uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
147 serial_delay(); 261static
262uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) {
263 uint8_t pecount = 0;
264 for (uint8_t i = 0; i < size; ++i) {
265 uint8_t data;
266 sync_recv();
267 data = serial_read_chunk(&pecount, 8);
268 buffer[i] = data;
269 }
270 return pecount == 0;
271}
148 272
149 // read the middle of pulses 273inline static
150 _delay_us(SERIAL_DELAY/2); 274void change_sender2reciver(void) {
275 sync_send(); //0
276 serial_delay_half1(); //1
277 serial_low(); //2
278 serial_input_with_pullup(); //2
279 serial_delay_half1(); //3
280}
151 281
152 uint8_t checksum_computed = 0; 282inline static
153 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) { 283void change_reciver2sender(void) {
154 serial_master_buffer[i] = serial_read_byte(); 284 sync_recv(); //0
155 sync_send(); 285 serial_delay(); //1
156 checksum_computed += serial_master_buffer[i]; 286 serial_low(); //3
157 } 287 serial_output(); //3
158 uint8_t checksum_received = serial_read_byte(); 288 serial_delay_half1(); //4
159 sync_send(); 289}
160 290
161 serial_input(); // end transaction 291// interrupt handle to be used by the target device
292ISR(SERIAL_PIN_INTERRUPT) {
162 293
163 if ( checksum_computed != checksum_received ) { 294#ifndef SERIAL_USE_MULTI_TRANSACTION
164 status |= SLAVE_DATA_CORRUPT; 295 serial_low();
296 serial_output();
297 SSTD_t *trans = Transaction_table;
298#else
299 // recive transaction table index
300 uint8_t tid;
301 uint8_t pecount = 0;
302 sync_recv();
303 tid = serial_read_chunk(&pecount,4);
304 if(pecount> 0)
305 return;
306 serial_delay_half1();
307
308 serial_high(); // response step1 low->high
309 serial_output();
310 _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT*SLAVE_INT_ACK_WIDTH);
311 SSTD_t *trans = &Transaction_table[tid];
312 serial_low(); // response step2 ack high->low
313#endif
314
315 // target send phase
316 if( trans->target2initiator_buffer_size > 0 )
317 serial_send_packet((uint8_t *)trans->target2initiator_buffer,
318 trans->target2initiator_buffer_size);
319 // target switch to input
320 change_sender2reciver();
321
322 // target recive phase
323 if( trans->initiator2target_buffer_size > 0 ) {
324 if (serial_recive_packet((uint8_t *)trans->initiator2target_buffer,
325 trans->initiator2target_buffer_size) ) {
326 *trans->status = TRANSACTION_ACCEPTED;
327 } else {
328 *trans->status = TRANSACTION_DATA_ERROR;
329 }
165 } else { 330 } else {
166 status &= ~SLAVE_DATA_CORRUPT; 331 *trans->status = TRANSACTION_ACCEPTED;
167 } 332 }
168}
169 333
170inline 334 sync_recv(); //weit initiator output to high
171bool serial_slave_DATA_CORRUPT(void) {
172 return status & SLAVE_DATA_CORRUPT;
173} 335}
174 336
175// Copies the serial_slave_buffer to the master and sends the 337/////////
176// serial_master_buffer to the slave. 338// start transaction by initiator
339//
340// int soft_serial_transaction(int sstd_index)
177// 341//
178// Returns: 342// Returns:
179// 0 => no error 343// TRANSACTION_END
180// 1 => slave did not respond 344// TRANSACTION_NO_RESPONSE
181int serial_update_buffers(void) { 345// TRANSACTION_DATA_ERROR
182 // this code is very time dependent, so we need to disable interrupts 346// this code is very time dependent, so we need to disable interrupts
347#ifndef SERIAL_USE_MULTI_TRANSACTION
348int soft_serial_transaction(void) {
349 SSTD_t *trans = Transaction_table;
350#else
351int soft_serial_transaction(int sstd_index) {
352 SSTD_t *trans = &Transaction_table[sstd_index];
353#endif
183 cli(); 354 cli();
184 355
185 // signal to the slave that we want to start a transaction 356 // signal to the target that we want to start a transaction
186 serial_output(); 357 serial_output();
187 serial_low(); 358 serial_low();
188 _delay_us(1); 359 _delay_us(SLAVE_INT_WIDTH_US);
189 360
190 // wait for the slaves response 361#ifndef SERIAL_USE_MULTI_TRANSACTION
191 serial_input(); 362 // wait for the target response
192 serial_high(); 363 serial_input_with_pullup();
193 _delay_us(SERIAL_DELAY); 364 _delay_us(SLAVE_INT_RESPONSE_TIME);
194 365
195 // check if the slave is present 366 // check if the target is present
196 if (serial_read_pin()) { 367 if (serial_read_pin()) {
197 // slave failed to pull the line low, assume not present 368 // target failed to pull the line low, assume not present
369 serial_output();
370 serial_high();
371 *trans->status = TRANSACTION_NO_RESPONSE;
198 sei(); 372 sei();
199 return 1; 373 return TRANSACTION_NO_RESPONSE;
200 } 374 }
201 375
202 // if the slave is present syncronize with it 376#else
203 sync_recv(); 377 // send transaction table index
204 378 sync_send();
205 uint8_t checksum_computed = 0; 379 _delay_sub_us(TID_SEND_ADJUST);
206 // receive data from the slave 380 serial_write_chunk(sstd_index, 4);
207 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) { 381 serial_delay_half1();
208 serial_slave_buffer[i] = serial_read_byte(); 382
209 sync_recv(); 383 // wait for the target response (step1 low->high)
210 checksum_computed += serial_slave_buffer[i]; 384 serial_input_with_pullup();
385 while( !serial_read_pin() ) {
386 _delay_sub_us(2);
211 } 387 }
212 uint8_t checksum_received = serial_read_byte();
213 sync_recv();
214 388
215 if (checksum_computed != checksum_received) { 389 // check if the target is present (step2 high->low)
216 sei(); 390 for( int i = 0; serial_read_pin(); i++ ) {
217 return 2; 391 if (i > SLAVE_INT_ACK_WIDTH + 1) {
392 // slave failed to pull the line low, assume not present
393 serial_output();
394 serial_high();
395 *trans->status = TRANSACTION_NO_RESPONSE;
396 sei();
397 return TRANSACTION_NO_RESPONSE;
398 }
399 _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT);
218 } 400 }
401#endif
219 402
220 uint8_t checksum = 0; 403 // initiator recive phase
221 // send data to the slave 404 // if the target is present syncronize with it
222 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) { 405 if( trans->target2initiator_buffer_size > 0 ) {
223 serial_write_byte(serial_master_buffer[i]); 406 if (!serial_recive_packet((uint8_t *)trans->target2initiator_buffer,
224 sync_recv(); 407 trans->target2initiator_buffer_size) ) {
225 checksum += serial_master_buffer[i]; 408 serial_output();
409 serial_high();
410 *trans->status = TRANSACTION_DATA_ERROR;
411 sei();
412 return TRANSACTION_DATA_ERROR;
413 }
414 }
415
416 // initiator switch to output
417 change_reciver2sender();
418
419 // initiator send phase
420 if( trans->initiator2target_buffer_size > 0 ) {
421 serial_send_packet((uint8_t *)trans->initiator2target_buffer,
422 trans->initiator2target_buffer_size);
226 } 423 }
227 serial_write_byte(checksum);
228 sync_recv();
229 424
230 // always, release the line when not in use 425 // always, release the line when not in use
231 serial_output(); 426 sync_send();
232 serial_high();
233 427
428 *trans->status = TRANSACTION_END;
234 sei(); 429 sei();
235 return 0; 430 return TRANSACTION_END;
236} 431}
237 432
433#ifdef SERIAL_USE_MULTI_TRANSACTION
434int soft_serial_get_and_clean_status(int sstd_index) {
435 SSTD_t *trans = &Transaction_table[sstd_index];
436 cli();
437 int retval = *trans->status;
438 *trans->status = 0;;
439 sei();
440 return retval;
441}
442#endif
443
238#endif 444#endif
diff --git a/keyboards/crkbd/serial.h b/keyboards/crkbd/serial.h
index 43e51f7fa..76c6aaa04 100644
--- a/keyboards/crkbd/serial.h
+++ b/keyboards/crkbd/serial.h
@@ -1,32 +1,77 @@
1#ifndef MY_SERIAL_H 1#pragma once
2#define MY_SERIAL_H
3 2
4#include "config.h"
5#include <stdbool.h> 3#include <stdbool.h>
6 4
7/* TODO: some defines for interrupt setup */ 5// /////////////////////////////////////////////////////////////////
8#define SERIAL_PIN_DDR DDRD 6// Need Soft Serial defines in serial_config.h
9#define SERIAL_PIN_PORT PORTD 7// /////////////////////////////////////////////////////////////////
10#define SERIAL_PIN_INPUT PIND 8// ex.
9// #define SERIAL_PIN_DDR DDRD
10// #define SERIAL_PIN_PORT PORTD
11// #define SERIAL_PIN_INPUT PIND
12// #define SERIAL_PIN_MASK _BV(PD?) ?=0,2
13// #define SERIAL_PIN_INTERRUPT INT?_vect ?=0,2
14//
15// //// USE Simple API (OLD API, compatible with let's split serial.c)
16// ex.
17// #define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2
18// #define SERIAL_MASTER_BUFFER_LENGTH 1
19//
20// //// USE flexible API (using multi-type transaction function)
21// #define SERIAL_USE_MULTI_TRANSACTION
22//
23// /////////////////////////////////////////////////////////////////
11 24
12#ifndef USE_SERIAL_PD2
13#define SERIAL_PIN_MASK _BV(PD0)
14#define SERIAL_PIN_INTERRUPT INT0_vect
15#else
16#define SERIAL_PIN_MASK _BV(PD2)
17#define SERIAL_PIN_INTERRUPT INT2_vect
18#endif
19
20#define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2
21#define SERIAL_MASTER_BUFFER_LENGTH MATRIX_ROWS/2
22 25
23// Buffers for master - slave communication 26#ifndef SERIAL_USE_MULTI_TRANSACTION
27/* --- USE Simple API (OLD API, compatible with let's split serial.c) */
28#if SERIAL_SLAVE_BUFFER_LENGTH > 0
24extern volatile uint8_t serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH]; 29extern volatile uint8_t serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH];
30#endif
31#if SERIAL_MASTER_BUFFER_LENGTH > 0
25extern volatile uint8_t serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH]; 32extern volatile uint8_t serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH];
33#endif
26 34
27void serial_master_init(void); 35void serial_master_init(void);
28void serial_slave_init(void); 36void serial_slave_init(void);
29int serial_update_buffers(void); 37int serial_update_buffers(void);
30bool serial_slave_data_corrupt(void);
31 38
39#endif // USE Simple API
40
41// Soft Serial Transaction Descriptor
42typedef struct _SSTD_t {
43 uint8_t *status;
44 uint8_t initiator2target_buffer_size;
45 uint8_t *initiator2target_buffer;
46 uint8_t target2initiator_buffer_size;
47 uint8_t *target2initiator_buffer;
48} SSTD_t;
49
50// initiator is transaction start side
51void soft_serial_initiator_init(SSTD_t *sstd_table);
52// target is interrupt accept side
53void soft_serial_target_init(SSTD_t *sstd_table);
54
55// initiator resullt
56#define TRANSACTION_END 0
57#define TRANSACTION_NO_RESPONSE 0x1
58#define TRANSACTION_DATA_ERROR 0x2
59#ifndef SERIAL_USE_MULTI_TRANSACTION
60int soft_serial_transaction(void);
61#else
62int soft_serial_transaction(int sstd_index);
63#endif
64
65// target status
66// *SSTD_t.status has
67// initiator:
68// TRANSACTION_END
69// or TRANSACTION_NO_RESPONSE
70// or TRANSACTION_DATA_ERROR
71// target:
72// TRANSACTION_DATA_ERROR
73// or TRANSACTION_ACCEPTED
74#define TRANSACTION_ACCEPTED 0x4
75#ifdef SERIAL_USE_MULTI_TRANSACTION
76int soft_serial_get_and_clean_status(int sstd_index);
32#endif 77#endif
diff --git a/keyboards/crkbd/ssd1306.c b/keyboards/crkbd/ssd1306.c
index d07900119..b8f9512e3 100644
--- a/keyboards/crkbd/ssd1306.c
+++ b/keyboards/crkbd/ssd1306.c
@@ -123,6 +123,7 @@ static int8_t capture_sendchar(uint8_t c) {
123bool iota_gfx_init(bool rotate) { 123bool iota_gfx_init(bool rotate) {
124 bool success = false; 124 bool success = false;
125 125
126 i2c_master_init();
126 send_cmd1(DisplayOff); 127 send_cmd1(DisplayOff);
127 send_cmd2(SetDisplayClockDiv, 0x80); 128 send_cmd2(SetDisplayClockDiv, 0x80);
128 send_cmd2(SetMultiPlex, DisplayHeight - 1); 129 send_cmd2(SetMultiPlex, DisplayHeight - 1);
diff --git a/keyboards/crkbd/ssd1306.h b/keyboards/crkbd/ssd1306.h
index 59d31c9f3..76dd6a2a7 100644
--- a/keyboards/crkbd/ssd1306.h
+++ b/keyboards/crkbd/ssd1306.h
@@ -1,10 +1,8 @@
1#ifndef SSD1306_H 1#pragma once
2#define SSD1306_H
3 2
4#include <stdbool.h> 3#include <stdbool.h>
5#include <stdio.h> 4#include <stdio.h>
6#include "pincontrol.h" 5#include "pincontrol.h"
7#include "config.h"
8 6
9enum ssd1306_cmds { 7enum ssd1306_cmds {
10 DisplayOff = 0xAE, 8 DisplayOff = 0xAE,
@@ -88,7 +86,3 @@ void matrix_write(struct CharacterMatrix *matrix, const char *data);
88void matrix_write_ln(struct CharacterMatrix *matrix, const char *data); 86void matrix_write_ln(struct CharacterMatrix *matrix, const char *data);
89void matrix_write_P(struct CharacterMatrix *matrix, const char *data); 87void matrix_write_P(struct CharacterMatrix *matrix, const char *data);
90void matrix_render(struct CharacterMatrix *matrix); 88void matrix_render(struct CharacterMatrix *matrix);
91
92
93
94#endif