5.2.1 - Updated I2C files

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Committer:
jacobjohnson
Date:
Mon Feb 27 17:45:05 2017 +0000
Revision:
1:f30bdcd2b33b
Parent:
0:098463de4c5d
changed the inputscale from 1 to 7 in analogin_api.c.  This will need to be changed later, and accessed from the main level, but for now this allows the  adc to read a value from 0 to 3.7V, instead of just up to 1V.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
group-onsemi 0:098463de4c5d 1 /* mbed Microcontroller Library
group-onsemi 0:098463de4c5d 2 * Copyright (c) 2006-2013 ARM Limited
group-onsemi 0:098463de4c5d 3 *
group-onsemi 0:098463de4c5d 4 * Licensed under the Apache License, Version 2.0 (the "License");
group-onsemi 0:098463de4c5d 5 * you may not use this file except in compliance with the License.
group-onsemi 0:098463de4c5d 6 * You may obtain a copy of the License at
group-onsemi 0:098463de4c5d 7 *
group-onsemi 0:098463de4c5d 8 * http://www.apache.org/licenses/LICENSE-2.0
group-onsemi 0:098463de4c5d 9 *
group-onsemi 0:098463de4c5d 10 * Unless required by applicable law or agreed to in writing, software
group-onsemi 0:098463de4c5d 11 * distributed under the License is distributed on an "AS IS" BASIS,
group-onsemi 0:098463de4c5d 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
group-onsemi 0:098463de4c5d 13 * See the License for the specific language governing permissions and
group-onsemi 0:098463de4c5d 14 * limitations under the License.
group-onsemi 0:098463de4c5d 15 */
group-onsemi 0:098463de4c5d 16 #include "mbed_assert.h"
group-onsemi 0:098463de4c5d 17 #include "pwmout_api.h"
group-onsemi 0:098463de4c5d 18 #include "cmsis.h"
group-onsemi 0:098463de4c5d 19 #include "pinmap.h"
group-onsemi 0:098463de4c5d 20 #include "PeripheralPins.h" // For the Peripheral to Pin Definitions found in the individual Target's Platform
group-onsemi 0:098463de4c5d 21
group-onsemi 0:098463de4c5d 22 #define TCR_CNT_EN 0x00000001
group-onsemi 0:098463de4c5d 23 #define TCR_RESET 0x00000002
group-onsemi 0:098463de4c5d 24
group-onsemi 0:098463de4c5d 25 typedef struct {
group-onsemi 0:098463de4c5d 26 uint8_t timer;
group-onsemi 0:098463de4c5d 27 uint8_t mr;
group-onsemi 0:098463de4c5d 28 } timer_mr;
group-onsemi 0:098463de4c5d 29
group-onsemi 0:098463de4c5d 30 static timer_mr pwm_timer_map[11] = {
group-onsemi 0:098463de4c5d 31 {0, 0}, {0, 1}, {0, 2},
group-onsemi 0:098463de4c5d 32 {1, 0}, {1, 1},
group-onsemi 0:098463de4c5d 33 {2, 0}, {2, 1}, {2, 2},
group-onsemi 0:098463de4c5d 34 {3, 0}, {3, 1}, {3, 2},
group-onsemi 0:098463de4c5d 35 };
group-onsemi 0:098463de4c5d 36
group-onsemi 0:098463de4c5d 37 static LPC_CTxxBx_Type *Timers[4] = {
group-onsemi 0:098463de4c5d 38 LPC_CT16B0, LPC_CT16B1,
group-onsemi 0:098463de4c5d 39 LPC_CT32B0, LPC_CT32B1
group-onsemi 0:098463de4c5d 40 };
group-onsemi 0:098463de4c5d 41
group-onsemi 0:098463de4c5d 42 void pwmout_init(pwmout_t* obj, PinName pin) {
group-onsemi 0:098463de4c5d 43 // determine the channel
group-onsemi 0:098463de4c5d 44 PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
group-onsemi 0:098463de4c5d 45 MBED_ASSERT(pwm != (PWMName)NC);
group-onsemi 0:098463de4c5d 46
group-onsemi 0:098463de4c5d 47 obj->pwm = pwm;
group-onsemi 0:098463de4c5d 48
group-onsemi 0:098463de4c5d 49 // Timer registers
group-onsemi 0:098463de4c5d 50 timer_mr tid = pwm_timer_map[pwm];
group-onsemi 0:098463de4c5d 51 LPC_CTxxBx_Type *timer = Timers[tid.timer];
group-onsemi 0:098463de4c5d 52
group-onsemi 0:098463de4c5d 53 // Disable timer
group-onsemi 0:098463de4c5d 54 timer->TCR = 0;
group-onsemi 0:098463de4c5d 55
group-onsemi 0:098463de4c5d 56 // Power the correspondent timer
group-onsemi 0:098463de4c5d 57 LPC_SYSCON->SYSAHBCLKCTRL |= 1 << (tid.timer + 7);
group-onsemi 0:098463de4c5d 58
group-onsemi 0:098463de4c5d 59 /* Enable PWM function */
group-onsemi 0:098463de4c5d 60 timer->PWMC = (1 << 3)|(1 << 2)|(1 << 1)|(1 << 0);
group-onsemi 0:098463de4c5d 61
group-onsemi 0:098463de4c5d 62 /* Reset Functionality on MR3 controlling the PWM period */
group-onsemi 0:098463de4c5d 63 timer->MCR = 1 << 10;
group-onsemi 0:098463de4c5d 64
group-onsemi 0:098463de4c5d 65 // default to 20ms: standard for servos, and fine for e.g. brightness control
group-onsemi 0:098463de4c5d 66 pwmout_period_ms(obj, 20);
group-onsemi 0:098463de4c5d 67 pwmout_write (obj, 0);
group-onsemi 0:098463de4c5d 68
group-onsemi 0:098463de4c5d 69 // Wire pinout
group-onsemi 0:098463de4c5d 70 pinmap_pinout(pin, PinMap_PWM);
group-onsemi 0:098463de4c5d 71 }
group-onsemi 0:098463de4c5d 72
group-onsemi 0:098463de4c5d 73 void pwmout_free(pwmout_t* obj) {
group-onsemi 0:098463de4c5d 74 // [TODO]
group-onsemi 0:098463de4c5d 75 }
group-onsemi 0:098463de4c5d 76
group-onsemi 0:098463de4c5d 77 void pwmout_write(pwmout_t* obj, float value) {
group-onsemi 0:098463de4c5d 78 if (value < 0.0f) {
group-onsemi 0:098463de4c5d 79 value = 0.0;
group-onsemi 0:098463de4c5d 80 } else if (value > 1.0f) {
group-onsemi 0:098463de4c5d 81 value = 1.0;
group-onsemi 0:098463de4c5d 82 }
group-onsemi 0:098463de4c5d 83
group-onsemi 0:098463de4c5d 84 timer_mr tid = pwm_timer_map[obj->pwm];
group-onsemi 0:098463de4c5d 85 LPC_CTxxBx_Type *timer = Timers[tid.timer];
group-onsemi 0:098463de4c5d 86 uint32_t t_off = timer->MR3 - (uint32_t)((float)(timer->MR3) * value);
group-onsemi 0:098463de4c5d 87
group-onsemi 0:098463de4c5d 88 timer->MR[tid.mr] = t_off;
group-onsemi 0:098463de4c5d 89 }
group-onsemi 0:098463de4c5d 90
group-onsemi 0:098463de4c5d 91 float pwmout_read(pwmout_t* obj) {
group-onsemi 0:098463de4c5d 92 timer_mr tid = pwm_timer_map[obj->pwm];
group-onsemi 0:098463de4c5d 93 LPC_CTxxBx_Type *timer = Timers[tid.timer];
group-onsemi 0:098463de4c5d 94
group-onsemi 0:098463de4c5d 95 float v = (float)(timer->MR3 - timer->MR[tid.mr]) / (float)(timer->MR3);
group-onsemi 0:098463de4c5d 96 return (v > 1.0f) ? (1.0f) : (v);
group-onsemi 0:098463de4c5d 97 }
group-onsemi 0:098463de4c5d 98
group-onsemi 0:098463de4c5d 99 void pwmout_period(pwmout_t* obj, float seconds) {
group-onsemi 0:098463de4c5d 100 pwmout_period_us(obj, seconds * 1000000.0f);
group-onsemi 0:098463de4c5d 101 }
group-onsemi 0:098463de4c5d 102
group-onsemi 0:098463de4c5d 103 void pwmout_period_ms(pwmout_t* obj, int ms) {
group-onsemi 0:098463de4c5d 104 pwmout_period_us(obj, ms * 1000);
group-onsemi 0:098463de4c5d 105 }
group-onsemi 0:098463de4c5d 106
group-onsemi 0:098463de4c5d 107 // Set the PWM period, keeping the duty cycle the same.
group-onsemi 0:098463de4c5d 108 void pwmout_period_us(pwmout_t* obj, int us) {
group-onsemi 0:098463de4c5d 109 int i = 0;
group-onsemi 0:098463de4c5d 110 uint32_t period_ticks = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000);
group-onsemi 0:098463de4c5d 111
group-onsemi 0:098463de4c5d 112 timer_mr tid = pwm_timer_map[obj->pwm];
group-onsemi 0:098463de4c5d 113 LPC_CTxxBx_Type *timer = Timers[tid.timer];
group-onsemi 0:098463de4c5d 114 uint32_t old_period_ticks = timer->MR3;
group-onsemi 0:098463de4c5d 115
group-onsemi 0:098463de4c5d 116 // for 16bit timer, set prescaler to avoid overflow
group-onsemi 0:098463de4c5d 117 if (timer == LPC_CT16B0 || timer == LPC_CT16B1) {
group-onsemi 0:098463de4c5d 118 uint16_t high_period_ticks = period_ticks >> 16;
group-onsemi 0:098463de4c5d 119 timer->PR = high_period_ticks;
group-onsemi 0:098463de4c5d 120 period_ticks /= (high_period_ticks + 1);
group-onsemi 0:098463de4c5d 121 }
group-onsemi 0:098463de4c5d 122
group-onsemi 0:098463de4c5d 123 timer->TCR = TCR_RESET;
group-onsemi 0:098463de4c5d 124 timer->MR3 = period_ticks;
group-onsemi 0:098463de4c5d 125
group-onsemi 0:098463de4c5d 126 // Scale the pulse width to preserve the duty ratio
group-onsemi 0:098463de4c5d 127 if (old_period_ticks > 0) {
group-onsemi 0:098463de4c5d 128 for (i=0; i<3; i++) {
group-onsemi 0:098463de4c5d 129 uint32_t t_off = period_ticks - (uint32_t)(((uint64_t)timer->MR[i] * (uint64_t)period_ticks) / (uint64_t)old_period_ticks);
group-onsemi 0:098463de4c5d 130 timer->MR[i] = t_off;
group-onsemi 0:098463de4c5d 131 }
group-onsemi 0:098463de4c5d 132 }
group-onsemi 0:098463de4c5d 133 timer->TCR = TCR_CNT_EN;
group-onsemi 0:098463de4c5d 134 }
group-onsemi 0:098463de4c5d 135
group-onsemi 0:098463de4c5d 136 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
group-onsemi 0:098463de4c5d 137 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
group-onsemi 0:098463de4c5d 138 }
group-onsemi 0:098463de4c5d 139
group-onsemi 0:098463de4c5d 140 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
group-onsemi 0:098463de4c5d 141 pwmout_pulsewidth_us(obj, ms * 1000);
group-onsemi 0:098463de4c5d 142 }
group-onsemi 0:098463de4c5d 143
group-onsemi 0:098463de4c5d 144 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
group-onsemi 0:098463de4c5d 145 timer_mr tid = pwm_timer_map[obj->pwm];
group-onsemi 0:098463de4c5d 146 LPC_CTxxBx_Type *timer = Timers[tid.timer];
group-onsemi 0:098463de4c5d 147 uint32_t t_on = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000 / (timer->PR + 1));
group-onsemi 0:098463de4c5d 148
group-onsemi 0:098463de4c5d 149 timer->TCR = TCR_RESET;
group-onsemi 0:098463de4c5d 150 if (t_on > timer->MR3) {
group-onsemi 0:098463de4c5d 151 pwmout_period_us(obj, us);
group-onsemi 0:098463de4c5d 152 t_on = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000 / (timer->PR + 1));
group-onsemi 0:098463de4c5d 153 }
group-onsemi 0:098463de4c5d 154 uint32_t t_off = timer->MR3 - t_on;
group-onsemi 0:098463de4c5d 155 timer->MR[tid.mr] = t_off;
group-onsemi 0:098463de4c5d 156 timer->TCR = TCR_CNT_EN;
group-onsemi 0:098463de4c5d 157 }