当前位置: 首页 > article >正文

gd32F470配置CAN通信

can.c

#include "can.h"

can_trasnmit_message_struct transmit_message;

//传入参数为0,则初始化CAN0的GPIO引脚;传入参数为1,则初始化CAN1的GPIO引脚
void CanGpioInit(void)
{
    /* enable can clock */
    rcu_periph_clock_enable(RCU_CAN0);
    rcu_periph_clock_enable(RCU_GPIOD);

    /* configure CAN0 GPIO, CAN0_TX(PD1) and CAN0_RX(PD0) */
    gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
    gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_1);
    gpio_af_set(GPIOD, GPIO_AF_9, GPIO_PIN_1);

    gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
    gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_0);
    gpio_af_set(GPIOD, GPIO_AF_9, GPIO_PIN_0);
}
//初始化CAN和配置过滤器,不过滤任何ID
void can_networking_init(void)
{
    can_parameter_struct can_parameter;
    can_filter_parameter_struct can_filter;
    /* initialize CAN structures */
    can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
    can_struct_para_init(CAN_FILTER_STRUCT, &can_filter);
    /* initialize CAN register */
    can_deinit(CAN0);

    /* initialize CAN */
    can_parameter.time_triggered = DISABLE;
    can_parameter.auto_bus_off_recovery = ENABLE;
    can_parameter.auto_wake_up = DISABLE;
    can_parameter.auto_retrans = ENABLE;
    can_parameter.rec_fifo_overwrite = DISABLE;
    can_parameter.trans_fifo_order = DISABLE;
    can_parameter.working_mode = CAN_SILENT_LOOPBACK_MODE;
    can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
    can_parameter.time_segment_1 = CAN_BT_BS1_7TQ;
    can_parameter.time_segment_2 = CAN_BT_BS2_2TQ;
    /* baudrate 1Mbps */
    can_parameter.prescaler = 5;
    can_init(CAN0, &can_parameter);

    /* initialize filter */
    /* CAN0 filter number */
    can_filter.filter_number = 0;

    /* initialize filter */
    can_filter.filter_mode = CAN_FILTERMODE_MASK;
    can_filter.filter_bits = CAN_FILTERBITS_32BIT;
    can_filter.filter_list_high = 0x0000;
    can_filter.filter_list_low = 0x0000;
    can_filter.filter_mask_high = 0x0000;
    can_filter.filter_mask_low = 0x0000;
    can_filter.filter_fifo_number = CAN_FIFO0;
    can_filter.filter_enable = ENABLE;
    can_filter_init(&can_filter);
	
	//使能CAN0的接收中断
	nvic_irq_enable(CAN0_RX0_IRQn, 0, 0);
	can_interrupt_enable(CAN0, CAN_INTEN_RFNEIE0);
}


uint8_t can_send_msg(uint32_t id, uint8_t *msg, uint8_t len)
{
	uint32_t timeout = 0xFFFF;
	uint8_t transmit_mailbox = 0;
	
	transmit_message.tx_sfid = id;
    transmit_message.tx_efid = id;
    transmit_message.tx_ft = CAN_FT_DATA;
    transmit_message.tx_ff = CAN_FF_STANDARD;
    transmit_message.tx_dlen = len;
	for(int i=0;i<len;i++)
	{
		transmit_message.tx_data[i] = msg[i];
	}

	transmit_mailbox = can_message_transmit(CAN0, &transmit_message);
	
	/* waiting for transmit completed */
	timeout = 0xFFFF;
	while((CAN_TRANSMIT_OK != can_transmit_states(CAN0, transmit_mailbox)) && (0 != timeout)){
		timeout--;
	}
	if(timeout==0)
	{
		return 1;//发送失败
	} 
	return 0;//发送成功
}

can.h

#ifndef __CAN_H
#define __CAN_H

#include "head.h"

void CanGpioInit(void);
void can_networking_init(void);
uint8_t can_send_msg(uint32_t id, uint8_t *msg, uint8_t len);

extern can_trasnmit_message_struct transmit_message;

#endif

中断处理函数

char receive_flag;
can_receive_message_struct receive_message;

void CAN0_RX0_IRQHandler(void)
{
    /* check the receive message */
    can_message_receive(CAN0, CAN_FIFO0, &receive_message);
    if((0x7ab == receive_message.rx_sfid)&&(CAN_FF_STANDARD == receive_message.rx_ff) && (8 == receive_message.rx_dlen)){
        receive_flag = 1;
    }
}

主函数

	CanGpioInit();
	can_networking_init();
	
	uint8_t buf[8];
	buf[0] = 0xA0;
	buf[1] = 0xA1;
	buf[2] = 0xA2;
	buf[3] = 0xA3;
	buf[4] = 0xA4;
	buf[5] = 0xA5;
	buf[6] = 0xA6;
	buf[7] = 0xA7;
	
    while(1){
	    can_send_msg(0x7ab,buf,8);
		
        if(1 == receive_flag)
		{
            receive_flag = 0;
            for(int i = 0; i < receive_message.rx_dlen; i++){
                printf(" %02x", receive_message.rx_data[i]);
            }
        }
    }

 


http://www.kler.cn/a/228254.html

相关文章:

  • ES6语法
  • Git代码管理工具 — 5 GitHub远程仓库
  • Git 详细安装教程以及gitlab添加SSH密钥
  • 机器学习-使用梯度下降最小化均方误差
  • uiautomator2教程
  • 微前端qiankun的基本使用(vue-element-admin作为项目模版)
  • R语言学习case10:ggplot基础画图Parallel Coordinate Plot 平行坐标图
  • C#学习(十二)——Linq
  • 深度学习系列57: 清华大模型MiniCPM上手
  • STM32F407移植OpenHarmony笔记9
  • 算法刷题day05
  • idea常用设置
  • sentinel的Context创建流程分析
  • 【递归】 92. 反转链表 II
  • FPGA开发
  • 【Spring Boot 3】【@Scheduled】多线程执行定时任务
  • HTTP中传输协议的数据格式
  • Python爬虫某云音乐歌手及下载其免费音乐
  • 【java批量导出pdf】优化方案
  • Liunx基本指令
  • 【PyRestTest】高级使用
  • Python循环语句——while循环的嵌套应用
  • 乐鑫与 Elektor 杂志合作推出特刊,聚焦 AIoT 创新
  • 神经网络基本原理
  • 部署私有知识库项目FastGPT
  • 图论练习3