-
Notifications
You must be signed in to change notification settings - Fork 2
I2C slave & clock calibration #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Main comment: fix the many coding style inconsistencies. I only checked rapidly for other things but will check in details after style has been fixed.
This review is a bit messy as I started to review commits one by one, then changes as a whole (since the 2d I2C commit updates previous changes).
modules/clock/config/clock_config.h
Outdated
@@ -31,5 +31,10 @@ | |||
#define CLOCK_PER2_FREQ CLOCK_CPU_FREQ | |||
#define CLOCK_PER4_FREQ CLOCK_CPU_FREQ | |||
|
|||
// uncomment the following line to activate automatic run-time calibration of internal clock |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable should be documented using Doxygen syntax.
Something like this (need to check if Doxygen autodoc #undef
too):
/** @brief Active automatic run-time calibration of internal clock
* ...
*/
#undef CLOCK_AUTOMATIC_RUN_TIME_CALIBRATION
modules/clock/clock.c
Outdated
@@ -59,6 +61,14 @@ void clock_init(void) | |||
CLOCK_OSC_ENABLE_AND_WAIT(XOSC); | |||
#endif | |||
|
|||
#ifdef CLOCK_AUTOMATIC_RUN_TIME_CALIBRATION | |||
# if CLOCK_SOURCE == CLOCK_SOURCE_RC2M | |||
DFLLRC2M.CTRL = DFLL_ENABLE_bm; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line should be indented with 3 spaces (same below).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
corrected in ef2ed19
modules/i2c/i2c.h
Outdated
@@ -42,31 +40,35 @@ typedef struct i2cs_struct i2cs_t; | |||
#elif (defined I2CC_MASTER) | |||
# define i2cC (&TWIC.MASTER) | |||
#elif (defined I2CC_SLAVE) | |||
extern i2cs_t *const i2cC; | |||
# define X_(p,s) p ## C ## s | |||
#include "slavex.inc.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing indent (one space), same below.
modules/i2c/i2cs.h
Outdated
//@{ | ||
/** | ||
* @file | ||
* @brief I2C definitions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inaccurate comment (copied from i2c.h actually).
modules/i2c/i2cs.h
Outdated
#include "i2c_config.h" | ||
|
||
/* Transaction status defines.*/ | ||
#define I2CS_STATUS_READY 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coding style: don't align like this, use two spaces after the definition name.
modules/i2c/slavex.inc.c
Outdated
*/ | ||
void i2cX(_receive_handler(void)) { | ||
/* Enable stop interrupt. */ | ||
uint8_t currentCtrlA = TWIX.SLAVE.CTRLA; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use camelCase variable names.
modules/i2c/slavex.inc.c
Outdated
uint8_t data = TWIX.SLAVE.DATA; | ||
i2csX->received_data[i2csX->bytes_received] = data; | ||
|
||
/* Process data. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Useless comment.
modules/i2c/i2c.h
Outdated
@@ -12,7 +12,9 @@ | |||
#include <avr/io.h> | |||
#include <avarix/intlvl.h> | |||
#include <stdint.h> | |||
#include <stddef.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What requires this header?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of NULL keyword in slavex.inc.c file.
It has been moved in slavex.inc.c in ef2ed19
modules/i2c/i2cs.h
Outdated
|
||
/* Transaction result enumeration */ | ||
typedef enum { | ||
I2CS_RESULT_UNKNOWN = 0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't align commas (and equal).
Also, why using (0x01<<0)
and not simply 1?
modules/i2c/i2cs.h
Outdated
I2CS_RESULT_ABORTED | ||
} i2c_result_t; | ||
|
||
/*! \brief i2c slave driver struct. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency:
- use
/** @brief
- Use I2C (not i2c) in comments (unless speaking of the module or a symbol name).
And as above, don't try align comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Second pass. Will read actual I2C code more carefully at a better time (not so late).
modules/clock/config/clock_config.h
Outdated
@@ -31,5 +31,11 @@ | |||
#define CLOCK_PER2_FREQ CLOCK_CPU_FREQ | |||
#define CLOCK_PER4_FREQ CLOCK_CPU_FREQ | |||
|
|||
/** @brief Activates automatic run-time calibration of internal clock |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brief
should be separated from detailed information by starting a new paragraphe (empty line).
Also, details should be properly capitalized and punctuated, and only separated by one space from the comment block's *
.
Other occurrences below.
modules/i2c/i2cs.h
Outdated
//@{ | ||
/** | ||
* @file | ||
* @brief I2Cs definitions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a lazy change (what is the meaning of I2Cs?). I2C slave definitions would be better.
modules/i2c/i2cs.h
Outdated
#include <stdint.h> | ||
#include "i2c_config.h" | ||
|
||
// Transaction status defines |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use ///
for 1-line documentation of elements in Doxygen.
This is equivalent to /** @brief ... */
.
(Other occurrences below.)
modules/i2c/i2cs.h
Outdated
I2CS_RESULT_TRANSMIT_COLLISION, | ||
I2CS_RESULT_BUS_ERROR, | ||
I2CS_RESULT_FAIL, | ||
I2CS_RESULT_ABORTED |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, add a trailing comma after the last element.
modules/i2c/i2cs.h
Outdated
I2CS_RESULT_ABORTED | ||
} i2c_result_t; | ||
|
||
/* @brief I2C slave driver struct. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/**
instead of /*
(Other occurrences below.)
modules/i2c/slavex.inc.c
Outdated
*/ | ||
static void i2cX(_transmit_handler(void)) { | ||
/* If NACK, slave transmit transaction finished. */ | ||
if ((i2cX().bytes_transmit > 0) && (TWIX.SLAVE.STATUS & |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Remove superfluous parenthesis around first condition.
- Remove extra space after
if
.
Other occurrences below.
modules/i2c/slavex.inc.c
Outdated
/* If buffer overflow. */ | ||
else { | ||
TWIX.SLAVE.CTRLB = TWI_SLAVE_CMD_COMPTRANS_gc; | ||
i2cX(_transaction_finished(I2CS_RESULT_BUFFER_OVERFLOW)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be i2cX(_transaction_finished)(I2CS_RESULT_BUFFER_OVERFLOW);
Similar occurrences below.
|
||
#error Slave mode not supported yet | ||
#define I2CX(s) X_(I2C,s) | ||
#define i2cX(s) X_(i2c,s) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should define and use i2csX(s) X_(i2cs,s)
instead, for method names and everything that is slave-specific.
modules/i2c/slavex.inc.c
Outdated
(current_status & TWI_SLAVE_AP_bm)) { | ||
i2cX(_address_match_handler()); | ||
/* Process data. */ | ||
if(i2cX().process_data != NULL) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always enclose if
/else
/... bodies with {...}
.
(Multiple occurences.)
modules/i2c/slavex.inc.h
Outdated
|
||
|
||
/* I2cX Register callback */ | ||
void i2cX(s_register_callback)( void (*process_data_function)(void)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra space after (
.
include/avarix.h
Outdated
@@ -12,7 +12,8 @@ | |||
#define MAX(x,y) ((x) > (y) ? (x) : (y)) | |||
/// Clamp a value to range [x;y] | |||
#define CLAMP(v,x,y) ((v) < (x) ? (x) : (v) > (y) ? (y) : (v)) | |||
|
|||
/// Get number of elements in an array | |||
#define LENGTHOF(a) (sizeof(a)/sizeof((a)[0])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Version un peu meilleure :
#define LENGTHOF(a) ((sizeof(a) / sizeof((a)[0])) / (sizeof(a) % sizeof((a)[0]) == 0))
Ça permet de détecter à la compilation certains cas (pas tous malheureusement) où on passe un pointeur au lieu d'un tableau.
Sinon pour être 100% safe, y'a la méthode Russell, qui pique un peu quand même. ;)
modules/clock/config/clock_config.h
Outdated
* | ||
* Only usable with 2MHz or 32Mhz internal clocks. | ||
* It will use the 32768kHz calibrated clock as the adjustement source. | ||
/** @brief Activates automatic run-time calibration of internal clock |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
J'avais déjà mergé cette partie. Mieux vaut garder la version actuelle (déjà utilisée).
modules/i2c/i2c.c
Outdated
@@ -151,5 +151,9 @@ int8_t i2cm_recv(i2cm_t *m, uint8_t addr, uint8_t *data, uint8_t n) | |||
return i; | |||
} | |||
|
|||
int8_t i2cs_send_async(i2cs_t *s, uint8_t *data, uint8_t n) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code mort.
modules/i2c/i2c.h
Outdated
} i2cs_state_t; | ||
|
||
#ifndef I2CS_RECV_BUFFER_SIZE | ||
#define I2CS_RECV_BUFFER_SIZE 32 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coding style :
- indentation du
define
- double espace avant la valeur
(idem plus bas)
modules/i2c/i2c.h
Outdated
#define I2CS_SEND_BUFFER_SIZE 32 | ||
#endif | ||
|
||
struct i2cs; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forward declaration non nécessaire.
modules/i2c/slavex.inc.c
Outdated
i2cs->sent_bytes = 0; | ||
|
||
// ask user to provision send buffer | ||
int rsz = i2cs->send_callback(i2cs->send_buffer, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
La valeur de retour pourrait être un uint8_t
.
modules/i2c/slavex.inc.c
Outdated
sizeof(i2cs->send_buffer)); | ||
if(rsz > 0) { | ||
// user got some data to send | ||
i2cs->bytes_to_send = MIN(rsz,I2CS_SEND_BUFFER_SIZE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Par souci de cohérence, n'utilises pas tantôt
sizeof(i2cs->send_buffer)
, tantôtI2CS_SEND_BUFFER_SIZE
. - Espace après la virgule.
- Si
bytes_to_send
est trop grand, on devrait gérer ça comme une erreur plutôt que de passer ça sous silence, au moins l'utilisateur serait au courant.
modules/i2c/slavex.inc.h
Outdated
//@{ | ||
/** | ||
* @file | ||
* @brief I2C definitions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I2C slave definitions
modules/i2c/i2c.h
Outdated
|
||
int sent_bytes; | ||
int bytes_to_send; | ||
uint8_t send_buffer[I2CS_SEND_BUFFER_SIZE]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sauf erreur, on ne fait pas lire et écrire simultanément.
Les mêmes variables (notamment le buffer) peuvent être utilisées pour la lecture et l'écriture.
modules/i2c/i2c.h
Outdated
|
||
i2cs_state_t state; | ||
|
||
int recvd_bytes; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Utiliser des uint8_t
quand c'est possible.
modules/i2c/slavex.inc.c
Outdated
// Interrupt handler | ||
ISR(twiX(_TWIS_vect)) { | ||
|
||
i2cs_t *i2cs = &i2cX(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok.
Côté coût il ne devrait pas y avoir de différence (et le compilo devrait être capable d'optimiser).
C'était juste une question de style.
uint8_t send_buffer[I2CS_SEND_BUFFER_SIZE]; | ||
|
||
i2cs_recv_callback_t recv_callback; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tu peux retirer les lignes vides entre chaque définition de callback.
|
||
uint8_t sent_bytes; | ||
uint8_t bytes_to_send; | ||
uint8_t send_buffer[I2CS_SEND_BUFFER_SIZE]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ton avis sur la possibilité de mutualiser les variables recv/send (notamment les buffers) ?
(Sachant que tu peux utiliser une union pour garder des noms pertinents au besoin.)
TWIX.SLAVE.ADDR = I2CX(_ADDRESS) << 1; | ||
} | ||
|
||
void i2cX(s_register_reset_callback)(i2cs_reset_callback_t f) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Une raison pour ne pas plutôt avoir une fonction qui prend en paramètre le i2cs_t*
à modifier ?
Pour l'init on peut rester avec une fonction part I2C (vu que les init sont appellées d'un bloc par le module, de mémoire). Par contre pour les accesseurs ça rend la lib plus générique (pas besoin de connaître le nom de la fonction, on a juste à fournir le bon objet).
C'est comme ça que fonctionne le module UART notamment.
} | ||
|
||
// Interrupt handler | ||
ISR(twiX(_TWIS_vect)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
La routine d'interruption étant conséquente, ça serait intéressant d'ajouter une indirection vers une fonction générique qui prendrait un i2cs_t*
en paramètre.
Là encore, ça suit ce qui est fait pour l'UART.
No description provided.