Пример #1
0
unsigned char
i2c_inbyte(void)
{
	unsigned char aBitByte = 0;
	int i;

	/* Switch off I2C to get bit */
	i2c_disable();
	i2c_dir_in();
	i2c_delay(CLOCK_HIGH_TIME/2);

	/* Get bit */
	aBitByte |= i2c_getbit();

	/* Enable I2C */
	i2c_enable();
	i2c_delay(CLOCK_LOW_TIME/2);

	for (i = 1; i < 8; i++) {
		aBitByte <<= 1;
		/* Clock pulse */
		i2c_clk(I2C_CLOCK_HIGH);
		i2c_delay(CLOCK_HIGH_TIME);
		i2c_clk(I2C_CLOCK_LOW);
		i2c_delay(CLOCK_LOW_TIME);

		/* Switch off I2C to get bit */
		i2c_disable();
		i2c_dir_in();
		i2c_delay(CLOCK_HIGH_TIME/2);

		/* Get bit */
		aBitByte |= i2c_getbit();

		/* Enable I2C */
		i2c_enable();
		i2c_delay(CLOCK_LOW_TIME/2);
	}
	i2c_clk(I2C_CLOCK_HIGH);
	i2c_delay(CLOCK_HIGH_TIME);

        /*
	 * we leave the clock low, getbyte is usually followed
	 * by sendack/nack, they assume the clock to be low
	 */
        i2c_clk(I2C_CLOCK_LOW);
	return aBitByte;
}
Пример #2
0
 void read_coefficients() {
   int16_t a0coeff;
   int16_t b1coeff;
   int16_t b2coeff;
   int16_t c12coeff;
   BAR_POWER_UP();
   i2c_enable();

   i2c_start();
   i2c_write((DEVICE_ADDRESS | WRITE));
   i2c_write((READ_COEFFICIENTS_COMMAND));

   i2c_start();
   i2c_write((DEVICE_ADDRESS | READ));

   a0coeff = (( (uint16_t) i2c_read(1) << 8) | i2c_read(1));
   b1coeff = (( (uint16_t) i2c_read(1) << 8) | i2c_read(1));
   b2coeff = (( (uint16_t) i2c_read(1) << 8) | i2c_read(1));
   c12coeff = (( (uint16_t) (i2c_read(1) << 8) | i2c_read(0))) >> 2;


   _mpl115a2_a0 = (float)a0coeff / 8;
   _mpl115a2_b1 = (float)b1coeff / 8192;
   _mpl115a2_b2 = (float)b2coeff / 16384;
   _mpl115a2_c12 = (float)c12coeff;
   _mpl115a2_c12 /= 4194304.0;

   i2c_stop();
   i2c_disable();
   BAR_POWER_DOWN();
 }
Пример #3
0
void
i2c_disable_all()
{
  int i;
  for(i = 0; i < I2C_N_PORTS; i++)
    i2c_disable(i);
}
Пример #4
0
void mma7660_init(void)
{
  int32_t val;

  i2c_enable();

  buf[0] = INTSU;
  buf[1] = 0x00;
  i2c_transmitinit(ADDR, 2, buf);
  while(!i2c_transferred()) ;

  buf[0] = MODE;
  buf[1] = 0x01;
  i2c_transmitinit(ADDR, 2, buf);
  while(!i2c_transferred()) ;

  buf[0] = SR;
  buf[1] = 0x1F;
  i2c_transmitinit(ADDR, 2, buf);
  while(!i2c_transferred()) ;

  buf[0] = PDET;
  buf[1] = 0xFF;
  i2c_transmitinit(ADDR, 2, buf);
  while(!i2c_transferred()) ;

  buf[0] = PD;
  buf[1] = 0xFF;
  i2c_transmitinit(ADDR, 2, buf);
  while(!i2c_transferred()) ;

  i2c_disable();
}
Пример #5
0
void TwoWire::setClock(uint32_t frequencyHz)
{
    if (frequencyHz<=40000)
    {
        switch(frequencyHz)
        {
        case 1000000:
            dev_flags &= ~(I2C_FAST_MODE);
            dev_flags |= I2C_FAST_MODE_PLUS;// set FAST_MODE bit
            break;
        case 400000:
            dev_flags &= ~(I2C_FAST_MODE_PLUS);
            dev_flags |= I2C_FAST_MODE;// set FULL_SPEED bit
            break;
        case 100000:
        default:
            dev_flags &= ~(I2C_FAST_MODE | I2C_FAST_MODE_PLUS);// clear FAST_MODE and FULL_SPEED bits
            break;
        }
        if (sel_hard->regs->CR1 & I2C_CR1_PE){
            i2c_disable(sel_hard);
            i2c_master_enable(sel_hard, dev_flags);
        }
    }
    else
    {
        uint32 hz = (frequencyHz > 1200000) ? 1200000 : frequencyHz;
        i2c_overclock(sel_hard, hz);
    }
}
Пример #6
0
uint8 HardWire::process() {
    int8 res = i2c_master_xfer(sel_hard, &itc_msg, 1, 0);
    if (res != 0) {
        i2c_disable(sel_hard);
        i2c_master_enable(sel_hard, (I2C_BUS_RESET | dev_flags));
    }
    return 0;
}
Пример #7
0
unsigned char
i2c_inbyte(void)
{
	unsigned char aBitByte = 0;
	int i;

	
	i2c_disable();
	i2c_dir_in();
	i2c_delay(CLOCK_HIGH_TIME/2);

	
	aBitByte |= i2c_getbit();

	
	i2c_enable();
	i2c_delay(CLOCK_LOW_TIME/2);

	for (i = 1; i < 8; i++) {
		aBitByte <<= 1;
		
		i2c_clk(I2C_CLOCK_HIGH);
		i2c_delay(CLOCK_HIGH_TIME);
		i2c_clk(I2C_CLOCK_LOW);
		i2c_delay(CLOCK_LOW_TIME);

		
		i2c_disable();
		i2c_dir_in();
		i2c_delay(CLOCK_HIGH_TIME/2);

		
		aBitByte |= i2c_getbit();

		
		i2c_enable();
		i2c_delay(CLOCK_LOW_TIME/2);
	}
	i2c_clk(I2C_CLOCK_HIGH);
	i2c_delay(CLOCK_HIGH_TIME);

	i2c_clk(I2C_CLOCK_LOW);
	return aBitByte;
}
Пример #8
0
/**
 * \brief Initializes the requested I<SUP>2</SUP>C hardware module
 *
 * Initializes the I<SUP>2</SUP>C slave device requested and sets the provided
 * software module struct. Run this function before any further use of
 * the driver.
 *
 * \param[out] module  Pointer to software module struct
 * \param[in]  config  Pointer to the configuration struct
 *
 * \return Status of initialization.
 * \retval STATUS_OK                        Module initiated correctly
 * \retval STATUS_ERR_INVALID_ARG           Invalid argument in module or config structure.
 * \retval STATUS_ERR_ALREADY_INITIALIZED   If the Pinmux is not a valid one for I2C signals.
 *
 */
enum status_code i2c_slave_init(
		struct i2c_slave_module *const module,
		I2c *const hw,
		const struct i2c_slave_config *const config)
{
	/* Sanity check */
	Assert(module);
	Assert(module->hw);
	Assert(config);

	module->hw = hw;
	
	/* Sanity check arguments. */
	if ((module == NULL) || (config == NULL))
		return STATUS_ERR_INVALID_ARG;

	i2c_disable(module->hw);
	
	if (module->hw == I2C0)
		system_peripheral_reset(PERIPHERAL_I2C0_CORE);
	else if (module->hw == I2C1) {
		system_peripheral_reset(PERIPHERAL_I2C1_CORE);
	} else {
		return STATUS_ERR_INVALID_ARG;
	}

#if I2C_SLAVE_CALLBACK_MODE == true
	/* Initialize values in module. */
	module->registered_callback = 0;
	module->enabled_callback    = 0;
	module->buffer_length       = 0;
	module->buffer_remaining    = 0;
	module->buffer              = NULL;
	module->status              = STATUS_OK;

	_i2c_instances = (void*)module;
	if (module->hw == I2C0) {
		system_register_isr(RAM_ISR_TABLE_I2CRX0_INDEX, (uint32_t)_i2c_slave_rx_isr_handler);
		system_register_isr(RAM_ISR_TABLE_I2CTX0_INDEX, (uint32_t)_i2c_slave_tx_isr_handler);
		NVIC_EnableIRQ(I2C0_RX_IRQn);
		NVIC_EnableIRQ(I2C0_TX_IRQn);
	} else if (module->hw == I2C1) {
		system_register_isr(RAM_ISR_TABLE_I2CRX1_INDEX, (uint32_t)_i2c_slave_rx_isr_handler);
		system_register_isr(RAM_ISR_TABLE_I2CTX1_INDEX, (uint32_t)_i2c_slave_tx_isr_handler);
		NVIC_EnableIRQ(I2C1_RX_IRQn);
		NVIC_EnableIRQ(I2C1_TX_IRQn);
	}
#endif

	/* Set config and return status. */
	if(_i2c_slave_set_config(module, config) != STATUS_OK)
		return STATUS_ERR_NOT_INITIALIZED;

	return STATUS_OK;
}
Пример #9
0
unsigned char
i2c_inbyte(void)
{
	unsigned char aBitByte = 0;
	int i;

	/* Switch off I2C to get bit */
	i2c_disable();
	i2c_dir_in();
	i2c_delay(CLOCK_HIGH_TIME/2);

	/* Get bit */
	aBitByte |= i2c_getbit();

	/* Enable I2C */
	i2c_enable();
	i2c_delay(CLOCK_LOW_TIME/2);

	for (i = 1; i < 8; i++) {
		aBitByte <<= 1;
		/* Clock pulse */
		i2c_clk(I2C_CLOCK_HIGH);
		i2c_delay(CLOCK_HIGH_TIME);
		i2c_clk(I2C_CLOCK_LOW);
		i2c_delay(CLOCK_LOW_TIME);

		/* Switch off I2C to get bit */
		i2c_disable();
		i2c_dir_in();
		i2c_delay(CLOCK_HIGH_TIME/2);

		/* Get bit */
		aBitByte |= i2c_getbit();

		/* Enable I2C */
		i2c_enable();
		i2c_delay(CLOCK_LOW_TIME/2);
	}
	i2c_clk(I2C_CLOCK_HIGH);
	i2c_delay(CLOCK_HIGH_TIME);
	return aBitByte;
}
Пример #10
0
/*---------------------------------------------------------------------------*/
static int
configure(int type, int value)
{
  if(type != SENSORS_ACTIVE) {
    return SHT25_ERROR;
  }
  if(value) {
    i2c_enable();
  } else {
    i2c_disable();
  }
  enabled = value;
  return 0;
}
Пример #11
0
/*#---------------------------------------------------------------------------
 *#
 *# FUNCTION NAME: i2c_inbyte
 *#
 *# DESCRIPTION  : read a byte from the i2c interface
 *#
 *# PARAMETERS   : none
 *#
 *# RETURN       : returns the byte read from the I2C device
 *#
 *#---------------------------------------------------------------------------
 */
unsigned char i2c_inbyte( void )
{
#ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
    int n=MAXSCLRETRIES;
#endif
    unsigned char aBitByte = 0;
    unsigned char Mask     = 0x80;    /* !!! ATTENTION: do NOT use 'char', otherwise shifting is wrong!!! */
                                      /* Must be UNSIGNED, not SIGNED! */


    /* Switch off I2C to get bit */
    i2c_disable();
    i2c_sda_dir_in();

    while ( Mask != 0 )
    {
#ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
	i2c_scl_dir_in();
	for( ; n>0; n-- )
	{	
		if( i2c_scl_is_high() )
			break;
		i2c_delay( THIGH );
	}

        i2c_set_scl( SCL_HIGH );
	i2c_scl_dir_out();
#else
        i2c_set_scl( SCL_HIGH );
#endif
        i2c_delay( THIGH );

        if ( i2c_sda_is_high() )
        {
            aBitByte |= Mask;
        }

        i2c_set_scl( SCL_LOW );

        Mask >>= 1;

        i2c_delay( TLOW );
    }

    /*
     * we leave the clock low, getbyte is usually followed
     * by sendack/nack, they assume the clock to be low
     */
    return ( aBitByte );
}   /* i2c_inbyte */
Пример #12
0
/** Configure I2C address.
 *  @param obj     The I2C object
 *  @param idx     Currently not used
 *  @param address The address to be set
 *  @param mask    Currently not used
 */
void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
{
    struct i2c_s *obj_s = I2C_S(obj);

    /* disable I2C peripheral */
    i2c_disable(obj_s->i2c);
    /* I2C clock configure */
    i2c_clock_config(obj_s->i2c, 100000, I2C_DTCY_2);
    /* I2C address configure */
    i2c_mode_addr_config(obj_s->i2c, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, address);
    /* enable I2C0 */
    i2c_enable(obj_s->i2c);
    /* enable acknowledge */
    i2c_ack_config(obj_s->i2c, I2C_ACK_ENABLE);
}
Пример #13
0
uint8_t TwoWire::process() {
    int8 res = i2c_master_xfer(sel_hard, itc_msg, itc_msg_count, 0);
    if (res == I2C_ERROR_PROTOCOL) {
        if (sel_hard->error_flags & I2C_SR1_AF) { /* NACK */
            res = (sel_hard->error_flags & I2C_SR1_ADDR ? ENACKADDR : 
                                                          ENACKTRNS);
        } else if (sel_hard->error_flags & I2C_SR1_OVR) { /* Over/Underrun */
            res = EDATA;
        } else { /* Bus or Arbitration error */
            res = EOTHER;
        }
        i2c_disable(sel_hard);
        i2c_master_enable(sel_hard, (I2C_BUS_RESET | dev_flags));
    }
    return res;
}
Пример #14
0
void mma7660_acc(int *x, int *y, int *z)
{
  int tmp;
  i2c_enable();

  /* Read data */
  buf[0] = XOUT;
  i2c_transmitinit(ADDR, 1, buf);
  while(!i2c_transferred()) ;
  i2c_receiveinit(ADDR, 3, buf);
  while(!i2c_transferred()) ;

  i2c_disable();

  tmp = (signed char)((buf[0] & 0x20) ? (buf[0] | 0xC0) : (buf[0] & 0x3F));
  *x = (tmp*150)/32;
  tmp = (signed char)((buf[1] & 0x20) ? (buf[1] | 0xC0) : (buf[1] & 0x3F));
  *y = (tmp*150)/32;
  tmp = (signed char)((buf[2] & 0x20) ? (buf[2] | 0xC0) : (buf[2] & 0x3F));
  *z = (tmp*150)/32;
}
Пример #15
0
int
i2c_getack(void)
{
	int ack = 1;
	i2c_dir_out();
	i2c_data(I2C_DATA_HIGH);
	i2c_dir_in();
	i2c_delay(CLOCK_HIGH_TIME/4);
	i2c_clk(I2C_CLOCK_HIGH);
#if 0
	i2c_clk(1);
	i2c_data(1);
	i2c_data(1);
	i2c_disable();
	i2c_dir_in();
#endif

	i2c_delay(CLOCK_HIGH_TIME/2);
	if (i2c_getbit())
		ack = 0;
	i2c_delay(CLOCK_HIGH_TIME/2);
	if (!ack) {
		if (!i2c_getbit()) 
			ack = 1;
		i2c_delay(CLOCK_HIGH_TIME/2);
	}

#if 0
   i2c_data(I2C_DATA_LOW);

	i2c_enable();
	i2c_dir_out();
#endif
	i2c_clk(I2C_CLOCK_LOW);
	i2c_delay(CLOCK_HIGH_TIME/4);
	i2c_dir_out();
	i2c_data(I2C_DATA_HIGH);
	i2c_delay(CLOCK_LOW_TIME/2);
	return ack;
}
Пример #16
0
 uint16_t read_temperature_barometer_MPL115A2(){
 	 uint16_t ret = 0;
 	 uint8_t res = 0;
 	 float t;
 	 BAR_POWER_UP();
 	 i2c_enable();

 	 i2c_start();
 	 i2c_write((DEVICE_ADDRESS | WRITE));
 	 i2c_write((START_CONVERSION_COMMAND));
 	 i2c_write((0x01)); // trovato su un forum di uno spagnolo
 	 i2c_stop();
 	 //delay 1.6 ms
 	 __delay_cycles(1662500);

 	 i2c_start();
 	 i2c_write((DEVICE_ADDRESS | WRITE));
 	 i2c_write((READ_TEMPERATURE_MSB_COMMAND));

 	 i2c_start();
 	 i2c_write((DEVICE_ADDRESS | READ));

 	 res = i2c_read(1);
 	 //printf("temp MSB %x\n", res);
 	 ret = (res << 8);
 	 res = i2c_read(0);
 	 ret += res;
 	 ret = ret >> 6;

 	 i2c_stop();
 	 i2c_disable();
 	 BAR_POWER_DOWN();


 	  t = ((float) ret - 498.0F) / -5.35F +25.0F;           // C
 	  ret = (uint16_t)(t*100);
 	 return ret;
 }
Пример #17
0
static void i2c_init_as_master(unsigned char address)
{
//	__gpio_as_i2c();
	if(i2c_disable())
		printk("i2c not disable\n");
//	REG_I2C_CTRL = 0x43;
	REG_I2C_CTRL = 0x45;
	REG_I2C_TAR = address; /* slave id needed write only once */
	REG_I2C_INTM = 0;
	REG_I2C_FHCNT =49;
	REG_I2C_FLCNT =62;
//	REG_I2C_SHCNT =0xc80; // 6k
//	REG_I2C_SLCNT =0xeb0; // 6k
//	REG_I2C_SHCNT =0x640; // 12k
//	REG_I2C_SLCNT =0x758; // 12k
//	REG_I2C_SHCNT =0x320; //26k
//	REG_I2C_SLCNT =0x3ac; //26k
//	REG_I2C_SHCNT =0x12c; //71k
//	REG_I2C_SLCNT =0x160; //71k
//	REG_I2C_SHCNT =0xc8;
//	REG_I2C_SLCNT =0xe8;
	REG_I2C_ENB = 1; /*enable i2c*/
}
Пример #18
0
/** Configure the I2C frequency
 *
 *  @param obj The I2C object
 *  @param hz  Frequency in Hz
 */
void i2c_frequency(i2c_t *obj, int hz)
{
    int timeout;
    struct i2c_s *obj_s = I2C_S(obj);

    /* wait until I2C_FLAG_I2CBSY flag is reset */
    timeout = BUSY_TIMEOUT;
    while ((i2c_flag_get(obj_s->i2c, I2C_FLAG_I2CBSY)) && (--timeout != 0));

    /* reset to clear pending flags */
    i2c_hw_reset(obj);

    /* disable I2C peripheral */
    i2c_disable(obj_s->i2c);

    /* configure I2C frequence */
    i2c_clock_config(obj_s->i2c, hz, I2C_DTCY_2);

    /* configure I2C address mode and slave address */
    i2c_mode_addr_config(obj_s->i2c, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, 0);

    /* enable I2C peripheral */
    i2c_enable(obj_s->i2c);
}
Пример #19
0
int
i2c_getack(void)
{
	int ack = 1;
	/*
	 * enable output
	 */
	i2c_dir_out();
	/*
	 * Release data bus by setting
	 * data high
	 */
	i2c_data(I2C_DATA_HIGH);
	/*
	 * enable input
	 */
	i2c_dir_in();
	i2c_delay(CLOCK_HIGH_TIME/4);
	/*
	 * generate ACK clock pulse
	 */
	i2c_clk(I2C_CLOCK_HIGH);
	/*
	 * Use PORT PB instead of I2C
	 * for input. (I2C not working)
	 */
	i2c_clk(1);
	i2c_data(1);
	/*
	 * switch off I2C
	 */
	i2c_data(1);
	i2c_disable();
	i2c_dir_in();
	/*
	 * now wait for ack
	 */
	i2c_delay(CLOCK_HIGH_TIME/2);
	/*
	 * check for ack
	 */
	if(i2c_getbit())
		ack = 0;
	i2c_delay(CLOCK_HIGH_TIME/2);
	if(!ack){
		if(!i2c_getbit()) /* receiver pulld SDA low */
			ack = 1;
		i2c_delay(CLOCK_HIGH_TIME/2);
	}

	/*
	 * our clock is high now, make sure data is low
	 * before we enable our output. If we keep data high
	 * and enable output, we would generate a stop condition.
	 */
	i2c_data(I2C_DATA_LOW);

	/*
	 * end clock pulse
	 */
	i2c_enable();
	i2c_dir_out();
	i2c_clk(I2C_CLOCK_LOW);
	i2c_delay(CLOCK_HIGH_TIME/4);
	/*
	 * enable output
	 */
	i2c_dir_out();
	/*
	 * remove ACK clock pulse
	 */
	i2c_data(I2C_DATA_HIGH);
	i2c_delay(CLOCK_LOW_TIME/2);
	return ack;
}
Пример #20
0
TwoWire::~TwoWire() {
    i2c_disable(sel_hard);
    sel_hard = 0;
}
Пример #21
0
/*
 * Terminate I2C communication on the specified port
 */
void term_nxtcam(U8 port_id)
{
	i2c_disable(port_id);
}
Пример #22
0
/**
 * @brief Initialize an I2C device as slave (and master)
 * @param dev Device to enable
 * @param flags Bitwise or of the following I2C options:
 *              I2C_FAST_MODE: 400 khz operation,
 *              I2C_DUTY_16_9: 16/9 Tlow/Thigh duty cycle (only applicable for
 *                             fast mode),
 *              I2C_BUS_RESET: Reset the bus and clock out any hung slaves on
 *                             initialization,
 *              I2C_10BIT_ADDRESSING: Enable 10-bit addressing,
 *              I2C_REMAP: (deprecated, STM32F1 only) Remap I2C1 to SCL/PB8
 *                         SDA/PB9.
 *              I2C_SLAVE_DUAL_ADDRESS: Slave can respond on 2 i2C addresses
 *              I2C_SLAVE_GENERAL_CALL: SLA+W broadcast to all general call
 *                                      listeners on bus. Addr 0x00
 *              I2C_SLAVE_USE_RX_BUFFER: Use a buffer to receive the incoming
 *                                       data. Callback at end of recv
 *              I2C_SLAVE_USE_TX_BUFFER: Use a buffer to transmit data.
 *                                       Callback will be called before tx
 */
void i2c_slave_enable(i2c_dev *dev, uint32 flags) {
    i2c_disable(dev);
    i2c_master_enable(dev, dev->config_flags | flags);
}
Пример #23
0
void ClearExternalIO(void) {
	int i;

//	GS I2C Start
	i2c_disable();
	i2c_slave_disable();
//	GS I2C End

	if (SerialConsole != 1) SerialClose(1);
	if (SerialConsole != 2) SerialClose(2);
	if (SerialConsole != 3) SerialClose(3);
	if (SerialConsole != 4) SerialClose(4);

	// stop the sound
	SoundPlay = 0;
	CloseTimer2();
#ifdef OLIMEX
	CloseOC1();
#else
	CloseOC2();
#endif
	inttbl[NBRPINS + 2].intp = NULL;					// disable the tick interrupt
	for (i = 1; i < NBRPINS + 1; i++) {
		inttbl[i].intp = NULL;						// disable all interrupts
#ifdef OLIMEX
    #ifdef  OLIMEX_DUINOMITE_EMEGA
		if (ExtCurrentConfig[i] != EXT_CONSOLE_RESERVED && i != 35 ) {	// don't reset the serial console
    #else
		if (ExtCurrentConfig[i] != EXT_CONSOLE_RESERVED && i != 21 ) {	// don't reset the serial console
    #endif
#else
		if (ExtCurrentConfig[i] != EXT_CONSOLE_RESERVED) {              // don't reset the serial console
#endif
			ExtCfg(i, EXT_NOT_CONFIG);                              // all set to unconfigured
			ExtSet(i, 0);						// all outputs (when set) default to low
		}
	}
        InterruptReturn = NULL;
}


/****************************************************************************************************************************
Initialise the I/O pins
*****************************************************************************************************************************/
void initExtIO(void) {
	int i;

	for (i = 1; i < NBRPINS + 1; i++) {
		ExtCfg(i, EXT_NOT_CONFIG);                                      // all set to unconfigured
		ExtSet(i, 0);							// all outputs (when set) default to low
	}
#ifndef OLIMEX
 	CNCONbits.ON = 1;       						// turn on Change Notification module
 	CNPUEbits.CNPUE1 = 1;							// turn on the pullup for pin C13 also called CN1
#endif
	ExtCurrentConfig[0] = EXT_DIG_IN;                                       // and show that we can read from it
	P_LED_TRIS = P_OUTPUT; 							// make the LED pin an output
	ExtSet(0, 0);								// and turn it off
#ifdef OLIMEX
    #ifdef OLIMEX_DUINOMITE_EMEGA
	ExtCurrentConfig[35] = EXT_ANA_IN;
    #else
	ExtCurrentConfig[21] = EXT_ANA_IN;
    #endif
#endif

	// setup the analog (ADC) function
	AD1CON1 = 0x00E0;       						// automatic conversion after sampling
 	AD1CSSL = 0;       							// no scanning required
	AD1CON2 = 0;       							// use MUXA, use AVdd   &   AVss as Vref+/-
	AD1CON3 = 0x203; //0x1F3F;  							// Tsamp = 32 x Tad;
	AD1CON1bits.ADON = 1; 							// turn on the ADC

}


/****************************************************************************************************************************
Configure an I/O pin
Returns true if all is OK
*****************************************************************************************************************************/
int ExtCfg(int pin, int cfg) {
	int tris, ana, oc;

	if (pin < 0 || pin > NBRPINS) return false;						// initial sanity check

	// make sure that interrupts are disabled in case we are changing from an interrupt input
#ifdef OLIMEX
//	if (pin == 5) ConfigINT2(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_DISABLE);
//	if (pin == 6) ConfigINT3(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_DISABLE);
        #ifdef  OLIMEX_DUINOMITE_EMEGA
        #else
                if (pin == 7) ConfigINT4(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_DISABLE);
        #endif
#else
	if (pin == 11) ConfigINT1(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_DISABLE);
	if (pin == 12) ConfigINT2(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_DISABLE);
	if (pin == 13) ConfigINT3(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_DISABLE);
	if (pin == 14) ConfigINT4(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_DISABLE);
#endif

	inttbl[pin].intp = NULL;		// also disable a software interrupt on this pin

	switch (cfg) {
		case EXT_NOT_CONFIG:
//	#ifdef OLIMEX
//			if (pin == 5) {P_E5_TRIS = 1; P_E5_OC = 1;}
//			if (pin == 6) {P_E6_TRIS = 1; P_E6_OC = 1;}
//			if (pin == 11) {P_E11_TRIS = 1; P_E11_OC = 1;}
//			if (pin == 12) {P_E12_TRIS = 1; P_E12_OC = 1;}
//	#endif
			tris = 1; ana = 1; oc = 1;
			break;

		case EXT_ANA_IN:
//	#ifdef OLIMEX
			if (pin > 6 && pin < 19) return false;
//			if (pin == 5) {P_E5_TRIS = 1; P_E5_OC = 1;}
//			if (pin == 6) {P_E6_TRIS = 1; P_E6_OC = 1;}
//	#else
//			if (pin > 10) return false;
//	#endif
			tris = 1; ana = 0; oc = 1;
			break;

		case EXT_FREQ_IN:											// same as counting, so fall through
		case EXT_PER_IN:											// same as counting, so fall through
		case EXT_CNT_IN:
//	#ifdef OLIMEX
//	#else
//			if (pin == 11) {
//				INT1Count = INT1Value = 0;
//				ConfigINT1(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_ENABLE);
//				tris = 1; ana = 1; oc = 1;
//				break;
//			}
//	#endif
//	#ifdef OLIMEX
//			if (pin == 5) {
//				P_E5_TRIS = 1;
//				P_E5_OC = 1;
//	#else
//			if (pin == 12) {
//	#endif
//				INT2Count = INT2Value = 0;
//				ConfigINT2(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_ENABLE);
//				tris = 1; ana = 1; oc = 1;
//				break;
//			}
//	#ifdef OLIMEX
//			if (pin == 6) {
//				P_E6_TRIS = 1;
//				P_E6_OC = 1;
//	#else
//			if (pin == 13) {
//	#endif
//				INT3Count = INT3Value = 0;
//				ConfigINT3(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_ENABLE);
//				tris = 1; ana = 1; oc = 1;
//				break;
//			}
#ifdef  OLIMEX_DUINOMITE_EMEGA
                    break;
#else
	#ifdef OLIMEX
			if (pin == 7) {
	#else
			if (pin == 14) {
	#endif
				INT4Count = INT4Value = 0;
				ConfigINT4(EXT_INT_PRI_2 | RISING_EDGE_INT | EXT_INT_ENABLE);
				tris = 1; ana = 1; oc = 1;
				break;
			}
			return false;							// not an interrupt enabled pin
#endif

		case EXT_INT_LO:											// same as digital input, so fall through
		case EXT_INT_HI:											// same as digital input, so fall through
		case EXT_DIG_IN:
	#ifdef OLIMEX
//                        if (pin == 5) {P_E5_TRIS = 1; P_E5_OC = 1;}
//			if (pin == 6) {P_E6_TRIS = 1; P_E6_OC = 1;}
//			if (pin == 11) {P_E11_TRIS = 1; P_E11_OC = 1;}
//			if (pin == 12) {P_E12_TRIS = 1; P_E12_OC = 1;}
	#endif
			tris = 1; ana = 1; oc = 1;
			break;

		case EXT_DIG_OUT:
	#ifdef OLIMEX
//			if (pin == 11) {P_E11_TRIS = 1; P_E11_OC = 1;} //return false;
//			if (pin == 5) {P_E5_TRIS = 1; P_E5_OC = 1;}
//			if (pin == 6) {P_E6_TRIS = 1; P_E6_OC = 1;}
//			if (pin == 12) {P_E12_TRIS = 1; P_E12_OC = 1;}
	#endif
			tris = 0; ana = 1; oc = 0;
			break;

		case EXT_OC_OUT:
	#ifdef OLIMEX
//			if (pin == 11) {P_E11_TRIS = 1; P_E11_OC = 1;} //return false;
//			if (pin == 5) {P_E5_TRIS = 1; P_E5_OC = 1;}
//			if (pin == 6) {P_E6_TRIS = 1; P_E6_OC = 1;}
//			if (pin == 12) {P_E12_TRIS = 1; P_E12_OC = 1;}
	#else
			if (pin < 11) return false;
	#endif
			tris = 0; ana = 1; oc = 1;
			break;

		case EXT_COM_RESERVED:
		case EXT_CONSOLE_RESERVED:
			ExtCurrentConfig[pin] = cfg;		// don't do anything except set the config type
		#ifdef OLIMEX
//			if (pin == 5) {P_E5_TRIS = 1; P_E5_OC = 1; P_E5_ANALOG = 1;}
//			if (pin == 6) {P_E6_TRIS = 1; P_E6_OC = 1; P_E6_ANALOG = 1;}
//			if (pin == 11) {P_E11_TRIS = 1; P_E11_OC = 1;}
//			if (pin == 12) {P_E12_TRIS = 1; P_E12_OC = 1;}
		#endif
			return true;

		default:
			return false;
	}

	ExtCurrentConfig[pin] = cfg;

	// set the TRIS and analog characteristics
	switch (pin) {
		case 1:  P_E1_TRIS = tris;   P_E1_OC = oc;   P_E1_ANALOG = ana;		break;
		case 2:  P_E2_TRIS = tris;   P_E2_OC = oc;   P_E2_ANALOG = ana;		break;
		case 3:  P_E3_TRIS = tris;   P_E3_OC = oc;   P_E3_ANALOG = ana;		break;
		case 4:  P_E4_TRIS = tris;   P_E4_OC = oc;   P_E4_ANALOG = ana;		break;
		case 5:  P_E5_TRIS = tris;   P_E5_OC = oc;   P_E5_ANALOG = ana;		break;
		case 6:  P_E6_TRIS = tris;   P_E6_OC = oc;   P_E6_ANALOG = ana;		break;
	#ifdef OLIMEX
            #ifdef  OLIMEX_DUINOMITE_EMEGA
		case 7:  P_E7_TRIS = tris;   P_E7_OC = oc;   P_E7_ANALOG = ana;		break;
		case 8:  P_E8_TRIS = tris;   P_E8_OC = oc;   break;
		case 9:  P_E9_TRIS = tris;   P_E9_OC = oc;   break;
		case 10: P_E10_TRIS = tris;  P_E10_OC = oc;  break;
            #else
		case 7:  P_E7_TRIS = tris;   P_E7_OC = oc;	              break;
 		case 8:  if (!S.SDEnable) { P_E8_TRIS = tris; P_E8_OC = oc;}  break;
		case 9:  if (!S.SDEnable) { P_E9_TRIS = tris; P_E9_OC = oc;}  break;
		case 10: if (!S.SDEnable) { P_E10_TRIS = tris; P_E10_OC = oc;} break;
            #endif
	#else
		case 7:  P_E7_TRIS = tris;   P_E7_OC = oc;   P_E7_ANALOG = ana;		break;
		case 8:  P_E8_TRIS = tris;   P_E8_OC = oc;   P_E8_ANALOG = ana;		break;
		case 9:  P_E9_TRIS = tris;   P_E9_OC = oc;   P_E9_ANALOG = ana;		break;
		case 10: P_E10_TRIS = tris;  P_E10_OC = oc;  P_E10_ANALOG = ana;	break;
	#endif
		case 11: P_E11_TRIS = tris;  P_E11_OC = oc;			break;
		case 12: P_E12_TRIS = tris;  P_E12_OC = oc;			break;
		case 13: P_E13_TRIS = tris;  P_E13_OC = oc;			break;
		case 14: P_E14_TRIS = tris;  P_E14_OC = oc;			break;
		case 15: P_E15_TRIS = tris;  P_E15_OC = oc;			break;
		case 16: P_E16_TRIS = tris;  P_E16_OC = oc;			break;
		case 17: P_E17_TRIS = tris;  P_E17_OC = oc;			break;
		case 18: P_E18_TRIS = tris;  P_E18_OC = oc;			break;
	#ifdef OLIMEX
            // SPP +
            #ifdef	OLIMEX_DUINOMITE_EMEGA		// edit for DuinoMite eMega
		case 19: P_E19_TRIS = tris;  P_E19_OC = oc;			break;
		case 20: P_E20_TRIS = tris;  P_E20_OC = oc;                     break;
                case 21: P_E21_TRIS = tris;  P_E21_OC = oc;			break;
		case 22: P_E22_TRIS = tris;  P_E22_OC = oc;			break;
		case 23: P_E23_TRIS = tris;  P_E23_OC = oc;			break;
		case 24: P_E24_TRIS = tris;  P_E24_OC = oc;			break;
		case 25: P_E25_TRIS = tris;  P_E25_OC = oc;			break;
		case 26: P_E26_TRIS = tris;  P_E26_OC = oc;			break;
		case 27: P_E27_TRIS = tris;  P_E27_OC = oc;			break;
		case 28: P_E28_TRIS = tris;  P_E28_OC = oc;			break;
		case 29: P_E29_TRIS = tris;  P_E29_OC = oc;			break;
		case 30: P_E30_TRIS = tris;  P_E30_OC = oc;			break;
		case 31: P_E31_TRIS = tris;  P_E31_OC = oc; P_E31_ANALOG = ana;	break;
		case 32: P_E32_TRIS = tris;  P_E32_OC = oc; P_E31_ANALOG = ana;	break;
		case 33: P_E33_TRIS = tris;  P_E33_OC = oc;			break;
		case 34: P_E34_TRIS = tris;  P_E34_OC = oc;			break;
		case 35: P_E35_TRIS = tris;  P_E35_OC = oc; P_E31_ANALOG = ana; break;
		case 36: P_E36_TRIS = tris;  P_E36_OC = oc;			break;
		case 37: P_E37_TRIS = tris;  P_E37_OC = oc;			break;
		case 38: P_E38_TRIS = tris;  P_E38_OC = oc;			break;
		case 39: P_E39_TRIS = tris;  P_E39_OC = oc;			break;
            #else	// original by Geoff Graham for DuinoMite Mega
		case 19: //if (!S.VideoMode) {
                    P_E19_TRIS = tris; P_E19_OC = oc; P_E19_ANALOG = ana;//}
                break;
		case 20: if (!S.VideoMode || P_VGA_COMP) {P_E20_TRIS = tris; P_E20_OC = oc; P_E20_ANALOG = ana;} break;
		case 21: P_E21_TRIS = tris;  P_E21_OC = oc;  P_E21_ANALOG = ana;		break;
                case 22: P_E22_TRIS = tris;  P_E22_OC = oc; break;
                case 23: P_E23_TRIS = tris;  P_E23_OC = oc; break;
            #endif
            // SPP -
	#else
		case 19: P_E19_TRIS = tris;  P_E19_OC = oc;			break;
		case 20: P_E20_TRIS = tris;  P_E20_OC = oc;			break;
	#endif
	#ifdef UBW32
                case 21: P_E21_TRIS = tris;  P_E21_OC = oc;			break;
		case 22: P_E22_TRIS = tris;  P_E22_OC = oc;			break;
		case 23: P_E23_TRIS = tris;  P_E23_OC = oc;			break;
		case 24: P_E24_TRIS = tris;  P_E24_OC = oc;			break;
		case 25: P_E25_TRIS = tris;  P_E25_OC = oc;			break;
		case 26: P_E26_TRIS = tris;  P_E26_OC = oc;			break;
		case 27: P_E27_TRIS = tris;  P_E27_OC = oc;			break;
		case 28: P_E28_TRIS = tris;  P_E28_OC = oc;			break;
		case 29: P_E29_TRIS = tris;  P_E29_OC = oc;			break;
		case 30: P_E30_TRIS = tris;  P_E30_OC = oc;			break;
		case 31: P_E31_TRIS = tris;  P_E31_OC = oc;			break;
		case 32: P_E32_TRIS = tris;  P_E32_OC = oc;			break;
		case 33: P_E33_TRIS = tris;  P_E33_OC = oc;			break;
		case 34: P_E34_TRIS = tris;  P_E34_OC = oc;			break;
		case 35: P_E35_TRIS = tris;  P_E35_OC = oc;			break;
		case 36: P_E36_TRIS = tris;  P_E36_OC = oc;			break;
		case 37: P_E37_TRIS = tris;  P_E37_OC = oc;			break;
		case 38: P_E38_TRIS = tris;  P_E38_OC = oc;			break;
		case 39: P_E39_TRIS = tris;  P_E39_OC = oc;			break;
		case 40: P_E40_TRIS = tris;  P_E40_OC = oc;			break;
		case 41: P_E41_TRIS = tris;  P_E41_OC = oc;			break;
		case 42: P_E42_TRIS = tris;  P_E42_OC = oc;			break;
		case 43: P_E43_TRIS = tris;  P_E43_OC = oc;			break;
		case 44: P_E44_TRIS = tris;  P_E44_OC = oc;			break;
		case 45: P_E45_TRIS = tris;  P_E45_OC = oc;			break;
		case 46: P_E46_TRIS = tris;  P_E46_OC = oc;			break;
		case 47: P_E47_TRIS = tris;  P_E47_OC = oc;			break;
		case 48: P_E48_TRIS = tris;  P_E48_OC = oc;			break;
		case 49: P_E49_TRIS = tris;  P_E49_OC = oc;			break;
		case 50: P_E50_TRIS = tris;  P_E50_OC = oc;			break;
	#endif
	}

	if (cfg == EXT_NOT_CONFIG) ExtSet(pin, 0);						// set the default output to low
	return true;
}


/****************************************************************************************************************************
Set the output of a digital I/O pin
Returns true if all is OK
*****************************************************************************************************************************/
int ExtSet(int pin, int val){
	val = (val != 0);							// non zero is on
	INTDisableInterrupts();				// setting an output bit is NOT atomic and a bit set operation
																// in an interrupt could result in this set corrupting the output
	switch (pin) {
	#ifdef UBW32
		case 0:  P_LED_OUT = !val;  break;		// this is the LED - the UBW32 wired them upside down !!
	#else
		case 0:  P_LED_OUT = val;  break;			// this is the LED
	#endif
		case 1:  P_E1_OUT = val;   break;
		case 2:  P_E2_OUT = val;   break;
		case 3:  P_E3_OUT = val;   break;
		case 4:	 P_E4_OUT = val;   break;
		case 5:	 P_E5_OUT = val;   break;
		case 6:	 P_E6_OUT = val;   break;
		case 7:	 P_E7_OUT = val;   break;
	#ifdef OLIMEX
            #ifdef  OLIMEX_DUINOMITE_EMEGA
		case 8:	 P_E8_OUT = val;   break;
		case 9:	 P_E9_OUT = val;   break;
		case 10: P_E10_OUT = val;  break;
            #else
		case 8:	 if (!S.SDEnable) P_E8_OUT = val;   break;
		case 9:	 if (!S.SDEnable) P_E9_OUT = val;   break;
		case 10: if (!S.SDEnable) P_E10_OUT = val;  break;
            #endif
	#else
		case 8:	 P_E8_OUT = val;   break;
		case 9:	 P_E9_OUT = val;   break;
		case 10: P_E10_OUT = val;  break;
	#endif
		case 11: P_E11_OUT = val;  break;
		case 12: P_E12_OUT = val;  break;
		case 13: P_E13_OUT = val;  break;
		case 14: P_E14_OUT = val;  break;
		case 15: P_E15_OUT = val;  break;
		case 16: P_E16_OUT = val;  break;
		case 17: P_E17_OUT = val;  break;
		case 18: P_E18_OUT = val;  break;
	#ifdef OLIMEX
            #ifdef  OLIMEX_DUINOMITE_EMEGA
                case 19: P_E19_OUT = val; break;
                case 20: P_E20_OUT = val; break;
            #else
		case 19: P_E19_OUT = val;  break;
		case 20: if (!S.VideoMode || P_VGA_COMP) P_E20_OUT = val;  break;
                case 22: P_E22_OUT = val ; break;
                case 23: P_E23_OUT = val ; break;
            #endif
        #else
		case 19: P_E19_OUT = val;  break;
		case 20: P_E20_OUT = val;  break;
	#endif
        #if defined UBW32 || defined OLIMEX_DUINOMITE_EMEGA
		case 21: P_E21_OUT = val;	break;
		case 22: P_E22_OUT = val;	break;
		case 23: P_E23_OUT = val;	break;
		case 24: P_E24_OUT = val;	break;
		case 25: P_E25_OUT = val;	break;
		case 26: P_E26_OUT = val;	break;
		case 27: P_E27_OUT = val;	break;
		case 28: P_E28_OUT = val;	break;
		case 29: P_E29_OUT = val;	break;
		case 30: P_E30_OUT = val;	break;
		case 31: P_E31_OUT = val;	break;
		case 32: P_E32_OUT = val;	break;
		case 33: P_E33_OUT = val;	break;
		case 34: P_E34_OUT = val;	break;
		case 35: P_E35_OUT = val;	break;
		case 36: P_E36_OUT = val;	break;
		case 37: P_E37_OUT = val;	break;
		case 38: P_E38_OUT = val;	break;
		case 39: P_E39_OUT = val;	break;
        #endif
        #ifdef UBW32
		case 40: P_E40_OUT = val;	break;
		case 41: P_E41_OUT = val;	break;
		case 42: P_E42_OUT = val;	break;
		case 43: P_E43_OUT = val;	break;
		case 44: P_E44_OUT = val;	break;
		case 45: P_E45_OUT = val;	break;
		case 46: P_E46_OUT = val;	break;
		case 47: P_E47_OUT = val;	break;
		case 48: P_E48_OUT = val;	break;
		case 49: P_E49_OUT = val;	break;
		case 50: P_E50_OUT = val;	break;
	#endif
		default:
			INTEnableInterrupts();
			return false;
	}
	INTEnableInterrupts();
	return true;
}
Пример #24
0
/**
 * NOTE: The technique is not the same as that used in TinyVM.
 * The return value indicates the impact of the call on the VM
 * system. EXEC_CONTINUE normal return the system should return to the return
 * address provided by the VM. EXEC_RUN The call has modified the value of
 * VM PC and this should be used to restart execution. EXEC_RETRY The call
 * needs to be re-tried (typically for a GC failure), all global state
 * should be left intact, the PC has been set appropriately.
 *
 */
int dispatch_native(TWOBYTES signature, STACKWORD * paramBase)
{
  STACKWORD p0 = paramBase[0];
  switch (signature) {
  case wait_4_5V:
    return monitor_wait((Object *) word2ptr(p0), 0);
  case wait_4J_5V:
    return monitor_wait((Object *) word2ptr(p0), ((int)paramBase[1] > 0 ? 0x7fffffff : paramBase[2]));
  case notify_4_5V:
    return monitor_notify((Object *) word2ptr(p0), false);
  case notifyAll_4_5V:
    return monitor_notify((Object *) word2ptr(p0), true);
  case start_4_5V:
    // Create thread, allow for instruction restart
    return init_thread((Thread *) word2ptr(p0));
  case yield_4_5V:
    schedule_request(REQUEST_SWITCH_THREAD);
    break;
  case sleep_4J_5V:
    sleep_thread(((int)p0 > 0 ? 0x7fffffff : paramBase[1]));
    schedule_request(REQUEST_SWITCH_THREAD);
    break;
  case getPriority_4_5I:
    push_word(get_thread_priority((Thread *) word2ptr(p0)));
    break;
  case setPriority_4I_5V:
    {
      STACKWORD p = (STACKWORD) paramBase[1];

      if (p > MAX_PRIORITY || p < MIN_PRIORITY)
	return throw_new_exception(JAVA_LANG_ILLEGALARGUMENTEXCEPTION);
      else
	set_thread_priority((Thread *) word2ptr(p0), p);
    }
    break;
  case currentThread_4_5Ljava_3lang_3Thread_2:
    push_ref(ptr2ref(currentThread));
    break;
  case interrupt_4_5V:
    interrupt_thread((Thread *) word2ptr(p0));
    break;
  case interrupted_4_5Z:
    {
      JBYTE i = currentThread->interruptState != INTERRUPT_CLEARED;

      currentThread->interruptState = INTERRUPT_CLEARED;
      push_word(i);
    }
    break;
  case isInterrupted_4_5Z:
    push_word(((Thread *) word2ptr(p0))->interruptState
	      != INTERRUPT_CLEARED);
    break;
  case join_4_5V:
    join_thread((Thread *) word2ptr(p0), 0);
    break;
  case join_4J_5V:
    join_thread((Thread *) word2obj(p0), paramBase[2]);
    break;
  case halt_4I_5V:
    schedule_request(REQUEST_EXIT);
    break;
  case shutdown_4_5V:
    shutdown_program(false);
    break;
  case currentTimeMillis_4_5J:
    push_word(0);
    push_word(systick_get_ms());
    break;
  case readSensorValue_4I_5I:
    push_word(sp_read(p0, SP_ANA));
    break;
  case setPowerTypeById_4II_5V:
    sp_set_power(p0, paramBase[1]);
    break;
  case freeMemory_4_5J:
    push_word(0);
    push_word(getHeapFree());
    break;
  case totalMemory_4_5J:
    push_word(0);
    push_word(getHeapSize());
    break;
  case floatToRawIntBits_4F_5I:	// Fall through
  case intBitsToFloat_4I_5F:
    push_word(p0);
    break;
  case doubleToRawLongBits_4D_5J:	// Fall through
  case longBitsToDouble_4J_5D:
    push_word(p0);
    push_word(paramBase[1]);
    break;
  case drawString_4Ljava_3lang_3String_2II_5V:
    {
      String *p = (String *)word2obj(p0);
      Object *charArray;
      if (!p) return throw_new_exception(JAVA_LANG_NULLPOINTEREXCEPTION);
      charArray = (Object *) word2ptr(get_word_4_ns(fields_start(p)));
      if (!charArray) return throw_new_exception(JAVA_LANG_NULLPOINTEREXCEPTION);
      display_goto_xy(paramBase[1], paramBase[2]);
      display_jstring(p);
    }
    break;
  case drawInt_4III_5V:
    display_goto_xy(paramBase[1], paramBase[2]);
    display_int(p0, 0);
    break;
  case drawInt_4IIII_5V:
     display_goto_xy(paramBase[2], paramBase[3]);
     display_int(p0, paramBase[1]);
    break;   
  case asyncRefresh_4_5V:
    display_update();
    break;
  case clear_4_5V:
    display_clear(0);
    break;
  case getDisplay_4_5_1B:
    push_word(display_get_array());
    break;
  case setAutoRefreshPeriod_4I_5I:
    push_word(display_set_auto_update_period(p0));
    break;
  case getRefreshCompleteTime_4_5I:
    push_word(display_get_update_complete_time());
    break;
  case bitBlt_4_1BIIII_1BIIIIIII_5V:
    {
      Object *src = word2ptr(p0);
      Object *dst = word2ptr(paramBase[5]);
      display_bitblt((byte *)(src != NULL ?jbyte_array(src):NULL), paramBase[1], paramBase[2], paramBase[3], paramBase[4], (byte *)(dst!=NULL?jbyte_array(dst):NULL), paramBase[6], paramBase[7], paramBase[8], paramBase[9], paramBase[10], paramBase[11], paramBase[12]);
      break;
    }
  case getSystemFont_4_5_1B:
    push_word(display_get_font());
    break;
  case setContrast_4I_5V:
    nxt_lcd_set_pot(p0);
    break;
  case getBatteryStatus_4_5I:
    push_word(battery_voltage());
    break;
  case getButtons_4_5I:
    push_word(buttons_get());
    break;
  case getTachoCountById_4I_5I:
    push_word(nxt_motor_get_count(p0));
    break;
  case controlMotorById_4III_5V:
    nxt_motor_set_speed(p0, paramBase[1], paramBase[2]); 
    break;
  case resetTachoCountById_4I_5V:
    nxt_motor_set_count(p0, 0);
    break;
  case i2cEnableById_4II_5V:
    if (i2c_enable(p0, paramBase[1]) == 0)
      return EXEC_RETRY;
    else
      break;
  case i2cDisableById_4I_5V:
    i2c_disable(p0);
    break;
  case i2cStatusById_4I_5I:
    push_word(i2c_status(p0));
    break;
  case i2cStartById_4II_1BIII_5I:
    {
    	Object *p = word2obj(paramBase[2]);
    	JBYTE *byteArray = p ? jbyte_array(p) + paramBase[3] : NULL;
    	push_word(i2c_start(p0,
    	                    paramBase[1],
    	                    (U8 *)byteArray,
    	                    paramBase[4],
    	                    paramBase[5]));
    }
    break; 
  case i2cCompleteById_4I_1BII_5I:
    {
    	Object *p = word2ptr(paramBase[1]);
    	JBYTE *byteArray = p ? jbyte_array(p) + paramBase[2] : NULL;
    	push_word(i2c_complete(p0,
    	                       (U8 *)byteArray,
    	                       paramBase[3]));
    }
    break; 
  case playFreq_4III_5V:
    sound_freq(p0,paramBase[1], paramBase[2]);
    break;
  case btGetBC4CmdMode_4_5I:
    push_word(bt_get_mode());
    break;
  case btSetArmCmdMode_4I_5V:
    if (p0 == 0) bt_set_arm7_cmd();
    else bt_clear_arm7_cmd(); 
    break;
  case btSetResetLow_4_5V:
    bt_set_reset_low();
    break;
  case btSetResetHigh_4_5V:
    bt_set_reset_high();
    break;
  case btWrite_4_1BII_5I:
    {
      Object *p = word2ptr(p0);
      byte *byteArray = (byte *) jbyte_array(p);
      push_word(bt_write(byteArray, paramBase[1], paramBase[2]));                      
    }
    break;
  case btRead_4_1BII_5I:
    {
      Object *p = word2ptr(p0);
      byte *byteArray = (byte *) jbyte_array(p);
      push_word(bt_read(byteArray, paramBase[1], paramBase[2]));                      
    }
    break;
  case btPending_4_5I:
    {
      push_word(bt_event_check(0xffffffff));
    }
    break;
  case btEnable_4_5V:
    if (bt_enable() == 0)
      return EXEC_RETRY;
    else
      break;
  case btDisable_4_5V:
    bt_disable();
    break;
  case usbRead_4_1BII_5I:
     {
      Object *p = word2ptr(p0);
      byte *byteArray = (byte *) jbyte_array(p);
      push_word(udp_read(byteArray,paramBase[1], paramBase[2]));
    } 
    break;
  case usbWrite_4_1BII_5I:
     {
      Object *p = word2ptr(p0);
      byte *byteArray = (byte *) jbyte_array(p);
      push_word(udp_write(byteArray,paramBase[1], paramBase[2]));                      
    }
    break; 
  case usbStatus_4_5I:
    {
      push_word(udp_event_check(0xffffffff));
    }
    break;
  case usbEnable_4I_5V:
    {
      udp_enable(p0);
    }
    break;
  case usbDisable_4_5V:
    {
      udp_disable();
    }
    break;
  case usbReset_4_5V:
    udp_reset();
    break; 
  case usbSetSerialNo_4Ljava_3lang_3String_2_5V: 
    {
      byte *p = word2ptr(p0);
      int len;
      Object *charArray = (Object *) word2ptr(get_word_4_ns(fields_start(p)));

      len = get_array_length(charArray);
      udp_set_serialno((U8 *)jchar_array(charArray), len);
    }
    break;
  case usbSetName_4Ljava_3lang_3String_2_5V:
    {
      byte *p = word2ptr(p0);
      int len;
      Object *charArray = (Object *) word2ptr(get_word_4_ns(fields_start(p)));

      len = get_array_length(charArray);
      udp_set_name((U8 *)jchar_array(charArray), len);
    }
    break;
  case flashWritePage_4_1BI_5I:
    {
      Object *p = word2ptr(p0);
      unsigned long *intArray = (unsigned long *) jint_array(p);
      push_word(flash_write_page(intArray,paramBase[1]));                      
    }
    break;
  case flashReadPage_4_1BI_5I:
    {
      Object *p = word2ptr(p0);
      unsigned long *intArray = (unsigned long *) jint_array(p);
      push_word(flash_read_page(intArray,paramBase[1]));                      
    }
    break;
  case flashExec_4II_5I:
    push_word(run_program((byte *)(&FLASH_BASE[(p0*FLASH_PAGE_SIZE)]), paramBase[1]));
    break;
  case playSample_4IIIII_5V:
    sound_play_sample(((unsigned char *) &FLASH_BASE[(p0*FLASH_PAGE_SIZE)]) + paramBase[1],paramBase[2],paramBase[3],paramBase[4]);
    break;
  case playQueuedSample_4_1BIIII_5I:
    push_word(sound_add_sample((U8 *)jbyte_array(word2obj(p0)) + paramBase[1],paramBase[2],paramBase[3],paramBase[4]));
    break;
  case getTime_4_5I:
    push_word(sound_get_time());
    break;
  case getDataAddress_4Ljava_3lang_3Object_2_5I:
    if (is_array(word2obj(p0)))
      push_word (ptr2word ((byte *) array_start(word2ptr(p0))));
    else
      push_word (ptr2word ((byte *) fields_start(word2ptr(p0))));
    break;
  case getObjectAddress_4Ljava_3lang_3Object_2_5I:
    push_word(p0);
    break;
  case gc_4_5V:
    // Restartable garbage collection
    return garbage_collect();
  case shutDown_4_5V:
    shutdown(); // does not return
  case boot_4_5V:
    display_clear(1);
    while (1) nxt_avr_firmware_update_mode(); // does not return 
  case arraycopy_4Ljava_3lang_3Object_2ILjava_3lang_3Object_2II_5V:
    return arraycopy(word2ptr(p0), paramBase[1], word2ptr(paramBase[2]), paramBase[3], paramBase[4]);
  case executeProgram_4I_5V:
    // Exceute program, allow for instruction re-start
    return execute_program(p0);
  case setDebug_4_5V:
    set_debug(word2ptr(p0));
    break;
  case eventOptions_4II_5I:
    {
      byte old = debugEventOptions[p0];
      debugEventOptions[p0] = (byte)paramBase[1];
      push_word(old);
    }
    break;
  case suspendThread_4Ljava_3lang_3Object_2_5V:
    suspend_thread(ref2ptr(p0));
    break;
  case resumeThread_4Ljava_3lang_3Object_2_5V:
    resume_thread(ref2ptr(p0));
    break;
  case getProgramExecutionsCount_4_5I:
    push_word(gProgramExecutions);
    break;
  case getFirmwareRevision_4_5I:
    push_word((STACKWORD) getRevision());
    break;
  case getFirmwareRawVersion_4_5I:
    push_word((STACKWORD) VERSION_NUMBER); 
    break;
  case hsEnable_4II_5V:
    {
      if (hs_enable((int)p0, (int)paramBase[1]) == 0)
        return EXEC_RETRY;
    }
    break;
  case hsDisable_4_5V:
    {
      hs_disable();
    }
    break;
  case hsWrite_4_1BII_5I:
    {
      Object *p = word2ptr(p0);
      byte *byteArray = (byte *) jbyte_array(p);
      push_word(hs_write(byteArray, paramBase[1], paramBase[2]));                      
    }
    break;
  case hsRead_4_1BII_5I:
    {
      Object *p = word2ptr(p0);
      byte *byteArray = (byte *) jbyte_array(p);
      push_word(hs_read(byteArray, paramBase[1], paramBase[2]));                      
    }
    break;
  case hsPending_4_5I:
    {
      push_word(hs_pending());
    }
    break;
  case hsSend_4BB_1BII_1C_5I:
    {
      Object *p = word2ptr(paramBase[2]);
      U8 *data = (U8 *)jbyte_array(p);
      p = word2ptr(paramBase[5]);
      U16 *crc = (U16 *)jchar_array(p);
      push_word(hs_send((U8) p0, (U8)paramBase[1], data, paramBase[3], paramBase[4], crc));
    }
    break;
  case hsRecv_4_1BI_1CI_5I:
    {
      Object *p = word2ptr(p0);
      U8 *data = (U8 *)jbyte_array(p);
      p = word2ptr(paramBase[2]);
      U16 *crc = (U16 *)jchar_array(p);
      push_word(hs_recv(data, paramBase[1], crc, paramBase[3]));
    }
    break;
    
  case getUserPages_4_5I:
    push_word(FLASH_MAX_PAGES - flash_start_page);
    break;
  case setVMOptions_4I_5V:
    gVMOptions = p0;
    break;
  case getVMOptions_4_5I:
    push_word(gVMOptions);
    break;
  case isAssignable_4II_5Z:
    push_word(is_assignable(p0, paramBase[1]));
    break;
  case cloneObject_4Ljava_3lang_3Object_2_5Ljava_3lang_3Object_2:
    {
      Object *newObj = clone((Object *)ref2obj(p0));
      if (newObj == NULL) return EXEC_RETRY;
      push_word(obj2ref(newObj));
    }
    break;
  case memPeek_4III_5I:
    push_word(mem_peek(p0, paramBase[1], paramBase[2]));
    break;
  case memCopy_4Ljava_3lang_3Object_2IIII_5V:
    mem_copy(word2ptr(p0), paramBase[1], paramBase[2], paramBase[3], paramBase[4]);
    break;
  case memGetReference_4II_5Ljava_3lang_3Object_2:
    push_word(mem_get_reference(p0, paramBase[1]));
    break;
  case setSensorPin_4III_5V:
    sp_set(p0, paramBase[1], paramBase[2]);
    break;
  case getSensorPin_4II_5I:
    push_word(sp_get(p0, paramBase[1]));
    break;
  case setSensorPinMode_4III_5V:
    sp_set_mode(p0, paramBase[1], paramBase[2]);
    break;
  case readSensorPin_4II_5I:
    push_word(sp_read(p0, paramBase[1]));
    break;
  case nanoTime_4_5J:
    {
      U64 ns = systick_get_ns();
      push_word(ns >> 32);
      push_word(ns);
    }
    break;
  case createStackTrace_4Ljava_3lang_3Thread_2Ljava_3lang_3Object_2_5_1I:
    {
      Object *trace = create_stack_trace((Thread *)ref2obj(p0), ref2obj(paramBase[1]));
      if (trace == NULL) return EXEC_RETRY;
      push_word(obj2ref(trace));
    }
    break;
  case registerEvent_4_5I:
    push_word(register_event((NXTEvent *) ref2obj(p0)));
    break;
  case unregisterEvent_4_5I:
    push_word(unregister_event((NXTEvent *) ref2obj(p0)));
    break;
  case changeEvent_4II_5I:
    push_word(change_event((NXTEvent *) ref2obj(p0), paramBase[1], paramBase[2]));
    break;
  case isInitialized_4I_5Z:
    push_word(is_initialized_idx(p0));
    break;
  case allocate_4II_5Ljava_3lang_3Object_2:
    {
      Object *allocated;
      if(paramBase[1]>0){
        allocated=new_single_array(p0,paramBase[1]);
      }else{
        allocated=new_object_for_class(p0);
      }
      if(allocated == NULL) return EXEC_RETRY;
      push_word(obj2ref(allocated));
    }
    break;
  case memPut_4IIII_5V:
    store_word_ns((byte *)(memory_base[p0] + paramBase[1]), paramBase[2],paramBase[3]);
    break;
  case notifyEvent_4ILjava_3lang_3Thread_2_5Z:
    push_word(debug_event(paramBase[1], NULL, (Thread*) ref2obj(paramBase[2]), 0, 0, 0, 0));
    break;
  case setThreadRequest_4Ljava_3lang_3Thread_2Llejos_3nxt_3debug_3SteppingRequest_2_5V:
    {
      Thread *th = (Thread*) ref2obj(p0);
      th->debugData = (REFERENCE) paramBase[1];
      // currently we only get stepping requests
      if(paramBase[1])
        th->flags |= THREAD_STEPPING;
      else
        th->flags &= ~THREAD_STEPPING;
    }
    break;
  case isStepping_4Ljava_3lang_3Thread_2_5Z:
    {
      Thread *th = (Thread*) ref2obj(p0);
      push_word(is_stepping(th));
    }
    break;
  case setBreakpointList_4_1Llejos_3nxt_3debug_3Breakpoint_2I_5V:
    breakpoint_set_list((Breakpoint**) array_start(p0), paramBase[1]);
    break;
  case enableBreakpoint_4Llejos_3nxt_3debug_3Breakpoint_2Z_5V:
    breakpoint_enable((Breakpoint*) word2ptr(p0), (boolean) paramBase[1]);
    break;
  case firmwareExceptionHandler_4Ljava_3lang_3Throwable_2II_5V:
    firmware_exception_handler((Throwable *)p0, paramBase[1], paramBase[2]);
    break;
  case exitThread_4_5V:
    currentThread->state = DEAD;
    schedule_request(REQUEST_SWITCH_THREAD);
    break;
  case updateThreadFlags_4Ljava_3lang_3Thread_2II_5I:
    ((Thread *)p0)->flags |= paramBase[1];
    ((Thread *)p0)->flags &= ~paramBase[2];
//printf("m %x %d\n", p0, ((Thread *)p0)->flags);
    push_word(((Thread *)p0)->flags);
    break;
    
  default:
    return throw_new_exception(JAVA_LANG_NOSUCHMETHODERROR);
  }
  return EXEC_CONTINUE;
}
Пример #25
0
 uint16_t read_pressure_barometer_MPL115A2(){
	 uint16_t press = 0;
	 uint16_t temp = 0;
	 float press_comp = 0;
	 uint8_t res = 0;
	 	 BAR_POWER_UP();
	 	 i2c_enable();

	 	 i2c_start();
	 	 i2c_write((DEVICE_ADDRESS | WRITE));
	 	 i2c_write((START_CONVERSION_COMMAND));
	 	 i2c_write((0x01)); // trovato su un forum di uno spagnolo
	 	 i2c_stop();
	 	 //delay 1.6 ms
	 	 __delay_cycles(1662500);

	 	 // read temperature first

	 	i2c_start();
	 	i2c_write((DEVICE_ADDRESS | WRITE));
	 	i2c_write((READ_TEMPERATURE_MSB_COMMAND));

	 	i2c_start();
	 	i2c_write((DEVICE_ADDRESS | READ));
        res = i2c_read(1);
	 	//printf("temp MSB %x\n", res);
	 	temp = (res << 8);
	 	res = i2c_read(0);
	 	temp += res;
	 	temp = temp >> 6;

	 	//printf("temp is %d\n", temp);

	 	i2c_stop();

	 	i2c_start();
	 	i2c_write((DEVICE_ADDRESS | WRITE));
	 	i2c_write((READ_PRESSURE_MSB_COMMAND));

	 	 i2c_start();
	 	 i2c_write((DEVICE_ADDRESS | READ));

	 	 res = i2c_read(1);
	 	 //printf("temp MSB %x\n", res);
	 	 press = (res << 8);
	 	 res = i2c_read(0);
	 	 press += res;
	 	 press = press >> 6;

	 	 i2c_stop();
	 	 i2c_disable();
	 	 BAR_POWER_DOWN();

	 	//printf("press is %d\n", press);

	 	press_comp = _mpl115a2_a0 + (_mpl115a2_b1 + _mpl115a2_c12 * temp ) * press + _mpl115a2_b2 * temp;

	 	// hPa
	 	 return (uint16_t)((((65.0F / 1023.0F) * press_comp) + 50.0F)*10);

 }
Пример #26
0
/*#---------------------------------------------------------------------------
 *#
 *# FUNCTION NAME: i2c_getack
 *#
 *# DESCRIPTION  : checks if ack was received from ic2
 *#
 *# PARAMETERS   : none
 *#
 *# RETURN       : returns the ack state of the I2C device
 *#
 *#---------------------------------------------------------------------------
 */
int i2c_getack( void )
{
    int ack = 1;
#ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
    int n=MAXSCLRETRIES;
#endif

    /* generate ACK clock pulse */
    i2c_set_scl( SCL_HIGH );

    /* switch off I2C */
    i2c_disable();

#ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
    /* set clock low */
    i2c_set_scl( SCL_LOW );

    /* now wait for ack */
    i2c_delay( THIGH );

    /* set clock as input */
    i2c_scl_dir_in();

    /* wait for clock to rise (n=MAXSCLRETRIES) */
    for( ; n>0; n-- )
    {
	if( i2c_scl_is_high() )
            break;
	i2c_delay( THIGH );
    }

    i2c_set_scl( SCL_HIGH );

    i2c_scl_dir_out();

    i2c_delay( THIGH );
#else
    /* now wait for ack */
    i2c_delay( THIGH );
#endif

    /* check for ack: if SDA is high, then NACK, else ACK */
    if ( i2c_sda_is_high() )
    {
        ack = 0;
    }
    else
    {
        ack = 1;
    }

    /* end clock pulse */
    i2c_enable();
    i2c_set_scl( SCL_LOW );
    i2c_sda_dir_out();
    i2c_set_sda( SDA_LOW );

    /* Since we "lost" already THDDAT time, we can subtract it here... */
    i2c_delay( TLOW  - THDDAT );

    return ( ack );
}   /* i2c_getack */
Пример #27
0
HardWire::~HardWire() {
    i2c_disable(sel_hard);
    sel_hard = 0;
}
Пример #28
0
void iotlab_i2c_slave_stop()
{
    i2c_disable(I2C_2);
    _state.handlers_list.next = NULL;  // remove handlers
}
Пример #29
0
Файл: i2c_lpc.c Проект: Ttl/vna
void i2c_lpc_stop(i2c_bus_t* const bus) {
	const uint32_t port = (uint32_t)bus->obj;
	i2c_disable(port);
}
Пример #30
0
/*
 * Terminate I2C communication on the specified port
 */
void term_genericI2C_sensor(U8 port_id)
{
	i2c_disable(port_id);
}