/*!
 * @file        common.h
 *
 * @brief       Header for common.c module
 *
 * @version     V1.0.0
 *
 * @date        2026-02-26
 *
 * @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.
 */

/* Define to prevent recursive inclusion */
#ifndef COMMON_H
#define COMMON_H

/* Includes */
#include "g32a10xx_usart.h"
#include "bsp_flash.h"

/** @addtogroup Examples
  @{
  */

/** @addtogroup IAP_Examples
  @{
*/

/** @addtogroup IAP_BootLoader
  @{
  */

/** @defgroup IAP_BootLoader_Macros Macros
  @{
*/

#define USART_COM                           (USART1)

#define USART_COM_RXDATA                    (Usart_RxData(USART_COM))
#define USART_COM_TXDATA(c)                 (Usart_TxData(USART_COM, c))

#define USART_COM_RXREADY                   ((uint8_t)Usart_ReadStatusFlag(USART_COM, USART_FLAG_RXBNE) == (uint8_t)SET)
#define USART_COM_TXFINISH                  ((uint8_t)Usart_ReadStatusFlag(USART_COM, USART_FLAG_TXBE) == (uint8_t)RESET)

#define USART_COM_CLEAR_OVREE               (Usart_ClearStatusFlag(USART_COM, USART_FLAG_OVRE))

#define CHECK_AF(s)                         ((s >= (uint8_t)'A') && (s <= (uint8_t)'F'))
#define CHECK_af(s)                         ((s >= (uint8_t)'a') && (s <= (uint8_t)'f'))
#define CHECK_09(s)                         ((s >= (uint8_t)'0') && (s <= (uint8_t)'9'))
#define CHECK_HEX(s)                        CHECK_AF(s) || CHECK_af(s) || CHECK_09(s)
#define CHECK_DEC(s)                        CHECK_09(s)
#define CON_DEC(s)                          (s - (uint32_t)'0')

#define CON_HEX_alpha(s)                    (CHECK_AF(s) ? (s - (uint32_t)'A'+10U) : (s - (uint32_t)'a'+10U))
#define CON_HEX(s)                          (CHECK_09(s) ? (s - (uint32_t)'0') : CON_HEX_alpha(s))

#define SendString(x)                       Iap_SerialSendString((uint8_t*)(x))

/**@} end of group IAP_BootLoader_Macros */

/** @defgroup IAP_BootLoader_Structures Structures
  @{
*/
typedef void (*Iap_pFunction)(void);

typedef struct
{
    union
    {
        uint32_t jumpAddr;
        Iap_pFunction jumpFunAddr;
    } Iap_JumpUnion;
} Iap_JumpAddrType;

/**@} end of group IAP_BootLoader_Structures*/

/** @defgroup IAP_BootLoader_Functions Functions
  @{
  */

/* function declaration*/
void Iap_IntConStr(uint8_t* StrPtr, uint32_t Conint);
uint32_t Iap_StrConInt(const uint8_t* StrPtr, int32_t* ConintPtr);
uint32_t Iap_GetKeyPressed(uint8_t* InputKeyPtr);
uint8_t Iap_ReadKey(void);
uint8_t Iap_ReadKeyTimeOut(uint32_t DelayTimeOut);
void Iap_SendChar(uint8_t Character);
void Iap_SerialSendString(const uint8_t* StrPtr);
uint32_t Iap_StrHexConInt(const uint8_t* HexstrPtr, int32_t* ConintPtr);

/**@} end of group IAP_BootLoader_Functions */
/**@} end of group IAP_BootLoader */
/**@} end of group IAP_Examples */
/**@} end of group Examples */

#endif
