コード例 #1
0
ファイル: mytest.c プロジェクト: nvero/fmhobby
void testRFID(void)
{
//	D80000000000520007FF2A

	u16 tcrc=0;
	u8	i,j;

	tcrc = 0x00;
	for (i=0; i<8; i++)
	{
		tcrc ^= swapByte(mRFID[i+2]);
		for (j=0; j<=7; j++)
		{
			if ((tcrc & 0x80) !=0 )
			{
				tcrc = (tcrc << 1);
				tcrc ^= 0x11d;
			}
			else
			{
				tcrc = tcrc << 1;		
			}
			tcrc &= 0x1ff;
		}	

	}
	
	tcrc = swapByte(tcrc);	

   	i = tcrc;

}
コード例 #2
0
ファイル: crc.c プロジェクト: NexusIF/ProjectsBackup
/*! \brief
* Counts 8bit crc from input data vector.
*
*/
uint8_t simpleCrc(uint8_t message[], uint8_t nBytes)
{
    uint8_t remainder = 0;
    uint16_t byte = 0;
    uint8_t bit = 8;

    /*
     * Perform modulo-2 division, a byte at a time.
     */
    for (byte = 0; byte < nBytes; ++byte)
    {


        /*
         * Bring the next byte into the remainder.
         */
        uint8_t sbyte = message[byte];
        swapByte(&(sbyte));
        remainder ^= (sbyte << (WIDTH - 8));

        /*
         * Perform modulo-2 division, a bit at a time.
         */
        for (bit = 8; bit > 0; --bit)
        {
            /*
             * Try to divide the current data bit.
             */
            if (remainder & TOPBIT)
                remainder = (remainder << 1) ^ (POLYNOMIAL&((1<<WIDTH)-1));
            else
                remainder = (remainder << 1);
        }
    }

    /*
     * The final remainder is the CRC result.
     */
    swapByte(&(remainder));

    return remainder;

}   /* crcSlow() */
コード例 #3
0
ファイル: ch341a.c プロジェクト: Trel725/chavrprog
/* transfer len bytes of data to the spi device */
int32_t ch341SpiStream(uint8_t *out, uint8_t *in, uint32_t len)
{
    uint8_t inBuf[CH341_PACKET_LENGTH], outBuf[CH341_PACKET_LENGTH], *inPtr, *outPtr;
    int32_t ret, packetLen;
    bool done;

    if (devHandle == NULL) return -1;
  // disable cs toggle for every package
  //  ch341SpiCs(outBuf, true);
  //  ret = usbTransfer(__func__, BULK_WRITE_ENDPOINT, outBuf, 4);
  //  if (ret < 0) return -1;

    inPtr = in;

    do {
        done=true;
        packetLen=len+1;    // STREAM COMMAND + data length
        if (packetLen>CH341_PACKET_LENGTH) {
            packetLen=CH341_PACKET_LENGTH;
            done=false;
        }
        outPtr = outBuf;
        *outPtr++ = CH341A_CMD_SPI_STREAM;
        for (int i = 0; i < packetLen-1; ++i)
            *outPtr++ = swapByte(*out++);
        ret = usbTransfer(__func__, BULK_WRITE_ENDPOINT, outBuf, packetLen);
        if (ret < 0) return -1;
        ret = usbTransfer(__func__, BULK_READ_ENDPOINT, inBuf, packetLen-1);
        if (ret < 0) return -1;
        len -= ret;

        for (int i = 0; i < ret; ++i) // swap the buffer
            *inPtr++ = swapByte(inBuf[i]);
    } while (!done);

//    ch341SpiCs(outBuf, false);
//    ret = usbTransfer(__func__, BULK_WRITE_ENDPOINT, outBuf, 3);
//    if (ret < 0) return -1;
    return 0;
}
コード例 #4
0
ファイル: ch341a.c プロジェクト: Trel725/chavrprog
/* callback for bulk in async transfer */
void cbBulkIn(struct libusb_transfer *transfer)
{
    switch(transfer->status) {
        case LIBUSB_TRANSFER_COMPLETED:
            /* the first package has cmd and address info, so discard 4 bytes */
            if (transfer->user_data != NULL) {
                for(int i = (bulkin_count == 0) ? 4 : 0; i < transfer->actual_length; ++i) {
                    *((uint8_t*)transfer->user_data++) = swapByte(transfer->buffer[i]);
                }
            }
            bulkin_count++;
            break;
        default:
            fprintf(stderr, "\ncbBulkIn: error : %d\n", transfer->status);
            bulkin_count = -1;
    }
    return;
}
コード例 #5
0
ファイル: CRC.c プロジェクト: NexusIF/ProjectsBackup
unsigned char simpleCrc(unsigned char* message,unsigned int nBytes)
{
    uint8_t remainder = 0;

    /*
     * Perform modulo-2 division, a byte at a time.
     */
    for (uint16_t byte = 0; byte < nBytes; byte++)
    {

		uint8_t swapedByte = message[byte];
		swapByte(&swapedByte);
		/*
         * Bring the next byte into the remainder.
         */
        remainder ^=( swapedByte << (WIDTH - 8));

        /*
         * Perform modulo-2 division, a bit at a time.
         */
        for (uint8_t bit = 8; bit > 0; --bit)
        {
            /*
             * Try to divide the current data bit.
             */
            if (remainder & TOPBIT)
            {
                remainder = (remainder << 1) ^ POLYNOMIAL;
            }
            else
            {
                remainder = (remainder << 1);
            }
        }
    }

    /*
     * The final remainder is the CRC result.
     */
    return remainder;
}
コード例 #6
0
inline void swapEndian(float* v)
{
	unsigned char* x = (unsigned char*)v;
	swapByte(x+0, x+3); swapByte(x+1, x+2);
}
コード例 #7
0
inline void swapEndian(short* v)
{
	unsigned char* x = (unsigned char*)v;
	swapByte(x+0, x+1);
}
コード例 #8
0
ファイル: ch341a.c プロジェクト: Trel725/chavrprog
/* write buffer(*buf) to SPI flash */
int32_t ch341SpiWrite(uint8_t *buf, uint32_t add, uint32_t len)
{
    uint8_t out[WRITE_PAYLOAD_LENGTH];
    uint8_t in[CH341_PACKET_LENGTH];
    uint32_t tmp, pkg_count;
    struct libusb_transfer *xferBulkIn, *xferBulkOut;
    uint32_t idx = 0;
    uint32_t ret;
    int32_t old_counter;
    struct timeval tv = {0, 100};

    if (devHandle == NULL) return -1;
    memset(out, 0xff, WRITE_PAYLOAD_LENGTH);
    xferBulkIn  = libusb_alloc_transfer(0);
    xferBulkOut = libusb_alloc_transfer(0);

    while (len > 0) {
        out[0] = 0x06; // Write enable
        ret = ch341SpiStream(out, in, 1);
        ch341SpiCs(out, true);
        idx = CH341_PACKET_LENGTH;
        out[idx++] = CH341A_CMD_SPI_STREAM;
        out[idx++] = 0x40; // byte swapped command for Flash Page Write
        tmp = add;
        for (int i = 0; i < 3; ++i) { // starting address of next write
            out[idx++] = swapByte((tmp >> 16) & 0xFF);
            tmp <<= 8;
        }
        tmp = 0;
        pkg_count = 1;
        while ((idx < WRITE_PAYLOAD_LENGTH) && (len > tmp)) {
            if (idx % CH341_PACKET_LENGTH == 0) {
                out[idx++] = CH341A_CMD_SPI_STREAM;
                pkg_count ++;
            } else {
                out[idx++] = swapByte(*buf++);
                tmp++;
                if (((add + tmp) & 0xFF) == 0) // cross page boundary
                    break;
            }
        }
        len -= tmp;
        add += tmp;
        bulkin_count = 0;
        libusb_fill_bulk_transfer(xferBulkIn, devHandle, BULK_READ_ENDPOINT, in,
                CH341_PACKET_LENGTH, cbBulkIn, NULL, DEFAULT_TIMEOUT);
        libusb_submit_transfer(xferBulkIn);
        libusb_fill_bulk_transfer(xferBulkOut, devHandle, BULK_WRITE_ENDPOINT, out,
                idx, cbBulkOut, NULL, DEFAULT_TIMEOUT);
        libusb_submit_transfer(xferBulkOut);
        old_counter = bulkin_count;
        ret = 0;
        while (bulkin_count < pkg_count) {
            libusb_handle_events_timeout(NULL, &tv);
            if (bulkin_count == -1) { // encountered error
                ret = -1;
                break;
            }
            if (old_counter != bulkin_count) { // new package came
                if (bulkin_count != pkg_count)
                    libusb_submit_transfer(xferBulkIn);  // resubmit bulk in request
                old_counter = bulkin_count;
            }
        }
        if (ret < 0) break;
        ch341SpiCs(out, false);
        ret = usbTransfer(__func__, BULK_WRITE_ENDPOINT, out, 3);
        if (ret < 0) break;
        out[0] = 0x04; // Write disable
        ret = ch341SpiStream(out, in, 1);
        do {
            ret = ch341ReadStatus();
            if (ret != 0)
                libusb_handle_events_timeout(NULL, &tv);
        } while(ret != 0);
        if (force_stop == 1) { // user hit ctrl+C
            force_stop = 0;
            if (len > 0)
                fprintf(stderr, "User hit Ctrl+C, writing unfinished.\n");
            break;
        }
    }
    libusb_free_transfer(xferBulkIn);
    libusb_free_transfer(xferBulkOut);
    return ret;
}