aboutsummaryrefslogtreecommitdiffhomepage
path: root/AT91SAM7S256/Source/c_lowspeed.c
diff options
context:
space:
mode:
Diffstat (limited to 'AT91SAM7S256/Source/c_lowspeed.c')
-rw-r--r--AT91SAM7S256/Source/c_lowspeed.c137
1 files changed, 96 insertions, 41 deletions
diff --git a/AT91SAM7S256/Source/c_lowspeed.c b/AT91SAM7S256/Source/c_lowspeed.c
index f8baa92..658b5fc 100644
--- a/AT91SAM7S256/Source/c_lowspeed.c
+++ b/AT91SAM7S256/Source/c_lowspeed.c
@@ -18,6 +18,7 @@
#include "c_input.iom"
#include "c_lowspeed.h"
#include "d_lowspeed.h"
+#include <string.h>
static IOMAPLOWSPEED IOMapLowSpeed;
static VARSLOWSPEED VarsLowSpeed;
@@ -39,15 +40,89 @@ const HEADER cLowSpeed =
0x0000 //Code size - not used so far
};
+SBYTE cLowSpeedFastI2C(UBYTE ch);
+
void cLowSpeedInit(void* pHeader)
{
pHeaders = pHeader;
dLowSpeedInit();
IOMapLowSpeed.State = COM_CHANNEL_NONE_ACTIVE;
+ IOMapLowSpeed.NoRestartOnRead = COM_CHANNEL_RESTART_ALL;
+ IOMapLowSpeed.Speed = COM_CHANNEL_NONE_FAST;
+ IOMapLowSpeed.pFunc = &cLowSpeedFastI2C;
VarsLowSpeed.TimerState = TIMER_STOPPED;
}
+void cLowSpeedCompleteRead(UBYTE ch)
+{
+ for (VarsLowSpeed.InputBuf[ch].OutPtr = 0; VarsLowSpeed.InputBuf[ch].OutPtr < IOMapLowSpeed.InBuf[ch].BytesToRx; VarsLowSpeed.InputBuf[ch].OutPtr++)
+ {
+ IOMapLowSpeed.InBuf[ch].Buf[IOMapLowSpeed.InBuf[ch].InPtr] = VarsLowSpeed.InputBuf[ch].Buf[VarsLowSpeed.InputBuf[ch].OutPtr];
+ IOMapLowSpeed.InBuf[ch].InPtr++;
+ if (IOMapLowSpeed.InBuf[ch].InPtr >= SIZE_OF_LSBUF)
+ {
+ IOMapLowSpeed.InBuf[ch].InPtr = 0;
+ }
+ VarsLowSpeed.InputBuf[ch].Buf[VarsLowSpeed.InputBuf[ch].OutPtr] = 0;
+ }
+}
+
+void cLowSpeedLoadWriteBuffer(UBYTE ch)
+{
+ VarsLowSpeed.OutputBuf[ch].OutPtr = 0;
+ memcpy(VarsLowSpeed.OutputBuf[ch].Buf, IOMapLowSpeed.OutBuf[ch].Buf, IOMapLowSpeed.OutBuf[ch].InPtr);
+ VarsLowSpeed.OutputBuf[ch].InPtr = IOMapLowSpeed.OutBuf[ch].InPtr;
+ IOMapLowSpeed.OutBuf[ch].OutPtr = IOMapLowSpeed.OutBuf[ch].InPtr;
+/*
+ VarsLowSpeed.OutputBuf[ch].OutPtr = 0;
+ for (VarsLowSpeed.OutputBuf[ch].InPtr = 0; VarsLowSpeed.OutputBuf[ch].InPtr < IOMapLowSpeed.OutBuf[ch].InPtr; VarsLowSpeed.OutputBuf[ch].InPtr++)
+ {
+ VarsLowSpeed.OutputBuf[ch].Buf[VarsLowSpeed.OutputBuf[ch].InPtr] = IOMapLowSpeed.OutBuf[ch].Buf[IOMapLowSpeed.OutBuf[ch].OutPtr];
+ IOMapLowSpeed.OutBuf[ch].OutPtr++;
+ }
+*/
+}
+
+void cLowSpeedFinished(UBYTE ch, UBYTE bDone)
+{
+ IOMapLowSpeed.State = IOMapLowSpeed.State & ~LOWSPEED_CH_NUMBER[ch];
+ if (bDone)
+ IOMapLowSpeed.ChannelState[ch] = LOWSPEED_IDLE;
+ if (IOMapLowSpeed.State == 0)
+ {
+ dLowSpeedStopTimer();
+ VarsLowSpeed.TimerState = TIMER_STOPPED;
+ }
+}
+
+
+SBYTE cLowSpeedFastI2C(UBYTE ch)
+{
+ cLowSpeedLoadWriteBuffer(ch);
+ SBYTE result = dLowSpeedFastI2C(ch,
+ *(VarsLowSpeed.OutputBuf[ch].Buf),
+ VarsLowSpeed.OutputBuf[ch].Buf+1,
+ VarsLowSpeed.OutputBuf[ch].InPtr-1,
+ &(IOMapLowSpeed.InBuf[ch].BytesToRx),
+ VarsLowSpeed.InputBuf[ch].Buf);
+ if (result >= 0)
+ {
+ // finally copy the data from the VarsLowSpeed buffer into the IOMapLowSpeed buffer
+ // and update our channel state and mode
+ cLowSpeedCompleteRead(ch);
+ IOMapLowSpeed.Mode[ch] = LOWSPEED_DATA_RECEIVED;
+// IOMapLowSpeed.ChannelState[ch] = LOWSPEED_DONE;
+ cLowSpeedFinished(ch, TRUE);
+ }
+ else
+ {
+ IOMapLowSpeed.ChannelState[ch] = LOWSPEED_ERROR;
+ IOMapLowSpeed.ErrorType[ch] = (UBYTE)result;
+ }
+ return result;
+}
+
void cLowSpeedCtrl(void)
{
UBYTE Temp;
@@ -69,15 +144,22 @@ void cLowSpeedCtrl(void)
{
if ((pMapInput->Inputs[ChannelNumber].SensorType == LOWSPEED) || (pMapInput->Inputs[ChannelNumber].SensorType == LOWSPEED_9V))
{
- if (VarsLowSpeed.TimerState == TIMER_STOPPED)
- {
- dLowSpeedStartTimer();
- VarsLowSpeed.TimerState = TIMER_RUNNING;
- }
- IOMapLowSpeed.ChannelState[ChannelNumber] = LOWSPEED_LOAD_BUFFER;
- IOMapLowSpeed.ErrorType[ChannelNumber] = LOWSPEED_NO_ERROR;
- VarsLowSpeed.ErrorCount[ChannelNumber] = 0;
- dLowSpeedInitPins(ChannelNumber);
+ if (IOMapLowSpeed.Speed & (COM_CHANNEL_ONE_FAST << ChannelNumber))
+ {
+ cLowSpeedFastI2C(ChannelNumber);
+ }
+ else
+ {
+ if (VarsLowSpeed.TimerState == TIMER_STOPPED)
+ {
+ dLowSpeedStartTimer();
+ VarsLowSpeed.TimerState = TIMER_RUNNING;
+ }
+ IOMapLowSpeed.ChannelState[ChannelNumber] = LOWSPEED_LOAD_BUFFER;
+ IOMapLowSpeed.ErrorType[ChannelNumber] = LOWSPEED_NO_ERROR;
+ VarsLowSpeed.ErrorCount[ChannelNumber] = 0;
+ dLowSpeedInitPins(ChannelNumber);
+ }
}
else
{
@@ -91,13 +173,8 @@ void cLowSpeedCtrl(void)
{
if ((pMapInput->Inputs[ChannelNumber].SensorType == LOWSPEED) || (pMapInput->Inputs[ChannelNumber].SensorType == LOWSPEED_9V))
{
- VarsLowSpeed.OutputBuf[ChannelNumber].OutPtr = 0;
- for (VarsLowSpeed.OutputBuf[ChannelNumber].InPtr = 0; VarsLowSpeed.OutputBuf[ChannelNumber].InPtr < IOMapLowSpeed.OutBuf[ChannelNumber].InPtr; VarsLowSpeed.OutputBuf[ChannelNumber].InPtr++)
- {
- VarsLowSpeed.OutputBuf[ChannelNumber].Buf[VarsLowSpeed.OutputBuf[ChannelNumber].InPtr] = IOMapLowSpeed.OutBuf[ChannelNumber].Buf[IOMapLowSpeed.OutBuf[ChannelNumber].OutPtr];
- IOMapLowSpeed.OutBuf[ChannelNumber].OutPtr++;
- }
- if (dLowSpeedSendData(ChannelNumber, &VarsLowSpeed.OutputBuf[ChannelNumber].Buf[0], (VarsLowSpeed.OutputBuf[ChannelNumber].InPtr - VarsLowSpeed.OutputBuf[ChannelNumber].OutPtr)))
+ cLowSpeedLoadWriteBuffer(ChannelNumber);
+ if (dLowSpeedSendData(ChannelNumber, VarsLowSpeed.OutputBuf[ChannelNumber].Buf, VarsLowSpeed.OutputBuf[ChannelNumber].InPtr))
{
if (IOMapLowSpeed.InBuf[ChannelNumber].BytesToRx != 0)
{
@@ -167,16 +244,7 @@ void cLowSpeedCtrl(void)
Temp = dLowSpeedComRxStatus(ChannelNumber);
if (Temp == LOWSPEED_COMMUNICATION_SUCCESS)
{
- for (VarsLowSpeed.InputBuf[ChannelNumber].OutPtr = 0; VarsLowSpeed.InputBuf[ChannelNumber].OutPtr < IOMapLowSpeed.InBuf[ChannelNumber].BytesToRx; VarsLowSpeed.InputBuf[ChannelNumber].OutPtr++)
- {
- IOMapLowSpeed.InBuf[ChannelNumber].Buf[IOMapLowSpeed.InBuf[ChannelNumber].InPtr] = VarsLowSpeed.InputBuf[ChannelNumber].Buf[VarsLowSpeed.InputBuf[ChannelNumber].OutPtr];
- IOMapLowSpeed.InBuf[ChannelNumber].InPtr++;
- if (IOMapLowSpeed.InBuf[ChannelNumber].InPtr >= SIZE_OF_LSBUF)
- {
- IOMapLowSpeed.InBuf[ChannelNumber].InPtr = 0;
- }
- VarsLowSpeed.InputBuf[ChannelNumber].Buf[VarsLowSpeed.InputBuf[ChannelNumber].OutPtr] = 0;
- }
+ cLowSpeedCompleteRead(ChannelNumber);
IOMapLowSpeed.Mode[ChannelNumber] = LOWSPEED_DATA_RECEIVED;
IOMapLowSpeed.ChannelState[ChannelNumber] = LOWSPEED_DONE;
}
@@ -202,27 +270,14 @@ void cLowSpeedCtrl(void)
case LOWSPEED_ERROR:
{
- IOMapLowSpeed.State = IOMapLowSpeed.State & ~LOWSPEED_CH_NUMBER[ChannelNumber];
- if (IOMapLowSpeed.State == 0)
- {
- dLowSpeedStopTimer();
- VarsLowSpeed.TimerState = TIMER_STOPPED;
- }
+ cLowSpeedFinished(ChannelNumber, FALSE);
}
break;
-
case LOWSPEED_DONE:
{
- IOMapLowSpeed.State = IOMapLowSpeed.State & ~LOWSPEED_CH_NUMBER[ChannelNumber];
- IOMapLowSpeed.ChannelState[ChannelNumber] = LOWSPEED_IDLE;
- if (IOMapLowSpeed.State == 0)
- {
- dLowSpeedStopTimer();
- VarsLowSpeed.TimerState = TIMER_STOPPED;
- }
+ cLowSpeedFinished(ChannelNumber, TRUE);
}
break;
-
default:
break;
}