mbed library with additional peripherals for ST F401 board
Fork of mbed-src by
This mbed LIB has additional peripherals for ST F401 board
- UART2 : PA_3 rx, PA_2 tx
- UART3 : PC_7 rx, PC_6 tx
- I2C2 : PB_3 SDA, PB_10 SCL
- I2C3 : PB_4 SDA, PA_8 SCL
targets/hal/TARGET_Freescale/TARGET_KL25Z/pwmout_api.c@72:248c61396e08, 2014-01-13 (annotated)
- Committer:
- mbed_official
- Date:
- Mon Jan 13 10:45:05 2014 +0000
- Revision:
- 72:248c61396e08
- Parent:
- 20:4263a77256ae
- Child:
- 74:847f030b50ee
Synchronized with git revision f1904ba15c06215a7530efd2d5a16c25af9d29ff
Full URL: https://github.com/mbedmicro/mbed/commit/f1904ba15c06215a7530efd2d5a16c25af9d29ff/
KL46Z: Added Sleep, LED3 and LED4 definitions, switches
Who changed what in which revision?
User | Revision | Line number | New 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 |
bogdanm | 20:4263a77256ae | 42 | {D13, PWM_2 , 4}, // PTD1 , TPM0 CH1, |
bogdanm | 20:4263a77256ae | 43 | |
bogdanm | 20:4263a77256ae | 44 | {PTA0, PWM_6, 3}, |
bogdanm | 20:4263a77256ae | 45 | {PTA3, PWM_1, 3}, |
bogdanm | 20:4263a77256ae | 46 | {PTB0, PWM_7, 3}, |
bogdanm | 20:4263a77256ae | 47 | {PTB1, PWM_8, 3}, |
bogdanm | 20:4263a77256ae | 48 | {PTB2, PWM_9, 3}, |
bogdanm | 20:4263a77256ae | 49 | {PTB3, PWM_10, 3}, |
bogdanm | 20:4263a77256ae | 50 | {PTC1, PWM_1, 4}, |
bogdanm | 20:4263a77256ae | 51 | {PTC2, PWM_2, 4}, |
bogdanm | 20:4263a77256ae | 52 | {PTC3, PWM_3, 4}, |
bogdanm | 20:4263a77256ae | 53 | {PTC4, PWM_4, 4}, |
bogdanm | 20:4263a77256ae | 54 | {PTE20, PWM_7, 3}, |
bogdanm | 20:4263a77256ae | 55 | {PTE21, PWM_8, 3}, |
bogdanm | 20:4263a77256ae | 56 | {PTE22, PWM_9, 3}, |
bogdanm | 20:4263a77256ae | 57 | {PTE23, PWM_10, 3}, |
bogdanm | 20:4263a77256ae | 58 | {PTE24, PWM_1, 3}, |
bogdanm | 20:4263a77256ae | 59 | {PTE25, PWM_2, 3}, |
bogdanm | 20:4263a77256ae | 60 | {PTE29, PWM_3, 3}, |
bogdanm | 20:4263a77256ae | 61 | {PTE30, PWM_4, 3}, |
bogdanm | 20:4263a77256ae | 62 | {PTE31, PWM_5, 3}, |
bogdanm | 20:4263a77256ae | 63 | |
emilmont | 10:3bc89ef62ce7 | 64 | {NC , NC , 0} |
emilmont | 10:3bc89ef62ce7 | 65 | }; |
emilmont | 10:3bc89ef62ce7 | 66 | |
mbed_official | 72:248c61396e08 | 67 | static float pwm_clock; |
emilmont | 10:3bc89ef62ce7 | 68 | |
emilmont | 10:3bc89ef62ce7 | 69 | void pwmout_init(pwmout_t* obj, PinName pin) { |
emilmont | 10:3bc89ef62ce7 | 70 | // determine the channel |
emilmont | 10:3bc89ef62ce7 | 71 | PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
bogdanm | 19:398f4c622e1b | 72 | if (pwm == (PWMName)NC) |
emilmont | 10:3bc89ef62ce7 | 73 | error("PwmOut pin mapping failed"); |
mbed_official | 72:248c61396e08 | 74 | |
mbed_official | 72:248c61396e08 | 75 | uint32_t clkdiv = 0; |
mbed_official | 72:248c61396e08 | 76 | float clkval = SystemCoreClock / 1000000.0f; |
mbed_official | 72:248c61396e08 | 77 | |
mbed_official | 72:248c61396e08 | 78 | while (clkval > 1) { |
mbed_official | 72:248c61396e08 | 79 | clkdiv++; |
mbed_official | 72:248c61396e08 | 80 | clkval /= 2.0; |
mbed_official | 72:248c61396e08 | 81 | if (clkdiv == 7) |
mbed_official | 72:248c61396e08 | 82 | break; |
mbed_official | 72:248c61396e08 | 83 | } |
mbed_official | 72:248c61396e08 | 84 | |
mbed_official | 72:248c61396e08 | 85 | pwm_clock = clkval; |
emilmont | 10:3bc89ef62ce7 | 86 | unsigned int port = (unsigned int)pin >> PORT_SHIFT; |
emilmont | 10:3bc89ef62ce7 | 87 | unsigned int tpm_n = (pwm >> TPM_SHIFT); |
emilmont | 10:3bc89ef62ce7 | 88 | unsigned int ch_n = (pwm & 0xFF); |
emilmont | 10:3bc89ef62ce7 | 89 | |
emilmont | 10:3bc89ef62ce7 | 90 | SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port); |
emilmont | 10:3bc89ef62ce7 | 91 | SIM->SCGC6 |= 1 << (SIM_SCGC6_TPM0_SHIFT + tpm_n); |
emilmont | 10:3bc89ef62ce7 | 92 | SIM->SOPT2 |= SIM_SOPT2_TPMSRC(1); // Clock source: MCGFLLCLK or MCGPLLCLK |
emilmont | 10:3bc89ef62ce7 | 93 | |
emilmont | 10:3bc89ef62ce7 | 94 | TPM_Type *tpm = (TPM_Type *)(TPM0_BASE + 0x1000 * tpm_n); |
mbed_official | 72:248c61396e08 | 95 | tpm->SC = TPM_SC_CMOD(1) | TPM_SC_PS(clkdiv); // (clock)MHz / clkdiv ~= (0.75)MHz |
emilmont | 10:3bc89ef62ce7 | 96 | 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 | 97 | |
emilmont | 10:3bc89ef62ce7 | 98 | obj->CnV = &tpm->CONTROLS[ch_n].CnV; |
emilmont | 10:3bc89ef62ce7 | 99 | obj->MOD = &tpm->MOD; |
emilmont | 10:3bc89ef62ce7 | 100 | obj->CNT = &tpm->CNT; |
emilmont | 10:3bc89ef62ce7 | 101 | |
emilmont | 10:3bc89ef62ce7 | 102 | // default to 20ms: standard for servos, and fine for e.g. brightness control |
emilmont | 10:3bc89ef62ce7 | 103 | pwmout_period_ms(obj, 20); |
emilmont | 10:3bc89ef62ce7 | 104 | pwmout_write (obj, 0); |
emilmont | 10:3bc89ef62ce7 | 105 | |
emilmont | 10:3bc89ef62ce7 | 106 | // Wire pinout |
emilmont | 10:3bc89ef62ce7 | 107 | pinmap_pinout(pin, PinMap_PWM); |
emilmont | 10:3bc89ef62ce7 | 108 | } |
emilmont | 10:3bc89ef62ce7 | 109 | |
emilmont | 10:3bc89ef62ce7 | 110 | void pwmout_free(pwmout_t* obj) {} |
emilmont | 10:3bc89ef62ce7 | 111 | |
emilmont | 10:3bc89ef62ce7 | 112 | void pwmout_write(pwmout_t* obj, float value) { |
emilmont | 10:3bc89ef62ce7 | 113 | if (value < 0.0) { |
emilmont | 10:3bc89ef62ce7 | 114 | value = 0.0; |
emilmont | 10:3bc89ef62ce7 | 115 | } else if (value > 1.0) { |
emilmont | 10:3bc89ef62ce7 | 116 | value = 1.0; |
emilmont | 10:3bc89ef62ce7 | 117 | } |
emilmont | 10:3bc89ef62ce7 | 118 | |
emilmont | 10:3bc89ef62ce7 | 119 | *obj->CnV = (uint32_t)((float)(*obj->MOD) * value); |
emilmont | 10:3bc89ef62ce7 | 120 | *obj->CNT = 0; |
emilmont | 10:3bc89ef62ce7 | 121 | } |
emilmont | 10:3bc89ef62ce7 | 122 | |
emilmont | 10:3bc89ef62ce7 | 123 | float pwmout_read(pwmout_t* obj) { |
emilmont | 10:3bc89ef62ce7 | 124 | float v = (float)(*obj->CnV) / (float)(*obj->MOD); |
emilmont | 10:3bc89ef62ce7 | 125 | return (v > 1.0) ? (1.0) : (v); |
emilmont | 10:3bc89ef62ce7 | 126 | } |
emilmont | 10:3bc89ef62ce7 | 127 | |
emilmont | 10:3bc89ef62ce7 | 128 | void pwmout_period(pwmout_t* obj, float seconds) { |
emilmont | 10:3bc89ef62ce7 | 129 | pwmout_period_us(obj, seconds * 1000000.0f); |
emilmont | 10:3bc89ef62ce7 | 130 | } |
emilmont | 10:3bc89ef62ce7 | 131 | |
emilmont | 10:3bc89ef62ce7 | 132 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
emilmont | 10:3bc89ef62ce7 | 133 | pwmout_period_us(obj, ms * 1000); |
emilmont | 10:3bc89ef62ce7 | 134 | } |
emilmont | 10:3bc89ef62ce7 | 135 | |
emilmont | 10:3bc89ef62ce7 | 136 | // Set the PWM period, keeping the duty cycle the same. |
emilmont | 10:3bc89ef62ce7 | 137 | void pwmout_period_us(pwmout_t* obj, int us) { |
emilmont | 10:3bc89ef62ce7 | 138 | float dc = pwmout_read(obj); |
mbed_official | 72:248c61396e08 | 139 | *obj->MOD = (uint32_t)(pwm_clock * (float)us); |
emilmont | 10:3bc89ef62ce7 | 140 | pwmout_write(obj, dc); |
emilmont | 10:3bc89ef62ce7 | 141 | } |
emilmont | 10:3bc89ef62ce7 | 142 | |
emilmont | 10:3bc89ef62ce7 | 143 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
emilmont | 10:3bc89ef62ce7 | 144 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
emilmont | 10:3bc89ef62ce7 | 145 | } |
emilmont | 10:3bc89ef62ce7 | 146 | |
emilmont | 10:3bc89ef62ce7 | 147 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
emilmont | 10:3bc89ef62ce7 | 148 | pwmout_pulsewidth_us(obj, ms * 1000); |
emilmont | 10:3bc89ef62ce7 | 149 | } |
emilmont | 10:3bc89ef62ce7 | 150 | |
emilmont | 10:3bc89ef62ce7 | 151 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
mbed_official | 72:248c61396e08 | 152 | *obj->CnV = (uint32_t)(pwm_clock * (float)us); |
emilmont | 10:3bc89ef62ce7 | 153 | } |