Helpful ReplyLockedBuilding USB stack projects with XC8 compiler

Page: 12 > Showing page 1 of 2
Post
PStechPaul
Super Member
2012/10/04 04:13:24
I have finally had some success with building my USB project as well as the USB DEVICE - CDC - Serial Emulator project for the PICDEM FSUSB demo board. I've been able to get it to compile and link, with some conditionals to adjust for differences in the compilers, and I was able to load the software on my target platform, but it does not properly enumerate. The same code, used with MPLAB 8.76 and the C18 compiler, works OK. And it also works in MPLAB X 1.41 if I select the C18 toolset. So there is something quite different with the XC8 compiler.
 
One major difference I see is the location of the descriptor tables. C18 puts them at 1400h while XC8 puts them at 0400h. And I know that the C18 compiler uses the #pragma CODE ADDR to locate sections of code, while the XC8 compiler ignores them. Maybe there is an alternative method of locating code blocks in XC8. I can't do any more at this time, but I could supply the modified files that enabled the project to compile. There were quite a few errors of inconsistent declarations. Perhaps XC8 is a "better" compiler because it adheres more strictly to code standards, but unfortunately at this point I can't use it, at least for my USB projects.
 
Will there be a USB stack that is compatible with XC8?

[edit] I think I found the problem. The BDT (buffer descriptor tables) must be aligned on a 512 byte block, which is done in C18 by using the following:
#define BDT_BASE_ADDR_TAG   __attribute__ ((aligned (512))) 

but in XC8 it is done differently, and is not yet implemented:
 For 16- and 32-bit compilers, change any occurrence of the aligned attribute, as in the following example: 
char __attribute__((aligned(4)))mode; to __align, i.e., __align(4) char mode;
Caveats
This feature is not yet implemented on XC8.
So I thought I could just use absolute addressing, which is also done differently:
 In XC8, change absolute object definitions such as the following example: 
int scanMode @ 0x200; to:
int scanMode __at(0x200);
But I tried that and it gave several errors.
 #ifndef __XC8 
volatile BDT_ENTRY BDT[BDT_NUM_ENTRIES] BDT_BASE_ADDR_TAG;
#else
volatile BDT_ENTRY BDT[BDT_NUM_ENTRIES] __at(0x1400);
#endif
../../../Microchip Solutions v2012-08-22/Microchip/USB/usb_device.c:323: error: "," expected
../../../Microchip Solutions v2012-08-22/Microchip/USB/usb_device.c:323: error: ")" expected
../../../Microchip Solutions v2012-08-22/Microchip/USB/usb_device.c:323: error: illegal function qualifier(s)
So it seems that XC8 cannot be used for USB projects, unless there is some other trick to achieve alignment or absolute addressing. pink
 
post edited by PStechPaul - 2012/10/04 05:26:49
Peter Camilleri
Super Member
Re:Building USB stack projects with XC8 compiler 2012/10/04 06:10:00
The user guide section 5.5.4.1 suggests:
 
volatile BDT_ENTRY BDT[BDT_NUM_ENTRIES] @ 0x1400; 
PStechPaul
Super Member
Re:Building USB stack projects with XC8 compiler 2012/10/04 13:16:18
I tried that, and it compiles, but the BDT is still located at 0x0800. When I used it to try to set absolute address for the BDT where it is defined, it seemed to write "0x1400" at address location 0x800. I think the USB protocol is very strict about where the BDT is located, and will not enumerate if improperly placed. I don't understand all that much of the USB stack and I can't dig too deeply into it to make major changes that are probably needed to work with XC8. I'm attaching the files I modified and used for the semi-successful build and a map file for the build using C18, which works.
 
[edit]
A little more progress and understanding...
The BDT is actually located in RAM and its address is specified inusb_hal_pic18.h as
#if defined(__18CXX) 
    #if defined(__18F14K50) || defined(__18F13K50) || defined(__18LF14K50) || defined(__18LF13K50)
        #define USB_BDT_ADDRESS 0x200     //See Linker Script, BDT in bank 2 on these devices - usb2:0x200-0x2FF(256-byte)
    #elif defined(__18F47J53) || defined(__18F46J53) || defined(__18F27J53) || defined(__18F26J53) || defined(__18LF47J53) || defined(__18LF46J53) || defined(__18LF27J53) || defined(__18LF26J53)
        #define USB_BDT_ADDRESS 0xD00  //BDT in Bank 13 on these devices
    #elif defined(__18F97J94) || defined(__18F87J94) || defined(__18F67J94) || defined(__18F96J94) || defined(__18F86J94) || defined(__18F66J94) || defined(__18F96J99) || defined(__18F95J94) || defined(__18F86J99) || defined(__18F85J94) || defined(__18F66J99) || defined(__18F65J94)
        #define USB_BDT_ADDRESS 0x100  //BDT in Bank 1 on these devices
    #else
        #define USB_BDT_ADDRESS 0x400     //All other PIC18 devices place the BDT in usb4:0x400-0x4FF(256-byte)
    #endif
#endif

so I used the following in usb_device.c:
 
/** USB FIXED LOCATION VARIABLES ***********************************/
#if defined(__18CXX)
    #pragma udata USB_BDT=USB_BDT_ADDRESS
#endif
#ifndef __XC8
volatile BDT_ENTRY BDT[BDT_NUM_ENTRIES] BDT_BASE_ADDR_TAG;
#else
volatile BDT_ENTRY BDT[BDT_NUM_ENTRIES] @USB_BDT_ADDRESS;
#endif

Now I have seen that the BDT[] is located at 0x400 and cdc_data_tx is located in USB dual port memory at 0x500. USB RAM is actually from 0x400 to ox7ff.
The USB_SD_Ptr is located at 0x800 in program memory and configDescriptor1 starts at 0x806 as expected. But still the device will not enumerate.
 
I have updated the files in he attached zipfile:
 
[edit] 2012-10-05 I just submitted a support ticket for this issue...
post edited by PStechPaul - 2012/10/04 23:10:34
mswh
New Member
Re:Building USB stack projects with XC8 compiler 2012/10/05 13:39:47
Hi. I'm tring to do the same thing. I want this USB stack to work with XC8. As I can understand from your post - now it is impossible. Now (00:36 06.10.2012) I've two ways: use C18 or try to find a way to run it undex XC8...
Have you ever asked Microchip about this problem?
 
Peter Camilleri
Super Member
Re:Building USB stack projects with XC8 compiler 2012/10/05 13:45:47
Clearly we're into the "raise a ticket" territory. The @ should have worked!
This now smells like a bug that needs to be squashed!
PStechPaul
Super Member
Re:Building USB stack projects with XC8 compiler 2012/10/05 15:06:31
I submitted a ticket and the response was that we will have to wait until they release a version of the USB stack that is compatible with XC8, and particularly the PIC18F series processors. I'm not holding my breath! I think I'm pretty close since it does compile and run, although not well enough to function as a USB device. This discussion probably belongs in the USB sub-forum. The compiler is probably OK, and there may be some non-standard tweaks that were used for C18. It may have something to do with pointers and memory addressing. One clue is the difference of location for the descriptors. Using C18 they are shown in the MAP file:

               device_dsc   0x00148c    program     extern C:\PICcode\ORTM4H-USB\usb_descriptors_ORTM4H.c
        configDescriptor1   0x00149e    program     extern C:\PICcode\ORTM4H-USB\usb_descriptors_ORTM4H.c
                    sd000   0x0014e1    program     extern C:\PICcode\ORTM4H-USB\usb_descriptors_ORTM4H.c
                    sd001   0x0014e5    program     extern C:\PICcode\ORTM4H-USB\usb_descriptors_ORTM4H.c
                    sd002   0x001519    program     extern C:\PICcode\ORTM4H-USB\usb_descriptors_ORTM4H.c
                    sd003   0x00154d    program     extern C:\PICcode\ORTM4H-USB\usb_descriptors_ORTM4H.c
               USB_CD_Ptr   0x001563    program     extern C:\PICcode\ORTM4H-USB\usb_descriptors_ORTM4H.c
               USB_SD_Ptr   0x001567    program     extern C:\PICcode\ORTM4H-USB\usb_descriptors_ORTM4H.c

But the MAP file for XC8 is a much different (worse?) format, and the closest thing I could find was:
_configDescriptor1                        smallconst   000806
_device_dsc                               smallconst   0008C7
_sd000                                    smallconst   0008D9
_sd001                                    smallconst   000849
_sd002                                    smallconst   00087D
_sd003                                    smallconst   0008B1

I don't think a ROM memory address should be a "smallconst", and since this location is addressed by means of a BYTE* it might not be working properly. Here is a segment map that may help:

SEGMENTS        Name                           Load    Length   Top    Selector   Space  Class
                reset_vec                      000000  000004  000004         0       0  CODE   
                bssCOMRAM                      000001  00005D  00005E         1       1  COMRAM 
                intcode                        000008  000004  00000C         4       0  CODE   
                intcodelo                      000018  00000E  000026         C       0  CODE   
                bssBANK0                       000060  00006E  0000CE        60       1  BANK0  
                bssBANK1                       000100  000040  000140       100       1  BANK1  
                smallconst                     000800  0000DE  0008DE       400       0  SMALLCON
                text18                         0008DE  001470  001D4E       46F       0  CODE   
                idloc                          200000  000008  200008    200000       0  IDLOC  
                config                         300000  00000E  30000E    300000       0  CONFIG 

JTomas
New Member
Re:Building USB stack projects with XC8 compiler 2012/10/10 10:38:38
remapping all variable in usb_device.c can obtain that usb work ok with XC8 compiler:
I check with pic18f14k50 and work fine.
These are the changes:
 
USB_VOLATILE USB_DEVICE_STATE USBDeviceState@0x230;
USB_VOLATILE BYTE USBActiveConfiguration@0x231;
USB_VOLATILE BYTE USBAlternateInterface[USB_MAX_NUM_INT]@0x232;
volatile BDT_ENTRY *pBDTEntryEP0OutCurrent@0x233;
volatile BDT_ENTRY *pBDTEntryEP0OutNext@0x235;
volatile BDT_ENTRY *pBDTEntryOut[USB_MAX_EP_NUMBER+1]@0x237;
volatile BDT_ENTRY *pBDTEntryIn[USB_MAX_EP_NUMBER+1]@0x23B;
USB_VOLATILE BYTE shortPacketStatus@0x23F;
USB_VOLATILE BYTE controlTransferState@0x240;
USB_VOLATILE IN_PIPE inPipes[1]@0x241;
USB_VOLATILE OUT_PIPE outPipes[1]@0x246;
USB_VOLATILE BYTE *pDst@0x24D;
USB_VOLATILE BOOL RemoteWakeup@0x24F;
USB_VOLATILE BOOL USBBusIsSuspended@0x250;
USB_VOLATILE USTAT_FIELDS USTATcopy@0x251;
USB_VOLATILE BYTE endpoint_number@0x252;
USB_VOLATILE BOOL BothEP0OutUOWNsSet@0x253;
USB_VOLATILE EP_STATUS ep_data_in[USB_MAX_EP_NUMBER+1]@0x254;
USB_VOLATILE EP_STATUS ep_data_out[USB_MAX_EP_NUMBER+1]@0x256;
USB_VOLATILE BYTE USBStatusStageTimeoutCounter@0x258;
volatile BOOL USBDeferStatusStagePacket@0x259;
volatile BOOL USBStatusStageEnabledFlag1@0x25A;
volatile BOOL USBStatusStageEnabledFlag2@0x25B;
volatile BOOL USBDeferINDataStagePackets@0x25C;
volatile BOOL USBDeferOUTDataStagePackets@0x25D;
 
volatile BDT_ENTRY BDT[BDT_NUM_ENTRIES]@0x200; //BDT_BASE_ADDR_TAG;

volatile CTRL_TRF_SETUP SetupPkt@0x220; //CTRL_TRF_SETUP_ADDR_TAG;
volatile BYTE CtrlTrfData[USB_EP0_BUFF_SIZE]@0x228;
 
void USBDeviceTasks(void)@0x2B2
 
Possibly of other way would be more easy. How does it?
PStechPaul
Super Member
Re:Building USB stack projects with XC8 compiler 2012/11/03 02:14:44
I have just returned to this project and I tried the absolute locations as you suggested (except for the PIC18F4455 they start in location 0x400). But I found that anything with the qualifier "@" gets located in program ROM space rather than RAM. And the absolute location method shown in the documentation does not work at all. Here is what it says:
 
In XC8, change absolute object definitions such as the following example:
int scanMode @ 0x200;
to:
int scanMode __at(0x200);
But neither one works properly. Maybe it has to do with the PIC18 processor. This has been very frustrating.
obama
New Member
Re:Building USB stack projects with XC8 compiler 2012/12/02 09:20:41
PStechPaul [edit] 2012-10-05 I just submitted a support ticket for this issue...
Any news for this support ticket? With this patch, can I use XC8 for all PIC with usb controller?
Thank you
 
PStechPaul
Super Member
Re:Building USB stack projects with XC8 compiler 2012/12/02 15:59:22
The ticket was closed. They said that we must wait for a new release of the stack. I have found other issues with the XC8 compiler as well. There has been an update but I don't have time to do beta testing. The C18 compiler works well enough to get the job done for now. Smile
ok_vicinius
New Member
Re:Building USB stack projects with XC8 compiler 2013/04/11 23:25:04
Guys I found a solution to XC8-HID! (that works for me and i hope that it helps everybody)
 
i'm using PIC18F2550, I first look at the possible lastest file that works with usb framework, than i found this example in microchip solutions (2013-02-15)
MicrochipSolutions/USB/Device - HID - Custom Demos/Firmware/..
 
I converted the "USB Device - HID - Simple Custom Demo - XC32 - PIC32MX795F512L PIM" on MPLABX
 
I get every header and source that correpond of usb framework and introduce it in my project and do a little of changes, (most hardware I/O, like LED,POT BLA BLA BLA) just to compile in XC8 associated with my project.
 
ok, it compile but not get attached, fails when transmit the descriptor, so i look at the net and found this forum, and reading I try just one thing after.
 
inside of "usb_hal_pic18.h" i just modify the address of
#define BDT_BASE_ADDR_TAG   __attribute__ ((aligned (512)))@USB_BDT_ADDRESS
 
forcing the start address to be at pre-determined #define that holds USB_BDT_ADDRESS that is especific to a class of microcontrollers.
 
for me, it's 0x400 (PIC18F2550).... so
 
I compiled it, and TÁDÁ, works everything from now.
 
anyone? i'm waiting for results!
Cya.
 

PStechPaul
Super Member
Re:Building USB stack projects with XC8 compiler 2013/04/11 23:44:52
It may be a while before I can test this, but thanks for coming up with a possible solution. I had problems with the "aligned" directive, and the absolute location alternative located what was supposed to be in RAM, into program ROM (or flash). I'll have to download the latest framework and try it. But the problem seemed to be in the XC8 compiler (or perhaps the linker). What versions of those are you using?
newfound
Super Member
Re:Building USB stack projects with XC8 compiler 2013/04/12 00:49:48
A working HID stack for XC8 was posted on this forum about a month or so ago and there have been other reports of working USB stacks for the PIC18 and XC8 going back to Oct 2012. http://www.microchip.com/forums/fb.ashx?m=680523
 
 
It seems that XC8 PIC18 compatibility has been improved from the first release of XC8, just like microchip said it would.
 
 
post edited by newfound - 2013/04/12 00:57:37
ok_vicinius
New Member
Re:Building USB stack projects with XC8 compiler 2013/04/13 10:13:12
PStechPaul

It may be a while before I can test this, but thanks for coming up with a possible solution. I had problems with the "aligned" directive, and the absolute location alternative located what was supposed to be in RAM, into program ROM (or flash). I'll have to download the latest framework and try it. But the problem seemed to be in the XC8 compiler (or perhaps the linker). What versions of those are you using?

 
I'm using:
XC8 v1.12
XC32 1.20
MPLAB v1.70
microchip_solutions_v2013-02-15
PStechPaul
Super Member
Re:Building USB stack projects with XC8 compiler 2013/04/13 21:42:18
Well, I just upgraded to the new XC8, MPLABX, and 2-2013 stack, but when I try to compile my project I get:
 
 
../../../Microchip_Solutions_v2013-02-15/Microchip/USB/usb_device.c:279: warning: unknown pragma "udata"
../../../Microchip_Solutions_v2013-02-15/Microchip/USB/usb_device.c:323: warning: unknown pragma "udata"
../../../Microchip_Solutions_v2013-02-15/Microchip/USB/usb_device.c:370: warning: unknown pragma "code"
"C:\Program Files (x86)\Microchip\xc8\v1.12\bin\xc8.exe" --pass1  --chip=18F2450 -Q -G --asmlist  --double=24 --float=24 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 -I"C:/PICcode/ORTM4H-USB_XC8" -I"C:/Microchip_Solutions_v2013-02-15" -I"C:/Microchip_Solutions_v2013-02-15/Microchip/Include" -I"C:/Microchip_Solutions_v2013-02-15/Microchip/USB" -I"C:/Microchip_Solutions_v2013-02-15/Microchip/Include/USB" --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s"  -obuild/default/production/_ext/1306123216/usb_function_cdc.p1  "../../../Microchip_Solutions_v2013-02-15/Microchip/USB/CDC Device Driver/usb_function_cdc.c"
../../../Microchip_Solutions_v2013-02-15/Microchip/USB/CDC Device Driver/usb_function_cdc.c:126: warning: unknown pragma "udata"
../../../Microchip_Solutions_v2013-02-15/Microchip/USB/CDC Device Driver/usb_function_cdc.c:200: warning: unknown pragma "udata"
"C:\Program Files (x86)\Microchip\xc8\v1.12\bin\xc8.exe"  --chip=18F2450 -G --asmlist -mdist/default/production/ORTM-4H-R1_USB.X.production.map  --double=24 --float=24 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 -I"C:/PICcode/ORTM4H-USB_XC8" -I"C:/Microchip_Solutions_v2013-02-15" -I"C:/Microchip_Solutions_v2013-02-15/Microchip/Include" -I"C:/Microchip_Solutions_v2013-02-15/Microchip/USB" -I"C:/Microchip_Solutions_v2013-02-15/Microchip/Include/USB" --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s"   -odist/default/production/ORTM-4H-R1_USB.X.production.cof  build/default/production/_ext/1472/usb_descriptors_ORTM4H.p1 build/default/production/_ext/1472/main.p1 build/default/production/_ext/16771253/usb_device.p1 build/default/production/_ext/1306123216/usb_function_cdc.p1    
Microchip MPLAB XC8 C Compiler V1.12
Copyright (C) 2012 Microchip Technology Inc.
License type: Node Configuration
C:/Microchip_Solutions_v2013-02-15/Microchip/Include\USB\usb_function_cdc.h:1032: error: could not find space (8 bytes) for variable _cdc_notice
../../../Microchip_Solutions_v2013-02-15/Microchip/USB/CDC Device Driver/usb_function_cdc.c:189: error: could not find space (64 bytes) for variable _cdc_data_tx
../../../Microchip_Solutions_v2013-02-15/Microchip/USB/CDC Device Driver/usb_function_cdc.c:190: error: could not find space (64 bytes) for variable _cdc_data_rx
../../../Microchip_Solutions_v2013-02-15/Microchip/USB/CDC Device Driver/usb_function_cdc.c:193: error: could not find space (8 bytes) for variable _cdc_notice

The problem may be the incompatibility of the #pragma udata directives, which should locate the variables in the proper location in RAM.
I tried the following, which eliminated the errors but still would not link:
 
    __attribute__ ((section ("USB_VARIABLES=0x500")))
        __attribute__((address(0x480)))

So, I used the CDC_Demo project from the new stack, and with its default processor PIC16F1459 it compiled and linked OK. But when I chose the PIC18F4550 which is in my PICDEM FS USB demo board, it gave errors similar to what I saw with my own project. So I looked at the conditionals in the usb_function_cdc.c file, and the definitions for the buffers are much different from those for the PIC18F series:
 
    #elif defined(_16F1459) || defined(_16LF1459) || defined(_16F1454) || defined(_16LF1454) || defined(_16F1455) || defined(_16LF1455)
        #define IN_DATA_BUFFER_ADDRESS 0x2140
        #define OUT_DATA_BUFFER_ADDRESS 0x2190
        #define LINE_CODING_ADDRESS 0x20A0
        #define NOTICE_ADDRESS (LINE_CODING_ADDRESS + LINE_CODING_LENGTH)
        #define IN_DATA_BUFFER_ADDRESS_TAG  @IN_DATA_BUFFER_ADDRESS
        #define OUT_DATA_BUFFER_ADDRESS_TAG @OUT_DATA_BUFFER_ADDRESS
        #define LINE_CODING_ADDRESS_TAG     @LINE_CODING_ADDRESS
        #define NOTICE_ADDRESS_TAG          @NOTICE_ADDRESS

It appears that the problem is caused by incorrect nesting of the compiler conditionals, where the following appears prior to the XC8 switch:
/** V A R I A B L E S ********************************************************/ 
#if defined(__18CXX)

I've had some success by using:
#ifndef __XC8 & defined(__18CXX) 
 
But still no joy. I still get the linker error which can't fit the cdc_rxd and cdc_txd buffers and the cdc_notice. Even when I use absolute addresses in 0x400 and 0x500 space, it is always the same error. When I use the PIC16F1459, it compiles and links OK, but for that PIC the USB variables are located as follows:
        #define IN_DATA_BUFFER_ADDRESS 0x2140 
        #define OUT_DATA_BUFFER_ADDRESS 0x2190
        #define LINE_CODING_ADDRESS 0x20A0

Strangely, the file registers go only to oxFFF, and in the MAP file they are located:
_cdc_data_rx                              (abs)        002A0
_cdc_data_tx                              (abs)        00220
_cdc_mem_type                             bssBANK1     000CE
_cdc_notice                               (abs)        00127

post edited by PStechPaul - 2013/04/13 23:43:33
PStechPaul
Super Member
Re:Building USB stack projects with XC8 compiler 2013/04/14 16:54:22
Well, a bit more (dubious) progress. I was finally able to get the PIC18F4550 version to compile and link under XC8, but it would not enumerate as a USB device. By using the C18 compiler, it worked OK. It seems that the FAR directive for the USB RAM was a problem. Here are the revised portions of the code:
 
//***************** Modified ******************//
//volatile FAR unsigned char cdc_data_tx[CDC_DATA_IN_EP_SIZE] IN_DATA_BUFFER_ADDRESS_TAG;
//volatile FAR unsigned char cdc_data_rx[CDC_DATA_OUT_EP_SIZE] OUT_DATA_BUFFER_ADDRESS_TAG;
volatile unsigned char cdc_data_tx[CDC_DATA_IN_EP_SIZE] IN_DATA_BUFFER_ADDRESS_TAG;
volatile unsigned char cdc_data_rx[CDC_DATA_OUT_EP_SIZE] OUT_DATA_BUFFER_ADDRESS_TAG;
LINE_CODING line_coding LINE_CODING_ADDRESS_TAG;    // Buffer to store line coding information
//volatile FAR CDC_NOTICE cdc_notice NOTICE_ADDRESS_TAG;
volatile CDC_NOTICE cdc_notice NOTICE_ADDRESS_TAG;

I also had to change the header file where it was defined as an extern. These variables were placed in the correct portions of RAM as displayed in the File Registers.
When I compiled using the C18 compiler, I could program the PICdem FS USB board and it enumerated properly. Oddly, however, the configDescriptor1[] seems to appear in program memory location 0x0d30 instead of 0x0808 where it is supposed to be, and where the MAP file shows it.
_configDescriptor1                        smallconst   000808

as well as the LST file:
 
1892  000808                     _configDescriptor1:
  1893  000808  09                  db low(09h)
  1894  000809  02                  db low(02h)
  1895  00080A  43                  db low(043h)
  1896  00080B  00                  db low(0)
  1897  00080C  02                  db low(02h)
  1898  00080D  01                  db low(01h)
  1899  00080E  00                  db low(0)
  1900  00080F  C0                  db low(0C0h)
  1901  000810  32                  db low(032h)

File Registers:
    Address      00   02   04   06   08   0A   0C   0E ASCII 
0D30           0209 0043 0102 C000 0932 0004 0100 0202 ..C..... 2.......
0D40           0001 2405 1000 0401 0224 0502 0624 0100 ...$.... $...$...
0D50           2405 0001 0701 8105 0A03 0100 0409 0001 .$...... ........
0D60           0A02 0000 0700 0205 4002 0000 0507 0282 ........ .@......
0D70           0040 0400 0903 3404 4D03 6900 6300 7200 @......4 .M.i.c.r
0D80           6F00 6300 6800 6900 7000 2000 5400 6500 .o.c.h.i .p. .T.e
0D90           6300 6800 6E00 6F00 6C00 6F00 6700 7900 .c.h.n.o .l.o.g.y
0DA0           2000 4900 6E00 6300 2E00 3400 4303 4400 . .I.n.c ...4.C.D
0DB0           4300 2000 5200 5300 2D00 3200 3300 3200 .C. .R.S .-.2.3.2
0DC0           2000 4500 6D00 7500 6C00 6100 7400 6900 . .E.m.u .l.a.t.i
0DD0           6F00 6E00 2000 4400 6500 6D00 6F00 3000 .o.n. .D .e.m.o.0
0DE0           730D 770D AB0D FF0D 0E1E 6EF6 0E00 6EF7 .s.w.... ...n...n
0DF0           0E00 6EF8 0105 0009 50F5 6FCB 0009 50F5 ...n.... .P.o...P

With the XC8 compiler, these tables appear to be in the correct location, but it doesn't work...
 
    Address      00   02   04   06   08   0A   0C   0E  ASCII
0800           08C5 084B 087F 0808 0209 0043 0102 C000 ..K.... ..C.....
0810           0932 0004 0100 0202 0001 2405 1000 0401 2....... ...$....
0820           0224 0502 0624 0100 2405 0001 0701 8105 $...$... .$......
0830           0A03 0100 0409 0001 0A02 0000 0700 0205 ........ ........
0840           4002 0000 0507 0282 0040 3400 4D03 6900 .@...... @..4.M.i
0850           0A03 A4D8 D001 D001 D038 C056 FFD9 C057 ........ 8.V...W.
0860           5400 6500 6300 6800 6E00 6F00 6C00 6F00 .T.e.c.h .n.o.l.o
0870           6700 7900 2000 4900 6E00 6300 2E00 3400 .g.y. .I .n.c...4
0880           4303 4400 4300 2000 5200 5300 2D00 3200 .C.D.C.  .R.S.-.2
0890           3300 3200 2000 4500 6D00 7500 6C00 6100 .3.2. .E .m.u.l.a
08A0           7400 6900 6F00 6E00 2000 4400 6500 6D00 .t.i.o.n . .D.e.m
08B0           6F00 1200 0001 0202 0000 D808 0A04 0000 .o...... ........
08C0           0101 0002 0401 0903 0004 0427 A4D8 D001 ........ ..'.....
08D0           D001 D019 5025 0B1F 0900 A4D8 D001 D001 ....%P.. ........
08E0           D012 8E30 5026 0A03 A4D8 D001 D001 D005 ..0.&P.. ........
08F0           6E50 0E01 6E14 5050 D006 6E50 0E00 6E14 Pn...nPP ..Pn...n

Maybe I can force them to the same location as for the C18 compiler?? Ah, but I see that part of the descriptor has been corrupted! pink
And now I looked again and it seems to be OK!
 
    Address      00   02   04   06   08   0A   0C   0E     ASCII
0800           08C5 084B 087F 0808 0209 0043 0102 C000 ..K.... ..C.....
0810           0932 0004 0100 0202 0001 2405 1000 0401 2....... ...$....
0820           0224 0502 0624 0100 2405 0001 0701 8105 $...$... .$......
0830           0A03 0100 0409 0001 0A02 0000 0700 0205 ........ ........
0840           4002 0000 0507 0282 0040 3400 4D03 6900 .@...... @..4.M.i
0850           6300 7200 6F00 6300 6800 6900 7000 2000 .c.r.o.c .h.i.p.
0860           5400 6500 6300 6800 6E00 6F00 6C00 6F00 .T.e.c.h .n.o.l.o
0870           6700 7900 2000 4900 6E00 6300 2E00 3400 .g.y. .I .n.c...4
0880           4303 4400 4300 2000 5200 5300 2D00 3200 .C.D.C.  .R.S.-.2
0890           3300 3200 2000 4500 6D00 7500 6C00 6100 .3.2. .E .m.u.l.a
08A0           7400 6900 6F00 6E00 2000 4400 6500 6D00 .t.i.o.n . .D.e.m
08B0           6F00 1200 0001 0202 0000 D808 0A04 0000 .o...... ........
08C0           0101 0002 0401 0903 0004 0427 A4D8 D001 ........ ..'.....
08D0           D001 D019 5025 0B1F 0900 A4D8 D001 D001 ....%P.. ........
08E0           D012 8E30 5026 0A03 A4D8 D001 D001 D005 ..0.&P.. ........

And when I tried to build and program the device, MPLABX went pftt and disappeared so I have to restart it...
OK, restarted and it runs again. So I noticed that, with XC8, the outPipes is at 0x001 and inPipes at 0x038. That doesn't seem right. So I selected C18 again. Well, I have one gripe with that - it loses its include directories so it can't find some files. OK, annoying but easily fixed. Now, using XC8, outPipes is 0x5ac and inPipes is ox5a7. There are a bunch of USB variables that should be located in USB dual-port RAM. Somehow C18 gets it right, while XC8 does not...
In usb_device.c"
 
#ifndef __XC8
    #if defined(__18CXX)
    #pragma udata
    #endif
#endif
USB_VOLATILE USB_DEVICE_STATE USBDeviceState;
USB_VOLATILE BYTE USBActiveConfiguration;
USB_VOLATILE BYTE USBAlternateInterface[USB_MAX_NUM_INT];
volatile BDT_ENTRY *pBDTEntryEP0OutCurrent;
volatile BDT_ENTRY *pBDTEntryEP0OutNext;
volatile BDT_ENTRY *pBDTEntryOut[USB_MAX_EP_NUMBER+1];
volatile BDT_ENTRY *pBDTEntryIn[USB_MAX_EP_NUMBER+1];
USB_VOLATILE BYTE shortPacketStatus;
USB_VOLATILE BYTE controlTransferState;
USB_VOLATILE IN_PIPE inPipes[1];
USB_VOLATILE OUT_PIPE outPipes[1];
USB_VOLATILE BYTE *pDst;
USB_VOLATILE BOOL RemoteWakeup;
USB_VOLATILE BOOL USBBusIsSuspended;
USB_VOLATILE USTAT_FIELDS USTATcopy;
USB_VOLATILE BYTE endpoint_number;
USB_VOLATILE BOOL BothEP0OutUOWNsSet;
USB_VOLATILE EP_STATUS ep_data_in[USB_MAX_EP_NUMBER+1];
USB_VOLATILE EP_STATUS ep_data_out[USB_MAX_EP_NUMBER+1];
USB_VOLATILE BYTE USBStatusStageTimeoutCounter;
volatile BOOL USBDeferStatusStagePacket;
volatile BOOL USBStatusStageEnabledFlag1;
volatile BOOL USBStatusStageEnabledFlag2;
volatile BOOL USBDeferINDataStagePackets;
volatile BOOL USBDeferOUTDataStagePackets;

and USB_VOLATILE is simply:
 
#if defined(USB_POLLING)
    #define USB_VOLATILE
#else
    #define USB_VOLATILE volatile
#endif

In this case USB_POLLING is defined so USB_VOLATILE does not have a specific definition...
Well, I used the advice given by BThomas earlier, and when I set the BDT to the fixed address @BDT_ADDRESS, it just barely finished building and programming when XC8 went pffft again, but miraculously, it finally seems to work! I don't see how anyone could have gotten this to work without at least some of the kludges I made to the USB stack. I think I'll give it a rest now, and then I'll put together a package with the changes I made, and also make another ticket. There are some nice things about the MPLABX IDE and I may try to use it more now (although MPLAB 8.89 is much faster), but I'm not so sure about XC8, although the problem may well be still the USB stack which I think I have fixed, at least for my PIC18F2550... mr green
post edited by PStechPaul - 2013/04/14 19:24:29
sylvain
New Member
Re:Building USB stack projects with XC8 compiler 2013/04/17 02:05:16
dear all !
i try to understand what you're doing to compile the microchip usb stack but i hav somme problems !
 
So could someone, please, resume and explain what we must change on usb stack  to compile  usb HID or USB CDC with a 18f2550 or 18f4550 ?
 
thank you for all your work !!
PStechPaul
Super Member
Re:Building USB stack projects with XC8 compiler 2013/04/17 12:59:29
I copied all the files that I changed into a folder and zipped it. These changes were specifically to use the PIC18F2550 which is in my PICdem FS USB development board, and I have only verified that it compiles, links, and loads, and when the device is programmed it enumerates successfully. However, I just now found out that the device cannot start, so there is still a problem. I don't have time now to try anything more, but I expect that it could be tracked down by comparing the LST files to see where the differences may be. If you or anyone else finds a solution, please post it here. Thanks.
mad_c
Super Member
Re:Building USB stack projects with XC8 compiler 2013/04/17 14:45:43
PStechPaul

I have just returned to this project and I tried the absolute locations as you suggested (except for the PIC18F4455 they start in location 0x400). But I found that anything with the qualifier "@" gets located in program ROM space rather than RAM. And the absolute location method shown in the documentation does not work at all. Here is what it says:

In XC8, change absolute object definitions such as the following example:
int scanMode @ 0x200;
to:
int scanMode __at(0x200);
But neither one works properly. Maybe it has to do with the PIC18 processor. This has been very frustrating.

I have not been following this thread, but this comes as quite a surprise. Absolute objects define every SFR, so they are very well tested. I have certainly not heard anything about failures with absolutes variables. Can you post a simple example of what you are seeing, with a description of what you mean by 'works properly'. Please explain how you are confirming the address allocated to the objects etc. If you have the support ticket number, too, I can take a look at that. The small examples you post above should work fine, but maybe there is more to your code than this.
 
Jeff.
PStechPaul
Super Member
Re:Building USB stack projects with XC8 compiler 2013/04/17 19:39:17
The zipfile I attached has everything needed to build the project. I used the CDC Serial Emulator demo for the PIC16F1459 which compiled, linked, and loaded OK. But when I set the processor to the PIC18F4550 which is in the PICdem FS USB board, it had problems that were resolved only by making changes to the stack files. Perhaps I should go back and use the associated project file, but I think I already did that. I'm sure I found some serious errors for the PIC18 series. It probably works for the dsPIC and the PIC16 and PIC14K processors.
 
I just tried to build the CDC Basic Demo. I had to add the paths to some files. Then I got these errors:
(From USB_descriptors.c)
#if defined(__18CXX)
#pragma romdata
#endif
...
#if defined(__18CXX)
    #pragma code
#endif
 
(from main.c)
 #pragma code REMAPPED_RESET_VECTOR = REMAPPED_RESET_VECTOR_ADDRESS
 void _reset (void)
 {
     _asm goto _startup _endasm
 }

Here are the error messages:

"C:\Program Files (x86)\Microchip\xc8\v1.12\bin\xc8.exe" --pass1  --chip=18F4550 -Q -G --asmlist  --double=24 --float=24 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 -I"C:/Microchip_Solutions_v2013-02-15/Microchip" -I"C:/Microchip_Solutions_v2013-02-15/Microchip/Include" -I"C:/Microchip_Solutions_v2013-02-15/USB/Device - CDC - Basic Demo/Firmware" --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s"  -obuild/default/production/_ext/1472/usb_descriptors.p1  ../usb_descriptors.c
../usb_descriptors.c:162: warning: unknown pragma "romdata"
../usb_descriptors.c:302: warning: unknown pragma "code"
"C:\Program Files (x86)\Microchip\xc8\v1.12\bin\xc8.exe" --pass1  --chip=18F4550 -Q -G --asmlist  --double=24 --float=24 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug,9 --addrqual=ignore --mode=free -P -N255 -I"C:/Microchip_Solutions_v2013-02-15/Microchip" -I"C:/Microchip_Solutions_v2013-02-15/Microchip/Include" -I"C:/Microchip_Solutions_v2013-02-15/USB/Device - CDC - Basic Demo/Firmware" --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s"  -obuild/default/production/_ext/1472/main.p1  ../main.c
../main.c:319: warning: unknown pragma "udata"
../main.c:370: warning: unknown pragma "code"
../main.c:373: error: expression syntax
../main.c:374: error: ";" expected
../main.c:376: warning: unknown pragma "code"
../main.c:379: error: expression syntax
../main.c:380: error: ";" expected
../main.c:381: warning: unknown pragma "code"
../main.c:384: error: expression syntax
../main.c:385: error: ";" expected
../main.c:405: warning: unknown pragma "code"
../main.c:408: error: expression syntax
../main.c:409: error: ";" expected
../main.c:410: warning: unknown pragma "code"
../main.c:413: error: expression syntax
../main.c:414: error: ";" expected
../main.c:417: warning: unknown pragma "code"
../main.c:421: warning: unknown pragma "interrupt"
../main.c:433: warning: unknown pragma "interruptlow"
../main.c:484: warning: unknown pragma "code"

post edited by PStechPaul - 2013/04/17 20:13:18
mad_c
Super Member
Re:Building USB stack projects with XC8 compiler 2013/04/18 15:06:09
Hello,
PStechPaul
The zipfile I attached has everything needed to build the project.

 
Is this the ZIp file attached some time ago? I downloaded that. There is no project file or make file, some of the file have multiple copies (like usb_function_cdc.c usb_function_cdc (2).c and usb_function_cdc (3).c) and I have not idea which is being used, etc.
 
I am very happy to help people on these forums, but I do not work in a support capacity and so I can only dedicate a very limited time to this end. The post I responded to seemed to imply that simple absolute variables 'do not work'. Do you have a small test program that demonstrates this issue?
 

(From USB_descriptors.c)
#if defined(__18CXX) 
#pragma romdata
#endif
...
#if defined(__18CXX)
   #pragma code
#endif


Here are the error messages:
 
../usb_descriptors.c:162: warning: unknown pragma "romdata"
../usb_descriptors.c:302: warning: unknown pragma "code"


There was no USB_descriptors.c file in the ZIP (The usb_descriptors_ORTM4H.c file included in the ZIP does not contain the above code) , so I can't confirm what you compiled in this example. However, XC8 does not use any pragma (like romdata or code) for memory allocation. The code you quote above implies that these pragmas are only defined if __18CXX is defined, which is normally the case for C18. The XC8 compiler only defines this macro if it is running in C18 compatibility mode, which you should not be in. The __18CXX macro should not be manually defined if you are using XC8. Clearly something is defined which allows the pragmas to pass through.
 
The replacements for these pragmas are to make things const if they need to be in program memory. If they must be at a particular address, then you can either make the variable/function absolute, or use a section specifier, then supply an additional linker option to link the section at an address, if required. So, for example:
 
int __section("myPsect") foobar;
 
then supply the driver option:
 
-L-pmyPsect=0x300
 
for example. Only assign addresses to objects if you have to. If this code has been publish to work with XC8, the there is something not right in the code. This forum only deals with the compiler and the USB code is not written by the compiler group. If you have submitted a support ticket, then continue to work with them. They can work with groups responsible for the code.
 
I am sorry I can't help more. If there is evidence to suggest that absolute variables are not working, please let us know as I am not aware of this.
 
Jeff.
 
 
PStechPaul
Super Member
Re:Building USB stack projects with XC8 compiler 2013/04/18 16:40:48
The zipfile I'm referring to was posted just yesterday, and contained all the stack files that I had to change.
 
You may have something about the __18XX but AFAIK I have not defined it anywhere in my projects. I used a new project:
USB Device - CDC - Basic Demo -  C18 - PICDEM FSUSB
 
I imported it into MPLABX - Maybe that's the problem? But I tried to keep everything as close to original as possible, and I only had problems with the PIC18F devices. I added some extra directives to bypass the #pragmas but I did not change them to sections as you suggest. So I was able to get it to compile, link, and load without errors, but when I programmed the demo board it did not work.
 
It's been a long time since I found the possible bug which located RAM variables in ROM space, and it may be that it was a problem with the older version of XC8, or perhaps I did not understand how to use sections as you show in your post.
 
I have not raised a new ticket for this. I'll try to find anyplace where __18XX may be defined. But I don't have much hope for success, since the selection of the PIC18F processors seems to be inside the __18XX blocks.
 
Thanks!
mad_c
Super Member
Re:Building USB stack projects with XC8 compiler 2013/04/18 17:34:23
PStechPaul

The zipfile I'm referring to was posted just yesterday, and contained all the stack files that I had to change.

You may have something about the __18XX but AFAIK I have not defined it anywhere in my projects. I used a new project:
USB Device - CDC - Basic Demo -  C18 - PICDEM FSUSB

I imported it into MPLABX - Maybe that's the problem? But I tried to keep everything as close to original as possible, and I only had problems with the PIC18F devices. I added some extra directives to bypass the #pragmas but I did not change them to sections as you suggest. So I was able to get it to compile, link, and load without errors, but when I programmed the demo board it did not work.

It's been a long time since I found the possible bug which located RAM variables in ROM space, and it may be that it was a problem with the older version of XC8, or perhaps I did not understand how to use sections as you show in your post.

I have not raised a new ticket for this. I'll try to find anyplace where __18XX may be defined. But I don't have much hope for success, since the selection of the PIC18F processors seems to be inside the __18XX blocks.

Thanks!

Hi,
 
If you click the verbose build option, then build, you will see the options passed to the preprocessor (CPP). The preprocessor is passed options by the driver as to what macros will be predefined. The preprocessor also uses a -D option to do this. This will show you what preprocessor macros are defined by the compiler, so to speak. Anything else defined has come from code, somewhere. Preprocessor macros only have scope in a module, so if a module is processed with a macro defined, the definition has to be in that module (which means either in that source file, or any file included into that file). You can use
 
#ifdef __18CXX
#warning the macro is currently defined
#endif
 
etc. to see at what point the macro comes into existence.
 
Jeff.
 
PStechPaul
Super Member
Re:Building USB stack projects with XC8 compiler 2013/04/18 19:16:00
I found a definition for __18CXX in Compiler.h:

#if defined(__18CXX) && !defined(HI_TECH_C) 
 // PIC18 processor with Microchip C18 compiler
    #define COMPILER_MPLAB_C18
    #include <p18cxxx.h>
#elif defined(__PICC18__) && defined(HI_TECH_C) 
 // PIC18 processor with (Microchip) HI-TECH PICC-18 compiler
 #if !defined(__18CXX)
  #define __18CXX
 #endif
    #define COMPILER_HITECH_PICC18
 #include <htc.h>
#elif (defined(_PIC12) || defined(_PIC14) || defined(_PIC14E)) && defined(HI_TECH_C)
 // PIC10/12/16 processor with (Microchip) HI-TECH PICC compiler
    #define COMPILER_HITECH_PICC
    #include <htc.h>
#elif (defined(__PIC24F__) || defined(__PIC24FK__)) && defined(__C30__) // Microchip C30 compiler
 // PIC24F processor
    #define COMPILER_MPLAB_C30
 #include <p24Fxxxx.h>
#elif defined(__PIC24H__) && defined(__C30__) // Microchip C30 compiler
 // PIC24H processor
    #define COMPILER_MPLAB_C30
 #include <p24Hxxxx.h>
#elif defined(__PIC24E__) && defined(__C30__) // Microchip C30 compiler
 // PIC24E processor
    #define COMPILER_MPLAB_C30
 #include <p24Exxxx.h>
#elif defined(__dsPIC33F__) && defined(__C30__) // Microchip C30 compiler
 // dsPIC33F processor
    #define COMPILER_MPLAB_C30
 #include <p33Fxxxx.h>
#elif defined(__dsPIC33E__) && defined(__C30__) // Microchip C30 compiler
 // dsPIC33E processor
    #define COMPILER_MPLAB_C30
 #include <p33Exxxx.h>
#elif defined(__dsPIC30F__) && defined(__C30__) // Microchip C30 compiler
 // dsPIC30F processor
    #define COMPILER_MPLAB_C30
 #include <p30fxxxx.h>
#elif defined(__C30__)  // Microchip C30 compiler, but targeting "generic-16bit" processor.
    #define COMPILER_MPLAB_C30
 #include <p30sim.h>
 // Define some useful inline assembly functions which are normally in the
 // processor header files, but absent from the generic p30sim.h file.
 #if !defined(Nop)
  #define Nop()    __builtin_nop()
  #define ClrWdt() {__asm__ volatile ("clrwdt");}
  #define Sleep()  {__asm__ volatile ("pwrsav #0");}
  #define Idle()   {__asm__ volatile ("pwrsav #1");}
 #endif
#elif defined(__PIC32MX__) // Microchip C32 compiler
 #if !defined(__C32__)
  #define __C32__
 #endif
    #define COMPILER_MPLAB_C32
 #include <p32xxxx.h>
 #include <plib.h>
#else
 #error Unknown processor or compiler.  See Compiler.h
#endif

I switched back to the XC8 compiler and tried building again. I had to restore the include paths because it seems they are cleared when the compiler is changed. Now (as before) I am getting this in the LST file:
 
  108                           opt pagewidth 120
   109  0000                     SPPDATA equ 0F62h ;#
   110                           opt subtitle "HI-TECH Software Omniscient Code Generator (Lite mode) build 49521"

So I had to activate (again) the Workstation License to use the PRO version, but the "Lite Mode" messages are still there. I found that I had to select the PRO mode from the compiler options (which I also thought I did before), and I also set verbose mode to get more information.
 
PStechPaul
Super Member
Re:Building USB stack projects with XC8 compiler 2013/04/20 22:37:07
Whew, I finally got the Serial Emulator project for PICdem FSUSB (PIC18F4550) to compile, link, load, and actually enumerate and is reported as working properly as COM6 or COM15 depending on which USB port is used.
 
I have attached the modified stack files as well as the main.c and the MPLAB.X project file (which I renamed MPLAB1.X). I had to remove and separate the build and production folders.
USBstack.zip   MPLAB1.zip   build.zip   dist.zip
 
Basically, I used the File Registers output to get the absolute addresses of the USB variables and defined them for the PIC18F4550. It probably won't work for the PIC18F2450 which has only one bank for USB variables (ox400-0x4ff). Here are the absolute locations that finally worked:
 
 
volatile CTRL_TRF_SETUP SetupPkt@0x430; //CTRL_TRF_SETUP_ADDR_TAG;
volatile BYTE CtrlTrfData[USB_EP0_BUFF_SIZE]@0x438;
USB_VOLATILE USB_DEVICE_STATE USBDeviceState@0x591;
USB_VOLATILE BYTE USBActiveConfiguration@0x592;
USB_VOLATILE BYTE USBAlternateInterface[USB_MAX_NUM_INT]@0x593;
volatile BDT_ENTRY *pBDTEntryEP0OutCurrent@0x595;
volatile BDT_ENTRY *pBDTEntryEP0OutNext@0x597;
volatile BDT_ENTRY *pBDTEntryOut[USB_MAX_EP_NUMBER+1]@0x599;
volatile BDT_ENTRY *pBDTEntryIn[USB_MAX_EP_NUMBER+1]@0x59f; //4
USB_VOLATILE BYTE shortPacketStatus@0x5a5; //4
USB_VOLATILE BYTE controlTransferState@0x5a6; //1
USB_VOLATILE IN_PIPE inPipes[1]@0x5A7; //1
USB_VOLATILE OUT_PIPE outPipes[1]@0x5AC; //5
USB_VOLATILE BYTE *pDst@0x5B3; //7
USB_VOLATILE BOOL RemoteWakeup@0x5B5; //2
USB_VOLATILE BOOL USBBusIsSuspended@0x5B6; //1
USB_VOLATILE USTAT_FIELDS USTATcopy@0x5B7; //1
USB_VOLATILE BYTE endpoint_number@0x5B8; //1
USB_VOLATILE BOOL BothEP0OutUOWNsSet@0x5B9; //1
USB_VOLATILE EP_STATUS ep_data_in[USB_MAX_EP_NUMBER+1]@0x5Ba; //1
USB_VOLATILE EP_STATUS ep_data_out[USB_MAX_EP_NUMBER+1]@0x5Bd; //2
USB_VOLATILE BYTE USBStatusStageTimeoutCounter@0x5c0; //2
volatile BOOL USBDeferStatusStagePacket@0x5C1; //1
volatile BOOL USBStatusStageEnabledFlag1@0x5C2; //1
volatile BOOL USBStatusStageEnabledFlag2@0x5C3; //1
volatile BOOL USBDeferINDataStagePackets@0x5C4; //1
volatile BOOL USBDeferOUTDataStagePackets@0x5C5; //1

and (from usb_hal_pic18.h and usb_device.c)
 
#define BDT_BASE_ADDR_TAG   __attribute__ ((aligned (512))) // This is not used because it is not supported in XC8
#define USB_BDT_ADDRESS 0x400     //All other PIC18 devices place the BDT in usb4:0x400-0x4FF(256-byte)
#define CTRL_TRF_SETUP_ADDR_TAG
#define CTRL_TRF_DATA_ADDR_TAG
volatile BDT_ENTRY BDT[BDT_NUM_ENTRIES]@USB_BDT_ADDRESS;
volatile CTRL_TRF_SETUP SetupPkt CTRL_TRF_SETUP_ADDR_TAG;
volatile BYTE CtrlTrfData[USB_EP0_BUFF_SIZE] CTRL_TRF_DATA_ADDR_TAG;

It should be possible to locate and align these variables using sections, but I did not have any success. It should work like this (from the help):
 
int foo __attribute__((section("mysection"),address(0x900)));
int bar __attribute__((section("mysection")));
int baz __attribute__((section("mysection")));

But I tried it:
volatile CTRL_TRF_SETUP SetupPkt __attribute__((section("mysection"),address(0x430)));
volatile BYTE CtrlTrfData[USB_EP0_BUFF_SIZE] __attribute__((section("mysection")));
USB_VOLATILE USB_DEVICE_STATE USBDeviceState __attribute__((section("mysection")));

and the map file showed:
_SetupPkt                                 bssCOMRAM    000039
_CtrlTrfData                              bssBANK0     0000A7
_USBDeviceState                           bssBANK0     000066

post edited by PStechPaul - 2013/04/20 23:08:50
cesar.ls
New Member
Re:Building USB stack projects with XC8 compiler 2013/04/26 10:05:59
You just saved me ;-)
obama
New Member
Re:Building USB stack projects with XC8 compiler 2013/05/12 01:54:40
PStechPaul Whew, I finally got the Serial Emulator project for PICdem FSUSB (PIC18F4550) to compile, link, load, and actually enumerate and is reported as working properly as COM6 or COM15 depending on which USB port is used.
Thank you for your work.
I have a problem. I tried this simple echo:
 
void ProcessIO(void)
{  
    if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;

    LastRS232Out = getsUSBUSART(USB_Out_Buffer,64); //until the buffer is free.
    if(LastRS232Out > 0)
    {
        if((USBUSARTIsTxTrfReady()))
        {
                //USB_Out_Buffer[0] = 'a';
                putUSBUSART(USB_Out_Buffer, 64);
        }
    }
    CDCTxService();
}//end ProcessIO

With this code I should be to see every char I transmit to virtual COM, but I receive strange char. When I put the USB_Out_Buffer[0] = 'a' I receive always char a, so reception from CDC is working, but trasmission has a problem for me. I compiled with XC8 1.12 and MLA 2-2013.
Is it happen only to me?
 
davekw7x
Entropy++
Re:Building USB stack projects with XC8 compiler 2013/05/12 07:20:24
    
/*
 * Call this function once per main loop.
 *
 * Simply echo USB serial data back to the USB connection.
 *
 * davekw7x
 */
void ProcessIO()
{
    unsigned char numBytesRead;

    /*
     *  From Microchip demo programs
     */
    BlinkUSBStatus();

    /*
     *  Don't do anything unless USB is plugged in and the host
     *  CPU has the CDC connection open.
     */
    if ((USBDeviceState < CONFIGURED_STATE) || (USBSuspendControl == 1)) {
        return;
    }
    /*
     * Don't do anything if previous transfers haven't been
     * completed.
     */
    if (USBUSARTIsTxTrfReady()) {
        numBytesRead = getsUSBUSART(USB_Out_Buffer, 64);
        if (numBytesRead > 0) {
            putUSBUSART(USB_Out_Buffer, numBytesRead);
        }
    }
    CDCTxService();
} /* End ProcessIO() */

 
 
Regards,

Dave
post edited by davekw7x - 2013/05/12 07:25:33
obama
New Member
Re:Building USB stack projects with XC8 compiler 2013/05/13 11:52:27
Thank you Dave for your reply. I think the problem is with XC8 and USB on PIC18F.
The patch of PStechPaul resolve the problem with enumeration but I have some problems with trasmission PC -> PIC.
I tested the same source code my and dave with C18 and the same PIC18F2550 and works without problems. (It's a simple echo).
EDIT
I discovered which was my problem. When I have USB_CDC_SET_LINE_CODING_HANDLER enabled the trasmission PC -> PIC is not working and PIC -> PC is ok. With USB_CDC_SET_LINE_CODING_HANDLER disabled all works very well. I hope is useful. I believe I'm not alone with this problem (XC8 + USB PIC18F). I don't know how to resolve, in this moment line_coding is useless for me.
 
post edited by obama - 2013/05/13 12:42:10
jtemples
Super Member
☄ Helpful
Re:Building USB stack projects with XC8 compiler 2013/06/27 11:52:29
The "could not find space" error is caused by the use of the "far" qualifier in the declaration.  Simply remove "far" and the stack compiles, though the USB variables in usb_device.c aren't in the right location.  That's due to an error in usb_hal_pic18.h, which can be fixed like this:
 
#ifdef __XC8  

#define BDT_ENTRY_SIZE      ((USB_MAX_EP_NUMBER + 1) * 4 * 4)

#define BDT_BASE_ADDR_TAG   @ USB_BDT_ADDRESS
#define CTRL_TRF_SETUP_ADDR_TAG @ USB_BDT_ADDRESS + BDT_ENTRY_SIZE
#define CTRL_TRF_DATA_ADDR_TAG @ USB_BDT_ADDRESS + BDT_ENTRY_SIZE + 8

#else

#define BDT_BASE_ADDR_TAG
#define CTRL_TRF_SETUP_ADDR_TAG
#define CTRL_TRF_DATA_ADDR_TAG

#endif

 
This avoids hard-coding addresses on all of the USB variables, and the modified stack will work with any PIC18.
 
The #pragma warnings are fixed by changing __18CXX to COMPILER_MPLAB_C18 around each of the #pragma directives (__18CXX should not be changed to COMPILER_MPLAB_C18 in other places).
 
The attached files were tested with XC8 V1.12 and V2013-02-15 of MAL.
post edited by jtemples - 2013/07/01 09:10:01
sparkybg
New Member
Re:Building USB stack projects with XC8 compiler 2013/07/01 04:40:35
Can someone please upload all the files that need to be changed? Sample XC8 MPLAB.X project will also be helpful.
 
I've tried the "ported" bootloader from the other thread, it compiles and runs, the device is enumerated and can receive data from the PC, but refuses to send to PC from EP1.
jtemples
Super Member
Re:Building USB stack projects with XC8 compiler 2013/07/01 09:06:14
I've attached the corrected files to my original post.  You should be able to replace the existing files in MAL with these.  There are no changes to the sample applications or projects.
MCPUSB
New Member
Re:Building USB stack projects with XC8 compiler 2013/07/17 23:08:47
I was getting "could not find space" error and tried jtemples's attached files from post #30.  Now CDC-Basic Demo compiles without error with V2013-02-15 of MAL, MPLab IDE v8.91, and C18 v9.80.  However, I am getting this error msg at the link step:
 

Executing: "E:\Engineering\Microchip\xc8\bin\mplink.exe" /p18F4550 "rm18f4550 - HID Bootload.lkr" "Objects\PICDEM FSUSB\usb_descriptors.o" "Objects\PICDEM FSUSB\main.o" "Objects\PICDEM FSUSB\usb_function_cdc.o" "Objects\PICDEM FSUSB\usb_device.o" /u_CRUNTIME /z__MPLAB_BUILD=1 /o"K USB Device - CDC - Basic Demo -  C18 - PIC18F2455.cof" /M"K USB Device - CDC - Basic Demo -  C18 - PIC18F2455.map" /W
: 0: (500) undefined symbols: __pUSB_VARIABLES(K USB Device - CDC - Basic Demo -  C18 - PIC18F2455.obj) __pUSB_BDT(K USB Device - CDC - Basic Demo -  C18 - PIC18F2455.obj)
 rm18f4550 - HID Bootload.lkr: 15: (0) the LIBPATH directive is not supported (warning)
rm18f4550 - HID Bootload.lkr: 17: (0) the FILES directive is not supported (warning)
rm18f4550 - HID Bootload.lkr: 18: (0) the FILES directive is not supported (warning)
rm18f4550 - HID Bootload.lkr: 19: (0) the FILES directive is not supported (warning)
rm18f4550 - HID Bootload.lkr: 42: (0) the STACK directive is not supported (warning)
(908) exit status = 1
(908) exit status = 1Link step failed.

 
Could anyone please tell me where or what is __pUSB_VARIABLES and __pUSB_BDT?  Device is still PIC18F4550.  Although I would like to use PIC18F2455 eventually.  If I were to compile it with XC8, then I get bunch of unknown pragma errors.
 
 
jtemples
Super Member
Re:Building USB stack projects with XC8 compiler 2013/07/17 23:49:55
The changes above are for XC8, not C18.  The "C18" that's bundled with XC8 seems to be some sort of wrapper for XC8, and I know nothing about it, but I'd be surprised if it supported .lkr files in any useful way.
 
Does it build if you remove the .lkr file from the project?
MCPUSB
New Member
Re:Building USB stack projects with XC8 compiler 2013/07/18 00:17:33
jtemples 
 Does it build if you remove the .lkr file from the project? 
 
Try it already, I get the same link error messages.  However this is fixed if I use the real C18, see below.
 
jtemples
 The changes above are for XC8, not C18.  The "C18" that's bundled with XC8 seems to be some sort of wrapper for XC8, and I know nothing about it, but I'd be surprised if it supported .lkr files in any useful way.
 
I guess I was kind of confused.  It turns out that your are right that the C18 compiler that I have been using is actually \xc8\bin\mcc18.exe.  So I change everything to C18 v3.42 and it compiles and links (v4.43) fine.  Now I just need to figure out how to get it to work with XC8.  Thanks
 
Renbo
New Member
Re:Building USB stack projects with XC8 compiler 2013/12/14 17:05:32
Greetings!
 
I know this is a response to a VERY old thread.  I was trying to utilize the updated project files in order to get USB working in the CDC Serial Emulator project included in the Microchip library demos.  The difference is that I am on the library release "microchip_solutions_v2013-06-15", so I updated the include directories accordingly.
 
I am also using a PIC18F4455 in my project.
 
I am receiving the following exceptions on build:
../../../../Microchip/USB/CDC Device Driver/usb_function_cdc.c:187: error: could not find space (64 bytes) for variable _cdc_data_tx
../../../../Microchip/USB/CDC Device Driver/usb_function_cdc.c:188: error: could not find space (64 bytes) for variable _cdc_data_rx
../../../../Microchip/USB/CDC Device Driver/usb_function_cdc.c:191: error: could not find space (10 bytes) for variable _cdc_notice
C:/microchip_solutions_v2013-06-15/Microchip/Include\./USB/usb_function_cdc.h:1032: error: could not find space (10 bytes) for variable _cdc_notice
I've already had the PCB manufactured and gone through the work of assembling the board for this project, and have successfully been able to program the board using the PICKit2, so changing the device type at this point would be a huge step back.
 
The PIC18F4455 appears to have the same same amount of SRAM as the PIC18F2455, PIC18F2550 and PIC18F4550, so I am at a loss where my problem is.
 
Any assistance would be very helpful.  I am a software engineer by trade, but in the higher-level language arena.  This micro-controller stuff is a new experience!
Renbo
New Member
Re:Building USB stack projects with XC8 compiler 2013/12/14 17:10:24
Sorry!
 
I found the part of this thread that addresses the out of memory problem!  Please disregard my inquiry!
embpic
Starting Member
Re:Building USB stack projects with XC8 compiler 2014/04/02 23:37:33
hello sir i also need PIC18f4550 usb communication using CDC for virtual com. plz provide me working CDC project in xc8 compiler
CCP1CON
Super Member
Re:Building USB stack projects with XC8 compiler 2014/04/03 06:07:36
But I found that anything with the qualifier "@" gets located in program ROM space rather than RAM.

 
I use with 18F2550 and XC8 v1.30:
 
volatile BDT_ENTRY ep0Bo @ 0x400;
volatile BDT_ENTRY ep0Bi @ 0x404;

 
and it works ...
Rohit tandle
New Member
Re:Building USB stack projects with XC8 compiler 2014/08/07 07:02:56
HI
I am using USB Stack with PIC32MX795F512L it is working fine.
I want to through some data into USB but the data getting written into .txt in single line only.
Data should be written on next line after few char but it is getting written on one line only.
I have used line feed 0x0A and carriage return i.e 0X0D but no luck please help me to resolve the issue.
Page: 12 > Showing page 1 of 2