Greg Steiert / pegasus_dev

Dependents:   blinky_max32630fthr

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_hal_timer.cpp Source File

arm_hal_timer.cpp

00001 /*
00002  * Copyright (c) 2016 ARM Limited, All Rights Reserved
00003  */
00004 
00005 // Include before mbed.h to properly get UINT*_C()
00006 #include "ns_types.h"
00007 
00008 #include "cmsis_os.h"
00009 #include "mbed.h"
00010 
00011 #include "platform/arm_hal_timer.h"
00012 #include "platform/arm_hal_interrupt.h"
00013 
00014 static osThreadId timer_thread_id;
00015 
00016 static Timer timer;
00017 static Timeout timeout;
00018 static uint32_t due;
00019 static void (*arm_hal_callback)(void);
00020 
00021 static void timer_thread(const void *)
00022 {
00023     for (;;) {
00024         osSignalWait(1, osWaitForever);
00025         // !!! We don't do our own enter/exit critical - we rely on callback
00026         // doing it (ns_timer_interrupt_handler does)
00027         //platform_enter_critical();
00028         arm_hal_callback();
00029         //platform_exit_critical();
00030     }
00031 }
00032 
00033 // Called once at boot
00034 void platform_timer_enable(void)
00035 {
00036     static osThreadDef(timer_thread, osPriorityRealtime, /*1,*/ 2*1024);
00037     timer_thread_id = osThreadCreate(osThread(timer_thread), NULL);
00038     timer.start();
00039 }
00040 
00041 // Actually cancels a timer, not the opposite of enable
00042 void platform_timer_disable(void)
00043 {
00044     timeout.detach();
00045 }
00046 
00047 // Not called while running, fortunately
00048 void platform_timer_set_cb(void (*new_fp)(void))
00049 {
00050     arm_hal_callback = new_fp;
00051 }
00052 
00053 static void timer_callback(void)
00054 {
00055     due = 0;
00056     osSignalSet(timer_thread_id, 1);
00057     //callback();
00058 }
00059 
00060 // This is called from inside platform_enter_critical - IRQs can't happen
00061 void platform_timer_start(uint16_t slots)
00062 {
00063     timer.reset();
00064     due = slots * UINT32_C(50);
00065     timeout.attach_us(timer_callback, due);
00066 }
00067 
00068 // This is called from inside platform_enter_critical - IRQs can't happen
00069 uint16_t platform_timer_get_remaining_slots(void)
00070 {
00071     uint32_t elapsed = timer.read_us();
00072     if (elapsed < due) {
00073         return (uint16_t) ((due - elapsed) / 50);
00074     } else {
00075         return 0;
00076     }
00077 }
00078