fmc130m_4ch_ctl.c 8.41 KB
Newer Older
1
/*
2 3
 * Controlling the FMC 130 MSPS board
 */
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <bpm_client.h>

#define DFLT_BIND_FOLDER            "/tmp/bpm"

#define DFLT_BPM_NUMBER                     0
#define MAX_BPM_NUMBER                      1

#define DFLT_BOARD_NUMBER                   0

void print_help (char *program_name)
{
    printf( "Usage: %s [options]\n"
            "\t-h This help message\n"
            "\t-v Verbose output\n"
            "\t-board <AMC board = [0|1|2|3|4|5]>\n"
            "\t-bpm <BPM number = [0|1]>\n"
            "\t-b <broker_endpoint> Broker endpoint\n"
            "\t-pll_status PLL Lock status\n"
            "\t-trig_dir <dir> Trigger direction\n"
27 28 29 30 31
            "\t-trig_term <term> Trigger termination\n"
            "\t-adc_rand <Rand enable = [0|1]> Enable ADC Rand\n"
            "\t-adc_dith <Dith enable = [0|1]> Enable ADC Dith\n"
            "\t-adc_shdn <SHDN enable = [0|1]> Enable ADC SHDN\n"
            "\t-adc_rand <PGA enable = [0|1]> Enable ADC PGA\n"
32 33 34 35 36 37 38 39 40 41
            , program_name);
}

int main (int argc, char *argv [])
{
    int verbose = 0;
    char *broker_endp = NULL;
    char *board_number_str = NULL;
    char *bpm_number_str = NULL;
    char *trig_dir_str = NULL;
42
    char *trig_term_str = NULL;
43 44 45 46
    char *adc_rand_str = NULL;
    char *adc_dith_str = NULL;
    char *adc_shdn_str = NULL;
    char *adc_pga_str = NULL;
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    uint32_t get_pll_status = 0;
    char **str_p = NULL;

    if (argc < 3) {
        print_help (argv[0]);
        exit (1);
    }

    /* FIXME: This is rather buggy! */
    /* Simple handling of command-line options. This should be done
     *      * with getopt, for instance*/
    int i;
    for (i = 1; i < argc; i++)
    {
        if (streq(argv[i], "-v")) {
            verbose = 1;
        }
        else if (streq(argv[i], "-h"))
        {
            print_help (argv [0]);
            exit (1);
        }
        else if (streq(argv[i], "-board"))
        {
            str_p = &board_number_str;
        }
        else if (streq(argv[i], "-bpm"))
        {
            str_p = &bpm_number_str;
        }
        else if (streq (argv[i], "-b")) {
            str_p = &broker_endp;
        }
        else if (streq(argv[i], "-pll_status"))
        {
            get_pll_status = 1;
        }
        else if (streq (argv[i], "-trig_dir")) {
            str_p = &trig_dir_str;
        }
87 88
        else if (streq (argv[i], "-trig_term")) {
            str_p = &trig_term_str;
89
        }
90 91 92 93 94 95 96 97 98 99 100 101
        else if (streq (argv[i], "-adc_rand")) {
            str_p = &adc_rand_str;
        }
        else if (streq (argv[i], "-adc_dith")) {
            str_p = &adc_dith_str;
        }
        else if (streq (argv[i], "-adc_shdn")) {
            str_p = &adc_shdn_str;
        }
        else if (streq (argv[i], "-adc_pga")) {
            str_p = &adc_pga_str;
        }
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        /* Fallout for options with parameters */
        else {
            *str_p = strdup (argv[i]);
        }
    }

    /* Set default broker address */
    if (broker_endp == NULL) {
        broker_endp = strdup ("ipc://"DFLT_BIND_FOLDER);
    }

    /* Set default board number */
    uint32_t board_number;
    if (board_number_str == NULL) {
        fprintf (stderr, "[client:fmc130m_4ch]: Setting default value to BOARD number: %u\n",
                DFLT_BOARD_NUMBER);
        board_number = DFLT_BOARD_NUMBER;
    }
    else {
        board_number = strtoul (board_number_str, NULL, 10);
    }

    /* Set default bpm number */
    uint32_t bpm_number;
    if (bpm_number_str == NULL) {
        fprintf (stderr, "[client:fmc130m_4ch]: Setting default value to BPM number: %u\n",
                DFLT_BPM_NUMBER);
        bpm_number = DFLT_BPM_NUMBER;
    }
    else {
        bpm_number = strtoul (bpm_number_str, NULL, 10);

        if (bpm_number > MAX_BPM_NUMBER) {
            fprintf (stderr, "[client:fmc130m_4ch]: BPM number too big! Defaulting to: %u\n",
                    MAX_BPM_NUMBER);
            bpm_number = MAX_BPM_NUMBER;
        }
    }

    /* Generate the service names for each SMIO */
    char service[50];
143
    snprintf (service, sizeof (service), "BPM%u:DEVIO:FMC130M_4CH%u", board_number, bpm_number);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

    bpm_client_t *bpm_client = bpm_client_new (broker_endp, verbose, NULL);

    bpm_client_err_e err = BPM_CLIENT_SUCCESS;
    uint32_t trig_dir = 0;
    if (trig_dir_str != NULL) {
        trig_dir = strtoul (trig_dir_str, NULL, 10);

        fprintf (stdout, "[client:fmc130m_4ch]: trig_dir = %u\n", trig_dir);
        err = bpm_set_trig_dir (bpm_client, service, trig_dir);
        if (err != BPM_CLIENT_SUCCESS){
            fprintf (stderr, "[client:fmc130m_4ch]: bpm_set_trig_dir failed\n");
            goto err_bpm_exit;
        }
        fprintf (stdout, "[client:fmc130m_4ch]: bpm_set_trig_dir was successfully executed\n");
    }

161 162 163
    uint32_t trig_term = 0;
    if (trig_term_str != NULL) {
        trig_term = strtoul (trig_term_str, NULL, 10);
164

165 166
        fprintf (stdout, "[client:fmc130m_4ch]: trig_term = %u\n", trig_term);
        err = bpm_set_trig_term (bpm_client, service, trig_term);
167
        if (err != BPM_CLIENT_SUCCESS){
168
            fprintf (stderr, "[client:fmc130m_4ch]: bpm_set_trig_term failed\n");
169 170
            goto err_bpm_exit;
        }
171
        fprintf (stdout, "[client:fmc130m_4ch]: bpm_set_trig_term was successfully executed\n");
172 173
    }

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    uint32_t adc_rand = 0;
    if (adc_rand_str != NULL) {
        adc_rand = strtoul (adc_rand_str, NULL, 10);

        fprintf (stdout, "[client:fmc130m_4ch]: adc_rand = %u\n", adc_rand);
        err = bpm_set_adc_rand (bpm_client, service, adc_rand);
        if (err != BPM_CLIENT_SUCCESS){
            fprintf (stderr, "[client:fmc130m_4ch]: bpm_set_adc_rand failed\n");
            goto err_bpm_exit;
        }
        fprintf (stdout, "[client:fmc130m_4ch]: bpm_set_adc_rand was successfully executed\n");
    }

    uint32_t adc_dith = 0;
    if (adc_dith_str != NULL) {
        adc_dith = strtoul (adc_dith_str, NULL, 10);

        fprintf (stdout, "[client:fmc130m_4ch]: adc_dith = %u\n", adc_dith);
        err = bpm_set_adc_dith (bpm_client, service, adc_dith);
        if (err != BPM_CLIENT_SUCCESS){
            fprintf (stderr, "[client:fmc130m_4ch]: bpm_set_adc_dith failed\n");
            goto err_bpm_exit;
        }
        fprintf (stdout, "[client:fmc130m_4ch]: bpm_set_adc_dith was successfully executed\n");
    }

    uint32_t adc_shdn = 0;
    if (adc_shdn_str != NULL) {
        adc_shdn = strtoul (adc_shdn_str, NULL, 10);

        fprintf (stdout, "[client:fmc130m_4ch]: adc_shdn = %u\n", adc_shdn);
        err = bpm_set_adc_shdn (bpm_client, service, adc_shdn);
        if (err != BPM_CLIENT_SUCCESS){
            fprintf (stderr, "[client:fmc130m_4ch]: bpm_set_adc_shdn failed\n");
            goto err_bpm_exit;
        }
        fprintf (stdout, "[client:fmc130m_4ch]: bpm_set_adc_shdn was successfully executed\n");
    }

    uint32_t adc_pga = 0;
    if (adc_pga_str != NULL) {
        adc_pga = strtoul (adc_pga_str, NULL, 10);

        fprintf (stdout, "[client:fmc130m_4ch]: adc_pga = %u\n", adc_pga);
        err = bpm_set_adc_pga (bpm_client, service, adc_pga);
        if (err != BPM_CLIENT_SUCCESS){
            fprintf (stderr, "[client:fmc130m_4ch]: bpm_set_adc_pga failed\n");
            goto err_bpm_exit;
        }
        fprintf (stdout, "[client:fmc130m_4ch]: bpm_set_adc_pga was successfully executed\n");
    }

226 227 228 229 230 231 232 233 234 235 236 237 238
    if (get_pll_status) {
        uint32_t pll_status = 0;
        bpm_client_err_e err = bpm_get_fmc_pll_status (bpm_client, service, &pll_status);
        if (err != BPM_CLIENT_SUCCESS){
            fprintf (stderr, "[client:fmc130m_4ch]: bpm_get_fmc_pll_status failed\n");
            goto err_bpm_exit;
        }
        fprintf (stdout, "[client:fmc130m_4ch]: bpm_get_fmc_pll_status: 0x%08X\n",
                pll_status);
    }

err_bpm_exit:
    bpm_client_destroy (&bpm_client);
239 240 241 242 243 244 245 246 247 248 249 250
    str_p = &adc_rand_str;
    free (*str_p);
    adc_rand_str = NULL;
    str_p = &adc_dith_str;
    free (*str_p);
    adc_dith_str = NULL;
    str_p = &adc_shdn_str;
    free (*str_p);
    adc_shdn_str = NULL;
    str_p = &adc_pga_str;
    free (*str_p);
    adc_pga_str = NULL;
251
    str_p = &trig_term_str;
252
    free (*str_p);
253
    trig_term_str = NULL;
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    str_p = &trig_dir_str;
    free (*str_p);
    trig_dir_str = NULL;
    str_p = &broker_endp;
    free (*str_p);
    broker_endp = NULL;
    str_p = &board_number_str;
    free (*str_p);
    board_number_str = NULL;
    str_p = &bpm_number_str;
    free (*str_p);
    bpm_number_str = NULL;

    return 0;
}