Beispiel #1
0
void HAL_Servo_Detach(uint16_t pin)
{
  const STM32_Pin_Info* pin_info = HAL_Pin_Map() + pin;

  // Disable timer's channel
  switch (pin_info->timer_ch)
  {
    case TIM_Channel_1:
      pin_info->timer_peripheral->CCER &= ~TIM_CCER_CC1E;
      break;
    case TIM_Channel_2:
      pin_info->timer_peripheral->CCER &= ~TIM_CCER_CC2E;
      break;
    case TIM_Channel_3:
      pin_info->timer_peripheral->CCER &= ~TIM_CCER_CC3E;
      break;
    case TIM_Channel_4:
      pin_info->timer_peripheral->CCER &= ~TIM_CCER_CC4E;
      break;
    default:
      break;
  }

  // Disable timer if none of its channels are enabled
  if (!(pin_info->timer_peripheral->CCER & (TIM_CCER_CC1E | TIM_CCER_CC2E | TIM_CCER_CC3E | TIM_CCER_CC4E)))
  {
    TIM_Cmd(pin_info->timer_peripheral, DISABLE);
  }
}
Beispiel #2
0
void HAL_Interrupts_Detach_Ext(uint16_t pin, uint8_t keepHandler, void* reserved)
{
  //Map the Spark Core pin to the appropriate pin on the STM32
  STM32_Pin_Info* PIN_MAP = HAL_Pin_Map();
  uint16_t gpio_pin = PIN_MAP[pin].gpio_pin;
  uint8_t GPIO_PinSource = PIN_MAP[pin].gpio_pin_source;

  //Clear the pending interrupt flag for that interrupt pin
  if (!keepHandler || !exti_channels[GPIO_PinSource].fn)
    EXTI_ClearITPendingBit(gpio_pin);

  //unregister the user's handler
  if (!keepHandler) {
    exti_channels[GPIO_PinSource].fn = NULL;
    exti_channels[GPIO_PinSource].data = NULL;
  }

  //EXTI structure to init EXT
  EXTI_InitTypeDef EXTI_InitStructure = {0};

  //Select the appropriate EXTI line
  EXTI_InitStructure.EXTI_Line = gpio_pin;
  //disable that EXTI line
  EXTI_InitStructure.EXTI_LineCmd = DISABLE;
  //send values to registers
  EXTI_Init(&EXTI_InitStructure);
}
/*
 * @brief Recalls a saved pin mode.
 */
PinMode HAL_GPIO_Recall_Pin_Mode(uint16_t pin)
{
  // Recall pin mode in STM32_Pin_Info.user_property
  STM32_Pin_Info* PIN_MAP = HAL_Pin_Map();
  uint32_t uprop = (uint32_t)PIN_MAP[pin].user_property;
  if ((uprop & 0xFF000000) != 0xAA000000)
      return PIN_MODE_NONE;

  PinMode pm = (PinMode)((uprop & 0x00FF0000) >> 16);

  // Safety check
  switch(pm)
  {
      case INPUT:
      case OUTPUT:
      case INPUT_PULLUP:
      case INPUT_PULLDOWN:
      case AF_OUTPUT_PUSHPULL:
      case AF_OUTPUT_DRAIN:
      case AN_INPUT:
      case AN_OUTPUT:
      break;

      default:
      pm = PIN_MODE_NONE;
      break;
  }

  return pm;
}
/*
 * @brief Saves a pin mode to be recalled later.
 */
void HAL_GPIO_Save_Pin_Mode(uint16_t pin)
{
  // Save pin mode in STM32_Pin_Info.user_property
  STM32_Pin_Info* PIN_MAP = HAL_Pin_Map();
  uint32_t uprop = (uint32_t)PIN_MAP[pin].user_property;
  uprop = (uprop & 0xFFFF) | (((uint32_t)PIN_MAP[pin].pin_mode & 0xFF) << 16) | (0xAA << 24);
  PIN_MAP[pin].user_property = (int32_t)uprop;
}
Beispiel #5
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;
}
Beispiel #6
0
void HAL_Servo_Write_Pulse_Width(uint16_t pin, uint16_t pulseWidth) {
    HAL_PWM_Set_Resolution(pin, SERVO_PWM_RESOLUTION);
    NRF5x_Pin_Info *pin_map = HAL_Pin_Map();
    pin_map[pin].user_data = pulseWidth;

    uint32_t period_us = 1000000 / SERVO_TIM_PWM_FREQ;
    uint32_t pwm_duty_value = pulseWidth * SERVO_PWM_MAX_VALUE / period_us;
    HAL_PWM_Write_With_Frequency_Ext(pin, pwm_duty_value, SERVO_TIM_PWM_FREQ);
}
void ElectronSerialPipe::rxResume(void)
{
#if USE_USART3_HARDWARE_FLOW_CONTROL_RTS_CTS
    if (pause_) {
        pause_ = false;
        HAL_Pin_Mode(RTS_UC, AF_OUTPUT_PUSHPULL);
        STM32_Pin_Info* PIN_MAP = HAL_Pin_Map();
        GPIO_PinAFConfig(PIN_MAP[RTS_UC].gpio_peripheral, PIN_MAP[RTS_UC].gpio_pin_source, GPIO_AF_USART3);
    }
#endif

    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
}
Beispiel #8
0
static void gpiote_interrupt_handler(nrfx_gpiote_pin_t nrf_pin, nrf_gpiote_polarity_t action) {
    uint8_t pin = NRF_PIN_LOOKUP_TABLE[nrf_pin];
    if (pin == PIN_INVALID) {
        // Ignore
        return;
    }

    NRF5x_Pin_Info* PIN_MAP = HAL_Pin_Map();

    HAL_InterruptHandler user_isr_handle = m_exti_channels[PIN_MAP[pin].exti_channel].interrupt_callback.handler;
    void *data = m_exti_channels[PIN_MAP[pin].exti_channel].interrupt_callback.data;
    if (user_isr_handle) {
        user_isr_handle(data);
    }
}
Beispiel #9
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;
}
Beispiel #10
0
void HAL_I2C_Begin(HAL_I2C_Interface i2c, I2C_Mode mode, uint8_t address, void* reserved)
{
    STM32_Pin_Info* PIN_MAP = HAL_Pin_Map();

    i2cMap[i2c]->rxBufferIndex = 0;
    i2cMap[i2c]->rxBufferLength = 0;

    i2cMap[i2c]->txBufferIndex = 0;
    i2cMap[i2c]->txBufferLength = 0;

    i2cMap[i2c]->mode = mode;
    i2cMap[i2c]->ackFailure = false;

    /* Enable I2C clock */
    *i2cMap[i2c]->I2C_RCC_APBRegister |= i2cMap[i2c]->I2C_RCC_APBClockEnable;

    /* Enable and Release I2C Reset State */
    I2C_DeInit(i2cMap[i2c]->I2C_Peripheral);

    /* Connect I2C pins to respective AF */
    GPIO_PinAFConfig(PIN_MAP[i2cMap[i2c]->I2C_SCL_Pin].gpio_peripheral, PIN_MAP[i2cMap[i2c]->I2C_SCL_Pin].gpio_pin_source, i2cMap[i2c]->I2C_AF_Mapping);
    GPIO_PinAFConfig(PIN_MAP[i2cMap[i2c]->I2C_SDA_Pin].gpio_peripheral, PIN_MAP[i2cMap[i2c]->I2C_SDA_Pin].gpio_pin_source, i2cMap[i2c]->I2C_AF_Mapping);

    HAL_Pin_Mode(i2cMap[i2c]->I2C_SCL_Pin, AF_OUTPUT_DRAIN);
    HAL_Pin_Mode(i2cMap[i2c]->I2C_SDA_Pin, AF_OUTPUT_DRAIN);

    NVIC_InitTypeDef  NVIC_InitStructure;

    NVIC_InitStructure.NVIC_IRQChannel = i2cMap[i2c]->I2C_ER_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    if(i2cMap[i2c]->mode != I2C_MODE_MASTER)
    {
        NVIC_InitStructure.NVIC_IRQChannel = i2cMap[i2c]->I2C_EV_IRQn;
        NVIC_Init(&NVIC_InitStructure);
    }

    i2cMap[i2c]->I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
    i2cMap[i2c]->I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
    i2cMap[i2c]->I2C_InitStructure.I2C_OwnAddress1 = address << 1;
    i2cMap[i2c]->I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
    i2cMap[i2c]->I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
    i2cMap[i2c]->I2C_InitStructure.I2C_ClockSpeed = i2cMap[i2c]->I2C_ClockSpeed;

    /*
        From STM32 peripheral library notes:
        ====================================
        If an error occurs (ie. I2C error flags are set besides to the monitored
        flags), the I2C_CheckEvent() function may return SUCCESS despite
        the communication hold or corrupted real state.
        In this case, it is advised to use error interrupts to monitor
        the error events and handle them in the interrupt IRQ handler.
     */
    I2C_ITConfig(i2cMap[i2c]->I2C_Peripheral, I2C_IT_ERR, ENABLE);

    if(i2cMap[i2c]->mode != I2C_MODE_MASTER)
    {
        I2C_ITConfig(i2cMap[i2c]->I2C_Peripheral, I2C_IT_EVT | I2C_IT_BUF, ENABLE);
    }

    /* Enable the I2C peripheral */
    I2C_Cmd(i2cMap[i2c]->I2C_Peripheral, ENABLE);

    /* Apply I2C configuration after enabling it */
    I2C_Init(i2cMap[i2c]->I2C_Peripheral, &i2cMap[i2c]->I2C_InitStructure);

    i2cMap[i2c]->I2C_Enabled = true;
}
Beispiel #11
0
  the License, or (at your option) any later version.

  Adafruit Dot Star is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with NeoPixel.  If not, see <http://www.gnu.org/licenses/>.
  ------------------------------------------------------------------------*/

#include "dotstar.h"

#if defined(PLATFORM_ID)  //Only defined if a Particle device
  #include "application.h"
  STM32_Pin_Info* PIN_MAP = HAL_Pin_Map(); // Pointer required for highest access speed
#if (PLATFORM_ID == 0)  // Core
  #define pinLO(_pin) (PIN_MAP[_pin].gpio_peripheral->BRR = PIN_MAP[_pin].gpio_pin)
  #define pinHI(_pin) (PIN_MAP[_pin].gpio_peripheral->BSRR = PIN_MAP[_pin].gpio_pin)
#elif (PLATFORM_ID == 6) // Photon
  #define pinLO(_pin) (PIN_MAP[_pin].gpio_peripheral->BSRRH = PIN_MAP[_pin].gpio_pin)
  #define pinHI(_pin) (PIN_MAP[_pin].gpio_peripheral->BSRRL = PIN_MAP[_pin].gpio_pin)
#else
  #error "*** PLATFORM_ID not supported by this library. PLATFORM should be Core or Photon ***"
#endif
#endif

#define pinSet(_pin, _hilo) (_hilo ? pinHI(_pin) : pinLO(_pin))

#define spi_out(n) (void)SPI.transfer(n)
Beispiel #12
0
uint16_t HAL_Servo_Read_Pulse_Width(uint16_t pin) {
    NRF5x_Pin_Info* pin_map = HAL_Pin_Map();
    return pin_map[pin].user_data;
}
Beispiel #13
0
void HAL_Interrupts_Attach(uint16_t pin, HAL_InterruptHandler handler, void* data, InterruptMode mode, HAL_InterruptExtraConfiguration* config)
{
  uint8_t GPIO_PortSource = 0;    //variable to hold the port number

  //EXTI structure to init EXT
  EXTI_InitTypeDef EXTI_InitStructure = {0};
  //NVIC structure to set up NVIC controller
  NVIC_InitTypeDef NVIC_InitStructure = {0};

  //Map the Spark pin to the appropriate port and pin on the STM32
  STM32_Pin_Info* PIN_MAP = HAL_Pin_Map();
  GPIO_TypeDef *gpio_port = PIN_MAP[pin].gpio_peripheral;
  uint16_t gpio_pin = PIN_MAP[pin].gpio_pin;
  uint8_t GPIO_PinSource = PIN_MAP[pin].gpio_pin_source;


  //Clear pending EXTI interrupt flag for the selected pin
  EXTI_ClearITPendingBit(gpio_pin);

  //Select the port source
  if (gpio_port == GPIOA)
  {
    GPIO_PortSource = 0;
  }
  else if (gpio_port == GPIOB)
  {
    GPIO_PortSource = 1;
  }
  else if (gpio_port == GPIOC)
  {
    GPIO_PortSource = 2;
  }
  else if (gpio_port == GPIOD)
  {
    GPIO_PortSource = 3;
  }

  // Register the handler for the user function name
  if (config && config->version >= HAL_INTERRUPT_EXTRA_CONFIGURATION_VERSION_2 && config->keepHandler) {
    // keep the old handler
  } else {
    exti_channels[GPIO_PinSource].fn = handler;
    exti_channels[GPIO_PinSource].data = data;
  }

  //Connect EXTI Line to appropriate Pin
  SYSCFG_EXTILineConfig(GPIO_PortSource, GPIO_PinSource);

  //Configure GPIO EXTI line
  EXTI_InitStructure.EXTI_Line = gpio_pin;//EXTI_Line;

  //select the interrupt mode
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  switch (mode)
  {
    //case LOW:
    //There is no LOW mode in STM32, so using falling edge as default
    //EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
    //break;
    case CHANGE:
      //generate interrupt on rising or falling edge
      EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
      break;
    case RISING:
      //generate interrupt on rising edge
      EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
      break;
    case FALLING:
      //generate interrupt on falling edge
      EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
      break;
  }

  //enable EXTI line
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  //send values to registers
  EXTI_Init(&EXTI_InitStructure);

  //configure NVIC
  //select NVIC channel to configure
  NVIC_InitStructure.NVIC_IRQChannel = GPIO_IRQn[GPIO_PinSource];
  if (config == NULL) {
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 14;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  } else {
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = config->IRQChannelPreemptionPriority;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = config->IRQChannelSubPriority;

    // Keep the same priority
    if (config->version >= HAL_INTERRUPT_EXTRA_CONFIGURATION_VERSION_2) {
      if (config->keepPriority) {
        uint32_t priorityGroup = NVIC_GetPriorityGrouping();
        uint32_t priority = NVIC_GetPriority(NVIC_InitStructure.NVIC_IRQChannel);
        uint32_t p, sp;
        NVIC_DecodePriority(priority, priorityGroup, &p, &sp);
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = p;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = sp;
      }
    }
  }
  //enable IRQ channel
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  //update NVIC registers
  NVIC_Init(&NVIC_InitStructure);
}
Beispiel #14
0
uint32_t HAL_Interrupts_Pin_IRQn(pin_t pin) {
  STM32_Pin_Info* PIN_MAP = HAL_Pin_Map();
  return GPIO_IRQn[PIN_MAP[pin].gpio_pin];
}
void ElectronSerialPipe::begin(unsigned int baud)
{
    //HAL_USART_Begin(HAL_USART_SERIAL3, baud);
    USART_DeInit(USART3);

#if USE_USART3_HARDWARE_FLOW_CONTROL_RTS_CTS
    // Configure USART RTS and CTS as alternate function push-pull
    HAL_Pin_Mode(RTS_UC, AF_OUTPUT_PUSHPULL);
    HAL_Pin_Mode(CTS_UC, AF_OUTPUT_PUSHPULL);
#endif
    // Configure USART Rx and Tx as alternate function push-pull, and enable GPIOA clock
    HAL_Pin_Mode(RXD_UC, AF_OUTPUT_PUSHPULL);
    HAL_Pin_Mode(TXD_UC, AF_OUTPUT_PUSHPULL);

    // Enable USART Clock
    RCC->APB1ENR |= RCC_APB1Periph_USART3;

    // Connect USART pins to AFx
    STM32_Pin_Info* PIN_MAP = HAL_Pin_Map();

#if USE_USART3_HARDWARE_FLOW_CONTROL_RTS_CTS
    GPIO_PinAFConfig(PIN_MAP[RTS_UC].gpio_peripheral, PIN_MAP[RTS_UC].gpio_pin_source, GPIO_AF_USART3);
    GPIO_PinAFConfig(PIN_MAP[CTS_UC].gpio_peripheral, PIN_MAP[CTS_UC].gpio_pin_source, GPIO_AF_USART3);
#endif
    GPIO_PinAFConfig(PIN_MAP[RXD_UC].gpio_peripheral, PIN_MAP[RXD_UC].gpio_pin_source, GPIO_AF_USART3);
    GPIO_PinAFConfig(PIN_MAP[TXD_UC].gpio_peripheral, PIN_MAP[TXD_UC].gpio_pin_source, GPIO_AF_USART3);

    // NVIC Configuration
    NVIC_InitTypeDef NVIC_InitStructure;
    // Enable the USART Interrupt
    NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    // USART default configuration
    // USART configured as follow:
    // - BaudRate = (set baudRate as 9600 baud)
    // - Word Length = 8 Bits
    // - One Stop Bit
    // - No parity
    // - Hardware flow control disabled for Serial1/2/4/5
    // - Hardware flow control enabled (RTS and CTS signals) for Serial3
    // - Receive and transmit enabled
    USART_InitStructure.USART_BaudRate = baud;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
#if USE_USART3_HARDWARE_FLOW_CONTROL_RTS_CTS
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;
#endif

    // Configure USART
    USART_Init(USART3, &USART_InitStructure);

    // Enable the USART
    USART_Cmd(USART3, ENABLE);

    // Enable USART Receive and Transmit interrupts
    USART_ITConfig(USART3, USART_IT_TXE, ENABLE);
    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
}