-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSST26VF032B.c
More file actions
392 lines (318 loc) · 9.08 KB
/
Copy pathSST26VF032B.c
File metadata and controls
392 lines (318 loc) · 9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
* SST26VF032B.c
*
* Created on: 25-Dec-2024
* Author: 14169
*/
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_memmap.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/ssi.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/interrupt.h"
#include "inc/hw_ssi.h"
#include "inc/hw_ints.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "SST26VF032B.h"
/* Initializer Functions */
void Clock_Init(){
ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_CFG_VCO_160),80000000);
}
void Led_Init(){
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ);
GPIOPinTypeGPIOOutput(GPIO_PORTJ_BASE, GPIO_PIN_0 | GPIO_PIN_1);
}
void SPI_Init(){
/*
* 360 NW uses the SSI3 Module of TM4CNCPDT controller
* By default only SPI mode can be used in SST26VF032B SPI flash
* SST26VF032B/SST26VF032BA -> MAX CLOCK 104 MHz / 80 MHz
* Normal Read - 40 Mhz
*/
Clock_Init();
Led_Init();
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOQ);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOQ)); //wait for enable
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI3);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_SSI3)); //wait for enable
GPIOPinConfigure(GPIO_PQ0_SSI3CLK);
GPIOPinConfigure(GPIO_PQ1_SSI3FSS);
GPIOPinConfigure(GPIO_PQ2_SSI3XDAT0); //Tx
GPIOPinConfigure(GPIO_PQ3_SSI3XDAT1); //Rx
GPIOPinTypeGPIOOutput(GPIO_PORTQ_BASE, GPIO_PIN_1);
GPIOPinTypeSSI(GPIO_PORTQ_BASE,GPIO_PIN_0|GPIO_PIN_2|GPIO_PIN_3);
// SSIAdvModeSet(SSI3_BASE, SSI_ADV_MODE_READ_WRITE);
SSIConfigSetExpClk(SSI3_BASE, ui32SysClock, SSI_FRF_MOTO_MODE_3, SSI_MODE_MASTER, 1000000, 8);
SSIEnable(SSI3_BASE);
// flash precautionary cmds
GlobalWriteProtectUnlock();
LockBPR();
}
bool TEST_spi_init(){
uint32_t id = ReadJEDECID();
id &= 0x00FFFFFF;
return (id == 0x00BF2642)? true : false;
}
static inline void send(uint32_t base, uint8_t cmd) {
uint32_t discard;
SSIDataPut(base, cmd);
while (SSIBusy(base));
SSIDataGet(base, &discard); // discard dummy byte received while sending command
}
static inline void receive(uint32_t base, uint8_t* address){
uint32_t tempData; // Temporary variable to hold the dummy received byte
SSIDataPut(base, DUMMY);
while (SSIBusy(base));
SSIDataGet(base, &tempData);
*address = (uint8_t)tempData; // Extract the lower 8 bits
}
uint32_t ReadJEDECID(void) {
uint8_t manufacturerID, memoryType, capacity;
CHIP_SELECT(0);
uint32_t temp;
while (SSIDataGetNonBlocking(SSI3_BASE, &temp));
// Send command and discard dummy byte
send(SSI3_BASE, FLASH_ID);
receive(SSI3_BASE, &manufacturerID);
receive(SSI3_BASE, &memoryType);
receive(SSI3_BASE, &capacity);
CHIP_SELECT(1);
// Combine the JEDEC ID into a single 24-bit value
uint32_t jedecID = ((manufacturerID & 0xFF) << 16) |
((memoryType & 0xFF) << 8) |
(capacity & 0xFF);
return jedecID;
}
uint8_t ReadStatus()
{
uint8_t status;
uint32_t temp;
CHIP_SELECT(0);
while(SSIDataGetNonBlocking(SSI3_BASE, &temp)); // clear RX FIFO
send(SSI3_BASE, RDSR);
receive(SSI3_BASE, &status);
CHIP_SELECT(1);
return status;
}
uint8_t ReadConfig()
{
uint8_t config;
uint32_t temp;
CHIP_SELECT(0);
while(SSIDataGetNonBlocking(SSI3_BASE, &temp)); // clear RX FIFO
send(SSI3_BASE, RDCR);
receive(SSI3_BASE, &config);
CHIP_SELECT(1);
return config;
}
void wait(){
// Wait until the flash is no longer busy
uint32_t status = 0;
do {
status = ReadStatus();
} while(status & 0x01); // Check the BUSY bit (bit 0)
}
bool ReadFlash(uint32_t addr, uint32_t bytes, uint8_t* buffer, bool fast){
// read until CE is asserted high.
CHIP_SELECT(0);
uint32_t i = 0;
// send read cmd frames
SSIDataPut(SSI3_BASE, fast ? FREAD : READ);
SSIDataPut(SSI3_BASE, (addr >> 16) & 0xFF);
SSIDataPut(SSI3_BASE, (addr >> 8) & 0xFF);
SSIDataPut(SSI3_BASE, addr & 0xFF);
if(fast){
SSIDataPut(SSI3_BASE, DUMMY); //send dummy
}
while(SSIBusy(SSI3_BASE)); // wait for tx
uint32_t temp;
while(SSIDataGetNonBlocking(SSI3_BASE, &temp));
for(i = 0; i < bytes; i++){
receive(SSI3_BASE, &buffer[i]);
}
CHIP_SELECT(1);
/*
* The End-of-Transmission (EOT) interrupt indicates that
* the data has been transmitted completely
* and is only valid for Master mode devices/operations.
* The interrupt can also indicate that read
* data is ready immediately, without waiting for the
* receive FIFO time-out period to complete.
*/
if(SSIIntStatus(SSI3_BASE, 0) & SSI_TXEOT){
return true;
}
return false;
}
void ReadBPR(uint8_t* bpr){
CHIP_SELECT(0);
uint8_t i;
send(SSI3_BASE, RBPR);
for(i = 0; i < 10; i++){
receive(SSI3_BASE, &bpr[i]);
}
CHIP_SELECT(1);
}
void ResetFlash(){
//reset enable
CHIP_SELECT(0);
send(SSI3_BASE, RSTEN);
CHIP_SELECT(1);
// reset
CHIP_SELECT(0);
send(SSI3_BASE, RST);
CHIP_SELECT(1);
}
void ReadBurst(uint8_t len){
}
/*
WREN - enabled for below commands
Sector Erase, Block Erase, Chip Erase, Page Program,
Program Security ID, Lockout Security ID, Write Block
Protection Register, Lock-Down Block Protection Register,
nonvolatile Write Lock Lock-Down Register,
SPI Quad Page program, and Write STATUS Register.
*/
inline void WriteEnable(bool state){
CHIP_SELECT(0);
send(SSI3_BASE, (state)? WREN : WRDI);
CHIP_SELECT(1);
}
bool PageProgram(uint32_t addr, uint32_t size, uint8_t* data){
uint32_t i = 0;
WriteEnable(1);
CHIP_SELECT(0);
SSIDataPut(SSI3_BASE, PP);
SSIDataPut(SSI3_BASE, (addr >> 16) & 0xFF);
SSIDataPut(SSI3_BASE, (addr >> 8) & 0xFF);
SSIDataPut(SSI3_BASE, addr & 0xFF);
SSIDataGet(SSI3_BASE, &i); // clear dummy byte
size = (size>256)? 256 : size;
for(i = 0;i < size; i++){
SSIDataPut(SSI3_BASE, data[i]);
while(SSIBusy(SSI3_BASE)); // wait for tx
}
CHIP_SELECT(1);
wait();
if(SSIIntStatus(SSI3_BASE, 0) & SSI_TXEOT){
return true;
}
return false;
}
//void writeDisable(){
// CHIP_SELECT(0);
// SSIDataPut(SSI3_BASE, WRDI);
// while(SSIBusy(SSI3_BASE)); // wait for tx
// CHIP_SELECT(1);
//}
void WriteStatus(uint8_t config){
//write config register
WriteEnable(1);
CHIP_SELECT(0);
SSIDataPut(SSI3_BASE, WRSR);
SSIDataPut(SSI3_BASE, 0xFF); //dummy byte
SSIDataPut(SSI3_BASE, config);
while(SSIBusy(SSI3_BASE)); // wait for tx
CHIP_SELECT(1);
}
void GlobalWriteProtectUnlock(){
WriteEnable(1);
CHIP_SELECT(0);
send(SSI3_BASE, ULBPR);
CHIP_SELECT(1);
}
void LockBPR(){
WriteEnable(1);
CHIP_SELECT(0);
send(SSI3_BASE, LBPR);
CHIP_SELECT(1);
}
bool SectorErase(uint32_t addr){
WriteEnable(1);
CHIP_SELECT(0);
SSIDataPut(SSI3_BASE, SE);
SSIDataPut(SSI3_BASE, (addr >> 16) & 0xFF);
SSIDataPut(SSI3_BASE, (addr >> 8) & 0xFF);
SSIDataPut(SSI3_BASE, addr & 0xFF);
while(SSIBusy(SSI3_BASE)); // wait for tx
CHIP_SELECT(1);
wait();
// verify that trasnmit is done by checking tx fifo empty
return((HWREG(SSI3_BASE + SSI_O_SR) & SSI_SR_TFE) ? true : false);
}
void SetBurst(uint8_t bytes){
// factory default 8
CHIP_SELECT(0);
send(SSI3_BASE, SB);
switch(bytes){
case 8:
send(SSI3_BASE, BYTE_8);
break;
case 16:
send(SSI3_BASE, BYTE_16);
break;
case 32:
send(SSI3_BASE, BYTE_32);
break;
case 64:
send(SSI3_BASE, BYTE_64);
break;
}
CHIP_SELECT(1);
}
/*
// for aligned read access
void ReadBurst(uint8_t bytes){
}
*/
void BlockErase(uint32_t addr){
WriteEnable(1);
CHIP_SELECT(0);
SSIDataPut(SSI3_BASE, BE);
SSIDataPut(SSI3_BASE, (addr >> 16) & 0xFF);
SSIDataPut(SSI3_BASE, (addr >> 8) & 0xFF);
SSIDataPut(SSI3_BASE, addr & 0xFF);
while(SSIBusy(SSI3_BASE)); // wait for tx
CHIP_SELECT(1);
wait();
}
void ChipErase(void){
WriteEnable(1);
CHIP_SELECT(0);
SSIDataPut(SSI3_BASE, CE);
while(SSIBusy(SSI3_BASE)); // wait for tx
CHIP_SELECT(1);
wait();
}
uint8_t suspend(){
CHIP_SELECT(0);
SSIDataPut(SSI3_BASE, WRSU);
while(SSIBusy(SSI3_BASE)); // wait for tx
CHIP_SELECT(1);
wait();
//erase supended
if(ReadStatus() & STATUS_WSE)
return 1;
//prog suspended
else if(ReadStatus() & STATUS_WSP)
return 2;
return 0; //failed
}
uint8_t resume(){
CHIP_SELECT(0);
SSIDataPut(SSI3_BASE, WRRE);
while(SSIBusy(SSI3_BASE)); // wait for tx
CHIP_SELECT(1);
wait();
//erase resume
if(! (ReadStatus() & STATUS_WSE))
return 1;
//prog resume
else if(! (ReadStatus() & STATUS_WSP))
return 2;
return 0; // failed
}