25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

368 lines
10 KiB

  1. /*******************************************************************************
  2. * *
  3. * Copyright 2010 Rheinmetall Canada Inc. *
  4. * *
  5. * No part of this document may be reproduced, stored in *
  6. * a retrieval system, or transmitted, in any form or by any means, *
  7. * electronic, mechanical, photocopying, recording, or otherwise, *
  8. * without the prior written permission of Rheinmetall Canada Inc. *
  9. * *
  10. *******************************************************************************/
  11. /*
  12. Description:
  13. This is a template file for standard C code file.
  14. */
  15. /* ************************************************************************** */
  16. /* ¤Revision:
  17. 000 20100616 JFM,
  18. Original version.
  19. ### YYYYMMDD Initial, Bug Identification
  20. Change description.
  21. */
  22. /* ************************************************************************** */
  23. /* Includes */
  24. #include "define.h"
  25. #include "Util.h"
  26. #include <string.h>
  27. #include <stdio.h>
  28. #include "PrintfServer.h"
  29. //#include "Watchdog.h"
  30. /* ************************************************************************** */
  31. /* Local variables */
  32. #ifdef ENABLE_DEBUG_LOG
  33. char acDebugLog[DEBUG_LOG_SIZE][100];
  34. int iLogIndex = 0;
  35. int iWrapCntr = 0;
  36. #endif
  37. //void _mon_putc(char c); //override from stdio to redirect stdout
  38. //-----------------------------------------------------------------------------
  39. //void _mon_putc(char c)
  40. //{
  41. // TelnetPutPrintf(c);
  42. //}
  43. //-----------------------------------------------------------------------------
  44. //-----------------------------------------------------------------------------
  45. short SwapEndianShort(short p_sData)
  46. {
  47. return (((p_sData & 0xFF00) >> 8) | ((p_sData & 0x00FF) << 8));
  48. }
  49. //-----------------------------------------------------------------------------
  50. //-----------------------------------------------------------------------------
  51. int SwapEndianInt(int p_iData)
  52. {
  53. return (((p_iData & 0xFF000000) >> 24) | ((p_iData & 0x00FF0000) >> 8) | ((p_iData & 0x0000FF00) << 8) | ((p_iData & 0x000000FF) << 24));
  54. }
  55. //-----------------------------------------------------------------------------
  56. //-----------------------------------------------------------------------------
  57. #ifdef ENABLE_DEBUG_LOG
  58. char* GetDebugLogPtr(void)
  59. {
  60. char *pcPtr;
  61. if(iLogIndex == DEBUG_LOG_SIZE)
  62. {
  63. iLogIndex = 0;
  64. iWrapCntr++;
  65. }
  66. memset(&acDebugLog[iLogIndex][0],0,sizeof(acDebugLog[iLogIndex]));
  67. pcPtr = &acDebugLog[iLogIndex][0];
  68. iLogIndex++;
  69. return pcPtr;
  70. }
  71. //-----------------------------------------------------------------------------
  72. //-----------------------------------------------------------------------------
  73. void ClearDebugLog(void)
  74. {
  75. int i;
  76. for(i = 0; i < DEBUG_LOG_SIZE; i++)
  77. {
  78. memset(&acDebugLog[i][0],0,sizeof(acDebugLog[i]));
  79. }
  80. iLogIndex = 0;
  81. iWrapCntr = 0;
  82. }
  83. #endif
  84. //-----------------------------------------------------------------------------
  85. //-----------------------------------------------------------------------------
  86. int ConvertIntToStrLeadingZero(char* p_pcString, unsigned int p_iChar, int digit)
  87. {
  88. char temp[8];
  89. int i=0;
  90. sprintf(temp, "%X", p_iChar);
  91. //itoa(p_cChar,temp,radix);
  92. if(p_iChar < 0xF)
  93. {
  94. for (i=0; i<digit-1; i++)
  95. p_pcString[i] = '0'; //Add a Zero in front of the Character
  96. memcpy(&p_pcString[i], &temp[0], digit-i);
  97. }
  98. else if(p_iChar < 0xFF)
  99. {
  100. for (i=0; i<digit-2; i++)
  101. p_pcString[i] = '0'; //Add a Zero in front of the Character
  102. memcpy(&p_pcString[i], &temp[0], digit-i);
  103. }
  104. else if(p_iChar < 0xFFF)
  105. {
  106. for (i=0; i<digit-3; i++)
  107. p_pcString[i] = '0'; //Add a Zero in front of the Character
  108. memcpy(&p_pcString[i], &temp[0], digit-i);
  109. }
  110. else if(p_iChar < 0xFFFF)
  111. {
  112. for (i=0; i<digit-4; i++)
  113. p_pcString[i] = '0'; //Add a Zero in front of the Character
  114. memcpy(&p_pcString[i], &temp[0], digit-i);
  115. }
  116. else if(p_iChar < 0xFFFFF)
  117. {
  118. for (i=0; i<digit-5; i++)
  119. p_pcString[i] = '0'; //Add a Zero in front of the Character
  120. memcpy(&p_pcString[i], &temp[0], digit-i);
  121. }
  122. else if(p_iChar < 0xFFFFFF)
  123. {
  124. for (i=0; i<digit-6; i++)
  125. p_pcString[i] = '0'; //Add a Zero in front of the Character
  126. memcpy(&p_pcString[i], &temp[0], digit-i);
  127. }
  128. else if(p_iChar < 0xFFFFFFF)
  129. {
  130. for (i=0; i<digit-7; i++)
  131. p_pcString[i] = '0'; //Add a Zero in front of the Character
  132. memcpy(&p_pcString[i], &temp[0], digit-i);
  133. }
  134. return 1;
  135. }
  136. //-----------------------------------------------------------------------------
  137. //-----------------------------------------------------------------------------
  138. int ConvertIntToStr(char* p_pcString, unsigned int p_iChar)
  139. {
  140. char temp[8];
  141. int i=0;
  142. sprintf(temp, "%X", p_iChar);
  143. if(p_iChar < 0xF)
  144. {
  145. p_pcString[0] = temp[0];
  146. return 1;
  147. }
  148. else if(p_iChar < 0xFF)
  149. {
  150. for (i=0; i<2; i++)
  151. p_pcString[i] = temp[i];
  152. return 2;
  153. }
  154. else if(p_iChar < 0xFFF)
  155. {
  156. for (i=0; i<3; i++)
  157. p_pcString[i] = temp[i];
  158. return 3;
  159. }
  160. else if(p_iChar < 0xFFFF)
  161. {
  162. for (i=0; i<4; i++)
  163. p_pcString[i] = temp[i];
  164. return 4;
  165. }
  166. else if(p_iChar < 0xFFFFF)
  167. {
  168. for (i=0; i<5; i++)
  169. p_pcString[i] = temp[i];
  170. return 5;
  171. }
  172. else if(p_iChar < 0xFFFFFF)
  173. {
  174. for (i=0; i<6; i++)
  175. p_pcString[i] = temp[i];
  176. return 6;
  177. }
  178. else if(p_iChar < 0xFFFFFFF)
  179. {
  180. for (i=0; i<7; i++)
  181. p_pcString[i] = temp[i];
  182. return 7;
  183. }
  184. else if(p_iChar < 0xFFFFFFFF)
  185. {
  186. for (i=0; i<8; i++)
  187. p_pcString[i] = temp[i];
  188. return 8;
  189. }
  190. return 0;
  191. }
  192. //-----------------------------------------------------------------------------
  193. //-----------------------------------------------------------------------------
  194. int ConvertCharToStrLeadingZero(char* p_pcString, unsigned char p_cChar, int digit)
  195. {
  196. char temp[2];
  197. int i=0;
  198. sprintf(temp, "%X", p_cChar);
  199. //itoa(p_cChar,temp,radix);
  200. if(p_cChar < 0xF)
  201. {
  202. for (i=0; i<digit-1; i++)
  203. p_pcString[i] = '0'; //Add a Zero in front of the Character
  204. memcpy(&p_pcString[i], &temp[0], digit-i);
  205. }
  206. else if(p_cChar < 0xFF)
  207. {
  208. for (i=0; i<digit-2; i++)
  209. p_pcString[i] = '0'; //Add a Zero in front of the Character
  210. memcpy(&p_pcString[i], &temp[0], digit-i);
  211. }
  212. return 1;
  213. }
  214. //-----------------------------------------------------------------------------
  215. //-----------------------------------------------------------------------------
  216. unsigned int ConvertStrToValue(char* p_pcString, int p_iSize, int p_iRadix)
  217. {
  218. int i=0;
  219. int j=0;
  220. unsigned int uiValue = 0;
  221. unsigned int uiFactor = 1;
  222. if (p_iRadix == 16)
  223. {
  224. for (i=p_iSize-1; i>=0; i--)
  225. {
  226. if (i==p_iSize-1)
  227. {
  228. uiValue = p_pcString[i] - 0x30;
  229. }
  230. else
  231. {
  232. for (j=i; j<=p_iSize-2; j++)
  233. uiFactor *= 16;
  234. uiValue += (p_pcString[i] - 0x30) * uiFactor;
  235. }
  236. }
  237. }
  238. else if (p_iRadix == 10)
  239. {
  240. for (i=0; i<p_iSize; i++)
  241. {
  242. if (i==0)
  243. uiFactor = 1;
  244. else
  245. {
  246. for (j=0; j<i; j++)
  247. uiFactor *= 10;
  248. }
  249. uiValue += (p_pcString[i] - 0x30) * uiFactor;
  250. }
  251. }
  252. return uiValue;
  253. }
  254. //-----------------------------------------------------------------------------
  255. //-----------------------------------------------------------------------------
  256. int ConvertCharToStr(char* p_pcString, unsigned char p_cChar)
  257. {
  258. char temp[2];
  259. sprintf(temp, "%X", p_cChar);
  260. if(p_cChar < 0xF)
  261. {
  262. p_pcString[0] = temp[0];
  263. return 1;
  264. }
  265. else if(p_cChar < 0xFF)
  266. {
  267. p_pcString[0] = temp[0];
  268. p_pcString[1] = temp[1];
  269. return 2;
  270. }
  271. return 0;
  272. }
  273. //-----------------------------------------------------------------------------
  274. //-----------------------------------------------------------------------------
  275. // CRC-8 table (256 bytes)
  276. // Crc table for poly: $C7
  277. const int CRC8_TABLE[256] = { 0, 199, 73, 142, 146, 85, 219, 28, 227, 36, 170, 109, 113, 182, 56, 255,
  278. 1, 198, 72, 143, 147, 84, 218, 29, 226, 37, 171, 108, 112, 183, 57, 254,
  279. 2, 197, 75, 140, 144, 87, 217, 30, 225, 38, 168, 111, 115, 180, 58, 253,
  280. 3, 196, 74, 141, 145, 86, 216, 31, 224, 39, 169, 110, 114, 181, 59, 252,
  281. 4, 195, 77, 138, 150, 81, 223, 24, 231, 32, 174, 105, 117, 178, 60, 251,
  282. 5, 194, 76, 139, 151, 80, 222, 25, 230, 33, 175, 104, 116, 179, 61, 250,
  283. 6, 193, 79, 136, 148, 83, 221, 26, 229, 34, 172, 107, 119, 176, 62, 249,
  284. 7, 192, 78, 137, 149, 82, 220, 27, 228, 35, 173, 106, 118, 177, 63, 248,
  285. 8, 207, 65, 134, 154, 93, 211, 20, 235, 44, 162, 101, 121, 190, 48, 247,
  286. 9, 206, 64, 135, 155, 92, 210, 21, 234, 45, 163, 100, 120, 191, 49, 246,
  287. 10, 205, 67, 132, 152, 95, 209, 22, 233, 46, 160, 103, 123, 188, 50, 245,
  288. 11, 204, 66, 133, 153, 94, 208, 23, 232, 47, 161, 102, 122, 189, 51, 244,
  289. 12, 203, 69, 130, 158, 89, 215, 16, 239, 40, 166, 97, 125, 186, 52, 243,
  290. 13, 202, 68, 131, 159, 88, 214, 17, 238, 41, 167, 96, 124, 187, 53, 242,
  291. 14, 201, 71, 128, 156, 91, 213, 18, 237, 42, 164, 99, 127, 184, 54, 241,
  292. 15, 200, 70, 129, 157, 90, 212, 19, 236, 43, 165, 98, 126, 185, 55, 240 };
  293. //---------------------------------------------------------------------------
  294. //
  295. // Function: Crc8
  296. //
  297. // Parameter: char* message
  298. // int number of characters in the command
  299. //
  300. // Descrip: calc the crc of a commans string
  301. //
  302. // Author: ADubreuil (12/05/06)
  303. //
  304. //---------------------------------------------------------------------------
  305. int Crc8(char* answer, int length)
  306. {
  307. int i,crc,key;
  308. crc = 0;
  309. for(i=0;i<length;i++) {
  310. key = CRC8_TABLE[crc];
  311. crc = answer[i] ^ key;
  312. }
  313. return crc;
  314. }
  315. //---------------------------------------------------------------------------
  316. void Delay(unsigned int count)
  317. {
  318. // KickWatchdog();
  319. // while(--count);
  320. // KickWatchdog();
  321. }
  322. //---------------------------------------------------------------------------
  323. //EOF