可以在vscode执行
gcc main.c mytime.c
或者在main.c前面写成
#include <stdio.h>
#include "mytime.h"
#include "mytime.c"
这样可以执行gcc main.c
代码
#include <stdio.h>
#include "mytime.h"
typedef enum {
DIAG_LED_STATE_POWEROFF,
DIAG_LED_STATE_POWERON,
DIAG_LED_STATE_READY,
DIAG_LED_STATE_WORKING,
DIAG_LED_STATE_MAX
}DIAG_LED_STATE_T;
static DIAG_LED_STATE_T DiagLedStatus=0;
//开发应用API
void setDiagLedStatus(DIAG_LED_STATE_T s){
DiagLedStatus=s;
}
static DIAG_LED_STATE_T getDiagLedStatus(void){
return DiagLedStatus;
}
#define diagLedGreenOff();
#define diagLedGreenOn();
#define diagLedRedOff();
#define diagLedGreenBlinkStop();
#define diagLedGreenBlinkStart();
#define diagLedRedBlinkStart();
#define diagLedRedBlinkStop();
//循环执行 找到跳变
void handleDiagLedStatus(void){
static DIAG_LED_STATE_T oldDiagLedStatus=0;
DIAG_LED_STATE_T nowDiagLedStatus=getDiagLedStatus();
if(oldDiagLedStatus==nowDiagLedStatus)
return;
oldDiagLedStatus=nowDiagLedStatus;
switch(nowDiagLedStatus){
case DIAG_LED_STATE_POWEROFF:
diagLedGreenOff();
diagLedGreenBlinkStop();
diagLedRedOff();
diagLedRedBlinkStop();
case DIAG_LED_STATE_POWERON:
diagLedGreenOff();
diagLedGreenBlinkStart();
diagLedRedOff();
diagLedRedBlinkStop();
case DIAG_LED_STATE_READY:
diagLedGreenOn();
diagLedGreenBlinkStop();
diagLedRedOff();
diagLedRedBlinkStop();
case DIAG_LED_STATE_WORKING:
diagLedGreenOff();
diagLedGreenBlinkStop();
diagLedRedOff();
diagLedRedBlinkStart();
}
}
//定义
uint8_t mytimeHandleDIAGLED = 0xFF;
void diagLedBlink(void){printf("\r\n\r\n\r\n%s\r\n\r\n\r\n",__FUNCTION__);}
void main()
{
static time_type T1;
mytimeHandleDIAGLED = timer.creat(&T1,100 , 1 , diagLedBlink);
//loop
handleDiagLedStatus();
timer_isr();
}
#include "mytime.h"
#include "stdbool.h"
static uint8_t StimerTaskId = 0; //表示每个节点的标号 此后可以对外释放 方便开 和 关
static time_type *Stimerhead = NULL; //链表的头结点
//遍历链表 周期loop执行
void timer_isr( void )
{
time_type *priv = Stimerhead;
while( priv != NULL )
{
if( priv->enable)
{
if( ++priv->cnt >= priv->threshold)
{
priv->cnt = 0;
if(priv->fun != NULL) priv->fun();
}
}
priv = priv->next;
}
}
//关闭节点 《并没有链表移除 而是用标记位 优点:方便简单 缺点:内存无法释放》
uint8_t timer_stop_time(uint8_t handle)
{
time_type *priv = Stimerhead;
while( priv != NULL )
{
if( priv->handleId == handle)
{
priv->enable = false;
return true;
}
priv = priv->next;
}
return false;
}
//开启节点
uint8_t timer_start_time(uint8_t handle)
{
time_type *priv = Stimerhead;
while( priv != NULL )
{
if( priv->handleId == handle)
{
priv->enable = true;
priv->cnt = 0;
return true;
}
priv = priv->next;
}
return false;
}
//一个节点添加到链表 返回它的ID
uint8_t timer_register_isr( time_type *timenode, uint32_t time_out ,uint8_t start, time_call_back call_back)
{
time_type *priv;
time_type *this;
this = timenode;//可以用malloc实现 目前这里自带内存
this->cnt = 0;//实际计数值
this->enable = start;//开关
this->handleId = StimerTaskId++;//统一在链表中的ID
this->threshold = time_out;//超时计数值
this->fun = call_back;//节点逻辑函数
this->next = NULL;//节点后继
if( Stimerhead == NULL)
{
Stimerhead = this;//第一次 实例化head
}
else
{
priv = Stimerhead;
while( priv->next != NULL ) priv = priv->next;//第一次以后:尾插
priv->next = this;
}
return (this->handleId);
}
time_ops_type timer =
{
.creat = timer_register_isr,
.stop = timer_stop_time ,
.start = timer_start_time,
};
#ifndef _MYTIMER_H_
#define _MYTIMER_H_
#include <stdlib.h>
#include <string.h>
#define uint8_t unsigned char
#define uint32_t unsigned int
typedef void (*time_call_back)(void);
//节点的结构体
//因为我放弃了malloc只能参数自己携带内存 所以也对外提供节点的结构体
typedef struct _time
{
void *next;
uint8_t handleId;
uint8_t enable;
uint32_t cnt;
uint32_t threshold;
void (*fun)(void);
}time_type;
//再次抽象 句柄结构体
typedef struct
{
uint8_t (*creat) ( time_type *timenode ,uint32_t time_out ,uint8_t start, time_call_back call_back);
uint8_t (*stop) ( uint8_t handle);
uint8_t (*start) ( uint8_t handle);
}time_ops_type;
//对外提供的句柄
extern time_ops_type timer;
//周期轮训业务 轮训链表的函数
void timer_isr( void );
#endif
============SHIJI===========
/*
* Copyright (c) 2015, Freescale Semiconductor, Inc.
* Copyright 2016-2017 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
/* Standard includes. */
#include <assert.h>
#include <stdio.h>
#include <string.h>
/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
/* Freescale includes. */
#include "fsl_device_registers.h"
#include "fsl_debug_console.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "board.h"
/*******************************************************************************
* Definitions
******************************************************************************/
#define EXAMPLE_LED_GPIO BOARD_USER_LED_GPIO
#define EXAMPLE_LED_GPIO_PIN BOARD_USER_LED_GPIO_PIN
void ledInit(void){ PRINTF("%s.\r\n",__FUNCTION__);
gpio_pin_config_t led_config = {kGPIO_DigitalOutput, 0, kGPIO_NoIntmode};
GPIO_PinInit(EXAMPLE_LED_GPIO, EXAMPLE_LED_GPIO_PIN, &led_config);
}
#include <stdio.h>
#include "mytime.h"
typedef enum {
DIAG_LED_STATE_POWEROFF,
DIAG_LED_STATE_POWERON,
DIAG_LED_STATE_READY,
DIAG_LED_STATE_WORKING
}DIAG_LED_STATE_T;
DIAG_LED_STATE_T DiagLedStatus=0;
//开发应用API
void setDiagLedStatus(DIAG_LED_STATE_T s){
DiagLedStatus=s;
}
static DIAG_LED_STATE_T getDiagLedStatus(void){
return DiagLedStatus;
}
#define diagLedGreenOff() USER_LED_OFF()
#define diagLedGreenOn() USER_LED_ON()
#define diagLedRedOff();
#define diagLedGreenBlinkStop() timer.stop(mytimeHandleDIAGLED);
#define diagLedGreenBlinkStart()timer.start(mytimeHandleDIAGLED);
#define diagLedRedBlinkStart();
#define diagLedRedBlinkStop();
//定义
uint8_t mytimeHandleDIAGLED = 0xFF;
void diagLedBlink(void){PRINTF("%s.\r\n",__FUNCTION__);
GPIO_PortToggle(EXAMPLE_LED_GPIO, 1u << EXAMPLE_LED_GPIO_PIN);
}
void timeInit()
{
static time_type T1;
mytimeHandleDIAGLED = timer.creat(&T1, 10/*MAX*/ , 0/*STOP*/ , diagLedBlink);
//loop
//handleDiagLedStatus();
//timer_isr();
}
//循环执行 找到跳变
void handleDiagLedStatus(void){
static DIAG_LED_STATE_T oldDiagLedStatus=0;
DIAG_LED_STATE_T nowDiagLedStatus=getDiagLedStatus();
if(oldDiagLedStatus==nowDiagLedStatus)
return;
PRINTF("%s.%d\r\n",__FUNCTION__,nowDiagLedStatus);
oldDiagLedStatus=nowDiagLedStatus;
switch(nowDiagLedStatus){
case DIAG_LED_STATE_POWEROFF:
diagLedGreenOff();
diagLedGreenBlinkStop();
diagLedRedOff();
diagLedRedBlinkStop();
break;
case DIAG_LED_STATE_POWERON:
diagLedGreenOff();
diagLedGreenBlinkStart();
diagLedRedOff();
diagLedRedBlinkStop();
break;
case DIAG_LED_STATE_READY:
diagLedGreenBlinkStop();
diagLedGreenOn();
diagLedRedOff();
diagLedRedBlinkStop();
break;
case DIAG_LED_STATE_WORKING:
diagLedGreenOff();
diagLedGreenBlinkStop();
diagLedRedOff();
diagLedRedBlinkStart();
break;
}
}
#define EXAMPLE_SW_GPIO BOARD_USER_BUTTON_GPIO
#define EXAMPLE_SW_GPIO_PIN BOARD_USER_BUTTON_GPIO_PIN
#define EXAMPLE_SW_IRQ BOARD_USER_BUTTON_IRQ
#define EXAMPLE_GPIO_IRQHandler BOARD_USER_BUTTON_IRQ_HANDLER
#define EXAMPLE_SW_NAME BOARD_USER_BUTTON_NAME
void EXAMPLE_GPIO_IRQHandler(void){ PRINTF("%s\r\n",__FUNCTION__);
GPIO_PortClearInterruptFlags(EXAMPLE_SW_GPIO, 1U << EXAMPLE_SW_GPIO_PIN);
static DIAG_LED_STATE_T DiagLedStatus=0;
setDiagLedStatus(DiagLedStatus);
if(++DiagLedStatus>DIAG_LED_STATE_WORKING)
DiagLedStatus=0;
}
void buttonInit(void){ PRINTF("%s.\r\n",__FUNCTION__);
gpio_pin_config_t sw_config = {
kGPIO_DigitalInput,
0,
kGPIO_IntRisingEdge,
};
/* Init input switch GPIO. */
EnableIRQ(EXAMPLE_SW_IRQ);
GPIO_PinInit(EXAMPLE_SW_GPIO, EXAMPLE_SW_GPIO_PIN, &sw_config);
/* Enable GPIO pin interrupt */
GPIO_PortEnableInterrupts(EXAMPLE_SW_GPIO, 1U << EXAMPLE_SW_GPIO_PIN);
}
/* The software timer period. */
//#define SW_TIMER_PERIOD_MS (1000 / portTICK_PERIOD_MS)
#define SW_TIMER_PERIOD_MS (10 / portTICK_PERIOD_MS)
/*******************************************************************************
* Prototypes
******************************************************************************/
/* The callback function. */
static void SwTimerCallback(TimerHandle_t xTimer);
static void TaskTimerCallback(TimerHandle_t xTimer);
/*******************************************************************************
* Code
******************************************************************************/
/*!
* @brief Main function
*/
int main(void)
{
TimerHandle_t SwTimerHandle = NULL,TaskTimerHandle=NULL;
/* Init board hardware. */
BOARD_ConfigMPU();
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitDebugConsole();
SystemCoreClockUpdate();
/* Create the software timer. */
SwTimerHandle = xTimerCreate("SwTimer", /* Text name. */
SW_TIMER_PERIOD_MS, /* Timer period. */
pdTRUE, /* Enable auto reload. */
0, /* ID is not used. */
SwTimerCallback); /* The callback function. */
/* Start timer. */
xTimerStart(SwTimerHandle, 0);
TaskTimerHandle = xTimerCreate("TaskTimer", /* Text name. */
SW_TIMER_PERIOD_MS, /* Timer period. */
pdTRUE, /* Enable auto reload. */
0, /* ID is not used. */
TaskTimerCallback); /* The callback function. */
/* Start timer. */
xTimerStart(TaskTimerHandle, 0);
buttonInit();
ledInit();
timeInit();
PRINTF("SW_TIMER_PERIOD_MS=%d.\r\n",SW_TIMER_PERIOD_MS);
/* Start scheduling. */
vTaskStartScheduler();
for (;;)
;
}
/*!
* @brief Software timer callback.
*/
static void SwTimerCallback(TimerHandle_t xTimer)
{
//PRINTF("Tick.\r\n");//串口灯!有TX就会自动亮起red
//GPIO_PortToggle(EXAMPLE_LED_GPIO, 1u << EXAMPLE_LED_GPIO_PIN);
timer_isr();
}
static void TaskTimerCallback(TimerHandle_t xTimer)
{
handleDiagLedStatus();
}