Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a417fc1

Browse files
committed
[sync] Add back RTT SPI SD card driver
- All the examples except `HelloMo` are working on Sipeed Longan Nano
1 parent efc3d0a commit a417fc1

File tree

11 files changed

+1849
-49
lines changed

11 files changed

+1849
-49
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ I hope this project will release the power of multitasking on Arduino platform.
6262
| --- | --- | --- |
6363
| SAM | ARM Cortex-M3 | Tested with Arduino Due |
6464
| SAMD | ARM Cortex-M0+ | Tested with Arduino MKRZero |
65-
| GD32V | Bumblebee (RV32IMAC) | Tested with Longan Nano |
65+
| GD32V | Bumblebee (RV32IMAC) | Tested with Longan Nano ([GD32Vduino](https://github.com/onelife/gd32vduino)) |
6666
| STM32 | ARM Cortex-M7 | Tested with Nucleo-F767ZI |
6767

6868

‎examples/RttMutex/RttMutex.ino‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

1313
#define THREAD_NUM 3
14-
#define STACK_SIZE 256
14+
#define STACK_SIZE 384
1515

1616
struct rt_mutex lock;
1717

@@ -44,7 +44,7 @@ void rt_setup(void) {
4444

4545
// dynamically initialize threads
4646
for (i = 0; i < THREAD_NUM; i++) {
47-
name[7] = '1' + i;
47+
name[7] = '0' + i;
4848
if (RT_NULL == (tid[i] = rt_thread_create(
4949
name, // [in] thread name
5050
thread_entry, // [in] thread entry function

‎src/bsp/LonganNano/drv_spi.c‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,8 @@ enum bsp_spi_channel {
6666
};
6767

6868
/* Private function prototypes -----------------------------------------------*/
69-
static rt_err_t op_configure(struct rt_spi_device* dev,
70-
struct rt_spi_configuration* cfg);
71-
static rt_uint32_t op_xfer(struct rt_spi_device* dev,
72-
struct rt_spi_message* msg);
69+
static rt_err_t op_configure(struct rt_spi_device* dev, struct rt_spi_configuration* cfg);
70+
static rt_uint32_t op_xfer(struct rt_spi_device* dev, struct rt_spi_message* msg);
7371

7472
/* Private constants ---------------------------------------------------------*/
7573

@@ -115,15 +113,14 @@ static struct rt_spi_ops spi_ops = {
115113
};
116114

117115
/* Private functions ---------------------------------------------------------*/
118-
static rt_err_t op_configure(struct rt_spi_device* dev,
119-
struct rt_spi_configuration* cfg) {
116+
static rt_err_t op_configure(struct rt_spi_device* dev, struct rt_spi_configuration* cfg) {
120117
struct bsp_spi_contex *ctx;
121118
spi_parameter_struct init;
122119
rt_uint32_t clk;
123120

124121
RT_ASSERT(dev != RT_NULL);
125122
RT_ASSERT(cfg != RT_NULL);
126-
ctx = (struct bsp_spi_contex *)dev->bus->parent.user_data
123+
ctx = (struct bsp_spi_contex *)dev->bus->parent.user_data;
127124
RT_ASSERT(ctx != RT_NULL);
128125
LOG_D("[SPI%d] op_configure", ctx->chn);
129126

@@ -205,8 +202,7 @@ static rt_err_t op_configure(struct rt_spi_device* dev,
205202
return RT_EOK;
206203
};
207204

208-
static rt_uint32_t op_xfer(struct rt_spi_device* dev,
209-
struct rt_spi_message* msg) {
205+
static rt_uint32_t op_xfer(struct rt_spi_device* dev, struct rt_spi_message* msg) {
210206
struct bsp_spi_contex *ctx;
211207
struct rt_spi_configuration *cfg;
212208
rt_uint8_t mode3 = 0;

‎src/bsp/LonganNano/drv_tick.c‎

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,50 +26,63 @@ static rt_uint64_t overflow = 0;
2626

2727
/* Public functions ----------------------------------------------------------*/
2828
rt_err_t bsp_hw_tick_init(void) {
29-
rt_uint64_t ticks;
29+
rt_uint64_t ticks, compare;
3030

3131
/* get counter value */
32-
ticks = *(rt_uint64_t *)(TIMER_CTRL_ADDR + TIMER_MTIME);
32+
ticks = get_timer_value();
33+
compare = ticks + (TIMER_FREQ / RT_TICK_PER_SECOND);
3334
/* set compare value */
34-
if ((ticks+ (TIMER_FREQ / RT_TICK_PER_SECOND)) < ticks) {
35+
if (compare < ticks) {
3536
/* overflow */
36-
overflow = ticks + (TIMER_FREQ / RT_TICK_PER_SECOND);
37-
*(rt_uint64_t *)(TIMER_CTRL_ADDR + TIMER_MTIMECMP) = (rt_uint64_t)(-1);
37+
overflow = compare;
38+
*(volatile rt_uint32_t *)(TIMER_CTRL_ADDR + TIMER_MTIMECMP + 4) = 0xffffffff;
39+
*(volatile rt_uint32_t *)(TIMER_CTRL_ADDR + TIMER_MTIMECMP) = 0xffffffff;
40+
// *(volatile rt_uint64_t *)(TIMER_CTRL_ADDR + TIMER_MTIMECMP) = (rt_uint64_t)(-1);
3841
} else {
3942
overflow = 0;
40-
*(rt_uint64_t *)(TIMER_CTRL_ADDR + TIMER_MTIMECMP) = \
41-
ticks + (TIMER_FREQ / RT_TICK_PER_SECOND);
43+
*(volatile rt_uint32_t *)(TIMER_CTRL_ADDR + TIMER_MTIMECMP + 4) = (rt_uint32_t)(compare >> 32);
44+
*(volatile rt_uint32_t *)(TIMER_CTRL_ADDR + TIMER_MTIMECMP) = (rt_uint32_t)(compare & 0xffffffff);
45+
// *(volatile rt_uint64_t *)(TIMER_CTRL_ADDR + TIMER_MTIMECMP) = compare;
4246

4347
}
4448
*(rt_uint32_t *)(TIMER_CTRL_ADDR + TIMER_MSIP) = 1;
45-
eclic_irq_enable(CLIC_INT_TMR, 0, 0);
49+
/* set interrupt level and priority */
50+
eclic_set_nlbits(ECLIC_GROUP_LEVEL3_PRIO1);
51+
eclic_irq_enable(CLIC_INT_TMR, 7, 1);
4652

4753
return RT_EOK;
4854
}
4955

5056
/* system timer ISR */
5157
void eclic_mtip_handler(void) {
52-
rt_uint64_t ticks;
58+
rt_uint64_t ticks, compare;
5359

5460
/* update compare value */
5561
if (overflow > 0) {
5662
/* overflow 2nd half */
57-
*(rt_uint64_t *)(TIMER_CTRL_ADDR + TIMER_MTIME) = 0;
58-
*(rt_uint64_t *)(TIMER_CTRL_ADDR + TIMER_MTIMECMP) = overflow;
63+
*(volatile rt_uint32_t *)(TIMER_CTRL_ADDR + TIMER_MTIME + 4) = 0;
64+
*(volatile rt_uint32_t *)(TIMER_CTRL_ADDR + TIMER_MTIME) = 0;
65+
*(volatile rt_uint32_t *)(TIMER_CTRL_ADDR + TIMER_MTIMECMP + 4) = (rt_uint32_t)(overflow >> 32);
66+
*(volatile rt_uint32_t *)(TIMER_CTRL_ADDR + TIMER_MTIMECMP) = (rt_uint32_t)(overflow & 0xffffffff);
67+
// *(volatile rt_uint64_t *)(TIMER_CTRL_ADDR + TIMER_MTIME) = 0;
68+
// *(volatile rt_uint64_t *)(TIMER_CTRL_ADDR + TIMER_MTIMECMP) = overflow;
5969
overflow = 0;
6070
return;
6171
}
6272

63-
ticks = *(rt_uint64_t *)(TIMER_CTRL_ADDR + TIMER_MTIME);
64-
if ((ticks + (TIMER_FREQ / RT_TICK_PER_SECOND)) < ticks) {
73+
ticks = get_timer_value();
74+
compare = ticks + (TIMER_FREQ / RT_TICK_PER_SECOND);
75+
if (compare < ticks) {
6576
/* overflow */
66-
overflow = ticks + (TIMER_FREQ / RT_TICK_PER_SECOND);
67-
*(rt_uint64_t *)(TIMER_CTRL_ADDR + TIMER_MTIMECMP) = \
68-
(rt_uint64_t)(-1);
77+
overflow = compare;
78+
*(volatile rt_uint32_t *)(TIMER_CTRL_ADDR + TIMER_MTIMECMP + 4) = 0xffffffff;
79+
*(volatile rt_uint32_t *)(TIMER_CTRL_ADDR + TIMER_MTIMECMP) = 0xffffffff;
80+
// *(volatile rt_uint64_t *)(TIMER_CTRL_ADDR + TIMER_MTIMECMP) = (rt_uint64_t)(-1);
6981
} else {
7082
overflow = 0;
71-
*(rt_uint64_t *)(TIMER_CTRL_ADDR + TIMER_MTIMECMP) = \
72-
ticks + (TIMER_FREQ / RT_TICK_PER_SECOND);
83+
*(volatile rt_uint32_t *)(TIMER_CTRL_ADDR + TIMER_MTIMECMP + 4) = (rt_uint32_t)(compare >> 32);
84+
*(volatile rt_uint32_t *)(TIMER_CTRL_ADDR + TIMER_MTIMECMP) = (rt_uint32_t)(compare & 0xffffffff);
85+
// *(volatile rt_uint64_t *)(TIMER_CTRL_ADDR + TIMER_MTIMECMP) = compare;
7386
}
7487
rt_interrupt_enter();
7588
rt_tick_increase();

‎src/bsp/LonganNano/drv_usart.c‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ enum bsp_usart_channel {
4747
};
4848

4949
/* Private function prototypes -----------------------------------------------*/
50-
static rt_err_t op_configure(struct rt_serial_device *serial,
51-
struct serial_configure *cfg);
50+
static rt_err_t op_configure(struct rt_serial_device *serial, struct serial_configure *cfg);
5251
static rt_err_t op_control(struct rt_serial_device *serial, int cmd, void *arg);
5352
static int op_putc(struct rt_serial_device *serial, char ch);
5453
static int op_getc(struct rt_serial_device *serial);
@@ -82,8 +81,7 @@ static const struct rt_uart_ops usart_ops = {
8281
};
8382

8483
/* Private functions ---------------------------------------------------------*/
85-
static rt_err_t op_configure(struct rt_serial_device *serial,
86-
struct serial_configure *cfg) {
84+
static rt_err_t op_configure(struct rt_serial_device *serial, struct serial_configure *cfg) {
8785
struct bsp_usart_contex *ctx;
8886

8987
RT_ASSERT(serial != RT_NULL);
@@ -163,8 +161,7 @@ static rt_err_t op_control(struct rt_serial_device *serial, int cmd, void *arg)
163161
break;
164162

165163
case RT_DEVICE_CTRL_SET_INT:
166-
eclic_set_nlbits(ECLIC_GROUP_LEVEL3_PRIO1);
167-
eclic_irq_enable(ctx->irq, 1, 0);
164+
eclic_irq_enable(ctx->irq, 5, 1);
168165
/* enable receive interrupt */
169166
usart_interrupt_enable(ctx->usart_base, USART_INT_RBNE);
170167
break;

‎src/components/arduino/drv_spi_st7735.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static const rt_uint8_t init_code[] = {
112112
static const struct rt_device_graphic_info disp_info = {
113113
.pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565,
114114
.bits_per_pixel = 16,
115-
.reserved = 0,
115+
.pitch = 0,
116116
.width = CONFIG_GUI_WIDTH,
117117
.height = CONFIG_GUI_HIGH,
118118
.framebuffer = RT_NULL,
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright (c) 2006-2021, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2009年04月17日 Bernard first version.
9+
*/
10+
11+
#ifndef SPI_MSD_H_INCLUDED
12+
#define SPI_MSD_H_INCLUDED
13+
14+
#include <stdint.h>
15+
#include "spi.h"
16+
17+
/* SD command (SPI mode) */
18+
#define GO_IDLE_STATE 0 /* CMD0 R1 */
19+
#define SEND_OP_COND 1 /* CMD1 R1 */
20+
#define SWITCH_FUNC 6 /* CMD6 R1 */
21+
#define SEND_IF_COND 8 /* CMD8 R7 */
22+
#define SEND_CSD 9 /* CMD9 R1 */
23+
#define SEND_CID 10 /* CMD10 R1 */
24+
#define STOP_TRANSMISSION 12 /* CMD12 R1B */
25+
#define SEND_STATUS 13 /* CMD13 R2 */
26+
#define SET_BLOCKLEN 16 /* CMD16 R1 */
27+
#define READ_SINGLE_BLOCK 17 /* CMD17 R1 */
28+
#define READ_MULTIPLE_BLOCK 18 /* CMD18 R1 */
29+
#define WRITE_BLOCK 24 /* CMD24 R1 */
30+
#define WRITE_MULTIPLE_BLOCK 25 /* CMD25 R1 */
31+
#define PROGRAM_CSD 27 /* CMD27 R1 */
32+
#define SET_WRITE_PROT 28 /* CMD28 R1B */
33+
#define CLR_WRITE_PROT 29 /* CMD29 R1B */
34+
#define SEND_WRITE_PROT 30 /* CMD30 R1 */
35+
#define ERASE_WR_BLK_START_ADDR 32 /* CMD32 R1 */
36+
#define ERASE_WR_BLK_END_ADDR 33 /* CMD33 R1 */
37+
#define ERASE 38 /* CMD38 R1B */
38+
#define LOCK_UNLOCK 42 /* CMD42 R1 */
39+
#define APP_CMD 55 /* CMD55 R1 */
40+
#define GEN_CMD 56 /* CMD56 R1 */
41+
#define READ_OCR 58 /* CMD58 R3 */
42+
#define CRC_ON_OFF 59 /* CMD59 R1 */
43+
44+
/* Application-Specific Command */
45+
#define SD_STATUS 13 /* ACMD13 R2 */
46+
#define SEND_NUM_WR_BLOCKS 22 /* ACMD22 R1 */
47+
#define SET_WR_BLK_ERASE_COUNT 23 /* ACMD23 R1 */
48+
#define SD_SEND_OP_COND 41 /* ACMD41 R1 */
49+
#define SET_CLR_CARD_DETECT 42 /* ACMD42 R1 */
50+
#define SEND_SCR 51 /* ACMD51 R1 */
51+
52+
/* Start Data tokens */
53+
/* Tokens (necessary because at nop/idle (and CS active) only 0xff is on the data/command line) */
54+
#define MSD_TOKEN_READ_START 0xFE /* Data token start byte, Start Single Block Read */
55+
#define MSD_TOKEN_WRITE_SINGLE_START 0xFE /* Data token start byte, Start Single Block Write */
56+
57+
#define MSD_TOKEN_WRITE_MULTIPLE_START 0xFC /* Data token start byte, Start Multiple Block Write */
58+
#define MSD_TOKEN_WRITE_MULTIPLE_STOP 0xFD /* Data toke stop byte, Stop Multiple Block Write */
59+
60+
/* MSD reponses and error flags */
61+
#define MSD_RESPONSE_NO_ERROR 0x00
62+
#define MSD_IN_IDLE_STATE 0x01
63+
#define MSD_ERASE_RESET 0x02
64+
#define MSD_ILLEGAL_COMMAND 0x04
65+
#define MSD_COM_CRC_ERROR 0x08
66+
#define MSD_ERASE_SEQUENCE_ERROR 0x10
67+
#define MSD_ADDRESS_ERROR 0x20
68+
#define MSD_PARAMETER_ERROR 0x40
69+
#define MSD_RESPONSE_FAILURE 0xFF
70+
71+
/* Data response error */
72+
#define MSD_DATA_OK 0x05
73+
#define MSD_DATA_CRC_ERROR 0x0B
74+
#define MSD_DATA_WRITE_ERROR 0x0D
75+
#define MSD_DATA_OTHER_ERROR 0xFF
76+
#define MSD_DATA_RESPONSE_MASK 0x1F
77+
#define MSD_GET_DATA_RESPONSE(res) (res & MSD_DATA_RESPONSE_MASK)
78+
79+
#define MSD_CMD_LEN 6 /**< command, arg and crc. */
80+
#define MSD_RESPONSE_MAX_LEN 5 /**< response max len */
81+
#define MSD_CSD_LEN 16 /**< SD crad CSD register len */
82+
#define SECTOR_SIZE 512 /**< sector size, default 512byte */
83+
84+
/* card try timeout, unit: ms */
85+
#define CARD_TRY_TIMES 3000
86+
#define CARD_TRY_TIMES_ACMD41 800
87+
#define CARD_WAIT_TOKEN_TIMES 800
88+
89+
#define MSD_USE_PRE_ERASED /**< id define MSD_USE_PRE_ERASED, before CMD25, send ACMD23 */
90+
91+
/**
92+
* SD/MMC card type
93+
*/
94+
typedef enum
95+
{
96+
MSD_CARD_TYPE_UNKNOWN = 0, /**< unknown */
97+
MSD_CARD_TYPE_MMC, /**< MultiMedia Card */
98+
MSD_CARD_TYPE_SD_V1_X, /**< Ver 1.X Standard Capacity SD Memory Card */
99+
MSD_CARD_TYPE_SD_V2_X, /**< Ver 2.00 or later Standard Capacity SD Memory Card */
100+
MSD_CARD_TYPE_SD_SDHC, /**< High Capacity SD Memory Card */
101+
MSD_CARD_TYPE_SD_SDXC, /**< later Extended Capacity SD Memory Card */
102+
}msd_card_type;
103+
104+
typedef enum
105+
{
106+
response_type_unknown = 0,
107+
response_r1,
108+
response_r1b,
109+
response_r2,
110+
response_r3,
111+
response_r4,
112+
response_r5,
113+
response_r7,
114+
}response_type;
115+
116+
struct msd_device
117+
{
118+
struct rt_device parent; /**< RT-Thread device struct */
119+
struct rt_device_blk_geometry geometry; /**< sector size, sector count */
120+
struct rt_spi_device * spi_device; /**< SPI interface */
121+
msd_card_type card_type; /**< card type: MMC SD1.x SD2.0 SDHC SDXC */
122+
uint32_t max_clock; /**< MAX SPI clock */
123+
};
124+
125+
extern rt_err_t msd_init(const char * sd_device_name, const char * spi_device_name);
126+
127+
#endif // SPI_MSD_H_INCLUDED

‎src/components/drivers/ipc/pipe.c‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ static int pipe_fops_close(struct dfs_fd *fd)
149149
*
150150
* @return Return the operation status.
151151
* When the return value is 0, it means the operation is successful.
152-
* When the return value is -EINVAL, it means the command is invalid.
152+
* When the return value is -RT_EINVAL, it means the command is invalid.
153153
*/
154154
static int pipe_fops_ioctl(struct dfs_fd *fd, int cmd, void *args)
155155
{
@@ -167,7 +167,7 @@ static int pipe_fops_ioctl(struct dfs_fd *fd, int cmd, void *args)
167167
*((int*)args) = rt_ringbuffer_space_len(pipe->fifo);
168168
break;
169169
default:
170-
ret = -EINVAL;
170+
ret = -RT_EINVAL;
171171
break;
172172
}
173173

@@ -255,7 +255,7 @@ static int pipe_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
255255
rt_pipe_t *pipe;
256256
int wakeup = 0;
257257
int ret = 0;
258-
uint8_t *pbuf;
258+
rt_uint8_t *pbuf;
259259

260260
pipe = (rt_pipe_t *)fd->data;
261261

@@ -268,7 +268,7 @@ static int pipe_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
268268
if (count == 0)
269269
return 0;
270270

271-
pbuf = (uint8_t*)buf;
271+
pbuf = (rt_uint8_t*)buf;
272272
rt_mutex_take(&pipe->lock, -1);
273273

274274
while (1)
@@ -479,18 +479,18 @@ static rt_err_t rt_pipe_close(rt_device_t device)
479479
*/
480480
static rt_size_t rt_pipe_read(rt_device_t device, rt_off_t pos, void *buffer, rt_size_t count)
481481
{
482-
uint8_t *pbuf;
482+
rt_uint8_t *pbuf;
483483
rt_size_t read_bytes = 0;
484484
rt_pipe_t *pipe = (rt_pipe_t *)device;
485485

486486
if (device == RT_NULL)
487487
{
488-
rt_set_errno(EINVAL);
488+
rt_set_errno(RT_EINVAL);
489489
return 0;
490490
}
491491
if (count == 0) return 0;
492492

493-
pbuf = (uint8_t*)buffer;
493+
pbuf = (rt_uint8_t*)buffer;
494494
rt_mutex_take(&(pipe->lock), RT_WAITING_FOREVER);
495495

496496
while (read_bytes < count)
@@ -521,18 +521,18 @@ static rt_size_t rt_pipe_read(rt_device_t device, rt_off_t pos, void *buffer, rt
521521
*/
522522
static rt_size_t rt_pipe_write(rt_device_t device, rt_off_t pos, const void *buffer, rt_size_t count)
523523
{
524-
uint8_t *pbuf;
524+
rt_uint8_t *pbuf;
525525
rt_size_t write_bytes = 0;
526526
rt_pipe_t *pipe = (rt_pipe_t *)device;
527527

528528
if (device == RT_NULL)
529529
{
530-
rt_set_errno(EINVAL);
530+
rt_set_errno(RT_EINVAL);
531531
return 0;
532532
}
533533
if (count == 0) return 0;
534534

535-
pbuf = (uint8_t*)buffer;
535+
pbuf = (rt_uint8_t*)buffer;
536536
rt_mutex_take(&pipe->lock, -1);
537537

538538
while (write_bytes < count)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /