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

Add persistent led support with eeprom #9

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion drivers/chibios/spi_master.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ __attribute__((weak)) void spi_init(void) {
palSetPadMode(PAL_PORT(SPI_SCK_PIN), PAL_PAD(SPI_SCK_PIN), PAL_MODE_STM32_ALTERNATE_PUSHPULL);
palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), PAL_MODE_STM32_ALTERNATE_PUSHPULL);
palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), PAL_MODE_STM32_ALTERNATE_PUSHPULL);
#elif defined(HT32_SPI_USE_SPI1) || defined(HT32_SPI_USE_SPI2)
tech2077 marked this conversation as resolved.
Show resolved Hide resolved
palSetPadMode(PAL_PORT(SPI_SCK_PIN), PAL_PAD(SPI_SCK_PIN), PAL_HT32_MODE_AF(SPI_SCK_PAL_MODE) | PAL_MODE_OUTPUT_PUSHPULL);
palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), PAL_HT32_MODE_AF(SPI_MOSI_PAL_MODE) | PAL_MODE_OUTPUT_PUSHPULL);
palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), PAL_HT32_MODE_AF(SPI_MISO_PAL_MODE) | PAL_MODE_OUTPUT_PUSHPULL);
#else
palSetPadMode(PAL_PORT(SPI_SCK_PIN), PAL_PAD(SPI_SCK_PIN), PAL_MODE_ALTERNATE(SPI_SCK_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST);
palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), PAL_MODE_ALTERNATE(SPI_MOSI_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST);
Expand All @@ -53,12 +57,36 @@ bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor) {
return false;
}

#if defined(HT32_SPI_USE_SPI1) || defined(HT32_SPI_USE_SPI2)
spiConfig.cr0 = SPI_CR0_SELOEN;
spiConfig.cr1 = SPI_CR1_MODE | 8; // 8 bits and in master mode

if (lsbFirst) {
spiConfig.cr1 |= SPI_CR1_FIRSTBIT;
}

switch (mode) {
case 0:
spiConfig.cr1 |= SPI_CR1_FORMAT_MODE0;
break;
case 1:
spiConfig.cr1 |= SPI_CR1_FORMAT_MODE1;
break;
case 2:
spiConfig.cr1 |= SPI_CR1_FORMAT_MODE2;
break;
case 3:
spiConfig.cr1 |= SPI_CR1_FORMAT_MODE3;
break;
}

spiConfig.cpr = (roundedDivisor - 1) >> 1;
#else
spiConfig.cr1 = 0;

if (lsbFirst) {
spiConfig.cr1 |= SPI_CR1_LSBFIRST;
}

switch (mode) {
case 0:
break;
Expand Down Expand Up @@ -98,6 +126,7 @@ bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor) {
spiConfig.cr1 |= SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0;
break;
}
#endif

currentSlavePin = slavePin;
spiConfig.ssport = PAL_PORT(slavePin);
Expand Down
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;
}
}
Loading