1 //-----------------------------------------------------------------------------
2 // Borrowed initially from Arduino SPIFlash Library v.2.5.0
3 // Copyright (C) 2015 by Prajwal Bhattaram.
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // See LICENSE.txt for the text of the license.
16 //-----------------------------------------------------------------------------
17 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
18 // Common Instructions //
19 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
30 #define READSTAT1 0x05
31 #define READSTAT2 0x35
32 #define WRITESTAT 0x01
34 #define WRITEDISABLE 0x04
35 #define WRITEENABLE 0x06
41 #define SECTORERASE 0x20
42 #define BLOCK32ERASE 0x52
43 #define BLOCK64ERASE 0xD8
44 #define CHIPERASE 0xC7
46 #define UNIQUE_ID 0x4B
48 // Not used or not support command
50 #define POWERDOWN 0xB9
54 // Flash busy timeout: 20ms is the strict minimum when writing 256kb
55 #define BUSY_TIMEOUT 200000L
57 #define WINBOND_MANID 0xEF
58 #define WINBOND_2MB_DEVID 0x11
59 #define WINBOND_1MB_DEVID 0x10
60 #define WINBOND_512KB_DEVID 0x05
62 #define PAGESIZE 0x100
63 #define WINBOND_WRITE_DELAY 0x02
65 #define SPI_CLK 48000000
71 #define DUMMYBYTE 0xEE
73 #define NULLINT 0x0000
74 #define NO_CONTINUE 0x00
77 #define maxAddress capacity
79 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
80 // List of Error codes //
81 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
83 #define CALLBEGIN 0x01
84 #define UNKNOWNCHIP 0x02
85 #define UNKNOWNCAP 0x03
87 #define OUTOFBOUNDS 0x05
88 #define CANTENWRITE 0x06
89 #define PREVWRITTEN 0x07
91 #define NOSUSPEND 0x09
92 #define UNKNOWNERROR 0xFF
96 #define MAX_SECTORS 16
98 //#define FLASH_BAUD 24000000
99 #define FLASH_MINFAST 24000000 //33000000
100 #define FLASH_BAUD MCK/2
101 #define FLASH_FASTBAUD MCK
102 #define FLASH_MINBAUD FLASH_FASTBAUD
104 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
106 bool FlashInit(void);
107 void Flash_UniqueID(uint8_t *uid
);
108 void FlashStop(void);
110 void FlashSetup(uint32_t baudrate
);
111 bool Flash_CheckBusy(uint32_t timeout
);
112 uint8_t Flash_ReadStat1(void);
113 uint16_t FlashSendByte(uint32_t data
);
114 uint16_t FlashSendLastByte(uint32_t data
);
118 void FlashmemSetSpiBaudrate(uint32_t baudrate
);
119 bool Flash_WaitIdle(void);
120 void Flash_TransferAdresse(uint32_t address
);
122 void Flash_WriteEnable(void);
123 bool Flash_WipeMemoryPage(uint8_t page
);
124 bool Flash_WipeMemory(void);
125 bool Flash_Erase4k(uint8_t block
, uint8_t sector
);
126 //bool Flash_Erase32k(uint32_t address);
127 bool Flash_Erase64k(uint8_t block
);
130 uint8_t manufacturer_id
;
132 } flash_device_type_90_t
; // to differentiate from JDEC ID via cmd 9F
133 bool Flash_ReadID_90(flash_device_type_90_t
*result
);
135 uint16_t Flash_ReadData(uint32_t address
, uint8_t *out
, uint16_t len
);
136 uint16_t Flash_ReadDataCont(uint32_t address
, uint8_t *out
, uint16_t len
);
137 uint16_t Flash_Write(uint32_t address
, uint8_t *in
, uint16_t len
);
138 uint16_t Flash_WriteData(uint32_t address
, uint8_t *in
, uint16_t len
);
139 uint16_t Flash_WriteDataCont(uint32_t address
, uint8_t *in
, uint16_t len
);
140 void Flashmem_print_status(void);
141 void Flashmem_print_info(void);
143 #endif // #ifndef AS_BOOTROM