This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Cant change registers via pionters on nrf53

Hi, I want write code on rust, but build system of nrf sdk for nrf5340 too hard for creating bindings, so I decide to create own HAL on rust and embed it in C with BLE stack on C. I start write test code on C for blinking leds on nrf5340dk. When I write this code on C, it just dont work, but when I do the same steps on GDB, all work fine. I also noticed that when I write value at registers in code, all operations just ignore.

static void turn_on_all_leds()
{
    volatile uint32_t *gpio_base = (uint32_t *) 0x50842500UL;
    volatile uint32_t *gpio_out_clr = (uint32_t *) (gpio_base + 0x00CUL);
    volatile uint32_t *gpio_dir_set = (uint32_t *) (gpio_base + 0x018UL);

    *gpio_dir_set = 0xf0180000UL;
    *gpio_out_clr = 0x00180000UL;
}


I read Reference manual and think that SPU may prevent my work with registers, but I dont know, How I should turn off SPU correct or how I can setup sdk for it, or may be my problem related to something else. When I try get some information about setup SPU at GDB, it tell that it cant access memory. How I can work with all registers whenever I want?

Parents Reply
  • Hello again!

    Apologies for the delayed answer here. I reached out to some colleagues and they pointed out a mistake in your code. When you deal with pointer types the arithmetic take the pointer type into account. As an example, if you have a pointer to an array of uint32_t, and you want to access the next element in the array you have to increase the pointer by 4. In this case this means that your gpio_out_clr and gpio_dir_set pointers end up with the wrong value. This can be fixed by casting gpio_base to and int before doing the addition. Like this:

    volatile uint32_t *gpio_base = (uint32_t *) 0x50842500UL;
    volatile uint32_t *gpio_out_clr = (uint32_t *) ((int)gpio_base + 0x00CUL);
    volatile uint32_t *gpio_dir_set = (uint32_t *) ((int)gpio_base + 0x018UL);
    
    *gpio_dir_set = 0xf0180000UL;
    *gpio_out_clr = 0x00180000UL;


    By the way, one of the developers pointed out that you can use the standalone nRFX driver set as a starting point, instead of implementing your own HAL.

    Best regards,
    Carl Richard

Children
Related