]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/devs/can/m68k/mcf52xx/v2_0/tests/flexcan_wake.c
Initial revision
[karo-tx-redboot.git] / packages / devs / can / m68k / mcf52xx / v2_0 / tests / flexcan_wake.c
1 //==========================================================================
2 //
3 //        flexcan_wake.c
4 //
5 //        FlexCAN wake test
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12 //
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
16 //
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 //
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
32 //
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 //
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
42 //
43 // Author(s):     Uwe Kindler
44 // Contributors:  Uwe Kindler
45 // Date:          2005-09-10
46 // Description:   FlexCAN test of standby and wake
47 //####DESCRIPTIONEND####
48
49
50 //===========================================================================
51 //                                INCLUDES
52 //===========================================================================
53 #include <pkgconf/system.h>
54
55 #include <cyg/infra/testcase.h>         // test macros
56 #include <cyg/infra/cyg_ass.h>          // assertion macros
57 #include <cyg/infra/diag.h>
58
59 // Package requirements
60 #if defined(CYGPKG_IO_CAN) && defined(CYGPKG_KERNEL)
61
62 #include <pkgconf/kernel.h>
63 #include <cyg/io/io.h>
64 #include <cyg/io/canio.h>
65
66 // Package option requirements
67 #if defined(CYGFUN_KERNEL_API_C)
68
69 #include <cyg/hal/hal_arch.h>           // CYGNUM_HAL_STACK_SIZE_TYPICAL
70 #include <cyg/kernel/kapi.h>
71
72
73 //===========================================================================
74 //                               DATA TYPES
75 //===========================================================================
76 typedef struct st_thread_data
77 {
78     cyg_thread   obj;
79     long         stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
80     cyg_handle_t hdl;
81 } thread_data_t;
82
83
84 //===========================================================================
85 //                              LOCAL DATA
86 //===========================================================================
87 cyg_thread_entry_t can0_thread;
88 thread_data_t      can0_thread_data;
89
90
91 cyg_io_handle_t    hDrvFlexCAN;
92
93
94 //===========================================================================
95 //                          LOCAL FUNCTIONS
96 //===========================================================================
97 #include "can_test_aux.inl" // include CAN test auxiliary functions
98
99
100 //===========================================================================
101 //                             READER THREAD 
102 //===========================================================================
103 void can0_thread(cyg_addrword_t data)
104 {
105     cyg_uint32             len;
106     cyg_can_event          rx_event1;
107     cyg_can_event          rx_event2;
108     cyg_can_msgbuf_info    msgbox_info;
109     cyg_can_mode           mode; 
110     cyg_can_state          state;
111     
112    
113     diag_printf("Test of FlexCAN standby mode with selfwakeup\n"
114                 "As soon as a message arrives the FlexCAN modul\n"
115                 "will leave standby and generates a leave standby event.\n"
116                 "Each time you send a message you should see LSTY first\n"
117                 "for \"leaving standby\" and then \"RX\" for the\n"
118                 "RX event that caused the leave standby event. You can send\n"
119                 "a CAN data frame with any ID\n");
120                 
121     diag_printf("!!! This test can be stopped by sending a data frame with ID 0x100 !!!\n\n");
122     
123     len = sizeof(msgbox_info);
124     if (ENOERR != cyg_io_get_config(hDrvFlexCAN, CYG_IO_GET_CONFIG_CAN_MSGBUF_INFO ,&msgbox_info, &len))
125     {
126         CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
127     } 
128     else
129     {
130         diag_printf("Message boxes available: %d    free: %d\n", 
131                     msgbox_info.count, msgbox_info.free);
132     }
133     
134     while (1)
135     {
136        //
137        // now we set FlexCAN into standby mode
138        //
139        mode = CYGNUM_CAN_MODE_STANDBY;
140        len = sizeof(mode);
141        if (ENOERR != cyg_io_set_config(hDrvFlexCAN, CYG_IO_SET_CONFIG_CAN_MODE ,&mode, &len))
142        {
143            CYG_TEST_FAIL_FINISH("Error writing config of /dev/can0");
144        } 
145     
146        //
147        // now check if FlexCAN modul is really in standby mode
148        //
149        len = sizeof(state);
150        if (ENOERR != cyg_io_get_config(hDrvFlexCAN, CYG_IO_GET_CONFIG_CAN_STATE ,&state, &len))
151        {
152            CYG_TEST_FAIL_FINISH("Error reading config of /dev/can0");
153        } 
154     
155    
156        if (state != CYGNUM_CAN_STATE_STANDBY)
157        {
158            CYG_TEST_FAIL_FINISH("Error stopping FlexCAN /dev/can0");
159        }
160        
161        //
162        // as soon as a message arrives the FlexCAN modul leaves standby mode
163        // and we should receive a CYGNUM_CAN_EVENT_LEAVING_STANDBY event but
164        // we will also receive a RX event because a message arrived
165        // 
166        len = sizeof(rx_event1);      
167        if (ENOERR != cyg_io_read(hDrvFlexCAN, &rx_event1, &len))
168        {
169            CYG_TEST_FAIL_FINISH("Error reading from /dev/can0");
170        }
171        
172        len = sizeof(rx_event2);      
173        if (ENOERR != cyg_io_read(hDrvFlexCAN, &rx_event2, &len))
174        {
175            CYG_TEST_FAIL_FINISH("Error reading from /dev/can0");
176        }
177        
178        print_can_flags(rx_event1.flags, "");
179        print_can_flags(rx_event2.flags, "");
180        
181        //
182        // The first event we receive should be a leaving standby event because
183        // first flexcan leaves standby and then a message will be received
184        // 
185        if (!(rx_event1.flags & CYGNUM_CAN_EVENT_LEAVING_STANDBY))
186        {
187            CYG_TEST_FAIL_FINISH("CYGNUM_CAN_EVENT_LEAVING_STANDBY event expexted /dev/can0");
188        }
189        
190        if (rx_event2.msg.id == 0x100)
191        {
192            CYG_TEST_PASS_FINISH("flexcan_wake test OK");
193        }
194     }              
195 }
196
197
198 void
199 cyg_start(void)
200 {
201     CYG_TEST_INIT();
202     
203     //
204     // open flexcan device driver
205     //
206     if (ENOERR != cyg_io_lookup("/dev/can0", &hDrvFlexCAN)) 
207     {
208         CYG_TEST_FAIL_FINISH("Error opening /dev/can0");
209     }
210     
211    
212     //
213     // create the two threads which access the CAN device driver
214     // a reader thread with a higher priority and a writer thread
215     // with a lower priority
216     //
217     cyg_thread_create(4, can0_thread, 
218                         (cyg_addrword_t) 0,
219                                 "can0_thread", 
220                                 (void *) can0_thread_data.stack, 
221                                 1024 * sizeof(long),
222                                 &can0_thread_data.hdl, 
223                                 &can0_thread_data.obj);
224                                 
225     cyg_thread_resume(can0_thread_data.hdl);
226     
227     cyg_scheduler_start();
228 }
229
230 #else // CYGFUN_KERNEL_API_C
231 #define N_A_MSG "Needs kernel C API"
232 #endif
233
234 #else // CYGPKG_IO_CAN && CYGPKG_KERNEL
235 #define N_A_MSG "Needs IO/CAN and Kernel"
236 #endif
237
238 #ifdef N_A_MSG
239 void
240 cyg_start( void )
241 {
242     CYG_TEST_INIT();
243     CYG_TEST_NA( N_A_MSG);
244 }
245 #endif // N_A_MSG
246
247 // EOF flexcan_wake.c