mbed library sources, include can_api for nucleo-f091rc

Dependents:   CanNucleoF0_example

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Thu Mar 12 14:30:49 2015 +0000
Revision:
489:119543c9f674
Parent:
445:3312ed629f01
Synchronized with git revision 051854181516992fb498d51f9ee6e70cbad9e083

Full URL: https://github.com/mbedmicro/mbed/commit/051854181516992fb498d51f9ee6e70cbad9e083/

Fix ksdk mcu HAL - stopbit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 445:3312ed629f01 1 /* mbed Microcontroller Library
mbed_official 445:3312ed629f01 2 * Copyright (c) 2006-2015 ARM Limited
mbed_official 445:3312ed629f01 3 *
mbed_official 445:3312ed629f01 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 445:3312ed629f01 5 * you may not use this file except in compliance with the License.
mbed_official 445:3312ed629f01 6 * You may obtain a copy of the License at
mbed_official 445:3312ed629f01 7 *
mbed_official 445:3312ed629f01 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 445:3312ed629f01 9 *
mbed_official 445:3312ed629f01 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 445:3312ed629f01 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 445:3312ed629f01 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 445:3312ed629f01 13 * See the License for the specific language governing permissions and
mbed_official 445:3312ed629f01 14 * limitations under the License.
mbed_official 445:3312ed629f01 15 */
mbed_official 445:3312ed629f01 16 #include "mbed_assert.h"
mbed_official 445:3312ed629f01 17 #include "pwmout_api.h"
mbed_official 445:3312ed629f01 18
mbed_official 445:3312ed629f01 19 #include "cmsis.h"
mbed_official 445:3312ed629f01 20 #include "pinmap.h"
mbed_official 445:3312ed629f01 21 #include "PeripheralPins.h"
mbed_official 445:3312ed629f01 22
mbed_official 445:3312ed629f01 23 static float pwm_clock = 0;
mbed_official 445:3312ed629f01 24
mbed_official 445:3312ed629f01 25 void pwmout_init(pwmout_t* obj, PinName pin) {
mbed_official 445:3312ed629f01 26 // determine the channel
mbed_official 445:3312ed629f01 27 PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
mbed_official 445:3312ed629f01 28 MBED_ASSERT(pwm != (PWMName)NC);
mbed_official 445:3312ed629f01 29
mbed_official 445:3312ed629f01 30 uint32_t clkdiv = 0;
mbed_official 445:3312ed629f01 31 float clkval = SystemCoreClock / 1000000.0f;
mbed_official 445:3312ed629f01 32
mbed_official 445:3312ed629f01 33 while (clkval > 1) {
mbed_official 445:3312ed629f01 34 clkdiv++;
mbed_official 445:3312ed629f01 35 clkval /= 2.0;
mbed_official 445:3312ed629f01 36 if (clkdiv == 7)
mbed_official 445:3312ed629f01 37 break;
mbed_official 445:3312ed629f01 38 }
mbed_official 445:3312ed629f01 39
mbed_official 445:3312ed629f01 40 pwm_clock = clkval;
mbed_official 445:3312ed629f01 41 unsigned int ftm_n = (pwm >> TPM_SHIFT);
mbed_official 445:3312ed629f01 42 unsigned int ch_n = (pwm & 0xFF);
mbed_official 445:3312ed629f01 43
mbed_official 445:3312ed629f01 44 SIM->SCGC6 |= 1 << (SIM_SCGC6_FTM0_SHIFT + ftm_n);
mbed_official 445:3312ed629f01 45
mbed_official 445:3312ed629f01 46 FTM_Type *ftm = (FTM_Type *)(FTM0_BASE + 0x1000 * ftm_n);
mbed_official 445:3312ed629f01 47 ftm->CONF |= FTM_CONF_BDMMODE(3);
mbed_official 445:3312ed629f01 48 ftm->SC = FTM_SC_CLKS(1) | FTM_SC_PS(clkdiv); // (clock)MHz / clkdiv ~= (0.75)MHz
mbed_official 445:3312ed629f01 49 ftm->CONTROLS[ch_n].CnSC = (FTM_CnSC_MSB_MASK | FTM_CnSC_ELSB_MASK); /* No Interrupts; High True pulses on Edge Aligned PWM */
mbed_official 489:119543c9f674 50 ftm->MODE = FTM_MODE_FTMEN_MASK;
mbed_official 489:119543c9f674 51 ftm->SYNC = FTM_SYNC_CNTMIN_MASK;
mbed_official 489:119543c9f674 52 ftm->SYNCONF = FTM_SYNCONF_SYNCMODE_MASK | FTM_SYNCONF_SWSOC_MASK | FTM_SYNCONF_SWWRBUF_MASK;
mbed_official 489:119543c9f674 53
mbed_official 489:119543c9f674 54 //Without SYNCEN set CnV does not seem to update
mbed_official 489:119543c9f674 55 ftm->COMBINE = FTM_COMBINE_SYNCEN0_MASK | FTM_COMBINE_SYNCEN1_MASK | FTM_COMBINE_SYNCEN2_MASK | FTM_COMBINE_SYNCEN3_MASK;
mbed_official 445:3312ed629f01 56
mbed_official 445:3312ed629f01 57 obj->CnV = &ftm->CONTROLS[ch_n].CnV;
mbed_official 445:3312ed629f01 58 obj->MOD = &ftm->MOD;
mbed_official 489:119543c9f674 59 obj->SYNC = &ftm->SYNC;
mbed_official 445:3312ed629f01 60
mbed_official 445:3312ed629f01 61 // default to 20ms: standard for servos, and fine for e.g. brightness control
mbed_official 445:3312ed629f01 62 pwmout_period_ms(obj, 20);
mbed_official 489:119543c9f674 63 pwmout_write(obj, 0.0);
mbed_official 489:119543c9f674 64
mbed_official 445:3312ed629f01 65 // Wire pinout
mbed_official 445:3312ed629f01 66 pinmap_pinout(pin, PinMap_PWM);
mbed_official 445:3312ed629f01 67 }
mbed_official 445:3312ed629f01 68
mbed_official 445:3312ed629f01 69 void pwmout_free(pwmout_t* obj) {}
mbed_official 445:3312ed629f01 70
mbed_official 445:3312ed629f01 71 void pwmout_write(pwmout_t* obj, float value) {
mbed_official 445:3312ed629f01 72 if (value < 0.0) {
mbed_official 445:3312ed629f01 73 value = 0.0;
mbed_official 445:3312ed629f01 74 } else if (value > 1.0) {
mbed_official 445:3312ed629f01 75 value = 1.0;
mbed_official 445:3312ed629f01 76 }
mbed_official 489:119543c9f674 77
mbed_official 489:119543c9f674 78 while(*obj->SYNC & FTM_SYNC_SWSYNC_MASK);
mbed_official 445:3312ed629f01 79 *obj->CnV = (uint32_t)((float)(*obj->MOD + 1) * value);
mbed_official 489:119543c9f674 80 *obj->SYNC |= FTM_SYNC_SWSYNC_MASK;
mbed_official 445:3312ed629f01 81 }
mbed_official 445:3312ed629f01 82
mbed_official 445:3312ed629f01 83 float pwmout_read(pwmout_t* obj) {
mbed_official 489:119543c9f674 84 while(*obj->SYNC & FTM_SYNC_SWSYNC_MASK);
mbed_official 445:3312ed629f01 85 float v = (float)(*obj->CnV) / (float)(*obj->MOD + 1);
mbed_official 445:3312ed629f01 86 return (v > 1.0) ? (1.0) : (v);
mbed_official 445:3312ed629f01 87 }
mbed_official 445:3312ed629f01 88
mbed_official 445:3312ed629f01 89 void pwmout_period(pwmout_t* obj, float seconds) {
mbed_official 445:3312ed629f01 90 pwmout_period_us(obj, seconds * 1000000.0f);
mbed_official 445:3312ed629f01 91 }
mbed_official 445:3312ed629f01 92
mbed_official 445:3312ed629f01 93 void pwmout_period_ms(pwmout_t* obj, int ms) {
mbed_official 445:3312ed629f01 94 pwmout_period_us(obj, ms * 1000);
mbed_official 445:3312ed629f01 95 }
mbed_official 445:3312ed629f01 96
mbed_official 445:3312ed629f01 97 // Set the PWM period, keeping the duty cycle the same.
mbed_official 445:3312ed629f01 98 void pwmout_period_us(pwmout_t* obj, int us) {
mbed_official 445:3312ed629f01 99 float dc = pwmout_read(obj);
mbed_official 445:3312ed629f01 100 *obj->MOD = (uint32_t)(pwm_clock * (float)us) - 1;
mbed_official 489:119543c9f674 101 *obj->SYNC |= FTM_SYNC_SWSYNC_MASK;
mbed_official 445:3312ed629f01 102 pwmout_write(obj, dc);
mbed_official 445:3312ed629f01 103 }
mbed_official 445:3312ed629f01 104
mbed_official 445:3312ed629f01 105 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
mbed_official 445:3312ed629f01 106 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
mbed_official 445:3312ed629f01 107 }
mbed_official 445:3312ed629f01 108
mbed_official 445:3312ed629f01 109 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
mbed_official 445:3312ed629f01 110 pwmout_pulsewidth_us(obj, ms * 1000);
mbed_official 445:3312ed629f01 111 }
mbed_official 445:3312ed629f01 112
mbed_official 445:3312ed629f01 113 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
mbed_official 445:3312ed629f01 114 *obj->CnV = (uint32_t)(pwm_clock * (float)us);
mbed_official 489:119543c9f674 115 *obj->SYNC |= FTM_SYNC_SWSYNC_MASK;
mbed_official 445:3312ed629f01 116 }