![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/targets/TARGET_Atmel/TARGET_SAM_CortexM0P/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-2015 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 "cmsis.h" |
kadonotakashi | 0:8fdf9a60065b | 19 | #include "mbed_assert.h" |
kadonotakashi | 0:8fdf9a60065b | 20 | #include "ins_gclk.h" |
kadonotakashi | 0:8fdf9a60065b | 21 | #include "compiler.h" |
kadonotakashi | 0:8fdf9a60065b | 22 | #include "system.h" |
kadonotakashi | 0:8fdf9a60065b | 23 | #include "tc.h" |
kadonotakashi | 0:8fdf9a60065b | 24 | #include "tc_interrupt.h" |
kadonotakashi | 0:8fdf9a60065b | 25 | |
kadonotakashi | 0:8fdf9a60065b | 26 | #if (SAMD21) || (SAMR21) |
kadonotakashi | 0:8fdf9a60065b | 27 | #define TICKER_COUNTER_uS TC4 |
kadonotakashi | 0:8fdf9a60065b | 28 | #define TICKER_COUNTER_IRQn TC4_IRQn |
kadonotakashi | 0:8fdf9a60065b | 29 | #define TICKER_COUNTER_Handlr TC4_Handler |
kadonotakashi | 0:8fdf9a60065b | 30 | #elif (SAML21) /*SAML21 TCC4 does not support 32 bit counter operations*/ |
kadonotakashi | 0:8fdf9a60065b | 31 | #define TICKER_COUNTER_uS TC0 |
kadonotakashi | 0:8fdf9a60065b | 32 | #define TICKER_COUNTER_IRQn TC0_IRQn |
kadonotakashi | 0:8fdf9a60065b | 33 | #define TICKER_COUNTER_Handlr TC0_Handler |
kadonotakashi | 0:8fdf9a60065b | 34 | #endif |
kadonotakashi | 0:8fdf9a60065b | 35 | |
kadonotakashi | 0:8fdf9a60065b | 36 | static int us_ticker_inited = 0; |
kadonotakashi | 0:8fdf9a60065b | 37 | extern uint8_t g_sys_init; |
kadonotakashi | 0:8fdf9a60065b | 38 | |
kadonotakashi | 0:8fdf9a60065b | 39 | struct tc_module us_ticker_module; |
kadonotakashi | 0:8fdf9a60065b | 40 | |
kadonotakashi | 0:8fdf9a60065b | 41 | |
kadonotakashi | 0:8fdf9a60065b | 42 | static inline void tc_clear_interrupt( |
kadonotakashi | 0:8fdf9a60065b | 43 | struct tc_module *const module, |
kadonotakashi | 0:8fdf9a60065b | 44 | const enum tc_callback callback_type) |
kadonotakashi | 0:8fdf9a60065b | 45 | { |
kadonotakashi | 0:8fdf9a60065b | 46 | /* Sanity check arguments */ |
kadonotakashi | 0:8fdf9a60065b | 47 | MBED_ASSERT(module); |
kadonotakashi | 0:8fdf9a60065b | 48 | |
kadonotakashi | 0:8fdf9a60065b | 49 | /* Clear interrupt flags */ |
kadonotakashi | 0:8fdf9a60065b | 50 | if (callback_type == TC_CALLBACK_CC_CHANNEL0) { |
kadonotakashi | 0:8fdf9a60065b | 51 | module->hw->COUNT8.INTENCLR.reg = TC_INTFLAG_MC(1); |
kadonotakashi | 0:8fdf9a60065b | 52 | } else if (callback_type == TC_CALLBACK_CC_CHANNEL1) { |
kadonotakashi | 0:8fdf9a60065b | 53 | module->hw->COUNT8.INTENCLR.reg = TC_INTFLAG_MC(2); |
kadonotakashi | 0:8fdf9a60065b | 54 | } else { |
kadonotakashi | 0:8fdf9a60065b | 55 | module->hw->COUNT8.INTENCLR.reg = (1 << callback_type); |
kadonotakashi | 0:8fdf9a60065b | 56 | } |
kadonotakashi | 0:8fdf9a60065b | 57 | } |
kadonotakashi | 0:8fdf9a60065b | 58 | |
kadonotakashi | 0:8fdf9a60065b | 59 | void us_ticker_irq_handler_internal(struct tc_module* us_tc_module) |
kadonotakashi | 0:8fdf9a60065b | 60 | { |
kadonotakashi | 0:8fdf9a60065b | 61 | uint32_t status_flags; |
kadonotakashi | 0:8fdf9a60065b | 62 | |
kadonotakashi | 0:8fdf9a60065b | 63 | /* Clear TC capture overflow and TC count overflow */ |
kadonotakashi | 0:8fdf9a60065b | 64 | status_flags = TC_STATUS_CAPTURE_OVERFLOW | TC_STATUS_COUNT_OVERFLOW; |
kadonotakashi | 0:8fdf9a60065b | 65 | tc_clear_status(&us_ticker_module, status_flags); |
kadonotakashi | 0:8fdf9a60065b | 66 | |
kadonotakashi | 0:8fdf9a60065b | 67 | us_ticker_irq_handler(); |
kadonotakashi | 0:8fdf9a60065b | 68 | } |
kadonotakashi | 0:8fdf9a60065b | 69 | |
kadonotakashi | 0:8fdf9a60065b | 70 | void us_ticker_init(void) |
kadonotakashi | 0:8fdf9a60065b | 71 | { |
kadonotakashi | 0:8fdf9a60065b | 72 | uint32_t cycles_per_us; |
kadonotakashi | 0:8fdf9a60065b | 73 | uint32_t prescaler = 0; |
kadonotakashi | 0:8fdf9a60065b | 74 | struct tc_config config_tc; |
kadonotakashi | 0:8fdf9a60065b | 75 | |
kadonotakashi | 0:8fdf9a60065b | 76 | if (us_ticker_inited) return; |
kadonotakashi | 0:8fdf9a60065b | 77 | us_ticker_inited = 1; |
kadonotakashi | 0:8fdf9a60065b | 78 | |
kadonotakashi | 0:8fdf9a60065b | 79 | if (g_sys_init == 0) { |
kadonotakashi | 0:8fdf9a60065b | 80 | system_init(); |
kadonotakashi | 0:8fdf9a60065b | 81 | g_sys_init = 1; |
kadonotakashi | 0:8fdf9a60065b | 82 | } |
kadonotakashi | 0:8fdf9a60065b | 83 | |
kadonotakashi | 0:8fdf9a60065b | 84 | tc_get_config_defaults(&config_tc); |
kadonotakashi | 0:8fdf9a60065b | 85 | |
kadonotakashi | 0:8fdf9a60065b | 86 | cycles_per_us = system_gclk_gen_get_hz(config_tc.clock_source) / 1000000; |
kadonotakashi | 0:8fdf9a60065b | 87 | MBED_ASSERT(cycles_per_us > 0); |
kadonotakashi | 0:8fdf9a60065b | 88 | /*while((cycles_per_us & 1) == 0 && prescaler <= 10) { |
kadonotakashi | 0:8fdf9a60065b | 89 | cycles_per_us = cycles_per_us >> 1; |
kadonotakashi | 0:8fdf9a60065b | 90 | prescaler++; |
kadonotakashi | 0:8fdf9a60065b | 91 | }*/ |
kadonotakashi | 0:8fdf9a60065b | 92 | while((cycles_per_us > 1) && (prescaler <= 10)) { |
kadonotakashi | 0:8fdf9a60065b | 93 | cycles_per_us = cycles_per_us >> 1; |
kadonotakashi | 0:8fdf9a60065b | 94 | prescaler++; |
kadonotakashi | 0:8fdf9a60065b | 95 | } |
kadonotakashi | 0:8fdf9a60065b | 96 | if (prescaler >= 9) { |
kadonotakashi | 0:8fdf9a60065b | 97 | prescaler = 7; |
kadonotakashi | 0:8fdf9a60065b | 98 | } else if (prescaler >= 7) { |
kadonotakashi | 0:8fdf9a60065b | 99 | prescaler = 6; |
kadonotakashi | 0:8fdf9a60065b | 100 | } else if (prescaler >= 5) { |
kadonotakashi | 0:8fdf9a60065b | 101 | prescaler = 5; |
kadonotakashi | 0:8fdf9a60065b | 102 | } |
kadonotakashi | 0:8fdf9a60065b | 103 | |
kadonotakashi | 0:8fdf9a60065b | 104 | config_tc.clock_prescaler = (enum tc_clock_prescaler)TC_CTRLA_PRESCALER(prescaler); |
kadonotakashi | 0:8fdf9a60065b | 105 | config_tc.counter_size = TC_COUNTER_SIZE_32BIT; |
kadonotakashi | 0:8fdf9a60065b | 106 | config_tc.run_in_standby = true; |
kadonotakashi | 0:8fdf9a60065b | 107 | config_tc.counter_32_bit.value = 0; |
kadonotakashi | 0:8fdf9a60065b | 108 | config_tc.counter_32_bit.compare_capture_channel[TC_COMPARE_CAPTURE_CHANNEL_0] = 0xFFFFFFFF; |
kadonotakashi | 0:8fdf9a60065b | 109 | |
kadonotakashi | 0:8fdf9a60065b | 110 | /* Initialize the timer */ |
kadonotakashi | 0:8fdf9a60065b | 111 | tc_init(&us_ticker_module, TICKER_COUNTER_uS, &config_tc); |
kadonotakashi | 0:8fdf9a60065b | 112 | |
kadonotakashi | 0:8fdf9a60065b | 113 | /* Register callback function */ |
kadonotakashi | 0:8fdf9a60065b | 114 | tc_register_callback(&us_ticker_module, (tc_callback_t)us_ticker_irq_handler_internal, TC_CALLBACK_CC_CHANNEL0); |
kadonotakashi | 0:8fdf9a60065b | 115 | |
kadonotakashi | 0:8fdf9a60065b | 116 | /* Enable the timer module */ |
kadonotakashi | 0:8fdf9a60065b | 117 | tc_enable(&us_ticker_module); |
kadonotakashi | 0:8fdf9a60065b | 118 | } |
kadonotakashi | 0:8fdf9a60065b | 119 | |
kadonotakashi | 0:8fdf9a60065b | 120 | uint32_t us_ticker_read() |
kadonotakashi | 0:8fdf9a60065b | 121 | { |
kadonotakashi | 0:8fdf9a60065b | 122 | if (!us_ticker_inited) |
kadonotakashi | 0:8fdf9a60065b | 123 | us_ticker_init(); |
kadonotakashi | 0:8fdf9a60065b | 124 | |
kadonotakashi | 0:8fdf9a60065b | 125 | return tc_get_count_value(&us_ticker_module); |
kadonotakashi | 0:8fdf9a60065b | 126 | } |
kadonotakashi | 0:8fdf9a60065b | 127 | |
kadonotakashi | 0:8fdf9a60065b | 128 | void us_ticker_set_interrupt(timestamp_t timestamp) |
kadonotakashi | 0:8fdf9a60065b | 129 | { |
kadonotakashi | 0:8fdf9a60065b | 130 | uint32_t cur_time; |
kadonotakashi | 0:8fdf9a60065b | 131 | int32_t delta; |
kadonotakashi | 0:8fdf9a60065b | 132 | |
kadonotakashi | 0:8fdf9a60065b | 133 | cur_time = us_ticker_read(); |
kadonotakashi | 0:8fdf9a60065b | 134 | delta = (int32_t)((uint32_t)timestamp - cur_time); |
kadonotakashi | 0:8fdf9a60065b | 135 | if (delta < 0) { |
kadonotakashi | 0:8fdf9a60065b | 136 | /* Event already occurred in past */ |
kadonotakashi | 0:8fdf9a60065b | 137 | us_ticker_irq_handler(); |
kadonotakashi | 0:8fdf9a60065b | 138 | return; |
kadonotakashi | 0:8fdf9a60065b | 139 | } |
kadonotakashi | 0:8fdf9a60065b | 140 | |
kadonotakashi | 0:8fdf9a60065b | 141 | NVIC_DisableIRQ(TICKER_COUNTER_IRQn); |
kadonotakashi | 0:8fdf9a60065b | 142 | NVIC_SetVector(TICKER_COUNTER_IRQn, (uint32_t)TICKER_COUNTER_Handlr); |
kadonotakashi | 0:8fdf9a60065b | 143 | |
kadonotakashi | 0:8fdf9a60065b | 144 | /* Enable the callback */ |
kadonotakashi | 0:8fdf9a60065b | 145 | tc_enable_callback(&us_ticker_module, TC_CALLBACK_CC_CHANNEL0); |
kadonotakashi | 0:8fdf9a60065b | 146 | tc_set_compare_value(&us_ticker_module, TC_COMPARE_CAPTURE_CHANNEL_0, (uint32_t)timestamp); |
kadonotakashi | 0:8fdf9a60065b | 147 | |
kadonotakashi | 0:8fdf9a60065b | 148 | NVIC_EnableIRQ(TICKER_COUNTER_IRQn); |
kadonotakashi | 0:8fdf9a60065b | 149 | } |
kadonotakashi | 0:8fdf9a60065b | 150 | |
kadonotakashi | 0:8fdf9a60065b | 151 | void us_ticker_disable_interrupt(void) |
kadonotakashi | 0:8fdf9a60065b | 152 | { |
kadonotakashi | 0:8fdf9a60065b | 153 | /* Disable the callback */ |
kadonotakashi | 0:8fdf9a60065b | 154 | tc_disable_callback(&us_ticker_module, TC_CALLBACK_CC_CHANNEL0); |
kadonotakashi | 0:8fdf9a60065b | 155 | NVIC_DisableIRQ(TICKER_COUNTER_IRQn); |
kadonotakashi | 0:8fdf9a60065b | 156 | } |
kadonotakashi | 0:8fdf9a60065b | 157 | |
kadonotakashi | 0:8fdf9a60065b | 158 | void us_ticker_clear_interrupt(void) |
kadonotakashi | 0:8fdf9a60065b | 159 | { |
kadonotakashi | 0:8fdf9a60065b | 160 | uint32_t status_flags; |
kadonotakashi | 0:8fdf9a60065b | 161 | |
kadonotakashi | 0:8fdf9a60065b | 162 | /* Clear TC channel 0 match */ |
kadonotakashi | 0:8fdf9a60065b | 163 | status_flags = TC_STATUS_CHANNEL_0_MATCH; |
kadonotakashi | 0:8fdf9a60065b | 164 | tc_clear_status(&us_ticker_module, status_flags); |
kadonotakashi | 0:8fdf9a60065b | 165 | |
kadonotakashi | 0:8fdf9a60065b | 166 | /* Clear the interrupt */ |
kadonotakashi | 0:8fdf9a60065b | 167 | tc_clear_interrupt(&us_ticker_module, TC_CALLBACK_CC_CHANNEL0); |
kadonotakashi | 0:8fdf9a60065b | 168 | NVIC_ClearPendingIRQ(TICKER_COUNTER_IRQn); |
kadonotakashi | 0:8fdf9a60065b | 169 | } |
kadonotakashi | 0:8fdf9a60065b | 170 | |
kadonotakashi | 0:8fdf9a60065b | 171 | void us_ticker_free(void) |
kadonotakashi | 0:8fdf9a60065b | 172 | { |
kadonotakashi | 0:8fdf9a60065b | 173 | |
kadonotakashi | 0:8fdf9a60065b | 174 | } |