You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by an...@apache.org on 2022/02/16 11:29:14 UTC
[mynewt-nimble] 02/03: babblesim: Add hal_flash and hal_hw_id
This is an automated email from the ASF dual-hosted git repository.
andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git
commit 06a19ab940c6c35943477e3cacf0108b6053ca33
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Mon Feb 14 12:27:16 2022 +0100
babblesim: Add hal_flash and hal_hw_id
This allows to run more apps on BabbleSim, e.g. blecent and bleprph.
---
babblesim/hw/bsp/nrf52_bsim/src/hal_bsp.c | 33 +--
babblesim/hw/bsp/nrf52_bsim/syscfg.yml | 1 -
.../mcu/nordic/nrf52_bsim/include/mcu/native_bsp.h | 34 +++
babblesim/hw/mcu/nordic/nrf52_bsim/src/hal_flash.c | 289 +++++++++++++++++++++
babblesim/hw/mcu/nordic/nrf52_bsim/src/hal_hw_id.c | 73 ++++++
.../hw/mcu/nordic/nrf52_bsim/src/hal_native_priv.h | 27 ++
babblesim/hw/mcu/nordic/nrf52_bsim/syscfg.yml | 4 +-
7 files changed, 432 insertions(+), 29 deletions(-)
diff --git a/babblesim/hw/bsp/nrf52_bsim/src/hal_bsp.c b/babblesim/hw/bsp/nrf52_bsim/src/hal_bsp.c
index 4250249..821e9ed 100644
--- a/babblesim/hw/bsp/nrf52_bsim/src/hal_bsp.c
+++ b/babblesim/hw/bsp/nrf52_bsim/src/hal_bsp.c
@@ -28,13 +28,11 @@
#include "hal/hal_system.h"
#include "mcu/nrf52_hal.h"
#include "mcu/nrf52_periph.h"
+#include "mcu/native_bsp.h"
#include "bsp/bsp.h"
#include "defs/sections.h"
#include "uart_hal/uart_hal.h"
#include "uart/uart.h"
-#if MYNEWT_VAL(ENC_FLASH_DEV)
-#include <ef_nrf5x/ef_nrf5x.h>
-#endif
/*
* What memory to include in coredump.
@@ -46,32 +44,15 @@ static const struct hal_bsp_mem_dump dump_cfg[] = {
}
};
-#if MYNEWT_VAL(ENC_FLASH_DEV)
-static sec_data_secret struct eflash_nrf5x_dev enc_flash_dev0 = {
- .end_dev = {
- .efd_hal = {
- .hf_itf = &enc_flash_funcs,
- },
- .efd_hwdev = &nrf52k_flash_dev
- }
-};
-#endif
-
const struct hal_flash *
hal_bsp_flash_dev(uint8_t id)
{
-// /*
-// * Internal flash mapped to id 0.
-// */
-// if (id == 0) {
-// return &nrf52k_flash_dev;
-// }
-//#if MYNEWT_VAL(ENC_FLASH_DEV)
-// if (id == 1) {
-// return &enc_flash_dev0.end_dev.efd_hal;
-// }
-//#endif
- return NULL;
+ switch (id) {
+ case 0:
+ return &native_flash_dev;
+ default:
+ return NULL;
+ }
}
const struct hal_bsp_mem_dump *
diff --git a/babblesim/hw/bsp/nrf52_bsim/syscfg.yml b/babblesim/hw/bsp/nrf52_bsim/syscfg.yml
index 0e9eb60..2070c69 100644
--- a/babblesim/hw/bsp/nrf52_bsim/syscfg.yml
+++ b/babblesim/hw/bsp/nrf52_bsim/syscfg.yml
@@ -35,7 +35,6 @@ syscfg.defs:
syscfg.vals:
OS_MAIN_STACK_SIZE: 8000
- BLE_HCI_TRANSPORT: uart
MCU_TIMER_POLLER_PRIO: 0
BLE_LL_PRIO: 1
MCU_UART_POLLER_PRIO: 2
diff --git a/babblesim/hw/mcu/nordic/nrf52_bsim/include/mcu/native_bsp.h b/babblesim/hw/mcu/nordic/nrf52_bsim/include/mcu/native_bsp.h
new file mode 100644
index 0000000..dd79487
--- /dev/null
+++ b/babblesim/hw/mcu/nordic/nrf52_bsim/include/mcu/native_bsp.h
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#ifndef H_NATIVE_BSP_
+#define H_NATIVE_BSP_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern const struct hal_flash native_flash_dev;
+
+int uart_set_dev(int port, const char *dev_str);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* H_NATIVE_BSP_ */
diff --git a/babblesim/hw/mcu/nordic/nrf52_bsim/src/hal_flash.c b/babblesim/hw/mcu/nordic/nrf52_bsim/src/hal_flash.c
new file mode 100644
index 0000000..b39b515
--- /dev/null
+++ b/babblesim/hw/mcu/nordic/nrf52_bsim/src/hal_flash.c
@@ -0,0 +1,289 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <assert.h>
+#include <string.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "os/mynewt.h"
+
+#include "hal/hal_flash_int.h"
+#include "mcu/mcu_sim.h"
+
+char *native_flash_file;
+static int file = -1;
+static void *file_loc;
+
+static int native_flash_init(const struct hal_flash *dev);
+static int native_flash_read(const struct hal_flash *dev, uint32_t address,
+ void *dst, uint32_t length);
+static int native_flash_write(const struct hal_flash *dev, uint32_t address,
+ const void *src, uint32_t length);
+static int native_flash_erase_sector(const struct hal_flash *dev,
+ uint32_t sector_address);
+static int native_flash_sector_info(const struct hal_flash *dev, int idx,
+ uint32_t *address, uint32_t *size);
+
+static const struct hal_flash_funcs native_flash_funcs = {
+ .hff_read = native_flash_read,
+ .hff_write = native_flash_write,
+ .hff_erase_sector = native_flash_erase_sector,
+ .hff_sector_info = native_flash_sector_info,
+ .hff_init = native_flash_init
+};
+
+#if MYNEWT_VAL(MCU_FLASH_STYLE_ST)
+static const uint32_t native_flash_sectors[] = {
+ 0x00000000, /* 16 * 1024 */
+ 0x00004000, /* 16 * 1024 */
+ 0x00008000, /* 16 * 1024 */
+ 0x0000c000, /* 16 * 1024 */
+ 0x00010000, /* 64 * 1024 */
+ 0x00020000, /* 128 * 1024 */
+ 0x00040000, /* 128 * 1024 */
+ 0x00060000, /* 128 * 1024 */
+ 0x00080000, /* 128 * 1024 */
+ 0x000a0000, /* 128 * 1024 */
+ 0x000c0000, /* 128 * 1024 */
+ 0x000e0000, /* 128 * 1024 */
+};
+#elif MYNEWT_VAL(MCU_FLASH_STYLE_NORDIC)
+static uint32_t native_flash_sectors[1024 * 1024 / 2048];
+#else
+#error "Need to specify either MCU_FLASH_STYLE_NORDIC or MCU_FLASH_STYLE_ST"
+#endif
+
+#define FLASH_NUM_AREAS (int)(sizeof native_flash_sectors / \
+ sizeof native_flash_sectors[0])
+
+const struct hal_flash native_flash_dev = {
+ .hf_itf = &native_flash_funcs,
+ .hf_base_addr = 0,
+ .hf_size = 1024 * 1024,
+ .hf_sector_cnt = FLASH_NUM_AREAS,
+ .hf_align = MYNEWT_VAL(MCU_FLASH_MIN_WRITE_SIZE),
+ .hf_erased_val = 0xff,
+};
+
+static void
+flash_native_erase(uint32_t addr, uint32_t len)
+{
+ memset(file_loc + addr, 0xff, len);
+}
+
+static void
+flash_native_file_open(char *name)
+{
+ int created = 0;
+ char tmpl[] = "/tmp/native_flash.XXXXXX";
+
+ extern int ftruncate(int fd, off_t length);
+
+ if (file != -1) {
+ close(file);
+ file = -1;
+ }
+
+ if (name) {
+ file = open(name, O_RDWR);
+ if (file < 0) {
+ file = open(name, O_RDWR | O_CREAT, 0660);
+ assert(file > 0);
+ created = 1;
+ }
+ } else {
+ file = mkstemp(tmpl);
+ assert(file > 0);
+ created = 1;
+ }
+
+ if (created) {
+ if (ftruncate(file, native_flash_dev.hf_size) < 0) {
+ assert(0);
+ }
+ }
+
+ if (file_loc != NULL) {
+ munmap(file_loc, native_flash_dev.hf_size);
+ }
+
+ file_loc = mmap(0, native_flash_dev.hf_size,
+ PROT_READ | PROT_WRITE, MAP_SHARED, file, 0);
+ assert(file_loc != MAP_FAILED);
+ if (created) {
+ flash_native_erase(0, native_flash_dev.hf_size);
+ }
+
+ /* If using a temporary file, unlink it immediately. */
+ if (name == NULL) {
+ remove(tmpl);
+ }
+}
+
+static void
+flash_native_ensure_file_open(void)
+{
+ if (file == 0) {
+ flash_native_file_open(NULL);
+ }
+}
+
+static int
+flash_native_write_internal(uint32_t address, const void *src, uint32_t length,
+ int allow_overwrite)
+{
+ static uint8_t buf[256];
+ uint32_t cur;
+ uint32_t end;
+ int chunk_sz;
+ int rc;
+ int i;
+
+ if (length == 0) {
+ return 0;
+ }
+
+ end = address + length;
+
+ flash_native_ensure_file_open();
+
+ cur = address;
+ while (cur < end) {
+ if (end - cur < sizeof buf) {
+ chunk_sz = end - cur;
+ } else {
+ chunk_sz = sizeof buf;
+ }
+
+ /* Ensure data is not being overwritten. */
+ if (!allow_overwrite) {
+ rc = native_flash_read(NULL, cur, buf, chunk_sz);
+ assert(rc == 0);
+ for (i = 0; i < chunk_sz; i++) {
+ assert(buf[i] == 0xff);
+ }
+ }
+
+ cur += chunk_sz;
+ }
+
+ memcpy((char *)file_loc + address, src, length);
+
+ return 0;
+}
+
+static int
+native_flash_write(const struct hal_flash *dev, uint32_t address,
+ const void *src, uint32_t length)
+{
+ assert(address % native_flash_dev.hf_align == 0);
+ return flash_native_write_internal(address, src, length, 0);
+}
+
+int
+flash_native_memset(uint32_t offset, uint8_t c, uint32_t len)
+{
+ memset(file_loc + offset, c, len);
+ return 0;
+}
+
+static int
+native_flash_read(const struct hal_flash *dev, uint32_t address, void *dst,
+ uint32_t length)
+{
+ flash_native_ensure_file_open();
+ memcpy(dst, (char *)file_loc + address, length);
+
+ return 0;
+}
+
+static int
+find_area(uint32_t address)
+{
+ int i;
+
+ for (i = 0; i < FLASH_NUM_AREAS; i++) {
+ if (native_flash_sectors[i] == address) {
+ return i;
+ }
+ }
+
+ return -1;
+}
+
+static int
+flash_sector_len(int sector)
+{
+ uint32_t end;
+
+ if (sector == FLASH_NUM_AREAS - 1) {
+ end = native_flash_dev.hf_size + native_flash_sectors[0];
+ } else {
+ end = native_flash_sectors[sector + 1];
+ }
+ return end - native_flash_sectors[sector];
+}
+
+static int
+native_flash_erase_sector(const struct hal_flash *dev, uint32_t sector_address)
+{
+ int area_id;
+ uint32_t len;
+
+ flash_native_ensure_file_open();
+
+ area_id = find_area(sector_address);
+ if (area_id == -1) {
+ return -1;
+ }
+ len = flash_sector_len(area_id);
+ flash_native_erase(sector_address, len);
+ return 0;
+}
+
+static int
+native_flash_sector_info(const struct hal_flash *dev, int idx,
+ uint32_t *address, uint32_t *size)
+{
+ assert(idx < FLASH_NUM_AREAS);
+
+ *address = native_flash_sectors[idx];
+ *size = flash_sector_len(idx);
+ return 0;
+}
+
+static int
+native_flash_init(const struct hal_flash *dev)
+{
+ //if (native_flash_file) {
+ flash_native_file_open(native_flash_file);
+ //}
+#if MYNEWT_VAL(MCU_FLASH_STYLE_NORDIC)
+ int i;
+
+ for (i = 0; i < FLASH_NUM_AREAS; i++) {
+ native_flash_sectors[i] = i * 2048;
+ }
+#endif
+ return 0;
+}
diff --git a/babblesim/hw/mcu/nordic/nrf52_bsim/src/hal_hw_id.c b/babblesim/hw/mcu/nordic/nrf52_bsim/src/hal_hw_id.c
new file mode 100644
index 0000000..09e3980
--- /dev/null
+++ b/babblesim/hw/mcu/nordic/nrf52_bsim/src/hal_hw_id.c
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <inttypes.h>
+#include <string.h>
+
+#include "hal/hal_bsp.h"
+
+#include "hal_native_priv.h"
+
+#ifndef min
+#define min(a, b) ((a)<(b)?(a):(b))
+#endif
+
+static uint8_t hal_hw_id[HAL_BSP_MAX_ID_LEN];
+static uint8_t hal_hw_id_len;
+
+int
+hal_bsp_hw_id_len(void)
+{
+ if (hal_hw_id_len != 0) {
+ return hal_hw_id_len;
+ } else {
+ return HAL_BSP_MAX_ID_LEN;
+ }
+}
+
+/*
+ * This can be used as the unique hardware identifier for the platform, as
+ * it's supposed to be unique for this particular MCU.
+ */
+int
+hal_bsp_hw_id(uint8_t *id, int max_len)
+{
+ if (hal_hw_id_len) {
+ if (max_len > hal_hw_id_len) {
+ max_len = hal_hw_id_len;
+ }
+ memcpy(id, hal_hw_id, max_len);
+ return max_len;
+ }
+ if (max_len > HAL_BSP_MAX_ID_LEN) {
+ max_len = HAL_BSP_MAX_ID_LEN;
+ }
+ memset(id, 0x42, max_len);
+ return max_len;
+}
+
+void
+hal_bsp_set_hw_id(const uint8_t *id, int len)
+{
+ if (len > HAL_BSP_MAX_ID_LEN) {
+ len = HAL_BSP_MAX_ID_LEN;
+ }
+ hal_hw_id_len = len;
+ memcpy(hal_hw_id, id, len);
+}
diff --git a/babblesim/hw/mcu/nordic/nrf52_bsim/src/hal_native_priv.h b/babblesim/hw/mcu/nordic/nrf52_bsim/src/hal_native_priv.h
new file mode 100644
index 0000000..1060053
--- /dev/null
+++ b/babblesim/hw/mcu/nordic/nrf52_bsim/src/hal_native_priv.h
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef __HAL_NATIVE_PRIV_H__
+#define __HAL_NATIVE_PRIV_H__
+
+#include <stdint.h>
+
+void hal_bsp_set_hw_id(const uint8_t *id, int len);
+
+#endif /* __HAL_NATIVE_PRIV_H__ */
diff --git a/babblesim/hw/mcu/nordic/nrf52_bsim/syscfg.yml b/babblesim/hw/mcu/nordic/nrf52_bsim/syscfg.yml
index b1bc4b8..d6b6695 100644
--- a/babblesim/hw/mcu/nordic/nrf52_bsim/syscfg.yml
+++ b/babblesim/hw/mcu/nordic/nrf52_bsim/syscfg.yml
@@ -478,13 +478,13 @@ syscfg.defs:
value: 1
MCU_FLASH_STYLE_ST:
description: Emulated flash layout is similar to one in STM32.
- value: 1
+ value: 0
restrictions:
- "!MCU_FLASH_STYLE_NORDIC"
MCU_FLASH_STYLE_NORDIC:
description: >
Emulated flash layout is similar to one in NRF51/2 and SAMD21.
- value: 0
+ value: 1
restrictions:
- "!MCU_FLASH_STYLE_ST"
MCU_UART_POLLER_PRIO: