目前调试STM32F205的CAN通信,发送数据总是错误,而且一直发送固定不变的数据,跟踪调试发现以下现象:
(1)问题出在CAN的发送函数uint8_t CAN_Transmit(CAN_TypeDef* CANx, CanTxMsg* TxMessage)上,
同样的程序在STM32F103上运行正常,到STM32F205上则异常;
(2)在F103上执行到CANx->sTxMailBox[transmit_mailbox].TIR &= TMIDxR_TXRQ; 语句时
STID、EXID、IDE、RTR均能清零,能够将新的数据赋值给CAN_RI0R;
在STM32F205上运行时,CAN_RI0R寄存器数值不能清零,赋值语句也不能更改寄存器的数值
(3)同样的,设置DLC、DATA[0]-DATA[7]的寄存器操作在F103正常,在F205则不能更改;
(4)两个STM32F205的运行现象一致,应该不是单片机硬件的问题;
(5)CAN寄存器的强制赋值语句也不能改变其数值,如图所示数不变。
(6)CAN能够发送数据出来,可以更改波特率,都能发送。更改波特率1Mbps,500kbps,250kbps结果都是一样的;接收的肯定没有问题,两个USBCAN的接收器结果一致,其中包括使用周立功的USBCAN2。
就是发的数不变:始终是
ID:0x1ffffffe 数据帧 扩展帧 数据长度:0x05 数据:f5 ff ff ff f5
(7)使用的是STM32F205的CAN2
关于CAN的程序清单如下:
#include "stm32f2xx.h"
#include "main.h"
#include <stdio.h>
CanTxMsg TxMessage;
CanRxMsg RxMessage;
/* Private function prototypes -----------------------------------------------*/
void CAN_NVIC_Config(void);
void CAN_Config(void);
void Init_RxMes(CanRxMsg *RxMessage);
/**
* @brief Configures the CAN.
* @param None
* @retval None
*/
void CAN_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN GPIOs configuration **************************************************/
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(CAN_GPIO_CLK, ENABLE);
/* Connect CAN pins to AF9 */
GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_RX_SOURCE, CAN_AF_PORT); //PB12
GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_TX_SOURCE, CAN_AF_PORT); //PB13
/* Configure CAN RX and TX pins */
GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN | CAN_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(CAN_GPIO_PORT, &GPIO_InitStructure);
/* CAN configuration ********************************************************/
/* Enable CAN clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE);
/* CAN register init */
CAN_DeInit(CANx);
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */
CAN_InitStructure.CAN_TTCM = DISABLE;
CAN_InitStructure.CAN_ABOM = DISABLE;
CAN_InitStructure.CAN_AWUM = DISABLE;
CAN_InitStructure.CAN_NART = DISABLE;
CAN_InitStructure.CAN_RFLM = DISABLE;
CAN_InitStructure.CAN_TXFP = DISABLE;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
//CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
/* CAN Baudrate = 500kBps (CAN clocked at 30 MHz) */
/* 30/4=7.5 7.5/(1+6+8)=0.5
*/
CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
CAN_InitStructure.CAN_Prescaler = 8;
CAN_Init(CANx, &CAN_InitStructure);
CAN_FilterInitStructure.CAN_FilterNumber = 14;
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
/* Transmit Structure preparation */
TxMessage.StdId = 0x0012;
TxMessage.ExtId = 0x1314;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.DLC = 8;
TxMessage.Data[0] = 0xAB;
TxMessage.Data[1] = 0xCD;
TxMessage.Data[2] = 0x00;
TxMessage.Data[3] = 0x00;
TxMessage.Data[4] = 0x00;
TxMessage.Data[5] = 0x00;
TxMessage.Data[6] = 0x00;
TxMessage.Data[7] = 0x00;
/* Enable FIFO 0 message pending Interrupt */
CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE);
}
/**
* @brief Configures the NVIC for CAN.
* @param None
* @retval None
*/
void CAN_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**
* @brief Initializes the Rx Message.
* @param RxMessage: pointer to the message to initialize
* @retval None
*/
void Init_RxMes(CanRxMsg *RxMessage)
{
uint8_t i = 0;
RxMessage->StdId = 0x00;
RxMessage->ExtId = 0x00;
RxMessage->IDE = CAN_ID_STD;
RxMessage->DLC = 0;
RxMessage->FMI = 0;
for (i = 0;i < 8;i++)
{
RxMessage->Data = 0x01;
}
}
uint8_t j;
void CAN(void)
{
uint32_t i;
CanTxMsg TxMessage2;
CANx->sTxMailBox[0].TIR = 0x00;
CANx->sTxMailBox[0].TDTR = 0x00;
CANx->sTxMailBox[0].TDLR = 0x00;
CANx->sTxMailBox[0].TDHR = 0x00;
CANx->sTxMailBox[1].TIR = 0x00;
CANx->sTxMailBox[1].TDTR = 0x00;
CANx->sTxMailBox[1].TDLR = 0x00;
CANx->sTxMailBox[1].TDHR = 0x00;
CANx->sTxMailBox[2].TIR = 0x00;
CANx->sTxMailBox[2].TDTR = 0x00;
CANx->sTxMailBox[2].TDLR = 0x00;
CANx->sTxMailBox[2].TDHR = 0x00;
TxMessage2.StdId = 0x00;
TxMessage2.ExtId = 0x1314;
TxMessage2.RTR = CAN_RTR_DATA;
TxMessage2.IDE = CAN_ID_EXT;
TxMessage2.DLC = 8;
TxMessage2.Data[0] = 0xAB;
TxMessage2.Data[1] = 0xCD;
TxMessage2.Data[2] = 0x00;
TxMessage2.Data[3] = 0x00;
TxMessage2.Data[4] = 0x00;
TxMessage2.Data[5] = 0x00;
TxMessage2.Data[6] = 0x00;
TxMessage2.Data[7] = 0x00;
/* NVIC configuration */
CAN_NVIC_Config();
/* CAN configuration */
CAN_Config();
CAN_WakeUp(CAN2);
TxMessage.Data[0] = 0x12;
j = CAN_Transmit(CAN2, &TxMessage2);
while(1)
{
j = CAN_Transmit(CAN2, &TxMessage2);
for(i=0;i<9000000;i++);
}
}
跟踪调试的图片如下: |