cancel
Showing results for 
Search instead for 
Did you mean: 

Why doesn't my PC recognize STM32 as USB Device when sending data to it through USB?

HKakk.1
Associate

I want to send Data through USB from STM32. I've written the following bare-metal code to do it. But my PC doesn't recognize USB device when plugged in.

#include "stm32f10x.h"
 
void USB_Endpoint_Configure(uint8_t endpoint, uint8_t type, uint8_t dir) {
 
  // Check the parameters.
  if (endpoint >= 16) {
    return;
  }
 
  // Set the endpoint type.
  *((volatile unsigned int *)0x40005C00 + endpoint * 4 + 0x00) = type;
 
  // Set the endpoint direction.
  *((volatile unsigned int *)0x40005C00 + endpoint * 4 + 0x01) = dir;
 
  // Set the endpoint max packet size.
  *((volatile unsigned int *)0x40005C00 + endpoint * 4 + 0x02) = 64;
 
  // Enable the endpoint.
  *((volatile unsigned int *)0x40005C00 + endpoint * 4 + 0x04) |= 0x00000001;
}
 
int USB_Endpoint_Write(uint8_t endpoint, const void *data, int size) {
  // Check if the endpoint is valid.
  if (endpoint >= 0x80 || endpoint >= 0xE0) {
    return -1;
  }
 
  // Check if the data is large enough to fill the endpoint buffer.
  if (size > 64) {
    return -1;
  }
 
  // Write the data to the endpoint buffer.
  for (int i = 0; i < size; i++) {
    *((uint8_t *)(0x40000000 + (endpoint << 7) + i)) = *((const uint8_t *)data + i);
  }
 
  // Send the data to the device.
  return 0;
}
 
 
int main(void){
	//USB_Init()
	// Reset the USB controller.
  RCC->APB1ENR |= RCC_APB1ENR_USBEN;
  *((volatile unsigned int *)0x40005C00) |= 0x00000001;
  while (*((volatile unsigned int *)0x40005C00) & 0x00000001);
	
	// Initialize the USB clock.
  RCC->APB1ENR |= 0x00000001;
 
  // Initialize the USB interrupts.
  NVIC_EnableIRQ(5);
 
	USB_Endpoint_Configure(0x00, 0x00, 0x00);
  USB_Endpoint_Configure(0x80, 0x00, 0x40);
	
	
	
	
	USB_Endpoint_Configure(0x00, 0x00, 0x00);
	
	
	char data[]="Hello WOrld!!!";
	
	 USB_Endpoint_Write(0x00, data, sizeof(data));
	 
	 //Enable endpoint EP0
	 *((uint8_t *)(0x40000000 + (0x00 << 7) + 0)) |= 1;
	 
		// Wait for the data to be transferred.
		while (!*((uint8_t *)(0x40000000 + (0x00 << 7) + 1)) & 0x80) {
    // Do nothing.
		}
		
		//Disable endpoint EP0
	 *((uint8_t *)(0x40000000 + (0x00 << 7) + 0)) |= 0;
 
}
 

I could do the same thing while coding through HAL libraries and it worked successfully. Hence, there are no physical issues with USB cable or device. So can anyone help to do it in Bare-Metal??

2 REPLIES 2
AScha.3
Chief II

so...just why you dont use HAL lib and ...its fine ? dont like it ? or just experimental ?

If you feel a post has answered your question, please click "Accept as Solution".
HKakk.1
Associate

Actually, this code is part of a bigger project. And, that whole project is done in Bare-metal. So, I need to do this in bare-metal only!!!