
/************************************************************************
 * Copyright(c) 2023 Levetop Semiconductor Co.,Ltd. All rights reserved.
 *
 * @file: main.c
 * @Brief: This SDK is used for test LT269 hardware basic function
 *
 * @Version: V0.9
 * @Date: 2023-12-25
 *
 * @Author: Bert
 *************************************************************************/

#ifndef _LT_API_H_
#define _LT_API_H_

//#include "type.h"

#include "sys.h"
#include "delay.h"
#include "debug.h"
#include "cache_drv.h"

#include "spi_reg.h"
#include "spi_drv.h"
#include "adc_drv.h"
#include "rtc_drv.h"
#include "flash_drv.h"
#include "eflash_drv.h"
#include "pit_drv.h"
#include "adc_drv.h"
#include "pwm_drv.h"
#include "gpio_drv.h"
#include "ff.h"
#include "rtp_drv.h"
#include "ctp_drv.h"
#include "wdt_drv.h"
#include "i2c_drv.h"
#include "lcd_drv.h"

#include "lcd_func.h"

#include "font.h"
#include "misc.h"

// sys.c
extern void NVIC_SetVectorTable(uint32_t NVIC_VectTab,uint32_t Offset);
extern void NVIC_Init(uint8_t NVIC_PreemptionPriority,uint8_t NVIC_SubPriority,uint8_t NVIC_Channel,uint8_t NVIC_Group)	;
extern void Sys_Init(void);
extern void SysTick_Init(uint32_t ticks);  // ticks between 2 interrupt 
extern void INTX_DISABLE(void);
extern void INTX_ENABLE(void);
extern void Sys_Standby(uint8_t mode);         		

// =========================================================
// delay.c
extern void DelayMS(uint32_t time);
extern void DelayUS(uint32_t time);

// wdt_drv.c
extern void WDT_Init(uint16_t WMRCounterVal);
extern void WDT_Trigger(void);

// rtc_drv.c
extern void RTC_Init(bool clk_sel);
extern void RTC_SetDate(uint8_t year, uint8_t mon, uint8_t day);
extern void RTC_GetDate(uint8_t *year, uint8_t *mon, uint8_t *day);
extern void RTC_SetTime(uint8_t hour, uint8_t minute, uint8_t second);
extern void RTC_GetTime(uint8_t *hour, uint8_t *minute, uint8_t *second);
extern void RTC_SetDayOfWeek(uint8_t dayOfWeek);
extern uint8_t RTC_GetDayOfWeek(void);

// debug.c
extern void UARTDebugPort_Init(UART_TypeDef *UARTx,uint32_t baudrate);

// pit_drv 
extern void PIT_Init(PIT_TypeDef *PITx,PIT_CLKDIV Prescaler, uint32_t reloadCounterVal,bool intEnable);
extern void PIT_Enable(PIT_TypeDef *PITx, bool state);
extern void PIT_IntEnable(PIT_TypeDef *PITx,bool state);
extern uint32_t PIT_ReadCounter(PIT_TypeDef *PITx);
extern void PIT_WriteCounter(PIT_TypeDef *PITx, uint32_t value);

// uart_drv.c
extern void UART_Init(UART_TypeDef *UARTx,uint32_t baudrate,uint8_t parity,uint8_t intMode);
extern bool UART_ReceiveByte(UART_TypeDef *UARTx,uint8_t *buffer);
extern bool UART_ReceiveBytes(UART_TypeDef *UARTx,uint8_t *buffer,uint16_t length);
extern void UART_SendByte(UART_TypeDef *UARTx,uint8_t data);
extern void UART_SendBytes(UART_TypeDef *UARTx,uint8_t *buffer,uint16_t length);

// adc_drv.c
extern void ADC_Init(ADC_TypeDef *ADCx,uint8_t channel);
extern uint16_t ADC_GetValue(ADC_TypeDef *ADCx,uint8_t channel);
extern void ADC_StartConverion(void);
extern void ADC_StopConverion(void);

// pwm_drv.c
extern void PWM_Init(PWM_TypeDef *PWMx,uint8_t channel,uint32_t frequency, uint8_t dutyCycle, bool polarity);
extern void PWM_AdjustDutyCycle(PWM_TypeDef *PWMx, uint8_t channel, uint8_t dutyCycle);
extern void PWM_AdjustFrequency(PWM_TypeDef *PWMx, uint8_t channel, uint32_t frequency);

// gpio_drv.c
extern void GPIOPin_Init(uint8_t pinx, bool dir,uint8_t pull);
extern void GPIOPort_Init(uint8_t portx, uint8_t dir, uint8_t pull);
extern bool GPIOPin_Read(uint8_t pinx);
extern void GPIOPin_Write(uint8_t pinx, bool state);
extern uint8_t GPIOPort_Read(uint8_t portx);
extern void GPIOPort_Write(uint8_t portx, uint8_t state);
extern void GINTPin_Init(uint8_t pinx, uint8_t triggerMode,bool intEnable);
extern void GINTInterrupt_Init(uint8_t PINx,uint8_t IntMode);

// lcd_drv.c
extern void LCD_Init(uint8_t type,uint16_t pixelX, uint16_t pixelY);

// pwm_func.c
extern void Backlight_Init(void);
extern void Backlight_Adjust(uint8_t brightness);
extern void Backlight_Enable(bool state);

// rtp_drv.c
extern void RTP_Init(void);
extern bool RTP_Calibrate(void);
extern bool RTP_ReadXY(uint16_t *x, uint16_t *y);

// spi_drv.c
extern void SPI_Init(SPI_TypeDef* SPIx,bool mode);
extern void SPIMaster_SendByte(SPI_TypeDef* SPIx,uint8_t data);
extern void SPIMaster_ReadBytes(SPI_TypeDef* SPIx,uint8_t *buffer,uint16_t length);
extern bool SPIMaster_ReceiveByte(SPI_TypeDef* SPIx,uint8_t *data);
extern bool SPIMaster_ReceiveBytes(SPI_TypeDef* SPIx,uint8_t *buffer,uint16_t length);
extern void SPISlave_SendByte(SPI_TypeDef* SPIx,uint8_t data);
extern void SPISlave_ReadBytes(SPI_TypeDef* SPIx,uint8_t *buffer,uint16_t length);
extern bool SPI_SlaveReceiveByte(SPI_TypeDef* SPIx, UINT8 *data);
extern bool SPISlave_ReceiveBytes(SPI_TypeDef* SPIx,uint8_t *buffer,uint16_t length);


// i2c_drv.c
extern void I2CMaster_Init(I2C_TypeDef *I2Cx, uint8_t preScaler);
extern void I2CSlave_Init(I2C_TypeDef *I2Cx, uint16_t addrID);
extern bool I2CMaster_SendBytes(I2C_TypeDef *I2Cx,u16 addrID, u8 *buffer, u16 length);
extern bool I2CSlave_ReceiveBytes(I2C_TypeDef *I2Cx,uint8_t *buffer,uint16_t length);
	
// sdio_func.c
extern uint8_t SD_Init(void);
extern uint8_t SD_ReadSector(uint32_t sector,uint8_t *buffer);
extern uint8_t SD_ReadSectors(uint32_t sector,uint8_t *buffer, uint32_t sectorCount);
extern uint8_t SD_WriteSector(uint32_t sector, const uint8_t *data);
extern uint8_t SD_WriteSectors(uint32_t sector, const uint8_t *data, uint32_t sectorCount);

// eflash_drv.h
extern void EFLASH_Init(uint32_t sys_clk_khz); // sys_clk_khz is alawys g_sys_clk/1000
extern uint8_t EFLASH_ErasePage(uint32_t pageAddr);
extern uint8_t EFLASH_ReadByte(uint32_t addr);
extern void EFLASH_ReadBytes(uint32_t address, uint8_t *buffer, uint32_t length);
extern uint16_t EFLASH_ReadHWord(uint32_t addr);
extern uint32_t EFLASH_ReadWord(uint32_t addr);
extern void EFLASH_ReadWords(uint32_t addr, uint32_t *buffer, uint32_t length);
extern uint8_t EFLASH_WriteWord(uint32_t addr, uint32_t data);
extern uint8_t EFLASH_WriteWords(uint32_t addr, uint32_t *des, uint32_t length);
extern uint8_t EFLASH_WritePage(uint32_t pageBeginAddr, const uint8_t* buffer, uint32_t length);


// flash_drv.h
extern uint8_t Flash_ReadID(void);
extern void FlashPort_Init(void);
// NOR flash
extern void NORFlash_EraseSector(uint32_t sectorNum);
extern void NORFlash_EraseBlock(uint32_t blockNum);
extern void NORFlash_EraseChip(void);
extern uint8_t NORFlash_ReadByte(uint32_t addr);
extern void NORFlash_ReadBytes(uint32_t addr,uint8_t* buffer,uint16_t length);
extern uint16_t NORFlash_ReadHWord(uint32_t addr);
extern uint32_t NORFlash_ReadWord(uint32_t addr);
extern void NORFlash_WriteByte(uint32_t addr,uint8_t data);
extern void NORFlash_WriteBytes(uint32_t addr,uint8_t* buffer,uint16_t length);
extern void NORFlash_WriteHWord(uint32_t addr,uint16_t data);
extern void NORFlash_WriteWord(uint32_t addr,uint32_t data);
extern bool NORFlash_WritePages(uint32_t pageBeginAddr,uint8_t* buffer,uint16_t length,bool check);
// NAND flash
extern void NANDFlash_EraseBlock(uint16_t blockNum);
extern void NANDFlash_EraseChip(void);
extern uint8_t NANDFlash_ReadByte(uint32_t addr);
extern uint16_t NANDFlash_ReadHWord(uint32_t addr);
extern uint32_t NANDFlash_ReadWord(uint32_t addr);
//extern void NANDFlash_ReadBytes(uint32_t addr,uint8_t* buffer,uint16_t length);
//extern void NANDFlash_ReadPages(uint32_t pageBeginAddr,uint8_t* buffer,uint16_t length);
extern void NANDFlash_WriteByte(uint32_t addr,uint8_t data);
extern void NANDFlash_WriteHWord(uint32_t addr,uint16_t data);
extern void NANDFlash_WriteWord(uint32_t addr,uint32_t data);
extern void NANDFlash_WriteBytes(uint32_t addr,uint8_t* buffer,uint16_t length);
extern bool NANDFlash_WritePages(uint32_t pageBeginAddr,uint8_t* buffer,uint16_t length, bool check);


// Drawing function
extern void Display_8BitHex(uint16_t x,uint16_t y,uint8_t data,uint16_t fgColor,uint16_t bgColor);
extern void Display_16BitHex(uint16_t x,uint16_t y,uint16_t data,uint16_t fgColor,uint16_t bgColor);
extern void Display_32BitHex(uint16_t x,uint16_t y,uint32_t data,uint16_t fgColor,uint16_t bgColor);
extern void Display_8BitDec(uint16_t x,uint16_t y,uint8_t data,uint16_t fgColor,uint16_t bgColor);
extern void Display_16BitDec(uint16_t x,uint16_t y,uint16_t data,uint16_t fgColor,uint16_t bgColor);
extern void Display_32BitDec(uint16_t x,uint16_t y,uint32_t data,uint16_t fgColor,uint16_t bgColor);


extern void Screen_Clear(uint16_t color);
extern void Display_16BitBMP(uint16_t x,uint16_t y,unsigned const char *bmp,uint16_t transColor);
extern void Display_Text(uint16_t x,uint16_t y,uint8_t font,uint8_t transparency,uint16_t fgColor,uint16_t bgColor,char *str);





// ft5216.c
//void CTP_Init(uint8_t tyType);	// GT_TOUCH or FP_TOUCH
//bool CTP_ReadXY(uint8_t type,uint16_t *x, uint16_t *y);

//extern void SDRAM_Init(uint32_t freq);

//extern void DAC_Init(void);

//extern void SSI_Init(uint16_t portNo);

// Buzzer
//extern void Buzzer_Init(void);		
//extern void Buzzer_Enable(bool set);

// usbdev_control.c
// extern void USB_Init(void);

#endif
