/***************************************************************************//** * @file devices.cpp * @brief Implementation of devices.cpp * @author ******************************************************************************* * Copyright 2022(c) Analog Devices, Inc. * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * - Neither the name of Analog Devices, Inc. nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * - The use of this software may or may not infringe the patent rights * of one or more patent holders. This license does not release you * from the requirement that you obtain separate licenses from these * patent holders to use this software. * - Use of the software either in source or binary form, must be run * on or directly connected to an Analog Devices Inc. component. * * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************/ #include #include #include #include #include #include #include #ifdef EVAL_BUILD #include #include #endif // EVAL_BUILD #include #include #include #include #define SIZE_OF_FLASH_FILE sizeof(flash_file) #define SIZE_OF_DAC_FILE sizeof(dac_file) #define SIZE_OF_PWM_FILE sizeof(pwm_file) #define SIZE_OF_AD8253_FILE sizeof(ad8253_file) #define SIZE_OF_ADC_FILE sizeof(adc_file) #define SIZE_OF_CAL_FILE sizeof(cal_file) #define SIZE_OF_SER_FILE sizeof(ser_file) #ifdef EVAL_BUILD FILE *p_lcd; #endif // EVAL_BUILD FILE *p_uart, *p_flash, *p_dac, *p_adc, *p_pwm, *p_ad8253, *p_cal, *p_ser; static char stdout_buf[64]; static char stdin_buf[16]; static char stderr_buf[0]; static char p_flash_buf[SIZE_OF_FLASH_FILE]; static char p_uart_buf[0]; #ifdef EVAL_BUILD static char p_lcd_buf[16]; #endif // EVAL_BUILD static char p_dac_buf[SIZE_OF_DAC_FILE]; static char p_pwm_buf[SIZE_OF_PWM_FILE]; static char p_ad8253_buf[SIZE_OF_AD8253_FILE]; static char p_adc_buf[SIZE_OF_ADC_FILE]; static char p_cal_buf[SIZE_OF_CAL_FILE]; static char p_ser_buf[SIZE_OF_SER_FILE]; static void verify_cal_disk(void); static void verify_ser_disk(void); static bool is_coeff_in_range(float coeff); void initial_devices(void) { // setvbuf(stdout, nullptr, _IOLBF, 64); // setvbuf(stdin, nullptr, _IOFBF, 16); // setvbuf(stderr, nullptr, _IONBF, 0); setvbuf(stdout, stdout_buf, _IOLBF, 64); setvbuf(stdin, stdin_buf, _IOFBF, 16); setvbuf(stderr, stderr_buf, _IONBF, 0); NVIC_SetPriorityGrouping(6); //2 groups, each group have 4 subpriorities #ifdef EVAL_BUILD buzzer_open(); #endif // EVAL_BUILD p_flash = fopen("flash", "rb+"); // setvbuf(p_flash, nullptr, _IOFBF, sizeof(flash_file)); setvbuf(p_flash, p_flash_buf, _IOFBF, SIZE_OF_FLASH_FILE); flash_file setting; rewind(p_flash); fread(&setting, sizeof(flash_file), 1, p_flash); if (setting.baud_rate <= configure_file.max_baud_rate && setting.baud_rate >= configure_file.min_baud_rate && setting.cell_const <= configure_file.max_cell_const && setting.cell_const >= configure_file.min_cell_const && setting.frequency <= configure_file.max_freq && setting.frequency >= configure_file.min_freq && setting.temp_coef <= configure_file.max_temp_coef && setting.temp_coef >= configure_file.min_temp_coef && setting.voltage <= configure_file.max_voltage && setting.voltage >= configure_file.min_voltage && setting.setup <= configure_file.max_setup && setting.setup >= configure_file.min_setup && setting.hold <= configure_file.max_hold && setting.hold >= configure_file.min_hold && setting.brightness <= configure_file.max_brightness && setting.brightness >= configure_file.min_brightness) { //check flash_disk #ifdef EVAL_BUILD beep(50); // disk ok #endif // EVAL_BUILD } else { // disk error rewind(p_flash); fwrite(&default_setting, sizeof(flash_file), 1, p_flash); fflush(p_flash); #ifdef EVAL_BUILD beep(50, 50, 50, 50, 50, 50, 50, 50, 50); #endif // EVAL_BUILD } p_cal = fopen("cal", "rb+"); setvbuf(p_cal, p_cal_buf, _IOFBF, SIZE_OF_CAL_FILE); verify_cal_disk(); p_ser = fopen("ser", "rb+"); setvbuf(p_ser, p_ser_buf, _IOFBF, SIZE_OF_SER_FILE); verify_ser_disk(); p_uart = fopen("uart", "r"); // setvbuf(p_uart, nullptr, _IONBF, 0); setvbuf(p_uart, p_uart_buf, _IONBF, 0); #ifdef EVAL_BUILD p_lcd = fopen("lcd", "rb+"); // setvbuf(p_lcd, nullptr, _IOFBF, 16); setvbuf(p_lcd, p_lcd_buf, _IOFBF, 16); #endif // EVAL_BUILD p_dac = fopen("dac", "rb+"); // setvbuf(p_dac, nullptr, _IOFBF, sizeof(dac_file)); setvbuf(p_dac, p_dac_buf, _IOFBF, SIZE_OF_DAC_FILE); p_pwm = fopen("pwm", "rb+"); // setvbuf(p_pwm, nullptr, _IOFBF, sizeof(pwm_file)); setvbuf(p_pwm, p_pwm_buf, _IOFBF, SIZE_OF_PWM_FILE); p_ad8253 = fopen("ad8253", "rb+"); // setvbuf(p_ad8253, nullptr, _IOFBF, sizeof(ad8253_file)); setvbuf(p_ad8253, p_ad8253_buf, _IOFBF, SIZE_OF_AD8253_FILE); p_adc = fopen("adc", "rb"); // setvbuf(p_adc, nullptr, _IOFBF, sizeof(adc_file)); setvbuf(p_adc, p_adc_buf, _IOFBF, SIZE_OF_ADC_FILE); #ifdef EVAL_BUILD encoder_open(); #endif // EVAL_BUILD } /** * @brief Verifies calibration coefficients stored on disk. * * Reads all calibration coefficients from persistent storage and checks each * coefficient to ensure it lies within the valid numeric range. If any * coefficient is out of range, the error flag is set. */ static void verify_cal_disk(void) { cal_file cal_data; rewind(p_cal); fread(&cal_data, sizeof(cal_file), 1, p_cal); if ( ( true == is_coeff_in_range( cal_data.coeff1 ) ) && ( true == is_coeff_in_range( cal_data.coeff2 ) ) && ( true == is_coeff_in_range( cal_data.coeff3 ) ) && ( true == is_coeff_in_range( cal_data.coeff4 ) ) && ( true == is_coeff_in_range( cal_data.coeff5 ) ) && ( true == is_coeff_in_range( cal_data.coeff6 ) ) && ( true == is_coeff_in_range( cal_data.coeff7 ) ) && ( true == is_coeff_in_range( cal_data.coeff8 ) ) && ( true == is_coeff_in_range( cal_data.coeff9 ) ) && ( true == is_coeff_in_range( cal_data.coeff10 ) ) && ( true == is_coeff_in_range( cal_data.coeff11 ) ) && ( true == is_coeff_in_range( cal_data.coeff12 ) ) ) { //check cal_disk // disk ok } else { // disk error set_error_flag(); } } /** * @brief Verifies version and serial fields read from persistent storage. * * Reads hardware version and serial number from persistent storage and checks * that lengths fall within the configured minimum and maximum bounds. * Sets the error flag if validation fails. */ static void verify_ser_disk(void) { ser_file ser_data; rewind(p_ser); fread(&ser_data, sizeof(ser_file), 1, p_ser); char fw_ver[MAX_VERSION_LENGTH] = FIRMWARE_VERSION; int fw_len = strlen(fw_ver); int hw_len = strlen(ser_data.hw_ver); int sn_len = strlen(ser_data.ser_num); if (fw_len < MAX_VERSION_LENGTH && fw_len >= MIN_VERSION_LENGTH && hw_len < MAX_VERSION_LENGTH && hw_len >= MIN_VERSION_LENGTH && sn_len < MAX_VERSION_LENGTH && sn_len >= MIN_VERSION_LENGTH) { //check ser_disk // disk ok } else { // disk error set_error_flag(); } } /** * @brief Checks whether a coefficient value lies within the valid range. * * @param[in] coeff Coefficient value to be validated. * * @return true if the coefficient is within the allowed range; false otherwise. */ static bool is_coeff_in_range(float coeff) { bool result = false; if ((coeff <= MAX_COEFF) && (coeff >= MIN_COEFF)) { result = true; } return result; }