Bladeren bron

Added Flash Read/Write.

Vladimir N. Shilov 2 jaren geleden
bovenliggende
commit
a707bf31d8
9 gewijzigde bestanden met toevoegingen van 181 en 32 verwijderingen
  1. 0 3
      Inc/board.h
  2. 0 7
      Inc/main.h
  3. 32 0
      Inc/utils.h
  4. 25 1
      MDK-ARM/MNC-IN12x5.uvoptx
  5. 11 1
      MDK-ARM/MNC-IN12x5.uvprojx
  6. 1 0
      Makefile
  7. 0 12
      Src/board.c
  8. 19 8
      Src/clock.c
  9. 93 0
      Src/utils.c

+ 0 - 3
Inc/board.h

@@ -183,7 +183,4 @@ void tube_PowerOn(tube_pos_t tube);
 void tube_PowerOff(tube_pos_t tube);
 void tube_BrightLevel(tube_pos_t tube, uint8_t bright);
 
-int Flash_Write(uint64_t * data);
-int Flash_Read(uint64_t * data);
-
 #endif /* _BPARD_H */

+ 0 - 7
Inc/main.h

@@ -99,13 +99,6 @@ typedef union {
   } s32;                  // element spec. for acc. struct with low or high u16
 } nt32_t;
 
-typedef union {
-  uint64_t  u64;
-  int64_t   i64;
-  uint32_t  u32[2];
-  int32_t   i32[2];
-} nt64_t;
-
 /* Exported constants --------------------------------------------------------*/
 /* Exported macro ------------------------------------------------------------*/
 /* Exported functions prototypes ---------------------------------------------*/

+ 32 - 0
Inc/utils.h

@@ -0,0 +1,32 @@
+#pragma once
+#ifndef _UTILS_H
+#define _UTILS_H
+
+#include "stm32g0xx.h"
+
+/* Type defs */
+typedef union {
+  uint64_t  u64;
+  int64_t   i64;
+  uint32_t  u32[2];
+  int32_t   i32[2];
+} nt64_t;
+
+typedef enum {
+  Flash_Ok = 0,
+  Flash_Error = 1,
+  Flash_PG_End = 2,
+  Flash_PG_Clear = 3
+} flash_result_t;
+
+/* Defines */
+#define FLASH_PAGE_START  0x0800F800
+#define FLASH_PAGE_END    0x0800FFFF
+#define FLASH_PAGE_NMB    31
+#define FLASH_PAGE_NUM    1
+
+/* Functions prototypes */
+flash_result_t Flash_Write(uint64_t * data);
+flash_result_t Flash_Read(uint64_t * data);
+
+#endif /* _UTILS_H */

+ 25 - 1
MDK-ARM/MNC-IN12x5.uvoptx

@@ -575,6 +575,30 @@
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
+    <File>
+      <GroupNumber>2</GroupNumber>
+      <FileNumber>26</FileNumber>
+      <FileType>1</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
+      <PathWithFileName>..\Src\utils.c</PathWithFileName>
+      <FilenameWithoutPath>utils.c</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
+    <File>
+      <GroupNumber>2</GroupNumber>
+      <FileNumber>27</FileNumber>
+      <FileType>5</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
+      <PathWithFileName>..\Inc\utils.h</PathWithFileName>
+      <FilenameWithoutPath>utils.h</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
   </Group>
 
   <Group>
@@ -585,7 +609,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>26</FileNumber>
+      <FileNumber>28</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>

+ 11 - 1
MDK-ARM/MNC-IN12x5.uvprojx

@@ -280,7 +280,7 @@
               </OCR_RVCT4>
               <OCR_RVCT5>
                 <Type>1</Type>
-                <StartAddress>0x800f80</StartAddress>
+                <StartAddress>0x800f800</StartAddress>
                 <Size>0x800</Size>
               </OCR_RVCT5>
               <OCR_RVCT6>
@@ -564,6 +564,16 @@
               <FileType>1</FileType>
               <FilePath>..\Src\clock.c</FilePath>
             </File>
+            <File>
+              <FileName>utils.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\Src\utils.c</FilePath>
+            </File>
+            <File>
+              <FileName>utils.h</FileName>
+              <FileType>5</FileType>
+              <FilePath>..\Inc\utils.h</FilePath>
+            </File>
           </Files>
         </Group>
         <Group>

+ 1 - 0
Makefile

@@ -42,6 +42,7 @@ C_SOURCES =  \
 Src/main.c \
 Src/board.c \
 Src/sensor.c \
+Src/utils.c \
 Src/clock.c \
 Src/stm32g0xx_it.c \
 Src/i2c.c \

+ 0 - 12
Src/board.c

@@ -745,15 +745,3 @@ static void USART1_UART_Init(void)
   {
   }
 }
-
-int Flash_Write(uint64_t * data) {
-  nt64_t val;
-  val.u64 = * data;
-  return 0;
-}
-
-int Flash_Read(uint64_t * data) {
-  nt64_t val;
-  val.u64 = * data;
-  return 0;
-}

+ 19 - 8
Src/clock.c

@@ -1,4 +1,5 @@
 #include "clock.h"
+#include "utils.h"
 
 /* type defs */
 typedef struct t_btn {
@@ -47,14 +48,24 @@ void Clock_Init(void) {
   RTC_ReadAll(&Clock);
   showTime();
 
-  //Lighting.u64 = flash_read();
-  Lighting.name.DayHour = MORNING_HOUR;
-  Lighting.name.NightHour = EVENING_HOUR;
-  Lighting.name.DayBright = DAY_BR_LVL;
-  Lighting.name.NightBright = NIGHT_BR_LVL;
-  Lighting.name.DayMode = light_Rainbow;
-  Lighting.name.NightMode = light_Colour;
-  Lighting.name.NightColour = COLOUR_NIXIE;
+  flash_result_t flash_res = Flash_Read(&Lighting.u64);
+  if ( flash_res != Flash_Ok) {
+    Lighting.name.DayHour = MORNING_HOUR;
+    Lighting.name.NightHour = EVENING_HOUR;
+    Lighting.name.DayBright = DAY_BR_LVL;
+    Lighting.name.NightBright = NIGHT_BR_LVL;
+    Lighting.name.DayMode = light_Rainbow;
+    Lighting.name.NightMode = light_Colour;
+    Lighting.name.NightColour = COLOUR_NIXIE;
+  } else {
+    if (Lighting.name.DayHour == 0xff) {
+      /* launch set parameters
+      ES_SetState(stSetDayHour);
+      setDNbegin();
+      setDayHourBegin();
+      white (ES_GetState() != ???); */
+    }
+  }
 
   check_DayNight();
 }

+ 93 - 0
Src/utils.c

@@ -0,0 +1,93 @@
+#include "utils.h"
+
+/* Defines */
+#define WRITE_KEY1  0x45670123
+#define WRITE_KEY2  0xCDEF89AB
+
+/* Functions */
+flash_result_t Flash_Write(uint64_t * data) {
+  nt64_t val;
+  flash_result_t res = Flash_Ok;
+  uint32_t address = FLASH_PAGE_START;
+
+  val.u64 = *data;
+
+  // search first free cell
+  while ((address < FLASH_PAGE_END) || ((*(__IO uint32_t*)address) != 0xffffffff)) {
+    address += 8;
+  }
+
+  /* Authorize the FLASH Registers access */
+  FLASH->KEYR = WRITE_KEY1;
+  FLASH->KEYR = WRITE_KEY2;
+
+  /* verify Flash is unlock */
+  if (READ_BIT(FLASH->CR, FLASH_CR_LOCK) != 0x00U) {
+    res = Flash_Error;
+  } else {
+
+    if (address > FLASH_PAGE_END) {
+      // no space, format flash page
+      uint32_t tmp;
+
+      /* Get configuration register, then clear page number */
+      tmp = (FLASH->CR & ~FLASH_CR_PNB);
+
+      /* Set page number, Page Erase bit & Start bit */
+      FLASH->CR = (tmp | (FLASH_CR_STRT | (FLASH_PAGE_NMB <<  FLASH_CR_PNB_Pos) | FLASH_CR_PER));
+
+      /* Wait for end of page erase */
+      while ((FLASH->CR & FLASH_CR_STRT) != 0) { __NOP(); }
+
+      address = FLASH_PAGE_START;
+    }
+
+    // write data from address
+    /* Set PG bit */
+    FLASH->CR |= FLASH_CR_PG;
+
+    /* Program first word */
+    *(uint32_t *)address = val.u32[0];
+
+    /* Barrier to ensure programming is performed in 2 steps, in right order
+      (independently of compiler optimization behavior) */
+    __ISB();
+
+    /* Program second word */
+    *(uint32_t *)(address + 4U) = val.u32[1];
+
+    /* Set the LOCK Bit to lock the FLASH Registers access */
+    SET_BIT(FLASH->CR, FLASH_CR_LOCK);
+
+  }
+
+  return res;
+}
+
+/**
+ * @brief Read fom flash latest actual data.
+ * @param data 
+ * @return flash_result_t 
+ */
+flash_result_t Flash_Read(uint64_t * data) {
+  nt64_t val;
+  flash_result_t res = Flash_Ok;
+  uint32_t address = FLASH_PAGE_START;
+
+  while ((address < FLASH_PAGE_END) || ((*(__IO uint32_t*)address) != 0xffffffff)) {
+    val.u32[0] = (*(__IO uint32_t*)address);
+    address += 4;
+    val.u32[1] = (*(__IO uint32_t*)address);
+    address += 4;
+  }
+
+  *data = val.u64;
+
+  if (address == FLASH_PAGE_START) {
+    res = Flash_PG_Clear;
+  } else if (address > FLASH_PAGE_END) {
+    res = Flash_PG_End;
+  }
+
+  return res;
+}