mbed library with additional peripherals for ST F401 board

Fork of mbed-src by mbed official

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
Committer:
mbed_official
Date:
Thu Dec 26 13:00:06 2013 +0000
Revision:
68:41613245dfd7
Child:
73:299c67215126
Synchronized with git revision fba199a9c4445231b0f38e1e113c118182635546

Full URL: https://github.com/mbedmicro/mbed/commit/fba199a9c4445231b0f38e1e113c118182635546/

target K20D5M

Who changed what in which revision?

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