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

GPIOs pins to be set to send command

Hi everyone,

for a project, we need to interface an analog sensor which is not using standard protocols like I2C or SPI.

Instead, it uses 6 pins to receive commands. So, if I need to send for example command 0x24, I need to set this value on the 6 GPIOs. 

I found an older discussion where a solution to this problem was proposed using some pins (discussed here) but since I am 

forced by design to use pins P1.01-P1.06 I am getting a warning from Segger Embedded Studio compiler "left shift count >= width of type".

My code is simply the following

void writeCommandPort(uint8_t value)
{
    uint32_t pin_mask = 0xF << SENSOR_FIRST_PIN;
    NRF_GPIO->OUT = (NRF_GPIO->OUT & ~pin_mask) | (value << SENSOR_FIRST_PIN);  
}


//Where SENSOR_FIRST_PIN = NRF_GPIO_PIN_MAP(1,1) 

Can someone help me understanding what I am doing wrong?


Thanks a lot!

Parents
  • Tried your code, but couldn't get the warning. Maybe you can share a small SES project + main.c I can run on a nRF52840?

    Kenneth

  • Hello,

    this code is part of a pretty large project and I cannot share it.
    But I found the reason to the warning: since SENSOR_FIRST_PIN = NRF_GPIO_PIN_MAP(1,1), this means that

    the pin_mask is = 33, so the pin_mask value falls out of the possible range for a uint32_t value....

    The solution I found is to use the following code

    void writeCommandPort(uint8_t value)
    {
        uint64_t pin_mask = 0xFLL << SENSOR_FIRST_PIN;
        NRF_GPIO->OUT = (NRF_GPIO->OUT & ~pin_mask) | ((uint64_t)value << SENSOR_FIRST_PIN);  
    }
    
    
    //Where SENSOR_FIRST_PIN = NRF_GPIO_PIN_MAP(1,1) 

    Essentially, I used 64 bits value for the mask, since the pins I am going to modify are > 32. 

    I still need to test it, so I would appreciate any feedback if you think there is something wrong with this solution,

    Thanks

  • Ah.. or presuming you are using port1 just something like:

    #define SENSOR_FIRST_PIN_PORT1 1
    
    void writeCommandPort1(uint8_t value)
    {
        uint32_t pin_mask = 0xF << SENSOR_FIRST_PIN_PORT1;
        NRF_P1->OUT = (NRF_P1->OUT & ~pin_mask) | (value << SENSOR_FIRST_PIN_PORT1);  
    }

Reply Children
No Data
Related