我们要在rtthread studio 开发环境中建立stm32h743xih6芯片的工程。我们使用一块stm32h743及fpga的核心板完成相关实验,核心板如图:
1.首先建立rtthread工程
2.添加相关程序如下,我们在上一节的代码中添加相关代码:
#include <rtthread.h>
#include “rtdevice.h”
#define DBG_TAG “main”
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
#include “drv_common.h”
static int uart_sample(int argc);
#define SAMPLE_UART_NAME “uart2”
#define LED_PIN_NUM_BLUE GET_PIN(H,14) /* PH14 /
#define LED_PIN_NUM_GREEN GET_PIN(H,15) / PH14 /
#define LED_PIN_NUM_RED GET_PIN(I,1) / PH14 /
/ 用于接收消息的信号量 */
static struct rt_semaphore rx_sem;
static rt_device_t serial;
int main(void)
{
int count = 1;
uart_sample(2);
while (count++)
{
LOG_D(“Hello RT-Thread743!”);
rt_kprintf(“LED_PIN_NUM_BLUE= %d !\n”, LED_PIN_NUM_BLUE);
rt_kprintf(“LED_PIN_NUM_GREEN= %d !\n”, LED_PIN_NUM_GREEN);
// rt_kprintf(“LED_PIN_NUM_RED= %d !\n”, LED_PIN_NUM_RED);
rt_pin_write(LED_PIN_NUM_BLUE, PIN_LOW);
rt_thread_mdelay(500);
rt_pin_write(LED_PIN_NUM_BLUE, PIN_HIGH);
rt_thread_mdelay(500);
rt_pin_write(LED_PIN_NUM_GREEN, PIN_LOW);
rt_thread_mdelay(500);
rt_pin_write(LED_PIN_NUM_GREEN, PIN_HIGH);
rt_thread_mdelay(500);
// rt_pin_write(LED_PIN_NUM_RED, PIN_LOW);
// rt_thread_mdelay(500);
// rt_pin_write(LED_PIN_NUM_RED, PIN_HIGH);
// rt_thread_mdelay(500);
}
return RT_EOK;
}
/* 接收数据回调函数 /
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);
}
}
static int uart_sample(int argc)
{
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, SAMPLE_UART_NAME, RT_NAME_MAX);
}
else
{
rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
}
//PIN设备设置
rt_pin_mode(LED_PIN_NUM_BLUE, PIN_MODE_OUTPUT_OD);
rt_pin_write(LED_PIN_NUM_BLUE, PIN_HIGH);
rt_pin_mode(LED_PIN_NUM_GREEN, PIN_MODE_OUTPUT_OD);
rt_pin_write(LED_PIN_NUM_GREEN, PIN_HIGH);
// rt_pin_mode(LED_PIN_NUM_RED, PIN_MODE_OUTPUT_OD);
// rt_pin_write(LED_PIN_NUM_RED, PIN_HIGH);
//
/* 查找系统中的串口设备 */
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_OFLAG_RDWR);
/* 设置接收回调函数 */
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, 2048, 25, 10);
/* 创建成功则启动线程 */
if (thread != RT_NULL)
{
rt_thread_startup(thread);
rt_kprintf("线程启动成功!\n");
}
else
{
ret = RT_ERROR;
}
return ret;
}
我们的硬件原理图如图:
所以我们先获得管脚的引脚编号如下:
#define LED_PIN_NUM_BLUE GET_PIN(H,14) /* PH14 /
#define LED_PIN_NUM_GREEN GET_PIN(H,15) / PH15 /
#define LED_PIN_NUM_RED GET_PIN(I,1) / PI1 */
这样我们就获得了引脚编号,在如下程序中打印下,看看对不对
rt_kprintf(“LED_PIN_NUM_BLUE= %d !\n”, LED_PIN_NUM_BLUE);
rt_kprintf(“LED_PIN_NUM_GREEN= %d !\n”, LED_PIN_NUM_GREEN);
3.我们在使用pin设备前都要进行模式设置及初始化工作,我们将引脚设为开漏输出,并将引脚初始化为高,这样按照原理图,初始化完成后,led灯会熄灭,如下:
rt_pin_mode(LED_PIN_NUM_BLUE, PIN_MODE_OUTPUT_OD);
rt_pin_write(LED_PIN_NUM_BLUE, PIN_HIGH);
rt_pin_mode(LED_PIN_NUM_GREEN, PIN_MODE_OUTPUT_OD);
rt_pin_write(LED_PIN_NUM_GREEN, PIN_HIGH);
4.编译下载
我们可以看到打印的引脚编号分别为126和127.我们下载到芯片后就会看到led灯珠闪烁。