upm_result_t lis3dh_write_reg(const lis3dh_context dev, uint8_t reg, uint8_t val) { assert(dev != NULL); if (dev->spi) { // Mask off 0x80 for writing reg &= 0x7F; uint8_t pkt[2] = { reg, val }; _csOn(dev); if (mraa_spi_transfer_buf(dev->spi, pkt, NULL, 2)) { _csOff(dev); printf("%s: mraa_spi_transfer_buf() failed.", __FUNCTION__); return UPM_ERROR_OPERATION_FAILED; } _csOff(dev); } else { if (mraa_i2c_write_byte_data(dev->i2c, val, reg)) { printf("%s: mraa_i2c_write_byte_data() failed.", __FUNCTION__); return UPM_ERROR_OPERATION_FAILED; } } return UPM_SUCCESS; }
void RF22::spiBurstRead(uint8_t reg, uint8_t* dest, uint8_t len) { uint8_t *request; uint8_t *response; request = (uint8_t *) malloc(sizeof(uint8_t) * (len + 1)); response = (uint8_t *) malloc(sizeof(uint8_t) * (len + 1)); memset(request, 0x00, len + 1); memset(response, 0x00, len + 1); request[0] = reg & ~RF22_SPI_WRITE_MASK; memcpy (&request[1], dest, len); mraa_gpio_write(_cs, 0x1); mraa_gpio_write(_cs, 0x0); usleep(100); mraa_spi_transfer_buf(_spi, request, response, len + 1); usleep(100); mraa_gpio_write(_cs, 0x1); memcpy (dest, &response[1], len); free (request); free (response); }
int lis3dh_read_regs(const lis3dh_context dev, uint8_t reg, uint8_t* buffer, int len) { assert(dev != NULL); if (dev->spi) { // Needed for read with address autoincrement reg |= 0xC0; uint8_t sbuf[len + 1]; memset((char*) sbuf, 0, len + 1); sbuf[0] = reg; _csOn(dev); if (mraa_spi_transfer_buf(dev->spi, sbuf, sbuf, len + 1)) { _csOff(dev); printf("%s: mraa_spi_transfer_buf() failed\n", __FUNCTION__); return -1; } _csOff(dev); // Now copy it into user buffer for (int i = 0; i < len; i++) { buffer[i] = sbuf[i + 1]; } } else { // Needed for read with address autoincrement reg |= 0x80; if (mraa_i2c_read_bytes_data(dev->i2c, reg, buffer, len) != len) { return -1; } } return len; }
static upm_result_t kx122_write_register(const kx122_context dev, uint8_t reg, uint8_t val) { if(dev->using_spi){ reg &= SPI_WRITE; uint8_t spi_data[2] = {reg,val}; kx122_chip_select_on(dev); if(mraa_spi_transfer_buf(dev->spi,spi_data,NULL,(sizeof(spi_data) / sizeof(uint8_t))) != MRAA_SUCCESS){ printf("%s: mraa_spi_transfer_buf() failed.\n", __FUNCTION__); kx122_chip_select_off(dev); return UPM_ERROR_OPERATION_FAILED; } kx122_chip_select_off(dev); return UPM_SUCCESS; } else{ if(mraa_i2c_write_byte_data(dev->i2c,val,reg) != MRAA_SUCCESS){ printf("%s: mraa_i2c_write_byte_data() failed.\n",__FUNCTION__); return UPM_ERROR_OPERATION_FAILED; } return UPM_SUCCESS; } }
//Register operations static upm_result_t kx122_read_register(const kx122_context dev, uint8_t reg, uint8_t *data) { if(dev->using_spi){ reg |= SPI_READ; uint8_t spi_data[2] = {reg,0}; kx122_chip_select_on(dev); if(mraa_spi_transfer_buf(dev->spi,spi_data,spi_data,(sizeof(spi_data) / sizeof(uint8_t))) != MRAA_SUCCESS){ printf("%s: mraa_spi_transfer_buf() failed.\n", __FUNCTION__); kx122_chip_select_off(dev); return UPM_ERROR_OPERATION_FAILED; } *data = spi_data[1]; kx122_chip_select_off(dev); return UPM_SUCCESS; } else{ int value = mraa_i2c_read_byte_data(dev->i2c,reg); if(value != -1){ *data = (uint8_t) value; return UPM_SUCCESS; } printf("%s: mraa_i2c_read_byte_data() failed, reading from register 0x%x\n",__FUNCTION__, reg); return UPM_ERROR_OPERATION_FAILED; } }
uint8_t* mraa_spi_write_buf(mraa_spi_context dev, uint8_t* data, int length) { uint8_t* recv = malloc(sizeof(uint8_t) * length); if (mraa_spi_transfer_buf(dev, data, recv, length) != MRAA_SUCCESS) { free(recv); return NULL; } return recv; }
uint8_t* mraa_spi_write_buf(mraa_spi_context dev, uint8_t* data, int length) { if (dev == NULL) { syslog(LOG_ERR, "spi: write_buf: context is invalid"); return NULL; } uint8_t* recv = malloc(sizeof(uint8_t) * length); if (mraa_spi_transfer_buf(dev, data, recv, length) != MRAA_SUCCESS) { free(recv); return NULL; } return recv; }
static upm_result_t kx122_read_registers(const kx122_context dev, uint8_t reg, uint8_t *buffer, uint len) { if(dev->using_spi){ reg |= SPI_READ; uint8_t spi_data_buffer[len + 1]; spi_data_buffer[0] = reg; kx122_chip_select_on(dev); if(mraa_spi_transfer_buf(dev->spi,spi_data_buffer,spi_data_buffer, (sizeof(spi_data_buffer) / sizeof(uint8_t))) != MRAA_SUCCESS){ printf("%s: mraa_spi_transfer_buf() failed.\n", __FUNCTION__); kx122_chip_select_off(dev); return UPM_ERROR_OPERATION_FAILED; } for (size_t i = 0; i < len; i++) { buffer[i] = spi_data_buffer[i + 1]; } kx122_chip_select_off(dev); return UPM_SUCCESS; } else{ uint8_t data_buffer[len]; if(mraa_i2c_read_bytes_data(dev->i2c,reg,data_buffer,len) != (int)len){ return UPM_ERROR_OPERATION_FAILED; } else{ for(size_t i = 0; i < len; i++){ buffer[i] = data_buffer[i]; } return UPM_SUCCESS; } } }
uint8_t lis3dh_read_reg(const lis3dh_context dev, uint8_t reg) { assert(dev != NULL); if (dev->spi) { // Needed for read reg |= 0x80; uint8_t pkt[2] = { reg, 0 }; _csOn(dev); if (mraa_spi_transfer_buf(dev->spi, pkt, pkt, 2)) { _csOff(dev); printf("%s: mraa_spi_transfer_buf() failed\n", __FUNCTION__); return 0xFF; } _csOff(dev); return pkt[1]; } else { return (uint8_t) mraa_i2c_read_byte_data(dev->i2c, reg); } }
/** * Transfer data to and from SPI device Receive pointer may be null if * return data is not needed. * * @param txBuf buffer to send * @param rxBuf buffer to optionally receive data from spi device * @param length size of buffer to send * @return Result of operation */ Result transfer(uint8_t* txBuf, uint8_t* rxBuf, int length) { return (Result) mraa_spi_transfer_buf(m_spi, txBuf, rxBuf, length); }