Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions targets/TARGET_STM/TARGET_STM32F0/can_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define MBED_CAN_DEVICE_H

#include "cmsis.h"
#include "stm32f0xx_hal.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions targets/TARGET_STM/TARGET_STM32F1/can_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define MBED_CAN_DEVICE_H

#include "cmsis.h"
#include "stm32f1xx_hal.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions targets/TARGET_STM/TARGET_STM32F2/can_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define MBED_CAN_DEVICE_H

#include "cmsis.h"
#include "stm32f2xx_hal.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions targets/TARGET_STM/TARGET_STM32F3/can_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define MBED_CAN_DEVICE_H

#include "cmsis.h"
#include "stm32f3xx_hal.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions targets/TARGET_STM/TARGET_STM32F4/can_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define MBED_CAN_DEVICE_H

#include "cmsis.h"
#include "stm32f4xx_hal.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions targets/TARGET_STM/TARGET_STM32F7/can_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define MBED_CAN_DEVICE_H

#include "cmsis.h"
#include "stm32f7xx_hal.h"

#ifdef __cplusplus
extern "C" {
Expand Down
1 change: 1 addition & 0 deletions targets/TARGET_STM/TARGET_STM32L4/can_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define MBED_CAN_DEVICE_H

#include "cmsis.h"
#include "stm32l4xx_hal.h"

#ifdef __cplusplus
extern "C" {
Expand Down
41 changes: 32 additions & 9 deletions targets/TARGET_STM/can_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down