Explorar el Código

add pwm device sample and astyle all files

misonyo hace 7 años
padre
commit
43d8621fc5
Se han modificado 8 ficheros con 405 adiciones y 324 borrados
  1. 142 142
      i2c_aht10_sample.c
  2. 6 6
      iwdg_sample.c
  3. 3 3
      led_blink_sample.c
  4. 67 67
      pin_beep_sample.c
  5. 81 0
      pwm_led_sample.c
  6. 11 11
      sd_sample.c
  7. 3 3
      spi_w25q_sample.c
  8. 92 92
      uart_sample.c

+ 142 - 142
i2c_aht10_sample.c

@@ -1,142 +1,142 @@
-/*
- * 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.
- */
-/* 
- * 程序清单:这是一个 I2C 设备使用例程
- * 例程导出了 i2c_aht10_sample 命令到控制终端
- * 命令调用格式:i2c_aht10_sample i2c1
- * 命令解释:命令第二个参数是要使用的I2C总线设备名称,为空则使用默认的I2C总线设备
- * 程序功能:通过 I2C 设备读取温湿度传感器 aht10 的温湿度数据并打印
-*/
-
-#include <rtthread.h>
-#include <rtdevice.h>
-
-#define AHT10_I2C_BUS_NAME          "i2c1"  /* 传感器连接的I2C总线设备名称 */
-#define AHT10_ADDR                  0x38
-#define AHT10_CALIBRATION_CMD       0xE1    /* 校准命令 */
-#define AHT10_NORMAL_CMD            0xA8    /* 一般命令 */
-#define AHT10_GET_DATA              0xAC    /* 获取数据命令 */
-
-static struct rt_i2c_bus_device *i2c_bus = RT_NULL;
-static rt_bool_t initialized = RT_FALSE;        /* 传感器初始化状态 */
-
-/* 写传感器寄存器 */
-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; //cmd
-    buf[1] = data[0];
-    buf[2] = data[1];
-
-    msgs.addr = AHT10_ADDR;
-    msgs.flags = RT_I2C_WR;
-    msgs.buf = buf;
-    msgs.len = 3;
-    
-    /* 调用I2C设备接口发送数据 */
-    if (rt_i2c_transfer(bus, &msgs, 1) == 1)
-        return RT_EOK;
-    else
-        return -RT_ERROR;
-}
-
-/* 读传感器寄存器数据 */
-static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint8_t len, rt_uint8_t *buf)
-{
-    struct rt_i2c_msg msgs;
-
-    msgs.addr = AHT10_ADDR;
-    msgs.flags = RT_I2C_RD;
-    msgs.buf = buf;
-    msgs.len = len;
-
-    /* 调用I2C设备接口接收数据 */
-    if (rt_i2c_transfer(bus, &msgs, 1) == 1)
-        return RT_EOK;
-    else
-        return -RT_ERROR;
-}
-
-static void read_temp_humi(float *cur_temp,float *cur_humi)
-{
-    rt_uint8_t temp[6];
-
-    write_reg(i2c_bus, AHT10_GET_DATA, 0);      /* 发送命令 */
-    read_regs(i2c_bus, 6, temp);                /* 获取传感器数据 */
-
-    /* 湿度数据转换 */
-    *cur_humi = (temp[1] << 12 | temp[2] << 4 | (temp[3] & 0xf0) >> 4) * 100.0 / (1 << 20);
-    /* 温度数据转换 */
-    *cur_temp = ((temp[3] & 0xf) << 16 | temp[4] << 8 | temp[5]) * 200.0 / (1 << 20) - 50;
-}
-
-static void aht10_init(const char *name)
-{
-    rt_uint8_t temp[2] = {0, 0};
-
-    /* 查找I2C总线设备,获取I2C总线设备句柄 */
-    i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(name);
-
-    if (i2c_bus == RT_NULL)
-    {
-        rt_kprintf("can't find %s device!\n", name);
-    }
-    else
-    {
-        write_reg(i2c_bus, AHT10_NORMAL_CMD, temp);
-        rt_thread_mdelay(400);
-
-        temp[0] = 0x08;
-        temp[1] = 0x00;
-        write_reg(i2c_bus, AHT10_CALIBRATION_CMD, temp); 
-        rt_thread_mdelay(400);
-        initialized = RT_TRUE;
-    }
-}
-
-static void i2c_aht10_sample(int argc,char *argv[])
-{
-    float humidity, temperature;
-    char name[RT_NAME_MAX];
-
-    humidity = 0.0;
-    temperature = 0.0;
-
-    if (argc == 2)
-    {
-        rt_strncpy(name, argv[1], RT_NAME_MAX);
-    }
-    else
-    {
-        rt_strncpy(name, AHT10_I2C_BUS_NAME, RT_NAME_MAX);
-    }
-
-    if (!initialized)
-    {
-        /* 传感器初始化 */
-        aht10_init(name);
-    }
-    if (initialized)
-    {
-        /* 读取温湿度数据 */
-        read_temp_humi(&temperature, &humidity);
-
-        rt_kprintf("read aht10 sensor humidity   : %d.%d %%\n", (int)humidity, (int)(humidity * 10) % 10);
-        rt_kprintf("read aht10 sensor temperature: %d.%d \n", (int)temperature, (int)(temperature * 10) % 10);
-    }
-    else
-    {
-        rt_kprintf("initialize sensor failed!\n");
-    }
-}
-/* 导出到 msh 命令列表中 */
-MSH_CMD_EXPORT(i2c_aht10_sample, i2c aht10 sample);
+/*
+ * 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.
+ */
+/*
+ * 程序清单:这是一个 I2C 设备使用例程
+ * 例程导出了 i2c_aht10_sample 命令到控制终端
+ * 命令调用格式:i2c_aht10_sample i2c1
+ * 命令解释:命令第二个参数是要使用的I2C总线设备名称,为空则使用默认的I2C总线设备
+ * 程序功能:通过 I2C 设备读取温湿度传感器 aht10 的温湿度数据并打印
+*/
+
+#include <rtthread.h>
+#include <rtdevice.h>
+
+#define AHT10_I2C_BUS_NAME          "i2c1"  /* 传感器连接的I2C总线设备名称 */
+#define AHT10_ADDR                  0x38
+#define AHT10_CALIBRATION_CMD       0xE1    /* 校准命令 */
+#define AHT10_NORMAL_CMD            0xA8    /* 一般命令 */
+#define AHT10_GET_DATA              0xAC    /* 获取数据命令 */
+
+static struct rt_i2c_bus_device *i2c_bus = RT_NULL;
+static rt_bool_t initialized = RT_FALSE;        /* 传感器初始化状态 */
+
+/* 写传感器寄存器 */
+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; //cmd
+    buf[1] = data[0];
+    buf[2] = data[1];
+
+    msgs.addr = AHT10_ADDR;
+    msgs.flags = RT_I2C_WR;
+    msgs.buf = buf;
+    msgs.len = 3;
+
+    /* 调用I2C设备接口发送数据 */
+    if (rt_i2c_transfer(bus, &msgs, 1) == 1)
+        return RT_EOK;
+    else
+        return -RT_ERROR;
+}
+
+/* 读传感器寄存器数据 */
+static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint8_t len, rt_uint8_t *buf)
+{
+    struct rt_i2c_msg msgs;
+
+    msgs.addr = AHT10_ADDR;
+    msgs.flags = RT_I2C_RD;
+    msgs.buf = buf;
+    msgs.len = len;
+
+    /* 调用I2C设备接口接收数据 */
+    if (rt_i2c_transfer(bus, &msgs, 1) == 1)
+        return RT_EOK;
+    else
+        return -RT_ERROR;
+}
+
+static void read_temp_humi(float *cur_temp, float *cur_humi)
+{
+    rt_uint8_t temp[6];
+
+    write_reg(i2c_bus, AHT10_GET_DATA, 0);      /* 发送命令 */
+    read_regs(i2c_bus, 6, temp);                /* 获取传感器数据 */
+
+    /* 湿度数据转换 */
+    *cur_humi = (temp[1] << 12 | temp[2] << 4 | (temp[3] & 0xf0) >> 4) * 100.0 / (1 << 20);
+    /* 温度数据转换 */
+    *cur_temp = ((temp[3] & 0xf) << 16 | temp[4] << 8 | temp[5]) * 200.0 / (1 << 20) - 50;
+}
+
+static void aht10_init(const char *name)
+{
+    rt_uint8_t temp[2] = {0, 0};
+
+    /* 查找I2C总线设备,获取I2C总线设备句柄 */
+    i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(name);
+
+    if (i2c_bus == RT_NULL)
+    {
+        rt_kprintf("can't find %s device!\n", name);
+    }
+    else
+    {
+        write_reg(i2c_bus, AHT10_NORMAL_CMD, temp);
+        rt_thread_mdelay(400);
+
+        temp[0] = 0x08;
+        temp[1] = 0x00;
+        write_reg(i2c_bus, AHT10_CALIBRATION_CMD, temp);
+        rt_thread_mdelay(400);
+        initialized = RT_TRUE;
+    }
+}
+
+static void i2c_aht10_sample(int argc, char *argv[])
+{
+    float humidity, temperature;
+    char name[RT_NAME_MAX];
+
+    humidity = 0.0;
+    temperature = 0.0;
+
+    if (argc == 2)
+    {
+        rt_strncpy(name, argv[1], RT_NAME_MAX);
+    }
+    else
+    {
+        rt_strncpy(name, AHT10_I2C_BUS_NAME, RT_NAME_MAX);
+    }
+
+    if (!initialized)
+    {
+        /* 传感器初始化 */
+        aht10_init(name);
+    }
+    if (initialized)
+    {
+        /* 读取温湿度数据 */
+        read_temp_humi(&temperature, &humidity);
+
+        rt_kprintf("read aht10 sensor humidity   : %d.%d %%\n", (int)humidity, (int)(humidity * 10) % 10);
+        rt_kprintf("read aht10 sensor temperature: %d.%d \n", (int)temperature, (int)(temperature * 10) % 10);
+    }
+    else
+    {
+        rt_kprintf("initialize sensor failed!\n");
+    }
+}
+/* 导出到 msh 命令列表中 */
+MSH_CMD_EXPORT(i2c_aht10_sample, i2c aht10 sample);

+ 6 - 6
iwdg_sample.c

@@ -7,7 +7,7 @@
  * Date           Author       Notes
  * Date           Author       Notes
  * 2018-09-25     misonyo      first edition.
  * 2018-09-25     misonyo      first edition.
  */
  */
-/* 
+/*
  * 程序清单:这是一个独立看门狗设备使用例程
  * 程序清单:这是一个独立看门狗设备使用例程
  * 例程导出了 iwdg_sample 命令到控制终端
  * 例程导出了 iwdg_sample 命令到控制终端
  * 命令调用格式:iwdg_sample iwg
  * 命令调用格式:iwdg_sample iwg
@@ -29,7 +29,7 @@ static void idle_hook(void)
     rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
     rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
 }
 }
 
 
-static int iwdg_sample(int argc,char *argv[])
+static int iwdg_sample(int argc, char *argv[])
 {
 {
     rt_err_t result = RT_EOK;
     rt_err_t result = RT_EOK;
     rt_uint32_t timeout = 1000;    /* 超时时间为1000ms*/
     rt_uint32_t timeout = 1000;    /* 超时时间为1000ms*/
@@ -48,21 +48,21 @@ static int iwdg_sample(int argc,char *argv[])
     wdg_dev = rt_device_find(device_name);
     wdg_dev = rt_device_find(device_name);
     if (!wdg_dev)
     if (!wdg_dev)
     {
     {
-        rt_kprintf("find %s failed!\n",device_name);
+        rt_kprintf("find %s failed!\n", device_name);
         return RT_ERROR;
         return RT_ERROR;
     }
     }
     /* 初始化设备 */
     /* 初始化设备 */
     result = rt_device_init(wdg_dev);
     result = rt_device_init(wdg_dev);
     if (result != RT_EOK)
     if (result != RT_EOK)
     {
     {
-        rt_kprintf("initialize %s failed!\n",device_name);
+        rt_kprintf("initialize %s failed!\n", device_name);
         return RT_ERROR;
         return RT_ERROR;
     }
     }
     /* 设置看门狗溢出时间 */
     /* 设置看门狗溢出时间 */
-    result = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, (void*)timeout);
+    result = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, (void *)timeout);
     if (result != RT_EOK)
     if (result != RT_EOK)
     {
     {
-        rt_kprintf("set %s timeout failed!\n",device_name);
+        rt_kprintf("set %s timeout failed!\n", device_name);
         return RT_ERROR;
         return RT_ERROR;
     }
     }
     /* 设置空闲线程回调函数 */
     /* 设置空闲线程回调函数 */

+ 3 - 3
led_blink_sample.c

@@ -7,7 +7,7 @@
  * Date           Author       Notes
  * Date           Author       Notes
  * 2018-09-25     misonyo      first edition.
  * 2018-09-25     misonyo      first edition.
  */
  */
-/* 
+/*
  * 程序清单:这是一个通过PIN脚控制LED亮灭的使用例程
  * 程序清单:这是一个通过PIN脚控制LED亮灭的使用例程
  * 例程导出了 led_sample 命令到控制终端
  * 例程导出了 led_sample 命令到控制终端
  * 命令调用格式:led_sample 41
  * 命令调用格式:led_sample 41
@@ -47,7 +47,7 @@ static void led_entry(void *parameter)
     }
     }
 }
 }
 
 
-static void led_sample(int argc,char *argv[])
+static void led_sample(int argc, char *argv[])
 {
 {
     rt_thread_t tid;
     rt_thread_t tid;
 
 
@@ -65,7 +65,7 @@ static void led_sample(int argc,char *argv[])
                            led_entry,
                            led_entry,
                            RT_NULL,
                            RT_NULL,
                            512,
                            512,
-                           RT_THREAD_PRIORITY_MAX/3,
+                           RT_THREAD_PRIORITY_MAX / 3,
                            20);
                            20);
     if (tid != RT_NULL)
     if (tid != RT_NULL)
     {
     {

+ 67 - 67
pin_beep_sample.c

@@ -1,67 +1,67 @@
-/*
- * 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.
- */
-/* 
- * 程序清单:这是一个 PIN 设备使用例程
- * 例程导出了 pin_beep_sample 命令到控制终端
- * 命令调用格式:pin_beep_sample
- * 程序功能:通过按键控制蜂鸣器对应引脚的电平状态控制蜂鸣器
-*/
-
-#include <rtthread.h>
-#include <rtdevice.h>
-
-/* 引脚编号,通过查看驱动文件drv_gpio.c确定 */
-#ifndef BEEP_PIN_NUM
-#define BEEP_PIN_NUM            35  /* PB0 */
-#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_beep_sample(void)
-{
-    /* 蜂鸣器引脚为输出模式 */
-    rt_pin_mode(BEEP_PIN_NUM, PIN_MODE_OUTPUT);
-    /* 默认低电平 */
-    rt_pin_write(BEEP_PIN_NUM, PIN_LOW);
-    
-    /* 按键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);
-}
-/* 导出到 msh 命令列表中 */
-MSH_CMD_EXPORT(pin_beep_sample, pin beep sample);
+/*
+ * 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.
+ */
+/*
+ * 程序清单:这是一个 PIN 设备使用例程
+ * 例程导出了 pin_beep_sample 命令到控制终端
+ * 命令调用格式:pin_beep_sample
+ * 程序功能:通过按键控制蜂鸣器对应引脚的电平状态控制蜂鸣器
+*/
+
+#include <rtthread.h>
+#include <rtdevice.h>
+
+/* 引脚编号,通过查看驱动文件drv_gpio.c确定 */
+#ifndef BEEP_PIN_NUM
+    #define BEEP_PIN_NUM            35  /* PB0 */
+#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_beep_sample(void)
+{
+    /* 蜂鸣器引脚为输出模式 */
+    rt_pin_mode(BEEP_PIN_NUM, PIN_MODE_OUTPUT);
+    /* 默认低电平 */
+    rt_pin_write(BEEP_PIN_NUM, PIN_LOW);
+
+    /* 按键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);
+}
+/* 导出到 msh 命令列表中 */
+MSH_CMD_EXPORT(pin_beep_sample, pin beep sample);

+ 81 - 0
pwm_led_sample.c

@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2018-11-25     misonyo      first implementation.
+ */
+/*
+ * 程序清单:这是一个 PWM 设备使用例程
+ * 例程导出了 pwm_led_sample 命令到控制终端
+ * 命令调用格式:pwm_led_sample
+ * 程序功能:通过 PWM 设备控制 LED 灯的亮度,可以看到LED不停的由暗变到亮,然后又从亮变到暗。
+*/
+
+#include <rtthread.h>
+#include <rtdevice.h>
+
+/* LED PIN脚编号,查看驱动文件drv_gpio.c确定 */
+#define LED_PIN_NUM         57
+#define PWM_DEV_NAME        "pwm3"
+#define PWM_DEV_CHANNEL     4
+
+static void pwm_led_entry(void *parameter)
+{
+    rt_uint32_t period, pulse, dir;
+    struct rt_device_pwm *pwm_dev;
+    /* 设置LED引脚脚模式为输出 */
+    rt_pin_mode(LED_PIN_NUM, PIN_MODE_OUTPUT);
+    /* 拉高LED引脚 */
+    rt_pin_write(LED_PIN_NUM, PIN_HIGH);
+
+    pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
+
+    rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
+
+    period = 500000;    /* 0.5ms周期,单位为纳秒ns */
+    dir = 1;            /* PWM脉冲宽度值的增减方向 */
+    pulse = 0;          /* PWM脉冲宽度值,单位为纳秒ns */
+    while (1)
+    {
+        rt_thread_mdelay(50);
+        if (dir)
+        {
+            pulse += 5000;      /* 从0值开始每次增加5000ns */
+        }
+        else
+        {
+            pulse -= 5000;      /* 从最大值开始每次减少5000ns */
+        }
+        if (pulse >= 500000)
+        {
+            dir = 0;
+        }
+        if (0 == pulse)
+        {
+            dir = 1;
+        }
+
+        rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
+    }
+}
+
+static void pwm_led_sample(int argc, char *argv[])
+{
+    rt_thread_t tid;
+
+    tid = rt_thread_create("pwm",
+                           pwm_led_entry,
+                           RT_NULL,
+                           512,
+                           RT_THREAD_PRIORITY_MAX / 3,
+                           20);
+    if (tid != RT_NULL)
+    {
+        rt_thread_startup(tid);
+    }
+}
+/* 导出到 msh 命令列表中 */
+MSH_CMD_EXPORT(pwm_led_sample, pwm sample);

+ 11 - 11
sd_sample.c

@@ -7,7 +7,7 @@
  * Date           Author       Notes
  * Date           Author       Notes
  * 2018-09-25     misonyo      first edition.
  * 2018-09-25     misonyo      first edition.
  */
  */
-/* 
+/*
  * 程序清单:这是一个通过SD卡设备的使用例程
  * 程序清单:这是一个通过SD卡设备的使用例程
  * 例程导出了 sd_sample 命令到控制终端
  * 例程导出了 sd_sample 命令到控制终端
  * 命令调用格式:sd_sample sd0
  * 命令调用格式:sd_sample sd0
@@ -37,7 +37,7 @@ static rt_err_t sd_sample(int argc, char *argv[])
     rt_err_t result;
     rt_err_t result;
     rt_device_t sd_device;
     rt_device_t sd_device;
     char sd_name[RT_NAME_MAX];
     char sd_name[RT_NAME_MAX];
-    rt_uint8_t* write_buff, *read_buff;
+    rt_uint8_t *write_buff, *read_buff;
     struct rt_device_blk_geometry geo;
     struct rt_device_blk_geometry geo;
     rt_uint8_t block_num;
     rt_uint8_t block_num;
     /* 判断命令行参数是否给定了设备名称 */
     /* 判断命令行参数是否给定了设备名称 */
@@ -48,7 +48,7 @@ static rt_err_t sd_sample(int argc, char *argv[])
     else
     else
     {
     {
         rt_strncpy(sd_name, SD_DEVICE_NAME, RT_NAME_MAX);
         rt_strncpy(sd_name, SD_DEVICE_NAME, RT_NAME_MAX);
-    }    
+    }
     /* 查找设备获取设备句柄 */
     /* 查找设备获取设备句柄 */
     sd_device = rt_device_find(sd_name);
     sd_device = rt_device_find(sd_name);
     if (sd_device == RT_NULL)
     if (sd_device == RT_NULL)
@@ -58,7 +58,7 @@ static rt_err_t sd_sample(int argc, char *argv[])
     }
     }
     /* 打开设备 */
     /* 打开设备 */
     result = rt_device_open(sd_device, RT_DEVICE_OFLAG_RDWR);
     result = rt_device_open(sd_device, RT_DEVICE_OFLAG_RDWR);
-    if( result != RT_EOK )
+    if (result != RT_EOK)
     {
     {
         rt_kprintf("open device %s failed!\n", sd_name);
         rt_kprintf("open device %s failed!\n", sd_name);
         return result;
         return result;
@@ -67,7 +67,7 @@ static rt_err_t sd_sample(int argc, char *argv[])
     rt_memset(&geo, 0, sizeof(geo));
     rt_memset(&geo, 0, sizeof(geo));
     /* 获取块设备信息 */
     /* 获取块设备信息 */
     result = rt_device_control(sd_device, RT_DEVICE_CTRL_BLK_GETGEOME, &geo);
     result = rt_device_control(sd_device, RT_DEVICE_CTRL_BLK_GETGEOME, &geo);
-     if( result != RT_EOK )
+    if (result != RT_EOK)
     {
     {
         rt_kprintf("control device %s failed!\n", sd_name);
         rt_kprintf("control device %s failed!\n", sd_name);
         return result;
         return result;
@@ -78,13 +78,13 @@ static rt_err_t sd_sample(int argc, char *argv[])
     rt_kprintf("block   size : %d byte\n", geo.block_size);
     rt_kprintf("block   size : %d byte\n", geo.block_size);
     /* 准备读写缓冲区空间,大小为一个块 */
     /* 准备读写缓冲区空间,大小为一个块 */
     read_buff = rt_malloc(geo.block_size);
     read_buff = rt_malloc(geo.block_size);
-    if( read_buff == RT_NULL )
+    if (read_buff == RT_NULL)
     {
     {
         rt_kprintf("no memory for read buffer!\n");
         rt_kprintf("no memory for read buffer!\n");
         return RT_ERROR;
         return RT_ERROR;
     }
     }
     write_buff = rt_malloc(geo.block_size);
     write_buff = rt_malloc(geo.block_size);
-    if( write_buff == RT_NULL )
+    if (write_buff == RT_NULL)
     {
     {
         rt_kprintf("no memory for write buffer!\n");
         rt_kprintf("no memory for write buffer!\n");
         rt_free(read_buff);
         rt_free(read_buff);
@@ -98,14 +98,14 @@ static rt_err_t sd_sample(int argc, char *argv[])
     block_num = rt_device_write(sd_device, 0, write_buff, 1);
     block_num = rt_device_write(sd_device, 0, write_buff, 1);
     if (1 != block_num)
     if (1 != block_num)
     {
     {
-        rt_kprintf("write device %s failed!\n",sd_name);
+        rt_kprintf("write device %s failed!\n", sd_name);
     }
     }
-    
+
     /* 从SD卡中读出数据,并保存在读数据缓冲区中 */
     /* 从SD卡中读出数据,并保存在读数据缓冲区中 */
     block_num = rt_device_read(sd_device, 0, read_buff, 1);
     block_num = rt_device_read(sd_device, 0, read_buff, 1);
     if (1 != block_num)
     if (1 != block_num)
     {
     {
-        rt_kprintf("read %s device failed!\n",sd_name);
+        rt_kprintf("read %s device failed!\n", sd_name);
     }
     }
 
 
     /* 比较写数据缓冲区和读数据缓冲区的内容是否完全一致 */
     /* 比较写数据缓冲区和读数据缓冲区的内容是否完全一致 */
@@ -117,7 +117,7 @@ static rt_err_t sd_sample(int argc, char *argv[])
     {
     {
         rt_kprintf("Block test Fail!\n");
         rt_kprintf("Block test Fail!\n");
     }
     }
-   /* 释放缓冲区空间 */ 
+    /* 释放缓冲区空间 */
     rt_free(read_buff);
     rt_free(read_buff);
     rt_free(write_buff);
     rt_free(write_buff);
 
 

+ 3 - 3
spi_w25q_sample.c

@@ -7,7 +7,7 @@
  * Date           Author       Notes
  * Date           Author       Notes
  * 2018-08-15     misonyo      first implementation.
  * 2018-08-15     misonyo      first implementation.
  */
  */
-/* 
+/*
  * 程序清单:这是一个 SPI 设备使用例程
  * 程序清单:这是一个 SPI 设备使用例程
  * 例程导出了 spi_w25q_sample 命令到控制终端
  * 例程导出了 spi_w25q_sample 命令到控制终端
  * 命令调用格式:spi_w25q_sample spi10
  * 命令调用格式:spi_w25q_sample spi10
@@ -20,7 +20,7 @@
 
 
 #define W25Q_SPI_DEVICE_NAME     "qspi10"
 #define W25Q_SPI_DEVICE_NAME     "qspi10"
 
 
-static void spi_w25q_sample(int argc,char *argv[])
+static void spi_w25q_sample(int argc, char *argv[])
 {
 {
     struct rt_spi_device *spi_dev_w25q;
     struct rt_spi_device *spi_dev_w25q;
     char name[RT_NAME_MAX];
     char name[RT_NAME_MAX];
@@ -49,7 +49,7 @@ static void spi_w25q_sample(int argc,char *argv[])
         rt_kprintf("use rt_spi_send_then_recv() read w25q ID is:%x%x\n", id[3], id[4]);
         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 */
         /* 方式2:使用 rt_spi_transfer_message()发送命令读取ID */
-        struct rt_spi_message msg1,msg2;
+        struct rt_spi_message msg1, msg2;
 
 
         msg1.send_buf   = &w25x_read_id;
         msg1.send_buf   = &w25x_read_id;
         msg1.recv_buf   = RT_NULL;
         msg1.recv_buf   = RT_NULL;

+ 92 - 92
uart_sample.c

@@ -1,92 +1,92 @@
-/*
- * 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.
- */
-/* 
- * 程序清单:这是一个 串口 设备使用例程
- * 例程导出了 uart_sample 命令到控制终端
- * 命令调用格式:uart_sample uart2
- * 命令解释:命令第二个参数是要使用的串口设备名称,为空则使用默认的串口设备
- * 程序功能:通过串口输出字符串"hello RT-Thread!",然后错位输出输入的字符
-*/
-
-#include <rtthread.h>
-
-#define SAMPLE_UART_NAME       "uart2"
-
-/* 用于接收消息的信号量 */
-static struct rt_semaphore rx_sem;
-static char uart_name[RT_NAME_MAX];
-/* 接收数据回调函数 */
-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)
-{
-    char ch;
-    rt_device_t serial;
-    char str[] = "hello RT-Thread!\r\n";
-
-    /* 查找系统中的串口设备 */
-    serial = rt_device_find(uart_name);
-
-    if (serial != RT_NULL)
-    {
-        rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
-        /* 以读写及中断接收方式打开串口设备 */
-        rt_device_open(serial, RT_DEVICE_OFLAG_RDWR|RT_DEVICE_FLAG_INT_RX);
-        /* 设置接收回调函数 */
-        rt_device_set_rx_indicate(serial, uart_input);
-        /* 发送字符串 */
-        rt_device_write(serial, 0, str, (sizeof(str) - 1));
-        
-        while (1)
-        {
-            /* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */
-            while (rt_device_read(serial, -1, &ch, 1) != 1)
-            {
-                /* 阻塞等待接收信号量,等到信号量后再次读取数据 */
-                rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
-            }
-            /* 读取到的数据通过串口错位输出 */
-            ch = ch + 1;
-            rt_device_write(serial, 0, &ch, 1);
-        }
-    }
-    else
-    {
-        rt_kprintf("uart sample run failed! can't find %s device!\n",uart_name);
-    }
-}
-
-static void uart_sample(int argc,char *argv[])
-{
-    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_thread_t thread = rt_thread_create("serial",serial_thread_entry, RT_NULL, 1024, 25, 10);
-    /* 创建成功则启动线程 */
-    if (thread!= RT_NULL)
-    {
-        rt_thread_startup(thread);
-    }
-}
-/* 导出到 msh 命令列表中 */
-MSH_CMD_EXPORT(uart_sample, uart device sample);
+/*
+ * 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.
+ */
+/*
+ * 程序清单:这是一个 串口 设备使用例程
+ * 例程导出了 uart_sample 命令到控制终端
+ * 命令调用格式:uart_sample uart2
+ * 命令解释:命令第二个参数是要使用的串口设备名称,为空则使用默认的串口设备
+ * 程序功能:通过串口输出字符串"hello RT-Thread!",然后错位输出输入的字符
+*/
+
+#include <rtthread.h>
+
+#define SAMPLE_UART_NAME       "uart2"
+
+/* 用于接收消息的信号量 */
+static struct rt_semaphore rx_sem;
+static char uart_name[RT_NAME_MAX];
+/* 接收数据回调函数 */
+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)
+{
+    char ch;
+    rt_device_t serial;
+    char str[] = "hello RT-Thread!\r\n";
+
+    /* 查找系统中的串口设备 */
+    serial = rt_device_find(uart_name);
+
+    if (serial != RT_NULL)
+    {
+        rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
+        /* 以读写及中断接收方式打开串口设备 */
+        rt_device_open(serial, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
+        /* 设置接收回调函数 */
+        rt_device_set_rx_indicate(serial, uart_input);
+        /* 发送字符串 */
+        rt_device_write(serial, 0, str, (sizeof(str) - 1));
+
+        while (1)
+        {
+            /* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */
+            while (rt_device_read(serial, -1, &ch, 1) != 1)
+            {
+                /* 阻塞等待接收信号量,等到信号量后再次读取数据 */
+                rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
+            }
+            /* 读取到的数据通过串口错位输出 */
+            ch = ch + 1;
+            rt_device_write(serial, 0, &ch, 1);
+        }
+    }
+    else
+    {
+        rt_kprintf("uart sample run failed! can't find %s device!\n", uart_name);
+    }
+}
+
+static void uart_sample(int argc, char *argv[])
+{
+    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_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
+    /* 创建成功则启动线程 */
+    if (thread != RT_NULL)
+    {
+        rt_thread_startup(thread);
+    }
+}
+/* 导出到 msh 命令列表中 */
+MSH_CMD_EXPORT(uart_sample, uart device sample);