/**
  ******************************************************************************
  * @file    spi_flash.h
  *
  * @brief   This file contains all the functions prototypes for the apm32_eval_spi_flash
  *
  * @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.
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __SPI_FLASH_H
#define __SPI_FLASH_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "apm32a0xx.h"
#include "apm32a0xx_spi.h"
#include "apm32a0xx_gpio.h"
#include "apm32a0xx_rcm.h"


/** @addtogroup Examples
  * @{
  */
  
/** @addtogroup SPI_FLASH_Polling
  * @{
  */   
  
/** @defgroup SPI_FLASH_Polling_Macros Macros
  * @{
  */
  
#define sFLASH_CMD_RDSR           0x05  
#define sFLASH_CMD_SE             0xD8  
#define sFLASH_CMD_RDID           0x9F  
#define sFLASH_CMD_BE             0xC7  

#define sFLASH_CMD_WRITE          0x02  
#define sFLASH_CMD_READ           0x03  
#define sFLASH_CMD_WRSR           0x01  
#define sFLASH_CMD_WREN           0x06  

#define sFLASH_DUMMY_BYTE         0xA5
#define sFLASH_SPI_PAGESIZE       0x100
      
#define sFLASH_M25Q_ID          0xEF4017

#define sFLASH_WIP_FLAG           0x01  

#define sFLASH_CS_PIN                    GPIO_PIN_4                 
#define sFLASH_CS_GPIO_PORT              GPIOA                       
#define sFLASH_CS_GPIO_CLK               RCM_AHB_PERIPH_GPIOA
#define sFLASH_SPI                       SPI1
#define sFLASH_SPI_CLK                   RCM_APB2_PERIPH_SPI1
#define sFLASH_SPI_SCK_PIN               GPIO_PIN_5                 
#define sFLASH_SPI_SCK_GPIO_PORT         GPIOA                       
#define sFLASH_SPI_SCK_GPIO_CLK          RCM_AHB_PERIPH_GPIOA
#define sFLASH_SPI_MISO_PIN              GPIO_PIN_6                  
#define sFLASH_SPI_MISO_GPIO_PORT        GPIOA                       
#define sFLASH_SPI_MISO_GPIO_CLK         RCM_AHB_PERIPH_GPIOA
#define sFLASH_SPI_MOSI_PIN              GPIO_PIN_7                  
#define sFLASH_SPI_MOSI_GPIO_PORT        GPIOA                      
#define sFLASH_SPI_MOSI_GPIO_CLK         RCM_AHB_PERIPH_GPIOA

#define sFLASH_CS_HIGH     GPIO_SetBit(GPIOA, GPIO_PIN_4)  
#define sFLASH_CS_LOW       GPIO_ClearBit(GPIOA, GPIO_PIN_4)
 
/**
  * @} end of group SPI_FLASH_Polling_Macros*/ 
 

/** @defgroup SPI_FLASH_Polling_Functions Functions
  * @{
  */
void sFLASH_LowLevelReset(void);
void sFLASH_Reset(void);
void sFLASH_Config(void);
void sFLASH_SectorErase(uint32_t SectorAddr);
void sFLASH_FullErase(void);
void sFLASH_PageProgram(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite);
void sFLASH_WriteData(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite);
void sFLASH_ReadData(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead);
uint32_t sFLASH_ReadID(void);
void sFLASH_ReadSequenceStart(uint32_t ReadAddr);

uint8_t sFLASH_ReadOneByte(void);
uint8_t sFLASH_TxOneByte(uint8_t byte);
uint16_t sFLASH_TxOneHalfWord(uint16_t HalfWord);
void sFLASH_WriteEnable(void);
void sFLASH_WaitForWriteEnd(void);

#ifdef __cplusplus
}
#endif

#endif /* __SPI_FLASH_H */
/**
  * @}
  */

/**
  * @}
  */ 

/**
  * @}
  */  

