utzig closed pull request #824: Add stm32 common hal for shared drivers
URL: https://github.com/apache/mynewt-core/pull/824
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/hw/mcu/stm/stm32_common/pkg.yml b/hw/mcu/stm/stm32_common/pkg.yml
new file mode 100644
index 000000000..90575b30f
--- /dev/null
+++ b/hw/mcu/stm/stm32_common/pkg.yml
@@ -0,0 +1,29 @@
+#
+# 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.
+#
+
+pkg.name: hw/mcu/stm/stm32_common
+pkg.description: STM32 shared drivers
+pkg.author: "Apache Mynewt <d...@mynewt.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/";
+pkg.keywords:
+    - stm32
+
+pkg.deps:
+    - hw/hal
+    - hw/cmsis-core
diff --git a/hw/mcu/stm/stm32f1xx/src/hal_os_tick.c 
b/hw/mcu/stm/stm32_common/src/hal_os_tick.c
similarity index 100%
rename from hw/mcu/stm/stm32f1xx/src/hal_os_tick.c
rename to hw/mcu/stm/stm32_common/src/hal_os_tick.c
diff --git a/hw/mcu/stm/stm32f4xx/src/hal_system.c 
b/hw/mcu/stm/stm32_common/src/hal_system.c
similarity index 95%
rename from hw/mcu/stm/stm32f4xx/src/hal_system.c
rename to hw/mcu/stm/stm32_common/src/hal_system.c
index 216783041..57522dc2d 100644
--- a/hw/mcu/stm/stm32f4xx/src/hal_system.c
+++ b/hw/mcu/stm/stm32_common/src/hal_system.c
@@ -17,10 +17,9 @@
  * under the License.
  */
 
-#include <mcu/cortex_m4.h>
+#include "mcu/stm32_hal.h"
 #include <os/os.h>
 #include "hal/hal_system.h"
-#include "stm32f4xx_hal_def.h"
 
 void
 hal_system_reset(void)
diff --git a/hw/mcu/stm/stm32f4xx/src/hal_system_start.c 
b/hw/mcu/stm/stm32_common/src/hal_system_start.c
similarity index 91%
rename from hw/mcu/stm/stm32f4xx/src/hal_system_start.c
rename to hw/mcu/stm/stm32_common/src/hal_system_start.c
index 42c85a062..4f266329d 100644
--- a/hw/mcu/stm/stm32f4xx/src/hal_system_start.c
+++ b/hw/mcu/stm/stm32_common/src/hal_system_start.c
@@ -20,7 +20,8 @@
 #include <assert.h>
 #include <stddef.h>
 #include <inttypes.h>
-#include <mcu/cortex_m4.h>
+
+#include "mcu/stm32_hal.h"
 
 /**
  * Boots the image described by the supplied image header.
@@ -44,9 +45,7 @@ hal_system_start(void *img_start)
     jump_addr = base0entry;
     fn = (jump_fn *)jump_addr;
 
-    /* Remap memory such that flash gets mapped to the code region. */
-    SYSCFG->MEMRMP = 0;
-    __DSB();
+    STM32_HAL_FLASH_REMAP();
 
     /* Jump to image. */
     fn();
diff --git a/hw/mcu/stm/stm32l1xx/src/hal_watchdog.c 
b/hw/mcu/stm/stm32_common/src/hal_watchdog.c
similarity index 95%
rename from hw/mcu/stm/stm32l1xx/src/hal_watchdog.c
rename to hw/mcu/stm/stm32_common/src/hal_watchdog.c
index 1183fc31d..02723f0ee 100644
--- a/hw/mcu/stm/stm32l1xx/src/hal_watchdog.c
+++ b/hw/mcu/stm/stm32_common/src/hal_watchdog.c
@@ -18,8 +18,7 @@
  */
 
 #include "hal/hal_watchdog.h"
-#include "stm32l1xx_hal.h"
-#include "stm32l1xx_hal_iwdg.h"
+#include "mcu/stm32_hal.h"
 
 IWDG_HandleTypeDef g_wdt_cfg;
 
@@ -40,6 +39,7 @@ hal_watchdog_init(uint32_t expire_msecs)
     g_wdt_cfg.Instance = IWDG;
     g_wdt_cfg.Init.Prescaler = IWDG_PRESCALER_256;
     g_wdt_cfg.Init.Reload = reload;
+    STM32_HAL_WATCHDOG_CUSTOM_INIT(&g_wdt_cfg);
 
     return 0;
 }
@@ -56,4 +56,3 @@ hal_watchdog_tickle(void)
 {
     HAL_IWDG_Refresh(&g_wdt_cfg);
 }
-
diff --git a/hw/mcu/stm/stm32f7xx/src/stm32f7xx_hw_id.c 
b/hw/mcu/stm/stm32_common/src/stm32_hw_id.c
similarity index 88%
rename from hw/mcu/stm/stm32f7xx/src/stm32f7xx_hw_id.c
rename to hw/mcu/stm/stm32_common/src/stm32_hw_id.c
index 684756919..4dad67af9 100644
--- a/hw/mcu/stm/stm32f7xx/src/stm32f7xx_hw_id.c
+++ b/hw/mcu/stm/stm32_common/src/stm32_hw_id.c
@@ -20,23 +20,20 @@
 #include <inttypes.h>
 #include <string.h>
 
+#include "mcu/stm32_hal.h"
 #include <hal/hal_bsp.h>
 
 #ifndef min
 #define min(a, b) ((a)<(b)?(a):(b))
 #endif
 
-/*
- * STM32F7 has a unique 96-bit id at address 0x1FF0F420.
- * See ref manual chapter 45.1.
- */
 int
 hal_bsp_hw_id(uint8_t *id, int max_len)
 {
     int cnt;
 
     cnt = min(12, max_len);
-    memcpy(id, (void *)0x1FF0F420, cnt);
+    memcpy(id, (void *)STM32_HW_ID_ADDR, cnt);
 
     return cnt;
 }
diff --git a/hw/mcu/stm/stm32f1xx/src/stm32f1xx_hw_id.c 
b/hw/mcu/stm/stm32f1xx/include/mcu/stm32_hal.h
similarity index 68%
rename from hw/mcu/stm/stm32f1xx/src/stm32f1xx_hw_id.c
rename to hw/mcu/stm/stm32f1xx/include/mcu/stm32_hal.h
index 9fac0a353..9dc7e870d 100644
--- a/hw/mcu/stm/stm32f1xx/src/stm32f1xx_hw_id.c
+++ b/hw/mcu/stm/stm32f1xx/include/mcu/stm32_hal.h
@@ -17,26 +17,34 @@
  * under the License.
  */
 
-#include <inttypes.h>
-#include <string.h>
+#ifndef STM32_HAL_H
+#define STM32_HAL_H
 
-#include <hal/hal_bsp.h>
-
-#ifndef min
-#define min(a, b) ((a)<(b)?(a):(b))
+#ifdef __cplusplus
+extern "C" {
 #endif
 
-/*
+#include <mcu/cortex_m3.h>
+
+#include "stm32f1xx_hal.h"
+#include "stm32f1xx_hal_def.h"
+
+/* hal_watchdog */
+#include "stm32f1xx_hal_iwdg.h"
+#define STM32_HAL_WATCHDOG_CUSTOM_INIT(x)
+
+/* hal_system_start */
+#define STM32_HAL_FLASH_REMAP()
+
+/* stm32_hw_id
+ *
  * STM32F1 has a unique 96-bit id at address 0x1FFFF7E8.
  * See ref manual chapter 30.2.
  */
-int
-hal_bsp_hw_id(uint8_t *id, int max_len)
-{
-    int cnt;
-
-    cnt = min(12, max_len);
-    memcpy(id, (void *)0x1FFFF7E8, cnt);
+#define STM32_HW_ID_ADDR 0x1FFFF7E8
 
-    return cnt;
+#ifdef __cplusplus
 }
+#endif
+
+#endif /* STM32_HAL_H */
diff --git a/hw/mcu/stm/stm32f1xx/pkg.yml b/hw/mcu/stm/stm32f1xx/pkg.yml
index 919a3e025..d7bb5b8f6 100644
--- a/hw/mcu/stm/stm32f1xx/pkg.yml
+++ b/hw/mcu/stm/stm32f1xx/pkg.yml
@@ -35,5 +35,5 @@ pkg.ign_dirs:
 
 pkg.deps:
     - hw/hal
-    - hw/mcu/stm/stm32f1xx
+    - hw/mcu/stm/stm32_common
     - hw/cmsis-core
diff --git a/hw/mcu/stm/stm32f1xx/src/hal_system.c 
b/hw/mcu/stm/stm32f1xx/src/hal_system.c
deleted file mode 100644
index e6408af04..000000000
--- a/hw/mcu/stm/stm32f1xx/src/hal_system.c
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * 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 <mcu/cortex_m3.h>
-#include <os/os.h>
-#include "hal/hal_system.h"
-#include "stm32f1xx_hal_def.h"
-
-void
-hal_system_reset(void)
-{
-    while (1) {
-        if (hal_debugger_connected()) {
-            /*
-             * If debugger is attached, breakpoint here.
-             */
-            asm("bkpt");
-        }
-        NVIC_SystemReset();
-    }
-}
-
-int
-hal_debugger_connected(void)
-{
-    return CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk;
-}
-
-uint32_t
-HAL_GetTick(void)
-{
-    return os_time_get();
-}
-
-HAL_StatusTypeDef
-HAL_InitTick (uint32_t TickPriority)
-{
-    return HAL_OK;
-}
diff --git a/hw/mcu/stm/stm32f1xx/src/hal_system_start.c 
b/hw/mcu/stm/stm32f1xx/src/hal_system_start.c
deleted file mode 100644
index 5e9dee47f..000000000
--- a/hw/mcu/stm/stm32f1xx/src/hal_system_start.c
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * 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 <assert.h>
-#include <stddef.h>
-#include <inttypes.h>
-#include <mcu/cortex_m3.h>
-
-/**
- * Boots the image described by the supplied image header.
- *
- * @param hdr                   The header for the image to boot.
- */
-void
-hal_system_start(void *img_start)
-{
-    typedef void jump_fn(void);
-
-    uint32_t base0entry;
-    uint32_t jump_addr;
-    jump_fn *fn;
-
-    /* First word contains initial MSP value. */
-    __set_MSP(*(uint32_t *)img_start);
-
-    /* Second word contains address of entry point (Reset_Handler). */
-    base0entry = *(uint32_t *)(img_start + 4);
-    jump_addr = base0entry;
-    fn = (jump_fn *)jump_addr;
-
-    /* Remap memory such that flash gets mapped to the code region. */
-    //SYSCFG->MEMRMP = 0;
-    //__DSB();
-
-    /* Jump to image. */
-    fn();
-}
-
diff --git a/hw/mcu/stm/stm32f1xx/src/hal_watchdog.c 
b/hw/mcu/stm/stm32f1xx/src/hal_watchdog.c
deleted file mode 100644
index 7bc5a696b..000000000
--- a/hw/mcu/stm/stm32f1xx/src/hal_watchdog.c
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * 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 "hal/hal_watchdog.h"
-#include "stm32f1xx_hal.h"
-#include "stm32f1xx_hal_iwdg.h"
-
-IWDG_HandleTypeDef g_wdt_cfg;
-
-int
-hal_watchdog_init(uint32_t expire_msecs)
-{
-    uint32_t reload;
-
-    /* Max prescaler is 256 */
-    reload = 32768 / 256;
-    reload = (reload * expire_msecs) / 1000;
-
-    /* Check to make sure we are not trying a reload value that is too large */
-    if (reload > IWDG_RLR_RL) {
-        return -1;
-    }
-
-    g_wdt_cfg.Instance = IWDG;
-    g_wdt_cfg.Init.Prescaler = IWDG_PRESCALER_256;
-    g_wdt_cfg.Init.Reload = reload;
-
-    return 0;
-}
-
-void
-hal_watchdog_enable(void)
-{
-    __HAL_DBGMCU_FREEZE_IWDG();
-    HAL_IWDG_Init(&g_wdt_cfg);
-}
-
-void
-hal_watchdog_tickle(void)
-{
-    HAL_IWDG_Refresh(&g_wdt_cfg);
-}
-
diff --git a/hw/mcu/stm/stm32f4xx/src/stm32f4xx_hw_id.c 
b/hw/mcu/stm/stm32f4xx/include/mcu/stm32_hal.h
similarity index 60%
rename from hw/mcu/stm/stm32f4xx/src/stm32f4xx_hw_id.c
rename to hw/mcu/stm/stm32f4xx/include/mcu/stm32_hal.h
index 19f99db7d..ce4442fe3 100644
--- a/hw/mcu/stm/stm32f4xx/src/stm32f4xx_hw_id.c
+++ b/hw/mcu/stm/stm32f4xx/include/mcu/stm32_hal.h
@@ -17,26 +17,38 @@
  * under the License.
  */
 
-#include <inttypes.h>
-#include <string.h>
+#ifndef STM32_HAL_H
+#define STM32_HAL_H
 
-#include <hal/hal_bsp.h>
-
-#ifndef min
-#define min(a, b) ((a)<(b)?(a):(b))
+#ifdef __cplusplus
+extern "C" {
 #endif
 
-/*
+#include <mcu/cortex_m4.h>
+
+#include "stm32f4xx_hal.h"
+#include "stm32f4xx_hal_def.h"
+
+/* hal_watchdog */
+#include "stm32f4xx_hal_iwdg.h"
+#define STM32_HAL_WATCHDOG_CUSTOM_INIT(x)
+
+/* hal_system_start */
+#define STM32_HAL_FLASH_REMAP()                  \
+    do {                                         \
+        SYSCFG->MEMRMP = 0;                      \
+        __DSB();                                 \
+    } while (0)
+
+/* stm32_hw_id
+ *
  * STM32F4 has a unique 96-bit id at address 0x1FFF7A10.
  * See ref manual chapter 39.1.
  */
-int
-hal_bsp_hw_id(uint8_t *id, int max_len)
-{
-    int cnt;
-
-    cnt = min(12, max_len);
-    memcpy(id, (void *)0x1FFF7A10, cnt);
+#define STM32_HW_ID_ADDR 0x1FFF7A10
 
-    return cnt;
+#ifdef __cplusplus
 }
+#endif
+
+#endif /* STM32_HAL_H */
diff --git a/hw/mcu/stm/stm32f4xx/pkg.yml b/hw/mcu/stm/stm32f4xx/pkg.yml
index 2ebebba03..0b5602455 100644
--- a/hw/mcu/stm/stm32f4xx/pkg.yml
+++ b/hw/mcu/stm/stm32f4xx/pkg.yml
@@ -41,7 +41,7 @@ pkg.ign_files:
 pkg.ign_dirs:
     - "Device"
 
-pkg.deps: 
-    - hw/hal 
-    - hw/mcu/stm/stm32f4xx
+pkg.deps:
+    - hw/hal
+    - hw/mcu/stm/stm32_common
     - hw/cmsis-core
diff --git a/hw/mcu/stm/stm32f4xx/src/hal_os_tick.c 
b/hw/mcu/stm/stm32f4xx/src/hal_os_tick.c
deleted file mode 100644
index 46b935a74..000000000
--- a/hw/mcu/stm/stm32f4xx/src/hal_os_tick.c
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * 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 <assert.h>
-#include <os/os.h>
-#include <hal/hal_os_tick.h>
-
-/*
- * XXX implement tickless mode.
- */
-void
-os_tick_idle(os_time_t ticks)
-{
-    OS_ASSERT_CRITICAL();
-    __DSB();
-    __WFI();
-}
-
-void
-os_tick_init(uint32_t os_ticks_per_sec, int prio)
-{
-    uint32_t reload_val;
-
-    reload_val = ((uint64_t)SystemCoreClock / os_ticks_per_sec) - 1;
-
-    /* Set the system time ticker up */
-    SysTick->LOAD = reload_val;
-    SysTick->VAL = 0;
-    SysTick->CTRL = 0x0007;
-
-    /* Set the system tick priority */
-    NVIC_SetPriority(SysTick_IRQn, prio);
-
-    /*
-     * Keep clocking debug even when CPU is sleeping, stopped or in standby.
-     */
-    DBGMCU->CR |= (DBGMCU_CR_DBG_SLEEP | DBGMCU_CR_DBG_STOP |
-      DBGMCU_CR_DBG_STANDBY);
-}
diff --git a/hw/mcu/stm/stm32f4xx/src/hal_watchdog.c 
b/hw/mcu/stm/stm32f4xx/src/hal_watchdog.c
deleted file mode 100644
index 4d551515d..000000000
--- a/hw/mcu/stm/stm32f4xx/src/hal_watchdog.c
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * 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 "hal/hal_watchdog.h"
-#include "stm32f4xx_hal.h"
-#include "stm32f4xx_hal_iwdg.h"
-
-IWDG_HandleTypeDef g_wdt_cfg;
-
-int
-hal_watchdog_init(uint32_t expire_msecs)
-{
-    uint32_t reload;
-
-    /* Max prescaler is 256 */
-    reload = 32768 / 256;
-    reload = (reload * expire_msecs) / 1000;
-
-    /* Check to make sure we are not trying a reload value that is too large */
-    if (reload > IWDG_RLR_RL) {
-        return -1;
-    }
-
-    g_wdt_cfg.Instance = IWDG;
-    g_wdt_cfg.Init.Prescaler = IWDG_PRESCALER_256;
-    g_wdt_cfg.Init.Reload = reload;
-
-    return 0;
-}
-
-void
-hal_watchdog_enable(void)
-{
-    __HAL_DBGMCU_FREEZE_IWDG();
-    HAL_IWDG_Init(&g_wdt_cfg);
-}
-
-void
-hal_watchdog_tickle(void)
-{
-    HAL_IWDG_Refresh(&g_wdt_cfg);
-}
-
diff --git a/hw/mcu/stm/stm32f7xx/src/hal_watchdog.c 
b/hw/mcu/stm/stm32f7xx/include/mcu/stm32_hal.h
similarity index 51%
rename from hw/mcu/stm/stm32f7xx/src/hal_watchdog.c
rename to hw/mcu/stm/stm32f7xx/include/mcu/stm32_hal.h
index 0d869b24d..9187517c6 100644
--- a/hw/mcu/stm/stm32f7xx/src/hal_watchdog.c
+++ b/hw/mcu/stm/stm32f7xx/include/mcu/stm32_hal.h
@@ -17,43 +17,42 @@
  * under the License.
  */
 
-#include "hal/hal_watchdog.h"
-#include "stm32f7xx_hal.h"
-#include "stm32f7xx_hal_iwdg.h"
-
-IWDG_HandleTypeDef g_wdt_cfg;
+#ifndef STM32_HAL_H
+#define STM32_HAL_H
 
-int
-hal_watchdog_init(uint32_t expire_msecs)
-{
-    uint32_t reload;
+#ifdef __cplusplus
+extern "C" {
+#endif
 
-    /* Max prescaler is 256 */
-    reload = 32768 / 256;
-    reload = (reload * expire_msecs) / 1000;
+#include <mcu/cortex_m7.h>
 
-    /* Check to make sure we are not trying a reload value that is too large */
-    if (reload > IWDG_RLR_RL) {
-        return -1;
-    }
-
-    g_wdt_cfg.Instance = IWDG;
-    g_wdt_cfg.Init.Prescaler = IWDG_PRESCALER_256;
-    g_wdt_cfg.Init.Reload = reload;
-    g_wdt_cfg.Init.Window = IWDG_WINDOW_DISABLE;
+#include "stm32f7xx_hal.h"
+#include "stm32f7xx_hal_def.h"
 
-    return 0;
-}
+/* hal_watchdog */
+#include "stm32f7xx_hal_iwdg.h"
+#define STM32_HAL_WATCHDOG_CUSTOM_INIT(x)           \
+    do {                                            \
+        (x)->Init.Window = IWDG_WINDOW_DISABLE;     \
+    } while (0)
+
+/* hal_system_start */
+#define STM32_HAL_FLASH_REMAP()                  \
+    do {                                         \
+        SYSCFG->MEMRMP = 0;                      \
+        SCB_InvalidateICache();                  \
+        __DSB();                                 \
+    } while (0)
+
+/* stm32_hw_id
+ *
+ * STM32F7 has a unique 96-bit id at address 0x1FF0F420.
+ * See ref manual chapter 45.1.
+ */
+#define STM32_HW_ID_ADDR 0x1FF0F420
 
-void
-hal_watchdog_enable(void)
-{
-    __HAL_DBGMCU_FREEZE_IWDG();
-    HAL_IWDG_Init(&g_wdt_cfg);
+#ifdef __cplusplus
 }
+#endif
 
-void
-hal_watchdog_tickle(void)
-{
-    HAL_IWDG_Refresh(&g_wdt_cfg);
-}
+#endif /* STM32_HAL_H */
diff --git a/hw/mcu/stm/stm32f7xx/pkg.yml b/hw/mcu/stm/stm32f7xx/pkg.yml
index f82185bf9..e0df78f32 100644
--- a/hw/mcu/stm/stm32f7xx/pkg.yml
+++ b/hw/mcu/stm/stm32f7xx/pkg.yml
@@ -35,5 +35,5 @@ pkg.ign_dirs:
 
 pkg.deps:
     - hw/hal
-    - hw/mcu/stm/stm32f7xx
+    - hw/mcu/stm/stm32_common
     - hw/cmsis-core
diff --git a/hw/mcu/stm/stm32f7xx/src/hal_os_tick.c 
b/hw/mcu/stm/stm32f7xx/src/hal_os_tick.c
deleted file mode 100644
index 46b935a74..000000000
--- a/hw/mcu/stm/stm32f7xx/src/hal_os_tick.c
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * 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 <assert.h>
-#include <os/os.h>
-#include <hal/hal_os_tick.h>
-
-/*
- * XXX implement tickless mode.
- */
-void
-os_tick_idle(os_time_t ticks)
-{
-    OS_ASSERT_CRITICAL();
-    __DSB();
-    __WFI();
-}
-
-void
-os_tick_init(uint32_t os_ticks_per_sec, int prio)
-{
-    uint32_t reload_val;
-
-    reload_val = ((uint64_t)SystemCoreClock / os_ticks_per_sec) - 1;
-
-    /* Set the system time ticker up */
-    SysTick->LOAD = reload_val;
-    SysTick->VAL = 0;
-    SysTick->CTRL = 0x0007;
-
-    /* Set the system tick priority */
-    NVIC_SetPriority(SysTick_IRQn, prio);
-
-    /*
-     * Keep clocking debug even when CPU is sleeping, stopped or in standby.
-     */
-    DBGMCU->CR |= (DBGMCU_CR_DBG_SLEEP | DBGMCU_CR_DBG_STOP |
-      DBGMCU_CR_DBG_STANDBY);
-}
diff --git a/hw/mcu/stm/stm32f7xx/src/hal_system.c 
b/hw/mcu/stm/stm32f7xx/src/hal_system.c
deleted file mode 100644
index fa50eadc6..000000000
--- a/hw/mcu/stm/stm32f7xx/src/hal_system.c
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * 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 <mcu/cortex_m7.h>
-#include <os/os.h>
-#include "hal/hal_system.h"
-#include "stm32f7xx_hal_def.h"
-
-void
-hal_system_reset(void)
-{
-    while (1) {
-        if (hal_debugger_connected()) {
-            /*
-             * If debugger is attached, breakpoint here.
-             */
-            asm("bkpt");
-        }
-        NVIC_SystemReset();
-    }
-}
-
-int
-hal_debugger_connected(void)
-{
-    return CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk;
-}
-
-uint32_t
-HAL_GetTick(void)
-{
-    return os_time_get();
-}
-
-HAL_StatusTypeDef
-HAL_InitTick (uint32_t TickPriority)
-{
-    return HAL_OK;
-}
diff --git a/hw/mcu/stm/stm32f7xx/src/hal_system_start.c 
b/hw/mcu/stm/stm32f7xx/src/hal_system_start.c
deleted file mode 100644
index d18bb2173..000000000
--- a/hw/mcu/stm/stm32f7xx/src/hal_system_start.c
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * 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 <assert.h>
-#include <stddef.h>
-#include <inttypes.h>
-#include <mcu/cortex_m7.h>
-
-/**
- * Boots the image described by the supplied image header.
- *
- * @param hdr                   The header for the image to boot.
- */
-void
-hal_system_start(void *img_start)
-{
-    typedef void jump_fn(void);
-
-    uint32_t base0entry;
-    uint32_t jump_addr;
-    jump_fn *fn;
-
-    /* First word contains initial MSP value. */
-    __set_MSP(*(uint32_t *)img_start);
-
-    /* Second word contains address of entry point (Reset_Handler). */
-    base0entry = *(uint32_t *)(img_start + 4);
-    jump_addr = base0entry;
-    fn = (jump_fn *)jump_addr;
-
-    /* Remap memory such that flash gets mapped to the code region. */
-    SYSCFG->MEMRMP = 0;
-    SCB_InvalidateICache();
-    __DSB();
-
-    /* Jump to image. */
-    fn();
-}
-
diff --git a/hw/mcu/stm/stm32l1xx/src/stm32l1xx_hw_id.c 
b/hw/mcu/stm/stm32l1xx/include/mcu/stm32_hal.h
similarity index 62%
rename from hw/mcu/stm/stm32l1xx/src/stm32l1xx_hw_id.c
rename to hw/mcu/stm/stm32l1xx/include/mcu/stm32_hal.h
index 41ef2f958..5cdcfd813 100644
--- a/hw/mcu/stm/stm32l1xx/src/stm32l1xx_hw_id.c
+++ b/hw/mcu/stm/stm32l1xx/include/mcu/stm32_hal.h
@@ -17,29 +17,39 @@
  * under the License.
  */
 
-#include <inttypes.h>
-#include <string.h>
+#ifndef STM32_HAL_H
+#define STM32_HAL_H
 
-#include <hal/hal_bsp.h>
-
-#ifndef min
-#define min(a, b) ((a)<(b)?(a):(b))
+#ifdef __cplusplus
+extern "C" {
 #endif
 
-/*
+#include <mcu/cortex_m3.h>
+
+#include "stm32l1xx_hal.h"
+#include "stm32l1xx_hal_def.h"
+
+/* hal_watchdog */
+#include "stm32l1xx_hal_iwdg.h"
+#define STM32_HAL_WATCHDOG_CUSTOM_INIT(x)
+
+/* hal_system_start */
+#define STM32_HAL_FLASH_REMAP()                  \
+    do {                                         \
+        SYSCFG->MEMRMP = 0;                      \
+        __DSB();                                 \
+    } while (0)
+
+/* stm32_hw_id
+ *
  * STM32L1 has a unique 96-bit id at address either address
  * 0x1FF80050 or 0x1FF800D0 depending on the specific device.
  * See ref manual chapter 31.2.
  */
-int
-hal_bsp_hw_id(uint8_t *id, int max_len)
-{
-    int cnt;
-
-    cnt = min(12, max_len);
-
-    /* 96-bit id address for STM32L152C */
-    memcpy(id, (void *)0x1FF800D0, cnt);
+#define STM32_HW_ID_ADDR 0x1FF800D0
 
-    return cnt;
+#ifdef __cplusplus
 }
+#endif
+
+#endif /* STM32_HAL_H */
diff --git a/hw/mcu/stm/stm32l1xx/pkg.yml b/hw/mcu/stm/stm32l1xx/pkg.yml
index 713c798ac..64e822252 100644
--- a/hw/mcu/stm/stm32l1xx/pkg.yml
+++ b/hw/mcu/stm/stm32l1xx/pkg.yml
@@ -35,5 +35,5 @@ pkg.ign_dirs:
 
 pkg.deps:
     - hw/hal
-    - hw/mcu/stm/stm32l1xx
+    - hw/mcu/stm/stm32_common
     - hw/cmsis-core
diff --git a/hw/mcu/stm/stm32l1xx/src/hal_os_tick.c 
b/hw/mcu/stm/stm32l1xx/src/hal_os_tick.c
deleted file mode 100644
index 46b935a74..000000000
--- a/hw/mcu/stm/stm32l1xx/src/hal_os_tick.c
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * 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 <assert.h>
-#include <os/os.h>
-#include <hal/hal_os_tick.h>
-
-/*
- * XXX implement tickless mode.
- */
-void
-os_tick_idle(os_time_t ticks)
-{
-    OS_ASSERT_CRITICAL();
-    __DSB();
-    __WFI();
-}
-
-void
-os_tick_init(uint32_t os_ticks_per_sec, int prio)
-{
-    uint32_t reload_val;
-
-    reload_val = ((uint64_t)SystemCoreClock / os_ticks_per_sec) - 1;
-
-    /* Set the system time ticker up */
-    SysTick->LOAD = reload_val;
-    SysTick->VAL = 0;
-    SysTick->CTRL = 0x0007;
-
-    /* Set the system tick priority */
-    NVIC_SetPriority(SysTick_IRQn, prio);
-
-    /*
-     * Keep clocking debug even when CPU is sleeping, stopped or in standby.
-     */
-    DBGMCU->CR |= (DBGMCU_CR_DBG_SLEEP | DBGMCU_CR_DBG_STOP |
-      DBGMCU_CR_DBG_STANDBY);
-}
diff --git a/hw/mcu/stm/stm32l1xx/src/hal_system.c 
b/hw/mcu/stm/stm32l1xx/src/hal_system.c
deleted file mode 100644
index 7dc83fc09..000000000
--- a/hw/mcu/stm/stm32l1xx/src/hal_system.c
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * 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 <mcu/cortex_m3.h>
-#include <os/os.h>
-#include "hal/hal_system.h"
-#include "stm32l1xx_hal_def.h"
-
-void
-hal_system_reset(void)
-{
-    while (1) {
-        if (hal_debugger_connected()) {
-            /*
-             * If debugger is attached, breakpoint here.
-             */
-            asm("bkpt");
-        }
-        NVIC_SystemReset();
-    }
-}
-
-int
-hal_debugger_connected(void)
-{
-    return CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk;
-}
-
-uint32_t
-HAL_GetTick(void)
-{
-    return os_time_get();
-}
-
-HAL_StatusTypeDef
-HAL_InitTick (uint32_t TickPriority)
-{
-    return HAL_OK;
-}
diff --git a/hw/mcu/stm/stm32l1xx/src/hal_system_start.c 
b/hw/mcu/stm/stm32l1xx/src/hal_system_start.c
deleted file mode 100644
index 872558b44..000000000
--- a/hw/mcu/stm/stm32l1xx/src/hal_system_start.c
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * 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 <assert.h>
-#include <stddef.h>
-#include <inttypes.h>
-#include <mcu/cortex_m3.h>
-
-/**
- * Boots the image described by the supplied image header.
- *
- * @param hdr                   The header for the image to boot.
- */
-void
-hal_system_start(void *img_start)
-{
-    typedef void jump_fn(void);
-
-    uint32_t base0entry;
-    uint32_t jump_addr;
-    jump_fn *fn;
-
-    /* First word contains initial MSP value. */
-    __set_MSP(*(uint32_t *)img_start);
-
-    /* Second word contains address of entry point (Reset_Handler). */
-    base0entry = *(uint32_t *)(img_start + 4);
-    jump_addr = base0entry;
-    fn = (jump_fn *)jump_addr;
-
-    /* Remap memory such that flash gets mapped to the code region. */
-    SYSCFG->MEMRMP = 0;
-    __DSB();
-
-    /* Jump to image. */
-    fn();
-}
-


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to