nuttx 101
대상
- px4 코드 이해하고 새로운 px4앱 또는 드라이버를 만들려고 하는 분
왜?
- px4 구조가 nuttx 구조를 따른다.
- posix 구조/ mcu계의 리눅스. (posix os 포팅 가능)
- 멀티태스크 실행이 편리하다. RTOS니깐
Shell 제공(NSH), px4가 nsh을 쓴다.
nuttx 소개
- RTOS(real-time operating system)
- 멀티 테스크 관리
코드: http://subak.io/code/px4/NuttX
특징
- 8bit ~ 32bit MCU 지원
- 표준 (Posix) https://en.wikipedia.org/wiki/POSIX
- Task 관리
- Pseudo Filesystem
- Device Driver
- Modular Design
- FIFO, round-robin, and “sporadic” scheduling. 선택
- 리얼타임
- BSD
- GNU toolchains
구조
POSIX 구조. 물론 100%는 아니다.
https://bitbucket.org/nuttx/arch
용어
nuttx 에서
- Task: 일반적인 RTOS에서의 Thread
- pthread: 부모 테스크의 자원을 공유하는 Task
NuttX System Calls
NuttX 코드 검색 http://subak.io/code/px4/NuttX/
open, write, read close
Task control interfaces
- task_create()
- task_init()
- task_activate()
- task_delete()
- task_restart()
- exit()
- getpid()
- vfork()
- execv()
- execl()
Task Priorities and Scheduling
- sched_setparam(pid_t pid, const struct sched_param *param);
- sched_getparam (pid_t pid, struct sched_param *param);
File I/O
Pseudo Root File System
- Standard I/O
- printf(const char *format, ...);
- fputc fgetc
- Standard String
IPC
Named Message Queue Interfaces
Allow processes(Tasks) to exchange data in the form of messages.
- mq_open():
- mq_send():
- mq_receive():
- mq_close():
- mq_unlink():
Posix 프로그래밍
- File I/O
- Standard I/O
- Process Control
- Signals
- Threads
- IPC
Posix 프로그래밍 참고:
man fopen
- Advanced Programming in the Unix Environment by W. Richard Stevens and Stephen A. Rago
- Advanced Unix Programming by Marc J. Rochkind
- Programming with POSIX® Threads by David R. Butenhof
App
NSH
/NuttX/apps/nshlib
- shell
- 간단한 shell programming 가능
- px4에서도 디버깅 또는 앱실행할때 nsh을 사용한다.
- http://nuttx.org/doku.php?id=documentation:nuttshell
NSH 실습
picocom /dev/tty.usbserial-AH01JABI --baud 57600
nsh> help
- nsh에서 파일 디렉토리를 읽어보자.
- 명령을 참고하여 실행된 앱의 리스트를 가져오고, px4 리붓을 해보자.
nsh 명령어: http://nuttx.org/doku.php?id=documentation:nuttshell
PX4에서 NSH접속하기
준비물
- picocom 터미널 프로그램
- ftdi232 3.3v 모듈 Pixhawk SERIAL 4/5 포트의 4(TX),5(RX),6(GND)에 연결
- 또는 SD 카드를 뺴고 usb 시리얼포트로 접속한다.
sudo apt-get install picocom
picocom /dev/tty.usbserial-AH01JABI --baud 57600
adc app
apps/examples/adc
실습: adc app을 읽어 보자.
새로운 nuttx app 추가하기
참고: https://bitbucket.org/nuttx/apps
//px4 nuttx는 다음 방법으로 앱추가 안된다. px4_code.md 참고
코드: apps/examples/hello/hello_main.c entry point 설정
#include <nuttx/config.h> /* 1 */ #include <stdio.h> int hello_main(int argc, char *argv[]) /* 2 */ { printf("Hello, World!!\n"); /* 3 */ return 0; }
앱 빌드 설정추가: add apps/examples/hello/Make.defs KConfig Makefile 예제 코드 참고하여 빌드 설정.
APPNAME = hello PRIORITY = SCHED_PRIORITY_DEFAULT STACKSIZE = 768 ASRCS = asm source file list as a.asm b.asm ... CSRCS = C source file list as foo1.c foo2.c ..
edit apps/examples/hello/Make.defs에 다음 추가.
ifeq ($(CONFIG_APPNAME),y) CONFIGURED_APPS += examples/hello endif
포팅
NuttX RTOS Porting Guide
http://www.nuttx.org/Documentation/NuttxPortingGuide.html