status_t
arch_debug_console_init(kernel_args *args)
{
    // only use the port if we could find one, else use the standard port
    if (args != NULL && args->platform_args.serial_base_ports[0] != 0)
        sSerialBasePort = args->platform_args.serial_base_ports[0];

    init_serial_port(sSerialBasePort, kSerialBaudRate);

    return B_OK;
}
Example #2
0
//*FUNCTION DEFINITIONS
bool initSerial( unsigned int & serialPort, char * serialAddr )
{
#ifdef ENABLE_SERIAL
  //initialize serial port
  serialPort = open_port(serialAddr);

  if (init_serial_port(serialPort) == SERIAL_ERROR)
    return false;
  else
    return true;
#endif
}
 event_producer_elotouch::event_producer_elotouch(std::string devname)
     : just_untouched(false), raw_mode_(false)
 {
     calib_b=-3670;
     calib_a=1.0/(400-3670);
     calib_d=-500;
     calib_c=1.0/(3540-500);
     fd = open(devname.c_str(),O_RDWR | O_NOCTTY  | O_NDELAY);
     if(fd <0)
         ERROR("Couldnt open elotouch Device " << devname);
     init_serial_port(fd);
     init_elotouch(fd,false);
     
 }
status_t
arch_debug_console_init_settings(kernel_args *args)
{
    uint32 baudRate = kSerialBaudRate;
    uint16 basePort = sSerialBasePort;
    void *handle;

    // get debug settings
    handle = load_driver_settings("kernel");
    if (handle != NULL) {
        const char *value = get_driver_parameter(handle, "serial_debug_port",
                            NULL, NULL);
        if (value != NULL) {
            int32 number = strtol(value, NULL, 0);
            if (number >= MAX_SERIAL_PORTS) {
                // use as port number directly
                basePort = number;
            } else if (number >= 0) {
                // use as index into port array
                if (args->platform_args.serial_base_ports[number] != 0)
                    basePort = args->platform_args.serial_base_ports[number];
            } else {
                // ignore value and use default
            }
        }

        value = get_driver_parameter(handle, "serial_debug_speed", NULL, NULL);
        if (value != NULL) {
            int32 number = strtol(value, NULL, 0);
            switch (number) {
            case 9600:
            case 19200:
            case 38400:
            case 57600:
            case 115200:
                //case 230400:
                baudRate = number;
            }
        }

        unload_driver_settings(handle);
    }

    if (sSerialBasePort == basePort && baudRate == kSerialBaudRate)
        return B_OK;

    init_serial_port(sSerialBasePort, kSerialBaudRate);

    return B_OK;
}
Example #5
0
File: main.c Project: ryry0/Toolbox
int main(int argc, char **argv)
{
    char *path;
    char chtosend='\n';
    int fd;             //file descriptor
    signal(SIGINT, terminate);

    if (argc > 1)
    {
      path  = argv[1];
    }
    else
    {
      printf("\nAddress of serial device required as argument.\n");
      return 1;
    }

    printf("Opening serial port...\n");
    fd = open_port(path);

    printf("Initializing serial port...\n");
    if(init_serial_port(fd))
    {
        printf("Initialization failed!\n");
        return 1;
    }

    printf("Initialized!\n");
    printf("Press escape to quit.\n");
    printf("Press ENTER when ready:\n");

    chtosend = getch();               //pause until ready
    printf("Ready!\n");
    //echoPrint(fd);                      //read the buffer

    while(chtosend != 0x1B)           //while not escape read and send data
    {
        chtosend = getch();
        printf("0x%x \t %c\n", chtosend,chtosend);

        write(fd, (char*) &chtosend, sizeof(chtosend));
        //echoPrint(fd);
    }

    printf("\nExiting..\n");
    close(fd);
    return 0;
}
Example #6
0
int __declspec(dllexport) serial(struct unit *unit) {
  int iobase;
  int irq;
  char devname[8];

  iobase = get_unit_iobase(unit);
  irq = get_unit_irq(unit);

  if (iobase < 0) iobase = serial_default_iobase[next_serial_portno - 1];
  if (irq < 0) irq = serial_default_irq[next_serial_portno - 1];

  sprintf(devname, "com%d", next_serial_portno++);
  init_serial_port(devname, iobase, irq, unit);

  return 0;
}
Example #7
0
int
main(int argc, char** argv)
{
  const char* portname = "/dev/ttyS0";
  int         c;

  while ((c = getopt(argc, argv, "p:?")) != -1)
    switch (c)
    {
      case 'p': portname = optarg; break;
      case '?': exit_usage();
      default:  abort();
    }

  if (optind < argc)
    exit_usage();

  setlocale(LC_ALL, "");
  init_screen();

  int portfd = open(portname, O_RDWR | O_NOCTTY | O_NONBLOCK);
  if (portfd < 0)
    exit_error(portname, errno);

  init_serial_port(portfd, portname);

  int flags = fcntl(portfd, F_GETFL, 0);
  if (flags < 0 || fcntl(portfd, F_SETFL, flags & ~O_NONBLOCK) < 0)
    exit_error(portname, errno);

  wprintw(win_header, "Using serial port %s.\nPress CTRL-D to quit.", portname);
  waddch(win_prompt, '>');
  wnoutrefresh(win_header);
  wnoutrefresh(win_prompt);
  wnoutrefresh(win_input);
  doupdate();

  input_loop(portfd);

  while (close(portfd) < 0)
    if (errno != EINTR)
      exit_error(portname, errno);

  destroy_screen();
  return 0;
}
Example #8
0
//----------------------------------------------------
void get_compass_data_full(unsigned char *buffer_in)
{
	int ttys_descriptor;
	int bytes_arrived = 0;

	while(1) 
	{
			ttys_descriptor = init_serial_port();
			write_cmd(ttys_descriptor,0x23);
			sleep(1);
			bytes_arrived = rs232_buffer_in_lenght(ttys_descriptor);
			if (bytes_arrived != ANSWER_CMD_FULL_LENGHT)	
			{
				printf("ERROR: Re-send command (FULL) to serial port, bytes arrived:%d\n",bytes_arrived);
				rs232_close(ttys_descriptor);
				continue;
			}
			break;	
	}
	rs232_get(ttys_descriptor,buffer_in, ANSWER_CMD_FULL_LENGHT);
	rs232_close(ttys_descriptor);	
}
Example #9
0
/*
 * Our main
 */
int main(void)
{    
    /* Check if the firmware asked to start the bootloader */
    if (bootloader_start_var == 0xBEEF)
    {
        wdt_disable();
        start_bootloader();
    }
    
    /* Pull PC3 low to enable boot loader */
    PORTC_DIRCLR = PIN3_bm;                         // PC3 input
    PORTC.PIN3CTRL = PORT_OPC_PULLUP_gc;            // Pullup PC3
    _delay_ms(10);                                  // Small delay
    if((PORTC_IN & PIN3_bm) == 0)                   // Check PC3
    {
        start_bootloader();
    }
    
    /* Normal boot */
    switch_to_32MHz_clock();                        // Switch to 32MHz
    _delay_ms(1000);                                // Wait for power to settle
    init_serial_port();                             // Initialize serial port    
    init_dac();                                     // Init DAC
    init_adc();                                     // Init ADC
    init_ios();                                     // Init IOs
    init_calibration();                             // Init calibration
    enable_interrupts();                            // Enable interrupts
    init_usb();                                     // Init USB comms
    functional_test();                              // Functional test if started for the first time

    uint8_t current_fw_mode = MODE_IDLE;
    while(1)
    {
        if (current_fw_mode == MODE_CAP_MES)
        {
            // If we are in cap measurement mode and have a report to send
            if(cap_measurement_loop(&cap_report) == TRUE)
            {
                maindprintf_P(PSTR("*"));
                usb_packet.length = sizeof(cap_report);
                usb_packet.command_id = CMD_CAP_MES_REPORT;
                memcpy((void*)usb_packet.payload, (void*)&cap_report, sizeof(cap_report));
                usb_send_data((uint8_t*)&usb_packet);
            }
        }
        
        // USB command parser
        if (usb_receive_data((uint8_t*)&usb_packet) == TRUE)
        {
            switch(usb_packet.command_id)
            {
                case CMD_BOOTLOADER_START:
                {
                    maindprintf_P(PSTR("USB- Bootloader start"));
                    bootloader_start_var = 0xBEEF;
                    wdt_enable(WDTO_1S);
                    while(1);
                }
                case CMD_PING: 
                {
                    maindprintf_P(PSTR("."));
                    // Ping packet, resend the same one
                    usb_send_data((uint8_t*)&usb_packet);
                    break;
                }
                case CMD_VERSION:
                {
                    maindprintf_P(PSTR("USB- Version\r\n"));
                    // Version request packet
                    strcpy((char*)usb_packet.payload, CAPMETER_VER);
                    usb_packet.length = sizeof(CAPMETER_VER);
                    usb_send_data((uint8_t*)&usb_packet);
                    break;
                }
                case CMD_OE_CALIB_STATE:
                {
                    maindprintf_P(PSTR("USB- Calib state\r\n"));
                    // Get open ended calibration state.
                    if (is_platform_calibrated() == TRUE)
                    {
                        // Calibrated, return calibration data
                        usb_packet.length = get_openended_calibration_data(usb_packet.payload);
                    } 
                    else
                    {
                        // Not calibrated, return 0
                        usb_packet.length = 1;
                        usb_packet.payload[0] = 0;
                    }
                    usb_send_data((uint8_t*)&usb_packet);    
                    break;                
                }
                case CMD_OE_CALIB_START:
                {
                    maindprintf_P(PSTR("USB- Calib start\r\n"));
                    // Check if we are in idle mode
                    if (current_fw_mode == MODE_IDLE)
                    {
                        // Calibration start
                        start_openended_calibration(usb_packet.payload[0], usb_packet.payload[1], usb_packet.payload[2]);
                        usb_packet.length = get_openended_calibration_data(usb_packet.payload);
                    }
                    else
                    {
                        usb_packet.length = 1;
                        usb_packet.payload[0] = USB_RETURN_ERROR;
                    }
                    usb_send_data((uint8_t*)&usb_packet);    
                    break;                
                }
                case CMD_GET_OE_CALIB:
                {
                    maindprintf_P(PSTR("USB- Calib data\r\n"));
                    // Get calibration data
                    usb_packet.length = get_openended_calibration_data(usb_packet.payload);
                    usb_send_data((uint8_t*)&usb_packet);    
                    break;                
                }
                case CMD_SET_VBIAS:
                {
                    // Check that we are not measuring anything and if so, skip samples and stop oscillation
                    if (current_fw_mode == MODE_CAP_MES)
                    {
                        pause_capacitance_measurement_mode();
                    }
                    
                    // Enable and set vbias... can also be called to update it
                    uint16_t* temp_vbias = (uint16_t*)usb_packet.payload;
                    uint16_t set_vbias = enable_bias_voltage(*temp_vbias);
                    uint16_t cur_dacv = get_current_vbias_dac_value();
                    usb_packet.length = 4;
                    memcpy((void*)usb_packet.payload, (void*)&set_vbias, sizeof(set_vbias));
                    memcpy((void*)&usb_packet.payload[2], (void*)&cur_dacv, sizeof(cur_dacv));
                    usb_send_data((uint8_t*)&usb_packet);
                    
                    // If we are measuring anything, resume measurements
                    if (current_fw_mode == MODE_CAP_MES)
                    {
                        resume_capacitance_measurement_mode();
                    }                    
                    break;
                }
                case CMD_DISABLE_VBIAS:
                {
                    // Disable vbias
                    usb_packet.length = 0;
                    disable_bias_voltage();
                    usb_send_data((uint8_t*)&usb_packet);
                    break;
                }
                case CMD_CUR_MES_MODE:
                {
                    // Enable current measurement or start another measurement
                    if (current_fw_mode == MODE_IDLE)
                    {
                        set_current_measurement_mode(usb_packet.payload[0]);
                        current_fw_mode = MODE_CURRENT_MES;
                    } 
                    // Check if we are in the right mode to start a measurement
                    if (current_fw_mode == MODE_CURRENT_MES)
                    {
                        // We either just set current measurement mode or another measurement was requested
                        if (get_configured_adc_ampl() != usb_packet.payload[0])
                        {
                            // If the amplification isn't the same one as requested
                            set_current_measurement_mode(usb_packet.payload[0]);
                        }
                        // Start measurement
                        usb_packet.length = 2;
                        uint16_t return_value = cur_measurement_loop(usb_packet.payload[1]);
                        memcpy((void*)usb_packet.payload, (void*)&return_value, sizeof(return_value));
                        usb_send_data((uint8_t*)&usb_packet);
                    }
                    else
                    {
                        usb_packet.length = 1;
                        usb_packet.payload[0] = USB_RETURN_ERROR;
                        usb_send_data((uint8_t*)&usb_packet);
                    }
                    break;
                }
                case CMD_CUR_MES_MODE_EXIT:
                {
                    if (current_fw_mode == MODE_CURRENT_MES)
                    {
                        usb_packet.payload[0] = USB_RETURN_OK;
                        disable_current_measurement_mode();
                        current_fw_mode = MODE_IDLE;
                    }
                    else
                    {
                        usb_packet.payload[0] = USB_RETURN_ERROR;
                    }
                    usb_packet.length = 1;
                    usb_send_data((uint8_t*)&usb_packet);
                    break;                    
                }
                case CMD_CAP_REPORT_FREQ:
                {
                    if (current_fw_mode == MODE_IDLE)
                    {
                        set_capacitance_report_frequency(usb_packet.payload[0]);
                        usb_packet.payload[0] = USB_RETURN_OK;
                    }
                    else
                    {
                        usb_packet.payload[0] = USB_RETURN_ERROR;
                    }
                    usb_packet.length = 1;
                    usb_send_data((uint8_t*)&usb_packet);
                    break;
                }
                case CMD_CAP_MES_START:
                {
                    if (current_fw_mode == MODE_IDLE)
                    {
                        current_fw_mode = MODE_CAP_MES;
                        set_capacitance_measurement_mode();
                        usb_packet.payload[0] = USB_RETURN_OK;
                    }
                    else
                    {
                        usb_packet.payload[0] = USB_RETURN_ERROR;
                    }
                    usb_packet.length = 1;
                    usb_send_data((uint8_t*)&usb_packet);
                    break;
                }
                case CMD_CAP_MES_EXIT:
                {
                    if (current_fw_mode == MODE_CAP_MES)
                    {
                        current_fw_mode = MODE_IDLE;
                        disable_capacitance_measurement_mode();
                        usb_packet.payload[0] = USB_RETURN_OK;
                    }
                    else
                    {
                        usb_packet.payload[0] = USB_RETURN_ERROR;
                    }
                    usb_packet.length = 1;
                    usb_send_data((uint8_t*)&usb_packet);
                    break;
                }
                case CMD_SET_VBIAS_DAC:
                {
                    uint16_t* requested_dac_val = (uint16_t*)usb_packet.payload;
                    uint16_t* requested_wait = (uint16_t*)&usb_packet.payload[2];
                    
                    usb_packet.length = 2;
                    if (is_ldo_enabled() == TRUE)
                    {
                        uint16_t set_vbias = force_vbias_dac_change(*requested_dac_val, *requested_wait);
                        memcpy((void*)usb_packet.payload, (void*)&set_vbias, sizeof(set_vbias));
                    } 
                    else
                    {
                        usb_packet.payload[0] = 0;
                        usb_packet.payload[1] = 0;
                    }        
                    usb_send_data((uint8_t*)&usb_packet);            
                    break;
                }
                case CMD_RESET_STATE:
                {
                    maindprintf_P(PSTR("USB- Reset\r\n"));
                    usb_packet.length = 1;
                    current_fw_mode = MODE_IDLE;
                    if(is_platform_calibrated() == TRUE)
                    {
                        disable_bias_voltage();
                        disable_current_measurement_mode();
                        disable_capacitance_measurement_mode();
                        usb_packet.payload[0] = USB_RETURN_OK;                        
                    }
                    else
                    {
                        usb_packet.payload[0] = USB_RETURN_ERROR;
                    }
                    usb_send_data((uint8_t*)&usb_packet);
                    break;
                }
                case CMD_SET_EEPROM_VALS:
                {
                    uint16_t* addr = (uint16_t*)usb_packet.payload;
                    uint16_t size = usb_packet.payload[2];
                    if(((*addr) + size > APP_STORED_DATA_MAX_SIZE) || (size > (RAWHID_RX_SIZE-5)))
                    {
                         usb_packet.payload[0] = USB_RETURN_ERROR;
                    }
                    else
                    {
                        eeprom_write_block((void*)&usb_packet.payload[3], (void*)(EEP_APP_STORED_DATA + (*addr)), size);
                        usb_packet.payload[0] = USB_RETURN_OK;
                    }
                    usb_packet.length = 1;
                    usb_send_data((uint8_t*)&usb_packet);
                    break;
                }
                case CMD_READ_EEPROM_VALS:
                {
                    uint16_t* addr = (uint16_t*)usb_packet.payload;
                    uint16_t size = usb_packet.payload[2];
                    if(((*addr) + size > APP_STORED_DATA_MAX_SIZE) || (size > (RAWHID_TX_SIZE-2)))
                    {
                        usb_packet.length = 1;
                        usb_packet.payload[0] = USB_RETURN_ERROR;
                    }
                    else
                    {
                        usb_packet.length = size;
                        eeprom_read_block(usb_packet.payload, (void*)(EEP_APP_STORED_DATA + (*addr)), size);
                    }
                    usb_send_data((uint8_t*)&usb_packet);
                    break;
                }
                default: break;
            }
        }
    }
    
    // Left here for reference
    //enable_bias_voltage(850);while(1);
    //automated_current_testing();
    //ramp_bias_voltage_test();
    //voltage_settling_test();
    //automated_vbias_testing();
    //automated_current_testing();
    //peak_to_peak_adc_noise_measurement_test();
    //ramp_bias_voltage_test();
    //printf("blah\r\n");_delay_ms(3333);
    //ramp_current_test();
    //functional_test();
    //while(1);
    //calibrate_thresholds();                         // Calibrate vup vlow & thresholds
    //calibrate_cur_mos_0nA();                        // Calibrate 0nA point and store values in eeprom
    //calibrate_current_measurement();                // Calibrate the ADC for current measurements
}
Example #10
0
int main(void)
{
	//disable interrupts
	cli();
	//reset watchdog
	wdt_reset();
	wdt_disable();
	
	init_serial_port(9600);
	init_rf_module();
	
	//TODO: set-up unused pins to avoid floating!
	
	//set direction registers ( 1 - output, 0 - input )
	DDRB |= ( 1 << LED1_PIN ) | ( 1 << LED2_PIN );
	
	set_led_state(1, 0);
	set_led_state(2, 0);
	
	
	//set up WDT interrupt
	_WD_CONTROL_REG = (1<<WDCE) | (1<<WDE);
	//Start watchdog timer with 8s prescaler
	_WD_CONTROL_REG = (1<<WDIE) | (1<<WDE) | (1<<WDP3) | (1<<WDP0);
	
	//Enable global interrupts
	sei();
	
	for ( int i = 0; i < 3; i++ )
	{
		set_led_state(1, 1);
		_delay_ms(100);
		set_led_state(1, 0);
		_delay_ms(350);
	}
	
	uint8_t cycle_count = 0;
	
	while ( 1 )
	{
		cycle_count++;
		if ( cycle_count == 3 )
		{
			for ( int i = 0; i < 2; i++ )
			{
				set_led_state(2, 1);
				_delay_ms(200);
				set_led_state(2, 0);
				_delay_ms(200);
			}
			wdt_reset();
			read_temperature_data();
			cycle_count = 0;
		}else
		{
			set_led_state(1, 1);
			_delay_ms(1000);
			set_led_state(1, 0);	
		}
		
		//wdt_reset();
		
		set_sleep_mode(SLEEP_MODE_PWR_DOWN);
		sei();
		sleep_mode();

	}
	
	/*
	float temperature, humidity;
	while(1)
    {
        _delay_ms(1000);
		power_up_sensor();
		_delay_ms(2500);
		temperature = 0.0;
		humidity = 0.0;
		read_data(&temperature, &humidity);
		
		_delay_ms(1500);
		power_down_sensor();
		
    }*/
}
Example #11
0
/*
 * Get datapoint data according to datapoint properties.
 * Here we fake the data using random numbers.
 */
void * get_datapoint_data(void *props)
{
	float temperature = 0.0;
	void *ret = NULL;
	const char *name = get_string_by_name(props, "name");
	ret = malloc(sizeof(double));
	if (ret == NULL) {
		perror("No memory available.\n");
		exit(-1);
	}

	if (strcmp(name, "grove_temperature") == 0) {
		/* the temperature sensor's output connect to a0 pin of
		   galileo */
		int a0v = galileo_analog_read(0);
		/* the value of a0v should within [0,4096], that use 12bit to
		 * moderize 0~5v voltage, which means 0 stands for 0v while
		 * 4096 stands for 5v. */
		printf("Readed a0 pin voltage: %1.2f\n", ((double)a0v * 5) / 4096);

		/* then next we'll need to calculate temperature based on
		 * the design of temperature sensor.
		 */
		int val = a0v / 4;
		int B = 3975;
		float resistance;
		if (val != 0) {
			resistance = (float)(1023 - val) * 10000 / val;
			temperature = 1 / (log(resistance / 10000) / B + 1 / 298.15) - 273.15;
		}
		printf("The temperature is: %2.2f c\n", temperature);
		/* return the temperature to libsensor */
		*(double *)ret = (double)temperature;
	} else if (strcmp(name, "grove_light") == 0) {
		int a1v = galileo_analog_read(1);
		printf("Readed a1 pin voltage: %1.2f\n", ((double)a1v * 5) / 4096);

		int val = a1v / 4;
		printf("The light number is: %2.2f\n", (double)val);
		*(double *)ret = (double)val;
	} else if (strcmp(name, "grove_sound") == 0) {
		int a2v = galileo_analog_read(2);
		printf("Readed a2 pin voltage: %1.2f\n", ((double)a2v * 5) / 4096);

		int val = a2v / 4;
		printf("The sound number is: %2.2f\n", (double)val);
		*(double *)ret = (double)val;
	} else if (strcmp(name, "lm35-temperature") == 0) {
		int a0v = galileo_analog_read(0);
		/* get voltage */
		printf("Readed a0 pin voltage: %1.2f\n", ((double)a0v * 5) / 4096);

		double val = a0v / 4;
		/* the lm35 output voltage is 10mV per degree, from 0 to 100 C */
		double temperature = (val * 5 / 1024) * 100.00;
		printf("The lm35 temperature is: %2.2f C\n", temperature);
		*(double *)ret = temperature;
	} else if (strcmp(name, "oc_image") == 0) {
		struct timeb t;
		ftime(&t);
		/* prepare a image file then return its name to libsensor */
		char *file = NULL;
		char *cmd = NULL;
		/* note, according to asprintf, the 'file' need be freed
		   later, which will be done by libsensor */
		asprintf(&file, "image_%lld%s", 1000 * (long long)t.time + t.millitm, ".jpg");
		asprintf(&cmd, "capture %s 2>/dev/null", file);
		system(cmd);
		free(cmd);
		cmd = NULL;
		ret = (void*)file;
	} else if (strcmp(name, "image") == 0) {
		struct timeb t;
		ftime(&t);
		/* prepare a image file then return its name to libsensor */
		char *file = NULL;
		char *cmd = NULL;
		/* note, according to asprintf, the 'file' need be freed
		   later, which will be done by libsensor */
		asprintf(&file, "image_%lld%s", 1000 * (long long)t.time + t.millitm, ".jpg");
		asprintf(&cmd, "fswebcam -r 1280x720 --save %s 2>/dev/null", file);
		system(cmd);
		free(cmd);
		cmd = NULL;
		ret = (void*)file;
	} else if (strcmp(name, "serial_test") == 0) {
		unsigned char buf[32] = {};
		int fd = init_serial_port(SERISL_DEVICE);
		if (fd < 0) {
			*(double *)ret = 0;
		} else {
			int len = do_read_from_serial(buf, fd);
			dump_str(buf, len);
			*(double *)ret = 1;
		}
		clean_serial_port(fd);
	}

	return ret;
}