diff options
author | Yves Fischer <yvesf-git@xapek.org> | 2011-10-11 21:56:36 +0200 |
---|---|---|
committer | Yves Fischer <yvesf-git@xapek.org> | 2011-10-11 21:56:36 +0200 |
commit | cbc941282074856fc6179761b70a0c9879b0b64d (patch) | |
tree | ed15c6d9e20ceaf1ff01a05aa63054410bb8ed54 /firmware/i2c.c | |
parent | 324c5ba9098c1010d0aa8c1e26b95509878ce9f2 (diff) | |
download | mini-octopus-cbc941282074856fc6179761b70a0c9879b0b64d.tar.gz mini-octopus-cbc941282074856fc6179761b70a0c9879b0b64d.zip |
update firmware to @319
Diffstat (limited to 'firmware/i2c.c')
-rw-r--r-- | firmware/i2c.c | 113 |
1 files changed, 53 insertions, 60 deletions
diff --git a/firmware/i2c.c b/firmware/i2c.c index b7d55fe..4baa7bb 100644 --- a/firmware/i2c.c +++ b/firmware/i2c.c @@ -36,9 +36,12 @@ #include "common.h" #include "protocol.h" #include "i2c.h" +#define I2C_WAIT_FOR_COMPLETE() while (!(TWCR & (1<<TWINT))); + +void i2c_parser(char *buf) +{ + answer[0] = buf[0]; -void i2c_parser(char *buf) { -answer[0] = buf[0]; switch(buf[0]){ case CMD_I2C_INIT: i2c_init_usb(); @@ -49,45 +52,32 @@ answer[0] = buf[0]; case CMD_I2C_SET_BITRATE: i2c_set_bitrate_usb((char)buf[2]); break; - case CMD_I2C_SEND_BYTE: - i2c_send_byte_usb((int)buf[2]); + + case CMD_I2C_SEND: + i2c_send_usb((int)buf[2], &buf[3]); break; - case CMD_I2C_RECV_BYTES: - i2c_reveice_bytes_usb((int)buf[3],(int)buf[1]-4, (int)buf[2]); + case CMD_I2C_RECV: + i2c_recv_usb((int)buf[2],(int)buf[3]); break; + case CMD_I2C_SEND_START: i2c_send_start_usb(); break; case CMD_I2C_SEND_STOP: i2c_send_stop_usb(); break; + default: - answer[0]=buf[0]; - answer[1]=RSP_UNKOWN_CMD; - answer[2]=0; + answer[1] = RSP_UNKOWN_CMD; + answer[2] = 0; CommandAnswer(3); } } -unsigned char i2c_get_twsr(void) -{ - return (TWSR & 0xF8); -} - - -void i2c_wait_for_complete(void) -{ - while (!(TWCR & (1<<TWINT))){ - //do nothing just wait - ; - } -} - void i2c_init_usb(void) { - answer[0]=CMD_I2C_INIT; answer[1]=(unsigned char)i2c_init(); answer[2]=0; CommandAnswer(3); @@ -105,7 +95,6 @@ int i2c_init(void) void i2c_deinit_usb(void) { - answer[0]=CMD_I2C_DEINIT; answer[1]=(unsigned char)i2c_deinit(); answer[2]=0; CommandAnswer(3); @@ -114,17 +103,16 @@ void i2c_deinit_usb(void) int i2c_deinit(void) { TWCR &= ~(1<<TWEN); // deinit - octopus.ports[12]=PIN_OUT; // this is wrong! - octopus.ports[13]=PIN_OUT; // this is wrong! + octopus.ports[12]=PIN_NONE; + octopus.ports[13]=PIN_NONE; return RSP_OK; } void i2c_set_bitrate_usb(char bitrate) { - answer[0] = CMD_I2C_SET_BITRATE; answer[1] = (unsigned char)i2c_set_bitrate(bitrate); - answer[2] = '\0'; + answer[2] = 0; CommandAnswer(3); } @@ -140,77 +128,82 @@ int i2c_set_bitrate(char bitrate) { void i2c_send_start_usb(void) { - answer[0] = CMD_I2C_SEND_START; answer[1] = (unsigned char)i2c_send_start(); - answer[2] = '\0'; + answer[2] = 0; CommandAnswer(3); } +unsigned char i2c_get_twsr(void) +{ + return (TWSR & 0xF8); +} + int i2c_send_start(void) { i2c_get_twsr(); TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); - i2c_wait_for_complete(); + I2C_WAIT_FOR_COMPLETE(); return RSP_OK; } + + + void i2c_send_stop_usb(void) { - answer[0]=CMD_I2C_SEND_STOP; answer[1]=(unsigned char)i2c_send_stop(); answer[2]=0; CommandAnswer(3); } - int i2c_send_stop(void) { TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); + while(TWCR & (1<<TWSTO)); return RSP_OK; } -void i2c_send_byte_usb(char data) +void i2c_send_usb(int length, char *data) { - i2c_send_byte(data); + i2c_send(data, length); - answer[0] = CMD_I2C_SEND_BYTE; answer[1] = RSP_OK; - answer[2] = '\0'; + answer[2] = 0; CommandAnswer(3); } -void i2c_send_byte(char data) +void i2c_send(char *data, int length) { - TWDR = data; - TWCR = (1<<TWINT) | (1<<TWEN); - i2c_wait_for_complete(); + int i; + + for(i = 0; i < length; i++) { + TWDR = data[i]; + TWCR = (1<<TWINT) | (1<<TWEN); + I2C_WAIT_FOR_COMPLETE(); + } } -void i2c_reveice_bytes_usb(int address,int len, int timeout) + +void i2c_recv_usb(int address, int length) { - /* receive */ - //i2c_get_twsr(); - //print_hex(s); - - //i2c_send_start(); - /* slave address = 0x10; */ - //buf[3] = (0x10 << 1)|1; /* (00010000<<1) | R */ - //i2c_send_byte( (0x10 << 1)|1); - - - answer[0]=CMD_I2C_RECV_BYTES; + int i; + + answer[0]=CMD_I2C_RECV; answer[1]=RSP_OK; - answer[2]=1; - answer[3]=i2c_receive_byte(); - CommandAnswer(5); + + for(i = 0; i < length; i++) + answer[2+i] = i2c_recv_byte(); + + answer[length+2] = 0; + + CommandAnswer(3+length); } -unsigned char i2c_receive_byte(void) +unsigned char i2c_recv_byte(void) { TWCR = (1<<TWINT) | (1<<TWEN); - i2c_wait_for_complete(); + I2C_WAIT_FOR_COMPLETE(); return TWDR; } - |