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 CCS Technical Support

Help with RN4020 BLE radio and pointer variables

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



Joined: 14 Sep 2008
Posts: 8

View user's profile Send private message

Help with RN4020 BLE radio and pointer variables
PostPosted: Sat Oct 11, 2014 9:23 am     Reply with quote

I am trying to use the new Microchip RN4020 BLE module to send a 2byte value to my iPad. Everything works fine, however my variable named value only increments once. I am fairly certain the radio is set up correctly with a buffer of two bytes and the notify property set. I think my problem is in the use of pointer variables. I have tried this same code without pointers and it still does not work. Any help will be very much appreciated!


Code:

#include <18F25k80.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES HSH
#FUSES NOPLLEN
#include <string.h>
#define CMD pin_a3
#define WAKE_SW pin_a1
#define WAKE_HW pin_a2
#use delay(clock=20000000)
#use rs232(baud=115200, RECEIVE_BUFFER=100,PARITY=N,ERRORS, UART2, stream = RN4020)

//====================================== VARIABLES
int16 count;
int16 *value;
int1 dataReady = false;
char buffer[50];
char comStr [50];

union{
   unsigned long s_long;
   unsigned int8 u_byte[2];
   }sample;

//====================================== INTERUPTS
#INT_RDA2
void serial_isr() {
   count = 0;
   do {
   buffer[count]=fgetc();
   count ++;
   }
   while (rcv_buffer_bytes() != 0);
  // disable_interrupts(int_rda2);
   dataReady = true;
}

 //====================================== MAIN
 void main(void) {
 setup_uart (115200,RN4020);
 output_high (WAKE_SW);
 //====================================== Configure RN4020
 puts ("SF,1,2"); //clear config
 putc (13); 
 delay_ms(200);
 
 puts ("R,1");
 putc (13); 
 delay_ms(5000);
 
 puts ("SR,20004000"); // iOS mode
 putc (13);
 delay_ms(200);
 
 puts ("SS,00000001"); // broadcast only private services
 putc (13);
 delay_ms(200);
 
 puts ("PZ"); // put in private service mode
 putc (13);
 delay_ms(200);
 
 puts ("PS,11223344556677889900AABBCCDDEEFF"); // create private service
 putc (13);
 delay_ms(200);
 
 puts ("PC,010203040506070809000A0B0C0D0E0F,33,02"); // add characteristic with read 2 bytes and notify properties
 putc (13);
 delay_ms(200);
 
 puts ("PC,111213141516171819101A1B1C1D1E1F,18,02"); // add characteristic with write 2 byte buffer and notify
 putc (13);
 delay_ms(200);
 
 puts ("R,1"); //reset rn4020 to accept new config
 putc (13); 
 delay_ms(10000); // wait for RN4020 reboot
 
// puts ("A"); // put in advertisement mode if not already configured in SR
//putc (13);
 
 enable_interrupts(global);
 enable_interrupts(int_rda2);
 output_low (pin_a0);
 *value = 0x00;
while(true) {
   comStr = "SUW,010203040506070809000A0B0C0D0E0F";
   //comStr = "SHW,001C";
   output_high(pin_a0); //indicate in while loop
   *value += 1;
   printf ("%s,%lx", comStr,&value); // send command with value concatenated
   putc (13);
   delay_ms(500);
   output_low (pin_a0);
   delay_ms(500);   
   }
 }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Oct 11, 2014 1:23 pm     Reply with quote

Quote:
int16 *value;

This pointer is never initialized. It's pointing off into space somewhere.

I put a couple lines of code near the start of main() to print out the
contents of the value pointer. I changed the UART to UART1, so I could
use MPLAB 8.92's "Uart1" simulator output.
Code:
fprintf(RN4020, "value ptr = %lx \r", value);
while(TRUE);

I get this:
Quote:
value ptr = 0000

It's not pointing at any storage location, such as an array or a 16-bit
variable. So when you do this later in your code,
Code:
 *value = 0x00;

you are over-writing RAM addresses 0x00 and 0x01 with 0x00.
That's not what you want.

I don't see the need in your code to use a pointer at all. You're just
incrementing a 16-bit variable and using printf to send it as 4 ASCII
hex bytes. (Is that whay you want ?)


There might be other problems. I have not used CCS's receive buffer
feature yet. I can't do it at my current location. That code should at
least be reviewed.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sat Oct 11, 2014 2:40 pm     Reply with quote

If you are using the receive buffer, you don't want your own INT_RDA (or RDA2). As written he has the CCS buffer code, inside an interrupt driven buffer code. Unbelievably wasteful, and likely to lead to problems.
The CCS code does work, but does not handle overflows.
Better to get rid of the CCS code, and just run your own, or get rid of INT_RDA2, enable the interrupt option in the CCS code, and just call kbhit, when you want to know if any data is available.
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