2748 views|7 replies

1w

Posts

16

Resources
The OP
 

【GD32L233C-START Review】4. Signal [Copy link]

This post was last edited by ddllxxrr on 2022-1-27 21:32

Semaphores are used to coordinate the running order between threads, or to synchronize threads.

The semaphore of RT_THREAD NANO is simple.

Define the semaphore control block:

rt_sem_t variable;

Define a semaphore.

rt_sem_creat();

Get a semaphore:

rt_sem_take();

Release a semaphore:

rt_sesm_release();

The implementation procedure is as follows:

The programming idea is very simple: one thread just keeps acquiring and releasing the semaphore, and the other thread waits for the semaphore.


#include "gd32l23x.h"
#include "systick.h"
#include "rtthread.h"

static rt_thread_t led1_thr = RT_NULL;
static rt_thread_t delay_thr = RT_NULL;
static rt_sem_t sem_led = RT_NULL;
//struct rt_semaphore sem_mult;
static void thread_led1_entry(void *parameter);
static void thread_delay_entry(void *parameter);

/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
int main(void)
{
    systick_config();

    /* 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);
    gpio_mode_set(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_6 | GPIO_PIN_7);
    gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7);
    /* reset LED GPIO pin */
    gpio_bit_reset(GPIOA, GPIO_PIN_7 | GPIO_PIN_8);
    gpio_bit_reset(GPIOC, GPIO_PIN_6 | GPIO_PIN_7);
	   
	  sem_led = rt_sem_create("sled",0,RT_IPC_FLAG_FIFO);
	  
	  led1_thr = rt_thread_create( "led1",     /*????*/                    
                                  thread_led1_entry,/*??????*/
                                  RT_NULL,/*????????*/
                                  256,    /*?????*/
                                  4 ,    /*?????*/
                                  20);   /*?????*/
			rt_thread_startup(led1_thr);
	  delay_thr = rt_thread_create("delay",
		                             thread_delay_entry,
																 RT_NULL,
																 256,
																 4,
																 20
																 );
		
		rt_thread_startup(delay_thr);
    sem_led = rt_sem_create("ssled",1,RT_IPC_FLAG_FIFO);
    while(1) {
//        /* turn on LED1, turn off LED4 */
//        gpio_bit_set(GPIOA, GPIO_PIN_7);
//        gpio_bit_reset(GPIOC, GPIO_PIN_7);
//        delay_1ms(500);

//        /* turn on LED2, turn off LED1 */
//        gpio_bit_set(GPIOA, GPIO_PIN_8);
//        gpio_bit_reset(GPIOA, GPIO_PIN_7);
//        delay_1ms(500);

//        /* turn on LED3, turn off LED2 */
//        gpio_bit_set(GPIOC, GPIO_PIN_6);
//        gpio_bit_reset(GPIOA, GPIO_PIN_8);
//        delay_1ms(500);

//        /* turn on LED4, turn off LED3 */
//        gpio_bit_set(GPIOC, GPIO_PIN_7);
//        gpio_bit_reset(GPIOC, GPIO_PIN_6);
//        delay_1ms(500);
			
			      rt_thread_mdelay(100);
    }
}


static void thread_led1_entry(void *parameter)
{
  while(1)
	{	
    rt_sem_take(sem_led,RT_WAITING_FOREVER);
		
	gpio_bit_toggle(GPIOC,GPIO_PIN_7);
	  rt_thread_mdelay(500);
		rt_sem_release(sem_led);
	}
}


static void thread_delay_entry(void *parameter)
{
  while(1)
	{	
		rt_sem_release(sem_led);
		
		rt_thread_mdelay(1000);
		rt_sem_take(sem_led,RT_WAITING_FOREVER);
		
	}
}

Running results: One light flashes every half a second.

This post is from GD32 MCU

Latest reply

clear Thanks for reply   Details Published on 2022-1-28 08:07
Personal signaturehttp://shop34182318.taobao.com/
https://shop436095304.taobao.com/?spm=a230r.7195193.1997079397.37.69fe60dfT705yr
 

6555

Posts

0

Resources
2
 

What kind of signal is this RT_THREAD NANO semaphore?

How to define

This post is from GD32 MCU

Comments

Reply at night, on my home computer  Details Published on 2022-1-27 18:28
Reply at night, on my home computer  Details Published on 2022-1-27 08:57
Reply at night, on my home computer  Details Published on 2022-1-27 08:19
 
 
 

1w

Posts

16

Resources
3
 
Jacktang posted on 2022-1-27 07:39 What kind of signal is this RT_THREAD NANO semaphore and how is it defined?

Reply at night, on my home computer

This post is from GD32 MCU
 
Personal signaturehttp://shop34182318.taobao.com/
https://shop436095304.taobao.com/?spm=a230r.7195193.1997079397.37.69fe60dfT705yr
 
 

174

Posts

1

Resources
4
 
Jacktang posted on 2022-1-27 07:39 What kind of signal is this RT_THREAD NANO semaphore and how is it defined?

This uses a 0/1 binary semaphore.

The host has another counting semaphore

This post is from GD32 MCU

Comments

It should be  Details Published on 2022-1-27 19:48
 
 
 

1w

Posts

16

Resources
5
 
Jacktang posted on 2022-1-27 07:39 What kind of signal is this RT_THREAD NANO semaphore and how is it defined?

I declared a semaphore earlier and it worked without initializing it.

static rt_sem_t sem_led;

This post is from GD32 MCU

Comments

Understood thanks for reply  Details Published on 2022-1-28 08:07
 
Personal signaturehttp://shop34182318.taobao.com/
https://shop436095304.taobao.com/?spm=a230r.7195193.1997079397.37.69fe60dfT705yr
 
 

1w

Posts

16

Resources
6
 
chrisrh posted on 2022-1-27 08:57 This uses a 0/1 binary semaphore. Can you give me another counting semaphore?

It should be

This post is from GD32 MCU
 
Personal signaturehttp://shop34182318.taobao.com/
https://shop436095304.taobao.com/?spm=a230r.7195193.1997079397.37.69fe60dfT705yr
 
 

6555

Posts

0

Resources
7
 
ddllxxrr posted on 2022-1-27 18:28 I just declared a semaphore before, and it worked without initializing it. static rt_sem_t sem_led;

clear

Thanks for reply

This post is from GD32 MCU

Comments

It's not good to use without the initial use. That day, I don't know what happened.  Details Published on 2022-1-28 12:07
 
 
 

1w

Posts

16

Resources
8
 
Jacktang posted on 2022-1-28 08:07 I understand. Thanks for your reply

It's not good to use without the initial use. That day, I don't know what happened.

This post is from GD32 MCU
 
Personal signaturehttp://shop34182318.taobao.com/
https://shop436095304.taobao.com/?spm=a230r.7195193.1997079397.37.69fe60dfT705yr
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list