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