{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qick import *\n",
    "from qick.rfboard import *\n",
    "\n",
    "import numpy as np\n",
    "from numpy.fft import fft, fftshift\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import scipy.io"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "\n",
       "try {\n",
       "require(['notebook/js/codecell'], function(codecell) {\n",
       "  codecell.CodeCell.options_default.highlight_modes[\n",
       "      'magic_text/x-csrc'] = {'reg':[/^%%microblaze/]};\n",
       "  Jupyter.notebook.events.one('kernel_ready.Kernel', function(){\n",
       "      Jupyter.notebook.get_cells().map(function(cell){\n",
       "          if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;\n",
       "  });\n",
       "});\n",
       "} catch (e) {};\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "\n",
       "try {\n",
       "require(['notebook/js/codecell'], function(codecell) {\n",
       "  codecell.CodeCell.options_default.highlight_modes[\n",
       "      'magic_text/x-csrc'] = {'reg':[/^%%pybind11/]};\n",
       "  Jupyter.notebook.events.one('kernel_ready.Kernel', function(){\n",
       "      Jupyter.notebook.get_cells().map(function(cell){\n",
       "          if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;\n",
       "  });\n",
       "});\n",
       "} catch (e) {};\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "QICK configuration:\n",
      "\n",
      "\tBoard: ZCU216\n",
      "\n",
      "\tSoftware version: 0.2.226\n",
      "\tFirmware timestamp: Fri Dec 15 13:31:32 2023\n",
      "\n",
      "\tGlobal clocks (MHz): tProcessor 349.997, RF reference 245.760\n",
      "\n",
      "\t16 signal generator channels:\n",
      "\t0:\taxis_sg_mux4_v1 - envelope memory 0 samples (0.000 us)\n",
      "\t\tfs=4915.200 MHz, fabric=307.200 MHz, 16-bit DDS, range=1228.800 MHz\n",
      "\t\tDAC tile 0, blk 0 is 0_228, on JHC1\n",
      "\t1:\taxis_sg_int4_v1 - envelope memory 4096 samples (13.333 us)\n",
      "\t\tfs=4915.200 MHz, fabric=307.200 MHz, 16-bit DDS, range=1228.800 MHz\n",
      "\t\tDAC tile 0, blk 1 is 1_228, on JHC2\n",
      "\t2:\taxis_sg_int4_v1 - envelope memory 4096 samples (13.333 us)\n",
      "\t\tfs=4915.200 MHz, fabric=307.200 MHz, 16-bit DDS, range=1228.800 MHz\n",
      "\t\tDAC tile 0, blk 2 is 2_228, on JHC1\n",
      "\t3:\taxis_sg_int4_v1 - envelope memory 4096 samples (13.333 us)\n",
      "\t\tfs=4915.200 MHz, fabric=307.200 MHz, 16-bit DDS, range=1228.800 MHz\n",
      "\t\tDAC tile 0, blk 3 is 3_228, on JHC2\n",
      "\t4:\taxis_sg_int4_v1 - envelope memory 4096 samples (13.333 us)\n",
      "\t\tfs=4915.200 MHz, fabric=307.200 MHz, 16-bit DDS, range=1228.800 MHz\n",
      "\t\tDAC tile 1, blk 0 is 0_229, on JHC1\n",
      "\t5:\taxis_sg_int4_v1 - envelope memory 4096 samples (13.333 us)\n",
      "\t\tfs=4915.200 MHz, fabric=307.200 MHz, 16-bit DDS, range=1228.800 MHz\n",
      "\t\tDAC tile 1, blk 1 is 1_229, on JHC2\n",
      "\t6:\taxis_sg_int4_v1 - envelope memory 4096 samples (13.333 us)\n",
      "\t\tfs=4915.200 MHz, fabric=307.200 MHz, 16-bit DDS, range=1228.800 MHz\n",
      "\t\tDAC tile 1, blk 2 is 2_229, on JHC1\n",
      "\t7:\taxis_sg_int4_v1 - envelope memory 4096 samples (13.333 us)\n",
      "\t\tfs=4915.200 MHz, fabric=307.200 MHz, 16-bit DDS, range=1228.800 MHz\n",
      "\t\tDAC tile 1, blk 3 is 3_229, on JHC2\n",
      "\t8:\taxis_signal_gen_v6 - envelope memory 16384 samples (1.667 us)\n",
      "\t\tfs=9830.400 MHz, fabric=614.400 MHz, 32-bit DDS, range=9830.400 MHz\n",
      "\t\tDAC tile 2, blk 0 is 0_230, on JHC3\n",
      "\t9:\taxis_signal_gen_v6 - envelope memory 16384 samples (1.667 us)\n",
      "\t\tfs=9830.400 MHz, fabric=614.400 MHz, 32-bit DDS, range=9830.400 MHz\n",
      "\t\tDAC tile 2, blk 1 is 1_230, on JHC4\n",
      "\t10:\taxis_signal_gen_v6 - envelope memory 16384 samples (1.667 us)\n",
      "\t\tfs=9830.400 MHz, fabric=614.400 MHz, 32-bit DDS, range=9830.400 MHz\n",
      "\t\tDAC tile 2, blk 2 is 2_230, on JHC3\n",
      "\t11:\taxis_signal_gen_v6 - envelope memory 16384 samples (1.667 us)\n",
      "\t\tfs=9830.400 MHz, fabric=614.400 MHz, 32-bit DDS, range=9830.400 MHz\n",
      "\t\tDAC tile 2, blk 3 is 3_230, on JHC4\n",
      "\t12:\taxis_signal_gen_v6 - envelope memory 16384 samples (1.667 us)\n",
      "\t\tfs=9830.400 MHz, fabric=614.400 MHz, 32-bit DDS, range=9830.400 MHz\n",
      "\t\tDAC tile 3, blk 0 is 0_231, on JHC3\n",
      "\t13:\taxis_signal_gen_v6 - envelope memory 16384 samples (1.667 us)\n",
      "\t\tfs=9830.400 MHz, fabric=614.400 MHz, 32-bit DDS, range=9830.400 MHz\n",
      "\t\tDAC tile 3, blk 1 is 1_231, on JHC4\n",
      "\t14:\taxis_signal_gen_v6 - envelope memory 16384 samples (1.667 us)\n",
      "\t\tfs=9830.400 MHz, fabric=614.400 MHz, 32-bit DDS, range=9830.400 MHz\n",
      "\t\tDAC tile 3, blk 2 is 2_231, on JHC3\n",
      "\t15:\taxis_signal_gen_v6 - envelope memory 16384 samples (1.667 us)\n",
      "\t\tfs=9830.400 MHz, fabric=614.400 MHz, 32-bit DDS, range=9830.400 MHz\n",
      "\t\tDAC tile 3, blk 3 is 3_231, on JHC4\n",
      "\n",
      "\t8 readout channels:\n",
      "\t0:\taxis_pfb_readout_v2 - controlled by PYNQ\n",
      "\t\tfs=2457.600 MHz, fabric=307.200 MHz, 32-bit DDS, range=307.200 MHz\n",
      "\t\tmaxlen 16384 accumulated, 16384 decimated (53.333 us)\n",
      "\t\ttriggered by output 7, pin 4, feedback to tProc input 0\n",
      "\t\tADC tile 2, blk 0 is 0_226, on JHC7\n",
      "\t1:\taxis_pfb_readout_v2 - controlled by PYNQ\n",
      "\t\tfs=2457.600 MHz, fabric=307.200 MHz, 32-bit DDS, range=307.200 MHz\n",
      "\t\tmaxlen 16384 accumulated, 16384 decimated (53.333 us)\n",
      "\t\ttriggered by output 7, pin 5, feedback to tProc input 1\n",
      "\t\tADC tile 2, blk 0 is 0_226, on JHC7\n",
      "\t2:\taxis_pfb_readout_v2 - controlled by PYNQ\n",
      "\t\tfs=2457.600 MHz, fabric=307.200 MHz, 32-bit DDS, range=307.200 MHz\n",
      "\t\tmaxlen 16384 accumulated, 16384 decimated (53.333 us)\n",
      "\t\ttriggered by output 7, pin 6, feedback to tProc input 2\n",
      "\t\tADC tile 2, blk 0 is 0_226, on JHC7\n",
      "\t3:\taxis_pfb_readout_v2 - controlled by PYNQ\n",
      "\t\tfs=2457.600 MHz, fabric=307.200 MHz, 32-bit DDS, range=307.200 MHz\n",
      "\t\tmaxlen 16384 accumulated, 16384 decimated (53.333 us)\n",
      "\t\ttriggered by output 7, pin 7, feedback to tProc input 3\n",
      "\t\tADC tile 2, blk 0 is 0_226, on JHC7\n",
      "\t4:\taxis_readout_v2 - controlled by PYNQ\n",
      "\t\tfs=2457.600 MHz, fabric=307.200 MHz, 32-bit DDS, range=2457.600 MHz\n",
      "\t\tmaxlen 16384 accumulated, 16384 decimated (53.333 us)\n",
      "\t\ttriggered by output 7, pin 8, feedback to tProc input -1\n",
      "\t\tADC tile 1, blk 0 is 0_225, on JHC5\n",
      "\t5:\taxis_readout_v2 - controlled by PYNQ\n",
      "\t\tfs=2457.600 MHz, fabric=307.200 MHz, 32-bit DDS, range=2457.600 MHz\n",
      "\t\tmaxlen 16384 accumulated, 16384 decimated (53.333 us)\n",
      "\t\ttriggered by output 7, pin 9, feedback to tProc input -1\n",
      "\t\tADC tile 1, blk 1 is 1_225, on JHC6\n",
      "\t6:\taxis_readout_v2 - controlled by PYNQ\n",
      "\t\tfs=2457.600 MHz, fabric=307.200 MHz, 32-bit DDS, range=2457.600 MHz\n",
      "\t\tmaxlen 16384 accumulated, 16384 decimated (53.333 us)\n",
      "\t\ttriggered by output 7, pin 10, feedback to tProc input -1\n",
      "\t\tADC tile 1, blk 2 is 2_225, on JHC5\n",
      "\t7:\taxis_readout_v2 - controlled by PYNQ\n",
      "\t\tfs=2457.600 MHz, fabric=307.200 MHz, 32-bit DDS, range=2457.600 MHz\n",
      "\t\tmaxlen 16384 accumulated, 16384 decimated (53.333 us)\n",
      "\t\ttriggered by output 7, pin 11, feedback to tProc input -1\n",
      "\t\tADC tile 1, blk 3 is 3_225, on JHC6\n",
      "\n",
      "\t4 digital output pins:\n",
      "\t0:\tSPARE0_1V8\n",
      "\t1:\tSPARE1_1V8\n",
      "\t2:\tSPARE2_1V8\n",
      "\t3:\tSPARE3_1V8\n",
      "\n",
      "\ttProc axis_tproc64x32_x8: program memory 1024 words, data memory 1024 words\n",
      "\t\texternal start pin: None\n"
     ]
    }
   ],
   "source": [
    "# Load bitstream with custom overlay\n",
    "soc = RFQickSoc216V1('./qick_rf216_v1.bit', clk_output=None)\n",
    "soccfg = soc\n",
    "\n",
    "print(soccfg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "##################################################\n",
    "### Long duration signal for spectrum analysis ###\n",
    "##################################################\n",
    "class MuxSGTest(AveragerProgram):\n",
    "    def initialize(self):\n",
    "        # Set the nyquist zone\n",
    "        self.declare_gen(ch=self.cfg[\"gen_ch\"], nqz=1,\n",
    "                         mixer_freq = self.cfg['mixer_freq'],\n",
    "                         mux_freqs = self.cfg['pulse_freqs'])        \n",
    "        \n",
    "        # Pulse registers.\n",
    "        length = self.us2cycles(self.cfg['pulse_length'], gen_ch=self.cfg['gen_ch'])\n",
    "        self.set_pulse_registers(ch = self.cfg['gen_ch'], style = 'const', length=length, mask=[0,1,2,3])\n",
    "        \n",
    "        self.synci(200)\n",
    "    \n",
    "    def body(self):\n",
    "        # Trigger.\n",
    "        self.trigger(pins=[0],t=40)\n",
    "\n",
    "        # Pulses.\n",
    "        self.pulse(ch=self.cfg['gen_ch'])\n",
    "       \n",
    "config={\"gen_ch\"      : 0,\n",
    "        \"reps\"        : 1,\n",
    "        \"mixer_freq\"  : 1400,\n",
    "        \"pulse_length\": 10000000, # 10 s\n",
    "        \"pulse_freqs\" : [60,250,400,500],\n",
    "        \"period\"      : 2,\n",
    "        \"phrst\"       : 1,\n",
    "       }\n",
    "\n",
    "prog = MuxSGTest(soccfg, config)\n",
    "prog.config_all(soccfg)\n",
    "\n",
    "########################\n",
    "### RF Board Setting ###\n",
    "########################\n",
    "nqz = 1\n",
    "\n",
    "# Set Nyquist Zone.\n",
    "dacid = soc['gens'][config['gen_ch']]['dac']\n",
    "soc.rf.set_nyquist(dacid, nqz=nqz)\n",
    "\n",
    "# Set Filter.\n",
    "# soc['rfboard']['dacs'][config['gen_ch']].set_filter(fc=7, ftype='highpass')\n",
    "soc.rfb_set_gen_filter(gen_ch=config['gen_ch'], fc=7, ftype='highpass')\n",
    "\n",
    "# Set attenuator on DAC.\n",
    "# soc['rfboard']['dacs'][config['gen_ch']].set_attn_db(attn=0, db=0)\n",
    "# soc['rfboard']['dacs'][config['gen_ch']].set_attn_db(attn=1, db=10)\n",
    "soc.rfb_set_gen_rf(gen_ch=config['gen_ch'], att1=0, att2=10)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "soc.tproc.start()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<qick.drivers.generator.AxisSgMux4V1 at 0xffff5ef295a0>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "soc.gens[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
