twitest.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*!
  2. * Copyright (C) 2011-2012 Uwe Bonnes
  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 <string.h>
  35. #include <stdio.h>
  36. #include <io.h>
  37. #include <dev/board.h>
  38. #if defined(DEF_TWIBUS)
  39. #include <dev/twif.h>
  40. #endif
  41. #include <sys/timer.h>
  42. #include <sys/thread.h>
  43. #include <sys/event.h>
  44. #include <dev/gpio.h>
  45. static const char *banner = "\nNut/OS TW Sample " __DATE__ " " __TIME__;
  46. #if defined(DEF_TWIBUS)
  47. static char inbuf[128];
  48. #endif
  49. #if defined(LED1_PORT) && defined( LED1_PIN)
  50. #define LED1_INIT GpioPinConfigSet( LED1_PORT, LED1_PIN, GPIO_CFG_OUTPUT)
  51. #define LED1_TOGGLE GpioPinSet(LED1_PORT, LED1_PIN, \
  52. !(GpioPinGet(LED1_PORT, LED1_PIN)))
  53. #else
  54. #define LED1_INIT
  55. #define LED1_TOGGLE
  56. #endif
  57. #if defined(LED2_PORT) && defined( LED2_PIN)
  58. #define LED2_START_THREAD \
  59. if (NutThreadCreate("t2", LedBlink, 0, 512)== 0)\
  60. printf("Can't create LED thread\n"); \
  61. else \
  62. printf("LED thread started\n");
  63. THREAD(LedBlink, arg)
  64. {
  65. GpioPinConfigSet( LED2_PORT, LED2_PIN, GPIO_CFG_OUTPUT);
  66. for(;;)
  67. {
  68. NutSleep(100);
  69. GpioPinSetHigh(LED2_PORT, LED2_PIN);
  70. NutSleep(100);
  71. GpioPinSetLow(LED2_PORT, LED2_PIN);
  72. }
  73. }
  74. #else
  75. #define LED2_START_THREAD
  76. #endif
  77. void Hardware_Init(void)
  78. {
  79. #if defined(F4_DISCOVERY)
  80. /* Set all CS43L22 I2C relates pins to a safe state*/
  81. GpioPinConfigSet(NUTGPIO_PORTE, 3, GPIO_CFG_OUTPUT);
  82. GpioPinSetHigh(NUTGPIO_PORTE, 3);
  83. GpioPinConfigSet(NUTGPIO_PORTC, 7, GPIO_CFG_OUTPUT);
  84. GpioPinSetHigh(NUTGPIO_PORTC, 7);
  85. GpioPinConfigSet(NUTGPIO_PORTC, 10, GPIO_CFG_OUTPUT);
  86. GpioPinSetHigh(NUTGPIO_PORTC, 10);
  87. GpioPinConfigSet(NUTGPIO_PORTC, 12, GPIO_CFG_OUTPUT);
  88. GpioPinSetHigh(NUTGPIO_PORTC, 12);
  89. GpioPinConfigSet(NUTGPIO_PORTC, 14, GPIO_CFG_OUTPUT);
  90. GpioPinSetHigh(NUTGPIO_PORTC, 14);
  91. GpioPinConfigSet(AUDIO_RST_PORT, AUDIO_RST_PIN, GPIO_CFG_OUTPUT);
  92. GpioPinSetLow(AUDIO_RST_PORT, AUDIO_RST_PIN);
  93. NutSleep(11);
  94. GpioPinSetHigh(AUDIO_RST_PORT, AUDIO_RST_PIN);
  95. NutSleep(10);
  96. #endif
  97. }
  98. int ScanBus(uint32_t tmo)
  99. {
  100. int res = 1;
  101. #if defined(DEF_TWIBUS)
  102. uint8_t a;
  103. /* Don't do a general call (a == 0) */
  104. for(a=1; a <80; a++)
  105. {
  106. if ((NutTwiMasterTranceive(&DEF_TWIBUS, a, NULL, 0, NULL, 0, tmo))
  107. != -1)
  108. {
  109. printf("Found I2C device at 0x%02x\n", a);
  110. res = 0;
  111. }
  112. }
  113. #endif
  114. return res;
  115. }
  116. /*
  117. * TWI sample: Scan the TWI Bus and look for connected devices
  118. *
  119. * Some functions do not work with ICCAVR.
  120. */
  121. int main(void)
  122. {
  123. int res;
  124. uint32_t baud = 115200;
  125. #if defined(DEF_TWIBUS)
  126. uint32_t tmo;
  127. #endif
  128. res = NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  129. if (res )
  130. goto error;
  131. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  132. freopen(DEV_CONSOLE.dev_name, "r", stdin);
  133. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  134. puts(banner);
  135. LED1_INIT;
  136. LED2_START_THREAD;
  137. Hardware_Init();
  138. #if !defined(DEF_TWIBUS)
  139. puts("Please indicate the TWI Bus to scan!");
  140. goto error;
  141. #else
  142. res = NutRegisterTwiBus( &DEF_TWIBUS, 0);
  143. if (res !=0)
  144. {
  145. printf("NutRegisterTwiBus failed\n");
  146. goto error;
  147. }
  148. else
  149. {
  150. printf("NutRegisterTwiBus success\n");
  151. }
  152. baud = 100000;
  153. res = NutTwiIOCtl(&DEF_TWIBUS, TWI_SETSPEED, &baud);
  154. if (res !=0)
  155. {
  156. printf("Can't set speed TWI\n");
  157. }
  158. else
  159. {
  160. NutTwiIOCtl(&DEF_TWIBUS, TWI_GETSPEED, &baud);
  161. printf("TWI speed is %ld. ", baud );
  162. /* Waiting for bus free often takes 1 ms */
  163. tmo = 20000/baud + 2;
  164. printf("Using %ld ms as Timeout\n", tmo);
  165. }
  166. ScanBus(tmo);
  167. for (;;) {
  168. LED1_TOGGLE;
  169. puts("Press \"Enter\" to scan I2C bus for devices ");
  170. fgets(inbuf, sizeof(inbuf), stdin);
  171. ScanBus(tmo);
  172. }
  173. return 0;
  174. #endif
  175. error:
  176. while(1)
  177. {
  178. LED1_TOGGLE;
  179. NutSleep(100);
  180. }
  181. return 0;
  182. }