View previous topic :: View next topic |
Author |
Message |
Tom.wmd
Joined: 18 Apr 2008 Posts: 7 Location: Newcastle - UK
|
Converting code for CC2420 |
Posted: Fri Apr 18, 2008 5:42 pm |
|
|
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
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
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
|
|
Posted: Fri Apr 18, 2008 6:04 pm |
|
|
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
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 18, 2008 8:12 pm |
|
|
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
|
|
Posted: Sat Apr 19, 2008 11:15 am |
|
|
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
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
Thanks
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
|
|
Posted: Sat Apr 19, 2008 11:27 am |
|
|
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
|
|
Posted: Sat Apr 19, 2008 11:31 am |
|
|
Thanks PCM, I'm still looking over it but it's always helpful to have a second opinion |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Apr 19, 2008 4:37 pm |
|
|
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
|
|
Posted: Sat Apr 19, 2008 5:12 pm |
|
|
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
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
Any ideas? |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Apr 19, 2008 6:16 pm |
|
|
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
|
|
Posted: Sat Apr 19, 2008 6:36 pm |
|
|
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
Thank you for your help
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
|
|
Posted: Sun Apr 20, 2008 9:40 am |
|
|
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);
} |
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 |
Posted: Mon May 19, 2008 7:41 am |
|
|
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. |
|
|
|