Compare commits
	
		
			3 Commits
		
	
	
		
			main
			...
			2ab77d75d6
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						
						
							
						
						2ab77d75d6
	
				 | 
					
					
						|||
| 
						
						
							
						
						1b79e5cbea
	
				 | 
					
					
						|||
| 
						
						
							
						
						7de8bbdc3b
	
				 | 
					
					
						
@ -24,6 +24,8 @@ When using the FDCAN peripheral (H7 series), you also need to define
 | 
				
			|||||||
`FTCAN_NUM_FILTERS` (and set it to the value of "Std Filters Nbr" you configured
 | 
					`FTCAN_NUM_FILTERS` (and set it to the value of "Std Filters Nbr" you configured
 | 
				
			||||||
in your `.ioc`).
 | 
					in your `.ioc`).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					If you want to use the dedicated buffers of the FDCAN, you must additionally define `FTCAN_NUM_TX_BUFFERS` (set to value of "Tx Buffers Nbr" in CubeMX).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
If you use
 | 
					If you use
 | 
				
			||||||
[VSCode with the stm-32-for-vscode extension](https://podio.com/fasttubede/modulubergreifend/apps/tech-tutorials/items/57),
 | 
					[VSCode with the stm-32-for-vscode extension](https://podio.com/fasttubede/modulubergreifend/apps/tech-tutorials/items/57),
 | 
				
			||||||
you can add these definitions in the `STM32-for-VSCode-config.yaml` file, e.g.:
 | 
					you can add these definitions in the `STM32-for-VSCode-config.yaml` file, e.g.:
 | 
				
			||||||
@ -33,6 +35,7 @@ you can add these definitions in the `STM32-for-VSCode-config.yaml` file, e.g.:
 | 
				
			|||||||
cDefinitions:
 | 
					cDefinitions:
 | 
				
			||||||
  - STM32H7
 | 
					  - STM32H7
 | 
				
			||||||
  - FTCAN_NUM_FILTERS=32
 | 
					  - FTCAN_NUM_FILTERS=32
 | 
				
			||||||
 | 
					  - FTCAN_NUM_TX_BUFFERS=10 # optional, only if using dedicated tx buffers
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Usage
 | 
					## Usage
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										63
									
								
								can-halal.c
									
									
									
									
									
								
							
							
						
						
									
										63
									
								
								can-halal.c
									
									
									
									
									
								
							@ -100,48 +100,51 @@ HAL_StatusTypeDef ftcan_init(FDCAN_HandleTypeDef *handle) {
 | 
				
			|||||||
  return HAL_FDCAN_Start(hcan);
 | 
					  return HAL_FDCAN_Start(hcan);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
HAL_StatusTypeDef ftcan_transmit(uint16_t id, const uint8_t *data,
 | 
					static FDCAN_TxHeaderTypeDef * fdcan_initialize_tx_header(FDCAN_TxHeaderTypeDef * header, uint16_t id, size_t datalen) {
 | 
				
			||||||
                                 size_t datalen) {
 | 
					  header->Identifier = id;
 | 
				
			||||||
  static FDCAN_TxHeaderTypeDef header;
 | 
					  header->IdType = FDCAN_STANDARD_ID;
 | 
				
			||||||
  header.Identifier = id;
 | 
					  header->TxFrameType = FDCAN_DATA_FRAME;
 | 
				
			||||||
  header.IdType = FDCAN_STANDARD_ID;
 | 
					 | 
				
			||||||
  header.TxFrameType = FDCAN_DATA_FRAME;
 | 
					 | 
				
			||||||
  switch (datalen) {
 | 
					  switch (datalen) {
 | 
				
			||||||
  case 0:
 | 
					  case 0:
 | 
				
			||||||
    header.DataLength = FDCAN_DLC_BYTES_0;
 | 
					    header->DataLength = FDCAN_DLC_BYTES_0;
 | 
				
			||||||
    break;
 | 
					    break;
 | 
				
			||||||
  case 1:
 | 
					  case 1:
 | 
				
			||||||
    header.DataLength = FDCAN_DLC_BYTES_1;
 | 
					    header->DataLength = FDCAN_DLC_BYTES_1;
 | 
				
			||||||
    break;
 | 
					    break;
 | 
				
			||||||
  case 2:
 | 
					  case 2:
 | 
				
			||||||
    header.DataLength = FDCAN_DLC_BYTES_2;
 | 
					    header->DataLength = FDCAN_DLC_BYTES_2;
 | 
				
			||||||
    break;
 | 
					    break;
 | 
				
			||||||
  case 3:
 | 
					  case 3:
 | 
				
			||||||
    header.DataLength = FDCAN_DLC_BYTES_3;
 | 
					    header->DataLength = FDCAN_DLC_BYTES_3;
 | 
				
			||||||
    break;
 | 
					    break;
 | 
				
			||||||
  case 4:
 | 
					  case 4:
 | 
				
			||||||
    header.DataLength = FDCAN_DLC_BYTES_4;
 | 
					    header->DataLength = FDCAN_DLC_BYTES_4;
 | 
				
			||||||
    break;
 | 
					    break;
 | 
				
			||||||
  case 5:
 | 
					  case 5:
 | 
				
			||||||
    header.DataLength = FDCAN_DLC_BYTES_5;
 | 
					    header->DataLength = FDCAN_DLC_BYTES_5;
 | 
				
			||||||
    break;
 | 
					    break;
 | 
				
			||||||
  case 6:
 | 
					  case 6:
 | 
				
			||||||
    header.DataLength = FDCAN_DLC_BYTES_6;
 | 
					    header->DataLength = FDCAN_DLC_BYTES_6;
 | 
				
			||||||
    break;
 | 
					    break;
 | 
				
			||||||
  case 7:
 | 
					  case 7:
 | 
				
			||||||
    header.DataLength = FDCAN_DLC_BYTES_7;
 | 
					    header->DataLength = FDCAN_DLC_BYTES_7;
 | 
				
			||||||
    break;
 | 
					    break;
 | 
				
			||||||
  case 8:
 | 
					  case 8:
 | 
				
			||||||
  default:
 | 
					  default:
 | 
				
			||||||
    header.DataLength = FDCAN_DLC_BYTES_8;
 | 
					    header->DataLength = FDCAN_DLC_BYTES_8;
 | 
				
			||||||
    break;
 | 
					    break;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  header.ErrorStateIndicator = FDCAN_ESI_PASSIVE;
 | 
					  header->ErrorStateIndicator = FDCAN_ESI_PASSIVE;
 | 
				
			||||||
  header.BitRateSwitch = FDCAN_BRS_OFF;
 | 
					  header->BitRateSwitch = FDCAN_BRS_OFF;
 | 
				
			||||||
  header.FDFormat = FDCAN_CLASSIC_CAN;
 | 
					  header->FDFormat = FDCAN_CLASSIC_CAN;
 | 
				
			||||||
  header.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
 | 
					  header->TxEventFifoControl = FDCAN_NO_TX_EVENTS;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return HAL_FDCAN_AddMessageToTxFifoQ(hcan, &header, data);
 | 
					  return header;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					HAL_StatusTypeDef ftcan_transmit(uint16_t id, const uint8_t *data, size_t datalen) {
 | 
				
			||||||
 | 
					    static FDCAN_TxHeaderTypeDef header;
 | 
				
			||||||
 | 
					    return HAL_FDCAN_AddMessageToTxFifoQ(hcan, fdcan_initialize_tx_header(&header, id, datalen), data);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
HAL_StatusTypeDef ftcan_add_filter(uint16_t id, uint16_t mask) {
 | 
					HAL_StatusTypeDef ftcan_add_filter(uint16_t id, uint16_t mask) {
 | 
				
			||||||
@ -217,6 +220,26 @@ void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *handle,
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  ftcan_msg_received_cb(header.Identifier, datalen, data);
 | 
					  ftcan_msg_received_cb(header.Identifier, datalen, data);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					HAL_StatusTypeDef ftcan_get_tx_queue_length(uint32_t *length) {
 | 
				
			||||||
 | 
					  if (length == NULL) {
 | 
				
			||||||
 | 
					    return HAL_ERROR;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  *length = HAL_FDCAN_GetTxFifoFreeLevel(hcan);
 | 
				
			||||||
 | 
					  return HAL_OK;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if FTCAN_NUM_TX_BUFFERS
 | 
				
			||||||
 | 
					static_assert(FDCAN_TX_BUFFER11 == 1 << 11 && FDCAN_TX_BUFFER21 == 1 << 21, "Your HAL version has a different definition for the transmit buffer indices. Please update your HAL version or change the code below.");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					HAL_StatusTypeDef ftcan_transmit_buffer(uint16_t id, const uint8_t *data, size_t datalen, uint32_t buffer_index) {
 | 
				
			||||||
 | 
					    static FDCAN_TxHeaderTypeDef header;
 | 
				
			||||||
 | 
					    if (buffer_index >= FTCAN_NUM_TX_BUFFERS) {
 | 
				
			||||||
 | 
					        return HAL_ERROR;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return HAL_FDCAN_AddMessageToTxBuffer(hcan, fdcan_initialize_tx_header(&header, id, datalen), data, 1 << buffer_index);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					#endif // FTCAN_NUM_TX_BUFFERS
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
__weak void ftcan_msg_received_cb(uint16_t id, size_t datalen,
 | 
					__weak void ftcan_msg_received_cb(uint16_t id, size_t datalen,
 | 
				
			||||||
 | 
				
			|||||||
@ -41,6 +41,13 @@ HAL_StatusTypeDef ftcan_init(FDCAN_HandleTypeDef *handle);
 | 
				
			|||||||
#error "Unknown CAN peripheral"
 | 
					#error "Unknown CAN peripheral"
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if defined(FTCAN_IS_FDCAN) && defined(FTCAN_NUM_TX_BUFFERS)
 | 
				
			||||||
 | 
					static_assert(FTCAN_NUM_TX_BUFFERS > 0,
 | 
				
			||||||
 | 
					               "If you want to use the dedicated transmit buffers, you must define FTCAN_NUM_TX_BUFFERS as configured in CubeMX (at least 1).");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					HAL_StatusTypeDef ftcan_transmit_buffer(uint16_t id, const uint8_t *data, size_t datalen, uint32_t buffer_index);
 | 
				
			||||||
 | 
					#endif // FTCAN_IS_FDCAN && FTCAN_NUM_TX_BUFFERS
 | 
				
			||||||
 | 
					
 | 
				
			||||||
HAL_StatusTypeDef ftcan_transmit(uint16_t id, const uint8_t *data,
 | 
					HAL_StatusTypeDef ftcan_transmit(uint16_t id, const uint8_t *data,
 | 
				
			||||||
                                 size_t datalen);
 | 
					                                 size_t datalen);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user