rt-thread学习(一) 创建led闪烁 加入串口设备 pwm配置 modbus主/从测试代码
led灯闪烁
#include <rtthread.h>
#include <rtdevice.h>
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
#include "drv_common.h"
#define LED0_PIN GET_PIN(C, 13)
int main(void)
{
int count = 1;
rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
while (count++)
{
rt_pin_write(LED0_PIN, count % 2);
LOG_D("Hello RT-Thread!");
rt_thread_mdelay(1000);
}
return RT_EOK;
}
加入串口设备
直接在board.h里 添加宏定义
#define BSP_USING_UART6
#define BSP_UART2_TX_PIN "PA2"
#define BSP_UART2_RX_PIN "PA3"
使用cubemx 配置hse
直接配时钟即可 不要勾选 分成.c/.h 在rtt studio中会自动调用生成的 系统时钟配置 还要修改 HSE_VALUE
#define HSE_VALUE ((uint32_t)8000000U)
构建排除
配置pwm
参考文章 如果要用pwm1的话,要在pwm_config.h里自己加
#ifdef BSP_USING_PWM1
#ifndef PWM1_CONFIG
#define PWM1_CONFIG \
{ \
.tim_handle.Instance = TIM1, \
.name = "pwm1", \
.channel = 0 \
}
#endif
board.h
#define BSP_USING_PWM1
#define BSP_USING_PWM1_CH1
stm32f4xx_hal_conf.h 开启宏定义
#define HAL_TIM_MODULE_ENABLED
测试代码
#include <rtthread.h>
#include <board.h>
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
#include <rtdevice.h>
#define PWM_DEV_NAME "pwm1"
#define PWM_DEV_CHANNEL 1 << 0
struct rt_device_pwm *pwm_dev;
static void pwm_thread_entry(void *parameter)
{
rt_uint32_t period, pulse,dir=1;
period = 20000000;
pulse = 0;
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);
}
rt_kprintf("pwm sample run ! find %s device!\n", PWM_DEV_NAME);
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 += period*0.01;
}
else
{
pulse -= period*0.01;
}
if (pulse <= period*0.1)
{
dir = 1;
}
if ( pulse>=period*0.9)
{
dir = 0;
}
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
}
}
static int pwm_led_init(void)
{
static rt_thread_t tid_pwm = RT_NULL;
rt_err_t ret = RT_EOK;
tid_pwm = rt_thread_create("t_pwm",pwm_thread_entry, RT_NULL, 512, 24, 10);
if (tid_pwm != RT_NULL)
{
rt_thread_startup(tid_pwm);
}
else
{
ret = RT_ERROR;
}
return ret;
}
INIT_APP_EXPORT(pwm_led_init);
int main(void)
{
pwm_led_init();
return RT_EOK;
}
freemodbus 测试代码
#include <rtthread.h>
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
#include "mb.h"
#include "user_mb_app.h"
#define SLAVE_ADDR 0x01
#define PORT_NUM 2
#define PORT_BAUDRATE 115200
#define PORT_PARITY MB_PAR_EVEN
#define MB_POLL_THREAD_PRIORITY 10
#define MB_SEND_THREAD_PRIORITY RT_THREAD_PRIORITY_MAX - 1
#define MB_POLL_CYCLE_MS 200
extern USHORT usSRegHoldBuf[S_REG_HOLDING_NREGS];
static void send_thread_entry(void *parameter)
{
USHORT *usRegHoldingBuf;
usRegHoldingBuf = usSRegHoldBuf;
rt_base_t level;
while (1)
{
level = rt_hw_interrupt_disable();
usRegHoldingBuf[0] = (USHORT)(18);
usRegHoldingBuf[1] = (USHORT)(96);
rt_hw_interrupt_enable(level);
rt_thread_mdelay(1000);
}
}
static void mb_slave_poll(void *parameter)
{
eMBInit(MB_RTU, SLAVE_ADDR, PORT_NUM, PORT_BAUDRATE, PORT_PARITY);
eMBEnable();
while (1)
{
eMBPoll();
rt_thread_mdelay(50);
}
}
int mb_slave_samlpe()
{
static rt_uint8_t is_init = 0;
rt_thread_t tid1 = RT_NULL, tid2 = RT_NULL;
if (is_init > 0)
{
rt_kprintf("sample is running\n");
return -RT_ERROR;
}
tid1 = rt_thread_create("md_s_poll", mb_slave_poll, RT_NULL, 1024, MB_POLL_THREAD_PRIORITY, 10);
if (tid1 != RT_NULL)
{
rt_thread_startup(tid1);
}
else
{
goto __exit;
}
tid2 = rt_thread_create("md_s_send", send_thread_entry, RT_NULL, 512, MB_SEND_THREAD_PRIORITY, 10);
if (tid2 != RT_NULL)
{
rt_thread_startup(tid2);
}
else
{
goto __exit;
}
is_init = 1;
return RT_EOK;
__exit:
if (tid1)
rt_thread_delete(tid1);
if (tid2)
rt_thread_delete(tid2);
return -RT_ERROR;
}
MSH_CMD_EXPORT(mb_slave_samlpe, run a modbus slave sample);
int main(void)
{
mb_slave_samlpe();
return RT_EOK;
}
freemodbus 从机测试代码
#include <rtthread.h>
#include <board.h>
#include <rtdevice.h>
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
#include "mb.h"
#include "mb_m.h"
#include "mbconfig.h"
#include "user_mb_app.h"
extern USHORT usMRegInBuf[MB_MASTER_TOTAL_SLAVE_NUM][M_REG_INPUT_NREGS];
extern USHORT usMRegHoldBuf[MB_MASTER_TOTAL_SLAVE_NUM][M_REG_HOLDING_NREGS];
#define SLAVE_ADDR 0x01
#define PORT_NUM 2
#define PORT_BAUDRATE 115200
#define PORT_PARITY MB_PAR_NONE
#define MB_POLL_THREAD_PRIORITY 10
#define MB_SEND_THREAD_PRIORITY RT_THREAD_PRIORITY_MAX - 1
#define MB_SEND_REG_START 2
#define MB_SEND_REG_NUM 2
#define MB_POLL_CYCLE_MS 800
#define MB_READ_REG_NUM 2
#define MB_READ_REG_ADDR 0x000f
static void send_thread_entry(void *parameter)
{
eMBMasterReqErrCode error_code2 = MB_MRE_NO_ERR;
static rt_uint16_t error_count2 = 0;
while (1)
{
error_code2 =eMBMasterReqReadHoldingRegister( SLAVE_ADDR, MB_READ_REG_ADDR, MB_READ_REG_NUM, RT_WAITING_FOREVER );
if (error_code2 != MB_MRE_NO_ERR)
{
error_count2++;
rt_kprintf("\r\n \r\n error_code2 = %d, error_count2 = %d \r\n",error_code2,error_count2);
error_code2 = MB_MRE_NO_ERR;
} else
{
uint32_t distance=0;
rt_kprintf("HoldBuf[0] = %d, HoldBuf[1] = %d\r\n",usMRegHoldBuf[SLAVE_ADDR-1][MB_READ_REG_ADDR],usMRegHoldBuf[SLAVE_ADDR-1][MB_READ_REG_ADDR+1]);
}
rt_thread_mdelay(1000);
}
}
static void mb_master_poll(void *parameter)
{
eMBMasterInit(MB_RTU, PORT_NUM, PORT_BAUDRATE, PORT_PARITY);
eMBMasterEnable();
while (1)
{
eMBMasterPoll();
rt_thread_mdelay(50);
}
}
int mb_master_samlpe()
{
static rt_uint8_t is_init = 0;
rt_thread_t tid1 = RT_NULL, tid2 = RT_NULL;
if (is_init > 0)
{
rt_kprintf("sample is running\n");
return -RT_ERROR;
}
tid1 = rt_thread_create("md_m_poll", mb_master_poll, RT_NULL, 512, MB_POLL_THREAD_PRIORITY, 10);
if (tid1 != RT_NULL)
{
rt_thread_startup(tid1);
}
else
{
goto __exit;
}
tid2 = rt_thread_create("md_m_send", send_thread_entry, RT_NULL, 512, MB_SEND_THREAD_PRIORITY, 10);
if (tid2 != RT_NULL)
{
rt_thread_startup(tid2);
}
else
{
goto __exit;
}
is_init = 1;
return RT_EOK;
__exit:
if (tid1)
rt_thread_delete(tid1);
if (tid2)
rt_thread_delete(tid2);
return -RT_ERROR;
}
MSH_CMD_EXPORT(mb_master_samlpe, run a modbus master sample);
int main(void)
{
mb_master_samlpe();
return RT_EOK;
}
|