The following small sketch crashes but works if I remove the slave part of the interrupt handler. The borad is connected to a BMP050 barometric pressure sensor.
#include <stdint.h>
#include <libmaple/i2c.h>
#define I2CPORT I2C2
/************************************************************
*
* I2C convenience functions
*
************************************************************/
i2c_msg msg[2];
uint8_t buffer[20];
void i2c_read_buffer(uint8_t device, uint8_t address, uint8_t * buf, uint8_t count)
{
int32_t rc;
/* registar address (W)*/
msg[0].addr = device;
msg[0].flags = 0;
msg[0].data = (uint8_t *)&address;
msg[0].length = 1;
msg[0].xferred = 0;
/* count byte's data request (R)*/
msg[1].addr = device;
msg[1].flags = I2C_MSG_READ;
msg[1].data = buf;
msg[1].length = count;
msg[1].xferred = 0;
rc = i2c_master_xfer(I2CPORT,msg,2,1000);
if (rc<0) {
I2CPORT->error_flags = 0;
I2CPORT->state = I2C_STATE_IDLE;
}
}
/*******************************************************************
Application - setup and main loop
*******************************************************************/
void setup() {
SerialUSB.begin();
i2c_init(I2CPORT);
i2c_master_enable(I2CPORT, I2C_BUS_RESET);
int16_t value;
i2c_read_buffer(0x77, 0xAA, buffer, 2);
value = (buffer[0]<<8) + buffer[1];
}
void loop() {
if (SerialUSB.available()>0) {
SerialUSB.read();
int16_t value;
i2c_read_buffer(0x77, 0xAA, buffer, 2);
value = (buffer[0]<<8) + buffer[1];
SerialUSB.println(value);
}
}