You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ma...@apache.org on 2015/11/26 00:56:32 UTC
[7/7] incubator-mynewt-larva git commit: First take on HAL flash
implementation for STM32F3.
First take on HAL flash implementation for STM32F3.
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/b544c2a1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/b544c2a1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/b544c2a1
Branch: refs/heads/master
Commit: b544c2a12e958ac3585f72a0ac2546f10eaeee65
Parents: 0364a00
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Nov 25 15:54:54 2015 -0800
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Wed Nov 25 15:54:54 2015 -0800
----------------------------------------------------------------------
hw/bsp/stm32f3discovery/src/hal_bsp.c | 18 ++-
hw/bsp/stm32f3discovery/src/os_bsp.c | 20 ++++
hw/mcu/stm/stm32f3xx/include/mcu/stm32f3_bsp.h | 3 +
hw/mcu/stm/stm32f3xx/src/hal_flash.c | 122 ++++++++++++++++++++
4 files changed, 162 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b544c2a1/hw/bsp/stm32f3discovery/src/hal_bsp.c
----------------------------------------------------------------------
diff --git a/hw/bsp/stm32f3discovery/src/hal_bsp.c b/hw/bsp/stm32f3discovery/src/hal_bsp.c
index 74c468c..bcdd8b9 100644
--- a/hw/bsp/stm32f3discovery/src/hal_bsp.c
+++ b/hw/bsp/stm32f3discovery/src/hal_bsp.c
@@ -13,7 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+#include <stddef.h>
+
#include "hal/hal_gpio.h"
+#include "hal/hal_flash_int.h"
#include "mcu/stm32f30x.h"
#include "mcu/stm32f30x_rcc.h"
#include "mcu/stm32f30x_gpio.h"
@@ -34,8 +37,21 @@ static const struct stm32f3_uart_cfg uart_cfg[UART_CNT] = {
}
};
-const struct stm32f3_uart_cfg *bsp_uart_config(int port)
+const struct stm32f3_uart_cfg *
+bsp_uart_config(int port)
{
assert(port < UART_CNT);
return &uart_cfg[port];
}
+
+const struct hal_flash *
+bsp_flash_dev(uint8_t id)
+{
+ /*
+ * Internal flash mapped to id 0.
+ */
+ if (id != 0) {
+ return NULL;
+ }
+ return &stm32f3_flash_dev;
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b544c2a1/hw/bsp/stm32f3discovery/src/os_bsp.c
----------------------------------------------------------------------
diff --git a/hw/bsp/stm32f3discovery/src/os_bsp.c b/hw/bsp/stm32f3discovery/src/os_bsp.c
index 865b804..aa69c45 100644
--- a/hw/bsp/stm32f3discovery/src/os_bsp.c
+++ b/hw/bsp/stm32f3discovery/src/os_bsp.c
@@ -16,6 +16,24 @@
void *_sbrk(int incr);
void _close(int fd);
+/*
+ * XXXX for now have it here.
+ */
+#include <util/flash_map.h>
+
+static struct flash_area bsp_flash_areas[] = {
+ [FLASH_AREA_IMAGE_0] = {
+ .fa_flash_id = 0,
+ .fa_off = 0x08000000,
+ .fa_size = (192 * 1024)
+ },
+ [FLASH_AREA_NFFS] = {
+ .fa_flash_id = 0,
+ .fa_off = 0x08030000,
+ .fa_size = (32 * 1024)
+ }
+};
+
void
os_bsp_init(void)
{
@@ -24,4 +42,6 @@ os_bsp_init(void)
*/
_sbrk(0);
_close(0);
+ flash_area_init(bsp_flash_areas,
+ sizeof(bsp_flash_areas) / sizeof(bsp_flash_areas[0]));
}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b544c2a1/hw/mcu/stm/stm32f3xx/include/mcu/stm32f3_bsp.h
----------------------------------------------------------------------
diff --git a/hw/mcu/stm/stm32f3xx/include/mcu/stm32f3_bsp.h b/hw/mcu/stm/stm32f3xx/include/mcu/stm32f3_bsp.h
index 58023fb..d9871d4 100644
--- a/hw/mcu/stm/stm32f3xx/include/mcu/stm32f3_bsp.h
+++ b/hw/mcu/stm/stm32f3xx/include/mcu/stm32f3_bsp.h
@@ -39,4 +39,7 @@ const struct stm32f3_uart_cfg *bsp_uart_config(int port);
*/
int hal_gpio_init_af(int pin, uint8_t af_type, enum gpio_pull pull);
+struct hal_flash;
+extern const struct hal_flash stm32f3_flash_dev;
+
#endif /* __MCU_STM32F3_BSP_H_ */
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b544c2a1/hw/mcu/stm/stm32f3xx/src/hal_flash.c
----------------------------------------------------------------------
diff --git a/hw/mcu/stm/stm32f3xx/src/hal_flash.c b/hw/mcu/stm/stm32f3xx/src/hal_flash.c
new file mode 100644
index 0000000..35cc31b
--- /dev/null
+++ b/hw/mcu/stm/stm32f3xx/src/hal_flash.c
@@ -0,0 +1,122 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed 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.
+ */
+
+/*
+ * Internal flash for STM32F3.
+ * Size of the flash depends on the MCU model, flash is memory mapped
+ * and is divided to 2k sectors throughout.
+ * Programming is done 2 bytes at a time.
+ */
+#include <string.h>
+#include <assert.h>
+#include <hal/hal_flash_int.h>
+
+#include "mcu/stm32f30x.h"
+#include "mcu/stm32f30x_flash.h"
+
+#define STM32F3_BASE_ADDR 0x08000000
+#define STM32F3_ADDR_2_SEC_IDX(a) (((a) - STM32F3_BASE_ADDR) / 2048)
+#define STM32F3_SEC_IDX_2_ADDR(idx) (STM32F3_BASE_ADDR + ((idx) * 2048))
+
+static int stm32f3_flash_read(uint32_t address, void *dst, uint32_t num_bytes);
+static int stm32f3_flash_write(uint32_t address, const void *src,
+ uint32_t num_bytes);
+static int stm32f3_flash_erase_sector(uint32_t sector_address);
+static int stm32f3_flash_sector_info(int idx, uint32_t *addr, uint32_t *sz);
+static int stm32f3_flash_init(void);
+
+static const struct hal_flash_funcs stm32f3_flash_funcs = {
+ .hff_read = stm32f3_flash_read,
+ .hff_write = stm32f3_flash_write,
+ .hff_erase_sector = stm32f3_flash_erase_sector,
+ .hff_sector_info = stm32f3_flash_sector_info,
+ .hff_init = stm32f3_flash_init
+};
+
+const struct hal_flash stm32f3_flash_dev = {
+ .hf_itf = &stm32f3_flash_funcs,
+ .hf_base_addr = STM32F3_BASE_ADDR,
+#ifdef STM32F303xC
+ .hf_size = 256 * 1024,
+ .hf_sector_cnt = 128
+#endif
+};
+
+static int
+stm32f3_flash_read(uint32_t address, void *dst, uint32_t num_bytes)
+{
+ memcpy(dst, (void *)address, num_bytes);
+ return 0;
+}
+
+static int
+stm32f3_flash_write(uint32_t address, const void *src, uint32_t len)
+{
+ FLASH_Status rc;
+ int num_half_words;
+ int i;
+ uint16_t *src16 = (uint16_t *)src;
+
+ if (address % sizeof(uint16_t)) {
+ /*
+ * Unaligned write.
+ */
+ return -1;
+ }
+ num_half_words = len / 2;
+
+ for (i = 0; i < num_half_words; i++) {
+ rc = FLASH_ProgramHalfWord(address, src16[i]);
+ if (rc != FLASH_COMPLETE) {
+ goto err;
+ }
+ address += 2;
+ }
+ if (num_half_words * 2 != len) {
+ rc = FLASH_ProgramHalfWord(address, ((uint8_t *)src)[len] << 8);
+ if (rc != FLASH_COMPLETE) {
+ goto err;
+ }
+ }
+ return 0;
+err:
+ return -1;
+}
+
+static int
+stm32f3_flash_erase_sector(uint32_t sector_address)
+{
+ if (FLASH_ErasePage(sector_address) == FLASH_COMPLETE) {
+ return 0;
+ }
+ return -1;
+}
+
+static int
+stm32f3_flash_sector_info(int idx, uint32_t *addr, uint32_t *sz)
+{
+ assert(idx < stm32f3_flash_dev.hf_sector_cnt);
+ *addr = STM32F3_SEC_IDX_2_ADDR(idx);
+ *sz = 2048;
+ return 0;
+}
+
+static int
+stm32f3_flash_init(void)
+{
+ FLASH_Unlock();
+ return 0;
+}