Skip to content

I2C slave & clock calibration - #1

Open
AugustinBoisvert wants to merge 8 commits into
masterfrom
dev
Open

I2C slave & clock calibration#1
AugustinBoisvert wants to merge 8 commits into
masterfrom
dev

Conversation

@AugustinBoisvert

Copy link
Copy Markdown
Member

No description provided.

@benoitryder benoitryder left a comment

Copy link
Copy Markdown
Member

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).

Comment thread modules/clock/config/clock_config.h Outdated
#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

Copy link
Copy Markdown
Member

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

Comment thread modules/clock/clock.c

#ifdef CLOCK_AUTOMATIC_RUN_TIME_CALIBRATION
# if CLOCK_SOURCE == CLOCK_SOURCE_RC2M
DFLLRC2M.CTRL = DFLL_ENABLE_bm;

Copy link
Copy Markdown
Member

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).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

corrected in ef2ed19

Comment thread modules/i2c/i2c.h Outdated
#elif (defined I2CC_SLAVE)
extern i2cs_t *const i2cC;
# define X_(p,s) p ## C ## s
#include "slavex.inc.h"

Copy link
Copy Markdown
Member

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.

Comment thread modules/i2c/i2cs.h Outdated
//@{
/**
* @file
* @brief I2C definitions

Copy link
Copy Markdown
Member

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).

Comment thread modules/i2c/i2cs.h Outdated
#include "i2c_config.h"

/* Transaction status defines.*/
#define I2CS_STATUS_READY 0

Copy link
Copy Markdown
Member

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.

Comment thread modules/i2c/slavex.inc.c Outdated
*/
void i2cX(_receive_handler(void)) {
/* Enable stop interrupt. */
uint8_t currentCtrlA = TWIX.SLAVE.CTRLA;

Copy link
Copy Markdown
Member

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.

Comment thread modules/i2c/slavex.inc.c Outdated
uint8_t data = TWIX.SLAVE.DATA;
i2csX->received_data[i2csX->bytes_received] = data;

/* Process data. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless comment.

Comment thread modules/i2c/i2c.h Outdated
#include <avr/io.h>
#include <avarix/intlvl.h>
#include <stdint.h>
#include <stddef.h>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What requires this header?

Copy link
Copy Markdown
Member Author

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

Comment thread modules/i2c/i2cs.h Outdated

/* Transaction result enumeration */
typedef enum {
I2CS_RESULT_UNKNOWN = 0,

Copy link
Copy Markdown
Member

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?

Comment thread modules/i2c/i2cs.h Outdated
I2CS_RESULT_ABORTED
} i2c_result_t;

/*! \brief i2c slave driver struct.

Copy link
Copy Markdown
Member

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.

@benoitryder benoitryder left a comment

Copy link
Copy Markdown
Member

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).

Comment thread modules/clock/config/clock_config.h Outdated
#define CLOCK_PER2_FREQ CLOCK_CPU_FREQ
#define CLOCK_PER4_FREQ CLOCK_CPU_FREQ

/** @brief Activates automatic run-time calibration of internal clock

Copy link
Copy Markdown
Member

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.

Comment thread modules/i2c/i2cs.h Outdated
//@{
/**
* @file
* @brief I2Cs definitions

Copy link
Copy Markdown
Member

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.

Comment thread modules/i2c/i2cs.h Outdated
#include <stdint.h>
#include "i2c_config.h"

// Transaction status defines

Copy link
Copy Markdown
Member

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.)

Comment thread modules/i2c/i2cs.h Outdated
I2CS_RESULT_TRANSMIT_COLLISION,
I2CS_RESULT_BUS_ERROR,
I2CS_RESULT_FAIL,
I2CS_RESULT_ABORTED

Copy link
Copy Markdown
Member

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.

Comment thread modules/i2c/i2cs.h Outdated
I2CS_RESULT_ABORTED
} i2c_result_t;

/* @brief I2C slave driver struct.

Copy link
Copy Markdown
Member

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.)

Comment thread 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 &

Copy link
Copy Markdown
Member

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.

Comment thread 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));

Copy link
Copy Markdown
Member

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.

Comment thread modules/i2c/slavex.inc.c

#error Slave mode not supported yet
#define I2CX(s) X_(I2C,s)
#define i2cX(s) X_(i2c,s)

Copy link
Copy Markdown
Member

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.

Comment thread modules/i2c/slavex.inc.c Outdated
(current_status & TWI_SLAVE_AP_bm)) {
i2cX(_address_match_handler());
/* Process data. */
if(i2cX().process_data != NULL)

Copy link
Copy Markdown
Member

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.)

Comment thread modules/i2c/slavex.inc.h Outdated


/* I2cX Register callback */
void i2cX(s_register_callback)( void (*process_data_function)(void));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space after (.

Comment thread include/avarix.h Outdated
#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]))

Copy link
Copy Markdown
Member

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. ;)

Comment thread 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

Copy link
Copy Markdown
Member

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).

Comment thread modules/i2c/i2c.c Outdated
return i;
}

int8_t i2cs_send_async(i2cs_t *s, uint8_t *data, uint8_t n) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code mort.

Comment thread modules/i2c/i2c.h Outdated
} i2cs_state_t;

#ifndef I2CS_RECV_BUFFER_SIZE
#define I2CS_RECV_BUFFER_SIZE 32

Copy link
Copy Markdown
Member

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)

Comment thread modules/i2c/i2c.h Outdated
#define I2CS_SEND_BUFFER_SIZE 32
#endif

struct i2cs;

Copy link
Copy Markdown
Member

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.

Comment thread modules/i2c/slavex.inc.c Outdated
i2cs->sent_bytes = 0;

// ask user to provision send buffer
int rsz = i2cs->send_callback(i2cs->send_buffer,

Copy link
Copy Markdown
Member

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.

Comment thread 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);

Copy link
Copy Markdown
Member

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ôt I2CS_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.

Comment thread modules/i2c/slavex.inc.h Outdated
//@{
/**
* @file
* @brief I2C definitions

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I2C slave definitions

Comment thread modules/i2c/i2c.h

int sent_bytes;
int bytes_to_send;
uint8_t send_buffer[I2CS_SEND_BUFFER_SIZE];

Copy link
Copy Markdown
Member

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.

Comment thread modules/i2c/i2c.h Outdated

i2cs_state_t state;

int recvd_bytes;

Copy link
Copy Markdown
Member

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.

Comment thread modules/i2c/slavex.inc.c
// Interrupt handler
ISR(twiX(_TWIS_vect)) {

i2cs_t *i2cs = &i2cX();

Copy link
Copy Markdown
Member

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.

Comment thread modules/i2c/i2c.h
uint8_t send_buffer[I2CS_SEND_BUFFER_SIZE];

i2cs_recv_callback_t recv_callback;

Copy link
Copy Markdown
Member

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.

Comment thread modules/i2c/i2c.h

uint8_t sent_bytes;
uint8_t bytes_to_send;
uint8_t send_buffer[I2CS_SEND_BUFFER_SIZE];

Copy link
Copy Markdown
Member

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.)

Comment thread modules/i2c/slavex.inc.c
TWIX.SLAVE.ADDR = I2CX(_ADDRESS) << 1;
}

void i2cX(s_register_reset_callback)(i2cs_reset_callback_t f)

Copy link
Copy Markdown
Member

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.

Comment thread modules/i2c/slavex.inc.c
}

// Interrupt handler
ISR(twiX(_TWIS_vect))

Copy link
Copy Markdown
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants