0% found this document useful (0 votes)
2 views7 pages

Practice of can bus

The document outlines the configuration of hardware and software for a system involving GPIO, UART, and CAN communication. It includes code snippets for initializing GPIO pins for LEDs, configuring UART for monitoring, and setting up CAN communication with specific parameters. Additionally, it describes the main function that handles data transmission and reception, along with a practice scenario involving a tester board and ECU board interaction.

Uploaded by

Trọng Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Practice of can bus

The document outlines the configuration of hardware and software for a system involving GPIO, UART, and CAN communication. It includes code snippets for initializing GPIO pins for LEDs, configuring UART for monitoring, and setting up CAN communication with specific parameters. Additionally, it describes the main function that handles data transmission and reception, along with a practice scenario involving a tester board and ECU board interaction.

Uploaded by

Trọng Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

I.

Hardware

II. Software
A. GPIO_ Configuration for Led (LED1->LED4)
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
//LED1 (PC9) LED2 (PC10) LED3 (PC11) LED4 (PC12)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1| GPIO_Pin_2| GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
B. UART3_ Configuration for monitoring
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;

RCC_AHB1PeriphClockCmd(Open_USARTx_TX_GPIO_CLK,ENABLE);
RCC_AHB1PeriphClockCmd(Open_USARTx_RX_GPIO_CLK,ENABLE);

#if defined OpenUSART1 || defined OpenUSART6


RCC_APB2PeriphClockCmd(Open_USARTx_CLK,ENABLE);
#else
RCC_APB1PeriphClockCmd(Open_USARTx_CLK,ENABLE);
#endif

GPIO_PinAFConfig(Open_USARTx_TX_GPIO_PORT, Open_USARTx_TX_SOURCE, Open_USARTx_TX_AF);


GPIO_PinAFConfig(Open_USARTx_RX_GPIO_PORT, Open_USARTx_RX_SOURCE, Open_USARTx_RX_AF);

/*
* Open_USARTx_TX -> PA9 , Open_USARTx_RX -PA10
*/
GPIO_InitStructure.GPIO_Pin = Open_USARTx_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(Open_USARTx_TX_GPIO_PORT, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = Open_USARTx_RX_PIN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(Open_USARTx_RX_GPIO_PORT, &GPIO_InitStructure);

#if defined HwFlowControl && !defined OpenUART4 && !defined OpenUART5


RCC_AHB1PeriphClockCmd(Open_USARTx_RTS_GPIO_CLK,ENABLE);
RCC_AHB1PeriphClockCmd(Open_USARTx_CTS_GPIO_CLK,ENABLE);

GPIO_PinAFConfig(Open_USARTx_RTS_GPIO_PORT, Open_USARTx_RTS_SOURCE, Open_USARTx_RTS_AF);


GPIO_PinAFConfig(Open_USARTx_CTS_GPIO_PORT, Open_USARTx_CTS_SOURCE, Open_USARTx_CTS_AF);

GPIO_InitStructure.GPIO_Pin = Open_USARTx_RTS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(Open_USARTx_RTS_GPIO_PORT, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = Open_USARTx_CTS_PIN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(Open_USARTx_CTS_GPIO_PORT, &GPIO_InitStructure);
#endif
/*
USARTx configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit
*/

USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
#if defined HwFlowControl && !defined OpenUART4 && !defined OpenUART5
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;
#else
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
#endif
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(Open_USARTx, &USART_InitStructure);
/* Enable the Open_USART Transmit interrupt: this interrupt is generated when the
Open_USARTx transmit data register is empty */
USART_ITConfig(Open_USARTx,USART_IT_RXNE,ENABLE);

USART_Cmd(Open_USARTx, ENABLE);

void USART_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

/* Enable the USARTx Interrupt */


NVIC_InitStructure.NVIC_IRQChannel = Open_USARTx_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
C. Can_configuration
void CAN_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* CAN GPIOs configuration **************************************************/

/* Enable GPIO clock */


RCC_AHB1PeriphClockCmd(Open_CAN_GPIO_CLK, ENABLE);
RCC_APB1PeriphClockCmd(Open_CAN_CLK, ENABLE);
/* Connect CAN pins to AF9 */
GPIO_PinAFConfig(Open_CAN_GPIO_PORT, Open_CAN_RX_SOURCE, Open_CAN_AF_PORT);
GPIO_PinAFConfig(Open_CAN_GPIO_PORT, Open_CAN_TX_SOURCE, Open_CAN_AF_PORT);
/* Configure CAN RX and TX pins */
GPIO_InitStructure.GPIO_Pin = Open_CAN_RX_PIN | Open_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(Open_CAN_GPIO_PORT, &GPIO_InitStructure);
/* CAN configuration ********************************************************/
/* Enable CAN clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
RCC_APB1PeriphClockCmd(Open_CAN_CLK, ENABLE);
/* CAN register init */
CAN_DeInit(Open_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_SJW = CAN_SJW_1tq;

/* CAN Baudrate = 1MBps (CAN clocked at 30 MHz) */


CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
CAN_InitStructure.CAN_Prescaler = 2;
CAN_Init(Open_CANx, &CAN_InitStructure);

/* CAN filter init */


#ifdef OpenCAN1
CAN_FilterInitStructure.CAN_FilterNumber = 0;
#endif
#ifdef OpenCAN2
CAN_FilterInitStructure.CAN_FilterNumber = 14;
#endif
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 = 0x321;
TxMessage.ExtId = 0x01;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.DLC = 1;

/* Enable FIFO 0 message pending Interrupt */


CAN_ITConfig(Open_CANx, CAN_IT_FMP0, ENABLE);
}

* Configures the NVIC for CAN.


*/
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

#ifdef OpenCAN1
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
#else /* USE_CAN2 */
NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
#endif /* OpenCAN1 */

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
Example:

Diagnostic CAN ID
Request Id: 0x712
Tester send request via Request Id to ECU
Tester receives Id from ECU response and then show the value on monitor (PC) via UART3
int main(void)
{
GPIO_Configuration();
USART_Configuration();
NVIC_Config();
CAN_Config();
//show information on monitor (PC) via UART3
printf("\r\n****************************************************************\r\n");
printf("CAN-Bus Test \r\n");
printf("CAN-Bus Speed 100kHz \r\n");
/* Infinite loop */
while (1)
{
if( CanFlag == ENABLE )
{
CanFlag = DISABLE;
printf("CAN Receive Data \r\n");
printf("CAN ID %x \r\n",CAN_ID);
printf("CAN_DATA0 %x \r\n",CAN_DATA0);
printf("CAN_DATA1 %x \r\n",CAN_DATA1);
printf("CAN_DATA2 %x \r\n",CAN_DATA2);
printf("CAN_DATA3 %x \r\n",CAN_DATA3);
printf("CAN_DATA4 %x \r\n",CAN_DATA4);
printf("CAN_DATA5 %x \r\n",CAN_DATA5);
printf("CAN_DATA6 %x \r\n",CAN_DATA6);
printf("CAN_DATA7 %x \r\n",CAN_DATA7);
}
CanWriteData(0x712);
if( Display )
{
/*====LED-ON=======*/
GPIO_SetBits(GPIOB , GPIO_Pin_0);
GPIO_SetBits(GPIOB , GPIO_Pin_1);
GPIO_SetBits(GPIOB , GPIO_Pin_2);
GPIO_SetBits(GPIOB , GPIO_Pin_3);
}
else
{
/*====LED-OFF=======*/
GPIO_ResetBits(GPIOB , GPIO_Pin_0);
GPIO_ResetBits(GPIOB , GPIO_Pin_1);
GPIO_ResetBits(GPIOB , GPIO_Pin_2);
GPIO_ResetBits(GPIOB , GPIO_Pin_3);
}
Display = ~Display;
Delay(); /* delay 200ms */
Delay(); /* delay 200ms */
Delay(); /* delay 200ms */
Delay(); /* delay 200ms */
}
}
Refer to attached file

Practice
In this practice,

- Tester board will send request to ECU board read Wakeup button value and User button
value.
- ECU board receives request, Wakeup button value and User button value are stored in
“Data7” of frame.
- Tester board receives Wakeup button value and User button value from ECU board
response and then show the value on LED1 and LED2.
- Tester board will send request to read Wakeup button value and User button value for each
1 second.

ID of Tester is: 0xF001

You might also like