From 52d38a1be076c6fd5abdacfe3805dd0e7795d102 Mon Sep 17 00:00:00 2001 From: adustm Date: Thu, 27 Apr 2017 11:12:36 +0200 Subject: [PATCH 1/6] Handle can_frequency sync error add a timeout + return an error message --- targets/TARGET_STM/can_api.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/targets/TARGET_STM/can_api.c b/targets/TARGET_STM/can_api.c index 202a860bfb2..8d4b65cc445 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,34 @@ 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 = 0; if (btr > 0) { can->MCR |= CAN_MCR_INRQ ; while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) { } + /* Get tick */ + tickstart = HAL_GetTick(); can->BTR = btr; can->MCR &= ~(uint32_t)CAN_MCR_INRQ; while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) { + if((HAL_GetTick() - tickstart ) > 2) + { + status = HAL_CAN_STATE_TIMEOUT; + break; + } + } + if (status !=0) { + error("can ESR 0x%04x.%04x + timeout status %d", (can->ESR&0XFFFF0000)>>16, (can->ESR&0XFFFF), status); + status=0; + } else { + status=1; //mbed OK value } - return 1; } else { - return 0; + status=0; } + return status; } int can_write(can_t *obj, CAN_Message msg, int cc) @@ -243,8 +260,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; From 897625cc96822875c011452441f2a6cd63dd2fe2 Mon Sep 17 00:00:00 2001 From: adustm Date: Thu, 27 Apr 2017 18:13:59 +0200 Subject: [PATCH 2/6] Give access to HAL_GetTick function --- targets/TARGET_STM/TARGET_STM32F0/can_device.h | 1 + targets/TARGET_STM/TARGET_STM32F1/can_device.h | 1 + targets/TARGET_STM/TARGET_STM32F2/can_device.h | 1 + targets/TARGET_STM/TARGET_STM32F3/can_device.h | 1 + targets/TARGET_STM/TARGET_STM32F4/can_device.h | 1 + targets/TARGET_STM/TARGET_STM32F7/can_device.h | 1 + targets/TARGET_STM/TARGET_STM32L4/can_device.h | 1 + 7 files changed, 7 insertions(+) diff --git a/targets/TARGET_STM/TARGET_STM32F0/can_device.h b/targets/TARGET_STM/TARGET_STM32F0/can_device.h index c0ab4bf66d4..cff47d6a4c6 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 d0e3ad0ce72..3792d0743e9 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 86899889443..97a54210a06 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 3337c51dc77..1d777682008 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 86899889443..ffc85b663c2 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 86899889443..7f4f6bf253f 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 d0e3ad0ce72..3cb1c1b6c17 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" { From 9a1d05551ba785a158b260796b80b78757060321 Mon Sep 17 00:00:00 2001 From: adustm Date: Tue, 16 May 2017 13:32:47 +0200 Subject: [PATCH 3/6] Optimize the use of the status value --- targets/TARGET_STM/can_api.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/targets/TARGET_STM/can_api.c b/targets/TARGET_STM/can_api.c index 8d4b65cc445..c86b6ad6a0f 100644 --- a/targets/TARGET_STM/can_api.c +++ b/targets/TARGET_STM/can_api.c @@ -198,7 +198,7 @@ int can_frequency(can_t *obj, int f) int btr = can_speed(pclk, (unsigned int)f, 1); CAN_TypeDef *can = (CAN_TypeDef *)(obj->can); uint32_t tickstart = 0; - int status = 0; + int status = 1; if (btr > 0) { can->MCR |= CAN_MCR_INRQ ; @@ -211,15 +211,12 @@ int can_frequency(can_t *obj, int f) while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) { if((HAL_GetTick() - tickstart ) > 2) { - status = HAL_CAN_STATE_TIMEOUT; + status = 0; break; } } - if (status !=0) { + if (status ==0) { error("can ESR 0x%04x.%04x + timeout status %d", (can->ESR&0XFFFF0000)>>16, (can->ESR&0XFFFF), status); - status=0; - } else { - status=1; //mbed OK value } } else { status=0; From b99ccf65bd57a9e4e4d83e9bbcf8d1ab72659751 Mon Sep 17 00:00:00 2001 From: adustm Date: Tue, 16 May 2017 16:16:26 +0200 Subject: [PATCH 4/6] Add timeout also on the 1st while loop --- targets/TARGET_STM/can_api.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/targets/TARGET_STM/can_api.c b/targets/TARGET_STM/can_api.c index c86b6ad6a0f..4b7556a0a47 100644 --- a/targets/TARGET_STM/can_api.c +++ b/targets/TARGET_STM/can_api.c @@ -202,21 +202,32 @@ int can_frequency(can_t *obj, int f) if (btr > 0) { can->MCR |= CAN_MCR_INRQ ; - while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) { - } /* Get tick */ tickstart = HAL_GetTick(); - can->BTR = btr; - can->MCR &= ~(uint32_t)CAN_MCR_INRQ; - while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) { + 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); + 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"); } } else { status=0; From 795bfd2288b554d0ee99257e572106e305a11ea2 Mon Sep 17 00:00:00 2001 From: adustm Date: Mon, 22 May 2017 11:08:15 +0200 Subject: [PATCH 5/6] Fix coding style --- targets/TARGET_STM/can_api.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/targets/TARGET_STM/can_api.c b/targets/TARGET_STM/can_api.c index 4b7556a0a47..04d17b1c909 100644 --- a/targets/TARGET_STM/can_api.c +++ b/targets/TARGET_STM/can_api.c @@ -88,7 +88,7 @@ void can_init_freq (can_t *obj, PinName rd, PinName td, int 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); @@ -205,8 +205,7 @@ int can_frequency(can_t *obj, int f) /* Get tick */ tickstart = HAL_GetTick(); while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) { - if((HAL_GetTick() - tickstart ) > 2) - { + if ((HAL_GetTick() - tickstart ) > 2) { status = 0; break; } @@ -217,20 +216,19 @@ int can_frequency(can_t *obj, int f) /* Get tick */ tickstart = HAL_GetTick(); while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) { - if((HAL_GetTick() - tickstart ) > 2) - { + 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); + 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"); } } else { - status=0; + status = 0; } return status; } From 6770678811d0cadac8a11a10dafa71e7abcf73ab Mon Sep 17 00:00:00 2001 From: adustm Date: Tue, 23 May 2017 10:27:35 +0200 Subject: [PATCH 6/6] Fix another typo --- targets/TARGET_STM/can_api.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/targets/TARGET_STM/can_api.c b/targets/TARGET_STM/can_api.c index 04d17b1c909..332211e04a4 100644 --- a/targets/TARGET_STM/can_api.c +++ b/targets/TARGET_STM/can_api.c @@ -205,7 +205,7 @@ int can_frequency(can_t *obj, int f) /* Get tick */ tickstart = HAL_GetTick(); while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) { - if ((HAL_GetTick() - tickstart ) > 2) { + if ((HAL_GetTick() - tickstart) > 2) { status = 0; break; } @@ -216,7 +216,7 @@ int can_frequency(can_t *obj, int f) /* Get tick */ tickstart = HAL_GetTick(); while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) { - if ((HAL_GetTick() - tickstart ) > 2) { + if ((HAL_GetTick() - tickstart) > 2) { status = 0; break; }