Remove all compilation warnings in adxl345.c

This commit is contained in:
grisel-davy 2021-02-18 10:29:11 +01:00
parent a9a94282f8
commit 6fa2fd6a11
2 changed files with 74 additions and 81 deletions

118
adxl345.c
View file

@ -55,14 +55,6 @@ static int fifo_len(struct Fifo *f) {
}
}
static int fifo_empty(struct Fifo *f) {
return fifo_len(f) == 0;
}
static int fifo_full(struct Fifo *f) {
return fifo_len(f) == FIFO_SIZE;
}
static void fifo_push(struct Fifo *f, struct Sample *s) {
f->content[f->write_idx] = *s;
f->write_idx = (f->write_idx + 1) % FIFO_SIZE;
@ -84,10 +76,11 @@ ssize_t adxl345_read(struct file *file, char __user *buf, size_t count, loff_t *
char nsample;
char rest;
unsigned char res[count];
unsigned char res[FIFO_SIZE*6]; // Not res[count] to avoid C99 warning
int i=0;
int16_t direction_sample;
struct Sample sample;
int ret;
// TODO: permetre de choisir l'axe de lecture
//pr_info("In adxl345_read function\n");
@ -105,7 +98,7 @@ ssize_t adxl345_read(struct file *file, char __user *buf, size_t count, loff_t *
rest = 0;
}
//pr_info("%i sample in fifo\nGetting %i full sample ans %i more bytes\n",fifo_len(&(d->fifo)),nsample,rest);
//pr_info("%i sample in fifo\nGetting %i full sample and %i more bytes\n",fifo_len(&(d->fifo)),nsample,rest);
// Get the full samples
for(i=0;i<nsample;i++){
@ -118,11 +111,13 @@ ssize_t adxl345_read(struct file *file, char __user *buf, size_t count, loff_t *
res[i*6+5] = (unsigned char) (sample.z>>8);
res[i*6+4] = (unsigned char) (sample.z & 0x00ff);
//pr_info("%i %i %i\n",sample.x,sample.y,sample.z);
//pr_info("%x %x %x %x\n",res[i], res[i+1], res[i+2],res[i+3]);
}
// Get the rest
sample = fifo_pop(&(d->fifo));
if(rest>0){
sample = fifo_pop(&(d->fifo));
}
for(i=nsample*6;i<nsample*6+rest;i++){
//pr_info("Getting Byte %i/%i\n",i,nsample*6+rest);
// Get the correct direction
@ -135,7 +130,10 @@ ssize_t adxl345_read(struct file *file, char __user *buf, size_t count, loff_t *
// Ecriture du resultat dans le buffer de retour
//pr_info("%x %x %x %x\n",res[3], res[2], res[1],res[0]);
copy_to_user(buf, res, nsample*6+rest);
if((ret = copy_to_user(buf, res, nsample*6+rest)) > 0){
pr_info("%i bytes not copied to user\n",ret);
return(-1);
}
return (ssize_t)nsample*6+rest;
}
@ -144,10 +142,7 @@ irq_handler_t adxl345_int(int irq, struct adxl345_device * d){
struct Sample sample;
char question[2];
// TODO: changer le format de question
question[0] = 0x39;
question[1] = 0x00;
char question = 0x39;
unsigned char response[6];
char ret_code[2];
@ -160,7 +155,7 @@ irq_handler_t adxl345_int(int irq, struct adxl345_device * d){
struct i2c_client *client = container_of(dev,struct i2c_client,dev);
// Get number of entries in Hard FIFO
ret_code[0] = i2c_master_send(client,question,1);
ret_code[0] = i2c_master_send(client,&question,1);
ret_code[1] = i2c_master_recv(client,response,1);
if(ret_code[0] < 0 || ret_code[1] < 0){
pr_info("Error in reading FIFO STATUS: %i %i\n",ret_code[0], ret_code[1]);
@ -170,8 +165,8 @@ irq_handler_t adxl345_int(int irq, struct adxl345_device * d){
for(i=nentries; i>0; i--){
// Get entries from Hard FIFO
question[0] = 0x32;
ret_code[0] = i2c_master_send(client,question,1);
question = 0x32;
ret_code[0] = i2c_master_send(client,&question,1);
ret_code[1] = i2c_master_recv(client,response,6);
sample.x = (int16_t)(response[1]<<8 | response[0]);
@ -185,43 +180,48 @@ irq_handler_t adxl345_int(int irq, struct adxl345_device * d){
nsample = fifo_len(&(d->fifo));
//pr_info("%i samples in Soft FIFO\n",nsample);
return IRQ_HANDLED;
return (void *)IRQ_HANDLED;
}
static int adxl345_probe(struct i2c_client *client,const struct i2c_device_id *id){
// Initialize structures
// Instantiation de adxl345_device
// i2c variables
char question[2];
char response[1];
char ret_code;
// Interuption variables
void *int_ptr = &adxl345_int;
const char *device_name = "adxl345";
int code;
// Init adxl345_device
struct adxl345_device *d;
d = (struct adxl345_device *) kzalloc(sizeof(struct adxl345_device), GFP_KERNEL);
pr_info("adxl_345_device and file_operation struct allocated\n");
// Initialisation de la fifo
// Init FIFO
fifo_init(&(d->fifo));
pr_info("Fifo Initialized\n");
// Association avec i2c_client
// Init i2c_client
i2c_set_clientdata(client,d);
pr_info("i2c_clientdata set\n");
// Remplissage des champs de miscdev
// Populate miscdev fields
d->miscdev.parent = &(client->dev);
d->miscdev.minor = MISC_DYNAMIC_MINOR;
d->miscdev.name = "adxl345";
d->miscdev.fops = &adxl345_fops;
// Erreur associée: adxl345_fops has an incomplete type struct file_operation
pr_info("miscdev fields set\n");
//pr_info("miscdev fields set\n");
// Enregistrement aupres de misc
// Registering to misc
misc_register(&(d->miscdev));
pr_info("misc_register set\n");
pr_info("Misc minor is: %i\n",d->miscdev.minor);
//pr_info("misc_register set\n");
//pr_info("Misc minor is: %i\n",d->miscdev.minor);
// Allocate question(address), response, and return code. All Char
char question[2];
char response[1];
char ret_code;
// Verify device ID
question[0] = 0x00;
@ -231,10 +231,9 @@ static int adxl345_probe(struct i2c_client *client,const struct i2c_device_id *i
if(ret_code < 0){
pr_info("i2c return code: %i\n",ret_code);
}
pr_info("DEVID: %x\n",*response);
// Set parameters
//pr_info("DEVID: %x\n",*response);
// Set device parameters
// Power control measure mode
question[0] = 0x2D;
question[1] = 0x08;
@ -278,60 +277,41 @@ static int adxl345_probe(struct i2c_client *client,const struct i2c_device_id *i
pr_info("Device Setting done.\n");
// Interruption Handler:
void *int_ptr = &adxl345_int;
const char *device_name = "adxl345";
int code;
code = devm_request_threaded_irq(&(client->dev),client->irq, NULL, int_ptr, IRQF_ONESHOT, device_name, d);
pr_info("irq declaration done. Irq number: %i\n",client->irq);
pr_info("irq request return code: %i\n",code);
//pr_info("irq declaration done. Irq number: %i\n",client->irq);
//pr_info("irq request return code: %i\n",code);
pr_info("ADXL345 device setup done.\n");
return(0);
}
static int adxl345_remove(struct i2c_client *client)
{
// Allocate question(address), response, and return code. All Char
char *question;
char *response;
char *ret_code;
question = kmalloc(2,GFP_KERNEL);
response = kmalloc(1,GFP_KERNEL);
ret_code = kmalloc(1,GFP_KERNEL);
if (!question && !response && !ret_code){
pr_info("Unable to get allocate memory (question)\n");
kfree(question);
kfree(response);
kfree(ret_code);
return(-1);
}
struct adxl345_device *d;
char question[2];
char ret_code;
// Power control measure mode
question[0] = 0x2D;
question[1] = 0x00;
ret_code = i2c_master_send(client,question,2);
if(ret_code < 0){
pr_info("Error when setting device in standby mode.\nReturn code: %i\n", ret_code);
return(1);
}
else{
pr_info("Device put to Standby mode");
pr_info("ADXL345 Device put to standby mode.");
}
// Free memory
kfree(question);
kfree(response);
kfree(ret_code);
// récupération de l'instance de adxl345_device
struct adxl345_device *d;
d = i2c_get_clientdata(client);
// Désenregistrement de misc
misc_deregister(&(d->miscdev));
kfree(d);
pr_info("ADXL345 Removed\n");
pr_info("ADXL345 Device unregistered.\n");
return(0);
}

View file

@ -1,30 +1,43 @@
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
const
struct Sample{
int16_t x;
int16_t y;
int16_t z;
};
int main(){
int fd;
char buf[32];
size_t count = 1;
unsigned char buf[64];
size_t count = 6;
ssize_t ret_size;
struct Sample sample;
if((fd = open("/dev/adxl345", O_RDWR)) == -1){
printf("/dev/adxl345 opening failed\n");
return(0);
}
else{
if((ret_size = read(fd, &buf, count)) < 0){
printf("Error while reading the device file.\n");
return(1);
}
else{
printf("Data read successfully. Asked %i byte, received %i byte\n",count, ret_size);
printf("Data:\n");
for(int i = 0; i<ret_size; i++){
printf("%i",buf[i]);
for(int i=0; i<100; i++){
if((ret_size = read(fd, &buf, count)) < 0){
printf("Error while reading the device file.\n");
return(1);
}
else if(ret_size < count){
printf(".");
}
else{
sample.x = (int16_t)(buf[1]<<8 | buf[0]);
sample.y = (int16_t)(buf[3]<<8 | buf[2]);
sample.z = (int16_t)(buf[5]<<8 | buf[4]);
printf("Sample %i: %i %i %i\n",i,sample.x,sample.y,sample.z);
}
}
}