mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
bogdanm
Date:
Mon Aug 05 14:12:34 2013 +0300
Revision:
13:0645d8841f51
Parent:
vendor/Freescale/KL25Z/hal/pwmout_api.c@10:3bc89ef62ce7
Child:
19:398f4c622e1b
Update mbed sources to revision 64

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 10:3bc89ef62ce7 1 /* mbed Microcontroller Library
emilmont 10:3bc89ef62ce7 2 * Copyright (c) 2006-2013 ARM Limited
emilmont 10:3bc89ef62ce7 3 *
emilmont 10:3bc89ef62ce7 4 * Licensed under the Apache License, Version 2.0 (the "License");
emilmont 10:3bc89ef62ce7 5 * you may not use this file except in compliance with the License.
emilmont 10:3bc89ef62ce7 6 * You may obtain a copy of the License at
emilmont 10:3bc89ef62ce7 7 *
emilmont 10:3bc89ef62ce7 8 * http://www.apache.org/licenses/LICENSE-2.0
emilmont 10:3bc89ef62ce7 9 *
emilmont 10:3bc89ef62ce7 10 * Unless required by applicable law or agreed to in writing, software
emilmont 10:3bc89ef62ce7 11 * distributed under the License is distributed on an "AS IS" BASIS,
emilmont 10:3bc89ef62ce7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
emilmont 10:3bc89ef62ce7 13 * See the License for the specific language governing permissions and
emilmont 10:3bc89ef62ce7 14 * limitations under the License.
emilmont 10:3bc89ef62ce7 15 */
emilmont 10:3bc89ef62ce7 16 #include "pwmout_api.h"
emilmont 10:3bc89ef62ce7 17
emilmont 10:3bc89ef62ce7 18 #include "cmsis.h"
emilmont 10:3bc89ef62ce7 19 #include "pinmap.h"
emilmont 10:3bc89ef62ce7 20 #include "error.h"
emilmont 10:3bc89ef62ce7 21
emilmont 10:3bc89ef62ce7 22 static const PinMap PinMap_PWM[] = {
emilmont 10:3bc89ef62ce7 23 // LEDs
emilmont 10:3bc89ef62ce7 24 {LED_RED , PWM_9 , 3}, // PTB18, TPM2 CH0
emilmont 10:3bc89ef62ce7 25 {LED_GREEN, PWM_10, 3}, // PTB19, TPM2 CH1
emilmont 10:3bc89ef62ce7 26 {LED_BLUE , PWM_2 , 4}, // PTD1 , TPM0 CH1
emilmont 10:3bc89ef62ce7 27
emilmont 10:3bc89ef62ce7 28 // Arduino digital pinout
emilmont 10:3bc89ef62ce7 29 {D0, PWM_9 , 3}, // PTA1 , TPM2 CH0
emilmont 10:3bc89ef62ce7 30 {D1, PWM_10, 3}, // PTA2 , TPM2 CH1
emilmont 10:3bc89ef62ce7 31 {D2, PWM_5 , 4}, // PTD4 , TPM0 CH4
emilmont 10:3bc89ef62ce7 32 {D3, PWM_7 , 3}, // PTA12, TPM1 CH0
emilmont 10:3bc89ef62ce7 33 {D4, PWM_2 , 3}, // PTA4 , TPM0 CH1
emilmont 10:3bc89ef62ce7 34 {D5, PWM_3 , 3}, // PTA5 , TPM0 CH2
emilmont 10:3bc89ef62ce7 35 {D6, PWM_5 , 3}, // PTC8 , TPM0 CH4
emilmont 10:3bc89ef62ce7 36 {D7, PWM_6 , 3}, // PTC9 , TPM0 CH5
emilmont 10:3bc89ef62ce7 37 {D8, PWM_8 , 3}, // PTA13, TPM1 CH1
emilmont 10:3bc89ef62ce7 38 {D9, PWM_6 , 4}, // PTD5 , TPM0 CH5
emilmont 10:3bc89ef62ce7 39 {D10, PWM_1 , 4}, // PTD0 , TPM0 CH0
emilmont 10:3bc89ef62ce7 40 {D11, PWM_3 , 4}, // PTD2 , TPM0 CH2
emilmont 10:3bc89ef62ce7 41 {D12, PWM_4 , 4}, // PTD3 , TPM0 CH3
emilmont 10:3bc89ef62ce7 42 {D13, PWM_2 , 4}, // PTD1 , TPM0 CH1
emilmont 10:3bc89ef62ce7 43 {NC , NC , 0}
emilmont 10:3bc89ef62ce7 44 };
emilmont 10:3bc89ef62ce7 45
emilmont 10:3bc89ef62ce7 46 #define PWM_CLOCK_MHZ (0.75) // (48)MHz / 64 = (0.75)MHz
emilmont 10:3bc89ef62ce7 47
emilmont 10:3bc89ef62ce7 48 void pwmout_init(pwmout_t* obj, PinName pin) {
emilmont 10:3bc89ef62ce7 49 // determine the channel
emilmont 10:3bc89ef62ce7 50 PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
emilmont 10:3bc89ef62ce7 51 if (pwm == (uint32_t)NC)
emilmont 10:3bc89ef62ce7 52 error("PwmOut pin mapping failed");
emilmont 10:3bc89ef62ce7 53
emilmont 10:3bc89ef62ce7 54 unsigned int port = (unsigned int)pin >> PORT_SHIFT;
emilmont 10:3bc89ef62ce7 55 unsigned int tpm_n = (pwm >> TPM_SHIFT);
emilmont 10:3bc89ef62ce7 56 unsigned int ch_n = (pwm & 0xFF);
emilmont 10:3bc89ef62ce7 57
emilmont 10:3bc89ef62ce7 58 SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port);
emilmont 10:3bc89ef62ce7 59 SIM->SCGC6 |= 1 << (SIM_SCGC6_TPM0_SHIFT + tpm_n);
emilmont 10:3bc89ef62ce7 60 SIM->SOPT2 |= SIM_SOPT2_TPMSRC(1); // Clock source: MCGFLLCLK or MCGPLLCLK
emilmont 10:3bc89ef62ce7 61
emilmont 10:3bc89ef62ce7 62 TPM_Type *tpm = (TPM_Type *)(TPM0_BASE + 0x1000 * tpm_n);
emilmont 10:3bc89ef62ce7 63 tpm->SC = TPM_SC_CMOD(1) | TPM_SC_PS(6); // (48)MHz / 64 = (0.75)MHz
emilmont 10:3bc89ef62ce7 64 tpm->CONTROLS[ch_n].CnSC = (TPM_CnSC_MSB_MASK | TPM_CnSC_ELSB_MASK); /* No Interrupts; High True pulses on Edge Aligned PWM */
emilmont 10:3bc89ef62ce7 65
emilmont 10:3bc89ef62ce7 66 obj->CnV = &tpm->CONTROLS[ch_n].CnV;
emilmont 10:3bc89ef62ce7 67 obj->MOD = &tpm->MOD;
emilmont 10:3bc89ef62ce7 68 obj->CNT = &tpm->CNT;
emilmont 10:3bc89ef62ce7 69
emilmont 10:3bc89ef62ce7 70 // default to 20ms: standard for servos, and fine for e.g. brightness control
emilmont 10:3bc89ef62ce7 71 pwmout_period_ms(obj, 20);
emilmont 10:3bc89ef62ce7 72 pwmout_write (obj, 0);
emilmont 10:3bc89ef62ce7 73
emilmont 10:3bc89ef62ce7 74 // Wire pinout
emilmont 10:3bc89ef62ce7 75 pinmap_pinout(pin, PinMap_PWM);
emilmont 10:3bc89ef62ce7 76 }
emilmont 10:3bc89ef62ce7 77
emilmont 10:3bc89ef62ce7 78 void pwmout_free(pwmout_t* obj) {}
emilmont 10:3bc89ef62ce7 79
emilmont 10:3bc89ef62ce7 80 void pwmout_write(pwmout_t* obj, float value) {
emilmont 10:3bc89ef62ce7 81 if (value < 0.0) {
emilmont 10:3bc89ef62ce7 82 value = 0.0;
emilmont 10:3bc89ef62ce7 83 } else if (value > 1.0) {
emilmont 10:3bc89ef62ce7 84 value = 1.0;
emilmont 10:3bc89ef62ce7 85 }
emilmont 10:3bc89ef62ce7 86
emilmont 10:3bc89ef62ce7 87 *obj->CnV = (uint32_t)((float)(*obj->MOD) * value);
emilmont 10:3bc89ef62ce7 88 *obj->CNT = 0;
emilmont 10:3bc89ef62ce7 89 }
emilmont 10:3bc89ef62ce7 90
emilmont 10:3bc89ef62ce7 91 float pwmout_read(pwmout_t* obj) {
emilmont 10:3bc89ef62ce7 92 float v = (float)(*obj->CnV) / (float)(*obj->MOD);
emilmont 10:3bc89ef62ce7 93 return (v > 1.0) ? (1.0) : (v);
emilmont 10:3bc89ef62ce7 94 }
emilmont 10:3bc89ef62ce7 95
emilmont 10:3bc89ef62ce7 96 void pwmout_period(pwmout_t* obj, float seconds) {
emilmont 10:3bc89ef62ce7 97 pwmout_period_us(obj, seconds * 1000000.0f);
emilmont 10:3bc89ef62ce7 98 }
emilmont 10:3bc89ef62ce7 99
emilmont 10:3bc89ef62ce7 100 void pwmout_period_ms(pwmout_t* obj, int ms) {
emilmont 10:3bc89ef62ce7 101 pwmout_period_us(obj, ms * 1000);
emilmont 10:3bc89ef62ce7 102 }
emilmont 10:3bc89ef62ce7 103
emilmont 10:3bc89ef62ce7 104 // Set the PWM period, keeping the duty cycle the same.
emilmont 10:3bc89ef62ce7 105 void pwmout_period_us(pwmout_t* obj, int us) {
emilmont 10:3bc89ef62ce7 106 float dc = pwmout_read(obj);
emilmont 10:3bc89ef62ce7 107 *obj->MOD = PWM_CLOCK_MHZ * us;
emilmont 10:3bc89ef62ce7 108 pwmout_write(obj, dc);
emilmont 10:3bc89ef62ce7 109 }
emilmont 10:3bc89ef62ce7 110
emilmont 10:3bc89ef62ce7 111 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
emilmont 10:3bc89ef62ce7 112 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
emilmont 10:3bc89ef62ce7 113 }
emilmont 10:3bc89ef62ce7 114
emilmont 10:3bc89ef62ce7 115 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
emilmont 10:3bc89ef62ce7 116 pwmout_pulsewidth_us(obj, ms * 1000);
emilmont 10:3bc89ef62ce7 117 }
emilmont 10:3bc89ef62ce7 118
emilmont 10:3bc89ef62ce7 119 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
emilmont 10:3bc89ef62ce7 120 *obj->CnV = PWM_CLOCK_MHZ * us;
emilmont 10:3bc89ef62ce7 121 }