]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/pdk.git/blob - packages/ti/transport/ndk/nimu/example/CCLink/cclink_master/sample/src/CCIEF_BASIC_SLAVES.c
transport: add to PDK
[processor-sdk/pdk.git] / packages / ti / transport / ndk / nimu / example / CCLink / cclink_master / sample / src / CCIEF_BASIC_SLAVES.c
1 /*\r
2  * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/\r
3  * Copyright (C) 2016 CC-Link Partner Association -http://am.cc-link.org/\r
4  * \r
5  * \r
6  *  Redistribution and use in source and binary forms, with or without \r
7  *  modification, are permitted provided that the following conditions \r
8  *  are met:\r
9  *\r
10  *    Redistributions of source code must retain the above copyright \r
11  *    notice, this list of conditions and the following disclaimer.\r
12  *\r
13  *    Redistributions in binary form must reproduce the above copyright\r
14  *    notice, this list of conditions and the following disclaimer in the \r
15  *    documentation and/or other materials provided with the   \r
16  *    distribution.\r
17  *\r
18  *    Neither the name of Texas Instruments Incorporated nor the names of\r
19  *    its contributors may be used to endorse or promote products derived\r
20  *    from this software without specific prior written permission.\r
21  *\r
22  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \r
23  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT \r
24  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r
25  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT \r
26  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, \r
27  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT \r
28  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
29  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
30  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
31  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE \r
32  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
33  *\r
34 */\r
35 \r
36 #include "CCIEF_BASIC_SLAVES.h"\r
37 #include <string.h>\r
38 \r
39 #ifdef __TIRTOS__ \r
40 #include <ti/ndk/inc/stkmain.h>\r
41 #include <ti/ndk/inc/netmain.h>\r
42 #include <ti/ndk/inc/socket.h>\r
43 #include <ti/drv/uart/UART.h>\r
44 #include <ti/drv/uart/UART_stdio.h>\r
45 #include <ti/drv/uart/src/UART_osal.h>\r
46 #include <ti/drv/uart/soc/UART_soc.h>\r
47 #include <ti/drv/uart/src/UART_utils_defs.h>\r
48 #include <ti/drv/uart/test/src/UART_board.h>\r
49 \r
50 extern uint32_t display_slave_connection_status;\r
51 \r
52 #endif\r
53 \r
54 /* Definition of function of sample program */\r
55 static void ccief_basic_slaves_execute_state_disconnect( CCIEF_BASIC_SLAVES_CYCLIC_DATA_INFO *pSlave, int iEvent );\r
56 static void ccief_basic_slaves_execute_state_connecting( CCIEF_BASIC_SLAVES_CYCLIC_DATA_INFO *pSlave, int iEvent );\r
57 static void ccief_basic_slaves_execute_state_cyclic_stop( CCIEF_BASIC_SLAVES_CYCLIC_DATA_INFO *pSlave, int iEvent );\r
58 static void ccief_basic_slaves_execute_state_cyclic_end( CCIEF_BASIC_SLAVES_CYCLIC_DATA_INFO *pSlave, int iEvent );\r
59 static void ccief_basic_slaves_execute_state_cyclic( CCIEF_BASIC_SLAVES_CYCLIC_DATA_INFO *pSlave, int iEvent );\r
60 \r
61 /************************************************************************************/\r
62 /* This is an user defined function for initialization of the slave.                            */\r
63 /************************************************************************************/\r
64 void ccief_basic_slaves_initialize( CCIEF_BASIC_SLAVES_CYCLIC_DATA_INFO *pSlave )\r
65 {\r
66         /* Set the slave state */\r
67         pSlave->iState = CCIEF_BASIC_STATE_SLAVE_DISCONNECT;\r
68 \r
69         return;\r
70 }\r
71 \r
72 /************************************************************************************/\r
73 /* This is an user defined function for executed according to the state                         */\r
74 /* of the slave.                                                                                                                                        */\r
75 /************************************************************************************/\r
76 void ccief_basic_slaves_execute_state( CCIEF_BASIC_SLAVES_CYCLIC_DATA_INFO *pSlave, int iEvent )\r
77 {\r
78         /* Execute the state of the slave */\r
79         switch ( pSlave->iState )\r
80         {\r
81                 /* Disconnection of the cyclic network. */\r
82                 case CCIEF_BASIC_STATE_SLAVE_DISCONNECT:\r
83                         ccief_basic_slaves_execute_state_disconnect( pSlave, iEvent );\r
84                         break;\r
85                 /* Connecting for the cyclic network. */\r
86                 case CCIEF_BASIC_STATE_SLAVE_CONNECTING:\r
87                         ccief_basic_slaves_execute_state_connecting( pSlave, iEvent );\r
88                         break;\r
89                 /* Stop the cyclic operation. */\r
90                 case CCIEF_BASIC_STATE_SLAVE_CYCLIC_STOP:\r
91                         ccief_basic_slaves_execute_state_cyclic_stop( pSlave, iEvent );\r
92                         break;\r
93                 /* End of the cyclic operation. */\r
94                 case CCIEF_BASIC_STATE_SLAVE_CYCLIC_END:\r
95                         ccief_basic_slaves_execute_state_cyclic_end( pSlave, iEvent );\r
96                         break;\r
97                 /* Running the cyclic operation. */\r
98                 case CCIEF_BASIC_STATE_SLAVE_CYCLIC:\r
99                         ccief_basic_slaves_execute_state_cyclic( pSlave, iEvent );\r
100                         break;\r
101 \r
102                 default:\r
103                         break;\r
104         }\r
105 \r
106         return;\r
107 }\r
108 \r
109 /************************************************************************************/\r
110 /* This is an user defined function for state of disconnect of the cyclic network.      */\r
111 /************************************************************************************/\r
112 void ccief_basic_slaves_execute_state_disconnect( CCIEF_BASIC_SLAVES_CYCLIC_DATA_INFO *pSlave, int iEvent )\r
113 {\r
114         /* Check the event */\r
115         switch ( iEvent )\r
116         {\r
117                 /* Changing the parameter */\r
118                 case CCIEF_BASIC_EVENT_SLAVE_PARAMETER_CHANGE:\r
119                         /* Nothing */\r
120                         break;\r
121                 /* Make the cyclic data */\r
122                 case CCIEF_BASIC_EVENT_SLAVE_LINK_SCAN_START:\r
123                         /* Set the cyclic state */\r
124                         pSlave->iCyclicState = CCIEF_BASIC_CYCLIC_STATE_OFF;\r
125                         /* Check the start cyclic of user operation */\r
126                         if ( pSlave->iCyclicStart == CCIEF_BASIC_CYCLIC_START )\r
127                         {\r
128                                 /* Set the cyclic state to connecting */\r
129                                 pSlave->iCyclicStateSet = CCIEF_BASIC_CYCLIC_STATE_SET_CONNECTING;\r
130                                 /* Initialize of the timeout count */\r
131                                 pSlave->usTimeoutCount = 0;\r
132                                 /* Set the slave state */\r
133                                 pSlave->iState = CCIEF_BASIC_STATE_SLAVE_CONNECTING;\r
134                         }\r
135                         else\r
136                         {\r
137                                 /* Set the cyclic state to stop cyclic */\r
138                                 pSlave->iCyclicStateSet = CCIEF_BASIC_CYCLIC_STATE_SET_CYCLIC_STOP;\r
139                                 /* Set the slave state */\r
140                                 pSlave->iState = CCIEF_BASIC_STATE_SLAVE_CYCLIC_STOP;\r
141                         }\r
142                         break;\r
143                 /* Receiving the error of the duplication master from the slave */\r
144                 case CCIEF_BASIC_EVENT_SLAVE_DUPLICATION_MASTER_ERR:\r
145                         /* Nothing */\r
146                         break;\r
147 \r
148                 default:\r
149                         break;\r
150         }\r
151 \r
152         return;\r
153 }\r
154 \r
155 /************************************************************************************/\r
156 /* This is an user defined function for state of connecting for the cyclic network.     */\r
157 /************************************************************************************/\r
158 void ccief_basic_slaves_execute_state_connecting( CCIEF_BASIC_SLAVES_CYCLIC_DATA_INFO *pSlave, int iEvent )\r
159 {\r
160         /* Check the event */\r
161         switch ( iEvent )\r
162         {\r
163                 /* Changing the parameter */\r
164                 case CCIEF_BASIC_EVENT_SLAVE_PARAMETER_CHANGE:\r
165                         /* Set the slave state */\r
166                         pSlave->iState = CCIEF_BASIC_STATE_SLAVE_DISCONNECT;\r
167                         break;\r
168                 /* Receiving the cyclic data from the slave */\r
169                 case CCIEF_BASIC_EVENT_SLAVE_CYCLIC_DATA_RECV:\r
170                         /* Check the End code of the slave */\r
171                         if ( pSlave->usEndCode == CCIEF_BASIC_END_OK )\r
172                         {\r
173                                 /* Set the slave state */\r
174                                 pSlave->iState = CCIEF_BASIC_STATE_SLAVE_CYCLIC_END;\r
175                         }\r
176                         else\r
177                         {\r
178                                 /* Set the slave state */\r
179                                 pSlave->iState = CCIEF_BASIC_STATE_SLAVE_DISCONNECT;\r
180 #ifdef __TIRTOS__\r
181                                 if (display_slave_connection_status > 0)\r
182                                 {\r
183                                     UART_printf( "Slave Id %08X Status: disconnected\n", pSlave->ulId);\r
184                                     display_slave_connection_status = 2;\r
185                                 }\r
186 #endif\r
187                         }\r
188                         break;\r
189                 /* Timeout the response of the cyclic data */\r
190                 case CCIEF_BASIC_EVENT_SLAVE_CYCLIC_DATA_TIMEOUT:\r
191                         /* Set the slave state */\r
192                         pSlave->iState = CCIEF_BASIC_STATE_SLAVE_DISCONNECT;\r
193 #ifdef __TIRTOS__\r
194                                 if (display_slave_connection_status  > 0)\r
195                                 {\r
196                                     UART_printf( "Slave Id %08X Status: disconnected\n", pSlave->ulId);\r
197                                     display_slave_connection_status = 2;\r
198                                 }\r
199 #endif\r
200                         break;\r
201                 /* The end of the cyclic data of all slaves */\r
202                 case CCIEF_BASIC_EVENT_SLAVE_CYCLIC_END:\r
203                         /* Set the slave state */\r
204                         pSlave->iState = CCIEF_BASIC_STATE_SLAVE_DISCONNECT;\r
205 #ifdef __TIRTOS__\r
206                                 if (display_slave_connection_status > 0)\r
207                                 {\r
208                                     UART_printf( "Slave Id %08X Status: disconnected\n", pSlave->ulId);\r
209                                     display_slave_connection_status = 2;\r
210                                 }\r
211 #endif\r
212                         break;\r
213                 /* Receiving the error of the duplication master from the slave */\r
214                 case CCIEF_BASIC_EVENT_SLAVE_DUPLICATION_MASTER_ERR:\r
215                         /* Set the slave state */\r
216                         pSlave->iState = CCIEF_BASIC_STATE_SLAVE_DISCONNECT;\r
217                         break;\r
218 \r
219                 default:\r
220                         break;\r
221         }\r
222 \r
223         return;\r
224 }\r
225 \r
226 /************************************************************************************/\r
227 /* This is an user defined function for state of stop the cyclic operation.                     */\r
228 /************************************************************************************/\r
229 void ccief_basic_slaves_execute_state_cyclic_stop( CCIEF_BASIC_SLAVES_CYCLIC_DATA_INFO *pSlave, int iEvent )\r
230 {\r
231         /* Check the event */\r
232         switch ( iEvent )\r
233         {\r
234                 /* Changing the parameter */\r
235                 case CCIEF_BASIC_EVENT_SLAVE_PARAMETER_CHANGE:\r
236                         /* Set the slave state */\r
237                         pSlave->iState = CCIEF_BASIC_STATE_SLAVE_DISCONNECT;\r
238                         break;\r
239                 /* Timeout the response of the cyclic data */\r
240                 case CCIEF_BASIC_EVENT_SLAVE_CYCLIC_DATA_TIMEOUT:\r
241                         /* Set the slave state */\r
242                         pSlave->iState = CCIEF_BASIC_STATE_SLAVE_DISCONNECT;\r
243                         break;\r
244                 /* The end of the cyclic data of all slaves */\r
245                 case CCIEF_BASIC_EVENT_SLAVE_CYCLIC_END:\r
246                         /* Set the slave state */\r
247                         pSlave->iState = CCIEF_BASIC_STATE_SLAVE_DISCONNECT;\r
248                         break;\r
249                 /* Receiving the error of the duplication master from the slave */\r
250                 case CCIEF_BASIC_EVENT_SLAVE_DUPLICATION_MASTER_ERR:\r
251                         /* Set the slave state */\r
252                         pSlave->iState = CCIEF_BASIC_STATE_SLAVE_DISCONNECT;\r
253                         break;\r
254 \r
255                 default:\r
256                         break;\r
257         }\r
258 \r
259         return;\r
260 }\r
261 \r
262 /************************************************************************************/\r
263 /* This is an user defined function for state of end of the cyclic operation.           */\r
264 /************************************************************************************/\r
265 void ccief_basic_slaves_execute_state_cyclic_end( CCIEF_BASIC_SLAVES_CYCLIC_DATA_INFO *pSlave, int iEvent )\r
266 {\r
267         /* Check the event */\r
268         switch ( iEvent )\r
269         {\r
270                 /* Changing the parameter */\r
271                 case CCIEF_BASIC_EVENT_SLAVE_PARAMETER_CHANGE:\r
272                         /* Set the slave state */\r
273                         pSlave->iState = CCIEF_BASIC_STATE_SLAVE_DISCONNECT;\r
274                         break;\r
275                 /* Make the cyclic data */\r
276                 case CCIEF_BASIC_EVENT_SLAVE_LINK_SCAN_START:\r
277                         /* Check the start cyclic of user operation */\r
278                         if ( pSlave->iCyclicStart == CCIEF_BASIC_CYCLIC_START )\r
279                         {\r
280                                 /* Set the cyclic state */\r
281                                 pSlave->iCyclicState = CCIEF_BASIC_CYCLIC_STATE_ON;\r
282                                 /* Set the cyclic state to cyclic */\r
283                                 pSlave->iCyclicStateSet = CCIEF_BASIC_CYCLIC_STATE_SET_CYCLIC;\r
284                                 /* Set the slave state */\r
285                                 pSlave->iState = CCIEF_BASIC_STATE_SLAVE_CYCLIC;\r
286                         }\r
287                         else\r
288                         {\r
289                                 /* Set the cyclic state to stop cyclic */\r
290                                 pSlave->iCyclicStateSet = CCIEF_BASIC_CYCLIC_STATE_SET_CYCLIC_STOP;\r
291                                 /* Set the slave state */\r
292                                 pSlave->iState = CCIEF_BASIC_STATE_SLAVE_CYCLIC_STOP;\r
293                         }\r
294                         break;\r
295                 /* Receiving the error of the duplication master from the slave */\r
296                 case CCIEF_BASIC_EVENT_SLAVE_DUPLICATION_MASTER_ERR:\r
297                         /* Set the slave state */\r
298                         pSlave->iState = CCIEF_BASIC_STATE_SLAVE_DISCONNECT;\r
299                         break;\r
300                 /* Receiving the cyclic data from the slave */\r
301                 case CCIEF_BASIC_EVENT_SLAVE_CYCLIC_DATA_RECV:\r
302                         /* Check the response received */\r
303                         if( pSlave->iReceiveComplete == CCIEF_BASIC_SLAVE_RESPONSE_RECEIVED )\r
304                         {\r
305                                 /* Already receved response */\r
306                                 /* Set the error code of the slave duplication */\r
307                                 pSlave->iDuplicateState = CCIEF_BASIC_SLAVE_DUPLICATION_DETECT;\r
308                         }\r
309                         break;\r
310                 default:\r
311                         break;\r
312         }\r
313 \r
314         return;\r
315 }\r
316 \r
317 /************************************************************************************/\r
318 /* This is an user defined function for state of running the cyclic operation.          */\r
319 /************************************************************************************/\r
320 void ccief_basic_slaves_execute_state_cyclic( CCIEF_BASIC_SLAVES_CYCLIC_DATA_INFO *pSlave, int iEvent )\r
321 {\r
322         /* Check the event */\r
323         switch ( iEvent )\r
324         {\r
325                 /* Changing the parameter */\r
326                 case CCIEF_BASIC_EVENT_SLAVE_PARAMETER_CHANGE:\r
327                         /* Set the slave state */\r
328                         pSlave->iState = CCIEF_BASIC_STATE_SLAVE_DISCONNECT;\r
329                         break;\r
330                 /* Receiving the cyclic data from the slave */\r
331                 case CCIEF_BASIC_EVENT_SLAVE_CYCLIC_DATA_RECV:\r
332                         /* Get the current number of frame sequence */\r
333                         /* Check the end code of the slave */\r
334                         if ( pSlave->usEndCode == CCIEF_BASIC_END_OK )\r
335                         {\r
336                                 /* Check the number of frame sequence */\r
337                                 if ( *pSlave->pusFrameSequenceNumber == pSlave->usFrameSequenceNumber )\r
338                                 {\r
339                                         /* Receive the response data from the slave */\r
340                                         /* RWr */\r
341                                         memcpy( pSlave->pusRWr, pSlave->pusSlaveRWr, pSlave->pParameter->usOccupiedStationNumber * CCIEF_BASIC_RWW_RWR_SIZE );\r
342                                         /* RX */\r
343                                         memcpy( pSlave->pusRX, pSlave->pusSlaveRX, pSlave->pParameter->usOccupiedStationNumber * CCIEF_BASIC_RX_RY_SIZE );\r
344                                         /* Initialize of the timeout count */\r
345                                         pSlave->usTimeoutCount = 0;\r
346                                         /* Set the slave state */\r
347                                         pSlave->iState = CCIEF_BASIC_STATE_SLAVE_CYCLIC_END;\r
348                                         \r
349                                         /* Set the receive complete flag  */\r
350                                         pSlave->iReceiveComplete = CCIEF_BASIC_SLAVE_RESPONSE_RECEIVED;\r
351                                 }\r
352                                 else\r
353                                 {\r
354                                         /* Nothing */\r
355                                 }\r
356                         }\r
357                         else\r
358                         {\r
359                                 /* Set the slave state */\r
360                                 pSlave->iState = CCIEF_BASIC_STATE_SLAVE_DISCONNECT;\r
361                         }\r
362                         break;\r
363                 /* Timeout the response of the cyclic data */\r
364                 case CCIEF_BASIC_EVENT_SLAVE_CYCLIC_DATA_TIMEOUT:\r
365                         /* Increase of the timeout count */\r
366                         pSlave->usTimeoutCount ++;\r
367                         /* Check of the timeout count */\r
368                         if ( pSlave->usTimeoutCount < pSlave->usCyclicTransmissionTimeoutCount )\r
369                         {\r
370                                 /* Set the slave state */\r
371                                 pSlave->iState = CCIEF_BASIC_STATE_SLAVE_CYCLIC_END;\r
372                         }\r
373                         else\r
374                         {\r
375                                 /* Set the slave state */\r
376                                 pSlave->iState = CCIEF_BASIC_STATE_SLAVE_DISCONNECT;\r
377 #ifdef __TIRTOS__\r
378                                 if (display_slave_connection_status > 0)\r
379                                 {\r
380                                     UART_printf( "Slave Id %08X Status: disconnected\n", pSlave->ulId);\r
381                                     display_slave_connection_status = 2;\r
382                                 }\r
383 #endif\r
384                         }\r
385                         break;\r
386                 /* Receiving the error of the duplication master from the slave */\r
387                 case CCIEF_BASIC_EVENT_SLAVE_DUPLICATION_MASTER_ERR:\r
388                         /* Set the slave state */\r
389                         pSlave->iState = CCIEF_BASIC_STATE_SLAVE_DISCONNECT;\r
390                         break;\r
391 \r
392                 default:\r
393                         break;\r
394         }\r
395 \r
396         return;\r
397 }\r