Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 11 months ago.
about using GPIO for digital output
Hi there, I finally get my nucleo-F446Re working after long time trying to get the driver installed. I run few examples in this site, working good. I am now trying to write a very simple code to output a digital HIGH / LOW to a specific pin. I am not sure how to do that since I didn't find a name mapping for each pin to the library, so I copy some code online and try that, but it doesn't work. To make it simple, I am trying to control the PA3 (pin 1) on the board, flip the status every 5 seconds, so I have
#include "mbed.h" PortOut myIOs(PortA, PA_3); int main() { while(1) { myIOs = 1; wait(5); // 5s myIOs = 0; wait(5) // 5s; } }
But I didn't get any output to the scope. What's wrong with this code? Thanks.
1 Answer
8 years, 11 months ago.
A few issues:
1) The format for the PortOut is not correct. Please review this webpage for more details:
https://developer.mbed.org/handbook/PortOut
2) The use of PA_2 and PA_3 are not recommended as these 2 lines are mapped to the local UART which is used for debugging. We found this issue yesterday on our STM32F401RE Nucleo board which also failed to toggle the PA_2 and PA_3 pins. After reviewing the Nucleo schematic, the results are logical and cannot be used unless you change the solder pads under the Nucleo board.
See section 5.7 of this document for more details:
3) Instead, consider the following:
#include "mbed.h" DigitalOut myIOs(PB_8); // review the results on PB_8 int main() { while(1) { myIOs = 1; wait(5); // 5s myIOs = 0; wait(5) // 5s; } }
https://developer.mbed.org/handbook/DigitalOut
Hello Kim. As some general advice - do you plan to use UART feedback with the host PC ? That is, are you printing out any data and using the host PC to view this data ? If yes, then you should consider some other port pins and do not use PA2 & PA3 since they are mated with the UART connection for code development.
Again as I do not know the specifics of your program you are porting, you will have to review the code to see if the author is writing to the entire port to function correctly or writing a bit at a time. You are correct in that DigitalOut will allow for writing an output value to a single port pin only. PortOut can write to the entire port block and the MASK will allow you to mask (prevent) the changing of the bits you wish to leave as-is. If the MASK bit is 0 then that port bit will not change, if the MASK bit is 1 then that port bit is able to change with a single write. The STM32F series processors are very complex beasts to initialize so the use of MBED is most welcomed to simplify the tasks. As always, suggest you experiment and take simple steps to validate which port pins are suitable for your project. Moving from STM32F1 to STM32F4 should be fine and you will gain speed at the very least with this move. As the Nucleo boards are so low cost, consider to buy another and make the modifications noted in my last post (section 5.7) which are solder pads underneath the Nucleo board. Once you allow for PA2 & PA3 to be mapped to the male pin headers, you are free to continue with your project using these port pins but will lose the UART interface to the host computer.
Thanks a lot. Your code works. I have few questions about the port. I still don't understand what's the main difference between DigitalOut and PortOut, is digital port only control 1 pin while portout could be more than 1?
I have a code created by someone else to use STM32F1 library to control STM32F1xx micro's GPIO to output some signal. I want to port them to STM32F4xx with mbed. I wonder if it is the same thing to use DigitalOut to replace GPIO? I need to use all pins in CN9 and pin 1 to 6 on CN5, but if pin1 (PA3) and pin2 (PA2) cannot be used so I have to redesign my hardware connection.
posted by 29 Jan 2016