GPIO Demo

```char mmRspBuf[100] = {0}; #include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdint.h> #include "UART.h" #include "osa.h" #include "cgpio.h" #define sdk_uart_printf(fmt, args...) do { fatal_printf("[sdk]"fmt, ##args); } while(0) #define _TASK_STACK_SIZE 1280 static UINT32 _task_stack[_TASK_STACK_SIZE/sizeof(UINT32)]; static OSTaskRef _task_ref = NULL; static OSATimerRef _timer_ref = NULL; static OSFlagRef _flag_ref = NULL; #define GPIO_TEST_PIN 74 //PIN 2 #define TASK_TIMER_CHANGE_FLAG_BIT 0x01 static void _timer_callback(UINT32 tmrId); static void _task(void *ptr); // Device bootup hook before Phase1Inits. // If you have some work to be init, you may implete it here. // ex: you may start your task here. or do some initialize here. extern void Phase1Inits_enter(void); // Device bootup hook after Phase1Inits. // If you have some work to be init, you may implete it here. // ex: you may start your task here. or do some initialize here. extern void Phase1Inits_exit(void); // Device bootup hook before Phase2Inits. // If you have some work to be init, you may implete it here. // ex: you may start your task here. or do some initialize here. extern void Phase2Inits_enter(void); // Device bootup hook after Phase2Inits. // If you have some work to be init, you may implete it here. // ex: you may start your task here. or do some initialize here. extern void Phase2Inits_exit(void); void Phase1Inits_enter(void) { } void Phase1Inits_exit(void) { } void Phase2Inits_enter(void) { } void Phase2Inits_exit(void) { int ret; GPIOReturnCode status = GPIORC_OK; status = GpioSetDirection(GPIO_TEST_PIN, GPIO_OUT_PIN); if (status != GPIORC_OK){ sdk_uart_printf("status: 0x%lx", status); } ret = OSAFlagCreate(&_flag_ref); ASSERT(ret == OS_SUCCESS); ret = OSATimerCreate(&_timer_ref); ASSERT(ret == OS_SUCCESS); ret = OSATaskCreate(&_task_ref, _task_stack, _TASK_STACK_SIZE, 20, "test-task", _task, NULL); ASSERT(ret == OS_SUCCESS); OSATimerStart(_timer_ref, 2 * 200, 2 * 200, _timer_callback, 0); // 2 seconds timer } static int _gpio_set(UINT32 GPIO_NUM, int val) { GPIOReturnCode status = GPIORC_OK; status = GpioSetLevel(GPIO_NUM, val ? 1 : 0); if (status != GPIORC_OK){ sdk_uart_printf("status: 0x%lx", status); return -1; } return 0; } static void _timer_callback(UINT32 tmrId) { OSAFlagSet(_flag_ref, TASK_TIMER_CHANGE_FLAG_BIT, OSA_FLAG_OR); } static void _task(void *ptr) { OSA_STATUS status; UINT32 flag_value; UINT32 flag_mask = TASK_TIMER_CHANGE_FLAG_BIT; while(1) { status = OSAFlagWait(_flag_ref, flag_mask, OSA_FLAG_OR_CLEAR, &flag_value, OSA_SUSPEND); ASSERT(status == OS_SUCCESS); if (flag_value & TASK_TIMER_CHANGE_FLAG_BIT) { static int count = 0; count++; sdk_uart_printf("_task: timer\n"); if (count & 0x01) { _gpio_set(GPIO_TEST_PIN, 1); } else { _gpio_set(GPIO_TEST_PIN, 0); } } } } ```