硬汉嵌入式论坛

 找回密码
 立即注册
查看: 746|回复: 7
收起左侧

[ThreadX全家桶] threadx 线程序 malloc 不成功

[复制链接]

15

主题

87

回帖

132

积分

初级会员

积分
132
发表于 2022-11-10 20:07:43 | 显示全部楼层 |阅读模式
有些芯片里是可以的,也没有作特别的设置,
就两个线程,一个用于gui,一个用于用户作务
创建线程的先后有影响,优先级好像也有影响
两个线程,一个线程可以,另一个线程不行,由于程序里需要
申请内存,搞不懂是哪里的问题,使用的芯片是 f4, threadx + touchgfx

p = malloc(30); //为啥就不成功呢?

    free(p);


哪位指点一下哈
回复

使用道具 举报

80

主题

718

回帖

958

积分

金牌会员

积分
958
发表于 2022-11-10 22:04:03 | 显示全部楼层
为啥不适用ThreadX自带的内存管理要用malloc,malloc在这个环境下使用危险而且碎片严重。
回复

使用道具 举报

15

主题

87

回帖

132

积分

初级会员

积分
132
 楼主| 发表于 2022-11-11 08:15:45 | 显示全部楼层
因为程序需要一个缓存,这是一个中间件
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107585
QQ
发表于 2022-11-11 09:50:22 | 显示全部楼层
分享你的工程到百度云,我看下。
回复

使用道具 举报

3

主题

1255

回帖

1264

积分

至尊会员

积分
1264
发表于 2022-11-11 13:16:49 | 显示全部楼层
你包装一下接口就好吧,用ThreadX自带的来替代你说的那个。
回复

使用道具 举报

15

主题

87

回帖

132

积分

初级会员

积分
132
 楼主| 发表于 2022-11-11 13:43:21 | 显示全部楼层
static    void  app_gui_task(unsigned long  para);
static                 void        app_user_task(unsigned long  para);

#define   task_stack_gui_size         2048
#define   task_stack_user_task_size   2048

#define   user_task_stack_buf_size  (task_stack_gui_size + task_stack_user_task_size + 256)
uint32_t   user_task_stack_buf[user_task_stack_buf_size / 4];

#define   task_gui_prio               3u
#define         task_user_task_prio                                        4u

volatile uint8_t IsTransmittingBlock_;

static    TX_THREAD   app_tcb_gui;
static    TX_THREAD   app_tcb_user_task;
/**
*/
void  user_thread_define(void *first_unused_memory)
{
  CHAR    *pointer = TX_NULL;
  TX_BYTE_POOL    byte_pool_0;
  volatile uint32_t  result;
  
  if (tx_byte_pool_create(&byte_pool_0, "Tx App memory pool", user_task_stack_buf, user_task_stack_buf_size) != TX_SUCCESS)
  {
    return;
  }
  
  result = tx_byte_allocate(&byte_pool_0, (VOID **) &pointer, task_stack_user_task_size, TX_NO_WAIT);  
        result = tx_thread_create(&app_tcb_user_task , \
                    "app user task",
                    app_user_task,
                    0,\
                    pointer, \
                    task_stack_user_task_size, \
                    task_user_task_prio , \
                    task_user_task_prio , \
                    TX_NO_TIME_SLICE , \
                    TX_AUTO_START );                       


  result = tx_byte_allocate(&byte_pool_0, (VOID **) &pointer, task_stack_gui_size, TX_NO_WAIT);
  
  result = tx_thread_create(&app_tcb_gui , \
                    "app gui",
                    app_gui_task,
                    0,\
                    pointer, \
                    task_stack_gui_size, \
                    task_gui_prio , \
                    task_gui_prio , \
                    TX_NO_TIME_SLICE , \
                    TX_AUTO_START );                 
}



void  app_gui_task(ULONG  para)
{
  uint8_t str[] = {"this test program display update"};
  
  modbus_rtu_init(&mod_rs232 , &mod_rs232_info);
  
  i2c_eeprom_init(&hi2c3 , i2c_read , i2c_write , HAL_GetTick);
  eep_read(reg_offset(reg_buf ,&reg_buf[100]) * 2 , (500-100)*2 , (uint8_t*)&reg_buf[100] , 200);
  
  update_set_dis_buf();
  //give_framebuffer_semaphore();
  request_read_reg(1, 20 ,40, -1, &mod_master_ins);
  request_read_reg(1, 100 ,40, -1, &mod_master_ins);
  
  touch_drv_init(&touch_drv , 480 , 320, pen_down_detect , sample_x_y);
  touch_drv.calibrated        = touch_calib_valid;
  touch_drv.pfn_thread_sleep  = HAL_Delay;
  touch_drv.dis_dev = &dis_dev;
  
//  user_para_sensor_1_type = 1;//                (*((uint16_t*)&reg_buf[107]))  //0-关闭,1-拉力,2-扭力,3-应力
//  user_para_sensor_2_type = 2;//                (*((uint16_t*)&reg_buf[108]))  
//  user_para_sensor_3_type = 3;//                (*((uint16_t*)&reg_buf[109]))  
  
  if(touch_calib_valid == 1)
  {
    touch_drv.y_top     = touch_y_top;
    touch_drv.y_bottom  = touch_y_bottom;
    touch_drv.x_left    = touch_x_left;
    touch_drv.x_right   = touch_x_right;
   
    touch_drv.mark_y_top    = touch_mark_y_top;
    touch_drv.mark_y_bottom = touch_mark_y_bottom;
    touch_drv.mark_x_left   = touch_mark_x_left;
    touch_drv.mark_x_right  = touch_mark_x_right;
  }

  //等待gui 进入初始化
  while((dev_sta & sys_init) == 0)
  {
    tx_thread_sleep(10);
  }
  //等待初始化结束
  while((dev_sta & sys_init))
  {
    tx_thread_sleep(10);
    if(touch_drv.pfn_pen_down_detect() == 0)
      {
        dev_sta &= ~sys_init;
        
        touch_drv.y_top     = 1543;
        touch_drv.y_bottom  = 458;
        touch_drv.x_left    = 485;
        touch_drv.x_right   = 1536;
        
        touch_drv.mark_y_top = touch_drv.y_range / 5;
        touch_drv.mark_y_bottom = touch_drv.y_range - touch_drv.mark_y_top;
        touch_drv.mark_x_left = touch_drv.x_range / 5;
        touch_drv.mark_x_right = touch_drv.x_range - touch_drv.mark_x_left;
        
        if(0 == touch_drv_calibrate(&touch_drv))
        {
          touch_y_top = touch_drv.y_top;
          touch_y_bottom = touch_drv.y_bottom;         
          touch_x_left                = touch_drv.x_left;
          touch_x_right                = touch_drv.x_right;

          touch_mark_y_top                = touch_drv.mark_y_top;
          touch_mark_y_bottom        = touch_drv.mark_y_bottom;
          touch_mark_x_left                = touch_drv.mark_x_left;
          touch_mark_x_right        = touch_drv.mark_x_right;

          touch_calib_valid          = 1;
         
          eeprom_queue_append((void*)&touch_y_top , reg_offset(reg_buf , &touch_y_top) * 2 , 9*2 , &eep_queue);
        }
        
        dev_sta |= calib_touch_end;
        break;
      }
  }
  void *p = malloc(10);
  free(p);
  //HAL_TIM_Base_Start_IT(&htim9);
  for(;;)
  {
    tx_thread_sleep(1);
    uart_rxd_task(&rs485_comm);
    uart_rxd_task(&rs232_comm);
    mod_master_task(&mod_master_ins);   
    force_sample();
    touch_drv_update(&touch_drv);
    real_pos = touch_drv.current_touch_coord.x % 1000;
    real_pos += touch_drv.current_touch_coord.y * 1000;
    p = malloc(30);
    eeprom_task(&eep_queue);
    free(p);
  }
}

__IO float monitor_f;

extern void DisplayDriver_TransferCompleteCallback();
extern void touchgfxSignalVSync(void);

void  app_user_task(unsigned long  para)
{
  volatile uint32_t  base ,tick ,acculate;
  uint32_t  lcd_frame_tick;
  
  uint8_t str_buf[32];
  beep_on(1);
  uint8_t *p;
  for(;;)
  {
    tx_thread_sleep(1);
    if(++acculate >= 100)
    {
      acculate = 0;
      tick = HAL_GetTick() - base;
      base = HAL_GetTick();
    }
   
     p = malloc(12);
   
    if((HAL_GetTick() - lcd_frame_tick ) >= 1000)
    {
      lcd_frame_tick = HAL_GetTick();  
      float *fx = (float *)&reg_buf[20];
      monitor_f = *fx;
      f_to_str(monitor_f, 2, str_buf);
      //st7796_dis_str_26x48(20, 100, str_buf , 0);
      uint16_t x = user_para_f_unit;
      if(++x >= 3)
      {
        x = 0;
      }
      
      uint16_t o = (uint32_t)&user_para_f_unit - (uint32_t)&reg_buf[0];
      //request_write_reg(1 , o >> 1 , 1 , 1, &x ,&mod_master_ins);
    }
   
    s1_sample();
    key_scan_task(&key_tick);
    beep_tick_pro(&beep_tick);
    key_process(&key_tick);
    if(writing == 1 )
    {      
      if(0 == st7796_draw_rect(wr_x , wr_y , wr_w, wr_h, (void*)pix))
        {writing = 2;}
      else
        {
          writing = 3;
        }      
    }   
   
    free(p);
    p = 0;
  
  }
}
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107585
QQ
发表于 2022-11-12 10:01:41 | 显示全部楼层
yuntian 发表于 2022-11-11 13:43
static    void  app_gui_task(unsigned long  para);
static                 void        app_user_task(unsigned long  para) ...

malloc使用的.S启动文件的heap,要看这个地方的设置才行。

建议直接分享工程。
回复

使用道具 举报

15

主题

87

回帖

132

积分

初级会员

积分
132
 楼主| 发表于 2022-11-12 22:03:08 | 显示全部楼层
那个设置大小都试过了,结果一样,这个工程有很多其它代码,不能上传啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-6-7 06:15 , Processed in 0.168332 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表