Compare commits
	
		
			13 Commits
		
	
	
		
			eb74553f89
			...
			main
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 60dab2ade1 | |||
| 
						
						
							
						
						887f92167d
	
				 | 
					
					
						|||
| 
						
						
							
						
						4e3bb026f8
	
				 | 
					
					
						|||
| 0b91166780 | |||
| 5c90c7cda4 | |||
| 6f6cbf1d1e | |||
| 80068a98fa | |||
| de2c25828b | |||
| 8cca72d90d | |||
| 433a142732 | |||
| f151e3a4fe | |||
| 66983c30d3 | |||
| b2929ad664 | 
							
								
								
									
										45
									
								
								README.md
									
									
									
									
									
								
							
							
						
						@ -18,11 +18,23 @@ is included:
 | 
			
		||||
 | 
			
		||||
- `STM32F3`
 | 
			
		||||
- `STM32H7`
 | 
			
		||||
- `STM32F0`
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
in your `.ioc`).
 | 
			
		||||
 | 
			
		||||
If you use
 | 
			
		||||
[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.:
 | 
			
		||||
 | 
			
		||||
```yaml
 | 
			
		||||
# Compiler definitions. The -D prefix for the compiler will be automatically added.
 | 
			
		||||
cDefinitions:
 | 
			
		||||
  - STM32H7
 | 
			
		||||
  - FTCAN_NUM_FILTERS=32
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## Usage
 | 
			
		||||
 | 
			
		||||
1. Include `can-halal.h`
 | 
			
		||||
@ -31,3 +43,36 @@ in your `.ioc`).
 | 
			
		||||
4. To transmit messages, call `ftcan_transmit()`
 | 
			
		||||
5. When a message is received, `ftcan_msg_received_cb()` is called. It has a
 | 
			
		||||
   default empty implementation, which you can simply override.
 | 
			
		||||
 | 
			
		||||
## Enabling CAN in STM32CubeMX
 | 
			
		||||
 | 
			
		||||
This isn't specific to `can-halal`, but for completeness sake is included here.
 | 
			
		||||
 | 
			
		||||
### bxCAN (e.g. STM32F3xx)
 | 
			
		||||
 | 
			
		||||
1. Enable the CAN peripheral  
 | 
			
		||||
   
 | 
			
		||||
2. Setup the [bit timings](http://bittiming.can-wiki.info/).  
 | 
			
		||||
   **Note:** the baud rate depends on your system clock, so make sure that is
 | 
			
		||||
   setup correctly first!  
 | 
			
		||||
   
 | 
			
		||||
3. Make sure the CAN_RX0 interrupt is enabled  
 | 
			
		||||
   
 | 
			
		||||
 | 
			
		||||
### FDCAN (e.g. STM32H7xx)
 | 
			
		||||
 | 
			
		||||
1. Enable the CAN peripheral  
 | 
			
		||||
   
 | 
			
		||||
2. Setup the frame format, nominal SJW, filters, and FIFOs. The numbers for
 | 
			
		||||
   filters/FIFOs in the screenshot are examples.  
 | 
			
		||||
   **Note:** You need to tell `can-halal` about the number of filters by
 | 
			
		||||
   defining `FTCAN_NUM_FILTERS` (see above).  
 | 
			
		||||
   
 | 
			
		||||
3. Setup the [bit timings](http://bittiming.can-wiki.info/). We only use CAN in
 | 
			
		||||
   classic mode, not FD mode, so we only need to worry about the nominal bit
 | 
			
		||||
   timings  
 | 
			
		||||
   **Note:** the baud rate depends on your system clock, so make sure that is
 | 
			
		||||
   setup correctly first!  
 | 
			
		||||
   
 | 
			
		||||
4. Make sure the interrupts are enabled
 | 
			
		||||
   
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										20
									
								
								can-halal.c
									
									
									
									
									
								
							
							
						
						@ -32,14 +32,14 @@ HAL_StatusTypeDef ftcan_add_filter(uint16_t id, uint16_t mask) {
 | 
			
		||||
  static uint32_t next_filter_no = 0;
 | 
			
		||||
  static CAN_FilterTypeDef filter;
 | 
			
		||||
  if (next_filter_no % 2 == 0) {
 | 
			
		||||
    filter.FilterIdHigh = id;
 | 
			
		||||
    filter.FilterMaskIdHigh = mask;
 | 
			
		||||
    filter.FilterIdLow = 0;
 | 
			
		||||
    filter.FilterMaskIdLow = 0xFFFF;
 | 
			
		||||
    filter.FilterIdHigh = id << 5;
 | 
			
		||||
    filter.FilterMaskIdHigh = mask << 5;
 | 
			
		||||
    filter.FilterIdLow = id << 5;
 | 
			
		||||
    filter.FilterMaskIdLow = mask << 5;
 | 
			
		||||
  } else {
 | 
			
		||||
    // Leave high filter untouched from the last configuration
 | 
			
		||||
    filter.FilterIdLow = id;
 | 
			
		||||
    filter.FilterMaskIdLow = mask;
 | 
			
		||||
    filter.FilterIdLow = id << 5;
 | 
			
		||||
    filter.FilterMaskIdLow = mask << 5;
 | 
			
		||||
  }
 | 
			
		||||
  filter.FilterFIFOAssignment = CAN_FILTER_FIFO0;
 | 
			
		||||
  filter.FilterBank = next_filter_no / 2;
 | 
			
		||||
@ -220,7 +220,11 @@ void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *handle,
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
__weak void ftcan_msg_received_cb(uint16_t id, size_t datalen,
 | 
			
		||||
                                  const uint8_t *data) {}
 | 
			
		||||
                            const uint8_t *data) {
 | 
			
		||||
  (void)id;
 | 
			
		||||
  (void)datalen;
 | 
			
		||||
  (void)data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint64_t ftcan_unmarshal_unsigned(const uint8_t **data_ptr, size_t num_bytes) {
 | 
			
		||||
  if (num_bytes > 8) {
 | 
			
		||||
@ -257,7 +261,7 @@ uint8_t *ftcan_marshal_unsigned(uint8_t *data, uint64_t val, size_t num_bytes) {
 | 
			
		||||
    num_bytes = 8;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  for (size_t i = num_bytes - 1; i >= 0; i++) {
 | 
			
		||||
  for (int i = num_bytes - 1; i >= 0; i--) {
 | 
			
		||||
    data[i] = val & 0xFF;
 | 
			
		||||
    val >>= 8;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										33
									
								
								can-halal.h
									
									
									
									
									
								
							
							
						
						@ -1,15 +1,36 @@
 | 
			
		||||
#ifndef CAN_HALAL_H
 | 
			
		||||
#define CAN_HALAL_H
 | 
			
		||||
 | 
			
		||||
// Define family macros if none are defined and we recognize a chip macro
 | 
			
		||||
#if !defined(STM32F3) && !defined(STM32H7) && !defined(STM32F0)
 | 
			
		||||
#if defined(STM32F302x6) || defined(STM32F302x8) || defined(STM32F302xB) ||    \
 | 
			
		||||
    defined(STM32F302xC)
 | 
			
		||||
#define STM32F3
 | 
			
		||||
#endif
 | 
			
		||||
#if defined(STM32H7A3xx) || defined(STM32H723xx) || defined(STM32H753xx)
 | 
			
		||||
#define STM32H7
 | 
			
		||||
#endif
 | 
			
		||||
#if defined(STM32F042x6)
 | 
			
		||||
#define STM32F0
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#if defined(STM32F3)
 | 
			
		||||
#include "stm32f3xx_hal.h"
 | 
			
		||||
#define FTCAN_IS_BXCAN
 | 
			
		||||
#define FTCAN_NUM_FILTERS 13
 | 
			
		||||
    #include "stm32f3xx_hal.h"
 | 
			
		||||
    #define FTCAN_IS_BXCAN
 | 
			
		||||
    #define FTCAN_NUM_FILTERS 13
 | 
			
		||||
#elif defined(STM32H7)
 | 
			
		||||
#include "stm32h7xx_hal.h"
 | 
			
		||||
#define FTCAN_IS_FDCAN
 | 
			
		||||
    #include "stm32h7xx_hal.h"
 | 
			
		||||
    #define FTCAN_IS_FDCAN
 | 
			
		||||
    #ifndef FTCAN_NUM_FILTERS
 | 
			
		||||
        #error "Please configure the number of filters in CubeMX, and then add a compiler define for FTCAN_NUM_FILTERS"
 | 
			
		||||
    #endif
 | 
			
		||||
#elif defined(STM32F0)
 | 
			
		||||
    #include "stm32f0xx_hal.h"
 | 
			
		||||
    #define FTCAN_IS_BXCAN
 | 
			
		||||
    #define FTCAN_NUM_FILTERS 13
 | 
			
		||||
#else
 | 
			
		||||
#error "Couldn't detect STM family"
 | 
			
		||||
    #error "Couldn't detect STM family"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#if defined(FTCAN_IS_BXCAN)
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								doc/bxcan-activate.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 28 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								doc/bxcan-bittimings.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 41 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								doc/bxcan-interrupt.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 38 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								doc/fdcan-activate.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 21 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								doc/fdcan-basic.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 45 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								doc/fdcan-bittimings.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 25 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								doc/fdcan-interrupt.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 17 KiB  |