euint8 if_spi_readbuf(hwInterface *iface, euint8 *buf, euint16 len)
{
    alt_u8  status;
    alt_u16 i;
   
    /* Set the SSO bit (force chipselect) */
    IOWR_ALTERA_AVALON_SPI_CONTROL(SPI_0_BASE, 0x400);
    /* Clear RX reg*/
    buf[0] = IORD_ALTERA_AVALON_SPI_RXDATA(SPI_0_BASE);
    /* wait till transmit and ready ok */
    do{
        status = IORD_ALTERA_AVALON_SPI_STATUS(SPI_0_BASE);//get status
    }while ((status & ALTERA_AVALON_SPI_STATUS_TRDY_MSK) == 0 );
    
    for(i=0;i<len;i++)
    {
        IOWR_ALTERA_AVALON_SPI_TXDATA(SPI_0_BASE, DUMMY);
        /* Wait until the interface has finished transmitting */
        
        while ((IORD_ALTERA_AVALON_SPI_STATUS(SPI_0_BASE) & ALTERA_AVALON_SPI_STATUS_RRDY_MSK) == 0);

        /* reciver ready */
        buf[i] = IORD_ALTERA_AVALON_SPI_RXDATA(SPI_0_BASE);
    
    }
 
    IOWR_ALTERA_AVALON_SPI_CONTROL(SPI_0_BASE, 0);
    return (0);
}
 bool  SPI_read16(alt_u16 * data)
 {
     int cnt = 0;
     while (( IORD_ALTERA_AVALON_SPI_STATUS(SPI_ADC_BASE) & ALTERA_AVALON_SPI_STATUS_RRDY_MSK ) == 0)
     {
         cnt++;
         usleep(LONG_DELAY);
         if (cnt >= 3)       //give it three chances
             return false ; // signify error
      }
     *(data) = IORD_ALTERA_AVALON_SPI_RXDATA(SPI_ADC_BASE); // read rxdata
     return true; //successful read
 }
Beispiel #3
0
int initLIS2DH12(void)
{
	int i;
	alt_u16  reg_read=0xA000;
	alt_u16   reg_data[2];


	IOWR_ALTERA_AVALON_SPI_CONTROL(SPI_GSENSOR_BASE,0x00);          //Initialize the spi control reg
	IOWR_ALTERA_AVALON_SPI_TXDATA(SPI_GSENSOR_BASE, 0x2097);		//setup data rate and enable x,y,z axes
	usleep(100*2000);
	IOWR_ALTERA_AVALON_SPI_TXDATA(SPI_GSENSOR_BASE, 0x23b8);		//full scale, high resolution
	usleep(100*2000);

	// Read the Device ID register
	IOWR_ALTERA_AVALON_SPI_TXDATA(SPI_GSENSOR_BASE, (0x8F00)); // setup address to read from (0x0F), MSbit is R/Wn
	usleep(100*2000);
	if((IORD_ALTERA_AVALON_SPI_RXDATA(SPI_GSENSOR_BASE))!=0x33)   // verify the device id matches (0x33)
		return -1;

	return 1; // success
}
Beispiel #4
0
void lis2dh12Read(alt_u16 *data_x, alt_u16 *data_y, alt_u16 *data_z)
{
	int i;
	alt_u8   data[6];


	  for(i=0;i<6;i++){
		  IOWR_ALTERA_AVALON_SPI_TXDATA(SPI_GSENSOR_BASE, rdata[i]);
		  usleep(100*200);
		  data[i] = IORD_ALTERA_AVALON_SPI_RXDATA(SPI_GSENSOR_BASE);
	   }

	  //raw data values
	  *data_x = ((data[1]<<8)|data[0]);
	  *data_y = ((data[3]<<8)|data[2]);
	  *data_z = ((data[5]<<8)|data[4]);


	  // actual g values, unused:
	  // x = (float)(data_x/16.0)*12.0;
	  // y = (float)(data_y/16.0)*12.0;
	  // z = (float)(data_z/16.0)*12.0;
}
euint8 if_spiSend(hwInterface *iface, euint8 outgoing)
{
	euint8 incoming=0;
	alt_u8  SD_Data=0,status;
  
	
	/* Set the SSO bit (force chipselect) */
	IOWR_ALTERA_AVALON_SPI_CONTROL(SPI_0_BASE, 0x400);

	do{
    	    status = IORD_ALTERA_AVALON_SPI_STATUS(SPI_0_BASE);//get status
	}while (((status & ALTERA_AVALON_SPI_STATUS_TRDY_MSK) == 0 ) &&
            (status & ALTERA_AVALON_SPI_STATUS_RRDY_MSK) == 0);
	/* wait till transmit and ready ok */

	IOWR_ALTERA_AVALON_SPI_TXDATA(SPI_0_BASE, outgoing);
    
	/* Wait until the interface has finished transmitting */
	do{status = IORD_ALTERA_AVALON_SPI_STATUS(SPI_0_BASE);}
	while ((status & ALTERA_AVALON_SPI_STATUS_TMT_MSK) == 0);

	/* reciver ready */
	if (((status & 0x80) != 0) ){
    	    SD_Data = IORD_ALTERA_AVALON_SPI_RXDATA(SPI_0_BASE);
	}
	else{
		printf("\n no recive after transmit");
	}
 
	IOWR_ALTERA_AVALON_SPI_SLAVE_SEL(SPI_0_BASE, 1);
	IOWR_ALTERA_AVALON_SPI_CONTROL(SPI_0_BASE, 0);
 
	if( (status & 0x100) !=0)
    	printf("\n error in spi error in spi");
  
	return (SD_Data);
}
int alt_avalon_spi_command(alt_u32 base, alt_u32 slave,
                           alt_u32 write_length, const alt_u8 * write_data,
                           alt_u32 read_length, alt_u8 * read_data,
                           alt_u32 flags)
{
  const alt_u8 * write_end = write_data + write_length;
  alt_u8 * read_end = read_data + read_length;

  alt_u32 write_zeros = read_length;
  alt_u32 read_ignore = write_length;
  alt_u32 status;

  /* We must not send more than two bytes to the target before it has
   * returned any as otherwise it will overflow. */
  /* Unfortunately the hardware does not seem to work with credits > 1,
   * leave it at 1 for now. */
  alt_32 credits = 1;

  /* Warning: this function is not currently safe if called in a multi-threaded
   * environment, something above must perform locking to make it safe if more
   * than one thread intends to use it.
   */

  IOWR_ALTERA_AVALON_SPI_SLAVE_SEL(base, 1 << slave);
  
  /* Set the SSO bit (force chipselect) only if the toggle flag is not set */
  if ((flags & ALT_AVALON_SPI_COMMAND_TOGGLE_SS_N) == 0) {
    IOWR_ALTERA_AVALON_SPI_CONTROL(base, ALTERA_AVALON_SPI_CONTROL_SSO_MSK);
  }

  /*
   * Discard any stale data present in the RXDATA register, in case
   * previous communication was interrupted and stale data was left
   * behind.
   */
  IORD_ALTERA_AVALON_SPI_RXDATA(base);
    
  /* Keep clocking until all the data has been processed. */
  for ( ; ; )
  {
    
    do
    {
      status = IORD_ALTERA_AVALON_SPI_STATUS(base);
    }
    while (((status & ALTERA_AVALON_SPI_STATUS_TRDY_MSK) == 0 || credits == 0) &&
            (status & ALTERA_AVALON_SPI_STATUS_RRDY_MSK) == 0);

    if ((status & ALTERA_AVALON_SPI_STATUS_TRDY_MSK) != 0 && credits > 0)
    {
      credits--;

      if (write_data < write_end)
        IOWR_ALTERA_AVALON_SPI_TXDATA(base, *write_data++);
      else if (write_zeros > 0)
      {
        write_zeros--;
        IOWR_ALTERA_AVALON_SPI_TXDATA(base, 0);
      }
      else
        credits = -1024;
    };

    if ((status & ALTERA_AVALON_SPI_STATUS_RRDY_MSK) != 0)
    {
      alt_u32 rxdata = IORD_ALTERA_AVALON_SPI_RXDATA(base);

      if (read_ignore > 0)
        read_ignore--;
      else
        *read_data++ = (alt_u8)rxdata;
      credits++;

      if (read_ignore == 0 && read_data == read_end)
        break;
    }
    
  }

  /* Wait until the interface has finished transmitting */
  do
  {
    status = IORD_ALTERA_AVALON_SPI_STATUS(base);
  }
  while ((status & ALTERA_AVALON_SPI_STATUS_TMT_MSK) == 0);

  /* Clear SSO (release chipselect) unless the caller is going to
   * keep using this chip
   */
  if ((flags & ALT_AVALON_SPI_COMMAND_MERGE) == 0)
    IOWR_ALTERA_AVALON_SPI_CONTROL(base, 0);

  return read_length;
}
int openPowerlink(void)
{
    const BYTE              abMacAddr[] = {MAC_ADDR};
    static tEplApiInitParam EplApiInitParam = {0};
	// needed for process var
	tEplObdSize         	ObdSize;
	tEplKernel 				EplRet;
	unsigned int			uiVarEntries;

    fShutdown_l = FALSE;

	////////////////////////
	// setup th EPL Stack //
	////////////////////////

	// set EPL init parameters
    EplApiInitParam.m_uiSizeOfStruct = sizeof (EplApiInitParam);

#ifdef NODESWITCH_SPI_BASE
    // read node-ID from hex switch on baseboard, which is connected via SPI shift register
    IOWR_ALTERA_AVALON_SPI_TXDATA(NODESWITCH_SPI_BASE, 0xFF);   // generate pulse for latching inputs
    while ((IORD_ALTERA_AVALON_SPI_STATUS(NODESWITCH_SPI_BASE) & ALTERA_AVALON_SPI_STATUS_RRDY_MSK) == 0)
    {   // wait
    }
    EplApiInitParam.m_uiNodeId = IORD_ALTERA_AVALON_SPI_RXDATA(NODESWITCH_SPI_BASE);
#endif
#ifdef NODESWITCH_PIO_BASE
    EplApiInitParam.m_uiNodeId = IORD_ALTERA_AVALON_PIO_DATA(NODESWITCH_PIO_BASE);
#endif

    if (EplApiInitParam.m_uiNodeId == EPL_C_ADR_INVALID)
    {
        EplApiInitParam.m_uiNodeId = NODEID; // defined at the top of this file!
    }

    EPL_MEMCPY(EplApiInitParam.m_abMacAddress, abMacAddr, sizeof(EplApiInitParam.m_abMacAddress));
    EplApiInitParam.m_abMacAddress[5] = (BYTE) EplApiInitParam.m_uiNodeId;

    // calculate IP address
    EplApiInitParam.m_dwIpAddress = (0xFFFFFF00 & IP_ADDR) | EplApiInitParam.m_uiNodeId;
	EplApiInitParam.m_uiIsochrTxMaxPayload = 256;
	EplApiInitParam.m_uiIsochrRxMaxPayload = 256;
	EplApiInitParam.m_dwPresMaxLatency = 2000;
	EplApiInitParam.m_dwAsndMaxLatency = 2000;
	EplApiInitParam.m_fAsyncOnly = FALSE;
	EplApiInitParam.m_dwFeatureFlags = -1;
	EplApiInitParam.m_dwCycleLen = CYCLE_LEN;
	EplApiInitParam.m_uiPreqActPayloadLimit = 36;
	EplApiInitParam.m_uiPresActPayloadLimit = 36;
	EplApiInitParam.m_uiMultiplCycleCnt = 0;
	EplApiInitParam.m_uiAsyncMtu = 1500;
	EplApiInitParam.m_uiPrescaler = 2;
	EplApiInitParam.m_dwLossOfFrameTolerance = 500000;
	EplApiInitParam.m_dwAsyncSlotTimeout = 3000000;
	EplApiInitParam.m_dwWaitSocPreq = 0;
	EplApiInitParam.m_dwDeviceType = -1;
	EplApiInitParam.m_dwVendorId = -1;
	EplApiInitParam.m_dwProductCode = -1;
	EplApiInitParam.m_dwRevisionNumber = -1;
	EplApiInitParam.m_dwSerialNumber = -1;
	EplApiInitParam.m_dwSubnetMask = SUBNET_MASK;
	EplApiInitParam.m_dwDefaultGateway = 0;
    EplApiInitParam.m_dwApplicationSwDate = 1;       // PDL_LocVerApplSw_REC.ApplSwDate_U32 on programmable device or date portion of NMT_ManufactSwVers_VS on non-programmable device
	EplApiInitParam.m_pfnCbEvent = AppCbEvent;
    EplApiInitParam.m_pfnCbSync  = AppCbSync;
    EplApiInitParam.m_pfnObdInitRam = EplObdInitRam;
    EplApiInitParam.m_dwSyncResLatency = EPL_C_DLL_T_IFG;

	// initialize EPL stack
    printf("init EPL Stack with node-ID 0x%02X:\n", EplApiInitParam.m_uiNodeId);
	EplRet = EplApiInitialize(&EplApiInitParam);
	if(EplRet != kEplSuccessful) {
        printf("init EPL Stack... error %X\n\n", EplRet);
		goto Exit;
    }
    printf("init EPL Stack...ok\n\n");

	// link process variables used by CN to object dictionary
    printf("linking process vars:\n");
    ObdSize = sizeof(bButtonInputs_l);
    uiVarEntries = 1;
    EplRet = EplApiLinkObject(0x6000, &bButtonInputs_l, &uiVarEntries, &ObdSize, 0x01);
    if (EplRet != kEplSuccessful)
    {
        printf("linking process vars... error\n\n");
        goto ExitShutdown;
    }

    ObdSize = sizeof(abVirtualInputs_l[0]);
    uiVarEntries = 17;
    EplRet = EplApiLinkObject(0x2000, abVirtualInputs_l, &uiVarEntries, &ObdSize, 0x01);
    if (EplRet != kEplSuccessful)
    {
        printf("linking process vars... error\n\n");
        goto ExitShutdown;
    }

    ObdSize = sizeof(adwVirtualInputs_l[0]);
    uiVarEntries = 8;
    EplRet = EplApiLinkObject(0x2001, adwVirtualInputs_l, &uiVarEntries, &ObdSize, 0x01);
    if (EplRet != kEplSuccessful)
    {
        printf("linking process vars... error\n\n");
        goto ExitShutdown;
    }

    ObdSize = sizeof(wDigitalOutputs_l);
    uiVarEntries = 1;
    EplRet = EplApiLinkObject(0x6300, &wDigitalOutputs_l, &uiVarEntries, &ObdSize, 0x01);
    if (EplRet != kEplSuccessful)
    {
        printf("linking process vars... error\n\n");
        goto ExitShutdown;
    }

    ObdSize = sizeof(bLedOutputs_l);
    uiVarEntries = 1;
    EplRet = EplApiLinkObject(0x6200, &bLedOutputs_l, &uiVarEntries, &ObdSize, 0x01);
    if (EplRet != kEplSuccessful)
    {
        printf("linking process vars... error\n\n");
        goto ExitShutdown;
    }

    ObdSize = sizeof(abVirtualOutputs_l[0]);
    uiVarEntries = 15;
    EplRet = EplApiLinkObject(0x2200, abVirtualOutputs_l, &uiVarEntries, &ObdSize, 0x01);
    if (EplRet != kEplSuccessful)
    {
        printf("linking process vars... error\n\n");
        goto ExitShutdown;
    }

    ObdSize = sizeof(adwVirtualOutputs_l[0]);
    uiVarEntries = 8;
    EplRet = EplApiLinkObject(0x2201, adwVirtualOutputs_l, &uiVarEntries, &ObdSize, 0x01);
    if (EplRet != kEplSuccessful)
    {
        printf("linking process vars... error\n\n");
        goto ExitShutdown;
    }
	printf("linking process vars... ok\n\n");

	// start the EPL stack
    printf("start EPL Stack...\n");
	EplRet = EplApiExecNmtCommand(kEplNmtEventSwReset);
    if (EplRet != kEplSuccessful) {
        printf("start EPL Stack... error\n\n");
        goto ExitShutdown;
    }
    printf("start EPL Stack... ok\n\n");

    printf("NIOS II with openPowerlink is ready!\n\n");

#ifdef LED_PIO_BASE
    IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, 0xFF);
#endif

    while(1)
    {
        EplApiProcess();
        if (fShutdown_l == TRUE)
            break;
    }

ExitShutdown:
    printf("Shutdown EPL Stack\n");
    EplApiShutdown(); //shutdown node

Exit:
	return EplRet;
}