/* Copyright 2014 Kai Ryu This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #define KIMERA_C #include #include #include #include #include #include "action.h" #include "suspend.h" #include "i2cmaster.h" #include "kimera.h" #include "debug.h" #define SCL_CLOCK 400000L #define SCL_DURATION (1000000L/SCL_CLOCK)/2 extern uint8_t i2c_force_stop; uint8_t row_mapping[PX_COUNT] = { #ifndef TWO_HEADED_KIMERA 0, 1, 2, 3, 4, 5, 6, 7, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED #else 0, 1, 2, 3, 4, 5, 6, 7, 32, 33, 34, 35, 36, 37, 38, 39, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED #endif }; uint8_t col_mapping[PX_COUNT] = { #ifndef TWO_HEADED_KIMERA 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED #else 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55 #endif }; #ifndef TWO_HEADED_KIMERA uint8_t row_count = 8; uint8_t col_count = 24; #else uint8_t row_count = 16; uint8_t col_count = 32; #endif uint8_t data[EXP_COUNT][EXP_PORT_COUNT]; uint8_t exp_status = 0; void kimera_init(void) { /* read config */ //write_matrix_mapping(); /* debug */ if (read_matrix_mapping()) { write_matrix_mapping(); } /* init i2c */ i2c_init(); /* init i/o expanders */ kimera_scan(); /* init watch dog */ wdt_init(); } void wdt_init(void) { cli(); wdt_reset(); wdt_intr_enable(WDTO_1S); sei(); } uint8_t read_matrix_mapping(void) { uint8_t error = 0; /* read number of rows and cols */ row_count = eeprom_read_byte(EECONFIG_ROW_COUNT); col_count = eeprom_read_byte(EECONFIG_COL_COUNT); if (row_count == 0) error++; if (row_count == UNCONFIGURED) error++; if (col_count == 0) error++; if (col_count == UNCONFIGURED) error++; if (row_count + col_count > PX_COUNT) error++; /* read row mapping */ uint8_t *mapping = EECONFIG_ROW_COL_MAPPING; for (uint8_t i = 0; i < PX_COUNT; i++) { if (i < row_count) { row_mapping[i] = eeprom_read_byte(mapping++); if (row_mapping[i] >= PX_COUNT) error++; } else { row_mapping[i] = UNCONFIGURED; } } /* read col mapping*/ for (uint8_t i = 0; i < PX_COUNT; i++) { if (i < col_count) { col_mapping[i] = eeprom_read_byte(mapping++); if (col_mapping[i] >= PX_COUNT) error++; } else { col_mapping[i] = UNCONFIGURED; } } return error; } void write_matrix_mapping(void) { /* write number of rows and cols */ eeprom_write_byte(EECONFIG_ROW_COUNT, row_count); eeprom_write_byte(EECONFIG_COL_COUNT, col_count); /* write row mapping */ uint8_t *mapping = EECONFIG_ROW_COL_MAPPING; for (uint8_t row = 0; row < row_count; row++) { eeprom_write_byte(mapping++, row_mapping[row]); } /* write col mapping */ for (uint8_t col = 0; col < col_count; col++) { eeprom_write_byte(mapping++, col_mapping[col]); } } void kimera_scan(void) { wdt_reset(); uint8_t ret; for (uint8_t exp = 0; exp < EXP_COUNT; exp++) { ret = i2c_start(EXP_ADDR(exp) | I2C_READ); if (exp_status & (1<