This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
forked from qmk/qmk_firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add persistent led support with eeprom (qmk#9)
* adding HT32 support to chibios SPI master driver * add support for W25X20CL SPI eeprom * add makefile flag for eeprom feature * add spi support to keyboard startup and config * example keymap using eeprom profile loading
- Loading branch information
Showing
7 changed files
with
590 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
/* Copyright 2020 Nick Brassel (tzarc) and tech2077 | ||
* | ||
* 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 SPI 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 "spi_master.h" | ||
#include "eeprom.h" | ||
#include "eeprom_w25x20cl.h" | ||
|
||
#define CMD_WREN 0x06u | ||
#define CMD_WRDI 0x04u | ||
#define CMD_RDSR 0x05u | ||
#define CMD_WRSR 0x01u | ||
#define CMD_READ 0x03u | ||
#define CMD_WRITE 0x02u | ||
#define CMD_SECTOR_ERASE 0x20u | ||
|
||
#define SR_WIP 0x01u | ||
|
||
// #define DEBUG_EEPROM_OUTPUT | ||
|
||
#ifndef EXTERNAL_EEPROM_SPI_TIMEOUT | ||
# define EXTERNAL_EEPROM_SPI_TIMEOUT 100 | ||
#endif | ||
|
||
#if defined(CONSOLE_ENABLE) && defined(DEBUG_EEPROM_OUTPUT) | ||
# include "timer.h" | ||
# include "debug.h" | ||
#endif // CONSOLE_ENABLE | ||
|
||
bool spi_eeprom_start(void) { | ||
return spi_start(EXTERNAL_EEPROM_SPI_SLAVE_SELECT_PIN, EXTERNAL_EEPROM_SPI_LSBFIRST, EXTERNAL_EEPROM_SPI_MODE, EXTERNAL_EEPROM_SPI_CLOCK_DIVISOR); | ||
} | ||
|
||
static spi_status_t spi_eeprom_wait_while_busy(int timeout) { | ||
uint32_t deadline = timer_read32() + timeout; | ||
spi_status_t response; | ||
spi_write(CMD_RDSR); | ||
do { | ||
response = spi_read(); | ||
if (timer_read32() >= deadline) { | ||
return SPI_STATUS_TIMEOUT; | ||
} | ||
} while ((uint16_t)response & SR_WIP); | ||
return SPI_STATUS_SUCCESS; | ||
} | ||
//---------------------------------------------------------------------------------------------------------------------- | ||
|
||
void eeprom_erase(uint32_t addr) { | ||
#if defined(CONSOLE_ENABLE) && defined(DEBUG_EEPROM_OUTPUT) | ||
uint32_t start = timer_read32(); | ||
#endif | ||
|
||
spi_eeprom_start(); | ||
spi_write(CMD_WREN); | ||
spi_stop(); | ||
|
||
spi_eeprom_start(); | ||
spi_write(CMD_SECTOR_ERASE); | ||
spi_write((uint8_t)((addr & 0xFF0000u) >> 16u)); | ||
spi_write((uint8_t)((addr & 0x00FF00u) >> 8u)); | ||
spi_write((uint8_t)((addr & 0x0000FFu))); | ||
spi_stop(); | ||
|
||
spi_eeprom_start(); | ||
spi_status_t response = spi_eeprom_wait_while_busy(10000); | ||
spi_stop(); | ||
if (response == SPI_STATUS_TIMEOUT) { | ||
dprint("SPI timeout for WIP check\n"); | ||
return; | ||
} | ||
|
||
#if defined(CONSOLE_ENABLE) && defined(DEBUG_EEPROM_OUTPUT) | ||
dprintf("EEPROM erase took %ldms to complete\n", ((long)(timer_read32() - start))); | ||
#endif | ||
} | ||
|
||
void eeprom_read(void *buf, uint32_t addr, size_t len) { | ||
//------------------------------------------------- | ||
// Wait for the write-in-progress bit to be cleared | ||
bool res = spi_eeprom_start(); | ||
if (!res) { | ||
dprint("failed to start SPI for WIP check\n"); | ||
spi_stop(); | ||
return; | ||
} | ||
|
||
spi_status_t response = spi_eeprom_wait_while_busy(EXTERNAL_EEPROM_SPI_TIMEOUT); | ||
spi_stop(); | ||
if (response == SPI_STATUS_TIMEOUT) { | ||
dprint("SPI timeout for WIP check\n"); | ||
spi_stop(); | ||
return; | ||
} | ||
|
||
//------------------------------------------------- | ||
// Perform read | ||
res = spi_eeprom_start(); | ||
if (!res) { | ||
dprint("failed to start SPI for read\n"); | ||
spi_stop(); | ||
return; | ||
} | ||
|
||
spi_write(CMD_READ); | ||
spi_write((uint8_t)((addr & 0xFF0000u) >> 16u)); | ||
spi_write((uint8_t)((addr & 0x00FF00u) >> 8u)); | ||
spi_write((uint8_t)((addr & 0x0000FFu))); | ||
spi_receive(buf, len); | ||
spi_stop(); | ||
|
||
#if defined(CONSOLE_ENABLE) && defined(DEBUG_EEPROM_OUTPUT) | ||
dprintf("[EEPROM R] 0x%08lX: ", (addr)); | ||
for (size_t i = 0; i < len; ++i) { | ||
dprintf(" %02X", (int)(((uint8_t *)buf)[i])); | ||
} | ||
dprintf("\n"); | ||
#endif // DEBUG_EEPROM_OUTPUT | ||
|
||
} | ||
|
||
void eeprom_write(const void *buf, uint32_t addr, size_t len) { | ||
eeprom_erase(addr); | ||
|
||
//------------------------------------------------- | ||
// Enable writes | ||
bool res = spi_eeprom_start(); | ||
if (!res) { | ||
dprint("failed to start SPI for write-enable\n"); | ||
spi_stop(); | ||
return; | ||
} | ||
|
||
spi_write(CMD_WREN); | ||
spi_stop(); | ||
|
||
wait_us(1); | ||
|
||
//------------------------------------------------- | ||
// Perform the write | ||
res = spi_eeprom_start(); | ||
if (!res) { | ||
dprint("failed to start SPI for write\n"); | ||
spi_stop(); | ||
return; | ||
} | ||
|
||
#if defined(CONSOLE_ENABLE) && defined(DEBUG_EEPROM_OUTPUT) | ||
dprintf("[EEPROM W] 0x%08lX: ", ((uint32_t)(uintptr_t)addr)); | ||
for (size_t i = 0; i < len; i++) { | ||
dprintf(" %02X", (int)(uint8_t)(buf[i])); | ||
} | ||
dprintf("\n"); | ||
#endif // DEBUG_EEPROM_OUTPUT | ||
|
||
spi_write(CMD_WRITE); | ||
spi_write((uint8_t)((addr & 0xFF0000u) >> 16u)); | ||
spi_write((uint8_t)((addr & 0x00FF00u) >> 8u)); | ||
spi_write((uint8_t)((addr & 0x0000FFu))); | ||
spi_transmit(buf, len); | ||
spi_stop(); | ||
|
||
res = spi_eeprom_start(); | ||
if (!res) { | ||
dprint("failed to start SPI for status\n"); | ||
spi_stop(); | ||
return; | ||
} | ||
spi_status_t response = spi_eeprom_wait_while_busy(EXTERNAL_EEPROM_SPI_TIMEOUT); | ||
spi_stop(); | ||
if (response == SPI_STATUS_TIMEOUT) { | ||
dprint("SPI timeout for WIP check\n"); | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* Copyright 2020 Nick Brassel (tzarc) and tech2077 | ||
* | ||
* 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 | ||
|
||
void eeprom_erase(uint32_t addr); | ||
void eeprom_read(void *buf, uint32_t addr, size_t len); | ||
void eeprom_write(const void *buf, uint32_t addr, size_t len); | ||
|
||
/* | ||
The slave select pin of the EEPROM. | ||
This needs to be a normal GPIO pin_t value, such as A7. | ||
*/ | ||
#ifndef EXTERNAL_EEPROM_SPI_SLAVE_SELECT_PIN | ||
# error "No chip select pin defined -- missing EXTERNAL_EEPROM_SPI_SLAVE_SELECT_PIN" | ||
#endif | ||
|
||
/* | ||
The clock divisor for SPI to ensure that the MCU is within the | ||
specifications of the EEPROM chip. Generally this will be PCLK divided by | ||
the intended divisor -- check your clock settings and the datasheet of | ||
your EEPROM. | ||
*/ | ||
#ifndef EXTERNAL_EEPROM_SPI_CLOCK_DIVISOR | ||
# ifdef __AVR__ | ||
# define EXTERNAL_EEPROM_SPI_CLOCK_DIVISOR 8 | ||
# else | ||
# define EXTERNAL_EEPROM_SPI_CLOCK_DIVISOR 64 | ||
# endif | ||
#endif | ||
|
||
/* | ||
The SPI mode to communicate with the EEPROM. | ||
*/ | ||
#ifndef EXTERNAL_EEPROM_SPI_MODE | ||
# define EXTERNAL_EEPROM_SPI_MODE 0 | ||
#endif | ||
|
||
/* | ||
Whether or not the SPI communication between the MCU and EEPROM should be | ||
LSB-first. | ||
*/ | ||
#ifndef EXTERNAL_EEPROM_SPI_LSBFIRST | ||
# define EXTERNAL_EEPROM_SPI_LSBFIRST false | ||
#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 4. | ||
As expected, consult the datasheet for specifics of your EEPROM. | ||
*/ | ||
#ifndef EXTERNAL_EEPROM_ADDRESS_SIZE | ||
# define EXTERNAL_EEPROM_ADDRESS_SIZE 2 | ||
#endif |
Oops, something went wrong.