Skip to content

Instantly share code, notes, and snippets.

@Gfast2
Created August 10, 2017 17:56
Show Gist options
  • Save Gfast2/372ec0ca099712adc351f3b92ca53e71 to your computer and use it in GitHub Desktop.
Save Gfast2/372ec0ca099712adc351f3b92ca53e71 to your computer and use it in GitHub Desktop.
porting bmp280 driver for Beaglebone Black
/* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
* See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "bmp280.h"
#define I2C_BUFFER_LEN 8
#define BUFFER_LENGTH 0xFF
#define BMP280_DATA_INDEX 1
#define BMP280_ADDRESS_INDEX 2
// delay routine
void BMP280_dela_msek(u32 msek){
// do nothing
}
s8 myBMP280_I2C_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
{
s32 iError = BMP280_INIT_VALUE;
u8 array[I2C_BUFFER_LEN];
u8 stringpos = BMP280_INIT_VALUE;
array[BMP280_INIT_VALUE] = reg_addr;
/*
* Please take the below function as your reference for
* write the data using I2C communication
* "IERROR = I2C_WRITE_STRING(DEV_ADDR, ARRAY, CNT+1)"
* add your I2C write function here
* iError is an return value of I2C read function
* Please select your valid return value
* In the driver SUCCESS defined as BMP280_INIT_VALUE
* and FAILURE defined as -1
* Note :
* This is a full duplex operation,
* The first read data is discarded, for that extra write operation
* have to be initiated.Thus cnt+1 operation done in the I2C write string function
* For more information please refer data sheet SPI communication:
*/
// ---- Opening the Bus ---- //
int file;
char *filename = "/dev/i2c-2";
if( ( file = open(filename, O_RDWR) ) < 0 ) { // open() will won't let file buffer works
perror("Failed to open the i2c bus");
iError = ERROR;
return (s8)iError;
}
// Initiating communication with I2C device
int addr = dev_addr;
// Spezifizieren der Slave-Adresse -> Kommunikation frei geben
if( ioctl(file, I2C_SLAVE, addr) < 0 ){
printf("Failed to acquire bus access and/or talk to slave.\n");
iError = ERROR;
return (s8)iError;
}
// get values into local array
for (stringpos = BMP280_INIT_VALUE; stringpos < cnt; stringpos++) {
array[stringpos + BMP280_DATA_INDEX] = *(reg_data + stringpos);
// printf("W: 0x%02x => 0x%02x\n", stringpos, *(reg_data + stringpos));
}
// do the actual register write
if(write(file, array, cnt+1) != cnt+1){
printf("Failed to position the read pointer");
printf("\n\n");
iError = ERROR;
return (s8)iError;
}
close(file);
return (s8)iError;
}
s8 myBMP280_I2C_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
{
s32 iError = BMP280_INIT_VALUE;
u8 array[I2C_BUFFER_LEN] = {BMP280_INIT_VALUE};
u8 stringpos = BMP280_INIT_VALUE;
array[BMP280_INIT_VALUE] = reg_addr;
int file;
char *filename = "/dev/i2c-2";
if( ( file = open(filename, O_RDWR) ) < 0 ) { // open() will won't let file buffer works
perror("Failed to open the i2c bus");
iError = ERROR;
return (s8)iError;
}
// Initiating communication with I2C device
int addr = dev_addr;
// Spezifizieren der Slave-Adresse -> Kommunikation frei geben
if( ioctl(file, I2C_SLAVE, addr) < 0 ){
printf("Failed to acquire bus access and/or talk to slave.\n");
iError = ERROR;
return (s8)iError;
}
// position the register pointer firstly to the start position
if(write(file, array, 1) != 1){
printf("Failed to position the read pointer");
printf("\n\n");
iError = ERROR;
return (s8)iError;
}
// Using I2C Read
if(read(file, array,cnt) != cnt){
printf("Failed to read from the i2c bus.\n");
printf("\n\n");
iError = ERROR;
return (s8)iError;
}
close(file);
/* Please take the below function as your reference
* to read the data using I2C communication
* add your I2C read function here.
* "IERROR = I2C_WRITE_READ_STRING(DEV_ADDR, ARRAY, ARRAY, 1, CNT)"
* iError is an return value of SPI write function
* Please select your valid return value
* In the driver SUCCESS defined as BMP280_INIT_VALUE
* and FAILURE defined as -1
*/
for (stringpos = BMP280_INIT_VALUE; stringpos < cnt; stringpos++) {
*(reg_data + stringpos) = array[stringpos];
// printf("R: 0x%02x => 0x%02x\n", stringpos+reg_addr, array[stringpos]);
;
}
return (s8)iError;
}
int main(int argc, char** argv) {
struct bmp280_t bmp280;
bmp280.bus_read = myBMP280_I2C_bus_read;
bmp280.bus_write= myBMP280_I2C_bus_write;
bmp280.dev_addr = BMP280_I2C_ADDRESS1;
bmp280.delay_msec = BMP280_dela_msek;
/* The variable used to assign the standby time*/
u8 v_standby_time_u8 = BMP280_INIT_VALUE;
/* The variable used to read real temperature*/
s32 v_actual_temp_combined_s32 = BMP280_INIT_VALUE;
/* The variable used to read real pressure*/
u32 v_actual_press_combined_u32 = BMP280_INIT_VALUE;
/* result of communication results*/
s32 com_rslt = ERROR;
com_rslt = bmp280_init(&bmp280);
com_rslt += bmp280_set_power_mode(BMP280_NORMAL_MODE);
com_rslt += bmp280_set_work_mode(BMP280_ULTRA_HIGH_RESOLUTION_MODE);
com_rslt += bmp280_set_standby_durn(BMP280_STANDBY_TIME_1_MS);
com_rslt += bmp280_get_standby_durn(&v_standby_time_u8);
/************************* END INITIALIZATION *************************/
/*------------------------------------------------------------------*
****** INDIVIDUAL APIs TO READ UNCOMPENSATED PRESSURE AND TEMPERATURE*******
*---------------------------------------------------------------------*/
/* API is used to read the uncompensated temperature*/
while(1){
com_rslt += bmp280_read_pressure_temperature(&v_actual_press_combined_u32,
&v_actual_temp_combined_s32);
printf("temp: %.2f Cel., Pres: %d Pa\n",
((float)v_actual_temp_combined_s32)/100,
v_actual_press_combined_u32);
sleep(1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment