HomeAut
flash.h
1 /*******************************************************************************
2  * Purpose:
3  ******************************************************************************/
4 
5 #ifndef FLASH_H_
6 #define FLASH_H_
7 
8 
9 /*------------------------------------------------------------------------------
10  * Includes
11  *----------------------------------------------------------------------------*/
12 #include "include.h"
13 
14 
15 /*------------------------------------------------------------------------------
16  * Macros & definitions
17  *----------------------------------------------------------------------------*/
18 
19 #ifdef CONFIG_MODULE_FLASH_ENABLE
20 
21 
22  // S25FL204K
23 
24 // Write Enable (06h)
25  #define FLASH_COMMAND_WRITE_ENABLE ((uint8_t) 0x06 )
26 
27 // Read Data 03h
28 #define FLASH_COMMAND_READ_DATA ((uint8_t) 0x03 )
29 
30 // Fast Read Data 0Bh
31 #define FLASH_COMMAND_FAST_READ_DATA ((uint8_t) 0x03 )
32 
33 
34 // Page Program 02h
35 #define FLASH_COMMAND_PAGE_PROGRAM ((uint8_t) 0x02 )
36 
37 // Read Status Register
38 #define FLASH_COMMAND_READ_STATUS_REGISTER ((uint8_t) 0x05 )
39 
40 // Write Status Register
41 #define FLASH_COMMAND_WRITE_STATUS_REGISTER ((uint8_t) 0x01 )
42 
43 //Block Erase
44 //(64 kB) D8h A23-A16 A15-A8 A7-A0
45 #define FLASH_COMMAND_BLOCK_ERASE ((uint8_t) 0xD8 )
46 
47 //Sector Erase
48 //(4 kB) 20h A23-A16 A15-A8 A7-A0
49 #define FLASH_COMMAND_SECTOR_ERASE ((uint8_t) 0x20 )
50 
51 //Chip Erase C7h/60h
52 #define FLASH_COMMAND_CHIP_ERASE ((uint8_t) 0xC7 )
53 
54 //Power-down B9h
55 #define FLASH_COMMAND_POWER_DOWN ((uint8_t) 0xB9 )
56 
57 
58  /*
59  Command
60 Name Byte1 Code Byte2 Byte3 Byte4 Byte5 Byte6 N-bytes
61 Write Enable 06h
62 write Disable 04h
63 Read Status
64 Register 05h (S7-S0) (1) (Note 2)
65 Write Status
66 Register 01h S7-S0
67 Read Data 03h A23-A16 A15-A8 A7-A0 (D7-D0) (Next byte) continuous
68 Fast Read 0Bh A23-A16 A15-A8 A7-A0 dummy (D7-D0) (Next byte)
69 continuous
70 Fast Read Dual
71 Output 3Bh A23-A16 A15-A8 A7-A0 dummy
72 I/O= (D6, D4,
73 D2, D0)
74 O= (D7, D5,
75 D3, D1)
76 (One byte per 4
77 clocks,
78 continuous)
79 Page Program 02h A23-A16 A15-A8 A7-A0 (D7-D0) (Next byte) Up to 256
80 bytes
81 Block Erase
82 (64 kB) D8h A23-A16 A15-A8 A7-A0
83 Sector Erase
84 (4 kB) 20h A23-A16 A15-A8 A7-A0
85 Chip Erase C7h/60h
86 Power-down B9h
87 Release
88 Power-down /
89 Device ID
90 ABh dummy dummy dummy (ID7-ID0) (4)
91 Manufacturer /
92 Device ID (3) 90h dummy dummy 00h (M7-M0) (ID7-ID0)
93 JEDEC ID 9Fh (M7-M0)
94 manufacturer
95 (ID15-ID8)
96 Memory Type
97 (ID7-ID0)
98 Capacity
99  */
100 
101 
102 #define FLASH_CS_ACTIVE() HAL_GPIO_WritePin(FLASH_SPI_CS_GPIO_PORT,FLASH_SPI_CS_GPIO_PIN,GPIO_PIN_RESET);
103 #define FLASH_CS_INACTIVE() HAL_GPIO_WritePin(FLASH_SPI_CS_GPIO_PORT,FLASH_SPI_CS_GPIO_PIN,GPIO_PIN_SET);
104 
105 
106 // STATUS REGISTER
107 
108 // WIP: Write in Progress
109 #define FLASH_STATUS_REGISTER_WRITEINPROGRESS_BIT ( 1 << 0)
110 // WEL: Write enable Latch
111 #define FLASH_STATUS_REGISTER_WRITEENABLELATCH_BIT ( 1 << 1)
112 
113 // Block protect bits - BP0
114 #define FLASH_STATUS_REGISTER_BP0_BIT ( 1 << 2)
115 // Block protect bits - BP1
116 #define FLASH_STATUS_REGISTER_BP1_BIT ( 1 << 3)
117 // Block protect bits - BP2
118 #define FLASH_STATUS_REGISTER_BP2_BIT ( 1 << 4)
119 // Block protect bits - BP3
120 #define FLASH_STATUS_REGISTER_BP3_BIT ( 1 << 5)
121 
122 // REV: Reserved Bits
123 #define FLASH_STATUS_REGISTER_RESERVED_BIT ( 1 << 6)
124 // SRP: The Status Register Proctect
125 #define FLASH_STATUS_REGISTER_STATUSREGISTERPROTECTED_BIT ( 1 << 7)
126 
127 
128 
129 
130 /*------------------------------------------------------------------------------
131  * Type definitions
132  *----------------------------------------------------------------------------*/
133 
134 
135 /*------------------------------------------------------------------------------
136  * Global variables
137  *----------------------------------------------------------------------------*/
138 
139 
140 /*------------------------------------------------------------------------------
141  * Local variables
142  *----------------------------------------------------------------------------*/
143 
144 
145 /*------------------------------------------------------------------------------
146  * Local function declarations
147  *----------------------------------------------------------------------------*/
148 
149 
150 /*------------------------------------------------------------------------------
151  * Global function declarations
152  *----------------------------------------------------------------------------*/
153 void FLASH_Init( void );
154 void FLASH_Test ( void );
155 void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi);
156 void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi);
157 
158 uint8_t FLASH_Read(uint32_t address, uint8_t *Buffer, uint8_t size, uint16_t timeout);
159 uint8_t FLASH_Write(uint32_t address, uint8_t *Buffer, uint8_t size, uint16_t timeout);
160 
161 uint8_t FLASH_BlockErase (uint32_t address, uint16_t timeout);
162 uint8_t FLASH_SectorErase (uint32_t address, uint16_t timeout);
163 
164 void FLASH_Test_WithEraseWriteRead ( void );
165 
166 
167 #endif // #ifdef CONFIG_MODULE_FLASH_ENABLE
168 
169 
170 #endif /* FLASH_H_ */