⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdo.c

📁 AVR平台下的CanOpen协议桟源码包括应用
💻 C
📖 第 1 页 / 共 4 页
字号:
		  /* The sub-index does not exist */
		  sdo_error = SDO_ECODE_ATTRIBUTE;
		}
	    }
	}
      else
	{
	  /* The sub-index does not exist */
	  sdo_error = SDO_ECODE_ATTRIBUTE;
	}
      break;

    case OD_ANALOG_HI:
      switch( od_index_lo )
	{
	case OD_ANALOG_IN_LO:
	  if( od_subind == OD_NO_OF_ENTRIES )
	    {
	      msg_data[4] = ADC_MAX_INPUTS;
	      nbytes = 1;  /* Significant bytes < 4 */
	    }
	  else
	    {
	      if( od_subind <= ADC_MAX_INPUTS )
		{
		  if( adc_read( od_subind-1, &msg_data[4] ) == FALSE )
		    {
		      /* Something went wrong... */
		      sdo_error = SDO_ECODE_HARDWARE;
		    }
		  else
		    {
		      /* Performed a conversion */
		      nbytes = 3;  /* Significant bytes < 4 */
		    }
		}
	      else
		{
		  /* The sub-index does not exist */
		  sdo_error = SDO_ECODE_ATTRIBUTE;
		}
	    }
	  break;

	case OD_ANALOG_OUT_16_LO:
	  if( od_subind == OD_NO_OF_ENTRIES )
	    {
	      msg_data[4] = dac_chan_cnt();
	      nbytes = 1;  /* Significant bytes < 4 */
	    }
	  else
	    {
	      if( od_subind <= dac_chan_cnt() )
		{
		  if( dac_read( od_subind-1, &msg_data[4] ) == FALSE )
		    {
		      /* Something went wrong... */
		      sdo_error = SDO_ECODE_HARDWARE;
		    }
		  else
		    {
		      /* Read current DAC setting */
		      nbytes = 2;  /* Significant bytes < 4 */
		    }
		}
	      else
		{
		  /* The sub-index does not exist */
		  sdo_error = SDO_ECODE_ATTRIBUTE;
		}
	    }
	  break;

	case OD_ANALOG_IN_INT_ENA_LO:
	  if( od_subind == 0 )
	    {
	      msg_data[4] = adc_get_readout_on_change();
	      nbytes = 1;  /* Significant bytes < 4 */
	    }
	  else
	    {
	      /* The sub-index does not exist */
	      sdo_error = SDO_ECODE_ATTRIBUTE;
	    }
	  break;

	case OD_ANALOG_IN_DELTA_LO:
	case OD_ANALOG_IN_UPPER_LO:
	case OD_ANALOG_IN_LOWER_LO:
	  if( od_subind == OD_NO_OF_ENTRIES )
	    {
	      msg_data[4] = ADC_MAX_INPUTS;
	      nbytes = 1;  /* Significant bytes < 4 */
	    }
	  else
	    {
	      if( od_subind <= ADC_MAX_INPUTS )
		{
		  switch( od_index_lo )
		    {
		    case OD_ANALOG_IN_DELTA_LO:
		      adc_get_delta( od_subind-1, &msg_data[4] );
		      break;
		    case OD_ANALOG_IN_UPPER_LO:
		      adc_get_upperlimit( od_subind-1, &msg_data[4] );
		      break;
		    case OD_ANALOG_IN_LOWER_LO:
		      adc_get_lowerlimit( od_subind-1, &msg_data[4] );
		      break;
		    default:
		      /* The sub-index does not exist */
		      sdo_error = SDO_ECODE_ATTRIBUTE;
		      break;
		    }
		}
	      else
		{
		  /* The sub-index does not exist */
		  sdo_error = SDO_ECODE_ATTRIBUTE;
		}
	    }
	  break;

	default:
	  /* The index can not be accessed, does not exist */
	  sdo_error = SDO_ECODE_NONEXISTENT;
	  break;
	}
      break;

    case OD_ADC_CONFIG_HI:
      switch( od_index_lo )
	{
	case OD_ADC_CONFIG_LO:
	  if( adc_get_config( od_subind,
			      &nbytes, &msg_data[4] ) == FALSE )
	    {
	      /* The sub-index does not exist or
		 cannot be accessed at the moment (ADC in use) */
	      sdo_error = SDO_ECODE_ATTRIBUTE;
	    }
	  break;

	case OD_ADC_RECALIB_SCAN_LO:
	  if( od_subind == 0 )
	    {
	      msg_data[4] = adc_get_calib_before_scan();
	      nbytes = 1;  /* Significant bytes < 4 */
	    }
	  else
	    {
	      /* The sub-index does not exist */
	      sdo_error = SDO_ECODE_ATTRIBUTE;
	    }
	  break;

	case OD_ADC_DELTA_ENA_LO:
	  if( od_subind == 0 )
	    {
	      msg_data[4] = adc_get_delta_scan_ena();
	      nbytes = 1;  /* Significant bytes < 4 */
	    }
	  else
	    {
	      /* The sub-index does not exist */
	      sdo_error = SDO_ECODE_ATTRIBUTE;
	    }
	  break;

	case OD_ADC_WINDOW_ENA_LO:
	  if( od_subind == 0 )
	    {
	      msg_data[4] = adc_get_window_scan_ena();
	      nbytes = 1;  /* Significant bytes < 4 */
	    }
	  else
	    {
	      /* The sub-index does not exist */
	      sdo_error = SDO_ECODE_ATTRIBUTE;
	    }
	  break;

	case OD_ADC_WINDOW_CNTR_LO:
	  if( od_subind == 0 )
	    {
	      msg_data[4] = adc_get_window_scan_cntr();
	      nbytes = 1;  /* Significant bytes < 4 */
	    }
	  else
	    {
	      /* The sub-index does not exist */
	      sdo_error = SDO_ECODE_ATTRIBUTE;
	    }
	  break;


	default:
	  /* The index can not be accessed, does not exist */
	  sdo_error = SDO_ECODE_NONEXISTENT;
	  break;
	}
      break;

    case OD_ADC_CALIB_RANGE_HI:
      if( od_index_lo == OD_ADC_CALIB_RANGE_LO )
	{
	  if( od_subind == 0 )
	    {
	      msg_data[4] = STORE_ADC_CALIB_BLOCKS;
	      nbytes = 1;  /* Significant bytes < 4 */
	    }
	  else
	    {
	      /* The sub-index does not exist */
	      sdo_error = SDO_ECODE_ATTRIBUTE;
	    }
	}
      else
	{
	  /* The index can not be accessed, does not exist */
	  sdo_error = SDO_ECODE_NONEXISTENT;
	}
      break;

    case OD_ADC_CALIB_PARS_HI:
      if( od_index_lo < STORE_ADC_CALIB_BLOCKS )
	{
	  if( od_subind == OD_NO_OF_ENTRIES )
	    {
	      msg_data[4] = 4;
	      nbytes = 1;  /* Significant bytes < 4 */
	    }
	  else
	    {
	      if( od_subind <= STORE_ADC_CALIB_PARS )
		{
		  if( adc_get_calib_const( od_index_lo, od_subind-1,
					   &msg_data[4], TRUE ) == FALSE )
		    {
		      /* EEPROM read operation failed
			 or calibration constant simply not present */
		      sdo_error = SDO_ECODE_HARDWARE;
		    }
		}
	      else
		{
		  /* The sub-index does not exist */
		  sdo_error = SDO_ECODE_ATTRIBUTE;
		}
	    }
	}
      else
	{
	  /* The index can not be accessed, does not exist */
	  sdo_error = SDO_ECODE_NONEXISTENT;
	}
      break;

    case OD_DIGIN_PAR_HI:
      if( od_index_lo == OD_DIGIN_DEBOUNCE_LO )
	{
	  if( od_subind == 0 )
	    {
	      msg_data[4] = digin_get_debounce();
	      nbytes = 1;  /* Significant bytes < 4 */
	    }
	  else
	    {
	      /* The sub-index does not exist */
	      sdo_error = SDO_ECODE_ATTRIBUTE;
	    }
	}
      else
	{
	  /* The index can not be accessed, does not exist */
	  sdo_error = SDO_ECODE_NONEXISTENT;
	}
      break;

    case OD_DIGOUT_PAR_HI:
      if( od_index_lo == OD_DIGOUT_INIT_LO )
	{
	  if( od_subind == 0 )
	    {
	      msg_data[4] = digout_get_init();
	      nbytes = 1;  /* Significant bytes < 4 */
	    }
	  else
	    {
	      /* The sub-index does not exist */
	      sdo_error = SDO_ECODE_ATTRIBUTE;
	    }
	}
      else
	{
	  /* The index can not be accessed, does not exist */
	  sdo_error = SDO_ECODE_NONEXISTENT;
	}
      break;

    case OD_DAC_CONFIG_HI:
      if( od_index_lo == OD_DAC_CONFIG_LO )
	{
	  switch( od_subind )
	    {
	    case OD_NO_OF_ENTRIES:
	      msg_data[4] = 3;
	      nbytes = 1;  /* Significant bytes < 4 */
	      break;
	    case 1:
	      msg_data[4] = dac_chan_cnt();
	      nbytes = 1;  /* Significant bytes < 4 */
	      break;
	    case 2:
	      msg_data[4] = dac_get_max525_select();
	      nbytes = 1;  /* Significant bytes < 4 */
	      break;
	    case 3:
	      msg_data[4] = dac_get_opto_delay();
	      nbytes = 1;  /* Significant bytes < 4 */
	      break;
	    default:
	      /* The sub-index does not exist */
	      sdo_error = SDO_ECODE_ATTRIBUTE;
	      break;
	    }
	}
      else
	{
	  /* The index can not be accessed, does not exist */
	  sdo_error = SDO_ECODE_NONEXISTENT;
	}
      break;

    case OD_SPI_HI:
      switch( od_index_lo )
	{
	case OD_SPI_ACCESS_LO:
	  switch( od_subind )
	    {
	    case OD_NO_OF_ENTRIES:
	      msg_data[4] = 4;
	      nbytes = 1;  /* Significant bytes < 4 */
	      break;
	    case 4:
	      msg_data[7] = spi_read_byte();
	    case 3:
	      msg_data[6] = spi_read_byte();
	    case 2:
	      msg_data[5] = spi_read_byte();
	    case 1:
	      msg_data[4] = spi_read_byte();
	      break;
	    default:
	      /* The sub-index does not exist */
	      sdo_error = SDO_ECODE_ATTRIBUTE;
	      break;
	    }
	  break;

	case OD_SPI_SELECT_LO:
	  msg_data[4] = spi_get_chipselect();
	  nbytes = 1;  /* Significant bytes < 4 */
	  break;

	case OD_SPI_CONFIG_LO:
	  nbytes = 1;  /* Significant bytes < 4 */
	  switch( od_subind )
	    {
	    case OD_NO_OF_ENTRIES:
	      msg_data[4] = 2;
	      break;
	    case 1:
	      msg_data[4] = spi_get_holdtime();
	      break;
	    case 2:
	      msg_data[4] = spi_get_rising_clk();
	      break;
	    default:
	      /* The sub-index does not exist */
	      sdo_error = SDO_ECODE_ATTRIBUTE;
	      break;
	    }
	  break;

	default:
	  /* The index can not be accessed, does not exist */
	  sdo_error = SDO_ECODE_NONEXISTENT;
	  break;
	}
      break;

    case OD_CALC_CRC_HI:
      if( od_index_lo == OD_CALC_CRC_LO )
	{
	  UINT16 crc;
	  BYTE   result;

	  switch( od_subind )
	    {
	    case OD_NO_OF_ENTRIES:
	      msg_data[4] = 3;
	      nbytes = 1;  /* Significant bytes < 4 */
	      break;

	    case OD_CRC_MASTER_FLASH:
	    case OD_CRC_SLAVE_FLASH:
	      if( od_subind == OD_CRC_MASTER_FLASH )
		result = crc_master( &crc );
	      else
		result = crc_slave( &crc );

	      if( result == FALSE )
		{
		  /* Something went wrong... */
		  if( crc == (UINT16) 0 )
		    {
		      /* No CRC found... */
		      sdo_error = SDO_ECODE_ACCESS;
		    }
		  else
		    {
		      /* Access error while reading Master FLASH */
		      sdo_error = SDO_ECODE_HARDWARE;
		    }
		}
	      else
		{
		  nbytes = 2;  /* Significant bytes < 4 */
		  msg_data[4] = (BYTE) (crc & (UINT16) 0x00FF);
		  msg_data[5] = (BYTE) ((crc & (UINT16) 0xFF00) >> 8);
		}
	      break;

	    case OD_CRC_MASTER_FLASH_GET:
	      result = crc_get( &msg_data[4] );
	      if( result == FALSE )
		{
		  /* No CRC found... */
		  sdo_error = SDO_ECODE_ACCESS;
		}
	      nbytes = 2;  /* Significant bytes < 4 */
	      break;

	    default:
	      /* The sub-index does not exist */
	      sdo_error = SDO_ECODE_ATTRIBUTE;
	      break;
	    }
	}
      else
	{
	  /* The index can not be accessed, does not exist */
	  sdo_error = SDO_ECODE_NONEXISTENT;
	}
      break;

    case OD_ELMB_SERIAL_NO_HI:
      if( od_index_lo == OD_ELMB_SERIAL_NO_LO )
	{
	  if( od_subind == 0 )
	    {
	      if( sn_get_serial_number( &msg_data[4] ) == FALSE )
		{
		  /* EEPROM read operation failed
		     or Serial Number simply not present */
		  sdo_error = SDO_ECODE_HARDWARE;
		}
	    }
	  else
	    {
	      /* The sub-index does not exist */
	      sdo_error = SDO_ECODE_ATTRIBUTE;
	    }
	}
      else
	{
	  /* The index can not be accessed, does not exist */
	  sdo_error = SDO_ECODE_NONEXISTENT;
	}
      break;

    case OD_COMPILE_OPTIONS_HI:
      if( od_index_lo == OD_COMPILE_OPTIONS_LO )
	{
	  if( od_subind == 0 )
	    {
#ifdef __ALL_MOTHERBOARDS__
	      msg_data[4] |= 0x01;
#endif
#ifdef __MOTHERBOARD1__
	      msg_data[4] |= 0x02;
#endif
#ifdef __ADC_AVR__
	      msg_data[4] |= 0x08;
#endif
#ifdef __ADC_NONE__
	      msg_data[4] |= 0x10;
#endif
#ifdef __7BIT_NODEID__
	      msg_data[4] |= 0x20;
#endif
#ifdef __RS232__
	      msg_data[4] |= 0x40;
#endif
#ifdef __ELMB103__
	      msg_data[4] |= 0x80;
#endif
#ifdef __VARS_IN_EEPROM__
	      msg_data[5] |= 0x01;
#endif
#ifdef __INCLUDE_TESTS__
	      msg_data[5] |= 0x04;
#endif
#ifdef __CAN_REFRESH__
	      msg_data[5] |= 0x10;
#endif
#ifdef __2313_SLAVE_PRESENT__
	      msg_data[5] |= 0x20;
#endif
	    }
	  else
	    {
	      /* The sub-index does not exist */
	      sdo_error = SDO_ECODE_ATTRIBUTE;
	    }
	}
      else
	{
	  /* The index can not be accessed, does not exist */
	  sdo_error = SDO_ECODE_NONEXISTENT;
	}
      break;

#ifdef __INCLUDE_TESTS__
    case OD_TEST_HI:
      /* Some (self)tests can be performed on I/O and memory, etc... */ 

      if( od_index_lo == OD_TEST_LO )
	{
	  switch( od_subind )
	    {
	    case OD_NO_OF_ENTRIES:
	      /* The number of tests available */
	      msg_data[4] = 2;
	      nbytes = 1;  /* Significant bytes < 4 */
	      break;

	    case OD_IO_TEST:
	      /* Do a predefined test on all available I/O PORTs and PINs;
		 this can be one of the production acceptance tests
		 of the (ELMB +) Motherboard */
	      iotest( &msg_data[4] );
#if !(defined(__ADC_AVR__) || defined(__ADC_NONE__))
	      /* ADC serial interface has been corrupted by I/O-test */
	      adc_serial_init();
#endif
	      break;

	    case OD_WATCHDOG_RESET_TEST:
	      /* The Watchdog Timer should kick in... */
	      while(1)
		{
		  MCUCR |= BIT( SE );
		  SLEEP();
		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -