/*====================================================================================================*/ void MPU9250_SetSpeed( uint8_t speedSel ) { __HAL_SPI_DISABLE(&MPUPx_InitStruct); MPUPx_InitStruct.Init.BaudRatePrescaler = speedSel; HAL_SPI_Init(&MPUPx_InitStruct); __HAL_SPI_ENABLE(&MPUPx_InitStruct); }
/** * @brief Initializes the SPI according to the specified parameters * in the SPI_InitTypeDef and create the associated handle. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains * the configuration information for SPI module. * @retval HAL status */ HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi) { /* Check the SPI handle allocation */ if(hspi == NULL) { return HAL_ERROR; } /* Check the parameters */ assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance)); assert_param(IS_SPI_MODE(hspi->Init.Mode)); assert_param(IS_SPI_DIRECTION_MODE(hspi->Init.Direction)); assert_param(IS_SPI_DATASIZE(hspi->Init.DataSize)); assert_param(IS_SPI_CPOL(hspi->Init.CLKPolarity)); assert_param(IS_SPI_CPHA(hspi->Init.CLKPhase)); assert_param(IS_SPI_NSS(hspi->Init.NSS)); assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler)); assert_param(IS_SPI_FIRST_BIT(hspi->Init.FirstBit)); assert_param(IS_SPI_TIMODE(hspi->Init.TIMode)); assert_param(IS_SPI_CRC_CALCULATION(hspi->Init.CRCCalculation)); assert_param(IS_SPI_CRC_POLYNOMIAL(hspi->Init.CRCPolynomial)); if(hspi->State == HAL_SPI_STATE_RESET) { /* Allocate lock resource and initialize it */ hspi->Lock = HAL_UNLOCKED; /* Init the low level hardware : GPIO, CLOCK, NVIC... */ HAL_SPI_MspInit(hspi); } hspi->State = HAL_SPI_STATE_BUSY; /* Disble the selected SPI peripheral */ __HAL_SPI_DISABLE(hspi); /*----------------------- SPIx CR1 & CR2 Configuration ---------------------*/ /* Configure : SPI Mode, Communication Mode, Data size, Clock polarity and phase, NSS management, Communication speed, First bit and CRC calculation state */ hspi->Instance->CR1 = (hspi->Init.Mode | hspi->Init.Direction | hspi->Init.DataSize | hspi->Init.CLKPolarity | hspi->Init.CLKPhase | (hspi->Init.NSS & SPI_CR1_SSM) | hspi->Init.BaudRatePrescaler | hspi->Init.FirstBit | hspi->Init.CRCCalculation); /* Configure : NSS management */ hspi->Instance->CR2 = (((hspi->Init.NSS >> 16) & SPI_CR2_SSOE) | hspi->Init.TIMode); /*---------------------------- SPIx CRCPOLY Configuration ------------------*/ /* Configure : CRC Polynomial */ hspi->Instance->CRCPR = hspi->Init.CRCPolynomial; #if defined (STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined (STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined (STM32L151xE) || defined (STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX) /* Activate the SPI mode (Make sure that I2SMOD bit in I2SCFGR register is reset) */ CLEAR_BIT(hspi->Instance->I2SCFGR, SPI_I2SCFGR_I2SMOD); #endif hspi->ErrorCode = HAL_SPI_ERROR_NONE; hspi->State = HAL_SPI_STATE_READY; return HAL_OK; }
/*====================================================================================================*/ void IMU_SetSpeed( uint8_t speedSel ) { __HAL_SPI_DISABLE(&IMU_HandleStruct); IMU_HandleStruct.Init.BaudRatePrescaler = speedSel; HAL_SPI_Init(&IMU_HandleStruct); __HAL_SPI_ENABLE(&IMU_HandleStruct); }
/*====================================================================================================*/ static void MPU9250_SetSpeed( uint8_t SpeedSel ) { __HAL_SPI_DISABLE(&SPI_HandleStruct); SPI_HandleStruct.Init.BaudRatePrescaler = SpeedSel; HAL_SPI_Init(&SPI_HandleStruct); __HAL_SPI_ENABLE(&SPI_HandleStruct); }
void DevSPI::resetDev() { __HAL_SPI_DISABLE( spi ); delay_ms( 1 ); __HAL_SPI_ENABLE( spi ); nss_post_cond(); delay_ms( 1 ); last_rc = HAL_OK; }
static void init_spi(spi_t *obj) { SPI_HandleTypeDef *handle = &SpiHandle[obj->spi.module]; __HAL_SPI_DISABLE(handle); HAL_SPI_Init(handle); __HAL_SPI_ENABLE(handle); }
void spi_abort_asynch(spi_t *obj) { // diable interrupt vIRQ_DisableIRQ(SpiIRQs[obj->spi.module]); // clean-up SPI_HandleTypeDef *handle = &SpiHandle[obj->spi.module]; __HAL_SPI_DISABLE(handle); HAL_SPI_DeInit(handle); HAL_SPI_Init(handle); __HAL_SPI_ENABLE(handle); }
void init_spi(spi_t *obj) { struct spi_s *spiobj = SPI_S(obj); SPI_HandleTypeDef *handle = &(spiobj->handle); __HAL_SPI_DISABLE(handle); DEBUG_PRINTF("init_spi: instance=0x%8X\r\n", (int)handle->Instance); if (HAL_SPI_Init(handle) != HAL_OK) { error("Cannot initialize SPI"); } __HAL_SPI_ENABLE(handle); }
void spi_abort_asynch(spi_t *obj) { struct spi_s *spiobj = SPI_S(obj); SPI_HandleTypeDef *handle = &(spiobj->handle); // disable interrupt IRQn_Type irq_n = spiobj->spiIRQ; NVIC_ClearPendingIRQ(irq_n); NVIC_DisableIRQ(irq_n); // clean-up __HAL_SPI_DISABLE(handle); HAL_SPI_DeInit(handle); HAL_SPI_Init(handle); __HAL_SPI_ENABLE(handle); }
static void init_spi(spi_t *obj) { SpiHandle.Instance = (SPI_TypeDef *)(obj->spi); __HAL_SPI_DISABLE(&SpiHandle); SpiHandle.Init.Mode = obj->mode; SpiHandle.Init.BaudRatePrescaler = obj->br_presc; SpiHandle.Init.Direction = SPI_DIRECTION_2LINES; SpiHandle.Init.CLKPhase = obj->cpha; SpiHandle.Init.CLKPolarity = obj->cpol; SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; SpiHandle.Init.CRCPolynomial = 7; SpiHandle.Init.DataSize = obj->bits; SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB; SpiHandle.Init.NSS = obj->nss; SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED; HAL_SPI_Init(&SpiHandle); __HAL_SPI_ENABLE(&SpiHandle); }
/* Private functions */ static void TM_SPIx_Init(SPI_TypeDef* SPIx, TM_SPI_PinsPack_t pinspack, TM_SPI_Mode_t SPI_Mode, uint16_t SPI_BaudRatePrescaler, uint16_t SPI_MasterSlave, uint16_t SPI_FirstBit) { SPI_HandleTypeDef SPIHandle; /* Save instance */ SPIHandle.Instance = SPIx; #ifdef SPI1 if (SPIx == SPI1) { /* Enable SPI clock */ __HAL_RCC_SPI1_CLK_ENABLE(); /* Init pins */ TM_SPI1_INT_InitPins(pinspack); /* Set options */ SPIHandle.Init.DataSize = TM_SPI1_DATASIZE; } #endif #ifdef SPI2 if (SPIx == SPI2) { /* Enable SPI clock */ __HAL_RCC_SPI2_CLK_ENABLE(); /* Init pins */ TM_SPI2_INT_InitPins(pinspack); /* Set options */ SPIHandle.Init.DataSize = TM_SPI2_DATASIZE; } #endif #ifdef SPI3 if (SPIx == SPI3) { /* Enable SPI clock */ __HAL_RCC_SPI3_CLK_ENABLE(); /* Init pins */ TM_SPI3_INT_InitPins(pinspack); /* Set options */ SPIHandle.Init.DataSize = TM_SPI3_DATASIZE; } #endif #ifdef SPI4 if (SPIx == SPI4) { /* Enable SPI clock */ __HAL_RCC_SPI4_CLK_ENABLE(); /* Init pins */ TM_SPI4_INT_InitPins(pinspack); /* Set options */ SPIHandle.Init.DataSize = TM_SPI4_DATASIZE; } #endif #ifdef SPI5 if (SPIx == SPI5) { /* Enable SPI clock */ __HAL_RCC_SPI5_CLK_ENABLE(); /* Init pins */ TM_SPI5_INT_InitPins(pinspack); /* Set options */ SPIHandle.Init.DataSize = TM_SPI5_DATASIZE; } #endif #ifdef SPI6 if (SPIx == SPI6) { /* Enable SPI clock */ __HAL_RCC_SPI6_CLK_ENABLE(); /* Init pins */ TM_SPI6_INT_InitPins(pinspack); /* Set options */ SPIHandle.Init.DataSize = TM_SPI6_DATASIZE; } #endif /* Fill SPI settings */ SPIHandle.Init.BaudRatePrescaler = SPI_BaudRatePrescaler; SPIHandle.Init.FirstBit = SPI_FirstBit; SPIHandle.Init.Mode = SPI_MasterSlave; SPIHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; SPIHandle.Init.CRCPolynomial = 7; SPIHandle.Init.TIMode = SPI_TIMODE_DISABLE; SPIHandle.Init.NSS = SPI_NSS_SOFT; SPIHandle.Init.Direction = SPI_DIRECTION_2LINES; #if defined(STM32F7xx) SPIHandle.Init.NSSPMode = SPI_NSS_PULSE_DISABLE; SPIHandle.Init.CRCLength = SPI_CRC_LENGTH_8BIT; #endif /* SPI mode */ if (SPI_Mode == TM_SPI_Mode_0) { SPIHandle.Init.CLKPolarity = SPI_POLARITY_LOW; SPIHandle.Init.CLKPhase = SPI_PHASE_1EDGE; } else if (SPI_Mode == TM_SPI_Mode_1) { SPIHandle.Init.CLKPolarity = SPI_POLARITY_LOW; SPIHandle.Init.CLKPhase = SPI_PHASE_2EDGE; } else if (SPI_Mode == TM_SPI_Mode_2) { SPIHandle.Init.CLKPolarity = SPI_POLARITY_HIGH; SPIHandle.Init.CLKPhase = SPI_PHASE_1EDGE; } else if (SPI_Mode == TM_SPI_Mode_3) { SPIHandle.Init.CLKPolarity = SPI_POLARITY_HIGH; SPIHandle.Init.CLKPhase = SPI_PHASE_2EDGE; } /* Disable first */ __HAL_SPI_DISABLE(&SPIHandle); /* Init SPI */ HAL_SPI_Init(&SPIHandle); /* Enable SPI */ __HAL_SPI_ENABLE(&SPIHandle); }
/** * @brief Initializes the SPI according to the specified parameters * in the SPI_InitTypeDef and create the associated handle. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains * the configuration information for SPI module. * @retval HAL status */ HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi) { /* Check the SPI handle allocation */ if(hspi == NULL) { return HAL_ERROR; } /* Check the parameters */ assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance)); assert_param(IS_SPI_MODE(hspi->Init.Mode)); assert_param(IS_SPI_DIRECTION_MODE(hspi->Init.Direction)); assert_param(IS_SPI_DATASIZE(hspi->Init.DataSize)); assert_param(IS_SPI_CPOL(hspi->Init.CLKPolarity)); assert_param(IS_SPI_CPHA(hspi->Init.CLKPhase)); assert_param(IS_SPI_NSS(hspi->Init.NSS)); assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler)); assert_param(IS_SPI_FIRST_BIT(hspi->Init.FirstBit)); assert_param(IS_SPI_TIMODE(hspi->Init.TIMode)); assert_param(IS_SPI_CRC_CALCULATION(hspi->Init.CRCCalculation)); assert_param(IS_SPI_CRC_POLYNOMIAL(hspi->Init.CRCPolynomial)); if(hspi->State == HAL_SPI_STATE_RESET) { /* Init the low level hardware : GPIO, CLOCK, NVIC... */ HAL_SPI_MspInit(hspi); } hspi->State = HAL_SPI_STATE_BUSY; /* Disble the selected SPI peripheral */ __HAL_SPI_DISABLE(hspi); /*----------------------- SPIx CR1 & CR2 Configuration ---------------------*/ /* Configure : SPI Mode, Communication Mode, Data size, Clock polarity and phase, NSS management, Communication speed, First bit and CRC calculation state */ WRITE_REG(hspi->Instance->CR1, (hspi->Init.Mode | hspi->Init.Direction | hspi->Init.DataSize | hspi->Init.CLKPolarity | hspi->Init.CLKPhase | (hspi->Init.NSS & SPI_CR1_SSM) | hspi->Init.BaudRatePrescaler | hspi->Init.FirstBit | hspi->Init.CRCCalculation) ); /* Configure : NSS management */ WRITE_REG(hspi->Instance->CR2, (((hspi->Init.NSS >> 16) & SPI_CR2_SSOE) | hspi->Init.TIMode)); /*---------------------------- SPIx CRCPOLY Configuration ------------------*/ /* Configure : CRC Polynomial */ WRITE_REG(hspi->Instance->CRCPR, hspi->Init.CRCPolynomial); #if defined (STM32F101x6) || defined (STM32F101xB) || defined (STM32F101xE) || defined (STM32F101xG) || defined (STM32F102x6) || defined (STM32F102xB) || defined (STM32F103x6) || defined (STM32F103xB) || defined (STM32F103xE) || defined (STM32F103xG) || defined (STM32F105xC) || defined (STM32F107xC) /* Activate the SPI mode (Make sure that I2SMOD bit in I2SCFGR register is reset) */ CLEAR_BIT(hspi->Instance->I2SCFGR, SPI_I2SCFGR_I2SMOD); #endif #if defined (STM32F101xE) || defined (STM32F103xE) /* Check RevisionID value for identifying if Device is Rev Z (0x0001) in order to enable workaround for CRC errors wrongly detected */ /* Pb is that ES_STM32F10xxCDE also identify an issue in Debug registers access while not in Debug mode. Revision ID information is only available in Debug mode, so Workaround could not be implemented to distinguish Rev Z devices (issue present) from more recent version (issue fixed). So, in case of Revison Z F101 or F103 devices, below variable should be assigned to 1 */ uCRCErrorWorkaroundCheck = 0; #else uCRCErrorWorkaroundCheck = 0; #endif hspi->ErrorCode = HAL_SPI_ERROR_NONE; hspi->State = HAL_SPI_STATE_READY; return HAL_OK; }
/** * @brief MPU 片选失能 * @param None * @retval None */ static void MPU_SPIDeselect(void) { HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET); __HAL_SPI_DISABLE(MPU_SPI); }
void DevSPI::deInit() { __HAL_SPI_DISABLE( spi ); HAL_SPI_DeInit( spi ); last_rc = HAL_OK; last_err = 0; }
void spi_free(spi_t *obj) { struct spi_s *spiobj = SPI_S(obj); SPI_HandleTypeDef *handle = &(spiobj->handle); DEBUG_PRINTF("spi_free\r\n"); __HAL_SPI_DISABLE(handle); HAL_SPI_DeInit(handle); #if defined SPI1_BASE // Reset SPI and disable clock if (spiobj->spi == SPI_1) { __HAL_RCC_SPI1_FORCE_RESET(); __HAL_RCC_SPI1_RELEASE_RESET(); __HAL_RCC_SPI1_CLK_DISABLE(); } #endif #if defined SPI2_BASE if (spiobj->spi == SPI_2) { __HAL_RCC_SPI2_FORCE_RESET(); __HAL_RCC_SPI2_RELEASE_RESET(); __HAL_RCC_SPI2_CLK_DISABLE(); } #endif #if defined SPI3_BASE if (spiobj->spi == SPI_3) { __HAL_RCC_SPI3_FORCE_RESET(); __HAL_RCC_SPI3_RELEASE_RESET(); __HAL_RCC_SPI3_CLK_DISABLE(); } #endif #if defined SPI4_BASE if (spiobj->spi == SPI_4) { __HAL_RCC_SPI4_FORCE_RESET(); __HAL_RCC_SPI4_RELEASE_RESET(); __HAL_RCC_SPI4_CLK_DISABLE(); } #endif #if defined SPI5_BASE if (spiobj->spi == SPI_5) { __HAL_RCC_SPI5_FORCE_RESET(); __HAL_RCC_SPI5_RELEASE_RESET(); __HAL_RCC_SPI5_CLK_DISABLE(); } #endif #if defined SPI6_BASE if (spiobj->spi == SPI_6) { __HAL_RCC_SPI6_FORCE_RESET(); __HAL_RCC_SPI6_RELEASE_RESET(); __HAL_RCC_SPI6_CLK_DISABLE(); } #endif // Configure GPIOs pin_function(spiobj->pin_miso, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); pin_function(spiobj->pin_mosi, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); pin_function(spiobj->pin_sclk, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); if (handle->Init.NSS != SPI_NSS_SOFT) { pin_function(spiobj->pin_ssel, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); } }