AnsweredAssumed Answered

VCP with STM32F4 Interface with FreeRTOS

Question asked by Manish on Sep 30, 2013
 am working on USB Serial with STM32F4 Discovery and FreeRTOS.I am using arm-none-eabi toolchain.
I have Write Code for USB-serial Implementation using USB device and OTG library.I have verified code and Serial communication.Thats working Fine.

But when I'm tring to implement it using freeRTOS the USB virtual port will  not detected.i.e USB Device Driver will not loaded.

I thinks it may problem of Interrupts related to USB Driver
.I am attaching both USB-serial working code (without freeRTOS) and USB-serial code (with freeRTOS)
and following are main.c for both...Plz guide me regarding that.
####################################################################




//..................................main.c without FreeRTOS.................................................


#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>


#include "stm32f4xx_conf.h"
#include "stm32f4xx.h"
#include "main.h"




#include "usbd_cdc_core.h"
#include "usbd_usr.h"
#include "usbd_desc.h"
#include "usbd_cdc_vcp.h"




#define MAX_COUNT 3 
#define SIZE 1000


float adcValue[SIZE];
//uint32_t i;
uint32_t count = 0;


uint32_t ADCx = ADC1;
//uint32_t USARTx = USART1;


// Private variables
volatile uint32_t time_var1, time_var2;
__ALIGN_BEGIN USB_OTG_CORE_HANDLE  USB_OTG_dev __ALIGN_END;


// Private function prototypes
void AdcTask();
void initx();
void Delay(volatile uint32_t nCount);
void init();
void calculation_test();




void AdcTask(){


     ADC_SoftwareStartConv(ADCx);
        ADC_ITConfig(ADCx,ADC_IT_EOC,ENABLE);
}




void initx(){


    adcInit();


}
double val;
void ADC_IRQHandler(void){
 
      val= (float)ADC_GetConversionValue(ADCx);
     
}






int main(void) {
     init();
        initx();


     /*
      * Disable STDOUT buffering. Otherwise nothing will be printed
      * before a newline character or when the buffer is flushed.
      */
     setbuf(stdout, NULL);
        AdcTask();
     calculation_test();
     
     
     for(;;) {
          GPIO_SetBits(GPIOD, GPIO_Pin_12);
          Delay(10);
          GPIO_ResetBits(GPIOD, GPIO_Pin_12);
          Delay(10);
     }


     return 0;
}


void calculation_test() {
     float a = 1.001;
     int iteration = 0;
     for(;;)
     {


     
          GPIO_SetBits(GPIOD, GPIO_Pin_12);
          Delay(10);
          GPIO_ResetBits(GPIOD, GPIO_Pin_12);
          Delay(10);


          time_var2 = 0;


                int j=0;
                Delay(200);
          printf("ADC Data:  %lf\n",val);
             iteration++;
     }
}


void init() {
     GPIO_InitTypeDef  GPIO_InitStructure;


     // ---------- SysTick timer -------- //
     if (SysTick_Config(SystemCoreClock / 1000)) {
          // Capture error
          while (1){};
     }


     // ---------- GPIO -------- //
     // GPIOD Periph clock enable
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);


     // Configure PD12, PD13, PD14 and PD15 in output pushpull mode
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
     GPIO_Init(GPIOD, &GPIO_InitStructure);


     // ------------- USB -------------- //
     USBD_Init(&USB_OTG_dev,
                 USB_OTG_FS_CORE_ID,
                 &USR_desc,
                 &USBD_CDC_cb,
                 &USR_cb);
}


/*
 * Called from systick handler
 */
void timing_handler() {
     if (time_var1) {
          time_var1--;
     }


     time_var2++;
}


/*
 * Delay a number of systick cycles (1ms)
 */
void Delay(volatile uint32_t nCount) {
     time_var1 = nCount;
     while(time_var1){};
}


//..................................main.c with FreeRTOS.................................................


#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>


#include "stm32f4xx_conf.h"
#include "stm32f4xx.h"
#include "main.h"




#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"




#include "usbd_cdc_core.h"
#include "usbd_usr.h"
#include "usbd_desc.h"
#include "usbd_cdc_vcp.h"


// Private variables
volatile uint32_t time_var1, time_var2;
__ALIGN_BEGIN USB_OTG_CORE_HANDLE  USB_OTG_dev __ALIGN_END;




uint32_t count = 0;




// Private function prototypes
void Delay(volatile uint32_t nCount);
void init();
void calculation_test();




/*Task to control LED on GPIO 13*/
static void TaskA(void *pvParameters)
{
    int A=0;


    pvParameters = pvParameters;


    //Continous toggeling 
    for(;;) 
    {
      A++;
      //Set LED
      GPIO_SetBits(GPIOD, GPIO_Pin_13);
      //Delay 500 ms
      Delay(200);
      //Clear LED
      GPIO_ResetBits(GPIOD, GPIO_Pin_13);
      //Delay 500 ms
      Delay(200);
    }
}




int main(void) {
     init();
         
 
     /*
      * Disable STDOUT buffering. Otherwise nothing will be printed
      * before a newline character or when the buffer is flushed.
      */


        setbuf(stdout, NULL);                   
        calculation_test();
         
                 GPIO_SetBits(GPIOD, GPIO_Pin_12);
          Delay(500);
          GPIO_ResetBits(GPIOD, GPIO_Pin_12);
          Delay(500);
     xTaskCreate(TaskA, (signed char*)"TaskA", 128, NULL, tskIDLE_PRIORITY+1, NULL);
      


        /*! Task scheduling started */     
       


     //for(;;) {
          
//     }
 vTaskStartScheduler();
     //return 0;
}


void calculation_test() {
     float a = 1.001;
     int iteration = 0;


     //for(;;) {
          GPIO_SetBits(GPIOD, GPIO_Pin_12);
          Delay(500);
          GPIO_ResetBits(GPIOD, GPIO_Pin_12);
          Delay(500);


          time_var2 = 0;
     //     for (int i = 0;i < 1000000;i++) {
                for (int i = 0;i < 10;i++) {
               a += 0.01 * sqrtf(a);
          }
              
             
                 a += 0.01 * sqrtf(a); 
          printf("Time:      %lu ms\n\r", time_var2);
          printf("Iteration: %i\n\r", iteration);
          printf("Value:     %.5f\n\n\r", a);
             
          iteration++;
     //}
}


void init() {
     GPIO_InitTypeDef  GPIO_InitStructure;


     // ---------- SysTick timer -------- //
     if (SysTick_Config(SystemCoreClock / 1000)) {
          // Capture error
          while (1){};
     }


     // ---------- GPIO -------- //
     // GPIOD Periph clock enable
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);


     // Configure PD12, PD13, PD14 and PD15 in output pushpull mode
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
     GPIO_Init(GPIOD, &GPIO_InitStructure);


     // ------------- USB -------------- //
     USBD_Init(&USB_OTG_dev,
                 USB_OTG_FS_CORE_ID,
                 &USR_desc,
                 &USBD_CDC_cb,
                 &USR_cb);
}










/*
 * Called from systick handler
 */
void timing_handler() {
     if (time_var1) {
          time_var1--;
     }


     time_var2++;
}






/*
 * Delay a number of systick cycles (1ms)
 */
void Delay(volatile uint32_t nCount) {
     time_var1 = nCount;
     while(time_var1){};
}




Outcomes