Author Topic: STM32F103 USB CDC Tx Error  (Read 3281 times)

0 Members and 1 Guest are viewing this topic.

Offline noname4meTopic starter

  • Regular Contributor
  • *
  • Posts: 93
STM32F103 USB CDC Tx Error
« on: July 14, 2016, 09:16:57 pm »
Hi,

I am having some issues with trying to send information from an STM32F103C8T6 minimal development board using the USB in CDC mode.

I have used STMCubeMX to generate the startup code, and I am able to get the ADC working, and the USB working to a degree.  I am able to send hard coded "test" out of the board to a PC terminal.

I am trying to read the ADC value, and then transfer this to the PC terminal but I am getting only rubbish ascii characters.

I consider myself a beginner in terms of microcontrollers and C programming.

The code I am using is below. 

I am trying to read the ADC, turn this into a char * (I think) which can be transmitted to the PC using the CDC_Transmit_FS() function (which is a standard library function).

It complains that I can't return the array from the conversion function at the bottom (I thought that the problem with the program was that the USB transmission was being sent in the wrong format, and I'm trying to convert it to something more readable).

Any help please, or something that I can read?

Thanks

Code: [Select]
  //unsigned char chunk []= {0x46,0x6F,0x6F,0x64,0x20,0x0D,0x0A};
 
  unsigned char chunk [0xFF];
 
  uint8_t sender[sizeof(chunk)];
 
  HAL_Delay(1000);
  uint16_t tres;
  while (1)
 
 
 
  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

    tres = Getval();
   
    chunk[0] = tres;
    for (int i=0; i < sizeof(chunk); i++){
   
    sender[i] = chunk[i];
   
    uint16_t p;
   
    p = sizeof(sender[i]);
   
    CDC_Transmit_FS(&sender[i],4);
   
    HAL_Delay(100);
   
   
    }
   
    HAL_Delay(1000);
   




Code: [Select]
int  bin2char(int binter){
  int charback[4]; 
   
  //int dib = binter/4;
 
  int i = 4;
   
   
   for (int j = 0; j == i; j++){
   
     charback[j] = binter & 0x0F;
       
     binter = binter >> 0x0F;
   
   }
   
   
   
   return charback;
 }
 

Offline MosherIV

  • Super Contributor
  • ***
  • Posts: 1530
  • Country: gb
Re: STM32F103 USB CDC Tx Error
« Reply #1 on: July 15, 2016, 12:01:54 am »
Hi

Firstly, the St USB CDC driver code is flawed when you send lots of characters. From the sound if it that is not your problem but be aware of it.

You need to create an array of chars as a variable
Char foo[15]

Then to turn the ADC readings into a string representation, use sprintf
Sprintf( &foo[0], "%i", adc_value );

Then send the char variable to the usb transmit function.
 

Offline noname4meTopic starter

  • Regular Contributor
  • *
  • Posts: 93
Re: STM32F103 USB CDC Tx Error
« Reply #2 on: July 15, 2016, 07:25:35 pm »
Hi MosherIV,

Thanks for your reply.

I tried using the Sprintf function but the compiler complains that the function is being declared implicitly.

I take this to mean that there is no code match for the function Sprintf().

The include files that I am using are:

Code: [Select]
/* Includes ------------------------------------------------------------------*/
#include "stm32f1xx_hal.h"
#include "usb_device.h"

/* USER CODE BEGIN Includes */
#include "usbd_cdc_if.h"
#include "stdio.h"
#include "stdlib.h"
#include "stdint.h"
#include "string.h"
#include "usbd_cdc.h"

Is there something that I'm missing here?

Thanks
 

Offline Signal32

  • Frequent Contributor
  • **
  • Posts: 251
  • Country: us
Re: STM32F103 USB CDC Tx Error
« Reply #3 on: July 15, 2016, 07:28:01 pm »
I tried using the Sprintf function but the compiler complains that the function is being declared implicitly.
Are you calling "Sprintf" or "sprintf", notice that it has to be all lower case.
 

Offline noname4meTopic starter

  • Regular Contributor
  • *
  • Posts: 93
Re: STM32F103 USB CDC Tx Error
« Reply #4 on: July 15, 2016, 07:35:56 pm »
I was using the capitalised one - smaller case seems not to complain.

Apologies - my bad...
 

Offline noname4meTopic starter

  • Regular Contributor
  • *
  • Posts: 93
Re: STM32F103 USB CDC Tx Error
« Reply #5 on: July 15, 2016, 07:51:12 pm »
Hi,

Thanks all, it's working now.

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf