Esempio n. 1
0
File: prog.c Progetto: gke/UAVP
void ReadParametersEE(void)
{
	int8 *p, c; 
	uint16 addr;

	if( IK5 > _Neutral )
		addr = _EESet2;	
	else
		addr = _EESet1;
	
	for(p = &FirstProgReg; p <= &LastProgReg; p++)
		*p = ReadEE(addr++);

	BatteryVolts = LowVoltThres;

	// Sanity check
	//if timing value is lower than 1, set it to 10ms!
	// Note TimeSlot is re-read from EEPROM each cycle
	
	if( TimeSlot < 2 )
		TimeSlot = 2;
	else
	if ( TimeSlot > 20 )
		TimeSlot = 20;
} // ReadParametersEE
Esempio n. 2
0
File: prog.c Progetto: gke/UAVP
void WriteEE(uint8 addr, int8 d)
{
	int8 rd;
	uint8 IntsWereEnabled;
	
	rd = ReadEE(addr);
	if ( rd != d )						// avoid redundant writes
	{
		EEDATA = d;				
		EEADR = addr;
		EECON1bits.EEPGD = false;
		EECON1bits.WREN = true;
		
		IntsWereEnabled = InterruptsEnabled;
		DisableInterrupts;
		EECON2 = 0x55;
		EECON2 = 0xaa;
		EECON1bits.WR = true;
		while(EECON1bits.WR);
		if ( IntsWereEnabled )
			EnableInterrupts;

		EECON1bits.WREN = false;
	}

} // WriteEE
Esempio n. 3
0
/********************************************************************
 *	メモリー読み出し
 ********************************************************************
 */
void cmd_peek(void)
{
	uchar i,size,area;
	uchar *p;

 	size = PacketFromPC.size;
	area = size & AREA_MASK;
	size = size & SIZE_MASK;
 	p = (uchar*)PacketFromPC.adrs;

	if(area & AREA_PGMEM) {
//		PacketFromPC.data[0]=0;	// TBLPTRUをゼロクリア.
		TBLPTR = (unsigned short long)PacketFromPC.adrs;

		for(i=0;i<size;i++) {
				_asm
				tblrdpostinc
				_endasm
			PacketToPC.raw[i]=TABLAT;
		}
#if	0
	}else if(area & AREA_EEPROM) {
		unsigned char ee_adr = (unsigned char)(PacketFromPC.adrs & 0xff);

		for(i=0;i<size;i++) {
			PacketToPC.raw[i] = ReadEE(ee_adr++);
		}
#endif
	}else{
		for(i=0;i<size;i++) {
			PacketToPC.raw[i]=*p++;
		}
	}
	ToPcRdy = 1;
}
Esempio n. 4
0
//=============================================================================
//  routine to restore setup data 
// 
//=============================================================================
int restore_setup( void )
{
	int size = sizeof(pid);
	int *dptr = (int *)&pid;
	int res = 0;
	int offset = 0;

	// this routine attempts to read the entire calibration structure
	// into the ram on board.
	// read 16 words of structure at a time
	while (size > 0)
	{
		res = ReadEE(__builtin_tblpage(&pidEE),
					 __builtin_tbloffset(&pidEE)+offset,
					dptr, ROW);
		if (res)
			printf("%d read from eeprom failed at offset %d\r\n",
					res,offset);

		offset += ROW*2;		// bump offset to destination 32 bytes up 
		dptr   += ROW;			// bump source ptr up 16 words
		size   -= ROW*2;	    // 16 words or 32 bytes/write
	}
	return res;
}
Esempio n. 5
0
void set_serial_number()
{
	unsigned char ch;
	int i, j;
	if (ReadEE(EE_SERIAL_LEN) == 4) {
		/* データの妥当性を検証 */
		j = 0;
		for (i = EE_SERIAL_TOP; i < EE_SERIAL_TOP + 4; i++) {
			ch = ReadEE(i);
			if (' ' <= ch && ch <= 'z') {
				j++;
			}
		}
		/* データが妥当であれば、値をセットする */
		if (j == 4) {
			for (i = 0; i < 4; i++) {
				sd003.string[i] = ReadEE (EE_SERIAL_TOP + i);
			}
		}
	}
}
Esempio n. 6
0
void BootService(void)
{
    BlinkUSBStatus();
    if((usb_device_state < CONFIGURED_STATE)||(UCONbits.SUSPND==1)) return;
    
    if(trf_state == SENDING_RESP)
    {
        if(!mBootTxIsBusy())
        {
            BOOT_BD_OUT.Cnt = sizeof(dataPacket);
            mUSBBufferReady(BOOT_BD_OUT);
            trf_state = WAIT_FOR_CMD;
        }//end if
        return;
    }//end if
    
    if(!mBootRxIsBusy())
    {
        counter = 0;
        switch(dataPacket.CMD)
        {
            case READ_VERSION:
                ReadVersion();
                counter=0x04;
                break;

            case READ_FLASH:
            case READ_CONFIG:
                ReadProgMem();
                counter+=0x05;
                break;

            case WRITE_FLASH:
                WriteProgMem();
                counter=0x01;
                break;

            case ERASE_FLASH:
                EraseProgMem();
                counter=0x01;
                break;

            case READ_EEDATA:
                ReadEE();
                counter+=0x05;
                break;

            case WRITE_EEDATA:
                WriteEE();
                counter=0x01;
                break;

            case WRITE_CONFIG:
                WriteConfig();
                counter=0x01;
                break;
            
            case RESET:
                //When resetting, make sure to drop the device off the bus
                //for a period of time. Helps when the device is suspended.
                UCONbits.USBEN = 0;
                big_counter = 0;
                while(--big_counter);
                
                Reset();
                break;
            
            case UPDATE_LED:
                if(dataPacket.led_num == 3)
                {
                    mLED_3 = dataPacket.led_status;
                    counter = 0x01;
                }//end if
                if(dataPacket.led_num == 4)
                {
                    mLED_4 = dataPacket.led_status;
                    counter = 0x01;
                }//end if
                break;
                
            default:
                break;
        }//end switch()
        trf_state = SENDING_RESP;
        if(counter != 0)
        {
            BOOT_BD_IN.Cnt = counter;
            mUSBBufferReady(BOOT_BD_IN);
        }//end if
    }//end if
}//end BootService
Esempio n. 7
0
void run_CameraTurn() {
	int cam_mode,cam_width,cam_heigth,cam_zoom,cam_size;
	int i;
	unsigned char *buf_ptr, pixel, lightest;
	unsigned int left, right, lightPos;

	#include "DataEEPROM.h"
	/*read HW version from the eeprom (last word)*/
	int HWversion=0xFFFF;
	int temp = 0;
	temp = ReadEE(0x7F,0xFFFE,&HWversion, 1);
	temp = temp & 0x03;	// get the camera rotation from the HWversion byte

	/*Cam default parameter*/
	cam_mode=GREY_SCALE_MODE;
	if ((temp==3)||(temp==0)) { // 0' and 180' camera rotation
		cam_width=1;
		cam_heigth=60;
	} else {
		cam_width=60;
		cam_heigth=1;
	}
	cam_zoom=8;
	cam_size=cam_width*cam_heigth;
	e_poxxxx_init_cam();
	e_poxxxx_config_cam((ARRAY_WIDTH -cam_width*cam_zoom)/2,(ARRAY_HEIGHT-cam_heigth*cam_zoom)/2,cam_width*cam_zoom,cam_heigth*cam_zoom,cam_zoom,cam_zoom,cam_mode);
	e_poxxxx_set_mirror(1,1);
	e_poxxxx_write_cam_registers();

	while (1) {
		e_poxxxx_launch_capture(&buffer[0]);	// start camera capture
		e_led_clear();
		e_set_body_led(0);
		e_set_front_led(0);

		while(!e_poxxxx_is_img_ready());	// wait end of capture
		buf_ptr=(unsigned char*)&buffer[0];
		left=0; right=0; lightPos=0; lightest=0;
		for (i=0; i<30; i++) {	//left
			pixel=*buf_ptr;
			buf_ptr++;
			left+=pixel;
			if (pixel>lightest) {
				lightest=pixel;
				lightPos=i;
			}
		}
		for (; i<cam_heigth; i++) {	//right
			pixel=*buf_ptr;
			buf_ptr++;
			right+=pixel;
			if (pixel>lightest) {
				lightest=pixel;
				lightPos=i;
			}
		}
		if (lightPos<20) {	//led on at lightest position
			e_set_led(7,1); }
		else if (lightPos<40) {
			e_set_led(0,1); }
		else {
			e_set_led(1,1); }

		if ((temp==3)||(temp==2)) { // 0' and 90' camera rotation
			e_set_speed_left(10*(lightPos-30));  // motor speed in steps/s
			e_set_speed_right(-10*(lightPos-30));
		} else {
			e_set_speed_left(-10*(lightPos-30));  // motor speed in steps/s
			e_set_speed_right(10*(lightPos-30));
		}

		sprintf(buffer, "left %u, right %u, lightest %u, lightPos %u\r\n", left, right, lightest, lightPos);
		e_send_uart1_char(buffer, strlen(buffer));
		wait(5000);
	}								
}
Esempio n. 8
0
int run_asercom(void) {
	static char c1,c2,wait_cam=0;
	static int	i,j,n,speedr,speedl,positionr,positionl,LED_nbr,LED_action,accx,accy,accz,sound;
	static int cam_mode,cam_width,cam_heigth,cam_zoom,cam_size,cam_x1,cam_y1;
	static char first=0;
	char *ptr;
	static int mod, reg, val;
#ifdef IR_RECEIVER
	char ir_move = 0,ir_address= 0, ir_last_move = 0;
#endif
	static TypeAccSpheric accelero;
	//static TypeAccRaw accelero_raw;
	int use_bt=0;
	//e_init_port();    // configure port pins
	//e_start_agendas_processing();
	e_init_motors();
	//e_init_uart1();   // initialize UART to 115200 Kbaud
	//e_init_ad_scan();

	selector = getselector(); //SELECTOR0 + 2*SELECTOR1 + 4*SELECTOR2 + 8*SELECTOR3;
	if(selector==10) {
		use_bt=0;
	} else {
		use_bt=1;
	}

#ifdef FLOOR_SENSORS
	if(use_bt) {	// the I2C must remain disabled when using the gumstix extension
		e_i2cp_init();
	}
#endif

#ifdef IR_RECEIVER
	e_init_remote_control();
#endif
	if(RCONbits.POR) {	// reset if power on (some problem for few robots)
		RCONbits.POR=0;
		RESET();
	}
	/*read HW version from the eeprom (last word)*/
	static int HWversion=0xFFFF;
	ReadEE(0x7F,0xFFFE,&HWversion, 1);

	/*Cam default parameter*/
	cam_mode=RGB_565_MODE;
	cam_width=40; // DEFAULT_WIDTH;
	cam_heigth=40; // DEFAULT_HEIGHT;
	cam_zoom=8;
	cam_size=cam_width*cam_heigth*2;

	if(use_bt) {
		e_poxxxx_init_cam();
		//e_po6030k_set_sketch_mode(E_PO6030K_SKETCH_COLOR);
		e_poxxxx_config_cam((ARRAY_WIDTH -cam_width*cam_zoom)/2,(ARRAY_HEIGHT-cam_heigth*cam_zoom)/2,cam_width*cam_zoom,cam_heigth*cam_zoom,cam_zoom,cam_zoom,cam_mode);
		e_poxxxx_set_mirror(1,1);
		e_poxxxx_write_cam_registers();
	}
	
	e_acc_calibr();
	
	if(use_bt) {
	uart1_send_static_text("\f\a"
			"WELCOME to the SerCom protocol on e-Puck\r\n"
			"the EPFL education robot type \"H\" for help\r\n");
	} else {
	uart2_send_static_text("\f\a"
			"WELCOME to the SerCom protocol on e-Puck\r\n"
			"the EPFL education robot type \"H\" for help\r\n");
	}


	while(1) {
		if(use_bt) {
			while (e_getchar_uart1(&c)==0)
			#ifdef IR_RECEIVER
					{
						ir_move = e_get_data();
						ir_address = e_get_address();
						if (((ir_address ==  0)||(ir_address ==  8))&&(ir_move!=ir_last_move)){
							switch(ir_move) {
								case 1:
									speedr = SPEED_IR;
									speedl = SPEED_IR/2;
									break;
								case 2:
									speedr = SPEED_IR;
									speedl = SPEED_IR;
									break;
								case 3:
									speedr = SPEED_IR/2;
									speedl = SPEED_IR;
									break;
								case 4:
									speedr = SPEED_IR;
									speedl = -SPEED_IR;
									break;
								case 5:
									speedr = 0;
									speedl = 0;
									break;
								case 6:
									speedr = -SPEED_IR;
									speedl = SPEED_IR;
									break;
								case 7:
									speedr = -SPEED_IR;
									speedl = -SPEED_IR/2;
									break;
								case 8:
									speedr = -SPEED_IR;
									speedl = -SPEED_IR;
									break;
								case 9:
									speedr = -SPEED_IR/2;
									speedl = -SPEED_IR;
									break;
								case 0:
									if(first==0){
										e_init_sound();
										first=1;
									}
									e_play_sound(11028,8016);
									break;
								default:
									speedr = speedl = 0;
							}
							ir_last_move = ir_move;
							e_set_speed_left(speedl);
							e_set_speed_right(speedr);
							}
					}
			#else 
					;
			#endif
		} else {
			while (e_getchar_uart2(&c)==0)
			#ifdef IR_RECEIVER
					{
						ir_move = e_get_data();
						ir_address = e_get_address();
						if (((ir_address ==  0)||(ir_address ==  8))&&(ir_move!=ir_last_move)){
							switch(ir_move) {
								case 1:
									speedr = SPEED_IR;
									speedl = SPEED_IR/2;
									break;
								case 2:
									speedr = SPEED_IR;
									speedl = SPEED_IR;
									break;
								case 3:
									speedr = SPEED_IR/2;
									speedl = SPEED_IR;
									break;
								case 4:
									speedr = SPEED_IR;
									speedl = -SPEED_IR;
									break;
								case 5:
									speedr = 0;
									speedl = 0;
									break;
								case 6:
									speedr = -SPEED_IR;
									speedl = SPEED_IR;
									break;
								case 7:
									speedr = -SPEED_IR;
									speedl = -SPEED_IR/2;
									break;
								case 8:
									speedr = -SPEED_IR;
									speedl = -SPEED_IR;
									break;
								case 9:
									speedr = -SPEED_IR/2;
									speedl = -SPEED_IR;
									break;
								case 0:
									if(first==0){
										e_init_sound();
										first=1;
									}
									e_play_sound(11028,8016);
									break;
								default:
									speedr = speedl = 0;
							}
							ir_last_move = ir_move;
							e_set_speed_left(speedl);
							e_set_speed_right(speedr);
							}
					}
			#else 
					;
			#endif
		}

		if (c<0) { // binary mode (big endian)
			i=0;
			do {
				switch(-c) { 
        		case 'a': // Read acceleration sensors in a non
                  // filtered way, some as ASCII
          			accx = e_get_acc_filtered(0, 1); 
          			accy = e_get_acc_filtered(1, 1); 
          			accz = e_get_acc_filtered(2, 1); 
				
				//accx = e_get_acc(0);	//too much noisy
				//accy = e_get_acc(1);
				//accz = e_get_acc(2);

				buffer[i++] = accx & 0xff;
          			buffer[i++] = accx >> 8;
          			buffer[i++] = accy & 0xff;
          			buffer[i++] = accy >> 8;
          			buffer[i++] = accz & 0xff;
          			buffer[i++] = accz >> 8;
				
				/*
          			accelero_raw=e_read_acc_xyz();
				ptr=(char *)&accelero_raw.acc_x;
				buffer[i++]=(*ptr);
				ptr++;
				buffer[i++]=(*ptr);
				ptr++;

				ptr=(char *)&accelero_raw.acc_y;
				buffer[i++]=(*ptr);
				ptr++;
				buffer[i++]=(*ptr);
				ptr++;

				ptr=(char *)&accelero_raw.acc_z;
				buffer[i++]=(*ptr);
				ptr++;
				buffer[i++]=(*ptr);
				ptr++;
				*/
          			break;
				case 'A': // read acceleration sensors
					accelero=e_read_acc_spheric();
					ptr=(char *)&accelero.acceleration;
					buffer[i++]=(*ptr);
					ptr++;
					buffer[i++]=(*ptr);
					ptr++;
					buffer[i++]=(*ptr);
					ptr++;
					buffer[i++]=(*ptr);
				
					ptr=(char *)&accelero.orientation;
					buffer[i++]=(*ptr);
					ptr++;
					buffer[i++]=(*ptr);
					ptr++;
					buffer[i++]=(*ptr);
					ptr++;
					buffer[i++]=(*ptr);
		
					ptr=(char *)&accelero.inclination;
					buffer[i++]=(*ptr);
					ptr++;
					buffer[i++]=(*ptr);
					ptr++;
					buffer[i++]=(*ptr);
					ptr++;
					buffer[i++]=(*ptr);
				
					break;
				case 'b': // battery ok?
					buffer[i++] = BATT_LOW;
					break;
				case 'D': // set motor speed
					if(use_bt) {
						while (e_getchar_uart1(&c1)==0);
						while (e_getchar_uart1(&c2)==0);
					} else {
						while (e_getchar_uart2(&c1)==0);
						while (e_getchar_uart2(&c2)==0);
					}
					speedl=(unsigned char)c1+((unsigned int)c2<<8);
					if(use_bt) {
						while (e_getchar_uart1(&c1)==0);
						while (e_getchar_uart1(&c2)==0);
					} else {
						while (e_getchar_uart2(&c1)==0);
						while (e_getchar_uart2(&c2)==0);					
					}
					speedr=(unsigned char)c1+((unsigned  int)c2<<8);
					e_set_speed_left(speedl);
					e_set_speed_right(speedr);
					break;
        		case 'E': // get motor speed
          			buffer[i++] = speedl & 0xff;
          			buffer[i++] = speedl >> 8;
          			buffer[i++] = speedr & 0xff;
          			buffer[i++] = speedr >> 8;
          			break;
				case 'I': // get camera image
					if(use_bt) {
						e_poxxxx_launch_capture(&buffer[i+3]);
						wait_cam=1;
						buffer[i++]=(char)cam_mode&0xff;//send image parameter
						buffer[i++]=(char)cam_width&0xff;
						buffer[i++]=(char)cam_heigth&0xff;
						i+=cam_size;
					}
					break;
				case 'L': // set LED
					if(use_bt) {
						while (e_getchar_uart1(&c1)==0);
						while (e_getchar_uart1(&c2)==0);
					} else {
						while (e_getchar_uart2(&c1)==0);
						while (e_getchar_uart2(&c2)==0);
					}
					switch(c1) {
						case 8:
							if(use_bt) {
								e_set_body_led(c2);
							}
							break;
						case 9:
							if(use_bt) {
								e_set_front_led(c2);
							}
							break;
						default:
							e_set_led(c1,c2);
							break;
					}
					break;
				case 'M': // optional floor sensors
#ifdef FLOOR_SENSORS
					if(use_bt) {
	          			e_i2cp_init();
	          			e_i2cp_enable();
	          			e_i2cp_read(0xC0, 0);
	          			for(j = 0; j < 6; j++) {
	            			if (j % 2 == 0) buffer[i++] = e_i2cp_read(0xC0, j + 1);
	            			else            buffer[i++] = e_i2cp_read(0xC0, j - 1);
	          			}
#ifdef CLIFF_SENSORS
          				for(j=13; j<17; j++) {
            				if (j % 2 == 0) buffer[i++] = e_i2cp_read(0xC0, j - 1);
            				else            buffer[i++] = e_i2cp_read(0xC0, j + 1);	          				
	          			}
#endif
	          			e_i2cp_disable();
					}
#else
					for(j=0;j<6;j++) buffer[i++]=0;
#endif
					break;
				case 'N': // read proximity sensors
					if(use_bt) {
						for(j=0;j<8;j++) {
							n=e_get_calibrated_prox(j);	// or ? n=e_get_prox(j);
							buffer[i++]=n&0xff;
							buffer[i++]=n>>8;
						}
					} else {
						for(j=0;j<10;j++) {
							n=e_get_calibrated_prox(j);	// or ? n=e_get_prox(j);
							buffer[i++]=n&0xff;
							buffer[i++]=n>>8;
						}
					}
					break;
				case 'O': // read light sensors
					if(use_bt) {
						for(j=0;j<8;j++) {
	
							n=e_get_ambient_light(j);
							buffer[i++]=n&0xff;
							buffer[i++]=n>>8;
						}
					} else {
						for(j=0;j<10;j++) {
							n=e_get_ambient_light(j);
							buffer[i++]=n&0xff;
							buffer[i++]=n>>8;
						}
					}
					break;
				case 'Q': // read encoders
                    n=e_get_steps_left();
					buffer[i++]=n&0xff;
					buffer[i++]=n>>8;
                    n=e_get_steps_right();
					buffer[i++]=n&0xff;
					buffer[i++]=n>>8;
					break;
        		case 'u': // get last micro volumes
          			n = e_get_micro_volume(0);
          			buffer[i++] = n & 0xff;
          			buffer[i++] = n >> 8;

          			n = e_get_micro_volume(1);
          			buffer[i++] = n & 0xff;
          			buffer[i++] = n >> 8;

          			n = e_get_micro_volume(2);
          			buffer[i++] = n & 0xff;
          			buffer[i++] = n >> 8;
          			break;
				case 'U': // get micro buffer
					ptr=(char *)e_mic_scan;
					if(use_bt) {
						e_send_uart1_char(ptr,600);//send sound buffer
					} else {
						e_send_uart2_char(ptr,600);//send sound buffer
					}
					n=e_last_mic_scan_id;//send last scan
					buffer[i++]=n&0xff;
					break;
				default: // silently ignored
					break;
				}
				if(use_bt) {
					while (e_getchar_uart1(&c)==0); // get next command
				} else {
					while (e_getchar_uart2(&c)==0); // get next command
				}
			} while(c);