format es32f06543
This commit is contained in:
parent
ee27b0cf3e
commit
421867f49e
|
@ -0,0 +1,4 @@
|
|||
# files format check exclude path, please follow the instructions below to modify;
|
||||
|
||||
dir_path:
|
||||
- libraries
|
|
@ -33,11 +33,11 @@
|
|||
/* GPIO 配置 */
|
||||
|
||||
typedef struct {
|
||||
uint8_t pin;
|
||||
uint8_t pin_mode;
|
||||
uint8_t pin_level;
|
||||
uint8_t irq_en;
|
||||
uint8_t irq_mode;
|
||||
uint8_t pin;
|
||||
uint8_t pin_mode;
|
||||
uint8_t pin_level;
|
||||
uint8_t irq_en;
|
||||
uint8_t irq_mode;
|
||||
void (*callback)(void *arg);
|
||||
} gpio_conf_t;
|
||||
|
||||
|
|
|
@ -55,7 +55,7 @@ void NVIC_Configuration(void)
|
|||
*******************************************************************************/
|
||||
void SystemClock_Config(void)
|
||||
{
|
||||
SYSCFG_UNLOCK();
|
||||
SYSCFG_UNLOCK();
|
||||
#if ES_CMU_LRC_EN
|
||||
SET_BIT(CMU->CLKENR, CMU_CLKENR_LRCEN_MSK);
|
||||
#else
|
||||
|
@ -80,7 +80,7 @@ void SystemClock_Config(void)
|
|||
CLEAR_BIT(CMU->CLKENR, CMU_CLKENR_HOSCEN_MSK);
|
||||
#endif /*ES_CMU_HOSC_EN*/
|
||||
|
||||
SYSCFG_LOCK();
|
||||
SYSCFG_LOCK();
|
||||
|
||||
#if ES_CMU_PLL1_EN
|
||||
/*PLL的源必须是4M*/
|
||||
|
|
|
@ -1,52 +0,0 @@
|
|||
# Prerequisites
|
||||
*.d
|
||||
|
||||
# Object files
|
||||
*.o
|
||||
*.ko
|
||||
*.obj
|
||||
*.elf
|
||||
|
||||
# Linker output
|
||||
*.ilk
|
||||
*.map
|
||||
*.exp
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Libraries
|
||||
*.lib
|
||||
*.a
|
||||
*.la
|
||||
*.lo
|
||||
|
||||
# Shared objects (inc. Windows DLLs)
|
||||
*.dll
|
||||
*.so
|
||||
*.so.*
|
||||
*.dylib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
*.i*86
|
||||
*.x86_64
|
||||
*.hex
|
||||
|
||||
# Debug files
|
||||
*.dSYM/
|
||||
*.su
|
||||
*.idb
|
||||
*.pdb
|
||||
|
||||
# Kernel Module Compile Results
|
||||
*.mod*
|
||||
*.cmd
|
||||
.tmp_versions/
|
||||
modules.order
|
||||
Module.symvers
|
||||
Mkfile.old
|
||||
dkms.conf
|
|
@ -1,47 +0,0 @@
|
|||
# 外设驱动测试用例
|
||||
|
||||
## 1、介绍
|
||||
|
||||
这个软件包包含一些外设设备操作的例程。
|
||||
|
||||
### 1.1 例程说明
|
||||
|
||||
| 文件 | 说明 |
|
||||
| ---------------- | ------------------------------- |
|
||||
| adc_vol_sample.c | 使用 ADC 设备转换电压数据 |
|
||||
| can_sample.c | 通过 CAN 设备发送一帧,并创建一个线程接收数据然后打印输出。 |
|
||||
| hwtimer_sample.c | 使用 硬件定时器定时 |
|
||||
| i2c_sample.c | 使用 i2c 设备进行读写 |
|
||||
| pm.c | 反复进入不同程度的睡眠。 |
|
||||
| led_blink_sample.c | 使用 pin 设备控制 LED 闪烁 |
|
||||
| pin_beep_sample.c | 使用 pin 设备控制蜂鸣器 |
|
||||
| pwm_led_sample.c | 使用 pwm 设备控制 LED 的亮度 |
|
||||
| rtc_sample.c | 使用 rtc 设备设置年月日时分秒信息 |
|
||||
| spi_sample.c | 使用 spi 设备进行读写 |
|
||||
| uart_sample.c | 使用 serial 设备中断接收及轮询发送模式收发数据 |
|
||||
|
||||
### 1.2 依赖
|
||||
|
||||
依赖设备管理模块提供的设备驱动。
|
||||
|
||||
## 2、如何打开 外设驱动测试用例
|
||||
|
||||
使用 外设驱动测试用例 需要在 RT-Thread 的menuconfig中选择它,具体路径如下:
|
||||
|
||||
```
|
||||
Hardware Driver Config --->
|
||||
Peripheral Driver test example--->
|
||||
```
|
||||
|
||||
## 3、使用 外设驱动测试用例
|
||||
|
||||
在打开 Peripheral Driver test example 后,当进行 BSP 编译时,选择的软件包相关源代码会被加入到 BSP 工程中进行编译。
|
||||
|
||||
## 4、注意事项
|
||||
|
||||
暂无。
|
||||
|
||||
## 5、联系方式 & 感谢
|
||||
|
||||
* 维护:[misonyo](https://github.com/misonyo)
|
||||
* 主页:https://github.com/RT-Thread-packages/peripheral-sample
|
|
@ -1,62 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-11-29 misonyo first implementation.
|
||||
*/
|
||||
/*
|
||||
* 程序清单: ADC 设备使用例程
|
||||
* 例程导出了 adc_sample 命令到控制终端
|
||||
* 命令调用格式:adc_sample
|
||||
* 程序功能:通过 ADC 设备采样电压值并转换为数值。
|
||||
* 示例代码参考电压为3.3V,转换位数为12位。
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
|
||||
#ifdef RT_USING_ADC
|
||||
|
||||
#define ADC_DEV_NAME "adc0" /* ADC 设备名称 */
|
||||
#define ADC_DEV_CHANNEL 5 /* PA1 ADC 通道 */
|
||||
#define REFER_VOLTAGE 330 /* 参考电压 3.3V,数据精度乘以100保留2位小数*/
|
||||
#define CONVERT_BITS (1 << 12) /* 转换位数为12位 */
|
||||
|
||||
static int adc_vol_sample(int argc, char *argv[])
|
||||
{
|
||||
rt_adc_device_t adc_dev;
|
||||
rt_uint32_t value, vol;
|
||||
rt_err_t ret = RT_EOK;
|
||||
|
||||
/* 查找设备 */
|
||||
adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
|
||||
if (adc_dev == RT_NULL)
|
||||
{
|
||||
rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
|
||||
return RT_ERROR;
|
||||
}
|
||||
|
||||
/* 使能设备 */
|
||||
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);
|
||||
|
||||
/* 转换为对应电压值 */
|
||||
vol = value * REFER_VOLTAGE / CONVERT_BITS;
|
||||
rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100);
|
||||
|
||||
/* 关闭通道 */
|
||||
ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL);
|
||||
|
||||
return ret;
|
||||
}
|
||||
/* 导出到 msh 命令列表中 */
|
||||
MSH_CMD_EXPORT(adc_vol_sample, adc voltage convert sample);
|
||||
|
||||
#endif
|
|
@ -1,148 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2019-06-25 misonyo first implementation.
|
||||
*/
|
||||
/*
|
||||
* 程序清单:这是一个 CAN 设备使用例程
|
||||
* 例程导出了 can_sample 命令到控制终端
|
||||
* 命令调用格式:can_sample can2
|
||||
* 命令解释:命令第二个参数是要使用的 CAN 设备名称,为空则使用默认的 CAN 设备
|
||||
* 程序功能:通过 CAN 设备发送一帧,并创建一个线程接收数据然后打印输出。
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include "rtdevice.h"
|
||||
|
||||
#ifdef RT_USING_CAN
|
||||
|
||||
#define CAN_DEV_NAME "can0" /* CAN 设备名称 */
|
||||
|
||||
static struct rt_semaphore rx_sem; /* 用于接收消息的信号量 */
|
||||
static rt_device_t can_dev; /* CAN 设备句柄 */
|
||||
|
||||
/* 接收数据回调函数 */
|
||||
static rt_err_t can_rx_call(rt_device_t dev, rt_size_t size)
|
||||
{
|
||||
/* CAN 接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
|
||||
rt_sem_release(&rx_sem);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static void can_rx_thread(void *parameter)
|
||||
{
|
||||
int i;
|
||||
struct rt_can_msg rxmsg = {0};
|
||||
|
||||
/* 设置接收回调函数 */
|
||||
rt_device_set_rx_indicate(can_dev, can_rx_call);
|
||||
|
||||
#ifdef RT_CAN_USING_HDR
|
||||
|
||||
rt_err_t res;
|
||||
|
||||
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}; /* 一共有5个过滤表 */
|
||||
/* 设置硬件过滤表 */
|
||||
res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg);
|
||||
RT_ASSERT(res == RT_EOK);
|
||||
#endif
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* hdr值为-1,表示直接从uselist链表读取数据 */
|
||||
rxmsg.hdr = -1;
|
||||
/* 阻塞等待接收信号量 */
|
||||
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
|
||||
/* 从CAN读取一帧数据 */
|
||||
rt_device_read(can_dev, 0, &rxmsg, sizeof(rxmsg));
|
||||
/* 打印数据ID及内容 */
|
||||
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;
|
||||
}
|
||||
|
||||
/* 初始化CAN接收信号量 */
|
||||
rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
|
||||
|
||||
/* 以中断接收及发送方式打开CAN设备 */
|
||||
res = rt_device_open(can_dev, RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_INT_RX);
|
||||
RT_ASSERT(res == RT_EOK);
|
||||
|
||||
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; /* ID为0x78 */
|
||||
msg.ide = RT_CAN_STDID; /* 标准格式 */
|
||||
msg.rtr = RT_CAN_DTR; /* 数据帧 */
|
||||
msg.len = 8; /* 数据长度为8 */
|
||||
/* 待发送的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;
|
||||
/* 发送一帧CAN数据 */
|
||||
size = rt_device_write(can_dev, 0, &msg, sizeof(msg));
|
||||
if (size == 0)
|
||||
{
|
||||
rt_kprintf("can dev write data failed!\n");
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
/* 导出到 msh 命令列表中 */
|
||||
MSH_CMD_EXPORT(can_sample, can device sample);
|
||||
|
||||
#endif
|
|
@ -1,89 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-11-30 misonyo first implementation.
|
||||
*/
|
||||
/*
|
||||
* 程序清单:这是一个 hwtimer 设备使用例程
|
||||
* 例程导出了 hwtimer_sample 命令到控制终端
|
||||
* 命令调用格式:hwtimer_sample
|
||||
* 程序功能:硬件定时器超时回调函数周期性的打印当前tick值,2次tick值之差换算为时间等同于定时时间值。
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#ifdef RT_USING_HWTIMER
|
||||
|
||||
#define HWTIMER_DEV_NAME "timer1" /* 定时器名称 */
|
||||
|
||||
/* 定时器超时回调函数 */
|
||||
static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
/* 设置定时器超时值为5s并启动定时器 */
|
||||
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;
|
||||
}
|
||||
|
||||
/* 延时3500ms */
|
||||
rt_thread_mdelay(3500);
|
||||
|
||||
/* 读取定时器当前值 */
|
||||
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 命令列表中 */
|
||||
MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);
|
||||
|
||||
#endif
|
|
@ -1,109 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2020-12-15 liuhy first implementation.
|
||||
*/
|
||||
/*
|
||||
* 程序清单:这是一个 I2C 设备使用例程
|
||||
* 例程导出了 i2c_e2_sample 命令到控制终端
|
||||
* 命令调用格式:i2c_e2_sample
|
||||
* 命令解释:使用默认的I2C总线设备i2c0
|
||||
* 程序功能:通过 I2C 设备写读e2prom,ST24C04WP。
|
||||
*/
|
||||
|
||||
/*ST24C04WP 有2个Block :Block0 的从机地址为:0x50,Block1 的从机地址为:0x51
|
||||
一个Block有 256字节,一页16字节,写只可在一页内(超过一页的范围后,会回到页的开始),读无页限制*/
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#ifdef RT_USING_I2C
|
||||
|
||||
#define I2C_BUS_NAME "i2c0" /*I2C总线设备名称 */
|
||||
#define SLAVE_ADDR 0x50 /*从机地址*/
|
||||
#define MEM_ADDR 0x00 /*从机的起始储存地址,范围:0x00到0xEF(例程写读范围:2页)*/
|
||||
#define ADDR_LEN 1 /*定义从机储存地址的长度,默认8位,1字节*/
|
||||
#define STR_LEN 16 /*接收发送的页数据长度 ,最大16*/
|
||||
|
||||
static rt_uint8_t mem_addr,rx_buffer[33] = { 0U }; /*读两页,需要32字节,字符串结束'\0'*/
|
||||
/*第一个字节' '用来放 E2PROM 的内存地址,最后一个字节'\0'作为子串的结束,不存入e2prom*/
|
||||
static rt_uint8_t tx_buffer1[STR_LEN + ADDR_LEN + 1] = " e2prom example !\0";
|
||||
static rt_uint8_t tx_buffer2[STR_LEN + ADDR_LEN + 1] = " ABCDEFGH12345678\0";
|
||||
|
||||
static void i2c_e2_sample(int argc, char *argv[])
|
||||
{
|
||||
struct rt_i2c_bus_device *i2c_bus = RT_NULL; /* I2C总线设备句柄 */
|
||||
struct rt_i2c_msg i2c_msg[2]; /* I2C消息 */
|
||||
rt_size_t s_stat;
|
||||
|
||||
i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(I2C_BUS_NAME); /* 通过名字获取I2C总线设备的句柄 */
|
||||
|
||||
if( i2c_bus == RT_NULL)
|
||||
{
|
||||
rt_kprintf("can't find i2c device :%s !\n",I2C_BUS_NAME);
|
||||
return;
|
||||
}
|
||||
|
||||
/*写T24C04WP
|
||||
如果 (MEM_ADDR & 0x0F) + STR_LEN <= 16, 写的范围为:(MEM_ADDR , MEM_ADDR + STR_LEN )
|
||||
(0x10 + MEM_ADDR , 0x10 + MEM_ADDR + STR_LEN )
|
||||
如果 (MEM_ADDR & 0x0F) + STR_LEN > 16, 超出范围的部分会在页内循环写。*/
|
||||
tx_buffer1[0] = MEM_ADDR;
|
||||
|
||||
/*初始化消息*/
|
||||
i2c_msg[0].addr = SLAVE_ADDR; /* 从机地址 */
|
||||
i2c_msg[0].len = ADDR_LEN + STR_LEN ; /* 写入的长度,地址+数据 */
|
||||
i2c_msg[0].buf = tx_buffer1; /* 待写入第一段数据 */
|
||||
i2c_msg[0].flags = RT_I2C_WR; /* I2C写 */
|
||||
s_stat = rt_i2c_transfer(i2c_bus,i2c_msg,1); /* 写入第一段数据 */
|
||||
|
||||
if( s_stat == 1 )rt_kprintf("write successful. \nmessage: %s\n",&tx_buffer1[1]);
|
||||
else rt_kprintf("device %s write fail \n",I2C_BUS_NAME);
|
||||
|
||||
tx_buffer2[0] = MEM_ADDR + 0x10; /*加一页*/
|
||||
i2c_msg[0].buf = tx_buffer2; /* 待写入第二段数据 */
|
||||
s_stat = rt_i2c_transfer(i2c_bus,i2c_msg,1); /* 写入第二段数据 */
|
||||
|
||||
if( s_stat == 1 )rt_kprintf("write successful. \nmessage: %s\n",&tx_buffer2[1]);
|
||||
else rt_kprintf("device %s write fail \n",I2C_BUS_NAME);
|
||||
|
||||
/*读T24C04WP 读2页的数据。读数据需要2条消息:第一条消息:发送读取的地址。
|
||||
第二条消息:读取具体的数据。*/
|
||||
|
||||
mem_addr = MEM_ADDR & 0xF0; /*从页的开始读*/
|
||||
|
||||
i2c_msg[0].len = ADDR_LEN;
|
||||
i2c_msg[0].buf = &mem_addr;
|
||||
|
||||
i2c_msg[1].addr = SLAVE_ADDR; /* 从机地址 */
|
||||
i2c_msg[1].len = 32; /* 读取的数据长度:2*16 */
|
||||
i2c_msg[1].buf = rx_buffer; /* 数据存放地址 */
|
||||
i2c_msg[1].flags = RT_I2C_RD; /* I2C读 */
|
||||
s_stat = rt_i2c_transfer(i2c_bus,i2c_msg,2); /* 读已写的2页 */
|
||||
|
||||
if( s_stat == 2 )rt_kprintf(" read successful \n messege : %s \n",rx_buffer);
|
||||
else
|
||||
rt_kprintf("read fail \n");
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
/* 导出到 msh 命令列表中 */
|
||||
MSH_CMD_EXPORT(i2c_e2_sample, i2c e2prom sample);
|
||||
|
||||
#endif
|
|
@ -1,109 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2020-12-15 liuhy first implementation.
|
||||
*/
|
||||
/*
|
||||
* 程序清单:这是一个 I2C 设备使用例程
|
||||
* 例程导出了 i2c_io_sample 命令到控制终端
|
||||
* 命令调用格式:i2c_io_sample
|
||||
* 命令解释:使用默认的I2C总线设备i2c0
|
||||
* 程序功能:通过 I2C 设备接收数据并打印,然后将接收的字符加1输出。
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#ifdef RT_USING_I2C
|
||||
|
||||
#define I2C_BUS_NAME "i2c0" /* I2C总线设备名称 */
|
||||
#define SLAVE_ADDR 0x2D /* 从机地址 */
|
||||
#define STR_LEN 16 /* 接收发送的数据长度 */
|
||||
|
||||
static void i2c_io_sample(int argc, char *argv[])
|
||||
{
|
||||
|
||||
struct rt_i2c_bus_device *i2c_bus = RT_NULL; /* I2C总线设备句柄 */
|
||||
struct rt_i2c_msg temp_msg; /* I2C消息 */
|
||||
rt_uint8_t buffer[STR_LEN] = { 0U };
|
||||
rt_uint32_t i,num_msg;
|
||||
rt_size_t s_stat;
|
||||
|
||||
i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(I2C_BUS_NAME); /* 通过名字获取I2C总线设备的句柄 */
|
||||
|
||||
if( i2c_bus == RT_NULL)
|
||||
{
|
||||
rt_kprintf("can't find i2c device :%s !\n",I2C_BUS_NAME);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/*初始化消息*/
|
||||
temp_msg.addr = SLAVE_ADDR; /* 从机地址 */
|
||||
temp_msg.len = STR_LEN; /* 传输的数据长度 */
|
||||
temp_msg.buf = buffer; /* 读写缓存器 */
|
||||
|
||||
num_msg = 1; /* 传输一条消息 */
|
||||
|
||||
temp_msg.flags = RT_I2C_RD; /* I2C读 */
|
||||
s_stat = rt_i2c_transfer(i2c_bus,&temp_msg,num_msg); /* 传输消息 */
|
||||
|
||||
if( s_stat == num_msg )
|
||||
{
|
||||
rt_kprintf("receive successful. \n receive messege : %s \n:",buffer);
|
||||
|
||||
for( i = 0 ; i < STR_LEN ; i++)
|
||||
rt_kprintf(" %x",(unsigned int)buffer[i]);
|
||||
|
||||
rt_kprintf("\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("device s% recieve fail \n buffer : s%\n",I2C_BUS_NAME,buffer);
|
||||
return;
|
||||
}
|
||||
|
||||
for( i = 0 ; i < STR_LEN ; i++)
|
||||
buffer[i]++;
|
||||
|
||||
temp_msg.flags = RT_I2C_WR; /* I2C写 */
|
||||
s_stat = rt_i2c_transfer(i2c_bus,&temp_msg,num_msg); /* 传输一条 */
|
||||
|
||||
if( s_stat == num_msg )
|
||||
{
|
||||
rt_kprintf(" send successful \n messege : %s \n:",buffer);
|
||||
|
||||
for( i = 0 ; i < STR_LEN ; i++)
|
||||
rt_kprintf(" %x",(unsigned int)buffer[i]);
|
||||
|
||||
rt_kprintf("\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("device s% send fail \n",I2C_BUS_NAME);
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
/* 导出到 msh 命令列表中 */
|
||||
MSH_CMD_EXPORT(i2c_io_sample, i2c io sample);
|
||||
|
||||
#endif
|
|
@ -1,84 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-09-25 misonyo first edition.
|
||||
*/
|
||||
/*
|
||||
* 程序清单:这是一个通过PIN脚控制LED亮灭的使用例程
|
||||
* 例程导出了 led_sample 命令到控制终端
|
||||
* 命令调用格式:led_sample 41
|
||||
* 命令解释:命令第二个参数是要使用的PIN脚编号,为空则使用例程默认的引脚编号。
|
||||
* 程序功能:程序创建一个led线程,线程每隔1000ms改变PIN脚状态,达到控制led灯
|
||||
* 亮灭的效果。
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
#include <stdlib.h>
|
||||
#include "drv_gpio.h"
|
||||
|
||||
/* PIN脚编号,查看驱动文件drv_gpio.c确定 */
|
||||
#define LED_PIN_NUM GET_PIN(B,9) /*PB9*/
|
||||
static int pin_num;
|
||||
|
||||
static void led_entry(void *parameter)
|
||||
{
|
||||
int count = 0;
|
||||
/* 设置PIN脚模式为输出 */
|
||||
rt_pin_mode(pin_num, PIN_MODE_OUTPUT);
|
||||
|
||||
while (1)
|
||||
{
|
||||
count++;
|
||||
rt_kprintf("thread run count : %d\r\n", count);
|
||||
/* 拉低PIN脚 */
|
||||
rt_pin_write(pin_num, PIN_LOW);
|
||||
rt_kprintf("led on!\r\n");
|
||||
/* 延时1000ms */
|
||||
rt_thread_mdelay(1000);
|
||||
|
||||
/* 拉高PIN脚 */
|
||||
rt_pin_write(pin_num, PIN_HIGH);
|
||||
rt_kprintf("led off!\r\n");
|
||||
rt_thread_mdelay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
static int led_sample(int argc, char *argv[])
|
||||
{
|
||||
rt_thread_t tid;
|
||||
rt_err_t ret = RT_EOK;
|
||||
|
||||
/* 判断命令行参数是否给定了PIN脚编号 */
|
||||
if (argc == 2)
|
||||
{
|
||||
pin_num = atoi(argv[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
pin_num = LED_PIN_NUM;
|
||||
}
|
||||
|
||||
tid = rt_thread_create("led",
|
||||
led_entry,
|
||||
RT_NULL,
|
||||
512,
|
||||
RT_THREAD_PRIORITY_MAX / 3,
|
||||
20);
|
||||
if (tid != RT_NULL)
|
||||
{
|
||||
rt_thread_startup(tid);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = RT_ERROR;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
/* 导出到 msh 命令列表中 */
|
||||
MSH_CMD_EXPORT(led_sample, led sample);
|
|
@ -1,68 +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.
|
||||
*/
|
||||
/*
|
||||
* 程序清单:这是一个 PIN 设备使用例程
|
||||
* 例程导出了 pin_beep_sample 命令到控制终端
|
||||
* 命令调用格式:pin_beep_sample
|
||||
* 程序功能:通过按键控制蜂鸣器对应引脚的电平状态控制蜂鸣器
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
#include "drv_gpio.h"
|
||||
|
||||
/* 引脚编号,通过查看驱动文件drv_gpio.c确定 */
|
||||
#ifndef BEEP_PIN_NUM
|
||||
#define BEEP_PIN_NUM GET_PIN(B,9) /* PB9 */
|
||||
#endif
|
||||
#ifndef KEY0_PIN_NUM
|
||||
#define KEY0_PIN_NUM GET_PIN(F,0) /* PF0 */
|
||||
#endif
|
||||
#ifndef KEY1_PIN_NUM
|
||||
#define KEY1_PIN_NUM GET_PIN(F,1) /* PF1 */
|
||||
#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);
|
|
@ -1,211 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2020-12-15 liuhy first implementation.
|
||||
*/
|
||||
/*
|
||||
* 程序清单:这是一个 pm睡眠唤醒的使用例程
|
||||
* 例程导出了 pm_sample 命令到控制终端
|
||||
* 命令调用格式:pm_sample
|
||||
* 命令解释:进入不同的睡眠模式,然后用按键唤醒。
|
||||
* 程序功能:通过串口输出字符串,告知进入睡眠和唤醒睡眠的情况。
|
||||
* 注意:进入睡眠前,如果有中断挂起(SYSTICK、UART、EXTI等),睡眠将被瞬间唤醒。
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
#include "drv_gpio.h"
|
||||
|
||||
|
||||
#ifdef RT_USING_PM
|
||||
#define PM_NAME "pm" /* 设备名称 */
|
||||
#define WAKE_UP_PIN GET_PIN(F,0) /* 唤醒源 */
|
||||
#define SLEEP_TIMES 12 /* 进入睡眠次数,轮流进入不同的睡眠模式,包括无睡眠模式 */
|
||||
|
||||
|
||||
struct pm_callback_t
|
||||
{
|
||||
volatile int in_fun_times; /*进入函数的次数*/
|
||||
volatile char flag; /*标志*/
|
||||
volatile int mode; /*需要打印的模式*/
|
||||
};
|
||||
|
||||
volatile struct pm_callback_t g_pm_data;
|
||||
|
||||
/*进入睡眠前,睡眠唤醒后,都会进入。*/
|
||||
/*函数打印睡眠相关的信息*/
|
||||
void sleep_in_out_callback(rt_uint8_t event, rt_uint8_t mode, void *data)
|
||||
{
|
||||
/*没有标志,不处理*/
|
||||
if(!(g_pm_data.flag))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/*标志不正常,清空标志*/
|
||||
if((g_pm_data.flag) > 2)
|
||||
{
|
||||
(g_pm_data.flag) = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
/*模式不匹配*/
|
||||
if(g_pm_data.mode != mode )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/*进入的事件*/
|
||||
switch(event)
|
||||
{
|
||||
/*进入睡眠前*/
|
||||
case RT_PM_ENTER_SLEEP: g_pm_data.flag = 1;
|
||||
rt_kprintf("\n\r##%d : ENTER ",g_pm_data.in_fun_times);
|
||||
g_pm_data.in_fun_times++; /*进入睡眠次数+1*/
|
||||
break;
|
||||
/*睡眠唤醒后*/
|
||||
case RT_PM_EXIT_SLEEP: g_pm_data.flag = 0; /*睡眠唤醒后*/
|
||||
/*从深睡眠唤醒后,等待UART时钟未恢复稳定,输出可能丢失*/
|
||||
rt_kprintf("\n\rEXIT\n\r");
|
||||
rt_pm_request(PM_SLEEP_MODE_NONE); /*进无休眠模式*/
|
||||
return;
|
||||
|
||||
default: break;
|
||||
|
||||
};
|
||||
|
||||
/*当前的睡眠模式*/
|
||||
switch(mode)
|
||||
{
|
||||
case PM_SLEEP_MODE_NONE: rt_kprintf("PM_SLEEP_MODE_NONE\n\r");
|
||||
break;
|
||||
case PM_SLEEP_MODE_IDLE: rt_kprintf("PM_SLEEP_MODE_IDLE\n\r");
|
||||
break;
|
||||
case PM_SLEEP_MODE_LIGHT: rt_kprintf("PM_SLEEP_MODE_LIGHT\n\r");
|
||||
break;
|
||||
case PM_SLEEP_MODE_DEEP: rt_kprintf("PM_SLEEP_MODE_DEEP\n\r");
|
||||
break;
|
||||
case PM_SLEEP_MODE_STANDBY: rt_kprintf("PM_SLEEP_MODE_STANDBY\n\r");
|
||||
break;
|
||||
case PM_SLEEP_MODE_SHUTDOWN: rt_kprintf("PM_SLEEP_MODE_SHUTDOWN\n\r");
|
||||
break;
|
||||
case PM_SLEEP_MODE_MAX: rt_kprintf("PM_SLEEP_MODE_MAX\n\r");
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* pm测试函数 */
|
||||
static void pm_test(void *parameter)
|
||||
{
|
||||
int in_mode[7],i = 0;
|
||||
|
||||
g_pm_data.in_fun_times = 0;
|
||||
g_pm_data.flag = 0;
|
||||
|
||||
in_mode[0] = PM_SLEEP_MODE_NONE;
|
||||
in_mode[1] = PM_SLEEP_MODE_IDLE;
|
||||
in_mode[2] = PM_SLEEP_MODE_LIGHT;
|
||||
in_mode[3] = PM_SLEEP_MODE_DEEP;
|
||||
in_mode[4] = PM_SLEEP_MODE_STANDBY;
|
||||
in_mode[5] = PM_SLEEP_MODE_SHUTDOWN;
|
||||
in_mode[6] = PM_SLEEP_MODE_MAX;
|
||||
|
||||
/*设置回调函数和私有数据*/
|
||||
rt_pm_notify_set(sleep_in_out_callback,RT_NULL);
|
||||
|
||||
while(i < SLEEP_TIMES)
|
||||
{
|
||||
|
||||
g_pm_data.mode = in_mode[i%6];
|
||||
|
||||
/*无休眠模式,不赋予标志*/
|
||||
if(g_pm_data.mode != PM_SLEEP_MODE_NONE)
|
||||
{
|
||||
g_pm_data.flag = 2;
|
||||
|
||||
}
|
||||
|
||||
/*彻底释放无休眠模式*/
|
||||
rt_pm_release_all(PM_SLEEP_MODE_NONE);
|
||||
|
||||
/*请求选择的休眠模式*/
|
||||
rt_pm_request(in_mode[i%6]);
|
||||
|
||||
rt_thread_mdelay(500);
|
||||
|
||||
/*无休眠模式,不需要额外的等待*/
|
||||
while(( g_pm_data.flag != 0 )&&(g_pm_data.mode != PM_SLEEP_MODE_NONE))
|
||||
{
|
||||
rt_thread_mdelay(500);
|
||||
}
|
||||
|
||||
/*释放选择的休眠模式 ,彻底释放*/
|
||||
rt_pm_release_all(in_mode[i%6]);
|
||||
|
||||
i++;
|
||||
|
||||
}
|
||||
|
||||
/*切换为无睡眠模式*/
|
||||
rt_pm_request(PM_SLEEP_MODE_NONE);
|
||||
|
||||
/*清除回调函数和私有数据*/
|
||||
rt_pm_notify_set(RT_NULL,RT_NULL);
|
||||
|
||||
rt_kprintf("thread pm_test close\n\r");
|
||||
|
||||
}
|
||||
|
||||
/*按键唤醒的回调函数*/
|
||||
void wake_by_pin(void *args)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static int pm_sample(int argc, char *argv[])
|
||||
{
|
||||
rt_thread_t thread;
|
||||
|
||||
/* 按键引脚为输入模式 */
|
||||
rt_pin_mode(WAKE_UP_PIN, PIN_MODE_INPUT_PULLUP);
|
||||
|
||||
/* 绑定中断,下降沿模式,回调函数名为wake_by_pin */
|
||||
rt_pin_attach_irq(WAKE_UP_PIN, PIN_IRQ_MODE_RISING, wake_by_pin, RT_NULL);
|
||||
/* 使能中断 */
|
||||
rt_pin_irq_enable(WAKE_UP_PIN, PIN_IRQ_ENABLE);
|
||||
|
||||
thread = rt_thread_create("pm_test", pm_test, RT_NULL, 1024, 25, 10);
|
||||
|
||||
if (thread != RT_NULL)
|
||||
{
|
||||
rt_thread_startup(thread);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("create pm_test thread failed!\n\r");
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
/* 导出到 msh 命令列表中 */
|
||||
MSH_CMD_EXPORT(pm_sample, pm sample);
|
||||
|
||||
#endif
|
|
@ -1,78 +0,0 @@
|
|||
/*
|
||||
* 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>
|
||||
#include "drv_gpio.h"
|
||||
|
||||
#ifdef RT_USING_PWM
|
||||
|
||||
#define LED_PIN_NUM GET_PIN(A,2) /* LED PIN脚编号,查看驱动文件drv_gpio.c确定 */
|
||||
#define PWM_DEV_NAME "pwm1" /* PWM设备名称 */
|
||||
#define PWM_DEV_CHANNEL 1 /* PWM通道 */
|
||||
|
||||
struct rt_device_pwm *pwm_dev; /* PWM设备句柄 */
|
||||
|
||||
static int pwm_led_sample(int argc, char *argv[])
|
||||
{
|
||||
rt_uint32_t period, pulse, dir;
|
||||
|
||||
period = 500000; /* 周期为0.5ms,单位为纳秒ns */
|
||||
dir = 1; /* PWM脉冲宽度值的增减方向 */
|
||||
pulse = 0; /* PWM脉冲宽度值,单位为纳秒ns */
|
||||
|
||||
/* 查找设备 */
|
||||
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);
|
||||
if (dir)
|
||||
{
|
||||
pulse += 5000; /* 从0值开始每次增加5000ns */
|
||||
}
|
||||
else
|
||||
{
|
||||
pulse -= 5000; /* 从最大值开始每次减少5000ns */
|
||||
}
|
||||
if (pulse >= period)
|
||||
{
|
||||
dir = 0;
|
||||
}
|
||||
if (0 == pulse)
|
||||
{
|
||||
dir = 1;
|
||||
}
|
||||
|
||||
/* 设置PWM周期和脉冲宽度 */
|
||||
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
|
||||
}
|
||||
}
|
||||
/* 导出到 msh 命令列表中 */
|
||||
MSH_CMD_EXPORT(pwm_led_sample, pwm sample);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,55 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-11-30 misonyo first implementation.
|
||||
*/
|
||||
/*
|
||||
* 程序清单:这是一个 RTC 设备使用例程
|
||||
* 例程导出了 rtc_sample 命令到控制终端
|
||||
* 命令调用格式:rtc_sample
|
||||
* 程序功能:设置RTC设备的日期和时间,延时一段时间后获取当前时间并打印显示。
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#ifdef RT_USING_RTC
|
||||
|
||||
static int rtc_sample(int argc, char *argv[])
|
||||
{
|
||||
rt_err_t ret = RT_EOK;
|
||||
time_t now;
|
||||
|
||||
/* 设置日期 */
|
||||
ret = set_date(2018, 12, 3);
|
||||
if (ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("set RTC date failed\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* 设置时间 */
|
||||
ret = set_time(11, 15, 50);
|
||||
if (ret != RT_EOK)
|
||||
{
|
||||
rt_kprintf("set RTC time failed\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* 延时3秒 */
|
||||
rt_thread_mdelay(3000);
|
||||
|
||||
/* 获取时间 */
|
||||
now = time(RT_NULL);
|
||||
rt_kprintf("%s\n", ctime(&now));
|
||||
|
||||
return ret;
|
||||
}
|
||||
/* 导出到 msh 命令列表中 */
|
||||
MSH_CMD_EXPORT(rtc_sample, rtc sample);
|
||||
|
||||
#endif
|
|
@ -1,152 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2020-12-15 liuhy first implementation.
|
||||
*/
|
||||
/*
|
||||
* 程序清单:这是一个 SPI 设备使用例程
|
||||
* 例程导出了 spi_io_sample 命令到控制终端
|
||||
* 命令调用格式:spi_io_sample
|
||||
* 程序功能:通过SPI设备先读取数据,然后每个字符加1后输出。
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#ifdef RT_USING_SPI
|
||||
|
||||
#define SPI_DEVICE_NAME "spi00"
|
||||
#define BUF_LEN 16
|
||||
|
||||
static void spi_io_sample(int argc, char *argv[])
|
||||
{
|
||||
struct rt_spi_device * spi_dev; /* spi设备的句柄 */
|
||||
rt_uint8_t i,buffer[BUF_LEN] = { 0U };
|
||||
rt_err_t s_stat;
|
||||
rt_err_t result;
|
||||
|
||||
/* 查找 spi设备 获取spi设备句柄 */
|
||||
spi_dev = (struct rt_spi_device *)rt_device_find(SPI_DEVICE_NAME);
|
||||
|
||||
if (spi_dev == RT_NULL)
|
||||
{
|
||||
rt_kprintf("spi sample run failed! can't find %s device!\n", SPI_DEVICE_NAME);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 配置SPI设备 */
|
||||
s_stat = rt_spi_configure(spi_dev,&(spi_dev->config));
|
||||
|
||||
if(s_stat != RT_EOK)
|
||||
{
|
||||
rt_kprintf(" spi config fail !\n ");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/* 获取总线 ,防止总线被多个线程同时使用 */
|
||||
result = rt_spi_take_bus(spi_dev);
|
||||
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
rt_kprintf(" %s take spi bus failed! \n", SPI_DEVICE_NAME);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 选中片选 */
|
||||
result = rt_spi_take(spi_dev);
|
||||
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
rt_kprintf(" %s take spi cs failed! \n", SPI_DEVICE_NAME);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/*接收一次数据*/
|
||||
result = rt_spi_recv(spi_dev,buffer,BUF_LEN);
|
||||
|
||||
if(result != BUF_LEN)
|
||||
{
|
||||
rt_kprintf("receive fail. \n buffer is : %s \n:",buffer);
|
||||
|
||||
for( i = 0 ; i < BUF_LEN ; i++)
|
||||
rt_kprintf(" %x",(unsigned int)buffer[i]);
|
||||
|
||||
rt_kprintf("\n");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
rt_kprintf("receive successful. \n buffer is : %s \n:",buffer);
|
||||
|
||||
for( i = 0 ; i < BUF_LEN ; i++)
|
||||
rt_kprintf(" %x",(unsigned int)buffer[i]);
|
||||
|
||||
rt_kprintf("\n");
|
||||
|
||||
/* 将接收到的数据加1 */
|
||||
for( i = 0 ; i < BUF_LEN ; i++)
|
||||
buffer[i]++;
|
||||
|
||||
/*发送数据*/
|
||||
result = rt_spi_send(spi_dev,buffer,BUF_LEN);
|
||||
|
||||
if(result != BUF_LEN)
|
||||
{
|
||||
rt_kprintf("send fail. \n buffer is : %s \n:",buffer);
|
||||
|
||||
for( i = 0 ; i < BUF_LEN ; i++)
|
||||
rt_kprintf(" %x",(unsigned int)buffer[i]);
|
||||
|
||||
rt_kprintf("\n");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
rt_kprintf("send successful. \n buffer is : %s \n:",buffer);
|
||||
|
||||
for( i = 0 ; i < BUF_LEN ; i++)
|
||||
rt_kprintf(" %x",(unsigned int)buffer[i]);
|
||||
|
||||
rt_kprintf("\n");
|
||||
|
||||
/* 释放片选 */
|
||||
result = rt_spi_release(spi_dev);
|
||||
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
rt_kprintf(" %s release spi cs failed! \n", SPI_DEVICE_NAME);
|
||||
return;
|
||||
}
|
||||
|
||||
/* 释放总线 */
|
||||
result = rt_spi_release_bus(spi_dev);
|
||||
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
rt_kprintf(" %s release spi bus failed! \n", SPI_DEVICE_NAME);
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
/* 导出到 msh 命令列表中 */
|
||||
MSH_CMD_EXPORT(spi_io_sample, spi sample);
|
||||
|
||||
#endif
|
|
@ -1,149 +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.
|
||||
*/
|
||||
/*
|
||||
* 程序清单:这是一个 串口 设备使用例程
|
||||
* 例程导出了 uart_sample 命令到控制终端
|
||||
* 命令调用格式:uart_sample uart2
|
||||
* 命令解释:命令第二个参数是要使用的串口设备名称,为空则使用默认的串口设备
|
||||
* 程序功能:通过串口输出字符串"hello RT-Thread!",然后根据例子类型,处理输入数据,然后输出
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
/* UART_SAMPLE_TYPE = 1,错位输出
|
||||
= 2 接收到固定格式之后输出
|
||||
*/
|
||||
#define UART_SAMPLE_TYPE 1
|
||||
|
||||
#if (UART_SAMPLE_TYPE == 2)
|
||||
|
||||
#define SAMPLE_UART_RXBUF_SIZE 256 /* 接收缓存大小 */
|
||||
#define SAMPLE_UART_END_SRTING "\r\n" /* 结尾固定格式 */
|
||||
|
||||
#endif
|
||||
|
||||
#define SAMPLE_UART_NAME "uart0" /* 串口设备名称 */
|
||||
|
||||
/* 用于接收消息的信号量 */
|
||||
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)
|
||||
{
|
||||
#if (UART_SAMPLE_TYPE == 1)
|
||||
char ch;
|
||||
|
||||
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);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if (UART_SAMPLE_TYPE == 2)
|
||||
char rx_buf[SAMPLE_UART_RXBUF_SIZE],*end = SAMPLE_UART_END_SRTING; /*rx_buf[]:接收缓存,可修改大小 end[]:固定的结束格式(可修改,不可包含'\0')*/
|
||||
uint32_t rx_index = 0; /*接收数据的索引 */
|
||||
uint32_t end_len = rt_strlen(end); /*固定的结束格式的长度*/
|
||||
|
||||
while(1)
|
||||
{
|
||||
|
||||
/* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */
|
||||
while (rt_device_read(serial, -1, (rx_buf + rx_index), 1) != 1)
|
||||
{
|
||||
/* 阻塞等待接收信号量,等到信号量后再次读取数据 */
|
||||
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
|
||||
}
|
||||
|
||||
rx_index++;
|
||||
if(rx_index >= SAMPLE_UART_RXBUF_SIZE)
|
||||
{
|
||||
rt_kprintf("rx_buf over!\r\n"); /*范围越界*/
|
||||
}
|
||||
|
||||
/*判断是否固定的结尾格式*/
|
||||
if((rx_index >= end_len)&&\
|
||||
((rt_strncmp(end,(rx_buf + rx_index - end_len),end_len)) == 0))
|
||||
{
|
||||
rt_device_write(serial, 0, rx_buf, rx_index);
|
||||
rx_index = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
static int uart_sample(int argc, char *argv[])
|
||||
{
|
||||
rt_err_t ret = RT_EOK;
|
||||
char uart_name[RT_NAME_MAX];
|
||||
char str[] = "hello RT-Thread!\r\n";
|
||||
|
||||
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_INT_RX);
|
||||
/* 设置接收回调函数 */
|
||||
rt_device_set_rx_indicate(serial, uart_input);
|
||||
/* 发送字符串 */
|
||||
rt_device_write(serial, 0, str, (sizeof(str) - 1));
|
||||
|
||||
/* 创建 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);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = RT_ERROR;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
/* 导出到 msh 命令列表中 */
|
||||
MSH_CMD_EXPORT(uart_sample, uart device sample);
|
|
@ -35,18 +35,18 @@ static rt_uint32_t get_can_baud_index(rt_uint32_t baud,can_init_t * init)
|
|||
double target,temp,min;
|
||||
uint32_t i,j,j_max,near = 0;
|
||||
target = (double)(ald_cmu_get_pclk1_clock());
|
||||
target/= baud; /*计算误差1*/
|
||||
target/= baud; /*计算误差1*/
|
||||
|
||||
min = 0xFFFFFFFF;
|
||||
|
||||
for(i = 1 + 16 + 8 ;i > 2;i--) /*SYNC_SEG + SEG1 + SEG2*/
|
||||
{
|
||||
j_max = target/i/(0.98) + 1; /*缩小范围*/
|
||||
j_max = target/i/(0.98) + 1; /*缩小范围*/
|
||||
j_max = (j_max > 1024) ? (1024) : (j_max);
|
||||
|
||||
for(j = target/i/1.02 ;j < j_max;j++)
|
||||
{
|
||||
temp = target/i/j; /*计算误差2*/
|
||||
temp = target/i/j; /*计算误差2*/
|
||||
temp = (temp > 1) ? (temp - 1) : (1 - temp);
|
||||
temp+= ((1.0 * i * j) / 0xFFFFFFFF) ;
|
||||
|
||||
|
@ -118,7 +118,7 @@ static rt_err_t _can_config(struct rt_can_device *can_device, struct can_configu
|
|||
drv_can->CanHandle.init.mode = CAN_MODE_SILENT_LOOPBACK;
|
||||
break;
|
||||
}
|
||||
/*配置参数*/
|
||||
/*配置参数*/
|
||||
if(get_can_baud_index(cfg->baud_rate,&(drv_can->CanHandle.init)))
|
||||
{
|
||||
return -RT_ERROR;
|
||||
|
@ -231,7 +231,7 @@ static rt_err_t _can_control(struct rt_can_device *can_device, int cmd, void *ar
|
|||
for (int i = 0; i < filter_cfg->count; i++)
|
||||
{
|
||||
|
||||
/*默认过滤表判断*/
|
||||
/*默认过滤表判断*/
|
||||
if(filter_cfg->items[i].hdr < drv_can->device.config.maxhdr)
|
||||
drv_can->FilterConfig.number = filter_cfg->items[i].hdr;
|
||||
else
|
||||
|
@ -239,17 +239,17 @@ static rt_err_t _can_control(struct rt_can_device *can_device, int cmd, void *ar
|
|||
|
||||
if(filter_cfg->items[i].mode)
|
||||
{
|
||||
/*标识符列表模式: 类型匹配 ,id匹配为:接收的id = 配置的id
|
||||
或者 = 配置的mask ,通过*/
|
||||
/*扩展帧*/
|
||||
/*标识符列表模式: 类型匹配 ,id匹配为:接收的id = 配置的id
|
||||
或者 = 配置的mask ,通过*/
|
||||
/*扩展帧*/
|
||||
if(filter_cfg->items[i].ide)
|
||||
{
|
||||
// filter_cfg->items[i].id = filter_cfg->items[i].id ; /*id 29 位*/
|
||||
// filter_cfg->items[i].id = filter_cfg->items[i].id ; /*id 29 位*/
|
||||
filter_cfg->items[i].mask = ((filter_cfg->items[i].mask << 3) |
|
||||
(filter_cfg->items[i].ide << 2) |
|
||||
(filter_cfg->items[i].rtr << 1));
|
||||
}
|
||||
else /*标准帧*/
|
||||
else /*标准帧*/
|
||||
{
|
||||
filter_cfg->items[i].id = (filter_cfg->items[i].id << 18);
|
||||
filter_cfg->items[i].mask = ((filter_cfg->items[i].mask << 21) |
|
||||
|
@ -259,20 +259,20 @@ static rt_err_t _can_control(struct rt_can_device *can_device, int cmd, void *ar
|
|||
}
|
||||
else
|
||||
{
|
||||
/*标识符掩码模式*/
|
||||
/*扩展帧*/
|
||||
/*标识符掩码模式*/
|
||||
/*扩展帧*/
|
||||
if(filter_cfg->items[i].ide)
|
||||
{
|
||||
filter_cfg->items[i].mask = (filter_cfg->items[i].mask)<<3;
|
||||
}
|
||||
else /*标准帧*/
|
||||
else /*标准帧*/
|
||||
{
|
||||
filter_cfg->items[i].id = (filter_cfg->items[i].id)<<18;
|
||||
filter_cfg->items[i].mask = (filter_cfg->items[i].mask)<<21;
|
||||
}
|
||||
|
||||
#if ES_C_CAN_FILTER_FRAME_TYPE
|
||||
/*匹配类型*/
|
||||
/*匹配类型*/
|
||||
filter_cfg->items[i].mask |= 0x6;
|
||||
#endif
|
||||
|
||||
|
|
|
@ -276,7 +276,7 @@ static rt_err_t es32f0_hwtimer_control(rt_hwtimer_t *timer,
|
|||
temp = (double)ald_cmu_get_pclk1_clock();
|
||||
target = temp/freq;
|
||||
|
||||
if(target < 0x10001) /*最大分频 = max(PRES)+1*/
|
||||
if(target < 0x10001) /*最大分频 = max(PRES)+1*/
|
||||
{
|
||||
temp = target - (int)(target);
|
||||
|
||||
|
@ -293,7 +293,7 @@ static rt_err_t es32f0_hwtimer_control(rt_hwtimer_t *timer,
|
|||
|
||||
}
|
||||
|
||||
if(ret == RT_EOK) /*更新信息*/
|
||||
if(ret == RT_EOK) /*更新信息*/
|
||||
hwtimer->parent.freq = ald_cmu_get_pclk1_clock()/((hwtimer->hwtimer_periph->perh->PRES & 0xFFFF)+1);
|
||||
|
||||
}
|
||||
|
|
|
@ -275,7 +275,7 @@ int rt_hw_spi_init(void)
|
|||
gpio_init_t gpio_instruct;
|
||||
|
||||
gpio_instruct.odos = GPIO_PUSH_PULL;
|
||||
gpio_instruct.type = GPIO_TYPE_CMOS;
|
||||
gpio_instruct.type = GPIO_TYPE_CMOS;
|
||||
gpio_instruct.flt = GPIO_FILTER_DISABLE;
|
||||
gpio_instruct.odrv = GPIO_OUT_DRIVE_NORMAL;
|
||||
#ifdef BSP_USING_SPI0
|
||||
|
|
Loading…
Reference in New Issue