Thanks a bizillion times Tomas!
You are my hero and made my day, truly :smileyhappy:
I had used a macro (atmega8) in as if it returned the first bit as set, and it stayed to the second byte ...
Now all three modules work just fine!!!!
Best Regards,
Hans-Henrik
P.S: For anyone interested in atmega8 code looks like:
// Set FXOS8700 in SPI mode
void init_spi(void) {
FXOS_SS_ENABLE; // To open tri-state buffer and set MISO high
MISO_HI; // SPI init demands SA0/MOSI high during reset
FXOS_RESET_HI; // Active high
_delay_ms(1); // ??
FXOS_RESET_LO;
_delay_ms(1); // FXOS8700 Datasheet defines 1ms wait after reset [p.19]
FXOS_SS_DISABLE;
}
// Read values from register FXOS8700
void SPI_ReadFXOS(uint8_t reg, volatile uint8_t* rbuf, uint8_t len) {
FXOS_SS_ENABLE;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
SPDR = cbi(reg,7); // Transmit; clear bit 7 for reading + 7 bits of register address
while(!(SPSR & 1<<SPIF)); // Wait for SPIF flag
SPDR = reg; // Transmit bit 7 of register address
while(!(SPSR & 1<<SPIF)); // Wait for SPIF flag
for(uint8_t i=0; i<len; i++) {
SPDR = 0x00; // Transmit
while(!(SPSR & 1<<SPIF)); // Wait for SPIF flag
rbuf[i] = SPDR; // Clear SPIF flag 2/2
}
}
FXOS_SS_DISABLE; // 2) For ISR context switcher which re-enable SPI unit
}
// Write value to register FXOS8700 <------------------------- ineffektiv loop
void SPI_WriteFXOS(uint8_t reg, uint8_t val) {
FXOS_SS_ENABLE;
uint8_t reg1 = reg;
sbi(reg1,7);
uint8_t wbuf[3] = {reg1, reg, val}; // Two bytes for write bit and address
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
for(uint8_t i=0; i<3; i++) {
SPDR = wbuf[i]; // Transmit
while(!(SPSR & 1<<SPIF)); // Wait for SPIF flag
}
}
FXOS_SS_DISABLE; // 2) For ISR context switcher which re-enable SPI unit
}