protocol.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * This file is part of the libsigrok project.
  3. *
  4. * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com>
  5. * Copyright (C) 2012 Renato Caldas <rmsc@fe.up.pt>
  6. * Copyright (C) 2013 Lior Elazary <lelazary@yahoo.com>
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef LIBSIGROK_HARDWARE_LINK_MSO19_PROTOCOL_H
  22. #define LIBSIGROK_HARDWARE_LINK_MSO19_PROTOCOL_H
  23. #include <stdint.h>
  24. #include <string.h>
  25. #include <glib.h>
  26. #include <libudev.h>
  27. #include "libsigrok.h"
  28. #include "libsigrok-internal.h"
  29. #define LOG_PREFIX "link-mso19"
  30. #define USB_VENDOR "3195"
  31. #define USB_PRODUCT "f190"
  32. #define NUM_CHANNELS (1 + 8)
  33. #define NUM_TRIGGER_STAGES 4
  34. #define TRIGGER_TYPE "01" //the first r/f is used for the whole group
  35. #define SERIALCOMM "460800/8n1/flow=2"
  36. #define SERIALCONN "/dev/ttyUSB0"
  37. #define CLOCK_RATE SR_MHZ(100)
  38. #define MIN_NUM_SAMPLES 4
  39. #define MSO_TRIGGER_UNKNOWN '!'
  40. #define MSO_TRIGGER_UNKNOWN1 '1'
  41. #define MSO_TRIGGER_UNKNOWN2 '2'
  42. #define MSO_TRIGGER_UNKNOWN3 '3'
  43. #define MSO_TRIGGER_WAIT '4'
  44. #define MSO_TRIGGER_FIRED '5'
  45. #define MSO_TRIGGER_DATAREADY '6'
  46. enum trigger_slopes {
  47. SLOPE_POSITIVE = 0,
  48. SLOPE_NEGATIVE,
  49. };
  50. /* Structure for the pattern generator state */
  51. struct mso_patgen {
  52. /* Pattern generator clock config */
  53. uint16_t clock;
  54. /* Buffer start address */
  55. uint16_t start;
  56. /* Buffer end address */
  57. uint16_t end;
  58. /* Pattern generator config */
  59. uint8_t config;
  60. /* Samples buffer */
  61. uint8_t buffer[1024];
  62. /* Input/output configuration for the samples buffer (?) */
  63. uint8_t io[1024];
  64. /* Number of loops for the pattern generator */
  65. uint8_t loops;
  66. /* Bit enable mask for the I/O lines */
  67. uint8_t mask;
  68. };
  69. /* Data structure for the protocol trigger state */
  70. struct mso_prototrig {
  71. /* Word match buffer */
  72. uint8_t word[4];
  73. /* Masks for the wordmatch buffer */
  74. uint8_t mask[4];
  75. /* SPI mode 0, 1, 2, 3. Set to 0 for I2C */
  76. uint8_t spimode;
  77. };
  78. /* Private, per-device-instance driver context. */
  79. struct dev_context {
  80. /* info */
  81. uint8_t hwmodel;
  82. uint8_t hwrev;
  83. struct sr_serial_dev_inst *serial;
  84. // uint8_t num_sample_rates;
  85. /* calibration */
  86. double vbit;
  87. uint16_t dac_offset;
  88. uint16_t offset_range;
  89. uint64_t limit_samples;
  90. uint64_t num_samples;
  91. /* register cache */
  92. uint8_t ctlbase1;
  93. uint8_t ctlbase2;
  94. /* state */
  95. uint8_t la_threshold;
  96. uint64_t cur_rate;
  97. uint8_t dso_probe_attn;
  98. int8_t use_trigger;
  99. uint8_t trigger_chan;
  100. uint8_t trigger_slope;
  101. uint8_t trigger_outsrc;
  102. uint8_t trigger_state;
  103. uint8_t trigger_holdoff[2];
  104. uint8_t la_trigger;
  105. uint8_t la_trigger_mask;
  106. double dso_trigger_voltage;
  107. uint16_t dso_trigger_width;
  108. struct mso_prototrig protocol_trigger;
  109. void *cb_data;
  110. uint16_t buffer_n;
  111. char buffer[4096];
  112. };
  113. SR_PRIV int mso_parse_serial(const char *iSerial, const char *iProduct,
  114. struct dev_context *ctx);
  115. SR_PRIV int mso_check_trigger(struct sr_serial_dev_inst *serial,
  116. uint8_t * info);
  117. SR_PRIV int mso_reset_adc(struct sr_dev_inst *sdi);
  118. SR_PRIV int mso_clkrate_out(struct sr_serial_dev_inst *serial, uint16_t val);
  119. SR_PRIV int mso_configure_rate(const struct sr_dev_inst *sdi, uint32_t rate);
  120. SR_PRIV int mso_receive_data(int fd, int revents, void *cb_data);
  121. SR_PRIV int mso_configure_trigger(const struct sr_dev_inst *sdi);
  122. SR_PRIV int mso_configure_threshold_level(const struct sr_dev_inst *sdi);
  123. SR_PRIV int mso_read_buffer(struct sr_dev_inst *sdi);
  124. SR_PRIV int mso_arm(const struct sr_dev_inst *sdi);
  125. SR_PRIV int mso_force_capture(struct sr_dev_inst *sdi);
  126. SR_PRIV int mso_dac_out(const struct sr_dev_inst *sdi, uint16_t val);
  127. SR_PRIV inline uint16_t mso_calc_raw_from_mv(struct dev_context *devc);
  128. SR_PRIV int mso_reset_fsm(struct sr_dev_inst *sdi);
  129. SR_PRIV int mso_toggle_led(struct sr_dev_inst *sdi, int state);
  130. SR_PRIV int mso_configure_channels(const struct sr_dev_inst *sdi);
  131. SR_PRIV void stop_acquisition(const struct sr_dev_inst *sdi);
  132. /* bank agnostic registers */
  133. #define REG_CTL2 15
  134. /* bank 0 registers */
  135. #define REG_BUFFER 1
  136. #define REG_TRIGGER 2
  137. #define REG_CLKRATE1 9
  138. #define REG_CLKRATE2 10
  139. #define REG_DAC1 12
  140. #define REG_DAC2 13
  141. /* possibly bank agnostic: */
  142. #define REG_CTL1 14
  143. /* bank 2 registers (SPI/I2C protocol trigger) */
  144. #define REG_PT_WORD(x) (x)
  145. #define REG_PT_MASK(x) (x + 4)
  146. #define REG_PT_SPIMODE 8
  147. /* bits - REG_CTL1 */
  148. #define BIT_CTL1_RESETFSM (1 << 0)
  149. #define BIT_CTL1_ARM (1 << 1)
  150. #define BIT_CTL1_ADC_UNKNOWN4 (1 << 4) /* adc enable? */
  151. #define BIT_CTL1_RESETADC (1 << 6)
  152. #define BIT_CTL1_LED (1 << 7)
  153. /* bits - REG_CTL2 */
  154. #define BITS_CTL2_BANK(x) (x & 0x3)
  155. #define BIT_CTL2_SLOWMODE (1 << 5)
  156. struct rate_map {
  157. uint32_t rate;
  158. uint16_t val;
  159. uint8_t slowmode;
  160. };
  161. static const struct rate_map rate_map[] = {
  162. { SR_MHZ(200), 0x0205, 0 },
  163. { SR_MHZ(100), 0x0105, 0 },
  164. { SR_MHZ(50), 0x0005, 0 },
  165. { SR_MHZ(20), 0x0303, 0 },
  166. { SR_MHZ(10), 0x0308, 0 },
  167. { SR_MHZ(5), 0x030c, 0 },
  168. { SR_MHZ(2), 0x0330, 0 },
  169. { SR_MHZ(1), 0x0362, 0 },
  170. { SR_KHZ(500), 0x03c6, 0 },
  171. { SR_KHZ(200), 0x07f2, 0 },
  172. { SR_KHZ(100), 0x0fe6, 0 },
  173. { SR_KHZ(50), 0x1fce, 0 },
  174. { SR_KHZ(20), 0x4f86, 0 },
  175. { SR_KHZ(10), 0x9f0e, 0 },
  176. { SR_KHZ(5), 0x03c7, 0x20 },
  177. { SR_KHZ(2), 0x07f3, 0x20 },
  178. { SR_KHZ(1), 0x0fe7, 0x20 },
  179. { SR_HZ(500), 0x1fcf, 0x20 },
  180. { SR_HZ(200), 0x4f87, 0x20 },
  181. { SR_HZ(100), 0x9f0f, 0x20 },
  182. };
  183. /* FIXME: Determine corresponding voltages */
  184. static const uint16_t la_threshold_map[] = {
  185. 0x8600,
  186. 0x8770,
  187. 0x88ff,
  188. 0x8c70,
  189. 0x8eff,
  190. 0x8fff,
  191. };
  192. #endif