% Copyright (c) 2023 SpinCore Technologies, Inc.
% http://www.spincore.com
% 
% This software is provided 'as-is', without any express or implied warranty. 
% In no event will the authors be held liable for any damages arising from the 
% use of this software.
% 
% Permission is granted to anyone to use this software for any purpose, 
% including commercial applications, and to alter it and redistribute it
% freely, subject to the following restrictions:
% 
% 1. The origin of this software must not be misrepresented; you must not
% claim that you wrote the original software. If you use this software in a
% product, an acknowledgment in the product documentation would be appreciated
% but is not required.
% 2. Altered source versions must be plainly marked as such, and must not be
% misrepresented as being the original software.
% 3. This notice may not be removed or altered from any source distribution.
%
% THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
% THE SOFTWARE.
%
clc; clear;

global SPINAPI_DLL_NAME

SPINAPI_DLL_NAME = 'spinapi64';

addpath('../MATLAB_SpinAPI'); % MATLAB_SpinAPI Folder path address

SpinAPI_Drive = 'C:'; % Modify to the hard drive letter that contains SpinAPI

% Loads spinapi64 library and headers
% All files are located in SpinAPI folder
if ~libisloaded('spinapi64')  % Needs the MinGW-w64 add-on
    loadlibrary([SpinAPI_Drive,'\SpinCore\SpinAPI\lib\spinapi64.dll'], ...
                 [SpinAPI_Drive,'\SpinCore\SpinAPI\include\spinapi.h'], 'addheader',...
                 [SpinAPI_Drive,'\SpinCore\SpinAPI\include\pulseblaster.h'], 'addheader',...
                 [SpinAPI_Drive,'\SpinCore\SpinAPI\include\radioprocessor.h'], 'addheader',...
                 [SpinAPI_Drive,'\SpinCore\SpinAPI\include\dds.h']);
end

% Load constants to the workspace as variables
load('CONST.mat');

CLOCK_FREQ = 75.0;

% User friendly names for the control bits
TX_ENABLE = 1;
TX_DISABLE = 0;
PHASE_RESET = 1;
NO_PHASE_RESET = 0;
DO_TRIGGER = 1;
NO_TRIGGER = 0;
USE_SHAPE = 1;
NO_SHAPE = 0;

shape_data = zeros(1024);	% Waveform for the pulse shape

% Uncommenting the line below will generate a debug log in your current
% directory that can help debug any problems that you may be experiencing   
% pb_set_debug(1);

fprintf('Copyright (c) 2023 SpinCore Technologies, Inc.\n\n');


if pb_init() ~= 0
  error('Error initializing board: %s\n', pb_get_error());
end

pb_select_board(0); 
pb_core_clock(CLOCK_FREQ);
pb_set_defaults();

% The DDS Shape table allows for 1024 envelope points which modulates the output
for i = 1:1024
	shape_data(i) = -1*(i/512.0) + 1.0;
end 

% Load the shape data on the board
pb_dds_load(shape_data, 'DEVICE_SHAPE');

fprintf('\n');
fprintf(['This program demonstrates the shaped pulse feature of the PBDDS-I-300.\n'...
	   'For a DC ramp from -2V to 2V when terminated with a 50 ohm load.\n\n']);

% Set amplitude register to full scale output 1
pb_set_amp(1, 0);

% Zero frequency is DC(always 1 when phase is 90.0)
pb_start_programming(FREQ_REGS);
pb_set_freq(0.0 * MHz);
pb_stop_programming();

% Set the TX phase to 90.0(start at sine wave peak)
pb_start_programming(TX_PHASE_REGS);
pb_set_phase(90.0);		
pb_stop_programming();

pb_start_programming(PULSE_PROGRAM);

% 20 us shaped pulse, with amplitude set by register 0. TTL outputs on
start = pb_inst_dds_shape(0, 0, TX_ENABLE, NO_PHASE_RESET, USE_SHAPE, 0, '1FF', CONTINUE, 0, 20.0 * ms);
pb_inst_dds_shape(0, 0, TX_DISABLE, PHASE_RESET, NO_SHAPE, 0, 0, BRANCH, start, 50.0 * ms);
  
pb_stop_programming();

pb_reset();
pb_start();

fprintf('Program has been triggered.\n');

fprintf('Press enter to continue...\n')
pause; 

% Release control of the board
pb_close();

% Unload library and free memory
unloadlibrary('spinapi64');