Naze32 clone with Frysky receiver
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Combined-PPM signal generator
  3. *
  4. * The whole CPPM pulse should be 20ms long.
  5. * It contains 8 channels with 2ms each, followed
  6. * by 4ms of silence. One channel pulse varies between
  7. * 1 and 2 ms. They are seperated with a very short low
  8. * pulse of about 0.1ms.
  9. *
  10. * 20.000us
  11. * - 8 * 2.000us = 16.000us
  12. * - 9 * 100us = 900us
  13. * = 3.100us
  14. *
  15. * 1 2 3 4 5 6 7 8
  16. * ___ ___ ___ ___ ___ ___ ___ ___ __________
  17. * | | | | | | | | | | | | | | | | | | |
  18. * | | | | | | | | | | | | | | | | | | |
  19. * | | | | | | | | | | | | | | | | | | |
  20. * |_| |_| |_| |_| |_| |_| |_| |_| |_| |
  21. *
  22. * States:
  23. * 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
  24. */
  25. #include <avr/io.h>
  26. #include <avr/interrupt.h>
  27. #include "cppm.h"
  28. #define MAX_STATES 17
  29. #define CHANNELS 8
  30. #define WHOLE_PULSE_WIDTH 20000
  31. #define PULSE_WIDTH 2000
  32. #define MAX_PULSE_WIDTH (CHANNELS * PULSE_WIDTH) // 16.000
  33. #define PULSE_LOW 100
  34. #define PULSE_LOW_SUM ((CHANNELS + 1) * PULSE_LOW) // 900
  35. #define MIN_WAIT (WHOLE_PULSE_WIDTH - MAX_PULSE_WIDTH - PULSE_LOW_SUM) // 3100
  36. #define TIME_AFTER_OVERFLOW 128
  37. #define TIME_MULTIPLIER 2
  38. volatile uint16_t cppmData[CHANNELS] = { 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500 };
  39. volatile uint16_t delaySum = 0;
  40. volatile uint8_t state = 0;
  41. volatile uint16_t triggerTimeRemaining = 0;
  42. #define NONE 0
  43. #define COMPARE_MATCH 1
  44. #define OVERFLOW 2
  45. volatile uint8_t triggerState = NONE;
  46. static void triggerIn(uint16_t us);
  47. static void nextState(void);
  48. void cppmInit(void) {
  49. // Set pin to output mode
  50. CPPM_DDR |= (1 << CPPM_PIN);
  51. // Start with a low pulse
  52. CPPM_PORT &= ~(1 << CPPM_PIN);
  53. TCCR0B |= (1 << CS01); // Prescaler: 8
  54. #ifdef DEBUG
  55. TIMSK0 |= (1 << TOIE0) | (1 << OCIE0A); // Enable Overflow and Compare Match Interrupt
  56. #else
  57. TIMSK |= (1 << TOIE0) | (1 << OCIE0A); // Enable Overflow and Compare Match Interrupt
  58. #endif
  59. OCR0A = 0;
  60. state = 0;
  61. delaySum = MIN_WAIT;
  62. triggerIn(PULSE_LOW);
  63. }
  64. void cppmCopy(uint16_t *data) {
  65. cli();
  66. for (int i = 0; i < CHANNELS; i++) {
  67. cppmData[i] = data[i];
  68. }
  69. sei();
  70. }
  71. static void triggerIn(uint16_t us) {
  72. TCNT0 = 0; // Reset Timer
  73. if (us <= (TIME_AFTER_OVERFLOW - 1)) {
  74. triggerState = COMPARE_MATCH;
  75. OCR0A = us * TIME_MULTIPLIER;
  76. } else {
  77. triggerState = OVERFLOW;
  78. triggerTimeRemaining = us - TIME_AFTER_OVERFLOW;
  79. }
  80. }
  81. static void nextState(void) {
  82. state++;
  83. if (state > MAX_STATES) {
  84. state = 0;
  85. delaySum = MIN_WAIT;
  86. }
  87. if ((state % 2) == 0) {
  88. // pulse pause
  89. CPPM_PORT &= ~(1 << CPPM_PIN);
  90. triggerIn(PULSE_LOW);
  91. } else {
  92. CPPM_PORT |= (1 << CPPM_PIN);
  93. if (state <= 15) {
  94. // normal ppm pulse
  95. uint8_t index = state / 2;
  96. triggerIn(cppmData[index]);
  97. delaySum += PULSE_WIDTH - cppmData[index];
  98. } else {
  99. // sync pulse
  100. triggerIn(delaySum);
  101. }
  102. }
  103. }
  104. #ifdef DEBUG
  105. ISR(TIMER0_OVF_vect) {
  106. #else
  107. ISR(TIM0_OVF_vect) {
  108. #endif
  109. if (triggerState == OVERFLOW) {
  110. if (triggerTimeRemaining == 0) {
  111. triggerState = NONE;
  112. nextState();
  113. } else {
  114. triggerIn(triggerTimeRemaining);
  115. }
  116. }
  117. }
  118. #ifdef DEBUG
  119. ISR(TIMER0_COMPA_vect) {
  120. #else
  121. ISR(TIM0_COMPA_vect) {
  122. #endif
  123. if (triggerState == COMPARE_MATCH) {
  124. triggerState = NONE;
  125. nextState();
  126. }
  127. }