Index: ADuCM360_demo_cn0359/src/hal/devices.cpp =================================================================== diff -u -r8d80f35bf88694bd50d769b9bab31db8364a9004 -re68895e85fe32aca137ce08ac5d6accd67fec591 --- ADuCM360_demo_cn0359/src/hal/devices.cpp (.../devices.cpp) (revision 8d80f35bf88694bd50d769b9bab31db8364a9004) +++ ADuCM360_demo_cn0359/src/hal/devices.cpp (.../devices.cpp) (revision e68895e85fe32aca137ce08ac5d6accd67fec591) @@ -48,21 +48,53 @@ #include #include #include +#include +#include -FILE *p_lcd, *p_uart, *p_flash, *p_dac, *p_adc, *p_pwm, *p_ad8253; +#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) +FILE *p_lcd, *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]; +static char p_lcd_buf[16]; +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, 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 buzzer_open(); p_flash = fopen("flash", "rb+"); - setvbuf(p_flash, nullptr, _IOFBF, sizeof(flash_file)); +// 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); @@ -93,23 +125,127 @@ beep(50, 50, 50, 50, 50, 50, 50, 50, 50); } + 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, nullptr, _IONBF, 0); + setvbuf(p_uart, p_uart_buf, _IONBF, 0); p_lcd = fopen("lcd", "rb+"); - setvbuf(p_lcd, nullptr, _IOFBF, 16); +// setvbuf(p_lcd, nullptr, _IOFBF, 16); + setvbuf(p_lcd, p_lcd_buf, _IOFBF, 16); p_dac = fopen("dac", "rb+"); - setvbuf(p_dac, nullptr, _IOFBF, sizeof(dac_file)); +// 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, 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, 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, nullptr, _IOFBF, sizeof(adc_file)); + setvbuf(p_adc, p_adc_buf, _IOFBF, SIZE_OF_ADC_FILE); encoder_open(); } + + +/** + * @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; +}