diff --git a/keyboards/yandrstudio/tdcq64_2s/f401/eep/eeprom_stm32.c b/keyboards/yandrstudio/tdcq64_2s/f401/eep/eeprom_stm32.c new file mode 100644 index 000000000000..12071a21291e --- /dev/null +++ b/keyboards/yandrstudio/tdcq64_2s/f401/eep/eeprom_stm32.c @@ -0,0 +1,217 @@ +/* + * This software is experimental and a work in progress. + * Under no circumstances should these files be used in relation to any critical system(s). + * Use of these files is at your own risk. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR + * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * This files are free to use from http://engsta.com/stm32-flash-memory-eeprom-emulator/ by + * Artur F. + * + * Modifications for QMK and STM32F303 by Yiancar + * update for STM32F4 support and the the flashing algorithm by yulei + * Adjust the file structure to custom EEPROM_DRIVER by jiaxin96 for support stm32f401cc + */ + +#include +#include +#include "eeprom_stm32_custom.h" +#include "eeprom_driver.h" +/***************************************************************************** + * Allows to use the internal flash to store non volatile data. To initialize + * the functionality use the EEPROM_Init() function. Be sure that by reprogramming + * of the controller just affected pages will be deleted. In other case the non + * volatile data will be lost. + ******************************************************************************/ + +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +/* Functions -----------------------------------------------------------------*/ + +#define IS_VALID_ADDRESS(x) ((x) < FEE_MAX_BYTES) + +uint8_t DataBuf[FEE_MAX_BYTES]; +static void EEPROM_Backup(void); +static FLASH_Status EEPROM_Restore(void); +static uint32_t EEPROM_FindValidAddress(void); +static void EEPROM_Clear(void); + +/***************************************************************************** + * Unlock the FLASH programming and initialized the DataBuf + ******************************************************************************/ +uint16_t EEPROM_Init(void) { + // unlock flash + FLASH_Unlock(); + + // initialize DataBuf + EEPROM_Backup(); + + // Clear Flags + // FLASH_ClearFlag(FLASH_SR_EOP|FLASH_SR_PGERR|FLASH_SR_WRPERR); + + return FEE_DENSITY_BYTES; +} +void eeprom_driver_init(void) { + EEPROM_Init(); +} + +/***************************************************************************** + * Erase the whole reserved Flash Space used for user Data + ******************************************************************************/ +void EEPROM_Erase(void) { + // erase all flash pages + EEPROM_Clear(); + + // reset the content of the buffer + memset(&DataBuf[0], FEE_EMPTY_BYTE, sizeof(DataBuf)); +} +void eeprom_driver_erase(void) { + EEPROM_Erase(); +} +/***************************************************************************** + * Write data with its eeprom address to flash if there has empty words, + * otherwise backup the current valid data, erase all flash pages, + * and finally restore the valid data. + *******************************************************************************/ +uint16_t EEPROM_WriteDataByte(uint16_t Address, uint8_t DataByte) { + FLASH_Status FlashStatus = FLASH_COMPLETE; + + uint32_t addr; + // exit if not a valid address + if (!IS_VALID_ADDRESS(Address)) { + return 0; + } + +// we are sure the address will not be out of bound +#pragma GCC diagnostic ignored "-Warray-bounds" + // same value, do not need program + if (DataBuf[Address] == DataByte) { + return FlashStatus; + } + + // find the address can be written + addr = EEPROM_FindValidAddress(); + DataBuf[Address] = DataByte; +#pragma GCC diagnostic pop + if (addr == FEE_EMPTY_VALUE) { + // EEPROM is full, need to erase and reprogramming + EEPROM_Clear(); + EEPROM_Restore(); + } else { + FLASH_ProgramHalfWord(FEE_ADDR_ADDRESS(addr), Address); + FLASH_ProgramHalfWord(FEE_DATA_ADDRESS(addr), DataByte); + } + + return FlashStatus; +} +/***************************************************************************** + * Read once data byte from a specified address. + *******************************************************************************/ +uint8_t EEPROM_ReadDataByte(uint16_t Address) { + if (!IS_VALID_ADDRESS(Address)) { + return FEE_EMPTY_BYTE; + } + + // Get Byte from caches + return DataBuf[Address]; +} + +/***************************************************************************** + * helper functions + *******************************************************************************/ +// backup the current data +void EEPROM_Backup(void) +{ + uint32_t begin = FEE_PAGE_BASE_ADDRESS; + uint32_t end = FEE_PAGE_END_ADDRESS; + memset(&DataBuf[0], FEE_EMPTY_BYTE, sizeof(DataBuf)); + while( begin < end) { + uint16_t addr = *(__IO uint16_t*)(FEE_ADDR_ADDRESS(begin)); + if (IS_VALID_ADDRESS(addr)) { + DataBuf[addr] = *(__IO uint16_t*)(FEE_DATA_ADDRESS(begin)); + } else if( addr == FEE_EMPTY_WORD) { + uint16_t data = *(__IO uint16_t*)(FEE_DATA_ADDRESS(begin)); + if (data == FEE_EMPTY_WORD) { + // we reached the end of valid data + break; + } + } + begin += 4; + } +} +// restore data from DataBuf +FLASH_Status EEPROM_Restore(void) { + uint32_t cur = FEE_PAGE_BASE_ADDRESS; + for (uint8_t i = 0; i < FEE_MAX_BYTES; i++) { + if (DataBuf[i] != FEE_EMPTY_BYTE) { + FLASH_ProgramHalfWord(FEE_ADDR_ADDRESS(cur), i); + FLASH_ProgramHalfWord(FEE_DATA_ADDRESS(cur), DataBuf[i]); + cur += 4; + } + } + return FLASH_COMPLETE; +} +// find an empty place for programming +uint32_t EEPROM_FindValidAddress(void) { + uint32_t begin = FEE_PAGE_BASE_ADDRESS; + uint32_t end = FEE_PAGE_END_ADDRESS; + while( begin < end) { + uint32_t data = *(__IO uint32_t*)(begin); + if (data == FEE_EMPTY_VALUE) { + return begin; + } + begin += 4; + } + return FEE_EMPTY_VALUE; +} + +void EEPROM_Clear(void) +{ +#if defined(EEPROM_EMU_STM32F401xC) + FLASH_ErasePage(FEE_SECTOR_ID); +#else + int page_num = 0; + + // delete all pages from specified start page to the last page + do { + FLASH_ErasePage(FEE_PAGE_BASE_ADDRESS + (page_num * FEE_PAGE_SIZE)); + page_num++; + } while (page_num < FEE_DENSITY_PAGES); +#endif +} +/***************************************************************************** + * Wrap library in AVR style functions. + *******************************************************************************/ +uint8_t eeprom_read_byte_f4(const uint8_t *Address) { + const uint16_t p = (const uint32_t)Address; + return EEPROM_ReadDataByte(p); +} + +void eeprom_write_byte_f4(uint8_t *Address, uint8_t Value) { + uint16_t p = (uint32_t)Address; + EEPROM_WriteDataByte(p, Value); +} + + +void eeprom_read_block(void *buf, const void *addr, size_t len) { + const uint8_t *p = (const uint8_t *)addr; + uint8_t * dest = (uint8_t *)buf; + while (len--) { + *dest++ = eeprom_read_byte_f4(p++); + } +} + +void eeprom_write_block(const void *buf, void *addr, size_t len) { + uint8_t * p = (uint8_t *)addr; + const uint8_t *src = (const uint8_t *)buf; + while (len--) { + eeprom_write_byte_f4(p++, *src++); + } +} + + diff --git a/keyboards/yandrstudio/tdcq64_2s/f401/eep/eeprom_stm32_custom.h b/keyboards/yandrstudio/tdcq64_2s/f401/eep/eeprom_stm32_custom.h new file mode 100644 index 000000000000..096842aaaf16 --- /dev/null +++ b/keyboards/yandrstudio/tdcq64_2s/f401/eep/eeprom_stm32_custom.h @@ -0,0 +1,104 @@ +/* + * This software is experimental and a work in progress. + * Under no circumstances should these files be used in relation to any critical system(s). + * Use of these files is at your own risk. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR + * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * This files are free to use from http://engsta.com/stm32-flash-memory-eeprom-emulator/ by + * Artur F. + * + * Modifications for QMK and STM32F303 by Yiancar + * + * This library assumes 8-bit data locations. To add a new MCU, please provide the flash + * page size and the total flash size in Kb. The number of available pages must be a multiple + * of 2. Only half of the pages account for the total EEPROM size. + * This library also assumes that the pages are not used by the firmware. + * Adjust the file structure to custom EEPROM_DRIVER by jiaxin96 for support stm32f401cc + */ + +#pragma once + +#include +#include +#include "flash_stm32.h" + +// HACK ALERT. This definition may not match your processor +// To Do. Work out correct value for EEPROM_PAGE_SIZE on the STM32F103CT6 etc +#if defined(EEPROM_EMU_STM32F303xC) +# define MCU_STM32F303CC +#elif defined(EEPROM_EMU_STM32F103xB) +# define MCU_STM32F103RB +#elif defined(EEPROM_EMU_STM32F072xB) +# define MCU_STM32F072CB +#elif defined(EEPROM_EMU_STM32F042x6) +# define MCU_STM32F042K6 +#elif defined(EEPROM_EMU_STM32F401xC) +# define MCU_STM32F401xC +#else +# error "not implemented." +#endif + + + + + +#ifndef EEPROM_PAGE_SIZE +# if defined(MCU_STM32F103RB) || defined(MCU_STM32F042K6) +# define FEE_PAGE_SIZE (uint16_t)0x400 // Page size = 1KByte +# define FEE_DENSITY_PAGES 4 // How many pages are used +# elif defined(MCU_STM32F103ZE) || defined(MCU_STM32F103RE) || defined(MCU_STM32F103RD) || defined(MCU_STM32F303CC) || defined(MCU_STM32F072CB) +# define FEE_PAGE_SIZE (uint16_t)0x800 // Page size = 2KByte +# define FEE_DENSITY_PAGES 4 // How many pages are used +# elif defined(MCU_STM32F401xC) +# define FEE_PAGE_SIZE (uint32_t)0x20000 // Page size = 128KByte +# define FEE_DENSITY_PAGES 1 // How many pages are used +# define FEE_SECTOR_ID 5 // sector id of the flash +# else +# error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)." +# endif +#endif + +#ifndef EEPROM_START_ADDRESS +# if defined(MCU_STM32F103RB) || defined(MCU_STM32F072CB) +# define FEE_MCU_FLASH_SIZE 128 // Size in Kb +# elif defined(MCU_STM32F042K6) +# define FEE_MCU_FLASH_SIZE 32 // Size in Kb +# elif defined(MCU_STM32F103ZE) || defined(MCU_STM32F103RE) +# define FEE_MCU_FLASH_SIZE 512 // Size in Kb +# elif defined(MCU_STM32F103RD) +# define FEE_MCU_FLASH_SIZE 384 // Size in Kb +# elif defined(MCU_STM32F303CC) +# define FEE_MCU_FLASH_SIZE 256 // Size in Kb +# elif defined(MCU_STM32F401xC) +# define FEE_MCU_FLASH_SIZE 256 // Size in Kb +# else +# error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)." +# endif +#endif + +// DONT CHANGE +// Choose location for the first EEPROM Page address on the top of flash +// #define FEE_PAGE_BASE_ADDRESS ((uint32_t)(0x8000000 + FEE_MCU_FLASH_SIZE * 1024 - FEE_DENSITY_PAGES * FEE_PAGE_SIZE)) +// #define FEE_DENSITY_BYTES ((FEE_PAGE_SIZE / 2) * FEE_DENSITY_PAGES - 1) +// #define FEE_LAST_PAGE_ADDRESS (FEE_PAGE_BASE_ADDRESS + (FEE_PAGE_SIZE * FEE_DENSITY_PAGES)) +// #define FEE_EMPTY_WORD ((uint16_t)0xFFFF) +// #define FEE_ADDR_OFFSET(Address) (Address * 2) // 1Byte per Word will be saved to preserve Flash + +#define FEE_PAGE_END_ADDRESS ((uint32_t)(0x08000000 + FEE_MCU_FLASH_SIZE * 1024)) +#define FEE_PAGE_BASE_ADDRESS ((uint32_t)((FEE_PAGE_END_ADDRESS) - FEE_DENSITY_PAGES * FEE_PAGE_SIZE)) +#define FEE_DENSITY_BYTES ((FEE_PAGE_SIZE / 4) * FEE_DENSITY_PAGES - 1) // 4 Bytes for address, value pair +//#define FEE_LAST_PAGE_ADDRESS (FEE_PAGE_BASE_ADDRESS + (FEE_PAGE_SIZE * FEE_DENSITY_PAGES)) +#define FEE_EMPTY_BYTE ((uint8_t)0xFF) +#define FEE_EMPTY_WORD ((uint16_t)0xFFFF) +#define FEE_EMPTY_VALUE ((uint32_t)0xFFFFFFFF) +//#define FEE_ADDR_OFFSET(Address) (Address * 2) // 1 Byte per Word will be saved to preserve Flash +#define FEE_DATA_ADDRESS(Address) ((Address)+2) // flash address of the eeprom data +#define FEE_ADDR_ADDRESS(Address) (Address) // flash address of the eeprom address +#define FEE_MAX_BYTES ((FEE_DENSITY_BYTES+1) > 1024 ? 1024 : (FEE_DENSITY_BYTES+1)) // eeprom size + diff --git a/keyboards/yandrstudio/tdcq64_2s/f401/eep/flash_stm32.c b/keyboards/yandrstudio/tdcq64_2s/f401/eep/flash_stm32.c new file mode 100644 index 000000000000..c3f1937c5258 --- /dev/null +++ b/keyboards/yandrstudio/tdcq64_2s/f401/eep/flash_stm32.c @@ -0,0 +1,223 @@ +/* + * This software is experimental and a work in progress. + * Under no circumstances should these files be used in relation to any critical system(s). + * Use of these files is at your own risk. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR + * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * This files are free to use from https://github.com/rogerclarkmelbourne/Arduino_STM32 and + * https://github.com/leaflabs/libmaple + * + * Modifications for QMK and STM32F303 by Yiancar + * Adjust the file structure to custom EEPROM_DRIVER by jiaxin96 for support stm32f401cc + */ + +#if defined(EEPROM_EMU_STM32F303xC) +# define STM32F303xC +# include "stm32f3xx.h" +#elif defined(EEPROM_EMU_STM32F103xB) +# define STM32F103xB +# include "stm32f1xx.h" +#elif defined(EEPROM_EMU_STM32F072xB) +# define STM32F072xB +# include "stm32f0xx.h" +#elif defined(EEPROM_EMU_STM32F042x6) +# define STM32F042x6 +# include "stm32f0xx.h" +#elif defined(EEPROM_EMU_STM32F401xC) +# define STM32F401xC +# include "stm32f4xx.h" +#else +# error "not implemented." +#endif + + +#include "flash_stm32.h" + +#if defined(EEPROM_EMU_STM32F103xB) +# define FLASH_SR_WRPERR FLASH_SR_WRPRTERR +#endif + +#if defined(EEPROM_EMU_STM32F401xC) +# define FLASH_PSIZE_HFWORD FLASH_CR_PSIZE_0 +# define FLASH_PSIZE_WORD FLASH_CR_PSIZE_1 +# define FLASH_CR_SNB_POS 3 +/* the flash key was not defined in the CMSIS used by current chibios */ +#define FLASH_KEY1 0x45670123U +#define FLASH_KEY2 0xCDEF89ABU +# define FLASH_SR_FLAGS (FLASH_SR_PGAERR|FLASH_SR_PGPERR|FLASH_SR_PGSERR|FLASH_SR_WRPERR) +#else +# define FLASH_SR_FLAGS (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR) +#endif + +/* Delay definition */ +#define EraseTimeout ((uint32_t)0x00000FFF) +#define ProgramTimeout ((uint32_t)0x0000001F) + +#define ASSERT(exp) (void)((0)) + +/** + * @brief Inserts a time delay. + * @param None + * @retval None + */ +static void delay(void) { + __IO uint32_t i = 0; + for (i = 0xFF; i != 0; i--) { + } +} + +/** + * @brief Returns the FLASH Status. + * @param None + * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG, + * FLASH_ERROR_WRP or FLASH_COMPLETE + */ +FLASH_Status FLASH_GetStatus(void) { + if ((FLASH->SR & FLASH_SR_BSY) == FLASH_SR_BSY) return FLASH_BUSY; + +#if defined(EEPROM_EMU_STM32F401xC) + if ((FLASH->SR & (FLASH_SR_PGSERR | FLASH_SR_PGPERR | FLASH_SR_PGAERR))) return FLASH_ERROR_PG; + if ((FLASH->SR & FLASH_SR_WRPERR)) return FLASH_ERROR_WRP; +#else + if ((FLASH->SR & FLASH_SR_PGERR) != 0) return FLASH_ERROR_PG; + if ((FLASH->SR & FLASH_SR_WRPERR) != 0) return FLASH_ERROR_WRP; + if ((FLASH->SR & FLASH_OBR_OPTERR) != 0) return FLASH_ERROR_OPT; +#endif + + return FLASH_COMPLETE; +} + +/** + * @brief Waits for a Flash operation to complete or a TIMEOUT to occur. + * @param Timeout: FLASH progamming Timeout + * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG, + * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. + */ +FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout) { + FLASH_Status status; + + /* Check for the Flash Status */ + status = FLASH_GetStatus(); + /* Wait for a Flash operation to complete or a TIMEOUT to occur */ + while ((status == FLASH_BUSY) && (Timeout != 0x00)) { + delay(); + status = FLASH_GetStatus(); + Timeout--; + } + if (Timeout == 0) status = FLASH_TIMEOUT; + /* Return the operation status */ + return status; +} + +/** + * @brief Erases a specified FLASH page. + * @param Page_Address: The page address or sector to be erased. + * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG, + * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. + */ +FLASH_Status FLASH_ErasePage(uint32_t Page) { + FLASH_Status status = FLASH_COMPLETE; + /* Check the parameters */ + //ASSERT(IS_FLASH_ADDRESS(Page_Address)); + /* Wait for last operation to be completed */ + status = FLASH_WaitForLastOperation(EraseTimeout); + + if (status == FLASH_COMPLETE) { + /* if the previous operation is completed, proceed to erase the page */ +#if defined(EEPROM_EMU_STM32F401xC) + FLASH->CR &= ~FLASH_CR_SNB; + FLASH->CR |= FLASH_CR_SER | (Page << FLASH_CR_SNB_POS); +#else + FLASH->CR |= FLASH_CR_PER; + FLASH->AR = Page; +#endif + FLASH->CR |= FLASH_CR_STRT; + + /* Wait for last operation to be completed */ + status = FLASH_WaitForLastOperation(EraseTimeout); + /* clear the SER or PER Bit */ +#if defined(EEPROM_EMU_STM32F401xC) + FLASH->CR &= ~(FLASH_CR_SER | FLASH_CR_SNB); +#else + FLASH->CR &= ~FLASH_CR_PER; +#endif + FLASH_ClearFlag(FLASH_SR_FLAGS); + } + /* Return the Erase Status */ + return status; +} + +/** + * @brief Programs a half word at a specified address. + * @param Address: specifies the address to be programmed. + * @param Data: specifies the data to be programmed. + * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG, + * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. + */ +FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data) { + FLASH_Status status = FLASH_BAD_ADDRESS; + if ((Address % sizeof(uint16_t) != 0 ) || !IS_FLASH_ADDRESS(Address)) { + return status; + } + + /* Wait for last operation to be completed */ + status = FLASH_WaitForLastOperation(ProgramTimeout); + if (status == FLASH_COMPLETE) { + /* if the previous operation is completed, proceed to program the new data */ +#if defined(EEPROM_EMU_STM32F401xC) + FLASH->CR &= ~FLASH_CR_PSIZE; + FLASH->CR |= FLASH_PSIZE_HFWORD; +#endif + FLASH->CR |= FLASH_CR_PG; + *(__IO uint16_t*)Address = Data; + /* Wait for last operation to be completed */ + status = FLASH_WaitForLastOperation(ProgramTimeout); + if (status != FLASH_TIMEOUT) { + /* if the program operation is completed, disable the PG Bit */ + FLASH->CR &= ~FLASH_CR_PG; + } + FLASH_ClearFlag(FLASH_SR_FLAGS); + } + return status; +} + +/** + * @brief Unlocks the FLASH Program Erase Controller. + * @param None + * @retval None + */ +void FLASH_Unlock(void) { + /* Authorize the FPEC Access */ + FLASH->KEYR = FLASH_KEY1; + FLASH->KEYR = FLASH_KEY2; +} + +/** + * @brief Locks the FLASH Program Erase Controller. + * @param None + * @retval None + */ +void FLASH_Lock(void) { + /* Set the Lock Bit to lock the FPEC and the FCR */ + FLASH->CR |= FLASH_CR_LOCK; +} + +/** + * @brief Clears the FLASH's pending flags. + * @param FLASH_FLAG: specifies the FLASH flags to clear. + * This parameter can be any combination of the following values: + * @arg FLASH_FLAG_PGERR: FLASH Programming error flag flag + * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag + * @arg FLASH_FLAG_EOP: FLASH End of Programming flag + * @retval None + */ +void FLASH_ClearFlag(uint32_t FLASH_FLAG) { + /* Clear the flags */ + FLASH->SR |= FLASH_FLAG; +} diff --git a/keyboards/yandrstudio/tdcq64_2s/f401/rules.mk b/keyboards/yandrstudio/tdcq64_2s/f401/rules.mk index 9454a9d06f28..1fe62f6dea45 100644 --- a/keyboards/yandrstudio/tdcq64_2s/f401/rules.mk +++ b/keyboards/yandrstudio/tdcq64_2s/f401/rules.mk @@ -3,13 +3,22 @@ MCU = STM32F401 # Address of the bootloader in system memory STM32_BOOTLOADER_ADDRESS = 0x1FFF0000 - - -CUSTOM_MATRIX = lite # for using the A9 pin as matrix io +# Bootloader selection +BOOTLOADER = stm32-dfu # # project specific file +CUSTOM_MATRIX = lite # for using the A9 pin as matrix io QUANTUM_SRC += matrix.c - +RGB_MATRIX_ENABLE = yes +RGB_MATRIX_DRIVER = WS2812 +BACKLIGHT_ENABLE = yes WS2812_DRIVER = pwm BACKLIGHT_DRIVER = pwm + + +EEPROM_DRIVER = custom +SRC += eep/eeprom_stm32.c +SRC += eep/flash_stm32.c +OPT_DEFS += -DEEPROM_EMU_STM32F401xC +COMMON_VPATH += keyboards/yandrstudio/tdcq64_2s/f401/eep diff --git a/keyboards/yandrstudio/tdcq64_2s/rules.mk b/keyboards/yandrstudio/tdcq64_2s/rules.mk index 1d6b7abbe039..0e21accedb01 100644 --- a/keyboards/yandrstudio/tdcq64_2s/rules.mk +++ b/keyboards/yandrstudio/tdcq64_2s/rules.mk @@ -11,7 +11,5 @@ COMMAND_ENABLE = yes # Commands for debug and configuration SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend # if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work NKRO_ENABLE = yes # USB Nkey Rollover -RGB_MATRIX_ENABLE = yes -RGB_MATRIX_DRIVER = WS2812 -BACKLIGHT_ENABLE = yes + diff --git a/keyboards/yandrstudio/tdcq68/f401/eep/eeprom_stm32.c b/keyboards/yandrstudio/tdcq68/f401/eep/eeprom_stm32.c new file mode 100644 index 000000000000..12071a21291e --- /dev/null +++ b/keyboards/yandrstudio/tdcq68/f401/eep/eeprom_stm32.c @@ -0,0 +1,217 @@ +/* + * This software is experimental and a work in progress. + * Under no circumstances should these files be used in relation to any critical system(s). + * Use of these files is at your own risk. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR + * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * This files are free to use from http://engsta.com/stm32-flash-memory-eeprom-emulator/ by + * Artur F. + * + * Modifications for QMK and STM32F303 by Yiancar + * update for STM32F4 support and the the flashing algorithm by yulei + * Adjust the file structure to custom EEPROM_DRIVER by jiaxin96 for support stm32f401cc + */ + +#include +#include +#include "eeprom_stm32_custom.h" +#include "eeprom_driver.h" +/***************************************************************************** + * Allows to use the internal flash to store non volatile data. To initialize + * the functionality use the EEPROM_Init() function. Be sure that by reprogramming + * of the controller just affected pages will be deleted. In other case the non + * volatile data will be lost. + ******************************************************************************/ + +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +/* Functions -----------------------------------------------------------------*/ + +#define IS_VALID_ADDRESS(x) ((x) < FEE_MAX_BYTES) + +uint8_t DataBuf[FEE_MAX_BYTES]; +static void EEPROM_Backup(void); +static FLASH_Status EEPROM_Restore(void); +static uint32_t EEPROM_FindValidAddress(void); +static void EEPROM_Clear(void); + +/***************************************************************************** + * Unlock the FLASH programming and initialized the DataBuf + ******************************************************************************/ +uint16_t EEPROM_Init(void) { + // unlock flash + FLASH_Unlock(); + + // initialize DataBuf + EEPROM_Backup(); + + // Clear Flags + // FLASH_ClearFlag(FLASH_SR_EOP|FLASH_SR_PGERR|FLASH_SR_WRPERR); + + return FEE_DENSITY_BYTES; +} +void eeprom_driver_init(void) { + EEPROM_Init(); +} + +/***************************************************************************** + * Erase the whole reserved Flash Space used for user Data + ******************************************************************************/ +void EEPROM_Erase(void) { + // erase all flash pages + EEPROM_Clear(); + + // reset the content of the buffer + memset(&DataBuf[0], FEE_EMPTY_BYTE, sizeof(DataBuf)); +} +void eeprom_driver_erase(void) { + EEPROM_Erase(); +} +/***************************************************************************** + * Write data with its eeprom address to flash if there has empty words, + * otherwise backup the current valid data, erase all flash pages, + * and finally restore the valid data. + *******************************************************************************/ +uint16_t EEPROM_WriteDataByte(uint16_t Address, uint8_t DataByte) { + FLASH_Status FlashStatus = FLASH_COMPLETE; + + uint32_t addr; + // exit if not a valid address + if (!IS_VALID_ADDRESS(Address)) { + return 0; + } + +// we are sure the address will not be out of bound +#pragma GCC diagnostic ignored "-Warray-bounds" + // same value, do not need program + if (DataBuf[Address] == DataByte) { + return FlashStatus; + } + + // find the address can be written + addr = EEPROM_FindValidAddress(); + DataBuf[Address] = DataByte; +#pragma GCC diagnostic pop + if (addr == FEE_EMPTY_VALUE) { + // EEPROM is full, need to erase and reprogramming + EEPROM_Clear(); + EEPROM_Restore(); + } else { + FLASH_ProgramHalfWord(FEE_ADDR_ADDRESS(addr), Address); + FLASH_ProgramHalfWord(FEE_DATA_ADDRESS(addr), DataByte); + } + + return FlashStatus; +} +/***************************************************************************** + * Read once data byte from a specified address. + *******************************************************************************/ +uint8_t EEPROM_ReadDataByte(uint16_t Address) { + if (!IS_VALID_ADDRESS(Address)) { + return FEE_EMPTY_BYTE; + } + + // Get Byte from caches + return DataBuf[Address]; +} + +/***************************************************************************** + * helper functions + *******************************************************************************/ +// backup the current data +void EEPROM_Backup(void) +{ + uint32_t begin = FEE_PAGE_BASE_ADDRESS; + uint32_t end = FEE_PAGE_END_ADDRESS; + memset(&DataBuf[0], FEE_EMPTY_BYTE, sizeof(DataBuf)); + while( begin < end) { + uint16_t addr = *(__IO uint16_t*)(FEE_ADDR_ADDRESS(begin)); + if (IS_VALID_ADDRESS(addr)) { + DataBuf[addr] = *(__IO uint16_t*)(FEE_DATA_ADDRESS(begin)); + } else if( addr == FEE_EMPTY_WORD) { + uint16_t data = *(__IO uint16_t*)(FEE_DATA_ADDRESS(begin)); + if (data == FEE_EMPTY_WORD) { + // we reached the end of valid data + break; + } + } + begin += 4; + } +} +// restore data from DataBuf +FLASH_Status EEPROM_Restore(void) { + uint32_t cur = FEE_PAGE_BASE_ADDRESS; + for (uint8_t i = 0; i < FEE_MAX_BYTES; i++) { + if (DataBuf[i] != FEE_EMPTY_BYTE) { + FLASH_ProgramHalfWord(FEE_ADDR_ADDRESS(cur), i); + FLASH_ProgramHalfWord(FEE_DATA_ADDRESS(cur), DataBuf[i]); + cur += 4; + } + } + return FLASH_COMPLETE; +} +// find an empty place for programming +uint32_t EEPROM_FindValidAddress(void) { + uint32_t begin = FEE_PAGE_BASE_ADDRESS; + uint32_t end = FEE_PAGE_END_ADDRESS; + while( begin < end) { + uint32_t data = *(__IO uint32_t*)(begin); + if (data == FEE_EMPTY_VALUE) { + return begin; + } + begin += 4; + } + return FEE_EMPTY_VALUE; +} + +void EEPROM_Clear(void) +{ +#if defined(EEPROM_EMU_STM32F401xC) + FLASH_ErasePage(FEE_SECTOR_ID); +#else + int page_num = 0; + + // delete all pages from specified start page to the last page + do { + FLASH_ErasePage(FEE_PAGE_BASE_ADDRESS + (page_num * FEE_PAGE_SIZE)); + page_num++; + } while (page_num < FEE_DENSITY_PAGES); +#endif +} +/***************************************************************************** + * Wrap library in AVR style functions. + *******************************************************************************/ +uint8_t eeprom_read_byte_f4(const uint8_t *Address) { + const uint16_t p = (const uint32_t)Address; + return EEPROM_ReadDataByte(p); +} + +void eeprom_write_byte_f4(uint8_t *Address, uint8_t Value) { + uint16_t p = (uint32_t)Address; + EEPROM_WriteDataByte(p, Value); +} + + +void eeprom_read_block(void *buf, const void *addr, size_t len) { + const uint8_t *p = (const uint8_t *)addr; + uint8_t * dest = (uint8_t *)buf; + while (len--) { + *dest++ = eeprom_read_byte_f4(p++); + } +} + +void eeprom_write_block(const void *buf, void *addr, size_t len) { + uint8_t * p = (uint8_t *)addr; + const uint8_t *src = (const uint8_t *)buf; + while (len--) { + eeprom_write_byte_f4(p++, *src++); + } +} + + diff --git a/keyboards/yandrstudio/tdcq68/f401/eep/eeprom_stm32_custom.h b/keyboards/yandrstudio/tdcq68/f401/eep/eeprom_stm32_custom.h new file mode 100644 index 000000000000..096842aaaf16 --- /dev/null +++ b/keyboards/yandrstudio/tdcq68/f401/eep/eeprom_stm32_custom.h @@ -0,0 +1,104 @@ +/* + * This software is experimental and a work in progress. + * Under no circumstances should these files be used in relation to any critical system(s). + * Use of these files is at your own risk. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR + * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * This files are free to use from http://engsta.com/stm32-flash-memory-eeprom-emulator/ by + * Artur F. + * + * Modifications for QMK and STM32F303 by Yiancar + * + * This library assumes 8-bit data locations. To add a new MCU, please provide the flash + * page size and the total flash size in Kb. The number of available pages must be a multiple + * of 2. Only half of the pages account for the total EEPROM size. + * This library also assumes that the pages are not used by the firmware. + * Adjust the file structure to custom EEPROM_DRIVER by jiaxin96 for support stm32f401cc + */ + +#pragma once + +#include +#include +#include "flash_stm32.h" + +// HACK ALERT. This definition may not match your processor +// To Do. Work out correct value for EEPROM_PAGE_SIZE on the STM32F103CT6 etc +#if defined(EEPROM_EMU_STM32F303xC) +# define MCU_STM32F303CC +#elif defined(EEPROM_EMU_STM32F103xB) +# define MCU_STM32F103RB +#elif defined(EEPROM_EMU_STM32F072xB) +# define MCU_STM32F072CB +#elif defined(EEPROM_EMU_STM32F042x6) +# define MCU_STM32F042K6 +#elif defined(EEPROM_EMU_STM32F401xC) +# define MCU_STM32F401xC +#else +# error "not implemented." +#endif + + + + + +#ifndef EEPROM_PAGE_SIZE +# if defined(MCU_STM32F103RB) || defined(MCU_STM32F042K6) +# define FEE_PAGE_SIZE (uint16_t)0x400 // Page size = 1KByte +# define FEE_DENSITY_PAGES 4 // How many pages are used +# elif defined(MCU_STM32F103ZE) || defined(MCU_STM32F103RE) || defined(MCU_STM32F103RD) || defined(MCU_STM32F303CC) || defined(MCU_STM32F072CB) +# define FEE_PAGE_SIZE (uint16_t)0x800 // Page size = 2KByte +# define FEE_DENSITY_PAGES 4 // How many pages are used +# elif defined(MCU_STM32F401xC) +# define FEE_PAGE_SIZE (uint32_t)0x20000 // Page size = 128KByte +# define FEE_DENSITY_PAGES 1 // How many pages are used +# define FEE_SECTOR_ID 5 // sector id of the flash +# else +# error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)." +# endif +#endif + +#ifndef EEPROM_START_ADDRESS +# if defined(MCU_STM32F103RB) || defined(MCU_STM32F072CB) +# define FEE_MCU_FLASH_SIZE 128 // Size in Kb +# elif defined(MCU_STM32F042K6) +# define FEE_MCU_FLASH_SIZE 32 // Size in Kb +# elif defined(MCU_STM32F103ZE) || defined(MCU_STM32F103RE) +# define FEE_MCU_FLASH_SIZE 512 // Size in Kb +# elif defined(MCU_STM32F103RD) +# define FEE_MCU_FLASH_SIZE 384 // Size in Kb +# elif defined(MCU_STM32F303CC) +# define FEE_MCU_FLASH_SIZE 256 // Size in Kb +# elif defined(MCU_STM32F401xC) +# define FEE_MCU_FLASH_SIZE 256 // Size in Kb +# else +# error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)." +# endif +#endif + +// DONT CHANGE +// Choose location for the first EEPROM Page address on the top of flash +// #define FEE_PAGE_BASE_ADDRESS ((uint32_t)(0x8000000 + FEE_MCU_FLASH_SIZE * 1024 - FEE_DENSITY_PAGES * FEE_PAGE_SIZE)) +// #define FEE_DENSITY_BYTES ((FEE_PAGE_SIZE / 2) * FEE_DENSITY_PAGES - 1) +// #define FEE_LAST_PAGE_ADDRESS (FEE_PAGE_BASE_ADDRESS + (FEE_PAGE_SIZE * FEE_DENSITY_PAGES)) +// #define FEE_EMPTY_WORD ((uint16_t)0xFFFF) +// #define FEE_ADDR_OFFSET(Address) (Address * 2) // 1Byte per Word will be saved to preserve Flash + +#define FEE_PAGE_END_ADDRESS ((uint32_t)(0x08000000 + FEE_MCU_FLASH_SIZE * 1024)) +#define FEE_PAGE_BASE_ADDRESS ((uint32_t)((FEE_PAGE_END_ADDRESS) - FEE_DENSITY_PAGES * FEE_PAGE_SIZE)) +#define FEE_DENSITY_BYTES ((FEE_PAGE_SIZE / 4) * FEE_DENSITY_PAGES - 1) // 4 Bytes for address, value pair +//#define FEE_LAST_PAGE_ADDRESS (FEE_PAGE_BASE_ADDRESS + (FEE_PAGE_SIZE * FEE_DENSITY_PAGES)) +#define FEE_EMPTY_BYTE ((uint8_t)0xFF) +#define FEE_EMPTY_WORD ((uint16_t)0xFFFF) +#define FEE_EMPTY_VALUE ((uint32_t)0xFFFFFFFF) +//#define FEE_ADDR_OFFSET(Address) (Address * 2) // 1 Byte per Word will be saved to preserve Flash +#define FEE_DATA_ADDRESS(Address) ((Address)+2) // flash address of the eeprom data +#define FEE_ADDR_ADDRESS(Address) (Address) // flash address of the eeprom address +#define FEE_MAX_BYTES ((FEE_DENSITY_BYTES+1) > 1024 ? 1024 : (FEE_DENSITY_BYTES+1)) // eeprom size + diff --git a/keyboards/yandrstudio/tdcq68/f401/eep/flash_stm32.c b/keyboards/yandrstudio/tdcq68/f401/eep/flash_stm32.c new file mode 100644 index 000000000000..c3f1937c5258 --- /dev/null +++ b/keyboards/yandrstudio/tdcq68/f401/eep/flash_stm32.c @@ -0,0 +1,223 @@ +/* + * This software is experimental and a work in progress. + * Under no circumstances should these files be used in relation to any critical system(s). + * Use of these files is at your own risk. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR + * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * This files are free to use from https://github.com/rogerclarkmelbourne/Arduino_STM32 and + * https://github.com/leaflabs/libmaple + * + * Modifications for QMK and STM32F303 by Yiancar + * Adjust the file structure to custom EEPROM_DRIVER by jiaxin96 for support stm32f401cc + */ + +#if defined(EEPROM_EMU_STM32F303xC) +# define STM32F303xC +# include "stm32f3xx.h" +#elif defined(EEPROM_EMU_STM32F103xB) +# define STM32F103xB +# include "stm32f1xx.h" +#elif defined(EEPROM_EMU_STM32F072xB) +# define STM32F072xB +# include "stm32f0xx.h" +#elif defined(EEPROM_EMU_STM32F042x6) +# define STM32F042x6 +# include "stm32f0xx.h" +#elif defined(EEPROM_EMU_STM32F401xC) +# define STM32F401xC +# include "stm32f4xx.h" +#else +# error "not implemented." +#endif + + +#include "flash_stm32.h" + +#if defined(EEPROM_EMU_STM32F103xB) +# define FLASH_SR_WRPERR FLASH_SR_WRPRTERR +#endif + +#if defined(EEPROM_EMU_STM32F401xC) +# define FLASH_PSIZE_HFWORD FLASH_CR_PSIZE_0 +# define FLASH_PSIZE_WORD FLASH_CR_PSIZE_1 +# define FLASH_CR_SNB_POS 3 +/* the flash key was not defined in the CMSIS used by current chibios */ +#define FLASH_KEY1 0x45670123U +#define FLASH_KEY2 0xCDEF89ABU +# define FLASH_SR_FLAGS (FLASH_SR_PGAERR|FLASH_SR_PGPERR|FLASH_SR_PGSERR|FLASH_SR_WRPERR) +#else +# define FLASH_SR_FLAGS (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR) +#endif + +/* Delay definition */ +#define EraseTimeout ((uint32_t)0x00000FFF) +#define ProgramTimeout ((uint32_t)0x0000001F) + +#define ASSERT(exp) (void)((0)) + +/** + * @brief Inserts a time delay. + * @param None + * @retval None + */ +static void delay(void) { + __IO uint32_t i = 0; + for (i = 0xFF; i != 0; i--) { + } +} + +/** + * @brief Returns the FLASH Status. + * @param None + * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG, + * FLASH_ERROR_WRP or FLASH_COMPLETE + */ +FLASH_Status FLASH_GetStatus(void) { + if ((FLASH->SR & FLASH_SR_BSY) == FLASH_SR_BSY) return FLASH_BUSY; + +#if defined(EEPROM_EMU_STM32F401xC) + if ((FLASH->SR & (FLASH_SR_PGSERR | FLASH_SR_PGPERR | FLASH_SR_PGAERR))) return FLASH_ERROR_PG; + if ((FLASH->SR & FLASH_SR_WRPERR)) return FLASH_ERROR_WRP; +#else + if ((FLASH->SR & FLASH_SR_PGERR) != 0) return FLASH_ERROR_PG; + if ((FLASH->SR & FLASH_SR_WRPERR) != 0) return FLASH_ERROR_WRP; + if ((FLASH->SR & FLASH_OBR_OPTERR) != 0) return FLASH_ERROR_OPT; +#endif + + return FLASH_COMPLETE; +} + +/** + * @brief Waits for a Flash operation to complete or a TIMEOUT to occur. + * @param Timeout: FLASH progamming Timeout + * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG, + * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. + */ +FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout) { + FLASH_Status status; + + /* Check for the Flash Status */ + status = FLASH_GetStatus(); + /* Wait for a Flash operation to complete or a TIMEOUT to occur */ + while ((status == FLASH_BUSY) && (Timeout != 0x00)) { + delay(); + status = FLASH_GetStatus(); + Timeout--; + } + if (Timeout == 0) status = FLASH_TIMEOUT; + /* Return the operation status */ + return status; +} + +/** + * @brief Erases a specified FLASH page. + * @param Page_Address: The page address or sector to be erased. + * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG, + * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. + */ +FLASH_Status FLASH_ErasePage(uint32_t Page) { + FLASH_Status status = FLASH_COMPLETE; + /* Check the parameters */ + //ASSERT(IS_FLASH_ADDRESS(Page_Address)); + /* Wait for last operation to be completed */ + status = FLASH_WaitForLastOperation(EraseTimeout); + + if (status == FLASH_COMPLETE) { + /* if the previous operation is completed, proceed to erase the page */ +#if defined(EEPROM_EMU_STM32F401xC) + FLASH->CR &= ~FLASH_CR_SNB; + FLASH->CR |= FLASH_CR_SER | (Page << FLASH_CR_SNB_POS); +#else + FLASH->CR |= FLASH_CR_PER; + FLASH->AR = Page; +#endif + FLASH->CR |= FLASH_CR_STRT; + + /* Wait for last operation to be completed */ + status = FLASH_WaitForLastOperation(EraseTimeout); + /* clear the SER or PER Bit */ +#if defined(EEPROM_EMU_STM32F401xC) + FLASH->CR &= ~(FLASH_CR_SER | FLASH_CR_SNB); +#else + FLASH->CR &= ~FLASH_CR_PER; +#endif + FLASH_ClearFlag(FLASH_SR_FLAGS); + } + /* Return the Erase Status */ + return status; +} + +/** + * @brief Programs a half word at a specified address. + * @param Address: specifies the address to be programmed. + * @param Data: specifies the data to be programmed. + * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG, + * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. + */ +FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data) { + FLASH_Status status = FLASH_BAD_ADDRESS; + if ((Address % sizeof(uint16_t) != 0 ) || !IS_FLASH_ADDRESS(Address)) { + return status; + } + + /* Wait for last operation to be completed */ + status = FLASH_WaitForLastOperation(ProgramTimeout); + if (status == FLASH_COMPLETE) { + /* if the previous operation is completed, proceed to program the new data */ +#if defined(EEPROM_EMU_STM32F401xC) + FLASH->CR &= ~FLASH_CR_PSIZE; + FLASH->CR |= FLASH_PSIZE_HFWORD; +#endif + FLASH->CR |= FLASH_CR_PG; + *(__IO uint16_t*)Address = Data; + /* Wait for last operation to be completed */ + status = FLASH_WaitForLastOperation(ProgramTimeout); + if (status != FLASH_TIMEOUT) { + /* if the program operation is completed, disable the PG Bit */ + FLASH->CR &= ~FLASH_CR_PG; + } + FLASH_ClearFlag(FLASH_SR_FLAGS); + } + return status; +} + +/** + * @brief Unlocks the FLASH Program Erase Controller. + * @param None + * @retval None + */ +void FLASH_Unlock(void) { + /* Authorize the FPEC Access */ + FLASH->KEYR = FLASH_KEY1; + FLASH->KEYR = FLASH_KEY2; +} + +/** + * @brief Locks the FLASH Program Erase Controller. + * @param None + * @retval None + */ +void FLASH_Lock(void) { + /* Set the Lock Bit to lock the FPEC and the FCR */ + FLASH->CR |= FLASH_CR_LOCK; +} + +/** + * @brief Clears the FLASH's pending flags. + * @param FLASH_FLAG: specifies the FLASH flags to clear. + * This parameter can be any combination of the following values: + * @arg FLASH_FLAG_PGERR: FLASH Programming error flag flag + * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag + * @arg FLASH_FLAG_EOP: FLASH End of Programming flag + * @retval None + */ +void FLASH_ClearFlag(uint32_t FLASH_FLAG) { + /* Clear the flags */ + FLASH->SR |= FLASH_FLAG; +} diff --git a/keyboards/yandrstudio/tdcq68/f401/rules.mk b/keyboards/yandrstudio/tdcq68/f401/rules.mk index a03d74a387ea..732c221639be 100644 --- a/keyboards/yandrstudio/tdcq68/f401/rules.mk +++ b/keyboards/yandrstudio/tdcq68/f401/rules.mk @@ -3,12 +3,21 @@ MCU = STM32F401 # Address of the bootloader in system memory STM32_BOOTLOADER_ADDRESS = 0x1FFF0000 - - -CUSTOM_MATRIX = lite # for using the A9 pin as matrix io +# Bootloader selection +BOOTLOADER = stm32-dfu # # project specific file +CUSTOM_MATRIX = lite # for using the A9 pin as matrix io QUANTUM_SRC += matrix.c +RGB_MATRIX_ENABLE = yes +RGB_MATRIX_DRIVER = WS2812 WS2812_DRIVER = pwm + + +EEPROM_DRIVER = custom +SRC += eep/eeprom_stm32.c +SRC += eep/flash_stm32.c +OPT_DEFS += -DEEPROM_EMU_STM32F401xC +COMMON_VPATH += keyboards/yandrstudio/tdcq68/f401/eep diff --git a/keyboards/yandrstudio/tdcq68/rules.mk b/keyboards/yandrstudio/tdcq68/rules.mk index b513b7df7794..ae497cec4519 100644 --- a/keyboards/yandrstudio/tdcq68/rules.mk +++ b/keyboards/yandrstudio/tdcq68/rules.mk @@ -12,7 +12,6 @@ SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend # if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work NKRO_ENABLE = yes # USB Nkey Rollover -RGB_MATRIX_ENABLE = yes -RGB_MATRIX_DRIVER = WS2812 + diff --git a/keyboards/yandrstudio/whiteMouse28T/f401/config.h b/keyboards/yandrstudio/whiteMouse28T/f401/config.h index 1ace919f7251..4657f08d1db3 100644 --- a/keyboards/yandrstudio/whiteMouse28T/f401/config.h +++ b/keyboards/yandrstudio/whiteMouse28T/f401/config.h @@ -77,7 +77,7 @@ # ifdef VIA_ENABLE # define VIA_EEPROM_LAYOUT_OPTIONS_SIZE 2 -# define DYNAMIC_KEYMAP_LAYER_COUNT 20 +# define DYNAMIC_KEYMAP_LAYER_COUNT 24 # define DYNAMIC_KEYMAP_MACRO_COUNT 30 # define DYNAMIC_KEYMAP_EEPROM_MAX_ADDR 10240 # endif diff --git a/keyboards/yandrstudio/whiteMouse28T/f401/eep/eeprom_stm32.c b/keyboards/yandrstudio/whiteMouse28T/f401/eep/eeprom_stm32.c new file mode 100644 index 000000000000..12071a21291e --- /dev/null +++ b/keyboards/yandrstudio/whiteMouse28T/f401/eep/eeprom_stm32.c @@ -0,0 +1,217 @@ +/* + * This software is experimental and a work in progress. + * Under no circumstances should these files be used in relation to any critical system(s). + * Use of these files is at your own risk. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR + * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * This files are free to use from http://engsta.com/stm32-flash-memory-eeprom-emulator/ by + * Artur F. + * + * Modifications for QMK and STM32F303 by Yiancar + * update for STM32F4 support and the the flashing algorithm by yulei + * Adjust the file structure to custom EEPROM_DRIVER by jiaxin96 for support stm32f401cc + */ + +#include +#include +#include "eeprom_stm32_custom.h" +#include "eeprom_driver.h" +/***************************************************************************** + * Allows to use the internal flash to store non volatile data. To initialize + * the functionality use the EEPROM_Init() function. Be sure that by reprogramming + * of the controller just affected pages will be deleted. In other case the non + * volatile data will be lost. + ******************************************************************************/ + +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +/* Functions -----------------------------------------------------------------*/ + +#define IS_VALID_ADDRESS(x) ((x) < FEE_MAX_BYTES) + +uint8_t DataBuf[FEE_MAX_BYTES]; +static void EEPROM_Backup(void); +static FLASH_Status EEPROM_Restore(void); +static uint32_t EEPROM_FindValidAddress(void); +static void EEPROM_Clear(void); + +/***************************************************************************** + * Unlock the FLASH programming and initialized the DataBuf + ******************************************************************************/ +uint16_t EEPROM_Init(void) { + // unlock flash + FLASH_Unlock(); + + // initialize DataBuf + EEPROM_Backup(); + + // Clear Flags + // FLASH_ClearFlag(FLASH_SR_EOP|FLASH_SR_PGERR|FLASH_SR_WRPERR); + + return FEE_DENSITY_BYTES; +} +void eeprom_driver_init(void) { + EEPROM_Init(); +} + +/***************************************************************************** + * Erase the whole reserved Flash Space used for user Data + ******************************************************************************/ +void EEPROM_Erase(void) { + // erase all flash pages + EEPROM_Clear(); + + // reset the content of the buffer + memset(&DataBuf[0], FEE_EMPTY_BYTE, sizeof(DataBuf)); +} +void eeprom_driver_erase(void) { + EEPROM_Erase(); +} +/***************************************************************************** + * Write data with its eeprom address to flash if there has empty words, + * otherwise backup the current valid data, erase all flash pages, + * and finally restore the valid data. + *******************************************************************************/ +uint16_t EEPROM_WriteDataByte(uint16_t Address, uint8_t DataByte) { + FLASH_Status FlashStatus = FLASH_COMPLETE; + + uint32_t addr; + // exit if not a valid address + if (!IS_VALID_ADDRESS(Address)) { + return 0; + } + +// we are sure the address will not be out of bound +#pragma GCC diagnostic ignored "-Warray-bounds" + // same value, do not need program + if (DataBuf[Address] == DataByte) { + return FlashStatus; + } + + // find the address can be written + addr = EEPROM_FindValidAddress(); + DataBuf[Address] = DataByte; +#pragma GCC diagnostic pop + if (addr == FEE_EMPTY_VALUE) { + // EEPROM is full, need to erase and reprogramming + EEPROM_Clear(); + EEPROM_Restore(); + } else { + FLASH_ProgramHalfWord(FEE_ADDR_ADDRESS(addr), Address); + FLASH_ProgramHalfWord(FEE_DATA_ADDRESS(addr), DataByte); + } + + return FlashStatus; +} +/***************************************************************************** + * Read once data byte from a specified address. + *******************************************************************************/ +uint8_t EEPROM_ReadDataByte(uint16_t Address) { + if (!IS_VALID_ADDRESS(Address)) { + return FEE_EMPTY_BYTE; + } + + // Get Byte from caches + return DataBuf[Address]; +} + +/***************************************************************************** + * helper functions + *******************************************************************************/ +// backup the current data +void EEPROM_Backup(void) +{ + uint32_t begin = FEE_PAGE_BASE_ADDRESS; + uint32_t end = FEE_PAGE_END_ADDRESS; + memset(&DataBuf[0], FEE_EMPTY_BYTE, sizeof(DataBuf)); + while( begin < end) { + uint16_t addr = *(__IO uint16_t*)(FEE_ADDR_ADDRESS(begin)); + if (IS_VALID_ADDRESS(addr)) { + DataBuf[addr] = *(__IO uint16_t*)(FEE_DATA_ADDRESS(begin)); + } else if( addr == FEE_EMPTY_WORD) { + uint16_t data = *(__IO uint16_t*)(FEE_DATA_ADDRESS(begin)); + if (data == FEE_EMPTY_WORD) { + // we reached the end of valid data + break; + } + } + begin += 4; + } +} +// restore data from DataBuf +FLASH_Status EEPROM_Restore(void) { + uint32_t cur = FEE_PAGE_BASE_ADDRESS; + for (uint8_t i = 0; i < FEE_MAX_BYTES; i++) { + if (DataBuf[i] != FEE_EMPTY_BYTE) { + FLASH_ProgramHalfWord(FEE_ADDR_ADDRESS(cur), i); + FLASH_ProgramHalfWord(FEE_DATA_ADDRESS(cur), DataBuf[i]); + cur += 4; + } + } + return FLASH_COMPLETE; +} +// find an empty place for programming +uint32_t EEPROM_FindValidAddress(void) { + uint32_t begin = FEE_PAGE_BASE_ADDRESS; + uint32_t end = FEE_PAGE_END_ADDRESS; + while( begin < end) { + uint32_t data = *(__IO uint32_t*)(begin); + if (data == FEE_EMPTY_VALUE) { + return begin; + } + begin += 4; + } + return FEE_EMPTY_VALUE; +} + +void EEPROM_Clear(void) +{ +#if defined(EEPROM_EMU_STM32F401xC) + FLASH_ErasePage(FEE_SECTOR_ID); +#else + int page_num = 0; + + // delete all pages from specified start page to the last page + do { + FLASH_ErasePage(FEE_PAGE_BASE_ADDRESS + (page_num * FEE_PAGE_SIZE)); + page_num++; + } while (page_num < FEE_DENSITY_PAGES); +#endif +} +/***************************************************************************** + * Wrap library in AVR style functions. + *******************************************************************************/ +uint8_t eeprom_read_byte_f4(const uint8_t *Address) { + const uint16_t p = (const uint32_t)Address; + return EEPROM_ReadDataByte(p); +} + +void eeprom_write_byte_f4(uint8_t *Address, uint8_t Value) { + uint16_t p = (uint32_t)Address; + EEPROM_WriteDataByte(p, Value); +} + + +void eeprom_read_block(void *buf, const void *addr, size_t len) { + const uint8_t *p = (const uint8_t *)addr; + uint8_t * dest = (uint8_t *)buf; + while (len--) { + *dest++ = eeprom_read_byte_f4(p++); + } +} + +void eeprom_write_block(const void *buf, void *addr, size_t len) { + uint8_t * p = (uint8_t *)addr; + const uint8_t *src = (const uint8_t *)buf; + while (len--) { + eeprom_write_byte_f4(p++, *src++); + } +} + + diff --git a/keyboards/yandrstudio/whiteMouse28T/f401/eep/eeprom_stm32_custom.h b/keyboards/yandrstudio/whiteMouse28T/f401/eep/eeprom_stm32_custom.h new file mode 100644 index 000000000000..096842aaaf16 --- /dev/null +++ b/keyboards/yandrstudio/whiteMouse28T/f401/eep/eeprom_stm32_custom.h @@ -0,0 +1,104 @@ +/* + * This software is experimental and a work in progress. + * Under no circumstances should these files be used in relation to any critical system(s). + * Use of these files is at your own risk. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR + * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * This files are free to use from http://engsta.com/stm32-flash-memory-eeprom-emulator/ by + * Artur F. + * + * Modifications for QMK and STM32F303 by Yiancar + * + * This library assumes 8-bit data locations. To add a new MCU, please provide the flash + * page size and the total flash size in Kb. The number of available pages must be a multiple + * of 2. Only half of the pages account for the total EEPROM size. + * This library also assumes that the pages are not used by the firmware. + * Adjust the file structure to custom EEPROM_DRIVER by jiaxin96 for support stm32f401cc + */ + +#pragma once + +#include +#include +#include "flash_stm32.h" + +// HACK ALERT. This definition may not match your processor +// To Do. Work out correct value for EEPROM_PAGE_SIZE on the STM32F103CT6 etc +#if defined(EEPROM_EMU_STM32F303xC) +# define MCU_STM32F303CC +#elif defined(EEPROM_EMU_STM32F103xB) +# define MCU_STM32F103RB +#elif defined(EEPROM_EMU_STM32F072xB) +# define MCU_STM32F072CB +#elif defined(EEPROM_EMU_STM32F042x6) +# define MCU_STM32F042K6 +#elif defined(EEPROM_EMU_STM32F401xC) +# define MCU_STM32F401xC +#else +# error "not implemented." +#endif + + + + + +#ifndef EEPROM_PAGE_SIZE +# if defined(MCU_STM32F103RB) || defined(MCU_STM32F042K6) +# define FEE_PAGE_SIZE (uint16_t)0x400 // Page size = 1KByte +# define FEE_DENSITY_PAGES 4 // How many pages are used +# elif defined(MCU_STM32F103ZE) || defined(MCU_STM32F103RE) || defined(MCU_STM32F103RD) || defined(MCU_STM32F303CC) || defined(MCU_STM32F072CB) +# define FEE_PAGE_SIZE (uint16_t)0x800 // Page size = 2KByte +# define FEE_DENSITY_PAGES 4 // How many pages are used +# elif defined(MCU_STM32F401xC) +# define FEE_PAGE_SIZE (uint32_t)0x20000 // Page size = 128KByte +# define FEE_DENSITY_PAGES 1 // How many pages are used +# define FEE_SECTOR_ID 5 // sector id of the flash +# else +# error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)." +# endif +#endif + +#ifndef EEPROM_START_ADDRESS +# if defined(MCU_STM32F103RB) || defined(MCU_STM32F072CB) +# define FEE_MCU_FLASH_SIZE 128 // Size in Kb +# elif defined(MCU_STM32F042K6) +# define FEE_MCU_FLASH_SIZE 32 // Size in Kb +# elif defined(MCU_STM32F103ZE) || defined(MCU_STM32F103RE) +# define FEE_MCU_FLASH_SIZE 512 // Size in Kb +# elif defined(MCU_STM32F103RD) +# define FEE_MCU_FLASH_SIZE 384 // Size in Kb +# elif defined(MCU_STM32F303CC) +# define FEE_MCU_FLASH_SIZE 256 // Size in Kb +# elif defined(MCU_STM32F401xC) +# define FEE_MCU_FLASH_SIZE 256 // Size in Kb +# else +# error "No MCU type specified. Add something like -DMCU_STM32F103RB to your compiler arguments (probably in a Makefile)." +# endif +#endif + +// DONT CHANGE +// Choose location for the first EEPROM Page address on the top of flash +// #define FEE_PAGE_BASE_ADDRESS ((uint32_t)(0x8000000 + FEE_MCU_FLASH_SIZE * 1024 - FEE_DENSITY_PAGES * FEE_PAGE_SIZE)) +// #define FEE_DENSITY_BYTES ((FEE_PAGE_SIZE / 2) * FEE_DENSITY_PAGES - 1) +// #define FEE_LAST_PAGE_ADDRESS (FEE_PAGE_BASE_ADDRESS + (FEE_PAGE_SIZE * FEE_DENSITY_PAGES)) +// #define FEE_EMPTY_WORD ((uint16_t)0xFFFF) +// #define FEE_ADDR_OFFSET(Address) (Address * 2) // 1Byte per Word will be saved to preserve Flash + +#define FEE_PAGE_END_ADDRESS ((uint32_t)(0x08000000 + FEE_MCU_FLASH_SIZE * 1024)) +#define FEE_PAGE_BASE_ADDRESS ((uint32_t)((FEE_PAGE_END_ADDRESS) - FEE_DENSITY_PAGES * FEE_PAGE_SIZE)) +#define FEE_DENSITY_BYTES ((FEE_PAGE_SIZE / 4) * FEE_DENSITY_PAGES - 1) // 4 Bytes for address, value pair +//#define FEE_LAST_PAGE_ADDRESS (FEE_PAGE_BASE_ADDRESS + (FEE_PAGE_SIZE * FEE_DENSITY_PAGES)) +#define FEE_EMPTY_BYTE ((uint8_t)0xFF) +#define FEE_EMPTY_WORD ((uint16_t)0xFFFF) +#define FEE_EMPTY_VALUE ((uint32_t)0xFFFFFFFF) +//#define FEE_ADDR_OFFSET(Address) (Address * 2) // 1 Byte per Word will be saved to preserve Flash +#define FEE_DATA_ADDRESS(Address) ((Address)+2) // flash address of the eeprom data +#define FEE_ADDR_ADDRESS(Address) (Address) // flash address of the eeprom address +#define FEE_MAX_BYTES ((FEE_DENSITY_BYTES+1) > 1024 ? 1024 : (FEE_DENSITY_BYTES+1)) // eeprom size + diff --git a/keyboards/yandrstudio/whiteMouse28T/f401/eep/flash_stm32.c b/keyboards/yandrstudio/whiteMouse28T/f401/eep/flash_stm32.c new file mode 100644 index 000000000000..c3f1937c5258 --- /dev/null +++ b/keyboards/yandrstudio/whiteMouse28T/f401/eep/flash_stm32.c @@ -0,0 +1,223 @@ +/* + * This software is experimental and a work in progress. + * Under no circumstances should these files be used in relation to any critical system(s). + * Use of these files is at your own risk. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR + * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * This files are free to use from https://github.com/rogerclarkmelbourne/Arduino_STM32 and + * https://github.com/leaflabs/libmaple + * + * Modifications for QMK and STM32F303 by Yiancar + * Adjust the file structure to custom EEPROM_DRIVER by jiaxin96 for support stm32f401cc + */ + +#if defined(EEPROM_EMU_STM32F303xC) +# define STM32F303xC +# include "stm32f3xx.h" +#elif defined(EEPROM_EMU_STM32F103xB) +# define STM32F103xB +# include "stm32f1xx.h" +#elif defined(EEPROM_EMU_STM32F072xB) +# define STM32F072xB +# include "stm32f0xx.h" +#elif defined(EEPROM_EMU_STM32F042x6) +# define STM32F042x6 +# include "stm32f0xx.h" +#elif defined(EEPROM_EMU_STM32F401xC) +# define STM32F401xC +# include "stm32f4xx.h" +#else +# error "not implemented." +#endif + + +#include "flash_stm32.h" + +#if defined(EEPROM_EMU_STM32F103xB) +# define FLASH_SR_WRPERR FLASH_SR_WRPRTERR +#endif + +#if defined(EEPROM_EMU_STM32F401xC) +# define FLASH_PSIZE_HFWORD FLASH_CR_PSIZE_0 +# define FLASH_PSIZE_WORD FLASH_CR_PSIZE_1 +# define FLASH_CR_SNB_POS 3 +/* the flash key was not defined in the CMSIS used by current chibios */ +#define FLASH_KEY1 0x45670123U +#define FLASH_KEY2 0xCDEF89ABU +# define FLASH_SR_FLAGS (FLASH_SR_PGAERR|FLASH_SR_PGPERR|FLASH_SR_PGSERR|FLASH_SR_WRPERR) +#else +# define FLASH_SR_FLAGS (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR) +#endif + +/* Delay definition */ +#define EraseTimeout ((uint32_t)0x00000FFF) +#define ProgramTimeout ((uint32_t)0x0000001F) + +#define ASSERT(exp) (void)((0)) + +/** + * @brief Inserts a time delay. + * @param None + * @retval None + */ +static void delay(void) { + __IO uint32_t i = 0; + for (i = 0xFF; i != 0; i--) { + } +} + +/** + * @brief Returns the FLASH Status. + * @param None + * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG, + * FLASH_ERROR_WRP or FLASH_COMPLETE + */ +FLASH_Status FLASH_GetStatus(void) { + if ((FLASH->SR & FLASH_SR_BSY) == FLASH_SR_BSY) return FLASH_BUSY; + +#if defined(EEPROM_EMU_STM32F401xC) + if ((FLASH->SR & (FLASH_SR_PGSERR | FLASH_SR_PGPERR | FLASH_SR_PGAERR))) return FLASH_ERROR_PG; + if ((FLASH->SR & FLASH_SR_WRPERR)) return FLASH_ERROR_WRP; +#else + if ((FLASH->SR & FLASH_SR_PGERR) != 0) return FLASH_ERROR_PG; + if ((FLASH->SR & FLASH_SR_WRPERR) != 0) return FLASH_ERROR_WRP; + if ((FLASH->SR & FLASH_OBR_OPTERR) != 0) return FLASH_ERROR_OPT; +#endif + + return FLASH_COMPLETE; +} + +/** + * @brief Waits for a Flash operation to complete or a TIMEOUT to occur. + * @param Timeout: FLASH progamming Timeout + * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG, + * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. + */ +FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout) { + FLASH_Status status; + + /* Check for the Flash Status */ + status = FLASH_GetStatus(); + /* Wait for a Flash operation to complete or a TIMEOUT to occur */ + while ((status == FLASH_BUSY) && (Timeout != 0x00)) { + delay(); + status = FLASH_GetStatus(); + Timeout--; + } + if (Timeout == 0) status = FLASH_TIMEOUT; + /* Return the operation status */ + return status; +} + +/** + * @brief Erases a specified FLASH page. + * @param Page_Address: The page address or sector to be erased. + * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG, + * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. + */ +FLASH_Status FLASH_ErasePage(uint32_t Page) { + FLASH_Status status = FLASH_COMPLETE; + /* Check the parameters */ + //ASSERT(IS_FLASH_ADDRESS(Page_Address)); + /* Wait for last operation to be completed */ + status = FLASH_WaitForLastOperation(EraseTimeout); + + if (status == FLASH_COMPLETE) { + /* if the previous operation is completed, proceed to erase the page */ +#if defined(EEPROM_EMU_STM32F401xC) + FLASH->CR &= ~FLASH_CR_SNB; + FLASH->CR |= FLASH_CR_SER | (Page << FLASH_CR_SNB_POS); +#else + FLASH->CR |= FLASH_CR_PER; + FLASH->AR = Page; +#endif + FLASH->CR |= FLASH_CR_STRT; + + /* Wait for last operation to be completed */ + status = FLASH_WaitForLastOperation(EraseTimeout); + /* clear the SER or PER Bit */ +#if defined(EEPROM_EMU_STM32F401xC) + FLASH->CR &= ~(FLASH_CR_SER | FLASH_CR_SNB); +#else + FLASH->CR &= ~FLASH_CR_PER; +#endif + FLASH_ClearFlag(FLASH_SR_FLAGS); + } + /* Return the Erase Status */ + return status; +} + +/** + * @brief Programs a half word at a specified address. + * @param Address: specifies the address to be programmed. + * @param Data: specifies the data to be programmed. + * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG, + * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. + */ +FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data) { + FLASH_Status status = FLASH_BAD_ADDRESS; + if ((Address % sizeof(uint16_t) != 0 ) || !IS_FLASH_ADDRESS(Address)) { + return status; + } + + /* Wait for last operation to be completed */ + status = FLASH_WaitForLastOperation(ProgramTimeout); + if (status == FLASH_COMPLETE) { + /* if the previous operation is completed, proceed to program the new data */ +#if defined(EEPROM_EMU_STM32F401xC) + FLASH->CR &= ~FLASH_CR_PSIZE; + FLASH->CR |= FLASH_PSIZE_HFWORD; +#endif + FLASH->CR |= FLASH_CR_PG; + *(__IO uint16_t*)Address = Data; + /* Wait for last operation to be completed */ + status = FLASH_WaitForLastOperation(ProgramTimeout); + if (status != FLASH_TIMEOUT) { + /* if the program operation is completed, disable the PG Bit */ + FLASH->CR &= ~FLASH_CR_PG; + } + FLASH_ClearFlag(FLASH_SR_FLAGS); + } + return status; +} + +/** + * @brief Unlocks the FLASH Program Erase Controller. + * @param None + * @retval None + */ +void FLASH_Unlock(void) { + /* Authorize the FPEC Access */ + FLASH->KEYR = FLASH_KEY1; + FLASH->KEYR = FLASH_KEY2; +} + +/** + * @brief Locks the FLASH Program Erase Controller. + * @param None + * @retval None + */ +void FLASH_Lock(void) { + /* Set the Lock Bit to lock the FPEC and the FCR */ + FLASH->CR |= FLASH_CR_LOCK; +} + +/** + * @brief Clears the FLASH's pending flags. + * @param FLASH_FLAG: specifies the FLASH flags to clear. + * This parameter can be any combination of the following values: + * @arg FLASH_FLAG_PGERR: FLASH Programming error flag flag + * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag + * @arg FLASH_FLAG_EOP: FLASH End of Programming flag + * @retval None + */ +void FLASH_ClearFlag(uint32_t FLASH_FLAG) { + /* Clear the flags */ + FLASH->SR |= FLASH_FLAG; +} diff --git a/keyboards/yandrstudio/whiteMouse28T/f401/rules.mk b/keyboards/yandrstudio/whiteMouse28T/f401/rules.mk index c92994b02a55..a5a7c16b1bbe 100644 --- a/keyboards/yandrstudio/whiteMouse28T/f401/rules.mk +++ b/keyboards/yandrstudio/whiteMouse28T/f401/rules.mk @@ -7,13 +7,19 @@ STM32_BOOTLOADER_ADDRESS = 0x1FFF0000 # Bootloader selection BOOTLOADER = stm32-dfu - -CUSTOM_MATRIX = lite # for using the A9 pin as matrix io - # # project specific file +CUSTOM_MATRIX = lite # for using the A9 pin as matrix io QUANTUM_SRC += matrix.c - +RGB_MATRIX_ENABLE = yes +RGB_MATRIX_DRIVER = WS2812 +# RGBLIGHT_ENABLE = yes +# RGBLIGHT_DRIVER = WS2812 WS2812_DRIVER = pwm EEPROM_DRIVER = spi +# EEPROM_DRIVER = custom +# SRC += eep/eeprom_stm32.c +# SRC += eep/flash_stm32.c +# OPT_DEFS += -DEEPROM_EMU_STM32F401xC +# COMMON_VPATH += keyboards/yandrstudio/whiteMouse28T/f401/eep diff --git a/keyboards/yandrstudio/whiteMouse28T/keymaps/via/keymap.c b/keyboards/yandrstudio/whiteMouse28T/keymaps/via/keymap.c index 6effcb532d9a..019bc029b7ce 100644 --- a/keyboards/yandrstudio/whiteMouse28T/keymaps/via/keymap.c +++ b/keyboards/yandrstudio/whiteMouse28T/keymaps/via/keymap.c @@ -26,17 +26,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, - KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS), - LAYOUT( - KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, - KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, - KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, - KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS), - LAYOUT( - KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, - KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, - KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, - KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS) + KC_TRNS, KC_TRNS, KC_TRNS, KC_SPACE,KC_TRNS, KC_TRNS, KC_TRNS) }; diff --git a/keyboards/yandrstudio/whiteMouse28T/rules.mk b/keyboards/yandrstudio/whiteMouse28T/rules.mk index a2e0638abb18..9153509f5ad8 100644 --- a/keyboards/yandrstudio/whiteMouse28T/rules.mk +++ b/keyboards/yandrstudio/whiteMouse28T/rules.mk @@ -11,9 +11,5 @@ COMMAND_ENABLE = yes # Commands for debug and configuration SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend # if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work NKRO_ENABLE = yes # USB Nkey Rollover -RGB_MATRIX_ENABLE = yes -RGB_MATRIX_DRIVER = WS2812 -# RGBLIGHT_ENABLE = yes -# RGBLIGHT_DRIVER = WS2812