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.

example_dma.cpp 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #ifdef COMPILE_EXAMPLE_CODE_MODSERIAL_MODDMA
  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. /* Note, this example requires that you also import into the Mbed
  8. compiler the MODDMA project as well as MODSERIAL
  9. http://mbed.org/users/AjK/libraries/MODDMA/latest
  10. MODDMA.h MUST come before MODSERIAL.h */
  11. #include "MODDMA.h" // <--- Declare first
  12. #include "MODSERIAL.h" // Flollowed by MODSERIAL
  13. DigitalOut led1(LED1);
  14. DigitalOut led2(LED2);
  15. DigitalOut led3(LED3);
  16. DigitalOut led4(LED4);
  17. MODSERIAL pc(USBTX, USBRX);
  18. /*
  19. * As experiement, you can define MODSERIAL as show here and see what
  20. * effects it has on the LEDs.
  21. *
  22. * MODSERIAL uart(TX_PIN, RX_PIN, 512);
  23. * With this, the 512 characters sent can straight into the buffer
  24. * vary quickly. This means LED1 is only on briefly as the TX buffer
  25. * fills.
  26. *
  27. * MODSERIAL uart(TX_PIN, RX_PIN, 32);
  28. * With this, the buffer is smaller than the default 256 bytes and
  29. * therefore LED1 stays on much longer while the system waits for
  30. * room in the TX buffer.
  31. */
  32. MODSERIAL uart(TX_PIN, RX_PIN);
  33. MODDMA dma;
  34. // This function is called when a character goes from the TX buffer
  35. // to the Uart THR FIFO register.
  36. void txCallback(void) {
  37. led2 = !led2;
  38. }
  39. // This function is called when TX buffer goes empty
  40. void txEmpty(void) {
  41. led2 = 0;
  42. pc.puts(" Done. ");
  43. }
  44. void dmaComplete(void) {
  45. led1 = 1;
  46. }
  47. // This function is called when a character goes into the RX buffer.
  48. void rxCallback(void) {
  49. led3 = !led3;
  50. pc.putc(uart.getc());
  51. }
  52. int main() {
  53. char s1[] = " *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* ";
  54. int c = 'A';
  55. // Tell MODSERIAL where the MODDMA controller is.
  56. pc.MODDMA( &dma );
  57. // Ensure the baud rate for the PC "USB" serial is much
  58. // higher than "uart" baud rate below.
  59. pc.baud( PC_BAUD );
  60. // Use a deliberatly slow baud to fill up the TX buffer
  61. uart.baud(1200);
  62. uart.attach( &txCallback, MODSERIAL::TxIrq );
  63. uart.attach( &rxCallback, MODSERIAL::RxIrq );
  64. uart.attach( &txEmpty, MODSERIAL::TxEmpty );
  65. // Loop sending characters. We send 512
  66. // which is twice the default TX/RX buffer size.
  67. led1 = 0;
  68. // Send the buffer s using DMA channel 7
  69. pc.attach_dmaSendComplete( &dmaComplete );
  70. pc.dmaSend( s1, sizeof(s1), MODDMA::Channel_7 );
  71. for (int loop = 0; loop < 512; loop++) {
  72. uart.printf("%c", c);
  73. c++;
  74. if (c > 'Z') c = 'A';
  75. }
  76. led1 = 0; // Show the end of sending by switching off LED1.
  77. // End program. Flash LED4. Notice how LED 2 and 3 continue
  78. // to flash for a short period while the interrupt system
  79. // continues to send the characters left in the TX buffer.
  80. while(1) {
  81. led4 = !led4;
  82. wait(0.25);
  83. }
  84. }
  85. /*
  86. * Notes. Here is the sort of output you can expect on your PC/Mac/Linux host
  87. * machine that is connected to the "pc" USB serial port.
  88. *
  89. * *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* ABCDEFGHIJKLMNOPQRSTUVWXYZABCDE
  90. * FGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZA
  91. * BCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVW
  92. * XYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRS
  93. * TUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNO
  94. * PQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJK
  95. * LMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFG
  96. * HIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQ Done. R
  97. *
  98. * Note how the DMA blocks the TX buffer sending under standard interrupt control.
  99. * Not until the DMA transfer is complete will "normal" buffered TX sending resume.
  100. *
  101. * Of interest is that last "R" character after the system has said "Done."
  102. * This comes from the fact that the TxEmpty callback is made when the TX buffer
  103. * becomes empty. MODSERIAL makes use of the fact that the Uarts built into the
  104. * LPC17xx device use a 16 byte FIFO on both RX and TX channels. This means that
  105. * when the TxEmpty callback is made, the TX buffer is empty, but that just means
  106. * the "last few characters" were written to the TX FIFO. So although the TX
  107. * buffer has gone empty, the Uart's transmit system is still sending any remaining
  108. * characters from it's TX FIFO. If you want to be truely sure all the characters
  109. * you have sent have left the Mbed then call txIsBusy(); This function will
  110. * return true if characters are still being sent. If it returns false after
  111. * the Tx buffer is empty then all your characters have been sent.
  112. *
  113. * In a similar way, when characters are received into the RX FIFO, the entire
  114. * FIFO contents is moved to the RX buffer, assuming there is room left in the
  115. * RX buffer. If there is not, any remaining characters are left in the RX FIFO
  116. * and will be moved to the RX buffer on the next interrupt or when the running
  117. * program removes a character(s) from the RX buffer with the getc() method.
  118. */
  119. #endif