2022-01-06 09:19:46 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Borrowed initially from Arduino SPIFlash Library v.2.5.0
|
|
|
|
// Copyright (C) 2015 by Prajwal Bhattaram.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// See LICENSE.txt for the text of the license.
|
|
|
|
//-----------------------------------------------------------------------------
|
2018-02-13 21:12:28 +08:00
|
|
|
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
|
2019-03-10 03:34:41 +08:00
|
|
|
// Common Instructions //
|
2018-02-13 21:12:28 +08:00
|
|
|
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
|
|
|
|
#ifndef __FLASHMEM_H
|
|
|
|
#define __FLASHMEM_H
|
|
|
|
|
2019-08-08 22:57:33 +08:00
|
|
|
#include "common.h"
|
2018-02-13 21:12:28 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
// Used Command
|
|
|
|
#define ID 0x90
|
|
|
|
#define MANID 0x90
|
|
|
|
#define JEDECID 0x9F
|
2018-03-19 22:58:50 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
#define READSTAT1 0x05
|
|
|
|
#define READSTAT2 0x35
|
|
|
|
#define WRITESTAT 0x01
|
2018-03-19 22:58:50 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
#define WRITEDISABLE 0x04
|
|
|
|
#define WRITEENABLE 0x06
|
2018-03-19 22:58:50 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
#define READDATA 0x03
|
|
|
|
#define FASTREAD 0x0B
|
|
|
|
#define PAGEPROG 0x02
|
2018-03-19 22:58:50 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
#define SECTORERASE 0x20
|
|
|
|
#define BLOCK32ERASE 0x52
|
|
|
|
#define BLOCK64ERASE 0xD8
|
|
|
|
#define CHIPERASE 0xC7
|
2018-03-19 22:58:50 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
#define UNIQUE_ID 0x4B
|
2018-03-19 22:58:50 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
// Not used or not support command
|
|
|
|
#define RELEASE 0xAB
|
|
|
|
#define POWERDOWN 0xB9
|
|
|
|
#define SUSPEND 0x75
|
|
|
|
#define RESUME 0x7A
|
2018-03-19 22:58:50 +08:00
|
|
|
|
2019-05-01 22:48:04 +08:00
|
|
|
// Flash busy timeout: 20ms is the strict minimum when writing 256kb
|
2019-07-15 07:13:11 +08:00
|
|
|
#define BUSY_TIMEOUT 200000L
|
2018-02-13 21:12:28 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
#define WINBOND_MANID 0xEF
|
|
|
|
#define WINBOND_DEVID 0x11
|
|
|
|
#define PAGESIZE 0x100
|
2018-09-03 06:02:44 +08:00
|
|
|
#define WINBOND_WRITE_DELAY 0x02
|
2018-02-13 21:12:28 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
#define SPI_CLK 48000000
|
2018-03-19 22:58:50 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
#define BUSY 0x01
|
|
|
|
#define WRTEN 0x02
|
|
|
|
#define SUS 0x40
|
2018-03-19 22:58:50 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
#define DUMMYBYTE 0xEE
|
|
|
|
#define NULLBYTE 0x00
|
|
|
|
#define NULLINT 0x0000
|
|
|
|
#define NO_CONTINUE 0x00
|
|
|
|
#define PASS 0x01
|
|
|
|
#define FAIL 0x00
|
|
|
|
#define maxAddress capacity
|
2018-02-13 21:12:28 +08:00
|
|
|
|
|
|
|
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
|
2019-03-10 03:34:41 +08:00
|
|
|
// List of Error codes //
|
2018-02-13 21:12:28 +08:00
|
|
|
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
|
2019-03-10 03:34:41 +08:00
|
|
|
#define SUCCESS 0x00
|
|
|
|
#define CALLBEGIN 0x01
|
|
|
|
#define UNKNOWNCHIP 0x02
|
|
|
|
#define UNKNOWNCAP 0x03
|
|
|
|
#define CHIPBUSY 0x04
|
|
|
|
#define OUTOFBOUNDS 0x05
|
|
|
|
#define CANTENWRITE 0x06
|
|
|
|
#define PREVWRITTEN 0x07
|
|
|
|
#define LOWRAM 0x08
|
|
|
|
#define NOSUSPEND 0x09
|
|
|
|
#define UNKNOWNERROR 0xFF
|
2018-02-13 21:12:28 +08:00
|
|
|
|
2018-04-20 22:11:10 +08:00
|
|
|
// List of blocks
|
2019-03-10 03:34:41 +08:00
|
|
|
#define MAX_BLOCKS 4
|
|
|
|
#define MAX_SECTORS 16
|
2018-04-20 22:11:10 +08:00
|
|
|
|
2018-09-06 11:15:52 +08:00
|
|
|
//#define FLASH_BAUD 24000000
|
|
|
|
#define FLASH_MINFAST 24000000 //33000000
|
|
|
|
#define FLASH_BAUD MCK/2
|
|
|
|
#define FLASH_FASTBAUD MCK
|
|
|
|
#define FLASH_MINBAUD FLASH_FASTBAUD
|
|
|
|
|
2018-02-20 19:03:11 +08:00
|
|
|
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
|
2018-09-06 11:15:52 +08:00
|
|
|
|
2020-05-10 22:59:38 +08:00
|
|
|
bool FlashInit(void);
|
2023-02-18 08:59:00 +08:00
|
|
|
void Flash_UniqueID(uint8_t *uid);
|
2018-02-20 19:03:11 +08:00
|
|
|
void FlashStop(void);
|
2023-02-18 08:59:00 +08:00
|
|
|
|
|
|
|
void FlashSetup(uint32_t baudrate);
|
|
|
|
bool Flash_CheckBusy(uint32_t timeout);
|
2018-02-20 19:03:11 +08:00
|
|
|
uint8_t Flash_ReadStat1(void);
|
2018-03-19 22:58:50 +08:00
|
|
|
uint16_t FlashSendByte(uint32_t data);
|
2023-02-18 08:59:00 +08:00
|
|
|
uint16_t FlashSendLastByte(uint32_t data);
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef AS_BOOTROM
|
|
|
|
void FlashmemSetSpiBaudrate(uint32_t baudrate);
|
|
|
|
bool Flash_WaitIdle(void);
|
2018-09-06 11:15:52 +08:00
|
|
|
void Flash_TransferAdresse(uint32_t address);
|
2018-04-19 06:27:44 +08:00
|
|
|
|
2020-05-10 22:59:38 +08:00
|
|
|
void Flash_WriteEnable(void);
|
2018-05-03 18:15:03 +08:00
|
|
|
bool Flash_WipeMemoryPage(uint8_t page);
|
2020-05-10 22:59:38 +08:00
|
|
|
bool Flash_WipeMemory(void);
|
2018-04-20 22:11:10 +08:00
|
|
|
bool Flash_Erase4k(uint8_t block, uint8_t sector);
|
|
|
|
//bool Flash_Erase32k(uint32_t address);
|
|
|
|
bool Flash_Erase64k(uint8_t block);
|
2018-04-19 06:27:44 +08:00
|
|
|
|
2023-02-19 04:16:50 +08:00
|
|
|
typedef struct {
|
|
|
|
uint8_t manufacturer_id;
|
|
|
|
uint8_t device_id;
|
|
|
|
} flash_device_type_90_t; // to differentiate from JDEC ID via cmd 9F
|
|
|
|
bool Flash_ReadID_90(flash_device_type_90_t* result);
|
|
|
|
|
2018-04-19 06:27:44 +08:00
|
|
|
uint16_t Flash_ReadData(uint32_t address, uint8_t *out, uint16_t len);
|
2018-09-03 06:02:44 +08:00
|
|
|
uint16_t Flash_ReadDataCont(uint32_t address, uint8_t *out, uint16_t len);
|
2018-09-23 11:29:55 +08:00
|
|
|
uint16_t Flash_Write(uint32_t address, uint8_t *in, uint16_t len);
|
2018-04-19 06:27:44 +08:00
|
|
|
uint16_t Flash_WriteData(uint32_t address, uint8_t *in, uint16_t len);
|
2018-09-03 06:02:44 +08:00
|
|
|
uint16_t Flash_WriteDataCont(uint32_t address, uint8_t *in, uint16_t len);
|
2018-03-31 16:35:40 +08:00
|
|
|
void Flashmem_print_status(void);
|
2019-05-15 02:57:56 +08:00
|
|
|
void Flashmem_print_info(void);
|
2023-02-18 08:59:00 +08:00
|
|
|
|
|
|
|
#endif // #ifndef AS_BOOTROM
|
|
|
|
|
2018-03-31 16:35:40 +08:00
|
|
|
|
2019-03-12 07:12:26 +08:00
|
|
|
#endif
|