importing repo

Committer:
mahphalke
Date:
Mon Aug 02 16:03:08 2021 +0530
Revision:
18:5ae03a197e59
Parent:
17:af1f2416dd26
Modified the ADC data capture module to remove dependancy on type of ADC and it's specific operations

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mahphalke 17:af1f2416dd26 1 /***************************************************************************//**
mahphalke 17:af1f2416dd26 2 * @file pwm.cpp
mahphalke 17:af1f2416dd26 3 * @brief Implementation of PWM Mbed platform driver interfaces
mahphalke 17:af1f2416dd26 4 ********************************************************************************
mahphalke 17:af1f2416dd26 5 * Copyright (c) 2021 Analog Devices, Inc.
mahphalke 17:af1f2416dd26 6 *
mahphalke 17:af1f2416dd26 7 * All rights reserved.
mahphalke 17:af1f2416dd26 8 *
mahphalke 17:af1f2416dd26 9 * This software is proprietary to Analog Devices, Inc. and its licensors.
mahphalke 17:af1f2416dd26 10 * By using this software you agree to the terms of the associated
mahphalke 17:af1f2416dd26 11 * Analog Devices Software License Agreement.
mahphalke 17:af1f2416dd26 12 *******************************************************************************/
mahphalke 17:af1f2416dd26 13
mahphalke 17:af1f2416dd26 14 /******************************************************************************/
mahphalke 17:af1f2416dd26 15 /***************************** Include Files **********************************/
mahphalke 17:af1f2416dd26 16 /******************************************************************************/
mahphalke 17:af1f2416dd26 17
mahphalke 17:af1f2416dd26 18 #include <stdio.h>
mahphalke 17:af1f2416dd26 19 #include <mbed.h>
mahphalke 17:af1f2416dd26 20
mahphalke 17:af1f2416dd26 21 // Platform drivers needs to be C-compatible to work with other drivers
mahphalke 17:af1f2416dd26 22 #ifdef __cplusplus
mahphalke 17:af1f2416dd26 23 extern "C"
mahphalke 17:af1f2416dd26 24 {
mahphalke 17:af1f2416dd26 25 #endif // _cplusplus
mahphalke 17:af1f2416dd26 26
mahphalke 17:af1f2416dd26 27 #include "error.h"
mahphalke 17:af1f2416dd26 28 #include "pwm.h"
mahphalke 17:af1f2416dd26 29 #include "gpio.h"
mahphalke 17:af1f2416dd26 30 #include "pwm_extra.h"
mahphalke 17:af1f2416dd26 31
mahphalke 17:af1f2416dd26 32 /******************************************************************************/
mahphalke 17:af1f2416dd26 33 /********************** Macros and Constants Definitions **********************/
mahphalke 17:af1f2416dd26 34 /******************************************************************************/
mahphalke 17:af1f2416dd26 35
mahphalke 17:af1f2416dd26 36 /******************************************************************************/
mahphalke 17:af1f2416dd26 37 /********************** Variables and User defined data types *****************/
mahphalke 17:af1f2416dd26 38 /******************************************************************************/
mahphalke 17:af1f2416dd26 39
mahphalke 17:af1f2416dd26 40 /******************************************************************************/
mahphalke 17:af1f2416dd26 41 /************************ Functions Declarations ******************************/
mahphalke 17:af1f2416dd26 42 /******************************************************************************/
mahphalke 17:af1f2416dd26 43
mahphalke 17:af1f2416dd26 44 /******************************************************************************/
mahphalke 17:af1f2416dd26 45 /************************ Functions Definitions *******************************/
mahphalke 17:af1f2416dd26 46 /******************************************************************************/
mahphalke 17:af1f2416dd26 47
mahphalke 17:af1f2416dd26 48 /**
mahphalke 17:af1f2416dd26 49 * @brief Initialized the PWM interface
mahphalke 17:af1f2416dd26 50 * @param desc[in, out] - Pointer where the configured instance is stored
mahphalke 17:af1f2416dd26 51 * @param param[in] - Configuration information for the instance
mahphalke 17:af1f2416dd26 52 * @return SUCCESS in case of success, FAILURE otherwise.
mahphalke 17:af1f2416dd26 53 */
mahphalke 17:af1f2416dd26 54 int32_t pwm_init(struct pwm_desc **desc,
mahphalke 17:af1f2416dd26 55 const struct pwm_init_param *param)
mahphalke 17:af1f2416dd26 56 {
mahphalke 17:af1f2416dd26 57 struct pwm_desc *new_pwm_desc;
mahphalke 17:af1f2416dd26 58 mbed::PwmOut *pwm;
mahphalke 17:af1f2416dd26 59 struct mbed_pwm_desc *new_mbed_pwm_desc;
mahphalke 17:af1f2416dd26 60
mahphalke 17:af1f2416dd26 61 if (!desc || !param) {
mahphalke 17:af1f2416dd26 62 return FAILURE;
mahphalke 17:af1f2416dd26 63 }
mahphalke 17:af1f2416dd26 64
mahphalke 17:af1f2416dd26 65 /* Allocate memory for general PWM descriptor */
mahphalke 17:af1f2416dd26 66 new_pwm_desc = (pwm_desc *)malloc(sizeof(pwm_desc));
mahphalke 17:af1f2416dd26 67 if (!new_pwm_desc) {
mahphalke 17:af1f2416dd26 68 goto err_new_pwm_desc;
mahphalke 17:af1f2416dd26 69 }
mahphalke 17:af1f2416dd26 70
mahphalke 17:af1f2416dd26 71 new_pwm_desc->id = param->id; // Pin number
mahphalke 17:af1f2416dd26 72 new_pwm_desc->period_ns = param->period_ns; // PWM period
mahphalke 17:af1f2416dd26 73
mahphalke 17:af1f2416dd26 74 /* Create and initialize Mbed PWM object */
mahphalke 17:af1f2416dd26 75 pwm = new PwmOut((PinName)param->id);
mahphalke 17:af1f2416dd26 76 if (!pwm) {
mahphalke 17:af1f2416dd26 77 goto err_pwm;
mahphalke 17:af1f2416dd26 78 }
mahphalke 17:af1f2416dd26 79
mahphalke 17:af1f2416dd26 80 /* Allocate memory for Mbed specific PWM descriptor for future use */
mahphalke 17:af1f2416dd26 81 new_mbed_pwm_desc = (mbed_pwm_desc *)malloc(sizeof(mbed_pwm_desc));
mahphalke 17:af1f2416dd26 82 if (!new_mbed_pwm_desc) {
mahphalke 17:af1f2416dd26 83 goto err_new_mbed_pwm_desc;
mahphalke 17:af1f2416dd26 84 }
mahphalke 17:af1f2416dd26 85
mahphalke 17:af1f2416dd26 86 new_mbed_pwm_desc->pwm_obj = (mbed::PwmOut *)pwm;
mahphalke 17:af1f2416dd26 87 new_pwm_desc->extra = (mbed_pwm_desc *)new_mbed_pwm_desc;
mahphalke 17:af1f2416dd26 88
mahphalke 17:af1f2416dd26 89 /* Configure Mbed PWM parameters */
mahphalke 17:af1f2416dd26 90 pwm->period_us(param->period_ns / 1000); // Period in usec
mahphalke 17:af1f2416dd26 91 pwm->pulsewidth_us(param->duty_cycle_ns / 1000); // Duty cycle in usec
mahphalke 17:af1f2416dd26 92 pwm_disable(new_pwm_desc);
mahphalke 17:af1f2416dd26 93
mahphalke 17:af1f2416dd26 94 *desc = new_pwm_desc;
mahphalke 17:af1f2416dd26 95
mahphalke 17:af1f2416dd26 96 return SUCCESS;
mahphalke 17:af1f2416dd26 97
mahphalke 17:af1f2416dd26 98 err_new_mbed_pwm_desc:
mahphalke 17:af1f2416dd26 99 free(pwm);
mahphalke 17:af1f2416dd26 100 err_pwm:
mahphalke 17:af1f2416dd26 101 free(new_pwm_desc);
mahphalke 17:af1f2416dd26 102 err_new_pwm_desc:
mahphalke 17:af1f2416dd26 103 // Nothing to free
mahphalke 17:af1f2416dd26 104
mahphalke 17:af1f2416dd26 105 return FAILURE;
mahphalke 17:af1f2416dd26 106 }
mahphalke 17:af1f2416dd26 107
mahphalke 17:af1f2416dd26 108
mahphalke 17:af1f2416dd26 109 /**
mahphalke 17:af1f2416dd26 110 * @brief Enable the PWM interface
mahphalke 17:af1f2416dd26 111 * @param desc[in, out] - Pointer where the configured instance is stored
mahphalke 17:af1f2416dd26 112 * @return SUCCESS in case of success, FAILURE otherwise.
mahphalke 17:af1f2416dd26 113 */
mahphalke 17:af1f2416dd26 114 int32_t pwm_enable(struct pwm_desc *desc)
mahphalke 17:af1f2416dd26 115 {
mahphalke 17:af1f2416dd26 116 mbed::PwmOut *pwm;
mahphalke 17:af1f2416dd26 117
mahphalke 17:af1f2416dd26 118 if (desc) {
mahphalke 17:af1f2416dd26 119 pwm = (mbed::PwmOut *)(((mbed_pwm_desc *)desc->extra)->pwm_obj);
mahphalke 17:af1f2416dd26 120 if (!pwm) {
mahphalke 17:af1f2416dd26 121 return FAILURE;
mahphalke 17:af1f2416dd26 122 }
mahphalke 17:af1f2416dd26 123
mahphalke 17:af1f2416dd26 124 pwm->resume();
mahphalke 17:af1f2416dd26 125
mahphalke 17:af1f2416dd26 126 return SUCCESS;
mahphalke 17:af1f2416dd26 127 }
mahphalke 17:af1f2416dd26 128
mahphalke 17:af1f2416dd26 129 return FAILURE;
mahphalke 17:af1f2416dd26 130 }
mahphalke 17:af1f2416dd26 131
mahphalke 17:af1f2416dd26 132
mahphalke 17:af1f2416dd26 133 /**
mahphalke 17:af1f2416dd26 134 * @brief Disable the PWM interface
mahphalke 17:af1f2416dd26 135 * @param desc[in, out] - Pointer where the configured instance is stored
mahphalke 17:af1f2416dd26 136 * @return SUCCESS in case of success, FAILURE otherwise.
mahphalke 17:af1f2416dd26 137 */
mahphalke 17:af1f2416dd26 138 int32_t pwm_disable(struct pwm_desc *desc)
mahphalke 17:af1f2416dd26 139 {
mahphalke 17:af1f2416dd26 140 mbed::PwmOut *pwm;
mahphalke 17:af1f2416dd26 141
mahphalke 17:af1f2416dd26 142 if (desc) {
mahphalke 17:af1f2416dd26 143 pwm = (mbed::PwmOut *)(((mbed_pwm_desc *)desc->extra)->pwm_obj);
mahphalke 17:af1f2416dd26 144
mahphalke 17:af1f2416dd26 145 if (!pwm) {
mahphalke 17:af1f2416dd26 146 return FAILURE;
mahphalke 17:af1f2416dd26 147 }
mahphalke 17:af1f2416dd26 148
mahphalke 17:af1f2416dd26 149 pwm->suspend();
mahphalke 17:af1f2416dd26 150
mahphalke 17:af1f2416dd26 151 return SUCCESS;
mahphalke 17:af1f2416dd26 152 }
mahphalke 17:af1f2416dd26 153
mahphalke 17:af1f2416dd26 154 return FAILURE;
mahphalke 17:af1f2416dd26 155 }
mahphalke 17:af1f2416dd26 156
mahphalke 17:af1f2416dd26 157
mahphalke 17:af1f2416dd26 158 /**
mahphalke 17:af1f2416dd26 159 * @brief Remove the memory allocated for PWM device descriptors
mahphalke 17:af1f2416dd26 160 * @param desc[in, out] - Pointer where the configured instance is stored
mahphalke 17:af1f2416dd26 161 * @return SUCCESS in case of success, FAILURE otherwise.
mahphalke 17:af1f2416dd26 162 */
mahphalke 17:af1f2416dd26 163 int32_t pwm_remove(struct pwm_desc *desc)
mahphalke 17:af1f2416dd26 164 {
mahphalke 17:af1f2416dd26 165 if (!desc) {
mahphalke 17:af1f2416dd26 166 return FAILURE;
mahphalke 17:af1f2416dd26 167 }
mahphalke 17:af1f2416dd26 168
mahphalke 17:af1f2416dd26 169 if (((mbed_pwm_desc *)desc->extra)->pwm_obj) {
mahphalke 17:af1f2416dd26 170 delete((mbed::PwmOut *)((mbed_pwm_desc *)desc->extra)->pwm_obj);
mahphalke 17:af1f2416dd26 171 }
mahphalke 17:af1f2416dd26 172
mahphalke 17:af1f2416dd26 173 free(desc);
mahphalke 17:af1f2416dd26 174
mahphalke 17:af1f2416dd26 175 return SUCCESS;
mahphalke 17:af1f2416dd26 176 }
mahphalke 17:af1f2416dd26 177
mahphalke 17:af1f2416dd26 178
mahphalke 17:af1f2416dd26 179 #ifdef __cplusplus // Closing extern c
mahphalke 17:af1f2416dd26 180 }
mahphalke 17:af1f2416dd26 181 #endif // _cplusplus