1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
|
DZCOMM 0.9.9 (WIP)
A serial port API.
By Neil Townsend, Dec 08, 1999.
See the AUTHORS file for a
complete list of contributors.
#include <std_disclaimer.h>
"I do not accept responsibility for any effects, adverse or otherwise,
that this code may have on you, your computer, your sanity, your dog,
and anything else that you can think of. Use it at your own risk."
========================================
============ General points ============
========================================
See readme.txt for a general introduction, copyright details, and
information about how to install dzcomm and link your program with it.
Dzcomm is a result of the magnificent Allegro library. It has provided the
inspiration and in many cases the code which has simplfied the process of
developping what I hope will become a multi-platform serial line API.
Hopefully, Dzcomm has now evolved to the point where it can work alongside
allegro but no longer relies on it. It should have alternative routines
for everything it previosuly relied on allegro to do and should decide
when you compile it in whether to use allegros routines or its own.
To use dzcomm in your code you will need to put
#include <dzcomm.h>
in your code. Obviously (I hope) this will only work after you've installed
the library. If you wish to use allegro alongside dzcomm you will need to
include the allegro library. If you do so it is IMPERATIVE that you include
allegro first (this how dzcomm knows where to link to):
#include <allegro.h>
#include <dzcomm.h>
If you have more than one C file in a project and you wish to access dzcomm
from more than one of those files, you must put the following #define before
all but one of the dzcomm includes:
#define DZCOMM_SECONDARY_INCLUDE
#include <dzcomm.h>
However, do remember to inlcude it once without the #define!
============================================
============ Port configuration ============
============================================
To use dzcomm it is necessary to initialise the library and then to configure
the ports. Each serial line is considered as an indepnedent input/output port
and has certain characteristics associated with it. All the routines which talk
to, read from or somehow control an serial line will take, as one of their
arguemnts, a pointer to the structure which defines that port:
comm_port *port.
This structure contains a complete desription of how the port is configured.
However, it is not necessary (or advisable) to alter the contents of this
structure directly. A collection of routines are provided which enable the
port to be configured and used without having to mess around with the guts
of this structure.
The following sequence should be used:
comm_port *my_port;
dzcomm_init(); // initialise the library
my_port = comm_port_init(_comN); // create the port
comm_port_set_*(my_port, *); // define it
comm_port_install_handler(my_port) // run the port as per this definition
// And then read and write from and to the port.
int dzcomm_init();
Initialises the dzcomm library. A necessary call before using any of the
other functions. Having done this you will want to initialise one or more
ports and then use them. The function returns 1 if dzcomm is successfully
installed, 0 otherwise.
void dzcomm_closedown();
Does the opposite to dzcomm_init. Closedown the dzcomm serial API and
releases system resources as much as it can. Is automatically called
on program exit (assuming that dzcomm_init has been called) and will
will uninstall and delete any ports which require it. It is therefore
not necessary to uninstall or delete ports explicity in code which uses
the dzcomm library (0.9.4 and above).
comm_port *comm_port_init(comm n);
Creates a handle (which is a pointer to a structre) for a new port.
The argument, defined below, simply gives comm_port_init a hint as
to which potentially built-in port the port should define. If the
port you are using bears no relation to a standard PC built in port
just send an arbitrary argument and then use the comm_port_set_*
routines to describe the port. Even if if you are using a standard
port you will want to use the comm_port_set_* routines to complete
the configuration of the port.
The argument must be one of the following. Please use these values
rather than infering what the enum values might be.
typedef enum {_com1, _com2, _com3, _com4, _com5, _com6, _com7, _com8} comm;
int comm_port_load_settings(comm_port *port, char *file_name)
Load the setting for a comm port from a file. Traditionally these files
have had a ".ini" extension but that is not required. Return values are
1 for success and 0 for failure.
Format of the file:
baud=; //={110,150,300,600,1200,2400,...,115200}
Example : baud=9600;
data=; //={5,6,7,8}
Example : data=7;
parity=; //={Even,Odd,No(None)}
Example : parity=Even;
stop=; //={1,2}
Example : stop=1;
irq=;
Example : irq=5;
address=; // port address in hex.
Example : address=0x2f8; // Note : use 0x prefix always.
control=;
//={xon_xoff(xon/xoff),rts_cts(rts/cts),no(none)}
Example : control=xon_xoff;
name=; // just name of port. Not used by any lib function.
Example : name=Modem Courier V.EVERYTHING;
void comm_port_set_irq_num(comm_port *port, unsigned char irq_num);
Set the IRQ number for the port. irq_num should be the IRQ line the port is
set to use, between 0 and 15 inclusive. The value set will only come into effect
when a port is reinstalled.
void comm_port_set_name(comm_port *port, char *name);
Set the name that is associated with the port. name should be a pointer to
a 0 terminated string which will be copied into an internal string. The
maximum allowed length of the string is DZCOMM_PORT_NAME_MAX_LENGTH, which
is defined in dzcomm.h. This name is irrelevant to the actual running of the
port and this _set_ call will take effect immediately.
void comm_port_set_port_address(comm_port *port, unsigned short int port_address);
Set the address of the port. The value set will only come into effect
when a port is reinstalled.
void comm_port_set_baud_rate(comm_port *port, baud_bits baud_rate);
Set the baud rate the port should use. The value set will only come into effect
when a port is reinstalled.
The argument must be one of the following. Please use these values
rather than infering what the enum values might be.
typedef enum {_110, _150, _300, _600, _1200, _2400, _4800,
_9600, _19200, _38400, _57600, _115200} baud_bits;
void comm_port_set_data_bits(comm_port *port, data_bits num_data_bits);
Set the number of data bits the port should use. The value set will only
come into effect when a port is reinstalled.
The argument must be one of the following. Please use these values
rather than infering what the enum values might be.
typedef enum {BITS_5, BITS_6, BITS_7, BITS_8} data_bits;
void comm_port_set_stop_bits(comm_port *port, data_bits num_stop_bits);
Set the number of stop bits the port should use. The value set will only
come into effect when a port is reinstalled.
The argument must be one of the following. Please use these values
rather than infering what the enum values might be.
typedef enum {STOP_1, STOP_2} stop_bits;
void comm_port_set_parity(comm_port *port, parity_bits irq_num);
Set the parity method for the port. The value set will only
come into effect when a port is reinstalled.
The argument must be one of the following. Please use these values
rather than infering what the enum values might be.
typedef enum {NO_PARITY, ODD_PARITY, EVEN_PARITY, MARK_PARITY, SPACE_PARITY} parity_bits;
void comm_port_set_flow_control(comm_port *port, flow_control_type flow_option);
Set the flow control method the port should employ. The value set will only
come into effect when a port is reinstalled.
The argument must be one of the following. Please use these values
rather than infering what the enum values might be.
typedef enum {NO_CONTROL, XON_XOFF, RTS_CTS} flow_control_type;
void comm_port_set_lsr_handler(comm_port *port, dz_ll_handler lsr_handler);
HANDLE WITH CARE. This sets a routine which will be called when the
UART returns an LSR condition. The code mnust be locked in memory if
your system requires it. The lsr_handler argument is the function
which will be called when the condition arises:
int lsr_hander(comm_port *port, int lsr_contents)
It take two arguments: the port on which the condition arose and the
contents of the LSR register. You should not reread the LSR in the
handler as the read which was used to pass the contents to this
function will have reset it.
dz_ll_handler is simply a shorthand for a pointer to the above type of
function.
This call takes effect immediately.
void comm_port_set_msr_handler(comm_port *port, int (*msr_handler)(comm_port *port, int msr_contents));
HANDLE WITH CARE. This sets a routine which will be called when the
UART returns an MSR condition. The code mnust be locked in memory if
your system requires it. the msr_handler argument is the function
which will be called when the condition arises:
int msr_hander(comm_port *port, int msr_contents)
It take two arguments: the port on which the condition arose and the
contents of the MSR register. You should not reread the MSR in the
handler as the read which was used to pass the contents to this
function will have reset it.
dz_ll_handler is simply a shorthand for a pointer to the above type of
function.
This call takes effect immediately.
unsigned char comm_port_get_irq_num(comm_port *port);
Returns the IRQ number for the port. Returns the most recent value set by the
_set_ routine, which may not have been into action yet.
char *comm_port_get_name(comm_port *port, char *name, int max_chars);
Returns the name that is associated with the port. name should be a pointer to
a block of memory which can hold up to max_chars characters in which the name
will be stored. Note that the maximum length of a port name is
DZCOMM_PORT_NAME_MAX_LENGTH.
unsigned short int comm_port_get_port_address(comm_port *port);
Returns the address of the port. Returns the most recent value set by the
_set_ routine, which may not have been into action yet.
baud_bits comm_port_get_baud_rate(comm_port *port);
Returns the baud rate the port should use. Returns the most recent value set by the
_set_ routine, which may not have been into action yet.
data_bits comm_port_get_data_bits(comm_port *port);
Returns the number of data bits the port should use. Returns the most recent value set by the
_set_ routine, which may not have been into action yet.
data_bits comm_port_get_stop_bits(comm_port *port);
Returns the number of stop bits the port should use. Returns the most recent value set by the
_set_ routine, which may not have been into action yet.
parity_bits comm_port_get_parity(comm_port *port);
Returns the flow control method for the port. Returns the most recent value set by the
_set_ routine, which may not have been into action yet.
flow_control_type comm_port_get_flow_control(comm_port *port);
Returns the flow control method used by the port.
dz_ll_handler comm_port_get_lsr_handler(comm_port *port);
Return the user's lsr handler, if any. It returns a pointer to a function
of type:
int lsr_hander(comm_port *port, int lsr_contents)
or NULL if no function has been set.
dz_ll_handler comm_port_get_msr_handler(comm_port *port);
Return the user's lsr handler, if any. It returns a pointer to a function
of type:
int msr_hander(comm_port *port, int msr_contents)
or NULL if no function has been set.
===================================
============ Data flow ============
===================================
Having set up your ports you will want to use them! To do this you must
install the handler. Having donw this ou can then read from and write
to the ports.
int comm_port_install_handler(comm_port *port);
Once the port has been configured, whether from a file or by using the
comm_port_set_* routines, this function must be called to ensure that
the port is running with the desired confiuration. It should only be
called once per port, configuration changes should be enfored using
comm_port_reinstall. Until this function is called the port cannot be
written to or read from.
int comm_port_reinsatall(comm_port *port);
If there is a need to reconfigure the port after comm_port_install_port
has been called, this can be achieved in two steps. First, reconfigure
the comm_port structure either using a file or using the comm_port_set_*
functions. Then call this function to ensure that they take effect. This
call will wait for any remaining data on the transmit queue to clear
before reconfiguring the port.
void comm_port_uninstall(comm_port *port);
Removes the port from the list of functioning ports under dzcomm. Once
this function has been called it is no longer to write to or read from
the port. Use comm_port_install_handler to get the port running again.
This function will wait for any remaining data on the transmit queue to
clear before uninstalling the port.
void comm_port_flush_output(comm_port *port);
Flushes the input buffer.
void comm_port_flush_input(comm_port *port);
Flushes the input buffer.
void comm_port_delete(comm_port *port);
Having uninstalled your port you may decide that you don't need the
configuration data either and can better use the memory elsewhere.
Calling this function will free up the memory used to store and run
the port. However, please don't try to install the port after you've
called comm_port_delete.
int comm_port_out(comm_port *port, unsigned char c);
Puts 'c' on the software output buffer. The UART will then output the
contents of this buffer. c must not use more than the number of bits
specified configured for data. Returns 1 if the character is successfuly
placed on the buffer, 0 otherwise.
int comm_port_out_empty(comm_port *port);
Indicates if there is data waiting to be sent to the UART. Returns 0
if there is data waiting to be sent, -1 if the buffer is empty. Also
returns 0 on failure.
int comm_port_out_full(comm_port *port);
Indicates if the buffer of data waiting to be sent is now full. Returns -1
if this is the case and 0 if there is still room for more data on the
buffer. Also returns 0 on failure.
int comm_port_string_send(comm_port *port,char *s);
The string 's' is sent to the output buffer one character at a time; the
UART will transmit this down the serial line. Returns the number of characters
successfully placed on the output buffer.
int comm_port_command_send(comm_port *port,char *s);
The string 's' is sent to the output buffer one character at a time; the
UART will transmit this down the serial line. At the end of the string a
'\r' character is sent. Returns the number of characters successfully placed
on the output buffer, counting the '\r'. A successfuly send will therefore
return the length of the string given to it plus one.
int comm_port_in_empty(comm_port *port);
Indicates if data has come in on the serial line. It returns -1 if there is
no data (ie. the buffer is empty), 0 if there is data waiting to be read.
Also returns 0 on failure.
int comm_port_in_full(comm_port *port);
Indicates if the buffer of data which has come in on the serial line is now
full. It retunr -1 if this is the case (ie. the buffer is full) and 0
otherwise. Also return 0 on failure.
inline int comm_port_test(comm_port *port);
Checks to see data has come in on the data line. If there is none it returns
-1, like comm_port_in_empty. If there is some it returns the next character
from the queue of those received from the serial line (and takes it off the
queue).
void comm_port_send_break(comm_port *port, int msec_duration);
A break signal is issued on the serial line. The break signal will be at
least of the duration specified. It may be longer.
int comm_port_give_line_status(comm_port *port, dzcomm_line line);
Returns the value (1 or 0) of the register requested or -1 on failure. Note
that the value returned is the inverse of the physical state of the line
connected to the UART because the control lines on a UART are all low
when the register is high and vice versa.
The control line is defined using the following enum:
typedef enum {DZCOMM_DTR, DZCOMM_RTS, DZCOMM_CTS, DZCOMM_DSR} dzcomm_line;
int comm_port_set_line_status(comm_port *port, dzcomm_line line, int value);
Sets the value of the register requested and return 1 on success or -1 on
failure. Note that the physical state of the line connected to the UART
will be the inverse of the value set here because the control lines on
a UART are all low when the register is high and vice versa.
The control line is defined using the following enum:
typedef enum {DZCOMM_DTR, DZCOMM_RTS, DZCOMM_CTS, DZCOMM_DSR} dzcomm_line;
Be careful when using this function - if you are using CTS_RTS flow
control or if the UART at the other end is snesitive to it, you may
cause confusion ...
int modem_hangup(comm_port *port);
Sends "+++ATH0" to the line which tells it to hang up if it's a modem. Returns
one if the entire string (including the concluding '\r') is placed on the output
buffer, zero otherwise.
====================================
============ Queue code ============
====================================
Dzcomm comes with some FIFO queue code whcih is lso available for general use.
fifo_queue *queue_new(unsigned int size);
This function creates a queue of ints. If you need to store data values
of a different size use queue_new_ function instead of it. It returns
a pointer to a structure which serves as a handle for the queue, or
NULL on failure.
Example :
fifo_queue *q;
if ((q=queue_new(1024))==NULL) return(0);
fifo_queue *queue_new_(unsigned int size,unsigned int dsize);
Create a queue of 'size' elements each of size 'dsize'. It returns a
pointer to a structure which serves as a handle for the queue or NULL
on failure.
Example :
typedef struct {
int code;
int p1;
int p2;
} msg;
//...
fifo_queue *msg_q;
if ((msg_q=queue_new(1024,sizeof(msg)))==NULL) return(0);
void queue_delete(fifo_queue *q);
Delete queue description and free occupied space. Don't use the
queue after this ...
void queue_reset(fifo_queue *q);
Set head and tail of the queue to zero (data lost).
int queue_put(fifo_queue *q,int c);
Put value into given integer queue. On success returns
DZQ_NEARLY_FULL if the queue's getting a tad full,
DZQ_NOT_NEARLY_FULL otherwise. On failure returns
DZQ_FAIL_NO_Q if q was a NULL pointer or DZQ_FAIL_FULL
if the queue was actually full. Note that failure codes
are less than zero, success codes greater than or equal
to zero. If you need to store data of a different size
then use the queue_put_ function.
int queue_put_(fifo_queue *q,void *data)
Put value into given queue. On success returns
DZQ_NEARLY_FULL if the queue's getting a tad full,
DZQ_NOT_NEARLY_FULL otherwise. On failure returns
DZQ_FAIL_NO_Q if q was a NULL pointer or DZQ_FAIL_FULL
if the queue was actually full. Note that failure codes
are less than zero, success codes greater than or equal
to zero.
int queue_peek(fifo_queue *q);
See the integer value at the top of an integer queue. Returns the
value from the top of the queue but does not remove it from the
queue. If the queue is not of integers then use the queue_peek_
function. Use queue_empty before calling queue_get to verify that
there is data to peek at.
int queue_peek_(fifo_queue *q,void *data);
See the value at the top of the queue. Returns (in the memory
location pointed to by data) the value from the top of the queue
but does not remove it from the queue. The returned integer is
DZQ_NEARLY_FULL if the queue's getting a tad full,
DZQ_NOT_NEARLY_FULL otherwise. On failure returns
DZQ_FAIL_NO_Q if q was a NULL pointer or DZQ_FAIL_EMPTY
if the queue was actually empty. Note that failure codes
are less than zero, success codes greater than or equal
to zero. It's not a bad idea to use
queue_empty before calling queue_peek_ to verify that there is data
to get.
int queue_get(fifo_queue *q);
Get integer values from an integer queue. Returns the value from
the top of the queue. If the queue is not of integers then use the
queue_get_ function. Use queue_empty before calling queue_get to
verify that there is data to get.
int queue_get_(fifo_queue *q,void *data);
Get value from queue. Returns the value from the top of the queue
(in the memory location pointed to by data). The returned integer
is DZQ_NEARLY_FULL if the queue's getting a tad full,
DZQ_NOT_NEARLY_FULL otherwise. On failure returns
DZQ_FAIL_NO_Q if q was a NULL pointer or DZQ_FAIL_EMPTY
if the queue was actually empty. Note that failure codes
are less than zero, success codes greater than or equal
to zero. Use queue_empty before calling queue_get to verify that
there is data to get.
int queue_empty(fifo_queue *q);
Test queue for emptiness. Returns DZQ_EMPTY if the queue is empty,
DZQ_NOT_EMPTY if the queue is not full and DZQ_FAIL_INTERNAL on
failure.
int queue_nearly_full(fifo_queue *q);
Test to indicate if the queue is nearly full (ie over 75% at
present). Returns DZQ_NEARLY_FULL if the queue is nearly full
and DZQ_NOT_NEARLY_FULL otherwise.
int queue_full(fifo_queue *q);
Test to indicate if the queue is full. Returns DZQ_FULL if the
queue is full and DZQ_NOT_FULL otherwise.
====================================
============ Conclusion ============
====================================
Doubtless I've missed something or not been clear above. Feel free to
suggest how the documentation could be improved!
I hope you find some of this stuff useful.
By Neil Townsned
http://www.robots.ox.ac.uk/dzcomm
|