led.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. void rt_hw_led_init(void)
  2. {
  3. return;
  4. }
  5. #include <stdint.h>
  6. #include "platform.h"
  7. void rt_hw_led_on(int led)
  8. {
  9. /*
  10. // Make sure the HFROSC is on before the next line:
  11. PRCI_REG(PRCI_HFROSCCFG) |= ROSC_EN(1);
  12. // Run off 16 MHz Crystal for accuracy. Note that the
  13. // first line is
  14. PRCI_REG(PRCI_PLLCFG) = (PLL_REFSEL(1) | PLL_BYPASS(1));
  15. PRCI_REG(PRCI_PLLCFG) |= (PLL_SEL(1));
  16. // Turn off HFROSC to save power
  17. PRCI_REG(PRCI_HFROSCCFG) &= ~(ROSC_EN(1));
  18. */
  19. // Wait a bit to avoid corruption on the UART.
  20. // (In some cases, switching to the IOF can lead
  21. // to output glitches, so need to let the UART
  22. // reciever time out and resynchronize to the real
  23. // start of the stream.
  24. volatile int i=0;
  25. while(i < 10000){i++;}
  26. uint16_t r=0xFF;
  27. uint16_t g=0;
  28. uint16_t b=0;
  29. char c = 0;
  30. // Set up RGB PWM
  31. /*
  32. PWM1_REG(PWM_CFG) = 0;
  33. // To balance the power consumption, make one left, one right, and one center aligned.
  34. PWM1_REG(PWM_CFG) = (PWM_CFG_ENALWAYS) | (PWM_CFG_CMP2CENTER);
  35. PWM1_REG(PWM_COUNT) = 0;
  36. // Period is approximately 244 Hz
  37. // the LEDs are intentionally left somewhat dim,
  38. // as the full brightness can be painful to look at.
  39. PWM1_REG(PWM_CMP0) = 0;
  40. */
  41. if(r > 0 && b == 0){
  42. r--;
  43. g++;
  44. }
  45. if(g > 0 && r == 0){
  46. g--;
  47. b++;
  48. }
  49. if(b > 0 && g == 0){
  50. r++;
  51. b--;
  52. }
  53. uint32_t G = g;
  54. uint32_t R = r;
  55. uint32_t B = b;
  56. PWM1_REG(PWM_CMP1) = G << 4; // PWM is low on the left, GPIO is low on the left side, LED is ON on the left.
  57. PWM1_REG(PWM_CMP2) = (B << 1) << 4; // PWM is high on the middle, GPIO is low in the middle, LED is ON in the middle.
  58. PWM1_REG(PWM_CMP3) = 0xFFFF - (R << 4); // PWM is low on the left, GPIO is low on the right, LED is on on the right.
  59. return;
  60. }
  61. void rt_hw_led_off(int led)
  62. {
  63. return;
  64. }