Welcome to mirror list, hosted at ThFree Co, Russian Federation.

hw_flash.c « Src « Core « BLE_MeshLightingLPN « BLE « Applications « P-NUCLEO-WB55.USBDongle « Projects - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7355cea622b18b3e2dcadd9a791bd8ec9bea7a00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
  ******************************************************************************
 * @file    hw_flash.c
 * @author  MCD Application Team
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2019-2021 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */

#include "common.h"
#include "stm32_seq.h"
//#include "dbg_gpio.h"
#include "hw_flash.h"
#include "stm32wbxx_hal.h"

static void HW_FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
static void HW_FLASH_PageErase(uint32_t Page);
//static void HW_FLASH_WaitEndOfOperation(void);

/*****************************************************************************/

MOBLE_RESULT HW_FLASH_Write(uint32_t address, uint64_t data)
{
  /* Enable EOP interrupt */
 // __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);

 // __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);

  HW_FLASH_Program_DoubleWord(address, data);

 // HW_FLASH_WaitEndOfOperation();

  /* Disable EOP interrupt */
//  __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP);

  /* Clear the PG bit once data has been written */
  CLEAR_BIT(FLASH->CR, FLASH_CR_PG);

  return (MOBLE_RESULT_SUCCESS);
}

/*****************************************************************************/

MOBLE_RESULT HW_FLASH_Erase(uint32_t page, uint16_t n, int interrupt)
{
  UNUSED(interrupt);

  uint32_t loop;

  /* Enable EOP interrupt */
  __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);

  for( loop = 0;  loop < n ; loop++)
  {
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);

    HW_FLASH_PageErase(page+loop);

//    HW_FLASH_WaitEndOfOperation();
  }

  /* Disable EOP interrupt */
  __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP);

  /* Clear the page erase bit */
  CLEAR_BIT(FLASH->CR, (FLASH_CR_PER | FLASH_CR_PNB));

  return (MOBLE_RESULT_SUCCESS);
}

#if 0
/*****************************************************************************/

uint32_t HW_FLASH_OB_GetIPCCBufferAddr(void)
{
  return READ_BIT(FLASH->IPCCBR, FLASH_IPCCBR_IPCCDBA);
}

/*****************************************************************************/

uint32_t HW_FLASH_OB_GetSFSA(void)
{
  return (READ_BIT(FLASH->SFR, FLASH_SFR_SFSA) >> FLASH_SFR_SFSA_Pos);
}

/*****************************************************************************/

uint32_t HW_FLASH_OB_GetSBRSA(void)
{
  return (READ_BIT(FLASH->SRRVR, FLASH_SRRVR_SBRSA) >> FLASH_SRRVR_SBRSA_Pos);
}

/*****************************************************************************/

uint32_t HW_FLASH_OB_GetSNBRSA(void)
{
  return (READ_BIT(FLASH->SRRVR, FLASH_SRRVR_SNBRSA) >> FLASH_SRRVR_SNBRSA_Pos);
}
#endif

/*************************************************************
 *
 * LOCAL FUNCTIONS
 *
 *************************************************************/

/**
 * This is a copy of FLASH_Program_DoubleWord() from the HAL
 */
static void HW_FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
{
//  DBG_GPIO_Gr2Set(DBG_GPIO_GR2_FLASH_WRITE);

  /* Set PG bit */
  SET_BIT(FLASH->CR, FLASH_CR_PG);

  /* Program first word */
  *(uint32_t *)Address = (uint32_t)Data;

  /* 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) = (uint32_t)(Data >> 32U);

//  DBG_GPIO_Gr2Reset(DBG_GPIO_GR2_FLASH_WRITE);
}

/**
 * This is a copy of LASH_PageErase() from the HAL
 */
static void HW_FLASH_PageErase(uint32_t Page)
{
//  DBG_GPIO_Gr2Set(DBG_GPIO_GR2_FLASH_ERASE);

  /* Proceed to erase the page */
  MODIFY_REG(FLASH->CR, FLASH_CR_PNB, ((Page << FLASH_CR_PNB_Pos) | FLASH_CR_PER | FLASH_CR_STRT));

//  DBG_GPIO_Gr2Set(DBG_GPIO_GR2_FLASH_ERASE);
}