STM32-RTC-Configuration&InputHandling

by Marwen Maghrebi
// Function to validate the date
bool validateDate(RTC_DateTypeDef *date) {
    // Array with the number of days in each month
    uint8_t daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    // Validate month
    if (date->Month < 1 || date->Month > 12) {
        return false;
    }

    // Validate day
    if (date->Date < 1 || date->Date > daysInMonth[date->Month - 1]) {
        return false;
    }

    // The date is valid
    return true;
}

// Function to validate the time
bool validateTime(RTC_TimeTypeDef *time) {
    // Validate hours, minutes, and seconds
    if (time->Hours > 23 || time->Minutes > 59 || time->Seconds > 59) {
        return false;
    }

    // The time is valid
    return true;
}

// Function to check format of the input strings
bool checkInputFormat(const char *input, const char *format) {
    // Check for the correct length
    if (strlen(input) != strlen(format)) {
        return false;
    }

    for (size_t i = 0; i < strlen(format); i++) {
        if (format[i] == ':') {
            if (input[i] != ':') return false; // Expecting ':' in time
        } else if (format[i] == '/') {
            if (input[i] != '/') return false; // Expecting '/' in date
        } else if (!isdigit(input[i])) {
            return false; // Expecting a digit
        }
    }
    return true; // Format is correct
}

void Set_RTC_TimeAndDate(void) {
    RTC_TimeTypeDef sTime;
    RTC_DateTypeDef sDate;
    char uart_buf[50];  // Ensure this is appropriately sized for your messages

    while (1) {  // Loop until valid date and time are set
        // Get the time from UART
        const char *timePrompt = "Enter Time (HH:MM:SS): ";
        HAL_UART_Transmit(&huart1, (uint8_t *)timePrompt, strlen(timePrompt), HAL_MAX_DELAY);
        UART_ReceiveString(uart_buf, sizeof(uart_buf));  // Assuming uart_buf is defined

        // Validate the format of the input
        if (!checkInputFormat(uart_buf, "HH:MM:SS")) {
            const char *errorMsg = "Invalid time format. Please use HH:MM:SS\n\r";
            HAL_UART_Transmit(&huart1, (uint8_t *)errorMsg, strlen(errorMsg), HAL_MAX_DELAY);
            continue;  // Prompt for input again
        }

        // Parse the received time from uart_buf
        sTime.Hours = (uart_buf[0] - '0') * 10 + (uart_buf[1] - '0');
        sTime.Minutes = (uart_buf[3] - '0') * 10 + (uart_buf[4] - '0');
        sTime.Seconds = (uart_buf[6] - '0') * 10 + (uart_buf[7] - '0');

        // Prompt for date input
        const char *datePrompt = "Enter Date (DD/MM/YY): ";
        HAL_UART_Transmit(&huart1, (uint8_t *)datePrompt, strlen(datePrompt), HAL_MAX_DELAY);
        UART_ReceiveString(uart_buf, sizeof(uart_buf));  // Assuming uart_buf is defined

        // Validate the format of the input
        if (!checkInputFormat(uart_buf, "DD/MM/YY")) {
            const char *errorMsg = "Invalid date format. Please use DD/MM/YY\n\r";
            HAL_UART_Transmit(&huart1, (uint8_t *)errorMsg, strlen(errorMsg), HAL_MAX_DELAY);
            continue;  // Prompt for input again
        }

        // Parse the received date from uart_buf
        sDate.Date =  (uart_buf[0] - '0') * 10 +   (uart_buf[1] - '0');
        sDate.Month = (uart_buf[3] - '0') * 10 +   (uart_buf[4] - '0');
        sDate.Year =  (uart_buf[6] - '0') * 10 +   (uart_buf[7] - '0');  // Two-digit year

        // Validate date and time
        bool dateValid = validateDate(&sDate);
        bool timeValid = validateTime(&sTime);

        if (!dateValid || !timeValid) {
            const char *errorMsg = "Error in time or in date. Please try again.\n\r";
            HAL_UART_Transmit(&huart1, (uint8_t *)errorMsg, strlen(errorMsg), HAL_MAX_DELAY);
            continue;  // Prompt for input again
        }

        // If valid, set the time and date
        HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
        HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN);

        const char *successMsg = "RTC set successfully\n\r";
        HAL_UART_Transmit(&huart1, (uint8_t *)successMsg, strlen(successMsg), HAL_MAX_DELAY);
        break;  // Exit the loop since time and date are set successfully
    }
}

void UART_ReceiveString(char *buffer, uint16_t bufferSize) {
    uint16_t i = 0;
    char received;

    // Receive each character until '\r' is received or buffer is full
    while (i < bufferSize - 1) {
        HAL_UART_Receive(&huart1, (uint8_t *)&received, 1, HAL_MAX_DELAY);
        if (received == '\r') {  // End of input
            break;
        }
        buffer[i++] = received;
    }
    buffer[i] = '\0';  // Null-terminate the string
}
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
-
00:00
00:00
Update Required Flash plugin
-
00:00
00:00