CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Converting code for CC2420

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Tom.wmd



Joined: 18 Apr 2008
Posts: 7
Location: Newcastle - UK

View user's profile Send private message

Converting code for CC2420
PostPosted: Fri Apr 18, 2008 5:42 pm     Reply with quote

I'm trying to convert some code I have from C18 into CCS, I've got it compiling but I must be missing something or have done something wrong because the code doesn't work Crying or Very sad

I only started using CCS a couple months back so maybe I'm missing something trivial?

I've attached the original working code and my "convert"

I'm using the Pixie board (datasheet) and trying to code for the CC2420 (datasheet)

If anyone could help or point me in the right direction I would be eternally grateful as I've had no luck so far Smile


Last edited by Tom.wmd on Fri Feb 06, 2009 1:50 pm; edited 3 times in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 18, 2008 6:04 pm     Reply with quote

Quote:
I've attached the original working code and my "convert"

I don't see these attachments. All I see are the data sheets.
Tom.wmd



Joined: 18 Apr 2008
Posts: 7
Location: Newcastle - UK

View user's profile Send private message

PostPosted: Fri Apr 18, 2008 6:31 pm     Reply with quote

Trust me to forget the important bit Embarassed

Original version and my version

Microchip Zigbee Stack for C18 and Hi-Tech compilers:
http://ww1.microchip.com/downloads/en/AppNotes/MpZBeeV1.0-3.5.zip

+++++++++++++++++
Tom.wmd link is dead.
Microchip link provided for reference.
- Forum Moderator
+++++++++++++++++


Last edited by Tom.wmd on Sat Apr 19, 2008 11:08 am; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 18, 2008 8:12 pm     Reply with quote

There is no correlation between the filenames in the two folders.
One folder has a cc2420.c file, but other folder does not.
Another folder has got three "main" files.
There's no way to do a comparison of the translated files.
There should be a 1:1 correspondence. But, there isn't.
For that reason, I am giving up.
Tom.wmd



Joined: 18 Apr 2008
Posts: 7
Location: Newcastle - UK

View user's profile Send private message

PostPosted: Sat Apr 19, 2008 11:15 am     Reply with quote

Thanks for have a look at it.
Quote:
There should be a 1:1 correspondence. But, there isn't.

After looking back over the code I realise what a shambles I left it in Embarassed


I have uploaded my 1:1 conversion. All files named the same and code as close as possible.

If you could have a look over that, or if anyone else would, I would be very grateful.


p.s also remembered to attach it this time Very Happy
Thanks Very Happy


Last edited by Tom.wmd on Fri Feb 06, 2009 1:50 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Apr 19, 2008 11:27 am     Reply with quote

That's better. If no one else helps you before then, I'll look at it on Sunday.
Tom.wmd



Joined: 18 Apr 2008
Posts: 7
Location: Newcastle - UK

View user's profile Send private message

PostPosted: Sat Apr 19, 2008 11:31 am     Reply with quote

Thanks PCM, I'm still looking over it but it's always helpful to have a second opinion Very Happy
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sat Apr 19, 2008 4:37 pm     Reply with quote

Code:
char Receive_CC2420(char *data, int timeout)
{
   char len=0;
   int i;
Doesn't look critical, but in order to make your code equal on both compilers change the two declarations of 'int' to 'long'.

Tip: For improved compatibility between platforms I prefer to use (user defined) types like int8, int16 and int32 which can be made equal on all your compilers.

Quote:
setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_4);
Bug: This configures the SPI bus in mode01 while your original code was mode00. See for comparison:
Code:
#define SPI_MODE_0_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_0_1  (SPI_L_TO_H)
#define SPI_MODE_1_0  (SPI_H_TO_L)
#define SPI_MODE_1_1  (SPI_H_TO_L | SPI_XMIT_L_TO_H)
Tom.wmd



Joined: 18 Apr 2008
Posts: 7
Location: Newcastle - UK

View user's profile Send private message

PostPosted: Sat Apr 19, 2008 5:12 pm     Reply with quote

ckielstra wrote:
Doesn't look critical, but in order to make your code equal on both compilers change the two declarations of 'int' to 'long'.

Have done thanks Very Happy

I was playing around with the SPI modes before as I hadn't seen any reference to which L_TO_H and H_TO_L were.
I ended up changing from
Code:
setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_4);

to
Code:
setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_4);

This seemed to fix the problem but I have two problems.

1. It works fine when connected to the ICD and a USB to serial converter but not from the battery powered circuit, if you switch it on and off a few times and you're lucky it will work.

Are there any fuses or other settings I need to put in place? I'm running it from a 3.7V battery.

2. When I switch the SPI mode, to try SPI_XMIT, it doesn't work, but then when I switch it back to the same code it still doesn't work.

The only way I get it working again is if I program it with the original C18 code and then back to my CCS version Confused

Any ideas?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sat Apr 19, 2008 6:16 pm     Reply with quote

Tom.wmd wrote:
I was playing around with the SPI modes before as I hadn't seen any reference to which L_TO_H and H_TO_L were.
I ended up changing from
Code:
setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_4);

to
Code:
setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_4);

This seemed to fix the problem but I have two problems.
Check the CC2420 datasheet:
- Clock level in inactive state is low
- Data is clocked in at the positive clock edge.
Then read the SPI specifications at: http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
From this you can conclude the required SPI mode = 0 (CPOL=0 / CPHA=0)
Code:
setup_spi(SPI_MASTER|SPI_L_TO_H | SPI_XMIT_L_TO_H|SPI_CLK_DIV_4);
Tom.wmd



Joined: 18 Apr 2008
Posts: 7
Location: Newcastle - UK

View user's profile Send private message

PostPosted: Sat Apr 19, 2008 6:36 pm     Reply with quote

ckielstra wrote:
From this you can conclude the required SPI mode = 0 (CPOL=0 / CPHA=0)
Code:
setup_spi(SPI_MASTER|SPI_L_TO_H | SPI_XMIT_L_TO_H|SPI_CLK_DIV_4);


I have tested it again and it's working now Confused

Thank you for your help Very Happy

Are there any fuses I haven't set or perhaps something that it setup at the beginning correctly because it is still hit and miss if the PIC will run when hooked up to the battery
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Apr 20, 2008 9:40 am     Reply with quote

Tom.wmd wrote:
Are there any fuses I haven't set or perhaps something that it setup at the beginning correctly because it is still hit and miss if the PIC will run when hooked up to the battery
Some small improvements can be made to the fuses but don't expect too much from this:
    - Change NOPUT to PUT; enabling the Power-Up Timer holds the PIC in reset for a short time during start-up to prevent power related problems.
    - Add NOXINST; the CCS compiler does not yet support the extended instruction set and this sometimes causes weird problems.


I don't know how all your external hardware is connected so it is difficult to give more hints. One difference I noticed is that in your original code PortA is configured with all outputs low, this is missing in your translated code. Also, the use of Default_IO in combination with Fast_IO might be confusing to the compiler and I suggest the removing of Default_IO. Here is a modified version of your init function:
Code:
#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)

//**************************************************************************
//** Function to initialise all I/O resources used by Processor ************
//**************************************************************************
void Init_IO(void)
{
   #use fast_io(A)               //Use Fast_io for settings TRIS
   #use fast_io(B)
   #use fast_io(C)
   SET_TRIS_A(0XEC);             // A0, A1 and A4 are LED outputs.
   SET_TRIS_B(0x0F);
   SET_TRIS_C(0x90);             // C3&C5=outputs for SPI, C0=CSn output, C2=RESETn output
   
   output_A(0);                  // Init all LEDs
   output_high(PIN_C0);          // CSn initially set high (CC2420)
   output_high(PIN_C2);          // RESETn initially set high (CC2420)
   
   setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_4);
}


Code:
#define LED PIN_A4
I updated the call to set_tris() in the new init function above to include this LED.

Several output pins are not initialised in the above setup code. What is connected to your pins B4-B7, and C1?
kvl
Guest







Converting code for CC2420
PostPosted: Mon May 19, 2008 7:41 am     Reply with quote

Hi all,

Even though these messages were written only fairly recently, I cannot check the sourcecode anymore -- would it be possible to repost it?

cheers,

Kristof.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group