I2C speed options

This commit is contained in:
whowechina
2025-11-27 14:11:49 +08:00
parent 942e5d000f
commit 9fc894b66a
6 changed files with 47 additions and 10 deletions
Binary file not shown.
+2 -1
View File
@@ -12,7 +12,8 @@
#define BUS_I2C i2c0
#define BUS_I2C_SDA 0
#define BUS_I2C_SCL 1
#define BUS_I2C_FREQ 266*1000
#define BUS_I2C_FREQ 166*1000
#define BUS_I2C_FAST_FREQ 400*1000
#define SPIN_DEF { 13, 12, 11, 10, 9 }
+29 -3
View File
@@ -27,11 +27,12 @@ static void disp_light()
static void disp_spin()
{
printf("[Spin]\n");
printf(" Fast I2C: %s.\n", musec_cfg->spin.fast_i2c ? "ON" : "OFF");
printf(" Units Per Turn: %d.\n", musec_cfg->spin.units_per_turn);
for (int i = 0; i < 5; i++) {
printf(" Spinner %d: %s, %s.\n", i + 1,
spin_present(i) ? "OK" : "ERROR",
musec_cfg->spin.reversed[i] ? "Reversed" : "Forward");
musec_cfg->spin.reversed & (1 << i) ? "Reversed" : "Forward");
}
}
@@ -137,15 +138,36 @@ static void handle_spin_invert(int id, const char *dir)
return;
}
musec_cfg->spin.reversed[id] = match;
if (match == 0) {
musec_cfg->spin.reversed &= ~(1 << id);
} else if (match == 1) {
musec_cfg->spin.reversed |= (1 << id);
}
config_changed();
disp_spin();
}
static void handle_i2c_option(const char *option)
{
const char *usage = "Usage: spin fast_i2c <on|off>\n";
const char *choices[] = {"off", "on"};
int match = cli_match_prefix(choices, count_of(choices), option);
if (match < 0) {
printf(usage);
return;
}
musec_cfg->spin.fast_i2c = match;
save_request(true); // immediate save
disp_spin();
printf("Note: Please reboot the device to apply I2C speed change.\n");
}
static void handle_spin(int argc, char *argv[])
{
const char *usage = "Usage: spin rate <units_per_turn>\n"
" spin fast_i2c <on|off>\n"
" spin <id> <normal|reverse>\n"
" units_per_turn: 20..255\n"
" id: 1..5\n";
@@ -154,7 +176,7 @@ static void handle_spin(int argc, char *argv[])
return;
}
const char *choices[] = { "1", "2", "3", "4", "5", "rate" };
const char *choices[] = { "1", "2", "3", "4", "5", "fast_i2c", "rate" };
int match = cli_match_prefix(choices, count_of(choices), argv[0]);
if (match < 0) {
printf(usage);
@@ -162,6 +184,10 @@ static void handle_spin(int argc, char *argv[])
}
if (match == 5) {
handle_i2c_option(argv[1]);
return;
}
if (match == 6) {
handle_spin_rate(argv[1]);
return;
}
+1
View File
@@ -13,6 +13,7 @@ musec_cfg_t *musec_cfg;
static musec_cfg_t default_cfg = {
.spin = {
.fast_i2c = false,
.units_per_turn = 80,
},
.light = {
+3 -1
View File
@@ -17,7 +17,9 @@ typedef struct {
typedef struct __attribute__((packed)) {
struct {
uint8_t units_per_turn;
uint8_t reversed[5];
bool fast_i2c;
uint8_t reversed;
uint8_t reserved[3];
} spin;
struct {
bool internal;
+12 -5
View File
@@ -31,7 +31,12 @@ void spin_init()
gpio_init(BUS_I2C_SCL);
gpio_set_function(BUS_I2C_SCL, GPIO_FUNC_I2C);
gpio_pull_up(BUS_I2C_SCL);
i2c_init(BUS_I2C, BUS_I2C_FREQ);
if (musec_cfg->spin.fast_i2c) {
i2c_init(BUS_I2C, BUS_I2C_FAST_FREQ);
} else {
i2c_init(BUS_I2C, BUS_I2C_FREQ);
}
for (int i = 0; i < SPIN_NUM; i++) {
uint8_t ce = spin_enablers[i];
@@ -66,19 +71,21 @@ static uint16_t spin_reading[SPIN_NUM];
void spin_update()
{
static bool flip = false;
flip = !flip;
static int count = 0;
for (int i = 0; i < SPIN_NUM; i++) {
if (flip ^ (i % 2)) {
if (!musec_cfg->spin.fast_i2c && (i != count)) {
continue;
}
tmag5273_use(i);
spin_reading[i] = tmag5273_read_angle();
if (musec_cfg->spin.reversed[i]) {
if (musec_cfg->spin.reversed & (1 << i)) {
spin_reading[i] = FULL_SCALE - spin_reading[i];
}
}
count = (count + 1) % SPIN_NUM;
}
uint16_t spin_read(uint8_t index)