From dc65d8d19b6f239e69a2a65209cdc61bb0b88818 Mon Sep 17 00:00:00 2001 From: dgjames <1943357252@qq.com> Date: Sat, 21 Dec 2024 17:03:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=B2=E5=8F=A3=E5=8F=91=E9=80=81=EF=BC=88?= =?UTF-8?q?=E6=9C=AA=E6=88=90=E5=8A=9F=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .config | 2 +- .vscode/c_cpp_properties.json | 3 +- board/board.h | 4 +++ my_pro/my_func.c | 2 ++ my_pro/my_func.h | 3 +- my_pro/myinfrared.c | 25 ++++++++++++- my_pro/myproject.c | 67 +++++++++++++++++++++++++++++------ my_pro/pin_irq_example.c | 2 ++ rtconfig.h | 1 - 9 files changed, 93 insertions(+), 16 deletions(-) diff --git a/.config b/.config index 09cae91..784a08b 100644 --- a/.config +++ b/.config @@ -1660,7 +1660,7 @@ CONFIG_BSP_USING_UART1=y # CONFIG_BSP_UART1_RX_USING_DMA is not set # CONFIG_BSP_UART1_TX_USING_DMA is not set CONFIG_BSP_USING_UART2=y -CONFIG_BSP_UART2_RX_USING_DMA=y +# CONFIG_BSP_UART2_RX_USING_DMA is not set # CONFIG_BSP_UART2_TX_USING_DMA is not set # CONFIG_BSP_USING_UART3 is not set # CONFIG_BSP_USING_UART4 is not set diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index e157755..922d704 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -422,7 +422,8 @@ "d:\\src", "d:\\src\\klibc", "d:\\Develop\\libraries\\STM32F4xx_HAL\\STM32F4xx_HAL_Driver\\Src", - "d:\\Develop\\libraries\\STM32F4xx_HAL\\CMSIS\\Device\\ST\\STM32F4xx\\Source\\Templates" + "d:\\Develop\\libraries\\STM32F4xx_HAL\\CMSIS\\Device\\ST\\STM32F4xx\\Source\\Templates", + "${workspaceFolder}/packages/aht10-latest/inc" ] } ], diff --git a/board/board.h b/board/board.h index 385043a..f7c4374 100644 --- a/board/board.h +++ b/board/board.h @@ -25,6 +25,10 @@ extern "C" { #define STM32_FLASH_SIZE (1024 * 1024) #define STM32_FLASH_END_ADDRESS ((uint32_t)(STM32_FLASH_START_ADRESS + STM32_FLASH_SIZE)) + +#define BSP_UART2_TX "PA2" +#define BSP_UART2_RX "PA3" + #if defined(__ARMCC_VERSION) extern int Image$$RW_IRAM1$$ZI$$Limit; #define HEAP_BEGIN ((void *)&Image$$RW_IRAM1$$ZI$$Limit) diff --git a/my_pro/my_func.c b/my_pro/my_func.c index 38e5748..9248da7 100644 --- a/my_pro/my_func.c +++ b/my_pro/my_func.c @@ -7,6 +7,8 @@ #include #define LCD_MAX 240 +//串口 +rt_device_t serial; extern char tmp[]; void lcd_black(int x, int y) diff --git a/my_pro/my_func.h b/my_pro/my_func.h index 1632e99..fdf13f7 100644 --- a/my_pro/my_func.h +++ b/my_pro/my_func.h @@ -6,7 +6,8 @@ #define LCD_MAX 240 #define EVENT_MQTT_ENABLE (1<<3) // struct rt_event my_event; - +//串口 +extern rt_device_t serial; void mytime(); void greattime(); diff --git a/my_pro/myinfrared.c b/my_pro/myinfrared.c index f466edf..c821c10 100644 --- a/my_pro/myinfrared.c +++ b/my_pro/myinfrared.c @@ -30,20 +30,41 @@ void snake_compare(rt_uint8_t key, rt_uint8_t repeat) rt_atomic_store(&snake_pressed, snake_max + 1); // 上 if (rt_strcmp(tmp, "30") == 0 || rt_strcmp(tmp, "53") == 0) + { if (rt_atomic_load(&now_direction) != 2) rt_atomic_store(&now_direction, 0); + char str[] = "forward"; + rt_device_write(serial, 0, str, (sizeof(str) - 1)); + + } + // 左 if (rt_strcmp(tmp, "E8") == 0 || rt_strcmp(tmp, "99") == 0) + { if (rt_atomic_load(&now_direction) != 3) rt_atomic_store(&now_direction, 1); + char str[] = "left"; + rt_device_write(serial, 0, str, (sizeof(str) - 1)); + } + // 下 if (rt_strcmp(tmp, "B0") == 0 || rt_strcmp(tmp, "4B") == 0) + { if (rt_atomic_load(&now_direction) != 0) rt_atomic_store(&now_direction, 2); + char str[] = "back"; + rt_device_write(serial, 0, str, (sizeof(str) - 1)); + } + // 右 if (rt_strcmp(tmp, "68") == 0 || rt_strcmp(tmp, "83") == 0) + { if (rt_atomic_load(&now_direction) != 1) rt_atomic_store(&now_direction, 3); + char str[] = "right"; + rt_device_write(serial, 0, str, (sizeof(str) - 1)); + } + // 菜单(切换页面) if (repeat == 0 && (rt_strcmp(tmp, "88") == 0 || rt_strcmp(tmp, "11") == 0)) { @@ -58,7 +79,9 @@ void snake_compare(rt_uint8_t key, rt_uint8_t repeat) // 确认(暂停、页面冻结) if (repeat == 0 && (rt_strcmp(tmp, "73") == 0||rt_strcmp(tmp, "FF") == 0)) { - + char str[] = "OK"; + /* 发送字符串 */ + rt_device_write(serial, 0, str, (sizeof(str) - 1)); if (page_chosen == 4&& page_stop == 0) { // rt_event_send(&my_event, EVENT_MQTT_ENABLE); diff --git a/my_pro/myproject.c b/my_pro/myproject.c index 9181bc6..f24755e 100644 --- a/my_pro/myproject.c +++ b/my_pro/myproject.c @@ -9,6 +9,8 @@ #include #include #include +#include "rtdevice.h" +// #include "serial_v2.h" #include "mysnake.h" #include "infrared.h" #include @@ -119,7 +121,7 @@ static int example_subscribe(void *handle) const char *fmt = "/%s/%s/user/get"; char *topic = NULL; int topic_len = 0; - + topic_len = rt_strlen(fmt) + rt_strlen(DEMO_PRODUCT_KEY) + rt_strlen(DEMO_DEVICE_NAME) + 1; topic = rt_malloc(topic_len); if (topic == NULL) @@ -198,7 +200,7 @@ void make_file() } return; } -int cnt = 0,page_tog=0; +int cnt = 0, page_tog = 0; void tmp_payload(void) { // 读取温湿度值 @@ -272,11 +274,11 @@ void tmp_payload(void) } if (ps_data > 1022) { - page_tog=1; + page_tog = 1; } - if(page_tog && ps_data<1000) + if (page_tog && ps_data < 1000) { - page_tog=0; + page_tog = 0; page_chosen = (page_chosen % PAGE_MAX) + 1; page_first = 1; } @@ -385,23 +387,48 @@ static void mqtt_example_main(void *parameter) void cdc_entry(void *parameter) { - + rt_device_t dev = RT_NULL; char buf[] = "hello rt-thread!\r\n"; dev = rt_device_find("vcom"); - + if (dev) rt_device_open(dev, RT_DEVICE_FLAG_RDWR); else - return ; - + return; + while (1) { rt_device_write(dev, 0, buf, rt_strlen(buf)); rt_thread_mdelay(500); } } +/* 接收数据回调函数 */ +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; + + 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); + } +} @@ -418,12 +445,28 @@ void ath_init(void) void serial_init(void) { // 初始化设备 - Dev = aht10_init(AHT10_I2C_BUS); + serial = rt_device_find("uart2"); if (Dev == RT_NULL) { - rt_kprintf("AHT10_init Fail"); + rt_kprintf("uart1 not find. Fail"); return; } + struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /* 初始化配置参数 */ + + config.baud_rate = BAUD_RATE_9600; // 修改波特率为 9600 + config.data_bits = DATA_BITS_8; // 数据位 8 + config.stop_bits = STOP_BITS_1; // 停止位 1 + config.bufsz = 1024; // 修改缓冲区 buff size 为 128 + config.parity = PARITY_NONE; // 无奇偶校验位 + + /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */ + rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config); + + /* 以 DMA 接收及轮询发送方式打开串口设备 */ + rt_device_open(serial, RT_DEVICE_FLAG_INT_RX); + char str[] = "hello RTT\r\n"; + /* 发送字符串 */ + rt_device_write(serial, 0, str, (sizeof(str) - 1)); } void mqt_init(void) { @@ -537,5 +580,7 @@ void my_project(void) cdc_init(); tst_init(); // 不知道为什么不能在mqtt_init()之前,不然报错 + + serial_init(); } MSH_CMD_EXPORT_ALIAS(my_project, myproject, run my project); \ No newline at end of file diff --git a/my_pro/pin_irq_example.c b/my_pro/pin_irq_example.c index 1944f21..3f8f4aa 100644 --- a/my_pro/pin_irq_example.c +++ b/my_pro/pin_irq_example.c @@ -11,6 +11,8 @@ #define KEY_DOWN GET_PIN(C, 1) #define KEY_LEFT GET_PIN(C, 0) #define KEY_RIGHT GET_PIN(C, 4) + + extern rt_atomic_t now_direction ; extern rt_atomic_t snake_pressed ; extern int snake_max; diff --git a/rtconfig.h b/rtconfig.h index c0367d2..8333dfa 100644 --- a/rtconfig.h +++ b/rtconfig.h @@ -639,7 +639,6 @@ #define BSP_USING_UART #define BSP_USING_UART1 #define BSP_USING_UART2 -#define BSP_UART2_RX_USING_DMA #define BSP_USING_UART6 #define BSP_USING_TIM #define BSP_USING_TIM11