foxBMS  1.2.1
The foxBMS Battery Management System API Documentation
pwm.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2021, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * We kindly request you to use one or more of the following phrases to refer to
34  * foxBMS in your hardware, software, documentation or advertising materials:
35  *
36  * - ″This product uses parts of foxBMS®″
37  * - ″This product includes parts of foxBMS®″
38  * - ″This product is derived from foxBMS®″
39  *
40  */
41 
42 /**
43  * @file pwm.c
44  * @author foxBMS Team
45  * @date 2021-10-07 (date of creation)
46  * @updated 2021-10-08 (date of last update)
47  * @ingroup DRIVERS
48  * @prefix PWM
49  *
50  * @brief Implementation of the PWM module.
51  *
52  */
53 
54 /*========== Includes =======================================================*/
55 #include "pwm.h"
56 
57 #include "HL_etpwm.h"
58 
59 #include "fsystem.h"
60 
61 /*========== Macros and Definitions =========================================*/
62 /** lower threshold permill */
63 #define PWM_LOWER_THRESHOLD_PERM (1u)
64 
65 /** upper threshold permill */
66 #define PWM_UPPER_THRESHOLD_PERM (999u)
67 
68 /** full period in promill */
69 #define PWM_FULL_PERIOD_PERM (1000u)
70 
71 /*========== Static Constant and Variable Definitions =======================*/
72 /** linear offset (through output circuit) */
73 static const int16_t pwm_kLinearOffset = 0;
74 
75 /*========== Extern Constant and Variable Definitions =======================*/
76 
77 /*========== Static Function Prototypes =====================================*/
78 /** returns the ePWM time period (currently for ePWM1A)
79  * @returns ePWM time period in counter ticks
80  */
81 static uint16_t PWM_GetEtpwmTimePeriod(void);
82 
83 /** returns a ePWM counter value based on a duty cycle
84  * @param[in] dutyCycle_perm duty cycle in permill
85  * @returns counter value
86  */
87 static uint16_t PWM_ComputeCounterValueFromDutyCycle(uint16_t dutyCycle_perm);
88 
89 /*========== Static Function Implementations ================================*/
90 static uint16_t PWM_GetEtpwmTimePeriod(void) {
91  etpwm_config_reg_t etPwmConfig = {0};
92  etpwm1GetConfigValue(&etPwmConfig, CurrentValue); /* retrieve the current config */
93  return etPwmConfig.CONFIG_TBPRD;
94 }
95 
96 static uint16_t PWM_ComputeCounterValueFromDutyCycle(uint16_t dutyCycle_perm) {
97  FAS_ASSERT((dutyCycle_perm >= PWM_LOWER_THRESHOLD_PERM) && (dutyCycle_perm <= PWM_UPPER_THRESHOLD_PERM));
98 
99  uint16_t basePeriod = PWM_GetEtpwmTimePeriod();
100  uint32_t counterValue = (((uint32_t)basePeriod * (uint32_t)dutyCycle_perm) / PWM_FULL_PERIOD_PERM);
101 
102  FAS_ASSERT(counterValue <= (uint16_t)UINT16_MAX);
103  return (uint16_t)counterValue;
104 }
105 
106 /*========== Extern Function Implementations ================================*/
107 extern void PWM_StartPwm(void) {
108  /* go to privileged mode in order to access control register */
110  etpwmStartTBCLK();
111  /* done; go back to user mode */
113 }
114 
115 extern void PWM_StopPwm(void) {
116  /* go to privileged mode in order to access control register */
118  etpwmStopTBCLK();
119  /* done; go back to user mode */
121 }
122 
123 extern void PWM_SetDutyCycle(uint16_t dutyCycle_perm) {
124  FAS_ASSERT(dutyCycle_perm <= (uint16_t)INT16_MAX);
125  int16_t intermediateDutyCycle_perm = (int16_t)dutyCycle_perm + pwm_kLinearOffset;
126  /* prevent wrap-around */
127  if (intermediateDutyCycle_perm < 0) {
128  intermediateDutyCycle_perm = 0;
129  }
130  uint16_t correctedDutyCycle_perm = (uint16_t)intermediateDutyCycle_perm;
131 
132  if (correctedDutyCycle_perm < PWM_LOWER_THRESHOLD_PERM) {
133  correctedDutyCycle_perm = PWM_LOWER_THRESHOLD_PERM;
134  }
135 
136  if (correctedDutyCycle_perm > PWM_UPPER_THRESHOLD_PERM) {
137  correctedDutyCycle_perm = PWM_UPPER_THRESHOLD_PERM;
138  }
139 
140  etpwmSetCmpA(etpwmREG1, PWM_ComputeCounterValueFromDutyCycle(correctedDutyCycle_perm));
141 }
142 
143 /*========== Getter for static Variables (Unit Test) ========================*/
144 #ifdef UNITY_UNIT_TEST
145 extern int16_t TEST_PWM_GetLinearOffset(void) {
146  return pwm_kLinearOffset;
147 }
148 #endif
149 
150 /*========== Externalized Static Function Implementations (Unit Test) =======*/
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:239
Function to switch between user mode and privilege mode.
#define FSYS_SwitchToUserMode()
Switch back to user mode.
Definition: fsystem.h:110
long FSYS_RaisePrivilege(void)
raise privilege
void PWM_SetDutyCycle(uint16_t dutyCycle_perm)
Set the duty cycle of the PWM (currently only channel 1A)
Definition: pwm.c:123
void PWM_StopPwm(void)
Stop the PWM (stops all configured ePWM channels)
Definition: pwm.c:115
static uint16_t PWM_GetEtpwmTimePeriod(void)
Definition: pwm.c:90
#define PWM_UPPER_THRESHOLD_PERM
Definition: pwm.c:66
#define PWM_FULL_PERIOD_PERM
Definition: pwm.c:69
static uint16_t PWM_ComputeCounterValueFromDutyCycle(uint16_t dutyCycle_perm)
Definition: pwm.c:96
void PWM_StartPwm(void)
Start the PWM (starts all configured ePWM channels)
Definition: pwm.c:107
#define PWM_LOWER_THRESHOLD_PERM
Definition: pwm.c:63
static const int16_t pwm_kLinearOffset
Definition: pwm.c:73
PWM driver for the TMS570LC43xx.