Last active
December 26, 2020 20:55
-
-
Save owennewo/958fdfed3225c0c3a76b375c3edf12b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// THIS DOESN'T WORK. | |
// Discussed here https://www.stm32duino.com/viewtopic.php?f=7&t=832 | |
#include <Arduino.h> | |
#include "fdcan.h" | |
FDCAN_HandleTypeDef hfdcan1; | |
FDCAN_TxHeaderTypeDef tx_header; | |
void sendCanMessage() { | |
uint8_t TxData[2]; | |
TxData[0] = 1; | |
TxData[1] = 0xAD; | |
if (HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &tx_header, TxData) != HAL_OK) | |
{ | |
Serial.println("Error add"); | |
Error_Handler(); | |
} else { | |
Serial.println("Added"); | |
} | |
} | |
void pollCanMessage() { | |
uint8_t receiveData[8]; | |
FDCAN_RxHeaderTypeDef* receiveHeader = {}; | |
HAL_StatusTypeDef status = HAL_FDCAN_GetRxMessage(&hfdcan1, FDCAN_RX_FIFO0, receiveHeader, receiveData); | |
if ( status != HAL_OK) { | |
Serial.print("no data: "); Serial.println(status); | |
} else { | |
//we have data | |
Serial.println(receiveData[1]); | |
} | |
} | |
void setup() { | |
pinMode(CAN_TERM, OUTPUT); | |
pinMode(CAN_TX, OUTPUT); | |
pinMode(CAN_RX, INPUT); | |
// this enables the 120ohm resistor | |
digitalWrite(CAN_TERM, HIGH); | |
// hold low to enable can tranceiver | |
pinMode(CAN_SHDN, OUTPUT); | |
digitalWrite(CAN_SHDN, LOW); | |
Serial.begin(115200); | |
delay(1000); | |
Serial.println("setup start"); | |
tx_header.Identifier = 0x321; | |
tx_header.IdType = FDCAN_STANDARD_ID; | |
tx_header.TxFrameType = FDCAN_DATA_FRAME; | |
tx_header.DataLength = FDCAN_DLC_BYTES_2; | |
tx_header.ErrorStateIndicator = FDCAN_ESI_ACTIVE; | |
tx_header.BitRateSwitch = FDCAN_BRS_OFF; | |
tx_header.FDFormat = FDCAN_CLASSIC_CAN; | |
tx_header.TxEventFifoControl = FDCAN_NO_TX_EVENTS; | |
tx_header.MessageMarker = 0; | |
__HAL_RCC_GPIOB_CLK_ENABLE(); | |
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; | |
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_FDCAN; | |
PeriphClkInit.FdcanClockSelection = RCC_FDCANCLKSOURCE_PCLK1; | |
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) | |
{ | |
Error_Handler(); | |
} | |
pinmap_pinout(PB_9, PinMap_CAN_TD); | |
pinmap_pinout(PA_11, PinMap_CAN_RD); | |
hfdcan1.Instance = FDCAN1; | |
hfdcan1.Init.ClockDivider = FDCAN_CLOCK_DIV1; | |
hfdcan1.Init.FrameFormat = FDCAN_FRAME_FD_BRS; //FDCAN_FRAME_CLASSIC; // FDCAN_FRAME_FD_NO_BRS | |
hfdcan1.Init.Mode = FDCAN_MODE_NORMAL; // FDCAN_MODE_INTERNAL_LOOPBACK | FDCAN_MODE_NORMAL; | |
hfdcan1.Init.AutoRetransmission = DISABLE; | |
hfdcan1.Init.TransmitPause = DISABLE; | |
hfdcan1.Init.ProtocolException = DISABLE; | |
hfdcan1.Init.NominalPrescaler = 20;//1; | |
hfdcan1.Init.NominalSyncJumpWidth = 1; | |
hfdcan1.Init.NominalTimeSeg1 = 14; //2; | |
hfdcan1.Init.NominalTimeSeg2 = 2; | |
hfdcan1.Init.DataPrescaler = 1; | |
hfdcan1.Init.DataSyncJumpWidth = 1; | |
hfdcan1.Init.DataTimeSeg1 = 1; | |
hfdcan1.Init.DataTimeSeg2 = 1; | |
hfdcan1.Init.StdFiltersNbr = 1; //0; | |
hfdcan1.Init.ExtFiltersNbr = 0; | |
hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION; // FDCAN_TX_QUEUE_OPERATION; | |
Serial.println("setup before init"); | |
if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK) | |
{ | |
Error_Handler(); | |
} | |
Serial.println("setup after init"); | |
FDCAN_FilterTypeDef sFilterConfig; | |
/* Configure Rx filter */ | |
sFilterConfig.IdType = FDCAN_STANDARD_ID; | |
sFilterConfig.FilterIndex = 0; | |
sFilterConfig.FilterType = FDCAN_FILTER_MASK; | |
sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0; | |
sFilterConfig.FilterID1 = 0x321; | |
sFilterConfig.FilterID2 = 0x7FF; | |
if (HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig) != HAL_OK) | |
{ | |
Error_Handler(); | |
} | |
if (HAL_FDCAN_ConfigGlobalFilter(&hfdcan1, FDCAN_REJECT, FDCAN_REJECT, FDCAN_FILTER_REMOTE, FDCAN_FILTER_REMOTE) != HAL_OK) | |
{ | |
Error_Handler(); | |
} | |
/* Start the FDCAN module */ | |
if (HAL_FDCAN_Start(&hfdcan1) != HAL_OK) | |
{ | |
Error_Handler(); | |
} | |
if (HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0) != HAL_OK) | |
{ | |
Error_Handler(); | |
} | |
Serial.println("setup notifies"); | |
} | |
void loop() { | |
static long last_micros = 0; | |
long now = micros(); | |
if (now-last_micros > 1000000) { | |
sendCanMessage(); | |
last_micros = now; | |
delay(50); | |
pollCanMessage(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment