2 * U-boot - cmd_bf537led.c
4 * Copyright (C) 2006 Aaron Gage, Ocean Optics Inc.
6 * See file CREDITS for list of people who contributed to this
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.
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.
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,
27 #include <asm/blackfin.h>
28 #include <asm-blackfin/string.h>
29 #ifdef CONFIG_BF537_STAMP_LEDCMD
31 /* Define the command usage in a reusable way */
33 "led <number> <action>\n" \
34 " <number> - Index (0-5) of LED to change, or \"all\"\n" \
35 " <action> - Must be one of:\n" \
38 /* Number of LEDs supported by the board */
40 /* The BF537 stamp has 6 LEDs. This mask indicates that all should be lit. */
41 #define LED_ALL_MASK 0x003F
43 void show_cmd_usage(void);
44 void set_led_state(int index, int state);
45 void configure_GPIO_to_output(int index);
47 /* Map of LEDs according to their GPIO ports. This can be rearranged or
48 * otherwise changed to account for different GPIO configurations.
50 int led_ports[] = { PF6, PF7, PF8, PF9, PF10, PF11 };
52 #define ACTION_TOGGLE -1
56 #define LED_STATE_OFF 0
57 #define LED_STATE_ON 1
59 /* This is a trivial atoi implementation since we don't have one available */
60 int atoi(char *string)
67 length = strlen(string);
68 for (i = 0; i < length; i++) {
69 if (0 == i && string[0] == '-') {
73 if (string[i] > '9' || string[i] < '0') {
77 retval += string[i] - '0';
83 int do_bf537led(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
86 int led_current_state = 0;
87 int action = ACTION_OFF;
91 /* Not enough arguments, so just show usage information */
96 if (strcmp(argv[1], "all") == 0) {
97 led_mask = LED_ALL_MASK;
100 if (temp < 0 || temp >= NUMBER_LEDS) {
101 printf("Invalid LED number [%s]\n", argv[1]);
105 led_mask |= (1 << temp);
108 if (strcmp(argv[2], "off") == 0) {
110 } else if (strcmp(argv[2], "on") == 0) {
112 } else if (strcmp(argv[2], "toggle") == 0) {
113 action = ACTION_TOGGLE;
115 printf("Invalid action [%s]\n", argv[2]);
120 for (temp = 0; temp < NUMBER_LEDS; temp++) {
121 if ((led_mask & (1 << temp)) > 0) {
123 * It is possible that the user has wired one of PF6-PF11 to
124 * something other than an LED, so this will only change a pin
125 * to output if the user has indicated a state change. This may
126 * happen a lot, but this way is safer than just setting all pins
129 configure_GPIO_to_output(temp);
132 ((*pPORTFIO & led_ports[temp]) >
133 0) ? LED_STATE_ON : LED_STATE_OFF;
135 printf("LED state for index %d (%x) is %d\n", temp, led_ports[temp],
137 printf("*pPORTFIO is %x\n", *pPORTFIO);
139 if (ACTION_ON == action
140 || (ACTION_TOGGLE == action
141 && 0 == led_current_state)) {
142 printf("Turning LED %d on\n", temp);
143 set_led_state(temp, LED_STATE_ON);
145 printf("Turning LED %d off\n", temp);
146 set_led_state(temp, LED_STATE_OFF);
155 * The GPIO pins that go to the LEDs on the BF537 stamp must be configured
156 * as output. This function simply configures them that way. This could
157 * be done to all of the GPIO lines at once, but if a user is using a
158 * custom board, this will try to be nice and only change the GPIO lines
159 * that the user specifically names.
161 void configure_GPIO_to_output(int index)
165 port = led_ports[index];
167 /* Clear the Port F Function Enable Register */
168 *pPORTF_FER &= ~port;
169 /* Set the Port F I/O direction register */
170 *pPORTFIO_DIR |= port;
171 /* Clear the Port F I/O Input Enable Register */
172 *pPORTFIO_INEN &= ~port;
175 /* Enforce the given state on the GPIO line for the indicated LED */
176 void set_led_state(int index, int state)
180 port = led_ports[index];
182 if (LED_STATE_OFF == state) {
183 /* Clear the bit to turn off the LED */
186 /* Set the bit to turn on the LED */
191 /* Display usage information */
192 void show_cmd_usage()
194 printf("Usage:\n%s", USAGE_LONG);
197 /* Register information for u-boot to find this command */
198 U_BOOT_CMD(led, 3, 1, do_bf537led,
199 "led- Control BF537 stamp LEDs\n", USAGE_LONG);