cancel
Showing results for 
Search instead for 
Did you mean: 

bh1750 sensor i2c poll method working fine but i2c DMA method not working with attached code . will DMA only transfer the value to global variable ?

MMARI.1
Senior

0693W00000YAVmWQAX.png

 
#include "newblue_bh1750.h"
 
 
I2C_HandleTypeDef 	*bh1750_i2c;	// Handler to I2C interface
 
bh1750_mode 		Bh1750_Mode;	// Current sensor mode
uint8_t 			Bh1750_Mtreg;	// Current MT register value
 
void BH1750_Init(I2C_HandleTypeDef *hi2c)
{
	bh1750_i2c = hi2c;
	BH1750_Reset();
	BH1750_SetMtreg(BH1750_DEFAULT_MTREG);
 
 
}
 
//
//	Reset all registers to default value.
//
void BH1750_Reset(void)
{
	uint8_t tmp = 0x07;
 
	if(HAL_I2C_Master_Transmit_DMA(bh1750_i2c, BH1750_ADDRESS, &tmp, 1)!=HAL_OK )
	//if(HAL_I2C_Master_Transmit(bh1750_i2c, BH1750_ADDRESS, &tmp, 1,10)!=HAL_OK )
	{
	  Error_Handler();
	}
 
}
 
//
//	Set the power state.
//	0 - sleep, low power.
//	1 - running.
//
void BH1750_PowerState(uint8_t PowerOn)
{
	PowerOn = (PowerOn? 1:0);
	if( HAL_I2C_Master_Transmit_DMA(bh1750_i2c, BH1750_ADDRESS, &PowerOn, 1)!=HAL_OK)
	//if( HAL_I2C_Master_Transmit(bh1750_i2c, BH1750_ADDRESS, &PowerOn, 1,10)!=HAL_OK)
	{	Error_Handler(); }
}
 
//
//	Set the mode of converting. Look into bh1750_mode enum.
//
void BH1750_SetMode(bh1750_mode Mode)
{
	if(!((Mode >> 4) || (Mode >> 5))) Error_Handler();
	if((Mode & 0x0F) > 3) Error_Handler();
 
	Bh1750_Mode = Mode;
	if(HAL_I2C_Master_Transmit_DMA(bh1750_i2c, BH1750_ADDRESS, &Mode, 1)!=HAL_OK )
	//if(HAL_I2C_Master_Transmit(bh1750_i2c, BH1750_ADDRESS, &Mode, 1,10)!=HAL_OK )
	{	Error_Handler(); }
}
 
//
//	Set the Measurement Time register. It allows to increase or decrease the sensitivity.
//
void BH1750_SetMtreg(uint8_t Mtreg)
{
	HAL_StatusTypeDef retCode;
	if (Mtreg < 31 || Mtreg > 254) {
		Error_Handler();
	}
 
	Bh1750_Mtreg = Mtreg;
 
	uint8_t tmp[2];
 
	tmp[0] = (0x40 | (Mtreg >> 5));
	tmp[1] = (0x60 | (Mtreg & 0x1F));
	retCode = HAL_I2C_Master_Transmit_DMA(bh1750_i2c, BH1750_ADDRESS, &tmp[0], 1);
	//retCode = HAL_I2C_Master_Transmit(bh1750_i2c, BH1750_ADDRESS, &tmp[0], 1,10);
	if (HAL_OK != retCode) {
		Error_Handler();
	}
	retCode = HAL_I2C_Master_Transmit_DMA(bh1750_i2c, BH1750_ADDRESS, &tmp[0], 1);
	//retCode = HAL_I2C_Master_Transmit(bh1750_i2c, BH1750_ADDRESS, &tmp[1], 1,10);
	if (HAL_OK == retCode) {
		asm("NOP");
	}
 
	//Error_Handler();
}
 
//
//	Trigger the conversion in manual modes.
//	For low resolution conversion time is typical 16 ms,
//	for high resolution 120 ms. You need to wait until read the measurement value.
//	There is no need to exit low power mode for manual conversion. It makes automatically.
//
void BH1750_TriggerManualConversion(void)
{
	BH1750_SetMode(Bh1750_Mode);
 
}
 
//
//	Read the converted value and calculate the result.
//
void BH1750_ReadLight(uint_fast32_t *raw)
{
 
	uint_fast32_t result;
	uint8_t tmp[2];
	if(HAL_I2C_Master_Receive_DMA(bh1750_i2c, BH1750_ADDRESS, tmp, 2)!=HAL_OK)
	//if(HAL_I2C_Master_Receive(bh1750_i2c, BH1750_ADDRESS, tmp, 2,10)!=HAL_OK)
	{
		Error_Handler();
 
 
	}
	result = (tmp[0] << 8) | (tmp[1]);
	*raw=result;
 
}
******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  
#include "main.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
 
#include"newblue_bh1750.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 ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
DMA_HandleTypeDef hdma_i2c1_rx;
DMA_HandleTypeDef hdma_i2c1_tx;
 
/* USER CODE BEGIN PV */
 
uint_fast32_t  rawvalue=0;
uint_fast32_t  RAWVALUE =0;
volatile uint_fast32_t  count=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_I2C1_Init(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
 
/* 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_I2C1_Init();
  /* USER CODE BEGIN 2 */
   BH1750_Init(&hi2c1);
 
   BH1750_SetMode(CONTINUOUS_HIGH_RES_MODE_2);
 
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
	   BH1750_ReadLight(&rawvalue);
	  	  RAWVALUE=rawvalue;
	  	  HAL_Delay(1000);
 
    /* 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 I2C1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_I2C1_Init(void)
{
 
  /* USER CODE BEGIN I2C1_Init 0 */
 
  /* USER CODE END I2C1_Init 0 */
 
  /* USER CODE BEGIN I2C1_Init 1 */
 
  /* USER CODE END I2C1_Init 1 */
  hi2c1.Instance = I2C1;
  hi2c1.Init.ClockSpeed = 100000;
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN I2C1_Init 2 */
 
  /* USER CODE END I2C1_Init 2 */
 
}
 
/**
  * Enable DMA controller clock
  */
static void MX_DMA_Init(void)
{
 
  /* DMA controller clock enable */
  __HAL_RCC_DMA1_CLK_ENABLE();
 
  /* DMA interrupt init */
  /* DMA1_Channel6_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Channel6_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel6_IRQn);
  /* DMA1_Channel7_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Channel7_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel7_IRQn);
 
}
 
/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
 
  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
 
  /*Configure GPIO pin : PC13 */
  GPIO_InitStruct.Pin = GPIO_PIN_13;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
 
}
 
/* USER CODE BEGIN 4 */
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c1)
{
  /* Toggle LED_GREEN: Transfer in reception process is correct */
 
	HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
	count++;
}
 
/* 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
 
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 */

3 REPLIES 3
MMARI.1
Senior

i am able to getting result with i2c DMA with following .

if(HAL_I2C_Mem_Read_DMA(bh1750_i2c, BH1750_ADDRESS, BH1750_ADDRESS, 1, tmp, 2)!=HAL_OK

instead of

if(HAL_I2C_Master_Receive_DMA(bh1750_i2c, BH1750_ADDRESS, tmp, 2)!=HAL_OK)

i am did know why Master_Receive_DMA is not working and Mem_Read_DMA is working .

Bh1750 sensor having single register Master_Receive_DMA is passing only one time read address with read bit but Mem_Read_DMA sending one time read address with read bit and additionally sending same read address without read bit but it's working .may be some one explain

KnarfB
Principal III

HAL_I2C_Master_Receive_DMA will immediately return, before the buffer is filled. So it makes no sense providing a stack local variable.

The clines afterward must belong to a completion handler, which is called after DMA was completed.

If you put them directly under HAL_I2C_Master_Receive_DMA, it may or may not work depending on timing, but this is principially wrong.

hth

KnarfB

MMARI.1
Senior

Hi, there is any possibility of stop and start DMA in i2c mode. Since sensor giving continues measurement i2c DMA giving continues call back.