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

Thread: Correct way to use Serial object outside of Arduino IDE

  1. #1

    Correct way to use Serial object outside of Arduino IDE

    Hi,

    I'm trying to work with a teensy 4.1 board but using linux command line tooling instead of the Arduino IDE.

    To use the Serial object (virtual serial over usb, I think), I tried including usb_serial.h, at which point I hit this case: https://forum.pjrc.com/threads/34651...n-usb_serial-h

    According to Paul's post there, I'm doing it wrong, so what's the conventional way to do this?

    Thanks

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,016
    Sorry I am not really sure what you are asking here? Or what your setup is or the like. Or see any of your code or ...
    Or what is working for you and what is not.

    Example is the Linux your main PC that you wish to build for? Or is it something connected up for example like an RPI or UP or ... that is connected through a remote connection, example Windows using PuttY...
    If so again you have many options. Build on Windows, copy the binary down to the remote machine, and then use command line like teensy_loader_cli to program your Teensy 4.1... I used to do this with things like RPI.

    Or you wish to simply build on Linux box with command line. One option is to use makefiles and build using make.
    Another option, is I know that @FrankB with @defragster setup some batch scripts for windows, which build using the same tools as Arduino. I use this within sublimeText to do builds. Not sure if anyone has converted these scripts to linux scripts or not. I like this approach as then the things I build with either Arduino or within SublimeText should produce the same binary.

    There may be other ways as well, I know lots of people use PlatformIO to build, don't know if they have a method to do builds from CLI...

    But again not sure if this is answering your question. USB Serial. Assuming you are setup to use the core code with C++, you should simply be able to user the Serial object. Now if you are one who only wishes to use C, than maybe you might need some header files like that. But again hard to tell.


    Again sorry for just throwing out darts here, but hopefully gives you some possibilities.

  3. #3
    Thanks Kurt.

    Yeah, I'm just trying to build a teensy executable/hex file on a linux machine. I think I'm alright with the Makefile part, or at least the one I have seems to work okay. It's pretty much a hacked up version of this one: https://github.com/PaulStoffregen/co...ensy4/Makefile I have the CLI teensy loader also, works fine for known-good .hex files so far.

    Assuming you are setup to use the core code with C++, you should simply be able to user the Serial object.
    I'm using C++, and I think this might be the part I'm missing. what's the correct way to use the core code? Certainly, something must be included somewhere to use the Serial object?

    The code I'm trying to compile would be something like this:

    Code:
    #include "teensy4/usb_serial.h"
    
    int main()
    {
      while (!Serial) {
    
      }
    
      Serial.write(<stuff>);
    }

  4. #4
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,016
    Sorry I have not played with the makefiles in a very long time... But one key thing is to make sure you have the right USB type selected.

    That is in the makefile you pointed to you see: OPTIONS = -DF_CPU=600000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH -DUSING_MAKEFILE
    So I believe it should have all of that stuff included. But again I have not played with it in a very long time.

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,103
    Quote Originally Posted by seanseanseansean View Post
    I think I'm alright with the Makefile part, or at least the one I have seems to work okay. It's pretty much a hacked up version of this one: https://github.com/PaulStoffregen/co...ensy4/Makefile I have the CLI teensy loader also, works fine for known-good .hex files so far.
    I tested just now by adding Serial.println("test"); to main.cpp. That original Makefile compiles it properly. I uploaded the hex file to a Teensy 4.0, and indeed it does print "test" every second when I open the port with the Arduino Serial Monitor.

    So I'm going to suggest you go back to testing with the original not-hacked-up Makefile to get Serial.print() working. Then maybe compare what you changed, or re-apply your changes incrementally until you find the thing that broke Serial.print().

  6. #6
    Thanks Paul.

    I don't really think I was creating a project in the right way at all. I had a folder that simply had my main.cpp and the Makefile, but I guess with that setup, there's no way to get what's in usb_serial.c, etc.

    So, I tried just editing the main.cpp in my local, unmodified copy of https://github.com/PaulStoffregen/co...master/teensy4. I still got the same error at first, but first including Arduino.h in my main.cpp got things working.

    Let me know if there's a better way to do this, but I'm happy things are working

  7. #7
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,016
    Hopefully Paul or someone who uses Linux and Makefiles a lot can give you a decent answer.

    If I were wanting to do this, I would probably tend to have sort of recursive makefiles. That is for each library and core I was using, I would have each of them maybe have their own makefiles and have them generate a library (archive).

    I would then have my main sketches have their own makefile that calls through to first call of to make sure all of the things I used was built and then builds my sketch and links it all together.
    Again others may have different approaches...

    Mine was modeled if I remember correctly after some ones from another member (here and TrossenRobotics) @jwatte
    It also did a little dependency stuff by building .d files...

    Also these builds were for the host not for a teensy. Used them on RPI, BBBK, Edison, UP, Odroid...

    But an example one I have on my RPI project for building an example sketch to test out PWM looked like:
    Code:
    #~~~~~~~~~~~~~~~~~~~~ Output File Name ~~~~~~~~~~~~~~~~~~~~
    MAIN_OUT = BasicPWMExample
    
    #~~~~~~~~~~~~~~~~~~~~ Source Files ~~~~~~~~~~~~~~~~~~~~
    SOURCES = \
    		BasicPWMExample.cpp  
    
    MAIN_OBJS:= $(subst .cpp,.o,$(SOURCES))
    
    MAIN_DEPS:= $(subst .cpp,.d,$(SOURCES))
    
    #~~~~~~~~~~~~~~~~~~~~ Include Directories ~~~~~~~~~~~~~~~~~~~~
    INCLUDE_DIRS = -I. -I../library
    
    
    #~~~~~~~~~~~~~~~~~~~~ Library Directories ~~~~~~~~~~~~~~~~~~~~
    LIBRARY_DIRS = -L/usr/lib/arm-linux-gnueabihf -L../library
    
    #~~~~~~~~~~~~~~~~~~~~ Compiler Options ~~~~~~~~~~~~~~~~~~~~
    COMPILE_OPTS = -pedantic -g -O2 -fno-rtti
    
    #~~~~~~~~~~~~~~~~~~~~ Linker Options ~~~~~~~~~~~~~~~~~~~~
    LDFLAGS = $(LIBRARY_DIRS) -lpthread -lArduinoPort -lrt
    
    #~~~~~~~~~~~~~~~~~~~~ Toolchain Prefix ~~~~~~~~~~~~~~~~~~~~
    ifeq ($(OSTYPE),linux-gnueabi)
    TCHAIN_PREFIX=arm-angstrom-linux-gnueabi-
    else
    TCHAIN_PREFIX=arm-linux-gnueabihf-
    endif
    #TCHAIN_PREFIX=x86_64-linux-gnu-
    
    CXX = $(TCHAIN_PREFIX)g++
    CXXFLAGS = $(COMPILE_OPTS) $(INCLUDE_DIRS)
    
    #~~~~~~~~~~~~~~~~~~~~ all ~~~~~~~~~~~~~~~~~~~~
    
    all: LIBRARY begin gccversion build end
    
    #~~~~~~~~~~~~~~~~~~~~ library ~~~~~~~~~~~~~~~~~~~~
    LIBRARY:
    	cd ../library; $(MAKE)
    
    
    #~~~~~~~~~~~~~~~~~~~~ build ~~~~~~~~~~~~~~~~~~~~
    
    
    build: $(MAIN_OUT)
    
    $(MAIN_OUT): $(MAIN_OBJS) ../library/libArduinoPort.a
    	$(CXX) $(CXXFLAGS) $(MAIN_OBJS) -o $(MAIN_OUT) $(LDFLAGS)
    
    MSG_BEGIN = -------- begin --------
    MSG_END = --------  end  --------
    
    #~~~~~~~~~~~~~~~~~~~~ Eye candy ~~~~~~~~~~~~~~~~~~~~
    begin:
    	@echo
    	@echo $(MSG_BEGIN)
    
    end:
    	@echo $(MSG_END)
    	@echo
    
    gccversion:
    	@$(CC) --version
    
    #~~~~~~~~~~~~~~~~~~~~ clean ~~~~~~~~~~~~~~~~~~~~
    clean: begin clean_list end
    
    clean_list:
    	-rm $(MAIN_OBJS)
    	-rm $(MAIN_OUT)
    	-rm $(MAIN_DEPS)
    	-cd ../library ; make clean
    
    #~~~~~~~~~~~~~~~~~~~~ backup ~~~~~~~~~~~~~~~~~~~~
    backup: clean
    	tar cJvf ../$(MAIN_OUT)_`date +"%Y-%m-%d_%H%M"`.tar.xz *
    
    #~~~~~~~~~~~~~~~~~~~~ Dependency Generation
    include $(subst .cpp,.d,$(SOURCES))
    
    %.d: %.cpp
    	$(CC) -M $(CXXFLAGS) $< > $@.$$$$; \
    	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@;  \
    	rm -f $@.$$$$
    But again I don't know how relevant something like this is to what your desired setup is.

  8. #8
    Yeah I'm probably headed down that path now that I have a better understanding of things, thanks.

Posting Permissions

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