【RTL8713】工程结构——以建立LED点亮工程为例

背景

本工程基于 RTL8721 开发板

参考 https://gitee.com/ameba-aiot/gpio_led_demo 项目

用户自己建的整个工程长啥样

1. 先看整个工程,有一个顶层的 CMakeLists.txt, 里面内容是 ameba_add_subdirectory(app_example),其中 app_example  是用户自定义的文件夹(用来放自己的应用程序)

image

用户目录的 CMakeLists.txt

然后是用户自定义的文件夹:app_example, CMakeLists.txt 内容如下, 主要是增加源文件(.c), 然后增加内部库(internal_library)

# Please refer to path/to/your/ameba_sdk/cmake/CMakeLists-template.cmake
set(private_sources)
ameba_list_append(private_sources app_main.c gpio_led_demo.c)
ameba_add_internal_library(app_example gpio_led_demo
    p_SOURCES
	    ${private_sources}
)

也可以按下面格式写:

ameba_internal_library(example_wifi_audio_player)

target_sources(
    ${CURRENT_LIB_NAME} PRIVATE

    example_wifi_audio_player.c
    audio_udp_stream.c
    audio_tts_cosyvoice.c
    cmd_audio_ctl.c
    app_example.c
)

target_include_directories(
    ${CURRENT_LIB_NAME} PRIVATE
    ${BASEDIR}/component/audio/interfaces
    ${BASEDIR}/component/audio/base/osal/osal_c/interfaces
)

用户目录的主函数:

如下方,其中的 void app_example(void)相当于主函数,用户的调用的逻辑全部要写在里面

#include "gpio_led_demo.h"
#include "FreeRTOS.h"
#include "task.h"

#define TAG "app_main" 

void app_example(void)
{
    RTK_LOGI(TAG, "gpio_led_demo start!\r\n");
    gpio_led_init();//初始化LED    

    if (rtos_task_create( NULL, "gpio_led_toggle", (rtos_task_t)gpio_led_toggle, NULL, 1024, tskIDLE_PRIORITY + 2 ) != RTK_SUCCESS )
    {
        RTK_LOGE(TAG, "gpio_led_toggle failed!\r\n");
    }   
}
    

LED驱动部分

以下是头文件 gpio_led_demo.h ,可见其内容是初始化和让LED闪烁;

#ifndef _GPIO_LDE_DEMO_H_
#define _GPIO_LDE_DEMO_H_

#include "ameba_soc.h"
#include "os_wrapper.h"
#include <stdio.h>
#include "platform_autoconf.h"


//定义三个gpio对应的evb pin idex
#define LED1_PIN _PA_14 //G
#define LED2_PIN _PA_15 //R
#define LED3_PIN _PA_16 //B

void gpio_led_init(void);
void gpio_led_toggle(void);

#endif

以下是具体实现的LED驱动源文件 gpio_led_demo.c:

#include "gpio_led_demo.h"

// @brief  GPIO Init Structure Definition
  
// typedef struct {
// 	u32 GPIO_Mode;			/*!< Specifies the operating mode for the selected pins.
// 								This parameter can be a value of @ref GPIO_Mode */

// 	u32 GPIO_PuPd;			/*!< Specifies the operating Pull-up/Pull down for the selected pins.
// 								This parameter can be a value of @ref GPIO_Pull_Type */

// 	u32 GPIO_ITTrigger;		/*!< Specifies interrupt mode is level or edge trigger
// 								This parameter can be a value of @ref GPIO_INT_Trigger_Type */

// 	u32 GPIO_ITPolarity;	/*!< Specifies interrupt mode is high or low active trigger
// 								This parameter can be a value of @ref GPIO_INT_Polarity_Type */

// 	u32 GPIO_ITDebounce;	/*!< Specifies enable or disable de-bounce for interrupt
// 								This parameter can be a value of @ref GPIO_INT_Debounce_Type*/

// 	u32 GPIO_Pin;			/*!< Specifies the selected pins.
// 								This parameter contains two parts: Pin[7:5]: port number; Pin[4:0]: pin number */
// } GPIO_InitTypeDef; 

void gpio_led_init()
{    
    //定义三个gpio给LED使用
    GPIO_InitTypeDef led1_gpio;
    GPIO_InitTypeDef led2_gpio;
    GPIO_InitTypeDef led3_gpio;

    //初始化LED 对应的GPIO io
    led1_gpio.GPIO_Pin = LED1_PIN;
    led2_gpio.GPIO_Pin = LED2_PIN;
    led3_gpio.GPIO_Pin = LED3_PIN;

    //GPIO mode == out
    led1_gpio.GPIO_Mode = GPIO_Mode_OUT;
    led2_gpio.GPIO_Mode = GPIO_Mode_OUT;
    led3_gpio.GPIO_Mode = GPIO_Mode_OUT;

    //GPIO init
    GPIO_Init(&led1_gpio); 
    GPIO_Init(&led2_gpio); 
    GPIO_Init(&led3_gpio); 
   
    printf("gpio_led_setup ready!\r\n");
    //LED 全部亮一次后再全灭
    GPIO_WriteBit(LED1_PIN, 1);
    GPIO_WriteBit(LED2_PIN, 1);
    GPIO_WriteBit(LED3_PIN, 1);
    rtos_time_delay_ms(1000);
    GPIO_WriteBit(LED1_PIN, 0);
    GPIO_WriteBit(LED2_PIN, 0);
    GPIO_WriteBit(LED3_PIN, 0); 
    rtos_time_delay_ms(2000);   
    return;
}

void gpio_led_toggle()
{    
    int i = 0;
    while (1)
    {
         //led toggle,间隔1s切换一次gpio
        GPIO_WriteBit(LED1_PIN, 1);
        rtos_time_delay_ms(1000);

        GPIO_WriteBit(LED1_PIN, 0);
        GPIO_WriteBit(LED2_PIN, 1);
        rtos_time_delay_ms(1000);

        GPIO_WriteBit(LED2_PIN, 0);
        GPIO_WriteBit(LED3_PIN, 1);
        rtos_time_delay_ms(1000);

        GPIO_WriteBit(LED3_PIN, 0);
        printf("==> LED toggle count:%d\r\n", ++i);   
    }    
    rtos_task_delete(NULL);
}

 

 

参考资料

https://gitee.com/ameba-aiot/gpio_led_demo

posted @ 2026-11-02 20:06  FBshark  阅读(11)  评论(0)    收藏  举报