Wiselib
wiselib.testing/external_interface/contiki/contiki_byte_com_uart.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  ** This file is part of the generic algorithm library Wiselib.           **
00003  ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project.      **
00004  **                                                                       **
00005  ** The Wiselib is free software: you can redistribute it and/or modify   **
00006  ** it under the terms of the GNU Lesser General Public License as        **
00007  ** published by the Free Software Foundation, either version 3 of the    **
00008  ** License, or (at your option) any later version.                       **
00009  **                                                                       **
00010  ** The Wiselib is distributed in the hope that it will be useful,        **
00011  ** but WITHOUT ANY WARRANTY; without even the implied warranty of        **
00012  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         **
00013  ** GNU Lesser General Public License for more details.                   **
00014  **                                                                       **
00015  ** You should have received a copy of the GNU Lesser General Public      **
00016  ** License along with the Wiselib.                                       **
00017  ** If not, see <http://www.gnu.org/licenses/>.                           **
00018  ***************************************************************************/
00019 #ifndef CONNECTOR_CONTIKI_BYTE_UARTMODEL_H
00020 #define CONNECTOR_CONTIKI_BYTE_UARTMODEL_H
00021 
00022 #include "external_interface/contiki/contiki_types.h"
00023 #include "util/delegates/delegate.hpp"
00024 #include <stdio.h>
00025 #include <string.h>
00026 extern "C"
00027 {
00028    #include "contiki.h"
00029 #ifdef CONTIKI_TARGET_sky
00030    #include "dev/uart1.h"
00031 #endif
00032 }
00033 
00034 namespace wiselib
00035 {
00036    typedef delegate1<int, uint8_t> contiki_byte_uart_delegate_t;
00037    typedef contiki_byte_uart_delegate_t byte_uart_delegate_t;
00038    // -----------------------------------------------------------------------
00039    typedef int (*contiki_byte_uart_fp)( uint8_t );
00040    extern contiki_byte_uart_fp contiki_internal_byte_uart_callback;
00041    // -----------------------------------------------------------------------
00042    void init_contiki_byte_uart( void );
00043    int contiki_byte_uart_add_receiver( contiki_byte_uart_delegate_t& delegate );
00044    void contiki_byte_uart_del_receiver( int idx );
00045    int contiki_byte_uart_notify_receivers( uint8_t data );
00046    // -----------------------------------------------------------------------
00047    // -----------------------------------------------------------------------
00048    // -----------------------------------------------------------------------
00054    template<typename OsModel_P>
00055    class ContikiByteUartModel
00056    {
00057    public:
00058       typedef OsModel_P OsModel;
00059 
00060       typedef ContikiByteUartModel<OsModel> self_type;
00061       typedef self_type* self_pointer_t;
00062 
00063       typedef uint8_t block_data_t;
00064       typedef uint8_t size_t;
00065       // --------------------------------------------------------------------
00066       enum ErrorCodes
00067       {
00068          SUCCESS = OsModel::SUCCESS,
00069          ERR_UNSPEC = OsModel::ERR_UNSPEC
00070       };
00071       // --------------------------------------------------------------------
00072       void init()
00073       {
00074          init_contiki_byte_uart();
00075       }
00076       // --------------------------------------------------------------------
00077       int enable_serial_comm()
00078       {
00079          contiki_internal_byte_uart_callback = contiki_byte_uart_notify_receivers;
00080          return SUCCESS;
00081       }
00082       // --------------------------------------------------------------------
00083       int disable_serial_comm()
00084       {
00085          contiki_internal_byte_uart_callback = 0;
00086          return SUCCESS;
00087       }
00088       // --------------------------------------------------------------------
00089       int write( size_t len, block_data_t *buf )
00090       {
00091 #ifdef CONTIKI_TARGET_sky
00092          for ( int i = 0; i < len; i++ )
00093             uart1_writeb( buf[i] );
00094 #else
00095    #warning FIX THIS FOR CONTIKI MICAZ/MSB COMPILATION!
00096 #endif
00097 
00098          return SUCCESS;
00099       }
00100       // --------------------------------------------------------------------
00101       template<class T, void (T::*TMethod)(size_t, block_data_t*)>
00102       inline int reg_read_callback( T *obj_pnt );
00103       // --------------------------------------------------------------------
00104       int unreg_recv_callback( int idx )
00105       {
00106          contiki_byte_uart_del_receiver( idx );
00107          return SUCCESS;
00108       }
00109    };
00110    // --------------------------------------------------------------------
00111    // -----------------------------------------------------------------------
00112    // -----------------------------------------------------------------------
00113    template<typename OsModel_P>
00114    template<class T,
00115             void (T::*TMethod)( typename ContikiByteUartModel<OsModel_P>::size_t,
00116                                 typename ContikiByteUartModel<OsModel_P>::block_data_t*)>
00117    int
00118    ContikiByteUartModel<OsModel_P>::
00119    reg_read_callback( T *obj_pnt )
00120    {
00121       contiki_byte_uart_delegate_t delegate =
00122          contiki_byte_uart_delegate_t::from_method<T, TMethod>( obj_pnt );
00123       return contiki_byte_uart_add_receiver( delegate );
00124    }
00125 }
00126 
00127 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines