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_32MB_DEVID 0x15
59 #define WINBOND_16MB_DEVID 0x14
60 #define WINBOND_8MB_DEVID 0x13
61 #define WINBOND_4MB_DEVID 0x12
62 #define WINBOND_2MB_DEVID 0x11
63 #define WINBOND_1MB_DEVID 0x10
64 #define WINBOND_512KB_DEVID 0x05
66 #define PAGESIZE 0x100
67 #define WINBOND_WRITE_DELAY 0x02
69 #define SPI_CLK 48000000
75 #define DUMMYBYTE 0xEE
77 #define NULLINT 0x0000
78 #define NO_CONTINUE 0x00
81 #define maxAddress capacity
83 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
84 // List of Error codes //
85 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
87 #define CALLBEGIN 0x01
88 #define UNKNOWNCHIP 0x02
89 #define UNKNOWNCAP 0x03
91 #define OUTOFBOUNDS 0x05
92 #define CANTENWRITE 0x06
93 #define PREVWRITTEN 0x07
95 #define NOSUSPEND 0x09
96 #define UNKNOWNERROR 0xFF
100 #define MAX_SECTORS 16
102 //#define FLASH_BAUD 24000000
103 #define FLASH_MINFAST 24000000 //33000000
104 #define FLASH_BAUD MCK/2
105 #define FLASH_FASTBAUD MCK
106 #define FLASH_MINBAUD FLASH_FASTBAUD
108 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
110 bool FlashInit(void);
111 void Flash_UniqueID(uint8_t *uid
);
112 void FlashStop(void);
114 void FlashSetup(uint32_t baudrate
);
115 bool Flash_CheckBusy(uint32_t timeout
);
116 uint8_t Flash_ReadStat1(void);
117 uint16_t FlashSendByte(uint32_t data
);
118 uint16_t FlashSendLastByte(uint32_t data
);
122 void FlashmemSetSpiBaudrate(uint32_t baudrate
);
123 bool Flash_WaitIdle(void);
124 void Flash_TransferAdresse(uint32_t address
);
126 void Flash_WriteEnable(void);
127 bool Flash_WipeMemoryPage(uint8_t page
);
128 bool Flash_WipeMemory(void);
129 bool Flash_Erase4k(uint8_t block
, uint8_t sector
);
130 //bool Flash_Erase32k(uint32_t address);
131 bool Flash_Erase64k(uint8_t block
);
134 uint8_t manufacturer_id
;
137 } flash_device_type_t
; // extra device_id used for the JEDEC ID read via cmd 9F
138 bool Flash_ReadID(flash_device_type_t
*result
, bool read_jedec
);
140 uint16_t Flash_ReadData(uint32_t address
, uint8_t *out
, uint16_t len
);
141 uint16_t Flash_ReadDataCont(uint32_t address
, uint8_t *out
, uint16_t len
);
142 uint16_t Flash_Write(uint32_t address
, uint8_t *in
, uint16_t len
);
143 uint16_t Flash_WriteData(uint32_t address
, uint8_t *in
, uint16_t len
);
144 uint16_t Flash_WriteDataCont(uint32_t address
, uint8_t *in
, uint16_t len
);
145 void Flashmem_print_status(void);
146 void Flashmem_print_info(void);
148 #endif // #ifndef AS_BOOTROM