/*
 * Class:     io_silverspoon_bulldog_linux_jni_NativeSpi
 * Method:    spiOpen
 * Signature: (Ljava/lang/String;III)I
 */
JNIEXPORT jint JNICALL Java_io_silverspoon_bulldog_linux_jni_NativeSpi_spiOpen
  (JNIEnv * env, jclass clazz, jstring path, jint mode, jint speed, jint bitsPerWord, jboolean lsbFirst) {
	char fileName[256];
	int len = (*env)->GetStringLength(env, path);
	(*env)->GetStringUTFRegion(env, path, 0, len, fileName);
	return spiOpen(fileName, mode, speed, bitsPerWord, lsbFirst == JNI_TRUE ? 1 : 0);
}
예제 #2
0
int main(int argc, char *argv[])
{
   int loops=LOOPS;
   int speed=SPEED;
   int bytes=BYTES;
   int i;
   int h;
   double start, diff, sps;
   unsigned char buf[16384];

   if (argc > 1) bytes = atoi(argv[1]);
   if ((bytes < 1) || (bytes > 16383)) bytes = BYTES;

   if (argc > 2) speed = atoi(argv[2]);
   if ((speed < 32000) || (speed > 32000000)) speed = SPEED;

   if (argc > 3) loops = atoi(argv[3]);
   if ((loops < 1) || (loops > 10000000)) loops = LOOPS;

   if (gpioInitialise() < 0) return 1;

   h = spiOpen(0, speed, 0);

   if (h < 0) return 2;

   start = time_time();

   for (i=0; i<loops; i++)
   {
      spiXfer(h, buf, buf, bytes);
   }

   diff = time_time() - start;

   printf("sps=%.1f: %d bytes @ %d bps (loops=%d time=%.1f)\n",
      (double)loops / diff, bytes, speed, loops, diff);

   spiClose(h);

   gpioTerminate();

   return 0;
}
예제 #3
0
파일: main.cpp 프로젝트: kaiwhata/eye_test
int init_hardware()
{

    if (gpioInitialise() < 0)
    {
      printf("GPIO initialization failed \n");
      return -1;
    }
    //gpioSetMode(24,PI_OUTPUT);

    // set motor pins
    gpioSetMode(12,PI_OUTPUT);
    gpioSetMode(16,PI_OUTPUT);
    gpioSetMode(20,PI_OUTPUT);
    gpioSetMode(21,PI_OUTPUT);

    //how many detail levels (1 = just the capture res, > 1 goes down by half each level, 4 max)
	//int num_levels = 1;
    cam = StartCamera(CAMERA_WIDTH, CAMERA_HEIGHT,30,1,true);
    if (cam == NULL)
    {
        printf(" Camera initialization failure\n");
        return -2;
    }
    // screen parameters


    spi_h = spiOpen(0,5000,0);
    if (spi_h<0)
    {
        printf("SPI failure\n");
        return -3;
    }
    // success
    // give camera time to settle
    sleep(1);
    return 0;

}
예제 #4
0
/**
 * \brief Receive data full-duplex over SPI
 *
 * If there is data waiting to be sent, that is transferred.
 * Data is also received over serial wire and then converted
 * into a SansgridSerial structure.  
 * \param sg_serial[out]		Where received data is placed. 
 * \param size[out]				Size of the returned payload, zero if not packet
 * received
 */
int8_t sgSerialReceive(SansgridSerial **sg_serial, uint32_t *size) {
	// Transmit/Receive serialdata, size of packet stored in size
	// Code from
	// https://git.drogon.net/?p=wiringPi;a=blob;f=examples/isr.c;h=2bef54af13a60b95ad87fbfc67d2961722eb016e;hb=HEAD
	int i;
	SansgridSerial *sg_serial_out = NULL;
	char buffer[sizeof(SansgridSerial)+1];
	int fd;
	// Set when we're sending data
	int sending = 0;
	int exit_code = 0;
	*size = sizeof(SansgridSerial);
	syslog(LOG_INFO, "Waiting for data over serial");
	if (!sem_initd) {
		sem_init(&wait_on_slave, 0, 0);
		sem_initd = 1;
	}
	memset(buffer, 0x0, sizeof(buffer));
	buffer[0] = SG_SERIAL_CTRL_NO_DATA;

	// receive data
	
	// first make sure semaphore is zeroed out
	while (sem_trywait(&wait_on_slave) == 0);
	// If there are no pending requests, wait until we get one
	if (digitalRead((SLAVE_INT_PIN)) && !queueSize(tx_buffer))
		sem_wait(&wait_on_slave);

	// Transfer about to occur
	if (queueTryDequeue(tx_buffer, (void**)&sg_serial_out) >= 0) {
		// Data needs to be sent
		sg_serial_out->control = SG_SERIAL_CTRL_VALID_DATA;
		*sg_serial = sg_serial_out;
		memcpy(buffer, sg_serial_out, *size);
		sending = 1;
	} else {
		// No data needs to be sent
		*sg_serial = (SansgridSerial*)malloc(sizeof(SansgridSerial));
		(*sg_serial)->control = SG_SERIAL_CTRL_NO_DATA;
		sending = 0;
	}


	pthread_mutex_lock(&transfer_lock);
	// LOCKED


	// Make sure only one thread can access SPI at a time
	syslog(LOG_INFO, "Sending data over Serial");

	// this needs to be atomic, since fd comes from static global g_fd
	if ((fd = spiOpen()) == -1) {
		return -1;
	}

	// (debug) Print our data to send 
	printf("Sending:\n");
	spiPrintSgSerial(buffer, *size);

	spiTransfer(buffer, *size);
	close(fd);

	// (debug) Print what we got 
	printf("Receiving:\n");
	spiPrintSgSerial(buffer, *size);

	memcpy(*sg_serial, buffer, *size);
	if (buffer[0] == SG_SERIAL_CTRL_VALID_DATA) {
		queueEnqueue(dispatch, sgSerialCP(*sg_serial));
		*size = sizeof(SansgridSerial);
		// Radio can't get spammed. Throttle sending
		exit_code = 0;
	} else {
		// No need for sg_serial anymore
		//free(sg_serial);
		//*size = 0;
		if (buffer[0] != SG_SERIAL_CTRL_VALID_DATA
				&& sending == 0) {
			// Didn't send valid data, and didn't get valid data
			syslog(LOG_WARNING, "Bad data on SPI");
			exit_code = -1;
		}
	}

	for (i=0; i<3; i++) {
		sleep(1);
		if (!digitalRead(SLAVE_INT_PIN))
			break;
	}


	pthread_mutex_unlock(&transfer_lock);
	// UNLOCKED



	return exit_code;
}
예제 #5
0
int32_t main(void)
{
    uint16_t u16ID;
    uint32_t u32ByteCount, u32FlashAddress, u32PageNumber;
    uint32_t nError = 0;

    outpw(REG_CLK_HCLKEN, 0x0527);
    outpw(REG_CLK_PCLKEN0, 0);
    outpw(REG_CLK_PCLKEN1, 0);

    sysDisableCache();
    sysFlushCache(I_D_CACHE);
    sysEnableCache(CACHE_WRITE_BACK);
    sysInitializeUART();

    /* Configure multi function pins to SPI0 */
    outpw(REG_SYS_GPB_MFPL, (inpw(REG_SYS_GPB_MFPL) & ~0xff000000) | 0xBB000000);
    outpw(REG_SYS_GPB_MFPH, (inpw(REG_SYS_GPB_MFPH) & ~0xff) | 0xBB);

    spiInit(0);
    spiOpen(0);

    // set spi interface speed to 2MHz
    spiIoctl(0, SPI_IOC_SET_SPEED, 2000000, 0);
    // set transfer length to 8-bit
    spiIoctl(0, SPI_IOC_SET_TX_BITLEN, 8, 0);
    // set transfer mode to mode-0
    spiIoctl(0, SPI_IOC_SET_MODE, 0, 0);

    // check flash id
    if((u16ID = SpiFlash_ReadMidDid()) == 0xEF17) {
        sysprintf("Flash found: W25Q128BV ...\n");
    } else
        sysprintf("Flash ID, 0x%x\n", u16ID);

    sysprintf("Erase chip ...");

    /* Erase SPI flash */
    SpiFlash_ChipErase();

    /* Wait ready */
    SpiFlash_WaitReady();

    sysprintf("[OK]\n");

    /* init source data buffer */
    for(u32ByteCount=0; u32ByteCount<TEST_LENGTH; u32ByteCount++) {
        SrcArray[u32ByteCount] = u32ByteCount;
    }

    sysprintf("Start to normal write data to Flash ...");
    /* Program SPI flash */
    u32FlashAddress = 0;
    for(u32PageNumber=0; u32PageNumber<TEST_NUMBER; u32PageNumber++) {
        /* page program */
        SpiFlash_NormalPageProgram(u32FlashAddress, SrcArray);
        SpiFlash_WaitReady();
        u32FlashAddress += 0x100;
    }

    sysprintf("[OK]\n");

    /* clear destination data buffer */
    for(u32ByteCount=0; u32ByteCount<TEST_LENGTH; u32ByteCount++) {
        DestArray[u32ByteCount] = 0;
    }

    sysprintf("Normal Read & Compare ...");

    /* Read SPI flash */
    u32FlashAddress = 0;
    for(u32PageNumber=0; u32PageNumber<TEST_NUMBER; u32PageNumber++) {
        /* page read */
        SpiFlash_NormalRead(u32FlashAddress, DestArray);
        u32FlashAddress += 0x100;

        for(u32ByteCount=0; u32ByteCount<TEST_LENGTH; u32ByteCount++) {
            if(DestArray[u32ByteCount] != SrcArray[u32ByteCount])
                nError ++;
        }
    }

    if(nError == 0)
        sysprintf("[OK]\n");
    else
        sysprintf("[FAIL]\n");

    return 1;
}