#ifndef TRBNET_H
#define TRBNET_H

extern const char trbnet_version[];

#include <stdint.h>
#include <stdio.h>

extern unsigned int trb_debug;
extern unsigned int trb_dma;

#ifdef __cplusplus
extern "C" {
#endif

/* ---------------------------------------------------------------------- */

int trb_connect(const char* server);

int init_ports();

int trb_register_read(uint16_t trb_address,                             
                      uint16_t reg_address,                             
                      uint32_t* data,
                      unsigned int dsize);                                 
  
int trb_registertime_read(uint16_t trb_address,                             
                      uint16_t reg_address,                             
                      uint32_t* data,
                      unsigned int dsize); 
    
int trb_register_read_mem(uint16_t trb_address,                             
                          uint16_t reg_address,                             
                          uint8_t option,
                          uint16_t size,
                          uint32_t* data,
                          unsigned int dsize);
  
int trb_registertime_read_mem(uint16_t trb_address,
                              uint16_t reg_address,                       
                              uint8_t option,
                              uint16_t size,
                              uint32_t* data,
                              unsigned int dsize);                          
                        
int trb_register_write(uint16_t trb_address,                            
                       uint16_t reg_address,                            
                       uint32_t value);                                 
                                                                        
int trb_register_write_mem(uint16_t trb_address,                             
                           uint16_t reg_address,                             
                           uint8_t option,
                           const  uint32_t* data,
                           uint16_t size);

int trb_read_uid(uint16_t trb_address,                                  
                 uint32_t* data,
                 unsigned int dsize);                                 

int trb_set_address(uint64_t uid,
                    uint8_t endpoint,
                    uint16_t trb_address);


int trb_ipu_data_read(uint8_t type,
                      uint8_t trg_info,
                      uint8_t trg_random,
                      uint16_t trg_number,
                      uint32_t* data, 
                      unsigned int dsize);

int trb_send_trigger(uint8_t type,
                     uint32_t info,
                     uint8_t random,
                     uint16_t number);

int trb_send_trigger_rich(uint8_t input,
                          uint8_t type,
                          uint32_t info,
                          uint8_t random,
                          uint16_t number);

int fpga_register_read(uint16_t reg_address, uint32_t* value);

int fpga_register_write(uint16_t reg_address, uint32_t value);

int trb_fifo_flush(uint8_t channel);

int network_reset();

int com_reset();
  
int trb_register_modify(uint16_t trb_address,
                        uint16_t reg_address,
                        int mode,
                        uint32_t bitMask,
                        uint32_t bitValue);

/* ---------------------------------------------------------------------- */

/* This library provides several function to access the trbnet on a 
   Etrax-Board.
   
*/

/************************************************************************/
/* In case of any error the gloabl varianble 'trb_errno' will be set,   */
/* see trberror.h for details.                                          */
/************************************************************************/
                                                                        
                                                                        
/************************************************************************/
/* int trb_register_read(uint16_t trb_address,                             
                         uint16_t reg_address,                             
                         uint32_t* data,
                         unsigned int dsize);

   trb_address: TRB-Address of the TRB-Endpoint
   reg_address: Register-Address to be read from, broadcasts are supported
   data:        Pointer to a uint32_t Data-Buffer 
   dsize:       Size of the Data-Buffer in units of 32bit-words
   
   ReturnValue: == -1 on error, trberrno will be set  
                >=  0 number of 32bit-words which were stored in Data-Buffer
   
   TRB-Channel used: slow control (3)
   
   reads the register reg_address of a TRB-Endpoint with address 
   trb_address. The received data is stored in the Data-Buffer data.
   
   The format of the Data-Buffer is:
    first word:  TRB-Address of the sender
    second word: register value
   
   --> The size of the Data-Buffer must be at least >= 2
                                                                        
*/


/************************************************************************/
/* int trb_register_read_mem(uint16_t trb_address,                             
                             uint16_t reg_address,                             
                             uint8_t option,
                             uint16_t size,
                             uint32_t* data,
                             unsigned int dsize);

   trb_address:    TRB-Address of the TRB-Endpoint
   reg_address:    Register-Address to be read from, broadcasts are supported
   uint8_t option: 
   uint16_t size:  Number of 32Bit-words to be read 
   data:           Pointer to a uint32_t Data-Buffer 
   dsize:          Size of the Data-Buffer in units of 32bit-words
   
   ReturnValue: == -1 on error, trberrno will be set  
                >=  0 number of 32bit-words which were stored in Data-Buffer
   
   TRB-Channel used: slow control (3)
   
   reads the register reg_address of a TRB-Endpoint with address 
   trb_address. The received data is stored in the Data-Buffer data.
   
   The format of the Data-Buffer is:
    first word:  TRB-Address of the sender (Lower 2Bytes), len (Upper 2bytes)
    second word: register value
   
   --> The size of the Data-Buffer must be at least >= 2
                                                                        
*/


/************************************************************************/
/* int trb_register_write(uint16_t trb_address,                            
                          uint16_t reg_address,                            
                          uint32_t value);   

   trb_address: TRB-Address of the TRB-Endpoint, broadcasts are supported
   reg_address: Register-Address to be written to
   value:       32bit-word to be writen to the register
   
   ReturnValue: == -1 on error, trberrno will be set   
                
   TRB-Channel used: slow control (3)
   
   writes the value to the register reg_address of a TRB-Endpoint with address 
   trb_address.
*/

/************************************************************************/
/* int trb_read_uid(uint16_t trb_address,
                    uint32_t* data,
                    unsigned int dsize);  
                    
   trb_address: TRB-Address of the TRB-Endpoint, broadcasts are supported
   data:        Pointer to a uint32_t Data-Buffer 
   dsize:       Size of the Data-Buffer in units of 32bit-words

   ReturnValue: == -1 on error, trberrno will be set
                >=  0 number of 32bit-words which were stored in Data-Buffer

   TRB-Channel used: slow control (3)
           
   reads the Unique-Id of a TRB-Endpoint with address trb_address. The 
   received data is stored in the Data-Buffer data.
   
   The format of the Data-Buffer is:
    first word:  UID High-32Bit Word
    second word: UID Low-32Bit Word
    third word:  Endpoint Number
    fourth word: TRB-Address of the sender

    --> The size of the Data-Buffer must be at least >= 4

*/

/************************************************************************/
/* int trb_set_address(uint64_t uid,
                       uint8_t endpoint,
                       uint16_t trb_address);
                       
   uint64_t uid:           the UID of the Endpoint
   uint8_t endpoint:       Number of the TRBNet-Endpoint
   uint16_t trb_address:   the new TRB-Netaddress, broadcasts are not supported
   
   ReturnValue: == -1 on error, trberrno will be set
                ==  0 on success
   
   TRB-Channel used: slow control (3)
   
   sets a new TRB-Address trb_address of the give TRFNet-endpoint.
*/

/************************************************************************/
/* int trb_ipu_data_read(uint8_t type,
                         uint8_t trg_info,
                         uint8_t trg_random,
                         uint16_t trg_number,
                         uint32_t* data, 
                         unsigned int dsize);

    data:        Pointer to a uint32_t Data-Buffer 
    dsize:       Size of the Data-Buffer in units of 32bit-words
    
    ReturnValue: == -1 on error, trberrno will be set
                 >=  0 0 number of 32bit-words which were stored in Data-Buffer

    send a request to all TRBNet-Endpoints to readout the IPU-Data. The IPU 
    Datastream will be stored in the user DataBufer.
*/

/************************************************************************/
/* int trb_send_trigger(uint8_t type,
                       uint32_t info,
                       uint8_t random,
                       uint16_t number);
*/

#ifdef __cplusplus
}
#endif

#endif
