feat: Radiomaster TX15 support (#6484)
Co-authored-by: Malte Langermann <54738901+gagarinlg@users.noreply.github.com> Co-authored-by: 3djc <3djc@gh.com> Co-authored-by: raphaelcoeffic <1050031+raphaelcoeffic@users.noreply.github.com> Co-authored-by: philmoz <phil.a.mitchell@gmail.com> Co-authored-by: Peter Feerick <peter.feerick@gmail.com>
This commit is contained in:
co-authored by
Malte Langermann
3djc
raphaelcoeffic
philmoz
Peter Feerick
parent
3a00c0812b
commit
324b2c7761
@@ -0,0 +1,182 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
/*
|
||||
WARNING: while this ICM42607C belong to the ICM426xx familly, it is NOT compatible
|
||||
with code for generic ICM4267XX. All register have different addresses
|
||||
*/
|
||||
|
||||
#include "edgetx.h"
|
||||
#include "hal/i2c_driver.h"
|
||||
#include "stm32_i2c_driver.h"
|
||||
#include "icm42607C.h"
|
||||
#include "imu_filter.h"
|
||||
#include "stm32_gpio.h"
|
||||
#include "inactivity_timer.h"
|
||||
#include "hal.h"
|
||||
|
||||
int16_t getGyroTemperature()
|
||||
{
|
||||
uint8_t reg = TEMP_DATA_X0_REG;
|
||||
uint8_t buf[2] = {0};
|
||||
|
||||
if (stm32_i2c_read(IMU_I2C_BUS, ICM426xx_I2C_ADDR, reg, 1, buf, 2, 1000) < 0) {
|
||||
TRACE("ICM426xx ERROR: TEMP_DATA_X0_REG i2c read error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (((buf[0] << 8) | buf[1]) / 128) + 25;
|
||||
}
|
||||
|
||||
int write_cmd(uint8_t reg, uint8_t val)
|
||||
{
|
||||
return i2c_write(IMU_I2C_BUS, ICM426xx_I2C_ADDR, reg, 1, &val, 1);
|
||||
}
|
||||
|
||||
#if defined(IMU_INT_GPIO)
|
||||
static void imu_exti_isr(void)
|
||||
{
|
||||
inactivityTimerReset(ActivitySource::MainControls);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int write_mreg1(uint8_t reg, uint8_t val) {
|
||||
if (write_cmd(BLK_SEL_W_REG, 0x00) < 0) return -1;
|
||||
if (write_cmd(MADDR_W_REG, reg) < 0) return -1;
|
||||
if (write_cmd(M_W_REG, val) < 0) return -1;
|
||||
delay_us(10); //42627-pdf p12“Accessing MREG1, MREG2 And MREG3 Registers
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gyroInit(void)
|
||||
{
|
||||
TRACE("ICM426xx I2C Init");
|
||||
|
||||
if (i2c_init(IMU_I2C_BUS) < 0) {
|
||||
TRACE("ICM426xx ERROR: i2c_init bus error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (i2c_dev_ready(IMU_I2C_BUS, ICM426xx_I2C_ADDR) < 0) {
|
||||
TRACE("ICM426xx device init error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint8_t data = 0;
|
||||
if (stm32_i2c_read(IMU_I2C_BUS, ICM426xx_I2C_ADDR, 0x75, 1, &data, 1, 1000) < 0) {
|
||||
TRACE("ICM426xx ERROR: i2c read error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (data != 0x61) {
|
||||
TRACE("ICM426xx ERROR: this code only works on ICM42607C");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(write_cmd(SIGNAL_PATH_RESET, SOFT_RESET_CMD) < 0) {
|
||||
TRACE("ICM426xx ERROR: SOFT_RESET_CMD error");
|
||||
return -1;
|
||||
}
|
||||
delay_ms(100);
|
||||
|
||||
if(write_cmd(PWR_MGMT0_REG, PWR_MGMT0_ENABLE) < 0) {
|
||||
TRACE("ICM426xx ERROR: PWR_MGMT0_REG error");
|
||||
return -1;
|
||||
}
|
||||
delay_ms(10);
|
||||
|
||||
// Set gyro to 2000 dps, 1kHz ODR
|
||||
if(write_cmd(GYRO_CONFIG0_REG, GYRO_ODR_1KHZ) < 0) {
|
||||
TRACE("ICM426xx ERROR: GYRO_CONFIG0_REG error");
|
||||
return -1;
|
||||
}
|
||||
delay_ms(1);
|
||||
|
||||
// Set accel to ±4g, 1kHz ODR
|
||||
if (write_cmd(ACCEL_CONFIG0_REG, ACCEL_ODR_1KHZ) < 0) {
|
||||
TRACE("ICM426xx ERROR: ACCEL_CONFIG0_REG error");
|
||||
return -1;
|
||||
}
|
||||
delay_ms(1);
|
||||
|
||||
|
||||
if (write_cmd(INT_CONFIG_REG, 0x00) < 0) {
|
||||
TRACE("ICM426xx ERROR: INT_CONFIG_REG error");
|
||||
return -1;
|
||||
}
|
||||
delay_ms(1);
|
||||
|
||||
#if defined(IMU_INT_GPIO)
|
||||
// CCEL_WOM_X_THR:0-255(0-1g),0x10--16mg
|
||||
if (write_mreg1(ACCEL_WOM_X_THR_REG, 0xFE) < 0) return -1;
|
||||
if (write_mreg1(ACCEL_WOM_Y_THR_REG, 0xFE) < 0) return -1;
|
||||
if (write_mreg1(ACCEL_WOM_Z_THR_REG, 0xFE) < 0) return -1;
|
||||
|
||||
delay_ms(1);
|
||||
// config WoM
|
||||
if (write_cmd(WOM_CONFIG_REG, 0x03) < 0) return -1; // OR mode
|
||||
delay_ms(1);
|
||||
// enable INT1
|
||||
if (write_cmd(INT_SOURCE1_REG, 0x07) < 0) return -1; // XYZ interrupt
|
||||
delay_ms(1);
|
||||
// config INT1 pin
|
||||
if (write_cmd(INT_CONFIG_REG, 0x00) < 0) return -1;
|
||||
|
||||
gpio_init_int(IMU_INT_GPIO, GPIO_IN_PU, GPIO_FALLING, imu_exti_isr);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gyroRead(uint8_t buffer[IMU_BUFFER_LENGTH])
|
||||
{
|
||||
IMU_RawData_t imu_raw;
|
||||
IMU_FilteredData_t *filtered;
|
||||
|
||||
uint8_t buf[6];
|
||||
uint8_t reg = GYRO_DATA_X0_REG;
|
||||
if (stm32_i2c_read(IMU_I2C_BUS, ICM426xx_I2C_ADDR, reg, 1, buf, 6, 1000) < 0) {
|
||||
TRACE("ICM426xx ERROR: i2c read error");
|
||||
return -1;
|
||||
}
|
||||
imu_raw.gyro_x = -((int16_t)(buf[0] << 8) | buf[1]); // x
|
||||
imu_raw.gyro_y = -((int16_t)(buf[2] << 8) | buf[3]); // y
|
||||
imu_raw.gyro_z = ((int16_t)(buf[4] << 8) | buf[5]); // z
|
||||
|
||||
reg = ACCEL_DATA_X0_REG;
|
||||
if (stm32_i2c_read(IMU_I2C_BUS, ICM426xx_I2C_ADDR, reg, 1, buf, 6, 1000) < 0) {
|
||||
TRACE("ICM426xx ERROR: i2c read error");
|
||||
return -1;
|
||||
}
|
||||
imu_raw.accel_x = ((int16_t)(buf[0] << 8) | buf[1]); // x
|
||||
imu_raw.accel_y = ((int16_t)(buf[2] << 8) | buf[3]); // y
|
||||
imu_raw.accel_z = ((int16_t)(buf[4] << 8) | buf[5]); // z
|
||||
|
||||
// Process through filters
|
||||
process_imu_data(&imu_raw);
|
||||
filtered = get_filtered_imu_data();
|
||||
|
||||
*(int16_t*)&buffer[6] = -(int16_t)filtered->accel_x; // x
|
||||
*(int16_t*)&buffer[8] = -(int16_t)filtered->accel_y; // y
|
||||
*(int16_t*)&buffer[10] = (int16_t)filtered->accel_z; // z
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
/*
|
||||
WARNING: while this ICM42607C belong to the ICM426xx familly, it is NOT compatible
|
||||
with code for generic ICM4267XX. All register have different addresses
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#define ICM426xx_I2C_ADDR 0x68
|
||||
|
||||
// Register addresses
|
||||
#define WHO_AM_I_REG 0x75
|
||||
#define PWR_MGMT0_REG 0x1F
|
||||
#define SIGNAL_PATH_RESET 0x02
|
||||
#define ACCEL_CONFIG0_REG 0x21
|
||||
#define GYRO_CONFIG0_REG 0x20
|
||||
#define INT_CONFIG_REG 0x06
|
||||
#define TEMP_DATA_X0_REG 0x09
|
||||
|
||||
// Config values
|
||||
#define SOFT_RESET_CMD 0x10
|
||||
|
||||
#define PWR_MGMT0_ENABLE 0x0F // ACCEL_LNM + GYRO_LNM
|
||||
#define ACCEL_ODR_1KHZ 0x45 // Accel 1 kHz ±4g
|
||||
#define GYRO_ODR_1KHZ 0x05 // Gyro 1 kHz ±2000 dps
|
||||
|
||||
#define GYRO_DATA_X0_REG 0x11
|
||||
#define ACCEL_DATA_X0_REG 0x0B
|
||||
|
||||
#define BLK_SEL_W_REG 0x79
|
||||
#define MADDR_W_REG 0x7A
|
||||
#define M_W_REG 0x7B
|
||||
#define ACCEL_WOM_X_THR_REG 0x4B
|
||||
#define ACCEL_WOM_Y_THR_REG 0x4C
|
||||
#define ACCEL_WOM_Z_THR_REG 0x4D
|
||||
#define WOM_CONFIG_REG 0x27
|
||||
#define INT_SOURCE1_REG 0x2C
|
||||
#define INT_CONFIG_REG 0x06
|
||||
#define INT_STATUS2_REG 0x3B
|
||||
|
||||
typedef struct {
|
||||
float fTemperatureDegC; //°C
|
||||
float fAccX, fAccY, fAccZ; // m/s^2
|
||||
float fGyroXradps, fGyroYradps, fGyroZradps; // rad/s
|
||||
} sIMUoutput;
|
||||
|
||||
extern sIMUoutput IMUoutput;
|
||||
|
||||
int gyroInit(void);
|
||||
int gyroRead(unsigned char*);
|
||||
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#include "edgetx.h"
|
||||
#include "imu_filter.h"
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
// Filter parameters
|
||||
#define COMPLEMENTARY_ALPHA 0.92f // Complementary filter coefficient
|
||||
#define LP_FILTER_ALPHA 0.9f // Low-pass filter coefficient
|
||||
#define SAMPLE_TIME_S 0.005f // Gyro is read every 5ms as part of execMixerFrequentActions()
|
||||
#define RAD_TO_DEG 57.2958f
|
||||
#define DEG_TO_RAD 0.0174533f
|
||||
|
||||
// Filter state variables
|
||||
static IMU_FilteredData_t filtered_data = {0};
|
||||
static float prev_accel[3] = {0};
|
||||
static float prev_gyro[3] = {0};
|
||||
static uint8_t filter_initialized = 0;
|
||||
|
||||
// Low-pass filter implementation
|
||||
float low_pass_filter(float new_value, float prev_value, float alpha) {
|
||||
return alpha * prev_value + (1.0f - alpha) * new_value;
|
||||
}
|
||||
|
||||
// Calculate roll and pitch from accelerometer data
|
||||
void calculate_accel_angles(float ax, float ay, float az, float *roll, float *pitch) {
|
||||
// Normalize accelerometer data
|
||||
float norm = sqrtf(ax*ax + ay*ay + az*az);
|
||||
if (norm == 0.0f) return;
|
||||
|
||||
ax /= norm;
|
||||
ay /= norm;
|
||||
az /= norm;
|
||||
|
||||
// Calculate angles
|
||||
*roll = atan2f(ay, sqrtf(ax*ax + az*az));
|
||||
*pitch = atan2f(-ax, sqrtf(ay*ay + az*az));
|
||||
}
|
||||
|
||||
// Complementary filter for attitude estimation
|
||||
void complementary_filter(IMU_RawData_t *raw_data, float dt) {
|
||||
float accel_roll, accel_pitch;
|
||||
|
||||
// Calculate angles from accelerometer
|
||||
calculate_accel_angles(raw_data->accel_x, raw_data->accel_y, raw_data->accel_z,
|
||||
&accel_roll, &accel_pitch);
|
||||
|
||||
if (!filter_initialized) {
|
||||
// Initialize filter with accelerometer angles
|
||||
filtered_data.roll = accel_roll;
|
||||
filtered_data.pitch = accel_pitch;
|
||||
filtered_data.yaw = 0.0f;
|
||||
filter_initialized = 1;
|
||||
} else {
|
||||
// Integrate gyroscope data
|
||||
float gyro_roll = filtered_data.roll + raw_data->gyro_x * dt;
|
||||
float gyro_pitch = filtered_data.pitch + raw_data->gyro_y * dt;
|
||||
float gyro_yaw = filtered_data.yaw + raw_data->gyro_z * dt;
|
||||
|
||||
// Apply complementary filter
|
||||
filtered_data.roll = COMPLEMENTARY_ALPHA * gyro_roll +
|
||||
(1.0f - COMPLEMENTARY_ALPHA) * accel_roll;
|
||||
filtered_data.pitch = COMPLEMENTARY_ALPHA * gyro_pitch +
|
||||
(1.0f - COMPLEMENTARY_ALPHA) * accel_pitch;
|
||||
filtered_data.yaw = gyro_yaw; // No magnetometer correction in this example
|
||||
|
||||
// Keep yaw within -π to π range
|
||||
while (filtered_data.yaw > M_PI) filtered_data.yaw -= 2.0f * M_PI;
|
||||
while (filtered_data.yaw < -M_PI) filtered_data.yaw += 2.0f * M_PI;
|
||||
}
|
||||
}
|
||||
|
||||
// Apply low-pass filter to raw sensor data
|
||||
void apply_lowpass_filter(IMU_RawData_t *raw_data) {
|
||||
// Filter accelerometer data
|
||||
filtered_data.accel_x = low_pass_filter(raw_data->accel_x, prev_accel[0], LP_FILTER_ALPHA);
|
||||
filtered_data.accel_y = low_pass_filter(raw_data->accel_y, prev_accel[1], LP_FILTER_ALPHA);
|
||||
filtered_data.accel_z = low_pass_filter(raw_data->accel_z, prev_accel[2], LP_FILTER_ALPHA);
|
||||
|
||||
// Filter gyroscope data
|
||||
filtered_data.gyro_x = low_pass_filter(raw_data->gyro_x, prev_gyro[0], LP_FILTER_ALPHA);
|
||||
filtered_data.gyro_y = low_pass_filter(raw_data->gyro_y, prev_gyro[1], LP_FILTER_ALPHA);
|
||||
filtered_data.gyro_z = low_pass_filter(raw_data->gyro_z, prev_gyro[2], LP_FILTER_ALPHA);
|
||||
|
||||
// Update previous values
|
||||
prev_accel[0] = filtered_data.accel_x;
|
||||
prev_accel[1] = filtered_data.accel_y;
|
||||
prev_accel[2] = filtered_data.accel_z;
|
||||
|
||||
prev_gyro[0] = filtered_data.gyro_x;
|
||||
prev_gyro[1] = filtered_data.gyro_y;
|
||||
prev_gyro[2] = filtered_data.gyro_z;
|
||||
}
|
||||
|
||||
// Main IMU filter processing function
|
||||
void process_imu_data(IMU_RawData_t *raw_data) {
|
||||
// Apply low-pass filtering to raw data
|
||||
apply_lowpass_filter(raw_data);
|
||||
|
||||
// Apply complementary filter for attitude estimation
|
||||
complementary_filter(raw_data, SAMPLE_TIME_S);
|
||||
}
|
||||
|
||||
// Get filtered IMU data
|
||||
IMU_FilteredData_t* get_filtered_imu_data(void) {
|
||||
return &filtered_data;
|
||||
}
|
||||
|
||||
// Reset filter state
|
||||
void reset_imu_filter(void) {
|
||||
memset(&filtered_data, 0, sizeof(filtered_data));
|
||||
memset(prev_accel, 0, sizeof(prev_accel));
|
||||
memset(prev_gyro, 0, sizeof(prev_gyro));
|
||||
filter_initialized = 0;
|
||||
}
|
||||
|
||||
// Advanced Kalman filter structure (optional implementation)
|
||||
typedef struct {
|
||||
float Q_angle; // Process noise variance for accelerometer
|
||||
float Q_bias; // Process noise variance for gyro bias
|
||||
float R_measure; // Measurement noise variance
|
||||
float angle; // Estimated angle
|
||||
float bias; // Estimated bias
|
||||
float rate; // Unbiased rate
|
||||
float P[2][2]; // Error covariance matrix
|
||||
} kalman_state_t;
|
||||
|
||||
// Kalman filter implementation (more advanced option)
|
||||
float kalman_filter(kalman_state_t *state, float new_angle, float new_rate, float dt) {
|
||||
// Prediction step
|
||||
state->rate = new_rate - state->bias;
|
||||
state->angle += dt * state->rate;
|
||||
|
||||
// Update error covariance
|
||||
state->P[0][0] += dt * (dt * state->P[1][1] - state->P[0][1] - state->P[1][0] + state->Q_angle);
|
||||
state->P[0][1] -= dt * state->P[1][1];
|
||||
state->P[1][0] -= dt * state->P[1][1];
|
||||
state->P[1][1] += state->Q_bias * dt;
|
||||
|
||||
// Innovation
|
||||
float S = state->P[0][0] + state->R_measure;
|
||||
float K[2] = {state->P[0][0] / S, state->P[1][0] / S};
|
||||
|
||||
float y = new_angle - state->angle;
|
||||
|
||||
// Update step
|
||||
state->angle += K[0] * y;
|
||||
state->bias += K[1] * y;
|
||||
|
||||
// Update error covariance
|
||||
float P00_temp = state->P[0][0];
|
||||
float P01_temp = state->P[0][1];
|
||||
|
||||
state->P[0][0] -= K[0] * P00_temp;
|
||||
state->P[0][1] -= K[0] * P01_temp;
|
||||
state->P[1][0] -= K[1] * P00_temp;
|
||||
state->P[1][1] -= K[1] * P01_temp;
|
||||
|
||||
return state->angle;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
// IMU data structure
|
||||
typedef struct {
|
||||
float accel_x, accel_y, accel_z; // Accelerometer data (m/s²)
|
||||
float gyro_x, gyro_y, gyro_z; // Gyroscope data (rad/s)
|
||||
float mag_x, mag_y, mag_z; // Magnetometer data (optional)
|
||||
} IMU_RawData_t;
|
||||
|
||||
typedef struct {
|
||||
float roll, pitch, yaw; // Filtered attitude (radians)
|
||||
float accel_x, accel_y, accel_z; // Filtered acceleration
|
||||
float gyro_x, gyro_y, gyro_z; // Filtered gyroscope
|
||||
} IMU_FilteredData_t;
|
||||
|
||||
void process_imu_data(IMU_RawData_t *raw_data);
|
||||
IMU_FilteredData_t* get_filtered_imu_data(void);
|
||||
@@ -20,6 +20,7 @@
|
||||
*/
|
||||
|
||||
#include "pca95xx.h"
|
||||
#include "debug.h"
|
||||
|
||||
#define REG_INPUT 0x00
|
||||
#define REG_OUTPUT 0x02
|
||||
@@ -46,10 +47,12 @@ int pca95xx_init(pca95xx_t* dev, etx_i2c_bus_t bus, uint16_t addr)
|
||||
dev->output = 0;
|
||||
|
||||
if (i2c_init(bus) < 0) {
|
||||
TRACE("PCA95 bus init error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (i2c_dev_ready(bus, addr) < 0) {
|
||||
TRACE("PCA95 device init error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,11 @@
|
||||
|
||||
#include "hal/i2c_driver.h"
|
||||
|
||||
/* WARNING
|
||||
Ti PCA95 chip do NOT number pin sequentially, it goes PIN0 to PIN7 then PIN10 to PIN17.
|
||||
So when schematic indicate P10, it is PIN_8 in the following table
|
||||
*/
|
||||
|
||||
#define PCA95XX_PIN_0 (1 << 0)
|
||||
#define PCA95XX_PIN_1 (1 << 1)
|
||||
#define PCA95XX_PIN_2 (1 << 2)
|
||||
@@ -40,6 +45,23 @@
|
||||
#define PCA95XX_PIN_14 (1 << 14)
|
||||
#define PCA95XX_PIN_15 (1 << 15)
|
||||
|
||||
#define PCA95XX_P0 PCA95XX_PIN_0
|
||||
#define PCA95XX_P1 PCA95XX_PIN_1
|
||||
#define PCA95XX_P2 PCA95XX_PIN_2
|
||||
#define PCA95XX_P3 PCA95XX_PIN_3
|
||||
#define PCA95XX_P4 PCA95XX_PIN_4
|
||||
#define PCA95XX_P5 PCA95XX_PIN_5
|
||||
#define PCA95XX_P6 PCA95XX_PIN_6
|
||||
#define PCA95XX_P7 PCA95XX_PIN_7
|
||||
#define PCA95XX_P10 PCA95XX_PIN_8
|
||||
#define PCA95XX_P11 PCA95XX_PIN_9
|
||||
#define PCA95XX_P12 PCA95XX_PIN_10
|
||||
#define PCA95XX_P13 PCA95XX_PIN_11
|
||||
#define PCA95XX_P14 PCA95XX_PIN_12
|
||||
#define PCA95XX_P15 PCA95XX_PIN_13
|
||||
#define PCA95XX_P16 PCA95XX_PIN_14
|
||||
#define PCA95XX_P17 PCA95XX_PIN_15
|
||||
|
||||
typedef struct {
|
||||
etx_i2c_bus_t bus;
|
||||
uint16_t addr;
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "tas2505.h"
|
||||
#include "stm32_i2s.h"
|
||||
#include "timers_driver.h"
|
||||
#include "hal/audio_driver.h"
|
||||
|
||||
#include "debug.h"
|
||||
|
||||
@@ -126,3 +127,25 @@ int tas2505_init(tas2505_t* dev)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// adjust this value for a volume level just above the silence
|
||||
// values is attenuation in dB, higher value - less volume
|
||||
// max value is 126
|
||||
#define VOLUME_MIN_DB 60
|
||||
|
||||
void tas2505_set_volume(tas2505_t* dev, uint8_t volume)
|
||||
{
|
||||
if (volume > VOLUME_LEVEL_MAX) {
|
||||
volume = VOLUME_LEVEL_MAX;
|
||||
}
|
||||
// maximum volume is 0x00 and total silence is 0xFE
|
||||
if (volume == 0) {
|
||||
tas2505_write_reg(dev, TAS2505_SPKVOL1, 0xFE);
|
||||
tas2505_write_reg(dev, TAS2505_HP_VOL, 0xFE);
|
||||
} else {
|
||||
uint32_t vol = (VOLUME_MIN_DB * 2) -
|
||||
((uint32_t)volume * (VOLUME_MIN_DB * 2)) / VOLUME_LEVEL_MAX;
|
||||
tas2505_write_reg(dev, TAS2505_SPKVOL1, vol);
|
||||
tas2505_write_reg(dev, TAS2505_HP_VOL, vol);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,3 +94,4 @@ typedef struct {
|
||||
|
||||
int tas2505_probe(tas2505_t* dev, etx_i2c_bus_t bus, uint16_t addr);
|
||||
int tas2505_init(tas2505_t* dev);
|
||||
void tas2505_set_volume(tas2505_t* dev, uint8_t volume);
|
||||
|
||||
@@ -477,7 +477,7 @@ int uf2_fat_write_block(uint32_t block_no, uint8_t *data)
|
||||
wr_st->written_mask[pos] |= mask;
|
||||
wr_st->num_written++;
|
||||
TRACE_DEBUG("[UF2] wr #%d (%d / %d)\n", bl->blockNo,
|
||||
state->num_written, bl->numBlocks);
|
||||
wr_st->num_written, bl->numBlocks);
|
||||
}
|
||||
if (wr_st->num_written >= wr_st->num_blocks) {
|
||||
TRACE_DEBUG("[UF2] done: reboot\n");
|
||||
|
||||
Reference in New Issue
Block a user