2009-11-12 04:40 AM
STM32F101 PA4 pin priority - SPI or USART ?
2011-05-17 04:29 AM
Hello all,
I am starting with STM32 controllers. I have F101 in LQFP48 and one dilemma. I want to use SPI1 (in master mode for MMC card) and USART2 in asynchronous mode (for comm with PC). But without remapping PA4 is shared for USART2_CK and SPI1_NSS. So, if I set PA4 as GPIO output, will USART2 object against it ? I mean, does anybody know if I can use PA2,PA3 for USART2, PA5-PA7 for SPI1 and PA4 as SPI select ? Thank you in advance, Vasek2011-05-17 04:29 AM
Hello Vasek,
No problem, I myself is using the same type of config you are using. Everything lies in GPIO config: For USART2: // Configure USART2 Tx (PA2) as alternate function push-pull GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); // Configure USART2 Rx (PA3) as input floating GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); For SPI1: // Configure SPI1 (RF) pins: SCK, MOSI GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); // CS SPI1 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); and then peripheral config: // USART2 Config USART_ClockInitStructure.USART_Clock = USART_Clock_Disable; USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; USART_ClockInit(USART2, &USART_ClockInitStructure); USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(USART2, &USART_InitStructure); // Enable the USART2 USART_Cmd(USART2, ENABLE); // SPI1 Config SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; // Clock idle high SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; // Data capture on second edge of clock SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; // Master clock set to 9Mbits SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); // Disable CRC calculation SPI_CalculateCRC (SPI1, DISABLE); // Enable SPI1 SPI_Cmd(SPI1, ENABLE); Disabling USART clock and setting SPI NSS ''soft'' will not create any conflict on the pin PA4 and it then can be used as a standard IO pin for your SPI select. Phil [ This message was edited by: philippe.balz on 12-11-2009 17:22 ]2011-05-17 04:29 AM
I will add: in GPIO mode, the pin is completely controlled by SW, not by comm. peripherals.
In AF mode the pin is controlled by a COMBINATION of peripheral outputs, peripheral influence is usually gated with their global clock. If both peripherals are needed and one output should prevail, make sure the output of other peripheral, combined on the pin, is fixed in log. 0 or 1 (need to test, but works).