Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "main.h"
- #include "stm32f10x_conf.h"
- #include "stm32f10x.h"
- #include "comm.h"
- SPI_InitTypeDef SPI2_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- const char welcome[]="Hello test serial communication !!\n\n";
- int main(void)
- {
- // clock
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 | RCC_APB1Periph_USART2, ENABLE);
- //Configuration GPIO
- //GPIO LED STAT
- GPIO_InitStructure.GPIO_Pin= GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- // GPIO USART
- /* Configure USART2 TX 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 as input floating */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //GPIO SPI :
- //PC6 WP
- GPIO_InitStructure.GPIO_Pin= GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- //PC7 CP
- GPIO_InitStructure.GPIO_Pin= GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- /* Configure NSS select */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_SetBits(GPIOB, GPIO_Pin_12);
- //PB13 SPI2 SCK
- GPIO_InitStructure.GPIO_Pin= GPIO_Pin_13;
- GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF_PP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- //PB14 SPI2 MISO
- GPIO_InitStructure.GPIO_Pin= GPIO_Pin_14;
- GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IPU;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- //PB15 SPI2 MOSI
- GPIO_InitStructure.GPIO_Pin= GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF_PP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- //Configuration USART2
- 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_Rx | USART_Mode_Tx;
- /* Configure USART2 */
- USART_Init(USART2, &USART_InitStructure);
- /* Enable the USART2 */
- USART_Cmd(USART2, ENABLE);
- // Configuration SPI2
- SPI2_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
- SPI2_InitStructure.SPI_Mode = SPI_Mode_Master;
- SPI2_InitStructure.SPI_DataSize = SPI_DataSize_8b;
- SPI2_InitStructure.SPI_CPOL = SPI_CPOL_Low;
- SPI2_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
- SPI2_InitStructure.SPI_NSS = SPI_NSS_Soft;
- SPI2_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
- SPI2_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
- SPI2_InitStructure.SPI_CRCPolynomial = 7;
- SPI_Init(SPI2, &SPI2_InitStructure);
- // CRC DISABLE
- SPI_CalculateCRC(SPI2, DISABLE);
- // Activation SPI
- SPI_Cmd(SPI2, ENABLE);
- // SET / RESET LED STAT
- GPIOC->BSRR = 1 << 12;
- //send on USART2
- comm_puts(welcome);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement