Skip to content

Commit 5eb5b60

Browse files
noogesjackhumbert
authored andcommitted
Add Levinson keyboard (#1723)
* Fork lets_split to levinson Update subproject default * Update Readme * Pass LED backlight info from master to slave over serial
1 parent 19f48fa commit 5eb5b60

23 files changed

+1903
-0
lines changed

keyboards/levinson/config.h

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright 2012 Jun Wako <wakojun@gmail.com>
3+
Copyright 2015 Jack Humbert
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 2 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#ifndef CONFIG_H
20+
#define CONFIG_H
21+
22+
#include "config_common.h"
23+
24+
#ifdef SUBPROJECT_rev1
25+
#include "rev1/config.h"
26+
#endif
27+
#ifdef SUBPROJECT_rev2
28+
#include "rev2/config.h"
29+
#endif
30+
31+
#endif

keyboards/levinson/i2c.c

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#include <util/twi.h>
2+
#include <avr/io.h>
3+
#include <stdlib.h>
4+
#include <avr/interrupt.h>
5+
#include <util/twi.h>
6+
#include <stdbool.h>
7+
#include "i2c.h"
8+
9+
#ifdef USE_I2C
10+
11+
// Limits the amount of we wait for any one i2c transaction.
12+
// Since were running SCL line 100kHz (=> 10μs/bit), and each transactions is
13+
// 9 bits, a single transaction will take around 90μs to complete.
14+
//
15+
// (F_CPU/SCL_CLOCK) => # of μC cycles to transfer a bit
16+
// poll loop takes at least 8 clock cycles to execute
17+
#define I2C_LOOP_TIMEOUT (9+1)*(F_CPU/SCL_CLOCK)/8
18+
19+
#define BUFFER_POS_INC() (slave_buffer_pos = (slave_buffer_pos+1)%SLAVE_BUFFER_SIZE)
20+
21+
volatile uint8_t i2c_slave_buffer[SLAVE_BUFFER_SIZE];
22+
23+
static volatile uint8_t slave_buffer_pos;
24+
static volatile bool slave_has_register_set = false;
25+
26+
// Wait for an i2c operation to finish
27+
inline static
28+
void i2c_delay(void) {
29+
uint16_t lim = 0;
30+
while(!(TWCR & (1<<TWINT)) && lim < I2C_LOOP_TIMEOUT)
31+
lim++;
32+
33+
// easier way, but will wait slightly longer
34+
// _delay_us(100);
35+
}
36+
37+
// Setup twi to run at 100kHz
38+
void i2c_master_init(void) {
39+
// no prescaler
40+
TWSR = 0;
41+
// Set TWI clock frequency to SCL_CLOCK. Need TWBR>10.
42+
// Check datasheets for more info.
43+
TWBR = ((F_CPU/SCL_CLOCK)-16)/2;
44+
}
45+
46+
// Start a transaction with the given i2c slave address. The direction of the
47+
// transfer is set with I2C_READ and I2C_WRITE.
48+
// returns: 0 => success
49+
// 1 => error
50+
uint8_t i2c_master_start(uint8_t address) {
51+
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTA);
52+
53+
i2c_delay();
54+
55+
// check that we started successfully
56+
if ( (TW_STATUS != TW_START) && (TW_STATUS != TW_REP_START))
57+
return 1;
58+
59+
TWDR = address;
60+
TWCR = (1<<TWINT) | (1<<TWEN);
61+
62+
i2c_delay();
63+
64+
if ( (TW_STATUS != TW_MT_SLA_ACK) && (TW_STATUS != TW_MR_SLA_ACK) )
65+
return 1; // slave did not acknowledge
66+
else
67+
return 0; // success
68+
}
69+
70+
71+
// Finish the i2c transaction.
72+
void i2c_master_stop(void) {
73+
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
74+
75+
uint16_t lim = 0;
76+
while(!(TWCR & (1<<TWSTO)) && lim < I2C_LOOP_TIMEOUT)
77+
lim++;
78+
}
79+
80+
// Write one byte to the i2c slave.
81+
// returns 0 => slave ACK
82+
// 1 => slave NACK
83+
uint8_t i2c_master_write(uint8_t data) {
84+
TWDR = data;
85+
TWCR = (1<<TWINT) | (1<<TWEN);
86+
87+
i2c_delay();
88+
89+
// check if the slave acknowledged us
90+
return (TW_STATUS == TW_MT_DATA_ACK) ? 0 : 1;
91+
}
92+
93+
// Read one byte from the i2c slave. If ack=1 the slave is acknowledged,
94+
// if ack=0 the acknowledge bit is not set.
95+
// returns: byte read from i2c device
96+
uint8_t i2c_master_read(int ack) {
97+
TWCR = (1<<TWINT) | (1<<TWEN) | (ack<<TWEA);
98+
99+
i2c_delay();
100+
return TWDR;
101+
}
102+
103+
void i2c_reset_state(void) {
104+
TWCR = 0;
105+
}
106+
107+
void i2c_slave_init(uint8_t address) {
108+
TWAR = address << 0; // slave i2c address
109+
// TWEN - twi enable
110+
// TWEA - enable address acknowledgement
111+
// TWINT - twi interrupt flag
112+
// TWIE - enable the twi interrupt
113+
TWCR = (1<<TWIE) | (1<<TWEA) | (1<<TWINT) | (1<<TWEN);
114+
}
115+
116+
ISR(TWI_vect);
117+
118+
ISR(TWI_vect) {
119+
uint8_t ack = 1;
120+
switch(TW_STATUS) {
121+
case TW_SR_SLA_ACK:
122+
// this device has been addressed as a slave receiver
123+
slave_has_register_set = false;
124+
break;
125+
126+
case TW_SR_DATA_ACK:
127+
// this device has received data as a slave receiver
128+
// The first byte that we receive in this transaction sets the location
129+
// of the read/write location of the slaves memory that it exposes over
130+
// i2c. After that, bytes will be written at slave_buffer_pos, incrementing
131+
// slave_buffer_pos after each write.
132+
if(!slave_has_register_set) {
133+
slave_buffer_pos = TWDR;
134+
// don't acknowledge the master if this memory loctaion is out of bounds
135+
if ( slave_buffer_pos >= SLAVE_BUFFER_SIZE ) {
136+
ack = 0;
137+
slave_buffer_pos = 0;
138+
}
139+
slave_has_register_set = true;
140+
} else {
141+
i2c_slave_buffer[slave_buffer_pos] = TWDR;
142+
BUFFER_POS_INC();
143+
}
144+
break;
145+
146+
case TW_ST_SLA_ACK:
147+
case TW_ST_DATA_ACK:
148+
// master has addressed this device as a slave transmitter and is
149+
// requesting data.
150+
TWDR = i2c_slave_buffer[slave_buffer_pos];
151+
BUFFER_POS_INC();
152+
break;
153+
154+
case TW_BUS_ERROR: // something went wrong, reset twi state
155+
TWCR = 0;
156+
default:
157+
break;
158+
}
159+
// Reset everything, so we are ready for the next TWI interrupt
160+
TWCR |= (1<<TWIE) | (1<<TWINT) | (ack<<TWEA) | (1<<TWEN);
161+
}
162+
#endif

keyboards/levinson/i2c.h

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef I2C_H
2+
#define I2C_H
3+
4+
#include <stdint.h>
5+
6+
#ifndef F_CPU
7+
#define F_CPU 16000000UL
8+
#endif
9+
10+
#define I2C_READ 1
11+
#define I2C_WRITE 0
12+
13+
#define I2C_ACK 1
14+
#define I2C_NACK 0
15+
16+
#define SLAVE_BUFFER_SIZE 0x10
17+
18+
// i2c SCL clock frequency
19+
#define SCL_CLOCK 400000L
20+
21+
extern volatile uint8_t i2c_slave_buffer[SLAVE_BUFFER_SIZE];
22+
23+
void i2c_master_init(void);
24+
uint8_t i2c_master_start(uint8_t address);
25+
void i2c_master_stop(void);
26+
uint8_t i2c_master_write(uint8_t data);
27+
uint8_t i2c_master_read(int);
28+
void i2c_reset_state(void);
29+
void i2c_slave_init(uint8_t address);
30+
31+
32+
static inline unsigned char i2c_start_read(unsigned char addr) {
33+
return i2c_master_start((addr << 1) | I2C_READ);
34+
}
35+
36+
static inline unsigned char i2c_start_write(unsigned char addr) {
37+
return i2c_master_start((addr << 1) | I2C_WRITE);
38+
}
39+
40+
// from SSD1306 scrips
41+
extern unsigned char i2c_rep_start(unsigned char addr);
42+
extern void i2c_start_wait(unsigned char addr);
43+
extern unsigned char i2c_readAck(void);
44+
extern unsigned char i2c_readNak(void);
45+
extern unsigned char i2c_read(unsigned char ack);
46+
47+
#define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak();
48+
49+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef CONFIG_USER_H
2+
#define CONFIG_USER_H
3+
4+
#include "../../config.h"
5+
6+
/* Use I2C or Serial, not both */
7+
8+
#define USE_SERIAL
9+
// #define USE_I2C
10+
11+
/* Select hand configuration */
12+
13+
#define MASTER_LEFT
14+
// #define _MASTER_RIGHT
15+
// #define EE_HANDS
16+
17+
#define TAPPING_TERM 150
18+
19+
#undef RGBLED_NUM
20+
#define RGBLIGHT_ANIMATIONS
21+
#define RGBLED_NUM 12
22+
#define RGBLIGHT_HUE_STEP 8
23+
#define RGBLIGHT_SAT_STEP 8
24+
#define RGBLIGHT_VAL_STEP 8
25+
26+
#define BACKLIGHT_PIN B6
27+
//#define BACKLIGHT_BREATHING
28+
#define BACKLIGHT_LEVELS 7
29+
30+
#endif

0 commit comments

Comments
 (0)