{"componentChunkName":"component---src-templates-blog-post-js","path":"/posts/stm32discovery_2/","result":{"data":{"markdownRemark":{"html":"<p>Since I'm starting off right now coding on bare metal (I'm not running an RTOS), I've got only the main() thread running and no other task manager to speak of. This means I have limited options when addressing the flow of tasks in my code. For example, what I've observed is that if I make two calls to <code>CDC_Transmit_FS</code> consecutively, the second one doesn't run:</p>\n<pre><code class=\"language-cpp\">void ExampleClass::printChar()\n{\n    int len = strlen(this->charValue);          // charValue = \"charstr\"\n    uint8_t charout[] = \"printChar Output: \";\n    CDC_Transmit_FS(charout, 18);\n    CDC_Transmit_FS((uint8_t*)this->charValue,len);        \n}\n</code></pre>\n<p>Results with a printout of the following</p>\n<pre><code>--- Miniterm on /dev/cu.usbmodem205A319838471  9600,8,N,1 ---\n--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---\nHello World!\nprintChar Output: Hello World!\nprintChar Output: Hello World!\nprintChar Output: Hello World!\nprintChar Output: Hello World!\n</code></pre>\n<p>I expected to see \"charstr\\n\" after printChar output (so charstr with a carriage return). Why does this happen? Because the call to <code>CDC_Transmit_FS</code> gets called while the USB peripheral is still busy. Therefore, the way to fix this is to include CDC<em>Transmit</em>FS with another function that will do a busy wait.</p>\n<pre><code class=\"language-cpp\">void usb_print(uint8_t* Buf, uint16_t Len){\n    while (CDC_Transmit_FS(Buf,Len)) {}     // Busy wait to ensure that the transmit completes\n}\n\nvoid ExampleClass::printChar()\n{\n    int len = strlen(this->charValue);\n    uint8_t charout[] = \"printChar Output: \";     \n\n    usb_print(charout,18);\n    usb_print((uint8_t*)this->charValue,len);\n}\n</code></pre>\n<p>And here is the result!</p>\n<pre><code>--- Miniterm on /dev/cu.usbmodem205A319838471  9600,8,N,1 ---\n--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---\nHello World!\nprintChar Output: charstr\nHello World!\nprintChar Output: charstr\nHello World!\nprintChar Output: charstr\n</code></pre>","frontmatter":{"title":"B-L475E-IOT01A - Including Busy Wait on USB transmit"}}},"pageContext":{"slug":"/posts/stm32discovery_2/"}},"staticQueryHashes":["3649515864"]}