diff options
author | Florian Didron <fdidron@users.noreply.github.com> | 2020-01-21 13:50:53 +0900 |
---|---|---|
committer | Florian Didron <fdidron@users.noreply.github.com> | 2020-02-26 10:15:12 +0900 |
commit | d3f23ecfbcd2a69714422ab68f346e51fcc2a6de (patch) | |
tree | c5c07493f4175738f623ca23f618682cc04b779a /drivers | |
parent | 0c676d6e471e3c1eabf3158fab5cb4645abd29ee (diff) |
Apollo (#246)
* Add External EEPROM driver
* Add keyboard
* Fix i2c address
* Revert i2c address for eeprom
* Reduce page size for 24LC128
* Disable External EEPROM for now
* Fix up RGB Matrix and layout
* Cleanup moonlander.c file
* Fix led array center
* Disable External EEPROM for now
* Cleanup and optimization of files
* Add and clean up mappings
* Enable "ZSA" defaults for Moonlander
* Update matrix to use i2c_readReg
* Disable render limit for rgb matrix
* Update pin controls to GPIO Commands
* Add proper changed mechanism
* Additional cleanup
* Run clang-format on moonlander files
* Align keymap
* Limit brightness for rgb matrix to 200
To prevent power draw issues
* Add USB-IF VID/PID
* Add RGB Matrix Sleep code
* Enable External EEPROM on Moonlander
* Port over Ergodox Expander error handling and retry code
* Re-init RGB Matrix drivers when half is reconnected
* Add threaded LED display
* Correct layer state settings after moonlander_led_task
* Update Moonlander to include Oryx configuration
* Add webUSB Stuff
* Update default keymap
* fix: merge conflict
Co-authored-by: Drashna Jaelre <drashna@live.com>
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/arm/i2c_master.c | 4 | ||||
-rw-r--r-- | drivers/eeprom/eeprom_driver.c | 73 | ||||
-rw-r--r-- | drivers/eeprom/eeprom_driver.h | 22 | ||||
-rw-r--r-- | drivers/eeprom/eeprom_i2c.c | 120 | ||||
-rw-r--r-- | drivers/eeprom/eeprom_i2c.h | 115 | ||||
-rw-r--r-- | drivers/eeprom/eeprom_transient.c | 52 | ||||
-rw-r--r-- | drivers/eeprom/eeprom_transient.h | 24 |
7 files changed, 410 insertions, 0 deletions
diff --git a/drivers/arm/i2c_master.c b/drivers/arm/i2c_master.c index 5a03b9f306..9ced3d2e3e 100644 --- a/drivers/arm/i2c_master.c +++ b/drivers/arm/i2c_master.c @@ -91,6 +91,10 @@ i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16 return chibios_to_qmk(&status); } +i2c_status_t i2c_transmit_receive(uint8_t address, uint8_t * tx_body, uint16_t tx_length, uint8_t * rx_body, uint16_t rx_length) { + return i2cMasterTransmitTimeout(&I2C_DRIVER, address/2, tx_body, tx_length, rx_body, rx_length, MS2ST(100)); +} + i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout) { i2c_address = devaddr; i2cStart(&I2C_DRIVER, &i2cconfig); diff --git a/drivers/eeprom/eeprom_driver.c b/drivers/eeprom/eeprom_driver.c new file mode 100644 index 0000000000..3835e5e9d0 --- /dev/null +++ b/drivers/eeprom/eeprom_driver.c @@ -0,0 +1,73 @@ +/* Copyright 2019 Nick Brassel (tzarc) + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#include <stdint.h> +#include <string.h> + +#include "eeprom_driver.h" + +uint8_t eeprom_read_byte(const uint8_t *addr) { + uint8_t ret; + eeprom_read_block(&ret, addr, 1); + return ret; +} + +uint16_t eeprom_read_word(const uint16_t *addr) { + uint16_t ret; + eeprom_read_block(&ret, addr, 2); + return ret; +} + +uint32_t eeprom_read_dword(const uint32_t *addr) { + uint32_t ret; + eeprom_read_block(&ret, addr, 4); + return ret; +} + +void eeprom_write_byte(uint8_t *addr, uint8_t value) { eeprom_write_block(&value, addr, 1); } + +void eeprom_write_word(uint16_t *addr, uint16_t value) { eeprom_write_block(&value, addr, 2); } + +void eeprom_write_dword(uint32_t *addr, uint32_t value) { eeprom_write_block(&value, addr, 4); } + +void eeprom_update_block(const void *buf, void *addr, size_t len) { + uint8_t read_buf[len]; + eeprom_read_block(read_buf, addr, len); + if (memcmp(buf, read_buf, len) != 0) { + eeprom_write_block(buf, addr, len); + } +} + +void eeprom_update_byte(uint8_t *addr, uint8_t value) { + uint8_t orig = eeprom_read_byte(addr); + if (orig != value) { + eeprom_write_byte(addr, value); + } +} + +void eeprom_update_word(uint16_t *addr, uint16_t value) { + uint16_t orig = eeprom_read_word(addr); + if (orig != value) { + eeprom_write_word(addr, value); + } +} + +void eeprom_update_dword(uint32_t *addr, uint32_t value) { + uint32_t orig = eeprom_read_dword(addr); + if (orig != value) { + eeprom_write_dword(addr, value); + } +} diff --git a/drivers/eeprom/eeprom_driver.h b/drivers/eeprom/eeprom_driver.h new file mode 100644 index 0000000000..74592bc8f0 --- /dev/null +++ b/drivers/eeprom/eeprom_driver.h @@ -0,0 +1,22 @@ +/* Copyright 2019 Nick Brassel (tzarc) + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "eeprom.h" + +void eeprom_driver_init(void); +void eeprom_driver_erase(void); diff --git a/drivers/eeprom/eeprom_i2c.c b/drivers/eeprom/eeprom_i2c.c new file mode 100644 index 0000000000..10bd4a1be1 --- /dev/null +++ b/drivers/eeprom/eeprom_i2c.c @@ -0,0 +1,120 @@ +/* Copyright 2019 Nick Brassel (tzarc) + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#include <stdint.h> +#include <string.h> + +/* + Note that the implementations of eeprom_XXXX_YYYY on AVR are normally + provided by avr-libc. The same functions are reimplemented below and are + rerouted to the external i2c equivalent. + + Seemingly, as this is compiled from within QMK, the object file generated + during the build overrides the avr-libc implementation during the linking + stage. + + On other platforms such as ARM, there are no provided implementations, so + there is nothing to override during linkage. +*/ + +#include "wait.h" +#include "i2c_master.h" +#include "eeprom.h" +#include "eeprom_i2c.h" + +// #define DEBUG_EEPROM_OUTPUT + +#ifdef DEBUG_EEPROM_OUTPUT +# include "print.h" +#endif // DEBUG_EEPROM_OUTPUT + +static inline void init_i2c_if_required(void) { + static int done = 0; + if (!done) { + i2c_init(); + done = 1; + } +} + +static inline void fill_target_address(uint8_t *buffer, const void *addr) { + intptr_t p = (intptr_t)addr; + for (int i = 0; i < EXTERNAL_EEPROM_ADDRESS_SIZE; ++i) { + buffer[EXTERNAL_EEPROM_ADDRESS_SIZE - 1 - i] = p & 0xFF; + p >>= 8; + } +} + +void eeprom_driver_init(void) {} + +void eeprom_driver_erase(void) { + uint8_t buf[EXTERNAL_EEPROM_PAGE_SIZE]; + memset(buf, 0x00, EXTERNAL_EEPROM_PAGE_SIZE); + for (intptr_t addr = 0; addr < EXTERNAL_EEPROM_BYTE_COUNT; addr += EXTERNAL_EEPROM_PAGE_SIZE) { + eeprom_write_block(buf, (void *)addr, EXTERNAL_EEPROM_PAGE_SIZE); + } +} + +void eeprom_read_block(void *buf, const void *addr, size_t len) { + uint8_t complete_packet[EXTERNAL_EEPROM_ADDRESS_SIZE]; + fill_target_address(complete_packet, addr); + + init_i2c_if_required(); + i2c_transmit(EXTERNAL_EEPROM_I2C_ADDRESS((intptr_t)addr), complete_packet, EXTERNAL_EEPROM_ADDRESS_SIZE, 100); + i2c_receive(EXTERNAL_EEPROM_I2C_ADDRESS((intptr_t)addr), buf, len, 100); + +#ifdef DEBUG_EEPROM_OUTPUT + uprintf("[EEPROM R] 0x%04X: ", ((int)addr)); + for (size_t i = 0; i < len; ++i) { + uprintf(" %02X", (int)(((uint8_t *)buf)[i])); + } + uprintf("\n"); +#endif // DEBUG_EEPROM_OUTPUT +} + +void eeprom_write_block(const void *buf, void *addr, size_t len) { + uint8_t complete_packet[EXTERNAL_EEPROM_ADDRESS_SIZE + EXTERNAL_EEPROM_PAGE_SIZE]; + uint8_t *read_buf = (uint8_t *)buf; + intptr_t target_addr = (intptr_t)addr; + + init_i2c_if_required(); + while (len > 0) { + intptr_t page_offset = target_addr % EXTERNAL_EEPROM_PAGE_SIZE; + int write_length = EXTERNAL_EEPROM_PAGE_SIZE - page_offset; + if (write_length > len) { + write_length = len; + } + + fill_target_address(complete_packet, (const void *)target_addr); + for (uint8_t i = 0; i < write_length; i++) { + complete_packet[EXTERNAL_EEPROM_ADDRESS_SIZE + i] = read_buf[i]; + } + +#ifdef DEBUG_EEPROM_OUTPUT + uprintf("[EEPROM W] 0x%04X: ", ((int)target_addr)); + for (uint8_t i = 0; i < write_length; i++) { + uprintf(" %02X", (int)(read_buf[i])); + } + uprintf("\n"); +#endif // DEBUG_EEPROM_OUTPUT + + i2c_transmit(EXTERNAL_EEPROM_I2C_ADDRESS((intptr_t)addr), complete_packet, EXTERNAL_EEPROM_ADDRESS_SIZE + write_length, 100); + wait_ms(EXTERNAL_EEPROM_WRITE_TIME); + + read_buf += write_length; + target_addr += write_length; + len -= write_length; + } +} diff --git a/drivers/eeprom/eeprom_i2c.h b/drivers/eeprom/eeprom_i2c.h new file mode 100644 index 0000000000..51bce825b0 --- /dev/null +++ b/drivers/eeprom/eeprom_i2c.h @@ -0,0 +1,115 @@ +/* Copyright 2019 Nick Brassel (tzarc) + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#pragma once + +/* + Default device configurations: + + For the Sparkfun Qwiic I2C EEPROM module: https://www.sparkfun.com/products/14764 + #define EEPROM_I2C_CAT24C512 // (part number 24512A) + #define EEPROM_I2C_RM24C512C // (part number 24512C) + + For the Sparkfun I2C EEPROM chip: https://www.sparkfun.com/products/525 + #define EEPROM_I2C_24LC256 + + For the Adafruit I2C FRAM chip: https://www.adafruit.com/product/1895 + #define EEPROM_I2C_MB85RC256V +*/ +#if defined(EEPROM_I2C_CAT24C512) +# define EXTERNAL_EEPROM_BYTE_COUNT 65536 +# define EXTERNAL_EEPROM_PAGE_SIZE 128 +# define EXTERNAL_EEPROM_ADDRESS_SIZE 2 +# define EXTERNAL_EEPROM_WRITE_TIME 5 +#elif defined(EEPROM_I2C_RM24C512C) +# define EXTERNAL_EEPROM_BYTE_COUNT 65536 +# define EXTERNAL_EEPROM_PAGE_SIZE 128 +# define EXTERNAL_EEPROM_ADDRESS_SIZE 2 +# define EXTERNAL_EEPROM_WRITE_TIME 3 +#elif defined(EEPROM_I2C_24LC256) +# define EXTERNAL_EEPROM_BYTE_COUNT 32768 +# define EXTERNAL_EEPROM_PAGE_SIZE 64 +# define EXTERNAL_EEPROM_ADDRESS_SIZE 2 +# define EXTERNAL_EEPROM_WRITE_TIME 5 +#elif defined(EEPROM_I2C_24LC128) +# define EXTERNAL_EEPROM_BYTE_COUNT 16384 +# define EXTERNAL_EEPROM_PAGE_SIZE 64 +# define EXTERNAL_EEPROM_ADDRESS_SIZE 2 +# define EXTERNAL_EEPROM_WRITE_TIME 5 +#elif defined(EEPROM_I2C_MB85RC256V) +# define EXTERNAL_EEPROM_BYTE_COUNT 32768 +# define EXTERNAL_EEPROM_PAGE_SIZE 128 +# define EXTERNAL_EEPROM_ADDRESS_SIZE 2 +# define EXTERNAL_EEPROM_WRITE_TIME 0 +#endif + +/* + The base I2C address of the EEPROM. + This needs to be shifted up by 1, to match i2c_master requirements. +*/ +#ifndef EXTERNAL_EEPROM_I2C_BASE_ADDRESS +# define EXTERNAL_EEPROM_I2C_BASE_ADDRESS 0b10100000 +#endif + +/* + The calculated I2C address based on the input memory location. + + For EEPROM chips that embed part of the memory location in the I2C address + such as AT24M02 you can use something similar to the following (ensuring the + result is shifted by left by 1): + + #define EXTERNAL_EEPROM_I2C_ADDRESS(loc) \ + (EXTERNAL_EEPROM_I2C_BASE_ADDRESS | ((((loc) >> 16) & 0x07) << 1)) + +*/ +#ifndef EXTERNAL_EEPROM_I2C_ADDRESS +# define EXTERNAL_EEPROM_I2C_ADDRESS(loc) (EXTERNAL_EEPROM_I2C_BASE_ADDRESS) +#endif + +/* + The total size of the EEPROM, in bytes. The EEPROM datasheet will usually + specify this value in kbits, and will require conversion to bytes. +*/ +#ifndef EXTERNAL_EEPROM_BYTE_COUNT +# define EXTERNAL_EEPROM_BYTE_COUNT 8192 +#endif + +/* + The page size in bytes of the EEPROM, as specified in the datasheet. +*/ +#ifndef EXTERNAL_EEPROM_PAGE_SIZE +# define EXTERNAL_EEPROM_PAGE_SIZE 32 +#endif + +/* + The address size in bytes of the EEPROM. For EEPROMs with <=256 bytes, this + will likely be 1. For EEPROMs >256 and <=65536, this will be 2. For EEPROMs + >65536, this will likely need to be 2 with the modified variant of + EXTERNAL_EEPROM_I2C_ADDRESS above. + + As expected, consult the datasheet for specifics of your EEPROM. +*/ +#ifndef EXTERNAL_EEPROM_ADDRESS_SIZE +# define EXTERNAL_EEPROM_ADDRESS_SIZE 2 +#endif + +/* + The write cycle time of the EEPROM in milliseconds, as specified in the + datasheet. +*/ +#ifndef EXTERNAL_EEPROM_WRITE_TIME +# define EXTERNAL_EEPROM_WRITE_TIME 5 +#endif diff --git a/drivers/eeprom/eeprom_transient.c b/drivers/eeprom/eeprom_transient.c new file mode 100644 index 0000000000..318a827900 --- /dev/null +++ b/drivers/eeprom/eeprom_transient.c @@ -0,0 +1,52 @@ +/* Copyright 2019 Nick Brassel (tzarc) + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#include <stdint.h> +#include <string.h> + +#include "eeprom_driver.h" +#include "eeprom_transient.h" + +static uint8_t transientBuffer[TRANSIENT_EEPROM_SIZE] = {0}; + +size_t clamp_length(intptr_t offset, size_t len) { + if (offset + len > TRANSIENT_EEPROM_SIZE) { + len = TRANSIENT_EEPROM_SIZE - offset; + } + + return len; +} + +void eeprom_driver_init(void) { eeprom_driver_erase(); } + +void eeprom_driver_erase(void) { memset(transientBuffer, 0x00, TRANSIENT_EEPROM_SIZE); } + +void eeprom_read_block(void *buf, const void *addr, size_t len) { + intptr_t offset = (intptr_t)addr; + memset(buf, 0x00, len); + len = clamp_length(offset, len); + if (len > 0) { + memcpy(buf, &transientBuffer[offset], len); + } +} + +void eeprom_write_block(const void *buf, void *addr, size_t len) { + intptr_t offset = (intptr_t)addr; + len = clamp_length(offset, len); + if (len > 0) { + memcpy(&transientBuffer[offset], buf, len); + } +} diff --git a/drivers/eeprom/eeprom_transient.h b/drivers/eeprom/eeprom_transient.h new file mode 100644 index 0000000000..a739fe393a --- /dev/null +++ b/drivers/eeprom/eeprom_transient.h @@ -0,0 +1,24 @@ +/* Copyright 2019 Nick Brassel (tzarc) + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#pragma once + +/* + The size of the transient EEPROM buffer size. +*/ +#ifndef TRANSIENT_EEPROM_SIZE +# define TRANSIENT_EEPROM_SIZE 64 +#endif |