The following is a low level code in mdk arm for stm32f1xx to receive data from the adxl thru i2c dma.. see in int main, how i commented the i2c read the second time…. The code works … as soon as i call it second time, the code freezes… sorry for the code to be so clueless, but im really new and struggling w this. please help me.
#include "main.h"
#include "stdio.h"
#include "math.h"
#define adxlAddr (0x53<<1)
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
void uartEnable(void);
void USART1_SendFloat(float f);
void i2cEnable(void);
void i2cWrite(uint8_t addr, uint8_t dat);
void i2cRead(uint8_t addr, int numBytes);
int dataRec[10];
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
RCC->APB2ENR = 0xFC;
uartEnable();
i2cEnable();
GPIOC->CRH = 0x42444444;
GPIOC->ODR = 0x0;
HAL_Delay(1);
GPIOC->ODR = 0xF000;
i2cWrite(0x2D,0x08);
i2cWrite(0x31,0);
i2cRead(0x32,6);
// i2cRead(0x32,6);
while (1)
{
USART1_SendFloat(3.141592);
HAL_Delay(10);
}
}
void uartEnable(void)
{
RCC->APB2ENR |= 0x4005;
GPIOA->CRH &= 0xFFFFFF0F;
GPIOA->CRH |= 0x000000B0;
USART1->BRR = 625;
USART1->CR1 = 0x200C;
}
void USART1_SendFloat(float f) {
char buffer[20];
sprintf(buffer, "%fn", f);
for (int i = 0; buffer[i] != ''; i++) {
// Wait until data register is empty
while (!(USART1->SR & USART_SR_TXE));
// Send character
USART1->DR = buffer[i];
}
}
void i2cEnable(void)
{
RCC->APB1ENR |= RCC_APB1ENR_I2C1EN;
//I2C1->CR1 = (1<<15);
I2C1->CR2 = 36;
I2C1->CCR |= 180;
I2C1->TRISE |= 37;
I2C1->CR1 |= I2C_CR1_ACK;
GPIOB->CRL = 0xFF444444;
I2C1->CR1 |= 1;
}
void i2cWrite (uint8_t addr, uint8_t dat)
{
I2C1->CR1 |= (1<<8);
while(!(I2C1->SR1 & 1)){};
I2C1->DR = adxlAddr;
while(!(I2C1->SR1 & (2))){};
uint16_t temp = I2C1->SR2;
I2C1->DR = addr;
while(!(I2C1->SR1 & (1<<7))){};
I2C1->DR = dat;
while(!(I2C1->SR1 & (1<<7))){};
I2C1->CR1 |= (1<<9);
}
void i2cRead(uint8_t addr, int numBytes)
{
RCC->AHBENR |= 1; //DMA en
I2C1->CR2 |= (1<<11);
I2C1->CR1 |= (1<<10);
DMA1_Channel7->CCR = 0;
DMA1_Channel7->CMAR = (uint32_t)dataRec;
DMA1_Channel7->CPAR = (uint32_t)&I2C1->DR;
DMA1_Channel7->CNDTR = numBytes;
DMA1_Channel7->CCR |= (1<<1) | (1<<7) | 1;
I2C1->CR1 |= (1<<8);
while(!(I2C1->SR1 & 1)){};
I2C1->DR = adxlAddr;
while(!(I2C1->SR1 & (2))){};
uint16_t temp = I2C1->SR2;
I2C1->DR = (addr);
while(!(I2C1->SR1 & (1<<7))){};
I2C1->CR1 |= (1<<9);
I2C1->CR1 |= (1<<8);
while(!(I2C1->SR1 & 1)){};
I2C1->DR = (adxlAddr|1);
while(!(I2C1->SR1 & (2))){};
temp = I2C1->SR2;
while((DMA1->ISR & DMA_ISR_TCIF7) ==0){};
I2C1->CR1 |= (1<<9);
DMA1->IFCR = 0x0FFFFFFF;
}
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();
}
}
static void MX_GPIO_Init(void)
{
__HAL_RCC_GPIOD_CLK_ENABLE();
}
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.
* @param file: pointer to the source file name
* @param 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 %drn", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
i treid resetting the dma everytime the read function is called and reinit-ing it… the code still freezes the second time… problem is, it is working 100% the first time, no matter memory location and num of bytes… and ive confirmed that the tranferred data is valid… why the second time… why does it work the first time and not second?
Ali Athar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.