This commit is contained in:
ZhuRui 2024-01-20 17:44:50 +08:00
parent c53933c3c6
commit 919ba55fe1
22 changed files with 415 additions and 119 deletions

View File

@ -1,6 +0,0 @@
{
"ExpandedNodes": [
""
],
"PreviewInSolutionExplorer": false
}

Binary file not shown.

Binary file not shown.

View File

@ -1,6 +0,0 @@
{
"idf.adapterTargetName": "esp32s3",
"files.associations": {
"can_network.h": "c"
}
}

View File

@ -12,6 +12,7 @@
#include "esp_check.h"
#define MOTOR_CTRL_TIMER_DIVIDER (16) // Hardware timer clock divider
#define TIMER_BASE_CLK (160)
#define MOTOR_CTRL_TIMER_SCALE (TIMER_BASE_CLK / MOTOR_CTRL_TIMER_DIVIDER) // convert counter value to seconds
#define MOTOR_CONTROL_TIMER_GROUP TIMER_GROUP_0

View File

@ -16,7 +16,6 @@ extern "C" {
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "driver/timer.h"
typedef struct {
int (*get_pulse_callback)(void *);
void *callback_args;

View File

@ -6,7 +6,6 @@
CONDITIONS OF ANY KIND, either express or implied.
*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
@ -22,6 +21,7 @@
#include "esp_bt_defs.h"
#include "esp_bt_main.h"
#include "bt_server.h"
#define SPP_DEBUG_MODE 1
#define GATTS_TABLE_TAG "GATTS_SPP_DEMO"

View File

@ -101,7 +101,7 @@ static void twai_receive_task(void *arg)
{
index += sprintf(&msg_data_str[i], "%02x ", rx_msg.data[i]);
}
ESP_LOGI(TAG, "recive msg id=%x,len=%d,data=[%s]", rx_msg.identifier, rx_msg.data_length_code, msg_data_str);
ESP_LOGI(TAG, "recive msg id= %u,len=%d,data=[%s]", rx_msg.identifier, rx_msg.data_length_code, msg_data_str);
}
}
}

View File

@ -27,12 +27,12 @@
#include "driver/mcpwm.h"
/*蓝牙相关的头文件*/
#include "esp_bt.h"
#include "../../../components/bt/include/esp32/include/esp_bt.h"
#include "driver/uart.h"
#include "esp_gap_ble_api.h"
#include "esp_gatts_api.h"
#include "esp_bt_defs.h"
#include "esp_bt_main.h"
#include "../../../components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h"
#include "../../../components/bt/host/bluedroid/api/include/api/esp_gatts_api.h"
#include "../../../components/bt/host/bluedroid/api/include/api/esp_bt_defs.h"
#include "../../../components/bt/host/bluedroid/api/include/api/esp_bt_main.h"
/* The examples use WiFi configuration that you can set via project configuration menu
@ -70,6 +70,8 @@
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
#define GPIO_4G_PWR GPIO_NUM_4
#define PWR_4G_ON (0)
#define PWR_4G_OFF (1)
static const char *TAG = "main";
@ -77,7 +79,6 @@ static const char *TAG = "main";
extern void wifi_init_softap(void);
void ads1220_init(void);
void PWR_4G_Init(void);
extern void can_init(void);
extern void FLOW_init();
@ -103,10 +104,10 @@ void app_main(void)
}
ESP_ERROR_CHECK(ret);
ESP_ERROR_CHECK(i2c_master_init());
config_load();
config_load();//读取保存在FRAM里的数据
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
rtc_clk_apb_freq = rtc_clk_apb_freq_get();
ESP_LOGI(TAG, "rtc_clk_apb_freq=%d", rtc_clk_apb_freq);
ESP_LOGI(TAG, "rtc_clk_apb_freq=%u", rtc_clk_apb_freq);
wifi_init_softap();//ok
// wifi_init_sta();
@ -130,19 +131,22 @@ void app_main(void)
void PWR_4G_Init(void)
{
// zero-initialize the config structure.
gpio_config_t io_conf = {};
gpio_config_t io_conf = {
// disable interrupt
io_conf.intr_type = GPIO_INTR_DISABLE;
.intr_type = GPIO_INTR_DISABLE,
// set as output mode
io_conf.mode = GPIO_MODE_OUTPUT;
.mode = GPIO_MODE_OUTPUT,
// bit mask of the pins that you want to set,e.g.GPIO18/19
io_conf.pin_bit_mask = GPIO_4G_PWR;
.pin_bit_mask = GPIO_4G_PWR,
// disable pull-down mode
io_conf.pull_down_en = 0;
.pull_down_en = 0,
// disable pull-up mode
io_conf.pull_up_en = 0;
.pull_up_en = 0,
};
// configure GPIO with the given settings
gpio_config(&io_conf);
gpio_set_level(GPIO_4G_PWR, 0);
gpio_set_level(GPIO_4G_PWR, PWR_4G_OFF);
vTaskDelay(pdMS_TO_TICKS(1000));
gpio_set_level(GPIO_4G_PWR, PWR_4G_ON);
}

View File

@ -166,21 +166,24 @@ int ModbusSlaveProcess(uint8_t *txbuf, uint8_t *rxbuf, uint16_t rxLen, int is_cr
int i;
uint8_t *pDat;
uint16_t *pWordVar;
if(rxLen<4)
if(rxLen<4){
return 0;
if((rxbuf[0]==modbus_addr) || (rxbuf[0]==255))
{
if(is_crc != 0)
{
}
/*地址校验*/
if((rxbuf[0]==modbus_addr) || (rxbuf[0]==255)){
/*crc校验*/
if(is_crc != 0){
crcData = rxbuf[rxLen-1]+(rxbuf[rxLen-2]<<8);
crcChk = crc16(rxbuf,rxLen-2);
if( crcData != crcChk)
{
if( crcData != crcChk){
return 0;
}
}
add = ((uint16_t)rxbuf[2]<<8)+ rxbuf[3];
//if(Length !=
length = (rxbuf[4]<<8) | rxbuf[5];
txbuf[0] = rxbuf[0];

View File

@ -95,7 +95,7 @@ int ads1220_watchdog = 0;
extern xQueueHandle gpio_evt_queue;
xQueueHandle gpio_evt_queue = NULL;
uint16_t gWordVar[gWORD_SIZE];
extern uint16_t gWordVar[gWORD_SIZE];
int ad_value[4];
uint32_t ad_update_time[4];
uint8_t ad_watchdog_cnt = 0;
@ -387,10 +387,11 @@ extern flow_config_t *flow_config;
extern flow_t *pflow;
extern uint8_t timeout[2];
extern int flow_rem[2];
void ads1220_task(void)
{
uint32_t io_num;
static uint32_t ad_flow_tick = 10;
// static uint32_t ad_flow_tick = 10;
while (1)
{
// ESP_LOGI(TAG, "xQueueReceive before\n ");

View File

@ -9,7 +9,9 @@
#include "esp_check.h"
#include "soc/rtc.h"
#include "driver/mcpwm.h"
#include "../../../components/hal/include/hal/gpio_types.h"
// #include "../../../components/driver/gpio/include/driver/gpio.h"
#include "driver/gpio.h"
const static char *TAG = "capture";
typedef struct capture_event

View File

@ -6,7 +6,6 @@
// #include "SEGGER_RTT.h"
#include "ModbusM.h"
uint32_t TickDiff(uint32_t comptime);
extern void modbus_master_poll(int n);
extern int modbus_master_on_revice(int ch, uint8_t *rxbuf, int length);

View File

@ -136,6 +136,7 @@ void config_load(void)
}
}
//恢复默认配置
void restore_default(void)
{
memcpy(cal_4_20ma, &default_cal_4_20ma, sizeof(default_cal_4_20ma));

View File

@ -12,13 +12,13 @@
#include "ModbusS.h"
#include "rotary_encoder.h"
#include "esp_log.h"
#include "esp_check.h"
#include "soc/rtc.h"
#include "driver/mcpwm.h"
// #include "driver/mcpwm_prelude.h"
#include "config.h"
#include "esp_gatts_api.h"
#include "../../../../components/bt/host/bluedroid/api/include/api/esp_gatts_api.h"
#include "bt_server.h"
#include "esp_system.h"
@ -58,6 +58,14 @@ typedef struct capture_event
#define DEPTH_PIN_ENC_B 41 // 深度控制GPIO端口 TIM3_CH2
#define SEND_DATA_TEST 0
#define PILE_STATE_STOP 0x0100
#define PILE_STATE_PAUSE 0x0200
#define PILE_STATE_WORK 0x0300
#define PILE_DIR_NONE 0x01
#define PILE_DIR_UP 0x02
#define PILE_DIR_DOWN 0x03
// #define DEPTH_PIN_ENC_A 36 // 深度脉冲GPIO端口
// #define DEPTH_PIN_ENC_B 35 // 深度控制GPIO端口
@ -124,7 +132,7 @@ typedef struct{
typedef struct
{
int16_t up_down; // 12
uint16_t pile_work_state_and_direction; // 12
int16_t speed; // 13
int16_t depth; // 14
uint16_t sample_count; // 15
@ -188,7 +196,8 @@ void capture_depth_init()
.cap_edge = MCPWM_NEG_EDGE,
.cap_prescale = 1,
.capture_cb = depth_isr_handler, // 绑定深度中断处理函数
.user_data = NULL};
.user_data = NULL
};
if (depth_config->input_type > 1)
{
conf.cap_edge = MCPWM_BOTH_EDGE;
@ -254,7 +263,7 @@ void depth_init(void)
// // HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL);
// }
depth_data->depth_offset = -depth_config->depth_offset;
depth_data->up_down = 1; // 默认工作
depth_data->pile_work_state_and_direction = (PILE_STATE_STOP | PILE_DIR_NONE); // 默认不工作
}
enum
@ -344,10 +353,10 @@ void send_data_to_bt(void){
gWordVar[TIME] = time;
send_to_bt2.time = time;
#endif
ESP_LOGI(TAG, "flow 1 :%d\n",send_to_bt2.flow[0]);
ESP_LOGI(TAG, "flow 2 :%d\n",send_to_bt2.flow[1]);
ESP_LOGI(TAG, "total_flow 1 :%d\n",send_to_bt2.total_flow[0]);
ESP_LOGI(TAG, "total_flow 2 :%d\n",send_to_bt2.total_flow[1]);
// ESP_LOGI(TAG, "flow 1 :%d\n",send_to_bt2.flow[0]);
// ESP_LOGI(TAG, "flow 2 :%d\n",send_to_bt2.flow[1]);
// ESP_LOGI(TAG, "total_flow 1 :%u\n",send_to_bt2.total_flow[0]);
// ESP_LOGI(TAG, "total_flow 2 :%u\n",send_to_bt2.total_flow[1]);
if(is_connected == true){
ESP_LOGI(TAG, "is connected\n");
esp_err_t err = esp_ble_gatts_send_indicate(spp_gatts_if,
@ -371,6 +380,257 @@ void send_data_to_bt(void){
}
/**
* _enc1_update_time 1
* enc1_update_time 1
* time_diff
* depth_config
* enc1_value 1
* prev_depth
*/
void depth_task(void *arg)
{
ESP_LOGI(TAG, "depth_task start\n");
int time_diff = 0;
int speed_timeout = 0;
int last_speed_enc_value = 0; // 上次速度计算的编码器值
int speed_enc_update_time = 0;
int speed_calc_count = 0;
int enc_value = 0;
int bt_time_count = 0;
int64_t work_start_time = 0;
uint16_t last_work_state = 0;
depth_data->depth_offset = -depth_config->depth_offset;
depth_data->pile_work_state_and_direction = 0x0300;
last_work_state = depth_data->pile_work_state_and_direction;
record->pile_id = ++last_pile_id;
gWordVar[LAST_PILE_ID_ADDR] = last_pile_id;
while(1)
{
ESP_LOGI(TAG, "pile_work_state_and_direction = 0x%04x\n",gWordVar[12]);
if (_enc1_update_time != enc1_update_time)
{
ESP_LOGI(TAG, "_enc1_update_time != enc1_update_time\n");
enc_update_time = enc1_update_time;
enc_value = enc1_value;
time_diff = abs_sub(enc_update_time, _enc1_update_time);
_enc1_update_time = enc_update_time;
}
if (time_diff != 0)
{
ESP_LOGI(TAG, "time_diff = %d\n",time_diff);
int16_t depth = enc_value * depth_config->N / depth_config->M; // 1mm
depth_data->depth = depth - depth_data->depth_offset;
/*深度补偿修正*/
if (depth_data->depth > depth_config->max_depth)
{
ESP_LOGI(TAG, "depth_data->depth > depth_config->max_depth\n");
depth_data->depth_offset = depth - depth_config->max_depth;
depth_data->depth = depth_config->max_depth;
}
else if (depth_data->depth < depth_config->min_depth)
{
ESP_LOGI(TAG, "depth_data->depth < depth_config->min_depth\n");
depth_data->depth_offset = depth - depth_config->min_depth;
depth_data->depth = depth_config->min_depth;
}
/*更新记录值*/
if (depth_data->depth > record->max_depth)
{
ESP_LOGI(TAG, "depth_data->depth > record->max_depth\n");
record->max_depth = depth_data->depth;
//send_to_bt1.max_depth = depth_data->depth;
}
// 500ms计算一次速度
if (speed_calc_count++ > 50)
{
ESP_LOGI(TAG, "speed_calc_count++ > 50\n");
speed_calc_count = 0;
int speed_time_diff = abs_sub(enc_update_time, speed_enc_update_time);
int time_diff_us = speed_time_diff / (APB_CLK_FREQ / 1000000);
if (time_diff_us > 0 && time_diff_us < 5000000)
{
ESP_LOGI(TAG, "time_diff_us:%d,dist_diff=%d", time_diff_us, enc_value - last_speed_enc_value);
// speed = dist_diff / speed_time_diff speed 单位mm/min dist_diff 单位mm speed_time_diff 单位us
depth_data->speed = (enc_value - last_speed_enc_value) * depth_config->N / depth_config->M * 600000ll / time_diff_us;
speed_timeout = 0;
speed_enc_update_time = enc_update_time;
last_speed_enc_value = enc_value;
}
else
{
if (++speed_timeout > 10)
{
depth_data->speed = 0;
speed_timeout = 0;
speed_enc_update_time = enc_update_time;
last_speed_enc_value = enc_value;
}
}
}
uint16_t pile_work_state = (depth_data->pile_work_state_and_direction & 0xff00);
if (pile_work_state == PILE_STATE_WORK)
{
ESP_LOGI(TAG, "pile_work_state == PILE_STATE_WORK\n");
/*如果机器从停止状态->工作状态,则重新记录工作开始时间*/
if(last_work_state == PILE_STATE_STOP){
work_start_time = esp_timer_get_time();
depth_data->one_pile_work_time = 0;
}
int64_t current_work_time = esp_timer_get_time();
if(work_start_time != 0){
depth_data->one_pile_work_time = (uint16_t)((current_work_time - work_start_time)/1000000);
}
/*下钻,深度增加*/
if (enc_value > last_enc_value)
{
ESP_LOGI(TAG, "enc_value > last_enc_value\n");
uint8_t pile_work_dir = (depth_data->pile_work_state_and_direction & 0xff);
if(pile_work_dir != PILE_DIR_DOWN)
{
// 方向改变,更新目标采样深度
depth_data->pile_work_state_and_direction = ((depth_data->pile_work_state_and_direction & 0xff00) | PILE_DIR_DOWN);
target_sample_depth = (depth_data->depth / depth_config->sample_depth) * depth_config->sample_depth;
// 小于半个采样长度的合并到下一个采样点
if (abs(depth_data->depth - target_sample_depth) < depth_config->sample_depth / 2)
{
target_sample_depth += depth_config->sample_depth;
}
}
// 到达或超过目标采样点
if (depth_data->depth >= target_sample_depth)
{
// 由于编码器精度问题不能确保数据在采样点上,需要通过插值计算采样点
// 当使用10线编码器时每个脉冲深度达6.25cm当采样间隔为10cm时抖动值太大
if ((prev_depth - depth_data->depth) != 0)
{
int i;
//uint32_t time = (target_sample_depth - prev_depth) * (enc_update_time - prev_update_time) / (depth_data->depth - prev_depth) + prev_update_time;
for (i = 0; i < 2; i++)
{
// int total_flow = get_total_flow_by_time(i, time);
int total_flow = get_total_flow_by_time(i, pflow[i].update_time);
int flow = total_flow - depth_data->last_total_flow[i];
if (flow < 0)
{
flow = 0;
}
depth_data->depth_flow[i] = flow;
depth_data->last_total_flow[i] = total_flow;
}
depth_data->sample_count++;
target_sample_depth += depth_config->sample_depth;
add_recod_item();
}
}
}
/*上钻,深度减少*/
else if (enc_value < last_enc_value)
{
ESP_LOGI(TAG, "enc_value < last_enc_value\n");
uint8_t pile_work_dir = (depth_data->pile_work_state_and_direction & 0xff);
if (pile_work_dir != PILE_DIR_UP)
{
// 方向改变
depth_data->pile_work_state_and_direction = ((depth_data->pile_work_state_and_direction & 0xff00) | PILE_DIR_UP);
target_sample_depth = (depth_data->depth / depth_config->sample_depth - 1) * depth_config->sample_depth;
if (abs(depth_data->depth - target_sample_depth) < depth_config->sample_depth / 2)
{
// 小于半个采样长度的合并到下一个采样点
target_sample_depth -= depth_config->sample_depth;
}
}
if (depth_data->depth <= target_sample_depth)
{
// 由于编码器精度问题不能确保数据在采样点上,需要通过插值计算采样点
// 当使用10线编码器时每个脉冲深度达7.6cm当采样间隔为10cm时抖动值太大
if ((prev_depth - depth_data->depth) != 0)
{
int i;
//uint32_t time = (prev_depth - target_sample_depth) * (enc_update_time - prev_update_time) / (prev_depth - depth_data->depth) + prev_update_time;
for (i = 0; i < 2; i++)
{
// int total_flow = get_total_flow_by_time(i, time);
int total_flow = get_total_flow_by_time(i, pflow[i].update_time);
int flow = total_flow - depth_data->last_total_flow[i];
if (flow < 0)
{
flow = 0;
}
depth_data->depth_flow[i] = flow;
depth_data->last_total_flow[i] = total_flow;
}
depth_data->sample_count++;
add_recod_item();
target_sample_depth -= depth_config->sample_depth;
}
}
}
else
{
depth_data->pile_work_state_and_direction = ((depth_data->pile_work_state_and_direction & 0xff00) | PILE_DIR_NONE);
}
if (depth_data->depth < depth_config->inc_pile_depth) // 小于指定深度时才允许行走清零
{
if (pMoveCtx->pile_inc_req == 1)
{
pMoveCtx->pile_inc_req = 0;
reset_depth();
}
}
else
{
if (pMoveCtx->pile_inc_req == 1)
{
pMoveCtx->pile_inc_req = 0;
}
}
}
else if((depth_data->pile_work_state_and_direction & 0xff00) == PILE_STATE_PAUSE)
{
}
else if((depth_data->pile_work_state_and_direction & 0xff00) == PILE_STATE_STOP)
{
work_start_time = 0;
}
last_work_state = pile_work_state;
prev_depth = depth_data->depth;
prev_update_time = enc_update_time;
last_enc_value = enc_value;
}
else{
ESP_LOGI(TAG, "time diff = 0\n");
vTaskDelay(100);
}
//每隔500ms向蓝牙发送一次数据
// if(bt_time_count++ > 50){
// ESP_LOGI(TAG, "bt_time_count++ > 50\n");
// bt_time_count = 0;
// send_data_to_bt();
// }
vTaskDelay(10);
}
}
#if 0
void depth_task(void *arg)
{
ESP_LOGI(TAG, "go to depth_task sucessfully\n");
@ -392,8 +652,7 @@ void depth_task(void *arg)
gWordVar[LAST_PILE_ID_ADDR] = last_pile_id;
/*获取当前时间*/
uint16_t last_work_time = 0;
uint16_t current_work_time = 0;
uint16_t work_start_time = 0;
uint8_t is_work = 0;
while (1)
@ -410,9 +669,11 @@ void depth_task(void *arg)
if (time_diff != 0)
{
ESP_LOGI(TAG, "time_diff != 0\n");
ESP_LOGI(TAG, "time_diff = %d\n",time_diff);
int16_t depth = enc_value * depth_config->N / depth_config->M; // 1mm
depth_data->depth = depth - depth_data->depth_offset;
depth_data->depth = depth - depth_data->depth_offset;//为什么要减去深度偏移
/*深度补偿修正*/
if (depth_data->depth > depth_config->max_depth)
{
ESP_LOGI(TAG, "depth_data->depth > depth_config->max_depth\n");
@ -425,13 +686,16 @@ void depth_task(void *arg)
depth_data->depth_offset = depth - depth_config->min_depth;
depth_data->depth = depth_config->min_depth;
}
/*更新记录值*/
if (depth_data->depth > record->max_depth)
{
ESP_LOGI(TAG, "depth_data->depth > record->max_depth\n");
record->max_depth = depth_data->depth;
//send_to_bt1.max_depth = depth_data->depth;
}
if (speed_calc_count++ > 50) // 500ms计算一次速度
// 500ms计算一次速度
if (speed_calc_count++ > 50)
{
ESP_LOGI(TAG, "speed_calc_count++ > 50\n");
speed_calc_count = 0;
@ -462,35 +726,42 @@ void depth_task(void *arg)
{
is_work ++;
if(is_work == 1){
depth_data->one_pile_work_time = 0;
last_work_time = (uint16_t)(esp_timer_get_time()/1000000);
int64_t time = esp_timer_get_time();
if(time < 0){
work_start_time = 0;
}
if(is_work > 1){
current_work_time = (uint16_t)(esp_timer_get_time()/1000000);
depth_data->one_pile_work_time = current_work_time - last_work_time;
if(depth_data->one_pile_work_time < 0){
work_start_time = (uint16_t)(time/1000000);
depth_data->one_pile_work_time = 0;
}
last_work_time = current_work_time;
else{
int64_t time = esp_timer_get_time();
if(time > 0){
if((uint16_t)(time/1000000) > work_start_time){
depth_data->one_pile_work_time = ((uint16_t)(time/1000000) - work_start_time);
}
}
}
depth_data->one_pile_work_time = current_work_time - last_work_time;
last_work_time = current_work_time;
ESP_LOGI(TAG, "depth_data->up_down > STOP\n");
// 下钻,深度增加
if (enc_value > last_enc_value)
{ // 下钻,深度增加
{
if (depth_data->up_down != 1)
{ // 方向改变
{
// 方向改变,更新目标采样深度
depth_data->up_down = 1;
target_sample_depth = (depth_data->depth / depth_config->sample_depth) * depth_config->sample_depth;
// 小于半个采样长度的合并到下一个采样点
if (abs(depth_data->depth - target_sample_depth) < depth_config->sample_depth / 2)
{ // 小于半个采样长度的合并到下一个采样点
{
target_sample_depth += depth_config->sample_depth;
}
}
// 到达或超过目标采样点
if (depth_data->depth >= target_sample_depth)
{ // 到达或超过目标采样点
{
// 由于编码器精度问题不能确保数据在采样点上,需要通过插值计算采样点
// 当使用10线编码器时每个脉冲深度达6.25cm当采样间隔为10cm时抖动值太大
if ((prev_depth - depth_data->depth) != 0)
@ -599,7 +870,7 @@ void depth_task(void *arg)
vTaskDelay(10);
}
}
#endif
void add_recod_item(void)
{
//每10cm计算一次
@ -663,7 +934,7 @@ void reset_depth(void)
depth_data->depth = depth_config->depth_offset;
enc1_value = 0;
enc1_update_time = 0;
depth_data->up_down = 1; // 默认工作
depth_data->pile_work_state_and_direction = PILE_STATE_STOP | PILE_DIR_NONE; // 默认不工作
}
void DEPTH_init()

View File

@ -200,7 +200,7 @@ void flow_task(void *arg)
ccr = ccr / (rtc_clk_apb_freq / 1000000);
// __enable_irq();
time_diff = ccr - last_ccr[ch];
ESP_LOGI(TAG, "(type2) t15_ccr_times[%d]: %u %u time_diff: %u", ch, ccr, ccr_times, time_diff);
ESP_LOGI(TAG, "(type2) t15_ccr_times[%d]: %u %d time_diff: %d", ch, ccr, ccr_times, time_diff);
printf("rtc_clk_apb_freq : %u\n", rtc_clk_apb_freq);
if (time_diff != 0)
{

View File

@ -27,6 +27,22 @@ static const char *TAG = "UART0";
uint8_t txbuf[BUF_SIZE];
uint8_t rxbuf[BUF_SIZE];
extern int ModbusSlaveProcess(uint8_t *txbuf, uint8_t *rxbuf, uint16_t rxLen, int is_crc);
extern uint16_t gWordVar[];
void LED1_Toggle(void)
{
static unsigned char flg = 1;
if (flg)
{
gpio_set_level(LED1_GPIO_PIN, 0);
flg = 0;
}
else
{
gpio_set_level(LED1_GPIO_PIN, 1);
flg = 1;
}
}
void uart0_init(void)
{
@ -61,26 +77,14 @@ void uart0_init(void)
ESP_ERROR_CHECK(uart_set_rx_timeout(uart_num, UART_READ_TOUT));
}
void LED1_Toggle(void)
{
static unsigned char flg = 1;
if (flg)
{
gpio_set_level(LED1_GPIO_PIN, 0);
flg = 0;
}
else
{
gpio_set_level(LED1_GPIO_PIN, 1);
flg = 1;
}
}
void uart0_modbus_slave_task(void *arg)
{
uart0_init();
// uint8_t count = 0;
while (1)
{
// ESP_LOGI("modbus_slave","enter while");
// Read data from UART
int txlen = 0;
int len = uart_read_bytes(UART_PORT_NUM, rxbuf, BUF_SIZE, UART_READ_TOUT);
@ -88,15 +92,20 @@ void uart0_modbus_slave_task(void *arg)
// Write data back to UART
if (len > 0)
{
ESP_LOGI("modbus_slave","len = %d",len);
// uart_write_bytes(UART_PORT_NUM, rxbuf, len);
// ESP_LOGI(TAG, "uart_read_bytes len=%d", len);
txlen = ModbusSlaveProcess(txbuf, rxbuf, len, 1);
if (txlen > 0)
{
ESP_LOGI("modbus_slave","txlen = %d",txlen);
LED1_Toggle();
uart_write_bytes(UART_PORT_NUM, txbuf, txlen);
int uart_write_length = uart_write_bytes(UART_PORT_NUM, txbuf, txlen);
ESP_LOGI("modbus_slave","uart_write_length = %d",uart_write_length);
ESP_LOGI("modbus_slave","gWordVar[12] = 0x%04x",gWordVar[12]);
}
}
// if (tick - last_rxtick >= PACKET_READ_TICS)
// {
// com_poll_modbus_master_poll(0);
@ -120,6 +129,5 @@ void uart0_modbus_slave_task(void *arg)
void uart0_modbus_slave_init(void)
{
uart0_init();
xTaskCreate(uart0_modbus_slave_task, "uart0_modbus_slave_task", 4096, NULL, 10, NULL);
}

View File

@ -50,7 +50,7 @@
#define PRINT_UART_BAUD_RATE (115200)
#define PRINT_TASK_STACK_SIZE (1024)
static const char *TAG = "UART";
// static const char *TAG = "UART";
#define BUF_SIZE (1024)

View File

@ -11,17 +11,16 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "../../../components/esp_wifi/include/esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sys.h"
#include "driver/ledc.h"
#include "esp_err.h"
#include "../../../components/esp_hw_support/include/esp_mac.h"
/* The examples use WiFi configuration that you can set via project configuration menu.
If you'd rather not, just change the below entries to strings with
@ -48,14 +47,12 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base,
if (event_id == WIFI_EVENT_AP_STACONNECTED)
{
wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *)event_data;
ESP_LOGI(TAG, "station " MACSTR " join, AID=%d",
MAC2STR(event->mac), event->aid);
ESP_LOGI(TAG, "station " MACSTR " join, AID=%d",MAC2STR(event->mac), event->aid);
}
else if (event_id == WIFI_EVENT_AP_STADISCONNECTED)
{
wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *)event_data;
ESP_LOGI(TAG, "station " MACSTR " leave, AID=%d",
MAC2STR(event->mac), event->aid);
ESP_LOGI(TAG, "station " MACSTR " leave, AID=%d",MAC2STR(event->mac), event->aid);
}
}
extern void ModBusTCPSlave_init(void);

View File

@ -221,7 +221,10 @@ CONFIG_BT_CTRL_HCI_MODE_VHCI=y
# CONFIG_BT_CTRL_HCI_MODE_UART_H4 is not set
CONFIG_BT_CTRL_HCI_TL=1
CONFIG_BT_CTRL_ADV_DUP_FILT_MAX=30
# CONFIG_BT_CTRL_HW_CCA is not set
CONFIG_BT_BLE_CCA_MODE_NONE=y
# CONFIG_BT_BLE_CCA_MODE_HW is not set
# CONFIG_BT_BLE_CCA_MODE_SW is not set
CONFIG_BT_BLE_CCA_MODE=0
CONFIG_BT_CTRL_HW_CCA_VAL=20
CONFIG_BT_CTRL_HW_CCA_EFF=0
CONFIG_BT_CTRL_CE_LENGTH_TYPE_ORIG=y
@ -301,6 +304,9 @@ CONFIG_BT_GATT_MAX_SR_ATTRIBUTES=100
# CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL is not set
CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_AUTO=y
CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MODE=0
# CONFIG_BT_GATTS_ROBUST_CACHING_ENABLED is not set
# CONFIG_BT_GATTS_DEVICE_NAME_WRITABLE is not set
# CONFIG_BT_GATTS_APPEARANCE_WRITABLE is not set
CONFIG_BT_GATTC_ENABLE=y
CONFIG_BT_GATTC_MAX_CACHE_CHAR=40
# CONFIG_BT_GATTC_CACHE_NVS_FLASH is not set
@ -491,8 +497,10 @@ CONFIG_BT_SMP_ENABLE=y
# CONFIG_BT_BLE_ACT_SCAN_REP_ADV_SCAN is not set
CONFIG_BT_BLE_ESTAB_LINK_CONN_TOUT=30
CONFIG_BT_MAX_DEVICE_NAME_LEN=32
CONFIG_BT_BLE_RPA_TIMEOUT=900
# CONFIG_BT_BLE_50_FEATURES_SUPPORTED is not set
CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y
# CONFIG_BT_BLE_HIGH_DUTY_ADV_INTERVAL is not set
# end of Bluedroid Options
# end of Bluetooth
@ -781,6 +789,7 @@ CONFIG_ESP_PHY_CALIBRATION_AND_DATA_STORAGE=y
# CONFIG_ESP_PHY_INIT_DATA_IN_PARTITION is not set
CONFIG_ESP_PHY_MAX_WIFI_TX_POWER=20
CONFIG_ESP_PHY_MAX_TX_POWER=20
# CONFIG_ESP_PHY_REDUCE_TX_POWER is not set
CONFIG_ESP_PHY_ENABLE_USB=y
CONFIG_ESP_PHY_RF_CAL_PARTIAL=y
# CONFIG_ESP_PHY_RF_CAL_NONE is not set
@ -977,7 +986,7 @@ CONFIG_FREERTOS_TICK_SUPPORT_SYSTIMER=y
CONFIG_FREERTOS_CORETIMER_SYSTIMER_LVL1=y
# CONFIG_FREERTOS_CORETIMER_SYSTIMER_LVL3 is not set
CONFIG_FREERTOS_SYSTICK_USES_SYSTIMER=y
CONFIG_FREERTOS_HZ=1000
CONFIG_FREERTOS_HZ=100
CONFIG_FREERTOS_ASSERT_ON_UNTESTED_FUNCTION=y
# CONFIG_FREERTOS_CHECK_STACKOVERFLOW_NONE is not set
# CONFIG_FREERTOS_CHECK_STACKOVERFLOW_PTRVAL is not set
@ -1826,6 +1835,7 @@ CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE=y
# CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION is not set
CONFIG_ESP32_PHY_MAX_WIFI_TX_POWER=20
CONFIG_ESP32_PHY_MAX_TX_POWER=20
# CONFIG_ESP32_REDUCE_PHY_TX_POWER is not set
CONFIG_ESP_SYSTEM_PM_POWER_DOWN_CPU=y
# CONFIG_ESP32S2_PANIC_PRINT_HALT is not set
CONFIG_ESP32S2_PANIC_PRINT_REBOOT=y

View File

@ -221,7 +221,10 @@ CONFIG_BT_CTRL_HCI_MODE_VHCI=y
# CONFIG_BT_CTRL_HCI_MODE_UART_H4 is not set
CONFIG_BT_CTRL_HCI_TL=1
CONFIG_BT_CTRL_ADV_DUP_FILT_MAX=30
# CONFIG_BT_CTRL_HW_CCA is not set
CONFIG_BT_BLE_CCA_MODE_NONE=y
# CONFIG_BT_BLE_CCA_MODE_HW is not set
# CONFIG_BT_BLE_CCA_MODE_SW is not set
CONFIG_BT_BLE_CCA_MODE=0
CONFIG_BT_CTRL_HW_CCA_VAL=20
CONFIG_BT_CTRL_HW_CCA_EFF=0
CONFIG_BT_CTRL_CE_LENGTH_TYPE_ORIG=y
@ -301,6 +304,9 @@ CONFIG_BT_GATT_MAX_SR_ATTRIBUTES=100
# CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MANUAL is not set
CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_AUTO=y
CONFIG_BT_GATTS_SEND_SERVICE_CHANGE_MODE=0
# CONFIG_BT_GATTS_ROBUST_CACHING_ENABLED is not set
# CONFIG_BT_GATTS_DEVICE_NAME_WRITABLE is not set
# CONFIG_BT_GATTS_APPEARANCE_WRITABLE is not set
CONFIG_BT_GATTC_ENABLE=y
CONFIG_BT_GATTC_MAX_CACHE_CHAR=40
# CONFIG_BT_GATTC_CACHE_NVS_FLASH is not set
@ -491,8 +497,10 @@ CONFIG_BT_SMP_ENABLE=y
# CONFIG_BT_BLE_ACT_SCAN_REP_ADV_SCAN is not set
CONFIG_BT_BLE_ESTAB_LINK_CONN_TOUT=30
CONFIG_BT_MAX_DEVICE_NAME_LEN=32
CONFIG_BT_BLE_RPA_TIMEOUT=900
# CONFIG_BT_BLE_50_FEATURES_SUPPORTED is not set
CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y
# CONFIG_BT_BLE_HIGH_DUTY_ADV_INTERVAL is not set
# end of Bluedroid Options
# end of Bluetooth
@ -781,6 +789,7 @@ CONFIG_ESP_PHY_CALIBRATION_AND_DATA_STORAGE=y
# CONFIG_ESP_PHY_INIT_DATA_IN_PARTITION is not set
CONFIG_ESP_PHY_MAX_WIFI_TX_POWER=20
CONFIG_ESP_PHY_MAX_TX_POWER=20
# CONFIG_ESP_PHY_REDUCE_TX_POWER is not set
CONFIG_ESP_PHY_ENABLE_USB=y
CONFIG_ESP_PHY_RF_CAL_PARTIAL=y
# CONFIG_ESP_PHY_RF_CAL_NONE is not set
@ -827,14 +836,17 @@ CONFIG_ESP_MAIN_TASK_AFFINITY_CPU0=y
# CONFIG_ESP_MAIN_TASK_AFFINITY_NO_AFFINITY is not set
CONFIG_ESP_MAIN_TASK_AFFINITY=0x0
CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE=2048
# CONFIG_ESP_CONSOLE_UART_DEFAULT is not set
CONFIG_ESP_CONSOLE_UART_DEFAULT=y
# CONFIG_ESP_CONSOLE_USB_CDC is not set
CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=y
# CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG is not set
# CONFIG_ESP_CONSOLE_UART_CUSTOM is not set
# CONFIG_ESP_CONSOLE_NONE is not set
CONFIG_ESP_CONSOLE_SECONDARY_NONE=y
# CONFIG_ESP_CONSOLE_SECONDARY_NONE is not set
CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG=y
CONFIG_ESP_CONSOLE_UART=y
CONFIG_ESP_CONSOLE_MULTIPLE_UART=y
CONFIG_ESP_CONSOLE_UART_NUM=-1
CONFIG_ESP_CONSOLE_UART_NUM=0
CONFIG_ESP_CONSOLE_UART_BAUDRATE=115200
CONFIG_ESP_INT_WDT=y
CONFIG_ESP_INT_WDT_TIMEOUT_MS=300
CONFIG_ESP_INT_WDT_CHECK_CPU1=y