/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2023. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */
/******************************************************************************
 * @version: V1.0.0 demo
 ******************************************************************************/

/*!
* @file spi_w25x16.h
*
* @brief This file is the library function of spi_w25x16
*
*/

#ifndef _SPI_W25X16_H
#define _SPI_W25X16_H


#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/* ===========================================  Includes  =========================================== */
#include "string.h"
#include "stdint.h"
    
/* ============================================  Define  ============================================ */
#define W25X16_BUSY_MSK         (0x01)
#define W25X16_WEL_MSK          (0x02)

#define W25X16_CMD_WR_EN        (0x06)/* enable write */
#define W25X16_CMD_WR_DIS       (0x07)/* disable write */
#define W25X16_CMD_RD_STS       (0x05)/* read status register  */
#define W25X16_CMD_WR_STS       (0x01)/* write status register */
#define W25X16_CMD_RD_DAT       (0x03)/* read data */
#define W25X16_CMD_FR_DAT       (0x0B)/* fast read data*/
#define W25X16_CMD_WR_DAT       (0x02)/* page write */
#define W25X16_CMD_ER_SEC       (0x20)/* erase sector */
#define W25X16_CMD_ER_BLK       (0xD8)/* erase block */
#define W25X16_CMD_ER_CHP       (0xC7)/* erase all */
#define W25X16_CMD_DEVICEID     (0x90)/* read device ID */

#define W25X16_SEC_SIZE         (4096)/* 4KB */
#define W25X16_ADDR_MAX         (0x200000U)

#define W25X16_DATA_BUF_LEN     (1030)

#define W25X16_TEST_DAT_LEN     (1024)
#define W25X16_WRRD_DAT_ADDR    (0x00)


/* ===========================================  Typedef  ============================================ */

/* ==========================================  Variables  =========================================== */
extern uint8_t g_SpiWrDataBuf[W25X16_DATA_BUF_LEN];
extern uint8_t g_SpiRdDataBuf[W25X16_DATA_BUF_LEN];

/* ====================================  Functions declaration  ===================================== */

/**
* @brief    Write specified length byte data to w25x16
*
* @param[in] startAddr: start Addr
* @param[in] pDataBuf:  data buffer
* @param[in] dataLen:   data len
* @return   void
*
*/
extern void W25X16_WriteBytes(uint32_t startAddr, uint8_t *pDataBuf, uint16_t dataLen);
/**
* @brief   Read specified length byte data from w25x16
*
* @param[in] startAddr: start addr
* @param[in] pDataBuf:  data buffer
* @param[in] dataLen:   data len
* @return   void
*
*/
extern void W25X16_ReadBytes(uint32_t startAddr, uint8_t *pDataBuf, uint16_t dataLen);

/**
* @brief    erase W25X16.
*
* @param[in] startAddr:  start Addr
* @param[in] secErNum: sector number
* @return   void
*
*/
extern void W25X16_ErsSector(uint32_t startAddr, uint8_t secErNum);

/*!
 * @brief SPI模块初始化配置，主机模式
 *
 * @param none
 *
 * @return none
 */
extern void SPI_Master_Init(void);

/*!
* @brief  Communication test of W25x16
*
* @param[in] none
* @return none
*/
extern void W25X16_Test(void);

/**
* @prototype W25X16_ReadDeviceID(void)
*
* @param[in] void
* @return    void
*
* @brief    ??W25X16???.
*/
extern void W25X16_ReadDeviceID(void);

/* ======================================  Functions define  ======================================== */

#ifdef __cplusplus
}
#endif

#endif /* _SPI_W25X16_H */

/* =============================================  EOF  ============================================== */
