Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_hal_timer.cpp Source File

arm_hal_timer.cpp

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2017 ARM Ltd.
00003 //
00004 // SPDX-License-Identifier: Apache-2.0
00005 //
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 //
00010 //     http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 // ----------------------------------------------------------------------------
00018 
00019 // Include before mbed.h to properly get UINT*_C()
00020 #include "ns_types.h"
00021 
00022 #include "pal.h"
00023 #include "pal_rtos.h"
00024 
00025 #include "platform/arm_hal_timer.h"
00026 #include "platform/arm_hal_interrupt.h"
00027 
00028 #include <assert.h>
00029 
00030 // Low precision platform tick timer variables
00031 static void (*tick_timer_callback)(void);
00032 static palTimerID_t tick_timer_id;
00033 #define TICK_TIMER_ID   1
00034 
00035 void timer_callback(void const *funcArgument)
00036 {
00037     (void)funcArgument;
00038     if (tick_timer_callback != NULL) {
00039         tick_timer_callback();
00040     }
00041 }
00042 
00043 #ifdef MBED_CONF_NANOSTACK_EVENTLOOP_EXCLUDE_HIGHRES_TIMER
00044 extern "C" int8_t ns_timer_sleep(void);
00045 #endif
00046 
00047 // static method for creating the timer, called implicitly by platform_tick_timer_register if
00048 // timer was not enabled already
00049 static void tick_timer_create(void)
00050 {
00051     palStatus_t status;
00052     status = pal_init();
00053     assert(PAL_SUCCESS == status);
00054     status = pal_osTimerCreate(timer_callback, NULL, palOsTimerPeriodic , &tick_timer_id);
00055     assert(PAL_SUCCESS == status);
00056     
00057 }
00058 
00059 // Low precision platform tick timer
00060 extern "C"
00061 int8_t platform_tick_timer_register(void (*tick_timer_cb_handler)(void))
00062 {
00063     if (tick_timer_id == 0) {
00064         tick_timer_create();
00065     }
00066     tick_timer_callback = tick_timer_cb_handler;
00067     return TICK_TIMER_ID;
00068 }
00069 
00070 extern "C"
00071 int8_t platform_tick_timer_start(uint32_t period_ms)
00072 {
00073     int8_t retval = -1;
00074     if ((tick_timer_id != 0) && (PAL_SUCCESS == pal_osTimerStart(tick_timer_id, period_ms))) {
00075         retval = 0;
00076     }
00077     return retval;
00078 }
00079 
00080 extern "C"
00081 int8_t platform_tick_timer_stop(void)
00082 {
00083     int8_t retval = -1;
00084     if ((tick_timer_id != 0) && (PAL_SUCCESS == pal_osTimerStop(tick_timer_id))) {
00085         retval = 0;
00086     }
00087 
00088     // release PAL side resources
00089     pal_osTimerDelete(&tick_timer_id);
00090     pal_destroy();
00091 
00092     return retval;
00093 }
00094 
00095