![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/us_ticker.c@0:8fdf9a60065b, 2018-10-10 (annotated)
- Committer:
- kadonotakashi
- Date:
- Wed Oct 10 00:33:53 2018 +0000
- Revision:
- 0:8fdf9a60065b
how to make mbed librry
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | /* mbed Microcontroller Library |
kadonotakashi | 0:8fdf9a60065b | 2 | * Copyright (c) 2006-2018 ARM Limited |
kadonotakashi | 0:8fdf9a60065b | 3 | * |
kadonotakashi | 0:8fdf9a60065b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kadonotakashi | 0:8fdf9a60065b | 5 | * you may not use this file except in compliance with the License. |
kadonotakashi | 0:8fdf9a60065b | 6 | * You may obtain a copy of the License at |
kadonotakashi | 0:8fdf9a60065b | 7 | * |
kadonotakashi | 0:8fdf9a60065b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kadonotakashi | 0:8fdf9a60065b | 9 | * |
kadonotakashi | 0:8fdf9a60065b | 10 | * Unless required by applicable law or agreed to in writing, software |
kadonotakashi | 0:8fdf9a60065b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kadonotakashi | 0:8fdf9a60065b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kadonotakashi | 0:8fdf9a60065b | 13 | * See the License for the specific language governing permissions and |
kadonotakashi | 0:8fdf9a60065b | 14 | * limitations under the License. |
kadonotakashi | 0:8fdf9a60065b | 15 | */ |
kadonotakashi | 0:8fdf9a60065b | 16 | #include <stddef.h> |
kadonotakashi | 0:8fdf9a60065b | 17 | #include "us_ticker_api.h" |
kadonotakashi | 0:8fdf9a60065b | 18 | #include "PeripheralNames.h" |
kadonotakashi | 0:8fdf9a60065b | 19 | #include "fsl_pit.h" |
kadonotakashi | 0:8fdf9a60065b | 20 | #include "fsl_clock_config.h" |
kadonotakashi | 0:8fdf9a60065b | 21 | |
kadonotakashi | 0:8fdf9a60065b | 22 | const ticker_info_t* us_ticker_get_info() |
kadonotakashi | 0:8fdf9a60065b | 23 | { |
kadonotakashi | 0:8fdf9a60065b | 24 | static const ticker_info_t info = { |
kadonotakashi | 0:8fdf9a60065b | 25 | 1000000, // 1 MHz |
kadonotakashi | 0:8fdf9a60065b | 26 | 32 // 32 bit counter |
kadonotakashi | 0:8fdf9a60065b | 27 | }; |
kadonotakashi | 0:8fdf9a60065b | 28 | return &info; |
kadonotakashi | 0:8fdf9a60065b | 29 | } |
kadonotakashi | 0:8fdf9a60065b | 30 | |
kadonotakashi | 0:8fdf9a60065b | 31 | static bool us_ticker_inited = false; |
kadonotakashi | 0:8fdf9a60065b | 32 | |
kadonotakashi | 0:8fdf9a60065b | 33 | static void pit_isr(void) |
kadonotakashi | 0:8fdf9a60065b | 34 | { |
kadonotakashi | 0:8fdf9a60065b | 35 | PIT_ClearStatusFlags(PIT, kPIT_Chnl_3, PIT_TFLG_TIF_MASK); |
kadonotakashi | 0:8fdf9a60065b | 36 | PIT_ClearStatusFlags(PIT, kPIT_Chnl_2, PIT_TFLG_TIF_MASK); |
kadonotakashi | 0:8fdf9a60065b | 37 | PIT_StopTimer(PIT, kPIT_Chnl_2); |
kadonotakashi | 0:8fdf9a60065b | 38 | PIT_StopTimer(PIT, kPIT_Chnl_3); |
kadonotakashi | 0:8fdf9a60065b | 39 | |
kadonotakashi | 0:8fdf9a60065b | 40 | us_ticker_irq_handler(); |
kadonotakashi | 0:8fdf9a60065b | 41 | } |
kadonotakashi | 0:8fdf9a60065b | 42 | |
kadonotakashi | 0:8fdf9a60065b | 43 | /** Initialize the high frequency ticker |
kadonotakashi | 0:8fdf9a60065b | 44 | * |
kadonotakashi | 0:8fdf9a60065b | 45 | */ |
kadonotakashi | 0:8fdf9a60065b | 46 | void us_ticker_init(void) |
kadonotakashi | 0:8fdf9a60065b | 47 | { |
kadonotakashi | 0:8fdf9a60065b | 48 | /* Common for ticker/timer. */ |
kadonotakashi | 0:8fdf9a60065b | 49 | uint32_t busClock; |
kadonotakashi | 0:8fdf9a60065b | 50 | /* Structure to initialize PIT. */ |
kadonotakashi | 0:8fdf9a60065b | 51 | pit_config_t pitConfig; |
kadonotakashi | 0:8fdf9a60065b | 52 | |
kadonotakashi | 0:8fdf9a60065b | 53 | PIT_GetDefaultConfig(&pitConfig); |
kadonotakashi | 0:8fdf9a60065b | 54 | PIT_Init(PIT, &pitConfig); |
kadonotakashi | 0:8fdf9a60065b | 55 | |
kadonotakashi | 0:8fdf9a60065b | 56 | busClock = CLOCK_GetFreq(kCLOCK_BusClk); |
kadonotakashi | 0:8fdf9a60065b | 57 | |
kadonotakashi | 0:8fdf9a60065b | 58 | /* Let the timer to count if re-init. */ |
kadonotakashi | 0:8fdf9a60065b | 59 | if (!us_ticker_inited) { |
kadonotakashi | 0:8fdf9a60065b | 60 | |
kadonotakashi | 0:8fdf9a60065b | 61 | PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, busClock / 1000000 - 1); |
kadonotakashi | 0:8fdf9a60065b | 62 | PIT_SetTimerPeriod(PIT, kPIT_Chnl_1, 0xFFFFFFFF); |
kadonotakashi | 0:8fdf9a60065b | 63 | PIT_SetTimerChainMode(PIT, kPIT_Chnl_1, true); |
kadonotakashi | 0:8fdf9a60065b | 64 | PIT_StartTimer(PIT, kPIT_Chnl_0); |
kadonotakashi | 0:8fdf9a60065b | 65 | PIT_StartTimer(PIT, kPIT_Chnl_1); |
kadonotakashi | 0:8fdf9a60065b | 66 | } |
kadonotakashi | 0:8fdf9a60065b | 67 | |
kadonotakashi | 0:8fdf9a60065b | 68 | /* Configure interrupt generation counters and disable ticker interrupts. */ |
kadonotakashi | 0:8fdf9a60065b | 69 | PIT_StopTimer(PIT, kPIT_Chnl_3); |
kadonotakashi | 0:8fdf9a60065b | 70 | PIT_StopTimer(PIT, kPIT_Chnl_2); |
kadonotakashi | 0:8fdf9a60065b | 71 | PIT_SetTimerPeriod(PIT, kPIT_Chnl_2, busClock / 1000000 - 1); |
kadonotakashi | 0:8fdf9a60065b | 72 | PIT_SetTimerChainMode(PIT, kPIT_Chnl_3, true); |
kadonotakashi | 0:8fdf9a60065b | 73 | PIT_ClearStatusFlags(PIT, kPIT_Chnl_3, PIT_TFLG_TIF_MASK); |
kadonotakashi | 0:8fdf9a60065b | 74 | NVIC_SetVector(PIT3_IRQn, (uint32_t) pit_isr); |
kadonotakashi | 0:8fdf9a60065b | 75 | NVIC_EnableIRQ(PIT3_IRQn); |
kadonotakashi | 0:8fdf9a60065b | 76 | PIT_DisableInterrupts(PIT, kPIT_Chnl_3, kPIT_TimerInterruptEnable); |
kadonotakashi | 0:8fdf9a60065b | 77 | |
kadonotakashi | 0:8fdf9a60065b | 78 | us_ticker_inited = true; |
kadonotakashi | 0:8fdf9a60065b | 79 | } |
kadonotakashi | 0:8fdf9a60065b | 80 | |
kadonotakashi | 0:8fdf9a60065b | 81 | /** Read the current counter |
kadonotakashi | 0:8fdf9a60065b | 82 | * |
kadonotakashi | 0:8fdf9a60065b | 83 | * @return The current timer's counter value in ticks |
kadonotakashi | 0:8fdf9a60065b | 84 | */ |
kadonotakashi | 0:8fdf9a60065b | 85 | uint32_t us_ticker_read() |
kadonotakashi | 0:8fdf9a60065b | 86 | { |
kadonotakashi | 0:8fdf9a60065b | 87 | return ~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1)); |
kadonotakashi | 0:8fdf9a60065b | 88 | } |
kadonotakashi | 0:8fdf9a60065b | 89 | |
kadonotakashi | 0:8fdf9a60065b | 90 | /** Disable us ticker interrupt |
kadonotakashi | 0:8fdf9a60065b | 91 | * |
kadonotakashi | 0:8fdf9a60065b | 92 | */ |
kadonotakashi | 0:8fdf9a60065b | 93 | void us_ticker_disable_interrupt(void) |
kadonotakashi | 0:8fdf9a60065b | 94 | { |
kadonotakashi | 0:8fdf9a60065b | 95 | PIT_DisableInterrupts(PIT, kPIT_Chnl_3, kPIT_TimerInterruptEnable); |
kadonotakashi | 0:8fdf9a60065b | 96 | } |
kadonotakashi | 0:8fdf9a60065b | 97 | |
kadonotakashi | 0:8fdf9a60065b | 98 | /** Clear us ticker interrupt |
kadonotakashi | 0:8fdf9a60065b | 99 | * |
kadonotakashi | 0:8fdf9a60065b | 100 | */ |
kadonotakashi | 0:8fdf9a60065b | 101 | void us_ticker_clear_interrupt(void) |
kadonotakashi | 0:8fdf9a60065b | 102 | { |
kadonotakashi | 0:8fdf9a60065b | 103 | PIT_ClearStatusFlags(PIT, kPIT_Chnl_3, PIT_TFLG_TIF_MASK); |
kadonotakashi | 0:8fdf9a60065b | 104 | } |
kadonotakashi | 0:8fdf9a60065b | 105 | |
kadonotakashi | 0:8fdf9a60065b | 106 | /** Set interrupt for specified timestamp |
kadonotakashi | 0:8fdf9a60065b | 107 | * |
kadonotakashi | 0:8fdf9a60065b | 108 | * @param timestamp The time in ticks when interrupt should be generated |
kadonotakashi | 0:8fdf9a60065b | 109 | */ |
kadonotakashi | 0:8fdf9a60065b | 110 | void us_ticker_set_interrupt(timestamp_t timestamp) |
kadonotakashi | 0:8fdf9a60065b | 111 | { |
kadonotakashi | 0:8fdf9a60065b | 112 | /* We get here absolute interrupt time which takes into account counter overflow. |
kadonotakashi | 0:8fdf9a60065b | 113 | * Since we use additional count-down timer to generate interrupt we need to calculate |
kadonotakashi | 0:8fdf9a60065b | 114 | * load value based on time-stamp. |
kadonotakashi | 0:8fdf9a60065b | 115 | */ |
kadonotakashi | 0:8fdf9a60065b | 116 | const uint32_t now_ticks = us_ticker_read(); |
kadonotakashi | 0:8fdf9a60065b | 117 | uint32_t delta_ticks = |
kadonotakashi | 0:8fdf9a60065b | 118 | timestamp >= now_ticks ? timestamp - now_ticks : (uint32_t)((uint64_t) timestamp + 0xFFFFFFFF - now_ticks); |
kadonotakashi | 0:8fdf9a60065b | 119 | |
kadonotakashi | 0:8fdf9a60065b | 120 | if (delta_ticks == 0) { |
kadonotakashi | 0:8fdf9a60065b | 121 | /* The requested delay is less than the minimum resolution of this counter. */ |
kadonotakashi | 0:8fdf9a60065b | 122 | delta_ticks = 1; |
kadonotakashi | 0:8fdf9a60065b | 123 | } |
kadonotakashi | 0:8fdf9a60065b | 124 | |
kadonotakashi | 0:8fdf9a60065b | 125 | PIT_StopTimer(PIT, kPIT_Chnl_3); |
kadonotakashi | 0:8fdf9a60065b | 126 | PIT_StopTimer(PIT, kPIT_Chnl_2); |
kadonotakashi | 0:8fdf9a60065b | 127 | PIT_SetTimerPeriod(PIT, kPIT_Chnl_3, delta_ticks); |
kadonotakashi | 0:8fdf9a60065b | 128 | PIT_EnableInterrupts(PIT, kPIT_Chnl_3, kPIT_TimerInterruptEnable); |
kadonotakashi | 0:8fdf9a60065b | 129 | PIT_StartTimer(PIT, kPIT_Chnl_3); |
kadonotakashi | 0:8fdf9a60065b | 130 | PIT_StartTimer(PIT, kPIT_Chnl_2); |
kadonotakashi | 0:8fdf9a60065b | 131 | } |
kadonotakashi | 0:8fdf9a60065b | 132 | |
kadonotakashi | 0:8fdf9a60065b | 133 | /** Fire us ticker interrupt |
kadonotakashi | 0:8fdf9a60065b | 134 | * |
kadonotakashi | 0:8fdf9a60065b | 135 | */ |
kadonotakashi | 0:8fdf9a60065b | 136 | void us_ticker_fire_interrupt(void) |
kadonotakashi | 0:8fdf9a60065b | 137 | { |
kadonotakashi | 0:8fdf9a60065b | 138 | NVIC_SetPendingIRQ(PIT3_IRQn); |
kadonotakashi | 0:8fdf9a60065b | 139 | } |
kadonotakashi | 0:8fdf9a60065b | 140 | |
kadonotakashi | 0:8fdf9a60065b | 141 | void us_ticker_free(void) |
kadonotakashi | 0:8fdf9a60065b | 142 | { |
kadonotakashi | 0:8fdf9a60065b | 143 | PIT_StartTimer(PIT, kPIT_Chnl_3); |
kadonotakashi | 0:8fdf9a60065b | 144 | PIT_StartTimer(PIT, kPIT_Chnl_2); |
kadonotakashi | 0:8fdf9a60065b | 145 | PIT_StartTimer(PIT, kPIT_Chnl_1); |
kadonotakashi | 0:8fdf9a60065b | 146 | PIT_StartTimer(PIT, kPIT_Chnl_0); |
kadonotakashi | 0:8fdf9a60065b | 147 | |
kadonotakashi | 0:8fdf9a60065b | 148 | PIT_DisableInterrupts(PIT, kPIT_Chnl_3, kPIT_TimerInterruptEnable); |
kadonotakashi | 0:8fdf9a60065b | 149 | NVIC_DisableIRQ(PIT3_IRQn); |
kadonotakashi | 0:8fdf9a60065b | 150 | |
kadonotakashi | 0:8fdf9a60065b | 151 | us_ticker_inited = false; |
kadonotakashi | 0:8fdf9a60065b | 152 | } |