aboutsummaryrefslogtreecommitdiffstats
path: root/keyboards/meira/issi.c
blob: 600a465ba3cac5c8e17fc13f3491f2cdc856709c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#ifdef ISSI_ENABLE

#include <stdlib.h>
#include <stdint.h>
#include <util/delay.h>
#include <avr/sfr_defs.h>
#include <avr/io.h>
#include <util/twi.h>
#include "issi.h"
#include "print.h"
#include "TWIlib.h"

#define ISSI_ADDR_DEFAULT 0xE8

#define ISSI_REG_CONFIG 0x00
#define ISSI_REG_CONFIG_PICTUREMODE 0x00
#define ISSI_REG_CONFIG_AUTOPLAYMODE 0x08

#define ISSI_CONF_PICTUREMODE 0x00
#define ISSI_CONF_AUTOFRAMEMODE 0x04
#define ISSI_CONF_AUDIOMODE 0x08

#define ISSI_REG_PICTUREFRAME 0x01

#define ISSI_REG_SHUTDOWN 0x0A
#define ISSI_REG_AUDIOSYNC 0x06

#define ISSI_COMMANDREGISTER 0xFD
#define ISSI_BANK_FUNCTIONREG 0x0B // helpfully called 'page nine'
uint8_t control[8][9] = {
    {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0},
};
ISSIDeviceStruct *issi_devices[4] = {0, 0, 0, 0};

#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif

#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

#define I2C_WRITE 0
#define F_SCL 400000UL // SCL frequency
#define Prescaler 1
#define TWBR_val ((((F_CPU / F_SCL) / Prescaler) - 16 ) / 2)

uint8_t i2c_start(uint8_t address)
{
    // reset TWI control register
    TWCR = 0;
    // transmit START condition
    TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
    // wait for end of transmission
    while( !(TWCR & (1<<TWINT)) );

    // check if the start condition was successfully transmitted
    if((TWSR & 0xF8) != TW_START){ return 1; }

    // load slave address into data register
    TWDR = address;
    // start transmission of address
    TWCR = (1<<TWINT) | (1<<TWEN);
    // wait for end of transmission
    while( !(TWCR & (1<<TWINT)) );

    // check if the device has acknowledged the READ / WRITE mode
    uint8_t twst = TW_STATUS & 0xF8;
    if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;

    return 0;
}

uint8_t i2c_write(uint8_t data)
{
    // load data into data register
    TWDR = data;
    // start transmission of data
    TWCR = (1 << TWINT) | (1 << TWEN);
    // wait for end of transmission
    while (!(TWCR & (1 << TWINT)))
        ;

    if ((TWSR & 0xF8) != TW_MT_DATA_ACK) {
        return 1;
    }
    return 0;
}

uint8_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length)
{
    TWBR = (uint8_t)TWBR_val;
    if (i2c_start(address | I2C_WRITE))
        return 1;
    for (uint16_t i = 0; i < length; i++) {
        if (i2c_write(data[i]))
            return 1;
    }
    // transmit STOP condition
    TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
    return 0;
}

void setFrame(uint8_t device, uint8_t frame)
{
    static uint8_t current_frame = -1;
    if(current_frame != frame){
        uint8_t payload[] = {
            ISSI_ADDR_DEFAULT | device << 1,
            ISSI_COMMANDREGISTER,
            frame
        };
        TWITransmitData(payload, sizeof(payload), 0, 1);
    }
    // static uint8_t current_frame = 0xFF;
    // if(current_frame == frame){
    //     // return;
    // }
    // uint8_t payload[2] = { ISSI_COMMANDREGISTER, frame };
    // i2c_transmit(ISSI_ADDR_DEFAULT | device << 1, payload, 2);
    // current_frame = frame;
}

void writeRegister8(uint8_t device, uint8_t frame, uint8_t reg, uint8_t data)
{
    // Set the frame
    setFrame(device, frame);

    // Write to the register
    uint8_t payload[] = {
        ISSI_ADDR_DEFAULT | device << 1,
        reg,
        data
    };
    TWITransmitData(payload, sizeof(payload), 0, 1);
}

// void activateLED(uint8_t matrix, uint8_t cx, uint8_t cy, uint8_t pwm)
// {
//     xprintf("activeLED: %02X %02X %02X %02X\n", matrix, cy, cx, pwm);
//     uint8_t x = cx - 1;  // funciton takes 1 based counts, but we need 0...
//     uint8_t y = cy - 1;  // creating them once for less confusion
//     if(pwm == 0){
//         cbi(control[matrix][y], x);
//     }else{
//         sbi(control[matrix][y], x);
//     }
//     uint8_t device = (matrix & 0x06) >> 1;
//     uint8_t control_reg = (y << 1) | (matrix & 0x01);
//     uint8_t pwm_reg = 0;
//     switch(matrix & 0x01){
//         case 0:
//             pwm_reg = 0x24;
//             break;
//         case 1:
//             pwm_reg = 0x2C;
//             break;
//     }
//     pwm_reg += (y << 4) + x;
//     xprintf("  device: %02X\n", device);
//     xprintf("  control: %02X %02X\n", control_reg, control[matrix][y]);
//     xprintf("  pwm:     %02X %02X\n", pwm_reg, pwm);
//     writeRegister8(device, 0, control_reg, control[matrix][y]);
//     writeRegister8(device, 0, control_reg + 0x12, control[matrix][y]);
//     writeRegister8(device, 0, pwm_reg, pwm);
// }

void activateLED(uint8_t matrix, uint8_t cx, uint8_t cy, uint8_t pwm)
{
    uint8_t device_addr = (matrix & 0x06) >> 1;
    ISSIDeviceStruct *device = issi_devices[device_addr];
    if(device == 0){
        return;
    }
    // xprintf("activeLED: %02X %02X %02X %02X\n", matrix, cy, cx, pwm);
    uint8_t x = cx - 1;  // funciton takes 1 based counts, but we need 0...
    uint8_t y = cy - 1;  // creating them once for less confusion
    uint8_t control_reg = (y << 1) | (matrix & 0x01);
    if(pwm == 0){
        cbi(device->led_ctrl[control_reg], x);
        cbi(device->led_blink_ctrl[control_reg], x);
     }else{
        sbi(device->led_ctrl[control_reg], x);
        sbi(device->led_blink_ctrl[control_reg], x);
    }
    uint8_t pwm_reg = 0;
    switch(matrix & 0x01){
        case 0:
            pwm_reg = 0x00;
            break;
        case 1:
            pwm_reg = 0x08;
            break;
    }
    pwm_reg += (y << 4) + x;
    // xprintf("  device_addr: %02X\n", device_addr);
    // xprintf("  control: %02X %02X\n", control_reg, control[matrix][y]);
    // xprintf("  pwm:     %02X %02X\n", pwm_reg, pwm);
    // writeRegister8(device_addr, 0, control_reg, control[matrix][y]);
    device->led_pwm[pwm_reg] = pwm;
    device->led_dirty = 1;

    // writeRegister8(device_addr, 0, control_reg + 0x12, control[matrix][y]);
    // writeRegister8(device_addr, 0, pwm_reg, pwm);
}

void update_issi(uint8_t device_addr, uint8_t blocking)
{
    // This seems to take about 6ms
    ISSIDeviceStruct *device = issi_devices[device_addr];
    if(device != 0){
        if(device->fn_dirty){
            device->fn_dirty = 0;
            setFrame(device_addr, ISSI_BANK_FUNCTIONREG);
            TWITransmitData(&device->fn_device_addr, sizeof(device->fn_registers) + 2, 0, 1);
        }
        if(device->led_dirty){
            device->led_dirty = 0;
            setFrame(device_addr, 0);
            TWITransmitData(&device->led_device_addr, 0xB6, 0, blocking);
        }
    }
}

void issi_init(void)
{
    // Set LED_EN/SDB high to enable the chip
    xprintf("Enabing SDB on pin: %d\n", LED_EN_PIN);
    _SFR_IO8((LED_EN_PIN >> 4) + 1) &= ~_BV(LED_EN_PIN & 0xF); // IN
    _SFR_IO8((LED_EN_PIN >> 4) + 2) |=  _BV(LED_EN_PIN & 0xF); // HI
    TWIInit();
    for(uint8_t device_addr = 0; device_addr < 4; device_addr++){
        xprintf("ISSI Init device: %d\n", device_addr);
        // If this device has been previously allocated, free it
        if(issi_devices[device_addr] != 0){
            free(issi_devices[device_addr]);
        }
        // Try to shutdown the device, if this fails skip this device
        writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x00);
        while (!isTWIReady()){_delay_us(1);}
        if(TWIInfo.errorCode != 0xFF){
            xprintf("ISSI init failed %d %02X %02X\n", device_addr, TWIInfo.mode, TWIInfo.errorCode);
            continue;
        }
        // Allocate the device structure - calloc zeros it for us
        ISSIDeviceStruct *device = (ISSIDeviceStruct *)calloc(sizeof(ISSIDeviceStruct) * 2, 1);
        issi_devices[device_addr] = device;
        device->fn_device_addr = ISSI_ADDR_DEFAULT | device_addr << 1;
        device->fn_register_addr = 0;
        device->led_device_addr = ISSI_ADDR_DEFAULT | device_addr << 1;
        device->led_register_addr = 0;
        // set dirty bits so that all of the buffered data is written out
        device->fn_dirty = 1;
        device->led_dirty = 1;
        update_issi(device_addr, 1);
        // Set the function register to picture mode
        // device->fn_reg[ISSI_REG_CONFIG] = ISSI_REG_CONFIG_PICTUREMODE;
        writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x01);
    }

    // Shutdown and set all registers to 0
    // writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x00);
    // for(uint8_t bank = 0; bank <= 7; bank++){
    //     for (uint8_t reg = 0x00; reg <= 0xB3; reg++) {
    //         writeRegister8(device_addr, bank, reg, 0x00);
    //     }
    // }
    // for (uint8_t reg = 0; reg <= 0x0C; reg++) {
    //     writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, reg, 0x00);
    // }
    // writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_CONFIG, ISSI_REG_CONFIG_PICTUREMODE);
    // writeRegister8(device_addr, ISSI_BANK_FUNCTIONREG, ISSI_REG_SHUTDOWN, 0x01);
    // picture mode
    // writeRegister8(ISSI_BANK_FUNCTIONREG, 0x01, 0x01);

    //Enable blink
    // writeRegister8(ISSI_BANK_FUNCTIONREG, 0x05, 0x48B);

    //Enable Breath

}

#endif
span class="n">i < bit; i++ ) { serial_delay_half1(); // read the middle of pulses if( serial_read_pin() ) { byte = (byte << 1) | 1; p ^= 1; } else { byte = (byte << 1) | 0; p ^= 0; } _delay_sub_us(READ_WRITE_WIDTH_ADJUST); serial_delay_half2(); } /* recive parity bit */ serial_delay_half1(); // read the middle of pulses pb = serial_read_pin(); _delay_sub_us(READ_WRITE_WIDTH_ADJUST); serial_delay_half2(); *pterrcount += (p != pb)? 1 : 0; return byte; } // Sends a byte with MSB ordering void serial_write_chunk(uint8_t data, uint8_t bit) NO_INLINE; void serial_write_chunk(uint8_t data, uint8_t bit) { uint8_t b, p; for( p = PARITY, b = 1<<(bit-1); b ; b >>= 1) { if(data & b) { serial_high(); p ^= 1; } else { serial_low(); p ^= 0; } serial_delay(); } /* send parity bit */ if(p & 1) { serial_high(); } else { serial_low(); } serial_delay(); serial_low(); // sync_send() / senc_recv() need raise edge } static void serial_send_packet(uint8_t *buffer, uint8_t size) NO_INLINE; static void serial_send_packet(uint8_t *buffer, uint8_t size) { for (uint8_t i = 0; i < size; ++i) { uint8_t data; data = buffer[i]; sync_send(); serial_write_chunk(data,8); } } static uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) NO_INLINE; static uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) { uint8_t pecount = 0; for (uint8_t i = 0; i < size; ++i) { uint8_t data; sync_recv(); data = serial_read_chunk(&pecount, 8); buffer[i] = data; } return pecount == 0; } inline static void change_sender2reciver(void) { sync_send(); //0 serial_delay_half1(); //1 serial_low(); //2 serial_input_with_pullup(); //2 serial_delay_half1(); //3 } inline static void change_reciver2sender(void) { sync_recv(); //0 serial_delay(); //1 serial_low(); //3 serial_output(); //3 serial_delay_half1(); //4 } static inline uint8_t nibble_bits_count(uint8_t bits) { bits = (bits & 0x5) + (bits >> 1 & 0x5); bits = (bits & 0x3) + (bits >> 2 & 0x3); return bits; } // interrupt handle to be used by the target device ISR(SERIAL_PIN_INTERRUPT) { #ifndef SERIAL_USE_MULTI_TRANSACTION serial_low(); serial_output(); SSTD_t *trans = Transaction_table; #else // recive transaction table index uint8_t tid, bits; uint8_t pecount = 0; sync_recv(); bits = serial_read_chunk(&pecount,7); tid = bits>>3; bits = (bits&7) != nibble_bits_count(tid); if( bits || pecount> 0 || tid > Transaction_table_size ) { return; } serial_delay_half1(); serial_high(); // response step1 low->high serial_output(); _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT*SLAVE_INT_ACK_WIDTH); SSTD_t *trans = &Transaction_table[tid]; serial_low(); // response step2 ack high->low #endif // target send phase if( trans->target2initiator_buffer_size > 0 ) serial_send_packet((uint8_t *)trans->target2initiator_buffer, trans->target2initiator_buffer_size); // target switch to input change_sender2reciver(); // target recive phase if( trans->initiator2target_buffer_size > 0 ) { if (serial_recive_packet((uint8_t *)trans->initiator2target_buffer, trans->initiator2target_buffer_size) ) { *trans->status = TRANSACTION_ACCEPTED; } else { *trans->status = TRANSACTION_DATA_ERROR; } } else { *trans->status = TRANSACTION_ACCEPTED; } sync_recv(); //weit initiator output to high } ///////// // start transaction by initiator // // int soft_serial_transaction(int sstd_index) // // Returns: // TRANSACTION_END // TRANSACTION_NO_RESPONSE // TRANSACTION_DATA_ERROR // this code is very time dependent, so we need to disable interrupts #ifndef SERIAL_USE_MULTI_TRANSACTION int soft_serial_transaction(void) { SSTD_t *trans = Transaction_table; #else int soft_serial_transaction(int sstd_index) { if( sstd_index > Transaction_table_size ) return TRANSACTION_TYPE_ERROR; SSTD_t *trans = &Transaction_table[sstd_index]; #endif cli(); // signal to the target that we want to start a transaction serial_output(); serial_low(); _delay_us(SLAVE_INT_WIDTH_US); #ifndef SERIAL_USE_MULTI_TRANSACTION // wait for the target response serial_input_with_pullup(); _delay_us(SLAVE_INT_RESPONSE_TIME); // check if the target is present if (serial_read_pin()) { // target failed to pull the line low, assume not present serial_output(); serial_high(); *trans->status = TRANSACTION_NO_RESPONSE; sei(); return TRANSACTION_NO_RESPONSE; } #else // send transaction table index int tid = (sstd_index<<3) | (7 & nibble_bits_count(sstd_index)); sync_send(); _delay_sub_us(TID_SEND_ADJUST); serial_write_chunk(tid, 7); serial_delay_half1(); // wait for the target response (step1 low->high) serial_input_with_pullup(); while( !serial_read_pin() ) { _delay_sub_us(2); } // check if the target is present (step2 high->low) for( int i = 0; serial_read_pin(); i++ ) { if (i > SLAVE_INT_ACK_WIDTH + 1) { // slave failed to pull the line low, assume not present serial_output(); serial_high(); *trans->status = TRANSACTION_NO_RESPONSE; sei(); return TRANSACTION_NO_RESPONSE; } _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT); } #endif // initiator recive phase // if the target is present syncronize with it if( trans->target2initiator_buffer_size > 0 ) { if (!serial_recive_packet((uint8_t *)trans->target2initiator_buffer, trans->target2initiator_buffer_size) ) { serial_output(); serial_high(); *trans->status = TRANSACTION_DATA_ERROR; sei(); return TRANSACTION_DATA_ERROR; } } // initiator switch to output change_reciver2sender(); // initiator send phase if( trans->initiator2target_buffer_size > 0 ) { serial_send_packet((uint8_t *)trans->initiator2target_buffer, trans->initiator2target_buffer_size); } // always, release the line when not in use sync_send(); *trans->status = TRANSACTION_END; sei(); return TRANSACTION_END; } #ifdef SERIAL_USE_MULTI_TRANSACTION int soft_serial_get_and_clean_status(int sstd_index) { SSTD_t *trans = &Transaction_table[sstd_index]; cli(); int retval = *trans->status; *trans->status = 0;; sei(); return retval; } #endif #endif // Helix serial.c history // 2018-1-29 fork from let's split and add PD2, modify sync_recv() (#2308, bceffdefc) // 2018-6-28 bug fix master to slave comm and speed up (#3255, 1038bbef4) // (adjusted with avr-gcc 4.9.2) // 2018-7-13 remove USE_SERIAL_PD2 macro (#3374, f30d6dd78) // (adjusted with avr-gcc 4.9.2) // 2018-8-11 add support multi-type transaction (#3608, feb5e4aae) // (adjusted with avr-gcc 4.9.2) // 2018-10-21 fix serial and RGB animation conflict (#4191, 4665e4fff) // (adjusted with avr-gcc 7.3.0) // 2018-10-28 re-adjust compiler depend value of delay (#4269, 8517f8a66) // (adjusted with avr-gcc 5.4.0, 7.3.0)