led.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* ___DISCLAIMER___ */
  2. #include <arm/NXP/LPC17xx/LPC17xx.h>
  3. #include "bits.h"
  4. #include "timer.h"
  5. #include "led.h"
  6. #include "cli.h"
  7. static uint8_t led_bright[16]={255,253,252,251,249,247,244,239,232,223,210,191,165,127,74,0};
  8. int led_rdyledstate = 0;
  9. int led_readledstate = 0;
  10. int led_writeledstate = 0;
  11. int led_pwmstate = 0;
  12. /* LED connections (Rev.C)
  13. LED color IO PWM
  14. ---------------------------
  15. ready green P2.4 PWM1[5]
  16. read yellow P2.5 PWM1[6]
  17. write red P1.23 PWM1[4]
  18. */
  19. void rdyled(unsigned int state) {
  20. if(led_pwmstate) {
  21. rdybright(state?15:0);
  22. } else {
  23. BITBAND(LPC_GPIO2->FIODIR, 4) = state;
  24. }
  25. led_rdyledstate = state;
  26. }
  27. void readled(unsigned int state) {
  28. if(led_pwmstate) {
  29. readbright(state?15:0);
  30. } else {
  31. BITBAND(LPC_GPIO2->FIODIR, 5) = state;
  32. }
  33. led_readledstate = state;
  34. }
  35. void writeled(unsigned int state) {
  36. if(led_pwmstate) {
  37. writebright(state?15:0);
  38. } else {
  39. BITBAND(LPC_GPIO1->FIODIR, 23) = state;
  40. }
  41. led_writeledstate = state;
  42. }
  43. void rdybright(uint8_t bright) {
  44. LPC_PWM1->MR5 = led_bright[(bright & 15)];
  45. BITBAND(LPC_PWM1->LER, 5) = 1;
  46. }
  47. void readbright(uint8_t bright) {
  48. LPC_PWM1->MR6 = led_bright[(bright & 15)];
  49. BITBAND(LPC_PWM1->LER, 6) = 1;
  50. }
  51. void writebright(uint8_t bright) {
  52. LPC_PWM1->MR4 = led_bright[(bright & 15)];
  53. BITBAND(LPC_PWM1->LER, 4) = 1;
  54. }
  55. void led_clkout32(uint32_t val) {
  56. while(1) {
  57. rdyled(1);
  58. delay_ms(400);
  59. readled((val & BV(31))>>31);
  60. rdyled(0);
  61. val<<=1;
  62. delay_ms(400);
  63. }
  64. }
  65. void toggle_rdy_led() {
  66. rdyled(~led_rdyledstate);
  67. }
  68. void toggle_read_led() {
  69. readled(~led_readledstate);
  70. }
  71. void toggle_write_led() {
  72. writeled(~led_writeledstate);
  73. }
  74. void led_panic() {
  75. led_std();
  76. while(1) {
  77. rdyled(1);
  78. readled(1);
  79. writeled(1);
  80. delay_ms(100);
  81. rdyled(0);
  82. readled(0);
  83. writeled(0);
  84. delay_ms(100);
  85. cli_entrycheck();
  86. }
  87. }
  88. void led_pwm() {
  89. /* Rev.C P2.4, P2.5, P1.23 */
  90. BITBAND(LPC_PINCON->PINSEL4, 9) = 0;
  91. BITBAND(LPC_PINCON->PINSEL4, 8) = 1;
  92. BITBAND(LPC_PINCON->PINSEL4, 11) = 0;
  93. BITBAND(LPC_PINCON->PINSEL4, 10) = 1;
  94. BITBAND(LPC_PINCON->PINSEL3, 15) = 1;
  95. BITBAND(LPC_PINCON->PINSEL3, 14) = 0;
  96. BITBAND(LPC_PWM1->PCR, 12) = 1;
  97. BITBAND(LPC_PWM1->PCR, 13) = 1;
  98. BITBAND(LPC_PWM1->PCR, 14) = 1;
  99. led_pwmstate = 1;
  100. }
  101. void led_std() {
  102. BITBAND(LPC_PINCON->PINSEL4, 9) = 0;
  103. BITBAND(LPC_PINCON->PINSEL4, 8) = 0;
  104. BITBAND(LPC_PINCON->PINSEL4, 11) = 0;
  105. BITBAND(LPC_PINCON->PINSEL4, 10) = 0;
  106. BITBAND(LPC_PINCON->PINSEL3, 15) = 0;
  107. BITBAND(LPC_PINCON->PINSEL3, 14) = 0;
  108. BITBAND(LPC_PWM1->PCR, 12) = 0;
  109. BITBAND(LPC_PWM1->PCR, 13) = 0;
  110. BITBAND(LPC_PWM1->PCR, 14) = 0;
  111. led_pwmstate = 0;
  112. }
  113. void led_init() {
  114. /* power is already connected by default */
  115. /* set PCLK divider to 8 */
  116. BITBAND(LPC_SC->PCLKSEL0, 13) = 1;
  117. BITBAND(LPC_SC->PCLKSEL0, 12) = 1;
  118. LPC_PWM1->MR0 = 255;
  119. BITBAND(LPC_PWM1->LER, 0) = 1;
  120. BITBAND(LPC_PWM1->TCR, 0) = 1;
  121. BITBAND(LPC_PWM1->TCR, 3) = 1;
  122. BITBAND(LPC_PWM1->MCR, 1) = 1;
  123. }