eprintf mit irq

Committer:
est2fe
Date:
Thu Jul 21 07:38:51 2011 +0000
Revision:
4:5138a20c4989
Parent:
3:de07cab9fc01
Child:
5:8d716aa183dc
V03.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
est2fe 3:de07cab9fc01 1 #ifndef __EPRINTF_C__
est2fe 3:de07cab9fc01 2 #define __EPRINTF_C__
est2fe 2:f63032cc0c0a 3
est2fe 0:4843fcdba3cc 4 #include "eprintf.h"
est2fe 4:5138a20c4989 5 #include "mbos.h"
est2fe 0:4843fcdba3cc 6
est2fe 4:5138a20c4989 7 extern mbos os;
est2fe 4:5138a20c4989 8 extern Serial pc;
est2fe 4:5138a20c4989 9 // extern int eprintf_fehlerzaehler = 0;
est2fe 4:5138a20c4989 10
est2fe 4:5138a20c4989 11 extern rbuf_t *printbuf;
est2fe 4:5138a20c4989 12
est2fe 4:5138a20c4989 13 extern int letzter_Fehler;
est2fe 4:5138a20c4989 14
est2fe 4:5138a20c4989 15 char cb [SB_GROESSE]; // Der Spoolbuffer zum PC
est2fe 4:5138a20c4989 16 static int eprintf_fehlerzaehler = 0;
est2fe 2:f63032cc0c0a 17
est2fe 2:f63032cc0c0a 18 void TX_irq_eprintf (void) {
est2fe 2:f63032cc0c0a 19 // Wird bei Tx-Empty oder explizit aufgerufen
est2fe 2:f63032cc0c0a 20 os.SetEvent (EV_EPRINTF, TASK_EPRINTF_ID);
est2fe 2:f63032cc0c0a 21 return;
est2fe 0:4843fcdba3cc 22 }
est2fe 1:50fdadc001ef 23
est2fe 2:f63032cc0c0a 24 int eprintf (char *format_str, ...) {
est2fe 2:f63032cc0c0a 25 // Soll in den Printbuffer drucken, aber nur, wenn er nicht voll ist.
est2fe 2:f63032cc0c0a 26 // Returnwert:
est2fe 2:f63032cc0c0a 27 // 0 = ok
est2fe 2:f63032cc0c0a 28 // !0 = Fehler -> siehe defines
est2fe 2:f63032cc0c0a 29 int zeit;
est2fe 2:f63032cc0c0a 30 int letzter_Fehler = 0;
est2fe 2:f63032cc0c0a 31 Timer tout;
est2fe 2:f63032cc0c0a 32 tout.reset();
est2fe 2:f63032cc0c0a 33 tout.start();
est2fe 2:f63032cc0c0a 34 while (os.TestResource (RESOURCE_EPRINTF))
est2fe 2:f63032cc0c0a 35 {
est2fe 2:f63032cc0c0a 36 // warten bis Resource (eprintf selber) wieder frei ist.
est2fe 2:f63032cc0c0a 37 zeit = tout.read_us();
est2fe 2:f63032cc0c0a 38 if (zeit > TO_LockResource)
est2fe 2:f63032cc0c0a 39 {
est2fe 2:f63032cc0c0a 40 // Timeout
est2fe 2:f63032cc0c0a 41 letzter_Fehler = FEHLER_EPRINTF_TEST_RESOURCE;
est2fe 2:f63032cc0c0a 42 //eprintf ("\n\rletzer_Fehler = %d, in Zeile %d", letzter_Fehler, __LINE__);
est2fe 2:f63032cc0c0a 43 goto eprintf_end_fehler;
est2fe 2:f63032cc0c0a 44 }
est2fe 2:f63032cc0c0a 45 else
est2fe 2:f63032cc0c0a 46 {
est2fe 2:f63032cc0c0a 47 // warten
est2fe 2:f63032cc0c0a 48 }
est2fe 2:f63032cc0c0a 49 }
est2fe 2:f63032cc0c0a 50 tout.reset ();
est2fe 2:f63032cc0c0a 51 tout.start ();
est2fe 2:f63032cc0c0a 52 while (!os.LockResource (RESOURCE_EPRINTF))
est2fe 2:f63032cc0c0a 53 {
est2fe 2:f63032cc0c0a 54 // warten bis Resource gelockt ist
est2fe 2:f63032cc0c0a 55 zeit = tout.read_us ();
est2fe 2:f63032cc0c0a 56 if (zeit > TO_LockResource)
est2fe 2:f63032cc0c0a 57 {
est2fe 2:f63032cc0c0a 58 // Timeout
est2fe 2:f63032cc0c0a 59 letzter_Fehler = FEHLER_EPRINTF_LOCK_RESOURCE;
est2fe 2:f63032cc0c0a 60 //eprintf ("\n\rletzer_Fehler = %d, in Zeile %d", letzter_Fehler, __LINE__);
est2fe 2:f63032cc0c0a 61 goto eprintf_end_fehler;
est2fe 2:f63032cc0c0a 62 }
est2fe 2:f63032cc0c0a 63 else
est2fe 2:f63032cc0c0a 64 {
est2fe 2:f63032cc0c0a 65 // warten
est2fe 2:f63032cc0c0a 66 }
est2fe 2:f63032cc0c0a 67 }
est2fe 2:f63032cc0c0a 68 if ((printbuf->flags & FAST_VOLL))
est2fe 2:f63032cc0c0a 69 {
est2fe 2:f63032cc0c0a 70 // Wenn der Stecker raus ist, laeuft der Buffer voll -> nichts mehr drucken!
est2fe 2:f63032cc0c0a 71 letzter_Fehler = FEHLER_EPRINTF_FAST_VOLL;
est2fe 2:f63032cc0c0a 72 goto eprintf_end_fehler;
est2fe 2:f63032cc0c0a 73 }
est2fe 2:f63032cc0c0a 74 else
est2fe 2:f63032cc0c0a 75 {
est2fe 2:f63032cc0c0a 76 int slen; // Stringlaenge
est2fe 2:f63032cc0c0a 77 va_list args;
est2fe 2:f63032cc0c0a 78 va_start (args, format_str); // Zeiger auf die "..."
est2fe 2:f63032cc0c0a 79 vsnprintf (cb, (SB_GROESSE - 1), format_str, args);
est2fe 2:f63032cc0c0a 80 va_end (args);
est2fe 2:f63032cc0c0a 81 // Jetzt den Inhalt vom Spoolbuffer (cb) noch in den printbuffer packen
est2fe 2:f63032cc0c0a 82 slen = strlen (cb);
est2fe 2:f63032cc0c0a 83 if (slen > (printbuf->buflen - printbuf->bufcnt - 2))
est2fe 2:f63032cc0c0a 84 {
est2fe 4:5138a20c4989 85 // wenn es nicht reinpasst, gleich mit Fehler zurück
est2fe 2:f63032cc0c0a 86 letzter_Fehler = FEHLER_EPRINTF_STRING_ZU_GROSS;
est2fe 2:f63032cc0c0a 87 goto eprintf_end_fehler;
est2fe 2:f63032cc0c0a 88 }
est2fe 2:f63032cc0c0a 89 else
est2fe 2:f63032cc0c0a 90 {
est2fe 2:f63032cc0c0a 91 // es passt rein, also jedes Zeichen von cb in den Printbuffer
est2fe 2:f63032cc0c0a 92 int i; void *fehler;
est2fe 2:f63032cc0c0a 93 for (i = 0; i < slen; i++)
est2fe 2:f63032cc0c0a 94 {
est2fe 2:f63032cc0c0a 95 // rbuf_put (kbdbuf, &c)
est2fe 2:f63032cc0c0a 96 fehler = rbuf_put (printbuf, &cb[i]);
est2fe 2:f63032cc0c0a 97 if (!fehler)
est2fe 2:f63032cc0c0a 98 { // Fehler aufgetreten, warum auch immer!
est2fe 2:f63032cc0c0a 99 // Sollte eigentlich nie vorkommen!
est2fe 2:f63032cc0c0a 100 letzter_Fehler = FEHLER_EPRINTF_RBUF_PUT;
est2fe 2:f63032cc0c0a 101 goto eprintf_end_fehler;
est2fe 2:f63032cc0c0a 102 }
est2fe 2:f63032cc0c0a 103 }
est2fe 2:f63032cc0c0a 104 // Fertig! Nun noch melden, dass neue druckbare Zeichen
est2fe 2:f63032cc0c0a 105 // da sind und per Interrupt ausgegeben werden koennen.
est2fe 2:f63032cc0c0a 106 os.SetEvent (EV_EPRINTF, TASK_EPRINTF_ID);
est2fe 2:f63032cc0c0a 107 }
est2fe 2:f63032cc0c0a 108 }
est2fe 2:f63032cc0c0a 109 os.FreeResource (RESOURCE_EPRINTF);
est2fe 4:5138a20c4989 110 TX_irq_eprintf (); // Und das erste Zeichen, falls möglich, gleich ausgeben
est2fe 2:f63032cc0c0a 111 return 0; // 0 = kein Fehler
est2fe 1:50fdadc001ef 112
est2fe 2:f63032cc0c0a 113 eprintf_end_fehler:
est2fe 2:f63032cc0c0a 114 eprintf_fehlerzaehler++;
est2fe 2:f63032cc0c0a 115 os.FreeResource (RESOURCE_EPRINTF);
est2fe 4:5138a20c4989 116 return letzter_Fehler; // Mit Fehler zurück
est2fe 2:f63032cc0c0a 117 }
est2fe 2:f63032cc0c0a 118 void task_eprintf_verarbeitung (void) {
est2fe 2:f63032cc0c0a 119 // Grundsaetzlich gilt: Immer nur per os warten, wenn es nichts zu tun gibt!
est2fe 2:f63032cc0c0a 120 while (1)
est2fe 0:4843fcdba3cc 121 {
est2fe 2:f63032cc0c0a 122 eprintf_wait_event: // Bruch der Schleife while (printbuf->bufcnt > 0)
est2fe 2:f63032cc0c0a 123 os.WaitEvent (EV_EPRINTF);
est2fe 2:f63032cc0c0a 124 while ((printbuf->bufcnt > 0) && (pc.writeable ()))
est2fe 2:f63032cc0c0a 125 {
est2fe 2:f63032cc0c0a 126 // So lange was ausgeben bis nicht mehr moeglich
est2fe 2:f63032cc0c0a 127 int c;
est2fe 2:f63032cc0c0a 128 void *z;
est2fe 2:f63032cc0c0a 129 z = rbuf_get (printbuf, &c);
est2fe 2:f63032cc0c0a 130 if (z == 0)
est2fe 2:f63032cc0c0a 131 {
est2fe 2:f63032cc0c0a 132 // wenn Buffer leer, per os warten
est2fe 2:f63032cc0c0a 133 goto eprintf_wait_event;
est2fe 2:f63032cc0c0a 134 }
est2fe 2:f63032cc0c0a 135 else
est2fe 2:f63032cc0c0a 136 {
est2fe 2:f63032cc0c0a 137 pc.putc (c); // Das sollte die einzige Stelle im Programm sein, wo was auf den pc ausgegeben wird!
est2fe 2:f63032cc0c0a 138 }
est2fe 2:f63032cc0c0a 139 } // von while ((printbuf->bufcnt > 0) && (pc.writeable ()))
est2fe 2:f63032cc0c0a 140 } // von while (1)
est2fe 2:f63032cc0c0a 141 } // void task_eprintf_verarbeitung (void)
est2fe 0:4843fcdba3cc 142
est2fe 2:f63032cc0c0a 143 #endif // von #ifdef __EPRINTF_C__