2016-04-21 05:57 AM
Hello everyone!
I currently working on my masters work with STM32F1 MCU. I created work in STM32Cube - SPI2 communication protocol with gyro, UART communication protocol to PC. And everything was fine until I wanna write to gyro (for initialization CTRL_REG1). I try to write on in (i write 0x0F on location 0x20), and then read from same register because I noticed that something was not fine. It returns me every time 0XFF instead of 0x0F. Gyro: https://www.pololu.com/file/0J563/L3GD20.pdfI put my code below. Please help me :)int main(void){ uint8_t data_tx = 0; uint8_t data_rx = 0; /* 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_I2C2_Init(); MX_SPI2_Init(); MX_USART1_UART_Init(); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); //who am I HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); data_tx = 0x8F; //we wanna read from address 0x0F HAL_SPI_TransmitReceive(&hspi2, &data_tx, &data_rx, sizeof(data_tx), 0x1000); data_tx = 0x00; HAL_SPI_TransmitReceive(&hspi2, &data_tx, &data_rx, sizeof(data_rx), 0x1000); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); HAL_Delay(500); //initialization CTRL_REG1 (20h) HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); data_tx = 0x20; //register address HAL_SPI_TransmitReceive(&hspi2, &data_tx, &data_rx, sizeof(data_tx), 0x1000); data_tx = 0x1F; //mode HAL_SPI_TransmitReceive(&hspi2, &data_tx, &data_rx, sizeof(data_tx), 0x1000); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); HAL_Delay(500); //read from CTRL_REG1 (20h) HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); data_tx = 0xA0; //register address HAL_SPI_TransmitReceive(&hspi2, &data_tx, &data_rx, sizeof(data_tx), 0x1000); data_tx = 0x00; //mode HAL_SPI_TransmitReceive(&hspi2, &data_tx, &data_rx, sizeof(data_tx), 0x1000); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); HAL_Delay(500); while (1) { HAL_UART_Transmit(&huart1, &data_rx, sizeof(data_rx), 0x1000); HAL_Delay(1000); }}/** System Clock Configuration*/void SystemClock_Config(void){ RCC_OscInitTypeDef RCC_OscInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = 16; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; HAL_RCC_OscConfig(&RCC_OscInitStruct); RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0); HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); /* SysTick_IRQn interrupt configuration */ HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);}/* SPI2 init function */void MX_SPI2_Init(void){ hspi2.Instance = SPI2; hspi2.Init.Mode = SPI_MODE_MASTER; hspi2.Init.Direction = SPI_DIRECTION_2LINES; hspi2.Init.DataSize = SPI_DATASIZE_8BIT; hspi2.Init.CLKPolarity = SPI_POLARITY_HIGH; hspi2.Init.CLKPhase = SPI_PHASE_2EDGE; hspi2.Init.NSS = SPI_NSS_SOFT; hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB; hspi2.Init.TIMode = SPI_TIMODE_DISABLE; hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; hspi2.Init.CRCPolynomial = 10; HAL_SPI_Init(&hspi2);}/* USART1 init function */void MX_USART1_UART_Init(void){ huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; HAL_UART_Init(&huart1);}/** Configure pins as * Analog * Input * Output * EVENT_OUT * EXTI*/void MX_GPIO_Init(void){ /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE();}