-1

So I have a projects implemented on MCU in 2 steps/projects:

  1. Implementation of a bootloader using the mbed RTOS and USBDevice Library: USBHID, USBSerial...
  2. The application layer is implemented in a separate project where it get compiled and the binary and flash it using bootloader.

The question is what is the best approach to set up serial communication over UART to USB from application layer to my laptop for example? Should I be using the RTOS libraries or should the application code have its own USB libraries or even both? If using RTOS libraries how to have them "connected to"application code i.e. how can I call mbed functions in application project that doesn't have mbed?

Thanks

JJ Adams
  • 481
  • 4
  • 17
  • 1
    That's clearly not C code. Get the tags right. And we are not a consulting site. Read [ask]. – too honest for this site Jul 09 '17 at 13:44
  • @Olaf Thank you for your response to someone who is clearly new to the embedded world. – JJ Adams Jul 09 '17 at 13:50
  • You don't need a USB library if you are using mbed RTOS. The mbed RTOS must have API's in it's SDK to setup the UART of the particular microcontroller, please search for "serial" or "serial example" or "uart.c" in the SDK example folder. – Gaurav Pathak Jul 10 '17 at 11:13
  • @GauravPathak they don't seem to be using an actual UART, but rather to be using a USB channel in place of one, probably with a scheme such as CDC/ACM which the PC operating system will likely present as if it were a UART. – Chris Stratton Jul 13 '17 at 03:52
  • Typically one does not try to call functions in one independent MCU project from another, rather each project would bring along everything it needs. If you do want to call across, you'll need some sort of agreed upon interface, like a table of function pointers at a known location, so that one program can call routines in the other which are not actually present at link time, but only at runtime. Generally this is avoided, beyond having something like an entry point. Note MBED implies ARM, and ARM implies a block of vectors - you can read that to invoke your target firmware. – Chris Stratton Jul 13 '17 at 03:55

3 Answers3

1

Windows ports recognize the serial implementation of and UART application, so you have to download the mbed sdk/stack library and in your project workspace, create a make file with the linker path from library, compiler options. Next, in this file you must include the links to all OBJS, all of the sources participating in the build, inputs and outputs from these tool invocations to the build variables. This file will create your hex file for the target, including the serial libraries. After flashing on the target, if you connect through the USB, it must recognize the serial communication.

PS: run the make file with cmd.

KosmaS
  • 39
  • 1
  • This bares a vague resembles to the truth, but does not meet the norms for clarity, accuracy, or addressing the question asked which are expected of an answer here. – Chris Stratton Jul 13 '17 at 03:57
  • I wonder how this answer fits the question description and has been accepted as a valid answer!!! – Gaurav Pathak Jul 13 '17 at 11:08
1

mbed itself is an RTOS and is intended to be used on microcontrollers, Arm Cortex-M architecture. It doesn't run on a host PC running Windows or Linux or in other words x86 architecture. When you write an application with mbed, it would communicate over USB to UART by using a Serial class object or you can simply use printf to see messages on the COM Port that is enumerated on your PC. Hope this makes it clear.

Bilal Qamar
  • 322
  • 1
  • 2
  • 9
0

Arduino and also EspressIF have really good libraries for UART Serial communication using USB. I've written a custom library based on EspressIF hardwareserial.h library, available on GitHub here:

To initialize serial communication it can be done like below:

// ----------------------------------------------------
void mSerial::start(int baud) {
  if (this->DEBUG_EN) {
    if ( this->DEBUG_TO == this->DEBUG_BOTH_USB_UART || this->DEBUG_TO == this->DEBUG_TO_USB ){    
      Serial.begin(115200);
      Serial.println("mSerial started on the USB PORT.");
    }

    if ( this->DEBUG_TO == this->DEBUG_BOTH_USB_UART || this->DEBUG_TO == this->DEBUG_TO_UART ){
      this->UARTserial->begin(baud);
      this->UARTserial->println("mSerial started on the UART PORT.");
    }
  }
}

To receive serial data it can be done like this:

  // -----------------------------------------------------------------
bool mSerial::readSerialData(){
  this->serialDataReceived = "";
  
  if( Serial.available() ){ // if new data is coming from the HW Serial
    while(Serial.available()){
      char inChar = Serial.read();
      this->serialDataReceived += String(inChar);
    }
    this->printStr(">");
    return true;
  }else{
    return false;
  }
}

and to send serial data like this:

          xSemaphoreTake(MemLockSemaphoreSerial, portMAX_DELAY); // enter critical section
            this->UARTserial->print(mem);
            this->UARTserial->print(str);
            this->UARTserial->flush();
          xSemaphoreGive(MemLockSemaphoreSerial); // exit critical section    

I use a SemaphoreHandle_t so I can better handle the dual-core activity on the 2 cores of the ESP32.

One final note, when changing core frequency, is needed special handling of serial communication. I've written a function for that purpose alone. The full code can be found here. Below is only the function :

// ****************************************************
  bool setMCUclockFrequency(int clockFreq){
    xSemaphoreTake(this->McuFreqSemaphore, portMAX_DELAY);
      this->McuFrequencyBusy = true;
      changeMcuFreq(this-, this->interface->MIN_MCU_FREQUENCY);

        this->UARTserial->flush();
        
        setCpuFrequencyMhz(Freq);
        this->CURRENT_CLOCK_FREQUENCY=Freq;
        if (Freq < 80) {
          this->MCU_FREQUENCY_SERIAL_SPEED = 80 / Freq * this->SERIAL_DEFAULT_SPEED;
        } else {
          this->MCU_FREQUENCY_SERIAL_SPEED = this->SERIAL_DEFAULT_SPEED;
        }
        
        this->UARTserial->end();  
        delay(300);
        this->UARTserial->begin(this->MCU_FREQUENCY_SERIAL_SPEED);
        this->UARTserial->print("The current Serial Baud speed on the UART Port is ");
        this->UARTserial->println(this->MCU_FREQUENCY_SERIAL_SPEED);

      this->mserial->printStrln("Setting to min CPU Freq = " + String(getCpuFrequencyMhz()));
      this->McuFrequencyBusy = false;
    xSemaphoreGive(this->interface->McuFreqSemaphore);
    return true;
  };
Miguel Tomás
  • 1,714
  • 1
  • 13
  • 23