owned this note
owned this note
Published
Linked with GitHub
# 成功大學資訊工程學系進階嵌入式系統課程 Week#3 Lab39 (macOS 環境版)
###### tags: `embedded`
## Developing Environment
Note: 因為是一段時間以前就安裝好 STM32 porting environment 的,因此[Ubuntu 版本](http://embedded2015.hackpad.com/Week3-Lab39-ni3tzoIax7Y)前幾個步驟忘了有沒有),參考:http://wiki.csie.ncku.edu.tw/embedded/freertos#測試環境架設 之 `OS X 上安裝 gcc-arm-none-eabi、stlink、openocd`
* 作業系統:macOS (OS X) 10.12.2
* 取得 GNU Toolchain:
**gcc-arm-none-eabi**
```shell=
$ brew tap PX4/homebrew-px4
$ brew update
$ brew install gcc-arm-none-eabi
# 使用:$ arm-none-eabi-gcc
```
**libenewlib-arm-none-eabi**
`brew install libnewlib-arm-none-eabi`
<hr>
(2018-10-04)
如果發生:`No available formula with the name "libnewlib-arm-none-eabi"`,上網找不太到資料,試著直接 ./configure
這次重建環境沒辦法成功安裝 libnewlib-arm-none-eabi 後:
```shell=
↪ ./configure --disable-werror --enable-debug \
--target-list="arm-softmmu" \
--extra-cflags=-DSTM32_UART_NO_BAUD_DELAY \
--extra-cflags=-DSTM32_UART_ENABLE_OVERRUN \
--disable-gtk
ERROR: glib-2.12 gthread-2.0 is required to compile QEMU
> brew install glib
↪ ./configure --disable-werror --enable-debug \
--target-list="arm-softmmu" \
--extra-cflags=-DSTM32_UART_NO_BAUD_DELAY \
--extra-cflags=-DSTM32_UART_ENABLE_OVERRUN \
--disable-gtk
ERROR: pixman not present. Your options:
(1) Preferred: Install the pixman devel package (any recent
distro should have packages as Xorg needs pixman too).
(2) Fetch the pixman submodule, using:
git submodule update --init pixman
> git submodule update --init pixman
↪ ./configure --disable-werror --enable-debug \
--target-list="arm-softmmu" \
--extra-cflags=-DSTM32_UART_NO_BAUD_DELAY \
--extra-cflags=-DSTM32_UART_ENABLE_OVERRUN \
--disable-gtk
ld: unknown option: --verbose
Install prefix /usr/local
BIOS directory /usr/local/share/qemu
binary directory /usr/local/bin
library directory /usr/local/lib
module directory /usr/local/lib/qemu
libexec directory /usr/local/libexec
include directory /usr/local/include
config directory /usr/local/etc
local state directory /usr/local/var
Manual directory /usr/local/share/man
ELF interp prefix /usr/gnemul/qemu-%M
Source path /Users/veckh/Desktop/workspace/qemu_stm32
C compiler cc
Host C compiler cc
C++ compiler c++
Objective-C compiler clang
ARFLAGS rv
CFLAGS -D_REENTRANT -I/usr/local/Cellar/glib/2.58.1/include/glib-2.0 -I/usr/local/Cellar/glib/2.58.1/lib/glib-2.0/include -I/usr/local/opt/gettext/include -I/usr/local/Cellar/pcre/8.42/include -g
QEMU_CFLAGS -m64 -DOS_OBJECT_USE_OBJC=0 -arch x86_64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -Wstrict-prototypes -Wredundant-decls -Wall -Wundef -Wwrite-strings -Wmissing-prototypes -fno-strict-aliasing -fno-common -DSTM32_UART_ENABLE_OVERRUN -DSTM32_UART_NO_BAUD_DELAY -Wno-string-plus-int -Wno-initializer-overrides -Wendif-labels -Wmissing-include-dirs -Wempty-body -Wnested-externs -Wformat-security -Wformat-y2k -Winit-self -Wignored-qualifiers -Wold-style-definition -Wtype-limits -fstack-protector-strong -I$(SRC_PATH)/pixman/pixman -I$(BUILD_DIR)/pixman/pixman -I$(SRC_PATH)/dtc/libfdt
LDFLAGS -m64 -framework CoreFoundation -framework IOKit -arch x86_64 -g
make make
install install
python python -B
smbd /usr/sbin/smbd
module support no
host CPU x86_64
host big endian no
target list arm-softmmu
tcg debug enabled yes
gprof enabled no
sparse enabled no
strip binaries no
profiler no
static build no
Cocoa support yes
pixman internal
SDL support no
GTK support no
VTE support no
curses support yes
curl support yes
mingw32 support no
Audio drivers coreaudio
Block whitelist (rw)
Block whitelist (ro)
VirtFS support no
VNC support yes
VNC TLS support no
VNC SASL support yes
VNC JPEG support no
VNC PNG support no
VNC WS support no
xen support no
brlapi support no
bluez support no
Documentation yes
GUEST_BASE yes
PIE no
vde support no
netmap support no
Linux AIO support no
ATTR/XATTR support no
Install blobs yes
KVM support no
RDMA support no
TCG interpreter no
fdt support yes
preadv support no
fdatasync no
madvise yes
posix_madvise yes
sigev_thread_id no
uuid support yes
libcap-ng support no
vhost-net support no
vhost-scsi support no
Trace backends nop
spice support no
rbd support no
xfsctl support no
nss used no
libusb no
usb net redir no
GLX support no
libiscsi support no
libnfs support no
build guest agent yes
QGA VSS support no
seccomp support no
coroutine backend sigaltstack
coroutine pool yes
GlusterFS support no
virtio-blk-data-plane no
gcov gcov
gcov enabled no
TPM support yes
libssh2 support no
TPM passthrough no
QOM debugging yes
vhdx yes
Quorum no
lzo support no
snappy support no
NUMA host support no
```
之後繼續跑教學指令沒問題,但 warning 好像變多...
<hr>
## STM32 in QEMU
```
>>> mkdir -p workspace
>>> cd workspace
>>> git clone git://github.com/beckus/stm32_p103_demos.git
>>> git clone git://github.com/beckus/qemu_stm32.git
>>> cd qemu_stm32
>>> git submodule update --init dtc
>>> ./configure --disable-werror --enable-debug \
--target-list="arm-softmmu" \
--extra-cflags=-DSTM32_UART_NO_BAUD_DELAY \
--extra-cflags=-DSTM32_UART_ENABLE_OVERRUN \
--disable-gtk
>>> make
>>> cd ../stm32_p103_demos
>>> make all
>>> make blink_flash_QEMURUN
```
上述步驟成功畫面

`make button_QEMURUN`

如果滑鼠點到 QEMU emulator 視窗,會進入視窗畫面而鎖定滑鼠,按 `command + alt` 可以 release
## FreeRTOS on STM32
```
git clone https://github.com/fbukevin/freertos-basic.git
cd freertos-basic
make
make qemu
```

* Discover: `前面階段中的 qemu emulator 敘述跑到 terminal 了`
* Problem: `在 qemu emulator 中 keyboard 無法輸入`
原因是在 fio.c 中 fio_read function 的這一段:
```c
if (fio_fds[fd].fdread) {
r = fio_fds[fd].fdread(fio_fds[fd].opaque, buf, count);
} else {
r = -3;
}
```
其中的 fio_fds[fd].fdread() 沒有作用
in fio.h, we defined:
```c
typedef ssize_t (*fdread_t)(void * opaque, void * buf, size_t count);
```
and we assign this poiter in fio.c in fio_init():
```c
fio_fds[0].fdread = stdin_read;
```
so that we know whenever we called like this:
```c
r = fio_fds[fd].fdread(fio_fds[fd].opaque, buf, count);
```
it's equivalent to call:
```c
r = fio_fds[fd].stdin_read(fio_fds[fd].opaque, buf, count);
```
and we can find the implementation of stdin_read() in fio.c:
```c
static ssize_t stdin_read(void * opaque, void * buf, size_t count) {
...
}
```
in stdin_read, we found inputs are completed by function recv_byte() which is defined in main.c:
```c
char recv_byte()
{
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
char msg;
while(!xQueueReceive(serial_rx_queue, &msg, portMAX_DELAY));
return msg;
}
```
after tracing to here, we found that function `xQueueReceive()` which is not in freertos-basic/src is the problem cause input disabled.
* 另外一個可能是,當使用 make qemu,會出現:
```
(process:68826): GLib-WARNING **: gmem.c:482: custom memory allocation vtable not supported
```
很有可能是這個造成的
* 還有一個可能,用 make qemudbg 發現在 main.c 的 init_rs232() 就有 error,可能是因為 rs232 的初始化失敗,而 rs232 是負責 I/O 的
```
Error while running hook_stop:
No symbol "disassembly" in current context.
Reset_Handler () at freertos/libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/startup/gcc_ride7/startup_stm32f10x_md.s:67
67 movs r1, #0
Breakpoint 1 at 0x1686: file src/main.c, line 162.
Error while running hook_stop:
No symbol "disassembly" in current context.
Breakpoint 1, main () at src/main.c:162
162 init_rs232();
gdb$
```
目前實驗至此終止了!#