drivers/adxl345: cleanup/enhancements#6799
Conversation
drivers/adxl345/adxl345.c
Outdated
|
|
||
| int adxl345_init(adxl345_t *dev, const adxl345_params_t* params) | ||
| int adxl345_init(adxl345_t *dev, const adxl345_params_t* params, uint8_t range, | ||
| uint8_t rate, uint8_t full_res, float scale_factor) |
There was a problem hiding this comment.
As stated in https://github.com/RIOT-OS/RIOT/wiki/Guide:-Writing-a-device-driver-in-RIOT, the init function should have the dev and params struct as only parameters...
There was a problem hiding this comment.
Sorry I didn't remember that part ... I'll change that but I saw others drivers with more than dev and params as arguments like drivers/isl29020
| uint8_t range; /**< Sensitivity configuration */ | ||
| uint8_t rate; /**< Configured sample rate for accel */ | ||
| uint8_t full_res; /**< Resolution bit */ | ||
| uint8_t scale_factor; /**< Scale factor for converting value to mg */ |
There was a problem hiding this comment.
I don't know the device in detail, but it seems to me, like some of these fields are static configuration values, while others can be deducted from them, right?
interrupt -> dynamic param set at runtime -> belongs into adx1345_t
offset -> static params, part of adx1345_params_t
range -> static param, part of adx1345_params_t
rate -> static param, part of adx1345_params_t
full_res -> deducted from range?! -> goes into device descriptor
scale_factor -> deducted from range -> goes into device descriptor
There was a problem hiding this comment.
full_res is not deducted from range so it will belong to adxl345_params_t
I will move each part to right struct
drivers/adxl345/adxl345.c
Outdated
| @@ -43,6 +44,12 @@ int adxl345_init(adxl345_t *dev, const adxl345_params_t* params) | |||
| /* get device descriptor */ | |||
| dev->params= *params; | |||
There was a problem hiding this comment.
this seems wrong, assigment of adx1345_params_t ** to adx1345_params_t? If you do it like this, I think what you want to do is
memcpy(&dev->params, params, sizeof(adx1345_params_t));There was a problem hiding this comment.
But under further consideration, think of only copying the 'often used' fields (e.g. i2c, addr) into RAM and leave the 'use once' fields (e.g. offset) in ROM...
There was a problem hiding this comment.
memcpy(&dev->params, params, sizeof(adx1345_params_t)); was the initial implementation in the previous PR but @aabadie made me change this line. I think copying all parameters with memcpy is more clear.
There was a problem hiding this comment.
actually using dev->params = *params also works and it's also used in other places.
There was a problem hiding this comment.
ture, didn't notice the * instead of &, so never mind...
But still the second consideration holds: IMHO we don't need to copy all config params into RAM...
There was a problem hiding this comment.
@haukepetersen How am I suppose to put some members in RAM and others in ROM ? Should I remove them from the struct and call the #define params in adxl345_init function ?
There was a problem hiding this comment.
noo, actually what I meant is quite simple: instead of having the device descriptor look like
typedef struct {
adx1345_params_t params;
...
} adx1345_t;you could do something like
typedef struct {
i2c_t bus,
uint8_t addr,
adx1345_params_t *p,
....
} adx1345_t;In this case, you would only copy the 'often/periodically' used values into RAM for faster access, while you access the rest of the config params indirectly from ROM (we assume the configuration to be placed in ROM...).
| .scale_factor = ADXL345_PARAM_SCALE_FACTOR} | ||
| #define ADXL345_PARAMS { .i2c = ADXL345_PARAM_I2C, \ | ||
| .addr = ADXL345_PARAM_ADDR, \ | ||
| .offset = ADXL345_PARAM_OFFSET } |
There was a problem hiding this comment.
why don't you write the offset here inline as in .offset = { 0, 0, 0 } } here? Saves some of the verbose lines below...
There was a problem hiding this comment.
Because I copy/paste this from another driver without even thinking ! I'll change it.
|
@haukepetersen I add some commits I will squash when we're done. |
nope, no need. But see my comment above about only copying some params into the dev descriptor, then you would have to change this line anyhow... |
|
Thanks for your help @haukepetersen . I really tought you wanted something far more complicated... |
|
@haukepetersen data->x and data->y are supposed to be equals
|
|
I fixed my problem. It works now. |
aabadie
left a comment
There was a problem hiding this comment.
A few nitpicks. Otherwise looks good.
| adxl345_params_t params; /**< Device configuration */ | ||
| i2c_t i2c; /**< I2C device which is used */ | ||
| uint8_t addr; /**< I2C address */ | ||
| adxl345_params_t *params; /**< Device configuration */ |
tests/driver_adxl345/main.c
Outdated
| adxl345_read(&dev, &data); | ||
| printf("Acceleration [in mg]: X axis:%d Y axis:%d Z axis:%d\n", | ||
| data.x, data.y, data.z); | ||
| (int)data.x, (int)data.y, (int)data.z); |
| #define ADXL345_PARAM_FULL_RES (1) | ||
| #endif | ||
| #ifndef ADXL345_PARAM_OFFSET | ||
| #define ADXL345_PARAM_OFFSET { 0, 0, 0 } |
There was a problem hiding this comment.
please align defines in this file
drivers/adxl345/adxl345.c
Outdated
| #include <stdint.h> | ||
| #include <stdbool.h> | ||
| #include <string.h> | ||
| #include <math.h> |
|
@aabadie thanks for your review, I'll fix it. Here I multiply a integer by a real number so the result is a float and I store it into a int16_t |
I would say leave it as it is without the |
|
Squashed. |
Signed-off-by: dylad <dylan.laduranty@mesotic.com>
|
Murdock approves, go ! |
|
many thanks to both of you @aabadie @haukepetersen ! |
|
@aabadie: there were still un-addressed comments (see my comments above...). The state you merged is broken by design: what if you want to configure two of these devices? Now it won't work... Please make sure you look for open comments before you merge things... |
I'm sorry, I don't which one you are talking about. But apologizes, I should have for your confirmation first.
Can you be more explicit ? |
|
@haukepetersen I really thought all your comments were addressed. Which one did I miss ? |
| dev.i2c); | ||
|
|
||
| if (adxl345_init(&dev, adxl345_params) == ADXL345_OK) { | ||
| if (adxl345_init(&dev, (adxl345_params_t*)adxl345_params) == ADXL345_OK) { |
There was a problem hiding this comment.
@haukepetersen, were you mentioning this regarding the fact only one sensor can be initialized at a time ? Then yes, I admit the line should be:
adxl345_init(&dev, &adxl345_params[0]) == ADXL345_OK
haukepetersen
left a comment
There was a problem hiding this comment.
my fault, seems like my browser did not complete the connection to github when i submitted this review...
| #define ADDR (dev->addr) | ||
|
|
||
| int adxl345_init(adxl345_t *dev, const adxl345_params_t* params) | ||
| int adxl345_init(adxl345_t *dev, adxl345_params_t* params) |
There was a problem hiding this comment.
the const should stay here...
|
|
||
| /* get device descriptor */ | ||
| dev->params= *params; | ||
| dev->params = (adxl345_params_t*)params; |
There was a problem hiding this comment.
but what happened to the i2c bus and the addr?
| ADXL345_STANDBY_MODE, | ||
| ADXL345_SLEEP_MODE, | ||
| ADXL345_AUTOSLEEP_MODE, | ||
| }; |
There was a problem hiding this comment.
documentation missing
There was a problem hiding this comment.
(even though obvious what the enum members mean, doxygen will certainly complain about it...)
| adxl345_data_t data; | ||
|
|
||
| dev.i2c = ADXL345_PARAM_I2C; | ||
| dev.addr = ADXL345_PARAM_ADDR; |
| uint8_t range; /**< Sensitivity configuration */ | ||
| uint8_t rate; /**< Configured sample rate for accel */ | ||
| uint8_t full_res; /**< Resolution bit */ | ||
| } adxl345_params_t; |
There was a problem hiding this comment.
i2c and addr must stay in the params struct...
|
@haukepetersen |
|
perfect, thanks! |
@haukepetersen If you have some time, could you take a look at it please ?
I need some advises about the set state functions. I have to add a clear_state function for each mode. But I don't know how could I put it into one clear and efficient function.