aboutsummaryrefslogtreecommitdiff
path: root/AT91SAM7S256/Source/c_lowspeed.c
blob: de0cf77ec7d9622d80f1c8a8d6003d1e063e8412 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//
// Date init       14.12.2004
//
// Revision date   $Date:: 14-11-07 12:40                                    $
//
// Filename        $Workfile:: c_lowspeed.c                                  $
//
// Version         $Revision:: 1                                             $
//
// Archive         $Archive:: /LMS2006/Sys01/Main_V02/Firmware/Source/c_lows $
//
// Platform        C
//

#include  "stdconst.h"
#include  "modules.h"
#include  "c_lowspeed.iom"
#include  "c_input.iom"
#include  "c_lowspeed.h"
#include  "d_lowspeed.h"

static    IOMAPLOWSPEED   IOMapLowSpeed;
static    VARSLOWSPEED    VarsLowSpeed;
static    HEADER          **pHeaders;

const UBYTE LOWSPEED_CH_NUMBER[4] = {0x01, 0x02, 0x04, 0x08};

const     HEADER          cLowSpeed =
{
  0x000B0001L,
  "Low Speed",
  cLowSpeedInit,
  cLowSpeedCtrl,
  cLowSpeedExit,
  (void *)&IOMapLowSpeed,
  (void *)&VarsLowSpeed,
  (UWORD)sizeof(IOMapLowSpeed),
  (UWORD)sizeof(VarsLowSpeed),
  0x0000                      //Code size - not used so far
};

void      cLowSpeedInit(void* pHeader)
{
  pHeaders        = pHeader;

  dLowSpeedInit();
  IOMapLowSpeed.State = COM_CHANNEL_NONE_ACTIVE;
  VarsLowSpeed.TimerState = TIMER_STOPPED;
}

void      cLowSpeedCtrl(void)
{
  UBYTE Temp;
  UBYTE ChannelNumber = 0;
    	
  if (IOMapLowSpeed.State != 0)
  {
    for (ChannelNumber = 0; ChannelNumber < NO_OF_LOWSPEED_COM_CHANNEL; ChannelNumber++)
    {
      //Lowspeed com is activated
      switch (IOMapLowSpeed.ChannelState[ChannelNumber])			
      {
        case LOWSPEED_IDLE:
          {
          }
          break;
		
        case LOWSPEED_INIT:
          {
            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);			
            }
            else
            {
              IOMapLowSpeed.ChannelState[ChannelNumber] = LOWSPEED_ERROR;
              IOMapLowSpeed.ErrorType[ChannelNumber] = LOWSPEED_CH_NOT_READY;
            }
          }
          break;
			
        case LOWSPEED_LOAD_BUFFER:
          {
            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)))
              {
                if (IOMapLowSpeed.InBuf[ChannelNumber].BytesToRx != 0)
                {
                  dLowSpeedReceiveData(ChannelNumber, &VarsLowSpeed.InputBuf[ChannelNumber].Buf[0], IOMapLowSpeed.InBuf[ChannelNumber].BytesToRx, IOMapLowSpeed.NoRestartOnRead);
                  VarsLowSpeed.RxTimeCnt[ChannelNumber] = 0;
                }
                IOMapLowSpeed.ChannelState[ChannelNumber] = LOWSPEED_COMMUNICATING;
                IOMapLowSpeed.Mode[ChannelNumber] = LOWSPEED_TRANSMITTING;
              }
              else
              {
                IOMapLowSpeed.ChannelState[ChannelNumber] = LOWSPEED_ERROR;
                IOMapLowSpeed.ErrorType[ChannelNumber] = LOWSPEED_CH_NOT_READY;
              }
            }
            else
            {
              IOMapLowSpeed.ChannelState[ChannelNumber] = LOWSPEED_ERROR;
              IOMapLowSpeed.ErrorType[ChannelNumber] = LOWSPEED_CH_NOT_READY;
            }
          }
          break;
			
        case LOWSPEED_COMMUNICATING:
          {
            if ((pMapInput->Inputs[ChannelNumber].SensorType == LOWSPEED) || (pMapInput->Inputs[ChannelNumber].SensorType == LOWSPEED_9V))
            {
              if (IOMapLowSpeed.Mode[ChannelNumber] == LOWSPEED_TRANSMITTING)
              {
                Temp = dLowSpeedComTxStatus(ChannelNumber);			// Returns 0x00 if not done, 0x01 if success, 0xFF if error
                  
                if (Temp == LOWSPEED_COMMUNICATION_SUCCESS)
                {
                  if (IOMapLowSpeed.InBuf[ChannelNumber].BytesToRx != 0)
                  {
                    IOMapLowSpeed.Mode[ChannelNumber] = LOWSPEED_RECEIVING;							    			
                  }
                  else
                  {
                    IOMapLowSpeed.Mode[ChannelNumber] = LOWSPEED_DATA_RECEIVED;
                    IOMapLowSpeed.ChannelState[ChannelNumber] = LOWSPEED_DONE;
                  }
                }
                if (Temp == LOWSPEED_COMMUNICATION_ERROR)
                {
                  //ERROR in Communication, No ACK received from SLAVE, retry send data 3 times!
                  VarsLowSpeed.ErrorCount[ChannelNumber]++;
                  if (VarsLowSpeed.ErrorCount[ChannelNumber] > MAX_RETRY_TX_COUNT)
                  {
                    IOMapLowSpeed.ChannelState[ChannelNumber] = LOWSPEED_ERROR;
                    IOMapLowSpeed.ErrorType[ChannelNumber] = LOWSPEED_TX_ERROR;
                  }
                  else
                  {
                    IOMapLowSpeed.ChannelState[ChannelNumber] = LOWSPEED_LOAD_BUFFER;
                  }
                }			
              }
              if (IOMapLowSpeed.Mode[ChannelNumber] == LOWSPEED_RECEIVING)
              {
                VarsLowSpeed.RxTimeCnt[ChannelNumber]++;
                if (VarsLowSpeed.RxTimeCnt[ChannelNumber] > LOWSPEED_RX_TIMEOUT)
                {
                  IOMapLowSpeed.ChannelState[ChannelNumber] = LOWSPEED_ERROR;
                  IOMapLowSpeed.ErrorType[ChannelNumber] = LOWSPEED_RX_ERROR;
                }
                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;
                  }
                  IOMapLowSpeed.Mode[ChannelNumber] = LOWSPEED_DATA_RECEIVED;
                  IOMapLowSpeed.ChannelState[ChannelNumber] = LOWSPEED_DONE;
                }
                if (Temp == LOWSPEED_COMMUNICATION_ERROR)
                {
                  //There was and error in receiving data from the device
                  for (VarsLowSpeed.InputBuf[ChannelNumber].OutPtr = 0; VarsLowSpeed.InputBuf[ChannelNumber].OutPtr < IOMapLowSpeed.InBuf[ChannelNumber].BytesToRx; VarsLowSpeed.InputBuf[ChannelNumber].OutPtr++)
                  {
                    VarsLowSpeed.InputBuf[ChannelNumber].Buf[VarsLowSpeed.InputBuf[ChannelNumber].OutPtr] = 0;
                  }
                  IOMapLowSpeed.ChannelState[ChannelNumber] = LOWSPEED_ERROR;
                  IOMapLowSpeed.ErrorType[ChannelNumber] = LOWSPEED_RX_ERROR;
                }              
              }
            }
            else
            {
              IOMapLowSpeed.ChannelState[ChannelNumber] = LOWSPEED_ERROR;
              IOMapLowSpeed.ErrorType[ChannelNumber] = LOWSPEED_CH_NOT_READY;
            }          
          }
          break;	
			
        case LOWSPEED_ERROR:
        {
          IOMapLowSpeed.State = IOMapLowSpeed.State & ~LOWSPEED_CH_NUMBER[ChannelNumber];			
		      if (IOMapLowSpeed.State == 0)
		      {
		        dLowSpeedStopTimer();
		        VarsLowSpeed.TimerState = TIMER_STOPPED;
		      }
        }
        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;
		      }			
	      }
	      break;
			
	      default:
	   	    break;
	    }
    }
  }	
}

void      cLowSpeedExit(void)
{
  dLowSpeedExit();
}