helpfor studient
Dependents: STM32_F103-C8T6basecanblink_led
Fork of mbed-dev by
targets/TARGET_TOSHIBA/TARGET_TMPM46B/us_ticker.c@186:9c2029bfadbe, 2018-04-20 (annotated)
- Committer:
- Anna Bridge
- Date:
- Fri Apr 20 11:31:35 2018 +0100
- Revision:
- 186:9c2029bfadbe
- Parent:
- 185:08ed48f1de7f
Update to latest version of mbed lib
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 185:08ed48f1de7f | 1 | /* mbed Microcontroller Library |
AnnaBridge | 185:08ed48f1de7f | 2 | * (C)Copyright TOSHIBA ELECTRONIC DEVICES & STORAGE CORPORATION 2017 All rights reserved |
AnnaBridge | 185:08ed48f1de7f | 3 | * |
AnnaBridge | 185:08ed48f1de7f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AnnaBridge | 185:08ed48f1de7f | 5 | * you may not use this file except in compliance with the License. |
AnnaBridge | 185:08ed48f1de7f | 6 | * You may obtain a copy of the License at |
AnnaBridge | 185:08ed48f1de7f | 7 | * |
AnnaBridge | 185:08ed48f1de7f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AnnaBridge | 185:08ed48f1de7f | 9 | * |
AnnaBridge | 185:08ed48f1de7f | 10 | * Unless required by applicable law or agreed to in writing, software |
AnnaBridge | 185:08ed48f1de7f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AnnaBridge | 185:08ed48f1de7f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AnnaBridge | 185:08ed48f1de7f | 13 | * See the License for the specific language governing permissions and |
AnnaBridge | 185:08ed48f1de7f | 14 | * limitations under the License. |
AnnaBridge | 185:08ed48f1de7f | 15 | */ |
AnnaBridge | 185:08ed48f1de7f | 16 | #include "us_ticker_api.h" |
AnnaBridge | 185:08ed48f1de7f | 17 | #include "tmpm46b_tmrb.h" |
AnnaBridge | 185:08ed48f1de7f | 18 | |
AnnaBridge | 185:08ed48f1de7f | 19 | #define TMR16A_100US 0xFFFF |
AnnaBridge | 185:08ed48f1de7f | 20 | #define TMRB_CLK_DIV 0x3 |
AnnaBridge | 185:08ed48f1de7f | 21 | |
AnnaBridge | 185:08ed48f1de7f | 22 | static uint8_t us_ticker_inited = 0; // Is ticker initialized yet? |
AnnaBridge | 185:08ed48f1de7f | 23 | static volatile uint32_t us_ticker = 0; // timer counter |
AnnaBridge | 185:08ed48f1de7f | 24 | |
AnnaBridge | 185:08ed48f1de7f | 25 | const ticker_info_t* us_ticker_get_info() |
AnnaBridge | 185:08ed48f1de7f | 26 | { |
AnnaBridge | 185:08ed48f1de7f | 27 | static const ticker_info_t info = { |
AnnaBridge | 185:08ed48f1de7f | 28 | 1875000, // 1875000, |
AnnaBridge | 185:08ed48f1de7f | 29 | 16 // 16 bit counter |
AnnaBridge | 185:08ed48f1de7f | 30 | }; |
AnnaBridge | 185:08ed48f1de7f | 31 | return &info; |
AnnaBridge | 185:08ed48f1de7f | 32 | } |
AnnaBridge | 185:08ed48f1de7f | 33 | |
AnnaBridge | 185:08ed48f1de7f | 34 | // initialize us_ticker |
AnnaBridge | 185:08ed48f1de7f | 35 | void us_ticker_init(void) |
AnnaBridge | 185:08ed48f1de7f | 36 | { |
AnnaBridge | 185:08ed48f1de7f | 37 | TMRB_InitTypeDef m_tmrb0; |
AnnaBridge | 185:08ed48f1de7f | 38 | TMRB_FFOutputTypeDef FFStruct; |
AnnaBridge | 185:08ed48f1de7f | 39 | |
AnnaBridge | 185:08ed48f1de7f | 40 | if (us_ticker_inited) { |
AnnaBridge | 185:08ed48f1de7f | 41 | return; |
AnnaBridge | 185:08ed48f1de7f | 42 | } |
AnnaBridge | 185:08ed48f1de7f | 43 | us_ticker_inited = 1; |
AnnaBridge | 185:08ed48f1de7f | 44 | |
AnnaBridge | 185:08ed48f1de7f | 45 | // Enable channel 0 |
AnnaBridge | 185:08ed48f1de7f | 46 | TMRB_Enable(TSB_TB0); |
AnnaBridge | 185:08ed48f1de7f | 47 | // Stops and clear count operation |
AnnaBridge | 185:08ed48f1de7f | 48 | TMRB_SetRunState(TSB_TB0, TMRB_STOP); |
AnnaBridge | 185:08ed48f1de7f | 49 | // Disable to TBxFF0 reverse trigger |
AnnaBridge | 185:08ed48f1de7f | 50 | FFStruct.FlipflopCtrl = TMRB_FLIPFLOP_CLEAR; |
AnnaBridge | 185:08ed48f1de7f | 51 | FFStruct.FlipflopReverseTrg =TMRB_DISABLE_FLIPFLOP; |
AnnaBridge | 185:08ed48f1de7f | 52 | TMRB_SetFlipFlop(TSB_TB0, &FFStruct); |
AnnaBridge | 185:08ed48f1de7f | 53 | |
AnnaBridge | 185:08ed48f1de7f | 54 | // TSB_TB0 using free-run |
AnnaBridge | 185:08ed48f1de7f | 55 | m_tmrb0.Mode = TMRB_INTERVAL_TIMER; |
AnnaBridge | 185:08ed48f1de7f | 56 | m_tmrb0.ClkDiv = TMRB_CLK_DIV; |
AnnaBridge | 185:08ed48f1de7f | 57 | m_tmrb0.UpCntCtrl = TMRB_AUTO_CLEAR; |
AnnaBridge | 185:08ed48f1de7f | 58 | m_tmrb0.TrailingTiming = TMR16A_100US; |
AnnaBridge | 185:08ed48f1de7f | 59 | m_tmrb0.LeadingTiming = TMR16A_100US; |
AnnaBridge | 185:08ed48f1de7f | 60 | TMRB_Init(TSB_TB0, &m_tmrb0); |
AnnaBridge | 185:08ed48f1de7f | 61 | |
AnnaBridge | 185:08ed48f1de7f | 62 | // Enable TMRB when system is in idle mode |
AnnaBridge | 185:08ed48f1de7f | 63 | TMRB_SetIdleMode(TSB_TB0, ENABLE); |
AnnaBridge | 185:08ed48f1de7f | 64 | // Starts TSB_TB0 |
AnnaBridge | 185:08ed48f1de7f | 65 | TMRB_SetRunState(TSB_TB0, TMRB_RUN); |
AnnaBridge | 185:08ed48f1de7f | 66 | } |
AnnaBridge | 185:08ed48f1de7f | 67 | |
AnnaBridge | 185:08ed48f1de7f | 68 | uint32_t us_ticker_read(void) |
AnnaBridge | 185:08ed48f1de7f | 69 | { |
AnnaBridge | 185:08ed48f1de7f | 70 | uint32_t ret_val = 0; |
AnnaBridge | 185:08ed48f1de7f | 71 | |
AnnaBridge | 185:08ed48f1de7f | 72 | if (!us_ticker_inited) { |
AnnaBridge | 185:08ed48f1de7f | 73 | us_ticker_init(); |
AnnaBridge | 185:08ed48f1de7f | 74 | } |
AnnaBridge | 185:08ed48f1de7f | 75 | |
AnnaBridge | 185:08ed48f1de7f | 76 | ret_val = (uint32_t)TMRB_GetUpCntValue(TSB_TB0); |
AnnaBridge | 185:08ed48f1de7f | 77 | |
AnnaBridge | 185:08ed48f1de7f | 78 | return ret_val; |
AnnaBridge | 185:08ed48f1de7f | 79 | } |
AnnaBridge | 185:08ed48f1de7f | 80 | |
AnnaBridge | 185:08ed48f1de7f | 81 | void us_ticker_set_interrupt(timestamp_t timestamp) |
AnnaBridge | 185:08ed48f1de7f | 82 | { |
AnnaBridge | 185:08ed48f1de7f | 83 | TMRB_InitTypeDef m_tmrb1; |
AnnaBridge | 185:08ed48f1de7f | 84 | TMRB_FFOutputTypeDef FFStruct; |
AnnaBridge | 185:08ed48f1de7f | 85 | |
AnnaBridge | 185:08ed48f1de7f | 86 | const uint32_t now_ticks = us_ticker_read(); |
AnnaBridge | 185:08ed48f1de7f | 87 | uint32_t delta_ticks = |
AnnaBridge | 185:08ed48f1de7f | 88 | timestamp >= now_ticks ? timestamp - now_ticks : (uint32_t)((uint64_t) timestamp + 0xFFFF - now_ticks); |
AnnaBridge | 185:08ed48f1de7f | 89 | |
AnnaBridge | 185:08ed48f1de7f | 90 | if (delta_ticks == 0) { |
AnnaBridge | 185:08ed48f1de7f | 91 | /* The requested delay is less than the minimum resolution of this counter. */ |
AnnaBridge | 185:08ed48f1de7f | 92 | delta_ticks = 1; |
AnnaBridge | 185:08ed48f1de7f | 93 | } |
AnnaBridge | 185:08ed48f1de7f | 94 | |
AnnaBridge | 185:08ed48f1de7f | 95 | // Ticker interrupt handle |
AnnaBridge | 185:08ed48f1de7f | 96 | TMRB_Enable(TSB_TB1); |
AnnaBridge | 185:08ed48f1de7f | 97 | TMRB_SetRunState(TSB_TB1, TMRB_STOP); |
AnnaBridge | 185:08ed48f1de7f | 98 | NVIC_SetVector(INTTB1_IRQn, (uint32_t)us_ticker_irq_handler); |
AnnaBridge | 185:08ed48f1de7f | 99 | NVIC_EnableIRQ(INTTB1_IRQn); |
AnnaBridge | 185:08ed48f1de7f | 100 | |
AnnaBridge | 185:08ed48f1de7f | 101 | // Split delta for preventing the Multiply overflowing |
AnnaBridge | 185:08ed48f1de7f | 102 | FFStruct.FlipflopCtrl = TMRB_FLIPFLOP_CLEAR; |
AnnaBridge | 185:08ed48f1de7f | 103 | FFStruct.FlipflopReverseTrg = TMRB_DISABLE_FLIPFLOP; |
AnnaBridge | 185:08ed48f1de7f | 104 | TMRB_SetFlipFlop(TSB_TB1, &FFStruct); |
AnnaBridge | 185:08ed48f1de7f | 105 | |
AnnaBridge | 185:08ed48f1de7f | 106 | // TSB_TB0 using free-run |
AnnaBridge | 185:08ed48f1de7f | 107 | m_tmrb1.Mode = TMRB_INTERVAL_TIMER; |
AnnaBridge | 185:08ed48f1de7f | 108 | m_tmrb1.ClkDiv = TMRB_CLK_DIV; |
AnnaBridge | 185:08ed48f1de7f | 109 | m_tmrb1.UpCntCtrl = TMRB_AUTO_CLEAR; |
AnnaBridge | 185:08ed48f1de7f | 110 | m_tmrb1.TrailingTiming = delta_ticks; |
AnnaBridge | 185:08ed48f1de7f | 111 | m_tmrb1.LeadingTiming = delta_ticks; |
AnnaBridge | 185:08ed48f1de7f | 112 | TMRB_Init(TSB_TB1, &m_tmrb1); |
AnnaBridge | 185:08ed48f1de7f | 113 | TMRB_SetINTMask(TSB_TB1,TMRB_MASK_OVERFLOW_INT | TMRB_MASK_MATCH_LEADING_INT); |
AnnaBridge | 185:08ed48f1de7f | 114 | // Enable TMRB when system is in idle mode |
AnnaBridge | 185:08ed48f1de7f | 115 | TMRB_SetIdleMode(TSB_TB1, ENABLE); |
AnnaBridge | 185:08ed48f1de7f | 116 | TMRB_SetRunState(TSB_TB1, TMRB_RUN); |
AnnaBridge | 185:08ed48f1de7f | 117 | } |
AnnaBridge | 185:08ed48f1de7f | 118 | |
AnnaBridge | 185:08ed48f1de7f | 119 | void us_ticker_fire_interrupt(void) |
AnnaBridge | 185:08ed48f1de7f | 120 | { |
AnnaBridge | 185:08ed48f1de7f | 121 | NVIC_SetPendingIRQ(INTTB1_IRQn); |
AnnaBridge | 185:08ed48f1de7f | 122 | } |
AnnaBridge | 185:08ed48f1de7f | 123 | |
AnnaBridge | 185:08ed48f1de7f | 124 | void us_ticker_disable_interrupt(void) |
AnnaBridge | 185:08ed48f1de7f | 125 | { |
AnnaBridge | 185:08ed48f1de7f | 126 | // Also disable interrupts by NVIC |
AnnaBridge | 185:08ed48f1de7f | 127 | NVIC_DisableIRQ(INTTB1_IRQn); |
AnnaBridge | 185:08ed48f1de7f | 128 | } |
AnnaBridge | 185:08ed48f1de7f | 129 | |
AnnaBridge | 185:08ed48f1de7f | 130 | void us_ticker_clear_interrupt(void) |
AnnaBridge | 185:08ed48f1de7f | 131 | { |
AnnaBridge | 185:08ed48f1de7f | 132 | // No flag to clear |
AnnaBridge | 185:08ed48f1de7f | 133 | } |