mirror of
git://soft.sys114.com/klipper
synced 2026-02-13 10:10:33 +09:00
stm32: Improved CAN support for STM32 (#2976)
Reworked the STM32F0 CAN bus implementation. It's more robust and higher performance. Added support for function remapping to different pins. API is emulating an STM32F0. Improved and ported CAN bus to STM32F0, F1 and F4. Signed-off-by: Pontus Borg <glpontus@gmail.com>
This commit is contained in:
@@ -64,6 +64,18 @@ gpio_clock_enable(GPIO_TypeDef *regs)
|
||||
RCC->APB2ENR;
|
||||
}
|
||||
|
||||
|
||||
static void stm32f1_alternative_remap(uint32_t mapr_mask, uint32_t mapr_value)
|
||||
{
|
||||
// The MAPR register is a mix of write only and r/w bits
|
||||
// We have to save the written values in a global variable
|
||||
static uint32_t mapr = 0;
|
||||
|
||||
mapr &= ~mapr_mask;
|
||||
mapr |= mapr_value;
|
||||
AFIO->MAPR = mapr;
|
||||
}
|
||||
|
||||
// Set the mode and extended function of a pin
|
||||
void
|
||||
gpio_peripheral(uint32_t gpio, uint32_t mode, int pullup)
|
||||
@@ -105,9 +117,23 @@ gpio_peripheral(uint32_t gpio, uint32_t mode, int pullup)
|
||||
|
||||
if (gpio == GPIO('A', 13) || gpio == GPIO('A', 14))
|
||||
// Disable SWD to free PA13, PA14
|
||||
AFIO->MAPR = AFIO_MAPR_SWJ_CFG_DISABLE;
|
||||
stm32f1_alternative_remap(AFIO_MAPR_SWJ_CFG_Msk,
|
||||
AFIO_MAPR_SWJ_CFG_DISABLE);
|
||||
|
||||
// STM32F1 remaps functions to pins in a very different
|
||||
// way from other STM32s.
|
||||
// Code below is emulating a few mappings to work like an STM32F4
|
||||
uint32_t func = (mode >> 4) & 0xf;
|
||||
if(( gpio == GPIO('B', 8) || gpio == GPIO('B', 9)) &&
|
||||
func == 9) { // CAN
|
||||
stm32f1_alternative_remap(AFIO_MAPR_CAN_REMAP_Msk,
|
||||
AFIO_MAPR_CAN_REMAP_REMAP2);
|
||||
}
|
||||
// Add more as needed
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Handle USB reboot requests
|
||||
void
|
||||
usb_request_bootloader(void)
|
||||
@@ -180,7 +206,8 @@ armcm_main(void)
|
||||
|
||||
// Disable JTAG to free PA15, PB3, PB4
|
||||
enable_pclock(AFIO_BASE);
|
||||
AFIO->MAPR = AFIO_MAPR_SWJ_CFG_JTAGDISABLE;
|
||||
stm32f1_alternative_remap(AFIO_MAPR_SWJ_CFG_Msk,
|
||||
AFIO_MAPR_SWJ_CFG_JTAGDISABLE);
|
||||
|
||||
sched_main();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user