aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--keyboards/matrix/noah/eeprom_f4.c822
-rw-r--r--keyboards/matrix/noah/eeprom_f4.h130
-rw-r--r--keyboards/matrix/noah/rules.mk3
3 files changed, 1 insertions, 954 deletions
diff --git a/keyboards/matrix/noah/eeprom_f4.c b/keyboards/matrix/noah/eeprom_f4.c
deleted file mode 100644
index 3333d7d93..000000000
--- a/keyboards/matrix/noah/eeprom_f4.c
+++ /dev/null
@@ -1,822 +0,0 @@
1/**
2 ******************************************************************************
3 * @file EEPROM/EEPROM_Emulation/src/eeprom.c
4 * @author MCD Application Team
5 * @brief This file provides all the EEPROM emulation firmware functions.
6 ******************************************************************************
7 * @attention
8 *
9 * <h2><center>&copy; Copyright � 2017 STMicroelectronics International N.V.
10 * All rights reserved.</center></h2>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted, provided that the following conditions are met:
14 *
15 * 1. Redistribution of source code must retain the above copyright notice,
16 * this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 * 3. Neither the name of STMicroelectronics nor the names of other
21 * contributors to this software may be used to endorse or promote products
22 * derived from this software without specific written permission.
23 * 4. This software, including modifications and/or derivative works of this
24 * software, must execute solely and exclusively on microcontroller or
25 * microprocessor devices manufactured by or for STMicroelectronics.
26 * 5. Redistribution and use of this software other than as permitted under
27 * this license is void and will automatically terminate your rights under
28 * this license.
29 *
30 * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
31 * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
33 * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
34 * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
35 * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
36 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
38 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
41 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 *
43 ******************************************************************************
44 */
45/** @addtogroup EEPROM_Emulation
46 * @{
47 */
48
49/* Includes ------------------------------------------------------------------*/
50#include "eeprom_f4.h"
51
52/* Private typedef -----------------------------------------------------------*/
53/* Private define ------------------------------------------------------------*/
54/* Private macro -------------------------------------------------------------*/
55/* Private variables ---------------------------------------------------------*/
56
57/* Global variable used to store variable value in read sequence */
58uint16_t DataVar = 0;
59
60/* Virtual address defined by the user: 0xFFFF value is prohibited */
61extern uint16_t VirtAddVarTab[NB_OF_VAR];
62
63/* Private function prototypes -----------------------------------------------*/
64/* Private functions ---------------------------------------------------------*/
65static HAL_StatusTypeDef EE_Format(void);
66static uint16_t EE_FindValidPage(uint8_t Operation);
67static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data);
68static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data);
69static uint16_t EE_VerifyPageFullyErased(uint32_t Address);
70
71/**
72 * @brief Restore the pages to a known good state in case of page's status
73 * corruption after a power loss.
74 * @param None.
75 * @retval - Flash error code: on write Flash error
76 * - FLASH_COMPLETE: on success
77 */
78uint16_t EE_Init(void)
79{
80 uint16_t PageStatus0 = 6, PageStatus1 = 6;
81 uint16_t VarIdx = 0;
82 uint16_t EepromStatus = 0, ReadStatus = 0;
83 int16_t x = -1;
84 HAL_StatusTypeDef FlashStatus;
85
86 FLASH_UnlockF4();
87
88 /* Get Page0 status */
89 PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS);
90 /* Get Page1 status */
91 PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);
92
93 /* Check for invalid header states and repair if necessary */
94 switch (PageStatus0)
95 {
96 case ERASED:
97 if (PageStatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */
98 {
99 /* Erase Page0 */
100 if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS))
101 {
102 FlashStatus = FLASH_EraseSectorF4(PAGE0_ID);
103 /* If erase operation was failed, a Flash error code is returned */
104 if (FlashStatus != HAL_OK)
105 {
106 return FlashStatus;
107 }
108 }
109 }
110 else if (PageStatus1 == RECEIVE_DATA) /* Page0 erased, Page1 receive */
111 {
112 /* Erase Page0 */
113 if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS))
114 {
115 FlashStatus = FLASH_EraseSectorF4(PAGE1_ID);
116 /* If erase operation was failed, a Flash error code is returned */
117 if (FlashStatus != HAL_OK)
118 {
119 return FlashStatus;
120 }
121 }
122 /* Mark Page1 as valid */
123 FlashStatus = FLASH_ProgramHalfWordF4(PAGE1_BASE_ADDRESS, VALID_PAGE);
124 /* If program operation was failed, a Flash error code is returned */
125 if (FlashStatus != HAL_OK)
126 {
127 return FlashStatus;
128 }
129 }
130 else /* First EEPROM access (Page0&1 are erased) or invalid state -> format EEPROM */
131 {
132 /* Erase both Page0 and Page1 and set Page0 as valid page */
133 FlashStatus = EE_Format();
134 /* If erase/program operation was failed, a Flash error code is returned */
135 if (FlashStatus != HAL_OK)
136 {
137 return FlashStatus;
138 }
139 }
140 break;
141
142 case RECEIVE_DATA:
143 if (PageStatus1 == VALID_PAGE) /* Page0 receive, Page1 valid */
144 {
145 /* Transfer data from Page1 to Page0 */
146 for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++)
147 {
148 if (( *(__IO uint16_t*)(PAGE0_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
149 {
150 x = VarIdx;
151 }
152 if (VarIdx != x)
153 {
154 /* Read the last variables' updates */
155 ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
156 /* In case variable corresponding to the virtual address was found */
157 if (ReadStatus != 0x1)
158 {
159 /* Transfer the variable to the Page0 */
160 EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
161 /* If program operation was failed, a Flash error code is returned */
162 if (EepromStatus != HAL_OK)
163 {
164 return EepromStatus;
165 }
166 }
167 }
168 }
169 /* Mark Page0 as valid */
170 FlashStatus = FLASH_ProgramHalfWordF4(PAGE0_BASE_ADDRESS, VALID_PAGE);
171 /* If program operation was failed, a Flash error code is returned */
172 if (FlashStatus != HAL_OK)
173 {
174 return FlashStatus;
175 }
176 /* Erase Page1 */
177 if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS))
178 {
179 FlashStatus = FLASH_EraseSectorF4(PAGE1_ID);
180 /* If erase operation was failed, a Flash error code is returned */
181 if (FlashStatus != HAL_OK)
182 {
183 return FlashStatus;
184 }
185 }
186 }
187 else if (PageStatus1 == ERASED) /* Page0 receive, Page1 erased */
188 {
189 /* Erase Page1 */
190 if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS))
191 {
192 FlashStatus = FLASH_EraseSectorF4(PAGE1_ID);
193 /* If erase operation was failed, a Flash error code is returned */
194 if (FlashStatus != HAL_OK)
195 {
196 return FlashStatus;
197 }
198 }
199 /* Mark Page0 as valid */
200 FlashStatus = FLASH_ProgramHalfWordF4(PAGE0_BASE_ADDRESS, VALID_PAGE);
201 /* If program operation was failed, a Flash error code is returned */
202 if (FlashStatus != HAL_OK)
203 {
204 return FlashStatus;
205 }
206 }
207 else /* Invalid state -> format eeprom */
208 {
209 /* Erase both Page0 and Page1 and set Page0 as valid page */
210 FlashStatus = EE_Format();
211 /* If erase/program operation was failed, a Flash error code is returned */
212 if (FlashStatus != HAL_OK)
213 {
214 return FlashStatus;
215 }
216 }
217 break;
218
219 case VALID_PAGE:
220 if (PageStatus1 == VALID_PAGE) /* Invalid state -> format eeprom */
221 {
222 /* Erase both Page0 and Page1 and set Page0 as valid page */
223 FlashStatus = EE_Format();
224 /* If erase/program operation was failed, a Flash error code is returned */
225 if (FlashStatus != HAL_OK)
226 {
227 return FlashStatus;
228 }
229 }
230 else if (PageStatus1 == ERASED) /* Page0 valid, Page1 erased */
231 {
232 /* Erase Page1 */
233 if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS))
234 {
235 FlashStatus = FLASH_EraseSectorF4(PAGE1_ID);
236 /* If erase operation was failed, a Flash error code is returned */
237 if (FlashStatus != HAL_OK)
238 {
239 return FlashStatus;
240 }
241 }
242 }
243 else /* Page0 valid, Page1 receive */
244 {
245 /* Transfer data from Page0 to Page1 */
246 for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++)
247 {
248 if ((*(__IO uint16_t*)(PAGE1_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
249 {
250 x = VarIdx;
251 }
252 if (VarIdx != x)
253 {
254 /* Read the last variables' updates */
255 ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
256 /* In case variable corresponding to the virtual address was found */
257 if (ReadStatus != 0x1)
258 {
259 /* Transfer the variable to the Page1 */
260 EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
261 /* If program operation was failed, a Flash error code is returned */
262 if (EepromStatus != HAL_OK)
263 {
264 return EepromStatus;
265 }
266 }
267 }
268 }
269 /* Mark Page1 as valid */
270 FlashStatus = FLASH_ProgramHalfWordF4(PAGE1_BASE_ADDRESS, VALID_PAGE);
271 /* If program operation was failed, a Flash error code is returned */
272 if (FlashStatus != HAL_OK)
273 {
274 return FlashStatus;
275 }
276 /* Erase Page0 */
277 if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS))
278 {
279 FlashStatus = FLASH_EraseSectorF4(PAGE0_ID);
280 /* If erase operation was failed, a Flash error code is returned */
281 if (FlashStatus != HAL_OK)
282 {
283 return FlashStatus;
284 }
285 }
286 }
287 break;
288
289 default: /* Any other state -> format eeprom */
290 /* Erase both Page0 and Page1 and set Page0 as valid page */
291 FlashStatus = EE_Format();
292 /* If erase/program operation was failed, a Flash error code is returned */
293 if (FlashStatus != HAL_OK)
294 {
295 return FlashStatus;
296 }
297 break;
298 }
299
300 return HAL_OK;
301}
302
303/**
304 * @brief Verify if specified page is fully erased.
305 * @param Address: page address
306 * This parameter can be one of the following values:
307 * @arg PAGE0_BASE_ADDRESS: Page0 base address
308 * @arg PAGE1_BASE_ADDRESS: Page1 base address
309 * @retval page fully erased status:
310 * - 0: if Page not erased
311 * - 1: if Page erased
312 */
313uint16_t EE_VerifyPageFullyErased(uint32_t Address)
314{
315 uint32_t ReadStatus = 1;
316 uint16_t AddressValue = 0x5555;
317
318 /* Check each active page address starting from end */
319 while (Address <= PAGE0_END_ADDRESS)
320 {
321 /* Get the current location content to be compared with virtual address */
322 AddressValue = (*(__IO uint16_t*)Address);
323
324 /* Compare the read address with the virtual address */
325 if (AddressValue != ERASED)
326 {
327
328 /* In case variable value is read, reset ReadStatus flag */
329 ReadStatus = 0;
330
331 break;
332 }
333 /* Next address location */
334 Address = Address + 4;
335 }
336
337 /* Return ReadStatus value: (0: Page not erased, 1: Sector erased) */
338 return ReadStatus;
339}
340
341/**
342 * @brief Returns the last stored variable data, if found, which correspond to
343 * the passed virtual address
344 * @param VirtAddress: Variable virtual address
345 * @param Data: Global variable contains the read variable value
346 * @retval Success or error status:
347 * - 0: if variable was found
348 * - 1: if the variable was not found
349 * - NO_VALID_PAGE: if no valid page was found.
350 */
351uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data)
352{
353 uint16_t ValidPage = PAGE0;
354 uint16_t AddressValue = 0x5555, ReadStatus = 1;
355 uint32_t Address = EEPROM_START_ADDRESS, PageStartAddress = EEPROM_START_ADDRESS;
356
357 /* Get active Page for read operation */
358 ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
359
360 /* Check if there is no valid page */
361 if (ValidPage == NO_VALID_PAGE)
362 {
363 return NO_VALID_PAGE;
364 }
365
366 /* Get the valid Page start Address */
367 PageStartAddress = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE));
368
369 /* Get the valid Page end Address */
370 Address = (uint32_t)((EEPROM_START_ADDRESS - 2) + (uint32_t)((1 + ValidPage) * PAGE_SIZE));
371
372 /* Check each active page address starting from end */
373 while (Address > (PageStartAddress + 2))
374 {
375 /* Get the current location content to be compared with virtual address */
376 AddressValue = (*(__IO uint16_t*)Address);
377
378 /* Compare the read address with the virtual address */
379 if (AddressValue == VirtAddress)
380 {
381 /* Get content of Address-2 which is variable value */
382 *Data = (*(__IO uint16_t*)(Address - 2));
383
384 /* In case variable value is read, reset ReadStatus flag */
385 ReadStatus = 0;
386
387 break;
388 }
389 else
390 {
391 /* Next address location */
392 Address = Address - 4;
393 }
394 }
395
396 /* Return ReadStatus value: (0: variable exist, 1: variable doesn't exist) */
397 return ReadStatus;
398}
399
400/**
401 * @brief Writes/upadtes variable data in EEPROM.
402 * @param VirtAddress: Variable virtual address
403 * @param Data: 16 bit data to be written
404 * @retval Success or error status:
405 * - FLASH_COMPLETE: on success
406 * - PAGE_FULL: if valid page is full
407 * - NO_VALID_PAGE: if no valid page was found
408 * - Flash error code: on write Flash error
409 */
410uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data)
411{
412 uint16_t Status = 0;
413
414 /* Write the variable virtual address and value in the EEPROM */
415 Status = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
416
417 /* In case the EEPROM active page is full */
418 if (Status == PAGE_FULL)
419 {
420 /* Perform Page transfer */
421 Status = EE_PageTransfer(VirtAddress, Data);
422 }
423
424 /* Return last operation status */
425 return Status;
426}
427
428/**
429 * @brief Erases PAGE and PAGE1 and writes VALID_PAGE header to PAGE
430 * @param None
431 * @retval Status of the last operation (Flash write or erase) done during
432 * EEPROM formating
433 */
434static HAL_StatusTypeDef EE_Format(void)
435{
436 HAL_StatusTypeDef FlashStatus = HAL_OK;
437
438 /* Erase Page0 */
439 if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS))
440 {
441 FlashStatus = FLASH_EraseSectorF4(PAGE0_ID);
442 /* If erase operation was failed, a Flash error code is returned */
443 if (FlashStatus != HAL_OK)
444 {
445 return FlashStatus;
446 }
447 }
448 /* Set Page0 as valid page: Write VALID_PAGE at Page0 base address */
449 FlashStatus = FLASH_ProgramHalfWordF4(PAGE0_BASE_ADDRESS, VALID_PAGE);
450 /* If program operation was failed, a Flash error code is returned */
451 if (FlashStatus != HAL_OK)
452 {
453 return FlashStatus;
454 }
455
456 /* Erase Page1 */
457 if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS))
458 {
459 FlashStatus = FLASH_EraseSectorF4(PAGE1_ID);
460 /* If erase operation was failed, a Flash error code is returned */
461 if (FlashStatus != HAL_OK)
462 {
463 return FlashStatus;
464 }
465 }
466
467 return HAL_OK;
468}
469
470/**
471 * @brief Find valid Page for write or read operation
472 * @param Operation: operation to achieve on the valid page.
473 * This parameter can be one of the following values:
474 * @arg READ_FROM_VALID_PAGE: read operation from valid page
475 * @arg WRITE_IN_VALID_PAGE: write operation from valid page
476 * @retval Valid page number (PAGE or PAGE1) or NO_VALID_PAGE in case
477 * of no valid page was found
478 */
479static uint16_t EE_FindValidPage(uint8_t Operation)
480{
481 uint16_t PageStatus0 = 6, PageStatus1 = 6;
482
483 /* Get Page0 actual status */
484 PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS);
485
486 /* Get Page1 actual status */
487 PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);
488
489 /* Write or read operation */
490 switch (Operation)
491 {
492 case WRITE_IN_VALID_PAGE: /* ---- Write operation ---- */
493 if (PageStatus1 == VALID_PAGE)
494 {
495 /* Page0 receiving data */
496 if (PageStatus0 == RECEIVE_DATA)
497 {
498 return PAGE0; /* Page0 valid */
499 }
500 else
501 {
502 return PAGE1; /* Page1 valid */
503 }
504 }
505 else if (PageStatus0 == VALID_PAGE)
506 {
507 /* Page1 receiving data */
508 if (PageStatus1 == RECEIVE_DATA)
509 {
510 return PAGE1; /* Page1 valid */
511 }
512 else
513 {
514 return PAGE0; /* Page0 valid */
515 }
516 }
517 else
518 {
519 return NO_VALID_PAGE; /* No valid Page */
520 }
521
522 case READ_FROM_VALID_PAGE: /* ---- Read operation ---- */
523 if (PageStatus0 == VALID_PAGE)
524 {
525 return PAGE0; /* Page0 valid */
526 }
527 else if (PageStatus1 == VALID_PAGE)
528 {
529 return PAGE1; /* Page1 valid */
530 }
531 else
532 {
533 return NO_VALID_PAGE ; /* No valid Page */
534 }
535
536 default:
537 return PAGE0; /* Page0 valid */
538 }
539}
540
541/**
542 * @brief Verify if active page is full and Writes variable in EEPROM.
543 * @param VirtAddress: 16 bit virtual address of the variable
544 * @param Data: 16 bit data to be written as variable value
545 * @retval Success or error status:
546 * - FLASH_COMPLETE: on success
547 * - PAGE_FULL: if valid page is full
548 * - NO_VALID_PAGE: if no valid page was found
549 * - Flash error code: on write Flash error
550 */
551static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data)
552{
553 HAL_StatusTypeDef FlashStatus = HAL_OK;
554 uint16_t ValidPage = PAGE0;
555 uint32_t Address = EEPROM_START_ADDRESS, PageEndAddress = EEPROM_START_ADDRESS+PAGE_SIZE;
556
557 /* Get valid Page for write operation */
558 ValidPage = EE_FindValidPage(WRITE_IN_VALID_PAGE);
559
560 /* Check if there is no valid page */
561 if (ValidPage == NO_VALID_PAGE)
562 {
563 return NO_VALID_PAGE;
564 }
565
566 /* Get the valid Page start Address */
567 Address = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE));
568
569 /* Get the valid Page end Address */
570 PageEndAddress = (uint32_t)((EEPROM_START_ADDRESS - 1) + (uint32_t)((ValidPage + 1) * PAGE_SIZE));
571
572 /* Check each active page address starting from begining */
573 while (Address < PageEndAddress)
574 {
575 /* Verify if Address and Address+2 contents are 0xFFFFFFFF */
576 if ((*(__IO uint32_t*)Address) == 0xFFFFFFFF)
577 {
578 /* Set variable data */
579 FlashStatus = FLASH_ProgramHalfWordF4(Address, Data);
580 /* If program operation was failed, a Flash error code is returned */
581 if (FlashStatus != HAL_OK)
582 {
583 return FlashStatus;
584 }
585 /* Set variable virtual address */
586 FlashStatus = FLASH_ProgramHalfWordF4(Address + 2, VirtAddress);
587 /* Return program operation status */
588 return FlashStatus;
589 }
590 else
591 {
592 /* Next address location */
593 Address = Address + 4;
594 }
595 }
596
597 /* Return PAGE_FULL in case the valid page is full */
598 return PAGE_FULL;
599}
600
601/**
602 * @brief Transfers last updated variables data from the full Page to
603 * an empty one.
604 * @param VirtAddress: 16 bit virtual address of the variable
605 * @param Data: 16 bit data to be written as variable value
606 * @retval Success or error status:
607 * - FLASH_COMPLETE: on success
608 * - PAGE_FULL: if valid page is full
609 * - NO_VALID_PAGE: if no valid page was found
610 * - Flash error code: on write Flash error
611 */
612static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
613{
614 HAL_StatusTypeDef FlashStatus = HAL_OK;
615 uint32_t NewPageAddress = EEPROM_START_ADDRESS;
616 uint16_t OldPageId=0;
617 uint16_t ValidPage = PAGE0, VarIdx = 0;
618 uint16_t EepromStatus = 0, ReadStatus = 0;
619
620 /* Get active Page for read operation */
621 ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
622
623 if (ValidPage == PAGE1) /* Page1 valid */
624 {
625 /* New page address where variable will be moved to */
626 NewPageAddress = PAGE0_BASE_ADDRESS;
627
628 /* Old page ID where variable will be taken from */
629 OldPageId = PAGE1_ID;
630 }
631 else if (ValidPage == PAGE0) /* Page0 valid */
632 {
633 /* New page address where variable will be moved to */
634 NewPageAddress = PAGE1_BASE_ADDRESS;
635
636 /* Old page ID where variable will be taken from */
637 OldPageId = PAGE0_ID;
638 }
639 else
640 {
641 return NO_VALID_PAGE; /* No valid Page */
642 }
643
644 /* Set the new Page status to RECEIVE_DATA status */
645 FlashStatus = FLASH_ProgramHalfWordF4(NewPageAddress, RECEIVE_DATA);
646 /* If program operation was failed, a Flash error code is returned */
647 if (FlashStatus != HAL_OK)
648 {
649 return FlashStatus;
650 }
651
652 /* Write the variable passed as parameter in the new active page */
653 EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
654 /* If program operation was failed, a Flash error code is returned */
655 if (EepromStatus != HAL_OK)
656 {
657 return EepromStatus;
658 }
659
660 /* Transfer process: transfer variables from old to the new active page */
661 for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++)
662 {
663 if (VirtAddVarTab[VarIdx] != VirtAddress) /* Check each variable except the one passed as parameter */
664 {
665 /* Read the other last variable updates */
666 ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
667 /* In case variable corresponding to the virtual address was found */
668 if (ReadStatus != 0x1)
669 {
670 /* Transfer the variable to the new active page */
671 EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
672 /* If program operation was failed, a Flash error code is returned */
673 if (EepromStatus != HAL_OK)
674 {
675 return EepromStatus;
676 }
677 }
678 }
679 }
680
681 /* Erase the old Page: Set old Page status to ERASED status */
682 FlashStatus = FLASH_EraseSectorF4(OldPageId);
683 /* If erase operation was failed, a Flash error code is returned */
684 if (FlashStatus != HAL_OK)
685 {
686 return FlashStatus;
687 }
688
689 /* Set new Page status to VALID_PAGE status */
690 FlashStatus = FLASH_ProgramHalfWordF4(NewPageAddress, VALID_PAGE);
691 /* If program operation was failed, a Flash error code is returned */
692 if (FlashStatus != HAL_OK)
693 {
694 return FlashStatus;
695 }
696
697 /* Return last operation flash status */
698 return FlashStatus;
699}
700
701/* flash operation */
702
703#define EraseTimeout ((uint32_t)0x00000FFF)
704#define ProgramTimeout ((uint32_t)0x0000001F)
705
706#define FLASH_PSIZE_BYTE 0
707#define FLASH_PSIZE_HFWORD FLASH_CR_PSIZE_0
708#define FLASH_PSIZE_WORD FLASH_CR_PSIZE_1
709#define FLASH_CR_SNB_Pos 3
710#define FLASH_KEY1_F4 0x45670123
711#define FLASH_KEY2_F4 0xCDEF89AB
712
713#define ASSERT(exp) (void)((0))
714
715static void Flash_Delay(void)
716{
717 __IO uint32_t i = 0;
718 for(i = 0xFF; i != 0; i--) { }
719}
720
721static HAL_StatusTypeDef FLASH_GetStatus(void)
722{
723 if ((FLASH->SR & FLASH_SR_BSY) == FLASH_SR_BSY)
724 return HAL_BUSY;
725
726 if ((FLASH->SR & (FLASH_SR_PGSERR|FLASH_SR_PGPERR|FLASH_SR_PGAERR)) != 0)
727 return HAL_ERROR;
728
729 if ((FLASH->SR & FLASH_SR_WRPERR) != 0 )
730 return HAL_ERROR;
731
732 if ((FLASH->SR & FLASH_SR_SOP) != 0 )
733 return HAL_ERROR;
734
735 return HAL_OK;
736}
737
738static HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t timeout)
739{
740 /* Check for the Flash Status */
741 HAL_StatusTypeDef status = FLASH_GetStatus();
742 /* Wait for a Flash operation to complete or a TIMEOUT to occur */
743 while ((status == HAL_BUSY) && (timeout != 0x00))
744 {
745 Flash_Delay();
746 status = FLASH_GetStatus();
747 timeout--;
748 }
749 if (timeout == 0)
750 status = HAL_TIMEOUT;
751
752 return status;
753}
754HAL_StatusTypeDef FLASH_UnlockF4(void)
755{
756 FLASH->KEYR = FLASH_KEY1_F4;
757 FLASH->KEYR = FLASH_KEY2_F4;
758 return HAL_OK;
759}
760
761HAL_StatusTypeDef FLASH_EraseSectorF4(uint32_t sector)
762{
763 HAL_StatusTypeDef status = HAL_OK;
764 /* Wait for last operation to be completed */
765 status = FLASH_WaitForLastOperation(EraseTimeout);
766
767 if(status == HAL_OK) {
768 /* if the previous operation is completed, proceed to erase the page */
769 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
770 FLASH->CR |= FLASH_PSIZE_WORD;
771 CLEAR_BIT(FLASH->CR, FLASH_CR_SNB);
772 FLASH->CR |= FLASH_CR_SER | (sector << FLASH_CR_SNB_Pos);
773 FLASH->CR |= FLASH_CR_STRT;
774
775 /* Wait for last operation to be completed */
776 status = FLASH_WaitForLastOperation(EraseTimeout);
777 if(status != HAL_TIMEOUT)
778 {
779 /* if the erase operation is completed, disable the PER Bit */
780 FLASH->CR &= ~FLASH_CR_SNB;
781 FLASH->CR &= ~FLASH_CR_SER;
782 }
783
784 FLASH->SR = (FLASH_SR_EOP | FLASH_SR_SOP | FLASH_SR_WRPERR);
785 }
786 /* Return the Erase Status */
787 return status;
788}
789
790HAL_StatusTypeDef FLASH_ProgramHalfWordF4(uint32_t address, uint16_t data)
791{
792 HAL_StatusTypeDef status = HAL_ERROR;
793
794 if (IS_FLASH_ADDRESS(address))
795 {
796 /* Wait for last operation to be completed */
797 status = FLASH_WaitForLastOperation(ProgramTimeout);
798 if(status == HAL_OK) {
799 // set size to byte
800 FLASH->CR &= ~FLASH_CR_PSIZE;
801 FLASH->CR |= FLASH_PSIZE_HFWORD;
802 FLASH->CR |= FLASH_CR_PG;
803
804 *(__IO uint16_t*)address = data;
805 /* Wait for last operation to be completed */
806 status = FLASH_WaitForLastOperation(ProgramTimeout);
807 if(status != HAL_TIMEOUT)
808 {
809 /* if the program operation is completed, disable the PG Bit */
810 FLASH->CR &= ~FLASH_CR_PG;
811 }
812 FLASH->SR = (FLASH_SR_EOP | FLASH_SR_SOP | FLASH_SR_PGSERR | FLASH_SR_PGPERR | FLASH_SR_PGAERR | FLASH_SR_WRPERR);
813 }
814 }
815 return status;
816}
817
818/**
819 * @}
820 */
821
822/******************* (C) COPYRIGHT 2017 STMicroelectronics *****END OF FILE****/
diff --git a/keyboards/matrix/noah/eeprom_f4.h b/keyboards/matrix/noah/eeprom_f4.h
deleted file mode 100644
index bc4fa08cf..000000000
--- a/keyboards/matrix/noah/eeprom_f4.h
+++ /dev/null
@@ -1,130 +0,0 @@
1/**
2 ******************************************************************************
3 * @file EEPROM/EEPROM_Emulation/inc/eeprom.h
4 * @author MCD Application Team
5 * @brief This file contains all the functions prototypes for the EEPROM
6 * emulation firmware library.
7 ******************************************************************************
8 * @attention
9 *
10 * <h2><center>&copy; Copyright � 2017 STMicroelectronics International N.V.
11 * All rights reserved.</center></h2>
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted, provided that the following conditions are met:
15 *
16 * 1. Redistribution of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright notice,
19 * this list of conditions and the following disclaimer in the documentation
20 * and/or other materials provided with the distribution.
21 * 3. Neither the name of STMicroelectronics nor the names of other
22 * contributors to this software may be used to endorse or promote products
23 * derived from this software without specific written permission.
24 * 4. This software, including modifications and/or derivative works of this
25 * software, must execute solely and exclusively on microcontroller or
26 * microprocessor devices manufactured by or for STMicroelectronics.
27 * 5. Redistribution and use of this software other than as permitted under
28 * this license is void and will automatically terminate your rights under
29 * this license.
30 *
31 * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
32 * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
34 * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
35 * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
36 * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
37 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
39 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
40 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 *
44 ******************************************************************************
45 */
46
47/* Define to prevent recursive inclusion -------------------------------------*/
48#ifndef __EEPROM_H
49#define __EEPROM_H
50
51
52/* Includes ------------------------------------------------------------------*/
53#include "hal.h"
54
55typedef enum
56{
57 HAL_OK = 0x00U,
58 HAL_ERROR = 0x01U,
59 HAL_BUSY = 0x02U,
60 HAL_TIMEOUT = 0x03U
61} HAL_StatusTypeDef;
62
63/* Exported constants --------------------------------------------------------*/
64/* EEPROM emulation firmware error codes */
65#define EE_OK (uint32_t)HAL_OK
66#define EE_ERROR (uint32_t)HAL_ERROR
67#define EE_BUSY (uint32_t)HAL_BUSY
68#define EE_TIMEOUT (uint32_t)HAL_TIMEOUT
69
70/* Define the size of the sectors to be used */
71#define PAGE_SIZE (uint32_t)0x4000 /* Page size = 16KByte */
72
73/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
74 be done by word */
75#define VOLTAGE_RANGE (uint8_t)VOLTAGE_RANGE_3
76
77/* EEPROM start address in Flash */
78#define EEPROM_START_ADDRESS ((uint32_t)0x08008000) /* EEPROM emulation start address:
79 from sector2 : after 16KByte of used
80 Flash memory */
81
82/* Pages 0 and 1 base and end addresses */
83#define PAGE0_BASE_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + 0x0000))
84#define PAGE0_END_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + (PAGE_SIZE - 1)))
85#define PAGE0_ID 2//FLASH_SECTOR_2
86
87#define PAGE1_BASE_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + 0x4000))
88#define PAGE1_END_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + (2 * PAGE_SIZE - 1)))
89#define PAGE1_ID 3//FLASH_SECTOR_3
90
91/* Used Flash pages for EEPROM emulation */
92#define PAGE0 ((uint16_t)0x0000)
93#define PAGE1 ((uint16_t)0x0001) /* Page nb between PAGE0_BASE_ADDRESS & PAGE1_BASE_ADDRESS*/
94
95/* No valid page define */
96#define NO_VALID_PAGE ((uint16_t)0x00AB)
97
98/* Page status definitions */
99#define ERASED ((uint16_t)0xFFFF) /* Page is empty */
100#define RECEIVE_DATA ((uint16_t)0xEEEE) /* Page is marked to receive data */
101#define VALID_PAGE ((uint16_t)0x0000) /* Page containing valid data */
102
103/* Valid pages in read and write defines */
104#define READ_FROM_VALID_PAGE ((uint8_t)0x00)
105#define WRITE_IN_VALID_PAGE ((uint8_t)0x01)
106
107/* Page full define */
108#define PAGE_FULL ((uint8_t)0x80)
109
110/* Variables' number */
111#define NB_OF_VAR ((uint8_t)0x16)
112
113/* Exported types ------------------------------------------------------------*/
114/* Exported macro ------------------------------------------------------------*/
115/* Exported functions ------------------------------------------------------- */
116
117HAL_StatusTypeDef FLASH_UnlockF4(void);
118HAL_StatusTypeDef FLASH_EraseSectorF4(uint32_t sector);
119HAL_StatusTypeDef FLASH_ProgramHalfWordF4(uint32_t address, uint16_t data);
120
121uint16_t EE_Init(void);
122uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data);
123uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data);
124
125#define IS_FLASH_ADDRESS(ADDRESS) (((ADDRESS) >= EEPROM_START_ADDRESS) && ((ADDRESS) < PAGE1_END_ADDRESS))
126#define IS_FLASH_SECTOR(SECTOR) (((SECTOR) >= PAGE0_ID) && ((SECTOR) <= PAGE1_ID))
127
128#endif /* __EEPROM_H */
129
130/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff --git a/keyboards/matrix/noah/rules.mk b/keyboards/matrix/noah/rules.mk
index 217e780d8..68163386f 100644
--- a/keyboards/matrix/noah/rules.mk
+++ b/keyboards/matrix/noah/rules.mk
@@ -53,5 +53,4 @@ RGBLIGHT_CUSTOM_DRIVER = yes
53# project specific files 53# project specific files
54SRC += \ 54SRC += \
55 matrix.c \ 55 matrix.c \
56 ws2812_f4.c \ 56 ws2812_f4.c
57 eeprom_f4.c