F9 microkernel app development part 2 gpio meets led

1,418 views

Published on

Slides for Sharing at JuluOSDev 04/21

Published in: Technology, Business
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,418
On SlideShare
0
From Embeds
0
Number of Embeds
222
Actions
Shares
0
Downloads
32
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • 其實日常使用設備裡都有 GPIO,小到 LED 燈號,大到各式外接設備,大多都會使用到 GPIO。通常我們program使用的data都是放在memory比較多,而GPIO也提供類似的操作方法給programmer(讓我們去更改記憶體內容就可以去控制pin,並影響周遭設備的運行),但真正的設備的位置並非真正落在記憶體上(如:LED、Button、…等),故GPIO的核心是記憶體操作與設備之間的一些電路特性。
  • Typically, A GPIO port is grouped 8 GPIO pinsGPIO雖然建立起記憶體與設備之間的橋梁,但也並非我們就可以隨意使用,我們必須要經過設定之後才能讓我們想要的設備正常工作。一個pin通常可被設定成input、output、alternatefunction或analog,input會有兩種狀態表現(floating, pull-up/down),output也有兩種狀態表現(push-pull with pull-up/pull-down or open drain with pull-up/down)。input/output方向解說 : input是指記憶體方接收來自設備的訊號源,output是指記憶體傳送訊號給設備。當pin被設定成input時,非analog的設定下,我們可以利用GPIO的input data register(GPIOx_IDR) 或是memory中提供給目標設備的data register (當設成alternatefunction的時候)去接收data。當pin被設定成output時,非analog的設定下,GPIO本身有提供output data register (GPIOx_ODR)來對目標設備做控制,但要是pin不是使用原本預先定義好的功能時(非預先定義的功能都算是alternate function的類別),此時要用memory中,另外規劃給目標設備用的register。如果pin被設成analog的話,無論input or output都會由adc那邊做處理。
  • floating vs. pull-up/pull-down當input pin被處在高阻抗的模式下,若沒有外部訊號源進來的話,此時是無法確定pin的狀態(不能確定現在處在高電位或低電位),除非有外部訊號來驅動電路。換句話說,input floating,這個input電位狀態完全是由外部訊號來決定,沒有訊號驅動的話,就會呈現高阻抗狀態。剛剛提到floating在沒有外部訊號驅動的情況下是呈現高阻抗狀態(無法確定電位狀態=>不能明確表示現在值是0或1),如果我們需要這個pin有一個明確的預設狀態時,必須借助pull-up(pull-down)resistor來做調整,在pull-up resistor(pull-up外接高電壓,pull-down通常會接地)加入之下,讓pin的維持在明確的高電壓狀態(pull-down則是讓pin維持在低電壓狀態)。舉例來說,如果我們定電壓在3-4 V之間是1的狀態,0-1之間是0的狀態,高阻抗的時候,電壓是不明確的,有可能電壓值會落在1-3之間的不明確地帶,甚至是沒有在任何一個狀態維持一段時間,此時的狀態是未定的,但如果我們加入pull-up resistor的話,這個pin接受來自pull-up另一端的電壓供應,讓pin至少維持在3v以上時,我們就可以確定在沒有外部訊號驅動時,pin是維持在高電位狀態。
  • Typically, A GPIO port is grouped 8 GPIO pinsGPIO雖然建立起記憶體與設備之間的橋梁,但也並非我們就可以隨意使用,我們必須要經過設定之後才能讓我們想要的設備正常工作。一個pin通常可被設定成input、output、alternatefunction或analog,input會有兩種狀態表現(floating, pull-up/down),output也有兩種狀態表現(push-pull with pull-up/pull-down or open drain with pull-up/down)。input/output方向解說 : input是指記憶體方接收來自設備的訊號源,output是指記憶體傳送訊號給設備。當pin被設定成input時,非analog的設定下,我們可以利用GPIO的input data register(GPIOx_IDR) 或是memory中提供給目標設備的data register (當設成alternatefunction的時候)去接收data。當pin被設定成output時,非analog的設定下,GPIO本身有提供output data register (GPIOx_ODR)來對目標設備做控制,但要是pin不是使用原本預先定義好的功能時(非預先定義的功能都算是alternate function的類別),此時要用memory中,另外規劃給目標設備用的register。如果pin被設成analog的話,無論input or output都會由adc那邊做處理。
  • GPIO_OTYPER_PP
  • 而push-pull输出的优势是速度快,因为线路是以两种方式驱动的。而带了上拉电阻的线路,即使以最快的速度去提升电压,最快也要一个常量的R×C的时间。其中R是电阻,C是寄生电容(parasitic capacitance),包括了pin脚的电容和板子的电容。但是,push-pull相对的缺点是往往需要消耗更多的电流,即功耗相对大。而open-drain所消耗的电流相对较小,由电阻R所限制,而R不能太小,因为当输出为低电平的时候,需要sink更低的transistor,这意味着更高的功耗。而open-drain的好处之一是,允许你short多个open-drain的电路,共用一个上拉电阻,此种做法称为wired-OR连接,此时可以通过拉低任何一个IO的pin脚使得输出为低电平。为了输出高电平,则所有的都输出高电平。此种逻辑,就是“线与”的功能,可以不需要额外的门电路来实现此部分逻辑。http://www.douban.com/note/206867792/
  • 一些有預設控制元件的腳位(詳見- STM32F4-Discovery 中文使用手冊P.16)LD3:橙色,連接到STM32F407VGT6的PD13LD4:綠色,連接到STM32F407VGT6的PD12LD5:红色,連接到STM32F407VGT6的PD14LD6:藍色,連接到STM32F407VGT6的PD15B1(USER):連接到STM32F407VGT6的PA0B2(RESET):連接到NRST,用於重置STM32F407VGT6
  • F9 microkernel app development part 2 gpio meets led

    1. 1. Part 2: GPIO meets LED ben6 2014-04-21 F9-Microkernel App Development
    2. 2. Agenda • Why we have to know GPIO? • What’s GPIO? • How to use GPIO? F9
    3. 3. Why we have to know GPIO? Basis of external hardware control GPIO (General-purpose input/output) Storage devices NICs Buttons LED …
    4. 4. Agenda • Why we have to know GPIO? • What’s GPIO? – F9 GPIO API for basic app – Code reading about F9 GPIO Hooks • How to use GPIO? F9
    5. 5. What’s GPIO? • GPIO -- General-purpose input/output – Generic pin on a chip whose behavior can be controlled by the user at run time – Support DMA (Direct Memory Access) control
    6. 6. STM32F407VGT6 GPIOx
    7. 7. GPIO input • Floating: unknown voltage input stage • Pull-up: connect to high voltage • Pull-down: connected to ground • Non-analog mode: input data register (GPIO_IDR) Memory Devices gpio_input_bit(port, pin) { if (*GPIO_IDR(port) & (1 << pin)) return 1; return 0; }
    8. 8. GPIO output • push-pull with pull-up/pull-down • open-drain with pull-up/pull-down • Non-analog mode, output data register (GPIO_ODR) Memory Devices *GPIO_ODR(GPIOA) |= (1 << pin);
    9. 9. Agenda • Why we have to know GPIO? • What’s GPIO? – F9 GPIO API for basic app – Code reading about F9 GPIO Hooks • How to use GPIO? F9
    10. 10. PR #86 GPIO V1 implementation Kernel part • include/thread.h • include/user-gpioer.h • kernel/ipc.c • kernel/user-gpioer.c App part: library and app • user/apps/gpioer/main.c • user/include/gpioer.h • user/lib/io/gpioer.c Pull request#86 https://github.com/f9micro/f9-kernel/pull/86 GPIO V2: Kernel part GPIO driver code will move to user-space because this part violates L4 design principle GPIO V2: How to make thread has authority to access AHB1_1DEV?
    11. 11. V1: F9 GPIO API for basic app • Defined in user/include/gpioer.h • gpioer_config_output • gpioer_out void gpioer_config_output(uint8_t port, uint8_t pin, uint8_t pupd, uint8_t speed); void gpioer_out(uint8_t port, uint8_t pin, uint8_t action);
    12. 12. V1: gpioer_config_output void gpioer_config_output( uint8_t port, /* Ex: GPIOA, … , GPIOD, …*/ uint8_t pin, /* 0 – 15 */ uint8_t pupd, /* GPIO_PUPDR_UP */ uint8_t speed /* GPIO_PUPDR_UP,GPIO_OSPEEDR_50M */ ); Wrap gpio_config_output from include/platform/stm32f4/gpio.h
    13. 13. GPIO Output Type #define GPIO_OTYPER_PP (uint32_t (0x0) /* Output push-pull */ #define GPIO_OTYPER_OD (uint32_t)(0x1) /* Output open drain */ include/platform/stm32f4/registers.h
    14. 14. GPIO Speed #define GPIO_OSPEEDR_2M (uint32_t) (0x0) /* Output speed 2MHz */ #define GPIO_OSPEEDR_25M (uint32_t) (0x1) /* Output speed 25MHz */ #define GPIO_OSPEEDR_50M (uint32_t) (0x2) /* Output speed 50MHz */ #define GPIO_OSPEEDR_100M(uint32_t) (0x3) /* Output speed 100MHz*/ include/platform/stm32f4/registers.h
    15. 15. V1: gpioer_out void gpioer_out(uint8_t port, uint8_t pin, uint8_t action /* GPIO_HIGH or GPIO_LOW */ ); Wrap gpio_out_high and gpio_out_low from include/platform/stm32f4/gpio.h
    16. 16. Agenda • Why we have to know GPIO? • What’s GPIO? – F9 GPIO API for basic app – Code reading about F9 GPIO Hooks • How to use GPIO? F9
    17. 17. Code reading of GPIO for user part • user/include/gpioer.h • user/lib/io/gpioer.c
    18. 18. Code reading about F9 GPIO Hooks – include/thread.h – include/user-gpioer.h – kernel/build.mk – kernel/ipc.c – kernel/user-gpioer.c Implementation of this hook by reference the THREAD_LOG printf function
    19. 19. Agenda • Why we have to know GPIO? • What’s GPIO? • How to use GPIO? – First f9 GPIO app practice to control led light – Lab3: make led blinking in turns F9
    20. 20. First f9 GPIO app practice to control led light • PR86 – https://github.com/f9micro/f9-kernel/pull/86 • Demo 1. built-in 4 LEDs Blinking 2. trigger External LED
    21. 21. Live Demo 1: built-in 4 LEDs Blinking
    22. 22. STM32F407 Built-in 4 User LEDs LD3 (orange) LD4 (green) LD5 (red) LD6 (blue) PD13 PD12 PD14 PD15
    23. 23. Live Demo 2: trigger External LED
    24. 24. Lab 3: LEDs blinking Rocks Requirements – Modify LED sample to let the led in turns – Blinking interval 2 seconds Hints: reference user/apps/gpioer/main.c git clone https://github.com/benwei/f9-kernel.git git checkout 0421-gpioer
    25. 25. Summary: Points recall • GPIO is the basis of external control • What’s implementation of GPIO api for user app • Easy to use API for gpioer for pin output control
    26. 26. Discussions ? F9
    27. 27. References • F9 Microkernel source code and introduction • GCC Naked Attribute • ARM: Memory Model of Cortex M4 • Cortex™ -M4 Devices Generic User Guide pdf • General-purpose Input/Output (GPIO) • UM1472 User manual Discovery kit for STM32F407/417 lines • STM32F4-Discovery 中文使用手冊

    ×