Alios-Things 點亮led

Alios Things 點亮led

1 實現步驟

點個燈是爲了上手,瞭解一些最基礎的東西,比如程序框架,外設的一般操作方式。 Alios即然是一個OS,程序框架必然和裸機操作不同,先看個簡單的hello-world,代碼如下

#include <aos/aos.h>

static void app_delayed_action(void *arg)
{
    LOG("helloworld %s:%d %s\r\n", __func__, __LINE__, aos_task_name());
    aos_post_delayed_action(5000, app_delayed_action, NULL);
}

int application_start(int argc, char *argv[])
{
    LOG("application started.");
    aos_post_delayed_action(1000, app_delayed_action, NULL);
    aos_loop_run();

    return 0;
}

頭文件aos.h是alios動力的源頭,先不管有多複雜,照抄就是了。

application_start是程序的入口點,硬件上電初始化後,要跳到這裏來執行。 aos_post_delayed_action(1000, app_delayed_action, NULL)的作用是延遲執行指定的代碼,在1秒鐘後執行app_delayed_action函數,這個用法有點和Android中的Handler.postDelay方法類似。

然後在app_delay_action函數中,反覆延遲調用自身,從而達到反覆執行的目的。 在入口函數中的aos_loop_run則是進入事件循環,該幹啥就幹啥。

點燈用到GPIO,alios要忽略硬件之間的差異,必然得提供一份統一的API及差異化的硬件配置,統一的API提供了GPIO的初始化、讀寫操作,而差異化的硬件配置則針對特定的硬件平臺。 這個差異化的配置文件位於board目錄下,針對Alios Things Development Kit來說,該文件爲

AliOS-Things\board\developerkit\hal\hal_gpio_stm32_l4.h

部分代碼摘錄如下 :

#include "stm32l4xx_hal.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "aos/kernel.h"

#define PINS_IN_GROUP  16
#define GROUP_GPIOA    0
#define GROUP_GPIOB    1         
#define GROUP_GPIOC    2
#define GROUP_GPIOD    3         
#define GROUP_GPIOE    4
#define GROUP_GPIOF    5
#define GROUP_GPIOG    6
#define GROUP_GPIOH    7
         
#define HAL_GPIO_0                 ((uint8_t)0)        /* represent GPIOA pin 0 */
#define HAL_GPIO_1                 ((uint8_t)1)        /* represent GPIOA pin 1 */
#define HAL_GPIO_2                 ((uint8_t)2)        /* represent GPIOA pin 2 */
#define HAL_GPIO_3                 ((uint8_t)3)        /* represent GPIOA pin 3 */
#define HAL_GPIO_4                 ((uint8_t)4)        /* represent GPIOA pin 4 */
#define HAL_GPIO_5                 ((uint8_t)5)        /* represent GPIOA pin 5 */
#define HAL_GPIO_6                 ((uint8_t)6)        /* represent GPIOA pin 6 */
#define HAL_GPIO_7                 ((uint8_t)7)        /* represent GPIOA pin 7 */
#define HAL_GPIO_8                 ((uint8_t)8)        /* represent GPIOA pin 8 */
#define HAL_GPIO_9                 ((uint8_t)9)        /* represent GPIOA pin 9 */
#define HAL_GPIO_10                ((uint8_t)10)       /* represent GPIOA pin 10 */
#define HAL_GPIO_11                ((uint8_t)11)       /* represent GPIOA pin 11 */
#define HAL_GPIO_12                ((uint8_t)12)       /* represent GPIOA pin 12 */
#define HAL_GPIO_13                ((uint8_t)13)       /* represent GPIOA pin 13 */
#define HAL_GPIO_14                ((uint8_t)14)       /* represent GPIOA pin 14 */
#define HAL_GPIO_15                ((uint8_t)15)       /* represent GPIOA pin 15 */
         
#define HAL_GPIO_16                ((uint8_t)16)       /* represent GPIOB pin 0 */
#define HAL_GPIO_17                ((uint8_t)17)       /* represent GPIOB pin 1 */
#define HAL_GPIO_18                ((uint8_t)18)       /* represent GPIOB pin 2 */
#define HAL_GPIO_19                ((uint8_t)19)       /* represent GPIOB pin 3 */
#define HAL_GPIO_20                ((uint8_t)20)       /* represent GPIOB pin 4 */
#define HAL_GPIO_21                ((uint8_t)21)       /* represent GPIOB pin 5 */
#define HAL_GPIO_22                ((uint8_t)22)       /* represent GPIOB pin 6 */
#define HAL_GPIO_23                ((uint8_t)23)       /* represent GPIOB pin 7 */
#define HAL_GPIO_24                ((uint8_t)24)       /* represent GPIOB pin 8 */
#define HAL_GPIO_25                ((uint8_t)25)       /* represent GPIOB pin 9 */

有了這些基礎,接下來就可以點燈了。 開發板上提供了LED1, LED2,LED3,對應的硬件連接如圖 :

在這裏插入圖片描述

對應前面的GPIO映射得知在ALIOS中其編號爲22,接下來修改代碼如 :

#include <aos/aos.h>
#include <hal/soc/soc.h>

#define LED1 22
gpio_dev_t led;


static void app_delayed_action(void *arg)
{
    //LOG("helloworld %s:%d %s\r\n", __func__, __LINE__, aos_task_name());
    // 取反
    hal_gpio_output_toggle(&led);
    aos_post_delayed_action(500, app_delayed_action, NULL);
}

int application_start(int argc, char *argv[])
{
    LOG("application started.");
    /* gpio port config */
    led.port = LED1;
    /* set as output mode */
    led.config = OUTPUT_PUSH_PULL;
    /* configure GPIO with the given settings */
    hal_gpio_init(&led);

    aos_post_delayed_action(1000, app_delayed_action, NULL);
    aos_loop_run();

    return 0;
}

2 結果

然後編譯,上傳到開發板,LED1開始閃爍。

轉自:http://www.stmcu.org.cn/module/forum/forum.php?mod=viewthread&tid=616501&highlight=alios

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章