Skip to content
This repository was archived by the owner on Feb 21, 2020. It is now read-only.

Commit 2008da6

Browse files
fpistmalfran
authored andcommitted
SD: Split SD Class
Signed-off-by: Frederic.Pillon <frederic.pillon@st.com>
1 parent 3bc6ce3 commit 2008da6

File tree

6 files changed

+220
-61
lines changed

6 files changed

+220
-61
lines changed

libraries/SD/src/SD.cpp

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,12 @@ extern "C" {
5454
#include <stdlib.h>
5555
#include <string.h>
5656
#include <inttypes.h>
57-
#include "stm32f4xx_hal.h"
57+
#include "stm32f4xx_hal.h"
5858
}
5959

6060
#include "SD.h"
6161
SDClass SD;
6262

63-
FATFS SDFatFs; /* File system object for SD disk logical drive */
64-
char SDPath[4]; /* SD disk logical drive path */
65-
6663
/**
6764
* @brief Link SD, register the file system object to the FatFs mode and configure
6865
* relatives SD IOs except SD Detect Pin
@@ -71,32 +68,14 @@ char SDPath[4]; /* SD disk logical drive path */
7168
*/
7269
uint8_t SDClass::begin()
7370
{
74-
/*##-1- Link the SD disk I/O driver ########################################*/
75-
if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0)
76-
{
77-
/*##-2- Register the file system object to the FatFs module ##############*/
78-
if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) != FR_OK)
79-
{
80-
/* FatFs Initialization Error */
81-
return FALSE;
71+
/*##-1- Initializes SD IOs #############################################*/
72+
if (_card.init()) {
73+
return _fatFs.init();
8274
}
8375
else
8476
{
85-
/*##-3- Initializes SD IOs #############################################*/
86-
if(BSP_SD_Init() != MSD_OK)
87-
{
88-
return FALSE;
89-
}
90-
else
91-
{
92-
return TRUE;
93-
}
77+
return FALSE;
9478
}
95-
}
96-
else
97-
{
98-
return FALSE;
99-
}
10079
}
10180

10281
/**
@@ -107,32 +86,14 @@ uint8_t SDClass::begin()
10786
*/
10887
uint8_t SDClass::begin(uint8_t cspin)
10988
{
110-
/*##-1- Link the SD disk I/O driver ########################################*/
111-
if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0)
112-
{
113-
/*##-2- Register the file system object to the FatFs module ##############*/
114-
if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) != FR_OK)
115-
{
116-
/* FatFs Initialization Error */
117-
return FALSE;
89+
/*##-1- Initializes SD IOs #############################################*/
90+
if (_card.init(cspin)) {
91+
return _fatFs.init();
11892
}
11993
else
12094
{
121-
/*##-3- Initializes SD IOs #############################################*/
122-
if(BSP_SD_CSInit() != MSD_OK)
123-
{
124-
return FALSE;
125-
}
126-
else
127-
{
128-
return TRUE;
129-
}
95+
return FALSE;
13096
}
131-
}
132-
else
133-
{
134-
return FALSE;
135-
}
13697
}
13798

13899
/**

libraries/SD/src/SD.h

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,9 @@
1616
#define __SD_H__
1717

1818
#include <Arduino.h>
19-
/* otto includes component */
20-
#include "BSP/OTTO/otto_sd.h"
2119

22-
/* FatFs includes component */
23-
#include "FatFs/src/ff_gen_drv.h"
24-
#include "FatFs/src/drivers/sd_diskio.h"
25-
26-
#define FALSE ((uint8_t)0x00)
27-
#define TRUE ((uint8_t)0x01)
28-
29-
#define FILE_WRITE FA_WRITE
30-
#define FILE_READ FA_READ
20+
#include "Sd2Card.h"
21+
#include "SdFatFs.h"
3122

3223
class File {
3324
public:
@@ -63,8 +54,8 @@ class SDClass {
6354
public:
6455

6556
/* Initialize the SD peripheral */
66-
static uint8_t begin();
67-
static uint8_t begin(uint8_t cspin);
57+
uint8_t begin();
58+
uint8_t begin(uint8_t cspin);
6859
static File open(const char *filepath, uint8_t mode);
6960
static File open(const char *filepath);
7061
static uint8_t exists(const char *filepath);
@@ -74,6 +65,10 @@ class SDClass {
7465

7566
friend class File;
7667

68+
private:
69+
Sd2Card _card;
70+
SdFatFs _fatFs;
71+
7772
};
7873

7974
extern SDClass SD;

libraries/SD/src/Sd2Card.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* <Description>
3+
*
4+
* Copyright (C) 2016, STMicroelectronics - All Rights Reserved
5+
* Author: YOUR NAME <> for STMicroelectronics.
6+
*
7+
* License type: GPLv2
8+
*
9+
* This program is free software; you can redistribute it and/or modify it
10+
* under the terms of the GNU General Public License version 2 as published by
11+
* the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful, but
14+
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE.
16+
* See the GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License along with
19+
* this program. If not, see
20+
* <http://www.gnu.org/licenses/>.
21+
*/
22+
23+
#include <Arduino.h>
24+
#include "Sd2Card.h"
25+
26+
uint8_t Sd2Card::init(void) {
27+
if (BSP_SD_Init() == MSD_OK) {
28+
BSP_SD_GetCardInfo(&_SdCardInfo);
29+
return TRUE;
30+
} else {
31+
return FALSE;
32+
}
33+
}
34+
35+
uint8_t Sd2Card::init(uint8_t cspin) {
36+
if (BSP_SD_CSInit() == MSD_OK) {
37+
BSP_SD_GetCardInfo(&_SdCardInfo);
38+
return TRUE;
39+
} else {
40+
return FALSE;
41+
}
42+
}
43+
44+

libraries/SD/src/Sd2Card.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* <Description>
3+
*
4+
* Copyright (C) 2016, STMicroelectronics - All Rights Reserved
5+
* Author: YOUR NAME <> for STMicroelectronics.
6+
*
7+
* License type: GPLv2
8+
*
9+
* This program is free software; you can redistribute it and/or modify it
10+
* under the terms of the GNU General Public License version 2 as published by
11+
* the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful, but
14+
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE.
16+
* See the GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License along with
19+
* this program. If not, see
20+
* <http://www.gnu.org/licenses/>.
21+
*/
22+
#ifndef Sd2Card_h
23+
#define Sd2Card_h
24+
25+
/* otto includes component */
26+
#include "BSP/OTTO/otto_sd.h"
27+
28+
#define FALSE ((uint8_t)0x00)
29+
#define TRUE ((uint8_t)0x01)
30+
31+
// card types to match Arduino definition
32+
/** Standard capacity V1 SD card */
33+
#define SD_CARD_TYPE_SD1 STD_CAPACITY_SD_CARD_V1_1
34+
/** Standard capacity V2 SD card */
35+
#define SD_CARD_TYPE_SD2 STD_CAPACITY_SD_CARD_V2_0
36+
/** High Capacity SD card */
37+
#define SD_CARD_TYPE_SDHC HIGH_CAPACITY_SD_CARD
38+
39+
class Sd2Card {
40+
public:
41+
42+
uint8_t init(void);
43+
uint8_t init(uint8_t cspin);
44+
45+
/** Return the card type: SD V1, SD V2 or SDHC */
46+
uint8_t type(void) const {return _SdCardInfo.CardType;}
47+
48+
private:
49+
SD_CardInfo _SdCardInfo;
50+
51+
};
52+
#endif // sd2Card_h

libraries/SD/src/SdFatFs.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* <Description>
3+
*
4+
* Copyright (C) 2016, STMicroelectronics - All Rights Reserved
5+
* Author: YOUR NAME <> for STMicroelectronics.
6+
*
7+
* License type: GPLv2
8+
*
9+
* This program is free software; you can redistribute it and/or modify it
10+
* under the terms of the GNU General Public License version 2 as published by
11+
* the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful, but
14+
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE.
16+
* See the GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License along with
19+
* this program. If not, see
20+
* <http://www.gnu.org/licenses/>.
21+
*/
22+
23+
#include <Arduino.h>
24+
#include "SdFatFs.h"
25+
26+
uint8_t SdFatFs::init(void) {
27+
28+
/*##-1- Link the SD disk I/O driver ########################################*/
29+
if(FATFS_LinkDriver(&SD_Driver, _SDPath) == 0)
30+
{
31+
/*##-2- Register the file system object to the FatFs module ##############*/
32+
if(f_mount(&_SDFatFs, (TCHAR const*)_SDPath, 1) == FR_OK)
33+
{
34+
/* FatFs Initialization done */
35+
return TRUE;
36+
}
37+
}
38+
return FALSE;
39+
}
40+
41+
uint8_t SdFatFs::fatType(void)
42+
{
43+
switch (_SDFatFs.fs_type)
44+
{
45+
case FS_FAT12:
46+
return 12;
47+
case FS_FAT16:
48+
return 16;
49+
case FS_FAT32:
50+
return 32;
51+
default:
52+
return 0;
53+
}
54+
}

libraries/SD/src/SdFatFs.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* <Description>
3+
*
4+
* Copyright (C) 2016, STMicroelectronics - All Rights Reserved
5+
* Author: YOUR NAME <> for STMicroelectronics.
6+
*
7+
* License type: GPLv2
8+
*
9+
* This program is free software; you can redistribute it and/or modify it
10+
* under the terms of the GNU General Public License version 2 as published by
11+
* the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful, but
14+
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE.
16+
* See the GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License along with
19+
* this program. If not, see
20+
* <http://www.gnu.org/licenses/>.
21+
*/
22+
#ifndef SdFatFs_h
23+
#define SdFatFs_h
24+
25+
#include "Sd2Card.h"
26+
27+
/* FatFs includes component */
28+
#include "FatFs/src/ff_gen_drv.h"
29+
#include "FatFs/src/drivers/sd_diskio.h"
30+
31+
// To match Arduino definition
32+
#define FILE_WRITE FA_WRITE
33+
#define FILE_READ FA_READ
34+
35+
class SdFatFs {
36+
public:
37+
38+
uint8_t init(void);
39+
40+
/** Return the FatFs type: 12, 16, 32 (0: unknown)*/
41+
uint8_t fatType(void);
42+
43+
// inline functions that return volume info
44+
/** \return The volume's cluster size in blocks. */
45+
uint8_t blocksPerCluster(void) const {return _SDFatFs.csize;}
46+
/** \return The total number of clusters in the volume. */
47+
uint32_t clusterCount(void) const {return (_SDFatFs.n_fatent -2);}
48+
49+
private:
50+
FATFS _SDFatFs; /* File system object for SD disk logical drive */
51+
char _SDPath[4]; /* SD disk logical drive path */
52+
};
53+
#endif // sdFatFs_h

0 commit comments

Comments
 (0)