X-Git-Url: https://git.karo-electronics.de/?p=oswald.git;a=blobdiff_plain;f=metawatch%2Fmw_acc.c;fp=metawatch%2Fmw_acc.c;h=1080c3e5e53d671e833a1574e61f6793c2d23944;hp=0717a9246950dbf0a713ce23f6aec19134a4c3ef;hb=1b5790095c23913d02531727e47b79af3568e0b1;hpb=3e320aaa4175a0ed469581f1dea2eac35b390878 diff --git a/metawatch/mw_acc.c b/metawatch/mw_acc.c index 0717a92..1080c3e 100644 --- a/metawatch/mw_acc.c +++ b/metawatch/mw_acc.c @@ -49,6 +49,7 @@ __interrupt void ACCERLEROMETER_I2C_ISR(void) nop(); break; case ACCELEROMETER_STPIFG: + nop(); break; case ACCELEROMETER_RXIFG: if (LengthCount > 0) { @@ -106,6 +107,8 @@ void mw_acc_disable_i2c(void) void mw_acc_i2c_write(uint8_t RegisterAddress, uint8_t *pData, uint8_t Length) { + int tmo; + if (Length == 0 || pData == 0) return; @@ -134,8 +137,15 @@ void mw_acc_i2c_write(uint8_t RegisterAddress, uint8_t *pData, uint8_t Length) ACCELEROMETER_IE |= UCTXIE; ACCELEROMETER_TXBUF = RegisterAddress; - while (AccelerometerBusy) - nop(); + tmo = 0; + while (AccelerometerBusy) { + while (tmo++ < 1000) + __delay_cycles(16000); + if (tmo >= 1000) { + debug_uart_tx("ACC I2C tx tmo\n"); + return; + } + } while (ACCELEROMETER_CTL1 & UCTXSTP) nop(); @@ -180,17 +190,21 @@ void mw_acc_i2c_read_single(const uint8_t RegisterAddress, const uint8_t *pData) * received. If this is interrupted an extra byte may be read. * however, it will be discarded during the next read */ - if ( LengthCount == 1 ) { + if (LengthCount == 1) { /* errata usci30: prevent interruption of sending stop * so that only one byte is read * this requires 62 us @ 320 kHz, 51 @ 400 kHz */ + __disable_interrupt(); + ACCELEROMETER_CTL1 |= UCTXSTT; while(ACCELEROMETER_CTL1 & UCTXSTT) nop(); ACCELEROMETER_CTL1 |= UCTXSTP; + + __enable_interrupt(); } else { ACCELEROMETER_CTL1 |= UCTXSTT; } @@ -225,7 +239,9 @@ void mw_acc_init(void) char tstr[16]; #endif + // it takes at least 20ms to power up ENABLE_ACCELEROMETER_POWER(); + __delay_cycles(320000); mw_acc_init_i2c(); @@ -350,7 +366,7 @@ void mw_acc_disable(void) ACCELEROMETER_INT_DISABLE(); mw_acc_disable_i2c(); - DISABLE_ACCELEROMETER_POWER(); + /// DISABLE_ACCELEROMETER_POWER(); AccelState = ACCEL_STATE_DISABLED; } }