trab_fkt.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. /*
  2. * (C) Copyright 2003
  3. * Martin Krause, TQ-Systems GmbH, martin.krause@tqs.de
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #define DEBUG
  24. #include <common.h>
  25. #include <exports.h>
  26. #include <s3c2400.h>
  27. #include "tsc2000.h"
  28. #include "rs485.h"
  29. /*
  30. * define, to wait for the touch to be pressed, before reading coordinates in
  31. * command do_touch. If not defined, an error message is printed, when the
  32. * command do_touch is invoked and the touch is not pressed within an specific
  33. * interval.
  34. */
  35. #undef CONFIG_TOUCH_WAIT_PRESSED
  36. /* max time to wait for touch is pressed */
  37. #ifndef CONFIG_TOUCH_WAIT_PRESSED
  38. #define TOUCH_TIMEOUT 5
  39. #endif /* !CONFIG_TOUCH_WAIT_PRESSED */
  40. /* assignment of CPU internal ADC channels with TRAB hardware */
  41. #define VCC5V 2
  42. #define VCC12V 3
  43. /* CPLD-Register for controlling TRAB hardware functions */
  44. #define CPLD_BUTTONS ((volatile unsigned long *)0x04020000)
  45. #define CPLD_FILL_LEVEL ((volatile unsigned long *)0x04008000)
  46. #define CPLD_ROTARY_SWITCH ((volatile unsigned long *)0x04018000)
  47. #define CPLD_RS485_RE ((volatile unsigned long *)0x04028000)
  48. /* timer configuration bits for buzzer and PWM */
  49. #define START2 (1 << 12)
  50. #define UPDATE2 (1 << 13)
  51. #define INVERT2 (1 << 14)
  52. #define RELOAD2 (1 << 15)
  53. #define START3 (1 << 16)
  54. #define UPDATE3 (1 << 17)
  55. #define INVERT3 (1 << 18)
  56. #define RELOAD3 (1 << 19)
  57. #define PCLK 66000000
  58. #define BUZZER_FREQ 1000 /* frequency in Hz */
  59. #define PWM_FREQ 500
  60. /* definitions of I2C EEPROM device address */
  61. #define I2C_EEPROM_DEV_ADDR 0x54
  62. /* definition for touch panel calibration points */
  63. #define CALIB_TL 0 /* calibration point in (T)op (L)eft corner */
  64. #define CALIB_DR 1 /* calibration point in (D)own (R)ight corner */
  65. /* EEPROM address map */
  66. #define SERIAL_NUMBER 8
  67. #define TOUCH_X0 52
  68. #define TOUCH_Y0 54
  69. #define TOUCH_X1 56
  70. #define TOUCH_Y1 58
  71. #define CRC16 60
  72. /* EEPROM stuff */
  73. #define EEPROM_MAX_CRC_BUF 64
  74. /* RS485 stuff */
  75. #define RS485_MAX_RECEIVE_BUF_LEN 100
  76. /* Bit definitions for ADCCON */
  77. #define ADC_ENABLE_START 0x1
  78. #define ADC_READ_START 0x2
  79. #define ADC_STDBM 0x4
  80. #define ADC_INP_AIN0 (0x0 << 3)
  81. #define ADC_INP_AIN1 (0x1 << 3)
  82. #define ADC_INP_AIN2 (0x2 << 3)
  83. #define ADC_INP_AIN3 (0x3 << 3)
  84. #define ADC_INP_AIN4 (0x4 << 3)
  85. #define ADC_INP_AIN5 (0x5 << 3)
  86. #define ADC_INP_AIN6 (0x6 << 3)
  87. #define ADC_INP_AIN7 (0x7 << 3)
  88. #define ADC_PRSCEN 0x4000
  89. #define ADC_ECFLG 0x8000
  90. /* function test functions */
  91. int do_dip (void);
  92. int do_info (void);
  93. int do_vcc5v (void);
  94. int do_vcc12v (void);
  95. int do_buttons (void);
  96. int do_fill_level (void);
  97. int do_rotary_switch (void);
  98. int do_pressure (void);
  99. int do_v_bat (void);
  100. int do_vfd_id (void);
  101. int do_buzzer (char **);
  102. int do_led (char **);
  103. int do_full_bridge (char **);
  104. int do_dac (char **);
  105. int do_motor_contact (void);
  106. int do_motor (char **);
  107. int do_pwm (char **);
  108. int do_thermo (char **);
  109. int do_touch (char **);
  110. int do_rs485 (char **);
  111. int do_serial_number (char **);
  112. int do_crc16 (void);
  113. int do_power_switch (void);
  114. int do_gain (char **);
  115. int do_eeprom (char **);
  116. /* helper functions */
  117. static void adc_init (void);
  118. static int adc_read (unsigned int channel);
  119. static void print_identifier (void);
  120. #ifdef CONFIG_TOUCH_WAIT_PRESSED
  121. static void touch_wait_pressed (void);
  122. #else
  123. static int touch_check_pressed (void);
  124. #endif /* CONFIG_TOUCH_WAIT_PRESSED */
  125. static void touch_read_x_y (int *x, int *y);
  126. static int touch_write_clibration_values (int calib_point, int x, int y);
  127. static int rs485_send_line (const char *data);
  128. static int rs485_receive_chars (char *data, int timeout);
  129. static unsigned short updcrc(unsigned short icrc, unsigned char *icp,
  130. unsigned int icnt);
  131. #if (CONFIG_COMMANDS & CFG_CMD_I2C)
  132. static int trab_eeprom_read (char **argv);
  133. static int trab_eeprom_write (char **argv);
  134. int i2c_write_multiple (uchar chip, uint addr, int alen, uchar *buffer,
  135. int len);
  136. int i2c_read_multiple ( uchar chip, uint addr, int alen, uchar *buffer,
  137. int len);
  138. #endif /* CFG_CMD_I2C */
  139. /*
  140. * TRAB board specific commands. Especially commands for burn-in and function
  141. * test.
  142. */
  143. int trab_fkt (int argc, char *argv[])
  144. {
  145. int i;
  146. app_startup(argv);
  147. if (get_version () != XF_VERSION) {
  148. printf ("Wrong XF_VERSION. Please re-compile with actual "
  149. "u-boot sources\n");
  150. printf ("Example expects ABI version %d\n", XF_VERSION);
  151. printf ("Actual U-Boot ABI version %d\n", (int)get_version());
  152. return 1;
  153. }
  154. debug ("argc = %d\n", argc);
  155. for (i=0; i<=argc; ++i) {
  156. debug ("argv[%d] = \"%s\"\n", i, argv[i] ? argv[i] : "<NULL>");
  157. }
  158. adc_init ();
  159. switch (argc) {
  160. case 0:
  161. case 1:
  162. break;
  163. case 2:
  164. if (strcmp (argv[1], "info") == 0) {
  165. return (do_info ());
  166. }
  167. if (strcmp (argv[1], "dip") == 0) {
  168. return (do_dip ());
  169. }
  170. if (strcmp (argv[1], "vcc5v") == 0) {
  171. return (do_vcc5v ());
  172. }
  173. if (strcmp (argv[1], "vcc12v") == 0) {
  174. return (do_vcc12v ());
  175. }
  176. if (strcmp (argv[1], "buttons") == 0) {
  177. return (do_buttons ());
  178. }
  179. if (strcmp (argv[1], "fill_level") == 0) {
  180. return (do_fill_level ());
  181. }
  182. if (strcmp (argv[1], "rotary_switch") == 0) {
  183. return (do_rotary_switch ());
  184. }
  185. if (strcmp (argv[1], "pressure") == 0) {
  186. return (do_pressure ());
  187. }
  188. if (strcmp (argv[1], "v_bat") == 0) {
  189. return (do_v_bat ());
  190. }
  191. if (strcmp (argv[1], "vfd_id") == 0) {
  192. return (do_vfd_id ());
  193. }
  194. if (strcmp (argv[1], "motor_contact") == 0) {
  195. return (do_motor_contact ());
  196. }
  197. if (strcmp (argv[1], "crc16") == 0) {
  198. return (do_crc16 ());
  199. }
  200. if (strcmp (argv[1], "power_switch") == 0) {
  201. return (do_power_switch ());
  202. }
  203. break;
  204. case 3:
  205. if (strcmp (argv[1], "full_bridge") == 0) {
  206. return (do_full_bridge (argv));
  207. }
  208. if (strcmp (argv[1], "dac") == 0) {
  209. return (do_dac (argv));
  210. }
  211. if (strcmp (argv[1], "motor") == 0) {
  212. return (do_motor (argv));
  213. }
  214. if (strcmp (argv[1], "pwm") == 0) {
  215. return (do_pwm (argv));
  216. }
  217. if (strcmp (argv[1], "thermo") == 0) {
  218. return (do_thermo (argv));
  219. }
  220. if (strcmp (argv[1], "touch") == 0) {
  221. return (do_touch (argv));
  222. }
  223. if (strcmp (argv[1], "serial_number") == 0) {
  224. return (do_serial_number (argv));
  225. }
  226. if (strcmp (argv[1], "buzzer") == 0) {
  227. return (do_buzzer (argv));
  228. }
  229. if (strcmp (argv[1], "gain") == 0) {
  230. return (do_gain (argv));
  231. }
  232. break;
  233. case 4:
  234. if (strcmp (argv[1], "led") == 0) {
  235. return (do_led (argv));
  236. }
  237. if (strcmp (argv[1], "rs485") == 0) {
  238. return (do_rs485 (argv));
  239. }
  240. if (strcmp (argv[1], "serial_number") == 0) {
  241. return (do_serial_number (argv));
  242. }
  243. break;
  244. case 5:
  245. if (strcmp (argv[1], "eeprom") == 0) {
  246. return (do_eeprom (argv));
  247. }
  248. break;
  249. case 6:
  250. if (strcmp (argv[1], "eeprom") == 0) {
  251. return (do_eeprom (argv));
  252. }
  253. break;
  254. default:
  255. break;
  256. }
  257. printf ("Usage:\n<command> <parameter1> <parameter2> ...\n");
  258. return 1;
  259. }
  260. int do_info (void)
  261. {
  262. printf ("Stand-alone application for TRAB board function test\n");
  263. printf ("Built: %s at %s\n", __DATE__ , __TIME__ );
  264. return 0;
  265. }
  266. int do_dip (void)
  267. {
  268. unsigned int result = 0;
  269. int adc_val;
  270. int i;
  271. /***********************************************************
  272. DIP switch connection (according to wa4-cpu.sp.301.pdf, page 3):
  273. SW1 - AIN4
  274. SW2 - AIN5
  275. SW3 - AIN6
  276. SW4 - AIN7
  277. "On" DIP switch position short-circuits the voltage from
  278. the input channel (i.e. '0' conversion result means "on").
  279. *************************************************************/
  280. for (i = 7; i > 3; i--) {
  281. if ((adc_val = adc_read (i)) == -1) {
  282. printf ("Channel %d could not be read\n", i);
  283. return 1;
  284. }
  285. /*
  286. * Input voltage (switch open) is 1.8 V.
  287. * (Vin_High/VRef)*adc_res = (1,8V/2,5V)*1023) = 736
  288. * Set trigger at halve that value.
  289. */
  290. if (adc_val < 368)
  291. result |= (1 << (i-4));
  292. }
  293. /* print result to console */
  294. print_identifier ();
  295. for (i = 0; i < 4; i++) {
  296. if ((result & (1 << i)) == 0)
  297. printf("0");
  298. else
  299. printf("1");
  300. }
  301. printf("\n");
  302. return 0;
  303. }
  304. int do_vcc5v (void)
  305. {
  306. int result;
  307. /* VCC5V is connected to channel 2 */
  308. if ((result = adc_read (VCC5V)) == -1) {
  309. printf ("VCC5V could not be read\n");
  310. return 1;
  311. }
  312. /*
  313. * Calculate voltage value. Split in two parts because there is no
  314. * floating point support. VCC5V is connected over an resistor divider:
  315. * VCC5V=ADCval*2,5V/1023*(10K+30K)/10K.
  316. */
  317. print_identifier ();
  318. printf ("%d", (result & 0x3FF)* 10 / 1023);
  319. printf (".%d", ((result & 0x3FF)* 10 % 1023)* 10 / 1023);
  320. printf ("%d V\n", (((result & 0x3FF) * 10 % 1023 ) * 10 % 1023)
  321. * 10 / 1024);
  322. return 0;
  323. }
  324. int do_vcc12v (void)
  325. {
  326. int result;
  327. if ((result = adc_read (VCC12V)) == -1) {
  328. printf ("VCC12V could not be read\n");
  329. return 1;
  330. }
  331. /*
  332. * Calculate voltage value. Split in two parts because there is no
  333. * floating point support. VCC5V is connected over an resistor divider:
  334. * VCC12V=ADCval*2,5V/1023*(30K+270K)/30K.
  335. */
  336. print_identifier ();
  337. printf ("%d", (result & 0x3FF)* 25 / 1023);
  338. printf (".%d V\n", ((result & 0x3FF)* 25 % 1023) * 10 / 1023);
  339. return 0;
  340. }
  341. static int adc_read (unsigned int channel)
  342. {
  343. int j = 1000; /* timeout value for wait loop in us */
  344. int result;
  345. S3C2400_ADC *padc;
  346. padc = S3C2400_GetBase_ADC();
  347. channel &= 0x7;
  348. padc->ADCCON &= ~ADC_STDBM; /* select normal mode */
  349. padc->ADCCON &= ~(0x7 << 3); /* clear the channel bits */
  350. padc->ADCCON |= ((channel << 3) | ADC_ENABLE_START);
  351. while (j--) {
  352. if ((padc->ADCCON & ADC_ENABLE_START) == 0)
  353. break;
  354. udelay (1);
  355. }
  356. if (j == 0) {
  357. printf("%s: ADC timeout\n", __FUNCTION__);
  358. padc->ADCCON |= ADC_STDBM; /* select standby mode */
  359. return -1;
  360. }
  361. result = padc->ADCDAT & 0x3FF;
  362. padc->ADCCON |= ADC_STDBM; /* select standby mode */
  363. debug ("%s: channel %d, result[DIGIT]=%d\n", __FUNCTION__,
  364. (padc->ADCCON >> 3) & 0x7, result);
  365. /*
  366. * Wait for ADC to be ready for next conversion. This delay value was
  367. * estimated, because the datasheet does not specify a value.
  368. */
  369. udelay (1000);
  370. return (result);
  371. }
  372. static void adc_init (void)
  373. {
  374. S3C2400_ADC *padc;
  375. padc = S3C2400_GetBase_ADC();
  376. padc->ADCCON &= ~(0xff << 6); /* clear prescaler bits */
  377. padc->ADCCON |= ((65 << 6) | ADC_PRSCEN); /* set prescaler */
  378. /*
  379. * Wait some time to avoid problem with very first call of
  380. * adc_read(). Without * this delay, sometimes the first read adc
  381. * value is 0. Perhaps because the * adjustment of prescaler takes
  382. * some clock cycles?
  383. */
  384. udelay (1000);
  385. return;
  386. }
  387. int do_buttons (void)
  388. {
  389. int result;
  390. int i;
  391. result = *CPLD_BUTTONS; /* read CPLD */
  392. debug ("%s: cpld_taster (32 bit) %#x\n", __FUNCTION__, result);
  393. /* print result to console */
  394. print_identifier ();
  395. for (i = 16; i <= 19; i++) {
  396. if ((result & (1 << i)) == 0)
  397. printf("0");
  398. else
  399. printf("1");
  400. }
  401. printf("\n");
  402. return 0;
  403. }
  404. int do_power_switch (void)
  405. {
  406. int result;
  407. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  408. /* configure GPE7 as input */
  409. gpio->PECON &= ~(0x3 << (2 * 7));
  410. /* signal GPE7 from power switch is low active: 0=on , 1=off */
  411. result = ((gpio->PEDAT & (1 << 7)) == (1 << 7)) ? 0 : 1;
  412. print_identifier ();
  413. printf("%d\n", result);
  414. return 0;
  415. }
  416. int do_fill_level (void)
  417. {
  418. int result;
  419. result = *CPLD_FILL_LEVEL; /* read CPLD */
  420. debug ("%s: cpld_fuellstand (32 bit) %#x\n", __FUNCTION__, result);
  421. /* print result to console */
  422. print_identifier ();
  423. if ((result & (1 << 16)) == 0)
  424. printf("0\n");
  425. else
  426. printf("1\n");
  427. return 0;
  428. }
  429. int do_rotary_switch (void)
  430. {
  431. int result;
  432. /*
  433. * Please note, that the default values of the direction bits are
  434. * undefined after reset. So it is a good idea, to make first a dummy
  435. * call to this function, to clear the direction bits and set so to
  436. * proper values.
  437. */
  438. result = *CPLD_ROTARY_SWITCH; /* read CPLD */
  439. debug ("%s: cpld_inc (32 bit) %#x\n", __FUNCTION__, result);
  440. *CPLD_ROTARY_SWITCH |= (3 << 16); /* clear direction bits in CPLD */
  441. /* print result to console */
  442. print_identifier ();
  443. if ((result & (1 << 16)) == (1 << 16))
  444. printf("R");
  445. if ((result & (1 << 17)) == (1 << 17))
  446. printf("L");
  447. if (((result & (1 << 16)) == 0) && ((result & (1 << 17)) == 0))
  448. printf("0");
  449. if ((result & (1 << 18)) == 0)
  450. printf("0\n");
  451. else
  452. printf("1\n");
  453. return 0;
  454. }
  455. int do_vfd_id (void)
  456. {
  457. int i;
  458. long int pcup_old, pccon_old;
  459. int vfd_board_id;
  460. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  461. /* try to red vfd board id from the value defined by pull-ups */
  462. pcup_old = gpio->PCUP;
  463. pccon_old = gpio->PCCON;
  464. gpio->PCUP = (gpio->PCUP & 0xFFF0); /* activate GPC0...GPC3 pull-ups */
  465. gpio->PCCON = (gpio->PCCON & 0xFFFFFF00); /* configure GPC0...GPC3 as
  466. * inputs */
  467. udelay (10); /* allow signals to settle */
  468. vfd_board_id = (~gpio->PCDAT) & 0x000F; /* read GPC0...GPC3 port pins */
  469. gpio->PCCON = pccon_old;
  470. gpio->PCUP = pcup_old;
  471. /* print vfd_board_id to console */
  472. print_identifier ();
  473. for (i = 0; i < 4; i++) {
  474. if ((vfd_board_id & (1 << i)) == 0)
  475. printf("0");
  476. else
  477. printf("1");
  478. }
  479. printf("\n");
  480. return 0;
  481. }
  482. int do_buzzer (char **argv)
  483. {
  484. int counter;
  485. S3C24X0_TIMERS * const timers = S3C24X0_GetBase_TIMERS();
  486. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  487. /* set prescaler for timer 2, 3 and 4 */
  488. timers->TCFG0 &= ~0xFF00;
  489. timers->TCFG0 |= 0x0F00;
  490. /* set divider for timer 2 */
  491. timers->TCFG1 &= ~0xF00;
  492. timers->TCFG1 |= 0x300;
  493. /* set frequency */
  494. counter = (PCLK / BUZZER_FREQ) >> 9;
  495. timers->ch[2].TCNTB = counter;
  496. timers->ch[2].TCMPB = counter / 2;
  497. if (strcmp (argv[2], "on") == 0) {
  498. debug ("%s: frequency: %d\n", __FUNCTION__,
  499. BUZZER_FREQ);
  500. /* configure pin GPD7 as TOUT2 */
  501. gpio->PDCON &= ~0xC000;
  502. gpio->PDCON |= 0x8000;
  503. /* start */
  504. timers->TCON = (timers->TCON | UPDATE2 | RELOAD2) &
  505. ~INVERT2;
  506. timers->TCON = (timers->TCON | START2) & ~UPDATE2;
  507. return (0);
  508. }
  509. else if (strcmp (argv[2], "off") == 0) {
  510. /* stop */
  511. timers->TCON &= ~(START2 | RELOAD2);
  512. /* configure GPD7 as output and set to low */
  513. gpio->PDCON &= ~0xC000;
  514. gpio->PDCON |= 0x4000;
  515. gpio->PDDAT &= ~0x80;
  516. return (0);
  517. }
  518. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  519. return 1;
  520. }
  521. int do_led (char **argv)
  522. {
  523. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  524. /* configure PC14 and PC15 as output */
  525. gpio->PCCON &= ~(0xF << 28);
  526. gpio->PCCON |= (0x5 << 28);
  527. /* configure PD0 and PD4 as output */
  528. gpio->PDCON &= ~((0x3 << 8) | 0x3);
  529. gpio->PDCON |= ((0x1 << 8) | 0x1);
  530. switch (simple_strtoul(argv[2], NULL, 10)) {
  531. case 0:
  532. case 1:
  533. break;
  534. case 2:
  535. if (strcmp (argv[3], "on") == 0)
  536. gpio->PCDAT |= (1 << 14);
  537. else
  538. gpio->PCDAT &= ~(1 << 14);
  539. return 0;
  540. case 3:
  541. if (strcmp (argv[3], "on") == 0)
  542. gpio->PCDAT |= (1 << 15);
  543. else
  544. gpio->PCDAT &= ~(1 << 15);
  545. return 0;
  546. case 4:
  547. if (strcmp (argv[3], "on") == 0)
  548. gpio->PDDAT |= (1 << 0);
  549. else
  550. gpio->PDDAT &= ~(1 << 0);
  551. return 0;
  552. case 5:
  553. if (strcmp (argv[3], "on") == 0)
  554. gpio->PDDAT |= (1 << 4);
  555. else
  556. gpio->PDDAT &= ~(1 << 4);
  557. return 0;
  558. default:
  559. break;
  560. }
  561. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  562. return 1;
  563. }
  564. int do_full_bridge (char **argv)
  565. {
  566. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  567. /* configure PD5 and PD6 as output */
  568. gpio->PDCON &= ~((0x3 << 5*2) | (0x3 << 6*2));
  569. gpio->PDCON |= ((0x1 << 5*2) | (0x1 << 6*2));
  570. if (strcmp (argv[2], "+") == 0) {
  571. gpio->PDDAT |= (1 << 5);
  572. gpio->PDDAT |= (1 << 6);
  573. return 0;
  574. }
  575. else if (strcmp (argv[2], "-") == 0) {
  576. gpio->PDDAT &= ~(1 << 5);
  577. gpio->PDDAT |= (1 << 6);
  578. return 0;
  579. }
  580. else if (strcmp (argv[2], "off") == 0) {
  581. gpio->PDDAT &= ~(1 << 5);
  582. gpio->PDDAT &= ~(1 << 6);
  583. return 0;
  584. }
  585. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  586. return 1;
  587. }
  588. /* val must be in [0, 4095] */
  589. static inline unsigned long tsc2000_to_uv (u16 val)
  590. {
  591. return ((250000 * val) / 4096) * 10;
  592. }
  593. int do_dac (char **argv)
  594. {
  595. int brightness;
  596. /* initialize SPI */
  597. spi_init ();
  598. if (((brightness = simple_strtoul (argv[2], NULL, 10)) < 0) ||
  599. (brightness > 255)) {
  600. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  601. return 1;
  602. }
  603. tsc2000_write(TSC2000_REG_DACCTL, 0x0); /* Power up DAC */
  604. tsc2000_write(TSC2000_REG_DAC, brightness & 0xff);
  605. return 0;
  606. }
  607. int do_v_bat (void)
  608. {
  609. unsigned long ret, res;
  610. /* initialize SPI */
  611. spi_init ();
  612. tsc2000_write(TSC2000_REG_ADC, 0x1836);
  613. /* now wait for data available */
  614. adc_wait_conversion_done();
  615. ret = tsc2000_read(TSC2000_REG_BAT1);
  616. res = (tsc2000_to_uv(ret) + 1250) / 2500;
  617. res += (ERROR_BATTERY * res) / 1000;
  618. print_identifier ();
  619. printf ("%ld", (res / 100));
  620. printf (".%ld", ((res % 100) / 10));
  621. printf ("%ld V\n", (res % 10));
  622. return 0;
  623. }
  624. int do_pressure (void)
  625. {
  626. /* initialize SPI */
  627. spi_init ();
  628. tsc2000_write(TSC2000_REG_ADC, 0x2436);
  629. /* now wait for data available */
  630. adc_wait_conversion_done();
  631. print_identifier ();
  632. printf ("%d\n", tsc2000_read(TSC2000_REG_AUX2));
  633. return 0;
  634. }
  635. int do_motor_contact (void)
  636. {
  637. int result;
  638. result = *CPLD_FILL_LEVEL; /* read CPLD */
  639. debug ("%s: cpld_fuellstand (32 bit) %#x\n", __FUNCTION__, result);
  640. /* print result to console */
  641. print_identifier ();
  642. if ((result & (1 << 17)) == 0)
  643. printf("0\n");
  644. else
  645. printf("1\n");
  646. return 0;
  647. }
  648. int do_motor (char **argv)
  649. {
  650. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  651. /* Configure I/O port */
  652. gpio->PGCON &= ~(0x3 << 0);
  653. gpio->PGCON |= (0x1 << 0);
  654. if (strcmp (argv[2], "on") == 0) {
  655. gpio->PGDAT &= ~(1 << 0);
  656. return 0;
  657. }
  658. if (strcmp (argv[2], "off") == 0) {
  659. gpio->PGDAT |= (1 << 0);
  660. return 0;
  661. }
  662. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  663. return 1;
  664. }
  665. static void print_identifier (void)
  666. {
  667. printf ("## FKT: ");
  668. }
  669. int do_pwm (char **argv)
  670. {
  671. int counter;
  672. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  673. S3C24X0_TIMERS * const timers = S3C24X0_GetBase_TIMERS();
  674. if (strcmp (argv[2], "on") == 0) {
  675. /* configure pin GPD8 as TOUT3 */
  676. gpio->PDCON &= ~(0x3 << 8*2);
  677. gpio->PDCON |= (0x2 << 8*2);
  678. /* set prescaler for timer 2, 3 and 4 */
  679. timers->TCFG0 &= ~0xFF00;
  680. timers->TCFG0 |= 0x0F00;
  681. /* set divider for timer 3 */
  682. timers->TCFG1 &= ~(0xf << 12);
  683. timers->TCFG1 |= (0x3 << 12);
  684. /* set frequency */
  685. counter = (PCLK / PWM_FREQ) >> 9;
  686. timers->ch[3].TCNTB = counter;
  687. timers->ch[3].TCMPB = counter / 2;
  688. /* start timer */
  689. timers->TCON = (timers->TCON | UPDATE3 | RELOAD3) & ~INVERT3;
  690. timers->TCON = (timers->TCON | START3) & ~UPDATE3;
  691. return 0;
  692. }
  693. if (strcmp (argv[2], "off") == 0) {
  694. /* stop timer */
  695. timers->TCON &= ~(START2 | RELOAD2);
  696. /* configure pin GPD8 as output and set to 0 */
  697. gpio->PDCON &= ~(0x3 << 8*2);
  698. gpio->PDCON |= (0x1 << 8*2);
  699. gpio->PDDAT &= ~(1 << 8);
  700. return 0;
  701. }
  702. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  703. return 1;
  704. }
  705. int do_thermo (char **argv)
  706. {
  707. int channel, res;
  708. tsc2000_reg_init ();
  709. if (strcmp (argv[2], "all") == 0) {
  710. int i;
  711. for (i=0; i <= 15; i++) {
  712. res = tsc2000_read_channel(i);
  713. print_identifier ();
  714. printf ("c%d: %d\n", i, res);
  715. }
  716. return 0;
  717. }
  718. channel = simple_strtoul (argv[2], NULL, 10);
  719. res = tsc2000_read_channel(channel);
  720. print_identifier ();
  721. printf ("%d\n", res);
  722. return 0; /* return OK */
  723. }
  724. int do_touch (char **argv)
  725. {
  726. int x, y;
  727. if (strcmp (argv[2], "tl") == 0) {
  728. #ifdef CONFIG_TOUCH_WAIT_PRESSED
  729. touch_wait_pressed();
  730. #else
  731. {
  732. int i;
  733. for (i = 0; i < (TOUCH_TIMEOUT * 1000); i++) {
  734. if (touch_check_pressed ()) {
  735. break;
  736. }
  737. udelay (1000); /* pause 1 ms */
  738. }
  739. }
  740. if (!touch_check_pressed()) {
  741. print_identifier ();
  742. printf ("error: touch not pressed\n");
  743. return 1;
  744. }
  745. #endif /* CONFIG_TOUCH_WAIT_PRESSED */
  746. touch_read_x_y (&x, &y);
  747. print_identifier ();
  748. printf ("x=%d y=%d\n", x, y);
  749. return touch_write_clibration_values (CALIB_TL, x, y);
  750. }
  751. else if (strcmp (argv[2], "dr") == 0) {
  752. #ifdef CONFIG_TOUCH_WAIT_PRESSED
  753. touch_wait_pressed();
  754. #else
  755. {
  756. int i;
  757. for (i = 0; i < (TOUCH_TIMEOUT * 1000); i++) {
  758. if (touch_check_pressed ()) {
  759. break;
  760. }
  761. udelay (1000); /* pause 1 ms */
  762. }
  763. }
  764. if (!touch_check_pressed()) {
  765. print_identifier ();
  766. printf ("error: touch not pressed\n");
  767. return 1;
  768. }
  769. #endif /* CONFIG_TOUCH_WAIT_PRESSED */
  770. touch_read_x_y (&x, &y);
  771. print_identifier ();
  772. printf ("x=%d y=%d\n", x, y);
  773. return touch_write_clibration_values (CALIB_DR, x, y);
  774. }
  775. return 1; /* not "tl", nor "dr", so return error */
  776. }
  777. #ifdef CONFIG_TOUCH_WAIT_PRESSED
  778. static void touch_wait_pressed (void)
  779. {
  780. while (!(tsc2000_read(TSC2000_REG_ADC) & TC_PSM));
  781. }
  782. #else
  783. static int touch_check_pressed (void)
  784. {
  785. return (tsc2000_read(TSC2000_REG_ADC) & TC_PSM);
  786. }
  787. #endif /* CONFIG_TOUCH_WAIT_PRESSED */
  788. static int touch_write_clibration_values (int calib_point, int x, int y)
  789. {
  790. #if (CONFIG_COMMANDS & CFG_CMD_I2C)
  791. int x_verify = 0;
  792. int y_verify = 0;
  793. tsc2000_reg_init ();
  794. if (calib_point == CALIB_TL) {
  795. if (i2c_write_multiple (I2C_EEPROM_DEV_ADDR, TOUCH_X0, 1,
  796. (unsigned char *)&x, 2)) {
  797. return 1;
  798. }
  799. if (i2c_write_multiple (I2C_EEPROM_DEV_ADDR, TOUCH_Y0, 1,
  800. (unsigned char *)&y, 2)) {
  801. return 1;
  802. }
  803. /* verify written values */
  804. if (i2c_read_multiple (I2C_EEPROM_DEV_ADDR, TOUCH_X0, 1,
  805. (unsigned char *)&x_verify, 2)) {
  806. return 1;
  807. }
  808. if (i2c_read_multiple (I2C_EEPROM_DEV_ADDR, TOUCH_Y0, 1,
  809. (unsigned char *)&y_verify, 2)) {
  810. return 1;
  811. }
  812. if ((y != y_verify) || (x != x_verify)) {
  813. print_identifier ();
  814. printf ("error: verify error\n");
  815. return 1;
  816. }
  817. return 0; /* no error */
  818. }
  819. else if (calib_point == CALIB_DR) {
  820. if (i2c_write_multiple (I2C_EEPROM_DEV_ADDR, TOUCH_X1, 1,
  821. (unsigned char *)&x, 2)) {
  822. return 1;
  823. }
  824. if (i2c_write_multiple (I2C_EEPROM_DEV_ADDR, TOUCH_Y1, 1,
  825. (unsigned char *)&y, 2)) {
  826. return 1;
  827. }
  828. /* verify written values */
  829. if (i2c_read_multiple (I2C_EEPROM_DEV_ADDR, TOUCH_X1, 1,
  830. (unsigned char *)&x_verify, 2)) {
  831. return 1;
  832. }
  833. if (i2c_read_multiple (I2C_EEPROM_DEV_ADDR, TOUCH_Y1, 1,
  834. (unsigned char *)&y_verify, 2)) {
  835. return 1;
  836. }
  837. if ((y != y_verify) || (x != x_verify)) {
  838. print_identifier ();
  839. printf ("error: verify error\n");
  840. return 1;
  841. }
  842. return 0;
  843. }
  844. return 1;
  845. #else
  846. printf ("No I2C support enabled (CFG_CMD_I2C), could not write "
  847. "to EEPROM\n");
  848. return (1);
  849. #endif /* CFG_CMD_I2C */
  850. }
  851. static void touch_read_x_y (int *px, int *py)
  852. {
  853. tsc2000_write(TSC2000_REG_ADC, DEFAULT_ADC | TC_AD0 | TC_AD1);
  854. adc_wait_conversion_done();
  855. *px = tsc2000_read(TSC2000_REG_X);
  856. tsc2000_write(TSC2000_REG_ADC, DEFAULT_ADC | TC_AD2);
  857. adc_wait_conversion_done();
  858. *py = tsc2000_read(TSC2000_REG_Y);
  859. }
  860. int do_rs485 (char **argv)
  861. {
  862. int timeout;
  863. char data[RS485_MAX_RECEIVE_BUF_LEN];
  864. if (strcmp (argv[2], "send") == 0) {
  865. return (rs485_send_line (argv[3]));
  866. }
  867. else if (strcmp (argv[2], "receive") == 0) {
  868. timeout = simple_strtoul(argv[3], NULL, 10);
  869. if (rs485_receive_chars (data, timeout) != 0) {
  870. print_identifier ();
  871. printf ("## nothing received\n");
  872. return (1);
  873. }
  874. else {
  875. print_identifier ();
  876. printf ("%s\n", data);
  877. return (0);
  878. }
  879. }
  880. printf ("%s: unknown command %s\n", __FUNCTION__, argv[2]);
  881. return (1); /* unknown command, return error */
  882. }
  883. static int rs485_send_line (const char *data)
  884. {
  885. rs485_init ();
  886. trab_rs485_enable_tx ();
  887. rs485_puts (data);
  888. rs485_putc ('\n');
  889. return (0);
  890. }
  891. static int rs485_receive_chars (char *data, int timeout)
  892. {
  893. int i;
  894. int receive_count = 0;
  895. rs485_init ();
  896. trab_rs485_enable_rx ();
  897. /* test every 1 ms for received characters to avoid a receive FIFO
  898. * overrun (@ 38.400 Baud) */
  899. for (i = 0; i < (timeout * 1000); i++) {
  900. while (rs485_tstc ()) {
  901. if (receive_count >= RS485_MAX_RECEIVE_BUF_LEN-1)
  902. break;
  903. *data++ = rs485_getc ();
  904. receive_count++;
  905. }
  906. udelay (1000); /* pause 1 ms */
  907. }
  908. *data = '\0'; /* terminate string */
  909. if (receive_count == 0)
  910. return (1);
  911. else
  912. return (0);
  913. }
  914. int do_serial_number (char **argv)
  915. {
  916. #if (CONFIG_COMMANDS & CFG_CMD_I2C)
  917. unsigned int serial_number;
  918. if (strcmp (argv[2], "read") == 0) {
  919. if (i2c_read (I2C_EEPROM_DEV_ADDR, SERIAL_NUMBER, 1,
  920. (unsigned char *)&serial_number, 4)) {
  921. printf ("could not read from eeprom\n");
  922. return (1);
  923. }
  924. print_identifier ();
  925. printf ("%08d\n", serial_number);
  926. return (0);
  927. }
  928. else if (strcmp (argv[2], "write") == 0) {
  929. serial_number = simple_strtoul(argv[3], NULL, 10);
  930. if (i2c_write (I2C_EEPROM_DEV_ADDR, SERIAL_NUMBER, 1,
  931. (unsigned char *)&serial_number, 4)) {
  932. printf ("could not write to eeprom\n");
  933. return (1);
  934. }
  935. return (0);
  936. }
  937. printf ("%s: unknown command %s\n", __FUNCTION__, argv[2]);
  938. return (1); /* unknown command, return error */
  939. #else
  940. printf ("No I2C support enabled (CFG_CMD_I2C), could not write "
  941. "to EEPROM\n");
  942. return (1);
  943. #endif /* CFG_CMD_I2C */
  944. }
  945. int do_crc16 (void)
  946. {
  947. #if (CONFIG_COMMANDS & CFG_CMD_I2C)
  948. int crc;
  949. unsigned char buf[EEPROM_MAX_CRC_BUF];
  950. if (i2c_read (I2C_EEPROM_DEV_ADDR, 0, 1, buf, 60)) {
  951. printf ("could not read from eeprom\n");
  952. return (1);
  953. }
  954. crc = 0; /* start value of crc calculation */
  955. crc = updcrc (crc, buf, 60);
  956. print_identifier ();
  957. printf ("crc16=%#04x\n", crc);
  958. if (i2c_write (I2C_EEPROM_DEV_ADDR, CRC16, 1, (unsigned char *)&crc,
  959. sizeof (crc))) {
  960. printf ("could not read from eeprom\n");
  961. return (1);
  962. }
  963. return (0);
  964. #else
  965. printf ("No I2C support enabled (CFG_CMD_I2C), could not write "
  966. "to EEPROM\n");
  967. return (1);
  968. #endif /* CFG_CMD_I2C */
  969. }
  970. /*
  971. * Calculate, intelligently, the CRC of a dataset incrementally given a
  972. * buffer full at a time.
  973. * Initialize crc to 0 for XMODEM, -1 for CCITT.
  974. *
  975. * Usage:
  976. * newcrc = updcrc( oldcrc, bufadr, buflen )
  977. * unsigned int oldcrc, buflen;
  978. * char *bufadr;
  979. *
  980. * Compile with -DTEST to generate program that prints CRC of stdin to stdout.
  981. * Compile with -DMAKETAB to print values for crctab to stdout
  982. */
  983. /* the CRC polynomial. This is used by XMODEM (almost CCITT).
  984. * If you change P, you must change crctab[]'s initial value to what is
  985. * printed by initcrctab()
  986. */
  987. #define P 0x1021
  988. /* number of bits in CRC: don't change it. */
  989. #define W 16
  990. /* this the number of bits per char: don't change it. */
  991. #define B 8
  992. static unsigned short crctab[1<<B] = { /* as calculated by initcrctab() */
  993. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
  994. 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  995. 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
  996. 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
  997. 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
  998. 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
  999. 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
  1000. 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
  1001. 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
  1002. 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
  1003. 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
  1004. 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
  1005. 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
  1006. 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
  1007. 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
  1008. 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
  1009. 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
  1010. 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
  1011. 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
  1012. 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
  1013. 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
  1014. 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  1015. 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
  1016. 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
  1017. 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
  1018. 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
  1019. 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
  1020. 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
  1021. 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
  1022. 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
  1023. 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
  1024. 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
  1025. };
  1026. static unsigned short updcrc(unsigned short icrc, unsigned char *icp,
  1027. unsigned int icnt )
  1028. {
  1029. register unsigned short crc = icrc;
  1030. register unsigned char *cp = icp;
  1031. register unsigned int cnt = icnt;
  1032. while (cnt--)
  1033. crc = (crc<<B) ^ crctab[(crc>>(W-B)) ^ *cp++];
  1034. return (crc);
  1035. }
  1036. int do_gain (char **argv)
  1037. {
  1038. int range;
  1039. range = simple_strtoul (argv[2], NULL, 10);
  1040. if ((range < 1) || (range > 3))
  1041. {
  1042. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  1043. return 1;
  1044. }
  1045. tsc2000_set_range (range);
  1046. return (0);
  1047. }
  1048. int do_eeprom (char **argv)
  1049. {
  1050. #if (CONFIG_COMMANDS & CFG_CMD_I2C)
  1051. if (strcmp (argv[2], "read") == 0) {
  1052. return (trab_eeprom_read (argv));
  1053. }
  1054. else if (strcmp (argv[2], "write") == 0) {
  1055. return (trab_eeprom_write (argv));
  1056. }
  1057. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  1058. return (1);
  1059. #else
  1060. printf ("No I2C support enabled (CFG_CMD_I2C), could not write "
  1061. "to EEPROM\n");
  1062. return (1);
  1063. #endif /* CFG_CMD_I2C */
  1064. }
  1065. #if (CONFIG_COMMANDS & CFG_CMD_I2C)
  1066. static int trab_eeprom_read (char **argv)
  1067. {
  1068. int i;
  1069. int len;
  1070. unsigned int addr;
  1071. long int value = 0;
  1072. uchar *buffer;
  1073. buffer = (uchar *) &value;
  1074. addr = simple_strtoul (argv[3], NULL, 10);
  1075. addr &= 0xfff;
  1076. len = simple_strtoul (argv[4], NULL, 10);
  1077. if ((len < 1) || (len > 4)) {
  1078. printf ("%s: invalid parameter %s\n", __FUNCTION__,
  1079. argv[4]);
  1080. return (1);
  1081. }
  1082. for (i = 0; i < len; i++) {
  1083. if (i2c_read (I2C_EEPROM_DEV_ADDR, addr+i, 1, buffer+i, 1)) {
  1084. printf ("%s: could not read from i2c device %#x"
  1085. ", addr %d\n", __FUNCTION__,
  1086. I2C_EEPROM_DEV_ADDR, addr);
  1087. return (1);
  1088. }
  1089. }
  1090. print_identifier ();
  1091. if (strcmp (argv[5], "-") == 0) {
  1092. if (len == 1)
  1093. printf ("%d\n", (signed char) value);
  1094. else if (len == 2)
  1095. printf ("%d\n", (signed short int) value);
  1096. else
  1097. printf ("%ld\n", value);
  1098. }
  1099. else {
  1100. if (len == 1)
  1101. printf ("%d\n", (unsigned char) value);
  1102. else if (len == 2)
  1103. printf ("%d\n", (unsigned short int) value);
  1104. else
  1105. printf ("%ld\n", (unsigned long int) value);
  1106. }
  1107. return (0);
  1108. }
  1109. static int trab_eeprom_write (char **argv)
  1110. {
  1111. int i;
  1112. int len;
  1113. unsigned int addr;
  1114. long int value = 0;
  1115. uchar *buffer;
  1116. buffer = (uchar *) &value;
  1117. addr = simple_strtoul (argv[3], NULL, 10);
  1118. addr &= 0xfff;
  1119. len = simple_strtoul (argv[4], NULL, 10);
  1120. if ((len < 1) || (len > 4)) {
  1121. printf ("%s: invalid parameter %s\n", __FUNCTION__,
  1122. argv[4]);
  1123. return (1);
  1124. }
  1125. value = simple_strtol (argv[5], NULL, 10);
  1126. debug ("value=%ld\n", value);
  1127. for (i = 0; i < len; i++) {
  1128. if (i2c_write (I2C_EEPROM_DEV_ADDR, addr+i, 1, buffer+i, 1)) {
  1129. printf ("%s: could not write to i2c device %d"
  1130. ", addr %d\n", __FUNCTION__,
  1131. I2C_EEPROM_DEV_ADDR, addr);
  1132. return (1);
  1133. }
  1134. #if 0
  1135. printf ("chip=%#x, addr+i=%#x+%d=%p, alen=%d, *buffer+i="
  1136. "%#x+%d=%p=%#x \n",I2C_EEPROM_DEV_ADDR_DEV_ADDR , addr,
  1137. i, addr+i, 1, buffer, i, buffer+i, *(buffer+i));
  1138. #endif
  1139. udelay (30000); /* wait for EEPROM ready */
  1140. }
  1141. return (0);
  1142. }
  1143. int i2c_write_multiple (uchar chip, uint addr, int alen,
  1144. uchar *buffer, int len)
  1145. {
  1146. int i;
  1147. if (alen != 1) {
  1148. printf ("%s: addr len other than 1 not supported\n",
  1149. __FUNCTION__);
  1150. return (1);
  1151. }
  1152. for (i = 0; i < len; i++) {
  1153. if (i2c_write (chip, addr+i, alen, buffer+i, 1)) {
  1154. printf ("%s: could not write to i2c device %d"
  1155. ", addr %d\n", __FUNCTION__, chip, addr);
  1156. return (1);
  1157. }
  1158. #if 0
  1159. printf ("chip=%#x, addr+i=%#x+%d=%p, alen=%d, *buffer+i="
  1160. "%#x+%d=%p=\"%.1s\"\n", chip, addr, i, addr+i,
  1161. alen, buffer, i, buffer+i, buffer+i);
  1162. #endif
  1163. udelay (30000);
  1164. }
  1165. return (0);
  1166. }
  1167. int i2c_read_multiple ( uchar chip, uint addr, int alen,
  1168. uchar *buffer, int len)
  1169. {
  1170. int i;
  1171. if (alen != 1) {
  1172. printf ("%s: addr len other than 1 not supported\n",
  1173. __FUNCTION__);
  1174. return (1);
  1175. }
  1176. for (i = 0; i < len; i++) {
  1177. if (i2c_read (chip, addr+i, alen, buffer+i, 1)) {
  1178. printf ("%s: could not read from i2c device %#x"
  1179. ", addr %d\n", __FUNCTION__, chip, addr);
  1180. return (1);
  1181. }
  1182. }
  1183. return (0);
  1184. }
  1185. #endif /* CFG_CMD_I2C */