added prescaler for 16 bit pwm in LPC1347 target

Fork of mbed-dev by mbed official

Committer:
JojoS
Date:
Sat Sep 10 15:32:04 2016 +0000
Revision:
147:ba84b7dc41a7
Parent:
144:ef7eb2e8f9f7
added prescaler for 16 bit timers (solution as in LPC11xx), default prescaler 31 for max 28 ms period time

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 144:ef7eb2e8f9f7 1 /*
<> 144:ef7eb2e8f9f7 2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
<> 144:ef7eb2e8f9f7 3 * All rights reserved.
<> 144:ef7eb2e8f9f7 4 *
<> 144:ef7eb2e8f9f7 5 * Redistribution and use in source and binary forms, with or without modification,
<> 144:ef7eb2e8f9f7 6 * are permitted provided that the following conditions are met:
<> 144:ef7eb2e8f9f7 7 *
<> 144:ef7eb2e8f9f7 8 * o Redistributions of source code must retain the above copyright notice, this list
<> 144:ef7eb2e8f9f7 9 * of conditions and the following disclaimer.
<> 144:ef7eb2e8f9f7 10 *
<> 144:ef7eb2e8f9f7 11 * o Redistributions in binary form must reproduce the above copyright notice, this
<> 144:ef7eb2e8f9f7 12 * list of conditions and the following disclaimer in the documentation and/or
<> 144:ef7eb2e8f9f7 13 * other materials provided with the distribution.
<> 144:ef7eb2e8f9f7 14 *
<> 144:ef7eb2e8f9f7 15 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
<> 144:ef7eb2e8f9f7 16 * contributors may be used to endorse or promote products derived from this
<> 144:ef7eb2e8f9f7 17 * software without specific prior written permission.
<> 144:ef7eb2e8f9f7 18 *
<> 144:ef7eb2e8f9f7 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
<> 144:ef7eb2e8f9f7 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
<> 144:ef7eb2e8f9f7 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
<> 144:ef7eb2e8f9f7 22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
<> 144:ef7eb2e8f9f7 23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
<> 144:ef7eb2e8f9f7 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
<> 144:ef7eb2e8f9f7 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
<> 144:ef7eb2e8f9f7 26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
<> 144:ef7eb2e8f9f7 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
<> 144:ef7eb2e8f9f7 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<> 144:ef7eb2e8f9f7 29 */
<> 144:ef7eb2e8f9f7 30
<> 144:ef7eb2e8f9f7 31 #include "fsl_lptmr.h"
<> 144:ef7eb2e8f9f7 32
<> 144:ef7eb2e8f9f7 33 /*******************************************************************************
<> 144:ef7eb2e8f9f7 34 * Prototypes
<> 144:ef7eb2e8f9f7 35 ******************************************************************************/
<> 144:ef7eb2e8f9f7 36 /*!
<> 144:ef7eb2e8f9f7 37 * @brief Gets the instance from the base address to be used to gate or ungate the module clock
<> 144:ef7eb2e8f9f7 38 *
<> 144:ef7eb2e8f9f7 39 * @param base LPTMR peripheral base address
<> 144:ef7eb2e8f9f7 40 *
<> 144:ef7eb2e8f9f7 41 * @return The LPTMR instance
<> 144:ef7eb2e8f9f7 42 */
<> 144:ef7eb2e8f9f7 43 static uint32_t LPTMR_GetInstance(LPTMR_Type *base);
<> 144:ef7eb2e8f9f7 44
<> 144:ef7eb2e8f9f7 45 /*******************************************************************************
<> 144:ef7eb2e8f9f7 46 * Variables
<> 144:ef7eb2e8f9f7 47 ******************************************************************************/
<> 144:ef7eb2e8f9f7 48 /*! @brief Pointers to LPTMR bases for each instance. */
<> 144:ef7eb2e8f9f7 49 static LPTMR_Type *const s_lptmrBases[] = LPTMR_BASE_PTRS;
<> 144:ef7eb2e8f9f7 50
<> 144:ef7eb2e8f9f7 51 /*! @brief Pointers to LPTMR clocks for each instance. */
<> 144:ef7eb2e8f9f7 52 static const clock_ip_name_t s_lptmrClocks[] = LPTMR_CLOCKS;
<> 144:ef7eb2e8f9f7 53
<> 144:ef7eb2e8f9f7 54 /*******************************************************************************
<> 144:ef7eb2e8f9f7 55 * Code
<> 144:ef7eb2e8f9f7 56 ******************************************************************************/
<> 144:ef7eb2e8f9f7 57 static uint32_t LPTMR_GetInstance(LPTMR_Type *base)
<> 144:ef7eb2e8f9f7 58 {
<> 144:ef7eb2e8f9f7 59 uint32_t instance;
<> 144:ef7eb2e8f9f7 60
<> 144:ef7eb2e8f9f7 61 /* Find the instance index from base address mappings. */
<> 144:ef7eb2e8f9f7 62 for (instance = 0; instance < FSL_FEATURE_SOC_LPTMR_COUNT; instance++)
<> 144:ef7eb2e8f9f7 63 {
<> 144:ef7eb2e8f9f7 64 if (s_lptmrBases[instance] == base)
<> 144:ef7eb2e8f9f7 65 {
<> 144:ef7eb2e8f9f7 66 break;
<> 144:ef7eb2e8f9f7 67 }
<> 144:ef7eb2e8f9f7 68 }
<> 144:ef7eb2e8f9f7 69
<> 144:ef7eb2e8f9f7 70 assert(instance < FSL_FEATURE_SOC_LPTMR_COUNT);
<> 144:ef7eb2e8f9f7 71
<> 144:ef7eb2e8f9f7 72 return instance;
<> 144:ef7eb2e8f9f7 73 }
<> 144:ef7eb2e8f9f7 74
<> 144:ef7eb2e8f9f7 75 void LPTMR_Init(LPTMR_Type *base, const lptmr_config_t *config)
<> 144:ef7eb2e8f9f7 76 {
<> 144:ef7eb2e8f9f7 77 assert(config);
<> 144:ef7eb2e8f9f7 78
<> 144:ef7eb2e8f9f7 79 /* Ungate the LPTMR clock*/
<> 144:ef7eb2e8f9f7 80 CLOCK_EnableClock(s_lptmrClocks[LPTMR_GetInstance(base)]);
<> 144:ef7eb2e8f9f7 81
<> 144:ef7eb2e8f9f7 82 /* Configure the timers operation mode and input pin setup */
<> 144:ef7eb2e8f9f7 83 base->CSR = (LPTMR_CSR_TMS(config->timerMode) | LPTMR_CSR_TFC(config->enableFreeRunning) |
<> 144:ef7eb2e8f9f7 84 LPTMR_CSR_TPP(config->pinPolarity) | LPTMR_CSR_TPS(config->pinSelect));
<> 144:ef7eb2e8f9f7 85
<> 144:ef7eb2e8f9f7 86 /* Configure the prescale value and clock source */
<> 144:ef7eb2e8f9f7 87 base->PSR = (LPTMR_PSR_PRESCALE(config->value) | LPTMR_PSR_PBYP(config->bypassPrescaler) |
<> 144:ef7eb2e8f9f7 88 LPTMR_PSR_PCS(config->prescalerClockSource));
<> 144:ef7eb2e8f9f7 89 }
<> 144:ef7eb2e8f9f7 90
<> 144:ef7eb2e8f9f7 91 void LPTMR_Deinit(LPTMR_Type *base)
<> 144:ef7eb2e8f9f7 92 {
<> 144:ef7eb2e8f9f7 93 /* Disable the LPTMR and reset the internal logic */
<> 144:ef7eb2e8f9f7 94 base->CSR &= ~LPTMR_CSR_TEN_MASK;
<> 144:ef7eb2e8f9f7 95 /* Gate the LPTMR clock*/
<> 144:ef7eb2e8f9f7 96 CLOCK_DisableClock(s_lptmrClocks[LPTMR_GetInstance(base)]);
<> 144:ef7eb2e8f9f7 97 }
<> 144:ef7eb2e8f9f7 98
<> 144:ef7eb2e8f9f7 99 void LPTMR_GetDefaultConfig(lptmr_config_t *config)
<> 144:ef7eb2e8f9f7 100 {
<> 144:ef7eb2e8f9f7 101 assert(config);
<> 144:ef7eb2e8f9f7 102
<> 144:ef7eb2e8f9f7 103 /* Use time counter mode */
<> 144:ef7eb2e8f9f7 104 config->timerMode = kLPTMR_TimerModeTimeCounter;
<> 144:ef7eb2e8f9f7 105 /* Use input 0 as source in pulse counter mode */
<> 144:ef7eb2e8f9f7 106 config->pinSelect = kLPTMR_PinSelectInput_0;
<> 144:ef7eb2e8f9f7 107 /* Pulse input pin polarity is active-high */
<> 144:ef7eb2e8f9f7 108 config->pinPolarity = kLPTMR_PinPolarityActiveHigh;
<> 144:ef7eb2e8f9f7 109 /* Counter resets whenever TCF flag is set */
<> 144:ef7eb2e8f9f7 110 config->enableFreeRunning = false;
<> 144:ef7eb2e8f9f7 111 /* Bypass the prescaler */
<> 144:ef7eb2e8f9f7 112 config->bypassPrescaler = true;
<> 144:ef7eb2e8f9f7 113 /* LPTMR clock source */
<> 144:ef7eb2e8f9f7 114 config->prescalerClockSource = kLPTMR_PrescalerClock_1;
<> 144:ef7eb2e8f9f7 115 /* Divide the prescaler clock by 2 */
<> 144:ef7eb2e8f9f7 116 config->value = kLPTMR_Prescale_Glitch_0;
<> 144:ef7eb2e8f9f7 117 }