mbed library sources
Fork of mbed-src by
targets/hal/TARGET_Atmel/TARGET_SAM21/us_ticker.c@579:53297373a894, 2015-07-01 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Jul 01 09:45:11 2015 +0100
- Revision:
- 579:53297373a894
Synchronized with git revision d5b4d2ab9c47edb4dc5776e7177b0c2263459081
Full URL: https://github.com/mbedmicro/mbed/commit/d5b4d2ab9c47edb4dc5776e7177b0c2263459081/
Initial version of drivers for SAMR21
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 579:53297373a894 | 1 | /* mbed Microcontroller Library |
mbed_official | 579:53297373a894 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 579:53297373a894 | 3 | * |
mbed_official | 579:53297373a894 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 579:53297373a894 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 579:53297373a894 | 6 | * You may obtain a copy of the License at |
mbed_official | 579:53297373a894 | 7 | * |
mbed_official | 579:53297373a894 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 579:53297373a894 | 9 | * |
mbed_official | 579:53297373a894 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 579:53297373a894 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 579:53297373a894 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 579:53297373a894 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 579:53297373a894 | 14 | * limitations under the License. |
mbed_official | 579:53297373a894 | 15 | */ |
mbed_official | 579:53297373a894 | 16 | #include <stddef.h> |
mbed_official | 579:53297373a894 | 17 | #include "us_ticker_api.h" |
mbed_official | 579:53297373a894 | 18 | #include "cmsis.h" |
mbed_official | 579:53297373a894 | 19 | #include "mbed_assert.h" |
mbed_official | 579:53297373a894 | 20 | #include "ins_gclk.h" |
mbed_official | 579:53297373a894 | 21 | #include "compiler.h" |
mbed_official | 579:53297373a894 | 22 | #include "system.h" |
mbed_official | 579:53297373a894 | 23 | #include "tc.h" |
mbed_official | 579:53297373a894 | 24 | #include "tc_interrupt.h" |
mbed_official | 579:53297373a894 | 25 | |
mbed_official | 579:53297373a894 | 26 | #define TICKER_COUNTER_uS TC4 |
mbed_official | 579:53297373a894 | 27 | #define TICKER_COUNTER_IRQn TC4_IRQn |
mbed_official | 579:53297373a894 | 28 | #define TICKER_COUNTER_Handlr TC4_Handler |
mbed_official | 579:53297373a894 | 29 | |
mbed_official | 579:53297373a894 | 30 | static int us_ticker_inited = 0; |
mbed_official | 579:53297373a894 | 31 | extern uint8_t g_sys_init; |
mbed_official | 579:53297373a894 | 32 | |
mbed_official | 579:53297373a894 | 33 | struct tc_module us_ticker_module; |
mbed_official | 579:53297373a894 | 34 | |
mbed_official | 579:53297373a894 | 35 | |
mbed_official | 579:53297373a894 | 36 | static inline void tc_clear_interrupt( |
mbed_official | 579:53297373a894 | 37 | struct tc_module *const module, |
mbed_official | 579:53297373a894 | 38 | const enum tc_callback callback_type) |
mbed_official | 579:53297373a894 | 39 | { |
mbed_official | 579:53297373a894 | 40 | /* Sanity check arguments */ |
mbed_official | 579:53297373a894 | 41 | MBED_ASSERT(module); |
mbed_official | 579:53297373a894 | 42 | |
mbed_official | 579:53297373a894 | 43 | /* Clear interrupt flags */ |
mbed_official | 579:53297373a894 | 44 | if (callback_type == TC_CALLBACK_CC_CHANNEL0) { |
mbed_official | 579:53297373a894 | 45 | module->hw->COUNT8.INTENCLR.reg = TC_INTFLAG_MC(1); |
mbed_official | 579:53297373a894 | 46 | } else if (callback_type == TC_CALLBACK_CC_CHANNEL1) { |
mbed_official | 579:53297373a894 | 47 | module->hw->COUNT8.INTENCLR.reg = TC_INTFLAG_MC(2); |
mbed_official | 579:53297373a894 | 48 | } else { |
mbed_official | 579:53297373a894 | 49 | module->hw->COUNT8.INTENCLR.reg = (1 << callback_type); |
mbed_official | 579:53297373a894 | 50 | } |
mbed_official | 579:53297373a894 | 51 | } |
mbed_official | 579:53297373a894 | 52 | |
mbed_official | 579:53297373a894 | 53 | void us_ticker_irq_handler_internal(struct tc_module* us_tc_module) |
mbed_official | 579:53297373a894 | 54 | { |
mbed_official | 579:53297373a894 | 55 | uint32_t status_flags; |
mbed_official | 579:53297373a894 | 56 | |
mbed_official | 579:53297373a894 | 57 | /* Clear TC capture overflow and TC count overflow */ |
mbed_official | 579:53297373a894 | 58 | status_flags = TC_STATUS_CAPTURE_OVERFLOW | TC_STATUS_COUNT_OVERFLOW; |
mbed_official | 579:53297373a894 | 59 | tc_clear_status(&us_ticker_module, status_flags); |
mbed_official | 579:53297373a894 | 60 | |
mbed_official | 579:53297373a894 | 61 | us_ticker_irq_handler(); |
mbed_official | 579:53297373a894 | 62 | } |
mbed_official | 579:53297373a894 | 63 | |
mbed_official | 579:53297373a894 | 64 | void us_ticker_init(void) |
mbed_official | 579:53297373a894 | 65 | { |
mbed_official | 579:53297373a894 | 66 | uint32_t cycles_per_us; |
mbed_official | 579:53297373a894 | 67 | uint32_t prescaler = 0; |
mbed_official | 579:53297373a894 | 68 | struct tc_config config_tc; |
mbed_official | 579:53297373a894 | 69 | enum status_code ret_status; |
mbed_official | 579:53297373a894 | 70 | |
mbed_official | 579:53297373a894 | 71 | if (us_ticker_inited) return; |
mbed_official | 579:53297373a894 | 72 | us_ticker_inited = 1; |
mbed_official | 579:53297373a894 | 73 | |
mbed_official | 579:53297373a894 | 74 | if (g_sys_init == 0) { |
mbed_official | 579:53297373a894 | 75 | system_init(); |
mbed_official | 579:53297373a894 | 76 | g_sys_init = 1; |
mbed_official | 579:53297373a894 | 77 | } |
mbed_official | 579:53297373a894 | 78 | |
mbed_official | 579:53297373a894 | 79 | tc_get_config_defaults(&config_tc); |
mbed_official | 579:53297373a894 | 80 | |
mbed_official | 579:53297373a894 | 81 | cycles_per_us = system_gclk_gen_get_hz(config_tc.clock_source) / 1000000; |
mbed_official | 579:53297373a894 | 82 | MBED_ASSERT(cycles_per_us > 0); |
mbed_official | 579:53297373a894 | 83 | /*while((cycles_per_us & 1) == 0 && prescaler <= 10) { |
mbed_official | 579:53297373a894 | 84 | cycles_per_us = cycles_per_us >> 1; |
mbed_official | 579:53297373a894 | 85 | prescaler++; |
mbed_official | 579:53297373a894 | 86 | }*/ |
mbed_official | 579:53297373a894 | 87 | while((cycles_per_us > 1) && (prescaler <= 10)) { |
mbed_official | 579:53297373a894 | 88 | cycles_per_us = cycles_per_us >> 1; |
mbed_official | 579:53297373a894 | 89 | prescaler++; |
mbed_official | 579:53297373a894 | 90 | } |
mbed_official | 579:53297373a894 | 91 | if (prescaler >= 9) { |
mbed_official | 579:53297373a894 | 92 | prescaler = 7; |
mbed_official | 579:53297373a894 | 93 | } else if (prescaler >= 7) { |
mbed_official | 579:53297373a894 | 94 | prescaler = 6; |
mbed_official | 579:53297373a894 | 95 | } else if (prescaler >= 5) { |
mbed_official | 579:53297373a894 | 96 | prescaler = 5; |
mbed_official | 579:53297373a894 | 97 | } |
mbed_official | 579:53297373a894 | 98 | |
mbed_official | 579:53297373a894 | 99 | config_tc.clock_prescaler = TC_CTRLA_PRESCALER(prescaler); |
mbed_official | 579:53297373a894 | 100 | config_tc.counter_size = TC_COUNTER_SIZE_32BIT; |
mbed_official | 579:53297373a894 | 101 | config_tc.run_in_standby = true; |
mbed_official | 579:53297373a894 | 102 | config_tc.counter_32_bit.value = 0; |
mbed_official | 579:53297373a894 | 103 | config_tc.counter_32_bit.compare_capture_channel[TC_COMPARE_CAPTURE_CHANNEL_0] = 0xFFFFFFFF; |
mbed_official | 579:53297373a894 | 104 | |
mbed_official | 579:53297373a894 | 105 | /* Initialize the timer */ |
mbed_official | 579:53297373a894 | 106 | ret_status = tc_init(&us_ticker_module, TICKER_COUNTER_uS, &config_tc); |
mbed_official | 579:53297373a894 | 107 | MBED_ASSERT(ret_status == STATUS_OK); |
mbed_official | 579:53297373a894 | 108 | |
mbed_official | 579:53297373a894 | 109 | /* Register callback function */ |
mbed_official | 579:53297373a894 | 110 | tc_register_callback(&us_ticker_module, (tc_callback_t)us_ticker_irq_handler_internal, TC_CALLBACK_CC_CHANNEL0); |
mbed_official | 579:53297373a894 | 111 | |
mbed_official | 579:53297373a894 | 112 | /* Enable the timer module */ |
mbed_official | 579:53297373a894 | 113 | tc_enable(&us_ticker_module); |
mbed_official | 579:53297373a894 | 114 | } |
mbed_official | 579:53297373a894 | 115 | |
mbed_official | 579:53297373a894 | 116 | uint32_t us_ticker_read() |
mbed_official | 579:53297373a894 | 117 | { |
mbed_official | 579:53297373a894 | 118 | if (!us_ticker_inited) |
mbed_official | 579:53297373a894 | 119 | us_ticker_init(); |
mbed_official | 579:53297373a894 | 120 | |
mbed_official | 579:53297373a894 | 121 | return tc_get_count_value(&us_ticker_module); |
mbed_official | 579:53297373a894 | 122 | } |
mbed_official | 579:53297373a894 | 123 | |
mbed_official | 579:53297373a894 | 124 | void us_ticker_set_interrupt(timestamp_t timestamp) |
mbed_official | 579:53297373a894 | 125 | { |
mbed_official | 579:53297373a894 | 126 | uint32_t cur_time; |
mbed_official | 579:53297373a894 | 127 | int32_t delta; |
mbed_official | 579:53297373a894 | 128 | |
mbed_official | 579:53297373a894 | 129 | cur_time = us_ticker_read(); |
mbed_official | 579:53297373a894 | 130 | delta = (int32_t)((uint32_t)timestamp - cur_time); |
mbed_official | 579:53297373a894 | 131 | if (delta < 0) { |
mbed_official | 579:53297373a894 | 132 | /* Event already occurred in past */ |
mbed_official | 579:53297373a894 | 133 | us_ticker_irq_handler(); |
mbed_official | 579:53297373a894 | 134 | return; |
mbed_official | 579:53297373a894 | 135 | } |
mbed_official | 579:53297373a894 | 136 | |
mbed_official | 579:53297373a894 | 137 | NVIC_DisableIRQ(TICKER_COUNTER_IRQn); |
mbed_official | 579:53297373a894 | 138 | NVIC_SetVector(TICKER_COUNTER_IRQn, (uint32_t)TICKER_COUNTER_Handlr); |
mbed_official | 579:53297373a894 | 139 | |
mbed_official | 579:53297373a894 | 140 | /* Enable the callback */ |
mbed_official | 579:53297373a894 | 141 | tc_enable_callback(&us_ticker_module, TC_CALLBACK_CC_CHANNEL0); |
mbed_official | 579:53297373a894 | 142 | tc_set_compare_value(&us_ticker_module, TC_COMPARE_CAPTURE_CHANNEL_0, (uint32_t)timestamp); |
mbed_official | 579:53297373a894 | 143 | |
mbed_official | 579:53297373a894 | 144 | NVIC_EnableIRQ(TICKER_COUNTER_IRQn); |
mbed_official | 579:53297373a894 | 145 | } |
mbed_official | 579:53297373a894 | 146 | |
mbed_official | 579:53297373a894 | 147 | void us_ticker_disable_interrupt(void) |
mbed_official | 579:53297373a894 | 148 | { |
mbed_official | 579:53297373a894 | 149 | /* Disable the callback */ |
mbed_official | 579:53297373a894 | 150 | tc_disable_callback(&us_ticker_module, TC_CALLBACK_CC_CHANNEL0); |
mbed_official | 579:53297373a894 | 151 | NVIC_DisableIRQ(TICKER_COUNTER_IRQn); |
mbed_official | 579:53297373a894 | 152 | } |
mbed_official | 579:53297373a894 | 153 | |
mbed_official | 579:53297373a894 | 154 | void us_ticker_clear_interrupt(void) |
mbed_official | 579:53297373a894 | 155 | { |
mbed_official | 579:53297373a894 | 156 | uint32_t status_flags; |
mbed_official | 579:53297373a894 | 157 | |
mbed_official | 579:53297373a894 | 158 | /* Clear TC channel 0 match */ |
mbed_official | 579:53297373a894 | 159 | status_flags = TC_STATUS_CHANNEL_0_MATCH; |
mbed_official | 579:53297373a894 | 160 | tc_clear_status(&us_ticker_module, status_flags); |
mbed_official | 579:53297373a894 | 161 | |
mbed_official | 579:53297373a894 | 162 | /* Clear the interrupt */ |
mbed_official | 579:53297373a894 | 163 | tc_clear_interrupt(&us_ticker_module, TC_CALLBACK_CC_CHANNEL0); |
mbed_official | 579:53297373a894 | 164 | NVIC_ClearPendingIRQ(TICKER_COUNTER_IRQn); |
mbed_official | 579:53297373a894 | 165 | } |