Wiselib
|
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_ISENSE_SERIAL_COM_UART0_H 00020 #define CONNECTOR_ISENSE_SERIAL_COM_UART0_H 00021 00022 #include "external_interface/isense/isense_types.h" 00023 #include "util/delegates/delegate.hpp" 00024 #include "util/base_classes/uart_base.h" 00025 #include <isense/os.h> 00026 #include <isense/uart.h> 00027 #include <isense/task.h> 00028 00029 namespace wiselib 00030 { 00031 00042 template<typename OsModel_P, 00043 int MESSAGE_TYPE_IN = isense::Uart::MESSAGE_TYPE_CUSTOM_IN_1, 00044 int MESSAGE_TYPE_OUT = isense::Uart::MESSAGE_TYPE_PLOT, 00045 int UART = 0> 00046 class iSenseSerialComUartModel 00047 : public isense::UartPacketHandler, 00048 public isense::Task, 00049 public UartBase<OsModel_P, uint8, uint8> 00050 { 00051 public: 00052 typedef OsModel_P OsModel; 00053 00054 typedef iSenseSerialComUartModel<OsModel, MESSAGE_TYPE_IN, MESSAGE_TYPE_OUT, UART> self_type; 00055 typedef self_type* self_pointer_t; 00056 00057 typedef uint8 block_data_t; 00058 typedef uint8 size_t; 00059 #ifdef ISENSE_ENABLE_UART_16BIT 00060 typedef uint16 uart_packet_length_t; 00061 #else 00062 typedef uint8 uart_packet_length_t; 00063 #endif 00064 // -------------------------------------------------------------------- 00065 enum ErrorCodes 00066 { 00067 SUCCESS = OsModel::SUCCESS, 00068 ERR_UNSPEC = OsModel::ERR_UNSPEC 00069 }; 00070 // -------------------------------------------------------------------- 00071 iSenseSerialComUartModel( isense::Os& os ) 00072 : os_ ( os ), 00073 packet_length_ ( 0 ), 00074 wait_for_delivery_( false ), 00075 errors_ ( 0 ) 00076 {} 00077 // ----------------------------------------------------------------------- 00078 int enable_serial_comm() 00079 { 00080 os().uart(UART).enable(); 00081 os().uart(UART).set_packet_handler( MESSAGE_TYPE_IN, this ); 00082 os().uart(UART).enable_interrupt( true ); 00083 00084 return SUCCESS; 00085 } 00086 // ----------------------------------------------------------------------- 00087 int disable_serial_comm() 00088 { 00089 os().uart(UART).disable(); 00090 return SUCCESS; 00091 } 00092 // ----------------------------------------------------------------------- 00093 int write( size_t len, block_data_t *buf ) 00094 { 00095 //os().debug( "got %s", buf ); 00096 os().uart(UART).write_packet( MESSAGE_TYPE_OUT, (char*)buf, len ); 00097 00098 return SUCCESS; 00099 } 00100 // ----------------------------------------------------------------- 00101 void handle_uart_packet( uint8 type, uint8* buf, uart_packet_length_t length ) 00102 { 00103 if ( wait_for_delivery_ ) 00104 { 00105 errors_++; 00106 return; 00107 } 00108 00109 memcpy( packet_buffer_, buf, length ); 00110 packet_length_ = length; 00111 00112 wait_for_delivery_ = true; 00113 os().add_task( this, 0 ); 00114 } 00115 // ----------------------------------------------------------------- 00116 virtual void execute( void* userdata ) 00117 { 00118 self_type::notify_receivers( packet_length_, packet_buffer_ ); 00119 wait_for_delivery_ = false; 00120 } 00121 00122 private: 00123 isense::Os& os() 00124 { return os_; } 00125 // -------------------------------------------------------------------- 00126 isense::Os& os_; 00127 00128 uint8 packet_buffer_[512]; 00129 uart_packet_length_t packet_length_; 00130 volatile bool wait_for_delivery_; 00131 volatile int errors_; 00132 }; 00133 } 00134 00135 #endif