2021-02-16 12:57 AM
Hello! I'm working on project requaring CAN node. I decided to quckly check if the periphery is configured correcltly. For this purpose I connected two pull-ups resistors to Rx and Tx pins and tried to see some bits on the oscilloscope.
I configured GPIOs (as Alternate Function and Open Drain output), CAN and CAN's filters. Inside the "while(1)" cycle I tried to send some message. But there is no bits on th oscilloscope.
Here is my code.
I have 2 questions. Is it possible to test my software configuration the way I describe?
Could you review the code and explain what is wrong with that, please?
#include "stm32f0xx.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_gpio.h"
#include "stm32f0xx_can.h"
void CAN1_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);
CAN_OperatingModeRequest(CAN1,CAN_OperatingMode_Initialization);
CAN_InitTypeDef CAN;
CAN.CAN_Prescaler=100;
CAN.CAN_Mode=CAN_Mode_Normal;
CAN.CAN_SJW=CAN_SJW_1tq;
CAN.CAN_BS1=CAN_BS1_4tq;
CAN.CAN_BS2=CAN_BS1_3tq;
CAN.CAN_TTCM=DISABLE;
CAN.CAN_ABOM=DISABLE;
CAN.CAN_AWUM=DISABLE;
CAN.CAN_NART=DISABLE;
CAN.CAN_RFLM=DISABLE;
CAN.CAN_TXFP=DISABLE;
CAN_StructInit(&CAN);
CAN_ITConfig(CAN1,CAN_IT_FF0,ENABLE);
}
void CAN1_Filter_Init(void)
{
CAN_FilterInitTypeDef FILT;
FILT.CAN_FilterNumber = 0;
FILT.CAN_FilterMode = CAN_FilterMode_IdList;
FILT.CAN_FilterScale = CAN_FilterScale_16bit;
FILT.CAN_FilterIdHigh = 0x53A<<5;
FILT.CAN_FilterIdLow = 0x53D<<5;
FILT.CAN_FilterMaskIdHigh = 0x54D<<5;
FILT.CAN_FilterMaskIdLow = 0x54F<<5;
FILT.CAN_FilterFIFOAssignment = 0;
FILT.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&FILT);
FILT.CAN_FilterNumber = 1;
FILT.CAN_FilterIdHigh = 0xA<<5;
FILT.CAN_FilterIdLow = 0x33<<5;
FILT.CAN_FilterMaskIdHigh = 0x44<<5;
FILT.CAN_FilterMaskIdLow = 0x55<<5;
CAN_FilterInit(&FILT);
}
void GPIOA_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_12;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_4);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_4);
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_SetPriority(CEC_CAN_IRQn,1);
NVIC_EnableIRQ(CEC_CAN_IRQn);
}
void CEC_CAN_IRQHandler()
{
/*HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
CAN_ID = CanHandle->pRxMsg->StdId;
CAN_DLC = CanHandle->pRxMsg->DLC;
DATA[0] = CanHandle->pRxMsg->Data[0];
DATA[1] = CanHandle->pRxMsg->Data[1];
DATA[2] = CanHandle->pRxMsg->Data[2];
DATA[3] = CanHandle->pRxMsg->Data[3];
DATA[4] = CanHandle->pRxMsg->Data[4];
DATA[5] = CanHandle->pRxMsg->Data[5];
DATA[6] = CanHandle->pRxMsg->Data[6];
DATA[7] = CanHandle->pRxMsg->Data[7];
HAL_CAN_Receive_IT(&hcan, CAN_FIFO0);*/
//CAN_Receive()
}
int main(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
GPIOA_Init();
CAN1_Init();
CAN1_Filter_Init();
CanTxMsg Message;
Message.StdId=0x222;
Message.DLC=1;
Message.Data[0]=83;
CAN_OperatingModeRequest(CAN1,CAN_OperatingMode_Normal);
while(1)
{
CAN_Transmit(CAN1,&Message);
int i = 0;
for (i = 0; i < 1000000; ++i) {}
}
}
2021-02-16 01:09 AM
> I connected two pull-ups resistors to Rx and Tx
Try connecting RX and TX. On a regular bus, RX is also listening to its own messages sent by TX.