예제 #1
0
/*******************************************************************************
* Procedure:    tx_task
* Purpose:      Task that processes serial output for Nordic RF chip.
* Passed:
*   
* Returned:     nothing
* Globals:      none
*
* Date:         Author:             Comments:
*   2014-09-19  Neal Shurmantine    initial revision
*******************************************************************************/
void *tx_task(void * param)
{
    void *event_handle;
    uint32_t wait_time = WAIT_TIME_INFINITE;
    uint16_t event_mask = RF_SERIAL_TX_EVENT_BIT;
    uint16_t event_active;

//#define SERIAL_PORT_NAME  "/dev/ttyAMA0"
    if (setup_serial_port() == false) {
        OS_Error(OS_ERR_SERIAL_PORT);
    }
    
    event_handle = OS_EventCreate(0,false);
    TxMailbox = OS_MboxCreate(event_handle,RF_SERIAL_TX_EVENT_BIT);
printf("tx_task\n");
    while(1) {
        event_active = OS_TaskWaitEvents(event_handle, event_mask, wait_time);
        if (event_active == RF_SERIAL_TX_EVENT_BIT) {
            tx_msg_to_uart();
            //give a little space between stacked up messages
            event_mask = 0;
            wait_time = 20;
        }
        else {
            event_mask = RF_SERIAL_TX_EVENT_BIT;
            wait_time = WAIT_TIME_INFINITE;
       }
   }
}
예제 #2
0
// Returns true => Success
bool SSerialInterface::open_block( )
{
	// OPEN SERIAL PORT : 

	_fd = open( _cl_port, O_RDWR );	// | O_NONBLOCK
	serial_poll.fd = _fd;
	bool result = setup_serial_port( );
	result &= (serial_poll.fd > 0);
	return result;
}
예제 #3
0
void __init prom_init(void)
{
	DDR_t ddr = (DDR_t) DDR_VirtualAddress; /* define the pointer to the DDR registers */
	phys_t memsize = 0-ddr->ddrmask;
	
	/* this should be the very first message, even before serial is properly initialized */
	prom_setup_cmdline();
	setup_serial_port();

	mips_machgroup = MACH_GROUP_MIKROTIK;
	soft_reboot = read_c0_status() & SR_NMI;
	pm_power_off = NULL;

	/*
	 * give all RAM to boot allocator,
	 * except for the first 0x400 and the last 0x200 bytes
	 */
	add_memory_region(ddr->ddrbase + 0x400, memsize - 0x600, BOOT_MEM_RAM);
}
예제 #4
0
int main(int argc, char *argv[]){
	char* serial_port_name;
	if (argc ==1){
		serial_port_name = SERIALPORTDEFAULT;
	}else if (argc == 2){ // if a command line argument was supplied
		serial_port_name = argv[1];
	}else if(argc > 2){
		printf("Usage is: monitor [serial port name]\n");
	}
	PRINTDEBUG&&printf("main called..\n");
	setup_serial_port(serial_port_name);
	PRINTDEBUG&&printf("done setup...\n");
	
	// setup the log file
	logfile = open("temp_log",O_WRONLY|O_APPEND|O_CREAT,0666);// the 0666 is an octal number which sets up proper permissions so we can actually open the log file after
	write(serialport,"\r\n",2); // send a 'wake up' to the avr
	clear_serial_command_buffer();
	while(1){
		read_from_serial_port();
	}
	return quit();
}
예제 #5
0
int main(int argc,char **argv)
{
  if ((argc<3|| argc>4)
      ||(argc==4&&strcasecmp(argv[3],"force"))) {
    fprintf(stderr,"usage: flash900 <firmware> <serial port> [force]\n");
    exit(-1);
  }

  int fd=open(argv[2],O_RDWR);
  if (fd==-1) {
    fprintf(stderr,"Could not open serial port '%s'\n",argv[2]);
    exit(-1);
  }
    if (set_nonblock(fd)) {
      fprintf(stderr,"Could not set serial port '%s' non-blocking\n",argv[2]);
      exit(-1);
    }
    
    int speeds[8]={230400,115200,57600,38400,19200,9600,2400,1200};
    int speed_count=8;

  printf("Trying to get command mode...\n");
  int i;
  for(i=0;i<speed_count;i++) {
    // set port speed and non-blocking, and disable CTSRTS 
    if (setup_serial_port(fd,speeds[i])) {
      fprintf(stderr,"Could not setup serial port '%s'\n",argv[2]);
      exit(-1);
    }

    int last_char = 0;
    
    // Make sure we have left command mode and bootloader mode
    // 0 = $30 = bootloader reboot command
    unsigned char cmd[260]; bzero(&cmd[0],260);
    
    printf("Checking if stuck in bootloader\n");
    // Make sure there is no command in progress with the boot loader
    write(fd,cmd,260);
    // Try to sync with bootloader if it is already running
    cmd[0]=GET_DEVICE;
    cmd[1]=EOC;
    write(fd,cmd,2);
    unsigned char bootloaderdetect[4]={0x43,0x91,0x12,0x10};
    int state=0;
    long long timeout=gettime_ms()+1250;
    while(gettime_ms()<timeout) {
      unsigned char buffer[2];
      int r=read(fd,buffer,1);
      if (r==1) {
	//	printf("  read %02X\n",buffer[0]);
	if (buffer[0]==bootloaderdetect[state]) state++; else state=0;
	if (state==4) {
	  printf("Looks like we are in the bootloader already\n");
	  break;
	}
      }
    }
    
    if (state==4)
      printf("Detected RFD900 is already in bootloader\n");
    else
      {
	printf("Trying to switch to AT command mode\n");
	write(fd,"\b\b\b\b\b\b\b\b\b\b\b\b\r",14);
	// give it time to process the above, so that the first character of ATO
	// doesn't get eaten.
	usleep(10000);        
	write(fd,"ATO\r",4);
	// sleep(2); // allow 2 sec to reboot if it was in bootloader mode already
	
	// now try to get to AT command mode
	sleep(1);
	write(fd,"+++",3);
	
	// now wait for upto 1.2 seconds for "OK" 
	timeout=gettime_ms()+1200;
	state=0;
	while(gettime_ms()<timeout) {
	  char buffer[1];
	  int r=read(fd,buffer,1);
	  if (r==1) {
	    //	    printf("  read %02X\n",buffer[0]);
	    if ((buffer[0]=='K') && (last_char == 'O')) state=2; else state=0;
	    last_char = buffer[0];
	    if (state==2) break;
	  } else usleep(10000);
	}
	if (state==2) {	 
	  // try AT&UPDATE or ATS1=115\rAT&W\rATZ if the modem isn't already on 115200bps
	  printf("Switching to boot loader...\n");
	  char *cmd="AT&UPDATE\r\n";

	  if (speeds[i]==115200) {
	    write(fd,cmd,strlen(cmd));
	  } else {
	    char *cmd="ATS1=115\r\n";
	    write(fd,cmd,strlen(cmd));
	    sleep(1);
	    cmd="AT&W\r\n";
	    write(fd,cmd,strlen(cmd));
	    sleep(1);
	    cmd="ATZ\r\n";	  
	    write(fd,cmd,strlen(cmd));
	    sleep(1);

	    // Go back to looking for modem at 115200
	    printf("Changing modem from %d to 115200bps\n",
		   speeds[i]);
	    i=-1; continue;
	  }
	  
	  // then switch to 115200 regardless of the speed we were at,
	  // since the bootloader always talks 115200
	  setup_serial_port(fd,115200);
      
	  // give time to switch to boot loader
	  // and consume any characters that arrive in the meantime
	  timeout=gettime_ms()+1500;
	  while(gettime_ms()<timeout) {
	    unsigned char buffer[2];
	    int r=read(fd,(char *)buffer,1);
	    // fprintf(stderr,"Read %02X from bootloader.\n",buffer[0]);
	    if (r!=1) usleep(100000); else {
	      // printf("  read %02X\n",buffer[0]);
	    }
	  }
      
	  state=4;
	}
      }
    if (state==4) {
      // got command mode (probably)
      printf("Got OK at %d\n",speeds[i]);
      
      // ask for board ID
      unsigned char cmd[1024];
      cmd[0]=GET_DEVICE;
      cmd[1]=EOC;
      write(fd,cmd,2);

      int id = next_char(fd);
      int freq = next_char(fd);
      expect_insync(fd);
      expect_ok(fd);
     
      char filename[1024];
      snprintf(filename,1024,"%s-%02X-%02X.ihx",argv[1],id,freq);

      printf("Board id = $%02x, freq = $%02x : Will load firmware from '%s'\n",
	     id,freq,filename);
      
      ihex_recordset_t *ihex=ihex_rs_from_file(filename);
      if (!ihex) {
	fprintf(stderr,"Could not read intelhex from file '%s'\n",filename);

	// Reboot radio
	write(fd,"0",1);
	
	exit(-2);
      }
      printf("Read %d IHEX records from firmware file\n",ihex->ihrs_count);
      
      // Sort IHEX records into ascending address order so that when we flash 
      // them we don't mess things up by writing the flash data in the wrong order 
      qsort(ihex->ihrs_records,ihex->ihrs_count,sizeof(ihex_record_t),
	    compare_ihex_record);
      
      int i;
      if (0)
	for(i=0;i<ihex->ihrs_count;i++)
	  printf("$%04x - $%04x\n",
		 ihex->ihrs_records[i].ihr_address,
		 ihex->ihrs_records[i].ihr_address+
		 ihex->ihrs_records[i].ihr_length-1);
      
      
      // Reset parameters
      cmd[0]=PARAM_ERASE;
      cmd[1]=EOC;
      write(fd,cmd,2);
      expect_insync(fd);
      expect_ok(fd);

      printf("Erased parameters.\n");

      // Program all parts of the firmware and verify that that got written
      printf("Checking if the radio already has this version of firmware...\n");
      int fail=0;
      if (argc==3) {
	// read flash and compare with ihex records
	unsigned char buffer[65536];
	printf("Bulk reading from flash...\n");
	read_64kb_flash(fd,buffer);
	printf("Read all 64KB flash. Now verifying...\n");
	
	int i;
	for(i=0;i<ihex->ihrs_count;i++)
	  if (ihex->ihrs_records[i].ihr_type==0x00)
	    {
	      if (fail) break;
	      
	      if (memcmp(&buffer[ihex->ihrs_records[i].ihr_address],
			 ihex->ihrs_records[i].ihr_data,
			 ihex->ihrs_records[i].ihr_length))
		fail=1;
	    }
	
      }
      if ((argc==4)||fail)
	{
	  printf("\nFirmware differs: erasing and flashing...\n");

	  // Erase ROM
	  printf("Erasing flash.\n");
	  cmd[0]=CHIP_ERASE;
	  cmd[1]=EOC;
	  write(fd,cmd,2);
	  expect_insync(fd);
	  expect_ok(fd);

	  // Write ROM
	  printf("Flash erased, now writing new firmware.\n");
	  write_or_verify_flash(fd,ihex,1);
	}

      // Reboot radio
      write(fd,"0",1);

      break;
    } else {
      printf("Modem doesn't seem to be at %dbps\n",speeds[i]);
    }
    
  }

  return 0;
}