mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by Umar Naeem

Committer:
ranaumarnaeem
Date:
Tue May 23 12:54:50 2017 +0000
Revision:
165:2dd56e6daeec
Parent:
153:fa9ff456f731
jhjg

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 144:ef7eb2e8f9f7 1 /**
<> 144:ef7eb2e8f9f7 2 ******************************************************************************
<> 144:ef7eb2e8f9f7 3 * @file pwmout_api.c
<> 144:ef7eb2e8f9f7 4 * @brief Implementation of a PWM driver
<> 144:ef7eb2e8f9f7 5 * @internal
<> 144:ef7eb2e8f9f7 6 * @author ON Semiconductor
<> 144:ef7eb2e8f9f7 7 * $Rev:
<> 144:ef7eb2e8f9f7 8 * $Date:
<> 144:ef7eb2e8f9f7 9 ******************************************************************************
<> 147:30b64687e01f 10 * Copyright 2016 Semiconductor Components Industries LLC (d/b/a “ON Semiconductor”).
<> 147:30b64687e01f 11 * All rights reserved. This software and/or documentation is licensed by ON Semiconductor
<> 147:30b64687e01f 12 * under limited terms and conditions. The terms and conditions pertaining to the software
<> 147:30b64687e01f 13 * and/or documentation are available at http://www.onsemi.com/site/pdf/ONSEMI_T&C.pdf
<> 147:30b64687e01f 14 * (“ON Semiconductor Standard Terms and Conditions of Sale, Section 8 Software”) and
<> 147:30b64687e01f 15 * if applicable the software license agreement. Do not use this software and/or
<> 147:30b64687e01f 16 * documentation unless you have carefully read and you agree to the limited terms and
<> 147:30b64687e01f 17 * conditions. By using this software and/or documentation, you agree to the limited
<> 147:30b64687e01f 18 * terms and conditions.
<> 144:ef7eb2e8f9f7 19 *
<> 144:ef7eb2e8f9f7 20 * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
<> 144:ef7eb2e8f9f7 21 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
<> 144:ef7eb2e8f9f7 22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
<> 144:ef7eb2e8f9f7 23 * ON SEMICONDUCTOR SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL,
<> 144:ef7eb2e8f9f7 24 * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
<> 144:ef7eb2e8f9f7 25 * @endinternal
<> 144:ef7eb2e8f9f7 26 */
<> 144:ef7eb2e8f9f7 27 #include "pwmout_api.h"
<> 144:ef7eb2e8f9f7 28 #include "PeripheralPins.h"
<> 144:ef7eb2e8f9f7 29 #include "mbed_assert.h"
<> 144:ef7eb2e8f9f7 30 #include "clock.h"
<> 144:ef7eb2e8f9f7 31
<> 144:ef7eb2e8f9f7 32 #if DEVICE_PWMOUT
<> 144:ef7eb2e8f9f7 33
<> 144:ef7eb2e8f9f7 34 /**
<> 144:ef7eb2e8f9f7 35 * \defgroup hal_pwmout Pwmout hal functions
<> 144:ef7eb2e8f9f7 36 * @{
<> 144:ef7eb2e8f9f7 37 */
<> 144:ef7eb2e8f9f7 38
<> 144:ef7eb2e8f9f7 39 /** Initialize the pwm out peripheral and configure the pin
<> 144:ef7eb2e8f9f7 40 *
<> 144:ef7eb2e8f9f7 41 * @param obj The pwmout object to initialize
<> 144:ef7eb2e8f9f7 42 * @param pin The pwmout pin to initialize
<> 144:ef7eb2e8f9f7 43 */
<> 144:ef7eb2e8f9f7 44 void pwmout_init(pwmout_t *obj, PinName pin)
<> 144:ef7eb2e8f9f7 45 {
<> 144:ef7eb2e8f9f7 46 /* Get the base address of the PWM register using the pinmap functions ; pwmout_s struct contains base address only */
<> 144:ef7eb2e8f9f7 47 PWMName pwm;
<> 144:ef7eb2e8f9f7 48
<> 144:ef7eb2e8f9f7 49 pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
<> 144:ef7eb2e8f9f7 50 MBED_ASSERT(pwm != (PWMName)NC);
<> 144:ef7eb2e8f9f7 51
<> 144:ef7eb2e8f9f7 52 pinmap_pinout(pin, PinMap_PWM);
<> 144:ef7eb2e8f9f7 53
<> 144:ef7eb2e8f9f7 54 obj->pwmReg = (PwmReg_pt)pwm;
<> 144:ef7eb2e8f9f7 55 MBED_ASSERT(obj->pwmReg != 0x00000000);
<> 144:ef7eb2e8f9f7 56
<> 144:ef7eb2e8f9f7 57 CLOCK_ENABLE(CLOCK_PWM);
<> 144:ef7eb2e8f9f7 58
<> 144:ef7eb2e8f9f7 59 /* Configuration parameters of duty cycle 0x4000B000, and prescaler 0x4000B00C, shall be set to default values */
<> 144:ef7eb2e8f9f7 60 /* Duty cycle shall be 50% and prescaler shall be disabled by default */
<> 144:ef7eb2e8f9f7 61 obj->pwmReg->DUTYCYCLE = 0x80;
<> 144:ef7eb2e8f9f7 62
<> 144:ef7eb2e8f9f7 63 /* Write the PWM output enable register 0x4000B004, to 1 */
<> 153:fa9ff456f731 64 obj->pwmReg->PWM_ENABLE = 0x1;
<> 144:ef7eb2e8f9f7 65
<> 144:ef7eb2e8f9f7 66 obj->pwmReg->PRESCALE_DISABLE = 0x1;
<> 144:ef7eb2e8f9f7 67
<> 144:ef7eb2e8f9f7 68 }
<> 144:ef7eb2e8f9f7 69
<> 144:ef7eb2e8f9f7 70 /** Deinitialize the pwmout object
<> 144:ef7eb2e8f9f7 71 *
<> 144:ef7eb2e8f9f7 72 * @param obj The pwmout object
<> 144:ef7eb2e8f9f7 73 */
<> 144:ef7eb2e8f9f7 74 void pwmout_free(pwmout_t *obj)
<> 144:ef7eb2e8f9f7 75 {
<> 144:ef7eb2e8f9f7 76 /* Write the PWM output disable register 0x4000B008, to 1 */
<> 144:ef7eb2e8f9f7 77 obj->pwmReg->PWM_DISABLE = 0x1;
<> 144:ef7eb2e8f9f7 78 }
<> 144:ef7eb2e8f9f7 79
<> 144:ef7eb2e8f9f7 80 /** Set the output duty-cycle in range <0.0f, 1.0f>
<> 144:ef7eb2e8f9f7 81 *
<> 144:ef7eb2e8f9f7 82 * Value 0.0f represents 0 percentage, 1.0f represents 100 percent.
<> 144:ef7eb2e8f9f7 83 * @param obj The pwmout object
<> 144:ef7eb2e8f9f7 84 * @param percent The floating-point percentage number
<> 144:ef7eb2e8f9f7 85 */
<> 144:ef7eb2e8f9f7 86 void pwmout_write(pwmout_t *obj, float percent)
<> 144:ef7eb2e8f9f7 87 {
<> 144:ef7eb2e8f9f7 88 if (percent == 0.0) {
<> 144:ef7eb2e8f9f7 89 obj->pwmReg->DUTYCYCLE = 0x00;
<> 144:ef7eb2e8f9f7 90 } else if (percent == 1.0) {
<> 144:ef7eb2e8f9f7 91 obj->pwmReg->DUTYCYCLE = 0xFF;
<> 144:ef7eb2e8f9f7 92 } else {
<> 144:ef7eb2e8f9f7 93 /* Write the duty cycle config register 0x4000B000, with the value passed on */
<> 144:ef7eb2e8f9f7 94 /* ((percent * 255) + 1) is the duty cycle. Plus 1 is for accounting for round off errors; like a ceil function */
<> 144:ef7eb2e8f9f7 95 obj->pwmReg->DUTYCYCLE = (uint8_t)((percent * 255) + 1);
<> 144:ef7eb2e8f9f7 96 }
<> 144:ef7eb2e8f9f7 97 }
<> 144:ef7eb2e8f9f7 98
<> 144:ef7eb2e8f9f7 99 /** Read the current float-point output duty-cycle
<> 144:ef7eb2e8f9f7 100 *
<> 144:ef7eb2e8f9f7 101 * @param obj The pwmout object
<> 144:ef7eb2e8f9f7 102 * @return A floating-point output duty-cycle
<> 144:ef7eb2e8f9f7 103 */
<> 144:ef7eb2e8f9f7 104 float pwmout_read(pwmout_t *obj)
<> 144:ef7eb2e8f9f7 105 {
<> 144:ef7eb2e8f9f7 106 float retVal = 0.0;
<> 144:ef7eb2e8f9f7 107 float dc = 0.0;
<> 144:ef7eb2e8f9f7 108
<> 144:ef7eb2e8f9f7 109 /* Read out the value of duty cycle register 0x4000B000 and return as a percent */
<> 144:ef7eb2e8f9f7 110 /* Read value / 255 is the percent returned */
<> 144:ef7eb2e8f9f7 111 dc = obj->pwmReg->DUTYCYCLE;
<> 144:ef7eb2e8f9f7 112 retVal = dc/ (float)255;
<> 144:ef7eb2e8f9f7 113
<> 144:ef7eb2e8f9f7 114 return(retVal);
<> 144:ef7eb2e8f9f7 115 }
<> 144:ef7eb2e8f9f7 116
<> 144:ef7eb2e8f9f7 117 /** Set the PWM period specified in seconds, keeping the duty cycle the same
<> 144:ef7eb2e8f9f7 118 *
<> 144:ef7eb2e8f9f7 119 * Periods smaller than microseconds (the lowest resolution) are set to zero.
<> 144:ef7eb2e8f9f7 120 * @param obj The pwmout object
<> 144:ef7eb2e8f9f7 121 * @param seconds The floating-point seconds period
<> 144:ef7eb2e8f9f7 122 */
<> 144:ef7eb2e8f9f7 123 void pwmout_period(pwmout_t *obj, float seconds)
<> 144:ef7eb2e8f9f7 124 {
<> 144:ef7eb2e8f9f7 125 /* Cannot be configured, prescaler is either 256 or 4096 */
<> 144:ef7eb2e8f9f7 126 return;
<> 144:ef7eb2e8f9f7 127 }
<> 144:ef7eb2e8f9f7 128
<> 144:ef7eb2e8f9f7 129 /** Set the PWM period specified in miliseconds, keeping the duty cycle the same
<> 144:ef7eb2e8f9f7 130 *
<> 144:ef7eb2e8f9f7 131 * @param obj The pwmout object
<> 144:ef7eb2e8f9f7 132 * @param ms The milisecond period
<> 144:ef7eb2e8f9f7 133 */
<> 144:ef7eb2e8f9f7 134 void pwmout_period_ms(pwmout_t *obj, int ms)
<> 144:ef7eb2e8f9f7 135 {
<> 144:ef7eb2e8f9f7 136 /* Cannot be configured, prescaler is either 256 or 4096 */
<> 144:ef7eb2e8f9f7 137 return;
<> 144:ef7eb2e8f9f7 138 }
<> 144:ef7eb2e8f9f7 139
<> 144:ef7eb2e8f9f7 140 /** Set the PWM period specified in microseconds, keeping the duty cycle the same
<> 144:ef7eb2e8f9f7 141 *
<> 144:ef7eb2e8f9f7 142 * @param obj The pwmout object
<> 144:ef7eb2e8f9f7 143 * @param us The microsecond period
<> 144:ef7eb2e8f9f7 144 */
<> 144:ef7eb2e8f9f7 145 void pwmout_period_us(pwmout_t *obj, int us)
<> 144:ef7eb2e8f9f7 146 {
<> 144:ef7eb2e8f9f7 147 /* Cannot be configured, prescaler is either 256 or 4096 */
<> 144:ef7eb2e8f9f7 148 return;
<> 144:ef7eb2e8f9f7 149 }
<> 144:ef7eb2e8f9f7 150
<> 144:ef7eb2e8f9f7 151 /** Set the PWM pulsewidth specified in seconds, keeping the period the same.
<> 144:ef7eb2e8f9f7 152 *
<> 144:ef7eb2e8f9f7 153 * @param obj The pwmout object
<> 144:ef7eb2e8f9f7 154 * @param seconds The floating-point pulsewidth in seconds
<> 144:ef7eb2e8f9f7 155 */
<> 144:ef7eb2e8f9f7 156 void pwmout_pulsewidth(pwmout_t *obj, float seconds)
<> 144:ef7eb2e8f9f7 157 {
<> 144:ef7eb2e8f9f7 158 /* Pulse width can never be in seconds since the period
<> 144:ef7eb2e8f9f7 159 * itself is limited to either 8uSec or 128uSec
<> 144:ef7eb2e8f9f7 160 */
<> 144:ef7eb2e8f9f7 161 return;
<> 144:ef7eb2e8f9f7 162 }
<> 144:ef7eb2e8f9f7 163
<> 144:ef7eb2e8f9f7 164 /** Set the PWM pulsewidth specified in miliseconds, keeping the period the same.
<> 144:ef7eb2e8f9f7 165 *
<> 144:ef7eb2e8f9f7 166 * @param obj The pwmout object
<> 144:ef7eb2e8f9f7 167 * @param ms The floating-point pulsewidth in miliseconds
<> 144:ef7eb2e8f9f7 168 */
<> 144:ef7eb2e8f9f7 169 void pwmout_pulsewidth_ms(pwmout_t *obj, int ms)
<> 144:ef7eb2e8f9f7 170 {
<> 144:ef7eb2e8f9f7 171
<> 144:ef7eb2e8f9f7 172 /* Pulse width can never be in seconds since the period
<> 144:ef7eb2e8f9f7 173 * itself is limited to either 8uSec or 128uSec
<> 144:ef7eb2e8f9f7 174 */
<> 144:ef7eb2e8f9f7 175 return;
<> 144:ef7eb2e8f9f7 176 }
<> 144:ef7eb2e8f9f7 177
<> 144:ef7eb2e8f9f7 178 /** Set the PWM pulsewidth specified in microseconds, keeping the period the same.
<> 144:ef7eb2e8f9f7 179 *
<> 144:ef7eb2e8f9f7 180 * @param obj The pwmout object
<> 144:ef7eb2e8f9f7 181 * @param us The floating-point pulsewidth in microseconds
<> 144:ef7eb2e8f9f7 182 */
<> 144:ef7eb2e8f9f7 183 void pwmout_pulsewidth_us(pwmout_t *obj, int us)
<> 144:ef7eb2e8f9f7 184 {
<> 144:ef7eb2e8f9f7 185 int pulseWidth = 0;
<> 144:ef7eb2e8f9f7 186
<> 144:ef7eb2e8f9f7 187 /* Check if the uSec value is greater than 128uSec, if so reject */
<> 144:ef7eb2e8f9f7 188 if (us > 128) {
<> 144:ef7eb2e8f9f7 189 return;
<> 144:ef7eb2e8f9f7 190 }
<> 144:ef7eb2e8f9f7 191 /* If pulsewidth is less than 128uSec, set the prescaler to 4096
<> 144:ef7eb2e8f9f7 192 * by enabling prescale register 0x4000B00C to 1 */
<> 153:fa9ff456f731 193 obj->pwmReg->PRESCALE_ENABLE = 0x1;
<> 144:ef7eb2e8f9f7 194
<> 144:ef7eb2e8f9f7 195 /* Calculate the duty cycle based on the width of the pulse */
<> 144:ef7eb2e8f9f7 196 /* ((255 * us) / 128) + 1 = duty cycle */
<> 144:ef7eb2e8f9f7 197 pulseWidth = (int)((float)(255 * us)/(float)128) + 1;
<> 144:ef7eb2e8f9f7 198 if (us == 0) {
<> 144:ef7eb2e8f9f7 199 obj->pwmReg->DUTYCYCLE = 0x0;
<> 144:ef7eb2e8f9f7 200 } else if (us == 128) {
<> 144:ef7eb2e8f9f7 201 obj->pwmReg->DUTYCYCLE = 0xFF;
<> 144:ef7eb2e8f9f7 202 } else {
<> 144:ef7eb2e8f9f7 203 obj->pwmReg->DUTYCYCLE = (uint8_t)pulseWidth;
<> 144:ef7eb2e8f9f7 204 }
<> 144:ef7eb2e8f9f7 205 }
<> 144:ef7eb2e8f9f7 206
<> 144:ef7eb2e8f9f7 207 /**@}*/
<> 144:ef7eb2e8f9f7 208
<> 144:ef7eb2e8f9f7 209 #endif // DEVICE_PWMOUT