فهرست منبع

update samples

misonyo 7 سال پیش
والد
کامیت
41ff3e5abe
6فایلهای تغییر یافته به همراه51 افزوده شده و 58 حذف شده
  1. 6 5
      adc_vol_sample.c
  2. 5 5
      hwtimer_sample.c
  3. 10 2
      i2c_aht10_sample.c
  4. 4 3
      iwdg_sample.c
  5. 24 41
      pwm_led_sample.c
  6. 2 2
      uart_sample.c

+ 6 - 5
adc_vol_sample.c

@@ -8,17 +8,18 @@
  * 2018-11-29     misonyo      first implementation.
  */
 /*
- * 程序清单:这是一个 ADC 设备使用例程
+ * 程序清单: ADC 设备使用例程
  * 例程导出了 adc_sample 命令到控制终端
  * 命令调用格式:adc_sample
  * 程序功能:通过 ADC 设备采样电压值并转换为数值。
+ *           示例代码参考电压为3.3V,分辨率为12位。
 */
 
 #include <rtthread.h>
 #include <rtdevice.h>
 
-#define ADC_DEV_NAME        "adc1"
-#define ADC_DEV_CHANNEL     5
+#define ADC_DEV_NAME        "adc1"      /* ADC 设备名称 */
+#define ADC_DEV_CHANNEL     5           /* ADC 通道 */
 
 static int adc_vol_sample(int argc, char *argv[])
 {
@@ -37,7 +38,7 @@ static int adc_vol_sample(int argc, char *argv[])
     /* 使能设备 */
     ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL);
 
-    /* 读取数据 */
+    /* 读取采样值 */
     value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL);
     rt_kprintf("the value is :%d \n", value);
 
@@ -45,7 +46,7 @@ static int adc_vol_sample(int argc, char *argv[])
     vol = value * 330 / 4096;
     rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100);
 
-    /* 关闭设备 */
+    /* 关闭通道 */
     ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL);
 
     return ret;

+ 5 - 5
hwtimer_sample.c

@@ -11,13 +11,13 @@
  * 程序清单:这是一个 hwtimer 设备使用例程
  * 例程导出了 hwtimer_sample 命令到控制终端
  * 命令调用格式:hwtimer_sample
- * 程序功能:硬件定时器周期性的打印当前tick值,2次tick值之差也就是定时时间
+ * 程序功能:硬件定时器超时回调函数周期性的打印当前tick值,2次tick值之差换算为时间等同于定时时间值
 */
 
 #include <rtthread.h>
 #include <rtdevice.h>
 
-#define HWTIMER_DEV_NAME   "timer0"
+#define HWTIMER_DEV_NAME   "timer0"     /* 定时器名称 */
 
 /* 定时器超时回调函数 */
 static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
@@ -31,8 +31,8 @@ static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
 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_hwtimerval_t timeout_s;      /* 定时器超时值 */
+    rt_device_t hw_dev = RT_NULL;   /* 定时器设备句柄 */
     rt_hwtimer_mode_t mode;         /* 定时器模式 */
     rt_uint32_t freq = 10000;       /* 计数频率 */
 
@@ -44,7 +44,7 @@ static int hwtimer_sample(int argc, char *argv[])
         return RT_ERROR;
     }
 
-    /* 打开设备 */
+    /* 以读写方式打开设备 */
     ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
     if (ret != RT_EOK)
     {

+ 10 - 2
i2c_aht10_sample.c

@@ -24,8 +24,8 @@
 #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 struct rt_i2c_bus_device *i2c_bus = RT_NULL;     /* I2C总线设备句柄 */
+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)
@@ -44,9 +44,13 @@ static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint
 
     /* 调用I2C设备接口传输数据 */
     if (rt_i2c_transfer(bus, &msgs, 1) == 1)
+    {
         return RT_EOK;
+    }
     else
+    {
         return -RT_ERROR;
+    }
 }
 
 /* 读传感器寄存器数据 */
@@ -61,9 +65,13 @@ static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint8_t len, rt_uint
 
     /* 调用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)

+ 4 - 3
iwdg_sample.c

@@ -19,14 +19,15 @@
 #include <rtthread.h>
 #include <rtdevice.h>
 
-#define IWDG_DEVICE_NAME    "iwg"
+#define IWDG_DEVICE_NAME    "iwg"    /* 看门狗设备名称 */
 
-static rt_device_t wdg_dev;
+static rt_device_t wdg_dev;         /* 看门狗设备句柄 */
 
 static void idle_hook(void)
 {
     /* 在空闲线程的回调函数里喂狗 */
     rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
+    rt_kprintf("feed the dog!\n ");
 }
 
 static int iwdg_sample(int argc, char *argv[])
@@ -59,7 +60,7 @@ static int iwdg_sample(int argc, char *argv[])
         return RT_ERROR;
     }
     /* 设置看门狗溢出时间 */
-    ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, (void *)timeout);
+    ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
     if (ret != RT_EOK)
     {
         rt_kprintf("set %s timeout failed!\n", device_name);

+ 24 - 41
pwm_led_sample.c

@@ -17,14 +17,13 @@
 #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
+#define LED_PIN_NUM         57      /* LED PIN脚编号,查看驱动文件drv_gpio.c确定 */
+#define PWM_DEV_NAME        "pwm3"  /* PWM设备名称 */
+#define PWM_DEV_CHANNEL     4       /* PWM通道 */
 
-struct rt_device_pwm *pwm_dev;
+struct rt_device_pwm *pwm_dev;      /* PWM设备句柄 */
 
-static void pwm_led_entry(void *parameter)
+static int pwm_led_sample(int argc, char *argv[])
 {
     rt_uint32_t period, pulse, dir;
 
@@ -32,6 +31,24 @@ static void pwm_led_entry(void *parameter)
     dir = 1;            /* PWM脉冲宽度值的增减方向 */
     pulse = 0;          /* PWM脉冲宽度值,单位为纳秒ns */
 
+    /* 设置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);
+    if (pwm_dev == RT_NULL)
+    {
+        rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
+        return RT_ERROR;
+    }
+
+    /* 设置PWM周期和脉冲宽度默认值 */
+    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
+    /* 使能设备 */
+    rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
+    
     while (1)
     {
         rt_thread_mdelay(50);
@@ -56,39 +73,5 @@ static void pwm_led_entry(void *parameter)
         rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
     }
 }
-
-static int pwm_led_sample(int argc, char *argv[])
-{
-    rt_thread_t tid;
-
-    /* 设置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);
-    if (pwm_dev == RT_NULL)
-    {
-        rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
-        return RT_ERROR;
-    }
-
-    /* 使能设备 */
-    rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
-
-    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);
-    }
-
-    return RT_EOK;
-}
 /* 导出到 msh 命令列表中 */
-MSH_CMD_EXPORT(pwm_led_sample, pwm sample);
+MSH_CMD_EXPORT(pwm_led_sample, pwm sample);

+ 2 - 2
uart_sample.c

@@ -17,7 +17,7 @@
 
 #include <rtthread.h>
 
-#define SAMPLE_UART_NAME       "uart2"
+#define SAMPLE_UART_NAME       "uart2"      /* 串口设备名称 */
 
 /* 用于接收消息的信号量 */
 static struct rt_semaphore rx_sem;
@@ -65,7 +65,7 @@ static int uart_sample(int argc, char *argv[])
         rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
     }
 
-    /* 查找系统中的串口设备 */
+    /* 查找串口设备 */
     serial = rt_device_find(uart_name);
     if (!serial)
     {