i was trying to work on bluepill stm32f103c6t6 i triedto send can msgs in loopback mode but im not able to see the mssg in cro for the pins pa11,12 (tx,rx) can somebody help me out . the folowing code i have used for that
#include "stm32f1xx_hal.h"
#include <stdio.h>
// CAN message structure
CAN_TxHeaderTypeDef TxHeader;
uint8_t TxData[5] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE}; // Sample data
uint32_t TxMailbox;
// Add global variable to store CAN handle
CAN_HandleTypeDef hcan;
// UART handle and buffer
UART_HandleTypeDef huart1;
char uart_buffer[100];
// Function prototypes
void CAN_Configuration(void);
void UART_Init(void);
void CAN_Configuration(void) {
// Initialize CAN peripheral and other settings
// ...
// Configure the CAN settings (CAN ID and speed)
hcan.Instance = CAN1;
hcan.Init.Prescaler = 4; // Set the prescaler to achieve 500 Kbps (assuming 8 MHz APB1 clock)
hcan.Init.Mode = CAN_MODE_LOOPBACK; // Enable Loopback Mode
hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan.Init.TimeSeg1 = CAN_BS1_8TQ;
hcan.Init.TimeSeg2 = CAN_BS2_3TQ;
hcan.Init.TimeTriggeredMode = DISABLE;
hcan.Init.AutoBusOff = DISABLE;
hcan.Init.AutoWakeUp = DISABLE;
hcan.Init.AutoRetransmission = ENABLE;
hcan.Init.ReceiveFifoLocked = DISABLE;
hcan.Init.TransmitFifoPriority = DISABLE;
// Start the CAN peripheral
if (HAL_CAN_Init(&hcan) != HAL_OK) {
// Error handling (optional)
while (1);
}
}
void UART_Init(void) {
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK) {
// Error handling for UART initialization failure
while (1);
}
}
int _write(int file, char *ptr, int len) {
HAL_UART_Transmit(&huart1, (uint8_t*)ptr, len, HAL_MAX_DELAY);
return len;
}
int main(void) {
// Initialize HAL, GPIOs, and other peripherals
// ...
// Initialize UART for debugging
UART_Init();
// Initialize the CAN peripheral in Loopback Mode
CAN_Configuration();
// Configure the CAN message header
TxHeader.StdId = 0x400; // Set the CAN ID to 0x400
TxHeader.ExtId = 0; // Not used in standard CAN mode
TxHeader.RTR = CAN_RTR_DATA; // Data frame (not a remote frame)
TxHeader.IDE = CAN_ID_STD; // Standard ID format (not extended)
TxHeader.DLC = 5; // Data Length Code (number of bytes in TxData)
TxHeader.TransmitGlobalTime = DISABLE; // Optional: DISABLE or ENABLE
while (1) {
// Send the CAN message
if (HAL_CAN_AddTxMessage(&hcan, &TxHeader, TxData, &TxMailbox) != HAL_OK) {
// Error handling for transmission failure
printf("Transmission failed!\r\n");
} else {
printf("CAN message sent successfully!\r\n");
}
// Wait before sending the next message
HAL_Delay(1000); // 1 second delay between messages
}
}