First of all, I am not new to embedded and not new to STM32 programming. But this is my first Chibios project on an STM32.
I have most of the available STM32 dev kits but with Chibios I could only get the STm32F4-DISCOVERY to start a USB-CDC device successfully. This is no problem so far.
The intended project is very simple: I often work with embedded controllers that communicate via UART. But these system do not use standard baud rates or accept bigger baud rate errors than a standard USB dongle accepts. In my current case I have to AVRs talking at 250kBaud and my CP2101 USB UART dongle refuses to run on that speed. But if I go down to 115200Baud the AVRs have -8% error as both are equipped with straight crystals (2MHz / 8Mhz) which again causes only erratic characters.
The code is available here: https://github.com/Astralix/chibios-dualport-monitor
I copied the STM32F4-DISCOVERY-MEMS project as a starting point and threw out the MEMS, but kept the USB-CDC UART implementation. Then I added UART2 and UART3.
Then I do two unblocking ansynchronous reads to the serial port devices and copy the read content to the USB-CDC with a blocking write:
Code: Select all
/* Non blocking read from RX channel 2 */
cbuf2 = sdAsynchronousRead(&SD2, buf2, RX_BUF_SZ);
if (cbuf2) {
palSetPad(PORT_LED_GREEN, PIN_LED_GREEN);
cbuf2 = filter( (char*)tx_buf, (char*)buf2, cbuf2);
sdWrite(&SDU1, tx_buf, cbuf2);
}
After that the same is done for USART3 and the red LED.
This code resides in the main() while(1) loop followed by a chThdSleepMilliseconds(10);
The 10ms delay was chosen based on the following calculation:
250kBaud will result in approx. 25000 chars per second. Scanning the input buffers with 100Hz will give a maximum of 250 characters in the buffer. 250 characters can easily be passed and filtered and then put through the USB interface.
This pretty much works... but then it suddenly stops working. What I can see is, that the LED of one channel is lit but the system itself hangs after that. I have a blinking task running, and this task is hanging too in the most times, sometimes it still is blinking. The USB connection is thrown most times too.
So, I do not have set up much for debugging. I am using arm-none-eabi gcc toolchain 4.8.2 in ubuntu 14.04 with openocd 0.8.0 working fine with latest stlink-v2 on the discovery board.
Before trying to install lots of things, I would like to know if there are obvious pitfalls in my code regarding the way I am using the UARTs and the USB. I am pretty sure I misunderstood something as I am new to Chibios.
I know that the code has some bugs regarding the USB handling as it allows to write data to the USB even it is not connected. But this is not the reason for the crash and I fix it asap.
Thanks in advance for any helping hand
Astralix