Knight KE
/
Game_Master
游戏王对战板,目前code还是空的
mbed-os/targets/TARGET_STM/us_ticker.c@0:9b3d4731edbb, 2018-06-21 (annotated)
- Committer:
- WFKnight
- Date:
- Thu Jun 21 13:51:43 2018 +0000
- Revision:
- 0:9b3d4731edbb
UART, RTOS, LED
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WFKnight | 0:9b3d4731edbb | 1 | /* mbed Microcontroller Library |
WFKnight | 0:9b3d4731edbb | 2 | * Copyright (c) 2006-2016 ARM Limited |
WFKnight | 0:9b3d4731edbb | 3 | * |
WFKnight | 0:9b3d4731edbb | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
WFKnight | 0:9b3d4731edbb | 5 | * you may not use this file except in compliance with the License. |
WFKnight | 0:9b3d4731edbb | 6 | * You may obtain a copy of the License at |
WFKnight | 0:9b3d4731edbb | 7 | * |
WFKnight | 0:9b3d4731edbb | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
WFKnight | 0:9b3d4731edbb | 9 | * |
WFKnight | 0:9b3d4731edbb | 10 | * Unless required by applicable law or agreed to in writing, software |
WFKnight | 0:9b3d4731edbb | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
WFKnight | 0:9b3d4731edbb | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
WFKnight | 0:9b3d4731edbb | 13 | * See the License for the specific language governing permissions and |
WFKnight | 0:9b3d4731edbb | 14 | * limitations under the License. |
WFKnight | 0:9b3d4731edbb | 15 | */ |
WFKnight | 0:9b3d4731edbb | 16 | #include <stddef.h> |
WFKnight | 0:9b3d4731edbb | 17 | #include "us_ticker_api.h" |
WFKnight | 0:9b3d4731edbb | 18 | #include "PeripheralNames.h" |
WFKnight | 0:9b3d4731edbb | 19 | #include "hal_tick.h" |
WFKnight | 0:9b3d4731edbb | 20 | |
WFKnight | 0:9b3d4731edbb | 21 | #if TIM_MST_16BIT |
WFKnight | 0:9b3d4731edbb | 22 | #define TIMER_TICKER_BIT_WIDTH 16 |
WFKnight | 0:9b3d4731edbb | 23 | #else |
WFKnight | 0:9b3d4731edbb | 24 | #define TIMER_TICKER_BIT_WIDTH 32 |
WFKnight | 0:9b3d4731edbb | 25 | #endif |
WFKnight | 0:9b3d4731edbb | 26 | |
WFKnight | 0:9b3d4731edbb | 27 | TIM_HandleTypeDef TimMasterHandle; |
WFKnight | 0:9b3d4731edbb | 28 | |
WFKnight | 0:9b3d4731edbb | 29 | const ticker_info_t* us_ticker_get_info() |
WFKnight | 0:9b3d4731edbb | 30 | { |
WFKnight | 0:9b3d4731edbb | 31 | static const ticker_info_t info = { |
WFKnight | 0:9b3d4731edbb | 32 | 1000000, |
WFKnight | 0:9b3d4731edbb | 33 | TIMER_TICKER_BIT_WIDTH |
WFKnight | 0:9b3d4731edbb | 34 | }; |
WFKnight | 0:9b3d4731edbb | 35 | return &info; |
WFKnight | 0:9b3d4731edbb | 36 | } |
WFKnight | 0:9b3d4731edbb | 37 | |
WFKnight | 0:9b3d4731edbb | 38 | void us_ticker_init(void) |
WFKnight | 0:9b3d4731edbb | 39 | { |
WFKnight | 0:9b3d4731edbb | 40 | /* NOTE: assuming that HAL tick has already been initialized! */ |
WFKnight | 0:9b3d4731edbb | 41 | __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1); |
WFKnight | 0:9b3d4731edbb | 42 | } |
WFKnight | 0:9b3d4731edbb | 43 | |
WFKnight | 0:9b3d4731edbb | 44 | uint32_t us_ticker_read() |
WFKnight | 0:9b3d4731edbb | 45 | { |
WFKnight | 0:9b3d4731edbb | 46 | return TIM_MST->CNT; |
WFKnight | 0:9b3d4731edbb | 47 | } |
WFKnight | 0:9b3d4731edbb | 48 | |
WFKnight | 0:9b3d4731edbb | 49 | void us_ticker_set_interrupt(timestamp_t timestamp) |
WFKnight | 0:9b3d4731edbb | 50 | { |
WFKnight | 0:9b3d4731edbb | 51 | // NOTE: This function must be called with interrupts disabled to keep our |
WFKnight | 0:9b3d4731edbb | 52 | // timer interrupt setup atomic |
WFKnight | 0:9b3d4731edbb | 53 | // Set new output compare value |
WFKnight | 0:9b3d4731edbb | 54 | __HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_1, (uint32_t)timestamp); |
WFKnight | 0:9b3d4731edbb | 55 | // Ensure the compare event starts clear |
WFKnight | 0:9b3d4731edbb | 56 | __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1); |
WFKnight | 0:9b3d4731edbb | 57 | // Enable IT |
WFKnight | 0:9b3d4731edbb | 58 | __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1); |
WFKnight | 0:9b3d4731edbb | 59 | } |
WFKnight | 0:9b3d4731edbb | 60 | |
WFKnight | 0:9b3d4731edbb | 61 | void us_ticker_fire_interrupt(void) |
WFKnight | 0:9b3d4731edbb | 62 | { |
WFKnight | 0:9b3d4731edbb | 63 | __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1); |
WFKnight | 0:9b3d4731edbb | 64 | LL_TIM_GenerateEvent_CC1(TimMasterHandle.Instance); |
WFKnight | 0:9b3d4731edbb | 65 | // Enable IT |
WFKnight | 0:9b3d4731edbb | 66 | __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1); |
WFKnight | 0:9b3d4731edbb | 67 | } |
WFKnight | 0:9b3d4731edbb | 68 | |
WFKnight | 0:9b3d4731edbb | 69 | /* NOTE: must be called with interrupts disabled! */ |
WFKnight | 0:9b3d4731edbb | 70 | void us_ticker_disable_interrupt(void) |
WFKnight | 0:9b3d4731edbb | 71 | { |
WFKnight | 0:9b3d4731edbb | 72 | __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1); |
WFKnight | 0:9b3d4731edbb | 73 | } |
WFKnight | 0:9b3d4731edbb | 74 | |
WFKnight | 0:9b3d4731edbb | 75 | /* NOTE: must be called with interrupts disabled! */ |
WFKnight | 0:9b3d4731edbb | 76 | void us_ticker_clear_interrupt(void) |
WFKnight | 0:9b3d4731edbb | 77 | { |
WFKnight | 0:9b3d4731edbb | 78 | __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1); |
WFKnight | 0:9b3d4731edbb | 79 | } |
WFKnight | 0:9b3d4731edbb | 80 |