1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-26 15:03:53 +01:00

cpu/sam0_common: i2c: improve readability of baud rate calculation

Use variables to represent fSCL an fGCLK to make the baud rate calculation
more readable.
This commit is contained in:
Benjamin Valentin 2020-06-10 15:03:00 +02:00
parent 2fb0d9061f
commit 4df36cbfda

View File

@ -80,6 +80,9 @@ void i2c_init(i2c_t dev)
assert(dev < I2C_NUMOF);
const uint32_t fSCL = i2c_config[dev].speed;
const uint32_t fGCLK = sam0_gclk_freq(i2c_config[dev].gclk_src);
/* Initialize mutex */
mutex_init(&locks[dev]);
/* DISABLE I2C MASTER */
@ -123,9 +126,9 @@ void i2c_init(i2c_t dev)
bus(dev)->CTRLB.reg = SERCOM_I2CM_CTRLB_SMEN;
/* Set SPEED */
if (i2c_config[dev].speed > I2C_SPEED_FAST_PLUS) {
if (fSCL > I2C_SPEED_FAST_PLUS) {
bus(dev)->CTRLA.reg |= SERCOM_I2CM_CTRLA_SPEED(2);
} else if (i2c_config[dev].speed > I2C_SPEED_FAST) {
} else if (fSCL > I2C_SPEED_FAST) {
bus(dev)->CTRLA.reg |= SERCOM_I2CM_CTRLA_SPEED(1);
} else {
bus(dev)->CTRLA.reg |= SERCOM_I2CM_CTRLA_SPEED(0);
@ -134,15 +137,14 @@ void i2c_init(i2c_t dev)
/* Get the baudrate */
/* fSCL = fGCLK / (10 + 2 * BAUD) -> BAUD = fGCLK / (2 * fSCL) - 5 */
/* fSCL = fGCLK / (2 + 2 * HSBAUD) -> HSBAUD = fGCLK / (2 * fSCL) - 1 */
tmp_baud = (((sam0_gclk_freq(i2c_config[dev].gclk_src) +
(2 * (i2c_config[dev].speed)) - 1) / /* round up */
(2 * (i2c_config[dev].speed))) -
(i2c_config[dev].speed > I2C_SPEED_FAST_PLUS ? 1 : 5));
tmp_baud = (fGCLK + (2 * fSCL) - 1) /* round up */
/ (2 * fSCL)
- (fSCL > I2C_SPEED_FAST_PLUS ? 1 : 5);
/* Ensure baudrate is within limits */
assert(tmp_baud < 255 && tmp_baud > 0);
if (i2c_config[dev].speed > I2C_SPEED_FAST_PLUS) {
if (fSCL > I2C_SPEED_FAST_PLUS) {
bus(dev)->BAUD.reg = SERCOM_I2CM_BAUD_HSBAUD(tmp_baud);
} else {
bus(dev)->BAUD.reg = SERCOM_I2CM_BAUD_BAUD(tmp_baud);