|
Post by volhout on Mar 10, 2011 12:17:49 GMT 1
Dear Support team, I have been following this thread with great interest, since I am a great puppy lover (since 2.17), but have never taken the step to do anything else than assembly language, a bit of bash, and (in the early days) basic. Your joined effort to get BaCon working in Wary made me decide to drop Puppy 5 and install Wary 5.1.1 with success. Installed the devpack, and downloaded hug.bac. Using NicoEdit. And I have been programming since, taking Barry's proxy program as a reference. I am impressed, and would like to use this configuration for my hobby (model railway automation). In this I need digital IO. One of the options for this is USB IO chips like the FTDI range (I own a few FT245BM modules). But I have no clue on how to add the drivers for these USB chips to the BaCon environment. There is a Linux library (1.0.4) for Mandrake 9, but that would (probably) need re-compilation for Wary ? www.ftdichip.com/Drivers/D2XX.htm And even then, how can I access this library from BaCon ?? Do I use the EXEC command ? Can someone help me ?? Volhout P.S. I originally posted this at the puppy linux forum but was directed here. I hope this is the right place...
|
|
|
Post by Pjot on Mar 10, 2011 13:07:01 GMT 1
Hi volhout,
Can you show me a piece of code (preferably C) which uses these drivers? From there we can try to convert it to BaCon.
Thanks, Peter
|
|
|
Post by volhout on Mar 10, 2011 13:37:54 GMT 1
Hi Peter, There are several code examples in the xxx.tar file that you download when you click in the matrix at 1.0.4 for the Linux version of the FTDI driver. Is that sufficient to get an idea how difficult it is to port it ?? Agian, I am not looking for a "seamles" integration. Anything you can do to make it work is fine. When you have trouble working with the official FTDI driver, I may have found an alternative, but I am not sure how good the support and compatibility is. www.intra2net.com/en/developer/libftdi/index.phpC code example www.intra2net.com/en/developer/libftdi/documentation/Not sure how it must be tailored for Wary 5.1.1 (Puppy) Linux. Many of the USB-RS232 adapters use FTDI chips, so it may be possible for you to find a adapter locally (something to check of the library connects via USB at all). Alternatively I could send you one of mine if needed. Volhout.
|
|
|
Post by Pjot on Mar 10, 2011 18:57:01 GMT 1
Hi Volhout, Basically it comes down to IMPORTing the necessary C calls into BaCon and then use them in your prgram. You can find many examples of this on the BaCon website under the section "Interfaces". A very useful source of information is the FTDI header file on basis of which I have defined the IMPORTS and CONSTANTS below. If we look at the FTDI example program, we can port it as follows: TRAP LOCAL
' Import some calls first CONST library$ = "libftdi.so.1"
' Get the functions from the library IMPORT ftdi_init(long) FROM library$ TYPE int IMPORT ftdi_usb_open(long,int,int) FROM library$ TYPE int IMPORT ftdi_get_error_string(long) FROM library$ TYPE char* ALIAS ftdi_get_error_string$ IMPORT ftdi_read_chipid(long,unsigned int) FROM library$ TYPE int IMPORT ftdi_usb_close(long) FROM library$ TYPE int IMPORT ftdi_deinit(long) FROM library$ TYPE void
CONST TYPE_AM = 0 CONST TYPE_BM = 1 CONST TYPE_2232C = 2 CONST TYPE_R = 3 CONST TYPE_2232H = 4 CONST TYPE_4232H = 5
' BaCon does not know types so we define ' a RECORD with the needed members RECORD ftdic ' USB specific LOCAL usb_dev TYPE long LOCAL usb_read_timeout TYPE int LOCAL usb_write_timeout TYPE int ' FTDI specific LOCAL type TYPE int LOCAL baudrate TYPE int LOCAL bitbang_enabled TYPE unsigned char LOCAL *readbuffer TYPE unsigned char LOCAL readbuffer_offset TYPE unsigned int LOCAL readbuffer_remaining TYPE unsigned int LOCAL readbuffer_chunksize TYPE unsigned int LOCAL writebuffer_chunksize TYPE unsigned int LOCAL max_packet_size TYPE unsigned int ' FTDI FT2232C requirecments LOCAL interface TYPE int LOCAL index TYPE int ' Endpoints LOCAL in_ep TYPE int LOCAL out_ep TYPE int LOCAL bitbang_mode TYPE unsigned char LOCAL eeprom_size TYPE int LOCAL error_str TYPE char* LOCAL async_usb_buffer TYPE char* LOCAL async_usb_buffer_size TYPE unsigned int END RECORD
LOCAL chipid
IF ftdi_init(ADDRESS(ftdic)) < 0 THEN PRINT "ftdi_init failed" END 1 END IF
ret = ftdi_usb_open(ADDRESS(ftdic), 0x0403, 0x6001)
IF ret < 0 THEN PRINT "unable to open ftdi device: ", ret, " (", ftdi_get_error_string$(ADDRESS(ftdic)), ")" END 1 END IF
' Read out FTDIChip-ID of R type chips IF ftdic.type = TYPE_R THEN PRINT "ftdi_read_chipid: ", ftdi_read_chipid(ADDRESS(ftdic), ADDRESS(chipid)) PRINT "FTDI chipid: ", chipid END IF
ret = ftdi_usb_close(ADDRESS(ftdic))
IF ret < 0 THEN PRINT "unable to close ftdi device: ", ret, " (", ftdi_get_error_string$(ADDRESS(ftdic)), ")" END 1 END IF
ftdi_deinit(ADDRESS(ftdic))
END
Copy and paste this code into a file, for example "ftdi.bac", then compile. On my Ubuntu 10.10 64bit I see the following: This makes sense of course as I do not have such a chip attached to my system :-) Best regards Peter
|
|
2lss
Full Member
Posts: 140
|
Post by 2lss on Mar 11, 2011 0:37:53 GMT 1
I was wondering the same thing about ftdi devices.
However I was thinking of a different approach.
I have a Arduino (which is a little micro controller, coupled with an ftdi chip for usb support) and I used a bash script to read the output from /dev/ttyUSB0.
I don't have the script at the moment, but it used the 'tail' command like so:
tail -f /dev/ttyUSB0
This just ran in a loop and executed different programs based on the output of the Arduino.
I'm curious if something similar could be done with bacon. Like:
OPEN "/dev/ttyUSB0" FOR READING AS ftdi_device READLN txt$ FROM ftdi_device do stuff CLOSE FILE ftdi_device
|
|
|
Post by volhout on Mar 11, 2011 9:46:22 GMT 1
@peter I saw your replay and started doing everything that was described in the FTDI readme file. When I was ready, I compiled your program, and got an error message. Only then, I realized your example was based on the open source (the second option) library. I tried to find a list of actions to perform to build the open source library, but could not find one. I am stuck. There is nop compiled library in the tar file, and I have not the faintest clue where to start to compile it (have never done this before). It must be very straight forward, but how 2lssFrom what you write, you are using the FTDI VSP library (virtual serial port). That is the default library for most systems since the primary market for these chips is USB serial port extension. When you plug a FTDI device in (almost any) linux or windows PC it will show up as a serial port. But there is a second library The D2xx (direct access) that allows this so called "bit-bang mode. I will have to blacklist the VSP driver to allow this to work. Volhout
|
|
|
Post by Pjot on Mar 11, 2011 13:37:50 GMT 1
It's not a big deal, really. Just download the FTDI source package, untar and gunzip, then go into the newly created source directory and run './configure' (this tests for packages needed). When finished just run 'make' to compile and then 'make install' to install the library on your system. There are a lot of sites about Linux which provide additional information, for example: www.tuxfiles.org/linuxhelp/softinstall.htmlBest regards Peter
|
|
|
Post by volhout on Mar 11, 2011 14:41:08 GMT 1
Peter, Thanks a lot. I have managed to configure, make, and install the driver. After this I get the same error message as you did....and when I plug in the FT245B, the error message is gone. So I have a good starting point. I will respond to this thread when I make progress with the project. Thanks again for the prompt reply and help. Volhout.....
|
|
|
Post by Pjot on Mar 11, 2011 17:26:57 GMT 1
volhout: thanks for feedback! 2lss: this should work: OPEN "/dev/ttyUSB0" FOR READING AS ftdi_device WHILE TRUE READLN txt$ FROM ftdi_device IF LEN(txt$) > 0 THEN PRINT txt$ SLEEP 50 WEND
Regards Peter
|
|
2lss
Full Member
Posts: 140
|
Post by 2lss on Mar 21, 2011 4:53:35 GMT 1
I gave the 'bitbang' program (post four) with my arduino and here are the results:
ftdi_read_chipid: 0 FTDI chipid: 1919903481
Not sure what this means, but I'm assuming its good since it recognized the device.
Then I tried the example above. Unfortunately this seg faults at the READ line and causes some problems at the kernel level. Here is my demsg:
[35269.857134] usb 5-2: new full speed USB device using uhci_hcd and address 4 [35270.054147] usb 5-2: New USB device found, idVendor=0403, idProduct=6001 [35270.054156] usb 5-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3 [35270.054163] usb 5-2: Product: FT232R USB UART [35270.054167] usb 5-2: Manufacturer: FTDI [35270.054172] usb 5-2: SerialNumber: A900cexD [35270.054390] usb 5-2: configuration #1 chosen from 1 choice [35270.062227] ftdi_sio 5-2:1.0: FTDI USB Serial Device converter detected [35270.062289] usb 5-2: Detected FT232RL [35270.062294] usb 5-2: Number of endpoints 2 [35270.062299] usb 5-2: Endpoint 1 MaxPacketSize 64 [35270.062304] usb 5-2: Endpoint 2 MaxPacketSize 64 [35270.062308] usb 5-2: Setting MaxPacketSize 64 [35270.063254] usb 5-2: FTDI USB Serial Device converter now attached to ttyUSB0 [36112.486782] ftdi_kernel[28834]: segfault at 0 ip 00007f9440f6e13d sp 00007fffc71bf070 error 4 in libc-2.11.2.so[7f9440f0b000+158000]
The segfault happened after I ran the second example program.
I also found my bash script that I used to read ttyUSB0. Turns out I was wrong. I used the 'head' command instead of the 'tail' command.
sudo sh -c 'head -c 32 < /dev/ttyUSB0' EDIT: This was all done on Debian Stable
|
|
|
Post by Pjot on Mar 21, 2011 10:40:19 GMT 1
sudo sh -c 'head -c 32 < /dev/ttyUSB0'
Then the BaCon code should be different also - 'head -c 32' means the first 32K bytes?
Maybe first you can try to do this:
info$ = EXEC$("head -c 32 < /dev/ttyUSB0") PRINT info$
Regards Peter
|
|
2lss
Full Member
Posts: 140
|
Post by 2lss on Mar 22, 2011 5:52:20 GMT 1
Then the BaCon code should be different also - 'head -c 32' means the first 32K bytes? I checked the manual for 'head' and yes -c 32 means the first 32 kb. I'm not sure what the significance of 32 kbits means as its been a while since I experimented with the ardiuno. info$ = EXEC$("head -c 32 < /dev/ttyUSB0") PRINT info$ I that a try and it works as long as its run as root. I am slightly confused as to why READLN doesn't work. The bacon documentation says: and the arduino documentation says (for the serial.print function): www.arduino.cc/en/Serial/PrintSo it seems like it should work, unless I am missing something.
|
|
|
Post by Pjot on Mar 22, 2011 13:03:27 GMT 1
The BaCon READLN is based on the standard C fgets call, with an additional wrapper around it to handle large memory blocks. Note that 'fgets' always expects a CR (0x10) or an EOF on the file descriptor. The 'print' command where Arduino is pointing at, is a method of their "Serial" object. It is propriety software created to read from a serial interface, so it probably has a different implementation structure. Though the result of this command may look the same as the result of the READLN. We would need some glance at the way their 'print' is working to see if we can mimic this in BaCon also. For the 'head' command, we could try to use a binary read limited by 32K instead. This can be done using the GETBYTE statement. The 'head -c 32' could be done as follows: TRAP LOCAL
' How much data we read in total CONST amount = 32 * 1024
' Open file OPEN "/dev/ttyUSB0" FOR READING AS ftdi_device
' Cliam memory to store result mem = MEMORY(amount)
' Read from file descriptor GETBYTE mem FROM ftdi_device SIZE amount
' Close descriptor CLOSE FILE ftdi_device
' Now print the binary data in HRF (Human Readable Format) FOR x = 0 TO amount - 1 PRINT CHR$(PEEK(mem+x)); NEXT
FREE mem
Regards Peter
|
|
2lss
Full Member
Posts: 140
|
Post by 2lss on Mar 23, 2011 7:28:00 GMT 1
I gave the code above a try and it works just like 'head' Thank you for posting that. Would writing to the serial port be similar? ie using PUTBYTE? The 'print' command where Arduino is pointing at, is a method of their "Serial" object. It is propriety software created to read from a serial interface, so it probably has a different implementation structure. Though the result of this command may look the same as the result of the READLN. We would need some glance at the way their 'print' is working to see if we can mimic this in BaCon also. I did a little digging and found the git repo for the arduino project. Here is the source for the print function. github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/Print.cppIn addition to that, from what I read, the arduino language is just a set of functions that work with the avr-libc (part of the avr32-toolchain). So I'm assuming that the serial functions would be included in that? But anyway, I'm getting off topic. For my purposes I think the above example will will be fine. Thanks again for taking the time to help with this.
|
|
|
Post by Pjot on Mar 23, 2011 14:40:31 GMT 1
Yes, indeed. I'll leave this as an exercise to you The 'print' method code you show is C++ based. It shows so-called "overloaded" methods which do not demonstrate the Serial object implementation... anyway, this thread in the forum now has demonstrated 3 ways of accessing the ftdi chip, I hope one of them is useful Best regards Peter
|
|