status_led.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * (C) Copyright 2000-2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <status_led.h>
  9. /*
  10. * The purpose of this code is to signal the operational status of a
  11. * target which usually boots over the network; while running in
  12. * U-Boot, a status LED is blinking. As soon as a valid BOOTP reply
  13. * message has been received, the LED is turned off. The Linux
  14. * kernel, once it is running, will start blinking the LED again,
  15. * with another frequency.
  16. */
  17. /* ------------------------------------------------------------------------- */
  18. typedef struct {
  19. led_id_t mask;
  20. int state;
  21. int period;
  22. int cnt;
  23. } led_dev_t;
  24. led_dev_t led_dev[] = {
  25. { STATUS_LED_BIT,
  26. STATUS_LED_STATE,
  27. STATUS_LED_PERIOD,
  28. 0,
  29. },
  30. #if defined(STATUS_LED_BIT1)
  31. { STATUS_LED_BIT1,
  32. STATUS_LED_STATE1,
  33. STATUS_LED_PERIOD1,
  34. 0,
  35. },
  36. #endif
  37. #if defined(STATUS_LED_BIT2)
  38. { STATUS_LED_BIT2,
  39. STATUS_LED_STATE2,
  40. STATUS_LED_PERIOD2,
  41. 0,
  42. },
  43. #endif
  44. #if defined(STATUS_LED_BIT3)
  45. { STATUS_LED_BIT3,
  46. STATUS_LED_STATE3,
  47. STATUS_LED_PERIOD3,
  48. 0,
  49. },
  50. #endif
  51. #if defined(STATUS_LED_BIT4)
  52. { STATUS_LED_BIT4,
  53. STATUS_LED_STATE4,
  54. STATUS_LED_PERIOD4,
  55. 0,
  56. },
  57. #endif
  58. #if defined(STATUS_LED_BIT5)
  59. { STATUS_LED_BIT5,
  60. STATUS_LED_STATE5,
  61. STATUS_LED_PERIOD5,
  62. 0,
  63. },
  64. #endif
  65. };
  66. #define MAX_LED_DEV (sizeof(led_dev)/sizeof(led_dev_t))
  67. static int status_led_init_done = 0;
  68. void status_led_init(void)
  69. {
  70. led_dev_t *ld;
  71. int i;
  72. for (i = 0, ld = led_dev; i < MAX_LED_DEV; i++, ld++)
  73. __led_init (ld->mask, ld->state);
  74. status_led_init_done = 1;
  75. }
  76. void status_led_tick (ulong timestamp)
  77. {
  78. led_dev_t *ld;
  79. int i;
  80. if (!status_led_init_done)
  81. status_led_init ();
  82. for (i = 0, ld = led_dev; i < MAX_LED_DEV; i++, ld++) {
  83. if (ld->state != STATUS_LED_BLINKING)
  84. continue;
  85. if (++ld->cnt >= ld->period) {
  86. __led_toggle (ld->mask);
  87. ld->cnt -= ld->period;
  88. }
  89. }
  90. }
  91. void status_led_set (int led, int state)
  92. {
  93. led_dev_t *ld;
  94. if (led < 0 || led >= MAX_LED_DEV)
  95. return;
  96. if (!status_led_init_done)
  97. status_led_init ();
  98. ld = &led_dev[led];
  99. ld->state = state;
  100. if (state == STATUS_LED_BLINKING) {
  101. ld->cnt = 0; /* always start with full period */
  102. state = STATUS_LED_ON; /* always start with LED _ON_ */
  103. }
  104. __led_set (ld->mask, state);
  105. }