STM32F1XX and STM32F2 series serial port, SPI configuration, can configuration

Source: Internet
Author: User

Usually used in the serial port and SPI configuration listed here, so that a contingency, directly brought over to use. It also points out the frequent problems of STM32F1 series and STM32F2 series. The library function used is the standard library function, not the HAL Library.

First, the configuration of the serial port:
STM32F1 Series Serial Port:

* * Function Name: usart1_config () * Description: Serial 1 configuration function * Input: no * output: no * call: Internal call/void Usart1_config () {usart_inittypedef usart_i
        Nitstructure;

        Gpio_inittypedef gpio_initstructure;
        Gpio_structinit (&gpio_initstructure);
        Gpio_initstructure.gpio_pin = Gpio_pin_9;
        Gpio_initstructure.gpio_mode = gpio_mode_af_pp;
        Gpio_initstructure.gpio_speed = Gpio_speed_50mhz;
        Gpio_init (gpioa,&gpio_initstructure);
        Gpio_initstructure.gpio_pin = gpio_pin_10; Gpio_initstructure.gpio_mode = gpio_mode_in_floating;


        The RX PIN must be a float input gpio_init (gpioa,&gpio_initstructure);
        Usart_initstructure.usart_baudrate = 115200;
        Usart_initstructure.usart_hardwareflowcontrol=usart_hardwareflowcontrol_none; Usart_initstructure.usart_mode = Usart_mode_tx |
        USART_MODE_RX;
        Usart_initstructure.usart_parity =usart_parity_no;
        Usart_initstructure.usart_stopbits = Usart_stopbits_1; Usart_initstructure.usart_wordleNgth = usart_wordlength_8b;
        Usart_init (usart1,&usart_initstructure);
        Usart_itconfig (usart1,usart_it_tc,enable);//Use Send complete interrupt, Txe send register air break usart_itconfig (usart1,usart_it_rxne,enable);
Usart_cmd (usart1,enable); }

STM32F2 series serial port:

* * Function Name: Usart1_config * Description: Serial 1, normal serial port configuration function * Input: no * output: no * call: Internal call/void Usart1_config () {usart_inittypedef Usart_
    Initstructure;
    Gpio_inittypedef gpio_initstructure;

    Rcc_apb1periphclockcmd (rcc_apb2periph_usart1,enable);
    /**configure PA0,PA1 for the uart4*/gpio_structinit (&gpio_initstructure); Gpio_initstructure.gpio_pin = Gpio_pin_10 |
    Gpio_pin_9;
    Gpio_initstructure.gpio_mode = GPIO_MODE_AF;
    Gpio_initstructure.gpio_speed = Gpio_speed_50mhz;
    Gpio_init (gpioa,&gpio_initstructure);
    Gpio_pinafconfig (GPIOA,GPIO_PINSOURCE9,GPIO_AF_USART1);    Gpio_pinafconfig (GPIOA,GPIO_PINSOURCE10,GPIO_AF_USART1);
    Just configure the second function usart_initstructure.usart_baudrate = 115200;
    Usart_initstructure.usart_hardwareflowcontrol=usart_hardwareflowcontrol_none; Usart_initstructure.usart_mode = Usart_mode_rx |
    Usart_mode_tx;
    Usart_initstructure.usart_parity =usart_parity_no;
    Usart_initstructure.usart_stopbits = Usart_stopbits_1; Usart_initstrUcture.
    Usart_wordlength = usart_wordlength_8b;
    Usart_init (usart1,&usart_initstructure);
    Usart_itconfig (usart1,usart_it_tc,enable);//Use Send complete interrupt, Txe send register air break usart_itconfig (usart1,usart_it_rxne,enable);
Usart_cmd (usart1,enable); }

Two, SPI configuration:
STM32F1 series SPI:

* * Function Name: Spi_init () * Description: Initialize SPI, * input: no * output: no * call: Internal call/void Initspi () {/** PB3,4,5,PA15 * * spi_in
    Ittypedef spi_initstructure;

    Gpio_inittypedef gpio_initstructure;
    Rcc_apb1periphclockcmd (rcc_apb2periph_spi1,enable); Gpio_initstructure.gpio_pin = gpio_pin_3|
    Gpio_pin_5;
    Gpio_initstructure.gpio_mode = gpio_mode_af_pp;
    Gpio_initstructure.gpio_speed = Gpio_speed_50mhz;
    Gpio_init (gpiob,&gpio_initstructure);

    Gpio_pinremapconfig (gpio_remap_spi1,enable);
    Gpio_initstructure.gpio_pin = Gpio_pin_4;
    Gpio_initstructure.gpio_mode = gpio_mode_in_floating;//Here is the float input//gpio_init (gpiob,&gpio_initstructure);
    Gpio_initstructure.gpio_pin = gpio_pin_15;
    Gpio_initstructure.gpio_mode = gpio_mode_in_floating;

    Gpio_init (gpioa,&gpio_initstructure);
    Spi_initstructure.spi_baudrateprescaler = spi_baudrateprescaler_256;
    Spi_initstructure.spi_cpha = Spi_cpha_2edge;
   Spi_initstructure.spi_cpol = Spi_cpol_high; Spi_initstructure.spi_crcpolynomial = 7;
    Spi_initstructure.spi_datasize = spi_datasize_8b;
    Spi_initstructure.spi_direction = Spi_direction_1line_rx;
    Spi_initstructure.spi_firstbit = SPI_FIRSTBIT_MSB;
    Spi_initstructure.spi_mode = Spi_mode_slave;
    Spi_initstructure.spi_nss = Spi_nss_soft;
    Spi_init (spi1,&spi_initstructure);

 Spi_cmd (spi1,enable); }

STM32F2 Series SPI configuration:

* * Function name: hal_spi_mspinit (void) * Description: SPI configuration function, main configuration SPI1 * Input: no * output: no * call: Internal call/void Hal_spi_mspinit (void) {Spi_
    Inittypedef spi_initstructure;
    Gpio_inittypedef gpio_initstructure;


    Rcc_apb2periphclockcmd (rcc_apb2periph_spi1,enable); Gpio_initstructure.gpio_pin = Gpio_pin_3 | gpio_pin_4|
    Gpio_pin_5;
    Gpio_initstructure.gpio_mode = GPIO_MODE_AF;
    Gpio_initstructure.gpio_otype = gpio_otype_pp;
    GPIO_INITSTRUCTURE.GPIO_PUPD = gpio_pupd_up;
    Gpio_initstructure.gpio_speed = Gpio_speed_50mhz;
    Gpio_init (gpiob,&gpio_initstructure);
    Gpio_pinafconfig (GPIOB,GPIO_PINSOURCE3,GPIO_AF_SPI1);
    Gpio_pinafconfig (GPIOB,GPIO_PINSOURCE4,GPIO_AF_SPI1);


    Gpio_pinafconfig (GPIOB,GPIO_PINSOURCE5,GPIO_AF_SPI1);
    Gpio_initstructure.gpio_pin = gpio_pin_15;
    Gpio_initstructure.gpio_mode = Gpio_mode_out;
    Gpio_initstructure.gpio_otype = gpio_otype_pp;
    GPIO_INITSTRUCTURE.GPIO_PUPD = gpio_pupd_up; Gpio_initstructure.gpio_speed = Gpio_speed_50MHz;

    Gpio_init (gpioa,&gpio_initstructure);

    Gpio_resetbits (GPIOA,GPIO_PIN_15);
    Gpio_initstructure.gpio_pin = Gpio_pin_12;
    Gpio_initstructure.gpio_mode = gpio_mode_in;
    GPIO_INITSTRUCTURE.GPIO_PUPD = Gpio_pupd_nopull;
    Gpio_initstructure.gpio_speed = Gpio_speed_50mhz;

    Gpio_init (gpiod,&gpio_initstructure);
    Spi_cmd (spi1,disable);
    Spi_initstructure.spi_baudrateprescaler = spi_baudrateprescaler_16;
    Spi_initstructure.spi_cpha = Spi_cpha_2edge;
    Spi_initstructure.spi_cpol = Spi_cpol_high;
    Spi_initstructure.spi_datasize = spi_datasize_8b;
    Spi_initstructure.spi_direction =spi_direction_2lines_fullduplex;
    Spi_initstructure.spi_mode = Spi_mode_master;
    Spi_initstructure.spi_nss =spi_nss_soft;
    Spi_initstructure.spi_firstbit = SPI_FIRSTBIT_MSB;
    Spi_initstructure.spi_crcpolynomial = 7;
    Spi_init (spi1,&spi_initstructure);
Spi_cmd (spi1,enable); }

Three, can communication
STM32F2 series can configuration:

* * Function Name: Can_config * Description: CAN bus Configuration function * Input: no * output: no * call: Internal call/void Can_config () {can_inittypedef C
    An_initstructure;
    Can_filterinittypedef can_filterinitstructure;
    Gpio_inittypedef gpio_initstructure;

    Rcc_apb1periphclockcmd (rcc_apb1periph_can1,enable); /**configure the PA11 PA12 to can1*/gpio_initstructure.gpio_pin = Gpio_pin_11 |
    Gpio_pin_12;
    Gpio_initstructure.gpio_mode = GPIO_MODE_AF;
    Gpio_initstructure.gpio_otype = gpio_otype_pp;
    GPIO_INITSTRUCTURE.GPIO_PUPD = gpio_pupd_up;   
    Gpio_initstructure.gpio_speed = Gpio_speed_50mhz;
    Gpio_init (gpioa,&gpio_initstructure);
    Gpio_pinafconfig (GPIOA,GPIO_PINSOURCE11,GPIO_AF_CAN1);

    Gpio_pinafconfig (GPIOA,GPIO_PINSOURCE12,GPIO_AF_CAN1);
    Can_deinit (CAN1);  Can_initstructure.can_abom = enable;//automatic offline Can_initstructure.can_awum = enable;//automatic wake-up CAN_INITSTRUCTURE.CAN_TTCM =
    disable;//triggering communication Mode Can_initstructure.can_nart = enable;//Stop message retransmission automaticallyCAN_INITSTRUCTURE.CAN_RFLM = enable;//message automatic coverage CAN_INITSTRUCTURE.CAN_TXFP = disable;//Priority Decision CAN_INITSTRUCTURE.CAN_MO
    De = can_mode_normal;//working mode/**configure baudrate 1M * * * CAN_INITSTRUCTURE.CAN_SJW = CAN_SJW_1TQ;
    CAN_INITSTRUCTURE.CAN_BS1 = CAN_BS1_6TQ;
    CAN_INITSTRUCTURE.CAN_BS2 = CAN_BS2_8TQ;
    Can_initstructure.can_prescaler = 2;

    Can_init (CAN1, &can_initstructure);
    Can_filterinitstructure.can_filteractivation = ENABLE;
    Can_filterinitstructure.can_filterfifoassignment = Can_filterfifo0;
    Can_filterinitstructure.can_filteridhigh= 0x0000;
    Can_filterinitstructure.can_filteridlow = 0x0000;
    Can_filterinitstructure.can_filtermaskidhigh = 0x0000;
    Can_filterinitstructure.can_filtermaskidlow = 0x0000;
    Can_filterinitstructure.can_filtermode =can_filtermode_idmask;
    Can_filterinitstructure.can_filternumber = 0;
    Can_filterinitstructure.can_filterscale = Can_filterscale_32bit;
    Can_filterinit (&can_filterinitstructure); CaN_itconfig (can1,can_it_fmp0,enable); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.