Modularizando o src

Dependencies:   EALib EthernetInterface_vz mbed-rtos mbed

Fork of header_main_colinas_V0-20-09-14 by VZTECH

Revision:
99:e80850c51106
Parent:
97:8985817e8847
Child:
100:09a23fcd3bdf
--- a/parallelcpld.cpp	Sun Jan 11 03:30:47 2015 +0000
+++ b/parallelcpld.cpp	Mon Jan 19 18:00:42 2015 +0000
@@ -21,6 +21,8 @@
 ring_buffer * rb = ring_buffer_init( NULL );
 ring_buffer test;
 
+Timer delay_to_send_to_cbx;
+
 // Print a variable using parallel protocol
 void parallel_write( uint8_t data ){   
     data0 = BIT0&(data);
@@ -35,48 +37,59 @@
 
 // Needs: function parallel_write/ Global variables: TXBuffer, tx_clear / Configuration extern interrupt
 void send2callboxes( uint8_t * buffer ){
-    // Send the first position of TXBuffer first
-    if( tx_clear == 1 ){
-        tx_clear = 0;
-        xmemcpy( TXBuffer, buffer, __CB_BUFFER_SIZE__ );
-        parallel_write( TXBuffer[ 0 ] );
-        DataReady = 1;
-        xmemcpy( cb_tx_buffer, buffer, __CB_BUFFER_SIZE__ );
+    static bool once = true;
+    if( once ){
+        delay_to_send_to_cbx.start();
+        once = false;
+    }
+    
+    if( delay_to_send_to_cbx.read_ms() < 7 ){
+        droped_pkg_to_cb++;
+        uint8_t ret = ring_buffer_add( rb, buffer );
         
-        if( tx || debug_invite ){
-            if( ( debug_invite ) && ( ( buffer[ 6 ] == __INVITE__ ) || ( buffer[ 6 ] == __CB_BYE__ ) ) ){
-                char str[ 128 ];
-                strcpy( str, "TX -> CB:: \n\r " );
-                for( register uint16_t i = 0; i < 32; i++ ){
-                    char tmp[ 16 ];
-                    strcat( str, itoa( cb_tx_buffer[ i ], tmp, 16 ) );
-                    
-                    if( ( i != 0 ) && !( ( i + 1 ) % 50 ) ) strcat( str, "\n\r " );
-                    
-                    else strcat( str, " " );
-                }
-                send_msg( "%s", str );
-            } else if( tx ){
-                char str[ 1024 ];
-                strcpy( str, "TX :: \n\r " );
-                for( register uint16_t i = 0; i < __CB_BUFFER_SIZE__; i++ ){
-                    char tmp[ 16 ];
-                    strcat( str, itoa( cb_tx_buffer[ i ], tmp, 16 ) );
-                    if( ( i != 0 ) && !( ( i + 1 ) % 50 ) ) strcat( str, "\n\r " );
-                    
-                    else strcat( str, " " );
-                }
-                send_msg( "%s", str );
-                tx = false;
+        if( ret == 0x01 ) if( dparallel ){
+            debug_msg("Error: Ring buffer fully charged");
+            if( debug_fw ){
+                uint16_t ext = ( ( uint16_t )buffer[ 0 ] ) << 8  | buffer[ 1 ];
+                uint16_t port = ( ( uint16_t )buffer[ 2 ] ) << 8  | buffer[ 3 ];
+                fw_cbx_pkg( ext, port, ( char *)buffer );
             }
         }
-    }else{
-        // Error if the transmission is still in use
-        uint8_t ret = ring_buffer_add( rb, buffer );
-        
-        if( ret == 0x01 ) if( dparallel ) debug_msg("Error: Ring buffer fully charged");
         
         if( ret == 0x00 ) if( dparallel ) debug_msg("Success : package queued -- on queue %u", rb->size );
+    } else {
+    
+        delay_to_send_to_cbx.reset();
+        
+        if( tx_clear == 1 ){
+            tx_clear = 0;
+            xmemcpy( TXBuffer, buffer, __CB_BUFFER_SIZE__ );
+            // Send the first position of TXBuffer first
+            parallel_write( TXBuffer[ 0 ] );
+            DataReady = 1;
+            xmemcpy( cb_tx_buffer, buffer, __CB_BUFFER_SIZE__ );
+            cpld_pkg_tx_counter++;
+            
+            uint16_t ext = ( ( uint16_t )buffer[ 0 ] ) << 8  | buffer[ 1 ];
+            uint16_t port = ( ( uint16_t )buffer[ 2 ] ) << 8  | buffer[ 3 ];
+            uint8_t type = buffer[ 6 ];
+            
+            if( debug_fw ){
+                buffer[ 0 ] |= BIT7;
+                fw_cbx_pkg( ext, port, ( char *)buffer );
+            }
+            
+            if( debug_cb_tx == true ){            
+                if( type != __AUDIO__ ) send_msg("Pkg to CBx :: ( %d, %d ) -- Type :: %d", ext, port, type );            
+            } 
+        }else{
+            // Error if the transmission is still in use
+            uint8_t ret = ring_buffer_add( rb, buffer );
+            
+            if( ret == 0x01 ) if( dparallel ) debug_msg("Error: Ring buffer fully charged");
+            
+            if( ret == 0x00 ) if( dparallel ) debug_msg("Success : package queued -- on queue %u", rb->size );
+        }
     }
 }
 
@@ -104,11 +117,11 @@
 }
 
 void tx_buffer_ring_buffer_handler( void ){
-    if( ( rb->size != 0x00 ) && ( tx_clear == 1 ) ){
-        uint8_t buffer[ __CB_BUFFER_SIZE__ ];
+    if( ( rb->size != 0x00 ) && ( tx_clear == 1 ) && ( delay_to_send_to_cbx.read_ms() > 7 ) ){        
+        //uint8_t buffer[ __CB_BUFFER_SIZE__ ];
         uint8_t * _rb_next = ring_buffer_get_next( rb );
-        xmemcpy( buffer, _rb_next, __CB_BUFFER_SIZE__ );
+        //xmemcpy( buffer, _rb_next, __CB_BUFFER_SIZE__ );
         if( dparallel ) send_msg( "Ring Buffer less one -- remain %u", rb->size );
-        send2callboxes( buffer );
+        send2callboxes( _rb_next );        
     }
 }
\ No newline at end of file