/******************************************************************************
 * @file     upload.c
 * @brief    Upload an image from internal Flash
 * @version  V4.0.0
 * @date     30-July-2026
 *
 * @note
 * Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd.. All rights reserved.
 *
 * @attention
 * THIS SOFTWARE JUST PROVIDES CUSTOMERS WITH CODING INFORMATION REGARDING
 * THEIR PRODUCTS, WHICH AIMS AT SAVING TIME FOR THEM. SPINTROL SHALL NOT BE
 * LIABLE FOR THE USE OF THE SOFTWARE. SPINTROL DOES NOT GUARANTEE THE
 * CORRECTNESS OF THIS SOFTWARE AND RESERVES THE RIGHT TO MODIFY THE SOFTWARE
 * WITHOUT NOTIFICATION.
 *
 ******************************************************************************/

#include "ymodem.h"
#include "common.h"
#include <stdio.h>



void SerialUpload(void)
{
    uint32_t status = 0;

#if RS485_INTERFACE_ENABLED > 0
    /* RS485 Tx Enable */
    GPIO_WritePin(RS485_CONTROL_PIN, GPIO_LEVEL_LOW);
#endif

    printf("\n\n\rSelect Receive File ... (press any key to abort)\n\r");

#if RS485_INTERFACE_ENABLED > 0
    /* RS485 Rx Enable */
    GPIO_WritePin(RS485_CONTROL_PIN, GPIO_LEVEL_HIGH);
#endif

    if (GetKey() == MODEM_C)
    {
        /* Transmit the flash image through ymodem protocol */
        status = YModem_Transmit((uint8_t *)APPLICATION_ADDR, (const uint8_t *)"UploadedFlashImage.bin", FLASH_IMAGE_SIZE);

#if RS485_INTERFACE_ENABLED > 0
        /* RS485 Tx Enable */
        GPIO_WritePin(RS485_CONTROL_PIN, GPIO_LEVEL_LOW);
#endif

        if (status != 0)
        {
            printf("\n\rError Occured while Transmitting File\n\n\r");
        }
        else
        {
            printf("\n\rFile Trasmitted Successfully \n\n\r");
        }

#if RS485_INTERFACE_ENABLED > 0
        /* RS485 Rx Enable */
        GPIO_WritePin(RS485_CONTROL_PIN, GPIO_LEVEL_HIGH);
#endif
    }
    else
    {
#if RS485_INTERFACE_ENABLED > 0
        /* RS485 Tx Enable */
        GPIO_WritePin(RS485_CONTROL_PIN, GPIO_LEVEL_LOW);
#endif

        printf("\r\n\nAborted by user.\n\n\r");

#if RS485_INTERFACE_ENABLED > 0
        /* RS485 Rx Enable */
        GPIO_WritePin(RS485_CONTROL_PIN, GPIO_LEVEL_HIGH);
#endif
    }
}








/******************* Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd. ***** END OF FILE ****/
