Microchip RN4020 Bluetooth LE

Discussion in 'Embedded Systems and Microcontrollers' started by joeyd999, Jan 8, 2015.

  1. joeyd999

    Thread Starter AAC Fanatic!

    Jun 6, 2011
    Anyone have experience using the RN4020 module? Any hiccups or anything to be aware of?

    Also, it is my understanding that, as long as you use the module unmodified, no addtional FCC testing or approvals are required. Is this correct?

    Edit: I'll be using it PIC to PIC, not PIC to PC or mobile device.
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    Sorry I have no experience to share.

    I have yet to hear about the FCC tracking down wild bands of roving bluetooth free booters,so if this is for your own enjoyment got for it.

    However, I suspect you have bigger interests in mind, such as a commercial product. I'd hit up Microchip and see how wide their FCC authorization spreads.
  3. kender

    Senior Member

    Jan 17, 2007
    The documentation for RN4020 leaves a lot to be desired. I have to read every paragraph at least 3 times. Not many examples.
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    I have not used the microchip module but I have used one of those HC-06 modules from eBay. Very easy to implement. A lot cheaper too I bet.
  5. geoffers

    Active Member

    Oct 25, 2010
    I haven't used the rn4020 (sorry I'm giving you as much info as everyone else!) I do have one on the desk ready to try though. I have used a Rn42 though, was quite easy even for a fool like me to get going, roving networks do a datasheet for the commands which is good to go with the data sheet for the Rn42.

    Do let us know how you get on, the 4020 is half the price of 42 here in UK, about £4.50 I think, rs do list it as a development module though (not sure what that means?)

    When I've finished building the house and the baby's bigger maybe I'll get to it :)

    Cheers Geoff
  6. atferrari

    AAC Fanatic!

    Jan 6, 2004
    Somewhat related Joey, MCP recommends to make available the 4 specific pins used in the factory for tests in case the module has to be checked. It seems that an approval could be needed requiring a such a test.


    Sorry. The above pertains to the RN42, not to the RN4020. My bad.

    Last edited: Jun 11, 2016
  7. BR-549

    Distinguished Member

    Sep 22, 2013
    Neat little unit. I don’t believe you need to worry. Unless you built the chip.
  8. joeyd999

    Thread Starter AAC Fanatic!

    Jun 6, 2011
    The original project that I was going to use the RN4020 went by the wayside, but I am now working on another project -- this one integrates the RN4020 into an existing design with a PIC18F85J90.

    So far, I am getting the RN4020 initialized and connect to my Android. I can read and write the built-in characteristics. Things look good.

    Most annoying is the command interface -- it's all through ASCII commands and handshaking is done via returned messages like "AOK", "ERR", and such. Unfortunately, I cannot use any blocking functions to write the commands and wait for the response, so I am building what looks like is going to be a huge state machine to send commands and react to parsed responses. And, of course I am doing it in .asm.

    This is how I have fun. Seriously.
    cmartinez likes this.
  9. GopherT

    AAC Fanatic!

    Nov 23, 2012

    Fun? If were you, I would set the record for enjoying 5pm on Friday more than anyone else on earth.
  10. GopherT

    AAC Fanatic!

    Nov 23, 2012
    I just bought an HC06 but haven't had time to do anything with it. Do you have any PIC-based code to share?

    From what I've read, it seems like the HC-06 works a lot like the AT command-lines @joeyd999 is describing for the microchip device.
  11. joeyd999

    Thread Starter AAC Fanatic!

    Jun 6, 2011
    Spent the weekend coding. Much fun!
  12. GopherT

    AAC Fanatic!

    Nov 23, 2012
    Too bad for you! I got to transplant shrubs in the rain. Clay, shale, mud - oh, the joy!
  13. joeyd999

    Thread Starter AAC Fanatic!

    Jun 6, 2011
    You and I have very different ideas of "fun"...
    cmartinez and GopherT like this.
  14. NorthGuy

    Active Member

    Jun 28, 2014
    The things that you describe ... Both look like fun to me :)
  15. joeyd999

    Thread Starter AAC Fanatic!

    Jun 6, 2011
    The default 115K baud, on the receiver side, is killing me! Dammit, I'll make it work, though. Setting a lower baud is cheating, IMHO...
  16. NorthGuy

    Active Member

    Jun 28, 2014
    115K baud is one character every 86 us. Looks very slow to me. How fast is your PIC running?
  17. joeyd999

    Thread Starter AAC Fanatic!

    Jun 6, 2011
    The the PIC is running 8 mhz. That's not my problem, though. I've got a high priority interrupt that runs periodically and consumes more than two character times. For reasons I won't explain, these things need to happen within the interrupt, and not outside in the main code.

    The receiver is also running high priority. My solution is likely going to be to check the receiver interrupt flag within the other service routine and process it if necessary.

    The other alternative is to just change my clock speed -- but this is an existing application and I have reasons for not wanting to make major changes.

    Alternatively, I can just change the baud of the RN4020, but like I said, that's cheating.
  18. joeyd999

    Thread Starter AAC Fanatic!

    Jun 6, 2011
    Here's the first part of my code to parse responses. It looks for the 'standard responses' and sets a flag if one of the standard responses was received. If the response is non-standard, it sets another flag to indicate it, for further parsing later (the parsing method is determined by the command sent that evoked the response).

    Code (ASM):
    2. ;*****************************************
    3. ;** BTPRCV -- Process Bluetooth receiver **
    4. ;*****************************************
    6. btprcv    retbc    fbtinit            ;don't receive if in initialzation
    8.     clrf    btsresp,1        ;clear standard response flags
    10.     movlf    fsr0h,high btrcvbuff    ;point to receiver buffer
    11.     movff    btrhead,fsr0l        ;  and index to next character
    13. btplp    rcall    rxbyte            ;receiver byte available?
    14.     retz                ; z if none
    16.     movwf    indf0            ;save character at current position
    17.     movlw    '\r'            ;ignore return characters
    18.     cpfseq    indf0
    19.     movf    postinc0,w        ;get back in w, and update pointer
    20.     movff    fsr0l,btrhead        ;save new head position
    22.     xorlw    '\n'            ;new character a line feed?
    23.     bnz    btplp            ;no, get some more characters, if available  
    25.     movlfb    btrhead,low btrcvbuff    ;reset btrhead for next message
    27. ;parse for common messages
    28. ;if common response not matched, indicate (likely) parameters returned from RN4020
    30.     bsf    btsresp,0,1        ;set 1st bit in standard response flags
    32.     movdf    tblptr,btcmes        ;tblptr -> possible responses (\n separated, \0 terminated)
    34. btplp0    lfsr    0,btrcvbuff        ;fsr0 -> point to head of response buffer
    36. btplp1    tblrd    *+            ;get next character from list of responses
    37.     movf    tablat,w        ; into w reg
    38.     bz    btpnmf            ;if 0, no response matched
    40.     cpfseq    postinc0        ;test against charcter in response buffer
    41.     bra    btpnom            ;!= means no match, skip to next response
    43.     xorlw    '\n'            ;new line character?
    44.     bz    btpmat            ;yes, we have found a match
    46.     bra    btplp1            ;keep testing characters till match
    48. ;no match of current response, skip ahead till /n or /0
    50. btpnom    clrc                ;roll to next response
    51.     rlcf    btsresp,f,1
    53. btplp2    tblrd    *+            ;get next character from list of responses
    54.     movf    tablat,w        ; into w reg
    55.     bz    btpnmf            ;zero of no match found
    57.     xorlw    '\n'            ;new line character?
    58.     bnz    btplp2            ;loop till \0 or \n
    60.     bra    btplp0            ;and test against next response
    62. ;match found, response bit set
    64. btpmat
    66. ;match not found, non-standard response bit set
    68. btpnmf
    70.     return
    72. btcmes    db    "AOK\n","ERR\n","Bonded\n","Secured\n","Connection End\n","Reboot\n",'\0'
    Edit: For the umpteenth time! Please, someone, tell me how I set tab width within the code tags! I refuse to change my tabs to spaces.
    Last edited: May 24, 2016
  19. NorthGuy

    Active Member

    Jun 28, 2014
    Certainly, you have room for increasing it.

    If you find a good "middle" where you can put the check it certainly will work, but then changing your "big thingy" code will move this point back and forth in time and may disturb timing.

    You can try re-saving your shadow registers and enabling high-priority interrupts before entering your "big thingy" then restoring it all back at the end of it. This way your Bluetooth interrupts can interrupt your "big thingy". If your "big thingy" has critical sections which cannot be interrupted, then you can disable the interrupt around these sections.
  20. joeyd999

    Thread Starter AAC Fanatic!

    Jun 6, 2011
    What's really super, duper fun is drinking a few pints while writing some really complicated code. That's what I'm doing now. I may have to pop off into the off-topic sub-forum for awhile...