Subversion Repositories f9daq

Rev

Blame | Last modification | View Log | RSS feed

  1. /**
  2.  * $Id: fpga_osc.h 881 2013-12-16 05:37:34Z rp_jmenart $
  3.  *
  4.  * @brief Red Pitaya Oscilloscope FPGA controller.
  5.  *
  6.  * @Author Jure Menart <juremenart@gmail.com>
  7.  *
  8.  * (c) Red Pitaya  http://www.redpitaya.com
  9.  *
  10.  * This part of code is written in C programming language.
  11.  * Please visit http://en.wikipedia.org/wiki/C_(programming_language)
  12.  * for more details on the language used herein.
  13.  */
  14.  
  15. #ifndef __FPGA_OSC_H
  16. #define __FPGA_OSC_H
  17.  
  18. #include <stdint.h>
  19.  
  20. /** @defgroup fpga_osc_h fpga_osc_h
  21.  * @{
  22.  */
  23.  
  24. /** Base OSC FPGA address */
  25. #define OSC_FPGA_BASE_ADDR 0x40100000
  26. /** Base OSC FPGA core size */
  27. #define OSC_FPGA_BASE_SIZE 0x30000
  28. /** OSC FPGA input signal buffer length */
  29. #define OSC_FPGA_SIG_LEN   (16*1024)
  30. /** OSC FPGA ARM bit in configuration register */
  31. #define OSC_FPGA_CONF_ARM_BIT  1
  32. /** OSC FPGA reset bit in configuration register */
  33. #define OSC_FPGA_CONF_RST_BIT  2
  34.  
  35. /** OSC FPGA trigger source register mask */
  36. #define OSC_FPGA_TRIG_SRC_MASK 0x00000007
  37. /** OSC FPGA Channel A threshold register mask */
  38. #define OSC_FPGA_CHA_THR_MASK  0x00003fff
  39. /** OSC FPGA Channel B threshold register mask */
  40. #define OSC_FPGA_CHB_THR_MASK  0x00003fff
  41. /** OSC FPGA trigger delay register register mask */
  42. #define OSC_FPGA_TRIG_DLY_MASK 0xffffffff
  43. /** OSC FPGA data decimation mask */
  44. #define OSC_FPGA_DATA_DEC_MASK 0x0001ffff
  45.  
  46. /** OSC FPGA Channel A input signal buffer offset */
  47. #define OSC_FPGA_CHA_OFFSET    0x10000
  48. /** OSC FPGA Channel B input signal buffer offset */
  49. #define OSC_FPGA_CHB_OFFSET    0x20000
  50.  
  51. /** @brief OSC FPGA registry structure.
  52.  *
  53.  * This structure is direct image of physical FPGA memory. When accessing it all
  54.  * reads/writes are performed directly from/to FPGA OSC core.
  55.  */
  56. typedef struct osc_fpga_reg_mem_s {
  57.     /** @brief Offset 0x00 - configuration register
  58.      *
  59.      * Configuration register (offset 0x00):
  60.      * bit     [0] - arm_trigger
  61.      * bit     [1] - rst_wr_state_machine
  62.      * bits [31:2] - reserved
  63.      */
  64.     uint32_t conf;
  65.  
  66.     /** @brief Offset 0x04 - trigger source register
  67.      *
  68.      * Trigger source register (offset 0x04):
  69.      * bits [ 2 : 0] - trigger source:
  70.      *     1 - trig immediately
  71.      *     2 - ChA positive edge
  72.      *     3 - ChA negative edge
  73.      *     4 - ChB positive edge
  74.      *     5 - ChB negative edge
  75.      *     6 - External trigger 0
  76.      *     7 - External trigger 1
  77.      * bits [31 : 3] -reserved
  78.      */
  79.     uint32_t trig_source;
  80.  
  81.     /** @brief Offset 0x08 - Channel A threshold register
  82.      *
  83.      * Channel A threshold register (offset 0x08):
  84.      * bits [13: 0] - ChA threshold
  85.      * bits [31:14] - reserved
  86.      */
  87.     uint32_t cha_thr;
  88.  
  89.     /** @brief Offset 0x0C - Channel B threshold register
  90.      *
  91.      * Channel B threshold register (offset 0x0C):
  92.      * bits [13: 0] - ChB threshold
  93.      * bits [31:14] - reserved
  94.      */
  95.     uint32_t chb_thr;
  96.  
  97.     /** @brief Offset 0x10 - After trigger delay register
  98.      *
  99.      * After trigger delay register (offset 0x10)
  100.      * bits [31: 0] - trigger delay
  101.      * 32 bit number - how many decimated samples should be stored into a buffer.
  102.      * (max 16k samples)
  103.      */
  104.     uint32_t trigger_delay;
  105.  
  106.     /** @brief Offset 0x14 - Data decimation register
  107.      *
  108.      * Data decimation register (offset 0x14):
  109.      * bits [16: 0] - decimation factor, legal values:
  110.      *   1, 8, 64, 1024, 8192 65536
  111.      *   If other values are written data is undefined
  112.      * bits [31:17] - reserved
  113.      */
  114.     uint32_t data_dec;
  115.  
  116.     /** @brief Offset 0x18 - Current write pointer register
  117.      *
  118.      * Current write pointer register (offset 0x18), read only:
  119.      * bits [13: 0] - current write pointer
  120.      * bits [31:14] - reserved
  121.      */
  122.     uint32_t wr_ptr_cur;
  123.     /** @brief Offset 0x1C - Trigger write pointer register
  124.      *
  125.      * Trigger write pointer register (offset 0x1C), read only:
  126.      * bits [13: 0] - trigger pointer (pointer where trigger was detected)
  127.      * bits [31:14] - reserved
  128.      */
  129.     uint32_t wr_ptr_trigger;
  130.    
  131.     /** @brief  ChA & ChB hysteresis - both of the format:
  132.      * bits [13: 0] - hysteresis threshold
  133.      * bits [31:14] - reserved
  134.      */
  135.     uint32_t cha_hystersis;
  136.     uint32_t chb_hystersis;
  137.  
  138.     /** @brief
  139.      * bits [0] - enable signal average at decimation
  140.      * bits [31:1] - reserved
  141.      */
  142.     uint32_t other;
  143.    
  144.     uint32_t reseved;
  145.    
  146.     /** @brief ChA Equalization filter
  147.      * bits [17:0] - AA coefficient (pole)
  148.      * bits [31:18] - reserved
  149.      */
  150.     uint32_t cha_filt_aa;    
  151.    
  152.     /** @brief ChA Equalization filter
  153.      * bits [24:0] - BB coefficient (zero)
  154.      * bits [31:25] - reserved
  155.      */
  156.     uint32_t cha_filt_bb;    
  157.    
  158.     /** @brief ChA Equalization filter
  159.      * bits [24:0] - KK coefficient (gain)
  160.      * bits [31:25] - reserved
  161.      */
  162.     uint32_t cha_filt_kk;  
  163.    
  164.     /** @brief ChA Equalization filter
  165.      * bits [24:0] - PP coefficient (pole)
  166.      * bits [31:25] - reserved
  167.      */
  168.     uint32_t cha_filt_pp;    
  169.    
  170.    
  171.    
  172.  
  173.     /** @brief ChB Equalization filter
  174.      * bits [17:0] - AA coefficient (pole)
  175.      * bits [31:18] - reserved
  176.      */
  177.     uint32_t chb_filt_aa;    
  178.    
  179.     /** @brief ChB Equalization filter
  180.      * bits [24:0] - BB coefficient (zero)
  181.      * bits [31:25] - reserved
  182.      */
  183.     uint32_t chb_filt_bb;    
  184.    
  185.     /** @brief ChB Equalization filter
  186.      * bits [24:0] - KK coefficient (gain)
  187.      * bits [31:25] - reserved
  188.      */
  189.     uint32_t chb_filt_kk;  
  190.    
  191.     /** @brief ChB Equalization filter
  192.      * bits [24:0] - PP coefficient (pole)
  193.      * bits [31:25] - reserved
  194.      */
  195.     uint32_t chb_filt_pp;                
  196.    
  197.     /* ChA & ChB data - 14 LSB bits valid starts from 0x10000 and
  198.      * 0x20000 and are each 16k samples long */
  199. } osc_fpga_reg_mem_t;
  200.  
  201. /** @} */
  202.  
  203. // TODO: Move to a shared folder and share with scope & spectrum.
  204. /** Equalization & shaping filter coefficients */
  205. typedef struct {
  206.     uint32_t aa;
  207.     uint32_t bb;
  208.     uint32_t pp;
  209.     uint32_t kk;
  210. } ecu_shape_filter_t;
  211.  
  212. int osc_fpga_init(void);
  213. int osc_fpga_exit(void);
  214.  
  215. void get_equ_shape_filter(ecu_shape_filter_t *filt, uint32_t equal,
  216.                           uint32_t shaping, uint32_t gain);
  217. int osc_fpga_update_params(int trig_imm, int trig_source, int trig_edge,
  218.                            float trig_delay, float trig_level, int time_range,
  219.                            int equal, int shaping, int gain1, int gain2);
  220. int osc_fpga_reset(void);
  221. int osc_fpga_arm_trigger(void);
  222. int osc_fpga_set_trigger(uint32_t trig_source);
  223. int osc_fpga_set_trigger_delay(uint32_t trig_delay);
  224.  
  225. /* Returns 0 if no trigger, 1 if trigger */
  226. int osc_fpga_triggered(void);
  227.  
  228. /* Returns pointer to the ChA and ChB signals (of length OSC_FPGA_SIG_LEN) */
  229. int osc_fpga_get_sig_ptr(int **cha_signal, int **chb_signal);
  230.  
  231. /* Returns signal pointers from the FPGA */
  232. int osc_fpga_get_wr_ptr(int *wr_ptr_curr, int *wr_ptr_trig);
  233.  
  234. /* Returnes signal content */
  235. /* various constants */
  236. extern const float c_osc_fpga_smpl_freq;
  237. extern const float c_osc_fpga_smpl_period;
  238.  
  239. /* helper conversion functions */
  240. /* Convert correct value for FPGA trigger source from trig_immediately,
  241.  * trig_source and trig_edge from application params.
  242.  */
  243. int osc_fpga_cnv_trig_source(int trig_imm, int trig_source, int trig_edge);
  244. /* Converts time_range parameter (0-5) to decimation factor */
  245. int osc_fpga_cnv_time_range_to_dec(int time_range);
  246. /* Converts time in [s] to ADC samples (depends on decimation) */
  247. int osc_fpga_cnv_time_to_smpls(float time, int dec_factor);
  248. /* Converts voltage in [V] to ADC counts */
  249. int osc_fpga_cnv_v_to_cnt(float voltage);
  250. /* Converts ADC ounts to [V] */
  251. float osc_fpga_cnv_cnt_to_v(int cnts);
  252.  
  253. /* Debug - dump to stderr current parameter settings (leave out data) */
  254. void osc_fpga_dump_regs(void);
  255.  
  256. /* debugging - will be removed */
  257. extern osc_fpga_reg_mem_t *g_osc_fpga_reg_mem;
  258. extern                int  g_osc_fpga_mem_fd;
  259. int __osc_fpga_cleanup_mem(void);
  260.  
  261. #endif /* __FPGA_OSC_H*/
  262.