summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorarturo182 <arturo182@tlen.pl>2018-02-07 20:35:06 +0100
committerarturo182 <arturo182@tlen.pl>2018-02-07 22:41:20 +0100
commit47c8e20e5a17b72078faa4b1962c515def819578 (patch)
treef4f3e3d3fe6b4330671996efe8e74b866a152d89
parent9d191f7f9f386bb62f32b3aa9c6534dc84f9a48a (diff)
nrf: Make sure to not use SD functions when SD not available
-rw-r--r--ports/nrf/common-hal/os/__init__.c24
-rw-r--r--ports/nrf/internal_flash.c51
2 files changed, 59 insertions, 16 deletions
diff --git a/ports/nrf/common-hal/os/__init__.c b/ports/nrf/common-hal/os/__init__.c
index 0e6d63347..402b32bc5 100644
--- a/ports/nrf/common-hal/os/__init__.c
+++ b/ports/nrf/common-hal/os/__init__.c
@@ -30,8 +30,11 @@
#include "py/objtuple.h"
#include "py/qstr.h"
+#ifdef BLUETOOTH_SD
#include "nrf_sdm.h"
-#include "nrf_soc.h"
+#endif
+
+#include "nrf.h"
STATIC const qstr os_uname_info_fields[] = {
MP_QSTR_sysname, MP_QSTR_nodename,
@@ -61,15 +64,15 @@ mp_obj_t common_hal_os_uname(void) {
}
bool common_hal_os_urandom(uint8_t* buffer, uint32_t length) {
- uint8_t sd_en = 0;
- (void) sd_softdevice_is_enabled(&sd_en);
+#ifdef BLUETOOTH_SD
+ uint8_t sd_en = 0;
+ (void) sd_softdevice_is_enabled(&sd_en);
+
+ if (sd_en) {
+ return NRF_SUCCESS == sd_rand_application_vector_get(buffer,length);
+ }
+#endif
- if ( sd_en )
- {
- return NRF_SUCCESS == sd_rand_application_vector_get(buffer,length);
- }else
- {
- // SoftDevice is not enabled.
NRF_RNG->EVENTS_VALRDY = 0;
NRF_RNG->TASKS_START = 1;
@@ -82,7 +85,6 @@ bool common_hal_os_urandom(uint8_t* buffer, uint32_t length) {
}
NRF_RNG->TASKS_STOP = 1;
- }
- return true;
+ return true;
}
diff --git a/ports/nrf/internal_flash.c b/ports/nrf/internal_flash.c
index 4a1faad56..1681dc1c4 100644
--- a/ports/nrf/internal_flash.c
+++ b/ports/nrf/internal_flash.c
@@ -37,7 +37,10 @@
#include "supervisor/shared/rgb_led_status.h"
#include "nrf.h"
-#include "nrf_soc.h"
+
+#ifdef BLUETOOTH_SD
+#include "nrf_sdm.h"
+#endif
// defined in linker
extern uint32_t __fatfs_flash_start_addr[];
@@ -83,6 +86,8 @@ static uint32_t convert_block_to_flash_addr(uint32_t block) {
}
bool internal_flash_write_block(const uint8_t *src, uint32_t block) {
+ uint8_t sd_en = 0;
+
#ifdef MICROPY_HW_LED_MSC
port_pin_set_output_level(MICROPY_HW_LED_MSC, true);
#endif
@@ -99,12 +104,48 @@ bool internal_flash_write_block(const uint8_t *src, uint32_t block) {
memcpy(buf, flash_align, FLASH_PAGE_SIZE);
memcpy(buf + (dest%FLASH_PAGE_SIZE), src, FILESYSTEM_BLOCK_SIZE);
- if (NRF_SUCCESS != sd_flash_page_erase(pagenum)) {
- return false;
+#ifdef BLUETOOTH_SD
+ (void) sd_softdevice_is_enabled(&sd_en);
+
+ if (sd_en) {
+ if (NRF_SUCCESS != sd_flash_page_erase(pagenum)) {
+ return false;
+ }
+
+ if (NRF_SUCCESS != sd_flash_write((uint32_t*) flash_align, (uint32_t*) buf, FLASH_PAGE_SIZE / sizeof(uint32_t))) {
+ return false;
+ }
}
+#endif
+
+ if (!sd_en) {
+ // Erase
+ NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Een << NVMC_CONFIG_WEN_Pos);
+ while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
- if (NRF_SUCCESS != sd_flash_write((uint32_t*) flash_align, (uint32_t*) buf, FLASH_PAGE_SIZE/4)) {
- return false;
+ NRF_NVMC->ERASEPAGE = dest;
+ while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
+
+ NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
+ while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
+
+ // Write
+ uint32_t *src = (uint32_t*) buf;
+ uint32_t i = 0;
+
+ while (i < (FLASH_PAGE_SIZE / sizeof(uint32_t))) {
+ NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos);
+ while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
+
+ *flash_align++ = *src++;
+
+ while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
+
+ NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
+ while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
+
+ ++i;
+ }
}
clear_temp_status();