about Microchip RN4020

Thread Starter

KANG ZHANG

Joined Mar 15, 2018
11
Hi, I am new to this forum. I would like to use Microchip RN4020 for Bluetooth LE communication. My applicaiton is easy, using an MCU to collected data from an ADC chip (through SPI), and then send the data out through the RN4020 Chip (through UART).

My questions is what is the smallest microchip IC (in terms of pin out ) I can use to perform the job? From what I understand, I only need two communication protocol. One is SPI with ADC chip and one with UART for RN4020. Is that all? Any suggestions?

Thanks in advance!

-Kang
 

Thread Starter

KANG ZHANG

Joined Mar 15, 2018
11
Why does it have to be the smallest? What is your actual device package requirement?
For some reason, they wanted the MCU to be soldered to a flex cable circuit rather than a rigid PCB. Therefore, small package, low pin count and large pitch is desirable for reliabiliy.
If not possible, I have to design a rigid PCB module and attached it to the flex cable. This is the second choice.
 

be80be

Joined Jul 5, 2008
2,072
PIC16F1825 would be a good bet it can do uart and spi at same time 14 pin 4 for spi 2 for uart 1 for adc that kind knocks out any
12f chip

Power 2 pins
reset 1
4 for spi
2 for uart
1 for adc
Don't leave but 4

But it's a great little chip.
 

nsaspook

Joined Aug 27, 2009
13,272

jpanhalt

Joined Jan 18, 2008
11,087
As you may only need 3 pins for SPI (CS/SS,Clock, and MOSI/SDO) and 1 for UART a 6-pin device such as the 10F322 is quite small with relatively wide pin spacing in the SOT23-6 package.

A bit easier would be an 8 pin device such as the enhanced mid-range 12F18xx (e.g, 12F1840) that come with hardware SPI and UART (EUSART) in a variety packages (PDIP,SOIC,DFN, and UDFN).
 

Thread Starter

KANG ZHANG

Joined Mar 15, 2018
11
PIC16F1825 would be a good bet it can do uart and spi at same time 14 pin 4 for spi 2 for uart 1 for adc that kind knocks out any
12f chip

Power 2 pins
reset 1
4 for spi
2 for uart
1 for adc
Don't leave but 4

But it's a great little chip.
The chip looks great. thanks for the suggestion!
 

Thread Starter

KANG ZHANG

Joined Mar 15, 2018
11
Selecting a chip family or the exact device is just guessing without knowing how complex your application is. A example that might be overkill for your application.

My last RN4020 app programmed in C used a PIC24 low count device (PIC24FV16M202) like this one. http://www.microchip.com/wwwproducts/en/PIC24F16KM202

I used the DIP version for a DIY project.
https://forum.allaboutcircuits.com/threads/pic24-bluetooth-low-energy-dk.128497/#post-1054658
Thanks a lot for the info! I have just gone through your PIC24 project, and it's exactly the same as what I am trying to develop. I think I will order a few PIC24 MCU to do a trial on your code. By the way, may I know if your RN4020 had been burnt with v1.33. I contacted Microchip and they recommended to order RN4020 v1.33 for new user. If successfuly, I will try to migrate the code to smaller chips.
 

Thread Starter

KANG ZHANG

Joined Mar 15, 2018
11
As you may only need 3 pins for SPI (CS/SS,Clock, and MOSI/SDO) and 1 for UART a 6-pin device such as the 10F322 is quite small with relatively wide pin spacing in the SOT23-6 package.

A bit easier would be an 8 pin device such as the enhanced mid-range 12F18xx (e.g, 12F1840) that come with hardware SPI and UART (EUSART) in a variety packages (PDIP,SOIC,DFN, and UDFN).
Thanks for the suggestion. They are really small chips. I think I will try to develop on PIC24F family, and hopefully can migrate the code to these small MCU after optimiation or simplification. See how it goes.
 

nsaspook

Joined Aug 27, 2009
13,272
Thanks a lot for the info! I have just gone through your PIC24 project, and it's exactly the same as what I am trying to develop. I think I will order a few PIC24 MCU to do a trial on your code. By the way, may I know if your RN4020 had been burnt with v1.33. I contacted Microchip and they recommended to order RN4020 v1.33 for new user. If successfuly, I will try to migrate the code to smaller chips.
V1.33.4 is what's recommended and that should be what's currently in inventory at Microchip. There's a lot of extra debugging code, debug I/O outputs and hardware ('link' a slave p24 device on SPI2) in that project that can be removed to simplify the software so it might fit into a smaller chip in the P24 family. I think I removed all the chip specific includes a few days ago for the P24FV16KM202.

https://www.microchip.com/wwwproducts/en/RN4020
https://www.mouser.com/ProductDetail/Microchip-Technology/RN4020-V-RMBEC133?qs=1mbolxNpo8csPkRaxu3I2Q==
 
Last edited:

Thread Starter

KANG ZHANG

Joined Mar 15, 2018
11
V1.33.4 is what's recommended and that should be what's currently in inventory at Microchip. There's a lot of extra debugging code, debug I/O outputs and hardware ('link' a slave p24 device on SPI2) in that project that can be removed to simplify the software so it might fit into a smaller chip in the P24 family. I think I removed all the chip specific includes a few days ago for the P24FV16KM202.

https://www.microchip.com/wwwproducts/en/RN4020
https://www.mouser.com/ProductDetail/Microchip-Technology/RN4020-V-RMBEC133?qs=1mbolxNpo8csPkRaxu3I2Q==
Thanks a lot for the information. Your code compiles fine, and I am tring to optimize it for my app. I hv completed my schematic and routing the board now. By the way, where did you find the schematic and pcb liabrary for RN4020. I coundn't find them, and I coundn't even find the PIC24FV16KM202 library from Altium website. I am now using another similar PIC24F MCU to represent it.

Also, I found RN4020 V1.33 is currently out of stock and I have to wait for14+ weeks. I am now getting a few RN4020 V1.23 chips to try out. May I know if you have tested your code capability with RN4020 V1.23 version?

And, if I have to flash the RN4020 V1.23 chip with V1.33, can I do it over the UART port of my PC. I found the PICtail demo board used USB emulated UART port for firmware upgrade. I assume I can use the physical UART of my PC for the update, you have any experience? Your kind advice is much appreciated. Thank you,
 

Thread Starter

KANG ZHANG

Joined Mar 15, 2018
11
Thanks a lot for the information. Your code compiles fine, and I am tring to optimize it for my app. I hv completed my schematic and routing the board now. By the way, where did you find the schematic and pcb liabrary for RN4020. I coundn't find them, and I coundn't even find the PIC24FV16KM202 library from Altium website. I am now using another similar PIC24F MCU to represent it.

Also, I found RN4020 V1.33 is currently out of stock and I have to wait for14+ weeks. I am now getting a few RN4020 V1.23 chips to try out. May I know if you have tested your code capability with RN4020 V1.23 version?

And, if I have to flash the RN4020 V1.23 chip with V1.33, can I do it over the UART port of my PC. I found the PICtail demo board used USB emulated UART port for firmware upgrade. I assume I can use the physical UART of my PC for the update, you have any experience? Your kind advice is much appreciated. Thank you,
Just realized that the PC RS232 has different voltage levels, so I still have to use a USB to UART dongle. I will try out and see if it works.
 

nsaspook

Joined Aug 27, 2009
13,272
Thanks a lot for the information. Your code compiles fine, and I am tring to optimize it for my app. I hv completed my schematic and routing the board now. By the way, where did you find the schematic and pcb liabrary for RN4020. I coundn't find them, and I coundn't even find the PIC24FV16KM202 library from Altium website. I am now using another similar PIC24F MCU to represent it.

Also, I found RN4020 V1.33 is currently out of stock and I have to wait for14+ weeks. I am now getting a few RN4020 V1.23 chips to try out. May I know if you have tested your code capability with RN4020 V1.23 version?

And, if I have to flash the RN4020 V1.23 chip with V1.33, can I do it over the UART port of my PC. I found the PICtail demo board used USB emulated UART port for firmware upgrade. I assume I can use the physical UART of my PC for the update, you have any experience? Your kind advice is much appreciated. Thank you,
On vacation so I don't have local access to my system. Think 1.33 is hardcoded so a upgrade is needed. It's possible to ota upgrade with my firmware. My pcb board design was just to keep track, don't trust it. https://www.google.com/url?sa=t&source=web&rct=j&url=https://www.diymodules.org/eagle-show-library?type=usr&id=1012211043&ved=2ahUKEwiEyPyBlZLaAhUW1GMKHSLICIMQFjAAegQIBxAB&usg=AOvVaw0eI7oSl8dr-3uWCiQz2SwK
 

nsaspook

Joined Aug 27, 2009
13,272
I just checked the config.h version options. It checks for both 1.23 and 1.33 but all my tests were at 1.33 firmware. YMMV with 1.23
C:
// from config.h
//If defined, the RN4020's firmware version will be checked as part of initialization.
//If the version is not equal to the version specified below, the board will
//error out with the initialization error code display and not operate
#define VERIFY_RN_FW_VER
#define RN_FW_VER_MAJOR     1           //Require 1.23.5
#define RN_FW_VER_MINOR     23          //These values can be changed as needed
#define RN_FW_VER_PATCH     5

#define RN_FW_VER_MAJOR133     1
#define RN_FW_VER_MINOR133     33
#define RN_FW_VER_PATCH133     4

// from bluetooth.c
#ifdef VERIFY_RN_FW_VER
//Retrieve firmware version on module and check against the required version
//Returns true if version is correct; false if not or communication failure

uint16_t BT_CheckFwVer(void)
{
    char fpVer[20];
    char *pfpVer = fpVer;
    char strVer[100];
    char *pstrVer = strVer;
    uint16_t verMajor,
        verMinor,
        verPatch;

    //flush UART RX buffer just in case there's old data
    while (UART_IsNewRxData()) { //While buffer contains old data
        UART_ReadRxBuffer(); //Keep reading until empty
        WaitMs(100);
    }

    StartTimer(TMR_RN_COMMS, 2000);
    BT_SendCommand("v\r", false); // Get firmware version
    while (!BT_ReceivePacket(strVer)) {
        if (TimerDone(TMR_RN_COMMS)) {
            return false;
        }
    }

    //Skip to first digit
    while ((*pstrVer < '0' || *pstrVer > '9') && *pstrVer != NULL) {
        pstrVer++;
    }
    //Extract version number
    while ((*pstrVer >= '0' && *pstrVer <= '9') || *pstrVer == '.') {
        *pfpVer = *pstrVer;
        pfpVer++;
        pstrVer++;
    }
    *pfpVer = '\0';

    //Tokenize and convert to unsigned
    sscanf(fpVer, "%u.%u.%u", &verMajor, &verMinor, &verPatch);

    //Verify version number
    if ((verMajor != RN_FW_VER_MAJOR133) && (verMajor != RN_FW_VER_MAJOR)) {
        return false;
    }
    if ((verMinor != RN_FW_VER_MINOR133) && (verMinor != RN_FW_VER_MINOR)) {
        return false;
    }
    if ((verPatch != RN_FW_VER_PATCH133) && (verPatch != RN_FW_VER_PATCH)) {
        return false;
    }

    return verMinor;
}
#endif //VERIFY_RN_FW_VER
There is also a OTA update function in my code using.
C:
#define BT_OTA_UPD    PORTBbits.RB3
#define BT_OTA_UPD_TRIS    TRISBbits.TRISB3

// from bluetooth.c

    /* Jumper on DFU OTA UPDATE */
    BT_OTA_UPD_TRIS = 1; // set for jumper input
    CNPU1bits.CN7PUE = 1; // pullup for RB3
    WaitMs(2); // jumper pullup read delay, rise time is slow
    if (BT_OTA_UPD == 0) {
        BT_OTA_UPD_TRIS = 0; // set back to output
        BT_WAKE_SW = 1;
        BT_WAKE_HW = 1;
        BT_CMD = 0;

        WaitMs(100);
        BT_SendCommand("SF,2\r", false); // perform complete factory reset
        WaitMs(100);
        BT_CheckResponse(AOK);

        BT_SendCommand("SF,2\r", false); // perform complete factory reset again
        WaitMs(100);
        if (!BT_CheckResponse(AOK)) {
            return false;
        }

        BT_SendCommand("SDH,4.1\r", false); // defaults
        WaitMs(100);
        if (!BT_CheckResponse(AOK)) {
            return false;
        }
        BT_SendCommand("SDM,RN4020\r", false); // defaults
        WaitMs(100);
        if (!BT_CheckResponse(AOK)) {
            return false;
        }

        BT_SendCommand("SDN,Microchip\r", false); // defaults
        WaitMs(100);
        if (!BT_CheckResponse(AOK)) {
            return false;
        }

        BT_SendCommand("SP,7\r", false); // defaults
        WaitMs(100);
        if (!BT_CheckResponse(AOK)) {
            return false;
        }

        BT_SendCommand("SS,C0000000\r", false); // add service
        WaitMs(100);
        if (!BT_CheckResponse(AOK)) {
            return false;
        }

        BT_SendCommand("SR,32008000\r", false); // support MLDP, enable OTA (peripheral mode is enabled by default)
        WaitMs(100);
        if (!BT_CheckResponse(AOK)) {
            return false;
        }
        BT_SendCommand("R,1\r", false); //Force reboot

        //Wait for WS status high
        StartTimer(TMR_RN_COMMS, 4000); //Start 4s timeout
        while (BT_WS == 0) {
            if (TimerDone(TMR_RN_COMMS)) //Check if timed out
            {
                return false;
            }
        }

        //Wait for end of "CMD\r\n" - we don't check for full "CMD\r\n" string because we may
        //miss some bits or bytes at the beginning while the UART starts up
        StartTimer(TMR_RN_COMMS, 4000); //Start 4s timeout
        while (UART_ReadRxBuffer() != '\n') {
            if (TimerDone(TMR_RN_COMMS)) //Check if timed out
            {
                return false;
            }
        }

        BT_SendCommand("I\r", false); // MLDP mode
        BT_SendCommand("A\r", false); // start advertising

        /* wait loop controller for power cycle/reset */
        while (true) {
            while (true) { // fast flash waiting for OTA
                ClrWdt();
                while (UART_IsNewRxData()) { //While buffer contains old data
                    UART_ReadRxBuffer(); //Keep reading until empty
                    if (!UART_IsNewRxData()) {
                        WaitMs(200);
                    }
                }
                WaitMs(200);
                SLED = !SLED;
            }

        }

    }
    BT_OTA_UPD_TRIS = 0;

    return BT_CheckResponse("MD\r\n"); //Check that we received CMD indicating reboot is done 
}
I used an iPad mini for updates with the "Bluetooth Smart Data" app.
http://microchipdeveloper.com/ble:rn4020-app-example-dfu-ota

Other methods.
http://www.microchip.com/forums/m978113.aspx
 
Last edited:

Thread Starter

KANG ZHANG

Joined Mar 15, 2018
11
I just checked the config.h version options. It checks for both 1.23 and 1.33 but all my tests were at 1.33 firmware. YMMV with 1.23
C:
// from config.h
//If defined, the RN4020's firmware version will be checked as part of initialization.
//If the version is not equal to the version specified below, the board will
//error out with the initialization error code display and not operate
#define VERIFY_RN_FW_VER
#define RN_FW_VER_MAJOR     1           //Require 1.23.5
#define RN_FW_VER_MINOR     23          //These values can be changed as needed
#define RN_FW_VER_PATCH     5

#define RN_FW_VER_MAJOR133     1
#define RN_FW_VER_MINOR133     33
#define RN_FW_VER_PATCH133     4

// from bluetooth.c
#ifdef VERIFY_RN_FW_VER
//Retrieve firmware version on module and check against the required version
//Returns true if version is correct; false if not or communication failure

uint16_t BT_CheckFwVer(void)
{
    char fpVer[20];
    char *pfpVer = fpVer;
    char strVer[100];
    char *pstrVer = strVer;
    uint16_t verMajor,
        verMinor,
        verPatch;

    //flush UART RX buffer just in case there's old data
    while (UART_IsNewRxData()) { //While buffer contains old data
        UART_ReadRxBuffer(); //Keep reading until empty
        WaitMs(100);
    }

    StartTimer(TMR_RN_COMMS, 2000);
    BT_SendCommand("v\r", false); // Get firmware version
    while (!BT_ReceivePacket(strVer)) {
        if (TimerDone(TMR_RN_COMMS)) {
            return false;
        }
    }

    //Skip to first digit
    while ((*pstrVer < '0' || *pstrVer > '9') && *pstrVer != NULL) {
        pstrVer++;
    }
    //Extract version number
    while ((*pstrVer >= '0' && *pstrVer <= '9') || *pstrVer == '.') {
        *pfpVer = *pstrVer;
        pfpVer++;
        pstrVer++;
    }
    *pfpVer = '\0';

    //Tokenize and convert to unsigned
    sscanf(fpVer, "%u.%u.%u", &verMajor, &verMinor, &verPatch);

    //Verify version number
    if ((verMajor != RN_FW_VER_MAJOR133) && (verMajor != RN_FW_VER_MAJOR)) {
        return false;
    }
    if ((verMinor != RN_FW_VER_MINOR133) && (verMinor != RN_FW_VER_MINOR)) {
        return false;
    }
    if ((verPatch != RN_FW_VER_PATCH133) && (verPatch != RN_FW_VER_PATCH)) {
        return false;
    }

    return verMinor;
}
#endif //VERIFY_RN_FW_VER
There is also a OTA update function in my code using.
C:
#define BT_OTA_UPD    PORTBbits.RB3
#define BT_OTA_UPD_TRIS    TRISBbits.TRISB3

// from bluetooth.c

    /* Jumper on DFU OTA UPDATE */
    BT_OTA_UPD_TRIS = 1; // set for jumper input
    CNPU1bits.CN7PUE = 1; // pullup for RB3
    WaitMs(2); // jumper pullup read delay, rise time is slow
    if (BT_OTA_UPD == 0) {
        BT_OTA_UPD_TRIS = 0; // set back to output
        BT_WAKE_SW = 1;
        BT_WAKE_HW = 1;
        BT_CMD = 0;

        WaitMs(100);
        BT_SendCommand("SF,2\r", false); // perform complete factory reset
        WaitMs(100);
        BT_CheckResponse(AOK);

        BT_SendCommand("SF,2\r", false); // perform complete factory reset again
        WaitMs(100);
        if (!BT_CheckResponse(AOK)) {
            return false;
        }

        BT_SendCommand("SDH,4.1\r", false); // defaults
        WaitMs(100);
        if (!BT_CheckResponse(AOK)) {
            return false;
        }
        BT_SendCommand("SDM,RN4020\r", false); // defaults
        WaitMs(100);
        if (!BT_CheckResponse(AOK)) {
            return false;
        }

        BT_SendCommand("SDN,Microchip\r", false); // defaults
        WaitMs(100);
        if (!BT_CheckResponse(AOK)) {
            return false;
        }

        BT_SendCommand("SP,7\r", false); // defaults
        WaitMs(100);
        if (!BT_CheckResponse(AOK)) {
            return false;
        }

        BT_SendCommand("SS,C0000000\r", false); // add service
        WaitMs(100);
        if (!BT_CheckResponse(AOK)) {
            return false;
        }

        BT_SendCommand("SR,32008000\r", false); // support MLDP, enable OTA (peripheral mode is enabled by default)
        WaitMs(100);
        if (!BT_CheckResponse(AOK)) {
            return false;
        }
        BT_SendCommand("R,1\r", false); //Force reboot

        //Wait for WS status high
        StartTimer(TMR_RN_COMMS, 4000); //Start 4s timeout
        while (BT_WS == 0) {
            if (TimerDone(TMR_RN_COMMS)) //Check if timed out
            {
                return false;
            }
        }

        //Wait for end of "CMD\r\n" - we don't check for full "CMD\r\n" string because we may
        //miss some bits or bytes at the beginning while the UART starts up
        StartTimer(TMR_RN_COMMS, 4000); //Start 4s timeout
        while (UART_ReadRxBuffer() != '\n') {
            if (TimerDone(TMR_RN_COMMS)) //Check if timed out
            {
                return false;
            }
        }

        BT_SendCommand("I\r", false); // MLDP mode
        BT_SendCommand("A\r", false); // start advertising

        /* wait loop controller for power cycle/reset */
        while (true) {
            while (true) { // fast flash waiting for OTA
                ClrWdt();
                while (UART_IsNewRxData()) { //While buffer contains old data
                    UART_ReadRxBuffer(); //Keep reading until empty
                    if (!UART_IsNewRxData()) {
                        WaitMs(200);
                    }
                }
                WaitMs(200);
                SLED = !SLED;
            }

        }

    }
    BT_OTA_UPD_TRIS = 0;

    return BT_CheckResponse("MD\r\n"); //Check that we received CMD indicating reboot is done
}
I used an iPad mini for updates with the "Bluetooth Smart Data" app.
http://microchipdeveloper.com/ble:rn4020-app-example-dfu-ota

Other methods.
http://www.microchip.com/forums/m978113.aspx

Thanks a lot for the info. I disabled the VERIFY_RN_FW_VER check, and I shall try it with 1.23 first before performing OTA. I found some ppl really bricked their chips ...

By the way, if I were to send my sensing ADC data through public services (such as heart rate of 0x180D), do you feel it will send correct data to some of the already developped apps (such as: https://play.google.com/store/apps/details?id=com.pribble.htc.ble.hrm&hl=en )?

I guess these apps should typically have notification enabled, while your source code didn't check for notification msg from client (correct me if I am wrong).

Also, since you are using private services for your swithces and potential meters, may I know if your client is a mobile phone, or another RN4020? Did you configure the client code yourself? Thanks a lot!
 

nsaspook

Joined Aug 27, 2009
13,272
Thanks a lot for the info. I disabled the VERIFY_RN_FW_VER check, and I shall try it with 1.23 first before performing OTA. I found some ppl really bricked their chips ...

By the way, if I were to send my sensing ADC data through public services (such as heart rate of 0x180D), do you feel it will send correct data to some of the already developped apps (such as: https://play.google.com/store/apps/details?id=com.pribble.htc.ble.hrm&hl=en )?

I guess these apps should typically have notification enabled, while your source code didn't check for notification msg from client (correct me if I am wrong).

Also, since you are using private services for your swithces and potential meters, may I know if your client is a mobile phone, or another RN4020? Did you configure the client code yourself? Thanks a lot!
I've not had a problem with bricked chips using that code to up or downgrade but YMMV.

It's unlikely my ADC code transmit data format would display correctly using the heart beat service but it's a simple matter to reformat it in a compatible way for other apps.

This is the custom mobile phone client for this code base.

Android Studio source: https://github.com/nsaspook/blecm_new_app
 

Thread Starter

KANG ZHANG

Joined Mar 15, 2018
11
I've not had a problem with bricked chips using that code to up or downgrade but YMMV.

It's unlikely my ADC code transmit data format would display correctly using the heart beat service but it's a simple matter to reformat it in a compatible way for other apps.

This is the custom mobile phone client for this code base.

Android Studio source: https://github.com/nsaspook/blecm_new_app
Is the analog input bar (bottom of your phone) displaying the potential meter readings you send from PIC?
 

nsaspook

Joined Aug 27, 2009
13,272
Is the analog input bar (bottom of your phone) displaying the potential meter readings you send from PIC?
Yes, that's the analog signal readback. The update rate is set in the BLE C code config.h

On the app it's a simple 'Progress Bar'.

Java:
/** * Update persisted BLE Device Status and POST status update to the server * * @param potentiometer String value that contains the potentiometer value returned from the BLE Device */
private void updatePotentiometerChanges(String potentiometer){
// Get Latest status and update itStatus status = new Status();
if(DataStore.getBleStatus(BleService.this) != null){
status = DataStore.getBleStatus(BleService.this);
}

status.setPotentiometer(Integer.valueOf(potentiometer));
status.setDeviceType(ResponseModel.getDeviceType(modelNumber));
status.setUuid(DataStore.getBleDeviceUuid(BleService.this));

DataStore.persistBleStatus(BleService.this, status);
sendBroadcast(ACTION_UPDATE_UI);

if(!DataStore.getAwsAmi(BleService.this).isEmpty())
Api.postStatus(BleService.this, status);
}
 
Last edited:

Thread Starter

KANG ZHANG

Joined Mar 15, 2018
11
Yes, that's the analog signal readback. The update rate is set in the BLE C code config.h

On the app is a simple 'Progress Bar'.

Java:
/** * Update persisted BLE Device Status and POST status update to the server * * @param potentiometer String value that contains the potentiometer value returned from the BLE Device */private void updatePotentiometerChanges(String potentiometer){
// Get Latest status and update itStatus status = new Status();
if(DataStore.getBleStatus(BleService.this) != null){
status = DataStore.getBleStatus(BleService.this);
}

status.setPotentiometer(Integer.valueOf(potentiometer));
status.setDeviceType(ResponseModel.getDeviceType(modelNumber));
status.setUuid(DataStore.getBleDeviceUuid(BleService.this));

DataStore.persistBleStatus(BleService.this, status);
sendBroadcast(ACTION_UPDATE_UI);

if(!DataStore.getAwsAmi(BleService.this).isEmpty())
Api.postStatus(BleService.this, status);
}
Great! I shall try it out once the board is avaible.
 

Thread Starter

KANG ZHANG

Joined Mar 15, 2018
11
Hi, just one question regarding the code. You delcared the buffer type for ADC over SPI using below code.

Code:
union adc_buf_type {
    uint32_t ld;
    uint8_t bd[4];
    struct A_data map;
};

Code:
typedef struct A_data {
    uint32_t dummy12 : 12; // dummy space for adc data
    uint32_t nullbits : 2;
    uint32_t index : 3; //adc channel select
    uint32_t single_diff : 1;
    uint32_t start_bit : 1;
    uint32_t dummy8 : 8;
    uint32_t finish : 1;
    uint32_t in_progress : 1;
} A_data;
While when you send command to MCP3208 over SPI, the code is

Code:
if (!SPI_IsTxData()) {
            adcData.mcp3208_cmd.map.single_diff = 1;
            adcData.mcp3208_cmd.map.index = adcData.chan;
            SPI_ClearBufs(); // dump the spi buffers
            SPI_WriteTxBuffer(adcData.mcp3208_cmd.bd[2]);
            SPI_WriteTxBuffer(adcData.mcp3208_cmd.bd[1]);
            SPI_WriteTxBuffer(adcData.mcp3208_cmd.bd[0]);
            SPI_CS0 = 0; // select the ADC
            SPI_Speed(0);
            SPI_TxStart();
        }
Are you using MPLABXC16 compiler to compile your code? According to the compliers' guide, the allocation of a integer should not cross a storage boundary. In you code, is the typedef struct A_data bit-field defination be able to cross storage boundary?

According to the MCP3208 spec, the first bit it receives should be the start bit. I just worried if your SPI_WriteTxBuffer() really writes the correct start bit, single_diff selection bit and adc channel index.
 
Top