diff --git a/applications/adc_vol_sample.c b/applications/adc_vol_sample.c new file mode 100644 index 0000000..62d18ba --- /dev/null +++ b/applications/adc_vol_sample.c @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2006-2022, 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 +#include + +#define ADC_DEV_NAME "adc1" /* ADC 设备名称 */ +#define ADC_DEV_CHANNEL 18 /* ADC 通道 */ +#define REFER_VOLTAGE 330 /* 参考电压 3.3V,数据精度乘以100保留2位小数*/ +#define CONVERT_BITS (1 << 12) /* 转换位数为12位 */ +#define TEMP_DELTA 0//温度补偿 +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); + rt_int32_t t = (int)(143-vol)/0.43+25; + rt_kprintf("temp is %d ℃\n",t+TEMP_DELTA); + + /* 关闭通道 */ + ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL); + + return ret; +} +/* 导出到 msh 命令列表中 */ +MSH_CMD_EXPORT(adc_vol_sample, adc voltage convert sample); diff --git a/applications/main.c b/applications/main.c index a59b5af..0bb0445 100644 --- a/applications/main.c +++ b/applications/main.c @@ -30,3 +30,4 @@ int main(void) return RT_EOK; } +//fastlz_test -c demo.bin f.bin diff --git a/applications/uart_dma_sample.c b/applications/uart_dma_sample.c new file mode 100644 index 0000000..f72aeab --- /dev/null +++ b/applications/uart_dma_sample.c @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2006-2022, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-04-16 misonyo first implementation. + */ +/* + * 程序清单:这是一个串口设备 DMA 接收使用例程 + * 例程导出了 uart_dma_sample 命令到控制终端 + * 命令调用格式:uart_dma_sample uart3 + * 命令解释:命令第二个参数是要使用的串口设备名称,为空则使用默认的串口设备 + * 程序功能:通过串口输出字符串"hello RT-Thread!",并通过串口输出接收到的数据,然后打印接收到的数据。 +*/ + +#include +#include +#define SAMPLE_UART_NAME "uart3" /* 串口设备名称 */ +#define MAX_SIZE_TO_SAVE 1024*2 +/* 串口接收消息结构*/ +struct rx_msg +{ + rt_device_t dev; + rt_size_t size; +}; +/* 串口设备句柄 */ +static rt_device_t serial; +/* 消息队列控制块 */ +static struct rt_messagequeue rx_mq; + +/* 接收数据回调函数 */ +static rt_err_t uart_input(rt_device_t dev, rt_size_t size) +{ + struct rx_msg msg; + rt_err_t result; + msg.dev = dev; + msg.size = size; + + result = rt_mq_send(&rx_mq, &msg, sizeof(msg)); + if ( result == -RT_EFULL) + { + /* 消息队列满 */ + rt_kprintf("message queue full!\n"); + } + return result; +} + +static void serial_thread_entry(void *parameter) +{ + struct rx_msg msg; + rt_err_t result; + rt_uint32_t rx_length; + static char rx_buffer[RT_SERIAL_RB_BUFSZ + 1]; + + while (1) + { + rt_memset(&msg, 0, sizeof(msg)); + /* 从消息队列中读取消息*/ + result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER); + if (result == RT_EOK) + { + /* 从串口读取数据*/ + rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size); + rx_buffer[rx_length] = '\0'; + /* 通过串口设备 serial 输出读取到的消息 */ + rt_device_write(serial, 0, rx_buffer, rx_length); + int fd=0; + + fd =open("/rxdata/demo.bin",O_WRONLY | O_CREAT|O_APPEND); + if(fd <0) + { + rt_kprintf("open file failed!\r\n"); + } + else + { + int count =write(fd,rx_buffer,rx_length); + close(fd); + fd =0; + } + + + /* 打印数据 */ + rt_kprintf("%s\n",rx_buffer); + } + } +} + +static int uart_dma_sample(int argc, char *argv[]) +{ + rt_err_t ret = RT_EOK; + char uart_name[RT_NAME_MAX]; + static char msg_pool[256]; + 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_mq_init(&rx_mq, "rx_mq", + msg_pool, /* 存放消息的缓冲区 */ + sizeof(struct rx_msg), /* 一条消息的最大长度 */ + sizeof(msg_pool), /* 存放消息的缓冲区大小 */ + RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */ + + /* 以 DMA 接收及轮询发送方式打开串口设备 */ + rt_device_open(serial, RT_DEVICE_FLAG_DMA_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_dma_sample, uart device dma sample); diff --git a/drivers/board.c b/drivers/board.c index 668be0f..7f209d0 100644 --- a/drivers/board.c +++ b/drivers/board.c @@ -75,3 +75,27 @@ void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi) } } + + +/** +* @brief ADC MSP Initialization +* This function configures the hardware resources used in this example +* @param hadc: ADC handle pointer +* @retval None +*/ +void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) +{ + if(hadc->Instance==ADC1) + { + /* USER CODE BEGIN ADC1_MspInit 0 */ + + /* USER CODE END ADC1_MspInit 0 */ + /* Peripheral clock enable */ + __HAL_RCC_ADC1_CLK_ENABLE(); + /* USER CODE BEGIN ADC1_MspInit 1 */ + + /* USER CODE END ADC1_MspInit 1 */ + } + +} + diff --git a/drivers/board.h b/drivers/board.h index 83bae85..f005701 100644 --- a/drivers/board.h +++ b/drivers/board.h @@ -71,6 +71,12 @@ extern "C" #define BSP_UART1_TX_PIN "PA9" #define BSP_UART1_RX_PIN "PA10" +#define BSP_USING_UART3 +#define BSP_UART3_TX_PIN "PB10" +#define BSP_UART3_RX_PIN "PB11" +#define BSP_UART3_RX_USING_DMA + + /*-------------------------- UART CONFIG END --------------------------*/ /*-------------------------- I2C CONFIG BEGIN --------------------------*/ @@ -185,7 +191,7 @@ extern "C" * */ -/*#define BSP_USING_ADC1*/ +#define BSP_USING_ADC1 /*#define BSP_USING_ADC2*/ /*#define BSP_USING_ADC3*/ diff --git a/drivers/stm32f4xx_hal_conf.h b/drivers/stm32f4xx_hal_conf.h index 667fa4a..f0a95da 100644 --- a/drivers/stm32f4xx_hal_conf.h +++ b/drivers/stm32f4xx_hal_conf.h @@ -49,7 +49,7 @@ */ #define HAL_MODULE_ENABLED -/* #define HAL_ADC_MODULE_ENABLED */ +#define HAL_ADC_MODULE_ENABLED /* #define HAL_CRYP_MODULE_ENABLED */ /* #define HAL_CAN_MODULE_ENABLED */ /* #define HAL_CRC_MODULE_ENABLED */ diff --git a/rtconfig.h b/rtconfig.h index 5c88d43..386a64c 100644 --- a/rtconfig.h +++ b/rtconfig.h @@ -105,8 +105,10 @@ #define RT_USING_DEVICE_IPC #define RT_USING_SERIAL #define RT_USING_SERIAL_V1 -#define RT_SERIAL_RB_BUFSZ 64 +#define RT_SERIAL_USING_DMA +#define RT_SERIAL_RB_BUFSZ 4096 #define RT_USING_PIN +#define RT_USING_ADC #define RT_USING_RTC #define RT_USING_SPI #define RT_USING_SFUD