📄 t.c
字号:
#include "pioLib.h"IMPORT DAADIO daadio;IMPORT PIO pio;void pioPoll (port, msgQId) int port; MSG_Q_ID msgQId; { PIO_MSG_Q_DATA msgQData; UINT8 currentState; UINT8 lastState[PIO_MAX_CHANNELS_PER_PORT]; int channel; UINT8 * pOutputEnablePort; /* Error checking */ if (pio.initialized == FALSE) { printf ("pio not initialized\n"); return; } /* Fail if not an input port, i.e. if the corresponding bit in the output enable register is on */ pOutputEnablePort = (UINT8 *) daadio.baseAddr + PIO_OUTPUT_ENABLE_OFFSET; if (*pOutputEnablePort & (1 << port)) { printf ("Cannot poll an output port\n"); return; } /* Initialize our record of the last state of each channel to be OFF */ for (channel=0; channel<PIO_MAX_CHANNELS_PER_PORT; channel++) lastState[channel] = OFF; FOREVER { /* check each channel in port */ for (channel=0; channel<PIO_MAX_CHANNELS_PER_PORT; channel++) { /* Get the current state of the channel */ currentState = pioGet (port, channel); /* Check to see if the current state differs from the state of the channel the last time we checked. If it is, we need to generated a message. */ if (lastState[channel] != currentState) { /* Initialize the message structure and send it. */ msgQData.port = port; msgQData.channel = channel; msgQData.state = currentState; msgQData.timeStamp = tickGet(); if (pioMsgSend (msgQId, &msgQData) == ERROR) { printf ("Msg Queue fatal error: aborting poll task\n"); return; } /* Update our record of the last state for this channel. */ lastState[channel] = currentState; } taskDelay (PIO_POLL_DELAY); } } }void pioPoll1 (port, msgQId) int port; MSG_Q_ID msgQId; { PIO_MSG_Q_DATA msgQData; UINT8 currentState; UINT8 lastState = 0; UINT8 changedChannels; UINT8 * pPort; UINT8 channelBit; int channel; IMPORT int errno; /* Error checking */ if (pio.initialized == FALSE) { printf ("pio not initialized\n"); return; } /* Fail if not an input port, i.e. if the corresponding bit in the output enable register is on */ if (*((UINT8 *)daadio.baseAddr + PIO_OUTPUT_ENABLE_OFFSET) & (1 << port)) { printf ("port not an input port\n"); return; } /* Initialize <pPort> to point to the appropriate port */ pPort = PIO_PORT_NUM_TO_ADDR (port); FOREVER { taskDelay (PIO_POLL_DELAY); /* Get the current state of all the channels in the port. */ currentState = *pPort; /* If the state of the channels did not change, loop back. */ if (currentState == lastState) continue; /* Identify which channels changed state. By exclusive or'ing the current with the last state, we have, for each channel in the port, the corresponding bit set in <changedChannels>. */ changedChannels = currentState ^ lastState; /* Check each bit in <changedChannels> to see if that channel changed state. <channel> keeps track of which channel number we are checking. <channelToCheck> just holds a single set bit corresponding with the channel number we are checking for. */ for (channel=0; channel<PIO_MAX_CHANNELS_PER_PORT; channel++) { /* Set a bit in <channelBit> which reflects the channel we are checking. I.e., if channel 0, set bit 0; if channel 1, set bit 1, and so on. */ channelBit = 1 << channel; /* If the <channelBit> bit is not set in <changedChannels> then this <channel> did not change. Loop back and check the next channel. */ if (changedChannels & channelBit == 0) continue; /* <channel> changed */ /* Initialize the message structure and send it. */ msgQData.port = port; msgQData.channel = channel; msgQData.state = currentState; msgQData.timeStamp = tickGet(); if (pioMsgSend (msgQId, &msgQData) == ERROR) { printf ("Msg Queue fatal error: aborting poll task\n"); return; } } /* Update the last state with the current state */ lastState = currentState; } }STATUS pioMsgSend (msgQId, pMsgQData) MSG_Q_ID msgQId; PIO_MSG_Q_DATA * pMsgQData; { IMPORT int errno; if (msgQSend (msgQId, pMsgQData, sizeof (PIO_MSG_Q_DATA), NO_WAIT, MSG_PRI_NORMAL) == ERROR) { /* If failure was because queue is full, print warning otherwise return error. */ if (errno == S_objLib_OBJ_UNAVAILABLE) printf ("Msg Queue full, dumping data port %d, channel %d, status is %s at clock tick %d\n", pMsgQData->port, pMsgQData->channel, (pMsgQData->state) ? "ON" : "OFF", pMsgQData->timeStamp); else return (ERROR); } return (OK); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -