2024-08-27
06:59 PM
- last edited on
2024-08-30
03:13 AM
by
Lina_DABASINSKA
You can see my code
The 'send_frame' / 'CAN_CMD_STD' functions are about CAN bus transmission.
Removing 'CAN_CMD_STD' code will make all my code work.
In particular, the 'data_set1' array can contain appropriate adc values.
But if I add 'CAN_CMD_STD' code to 'send_frame' code, array 'data_set1' is not charged.
The transfer speed is so fast that I thought, so I used the delay, but it has no effect....
(And in fact I could get new values in my code when I really try transfer to other boards)
Can I get help?? thank you.
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2021 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "math.h"
#include "string.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;
CAN_HandleTypeDef hcan1;
TIM_HandleTypeDef htim6;
/* USER CODE BEGIN PV */
HAL_StatusTypeDef ret;
/* TIMER6 Variables */
uint32_t time_1ms;
uint32_t Tim6_Flag_1ms;
uint32_t Tim6_Flag_2ms;
uint32_t Tim6_Flag_10ms;
uint32_t Tim6_Flag_500ms;
/* CAN Variables */
CAN_FilterTypeDef sFilterConfig;
CAN_TxHeaderTypeDef TxHeader;
uint8_t TxData[8];
uint8_t RxData0[8];
uint8_t RxData1[8];
uint32_t TxMailbox;
uint8_t data_set1[8], data_set2[8], data_set3[8]; //8-16
uint16_t CAN_ID[3] = {0x10, 0x11, 0x12};
/* TOUCH SENSOR emgVariables */
// Encoder variables
// ADC varibales
/* EBIMU Variables */
//uint8_t acc_L[16] = {0}, acc_T[16] = {0};
//uint8_t acc_U[16] = {0};
//uint8_t acc_M[16] = {0};
//short SOP = (0x55<<8 | 0x55), SOP2 = 0;
//short SOP1 = 0, SOP3 = 0;
//short SOP4 = 0;
//short acc_Lx = 0, acc_Ly = 0, acc_Lz = 0, acc_Tx = 0, acc_Ty = 0, acc_Tz = 0;
//short acc_Ux = 0, acc_Uy = 0, acc_Uz = 0;
//short acc_Mx = 0, acc_My = 0, acc_Mz = 0;
//float acc_LA[3], acc_TR[3];
//float acc_UA[3];
//float acc_MA[3];
/* EMG Variables */
uint16_t emg[5];
uint16_t dat[5];
uint16_t cnt = 0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_CAN1_Init(void);
static void MX_TIM6_Init(void);
static void MX_ADC1_Init(void);
/* USER CODE BEGIN PFP */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);
HAL_StatusTypeDef CAN_Setting(void);
static void Can_FilterConfig(CAN_FilterTypeDef* sFilterConfig, uint32_t FilterBank, uint32_t FilterMode, uint32_t FilterScale,
uint32_t FilterIdHigh, uint32_t FilterIdLow, uint32_t FilterMaskIdHigh, uint32_t FilterMaskIdLow, uint32_t FilterFIFOAssignment,
uint32_t FilterActivation, uint32_t SlaveStartFilterBank);
void CAN_CMD_STD(uint16_t id, uint8_t length, uint8_t* data);
void send_frame(void);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) {
// Continuous ADC conversion of Analog inputs is complete and stored in the data structure to be used later.
dat[0] = emg[0];
dat[1] = emg[1];
dat[2] = emg[2];
dat[3] = emg[3];
dat[4] = emg[4];
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_CAN1_Init();
MX_TIM6_Init();
MX_ADC1_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim6);
while(CAN_Setting() != HAL_OK){};
HAL_Delay(500);
HAL_TIM_Base_Start_IT(&htim6);
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&emg, 5);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
/* 250HZ LOOP */
HAL_Delay(500);
if(Tim6_Flag_500ms)
{
send_frame();
}
/*if(Tim6_Flag_1ms)
{*/
/* EBIMU SENSOR 1*/
/*
ret = HAL_UART_Receive_DMA(&huart1, acc_L, 16);
SOP1 = (acc_L[0]<<8 | acc_L[1]);
if(ret == HAL_OK && SOP1 == SOP)
{
acc_Lx = (acc_L[8]<<8 | acc_L[9]); acc_Ly = (acc_L[10]<<8 | acc_L[11]); acc_Lz = (acc_L[12]<<8 | acc_L[13]);
acc_LA[0] = acc_Lx * 0.001; acc_LA[1] = acc_Ly * 0.001; acc_LA[2] = acc_Lz * 0.001;
}
*/
/* EBIMU SENSOR 2*/
/*
ret = HAL_UART_Receive_DMA(&huart3, acc_U, 16);
SOP2 = (acc_U[0]<<8 | acc_U[1]);
if(ret == HAL_OK && SOP2 == SOP)
{
acc_Ux = (acc_U[8]<<8 | acc_U[9]); acc_Uy = (acc_U[10]<<8 | acc_U[11]); acc_Uz = (acc_U[12]<<8 | acc_U[13]);
acc_UA[0] = acc_Ux * 0.001; acc_UA[1] = acc_Uy * 0.001; acc_UA[2] = acc_Uz * 0.001;
}
*/
/* EBIMU SENSOR 3*/
/*
ret = HAL_UART_Receive_DMA(&huart6, acc_T, 16);
SOP3 = (acc_T[0]<<8 | acc_T[1]);
if(ret == HAL_OK && SOP3 == SOP)
{
acc_Tx = (acc_T[8]<<8 | acc_T[9]); acc_Ty = (acc_T[10]<<8 | acc_T[11]); acc_Tz = (acc_T[12]<<8 | acc_T[13]);
acc_TR[0] = acc_Tx * 0.001; acc_TR[1] = acc_Ty * 0.001; acc_TR[2] = acc_Tz * 0.001;
}
*/
/* EBIMU SENSOR 4*/
/*
ret = HAL_UART_Receive_DMA(&huart3, acc_M, 16);
SOP4 = (acc_M[0]<<8 | acc_M[1]);
if(ret == HAL_OK && SOP4 == SOP)
{
acc_Mx = (acc_M[8]<<8 | acc_M[9]); acc_My = (acc_M[10]<<8 | acc_M[11]); acc_Mz = (acc_M[12]<<8 | acc_M[13]);
acc_MA[0] = acc_Mx * 0.001; acc_MA[1] = acc_My * 0.001; acc_MA[2] = acc_Mz * 0.001;
}
*/
/* FLAG Reset */
/*Tim6_Flag_1ms = 0;
}*/
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** 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.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
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_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief ADC1 Initialization Function
* None
* @retval None
*/
static void MX_ADC1_Init(void)
{
/* USER CODE BEGIN ADC1_Init 0 */
/* USER CODE END ADC1_Init 0 */
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC1_Init 1 */
/* USER CODE END ADC1_Init 1 */
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = ENABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 5;
hadc1.Init.DMAContinuousRequests = ENABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_11;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_56CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_12;
sConfig.Rank = 2;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_13;
sConfig.Rank = 3;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_14;
sConfig.Rank = 4;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_15;
sConfig.Rank = 5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
/* USER CODE END ADC1_Init 2 */
}
/**
* @brief CAN1 Initialization Function
* None
* @retval None
*/
static void MX_CAN1_Init(void)
{
/* USER CODE BEGIN CAN1_Init 0 */
/* USER CODE END CAN1_Init 0 */
/* USER CODE BEGIN CAN1_Init 1 */
/* USER CODE END CAN1_Init 1 */
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 7;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_15TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_8TQ;
hcan1.Init.TimeTriggeredMode = DISABLE;
hcan1.Init.AutoBusOff = ENABLE;
hcan1.Init.AutoWakeUp = ENABLE;
hcan1.Init.AutoRetransmission = ENABLE;
hcan1.Init.ReceiveFifoLocked = DISABLE;
hcan1.Init.TransmitFifoPriority = DISABLE;
if (HAL_CAN_Init(&hcan1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN CAN1_Init 2 */
/* USER CODE END CAN1_Init 2 */
}
/**
* @brief TIM6 Initialization Function
* None
* @retval None
*/
static void MX_TIM6_Init(void)
{
/* USER CODE BEGIN TIM6_Init 0 */
/* USER CODE END TIM6_Init 0 */
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM6_Init 1 */
/* USER CODE END TIM6_Init 1 */
htim6.Instance = TIM6;
htim6.Init.Prescaler = 83;
htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
htim6.Init.Period = 999;
htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM6_Init 2 */
/* USER CODE END TIM6_Init 2 */
}
/**
* Enable DMA controller clock
*/
static void MX_DMA_Init(void)
{
/* DMA controller clock enable */
__HAL_RCC_DMA2_CLK_ENABLE();
/* DMA interrupt init */
/* DMA2_Stream0_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);
}
/**
* @brief GPIO Initialization Function
* None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/*Configure GPIO pins : PC10 PC11 */
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
/* TIMER setting */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == htim6.Instance) time_1ms++;
if((time_1ms % 1) == 0) Tim6_Flag_1ms = 1;
if((time_1ms % 2) == 0) Tim6_Flag_2ms = 1;
if((time_1ms % 10) == 0) Tim6_Flag_10ms = 1;
if((time_1ms % 500) == 0) Tim6_Flag_500ms = 1;
}
/* CAN Setting */
HAL_StatusTypeDef CAN_Setting(void)
{
Can_FilterConfig(&sFilterConfig, 0, CAN_FILTERMODE_IDLIST, CAN_FILTERSCALE_16BIT, 0x10, 0x11, 0x12, 0x13, CAN_FILTER_FIFO0, ENABLE, 14);
if (HAL_CAN_Start(&hcan1) != HAL_OK)
{
/* START ERROR */
Error_Handler();
}
if (HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
{
/* NOTIFICATION ERROR */
Error_Handler();
}
if (HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO1_MSG_PENDING) != HAL_OK)
{
/* NOTIFICATION ERROR */
Error_Handler();
}
return HAL_OK;
}
/* CAN FILTER Setting */
static void Can_FilterConfig(CAN_FilterTypeDef* sFilterConfig, uint32_t FilterBank, uint32_t FilterMode, uint32_t FilterScale,
uint32_t FilterIdHigh, uint32_t FilterIdLow, uint32_t FilterMaskIdHigh, uint32_t FilterMaskIdLow, uint32_t FilterFIFOAssignment,
uint32_t FilterActivation, uint32_t SlaveStartFilterBank)
{
sFilterConfig->FilterBank = FilterBank;
sFilterConfig->FilterMode = FilterMode;
sFilterConfig->FilterScale = FilterScale;
sFilterConfig->FilterIdHigh = FilterIdHigh<<5;
sFilterConfig->FilterIdLow = FilterIdLow<<5;
sFilterConfig->FilterMaskIdHigh = FilterMaskIdHigh<<5;
sFilterConfig->FilterMaskIdLow = FilterMaskIdLow<<5;
sFilterConfig->FilterFIFOAssignment = FilterFIFOAssignment;
sFilterConfig->FilterActivation = FilterActivation;
sFilterConfig->SlaveStartFilterBank = SlaveStartFilterBank;
if(HAL_CAN_ConfigFilter(&hcan1, sFilterConfig) != HAL_OK)
{
/* Filter Configuration Error */
Error_Handler();
}
}
// CAN trasmission
void CAN_CMD_STD(uint16_t id, uint8_t length, uint8_t* data)
{
int k=0;
TxHeader.StdId = id;
TxHeader.DLC = length;
TxHeader.RTR = CAN_RTR_DATA;
TxHeader.IDE = CAN_ID_STD;
TxHeader.TransmitGlobalTime = DISABLE;
for ( k = 0 ; k < length ; k ++ ) TxData[k] = data[k];
if (HAL_CAN_AddTxMessage(&hcan1, &TxHeader, TxData, &TxMailbox) != HAL_OK)
{
/* Transmission request Error */
Error_Handler();
}
while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan1) != 3) {}; //Wait transmission complete
}
void send_frame()
{
// emg
data_set1[0] = dat[0] ;
data_set1[1] = (dat[0] >> 8);
data_set1[2] = dat[1];
data_set1[3] = (dat[1]>>8);
data_set1[4] = dat[2];
data_set1[5] = (dat[2]>>8);
data_set1[6] = dat[3];
data_set1[7] = (dat[3]>>8);
/*
data_set1[0] = 1;
data_set1[1] = 2;
data_set1[2] = 3;
data_set1[3] = 4;
data_set1[4] = 5;
data_set1[5] = 6;
data_set1[6] = 7;
data_set1[7] = 8;
*/
// Load cell
data_set2[0] = dat[4];
data_set2[1] = (dat[4]>>8);
// angle conversion
//memcpy(&ang_u32[0],&ang[0],8);
//memcpy(&ang_u32[1],&ang[1],8);
//Transmission
HAL_Delay(1000);
CAN_CMD_STD(CAN_ID[0], 8, data_set1);
CAN_CMD_STD(CAN_ID[1], 8, data_set2);
//CAN_CMD_STD(CAN_ID[2], 8, data_set3);
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* file: pointer to the source file name
* line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
Solved! Go to Solution.
2024-08-28 07:39 PM
Hello @lee_daeun .
I don't know if you intended it, but there seem to be a few problems.
First, it seems necessary to comment out lines 581 to 587 in the CAN_CMD_STD() function and then check whether the desired result is obtained when printing (or logging) TxData.
2024-08-28 07:39 PM
Hello @lee_daeun .
I don't know if you intended it, but there seem to be a few problems.
First, it seems necessary to comment out lines 581 to 587 in the CAN_CMD_STD() function and then check whether the desired result is obtained when printing (or logging) TxData.
2024-09-04 10:18 PM
I eliminated the 'while/if' code in transmisison code, and it worked well~ Thanks for your help!!