From f740d2f3bc6241403a2a7ffe44b85b44bded4b7f Mon Sep 17 00:00:00 2001 From: adustm Date: Tue, 11 Apr 2017 15:36:05 +0200 Subject: [PATCH 01/12] Add an mbed API that allows the initialization of the CAN at the correct CAN bus frequency --- drivers/CAN.cpp | 11 +++++++++++ drivers/CAN.h | 9 +++++++++ hal/can_api.h | 7 ++++--- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/drivers/CAN.cpp b/drivers/CAN.cpp index 93d6f6315d7..219b6d0c29d 100644 --- a/drivers/CAN.cpp +++ b/drivers/CAN.cpp @@ -34,6 +34,17 @@ CAN::CAN(PinName rd, PinName td) : _can(), _irq() { can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this); } +CAN::CAN(PinName rd, PinName td, int f) : _can(), _irq() { + // No lock needed in constructor + + for (int i = 0; i < sizeof _irq / sizeof _irq[0]; i++) { + _irq[i].attach(donothing); + } + + can_init_freq(&_can, rd, td, f); + can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this); +} + CAN::~CAN() { // No lock needed in destructor can_irq_free(&_can); diff --git a/drivers/CAN.h b/drivers/CAN.h index 9a81e9c7874..edbb39228c4 100644 --- a/drivers/CAN.h +++ b/drivers/CAN.h @@ -111,6 +111,15 @@ class CAN { * @endcode */ CAN(PinName rd, PinName td); + + /** Initialize CAN interface and set the frequency + * + * @param rd the rd pin + * @param td the td pin + * @param f the bus frequency in hertz + */ + CAN(PinName rd, PinName td, int f); + virtual ~CAN(); /** Set the frequency of the CAN interface diff --git a/hal/can_api.h b/hal/can_api.h index 2a9182187e1..723342f0821 100644 --- a/hal/can_api.h +++ b/hal/can_api.h @@ -57,9 +57,10 @@ typedef void (*can_irq_handler)(uint32_t id, CanIrqType type); typedef struct can_s can_t; -void can_init (can_t *obj, PinName rd, PinName td); -void can_free (can_t *obj); -int can_frequency(can_t *obj, int hz); +void can_init (can_t *obj, PinName rd, PinName td); +void can_init_freq (can_t *obj, PinName rd, PinName td, int hz); +void can_free (can_t *obj); +int can_frequency (can_t *obj, int hz); void can_irq_init (can_t *obj, can_irq_handler handler, uint32_t id); void can_irq_free (can_t *obj); From a912d05f3b78ed4f65d07f0236b6b36d6751352a Mon Sep 17 00:00:00 2001 From: adustm Date: Mon, 24 Apr 2017 15:04:44 +0200 Subject: [PATCH 02/12] Add can_init_frequency to RENESAS targets hz is not used in can_init functions --- targets/TARGET_RENESAS/TARGET_RZ_A1H/can_api.c | 6 +++++- targets/TARGET_RENESAS/TARGET_VK_RZ_A1H/can_api.c | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/targets/TARGET_RENESAS/TARGET_RZ_A1H/can_api.c b/targets/TARGET_RENESAS/TARGET_RZ_A1H/can_api.c index 1c22489be26..4a6e86033cd 100644 --- a/targets/TARGET_RENESAS/TARGET_RZ_A1H/can_api.c +++ b/targets/TARGET_RENESAS/TARGET_RZ_A1H/can_api.c @@ -539,7 +539,7 @@ static void can4_bus_err_irq(void) { can_err_irq(CAN_4, IRQ_BUS); } -void can_init(can_t *obj, PinName rd, PinName td) { +void can_init_freq(can_t *obj, PinName rd, PinName td, int hz) { __IO uint32_t *dmy_ctr; /* determine the CAN to use */ @@ -575,6 +575,10 @@ void can_init(can_t *obj, PinName rd, PinName td) { pinmap_pinout(td, PinMap_CAN_TD); } +void can_init(can_t *obj, PinName rd, PinName td) { + can_init_freq(obj, rd, td, 0); +} + void can_free(can_t *obj) { /* disable CAN clock */ CPGSTBCR3 |= CPG_STBCR3_BIT_MSTP32; diff --git a/targets/TARGET_RENESAS/TARGET_VK_RZ_A1H/can_api.c b/targets/TARGET_RENESAS/TARGET_VK_RZ_A1H/can_api.c index 7b6cf12e4e3..3e431cb4b55 100644 --- a/targets/TARGET_RENESAS/TARGET_VK_RZ_A1H/can_api.c +++ b/targets/TARGET_RENESAS/TARGET_VK_RZ_A1H/can_api.c @@ -556,7 +556,7 @@ static void can4_bus_err_irq(void) { can_err_irq(CAN_4, IRQ_BUS); } -void can_init(can_t *obj, PinName rd, PinName td) { +void can_init_freq(can_t *obj, PinName rd, PinName td, int hz) { __IO uint32_t *dmy_ctr; /* determine the CAN to use */ @@ -592,6 +592,10 @@ void can_init(can_t *obj, PinName rd, PinName td) { pinmap_pinout(td, PinMap_CAN_TD); } +void can_init(can_t *obj, PinName rd, PinName td) { + can_init_freq(obj, rd, td, 0); +} + void can_free(can_t *obj) { /* disable CAN clock */ CPGSTBCR3 |= CPG_STBCR3_BIT_MSTP32; From 1fe20b281ac94d9f897a85108ceb64c922368c48 Mon Sep 17 00:00:00 2001 From: adustm Date: Mon, 24 Apr 2017 18:22:56 +0200 Subject: [PATCH 03/12] Add can_init_frequency for NXP platforms --- .../TARGET_LPC11CXX/can_api.c | 16 ++++++++++------ targets/TARGET_NXP/TARGET_LPC15XX/can_api.c | 9 ++++++--- targets/TARGET_NXP/TARGET_LPC176X/can_api.c | 8 ++++++-- .../TARGET_LPC408X/TARGET_LPC4088/can_api.c | 12 ++++++++---- .../TARGET_LPC408X/TARGET_LPC4088_DM/can_api.c | 8 ++++++-- 5 files changed, 36 insertions(+), 17 deletions(-) diff --git a/targets/TARGET_NXP/TARGET_LPC11XX_11CXX/TARGET_LPC11CXX/can_api.c b/targets/TARGET_NXP/TARGET_LPC11XX_11CXX/TARGET_LPC11CXX/can_api.c index 643dec6334b..dfb0da85bda 100644 --- a/targets/TARGET_NXP/TARGET_LPC11XX_11CXX/TARGET_LPC11CXX/can_api.c +++ b/targets/TARGET_NXP/TARGET_LPC11XX_11CXX/TARGET_LPC11CXX/can_api.c @@ -268,26 +268,30 @@ int can_config_rxmsgobj(can_t *obj) { } -void can_init(can_t *obj, PinName rd, PinName td) { +void can_init_freq(can_t *obj, PinName rd, PinName td, int hz) { // Enable power and clock LPC_SYSCON->PRESETCTRL |= PRESETCTRL_CAN_RST_N; LPC_SYSCON->SYSAHBCLKCTRL |= SYSAHBCLKCTRL_CAN; - + // Enable Initialization mode if (!(LPC_CAN->CNTL & CANCNTL_INIT)) { LPC_CAN->CNTL |= CANCNTL_INIT; } - - can_frequency(obj, 125000); - + + can_frequency(obj, hz); + // Resume operation LPC_CAN->CNTL &= ~CANCNTL_INIT; while ( LPC_CAN->CNTL & CANCNTL_INIT ); - + // Initialize RX message object can_config_rxmsgobj(obj); } +void can_init(can_t *obj, PinName rd, PinName td) { + can_init_freq(obj, rd, td, 125000); +} + void can_free(can_t *obj) { LPC_SYSCON->SYSAHBCLKCTRL &= ~(SYSAHBCLKCTRL_CAN); LPC_SYSCON->PRESETCTRL &= ~(PRESETCTRL_CAN_RST_N); diff --git a/targets/TARGET_NXP/TARGET_LPC15XX/can_api.c b/targets/TARGET_NXP/TARGET_LPC15XX/can_api.c index 26fcbf21701..d0f0ff307ea 100644 --- a/targets/TARGET_NXP/TARGET_LPC15XX/can_api.c +++ b/targets/TARGET_NXP/TARGET_LPC15XX/can_api.c @@ -415,8 +415,7 @@ int can_config_txmsgobj(can_t *obj) { return 1; } - -void can_init(can_t *obj, PinName rd, PinName td) { +void can_init_freq(can_t *obj, PinName rd, PinName td, int hz) { // Enable power and clock LPC_SYSCON->SYSAHBCLKCTRL1 |= (1UL << 7); LPC_SYSCON->PRESETCTRL1 |= (1UL << 7); @@ -430,7 +429,7 @@ void can_init(can_t *obj, PinName rd, PinName td) { LPC_SWM->PINASSIGN[6] &= ~(0x00FFFF00L); LPC_SWM->PINASSIGN[6] |= (rd << 16) | (td << 8); - can_frequency(obj, 100000); + can_frequency(obj, hz); // Resume operation LPC_C_CAN0->CANCNTL &= ~(1UL << 0); @@ -442,6 +441,10 @@ void can_init(can_t *obj, PinName rd, PinName td) { can_config_txmsgobj(obj); } +void can_init(can_t *obj, PinName rd, PinName td) { + can_init_freq(obj, rd, td, 100000); +} + void can_free(can_t *obj) { LPC_SYSCON->SYSAHBCLKCTRL1 &= ~(1UL << 7); LPC_SYSCON->PRESETCTRL1 &= ~(1UL << 7); diff --git a/targets/TARGET_NXP/TARGET_LPC176X/can_api.c b/targets/TARGET_NXP/TARGET_LPC176X/can_api.c index 75d6d7e633a..f1301c24c08 100644 --- a/targets/TARGET_NXP/TARGET_LPC176X/can_api.c +++ b/targets/TARGET_NXP/TARGET_LPC176X/can_api.c @@ -292,7 +292,7 @@ static unsigned int can_speed(unsigned int sclk, unsigned int pclk, unsigned int } -void can_init(can_t *obj, PinName rd, PinName td) { +void can_init_freq(can_t *obj, PinName rd, PinName td, int hz) { CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD); CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); obj->dev = (LPC_CAN_TypeDef *)pinmap_merge(can_rd, can_td); @@ -313,11 +313,15 @@ void can_init(can_t *obj, PinName rd, PinName td) { can_reset(obj); obj->dev->IER = 0; // Disable Interrupts - can_frequency(obj, 100000); + can_frequency(obj, hz); LPC_CANAF->AFMR = ACCF_BYPASS; // Bypass Filter } +void can_init(can_t *obj, PinName rd, PinName td) { + can_init_freq(obj, rd, td, 100000); +} + void can_free(can_t *obj) { switch ((int)obj->dev) { case CAN_1: LPC_SC->PCONP &= ~(1 << 13); break; diff --git a/targets/TARGET_NXP/TARGET_LPC408X/TARGET_LPC4088/can_api.c b/targets/TARGET_NXP/TARGET_LPC408X/TARGET_LPC4088/can_api.c index 34f1a04d248..aeda13e60a5 100644 --- a/targets/TARGET_NXP/TARGET_LPC408X/TARGET_LPC4088/can_api.c +++ b/targets/TARGET_NXP/TARGET_LPC408X/TARGET_LPC4088/can_api.c @@ -239,7 +239,7 @@ static unsigned int can_speed(unsigned int pclk, unsigned int cclk, unsigned cha } -void can_init(can_t *obj, PinName rd, PinName td) { +void can_init_freq(can_t *obj, PinName rd, PinName td, int hz) { CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD); CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); obj->dev = (LPC_CAN_TypeDef *)pinmap_merge(can_rd, can_td); @@ -252,19 +252,23 @@ void can_init(can_t *obj, PinName rd, PinName td) { pinmap_pinout(rd, PinMap_CAN_RD); pinmap_pinout(td, PinMap_CAN_TD); - + switch ((int)obj->dev) { case CAN_1: obj->index = 0; break; case CAN_2: obj->index = 1; break; } - + can_reset(obj); obj->dev->IER = 0; // Disable Interrupts - can_frequency(obj, 100000); + can_frequency(obj, hz); LPC_CANAF->AFMR = ACCF_BYPASS; // Bypass Filter } +void can_init(can_t *obj, PinName rd, PinName td) { + can_init_freq(obj, rd, td, 100000); +} + void can_free(can_t *obj) { switch ((int)obj->dev) { case CAN_1: LPC_SC->PCONP &= ~(1 << 13); break; diff --git a/targets/TARGET_NXP/TARGET_LPC408X/TARGET_LPC4088_DM/can_api.c b/targets/TARGET_NXP/TARGET_LPC408X/TARGET_LPC4088_DM/can_api.c index 676bc274010..03f8bb1977e 100644 --- a/targets/TARGET_NXP/TARGET_LPC408X/TARGET_LPC4088_DM/can_api.c +++ b/targets/TARGET_NXP/TARGET_LPC408X/TARGET_LPC4088_DM/can_api.c @@ -236,7 +236,7 @@ static unsigned int can_speed(unsigned int pclk, unsigned int cclk, unsigned cha } -void can_init(can_t *obj, PinName rd, PinName td) { +void can_init_freq(can_t *obj, PinName rd, PinName td, int hz) { CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD); CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); obj->dev = (LPC_CAN_TypeDef *)pinmap_merge(can_rd, can_td); @@ -257,11 +257,15 @@ void can_init(can_t *obj, PinName rd, PinName td) { can_reset(obj); obj->dev->IER = 0; // Disable Interrupts - can_frequency(obj, 100000); + can_frequency(obj, hz); LPC_CANAF->AFMR = ACCF_BYPASS; // Bypass Filter } +void can_init(can_t *obj, PinName rd, PinName td) { + can_init_freq(obj, rd, td, 100000); +} + void can_free(can_t *obj) { switch ((int)obj->dev) { case CAN_1: LPC_SC->PCONP &= ~(1 << 13); break; From 3d44a3fcc30970e02ff89ecc77320b9d58c9ea27 Mon Sep 17 00:00:00 2001 From: adustm Date: Mon, 24 Apr 2017 18:41:35 +0200 Subject: [PATCH 04/12] add can_init_freq for NUVOTON platforms --- targets/TARGET_NUVOTON/TARGET_M451/can_api.c | 16 +++++++++++----- targets/TARGET_NUVOTON/TARGET_NUC472/can_api.c | 16 +++++++++++----- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/targets/TARGET_NUVOTON/TARGET_M451/can_api.c b/targets/TARGET_NUVOTON/TARGET_M451/can_api.c index 2625298a781..0fd92d9a5ab 100644 --- a/targets/TARGET_NUVOTON/TARGET_M451/can_api.c +++ b/targets/TARGET_NUVOTON/TARGET_M451/can_api.c @@ -42,8 +42,8 @@ }; - void can_init(can_t *obj, PinName rd, PinName td) - { +void can_init_freq(can_t *obj, PinName rd, PinName td, int hz) +{ uint32_t can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); uint32_t can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD); obj->can = (CANName)pinmap_merge(can_td, can_rd); @@ -69,12 +69,18 @@ PA0 = 0x00; PA1 = 0x00; - CAN_Open((CAN_T *)NU_MODBASE(obj->can), 500000, CAN_NORMAL_MODE); + CAN_Open((CAN_T *)NU_MODBASE(obj->can), hz, CAN_NORMAL_MODE); can_filter(obj, 0, 0, CANStandard, 0); } - - + + +void can_init(can_t *obj, PinName rd, PinName td) +{ + can_init_freq(obj, rd, td, 500000); +} + + void can_free(can_t *obj) { diff --git a/targets/TARGET_NUVOTON/TARGET_NUC472/can_api.c b/targets/TARGET_NUVOTON/TARGET_NUC472/can_api.c index ad4d30582b0..e77948e8e31 100644 --- a/targets/TARGET_NUVOTON/TARGET_NUC472/can_api.c +++ b/targets/TARGET_NUVOTON/TARGET_NUC472/can_api.c @@ -43,8 +43,8 @@ {NC, 0, 0, 0, 0, (IRQn_Type) 0, NULL} }; - - void can_init(can_t *obj, PinName rd, PinName td) + + void can_init_freq(can_t *obj, PinName rd, PinName td, int hz) { uint32_t can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); uint32_t can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD); @@ -75,12 +75,18 @@ PA2 = 0x00; PA3 = 0x00; - CAN_Open((CAN_T *)NU_MODBASE(obj->can), 500000, CAN_NORMAL_MODE); + CAN_Open((CAN_T *)NU_MODBASE(obj->can), hz, CAN_NORMAL_MODE); can_filter(obj, 0, 0, CANStandard, 0); } - - + + +void can_init(can_t *obj, PinName rd, PinName td) +{ + can_init_freq(obj, rd, td, 500000); +} + + void can_free(can_t *obj) { From 9115dd9b9864051b6c818fe4149075e6eb7ce828 Mon Sep 17 00:00:00 2001 From: adustm Date: Mon, 24 Apr 2017 18:47:39 +0200 Subject: [PATCH 05/12] Add can_init_freq for STM devices --- targets/TARGET_STM/can_api.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/targets/TARGET_STM/can_api.c b/targets/TARGET_STM/can_api.c index 8b6340fedf8..46b58ef1b3e 100644 --- a/targets/TARGET_STM/can_api.c +++ b/targets/TARGET_STM/can_api.c @@ -30,6 +30,11 @@ static uint32_t can_irq_ids[CAN_NUM] = {0}; static can_irq_handler irq_handler; void can_init(can_t *obj, PinName rd, PinName td) +{ + can_init_freq(obj, rd, td, 100000); +} + +void can_init_freq (can_t *obj, PinName rd, PinName td, int hz) { CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD); CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD); @@ -80,8 +85,8 @@ void can_init(can_t *obj, PinName rd, PinName td) error("Cannot initialize CAN"); } - // Set initial CAN frequency to 100 kb/s - can_frequency(obj, 100000); + // Set initial CAN frequency to requested kb/s + can_frequency(obj, hz); uint32_t filter_number = (obj->can == CAN_1) ? 0 : 14; can_filter(obj, 0, 0, CANStandard, filter_number); @@ -227,16 +232,16 @@ int can_write(can_t *obj, CAN_Message msg, int cc) } else { can->sTxMailBox[transmitmailbox].TIR |= ((msg.id << 3) | CAN_ID_EXT | msg.type); } - + /* Set up the DLC */ can->sTxMailBox[transmitmailbox].TDTR &= (uint32_t)0xFFFFFFF0; can->sTxMailBox[transmitmailbox].TDTR |= (msg.len & (uint8_t)0x0000000F); - + /* Set up the data field */ can->sTxMailBox[transmitmailbox].TDLR = (((uint32_t)msg.data[3] << 24) | - ((uint32_t)msg.data[2] << 16) | - ((uint32_t)msg.data[1] << 8) | - ((uint32_t)msg.data[0])); + ((uint32_t)msg.data[2] << 16) | + ((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) | From 6c1fc983b7ca60f8582f4aa39c330ab15988eb01 Mon Sep 17 00:00:00 2001 From: adustm Date: Thu, 27 Apr 2017 14:30:39 +0200 Subject: [PATCH 06/12] RENESAS: add call to can_frequency and use hz parameter of can_init_freq --- targets/TARGET_RENESAS/TARGET_RZ_A1H/can_api.c | 5 ++++- targets/TARGET_RENESAS/TARGET_VK_RZ_A1H/can_api.c | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/targets/TARGET_RENESAS/TARGET_RZ_A1H/can_api.c b/targets/TARGET_RENESAS/TARGET_RZ_A1H/can_api.c index 4a6e86033cd..6a727b84979 100644 --- a/targets/TARGET_RENESAS/TARGET_RZ_A1H/can_api.c +++ b/targets/TARGET_RENESAS/TARGET_RZ_A1H/can_api.c @@ -573,10 +573,13 @@ void can_init_freq(can_t *obj, PinName rd, PinName td, int hz) { /* pin out the can pins */ pinmap_pinout(rd, PinMap_CAN_RD); pinmap_pinout(td, PinMap_CAN_TD); + + /* set can frequency */ + can_frequency(obj, hz); } void can_init(can_t *obj, PinName rd, PinName td) { - can_init_freq(obj, rd, td, 0); + can_init_freq(obj, rd, td, 100000); } void can_free(can_t *obj) { diff --git a/targets/TARGET_RENESAS/TARGET_VK_RZ_A1H/can_api.c b/targets/TARGET_RENESAS/TARGET_VK_RZ_A1H/can_api.c index 3e431cb4b55..7192cf1ce66 100644 --- a/targets/TARGET_RENESAS/TARGET_VK_RZ_A1H/can_api.c +++ b/targets/TARGET_RENESAS/TARGET_VK_RZ_A1H/can_api.c @@ -590,10 +590,13 @@ void can_init_freq(can_t *obj, PinName rd, PinName td, int hz) { /* pin out the can pins */ pinmap_pinout(rd, PinMap_CAN_RD); pinmap_pinout(td, PinMap_CAN_TD); + + /* set can frequency */ + can_frequency(obj, hz); } void can_init(can_t *obj, PinName rd, PinName td) { - can_init_freq(obj, rd, td, 0); + can_init_freq(obj, rd, td, 100000); } void can_free(can_t *obj) { From f027cf429bc3fc886a5d74e8ae9f2e9dca07308b Mon Sep 17 00:00:00 2001 From: jeromecoutant Date: Fri, 5 May 2017 17:47:24 +0200 Subject: [PATCH 07/12] Add new CAN API test MBED_A30 --- .../unsupported/tests/mbed/can_api/main.cpp | 85 +++++++++++++++++++ tools/tests.py | 13 +++ 2 files changed, 98 insertions(+) create mode 100644 features/unsupported/tests/mbed/can_api/main.cpp diff --git a/features/unsupported/tests/mbed/can_api/main.cpp b/features/unsupported/tests/mbed/can_api/main.cpp new file mode 100644 index 00000000000..db864615fb2 --- /dev/null +++ b/features/unsupported/tests/mbed/can_api/main.cpp @@ -0,0 +1,85 @@ +#include "mbed.h" +#include "test_env.h" + +#if !DEVICE_CAN +#error [NOT_SUPPORTED] CAN not supported +#endif + +#if defined(TARGET_LPC1549) +#define CAN_RD D9 +#define CAN_TD D8 +#elif defined(TARGET_LPC1768) || defined(TARGET_LPC4088) +#define CAN_RD p9 +#define CAN_TD p10 +#elif defined(TARGET_B96B_F446VE) +#define CAN_RD PD_0 +#define CAN_TD PD_1 +#elif defined(TARGET_VK_RZ_A1H) +#define CAN_RD P5_9 +#define CAN_TD P5_10 +#elif defined(TARGET_NUCLEO_F042K6) || \ + defined(TARGET_NUCLEO_F072RB) || \ + defined(TARGET_NUCLEO_F091RC) || \ + defined(TARGET_NUCLEO_F302R8) || \ + defined(TARGET_NUCLEO_F303RE) || \ + defined(TARGET_NUCLEO_F303K8) || \ + defined(TARGET_NUCLEO_F334R8) || \ + defined(TARGET_NUCLEO_F412ZG) || \ + defined(TARGET_DISCO_F429ZI) || \ + defined(TARGET_NUCLEO_F446RE) || \ + defined(TARGET_NUCLEO_F746ZG) || \ + defined(TARGET_NUCLEO_L432KC) || \ + defined(TARGET_DISCO_L476VG) || \ + defined(TARGET_NUCLEO_L476RG) +#define CAN_RD PA_11 +#define CAN_TD PA_12 +#elif defined(TARGET_NUCLEO_F103RB) || \ + defined(TARGET_NUCLEO_F207ZG) || \ + defined(TARGET_DISCO_F303VC) || \ + defined(TARGET_NUCLEO_F303ZE) || \ + defined(TARGET_NUCLEO_F412ZG) || \ + defined(TARGET_NUCLEO_F429ZI) || \ + defined(TARGET_NUCLEO_F439ZI) || \ + defined(TARGET_NUCLEO_F446ZE) || \ + defined(TARGET_DISCO_F469NI) || \ + defined(TARGET_DISCO_F746NG) || \ + defined(TARGET_NUCLEO_F756ZG) || \ + defined(TARGET_NUCLEO_F767ZI) || \ + defined(TARGET_DISCO_F769NI) || \ + defined(TARGET_NUCLEO_L486RG) +#define CAN_RD PB_8 +#define CAN_TD PB_9 +#endif + +int result = true; + +void Check_CAN_Frequency(int CanFrequency) +{ + printf("Init CAN at %d Hz\n", CanFrequency); + + CAN can_object(CAN_RD, CAN_TD, CanFrequency); + +#if !defined(TARGET_VK_RZ_A1H) + can_object.mode(CAN::Reset); +#endif + + if (!can_object.mode(CAN::LocalTest)) { + printf("CAN MODE_TEST_LOCAL failed\n"); + result = false; + } +} + +int main() +{ + MBED_HOSTTEST_TIMEOUT(20); + MBED_HOSTTEST_SELECT(dev_null); + MBED_HOSTTEST_DESCRIPTION(CAN API at different frequency); + MBED_HOSTTEST_START("MBED_A30"); + + const int frequency_table[] = {10000, 50000, 100000, 500000, 1000000}; + for (uint32_t i = 0; i < sizeof(frequency_table)/sizeof(int); i++) { + Check_CAN_Frequency(frequency_table[i]); + } + + MBED_HOSTTEST_RESULT(result); +} diff --git a/tools/tests.py b/tools/tests.py index de409473a76..dfdf3d4130e 100644 --- a/tools/tests.py +++ b/tools/tests.py @@ -339,6 +339,19 @@ "automated": True, "peripherals": ["i2c_loop"] }, + { + "id": "MBED_A30", "description": "CAN API", + "source_dir": join(TEST_DIR, "mbed", "can_api"), + "dependencies": [MBED_LIBRARIES, TEST_MBED_LIB], + "automated": True, + "mcu": ["LPC1549", "LPC1768","B96B_F446VE", "VK_RZ_A1H", + "NUCLEO_F091RC", "NUCLEO_F072RB", "NUCLEO_F042K6", "NUCLEO_F334R8", "NUCLEO_F207ZG", + "NUCLEO_F303RE", "NUCLEO_F303K8", "NUCLEO_F302R8", "NUCLEO_F446RE","NUCLEO_F446ZE", + "DISCO_F469NI", "DISCO_F429ZI", "NUCLEO_F103RB", "NUCLEO_F746ZG", + "NUCLEO_F429ZI", "NUCLEO_F439ZI", "NUCLEO_F756ZG", "NUCLEO_L486RG", + "DISCO_F746NG", "DISCO_L476VG", "NUCLEO_L476RG", "NUCLEO_L432KC", + "DISCO_F769NI", "NUCLEO_F767ZI", "DISCO_F303VC", "NUCLEO_F412ZG"] + }, { "id": "MBED_BLINKY", "description": "Blinky", "source_dir": join(TEST_DIR, "mbed", "blinky"), From 577a0d972b65080ef1160893bc9b8839b1ecec7f Mon Sep 17 00:00:00 2001 From: adustm Date: Thu, 11 May 2017 16:53:26 +0200 Subject: [PATCH 08/12] STM targets: the free irq function was not well implemented the index of the table was out of range --- targets/TARGET_STM/can_api.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/targets/TARGET_STM/can_api.c b/targets/TARGET_STM/can_api.c index 46b58ef1b3e..202a860bfb2 100644 --- a/targets/TARGET_STM/can_api.c +++ b/targets/TARGET_STM/can_api.c @@ -104,7 +104,7 @@ void can_irq_free(can_t *obj) can->IER &= ~(CAN_IT_FMP0 | CAN_IT_FMP1 | CAN_IT_TME | \ CAN_IT_ERR | CAN_IT_EPV | CAN_IT_BOF); - can_irq_ids[obj->can] = 0; + can_irq_ids[obj->index] = 0; } void can_free(can_t *obj) From f9b94d5f6c80140b65f4e0ba5db2cfae03419935 Mon Sep 17 00:00:00 2001 From: adustm Date: Thu, 11 May 2017 17:05:18 +0200 Subject: [PATCH 09/12] Add NUCLEO_F303ZE in MBED_A30 test list --- tools/tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/tests.py b/tools/tests.py index dfdf3d4130e..871834f5f66 100644 --- a/tools/tests.py +++ b/tools/tests.py @@ -346,7 +346,7 @@ "automated": True, "mcu": ["LPC1549", "LPC1768","B96B_F446VE", "VK_RZ_A1H", "NUCLEO_F091RC", "NUCLEO_F072RB", "NUCLEO_F042K6", "NUCLEO_F334R8", "NUCLEO_F207ZG", - "NUCLEO_F303RE", "NUCLEO_F303K8", "NUCLEO_F302R8", "NUCLEO_F446RE","NUCLEO_F446ZE", + "NUCLEO_F303RE", "NUCLEO_F303K8", "NUCLEO_F303ZE", "NUCLEO_F302R8", "NUCLEO_F446RE","NUCLEO_F446ZE", "DISCO_F469NI", "DISCO_F429ZI", "NUCLEO_F103RB", "NUCLEO_F746ZG", "NUCLEO_F429ZI", "NUCLEO_F439ZI", "NUCLEO_F756ZG", "NUCLEO_L486RG", "DISCO_F746NG", "DISCO_L476VG", "NUCLEO_L476RG", "NUCLEO_L432KC", From cd15589c45d9fd9bd4dc6bf5c27c452dd4bcc4a6 Mon Sep 17 00:00:00 2001 From: adustm Date: Thu, 11 May 2017 17:51:28 +0200 Subject: [PATCH 10/12] Add header to the test main.cpp --- features/unsupported/tests/mbed/can_api/main.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/features/unsupported/tests/mbed/can_api/main.cpp b/features/unsupported/tests/mbed/can_api/main.cpp index db864615fb2..9c753136f93 100644 --- a/features/unsupported/tests/mbed/can_api/main.cpp +++ b/features/unsupported/tests/mbed/can_api/main.cpp @@ -1,3 +1,18 @@ +/* mbed Microcontroller Library + * Copyright (c) 2017 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #include "mbed.h" #include "test_env.h" From a769d2b6a5be9546c92bba2789c46be07fc3d1fd Mon Sep 17 00:00:00 2001 From: adustm Date: Mon, 15 May 2017 15:06:29 +0200 Subject: [PATCH 11/12] add comment in peripheral pin of DISCO_L476VG so that the user is aware of conflicts --- .../TARGET_STM32L476xG/TARGET_DISCO_L476VG/PeripheralPins.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_DISCO_L476VG/PeripheralPins.c b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_DISCO_L476VG/PeripheralPins.c index 32d63680265..fef7dd6f1ed 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_DISCO_L476VG/PeripheralPins.c +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/TARGET_DISCO_L476VG/PeripheralPins.c @@ -275,8 +275,8 @@ const PinMap PinMap_SPI_SSEL[] = { }; const PinMap PinMap_CAN_RD[] = { - {PB_8 , CAN_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF9_CAN1)}, - {PA_11, CAN_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF9_CAN1)}, + {PB_8 , CAN_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF9_CAN1)}, // warning: pin used by gyroscope + {PA_11, CAN_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF9_CAN1)}, // warning: pin used by USB {NC, NC, 0} }; From b6581d317c1bf3a076e2588b45d346ef9ce3d441 Mon Sep 17 00:00:00 2001 From: adustm Date: Tue, 16 May 2017 10:37:50 +0200 Subject: [PATCH 12/12] Rename the f parameter into hz to avoid confusion with a loopcounter --- drivers/CAN.cpp | 4 ++-- drivers/CAN.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/CAN.cpp b/drivers/CAN.cpp index 219b6d0c29d..675a47f2382 100644 --- a/drivers/CAN.cpp +++ b/drivers/CAN.cpp @@ -34,14 +34,14 @@ CAN::CAN(PinName rd, PinName td) : _can(), _irq() { can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this); } -CAN::CAN(PinName rd, PinName td, int f) : _can(), _irq() { +CAN::CAN(PinName rd, PinName td, int hz) : _can(), _irq() { // No lock needed in constructor for (int i = 0; i < sizeof _irq / sizeof _irq[0]; i++) { _irq[i].attach(donothing); } - can_init_freq(&_can, rd, td, f); + can_init_freq(&_can, rd, td, hz); can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this); } diff --git a/drivers/CAN.h b/drivers/CAN.h index edbb39228c4..3f3c1cecda8 100644 --- a/drivers/CAN.h +++ b/drivers/CAN.h @@ -116,9 +116,9 @@ class CAN { * * @param rd the rd pin * @param td the td pin - * @param f the bus frequency in hertz + * @param hz the bus frequency in hertz */ - CAN(PinName rd, PinName td, int f); + CAN(PinName rd, PinName td, int hz); virtual ~CAN();