2014-01-10 16:46:18 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
* Copyright (c) 2014, STMicroelectronics
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "us_ticker_api.h"
|
|
|
|
#include "PeripheralNames.h"
|
2016-10-25 13:33:40 +00:00
|
|
|
#include "hal_tick.h"
|
2014-01-10 16:46:18 +00:00
|
|
|
|
2016-10-25 16:23:09 +00:00
|
|
|
// A 32-bit timer is used
|
2016-10-25 13:33:40 +00:00
|
|
|
#if !TIM_MST_16BIT
|
2014-01-13 13:20:27 +00:00
|
|
|
|
2014-10-16 06:48:34 +00:00
|
|
|
static TIM_HandleTypeDef TimMasterHandle;
|
2014-02-19 15:29:09 +00:00
|
|
|
static int us_ticker_inited = 0;
|
2014-01-10 16:46:18 +00:00
|
|
|
|
2016-10-25 13:33:40 +00:00
|
|
|
void us_ticker_init(void) {
|
2014-01-10 16:46:18 +00:00
|
|
|
if (us_ticker_inited) return;
|
|
|
|
us_ticker_inited = 1;
|
2014-04-29 08:12:59 +00:00
|
|
|
|
2014-10-16 06:48:34 +00:00
|
|
|
TimMasterHandle.Instance = TIM_MST;
|
2014-04-29 08:12:59 +00:00
|
|
|
|
2014-10-16 06:48:34 +00:00
|
|
|
HAL_InitTick(0); // The passed value is not used
|
2014-01-10 16:46:18 +00:00
|
|
|
}
|
|
|
|
|
2016-10-25 13:33:40 +00:00
|
|
|
uint32_t us_ticker_read() {
|
2014-01-10 16:46:18 +00:00
|
|
|
if (!us_ticker_inited) us_ticker_init();
|
2014-02-19 15:29:09 +00:00
|
|
|
return TIM_MST->CNT;
|
2014-01-10 16:46:18 +00:00
|
|
|
}
|
|
|
|
|
2016-10-25 13:33:40 +00:00
|
|
|
void us_ticker_set_interrupt(timestamp_t timestamp) {
|
2016-10-27 14:47:32 +00:00
|
|
|
TimMasterHandle.Instance = TIM_MST;
|
2014-02-19 15:29:09 +00:00
|
|
|
// Set new output compare value
|
2016-10-25 13:33:40 +00:00
|
|
|
__HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_1, (uint32_t)timestamp);
|
2014-02-19 15:29:09 +00:00
|
|
|
// Enable IT
|
2014-10-16 06:48:34 +00:00
|
|
|
__HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
|
2014-01-10 16:46:18 +00:00
|
|
|
}
|
|
|
|
|
2016-10-25 13:33:40 +00:00
|
|
|
void us_ticker_disable_interrupt(void) {
|
2016-10-27 14:47:32 +00:00
|
|
|
TimMasterHandle.Instance = TIM_MST;
|
2014-10-16 06:48:34 +00:00
|
|
|
__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
|
2014-01-10 16:46:18 +00:00
|
|
|
}
|
|
|
|
|
2016-10-25 13:33:40 +00:00
|
|
|
void us_ticker_clear_interrupt(void) {
|
2016-10-27 14:47:32 +00:00
|
|
|
TimMasterHandle.Instance = TIM_MST;
|
2014-10-16 06:48:34 +00:00
|
|
|
__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
|
2014-01-10 16:46:18 +00:00
|
|
|
}
|
2016-10-25 13:33:40 +00:00
|
|
|
|
|
|
|
#endif // !TIM_MST_16BIT
|