Skip to content

Commit e1553fa

Browse files
committed
Add SD Example
1 parent 562234b commit e1553fa

File tree

7 files changed

+99
-15
lines changed

7 files changed

+99
-15
lines changed

examples/Basics/SDCard/SDCard.ino

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
*******************************************************************************
3+
* Copyright (c) 2023 by M5Stack
4+
* Equipped with M5CoreS3 sample source code
5+
* 配套 M5CoreS3 示例源代码
6+
* Visit for more information: https://docs.m5stack.com/en/core/CoreS3
7+
* 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/CoreS3
8+
*
9+
* Describe: TF Card. TF卡
10+
* Date: 2023/8/24
11+
*******************************************************************************
12+
In this example, we will detect the existence of a file and perform read and
13+
write operations on it
14+
在这个示例中,我们将会检测某文件是否存在,并进行读写文件操作
15+
*/
16+
17+
#include <M5CoreS3.h>
18+
19+
void setup() {
20+
M5.begin(true, true, true);
21+
M5.Lcd.setTextSize(2);
22+
M5.Lcd.setCursor(60, 0);
23+
M5.Lcd.print("SD Card Example");
24+
M5.Lcd.setCursor(0, 20);
25+
M5.Lcd.print("Pls open the serial port.");
26+
M5.Lcd.endWrite();
27+
28+
SPI.begin(SPI_SCK_PIN, SPI_MISO_PIN, SPI_MOSI_PIN, TFCARD_CS_PIN);
29+
if (M5.Axp.isSDCardExist()) {
30+
// Initialize the SD card. 初始化SD卡.
31+
if (!SD.begin(GPIO_NUM_4, SPI, 25000000)) {
32+
// Print a message if the SD card initialization
33+
// fails orif the SD card does not exist.
34+
// 如果SD卡初始化失败或者SD卡不存在,则打印消息.
35+
USBSerial.println("Card failed, or not present");
36+
while (1)
37+
;
38+
}
39+
} else {
40+
USBSerial.println("Please insert SD card");
41+
}
42+
43+
USBSerial.println("TF card initialized.");
44+
// Check if the "/hello.txt" file exists.
45+
// 查看是否存在"/hello.txt"文件
46+
if (SD.exists("/hello.txt")) {
47+
USBSerial.println("hello.txt exists.");
48+
} else {
49+
USBSerial.println("hello.txt doesn't exist.");
50+
}
51+
USBSerial.println("Creating hello.txt");
52+
File myFile = SD.open("/hello.txt",
53+
FILE_WRITE); // Create a new file "/hello.txt".
54+
// 创建一个新文件"/hello.txt"
55+
if (myFile) { // If the file is open, then write to it.
56+
// 如果文件打开,则进行写入操作
57+
USBSerial.println("Writing to test.txt...");
58+
myFile.println("SD test.");
59+
myFile.close(); // Close the file. 关闭文件
60+
USBSerial.println("done.");
61+
} else {
62+
USBSerial.println("error opening test.txt");
63+
}
64+
delay(500);
65+
// Open the file "/hello.txt" in read mode.
66+
// 以读取模式打开文件"/hello.txt"
67+
myFile = SD.open("/hello.txt", FILE_READ);
68+
if (myFile) {
69+
USBSerial.println("/hello.txt Content:");
70+
// Read the data from the file and print it until the reading is
71+
// complete. 从文件里读取数据并打印到串口,直到读取完成.
72+
while (myFile.available()) {
73+
USBSerial.write(myFile.read());
74+
}
75+
myFile.close();
76+
} else {
77+
USBSerial.println(
78+
"error opening /hello.txt"); // If the file is not open.
79+
// 如果文件没有打开
80+
}
81+
}
82+
void loop() {
83+
}

library.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
"description": "An ESP32 Arduino board",
44
"keywords": "M5CoreS3",
55
"authors": {
6-
"name": "M5Stack",
6+
"name": "Tinyu-Zhao,M5Stack",
77
"url": "http://www.m5stack.com"
88
},
99
"repository": {
1010
"type": "git",
1111
"url": "https://github.com/m5stack/M5CoreS3.git"
1212
},
13-
"version": "0.0.3",
13+
"version": "0.0.4",
1414
"frameworks": "arduino",
1515
"platforms": "espressif32",
1616
"headers": "M5CoreS3.h"

library.properties

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name=M5CoreS3
2-
version=0.0.3
3-
author=M5Stack
4-
maintainer=M5Stack
2+
version=0.0.4
3+
author=Tinyu-Zhao,M5Stack
4+
maintainer=Tinyu-Zhao,M5Stack
55
sentence=Library for M5CoreS3 Core development kit
66
paragraph=See more on http://M5Stack.com
77
category=Device Control

src/AXP2101.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,7 @@ void AXP2101::coreS3_VBUS_boost(bool state) {
383383
write1Byte(0xF0, 0x00);
384384
}
385385
}
386+
387+
bool AXP2101::isSDCardExist() {
388+
return (bool)!((read8Bit(AW9523_ADDR, 0x00) >> 4) & 0x01);
389+
}

src/AXP2101.h

+2-8
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,8 @@ class AXP2101 : public I2C_PORT {
9393
/* Temperature data */
9494
float getAXP173Temp();
9595
float getTSTemp();
96-
// -- sleep
97-
void PrepareToSleep(void);
98-
void RestoreFromLightSleep(void);
99-
void DeepSleep(uint64_t time_in_us = 0);
100-
void LightSleep(uint64_t time_in_us = 0);
10196

102-
void setBacklight(bool state);
103-
104-
void coreS3_init(); ////
97+
void coreS3_init();
10598
void coreS3_AW9523_init();
10699

107100
void setBoostEn(bool state);
@@ -110,6 +103,7 @@ class AXP2101 : public I2C_PORT {
110103
void setUsbOtgEn(bool state);
111104

112105
void coreS3_VBUS_boost(bool state);
106+
bool isSDCardExist();
113107
};
114108

115109
#endif

src/M5CoreS3.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
#include <Arduino.h>
55
#include <Wire.h>
6-
// #include "FS.h"
7-
// #include "SD.h"
6+
#include "FS.h"
7+
#include "SD.h"
88

99
// #include <FastLED.h>
1010
#include "M5Display.h"

src/utility/Config.h

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
// SD card
1616
#define TFCARD_CS_PIN 4
17+
#define SPI_MOSI_PIN 37
18+
#define SPI_MISO_PIN 35
19+
#define SPI_SCK_PIN 36
1720

1821
// UART
1922
#define USE_SERIAL USBSerial

0 commit comments

Comments
 (0)