Buffered Serial Port Driver for RTOS tested with a K64F
Dependents: JRO_CR2 frdm_test JRO_DDSv2 JRO_DDSv2_rev2019
Fork of SerialDriver by
Examples/Example_Blocking.cpp@0:cd0d79be0c1a, 2015-01-14 (annotated)
- Committer:
- BlazeX
- Date:
- Wed Jan 14 16:30:14 2015 +0000
- Revision:
- 0:cd0d79be0c1a
- Child:
- 1:1464146bd7fb
Release :)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BlazeX | 0:cd0d79be0c1a | 1 | /// @file Example_Blocking.cpp |
BlazeX | 0:cd0d79be0c1a | 2 | /// @brief Test blocking write / read |
BlazeX | 0:cd0d79be0c1a | 3 | /// |
BlazeX | 0:cd0d79be0c1a | 4 | /// - Uses SerialDriver with USBTX and USBRX. |
BlazeX | 0:cd0d79be0c1a | 5 | /// - Has much too small buffers, to how far can it get? |
BlazeX | 0:cd0d79be0c1a | 6 | /// |
BlazeX | 0:cd0d79be0c1a | 7 | /// - Testing how much bytes were transmitted, with too small TX buffer and forced non blocking |
BlazeX | 0:cd0d79be0c1a | 8 | /// - Waiting till 10 bytes are received |
BlazeX | 0:cd0d79be0c1a | 9 | /// - LED4 indicates parallel thread working |
BlazeX | 0:cd0d79be0c1a | 10 | /// |
BlazeX | 0:cd0d79be0c1a | 11 | |
BlazeX | 0:cd0d79be0c1a | 12 | #if 0 |
BlazeX | 0:cd0d79be0c1a | 13 | |
BlazeX | 0:cd0d79be0c1a | 14 | #include "mbed.h" |
BlazeX | 0:cd0d79be0c1a | 15 | #include "SerialDriver.h" |
BlazeX | 0:cd0d79be0c1a | 16 | |
BlazeX | 0:cd0d79be0c1a | 17 | SerialDriver pc(USBTX, USBRX, 4, 32); |
BlazeX | 0:cd0d79be0c1a | 18 | |
BlazeX | 0:cd0d79be0c1a | 19 | // This thread is running in parallel |
BlazeX | 0:cd0d79be0c1a | 20 | DigitalOut led4(LED4); |
BlazeX | 0:cd0d79be0c1a | 21 | void parallel(void const * argument) |
BlazeX | 0:cd0d79be0c1a | 22 | { |
BlazeX | 0:cd0d79be0c1a | 23 | while(1) |
BlazeX | 0:cd0d79be0c1a | 24 | { |
BlazeX | 0:cd0d79be0c1a | 25 | Thread::wait(20); |
BlazeX | 0:cd0d79be0c1a | 26 | led4= !led4; |
BlazeX | 0:cd0d79be0c1a | 27 | } |
BlazeX | 0:cd0d79be0c1a | 28 | } |
BlazeX | 0:cd0d79be0c1a | 29 | |
BlazeX | 0:cd0d79be0c1a | 30 | int main() |
BlazeX | 0:cd0d79be0c1a | 31 | { |
BlazeX | 0:cd0d79be0c1a | 32 | // Start the other thread |
BlazeX | 0:cd0d79be0c1a | 33 | Thread parallelTask(¶llel); |
BlazeX | 0:cd0d79be0c1a | 34 | |
BlazeX | 0:cd0d79be0c1a | 35 | |
BlazeX | 0:cd0d79be0c1a | 36 | const char * completeText= "This is a complete text. How much will you receive?"; |
BlazeX | 0:cd0d79be0c1a | 37 | const int completeTextLength= strlen(completeText); |
BlazeX | 0:cd0d79be0c1a | 38 | int writtenBytes; |
BlazeX | 0:cd0d79be0c1a | 39 | |
BlazeX | 0:cd0d79be0c1a | 40 | |
BlazeX | 0:cd0d79be0c1a | 41 | const int readBufferLength= 10; |
BlazeX | 0:cd0d79be0c1a | 42 | unsigned char readBuffer[readBufferLength]; |
BlazeX | 0:cd0d79be0c1a | 43 | int receivedBytes= 0; |
BlazeX | 0:cd0d79be0c1a | 44 | |
BlazeX | 0:cd0d79be0c1a | 45 | while(1) |
BlazeX | 0:cd0d79be0c1a | 46 | { |
BlazeX | 0:cd0d79be0c1a | 47 | // write non blocking, how much get transmitted? |
BlazeX | 0:cd0d79be0c1a | 48 | writtenBytes= pc.write((const unsigned char*)completeText, completeTextLength, false); |
BlazeX | 0:cd0d79be0c1a | 49 | |
BlazeX | 0:cd0d79be0c1a | 50 | // now print the result |
BlazeX | 0:cd0d79be0c1a | 51 | pc.printf("\r\nOnly %i of %i bytes were transmitted using non blocking write.\r\n", writtenBytes, completeTextLength); |
BlazeX | 0:cd0d79be0c1a | 52 | |
BlazeX | 0:cd0d79be0c1a | 53 | |
BlazeX | 0:cd0d79be0c1a | 54 | // wait for 10 bytes |
BlazeX | 0:cd0d79be0c1a | 55 | pc.printf("I wait for my 10 bytes. Send them!\r\n", writtenBytes, completeTextLength); |
BlazeX | 0:cd0d79be0c1a | 56 | receivedBytes+= pc.read(readBuffer, readBufferLength); |
BlazeX | 0:cd0d79be0c1a | 57 | |
BlazeX | 0:cd0d79be0c1a | 58 | // now print result |
BlazeX | 0:cd0d79be0c1a | 59 | pc.printf("Received %i bytes since start.\r\n\r\n", receivedBytes); |
BlazeX | 0:cd0d79be0c1a | 60 | |
BlazeX | 0:cd0d79be0c1a | 61 | |
BlazeX | 0:cd0d79be0c1a | 62 | // wait a bit |
BlazeX | 0:cd0d79be0c1a | 63 | Thread::wait(1000); |
BlazeX | 0:cd0d79be0c1a | 64 | } |
BlazeX | 0:cd0d79be0c1a | 65 | } |
BlazeX | 0:cd0d79be0c1a | 66 | |
BlazeX | 0:cd0d79be0c1a | 67 | #endif |