Skip to content

Commit ec5b34e

Browse files
committed
[fatfs] Add "DataLogger" example and "list_sd()" command
1 parent e8ee66b commit ec5b34e

File tree

8 files changed

+73
-13
lines changed

8 files changed

+73
-13
lines changed

examples/Blink/Blink.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <rtt.h>
22

3-
// NOTES: "CONFIG_USING_FINSH" and "CONFIG_USING_SPISD" in "rtt.h" may be turned off to save memory
3+
// NOTES: "CONFIG_USING_FINSH" and "CONFIG_USING_SPISD" in "rtconfig.h" may be turned off to save memory
44

55
struct rt_thread blink_thread;
66
byte blink_thread_stack[1024];

examples/DataLogger/DataLogger.ino

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <rtt.h>
2+
3+
// RT-Thread function called by "RT_T.begin()"
4+
void rt_setup(void) {
5+
}
6+
7+
void setup() {
8+
RT_T.begin();
9+
}
10+
11+
// this function will be called by "Arduino" thread
12+
void loop() {
13+
char buf[15];
14+
int sensorVal[3];
15+
int dataFile = -1;
16+
const unsigned int maxCount = 10;
17+
static unsigned int count = 0;
18+
19+
// read sensors once per second for 10 times
20+
if (count < maxCount) {
21+
count++;
22+
23+
// read three sensors and convert the result to string:
24+
for (int analogPin = 0; analogPin < 3; analogPin++) {
25+
sensorVal[analogPin] = analogRead(analogPin);
26+
}
27+
rt_sprintf(buf, "%03d,%03d,%03d\n", sensorVal[0], sensorVal[1], sensorVal[2]);
28+
29+
// open log file.
30+
dataFile = open("datalog.txt", O_CREAT | O_RDWR | O_ACCMODE | O_APPEND);
31+
32+
// if file opened successfully, then write to it:
33+
if (dataFile >= 0) {
34+
write(dataFile, buf, rt_strlen(buf));
35+
if (maxCount == count) {
36+
// flush data to SD:
37+
fsync(dataFile);
38+
}
39+
close(dataFile);
40+
// print to the serial port too:
41+
rt_kprintf(buf);
42+
} else {
43+
// if the file isn't open, pop up an error:
44+
rt_kprintf("*** error opening \"datalog.txt\" ***\n");
45+
}
46+
}
47+
48+
rt_thread_sleep(RT_TICK_PER_SECOND);
49+
}

examples/FinSH/FinSH.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
// state=0
1616
// led(id, state)
1717

18+
// NOTES: "CONFIG_USING_SPISD" in "rtconfig.h" may be turned off to save memory
19+
1820
extern "C" {
1921

2022
rt_uint32_t led_id = 0;

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=RT-Thread
2-
version=0.4.1
2+
version=0.4.2
33
author=Bernard Xiong <[email protected]>, onelife <[email protected]>
44
maintainer=onelife <[email protected]>
55
sentence=Real Time Operating System porting for Arduino SAM and SAMD boards

src/components/arduino/drv_spisd.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -853,8 +853,8 @@ static rt_err_t bsp_spiSd_contex_init(struct bsp_sd_contex *ctx,
853853
ctx->dev.write = bsp_spiSd_write;
854854
ctx->dev.control = bsp_spiSd_control;
855855
ctx->dev.user_data = (void *)ctx;
856-
ret = rt_device_register(&ctx->dev, name, RT_DEVICE_FLAG_RDWR | \
857-
RT_DEVICE_FLAG_REMOVABLE);
856+
ret = rt_device_register(&ctx->dev, name,
857+
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE);
858858
} while (0);
859859

860860
return ret;
@@ -899,8 +899,7 @@ rt_err_t bsp_hw_spiSd_init(void) {
899899
/*******************************************************************************
900900
* Export to FINSH
901901
******************************************************************************/
902-
#if 0 //#ifdef RT_USING_FINSH
903-
#include "components/finsh/finsh.h"
902+
#ifdef RT_USING_FINSH
904903

905904
rt_err_t list_sd(void) {
906905
struct bsp_sd_contex *ctx = SD_CTX();
@@ -914,16 +913,23 @@ rt_err_t list_sd(void) {
914913
rt_kprintf("Error: open failed!\n");
915914
return ret;
916915
}
916+
if (RT_EOK != (ret = rt_device_open(ctx->ldev, RT_DEVICE_OFLAG_RDWR))) {
917+
rt_kprintf("Error: open ldev failed!\n");
918+
rt_device_close(&ctx->dev);
919+
return ret;
920+
}
917921
SD_CS(1);
918922

919923
/* Receive CID as a data block (16 bytes) */
920924
if (sd_send_cmd(ctx, CMD10, 0x00000000, buf_res)) {
921925
SD_CS(0);
926+
rt_device_close(ctx->ldev);
922927
rt_device_close(&ctx->dev);
923928
rt_kprintf("Error: Get CID failed!\n");
924929
return -RT_ERROR;
925930
}
926931
SD_CS(0);
932+
rt_device_close(ctx->ldev);
927933

928934
rt_kprintf(" SD Card on %s\n", ctx->ldev->parent.name);
929935
rt_kprintf(" ------------------------------\n");
@@ -964,7 +970,7 @@ rt_err_t list_sd(void) {
964970

965971
return RT_EOK;
966972
}
967-
FINSH_FUNCTION_EXPORT(list_sd, list the SD card.)
973+
FINSH_FUNCTION_EXPORT(list_sd, show SD information.)
968974
#endif /* RT_USING_FINSH */
969975

970976
/***************************************************************************//**

src/rtconfig.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/***************************************************************************//**
2-
* @file rtt.h
2+
* @file rtconfig.h
33
* @brief Arduino RT-Thread library config
44
* @author onelife <onelife.real[at]gmail.com>
55
******************************************************************************/
6-
#ifndef __RTTHREAD_CFG_H__
7-
#define __RTTHREAD_CFG_H__
6+
#ifndef __RTCONFIG_H__
7+
#define __RTCONFIG_H__
88

99
/* Porting Options */
1010
#define CONFIG_ARDUINO
@@ -151,4 +151,4 @@
151151
/* Other Options */
152152
// #define RT_USING_COMPONENTS_INIT /* Not supported as no access to linker script */
153153

154-
#endif /* __RTTHREAD_CFG_H__ */
154+
#endif /* __RTCONFIG_H__ */

src/rtt.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#define __RTT_H__
88

99
#include "include/rtthread.h"
10+
#ifdef RT_USING_DFS
11+
#include "components/dfs/include/dfs_posix.h"
12+
#endif
1013

1114

1215
class RT_Thread {

src/shell_cmd.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ ADD_SHELL_CMD(cat, print file content, cat, void, const char *filename)
4646
ADD_SHELL_CMD(copy, copy file or dir, copy, void, const char *src, const char *dst)
4747
#endif
4848
#if CONFIG_USING_SPISD
49-
// ADD_SHELL_CMD(list_sd, show SD information, list_sd, void, void)
49+
ADD_SHELL_CMD(list_sd, show SD information, list_sd, rt_err_t, void)
5050
#endif
5151

5252
/* Please add your commands with the following format:
@@ -64,7 +64,7 @@ ADD_SHELL_CMD(copy, copy file or dir, copy, void, const char *src, const char *d
6464
Example 2: void led_on(void); =>
6565
ADD_SHELL_CMD(led0_on, Turn on builtin LED, led_on, void, void)
6666
67-
Example 3: long led_set(rt_uint32_t id, rt_int32_t val); =>
67+
Example 3: rt_uint32_t led_set(rt_uint32_t id, rt_int32_t val); =>
6868
ADD_SHELL_CMD(led, Turn on/off any LED, led_set, rt_uint32_t, rt_uint32_t id, rt_uint8_t state)
6969
*/
7070
// ADD_SHELL_CMD(led, Turn on/off builtin LED, led, rt_uint32_t, rt_uint32_t id, rt_uint8_t state)

0 commit comments

Comments
 (0)