Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use GE language in minimal hardware

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use the go language in the very small hardware". In the daily operation, I believe that many people have doubts about how to use the go language in the very small hardware. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use the go language in the very small hardware"! Next, please follow the editor to study!

Hardware part

STM32F030F4P6 left a deep impression:

CPU: Cortex M0 48 MHz (minimum configuration, only 12000 logic gates)

RAM: 4 KB

Flash: 16 KB

ADC, SPI, I2C, USART and several timers

All of these are packaged in TSSOP20. As you can see, this is a very small 32-bit system.

Software part

If you want to know how to program with Go on this development board, you need to read the hardware specification manual over and over again. You have to face the fact that there is little chance of supporting Cortex-M0 in the Go compiler. Moreover, this is only the first problem to be solved.

I'll use Emgo, but don't worry, you'll see later how it makes Go work as much as possible on such a small system.

Before I got this development board, there was no support for F0 MCU under the stm32/hal series. After a brief study of the reference manual, I found that the STM32F0 series is a reduced version of STM32F3, which makes it easier to develop on the new port.

If you want to follow the steps in this article, you need to install Emgo first

Cd $HOMEgit clone https://github.com/ziutek/emgo/cd emgo/egcgo install

Then set the environment variable

Export EGCC=path_to_arm_gcc # eg. / usr/local/arm/bin/arm-none-eabi-gccexport EGLD=path_to_arm_linker # eg. / usr/local/arm/bin/arm-none-eabi-ldexport EGAR=path_to_arm_archiver # eg. / usr/local/arm/bin/arm-none-eabi-ar export EGROOT=$HOME/emgo/egrootexport EGPATH=$HOME/emgo/egpath export EGARCH=cortexm0export EGOS=noosexport EGTARGET=f030x6

More detailed instructions can be found on the Emgo website.

Make sure egc is in your PATH. You can use go build instead of go install and copy egc to your $HOME/bin or / usr/local/bin.

Now, create a new folder for your first Emgo program, and then copy the linker script in the example:

Mkdir $HOME/firstemgocd $HOME/firstemgocp $EGPATH/src/stm32/examples/f030-demo-board/blinky/script.ld. Most basic procedure

Create a basic program in the main.go file:

Package main func main () {}

There is no problem with file compilation:

$egc$ arm-none-eabi-size cortexm0.elf text data bss dec hex filename 7452 172 104 7728 1e30 cortexm0.elf

The first compilation may take some time. The compiled binary takes up 7624 bytes of Flash space (text + data). For a program that does nothing, it takes up a lot of space. There are 8760 bytes left, which can be used to do something useful.

Try the traditional "Hello, World!" Program:

Package main import "fmt" func main () {fmt.Println ("Hello, World!")}

Unfortunately, this time it turned out a little bad:

$egc/usr/local/arm/bin/arm-none-eabi-ld: / home/michal/P/go/src/github.com/ziutek/emgo/egpath/src/stm32/examples/f030-demo-board/blog/cortexm0.elf section `.text 'will not fit in region `Flash'/usr/local/arm/bin/arm-none-eabi-ld: region `Flash' overflowed by 10880 bytesexit status 1

"Hello, World!" Flash space of at least 32KB on STM32F030x6 is required.

The fmt package forces the entire strconv and reflect packages to be included. These three packages take up a lot of space even in the stripped-down version of Emgo. We can't use this example. There are many applications that do not require good-looking text output. Usually, one or more LED, or seven-segment digital tube display is sufficient. However, in the second part, I will try to use the strconv package to format and display some numbers and text on the UART.

Flicker

There is a LED connected to PA4 pin and VCC on our development board. This time our code is a little longer:

Package main import ("delay"stm32/hal/gpio"stm32/hal/system"stm32/hal/system/timer/systick") var led gpio.Pin func init () {system.SetupPLL (8,1,48 systick.Setup) systick.Setup (2e6) gpio.A.EnableClock (false) led = gpio.A.Pin (4) cfg: = & gpio.Config {Mode: gpio.Out Driver: gpio.OpenDrain} led.Setup (cfg)} func main () {for {led.Clear () delay.Millisec (100) led.Set () delay.Millisec (900)}

By convention, the init function is used to initialize and configure peripherals.

System.SetupPLL (8,1,48 system.SetupPLL) is used to configure the RCC, using the PLL of the external 8 MHz oscillator as the system clock source. The PLL frequency divider is set to 1, and the frequency multiplier is set to 48 stroke 8 = 6, so that the system clock frequency is 48MHz.

Systick.Setup (2e6) uses the Cortex-M SYSTICK clock as the system clock, running every 2e6 nanosecond (500times per second).

Gpio.A.EnableClock (false) turns on the clock of GPIO A port. False means that this clock is disabled in low power mode, but this feature is not implemented in the STM32F0 series.

Led.Setup (cfg) sets the PA4 pin to an open-leak output.

Led.Clear () sets the PA4 pin to low, and in the leak opening setting, turn on LED.

Led.Set () sets PA4 to high level and turns off LED.

Compile this code:

$egc$ arm-none-eabi-size cortexm0.elf text data bss dec hex filename 9772 172168 10112 2780 cortexm0.elf

As you can see, this flicker takes up 2320 bytes, which is more space than the most basic program. There is still 6440 bytes of space left.

See if the code works:

$openocd-d0-f interface/stlink.cfg-f target/stm32f0x.cfg-c 'init; program cortexm0.elf; reset run Exit'Open On-Chip Debugger 0.10.0+dev-00319-g8f1f912a (2018-03-07-19:20) Licensed under GNU GPL v2For bug reports, read http://openocd.org/doc/doxygen/bugs.htmldebug_level: 0adapter speed: 1000 kHzadapter_nsrst_delay: 100none separateadapter speed: 950 kHztarget halted due to debug-request, current mode: Thread xPSR: 0xc1000000 pc: 0x0800119c msp: 0x20000da0adapter speed: 4000 kHz** Programming Started * * auto erase enabledtarget halted due to breakpoint Current mode: Thread xPSR: 0x61000000 pc: 0x2000003a msp: 0x20000da0wrote 10240 bytes from file cortexm0.elf in 0.817425s (12.234 KiB/s) * * Programming Finished * * adapter speed: 950kHz more Go language programming

If you are not a Go programmer, but you have heard something about the Go language, you might say, "Go syntax is good, but there is no significant improvement compared to C. let me see the channels and collaborations of the Go language!"

Next, I will show you one by one:

Import ("delay"stm32/hal/gpio"stm32/hal/system"stm32/hal/system/timer/systick") var led1, led2 gpio.Pin func init () {system.SetupPLL (8,1,48 system.SetupPLL 8) systick.Setup (2e6) gpio.A.EnableClock (false) led1 = gpio.A.Pin (4) led2 = gpio.A.Pin (5) cfg: & gpio.Config {Mode: gpio.Out Driver: gpio.OpenDrain} led1.Setup (cfg) led2.Setup (cfg)} func blinky (led gpio.Pin, period int) {for {led.Clear () delay.Millisec (100) led.Set () delay.Millisec (period-100)} func main () {go blinky (led1, 500) blinky (led2, 1000)}

The code changes are minor: a second LED has been added, and the main function in the previous example has been renamed to blinky and needs to provide two parameters. Main calls blinky first in the new protocol, so the two LED lights are used in parallel. It is worth mentioning that gpio.Pin can access different pins of the same GPIO port at the same time.

Emgo still has many shortcomings. One of these is that you need to specify the maximum number of goroutines (tasks) implementations in advance. It's time to modify the script.ld:

ISRStack = 1024 per MainStack = 1024 per TaskStack = 1024 per cent MaxTasks = 2; INCLUDE stm32/f030x4INCLUDE stm32/loadflashINCLUDE noos-cortexm

The size of the stack needs to be guessed, and you don't have to worry about it yet.

$egc$ arm-none-eabi-size cortexm0.elf text data bss dec hex filename 10020 172 10364 287c cortexm0.elf

The other LED and co-program take up a total of 248bytes of Flash space.

Aisle

Channel is a recommended way for cooperative programs to communicate with each other in Go language. Emgo even allows the use of buffers through interrupt handling. The next example shows this situation.

Package main import ("delay"rtos"stm32/hal/gpio"stm32/hal/irq"stm32/hal/system"stm32/hal/system/timer/systick"stm32/hal/tim") var (leds [3] gpio.Pin timer * tim.Periph ch = make (chan int, 1) func init () {system.SetupPLL (8,1) 48 systick.Setup (2e6) gpio.A.EnableClock (false) leds [0] = gpio.A.Pin (4) leds [1] = gpio.A.Pin (5) leds [2] = gpio.A.Pin (9) cfg: = & gpio.Config {Mode: gpio.Out, Driver: gpio.OpenDrain} for _ Led: = range leds {led.Set () led.Setup (cfg)} timer = tim.TIM3 pclk: = timer.Bus () .Clock () if pclk < system.AHB.Clock () {pclk * = 2} freq: = uint (1e3) / / Hz timer.EnableClock (true) timer.PSC.Store (tim.PSC (pclk/freq-1) timer.ARR.Store ( Ms timer.DIER.Store (tim.UIE) timer.CR1.Store (tim.CEN) rtos.IRQ (irq.TIM3). Enable ()} func blinky (led gpio.Pin) Period int) {for range ch {led.Clear () delay.Millisec (100) led.Set () delay.Millisec (period-100)} func main () {go blinky (leds [1], 500) blinky (leds [2], 500)} func timerISR () {timer.SR.Store (0) leds [0] .Set () select {case ch

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report