Просмотр исходного кода

delete needless readme.md and change some code

luo jiao 7 лет назад
Родитель
Сommit
ccf158c207
17 измененных файлов с 155 добавлено и 172 удалено
  1. 4 9
      README.md
  2. 0 19
      SConscript
  3. 0 0
      block/README.md
  4. 0 0
      can/README.md
  5. 0 0
      ethernet/README.md
  6. 0 0
      i2c/README.md
  7. 15 15
      i2c/i2c_aht10_sample.c
  8. 0 0
      pin/README.md
  9. 59 0
      pin/pin_beep_sample.c
  10. 0 53
      pin/pin_sample.c
  11. 0 0
      pwm/README.md
  12. 0 0
      serial/README.md
  13. 18 16
      serial/uart_sample.c
  14. 0 0
      spi/README.md
  15. 0 60
      spi/spi_sample.c
  16. 59 0
      spi/spi_w25q_sample.c
  17. 0 0
      usb/README.md

+ 4 - 9
README.md

@@ -8,15 +8,10 @@
 
 | 目录             | 例程                            |
 | ---------------- | ------------------------------- |
-| block            | 块设备的使用            |
-| can              | can 设备的使用             |
-| ethernet         | ethernet 设备的使用             |
-| i2c              | i2c 设备的使用             |
-| pin              | pin 设备的使用             |
-| pwm              | pwm 设备的使用 |
-| serial           | serial 设备的使用             |
-| spi              | spi 设备的使用             |
-| usb              | usb 设备的使用 |
+| i2c              | i2c 设备的使用                  |
+| pin              | pin 设备的使用                  |
+| serial           | serial 设备的使用               |
+| spi              | spi 设备的使用                  |
 
 ### 1.2 许可证
 

+ 0 - 19
SConscript

@@ -8,17 +8,6 @@ group = []
 CPPPATH = []
 
 # add kernel samples.
-if GetDepend('PERIPHERAL_SAMPLES_USING_BLOCK'):
-    src += Glob('block/*.c')
-    CPPPATH += [cwd + '/block']
-
-if GetDepend('PERIPHERAL_SAMPLES_USING_CAN'):
-    src += Glob('can/*.c')
-    CPPPATH += [cwd + '/can']
-
-if GetDepend('PERIPHERAL_SAMPLES_USING_ETHERNET'):
-    src += Glob('ethernet/*.c')
-    CPPPATH += [cwd + '/ethernet']
 
 if GetDepend('PERIPHERAL_SAMPLES_USING_I2C'):
     src += Glob('i2c/*.c')
@@ -28,10 +17,6 @@ if GetDepend('PERIPHERAL_SAMPLES_USING_PIN'):
     src += Glob('pin/*.c')
     CPPPATH += [cwd + '/pin']
 
-if GetDepend('PERIPHERAL_SAMPLES_USING_PWM'):
-    src += Glob('pwm/*.c')
-    CPPPATH += [cwd + '/pwm']
-
 if GetDepend('PERIPHERAL_SAMPLES_USING_SERIAL'):
     src += Glob('serial/*.c')
     CPPPATH += [cwd + '/serial']
@@ -40,10 +25,6 @@ if GetDepend('PERIPHERAL_SAMPLES_USING_SPI'):
     src += Glob('spi/*.c')
     CPPPATH += [cwd + '/spi']
 
-if GetDepend('PERIPHERAL_SAMPLES_USING_USB'):
-    src += Glob('usb/*.c')
-    CPPPATH += [cwd + '/usb']
-
 group = DefineGroup('peripheral-samples', src, depend = ['PKG_USING_PERIPHERAL_SAMPLES'], CPPPATH = CPPPATH)
 
 Return('group')

+ 0 - 0
block/README.md


+ 0 - 0
can/README.md


+ 0 - 0
ethernet/README.md


+ 0 - 0
i2c/README.md


+ 15 - 15
i2c/i2c_sample.c → i2c/i2c_aht10_sample.c

@@ -10,15 +10,18 @@
 
 #include <rtthread.h>
 #include <rtdevice.h>
+#include <stdbool.h>
 
 #define AHT10_ADDR                  0x38
 #define AHT10_CALIBRATION_CMD       0xE1    /* 校准命令 */
 #define AHT10_NORMAL_CMD            0xA8    /* 一般命令 */
 #define AHT10_GET_DATA              0xAC    /* 获取数据命令 */
-#define I2C_BUS_NAME                "i2c1"  /* 传感器连接的I2C总线设备名称 */
+#ifndef AHT10_I2C_BUS_NAME
+#define AHT10_I2C_BUS_NAME          "i2c1"  /* 传感器连接的I2C总线设备名称 */
+#endif
 
 static struct rt_i2c_bus_device *i2c_bus = RT_NULL;
-static rt_uint8_t init_flag = 0;      /* 0:传感器没有初始化,1:传感器已经初始化 */
+static bool initialized = false;                 /* 传感器初始化状态 */
 
 /* 写 传感器寄存器 */
 static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t *data)
@@ -71,39 +74,39 @@ static void aht10_init(void)
     rt_uint8_t temp[2] = {0, 0};
 
     /* 查找I2C总线设备,获取I2C总线设备句柄 */
-    i2c_bus = rt_i2c_bus_device_find(I2C_BUS_NAME);
+    i2c_bus = rt_i2c_bus_device_find(AHT10_I2C_BUS_NAME);
 
     if (i2c_bus == RT_NULL)
     {
-        rt_kprintf("can't find %s device", I2C_BUS_NAME);
+        rt_kprintf("can't find %s device", AHT10_I2C_BUS_NAME);
     }
     else
     {
         write_reg(i2c_bus, AHT10_NORMAL_CMD, temp);
-        rt_thread_delay(rt_tick_from_millisecond(600));     /* at least 300 ms */
+        rt_thread_mdelay(600);     /* at least 300 ms */
 
         temp[0] = 0x08;
         temp[1] = 0x00;
         write_reg(i2c_bus, AHT10_CALIBRATION_CMD, temp); 
-        rt_thread_delay(rt_tick_from_millisecond(600));     /* at least 300 ms */
+        rt_thread_mdelay(600);     /* at least 300 ms */
 
-        init_flag = 1;
+        initialized = true;
     }
 }
 
-static void i2c_sample(void)
+static void i2c_aht10_sample(void)
 {
     float humidity, temperature;
 
     humidity = 0.0;
     temperature = 0.0;
 
-    if (0 == init_flag)
+    if (!initialized)
     {
         /* 传感器初始化 */
         aht10_init();
     }
-    if (1 == init_flag)
+    if (initialized)
     {
         /* 读取温湿度数据 */
         read_temp_humi(&temperature, &humidity);
@@ -113,11 +116,8 @@ static void i2c_sample(void)
     }
     else
     {
-        rt_kprintf("init sensor failed\n");
+        rt_kprintf("initialize sensor failed\n");
     }
 }
 /* 导出到 msh 命令列表中 */
-MSH_CMD_EXPORT(i2c_sample, i2c device sample);
-
-
-
+MSH_CMD_EXPORT(i2c_aht10_sample, i2c aht10 sample);

+ 0 - 0
pin/README.md


+ 59 - 0
pin/pin_beep_sample.c

@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2018-08-15     misonyo      first implementation.
+ */
+
+#include <rtthread.h>
+#include <rtdevice.h>
+
+/* 引脚编号,通过查看drv_gpio.c确定 */
+#ifndef BEEP_PIN_NUM
+#define BEEP_PIN_NUM            37  /* PB2 */
+#endif
+#ifndef KEY0_PIN_NUM
+#define KEY0_PIN_NUM            55  /* PD8 */
+#endif
+#ifndef KEY1_PIN_NUM
+#define KEY1_PIN_NUM            56  /* PD9 */
+#endif
+
+void beep_on(void *args)
+{
+    rt_kprintf("turn on beep!\n");
+    
+    rt_pin_write(BEEP_PIN_NUM, PIN_HIGH);
+}
+
+void beep_off(void *args)
+{
+    rt_kprintf("turn off beep!\n");
+    
+    rt_pin_write(BEEP_PIN_NUM, PIN_LOW);
+}
+
+static void pin_sample(void)
+{
+    /* 按键0引脚为输入模式 */
+    rt_pin_mode(KEY0_PIN_NUM, PIN_MODE_INPUT_PULLUP);
+    /* 绑定中断,上升沿模式,回调函数名为beep_on */
+    rt_pin_attach_irq(KEY0_PIN_NUM, PIN_IRQ_MODE_FALLING, beep_on, RT_NULL);
+    /* 使能中断 */
+    rt_pin_irq_enable(KEY0_PIN_NUM, PIN_IRQ_ENABLE); 
+    
+    /* 按键1引脚为输入模式 */
+    rt_pin_mode(KEY1_PIN_NUM, PIN_MODE_INPUT_PULLUP);
+    /* 绑定中断,上升沿模式,回调函数名为beep_off */
+    rt_pin_attach_irq(KEY1_PIN_NUM, PIN_IRQ_MODE_FALLING, beep_off, RT_NULL);
+    /* 使能中断 */
+    rt_pin_irq_enable(KEY1_PIN_NUM, PIN_IRQ_ENABLE);
+    /* 蜂鸣器引脚为输出模式 */
+    rt_pin_mode(BEEP_PIN_NUM, PIN_MODE_OUTPUT);
+
+}
+/* 导出到 msh 命令列表中 */
+MSH_CMD_EXPORT(pin_beep_sample, pin beep sample);

+ 0 - 53
pin/pin_sample.c

@@ -1,53 +0,0 @@
-/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date           Author       Notes
- * 2018-08-15     misonyo      first implementation.
- */
-
-#include <rtthread.h>
-#include <rtdevice.h>
-
-/* 引脚编号,通过查看drv_gpio.c确定 */
-#define BEEP_PIN        37  /* PB2 */
-#define KEY0            55  /* PD8 */
-#define KEY1            56  /* PD9 */
-
-void beep_on(void *args)
-{
-    rt_kprintf("turn on beep!\n");
-    
-    rt_pin_write(BEEP_PIN, PIN_HIGH);
-}
-
-void beep_off(void *args)
-{
-    rt_kprintf("turn off beep!\n");
-    
-    rt_pin_write(BEEP_PIN, PIN_LOW);
-}
-
-static void pin_sample(void)
-{
-    /* 按键0引脚为输入模式 */
-    rt_pin_mode(KEY0, PIN_MODE_INPUT_PULLUP);
-    /* 绑定中断,上升沿模式,回调函数名为beep_on */
-    rt_pin_attach_irq(KEY0, PIN_IRQ_MODE_FALLING, beep_on, RT_NULL);
-    /* 使能中断 */
-    rt_pin_irq_enable(KEY0, PIN_IRQ_ENABLE); 
-    
-    /* 按键1引脚为输入模式 */
-    rt_pin_mode(KEY1, PIN_MODE_INPUT_PULLUP);
-    /* 绑定中断,上升沿模式,回调函数名为beep_off */
-    rt_pin_attach_irq(KEY1, PIN_IRQ_MODE_FALLING, beep_off, RT_NULL);
-    /* 使能中断 */
-    rt_pin_irq_enable(KEY1, PIN_IRQ_ENABLE);
-    /* 蜂鸣器引脚为输出模式 */
-    rt_pin_mode(BEEP_PIN, PIN_MODE_OUTPUT);
-
-}
-/* 导出到 msh 命令列表中 */
-MSH_CMD_EXPORT(pin_sample, pin device sample);

+ 0 - 0
pwm/README.md


+ 0 - 0
serial/README.md


+ 18 - 16
serial/uart_sample.c

@@ -10,14 +10,16 @@
 
 #include <rtthread.h>
 
-#define UART_NAME       "uart2"
+#ifndef SAMPLE_UART_NAME
+#define SAMPLE_UART_NAME       "uart2"
+#endif
 /* 用于接收消息的信号量 */
 static struct rt_semaphore rx_sem;
 
 /* 接收数据回调函数 */
 static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
 {
-    /* 串口2接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
+    /* 串口接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
     rt_sem_release(&rx_sem);
 
     return RT_EOK;
@@ -26,38 +28,38 @@ static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
 static void serial_thread_entry(void* parameter)
 {
     char ch;
-    rt_device_t serial2;
+    rt_device_t serial;
     char str[] = "hello RT-Thread!\r\n";
 
-    /* 查找系统中的串口2设备 */
-    serial2 = rt_device_find(UART_NAME);
+    /* 查找系统中的串口设备 */
+    serial = rt_device_find(UART_NAME);
     
-    if (serial2 != RT_NULL)
+    if (serial != RT_NULL)
     {
         rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
         /* 以读写及中断接收方式打开串口设备 */
-        rt_device_open(serial2, RT_DEVICE_OFLAG_RDWR|RT_DEVICE_FLAG_INT_RX);
-        /* 设置回调函数 */
-        rt_device_set_rx_indicate(serial2, uart_input);
+        rt_device_open(serial, RT_DEVICE_OFLAG_RDWR|RT_DEVICE_FLAG_INT_RX);
+        /* 设置接收回调函数 */
+        rt_device_set_rx_indicate(serial, uart_input);
         /* 发送字符串 */
-        rt_device_write(serial2, 0, str, (sizeof(str) - 1));
+        rt_device_write(serial, 0, str, (sizeof(str) - 1));
         
         while (1)
         {
-            /* 从串口2读取一个字节的数据,没有读取到则等待接收信号量 */
-            while (rt_device_read(serial2, -1, &ch, 1) != 1)
+            /* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */
+            while (rt_device_read(serial, -1, &ch, 1) != 1)
             {
-                /* 阻塞等待 接收信号量,等到信号量后再次读取数据 */
+                /* 阻塞等待接收信号量,等到信号量后再次读取数据 */
                 rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
             }
-        /* 读取到的数据通过串口2错位输出 */
+        /* 读取到的数据通过串口错位输出 */
         ch = ch + 1;
-        rt_device_write(serial2, 0, &ch, 1);
+        rt_device_write(serial, 0, &ch, 1);
         }
     }
     else
     {
-        rt_kprintf("open uart2 failed!\n");
+        rt_kprintf("open serial failed!\n");
     }
 }
 

+ 0 - 0
spi/README.md


+ 0 - 60
spi/spi_sample.c

@@ -1,60 +0,0 @@
-/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date           Author       Notes
- * 2018-08-15     misonyo      first implementation.
- */
-
-#include <rtthread.h>
-#include <rtdevice.h>
-#include <string.h>
-
-#define SPI_DEVICE_NAME     "qspi10"
-
-static void spi_sample(int argc,char *argv[])
-{
-    struct rt_spi_device *spi_dev_w25q128;
-    rt_uint8_t w25x_read_id = 0x90;
-    rt_uint8_t id[5] = {0};
-
-    /* find qspi10 device */
-    spi_dev_w25q128 = (struct rt_spi_device *)rt_device_find(SPI_DEVICE_NAME);
-    if (!spi_dev_w25q128)
-    {
-        rt_kprintf("spi sample run failed! can't find qspi device!");
-    }
-    else
-    {
-        if ((argc >= 2) && (strcmp(argv[1], "method1") == 0))
-        {
-            rt_spi_send_then_recv(spi_dev_w25q128, &w25x_read_id, 1, id, 5);
-            rt_kprintf("use method1 read w25q128 ID is:%x%x\n", id[3], id[4]);
-        }
-        else    /* 使用 rt_spi_transfer_message()*/
-        {
-            struct rt_spi_message msg1,msg2;
-
-            msg1.send_buf   = &w25x_read_id;
-            msg1.recv_buf   = RT_NULL;
-            msg1.length     = 1;
-            msg1.cs_take    = 1;
-            msg1.cs_release = 0;
-            msg1.next       = &msg2;
-
-            msg2.send_buf   = RT_NULL;
-            msg2.recv_buf   = id;
-            msg2.length     = 5;
-            msg2.cs_take    = 0;
-            msg2.cs_release = 1;
-            msg2.next       = RT_NULL;
-
-            rt_spi_transfer_message(spi_dev_w25q128, &msg1);
-            rt_kprintf("use method2 read w25q128 ID is:%x%x\n", id[3], id[4]);
-        }
-    }
-}
-/* 导出到 msh 命令列表中 */
-MSH_CMD_EXPORT(spi_sample, spi device sample);

+ 59 - 0
spi/spi_w25q_sample.c

@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2018-08-15     misonyo      first implementation.
+ */
+
+#include <rtthread.h>
+#include <rtdevice.h>
+
+#ifndef W25Q_SPI_DEVICE_NAME
+#define W25Q_SPI_DEVICE_NAME     "qspi10"
+#endif
+
+static void spi_sample(int argc,char *argv[])
+{
+    struct rt_spi_device *spi_dev_w25q;
+    rt_uint8_t w25x_read_id = 0x90;
+    rt_uint8_t id[5] = {0};
+
+    /* 查找 qspi 设备获取设备句柄 */
+    spi_dev_w25q = (struct rt_spi_device *)rt_device_find(W25Q_SPI_DEVICE_NAME);
+    if (!spi_dev_w25q)
+    {
+        rt_kprintf("spi sample run failed! can't find qspi device!");
+    }
+    else
+    {
+        /* 方式1:使用 rt_spi_send_then_recv()发送命令读取ID */
+        rt_spi_send_then_recv(spi_dev_w25q, &w25x_read_id, 1, id, 5);
+        rt_kprintf("use rt_spi_send_then_recv() read w25q ID is:%x%x\n", id[3], id[4]);
+
+        /* 方式2:使用 rt_spi_transfer_message()发送命令读取ID */
+        struct rt_spi_message msg1,msg2;
+
+        msg1.send_buf   = &w25x_read_id;
+        msg1.recv_buf   = RT_NULL;
+        msg1.length     = 1;
+        msg1.cs_take    = 1;
+        msg1.cs_release = 0;
+        msg1.next       = &msg2;
+
+        msg2.send_buf   = RT_NULL;
+        msg2.recv_buf   = id;
+        msg2.length     = 5;
+        msg2.cs_take    = 0;
+        msg2.cs_release = 1;
+        msg2.next       = RT_NULL;
+
+        rt_spi_transfer_message(spi_dev_w25q, &msg1);
+        rt_kprintf("use rt_spi_transfer_message() read w25q ID is:%x%x\n", id[3], id[4]);
+
+    }
+}
+/* 导出到 msh 命令列表中 */
+MSH_CMD_EXPORT(spi_w25q_sample, spi w25q sample);

+ 0 - 0
usb/README.md