change all memory management to heap instead of stack

This commit is contained in:
NeoZng
2022-10-20 21:38:48 +08:00
parent a3a95768e6
commit f46a463449
9 changed files with 64 additions and 74 deletions

View File

@@ -52,17 +52,22 @@ static void CANServiceInit()
HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING);
}
void CANRegister(can_instance* _instance)
can_instance* CANRegister(uint8_t tx_id,uint8_t rx_id,CAN_HandleTypeDef* can_handle,can_callback module_callback)
{
static uint8_t instance_idx;
if(!instance_idx)
static uint8_t idx;
if(!idx)
{
CANServiceInit();
}
_instance->rx_buff=(uint8_t*)malloc(8*sizeof(uint8_t));
_instance->tx_buff=(uint8_t*)malloc(8*sizeof(uint8_t));
CANAddFilter(_instance);
instance[instance_idx++]=_instance;
instance[idx]=(can_instance*)malloc(sizeof(can_instance));
instance[idx]->can_handle=can_handle;
instance[idx]->tx_id=tx_id;
instance[idx]->rx_id=rx_id;
instance[idx]->can_module_callback=module_callback;
instance[idx]->tx_buff=(uint8_t*)malloc(8*sizeof(uint8_t));
instance[idx]->rx_buff=(uint8_t*)malloc(8*sizeof(uint8_t));
CANAddFilter(instance[idx]);
return instance[idx++];
}
void CANTransmit(can_instance* _instance)

View File

@@ -9,9 +9,6 @@
#define MX_CAN_FILTER_CNT (4 * 14) // temporarily useless
#define DEVICE_CAN_CNT 2 // CAN1,CAN2
/* module callback,which resolve protocol when new mesg arrives*/
/* can instance typedef, every module registered to CAN should have this variable */
typedef struct tmp
{
@@ -24,6 +21,9 @@ typedef struct tmp
void (*can_module_callback)(struct tmp*); // callback needs an instance to tell among registered ones
} can_instance;
/* module callback,which resolve protocol when new mesg arrives*/
typedef void (*can_callback)(can_instance*);
/**
* @brief transmit mesg through CAN device
*
@@ -34,13 +34,10 @@ void CANTransmit(can_instance* _instance);
/**
* @brief Register a module to CAN service,remember to call this before using a CAN device
*
* @attention tx_id, rx_id, can_handle and module_callback should be set before calling this func
* for the rest configs, this func will do for you
*
* @param _instance can instance owned by a specific device, remember to initialize it!
*
*/
void CANRegister(can_instance* _instance);
can_instance* CANRegister(uint8_t tx_id,uint8_t rx_id,CAN_HandleTypeDef* can_handle,can_callback module_callback);
#endif