Updated: Serial Port Driver Code

I’ve updated the serial port driver to work with the C18 2.4 C compiler’s output stream functions. The web page will be updated tonight, adding links to the new code. Until then, please try out the software and see if you can find any bugs in the code or documentation. In it’s current incarnation, the CMUCam2 code cannot be made to work with a buffered serial driver. It will take some work, but I suspect that the camera code can be altered to work alongside this code. Here are the links:

http://kevin.org/frc/frc_serial_ports_0.3.zip
http://kevin.org/frc/edu_serial_ports_0.3.zip

And here’s the updated readme.txt:


The source code in serial_ports.c/.h contains a software 
implementation of a fully buffered, interrupt-driven, full-
duplex serial port driver that can be used with either or both 
on-board serial ports. This software is also specifically
designed to work with the output stream functions included 
with Microchip's C18 C compiler version 2.4  
The source code in serial_ports.c/.h will work with the 
Robovation (A/K/A EDU-RC) robot controller and the FIRST 
Robotics robot controller without modification.
Because you can now easily receive data from another computer, 
you can interact with your nifty IFI robot controller in real-
time to change operating parameters on-the-fly using common 
terminal emulation software, or send real telemetry to custom 
applications written with Visual Basic, Visual C++, MATLAB, 
etc... Don't want to drag a long serial cable behind your 'bot? 
Well, check-out the nifty SMiRF radio modem from SparkFun 
Electronics ([http://www.sparkfun.com](http://www.sparkfun.com/)). Would the coolness 
factor of your 'bot be elevated if you had a LCD mounted on 
board to display diagnostics (yes, this is a rhetorical 
question)? How about using one of the serial LCDs that can be 
found on the 'net? I've had success using Scott Edward's 
Electronics ([http://www.seetron.com](http://www.seetron.com/)) serial LCDs. The TRM-425L 
will work with the TTL-level serial port two and also includes 
a keypad interface. I've been mostly using the BPP-420L on 
serial port one. To use the above devices you'll need to build 
a simple three or four conductor cable. Disclaimer: Other than 
being a satisfied customer, I have no interest (financially, or 
otherwise) in the companies mentioned above.
The included project files were built with MPLAB version 6.60.
If your version of MPLAB complains about the project version, 
the best thing to do is just create a new project with MPLAB's 
project wizard. Include every file except: FRC_alltimers.lib, 
ifi_alltimers.lib, and you should be able to build the code.
By default, serial port one will operate at 115200 baud, which
is compatible with InnovationFIRST's terminal program, and 
serial port two will operate at 9600 baud, which will work with 
the above mentioned peripheral devices. These values can be 
easily changed by modifying the serial port initialization 
functions mentioned below.
For an example of how to use this software, see the code
in Process_Data_From_Master_uP(), which demonstrates how to
properly use this new functionality.
***************************************************************
Here's a description of the functions in serial_ports.c:

Init_Serial_Port_One()
Init_Serial_Port_Two()
These functions initialize the serial ports. This is where 
you will enable or disable the serial port's receive and/or 
transmit circuitry and set the baud rate at which it will 
operate. One or both of these functions must be called before 
any serial port operations can take place.

Serial_Port_One_Byte_Count()
Serial_Port_Two_Byte_Count()
These functions will return the number of bytes present in
their respective received data queues. Because there might
not be any data in the queues, these functions must be called 
before you can read any data from a serial port.

Read_Serial_Port_One()
Read_Serial_Port_Two()
These functions will return the next byte from the received
data queue. If no data is present in the queue, the function 
will return the number zero, which could cause problems if
your incoming data can also contain a zero. This is why the
Serial_Port_xxx_Byte_Count() functions must be called first.

Write_Serial_Port_One()
Write_Serial_Port_Two()
These functions put a byte of data on the serial port transmit 
queue. If the queue is full, the function will make you wait
until a storage slot becomes available before allowing your
code to execute again.

Rx_1_Int_Handler()
Rx_2_Int_Handler()
When a new byte of data is received by the serial port, the 
microcontroller will automatically call these functions to
get the new data and place it in the received data queue for
you. You shouldn't have to call these functions yourself.

Tx_1_Int_Handler()
Tx_2_Int_Handler()
When the serial port is ready to start sending a new byte of 
data, the microcontroller will automatically call these 
functions to get the next byte of data from the transmission 
queue and give it to the serial port for transmission. You 
shouldn't have to call these functions yourself.

_user_putc()
This function is the "glue" that interfaces the C18 output
stream functions to this serial port driver. If the global
variable stdout  is set to "_H_USER", which is defined in 
stdio.h, the C18 output stream functions will call this
function to send data to a serial port rather than the
library function putc(). Stdout is set to _H_USER within the
serial port initialization functions Init_Serial_Port_One()
and Init_Serial_Port_Two() described above.
***************************************************************
Nine things must be done before this software will work 
correctly:
  1a) FRC-RC: As this software is intended to replace IFI's
  serial port driver, the call to Serial_Driver_Initialize()
  in user_routines.c / User_Initialization() should be 
  removed or commented out. In addition, all references to
  "user_Serialdrv.c" and "user_Serialdrv.h" must be removed 
  from the project and all project source files.
  1b) EDU-RC: As this software is intended to replace IFI's
  serial port driver, the call to Initialize_Serial_Comms()
  in user_routines.c / User_Initialization() should be 
  removed or commented out. In addition, all references to
  "printf_lib.c" and "printf_lib.h" must be removed from 
  the project and all project source files.
  2) You must add the serial_ports.c/.h source files to
  your MPLAB project.
  3) A #include statement for the serial_ports.h header
  file must be included at the beginning of each source
  file that uses the serial ports. The statement should
  look like this: #include "serial_ports.h".
  4) If you intend to use the C18 output stream functions,
  a #include statement for the stdio.h header file must be 
  included at the beginning of each source file that calls
  any of these functions. The statement should look like 
  this: #include <serial_ports.h>.
  5) Init_Serial_Port_One() and/or Init_Serial_Port_Two()
  must be called from the User_Initialization() function 
  located in the user_routines.c source file.
  6) The interrupt handler(s) must be installed in the
  InterruptHandlerLow() function located in the 
  user_routines_fast.c source file. See the accompanying
  copy of user_routines_fast.c to see how this is done.
  7) Decide what functionality you need and comment out the
  #define ENABLE_SERIAL_PORT_xxx_yy entries in serial_ports.h
  as necessary. As an example, if you only need to send data
  using serial port one and would like to reclaim the resources
  used by serial port two and serial port one's receiver
  source code, the top of the serial_ports.h file would look
  like this:
  // comment out the next line to disable all serial port one
  // receive functionality
  // #define ENABLE_SERIAL_PORT_ONE_RX
  // comment out the next line to disable all serial port one
  // transmit functionality
  #define ENABLE_SERIAL_PORT_ONE_TX
  // comment out the next line to disable all serial port two
  // receive functionality
  // #define ENABLE_SERIAL_PORT_TWO_RX
  // comment out the next line to disable all serial port two
  // transmit functionality
  // #define ENABLE_SERIAL_PORT_TWO_TX
  By default, both serial ports and their respective receive
  and transmit sections are enabled.
  8) As the default output device for C18's output stream 
  functions is the null device, you'll presumably want to 
  change the value of stdout_serial_port to "SERIAL_PORT_ONE" 
  or "SERIAL_PORT_TWO" if you want to see printf()'s output.
  User_Initialization() is a good place to do this.
  9) To support terminal emulation software, 
 should 
  be used instead of just 
 in the printf() format string.

This serial port driver can send output stream data to either 
of the serial ports by setting the value of the global variable
stdout_serial_port before calling output stream functions like
printf(). Setting the value to "SERIAL_PORT_ONE" will send the 
output to serial port one. Likewise, setting the value to 
"SERIAL_PORT_TWO" will send the output to serial port two. 
Setting the value to "NUL" will send the output to the null 
device, meaning that the output is sent nowhere. These values 
are #define'd in serial_ports.h. As an example,
  stdout_serial_port = SERIAL_PORT_ONE;
  printf("Kernighan");
  stdout_serial_port = NUL;
  printf("and");
  stdout_serial_port = SERIAL_PORT_TWO;
  printf("Ritchie");
will send the text "Kernighan" to the peripheral device attached
to serial port one, the text "Ritchie" to the device attached to 
serial port two and the text "and" won't be sent anywhere.
By default, output is sent to the null device, which is the only
output device guaranteed to be present.

-Kevin

:ahh:

We just spent all day today updating our camera code to work with your old drivers! How changed are the new ones?

It’s called job security, Dude <grin!>. I added the “glue” function, _user_getc(), that’s required to interface to the new output stream functions. I dumped the old printf() interface, and now use the variable “stdout_serial_port” to select which port to send the output (stdout is now used by Microchip’s code). If you made the changes to the camera code I suspect you did, you should be able to just drop the new code into your build and disable serial port two in serial_ports.h.

-Kevin

Greg,

Don’t worry… We will deal with it.

Mike

Kevin,

Thanks (as always), for your hard work… I imagine we will continue to use your old serial code as a basis but I’ll diff your new files tonight and see if what you did makes sense for us to incorporate…

Our goal is to use port 1 for the camera and port 2 for the SMiRF so that we have a wireless interface for development of both the CMU target identification and our trajectory planner for navigation. So, we do not want to disable anything… I think we are close to having it figured out.

Once again, thanks.

Regards,

Mike

Mike,

The only reason to use the new driver is if you want to use Microchip’s new output stream capabilities. I didn’t touch the driver’s inner workings.

-Kevin