mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins
Fork of mbed-dev by
targets/TARGET_NXP/TARGET_LPC82X/us_ticker.c@188:3f10722804f9, 2018-07-30 (annotated)
- Committer:
- ua1arn
- Date:
- Mon Jul 30 12:31:10 2018 +0000
- Revision:
- 188:3f10722804f9
- Parent:
- 174:b96e65c34a4d
before add multi-configuration USB descriptors
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 144:ef7eb2e8f9f7 | 1 | /* mbed Microcontroller Library |
<> | 144:ef7eb2e8f9f7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
<> | 144:ef7eb2e8f9f7 | 3 | * |
<> | 144:ef7eb2e8f9f7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 144:ef7eb2e8f9f7 | 5 | * you may not use this file except in compliance with the License. |
<> | 144:ef7eb2e8f9f7 | 6 | * You may obtain a copy of the License at |
<> | 144:ef7eb2e8f9f7 | 7 | * |
<> | 144:ef7eb2e8f9f7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 144:ef7eb2e8f9f7 | 9 | * |
<> | 144:ef7eb2e8f9f7 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 144:ef7eb2e8f9f7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 144:ef7eb2e8f9f7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 144:ef7eb2e8f9f7 | 13 | * See the License for the specific language governing permissions and |
<> | 144:ef7eb2e8f9f7 | 14 | * limitations under the License. |
<> | 144:ef7eb2e8f9f7 | 15 | */ |
<> | 144:ef7eb2e8f9f7 | 16 | #include <stddef.h> |
<> | 144:ef7eb2e8f9f7 | 17 | #include "us_ticker_api.h" |
<> | 144:ef7eb2e8f9f7 | 18 | #include "PeripheralNames.h" |
<> | 144:ef7eb2e8f9f7 | 19 | |
<> | 144:ef7eb2e8f9f7 | 20 | static int us_ticker_inited = 0; |
<> | 144:ef7eb2e8f9f7 | 21 | int MRT_Clock_MHz; |
<> | 144:ef7eb2e8f9f7 | 22 | unsigned int ticker_fullcount_us; |
<> | 144:ef7eb2e8f9f7 | 23 | unsigned long int ticker_expired_count_us = 0; |
<> | 144:ef7eb2e8f9f7 | 24 | |
<> | 144:ef7eb2e8f9f7 | 25 | #define US_TICKER_TIMER_IRQn MRT_IRQn |
<> | 144:ef7eb2e8f9f7 | 26 | |
<> | 144:ef7eb2e8f9f7 | 27 | void us_ticker_init(void) { |
<> | 144:ef7eb2e8f9f7 | 28 | |
<> | 144:ef7eb2e8f9f7 | 29 | if (us_ticker_inited) |
<> | 144:ef7eb2e8f9f7 | 30 | return; |
<> | 144:ef7eb2e8f9f7 | 31 | |
<> | 144:ef7eb2e8f9f7 | 32 | us_ticker_inited = 1; |
<> | 144:ef7eb2e8f9f7 | 33 | |
<> | 144:ef7eb2e8f9f7 | 34 | // Calculate MRT clock value (MRT has no prescaler) |
<> | 144:ef7eb2e8f9f7 | 35 | MRT_Clock_MHz = (SystemCoreClock / 1000000); |
<> | 144:ef7eb2e8f9f7 | 36 | // Calculate fullcounter value in us (MRT has 31 bits and clock is 30MHz) |
<> | 144:ef7eb2e8f9f7 | 37 | ticker_fullcount_us = 0x80000000UL/MRT_Clock_MHz; |
<> | 144:ef7eb2e8f9f7 | 38 | |
<> | 144:ef7eb2e8f9f7 | 39 | // Enable the MRT clock |
<> | 144:ef7eb2e8f9f7 | 40 | LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 10); |
<> | 144:ef7eb2e8f9f7 | 41 | |
<> | 144:ef7eb2e8f9f7 | 42 | // Clear peripheral reset the MRT |
<> | 144:ef7eb2e8f9f7 | 43 | LPC_SYSCON->PRESETCTRL |= (1 << 7); |
<> | 144:ef7eb2e8f9f7 | 44 | |
<> | 144:ef7eb2e8f9f7 | 45 | // Force load interval value (Bit 0-30 is interval value, Bit 31 is Force Load bit) |
<> | 144:ef7eb2e8f9f7 | 46 | LPC_MRT->INTVAL0 = 0xFFFFFFFFUL; |
<> | 144:ef7eb2e8f9f7 | 47 | // Enable Ch0 interrupt, Mode 0 is Repeat Interrupt |
<> | 144:ef7eb2e8f9f7 | 48 | LPC_MRT->CTRL0 = (0x0 << 1) | (0x1 << 0); |
<> | 144:ef7eb2e8f9f7 | 49 | |
<> | 144:ef7eb2e8f9f7 | 50 | // Force load interval value (Bit 0-30 is interval value, Bit 31 is Force Load bit) |
<> | 144:ef7eb2e8f9f7 | 51 | LPC_MRT->INTVAL1 = 0x80000000UL; |
<> | 144:ef7eb2e8f9f7 | 52 | // Disable ch1 interrupt, Mode 0 is Repeat Interrupt |
<> | 144:ef7eb2e8f9f7 | 53 | LPC_MRT->CTRL1 = (0x0 << 1) | (0x0 << 0); |
<> | 144:ef7eb2e8f9f7 | 54 | |
<> | 144:ef7eb2e8f9f7 | 55 | // Set MRT interrupt vector |
<> | 144:ef7eb2e8f9f7 | 56 | NVIC_SetVector(US_TICKER_TIMER_IRQn, (uint32_t)us_ticker_irq_handler); |
<> | 144:ef7eb2e8f9f7 | 57 | NVIC_EnableIRQ(US_TICKER_TIMER_IRQn); |
<> | 144:ef7eb2e8f9f7 | 58 | } |
<> | 144:ef7eb2e8f9f7 | 59 | |
<> | 144:ef7eb2e8f9f7 | 60 | //TIMER0 is used for us ticker and timers (Timer, wait(), wait_us() etc) |
<> | 144:ef7eb2e8f9f7 | 61 | uint32_t us_ticker_read() { |
<> | 144:ef7eb2e8f9f7 | 62 | |
<> | 144:ef7eb2e8f9f7 | 63 | if (!us_ticker_inited) |
<> | 144:ef7eb2e8f9f7 | 64 | us_ticker_init(); |
<> | 144:ef7eb2e8f9f7 | 65 | |
<> | 144:ef7eb2e8f9f7 | 66 | // Generate ticker value |
<> | 144:ef7eb2e8f9f7 | 67 | // MRT source clock is SystemCoreClock (30MHz) and MRT is a 31-bit countdown timer |
<> | 144:ef7eb2e8f9f7 | 68 | // Calculate expected value using number of expired times to mimic a 32bit timer @ 1 MHz |
<> | 144:ef7eb2e8f9f7 | 69 | return (0x7FFFFFFFUL - LPC_MRT->TIMER0)/MRT_Clock_MHz + ticker_expired_count_us; |
<> | 144:ef7eb2e8f9f7 | 70 | } |
<> | 144:ef7eb2e8f9f7 | 71 | |
<> | 144:ef7eb2e8f9f7 | 72 | //TIMER1 is used for Timestamped interrupts (Ticker(), Timeout()) |
<> | 144:ef7eb2e8f9f7 | 73 | void us_ticker_set_interrupt(timestamp_t timestamp) { |
<> | 144:ef7eb2e8f9f7 | 74 | |
<> | 144:ef7eb2e8f9f7 | 75 | // MRT source clock is SystemCoreClock (30MHz) and MRT is a 31-bit countdown timer |
<> | 144:ef7eb2e8f9f7 | 76 | // Force load interval value (Bit 0-30 is interval value, Bit 31 is Force Load bit) |
<> | 144:ef7eb2e8f9f7 | 77 | // Note: The MRT has less counter headroom available than the typical mbed 32bit timer @ 1 MHz. |
<> | 144:ef7eb2e8f9f7 | 78 | // The calculated counter interval until the next timestamp will be truncated and an |
<> | 144:ef7eb2e8f9f7 | 79 | // 'early' interrupt will be generated in case the max required count interval exceeds |
<> | 144:ef7eb2e8f9f7 | 80 | // the available 31 bits space. However, the mbed us_ticker interrupt handler will |
<> | 144:ef7eb2e8f9f7 | 81 | // check current time against the next scheduled timestamp and simply re-issue the |
<> | 144:ef7eb2e8f9f7 | 82 | // same interrupt again when needed. The calculated counter interval will now be smaller. |
<> | 144:ef7eb2e8f9f7 | 83 | LPC_MRT->INTVAL1 = (((timestamp - us_ticker_read()) * MRT_Clock_MHz) | 0x80000000UL); |
<> | 144:ef7eb2e8f9f7 | 84 | |
<> | 144:ef7eb2e8f9f7 | 85 | // Enable interrupt |
<> | 144:ef7eb2e8f9f7 | 86 | LPC_MRT->CTRL1 |= 1; |
<> | 144:ef7eb2e8f9f7 | 87 | } |
<> | 144:ef7eb2e8f9f7 | 88 | |
AnnaBridge | 174:b96e65c34a4d | 89 | void us_ticker_fire_interrupt(void) |
AnnaBridge | 174:b96e65c34a4d | 90 | { |
AnnaBridge | 174:b96e65c34a4d | 91 | NVIC_SetPendingIRQ(US_TICKER_TIMER_IRQn); |
AnnaBridge | 174:b96e65c34a4d | 92 | } |
AnnaBridge | 174:b96e65c34a4d | 93 | |
<> | 144:ef7eb2e8f9f7 | 94 | //Disable Timestamped interrupts triggered by TIMER1 |
<> | 144:ef7eb2e8f9f7 | 95 | void us_ticker_disable_interrupt() { |
<> | 144:ef7eb2e8f9f7 | 96 | //Timer1 for Timestamped interrupts (31 bits downcounter @ SystemCoreClock) |
<> | 144:ef7eb2e8f9f7 | 97 | LPC_MRT->CTRL1 &= ~1; |
<> | 144:ef7eb2e8f9f7 | 98 | } |
<> | 144:ef7eb2e8f9f7 | 99 | |
<> | 144:ef7eb2e8f9f7 | 100 | void us_ticker_clear_interrupt() { |
<> | 144:ef7eb2e8f9f7 | 101 | |
<> | 144:ef7eb2e8f9f7 | 102 | //Timer1 for Timestamped interrupts (31 bits downcounter @ SystemCoreClock) |
<> | 144:ef7eb2e8f9f7 | 103 | if (LPC_MRT->STAT1 & 1) |
<> | 144:ef7eb2e8f9f7 | 104 | LPC_MRT->STAT1 = 1; |
<> | 144:ef7eb2e8f9f7 | 105 | |
<> | 144:ef7eb2e8f9f7 | 106 | //Timer0 for us counter (31 bits downcounter @ SystemCoreClock) |
<> | 144:ef7eb2e8f9f7 | 107 | if (LPC_MRT->STAT0 & 1) { |
<> | 144:ef7eb2e8f9f7 | 108 | LPC_MRT->STAT0 = 1; |
<> | 144:ef7eb2e8f9f7 | 109 | ticker_expired_count_us += ticker_fullcount_us; |
<> | 144:ef7eb2e8f9f7 | 110 | } |
<> | 144:ef7eb2e8f9f7 | 111 | } |