Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 10 of 10

Thread: Teensy LC serial buffer resize

  1. #1
    Junior Member
    Join Date
    Aug 2015
    Posts
    3

    Teensy LC serial buffer resize

    Hi,
    I am building a chain of 11 TeensyLC units connected by hardwareserial.

    I would like to blast packets of 270 bytes of data from one to the next, and this would be a bit easier to make work if the buffers were at least that size, so I can let the hardware transfer out & in the packets, then process them in the code (that is also doing other things).

    I have found several people describing how to change the Arduino core code to achieve larger buffers, but I am a bit lost with the TeensyLC, as it dosen't directly have a HardwareSerial.cpp in the cores.teensy3 dir, and the hardwareserial1.cpp (etc) only contains :
    Code:
    #include "HardwareSerial.h"
    
    HardwareSerial Serial1;
    
    void serialEvent1() __attribute__((weak));
    void serialEvent1() {}
    Which actual hardwareserial.cpp code is actually used ?

    The one in cores.teensy can be modified up to buffer size 256, due to the uint8_t indexing.
    The one in the arduino 1.6.3 I have installed seems to allow buffers larger than 256 bytes as it has 16 bit var alternative defines in the .h :

    Code:
    #if (SERIAL_TX_BUFFER_SIZE>256)
    typedef uint16_t tx_buffer_index_t;
    #else
    typedef uint8_t tx_buffer_index_t;
    #endif
    #
    (ps, all the teensy lc (and 3.1) stuff is also in a subdir 'avr' which is slightly non-intuitive as they are Arm)

    Thanks if you can help...I am new to all this having come from PIC and MeLabs PBP compiler.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,674
    You need to edit serial1.c. Here's a link to the exact line to edit:

    https://github.com/PaulStoffregen/co.../serial1.c#L40

  3. #3
    Junior Member
    Join Date
    Aug 2015
    Posts
    3
    Quote Originally Posted by PaulStoffregen View Post
    You need to edit serial1.c. Here's a link to the exact line to edit:

    https://github.com/PaulStoffregen/co.../serial1.c#L40
    That's great thank you Paul...I see where I need to change, and it also supports buffers with 16 bit indexes so I will give it a go.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,674
    Lots of people have made this buffer size mod on Teensy 3.1, which uses the same code.

    So far, there haven't been many confirmed cases of doing it on Teensy-LC, so I hope you'll post a followup with your results.

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,915
    I had a question I looked up - this change would need to be made in (hardware\teensy\avr\cores\teensy3\)serial[1,2,3].c as fitting the use case - looks like the right answer.

    I assume these buffers are taken byte for byte from RAM - per active serial port? And the port #2&#3 has '#define TX_BUFFER_SIZE 40' not 64 like the other buffers for #1&2&3

    I find this line #85 in the Serial#.c files that suggest only one 8 byte fifo UART - I suppose that is a holdover from T_3.0 unit?
    // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
    Looking at http://www.pjrc.com/teensy/teensy31.html says the T_3.1 has two FIFOs - Are there no FIFOs on the LC as this shows?
    http://www.pjrc.com/teensy/teensyLC.html

    And semi unrelated - USB (serial) - transmits up to 64 bytes per packet - I saw as I looked this was noted on PJRC.com - so lots of small prints make more waste in packet overhead.

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,674
    Yes, that comment in the code is old info when Teensy 3.0 was the only targeted hardware.

    On the USB side, yes the USB uses 64 byte packets. The code combines small writes to fill up the packets. Doing lots of small writes does use more CPU time to combine the data into the packets, but the code is very good at efficiently packing data to maximize USB bandwidth. There is a timeout and a send_now() function to force sending a partial packet. But other than those cases, you don't need to worry about poor bandwidth usage of packets... at least on the Teensy side.

    However, Windows and Linux are very poor at this. Only Macintosh combines small writes to efficiently use USB bandwidth. So on the PC side, large writes are essential if you want to efficiently send data to a Teensy. Here's some benchmarking I did a couple years ago. Scroll down to "Operating System Differences".

    http://www.pjrc.com/teensy/benchmark...l_receive.html

  7. #7
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,915
    Thx, I saw that table before but missed some of the context on the combined write as the reason for perf diff, also better context to see the Teensy combined the small packets - likely more efficiently than a user solution in general.

    looking AT : Serial1.availableForWrite()
    I wonder if that changed to: serial_write_buffer_free

    Added this thread link to Wiki topics for Serial Buffer adjustment and the USB notes
    Last edited by defragster; 08-14-2015 at 01:13 PM.

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,674
    Quote Originally Posted by defragster View Post
    looking AT : Serial1.availableForWrite()
    I wonder if that changed to: serial_write_buffer_free
    This name was chosen by the Arduino Team after a very long and exhausting public debate. It had many other names at various times. I tried on-and-off for 2+ years to get them to accept this seemingly very simple feature as an official part of Arduino, and every time it got bogged down in endless debate, usually over the name. Finally, "availableForWrite" was accepted as an official and permanent part of Arduino's official API.

    Internally, much of Teensyduino's code is pure C, with thin C++ wrappers. The idea is to (hopefully) help people who wish to use only C. But that seems to be less and less important as time goes on....

  9. #9
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,915
    Got it - I find HardwareSerial.h does the mapping for serial and serial2,serial3:
    virtual int availableForWrite(void) { return serial_write_buffer_free(); }

  10. #10
    Senior Member
    Join Date
    Jul 2014
    Posts
    2,365
    Quote Originally Posted by PaulStoffregen View Post
    Internally, much of Teensyduino's code is pure C, with thin C++ wrappers. The idea is to (hopefully) help people who wish to use only C. But that seems to be less and less important as time goes on....
    Well, I ONLY use pure C-code, so most of Paul's libraries are useful for me

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •