Can see is the use of the SPI1 PA4/PA5/PA6/PA7 reuse of function, so the code is as follows:
void SPI1_INIT (void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHBPeriphClockCmd (RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd (RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_AF;//multiplexing function
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;//push-pull output
GPIO_InitStructure. GPIO_PuPd=GPIO_PuPd_DOWN;//dropdown
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;//medium speed
GPIO_InitStructure. GPIO_Pin=PIN_SPI1_SCK | PIN_SPI1_MOSI | PIN_SPI1_MISO;
GPIO_Init (SPI1_PORT, & amp; GPIO_InitStructure);
GPIO_PinAFConfig (, GPIO_AF_0 SPI1_PORT, GPIO_PinSource5);
GPIO_PinAFConfig (SPI1_PORT, GPIO_PinSource6 GPIO_AF_0);
GPIO_PinAFConfig (SPI1_PORT GPIO_PinSource7, GPIO_AF_0);
GPIO_InitStructure. GPIO_Pin=PIN_SPI1_CSN;//configuration CSN
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//high speed
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;//output mode
GPIO_Init (SPI1_PORT, & amp; GPIO_InitStructure);
SPI_I2S_DeInit (SPI1);//register reset to the default
SPI_InitStructure. SPI_Direction=SPI_Direction_2Lines_FullDuplex;//double bidirectional full-duplex
SPI_InitStructure. SPI_Mode=SPI_Mode_Master;//host mode
SPI_InitStructure. SPI_DataSize=SPI_DataSize_8b;//8 frame structure
SPI_InitStructure. SPI_CPOL=SPI_CPOL_Low;//SCK for low level communication free time
SPI_InitStructure. SPI_CPHA=SPI_CPHA_1Edge;//the first clock along the capture
SPI_InitStructure. SPI_NSS=SPI_NSS_Soft;//software control NSS
SPI_InitStructure. SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_8; Eight points//SPI speed frequency
SPI_InitStructure. SPI_FirstBit=SPI_FirstBit_MSB;//data transmission from the MSB
SPI_InitStructure. SPI_CRCPolynomial=7;//CRC check
SPI_Init (SPI1, & amp; SPI_InitStructure);
SPI_RxFIFOThresholdConfig (SPI1, SPI_RxFIFOThreshold_QF);//important, the response data bits set to eight
SPI_Cmd (SPI1, ENABLE);
}
Above code using pin function mapping function:
GPIO_PinAFConfig (, GPIO_AF_0 SPI1_PORT, GPIO_PinSource5);
GPIO_PinAFConfig (SPI1_PORT, GPIO_PinSource6 GPIO_AF_0);
GPIO_PinAFConfig (SPI1_PORT GPIO_PinSource7, GPIO_AF_0);
Question: do not use the mapping function, also can work normally SPI, GPIO_PinAFConfig function is must use excuse me?
CodePudding user response:
GPIO_PinAFConfig is library functions, and its essence is initialized to this a few PIN register set, if not register initialization (so this a few PIN is in default state), are generally will not be able to use the SPI, if your application is not using a library function, but a separate set of register (premise is the correct configuration), and the use of the library function effect is the same,PS: the essence of library function is set up a register, if directly to register operation, the MCU stack overhead is significantly less than the use of library functions, weakness is modified or transplantation when there will be a larger workload,
CodePudding user response:
GPIO_AF_0 function of A port 0, 0 function is generally after power on the default functionBut suggest keep mapping function, so in port, problem when not easy omission