mbed-os
Fork of mbed-os by
targets/TARGET_STM/TARGET_STM32F1/us_ticker.c@1:3deb71413561, 2017-07-20 (annotated)
- Committer:
- xuaner
- Date:
- Thu Jul 20 14:26:57 2017 +0000
- Revision:
- 1:3deb71413561
- Parent:
- 0:f269e3021894
mbed_os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed Microcontroller Library |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2014, STMicroelectronics |
elessair | 0:f269e3021894 | 3 | * All rights reserved. |
elessair | 0:f269e3021894 | 4 | * |
elessair | 0:f269e3021894 | 5 | * Redistribution and use in source and binary forms, with or without |
elessair | 0:f269e3021894 | 6 | * modification, are permitted provided that the following conditions are met: |
elessair | 0:f269e3021894 | 7 | * |
elessair | 0:f269e3021894 | 8 | * 1. Redistributions of source code must retain the above copyright notice, |
elessair | 0:f269e3021894 | 9 | * this list of conditions and the following disclaimer. |
elessair | 0:f269e3021894 | 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
elessair | 0:f269e3021894 | 11 | * this list of conditions and the following disclaimer in the documentation |
elessair | 0:f269e3021894 | 12 | * and/or other materials provided with the distribution. |
elessair | 0:f269e3021894 | 13 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
elessair | 0:f269e3021894 | 14 | * may be used to endorse or promote products derived from this software |
elessair | 0:f269e3021894 | 15 | * without specific prior written permission. |
elessair | 0:f269e3021894 | 16 | * |
elessair | 0:f269e3021894 | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
elessair | 0:f269e3021894 | 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
elessair | 0:f269e3021894 | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
elessair | 0:f269e3021894 | 20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
elessair | 0:f269e3021894 | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
elessair | 0:f269e3021894 | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
elessair | 0:f269e3021894 | 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
elessair | 0:f269e3021894 | 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
elessair | 0:f269e3021894 | 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
elessair | 0:f269e3021894 | 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
elessair | 0:f269e3021894 | 27 | */ |
elessair | 0:f269e3021894 | 28 | #include <stddef.h> |
elessair | 0:f269e3021894 | 29 | #include "us_ticker_api.h" |
elessair | 0:f269e3021894 | 30 | #include "PeripheralNames.h" |
elessair | 0:f269e3021894 | 31 | |
elessair | 0:f269e3021894 | 32 | // Timer selection |
elessair | 0:f269e3021894 | 33 | #define TIM_MST TIM4 |
elessair | 0:f269e3021894 | 34 | |
elessair | 0:f269e3021894 | 35 | static TIM_HandleTypeDef TimMasterHandle; |
elessair | 0:f269e3021894 | 36 | static int us_ticker_inited = 0; |
elessair | 0:f269e3021894 | 37 | |
elessair | 0:f269e3021894 | 38 | volatile uint32_t SlaveCounter = 0; |
elessair | 0:f269e3021894 | 39 | volatile uint32_t oc_int_part = 0; |
elessair | 0:f269e3021894 | 40 | volatile uint16_t oc_rem_part = 0; |
elessair | 0:f269e3021894 | 41 | volatile uint16_t cnt_val = 0; |
elessair | 0:f269e3021894 | 42 | |
elessair | 0:f269e3021894 | 43 | void set_compare(uint16_t count) |
elessair | 0:f269e3021894 | 44 | { |
elessair | 0:f269e3021894 | 45 | TimMasterHandle.Instance = TIM_MST; |
elessair | 0:f269e3021894 | 46 | // Set new output compare value |
elessair | 0:f269e3021894 | 47 | __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, count); |
elessair | 0:f269e3021894 | 48 | // Enable IT |
elessair | 0:f269e3021894 | 49 | __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1); |
elessair | 0:f269e3021894 | 50 | } |
elessair | 0:f269e3021894 | 51 | |
elessair | 0:f269e3021894 | 52 | void us_ticker_init(void) |
elessair | 0:f269e3021894 | 53 | { |
elessair | 0:f269e3021894 | 54 | if (us_ticker_inited) return; |
elessair | 0:f269e3021894 | 55 | us_ticker_inited = 1; |
elessair | 0:f269e3021894 | 56 | |
elessair | 0:f269e3021894 | 57 | HAL_InitTick(0); // The passed value is not used |
elessair | 0:f269e3021894 | 58 | } |
elessair | 0:f269e3021894 | 59 | |
elessair | 0:f269e3021894 | 60 | uint32_t us_ticker_read() |
elessair | 0:f269e3021894 | 61 | { |
elessair | 0:f269e3021894 | 62 | uint32_t counter; |
elessair | 0:f269e3021894 | 63 | if (!us_ticker_inited) us_ticker_init(); |
elessair | 0:f269e3021894 | 64 | |
elessair | 0:f269e3021894 | 65 | //Current value of TIM_MST->CNT is stored in cnt_val and is |
elessair | 0:f269e3021894 | 66 | //updated in interrupt context |
elessair | 0:f269e3021894 | 67 | counter = (uint32_t)(SlaveCounter << 16); |
elessair | 0:f269e3021894 | 68 | counter += cnt_val; |
elessair | 0:f269e3021894 | 69 | |
elessair | 0:f269e3021894 | 70 | return counter; |
elessair | 0:f269e3021894 | 71 | } |
elessair | 0:f269e3021894 | 72 | |
elessair | 0:f269e3021894 | 73 | void us_ticker_set_interrupt(timestamp_t timestamp) |
elessair | 0:f269e3021894 | 74 | { |
elessair | 0:f269e3021894 | 75 | int delta = (int)((uint32_t)timestamp - us_ticker_read()); |
elessair | 0:f269e3021894 | 76 | uint16_t cval = TIM_MST->CNT; |
elessair | 0:f269e3021894 | 77 | |
elessair | 0:f269e3021894 | 78 | if (delta <= 0) { // This event was in the past |
elessair | 0:f269e3021894 | 79 | us_ticker_irq_handler(); |
elessair | 0:f269e3021894 | 80 | } else { |
elessair | 0:f269e3021894 | 81 | oc_int_part = (uint32_t)(delta >> 16); |
elessair | 0:f269e3021894 | 82 | oc_rem_part = (uint16_t)(delta & 0xFFFF); |
elessair | 0:f269e3021894 | 83 | if (oc_rem_part <= (0xFFFF - cval)) { |
elessair | 0:f269e3021894 | 84 | set_compare(cval + oc_rem_part); |
elessair | 0:f269e3021894 | 85 | oc_rem_part = 0; |
elessair | 0:f269e3021894 | 86 | } else { |
elessair | 0:f269e3021894 | 87 | set_compare(0xFFFF); |
elessair | 0:f269e3021894 | 88 | oc_rem_part = oc_rem_part - (0xFFFF - cval); |
elessair | 0:f269e3021894 | 89 | } |
elessair | 0:f269e3021894 | 90 | } |
elessair | 0:f269e3021894 | 91 | } |
elessair | 0:f269e3021894 | 92 | |
elessair | 0:f269e3021894 | 93 | void us_ticker_disable_interrupt(void) |
elessair | 0:f269e3021894 | 94 | { |
elessair | 0:f269e3021894 | 95 | TimMasterHandle.Instance = TIM_MST; |
elessair | 0:f269e3021894 | 96 | __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1); |
elessair | 0:f269e3021894 | 97 | } |
elessair | 0:f269e3021894 | 98 | |
elessair | 0:f269e3021894 | 99 | void us_ticker_clear_interrupt(void) |
elessair | 0:f269e3021894 | 100 | { |
elessair | 0:f269e3021894 | 101 | TimMasterHandle.Instance = TIM_MST; |
elessair | 0:f269e3021894 | 102 | if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) { |
elessair | 0:f269e3021894 | 103 | __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1); |
elessair | 0:f269e3021894 | 104 | } |
elessair | 0:f269e3021894 | 105 | } |