|
| 1 | +/***************************************************************************/ /** |
| 2 | + * @file analog_comparator_test_main.c |
| 3 | + * @brief Analog Comparator Unity Test Functions |
| 4 | + ******************************************************************************* |
| 5 | + * # License |
| 6 | + * <b>Copyright 2023 Silicon Laboratories Inc. www.silabs.com</b> |
| 7 | + ******************************************************************************* |
| 8 | +* |
| 9 | + * The licensor of this software is Silicon Laboratories Inc. Your use of this |
| 10 | + * software is governed by the terms of Silicon Labs Master Software License |
| 11 | + * Agreement (MSLA) available at |
| 12 | + * www.silabs.com/about-us/legal/master-software-license-agreement. This |
| 13 | + * software is distributed to you in Source Code format and is governed by the |
| 14 | + * sections of the MSLA applicable to Source Code. |
| 15 | + * |
| 16 | + ****************************************************************************** |
| 17 | + ******************************************************************************/ |
| 18 | +#include "sl_si91x_analog_comparator.h" |
| 19 | +#include "sl_system_init.h" |
| 20 | +#include "unity.h" |
| 21 | + |
| 22 | +/******************************************************************************* |
| 23 | + *************************** DEFINES / MACROS ******************************** |
| 24 | + ******************************************************************************/ |
| 25 | + |
| 26 | +/******************************************************************************* |
| 27 | + *************************** Global Variables ******************************* |
| 28 | + ******************************************************************************/ |
| 29 | + |
| 30 | +/******************************************************************************* |
| 31 | + ************************ Static Function Prototypes ************************ |
| 32 | + ******************************************************************************/ |
| 33 | +static void on_comparator1_callback(void); |
| 34 | +static void on_comparator2_callback(void); |
| 35 | + |
| 36 | +/******************************************************************************* |
| 37 | + ************************ Test Function Prototypes **************************** |
| 38 | + ******************************************************************************/ |
| 39 | +void test_analog_comparator_init(void); |
| 40 | +void test_analog_comparator_set_configurations(void); |
| 41 | +void test_analog_comparator_register_callback(void); |
| 42 | +void test_analog_comparator_unregister_callback(void); |
| 43 | +void test_analog_comparator_set_resistor_bank_threshold(void); |
| 44 | +void test_analog_comparator_set_reference_scaler_output(void); |
| 45 | +void test_analog_comparator_deinit(void); |
| 46 | + |
| 47 | +/******************************************************************************* |
| 48 | + * Main function in which all the test cases are tested using unity framework |
| 49 | + ******************************************************************************/ |
| 50 | +int main() |
| 51 | +{ |
| 52 | + sl_system_init(); |
| 53 | + UnityBeginGroup("ANALOG COMPARATOR"); |
| 54 | + RUN_TEST(test_analog_comparator_init, __LINE__); |
| 55 | + RUN_TEST(test_analog_comparator_set_configurations, __LINE__); |
| 56 | + RUN_TEST(test_analog_comparator_set_resistor_bank_threshold, __LINE__); |
| 57 | + RUN_TEST(test_analog_comparator_set_reference_scaler_output, __LINE__); |
| 58 | + RUN_TEST(test_analog_comparator_register_callback, __LINE__); |
| 59 | + RUN_TEST(test_analog_comparator_unregister_callback, __LINE__); |
| 60 | + RUN_TEST(test_analog_comparator_deinit, __LINE__); |
| 61 | + UnityEnd(); |
| 62 | + UnityPrintf("END"); |
| 63 | + while (1) { |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/******************************************************************************* |
| 68 | + * Function to test init API of comparator |
| 69 | + * |
| 70 | + * @param none |
| 71 | + * @return none |
| 72 | + ******************************************************************************/ |
| 73 | +void test_analog_comparator_init(void) |
| 74 | +{ |
| 75 | + UnityPrintf("\n"); |
| 76 | + UnityPrintf("Testing analog comparator init API \n"); |
| 77 | + sl_si91x_analog_comparator_init(); |
| 78 | + TEST_ASSERT_EQUAL_HEX(AUX_ADC_DAC_COMP->AUX_LDO_b.ENABLE_LDO, ENABLE); |
| 79 | + TEST_ASSERT_EQUAL_HEX(AUX_ADC_DAC_COMP->AUX_LDO_b.BYPASS_LDO, ENABLE); |
| 80 | + UnityPrintf("Comparator reference voltage is configured properly\n"); |
| 81 | + UnityPrintf("Analog Comparator init API test completed \n"); |
| 82 | +} |
| 83 | + |
| 84 | +/******************************************************************************* |
| 85 | + * Function to test the set configuration API of Analog comparator |
| 86 | + * |
| 87 | + * @param none |
| 88 | + * @return none |
| 89 | + ******************************************************************************/ |
| 90 | +void test_analog_comparator_set_configurations(void) |
| 91 | +{ |
| 92 | + UnityPrintf("\n"); |
| 93 | + UnityPrintf("Testing analog comparator set configuration API \n"); |
| 94 | + sl_status_t status; |
| 95 | + sl_analog_comparator_config_t handle1; |
| 96 | + sl_analog_comparator_config_t handle2; |
| 97 | + sl_analog_comparator_config_t handle3; |
| 98 | + sl_analog_comparator_config_t handle4; |
| 99 | + sl_analog_comparator_config_t handle5; |
| 100 | + sl_analog_comparator_config_t *test_null = NULL; |
| 101 | + |
| 102 | + handle1.comparator_number = SL_COMPARATOR_LAST; |
| 103 | + handle1.non_inverting_input = SL_COMPARATOR_GPIO_INPUT_1; |
| 104 | + handle1.inverting_input = SL_COMPARATOR_GPIO_INPUT_1; |
| 105 | + handle1.hystersis_value = 0; |
| 106 | + handle1.is_filter_enabled = false; |
| 107 | + |
| 108 | + handle2.comparator_number = SL_COMPARATOR_2; |
| 109 | + handle2.non_inverting_input = SL_COMPARATOR_INPUT_LAST; |
| 110 | + handle2.inverting_input = SL_COMPARATOR_GPIO_INPUT_1; |
| 111 | + handle2.hystersis_value = 0; |
| 112 | + handle2.is_filter_enabled = false; |
| 113 | + |
| 114 | + handle3.comparator_number = SL_COMPARATOR_2; |
| 115 | + handle3.non_inverting_input = SL_COMPARATOR_GPIO_INPUT_1; |
| 116 | + handle3.inverting_input = SL_COMPARATOR_INPUT_LAST; |
| 117 | + handle3.hystersis_value = 0; |
| 118 | + handle3.is_filter_enabled = false; |
| 119 | + |
| 120 | + handle4.comparator_number = SL_COMPARATOR_2; |
| 121 | + handle4.non_inverting_input = SL_COMPARATOR_GPIO_INPUT_1; |
| 122 | + handle4.inverting_input = SL_COMPARATOR_GPIO_INPUT_1; |
| 123 | + handle4.hystersis_value = SL_COMPARATOR_HYSTERSIS_VALUE_LAST; |
| 124 | + handle4.is_filter_enabled = false; |
| 125 | + |
| 126 | + handle5.comparator_number = SL_COMPARATOR_2; |
| 127 | + handle5.non_inverting_input = SL_COMPARATOR_GPIO_INPUT_1; |
| 128 | + handle5.inverting_input = SL_COMPARATOR_GPIO_INPUT_1; |
| 129 | + handle5.hystersis_value = 0; |
| 130 | + handle5.is_filter_enabled = false; |
| 131 | + |
| 132 | + status = sl_si91x_analog_comparator_set_configurations(test_null); |
| 133 | + TEST_ASSERT_EQUAL_HEX(SL_STATUS_NULL_POINTER, status); |
| 134 | + UnityPrintf("Status of API is correct, when tested with NULL pointer \n"); |
| 135 | + |
| 136 | + // testing with invalid parameters |
| 137 | + status = sl_si91x_analog_comparator_set_configurations(&handle1); |
| 138 | + TEST_ASSERT_EQUAL_HEX(SL_STATUS_INVALID_PARAMETER, status); |
| 139 | + UnityPrintf("Status of API is correct, tested with invalid comparator number \n"); |
| 140 | + |
| 141 | + status = sl_si91x_analog_comparator_set_configurations(&handle2); |
| 142 | + TEST_ASSERT_EQUAL_HEX(SL_STATUS_INVALID_PARAMETER, status); |
| 143 | + UnityPrintf("Status of API is correct, tested with invalid non-inverting input parameter \n"); |
| 144 | + |
| 145 | + status = sl_si91x_analog_comparator_set_configurations(&handle3); |
| 146 | + TEST_ASSERT_EQUAL_HEX(SL_STATUS_INVALID_PARAMETER, status); |
| 147 | + UnityPrintf("Status of API is correct, tested with invalid inverting input parameter \n"); |
| 148 | + |
| 149 | + status = sl_si91x_analog_comparator_set_configurations(&handle4); |
| 150 | + TEST_ASSERT_EQUAL_HEX(SL_STATUS_INVALID_PARAMETER, status); |
| 151 | + UnityPrintf("Status of API is correct, tested with invalid Hysteresis value parameter \n"); |
| 152 | + |
| 153 | + // testing with valid parameters |
| 154 | + status = sl_si91x_analog_comparator_set_configurations(&handle5); |
| 155 | + TEST_ASSERT_EQUAL_HEX(AUX_ADC_DAC_COMP->COMPARATOR1_b.CMP2_EN, 0); |
| 156 | + UnityPrintf("Comparator number is configured properly\n"); |
| 157 | + TEST_ASSERT_EQUAL_HEX(handle5.non_inverting_input, AUX_ADC_DAC_COMP->COMPARATOR1_b.CMP2_MUX_SEL_P); |
| 158 | + UnityPrintf("Comparator non-inverting input is configured properly\n"); |
| 159 | + TEST_ASSERT(handle5.inverting_input == AUX_ADC_DAC_COMP->COMPARATOR1_b.CMP2_MUX_SEL_N); |
| 160 | + UnityPrintf("Comparator inverting input is configured properly\n"); |
| 161 | + TEST_ASSERT(status == SL_STATUS_OK); |
| 162 | + UnityPrintf("Status of API is correct, tested with valid parameters \n"); |
| 163 | + UnityPrintf("Analog comparator set configuration API test completed \n"); |
| 164 | +} |
| 165 | + |
| 166 | +/******************************************************************************* |
| 167 | + * Function to test analog comparator set resistor bank threshold API |
| 168 | + * |
| 169 | + * @param none |
| 170 | + * @return none |
| 171 | + ******************************************************************************/ |
| 172 | +void test_analog_comparator_set_resistor_bank_threshold(void) |
| 173 | +{ |
| 174 | + UnityPrintf("\n"); |
| 175 | + UnityPrintf("Testing analog comparator set resistor bank threshold API \n"); |
| 176 | + sl_status_t status; |
| 177 | + |
| 178 | + status = sl_si91x_analog_comparator_set_resistor_bank_threshold(SL_COMPARATOR_THRESHOLD_VALUE_LAST); |
| 179 | + TEST_ASSERT_EQUAL_HEX(SL_STATUS_INVALID_PARAMETER, status); |
| 180 | + UnityPrintf("Status of API is correct, tested with invalid threshold value \n"); |
| 181 | + |
| 182 | + status = sl_si91x_analog_comparator_set_resistor_bank_threshold(SL_COMPARATOR_THRESHOLD_VALUE_FOR_1_86_VOLT); |
| 183 | + TEST_ASSERT_EQUAL_HEX(SL_STATUS_OK, status); |
| 184 | + UnityPrintf("Status of API is correct, tested with valid parameters \n"); |
| 185 | + TEST_ASSERT_EQUAL_HEX(AUX_ADC_DAC_COMP->BOD_b.BOD_THRSH, (SL_COMPARATOR_THRESHOLD_VALUE_FOR_1_86_VOLT & 0x1F)); |
| 186 | + UnityPrintf("Comparator threshold value is configured properly\n"); |
| 187 | + UnityPrintf("Analog Comparator set resistor bank threshold API test completed \n"); |
| 188 | +} |
| 189 | + |
| 190 | +/******************************************************************************* |
| 191 | + * Function to test analog comparator set reference scaler output API |
| 192 | + * |
| 193 | + * @param none |
| 194 | + * @return none |
| 195 | + ******************************************************************************/ |
| 196 | +void test_analog_comparator_set_reference_scaler_output(void) |
| 197 | +{ |
| 198 | + UnityPrintf("\n"); |
| 199 | + UnityPrintf("Testing analog comparator set reference scaler output API \n"); |
| 200 | + sl_status_t status; |
| 201 | + sl_analog_comparator_config_t handle6; |
| 202 | + handle6.comparator_number = SL_COMPARATOR_2; |
| 203 | + handle6.non_inverting_input = SL_COMPARATOR_RESISTOR_BANK_OUTPUT; |
| 204 | + handle6.inverting_input = SL_COMPARATOR_REFERRENCE_SCALER_OUTPUT; |
| 205 | + handle6.hystersis_value = 0; |
| 206 | + handle6.is_filter_enabled = false; |
| 207 | + status = sl_si91x_analog_comparator_set_configurations(&handle6); |
| 208 | + //setting non-inverting input through BOD output |
| 209 | + sl_si91x_analog_comparator_set_resistor_bank_threshold(SL_COMPARATOR_THRESHOLD_VALUE_FOR_1_86_VOLT); |
| 210 | + status = sl_si91x_analog_comparator_set_reference_scaler_output(SL_COMPARATOR_SCALE_FACTOR_VALUE_LAST); |
| 211 | + TEST_ASSERT_EQUAL_HEX(SL_STATUS_INVALID_PARAMETER, status); |
| 212 | + UnityPrintf("Status of API is correct, tested with invalid threshold value \n"); |
| 213 | + |
| 214 | + //setting inverting input through reference scaler |
| 215 | + status = sl_si91x_analog_comparator_set_reference_scaler_output(SL_COMPARATOR_SCALE_FACTOR_VALUE_FOR_0_8_VOLT); |
| 216 | + TEST_ASSERT_EQUAL_HEX(SL_STATUS_OK, status); |
| 217 | + UnityPrintf("Status of API is correct, tested with valid parameters \n"); |
| 218 | + TEST_ASSERT_EQUAL_HEX(AUX_ADC_DAC_COMP->BOD_b.REFBUF_VOLT_SEL, SL_COMPARATOR_SCALE_FACTOR_VALUE_FOR_0_8_VOLT); |
| 219 | + UnityPrintf("Comparator threshold value is configured properly\n"); |
| 220 | + UnityPrintf("Analog Comparator set reference scaler output API test completed \n"); |
| 221 | +} |
| 222 | + |
| 223 | +/******************************************************************************* |
| 224 | + * Function to test comparator callback register api |
| 225 | + * |
| 226 | + * @param none |
| 227 | + * @return none |
| 228 | + ******************************************************************************/ |
| 229 | +void test_analog_comparator_register_callback(void) |
| 230 | +{ |
| 231 | + UnityPrintf("\n"); |
| 232 | + UnityPrintf("Testing analog comparator callback register API \n"); |
| 233 | + sl_status_t status; |
| 234 | + |
| 235 | + status = sl_si91x_analog_comparator_register_callback(SL_COMPARATOR_2, NULL); |
| 236 | + TEST_ASSERT_EQUAL_HEX(SL_STATUS_NULL_POINTER, status); |
| 237 | + UnityPrintf("Status of API is correct, tested with NULL-pointer parameter \n"); |
| 238 | + |
| 239 | + status = sl_si91x_analog_comparator_register_callback(SL_COMPARATOR_LAST, on_comparator2_callback); |
| 240 | + TEST_ASSERT_EQUAL_HEX(SL_STATUS_INVALID_PARAMETER, status); |
| 241 | + UnityPrintf("Status of API is correct, tested with invalid comparator number \n"); |
| 242 | + |
| 243 | + status = sl_si91x_analog_comparator_register_callback(SL_COMPARATOR_2, on_comparator2_callback); |
| 244 | + TEST_ASSERT_EQUAL_HEX(SL_STATUS_OK, status); |
| 245 | + UnityPrintf("Status of API is correct, tested with valid parameters \n"); |
| 246 | + |
| 247 | + UnityPrintf("Callback is registered successfully \n"); |
| 248 | + UnityPrintf("Analog Comparator callback register test completed \n"); |
| 249 | +} |
| 250 | + |
| 251 | +/******************************************************************************* |
| 252 | + * Function to test comparator callback unregister api |
| 253 | + * |
| 254 | + * @param none |
| 255 | + * @return none |
| 256 | + ******************************************************************************/ |
| 257 | +void test_analog_comparator_unregister_callback(void) |
| 258 | +{ |
| 259 | + UnityPrintf("\n"); |
| 260 | + UnityPrintf("Testing analog comparator callback unregister Api \n"); |
| 261 | + sl_status_t status; |
| 262 | + |
| 263 | + status = sl_si91x_analog_comparator_unregister_callback(SL_COMPARATOR_LAST); |
| 264 | + TEST_ASSERT_EQUAL_HEX(SL_STATUS_INVALID_PARAMETER, status); |
| 265 | + UnityPrintf("Status of API is correct, tested with invalid comparator number \n"); |
| 266 | + |
| 267 | + status = sl_si91x_analog_comparator_unregister_callback(SL_COMPARATOR_2); |
| 268 | + TEST_ASSERT_EQUAL_HEX(SL_STATUS_OK, status); |
| 269 | + UnityPrintf("Status of API is correct, tested with valid parameters \n"); |
| 270 | + |
| 271 | + status = sl_si91x_analog_comparator_register_callback(SL_COMPARATOR_2, on_comparator2_callback); |
| 272 | + TEST_ASSERT_EQUAL_HEX(SL_STATUS_OK, status); |
| 273 | + UnityPrintf("An already registered callback is registered successfully after unregister\n"); |
| 274 | + UnityPrintf("Analog Comparator callback unregister test completed \n"); |
| 275 | +} |
| 276 | +/******************************************************************************* |
| 277 | + * Function to test comparator deinit api |
| 278 | + * |
| 279 | + * @param none |
| 280 | + * @return none |
| 281 | + ******************************************************************************/ |
| 282 | +void test_analog_comparator_deinit(void) |
| 283 | +{ |
| 284 | + UnityPrintf("\n"); |
| 285 | + UnityPrintf("Testing analog comparator deinit API \n"); |
| 286 | + sl_si91x_analog_comparator_deinit(); |
| 287 | + TEST_ASSERT_EQUAL_HEX(ULPCLK->ULP_AUXADC_CLK_GEN_REG_b.ULP_AUX_CLK_EN_b, DISABLE); |
| 288 | + UnityPrintf("Comparator AUXADC clock disabled successfully \n"); |
| 289 | + UnityPrintf("Analog Comparator deinit API test completed \n"); |
| 290 | +} |
| 291 | +/******************************************************************************* |
| 292 | + Comparator-1 callback function |
| 293 | + ******************************************************************************/ |
| 294 | +void on_comparator1_callback(void) |
| 295 | +{ |
| 296 | + UnityPrintf("In callback, it is registered successfully\n"); |
| 297 | + sl_si91x_analog_comparator_unregister_callback(SL_COMPARATOR_1); |
| 298 | +} |
| 299 | + |
| 300 | +/******************************************************************************* |
| 301 | + Comparator-2 callback function |
| 302 | + ******************************************************************************/ |
| 303 | +void on_comparator2_callback(void) |
| 304 | +{ |
| 305 | + UnityPrintf("In callback, it is registered successfully\n"); |
| 306 | + sl_si91x_analog_comparator_unregister_callback(SL_COMPARATOR_2); |
| 307 | +} |
0 commit comments