Main.c 21.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632

#include <stdio.h>
#include <usb.h>
#include <errno.h>
#include "ZestSC1.h"
#include "Local.h"

/*
 * Local function declaration
 */
static ZESTSC1_STATUS ZestSC1_CountCards(unsigned long *NumCards,
                                         unsigned long *CardIDs,
                                         unsigned long *SerialNumbers,
                                         ZESTSC1_FPGA_TYPE *FPGATypes,
                                         usb_dev_handle **RetHandle,
                                         int *Interface,
                                         unsigned long ReqCardID);
static ZESTSC1_STATUS ZestSC1_SetTimeOut(ZESTSC1_HANDLE Handle,
                                         unsigned long MilliSeconds);
static ZESTSC1_STATUS ZestSC1_ReadEEPROMRaw(usb_dev_handle *Handle,
                                            unsigned long Address,
                                            unsigned char *Data);


/******************************************************************************
* Return the total number of cards in the system                              *
* If CardIDs is non-NULL then fill in with a list of the card IDs             *
* If SerialNumbers is non-NULL then fill in with a list of the serial numbers *
******************************************************************************/
ZESTSC1_STATUS ZestSC1CountCards(unsigned long *NumCards,
                                 unsigned long *CardIDs,
                                 unsigned long *SerialNumbers,
                                 ZESTSC1_FPGA_TYPE *FPGATypes)
{
    ZESTSC1_STATUS Status;
    
    Status = ZestSC1_CountCards(NumCards, CardIDs, SerialNumbers, FPGATypes, NULL, NULL, 0);
    if (Status!=ZESTSC1_SUCCESS)
    {
        ZESTSC1_ERROR_GENERAL("ZestSC1CountCards", Status);
    }

    return ZESTSC1_SUCCESS;
}
static ZESTSC1_STATUS ZestSC1_CountCards(unsigned long *NumCards,
                                         unsigned long *CardIDs,
                                         unsigned long *SerialNumbers,
                                         ZESTSC1_FPGA_TYPE *FPGATypes,
                                         usb_dev_handle **RetHandle,
                                         int *Interface,
                                         unsigned long ReqCardID)
{
    int Count = 0;
    //usb_dev_handle *DeviceHandle;
    struct usb_bus *Buses;
    struct usb_bus *Bus;

    // Find ZestSC1 devices
    usb_init();

    usb_find_busses();
    usb_find_devices();
    Buses = usb_get_busses();
    for (Bus = Buses; Bus!=NULL; Bus = Bus->next)
    {
        struct usb_device *Dev;
        for (Dev = Bus->devices; Dev!=NULL; Dev = Dev->next)
        {
            if (Dev->descriptor.idVendor == VENDOR_ID &&
                Dev->descriptor.idProduct == PRODUCT_ID)
            {
                ZESTSC1_STATUS Status;
                unsigned char Value;
                unsigned long CardID = 255;
                unsigned long SerialNum = 0;
                ZESTSC1_FPGA_TYPE FPGAType = ZESTSC1_FPGA_UNKNOWN;
                int i;
                usb_dev_handle *DeviceHandle = usb_open(Dev);

                if (DeviceHandle==NULL)
                {
                    // Can't open device - move to next
                    continue;
                }

                // Get device details
                Status = ZestSC1_ReadEEPROMRaw(DeviceHandle, EEPROM_CARDID_ADDRESS, &Value);
                if (Status!=ZESTSC1_SUCCESS)
                {
                    usb_close(DeviceHandle);
                    continue;
                }
                CardID = Value;

                int ok = 1;
                for (i=0; i<4; i++)
                {
                    Status = ZestSC1_ReadEEPROMRaw(DeviceHandle, EEPROM_SERIAL_ADDRESS+i, &Value);
                    if (Status!=ZESTSC1_SUCCESS)
                    {
                        usb_close(DeviceHandle);
                        ok = 0;
                        break;
                    }
                    SerialNum = (SerialNum<<8) | Value;
                }
                if (!ok) continue;
                Status = ZestSC1_ReadEEPROMRaw(DeviceHandle, EEPROM_FPGA_ADDRESS, &Value);
                if (Status!=ZESTSC1_SUCCESS)
                {
                    usb_close(DeviceHandle);
                    continue;
                }
                FPGAType = Value;

                if (RetHandle!=NULL && CardID==ReqCardID)
                {
                    // Caller was requesting a specific card and this is it
                    // We know there is only one interface on the ZestSC1 - claim it
                    *Interface = Dev->config[0].interface[0].altsetting[0].bInterfaceNumber;
                    int ret = usb_claim_interface(DeviceHandle, Dev->config[0].interface[0].altsetting[0].bInterfaceNumber);
                    if (ret!=0)
                    {
                        usb_close(DeviceHandle);
                        return ZESTSC1_INTERNAL_ERROR;
                    }
                    *RetHandle = DeviceHandle;

                    return ZESTSC1_SUCCESS;
                }
                else
                {
                    // Caller was asking about all cards
                    if (CardIDs!=NULL)
                        CardIDs[Count] = CardID;
                    if (SerialNumbers!=NULL)
                        SerialNumbers[Count] = SerialNum;
                    if (FPGATypes!=NULL)
                        FPGATypes[Count] = FPGAType;
                    Count++;
                }
                usb_close(DeviceHandle);
            }
        }
    }

    if (RetHandle!=NULL)
    {
        return ZESTSC1_ILLEGAL_CARD_ID;
    }

    // Copy data to return parameters
    if (NumCards!=NULL)
    {
        *NumCards = Count;
    }

    return ZESTSC1_SUCCESS;
}


/******************************************************************************
* Open a card with a specified card ID                                        *
******************************************************************************/
ZESTSC1_STATUS ZestSC1OpenCard(unsigned long CardID,
                               ZESTSC1_HANDLE *Handle)
{
    ZESTSC1_STATUS Status;
    ZESTSC1_HANDLE_STRUCT *Struct;
    usb_dev_handle *DeviceHandle;
    int Interface = 0;
    int Count = 0;
    char Buffer[4096] = {0};
    int RetVal = 0;
    
    /*
     * Find the device index of the card
     */
    Status = ZestSC1_CountCards(NULL, NULL, NULL, NULL, &DeviceHandle, &Interface, CardID);
    if (Status!=ZESTSC1_SUCCESS)
    {
        ZESTSC1_ERROR("ZestSC1OpenCard", Status);
    }

    /*
     * Initialise the device structure
     */
    Struct = (ZESTSC1_HANDLE_STRUCT *)malloc(sizeof(ZESTSC1_HANDLE_STRUCT));
    if (Struct==NULL)
    {
        usb_release_interface(DeviceHandle, Interface);
        usb_close(DeviceHandle);
        ZESTSC1_ERROR("ZestSC1OpenCard", ZESTSC1_OUT_OF_MEMORY);
    }
    Struct->Magic = ZESTSC1_HANDLE_MAGIC;
    Struct->DeviceHandle = DeviceHandle;
    Struct->TimeOut = ZESTSC1_DEFAULT_TIMEOUT;
    Struct->Interface = Interface;

    /*
     * Set the default timeout
     */
    Status = ZestSC1_SetTimeOut((ZESTSC1_HANDLE)Struct, ZESTSC1_DEFAULT_TIMEOUT);
    if (Status!=ZESTSC1_SUCCESS)
    {
        usb_release_interface(DeviceHandle, Interface);
        usb_close(DeviceHandle);
        free(Struct);
        ZESTSC1_ERROR("ZestSC1OpenCard", Status);
    }

    *Handle = (ZESTSC1_HANDLE)Struct;

    // FIXME: First configuration after plugging in sometimes fails
    // Doing a dummy configuration of 4096 bytes seems to fix this
    /*
     * Reset 8051 and endpoints
     */
#if 0
    RetVal = usb_clear_halt(Struct->DeviceHandle, EP_INT_READ);
    if (RetVal!=0)
    {
        printf("RetVal = %d\n", RetVal);
        usb_release_interface(DeviceHandle, Interface);
        usb_close(DeviceHandle);
        free(Struct);
        ZESTSC1_ERROR("ZestSC1OpenCard", ZESTSC1_INTERNAL_ERROR);
    }
#endif
    Status = ZestSC1_Reset8051(*Handle);
    if (Status!=ZESTSC1_SUCCESS)
    {
        usb_release_interface(DeviceHandle, Interface);
        usb_close(DeviceHandle);
        free(Struct);
        ZESTSC1_ERROR("ZestSC1OpenCard", Status);
    }
#if 0
    if (usb_clear_halt(Struct->DeviceHandle, EP_DATA_WRITE)!=0)
    {
        usb_release_interface(DeviceHandle, Interface);
        usb_close(DeviceHandle);
        free(Struct);
        ZESTSC1_ERROR("ZestSC1OpenCard", ZESTSC1_INTERNAL_ERROR);
    }
    if (usb_clear_halt(Struct->DeviceHandle, EP_DATA_READ)!=0)
    {
        usb_release_interface(DeviceHandle, Interface);
        usb_close(DeviceHandle);
        free(Struct);
        ZESTSC1_ERROR("ZestSC1OpenCard", ZESTSC1_INTERNAL_ERROR);
    }
#endif

    /*
     * Send data to the card
     */
    RetVal = usb_control_msg(Struct->DeviceHandle, EP_CTRL_READ, VR_START_CONFIG,
                             sizeof(Buffer)&0xffff, sizeof(Buffer)&0xffff,
                             Buffer, 2, Struct->TimeOut);
    if (RetVal<=0)
    {
        usb_release_interface(DeviceHandle, Interface);
        usb_close(DeviceHandle);
        free(Struct);
        return ZESTSC1_INTERNAL_ERROR;
    }
    if (Buffer[1]!=0)
    {
        usb_release_interface(DeviceHandle, Interface);
        usb_close(DeviceHandle);
        free(Struct);
        return ZESTSC1_TIMEOUT;
    }
    for (Count=0; Count<(int)sizeof(Buffer); Count+=ZESTSC1_MAX_TRANSFER_LENGTH)
    {
        int Bytes;

        if ((sizeof(Buffer)-Count)<ZESTSC1_MAX_TRANSFER_LENGTH)
            Bytes = sizeof(Buffer)-Count;
        else
            Bytes = ZESTSC1_MAX_TRANSFER_LENGTH;

        if (usb_bulk_write(Struct->DeviceHandle, EP_CONFIG_WRITE,
                           Buffer+Count, Bytes, Struct->TimeOut)!=Bytes)
        {
            usb_release_interface(DeviceHandle, Interface);
            usb_close(DeviceHandle);
            free(Struct);
            return ZESTSC1_INTERNAL_ERROR;
        }
    }
    Status = ZestSC1_Reset8051(*Handle);
    if (Status!=ZESTSC1_SUCCESS)
    {
        usb_release_interface(DeviceHandle, Interface);
        usb_close(DeviceHandle);
        free(Struct);
        ZESTSC1_ERROR("ZestSC1OpenCard", Status);
    }
    // End FIXME

    return ZESTSC1_SUCCESS;
}


/******************************************************************************
* Get information about a card                                                *
******************************************************************************/
ZESTSC1_STATUS ZestSC1GetCardInfo(ZESTSC1_HANDLE Handle,
                                  ZESTSC1_CARD_INFO *Info)
{
    ZESTSC1_STATUS Status;
    unsigned char Value;
    unsigned long CardID = 255;
    unsigned long SerialNum = 0;
    ZESTSC1_FPGA_TYPE FPGAType = ZESTSC1_FPGA_UNKNOWN;
    unsigned long MemorySize = 0;
    unsigned long FirmwareVersion = 0;
    int i;
    char Buffer[3];

    ZESTSC1_CHECK_HANDLE("ZestSC1GetCardInfo", Handle);
    if (Info==NULL)
    {
        return ZESTSC1_SUCCESS;
    }

    // Get device details
    Status = ZestSC1_ReadEEPROMRaw(Struct->DeviceHandle, EEPROM_CARDID_ADDRESS, &Value);
    if (Status!=ZESTSC1_SUCCESS)
    {
        ZESTSC1_ERROR("ZestSC1GetCardInfo", ZESTSC1_INTERNAL_ERROR);
    }
    CardID = Value;

    for (i=0; i<4; i++)
    {
        Status = ZestSC1_ReadEEPROMRaw(Struct->DeviceHandle, EEPROM_SERIAL_ADDRESS+i, &Value);
        if (Status!=ZESTSC1_SUCCESS)
        {
            ZESTSC1_ERROR("ZestSC1GetCardInfo", ZESTSC1_INTERNAL_ERROR);
        }
        SerialNum = (SerialNum<<8) | Value;
    }

    Status = ZestSC1_ReadEEPROMRaw(Struct->DeviceHandle, EEPROM_FPGA_ADDRESS, &Value);
    if (Status!=ZESTSC1_SUCCESS)
    {
        ZESTSC1_ERROR("ZestSC1GetCardInfo", ZESTSC1_INTERNAL_ERROR);
    }
    FPGAType = Value;

    for (i=0; i<4; i++)
    {
        Status = ZestSC1_ReadEEPROMRaw(Struct->DeviceHandle, EEPROM_MEMORY_SIZE_ADDRESS+i, &Value);
        if (Status!=ZESTSC1_SUCCESS)
        {
            ZESTSC1_ERROR("ZestSC1GetCardInfo", ZESTSC1_INTERNAL_ERROR);
        }
        MemorySize = (MemorySize<<8) | Value;
    }

    if (usb_control_msg(Struct->DeviceHandle, EP_CTRL_READ, VR_GET_FIRMWARE_VER, 0, 0, Buffer, 3, Struct->TimeOut)<=0)
    {
        ZESTSC1_ERROR("ZestSC1GetCardInfo", ZESTSC1_INTERNAL_ERROR);
    }
    FirmwareVersion = Buffer[1] | (Buffer[2]<<8);

    Info->CardID = CardID;
    Info->SerialNumber = SerialNum;
    Info->FPGAType = FPGAType;
    Info->MemorySize = MemorySize;
    Info->TimeOut = Struct->TimeOut;
    Info->FirmwareVersion = FirmwareVersion;

    return ZESTSC1_SUCCESS;
}


/******************************************************************************
* Set the timeout value for a card.                                           *
* All blocking functions from now on will use this timeout value (in ms).     *
******************************************************************************/
ZESTSC1_STATUS ZestSC1_SetTimeOut(ZESTSC1_HANDLE Handle,
                                  unsigned long MilliSeconds)
{
    ZESTSC1_HANDLE_STRUCT *Struct = (ZESTSC1_HANDLE_STRUCT *)Handle;

    /*
     * Check the card handle is OK
     */
    if (Struct==NULL ||
        Struct->Magic!=ZESTSC1_HANDLE_MAGIC)
    {
        return ZESTSC1_ILLEGAL_HANDLE;
    }

    Struct->TimeOut = MilliSeconds;

    return ZESTSC1_SUCCESS;
}
ZESTSC1_STATUS ZestSC1SetTimeOut(ZESTSC1_HANDLE Handle,
                                 unsigned long MilliSeconds)
{
    ZESTSC1_STATUS Status;

    Status = ZestSC1_SetTimeOut(Handle, MilliSeconds);
    if (Status!=ZESTSC1_SUCCESS)
    {
        ZESTSC1_ERROR("ZestSC1SetTimeout", Status);
    }

    return  ZESTSC1_SUCCESS;
}

/******************************************************************************
* Set the ID of a card                                                        *
******************************************************************************/
ZESTSC1_STATUS ZestSC1SetCardID(ZESTSC1_HANDLE Handle,
                                unsigned long CardID)
{
    char Buffer[3];

    ZESTSC1_CHECK_HANDLE("ZestSC1SetCardID", Handle);

    if (usb_control_msg(Struct->DeviceHandle, EP_CTRL_READ, VR_WRITE_EEPROM, EEPROM_CARDID_ADDRESS, CardID, Buffer, 3, Struct->TimeOut)<=0 ||
        Buffer[1]!=0)
    {
        ZESTSC1_ERROR("ZestSC1SetCardID", ZESTSC1_INTERNAL_ERROR);
    }

    return ZESTSC1_SUCCESS;
}


/******************************************************************************
* Close a card and free up associated resources                               *
******************************************************************************/
ZESTSC1_STATUS ZestSC1CloseCard(ZESTSC1_HANDLE Handle)
{
    int RetVal;
    char Buffer[3];
    ZESTSC1_CHECK_HANDLE("ZestSC1CloseCard", Handle);

    /*
     * Clear FPGA
    */
    RetVal = usb_control_msg(Struct->DeviceHandle, EP_CTRL_READ, VR_START_CONFIG,
                             sizeof(Buffer)&0xffff, sizeof(Buffer)&0xffff,
                             Buffer, 2, Struct->TimeOut);
    /*
     * Reset 8051
     */
    /* ZestSC1_Reset8051(Struct->DeviceHandle); FIXED EC */
    ZestSC1_Reset8051(Handle);

    /*
     * Free other resources
     */
    usb_release_interface(Struct->DeviceHandle, Struct->Interface);
    usb_close(Struct->DeviceHandle);
    free(Struct);

    return ZESTSC1_SUCCESS;
}


/******************************************************************************
* Set the signal pin directions                                               *
******************************************************************************/
ZESTSC1_STATUS ZestSC1SetSignalDirection(ZESTSC1_HANDLE Handle,
                                         unsigned char Direction)
{
    char Buffer[1];

    ZESTSC1_CHECK_HANDLE("ZestSC1SetSignalDirection", Handle);

    if (usb_control_msg(Struct->DeviceHandle, EP_CTRL_READ, VR_SET_SIGNAL_DIR, Direction, 0, Buffer, 1, Struct->TimeOut)<=0)
    {
        ZESTSC1_ERROR("ZestSC1SetSignalDirection", ZESTSC1_INTERNAL_ERROR);
    }

    return ZESTSC1_SUCCESS;
}

/******************************************************************************
* Set the value on the signal pins                                            *
******************************************************************************/
ZESTSC1_STATUS ZestSC1SetSignals(ZESTSC1_HANDLE Handle,
                                 unsigned char Value)
{
    char Buffer[1];

    ZESTSC1_CHECK_HANDLE("ZestSC1SetSignals", Handle);

    if (usb_control_msg(Struct->DeviceHandle, EP_CTRL_READ, VR_SET_SIGNALS, Value, 0, Buffer, 1, Struct->TimeOut)<=0)
    {
        ZESTSC1_ERROR("ZestSC1SetSignals", ZESTSC1_INTERNAL_ERROR);
    }

    return ZESTSC1_SUCCESS;
}

/******************************************************************************
* Read the signals                                                            *
******************************************************************************/
ZESTSC1_STATUS ZestSC1ReadSignals(ZESTSC1_HANDLE Handle,
                                  unsigned char *Value)
{
    char Buffer[2];

    ZESTSC1_CHECK_HANDLE("ZestSC1ReadSignals", Handle);

    *Value = 0;

    if (usb_control_msg(Struct->DeviceHandle, EP_CTRL_READ, VR_GET_SIGNALS, 0, 0, Buffer, 2, Struct->TimeOut)<=0)
    {
        ZESTSC1_ERROR("ZestSC1ReadSignals", ZESTSC1_INTERNAL_ERROR);
    }

    *Value = Buffer[1];

    return ZESTSC1_SUCCESS;
}


/******************************************************************************
* Wait for the FPGA to generate an interrupt                                  *
******************************************************************************/
ZESTSC1_STATUS ZestSC1WaitForInterrupt(ZESTSC1_HANDLE Handle)
{
    char Buffer[8];
    //int RetVal;
    ZESTSC1_STATUS Status;

    // FIXME
    ZESTSC1_ERROR("ZestSC1WaitForInterrupt", ZESTSC1_INTERNAL_ERROR);
    
    ZESTSC1_CHECK_HANDLE("ZestSC1WaitForInterrupt", Handle);

    Status = ZestSC1_Transfer(Handle, EP_INT_READ, Buffer, 1);
    if (Status!=ZESTSC1_SUCCESS)
    {
        ZESTSC1_ERROR("ZestSC1WaitForInterrupt", Status);
    }

    return ZESTSC1_SUCCESS;
}


/*********************
* INTERNAL FUNCTIONS *
*********************/

/******************************************************************************
* Write a byte to the EEPROM                                                  *
******************************************************************************/
ZESTSC1_STATUS ZestSC1_WriteEEPROM(ZESTSC1_HANDLE Handle,
                                   unsigned long Address,
                                   unsigned char Data)
{
    char Buffer[3];
    
    ZESTSC1_CHECK_HANDLE("ZestSC1_WriteEEPROM", Handle);

    if (usb_control_msg(Struct->DeviceHandle, EP_CTRL_READ, VR_WRITE_EEPROM, Address, Data, Buffer, 3, Struct->TimeOut)<=0 ||
        Buffer[1]!=0)
    {
        ZESTSC1_ERROR("ZestSC1SetCardID", ZESTSC1_INTERNAL_ERROR);
    }

    return ZESTSC1_SUCCESS;
}

/******************************************************************************
* Read a byte from the EEPROM                                                 *
******************************************************************************/
ZESTSC1_STATUS ZestSC1_ReadEEPROM(ZESTSC1_HANDLE Handle,
                                  unsigned long Address,
                                  unsigned char *Data)
{
    ZESTSC1_CHECK_HANDLE("ZestSC1_ReadEEPROM", Handle);

    return ZestSC1_ReadEEPROMRaw(Struct->DeviceHandle, Address, Data);
}
static ZESTSC1_STATUS ZestSC1_ReadEEPROMRaw(usb_dev_handle *Handle,
                                            unsigned long Address,
                                            unsigned char *Data)
{
    char Buffer[3] = {0,0,0};

    if (usb_control_msg(Handle, EP_CTRL_READ, VR_READ_EEPROM, Address, 0, Buffer, 3, ZESTSC1_DEFAULT_TIMEOUT)<=0 ||
        Buffer[1]!=0)
    {
        return ZESTSC1_INTERNAL_ERROR;
    }

    *Data = Buffer[2];

    return ZESTSC1_SUCCESS;
}

/******************************************************************************
* Reset the 8051 microcontroller                                              *
******************************************************************************/
ZESTSC1_STATUS ZestSC1_Reset8051(ZESTSC1_HANDLE Handle)
{
    char Buffer[3];
    int RetVal;
    /* ZESTSC1_HANDLE_STRUCT *Struct = (ZESTSC1_HANDLE_STRUCT *)Handle; FIXED EC */
    ZESTSC1_CHECK_HANDLE("ZestSC1_Reset8051", Handle);
    
    Buffer[0] = 1;
    RetVal = usb_control_msg(Struct->DeviceHandle, EP_CTRL_WRITE, ANCHOR_LOAD_INTERNAL, 
                             CPUCS_REG_FX2, 0, Buffer, 1, Struct->TimeOut);
    if (RetVal<=0)
    {
        return ZESTSC1_INTERNAL_ERROR;
    }
    Buffer[0] = 0;
    RetVal = usb_control_msg(Struct->DeviceHandle, EP_CTRL_WRITE, ANCHOR_LOAD_INTERNAL, 
                             CPUCS_REG_FX2, 0, Buffer, 1, Struct->TimeOut);
    if (RetVal<=0)
    {
        return ZESTSC1_INTERNAL_ERROR;
    }
    
    return ZESTSC1_SUCCESS;
}