2020-12-09 12:49 AM
I have diferent STM32F103-NUCLEOs conected via buscan, all powered by external suply 12V (VIN pin). When I supply 12V, all NUCLEOs start working but are not able to communicate via buscan. Then I pulse black button (reset button) of NUCLEOs and it works!!! Why need to reset? Could I avoid reset step?
This is my code for one NUCLEO (the others are very similar):
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
CAN_HandleTypeDef hcan;
UART_HandleTypeDef huart2;
/* USER CODE BEGIN PV */
CAN_TxHeaderTypeDef txHeader;
CAN_RxHeaderTypeDef rxHeader;
CAN_FilterTypeDef canfil; //CAN Bus Filter
uint32_t TxMailbox; //CAN Bus Mail box variable
uint8_t canTX[8]={0,0,0,0,0,0,0,0}, canRX[8]={0}; //CAN Bus Data vars
uint8_t status=0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_CAN_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_CAN_Init();
/* USER CODE BEGIN 2 */
HAL_Delay(500);
//FILTRO CAN ADMITIMOS TODOS LOS MENSAJES
canfil.FilterBank = 0;
canfil.FilterMode = CAN_FILTERMODE_IDMASK;
canfil.FilterFIFOAssignment = CAN_RX_FIFO0;
canfil.FilterIdHigh = 0;
canfil.FilterIdLow = 0;
canfil.FilterMaskIdHigh = 0;
canfil.FilterMaskIdLow = 0;
canfil.FilterScale = CAN_FILTERSCALE_32BIT;
canfil.FilterActivation = ENABLE;
canfil.SlaveStartFilterBank = 14;
HAL_CAN_ConfigFilter(&hcan,&canfil); //Initialize CAN Filter
HAL_CAN_Start(&hcan);
HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING);
// set values to the transmit header
txHeader.DLC=8; // 8 byte
txHeader.IDE=CAN_ID_STD;
txHeader.RTR=CAN_RTR_DATA;
txHeader.StdId=0x244; //Id entre 0 y 0x7FF
txHeader.ExtId=0x02;
txHeader.TransmitGlobalTime=DISABLE;
canTX[4]=1;
HAL_CAN_AddTxMessage(&hcan, &txHeader, canTX, &TxMailbox); // Send CAN bus msg
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
canTX[4]=status; // Command value
HAL_CAN_AddTxMessage(&hcan, &txHeader, canTX, &TxMailbox); // Send CAN bus msg
HAL_Delay(1000); */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief CAN Initialization Function
* @param None
* @retval None
*/
static void MX_CAN_Init(void)
{
hcan.Instance = CAN1;
hcan.Init.Prescaler = 16;
hcan.Init.Mode = CAN_MODE_NORMAL;
hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan.Init.TimeSeg1 = CAN_BS1_4TQ;
hcan.Init.TimeSeg2 = CAN_BS2_2TQ;
hcan.Init.TimeTriggeredMode = DISABLE;
hcan.Init.AutoBusOff = DISABLE;
hcan.Init.AutoWakeUp = DISABLE;
hcan.Init.AutoRetransmission = DISABLE;
hcan.Init.ReceiveFifoLocked = DISABLE;
hcan.Init.TransmitFifoPriority = DISABLE;
if (HAL_CAN_Init(&hcan) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief USART2 Initialization Function
* @param None
* @retval None
*/
static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
/* USER CODE BEGIN 4 */
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcanx){
HAL_CAN_GetRxMessage(hcanx, CAN_RX_FIFO0, &rxHeader, canRX); //Receive CAN bus message to canRX buffer
if( canRX[3]==1){ //GET VALUE
canTX[4]=status; // Command value
HAL_CAN_AddTxMessage(&hcan, &txHeader, canTX, &TxMailbox); //
}else if(canRX[3]==2){ //SET VALUEs
if(canRX[4]==0){
//here some actions
}else if(canRX[4]==1){
// here others
}
status = canRX[4];
canTX[4]=status;
HAL_CAN_AddTxMessage(&hcan, &txHeader, canTX, &TxMailbox); // Send CAN bus msg
}
memset(canRX,0,8);
}
/* USER CODE END 4 */
2020-12-09 01:08 AM
Instrument the code paths and error handlers so you can understand the specific flow and failure in the first case.