Esempio n. 1
0
int main(void)
{
	uint8_t result;
	
	///////////////////////////////////////
	sys_state = SYS_INIT;
	sd_avlb = 1;
	speeder_avlb = 1;
	gps_avlb = 1;
	///////////////////////////////////////
	
/////////////////////////////////////////////////////////////
	result = init();
	if(result)
	{
#if DEBUG
		uart_puts_p(PSTR("\nINIT Failed! \n\n"));
#endif		
		Disp_Clear();
		XY_Str_P(0,0,PSTR("NO SD Card! -"));
		XY_Num(13,0,result);
		XY_Char(11,1,m_arrow);
		XY_Str_P(12,1,PSTR("SKIP"));
		//XY_Str_P(12,1,m_item[4]);
		sd_avlb = 0;
		timer1_start();
		
		while(!(key_scan() == KEY_OK))
			_delay_ms(100);
		sei();
	}
#if DEBUG
	else
		uart_puts_p(PSTR("\nINIT Completed! \n\n"));
#endif
	sys_state = SYS_READY;
/////////////////////////////////////////////////////////////


	while(1)
	{
	/////////////////////////////////////////////////////////////	
#if DEBUG	
		uart_puts_p(PSTR("get_ready() start! \n"));
#endif
		while(sys_state == SYS_READY)
			get_ready();
	/////////////////////////////////////////////////////////////

				
	/////////////////////////////////////////////////////////////	
#if DEBUG	
		uart_puts_p(PSTR("task() start! \n"));
#endif	
		while(sys_state == SYS_WORKING)
			task();
	/////////////////////////////////////////////////////////////
	}
	return 0;
}
Esempio n. 2
0
static void PrintSensorData(const char *what,int data) {

#ifdef UART_DEBUG
  uart_puts( what ); 
  uart_puts_p( cEqualSign );
  int2uart( data );
  uart_puts_p( cBlank );
#endif // UART_DEBUG
}
Esempio n. 3
0
void send_sensor(unsigned char i)
{
    char temp_str[16] = "0";
    utoa(i,temp_str,10);
    uart_puts(temp_str);
    uart_puts_p(PSTR(": "));
    utoa(sensors[i],temp_str,10);
    uart_puts(temp_str);
    uart_puts_p(PSTR("\n\r\n\r"));
}
Esempio n. 4
0
int help_function (char *param) {
    /* cli_entry_tab is only read, hopefully this is not
     * distorting our interrupt, in case a new cmdline is processed */
    uint8_t i;

    uart_puts_p(PSTR("Available commands:\n\r"));
    for (i = 0; i < cli_entries ; i++) {
        uart_puts_p(cli_entry_tab[i].name);
        uart_puts_p(PSTR("\t"));
        uart_puts_p(cli_entry_tab[i].usage);
        uart_puts_p(PSTR("\n\r"));
    }
    return 0;
}
Esempio n. 5
0
void get_ready(void)
{
	menu_init();
#if DEBUG	
	uart_puts_p(PSTR("menu_init() Completed! \n"));
#endif

	LED_on(LED_0);
	LED_on(LED_1);
	LED_on(LED_2);
	LED_on(LED_3);
	
	set_screen(SPLASH_SCN);
	_delay_ms(2000);
	
	gps_set_arg();
	
	LED_off(LED_0);
	LED_off(LED_1);
	LED_off(LED_2);
	LED_off(LED_3);
	
	set_screen(READY_SCN);
	menu_start();	
}
Esempio n. 6
0
void send_sensors()
{
    unsigned char i = 0;
    char temp_str[16] = "0";
    for (i = 0;i < 6 ;i++ )
    {
        utoa(i,temp_str,10);
        uart_puts(temp_str);
        uart_puts_p(PSTR(": "));
        //utoa((sensors[i]*5)/1024.0,temp_str,10);
        dtostrf((sensors[i]*5)/1024.0,1,5,temp_str);
        uart_puts(temp_str);
        uart_puts_p(PSTR("\n\r"));
    }

    uart_puts_p(PSTR("\n\r"));
}
Esempio n. 7
0
int main(void) {

  InitLEDPort();
  
  YellowLEDOn();
  delay_sec(1);
  YellowLEDOff();
  delay_sec(1);
  YellowLEDOn();

  delay_sec(1);
    
  i2c_init();                                // init I2C interface
#ifdef UART_DEBUG
  uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
  
  sei();

  uart_puts_P("\r\n'lsm303test' ready!\r\n");
#endif // UART_DEBUG

#ifdef LSM303DLH_USE_ACC
  LSM303DLHInitACC( I2C_DEV_LSM303DLH_ACC1 );
#endif // LSM303DLH_USE_ACC

#ifdef LSM303DLH_USE_MAG
  LSM303DLHInitMAG( I2C_DEV_LSM303DLH_MAG );
#endif // LSM303DLH_USE_MAG

  while ( 1 ) {

#ifdef UART_DEBUG
    uart_puts_P("\r\nRunning test... ");
#endif // UART_DEBUG

#if (defined LSM303DLH_USE_ACC) || (defined LSM303DLH_USE_MAG)

# ifdef LSM303DLH_USE_ACC
    LSM303DLHTestACC();
# endif // LSM303DLH_USE_ACC
# ifdef LSM303DLH_USE_MAG
    LSM303DLHTestMAG();
# endif // LSM303DLH_USE_MAG

#else

# ifdef UART_DEBUG
    uart_puts_p( cCRLF );
# endif // UART_DEBUG

    delay_sec(2);
#endif // LSM303DLH_USE_ACC || LSM303DLH_USE_MAG
  }
      
  return 0;
}
Esempio n. 8
0
void cli_init(void)
{
	led = 0;
	cursor = 0;

	for(uint8_t i = 0; i <= CMD_LEN; i++) {
		cmd[i] = '\0';
		hist[i] = '\0';
	}
	uart_puts_p(PSTR("> "));
}
Esempio n. 9
0
static uint8_t ieee_talk_handler (void)
{
  buffer_t *buf;
  uint8_t finalbyte;
  uint8_t c;
  uint8_t res;

  buf = find_buffer(ieee_data.secondary_address);
  if(buf == NULL) return -1;

  while (buf->read) {
    do {
      finalbyte = (buf->position == buf->lastused);
      c = buf->data[buf->position];
      if (finalbyte && buf->sendeoi) {
        /* Send with EOI */
        res = ieee_putc(c, 1);
        if(!res) uart_puts_p("EOI: ");
      } else {
        /* Send without EOI */
        res = ieee_putc(c, 0);
      }
      if(res) {
        if(res==0xfc) {
          uart_puts_P(PSTR("*** TIMEOUT ABORT***")); uart_putcrlf();
        }
        if(res!=0xfd) {
          uart_putc('c'); uart_puthex(res);
        }
        return 1;
      } else {
        uart_putc('>');
        uart_puthex(c); uart_putc(' ');
        if(isprint(c)) uart_putc(c); else uart_putc('?');
        uart_putcrlf();
      }
    } while (buf->position++ < buf->lastused);

    if(buf->sendeoi && ieee_data.secondary_address != 0x0f &&
      !buf->recordlen && buf->refill != directbuffer_refill) {
      buf->read = 0;
      break;
    }

    if (buf->refill(buf)) {
      return -1;
    }

    /* Search the buffer again, it can change when using large buffers */
    buf = find_buffer(ieee_data.secondary_address);
  }
  return 0;
}
Esempio n. 10
0
char handshake()
{
	uart_flush();
	_delay_ms(100);
	
	while(uart_available()==0)
	{

		PORTB |=(1<<PB5);
		_delay_ms(100);
		PORTB &=~(1<<PB5);
		_delay_ms(100);
		
	}
	
	if (uart_getc() == 'a')
	{
		PORTB |=(1<<PB5);
		_delay_ms(50);
		PORTB &=~(1<<PB5);
		_delay_ms(50);
		PORTB |=(1<<PB5);
		_delay_ms(50);
		PORTB &=~(1<<PB5);
		_delay_ms(50);
		
	}
	
	uart_putc('b');
	while(uart_available()==0)
	{
		PORTB |=(1<<PB5);
		_delay_ms(20);
		
		PORTB &= ~(1<<PB5);
		_delay_ms(20);
	}
	
	if (uart_getc() == 'c')
	{
		uart_putc('d');
		PortMode = REGULAR;
		return(0);  // handshake success
	}
	else
	{
		uart_puts_p(PSTR("\n Debug mode"));
		PortMode = DEBUG;
		return(1);  // debug mode
	}
}
Esempio n. 11
0
int
main(void)
{
  ioinit();
  /* stderr = stdout = stdin = &uart_str; */

  uart_puts_p(PSTR("Welcome to the DR - CLI\n\rType help!\n\r"));

  /* everything should be triggered by Timer0 */
  for (;;)
    ;

  return 0;
}
Esempio n. 12
0
int main(void)
{

    char millis_str[16] = "0";
    char temp_str[16] = "0";
    unsigned long delay = 1000;
    unsigned long start_millis;
    unsigned char i = 0;

    init();

    sei(); // set global interrupt

    ADCSRA |= (1<<ADSC);
    uart_puts_p(PSTR("Hello World\n\r"));

    //_delay_ms(1000);

    start_millis = millis;

    ADMUX = (ADMUX&0xF0)|(0x04);

    for(;;)
    {
        if( (millis - start_millis) == delay)
        {
//            uart_puts_P("millis = ");
//            utoa(millis,millis_str,10);
//            uart_puts(millis_str);
//            uart_puts_P("\n\r");


            send_sensors();

            //send_sensor(0);


            start_millis = millis;
            //TOGGLE_LED();
            ADC_flag = 1;
            ADCSRA |= (1<<ADSC);
            CLEAR_LED();

        }
    }

    return 0;
}
Esempio n. 13
0
static void LSM303DLHTestACC(void) {

#ifdef UART_DEBUG
  uart_puts_P("\r\n");
#endif // UART_DEBUG

  LSM303DLHData acc_data;

  // --- read accelerometer values
  
  uint8_t ret = LSM303DLHReadACC( I2C_DEV_LSM303DLH_ACC1, &acc_data );

  if ( ret ) { 

    YellowLEDOff();
    RedLEDOn(); 			     // show error

#ifdef UART_DEBUG
    if ( ret == I2C_ERR_NO_DEVICE ) uart_puts_P("ACC: I2C_ERR_NO_DEV\r\n");
#endif // UART_DEBUG

#ifdef UART_DEBUG
    if ( ret == I2C_ERROR ) uart_puts_P("ACC: I2C_ERROR\r\n");
#endif // UART_DEBUG

    return;
  }

  PrintSensorData( "AX", acc_data.fSensorX );
  PrintSensorData( "AY", acc_data.fSensorY );
  PrintSensorData( "AZ", acc_data.fSensorZ );
  
  uart_puts_p( cCRLF );
  
  delay_sec(1);
}
Esempio n. 14
0
static void LSM303DLHTestMAG(void) {

#ifdef UART_DEBUG
  uart_puts_P("\r\n");
#endif // UART_DEBUG

  LSM303DLHData mag_data;
  
  // --- read magnetometer values
  
  uint8_t ret = LSM303DLHReadMAG( I2C_DEV_LSM303DLH_MAG, &mag_data );
  
  if ( ret ) { 

    YellowLEDOff();
    RedLEDOn(); 			     // show error

#ifdef UART_DEBUG
    if ( ret == I2C_ERR_NO_DEVICE ) uart_puts_P("MAG: I2C_ERR_NO_DEV\r\n");
#endif // UART_DEBUG

#ifdef UART_DEBUG
    if ( ret == I2C_ERROR ) uart_puts_P("MAG: I2C_ERROR\r\n");
#endif // UART_DEBUG

    return;
  }

  PrintSensorData( "MX", mag_data.fSensorX );
  PrintSensorData( "MY", mag_data.fSensorY );
  PrintSensorData( "MZ", mag_data.fSensorZ );

  uart_puts_p( cCRLF );

  delay_sec(1);
}
Esempio n. 15
0
static int16_t ieee_listen_handler (uint8_t cmd)
/* Receive characters from IEEE-bus and write them to the
   listen buffer adressed by ieee_data.secondary_address.
   If a new command is received (ATN set), return it
*/
{
  buffer_t *buf;
  int16_t c;

  ieee_data.secondary_address = cmd & 0x0f;
  buf = find_buffer(ieee_data.secondary_address);

  /* Abort if there is no buffer or it's not open for writing */
  /* and it isn't an OPEN command                             */
  if ((buf == NULL || !buf->write) && (cmd & 0xf0) != 0xf0) {
    uart_putc('c');
    return -1;
  }

  switch(cmd & 0xf0) {
    case 0x60:
      uart_puts_p("DATA L ");
      break;
    case 0xf0:
      uart_puts_p("OPEN ");
      break;
    default:
      uart_puts_p("Unknown LH! ");
      break;
  }
  uart_puthex(ieee_data.secondary_address);
  uart_putcrlf();

  c = -1;
  for(;;) {
    /* Get a character ignoring timeout but but watching ATN */
    while((c = ieee_getc()) < 0);
    if (c  & FLAG_ATN) return c;

    uart_putc('<');
    if (c & FLAG_EOI) {
      uart_puts_p("EOI ");
      ieee_data.ieeeflags |= EOI_RECVD;
    } else ieee_data.ieeeflags &= ~EOI_RECVD;

    uart_puthex(c); uart_putc(' ');
    c &= 0xff; /* needed for isprint */
    if(isprint(c)) uart_putc(c); else uart_putc('?');
    uart_putcrlf();

    if((cmd & 0x0f) == 0x0f || (cmd & 0xf0) == 0xf0) {
      if (command_length < CONFIG_COMMAND_BUFFER_SIZE)
        command_buffer[command_length++] = c;
      if (ieee_data.ieeeflags & EOI_RECVD)
        /* Filenames are just a special type of command =) */
        ieee_data.ieeeflags |= COMMAND_RECVD;
    } else {
      /* Flush buffer if full */
      if (buf->mustflush) {
        if (buf->refill(buf)) return -2;
        /* Search the buffer again,                     */
        /* it can change when using large buffers       */
        buf = find_buffer(ieee_data.secondary_address);
      }

      buf->data[buf->position] = c;
      mark_buffer_dirty(buf);

      if (buf->lastused < buf->position) buf->lastused = buf->position;
      buf->position++;

      /* Mark buffer for flushing if position wrapped */
      if (buf->position == 0) buf->mustflush = 1;

      /* REL files must be syncronized on EOI */
      if(buf->recordlen && (ieee_data.ieeeflags & EOI_RECVD)) {
        if (buf->refill(buf)) return -2;
      }
    }   /* else-buffer */
  }     /* for(;;) */
}
Esempio n. 16
0
uint8_t print_disk_info(const struct fat_fs_struct* fs)
{
    if(!fs)
        return 0;

    struct sd_raw_info disk_info;
    if(!sd_raw_get_info(&disk_info))
        return 0;

    uart_puts_p(PSTR("manuf:  0x")); uart_putc_hex(disk_info.manufacturer); uart_putc('\n');
    uart_puts_p(PSTR("oem:    ")); uart_puts((char*) disk_info.oem); uart_putc('\n');
    uart_puts_p(PSTR("prod:   ")); uart_puts((char*) disk_info.product); uart_putc('\n');
    uart_puts_p(PSTR("rev:    ")); uart_putc_hex(disk_info.revision); uart_putc('\n');
    uart_puts_p(PSTR("serial: 0x")); uart_putdw_hex(disk_info.serial); uart_putc('\n');
    uart_puts_p(PSTR("date:   ")); uart_putw_dec(disk_info.manufacturing_month); uart_putc('/');
                                   uart_putw_dec(disk_info.manufacturing_year); uart_putc('\n');
    uart_puts_p(PSTR("size:   ")); uart_putdw_dec(disk_info.capacity / 1024 / 1024); uart_puts_p(PSTR("MB\n"));
    uart_puts_p(PSTR("copy:   ")); uart_putw_dec(disk_info.flag_copy); uart_putc('\n');
    uart_puts_p(PSTR("wr.pr.: ")); uart_putw_dec(disk_info.flag_write_protect_temp); uart_putc('/');
                                   uart_putw_dec(disk_info.flag_write_protect); uart_putc('\n');
    uart_puts_p(PSTR("format: ")); uart_putw_dec(disk_info.format); uart_putc('\n');
    uart_puts_p(PSTR("free:   ")); uart_putdw_dec(fat_get_fs_free(fs)); uart_putc('/');
                                   uart_putdw_dec(fat_get_fs_size(fs)); uart_putc('\n');

    return 1;
}
Esempio n. 17
0
int main()
{
    /* we will just use ordinary idle mode */
    set_sleep_mode(SLEEP_MODE_IDLE);

    /* setup uart */
    uart_init();

    while(1)
    {


        /* setup sd card slot */
        if(!sd_raw_init())
        {
#if DEBUG
            uart_puts_p(PSTR("MMC/SD initialization failed\n"));
#endif
            continue;
        }

        /* open first partition */
        struct partition_struct* partition = partition_open(sd_raw_read,
                                                            sd_raw_read_interval,
#if SD_RAW_WRITE_SUPPORT
                                                            sd_raw_write,
                                                            sd_raw_write_interval,
#else
                                                            0,
                                                            0,
#endif
                                                            0
                                                           );

        if(!partition)
        {
            /* If the partition did not open, assume the storage device
             * is a "superfloppy", i.e. has no MBR.
             */
            partition = partition_open(sd_raw_read,
                                       sd_raw_read_interval,
#if SD_RAW_WRITE_SUPPORT
                                       sd_raw_write,
                                       sd_raw_write_interval,
#else
                                       0,
                                       0,
#endif
                                       -1
                                      );
            if(!partition)
            {
#if DEBUG
                uart_puts_p(PSTR("opening partition failed\n"));
#endif
                continue;
            }
        }

        /* open file system */
        struct fat_fs_struct* fs = fat_open(partition);
        if(!fs)
        {
#if DEBUG
            uart_puts_p(PSTR("opening filesystem failed\n"));
#endif
            continue;
        }

        /* open root directory */
        struct fat_dir_entry_struct directory;
        fat_get_dir_entry_of_path(fs, "/", &directory);

        struct fat_dir_struct* dd = fat_open_dir(fs, &directory);
        if(!dd)
        {
#if DEBUG
            uart_puts_p(PSTR("opening root directory failed\n"));
#endif
            continue;
        }

        /* print some card information as a boot message */
        print_disk_info(fs);

        /* provide a simple shell */
        char buffer[24];
        while(1)
        {
            /* print prompt */
            uart_putc('>');
            uart_putc(' ');

            /* read command */
            char* command = buffer;
            if(read_line(command, sizeof(buffer)) < 1)
                continue;

            /* execute command */
            if(strcmp_P(command, PSTR("init")) == 0)
            {
                break;
            }
            else if(strncmp_P(command, PSTR("cd "), 3) == 0)
            {
                command += 3;
                if(command[0] == '\0')
                    continue;

                /* change directory */
                struct fat_dir_entry_struct subdir_entry;
                if(find_file_in_dir(fs, dd, command, &subdir_entry))
                {
                    struct fat_dir_struct* dd_new = fat_open_dir(fs, &subdir_entry);
                    if(dd_new)
                    {
                        fat_close_dir(dd);
                        dd = dd_new;
                        continue;
                    }
                }

                uart_puts_p(PSTR("directory not found: "));
                uart_puts(command);
                uart_putc('\n');
            }
            else if(strcmp_P(command, PSTR("ls")) == 0)
            {
                /* print directory listing */
                struct fat_dir_entry_struct dir_entry;
                while(fat_read_dir(dd, &dir_entry))
                {
                    uint8_t spaces = sizeof(dir_entry.long_name) - strlen(dir_entry.long_name) + 4;

                    uart_puts(dir_entry.long_name);
                    uart_putc(dir_entry.attributes & FAT_ATTRIB_DIR ? '/' : ' ');
                    while(spaces--)
                        uart_putc(' ');
                    uart_putdw_dec(dir_entry.file_size);
                    uart_putc('\n');
                }
            }
            else if(strncmp_P(command, PSTR("cat "), 4) == 0)
            {
                command += 4;
                if(command[0] == '\0')
                    continue;

                /* search file in current directory and open it */
                struct fat_file_struct* fd = open_file_in_dir(fs, dd, command);
                if(!fd)
                {
                    uart_puts_p(PSTR("error opening "));
                    uart_puts(command);
                    uart_putc('\n');
                    continue;
                }

                /* print file contents */
                uint8_t buffer[8];
                uint32_t offset = 0;
                while(fat_read_file(fd, buffer, sizeof(buffer)) > 0)
                {
                    uart_putdw_hex(offset);
                    uart_putc(':');
                    for(uint8_t i = 0; i < 8; ++i)
                    {
                        uart_putc(' ');
                        uart_putc_hex(buffer[i]);
                    }
                    uart_putc('\n');
                    offset += 8;
                }

                fat_close_file(fd);
            }
            else if(strcmp_P(command, PSTR("disk")) == 0)
            {
                if(!print_disk_info(fs))
                    uart_puts_p(PSTR("error reading disk info\n"));
            }
#if FAT_WRITE_SUPPORT
            else if(strncmp_P(command, PSTR("rm "), 3) == 0)
            {
                command += 3;
                if(command[0] == '\0')
                    continue;

                struct fat_dir_entry_struct file_entry;
                if(find_file_in_dir(fs, dd, command, &file_entry))
                {
                    if(fat_delete_file(fs, &file_entry))
                        continue;
                }

                uart_puts_p(PSTR("error deleting file: "));
                uart_puts(command);
                uart_putc('\n');
            }
            else if(strncmp_P(command, PSTR("touch "), 6) == 0)
            {
                command += 6;
                if(command[0] == '\0')
                    continue;

                struct fat_dir_entry_struct file_entry;
                if(!fat_create_file(dd, command, &file_entry))
                {
                    uart_puts_p(PSTR("error creating file: "));
                    uart_puts(command);
                    uart_putc('\n');
                }
            }
            else if(strncmp_P(command, PSTR("write "), 6) == 0)
            {
                command += 6;
                if(command[0] == '\0')
                    continue;

                char* offset_value = command;
                while(*offset_value != ' ' && *offset_value != '\0')
                    ++offset_value;

                if(*offset_value == ' ')
                    *offset_value++ = '\0';
                else
                    continue;

                /* search file in current directory and open it */
                struct fat_file_struct* fd = open_file_in_dir(fs, dd, command);
                if(!fd)
                {
                    uart_puts_p(PSTR("error opening "));
                    uart_puts(command);
                    uart_putc('\n');
                    continue;
                }

                int32_t offset = strtolong(offset_value);
                if(!fat_seek_file(fd, &offset, FAT_SEEK_SET))
                {
                    uart_puts_p(PSTR("error seeking on "));
                    uart_puts(command);
                    uart_putc('\n');

                    fat_close_file(fd);
                    continue;
                }

                /* read text from the shell and write it to the file */
                uint8_t data_len;
                while(1)
                {
                    /* give a different prompt */
                    uart_putc('<');
                    uart_putc(' ');

                    /* read one line of text */
                    data_len = read_line(buffer, sizeof(buffer));
                    if(!data_len)
                        break;

                    /* write text to file */
                    if(fat_write_file(fd, (uint8_t*) buffer, data_len) != data_len)
                    {
                        uart_puts_p(PSTR("error writing to file\n"));
                        break;
                    }
                }

                fat_close_file(fd);
            }
            else if(strncmp_P(command, PSTR("mkdir "), 6) == 0)
            {
                command += 6;
                if(command[0] == '\0')
                    continue;

                struct fat_dir_entry_struct dir_entry;
                if(!fat_create_dir(dd, command, &dir_entry))
                {
                    uart_puts_p(PSTR("error creating directory: "));
                    uart_puts(command);
                    uart_putc('\n');
                }
            }
#endif
#if SD_RAW_WRITE_BUFFERING
            else if(strcmp_P(command, PSTR("sync")) == 0)
            {
                if(!sd_raw_sync())
                    uart_puts_p(PSTR("error syncing disk\n"));
            }
#endif

            else if(strcmp_P(command, PSTR("sync")) == 0)
            {
                // vytvor adresar mereni_teploty

                // nekonecna smycka - pomoci RTCka kazdou minutu odmer teplotu
            }


            else
            {
                uart_puts_p(PSTR("unknown command: "));
                uart_puts(command);
                uart_putc('\n');
            }
        }

        /* close directory */
        fat_close_dir(dd);

        /* close file system */
        fat_close(fs);

        /* close partition */
        partition_close(partition);
    }

    return 0;
}
Esempio n. 18
0
void loop(){ //one frame
    static uint8_t escape_state = 0;
    uint16_t receive_state = 1;
    //primitive somewhat messy state machine of the uart interface
    do{ //Always empty the receive buffer since there are _delay_xxs in the following code and thus this might not run all that often.
        receive_state = uart_getc();
        char c = receive_state&0xFF;
        receive_state &= 0xFF00;
        //escape code format:
        // \\   - backslash
        // \n   - newline
        // \[x] - x
        //eats [n] commands:   's' (0x73) led value                         sets led number [led] to [value]
        //                     'b' (0x62) buffer buffer buffer buffer       sets the whole frame buffer
        //                     'a' (0x61) meter value                       sets analog meter number [meter] to [value]
        //                     'r' (0x72)                                   read the frame buffer
        //                     'd' (0x64) display digit digit digit digit   sets the 7-segment display (CAUTION: "display" currently is ignored)
        //this device will utter a "'c' (0x63) num state" when switch [num] changes state to [state]
        //commands are terminated by \n
        if(!receive_state){
            if(!escape_state){
                if(c == '\\'){
                    receive_state |= 0x02;
                    escape_state = 1;
                }else if(c == '\n'){
                    receive_state |= 0x02;
                    state = 0;
                }
            }else{
                receive_state = 0;
                escape_state = 0;
                switch(c){
                    case '\\':
                        break;
                    case 'n':
                        c = '\n';
                        break;
                }
            }
        }
        if(!receive_state){
            switch(state){
                case 0: //Do not assume anything about the variables used
                    //command char
                    switch(c){
#ifdef HAS_LED_SUPPORT
                        case 's':
                            state = 2;
                            break;
                        case 'b':
                            nbuf = 0;
                            state = 4;
                            break;
#endif//HAS_LED_SUPPORT
#ifdef HAS_PWM_SUPPORT
                        case 'a':
                            state = 5;
                            nbuf = 0;
                            break;
#endif//HAS_PWM_SUPPORT
#ifdef HAS_NOISE_MAKER_SUPPORT
                        case 'x':
                            //DDRF |= 0x01;
                            //PORTF |= 0x01;
                            break;
                        case 'X':
                            //DDRF &= 0xFE;
                            //PORTF &= 0xFE;
                            break;
#endif//HAS_NOISE_MAKER_SUPPORT
#ifdef HAS_LED_SUPPORT
                        case 'r':
                            uart_putc('r');
                            for(uint8_t i=0; i<sizeof(frameBuffer); i++){
                                uart_puthex(frameBuffer[i]);
                            }
                            uart_putc('\n');
                            break;
#endif//HAS_LED_SUPPORT
#ifdef HAS_7SEG_SUPPORT
                        case 'd':
                            nbuf = 0;
                            bpos = 0;
                            state = 7;
#endif//HAS_7SEG_SUPPORT
                    }
                    break;
#ifdef HAS_LED_SUPPORT
                case 2:
                    nbuf=c;
                    state = 3;
                    break;
                case 3:
                    setLED(nbuf, c);
                    state = 0;
                    break;
                case 4:
                    secondFrameBuffer[(uint8_t) nbuf] = c;
                    nbuf++;
                    if(nbuf == 7){
                        swapBuffers();
                        state = 0;
                    }
                    break;
#endif//HAS_LED_SUPPORT
#ifdef HAS_PWM_SUPPORT
                case 5:
                    if(c > PWM_COUNT)
                        c = 0;
                    nbuf = c;
                    if(nbuf >= PWM_COUNT)
                        nbuf = 0;
                    state = 6;
                    break;
                case 6:
                    pwm_val[(uint8_t) nbuf] = c;
                    uart_puts_p(PSTR("ACK\n"));
                    state = 0;
                    break;
#endif//HAS_PWM_SUPPORT
#ifdef HAS_7SEG_SUPPORT
                case 7:
                    nbuf = c;
                    state = 8;
                    break;
                case 8:
                    l7seg_buf[(uint8_t)bpos] = c;
                    bpos++;
                    if(bpos == 4){
                        state = 0;
                    }
                    break;
#endif//HAS_7SEG_SUPPORT
            }
        }
    }while(!receive_state);
    led_loop();
    r0ketbeam_loop();
    l7seg_loop();
    input_loop();
    pwm_loop();
    config_loop();
    _delay_ms(1);
}
int main(void)
{
static unsigned char eeprom_string[]__attribute__((section(".eeprom")))={"uart_puts_e testing..."};
static unsigned char flash_string[]__attribute__((progmem))="uart_puts_p testing...\n\r";

unsigned char        rx_buffer_contents[21];
int                  number=0;
unsigned char        string0[]="uart_putc testing...\n\r";
unsigned char        *ptr="uart_puts_e testing...";
unsigned char        in_char='\0';
unsigned char        c='\0';

#if    UART_DYNAMIC_BAUD_CHANGE == 1
/* 
    When UART_DYNAMIC_BAUD_CHANGE == 1, uart_init() takes the Baudrate and a command as parameters.
    Also note that if you use 2 UARTS, ONLY THE SELECTED UART WILL BE INITIALIZED!
    If you use 2 UARTS you should intialize the second UART also by another uart_init() command.
    You can select the UART to be initialized by giving a separate uart_command(SELECT_UARTx)
    prior to the uart_init() command, or within the same uart_init() statement.
*/
#if UART_MULTI_COMMAND_SUPPORT == 1        

/* UART 0 WILL BE INITIALIZED ONLY ! */ 
uart_init(115200, SELECT_UART0 | UART_RX_OFF | UART_TX_OFF | UART_ECHO_OFF);     

#elif UART_MULTI_COMMAND_SUPPORT == 0

/* UART 0 WILL BE INITIALIZED ONLY ! */ 
uart_init(115200, SELECT_UART0);  

#endif

#elif  UART_DYNAMIC_BAUD_CHANGE == 0
/* 
    When UART_DYNAMIC_BAUD_CHANGE == 0, Baudrate(s) is(are)fixed. uart_init() takes no parameters.
    Also note that if you use 2 UARTS, BOTH UARTS WILL BE INITIALIZED!
    You can still select Uart and give commands separetly to each UART.
*/
uart_init();  
#endif /* #elif  UART_DYNAMIC_BAUD_CHANGE == 0 */

/* ENABLE GLOBAL INTERRUPTS */
asm("sei");

#if    NUMBER_OF_UARTS == 2 && UART_DYNAMIC_BAUD_CHANGE == 1

uart_init(115200, SELECT_UART1);
uart_command(UART_TX_ON);
uart_puts_P("\r\nThis is the Second uart\r\n");
uart_command(UART_TX_OFF);
uart_command(SELECT_UART0);

#elif  NUMBER_OF_UARTS == 2 && UART_DYNAMIC_BAUD_CHANGE == 0

uart_command(SELECT_UART1);
uart_command(UART_TX_ON);
uart_puts_P("\r\nThis is the Second uart\r\n");
uart_command(UART_TX_OFF);
uart_command(SELECT_UART0);

#endif


    while(1)
        { 
          /* transmitting */
          uart_command( UART_TX_ON );
          uart_puts_P("\r\n");
          uart_puts("Constant String testing...\n\r");
          ptr=string0;
          while((c=*ptr++)) uart_putc(c);
          uart_puts_p(flash_string);
          uart_puts_P("uart_puts_P testing...\n\r");
          uart_puts_e(eeprom_string); uart_puts("\r\n");
          uart_command(UART_TX_OFF); uart_puts_P("This line should never be shown...\n\r");
          uart_command(UART_TX_ON);
          uart_puts_P("Number testing....\n\r");
          uart_puti((number=-123),0);   uart_puts("  "); 
          uart_puti((number=9999),0);   uart_puts("  ");
          uart_puti((number=-456),1);   uart_puts("  ");
          uart_puti((number=-456),3);   uart_puts("  ");
          uart_puti((number=456),2);    uart_puts("  ");
          uart_puti((number=12987),0);  uart_puts("\r\n");
#if UART0_AT_COMMAND_SUPPORT == 1
/**************************************************************************************************/
/* 
   "AT" COMMAND TESTING.
   NOTE! After a command is received the receiving stop! 
         Deal with the received "AT" command and then if you want re-enable "AT" receiving. 
*/  
          uart_command(UART_ECHO_ON);             /* optional you can have no echo if you like... */
          uart_puts_P("AT testing... (20 char max)\n\r");                      
/* enable AT command receiving (turns RX_ON automatically and flushes the rx buffer) */
          uart_command(UART_GET_AT_CMD);          
/* test to see if somethig was received. if not wait. (you dont need to wait in your programs) */          
          while( !uart_command(UART_AT_CMD_RECEIVED) ); 

#if UART0_ERROR_CHECK_SUPPORT == 1
          if(uart_command(UART_GET_ERROR))
           {
               uart_puts_P("\r\n");
               uart_puts_P("UART ERROR : ");
               uart_puti(uart_command(UART_GET_ERROR),0);
           }
#endif

          uart_get_buffer(rx_buffer_contents);
          uart_puts_P("\r\n");
          uart_puts_P("You Typed :  "); 
          uart_puts(rx_buffer_contents);  uart_puts("\r\n");
#endif
/**************************************************************************************************/

/* RAW DATA  TESTING  if enabled */ 
#if UART0_RAW_DATA_SUPPORT == 1
          
          uart_puts_P("uart RAW DATA testing...\n\r"); 
          uart_puts_P("Type at least 10 chars...\n\r"); 

          /* enable RAW data receiving (turns RX_ON automatically and flushes the rx buffer) */
          uart_command(UART_GET_RAW_DATA);  

          /* If 10 chars were received print them else wait */
          while( uart_command(UART_GET_RX_COUNT) < 10 );   

          /* Error checking if enabled */         
#if UART0_ERROR_CHECK_SUPPORT == 1
          if(uart_command(UART_GET_ERROR))
           {
               uart_puts_P("\r\n");
               uart_puts_P("UART ERROR : ");
               uart_puti(uart_command(UART_GET_ERROR),0);
           }
#endif
          /* Get the first 5 chars */
          uart_get_buf(rx_buffer_contents, 5);
          uart_puts_P("\r\n");
          uart_puts_P("You Typed :  "); 
          uart_puts(rx_buffer_contents);  uart_puts("\r\n");

          /* Wait untill the buffer gets filled with at least 10 chars again */ 
          uart_puts_P("Type some more ");
          uart_puts_P("\r\n");
          while( uart_command(UART_GET_RX_COUNT) < 10 ); 

          /* Error checking if enabled */ 
#if UART0_ERROR_CHECK_SUPPORT == 1
          if(uart_command(UART_GET_ERROR))
           {
               uart_puts_P("\r\n");
               uart_puts_P("UART ERROR : ");
               uart_puti(uart_command(UART_GET_ERROR),0);
           }
#endif

          uart_puts_P("\r\n");
          uart_puts_P("Here are the leftovers  ");
          uart_get_buffer(rx_buffer_contents);
          uart_puts(rx_buffer_contents);  uart_puts("\r\n");
#endif            

/**************************************************************************************************/
/* OTHER UART TESTS */

          /* receive a string test */
          uart_puts_P("uart_gets testing...\n\r");
          uart_command(UART_GET_STRING); 
          while( !uart_command(UART_STRING_RECEIVED) );

          /* Error checking if enabled */ 
#if UART0_ERROR_CHECK_SUPPORT == 1
          if(uart_command(UART_GET_ERROR))
           {
               uart_puts_P("\r\n");
               uart_puts_P("UART ERROR : ");
               uart_puti(uart_command(UART_GET_ERROR),0);
           }
#endif

          uart_puts("\r\n");
          uart_puts_P("You Typed :  "); 
          uart_get_buffer(rx_buffer_contents);
          uart_puts(rx_buffer_contents);  uart_puts("\r\n");

          /* using polled mode with uart_putc() */         
          uart_command(UART_ECHO_OFF);
          number=20;
          uart_puts("Type some more...\n\r");
          while(number) {in_char=uart_getc(); uart_putc(in_char); number--; }
          uart_putc('\n'); uart_putc('\r');
/* Prompt user for a repetition of the whole loop */                                                        
          uart_puts_P("Here we go again...\n\r");
          uart_puts_P("PRESS ENTER TO CONTINUE");
          while(uart_getc()!='\r'); uart_puts("\r\n");
          uart_command(UART_RX_OFF | UART_TX_OFF);              
        }
               
}
Esempio n. 20
0
//------------------------------------------------------------------------------
//		MAIN
//------------------------------------------------------------------------------
int main(void) {
	DDRC = 0x0F;
	DDRD = 0xF0;
	DDRB = 0x03 + (1 << 2) + (1 << 3) + (1 << 4);
	PORTD |= (1 << 2); // f�r Pullup
	SFIOR &= (1 << PUD);

	// Timer 2 mit externem 32kHz Quarz betreiben
	ASSR |= (1 << AS2);
	TCCR2 = (1 << CS22) + (1 << CS20); // /128 f�r 1Hz Int

	// Timer 1 f�r LED INT mit ca. 1,2kHz 		
	TCCR1A = 0;
	TCCR1B = (1 << WGM12) + (1 << CS10);
	OCR1A = 6667;

	// Timer Interrupts

	TIMSK = (1 << OCIE1A) + (1 << TOIE2); // set interrupt mask

	uart_init();
	uart_puts_p(
			"\r\nWordClock V1.0.0, Dez.2009 Martin Steppuhn (www.emsystech.de)\r\n");

	hour = 0;
	minute = 0;
	second = 0;

	sei();
	// Interrupt ein

	while (1) {
		if (sec_flag) //=== 1 Sekunde ===
		{
			sec_flag = false;
			if (BUTTON) {
				time_setup_cnt1++;
				if (time_setup_cnt1 > 3) {
					switch (e_SetupState) {
					case Idle:
						e_SetupState = StartHour;
						break;
					case InputHour:
						e_SetupState = StartMinute;
						break;
					case InputMinute:
						e_SetupState = Idle;
						break;
					case StartHour:
					case StartMinute:
						break;
					}
				}
			} else {
				time_setup_cnt0++;
				if (time_setup_cnt0 > 5) {
					e_SetupState = Idle;
				}
			}
			draw_time();
		}

		if (key_flag) //=== 10ms ===
		{
			key_flag = false;
			if (!BUTTON && button_mem) // falling Edge
					{
				button_mem = false;

				switch (e_SetupState) {
				case Idle:
					break;
				case StartHour:
					e_SetupState = InputHour;
					break;
				case StartMinute:
					e_SetupState = InputMinute;
					break;
				case InputHour:
					hour = (hour < 11) ? hour + 1 : 0;
					break;
				case InputMinute:
					minute = (minute < 59) ? minute + 1 : 0;
					break;
				}

			}
			if (BUTTON) {
				button_mem = true;
				time_setup_cnt0 = 0;
			} else {
				time_setup_cnt1 = 0;
			}
			draw_time();
		}

		// if(uart_kbhit())	{	c = uart_getc();	uart_putc(c);	}
	}
}
Esempio n. 21
0
int  main(void)
{
	//uart_set_FrameFormat(USART_8BIT_DATA|USART_1STOP_BIT|USART_NO_PARITY|USART_ASYNC_MODE); // default settings
	uart_init(BAUD_CALC(115200)); // 8n1 transmission is set as default
	
	stdout = &uart0_io; // attach uart stream to stdout & stdin
	stdin = &uart0_io; // uart0_in if no TX and uart0_out if no RX (depends on config macros)
	
	sei(); // enable interrupts, library wouldn't work without this
		
	uart_puts("hello from usart 0\r\n"); // write const string to usart buffer // C++ restriction, in C its the same as uart_putstr()
	//uart_puts("hello world\r\n"); // write const string to usart buffer // C++ restriction, in C its the same as uart_putstr()
	// if you do not have enough SRAM memory space to keep all strings, try to use puts_P instead
	uart_puts_P("hello from flashed, usart\r\n"); // write string to usart buffer from flash memory // string is parsed by PSTR() macro
	
		_delay_ms(5000);
	
	
	uart_puts_p(foo_string);
	
		
	char buffer[25];
	uart_gets(buffer, 25); // read at most 24 bytes from buffer (CR,LF will not be cut)
	
	
	int a;
	
	uart_puts("gimmie a number: ");
	a = uart_getint();
	
	
	uart_puts("numba a: ");
	uart_putint(a);
	uart_puts("\r\n");
	
	
	while(1)
	{
		uart_puts("bytes waiting in receiver buffer : ");
		uart_putint(uart_AvailableBytes()); // ask for bytes waiting in receiver buffer
		
		
		//uart_getsln(buffer, 25); // read 24 bytes or one line from usart buffer 
		
		
		if (!strcmp(buffer, "people who annoy you"))
		{
			uart_putc('>');
			_delay_ms(5000);
			uart_puts_P(" naggers");
		}
		
		
		uart_puts("\r\n");
		
		/*
		uart_putfloat(0.1337f);
		
		
		uart_puts("\r\n");
		uart_putstr(buffer); // write array string to usart buffer 
		uart_puts("\r\n");
		
		
		printf("Say my name: ");
		scanf("%s", buffer);
		
		printf("So it's %s, You are damn' right.\n", buffer);
		
		_delay_ms(5000);
		
		*/	
		
	}
	
}
int main(void)
{
	init_uart();																			// UART initalisieren und einschalten. Alle n�tigen Schnittstellen-Parameter und -Funktionen werden in rs232.h definiert
	i2c_init();																				// init I2C interface
	sei();
	uart_puts_p(FlashString);																// Demonstriert "rs232.c/uarts_put_p" f�r die Ausgabe eines Strings

	while(1)																				// Main- Endlosschleife
    {
		if (UART_MSG_FLAG==1)																// UART_MSG_FLAG auswerten: gesetzt in Empfangs- Interruptroutine wenn "CR" empfangen oder UART- Puffer voll
		{
			// (1) Daten an RS232 zur�ck
			uart_puts ("Folgende Daten an RS232 empfangen: ");
			uint8_t i;																		// Hilfsvariable f�r For-Next-Schleifenz�hler
			for (i=0;i<UART_RxCount;i++) {uart_putc(UART_RXBuffer[i]);}						// Empfangene Daten aus dem Puffer wieder �ber UART senden
			uart_puts(CR);
			// (2) Daten an I2C- Slave senden im MT-Mode
			if(!(i2c_start(SLAVE_ADRESSE+I2C_WRITE))) //Slave bereit zum schreiben?
				{
					uart_puts ("Daten: ");
					i2c_write(0x00);  // Buffer Startadresse setzen

					for (i=0;i<UART_RxCount;i++)											// Empfangene Daten in Array schreiben
						{
							i2c_write(UART_RXBuffer[i]);									// Bytes schreiben...
							uart_putc(UART_RXBuffer[i]);
						}
					i2c_stop();       // Zugriff beenden
					uart_puts (" an Slave gesendet."CR);
				}
			else
				{
					/* Hier k�nnte eine Fehlermeldung ausgegeben werden... */
					uart_puts("Fehler beim senden der Daten an Slave"CR);
				}
			// (3) Daten von I2C- Slave lesen im MR-Mode
			if(!(i2c_start(SLAVE_ADRESSE+I2C_WRITE))) //Slave bereit zum lesen?
				{
					uart_puts("Daten von Slave lesen: ");
					i2c_write(0x00); //Buffer Startadresse zum Auslesen
					i2c_rep_start(SLAVE_ADRESSE+I2C_READ); //Lesen beginnen

					for (i=0;i<UART_RxCount;i++)											// Empfangene Daten in Array schreiben
						{
							if ((i) < UART_RxCount-1)
							{
								TWI_RXBuffer[i]=i2c_readAck();									// Bytes schreiben...
								uart_putc(TWI_RXBuffer[i]);
							}
							else
							{
								TWI_RXBuffer[i]=i2c_readNak();									// letztes Bytes schreiben...
								uart_putc(TWI_RXBuffer[i]);
							}

						}

					i2c_stop();																// Zugriff beenden
					uart_puts(CR);
				}
			else
				{
					/* Hier k�nnte eine Fehlermeldung ausgegeben werden... */
					uart_puts("Fehler beim lesen der Daten an Slave"CR);
				}

			UART_RxCount=0;
			UART_MSG_FLAG=0;
		}

	}
}
Esempio n. 23
0
uint8_t init(void)
{
	uint8_t result;
	
	uart_init();
#if DEBUG
	uart_puts_p(PSTR("INIT Started! \n\n"));
	uart_puts_p(PSTR("uart_init() Completed! \n"));
#endif	
		
	LED_init();
#if DEBUG	
	uart_puts_p(PSTR("LED_init() Completed! \n"));
#endif

	key_init();
#if DEBUG	
	uart_puts_p(PSTR("key_init() Completed! \n"));
#endif
		
	LCD_Init();
#if DEBUG	
	uart_puts_p(PSTR("LCD_Init() Completed! \n"));
#endif	
	set_screen(BOOT_SCN);
	_delay_ms(100);

	set_screen(BOOT_SCN);
	_delay_ms(100);
	set_screen(BOOT_SCN);
	_delay_ms(100);
	set_screen(BOOT_SCN);
	_delay_ms(100);

	gps_init();
#if DEBUG	
	uart_puts_p(PSTR("gps_init() Completed! \n"));
#endif	
	set_screen(BOOT_SCN);
	_delay_ms(100);
	
	timer0_init();
#if DEBUG	
	uart_puts_p(PSTR("timer0_Init() Completed! \n"));
#endif	
	set_screen(BOOT_SCN);
	_delay_ms(100);

	timer1_init();
#if DEBUG	
	uart_puts_p(PSTR("timer1_Init() Completed! \n"));
#endif
	set_screen(BOOT_SCN);
	_delay_ms(100);
	
	result = sdreader_init();
	if(result)
	{
#if DEBUG		
		uart_puts_p(PSTR("ERROR : sdreader_init()"));
		uart_putc_hex(result);
		uart_puts_p(PSTR("\n\r"));
#endif	
		return result;
	}
#if DEBUG	
	else
		uart_puts_p(PSTR("sdreader_init() copmleted! \n\r"));
#endif
	set_screen(BOOT_SCN);
	_delay_ms(100);
	
	sei();
	set_screen(BOOT_SCN);
	_delay_ms(500);
	
	return 0;
}
Esempio n. 24
0
void ieee_mainloop(void) {
  int16_t cmd = 0;

  set_error(ERROR_DOSVERSION);

  ieee_data.bus_state = BUS_IDLE;
  ieee_data.device_state = DEVICE_IDLE;
  for(;;) {
    switch(ieee_data.bus_state) {
      case BUS_SLEEP:                               /* BUS_SLEEP */
        set_atn_irq(0);
        ieee_bus_idle();
        set_error(ERROR_OK);
        set_busy_led(0);
        uart_puts_P(PSTR("ieee.c/sleep ")); set_dirty_led(1);

        /* Wait until the sleep key is used again */
        while (!key_pressed(KEY_SLEEP))
          system_sleep();
        reset_key(KEY_SLEEP);

        set_atn_irq(1);
        update_leds();

        ieee_data.bus_state = BUS_IDLE;
        break;

      case BUS_IDLE:                                /* BUS_IDLE */
        ieee_bus_idle();
        while(IEEE_ATN) {   ;               /* wait for ATN */
          if (key_pressed(KEY_NEXT | KEY_PREV | KEY_HOME)) {
            change_disk();
          } else if (key_pressed(KEY_SLEEP)) {
            reset_key(KEY_SLEEP);
            ieee_data.bus_state = BUS_SLEEP;
            break;
          } else if (display_found && key_pressed(KEY_DISPLAY)) {
            display_service();
            reset_key(KEY_DISPLAY);
          }
          system_sleep();
      }

      if (ieee_data.bus_state != BUS_SLEEP)
        ieee_data.bus_state = BUS_FOUNDATN;
      break;

      case BUS_FOUNDATN:                            /* BUS_FOUNDATN */
        ieee_data.bus_state = BUS_ATNPROCESS;
        cmd = ieee_getc();
      break;

      case BUS_ATNPROCESS:                          /* BUS_ATNPROCESS */
        if(cmd < 0) {
          uart_putc('c');
          ieee_data.bus_state = BUS_IDLE;
          break;
        } else cmd &= 0xFF;
        uart_puts_p("ATN "); uart_puthex(cmd);
        uart_putcrlf();

        if (cmd == 0x3f) {                                  /* UNLISTEN */
          if(ieee_data.device_state == DEVICE_LISTEN) {
            ieee_data.device_state = DEVICE_IDLE;
            uart_puts_p("UNLISTEN\r\n");
          }
          ieee_data.bus_state = BUS_IDLE;
          break;
        } else if (cmd == 0x5f) {                           /* UNTALK */
          if(ieee_data.device_state == DEVICE_TALK) {
            ieee_data.device_state = DEVICE_IDLE;
            uart_puts_p("UNTALK\r\n");
          }
          ieee_data.bus_state = BUS_IDLE;
          break;
        } else if (cmd == (0x40 + device_address)) {        /* TALK */
          uart_puts_p("TALK ");
          uart_puthex(device_address); uart_putcrlf();
          ieee_data.device_state = DEVICE_TALK;
          /* disk drives never talk immediatly after TALK, so stay idle
             and wait for a secondary address given by 0x60-0x6f DATA */
          ieee_data.bus_state = BUS_IDLE;
          break;
        } else if (cmd == (0x20 + device_address)) {        /* LISTEN */
          ieee_data.device_state = DEVICE_LISTEN;
          uart_puts_p("LISTEN ");
          uart_puthex(device_address); uart_putcrlf();
          ieee_data.bus_state = BUS_IDLE;
          break;
        } else if ((cmd & 0xf0) == 0x60) {                  /* DATA */
          /* 8250LP sends data while ATN is still active, so wait
             for bus controller to release ATN or we will misinterpret
             data as a command */
          while(!IEEE_ATN);
          if(ieee_data.device_state == DEVICE_LISTEN) {
            cmd = ieee_listen_handler(cmd);
            cmd_handler();
            break;
          } else if (ieee_data.device_state == DEVICE_TALK) {
            ieee_data.secondary_address = cmd & 0x0f;
            uart_puts_p("DATA T ");
            uart_puthex(ieee_data.secondary_address);
            uart_putcrlf();
            if(ieee_talk_handler() == TIMEOUT_ABORT) {
              ieee_data.device_state = DEVICE_IDLE;
            }
            ieee_data.bus_state = BUS_IDLE;
            break;
          } else {
            ieee_data.bus_state = BUS_IDLE;
            break;
          }
        } else if (ieee_data.device_state == DEVICE_IDLE) {
          ieee_data.bus_state = BUS_IDLE;
          break;
          /* ----- if we reach this, we're LISTENer or TALKer ----- */
        } else if ((cmd & 0xf0) == 0xe0) {                  /* CLOSE */
          ieee_data.secondary_address = cmd & 0x0f;
          uart_puts_p("CLOSE ");
          uart_puthex(ieee_data.secondary_address);
          uart_putcrlf();
          /* Close all buffers if sec. 15 is closed */
          if(ieee_data.secondary_address == 15) {
            free_multiple_buffers(FMB_USER_CLEAN);
          } else {
            /* Close a single buffer */
            buffer_t *buf;
            buf = find_buffer (ieee_data.secondary_address);
            if (buf != NULL) {
              buf->cleanup(buf);
              free_buffer(buf);
            }
          }
          ieee_data.bus_state = BUS_IDLE;
          break;
        } else if ((cmd & 0xf0) == 0xf0) {                  /* OPEN */
          cmd = ieee_listen_handler(cmd);
          cmd_handler();
          break;
        } else {
          /* Command for other device or unknown command */
          ieee_data.bus_state = BUS_IDLE;
        }
      break;
    }   /* switch   */
  }     /* for()    */
}
Esempio n. 25
0
int8_t cli_interact(cli_processor *process, void *ptr)
{
	uint16_t ch;

	// check if LED1 is ON long enough (20 ms in this case)
	if (led) {
		uint16_t span = mill16();
		if ((span - led) > 20) {
			mmr_led_off();
			led = 0;
		}
	}

	if ((ch = serial_getc()) == 0)
		return 0;
	// light up on board LED1 indicating serial data 
	mmr_led_on();
	led = mill16();
	if (!led)
		led = 1;

	if (ch & EXTRA_KEY) {
		if (ch == ARROW_UP && (cursor == 0)) {
			// execute last successful command
			for(cursor = 0; ; cursor++) {
				cmd[cursor] = hist[cursor];
				if (cmd[cursor] == '\0')
					break;
			}
			uart_puts(cmd);
		}
		return 1;
	}

	if (ch == '\n') {
		serial_putc(ch);
		if (*cmd) {
			int8_t ret = process(cmd, ptr);
			memcpy(hist, cmd, sizeof(cmd));
			if (ret == CLI_EARG)
				uart_puts_p(PSTR("Invalid argument\n"));
			else if (ret == CLI_ENOTSUP)
				uart_puts_p(PSTR("Unknown command\n"));
			else if (ret == CLI_ENODEV)
				uart_puts_p(PSTR("Device error\n"));
		}
		for(uint8_t i = 0; i < cursor; i++)
			cmd[i] = '\0';
		cursor = 0;
		serial_putc('>');
		serial_putc(' ');
		return 1;
	}

	// backspace processing
	if (ch == '\b') {
		if (cursor) {
			cursor--;
			cmd[cursor] = '\0';
			serial_putc('\b');
			serial_putc(' ');
			serial_putc('\b');
		}
	}

	// skip control or damaged bytes
	if (ch < ' ')
		return 0;

	// echo
	serial_putc((uint8_t)ch);

	cmd[cursor++] = (uint8_t)ch;
	cursor &= CMD_LEN;
	// clean up in case of overflow (command too long)
	if (!cursor) {
		for(uint8_t i = 0; i <= CMD_LEN; i++)
			cmd[i] = '\0';
	}

	return 1;
}