Skip to content

Commit b92d3e1

Browse files
committed
stmhal: Add fatfs support, working with flash and SD card.
1 parent 9e5ea4d commit b92d3e1

12 files changed

Lines changed: 610 additions & 50 deletions

File tree

stmhal/Makefile

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ CMSIS_DIR=cmsis
1010
HAL_DIR=hal
1111
USBDEV_DIR=usbdev
1212
#USBHOST_DIR=usbhost
13-
#FATFS_DIR=fatfs
13+
FATFS_DIR=fatfs
1414
#CC3K_DIR=cc3k
1515
DFU=../tools/dfu.py
1616

@@ -23,7 +23,7 @@ INC += -I$(CMSIS_DIR)/devinc
2323
INC += -I$(HAL_DIR)/inc
2424
INC += -I$(USBDEV_DIR)/core/inc -I$(USBDEV_DIR)/class/cdc/inc
2525
#INC += -I$(USBHOST_DIR)
26-
#INC += -I$(FATFS_DIR)
26+
INC += -I$(FATFS_DIR)/src
2727
#INC += -I$(CC3K_DIR)
2828

2929
CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mabi=aapcs-linux -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion
@@ -80,7 +80,9 @@ SRC_C = \
8080
rtc.c \
8181
flash.c \
8282
storage.c \
83+
file.c \
8384
sdcard.c \
85+
diskio.c \
8486

8587
# lcd.c \
8688
# servo.c \
@@ -89,7 +91,6 @@ SRC_C = \
8991
# audio.c \
9092
# i2c.c \
9193
# adc.c \
92-
# file.c \
9394
# pybwlan.c \
9495
9596
SRC_S = \
@@ -137,10 +138,9 @@ SRC_USBDEV = $(addprefix $(USBDEV_DIR)/,\
137138
usbd_storage_msd.c \
138139
)
139140

140-
SRC_FATFS = $(addprefix $(FATFS_DIR)/,\
141+
SRC_FATFS = $(addprefix $(FATFS_DIR)/src/,\
141142
ff.c \
142-
diskio.c \
143-
ccsbcs.c \
143+
option/ccsbcs.c \
144144
)
145145

146146
SRC_CC3K = $(addprefix $(CC3K_DIR)/,\
@@ -162,9 +162,7 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
162162
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
163163
OBJ += $(addprefix $(BUILD)/, $(SRC_HAL:.c=.o))
164164
OBJ += $(addprefix $(BUILD)/, $(SRC_USBDEV:.c=.o))
165-
#OBJ += $(addprefix $(BUILD)/, $(SRC_STMUSBD:.c=.o))
166-
#OBJ += $(addprefix $(BUILD)/, $(SRC_STMUSBH:.c=.o))
167-
#OBJ += $(addprefix $(BUILD)/, $(SRC_FATFS:.c=.o))
165+
OBJ += $(addprefix $(BUILD)/, $(SRC_FATFS:.c=.o))
168166
#OBJ += $(addprefix $(BUILD)/, $(SRC_CC3K:.c=.o))
169167
OBJ += $(BUILD)/pins_$(BOARD).o
170168

stmhal/diskio.c

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/*-----------------------------------------------------------------------*/
2+
/* Low level disk I/O module skeleton for FatFs (C)ChaN, 2013 */
3+
/*-----------------------------------------------------------------------*/
4+
/* If a working storage control module is available, it should be */
5+
/* attached to the FatFs via a glue function rather than modifying it. */
6+
/* This is an example of glue functions to attach various exsisting */
7+
/* storage control module to the FatFs module with a defined API. */
8+
/*-----------------------------------------------------------------------*/
9+
10+
#include <stdint.h>
11+
#include <stdio.h>
12+
#include "ff.h" /* FatFs lower layer API */
13+
#include "diskio.h" /* FatFs lower layer API */
14+
#include "misc.h"
15+
#include "storage.h"
16+
#include "sdcard.h"
17+
18+
PARTITION VolToPart[] = {
19+
{0, 1}, // Logical drive 0 ==> Physical drive 0, 1st partition
20+
{1, 0}, // Logical drive 1 ==> Physical drive 1 (auto detection)
21+
/*
22+
{0, 2}, // Logical drive 2 ==> Physical drive 0, 2nd partition
23+
{0, 3}, // Logical drive 3 ==> Physical drive 0, 3rd partition
24+
*/
25+
};
26+
27+
/* Definitions of physical drive number for each media */
28+
#define PD_FLASH (0)
29+
#define PD_SDCARD (1)
30+
31+
/*-----------------------------------------------------------------------*/
32+
/* Initialize a Drive */
33+
/*-----------------------------------------------------------------------*/
34+
35+
DSTATUS disk_initialize (
36+
BYTE pdrv /* Physical drive nmuber (0..) */
37+
)
38+
{
39+
switch (pdrv) {
40+
case PD_FLASH:
41+
storage_init();
42+
return 0;
43+
44+
case PD_SDCARD:
45+
if (!sdcard_power_on()) {
46+
return STA_NODISK;
47+
}
48+
// TODO return STA_PROTECT if SD card is read only
49+
return 0;
50+
}
51+
52+
return STA_NOINIT;
53+
}
54+
55+
/*-----------------------------------------------------------------------*/
56+
/* Get Disk Status */
57+
/*-----------------------------------------------------------------------*/
58+
59+
DSTATUS disk_status (
60+
BYTE pdrv /* Physical drive nmuber (0..) */
61+
)
62+
{
63+
switch (pdrv) {
64+
case PD_FLASH :
65+
// flash is ready
66+
return 0;
67+
68+
case PD_SDCARD:
69+
// TODO return STA_PROTECT if SD card is read only
70+
return 0;
71+
}
72+
73+
return STA_NOINIT;
74+
}
75+
76+
/*-----------------------------------------------------------------------*/
77+
/* Read Sector(s) */
78+
/*-----------------------------------------------------------------------*/
79+
80+
DRESULT disk_read (
81+
BYTE pdrv, /* Physical drive nmuber (0..) */
82+
BYTE *buff, /* Data buffer to store read data */
83+
DWORD sector, /* Sector address (LBA) */
84+
UINT count /* Number of sectors to read (1..128) */
85+
)
86+
{
87+
switch (pdrv) {
88+
case PD_FLASH:
89+
for (int i = 0; i < count; i++) {
90+
if (!storage_read_block(buff + i * FLASH_BLOCK_SIZE, sector + i)) {
91+
return RES_ERROR;
92+
}
93+
}
94+
return RES_OK;
95+
96+
case PD_SDCARD:
97+
if (!sdcard_read_blocks(buff, sector, count)) {
98+
return RES_ERROR;
99+
}
100+
return RES_OK;
101+
}
102+
103+
return RES_PARERR;
104+
}
105+
106+
/*-----------------------------------------------------------------------*/
107+
/* Write Sector(s) */
108+
/*-----------------------------------------------------------------------*/
109+
110+
#if _USE_WRITE
111+
DRESULT disk_write (
112+
BYTE pdrv, /* Physical drive nmuber (0..) */
113+
const BYTE *buff, /* Data to be written */
114+
DWORD sector, /* Sector address (LBA) */
115+
UINT count /* Number of sectors to write (1..128) */
116+
)
117+
{
118+
switch (pdrv) {
119+
case PD_FLASH:
120+
for (int i = 0; i < count; i++) {
121+
if (!storage_write_block(buff + i * FLASH_BLOCK_SIZE, sector + i)) {
122+
return RES_ERROR;
123+
}
124+
}
125+
return RES_OK;
126+
127+
case PD_SDCARD:
128+
if (!sdcard_write_blocks(buff, sector, count)) {
129+
return RES_ERROR;
130+
}
131+
return RES_OK;
132+
}
133+
134+
return RES_PARERR;
135+
}
136+
#endif
137+
138+
139+
/*-----------------------------------------------------------------------*/
140+
/* Miscellaneous Functions */
141+
/*-----------------------------------------------------------------------*/
142+
143+
#if _USE_IOCTL
144+
DRESULT disk_ioctl (
145+
BYTE pdrv, /* Physical drive nmuber (0..) */
146+
BYTE cmd, /* Control code */
147+
void *buff /* Buffer to send/receive control data */
148+
)
149+
{
150+
switch (pdrv) {
151+
case PD_FLASH:
152+
switch (cmd) {
153+
case CTRL_SYNC:
154+
storage_flush();
155+
return RES_OK;
156+
157+
case GET_BLOCK_SIZE:
158+
*((DWORD*)buff) = 1; // high-level sector erase size in units of the small (512) block size
159+
return RES_OK;
160+
}
161+
break;
162+
163+
case PD_SDCARD:
164+
switch (cmd) {
165+
case CTRL_SYNC:
166+
return RES_OK;
167+
168+
case GET_BLOCK_SIZE:
169+
*((DWORD*)buff) = 1; // high-level sector erase size in units of the small (512) block size
170+
return RES_OK;
171+
}
172+
break;
173+
}
174+
175+
return RES_PARERR;
176+
}
177+
#endif
178+
179+
DWORD get_fattime (
180+
void
181+
)
182+
{
183+
// TODO replace with call to RTC
184+
int year = 2013;
185+
int month = 10;
186+
int day = 12;
187+
int hour = 21;
188+
int minute = 42;
189+
int second = 13;
190+
return ((year - 1980) << 25) | ((month) << 21) | ((day) << 16) | ((hour) << 11) | ((minute) << 5) | (second / 2);
191+
}

stmhal/diskio.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*-----------------------------------------------------------------------
2+
/ Low level disk interface modlue include file (C)ChaN, 2013
3+
/-----------------------------------------------------------------------*/
4+
5+
#ifndef _DISKIO_DEFINED
6+
#define _DISKIO_DEFINED
7+
8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif
11+
12+
#define _USE_WRITE 1 /* 1: Enable disk_write function */
13+
#define _USE_IOCTL 1 /* 1: Enable disk_ioctl fucntion */
14+
15+
#include "integer.h"
16+
17+
18+
/* Status of Disk Functions */
19+
typedef BYTE DSTATUS;
20+
21+
/* Results of Disk Functions */
22+
typedef enum {
23+
RES_OK = 0, /* 0: Successful */
24+
RES_ERROR, /* 1: R/W Error */
25+
RES_WRPRT, /* 2: Write Protected */
26+
RES_NOTRDY, /* 3: Not Ready */
27+
RES_PARERR /* 4: Invalid Parameter */
28+
} DRESULT;
29+
30+
31+
/*---------------------------------------*/
32+
/* Prototypes for disk control functions */
33+
34+
35+
DSTATUS disk_initialize (BYTE pdrv);
36+
DSTATUS disk_status (BYTE pdrv);
37+
DRESULT disk_read (BYTE pdrv, BYTE*buff, DWORD sector, UINT count);
38+
DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count);
39+
DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);
40+
41+
DWORD get_fattime (void);
42+
43+
/* Disk Status Bits (DSTATUS) */
44+
#define STA_NOINIT 0x01 /* Drive not initialized */
45+
#define STA_NODISK 0x02 /* No medium in the drive */
46+
#define STA_PROTECT 0x04 /* Write protected */
47+
48+
49+
/* Command code for disk_ioctrl fucntion */
50+
51+
/* Generic command (used by FatFs) */
52+
#define CTRL_SYNC 0 /* Flush disk cache (for write functions) */
53+
#define GET_SECTOR_COUNT 1 /* Get media size (for only f_mkfs()) */
54+
#define GET_SECTOR_SIZE 2 /* Get sector size (for multiple sector size (_MAX_SS >= 1024)) */
55+
#define GET_BLOCK_SIZE 3 /* Get erase block size (for only f_mkfs()) */
56+
#define CTRL_ERASE_SECTOR 4 /* Force erased a block of sectors (for only _USE_ERASE) */
57+
58+
/* Generic command (not used by FatFs) */
59+
#define CTRL_POWER 5 /* Get/Set power status */
60+
#define CTRL_LOCK 6 /* Lock/Unlock media removal */
61+
#define CTRL_EJECT 7 /* Eject media */
62+
#define CTRL_FORMAT 8 /* Create physical format on the media */
63+
64+
/* MMC/SDC specific ioctl command */
65+
#define MMC_GET_TYPE 10 /* Get card type */
66+
#define MMC_GET_CSD 11 /* Get CSD */
67+
#define MMC_GET_CID 12 /* Get CID */
68+
#define MMC_GET_OCR 13 /* Get OCR */
69+
#define MMC_GET_SDSTAT 14 /* Get SD status */
70+
71+
/* ATA/CF specific ioctl command */
72+
#define ATA_GET_REV 20 /* Get F/W revision */
73+
#define ATA_GET_MODEL 21 /* Get model name */
74+
#define ATA_GET_SN 22 /* Get serial number */
75+
76+
77+
/* MMC card type flags (MMC_GET_TYPE) */
78+
#define CT_MMC 0x01 /* MMC ver 3 */
79+
#define CT_SD1 0x02 /* SD ver 1 */
80+
#define CT_SD2 0x04 /* SD ver 2 */
81+
#define CT_SDC (CT_SD1|CT_SD2) /* SD */
82+
#define CT_BLOCK 0x08 /* Block addressing */
83+
84+
85+
#ifdef __cplusplus
86+
}
87+
#endif
88+
89+
#endif

0 commit comments

Comments
 (0)