nrf: Make sure to not use SD functions when SD not available

This commit is contained in:
arturo182 2018-02-07 20:35:06 +01:00
parent 9d191f7f9f
commit 47c8e20e5a
2 changed files with 60 additions and 17 deletions

View File

@ -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;
}

View File

@ -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 (NRF_SUCCESS != sd_flash_write((uint32_t*) flash_align, (uint32_t*) buf, FLASH_PAGE_SIZE/4)) {
return false;
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);
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();