【GD32L233C-START】14、RT-Thread多线程、消息队列使用

2022-04-04 15:58:32 浏览数 (1)

1、基本思路

三个线程,一个消息队列(大小是1,长度是12) 线程1:LED 500ms闪烁一次; 线程2:adc采样; 线程3:oled显示; 在线程2中,采样到数据后,通过消息队列发送到线程3,线程3收到消息后,更新oled显示。

2、RT-Thread配置

默认是没有使能消息队列的,需要使能消息队列;

3、代码实现

(1)Led初始化及led线程

代码语言:javascript复制
static rt_thread_t led_thread = RT_NULL;

void LedInit(void)
{
	/* enable the LED GPIO clock */
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_GPIOC);
    /* configure LED GPIO pin */
    gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_7 | GPIO_PIN_8);
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7 | GPIO_PIN_8);
    /* reset LED GPIO pin */
    gpio_bit_reset(GPIOA, GPIO_PIN_7 | GPIO_PIN_8);
}

static void led_thread_entry(void *parameter)
{
    while (1)
    {
	gpio_bit_toggle(GPIOA, GPIO_PIN_7|GPIO_PIN_8);
        rt_thread_mdelay(500);
	rt_kprintf("led blinkrn");
    }
}

void LedThreadStart(void)
{
    led_thread = rt_thread_create( "led",     /*线程名字*/                    
				led_thread_entry,/*线程入口函数*/
				RT_NULL,/*线程入口函数参数*/
				256,    /*线程栈大小*/
				4 ,    /*线程优先级*/
				20);   /*线程时间片*/
    rt_thread_startup (led_thread);
}

(2)adc初始化以及adc线程

代码语言:javascript复制
static rt_thread_t adc_thread = RT_NULL;
static uint8_t adc_data[12];

void AdcInit(void)
{
    AdcGpioInit();
    AdcConfig();
}

static void adc_thread_entry(void *parameter)
{
    uint16_t ref=0;
    float ch1=0;
    float vdd=0;
    float temp=0;
    uint16_t part1=0,part2=0;
	    
    int result;
    uint8_t *p=(uint8_t *)parameter;

    while(1)
    {
	ref=AdcSample(ADC_CHANNEL_17);
	vdd=1.2/(float)ref*4095;
	part1=(uint32_t)vdd;
	part2=vdd*1000-part1*1000;
	rt_kprintf("rnVdd=%d.%d Vrn",part1,part2);
		
        rt_memcpy(&p[0],(uint8_t *)&part1,2);
	rt_memcpy(&p[2],(uint8_t *)&part2,2);

	ch1=AdcSample(ADC_CHANNEL_1)*vdd / 4095;
	part1=(uint32_t)ch1;
	part2=ch1*1000-part1*1000;
	rt_kprintf("Channel 1=%d.%d Vrn",part1,part2);
		        
	rt_memcpy(&p[4],(uint8_t *)&part1,2);
	rt_memcpy(&p[6],(uint8_t *)&part2,2);
		
	temp = ((float)((int16_t)AdcSample(ADC_CHANNEL_16) - (*(int16_t *)(0x1FFFF7F8)))* vdd / 4095 * 1000 / vdd)   30;
	part1=(uint32_t)temp;
	part2=temp*100-part1*100;
	rt_kprintf("Temp=%d.%d rnrn",part1,part2);
		
	rt_memcpy(&p[8],(uint8_t *)&part1,2);
	rt_memcpy(&p[10],(uint8_t *)&part2,2);
		
	extern rt_mq_t adc_to_oled_mq;
		
	result = rt_mq_send(adc_to_oled_mq, p,12); //发送消息

	if (result != RT_EOK)
        {
	    rt_kprintf("%s,send mq errrn",adc_thread->name);
        }
	else
	{
	    rt_kprintf("%s,send mq successrn",adc_thread->name);
	}
		
	rt_memset(p,'',12);
			
	rt_thread_mdelay(5000);
    }
}

void AdcThreadStart(void)
{
    adc_thread = rt_thread_create("adc",
				adc_thread_entry,
				&adc_data,
				512,
	                        5,
		                20);
		
    rt_thread_startup (adc_thread);
}

(3)oled初始化、oled线程、消息队列创建

代码语言:javascript复制
static rt_thread_t oled_thread=RT_NULL;

rt_mq_t adc_to_oled_mq=RT_NULL;
static uint8_t oled_data[12];
static char temp[16];

static  void oled_thread_entry(void *parameter)
{
    uint8_t *p =(uint8_t *)parameter;
	
    while(1)
    {
	if(rt_mq_recv(adc_to_oled_mq,p,12,RT_WAITING_FOREVER)==RT_EOK)//读取消息
	{
	    rt_kprintf("%s,recv message queue rn",oled_thread->name);
			
	    rt_memset(temp,' ',sizeof(temp));
	    rt_sprintf(temp,"Vdd=%d.%d V",*(uint16_t*)(&p[0]),*(uint16_t*)(&p[2]));
	    OledShowString(10,0,temp,16);
	    rt_kprintf("%srn",temp);
						
	    rt_memset(temp,' ',sizeof(temp));
	    rt_sprintf(temp,"Ch1=%d.%d V",*(uint16_t*)(&p[4]),*(uint16_t*)(&p[6]));
	    OledShowString(10,2,temp,16);
	    rt_kprintf("%srn",temp);
						
	    rt_memset(temp,' ',sizeof(temp));
	    rt_sprintf(temp,"Temp=%d.%d",*(uint16_t*)(&p[8]),*(uint16_t*)(&p[10]));
	    OledShowString(10,4,temp,16);
	    rt_kprintf("%srn",temp);
	}
    }
}

void OledThreadStart(void)
{
    adc_to_oled_mq=rt_mq_create("adc_oled", 12,1, RT_IPC_FLAG_PRIO); //创建消息队列
    oled_thread= rt_thread_create("oled",oled_thread_entry,oled_data,512,7,20);	
    rt_thread_startup(oled_thread);
}

4、现象

0 人点赞