📄 portdrv.c
字号:
/****************************************************************
** *
** FILE : PortDrv.C *
** COPYRIGHT : (c) 2001 .Xiamen Yaxon NetWork CO.LTD *
** *
** *
** By : CCH 2002.1.15 *
****************************************************************/
#include "includes.h"
#include "bsp.h"
#include "errcode.h"
#include "errtask.h"
#include "tools.h"
#include "timetask.h"
#include "portdrv.h"
/*
********************************************************************************
* DEFINE CONFIG PARAMETERS
********************************************************************************
*/
#define MAX_PORT 2
#define PERIOD_FLASH MILTICK, 2 /* flash period = 100 ms */
/*
********************************************************************************
* DEFINE PORT CONTROL BLOCK STRUCTURE
********************************************************************************
*/
typedef struct {
INT8U time_high; /* UNIT: 100 ms */
INT8U time_low; /* UNIT: 100 ms */
INT8U ct_high;
INT8U ct_low;
INT8U ct_cycle;
void (*informer)(void);
} PCB_STRUCT;
/*
********************************************************************************
* DEFINE MODULE VARIANT
********************************************************************************
*/
static TMR_TSK *flashtmr;
static INT8U port[MAX_PORT];
static PCB_STRUCT pcb[MAX_PORT][2];
static void PulldownAllPort(void)
{
INT8U i;
for (i = 0; i < MAX_PORT; i++) {
ControlPort(i, PORT_PULLDOWN);
}
}
static void FlashTmrProc(void)
{
INT8U i, j;
StartTmr(flashtmr, PERIOD_FLASH);
for (i = 0; i < MAX_PORT; i++)
{
if (pcb[i][0].ct_cycle == 0 && pcb[i][1].ct_cycle == 0)
{
ControlPort(i, port[i]);
continue;
}
for (j = 0; j < 2; j++)
{
if (j == 0 && pcb[i][1].ct_cycle > 0) continue;
if (pcb[i][j].ct_cycle > 0)
{
if (pcb[i][j].ct_high > 0)
{
pcb[i][j].ct_high--;
ControlPort(i, PORT_PULLUP);
}
else
{
pcb[i][j].ct_low--;
ControlPort(i, PORT_PULLDOWN);
if (pcb[i][j].ct_low == 0)
{
if (pcb[i][j].ct_cycle != 0xff) pcb[i][j].ct_cycle--;
if (pcb[i][j].ct_cycle == 0)
{
if (pcb[i][j].informer != 0) pcb[i][j].informer();
}
pcb[i][j].ct_high = pcb[i][j].time_high;
pcb[i][j].ct_low = pcb[i][j].time_low;
}
}
}
}
}
}
static void DiagnoseProc(void)
{
if (GetTmrSwitch(flashtmr) != ON) ErrExit(ERR_PORTDRV_TMR);
}
void InitPortDrv(void)
{
INT8U i;
// PrintFromUART(1, "InitPortDrv开始执行!\n");
for (i = 0; i < MAX_PORT; i++)
{
pcb[i][0].ct_cycle = 0;
pcb[i][1].ct_cycle = 0;
}
flashtmr = CreateTimer(FlashTmrProc, 0);
StartTmr(flashtmr, PERIOD_FLASH);
PulldownAllPort();
InstallDiagProc(DiagnoseProc);
}
void ControlPort(INT8U type, INT8U ctltype)
{
if (type >= MAX_PORT) ErrExit(ERR_PORTDRV_TYPE);
port[type] = ctltype;
switch (type)
{
case PORT_LEDRED: /* PORT: LED_RED */
if (ctltype == PORT_PULLUP) Pullup_LEDRED();
else Pulldown_LEDRED();
break;
case PORT_LEDGREEN: /* PORT: LED_GREEN */
if (ctltype == PORT_PULLUP) Pullup_LEDGREEN();
else Pulldown_LEDGREEN();
break;
default:
break;
}
}
void StartFlashPort(INT8U type, INT8U cycle, INT8U time_high, INT8U time_low, void (*informer)(void))
{
if (type >= MAX_PORT) ErrExit(ERR_PORTDRV_TYPE);
pcb[type][1].ct_cycle = cycle;
pcb[type][1].ct_high = time_high;
pcb[type][1].ct_low = time_low;
pcb[type][1].time_high = time_high;
pcb[type][1].time_low = time_low;
pcb[type][1].informer = informer;
}
void StopFlashPort(INT8U type)
{
if (type >= MAX_PORT) ErrExit(ERR_PORTDRV_TYPE);
pcb[type][1].ct_cycle = 0;
ControlPort(type, PORT_PULLDOWN);
}
void InstallPermnentPort(INT8U type, INT8U time_high, INT8U time_low)
{
if (type >= MAX_PORT) ErrExit(ERR_PORTDRV_TYPE);
pcb[type][0].ct_cycle = 0xff;
pcb[type][0].ct_high = time_high;
pcb[type][0].ct_low = time_low;
pcb[type][0].time_high = time_high;
pcb[type][0].time_low = time_low;
pcb[type][0].informer = 0;
}
void RemovePermnentPort(INT8U type)
{
if (type >= MAX_PORT) ErrExit(ERR_PORTDRV_TYPE);
pcb[type][0].ct_cycle = 0;
ControlPort(type, PORT_PULLDOWN);
}
BOOLEAN PortIsFlashing(INT8U type)
{
if (type >= MAX_PORT) ErrExit(ERR_PORTDRV_TYPE);
if (pcb[type][0].ct_cycle > 0 || pcb[type][1].ct_cycle > 0) return TRUE;
else return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -