2017-02-21 09:45 PM
Hi, I am having trouble setting up the SPI port on a F030. The basic initialization code
The Ports are setup for AF0 on ports B3,4,5
// set baud rate
SPI1->CR1|=SPI_CR1_BR_0|SPI_CR1_BR_1|SPI_CR1_BR_2; // set baud rate
// setup CPOL and CPHA if not correct
// set for master
SPI1->CR1|=SPI_CR1_MSTR; // set to master
// set data size
SPI1->CR2|=SPI_CR2_DS_0|SPI_CR2_DS_1|SPI_CR2_DS_2; // set for 8 bits
// configure FRXTH to 8
SPI1->CR2|=SPI_CR2_FRXTH;
// now enable peripheral
SPI1->CR1|=SPI_CR1_SPE;
If I debug and watch what is happening, the bits get set correctly then when I enable the peripheral the enable bit does not get set and the master bit is cleared. I suspect I am not doing it in the right sequence but cannot find any comments on the right sequence.
Any help appreciated.
2017-02-21 10:31 PM
NSS low switches it to slave. If you don't intend to use NSS pin, use software NSS set to high.
JW
2017-02-22 02:35 AM
do you cube MX ?
/* SPI1 init function */void MX_SPI1_Init(void){
hspi1.Instance = SPI1; hspi1.Init.Mode = SPI_MODE_MASTER; hspi1.Init.Direction = SPI_DIRECTION_2LINES; hspi1.Init.DataSize = SPI_DATASIZE_8BIT; hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; hspi1.Init.NSS = SPI_NSS_SOFT; hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; hspi1.Init.TIMode = SPI_TIMODE_DISABLE; hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; hspi1.Init.CRCPolynomial = 7; hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE; if (HAL_SPI_Init(&hspi1) != HAL_OK) { Error_Handler(); }
}
void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle){
GPIO_InitTypeDef GPIO_InitStruct; if(spiHandle->Instance==SPI1) { /* USER CODE BEGIN SPI1_MspInit 0 */
/* USER CODE END SPI1_MspInit 0 */ /* Peripheral clock enable */ __HAL_RCC_SPI1_CLK_ENABLE(); /**SPI1 GPIO Configuration PA5 ------> SPI1_SCK PB4 ------> SPI1_MISO PB5 ------> SPI1_MOSI */ GPIO_InitStruct.Pin = GPIO_PIN_5; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Alternate = GPIO_AF0_SPI1; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Alternate = GPIO_AF0_SPI1; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN SPI1_MspInit 1 */
/* USER CODE END SPI1_MspInit 1 */ } else if(spiHandle->Instance==SPI2) { /* USER CODE BEGIN SPI2_MspInit 0 */
/* USER CODE END SPI2_MspInit 0 */ /* Peripheral clock enable */ __HAL_RCC_SPI2_CLK_ENABLE(); /**SPI2 GPIO Configuration PB13 ------> SPI2_SCK PB14 ------> SPI2_MISO PB15 ------> SPI2_MOSI */ GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Alternate = GPIO_AF0_SPI2; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN SPI2_MspInit 1 */
/* USER CODE END SPI2_MspInit 1 */ }}
void initSpi1DualLcd(void){//Initialise_SPI Tx systems Tx16SPIBuffer[0] = 0xFFFF; HAL_SPI_TransmitReceive(&hspi1, (uint8_t*)Tx16SPIBuffer, (uint8_t *)RxSPIBuffer, 1, 1000);
}
void quickSend8SPI(SPI_HandleTypeDef *hspi){
while( !( hspi->Instance->SR & SPI_FLAG_TXE)); *((__IO uint8_t *)&hspi->Instance->DR) = TxSPIByte; }void quickSendReceive8SPI(SPI_HandleTypeDef *hspi){
while( !( hspi->Instance->SR & SPI_FLAG_TXE));
*((__IO uint8_t *)&hspi->Instance->DR) = TxSPIByte; // force the SPI to transceive 8 bit while( !( hspi->Instance->SR & SPI_FLAG_TXE)); while( ( hspi->Instance->SR & SPI_FLAG_BSY)); while( ( hspi->Instance->SR & SPI_FLAG_RXNE)) // only the last byte in this buffer is wanted RxSPIByte1 = hspi->Instance->DR;
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?