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

Set Pin to high

Hello,

I want to set a GPIO Pin to high. Therefore I wrote the code below.

But when the software is written onto the nrf52811, I can not messure any voltage.

I do not want to use any library or external file for this.

//start of code

int main()
{

unsigned int base = 0x50000000; //base adress of GPIO
unsigned int out = 0x504; //offset
unsigned int outAdress = base + out;// adress of OUT Register

unsigned int write = 1; //set first Pin to high
unsigned int *point = outAdress; //Pointer on outAdress

*point = write; //set outAdress to 0000 0000 0000 0000 0000 0000 0000 0001

while(1)
{
    //do nothing
}
}

//end of code

Thank you for your help.

Parents
  • Hi 

    I do not want to use any library or external file for this.

    May I ask what you are trying to do with the device?

    Unless all you need to do is toggle some pins then not using any external files will make things quite complicated ;)

    As for your code, by default all pins are set as inputs, and you need to write to the DIR register to enable a pin as an output. Can you try this and see if it works better?

    Best regards
    Torbjørn

  • Hi,

    thank you for your reply. I basically want to understand how these registers work, how they are programmed. I want to do some general tests - get into this stuff. 

    As you proposed, I have changed the code. But it still does not work.

    Also I wonder if I am even able to run the chip only with this main.c file. Do you know if there has to be some files, that must be written to chip to operate anything on it?

    The extended code:

    //start of code

    int main()
    {

    unsigned int base = 0x50000000; //base adress of GPIO
    unsigned int outOffset = 0x504; //offset of out
    unsigned int dirOffset = 0x514; //offset of dir

    unsigned int dirAdress = base + dirOffset;// adress of DIR Register
    unsigned int outAdress = base + outOffset;// adress of OUT Register

    unsigned int write = 1;

    unsigned int *dir = dirAdress; //Pointer on dirAdress
    unsigned int *out = outAdress; //Pointer on outAdress

    *dir = write; //set outAdress to 0000 0000 0000 0000 0000 0000 0000 0001
    *out = write; //set outAdress to 0000 0000 0000 0000 0000 0000 0000 0001

    while(1)
    {

    }
    }

    //end of code

  • Hi 

    As a colleague of mine pointed out, in order to write to memory mapped registers it is important to define the variables as volatile. Otherwise the compiler could end up optimizing out your writes (or writing them to registers only), since the values you write don't get used anywhere else in the code. 

    The quickest way to 'manually' write directly to a memory mapped peripheral register is follows:

    // Setting the NRF_P0->DIR register to 1
    *((volatile int *)0x50000514) = 1;
    // Setting the NRF_P0->OUT register to 1
    *((volatile int *)0x50000504) = 1;

    Best regards
    Torbjørn

  • Hi

    I tested this code:

    //start of code

    int main() //first line

    {

    // Setting the NRF_P0->DIR register to 1
    *((volatile int *)0x50000514) = 1;
    // Setting the NRF_P0->OUT register to 1
    *((volatile int *)0x50000504) = 1;

    while(1)

    {

    }

    } //last line

    //end of code

    But it does not work. So I wonder if I am doing something wrong at building.

    So I have copied the example receiver from the SDK: examples\peripheral\radio\receiver

    The I opened the file receiver_pca10040.emProject with Segger Embedded Studio

    In this file I changed the main.c file to the code above.

    Then I clicked on Build -> build _pca10040

    Afterwards I upload the receiver_pca10040.hex file, I got in receiver\pca10040\blank\ses\Output\Release\Exe, which has the current date.

    For uploading I use nRF Connect v3.6.1

    Is their something wrong in my Method?

    Best regards
    Tobias

  • Hi Tobias

    What kind of hardware are you trying to program?

    Is it a standard development kit, or a custom board?

    Also, have you tried to flash the example directly from the SES interface, instead of using nRF Connect?

    The pca10040 project is designed for the nRF52832 device, not the nRF52811, so if you are targetting the nRF52811 this might be the problem. 

    Best regards
    Torbjørn

  • Hi,

    I am programming the ISP1907-LL with the Evalutation Board.

    I may found the problem:
    The "ble_app_uart_pca10040e_s112.hex" file, that comes with the SDK and works fine, is unequal to the file, which I get when I click on build in Segger Embedded Studio (wihtout changing code).

    Debugging from Segger Embedded Studio did also not work.

    Best regards
    Tobias

  • Hi Tobias

    Thanks for sharing the details on your board. 

    Your earlier code is trying to manipulate P0.00, but according to the ISP1907-LL documentation P0.00 is connected to the LF crystal, and can not be used as GPIO. 

    Only a subset of the available GPIO's on the part are brought out on the module, including P0.03, P0.04 and P0.05. 

    Are you able to try one of these pins instead and see if it works?

    A quick trick to manipulate P0.03 for instance is to write to the registers as such:

    // Setting P0.03 to output
    *((volatile int *)0x50000514) = (1 << 3);
    // Setting P0.03 high
    *((volatile int *)0x50000504) = (1 << 3);

    Best regards
    Torbjørn

Reply
  • Hi Tobias

    Thanks for sharing the details on your board. 

    Your earlier code is trying to manipulate P0.00, but according to the ISP1907-LL documentation P0.00 is connected to the LF crystal, and can not be used as GPIO. 

    Only a subset of the available GPIO's on the part are brought out on the module, including P0.03, P0.04 and P0.05. 

    Are you able to try one of these pins instead and see if it works?

    A quick trick to manipulate P0.03 for instance is to write to the registers as such:

    // Setting P0.03 to output
    *((volatile int *)0x50000514) = (1 << 3);
    // Setting P0.03 high
    *((volatile int *)0x50000504) = (1 << 3);

    Best regards
    Torbjørn

Children
  • Hi,

    I may found the problem:
    The "ble_app_uart_pca10040e_s112.hex" file, that comes with the SDK and works fine, is unequal to the file, which I get when I click on build in Segger Embedded Studio (wihtout changing code).

    Debugging from Segger Embedded Studio did also not work.

    Best regards
    Tobias

  • Hi Tobias

    To target the nRF52811 you should use the pca10056e project, not the pca10040e. 

    Most likely the reason the hex file is different is that the hex file stored in the project contains both the SoftDevice and the application, while the hex file you build only contains the application. If you program the project directly in Segger Embedded Studio this doesn't matter, since the SoftDevice will be flashed automatically in addition to the application hex. 

    Also, the ble_app_uart example uses the UART, which might be a problem on your board since it doesn't have anything connected to those pins. 

    Could you try the ble_app_eddystone example instead, and use the nRF Connect app for Android/iOS to see if you can see the advertising packets?

    Remember to use the pca10056e project, and try to program through the Segger interface. 

    Best regards
    Torbjørn

  • Hi Torbjørn,

    I could successfully set Pin 3 to HIGH. Thank you very much.

    I used a pca10056e project, not ble_app_eddystone due to this error: "cannot find ../../../../../../external/micro-ecc/nrf52nf_armgcc/armgcc/micro_ecc_lib_nrf52.a: No such file or directory". But another 10056e project file, without errors, worked fine.

    Best regards
    Tobias

  • Hi Tobias

    That is great news Slight smile

    For licensing reasons we are not allowed to distribute the micro_ecc library, so if you want to build an example that requires it you have to install this library separately, as described here.

    Best regards
    Torbjørn 

Related