00001
00002
00010
00011
00012
00014
00016 #include <memory.h>
00017 #include <string.h>
00018 #include <stdlib.h>
00019
00020 #ifdef LINUX
00021 #include <unistd.h>
00022 #include <sys/time.h>
00023 #endif
00024
00025 #ifdef WIN32
00026 #include <time.h>
00027 #include <sys/timeb.h>
00028 #endif
00029
00030 #include "com_phy_layer.h"
00031 #include "board_commons.h"
00032
00034
00036
00038
00040
00042
00044
00046
00048 UINT8 ror_bits( UINT8 data, int num_bits)
00049 {
00050 while( num_bits--)
00051 {
00052 data= ( data& 0x01)? (data>> 1)|0x80: (data>> 1);
00053 }
00054 return data;
00055 }
00056
00058
00060 UINT8 swap_bits( UINT8 data)
00061 {
00062 UINT8 tmp= 0x00;
00063 UINT8 msk_l, msk_r;
00064 for( msk_l= 0x80, msk_r= 0x01; msk_l; msk_l>>= 1, msk_r<<= 1)
00065 {
00066 tmp|= (data& msk_l)? msk_r: 0x00;
00067 }
00068 return tmp;
00069 }
00070
00072
00074 void delay(int msec)
00075 {
00076 #if defined (LINUX)
00077 usleep( msec* 1000);
00078 #elif defined (WIN32)
00079 Sleep( msec);
00080 #else
00081 ???
00082 #endif
00083 }
00084
00086
00088 UINT32 get_time()
00089 {
00090 static UINT32 startup= 0;
00091 UINT32 time_ms;
00092 #if defined (LINUX)
00093 struct timeval t1;
00094 struct timezone tz;
00095
00096 gettimeofday(&t1, &tz);
00097 if( !startup) {
00098 startup= (UINT32 )t1.tv_sec;
00099 }
00100 time_ms = (t1.tv_sec- startup) * 1000 + t1.tv_usec / 1000;
00101
00102 #elif defined (WIN32)
00103 struct _timeb timebuffer;
00104
00105 _ftime( &timebuffer );
00106 if( !startup) {
00107 startup= (long)timebuffer.time;
00108 }
00109 time_ms = (UINT32 )( timebuffer.time- startup) * 1000 + (UINT32 )timebuffer.millitm;
00110 #else
00111 ???
00112 #endif
00113
00114 return time_ms;
00115 }
00116
00118
00120
00121 #ifdef WIN32
00122
00123
00124
00125
00126
00127
00128
00129
00130 BOOL WINAPI DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
00131 {
00132 static int m_num_instances= 0;
00133
00134 switch (ul_reason_for_call) {
00135 case DLL_PROCESS_ATTACH:
00136 {
00137 if( !m_num_instances)
00138 {
00139 }
00140
00141 ++m_num_instances;
00142 }
00143 break;
00144 case DLL_PROCESS_DETACH:
00145 --m_num_instances;
00146 break;
00147 case DLL_THREAD_ATTACH:
00148 case DLL_THREAD_DETACH:
00149 break;
00150 }
00151 return TRUE;
00152 }
00153
00154 #else //Linux
00155
00156
00157
00158
00159
00160
00161
00162
00163 void _init( void )
00164 {
00165 }
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 void _fini( void )
00176 {
00177 }
00178
00179 #endif // WIN32
00180
00182
00183
00184
00186
00188
00190 BOOL board_open( board_data* p_data, const CPLConfig *p_com_config, const cmd_table* p_cmd_table)
00191 {
00192 memset( p_data, 0, sizeof( board_data));
00193
00194
00195 p_data->m_p_cmd_table= p_cmd_table;
00196 p_data->m_p_last_sent_cmd= malloc( LAST_SEND_CMD_SIZE);
00197 *p_data->m_p_last_sent_cmd= '\0';
00198
00199 p_data->m_p_cpl_object= malloc( sizeof( CPL_OBJ));
00200
00201 CPL_BiosInit( p_data->m_p_cpl_object);
00202 if( !CPL_Open( p_data->m_p_cpl_object, p_com_config))
00203 {
00204 free( p_data->m_p_cpl_object);
00205 p_data->m_p_cpl_object= (CPL_OBJ*)0;
00206
00207 free( p_data->m_p_last_sent_cmd);
00208 p_data->m_p_last_sent_cmd= (char*)0;
00209
00210 return FALSE;
00211 }
00212 return TRUE;
00213 }
00215
00217 BOOL board_close( board_data* p_data)
00218 {
00219 if( p_data->m_p_cpl_object){
00220 if( !CPL_Close( p_data->m_p_cpl_object))
00221 {
00222 return FALSE;
00223 }
00224 free( p_data->m_p_cpl_object);
00225 p_data->m_p_cpl_object= (CPL_OBJ*)0;
00226 }
00227 if( p_data->m_p_last_sent_cmd) {
00228 free( p_data->m_p_last_sent_cmd);
00229 p_data->m_p_last_sent_cmd= (char*)0;
00230 }
00231 return TRUE;
00232 }
00233
00235
00236
00237
00239
00241
00243 BOOL send_buffer( board_data* p_data, const UINT8* buff, UINT16 buff_size)
00244 {
00245 CPL_Flush( p_data->m_p_cpl_object, BOTH_BUFFER );
00246
00247
00248 if( !buff_size)
00249 return TRUE;
00250 while( buff_size--)
00251 {
00252 if( !CPL_direct_TX( p_data->m_p_cpl_object, buff, 1))
00253 return FALSE;
00254 buff++;
00255 Sleep( 50);
00256 }
00257 return TRUE;
00258 }
00259
00261
00263 BOOL receive_buffer( board_data* p_data, UINT8* buff, UINT16 *buff_size, int timeout_msec)
00264 {
00265 UINT16 bytes_to_read= *buff_size;
00266 int count_empty= 200;
00267
00268 timeout_msec/= 10;
00269 if( !timeout_msec)
00270 {
00271 timeout_msec= 10;
00272 }
00273
00274 *buff_size= 0;
00275 while( (*buff_size< bytes_to_read)&& ( count_empty--))
00276 {
00277 UINT16 bytes_read= bytes_to_read- *buff_size;
00278
00279 if( !CPL_direct_RX( p_data->m_p_cpl_object, buff+ *buff_size, &bytes_read))
00280 {
00281 return FALSE;
00282 }
00283 if( bytes_read)
00284 {
00285 *buff_size+= bytes_read;
00286 count_empty= timeout_msec;
00287 }
00288 else
00289 {
00290 Sleep( 10);
00291 }
00292 }
00293 return TRUE;
00294 }
00295
00297
00298
00299
00301
00303
00305 BOOL send_cmd( board_data* p_data, int cmd_index, int cmd_value)
00306 {
00307 if( ( cmd_value< p_data->m_p_cmd_table[ cmd_index].m_min_value)||
00308 ( cmd_value> p_data->m_p_cmd_table[ cmd_index].m_max_value))
00309 {
00310 return FALSE;
00311 }
00312 sprintf( p_data->m_p_last_sent_cmd, "%s%d\r", p_data->m_p_cmd_table[ cmd_index].m_cmd_string, cmd_value);
00313 return send_buffer( p_data, (UINT8*)p_data->m_p_last_sent_cmd, (UINT16)strlen( p_data->m_p_last_sent_cmd));
00314 }
00315
00317
00318
00319
00321
00323
00324
00325