|
|
View previous topic :: View next topic |
Author |
Message |
csshah
Joined: 28 Feb 2005 Posts: 24
|
driver for ftdi 245BM |
Posted: Tue Aug 09, 2005 6:40 pm |
|
|
hi all
I came across an old post of ftdi 245bm driver for ccs. actually i want to communicate with pc using pic 18f8720 and ftdi 245bm. i found the driver for usb from an old post but dont know how to make use of it in order to communicate with PC. here I am pasting that driver if u can tell me how can i use it with VC++ to get some console window for communication.
[#include .... // include CPU specific header and port definitions
#define TxCQsize 64 // Console Tx buffer size
#define RxCQsize 64 // Console Tx buffer size
//; Console Rx Ring Buffer control
byte Rx_HeadC;
byte Rx_TailC;
//; Console Tx Ring Buffer control
byte Tx_HeadC;
byte Tx_TailC;
// Console Tx buffer
byte Tx_BaseC[TxCQsize];
// Console Rx buffer
byte Rx_BaseC[RxCQsize];
void console_putc(char TxChar)
/************************************************************************************************
;
; Subroutine console_putc
;
; Stdout routine (BLOCKING) for putting characters to the console interface.
;
; On Exit:
;
; ***********************************************************************************************/
{
long counter = 65535; // setup counter to prevent lockup on failure of USB
//; test to determine if the USB device is active
if (!bit_test(USB_PwrEn))
{
while ((counter!=0) && bit_test(USB_WRF))
{
counter--;
}
if (Counter)
{
DBus = TxChar; //; write the character to the data bus
bit_set(USB_WR);
DBusDir = 0; //; set data bus to output
bit_clear(USB_WR);
DBusDir = 0xff; //; set data bus to input
}
else
{
// timeout waiting on USB
// .. do something else here such as write to serial port
putc(TxChar);
}
}
else
{
//; USB port not active, do something else like pass control to the async port code
putc(TxChar);
}
}
bit console_kbhit(void)
{
// test to determine if the USB device is active
return ((!bit_test(USB_PwrEn)) && (!bit_test(USB_RDF)));
}
char console_getc(void)
{
char Rx_Char = 0x00;
// test to determine if the USB device is active
// if not active pass exit
if ((!bit_test(USB_PwrEn)) && (!bit_test(USB_RDF)))
{
// here we have a character in the USB controller
DBusDir = 0xFF; // set the data bus to input
bit_clear(USB_RD);
Rx_Char = DBus;
bit_set(USB_RD);
}
return (Rx_Char);
}
void main()
{
init_pic();
//; initialise the I/O lines to the USB controller
bit_set(USB_FLUSH); //; initialise the flush FIFO pin
bit_clear(USB_WR); //; set default WR level
bit_set(USB_RD); //; set default RD level
//; initialise Console receive buffer pointers
Rx_HeadC = 0;
Rx_TailC = 0;
//; initialise Console transmit buffer pointers
Tx_HeadC = 0;
Tx_TailC = 0;
printf(console_putc,"Hello, world\r\n");
//ExecLoop
while (1)
{
// do something
// don't forget to scan the USB device
// note that the FTDI245 has a large input buffer
// there is not need to do a buffer to buffer copy
//
}
}
][/code] |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1635 Location: Perth, Australia
|
|
Posted: Tue Aug 09, 2005 9:18 pm |
|
|
Here is a slightly enhanced version of the same. It uses a flag to remember if the USB device is in a stalled state to avoid waiting. The FTDI can effectively stall if the windows machine has no application servicing the receive queue for virtual comm port assigned to the FDTI device. In this case the FTDI transmit buffer fills and, unless you have incorporated error handling, the PIC can loop indefinitiely waiting to send to the FTDI chip. Note this problem is a generic problem not specifically an FTDI issue.
Code: |
#include .... // include CPU specific header and USB port definitions
// define the USB Data bus
#define DBus PORTD // define the USB data bus
#define DBusDir TRISD // define the USB data bus direction register
// define the PIC I/O busses to the USB Controller
#define USB_PwrEn PORTB, 5 // low if USB active, else high
#define USB_WR LATB, 7 // I/O write strobe to the USB Controller
#define USB_RD LATB, 6 // I/O read strobe to the USB Controller
#define USB_RDF PORTE,0 // RD Ready Flag USB Controller
#define USB_WRF PORTE,1 // WR Ready Flag USB Controller
#define USB_Flush LATE,2 // Flush FIFO ignal to the USB Controller
boolean USB_Stall = false; // used to detect USB bus stall
#define putch_usb putc_usb
void putc_usb(char TxChar)
///////////////////////////////////////////////////////////////////////////
//
// Subroutine putc_usb
//
// Stdout routine (SEMI-BLOCKING) for puting characters to the USB interface.
//
// On Exit:
//
// Calls:
//
///////////////////////////////////////////////////////////////////////////
{
unsigned long counter;
counter = 5000; // setup counter to prevent lockup on failure of USB
// test to determine if the USB device is active
if (!bit_test(USB_PwrEn))
{
// if USB ready to accept write operations then clear the stall flag
if(!bit_test(USB_WRF))
USB_Stall = false;
// if the USB device is stalled then exit
if(bit_test(USB_WRF) && USB_Stall)
return;
while ((counter!=0) && bit_test(USB_WRF) && !bit_test(USB_PwrEn))
{
restart_wdt();
counter--;
}
if (counter && !bit_test(USB_WRF) && !bit_test(USB_PwrEn))
{
DBus = TxChar; // write the character to the data bus
bit_set(USB_WR);
DBusDir = 0; // set data bus to output
bit_clear(USB_WR);
DBusDir = 0xff; // set data bus to input
}
else
USB_Stall = true; // USB device appears to have stalled
}
}
#define getch_USB getc_USB
char getc_USB()
///////////////////////////////////////////////////////////////////////////
//
// Subroutine getc_USB
//
// Read a character from the USB controller (BLOCKING IF NO CHARACTER)
// Returns immediately if the USB controller is off line
//
// On Entry:
//
// On Exit:
//
// Calls:
//
///////////////////////////////////////////////////////////////////////////
{
Char RxChar = 0;
// test to determine if the USB device is active
if (!bit_test(USB_RDF) && (!bit_test(USB_PwrEn)))
{
// here if the USB port is active and characters are present
DBusDir = 0xff; // set data bus to input
bit_clear(USB_RD);
RxChar = DBus; // read the character from the data bus
bit_set(USB_RD);
}
return (RxChar);
}
boolean kbhit_USB()
///////////////////////////////////////////////////////////////////////////
//
// Subroutine kbhit_USB
//
// Return true is a character is ready to be extracted from the USB controller
//
// On Entry:
//
// On Exit:
//
// Calls:
//
///////////////////////////////////////////////////////////////////////////
{
return(!bit_test(USB_RDF) && (!bit_test(USB_PwrEn)));
}
void main()
{
// do PIC initialization
// init_pic();
// initialise the I/O lines to the USB controller
bit_set(USB_FLUSH); // initialise the flush FIFO pin
bit_clear(USB_WR); // set default WR level
bit_set(USB_RD); // set default RD level
printf(putc_usb,"Hello, world\r\n");
//ExecLoop
while (1)
{
// do something
// don't forget to scan the USB device
// note that the FTDI245 has a large input buffer
// there is not need to do a buffer to buffer copy
//
}
}
|
_________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|