cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F746G-Disco + SN65HVD230 CAN Connection

cbodie
Associate

I am trying to use the CAN Peripheral on my STM32F746G-Disco board. I am using a Waveshare SN65HVD230 transceiver from Amazon. I have built a working CAN bus and verified that messages are being sent 1 per second, using a USB Peak Can.  

cbodie_0-1754576049743.png

When I try and send a message from the STM32Board it is not being picked up on the Peak can.

The code works fine in LOOPBACK mode but in NORMAL it isnt working. 

Here is a scope capture from the Rx pin on the can transceiver. 

cbodie_1-1754576336725.jpeg

 

Main function code

int main(void)
{

  /* USER CODE BEGIN 1 */
  setvbuf(stdin, NULL, _IONBF,0);

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  //
  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_CAN1_Init();
  MX_USART1_UART_Init();

  CAN_FilterTypeDef canfilter;
  canfilter.FilterActivation = CAN_FILTER_ENABLE;
  canfilter.FilterBank = 0;
  canfilter.FilterFIFOAssignment = CAN_FILTER_FIFO0;
  canfilter.FilterIdHigh = 0x0000;
  canfilter.FilterIdLow = 0x0000;
  canfilter.FilterMaskIdHigh = 0x0000;
  canfilter.FilterMaskIdLow = 0x0000;
  canfilter.FilterMode = CAN_FILTERMODE_IDMASK;
  canfilter.FilterScale = CAN_FILTERSCALE_32BIT;
  canfilter.SlaveStartFilterBank = 0; // Only needed for dual CANs

  HAL_CAN_ConfigFilter(&hcan1, &canfilter);

  if(HAL_CAN_Start(&hcan1) != HAL_OK){
	  Error_Handler();
  }

  CAN_TxHeaderTypeDef TxHeader;
  CAN_RxHeaderTypeDef RxHeader;
  uint8_t TxData[8] = "HELLOCAN";
  uint8_t RxData[8];
  uint32_t TxMailbox;

  	TxHeader.DLC = 8;             // Data length
    TxHeader.IDE = CAN_ID_STD;   // Standard ID
    TxHeader.RTR = CAN_RTR_DATA; // Data frame
    TxHeader.StdId = 0x321;      // CAN ID

    // Transmit message
    if (HAL_CAN_AddTxMessage(&hcan1, &TxHeader, TxData, &TxMailbox) != HAL_OK) {
      Error_Handler();
    }

    // Wait for message to be received (polling version)
    while (HAL_CAN_GetRxFifoFillLevel(&hcan1, CAN_RX_FIFO0) == 0);

    if (HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK) {
      Error_Handler();
    } else {
    	HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
    }

 

Here is my ioc config

cbodie_2-1754581237848.png

cbodie_4-1754581283533.png

 

Any help is greatly apricated! Thank you

 

2 REPLIES 2
Karl Yamashita
Principal

If you can receive CAN messages then transmitting should work as well. You haven't indicated if you scoped the CAN Tx pin from the STM32. Are you sure you have the correct pin connected to the CAN transceiver? There are 4 possible CAN Tx pins that can be used.

Who turned off the lights?

This is a scope of the Tx pin, going into the can transceiver. 

cbodie_0-1754594620317.jpeg

 


I haven't been able to receive the messages in the code. It gets stuck at the while code line, and doesn't move from it, but I was able to see it on the scope and on the USB PEAK CAN. 

cbodie_2-1754595050926.jpeg

 

The grey wire is Rx and the Purple wire is Tx. I already checked to make sure that the transceiver is getting 3.3V.The top PIN should be PB8 and the one beneath it is PB9 (according to the card that came in the packaging for the STM. 

 

What do you mean by GIGO?