diff --git a/targets/TARGET_STM/TARGET_STM32F0/can_device.h b/targets/TARGET_STM/TARGET_STM32F0/can_device.h index c0ab4bf66d..cff47d6a4c 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/can_device.h +++ b/targets/TARGET_STM/TARGET_STM32F0/can_device.h @@ -17,6 +17,7 @@ #define MBED_CAN_DEVICE_H #include "cmsis.h" +#include "stm32f0xx_hal.h" #ifdef __cplusplus extern "C" { diff --git a/targets/TARGET_STM/TARGET_STM32F1/can_device.h b/targets/TARGET_STM/TARGET_STM32F1/can_device.h index d0e3ad0ce7..3792d0743e 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/can_device.h +++ b/targets/TARGET_STM/TARGET_STM32F1/can_device.h @@ -17,6 +17,7 @@ #define MBED_CAN_DEVICE_H #include "cmsis.h" +#include "stm32f1xx_hal.h" #ifdef __cplusplus extern "C" { diff --git a/targets/TARGET_STM/TARGET_STM32F2/can_device.h b/targets/TARGET_STM/TARGET_STM32F2/can_device.h index 8689988944..97a54210a0 100644 --- a/targets/TARGET_STM/TARGET_STM32F2/can_device.h +++ b/targets/TARGET_STM/TARGET_STM32F2/can_device.h @@ -17,6 +17,7 @@ #define MBED_CAN_DEVICE_H #include "cmsis.h" +#include "stm32f2xx_hal.h" #ifdef __cplusplus extern "C" { diff --git a/targets/TARGET_STM/TARGET_STM32F3/can_device.h b/targets/TARGET_STM/TARGET_STM32F3/can_device.h index 3337c51dc7..1d77768200 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/can_device.h +++ b/targets/TARGET_STM/TARGET_STM32F3/can_device.h @@ -17,6 +17,7 @@ #define MBED_CAN_DEVICE_H #include "cmsis.h" +#include "stm32f3xx_hal.h" #ifdef __cplusplus extern "C" { diff --git a/targets/TARGET_STM/TARGET_STM32F4/can_device.h b/targets/TARGET_STM/TARGET_STM32F4/can_device.h index 8689988944..ffc85b663c 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/can_device.h +++ b/targets/TARGET_STM/TARGET_STM32F4/can_device.h @@ -17,6 +17,7 @@ #define MBED_CAN_DEVICE_H #include "cmsis.h" +#include "stm32f4xx_hal.h" #ifdef __cplusplus extern "C" { diff --git a/targets/TARGET_STM/TARGET_STM32F7/can_device.h b/targets/TARGET_STM/TARGET_STM32F7/can_device.h index 8689988944..7f4f6bf253 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/can_device.h +++ b/targets/TARGET_STM/TARGET_STM32F7/can_device.h @@ -17,6 +17,7 @@ #define MBED_CAN_DEVICE_H #include "cmsis.h" +#include "stm32f7xx_hal.h" #ifdef __cplusplus extern "C" { diff --git a/targets/TARGET_STM/TARGET_STM32L4/can_device.h b/targets/TARGET_STM/TARGET_STM32L4/can_device.h index d0e3ad0ce7..3cb1c1b6c1 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/can_device.h +++ b/targets/TARGET_STM/TARGET_STM32L4/can_device.h @@ -17,6 +17,7 @@ #define MBED_CAN_DEVICE_H #include "cmsis.h" +#include "stm32l4xx_hal.h" #ifdef __cplusplus extern "C" { diff --git a/targets/TARGET_STM/can_api.c b/targets/TARGET_STM/can_api.c index 202a860bfb..332211e04a 100644 --- a/targets/TARGET_STM/can_api.c +++ b/targets/TARGET_STM/can_api.c @@ -85,8 +85,10 @@ void can_init_freq (can_t *obj, PinName rd, PinName td, int hz) error("Cannot initialize CAN"); } - // Set initial CAN frequency to requested kb/s - can_frequency(obj, hz); + // Set initial CAN frequency to 100 kb/s + if (can_frequency(obj, 100000) != 1) { + error("Can frequency could not be set\n"); + } uint32_t filter_number = (obj->can == CAN_1) ? 0 : 14; can_filter(obj, 0, 0, CANStandard, filter_number); @@ -195,19 +197,40 @@ int can_frequency(can_t *obj, int f) int pclk = HAL_RCC_GetPCLK1Freq(); int btr = can_speed(pclk, (unsigned int)f, 1); CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); + uint32_t tickstart = 0; + int status = 1; if (btr > 0) { can->MCR |= CAN_MCR_INRQ ; + /* Get tick */ + tickstart = HAL_GetTick(); while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) { + if ((HAL_GetTick() - tickstart) > 2) { + status = 0; + break; + } } - can->BTR = btr; - can->MCR &= ~(uint32_t)CAN_MCR_INRQ; - while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) { + if (status != 0) { + can->BTR = btr; + can->MCR &= ~(uint32_t)CAN_MCR_INRQ; + /* Get tick */ + tickstart = HAL_GetTick(); + while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) { + if ((HAL_GetTick() - tickstart) > 2) { + status = 0; + break; + } + } + if (status == 0) { + error("can ESR 0x%04x.%04x + timeout status %d", (can->ESR & 0xFFFF0000) >> 16, (can->ESR & 0xFFFF), status); + } + } else { + error("can init request timeout\n"); } - return 1; } else { - return 0; + status = 0; } + return status; } int can_write(can_t *obj, CAN_Message msg, int cc) @@ -243,8 +266,8 @@ int can_write(can_t *obj, CAN_Message msg, int cc) ((uint32_t)msg.data[1] << 8) | ((uint32_t)msg.data[0])); can->sTxMailBox[transmitmailbox].TDHR = (((uint32_t)msg.data[7] << 24) | - ((uint32_t)msg.data[6] << 16) | - ((uint32_t)msg.data[5] << 8) | + ((uint32_t)msg.data[6] << 16) | + ((uint32_t)msg.data[5] << 8) | ((uint32_t)msg.data[4])); /* Request transmission */ can->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ;