This post was last edited by fxyc87 on 2021-1-28 11:05
Last time I posted a link
I talked about how to compile the program, but it didn't work. Then I talked about how to play with MicroPython.
Later, I didn't give up and tried again, and finally succeeded.
The following is the process:
1. First download SDK, GIT, VS2019 C++ tools, GCC compiler. The specific method is in the official PDF. Set the system environment variables, preferably absolute addresses, as shown in the figure:
2. Download pico-project-generator , which is a GUI CMAKE tool. After downloading, there is this file pico_project.py in its main directory
It cannot be executed directly, and a parameter is required. It is recommended to create a batch file gui.bat in its directory with the following content:
python pico_project.py --gui
In this way, you can directly execute gui.bat in the future, and the interface is as follows:
3. After creating the project location, modify your program. My program is as follows:
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/uart.h"
#include "hardware/gpio.h"
#include "hardware/divider.h"
// UART defines
// By default the stdout UART is `uart0`, so we will use the second one
#define UART_ID uart1
#define BAUD_RATE 9600
// Use pins 4 and 5 for UART1
// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments
#define UART_TX_PIN 4
#define UART_RX_PIN 5
// GPIO defines
// Example uses GPIO 2
#define LED 25 //led 引脚
int main()
{
stdio_init_all();
// Set up our UART
uart_init(UART_ID, BAUD_RATE);
// Set the TX and RX pins by using the function select on the GPIO
// Set datasheet for more information on function select
gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
// GPIO initialisation.
// We will make this GPIO an input, and pull it up by default
gpio_init(LED);
gpio_set_dir(LED, GPIO_OUT);
gpio_put(LED,1); //打开LED
// Example of using the HW divider. The pico_divider library provides a more user friendly set of APIs
// over the divider (and support for 64 bit divides), and of course by default regular C language integer
// divisions are redirected thru that library, meaning you can just use C level `/` and `%` operators and
// gain the benefits of the fast hardware divider.
int32_t dividend = 123456;
int32_t divisor = -321;
// This is the recommended signed fast divider for general use.
divmod_result_t result = hw_divider_divmod_s32(dividend, divisor);
printf("%d/%d = %d remainder %d\n", dividend, divisor, to_quotient_s32(result), to_remainder_s32(result));
// This is the recommended unsigned fast divider for general use.
int32_t udividend = 123456;
int32_t udivisor = 321;
divmod_result_t uresult = hw_divider_divmod_u32(udividend, udivisor);
printf("%d/%d = %d remainder %d\n", udividend, udivisor, to_quotient_u32(uresult), to_remainder_u32(uresult));
puts("Hello, world!");
return 0;
}
Most of it was automatically generated, I only turned on the LED light and left the rest alone.
You can use VS CODE or Notepad to modify the program
The LED indicator is on the grounded GP25, see the drawing:
4. Compile
After numerous tests in yesterday's post, I failed. Today, I accidentally tried a command and it was able to compile.
The official command is to use the nmake command directly, but I can't execute it directly
Then I used the absolute command, C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.28.29333\bin\HostX64\x64\nmake.exe
It still reported a bunch of errors. Later, I considered using the ARM version and tried C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.28.29617\bin\Hostx86\x86\nmake.exe
Still doesn't work. I accidentally saw a DLL in the nmake directory, C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.28.29333\bin\HostX64\x64\2052\nmakeui.dll
Then I tried adding the -ui command, and a miracle happened. It could be compiled, and the elf file, hex file, and bin file were successfully compiled.
Note: As for why adding -ui works, I haven't checked it yet. I'll look for the help of namke later.
Note that it needs to be executed in the build directory under the project directory, such as ProjectName\build. The generated files are as shown below:
5. Conversion
This target board only supports uf2 files. I was planning to write a conversion program myself using c#, but I decided to use the official one.
There is a tool pico-sdk\tools\elf2uf2 in this directory of the official SDK, but it only has source code.
I created a C++ project with VS2019 and compiled it once. It was successful. The following is the compiled EXE. You can use it directly.
elf to uf2.exe, usage, command line input elf to uf2.exe original file.elf target file.uf2
elf转换到uf2.zip
(9.23 KB, downloads: 14)
6. Download
This is relatively simple. Press and hold the button before powering on, then plug in the USB to power on, and directly drag the UF2 file into the target disk. Then you will find that the LED light miraculously becomes brighter.
Isn’t it amazing?
Reason for modification: Supplementary pictures