led.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. while(1) {
  76. LPC_GPIO2->FIODIR |= BV(4) | BV(5);
  77. LPC_GPIO1->FIODIR |= BV(23);
  78. delay_ms(350);
  79. LPC_GPIO2->FIODIR &= ~(BV(4) | BV(5));
  80. LPC_GPIO1->FIODIR &= ~BV(23);
  81. delay_ms(350);
  82. cli_entrycheck();
  83. }
  84. }
  85. void led_pwm() {
  86. /* Rev.C P2.4, P2.5, P1.23 */
  87. BITBAND(LPC_PINCON->PINSEL4, 9) = 0;
  88. BITBAND(LPC_PINCON->PINSEL4, 8) = 1;
  89. BITBAND(LPC_PINCON->PINSEL4, 11) = 0;
  90. BITBAND(LPC_PINCON->PINSEL4, 10) = 1;
  91. BITBAND(LPC_PINCON->PINSEL3, 15) = 1;
  92. BITBAND(LPC_PINCON->PINSEL3, 14) = 0;
  93. BITBAND(LPC_PWM1->PCR, 12) = 1;
  94. BITBAND(LPC_PWM1->PCR, 13) = 1;
  95. BITBAND(LPC_PWM1->PCR, 14) = 1;
  96. led_pwmstate = 1;
  97. }
  98. void led_std() {
  99. BITBAND(LPC_PINCON->PINSEL4, 9) = 0;
  100. BITBAND(LPC_PINCON->PINSEL4, 8) = 0;
  101. BITBAND(LPC_PINCON->PINSEL4, 11) = 0;
  102. BITBAND(LPC_PINCON->PINSEL4, 10) = 0;
  103. BITBAND(LPC_PINCON->PINSEL3, 15) = 0;
  104. BITBAND(LPC_PINCON->PINSEL3, 14) = 0;
  105. BITBAND(LPC_PWM1->PCR, 12) = 0;
  106. BITBAND(LPC_PWM1->PCR, 13) = 0;
  107. BITBAND(LPC_PWM1->PCR, 14) = 0;
  108. led_pwmstate = 0;
  109. }
  110. void led_init() {
  111. /* power is already connected by default */
  112. /* set PCLK divider to 8 */
  113. BITBAND(LPC_SC->PCLKSEL1, 21) = 1;
  114. BITBAND(LPC_SC->PCLKSEL1, 20) = 1;
  115. LPC_PWM1->MR0 = 255;
  116. BITBAND(LPC_PWM1->LER, 0) = 1;
  117. BITBAND(LPC_PWM1->TCR, 0) = 1;
  118. BITBAND(LPC_PWM1->TCR, 3) = 1;
  119. BITBAND(LPC_PWM1->MCR, 1) = 1;
  120. }