Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of HardwareTimersLib by
Timer_TPM.cpp@1:7dde0e4d30da, 2014-03-09 (annotated)
- Committer:
- mgottscho
- Date:
- Sun Mar 09 01:09:56 2014 +0000
- Revision:
- 1:7dde0e4d30da
- Child:
- 2:5056ec8c52e8
Refactored code so that there can be different timer/frequency combinations. For example, Timer_48MHz now derives from Timer_TPM, which derives from HardwareTimer.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mgottscho | 1:7dde0e4d30da | 1 | /* Timer_TPM.cpp |
mgottscho | 1:7dde0e4d30da | 2 | * Tested with mbed board: FRDM-KL46Z |
mgottscho | 1:7dde0e4d30da | 3 | * Author: Mark Gottscho |
mgottscho | 1:7dde0e4d30da | 4 | * mgottscho@ucla.edu |
mgottscho | 1:7dde0e4d30da | 5 | */ |
mgottscho | 1:7dde0e4d30da | 6 | |
mgottscho | 1:7dde0e4d30da | 7 | #include "mbed.h" |
mgottscho | 1:7dde0e4d30da | 8 | #include "HardwareTimer.h" |
mgottscho | 1:7dde0e4d30da | 9 | #include "Timer_TPM.h" |
mgottscho | 1:7dde0e4d30da | 10 | |
mgottscho | 1:7dde0e4d30da | 11 | //Init class static variable |
mgottscho | 1:7dde0e4d30da | 12 | bool Timer_TPM::__initialized_tpm = false; |
mgottscho | 1:7dde0e4d30da | 13 | uint16_t Timer_TPM::__rolloverValue = 0xFFFF; |
mgottscho | 1:7dde0e4d30da | 14 | uint32_t Timer_TPM::__count = 0; |
mgottscho | 1:7dde0e4d30da | 15 | |
mgottscho | 1:7dde0e4d30da | 16 | |
mgottscho | 1:7dde0e4d30da | 17 | Timer_TPM::Timer_TPM() : |
mgottscho | 1:7dde0e4d30da | 18 | HardwareTimer(20.8333333333) //20.833... ns per tick, roll over at maximum possible value to reduce interrupt frequency |
mgottscho | 1:7dde0e4d30da | 19 | { |
mgottscho | 1:7dde0e4d30da | 20 | } |
mgottscho | 1:7dde0e4d30da | 21 | |
mgottscho | 1:7dde0e4d30da | 22 | Timer_TPM::~Timer_TPM() {} |
mgottscho | 1:7dde0e4d30da | 23 | |
mgottscho | 1:7dde0e4d30da | 24 | void Timer_TPM::disableTimer() { |
mgottscho | 1:7dde0e4d30da | 25 | if (!__valid) |
mgottscho | 1:7dde0e4d30da | 26 | return; |
mgottscho | 1:7dde0e4d30da | 27 | |
mgottscho | 1:7dde0e4d30da | 28 | TPM0->SC = 0; //Reset TPM |
mgottscho | 1:7dde0e4d30da | 29 | __enabled = false; |
mgottscho | 1:7dde0e4d30da | 30 | } |
mgottscho | 1:7dde0e4d30da | 31 | |
mgottscho | 1:7dde0e4d30da | 32 | uint32_t Timer_TPM::getTick() { |
mgottscho | 1:7dde0e4d30da | 33 | if (!__valid) |
mgottscho | 1:7dde0e4d30da | 34 | return 0; |
mgottscho | 1:7dde0e4d30da | 35 | |
mgottscho | 1:7dde0e4d30da | 36 | //Get raw time |
mgottscho | 1:7dde0e4d30da | 37 | __disable_irq(); |
mgottscho | 1:7dde0e4d30da | 38 | TPM0->CNT = 0; //Need to write to the TPM CNT register in order to read it (buffering) |
mgottscho | 1:7dde0e4d30da | 39 | uint16_t tick = TPM0->CNT; |
mgottscho | 1:7dde0e4d30da | 40 | uint32_t count = __count; |
mgottscho | 1:7dde0e4d30da | 41 | __enable_irq(); |
mgottscho | 1:7dde0e4d30da | 42 | |
mgottscho | 1:7dde0e4d30da | 43 | //Convert to ticks |
mgottscho | 1:7dde0e4d30da | 44 | return (uint32_t) count * __rolloverValue + tick; |
mgottscho | 1:7dde0e4d30da | 45 | } |
mgottscho | 1:7dde0e4d30da | 46 | |
mgottscho | 1:7dde0e4d30da | 47 | void Timer_TPM::__set_tpm(uint16_t count) { |
mgottscho | 1:7dde0e4d30da | 48 | //TPM0->SC &= ~TPM_SC_CMOD_MASK; //Stop clock |
mgottscho | 1:7dde0e4d30da | 49 | TPM0->CNT = 0; //Set the count register |
mgottscho | 1:7dde0e4d30da | 50 | TPM0->MOD = count; //Set the modulo register |
mgottscho | 1:7dde0e4d30da | 51 | TPM0->SC |= TPM_SC_TOIE_MASK; //Enable interrupt |
mgottscho | 1:7dde0e4d30da | 52 | TPM0->SC |= TPM_SC_CMOD(1); //Start the timer. Timer will increment on the TPM clock edges, not an external clock |
mgottscho | 1:7dde0e4d30da | 53 | } |
mgottscho | 1:7dde0e4d30da | 54 | |
mgottscho | 1:7dde0e4d30da | 55 | void Timer_TPM::__isr_tpm() { |
mgottscho | 1:7dde0e4d30da | 56 | TPM0->SC |= TPM_SC_TOF_MASK; |
mgottscho | 1:7dde0e4d30da | 57 | __set_tpm(__rolloverValue); |
mgottscho | 1:7dde0e4d30da | 58 | __count++; |
mgottscho | 1:7dde0e4d30da | 59 | } |