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.

example1.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #ifdef COMPILE_EXAMPLE1_CODE_MODSERIAL
  2. /*
  3. * To run this test program, link p9 to p10 so the Serial loops
  4. * back and receives characters it sends.
  5. */
  6. #include "mbed.h"
  7. #include "MODSERIAL.h"
  8. DigitalOut led1(LED1);
  9. DigitalOut led2(LED2);
  10. DigitalOut led3(LED3);
  11. DigitalOut led4(LED4);
  12. MODSERIAL pc(USBTX, USBRX);
  13. /*
  14. * As experiement, you can define MODSERIAL as show here and see what
  15. * effects it has on the LEDs.
  16. *
  17. * MODSERIAL uart(TX_PIN, RX_PIN, 512);
  18. * With this, the 512 characters sent can straight into the buffer
  19. * vary quickly. This means LED1 is only on briefly as the TX buffer
  20. * fills.
  21. *
  22. * MODSERIAL uart(TX_PIN, RX_PIN, 32);
  23. * With this, the buffer is smaller than the default 256 bytes and
  24. * therefore LED1 stays on much longer while the system waits for
  25. * room in the TX buffer.
  26. */
  27. MODSERIAL uart(TX_PIN, RX_PIN);
  28. // This function is called when a character goes from the TX buffer
  29. // to the Uart THR FIFO register.
  30. void txCallback(MODSERIAL_IRQ_INFO *q) {
  31. led2 = !led2;
  32. }
  33. // This function is called when TX buffer goes empty
  34. void txEmpty(MODSERIAL_IRQ_INFO *q) {
  35. led2 = 0;
  36. pc.puts(" Done. ");
  37. }
  38. // This function is called when a character goes into the RX buffer.
  39. void rxCallback(MODSERIAL_IRQ_INFO *q) {
  40. led3 = !led3;
  41. pc.putc(uart.getc());
  42. }
  43. int main() {
  44. int c = 'A';
  45. // Ensure the baud rate for the PC "USB" serial is much
  46. // higher than "uart" baud rate below.
  47. pc.baud(PC_BAUD);
  48. // Use a deliberatly slow baud to fill up the TX buffer
  49. uart.baud(1200);
  50. uart.attach(&txCallback, MODSERIAL::TxIrq);
  51. uart.attach(&rxCallback, MODSERIAL::RxIrq);
  52. uart.attach(&txEmpty, MODSERIAL::TxEmpty);
  53. // Loop sending characters. We send 512
  54. // which is twice the default TX/RX buffer size.
  55. led1 = 1; // Show start of sending with LED1.
  56. for (int loop = 0; loop < 512; loop++) {
  57. uart.printf("%c", c);
  58. c++;
  59. if (c > 'Z') c = 'A';
  60. }
  61. led1 = 0; // Show the end of sending by switching off LED1.
  62. // End program. Flash LED4. Notice how LED 2 and 3 continue
  63. // to flash for a short period while the interrupt system
  64. // continues to send the characters left in the TX buffer.
  65. while(1) {
  66. led4 = !led4;
  67. wait(0.25);
  68. }
  69. }
  70. /*
  71. * Notes. Here is the sort of output you can expect on your PC/Mac/Linux host
  72. * machine that is connected to the "pc" USB serial port.
  73. *
  74. * ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUV
  75. * WXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQR
  76. * STUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMN
  77. * OPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ
  78. * KLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEF
  79. * GHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZAB
  80. * CDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQ Done. R
  81. *
  82. * Of interest is that last "R" character after the system has said "Done."
  83. * This comes from the fact that the TxEmpty callback is made when the TX buffer
  84. * becomes empty. MODSERIAL makes use of the fact that the Uarts built into the
  85. * LPC17xx device use a 16 byte FIFO on both RX and TX channels. This means that
  86. * when the TxEmpty callback is made, the TX buffer is empty, but that just means
  87. * the "last few characters" were written to the TX FIFO. So although the TX
  88. * buffer has gone empty, the Uart's transmit system is still sending any remaining
  89. * characters from it's TX FIFO. If you want to be truely sure all the characters
  90. * you have sent have left the Mbed then call txIsBusy(); This function will
  91. * return true if characters are still being sent. If it returns false after
  92. * the Tx buffer is empty then all your characters have been sent.
  93. *
  94. * In a similar way, when characters are received into the RX FIFO, the entire
  95. * FIFO contents is moved to the RX buffer, assuming there is room left in the
  96. * RX buffer. If there is not, any remaining characters are left in the RX FIFO
  97. * and will be moved to the RX buffer on the next interrupt or when the running
  98. * program removes a character(s) from the RX buffer with the getc() method.
  99. */
  100. #endif