Skip to content

Commit

Permalink
r82xx: avoid redundant register writes for speed
Browse files Browse the repository at this point in the history
  • Loading branch information
sultanqasim committed Feb 29, 2024
1 parent f5978e8 commit 0b64f07
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/tuner_r82xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>

#include "rtlsdr_i2c.h"
#include "tuner_r82xx.h"
Expand Down Expand Up @@ -243,6 +244,7 @@ static void shadow_store(struct r82xx_priv *priv, uint8_t reg, const uint8_t *va

if (r < 0) {
len += r;
val -= r;
r = 0;
}
if (len <= 0)
Expand All @@ -253,11 +255,29 @@ static void shadow_store(struct r82xx_priv *priv, uint8_t reg, const uint8_t *va
memcpy(&priv->regs[r], val, len);
}

static bool shadow_equal(struct r82xx_priv *priv, uint8_t reg, const uint8_t *val,
int len)
{
int r = reg - REG_SHADOW_START;

if (r < 0 || len < 0 || len > NUM_REGS - r)
return false;

if (memcmp(&priv->regs[r], val, len) == 0)
return true;

return false;
}

static int r82xx_write(struct r82xx_priv *priv, uint8_t reg, const uint8_t *val,
unsigned int len)
{
int rc, size, pos = 0;

/* Avoid setting registers unnecessarily since it's slow */
if (shadow_equal(priv, reg, val, len))
return 0;

/* Store the shadow registers */
shadow_store(priv, reg, val, len);

Expand Down Expand Up @@ -1320,6 +1340,7 @@ int r82xx_init(struct r82xx_priv *priv)
priv->xtal_cap_sel = XTAL_HIGH_CAP_0P;

/* Initialize registers */
memset(priv->regs, 0, NUM_REGS);
rc = r82xx_write(priv, 0x05,
r82xx_init_array, sizeof(r82xx_init_array));

Expand Down

0 comments on commit 0b64f07

Please sign in to comment.