mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins

Dependents:   IGLOO_board

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Tue Mar 20 16:56:18 2018 +0000
Revision:
182:a56a73fd2a6f
Parent:
179:b0033dcd6934
Child:
184:08ed48f1de7f
mbed-dev library. Release version 160

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 172:7d866c31b3c5 1 /* mbed Microcontroller Library
AnnaBridge 172:7d866c31b3c5 2 * Copyright (c) 2015-2016 Nuvoton
AnnaBridge 172:7d866c31b3c5 3 *
AnnaBridge 172:7d866c31b3c5 4 * Licensed under the Apache License, Version 2.0 (the "License");
AnnaBridge 172:7d866c31b3c5 5 * you may not use this file except in compliance with the License.
AnnaBridge 172:7d866c31b3c5 6 * You may obtain a copy of the License at
AnnaBridge 172:7d866c31b3c5 7 *
AnnaBridge 172:7d866c31b3c5 8 * http://www.apache.org/licenses/LICENSE-2.0
AnnaBridge 172:7d866c31b3c5 9 *
AnnaBridge 172:7d866c31b3c5 10 * Unless required by applicable law or agreed to in writing, software
AnnaBridge 172:7d866c31b3c5 11 * distributed under the License is distributed on an "AS IS" BASIS,
AnnaBridge 172:7d866c31b3c5 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AnnaBridge 172:7d866c31b3c5 13 * See the License for the specific language governing permissions and
AnnaBridge 172:7d866c31b3c5 14 * limitations under the License.
AnnaBridge 172:7d866c31b3c5 15 */
AnnaBridge 172:7d866c31b3c5 16
AnnaBridge 172:7d866c31b3c5 17 #include "us_ticker_api.h"
AnnaBridge 172:7d866c31b3c5 18 #include "sleep_api.h"
AnnaBridge 172:7d866c31b3c5 19 #include "mbed_assert.h"
AnnaBridge 172:7d866c31b3c5 20 #include "nu_modutil.h"
AnnaBridge 172:7d866c31b3c5 21 #include "nu_miscutil.h"
AnnaBridge 172:7d866c31b3c5 22
AnnaBridge 182:a56a73fd2a6f 23 /* Micro seconds per second */
AnnaBridge 182:a56a73fd2a6f 24 #define NU_US_PER_SEC 1000000
AnnaBridge 182:a56a73fd2a6f 25 /* Timer clock per us_ticker tick */
AnnaBridge 182:a56a73fd2a6f 26 #define NU_TMRCLK_PER_TICK 1
AnnaBridge 182:a56a73fd2a6f 27 /* Timer clock per second */
AnnaBridge 182:a56a73fd2a6f 28 #define NU_TMRCLK_PER_SEC (1000 * 1000)
AnnaBridge 182:a56a73fd2a6f 29 /* Timer max counter bit size */
AnnaBridge 182:a56a73fd2a6f 30 #define NU_TMR_MAXCNT_BITSIZE 24
AnnaBridge 182:a56a73fd2a6f 31 /* Timer max counter */
AnnaBridge 182:a56a73fd2a6f 32 #define NU_TMR_MAXCNT ((1 << NU_TMR_MAXCNT_BITSIZE) - 1)
AnnaBridge 172:7d866c31b3c5 33
AnnaBridge 172:7d866c31b3c5 34 static void tmr0_vec(void);
AnnaBridge 172:7d866c31b3c5 35
AnnaBridge 182:a56a73fd2a6f 36 static const struct nu_modinit_s timer0_modinit = {TIMER_0, TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_PCLK0, 0, TMR0_RST, TMR0_IRQn, (void *) tmr0_vec};
AnnaBridge 172:7d866c31b3c5 37
AnnaBridge 182:a56a73fd2a6f 38 #define TIMER_MODINIT timer0_modinit
AnnaBridge 182:a56a73fd2a6f 39
AnnaBridge 182:a56a73fd2a6f 40 static int ticker_inited = 0;
AnnaBridge 172:7d866c31b3c5 41
AnnaBridge 172:7d866c31b3c5 42 #define TMR_CMP_MIN 2
AnnaBridge 172:7d866c31b3c5 43 #define TMR_CMP_MAX 0xFFFFFFu
AnnaBridge 172:7d866c31b3c5 44
AnnaBridge 172:7d866c31b3c5 45 void us_ticker_init(void)
AnnaBridge 172:7d866c31b3c5 46 {
AnnaBridge 182:a56a73fd2a6f 47 if (ticker_inited) {
AnnaBridge 172:7d866c31b3c5 48 return;
AnnaBridge 172:7d866c31b3c5 49 }
AnnaBridge 182:a56a73fd2a6f 50 ticker_inited = 1;
AnnaBridge 172:7d866c31b3c5 51
AnnaBridge 172:7d866c31b3c5 52 // Reset IP
AnnaBridge 182:a56a73fd2a6f 53 SYS_ResetModule(TIMER_MODINIT.rsetidx);
AnnaBridge 172:7d866c31b3c5 54
AnnaBridge 172:7d866c31b3c5 55 // Select IP clock source
AnnaBridge 182:a56a73fd2a6f 56 CLK_SetModuleClock(TIMER_MODINIT.clkidx, TIMER_MODINIT.clksrc, TIMER_MODINIT.clkdiv);
AnnaBridge 182:a56a73fd2a6f 57
AnnaBridge 172:7d866c31b3c5 58 // Enable IP clock
AnnaBridge 182:a56a73fd2a6f 59 CLK_EnableModuleClock(TIMER_MODINIT.clkidx);
AnnaBridge 172:7d866c31b3c5 60
AnnaBridge 172:7d866c31b3c5 61 // Timer for normal counter
AnnaBridge 182:a56a73fd2a6f 62 uint32_t clk_timer = TIMER_GetModuleClock((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
AnnaBridge 182:a56a73fd2a6f 63 uint32_t prescale_timer = clk_timer / NU_TMRCLK_PER_SEC - 1;
AnnaBridge 182:a56a73fd2a6f 64 MBED_ASSERT((prescale_timer != (uint32_t) -1) && prescale_timer <= 127);
AnnaBridge 182:a56a73fd2a6f 65 MBED_ASSERT((clk_timer % NU_TMRCLK_PER_SEC) == 0);
AnnaBridge 182:a56a73fd2a6f 66 uint32_t cmp_timer = TMR_CMP_MAX;
AnnaBridge 182:a56a73fd2a6f 67 MBED_ASSERT(cmp_timer >= TMR_CMP_MIN && cmp_timer <= TMR_CMP_MAX);
AnnaBridge 172:7d866c31b3c5 68 // NOTE: TIMER_CTL_CNTDATEN_Msk exists in NUC472, but not in M451/M480. In M451/M480, TIMER_CNT is updated continuously by default.
AnnaBridge 182:a56a73fd2a6f 69 ((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname))->CTL = TIMER_CONTINUOUS_MODE | prescale_timer/* | TIMER_CTL_CNTDATEN_Msk*/;
AnnaBridge 182:a56a73fd2a6f 70 ((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname))->CMP = cmp_timer;
AnnaBridge 172:7d866c31b3c5 71
AnnaBridge 182:a56a73fd2a6f 72 NVIC_SetVector(TIMER_MODINIT.irq_n, (uint32_t) TIMER_MODINIT.var);
AnnaBridge 172:7d866c31b3c5 73
AnnaBridge 182:a56a73fd2a6f 74 NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
AnnaBridge 172:7d866c31b3c5 75
AnnaBridge 182:a56a73fd2a6f 76 TIMER_EnableInt((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
AnnaBridge 182:a56a73fd2a6f 77 TIMER_Start((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
AnnaBridge 172:7d866c31b3c5 78 }
AnnaBridge 172:7d866c31b3c5 79
AnnaBridge 172:7d866c31b3c5 80 uint32_t us_ticker_read()
AnnaBridge 172:7d866c31b3c5 81 {
AnnaBridge 182:a56a73fd2a6f 82 if (! ticker_inited) {
AnnaBridge 172:7d866c31b3c5 83 us_ticker_init();
AnnaBridge 172:7d866c31b3c5 84 }
AnnaBridge 172:7d866c31b3c5 85
AnnaBridge 182:a56a73fd2a6f 86 TIMER_T * timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);
AnnaBridge 172:7d866c31b3c5 87
AnnaBridge 182:a56a73fd2a6f 88 return (TIMER_GetCounter(timer_base) / NU_TMRCLK_PER_TICK);
AnnaBridge 182:a56a73fd2a6f 89 }
AnnaBridge 172:7d866c31b3c5 90
AnnaBridge 182:a56a73fd2a6f 91 void us_ticker_set_interrupt(timestamp_t timestamp)
AnnaBridge 182:a56a73fd2a6f 92 {
AnnaBridge 182:a56a73fd2a6f 93 /* In continuous mode, counter will be reset to zero with the following sequence:
AnnaBridge 182:a56a73fd2a6f 94 * 1. Stop counting
AnnaBridge 182:a56a73fd2a6f 95 * 2. Configure new CMP value
AnnaBridge 182:a56a73fd2a6f 96 * 3. Restart counting
AnnaBridge 182:a56a73fd2a6f 97 *
AnnaBridge 182:a56a73fd2a6f 98 * This behavior is not what we want. To fix it, we could configure new CMP value
AnnaBridge 182:a56a73fd2a6f 99 * without stopping counting first.
AnnaBridge 182:a56a73fd2a6f 100 */
AnnaBridge 182:a56a73fd2a6f 101 TIMER_T * timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);
AnnaBridge 172:7d866c31b3c5 102
AnnaBridge 182:a56a73fd2a6f 103 /* NOTE: Because H/W timer requests min compare value, our implementation would have alarm delay of
AnnaBridge 182:a56a73fd2a6f 104 * (TMR_CMP_MIN - interval_clk) clocks when interval_clk is between [1, TMR_CMP_MIN). */
AnnaBridge 182:a56a73fd2a6f 105 uint32_t cmp_timer = timestamp * NU_TMRCLK_PER_TICK;
AnnaBridge 182:a56a73fd2a6f 106 cmp_timer = NU_CLAMP(cmp_timer, TMR_CMP_MIN, TMR_CMP_MAX);
AnnaBridge 182:a56a73fd2a6f 107 timer_base->CMP = cmp_timer;
AnnaBridge 172:7d866c31b3c5 108 }
AnnaBridge 172:7d866c31b3c5 109
AnnaBridge 172:7d866c31b3c5 110 void us_ticker_disable_interrupt(void)
AnnaBridge 172:7d866c31b3c5 111 {
AnnaBridge 182:a56a73fd2a6f 112 TIMER_DisableInt((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
AnnaBridge 172:7d866c31b3c5 113 }
AnnaBridge 172:7d866c31b3c5 114
AnnaBridge 172:7d866c31b3c5 115 void us_ticker_clear_interrupt(void)
AnnaBridge 172:7d866c31b3c5 116 {
AnnaBridge 182:a56a73fd2a6f 117 TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
AnnaBridge 172:7d866c31b3c5 118 }
AnnaBridge 172:7d866c31b3c5 119
AnnaBridge 172:7d866c31b3c5 120 void us_ticker_fire_interrupt(void)
AnnaBridge 172:7d866c31b3c5 121 {
AnnaBridge 172:7d866c31b3c5 122 // NOTE: This event was in the past. Set the interrupt as pending, but don't process it here.
AnnaBridge 172:7d866c31b3c5 123 // This prevents a recursive loop under heavy load which can lead to a stack overflow.
AnnaBridge 182:a56a73fd2a6f 124 NVIC_SetPendingIRQ(TIMER_MODINIT.irq_n);
AnnaBridge 182:a56a73fd2a6f 125 }
AnnaBridge 182:a56a73fd2a6f 126
AnnaBridge 182:a56a73fd2a6f 127 const ticker_info_t* us_ticker_get_info()
AnnaBridge 182:a56a73fd2a6f 128 {
AnnaBridge 182:a56a73fd2a6f 129 static const ticker_info_t info = {
AnnaBridge 182:a56a73fd2a6f 130 NU_TMRCLK_PER_SEC / NU_TMRCLK_PER_TICK,
AnnaBridge 182:a56a73fd2a6f 131 NU_TMR_MAXCNT_BITSIZE
AnnaBridge 182:a56a73fd2a6f 132 };
AnnaBridge 182:a56a73fd2a6f 133 return &info;
AnnaBridge 172:7d866c31b3c5 134 }
AnnaBridge 172:7d866c31b3c5 135
AnnaBridge 172:7d866c31b3c5 136 static void tmr0_vec(void)
AnnaBridge 172:7d866c31b3c5 137 {
AnnaBridge 182:a56a73fd2a6f 138 TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
AnnaBridge 182:a56a73fd2a6f 139
AnnaBridge 182:a56a73fd2a6f 140 // NOTE: us_ticker_set_interrupt() may get called in us_ticker_irq_handler();
AnnaBridge 182:a56a73fd2a6f 141 us_ticker_irq_handler();
AnnaBridge 172:7d866c31b3c5 142 }