led.c 2.8 KB

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