Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
Add persistent led support with eeprom (qmk#9)
Browse files Browse the repository at this point in the history
* 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
tech2077 authored and shearn89 committed Feb 16, 2022
1 parent 75d542a commit b8531d0
Show file tree
Hide file tree
Showing 7 changed files with 590 additions and 4 deletions.
24 changes: 20 additions & 4 deletions keyboards/annepro2/annepro2.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "hal.h"
#include "annepro2.h"
#include "annepro2_ble.h"
#include "spi_master.h"
#include "qmk_ap2_led.h"

static const SerialConfig ledUartConfig = {
Expand All @@ -26,17 +27,31 @@ uint16_t annepro2LedMatrix[MATRIX_ROWS * MATRIX_COLS] = {
};

void OVERRIDE keyboard_pre_init_kb(void) {
#if HAL_USE_SPI == TRUE
spi_init();
#endif
}

void OVERRIDE keyboard_post_init_kb(void) {
// Start LED UART
sdStart(&SD0, &ledUartConfig);
sdWrite(&SD0, ledMcuWakeup, 11);

// wait to receive response from wakeup
wait_ms(15);

// loop to clear out receive buffer from shine wakeup
while(!sdGetWouldBlock(&SD0))
sdGet(&SD0);

// Start BLE UART
sdStart(&SD1, &bleUartConfig);
annepro2_ble_startup();

// Give the send uart thread some time to
// send out the queue before we read back
wait_ms(5);

keyboard_post_init_user();
}

Expand Down Expand Up @@ -77,20 +92,21 @@ bool OVERRIDE process_record_kb(uint16_t keycode, keyrecord_t *record) {
case KC_AP_LED_OFF:
annepro2LedPrevProfile();
annepro2LedDisable();
return false;
break;

case KC_AP_LED_ON:
annepro2LedNextProfile();
annepro2LedEnable();
return false;
break;

case KC_AP_LED_NEXT_PROFILE:
annepro2LedNextProfile();
return false;
break;

case KC_AP_LED_PREV_PROFILE:
annepro2LedPrevProfile();
return false;
break;


default:
break;
Expand Down
25 changes: 25 additions & 0 deletions keyboards/annepro2/c18/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,31 @@
/* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */
#define DEBOUNCE 5

#if defined(ANNEPRO2_EEPROM)
// SPI Config
#define SPI_DRIVER SPID1
#define SPI_SCK_PIN A0
#define SPI_SCK_PAL_MODE 5
#define SPI_MOSI_PIN A1
#define SPI_MOSI_PAL_MODE 5
#define SPI_MISO_PIN A2
#define SPI_MISO_PAL_MODE 5
// EEPROM Config for W25X20CL
#define EXTERNAL_EEPROM_SPI_SLAVE_SELECT_PIN A3
#define EXTERNAL_EEPROM_SPI_CLOCK_DIVISOR 16
#define EXTERNAL_EEPROM_BYTE_COUNT 1024 // 262144
#define EXTERNAL_EEPROM_PAGE_SIZE 256
#define EXTERNAL_EEPROM_ADDRESS_SIZE 3
#define EXTERNAL_EEPROM_SPI_LSBFIRST false
#define EXTERNAL_EEPROM_SPI_MODE 3
// HAL Config
#define HAL_USE_SPI TRUE
#define SPI_SELECT_MODE SPI_SELECT_MODE_PAD
// MCU Config
#define HT32_SPI_USE_SPI1 TRUE
#define HT32_SPI1_IRQ_PRIORITY 9
#endif

/* number of backlight levels */
// #define BACKLIGHT_LEVELS 10

Expand Down
5 changes: 5 additions & 0 deletions keyboards/annepro2/c18/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ SRC = \
annepro2_ble.c \
qmk_ap2_led.c

ifeq ($(strip $(ANNEPRO2_EEPROM)), yes)
OPT_DEFS += -DANNEPRO2_EEPROM
SRC += spi_master.c eeprom_w25x20cl.c
endif

LAYOUTS +=

# MCU
Expand Down
203 changes: 203 additions & 0 deletions keyboards/annepro2/eeprom_w25x20cl.c
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;
}
}
84 changes: 84 additions & 0 deletions keyboards/annepro2/eeprom_w25x20cl.h
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
Loading

0 comments on commit b8531d0

Please sign in to comment.