This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Converting code for micro:bit v1 (nRF51822) to micro:bit v2 (nRF52833)

1_gpio.zip2_uart.zip3_gpiote.zip4_twi.zip

Hello,

I am a research assistant at the Norwegian University of Science and Technology. We have in the past been using the micro:bit v1 for our practical labs which purpose is to introduce the students to basic C programming. For that, we have been using GCC ARM as our compiler, nrfjprog to flash compiled code to the micro:bit, and lastly JLink to communicate with the micro:bit. The latter removes the DAL by just simply drag and drop of the hex file from Segger.com.

Our problem is that for this year, we have received the second version of the micro:bit, which uses a nrf52833. I was therefore wondering if there is someone who could help me on the way to replicate the simple assignments that we have earlier created for the v1 for the new v2?

I attached the solutions for micro:bit v1 as a zip file, with the Makefile required to run it. The code runs by going into each folder, and then compile with "make", before one can flash over the compiled program with "make flash". Here is a small description on what each code does:

  1. 1_GPIO programs the micro:bit v1 to turn on and off the LEDs in the matrix with the A/B button.
  2. 2_UART programs the micro:bit v1 for two ways communication. In our lab, we use picocom.
  3. 3_GPIOTE programs the micro:bit v1 to define events (button B pressed),  and 3 tasks (turn off and on the voltage to the three LEDs matrices)
  4. 4_TWI programs the micro:bit v1 to connect to the gyro so that the LED matrix can be used to describe the orientation of the micro:bit.

Ultimately, what we desire is to have something similar. Ideally a Makefile, a .build_system folder, and .c/h files.

What I have done untill now is to recreate the first assignments with GPIO, but the micro:bit does not seem to respond. I tried to install the new firmware for the micro:bit v2 for JLink but I suspect I have to modify the .build_system folder and the Makefile (I suspect one only needs to change the nrf51->nrf52 and set the cortex to 4?) accordingly.

Best wishes,

Kiet

Parents
  • Hi,

    A few tips:

    1. You should replace the system and startup files with the system_nrf52833.c/h and gcc_startup_nrf52833.S, not with *nrf52* files (these are for nRF52832). You can find the files in the latest MDK.
    2. Change the CPU type to Cortex M4 (and optionally enable hard float support, but that is likely not needed for you projects).
    3. Increase the Flash and RAM lengths in your linker script to match the sizes of the nRF52833.
    4. Verify that all used peripheral are located at the base address defined in your applications, see Instantiation. You should also check the register documentation for each peripheral that is used, to make sure no registers have been moved or removed. This step would have been simpler if you had used the header files from the MDK (e.g. nrf52833.h) instead of defining the registers in the applications.
    5. Check that the GPIOs that are used by application match the schematics of the micro:bit 2. The nRF52833 have two GPIO ports, so some GPIOs needs remapping to match the connected peripherals.

    Hope this helps you run your examples on the micro:bit v2.

    Best regards,
    Jørgen

  • Hi Kiet,

    NTNU_TTK4235 said:
    When I replace my system and startup files with the related nrf52833 variant, I have some dependencies. Do you know which ones are relevant for my use case (only basic manipulation of GPIO and some of the peripherals). I see that system_nrf52833.c is including nrf52.c, which again includes several nrf and nrf52 specific libraries: do you think is needed?

    The system files makes sure that all relevant erratas are applied, and applies various compile-time configurations, like NFCT pins, RESET pin, etc. These may not all be strictly needed for your projects, but it may be some work to remove the dependencies.

    NTNU_TTK4235 said:
    Just to verify since I am relatively new to embedded: I looked at the memory mapping of both the nRF52833 and the nRF51822, and it seems to be that they are similar where both have->
    1. MEMORY
    2. {   FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x40000
    3.     RAM (rwx) : ORIGIN =   0x20000000, LENGTH = 0x400
    4. }

    Yes, to be able to utilize the full flash and RAM of nRF52833, you should set this to:

    MEMORY
    {
      FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x80000
      RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x20000
    }

    I'm not sure if you erased the micro:bit 2 before programming it or not, but I would assume you are from your initial post. If not, there may be bootloader(s), etc. present, which may require you to modify the linker script to not overlap this.

    NTNU_TTK4235 said:
    So the difference between P1.xx and P0.xx is if the base adress is at 0x50000000 vs 0x50000000 and 0x50000300 right? 

    Yes, P0 is at base address 0x50000000 and P1 is at base address 0x50000300.

    Best regards,
    Jørgen

Reply
  • Hi Kiet,

    NTNU_TTK4235 said:
    When I replace my system and startup files with the related nrf52833 variant, I have some dependencies. Do you know which ones are relevant for my use case (only basic manipulation of GPIO and some of the peripherals). I see that system_nrf52833.c is including nrf52.c, which again includes several nrf and nrf52 specific libraries: do you think is needed?

    The system files makes sure that all relevant erratas are applied, and applies various compile-time configurations, like NFCT pins, RESET pin, etc. These may not all be strictly needed for your projects, but it may be some work to remove the dependencies.

    NTNU_TTK4235 said:
    Just to verify since I am relatively new to embedded: I looked at the memory mapping of both the nRF52833 and the nRF51822, and it seems to be that they are similar where both have->
    1. MEMORY
    2. {   FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x40000
    3.     RAM (rwx) : ORIGIN =   0x20000000, LENGTH = 0x400
    4. }

    Yes, to be able to utilize the full flash and RAM of nRF52833, you should set this to:

    MEMORY
    {
      FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x80000
      RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x20000
    }

    I'm not sure if you erased the micro:bit 2 before programming it or not, but I would assume you are from your initial post. If not, there may be bootloader(s), etc. present, which may require you to modify the linker script to not overlap this.

    NTNU_TTK4235 said:
    So the difference between P1.xx and P0.xx is if the base adress is at 0x50000000 vs 0x50000000 and 0x50000300 right? 

    Yes, P0 is at base address 0x50000000 and P1 is at base address 0x50000300.

    Best regards,
    Jørgen

Children
  • Hi Jørgen,

    Thank you so much for your answer. To that I implemented this basic skeleton code, but I seems to not be able to compile it and I am clueless. The skeleton code is made by using your aforementioned tips where I changed the linker (change memory spacing), makefile (to include Cortex M4), and included all of the libraries from the MDK.

    This is the error message I get from compiling main.c while including all of the dependencies. It seems that I must define the device somehow (maybe through my makefile or my linker?)

    Sorry for the basic questions, and thank you so much in advance (I attached my current progress as zip if you want to try to recreate my error messages),

    Best wishes,

    Kiet

    In file included from .build_system/system_nrf52.c:28,
                     from .build_system/system_nrf52833.c:35:
    .build_system/nrf.h:197:6: error: #error "Device must be defined. See nrf.h."
      197 |     #error "Device must be defined. See nrf.h."
          |      ^~~~~
    In file included from .build_system/system_nrf52.c:29,
                     from .build_system/system_nrf52833.c:35:
    .build_system/nrf_peripherals.h:67:6: error: #error "Device must be defined. See nrf.h."
       67 |     #error "Device must be defined. See nrf.h."
          |      ^~~~~
    In file included from .build_system/system_nrf52833.c:35:
    .build_system/system_nrf52.c:67:6: error: #error "A supported device macro must be defined."
       67 |     #error "A supported device macro must be defined."
          |      ^~~~~
    .build_system/system_nrf52.c: In function 'nvmc_wait':
    .build_system/system_nrf52.c:74:12: error: 'NRF_NVMC' undeclared (first use in this function)
       74 |     while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
          |            ^~~~~~~~
    .build_system/system_nrf52.c:74:12: note: each undeclared identifier is reported only once for each function it appears in
    .build_system/system_nrf52.c:74:31: error: 'NVMC_READY_READY_Busy' undeclared (first use in this function)
       74 |     while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
          |                               ^~~~~~~~~~~~~~~~~~~~~
    .build_system/system_nrf52.c: In function 'nvmc_config':
    .build_system/system_nrf52.c:81:5: error: 'NRF_NVMC' undeclared (first use in this function)
       81 |     NRF_NVMC->CONFIG = mode << NVMC_CONFIG_WEN_Pos;
          |     ^~~~~~~~
    .build_system/system_nrf52.c:81:32: error: 'NVMC_CONFIG_WEN_Pos' undeclared (first use in this function)
       81 |     NRF_NVMC->CONFIG = mode << NVMC_CONFIG_WEN_Pos;
          |                                ^~~~~~~~~~~~~~~~~~~
    make: *** [Makefile:35: default] Error 1
    1362.1_gpio.zip

  • Hi,

    You need to defined the chip variant as a preprocessor symbol. This can be done by adding it to the CFLAGS variable in the Makefile:

     

    CFLAGS += -DNRF52833_XXAA

    When this flag is set, the correct headers will be included in the build. You are missing a few in your .build_system directory:

    From MDK (can be found in nRF5_SDK_17.1.0_ddde560\modules\nrfx\mdk):

    • nrf51_to_nrf52.h
    • nrf52833.h
    • nrf52833_bitfields.h
    • nrf52833_peripherals.h
    • nrf52_to_nrf52833.h

    From ARM-CMSIS (can be found in nRF5_SDK_17.1.0_ddde560\components\toolchain\cmsis\include):

    • cmsis_compiler.h
    • cmsis_gcc.h
    • cmsis_version.h
    • core_cm4.h
    • mpu_armv7.h

    With the addition of these files and the preprocessor symbol, your application builds successfully for me.

    Best regards,
    Jørgen

Related