有点复杂的CAN多通道多FIFO循环发送函数

结构体声明和定义:

hal_can_tgtppkg_txpkg(CAN_CH_CMDMODE, (uint8_t *)phd, sizeof(PACK_HEAD_t));

typedef struct {
	uint8_t Phy;
	uint8_t Win;
}CANCFG_UNIT_t;

static CANCFG_UNIT_t can_config_tab[HAL_CANWINNUM_MAX] = {
	{ARCH_CAN0_PHYCODE,0},
	{ARCH_CAN1_PHYCODE,0},{ARCH_CAN1_PHYCODE,1},{ARCH_CAN1_PHYCODE,2},
	{ARCH_CAN1_PHYCODE,3},
};

static PKG_ALLCHFIFO_t rxpkg_ph0all_fifo[CANLNK_PH0WIN_NUMMAX];

#define HAL_TGTPPKGCMD_CANID				1	// ±»²âÄ¿±ê°åͨÐÅ½Ó¿Ú -- À©Õ¹Ö¡


static PKG_ALLCHFIFO_t rxpkg_ph1all_fifo[CANLNK_PH1WIN_NUMMAX];

typedef struct{
	uint8_t flgEnb;
	uint8_t codeOfPhy;
	uint8_t mode;
	uint8_t ide;
	uint32_t canrxid_oneidmode;
	uint32_t cantxid_oneidmode;
	uint32_t canrxid_moreidmode_st;
	uint32_t canrxid_moreidmode_end;
	uint32_t cantxid_moreidmode_st;
	uint32_t cantxid_moreidmode_end;
	
	PKG_FIFO_t rxpkg_fifo[CAN_CH_MAX];
}PKG_ALLCHFIFO_t;

#define CANLNK_PH1WIN_NUMMAX 4

发送函数:

int can_txpkg(uint8_t iWin, uint16_t dev,CAN_CH_t ch, uint8_t *txdat, uint32_t bytes)
{
	CANCFG_UNIT_t *pCanCfgInfo;
	PKG_ALLCHFIFO_t *pWinPkgFifo;
	uint8_t iCanPhycode;
	CanTxMsg txMessage;
	CAN_FRAME_t txframe;
	CAN_FRAME_t *pframe = &txframe;
	uint8_t *ptr;
	int ftotal;
	int ret, wlen, len;
	uint16_t *psum, sumchk;

	if (ch >= CAN_CH_MAX)
		return -1;
	
	if (txdat == NULL || bytes == 0)
		return 0;

	if (bytes > PKG_BYTES_MAX)
	{
		printf("Pkg size too large\r\n");
	}
	pCanCfgInfo = (CANCFG_UNIT_t *)&can_config_tab[iWin];//iWin  CANID  
	if (pCanCfgInfo->Phy == ARCH_CAN0_PHYCODE){
		pWinPkgFifo = &rxpkg_ph0all_fifo[pCanCfgInfo->Win];
	}
	else if (pCanCfgInfo->Phy == ARCH_CAN1_PHYCODE){
		pWinPkgFifo = &rxpkg_ph1all_fifo[pCanCfgInfo->Win];
	}
	
	if (pWinPkgFifo->flgEnb == 0) return 0;
	iCanPhycode = pWinPkgFifo->codeOfPhy;
	
	if (1)
	{
		sumchk = math_sum16_calc(txdat, bytes);
		psum = (uint16_t *)(txdat + bytes);
		*psum = sumchk;
		bytes += 2;
	}
	
	pframe->head.fch = ch;
	ftotal = (bytes + FRAME_DATA_SZIE - 1) / FRAME_DATA_SZIE;
	pframe->head.ftotal = ftotal;
	pframe->head.fidx = 0;

	txMessage.RTR = CAN_RTR_DATA;
	txMessage.IDE = (pWinPkgFifo->ide == 0) ? 0x0 : 0x04;
	if (pWinPkgFifo->mode == ONEID_ACCEPT_MODE){
		txMessage.ExtId = pWinPkgFifo->cantxid_oneidmode;
		txMessage.StdId = pWinPkgFifo->cantxid_oneidmode;
	}
	else if (pWinPkgFifo->mode == MOREID_ACCEPT_MODE){
		if (pWinPkgFifo->cantxid_moreidmode_st+dev<=\
			pWinPkgFifo->cantxid_moreidmode_end){
				txMessage.ExtId = pWinPkgFifo->cantxid_moreidmode_st+dev;
				txMessage.StdId = pWinPkgFifo->cantxid_moreidmode_st+dev;
		}
		else{
			txMessage.ExtId = pWinPkgFifo->cantxid_moreidmode_end;
			txMessage.StdId = pWinPkgFifo->cantxid_moreidmode_end;
		}
	}
	
	ptr = txdat;
	len = bytes;
	while (len > 0)
	{
		wlen = len > FRAME_DATA_SZIE ? FRAME_DATA_SZIE : len;
		if (wlen < FRAME_DATA_SZIE)
		{
			memset((void *)pframe->dat, 0, FRAME_DATA_SZIE);
			memcpy((void *)pframe->dat, (void *)ptr, len);
			wlen = ((wlen+1) >> 1) << 1;
		}
		else
		{
			memcpy((void *)pframe->dat, (void *)ptr, wlen);
		}
		
		pframe->head.fword = (wlen >> 1);
		
		memcpy((void *)txMessage.Data, (void *)pframe, wlen + 2);
		txMessage.DLC = wlen + 2;
		
		ret = arch_can_send(iCanPhycode, &txMessage);
		if (ret < 0)
			return ret;
		
		pframe->head.fidx++;
		len -= wlen;
		ptr += wlen;
	}
	return ret;
}

你可能感兴趣的:(STM32编程,STM32教程)