1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
|
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-01-31 shelton first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include "drv_common.h"
#include "drv_gpio.h"
/* defined the led2 pin: pc2 */
#define LED2_PIN GET_PIN(C, 2)
/* defined the led3 pin: pc3 */
#define LED3_PIN GET_PIN(C, 3)
/* defined the led4 pin: pc5 */
#define LED4_PIN GET_PIN(C, 5)
int main(void)
{
rt_uint32_t speed = 200;
/* set led2 pin mode to output */
rt_pin_mode(LED2_PIN, PIN_MODE_OUTPUT);
/* set led3 pin mode to output */
rt_pin_mode(LED3_PIN, PIN_MODE_OUTPUT);
/* set led4 pin mode to output */
rt_pin_mode(LED4_PIN, PIN_MODE_OUTPUT);
while (1)
{
rt_pin_write(LED2_PIN, PIN_LOW);
rt_thread_mdelay(speed);
rt_pin_write(LED3_PIN, PIN_LOW);
rt_thread_mdelay(speed);
rt_pin_write(LED4_PIN, PIN_LOW);
rt_thread_mdelay(speed);
rt_pin_write(LED2_PIN, PIN_HIGH);
rt_thread_mdelay(speed);
rt_pin_write(LED3_PIN, PIN_HIGH);
rt_thread_mdelay(speed);
rt_pin_write(LED4_PIN, PIN_HIGH);
rt_thread_mdelay(speed);
}
}
#define SAMPLE_UART_NAME "uart2"
static struct rt_semaphore rx_sem;
static rt_device_t serial;
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
rt_sem_release(&rx_sem);
return RT_EOK;
}
static void serial_thread_entry(void *parameter)
{
rt_uint32_t len = 0;
char ch[20];
while (1)
{
while ((len = rt_device_read(serial, 0, ch, 50)) <= 0)
{
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
}
rt_device_write(serial, 0, ch, len);
}
}
static int uart_sample(int argc, char *argv[])
{
rt_err_t ret = RT_EOK;
char uart_name[RT_NAME_MAX];
if (argc == 2)
{
rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
}
else
{
rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
}
serial = rt_device_find(uart_name);
if (!serial)
{
rt_kprintf("find %s failed!\n", uart_name);
return RT_ERROR;
}
rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX);
rt_device_set_rx_indicate(serial, uart_input);
rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
ret = RT_ERROR;
}
return ret;
}
MSH_CMD_EXPORT(uart_sample, uart device sample);
#ifdef BSP_USING_SPI
#include "drv_spi.h"
#include "spi_flash.h"
#include "spi_flash_sfud.h"
static int rt_hw_spi_flash_with_sfud_init(void)
{
rt_hw_spi_device_attach("spi2", "spi2_0", GPIOB, GPIO_PINS_12);
if (RT_NULL == rt_sfud_flash_probe("w25q", "spi2_0"))
{
return -RT_ERROR;
}
return RT_EOK;
}
INIT_COMPONENT_EXPORT(rt_hw_spi_flash_with_sfud_init);
#endif
#ifdef BSP_USING_QSPI
#include "drv_qspi.h"
#include "spi_flash.h"
#include "spi_flash_sfud.h"
static int rt_hw_qspi_flash_with_sfud_init(void)
{
at32_qspi_bus_attach_device("qspi2", "qspi2_0", RT_NULL, 4, RT_NULL, RT_NULL);
/* init n25qxx */
if (RT_NULL == rt_sfud_flash_probe("w25q", "qspi2_0"))
{
return -RT_ERROR;
}
return RT_EOK;
}
INIT_COMPONENT_EXPORT(rt_hw_qspi_flash_with_sfud_init);
#endif
#ifdef BSP_USING_ON_CHIP_FLASH
#include "drv_flash.h"
void dump(rt_uint8_t *buff, rt_uint32_t size)
{
rt_uint32_t index = 0;
rt_kprintf("dump data:");
for(index = 0; index < size; index++)
{
rt_kprintf(" %x", *(buff + index));
}
rt_kprintf("\n");
}
void fill(rt_uint8_t *buff, rt_uint32_t size)
{
rt_uint32_t index = 0;
for(index = 0; index < size; index++)
{
*(buff + index) = ~(*(buff + index) + index);
}
}
static int flash_sample(int argc, char *argv[])
{
#define TEST_ADDR 0x0800F000
#define BUFFER_SIZE 0x1000
rt_uint8_t *buff = rt_malloc(BUFFER_SIZE);
rt_memset(buff, 0, BUFFER_SIZE);
if(at32_flash_read(TEST_ADDR, buff, BUFFER_SIZE) != BUFFER_SIZE)
{
rt_kprintf("flash read error\n");
}
dump(buff, BUFFER_SIZE);
if(at32_flash_erase(TEST_ADDR, BUFFER_SIZE) != BUFFER_SIZE)
{
rt_kprintf("flash erase error\n");
}
fill(buff, BUFFER_SIZE);
if(at32_flash_write(TEST_ADDR, buff, BUFFER_SIZE) != BUFFER_SIZE)
{
rt_kprintf("flash write error\n");
}
if(at32_flash_read(TEST_ADDR, buff, BUFFER_SIZE) != BUFFER_SIZE)
{
rt_kprintf("flash read error\n");
}
dump(buff, BUFFER_SIZE);
rt_free(buff);
return 0;
}
MSH_CMD_EXPORT(flash_sample, flash sample);
#endif
#ifdef RT_USING_WDT
#define WDT_DEVICE_NAME "wdt"
static rt_device_t wdt_dev;
static void idle_hook(void)
{
rt_device_control(wdt_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
rt_kprintf("feed the dog!\n ");
}
static int wdt_sample(int argc, char *argv[])
{
rt_err_t ret = RT_EOK;
rt_uint32_t timeout = 1;
char device_name[RT_NAME_MAX];
if (argc == 2)
{
rt_strncpy(device_name, argv[1], RT_NAME_MAX);
}
else
{
rt_strncpy(device_name, WDT_DEVICE_NAME, RT_NAME_MAX);
}
wdt_dev = rt_device_find(device_name);
if (!wdt_dev)
{
rt_kprintf("find %s failed!\n", device_name);
return RT_ERROR;
}
ret = rt_device_control(wdt_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
if (ret != RT_EOK)
{
rt_kprintf("set %s timeout failed!\n", device_name);
return RT_ERROR;
}
ret = rt_device_control(wdt_dev, RT_DEVICE_CTRL_WDT_START, RT_NULL);
if (ret != RT_EOK)
{
rt_kprintf("start %s failed!\n", device_name);
return -RT_ERROR;
}
rt_thread_idle_sethook(idle_hook);
return ret;
}
MSH_CMD_EXPORT(wdt_sample, wdt sample);
#endif
#ifdef BSP_USING_CAN
#define CAN_DEV_NAME "can1"
static struct rt_semaphore rx_sem;
static rt_device_t can_dev;
static rt_err_t can_rx_call(rt_device_t dev, rt_size_t size)
{
rt_sem_release(&rx_sem);
return RT_EOK;
}
static void can_rx_thread(void *parameter)
{
int i;
#ifdef RT_CAN_USING_HDR
rt_err_t res;
#endif
struct rt_can_msg rxmsg = {0};
rt_device_set_rx_indicate(can_dev, can_rx_call);
#ifdef RT_CAN_USING_HDR
struct rt_can_filter_item items[5] =
{
RT_CAN_FILTER_ITEM_INIT(0x100, 0, 0, 0, 0x700, RT_NULL, RT_NULL), /* std,match ID:0x100~0x1ff,hdr ? - 1,??????? */
RT_CAN_FILTER_ITEM_INIT(0x300, 0, 0, 0, 0x700, RT_NULL, RT_NULL), /* std,match ID:0x300~0x3ff,hdr ? - 1 */
RT_CAN_FILTER_ITEM_INIT(0x211, 0, 0, 0, 0x7ff, RT_NULL, RT_NULL), /* std,match ID:0x211,hdr ? - 1 */
RT_CAN_FILTER_STD_INIT(0x486, RT_NULL, RT_NULL), /* std,match ID:0x486,hdr ? - 1 */
{0x555, 0, 0, 0, 0x7ff, 7,} /* std,match ID:0x555,hdr ? 7,???? 7 ???? */
};
struct rt_can_filter_config cfg = {5, 1, items};
res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg);
RT_ASSERT(res == RT_EOK);
#endif
while (1)
{
rxmsg.hdr_index = -1;
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
rt_device_read(can_dev, 0, &rxmsg, sizeof(rxmsg));
rt_kprintf("ID:%x", rxmsg.id);
for (i = 0; i < 8; i++)
{
rt_kprintf("%2x", rxmsg.data[i]);
}
rt_kprintf("\n");
}
}
int can_sample(int argc, char *argv[])
{
struct rt_can_msg msg = {0};
rt_err_t res;
rt_size_t size;
rt_thread_t thread;
char can_name[RT_NAME_MAX];
if (argc == 2)
{
rt_strncpy(can_name, argv[1], RT_NAME_MAX);
}
else
{
rt_strncpy(can_name, CAN_DEV_NAME, RT_NAME_MAX);
}
can_dev = rt_device_find(can_name);
if (!can_dev)
{
rt_kprintf("find %s failed!\n", can_name);
return RT_ERROR;
}
rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
res = rt_device_open(can_dev, RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_INT_RX);
RT_ASSERT(res == RT_EOK);
res = rt_device_control(can_dev, RT_CAN_CMD_SET_BAUD, (void *)CAN1MBaud);
thread = rt_thread_create("can_rx", can_rx_thread, RT_NULL, 1024, 25, 10);
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
rt_kprintf("create can_rx thread failed!\n");
}
msg.id = 0x78;
msg.ide = RT_CAN_STDID;
msg.rtr = RT_CAN_DTR;
msg.len = 8;
msg.data[0] = 0x00;
msg.data[1] = 0x11;
msg.data[2] = 0x22;
msg.data[3] = 0x33;
msg.data[4] = 0x44;
msg.data[5] = 0x55;
msg.data[6] = 0x66;
msg.data[7] = 0x77;
size = rt_device_write(can_dev, 0, &msg, sizeof(msg));
if (size == 0)
{
rt_kprintf("can dev write data failed!\n");
}
return res;
}
MSH_CMD_EXPORT(can_sample, can_sample);
#endif
#ifdef BSP_USING_I2C
#define EEPROM_I2C_BUS_NAME "i2c1"
#define EEPROM_ADDR 0x50
static rt_err_t read_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t *data)
{
rt_uint8_t buf[3];
struct rt_i2c_msg msgs;
buf[0] = reg;
msgs.addr = EEPROM_ADDR;
msgs.flags = RT_I2C_WR;
msgs.buf = buf;
msgs.len = 1;
if (rt_i2c_transfer(bus, &msgs, 1) != 1)
{
return -RT_ERROR;
}
msgs.addr = EEPROM_ADDR;
msgs.flags = RT_I2C_RD;
msgs.buf = data;
msgs.len = 2;
if (rt_i2c_transfer(bus, &msgs, 1) == 1)
{
return RT_EOK;
}
else
{
return -RT_ERROR;
}
}
static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t *data)
{
rt_uint8_t buf[3];
struct rt_i2c_msg msgs;
buf[0] = reg;
buf[1] = data[0];
buf[2] = data[1];
msgs.addr = EEPROM_ADDR;
msgs.flags = RT_I2C_WR;
msgs.buf = buf;
msgs.len = 3;
if (rt_i2c_transfer(bus, &msgs, 1) == 1)
{
return RT_EOK;
}
else
{
return -RT_ERROR;
}
}
int i2c_sample(int argc, char *argv[])
{
rt_err_t ret = RT_EOK;
static struct rt_i2c_bus_device *i2c_bus = RT_NULL;
rt_uint8_t rx_buff[2] = {0}, tx_buff[2] = {0x12, 0x34};
i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(EEPROM_I2C_BUS_NAME);
if (i2c_bus == RT_NULL)
{
rt_kprintf("can't find %s device!\n", EEPROM_I2C_BUS_NAME);
}
else
{
write_reg(i2c_bus, 0x10, tx_buff);
rt_thread_mdelay(10);
read_reg(i2c_bus, 0x10, rx_buff);
rt_kprintf("rx_buff: %x %x\r\n", rx_buff[0], rx_buff[1]);
}
return ret;
}
MSH_CMD_EXPORT(i2c_sample, i2c sample);
#endif
#ifdef BSP_USING_HWTIMER
#define HWTIMER_DEV_NAME "timer3"
static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
{
rt_kprintf("this is hwtimer timeout callback fucntion!\n");
rt_kprintf("tick is :%d !\n", rt_tick_get());
return 0;
}
static int hwtimer_sample(int argc, char *argv[])
{
rt_err_t ret = RT_EOK;
rt_hwtimerval_t timeout_s;
rt_device_t hw_dev = RT_NULL;
rt_hwtimer_mode_t mode;
hw_dev = rt_device_find(HWTIMER_DEV_NAME);
if (hw_dev == RT_NULL)
{
rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
return RT_ERROR;
}
ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
if (ret != RT_EOK)
{
rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
return ret;
}
rt_device_set_rx_indicate(hw_dev, timeout_cb);
mode = HWTIMER_MODE_PERIOD;
ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
if (ret != RT_EOK)
{
rt_kprintf("set mode failed! ret is :%d\n", ret);
return ret;
}
timeout_s.sec = 5;
timeout_s.usec = 0;
if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
{
rt_kprintf("set timeout value failed\n");
return RT_ERROR;
}
rt_thread_mdelay(1000);
rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
rt_kprintf("Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec);
return ret;
}
MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);
#endif
#ifdef BSP_USING_SPI
#include <rtthread.h>
#include <rtdevice.h>
#include "drv_spi.h"
#define W25Q_SPI_DEVICE "spi1_0"
#define MAX_DATA_LEN 3
static void spi_sample(int argc, char * argv[])
{
struct rt_spi_device *spi_device;
char name[RT_NAME_MAX];
rt_uint8_t tx_data[MAX_DATA_LEN] = {0x0};
rt_uint8_t rx_data[MAX_DATA_LEN] = {0x0};
rt_uint32_t index = 0;
rt_hw_spi_device_attach("spi1", W25Q_SPI_DEVICE, GPIOA, GPIO_PINS_4);
for(index = 0; index < MAX_DATA_LEN; index ++)
{
tx_data[index] = index;
}
if(argc == 2)
{
rt_strncpy(name, argv[1], RT_NAME_MAX);
}
else
{
rt_strncpy(name, W25Q_SPI_DEVICE, RT_NAME_MAX);
}
spi_device = (struct rt_spi_device *)rt_device_find(name);
if(!spi_device)
{
rt_kprintf("can't find %s device!\n", name);
}
else
{
rt_spi_send_then_recv(spi_device, tx_data, MAX_DATA_LEN, rx_data, MAX_DATA_LEN);
rt_kprintf("recv data:\n");
for(index = 0; index < MAX_DATA_LEN; index ++)
{
rt_kprintf(" 0x%x", rx_data[index]);
}
rt_kprintf("\n");
struct rt_spi_message msg1, msg2;
msg1.send_buf = tx_data;
msg1.recv_buf = RT_NULL;
msg1.length = MAX_DATA_LEN;
msg1.cs_take = 1;
msg1.cs_release = 0;
msg1.next = RT_NULL;
rt_spi_transfer_message(spi_device, &msg1);
msg2.send_buf = RT_NULL;
msg2.recv_buf = rx_data;
msg2.length = MAX_DATA_LEN;
msg2.cs_take = 0;
msg2.cs_release = 1;
msg2.next = RT_NULL;
rt_spi_transfer_message(spi_device, &msg2);
}
}
MSH_CMD_EXPORT(spi_sample, spi device sample);
#endif
|