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 2016/04/11 23:25:36 UTC

[06/28] incubator-mynewt-core git commit: Pass the interrupt priority as a parameter to 'os_bsp_systick_init()' instead of hardcoding it in the BSP.

Pass the interrupt priority as a parameter to 'os_bsp_systick_init()'
instead of hardcoding it in the BSP.

Configure TIMER1 to run at a frequency of 1MHz and generate a periodic
timer interrupt to drive the OS tick.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/b387dc65
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/b387dc65
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/b387dc65

Branch: refs/heads/develop
Commit: b387dc6525656ff77a17f9adb0c34be2e170ae5f
Parents: bb403e7
Author: Neel Natu <ne...@nahannisys.com>
Authored: Thu Mar 24 13:18:49 2016 -0700
Committer: Neel Natu <ne...@nahannisys.com>
Committed: Thu Mar 24 13:18:49 2016 -0700

----------------------------------------------------------------------
 hw/bsp/nrf51dk/src/os_bsp.c                     |  6 +--
 hw/bsp/nrf52pdk/src/os_bsp.c                    | 50 ++++++++++++++++++++
 .../nrf51xxx/include/mcu/nrf51_bitfields.h      |  3 ++
 .../nrf52xxx/include/mcu/nrf52_bitfields.h      |  4 ++
 libs/os/include/os/arch/cortex_m0/os/os_arch.h  |  2 +-
 libs/os/include/os/arch/cortex_m4/os/os_arch.h  |  2 +-
 libs/os/src/arch/cortex_m0/os_arch_arm.c        |  2 +-
 libs/os/src/arch/cortex_m4/os_arch_arm.c        | 25 +---------
 8 files changed, 63 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b387dc65/hw/bsp/nrf51dk/src/os_bsp.c
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf51dk/src/os_bsp.c b/hw/bsp/nrf51dk/src/os_bsp.c
index e09bdee..b8e3608 100644
--- a/hw/bsp/nrf51dk/src/os_bsp.c
+++ b/hw/bsp/nrf51dk/src/os_bsp.c
@@ -22,8 +22,6 @@
 #include "mcu/nrf51_bitfields.h"
 #include "mcu/nrf51_hal.h"
 
-#define BSP_LOWEST_PRIO     ((1 << __NVIC_PRIO_BITS) - 1)
-
 static struct flash_area bsp_flash_areas[] = {
     [FLASH_AREA_BOOTLOADER] = {
         .fa_flash_id = 0,       /* internal flash */
@@ -94,7 +92,7 @@ rtc0_timer_handler(void)
 }
 
 void
-os_bsp_systick_init(uint32_t os_ticks_per_sec)
+os_bsp_systick_init(uint32_t os_ticks_per_sec, int prio)
 {
     uint32_t ctx;
     uint32_t mask;
@@ -130,7 +128,7 @@ os_bsp_systick_init(uint32_t os_ticks_per_sec)
     NRF_RTC0->TASKS_CLEAR = 1;
 
     /* Set isr in vector table and enable interrupt */
-    NVIC_SetPriority(RTC0_IRQn, BSP_LOWEST_PRIO - 1);
+    NVIC_SetPriority(RTC0_IRQn, prio);
     NVIC_SetVector(RTC0_IRQn, (uint32_t)rtc0_timer_handler);
     NVIC_EnableIRQ(RTC0_IRQn);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b387dc65/hw/bsp/nrf52pdk/src/os_bsp.c
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf52pdk/src/os_bsp.c b/hw/bsp/nrf52pdk/src/os_bsp.c
index 92bd69c..0957c22 100644
--- a/hw/bsp/nrf52pdk/src/os_bsp.c
+++ b/hw/bsp/nrf52pdk/src/os_bsp.c
@@ -16,6 +16,11 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+#include <os/os.h>
+#include <bsp/cmsis_nvic.h>
+#include <mcu/nrf52.h>
+#include <mcu/nrf52_bitfields.h>
+#include <util/util.h>
 #include <hal/flash_map.h>
 
 static struct flash_area bsp_flash_areas[] = {
@@ -78,3 +83,48 @@ os_bsp_init(void)
     flash_area_init(bsp_flash_areas,
       sizeof(bsp_flash_areas) / sizeof(bsp_flash_areas[0]));
 }
+
+#define CALLOUT_TIMER       NRF_TIMER1
+#define CALLOUT_IRQ         TIMER1_IRQn
+#define CALLOUT_CMPREG      0
+#define CALLOUT_PRESCALER   4   /* prescaler to generate 1MHz timer freq */
+
+static void
+nrf52_timer_handler(void)
+{
+    /* Clear interrupt */
+    CALLOUT_TIMER->EVENTS_COMPARE[CALLOUT_CMPREG] = 0;
+
+    os_time_advance(1, true);
+}
+
+void
+os_bsp_systick_init(uint32_t os_ticks_per_sec, int prio)
+{
+    uint32_t usecs_per_os_tick;
+
+    usecs_per_os_tick = 1000000 / os_ticks_per_sec;
+
+    /*
+     * Program CALLOUT_TIMER to operate at 1MHz and trigger an output
+     * compare interrupt at a rate of 'os_ticks_per_sec'.
+     */
+    CALLOUT_TIMER->TASKS_STOP = 1;
+    CALLOUT_TIMER->TASKS_CLEAR = 1;
+    CALLOUT_TIMER->MODE = TIMER_MODE_MODE_Timer;
+    CALLOUT_TIMER->BITMODE = TIMER_BITMODE_BITMODE_32Bit;
+    CALLOUT_TIMER->PRESCALER = CALLOUT_PRESCALER;
+
+    CALLOUT_TIMER->CC[CALLOUT_CMPREG] = usecs_per_os_tick;
+    CALLOUT_TIMER->INTENSET = TIMER_COMPARE_INT_MASK(CALLOUT_CMPREG);
+    CALLOUT_TIMER->EVENTS_COMPARE[CALLOUT_CMPREG] = 0;
+
+    /* Enable shortcut to clear the timer on output compare */
+    CALLOUT_TIMER->SHORTS = TIMER_SHORTS_COMPARE_CLEAR(CALLOUT_CMPREG);
+
+    NVIC_SetPriority(CALLOUT_IRQ, prio);
+    NVIC_SetVector(CALLOUT_IRQ, (uint32_t)nrf52_timer_handler);
+    NVIC_EnableIRQ(CALLOUT_IRQ);
+
+    CALLOUT_TIMER->TASKS_START = 1;     /* start the callout timer */
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b387dc65/hw/mcu/nordic/nrf51xxx/include/mcu/nrf51_bitfields.h
----------------------------------------------------------------------
diff --git a/hw/mcu/nordic/nrf51xxx/include/mcu/nrf51_bitfields.h b/hw/mcu/nordic/nrf51xxx/include/mcu/nrf51_bitfields.h
index 5c877d9..bf7682b 100755
--- a/hw/mcu/nordic/nrf51xxx/include/mcu/nrf51_bitfields.h
+++ b/hw/mcu/nordic/nrf51xxx/include/mcu/nrf51_bitfields.h
@@ -6133,6 +6133,9 @@
 /* Register: TIMER_SHORTS */
 /* Description: Shortcuts for Timer. */
 
+#define TIMER_SHORTS_COMPARE_CLEAR(ccreg)   (1UL << (ccreg))
+#define TIMER_SHORTS_COMPARE_STOP(ccreg)    (1UL << ((ccreg) + 8))
+
 /* Bit 11 : Shortcut between CC[3] event and the STOP task. */
 #define TIMER_SHORTS_COMPARE3_STOP_Pos (11UL) /*!< Position of COMPARE3_STOP field. */
 #define TIMER_SHORTS_COMPARE3_STOP_Msk (0x1UL << TIMER_SHORTS_COMPARE3_STOP_Pos) /*!< Bit mask of COMPARE3_STOP field. */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b387dc65/hw/mcu/nordic/nrf52xxx/include/mcu/nrf52_bitfields.h
----------------------------------------------------------------------
diff --git a/hw/mcu/nordic/nrf52xxx/include/mcu/nrf52_bitfields.h b/hw/mcu/nordic/nrf52xxx/include/mcu/nrf52_bitfields.h
index a7c4db8..6d7de32 100755
--- a/hw/mcu/nordic/nrf52xxx/include/mcu/nrf52_bitfields.h
+++ b/hw/mcu/nordic/nrf52xxx/include/mcu/nrf52_bitfields.h
@@ -10347,6 +10347,9 @@
 /* Register: TIMER_SHORTS */
 /* Description: Shortcut register */
 
+#define TIMER_SHORTS_COMPARE_CLEAR(ccreg)   (1UL << (ccreg))
+#define TIMER_SHORTS_COMPARE_STOP(ccreg)    (1UL << ((ccreg) + 8))
+
 /* Bit 13 : Shortcut between EVENTS_COMPARE[5] event and TASKS_STOP task */
 #define TIMER_SHORTS_COMPARE5_STOP_Pos (13UL) /*!< Position of COMPARE5_STOP field. */
 #define TIMER_SHORTS_COMPARE5_STOP_Msk (0x1UL << TIMER_SHORTS_COMPARE5_STOP_Pos) /*!< Bit mask of COMPARE5_STOP field. */
@@ -10421,6 +10424,7 @@
 
 /* Register: TIMER_INTENSET */
 /* Description: Enable interrupt */
+#define TIMER_COMPARE_INT_MASK(ccreg)   (1UL << ((ccreg) + 16))
 
 /* Bit 21 : Write '1' to Enable interrupt on EVENTS_COMPARE[5] event */
 #define TIMER_INTENSET_COMPARE5_Pos (21UL) /*!< Position of COMPARE5 field. */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b387dc65/libs/os/include/os/arch/cortex_m0/os/os_arch.h
----------------------------------------------------------------------
diff --git a/libs/os/include/os/arch/cortex_m0/os/os_arch.h b/libs/os/include/os/arch/cortex_m0/os/os_arch.h
index 59b984d..23f6131 100755
--- a/libs/os/include/os/arch/cortex_m0/os/os_arch.h
+++ b/libs/os/include/os/arch/cortex_m0/os/os_arch.h
@@ -72,7 +72,7 @@ void os_default_irq_asm(void);
 void os_arch_idle(void);
 
 /* External function prototypes supplied by BSP */
-void os_bsp_systick_init(uint32_t os_ticks_per_sec);
+void os_bsp_systick_init(uint32_t os_ticks_per_sec, int prio);
 void os_bsp_init(void);
 void os_bsp_ctx_sw(void);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b387dc65/libs/os/include/os/arch/cortex_m4/os/os_arch.h
----------------------------------------------------------------------
diff --git a/libs/os/include/os/arch/cortex_m4/os/os_arch.h b/libs/os/include/os/arch/cortex_m4/os/os_arch.h
index 604a57b..ab0f95f 100755
--- a/libs/os/include/os/arch/cortex_m4/os/os_arch.h
+++ b/libs/os/include/os/arch/cortex_m4/os/os_arch.h
@@ -72,7 +72,7 @@ void os_default_irq_asm(void);
 void os_arch_idle(void);
 
 /* External function prototypes supplied by BSP */
-void os_bsp_systick_init(uint32_t os_tick_per_sec);
+void os_bsp_systick_init(uint32_t os_tick_per_sec, int prio);
 void os_bsp_init(void);
 void os_bsp_ctx_sw(void);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b387dc65/libs/os/src/arch/cortex_m0/os_arch_arm.c
----------------------------------------------------------------------
diff --git a/libs/os/src/arch/cortex_m0/os_arch_arm.c b/libs/os/src/arch/cortex_m0/os_arch_arm.c
index b70481a..224c045 100755
--- a/libs/os/src/arch/cortex_m0/os_arch_arm.c
+++ b/libs/os/src/arch/cortex_m0/os_arch_arm.c
@@ -256,7 +256,7 @@ os_arch_start(void)
     __set_PSP((uint32_t)t->t_stackptr + offsetof(struct stack_frame, r0));
 
     /* Intitialize and start system clock timer */
-    os_bsp_systick_init(OS_TICKS_PER_SEC);
+    os_bsp_systick_init(OS_TICKS_PER_SEC, SYSTICK_PRIO);
 
     /* Mark the OS as started, right before we run our first task */
     g_os_started = 1;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b387dc65/libs/os/src/arch/cortex_m4/os_arch_arm.c
----------------------------------------------------------------------
diff --git a/libs/os/src/arch/cortex_m4/os_arch_arm.c b/libs/os/src/arch/cortex_m4/os_arch_arm.c
index 3117af9..a819e43 100755
--- a/libs/os/src/arch/cortex_m4/os_arch_arm.c
+++ b/libs/os/src/arch/cortex_m4/os_arch_arm.c
@@ -233,29 +233,6 @@ os_arch_os_init(void)
     return err;
 }
 
-/**
- * os systick init
- *
- * Initializes systick for the MCU
- *
- * @param os_tick_usecs The number of microseconds in an os time tick
- */
-static void
-os_systick_init(uint32_t os_tick_usecs)
-{
-    uint32_t reload_val;
-
-    reload_val = (((uint64_t)SystemCoreClock * os_tick_usecs) / 1000000) - 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, SYSTICK_PRIO);
-}
-
 uint32_t
 os_arch_start(void)
 {
@@ -269,7 +246,7 @@ os_arch_start(void)
     __set_PSP((uint32_t)t->t_stackptr + offsetof(struct stack_frame, r0));
 
     /* Intitialize and start system clock timer */
-    os_systick_init(1000000 / OS_TICKS_PER_SEC);
+    os_bsp_systick_init(OS_TICKS_PER_SEC, SYSTICK_PRIO);
 
     /* Mark the OS as started, right before we run our first task */
     g_os_started = 1;