trab_fkt.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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. /* assignment of CPU internal ADC channels with TRAB hardware */
  30. #define VCC5V 2
  31. #define VCC12V 3
  32. /* CPLD-Register for controlling TRAB hardware functions */
  33. #define CPLD_BUTTONS ((volatile unsigned long *)0x04020000)
  34. #define CPLD_FILL_LEVEL ((volatile unsigned long *)0x04008000)
  35. #define CPLD_ROTARY_SWITCH ((volatile unsigned long *)0x04018000)
  36. #define CPLD_RS485_RE ((volatile unsigned long *)0x04028000)
  37. /* timer configuration bits for buzzer and PWM */
  38. #define START2 (1 << 12)
  39. #define UPDATE2 (1 << 13)
  40. #define INVERT2 (1 << 14)
  41. #define RELOAD2 (1 << 15)
  42. #define START3 (1 << 16)
  43. #define UPDATE3 (1 << 17)
  44. #define INVERT3 (1 << 18)
  45. #define RELOAD3 (1 << 19)
  46. #define PCLK 66000000
  47. #define BUZZER_FREQ 1000 /* frequency in Hz */
  48. #define BUZZER_TIME 1000000 /* time in us */
  49. #define PWM_FREQ 500
  50. /* definitions of I2C EEPROM device address */
  51. #define I2C_EEPROM_DEV_ADDR 0x54
  52. /* definition for touch panel calibration points */
  53. #define CALIB_TL 0 /* calibration point in (T)op (L)eft corner */
  54. #define CALIB_DR 1 /* calibration point in (D)own (R)ight corner */
  55. /* EEPROM addresse map */
  56. #define SERIAL_NUMBER 8
  57. #define TOUCH_X0 52
  58. #define TOUCH_Y0 54
  59. #define TOUCH_X1 56
  60. #define TOUCH_Y1 58
  61. #define CRC16 60
  62. /* EEPROM stuff */
  63. #define EEPROM_MAX_CRC_BUF 64
  64. /* RS485 stuff */
  65. #define RS485_MAX_RECEIVE_BUF_LEN 100
  66. /* Bit definitions for ADCCON */
  67. #define ADC_ENABLE_START 0x1
  68. #define ADC_READ_START 0x2
  69. #define ADC_STDBM 0x4
  70. #define ADC_INP_AIN0 (0x0 << 3)
  71. #define ADC_INP_AIN1 (0x1 << 3)
  72. #define ADC_INP_AIN2 (0x2 << 3)
  73. #define ADC_INP_AIN3 (0x3 << 3)
  74. #define ADC_INP_AIN4 (0x4 << 3)
  75. #define ADC_INP_AIN5 (0x5 << 3)
  76. #define ADC_INP_AIN6 (0x6 << 3)
  77. #define ADC_INP_AIN7 (0x7 << 3)
  78. #define ADC_PRSCEN 0x4000
  79. #define ADC_ECFLG 0x8000
  80. /* function test functions */
  81. int do_dip (void);
  82. int do_info (void);
  83. int do_vcc5v (void);
  84. int do_vcc12v (void);
  85. int do_buttons (void);
  86. int do_fill_level (void);
  87. int do_rotary_switch (void);
  88. int do_pressure (void);
  89. int do_v_bat (void);
  90. int do_vfd_id (void);
  91. int do_buzzer (void);
  92. int do_led (char **);
  93. int do_full_bridge (char **);
  94. int do_dac (char **);
  95. int do_motor_contact (void);
  96. int do_motor (char **);
  97. int do_pwm (char **);
  98. int do_thermo (char **);
  99. int do_touch (char **);
  100. int do_rs485 (char **);
  101. int do_serial_number (char **);
  102. int do_crc16 (void);
  103. /* helper functions */
  104. static void adc_init (void);
  105. static int adc_read (unsigned int channel);
  106. static void print_identifier (void);
  107. static void touch_wait_pressed (void);
  108. static void touch_read_x_y (int *x, int *y);
  109. static int touch_write_clibration_values (int calib_point, int x, int y);
  110. static int rs485_send_line (const char *data);
  111. static int rs485_receive_chars (char *data, int timeout);
  112. static unsigned short updcrc(unsigned short icrc, unsigned char *icp,
  113. unsigned int icnt);
  114. /*
  115. * TRAB board specific commands. Especially commands for burn-in and function
  116. * test.
  117. */
  118. int trab_fkt (int argc, char *argv[])
  119. {
  120. int i;
  121. app_startup(argv);
  122. if (get_version () != XF_VERSION) {
  123. printf ("Wrong XF_VERSION. Please re-compile with actual "
  124. "u-boot sources\n");
  125. printf ("Example expects ABI version %d\n", XF_VERSION);
  126. printf ("Actual U-Boot ABI version %d\n", (int)get_version());
  127. return 1;
  128. }
  129. debug ("argc = %d\n", argc);
  130. for (i=0; i<=argc; ++i) {
  131. debug ("argv[%d] = \"%s\"\n", i, argv[i] ? argv[i] : "<NULL>");
  132. }
  133. adc_init ();
  134. switch (argc) {
  135. case 0:
  136. case 1:
  137. break;
  138. case 2:
  139. if (strcmp (argv[1], "info") == 0) {
  140. return (do_info ());
  141. }
  142. if (strcmp (argv[1], "dip") == 0) {
  143. return (do_dip ());
  144. }
  145. if (strcmp (argv[1], "vcc5v") == 0) {
  146. return (do_vcc5v ());
  147. }
  148. if (strcmp (argv[1], "vcc12v") == 0) {
  149. return (do_vcc12v ());
  150. }
  151. if (strcmp (argv[1], "buttons") == 0) {
  152. return (do_buttons ());
  153. }
  154. if (strcmp (argv[1], "fill_level") == 0) {
  155. return (do_fill_level ());
  156. }
  157. if (strcmp (argv[1], "rotary_switch") == 0) {
  158. return (do_rotary_switch ());
  159. }
  160. if (strcmp (argv[1], "pressure") == 0) {
  161. return (do_pressure ());
  162. }
  163. if (strcmp (argv[1], "v_bat") == 0) {
  164. return (do_v_bat ());
  165. }
  166. if (strcmp (argv[1], "vfd_id") == 0) {
  167. return (do_vfd_id ());
  168. }
  169. if (strcmp (argv[1], "buzzer") == 0) {
  170. return (do_buzzer ());
  171. }
  172. if (strcmp (argv[1], "motor_contact") == 0) {
  173. return (do_motor_contact ());
  174. }
  175. if (strcmp (argv[1], "crc16") == 0) {
  176. return (do_crc16 ());
  177. }
  178. break;
  179. case 3:
  180. if (strcmp (argv[1], "full_bridge") == 0) {
  181. return (do_full_bridge (argv));
  182. }
  183. if (strcmp (argv[1], "dac") == 0) {
  184. return (do_dac (argv));
  185. }
  186. if (strcmp (argv[1], "motor") == 0) {
  187. return (do_motor (argv));
  188. }
  189. if (strcmp (argv[1], "pwm") == 0) {
  190. return (do_pwm (argv));
  191. }
  192. if (strcmp (argv[1], "thermo") == 0) {
  193. return (do_thermo (argv));
  194. }
  195. if (strcmp (argv[1], "touch") == 0) {
  196. return (do_touch (argv));
  197. }
  198. if (strcmp (argv[1], "serial_number") == 0) {
  199. return (do_serial_number (argv));
  200. }
  201. break;
  202. case 4:
  203. if (strcmp (argv[1], "led") == 0) {
  204. return (do_led (argv));
  205. }
  206. if (strcmp (argv[1], "rs485") == 0) {
  207. return (do_rs485 (argv));
  208. }
  209. if (strcmp (argv[1], "serial_number") == 0) {
  210. return (do_serial_number (argv));
  211. }
  212. break;
  213. default:
  214. break;
  215. }
  216. printf ("Usage:\n<command> <parameter1> <parameter2> ...\n");
  217. return 1;
  218. }
  219. int do_info (void)
  220. {
  221. printf ("Stand-alone application for TRAB board function test\n");
  222. printf ("Built: %s at %s\n", __DATE__ , __TIME__ );
  223. return 0;
  224. }
  225. int do_dip (void)
  226. {
  227. unsigned int result = 0;
  228. int adc_val;
  229. int i;
  230. /***********************************************************
  231. DIP switch connection (according to wa4-cpu.sp.301.pdf, page 3):
  232. SW1 - AIN4
  233. SW2 - AIN5
  234. SW3 - AIN6
  235. SW4 - AIN7
  236. "On" DIP switch position short-circuits the voltage from
  237. the input channel (i.e. '0' conversion result means "on").
  238. *************************************************************/
  239. for (i = 7; i > 3; i--) {
  240. if ((adc_val = adc_read (i)) == -1) {
  241. printf ("Channel %d could not be read\n", i);
  242. return 1;
  243. }
  244. /*
  245. * Input voltage (switch open) is 1.8 V.
  246. * (Vin_High/VRef)*adc_res = (1,8V/2,5V)*1023) = 736
  247. * Set trigger at halve that value.
  248. */
  249. if (adc_val < 368)
  250. result |= (1 << (i-4));
  251. }
  252. /* print result to console */
  253. print_identifier ();
  254. for (i = 0; i < 4; i++) {
  255. if ((result & (1 << i)) == 0)
  256. printf("0");
  257. else
  258. printf("1");
  259. }
  260. printf("\n");
  261. return 0;
  262. }
  263. int do_vcc5v (void)
  264. {
  265. int result;
  266. /* VCC5V is connected to channel 2 */
  267. if ((result = adc_read (VCC5V)) == -1) {
  268. printf ("VCC5V could not be read\n");
  269. return 1;
  270. }
  271. /*
  272. * Calculate voltage value. Split in two parts because there is no
  273. * floating point support. VCC5V is connected over an resistor divider:
  274. * VCC5V=ADCval*2,5V/1023*(10K+30K)/10K.
  275. */
  276. print_identifier ();
  277. printf ("%d", (result & 0x3FF)* 10 / 1023);
  278. printf (".%d", ((result & 0x3FF)* 10 % 1023)* 10 / 1023);
  279. printf ("%d V\n", (((result & 0x3FF) * 10 % 1023 ) * 10 % 1023)
  280. * 10 / 1024);
  281. return 0;
  282. }
  283. int do_vcc12v (void)
  284. {
  285. int result;
  286. if ((result = adc_read (VCC12V)) == -1) {
  287. printf ("VCC12V could not be read\n");
  288. return 1;
  289. }
  290. /*
  291. * Calculate voltage value. Split in two parts because there is no
  292. * floating point support. VCC5V is connected over an resistor divider:
  293. * VCC12V=ADCval*2,5V/1023*(30K+270K)/30K.
  294. */
  295. print_identifier ();
  296. printf ("%d", (result & 0x3FF)* 25 / 1023);
  297. printf (".%d V\n", ((result & 0x3FF)* 25 % 1023) * 10 / 1023);
  298. return 0;
  299. }
  300. static int adc_read (unsigned int channel)
  301. {
  302. int j = 1000; /* timeout value for wait loop in us */
  303. S3C2400_ADC *padc;
  304. padc = S3C2400_GetBase_ADC();
  305. channel &= 0x7;
  306. debug ("%s: adccon %#x\n", __FUNCTION__, padc->ADCCON);
  307. padc->ADCCON &= ~ADC_STDBM; /* select normal mode */
  308. padc->ADCCON &= ~(0x7 << 3); /* clear the channel bits */
  309. padc->ADCCON |= ((channel << 3) | ADC_ENABLE_START);
  310. debug ("%s: reading ch %d, addcon %#x\n", __FUNCTION__,
  311. (padc->ADCCON >> 3) & 0x7, padc->ADCCON);
  312. while (j--) {
  313. if ((padc->ADCCON & ADC_ENABLE_START) == 0)
  314. break;
  315. udelay (1);
  316. }
  317. if (j == 0) {
  318. printf("%s: ADC timeout\n", __FUNCTION__);
  319. padc->ADCCON |= ADC_STDBM; /* select standby mode */
  320. return -1;
  321. }
  322. padc->ADCCON |= ADC_STDBM; /* select standby mode */
  323. debug ("%s: return %#x, adccon %#x\n", __FUNCTION__, padc->ADCDAT & 0x3FF,
  324. padc->ADCCON);
  325. return (padc->ADCDAT & 0x3FF);
  326. }
  327. static void adc_init (void)
  328. {
  329. S3C2400_ADC *padc;
  330. padc = S3C2400_GetBase_ADC();
  331. debug ("%s: adccon %#x\n", __FUNCTION__, padc->ADCCON);
  332. padc->ADCCON &= ~(0xff << 6); /* clear prescaler bits */
  333. padc->ADCCON |= ((65 << 6) | ADC_PRSCEN); /* set prescaler */
  334. debug ("%s: init completed: adccon %#x\n", __FUNCTION__, padc->ADCCON);
  335. return;
  336. }
  337. int do_buttons (void)
  338. {
  339. int result;
  340. int i;
  341. result = *CPLD_BUTTONS; /* read CPLD */
  342. debug ("%s: cpld_taster (32 bit) %#x\n", __FUNCTION__, result);
  343. /* print result to console */
  344. print_identifier ();
  345. for (i = 16; i <= 19; i++) {
  346. if ((result & (1 << i)) == 0)
  347. printf("0");
  348. else
  349. printf("1");
  350. }
  351. printf("\n");
  352. return 0;
  353. }
  354. int do_fill_level (void)
  355. {
  356. int result;
  357. result = *CPLD_FILL_LEVEL; /* read CPLD */
  358. debug ("%s: cpld_fuellstand (32 bit) %#x\n", __FUNCTION__, result);
  359. /* print result to console */
  360. print_identifier ();
  361. if ((result & (1 << 16)) == 0)
  362. printf("0\n");
  363. else
  364. printf("1\n");
  365. return 0;
  366. }
  367. int do_rotary_switch (void)
  368. {
  369. int result;
  370. result = *CPLD_ROTARY_SWITCH; /* read CPLD */
  371. debug ("%s: cpld_inc (32 bit) %#x\n", __FUNCTION__, result);
  372. *CPLD_ROTARY_SWITCH |= (3 << 16); /* clear direction bits in CPLD */
  373. /* print result to console */
  374. print_identifier ();
  375. if ((result & (1 << 16)) == (1 << 16))
  376. printf("R");
  377. if ((result & (1 << 17)) == (1 << 17))
  378. printf("L");
  379. if (((result & (1 << 16)) == 0) && ((result & (1 << 17)) == 0))
  380. printf("0");
  381. if ((result & (1 << 18)) == 0)
  382. printf("0\n");
  383. else
  384. printf("1\n");
  385. return 0;
  386. }
  387. int do_vfd_id (void)
  388. {
  389. int i;
  390. long int pcup_old, pccon_old;
  391. int vfd_board_id;
  392. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  393. /* try to red vfd board id from the value defined by pull-ups */
  394. pcup_old = gpio->PCUP;
  395. pccon_old = gpio->PCCON;
  396. gpio->PCUP = (gpio->PCUP & 0xFFF0); /* activate GPC0...GPC3 pull-ups */
  397. gpio->PCCON = (gpio->PCCON & 0xFFFFFF00); /* configure GPC0...GPC3 as
  398. * inputs */
  399. udelay (10); /* allow signals to settle */
  400. vfd_board_id = (~gpio->PCDAT) & 0x000F; /* read GPC0...GPC3 port pins */
  401. gpio->PCCON = pccon_old;
  402. gpio->PCUP = pcup_old;
  403. /* print vfd_board_id to console */
  404. print_identifier ();
  405. for (i = 0; i < 4; i++) {
  406. if ((vfd_board_id & (1 << i)) == 0)
  407. printf("0");
  408. else
  409. printf("1");
  410. }
  411. printf("\n");
  412. return 0;
  413. }
  414. int do_buzzer (void)
  415. {
  416. int counter;
  417. S3C24X0_TIMERS * const timers = S3C24X0_GetBase_TIMERS();
  418. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  419. /* configure pin GPD7 as TOUT2 */
  420. gpio->PDCON &= ~0xC000;
  421. gpio->PDCON |= 0x8000;
  422. /* set prescaler for timer 2, 3 and 4 */
  423. timers->TCFG0 &= ~0xFF00;
  424. timers->TCFG0 |= 0x0F00;
  425. /* set divider for timer 2 */
  426. timers->TCFG1 &= ~0xF00;
  427. timers->TCFG1 |= 0x300;
  428. /* set frequency */
  429. counter = (PCLK / BUZZER_FREQ) >> 9;
  430. timers->ch[2].TCNTB = counter;
  431. timers->ch[2].TCMPB = counter / 2;
  432. debug ("%s: frequency: %d, duration: %d\n", __FUNCTION__, BUZZER_FREQ,
  433. BUZZER_TIME);
  434. /* start */
  435. timers->TCON = (timers->TCON | UPDATE2 | RELOAD2) & ~INVERT2;
  436. timers->TCON = (timers->TCON | START2) & ~UPDATE2;
  437. udelay (BUZZER_TIME);
  438. /* stop */
  439. timers->TCON &= ~(START2 | RELOAD2);
  440. /* port pin configuration */
  441. gpio->PDCON &= ~0xC000;
  442. gpio->PDCON |= 0x4000;
  443. gpio->PDDAT &= ~0x80;
  444. return 0;
  445. }
  446. int do_led (char **argv)
  447. {
  448. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  449. /* configure PC14 and PC15 as output */
  450. gpio->PCCON &= ~(0xF << 28);
  451. gpio->PCCON |= (0x5 << 28);
  452. /* configure PD0 and PD4 as output */
  453. gpio->PDCON &= ~((0x3 << 8) | 0x3);
  454. gpio->PDCON |= ((0x1 << 8) | 0x1);
  455. switch (simple_strtoul(argv[2], NULL, 10)) {
  456. case 0:
  457. case 1:
  458. break;
  459. case 2:
  460. if (strcmp (argv[3], "on") == 0)
  461. gpio->PCDAT |= (1 << 14);
  462. else
  463. gpio->PCDAT &= ~(1 << 14);
  464. return 0;
  465. case 3:
  466. if (strcmp (argv[3], "on") == 0)
  467. gpio->PCDAT |= (1 << 15);
  468. else
  469. gpio->PCDAT &= ~(1 << 15);
  470. return 0;
  471. case 4:
  472. if (strcmp (argv[3], "on") == 0)
  473. gpio->PDDAT |= (1 << 0);
  474. else
  475. gpio->PDDAT &= ~(1 << 0);
  476. return 0;
  477. case 5:
  478. if (strcmp (argv[3], "on") == 0)
  479. gpio->PDDAT |= (1 << 4);
  480. else
  481. gpio->PDDAT &= ~(1 << 4);
  482. return 0;
  483. default:
  484. break;
  485. }
  486. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  487. return 1;
  488. }
  489. int do_full_bridge (char **argv)
  490. {
  491. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  492. /* configure PD5 and PD6 as output */
  493. gpio->PDCON &= ~((0x3 << 5*2) | (0x3 << 6*2));
  494. gpio->PDCON |= ((0x1 << 5*2) | (0x1 << 6*2));
  495. if (strcmp (argv[2], "+") == 0) {
  496. gpio->PDDAT |= (1 << 5);
  497. gpio->PDDAT |= (1 << 6);
  498. return 0;
  499. }
  500. else if (strcmp (argv[2], "-") == 0) {
  501. gpio->PDDAT &= ~(1 << 5);
  502. gpio->PDDAT |= (1 << 6);
  503. return 0;
  504. }
  505. else if (strcmp (argv[2], "off") == 0) {
  506. gpio->PDDAT &= ~(1 << 5);
  507. gpio->PDDAT &= ~(1 << 6);
  508. return 0;
  509. }
  510. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  511. return 1;
  512. }
  513. /* val must be in [0, 4095] */
  514. static inline unsigned long tsc2000_to_uv (u16 val)
  515. {
  516. return ((250000 * val) / 4096) * 10;
  517. }
  518. int do_dac (char **argv)
  519. {
  520. int brightness;
  521. /* initialize SPI */
  522. spi_init ();
  523. if (((brightness = simple_strtoul (argv[2], NULL, 10)) < 0) ||
  524. (brightness > 255)) {
  525. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  526. return 1;
  527. }
  528. tsc2000_write(TSC2000_REG_DACCTL, 0x0); /* Power up DAC */
  529. tsc2000_write(TSC2000_REG_DAC, brightness & 0xff);
  530. return 0;
  531. }
  532. int do_v_bat (void)
  533. {
  534. unsigned long ret, res;
  535. /* initialize SPI */
  536. spi_init ();
  537. tsc2000_write(TSC2000_REG_ADC, 0x1836);
  538. /* now wait for data available */
  539. adc_wait_conversion_done();
  540. ret = tsc2000_read(TSC2000_REG_BAT1);
  541. res = (tsc2000_to_uv(ret) + 1250) / 2500;
  542. res += (ERROR_BATTERY * res) / 1000;
  543. print_identifier ();
  544. printf ("%ld", (res / 100));
  545. printf (".%ld", ((res % 100) / 10));
  546. printf ("%ld V\n", (res % 10));
  547. return 0;
  548. }
  549. int do_pressure (void)
  550. {
  551. /* initialize SPI */
  552. spi_init ();
  553. tsc2000_write(TSC2000_REG_ADC, 0x2436);
  554. /* now wait for data available */
  555. adc_wait_conversion_done();
  556. print_identifier ();
  557. printf ("%d\n", tsc2000_read(TSC2000_REG_AUX2));
  558. return 0;
  559. }
  560. int do_motor_contact (void)
  561. {
  562. int result;
  563. result = *CPLD_FILL_LEVEL; /* read CPLD */
  564. debug ("%s: cpld_fuellstand (32 bit) %#x\n", __FUNCTION__, result);
  565. /* print result to console */
  566. print_identifier ();
  567. if ((result & (1 << 17)) == 0)
  568. printf("0\n");
  569. else
  570. printf("1\n");
  571. return 0;
  572. }
  573. int do_motor (char **argv)
  574. {
  575. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  576. /* Configure I/O port */
  577. gpio->PGCON &= ~(0x3 << 0);
  578. gpio->PGCON |= (0x1 << 0);
  579. if (strcmp (argv[2], "on") == 0) {
  580. gpio->PGDAT &= ~(1 << 0);
  581. return 0;
  582. }
  583. if (strcmp (argv[2], "off") == 0) {
  584. gpio->PGDAT |= (1 << 0);
  585. return 0;
  586. }
  587. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  588. return 1;
  589. }
  590. static void print_identifier (void)
  591. {
  592. printf ("## FKT: ");
  593. }
  594. int do_pwm (char **argv)
  595. {
  596. int counter;
  597. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  598. S3C24X0_TIMERS * const timers = S3C24X0_GetBase_TIMERS();
  599. if (strcmp (argv[2], "on") == 0) {
  600. /* configure pin GPD8 as TOUT3 */
  601. gpio->PDCON &= ~(0x3 << 8*2);
  602. gpio->PDCON |= (0x2 << 8*2);
  603. /* set prescaler for timer 2, 3 and 4 */
  604. timers->TCFG0 &= ~0xFF00;
  605. timers->TCFG0 |= 0x0F00;
  606. /* set divider for timer 3 */
  607. timers->TCFG1 &= ~(0xf << 12);
  608. timers->TCFG1 |= (0x3 << 12);
  609. /* set frequency */
  610. counter = (PCLK / PWM_FREQ) >> 9;
  611. timers->ch[3].TCNTB = counter;
  612. timers->ch[3].TCMPB = counter / 2;
  613. /* start timer */
  614. timers->TCON = (timers->TCON | UPDATE3 | RELOAD3) & ~INVERT3;
  615. timers->TCON = (timers->TCON | START3) & ~UPDATE3;
  616. return 0;
  617. }
  618. if (strcmp (argv[2], "off") == 0) {
  619. /* stop timer */
  620. timers->TCON &= ~(START2 | RELOAD2);
  621. /* configure pin GPD8 as output and set to 0 */
  622. gpio->PDCON &= ~(0x3 << 8*2);
  623. gpio->PDCON |= (0x1 << 8*2);
  624. gpio->PDDAT &= ~(1 << 8);
  625. return 0;
  626. }
  627. printf ("%s: invalid parameter %s\n", __FUNCTION__, argv[2]);
  628. return 1;
  629. }
  630. int do_thermo (char **argv)
  631. {
  632. int channel, res;
  633. tsc2000_reg_init ();
  634. tsc2000_set_range (3);
  635. if (strcmp (argv[2], "all") == 0) {
  636. int i;
  637. for (i=0; i <= 15; i++) {
  638. res = tsc2000_read_channel(i);
  639. print_identifier ();
  640. printf ("c%d: %d\n", i, res);
  641. }
  642. return 0;
  643. }
  644. channel = simple_strtoul (argv[2], NULL, 10);
  645. res = tsc2000_read_channel(channel);
  646. print_identifier ();
  647. printf ("%d\n", res);
  648. return 0; /* return OK */
  649. }
  650. int do_touch (char **argv)
  651. {
  652. int x, y;
  653. if (strcmp (argv[2], "tl") == 0) {
  654. touch_wait_pressed();
  655. touch_read_x_y (&x, &y);
  656. print_identifier ();
  657. printf ("x=%d y=%d\n", x, y);
  658. return touch_write_clibration_values (CALIB_TL, x, y);
  659. }
  660. else if (strcmp (argv[2], "dr") == 0) {
  661. touch_wait_pressed();
  662. touch_read_x_y (&x, &y);
  663. print_identifier ();
  664. printf ("x=%d y=%d\n", x, y);
  665. return touch_write_clibration_values (CALIB_DR, x, y);
  666. }
  667. return 1; /* return error */
  668. }
  669. static void touch_wait_pressed (void)
  670. {
  671. while (!(tsc2000_read(TSC2000_REG_ADC) & TC_PSM));
  672. }
  673. static int touch_write_clibration_values (int calib_point, int x, int y)
  674. {
  675. #if (CONFIG_COMMANDS & CFG_CMD_I2C)
  676. tsc2000_reg_init ();
  677. if (calib_point == CALIB_TL) {
  678. if (i2c_write (I2C_EEPROM_DEV_ADDR, TOUCH_X0, 1,
  679. (char *)&x, 2)) {
  680. printf ("could not write to eeprom\n");
  681. return 1;
  682. }
  683. udelay(11000);
  684. if (i2c_write (I2C_EEPROM_DEV_ADDR, TOUCH_Y0, 1,
  685. (char *)&y, 2)) {
  686. printf ("could not write to eeprom\n");
  687. return 1;
  688. }
  689. udelay(11000);
  690. return 0;
  691. }
  692. else if (calib_point == CALIB_DR) {
  693. if (i2c_write (I2C_EEPROM_DEV_ADDR, TOUCH_X1, 1,
  694. (char *)&x, 2)) {
  695. printf ("could not write to eeprom\n");
  696. return 1;
  697. }
  698. udelay(11000);
  699. if (i2c_write (I2C_EEPROM_DEV_ADDR, TOUCH_Y1, 1,
  700. (char *)&y, 2)) {
  701. printf ("could not write to eeprom\n");
  702. return 1;
  703. }
  704. udelay(11000);
  705. return 0;
  706. }
  707. return 1;
  708. #else
  709. printf ("No I2C support enabled (CFG_CMD_I2C), could not write "
  710. "to EEPROM\n");
  711. return (1);
  712. #endif /* CFG_CMD_I2C */
  713. }
  714. static void touch_read_x_y (int *px, int *py)
  715. {
  716. tsc2000_write(TSC2000_REG_ADC, DEFAULT_ADC | TC_AD0 | TC_AD1);
  717. adc_wait_conversion_done();
  718. *px = tsc2000_read(TSC2000_REG_X);
  719. tsc2000_write(TSC2000_REG_ADC, DEFAULT_ADC | TC_AD2);
  720. adc_wait_conversion_done();
  721. *py = tsc2000_read(TSC2000_REG_Y);
  722. }
  723. int do_rs485 (char **argv)
  724. {
  725. int timeout;
  726. char data[RS485_MAX_RECEIVE_BUF_LEN];
  727. if (strcmp (argv[2], "send") == 0) {
  728. return (rs485_send_line (argv[3]));
  729. }
  730. else if (strcmp (argv[2], "receive") == 0) {
  731. timeout = simple_strtoul(argv[3], NULL, 10);
  732. if (rs485_receive_chars (data, timeout) != 0) {
  733. print_identifier ();
  734. printf ("## nothing received\n");
  735. return (1);
  736. }
  737. else {
  738. print_identifier ();
  739. printf ("%s\n", data);
  740. return (0);
  741. }
  742. }
  743. printf ("%s: unknown command %s\n", __FUNCTION__, argv[2]);
  744. return (1); /* unknown command, return error */
  745. }
  746. static int rs485_send_line (const char *data)
  747. {
  748. rs485_init ();
  749. trab_rs485_enable_tx ();
  750. rs485_puts (data);
  751. rs485_putc ('\n');
  752. return (0);
  753. }
  754. static int rs485_receive_chars (char *data, int timeout)
  755. {
  756. int i;
  757. int receive_count = 0;
  758. rs485_init ();
  759. trab_rs485_enable_rx ();
  760. /* test every 1 ms for received characters to avoid a receive FIFO
  761. * overrun (@ 38.400 Baud) */
  762. for (i = 0; i < (timeout * 1000); i++) {
  763. while (rs485_tstc ()) {
  764. if (receive_count >= RS485_MAX_RECEIVE_BUF_LEN-1)
  765. break;
  766. *data++ = rs485_getc ();
  767. receive_count++;
  768. }
  769. udelay (1000); /* pause 1 ms */
  770. }
  771. *data = '\0'; /* terminate string */
  772. if (receive_count == 0)
  773. return (1);
  774. else
  775. return (0);
  776. }
  777. int do_serial_number (char **argv)
  778. {
  779. #if (CONFIG_COMMANDS & CFG_CMD_I2C)
  780. unsigned int serial_number;
  781. if (strcmp (argv[2], "read") == 0) {
  782. if (i2c_read (I2C_EEPROM_DEV_ADDR, SERIAL_NUMBER, 1,
  783. (char *)&serial_number, 4)) {
  784. printf ("could not read from eeprom\n");
  785. return (1);
  786. }
  787. print_identifier ();
  788. printf ("%08d\n", serial_number);
  789. return (0);
  790. }
  791. else if (strcmp (argv[2], "write") == 0) {
  792. serial_number = simple_strtoul(argv[3], NULL, 10);
  793. if (i2c_write (I2C_EEPROM_DEV_ADDR, SERIAL_NUMBER, 1,
  794. (char *)&serial_number, 4)) {
  795. printf ("could not write to eeprom\n");
  796. return (1);
  797. }
  798. return (0);
  799. }
  800. printf ("%s: unknown command %s\n", __FUNCTION__, argv[2]);
  801. return (1); /* unknown command, return error */
  802. #else
  803. printf ("No I2C support enabled (CFG_CMD_I2C), could not write "
  804. "to EEPROM\n");
  805. return (1);
  806. #endif /* CFG_CMD_I2C */
  807. }
  808. int do_crc16 (void)
  809. {
  810. #if (CONFIG_COMMANDS & CFG_CMD_I2C)
  811. int crc;
  812. char buf[EEPROM_MAX_CRC_BUF];
  813. if (i2c_read (I2C_EEPROM_DEV_ADDR, 0, 1, buf, 60)) {
  814. printf ("could not read from eeprom\n");
  815. return (1);
  816. }
  817. crc = 0; /* start value of crc calculation */
  818. crc = updcrc (crc, buf, 60);
  819. print_identifier ();
  820. printf ("crc16=%#04x\n", crc);
  821. if (i2c_write (I2C_EEPROM_DEV_ADDR, CRC16, 1, (char *)&crc,
  822. sizeof (crc))) {
  823. printf ("could not read from eeprom\n");
  824. return (1);
  825. }
  826. return (0);
  827. #else
  828. printf ("No I2C support enabled (CFG_CMD_I2C), could not write "
  829. "to EEPROM\n");
  830. return (1);
  831. #endif /* CFG_CMD_I2C */
  832. }
  833. /*
  834. * Calculate, intelligently, the CRC of a dataset incrementally given a
  835. * buffer full at a time.
  836. * Initialize crc to 0 for XMODEM, -1 for CCITT.
  837. *
  838. * Usage:
  839. * newcrc = updcrc( oldcrc, bufadr, buflen )
  840. * unsigned int oldcrc, buflen;
  841. * char *bufadr;
  842. *
  843. * Compile with -DTEST to generate program that prints CRC of stdin to stdout.
  844. * Compile with -DMAKETAB to print values for crctab to stdout
  845. */
  846. /* the CRC polynomial. This is used by XMODEM (almost CCITT).
  847. * If you change P, you must change crctab[]'s initial value to what is
  848. * printed by initcrctab()
  849. */
  850. #define P 0x1021
  851. /* number of bits in CRC: don't change it. */
  852. #define W 16
  853. /* this the number of bits per char: don't change it. */
  854. #define B 8
  855. static unsigned short crctab[1<<B] = { /* as calculated by initcrctab() */
  856. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
  857. 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  858. 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
  859. 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
  860. 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
  861. 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
  862. 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
  863. 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
  864. 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
  865. 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
  866. 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
  867. 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
  868. 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
  869. 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
  870. 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
  871. 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
  872. 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
  873. 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
  874. 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
  875. 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
  876. 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
  877. 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  878. 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
  879. 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
  880. 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
  881. 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
  882. 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
  883. 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
  884. 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
  885. 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
  886. 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
  887. 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
  888. };
  889. static unsigned short updcrc(unsigned short icrc, unsigned char *icp,
  890. unsigned int icnt )
  891. {
  892. register unsigned short crc = icrc;
  893. register unsigned char *cp = icp;
  894. register unsigned int cnt = icnt;
  895. while (cnt--)
  896. crc = (crc<<B) ^ crctab[(crc>>(W-B)) ^ *cp++];
  897. return (crc);
  898. }