cmd_bf537led.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * U-boot - cmd_bf537led.c
  3. *
  4. * Copyright (C) 2006 Aaron Gage, Ocean Optics Inc.
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <config.h>
  26. #include <command.h>
  27. #include <asm/blackfin.h>
  28. #include <asm-blackfin/string.h>
  29. #ifdef CONFIG_BF537_STAMP_LEDCMD
  30. /* Define the command usage in a reusable way */
  31. #define USAGE_LONG \
  32. "led <number> <action>\n" \
  33. " <number> - Index (0-5) of LED to change, or \"all\"\n" \
  34. " <action> - Must be one of:\n" \
  35. " on off toggle\n"
  36. /* Number of LEDs supported by the board */
  37. #define NUMBER_LEDS 6
  38. /* The BF537 stamp has 6 LEDs. This mask indicates that all should be lit. */
  39. #define LED_ALL_MASK 0x003F
  40. void show_cmd_usage(void);
  41. void set_led_state(int index, int state);
  42. void configure_GPIO_to_output(int index);
  43. /* Map of LEDs according to their GPIO ports. This can be rearranged or
  44. * otherwise changed to account for different GPIO configurations.
  45. */
  46. int led_ports[] = { PF6, PF7, PF8, PF9, PF10, PF11 };
  47. #define ACTION_TOGGLE -1
  48. #define ACTION_OFF 0
  49. #define ACTION_ON 1
  50. #define LED_STATE_OFF 0
  51. #define LED_STATE_ON 1
  52. /* This is a trivial atoi implementation since we don't have one available */
  53. int atoi(char *string)
  54. {
  55. int length;
  56. int retval = 0;
  57. int i;
  58. int sign = 1;
  59. length = strlen(string);
  60. for (i = 0; i < length; i++) {
  61. if (0 == i && string[0] == '-') {
  62. sign = -1;
  63. continue;
  64. }
  65. if (string[i] > '9' || string[i] < '0') {
  66. break;
  67. }
  68. retval *= 10;
  69. retval += string[i] - '0';
  70. }
  71. retval *= sign;
  72. return retval;
  73. }
  74. int do_bf537led(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  75. {
  76. int led_mask = 0;
  77. int led_current_state = 0;
  78. int action = ACTION_OFF;
  79. int temp;
  80. if (3 != argc) {
  81. /* Not enough arguments, so just show usage information */
  82. show_cmd_usage();
  83. return 1;
  84. }
  85. if (strcmp(argv[1], "all") == 0) {
  86. led_mask = LED_ALL_MASK;
  87. } else {
  88. temp = atoi(argv[1]);
  89. if (temp < 0 || temp >= NUMBER_LEDS) {
  90. printf("Invalid LED number [%s]\n", argv[1]);
  91. show_cmd_usage();
  92. return 2;
  93. }
  94. led_mask |= (1 << temp);
  95. }
  96. if (strcmp(argv[2], "off") == 0) {
  97. action = ACTION_OFF;
  98. } else if (strcmp(argv[2], "on") == 0) {
  99. action = ACTION_ON;
  100. } else if (strcmp(argv[2], "toggle") == 0) {
  101. action = ACTION_TOGGLE;
  102. } else {
  103. printf("Invalid action [%s]\n", argv[2]);
  104. show_cmd_usage();
  105. return 3;
  106. }
  107. for (temp = 0; temp < NUMBER_LEDS; temp++) {
  108. if ((led_mask & (1 << temp)) > 0) {
  109. /*
  110. * It is possible that the user has wired one of PF6-PF11 to
  111. * something other than an LED, so this will only change a pin
  112. * to output if the user has indicated a state change. This may
  113. * happen a lot, but this way is safer than just setting all pins
  114. * to output.
  115. */
  116. configure_GPIO_to_output(temp);
  117. led_current_state =
  118. ((*pPORTFIO & led_ports[temp]) >
  119. 0) ? LED_STATE_ON : LED_STATE_OFF;
  120. /*
  121. printf("LED state for index %d (%x) is %d\n", temp, led_ports[temp],
  122. led_current_state);
  123. printf("*pPORTFIO is %x\n", *pPORTFIO);
  124. */
  125. if (ACTION_ON == action
  126. || (ACTION_TOGGLE == action
  127. && 0 == led_current_state)) {
  128. printf("Turning LED %d on\n", temp);
  129. set_led_state(temp, LED_STATE_ON);
  130. } else {
  131. printf("Turning LED %d off\n", temp);
  132. set_led_state(temp, LED_STATE_OFF);
  133. }
  134. }
  135. }
  136. return 0;
  137. }
  138. /*
  139. * The GPIO pins that go to the LEDs on the BF537 stamp must be configured
  140. * as output. This function simply configures them that way. This could
  141. * be done to all of the GPIO lines at once, but if a user is using a
  142. * custom board, this will try to be nice and only change the GPIO lines
  143. * that the user specifically names.
  144. */
  145. void configure_GPIO_to_output(int index)
  146. {
  147. int port;
  148. port = led_ports[index];
  149. /* Clear the Port F Function Enable Register */
  150. *pPORTF_FER &= ~port;
  151. /* Set the Port F I/O direction register */
  152. *pPORTFIO_DIR |= port;
  153. /* Clear the Port F I/O Input Enable Register */
  154. *pPORTFIO_INEN &= ~port;
  155. }
  156. /* Enforce the given state on the GPIO line for the indicated LED */
  157. void set_led_state(int index, int state)
  158. {
  159. int port;
  160. port = led_ports[index];
  161. if (LED_STATE_OFF == state) {
  162. /* Clear the bit to turn off the LED */
  163. *pPORTFIO &= ~port;
  164. } else {
  165. /* Set the bit to turn on the LED */
  166. *pPORTFIO |= port;
  167. }
  168. }
  169. /* Display usage information */
  170. void show_cmd_usage()
  171. {
  172. printf("Usage:\n%s", USAGE_LONG);
  173. }
  174. /* Register information for u-boot to find this command */
  175. U_BOOT_CMD(led, 3, 1, do_bf537led,
  176. "led- Control BF537 stamp LEDs\n", USAGE_LONG);
  177. #endif