Пример #1
0
uint8_t read_msg(uint8_t *buf)
{
  uint16_t ret;
  uint8_t *ret_data = (uint8_t*)&ret;

  for(int i=0; i<3; i++)
    {
    top:
      ret = uart0_getc();
      if(!ret_data[0]) // If no error
        buf[i] = ret_data[1];
      else // Handling of errors, TODO
        {
          switch (ret)
            {
            case UART_NO_DATA:
              sleep_mode();
              goto top;
            default: /* All other error cases*/
              send_msg(NAK, ret);
              uart0_flush(); // Flushing the receiving buffer, since
                             // we’re unable to continue anyway.
              return ret_data[0];
            }
        }
    }

  return 0;
}
Пример #2
0
void handle_error()
{
	state = StateDisconnected;
	led_off(UART0_LED_STATE);
	servo_set_position(DOCK_SERVO_ZAXIS_ID, DOCK_SERVO_POWER_OFF);
	servo_set_position(DOCK_SERVO_XAXIS_ID, DOCK_SERVO_POWER_OFF);
	uart0_flush();
}
Пример #3
0
Файл: main.c Проект: epccs/RPUno
int main(void) 
{
    // Initialize Timers, ADC, and clear bootloader, Arduino does these with init() in wiring.c 
    initTimers(); //Timer0 Fast PWM mode, Timer1 & Timer2 Phase Correct PWM mode.
    init_ADC_single_conversion(EXTERNAL_AVCC); // warning AREF should only have a bypass cap
    init_uart0_after_bootloader(); // bootloader may have the UART setup
    
    // setup()

    // Set digital pins to control load
    init_load();

    // Set digital pins to control solar
    init_pv();

    // put ADC in free running Auto Trigger mode
    enable_ADC_auto_conversion(FREE_RUNNING);
    
    
    /* Initialize UART, it returns a pointer to FILE so redirect of stdin and stdout works*/
    stdout = stdin = uartstream0_init(BAUD);
    
    /* Clear and setup the command buffer, (probably not needed at this point) */
    initCommandBuffer();

    sei(); // Enable global interrupts starts TIMER0, UART0, ADC and any other ISR's
    
    // this start up command should run cctest, e.g. after a reset.
    if (uart0_available() == 0)
    {
        strcpy_P(command_buf, PSTR("/0/cctest?"));
        command_done = 1;
        echo_on = 1;
        printf_P(PSTR("%s\r\n"), command_buf);
    }
    
    // loop() 
    while(1) /* I am tyring to use non-blocking code */
    { 
        // check if character is available to assemble a command, e.g. non-blocking
        if ( (!command_done) && uart0_available() ) // command_done is an extern from parse.h
        {
            // get a character from stdin and use it to assemble a command
            AssembleCommand(getchar());

            // address is the ascii value for '0' note: a null address will terminate the command string. 
            StartEchoWhenAddressed('0');
        }
        
        // check if a character is available, and if so flush transmit buffer and nuke the command in process.
        // A multi-drop bus can have another device start transmitting after getting an address byte so
        // the first byte is used as a warning, it is the onlly chance to detect a possible collision.
        if ( command_done && uart0_available() )
        {
            // dump the transmit buffer to limit a collision 
            uart0_flush(); 
            initCommandBuffer();
            
            //Enable the LT3652, which may have been turned off
            digitalWrite(SHUTDOWN, LOW);
            
            // trun off the load
            load_step(0);
        }
        
        // finish echo of the command line befor starting a reply (or the next part of a reply)
        if ( command_done && (uart0_availableForWrite() == UART_TX0_BUFFER_SIZE) )
        {
            if ( !echo_on  )
            { // this happons when the address did not match 
                initCommandBuffer();
            }
            else
            {
                if (command_done == 1)  
                {
                    findCommand();
                    command_done = 10;
                }
                
                // do not overfill the serial buffer since that blocks looping, e.g. process a command in 32 byte chunks
                if ( (command_done >= 10) && (command_done < 250) )
                {
                     ProcessCmd();
                }
                else 
                {
                    initCommandBuffer();
                }
            }
         }
    }        
    return 0;
}
Пример #4
0
Файл: main.c Проект: epccs/RPUno
int main(void) {    

    /* Initialize UART, it returns a pointer to FILE so redirect of stdin and stdout works*/
    stdout = stdin = uartstream0_init(BAUD);
    
    /* Initialize I2C, with the internal pull-up*/
    twi_init(1);

    /* Clear and setup the command buffer, (probably not needed at this point) */
    initCommandBuffer();
       
    sei(); // Enable global interrupts

    char rpu_addr = get_Rpu_address();
    
    // set a default address if RPU manager not found
    if (rpu_addr == 0)
    {
        rpu_addr = '0';
    }
    
    while(1) 
    {
        // check if character is available to assemble a command, e.g. non-blocking
        if ( (!command_done) && uart0_available() ) // command_done is an extern from parse.h
        {
            // get a character from stdin and use it to assemble a command
            AssembleCommand(getchar());

            // address is a char e.g. the ascii value for '0' warning: a null will terminate the command string. 
            StartEchoWhenAddressed(rpu_addr);
        }
        
        // check if the character is available, and if so stop transmit and the command in process.
        // a multi-drop bus can have another device start transmitting after the second received byte so
        // there is little time to detect a possible collision
        if ( command_done && uart0_available() )
        {
            // dump the transmit buffer to limit a collision 
            uart0_flush(); 
            initCommandBuffer();
        }
        
        // finish echo of the command line befor starting a reply (or the next part of reply)
        if ( command_done && (uart0_availableForWrite() == UART_TX0_BUFFER_SIZE) )
        {
            if ( !echo_on  )
            { // this happons when the address did not match
                initCommandBuffer();
            }
            else
            {
                // command is a pointer to string and arg[] is an array of pointers to strings
                // use findCommand to make them point to the correct places in the command line
                // this can only be done once, since spaces and delimeters are replaced with null termination
                if (command_done == 1)  
                {
                    findCommand();
                    command_done = 10;
                }
                
                if ( (command_done >= 10) && (command_done < 250) )
                {
                     ProcessCmd();
                }
                else 
                {
                    initCommandBuffer();
                }
            }
         }
    }        
    return 0;
}
Пример #5
0
void testrun_performance_reduce_bigint(void){
    printf_P(PSTR("\n=== performance measurement (reduce) ===\n"));
    unsigned i, j;
    bigint_t a,b,v;
    bigint_word_t v_w[192 * 2 / BIGINT_WORD_SIZE];
    bigint_word_t a_w[192 * 2 / BIGINT_WORD_SIZE];
    bigint_word_t b_w[192 * 2 / BIGINT_WORD_SIZE];
    uint32_t time_a, time_b;
    int32_t time_diff;
    int16_t faster_percent;
    v.wordv = v_w;
    for(j = 0; j < 32; ++j){
        do{
            for(i = 0; i < 192 * 2 / BIGINT_WORD_SIZE; ++i){
                ((uint8_t*)v_w)[i] = random();
            }
            v.length_W = 192 * 2 / BIGINT_WORD_SIZE;
            v.info = 0;
            bigint_adjust(&v);
        }while(0);

    //    printf_P(PSTR("candidate:\n"));
    //    bigint_print_hex(&v);
        a.wordv = a_w;
        b.wordv = b_w;
        calibrateTimer();

    //    printf_P(PSTR("\n  going to test optimized version: ...\n"));
        uart0_flush();
        time_a = 0;
        for(i = 0; i < 16; ++i){
            bigint_copy(&a, &v);
            startTimer(1);
            START_TIMER;
            bigint_reduce_p192(&a);
            STOP_TIMER;
            time_a += stopTimer();
        }
    //    printf_P(PSTR("    took: %"PRIu32" cycles\nresult:"), time);
    //    bigint_print_hex(&a);


    //    printf_P(PSTR("\n  going to test not-optimized version: ...\n"));
    //    uart0_flush();
        time_b = 0;
        for(i = 0; i < 16; ++i){
            bigint_copy(&b, &v);
            startTimer(1);
            START_TIMER;
            bigint_reduce(&b, &nist_curve_p192_p);
            STOP_TIMER;
            time_b += stopTimer();
        }
    //    printf_P(PSTR("    took: %"PRIu32" cycles\nresult:"), time);
    //    bigint_print_hex(&b);

        time_diff = time_b - time_a;
        faster_percent = (time_diff * 100) / time_b;

        printf_P(PSTR("  delta: %7"PRId32"  (%3"PRId16"%%)  :-"), time_diff, faster_percent);
        if(bigint_cmp_u(&a, &b)){
            printf_P(PSTR("(\n"));
        } else {
            printf_P(PSTR(")\n"));
        }
        uart0_flush();
    }
}
Пример #6
0
Файл: main.c Проект: epccs/RPUno
int main(void) {    

    /* Initialize UART, it returns a pointer to FILE so redirect of stdin and stdout works*/
    stdout = stdin = uartstream0_init(BAUD);
    initCommandBuffer();
       
    sei(); // Enable global interrupts starts the UART
    
    // non-blocking code in loop
    while(1) 
    {
        // check if character is available to assemble a command, e.g. non-blocking
        if ( (!command_done) && uart0_available() ) // command_done is an extern from parse.h
        {
            // get a character from stdin and use it to assemble a command
            AssembleCommand(getchar());

            // address is the ascii value for '0' note: a null address will terminate the command string. 
            StartEchoWhenAddressed('0');
        }
        
        // check if the character is available, and if so stop transmit and the command in process.
        // a multi-drop bus can have another device start transmitting after the second received byte so
        // there is little time to detect a possible collision
        if ( command_done && uart0_available() )
        {
            // dump the transmit buffer to limit a collision 
            uart0_flush(); 
            initCommandBuffer();
        }
        
        // finish echo of the command line befor starting a reply (or the next part of reply), also non-blocking.
        if ( command_done && (uart0_availableForWrite() == UART_TX0_BUFFER_SIZE) )
        {
            if ( !echo_on  )
            { // this happons when the address did not match
                initCommandBuffer();
            }
            else
            {
                // command is a pointer to string and arg[] is an array of pointers to strings
                // use findCommand to make them point to the correct places in the command line
                // this can only be done once, since spaces and delimeters are replaced with null termination
                if (command_done == 1)  
                {
                    findCommand();
                    command_done = 2;
                }
                if (command_done == 2)
                {
                    if (command != '\0' )
                    {
                        printf_P(PSTR("{\"cmd\": \"%s\"}\r\n"),command);
                        command_done = 3;
                    }
                    else
                    {
                        initCommandBuffer();
                    }
                }
                if (command_done == 3)
                {
                    printf_P(PSTR("{\"arg_count\": \"%d\"}\r\n"),arg_count);
                    if (arg_count > 0)
                    {
                        command_done = 4;
                    }
                    else
                    {
                        initCommandBuffer();
                    }
                }
                if (command_done == 4)
                {
                    printf_P(PSTR("{\"arg[0]\": \"%s\"}\r\n"),arg[0]);
                    if (arg_count > 1)
                    {
                        command_done = 5;
                    }
                    else
                    {
                        initCommandBuffer();
                    }
                }
                if (command_done == 5)
                {
                    printf_P(PSTR("{\"arg[1]\": \"%s\"}\r\n"),arg[1]);
                    if (arg_count > 2)
                    {
                        command_done = 6;
                    }
                    else
                    {
                        initCommandBuffer();
                    }
                }
                if (command_done == 6)
                {
                    printf_P(PSTR("{\"arg[2]\": \"%s\"}\r\n"),arg[2]);
                    if (arg_count > 3)
                    {
                        command_done = 7;
                    }
                    else
                    {
                        initCommandBuffer();
                    }
                }
                if (command_done == 7)
                {
                    printf_P(PSTR("{\"arg[3]\": \"%s\"}\r\n"),arg[3]);
                    if (arg_count > 4)
                    {
                        command_done = 8;
                    }
                    else
                    {
                        initCommandBuffer();
                    }
                }
                if (command_done == 8)
                {
                    printf_P(PSTR("{\"arg[4]\": \"%s\"}\r\n"),arg[4]);
                    initCommandBuffer();
                }
            }
         }
    }        
    return 0;
}
Пример #7
0
int main(void) 
{
    setup();

    while(1) 
    { 
        // use LED to show if I2C has a bus manager
        blink();
        
        // check if character is available to assemble a command, e.g. non-blocking
        if ( (!command_done) && uart0_available() ) // command_done is an extern from parse.h
        {
            // get a character from stdin and use it to assemble a command
            AssembleCommand(getchar());

            // address is an ascii value, warning: a null address would terminate the command string. 
            StartEchoWhenAddressed(rpu_addr);
        }
        
        // check if a character is available, and if so flush transmit buffer and nuke the command in process.
        // A multi-drop bus can have another device start transmitting after getting an address byte so
        // the first byte is used as a warning, it is the onlly chance to detect a possible collision.
        if ( command_done && uart0_available() )
        {
            // dump the transmit buffer to limit a collision 
            uart0_flush(); 
            initCommandBuffer();
        }
        
        // delay between ADC burst
        adc_burst();
          
        // finish echo of the command line befor starting a reply (or the next part of a reply)
        if ( command_done && (uart0_availableForWrite() == UART_TX0_BUFFER_SIZE) )
        {
            if ( !echo_on  )
            { // this happons when the address did not match 
                initCommandBuffer();
            }
            else
            {
                if (command_done == 1)  
                {
                    findCommand();
                    command_done = 10;
                }
                
                // do not overfill the serial buffer since that blocks looping, e.g. process a command in 32 byte chunks
                if ( (command_done >= 10) && (command_done < 250) )
                {
                     ProcessCmd();
                }
                else 
                {
                    initCommandBuffer();
                }
            }
         }
    }        
    return 0;
}
Пример #8
0
void process_messages()
{
#ifdef UART0_DISCONNECT_ON_DTR
	if ((PORT_UART0_DTR & (1 << PIN_UART0_DTR)))
	{
		if (state == StateConnected)
		{
			send_error(ErrorDisconnect, 0);
		}
		uart0_flush();
		return;
	}
#endif
	MessageParseResult res;
	while ((res = message_parse()) == ParseOk)
	{
		switch (message.identifier)
		{
			case IdAckMessage:
				handle_ack();
				break;
			case IdInitMessage:
				handle_init();
				break;
			case IdFootSwitchMessage:
				handle_footswitch();
				break;
			case IdInterlockResetMessage:
				handle_int_reset();
				break;
			case IdServoCtrlMessage:
				handle_servo_ctrl();
				break;
			case IdDockingLimitMessage:
				handle_docking_limit();
				break;
			case IdErrorMessage:
				handle_error();
				break;
			case IdDockingTareMessage:
				handle_docking_tare();
				break;
			case IdSettingsMessage:
				handle_settings();
				break;
			default:
				send_error(ErrorUnhandled, message.sequence);
				return;
		}
	}

	switch (res)
	{
		case ParseNotEnoughData: // buffer empty, done
			break;
//		case ParseInvalidIdentifier: // fall
//		case ParseInvalidSizeError: // fall
//		case ParseChecksumError: // fall
		default:
			send_error(ErrorParser, 0);
			break;
	}
}
Пример #9
0
enum pop_process_result pop_process(void) {
	enum popstate psn = ps;
	enum pop_process_result result = pp_continue;
	enum popreply preply;
	char * line = NULL;

	if (ps == pstart) {
		mail_using_modem = true;
		uart0_flush();
		wifi_connectgpio();
		// 10 seconden timeout voor het leggen van de verbinding
		ptrycounter = 10;
		psn = pconnectioninitiated;
	} else if (ps == pconnectioninitiated) {
		// verbonden?
		if (wifi_isconnectedgpio()) {
			psn = pconnected;
		} else {
			if (ptrycounter == 0) {
				psn = pfinished;
				fprintf_P(pcout, PSTR("P connection failed\r\n"));
				result = pp_connectionfailed;
			}
		}
	} else if (ps == pconnected) {
		// Verbonden!, login sturen
		if (uart0_havenewline()) {
			line = uart0_readline();
			preply = pop_read_reply(line);
			if (preply == pok) {
				printf_P(PSTR("USER %s\r\n"), currentsettings.username);
				psn = ploginsent;
			} else if (preply == perr) {
				fprintf_P(pcout, PSTR("P Connect error %s\r\n"), line);
				psn = pfinished;
			}
		}
	} else if (ps == ploginsent) {
		if(uart0_havenewline()) {
			line = uart0_readline();
			fprintf_P(pcout, PSTR("L %s\r\n"),line);
			preply = pop_read_reply(line);
			if (preply == pok) {
				fprintf_P(pcout, PSTR("P login ok\r\n"));
				psn = ploginok;
			} else if (preply == perr) {
				fprintf_P(pcout, PSTR("P error sending username %s\r\n"),line);
				psn = ploginerr;
			}
		}
	} else if (ps == ploginerr) {
		psn = pfinished;
	} else if (ps == ploginok) {
		printf_P(PSTR("PASS %s\r\n"), currentsettings.password);
		psn = ppassent;
	} else if (ps == ppassent) {
		if(uart0_havenewline()) {
			line = uart0_readline();
			fprintf_P(pcout, PSTR("L %s\r\n"),line);
			preply = pop_read_reply(line);
			if (preply == pok) {
				psn = ppassok;
			} else if (preply == perr) {
				fprintf_P(pcout, PSTR("P error sending password %s\r\n"),line);
				psn = ppasserr;
			}
		}
	} else if (ps == ppasserr) {
		psn = pfinished;
	} else if (ps == ppassok) {
		psn = ploggedin;
	} else if (ps == ploggedin) {
		printf_P(PSTR("STAT\r\n"));
		psn = pstatsent;
	} else if (ps == pstatsent) {
		if (uart0_havenewline()) {
			line = uart0_readline();
			fprintf_P(pcout, PSTR("L %s\r\n"),line);
			preply = pop_read_reply(line);
			if (preply == pok) {
				preply = pop_read_num_messages(line);
				if (pmailcount > MAXMESSAGES) {
					currentmailno = pmailcount;
					lastmsgtoreceive = pmailcount - MAXMESSAGES+1;;
					receivingpos = 0;
					psn = pquerymsgtop;
				} else if (pmailcount == 0){
					mails.fillcount = 0;
					psn = pquit;
				} else {
					currentmailno = pmailcount;
					lastmsgtoreceive = 1;
					receivingpos = 0;
					psn = pquerymsgtop;
				}
			} else if (preply == perr) {
				fprintf_P(pcout, PSTR("P error in STAT %d, %s\r\n"),preply,line);
				psn = pfinished;
			}
		}
	} else if (ps == pquerymsgtop) {
		printf_P(PSTR("TOP %lu 0\r\n"),currentmailno);
		psn = pmsgtopok;
	} else if (ps == pmsgtopok) {
		if (uart0_havenewline()) {
			line = uart0_readline();
			preply = pop_read_reply(line);
			if (preply == pok) {
				psn = preceivemsgtop;
				mails.m[receivingpos].sender[0] = '\0';
				mails.m[receivingpos].title[0] = '\0';
				mails.m[receivingpos].time[0] = '\0';
			} else if (preply == perr){
				fprintf_P(pcout, PSTR("P error in TOP %s\r\n"),line);
				psn = pfinished;
			}
		}
	} else if (ps == preceivemsgtop) {
		if (uart0_havenewline()) {
			line = uart0_readline();
			if (read_field(strFROMUCASE,line,mails.m[receivingpos].sender, MAXSENDERLENGHT)==ifound) {
				fprintf_P(pcout, PSTR("P read sender %s\r\n"),mails.m[receivingpos].sender);
			} else if (read_field(strSUBJECTUCASE,line,mails.m[receivingpos].title, MAXTITLELENGHT)==ifound) {
				fprintf_P(pcout, PSTR("P read subject %s\r\n"),mails.m[receivingpos].title);
			} else if (read_field(strDATEUCASE,line,mails.m[receivingpos].time, MAXTIMELENGHT)==ifound) {
				fprintf_P(pcout, PSTR("P read date %s\r\n"),mails.m[receivingpos].time);
			} else if (line[0] == '.') {
				// einde van de headers
				currentmailno--;
				receivingpos++;
				// klopt nog niet!!
				if (currentmailno >= lastmsgtoreceive) {
					psn = pquerymsgtop;
				} else {
					mails.fillcount=min(pmailcount,MAXMESSAGES);
					psn = pquit;
				}
			}
		}
	} else if (ps == pquit) {
		printf_P(PSTR("QUIT\r\n"));
		psn = pfinished;
	} else if (ps == pfinished) {
		wifi_disconnectgpio();
		mails.filling = false;
		mail_using_modem = false;
		result = pp_done;
		psn = pstart;
	}


	if (ps!=psn) {
		fprintf_P(pcout, PSTR("P State: %s %s\r\n"),popstateinfo[ps],popstateinfo[psn]);
		// max 30 sec in dezelfde state
		pcurrentstatecounter = 30;
	} else {
		if (pcurrentstatecounter == 0) {
			mails.filling = false;
			fprintf_P(pcout, PSTR("# fsm frozen\r\n"));
			wifi_disconnectgpio();
			result = pp_fsmfrozen;
			psn = pstart;
		}
	}
	ps = psn;
	return result;
}