async_test for test

Dependencies:   mbed-src

This is about asynchronous uart output.

When I need to debug my software in a ISR or a task(thread), I worried that printf() might cause some abnormal result.

So, I made my uart output function like printf().

Initialize async_print

title

void timerfunction_async_print()
{
    char c=0;

    while ( buffer_async_print.available() ) {
        buffer_async_print.dequeue(&c);
        pc.putc(c);
    }
    timer_async_print.attach(&timerfunction_async_print, 0.1);
}

void async_print(const char *format, ...)
{
    va_list args;
    
    va_start(args, format);
    
    vsnprintf(debug_line, sizeof(debug_line), format, args);
    int length = strlen(debug_line);
    
    for (int i=0; i<length; i++)
        buffer_async_print.queue(debug_line[i]);
    
    va_end(args);
}


int main()
{
    .....................................................

    // Initialize AsyncPrint
    timer_async_print.attach(&timerfunction_async_print, 0.1);
    p_async_print = &async_print;
    
    .....................................................
}

Implementation async_print

void (*p_async_print)(const char *format, ...);

void UART0_Handler()
{
    p_async_print("IRQ : UART_IT_FLAG_RXI \r\n");
    uart_irq(UART_0, 0);
}

Committer:
SteveKim
Date:
Mon Jul 13 04:38:28 2015 +0000
Revision:
0:51977f75213d
async_print for test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SteveKim 0:51977f75213d 1 /* Copyright (C) 2012 mbed.org, MIT License
SteveKim 0:51977f75213d 2 *
SteveKim 0:51977f75213d 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
SteveKim 0:51977f75213d 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
SteveKim 0:51977f75213d 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
SteveKim 0:51977f75213d 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
SteveKim 0:51977f75213d 7 * furnished to do so, subject to the following conditions:
SteveKim 0:51977f75213d 8 *
SteveKim 0:51977f75213d 9 * The above copyright notice and this permission notice shall be included in all copies or
SteveKim 0:51977f75213d 10 * substantial portions of the Software.
SteveKim 0:51977f75213d 11 *
SteveKim 0:51977f75213d 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
SteveKim 0:51977f75213d 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
SteveKim 0:51977f75213d 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
SteveKim 0:51977f75213d 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
SteveKim 0:51977f75213d 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SteveKim 0:51977f75213d 17 */
SteveKim 0:51977f75213d 18
SteveKim 0:51977f75213d 19 #ifndef CIRCBUFFER_H_
SteveKim 0:51977f75213d 20 #define CIRCBUFFER_H_
SteveKim 0:51977f75213d 21
SteveKim 0:51977f75213d 22 template <class T>
SteveKim 0:51977f75213d 23 class CircBuffer {
SteveKim 0:51977f75213d 24 public:
SteveKim 0:51977f75213d 25 CircBuffer(int length) {
SteveKim 0:51977f75213d 26 write = 0;
SteveKim 0:51977f75213d 27 read = 0;
SteveKim 0:51977f75213d 28 size = length + 1;
SteveKim 0:51977f75213d 29 buf = (T *)malloc(size * sizeof(T));
SteveKim 0:51977f75213d 30 };
SteveKim 0:51977f75213d 31
SteveKim 0:51977f75213d 32 bool isFull() {
SteveKim 0:51977f75213d 33 return (((write + 1) % size) == read);
SteveKim 0:51977f75213d 34 };
SteveKim 0:51977f75213d 35
SteveKim 0:51977f75213d 36 bool isEmpty() {
SteveKim 0:51977f75213d 37 return (read == write);
SteveKim 0:51977f75213d 38 };
SteveKim 0:51977f75213d 39
SteveKim 0:51977f75213d 40 void queue(T k) {
SteveKim 0:51977f75213d 41 if (isFull()) {
SteveKim 0:51977f75213d 42 read++;
SteveKim 0:51977f75213d 43 read %= size;
SteveKim 0:51977f75213d 44 }
SteveKim 0:51977f75213d 45 buf[write++] = k;
SteveKim 0:51977f75213d 46 write %= size;
SteveKim 0:51977f75213d 47 }
SteveKim 0:51977f75213d 48
SteveKim 0:51977f75213d 49 void flush() {
SteveKim 0:51977f75213d 50 read = 0;
SteveKim 0:51977f75213d 51 write = 0;
SteveKim 0:51977f75213d 52 }
SteveKim 0:51977f75213d 53
SteveKim 0:51977f75213d 54
SteveKim 0:51977f75213d 55 uint32_t available() {
SteveKim 0:51977f75213d 56 return (write >= read) ? write - read : size - read + write;
SteveKim 0:51977f75213d 57 };
SteveKim 0:51977f75213d 58
SteveKim 0:51977f75213d 59 bool dequeue(T * c) {
SteveKim 0:51977f75213d 60 bool empty = isEmpty();
SteveKim 0:51977f75213d 61 if (!empty) {
SteveKim 0:51977f75213d 62 *c = buf[read++];
SteveKim 0:51977f75213d 63 read %= size;
SteveKim 0:51977f75213d 64 }
SteveKim 0:51977f75213d 65 return(!empty);
SteveKim 0:51977f75213d 66 };
SteveKim 0:51977f75213d 67
SteveKim 0:51977f75213d 68 private:
SteveKim 0:51977f75213d 69 volatile uint32_t write;
SteveKim 0:51977f75213d 70 volatile uint32_t read;
SteveKim 0:51977f75213d 71 uint32_t size;
SteveKim 0:51977f75213d 72 T * buf;
SteveKim 0:51977f75213d 73 };
SteveKim 0:51977f75213d 74
SteveKim 0:51977f75213d 75 #endif