]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/meira/issi.c
Remove more commented out MCUs
[qmk_firmware.git] / keyboards / meira / issi.c
1 #ifdef ISSI_ENABLE
2
3 #include <stdlib.h>
4 #include <stdint.h>
5 #include <util/delay.h>
6 #include <avr/sfr_defs.h>
7 #include <avr/io.h>
8 #include <util/twi.h>
9 #include "issi.h"
10 #include "print.h"
11 #include "TWIlib.h"
12
13 #define ISSI_ADDR_DEFAULT 0xE8
14
15 #define ISSI_REG_CONFIG 0x00
16 #define ISSI_REG_CONFIG_PICTUREMODE 0x00
17 #define ISSI_REG_CONFIG_AUTOPLAYMODE 0x08
18
19 #define ISSI_CONF_PICTUREMODE 0x00
20 #define ISSI_CONF_AUTOFRAMEMODE 0x04
21 #define ISSI_CONF_AUDIOMODE 0x08
22
23 #define ISSI_REG_PICTUREFRAME 0x01
24
25 #define ISSI_REG_SHUTDOWN 0x0A
26 #define ISSI_REG_AUDIOSYNC 0x06
27
28 #define ISSI_COMMANDREGISTER 0xFD
29 #define ISSI_BANK_FUNCTIONREG 0x0B // helpfully called 'page nine'
30 uint8_t control[8][9] = {
31     {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0},
32     {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0},
33     {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0},
34     {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0},
35 };
36 ISSIDeviceStruct *issi_devices[4] = {0, 0, 0, 0};
37
38 #ifndef cbi
39 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
40 #endif
41
42 #ifndef sbi
43 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
44 #endif
45
46 #define I2C_WRITE 0
47 #define F_SCL 400000UL // SCL frequency
48 #define Prescaler 1
49 #define TWBR_val ((((F_CPU / F_SCL) / Prescaler) - 16 ) / 2)
50
51 uint8_t i2c_start(uint8_t address)
52 {
53     // reset TWI control register
54     TWCR = 0;
55     // transmit START condition
56     TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
57     // wait for end of transmission
58     while( !(TWCR & (1<<TWINT)) );
59
60     // check if the start condition was successfully transmitted
61     if((TWSR & 0xF8) != TW_START){ return 1; }
62
63     // load slave address into data register
64     TWDR = address;
65     // start transmission of address
66     TWCR = (1<<TWINT) | (1<<TWEN);
67     // wait for end of transmission
68     while( !(TWCR & (1<<TWINT)) );
69
70     // check if the device has acknowledged the READ / WRITE mode
71     uint8_t twst = TW_STATUS & 0xF8;
72     if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;
73
74     return 0;
75 }
76
77 uint8_t i2c_write(uint8_t data)
78 {
79     // load data into data register
80     TWDR = data;
81     // start transmission of data
82     TWCR = (1 << TWINT) | (1 << TWEN);
83     // wait for end of transmission
84     while (!(TWCR & (1 << TWINT)))
85         ;
86
87     if ((TWSR & 0xF8) != TW_MT_DATA_ACK) {
88         return 1;
89     }
90     return 0;
91 }
92
93 uint8_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length)
94 {
95     TWBR = (uint8_t)TWBR_val;
96     if (i2c_start(address | I2C_WRITE))
97         return 1;
98     for (uint16_t i = 0; i < length; i++) {
99         if (i2c_write(data[i]))
100             return 1;
101     }
102     // transmit STOP condition
103     TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
104     return 0;
105 }
106
107 void setFrame(uint8_t device, uint8_t frame)
108 {
109     static uint8_t current_frame = -1;
110     if(current_frame != frame){
111         uint8_t payload[] = {
112             ISSI_ADDR_DEFAULT | device << 1,
113             ISSI_COMMANDREGISTER,
114             frame
115         };
116         TWITransmitData(payload, sizeof(payload), 0, 1);
117     }
118     // static uint8_t current_frame = 0xFF;
119     // if(current_frame == frame){
120     //     // return;
121     // }
122     // uint8_t payload[2] = { ISSI_COMMANDREGISTER, frame };
123     // i2c_transmit(ISSI_ADDR_DEFAULT | device << 1, payload, 2);
124     // current_frame = frame;
125 }
126
127 void writeRegister8(uint8_t device, uint8_t frame, uint8_t reg, uint8_t data)
128 {
129     // Set the frame
130     setFrame(device, frame);
131
132     // Write to the register
133     uint8_t payload[] = {
134         ISSI_ADDR_DEFAULT | device << 1,
135         reg,
136         data
137     };
138     TWITransmitData(payload, sizeof(payload), 0, 1);
139 }
140
141 // void activateLED(uint8_t matrix, uint8_t cx, uint8_t cy, uint8_t pwm)
142 // {
143 //     xprintf("activeLED: %02X %02X %02X %02X\n", matrix, cy, cx, pwm);
144 //     uint8_t x = cx - 1;  // funciton takes 1 based counts, but we need 0...
145 //     uint8_t y = cy - 1;  // creating them once for less confusion
146 //     if(pwm == 0){
147 //         cbi(control[matrix][y], x);
148 //     }else{
149 //         sbi(control[matrix][y], x);
150 //     }
151 //     uint8_t device = (matrix & 0x06) >> 1;
152 //     uint8_t control_reg = (y << 1) | (matrix & 0x01);
153 //     uint8_t pwm_reg = 0;
154 //     switch(matrix & 0x01){
155 //         case 0:
156 //             pwm_reg = 0x24;
157 //             break;
158 //         case 1:
159 //             pwm_reg = 0x2C;
160 //             break;
161 //     }
162 //     pwm_reg += (y << 4) + x;
163 //     xprintf("  device: %02X\n", device);
164 //     xprintf("  control: %02X %02X\n", control_reg, control[matrix][y]);
165 //     xprintf("  pwm:     %02X %02X\n", pwm_reg, pwm);
166 //     writeRegister8(device, 0, control_reg, control[matrix][y]);
167 //     writeRegister8(device, 0, control_reg + 0x12, control[matrix][y]);
168 //     writeRegister8(device, 0, pwm_reg, pwm);
169 // }
170
171 void activateLED(uint8_t matrix, uint8_t cx, uint8_t cy, uint8_t pwm)
172 {
173     uint8_t device_addr = (matrix & 0x06) >> 1;
174     ISSIDeviceStruct *device = issi_devices[device_addr];
175     if(device == 0){
176         return;
177     }
178     // xprintf("activeLED: %02X %02X %02X %02X\n", matrix, cy, cx, pwm);
179     uint8_t x = cx - 1;  // funciton takes 1 based counts, but we need 0...
180     uint8_t y = cy - 1;  // creating them once for less confusion
181     uint8_t control_reg = (y << 1) | (matrix & 0x01);
182     if(pwm == 0){
183         cbi(device->led_ctrl[control_reg], x);
184         cbi(device->led_blink_ctrl[control_reg], x);
185      }else{
186         sbi(device->led_ctrl[control_reg], x);
187         sbi(device->led_blink_ctrl[control_reg], x);
188     }
189     uint8_t pwm_reg = 0;
190     switch(matrix & 0x01){
191         case 0:
192             pwm_reg = 0x00;
193             break;
194         case 1:
195             pwm_reg = 0x08;
196             break;
197     }
198     pwm_reg += (y << 4) + x;
199     // xprintf("  device_addr: %02X\n", device_addr);
200     // xprintf("  control: %02X %02X\n", control_reg, control[matrix][y]);
201     // xprintf("  pwm:     %02X %02X\n", pwm_reg, pwm);
202     // writeRegister8(device_addr, 0, control_reg, control[matrix][y]);
203     device->led_pwm[pwm_reg] = pwm;
204     device->led_dirty = 1;
205
206     // writeRegister8(device_addr, 0, control_reg + 0x12, control[matrix][y]);
207     // writeRegister8(device_addr, 0, pwm_reg, pwm);
208 }
209
210 void update_issi(uint8_t device_addr, uint8_t blocking)
211 {
212     // This seems to take about 6ms
213     ISSIDeviceStruct *device = issi_devices[device_addr];
214     if(device != 0){
215         if(device->fn_dirty){
216             device->fn_dirty = 0;
217             setFrame(device_addr, ISSI_BANK_FUNCTIONREG);
218             TWITransmitData(&device->fn_device_addr, sizeof(device->fn_registers) + 2, 0, 1);
219         }
220         if(device->led_dirty){
221             device->led_dirty = 0;
222             setFrame(device_addr, 0);
223             TWITransmitData(&device->led_device_addr, 0xB6, 0, blocking);
224         }
225     }
226 }
227
228 void issi_init(void)
229 {
230     // Set LED_EN/SDB high to enable the chip
231     xprintf("Enabing SDB on pin: %d\n", LED_EN_PIN);
232     _SFR_IO8((LED_EN_PIN >> 4) + 1) &= ~_BV(LED_EN_PIN & 0xF); // IN
233     _SFR_IO8((LED_EN_PIN >> 4) + 2) |=  _BV(LED_EN_PIN & 0xF); // HI
234     TWIInit();
235     for(uint8_t device_addr = 0; device_addr < 4; device_addr++){
236         xprintf("ISSI Init device: %d\n", device_addr);
237         // If this device has been previously allocated, free it
238         if(issi_devices[device_addr] != 0){
239             free(issi_devices[device_addr]);
240         }
241         // Try to shutdown the device, if this fails skip this device
242         writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x00);
243         while (!isTWIReady()){_delay_us(1);}
244         if(TWIInfo.errorCode != 0xFF){
245             xprintf("ISSI init failed %d %02X %02X\n", device_addr, TWIInfo.mode, TWIInfo.errorCode);
246             continue;
247         }
248         // Allocate the device structure - calloc zeros it for us
249         ISSIDeviceStruct *device = (ISSIDeviceStruct *)calloc(sizeof(ISSIDeviceStruct) * 2, 1);
250         issi_devices[device_addr] = device;
251         device->fn_device_addr = ISSI_ADDR_DEFAULT | device_addr << 1;
252         device->fn_register_addr = 0;
253         device->led_device_addr = ISSI_ADDR_DEFAULT | device_addr << 1;
254         device->led_register_addr = 0;
255         // set dirty bits so that all of the buffered data is written out
256         device->fn_dirty = 1;
257         device->led_dirty = 1;
258         update_issi(device_addr, 1);
259         // Set the function register to picture mode
260         // device->fn_reg[ISSI_REG_CONFIG] = ISSI_REG_CONFIG_PICTUREMODE;
261         writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x01);
262     }
263
264     // Shutdown and set all registers to 0
265     // writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x00);
266     // for(uint8_t bank = 0; bank <= 7; bank++){
267     //     for (uint8_t reg = 0x00; reg <= 0xB3; reg++) {
268     //         writeRegister8(device_addr, bank, reg, 0x00);
269     //     }
270     // }
271     // for (uint8_t reg = 0; reg <= 0x0C; reg++) {
272     //     writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, reg, 0x00);
273     // }
274     // writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_CONFIG, ISSI_REG_CONFIG_PICTUREMODE);
275     // writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x01);
276     // picture mode
277     // writeRegister8(ISSI_BANK_FUNCTIONREG, 0x01, 0x01);
278
279     //Enable blink
280     // writeRegister8(ISSI_BANK_FUNCTIONREG, 0x05, 0x48B);
281
282     //Enable Breath
283
284 }
285
286 #endif