1
0

kimera: Fix a bug of program memory const

This commit is contained in:
Kai Ryu 2014-05-30 19:52:06 +09:00
parent e939c9d7a6
commit 9365e69301
2 changed files with 40 additions and 34 deletions

View File

@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <stdbool.h> #include <stdbool.h>
#include <avr/eeprom.h> #include <avr/eeprom.h>
#include <util/delay.h>
#include "kimera.h" #include "kimera.h"
#include "debug.h" #include "debug.h"
@ -42,6 +43,7 @@ uint16_t shift_out_cache = 0;
void kimera_init(void) void kimera_init(void)
{ {
// read config // read config
write_matrix_mapping();
if (read_matrix_mapping()) { if (read_matrix_mapping()) {
write_matrix_mapping(); write_matrix_mapping();
} }
@ -70,9 +72,9 @@ uint8_t read_matrix_mapping(void)
return error; return error;
} }
for (uint8_t i = 0; i < MUX_COUNT; i++) { for (uint8_t mux = 0; mux < MUX_COUNT; mux++) {
mux_mapping[i] = mux_config & (1<<i); mux_mapping[mux] = mux_config & (1 << mux);
if (mux_mapping[i] == MUX_FOR_COL) { if (mux_mapping[mux] == MUX_FOR_COL) {
col_max_count += MUX_PORTS; col_max_count += MUX_PORTS;
} }
else { else {
@ -84,20 +86,20 @@ uint8_t read_matrix_mapping(void)
} }
uint8_t *mapping = EECONFIG_ROW_COL_MAPPING; uint8_t *mapping = EECONFIG_ROW_COL_MAPPING;
for (uint8_t i = 0; i < row_max_count; i++) { for (uint8_t row = 0; row < row_max_count; row++) {
row_mapping[i] = eeprom_read_byte(mapping++); row_mapping[row] = eeprom_read_byte(mapping++);
if (row_mapping[i] != UNCONFIGURED) { if (row_mapping[row] != UNCONFIGURED) {
if (mux_mapping[PX_TO_MUX(row_mapping[i])] != MUX_FOR_ROW) { if (mux_mapping[PX_TO_MUX(row_mapping[row])] != MUX_FOR_ROW) {
row_mapping[i] = UNCONFIGURED; row_mapping[row] = UNCONFIGURED;
error++; error++;
} }
} }
} }
for (uint8_t i = 0; i < col_max_count; i++) { for (uint8_t col = 0; col < col_max_count; col++) {
col_mapping[i] = eeprom_read_byte(mapping++); col_mapping[col] = eeprom_read_byte(mapping++);
if (col_mapping[i] != UNCONFIGURED) { if (col_mapping[col] != UNCONFIGURED) {
if (mux_mapping[PX_TO_MUX(col_mapping[i])] != MUX_FOR_COL) { if (mux_mapping[PX_TO_MUX(col_mapping[col])] != MUX_FOR_COL) {
col_mapping[i] = UNCONFIGURED; col_mapping[col] = UNCONFIGURED;
error++; error++;
} }
} }
@ -112,9 +114,9 @@ void write_matrix_mapping(void)
row_max_count = 0; row_max_count = 0;
col_max_count = 0; col_max_count = 0;
for (uint8_t i = 0; i < MUX_COUNT; i++) { for (uint8_t mux = 0; mux < MUX_COUNT; mux++) {
mux_config |= (mux_mapping[i] << i); mux_config |= (mux_mapping[mux] << mux);
if (mux_mapping[i] == MUX_FOR_COL) { if (mux_mapping[mux] == MUX_FOR_COL) {
col_max_count += MUX_PORTS; col_max_count += MUX_PORTS;
} }
else { else {
@ -124,11 +126,11 @@ void write_matrix_mapping(void)
eeprom_write_byte(EECONFIG_MUX_MAPPING, mux_config); eeprom_write_byte(EECONFIG_MUX_MAPPING, mux_config);
uint8_t *mapping = EECONFIG_ROW_COL_MAPPING; uint8_t *mapping = EECONFIG_ROW_COL_MAPPING;
for (uint8_t i = 0; i < row_max_count; i++) { for (uint8_t row = 0; row < row_max_count; row++) {
eeprom_write_byte(mapping++, row_mapping[i]); eeprom_write_byte(mapping++, row_mapping[row]);
} }
for (uint8_t i = 0; i < col_max_count; i++) { for (uint8_t col = 0; col < col_max_count; col++) {
eeprom_write_byte(mapping++, col_mapping[i]); eeprom_write_byte(mapping++, col_mapping[col]);
} }
} }
@ -145,14 +147,15 @@ void shift_out_word(uint16_t data)
void init_cols(void) void init_cols(void)
{ {
// init mux io pins // init mux io pins
for (uint8_t i = 0; i < MUX_COUNT; i++) { for (uint8_t mux = 0; mux < MUX_COUNT; mux++) {
if (mux_mapping[i] == MUX_FOR_COL) { uint8_t bit = MUX_TO_ZX_BIT(mux);
ZX_DDR &= ~(1 << zx_bit[i]); if (mux_mapping[mux] == MUX_FOR_COL) {
ZX_PORT |= (1 << zx_bit[i]); ZX_DDR &= ~(1 << bit);
ZX_PORT |= (1 << bit);
} }
else { else {
ZX_DDR |= (1 << zx_bit[i]); ZX_DDR |= (1 << bit);
ZX_PORT |= (1 << zx_bit[i]); ZX_PORT |= (1 << bit);
} }
} }
} }
@ -160,13 +163,13 @@ void init_cols(void)
matrix_row_t read_cols(void) matrix_row_t read_cols(void)
{ {
matrix_row_t cols = 0; matrix_row_t cols = 0;
for (uint8_t i = 0; i < MATRIX_COLS; i++) { for (uint8_t col = 0; col < MATRIX_COLS; col++) {
uint8_t px = col_mapping[i]; uint8_t px = col_mapping[col];
if (px != UNCONFIGURED) { if (px != UNCONFIGURED) {
shift_out_word(shift_out_cache | px_to_shift_out[px]); shift_out_word(shift_out_cache | PX_TO_SHIFT_OUT(px));
// TODO: delay _delay_us(10);
if (!(ZX_PIN & (1 << zx_bit[PX_TO_MUX(px)]))) { if (!(ZX_PIN & (1 << MUX_TO_ZX_BIT(PX_TO_MUX(px))))) {
cols |= (1 << i); cols |= (1 << col);
} }
} }
} }
@ -182,8 +185,8 @@ void select_row(uint8_t row)
{ {
uint8_t px = row_mapping[row]; uint8_t px = row_mapping[row];
if (px != UNCONFIGURED) { if (px != UNCONFIGURED) {
ZX_PORT &= ~(1 << zx_bit[PX_TO_MUX(px)]); ZX_PORT &= ~(1 << MUX_TO_ZX_BIT(PX_TO_MUX(row)));
shift_out_cache = px_to_shift_out[px] | mux_inh_to_shift_out[PX_TO_MUX(px)]; shift_out_cache = PX_TO_SHIFT_OUT(px) | MUX_INH_TO_SHIFT_OUT(PX_TO_MUX(px));
shift_out_word(shift_out_cache); shift_out_word(shift_out_cache);
} }
} }

View File

@ -89,6 +89,7 @@ const uint8_t PROGMEM zx_bit[] = {
PF5, PF6, PF7, PF4 PF5, PF6, PF7, PF4
}; };
#endif #endif
#define MUX_TO_ZX_BIT(x) (pgm_read_byte(zx_bit + (x)))
/* /*
Shift Register Multiplexer Shift Register Multiplexer
@ -153,6 +154,8 @@ const uint16_t PROGMEM mux_inh_to_shift_out[] = {
1<<MUX1_INH, 1<<MUX2_INH, 1<<MUX3_INH, 1<<MUX4_INH 1<<MUX1_INH, 1<<MUX2_INH, 1<<MUX3_INH, 1<<MUX4_INH
}; };
#endif #endif
#define PX_TO_SHIFT_OUT(x) (pgm_read_word(px_to_shift_out + (x)))
#define MUX_INH_TO_SHIFT_OUT(x) (pgm_read_word(mux_inh_to_shift_out + (x)))
/* Matrix Mapping in EEPROM */ /* Matrix Mapping in EEPROM */