Example #1
0
void HAL_SPI_Begin(HAL_SPI_Interface spi, uint16_t pin)
{
    if (pin==SPI_DEFAULT_SS)
        pin = spiMap[spi].SPI_SS_Pin;

    STM32_Pin_Info* PIN_MAP = HAL_Pin_Map();

    /* Enable SPI Clock */
    *spiMap[spi].SPI_RCC_APBRegister |= spiMap[spi].SPI_RCC_APBClockEnable;

    /* Connect SPI pins to AF */
    GPIO_PinAFConfig(PIN_MAP[spiMap[spi].SPI_SCK_Pin].gpio_peripheral, PIN_MAP[spiMap[spi].SPI_SCK_Pin].gpio_pin_source, spiMap[spi].SPI_AF_Mapping);
    GPIO_PinAFConfig(PIN_MAP[spiMap[spi].SPI_MISO_Pin].gpio_peripheral, PIN_MAP[spiMap[spi].SPI_MISO_Pin].gpio_pin_source, spiMap[spi].SPI_AF_Mapping);
    GPIO_PinAFConfig(PIN_MAP[spiMap[spi].SPI_MOSI_Pin].gpio_peripheral, PIN_MAP[spiMap[spi].SPI_MOSI_Pin].gpio_pin_source, spiMap[spi].SPI_AF_Mapping);

    HAL_Pin_Mode(spiMap[spi].SPI_SCK_Pin, AF_OUTPUT_PUSHPULL);
    HAL_Pin_Mode(spiMap[spi].SPI_MISO_Pin, AF_OUTPUT_PUSHPULL);
    HAL_Pin_Mode(spiMap[spi].SPI_MOSI_Pin, AF_OUTPUT_PUSHPULL);

    HAL_Pin_Mode(pin, OUTPUT);
    HAL_GPIO_Write(pin, Bit_SET);//HIGH

    /* SPI configuration */
    spiState[spi].SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    spiState[spi].SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    spiState[spi].SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    if(spiState[spi].SPI_Data_Mode_Set != true)
    {
        //Default: SPI_MODE3
        spiState[spi].SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
        spiState[spi].SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
    }
    spiState[spi].SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    if(spiState[spi].SPI_Clock_Divider_Set != true)
    {
        /* Defaults to 15Mbit/s on SPI1, SPI2 and SPI3 */
        if(spi == HAL_SPI_INTERFACE1)
        {
            spiState[spi].SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;//60/4=15
        }
        else
        {
            spiState[spi].SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;//30/2=15
        }
    }
    if(spiState[spi].SPI_Bit_Order_Set != true)
    {
        //Default: MSBFIRST
        spiState[spi].SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    }
    spiState[spi].SPI_InitStructure.SPI_CRCPolynomial = 7;

    SPI_Init(spiMap[spi].SPI_Peripheral, &spiState[spi].SPI_InitStructure);

    SPI_Cmd(spiMap[spi].SPI_Peripheral, ENABLE);

    spiState[spi].SPI_Enabled = true;
}
void ElectronSerialPipe::rxPause(void)
{
#if USE_USART3_HARDWARE_FLOW_CONTROL_RTS_CTS
    pause_ = true;
    HAL_Pin_Mode(RTS_UC, OUTPUT);
    HAL_GPIO_Write(RTS_UC, 1);
    USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);
#endif
}
/*
 * @brief Sets a GPIO pin to HIGH or LOW.
 */
void digitalWrite(pin_t pin, uint8_t value)
{
    PinMode mode = HAL_Get_Pin_Mode(pin);
    if (mode==PIN_MODE_NONE || is_input_mode(mode))
        return;
  // Safety check
  if( !pinAvailable(pin) ) {
    return;
  }

  HAL_GPIO_Write(pin, value);
}
Example #4
0
void HAL_SPI_Begin_Ext(HAL_SPI_Interface spi, SPI_Mode mode, uint16_t pin, void* reserved)
{
    if (pin == SPI_DEFAULT_SS)
        pin = spiMap[spi].SPI_SS_Pin;

    spiState[spi].SPI_SS_Pin = pin;
    STM32_Pin_Info* PIN_MAP = HAL_Pin_Map();

    spiState[spi].mode = mode;

    /* Enable SPI Clock */
    *spiMap[spi].SPI_RCC_APBRegister |= spiMap[spi].SPI_RCC_APBClockEnable;
    *spiMap[spi].SPI_RCC_AHBRegister |= spiMap[spi].SPI_RCC_AHBClockEnable;

    /* Connect SPI pins to AF */
    GPIO_PinAFConfig(PIN_MAP[spiMap[spi].SPI_SCK_Pin].gpio_peripheral, PIN_MAP[spiMap[spi].SPI_SCK_Pin].gpio_pin_source, spiMap[spi].SPI_AF_Mapping);
    GPIO_PinAFConfig(PIN_MAP[spiMap[spi].SPI_MISO_Pin].gpio_peripheral, PIN_MAP[spiMap[spi].SPI_MISO_Pin].gpio_pin_source, spiMap[spi].SPI_AF_Mapping);
    GPIO_PinAFConfig(PIN_MAP[spiMap[spi].SPI_MOSI_Pin].gpio_peripheral, PIN_MAP[spiMap[spi].SPI_MOSI_Pin].gpio_pin_source, spiMap[spi].SPI_AF_Mapping);

    HAL_Pin_Mode(spiMap[spi].SPI_SCK_Pin, AF_OUTPUT_PUSHPULL);
    HAL_Pin_Mode(spiMap[spi].SPI_MISO_Pin, AF_OUTPUT_PUSHPULL);
    HAL_Pin_Mode(spiMap[spi].SPI_MOSI_Pin, AF_OUTPUT_PUSHPULL);

    if (mode == SPI_MODE_MASTER)
    {
        HAL_Pin_Mode(pin, OUTPUT);
        HAL_GPIO_Write(pin, Bit_SET);//HIGH
    }
    else
    {
        HAL_Pin_Mode(pin, INPUT);
    }

    /* SPI configuration */
    spiState[spi].SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    spiState[spi].SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    spiState[spi].SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    if(spiState[spi].SPI_Data_Mode_Set != true)
    {
        //Default: SPI_MODE3
        spiState[spi].SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
        spiState[spi].SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
    }
    spiState[spi].SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

    if(spiState[spi].SPI_Clock_Divider_Set != true)
    {
        /* Defaults to 15Mbit/s on SPI1, SPI2 and SPI3 */
        if(spi == HAL_SPI_INTERFACE1)
        {
            spiState[spi].SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;//60/4=15
        }
        else
        {
            spiState[spi].SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;//30/2=15
        }
    }
    if(spiState[spi].SPI_Bit_Order_Set != true)
    {
        //Default: MSBFIRST
        spiState[spi].SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    }
    spiState[spi].SPI_InitStructure.SPI_CRCPolynomial = 7;

    SPI_Init(spiMap[spi].SPI_Peripheral, &spiState[spi].SPI_InitStructure);

    if (mode == SPI_MODE_SLAVE)
    {
        /* Attach interrupt to slave select pin */
        HAL_InterruptExtraConfiguration irqConf = {0};
        irqConf.size = sizeof(irqConf);
        irqConf.IRQChannelPreemptionPriority = 1;
        irqConf.IRQChannelSubPriority = 0;

        HAL_Interrupts_Attach(pin, &HAL_SPI_SS_Handler, (void*)(spi), CHANGE, &irqConf);

        /* Switch to slave mode */
        spiState[spi].SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
        SPI_Init(spiMap[spi].SPI_Peripheral, &spiState[spi].SPI_InitStructure);
    }

    if (mode == SPI_MODE_MASTER)
    {
        /* SPI peripheral should not be enabled in Slave mode until the device is selected */
        SPI_Cmd(spiMap[spi].SPI_Peripheral, ENABLE);
    }

    spiState[spi].SPI_Enabled = true;
}