Skip to content

Commit eb0dd7d

Browse files
committed
minor fix for hardware_serial.c; ongoing development for USB
1 parent 779e5ca commit eb0dd7d

File tree

7 files changed

+888
-674
lines changed

7 files changed

+888
-674
lines changed

cores/xmega/CDC.cpp

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
//////////////////////////////////////////////////////////////////////////////
2+
// //
3+
// ____ ____ ____ //
4+
// / ___|| _ \ / ___| ___ _ __ _ __ //
5+
// | | | | | || | / __|| '_ \ | '_ \ //
6+
// | |___ | |_| || |___ _| (__ | |_) || |_) | //
7+
// \____||____/ \____|(_)\___|| .__/ | .__/ //
8+
// |_| |_| //
9+
// //
10+
//////////////////////////////////////////////////////////////////////////////
11+
112
/* Copyright (c) 2011, Peter Barrett
213
**
314
** Permission to use, copy, modify, and/or distribute this software for
@@ -14,6 +25,20 @@
1425
** SOFTWARE.
1526
*/
1627

28+
/////////////////////////////////////////////////////////////////////////////////
29+
// XMEGA NOTES:
30+
//
31+
// a) it appears that, at one time at least, this was intended to be overridden
32+
// by user code, hence the use of 'WEAK' all over the place;
33+
// b) This API is *VERY* 'tricky' in that it's tied in heavily with the atmega
34+
// USB implementation and (in some cases) does 'magic things' that are not
35+
// apparently obvious to someone trying to port it to another processor
36+
// (for an example see original use of CDC_GetInterface - lame!)
37+
// c) Given the fact that it's (in my view) POORLY WRITTEN, it deserves a makeover.
38+
// d) K&R style is hard to read. I won't use it. Hard tabs are evil. Same.
39+
//
40+
/////////////////////////////////////////////////////////////////////////////////
41+
1742
#include "Platform.h"
1843
#include "USBAPI.h"
1944
#include <avr/wdt.h>
@@ -68,10 +93,28 @@ const CDCDescriptor _cdcInterface =
6893
D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,0x40,0)
6994
};
7095

71-
int WEAK CDC_GetInterface(u8* interfaceNum)
96+
int WEAK CDC_GetInterface(u8* interfaceNum, bool bSendPacket)
7297
{
73-
interfaceNum[0] += 2; // uses 2
74-
return USB_SendControl(TRANSFER_PGM,&_cdcInterface,sizeof(_cdcInterface));
98+
interfaceNum[0] += 2; // uses 2 interfaces
99+
100+
// NOTE: the original version of this, when calling 'USB_SendControl', may NOT
101+
// actually send a packet. In fact, something upstream was likely to
102+
// "just erase" the packet buffer until an actual packet was to be sent.
103+
// Not only is this _LAME_, it his HORRIBLE DESIGN PRACTICE, UN-INTUITIVE,
104+
// and JUST! PLAIN! WRONG!!! Therefore, I added a parameter 'bSendPacket'
105+
// to determine whether or not you actually send a packet. it makes a
106+
// LOT more sense. Besides, this API doesn't have official documentation.
107+
// So I'm changing it. Please modify your own version of 'CDC_GetInterface'
108+
// if you're overriding this version with your own.
109+
110+
if(bSendPacket)
111+
{
112+
return USB_SendControl(TRANSFER_PGM, &_cdcInterface, sizeof(_cdcInterface));
113+
}
114+
else
115+
{
116+
return 1;
117+
}
75118
}
76119

77120
bool WEAK CDC_Setup(Setup& setup)
@@ -120,10 +163,25 @@ bool WEAK CDC_Setup(Setup& setup)
120163
//
121164
// *(uint16_t *)0x0800 = 0x7777; note that on XMEGA this is a VERY bad thing
122165
// wdt_enable(WDTO_120MS);
166+
//
167+
// on the atmega, address 800H is the start of the final 256-byte page in RAM space for 2k RAM
168+
//
169+
// atmega328(p) RAM goes from 0x100 through 0x8ff - see datasheet for atmega 328 [etc.] section 8.3
170+
// 32U4 RAM goes through 0xaff - see datasheet for U4 processors, section 5.2
171+
// 8/16/32U2 RAM goes through 4FFH so this won't even work - see datasheet for U2 processors, section 7.2
172+
// basically it's a 'hack' and needs to be re-evaluated
173+
174+
// TODO: would it be safe to enable interrupts, NOT return from this function,
175+
// and simply wait until the appropriate time has elapsed? Or, as is
176+
// handled in the section below, this 'wait period' is canceled
177+
178+
// TODO: if I use a function that's part of the USB driver to trigger a soft boot, I can detect
179+
// that a soft boot has taken place using the bits in the 'RESET' status register. If all
180+
// I have to do is detect this, it's not a problem, and I won't need "magic memory locations"
123181

124182
// TODO: timeout-based reboot
125183
}
126-
else // 3 lines better than 1 - ALLMAN STYLE! - do *NOT* do '} else {' - BLEAH!
184+
else
127185
{
128186
// Most OSs do some intermediate steps when configuring ports and DTR can
129187
// twiggle more than once before stabilizing.

0 commit comments

Comments
 (0)