foxBMS - Unit Tests  1.6.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-10-12 (date of last update)
47  * @version v1.6.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 /*========== Unit Testing Framework Directives ==============================*/
70 TEST_INCLUDE_PATH("../../src/app/driver/foxmath")
71 TEST_INCLUDE_PATH("../../src/app/driver/pwm")
72 
73 /*========== Definitions and Implementations for Unit Test ==================*/
74 static uint8_t fsysRaisePrivilegeReturnValue = 0u;
75 long FSYS_RaisePrivilege(void) {
77 }
78 
79 void ecapInit(void) {
80  /* dummy implementation required for linking */
81 }
82 
83 uint32_t ecapGetCAP1(ecapBASE_t *ecap) {
84  return ecap->CAP1;
85 }
86 uint32_t ecapGetCAP2(ecapBASE_t *ecap) {
87  return ecap->CAP2;
88 }
89 uint32_t ecapGetCAP3(ecapBASE_t *ecap) {
90  return ecap->CAP3;
91 }
92 /** wraps the duty cycle function for test
93  *
94  * The output duty cycle is tested against the value computed by the function.
95  *
96  * @param[in] timeBasePeriod This value will be injected as a time base period
97  * @param[in] dutyCycleIn This value will be passed as requested duty cycle in permill
98  * @param[in] dutyCycleOut The expected output duty cycle (in counts)
99  */
100 void PWM_SetDutyCycle_Test(uint16_t timeBasePeriod, uint16_t dutyCycleIn, uint16_t dutyCycleOut) {
101  etpwm_config_reg_t etPwmConfig = {0};
102  etPwmConfig.CONFIG_TBPRD = timeBasePeriod;
103 
104  etpwm1GetConfigValue_Expect(NULL_PTR, CurrentValue);
105  etpwm1GetConfigValue_IgnoreArg_config_reg();
106  etpwm1GetConfigValue_ReturnThruPtr_config_reg(&etPwmConfig);
107 
108  etpwmSetCmpA_Expect(NULL_PTR, dutyCycleOut);
109  etpwmSetCmpA_IgnoreArg_etpwm();
110  PWM_SetDutyCycle(dutyCycleIn);
111 }
112 
113 /** calculates the PWM counter value
114  *
115  * @param[in] timeBasePeriod This time base period will be assumed for the calculation
116  * @param[in] dutyCycle The requested duty cycle in permill
117  */
118 uint16_t calculateCounterValue(uint16_t timeBasePeriod, uint16_t dutyCycle) {
119  /* retrieves the tuning value from the module */
120  int16_t linearOffset = TEST_PWM_GetLinearOffset();
121 
122  double correctedDutyCycle = ((double)dutyCycle + linearOffset);
123  if (correctedDutyCycle < 0) {
124  correctedDutyCycle = 0;
125  }
126 
127  /* bound to upper and lower threshold */
128  if (correctedDutyCycle < 1) {
129  correctedDutyCycle = 1;
130  }
131  if (correctedDutyCycle > 999) {
132  correctedDutyCycle = 999;
133  }
134 
135  uint16_t counterSteps = (((double)timeBasePeriod * correctedDutyCycle) / 1000);
136 
137  return counterSteps;
138 }
139 
140 /*========== Setup and Teardown =============================================*/
141 void setUp(void) {
143 }
144 
145 void tearDown(void) {
146 }
147 
148 /*========== Test Cases =====================================================*/
149 
150 /** test that the start function calls etPWM API */
151 void testPWM_StartPwm(void) {
152  /* an assertion should happen, when privileges cannot be raised */
155 
156  /* otherwise the API should be called */
158  etpwmStartTBCLK_Expect();
159  PWM_StartPwm();
160 }
161 
162 /** test that the stop function calls etPWM API */
163 void testPWM_StopPwm(void) {
164  /* an assertion should happen, when privileges cannot be raised */
167 
168  /* otherwise the API should be called */
170  etpwmStopTBCLK_Expect();
171  PWM_StopPwm();
172 }
173 
174 /** tests the duty cycle function */
176  uint16_t timeBasePeriod = 999u;
177  for (uint16_t duty = 1u; duty <= 999; duty = duty + 10) {
178  PWM_SetDutyCycle_Test(timeBasePeriod, duty, calculateCounterValue(timeBasePeriod, duty));
179  }
180 
181  timeBasePeriod = 4999;
182  uint16_t duty = 0u;
183  PWM_SetDutyCycle_Test(timeBasePeriod, duty, calculateCounterValue(timeBasePeriod, duty));
184  for (uint16_t duty = 1u; duty <= 999; duty = duty + 10) {
185  PWM_SetDutyCycle_Test(timeBasePeriod, duty, calculateCounterValue(timeBasePeriod, duty));
186  }
187 }
#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:175
void testPWM_StopPwm(void)
Definition: test_pwm.c:163
void ecapInit(void)
Definition: test_pwm.c:79
uint16_t calculateCounterValue(uint16_t timeBasePeriod, uint16_t dutyCycle)
Definition: test_pwm.c:118
uint32_t ecapGetCAP3(ecapBASE_t *ecap)
Definition: test_pwm.c:89
uint32_t ecapGetCAP1(ecapBASE_t *ecap)
Definition: test_pwm.c:83
void PWM_SetDutyCycle_Test(uint16_t timeBasePeriod, uint16_t dutyCycleIn, uint16_t dutyCycleOut)
Definition: test_pwm.c:100
void setUp(void)
Definition: test_pwm.c:141
void tearDown(void)
Definition: test_pwm.c:145
uint32_t ecapGetCAP2(ecapBASE_t *ecap)
Definition: test_pwm.c:86
long FSYS_RaisePrivilege(void)
Raise privilege.
Definition: test_pwm.c:75
void testPWM_StartPwm(void)
Definition: test_pwm.c:151
static uint8_t fsysRaisePrivilegeReturnValue
Definition: test_pwm.c:74