Пример #1
0
/*************************************************************************
 * Function Name: UartRead
 * Parameters:  UartNum_t Uart, pInt8U pBuffer, Int32U BufferSize
 *
 * Return: Int32U
 *
 * Description: Read received data from UART.
 *              Return number of readied characters
 *
 *************************************************************************/
Int32U UartRead(UartNum_t Uart, pInt8U pBuffer, Int32U BufferSize)
{
Int32U Count;
pUartFifo_t pUartFifo;

  switch(Uart)
  {
  case UART_1:
    pUartFifo = pUart1RxFifo;
    break;
  case UART_2:
    pUartFifo = pUart2RxFifo;
    break;
  case UART_3:
    pUartFifo = pUart3RxFifo;
    break;
  default:
    return(0);
  }

  for (Count = 0; Count < BufferSize; ++Count)
  {
    ENTR_CRT_SECTION();
    if(!FifoPop(pUartFifo,pBuffer+Count))
    {
      EXT_CRT_SECTION();
      break;
    }
    EXT_CRT_SECTION();
  }
  return(Count);
}
Пример #2
0
void main(void)
{

    ENTR_CRT_SECTION();
    /* Setup STM32 system (clock, PLL and Flash configuration) */
    SystemInit();

    /* Set the Vector Table base location at 0x08000000 */
    NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);

    // SysTick end of count event each 0.5s with input clock equal to 9MHz (HCLK/8, default)
    SysTick_Config(1500000);
    SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);

    // Step motor init
    StepMotorInit();
    // I2C1 init
    I2C1_Init();

    EXT_CRT_SECTION();

    // GLCD init
    GLCD_PowerUpInit((pInt8U)IAR_Logo.pPicStream); //(
    GLCD_Backlight(BACKLIGHT_ON);
    GLCD_SetFont(&Terminal_9_12_6,0x000F00,0x00FF0);
    GLCD_SetWindow(10,104,131,131);

    // Init Accl sensor
    if(FALSE == Accl_Init())
    {
        // Initialization fault
        GLCD_TextSetPos(0,0);
        GLCD_print("\fLIS3LV020 Init.\r\nfault\r\n");
        while(1);
    }
    Car_Init();
    while(1)
    {
        A=position();
        /*   A = accX[1];
           B = velX[1];
           C = posX[1]; */

        if(SysTickFl) //
        {
            SysTickFl = FALSE;
            GLCD_TextSetPos(0,0);
            GLCD_print("\f%d Deg\r\n",A);
        }

        GoCar(Test, TestTurn);
    }
}
Пример #3
0
/*************************************************************************
 * Function Name: UartWrite
 * Parameters:  UartNum_t Uart, pInt8U pBuffer, Int32U BufferSize
 *
 * Return: Int32U
 *
 * Description: Write a data to UART. Return number of successful
 *  transmitted bytes
 *
 *************************************************************************/
Int32U UartWrite(UartNum_t Uart, pInt8U pBuffer, Int32U BufferSize)
{
Int32U Count = 0;
pUartFifo_t pUartFifo;
USART_TypeDef * pUart;

  switch(Uart)
  {
  case UART_1:
    pUart = USART1;
    pUartFifo = pUart1TxFifo;
    break;
  case UART_2:
    pUart = USART2;
    pUartFifo = pUart2TxFifo;
    break;
  case UART_3:
    pUart = USART3;
    pUartFifo = pUart3TxFifo;
    break;
  default:
    return(0);
  }

  if(BufferSize != 0)
  {
    ENTR_CRT_SECTION();
    if(pUartFifo->PopIndx == pUartFifo->PushIndx)
    {
      USART_SendData(pUart,*pBuffer);
      USART_ITConfig(pUart,USART_IT_TXE ,ENABLE);
      ++Count;
    }

    for ( ; Count < BufferSize; ++Count)
    {
      if(!FifoPush(pUartFifo,*(pBuffer+Count)))
      {
        break;
      }
    }
    EXT_CRT_SECTION();
  }
  return(Count);
}
Пример #4
0
 void main(void)
{

  ENTR_CRT_SECTION();
  /* Setup STM32 system (clock, PLL and Flash configuration) */
  SystemInit();

  /* Set the Vector Table base location at 0x08000000 */
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
  


  // I2C1 init
  I2C1_Init();

  EXT_CRT_SECTION();

  // GLCD init
  GLCD_PowerUpInit(0x0); //(
  GLCD_Backlight(BACKLIGHT_ON);
  GLCD_SetFont(&Terminal_9_12_6,0x000F00,0x00FF0);
  GLCD_SetWindow(10,10,131,131);

  // Init Accl sensor
  if(FALSE == Accl_Init())
  {
    // Initialization fault
    GLCD_TextSetPos(0,0);
    GLCD_print("\fLIS3LV020 Init.\r\nfault\r\n");
    while(1);
  }
    //Init CarControl and Delay
  Car_Init();
  DWT_Init();
  HCSR04_Init();
    // SysTick end of count event each 0.5s with input clock equal to 9MHz (HCLK/8, default)
  SysTick_Config(150000);
  SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);

  DWT_Delayms(1000);
  while(1)
  {

    //while(1){GLCD_print("Current state: %d \r",GPIO_ReadInputDataBit(JS_LEFT_PORT, JS_LEFT_MASK));} 
    //car_feedback=accl_feedback();
 /*   A = accX[1];
    B = velX[1];
    C = posX[1]; */
if(SysTickF1)
{
        SysTickF1 = FALSE;
        //GLCD_TextSetPos(0,0);
            GLCD_print("%d, %d \r", get_Xvel(), accl_feedback());
            DWT_Delayms(500);
            
}
    if(NewInstr) //
    {
      DWT_Delayms(1000);
      NewInstr = FALSE;
    
    //GoCar(Test, TestTurn);
    // 1. Give command (desired state)
    //desiredState;

    // 2. Run machine learning to test action
    action = goToState(car_instr);
    GoCars(action);
    //  testExp();
    int runTime = 0;
    while (runTime < 1){
        DWT_Delayms(700);
        runTime++;
    }
            GLCD_TextSetPos(0,0);
            GLCD_print("\f%d,%d;%d,%d\r\n", get_X_accFeedback(0), get_Y_accFeedback(0), get_X_vel(0), get_Y_vel(0));
            GLCD_print("%d,%d;%d,%d\r\n", get_X_accFeedback(1), get_Y_accFeedback(1), get_X_vel(1), get_Y_vel(1));
            GLCD_print("%d,%d;%d,%d\r\n", get_X_accFeedback(2), get_Y_accFeedback(2), get_X_vel(2), get_Y_vel(2));
            GLCD_print("%d,%d;%d,%d\r\n", get_X_accFeedback(3), get_Y_accFeedback(3), get_X_vel(3), get_Y_vel(3));
            GLCD_print("%d,%d;%d,%d\r\n", get_X_accFeedback(4), get_Y_accFeedback(4), get_X_vel(4), get_Y_vel(4));
            GLCD_print("%d,%d;%d,%d\r\n", get_X_accFeedback(5), get_Y_accFeedback(5), get_X_vel(5), get_Y_vel(5));
            GLCD_print("%d,%d;%d,%d\r\n", get_X_accFeedback(6), get_Y_accFeedback(6), get_X_vel(6), get_Y_vel(6));
      //      GLCD_print("%d,%d;%d,%d\r\n", get_X_accFeedback(7), get_Y_accFeedback(7), get_X_vel(7), get_Y_vel(7));
                car_instr=car_stop;
    GoCars(car_instr);
            DWT_Delayms(2000);
    }
    //GoCars(3); //Stopping the car
    
    
    // 3. Wait x ms
    //Wait in ML-method testAllActions
    
    // 4. Return value from accelerometer
    //Return value from accelerometer in ML-method testAllActions
    
    // 5. Evaluate action compared to state
    //Already implemented in ML-file. No need to alter (probably)
    
    // 6. Repeat 3-5 until all actions has been tested
    //Already implemented in ML-file. No need to alter (probably)
    
    // 7. Choose the correct state
    //Already implemented in ML-file. No need to alter (probably)
  }
}
Пример #5
0
/*************************************************************************
 * Function Name: SPI2_DmaTransfer
 * Parameters: pInt8U pData,Int32U Size, SPI_TransferDir_t SPI_TransferDir
 * Return: none
 *
 * Description: DMA transfer
 *
 *************************************************************************/
void SPI2_DmaTransfer(pInt8U pData,Int32U Size, SPI_TransferDir_t SPI_TransferDir)
{
DMA_InitTypeDef  DMA_InitStructure;
Int32U Dummy = 0xFF;

  // Initialize DMA Rx channel
  DMA_DeInit(DMA_Channel4);
  DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&SPI2->DR;
  DMA_InitStructure.DMA_MemoryBaseAddr = (SPI_TransferDir == SPI_RECEIVE)?(Int32U)pData:(Int32U)&Dummy;
  DMA_InitStructure.DMA_BufferSize = Size;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = (SPI_TransferDir == SPI_RECEIVE)?DMA_MemoryInc_Enable:DMA_MemoryInc_Disable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  // Init channel
  DMA_Init(DMA_Channel4, &DMA_InitStructure);

  // Initialize DMA Tx channel
  DMA_DeInit(DMA_Channel5);
  DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&SPI2->DR;
  DMA_InitStructure.DMA_MemoryBaseAddr = (SPI_TransferDir == SPI_TRANSMIT)?(Int32U)pData:(Int32U)&Dummy;
  DMA_InitStructure.DMA_BufferSize = Size;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = (SPI_TransferDir == SPI_TRANSMIT)?DMA_MemoryInc_Enable:DMA_MemoryInc_Disable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  // Init channel
  DMA_Init(DMA_Channel5, &DMA_InitStructure);
  // Enable SPI2 DMA transfer
  SPI_DMACmd(SPI2,SPI_DMAReq_Rx,ENABLE);
  SPI_DMACmd(SPI2,SPI_DMAReq_Tx,ENABLE);


#ifdef DMA_ERRATA
  ENTR_CRT_SECTION();

  // Enable channel
  DMA_Cmd(DMA_Channel4,ENABLE);
  DMA_Cmd(DMA_Channel5,ENABLE);

  while(1)
  {
    if (  (DMA_GetITStatus(DMA_IT_TE4) == SET)
       || (DMA_GetITStatus(DMA_IT_TE5) == SET))
    {
      DMA_ClearITPendingBit(DMA_IT_GL4 | DMA_IT_GL5);
      DMA_Cmd(DMA_Channel5,DISABLE);
      DMA_Cmd(DMA_Channel4,DISABLE);
      break;
    }
    if (  (DMA_GetITStatus(DMA_IT_TC4) == SET)
       && (DMA_GetITStatus(DMA_IT_TC5) == SET))
    {
      break;
    }
  };
  EXT_CRT_SECTION();
#else
  // set the flag DMA Transfer in progress
  TransferStatus = TRUE;

  DMA_ITConfig(DMA_Channel4, DMA_IT_TC | DMA_IT_TE, ENABLE);
  DMA_ITConfig(DMA_Channel5, DMA_IT_TE, ENABLE);

  // Enable SPI2 DMA transfer
  SPI_DMACmd(SPI2,SPI_DMAReq_Rx,ENABLE);
  SPI_DMACmd(SPI2,SPI_DMAReq_Tx,ENABLE);
  while(TransferStatus);
#endif
  // wait until SPI transmit FIFO isn't empty
  while(SPI_GetFlagStatus(SPI2, SPI_FLAG_TXE)==RESET);
  // wait until SPI receive FIFO isn't empty
  while(SPI_GetFlagStatus(SPI2, SPI_FLAG_RXNE)==SET);

  SPI_DMACmd(SPI2,SPI_DMAReq_Tx,DISABLE);
  SPI_DMACmd(SPI2,SPI_DMAReq_Rx,DISABLE);

}