/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.3
 *
 * @date        2026-03-14
 *
 * @attention
 *
 *  Copyright (C) 2026 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (Geehy Semiconductor Software License Agreement).
 *
 *  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 Semiconductor Software License Agreement for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "Board.h"
#include "stdio.h"
#include "gpio_i2c_slave.h"
#include "user_i2c_config.h"

/** @addtogroup Examples
  @{
  */

/** @addtogroup I2C_Examples
  @{
  */

/** @addtogroup GPIO_I2C_SLAVE
  @{
*/

/** @defgroup GPIO_I2C_SLAVE_Macros Macros
  @{
*/

/* printf function configs to USART1 */
#define DEBUG_USART  USART1

/**@} end of group GPIO_I2C_SLAVE_Macros */

/** @defgroup GPIO_I2C_SLAVE_Variables Variables
  @{
*/

/**@} end of group GPIO_I2C_SLAVE_Variables */

/** @defgroup GPIO_I2C_SLAVE_Functions Functions
  @{
*/

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    uint8_t n=0;

    /* Initialize COM1 */
    Board_ComInit(COM1);
    
    /* Initialize the I2C gpio */
    I2c_InitSlaveGpio();

    while(1)
    {
        /* I2C slave device polling */
        I2c_Slave_Poll();
        
        /* print recevie data ,about 80ms*/
        if(recevieBufLen > I2C_SLAVE_REGADDR_SIZE)
        {
            if(recevieBufLen <= (I2C_SLAVE_PAGE_SIZE + I2C_SLAVE_REGADDR_SIZE))
            {
                /* print recevie address data */
                for(n=0; n<I2C_SLAVE_REGADDR_SIZE; n++)
                {
                    printf("RegAddr[%d] = 0x%x \r\n",n,recevieBuffer[n]);
                    /* copy recevie data to send buffer */
                    sendBuffer[n] = recevieBuffer[n];
                    /* flush recevie buffer */
                    recevieBuffer[n] = 0;
                }
                /* print recevie valid data */
                for(n=I2C_SLAVE_REGADDR_SIZE; n<recevieBufLen; n++)
                {
                    printf("Slave RecData[%d] = 0x%x \r\n",n,recevieBuffer[n]);
                    /* copy recevie data to send buffer */
                    sendBuffer[n] = recevieBuffer[n];
                    /* flush recevie buffer */
                    recevieBuffer[n] = 0;
                }
                /* reset recevie length */
                recevieBufLen = 0;
            }
            else
            {
                /* nothing */
            }
        } 
        else
        {
            /* nothing */
        }
    }
}

#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) == (uint8_t)RESET)
    {
        /* nothing */
    }

    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
#pragma message("Not supported compiler type")
#endif

/**@} end of group GPIO_I2C_SLAVE_Functions */
/**@} end of group GPIO_I2C_SLAVE */
/**@} end of group I2C_Examples */
/**@} end of group Examples */
