i2ctest.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*!
  2. * Copyright (C) 2013 Uwe Bonnes (bon@elektron.ikp.physik.tu-darmstadt.de)
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. #include <cfg/crt.h> /* Floating point configuration. */
  35. #include <string.h>
  36. #include <stdio.h>
  37. #include <io.h>
  38. #include <dev/board.h>
  39. #include <sys/timer.h>
  40. #include <sys/thread.h>
  41. #include <sys/event.h>
  42. #include <dev/gpio.h>
  43. #include <dev/i2cbus.h>
  44. static const char *banner = "\nNut/OS I2C Sample on " BOARDNAME
  45. " " __DATE__ " " __TIME__;
  46. #if defined(DEF_I2CBUS)
  47. static char inbuf[128];
  48. #endif
  49. #define MAX_TEST_DEVICES 16
  50. int found_device[MAX_TEST_DEVICES];
  51. #if defined(LED1_PORT) && defined( LED1_PIN)
  52. #define LED1_INIT GpioPinConfigSet( LED1_PORT, LED1_PIN, GPIO_CFG_OUTPUT)
  53. #define LED1_TOGGLE GpioPinSet(LED1_PORT, LED1_PIN, \
  54. !(GpioPinGet(LED1_PORT, LED1_PIN)))
  55. #else
  56. #define LED1_INIT
  57. #define LED1_TOGGLE
  58. #endif
  59. #if defined(LED2_PORT) && defined( LED2_PIN)
  60. #define LED2_START_THREAD \
  61. if (NutThreadCreate("t2", LedBlink, 0, 512)== 0)\
  62. puts("Can't create LED thread\n"); \
  63. else \
  64. puts("LED thread started\n");
  65. THREAD(LedBlink, arg)
  66. {
  67. GpioPinConfigSet( LED2_PORT, LED2_PIN, GPIO_CFG_OUTPUT);
  68. for(;;)
  69. {
  70. NutSleep(100);
  71. GpioPinSetHigh(LED2_PORT, LED2_PIN);
  72. NutSleep(100);
  73. GpioPinSetLow(LED2_PORT, LED2_PIN);
  74. }
  75. }
  76. #else
  77. #define LED2_START_THREAD
  78. #endif
  79. void Hardware_Init(void)
  80. {
  81. #if defined(F4_DISCOVERY)
  82. /* Set all CS43L22 I2C relates pins to a safe state*/
  83. GpioPinConfigSet(NUTGPIO_PORTE, 3, GPIO_CFG_OUTPUT);
  84. GpioPinSetHigh(NUTGPIO_PORTE, 3);
  85. GpioPinConfigSet(NUTGPIO_PORTC, 7, GPIO_CFG_OUTPUT);
  86. GpioPinSetHigh(NUTGPIO_PORTC, 7);
  87. GpioPinConfigSet(NUTGPIO_PORTC, 10, GPIO_CFG_OUTPUT);
  88. GpioPinSetHigh(NUTGPIO_PORTC, 10);
  89. GpioPinConfigSet(NUTGPIO_PORTC, 12, GPIO_CFG_OUTPUT);
  90. GpioPinSetHigh(NUTGPIO_PORTC, 12);
  91. GpioPinConfigSet(NUTGPIO_PORTC, 14, GPIO_CFG_OUTPUT);
  92. GpioPinSetHigh(NUTGPIO_PORTC, 14);
  93. GpioPinConfigSet(AUDIO_RST_PORT, AUDIO_RST_PIN, GPIO_CFG_OUTPUT);
  94. GpioPinSetLow(AUDIO_RST_PORT, AUDIO_RST_PIN);
  95. NutSleep(11);
  96. GpioPinSetHigh(AUDIO_RST_PORT, AUDIO_RST_PIN);
  97. NutSleep(10);
  98. #endif
  99. }
  100. #if defined(DEF_I2CBUS)
  101. #ifndef I2C_SLA_MAX44009
  102. #define I2C_SLA_MAX44009 0x4a
  103. #endif
  104. /*!
  105. * \brief I2C driver control block.
  106. */
  107. static NUTI2C_SLAVE i2cMax44009 = {
  108. NULL,
  109. I2C_SLA_MAX44009,
  110. 3000,
  111. NULL
  112. };
  113. #endif
  114. int dump_max44009(NUTI2C_BUS *bus)
  115. {
  116. #if defined(DEF_I2CBUS)
  117. #define MAX44009_CFG 0x2
  118. int res;
  119. uint8_t reg_addr, cfg, lux_h, lux_l;
  120. int lux;
  121. if (i2cMax44009.slave_bus == 0)
  122. {
  123. res = NutRegisterI2cSlave(&i2cMax44009, bus);
  124. if (res)
  125. return -1;
  126. }
  127. reg_addr = MAX44009_CFG;
  128. res = NutI2cMasterTransceive(&i2cMax44009, &reg_addr, 1, &cfg, 1);
  129. if (res < 0)
  130. return -1;
  131. reg_addr++;
  132. res = NutI2cMasterTransceive(&i2cMax44009, &reg_addr, 1, &lux_h, 1);
  133. if (res < 0)
  134. return -1;
  135. reg_addr++;
  136. /* we should read lux_l after lub_h without sending stop.
  137. * As of 2013, Feb 13, the Ethernut I2CBus API cant do taht
  138. */
  139. res = NutI2cMasterTransceive(&i2cMax44009, &reg_addr, 1, &lux_l, 1);
  140. if (res < 0)
  141. return -1;
  142. lux = ((lux_h & 0xf) * 720)<<((lux_h & 0xf0)>>4);
  143. printf("MAX44009: Lux %6d.%03d cfg %02x lux_h %02x lux_l %02x\n",
  144. lux/1000, lux %1000,cfg, lux_h, lux_l);
  145. return 0;
  146. }
  147. #define CS43L22_AUTO_INC 0x80
  148. #ifndef I2C_SLA_CS43L22
  149. #define I2C_SLA_CS43L22 0x4a
  150. #endif
  151. #if defined(DEF_I2CBUS)
  152. /*!
  153. * \brief I2C driver control block.
  154. */
  155. static NUTI2C_SLAVE i2cCs43l22 = {
  156. NULL,
  157. I2C_SLA_CS43L22,
  158. 3000,
  159. NULL
  160. };
  161. #endif
  162. int dump_cs43l22(NUTI2C_BUS *bus)
  163. {
  164. #define CS43L22_DATA_LEN 0x34
  165. int i, res;
  166. uint8_t data[CS43L22_DATA_LEN];
  167. if (i2cCs43l22.slave_bus == 0)
  168. {
  169. res = NutRegisterI2cSlave(&i2cCs43l22, bus);
  170. if (res)
  171. return -1;
  172. }
  173. data[0] = CS43L22_AUTO_INC;
  174. res = NutI2cMasterTransceive(&i2cCs43l22, data, 1, data, CS43L22_DATA_LEN);
  175. for (i=0; i<CS43L22_DATA_LEN; i++)
  176. {
  177. if ((i & 0xf) == 0)
  178. printf(" Addr %02x:", i);
  179. printf(" %02x", data[i]);
  180. if ((i & 0xf) == 0xf)
  181. puts("");
  182. }
  183. if ((i & 0xf) != 0)
  184. puts("");
  185. return 0;
  186. #else
  187. return -1;
  188. #endif
  189. }
  190. #define LMS303_AUTO_INC 0x80
  191. #ifndef I2C_SLA_LSM303_ACCEL
  192. #define I2C_SLA_LSM303_ACCEL 0x19
  193. #endif
  194. #if defined(DEF_I2CBUS)
  195. /*!
  196. * \brief I2C driver control block.
  197. */
  198. static NUTI2C_SLAVE i2cLsm303_accel = {
  199. NULL,
  200. I2C_SLA_LSM303_ACCEL,
  201. 3000,
  202. NULL
  203. };
  204. #endif
  205. int dump_lsm303_accel(NUTI2C_BUS *bus)
  206. {
  207. #if defined(DEF_I2CBUS)
  208. #define LSM303_ACCEL_DATA_LEN 0x6
  209. #define LSM303_ACCEL_DATA 0x28
  210. uint8_t data[LSM303_ACCEL_DATA_LEN];
  211. int res;
  212. int16_t ax, ay, az;
  213. if (i2cLsm303_accel.slave_bus == 0)
  214. {
  215. res = NutRegisterI2cSlave(&i2cLsm303_accel, bus);
  216. if (res)
  217. return -1;
  218. /* Enable Acceleration data Output with 1 Hz*/
  219. data[0] = 0x20;
  220. data[1] = 0x17;
  221. res = NutI2cMasterTransceive(&i2cLsm303_accel, data, 2, 0, 0);
  222. }
  223. data[0] = LMS303_AUTO_INC |LSM303_ACCEL_DATA;
  224. res = NutI2cMasterTransceive(&i2cLsm303_accel, data, 1, data,
  225. LSM303_ACCEL_DATA_LEN);
  226. if (res == -1)
  227. return -1;
  228. ax = (int16_t)(data[1]<< 8 | data[0]);
  229. ay = (int16_t)(data[3]<< 8 | data[2]);
  230. az = (int16_t)(data[5]<< 8 | data[4]);
  231. printf("Accel: ax %6d ay %6d az %6d\n", ax, ay, az);
  232. return 0;
  233. #else
  234. return -1;
  235. #endif
  236. }
  237. #ifndef I2C_SLA_LSM303_MAGNET
  238. #define I2C_SLA_LSM303_MAGNET 0x1e
  239. #endif
  240. #if defined(DEF_I2CBUS)
  241. /*!
  242. * \brief I2C driver control block.
  243. */
  244. static NUTI2C_SLAVE i2cLsm303_magnet = {
  245. NULL,
  246. I2C_SLA_LSM303_MAGNET,
  247. 3000,
  248. NULL
  249. };
  250. #endif
  251. int dump_lsm303_magnet(NUTI2C_BUS *bus)
  252. {
  253. #if defined(DEF_I2CBUS)
  254. #define LSM303_MAGNET_DATA_LEN 0x6
  255. #define LSM303_MAGNET_DATA 0x3
  256. #define LSM303_MAGNET_DATA 0x3
  257. #define LSM303_MAGNET_TEMP_DATA 0x31
  258. #define LSM303_MAGNET_TEMP_DATA_LEN 0x2
  259. uint8_t data[LSM303_MAGNET_DATA_LEN];
  260. int res;
  261. int16_t ax, ay, az, temp;
  262. if (i2cLsm303_magnet.slave_bus == 0)
  263. {
  264. res = NutRegisterI2cSlave(&i2cLsm303_magnet, bus);
  265. if (res)
  266. return -1;
  267. data[0] = 0x0; /* Start writing at XRA_REG_M*/
  268. data[1] = 0x80;/* Enable Magnet Output 0.75 Hz and Temperature*/
  269. data[2] = 0x20;/* Max sensivity*/
  270. data[3] = 0x00;/* Continous conversion*/
  271. res = NutI2cMasterTransceive(&i2cLsm303_magnet, data, 4, 0, 0);
  272. }
  273. data[0] = LMS303_AUTO_INC |LSM303_MAGNET_DATA;
  274. res = NutI2cMasterTransceive(&i2cLsm303_magnet, data, 1, data,
  275. LSM303_MAGNET_DATA_LEN);
  276. if (res == -1)
  277. return -1;
  278. ax = (int16_t)(data[0]<< 8 | data[1]);
  279. az = (int16_t)(data[2]<< 8 | data[3]);
  280. ay = (int16_t)(data[4]<< 8 | data[5]);
  281. printf("Magnet: ax %6d ay %6d az %6d,", ax, ay, az);
  282. data[0] = LMS303_AUTO_INC |LSM303_MAGNET_TEMP_DATA;
  283. res = NutI2cMasterTransceive(&i2cLsm303_magnet, data, 1, data,
  284. LSM303_MAGNET_TEMP_DATA_LEN);
  285. if (res == -1)
  286. return -1;
  287. temp = (int16_t)(data[0]<< 8 | data[1]);
  288. temp = temp >> 4;
  289. temp = temp *125;
  290. printf(" temp %4d.%03d\n", temp/1000, temp%1000);
  291. return 0;
  292. #else
  293. return -1;
  294. #endif
  295. }
  296. #ifndef I2C_SLA_SHT21
  297. #define I2C_SLA_SHT21 0x40
  298. #endif
  299. #if defined(DEF_I2CBUS)
  300. /*!
  301. * \brief I2C driver control block.
  302. */
  303. static NUTI2C_SLAVE i2cSht21 = {
  304. NULL,
  305. I2C_SLA_SHT21,
  306. 3000,
  307. NULL
  308. };
  309. #endif
  310. int dump_sht21(NUTI2C_BUS *bus)
  311. {
  312. #if defined(DEF_I2CBUS)
  313. int i, res;
  314. int rh, temp;
  315. uint8_t data[2], raw0[8], raw1[6];
  316. uint32_t rh_raw, temp_raw;
  317. if (i2cSht21.slave_bus == 0)
  318. {
  319. res = NutRegisterI2cSlave(&i2cSht21, bus);
  320. if (res)
  321. return -1;
  322. /* Read serial according to
  323. Sensirion_Humidity_SHT2x_Electronic_Identification_Code_V1.1.pdf*/
  324. /* First Memory access */
  325. data[0] = 0xfa;
  326. data[1] = 0x0f;
  327. res = NutI2cMasterTransceive(&i2cSht21, data, 2, raw0, 8);
  328. if (res < 0)
  329. return -1;
  330. /* Second Memory access */
  331. data[0] = 0xfc;
  332. data[1] = 0xc9;
  333. res = NutI2cMasterTransceive(&i2cSht21, data, 2, raw1, 6);
  334. if (res < 0)
  335. return -1;
  336. printf("Serial: %02x%02x%02x%02x%02x%02x%02x%02x,",
  337. raw1[3], raw1[4], raw0[0], raw0[2], raw0[4], raw0[6],
  338. raw1[0], raw1[1]);
  339. for (i = 0; i<8; i++)
  340. printf(" %02x", raw0[i]);
  341. for (i = 0; i<6; i++)
  342. printf(" %02x", raw1[i]);
  343. puts("\n");
  344. /* Default is highest resolution. no need to change*/
  345. }
  346. data[0] = 0xf5; /*Start RH measurement, no hold*/
  347. res = NutI2cMasterTransceive(&i2cSht21, data, 1, 0, 0);
  348. /* Poll for measurement complete*/
  349. do
  350. {
  351. NutSleep(1);
  352. res = NutI2cMasterTransceive(&i2cSht21, 0, 0, raw0, 3);
  353. } while(res < 0);
  354. if(res != 3)
  355. printf("Short Read: Read %d bytes vs 3 bytes requested\n", res);
  356. data[0] = 0xf3; /*Start Temp measurement, no hold*/
  357. res = NutI2cMasterTransceive(&i2cSht21, data, 1, 0, 0);
  358. do
  359. {
  360. NutSleep(5);
  361. res = NutI2cMasterTransceive(&i2cSht21, 0, 0, raw1, 3);
  362. } while(res < 0);
  363. if(res != 3)
  364. printf("Short Read: Read %d bytes vs 3 bytes requested\n", res);
  365. rh_raw = raw0[0]<<8|raw0[1];
  366. temp_raw = raw1[0]<<8|raw1[1];
  367. /* Calculation according Sensirion_Humidity_SHT21_Datasheet_V3.pdf
  368. * in units of 10**-3
  369. */
  370. rh = (rh_raw * (125000/2))/(0x10000/2) -6000;
  371. temp = (temp_raw * (175720/4))/(0x10000/4) -46850;
  372. printf("SHT21 : Temp %6d.%03d , Rh %2d.%03d%%\n",
  373. temp/1000, temp%1000, rh/1000, rh%1000);
  374. return 0;
  375. #else
  376. return -1;
  377. #endif
  378. }
  379. #if defined(DEV_I2CBUS_H)
  380. int ScanBus(NUTI2C_BUS *bus)
  381. {
  382. #if defined(DEF_I2CBUS)
  383. int res = 1, i;
  384. int sla =0;
  385. for (i=0; i<MAX_TEST_DEVICES-1; i++) {
  386. sla = NutI2cBusScan(bus, sla, 127);
  387. if (sla == I2C_SLA_NONE) {
  388. found_device[i] = 0;
  389. break;
  390. }
  391. found_device[i] = sla;
  392. printf("%3d (0x%02x) detected\n", sla, sla);
  393. sla++;
  394. }
  395. found_device[i] = 0;
  396. return res;
  397. #else
  398. return -1;
  399. #endif
  400. }
  401. #endif
  402. /*
  403. * I2C sample: Scan the I2C Bus and look for connected devices
  404. *
  405. * Some functions do not work with ICCAVR.
  406. */
  407. int main(void)
  408. {
  409. int res;
  410. uint32_t baud = 115200;
  411. res = NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  412. if (res )
  413. goto error;
  414. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  415. freopen(DEV_CONSOLE.dev_name, "r", stdin);
  416. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  417. puts(banner);
  418. LED1_INIT;
  419. LED2_START_THREAD;
  420. Hardware_Init();
  421. #if !defined(DEF_I2CBUS) || !defined(DEV_I2CBUS_H)
  422. puts("Please indicate the I2C Bus to scan!");
  423. goto error;
  424. #else
  425. res = NutI2cBusRate( &DEF_I2CBUS, 100000);
  426. if (res !=0)
  427. {
  428. puts("NutI2cBusRate failed\n");
  429. goto error;
  430. }
  431. else
  432. {
  433. printf("NutI2cBusRate success\n");
  434. }
  435. NutI2cBusTimeout(&DEF_I2CBUS, 10);
  436. ScanBus(&DEF_I2CBUS);
  437. for (;;) {
  438. int i;
  439. LED1_TOGGLE;
  440. for (i = 0; found_device[i]; i++)
  441. {
  442. switch(found_device[i])
  443. {
  444. #if defined(F3_DISCOVERY)
  445. case I2C_SLA_LSM303_ACCEL: /* ST LSM303 Accel on F3Discovery*/
  446. dump_lsm303_accel(&DEF_I2CBUS);
  447. break;
  448. case I2C_SLA_LSM303_MAGNET: /* ST LSM303 Magnet on F3Discovery*/
  449. dump_lsm303_magnet(&DEF_I2CBUS);
  450. break;
  451. #endif
  452. #if defined(F4_DISCOVERY)
  453. case I2C_SLA_CS43L22: /* Cirrus CS43L22 on F4Discovery, AD0 == 0*/
  454. dump_cs43l22(&DEF_I2CBUS);
  455. break;
  456. #endif
  457. #if defined(STM32_CAN)
  458. case I2C_SLA_MAX44009: /* MAX44009 on STM32_CAN AD0 == 0*/
  459. dump_max44009(&DEF_I2CBUS);
  460. break;
  461. case I2C_SLA_SHT21: /* SHT21 on STM32_CAN */
  462. dump_sht21(&DEF_I2CBUS);
  463. break;
  464. #endif
  465. }
  466. }
  467. puts("Press \"Enter\" to scan I2C bus for devices ");
  468. fgets(inbuf, sizeof(inbuf), stdin);
  469. }
  470. return 0;
  471. #endif
  472. error:
  473. while(1)
  474. {
  475. LED1_TOGGLE;
  476. NutSleep(100);
  477. }
  478. return 0;
  479. }