Sunday, November 6, 2011

PC application - Some code to start

For those who want to develop a PC application to configure the gyro.
Remember: This is only possible with the bootloader running!

/* Configuration */
typedef struct _config_
{
uint8_t version;
uint8_t mark[3];
uint16_t servo_freq; //not used
uint16_t hi_endpoint; // [1600..2100]
uint16_t low_endpoint; //[900..1400]
uint16_t midpoint; //not used
uint8_t reverse; // [0..1]
uint8_t rudder_deadband;// [1..100]
uint8_t rudder_speed; // [1..8]
uint8_t delay_cw; // [1..8]
uint8_t delay_ccw; // [1..8]
uint8_t rudder_delay; // [1..8]
uint8_t servo_speed; // [1..140] ms
uint8_t kP; // [1..128]
uint8_t kI; // [1..128]
uint8_t kD; // [1..128]
} config_t;

int enter_gyro(void)
{
SerialWrite(0x1B);
delay_ms(500);
SerialFlush();
SerialWrite('7');
if(SerialRead(200) == '?')
return 1;
return 0;
}

int read_gyro( uint16_t address, uint8_t *buf, uint16_t size)
{
if(!enter_gyro())
return 0;
SerialFlush();
SerialWrite('P'); // Enter Prog Mode
if(SerialRead(1000) != 0x0D) return 0;
SerialWrite('A'); // Set Address
_delay_ms(1);
SerialWrite(address>>8);
_delay_ms(1);
SerialWrite(address&0xFF);
if(SerialRead(1000) != 0x0D) return 0;
while(size--)
{
SerialWrite('d'); // Read Data Memory
*buf++ = SerialRead(1000);
}
SerialWrite('L'); // Leave Prog Mode
if(SerialRead(1000) != 0x0D) return 0;
return 1;
}

int write_gyro( uint16_t address, uint8_t *buf, uint16_t size)
{
SerialFlush();
SerialWrite('P'); // Enter Prog Mode
if(SerialRead(1000) != 0x0D) return 0;
SerialWrite('A'); // Set Address
_delay_ms(1);
SerialWrite(address>>8);
_delay_ms(1);
SerialWrite(address&0xFF);
if(SerialRead(1000) != 0x0D) return 0;
while(size--)
{
SerialWrite('D'); // Write Data Memory
_delay_ms(1);
SerialWrite(*buf++);
if(SerialRead(1000) != 0x0D) return 0;
}
SerialWrite('L'); // Leave Prog Mode
if(SerialRead(1000) != 0x0D) return 0;
return 1;
}

void main(void)
{
read_gyro( 6, (uint8_t *) &gyro_config, sizeof(config_t));
................
write_gyro( 6, (uint8_t *) &gyro_config, sizeof(config_t));

}

No comments:

Post a Comment