Skip to content

Commit 0bc6d3a

Browse files
anhmoltlemrey
authored andcommitted
drivers: flash: rram: use NRF_SDH_SOC_OBSERVER to fix write in app
Modifications to make write and erase work in bm application when option CONFIG_NRF_SDH_SOC is set (Common requirement for applications using SoftDevice). Signed-off-by: Andreas Moltumyr <[email protected]>
1 parent 123d2b4 commit 0bc6d3a

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

drivers/flash/soc_flash_nrf_rram.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <zephyr/drivers/flash.h>
1313
#include <zephyr/logging/log.h>
1414
#include <zephyr/irq.h>
15+
#include <zephyr/sys/__assert.h>
1516
#include <zephyr/sys/barrier.h>
1617
#include <hal/nrf_rramc.h>
1718
#include "nrf_soc.h"
@@ -88,6 +89,28 @@ static int nrf_rram_read(const struct device *dev, off_t addr, void *data, size_
8889
return 0;
8990
}
9091

92+
#if defined(CONFIG_NRF_SDH_SOC)
93+
static enum flash_status {
94+
FLASH_STATUS_READY,
95+
FLASH_STATUS_PENDING,
96+
FLASH_STATUS_DONE,
97+
FLASH_STATUS_ERROR,
98+
} flash_status = FLASH_STATUS_READY;
99+
100+
static void soc_evt_flash_handler(uint32_t evt_id, void *context)
101+
{
102+
__ASSERT_NO_MSG(flash_op_status == FLASH_OP_STATUS_PENDING);
103+
104+
if (evt_id == NRF_EVT_FLASH_OPERATION_SUCCESS) {
105+
flash_status = FLASH_STATUS_DONE;
106+
} else if (evt_id == NRF_EVT_FLASH_OPERATION_ERROR) {
107+
flash_status = FLASH_STATUS_ERROR;
108+
}
109+
}
110+
111+
NRF_SDH_SOC_OBSERVER(soc_evt_flash_obs, soc_evt_flash_handler, NULL, 0);
112+
#endif
113+
91114
static int nrf_rram_write(const struct device *dev, off_t addr, const void *data, size_t len)
92115
{
93116
int ret = 0;
@@ -115,16 +138,32 @@ static int nrf_rram_write(const struct device *dev, off_t addr, const void *data
115138

116139
LOG_DBG("Write: %p: %zu", (void *)addr, len);
117140

141+
#if defined(CONFIG_NRF_SDH_SOC)
142+
__ASSERT_NO_MSG(flash_op_status == FLASH_OP_STATUS_READY);
143+
flash_status = FLASH_STATUS_PENDING;
144+
#endif
145+
118146
ret = sd_flash_write((uint32_t *)addr, data, len / sizeof(uint32_t));
119147

120148
if (ret) {
149+
#if defined(CONFIG_NRF_SDH_SOC)
150+
flash_status = FLASH_STATUS_READY;
151+
#endif
121152
return -EIO;
122153
}
123154

124155
while (1) {
156+
sd_app_evt_wait();
157+
158+
#if defined(CONFIG_NRF_SDH_SOC)
159+
if (flash_status != FLASH_STATUS_PENDING) {
160+
ret = (flash_status == FLASH_STATUS_DONE) ? 0 : -EIO;
161+
flash_status = FLASH_STATUS_READY;
162+
break;
163+
}
164+
#else
125165
int taskid;
126166

127-
sd_app_evt_wait();
128167
ret = sd_evt_get(&taskid);
129168

130169
if (!ret && (taskid == NRF_EVT_FLASH_OPERATION_SUCCESS ||
@@ -135,6 +174,7 @@ static int nrf_rram_write(const struct device *dev, off_t addr, const void *data
135174

136175
break;
137176
}
177+
#endif
138178
}
139179

140180
barrier_dmem_fence_full(); /* Barrier following our last write. */

0 commit comments

Comments
 (0)