Created
May 13, 2015 10:45
-
-
Save 0xc0170/d8b944cc187e98092caf to your computer and use it in GitHub Desktop.
I2c - mma7760 test asynch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "mbed.h" | |
#define MMA7660_ADDRESS 0x98 | |
volatile int gevent; | |
volatile bool done; | |
volatile const char * why; | |
void callback(int event) { | |
done = true; | |
gevent = event; | |
switch(event) { | |
case I2C_EVENT_ERROR: | |
why = "Generic I2C error"; | |
break; | |
case I2C_EVENT_ERROR_NO_SLAVE: | |
why = "Slave did not reply"; | |
break; | |
case I2C_EVENT_TRANSFER_COMPLETE: | |
break; | |
case I2C_EVENT_TRANSFER_EARLY_NACK: | |
why = "Early NAK received"; | |
break; | |
default: | |
why = "Unknown Error"; | |
break; | |
} | |
} | |
int main(void) | |
{ | |
I2C i2c(PTE25, PTE24); | |
event_callback_t event; | |
event.attach(callback); | |
why = "running..."; | |
done = false; | |
printf("Testing MMA7660 on the mbed application shield \n"); | |
char mode, mma_reg[3]={0x07,0x07,0x07}; | |
// Read mode | |
int rc = i2c.transfer(MMA7660_ADDRESS, mma_reg, 1, &mode, 1, event, I2C_EVENT_ALL, false); | |
if (rc) { | |
printf("Failed to start transfer\n"); | |
return -1; | |
} | |
while (!done) { | |
sleep(); | |
} | |
// set MMA7660 to active | |
done = false; | |
mode |= 0x1; | |
rc = i2c.transfer(MMA7660_ADDRESS, &mode, 1, NULL, 0, event, I2C_EVENT_ALL, false); | |
if (rc) { | |
printf("Failed to start transfer\n"); | |
return -1; | |
} | |
while (!done) { | |
sleep(); | |
} | |
if (gevent != I2C_EVENT_TRANSFER_COMPLETE) { | |
printf("Failed to complete the transfer, event: %d \n", gevent); | |
return -1; | |
} | |
// Test connection | |
done = false; | |
rc = i2c.transfer(MMA7660_ADDRESS, NULL, 0, &mode, 1, event, I2C_EVENT_ALL, false); | |
if (rc) { | |
printf("Failed to start transfer\n"); | |
return -1; | |
} | |
while (!done) { | |
sleep(); | |
} | |
if (mode != 0) { | |
printf("Failed to find MMA7660 \n"); | |
return -1; | |
} | |
if (gevent != I2C_EVENT_TRANSFER_COMPLETE) { | |
printf("Failed to complete the transfer, event: %d \n", gevent); | |
return -1; | |
} | |
done = false; | |
rc = i2c.transfer(MMA7660_ADDRESS, NULL, 0, NULL, 0, event, I2C_EVENT_ALL, false); | |
if (rc) { | |
printf("Failed to start transfer\n"); | |
return -1; | |
} | |
while (!done) { | |
sleep(); | |
} | |
if (gevent != I2C_EVENT_TRANSFER_COMPLETE) { | |
printf("Failed to complete the transfer, event: %d \n", gevent); | |
return -1; | |
} | |
printf("{{success}}\r\n"); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment