takashi kadono
/
Nucleo_446
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/targets/TARGET_NUVOTON/nu_timer.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) 2015-2016 Nuvoton |
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 | |
kadonotakashi | 0:8fdf9a60065b | 17 | #include "nu_timer.h" |
kadonotakashi | 0:8fdf9a60065b | 18 | #include "mbed_power_mgmt.h" |
kadonotakashi | 0:8fdf9a60065b | 19 | #include "mbed_critical.h" |
kadonotakashi | 0:8fdf9a60065b | 20 | #include "us_ticker_api.h" |
kadonotakashi | 0:8fdf9a60065b | 21 | #include "mbed_assert.h" |
kadonotakashi | 0:8fdf9a60065b | 22 | |
kadonotakashi | 0:8fdf9a60065b | 23 | void nu_countdown_init(struct nu_countdown_ctx_s *ctx, us_timestamp_t interval_us) |
kadonotakashi | 0:8fdf9a60065b | 24 | { |
kadonotakashi | 0:8fdf9a60065b | 25 | core_util_critical_section_enter(); |
kadonotakashi | 0:8fdf9a60065b | 26 | sleep_manager_lock_deep_sleep(); |
kadonotakashi | 0:8fdf9a60065b | 27 | ctx->_ticker_data = get_us_ticker_data(); |
kadonotakashi | 0:8fdf9a60065b | 28 | ctx->_interval_end_us = ticker_read_us(ctx->_ticker_data) + interval_us; |
kadonotakashi | 0:8fdf9a60065b | 29 | ctx->_expired = false; |
kadonotakashi | 0:8fdf9a60065b | 30 | core_util_critical_section_exit(); |
kadonotakashi | 0:8fdf9a60065b | 31 | } |
kadonotakashi | 0:8fdf9a60065b | 32 | |
kadonotakashi | 0:8fdf9a60065b | 33 | bool nu_countdown_expired(struct nu_countdown_ctx_s *ctx) |
kadonotakashi | 0:8fdf9a60065b | 34 | { |
kadonotakashi | 0:8fdf9a60065b | 35 | core_util_critical_section_enter(); |
kadonotakashi | 0:8fdf9a60065b | 36 | if (! ctx->_expired) { |
kadonotakashi | 0:8fdf9a60065b | 37 | ctx->_expired = ticker_read_us(ctx->_ticker_data) >= ctx->_interval_end_us; |
kadonotakashi | 0:8fdf9a60065b | 38 | } |
kadonotakashi | 0:8fdf9a60065b | 39 | core_util_critical_section_exit(); |
kadonotakashi | 0:8fdf9a60065b | 40 | |
kadonotakashi | 0:8fdf9a60065b | 41 | return ctx->_expired; |
kadonotakashi | 0:8fdf9a60065b | 42 | } |
kadonotakashi | 0:8fdf9a60065b | 43 | |
kadonotakashi | 0:8fdf9a60065b | 44 | void nu_countdown_free(struct nu_countdown_ctx_s *ctx) |
kadonotakashi | 0:8fdf9a60065b | 45 | { |
kadonotakashi | 0:8fdf9a60065b | 46 | core_util_critical_section_enter(); |
kadonotakashi | 0:8fdf9a60065b | 47 | sleep_manager_unlock_deep_sleep(); |
kadonotakashi | 0:8fdf9a60065b | 48 | core_util_critical_section_exit(); |
kadonotakashi | 0:8fdf9a60065b | 49 | } |
kadonotakashi | 0:8fdf9a60065b | 50 | |
kadonotakashi | 0:8fdf9a60065b | 51 | |
kadonotakashi | 0:8fdf9a60065b | 52 | void nu_busy_wait_us(uint32_t us) |
kadonotakashi | 0:8fdf9a60065b | 53 | { |
kadonotakashi | 0:8fdf9a60065b | 54 | const uint32_t bits = us_ticker_get_info()->bits; |
kadonotakashi | 0:8fdf9a60065b | 55 | const uint32_t mask = (1 << bits) - 1; |
kadonotakashi | 0:8fdf9a60065b | 56 | MBED_ASSERT(us_ticker_get_info()->frequency == 1000000); |
kadonotakashi | 0:8fdf9a60065b | 57 | uint32_t prev = us_ticker_read(); |
kadonotakashi | 0:8fdf9a60065b | 58 | while (1) { |
kadonotakashi | 0:8fdf9a60065b | 59 | const uint32_t cur = us_ticker_read(); |
kadonotakashi | 0:8fdf9a60065b | 60 | const uint32_t elapsed = (cur - prev) & mask; |
kadonotakashi | 0:8fdf9a60065b | 61 | if (elapsed > us) { |
kadonotakashi | 0:8fdf9a60065b | 62 | break; |
kadonotakashi | 0:8fdf9a60065b | 63 | } |
kadonotakashi | 0:8fdf9a60065b | 64 | us -= elapsed; |
kadonotakashi | 0:8fdf9a60065b | 65 | prev = cur; |
kadonotakashi | 0:8fdf9a60065b | 66 | } |
kadonotakashi | 0:8fdf9a60065b | 67 | } |
kadonotakashi | 0:8fdf9a60065b | 68 | |
kadonotakashi | 0:8fdf9a60065b | 69 | /* Delay 4 cycles per round by hand-counting instruction cycles |
kadonotakashi | 0:8fdf9a60065b | 70 | * |
kadonotakashi | 0:8fdf9a60065b | 71 | * The delay function here is implemented by just hand-counting instruction cycles rather than preferred |
kadonotakashi | 0:8fdf9a60065b | 72 | * H/W timer since it is to use in cases where H/W timer is not available. Usually, it can delay at least |
kadonotakashi | 0:8fdf9a60065b | 73 | * 4-cycles per round. |
kadonotakashi | 0:8fdf9a60065b | 74 | * |
kadonotakashi | 0:8fdf9a60065b | 75 | * In modern pipeline core, plus flash performance and other factors, we cannot rely accurately on hand- |
kadonotakashi | 0:8fdf9a60065b | 76 | * counting instruction cycles for expected delay cycles. |
kadonotakashi | 0:8fdf9a60065b | 77 | */ |
kadonotakashi | 0:8fdf9a60065b | 78 | #if defined(__CC_ARM) |
kadonotakashi | 0:8fdf9a60065b | 79 | MBED_NOINLINE |
kadonotakashi | 0:8fdf9a60065b | 80 | __asm void nu_delay_cycle_x4(uint32_t rounds) |
kadonotakashi | 0:8fdf9a60065b | 81 | { |
kadonotakashi | 0:8fdf9a60065b | 82 | // AStyle should not format inline assembly |
kadonotakashi | 0:8fdf9a60065b | 83 | // *INDENT-OFF* |
kadonotakashi | 0:8fdf9a60065b | 84 | 1 |
kadonotakashi | 0:8fdf9a60065b | 85 | #if !defined(__CORTEX_M0) |
kadonotakashi | 0:8fdf9a60065b | 86 | NOP // 1 cycle |
kadonotakashi | 0:8fdf9a60065b | 87 | #endif |
kadonotakashi | 0:8fdf9a60065b | 88 | SUBS a1, a1, #1 // 1 cycle |
kadonotakashi | 0:8fdf9a60065b | 89 | BCS %BT1 // 3 cycles(M0)/2 cycles(non-M0) |
kadonotakashi | 0:8fdf9a60065b | 90 | BX lr |
kadonotakashi | 0:8fdf9a60065b | 91 | // *INDENT-ON* |
kadonotakashi | 0:8fdf9a60065b | 92 | } |
kadonotakashi | 0:8fdf9a60065b | 93 | #elif defined (__ICCARM__) |
kadonotakashi | 0:8fdf9a60065b | 94 | MBED_NOINLINE |
kadonotakashi | 0:8fdf9a60065b | 95 | void nu_delay_cycle_x4(uint32_t rounds) |
kadonotakashi | 0:8fdf9a60065b | 96 | { |
kadonotakashi | 0:8fdf9a60065b | 97 | __asm volatile( |
kadonotakashi | 0:8fdf9a60065b | 98 | "loop: \n" |
kadonotakashi | 0:8fdf9a60065b | 99 | #if !defined(__CORTEX_M0) |
kadonotakashi | 0:8fdf9a60065b | 100 | " NOP \n" // 1 cycle |
kadonotakashi | 0:8fdf9a60065b | 101 | #endif |
kadonotakashi | 0:8fdf9a60065b | 102 | " SUBS %0, %0, #1 \n" // 1 cycle |
kadonotakashi | 0:8fdf9a60065b | 103 | " BCS.n loop\n" // 3 cycles(M0)/2 cycles(non-M0) |
kadonotakashi | 0:8fdf9a60065b | 104 | : "+r"(rounds) |
kadonotakashi | 0:8fdf9a60065b | 105 | : |
kadonotakashi | 0:8fdf9a60065b | 106 | : "cc" |
kadonotakashi | 0:8fdf9a60065b | 107 | ); |
kadonotakashi | 0:8fdf9a60065b | 108 | } |
kadonotakashi | 0:8fdf9a60065b | 109 | #elif defined ( __GNUC__ ) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)) |
kadonotakashi | 0:8fdf9a60065b | 110 | MBED_NOINLINE |
kadonotakashi | 0:8fdf9a60065b | 111 | void nu_delay_cycle_x4(uint32_t rounds) |
kadonotakashi | 0:8fdf9a60065b | 112 | { |
kadonotakashi | 0:8fdf9a60065b | 113 | __asm__ volatile( |
kadonotakashi | 0:8fdf9a60065b | 114 | "%=:\n\t" |
kadonotakashi | 0:8fdf9a60065b | 115 | #if !defined(__CORTEX_M0) |
kadonotakashi | 0:8fdf9a60065b | 116 | "NOP\n\t" // 1 cycle |
kadonotakashi | 0:8fdf9a60065b | 117 | #endif |
kadonotakashi | 0:8fdf9a60065b | 118 | #if defined(__thumb__) && !defined(__thumb2__) && !defined(__ARMCC_VERSION) |
kadonotakashi | 0:8fdf9a60065b | 119 | "SUB %0, #1\n\t" // 1 cycle |
kadonotakashi | 0:8fdf9a60065b | 120 | #else |
kadonotakashi | 0:8fdf9a60065b | 121 | "SUBS %0, %0, #1\n\t" // 1 cycle |
kadonotakashi | 0:8fdf9a60065b | 122 | #endif |
kadonotakashi | 0:8fdf9a60065b | 123 | "BCS %=b\n\t" // 3 cycles(M0)/2 cycles(non-M0) |
kadonotakashi | 0:8fdf9a60065b | 124 | : "+l"(rounds) |
kadonotakashi | 0:8fdf9a60065b | 125 | : |
kadonotakashi | 0:8fdf9a60065b | 126 | : "cc" |
kadonotakashi | 0:8fdf9a60065b | 127 | ); |
kadonotakashi | 0:8fdf9a60065b | 128 | } |
kadonotakashi | 0:8fdf9a60065b | 129 | #endif |