04ef3007bc25733d8c95f55bca3f402d1aa5058d
1 #include "projectconfig.h"
4 #include "core/ssp/ssp.h"
6 #include <r0ketports.h>
11 OP_READARRAY_SLOW
= 0x03,
13 OP_ERASE_BLOCK_4K
= 0x20,
14 OP_ERASE_BLOCK_32K
= 0x52,
15 OP_ERASE_BLOCK_64k
= 0xd8,
17 OP_ERASE_CHIP2
= 0xc7,
19 OP_PROGRAM_PAGE
= 0x02,
20 OP_PROGRAM_SEQ
= 0xad,
21 OP_PROGRAM_SEQ2
= 0xaf,
23 OP_WRITE_ENABLE
= 0x06,
24 OP_WRITE_DISABLE
= 0x04,
26 OP_SECTOR_PROTEXT
= 0x36,
27 OP_SECTOR_UNPROTECT
= 0x39,
28 OP_SECTOR_STATUS
= 0x3c,
30 OP_STATUS_READ
= 0x05,
31 OP_STATUS_WRITE
= 0x01,
33 OP_DEVICEID_READ
= 0x9f,
40 STATUS_SPRL
= 1 << 7, // sector protection registers locked
41 STATUS_SPM
= 1 << 6, // sequential program mode
42 STATUS_EPE
= 1 << 5, // erase/program error
43 STATUS_WPP
= 1 << 4, // write protect pin
44 STATUS_SWP
= 3 << 2, // software protection
45 STATUS_WEL
= 1 << 1, // write enable latch
46 STATUS_BSY
= 1 << 0, // ready/busy
48 STATUS_SWP_NONE
= 0, // no sectors are software-protected
49 STATUS_SWP_SOME
= 1 << 2, // some sectors are software-protected
50 STATUS_SWP_ALL
= 3 << 2 // all sectors are software-protected
57 SECTOR_SIZE_FATFS
= 512
60 #define CS_LOW() gpioSetValue(RB_SPI_CS_DF, 0)
61 #define CS_HIGH() gpioSetValue(RB_SPI_CS_DF, 1)
63 static volatile DSTATUS status
= STA_NOINIT
;
65 static void wait_for_ready() {
66 BYTE reg_status
= 0xFF;
69 xmit_spi(OP_STATUS_READ
);
71 rcvr_spi_m((uint8_t *) ®_status
);
72 } while (reg_status
& STATUS_BSY
);
76 static void dataflash_powerdown() {
78 xmit_spi(OP_POWERDOWN
);
82 static void dataflash_resume() {
88 DSTATUS
dataflash_initialize() {
89 sspInit(0, sspClockPolarity_Low
, sspClockPhase_RisingEdge
);
91 gpioSetDir(RB_SPI_CS_DF
, gpioDirection_Output
);
93 status
&= ~STA_NOINIT
;
98 DSTATUS
dataflash_status() {
102 DRESULT
dataflash_random_read(BYTE
*buff
, DWORD offset
, DWORD length
) {
103 if (!length
) return RES_PARERR
;
104 if (status
& STA_NOINIT
) return RES_NOTRDY
;
105 if (offset
+ length
> PAGE_MAX
* PAGE_SIZE
) return RES_PARERR
;
109 xmit_spi(OP_READARRAY
);
110 xmit_spi((BYTE
)(offset
>> 16));
111 xmit_spi((BYTE
)(offset
>> 8));
112 xmit_spi((BYTE
)offset
);
113 xmit_spi(0x00); // follow up with don't care byte
123 DRESULT
dataflash_read(BYTE
*buff
, DWORD sector
, BYTE count
) {
124 return dataflash_random_read(buff
, sector
* SECTOR_SIZE_FATFS
, count
* SECTOR_SIZE_FATFS
);
128 DRESULT
dataflash_random_write(const BYTE
*buff
, DWORD offset
, DWORD length
) {
129 if (!length
) return RES_PARERR
;
130 if (status
& STA_NOINIT
) return RES_NOTRDY
;
131 if (offset
+ length
> PAGE_MAX
* PAGE_SIZE
) return RES_PARERR
;
136 DWORD pageaddr
= (offset
/ PAGE_SIZE
) * PAGE_SIZE
;
137 DWORD remaining
= PAGE_SIZE
- offset
% PAGE_SIZE
;
139 if (remaining
> length
) {
147 xmit_spi(OP_WRITE_ENABLE
);
150 // read page into the internal buffer
152 xmit_spi(OP_PROGRAM_PAGE
);
153 xmit_spi((BYTE
)(pageaddr
>> 16));
154 xmit_spi((BYTE
)(pageaddr
>> 8));
155 xmit_spi((BYTE
) pageaddr
);
159 } while (--remaining
);
167 DRESULT
dataflash_write(const BYTE
*buff
, DWORD sector
, BYTE count
) {
168 return dataflash_random_write(buff
, sector
* SECTOR_SIZE_FATFS
, count
* SECTOR_SIZE_FATFS
);
170 #endif /* _READONLY */
173 DRESULT
dataflash_ioctl(BYTE ctrl
, void *buff
) {
179 if (ctrl
== CTRL_POWER
) {
181 case 0: /* Sub control code == 0 (POWER_OFF) */
182 dataflash_powerdown();
185 case 1: /* Sub control code == 1 (POWER_ON) */
189 case 2: /* Sub control code == 2 (POWER_GET) */
190 // TODO: figure out a way to retrieve the powerstate
198 if (status
& STA_NOINIT
) return RES_NOTRDY
;
205 case GET_SECTOR_COUNT
:
206 // TODO: read from device ID register
207 *(WORD
*)buff
= PAGE_MAX
* PAGE_SIZE
/ SECTOR_SIZE_FATFS
;
210 case GET_SECTOR_SIZE
:
211 *(WORD
*)buff
= SECTOR_SIZE_FATFS
;
225 #endif /* _USE_IOCTL != 0 */
227 DWORD
get_fattime () {
230 struct tm* tm=mygmtime(getSeconds());
231 DWORD t= (((tm->tm_year-80)<<9)|
This page took 0.057149 seconds and 3 git commands to generate.