Difference between fopen and spin2cpp for full duplex serial
robyouyou
Posts: 3
Hi,
I have a problem that I can't wrap my head around. I'm trying to read serial data from an RFID reader using full duplex serial. When do a simple convert of the .spin file that works everything in propgcc works great. However when I try and use fopen with the _FullDuplexSerialDriver directly nothing works. Here's the code that does not work:
I'm I doing something monumentally stupid/missing something obvious? The same basic code structure (wait times, etc) works great via spin2cpp so I know it must be doing something related to the driver incorrectly (but I have no clue what).
Any help/blinding glimpses of the obvious would be greatly appreciated.
I have a problem that I can't wrap my head around. I'm trying to read serial data from an RFID reader using full duplex serial. When do a simple convert of the .spin file that works everything in propgcc works great. However when I try and use fopen with the _FullDuplexSerialDriver directly nothing works. Here's the code that does not work:
#include <propeller.h> #include <stdlib.h> #include <stdio.h> extern _Driver _FullDuplexSerialDriver; /** * Main program function. */ int main(void) { uint8_t tagID[12]; uint8_t Enable; uint8_t RFRead; int i; printf("Start of program\n"); waitcnt(CLKFREQ * 3 + CNT); Enable = 0xF8; RFRead = 0xFA; /* driver to use Baud rx tx mode */ FILE *NFCin = __fopen_driver(NFCin, &_FullDuplexSerialDriver, "19200, 4, 3", "rb+"); printf("Opened Serial Port 4/3 setting raw IO\n"); setvbuf(NFCin, NULL, _IONBF, 0); NFCin->_flag &= ~_IOCOOKED; printf("Turning on NFC Reader\n"); fwrite(&Enable, 1, 1, NFCin); waitcnt(CLKFREQ * 3 + CNT); fwrite(&RFRead, 1, 1, NFCin); waitcnt(CLKFREQ + CNT); printf("Reading Tag.\n"); while(1) { fwrite(&RFRead, 1, 1, NFCin); waitcnt(CLKFREQ/2 + CNT); fread(tagID, 1, 12, NFCin); for (i=0; i<= 11; i++) { printf("Byte %d = %x\n", i, tagID[i]); } waitcnt(CLKFREQ + CNT); } fclose(NFCin); return 0; }
I'm I doing something monumentally stupid/missing something obvious? The same basic code structure (wait times, etc) works great via spin2cpp so I know it must be doing something related to the driver incorrectly (but I have no clue what).
Any help/blinding glimpses of the obvious would be greatly appreciated.
Comments
It would probably be easier to set up two FILE structs, one for reading and one for writing. As long as they use the same pins the FullDuplexSerial driver will re-use the same COG.
Eric
Thanks for the tip. I tried the following:
and now the program hangs (blocking waiting for input I'm guessing) at the fread section in the while loop (before it would just spit out 0 as the byte read). I played around with the mode for the fopen and noticed that if I changed rb+ to w on the NFCout pointer, the program would hang at the first fwrite. So it appears maybe there's something odd going on with the mode string. Is there some special string to use for serial TTL communication (i.e. given this is a serial buffer and not a "file" should I always be starting at the head of the stream - rewinding the file pointer before each read/write)?
I have a suspicion that I'm not writing anything out at all as I included an fwrite to turn on a LED on the RFID reader board and the LED never comes on. Short of a scope, is there anyway to tell what's going on?
The odd thing about this is that the spin2cpp version works great - maybe I'll just have to go that route but it only supports 1 reader and I have 3 that I need to talk to (all TTL serial). I guess I could just start and stop the spin2cpp FDS driver for each reader but this feels like a cop out.
So to talk to a device you would use 'open/read/write' instead, which works directly on the device/driver level. How fread & co is implemented for the Propeller I don't know, but if it isn't buffering then it's strictly speaking not a stdio implementation.
-Tor
It's buffered.
Thanks jazzed. Ok so that means my general-case description holds up - the (upcoming) read/write functions would be a better choice for where you want to read X bytes from a communication channel if X bytes was specified in the call (so as to not let fread() having its own idea about how many bytes to read). With fwrite it's possible to work around the buffering problem with an fflush(), but fread() isn't that easy.
-Tor
Thanks again,
Rob.