update some motor defs

This commit is contained in:
NeoZng
2022-10-30 22:19:13 +08:00
parent 5f59ebb916
commit 6441982964
11 changed files with 224 additions and 53 deletions

View File

@@ -3,6 +3,7 @@
#include <stdlib.h>
#include "memory.h"
/* can instance ptrs storage, used for recv callback */
static can_instance* instance[MX_REGISTER_DEVICE_CNT];
/**
@@ -52,7 +53,11 @@ static void CANServiceInit()
HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING);
}
can_instance* CANRegister(uint8_t tx_id,uint8_t rx_id,CAN_HandleTypeDef* can_handle,can_callback module_callback)
/* -----------------------two callable function -----------------------*/
can_instance* CANRegister(can_instance_config config)
{
static uint8_t idx;
if(!idx)
@@ -60,25 +65,33 @@ can_instance* CANRegister(uint8_t tx_id,uint8_t rx_id,CAN_HandleTypeDef* can_han
CANServiceInit();
}
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]->txconf.StdId=config.tx_id;
instance[idx]->txconf.IDE=CAN_ID_STD;
instance[idx]->txconf.RTR=CAN_RTR_DATA;
instance[idx]->txconf.DLC=0x08;
instance[idx]->can_handle=config.can_handle;
instance[idx]->tx_id=config.tx_id;
instance[idx]->rx_id=config.rx_id;
instance[idx]->can_module_callback=config.can_module_callback;
CANAddFilter(instance[idx]);
return instance[idx++];
}
void CANTransmit(can_instance* _instance)
{
CAN_TxHeaderTypeDef txconf;
txconf.StdId=_instance->tx_id;
txconf.IDE=CAN_ID_STD;
txconf.RTR=CAN_RTR_DATA;
txconf.DLC=0x08;
while (HAL_CAN_GetTxMailboxesFreeLevel(_instance->can_handle) == 0);
HAL_CAN_AddTxMessage(_instance->can_handle, &txconf, _instance->tx_buff, &_instance->tx_mailbox);
while(HAL_CAN_GetTxMailboxesFreeLevel(_instance->can_handle) == 0);
HAL_CAN_AddTxMessage(_instance->can_handle, &_instance->txconf, _instance->tx_buff, &_instance->tx_mailbox);
}
/* -----------------------belows are callback definitions--------------------------*/
/**
* @brief this func will recv data from @param:fifox to a tmp can_rx_buff
* then, all the instances will be polling to check which should recv this pack of data
@@ -102,7 +115,8 @@ static void CANFIFOxCallback(CAN_HandleTypeDef* _hcan,uint32_t fifox)
}
}
/* ATTENTION: two CAN device in STM32 share two FIFO */
/* ATTENTION: two CAN devices in STM32 share two FIFOs */
/* functions below will call CANFIFOxCallback() to further process message from a specific CAN device */
/**
* @brief rx fifo callback. Once FIFO_0 is full,this func would be called
@@ -114,6 +128,7 @@ void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
CANFIFOxCallback(hcan, CAN_RX_FIFO0);
}
/**
* @brief rx fifo callback. Once FIFO_1 is full,this func would be called
*

View File

@@ -9,21 +9,35 @@
#define MX_CAN_FILTER_CNT (4 * 14) // temporarily useless
#define DEVICE_CAN_CNT 2 // CAN1,CAN2
/* can instance typedef, every module registered to CAN should have this variable */
typedef struct tmp
typedef struct _
{
CAN_HandleTypeDef* can_handle;
CAN_TxHeaderTypeDef txconf;
uint32_t tx_id;
uint32_t tx_mailbox;
uint8_t tx_buff[8];
uint8_t rx_buff[8];
uint32_t rx_id;
void (*can_module_callback)(struct tmp*); // callback needs an instance to tell among registered ones
void (*can_module_callback)(struct _*); // callback needs an instance to tell among registered ones
} can_instance;
/* this structure is used as initialization*/
typedef struct
{
CAN_HandleTypeDef* can_handle;
uint32_t tx_id;
uint32_t rx_id;
void (*can_module_callback)(can_instance*);
} can_instance_config;
/* module callback,which resolve protocol when new mesg arrives*/
typedef void (*can_callback)(can_instance*);
/**
* @brief transmit mesg through CAN device
*
@@ -31,13 +45,14 @@ typedef void (*can_callback)(can_instance*);
*/
void CANTransmit(can_instance* _instance);
/**
* @brief Register a module to CAN service,remember to call this before using a CAN device
*
* @param _instance can instance owned by a specific device, remember to initialize it!
*
* @param config init config
* @return can_instance* can instance owned by module
*/
can_instance* CANRegister(uint8_t tx_id,uint8_t rx_id,CAN_HandleTypeDef* can_handle,can_callback module_callback);
can_instance* CANRegister(can_instance_config config);
#endif