Skip to content

Commit

Permalink
Protobuf rpc component (#2)
Browse files Browse the repository at this point in the history
- base of protobuf rpc
- method of pb ppc
- refactor sub_bus_init with no global val
  • Loading branch information
sfxfs authored Dec 22, 2024
1 parent 491e101 commit 0e5e14b
Show file tree
Hide file tree
Showing 26 changed files with 660 additions and 404 deletions.
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
- [x] pca9685 驱动
- [x] Dshot 驱动
- [ ] pca9685 测试
- [ ] dshot 测试
- [x] dshot 测试
- [ ] protobuf rpc 相关方法实现

### 额外

Expand Down
32 changes: 16 additions & 16 deletions components/aht30/aht30_intf.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,15 @@
* </table>
*/

#include "driver/i2c_master.h" // esp_driver_i2c
#include "sdkconfig.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "aht30_intf.h"

#if CONFIG_SUB_ENABLE_AHT30
#include "esp_log.h"
#include "driver/i2c_master.h" // esp_driver_i2c

#if (CONFIG_SUB_AHT30_IIC_PORT == 0) && CONFIG_SUB_ENABLE_I2C0
extern i2c_master_bus_handle_t i2c0_bus_handle;
#define AHT30_I2C_BUS i2c0_bus_handle
#elif (CONFIG_SUB_AHT30_IIC_PORT == 1) && CONFIG_SUB_ENABLE_I2C0
extern i2c_master_bus_handle_t i2c1_bus_handle;
#define AHT30_I2C_BUS i2c1_bus_handle
#else
#error certain i2c num not found or disabled
#endif
#include "sdkconfig.h"

#endif
#include "aht30_intf.h"

i2c_master_dev_handle_t aht30_i2c_dev_handle;

Expand All @@ -68,6 +56,18 @@ i2c_master_dev_handle_t aht30_i2c_dev_handle;
uint8_t aht30_interface_iic_init(void)
{
#if CONFIG_SUB_ENABLE_AHT30
esp_err_t handle_ret;
i2c_master_bus_handle_t aht30_i2c_handle;
#if (CONFIG_SUB_AHT30_IIC_PORT == 0) && CONFIG_SUB_ENABLE_I2C0
handle_ret = i2c_master_get_bus_handle(0, &aht30_i2c_handle);
#elif (CONFIG_SUB_AHT30_IIC_PORT == 1) && CONFIG_SUB_ENABLE_I2C0
handle_ret = i2c_master_get_bus_handle(1, &aht30_i2c_handle);
#else
#error certain i2c num not found or disabled
#endif
if (ESP_OK != handle_ret)
return 1; // interface not init

i2c_device_config_t i2c_dev_conf = {
.device_address = CONFIG_SUB_AHT30_IIC_ADDRESS,
.scl_speed_hz = CONFIG_SUB_AHT30_IIC_FREQUENCY,
Expand Down
2 changes: 1 addition & 1 deletion components/dshot/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ menu "SUB:DSHOT"

config SUB_DSHOT_RESOLUTION
int "Resolution of Dshot"
default 32000000
default 40000000
help
Resolution of DSHOT, 32000000 is 32MHz.

Expand Down
16 changes: 15 additions & 1 deletion components/dshot/dshot.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include "driver/rmt_tx.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "esp_log.h"
#include "esp_check.h"
#include "driver/rmt_tx.h"

#include "sdkconfig.h"

#include "dshot.h"
Expand Down Expand Up @@ -216,6 +218,18 @@ int rmt_dshot_init(dshot_handle_t *handle, gpio_num_t gpio_num)
return -1;
}

rmt_transmit_config_t tx_config = {
.loop_count = -1, // infinite loop
};
dshot_esc_throttle_t throttle = {
.throttle = 0,
.telemetry_req = false, // telemetry is not supported in this example
};
if (ESP_OK != rmt_transmit(handle->channel, handle->encoder, &throttle, sizeof(throttle), &tx_config))
{
return -1;
}

return 0;
}

Expand Down
4 changes: 2 additions & 2 deletions components/pca9685/include/pca9685_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ extern "C"{
#define PCA9685_APP_DEFAULT_SUBADDRESS_1 0xE2 /**< 0xE2 */
#define PCA9685_APP_DEFAULT_SUBADDRESS_2 0xE4 /**< 0xE4 */
#define PCA9685_APP_DEFAULT_SUBADDRESS_3 0xE8 /**< 0xE8 */
#define PCA9685_APP_DEFAULT_OSCILLATOR_FREQUENCY 50000000 /**< 25MHz */
#define PCA9685_APP_DEFAULT_OSCILLATOR_FREQUENCY 50000000 /**< 50MHz */

/**
* @brief basic example init
Expand Down Expand Up @@ -99,7 +99,7 @@ uint8_t pca9685_app_deinit(void);
* 0.0 <= delay_percent <= 100.0
* 0.0 <= high_duty_cycle_percent <= 100.0
*/
uint8_t pca9685_app_write(pca9685_channel_t channel, float delay_percent, float high_duty_cycle_percent);
uint8_t pca9685_app_write(pca9685_channel_t channel, uint16_t high_duty_cycle_us);

/**
* @}
Expand Down
14 changes: 11 additions & 3 deletions components/pca9685/pca9685_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "pca9685_app.h"

static pca9685_handle_t gs_handle; /**< pca9685 handle */
static uint16_t g_output_freq_hz;

/**
* @brief basic example init
Expand Down Expand Up @@ -111,6 +112,7 @@ uint8_t pca9685_app_init(pca9685_address_t addr, uint16_t hz)

return 1;
}
g_output_freq_hz = hz;

/* set pre scale */
res = pca9685_set_prescaler(&gs_handle, reg);
Expand Down Expand Up @@ -327,13 +329,19 @@ uint8_t pca9685_app_deinit(void)
* 0.0 <= delay_percent <= 100.0
* 0.0 <= high_duty_cycle_percent <= 100.0
*/
uint8_t pca9685_app_write(pca9685_channel_t channel, float delay_percent, float high_duty_cycle_percent)
uint8_t pca9685_app_write(pca9685_channel_t channel, uint16_t high_duty_cycle_us)
{
uint8_t res;
uint16_t on_count, off_count;


if (g_output_freq_hz == 0)
return 1;
float high_duty_cycle_percent = 1.f / (float)g_output_freq_hz; // 周期 in s
high_duty_cycle_percent *= 1000.f * 1000.f; // s to us
high_duty_cycle_percent = (float)high_duty_cycle_us / high_duty_cycle_percent; // final val

/* convert data */
res = pca9685_pwm_convert_to_register(&gs_handle, delay_percent, high_duty_cycle_percent, (uint16_t *)&on_count, (uint16_t *)&off_count);
res = pca9685_pwm_convert_to_register(&gs_handle, 0.f, high_duty_cycle_percent, (uint16_t *)&on_count, (uint16_t *)&off_count);
if (res != 0)
{
return 1;
Expand Down
36 changes: 18 additions & 18 deletions components/pca9685/pca9685_intf.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,16 @@
* </table>
*/

#include "driver/i2c_master.h" // esp_driver_i2c
#include "driver/gpio.h"
#include "sdkconfig.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "pca9685_intf.h"

#if CONFIG_SUB_ENABLE_PCA9685
#include "esp_log.h"
#include "driver/gpio.h"
#include "driver/i2c_master.h"

#if (CONFIG_SUB_PCA9685_IIC_PORT == 0) && CONFIG_SUB_ENABLE_I2C0
extern i2c_master_bus_handle_t i2c0_bus_handle;
#define PCA9685_I2C_BUS i2c0_bus_handle
#elif (CONFIG_SUB_PCA9685_IIC_PORT == 1) && CONFIG_SUB_ENABLE_I2C1
extern i2c_master_bus_handle_t i2c1_bus_handle;
#define PCA9685_I2C_BUS i2c1_bus_handle
#else
#error certain i2c num not found or disabled
#endif
#include "sdkconfig.h"

#endif
#include "pca9685_intf.h"

i2c_master_dev_handle_t pca9685_i2c_dev_handle;

Expand All @@ -69,11 +57,23 @@ i2c_master_dev_handle_t pca9685_i2c_dev_handle;
uint8_t pca9685_interface_iic_init(void)
{
#if CONFIG_SUB_ENABLE_PCA9685
esp_err_t handle_ret;
i2c_master_bus_handle_t pca9685_i2c_handle;
#if (CONFIG_SUB_PCA9685_IIC_PORT == 0) && CONFIG_SUB_ENABLE_I2C0
handle_ret = i2c_master_get_bus_handle(0, &pca9685_i2c_handle);
#elif (CONFIG_SUB_PCA9685_IIC_PORT == 1) && CONFIG_SUB_ENABLE_I2C1
handle_ret = i2c_master_get_bus_handle(1, &pca9685_i2c_handle);
#else
#error certain i2c num not found or disabled
#endif
if (ESP_OK != handle_ret)
return 1; // interface not init

i2c_device_config_t i2c_dev_conf = {
.device_address = CONFIG_SUB_PCA9685_IIC_ADDRESS,
.scl_speed_hz = CONFIG_SUB_PCA9685_IIC_FREQUENCY,
};
if (ESP_OK == i2c_master_bus_add_device(PCA9685_I2C_BUS, &i2c_dev_conf, &pca9685_i2c_dev_handle))
if (ESP_OK == i2c_master_bus_add_device(pca9685_i2c_handle, &i2c_dev_conf, &pca9685_i2c_dev_handle))
return 0;
else
return 1; // NO_MEM
Expand Down
4 changes: 4 additions & 0 deletions components/protobuf_rpc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
idf_component_register(SRCS "pb_rpc.c" "message_cmd.c" "navi_master.pb.c"
INCLUDE_DIRS "include"
PRIV_REQUIRES esp_driver_uart dshot pca9685
REQUIRES nanopb)
92 changes: 92 additions & 0 deletions components/protobuf_rpc/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
menu "SUB:ProtoBuf-RPC"
menu "Interface"
config SUB_PROTOBUF_UART_PORT
int "Which UARTx will be used"
default 0
help
The interface that used to communicate with sub-navi.

config SUB_PROTOBUF_UART_BAUDRATE
int "Set UART baudrate"
default 115200
help
The baudrate of UARTx.

config SUB_PROTOBUF_UART_QUEUE_SIZE
int "Set UART queue size"
default 20
help
The queue size of UARTx.

config SUB_PROTOBUF_UART_CUSTOM_PINS
bool "Use custom pins"
default n
help
If you want to use custom pins for UARTx, enable this option.

if SUB_PROTOBUF_UART_CUSTOM_PINS
config SUB_PROTOBUF_UART_TX_PIN
int "UARTx TX pin"
default 17
help
The TX pin of UARTx.

config SUB_PROTOBUF_UART_RX_PIN
int "UARTx RX pin"
default 16
help
The RX pin of UARTx.
endif
endmenu

menu "Thruster"
config SUB_PROTOBUF_THRUSTER0_PIN
int "Thruster0 pin"
default 18
help
The pin of thruster channel 0.

config SUB_PROTOBUF_THRUSTER1_PIN
int "Thruster1 pin"
default 19
help
The pin of thruster channel 1.

config SUB_PROTOBUF_THRUSTER2_PIN
int "Thruster2 pin"
default 20
help
The pin of thruster channel 2.

config SUB_PROTOBUF_THRUSTER3_PIN
int "Thruster3 pin"
default 21
help
The pin of thruster channel 3.

config SUB_PROTOBUF_THRUSTER4_PIN
int "Thruster4 pin"
default 22
help
The pin of thruster channel 4.

config SUB_PROTOBUF_THRUSTER5_PIN
int "Thruster5 pin"
default 23
help
The pin of thruster channel 5.

config SUB_PROTOBUF_THRUSTER6_PIN
int "Thruster6 pin"
default 24
help
The pin of thruster channel 6.

config SUB_PROTOBUF_THRUSTER7_PIN
int "Thruster7 pin"
default 25
help
The pin of thruster channel 7.
endmenu

endmenu
13 changes: 13 additions & 0 deletions components/protobuf_rpc/include/message_cmd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef SUB_PB_RPC_CMD_H
#define SUB_PB_RPC_CMD_H

#include "navi_master.pb.h"

// for main
int message_cmd_init(void);

// for component itself
void message_thruster_cmd(ThrusterCommand *msg);
void message_pwmDev_cmd(PWMDevCommand *msg);

#endif //SUB_PB_RPC_CMD_H
Loading

0 comments on commit 0e5e14b

Please sign in to comment.