Ejemplo n.º 1
0
uint8_t TwoWire::endTransmission(uint8_t sendStop) {
	vm_i2c_ctrl_config_t conf_data;
	vm_i2c_ctrl_single_write_t write_data;
	VM_DCL_STATUS ret = 0;
	int32_t sent = 0;

	if(i2c_handle==-1)
	{
		vm_log_info("endTransmission,i2c handle is invalid.i2c_handle:%d",i2c_handle);
		return 0;
	}
    
    if(0 < txBufferLength) {
		conf_data.Reserved0 =  (VM_DCL_I2C_OWNER)0;
		conf_data.eTransactionMode = VM_DCL_I2C_TRANSACTION_FAST_MODE;
		conf_data.fgGetHandleWait = 0;
		conf_data.Reserved1 = 0;
		conf_data.u1DelayLen = 0;
		conf_data.u1SlaveAddress = txAddress;

		conf_data.u4FastModeSpeed = 100;
		conf_data.u4HSModeSpeed = 0;
		ret = vm_dcl_control(i2c_handle,VM_I2C_CMD_CONFIG,(void *)&conf_data);
	
		write_data.pu1Data = &(txBuffer[0]);
		write_data.u4DataLen = txBufferLength;
        ret = vm_dcl_control(i2c_handle,VM_I2C_CMD_SINGLE_WRITE,(void *)&write_data);
		sent = txBufferLength;
	}
  
	txBufferLength = 0;

	status = MASTER_IDLE;
	return sent;
}
Ejemplo n.º 2
0
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop) {

	// perform blocking read into buffer
	int readed = 0;
	VM_DCL_STATUS ret = 0;
	vm_i2c_ctrl_config_t conf_data;
	vm_i2c_ctrl_single_read_t  read_data;
	
	if(i2c_handle==-1)
	{
		vm_log_info("requestFrom,i2c handle is invalid.i2c_handle:%d",i2c_handle);
		return 0;
	}
	
	if (quantity > BUFFER_LENGTH)
		quantity = BUFFER_LENGTH;
	conf_data.Reserved0 = (VM_DCL_I2C_OWNER)0;
	conf_data.eTransactionMode = VM_DCL_I2C_TRANSACTION_FAST_MODE;
	conf_data.fgGetHandleWait = 0;
	conf_data.Reserved1 = 0;
	conf_data.u1DelayLen = 0;
	conf_data.u1SlaveAddress = address<<1;
	conf_data.u4FastModeSpeed = 100;
	conf_data.u4HSModeSpeed = 0;
	ret = vm_dcl_control(i2c_handle,VM_I2C_CMD_CONFIG,(void *)&conf_data);
	read_data.pu1Data = &(rxBuffer[0]);
	read_data.u4DataLen = quantity;
	ret = vm_dcl_control(i2c_handle,VM_I2C_CMD_SINGLE_READ,(void *)&read_data);
	rxBufferIndex = 0;
	rxBufferLength = quantity;

	return quantity;
}
Ejemplo n.º 3
0
/* AT command callback, which will be invoked when you send AT command from monitor tool */
static void at_callback(vm_cmd_command_t *param, void *user_data)
{
  vm_dcl_pmu_ld0_buck_enable_t val;

  if(strcmp("Test01",(char*)param->command_buffer) == 0)
  {
    /* enable vibrator LDO when receive AT command: AT+[1000]Test01 */
	/* vibrator will on */
    g_pmu_handle = vm_dcl_open(VM_DCL_PMU, 0);
    val.enable = TRUE;
    val.module = VM_DCL_PMU_VIBR;
    vm_dcl_control(g_pmu_handle, VM_DCL_PMU_CONTROL_LDO_BUCK_SET_ENABLE, (void *)&val);
    vm_dcl_close(g_pmu_handle);

  }
  else if(strcmp("Test02",(char*)param->command_buffer) == 0)
  {
    /* disable vibrator LDO when receive AT command: AT+[1000]Test02 */
	/* vibrator will off */
    g_pmu_handle = vm_dcl_open(VM_DCL_PMU, 0);
    val.enable = FALSE;
    val.module = VM_DCL_PMU_VIBR;
    vm_dcl_control(g_pmu_handle, VM_DCL_PMU_CONTROL_LDO_BUCK_SET_ENABLE, (void *)&val);
    vm_dcl_close(g_pmu_handle);

  }
}
extern void digitalWrite( uint32_t ulPin, uint32_t ulVal )
{   
    //vm_log_info("digitalWrite(): pin = %d , value = %d", ulPin, ulVal);
    
    if (ulPin > PIO_MAX_NUM )
    {
        return;
    }
    
    // write PIN
    switch (ulVal)
    {
        case HIGH:
            vm_dcl_control(g_APinDescription[ulPin].ulHandle,VM_DCL_GPIO_COMMAND_WRITE_HIGH, NULL);   
            break;
            
        case LOW:
            vm_dcl_control(g_APinDescription[ulPin].ulHandle,VM_DCL_GPIO_COMMAND_WRITE_LOW, NULL);  
            break;
            
        default:
            break;
    }
    
}
Ejemplo n.º 5
0
extern void pinMode( uint32_t ulPin, uint32_t ulMode )
{  
	VM_DCL_HANDLE gpio_handle; 
    vm_gpio_ctrl_set_pupd_r0_r1_t pupd;
    //vm_log_info("pinMode(): pin = %d , pin_internal = %d, mode = %d", ulPin, g_APinDescription[ulPin].ulGpioId, ulMode);
    
    if(!changePinType(ulPin, PIO_DIGITAL, &gpio_handle))
		return;

	if(gpio_handle == VM_DCL_HANDLE_INVALID)
		gpio_handle = vm_dcl_open(VM_DCL_GPIO, g_APinDescription[ulPin].ulGpioId);
    
    // set PIN direction
    switch ( ulMode )
    {
        case INPUT:
            vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_DIR_IN, NULL);
			
	     if( g_APinDescription[ulPin].ulPupd)
	     {
	       pupd.fgSetPupd = 0;
	       pupd.fgSetR0 = 1;
		pupd.fgSetR1 = 0;
            	vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_PUPD_R0_R1, &pupd);
	     }
	     else
	     {
            	vm_dcl_control(gpio_handle,VM_GPIO_CMD_ENABLE_PULL, NULL);
            	vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_PULL_LOW, NULL);
	     }
            break ;
        
        case INPUT_PULLUP:
            vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_DIR_IN, NULL);   
			
	     if( g_APinDescription[ulPin].ulPupd)
	     {
	       pupd.fgSetPupd = 1;
	       pupd.fgSetR0 = 1;
		pupd.fgSetR1 = 0;
            	vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_PUPD_R0_R1, &pupd);
	     }
	     else
	     {
            	vm_dcl_control(gpio_handle,VM_GPIO_CMD_ENABLE_PULL, NULL);
            	vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_PULL_HIGH, NULL);      
	     }
            break ;
        
        case OUTPUT:
            vm_dcl_control(gpio_handle,VM_GPIO_CMD_SET_DIR_OUT, NULL);
            break ;
        
        default:
            break ;
    }
    
    g_APinDescription[ulPin].ulHandle = gpio_handle;
    
}
Ejemplo n.º 6
0
ADC_HANDLE open_hx711(int scl_pin, int sda_pin)
{
	ADC_HANDLE handle = 0;
	handle_details* details;
	hx711_task* task;

	while (open_handles[handle] != NULL)
	{
		handle++;
		if (handle > MAX_HANDLE)
		{
			return ADC_HANDLE_INVALID;
		}
	}

	details = vm_calloc(sizeof(handle_details));
	if (details == NULL)
	{
		return ADC_HANDLE_INVALID;
	}

	task = &details->task;
	task->scl_handle = vm_dcl_open(VM_DCL_GPIO, scl_pin);
	if (task->scl_handle == VM_DCL_HANDLE_INVALID)
	{
		vm_free(details);
		return ADC_HANDLE_INVALID;
	}
	vm_dcl_control(task->scl_handle, VM_DCL_GPIO_COMMAND_SET_MODE_0, NULL);
	vm_dcl_control(task->scl_handle, VM_DCL_GPIO_COMMAND_SET_DIRECTION_OUT, NULL);
	vm_dcl_control(task->scl_handle, VM_DCL_GPIO_COMMAND_WRITE_HIGH, NULL);

	task->sda_handle = vm_dcl_open(VM_DCL_GPIO, sda_pin);
	if (task->sda_handle == VM_DCL_HANDLE_INVALID)
	{
		vm_dcl_close(task->scl_handle);
		vm_free(details);
		return ADC_HANDLE_INVALID;
	}
	vm_dcl_control(task->sda_handle, VM_DCL_GPIO_COMMAND_SET_MODE_0, NULL);
	vm_dcl_control(task->sda_handle, VM_DCL_GPIO_COMMAND_SET_DIRECTION_IN, NULL);

	vm_mutex_init(&details->mutex);
	task->op = WAIT;
	task->delay = 100;
	task->callback = dummy_callback;
	task->callback_env = NULL;
	open_handles[handle] = details;
	write_console("open\n");
	vm_thread_create(measurement, (void*) details, (VM_THREAD_PRIORITY) 0);
	return handle;
}
Ejemplo n.º 7
0
static VM_DRV_TP_BOOL ctp_goodix_gt9xx_set_device_mode(
		VM_DRV_TP_DEVICE_MODE mode) {
	VMUINT8 suspend_command = 0x05;
	VM_DCL_HANDLE eint_handle;
	ctp_info_t ctp_info;
	VMUINT8 ctp_buffer[2] = { 0 };

	if (mode == VM_DRV_TP_ACTIVE_MODE) {
		eint_handle = vm_dcl_open(VM_DCL_GPIO, gpio_ctp_eint_pin);
		vm_dcl_control(eint_handle, VM_DCL_GPIO_COMMAND_SET_MODE_0, NULL);
		vm_dcl_control(eint_handle, VM_DCL_GPIO_COMMAND_SET_DIRECTION_OUT,
				NULL);
		vm_dcl_control(eint_handle, VM_DCL_GPIO_COMMAND_WRITE_HIGH, NULL);
		delay_ms(15);
		vm_dcl_control(eint_handle, VM_DCL_GPIO_COMMAND_SET_MODE_2, NULL);
		vm_dcl_control(eint_handle, VM_DCL_GPIO_COMMAND_SET_DIRECTION_IN, NULL);
		vm_dcl_close(eint_handle);
	} else if ((mode == VM_DRV_TP_SLEEP_MODE)) {
		eint_handle = vm_dcl_open(VM_DCL_GPIO, gpio_ctp_eint_pin);
		vm_dcl_control(eint_handle, VM_DCL_GPIO_COMMAND_SET_MODE_0, NULL);
		vm_dcl_control(eint_handle, VM_DCL_GPIO_COMMAND_SET_DIRECTION_OUT,
				NULL);
		vm_dcl_control(eint_handle, VM_DCL_GPIO_COMMAND_WRITE_LOW, NULL);
		delay_ms(5);
		CTP_I2C_send(CTP_POWER_MODE_REG, &suspend_command, 1);
		CTP_I2C_read(CTP_POWER_MODE_REG, ctp_buffer, 1);
		vm_dcl_close(eint_handle);
	} else if (mode == VM_DRV_TP_FIRMWARE_UPDATE) {
		// need wdt
	}

	return VM_DRV_TP_FALSE;

}
Ejemplo n.º 8
0
// Lua: read = i2c.recv(size)
static v7_val_t i2c_recv(struct v7 *v7)
{
    uint32_t size = luaL_checkinteger(L, 1);
    int i;
    luaL_Buffer b;
    vm_dcl_i2c_control_continue_read_t param;
    VM_DCL_STATUS status;
    char rbuf[8];

    if(size == 0)
        return 0;

    param.data_ptr = rbuf;

    luaL_buffinit(L, &b);

    do {
        if(size >= 8) {
            param.data_length = 8;
            param.transfer_number = 8;
            status = vm_dcl_control(g_i2c_handle, VM_DCL_I2C_CMD_CONT_READ, &param);
            if(status != VM_DCL_STATUS_OK) {
                break;
            }

            for(i = 0; i < 8; i++) {
                luaL_addchar(&b, rbuf[i]);
            }

            size -= 8;
        } else {
            param.data_length = size;
            param.transfer_number = size;
            status = vm_dcl_control(g_i2c_handle, VM_DCL_I2C_CMD_CONT_READ, &param);
            if(status != VM_DCL_STATUS_OK) {
                break;
            }

            for(i = 0; i < size; i++) {
                luaL_addchar(&b, rbuf[i]);
            }

            size = 0;
        }
    } while(size > 0);
    luaL_pushresult(&b);
    return 1;
}
extern int digitalRead( uint32_t ulPin )
{
    vm_dcl_gpio_control_level_status_t  data;

	//vm_log_info("digitalRead(): pin = %d", ulPin);

	if (ulPin > PIO_MAX_NUM )
    {
        //vm_log_info("digitalRead(): pin mapping failed, return LOW as default.");
        return LOW;
    }
	 
    vm_dcl_control(g_APinDescription[ulPin].ulHandle, VM_DCL_GPIO_COMMAND_READ,(void *)&data);
   
    if ( data.level_status == VM_DCL_GPIO_IO_LOW )
    {
        //vm_log_info("digitalRead(): result = LOW");
        return LOW ;
    }
    else if ( data.level_status == VM_DCL_GPIO_IO_HIGH )
    {
        //vm_log_info("digitalRead(): result = HIGH");
        return HIGH ;
    }
    else
    {
        //vm_log_info("digitalRead(): read failed, return LOW as default.");
        return LOW ;
    }
}
Ejemplo n.º 10
0
extern int digitalRead( uint32_t ulPin )
{
    vm_gpio_ctrl_read_t  data;

	//vm_log_info("digitalRead(): pin = %d", ulPin);

	if (ulPin > PIO_MAX_NUM )
    {
        //vm_log_info("digitalRead(): pin mapping failed, return LOW as default.");
        return LOW;
    }

    if(g_APinDescription[ulPin].ulHandle == VM_DCL_HANDLE_INVALID)
    {
        g_APinDescription[ulPin].ulHandle = vm_dcl_open(VM_DCL_GPIO, g_APinDescription[ulPin].ulGpioId);
    }
	 
    vm_dcl_control(g_APinDescription[ulPin].ulHandle, VM_GPIO_CMD_READ,(void *)&data);
   
    if ( data.u1IOData == VM_GPIO_IO_LOW )
    {
        //vm_log_info("digitalRead(): result = LOW");
        return LOW ;
    }
    else if ( data.u1IOData == VM_GPIO_IO_HIGH )
    {
        //vm_log_info("digitalRead(): result = HIGH");
        return HIGH ;
    }
    else
    {
        //vm_log_info("digitalRead(): read failed, return LOW as default.");
        return LOW ;
    }
}
VM_DCL_STATUS vm_dcl_config_pin_mode(VMUINT pin, VM_DCL_PIN_MODE mode)
{
	VM_DCL_HANDLE gpio_handle;
	VMINT i, j;
	VM_DCL_STATUS status = VM_DCL_STATUS_FAIL;

	for(i = 0; i<VM_DCL_PIN_TABLE_SIZE; i++)
	{
		if(pinTable[i].gpio == pin)
		{
			break;
		}
	}
	if(i >=VM_DCL_PIN_TABLE_SIZE)
		return status;
	
	gpio_handle = vm_dcl_open(VM_DCL_GPIO,pin);

	for(j = 0; j<VM_DCL_PIN_MODE_MAX; j++)
	{
		if(pinTable[i].mux[j] == mode)
		{
			vm_dcl_control(gpio_handle,4+j,NULL);
			status = VM_DCL_STATUS_OK;
			break;
		}
	}
	vm_dcl_close(gpio_handle); 
	return status;
}
Ejemplo n.º 12
0
static int read_bit(hx711_task* task)
{
	vm_dcl_gpio_control_level_status_t pin_status;

	vm_dcl_control(task->scl_handle, VM_DCL_GPIO_COMMAND_WRITE_HIGH, NULL);
	vm_dcl_control(task->sda_handle, VM_DCL_GPIO_COMMAND_READ, &pin_status);
	vm_dcl_control(task->scl_handle, VM_DCL_GPIO_COMMAND_WRITE_LOW, NULL);
	if (pin_status.level_status == VM_DCL_GPIO_IO_LOW)
	{
		return 0;
	}
	else
	{
		return 1;
	} 
}
Ejemplo n.º 13
0
void adc_callback(void* parameter, VM_DCL_EVENT event, VM_DCL_HANDLE device_handle)
{
    vm_dcl_callback_data_t *data;
    vm_dcl_adc_measure_done_confirm_t * result;
    vm_dcl_adc_control_send_stop_t stop_data;
    VMINT status = 0;

    if(parameter!=NULL)
      {
        data = ( vm_dcl_callback_data_t*)parameter;
        result = (vm_dcl_adc_measure_done_confirm_t *)(data->local_parameters);

        if( result != NULL )
        {
            double *p;

            p =(double*)&(result->value);
            g_adc_result = (unsigned int)*p;
            vm_log_info("get adc data is %d",g_adc_result);
        }
     }

    // Stop ADC
    stop_data.owner_id = vm_dcl_get_owner_id();
    status = vm_dcl_control(g_adc_handle,VM_DCL_ADC_COMMAND_SEND_STOP,(void *)&stop_data);

    vm_dcl_close(g_adc_handle);
}
void lcd_backlight_level(VMUINT32 ulValue)
{
    VM_DCL_HANDLE pwm_handle;
    vm_dcl_pwm_set_clock_t pwm_clock;
    vm_dcl_pwm_set_counter_threshold_t pwm_config_adv;
    vm_dcl_config_pin_mode(3, VM_DCL_PIN_MODE_PWM);
    pwm_handle = vm_dcl_open(PIN2PWM(3), vm_dcl_get_owner_id());
    vm_dcl_control(pwm_handle, VM_PWM_CMD_START, 0);
    pwm_config_adv.counter = 100;
    pwm_config_adv.threshold = ulValue;
    pwm_clock.source_clock = 0;
    pwm_clock.source_clock_division = 3;
    vm_dcl_control(pwm_handle, VM_PWM_CMD_SET_CLOCK, (void *) (&pwm_clock));
    vm_dcl_control(pwm_handle, VM_PWM_CMD_SET_COUNTER_AND_THRESHOLD,
            (void *) (&pwm_config_adv));
    vm_dcl_close(pwm_handle);
}
Ejemplo n.º 15
0
VMINT32 analogRead(VMUINT8 ulPin)
{
	vm_dcl_adc_control_create_object_t obj_data;
	VMINT status = 0 , i;
	vm_dcl_adc_control_send_start_t start_data;

	if(ulPin > 3)// for ADC0 ADC1 ADC2 ADC3
	{
		vm_log_info("ADC pin number is error");
		return -1;
	}

	vm_dcl_close(g_adc_handle);

	g_adc_handle = vm_dcl_open(VM_DCL_GPIO, ulPin);

	vm_dcl_control(g_adc_handle,VM_DCL_GPIO_COMMAND_SET_MODE_2,NULL);
	vm_dcl_close(g_adc_handle);

	// Open ADC device
	g_adc_handle = vm_dcl_open(VM_DCL_ADC,0);
	// register ADC result callback
	status = vm_dcl_register_callback(g_adc_handle, VM_DCL_ADC_GET_RESULT ,(vm_dcl_callback)adc_callback, (void *)NULL);

	// Indicate to the ADC module driver to notify the result.
	obj_data.owner_id = vm_dcl_get_owner_id();
	// Set physical ADC channel which should be measured.
	obj_data.channel = PIN2CHANNEL(ulPin);
	// Set measurement period, the unit is in ticks.
	obj_data.period = 1;
	// Measurement count.
	obj_data.evaluate_count = 1;
	// Whether to send message to owner module or not.
	obj_data.send_message_primitive = 1;

	// setup ADC object
	status = vm_dcl_control(g_adc_handle,VM_DCL_ADC_COMMAND_CREATE_OBJECT,(void *)&obj_data);

	// start ADC
	start_data.owner_id = vm_dcl_get_owner_id();
	status = vm_dcl_control(g_adc_handle,VM_DCL_ADC_COMMAND_SEND_START,(void *)&start_data);

	return g_adc_result;
}
Ejemplo n.º 16
0
void key_init(void){
    VM_DCL_HANDLE kbd_handle;
    vm_dcl_kbd_control_pin_t kbdmap;

    kbd_handle = vm_dcl_open(VM_DCL_KBD,0);
    kbdmap.col_map = 0x09;
    kbdmap.row_map =0x05;
    vm_dcl_control(kbd_handle,VM_DCL_KBD_COMMAND_CONFIG_PIN, (void *)(&kbdmap));

    vm_dcl_close(kbd_handle);
}
Ejemplo n.º 17
0
VMINT8 digitalWrite(VMUINT8 ulPin,VMUINT8 ulData)
{
	VM_DCL_HANDLE gpio_handle;
	gpio_handle = vm_dcl_open(VM_DCL_GPIO, ulPin);

    if(gpio_handle != VM_DCL_HANDLE_INVALID)
    {
		if(ulData == LOW)
			vm_dcl_control(gpio_handle, VM_DCL_GPIO_COMMAND_WRITE_LOW, NULL);
		else if(ulData == HIGH)
			vm_dcl_control(gpio_handle, VM_DCL_GPIO_COMMAND_WRITE_HIGH, NULL);
    }
    else
    {
    	vm_log_info("gpio write pin fail");
    	return -1;
    }

	vm_dcl_close(gpio_handle);
	return TRUE;
}
Ejemplo n.º 18
0
static VMBOOL wait_result(hx711_task* task)
{
	vm_dcl_gpio_control_level_status_t pin_status;
	int timeout_counter = 8;

	vm_dcl_control(task->scl_handle, VM_DCL_GPIO_COMMAND_WRITE_LOW, NULL);
	vm_dcl_control(task->sda_handle, VM_DCL_GPIO_COMMAND_READ, &pin_status);
	while (timeout_counter > 0 && pin_status.level_status == VM_DCL_GPIO_IO_HIGH)
	{
		vm_thread_sleep(100);
		vm_dcl_control(task->sda_handle, VM_DCL_GPIO_COMMAND_READ, &pin_status);
		timeout_counter--;
	}

	if (pin_status.level_status == VM_DCL_GPIO_IO_LOW)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}
Ejemplo n.º 19
0
void UARTClass::begin( const uint32_t dwBaudRate )
{

    vm_dcl_sio_control_dcb_t data;
	
    if(_usbNum == 2)
    {
        if(!changePinType(0, PIO_UART, &uart_handle))
            return;
	
        if(uart_handle == VM_DCL_HANDLE_INVALID)
            uart_handle = vm_dcl_open(VM_DCL_SIO_UART_PORT1,vm_dcl_get_owner_id());
    }
    else
    {
        uart_handle = vm_dcl_open(VM_DCL_SIO_USB_PORT1,vm_dcl_get_owner_id());
    }

    if(VM_DCL_HANDLE_INVALID==uart_handle) 
    {
        vm_log_info((char*)"UARTClass failed");
        return;
    }

    data.owner_id = vm_dcl_get_owner_id();	
    data.config.baud_rate = (VM_DCL_SIO_UART_BAUDRATE)dwBaudRate;
    data.config.data_bits_per_char_length = 8;
    data.config.stop_bits = 1;
    data.config.parity = 0;
    data.config.flow_control = 1;
    data.config.sw_xon_char = 0x11;
    data.config.sw_xoff_char = 0x13;
    data.config.dsr_check = 0;
    vm_dcl_control(uart_handle,VM_DCL_SIO_COMMAND_SET_DCB_CONFIG,(void *)&data);
    vm_dcl_register_callback(uart_handle,VM_DCL_SIO_UART_READY_TO_READ,(vm_dcl_callback)UartIrqHandler,(void*)NULL);

    if(_usbNum == 2)
    {
        setPinHandle(0, uart_handle);
    }
    else
    {
        usb_device_handle = uart_handle;
    }
}
Ejemplo n.º 20
0
void UARTClass::begin( const uint32_t dwBaudRate )
{

    vm_sio_ctrl_dcb_t data;
	
    if(_usbNum == 2)
    {
        if(!changePinType(0, PIO_UART, &uart_handle))
            return;
	
        if(uart_handle == VM_DCL_HANDLE_INVALID)
            uart_handle = vm_dcl_open(vm_uart_port1,vm_dcl_get_ownerid());
    }
    else
    {
        uart_handle = vm_dcl_open(vm_usb_port1,vm_dcl_get_ownerid());
    }

    if(VM_DCL_HANDLE_INVALID==uart_handle) 
    {
        vm_log_info((char*)"UARTClass failed");
        return;
    }

    data.u4OwenrId = vm_dcl_get_ownerid();	
    data.rUARTConfig.u4Baud = dwBaudRate;
    data.rUARTConfig.u1DataBits = 8;
    data.rUARTConfig.u1StopBits = 1;
    data.rUARTConfig.u1Parity = 0;
    data.rUARTConfig.u1FlowControl = 1;
    data.rUARTConfig.ucXonChar = 0x11;
    data.rUARTConfig.ucXoffChar = 0x13;
    data.rUARTConfig.fgDSRCheck = 0;
    vm_dcl_control(uart_handle,VM_SIO_CMD_SET_DCB_CONFIG,(void *)&data);
    vm_dcl_registercallback(uart_handle,VM_UART_READY_TO_READ,(VM_DCL_CALLBACK)UartIrqHandler,(void*)NULL);

    if(_usbNum == 2)
    {
        setPinHandle(0, uart_handle);
    }
    else
    {
        usb_device_handle = uart_handle;
    }
}
Ejemplo n.º 21
0
UARTClass::operator bool()
{
    if(_usbNum == 2)
    {
        return true;
    }
	
    if(VM_DCL_HANDLE_INVALID != uart_handle) 
    {
        vm_uart_ctrl_com_response dtr;
        vm_dcl_control(uart_handle, 528, (vm_uart_ctrl_com_response*) &dtr);
        if (dtr.result == TRUE)
        {
            return true;
        }
    }
	
    delay(5);
    return false;

}
Ejemplo n.º 22
0
void retarget_setup(void)
{
    VM_DCL_HANDLE uart_handle;
    vm_dcl_sio_control_dcb_t settings;

    if (retarget_device_handle != -1)
    {
        return;
    }

#if defined(__HDK_LINKIT_ONE_V1__)
        vm_dcl_config_pin_mode(VM_PIN_D0, VM_DCL_PIN_MODE_UART);
        vm_dcl_config_pin_mode(VM_PIN_D1, VM_DCL_PIN_MODE_UART);
#elif defined(__HDK_LINKIT_ASSIST_2502__)
        vm_dcl_config_pin_mode(VM_PIN_P8, VM_DCL_PIN_MODE_UART);
        vm_dcl_config_pin_mode(VM_PIN_P9, VM_DCL_PIN_MODE_UART);
#endif

    g_owner_id = vm_dcl_get_owner_id();
    uart_handle = vm_dcl_open(VM_DCL_SIO_UART_PORT1, g_owner_id);
    settings.owner_id = g_owner_id;
    settings.config.dsr_check = 0;
    settings.config.data_bits_per_char_length = VM_DCL_SIO_UART_BITS_PER_CHAR_LENGTH_8;
    settings.config.flow_control = VM_DCL_SIO_UART_FLOW_CONTROL_NONE;
    settings.config.parity = VM_DCL_SIO_UART_PARITY_NONE;
    settings.config.stop_bits = VM_DCL_SIO_UART_STOP_BITS_1;
    settings.config.baud_rate = VM_DCL_SIO_UART_BAUDRATE_115200;
    settings.config.sw_xoff_char = 0x13;
    settings.config.sw_xon_char = 0x11;
    vm_dcl_control(uart_handle, VM_DCL_SIO_COMMAND_SET_DCB_CONFIG, (void *)&settings);

    retarget_rx_signal_id = vm_signal_create();

    vm_dcl_register_callback(uart_handle,
                             VM_DCL_SIO_UART_READY_TO_READ,
                             (vm_dcl_callback)__retarget_irq_handler,
                             (void*)NULL);

    retarget_device_handle = uart_handle;
}
Ejemplo n.º 23
0
// JS: i2c.setup(address, speed)
static v7_val_t i2c_setup(struct v7 *v7)
{
    vm_dcl_i2c_control_config_t conf_data;
    int result;
    v7_val_t addressv = v7_arg(v7, 0);
    v7_val_t speedv = v7_arg(v7, 1);

    int address, speed;

    if(!v7_is_number(addressv) || !v7_is_number(speedv)) {
        printf("Invalid arguments\n");
        return v7_create_undefined();
    }

    address = v7_to_number(addressv);
    speed = v7_to_number(speedv);

    if(speed >= 400) {
        conf_data.transaction_mode = VM_DCL_I2C_TRANSACTION_HIGH_SPEED_MODE;
    } else {
        conf_data.transaction_mode = VM_DCL_I2C_TRANSACTION_FAST_MODE;
    }

    if(g_i2c_handle == VM_DCL_HANDLE_INVALID) {
        g_i2c_handle = vm_dcl_open(VM_DCL_I2C, 0);
    }

    conf_data.reserved_0 = (VM_DCL_I2C_OWNER)0;
    conf_data.get_handle_wait = TRUE;
    conf_data.reserved_1 = 0;
    conf_data.delay_length = 0;
    conf_data.slave_address = (address << 1);
    conf_data.fast_mode_speed = speed;
    conf_data.high_mode_speed = 0;
    result = vm_dcl_control(g_i2c_handle, VM_DCL_I2C_CMD_CONFIG, &conf_data);

    return v7_create_number(result);
}
Ejemplo n.º 24
0
VMINT8 digitalRead(VMUINT8 ulPin)
{
	VM_DCL_HANDLE gpio_handle;
	vm_dcl_gpio_control_level_status_t data;
	VMUINT8 ret;

	gpio_handle = vm_dcl_open(VM_DCL_GPIO, ulPin);

    if(gpio_handle != VM_DCL_HANDLE_INVALID)
    {
		vm_dcl_control(gpio_handle, VM_DCL_GPIO_COMMAND_READ, (void*)&data);

		if (data.level_status == 1)ret = HIGH;
		else ret = LOW;
    }
    else
    {
    	vm_log_info("gpio read pin fail");
    	return -1;
    }

    vm_dcl_close(gpio_handle);
    return ret;
}
Ejemplo n.º 25
0
void attachInterrupt(VMUINT32 pin, void (*callback)(void), VMUINT32 mode)
{

	VM_DCL_HANDLE gpio_handle;
    VM_DCL_HANDLE eint_handle;
    vm_dcl_eint_control_config_t eint_config;
    vm_dcl_eint_control_sensitivity_t sens_data;
    vm_dcl_eint_control_hw_debounce_t deboun_time;
    VM_DCL_STATUS status;

    VMUINT8 i;
    for(i=0;i<EXTERNAL_NUM_INTERRUPTS;i++)
	{
    	if(gExinterruptsPio[i].pin == pin)
		{
    		vm_log_info("open EINT number is %d",gExinterruptsPio[i].eint);
			break;
		}
	}
    if(i == EXTERNAL_NUM_INTERRUPTS)
	{
    	vm_log_info("open EINT number error");
    	return;
	}

	detachInterrupt(pin);

	gpio_handle = vm_dcl_open(VM_DCL_GPIO,pin);
	if(gpio_handle == VM_DCL_HANDLE_INVALID)
	{
		vm_log_info("open eint gpio error, gpio_handle is %d", gpio_handle);
		return;
	}
	switch(pin)
	{
		case 0:case 1:case 2:
			vm_log_info("VM_DCL_GPIO_COMMAND_SET_MODE_1");
			vm_dcl_control(gpio_handle,VM_DCL_GPIO_COMMAND_SET_MODE_1,NULL);
		break;

		case 13:case 18:case 46:case 52:
			vm_log_info("VM_DCL_GPIO_COMMAND_SET_MODE_2");
			vm_dcl_control(gpio_handle,VM_DCL_GPIO_COMMAND_SET_MODE_2,NULL);
		break;

		case 10:case 11:
			vm_log_info("VM_DCL_GPIO_COMMAND_SET_MODE_3");
			vm_dcl_control(gpio_handle,VM_DCL_GPIO_COMMAND_SET_MODE_3,NULL);
		break;

		case 25:
			vm_log_info("VM_DCL_GPIO_COMMAND_SET_MODE_4");
			vm_dcl_control(gpio_handle,VM_DCL_GPIO_COMMAND_SET_MODE_4,NULL);
		break;

		default:
		break;
	}
	vm_dcl_control(gpio_handle,VM_DCL_GPIO_COMMAND_SET_DIRECTION_IN, NULL);
	vm_dcl_close(gpio_handle);


    memset(&eint_config,0, sizeof(vm_dcl_eint_control_config_t));
    memset(&sens_data,0, sizeof(vm_dcl_eint_control_sensitivity_t));
    memset(&deboun_time,0, sizeof(vm_dcl_eint_control_hw_debounce_t));

	eint_handle = vm_dcl_open(VM_DCL_EINT,gExinterruptsPio[i].eint);

    if(VM_DCL_HANDLE_INVALID == eint_handle)
    {
        vm_log_info("open EINT error, eint_handle is %d",eint_handle);
        return;
    }

    gExinterruptsPio[i].handle = eint_handle;
    gExinterruptsPio[i].cb = callback;

    status = vm_dcl_control(eint_handle ,VM_DCL_EINT_COMMAND_MASK,NULL);  // Usually, before we config eint, we mask it firstly.
    if(status != VM_DCL_STATUS_OK)
    {
    	vm_log_info("VM_DCL_EINT_COMMAND_MASK  = %d", status);
    }

	status = vm_dcl_register_callback(eint_handle , VM_DCL_EINT_EVENT_TRIGGER,(vm_dcl_callback)eint_callback,(void*)NULL );
	if(status != VM_DCL_STATUS_OK)
	{
		vm_log_info("VM_DCL_EINT_EVENT_TRIGGER = %d", status);
	}

    if(gExinterruptsPio[i].first == 0)
    {
	    if (mode == CHANGE)
	    {
			sens_data.sensitivity = 0;
			eint_config.act_polarity = 0;
			eint_config.auto_unmask = 1;
	    }
	    else
	    {
			if(mode == FALLING)
			{
				sens_data.sensitivity = 0;
				eint_config.act_polarity = 0;
				eint_config.auto_unmask = 1;
			}
			else if(mode == RISING)
			{
				sens_data.sensitivity = 0;
				eint_config.act_polarity = 1;
				eint_config.auto_unmask = 1;
			}
			else
			{
				vm_log_info("mode not support = %d", mode);
			}
	    }

	    status = vm_dcl_control(eint_handle ,VM_DCL_EINT_COMMAND_SET_SENSITIVITY,(void *)&sens_data);  /* set eint sensitivity */
	    if(status != VM_DCL_STATUS_OK)
	    {
	        vm_log_info("VM_DCL_EINT_COMMAND_SET_SENSITIVITY = %d", status);
	    }

	    deboun_time.debounce_time = 1;  // debounce time 1ms
	    status = vm_dcl_control(eint_handle ,VM_DCL_EINT_COMMAND_SET_HW_DEBOUNCE,(void *)&deboun_time); /* set debounce time */
	    if(status != VM_DCL_STATUS_OK)
	    {
	        vm_log_info("VM_DCL_EINT_COMMAND_SET_HW_DEBOUNCE = %d", status);
	    }

	    status = vm_dcl_control(eint_handle ,VM_DCL_EINT_COMMAND_MASK,NULL);  /* Usually, before we config eint, we mask it firstly. */
	    if(status != VM_DCL_STATUS_OK)
	    {
	    	vm_log_info("VM_DCL_EINT_COMMAND_MASK  = %d", status);
	    }

	    eint_config.debounce_enable = 0;    // 1 means enable hw debounce, 0 means disable.

	    status = vm_dcl_control(eint_handle ,VM_DCL_EINT_COMMAND_CONFIG,(void *)&eint_config);   // Please call this api finally, because we will unmask eint in this command.
	    if(status != VM_DCL_STATUS_OK)
	    {
	        vm_log_info("VM_DCL_EINT_COMMAND_CONFIG = %d", status);
	    }

	    if (mode == CHANGE)
	    {
	    	vm_dcl_eint_control_auto_change_polarity_t auto_change;
		    auto_change.auto_change_polarity = 1;
	        status = vm_dcl_control(eint_handle ,VM_DCL_EINT_COMMAND_SET_AUTO_CHANGE_POLARITY,(void *)&auto_change);   // Please call this api finally, because we will unmask eint in this command.
	        if(status != VM_DCL_STATUS_OK)
	        {
	            vm_log_info("VM_DCL_EINT_COMMAND_CONFIG change = %d", status);
	        }
	    }

	    gExinterruptsPio[i].first ++;

	    vm_log_info("attach Interrupt ok.");
    }
    else
    {
        status = vm_dcl_control(eint_handle,VM_DCL_EINT_COMMAND_UNMASK,NULL);  // call this function to unmask this eint.
        if(status != VM_DCL_STATUS_OK)
        {
             vm_log_info("VM_DCL_EINT_COMMAND_CONFIG = %d", status);
        }
    }
}
void lcd_init_st7789s(VMUINT32 bkground, void **buf_addr)
{  
  // Do HW Reset
	vm_dcl_control(lcd_reset_handle, VM_DCL_GPIO_COMMAND_WRITE_HIGH, NULL);
	vm_drv_lcd_delay_ms(1);
	vm_dcl_control(lcd_reset_handle, VM_DCL_GPIO_COMMAND_WRITE_LOW, NULL);
	vm_drv_lcd_delay_ms(10);
	vm_dcl_control(lcd_reset_handle, VM_DCL_GPIO_COMMAND_WRITE_HIGH, NULL);
	vm_drv_lcd_delay_ms(120);
  
	LCD_CtrlWrite_ST7789S(0x11);
	vm_drv_lcd_delay_ms(120);
	
#ifdef _TOUCH_SCREEN_V1_0_
	LCD_CtrlWrite_ST7789S(0x36);
	LCD_DataWrite_ST7789S(0x00);// C0 40 60
		
	LCD_CtrlWrite_ST7789S(0x35);
	LCD_DataWrite_ST7789S(0x00); //te on	
    
    LCD_CtrlWrite_ST7789S(0x3A);
	LCD_DataWrite_ST7789S(0x55);
	
	LCD_CtrlWrite_ST7789S(0x2a);
	LCD_DataWrite_ST7789S(0x00);
	LCD_DataWrite_ST7789S(0x00);
	LCD_DataWrite_ST7789S(0x00);
	LCD_DataWrite_ST7789S(0xef);

	LCD_CtrlWrite_ST7789S(0x2b);
	LCD_DataWrite_ST7789S(0x00);
	LCD_DataWrite_ST7789S(0x00);
	LCD_DataWrite_ST7789S(0x00);
	LCD_DataWrite_ST7789S(0xef);

    LCD_CtrlWrite_ST7789S(0xb2);
	LCD_DataWrite_ST7789S(0x0c);
	LCD_DataWrite_ST7789S(0x0c);
	LCD_DataWrite_ST7789S(0x00);
	LCD_DataWrite_ST7789S(0x33);
	LCD_DataWrite_ST7789S(0x33);
    
    LCD_CtrlWrite_ST7789S(0xB3);
    LCD_DataWrite_ST7789S(0x10);
    LCD_DataWrite_ST7789S(0xFF);	  // 0x05
    LCD_DataWrite_ST7789S(0x0F);

    LCD_CtrlWrite_ST7789S(0xB4);
    LCD_DataWrite_ST7789S(0x0B);

    LCD_CtrlWrite_ST7789S(0xB5);
    LCD_DataWrite_ST7789S(0x9F);

    LCD_CtrlWrite_ST7789S(0xB7);
    LCD_DataWrite_ST7789S(0x35);

    LCD_CtrlWrite_ST7789S(0xBB);
    LCD_DataWrite_ST7789S(0x28);

    LCD_CtrlWrite_ST7789S(0xBC);
    LCD_DataWrite_ST7789S(0xEC);

    LCD_CtrlWrite_ST7789S(0xBD);
    LCD_DataWrite_ST7789S(0xFE);

    LCD_CtrlWrite_ST7789S(0xC0);
    LCD_DataWrite_ST7789S(0x2C);

    LCD_CtrlWrite_ST7789S(0xC2);
    LCD_DataWrite_ST7789S(0x01);

    LCD_CtrlWrite_ST7789S(0xC3);
    LCD_DataWrite_ST7789S(0x1E); 

    LCD_CtrlWrite_ST7789S(0xC4);
    LCD_DataWrite_ST7789S(0x20);

    LCD_CtrlWrite_ST7789S(0xC6);    // Normal mode frame rate
    LCD_DataWrite_ST7789S(0x1E);	// 0x0f 60Hz while FPA and BPA = 0x0C

    LCD_CtrlWrite_ST7789S(0xD0);
    LCD_DataWrite_ST7789S(0xA4);
    LCD_DataWrite_ST7789S(0xA1);
    
    LCD_CtrlWrite_ST7789S(0xE0);
    LCD_DataWrite_ST7789S(0xD0);
    LCD_DataWrite_ST7789S(0x00);
    LCD_DataWrite_ST7789S(0x00);
    LCD_DataWrite_ST7789S(0x08);
    LCD_DataWrite_ST7789S(0x07);
    LCD_DataWrite_ST7789S(0x05);
    LCD_DataWrite_ST7789S(0x29);
    LCD_DataWrite_ST7789S(0x54);
    LCD_DataWrite_ST7789S(0x41);
    LCD_DataWrite_ST7789S(0x3C);
    LCD_DataWrite_ST7789S(0x17);
    LCD_DataWrite_ST7789S(0x15);
    LCD_DataWrite_ST7789S(0x1A);
    LCD_DataWrite_ST7789S(0x20);

    LCD_CtrlWrite_ST7789S(0xE1);
    LCD_DataWrite_ST7789S(0xD0);
    LCD_DataWrite_ST7789S(0x00);
    LCD_DataWrite_ST7789S(0x00);
    LCD_DataWrite_ST7789S(0x08);
    LCD_DataWrite_ST7789S(0x07);
    LCD_DataWrite_ST7789S(0x04);
    LCD_DataWrite_ST7789S(0x29);
    LCD_DataWrite_ST7789S(0x44);
    LCD_DataWrite_ST7789S(0x42);
    LCD_DataWrite_ST7789S(0x3B);
    LCD_DataWrite_ST7789S(0x16);
    LCD_DataWrite_ST7789S(0x15);
    LCD_DataWrite_ST7789S(0x1B);
    LCD_DataWrite_ST7789S(0x1F);
#endif

#ifdef _TOUCH_SCREEN_V1_1_
	LCD_CtrlWrite_ST7789S (0x36);
	LCD_DataWrite_ST7789S(0x00);
	LCD_CtrlWrite_ST7789S (0x3a);
	LCD_DataWrite_ST7789S(0x05);
	LCD_CtrlWrite_ST7789S (0x21);
	LCD_CtrlWrite_ST7789S (0x2a);
	LCD_DataWrite_ST7789S(0x00);
	LCD_DataWrite_ST7789S(0x00);
	LCD_DataWrite_ST7789S(0x00);
	LCD_DataWrite_ST7789S(0xef);
	LCD_CtrlWrite_ST7789S (0x2b);
	LCD_DataWrite_ST7789S(0x00);
	LCD_DataWrite_ST7789S(0x00);
	LCD_DataWrite_ST7789S(0x00);
	LCD_DataWrite_ST7789S(0xef);
	//--------------------------------ST7789V Frame rate setting----------------------------------//
	LCD_CtrlWrite_ST7789S (0xb2);
	LCD_DataWrite_ST7789S(0x0c);
	LCD_DataWrite_ST7789S(0x0c);
	LCD_DataWrite_ST7789S(0x00);
	LCD_DataWrite_ST7789S(0x33);
	LCD_DataWrite_ST7789S(0x33);
	LCD_CtrlWrite_ST7789S (0xb7);
	LCD_DataWrite_ST7789S(0x35);
	//---------------------------------ST7789V Power setting--------------------------------------//
	LCD_CtrlWrite_ST7789S (0xbb);
	LCD_DataWrite_ST7789S(0x1f);
	LCD_CtrlWrite_ST7789S (0xc0);
	LCD_DataWrite_ST7789S(0x2c);
	LCD_CtrlWrite_ST7789S (0xc2);
	LCD_DataWrite_ST7789S(0x01);
	LCD_CtrlWrite_ST7789S (0xc3);
	LCD_DataWrite_ST7789S(0x12);
	LCD_CtrlWrite_ST7789S (0xc4);
	LCD_DataWrite_ST7789S(0x20);
	LCD_CtrlWrite_ST7789S (0xc6);
	LCD_DataWrite_ST7789S(0x0f);
	LCD_CtrlWrite_ST7789S (0xd0);
	LCD_DataWrite_ST7789S(0xa4);
	LCD_DataWrite_ST7789S(0xa1);
	//--------------------------------ST7789V gamma setting--------------------------------------//
	LCD_CtrlWrite_ST7789S (0xe0);
	LCD_DataWrite_ST7789S(0xd0);
	LCD_DataWrite_ST7789S(0x08);
	LCD_DataWrite_ST7789S(0x11);
	LCD_DataWrite_ST7789S(0x08);
	LCD_DataWrite_ST7789S(0x0c);
	LCD_DataWrite_ST7789S(0x15);
	LCD_DataWrite_ST7789S(0x39);
	LCD_DataWrite_ST7789S(0x33);
	LCD_DataWrite_ST7789S(0x50);
	LCD_DataWrite_ST7789S(0x36);
	LCD_DataWrite_ST7789S(0x13);
	LCD_DataWrite_ST7789S(0x14);
	LCD_DataWrite_ST7789S(0x29);
	LCD_DataWrite_ST7789S(0x2d);
    
	LCD_CtrlWrite_ST7789S (0xe1);
	LCD_DataWrite_ST7789S(0xd0);
	LCD_DataWrite_ST7789S(0x08);
	LCD_DataWrite_ST7789S(0x10);
	LCD_DataWrite_ST7789S(0x08);
	LCD_DataWrite_ST7789S(0x06);
	LCD_DataWrite_ST7789S(0x06);
	LCD_DataWrite_ST7789S(0x39);
	LCD_DataWrite_ST7789S(0x44);
	LCD_DataWrite_ST7789S(0x51);
	LCD_DataWrite_ST7789S(0x0b);
	LCD_DataWrite_ST7789S(0x16);
	LCD_DataWrite_ST7789S(0x14);
	LCD_DataWrite_ST7789S(0x2f);
	LCD_DataWrite_ST7789S(0x31);
    
	LCD_CtrlWrite_ST7789S (0x29);
#endif			
	// clear the screen with black color
	LCD_CtrlWrite_ST7789S(0x2C);
	LCD_ClearAll_ST7789S(0x00);

	vm_drv_lcd_operation(VM_DRV_LCD_ENABLE_SERIAL0_2PIN);
	vm_drv_lcd_operation2(VM_DRV_LCD_SET_SERIAL0_IF_2PIN_SIZE, LCD_SCNF_IF_2PIN_WIDTH_16);
	
	// 2 data lane
	LCD_CtrlWrite_ST7789S(0xE7);
	LCD_DataWrite_ST7789S(0x10);

	// display on
	LCD_CtrlWrite_ST7789S(0x29);
	vm_drv_lcd_delay_ms(120);

}
Ejemplo n.º 27
0
static void enter_standby(hx711_task* task)
{
	vm_dcl_control(task->scl_handle, VM_DCL_GPIO_COMMAND_WRITE_HIGH, NULL);
	vm_thread_sleep(task->delay+1);
}
Ejemplo n.º 28
0
// JS: wrote = i2c.send(data1, [data2], ..., [datan] )
// data can be either a string, a table or an 8-bit number
static v7_val_t i2c_send(struct v7 *v7)
{
    const char* pdata;
    size_t datalen, i;
    int numdata;
    uint32_t wrote = 0;
    unsigned argn;
    vm_dcl_i2c_control_continue_write_t param;
    uint8_t wbuf[8]; // max size - 8
    uint8_t wbuf_index = 0;

    param.data_ptr = wbuf;

    if(lua_gettop(L) < 1)
        return luaL_error(L, "invalid number of arguments");
    for(argn = 1; argn <= lua_gettop(L); argn++) {
        // lua_isnumber() would silently convert a string of digits to an integer
        // whereas here strings are handled separately.
        if(lua_type(L, argn) == LUA_TNUMBER) {
            numdata = (int)luaL_checkinteger(L, argn);
            if(numdata < 0 || numdata > 255)
                return luaL_error(L, "numeric data must be from 0 to 255");

            wbuf[wbuf_index] = numdata;
            wbuf_index++;
            if(wbuf_index >= 8) {
                param.data_length = wbuf_index;
                param.transfer_number = wbuf_index;
                vm_dcl_control(g_i2c_handle, VM_DCL_I2C_CMD_CONT_WRITE, &param);
                wbuf_index = 0;
                wrote += 8;
            }
        } else if(lua_istable(L, argn)) {
            datalen = lua_objlen(L, argn);
            for(i = 0; i < datalen; i++) {
                lua_rawgeti(L, argn, i + 1);
                numdata = (int)luaL_checkinteger(L, -1);
                lua_pop(L, 1);
                if(numdata < 0 || numdata > 255)
                    return luaL_error(L, "numeric data must be from 0 to 255");

                wbuf[wbuf_index] = numdata;
                wbuf_index++;
                if(wbuf_index >= 8) {
                    param.data_length = wbuf_index;
                    param.transfer_number = wbuf_index;
                    vm_dcl_control(g_i2c_handle, VM_DCL_I2C_CMD_CONT_WRITE, &param);
                    wbuf_index = 0;
                    wrote += 8;
                }
            }
        } else {
            pdata = luaL_checklstring(L, argn, &datalen);
            for(i = 0; i < datalen; i++) {
                wbuf[wbuf_index] = numdata;
                wbuf_index++;
                if(wbuf_index >= 8) {
                    param.data_length = wbuf_index;
                    param.transfer_number = wbuf_index;
                    vm_dcl_control(g_i2c_handle, VM_DCL_I2C_CMD_CONT_WRITE, &param);
                    wbuf_index = 0;
                    wrote += 8;
                }
            }
        }
    }

    if(wbuf_index) {
        param.data_length = wbuf_index;
        param.transfer_number = wbuf_index;
        vm_dcl_control(g_i2c_handle, VM_DCL_I2C_CMD_CONT_WRITE, &param);
        wrote += wbuf_index;
    }

    lua_pushinteger(L, wrote);
    return 1;
}
Ejemplo n.º 29
0
VMINT8 pinMode(VMUINT8 ulPin,VMUINT8 ulMode)
{
	VM_DCL_HANDLE gpio_handle;
	gpio_handle = vm_dcl_open(VM_DCL_GPIO, ulPin);

    if(gpio_handle != VM_DCL_HANDLE_INVALID)
    {
		switch(ulMode)
		{
			case INPUT:
				vm_dcl_control(gpio_handle, VM_DCL_GPIO_COMMAND_SET_MODE_0, NULL);
				vm_dcl_control(gpio_handle, VM_DCL_GPIO_COMMAND_SET_DIRECTION_IN, NULL);
			break;

			case OUTPUT:
				vm_dcl_control(gpio_handle, VM_DCL_GPIO_COMMAND_SET_MODE_0, NULL);
				vm_dcl_control(gpio_handle, VM_DCL_GPIO_COMMAND_SET_DIRECTION_OUT, NULL);
			break;

			case INPUT_PULLUP:
				vm_dcl_control(gpio_handle, VM_DCL_GPIO_COMMAND_SET_MODE_0, NULL);
				vm_dcl_control(gpio_handle, VM_DCL_GPIO_COMMAND_SET_DIRECTION_IN, NULL);
				vm_dcl_control(gpio_handle, VM_DCL_GPIO_COMMAND_ENABLE_PULL, NULL);
				vm_dcl_control(gpio_handle, VM_DCL_GPIO_COMMAND_SET_PULL_HIGH, NULL);
			break;

			case INPUT_PULLDN:
				vm_dcl_control(gpio_handle, VM_DCL_GPIO_COMMAND_SET_MODE_0, NULL);
				vm_dcl_control(gpio_handle, VM_DCL_GPIO_COMMAND_SET_DIRECTION_IN, NULL);
				vm_dcl_control(gpio_handle, VM_DCL_GPIO_COMMAND_ENABLE_PULL, NULL);
				vm_dcl_control(gpio_handle, VM_DCL_GPIO_COMMAND_SET_PULL_LOW, NULL);
			break;

			default:
			break;
		}
    }
    else
    {
    	vm_log_info("gpio set pin mode fail");
    	return -1;
    }

	vm_dcl_close(gpio_handle);
	return TRUE;
}
Ejemplo n.º 30
0
void attachInterrupt(uint32_t pin, void (*callback)(void), uint32_t mode)
{
    VM_DCL_HANDLE eint_handle;
    vm_eint_ctrl_config_t eint_config;
    vm_eint_ctrl_set_sensitivity_t sens_data;
    vm_eint_ctrl_set_hw_deounce_t deboun_time;
    VM_DCL_STATUS status;
	
    if(pin > EXTERNAL_NUM_INTERRUPTS)
		return ;

	detachInterrupt(pin);

	if(!changePinType(gExinterruptsPio[pin].pin, PIO_EINT, &eint_handle))
		return;
	
    memset(&eint_config,0, sizeof(vm_eint_ctrl_config_t));
    memset(&sens_data,0, sizeof(vm_eint_ctrl_set_sensitivity_t));
    memset(&deboun_time,0, sizeof(vm_eint_ctrl_set_hw_deounce_t));

	if(eint_handle == VM_DCL_HANDLE_INVALID)
    	eint_handle = vm_dcl_open(VM_DCL_EINT,gExinterruptsPio[pin].eint);
	
    if(VM_DCL_HANDLE_INVALID == eint_handle)
    {
        vm_log_info("open EINT error");
        return;
    }

    setPinHandle(gExinterruptsPio[pin].pin, eint_handle);
    gExinterruptsPio[pin].handle = eint_handle;
    gExinterruptsPio[pin].cb = callback;

    status = vm_dcl_control(eint_handle ,VM_EINT_CMD_MASK,NULL);  /* Usually, before we config eint, we mask it firstly. */
    if(status != VM_DCL_STATUS_OK)
    {
    	vm_log_info("VM_EINT_CMD_MASK  = %d", status);
    }
	
     status = vm_dcl_registercallback(eint_handle , VM_EVENT_EINT_TRIGGER,(VM_DCL_CALLBACK)eint_callback,(void*)NULL );
     if(status != VM_DCL_STATUS_OK)
     {
         vm_log_info("VM_EVENT_EINT_TRIGGER = %d", status);
     }	
    if(gExinterruptsPio[pin].first == 0)
    {
			
	    if (mode == CHANGE)
	    {
	    		sens_data.sensitivity = 0;
	              eint_config.act_polarity = 0;
		       eint_config.auto_unmask = 1;
	    } 
	    else 
	    {		  
		  if (mode == FALLING)
		  {
	    		sens_data.sensitivity = 0;
	              eint_config.act_polarity = 0;
		       eint_config.auto_unmask = 1;
		  }
		  else if (mode == RISING) 
		  {
	    		sens_data.sensitivity = 0;
	              eint_config.act_polarity = 1;
		       eint_config.auto_unmask = 1;
		  }
		  else
		  {
		  		vm_log_info("mode not support = %d", mode);
		  }
	    }

	    status = vm_dcl_control(eint_handle ,VM_EINT_CMD_SET_SENSITIVITY,(void *)&sens_data);  /* set eint sensitivity */
	     if(status != VM_DCL_STATUS_OK)
	    {
	        vm_log_info("VM_EINT_CMD_SET_SENSITIVITY = %d", status);
	    }

	    deboun_time.debounce_time = 1;  /* debounce time 1ms */
	    status = vm_dcl_control(eint_handle ,VM_EINT_CMD_SET_HW_DEBOUNCE,(void *)&deboun_time); /* set debounce time */
	    if(status != VM_DCL_STATUS_OK)
	    {
	        vm_log_info("VM_EINT_CMD_SET_HW_DEBOUNCE = %d", status);
	    }

	    status = vm_dcl_control(eint_handle ,VM_EINT_CMD_MASK,NULL);  /* Usually, before we config eint, we mask it firstly. */
	    if(status != VM_DCL_STATUS_OK)
	    {
	    	vm_log_info("VM_EINT_CMD_MASK  = %d", status);
	    }

	    eint_config.debounce_en = 0;    /* 1 means enable hw debounce, 0 means disable. */

	    status = vm_dcl_control(eint_handle ,VM_EINT_CMD_CONFIG,(void *)&eint_config);   /* Please call this api finally, because we will unmask eint in this command. */
	    if(status != VM_DCL_STATUS_OK)
	    {
	        vm_log_info("VM_EINT_CMD_CONFIG = %d", status);
	    }
		
	    if (mode == CHANGE)
	    {
	    	 vm_eint_ctrl_set_auto_change_pol_t auto_change;
		 auto_change.autoChangePol = 1;
	        status = vm_dcl_control(eint_handle ,VM_EINT_CMD_SET_AUTO_CHANGE_POLARTIY,(void *)&auto_change);   /* Please call this api finally, because we will unmask eint in this command. */
	        if(status != VM_DCL_STATUS_OK)
	        {
	            vm_log_info("VM_EINT_CMD_CONFIG chage= %d", status);
	        }
	    }
	    gExinterruptsPio[pin].first ++;
    }
    else
    {	
        status = vm_dcl_control(eint_handle,VM_EINT_CMD_UNMASK,NULL);  /* call this function to unmask this eint. */
        if(status != VM_DCL_STATUS_OK)
        {
             vm_log_info("VM_EINT_CMD_CONFIG = %d", status);
        }
    }
}