/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.0
 *
 * @date        2023-03-30
 *
 * @attention
 *
 *  Copyright (C) 2023 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "main.h"
#include "spi_flash.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup SPI_FLASH_Polling
  @{
*/

/** @defgroup SPI_FLASH_Polling_Macros Macros
  @{
*/

#define  DEBUG_USART USART1

#define  FLASH_WriteAddress     0x700000
#define  FLASH_SectorToErase    FLASH_WriteAddress
#define  FLASH_ReadAddress      FLASH_WriteAddress

#define  sFLASH_ID       sFLASH_M25Q_ID
#define countof(a) (sizeof(a) / sizeof(*(a)))

#define  BufferSize (countof(Tx_Buffer)-1)



/**@} end of group SPI_FLASH_Polling_Macros */

/** @defgroup SPI_FLASH_Polling_Enumerations Enumerations
  @{
*/
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;

/**@} end of group SPI_FLASH_Polling_Enumerations */

/** @defgroup SPI_FLASH_Polling_Variables Variables
  @{
*/

volatile uint32_t FlashID = 0;
uint8_t Tx_Buffer[] = "APM32A103 SPI Firmware Library Example: communication with an W25Q SPI FLASH";
uint8_t  Rx_Buffer[BufferSize];
volatile TestStatus TransferStatus1 = FAILED;
volatile TestStatus TransferStatus2 = FAILED;
volatile uint8_t Index = 0x0;

void Delay(uint32_t count);

/**@} end of group SPI_FLASH_Polling_Variables */

/** @defgroup SPI_FLASH_Polling_Functions Functions
  @{
*/
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 */
int main(void)
{    
    APM_MINI_COMInit(COM1);

    sFLASH_Config();
  
    printf("spi flash testing!!!\r\n");  
    
    /* Read SPI Flash ID */
    FlashID = sFLASH_ReadID();

    printf("Flash ID = 0x%x\r\n",FlashID); 
  
    if (FlashID == sFLASH_ID)
    {    

        sFLASH_SectorErase(FLASH_SectorToErase);

        sFLASH_WriteData(Tx_Buffer, FLASH_WriteAddress, BufferSize);

        sFLASH_ReadData(Rx_Buffer, FLASH_ReadAddress, BufferSize);

        TransferStatus1 = Buffercmp(Tx_Buffer, Rx_Buffer, BufferSize);

        sFLASH_SectorErase(FLASH_SectorToErase);

        sFLASH_ReadData(Rx_Buffer, FLASH_ReadAddress, BufferSize);

        for (Index = 0; Index < BufferSize; Index++)
        {
            if (Rx_Buffer[Index] != 0xFF)
            {
                TransferStatus2 = FAILED;
            }
            else
            {
                TransferStatus2 = PASSED;
            }
        }
    }
    else
    {
        printf("Flash ID is not simple!\r\n");
    }
    
    if(TransferStatus2 == PASSED)
    {
        printf("the specified sector part is erased!!!\r\n");
        
    }else{
        printf("the specified sector part is not well erased!!!\r\n");
          
    }
    
    while(1)
    {
        if(TransferStatus1 == PASSED)
        {
            APM_MINI_LEDToggle(LED2);
        }
        Delay(0xfffff);
    }
}


/*!
  * @brief  Compares two buffers
  *
  * @param  pBuffer1, pBuffer2: buffers to be compared
  *
  * @param  BufferLength: buffer's length
  *
  * @retval PASSED: pBuffer1 identical to pBuffer2
  *         FAILED: pBuffer1 differs from pBuffer2
  *
  */
TestStatus Buffercmp(uint8_t* Buffer1, uint8_t* Buffer2, uint16_t BufferLength)
{
    while (BufferLength--)
    {
        if (*Buffer1 != *Buffer2)
        {
            return FAILED;
        }

        Buffer1++;
        Buffer2++;
    }

    return PASSED;
}

/*!
 * @brief       Delay
 *
 * @param       count:  delay count
 *
 * @retval      None
 *
 */
void Delay(uint32_t count)
{
    volatile uint32_t delay = count;

    while (delay--);
}

#if defined (__CC_ARM) || defined (__ICCARM__) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @param       *f:  pointer to a FILE that can recording all information
*              needed to control a stream
*
* @retval      The characters that need to be send.
*
* @note
*/
int fputc(int ch, FILE *f)
{
    /* send a byte of data to the serial port */
    USART_TxData(DEBUG_USART, (uint8_t)ch);

    /* wait for the data to be send  */
    while (USART_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);

    return (ch);
}

#elif defined (__GNUC__)

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @retval      The characters that need to be send.
*
* @note
*/
int __io_putchar(int ch)
{
    /* send a byte of data to the serial port */
    USART_TxData(DEBUG_USART, ch);

    /* wait for the data to be send  */
    while (USART_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);

    return ch;
}

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       file:  Meaningless in this function.
*
* @param       *ptr:  Buffer pointer for data to be sent.
*
* @param       len:  Length of data to be sent.
*
* @retval      The characters that need to be send.
*
* @note
*/
int _write(int file, char *ptr, int len)
{
    int i;
    for (i = 0; i < len; i++)
    {
        __io_putchar(*ptr++);
    }

    return len;
}

#else
#warning Not supported compiler type
#endif

/**@} end of group SPI_FLASH_Polling_Functions */
/**@} end of group SPI_FLASH_Polling */
/**@} end of group Examples */
