Rev Author Line No. Line
4932 kaklik 1 /************************************************************************
2 debug.c
3  
4 WFF USB Generic HID Demonstration 3
5 usbGenericHidCommunication reference firmware 3_0_0_0
6 Copyright (C) 2011 Simon Inns
7  
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12  
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17  
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20  
21 Email: simon.inns@gmail.com
22  
23 ************************************************************************/
24  
25 #ifndef DEBUG_C
26 #define DEBUG_C
27  
28 // Global includes
29 #include <string.h>
30  
31 // Local includes
32 #include "HardwareProfile.h"
33 #include "debug.h"
34  
35 // Microchip Application Library includes
36 // (expects V2.9a of the USB library from "Microchip Solutions v2011-07-14")
37 #include "./USB/usb.h"
38 #include "./USB/usb_function_hid.h"
39  
40 // Only compile in the global debug variables if debugging is required
41 #if defined(DEBUGON)
42  
43 // Buffer pointers
44 UINT debugBufferStart;
45 UINT debugBufferEnd;
46 UINT debugBufferLevel;
47  
48 // The following array is the cyclic buffer
49 UINT8 debugBuffer[DEBUGBUFFERSIZE];
50  
51 #endif
52  
53 // Initialise the debugging log functionality
54 void debugInitialise(void)
55 {
56 #if defined(DEBUGON)
57 // Reset the buffer's pointers
58 debugBufferStart = 0;
59 debugBufferEnd = 0;
60 debugBufferLevel = 0;
61 #endif
62 }
63  
64 // Send debug text to the debug log
65 void debugOut(char* debugString)
66 {
67 #if defined(DEBUGON)
68 UINT charNumber;
69  
70 // Is there space in the debug buffer?
71 if (debugBufferLevel + strlen(debugString) >= DEBUGBUFFERSIZE - 2)
72 {
73 // Buffer does not have enough space... silently drop the debug string
74 }
75 else
76 {
77 // Buffer is not full, write the bytes and update the end pointer
78 for (charNumber = 0; charNumber < strlen(debugString); charNumber++)
79 {
80 debugBuffer[debugBufferEnd] = debugString[charNumber];
81 debugBufferEnd = (debugBufferEnd + 1) % DEBUGBUFFERSIZE;
82  
83 // Increment the buffer level indicator
84 debugBufferLevel++;
85 }
86  
87 // Add a return and new line to the end of the string
88 debugBuffer[debugBufferEnd] = '\r';
89 debugBufferEnd = (debugBufferEnd + 1) % DEBUGBUFFERSIZE;
90 debugBufferLevel++;
91 debugBuffer[debugBufferEnd] = '\n';
92 debugBufferEnd = (debugBufferEnd + 1) % DEBUGBUFFERSIZE;
93 debugBufferLevel++;
94 }
95 #endif
96 }
97  
98 // Copy 63 bytes of the debug buffer to the USB send buffer
99 // The first byte is the number of characters transferred
100 void copyDebugToSendBuffer(BYTE* sendDataBuffer)
101 {
102 #if defined(DEBUGON)
103 UINT bytesToSend = 0;
104 UINT byteCounter;
105  
106 // Determine the number of bytes to send
107 if (debugBufferLevel > 63) bytesToSend = 63;
108 else bytesToSend = debugBufferLevel;
109  
110 // Place the number of sent bytes in byte[0] of the send buffer
111 sendDataBuffer[0] = bytesToSend - 1;
112  
113 if (debugBufferLevel != 0)
114 {
115 for (byteCounter = 1; byteCounter < bytesToSend; byteCounter++)
116 {
117 // Send the next byte to the send buffer
118 sendDataBuffer[byteCounter] = debugBuffer[debugBufferStart];
119  
120 // Update the cyclic buffer pointer
121 debugBufferStart = (debugBufferStart + 1) % DEBUGBUFFERSIZE;
122  
123 // Decrement the buffer level indicator
124 debugBufferLevel--;
125 }
126 }
127 #else
128 // Ensure that we indicate there is nothing to send if the host
129 // requests debug
130 sendDataBuffer[0] = 0;
131 #endif
132 }
133  
134 #endif