fixed drive strength

Dependents:   capstone_i2c

Fork of mbed-dev by mbed official

Committer:
cpadua
Date:
Tue Apr 11 20:39:24 2017 +0000
Revision:
163:1d4c9d0af1e9
Parent:
149:156823d33999
fixed i2c-api.c

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 149:156823d33999 1 /* mbed Microcontroller Library
<> 149:156823d33999 2 * Copyright (c) 2006-2013 ARM Limited
<> 149:156823d33999 3 *
<> 149:156823d33999 4 * Licensed under the Apache License, Version 2.0 (the "License");
<> 149:156823d33999 5 * you may not use this file except in compliance with the License.
<> 149:156823d33999 6 * You may obtain a copy of the License at
<> 149:156823d33999 7 *
<> 149:156823d33999 8 * http://www.apache.org/licenses/LICENSE-2.0
<> 149:156823d33999 9 *
<> 149:156823d33999 10 * Unless required by applicable law or agreed to in writing, software
<> 149:156823d33999 11 * distributed under the License is distributed on an "AS IS" BASIS,
<> 149:156823d33999 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<> 149:156823d33999 13 * See the License for the specific language governing permissions and
<> 149:156823d33999 14 * limitations under the License.
<> 149:156823d33999 15 */
<> 149:156823d33999 16 #include "mbed_assert.h"
<> 149:156823d33999 17 #include "pwmout_api.h"
<> 149:156823d33999 18 #include "cmsis.h"
<> 149:156823d33999 19 #include "pinmap.h"
<> 149:156823d33999 20
<> 149:156823d33999 21 #define TCR_CNT_EN 0x00000001
<> 149:156823d33999 22 #define TCR_RESET 0x00000002
<> 149:156823d33999 23
<> 149:156823d33999 24 /* To have a PWM where we can change both the period and the duty cycle,
<> 149:156823d33999 25 * we need an entire timer. With the following conventions:
<> 149:156823d33999 26 * * MR3 is used for the PWM period
<> 149:156823d33999 27 * * MR0, MR1, MR2 are used for the duty cycle
<> 149:156823d33999 28 */
<> 149:156823d33999 29 static const PinMap PinMap_PWM[] = {
<> 149:156823d33999 30 /* CT16B0 */
<> 149:156823d33999 31 {P0_8 , PWM_1, 2}, {P1_13, PWM_1, 2}, /* MR0 */
<> 149:156823d33999 32 {P0_9 , PWM_2, 2}, {P1_14, PWM_2, 2}, /* MR1 */
<> 149:156823d33999 33 {P0_10, PWM_3, 3}, {P1_15, PWM_3, 2}, /* MR2 */
<> 149:156823d33999 34
<> 149:156823d33999 35 /* CT16B1 */
<> 149:156823d33999 36 {P0_21, PWM_4, 1}, /* MR0 */
<> 149:156823d33999 37 {P0_22, PWM_5, 2}, {P1_23, PWM_5, 1}, /* MR1 */
<> 149:156823d33999 38
<> 149:156823d33999 39 /* CT32B0 */
<> 149:156823d33999 40 {P0_18, PWM_6, 2}, {P1_24, PWM_6, 1}, /* MR0 */
<> 149:156823d33999 41 {P0_19, PWM_7, 2}, {P1_25, PWM_7, 1}, /* MR1 */
<> 149:156823d33999 42 {P0_1 , PWM_8, 2}, {P1_26, PWM_8, 1}, /* MR2 */
<> 149:156823d33999 43
<> 149:156823d33999 44 /* CT32B1 */
<> 149:156823d33999 45 {P0_13, PWM_9 , 3}, //{P1_0, PWM_9 , 1}, /* MR0 */
<> 149:156823d33999 46 {P0_14, PWM_10, 3}, //{P1_1, PWM_10, 1}, /* MR1 */
<> 149:156823d33999 47 {P0_15, PWM_11, 3}, //{P1_2, PWM_11, 1}, /* MR2 */
<> 149:156823d33999 48
<> 149:156823d33999 49 {NC, NC, 0}
<> 149:156823d33999 50 };
<> 149:156823d33999 51
<> 149:156823d33999 52 typedef struct {
<> 149:156823d33999 53 uint8_t timer;
<> 149:156823d33999 54 uint8_t mr;
<> 149:156823d33999 55 } timer_mr;
<> 149:156823d33999 56
<> 149:156823d33999 57 static timer_mr pwm_timer_map[11] = {
<> 149:156823d33999 58 {0, 0}, {0, 1}, {0, 2},
<> 149:156823d33999 59 {1, 0}, {1, 1},
<> 149:156823d33999 60 {2, 0}, {2, 1}, {2, 2},
<> 149:156823d33999 61 {3, 0}, {3, 1}, {3, 2},
<> 149:156823d33999 62 };
<> 149:156823d33999 63
<> 149:156823d33999 64 static LPC_CTxxBx_Type *Timers[4] = {
<> 149:156823d33999 65 LPC_CT16B0, LPC_CT16B1,
<> 149:156823d33999 66 LPC_CT32B0, LPC_CT32B1
<> 149:156823d33999 67 };
<> 149:156823d33999 68
<> 149:156823d33999 69 void pwmout_init(pwmout_t* obj, PinName pin) {
<> 149:156823d33999 70 // determine the channel
<> 149:156823d33999 71 PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
<> 149:156823d33999 72 MBED_ASSERT(pwm != (PWMName)NC);
<> 149:156823d33999 73
<> 149:156823d33999 74 obj->pwm = pwm;
<> 149:156823d33999 75
<> 149:156823d33999 76 // Timer registers
<> 149:156823d33999 77 timer_mr tid = pwm_timer_map[pwm];
<> 149:156823d33999 78 LPC_CTxxBx_Type *timer = Timers[tid.timer];
<> 149:156823d33999 79
<> 149:156823d33999 80 // Disable timer
<> 149:156823d33999 81 timer->TCR = 0;
<> 149:156823d33999 82
<> 149:156823d33999 83 // Power the correspondent timer
<> 149:156823d33999 84 LPC_SYSCON->SYSAHBCLKCTRL |= 1 << (tid.timer + 7);
<> 149:156823d33999 85
<> 149:156823d33999 86 /* Enable PWM function */
<> 149:156823d33999 87 timer->PWMC = (1 << 3)|(1 << 2)|(1 << 1)|(1 << 0);
<> 149:156823d33999 88
<> 149:156823d33999 89 /* Reset Functionality on MR3 controlling the PWM period */
<> 149:156823d33999 90 timer->MCR = 1 << 10;
<> 149:156823d33999 91
<> 149:156823d33999 92 if (timer == LPC_CT16B0 || timer == LPC_CT16B1) {
<> 149:156823d33999 93 /* Set 16-bit timer prescaler to avoid timer expire for default 20ms
<> 149:156823d33999 94 This can be also modified by user application, but the prescaler value
<> 149:156823d33999 95 might be trade-off to timer accuracy */
<> 149:156823d33999 96 timer->PR = 30;
<> 149:156823d33999 97 }
<> 149:156823d33999 98
<> 149:156823d33999 99 // default to 20ms: standard for servos, and fine for e.g. brightness control
<> 149:156823d33999 100 pwmout_period_ms(obj, 20);
<> 149:156823d33999 101 pwmout_write (obj, 0);
<> 149:156823d33999 102
<> 149:156823d33999 103 // Wire pinout
<> 149:156823d33999 104 pinmap_pinout(pin, PinMap_PWM);
<> 149:156823d33999 105 }
<> 149:156823d33999 106
<> 149:156823d33999 107 void pwmout_free(pwmout_t* obj) {
<> 149:156823d33999 108 // [TODO]
<> 149:156823d33999 109 }
<> 149:156823d33999 110
<> 149:156823d33999 111 void pwmout_write(pwmout_t* obj, float value) {
<> 149:156823d33999 112 if (value < 0.0f) {
<> 149:156823d33999 113 value = 0.0;
<> 149:156823d33999 114 } else if (value > 1.0f) {
<> 149:156823d33999 115 value = 1.0;
<> 149:156823d33999 116 }
<> 149:156823d33999 117
<> 149:156823d33999 118 timer_mr tid = pwm_timer_map[obj->pwm];
<> 149:156823d33999 119 LPC_CTxxBx_Type *timer = Timers[tid.timer];
<> 149:156823d33999 120 uint32_t t_off = timer->MR3 - (uint32_t)((float)(timer->MR3) * value);
<> 149:156823d33999 121
<> 149:156823d33999 122 // to avoid spike pulse when duty is 0%
<> 149:156823d33999 123 if (value == 0) {
<> 149:156823d33999 124 t_off++;
<> 149:156823d33999 125 }
<> 149:156823d33999 126
<> 149:156823d33999 127 timer->TCR = TCR_RESET;
<> 149:156823d33999 128 timer->MR[tid.mr] = t_off;
<> 149:156823d33999 129 timer->TCR = TCR_CNT_EN;
<> 149:156823d33999 130 }
<> 149:156823d33999 131
<> 149:156823d33999 132 float pwmout_read(pwmout_t* obj) {
<> 149:156823d33999 133 timer_mr tid = pwm_timer_map[obj->pwm];
<> 149:156823d33999 134 LPC_CTxxBx_Type *timer = Timers[tid.timer];
<> 149:156823d33999 135
<> 149:156823d33999 136 float v = (float)(timer->MR3 - timer->MR[tid.mr]) / (float)(timer->MR3);
<> 149:156823d33999 137 if (timer->MR[tid.mr] > timer->MR3) {
<> 149:156823d33999 138 v = 0.0f;
<> 149:156823d33999 139 }
<> 149:156823d33999 140 return (v > 1.0f) ? (1.0f) : (v);
<> 149:156823d33999 141 }
<> 149:156823d33999 142
<> 149:156823d33999 143 void pwmout_period(pwmout_t* obj, float seconds) {
<> 149:156823d33999 144 pwmout_period_us(obj, seconds * 1000000.0f);
<> 149:156823d33999 145 }
<> 149:156823d33999 146
<> 149:156823d33999 147 void pwmout_period_ms(pwmout_t* obj, int ms) {
<> 149:156823d33999 148 pwmout_period_us(obj, ms * 1000);
<> 149:156823d33999 149 }
<> 149:156823d33999 150
<> 149:156823d33999 151 // Set the PWM period, keeping the duty cycle the same.
<> 149:156823d33999 152 void pwmout_period_us(pwmout_t* obj, int us) {
<> 149:156823d33999 153 int i = 0;
<> 149:156823d33999 154
<> 149:156823d33999 155 timer_mr tid = pwm_timer_map[obj->pwm];
<> 149:156823d33999 156 LPC_CTxxBx_Type *timer = Timers[tid.timer];
<> 149:156823d33999 157 uint32_t old_period_ticks = timer->MR3;
<> 149:156823d33999 158 uint32_t period_ticks = (SystemCoreClock / 1000000 * us) / (timer->PR + 1);
<> 149:156823d33999 159
<> 149:156823d33999 160 timer->TCR = TCR_RESET;
<> 149:156823d33999 161 timer->MR3 = period_ticks;
<> 149:156823d33999 162
<> 149:156823d33999 163 // Scale the pulse width to preserve the duty ratio
<> 149:156823d33999 164 if (old_period_ticks > 0) {
<> 149:156823d33999 165 for (i=0; i<3; i++) {
<> 149:156823d33999 166 uint32_t t_off = period_ticks - (uint32_t)(((uint64_t)timer->MR[i] * (uint64_t)period_ticks) / (uint64_t)old_period_ticks);
<> 149:156823d33999 167 timer->MR[i] = t_off;
<> 149:156823d33999 168 }
<> 149:156823d33999 169 }
<> 149:156823d33999 170 timer->TCR = TCR_CNT_EN;
<> 149:156823d33999 171 }
<> 149:156823d33999 172
<> 149:156823d33999 173 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
<> 149:156823d33999 174 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
<> 149:156823d33999 175 }
<> 149:156823d33999 176
<> 149:156823d33999 177 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
<> 149:156823d33999 178 pwmout_pulsewidth_us(obj, ms * 1000);
<> 149:156823d33999 179 }
<> 149:156823d33999 180
<> 149:156823d33999 181 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
<> 149:156823d33999 182 timer_mr tid = pwm_timer_map[obj->pwm];
<> 149:156823d33999 183 LPC_CTxxBx_Type *timer = Timers[tid.timer];
<> 149:156823d33999 184 uint32_t t_on = (uint32_t)((((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000) / (timer->PR + 1));
<> 149:156823d33999 185
<> 149:156823d33999 186 timer->TCR = TCR_RESET;
<> 149:156823d33999 187 if (t_on > timer->MR3) {
<> 149:156823d33999 188 pwmout_period_us(obj, us);
<> 149:156823d33999 189 }
<> 149:156823d33999 190 uint32_t t_off = timer->MR3 - t_on;
<> 149:156823d33999 191 timer->MR[tid.mr] = t_off;
<> 149:156823d33999 192 timer->TCR = TCR_CNT_EN;
<> 149:156823d33999 193 }