test
Fork of mbed-dev by
targets/TARGET_Realtek/TARGET_AMEBA/timer_api.c@168:e84263d55307, 2017-06-21 (annotated)
- Committer:
- AnnaBridge
- Date:
- Wed Jun 21 17:46:44 2017 +0100
- Revision:
- 168:e84263d55307
This updates the lib to the mbed lib v 145
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 168:e84263d55307 | 1 | /* mbed Microcontroller Library |
AnnaBridge | 168:e84263d55307 | 2 | * Copyright (c) 2013-2016 Realtek Semiconductor Corp. |
AnnaBridge | 168:e84263d55307 | 3 | * |
AnnaBridge | 168:e84263d55307 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AnnaBridge | 168:e84263d55307 | 5 | * you may not use this file except in compliance with the License. |
AnnaBridge | 168:e84263d55307 | 6 | * You may obtain a copy of the License at |
AnnaBridge | 168:e84263d55307 | 7 | * |
AnnaBridge | 168:e84263d55307 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AnnaBridge | 168:e84263d55307 | 9 | * |
AnnaBridge | 168:e84263d55307 | 10 | * Unless required by applicable law or agreed to in writing, software |
AnnaBridge | 168:e84263d55307 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AnnaBridge | 168:e84263d55307 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AnnaBridge | 168:e84263d55307 | 13 | * See the License for the specific language governing permissions and |
AnnaBridge | 168:e84263d55307 | 14 | * limitations under the License. |
AnnaBridge | 168:e84263d55307 | 15 | */ |
AnnaBridge | 168:e84263d55307 | 16 | |
AnnaBridge | 168:e84263d55307 | 17 | #include "objects.h" |
AnnaBridge | 168:e84263d55307 | 18 | #include "timer_api.h" |
AnnaBridge | 168:e84263d55307 | 19 | |
AnnaBridge | 168:e84263d55307 | 20 | #if CONFIG_TIMER_EN |
AnnaBridge | 168:e84263d55307 | 21 | |
AnnaBridge | 168:e84263d55307 | 22 | extern HAL_TIMER_OP HalTimerOp; |
AnnaBridge | 168:e84263d55307 | 23 | |
AnnaBridge | 168:e84263d55307 | 24 | extern HAL_Status HalTimerInitRtl8195a_Patch( |
AnnaBridge | 168:e84263d55307 | 25 | IN VOID *Data |
AnnaBridge | 168:e84263d55307 | 26 | ); |
AnnaBridge | 168:e84263d55307 | 27 | |
AnnaBridge | 168:e84263d55307 | 28 | static void gtimer_timeout_handler (uint32_t tid) |
AnnaBridge | 168:e84263d55307 | 29 | { |
AnnaBridge | 168:e84263d55307 | 30 | gtimer_t *obj = (gtimer_t *)tid; |
AnnaBridge | 168:e84263d55307 | 31 | gtimer_irq_handler handler; |
AnnaBridge | 168:e84263d55307 | 32 | u8 timer_id = obj->hal_gtimer_adp.TimerId; |
AnnaBridge | 168:e84263d55307 | 33 | |
AnnaBridge | 168:e84263d55307 | 34 | if (obj->handler != NULL) { |
AnnaBridge | 168:e84263d55307 | 35 | handler = (gtimer_irq_handler)obj->handler; |
AnnaBridge | 168:e84263d55307 | 36 | handler(obj->hid); |
AnnaBridge | 168:e84263d55307 | 37 | } |
AnnaBridge | 168:e84263d55307 | 38 | |
AnnaBridge | 168:e84263d55307 | 39 | if (!obj->is_periodcal) { |
AnnaBridge | 168:e84263d55307 | 40 | gtimer_stop(obj); |
AnnaBridge | 168:e84263d55307 | 41 | } |
AnnaBridge | 168:e84263d55307 | 42 | |
AnnaBridge | 168:e84263d55307 | 43 | if(timer_id < 2) { |
AnnaBridge | 168:e84263d55307 | 44 | // Timer0 | Timer1: clear ISR here |
AnnaBridge | 168:e84263d55307 | 45 | // Timer 2~7 ISR will be cleared in HAL |
AnnaBridge | 168:e84263d55307 | 46 | HalTimerClearIsr(timer_id); |
AnnaBridge | 168:e84263d55307 | 47 | } |
AnnaBridge | 168:e84263d55307 | 48 | } |
AnnaBridge | 168:e84263d55307 | 49 | |
AnnaBridge | 168:e84263d55307 | 50 | void gtimer_init (gtimer_t *obj, uint32_t tid) |
AnnaBridge | 168:e84263d55307 | 51 | { |
AnnaBridge | 168:e84263d55307 | 52 | PTIMER_ADAPTER pTimerAdapter = &(obj->hal_gtimer_adp); |
AnnaBridge | 168:e84263d55307 | 53 | |
AnnaBridge | 168:e84263d55307 | 54 | if ((tid == 1) || (tid == 6) || (tid == 7)) { |
AnnaBridge | 168:e84263d55307 | 55 | DBG_TIMER_ERR("gtimer_init: This timer is reserved for HAL driver\r\n", tid); |
AnnaBridge | 168:e84263d55307 | 56 | return; |
AnnaBridge | 168:e84263d55307 | 57 | } |
AnnaBridge | 168:e84263d55307 | 58 | |
AnnaBridge | 168:e84263d55307 | 59 | if (tid > GTIMER_MAX) { |
AnnaBridge | 168:e84263d55307 | 60 | DBG_TIMER_ERR("gtimer_init: Invalid TimerId=%d\r\n", tid); |
AnnaBridge | 168:e84263d55307 | 61 | return; |
AnnaBridge | 168:e84263d55307 | 62 | } |
AnnaBridge | 168:e84263d55307 | 63 | |
AnnaBridge | 168:e84263d55307 | 64 | pTimerAdapter->IrqDis = 0; // Enable Irq @ initial |
AnnaBridge | 168:e84263d55307 | 65 | pTimerAdapter->IrqHandle.IrqFun = (IRQ_FUN) gtimer_timeout_handler; |
AnnaBridge | 168:e84263d55307 | 66 | if(tid == 0) { |
AnnaBridge | 168:e84263d55307 | 67 | pTimerAdapter->IrqHandle.IrqNum = TIMER0_IRQ; |
AnnaBridge | 168:e84263d55307 | 68 | } else if(tid == 1) { |
AnnaBridge | 168:e84263d55307 | 69 | pTimerAdapter->IrqHandle.IrqNum = TIMER1_IRQ; |
AnnaBridge | 168:e84263d55307 | 70 | } else { |
AnnaBridge | 168:e84263d55307 | 71 | pTimerAdapter->IrqHandle.IrqNum = TIMER2_7_IRQ; |
AnnaBridge | 168:e84263d55307 | 72 | } |
AnnaBridge | 168:e84263d55307 | 73 | pTimerAdapter->IrqHandle.Priority = 0; |
AnnaBridge | 168:e84263d55307 | 74 | pTimerAdapter->IrqHandle.Data = (u32)obj; |
AnnaBridge | 168:e84263d55307 | 75 | pTimerAdapter->TimerId = (u8)tid; |
AnnaBridge | 168:e84263d55307 | 76 | pTimerAdapter->TimerIrqPriority = 0; |
AnnaBridge | 168:e84263d55307 | 77 | pTimerAdapter->TimerLoadValueUs = 0xFFFFFFFF; // Just a whatever value |
AnnaBridge | 168:e84263d55307 | 78 | pTimerAdapter->TimerMode = USER_DEFINED; |
AnnaBridge | 168:e84263d55307 | 79 | |
AnnaBridge | 168:e84263d55307 | 80 | HalTimerInit ((VOID*) pTimerAdapter); |
AnnaBridge | 168:e84263d55307 | 81 | } |
AnnaBridge | 168:e84263d55307 | 82 | |
AnnaBridge | 168:e84263d55307 | 83 | void gtimer_deinit (gtimer_t *obj) |
AnnaBridge | 168:e84263d55307 | 84 | { |
AnnaBridge | 168:e84263d55307 | 85 | PTIMER_ADAPTER pTimerAdapter = &(obj->hal_gtimer_adp); |
AnnaBridge | 168:e84263d55307 | 86 | |
AnnaBridge | 168:e84263d55307 | 87 | HalTimerDeInit((void*)pTimerAdapter); |
AnnaBridge | 168:e84263d55307 | 88 | } |
AnnaBridge | 168:e84263d55307 | 89 | |
AnnaBridge | 168:e84263d55307 | 90 | uint32_t gtimer_read_tick (gtimer_t *obj) |
AnnaBridge | 168:e84263d55307 | 91 | { |
AnnaBridge | 168:e84263d55307 | 92 | PTIMER_ADAPTER pTimerAdapter = &obj->hal_gtimer_adp; |
AnnaBridge | 168:e84263d55307 | 93 | |
AnnaBridge | 168:e84263d55307 | 94 | return (HalTimerOp.HalTimerReadCount(pTimerAdapter->TimerId)); |
AnnaBridge | 168:e84263d55307 | 95 | } |
AnnaBridge | 168:e84263d55307 | 96 | |
AnnaBridge | 168:e84263d55307 | 97 | uint64_t gtimer_read_us (gtimer_t *obj) |
AnnaBridge | 168:e84263d55307 | 98 | { |
AnnaBridge | 168:e84263d55307 | 99 | uint64_t time_us; |
AnnaBridge | 168:e84263d55307 | 100 | |
AnnaBridge | 168:e84263d55307 | 101 | time_us = gtimer_read_tick(obj)*1000000/32768; |
AnnaBridge | 168:e84263d55307 | 102 | |
AnnaBridge | 168:e84263d55307 | 103 | return (time_us); |
AnnaBridge | 168:e84263d55307 | 104 | } |
AnnaBridge | 168:e84263d55307 | 105 | |
AnnaBridge | 168:e84263d55307 | 106 | void gtimer_reload (gtimer_t *obj, uint32_t duration_us) |
AnnaBridge | 168:e84263d55307 | 107 | { |
AnnaBridge | 168:e84263d55307 | 108 | PTIMER_ADAPTER pTimerAdapter = &obj->hal_gtimer_adp; |
AnnaBridge | 168:e84263d55307 | 109 | |
AnnaBridge | 168:e84263d55307 | 110 | HalTimerReLoad(pTimerAdapter->TimerId, duration_us); |
AnnaBridge | 168:e84263d55307 | 111 | } |
AnnaBridge | 168:e84263d55307 | 112 | |
AnnaBridge | 168:e84263d55307 | 113 | |
AnnaBridge | 168:e84263d55307 | 114 | void gtimer_start (gtimer_t *obj) |
AnnaBridge | 168:e84263d55307 | 115 | { |
AnnaBridge | 168:e84263d55307 | 116 | PTIMER_ADAPTER pTimerAdapter = &obj->hal_gtimer_adp; |
AnnaBridge | 168:e84263d55307 | 117 | u8 TimerId = pTimerAdapter->TimerId; |
AnnaBridge | 168:e84263d55307 | 118 | |
AnnaBridge | 168:e84263d55307 | 119 | HalTimerEnable(TimerId); |
AnnaBridge | 168:e84263d55307 | 120 | } |
AnnaBridge | 168:e84263d55307 | 121 | |
AnnaBridge | 168:e84263d55307 | 122 | void gtimer_start_one_shout (gtimer_t *obj, uint32_t duration_us, void* handler, uint32_t hid) |
AnnaBridge | 168:e84263d55307 | 123 | { |
AnnaBridge | 168:e84263d55307 | 124 | obj->is_periodcal = _FALSE; |
AnnaBridge | 168:e84263d55307 | 125 | obj->handler = handler; |
AnnaBridge | 168:e84263d55307 | 126 | obj->hid = hid; |
AnnaBridge | 168:e84263d55307 | 127 | gtimer_reload(obj, duration_us); |
AnnaBridge | 168:e84263d55307 | 128 | gtimer_start(obj); |
AnnaBridge | 168:e84263d55307 | 129 | } |
AnnaBridge | 168:e84263d55307 | 130 | |
AnnaBridge | 168:e84263d55307 | 131 | void gtimer_start_periodical (gtimer_t *obj, uint32_t duration_us, void* handler, uint32_t hid) |
AnnaBridge | 168:e84263d55307 | 132 | { |
AnnaBridge | 168:e84263d55307 | 133 | obj->is_periodcal = _TRUE; |
AnnaBridge | 168:e84263d55307 | 134 | obj->handler = handler; |
AnnaBridge | 168:e84263d55307 | 135 | obj->hid = hid; |
AnnaBridge | 168:e84263d55307 | 136 | if (duration_us > GTIMER_TICK_US) { |
AnnaBridge | 168:e84263d55307 | 137 | // reload will takes extra 1 tick |
AnnaBridge | 168:e84263d55307 | 138 | duration_us -= GTIMER_TICK_US; |
AnnaBridge | 168:e84263d55307 | 139 | } |
AnnaBridge | 168:e84263d55307 | 140 | gtimer_reload(obj, duration_us); |
AnnaBridge | 168:e84263d55307 | 141 | gtimer_start(obj); |
AnnaBridge | 168:e84263d55307 | 142 | } |
AnnaBridge | 168:e84263d55307 | 143 | |
AnnaBridge | 168:e84263d55307 | 144 | void gtimer_stop (gtimer_t *obj) |
AnnaBridge | 168:e84263d55307 | 145 | { |
AnnaBridge | 168:e84263d55307 | 146 | PTIMER_ADAPTER pTimerAdapter = &obj->hal_gtimer_adp; |
AnnaBridge | 168:e84263d55307 | 147 | HalTimerDisable(pTimerAdapter->TimerId); |
AnnaBridge | 168:e84263d55307 | 148 | } |
AnnaBridge | 168:e84263d55307 | 149 | |
AnnaBridge | 168:e84263d55307 | 150 | #endif // end of "#if CONFIG_TIMER_EN" |
AnnaBridge | 168:e84263d55307 | 151 |