Setting outputs on GPIO in code

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a port pin set in START as an input to an ADC, but in code I also need to set that same pin temporarily as an output with push pull set high.  The code I'm using so far is this:

 

gpio_set_pin_direction(AD0, GPIO_DIRECTION_OUT); //set direction to output
gpio_set_pin_level(AD0, true); //set output level hi

//gpio_set_pin_function(const uint32_t pin, uint32_t function)  //the function define I don't know what to set to

 

I'm trying to set the pin to a push pull style output with a good drive capability.  The output does not go hi and I suspect its because I need to add that last line for set_pin_function to set it to push/pull or something.  But I can't find any listing for the names (defines) I can use for the second parameter.  It's for a SAMS70 chip

 

Also, when I want to go back to using the AD, is this the only lines I need (i.e. analog)?

 

gpio_set_pin_direction(AD0, GPIO_DIRECTION_OFF); //pin in low power state

 

This topic has a solution.
Last Edited: Thu. May 20, 2021 - 06:23 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Try make a small temporary project, set this pin to push-pull in it and see in code preview what code is used by START to make this setting.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for the suggestion.  I discovered that what was happening was this line was forcing the gpio to go into an input mode even though I used the pin commands to make it go to output mode

 

adc_async_enable_channel(&ADC_0, CONF_ADC_0_CHANNEL_7);

 

So to solve the problem I do the following when I want to use the pin in output mode:

 

adc_async_disable_channel(&ADC_0, CONF_ADC_0_CHANNEL_6); //disable the channel so we can use output mode
gpio_set_pin_direction(AD0, GPIO_DIRECTION_OUT); // set output mode
gpio_set_pin_level(AD0, true); //set output to hi
gpio_set_pin_function(AD0, GPIO_PIN_FUNCTION_OFF); //not sure this is needed

 

Then to go to input mode so the AD can use the pin I do this:

 

adc_async_enable_channel(&ADC_0, CONF_ADC_0_CHANNEL_7); //enable the pin as an input to the AD
gpio_set_pin_direction(AD1, GPIO_DIRECTION_OFF); //set pin as analog input

 

This all seems to work. 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Nafai wrote:
This all seems to work. 

Thanks for reporting back.

 

Please mark the solution - see Tip #5 in my signature, below (also note Tip #1):

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...