Skip to content

Conversation

AugustinBoisvert
Copy link
Member

No description provided.

Copy link
Member

@benoitryder benoitryder left a 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).

@@ -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
Copy link
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

@@ -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;
Copy link
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
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

@@ -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"
Copy link
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.

//@{
/**
* @file
* @brief I2C definitions
Copy link
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).

#include "i2c_config.h"

/* Transaction status defines.*/
#define I2CS_STATUS_READY 0
Copy link
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.

*/
void i2cX(_receive_handler(void)) {
/* Enable stop interrupt. */
uint8_t currentCtrlA = TWIX.SLAVE.CTRLA;
Copy link
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.

uint8_t data = TWIX.SLAVE.DATA;
i2csX->received_data[i2csX->bytes_received] = data;

/* Process data. */
Copy link
Member

Choose a reason for hiding this comment

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

Useless comment.

@@ -12,7 +12,9 @@
#include <avr/io.h>
#include <avarix/intlvl.h>
#include <stdint.h>
#include <stddef.h>
Copy link
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
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


/* Transaction result enumeration */
typedef enum {
I2CS_RESULT_UNKNOWN = 0,
Copy link
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?

I2CS_RESULT_ABORTED
} i2c_result_t;

/*! \brief i2c slave driver struct.
Copy link
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.

Copy link
Member

@benoitryder benoitryder left a 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).

@@ -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
Copy link
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.

//@{
/**
* @file
* @brief I2Cs definitions
Copy link
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.

#include <stdint.h>
#include "i2c_config.h"

// Transaction status defines
Copy link
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.)

I2CS_RESULT_TRANSMIT_COLLISION,
I2CS_RESULT_BUS_ERROR,
I2CS_RESULT_FAIL,
I2CS_RESULT_ABORTED
Copy link
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.

I2CS_RESULT_ABORTED
} i2c_result_t;

/* @brief I2C slave driver struct.
Copy link
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.)

*/
static void i2cX(_transmit_handler(void)) {
/* If NACK, slave transmit transaction finished. */
if ((i2cX().bytes_transmit > 0) && (TWIX.SLAVE.STATUS &
Copy link
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.

/* If buffer overflow. */
else {
TWIX.SLAVE.CTRLB = TWI_SLAVE_CMD_COMPTRANS_gc;
i2cX(_transaction_finished(I2CS_RESULT_BUFFER_OVERFLOW));
Copy link
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.


#error Slave mode not supported yet
#define I2CX(s) X_(I2C,s)
#define i2cX(s) X_(i2c,s)
Copy link
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.

(current_status & TWI_SLAVE_AP_bm)) {
i2cX(_address_match_handler());
/* Process data. */
if(i2cX().process_data != NULL)
Copy link
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.)



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

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]))
Copy link
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. ;)

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

@@ -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) {
Copy link
Member

Choose a reason for hiding this comment

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

Code mort.

} i2cs_state_t;

#ifndef I2CS_RECV_BUFFER_SIZE
#define I2CS_RECV_BUFFER_SIZE 32
Copy link
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)

#define I2CS_SEND_BUFFER_SIZE 32
#endif

struct i2cs;
Copy link
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.

i2cs->sent_bytes = 0;

// ask user to provision send buffer
int rsz = i2cs->send_callback(i2cs->send_buffer,
Copy link
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.

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

//@{
/**
* @file
* @brief I2C definitions
Copy link
Member

Choose a reason for hiding this comment

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

I2C slave definitions


int sent_bytes;
int bytes_to_send;
uint8_t send_buffer[I2CS_SEND_BUFFER_SIZE];
Copy link
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.


i2cs_state_t state;

int recvd_bytes;
Copy link
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.

// Interrupt handler
ISR(twiX(_TWIS_vect)) {

i2cs_t *i2cs = &i2cX();
Copy link
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.

uint8_t send_buffer[I2CS_SEND_BUFFER_SIZE];

i2cs_recv_callback_t recv_callback;

Copy link
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.


uint8_t sent_bytes;
uint8_t bytes_to_send;
uint8_t send_buffer[I2CS_SEND_BUFFER_SIZE];
Copy link
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.)

TWIX.SLAVE.ADDR = I2CX(_ADDRESS) << 1;
}

void i2cX(s_register_reset_callback)(i2cs_reset_callback_t f)
Copy link
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.

}

// Interrupt handler
ISR(twiX(_TWIS_vect))
Copy link
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