root/src/uart.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. dsp_uart_attr_init
  2. dsp_uart_attr_get_bytehandler
  3. dsp_uart_attr_set_bytehandler
  4. dsp_uart_create
  5. dsp_uart_destroy
  6. dsp_uart_bit_handler

   1 
   2 /*
   3  *      uart.c
   4  *
   5  *      Copyright (c) 2005 Robert Krten.  All Rights Reserved.
   6  *
   7  *      Redistribution and use in source and binary forms, with or without
   8  *      modification, are permitted provided that the following conditions
   9  *      are met:
  10  *
  11  *      1. Redistributions of source code must retain the above copyright
  12  *         notice, this list of conditions and the following disclaimer.
  13  *      2. Redistributions in binary form must reproduce the above copyright
  14  *         notice, this list of conditions and the following disclaimer in the
  15  *         documentation and/or other materials provided with the distribution.
  16  *
  17  *      THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
  18  *      ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19  *      IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20  *      ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
  21  *      FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22  *      DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23  *      OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24  *      HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25  *      LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26  *      OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27  *      SUCH DAMAGE.
  28  *
  29  *      This module contains a simple 8-bit UART, which performs a callback
  30  *      with the decoded byte value.
  31  *
  32  *      2005 06 11      R. Krten                created
  33 */
  34 
  35 #include <private/ftdm_core.h>
  36 #include <stdio.h>
  37 #include <stdlib.h>
  38 #include <string.h>
  39 #include <errno.h>
  40 
  41 #include "uart.h"
  42 
  43 /*
  44  *      dsp_uart_attr_init
  45  *
  46  *      Initializes the attributes structure; this must be done before the
  47  *      attributes structure is used.
  48 */
  49 
  50 void dsp_uart_attr_init (dsp_uart_attr_t *attr)
  51 {
  52         memset (attr, 0, sizeof (*attr));
  53 }
  54 
  55 /*
  56  *      dsp_uart_attr_get_bytehandler
  57  *      dsp_uart_attr_set_bytehandler
  58  *
  59  *      These functions get and set their respective elements from the
  60  *      attributes structure.  If an error code is returned, it is just
  61  *      zero == ok, -1 == fail.
  62 */
  63 
  64 bytehandler_func_t dsp_uart_attr_get_bytehandler(dsp_uart_attr_t *attr, void **bytehandler_arg)
  65 {
  66         *bytehandler_arg = attr->bytehandler_arg;
  67         return attr->bytehandler;
  68 }
  69 
  70 void dsp_uart_attr_set_bytehandler(dsp_uart_attr_t *attr, bytehandler_func_t bytehandler, void *bytehandler_arg)
  71 {
  72         attr->bytehandler = bytehandler;
  73         attr->bytehandler_arg = bytehandler_arg;
  74 }
  75 
  76 dsp_uart_handle_t *dsp_uart_create(dsp_uart_attr_t *attr)
  77 {
  78         dsp_uart_handle_t *handle;
  79 
  80         handle = ftdm_malloc(sizeof (*handle));
  81         if (handle) {
  82                 memset(handle, 0, sizeof (*handle));
  83 
  84                 /* fill the attributes member */
  85                 memcpy(&handle->attr, attr, sizeof (*attr));
  86         }
  87         return handle;
  88 }
  89 
  90 void dsp_uart_destroy(dsp_uart_handle_t **handle)
  91 {
  92         if (*handle) {
  93                 ftdm_safe_free(*handle);
  94                 *handle = NULL;
  95         }
  96 }
  97 
  98 
  99 void dsp_uart_bit_handler(void *x, int bit)
 100 {
 101         dsp_uart_handle_t *handle = (dsp_uart_handle_t *) x;
 102 
 103         if (!handle->have_start) {
 104                 if (bit) {
 105                         return;         /* waiting for start bit (0) */
 106                 }
 107                 handle->have_start = 1;
 108                 handle->data = 0;
 109                 handle->nbits = 0;
 110                 return;
 111         }
 112 
 113         handle->data >>= 1;
 114         handle->data |= 0x80 * !!bit;
 115         handle->nbits++;
 116 
 117         if (handle->nbits == 8) {
 118                 handle->attr.bytehandler(handle->attr.bytehandler_arg, handle->data);
 119                 handle->nbits = 0;
 120                 handle->data = 0;
 121                 handle->have_start = 0;
 122         }
 123 /* might consider handling errors in the future... */
 124 }
 125 

/* [<][>][^][v][top][bottom][index][help] */