foxBMS - Unit Tests  1.5.0
The foxBMS Unit Tests API Documentation
test_pwm.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2023, 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 test_pwm.c
44  * @author foxBMS Team
45  * @date 2021-10-08 (date of creation)
46  * @updated 2023-02-03 (date of last update)
47  * @version v1.5.0
48  * @ingroup UNIT_TEST_IMPLEMENTATION
49  * @prefix TEST
50  *
51  * @brief Tests for the PWM driver
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "unity.h"
57 #include "MockHL_etpwm.h"
58 
59 /* HL_ecap.h can not be mocked, due to 'ecapNotification' is implemented in
60  'pwm.c' and this would then lead to multiple defintions.
61  The solution is to only include the real HAL header, and then implement
62  stubs for the required functions (ecapInit, ecapGetCAP1, ecapGetCAP2,
63  ecapGetCAP3). */
64 #include "HL_ecap.h"
65 
66 #include "pwm.h"
67 #include "test_assert_helper.h"
68 
69 /*========== Definitions and Implementations for Unit Test ==================*/
70 static uint8_t fsysRaisePrivilegeReturnValue = 0u;
71 long FSYS_RaisePrivilege(void) {
73 }
74 
75 void ecapInit(void){};
76 
77 uint32_t ecapGetCAP1(ecapBASE_t *ecap) {
78  return ecap->CAP1;
79 }
80 uint32_t ecapGetCAP2(ecapBASE_t *ecap) {
81  return ecap->CAP2;
82 }
83 uint32_t ecapGetCAP3(ecapBASE_t *ecap) {
84  return ecap->CAP3;
85 }
86 /** wraps the duty cycle function for test
87  *
88  * The output duty cycle is tested against the value computed by the function.
89  *
90  * @param[in] timeBasePeriod This value will be injected as a time base period
91  * @param[in] dutyCycleIn This value will be passed as requested duty cycle in permill
92  * @param[in] dutyCycleOut The expected output duty cycle (in counts)
93  */
94 void PWM_SetDutyCycle_Test(uint16_t timeBasePeriod, uint16_t dutyCycleIn, uint16_t dutyCycleOut) {
95  etpwm_config_reg_t etPwmConfig = {0};
96  etPwmConfig.CONFIG_TBPRD = timeBasePeriod;
97 
98  etpwm1GetConfigValue_Expect(NULL_PTR, CurrentValue);
99  etpwm1GetConfigValue_IgnoreArg_config_reg();
100  etpwm1GetConfigValue_ReturnThruPtr_config_reg(&etPwmConfig);
101 
102  etpwmSetCmpA_Expect(NULL_PTR, dutyCycleOut);
103  etpwmSetCmpA_IgnoreArg_etpwm();
104  PWM_SetDutyCycle(dutyCycleIn);
105 }
106 
107 /** calculates the PWM counter value
108  *
109  * @param[in] timeBasePeriod This time base period will be assumed for the calculation
110  * @param[in] dutyCycle The requested duty cycle in permill
111  */
112 uint16_t calculateCounterValue(uint16_t timeBasePeriod, uint16_t dutyCycle) {
113  /* retrieves the tuning value from the module */
114  int16_t linearOffset = TEST_PWM_GetLinearOffset();
115 
116  double correctedDutyCycle = ((double)dutyCycle + linearOffset);
117  if (correctedDutyCycle < 0) {
118  correctedDutyCycle = 0;
119  }
120 
121  /* bound to upper and lower threshold */
122  if (correctedDutyCycle < 1) {
123  correctedDutyCycle = 1;
124  }
125  if (correctedDutyCycle > 999) {
126  correctedDutyCycle = 999;
127  }
128 
129  uint16_t counterSteps = (((double)timeBasePeriod * correctedDutyCycle) / 1000);
130 
131  return counterSteps;
132 }
133 
134 /*========== Setup and Teardown =============================================*/
135 void setUp(void) {
137 }
138 
139 void tearDown(void) {
140 }
141 
142 /*========== Test Cases =====================================================*/
143 
144 /** test that the start function calls etPWM API */
145 void testPWM_StartPwm(void) {
146  /* an assertion should happen, when privileges cannot be raised */
149 
150  /* otherwise the API should be called */
152  etpwmStartTBCLK_Expect();
153  PWM_StartPwm();
154 }
155 
156 /** test that the stop function calls etPWM API */
157 void testPWM_StopPwm(void) {
158  /* an assertion should happen, when privileges cannot be raised */
161 
162  /* otherwise the API should be called */
164  etpwmStopTBCLK_Expect();
165  PWM_StopPwm();
166 }
167 
168 /** tests the duty cycle function */
170  uint16_t timeBasePeriod = 999u;
171  for (uint16_t duty = 1u; duty <= 999; duty = duty + 10) {
172  PWM_SetDutyCycle_Test(timeBasePeriod, duty, calculateCounterValue(timeBasePeriod, duty));
173  }
174 
175  timeBasePeriod = 4999;
176  uint16_t duty = 0u;
177  PWM_SetDutyCycle_Test(timeBasePeriod, duty, calculateCounterValue(timeBasePeriod, duty));
178  for (uint16_t duty = 1u; duty <= 999; duty = duty + 10) {
179  PWM_SetDutyCycle_Test(timeBasePeriod, duty, calculateCounterValue(timeBasePeriod, duty));
180  }
181 }
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:77
void PWM_SetDutyCycle(uint16_t dutyCycle_perm)
Set the duty cycle of the PWM (currently only channel 1A)
Definition: pwm.c:153
void PWM_StopPwm(void)
Stop the PWM (stops all configured ePWM channels)
Definition: pwm.c:144
void PWM_StartPwm(void)
Start the PWM (starts all configured ePWM channels)
Definition: pwm.c:135
int16_t TEST_PWM_GetLinearOffset(void)
Definition: pwm.c:212
PWM driver for the TMS570LC43xx.
Helper for unit tests.
#define TEST_ASSERT_FAIL_ASSERT(_code_under_test)
assert whether assert macro has failed
void testPWM_SetDutyCycle(void)
Definition: test_pwm.c:169
void testPWM_StopPwm(void)
Definition: test_pwm.c:157
void ecapInit(void)
Definition: test_pwm.c:75
uint16_t calculateCounterValue(uint16_t timeBasePeriod, uint16_t dutyCycle)
Definition: test_pwm.c:112
uint32_t ecapGetCAP3(ecapBASE_t *ecap)
Definition: test_pwm.c:83
uint32_t ecapGetCAP1(ecapBASE_t *ecap)
Definition: test_pwm.c:77
void PWM_SetDutyCycle_Test(uint16_t timeBasePeriod, uint16_t dutyCycleIn, uint16_t dutyCycleOut)
Definition: test_pwm.c:94
void setUp(void)
Definition: test_pwm.c:135
void tearDown(void)
Definition: test_pwm.c:139
uint32_t ecapGetCAP2(ecapBASE_t *ecap)
Definition: test_pwm.c:80
long FSYS_RaisePrivilege(void)
Raise privilege.
Definition: test_pwm.c:71
void testPWM_StartPwm(void)
Definition: test_pwm.c:145
static uint8_t fsysRaisePrivilegeReturnValue
Definition: test_pwm.c:70