[譯]關於SPI通訊中片選訊號的選擇SPI_NSS_SOFT和SPI_NSS_HARD_OUTPUT以及SPI_NSS_HARD_INPUT
阿新 • • 發佈:2018-11-29
以下文章來自外網翻譯,侵刪
在ST官方程式碼中,SPI常見配置如下
Init structure:
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;//配置SPI全雙工,其實不知道ST怎麼想的,這不是廢話麼
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
/----------------------------------------------------------/
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.NSS = SPI_NSS_HARD_OUTPUT;//配置spi在master下,NSS作為SPI專用IO,由MCU自動控制片選,只能1主1從
hspi1.Init.NSS = SPI_NSS_HARD_INPUT;//僅當配置spi在slave下,作為從機片選輸入
/----------------------------------------------------------/
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;
Transmit sequence:
SPI_NSS_HARD_OUTPUT模式下:
HAL_SPI_TransmitReceive( &hspi1, btx, brx, l, 5 ); // timeout 5msec;
while( hspi1.State == HAL_SPI_STATE_BUSY ); // wait for xmission complete
So I decided setting the pin manually using GPIO (using SPI_NSS_SOFT in init):
SPI_NSS_SOFT模式下:
HAL_GPIO_WritePin( NSS1_GPIO_Port, NSS1_Pin, GPIO_PIN_RESET ); // NSS1 low HAL_SPI_TransmitReceive( &hspi1, btx, brx, l, 5 ); // timeout 5msec; while( hspi1.State == HAL_SPI_STATE_BUSY ); // wait xmission complete HAL_GPIO_WritePin( NSS1_GPIO_Port, NSS1_Pin, GPIO_PIN_SET ); // NSS1 high