Exemplo n.º 1
0
int HAL_Set_Direct_Interrupt_Handler(IRQn_Type irqn, HAL_Direct_Interrupt_Handler handler, uint32_t flags, void* reserved)
{
  if (irqn < NonMaskableInt_IRQn || irqn > HASH_RNG_IRQn) {
    return 1;
  }

  int32_t state = HAL_disable_irq();
  volatile uint32_t* isrs = (volatile uint32_t*)SCB->VTOR;

  if (handler == NULL && (flags & HAL_DIRECT_INTERRUPT_FLAG_RESTORE)) {
    // Restore
    HAL_Core_Restore_Interrupt(irqn);
  } else {
    isrs[IRQN_TO_IDX(irqn)] = (uint32_t)handler;
  }

  if (flags & HAL_DIRECT_INTERRUPT_FLAG_DISABLE) {
    // Disable
    NVIC_DisableIRQ(irqn);
  } else if (flags & HAL_DIRECT_INTERRUPT_FLAG_ENABLE) {
    NVIC_EnableIRQ(irqn);
  }

  HAL_enable_irq(state);

  return 0;
}
void USBD_Composite_Unregister_All() {
  int32_t irq = HAL_disable_irq();
  s_Classes = NULL;
  s_Classes_Count = 0;
  memset(s_Class_Entries, 0, sizeof(s_Class_Entries));
  HAL_enable_irq(irq);
}
Exemplo n.º 3
0
void HAL_USART_Send_Break(HAL_USART_Serial serial, void* reserved)
{
  int32_t state = HAL_disable_irq();
  while((usartMap[serial]->usart_peripheral->CR1 & USART_CR1_SBK) == SET);
  USART_SendBreak(usartMap[serial]->usart_peripheral);
  while((usartMap[serial]->usart_peripheral->CR1 & USART_CR1_SBK) == SET);
  HAL_enable_irq(state);
}
void USBD_Composite_Set_State(void* cls, bool state) {
  int32_t irq = HAL_disable_irq();
  for(USBD_Composite_Class_Data* c = s_Classes; c != NULL; c = c->next) {
    if (cls && c == cls) {
      c->active = state;
      break;
    }
  }
  HAL_enable_irq(irq);
}
Exemplo n.º 5
0
int32_t HAL_USB_USART_Available_Data(HAL_USB_USART_Serial serial)
{
    int32_t available = 0;
    int state = HAL_disable_irq();
    available = ring_data_avail(usbUsartMap[serial].data->rx_buffer_length,
                                usbUsartMap[serial].data->rx_buffer_head,
                                usbUsartMap[serial].data->rx_buffer_tail);
    HAL_enable_irq(state);
    return available;
}
Exemplo n.º 6
0
void HAL_USB_USART_Init(HAL_USB_USART_Serial serial, const HAL_USB_USART_Config* config)
{
    if (usbUsartMap[serial].data == NULL) {
        usbUsartMap[serial].data = &usbUsartInstanceData[serial];

        if (serial == HAL_USB_USART_SERIAL) {
            usbUsartMap[serial].data->ep_in_data = CDC0_IN_EP;
            usbUsartMap[serial].data->ep_in_int = CDC0_CMD_EP;
            usbUsartMap[serial].data->ep_out_data = CDC0_OUT_EP;
            usbUsartMap[serial].data->name = USBD_PRODUCT_STRING " " "Serial";
        } else if (serial == HAL_USB_USART_SERIAL1) {
            usbUsartMap[serial].data->ep_in_data = CDC1_IN_EP;
            usbUsartMap[serial].data->ep_in_int = CDC1_CMD_EP;
            usbUsartMap[serial].data->ep_out_data = CDC1_OUT_EP;
            usbUsartMap[serial].data->name = USBD_PRODUCT_STRING " " "USBSerial1";
        }

        usbUsartMap[serial].data->req_handler = HAL_USB_USART_Request_Handler;
    }

    if (config && (
        usbUsartMap[serial].data->rx_buffer == NULL ||
        usbUsartMap[serial].data->rx_buffer_size == 0 || 
        usbUsartMap[serial].data->tx_buffer == NULL ||
        usbUsartMap[serial].data->tx_buffer_size == 0))
    {
        HAL_USB_USART_Config conf;
        memset(&conf, 0, sizeof(conf));
        memcpy(&conf, config, (config->size>sizeof(conf) ? sizeof(conf) : config->size));

        if (!conf.rx_buffer) {
            conf.rx_buffer = malloc(USB_RX_BUFFER_SIZE);
            conf.rx_buffer_size = USB_RX_BUFFER_SIZE;
        }
        if (!conf.tx_buffer) {
            conf.tx_buffer = malloc(USB_TX_BUFFER_SIZE);
            conf.tx_buffer_size = USB_TX_BUFFER_SIZE;
        }

        // Just in case disable interrupts
        int32_t state = HAL_disable_irq();
        usbUsartMap[serial].data->rx_buffer = conf.rx_buffer;
        usbUsartMap[serial].data->rx_buffer_size = conf.rx_buffer_size;

        usbUsartMap[serial].data->tx_buffer = conf.tx_buffer;
        usbUsartMap[serial].data->tx_buffer_size = conf.tx_buffer_size;
        HAL_enable_irq(state);
    }

    if (!usbUsartMap[serial].cls) {
        usbUsartMap[serial].cls = USBD_Composite_Register(&USBD_MCDC_cb, usbUsartMap[serial].data, serial == HAL_USB_USART_SERIAL ? 1 : 0);
        usbUsartMap[serial].data->cls = usbUsartMap[serial].cls;
        USBD_Composite_Set_State(usbUsartMap[serial].cls, false);
    }
}
Exemplo n.º 7
0
void HAL_SPI_DMA_Transfer(HAL_SPI_Interface spi, void* tx_buffer, void* rx_buffer, uint32_t length, HAL_SPI_DMA_UserCallback userCallback)
{
    int32_t state = HAL_disable_irq();
    spiState[spi].SPI_DMA_UserCallback = userCallback;
    /* Config and initiate DMA transfer */
    HAL_SPI_DMA_Config(spi, tx_buffer, rx_buffer, length);
    if (spiState[spi].mode == SPI_MODE_SLAVE && spiState[spi].SPI_SS_State == 1)
    {
        SPI_Cmd(spiMap[spi].SPI_Peripheral, ENABLE);
    }
    HAL_enable_irq(state);
}
bool USBD_Composite_Get_State(void* cls) {
  bool state = false;
  int32_t irq = HAL_disable_irq();
  for(USBD_Composite_Class_Data* c = s_Classes; c != NULL; c = c->next) {
    if (cls && c == cls) {
      state = c->active;
      break;
    }
  }
  HAL_enable_irq(irq);

  return state;
}
Exemplo n.º 9
0
void HAL_Core_Button_Mirror_Pin_Disable(uint8_t bootloader, uint8_t button, void* reserved) {
    (void)button; // unused
    int32_t state = HAL_disable_irq();
    if (HAL_Buttons[BUTTON1_MIRROR].pin != PIN_INVALID) {
        HAL_Interrupts_Detach_Ext(HAL_Buttons[BUTTON1_MIRROR].pin, 1, NULL);
        HAL_Buttons[BUTTON1_MIRROR].active = 0;
        HAL_Buttons[BUTTON1_MIRROR].pin = PIN_INVALID;
    }
    HAL_enable_irq(state);

    if (bootloader) {
        BUTTON_Mirror_Persist(NULL);
    }
}
Exemplo n.º 10
0
int32_t HAL_USB_USART_Available_Data_For_Write(HAL_USB_USART_Serial serial)
{
    if (HAL_USB_USART_Is_Connected(serial))
    {
        int state = HAL_disable_irq();
        int32_t available = ring_space_avail(usbUsartMap[serial].data->tx_buffer_size,
                                             usbUsartMap[serial].data->tx_buffer_head,
                                             usbUsartMap[serial].data->tx_buffer_tail);
        HAL_enable_irq(state);
        return available;
    }

    return -1;
}
uint8_t USBD_Composite_Registered_Count(bool onlyActive) {
  uint8_t registered = (uint8_t)s_Classes_Count;

  if (onlyActive) {
    registered = 0;
    int32_t irq = HAL_disable_irq();
    for(USBD_Composite_Class_Data* c = s_Classes; c != NULL; c = c->next) {
      if (c->active)
        registered++;
    }
    HAL_enable_irq(irq);
  }

  return registered;
}
Exemplo n.º 12
0
int32_t HAL_USB_USART_Receive_Data(HAL_USB_USART_Serial serial, uint8_t peek)
{
    if (HAL_USB_USART_Available_Data(serial) > 0)
    {
        int state = HAL_disable_irq();
        uint8_t data = usbUsartMap[serial].data->rx_buffer[usbUsartMap[serial].data->rx_buffer_tail];
        if (!peek) {
            usbUsartMap[serial].data->rx_buffer_tail = ring_wrap(usbUsartMap[serial].data->rx_buffer_length,
                                                                 usbUsartMap[serial].data->rx_buffer_tail + 1);
        }
        HAL_enable_irq(state);
        return data;
    }

    return -1;
}
void USBD_Composite_Unregister(void* cls, void* priv) {
  int32_t irq = HAL_disable_irq();
  USBD_Composite_Class_Data* prev = NULL;
  for(USBD_Composite_Class_Data* c = s_Classes; c != NULL; prev = c, c = c->next) {
    if ((cls && c == cls) || (priv && c->priv == priv)) {
      c->inuse = 0;
      if (prev) {
        prev->next = c->next;
      } else {
        s_Classes = c->next;
      }
      s_Classes_Count--;
      break;
    }
  }
  HAL_enable_irq(irq);
}
void* USBD_Composite_Register(USBD_Multi_Instance_cb_Typedef* cb, void* priv, uint8_t front) {
  if (s_Classes_Count >= USBD_COMPOSITE_MAX_CLASSES || cb == NULL)
    return NULL;

  int32_t irq = HAL_disable_irq();
  USBD_Composite_Class_Data* cls = NULL;
  for (int i = 0; i < USBD_COMPOSITE_MAX_CLASSES; i++) {
    if (s_Class_Entries[i].inuse == 0) {
      cls = s_Class_Entries + i;
      break;
    }
  }

  if (cls) {
    cls->inuse = 1;
    cls->active = 1;
    cls->cb = cb;
    cls->priv = priv;
    cls->epMask = 0xffffffff;
    cls->interfaces = 0;
    cls->firstInterface = 0;
    cls->next = NULL;
    cls->cfg = NULL;

    if (s_Classes == NULL) {
      s_Classes = cls;
    } else {
      USBD_Composite_Class_Data* c = s_Classes;
      if (!front) {
        while(c->next) {
          c = c->next;
        }
        c->next = cls;
      } else {
        s_Classes = cls;
        cls->next = c;
      }
    }
    s_Classes_Count++;
  }
  HAL_enable_irq(irq);
  return (void*)cls;
}
Exemplo n.º 15
0
int32_t HAL_USB_USART_Send_Data(HAL_USB_USART_Serial serial, uint8_t data)
{
    int32_t ret = -1;
    int32_t available = 0;
    do {
        available = HAL_USB_USART_Available_Data_For_Write(serial);
    }
    while (available < 1 && available != -1 && HAL_USB_WillPreempt());
    // Confirm once again that the Host is connected
    int32_t state = HAL_disable_irq();
    if (HAL_USB_USART_Is_Connected(serial) && available > 0)
    {
        usbUsartMap[serial].data->tx_buffer[usbUsartMap[serial].data->tx_buffer_head] = data;
        usbUsartMap[serial].data->tx_buffer_head = ring_wrap(usbUsartMap[serial].data->tx_buffer_size,
                                                             usbUsartMap[serial].data->tx_buffer_head + 1);
        ret = 1;
    }
    HAL_enable_irq(state);

    return ret;
}
Exemplo n.º 16
0
void HAL_Core_Execute_Stop_Mode(void)
{
    int32_t state = HAL_disable_irq();
	if((BKP_ReadBackupRegister(BKP_DR9) >> 12) == 0xA)
	{
		uint16_t wakeUpPin = BKP_ReadBackupRegister(BKP_DR9) & 0xFF;
		InterruptMode edgeTriggerMode = (InterruptMode)((BKP_ReadBackupRegister(BKP_DR9) >> 8) & 0x0F);

		/* Clear Stop mode system flag */
		BKP_WriteBackupRegister(BKP_DR9, 0xFFFF);

		if ((wakeUpPin < TOTAL_PINS) && (edgeTriggerMode <= FALLING))
		{
			PinMode wakeUpPinMode = INPUT;

			/* Set required pinMode based on edgeTriggerMode */
			switch(edgeTriggerMode)
			{
			case CHANGE:
				wakeUpPinMode = INPUT;
				break;

			case RISING:
				wakeUpPinMode = INPUT_PULLDOWN;
				break;

			case FALLING:
				wakeUpPinMode = INPUT_PULLUP;
				break;
			}
			HAL_Pin_Mode(wakeUpPin, wakeUpPinMode);

			/* Configure EXTI Interrupt : wake-up from stop mode using pin interrupt */
			HAL_Interrupts_Attach(wakeUpPin, NULL, NULL, edgeTriggerMode, NULL);

			/* Request to enter STOP mode with regulator in low power mode */
			PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);

			/* Detach the Interrupt pin */
	        HAL_Interrupts_Detach(wakeUpPin);

            /* Disable RTC Alarm */
            HAL_RTC_Cancel_UnixAlarm();

			/* At this stage the system has resumed from STOP mode */
			/* Enable HSE, PLL and select PLL as system clock source after wake-up from STOP */

			/* Enable HSE */
			RCC_HSEConfig(RCC_HSE_ON);

			/* Wait till HSE is ready */
			if(RCC_WaitForHSEStartUp() != SUCCESS)
			{
				/* If HSE startup fails try to recover by system reset */
				NVIC_SystemReset();
			}

			/* Enable PLL */
			RCC_PLLCmd(ENABLE);

			/* Wait till PLL is ready */
			while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);

			/* Select PLL as system clock source */
			RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

			/* Wait till PLL is used as system clock source */
			while(RCC_GetSYSCLKSource() != 0x08);
		}
	}
    HAL_enable_irq(state);
}