├── py-rasp ├── temp-display │ ├── oled │ │ ├── __init__.py │ │ ├── logger.py │ │ └── oled.py │ ├── tmp64 │ │ ├── __init__.py │ │ └── tmp64.py │ ├── Adafruit_SSD1306 │ │ └── __init__.py │ ├── mcp3008 │ │ ├── __init__.py │ │ ├── logger.py │ │ └── mcp3008.py │ ├── monaco.ttf │ ├── docs │ │ └── enable_spi.md │ ├── client.py │ └── server.py └── comm-remote-host │ └── send_my_ip.py ├── asm-rasp └── mrt_experiment │ ├── gpio_tests │ ├── tools_debugging │ │ ├── gdbserver.dat │ │ ├── main.S │ │ ├── rpi.c │ │ └── rpi.h │ ├── blink │ │ ├── blink.c │ │ └── setOne.S │ ├── setPinOne │ │ ├── rpi.c │ │ ├── rpi.h │ │ └── setPins.S │ └── inputTest │ │ └── input_pins.S │ ├── input_test │ ├── record.txt │ ├── makefile │ ├── rpi.c │ ├── read_temp.c │ ├── rpi.h │ ├── input.S │ └── gpio_functions.S │ ├── calc_led │ ├── makefile │ ├── rpi.c │ ├── rpi.h │ ├── main.S │ └── gpio_functions.S │ ├── button_test │ ├── makefile │ ├── rpi.c │ ├── rpi.h │ ├── ref.S │ ├── main.S │ └── gpio_functions.S │ ├── led_rhythm │ ├── makefile │ ├── rpi.c │ ├── rpi.h │ ├── gpio_functions.S │ └── main.S │ ├── button_led_control │ ├── makefile │ ├── rpi.c │ ├── rpi.h │ ├── gpio_functions.S │ └── main.S │ ├── interaption_test │ ├── makefile │ ├── rpi.c │ ├── rpi.h │ ├── gpio_functions.S │ └── main.S │ ├── pwm_led │ ├── makefile │ ├── main.S │ ├── rpi.c │ ├── rpi.h │ ├── pwm_orignal.c │ ├── pwm.c │ └── pwm.s │ ├── pwm_test │ ├── makefile │ ├── rpi.c │ ├── rpi.h │ ├── pwm_settings.S │ ├── gpio_functions.S │ ├── mapping.c │ ├── pwm.c │ ├── pwm_back.c │ ├── main.S │ ├── main_back.S │ └── pwm.s │ ├── temp_mess │ ├── makefile │ ├── rpi.c │ ├── read_temp.c │ ├── rpi.h │ ├── gpio_functions.S │ └── main.S │ ├── baseboard_test │ ├── makefile │ ├── rpi.c │ ├── rpi.h │ └── inputPinTest.S │ ├── test_led │ └── test.py │ ├── mcp_test │ ├── rpi.c │ ├── read_mcp.c │ └── rpi.h │ └── util_functions │ └── unsigned_division.S ├── config-rasp └── wpa-config-debian │ ├── README.md │ ├── set_wpa.sh │ ├── wpa_supplicant.conf │ └── interfaces ├── c-rasp ├── ssd1306-i2c │ ├── README.md │ ├── makefile │ ├── hello_world.c │ └── ssd1306_i2c.h └── oled-udp │ ├── setup_env.sh │ ├── makefile │ ├── ssd1306_i2c.h │ ├── server.c │ └── client.c ├── README.md └── .gitignore /py-rasp/temp-display/oled/__init__.py: -------------------------------------------------------------------------------- 1 | from oled import OLED 2 | -------------------------------------------------------------------------------- /py-rasp/temp-display/tmp64/__init__.py: -------------------------------------------------------------------------------- 1 | from tmp64 import TMP64 2 | -------------------------------------------------------------------------------- /py-rasp/temp-display/Adafruit_SSD1306/__init__.py: -------------------------------------------------------------------------------- 1 | from SSD1306 import * -------------------------------------------------------------------------------- /py-rasp/temp-display/mcp3008/__init__.py: -------------------------------------------------------------------------------- 1 | from .mcp3008 import MCP3008 2 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/gpio_tests/tools_debugging/gdbserver.dat: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | sudo /usr/bin/gdbserver $* 3 | -------------------------------------------------------------------------------- /py-rasp/temp-display/monaco.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stevelorenz/codes-on-rasp/HEAD/py-rasp/temp-display/monaco.ttf -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/input_test/record.txt: -------------------------------------------------------------------------------- 1 | >>> bin(1342139905) 2 | '0b1001111111111110110111000000001' 3 | '0b1001111111111111110111000000001' 4 | -------------------------------------------------------------------------------- /config-rasp/wpa-config-debian/README.md: -------------------------------------------------------------------------------- 1 | # wpa_config_debian 2 | 3 | WPA configuration file and install script for quick-settings on Raspberry Pi or Odroid. 4 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/calc_led/makefile: -------------------------------------------------------------------------------- 1 | all: calc_led 2 | 3 | calc_led: main.S gpio_functions.S rpi.c 4 | gcc -g main.S gpio_functions.S rpi.c -o calc_led 5 | 6 | clean: 7 | rm -rf *.o 8 | rm -rf button_led 9 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/button_test/makefile: -------------------------------------------------------------------------------- 1 | all: button_led 2 | 3 | button_led: main.S gpio_functions.S rpi.c 4 | gcc -g main.S gpio_functions.S rpi.c -o button_led 5 | 6 | clean: 7 | rm -rf *.o 8 | rm -rf button_led 9 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/led_rhythm/makefile: -------------------------------------------------------------------------------- 1 | all: led_rhythm 2 | 3 | led_rhythm: main.S gpio_functions.S rpi.c 4 | gcc -g main.S gpio_functions.S rpi.c -o led_rhythm 5 | 6 | clean: 7 | rm -rf *.o 8 | rm -rf led_rhythm 9 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/button_led_control/makefile: -------------------------------------------------------------------------------- 1 | all: button_led 2 | 3 | button_led: main.S gpio_functions.S rpi.c 4 | gcc -g main.S gpio_functions.S rpi.c -o button_led 5 | 6 | clean: 7 | rm -rf *.o 8 | rm -rf button_led 9 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/interaption_test/makefile: -------------------------------------------------------------------------------- 1 | all: button_led 2 | 3 | button_led: main.S gpio_functions.S rpi.c 4 | gcc -g main.S gpio_functions.S rpi.c -o button_led 5 | 6 | clean: 7 | rm -rf *.o 8 | rm -rf button_led 9 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/pwm_led/makefile: -------------------------------------------------------------------------------- 1 | all: pwm_test 2 | 3 | pwm_test: main.S gpio_functions.S pwm_settings.S pwm.c 4 | gcc -g main.S gpio_functions.S pwm_settings.S pwm.c -o pwm_test 5 | 6 | clean: 7 | rm -rf *.o 8 | rm -rf pwm_test 9 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/pwm_test/makefile: -------------------------------------------------------------------------------- 1 | all: pwm_test 2 | 3 | pwm_test: main.S gpio_functions.S pwm_settings.S pwm.c 4 | gcc -g main.S gpio_functions.S pwm_settings.S pwm.c -o pwm_test 5 | 6 | clean: 7 | rm -rf *.o 8 | rm -rf pwm_test 9 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/input_test/makefile: -------------------------------------------------------------------------------- 1 | all: input 2 | 3 | input: input.S gpio_functions.S read_temp.c libwiringpi.so 4 | gcc input.S gpio_functions.S read_temp.c rpi.c -L. -lwiringpi -o input 5 | 6 | clean: 7 | rm -rf *.o 8 | rm -rf input 9 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/temp_mess/makefile: -------------------------------------------------------------------------------- 1 | all: tmp_proc 2 | 3 | tmp_proc: main.S gpio_functions.S read_temp.c rpi.c libwiringpi.so 4 | gcc -g main.S gpio_functions.S read_temp.c rpi.c -L. -lwiringpi -o tmp_proc 5 | 6 | clean: 7 | rm -rf *.o 8 | rm -rf tmp_proc 9 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/baseboard_test/makefile: -------------------------------------------------------------------------------- 1 | all: inputPinTest 2 | 3 | inputPinTest: inputPinTest.o 4 | gcc inputPinTest.o rpi.c -o inputPinTest 5 | 6 | inputPinTest.o: inputPinTest.S 7 | as -o inputPinTest.o inputPinTest.S 8 | 9 | clean: 10 | rm -rf inputPinTest *.o 11 | -------------------------------------------------------------------------------- /config-rasp/wpa-config-debian/set_wpa.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "Copy WPA config files..." 4 | sudo mv ./wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf 5 | sudo mv ./interfaces /etc/network/interfaces 6 | echo "Copy finised. Edit wpa_supplicant.conf" 7 | 8 | sudo nano /etc/wpa_supplicant/wpa_supplicant.conf 9 | -------------------------------------------------------------------------------- /c-rasp/ssd1306-i2c/README.md: -------------------------------------------------------------------------------- 1 | ## SSD1306_I2C 2 | 3 | SSD1306 I2C Driver for 128X64 OLED display connected to Raspberry Pi, based on [Wiring Pi](http://www.wiringpi.com/) 4 | 5 | ## Code Example 6 | 7 | hello_world.c: blink "hello world" on OLED display 8 | 9 | ## API Reference 10 | 11 | The codes are detailed commented. 12 | 13 | Usage of funtions can be found in ./ssd1306_i2c.h 14 | -------------------------------------------------------------------------------- /config-rasp/wpa-config-debian/wpa_supplicant.conf: -------------------------------------------------------------------------------- 1 | # WPA configs 2 | 3 | ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev 4 | update_config=1 5 | 6 | # Add networks 7 | # --------------------------------------------------------- 8 | 9 | network={ 10 | ssid="" 11 | psk="" 12 | key_mgmt=WPA-PSK 13 | } 14 | 15 | # --------------------------------------------------------- 16 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/test_led/test.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # Date : 4 | # Author : Xiang,Zuo 5 | # E-Mail : xianglinks@gmail.com 6 | # Github : stevelorenz 7 | 8 | import time 9 | import RPi.GPIO as GPIO 10 | 11 | GPIO.setmode(GPIO.BCM) 12 | GPIO.setup(12, GPIO.OUT) 13 | 14 | p = GPIO.PWM(12, 800000) 15 | 16 | p.start(0) 17 | 18 | try: 19 | while True: 20 | p.ChangeDutyCycle(1) 21 | 22 | except KeyboardInterrupt: 23 | pass 24 | p.stop(0) 25 | 26 | GPIO.cleanup() 27 | -------------------------------------------------------------------------------- /config-rasp/wpa-config-debian/interfaces: -------------------------------------------------------------------------------- 1 | # Interface configs /etc/networks/interfaces 2 | 3 | auto lo 4 | iface lo inet loopback 5 | 6 | auto eth0 7 | allow-hotplug eth0 8 | iface eth0 inet manual 9 | 10 | auto wlan0 11 | allow-hotplug wlan0 12 | 13 | # --- dynamic ip --- 14 | # iface wlan0 inet dhcp 15 | 16 | # --- static ip --- 17 | iface wlan0 inet static 18 | address 192.168.12.100 19 | netmask 255.255.255.0 20 | gateway 192.168.12.1 21 | 22 | wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf 23 | 24 | auto wlan1 25 | allow-hotplug wlan1 26 | iface wlan1 inet manual 27 | wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf 28 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/pwm_led/main.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : Hardware Support PWM on Rpi 3 | * Date : 2015-12-12 21:37:55 4 | * Author : Xiang, Zuo 5 | * Email : xianglinks@gmail.com 6 | ******************************************************************************/ 7 | 8 | /* ---------------- Code Section ---------------- */ 9 | .text 10 | .balign 4 11 | 12 | .global main 13 | .func main 14 | 15 | main: 16 | push {r4-r11, lr} 17 | 18 | end_main: 19 | mov r0, #0 20 | pop {r4-r11, pc} 21 | 22 | 23 | /* ---------------- Data Section ---------------- */ 24 | .data 25 | .balign 4 26 | 27 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/gpio_tests/blink/blink.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: 3 | > Author: Xiang,Zuo 4 | > Mail: xianglinks@gmail.com 5 | > Created Time: 6 | > Version: 0.1 7 | ************************************************************************/ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | int main(void) { 14 | int pin = 7; // define pin number 15 | if(wiringPiSetup() == -1 ) { 16 | printf("Setup did not work..."); 17 | } 18 | pinMode(pin, OUTPUT); // set the pin to output-mode 19 | while(1) { 20 | digitalWrite(pin, 1); 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /c-rasp/oled-udp/setup_env.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # --------------------------------------------------------- 3 | # About : Install tools and libs used for this experiment 4 | # Email : xianglinks@gmail.com 5 | # --------------------------------------------------------- 6 | 7 | echo "starting install dependencies" 8 | echo "including i2c-tools, wiringPi and cgdb" 9 | 10 | sudo apt-get update 11 | 12 | install="sudo apt-get install" 13 | 14 | $install i2c-tools 15 | $install cgdb 16 | 17 | echo "installing wiringPi lib" 18 | $install git 19 | git clone git://git.drogon.net/wiringPi ~/wiringPi 20 | cd ~/wiringPi || exit 21 | ./build 22 | 23 | echo "installation finished" 24 | echo "test wiringPi with /$ gpio readall" 25 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/gpio_tests/tools_debugging/main.S: -------------------------------------------------------------------------------- 1 | .extern sleep 2 | .extern map_peripheral 3 | .equ LED, 47 4 | .text 5 | .balign 4 6 | .global main 7 | .func main 8 | 9 | 10 | main: 11 | /* save registers and return address */ 12 | push {r4-r11, lr} 13 | /* call routine to initialize memory mapping */ 14 | bl map_peripheral 15 | mov r9, r0 16 | 17 | /* set GPIO 47 to output */ 18 | ldr r3, [r9, #(4 * (47/10))] 19 | and r3, #~(7 << (7*3)) 20 | str r3, [r9, #(4 * (47/10))] 21 | orr r3, #(1 << (7*3)) 22 | str r3, [r9, #(4 * (47/10))] 23 | 24 | /* set LED 47 */ 25 | movs r3, #(1 << (47-32)) 26 | str r3, [r9, #(4*8)] 27 | 28 | end_main: /* restore registers and return */ 29 | mov r0, #0 30 | pop {r4-r11, pc} 31 | -------------------------------------------------------------------------------- /py-rasp/temp-display/tmp64/tmp64.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | """ 4 | # About : Class for TMP64 temperature sensor 5 | # Date : 2016-03-17 6 | # Author : Xiang, Zuo 7 | # E-Mail : xianglinks@gmail.com 8 | """ 9 | 10 | from mcp3008 import MCP3008 11 | 12 | 13 | class TMP64(object): 14 | 15 | """Class for TMP64 temperature sensor""" 16 | 17 | def __init__(self, port=0, device=0): 18 | # creat object to control MCP3008 ADC 19 | self.mcp3008 = MCP3008(port, device) 20 | 21 | def readTmpValue(self, channel=0, places=1): 22 | """convert voltage to temperature value""" 23 | # get voltage from mcp3008 24 | volt = self.mcp3008.readVoltChannel(channel) 25 | # convert voltage to temperature 26 | voltm = volt * 1000 27 | temp = 25 + (voltm - 750) / float(10) 28 | # round the temp to specified number of decimal places 29 | temp = round(temp, places) 30 | return temp 31 | -------------------------------------------------------------------------------- /c-rasp/ssd1306-i2c/makefile: -------------------------------------------------------------------------------- 1 | # Compiler 2 | CC = gcc 3 | 4 | # Compiling options 5 | CFLAGS = -c -Wall 6 | 7 | # Option for Debug & Release 8 | ifeq ($(BUILD), debug) 9 | # "Debug" build - no optimization, and debugging symbols 10 | # $ make BUILD=debug 11 | CFLAGS += -O0 -g 12 | else 13 | # "Release" build - optimization, and no debug symbols 14 | # $ make 15 | CFLAGS += -O2 -s -DNDEBUG 16 | endif 17 | 18 | # Compiling header and libs 19 | HEADER = -I./ # header files 20 | LIBRA = -lwiringPi # lib in lib_path 21 | 22 | # Compiling rules 23 | # --------------------------------------------------------- 24 | all: hello-world 25 | 26 | hello-world: hello_world.o ssd1306_i2c.o 27 | $(CC) hello_world.o ssd1306_i2c.o $(LIBRA) -o hello-world 28 | 29 | hello_world.o: hello_world.c 30 | $(CC) $(CFLAGS) $(HEADER) hello_world.c 31 | 32 | ssd1306_i2c.o: ssd1306_i2c.c 33 | $(CC) $(CFLAGS) $(HEADER) ssd1306_i2c.c 34 | 35 | clean: 36 | rm *.o 37 | rm hello-world 38 | # --------------------------------------------------------- 39 | -------------------------------------------------------------------------------- /py-rasp/temp-display/docs/enable_spi.md: -------------------------------------------------------------------------------- 1 | # Enable SPI on Raspberry Pi 2 | 3 | The Raspberry Pi has a Serial Peripheral Interface (SPI) bus which can be enabled on [Pins](http://sur.ly/o/pinout.xyz/AA000014) 19,21,23,24 & 26. 4 | It is a synchronous serial data link standard and is used for short distance single master communication between devices. 5 | As far as the Pi is concerned this is usually relevant to certain sensors and add-on boards. 6 | 7 | The default Raspbian image disable SPI by default, so you should enable it before using. 8 | 9 | ## Enable SPI 10 | 11 | ####Method 1 - Using "Raspi-Config" 12 | 13 | `sudo raspi-config` 14 | 15 | advance -> SPI -> SPI kernel module loaded -> yes -> finish -> reboot 16 | 17 | ####Method 2 - Editing file manually 18 | 19 | `sudo nano /boot/config.txt` 20 | 21 | uncommit the following line in the file or add it at the end of file 22 | 23 | `dtparam=spi=on` 24 | 25 | save file and reboot using `sudo reboot` 26 | 27 | ## Check if SPI ist enabled 28 | 29 | check if SPI module is loaded by system using: 30 | 31 | `lsmod | grep spi_` 32 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/mcp_test/rpi.c: -------------------------------------------------------------------------------- 1 | #include "rpi.h" 2 | #include 3 | 4 | struct bcm2835_peripheral gpio = {GPIO_BASE}; 5 | 6 | // Exposes the physical address defined in the passed structure using mmap on /dev/mem 7 | volatile unsigned int * map_peripheral() 8 | { 9 | // Open /dev/mem 10 | if ((gpio.mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { 11 | printf("Failed to open /dev/mem, try checking permissions.\n"); 12 | return -1; 13 | } 14 | 15 | gpio.map = mmap( 16 | NULL, 17 | BLOCK_SIZE, 18 | PROT_READ|PROT_WRITE, 19 | MAP_SHARED, 20 | gpio.mem_fd, // File descriptor to physical memory virtual file '/dev/mem' 21 | gpio.addr_p // Address in physical map that we want this memory block to expose 22 | ); 23 | 24 | if (gpio.map == MAP_FAILED) { 25 | perror("mmap"); 26 | return NULL; 27 | } 28 | 29 | return gpio.addr = (volatile unsigned int *)gpio.map; 30 | } 31 | 32 | void unmap_peripheral() { 33 | munmap(gpio.map, BLOCK_SIZE); 34 | close(gpio.mem_fd); 35 | } 36 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/baseboard_test/rpi.c: -------------------------------------------------------------------------------- 1 | #include "rpi.h" 2 | #include 3 | 4 | struct bcm2835_peripheral gpio = {GPIO_BASE}; 5 | 6 | // Exposes the physical address defined in the passed structure using mmap on /dev/mem 7 | volatile unsigned int * map_peripheral() 8 | { 9 | // Open /dev/mem 10 | if ((gpio.mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { 11 | printf("Failed to open /dev/mem, try checking permissions.\n"); 12 | return -1; 13 | } 14 | 15 | gpio.map = mmap( 16 | NULL, 17 | BLOCK_SIZE, 18 | PROT_READ|PROT_WRITE, 19 | MAP_SHARED, 20 | gpio.mem_fd, // File descriptor to physical memory virtual file '/dev/mem' 21 | gpio.addr_p // Address in physical map that we want this memory block to expose 22 | ); 23 | 24 | if (gpio.map == MAP_FAILED) { 25 | perror("mmap"); 26 | return NULL; 27 | } 28 | 29 | return gpio.addr = (volatile unsigned int *)gpio.map; 30 | } 31 | 32 | void unmap_peripheral() { 33 | munmap(gpio.map, BLOCK_SIZE); 34 | close(gpio.mem_fd); 35 | } 36 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/gpio_tests/setPinOne/rpi.c: -------------------------------------------------------------------------------- 1 | #include "rpi.h" 2 | #include 3 | 4 | struct bcm2835_peripheral gpio = {GPIO_BASE}; 5 | 6 | // Exposes the physical address defined in the passed structure using mmap on /dev/mem 7 | volatile unsigned int * map_peripheral() 8 | { 9 | // Open /dev/mem 10 | if ((gpio.mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { 11 | printf("Failed to open /dev/mem, try checking permissions.\n"); 12 | return -1; 13 | } 14 | 15 | gpio.map = mmap( 16 | NULL, 17 | BLOCK_SIZE, 18 | PROT_READ|PROT_WRITE, 19 | MAP_SHARED, 20 | gpio.mem_fd, // File descriptor to physical memory virtual file '/dev/mem' 21 | gpio.addr_p // Address in physical map that we want this memory block to expose 22 | ); 23 | 24 | if (gpio.map == MAP_FAILED) { 25 | perror("mmap"); 26 | return NULL; 27 | } 28 | 29 | return gpio.addr = (volatile unsigned int *)gpio.map; 30 | } 31 | 32 | void unmap_peripheral() { 33 | munmap(gpio.map, BLOCK_SIZE); 34 | close(gpio.mem_fd); 35 | } 36 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/pwm_led/rpi.c: -------------------------------------------------------------------------------- 1 | #include "rpi.h" 2 | #include 3 | 4 | struct bcm2835_peripheral gpio = {GPIO_BASE}; 5 | 6 | /** 7 | * @brief map_peripheral 8 | * Exposes the physical address defined in the passed structure using mmap on /dev/mem 9 | * @return 10 | */ 11 | volatile unsigned int * map_peripheral() 12 | { 13 | // Open /dev/mem 14 | if ((gpio.mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { 15 | printf("Failed to open /dev/mem, try checking permissions.\n"); 16 | return -1; 17 | } 18 | 19 | gpio.map = mmap( 20 | NULL, 21 | BLOCK_SIZE, 22 | PROT_READ|PROT_WRITE, 23 | MAP_SHARED, 24 | gpio.mem_fd, // File descriptor to physical memory virtual file '/dev/mem' 25 | gpio.addr_p // Address in physical map that we want this memory block to expose 26 | ); 27 | 28 | if (gpio.map == MAP_FAILED) { 29 | perror("mmap"); 30 | return NULL; 31 | } 32 | 33 | return gpio.addr = (volatile unsigned int *)gpio.map; 34 | } 35 | 36 | void unmap_peripheral() { 37 | munmap(gpio.map, BLOCK_SIZE); 38 | close(gpio.mem_fd); 39 | } 40 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/button_test/rpi.c: -------------------------------------------------------------------------------- 1 | #include "rpi.h" 2 | #include 3 | 4 | struct bcm2835_peripheral gpio = {GPIO_BASE}; 5 | 6 | /** 7 | * @brief map_peripheral 8 | * Exposes the physical address defined in the passed structure using mmap on /dev/mem 9 | * @return 10 | */ 11 | volatile unsigned int * map_peripheral() 12 | { 13 | // Open /dev/mem 14 | if ((gpio.mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { 15 | printf("Failed to open /dev/mem, try checking permissions.\n"); 16 | return -1; 17 | } 18 | 19 | gpio.map = mmap( 20 | NULL, 21 | BLOCK_SIZE, 22 | PROT_READ|PROT_WRITE, 23 | MAP_SHARED, 24 | gpio.mem_fd, // File descriptor to physical memory virtual file '/dev/mem' 25 | gpio.addr_p // Address in physical map that we want this memory block to expose 26 | ); 27 | 28 | if (gpio.map == MAP_FAILED) { 29 | perror("mmap"); 30 | return NULL; 31 | } 32 | 33 | return gpio.addr = (volatile unsigned int *)gpio.map; 34 | } 35 | 36 | void unmap_peripheral() { 37 | munmap(gpio.map, BLOCK_SIZE); 38 | close(gpio.mem_fd); 39 | } 40 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/calc_led/rpi.c: -------------------------------------------------------------------------------- 1 | #include "rpi.h" 2 | #include 3 | 4 | struct bcm2835_peripheral gpio = {GPIO_BASE}; 5 | 6 | /** 7 | * @brief map_peripheral 8 | * Exposes the physical address defined in the passed structure using mmap on /dev/mem 9 | * @return 10 | */ 11 | volatile unsigned int * map_peripheral() 12 | { 13 | // Open /dev/mem 14 | if ((gpio.mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { 15 | printf("Failed to open /dev/mem, try checking permissions.\n"); 16 | return -1; 17 | } 18 | 19 | gpio.map = mmap( 20 | NULL, 21 | BLOCK_SIZE, 22 | PROT_READ|PROT_WRITE, 23 | MAP_SHARED, 24 | gpio.mem_fd, // File descriptor to physical memory virtual file '/dev/mem' 25 | gpio.addr_p // Address in physical map that we want this memory block to expose 26 | ); 27 | 28 | if (gpio.map == MAP_FAILED) { 29 | perror("mmap"); 30 | return NULL; 31 | } 32 | 33 | return gpio.addr = (volatile unsigned int *)gpio.map; 34 | } 35 | 36 | void unmap_peripheral() { 37 | munmap(gpio.map, BLOCK_SIZE); 38 | close(gpio.mem_fd); 39 | } 40 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/input_test/rpi.c: -------------------------------------------------------------------------------- 1 | #include "rpi.h" 2 | #include 3 | 4 | struct bcm2835_peripheral gpio = {GPIO_BASE}; 5 | 6 | /** 7 | * @brief map_peripheral 8 | * Exposes the physical address defined in the passed structure using mmap on /dev/mem 9 | * @return 10 | */ 11 | volatile unsigned int * map_peripheral() 12 | { 13 | // Open /dev/mem 14 | if ((gpio.mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { 15 | printf("Failed to open /dev/mem, try checking permissions.\n"); 16 | return -1; 17 | } 18 | 19 | gpio.map = mmap( 20 | NULL, 21 | BLOCK_SIZE, 22 | PROT_READ|PROT_WRITE, 23 | MAP_SHARED, 24 | gpio.mem_fd, // File descriptor to physical memory virtual file '/dev/mem' 25 | gpio.addr_p // Address in physical map that we want this memory block to expose 26 | ); 27 | 28 | if (gpio.map == MAP_FAILED) { 29 | perror("mmap"); 30 | return NULL; 31 | } 32 | 33 | return gpio.addr = (volatile unsigned int *)gpio.map; 34 | } 35 | 36 | void unmap_peripheral() { 37 | munmap(gpio.map, BLOCK_SIZE); 38 | close(gpio.mem_fd); 39 | } 40 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/led_rhythm/rpi.c: -------------------------------------------------------------------------------- 1 | #include "rpi.h" 2 | #include 3 | 4 | struct bcm2835_peripheral gpio = {GPIO_BASE}; 5 | 6 | /** 7 | * @brief map_peripheral 8 | * Exposes the physical address defined in the passed structure using mmap on /dev/mem 9 | * @return 10 | */ 11 | volatile unsigned int * map_peripheral() 12 | { 13 | // Open /dev/mem 14 | if ((gpio.mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { 15 | printf("Failed to open /dev/mem, try checking permissions.\n"); 16 | return -1; 17 | } 18 | 19 | gpio.map = mmap( 20 | NULL, 21 | BLOCK_SIZE, 22 | PROT_READ|PROT_WRITE, 23 | MAP_SHARED, 24 | gpio.mem_fd, // File descriptor to physical memory virtual file '/dev/mem' 25 | gpio.addr_p // Address in physical map that we want this memory block to expose 26 | ); 27 | 28 | if (gpio.map == MAP_FAILED) { 29 | perror("mmap"); 30 | return NULL; 31 | } 32 | 33 | return gpio.addr = (volatile unsigned int *)gpio.map; 34 | } 35 | 36 | void unmap_peripheral() { 37 | munmap(gpio.map, BLOCK_SIZE); 38 | close(gpio.mem_fd); 39 | } 40 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/pwm_test/rpi.c: -------------------------------------------------------------------------------- 1 | #include "rpi.h" 2 | #include 3 | 4 | struct bcm2835_peripheral gpio = {GPIO_BASE}; 5 | 6 | /** 7 | * @brief map_peripheral 8 | * Exposes the physical address defined in the passed structure using mmap on /dev/mem 9 | * @return 10 | */ 11 | volatile unsigned int * map_peripheral() 12 | { 13 | // Open /dev/mem 14 | if ((gpio.mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { 15 | printf("Failed to open /dev/mem, try checking permissions.\n"); 16 | return -1; 17 | } 18 | 19 | gpio.map = mmap( 20 | NULL, 21 | BLOCK_SIZE, 22 | PROT_READ|PROT_WRITE, 23 | MAP_SHARED, 24 | gpio.mem_fd, // File descriptor to physical memory virtual file '/dev/mem' 25 | gpio.addr_p // Address in physical map that we want this memory block to expose 26 | ); 27 | 28 | if (gpio.map == MAP_FAILED) { 29 | perror("mmap"); 30 | return NULL; 31 | } 32 | 33 | return gpio.addr = (volatile unsigned int *)gpio.map; 34 | } 35 | 36 | void unmap_peripheral() { 37 | munmap(gpio.map, BLOCK_SIZE); 38 | close(gpio.mem_fd); 39 | } 40 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/temp_mess/rpi.c: -------------------------------------------------------------------------------- 1 | #include "rpi.h" 2 | #include 3 | 4 | struct bcm2835_peripheral gpio = {GPIO_BASE}; 5 | 6 | /** 7 | * @brief map_peripheral 8 | * Exposes the physical address defined in the passed structure using mmap on /dev/mem 9 | * @return 10 | */ 11 | volatile unsigned int * map_peripheral() 12 | { 13 | // Open /dev/mem 14 | if ((gpio.mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { 15 | printf("Failed to open /dev/mem, try checking permissions.\n"); 16 | return -1; 17 | } 18 | 19 | gpio.map = mmap( 20 | NULL, 21 | BLOCK_SIZE, 22 | PROT_READ|PROT_WRITE, 23 | MAP_SHARED, 24 | gpio.mem_fd, // File descriptor to physical memory virtual file '/dev/mem' 25 | gpio.addr_p // Address in physical map that we want this memory block to expose 26 | ); 27 | 28 | if (gpio.map == MAP_FAILED) { 29 | perror("mmap"); 30 | return NULL; 31 | } 32 | 33 | return gpio.addr = (volatile unsigned int *)gpio.map; 34 | } 35 | 36 | void unmap_peripheral() { 37 | munmap(gpio.map, BLOCK_SIZE); 38 | close(gpio.mem_fd); 39 | } 40 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/interaption_test/rpi.c: -------------------------------------------------------------------------------- 1 | #include "rpi.h" 2 | #include 3 | 4 | struct bcm2835_peripheral gpio = {GPIO_BASE}; 5 | 6 | /** 7 | * @brief map_peripheral 8 | * Exposes the physical address defined in the passed structure using mmap on /dev/mem 9 | * @return 10 | */ 11 | volatile unsigned int * map_peripheral() 12 | { 13 | // Open /dev/mem 14 | if ((gpio.mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { 15 | printf("Failed to open /dev/mem, try checking permissions.\n"); 16 | return -1; 17 | } 18 | 19 | gpio.map = mmap( 20 | NULL, 21 | BLOCK_SIZE, 22 | PROT_READ|PROT_WRITE, 23 | MAP_SHARED, 24 | gpio.mem_fd, // File descriptor to physical memory virtual file '/dev/mem' 25 | gpio.addr_p // Address in physical map that we want this memory block to expose 26 | ); 27 | 28 | if (gpio.map == MAP_FAILED) { 29 | perror("mmap"); 30 | return NULL; 31 | } 32 | 33 | return gpio.addr = (volatile unsigned int *)gpio.map; 34 | } 35 | 36 | void unmap_peripheral() { 37 | munmap(gpio.map, BLOCK_SIZE); 38 | close(gpio.mem_fd); 39 | } 40 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/button_led_control/rpi.c: -------------------------------------------------------------------------------- 1 | #include "rpi.h" 2 | #include 3 | 4 | struct bcm2835_peripheral gpio = {GPIO_BASE}; 5 | 6 | /** 7 | * @brief map_peripheral 8 | * Exposes the physical address defined in the passed structure using mmap on /dev/mem 9 | * @return 10 | */ 11 | volatile unsigned int * map_peripheral() 12 | { 13 | // Open /dev/mem 14 | if ((gpio.mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { 15 | printf("Failed to open /dev/mem, try checking permissions.\n"); 16 | return -1; 17 | } 18 | 19 | gpio.map = mmap( 20 | NULL, 21 | BLOCK_SIZE, 22 | PROT_READ|PROT_WRITE, 23 | MAP_SHARED, 24 | gpio.mem_fd, // File descriptor to physical memory virtual file '/dev/mem' 25 | gpio.addr_p // Address in physical map that we want this memory block to expose 26 | ); 27 | 28 | if (gpio.map == MAP_FAILED) { 29 | perror("mmap"); 30 | return NULL; 31 | } 32 | 33 | return gpio.addr = (volatile unsigned int *)gpio.map; 34 | } 35 | 36 | void unmap_peripheral() { 37 | munmap(gpio.map, BLOCK_SIZE); 38 | close(gpio.mem_fd); 39 | } 40 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/gpio_tests/tools_debugging/rpi.c: -------------------------------------------------------------------------------- 1 | #include "rpi.h" 2 | #include 3 | 4 | struct bcm2835_peripheral gpio = {GPIO_BASE}; 5 | 6 | /** 7 | * @brief map_peripheral 8 | * Exposes the physical address defined in the passed structure using mmap on /dev/mem 9 | * @return 10 | */ 11 | volatile unsigned int * map_peripheral() 12 | { 13 | // Open /dev/mem 14 | if ((gpio.mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { 15 | printf("Failed to open /dev/mem, try checking permissions.\n"); 16 | return -1; 17 | } 18 | 19 | gpio.map = mmap( 20 | NULL, 21 | BLOCK_SIZE, 22 | PROT_READ|PROT_WRITE, 23 | MAP_SHARED, 24 | gpio.mem_fd, // File descriptor to physical memory virtual file '/dev/mem' 25 | gpio.addr_p // Address in physical map that we want this memory block to expose 26 | ); 27 | 28 | if (gpio.map == MAP_FAILED) { 29 | perror("mmap"); 30 | return NULL; 31 | } 32 | 33 | return gpio.addr = (volatile unsigned int *)gpio.map; 34 | } 35 | 36 | void unmap_peripheral() { 37 | munmap(gpio.map, BLOCK_SIZE); 38 | close(gpio.mem_fd); 39 | } 40 | -------------------------------------------------------------------------------- /c-rasp/oled-udp/makefile: -------------------------------------------------------------------------------- 1 | # Compiler 2 | CC = gcc 3 | 4 | # Compiling options 5 | CFLAGS = -c -Wall 6 | 7 | # Option for Debug & Release 8 | ifeq ($(BUILD), debug) 9 | # "Debug" build - no optimization, and debugging symbols 10 | # $ make BUILD=debug 11 | CFLAGS += -O0 -g 12 | else 13 | # "Release" build - optimization, and no debug symbols 14 | # $ make 15 | CFLAGS += -O2 -s -DNDEBUG 16 | endif 17 | 18 | # Compiling header and libs 19 | HEADER = -I. # header files 20 | LIBRA = -lwiringPi -lm # lib in lib_path 21 | 22 | # Compiling rules 23 | # --------------------------------------------------------- 24 | all: server-udp client-udp 25 | 26 | server-udp: server.o ssd1306_i2c.o 27 | $(CC) server.o ssd1306_i2c.o $(LIBRA) -o server-udp 28 | 29 | server.o: server.c 30 | $(CC) $(CFLAGS) $(HEADER) server.c 31 | 32 | ssd1306_i2c.o: ssd1306_i2c.c 33 | $(CC) $(CFLAGS) $(HEADER) ssd1306_i2c.c 34 | 35 | client-udp: client.o 36 | $(CC) client.o $(LIBRA) -o client-udp 37 | 38 | client.o: client.c 39 | $(CC) $(CFLAGS) client.c 40 | 41 | clean: 42 | rm *.o 43 | rm server-udp 44 | rm client-udp 45 | # --------------------------------------------------------- 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Codes On Raspberry Pi 2 | 3 | Some test-codes and small projects for hardware-related programming on Raspberry Pi (including with ASM , C and Python). 4 | 5 | ## Motivation 6 | 7 | These codes were written and tested for practical project-works of the micro computing technology lecture(MRT) at TU 8 | Dresden. 9 | 10 | Now it is developed mainly for learning and practising related knowledges as well as programming skills. 11 | 12 | ## Contents 13 | 14 | Codes and projects are firstly classified according to its developed language. 15 | 16 | ####1. ASM-Rasp 17 | 18 | - [MRT-Uebung](./asm-rasp/mrt_experiment/): For tests and expriments of MRT course. 19 | 20 | 21 | ####2. C-Rasp: 22 | 23 | - [ssd1306-i2c](./c-rasp/ssd1306-i2c): A SSD1306 driver program for 128X64 OLED display with I2C interface. 24 | 25 | ####3. Py-Rasp: 26 | 27 | - [comm-remote-host](./py-rasp/comm-remote-host/): Tools to communicate with remote hosts. e.g. Send its IP via email. 28 | 29 | ####4. Config-Rasp: Backup of config files and helper shell scripts. 30 | 31 | ## Main Reference 32 | 33 | - [Wiring Pi Homepage](http://wiringpi.com/) 34 | 35 | 36 | ## Contact 37 | 38 | - Zuo Xiang: xianglinks@gmail.com 39 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/mcp_test/read_mcp.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > About: Read Temperature on MCP3008 with Sensor 3 | > Author: Xiang,Zuo 4 | > Mail: xianglinks@gmail.com 5 | > Created Time: 6 | > Version: 0.1 7 | ************************************************************************/ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | 16 | #define BASE 200 17 | #define SPI_CHAN 0 18 | 19 | int main(void) { 20 | int chan; 21 | int x; 22 | printf ("Raspberry Pi wiringPi test program\n") ; 23 | 24 | if (wiringPiSetup () == -1) 25 | exit (1) ; 26 | 27 | mcp3004Setup (BASE, SPI_CHAN); // 3004 and 3008 are the same 4/8 channels 28 | 29 | /* for (chan = 0 ; chan < 8 ; ++chan) { */ 30 | /* x = analogRead(BASE + chan) ; */ 31 | /* printf("%d\n", x); */ 32 | /* } */ 33 | 34 | while(1) { 35 | x = analogRead(200); 36 | float volts = (x * 3.3) / 1024.0; 37 | float temperature; 38 | temperature = (volts - 0.5) * 100; 39 | printf("The Temperature is %f Celsius\n", temperature); 40 | sleep(3); 41 | } 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /py-rasp/temp-display/mcp3008/logger.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | """ 4 | # About : Logger 5 | # Date : 2016-03-09 6 | # Author : Xiang,Zuo 7 | # E-Mail : xianglinks@gmail.com 8 | """ 9 | 10 | import logging 11 | 12 | # creat logger 13 | logger = logging.getLogger("display_logger") 14 | logger.setLevel(logging.DEBUG) 15 | 16 | # --- creat handlers --- 17 | # --------------------------------- 18 | # handler for logging in terminal 19 | sh = logging.StreamHandler() 20 | sh.setLevel(logging.INFO) 21 | # handler for logging in file 22 | log_file_path = './logfile.log' 23 | fh = logging.FileHandler(log_file_path) 24 | fh.setLevel(logging.WARNING) 25 | # --------------------------------- 26 | 27 | # --- create formatter --- 28 | # --------------------------------- 29 | # formatter for terminal handler 30 | shfmt = logging.Formatter('%(message)s') 31 | sh.setFormatter(shfmt) 32 | 33 | # formatter for file handler 34 | fmt = "%(asctime)-15s %(levelname)s %(filename)s %(lineno)d %(process)d %(message)s" 35 | datefmt = "%a %d %b %Y %H:%M:%S" 36 | fhfmt = logging.Formatter(fmt, datefmt) 37 | fh.setFormatter(fhfmt) 38 | # --------------------------------- 39 | 40 | # --- add handler to logger --- 41 | logger.addHandler(sh) 42 | logger.addHandler(fh) 43 | -------------------------------------------------------------------------------- /py-rasp/temp-display/oled/logger.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | """ 4 | # About : Logger 5 | # Date : 2016-03-09 6 | # Author : Xiang,Zuo 7 | # E-Mail : xianglinks@gmail.com 8 | """ 9 | 10 | import logging 11 | 12 | # creat logger 13 | logger = logging.getLogger("display_logger") 14 | logger.setLevel(logging.DEBUG) 15 | 16 | # --- creat handlers --- 17 | # --------------------------------- 18 | # handler for logging in terminal 19 | sh = logging.StreamHandler() 20 | sh.setLevel(logging.INFO) 21 | # handler for logging in file 22 | log_file_path = './display.log' 23 | fh = logging.FileHandler(log_file_path) 24 | fh.setLevel(logging.WARNING) 25 | # --------------------------------- 26 | 27 | # --- create formatter --- 28 | # --------------------------------- 29 | # formatter for terminal handler 30 | shfmt = logging.Formatter('%(message)s') 31 | sh.setFormatter(shfmt) 32 | 33 | # formatter for file handler 34 | fmt = "%(asctime)-15s %(levelname)s %(filename)s %(lineno)d %(process)d %(message)s" 35 | datefmt = "%a %d %b %Y %H:%M:%S" 36 | fhfmt = logging.Formatter(fmt, datefmt) 37 | fh.setFormatter(fhfmt) 38 | # --------------------------------- 39 | 40 | # --- add handler to logger --- 41 | logger.addHandler(sh) 42 | logger.addHandler(fh) 43 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/temp_mess/read_temp.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > About: Get the digitized value of temp-sensor on Rpi with MCP3008-ADC 3 | > Author: Xiang,Zuo 4 | > Mail: xianglinks@gmail.com 5 | > Created Time: 2015-12-04 15:07:52 6 | > Version: 1.0 7 | ************************************************************************/ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #define BASE 200 15 | #define SPI_CHAN 0 16 | 17 | /** 18 | * @brief init_mcp 19 | * init wiringpi settings and MCP3008-ADC 20 | * this function should be called by initialisation only once (avoiding pin overlapping) 21 | */ 22 | void init_mcp(void) { 23 | if(wiringPiSetup () == -1) { 24 | printf("init do not work. aborting...\n"); 25 | exit(1); 26 | } 27 | // init MCP3008-ADC(using SPI-Dev on channel 0) 28 | mcp3004Setup (BASE, SPI_CHAN); 29 | } 30 | 31 | /** 32 | * @brief read_temp 33 | * read the digitized temp-value from sensor 34 | * 35 | * @return 36 | * int temp 37 | */ 38 | int read_temp(void) { 39 | int value; 40 | float volts, temp; 41 | value = analogRead(BASE); 42 | // caculate the temperature 43 | volts = (value * 3.3) / 1024.0; 44 | temp = (volts - 0.5) * 100; 45 | // convert float temp-value to a integer 46 | temp = (int)temp; 47 | return temp; 48 | } 49 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/input_test/read_temp.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > About: Read the temperature on Raspberry-Pi with MCP3008-ADC 3 | > Author: Xiang,Zuo 4 | > Mail: xianglinks@gmail.com 5 | > Created Time: 2015-12-04 15:07:52 6 | > Version: 0.3 7 | ************************************************************************/ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #define BASE 200 15 | #define SPI_CHAN 0 16 | 17 | /** 18 | * @brief init_mcp 19 | * Init wiringPi Setttings and MCP3008 ADC 20 | * This should run at the begin only once (avoid Pin overlapping) 21 | */ 22 | void init_mcp(void) { 23 | // init gpio for wiringPi 24 | if(wiringPiSetup () == -1) { 25 | printf("Init do not work. Aborting...\n"); 26 | exit(1); 27 | } 28 | 29 | // init MCP3008(using SPI-Dev on channel 0) 30 | mcp3004Setup (BASE, SPI_CHAN); 31 | } 32 | 33 | 34 | /** 35 | * @brief read_temp 36 | * Read the temperature from sensor 37 | * 38 | * @return 39 | * int temp 40 | */ 41 | int read_temp(void) { 42 | int value; 43 | float volts, temp; 44 | // get the digitalized value 45 | value = analogRead(200); 46 | // caculate the temperature 47 | volts = (value * 3.3) / 1024.0; 48 | temp = (volts - 0.5) * 100; 49 | // convert temperature to a integer 50 | temp = (int)temp; 51 | return temp; 52 | } 53 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/gpio_tests/blink/setOne.S: -------------------------------------------------------------------------------- 1 | /* 2 | * About : Set a pin of GPIO to 1 3 | * Date : 2015-12-01 22:15:50 4 | * Author : Xiang, Zuo 5 | * Email : xianglinks@gmail.com 6 | */ 7 | 8 | /* -- Data Section -- */ 9 | .data 10 | .balign 4 11 | 12 | /* define for strings */ 13 | stMsg: .asciz "Set pin using wiring Pi\n" 14 | errMsg: .asciz "Setup do not work Aborting...\n" 15 | sucMsg: .asciz "Pin 7 is now set to 1\n" 16 | 17 | pin: .int 7 18 | OUTPUT = 1 19 | 20 | 21 | /* -- Code Section -- */ 22 | .text 23 | .balign 4 24 | .global main 25 | .func main 26 | /* assignment for extern functions */ 27 | .extern printf 28 | .extern wiringPiSetup /* init the pin settings */ 29 | .extern pinMode /* set pin-mode: input or output*/ 30 | .extern digitalWrite /* write digital data into pin */ 31 | 32 | main: 33 | /* for function_call */ 34 | push {ip, lr} 35 | bl wiringPiSetup /* call wiringPiSetup Function */ 36 | mov r1, #-1 37 | cmp r1, r0 38 | bne init 39 | /* if Setup is wrong: quit the programm */ 40 | ldr r0, =errMsg 41 | bl printf 42 | b done 43 | 44 | init: 45 | ldr r0, =pin 46 | ldr r0, [r0] /* get the addr of pin*/ 47 | mov r1, #OUTPUT 48 | bl pinMode 49 | 50 | /* set the pin to 1 */ 51 | ldr r0, =pin 52 | ldr r0, [r0] 53 | mov r1, #1 54 | bl digitalWrite 55 | ldr r0, =sucMsg 56 | bl printf 57 | 58 | /* a always loop for test*/ 59 | loopA: 60 | b loopA 61 | 62 | done: 63 | pop {ip, pc} /* return the addr into pc */ 64 | -------------------------------------------------------------------------------- /py-rasp/temp-display/client.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | """ 4 | # About : UDP Clinet 5 | Read temperature value using TMP64 and send it using UDP 6 | # Date : 2016-03-17 14:59 7 | # Author : Xiang,Zuo 8 | # E-Mail : xianglinks@gmail.com 9 | """ 10 | 11 | import socket 12 | from sys import exit 13 | from time import sleep 14 | 15 | from tmp64 import TMP64 16 | 17 | 18 | # --- main function --- 19 | def main(): 20 | try: 21 | # init UDP socket 22 | UDP_IP = '127.0.0.1' 23 | UDP_PORT = 8008 24 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 25 | print('creat socket with ip %s and port %d' % (UDP_IP, UDP_PORT)) 26 | 27 | # creat tmp object 28 | # info about SPI device can be found in path: /dev/spidevx.x 29 | # i.e. /dev/spidev0.0 for port 0 an device 0 30 | tmp = TMP64(port=0, device=0) 31 | 32 | delay = 2 # define delay(seconds) between the measurements 33 | 34 | # --- loop for temperature measurements --- 35 | print('start send temperature message') 36 | while True: 37 | tmpValue = tmp.readTmpValue() # get temperature value 38 | tmpValueStr = str(tmpValue) 39 | sock.sendto(tmpValueStr, (UDP_IP, UDP_PORT)) # send tmpValue 40 | sleep(delay) 41 | 42 | except KeyboardInterrupt: 43 | print('\nKeyboardInterrupt, exit') 44 | exit(0) 45 | 46 | if __name__ == "__main__": 47 | main() 48 | -------------------------------------------------------------------------------- /c-rasp/ssd1306-i2c/hello_world.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | * About : Example Programm 3 | * Blink "hello world" on OLED Display 4 | * Author : Xiang,Zuo 5 | * Mail : xianglinks@gmail.com 6 | * Date : 2016-03-22 7 | * Ver : 0.1 8 | **************************************************************************/ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "ssd1306_i2c.h" 16 | 17 | // flag for keyboard interrupt 18 | static volatile int keepRunning = 1; 19 | 20 | // interrupt signal handler 21 | void intHandler(int sig) { 22 | keepRunning = 0; 23 | } 24 | 25 | // --- main function --- 26 | int main(int argc, char* argv[]) 27 | { 28 | /* catch interrupt signal(SIGINT), i.e. ctrl+c 29 | * if interrupt detected -> keepRunning = 0 30 | * */ 31 | signal(SIGINT, intHandler); 32 | 33 | int fd; // file descriptor for I2C writing 34 | fd = ssd1306I2CSetup(0x3C); // init SSD1306 and I2C interface 35 | displayOn(fd); // turn on display 36 | 37 | // -- main loop -- 38 | while(keepRunning) { 39 | // turn on display 40 | draw_line(1, 1, "hello"); 41 | draw_line(2, 1, "world"); 42 | updateDisplay(fd); 43 | sleep(1); 44 | clearDisplay(fd); 45 | } 46 | 47 | // --- routine when interrupt detected --- 48 | clearDisplay(fd); 49 | draw_line(2, 1, "off in 2 sec"); 50 | updateDisplay(fd); 51 | sleep(2); 52 | displayOff(fd); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/util_functions/unsigned_division.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : Caculate the 32-bit unsigned division 3 | * Date : 2015-12-16 17:50:16 4 | * Author : Xiang,Zuo 5 | * Email : xianglinks@gmail.com 6 | ******************************************************************************/ 7 | 8 | /* ---------------- Code Section ---------------- */ 9 | .text 10 | .balign 4 11 | 12 | /* Args: 13 | * r0 contains numerator 14 | * r1 contains divisor 15 | * r2 contains the quotient */ 16 | .global unsigned_division 17 | .func unsigned_division 18 | unsigned_division: 19 | mov r3, r1 /* r3 <- r1 */ 20 | cmp r3, r0, LSR #1 /* update cpsr with r3 - r0 / 2 */ 21 | .Lloop2: 22 | movls r3, r3, LSL #1 /* if r3 <= 2 * r0 ( C=0 or Z=1 ) then r3 <- r3 * 2 */ 23 | cmp r3, r0, LSR #1 /* update cpsr with r3 - (r0 / 2) */ 24 | bls .Lloop2 /* branch to .Lloop2 if r3 <= 2 * r0 ( C=0 or Z=1 ) */ 25 | 26 | mov r2, #0 /* init quotient r2 <- 0 */ 27 | 28 | .Lloop3: 29 | cmp r0, r3 /* update cpsr with r0 - r3 */ 30 | subhs r0, r0, r3 /* if r0 >= r3 ( C=1 ) then r0 <- r0 - r3 */ 31 | adc r2, r2, r2 /* r2 <- r2 + r2 + C. 32 | Note that if r0 >= r3 then C=1, C=0 otherwise */ 33 | 34 | mov r3, r3, LSR #1 /* r3 <- r3 / 2 */ 35 | cmp r3, r1 /* update cpsr with r3 - r1 */ 36 | bhs .Lloop3 /* if r3 >= r1 branch to .Lloop3 */ 37 | bx lr 38 | .endfunc 39 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/pwm_led/rpi.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | //RPi 1 #define BCM2708_PERI_BASE 0x20000000 10 | #define BCM2708_PERI_BASE 0x3f000000 11 | 12 | #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) // GPIO controller 13 | 14 | #define BLOCK_SIZE (4*1024) 15 | 16 | // IO Access 17 | struct bcm2835_peripheral { 18 | unsigned long addr_p; /* the physical base address */ 19 | int mem_fd; /* the file descriptor for /dev/mem */ 20 | void *map; /* the pointer to the map from mmap for later use with munmap */ 21 | volatile unsigned int *addr; /* the address in user space where we will find the mapped memory */ 22 | }; 23 | 24 | extern struct bcm2835_peripheral gpio; // They have to be found somewhere, but can't be in the header 25 | 26 | // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) 27 | #define READ_GPIO(g) *(gpio.addr + ((g)/10)) 28 | #define INP_GPIO(g) *(gpio.addr + ((g)/10)) &= ~(7<<(((g)%10)*3)) 29 | #define OUT_GPIO(g) *(gpio.addr + ((g)/10)) |= (1<<(((g)%10)*3)) 30 | #define SET_GPIO_ALT(g,a) *(gpio.addr + (((g)/10))) |= (((a)<=3?(a) + 4:(a)==4?3:2)<<(((g)%10)*3)) 31 | 32 | #define GPIO_SET *(gpio.addr + 7) // sets bits which are 1 ignores bits which are 0 33 | #define GPIO_SET2 *(gpio.addr + 8) // sets bits which are 1 ignores bits which are 0 34 | #define GPIO_CLR *(gpio.addr + 10) // clears bits which are 1 ignores bits which are 0 35 | #define GPIO_CLR2 *(gpio.addr + 11) // clears bits which are 1 ignores bits which are 0 36 | 37 | #define GPIO_READ(g) *(gpio.addr + 13) &= (1<<(g)) 38 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/mcp_test/rpi.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | //RPi 1 #define BCM2708_PERI_BASE 0x20000000 10 | #define BCM2708_PERI_BASE 0x3f000000 11 | 12 | #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) // GPIO controller 13 | 14 | #define BLOCK_SIZE (4*1024) 15 | 16 | // IO Access 17 | struct bcm2835_peripheral { 18 | unsigned long addr_p; /* the physical base address */ 19 | int mem_fd; /* the file descriptor for /dev/mem */ 20 | void *map; /* the pointer to the map from mmap for later use with munmap */ 21 | volatile unsigned int *addr; /* the address in user space where we will find the mapped memory */ 22 | }; 23 | 24 | extern struct bcm2835_peripheral gpio; // They have to be found somewhere, but can't be in the header 25 | 26 | // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) 27 | #define READ_GPIO(g) *(gpio.addr + ((g)/10)) 28 | #define INP_GPIO(g) *(gpio.addr + ((g)/10)) &= ~(7<<(((g)%10)*3)) 29 | #define OUT_GPIO(g) *(gpio.addr + ((g)/10)) |= (1<<(((g)%10)*3)) 30 | #define SET_GPIO_ALT(g,a) *(gpio.addr + (((g)/10))) |= (((a)<=3?(a) + 4:(a)==4?3:2)<<(((g)%10)*3)) 31 | 32 | #define GPIO_SET *(gpio.addr + 7) // sets bits which are 1 ignores bits which are 0 33 | #define GPIO_SET2 *(gpio.addr + 8) // sets bits which are 1 ignores bits which are 0 34 | #define GPIO_CLR *(gpio.addr + 10) // clears bits which are 1 ignores bits which are 0 35 | #define GPIO_CLR2 *(gpio.addr + 11) // clears bits which are 1 ignores bits which are 0 36 | 37 | #define GPIO_READ(g) *(gpio.addr + 13) &= (1<<(g)) 38 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/baseboard_test/rpi.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | //RPi 1 #define BCM2708_PERI_BASE 0x20000000 10 | #define BCM2708_PERI_BASE 0x3f000000 11 | 12 | #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) // GPIO controller 13 | 14 | #define BLOCK_SIZE (4*1024) 15 | 16 | // IO Access 17 | struct bcm2835_peripheral { 18 | unsigned long addr_p; /* the physical base address */ 19 | int mem_fd; /* the file descriptor for /dev/mem */ 20 | void *map; /* the pointer to the map from mmap for later use with munmap */ 21 | volatile unsigned int *addr; /* the address in user space where we will find the mapped memory */ 22 | }; 23 | 24 | extern struct bcm2835_peripheral gpio; // They have to be found somewhere, but can't be in the header 25 | 26 | // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) 27 | #define READ_GPIO(g) *(gpio.addr + ((g)/10)) 28 | #define INP_GPIO(g) *(gpio.addr + ((g)/10)) &= ~(7<<(((g)%10)*3)) 29 | #define OUT_GPIO(g) *(gpio.addr + ((g)/10)) |= (1<<(((g)%10)*3)) 30 | #define SET_GPIO_ALT(g,a) *(gpio.addr + (((g)/10))) |= (((a)<=3?(a) + 4:(a)==4?3:2)<<(((g)%10)*3)) 31 | 32 | #define GPIO_SET *(gpio.addr + 7) // sets bits which are 1 ignores bits which are 0 33 | #define GPIO_SET2 *(gpio.addr + 8) // sets bits which are 1 ignores bits which are 0 34 | #define GPIO_CLR *(gpio.addr + 10) // clears bits which are 1 ignores bits which are 0 35 | #define GPIO_CLR2 *(gpio.addr + 11) // clears bits which are 1 ignores bits which are 0 36 | 37 | #define GPIO_READ(g) *(gpio.addr + 13) &= (1<<(g)) 38 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/gpio_tests/setPinOne/rpi.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | //RPi 1 #define BCM2708_PERI_BASE 0x20000000 10 | #define BCM2708_PERI_BASE 0x3f000000 11 | 12 | #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) // GPIO controller 13 | 14 | #define BLOCK_SIZE (4*1024) 15 | 16 | // IO Access 17 | struct bcm2835_peripheral { 18 | unsigned long addr_p; /* the physical base address */ 19 | int mem_fd; /* the file descriptor for /dev/mem */ 20 | void *map; /* the pointer to the map from mmap for later use with munmap */ 21 | volatile unsigned int *addr; /* the address in user space where we will find the mapped memory */ 22 | }; 23 | 24 | extern struct bcm2835_peripheral gpio; // They have to be found somewhere, but can't be in the header 25 | 26 | // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) 27 | #define READ_GPIO(g) *(gpio.addr + ((g)/10)) 28 | #define INP_GPIO(g) *(gpio.addr + ((g)/10)) &= ~(7<<(((g)%10)*3)) 29 | #define OUT_GPIO(g) *(gpio.addr + ((g)/10)) |= (1<<(((g)%10)*3)) 30 | #define SET_GPIO_ALT(g,a) *(gpio.addr + (((g)/10))) |= (((a)<=3?(a) + 4:(a)==4?3:2)<<(((g)%10)*3)) 31 | 32 | #define GPIO_SET *(gpio.addr + 7) // sets bits which are 1 ignores bits which are 0 33 | #define GPIO_SET2 *(gpio.addr + 8) // sets bits which are 1 ignores bits which are 0 34 | #define GPIO_CLR *(gpio.addr + 10) // clears bits which are 1 ignores bits which are 0 35 | #define GPIO_CLR2 *(gpio.addr + 11) // clears bits which are 1 ignores bits which are 0 36 | 37 | #define GPIO_READ(g) *(gpio.addr + 13) &= (1<<(g)) 38 | -------------------------------------------------------------------------------- /py-rasp/temp-display/server.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | """ 4 | # About : UDP Server 5 | Recieve temperature message and display it on OLED 6 | # Date : 2016-03-17 7 | # Author : Xiang,Zuo 8 | # E-Mail : xianglinks@gmail.com 9 | """ 10 | 11 | import socket 12 | from sys import exit 13 | 14 | from oled import OLED 15 | 16 | 17 | # --- main function --- 18 | def main(): 19 | try: 20 | # init UDP socket 21 | UDP_IP = '' # recieve from all IPs 22 | UDP_PORT = 8008 23 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 24 | sock.bind((UDP_IP, UDP_PORT)) 25 | print('Bind UDP socket on %d' % UDP_PORT) 26 | 27 | # init control object for OLED display 28 | oled = OLED(rst=24, i2c_addr=0x3C) 29 | oled.on() # turn on display 30 | # set font face and size 31 | """ 32 | for font face you can use the path of your ttf file(just with name if in current dir) 33 | or you can use truetype fonts in /usr/share/fonts/truetype 34 | """ 35 | oled.set_text_style('FreeMono.ttf', fontsize=20, position=(10, 10)) 36 | print('OLED init finished') 37 | 38 | # --- recieve loop --- 39 | while True: 40 | data, addr = sock.recvfrom(1024) 41 | oled.display_text(data) # display text on OLED 42 | 43 | except KeyboardInterrupt: 44 | print('\nKeyboardInterrupt, exit') 45 | oled.off() # turn off display 46 | exit(0) 47 | 48 | finally: 49 | # turn off display 50 | oled = OLED(rst=24, i2c_addr=0x3C) 51 | oled.off() 52 | exit(0) 53 | 54 | if __name__ == "__main__": 55 | main() 56 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/gpio_tests/tools_debugging/rpi.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | //RPi 1 #define BCM2708_PERI_BASE 0x20000000 10 | #define BCM2708_PERI_BASE 0x3f000000 11 | 12 | #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) // GPIO controller 13 | 14 | #define BLOCK_SIZE (4*1024) 15 | 16 | // IO Access 17 | struct bcm2835_peripheral { 18 | unsigned long addr_p; /* the physical base address */ 19 | int mem_fd; /* the file descriptor for /dev/mem */ 20 | void *map; /* the pointer to the map from mmap for later use with munmap */ 21 | volatile unsigned int *addr; /* the address in user space where we will find the mapped memory */ 22 | }; 23 | 24 | extern struct bcm2835_peripheral gpio; // They have to be found somewhere, but can't be in the header 25 | 26 | // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) 27 | #define READ_GPIO(g) *(gpio.addr + ((g)/10)) 28 | #define INP_GPIO(g) *(gpio.addr + ((g)/10)) &= ~(7<<(((g)%10)*3)) 29 | #define OUT_GPIO(g) *(gpio.addr + ((g)/10)) |= (1<<(((g)%10)*3)) 30 | #define SET_GPIO_ALT(g,a) *(gpio.addr + (((g)/10))) |= (((a)<=3?(a) + 4:(a)==4?3:2)<<(((g)%10)*3)) 31 | 32 | #define GPIO_SET *(gpio.addr + 7) // sets bits which are 1 ignores bits which are 0 33 | #define GPIO_SET2 *(gpio.addr + 8) // sets bits which are 1 ignores bits which are 0 34 | #define GPIO_CLR *(gpio.addr + 10) // clears bits which are 1 ignores bits which are 0 35 | #define GPIO_CLR2 *(gpio.addr + 11) // clears bits which are 1 ignores bits which are 0 36 | 37 | #define GPIO_READ(g) *(gpio.addr + 13) &= (1<<(g)) 38 | -------------------------------------------------------------------------------- /py-rasp/comm-remote-host/send_my_ip.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # @Date : So 08 Nov 2015 10:23:39 CET 4 | # @About : Send ip addr to remote host with email 5 | # @Author : Xiang,Zuo 6 | # @Mail : xianglinks@gmail.com 7 | 8 | import socket 9 | import fcntl 10 | import struct 11 | import smtplib 12 | from email.mime.multipart import MIMEMultipart 13 | from email.mime.text import MIMEText 14 | 15 | 16 | # get ip of interfaces using socket 17 | def get_ip_addr(ifname): 18 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 19 | return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))[20:24]) 20 | 21 | # sender informations 22 | from_addr = 'your email addr' 23 | password = 'your password' 24 | smtp_server = 'smtp.gmail.com' 25 | # reciever informations 26 | to_addr = 'your object email addr' 27 | 28 | # set smtp server 29 | # server = smtplib.SMTP(smtp_server, 587) # using port 587 with starttls 30 | server = smtplib.SMTP_SSL(smtp_server, 465) # using port 465 with SMTP_SSL 31 | # if you using starttls you need to use ehlo() function 32 | server.ehlo() 33 | # server.starttls() 34 | 35 | # define the send message 36 | msg = MIMEMultipart() 37 | # add from to and Subject to this email 38 | msg['From'] = from_addr 39 | msg['To'] = to_addr 40 | msg['Subject'] = 'Report My IP_Addr' 41 | # the body message hier using text(not html) 42 | my_ip = get_ip_addr('wlan0') 43 | body = str(my_ip) 44 | msg.attach(MIMEText(body, 'plain', 'utf-8')) 45 | text = msg.as_string() 46 | 47 | # show the debug messages 48 | server.set_debuglevel(1) 49 | server.login(from_addr, password) 50 | # send email 51 | server.sendmail(from_addr, [to_addr], text) 52 | # close the server 53 | server.close() 54 | print("Successful send the IP_Addr...") 55 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/button_test/rpi.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | //RPi 1 #define BCM2708_PERI_BASE 0x20000000 10 | #define BCM2708_PERI_BASE 0x3f000000 11 | 12 | #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) // GPIO controller 13 | #define PWM_BASE (BCM2708_PERI_BASE + 0x20C000) // PWM controller 14 | #define CLOCK_BASE = (BCM2708_PERI_BASE + 0x101000); // Clock controller 15 | 16 | #define BLOCK_SIZE (4*1024) 17 | 18 | // IO Access 19 | struct bcm2835_peripheral { 20 | unsigned long addr_p; /* the physical base address */ 21 | int mem_fd; /* the file descriptor for /dev/mem */ 22 | void *map; /* the pointer to the map from mmap for later use with munmap */ 23 | volatile unsigned int *addr; /* the address in user space where we will find the mapped memory */ 24 | }; 25 | 26 | extern struct bcm2835_peripheral gpio; // They have to be found somewhere, but can't be in the header 27 | 28 | // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) 29 | #define READ_GPIO(g) *(gpio.addr + ((g)/10)) 30 | #define INP_GPIO(g) *(gpio.addr + ((g)/10)) &= ~(7<<(((g)%10)*3)) 31 | #define OUT_GPIO(g) *(gpio.addr + ((g)/10)) |= (1<<(((g)%10)*3)) 32 | #define SET_GPIO_ALT(g,a) *(gpio.addr + (((g)/10))) |= (((a)<=3?(a) + 4:(a)==4?3:2)<<(((g)%10)*3)) 33 | 34 | #define GPIO_SET *(gpio.addr + 7) // sets bits which are 1 ignores bits which are 0 35 | #define GPIO_SET2 *(gpio.addr + 8) // sets bits which are 1 ignores bits which are 0 36 | #define GPIO_CLR *(gpio.addr + 10) // clears bits which are 1 ignores bits which are 0 37 | #define GPIO_CLR2 *(gpio.addr + 11) // clears bits which are 1 ignores bits which are 0 38 | 39 | #define GPIO_READ(g) *(gpio.addr + 13) &= (1<<(g)) 40 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/calc_led/rpi.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | //RPi 1 #define BCM2708_PERI_BASE 0x20000000 10 | #define BCM2708_PERI_BASE 0x3f000000 11 | 12 | #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) // GPIO controller 13 | #define PWM_BASE (BCM2708_PERI_BASE + 0x20C000) // PWM controller 14 | #define CLOCK_BASE = (BCM2708_PERI_BASE + 0x101000); // Clock controller 15 | 16 | #define BLOCK_SIZE (4*1024) 17 | 18 | // IO Access 19 | struct bcm2835_peripheral { 20 | unsigned long addr_p; /* the physical base address */ 21 | int mem_fd; /* the file descriptor for /dev/mem */ 22 | void *map; /* the pointer to the map from mmap for later use with munmap */ 23 | volatile unsigned int *addr; /* the address in user space where we will find the mapped memory */ 24 | }; 25 | 26 | extern struct bcm2835_peripheral gpio; // They have to be found somewhere, but can't be in the header 27 | 28 | // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) 29 | #define READ_GPIO(g) *(gpio.addr + ((g)/10)) 30 | #define INP_GPIO(g) *(gpio.addr + ((g)/10)) &= ~(7<<(((g)%10)*3)) 31 | #define OUT_GPIO(g) *(gpio.addr + ((g)/10)) |= (1<<(((g)%10)*3)) 32 | #define SET_GPIO_ALT(g,a) *(gpio.addr + (((g)/10))) |= (((a)<=3?(a) + 4:(a)==4?3:2)<<(((g)%10)*3)) 33 | 34 | #define GPIO_SET *(gpio.addr + 7) // sets bits which are 1 ignores bits which are 0 35 | #define GPIO_SET2 *(gpio.addr + 8) // sets bits which are 1 ignores bits which are 0 36 | #define GPIO_CLR *(gpio.addr + 10) // clears bits which are 1 ignores bits which are 0 37 | #define GPIO_CLR2 *(gpio.addr + 11) // clears bits which are 1 ignores bits which are 0 38 | 39 | #define GPIO_READ(g) *(gpio.addr + 13) &= (1<<(g)) 40 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/input_test/rpi.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | //RPi 1 #define BCM2708_PERI_BASE 0x20000000 10 | #define BCM2708_PERI_BASE 0x3f000000 11 | 12 | #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) // GPIO controller 13 | #define PWM_BASE (BCM2708_PERI_BASE + 0x20C000) // PWM controller 14 | #define CLOCK_BASE = (BCM2708_PERI_BASE + 0x101000); // Clock controller 15 | 16 | #define BLOCK_SIZE (4*1024) 17 | 18 | // IO Access 19 | struct bcm2835_peripheral { 20 | unsigned long addr_p; /* the physical base address */ 21 | int mem_fd; /* the file descriptor for /dev/mem */ 22 | void *map; /* the pointer to the map from mmap for later use with munmap */ 23 | volatile unsigned int *addr; /* the address in user space where we will find the mapped memory */ 24 | }; 25 | 26 | extern struct bcm2835_peripheral gpio; // They have to be found somewhere, but can't be in the header 27 | 28 | // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) 29 | #define READ_GPIO(g) *(gpio.addr + ((g)/10)) 30 | #define INP_GPIO(g) *(gpio.addr + ((g)/10)) &= ~(7<<(((g)%10)*3)) 31 | #define OUT_GPIO(g) *(gpio.addr + ((g)/10)) |= (1<<(((g)%10)*3)) 32 | #define SET_GPIO_ALT(g,a) *(gpio.addr + (((g)/10))) |= (((a)<=3?(a) + 4:(a)==4?3:2)<<(((g)%10)*3)) 33 | 34 | #define GPIO_SET *(gpio.addr + 7) // sets bits which are 1 ignores bits which are 0 35 | #define GPIO_SET2 *(gpio.addr + 8) // sets bits which are 1 ignores bits which are 0 36 | #define GPIO_CLR *(gpio.addr + 10) // clears bits which are 1 ignores bits which are 0 37 | #define GPIO_CLR2 *(gpio.addr + 11) // clears bits which are 1 ignores bits which are 0 38 | 39 | #define GPIO_READ(g) *(gpio.addr + 13) &= (1<<(g)) 40 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/led_rhythm/rpi.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | //RPi 1 #define BCM2708_PERI_BASE 0x20000000 10 | #define BCM2708_PERI_BASE 0x3f000000 11 | 12 | #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) // GPIO controller 13 | #define PWM_BASE (BCM2708_PERI_BASE + 0x20C000) // PWM controller 14 | #define CLOCK_BASE = (BCM2708_PERI_BASE + 0x101000); // Clock controller 15 | 16 | #define BLOCK_SIZE (4*1024) 17 | 18 | // IO Access 19 | struct bcm2835_peripheral { 20 | unsigned long addr_p; /* the physical base address */ 21 | int mem_fd; /* the file descriptor for /dev/mem */ 22 | void *map; /* the pointer to the map from mmap for later use with munmap */ 23 | volatile unsigned int *addr; /* the address in user space where we will find the mapped memory */ 24 | }; 25 | 26 | extern struct bcm2835_peripheral gpio; // They have to be found somewhere, but can't be in the header 27 | 28 | // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) 29 | #define READ_GPIO(g) *(gpio.addr + ((g)/10)) 30 | #define INP_GPIO(g) *(gpio.addr + ((g)/10)) &= ~(7<<(((g)%10)*3)) 31 | #define OUT_GPIO(g) *(gpio.addr + ((g)/10)) |= (1<<(((g)%10)*3)) 32 | #define SET_GPIO_ALT(g,a) *(gpio.addr + (((g)/10))) |= (((a)<=3?(a) + 4:(a)==4?3:2)<<(((g)%10)*3)) 33 | 34 | #define GPIO_SET *(gpio.addr + 7) // sets bits which are 1 ignores bits which are 0 35 | #define GPIO_SET2 *(gpio.addr + 8) // sets bits which are 1 ignores bits which are 0 36 | #define GPIO_CLR *(gpio.addr + 10) // clears bits which are 1 ignores bits which are 0 37 | #define GPIO_CLR2 *(gpio.addr + 11) // clears bits which are 1 ignores bits which are 0 38 | 39 | #define GPIO_READ(g) *(gpio.addr + 13) &= (1<<(g)) 40 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/pwm_test/rpi.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | //RPi 1 #define BCM2708_PERI_BASE 0x20000000 10 | #define BCM2708_PERI_BASE 0x3f000000 11 | 12 | #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) // GPIO controller 13 | #define PWM_BASE (BCM2708_PERI_BASE + 0x20C000) // PWM controller 14 | #define CLOCK_BASE = (BCM2708_PERI_BASE + 0x101000); // Clock controller 15 | 16 | #define BLOCK_SIZE (4*1024) 17 | 18 | // IO Access 19 | struct bcm2835_peripheral { 20 | unsigned long addr_p; /* the physical base address */ 21 | int mem_fd; /* the file descriptor for /dev/mem */ 22 | void *map; /* the pointer to the map from mmap for later use with munmap */ 23 | volatile unsigned int *addr; /* the address in user space where we will find the mapped memory */ 24 | }; 25 | 26 | extern struct bcm2835_peripheral gpio; // They have to be found somewhere, but can't be in the header 27 | 28 | // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) 29 | #define READ_GPIO(g) *(gpio.addr + ((g)/10)) 30 | #define INP_GPIO(g) *(gpio.addr + ((g)/10)) &= ~(7<<(((g)%10)*3)) 31 | #define OUT_GPIO(g) *(gpio.addr + ((g)/10)) |= (1<<(((g)%10)*3)) 32 | #define SET_GPIO_ALT(g,a) *(gpio.addr + (((g)/10))) |= (((a)<=3?(a) + 4:(a)==4?3:2)<<(((g)%10)*3)) 33 | 34 | #define GPIO_SET *(gpio.addr + 7) // sets bits which are 1 ignores bits which are 0 35 | #define GPIO_SET2 *(gpio.addr + 8) // sets bits which are 1 ignores bits which are 0 36 | #define GPIO_CLR *(gpio.addr + 10) // clears bits which are 1 ignores bits which are 0 37 | #define GPIO_CLR2 *(gpio.addr + 11) // clears bits which are 1 ignores bits which are 0 38 | 39 | #define GPIO_READ(g) *(gpio.addr + 13) &= (1<<(g)) 40 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/temp_mess/rpi.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | //RPi 1 #define BCM2708_PERI_BASE 0x20000000 10 | #define BCM2708_PERI_BASE 0x3f000000 11 | 12 | #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) // GPIO controller 13 | #define PWM_BASE (BCM2708_PERI_BASE + 0x20C000) // PWM controller 14 | #define CLOCK_BASE = (BCM2708_PERI_BASE + 0x101000); // Clock controller 15 | 16 | #define BLOCK_SIZE (4*1024) 17 | 18 | // IO Access 19 | struct bcm2835_peripheral { 20 | unsigned long addr_p; /* the physical base address */ 21 | int mem_fd; /* the file descriptor for /dev/mem */ 22 | void *map; /* the pointer to the map from mmap for later use with munmap */ 23 | volatile unsigned int *addr; /* the address in user space where we will find the mapped memory */ 24 | }; 25 | 26 | extern struct bcm2835_peripheral gpio; // They have to be found somewhere, but can't be in the header 27 | 28 | // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) 29 | #define READ_GPIO(g) *(gpio.addr + ((g)/10)) 30 | #define INP_GPIO(g) *(gpio.addr + ((g)/10)) &= ~(7<<(((g)%10)*3)) 31 | #define OUT_GPIO(g) *(gpio.addr + ((g)/10)) |= (1<<(((g)%10)*3)) 32 | #define SET_GPIO_ALT(g,a) *(gpio.addr + (((g)/10))) |= (((a)<=3?(a) + 4:(a)==4?3:2)<<(((g)%10)*3)) 33 | 34 | #define GPIO_SET *(gpio.addr + 7) // sets bits which are 1 ignores bits which are 0 35 | #define GPIO_SET2 *(gpio.addr + 8) // sets bits which are 1 ignores bits which are 0 36 | #define GPIO_CLR *(gpio.addr + 10) // clears bits which are 1 ignores bits which are 0 37 | #define GPIO_CLR2 *(gpio.addr + 11) // clears bits which are 1 ignores bits which are 0 38 | 39 | #define GPIO_READ(g) *(gpio.addr + 13) &= (1<<(g)) 40 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/interaption_test/rpi.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | //RPi 1 #define BCM2708_PERI_BASE 0x20000000 10 | #define BCM2708_PERI_BASE 0x3f000000 11 | 12 | #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) // GPIO controller 13 | #define PWM_BASE (BCM2708_PERI_BASE + 0x20C000) // PWM controller 14 | #define CLOCK_BASE = (BCM2708_PERI_BASE + 0x101000); // Clock controller 15 | 16 | #define BLOCK_SIZE (4*1024) 17 | 18 | // IO Access 19 | struct bcm2835_peripheral { 20 | unsigned long addr_p; /* the physical base address */ 21 | int mem_fd; /* the file descriptor for /dev/mem */ 22 | void *map; /* the pointer to the map from mmap for later use with munmap */ 23 | volatile unsigned int *addr; /* the address in user space where we will find the mapped memory */ 24 | }; 25 | 26 | extern struct bcm2835_peripheral gpio; // They have to be found somewhere, but can't be in the header 27 | 28 | // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) 29 | #define READ_GPIO(g) *(gpio.addr + ((g)/10)) 30 | #define INP_GPIO(g) *(gpio.addr + ((g)/10)) &= ~(7<<(((g)%10)*3)) 31 | #define OUT_GPIO(g) *(gpio.addr + ((g)/10)) |= (1<<(((g)%10)*3)) 32 | #define SET_GPIO_ALT(g,a) *(gpio.addr + (((g)/10))) |= (((a)<=3?(a) + 4:(a)==4?3:2)<<(((g)%10)*3)) 33 | 34 | #define GPIO_SET *(gpio.addr + 7) // sets bits which are 1 ignores bits which are 0 35 | #define GPIO_SET2 *(gpio.addr + 8) // sets bits which are 1 ignores bits which are 0 36 | #define GPIO_CLR *(gpio.addr + 10) // clears bits which are 1 ignores bits which are 0 37 | #define GPIO_CLR2 *(gpio.addr + 11) // clears bits which are 1 ignores bits which are 0 38 | 39 | #define GPIO_READ(g) *(gpio.addr + 13) &= (1<<(g)) 40 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/button_led_control/rpi.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | //RPi 1 #define BCM2708_PERI_BASE 0x20000000 10 | #define BCM2708_PERI_BASE 0x3f000000 11 | 12 | #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) // GPIO controller 13 | #define PWM_BASE (BCM2708_PERI_BASE + 0x20C000) // PWM controller 14 | #define CLOCK_BASE = (BCM2708_PERI_BASE + 0x101000); // Clock controller 15 | 16 | #define BLOCK_SIZE (4*1024) 17 | 18 | // IO Access 19 | struct bcm2835_peripheral { 20 | unsigned long addr_p; /* the physical base address */ 21 | int mem_fd; /* the file descriptor for /dev/mem */ 22 | void *map; /* the pointer to the map from mmap for later use with munmap */ 23 | volatile unsigned int *addr; /* the address in user space where we will find the mapped memory */ 24 | }; 25 | 26 | extern struct bcm2835_peripheral gpio; // They have to be found somewhere, but can't be in the header 27 | 28 | // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) 29 | #define READ_GPIO(g) *(gpio.addr + ((g)/10)) 30 | #define INP_GPIO(g) *(gpio.addr + ((g)/10)) &= ~(7<<(((g)%10)*3)) 31 | #define OUT_GPIO(g) *(gpio.addr + ((g)/10)) |= (1<<(((g)%10)*3)) 32 | #define SET_GPIO_ALT(g,a) *(gpio.addr + (((g)/10))) |= (((a)<=3?(a) + 4:(a)==4?3:2)<<(((g)%10)*3)) 33 | 34 | #define GPIO_SET *(gpio.addr + 7) // sets bits which are 1 ignores bits which are 0 35 | #define GPIO_SET2 *(gpio.addr + 8) // sets bits which are 1 ignores bits which are 0 36 | #define GPIO_CLR *(gpio.addr + 10) // clears bits which are 1 ignores bits which are 0 37 | #define GPIO_CLR2 *(gpio.addr + 11) // clears bits which are 1 ignores bits which are 0 38 | 39 | #define GPIO_READ(g) *(gpio.addr + 13) &= (1<<(g)) 40 | -------------------------------------------------------------------------------- /py-rasp/temp-display/mcp3008/mcp3008.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | """ 4 | # About : Class for MCP3008 Analog Digital Converter 5 | # Date : 2016-03-17 6 | # Author : Xiang,Zuo 7 | # E-Mail : xianglinks@gmail.com 8 | """ 9 | 10 | from spidev import SpiDev 11 | 12 | 13 | class MCP3008(object): 14 | 15 | """Class for MCP3008 ADC""" 16 | 17 | def __init__(self, port=0, device=0): 18 | self.spi = SpiDev() 19 | # connect spi object to specified spi device 20 | self.spi.open(port, device) 21 | 22 | def readValueChannel(self, channel=0): 23 | """ 24 | read SPI data from MCP3008 on channel -> digital value 25 | spi.xfer2() send three bytes to the device 26 | the first byte is 1 -> 00000001 27 | the second byte is 8 + channel and left shift with 4 bits 28 | the third byte is 0 -> 00000000 29 | the device return 3 bytes as responce 30 | """ 31 | # perform spi transaction 32 | adc = self.spi.xfer2([1, (8 + channel) <<4, 0]) 33 | # extract value from data bytes 34 | data = ((adc[1] & 3) << 8) + adc[2] 35 | return data 36 | 37 | def readVoltChannel(self, channel=0, vmax=3.3, places=5): 38 | """ 39 | read the digital data from MCP3008 and convert it to voltage 40 | MCP3008: 10bit ADC -> value in number range 0-1023 41 | spi value -> voltage 42 | 0 -> 0v 43 | 1023 -> vmax 44 | """ 45 | # read spi digital value 46 | adc = self.spi.xfer2([1, (8 + channel) <<4, 0]) 47 | data = ((adc[1] & 3) << 8) + adc[2] 48 | # convert it to voltage 49 | volts = (data * vmax) / float(1023) 50 | # round to specified number of decimal places 51 | volts = round(volts, places) 52 | return volts 53 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/pwm_test/pwm_settings.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : 3 | * Date : 2015-12-12 09:52:51 4 | * Author : Xiang, Zuo 5 | * Email : xianglinks@gmail.com 6 | ******************************************************************************/ 7 | 8 | /* ---------------- Code Section ---------------- */ 9 | .text 10 | .balign 4 11 | 12 | .equ pwmPin, 12 /* BCM-pin 12 support hardware PWM */ 13 | 14 | /* 15 | * PWM_BASE = (BCM2835_PERI_BASE + 0x20C000) -> PWM controller 16 | * Bit Streams configured individually to output either PWM or serialised version of 32 bits 17 | * both modes clocked by clk_pwm, which is normally 100MHz, can be varied by the clock manager 18 | * PWN controller consist of two independent channels -> channel0 & channel1 19 | * two sub-modes in PWM controller: MSEN = 0(default) and MSEN = 1 20 | * hardware support PWM: BCM-pin 12: PWM0, alternate function 0(alt0) 21 | */ 22 | 23 | /* CLOCK_BASE = (BCM2835_PERI_BASE + 0x101000) -> CLOCK controller 24 | * base = 0x7e000000 clock 0x7e101000 25 | * genau infomations of address of register on BCM2835: http://elinux.org/BCM2835_registers 26 | * Page 105 -> GPIO general clock 27 | */ 28 | 29 | .extern usleep 30 | 31 | /* 32 | * The address for CM_PWMCTL(PWM clock Controller) is 0x7e1010a0 33 | * BCM2835_PWMCLK_CNTL -> Offset .word 40(4 byte) 34 | * bcm2835_clk + BCM2835_PWMCLK_CNTL = BCM2835_CLOCK_BASE + (BCM2835_PWMCLK_CNTL * 4) 35 | * so the CM_PWMCTL = BCM2835_CLOCK_BASE + 160 */ 36 | 37 | 38 | .global set_clock_pwm 39 | .func set_clock_pwm 40 | /* para: base_addr(r0) */ 41 | set_clock_pwm: 42 | push {lr} 43 | 44 | /* stop the clock for trigger */ 45 | ldr r3, =0x101000 46 | add r3, r3, r0 /* get BCM2835_CLOCK_BASE */ 47 | add r3, r3, #160 /* get CM_PWMCTL */ 48 | ldr r2, =1509949472 49 | str r2, [r3, #0] 50 | mov r0, #10 51 | bl usleep 52 | 53 | set_frequency: 54 | /* set frequency 55 | * DIVI is the integer part of the divisor 56 | * the fractional part (DIVF) drops clock cycles to get the output frequency 57 | * 320 bits for one cycle of 20 milliseconds = 62.5 us per bit = 16 kHz */ 58 | 59 | 60 | pop {pc} 61 | .endfunc 62 | 63 | /* ---------------- Data Section ---------------- */ 64 | .data 65 | .balign 4 66 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/baseboard_test/inputPinTest.S: -------------------------------------------------------------------------------- 1 | /* 2 | * About : Set Pin 16 as Output and Toggle the value(0V, 3.3V) 3 | * Date : 2015-12-02 22:25:30 4 | * Author : Xiang, Zuo 5 | * Email : xianglinks@gmail.com 6 | */ 7 | 8 | /* -- Data Section -- */ 9 | .data 10 | .balign 4 11 | msg1: .asciz "Mapping finished...\n" 12 | msg2: .asciz "Pin mode setting finished...\n" 13 | msg3: .asciz "Set BCM-Pin 16 to 3.3V...\n" 14 | msg4: .asciz "Set BCM-Pin 16 to 0V...\n" 15 | 16 | /* -- Code Section -- */ 17 | .text 18 | .balign 4 19 | .extern printf 20 | .extern map_peripheral 21 | .extern sleep /* function for sleep using value in r0 */ 22 | .equ pinNum, 16 /* BCM Pin Number */ 23 | .global main 24 | .func main 25 | 26 | main: 27 | push {r4-r11, lr} 28 | 29 | /* initialize memory mapping */ 30 | bl map_peripheral 31 | mov r9, r0 /* put base addr in r9 */ 32 | /* print debugging message */ 33 | ldr r0, =msg1 34 | bl printf 35 | 36 | /* set pin 16 to output 37 | * manipulating GPFSEL-Register 38 | * addr of register = base + 4 * ( pinNum / 10 ) 39 | * mode addr of pin = (pinNum % 10) * 3 40 | */ 41 | ldr r3, [r9, #(4 * (pinNum / 10))] /* read GPFSEL-Register to R3 */ 42 | and r3, #~(7 << ((pinNum % 10) * 3)) /* set select-bits to 000 -> input */ 43 | str r3, [r9, #(4 * (pinNum / 10))] 44 | orr r3, #(1 << ((pinNum % 10) * 3)) /* set select-bits to 001 -> output */ 45 | str r3, [r9, #(4 * (pinNum / 10))] /* write R3 to GPFSEL-Register */ 46 | ldr r0, =msg2 47 | bl printf 48 | 49 | /* main toggle loop */ 50 | loop: 51 | 52 | /* clear pin 16 53 | * GPSCLR-Register 4 * (10 + (pinNum/32)) 54 | * one bit stand for one pin 55 | * pin_addr = pinNum % 32 56 | */ 57 | mov r3, #(1 << (pinNum % 32)) 58 | str r3, [r9, #(4 * (10 + pinNum/32))] 59 | 60 | ldr r0, =msg4 61 | bl printf 62 | mov r0, #3 63 | bl sleep 64 | 65 | /* set pin 16 to 1 66 | * manipulating GPSET0-Register 67 | * GPSET0 - 0x7E20001C pin 00-32 68 | * GPSET1 - 0x7E200020 pin 32-53 69 | * one bit is stand for a pin 70 | * register addr = 4 * (7 + pinNum/32) 71 | */ 72 | mov r3, #(1 << (pinNum % 32)) 73 | str r3, [r9, #(4 * (7 + pinNum/32))] 74 | 75 | ldr r0, =msg3 76 | bl printf 77 | mov r0, #3 78 | bl sleep 79 | 80 | b loop /* a immer loop */ 81 | 82 | end_main: 83 | mov r0, #0 84 | pop {r4-r11, pc} 85 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/gpio_tests/setPinOne/setPins.S: -------------------------------------------------------------------------------- 1 | .extern sleep /* external function for sleeping */ 2 | .extern map_peripheral /* external function for mapping the GPIO peripherals */ 3 | .equ GPIO_PIN, 47 /* The GREEN LED is connected to GPIO_PIN 47 on the PI+ plattforms */ 4 | 5 | .text 6 | .balign 4 7 | .global main 8 | .func main 9 | 10 | main: 11 | /* Step 1: save registers and return address */ 12 | push {r4-r11, lr} 13 | 14 | /* Step 2: map_peripheral returns the base address of the GPIO registers in r0 */ 15 | bl map_peripheral 16 | mov r9, r0 17 | 18 | /* Step 3: set GPIO LED to output by manipulating the GPFSEL-register 19 | * with a read-modify-write pattern. Each 32bit-GPFSEL-Register contains 10 pins, so 20 | * the address of the register is simply calculated as BASE + 4 * (PinNumber/10). 21 | * The Function Select of each pin is 3 bits wide (000=Input, 001=Output), that is 22 | * bits 21-23 hold the mode of the x-7th pin (see BCM2835 ARM peripherals datasheet, page 89 ff.) */ 23 | ldr r3, [r9, #(4 * (GPIO_PIN/10))] /* read GPFSEL4 to R3 */ 24 | and r3, #~(7 << ((GPIO_PIN%10)*3)) /* set bits 21-23 to 000, i.e. set pin as input */ 25 | str r3, [r9, #(4 * (GPIO_PIN/10))] /* do we really need this intermediate write to GPFSEL4? */ 26 | orr r3, #(1 << ((GPIO_PIN%10)*3)) /* set bits 21-23 to 001, i.e. set pin as output */ 27 | str r3, [r9, #(4 * (GPIO_PIN/10))] /* write R3 to GPFSEL4 */ 28 | 29 | /* Step 4: toggle bits every second */ 30 | loop: 31 | /* Step 4.1: set LED and wait a second. Every 1 in the GPSET0/1 register sets the according pin, 0 are ignored 32 | * GPSET0 holds pins 0-31, GPSET1 pins 32-54. The bit position is the remainder of PinNumber / 32, the 33 | * address BASE + 7 + PinNumber / 32. */ 34 | mov r3, #(1 << (GPIO_PIN%32)) 35 | str r3, [r9, #(4*(7 + (GPIO_PIN/32)))] 36 | mov r0, #1 37 | bl sleep 38 | 39 | /* Step 4.2: CLEAR LED 47 for a second. Every 1 in the GPSCLR0/1 register clears the according pin, zeros are ignored */ 40 | mov r3, #(1 << (GPIO_PIN%32)) 41 | str r3, [r9, #(4*(10 + (GPIO_PIN/32)))] 42 | mov r0, #1 43 | bl sleep 44 | 45 | /* Step 4.3: continue for ever */ 46 | bal loop 47 | 48 | /* Step 5: restore registers and return by popping the saved lr to pc */ 49 | end_main: 50 | mov r0, #0 /* this is our return code by convention */ 51 | pop {r4-r11, pc} 52 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/gpio_tests/inputTest/input_pins.S: -------------------------------------------------------------------------------- 1 | .section .init 2 | .globl _start 3 | _start: 4 | 5 | 6 | b main 7 | 8 | .section .text 9 | 10 | main: 11 | 12 | 13 | /* 14 | * Set the stack point to 0x8000. 15 | */ 16 | 17 | mov sp,#0x8000 18 | 19 | 20 | /* 21 | * Location of gpio controller 22 | */ 23 | ldr r0, =0x20200000 24 | 25 | /*enable pin 23 as output*/ 26 | mov r1,#1 27 | lsl r1,#9 28 | str r1,[r0,#8] 29 | 30 | /*enable pin 18 as output*/ 31 | mov r1,#1 32 | lsl r1,#24 33 | str r1,[r0,#4] 34 | 35 | /*enable pin 17 as input*/ 36 | mov r1,#0 37 | lsl r1,#21 38 | str r1,[r0,#4] 39 | 40 | /*enable pin 22 as input*/ 41 | mov r1,#0 42 | lsl r1,#6 43 | str r1,[r0,#8] 44 | 45 | /*Enable ok light as output for status*/ 46 | mov r1,#1 47 | lsl r1,#18 48 | str r1,[r0,#4] 49 | 50 | /*Endless Loop*/ 51 | loop$: 52 | 53 | /*Turn ok light on (backwards)*/ 54 | ldr r0, =0x20200000 55 | mov r1,#1 56 | lsl r1,#16 57 | str r1,[r0,#40] 58 | 59 | /*if pin 17 input is high, change pin 18 high*/ 60 | /*input for pin 17 is gpio + 0x00000036 (#54), lone value #64*/ 61 | Pin18: 62 | ldr r2, =0x20200036 63 | ldr r0, =0x20200000 64 | ldrb r1,[r2] 65 | cmp r1, #64 66 | bne Pin23 67 | bl TurnPin18On 68 | bl TurnPin23Off 69 | 70 | 71 | /*if pin 22 input is high, change pin 23 high*/ 72 | /*input for pin 22 is gpio + 0x00000036 (#54), lone value #2*/ 73 | Pin23: 74 | ldr r2, =0x20200036 75 | ldr r0, =0x20200000 76 | ldrb r1,[r2] 77 | cmp r1, #2 78 | bne 79 | bl TurnPin23On 80 | bl TurnPin18Off 81 | 82 | 83 | /*if pin 22 input is high, change pin 23 high*/ 84 | /*input for pin 22 is gpio + 0x00000036 (#54), lone value #66*/ 85 | pin1823: 86 | ldr r2, =0x20200036 87 | ldr r0, =0x20200000 88 | ldrb r1,[r2] 89 | cmp r1, #66 90 | bne NoPin 91 | bl TurnPin23On 92 | bl TurnPin18On 93 | 94 | NoPin: 95 | ldr r2, =0x20200036 96 | ldr r0, =0x20200000 97 | ldrb r1,[r2] 98 | cmp r1, #0 99 | bne Wait 100 | bl TurnPin23On 101 | bl TurnPin18On 102 | 103 | Wait: 104 | mov r2,#0x3F0000 105 | wait1$: 106 | sub r2,#1 107 | cmp r2,#0 108 | bne wait1$ 109 | 110 | /*Turn ok light off (backwards)*/ 111 | ldr r0, =0x20200000 112 | mov r1,#1 113 | lsl r1,#16 114 | str r1,[r0,#28] 115 | 116 | mov r2,#0x3F0000 117 | wait2$: 118 | sub r2,#1 119 | cmp r2,#0 120 | bne wait2$ 121 | 122 | 123 | b loop$ 124 | 125 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/input_test/input.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : Temperature measurement and processing on Rpi 3 | * Date : 2015-12-03 23:52:37 4 | * Author : Xiang,Zuo 5 | * Email : xianglinks@gmail.com 6 | ******************************************************************************/ 7 | 8 | /* ---------------- Data Section ---------------- */ 9 | .data 10 | .balign 4 11 | 12 | /* definations for strings */ 13 | errMsg1: .asciz "initiation do not work. aborting...\n" 14 | errMsg2: .asciz "addr_mapping do not work. aborting...\n" 15 | tempMsg: .asciz "temperature is: %dC.\n" 16 | 17 | 18 | /* ---------------- Code Section ---------------- */ 19 | .text 20 | .balign 4 21 | 22 | /* import c fuctions */ 23 | .extern init_mcp /* init wiringPi and mcp3008 */ 24 | .extern read_temp /* read_temp using wiringPi */ 25 | .extern map_peripheral /* mapping the physical addr */ 26 | .extern sleep /* TODO do not use it */ 27 | .extern printf /* print value on the screen */ 28 | 29 | /* import asm functions */ 30 | .extern set_gpio_input 31 | .extern set_gpio_output 32 | .extern blue_led 33 | .extern green_led 34 | .extern red_led 35 | 36 | .equ btnPin, 15 37 | 38 | .global main 39 | .func main 40 | 41 | main: 42 | push {r4-r11, lr} 43 | 44 | /* initiation wiring_pins and mcp3008 (run only once) */ 45 | bl init_mcp 46 | mov r1, #-1 47 | cmp r0, r1 48 | bne mapping_addr 49 | /* when problem */ 50 | ldr r0, =errMsg1 51 | bl printf 52 | b end_main 53 | 54 | /* addr_mapping using c function */ 55 | mapping_addr: 56 | bl map_peripheral 57 | mov r9, r0 /* save gpio_base_addr in r9 */ 58 | mov r1, #-1 59 | cmp r0, r1 60 | bne gpio_settings 61 | /* when problem */ 62 | ldr r0, =errMsg2 63 | bl printf 64 | b end_main 65 | 66 | gpio_settings: 67 | mov r0, r9 68 | bl set_gpio_input 69 | 70 | mov r0, r9 71 | bl set_gpio_input 72 | 73 | /* get some thing from input */ 74 | input_loop: 75 | 76 | /* get gpio_pin level GPLEV-Register 77 | * addr = base + #(52 + 4 * (pinNum / 32)) 78 | */ 79 | ldr r3, [r9, #(52 + 4 * (btnPin / 32))] 80 | lsr r3, #15 81 | and r3, #1 82 | ldr r0, =tempMsg 83 | mov r1, r3 84 | bl printf 85 | 86 | 87 | mov r0, #1 88 | bl sleep 89 | 90 | b input_loop 91 | 92 | 93 | end_main: 94 | mov r0, #0 95 | pop {r4-r11, pc} 96 | -------------------------------------------------------------------------------- /py-rasp/temp-display/oled/oled.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | """ 4 | # About : Class for controlling display on OLED using SSD1306 driver 5 | # Date : 2016-03-16 12:21:47 6 | # Author : Xiang,Zuo 7 | # E-Mail : xianglinks@gmail.com 8 | """ 9 | 10 | from time import sleep 11 | 12 | from PIL import Image, ImageDraw, ImageFont 13 | 14 | import Adafruit_SSD1306 15 | from logger import logger 16 | 17 | 18 | class OLED(object): 19 | 20 | """ Controll display on OLED """ 21 | 22 | def __init__(self, rst=24, i2c_addr=0x3C): 23 | """init OLED class with I2C interface""" 24 | # creat objec to controll SSD1306 driver 25 | self.disp = Adafruit_SSD1306.SSD1306_128_64( 26 | rst=rst, i2c_address=i2c_addr) 27 | # get display size 28 | self.width = self.disp.width 29 | self.height = self.disp.height 30 | logger.debug('OLED init finished') 31 | 32 | def on(self): 33 | """turn on OLED display""" 34 | # creat image and draw object 35 | self.image = Image.new('1', (self.width, self.height)) 36 | self.draw = ImageDraw.Draw(self.image) 37 | # clear contents on display 38 | self.disp.begin() 39 | self.disp.clear() 40 | self.disp.display() 41 | logger.debug('OLED started') 42 | 43 | def off(self, seconds=3): 44 | """turn off OLED in seconds""" 45 | # print off_message on screen 46 | print('OLED will turn off in %d seconds' % seconds) 47 | # print off_message on OLED 48 | off_message = 'turn off in %d s' % seconds 49 | self.set_text_style('FreeMono.ttf', 13, (5, 25)) 50 | self.display_text(off_message) 51 | sleep(seconds) 52 | self.disp.off() 53 | 54 | def set_text_style(self, fontname='FreeMono.ttf', fontsize=10, position=(0, 0)): 55 | """set font face and size""" 56 | self.font = ImageFont.truetype(fontname, fontsize) 57 | # set font start position(tuple) 58 | self.position = position 59 | 60 | def display_text(self, text): 61 | """display text(string) on OLED""" 62 | # clear the old cotents in the display 63 | self.disp.clear() 64 | self.disp.display() 65 | # draw black rectangle as background 66 | self.draw.rectangle([0, 0, self.width, self.height], outline=0, fill=0) 67 | # draw text on rectangle 68 | self.draw.text(self.position, str(text), font=self.font, fill=255) 69 | self.disp.image(self.image) 70 | self.disp.display() 71 | logger.debug('display text finished') 72 | -------------------------------------------------------------------------------- /c-rasp/oled-udp/ssd1306_i2c.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | * About : SSD1306 OLED Driver Programm(using I2C Interface) 3 | * Depend on lib wiringPi 4 | * Author : Xiang,Zuo 5 | * Mail : xianglinks@gmail.com 6 | * Date : 2016-03-20 7 | * Ver : 0.3 8 | **************************************************************************/ 9 | 10 | #ifndef SSD1306_I2C_H 11 | #define SSD1306_I2C_H 12 | 13 | // --- Define functions --- 14 | // ---------------------------------------------- 15 | /** 16 | * @brief ssd1306I2CSetup 17 | * 18 | * @param i2cAddr: I2C device address 19 | * on Rpi, you can use i2cdetect -y 1 to get addr 20 | * 21 | * @return standard file descriptor for I2C operations 22 | */ 23 | int ssd1306I2CSetup(unsigned char i2cAddr); 24 | 25 | // send command byte to SSD1306 via I2C 26 | void sendCommand(int fd, unsigned char command); 27 | // send data byte to SSD1306 via I2C 28 | void sendData(int fd, unsigned char data); 29 | 30 | // OLED operations 31 | void displayOn(int fd); // turn on the display 32 | void displayOff(int fd); // turn off the display 33 | void clearDisplay(int fd); // clear the contents on display 34 | 35 | 36 | // Attention: The draw and draw_line functions only change the contents in local data frame 37 | // To update the contents on display should function updateDisplay() be used 38 | 39 | /** 40 | * @brief draw one ascii code at determined position 41 | * 42 | * @param x: 1 to 4 43 | * @param y: 1 to 16 44 | * @param ascii: one ascii code 45 | */ 46 | void draw(int x,int y,unsigned char ascii); 47 | 48 | /** 49 | * @brief draw one line of ascii codes at determined position on display 50 | * 51 | * @param x: 1 to 4 52 | * @param y: 1 to 16 53 | * @param ascii_str[16]: the number of ascii codes can not more than 16 54 | */ 55 | void draw_line(int x ,int y,unsigned char ascii_str[16]); 56 | 57 | /** 58 | * @brief write the frame data to SSD1306 to update display 59 | * 60 | * @param fd: file descriptor 61 | */ 62 | void updateDisplay(int fd); 63 | // ---------------------------------------------- 64 | 65 | // --- Define constants ---- 66 | // infos can be found in SSD1306 datasheet 67 | // ---------------------------------------------- 68 | // commands 69 | #define SSD1306_DISPLAYON 0xAF 70 | #define SSD1306_DISPLAYOFF 0xAE 71 | #define SSD1306_NORMALDISPLAY 0xA6 72 | #define SSD1306_INVERTDISPLAY 0xA7 73 | #define SSD1306_MULTIPLEX_RATIO 0xA8 74 | 75 | // registers 76 | #define SSD1306_CONTROL_REG 0x00 // Co = 0, DC = 0 77 | #define SSD1306_DATA_REG 0x40 78 | // ---------------------------------------------- 79 | 80 | #endif /* SSD1306_I2C_H */ 81 | -------------------------------------------------------------------------------- /c-rasp/ssd1306-i2c/ssd1306_i2c.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | * About : SSD1306 OLED Driver Programm(using I2C Interface) 3 | * Depend on lib wiringPi 4 | * Author : Xiang,Zuo 5 | * Mail : xianglinks@gmail.com 6 | * Date : 2016-03-20 7 | * Ver : 0.3 8 | **************************************************************************/ 9 | 10 | #ifndef SSD1306_I2C_H 11 | #define SSD1306_I2C_H 12 | 13 | // --- Define functions --- 14 | // ---------------------------------------------- 15 | /** 16 | * @brief ssd1306I2CSetup 17 | * 18 | * @param i2cAddr: I2C device address 19 | * on Rpi, you can use i2cdetect -y 1 to get addr 20 | * 21 | * @return standard file descriptor for I2C operations 22 | */ 23 | int ssd1306I2CSetup(unsigned char i2cAddr); 24 | 25 | // send command byte to SSD1306 via I2C 26 | void sendCommand(int fd, unsigned char command); 27 | // send data byte to SSD1306 via I2C 28 | void sendData(int fd, unsigned char data); 29 | 30 | // OLED operations 31 | void displayOn(int fd); // turn on the display 32 | void displayOff(int fd); // turn off the display 33 | void clearDisplay(int fd); // clear the contents on display 34 | 35 | 36 | // Attention: The draw and draw_line functions only change the contents in local data frame 37 | // To update the contents on display should function updateDisplay() be used 38 | 39 | /** 40 | * @brief draw one ascii code at determined position 41 | * 42 | * @param x: 1 to 4 43 | * @param y: 1 to 16 44 | * @param ascii: one ascii code 45 | */ 46 | void draw(int x,int y,unsigned char ascii); 47 | 48 | /** 49 | * @brief draw one line of ascii codes at determined position on display 50 | * 51 | * @param x: 1 to 4 52 | * @param y: 1 to 16 53 | * @param ascii_str[16]: the number of ascii codes can not more than 16 54 | */ 55 | void draw_line(int x ,int y,unsigned char ascii_str[16]); 56 | 57 | /** 58 | * @brief write the frame data to SSD1306 to update display 59 | * 60 | * @param fd: file descriptor 61 | */ 62 | void updateDisplay(int fd); 63 | // ---------------------------------------------- 64 | 65 | // --- Define constants ---- 66 | // infos can be found in SSD1306 datasheet 67 | // ---------------------------------------------- 68 | // commands 69 | #define SSD1306_DISPLAYON 0xAF 70 | #define SSD1306_DISPLAYOFF 0xAE 71 | #define SSD1306_NORMALDISPLAY 0xA6 72 | #define SSD1306_INVERTDISPLAY 0xA7 73 | #define SSD1306_MULTIPLEX_RATIO 0xA8 74 | 75 | // registers 76 | #define SSD1306_CONTROL_REG 0x00 // Co = 0, DC = 0 77 | #define SSD1306_DATA_REG 0x40 78 | // ---------------------------------------------- 79 | 80 | #endif /* SSD1306_I2C_H */ 81 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################### 2 | # Custom Ignore # 3 | ################### 4 | 5 | # --- ignored dirs --- 6 | # ----------------------------------------------- 7 | /py-rasp/oled-display/docs 8 | /sync 9 | # ----------------------------------------------- 10 | 11 | # --- ignored files --- 12 | # ----------------------------------------------- 13 | /py-rasp/oled-display/display.log 14 | ./upload.sh 15 | *.pdf 16 | # ----------------------------------------------- 17 | 18 | 19 | ##################### 20 | # Standard Ignore # 21 | ##################### 22 | 23 | # --- C Ignore --- 24 | # ----------------------------------------------- 25 | # Object files 26 | *.o 27 | *.ko 28 | *.obj 29 | *.elf 30 | 31 | # Precompiled Headers 32 | *.gch 33 | *.pch 34 | 35 | # Libraries 36 | *.lib 37 | *.a 38 | *.la 39 | *.lo 40 | 41 | # Shared objects (inc. Windows DLLs) 42 | *.dll 43 | *.so 44 | *.so.* 45 | *.dylib 46 | 47 | # Executables 48 | *.exe 49 | *.out 50 | *.app 51 | *.i*86 52 | *.x86_64 53 | *.hex 54 | 55 | # Debug files 56 | *.dSYM/ 57 | # ----------------------------------------------- 58 | 59 | # --- Python Ignore --- 60 | # ----------------------------------------------- 61 | # Byte-compiled / optimized / DLL files 62 | __pycache__/ 63 | *.py[cod] 64 | *$py.class 65 | 66 | # C extensions 67 | *.so 68 | 69 | # Distribution / packaging 70 | .Python 71 | env/ 72 | build/ 73 | develop-eggs/ 74 | dist/ 75 | downloads/ 76 | eggs/ 77 | .eggs/ 78 | lib/ 79 | lib64/ 80 | parts/ 81 | sdist/ 82 | var/ 83 | *.egg-info/ 84 | .installed.cfg 85 | *.egg 86 | 87 | # PyInstaller 88 | # Usually these files are written by a python script from a template 89 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 90 | *.manifest 91 | *.spec 92 | 93 | # Installer logs 94 | pip-log.txt 95 | pip-delete-this-directory.txt 96 | 97 | # Unit test / coverage reports 98 | htmlcov/ 99 | .tox/ 100 | .coverage 101 | .coverage.* 102 | .cache 103 | nosetests.xml 104 | coverage.xml 105 | *,cover 106 | .hypothesis/ 107 | 108 | # Translations 109 | *.mo 110 | *.pot 111 | 112 | # Django stuff: 113 | *.log 114 | local_settings.py 115 | 116 | # Flask instance folder 117 | instance/ 118 | 119 | # Scrapy stuff: 120 | .scrapy 121 | 122 | # Sphinx documentation 123 | docs/_build/ 124 | 125 | # PyBuilder 126 | target/ 127 | 128 | # IPython Notebook 129 | .ipynb_checkpoints 130 | 131 | # pyenv 132 | .python-version 133 | 134 | # celery beat schedule file 135 | celerybeat-schedule 136 | 137 | # dotenv 138 | .env 139 | 140 | # virtualenv 141 | venv/ 142 | ENV/ 143 | 144 | # Spyder project settings 145 | .spyderproject 146 | # ----------------------------------------------- 147 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/input_test/gpio_functions.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : Functions for interaction with GPIO-pins on Rpi 3 | * Date : 2015-12-07 09:49:43 4 | * Author : Xiang,Zuo 5 | * Email : xianglinks@gmail.com 6 | ******************************************************************************/ 7 | 8 | /* -- Code Section -- */ 9 | .text 10 | .balign 4 11 | 12 | /* def for bcm-pins */ 13 | .equ bluePin, 12 14 | .equ greenPin, 47 15 | .equ redPin, 14 16 | .equ btnPin, 15 17 | 18 | /* set pin's mode: manipulating GPFSEL-Register 19 | * addr of register = base + 4 * ( pinNum / 10 ) 20 | * mode addr of pin = (pinNum % 10) * 3 */ 21 | 22 | .global set_gpio_input 23 | .func set_gpio_input 24 | set_gpio_input: 25 | ldr r1, [r0, #(4 * (btnPin / 10))] 26 | and r1, #~(7 << ((btnPin % 10) * 3)) /* set pin-bits to 000 */ 27 | str r1, [r0, #(4 * (btnPin / 10))] 28 | .endfunc 29 | 30 | .global set_gpio_output 31 | .func set_gpio_output 32 | /* set pin as output */ 33 | set_gpio_output: 34 | ldr r1, [r0, #(4 * (bluePin / 10))] 35 | and r1, #~(7 << ((bluePin % 10) * 3)) /* set pin-bits to 000 */ 36 | orr r1, #(1 << ((bluePin % 10) * 3)) /* set pin-bits to 001 */ 37 | str r1, [r0, #(4 * (bluePin / 10))] 38 | 39 | ldr r1, [r0, #(4 * (greenPin / 10))] 40 | and r1, #~(7 << ((greenPin % 10) * 3)) 41 | orr r1, #(1 << ((greenPin % 10) * 3)) 42 | str r1, [r0, #(4 * (greenPin / 10))] 43 | 44 | ldr r1, [r0, #(4 * (redPin / 10))] 45 | and r1, #~(7 << ((redPin % 10) * 3)) 46 | orr r1, #(1 << ((redPin % 10) * 3)) 47 | str r1, [r0, #(4 * (redPin / 10))] 48 | 49 | bx lr 50 | .endfunc 51 | 52 | /* control leds 53 | * set pin's output: manipulating GPSET-Register 54 | * GPSET0 - 0x7E20001C pin 00-32 ; GPSET1 - 0x7E200020 pin 32-53 55 | * register addr = 4 * (7 + pinNum/32) */ 56 | 57 | .global blue_led 58 | .func blue_led 59 | blue_led: 60 | mov r1, #(1 << (bluePin % 32)) 61 | str r1, [r0, #(4 * (7 + bluePin / 32))] 62 | 63 | mov r1, #(1 << (greenPin % 32)) 64 | str r1, [r0, #(4*(10 + (greenPin / 32)))] 65 | 66 | mov r3, #(1 << (redPin % 32)) 67 | str r3, [r0, #(4*(10 + (redPin / 32)))] 68 | 69 | bx lr 70 | .endfunc 71 | 72 | .global green_led 73 | .func green_led 74 | green_led: 75 | mov r1, #(1 << (greenPin % 32)) 76 | str r1, [r0, #(4 * (7 + greenPin / 32))] 77 | 78 | mov r1, #(1 << (bluePin % 32)) 79 | str r1, [r0, #(4*(10 + (bluePin / 32)))] 80 | 81 | mov r3, #(1 << (redPin % 32)) 82 | str r3, [r0, #(4*(10 + (redPin / 32)))] 83 | 84 | bx lr 85 | .endfunc 86 | 87 | .global red_led 88 | .func red_led 89 | /* red on and blue, green off */ 90 | red_led: 91 | mov r1, #(1 << (redPin % 32)) 92 | str r1, [r0, #(4 * (7 + redPin / 32))] 93 | 94 | mov r1, #(1 << (bluePin % 32)) 95 | str r1, [r0, #(4*(10 + (bluePin / 32)))] 96 | 97 | mov r3, #(1 << (greenPin % 32)) 98 | str r3, [r0, #(4*(10 + (greenPin / 32)))] 99 | 100 | bx lr 101 | .endfunc 102 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/button_test/ref.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : Functions for interaction with GPIO-pins on Rpi 3 | * Date : 2015-12-11 16:00:37 4 | * Author : Xiang,Zuo 5 | * Email : xianglinks@gmail.com 6 | ******************************************************************************/ 7 | 8 | /* -- Code Section -- */ 9 | .text 10 | .balign 4 11 | 12 | /* definations for BCM-pins 13 | * this number is the same as that on LK-RB-Shield 14 | * the mapping between BCM-pins, WiringPi-pins and Physical-pins 15 | * can be found on webseite: http://www.pinout.xyz/ 16 | */ 17 | .equ ledPin, 14 18 | .equ btnPin, 15 19 | 20 | /* detailed information about GPIO-Registers 21 | * can be found on datasheet bcm2835 on page 89-105 22 | */ 23 | 24 | /* set pin's mode: manipulating GPFSEL-Register 25 | * address of register = base + 4 * ( pinNum / 10 ) 26 | * each 3 bits stand for a pin 27 | * mode position of pin = (pinNum % 10) * 3 28 | * 000 = GPIO Pin is an input 29 | * 001 = GPIO Pin is an output 30 | * 100 = GPIO Pin takes alternate function 0 31 | * 101 = GPIO Pin takes alternate function 1 32 | * 110 = GPIO Pin takes alternate function 2 33 | * 111 = GPIO Pin takes alternate function 3 34 | * 011 = GPIO Pin takes alternate function 4 35 | * 010 = GPIO Pin takes alternate function 5 */ 36 | 37 | /* set pin(btnPin) as input */ 38 | .global set_pin_input 39 | .func set_pin_input 40 | set_pin_input: 41 | ldr r1, [r0, #(4 * (btnPin / 10))] 42 | and r1, #~(7 << ((btnPin % 10) * 3)) /* set pin-bits to 000 */ 43 | str r1, [r0, #(4 * (btnPin / 10))] 44 | bx lr 45 | .endfunc 46 | 47 | /* set pin(ledPin) as output */ 48 | .global set_pin_output 49 | .func set_pin_output 50 | set_pin_output: 51 | and r1, #~(7 << ((ledPin % 10) * 3)) /* set pin-bits to 000 */ 52 | orr r1, #(1 << ((ledPin % 10) * 3)) /* set pin-bits to 001 */ 53 | str r1, [r0, #(4 * (ledPin / 10))] 54 | bx lr 55 | .endfunc 56 | 57 | /* get pin(btnPin) volt-level: reading GPLEV-Register 58 | * address of register = base + #(51 + 4 * (pinNum / 32)) 59 | * each bit stand for volt-level of the pin(0:low, 1:high(3.3V)) 60 | */ 61 | .global get_pin_level 62 | .func get_pin_level 63 | get_pin_level: 64 | ldr r1, [r0, #(52 + 4 * (btnPin / 32))] 65 | lsr r1, #btnPin 66 | and r1, #1 67 | mov r0, r1 /* r0 as return value */ 68 | bx lr 69 | .endfunc 70 | 71 | /* control led 72 | * set pin's output: manipulating GPSET-Register 73 | * GPSET0: pin(00-32) ; GPSET1: pin (32-53) 74 | * address of register = 4 * (7 + pinNum / 32) 75 | * each bit stand for one pin(0:low, 1:high(3.3V)) 76 | */ 77 | 78 | /* idle_status: all leds off */ 79 | .global set_led_off 80 | .func set_led_off 81 | set_led_off: 82 | mov r1, #(1 << (ledPin % 32)) 83 | str r1, [r0, #(4*(10 + (ledPin / 32)))] 84 | bx lr 85 | .endfunc 86 | 87 | /* on_status: all leds on */ 88 | .global set_led_on 89 | .func set_led_on 90 | set_led_on: 91 | mov r1, #(1 << (ledPin % 32)) 92 | str r1, [r0, #(4 * (7 + ledPin / 32))] 93 | bx lr 94 | .endfunc 95 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/interaption_test/gpio_functions.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : Functions for interaction with GPIO-pins on Rpi 3 | * Date : 2015-12-11 16:00:37 4 | * Author : Xiang,Zuo 5 | * Email : xianglinks@gmail.com 6 | ******************************************************************************/ 7 | 8 | /* -- Code Section -- */ 9 | .text 10 | .balign 4 11 | 12 | /* definations for BCM-pins 13 | * this number is the same as that on LK-RB-Shield 14 | * the mapping between BCM-pins, WiringPi-pins and Physical-pins 15 | * can be found on webseite: http://www.pinout.xyz/ 16 | */ 17 | .equ ledPin, 14 18 | .equ btnPin, 15 19 | 20 | /* detailed information about GPIO-Registers 21 | * can be found on datasheet bcm2835 on page 89-105 22 | */ 23 | 24 | /* set pin's mode: manipulating GPFSEL-Register 25 | * address of register = base + 4 * ( pinNum / 10 ) 26 | * each 3 bits stand for a pin 27 | * mode position of pin = (pinNum % 10) * 3 28 | * 000 = GPIO Pin is an input 29 | * 001 = GPIO Pin is an output 30 | * 100 = GPIO Pin takes alternate function 0 31 | * 101 = GPIO Pin takes alternate function 1 32 | * 110 = GPIO Pin takes alternate function 2 33 | * 111 = GPIO Pin takes alternate function 3 34 | * 011 = GPIO Pin takes alternate function 4 35 | * 010 = GPIO Pin takes alternate function 5 */ 36 | 37 | /* set pin(btnPin) as input */ 38 | .global set_pin_input 39 | .func set_pin_input 40 | set_pin_input: 41 | ldr r1, [r0, #(4 * (btnPin / 10))] 42 | and r1, #~(7 << ((btnPin % 10) * 3)) /* set pin-bits to 000 */ 43 | str r1, [r0, #(4 * (btnPin / 10))] 44 | bx lr 45 | .endfunc 46 | 47 | /* set pin(ledPin) as output */ 48 | .global set_pin_output 49 | .func set_pin_output 50 | set_pin_output: 51 | and r1, #~(7 << ((ledPin % 10) * 3)) /* set pin-bits to 000 */ 52 | orr r1, #(1 << ((ledPin % 10) * 3)) /* set pin-bits to 001 */ 53 | str r1, [r0, #(4 * (ledPin / 10))] 54 | bx lr 55 | .endfunc 56 | 57 | /* get pin(btnPin) volt-level: reading GPLEV-Register 58 | * address of register = base + #(51 + 4 * (pinNum / 32)) 59 | * each bit stand for volt-level of the pin(0:low, 1:high(3.3V)) 60 | */ 61 | .global get_pin_level 62 | .func get_pin_level 63 | get_pin_level: 64 | ldr r1, [r0, #(52 + 4 * (btnPin / 32))] 65 | lsr r1, #btnPin 66 | and r1, #1 67 | mov r0, r1 /* r0 as return value */ 68 | bx lr 69 | .endfunc 70 | 71 | /* control led 72 | * set pin's output: manipulating GPSET-Register 73 | * GPSET0: pin(00-32) ; GPSET1: pin (32-53) 74 | * address of register = 4 * (7 + pinNum / 32) 75 | * each bit stand for one pin(0:low, 1:high(3.3V)) 76 | */ 77 | 78 | /* idle_status: all leds off */ 79 | .global set_led_off 80 | .func set_led_off 81 | set_led_off: 82 | mov r1, #(1 << (ledPin % 32)) 83 | str r1, [r0, #(4*(10 + (ledPin / 32)))] 84 | bx lr 85 | .endfunc 86 | 87 | /* on_status: all leds on */ 88 | .global set_led_on 89 | .func set_led_on 90 | set_led_on: 91 | mov r1, #(1 << (ledPin % 32)) 92 | str r1, [r0, #(4 * (7 + ledPin / 32))] 93 | bx lr 94 | .endfunc 95 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/button_led_control/gpio_functions.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : Functions for interaction with GPIO-pins on Rpi 3 | * Date : 2015-12-11 16:00:37 4 | * Author : Xiang,Zuo 5 | * Email : xianglinks@gmail.com 6 | ******************************************************************************/ 7 | 8 | /* -- Code Section -- */ 9 | .text 10 | .balign 4 11 | 12 | /* definations for BCM-pins 13 | * this number is the same as that on LK-RB-Shield 14 | * the mapping between BCM-pins, WiringPi-pins and Physical-pins 15 | * can be found on webseite: http://www.pinout.xyz/ 16 | */ 17 | .equ ledPin, 14 18 | .equ btnPin, 15 19 | 20 | /* detailed information about GPIO-Registers 21 | * can be found on datasheet bcm2835 on page 89-105 22 | */ 23 | 24 | /* set pin's mode: manipulating GPFSEL-Register 25 | * address of register = base + 4 * ( pinNum / 10 ) 26 | * each 3 bits stand for a pin 27 | * mode position of pin = (pinNum % 10) * 3 28 | * 000 = GPIO Pin is an input 29 | * 001 = GPIO Pin is an output 30 | * 100 = GPIO Pin takes alternate function 0 31 | * 101 = GPIO Pin takes alternate function 1 32 | * 110 = GPIO Pin takes alternate function 2 33 | * 111 = GPIO Pin takes alternate function 3 34 | * 011 = GPIO Pin takes alternate function 4 35 | * 010 = GPIO Pin takes alternate function 5 */ 36 | 37 | /* set pin(btnPin) as input */ 38 | .global set_pin_input 39 | .func set_pin_input 40 | set_pin_input: 41 | ldr r1, [r0, #(4 * (btnPin / 10))] 42 | and r1, #~(7 << ((btnPin % 10) * 3)) /* set pin-bits to 000 */ 43 | str r1, [r0, #(4 * (btnPin / 10))] 44 | bx lr 45 | .endfunc 46 | 47 | /* set pin(ledPin) as output */ 48 | .global set_pin_output 49 | .func set_pin_output 50 | set_pin_output: 51 | and r1, #~(7 << ((ledPin % 10) * 3)) /* set pin-bits to 000 */ 52 | orr r1, #(1 << ((ledPin % 10) * 3)) /* set pin-bits to 001 */ 53 | str r1, [r0, #(4 * (ledPin / 10))] 54 | bx lr 55 | .endfunc 56 | 57 | /* get pin(btnPin) volt-level: reading GPLEV-Register 58 | * address of register = base + #(51 + 4 * (pinNum / 32)) 59 | * each bit stand for volt-level of the pin(0:low, 1:high(3.3V)) 60 | */ 61 | .global get_pin_level 62 | .func get_pin_level 63 | get_pin_level: 64 | ldr r1, [r0, #(52 + 4 * (btnPin / 32))] 65 | lsr r1, #btnPin 66 | and r1, #1 67 | mov r0, r1 /* r0 as return value */ 68 | bx lr 69 | .endfunc 70 | 71 | /* control led 72 | * set pin's output: manipulating GPSET-Register 73 | * GPSET0: pin(00-32) ; GPSET1: pin (32-53) 74 | * address of register = 4 * (7 + pinNum / 32) 75 | * each bit stand for one pin(0:low, 1:high(3.3V)) 76 | */ 77 | 78 | /* idle_status: all leds off */ 79 | .global set_led_off 80 | .func set_led_off 81 | set_led_off: 82 | mov r1, #(1 << (ledPin % 32)) 83 | str r1, [r0, #(4*(10 + (ledPin / 32)))] 84 | bx lr 85 | .endfunc 86 | 87 | /* on_status: all leds on */ 88 | .global set_led_on 89 | .func set_led_on 90 | set_led_on: 91 | mov r1, #(1 << (ledPin % 32)) 92 | str r1, [r0, #(4 * (7 + ledPin / 32))] 93 | bx lr 94 | .endfunc 95 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/button_test/main.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : Simple LED control(GPIO) with JT-Button 3 | * Date : 2015-12-11 15:12:46 4 | * Author : Xiang,Zuo 5 | * Email : xianglinks@gmail.com 6 | ******************************************************************************/ 7 | 8 | /* ---------------- Code Section ---------------- */ 9 | .text 10 | .balign 4 11 | 12 | /* determined by Application Binary Interface(ABI) 13 | * the calling of functions(both asm and c functions) in asm 14 | * use registers r0-r3 as first four arguments 15 | * and the frist four return-values use also r0-r3 16 | * more than four parameters should realized with stack */ 17 | 18 | .equ btnPin2, 21 19 | 20 | /* import c fuctions */ 21 | .extern map_peripheral /* mapping a block (4096 bytes) of physical addresses to virtual space */ 22 | 23 | /* import asm functions */ 24 | .extern set_pin_input 25 | .extern set_pin_output 26 | .extern get_pin_level 27 | 28 | /* main function */ 29 | .global main 30 | .func main 31 | main: 32 | push {r4-r11, lr} 33 | 34 | /* address mapping using c function */ 35 | mapping_addr: 36 | bl map_peripheral 37 | mov r9, r0 /* save the GPIO_BASE in r9 */ 38 | mov r1, #-1 39 | cmp r0, r1 40 | bne gpio_settings 41 | /* when problem with mapping */ 42 | ldr r0, =errMsg1 43 | bl printf 44 | b end_main 45 | 46 | /* gpio settings */ 47 | gpio_settings: 48 | mov r0, r9 49 | bl set_pin_output 50 | 51 | mov r0, r9 52 | mov r1, #btnPin2 53 | bl set_pin_input 54 | 55 | /* The bit for programm-status is saved in r10 56 | * 0 -> led_off 57 | * 1 -> led_on 58 | * this status should be changed by button-input signal */ 59 | 60 | mov r10, #0 /* init with led_off */ 61 | 62 | /* init with idle_status */ 63 | mov r0, r9 64 | bl set_led_off 65 | 66 | /* main loop */ 67 | main_loop: 68 | 69 | /* the change of programm-status should be triggered 70 | * by falling edge of the button-input: when there is 10 71 | */ 72 | 73 | /* get button-input( on:1, off:0 ) */ 74 | mov r0, r9 75 | bl get_pin_level 76 | cmp r0, #0 /* no button input signal -> keep current status */ 77 | beq wait 78 | 79 | /* if button-value is 1 80 | * the next button-input should be read: waiting for falling edge 81 | */ 82 | check_falling_edge: 83 | mov r0, r9 84 | bl get_pin_level 85 | cmp r0, #1 86 | beq check_falling_edge 87 | 88 | /* if the falling edge is detected: 10 -> change status 89 | * change the status using XOR */ 90 | change_status: 91 | eor r10, r10, #1 92 | 93 | /* check the status-bit in r10 */ 94 | check_status: 95 | cmp r10, #0 96 | beq idle_status 97 | 98 | /* if the status-bit is 1 */ 99 | on_status: 100 | mov r0, r9 101 | bl set_led_on 102 | b wait /* wait some time for GPIO setting up */ 103 | 104 | /* if the status-bit is 0 */ 105 | idle_status: 106 | mov r0, r9 107 | bl set_led_off 108 | b wait 109 | 110 | /* delay for some time(here default about 0.05s) */ 111 | wait: 112 | mov r3, #5 /* change delay time by modifying this value */ 113 | wait_loop2: 114 | sub r3, #1 115 | mov r2, #0x3F0000 116 | wait_loop1: 117 | sub r2,#1 118 | cmp r2,#0 119 | bne wait_loop1 120 | cmp r3, #0 121 | bne wait_loop2 122 | b main_loop 123 | /* end main_loop */ 124 | 125 | end_main: 126 | mov r0, #0 127 | pop {r4-r11, pc} 128 | 129 | 130 | /* ---------------- Data Section ---------------- */ 131 | .data 132 | .balign 4 133 | 134 | /* definations for strings */ 135 | errMsg1: .ascii "memory mapping do not work. aborting...\n" 136 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/interaption_test/main.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : Simple LED control(GPIO) with JT-Button 3 | * Date : 2015-12-11 15:12:46 4 | * Author : Xiang,Zuo 5 | * Email : xianglinks@gmail.com 6 | ******************************************************************************/ 7 | 8 | /* ---------------- Code Section ---------------- */ 9 | .text 10 | .balign 4 11 | 12 | /* determined by Application Binary Interface(ABI) 13 | * the calling of functions(both asm and c functions) in asm 14 | * use registers r0-r3 as first four arguments 15 | * and the frist four return-values use also r0-r3 16 | * more than four parameters should realized with stack */ 17 | 18 | /* import c fuctions */ 19 | .extern map_peripheral /* mapping a block (4096 bytes) of physical addresses to virtual space */ 20 | 21 | /* import asm functions */ 22 | .extern set_pin_input 23 | .extern set_pin_output 24 | .extern get_pin_level 25 | .extern set_led_on 26 | .extern set_led_off 27 | 28 | /* main function */ 29 | .global main 30 | .func main 31 | main: 32 | push {r4-r11, lr} 33 | 34 | /* address mapping using c function */ 35 | mapping_addr: 36 | bl map_peripheral 37 | mov r9, r0 /* save the GPIO_BASE in r9 */ 38 | mov r1, #-1 39 | cmp r0, r1 40 | bne gpio_settings 41 | /* when problem with mapping */ 42 | ldr r0, =errMsg1 43 | bl printf 44 | b end_main 45 | 46 | /* gpio settings */ 47 | gpio_settings: 48 | mov r0, r9 49 | bl set_pin_output 50 | 51 | mov r0, r9 52 | bl set_pin_input 53 | 54 | /* The bit for programm-status is saved in r10 55 | * 0 -> led_off 56 | * 1 -> led_on 57 | * this status should be changed by button-input signal */ 58 | 59 | mov r10, #0 /* init with led_off */ 60 | 61 | /* init with idle_status */ 62 | mov r0, r9 63 | bl set_led_off 64 | 65 | /* main loop */ 66 | main_loop: 67 | 68 | /* the change of programm-status should be triggered 69 | * by falling edge of the button-input: when there is 10 70 | */ 71 | 72 | /* get button-input( on:1, off:0 ) */ 73 | mov r0, r9 74 | bl get_pin_level 75 | cmp r0, #0 /* no button input signal -> keep current status */ 76 | beq wait 77 | 78 | /* if button-value is 1 79 | * the next button-input should be read: waiting for falling edge 80 | */ 81 | check_falling_edge: 82 | mov r0, r9 83 | bl get_pin_level 84 | cmp r0, #1 85 | beq check_falling_edge 86 | 87 | /* if the falling edge is detected: 10 -> change status 88 | * change the status using XOR */ 89 | change_status: 90 | eor r10, r10, #1 91 | 92 | /* check the status-bit in r10 */ 93 | check_status: 94 | cmp r10, #0 95 | beq idle_status 96 | 97 | /* if the status-bit is 1 */ 98 | on_status: 99 | mov r0, r9 100 | bl set_led_on 101 | b wait /* wait some time for GPIO setting up */ 102 | 103 | /* if the status-bit is 0 */ 104 | idle_status: 105 | mov r0, r9 106 | bl set_led_off 107 | b wait 108 | 109 | /* delay for some time(here default about 0.05s) */ 110 | wait: 111 | mov r3, #5 /* change delay time by modifying this value */ 112 | wait_loop2: 113 | sub r3, #1 114 | mov r2, #0x3F0000 115 | wait_loop1: 116 | sub r2,#1 117 | cmp r2,#0 118 | bne wait_loop1 119 | cmp r3, #0 120 | bne wait_loop2 121 | b main_loop 122 | /* end main_loop */ 123 | 124 | end_main: 125 | mov r0, #0 126 | pop {r4-r11, pc} 127 | 128 | 129 | /* ---------------- Data Section ---------------- */ 130 | .data 131 | .balign 4 132 | 133 | /* definations for strings */ 134 | errMsg1: .ascii "memory mapping do not work. aborting...\n" 135 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/button_led_control/main.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : Simple LED control(GPIO) with JT-Button 3 | * Date : 2015-12-11 15:12:46 4 | * Author : Xiang,Zuo 5 | * Email : xianglinks@gmail.com 6 | ******************************************************************************/ 7 | 8 | /* ---------------- Code Section ---------------- */ 9 | .text 10 | .balign 4 11 | 12 | /* determined by Application Binary Interface(ABI) 13 | * the calling of functions(both asm and c functions) in asm 14 | * use registers r0-r3 as first four arguments 15 | * and the frist four return-values use also r0-r3 16 | * more than four parameters should realized with stack */ 17 | 18 | /* import c fuctions */ 19 | .extern map_peripheral /* mapping a block (4096 bytes) of physical addresses to virtual space */ 20 | 21 | /* import asm functions */ 22 | .extern set_pin_input 23 | .extern set_pin_output 24 | .extern get_pin_level 25 | .extern set_led_on 26 | .extern set_led_off 27 | 28 | /* main function */ 29 | .global main 30 | .func main 31 | main: 32 | push {r4-r11, lr} 33 | 34 | /* address mapping using c function */ 35 | mapping_addr: 36 | bl map_peripheral 37 | mov r9, r0 /* save the GPIO_BASE in r9 */ 38 | mov r1, #-1 39 | cmp r0, r1 40 | bne gpio_settings 41 | /* when problem with mapping */ 42 | ldr r0, =errMsg1 43 | bl printf 44 | b end_main 45 | 46 | /* gpio settings */ 47 | gpio_settings: 48 | mov r0, r9 49 | bl set_pin_output 50 | 51 | mov r0, r9 52 | bl set_pin_input 53 | 54 | /* The bit for programm-status is saved in r10 55 | * 0 -> led_off 56 | * 1 -> led_on 57 | * this status should be changed by button-input signal */ 58 | 59 | mov r10, #0 /* init with led_off */ 60 | 61 | /* init with idle_status */ 62 | mov r0, r9 63 | bl set_led_off 64 | 65 | /* main loop */ 66 | main_loop: 67 | 68 | /* the change of programm-status should be triggered 69 | * by falling edge of the button-input: when there is 10 70 | */ 71 | 72 | /* get button-input( on:1, off:0 ) */ 73 | mov r0, r9 74 | bl get_pin_level 75 | cmp r0, #0 /* no button input signal -> keep current status */ 76 | beq wait 77 | 78 | /* if button-value is 1 79 | * the next button-input should be read: waiting for falling edge 80 | */ 81 | check_falling_edge: 82 | mov r0, r9 83 | bl get_pin_level 84 | cmp r0, #1 85 | beq check_falling_edge 86 | 87 | /* if the falling edge is detected: 10 -> change status 88 | * change the status using XOR */ 89 | change_status: 90 | eor r10, r10, #1 91 | 92 | /* check the status-bit in r10 */ 93 | check_status: 94 | cmp r10, #0 95 | beq idle_status 96 | 97 | /* if the status-bit is 1 */ 98 | on_status: 99 | mov r0, r9 100 | bl set_led_on 101 | b wait /* wait some time for GPIO setting up */ 102 | 103 | /* if the status-bit is 0 */ 104 | idle_status: 105 | mov r0, r9 106 | bl set_led_off 107 | b wait 108 | 109 | /* delay for some time(here default about 0.05s) */ 110 | wait: 111 | mov r3, #5 /* change delay time by modifying this value */ 112 | wait_loop2: 113 | sub r3, #1 114 | mov r2, #0x3F0000 115 | wait_loop1: 116 | sub r2,#1 117 | cmp r2,#0 118 | bne wait_loop1 119 | cmp r3, #0 120 | bne wait_loop2 121 | b main_loop 122 | /* end main_loop */ 123 | 124 | end_main: 125 | mov r0, #0 126 | pop {r4-r11, pc} 127 | 128 | 129 | /* ---------------- Data Section ---------------- */ 130 | .data 131 | .balign 4 132 | 133 | /* definations for strings */ 134 | errMsg1: .ascii "memory mapping do not work. aborting...\n" 135 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/pwm_test/gpio_functions.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : Functions for interaction with GPIO-pins on Rpi 3 | * Date : 2015-12-11 16:00:37 4 | * Author : Xiang,Zuo 5 | * Email : xianglinks@gmail.com 6 | ******************************************************************************/ 7 | 8 | /* -- Code Section -- */ 9 | .text 10 | .balign 4 11 | 12 | /* definations for BCM-pins 13 | * this number is the same as that on LK-RB-Shield 14 | * the mapping between BCM-pins, WiringPi-pins and Physical-pins 15 | * can be found on webseite: http://www.pinout.xyz/ 16 | */ 17 | .equ pwmPin, 12 18 | .equ ledPin, 13 19 | .equ btnPin, 15 20 | 21 | /* set pin's mode: manipulating GPFSEL-Register 22 | * address of register = base + 4 * ( pinNum / 10 ) 23 | * each 3 bits stand for a pin 24 | * mode position of pin = (pinNum % 10) * 3 25 | * 000 = GPIO Pin is an input 26 | * 001 = GPIO Pin is an output 27 | * 100 = GPIO Pin takes alternate function 0 28 | * 101 = GPIO Pin takes alternate function 1 29 | * 110 = GPIO Pin takes alternate function 2 30 | * 111 = GPIO Pin takes alternate function 3 31 | * 011 = GPIO Pin takes alternate function 4 32 | * 010 = GPIO Pin takes alternate function 5 */ 33 | 34 | /* set pin(btnPin) as input */ 35 | .global set_gpio_input 36 | .func set_gpio_input 37 | set_gpio_input: 38 | push {lr} 39 | ldr r1, [r0, #(4 * (btnPin / 10))] 40 | and r1, #~(7 << ((btnPin % 10) * 3)) /* set pin-bits to 000 */ 41 | str r1, [r0, #(4 * (btnPin / 10))] 42 | pop {pc} 43 | .endfunc 44 | 45 | .global set_led_on 46 | .func set_led_on 47 | set_led_on: 48 | push {lr} 49 | mov r1, #(1 << (ledPin % 32)) 50 | str r1, [r0, #(4 * (7 + ledPin / 32))] 51 | pop {pc} 52 | .endfunc 53 | 54 | /* idle_status: all led off */ 55 | .global set_led_off 56 | .func set_led_off 57 | set_led_off: 58 | push {lr} 59 | mov r1, #(1 << (ledPin % 32)) 60 | str r1, [r0, #(4*(10 + (ledPin / 32)))] 61 | pop {pc} 62 | .endfunc 63 | 64 | /* get pin(btnPin) volt-level: reading GPLEV-Register 65 | * address of register = base + #(51 + 4 * (pinNum / 32)) 66 | * each bit stand for volt-level of the pin(0:low, 1:high(3.3V)) 67 | */ 68 | .global get_pin_level 69 | .func get_pin_level 70 | get_pin_level: 71 | push {lr} 72 | ldr r1, [r0, #(52 + 4 * (btnPin / 32))] 73 | lsr r1, #btnPin 74 | and r1, #1 75 | mov r0, r1 /* r0 as return value */ 76 | pop {pc} 77 | .endfunc 78 | 79 | /* set pin(ledPin) as output */ 80 | .global set_gpio_output 81 | .func set_gpio_output 82 | set_gpio_output: 83 | push {lr} 84 | ldr r1, [r0, #(4 * (ledPin / 10))] 85 | and r1, #~(7 << ((ledPin % 10) * 3)) /* set pin-bits to 000 */ 86 | orr r1, #(1 << ((ledPin % 10) * 3)) /* set pin-bits to 001 */ 87 | str r1, [r0, #(4 * (ledPin / 10))] 88 | pop {pc} 89 | .endfunc 90 | 91 | /* 92 | * PWM_BASE = (BCM2708_PERI_BASE + 0x20C000) -> PWM controller 93 | * Bit Streams configured individually to output either PWM or serialised version of 32 bits 94 | * both modes clocked by clk_pwm, which is normally 100MHz, can be varied by the clock manager 95 | * PWN controller consist of two independent channels -> channel0 & channel1 96 | * two sub-modes in PWM controller: MSEN = 0(default) and MSEN = 1 97 | * hardware support PWM: BCM-pin 12: PWM0, alternate function 0(alt0) 98 | */ 99 | 100 | .global set_pin_pwm 101 | .func set_pin_pwm 102 | set_pin_pwm: 103 | push {lr} 104 | ldr r1, [r0, #(4 * (pwmPin / 10))] 105 | and r1, #~(7 << ((pwmPin % 10) * 3)) /* set pin-bits to 000 */ 106 | orr r1, #(4 << ((pwmPin % 10) * 3)) /* set pin-bits to 100 -> alt func 0 */ 107 | str r1, [r0, #(4 * (pwmPin / 10))] 108 | pop {pc} 109 | .endfunc 110 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/pwm_test/mapping.c: -------------------------------------------------------------------------------- 1 | // Rpi1 #define BCM2708_PERI_BASE 0x20000000 2 | #define BCM2708_PERI_BASE 0x3f000000 // Rpi2 3 | #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */ 4 | #define PWM_BASE (BCM2708_PERI_BASE + 0x20C000) /* PWM controller */ 5 | #define CLOCK_BASE (BCM2708_PERI_BASE + 0x101000) /* CLOCK controller */ 6 | 7 | #define PWM_CTL 0 8 | #define PWM_RNG1 4 9 | #define PWM_DAT1 5 10 | 11 | #define PWMCLK_CNTL 40 // PWM CLOCK controller 12 | #define PWMCLK_DIV 41 // PWM CLOCK Divisor 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | 26 | #define PAGE_SIZE (4*1024) 27 | #define BLOCK_SIZE (4*1024) 28 | 29 | // I/O access 30 | volatile unsigned *peri_base; 31 | volatile unsigned *gpio; 32 | volatile unsigned *pwm; 33 | volatile unsigned *clk; 34 | 35 | // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y) 36 | #define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3)) 37 | #define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3)) 38 | #define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3)) 39 | 40 | #define GPIO_SET *(gpio+7) // sets bits which are 1 ignores bits which are 0 41 | #define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0 42 | 43 | // map 4k register memory for direct access from user space and return a user space pointer to it 44 | volatile unsigned *mapRegisterMemory(int base) 45 | { 46 | static int mem_fd = 0; 47 | char *mem, *map; 48 | 49 | /* open /dev/mem */ 50 | if (!mem_fd) { 51 | if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { 52 | printf("can't open /dev/mem \n"); 53 | exit (-1); 54 | } 55 | } 56 | 57 | /* mmap register */ 58 | 59 | // Allocate MAP block 60 | if ((mem = malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) { 61 | printf("allocation error \n"); 62 | exit (-1); 63 | } 64 | 65 | // Make sure pointer is on 4K boundary 66 | if ((unsigned long)mem % PAGE_SIZE) 67 | mem += PAGE_SIZE - ((unsigned long)mem % PAGE_SIZE); 68 | 69 | // Now map it 70 | map = (char *)mmap( 71 | (caddr_t)mem, 72 | BLOCK_SIZE, 73 | PROT_READ|PROT_WRITE, 74 | MAP_SHARED|MAP_FIXED, 75 | mem_fd, 76 | base 77 | ); 78 | 79 | if ((long)map < 0) { 80 | printf("mmap error %d\n", (int)map); 81 | exit (-1); 82 | } 83 | 84 | // Always use volatile pointer! 85 | return (volatile unsigned *)map; 86 | } 87 | 88 | // set up a memory regions to access GPIO, PWM and the clock manager 89 | void setupRegisterMemoryMappings() 90 | { 91 | peri_base = mapRegisterMemory(BCM2708_PERI_BASE); 92 | gpio = mapRegisterMemory(GPIO_BASE); 93 | pwm = mapRegisterMemory(PWM_BASE); 94 | clk = mapRegisterMemory(CLOCK_BASE); 95 | } 96 | 97 | // functions to get the addr 98 | int returnBCMPeriBaseAddr(void) 99 | { 100 | peri_base = mapRegisterMemory(BCM2708_PERI_BASE); 101 | return (volatile unsigned int *)peri_base; 102 | } 103 | 104 | int returnGPIOBaseAddr(void) 105 | { 106 | gpio = mapRegisterMemory(GPIO_BASE); 107 | return (volatile unsigned int *)gpio; 108 | } 109 | 110 | int returnPWMBaseAddr(void) 111 | { 112 | pwm = mapRegisterMemory(PWM_BASE); 113 | return (volatile unsigned int *)pwm; 114 | } 115 | 116 | 117 | int returnCLKBaseAddr(void) 118 | { 119 | clk = mapRegisterMemory(CLOCK_BASE); 120 | return (volatile unsigned int *)clk; 121 | } 122 | 123 | 124 | void setServo(int percent) 125 | { 126 | int bitCount; 127 | unsigned int bits = 0; 128 | 129 | // 32 bits = 2 milliseconds 130 | bitCount = 16 + 16 * percent / 100; 131 | if (bitCount > 32) bitCount = 32; 132 | if (bitCount < 1) bitCount = 1; 133 | bits = 0; 134 | while (bitCount) { 135 | bits <<= 1; 136 | bits |= 1; 137 | bitCount--; 138 | } 139 | *(pwm + PWM_DAT1) = bits; 140 | } 141 | -------------------------------------------------------------------------------- /c-rasp/oled-udp/server.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | * About : UDP Server 3 | * reciver temperature value and show it on the OLED display 4 | * Author : Xiang,Zuo 5 | * Mail : xianglinks@gmail.com 6 | * Date : 2016-03-22 7 | **************************************************************************/ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #include "ssd1306_i2c.h" 21 | 22 | // --- define constants --- 23 | #define BUFFSIZE 1024 24 | #define DELAY 1 25 | #define UDP_PORT 9000 26 | 27 | // --- catch keyboardinterrupt --- 28 | // flag for keyboard interrupt 29 | static volatile int keepRunning = 1; 30 | 31 | // interrupt signal handler 32 | void intHandler(int sig) 33 | { 34 | keepRunning = 0; 35 | } 36 | 37 | // --- main function --- 38 | int main(int argc, char* argv[]) 39 | { 40 | /* catch interrupt signal(SIGINT), i.e. ctrl+c 41 | * if interrupt detected -> keepRunning = 0 42 | * */ 43 | signal(SIGINT, intHandler); 44 | 45 | // init display 46 | // ------------------------------------------ 47 | int fd; // file descriptor for I2C writing 48 | fd = ssd1306I2CSetup(0x3C); // init SSD1306 and I2C interface 49 | displayOn(fd); 50 | // print init info on display 51 | printf("OLED display init finished\n"); 52 | draw_line(2, 1, "init done"); 53 | updateDisplay(fd); 54 | sleep(1); // wait for one second 55 | clearDisplay(fd); 56 | // ------------------------------------------ 57 | 58 | // init UDP socket 59 | // ------------------------------------------ 60 | struct sockaddr_in server_addr; // server net_addr struct: ipv4 61 | struct sockaddr_in remote_addr; // client net_addr struct: ipv4 62 | char buffer[BUFFSIZE]; // buffer for data trans BUFFSIZE defined in stdio.h (1024 for gcc) 63 | 64 | memset(&server_addr, 0, sizeof(server_addr)); // clear to zero 65 | // set server socket struct 66 | server_addr.sin_family = AF_INET; // use IP 67 | server_addr.sin_addr.s_addr = INADDR_ANY; // recive from any IP 68 | server_addr.sin_port = htons(UDP_PORT); // UDP port 69 | 70 | int serverSockFd; // server socket file descriptor 71 | 72 | // creat server socket 73 | if ((serverSockFd = socket(PF_INET, SOCK_DGRAM, 0)) < 0 ) { 74 | // creat failed 75 | perror("socket"); 76 | exit(1); 77 | } 78 | 79 | // bind server socket to server_addr struct 80 | // this step is not nesssary if reciving from any IPs 81 | if (bind(serverSockFd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) < 0) { 82 | perror("bind"); 83 | exit(1); 84 | } 85 | 86 | int sin_size = sizeof(struct sockaddr_in); 87 | 88 | printf("Server UDP socket init and bind finished\n"); 89 | // print socket init info on display 90 | draw_line(2, 1, "socket init done"); 91 | updateDisplay(fd); 92 | sleep(1); 93 | clearDisplay(fd); 94 | // ------------------------------------------ 95 | 96 | // -- main loop: recive tmp value and show it on display -- 97 | int len; // data len for recived data 98 | while( keepRunning ) { 99 | // use revfrom function to get data from client 100 | if ((len=(recvfrom(serverSockFd, buffer, BUFFSIZE, 0, (struct sockaddr *)&remote_addr, &sin_size))) < 0) { 101 | perror("revfrom"); 102 | exit(1); 103 | } 104 | 105 | buffer[len] = '\0'; // add string flag at the end of buffer 106 | printf("recieve: %s\n", buffer); 107 | 108 | draw_line(2, 1, buffer); 109 | updateDisplay(fd); 110 | sleep(DELAY); 111 | clearDisplay(fd); 112 | } 113 | 114 | // --- routine when interrupt detected --- 115 | clearDisplay(fd); 116 | // print off info on display 117 | draw_line(2, 1, "off in 2 seconds"); 118 | updateDisplay(fd); 119 | sleep(2); 120 | displayOff(fd); // turn off display 121 | close(serverSockFd); // close server socket 122 | printf("\nUDP Server closed\n"); 123 | 124 | return 0; 125 | } 126 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/temp_mess/gpio_functions.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : Functions for interaction with GPIO-pins on Rpi 3 | * Date : 2015-12-07 09:49:43 4 | * Author : Xiang,Zuo 5 | * Email : xianglinks@gmail.com 6 | ******************************************************************************/ 7 | 8 | /* -- Code Section -- */ 9 | .text 10 | .balign 4 11 | 12 | /* definations for BCM-pins 13 | * this number is the same as that on LK-RB-Shield 14 | * the mapping between BCM-pins, WiringPi-pins and Physical-pins 15 | * can be found on webseite: http://www.pinout.xyz/ 16 | */ 17 | .equ yellowPin, 12 18 | .equ greenPin, 13 19 | .equ redPin, 14 20 | .equ btnPin, 15 21 | 22 | /* detailed information about GPIO-Registers 23 | * can be found on datasheet bcm2835 on page 89-105 24 | */ 25 | 26 | /* set pin's mode: manipulating GPFSEL-Register 27 | * address of register = base + 4 * ( pinNum / 10 ) 28 | * each 3 bits stand for a pin 29 | * mode position of pin = (pinNum % 10) * 3 30 | * 000 = GPIO Pin is an input 31 | * 001 = GPIO Pin is an output 32 | * 100 = GPIO Pin takes alternate function 0 33 | * 101 = GPIO Pin takes alternate function 1 34 | * 110 = GPIO Pin takes alternate function 2 35 | * 111 = GPIO Pin takes alternate function 3 36 | * 011 = GPIO Pin takes alternate function 4 37 | * 010 = GPIO Pin takes alternate function 5 */ 38 | 39 | /* set pin(btnPin) as input */ 40 | .global set_pin_input 41 | .func set_pin_input 42 | set_pin_input: 43 | ldr r1, [r0, #(4 * (btnPin / 10))] 44 | and r1, #~(7 << ((btnPin % 10) * 3)) /* set pin-bits to 000 */ 45 | str r1, [r0, #(4 * (btnPin / 10))] 46 | bx lr 47 | .endfunc 48 | 49 | /* set pins as output */ 50 | .global set_pin_output 51 | .func set_pin_output 52 | set_pin_output: 53 | ldr r1, [r0, #(4 * (yellowPin / 10))] 54 | and r1, #~(7 << ((yellowPin % 10) * 3)) /* set pin-bits to 000 */ 55 | orr r1, #(1 << ((yellowPin % 10) * 3)) /* set pin-bits to 001 */ 56 | str r1, [r0, #(4 * (yellowPin / 10))] 57 | ldr r1, [r0, #(4 * (greenPin / 10))] 58 | and r1, #~(7 << ((greenPin % 10) * 3)) 59 | orr r1, #(1 << ((greenPin % 10) * 3)) 60 | str r1, [r0, #(4 * (greenPin / 10))] 61 | ldr r1, [r0, #(4 * (redPin / 10))] 62 | and r1, #~(7 << ((redPin % 10) * 3)) 63 | orr r1, #(1 << ((redPin % 10) * 3)) 64 | str r1, [r0, #(4 * (redPin / 10))] 65 | bx lr 66 | .endfunc 67 | 68 | /* get pin(btnPin) volt-level: reading GPLEV-Register 69 | * address of register = base + #(52 + 4 * (pinNum / 32)) 70 | * each bit stand for volt-level of the pin(0:low, 1:high(3.3V)) 71 | */ 72 | .global get_pin_level 73 | .func get_pin_level 74 | get_pin_level: 75 | ldr r1, [r0, #(52 + 4 * (btnPin / 32))] 76 | lsr r1, #btnPin 77 | and r1, #1 78 | mov r0, r1 /* r0 as return value */ 79 | bx lr 80 | .endfunc 81 | 82 | /* control leds 83 | * set pin's output: manipulating GPSET-Register 84 | * GPSET0: pin(00-32) ; GPSET1: pin (32-53) 85 | * address of register = 4 * (7 + pinNum / 32) 86 | * each bit stand for one pin(0:low, 1:high(3.3V)) 87 | */ 88 | .global yellow_led 89 | .func yellow_led 90 | yellow_led: 91 | mov r1, #(1 << (yellowPin % 32)) 92 | str r1, [r0, #(4 * (7 + yellowPin / 32))] 93 | mov r1, #(1 << (greenPin % 32)) 94 | str r1, [r0, #(4*(10 + (greenPin / 32)))] 95 | mov r1, #(1 << (redPin % 32)) 96 | str r1, [r0, #(4*(10 + (redPin / 32)))] 97 | bx lr 98 | .endfunc 99 | 100 | .global green_led 101 | .func green_led 102 | green_led: 103 | mov r1, #(1 << (greenPin % 32)) 104 | str r1, [r0, #(4 * (7 + greenPin / 32))] 105 | mov r1, #(1 << (yellowPin % 32)) 106 | str r1, [r0, #(4*(10 + (yellowPin / 32)))] 107 | mov r1, #(1 << (redPin % 32)) 108 | str r1, [r0, #(4*(10 + (redPin / 32)))] 109 | bx lr 110 | .endfunc 111 | 112 | .global red_led 113 | .func red_led 114 | red_led: 115 | mov r1, #(1 << (redPin % 32)) 116 | str r1, [r0, #(4 * (7 + redPin / 32))] 117 | mov r1, #(1 << (yellowPin % 32)) 118 | str r1, [r0, #(4*(10 + (yellowPin / 32)))] 119 | mov r1, #(1 << (greenPin % 32)) 120 | str r1, [r0, #(4*(10 + (greenPin / 32)))] 121 | bx lr 122 | .endfunc 123 | 124 | /* idle_status: all leds off */ 125 | .global set_idle 126 | .func set_idle 127 | set_idle: 128 | mov r1, #(1 << (yellowPin % 32)) 129 | str r1, [r0, #(4*(10 + (yellowPin / 32)))] 130 | mov r1, #(1 << (greenPin % 32)) 131 | str r1, [r0, #(4*(10 + (greenPin / 32)))] 132 | mov r1, #(1 << (redPin % 32)) 133 | str r1, [r0, #(4*(10 + (redPin / 32)))] 134 | bx lr 135 | .endfunc 136 | -------------------------------------------------------------------------------- /c-rasp/oled-udp/client.c: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | * About : UDP Client 3 | * get temperature value and send it to server via UDP 4 | * Author : Xiang,Zuo 5 | * Mail : xianglinks@gmail.com 6 | * Date : 2016-03-22 7 | **************************************************************************/ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #include 22 | #include 23 | 24 | // --- define constants --- 25 | #define UDP_PORT 9000 26 | #define BUFFSIZE 1024 27 | #define MAXVOLT 3.3 28 | #define DELAY 1 29 | 30 | #define BASE 200 31 | #define SPI_CHAN 0 32 | 33 | 34 | // --- define functions --- 35 | // get temperature value 36 | float read_temp(void); 37 | 38 | // flag for keyboard interrupt 39 | static volatile int keepRunning = 1; 40 | 41 | // interrupt signal handler 42 | void intHandler(int sig) 43 | { 44 | keepRunning = 0; 45 | } 46 | 47 | // --- main function --- 48 | int main(int argc, char *argv[]) 49 | { 50 | /* catch interrupt signal(SIGINT), i.e. ctrl+c 51 | * if interrupt detected -> keepRunning = 0 52 | * */ 53 | signal(SIGINT, intHandler); 54 | 55 | // --- init MCP3008 ADC --- 56 | // ------------------------------------------ 57 | if(wiringPiSetup () == -1) { 58 | printf("wiringP init do not work. exiting.\n"); 59 | exit(1); 60 | } 61 | // init MCP3008-ADC(using SPI interface on channel 0) 62 | mcp3004Setup(BASE, SPI_CHAN); 63 | printf("MCP3008 ADC init done\n"); 64 | // ------------------------------------------ 65 | 66 | // -- init UDP socket -- 67 | // ------------------------------------------ 68 | int clientSockFd; 69 | struct sockaddr_in remote_addr; // server net_addr struct 70 | 71 | char buffer[BUFFSIZE]; // buffer for data transmitt 72 | 73 | memset(&remote_addr, 0, sizeof(remote_addr)); // clear to zero 74 | 75 | // set remote_addr struct 76 | remote_addr.sin_family=AF_INET; // use IPv4 77 | remote_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); // server IP 78 | remote_addr.sin_port=htons(UDP_PORT); // UDP port 79 | 80 | // creat UDP socket 81 | if((clientSockFd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) 82 | { 83 | perror("socket"); 84 | exit(1); 85 | } 86 | 87 | int sin_size; 88 | sin_size=sizeof(struct sockaddr_in); 89 | printf("UDP socket init done\n"); 90 | // ------------------------------------------ 91 | 92 | // --- main loop --- 93 | // read tmp value and send it to server 94 | float tmp; //tmp value 95 | char tmpChar[6]; // tmp char 96 | 97 | while(keepRunning) { 98 | tmp = read_temp(); // get tmp value 99 | sprintf(tmpChar, "%.1f", tmp); // convert float to string 100 | strcpy(buffer, tmpChar); // copy tmp char into buffer 101 | printf("sending: %s\n", buffer); 102 | // send data in buffer to server 103 | int len; 104 | /* 105 | * strlen(): scan from somewhere to the first str_end_flag('\0') and return the counter, exclusive the str_end_flag 106 | * therefore the str_end_flag should be added at the reciver 107 | * */ 108 | if((len = sendto(clientSockFd, buffer, strlen(buffer), 0, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr))) < 0) 109 | { // return -1 when error 110 | perror("sendto"); 111 | exit(1); 112 | } 113 | sleep(DELAY); 114 | } 115 | 116 | // --- routine when interrupt detected --- 117 | close(clientSockFd); // close socket 118 | printf("\nUDP Client closed\n"); 119 | return 0; 120 | } 121 | 122 | /** 123 | * @brief: get temperature using TMP64 sensor and MCP3008 ADC 124 | * 125 | * @return: temperature value 126 | */ 127 | float read_temp(void) { 128 | int value; 129 | float volt, volt_m, tmp; 130 | 131 | // read digital value from MCP3008 132 | value = analogRead(BASE); 133 | 134 | // clac the voltage 135 | volt = (value * MAXVOLT) / 1024.0; 136 | 137 | // convert voltage to temperature 138 | volt_m = volt * 1000; //convert V to mV 139 | // TMP64, scale factor:10 and offset:0.5V 140 | tmp = 25 + (volt_m - 750) / 10.0; 141 | // tmp = (volt_m - 500) / 10.0; // same result 142 | 143 | // round the tmp value to 1 decimal places 144 | tmp = roundf(tmp * 10) / 10; 145 | return tmp; 146 | } 147 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/temp_mess/main.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : Temperature measurement and processing on Rpi 3 | * Date : 2015-12-03 23:52:37 4 | * Author : Xiang,Zuo 5 | * Email : xianglinks@gmail.com 6 | ******************************************************************************/ 7 | 8 | /* ---------------- Code Section ---------------- */ 9 | .text 10 | .balign 4 11 | 12 | /* determined by Application Binary Interface(ABI) 13 | * the calling of functions(both asm and c functions) in asm 14 | * use registers r0-r3 as first four arguments 15 | * and the frist four return-values use also r0-r3 16 | * more than four parameters should realized with stack */ 17 | 18 | /* import c fuctions */ 19 | .extern init_mcp /* init wiring-pi and mcp3008 */ 20 | .extern read_temp /* read_temp using wiringPi */ 21 | .extern map_peripheral /* mapping the physical addr */ 22 | .extern printf /* print value on the screen */ 23 | 24 | /* import asm functions */ 25 | .extern set_pin_input 26 | .extern set_pin_output 27 | .extern get_pin_level 28 | .extern set_idle 29 | .extern yellow_led 30 | .extern green_led 31 | .extern red_led 32 | 33 | /* main function */ 34 | .global main 35 | .func main 36 | main: 37 | push {r4-r11, lr} 38 | 39 | /* initiation wiring_pins and mcp3008 40 | * (run only once) */ 41 | bl init_mcp 42 | mov r1, #-1 43 | cmp r0, r1 44 | bne mapping_addr 45 | /* when problem */ 46 | ldr r0, =errMsg1 47 | bl printf 48 | b end_main 49 | 50 | /* addr_mapping using c function */ 51 | mapping_addr: 52 | bl map_peripheral 53 | mov r9, r0 /* save gpio_base_addr in r9 */ 54 | mov r1, #-1 55 | cmp r0, r1 56 | bne gpio_settings 57 | /* when problem */ 58 | ldr r0, =errMsg2 59 | bl printf 60 | b end_main 61 | 62 | gpio_settings: 63 | mov r0, r9 64 | bl set_pin_output 65 | 66 | mov r0, r9 67 | bl set_pin_input 68 | 69 | /* The bit for programm-status is saved in r10 70 | * 0 -> idle_status: stop temp-processing 71 | * 1 -> proc_status: start temp-processing 72 | * this status should be changed by button-input */ 73 | 74 | mov r10, #0 /* init with idle_status */ 75 | 76 | /* main loop */ 77 | main_loop: 78 | 79 | /* the change of programm-status should be triggered 80 | * by falling edge of the button-input: 10 */ 81 | 82 | /* get button-input( on:1, off:0 ) */ 83 | mov r0, r9 84 | bl get_pin_level 85 | cmp r0, #0 86 | beq check_status 87 | 88 | /* if button-value is 1 89 | * the next button-input should be read 90 | */ 91 | check_falling_edge: 92 | mov r0, r9 93 | bl get_pin_level 94 | cmp r0, #1 95 | beq check_falling_edge /* if the input is still 1 -> loop for falling edge checking */ 96 | 97 | /* if the falling edge is detected: 10 -> change status 98 | * change the status using XOR */ 99 | 100 | change_status: 101 | eor r10, r10, #1 102 | 103 | /* check the status-bit in r10 */ 104 | check_status: 105 | /* check up status */ 106 | cmp r10, #0 107 | beq idle_status 108 | 109 | proc_staus: 110 | /* get the temp-value */ 111 | bl read_temp 112 | mov r4, r0 /* save the temp-value in r4 before calling fuctions */ 113 | mov r1, r0 114 | /* print the tmp-value on screen */ 115 | ldr r0, =tempMsg 116 | bl printf 117 | 118 | /* processing of temp-value */ 119 | mov r5, #25 120 | cmp r4, r5 121 | bgt high_temp 122 | 123 | mov r5, #19 124 | cmp r4, r5 125 | bgt med_temp 126 | 127 | /* temperature < 20 -> yellow_led */ 128 | low_temp: 129 | mov r0, r9 130 | bl yellow_led 131 | b wait 132 | 133 | /* temperature > 25C -> red_led*/ 134 | high_temp: 135 | mov r0, r9 136 | bl red_led 137 | b wait 138 | 139 | /* 20C =< temperature <= 25C -> green_led */ 140 | med_temp: 141 | mov r0, r9 142 | bl green_led 143 | b wait 144 | 145 | /* operate for idle status */ 146 | idle_status: 147 | mov r0, r9 148 | bl set_idle 149 | b wait 150 | 151 | /* delay for some time(here default about 0.2s) */ 152 | wait: 153 | mov r3, #20 /* change delay time by modifying this value */ 154 | wait_loop2: 155 | sub r3, #1 156 | mov r2, #0x3F0000 157 | wait_loop1: 158 | sub r2,#1 159 | cmp r2,#0 160 | bne wait_loop1 161 | cmp r3, #0 162 | bne wait_loop2 163 | b main_loop 164 | /* end main_loop */ 165 | 166 | end_main: 167 | mov r0, #0 168 | pop {r4-r11, pc} 169 | 170 | 171 | /* ---------------- Data Section ---------------- */ 172 | .data 173 | .balign 4 174 | 175 | /* definations for strings */ 176 | errMsg1: .ascii "initiation do not work. aborting...\n" 177 | errMsg2: .ascii "memory mapping do not work. aborting...\n" 178 | tempMsg: .ascii "temperature is: %dC.\n" 179 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/calc_led/main.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : Calculate the Addition of Two Numbers inputed by Button and show the 3 | result using Leds 4 | * Date : 2016-01-08 11:48:28 5 | * Author : Xiang,Zuo 6 | * Email : xianglinks@gmail.com 7 | ******************************************************************************/ 8 | 9 | /* ---------------- Code Section ---------------- */ 10 | .text 11 | .balign 4 12 | 13 | /* determined by Application Binary Interface(ABI) 14 | * the calling of functions(both asm and c functions) in asm 15 | * use registers r0-r3 as first four arguments 16 | * and the frist four return-values use also r0-r3 17 | * more than four parameters should realized with stack */ 18 | 19 | /* init input pins */ 20 | .equ btnPin1, 5 21 | .equ btnPin2, 15 22 | 23 | /* import c fuctions */ 24 | .extern map_peripheral /* mapping a block (4096 bytes) of physical addresses to virtual space */ 25 | .extern printf 26 | 27 | /* import asm functions */ 28 | .extern set_pin_input 29 | .extern set_pin_output 30 | .extern get_pin_level 31 | 32 | .extern set_idle 33 | .extern blink_yellow_led 34 | .extern blink_red_led 35 | .extern blink_all_leds 36 | 37 | .extern show_sum_led 38 | .extern show_sum_blink 39 | 40 | .extern wait 41 | 42 | /* main function */ 43 | .global main 44 | .func main 45 | main: 46 | push {r4-r11, lr} 47 | 48 | /* address mapping using c function */ 49 | mapping_addr: 50 | bl map_peripheral 51 | mov r9, r0 /* save the GPIO_BASE in r9 */ 52 | mov r1, #-1 53 | cmp r0, r1 54 | bne gpio_settings 55 | /* when problem with mapping */ 56 | ldr r0, =errMsg1 57 | bl printf 58 | b end_main 59 | 60 | /* gpio settings */ 61 | gpio_settings: 62 | mov r0, r9 63 | bl set_pin_input 64 | 65 | mov r0, r9 66 | bl set_pin_output 67 | 68 | /* init registers for input1 and input2 */ 69 | mov r7, #0 70 | mov r8, #0 71 | 72 | /* set all leds off */ 73 | mov r0, r9 74 | bl set_idle 75 | 76 | /* get the first value using button1 */ 77 | ldr r0, =infoMsg1 78 | bl printf 79 | 80 | get_btn1_input: 81 | mov r0, r9 82 | mov r1, #btnPin1 83 | bl get_pin_level 84 | cmp r0, #0 85 | beq get_btn1_input 86 | 87 | check_btn2_input: 88 | mov r0, r9 89 | mov r1, #btnPin2 90 | bl get_pin_level 91 | cmp r0, #1 92 | beq end_btn1_input 93 | 94 | check_btn1_falling_edge: 95 | mov r0, r9 96 | mov r1, #btnPin1 97 | bl get_pin_level 98 | cmp r0, #1 99 | beq check_btn1_falling_edge 100 | 101 | /* when the falling edge of button1 is detected */ 102 | update_input1: 103 | add r7, r7, #1 104 | mov r0, r9 105 | bl blink_yellow_led 106 | b get_btn1_input 107 | /* end_btn1_input */ 108 | 109 | end_btn1_input: 110 | /* get the second value using button2 */ 111 | ldr r0, =infoMsg2 112 | bl printf 113 | mov r0, #300 114 | bl wait 115 | 116 | get_btn2_input: 117 | mov r0, r9 118 | mov r1, #btnPin2 119 | bl get_pin_level 120 | cmp r0, #0 121 | beq get_btn2_input 122 | 123 | check_btn1_input: 124 | mov r0, r9 125 | mov r1, #btnPin1 126 | bl get_pin_level 127 | cmp r0, #1 128 | beq start_calc 129 | 130 | check_btn2_falling_edge: 131 | mov r0, r9 132 | mov r1, #btnPin2 133 | bl get_pin_level 134 | cmp r0, #1 135 | beq check_btn2_falling_edge 136 | 137 | /* when the falling edge of button1 is detected */ 138 | update_input2: 139 | add r8, r8, #1 140 | mov r0, r9 141 | bl blink_red_led 142 | b get_btn2_input 143 | /* end_btn2_input */ 144 | 145 | start_calc: 146 | mov r0, r9 147 | bl blink_all_leds 148 | /* print the value on screen */ 149 | ldr r0, =valueMsg 150 | add r1, r7, r8 151 | bl printf 152 | add r6, r7, r8 153 | /* if the result is greater than 7 -> use blink to show result */ 154 | cmp r6, #7 155 | bgt use_blink 156 | /* otherwise leds should be used as binary code */ 157 | use_led: 158 | mov r0, r9 159 | mov r1, r6 160 | mov r2, #12 161 | bl show_sum_led 162 | b end_main 163 | 164 | use_blink: 165 | mov r0, r9 166 | mov r1, r6 167 | bl show_sum_blink 168 | 169 | end_main: 170 | mov r0, #2000 171 | bl wait 172 | mov r0, r9 173 | bl set_idle 174 | mov r0, #0 175 | pop {r4-r11, pc} 176 | .endfunc 177 | 178 | /* ---------------- Data Section ---------------- */ 179 | .data 180 | .balign 4 181 | 182 | /* definations for strings 183 | * asciz: add the final zero byte automatically 184 | * ascii: the final zero bytes should manually added 185 | */ 186 | errMsg1: .asciz "memory mapping do not work. aborting...\n" 187 | infoMsg1: .asciz "please input the first value using button1: \n" 188 | infoMsg2: .asciz "please input the second value using button2: \n" 189 | valueMsg: .asciz "the sum is %d\n" 190 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/pwm_led/pwm_orignal.c: -------------------------------------------------------------------------------- 1 | //RPi 1 #define BCM2708_PERI_BASE 0x20000000 2 | #define BCM2708_PERI_BASE 0x3f000000 3 | #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */ 4 | #define PWM_BASE (BCM2708_PERI_BASE + 0x20C000) /* PWM controller */ 5 | #define CLOCK_BASE (BCM2708_PERI_BASE + 0x101000) 6 | 7 | #define PWM_CTL 0 8 | #define PWM_RNG1 4 9 | #define PWM_DAT1 5 10 | 11 | #define PWMCLK_CNTL 40 12 | #define PWMCLK_DIV 41 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | 26 | #define PAGE_SIZE (4*1024) 27 | #define BLOCK_SIZE (4*1024) 28 | 29 | // I/O access 30 | volatile unsigned *gpio; 31 | volatile unsigned *pwm; 32 | volatile unsigned *clk; 33 | 34 | // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y) 35 | #define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3)) 36 | #define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3)) 37 | #define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3)) 38 | 39 | #define GPIO_SET *(gpio+7) // sets bits which are 1 ignores bits which are 0 40 | #define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0 41 | 42 | // map 4k register memory for direct access from user space and return a user space pointer to it 43 | volatile unsigned *mapRegisterMemory(int base) 44 | { 45 | static int mem_fd = 0; 46 | char *mem, *map; 47 | 48 | /* open /dev/mem */ 49 | if (!mem_fd) { 50 | if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { 51 | printf("can't open /dev/mem \n"); 52 | exit (-1); 53 | } 54 | } 55 | 56 | /* mmap register */ 57 | 58 | // Allocate MAP block 59 | if ((mem = malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) { 60 | printf("allocation error \n"); 61 | exit (-1); 62 | } 63 | 64 | // Make sure pointer is on 4K boundary 65 | if ((unsigned long)mem % PAGE_SIZE) 66 | mem += PAGE_SIZE - ((unsigned long)mem % PAGE_SIZE); 67 | 68 | // Now map it 69 | map = (char *)mmap( 70 | (caddr_t)mem, 71 | BLOCK_SIZE, 72 | PROT_READ|PROT_WRITE, 73 | MAP_SHARED|MAP_FIXED, 74 | mem_fd, 75 | base 76 | ); 77 | 78 | if ((long)map < 0) { 79 | printf("mmap error %d\n", (int)map); 80 | exit (-1); 81 | } 82 | 83 | // Always use volatile pointer! 84 | return (volatile unsigned *)map; 85 | } 86 | 87 | // set up a memory regions to access GPIO, PWM and the clock manager 88 | void setupRegisterMemoryMappings() 89 | { 90 | gpio = mapRegisterMemory(GPIO_BASE); 91 | pwm = mapRegisterMemory(PWM_BASE); 92 | clk = mapRegisterMemory(CLOCK_BASE); 93 | } 94 | 95 | void setServo(int percent) 96 | { 97 | int bitCount; 98 | unsigned int bits = 0; 99 | 100 | // 32 bits = 2 milliseconds 101 | bitCount = 16 + 16 * percent / 100; 102 | if (bitCount > 32) bitCount = 32; 103 | if (bitCount < 1) bitCount = 1; 104 | bits = 0; 105 | while (bitCount) { 106 | bits <<= 1; 107 | bits |= 1; 108 | bitCount--; 109 | } 110 | *(pwm + PWM_DAT1) = bits; 111 | } 112 | 113 | // init hardware 114 | void initHardware() 115 | { 116 | // mmap register space 117 | setupRegisterMemoryMappings(); 118 | 119 | // set PWM alternate function for GPIO18 120 | SET_GPIO_ALT(12, 0); 121 | 122 | // stop clock and waiting for busy flag doesn't work, so kill clock 123 | *(clk + PWMCLK_CNTL) = 0x5A000000 | (1 << 5); 124 | usleep(10); 125 | 126 | // set frequency 127 | // DIVI is the integer part of the divisor 128 | // the fractional part (DIVF) drops clock cycles to get the output frequency, bad for servo motors 129 | // 320 bits for one cycle of 20 milliseconds = 62.5 us per bit = 16 kHz 130 | int idiv = (int) (19200000.0f / 16000.0f); 131 | if (idiv < 1 || idiv > 0x1000) { 132 | printf("idiv out of range: %x\n", idiv); 133 | exit(-1); 134 | } 135 | *(clk + PWMCLK_DIV) = 0x5A000000 | (idiv<<12); 136 | 137 | // source=osc and enable clock 138 | *(clk + PWMCLK_CNTL) = 0x5A000011; 139 | 140 | // disable PWM 141 | *(pwm + PWM_CTL) = 0; 142 | 143 | // needs some time until the PWM module gets disabled, without the delay the PWM module crashs 144 | usleep(10); 145 | 146 | // filled with 0 for 20 milliseconds = 320 bits 147 | *(pwm + PWM_RNG1) = 320; 148 | 149 | // 32 bits = 2 milliseconds, init with 1 millisecond 150 | setServo(0); 151 | 152 | // start PWM1 in serializer mode 153 | *(pwm + PWM_CTL) = 3; 154 | } 155 | 156 | int main(int argc, char **argv) 157 | { 158 | // init PWM module for GPIO pin 18 with 50 Hz frequency 159 | initHardware(); 160 | 161 | // servo test, position in percent: 0 % = 1 ms, 100 % = 2 ms 162 | while (1) { 163 | setServo(0); 164 | sleep(1); 165 | setServo(25); 166 | sleep(1); 167 | setServo(50); 168 | sleep(1); 169 | setServo(75); 170 | sleep(1); 171 | setServo(100); 172 | sleep(1); 173 | } 174 | return 0; 175 | } 176 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/led_rhythm/gpio_functions.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : Functions for interaction with GPIO-pins on Rpi 3 | * Date : 2015-12-14 12:26:40 4 | * Author : Xiang,Zuo 5 | * Email : xianglinks@gmail.com 6 | ******************************************************************************/ 7 | 8 | /* -- Code Section -- */ 9 | .text 10 | .balign 4 11 | 12 | /* definations for BCM-pins 13 | * this number is the same as that on LK-RB-Shield 14 | * the mapping between BCM-pins, WiringPi-pins and Physical-pins 15 | * can be found on webseite: http://www.pinout.xyz/ 16 | */ 17 | 18 | .equ yellowPin, 12 19 | .equ greenPin, 13 20 | .equ redPin, 14 21 | .equ btnPin, 15 22 | 23 | /* detailed information about GPIO-Registers 24 | * can be found on datasheet bcm2835 on page 89-105 25 | */ 26 | 27 | /* set pin's mode: manipulating GPFSEL-Register 28 | * address of register = base + 4 * ( pinNum / 10 ) 29 | * each 3 bits stand for a pin 30 | * mode position of pin = (pinNum % 10) * 3 31 | * 000 = GPIO Pin is an input 32 | * 001 = GPIO Pin is an output 33 | * 100 = GPIO Pin takes alternate function 0 34 | * 101 = GPIO Pin takes alternate function 1 35 | * 110 = GPIO Pin takes alternate function 2 36 | * 111 = GPIO Pin takes alternate function 3 37 | * 011 = GPIO Pin takes alternate function 4 38 | * 010 = GPIO Pin takes alternate function 5 */ 39 | 40 | /* set pin as input */ 41 | .global set_pin_input 42 | .func set_pin_input 43 | set_pin_input: 44 | ldr r1, [r0, #(4 * (btnPin / 10))] 45 | and r1, #~(7 << ((btnPin % 10) * 3)) /* set pin-bits to 000 */ 46 | str r1, [r0, #(4 * (btnPin / 10))] 47 | bx lr 48 | .endfunc 49 | 50 | /* set pin as output */ 51 | .global set_pin_output 52 | .func set_pin_output 53 | set_pin_output: 54 | ldr r1, [r0, #(4 * (yellowPin / 10))] 55 | and r1, #~(7 << ((yellowPin % 10) * 3)) /* set pin-bits to 000 */ 56 | orr r1, #(1 << ((yellowPin % 10) * 3)) /* set pin-bits to 001 */ 57 | str r1, [r0, #(4 * (yellowPin / 10))] 58 | ldr r1, [r0, #(4 * (greenPin / 10))] 59 | and r1, #~(7 << ((greenPin % 10) * 3)) 60 | orr r1, #(1 << ((greenPin % 10) * 3)) 61 | str r1, [r0, #(4 * (greenPin / 10))] 62 | ldr r1, [r0, #(4 * (redPin / 10))] 63 | and r1, #~(7 << ((redPin % 10) * 3)) 64 | orr r1, #(1 << ((redPin % 10) * 3)) 65 | str r1, [r0, #(4 * (redPin / 10))] 66 | bx lr 67 | .endfunc 68 | 69 | /* get pin(btnPin) volt-level: reading GPLEV-Register 70 | * address of register = base + #(51 + 4 * (pinNum / 32)) 71 | * each bit stand for volt-level of the pin(0:low, 1:high(3.3V)) 72 | */ 73 | .global get_pin_level 74 | .func get_pin_level 75 | get_pin_level: 76 | ldr r1, [r0, #(52 + 4 * (btnPin / 32))] 77 | lsr r1, #btnPin 78 | and r1, #1 79 | mov r0, r1 /* r0 as return value */ 80 | bx lr 81 | .endfunc 82 | 83 | /* control led 84 | * set pin's output: manipulating GPSET-Register 85 | * GPSET0: pin(00-32) ; GPSET1: pin (32-53) 86 | * address of register = 4 * (7 + pinNum / 32) 87 | * each bit stand for one pin(0:low, 1:high(3.3V)) 88 | */ 89 | 90 | /* idle_status: all leds off */ 91 | .global set_idle 92 | .func set_idle 93 | set_idle: 94 | mov r1, #(1 << (yellowPin % 32)) 95 | str r1, [r0, #(4 * (10 + (yellowPin / 32)))] 96 | mov r1, #(1 << (greenPin % 32)) 97 | str r1, [r0, #(4 * (10 + (greenPin / 32)))] 98 | mov r1, #(1 << (redPin % 32)) 99 | str r1, [r0, #(4 * (10 + (redPin / 32)))] 100 | bx lr 101 | .endfunc 102 | 103 | /* on_status: all leds on */ 104 | .global set_all_on 105 | .func set_all_on 106 | set_all_on: 107 | mov r1, #(1 << (yellowPin % 32)) 108 | str r1, [r0, #(4 * (7 + yellowPin / 32))] 109 | mov r1, #(1 << (greenPin % 32)) 110 | str r1, [r0, #(4 * (7 + greenPin / 32))] 111 | mov r1, #(1 << (redPin % 32)) 112 | str r1, [r0, #(4 * (7 + redPin / 32))] 113 | bx lr 114 | .endfunc 115 | 116 | /* functions to set the single led on */ 117 | .global set_yellow_on 118 | .func set_yellow_on 119 | set_yellow_on: 120 | mov r1, #(1 << (yellowPin % 32)) 121 | str r1, [r0, #(4 * (7 + yellowPin / 32))] 122 | mov r1, #(1 << (greenPin % 32)) 123 | str r1, [r0, #(4 * (10 + greenPin / 32))] 124 | mov r1, #(1 << (redPin % 32)) 125 | str r1, [r0, #(4 * (10 + redPin / 32))] 126 | bx lr 127 | .endfunc 128 | 129 | .global set_green_on 130 | .func set_green_on 131 | set_green_on: 132 | mov r1, #(1 << (yellowPin % 32)) 133 | str r1, [r0, #(4 * (10 + yellowPin / 32))] 134 | mov r1, #(1 << (greenPin % 32)) 135 | str r1, [r0, #(4 * (7 + greenPin / 32))] 136 | mov r1, #(1 << (redPin % 32)) 137 | str r1, [r0, #(4 * (10 + redPin / 32))] 138 | bx lr 139 | .endfunc 140 | 141 | .global set_red_on 142 | .func set_red_on 143 | set_red_on: 144 | mov r1, #(1 << (yellowPin % 32)) 145 | str r1, [r0, #(4 * (10 + yellowPin / 32))] 146 | mov r1, #(1 << (greenPin % 32)) 147 | str r1, [r0, #(4 * (10 + greenPin / 32))] 148 | mov r1, #(1 << (redPin % 32)) 149 | str r1, [r0, #(4 * (7 + redPin / 32))] 150 | bx lr 151 | .endfunc 152 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/pwm_led/pwm.c: -------------------------------------------------------------------------------- 1 | // PWM example, based on code from http://elinux.org/RPi_Low-level_peripherals for the mmap part 2 | // and http://www.raspberrypi.org/phpBB3/viewtopic.php?t=8467&p=124620 for PWM initialization 3 | // 4 | // compile with "gcc pwm.c -o pwm", test with "./pwm" (needs to be root for /dev/mem access) 5 | // 6 | // Frank Buss, 2012 7 | 8 | #define BCM2708_PERI_BASE 0x20000000 9 | #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */ 10 | #define PWM_BASE (BCM2708_PERI_BASE + 0x20C000) /* PWM controller */ 11 | #define CLOCK_BASE (BCM2708_PERI_BASE + 0x101000) 12 | 13 | #define PWM_CTL 0 14 | #define PWM_RNG1 4 15 | #define PWM_DAT1 5 16 | 17 | #define PWMCLK_CNTL 40 18 | #define PWMCLK_DIV 41 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | 32 | #define PAGE_SIZE (4*1024) 33 | #define BLOCK_SIZE (4*1024) 34 | 35 | // I/O access 36 | volatile unsigned *gpio; 37 | volatile unsigned *pwm; 38 | volatile unsigned *clk; 39 | 40 | // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y) 41 | #define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3)) 42 | #define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3)) 43 | #define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3)) 44 | 45 | #define GPIO_SET *(gpio+7) // sets bits which are 1 ignores bits which are 0 46 | #define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0 47 | 48 | // map 4k register memory for direct access from user space and return a user space pointer to it 49 | volatile unsigned *mapRegisterMemory(int base) 50 | { 51 | static int mem_fd = 0; 52 | char *mem, *map; 53 | 54 | /* open /dev/mem */ 55 | if (!mem_fd) { 56 | if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { 57 | printf("can't open /dev/mem \n"); 58 | exit (-1); 59 | } 60 | } 61 | 62 | /* mmap register */ 63 | 64 | // Allocate MAP block 65 | if ((mem = malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) { 66 | printf("allocation error \n"); 67 | exit (-1); 68 | } 69 | 70 | // Make sure pointer is on 4K boundary 71 | if ((unsigned long)mem % PAGE_SIZE) 72 | mem += PAGE_SIZE - ((unsigned long)mem % PAGE_SIZE); 73 | 74 | // Now map it 75 | map = (char *)mmap( 76 | (caddr_t)mem, 77 | BLOCK_SIZE, 78 | PROT_READ|PROT_WRITE, 79 | MAP_SHARED|MAP_FIXED, 80 | mem_fd, 81 | base 82 | ); 83 | 84 | if ((long)map < 0) { 85 | printf("mmap error %d\n", (int)map); 86 | exit (-1); 87 | } 88 | 89 | // Always use volatile pointer! 90 | return (volatile unsigned *)map; 91 | } 92 | 93 | // set up a memory regions to access GPIO, PWM and the clock manager 94 | void setupRegisterMemoryMappings() 95 | { 96 | gpio = mapRegisterMemory(GPIO_BASE); 97 | pwm = mapRegisterMemory(PWM_BASE); 98 | clk = mapRegisterMemory(CLOCK_BASE); 99 | } 100 | 101 | void setServo(int percent) 102 | { 103 | int bitCount; 104 | unsigned int bits = 0; 105 | 106 | // 32 bits = 2 milliseconds 107 | bitCount = 16 + 16 * percent / 100; 108 | if (bitCount > 32) bitCount = 32; 109 | if (bitCount < 1) bitCount = 1; 110 | bits = 0; 111 | while (bitCount) { 112 | bits <<= 1; 113 | bits |= 1; 114 | bitCount--; 115 | } 116 | *(pwm + PWM_DAT1) = bits; 117 | } 118 | 119 | // init hardware 120 | void initHardware() 121 | { 122 | // mmap register space 123 | setupRegisterMemoryMappings(); 124 | 125 | // set PWM alternate function for GPIO18 126 | SET_GPIO_ALT(18, 5); 127 | 128 | // stop clock and waiting for busy flag doesn't work, so kill clock 129 | *(clk + PWMCLK_CNTL) = 0x5A000000 | (1 << 5); 130 | usleep(10); 131 | 132 | // set frequency 133 | // DIVI is the integer part of the divisor 134 | // the fractional part (DIVF) drops clock cycles to get the output frequency, bad for servo motors 135 | // 320 bits for one cycle of 20 milliseconds = 62.5 us per bit = 16 kHz 136 | int idiv = (int) (19200000.0f / 16000.0f); 137 | if (idiv < 1 || idiv > 0x1000) { 138 | printf("idiv out of range: %x\n", idiv); 139 | exit(-1); 140 | } 141 | *(clk + PWMCLK_DIV) = 0x5A000000 | (idiv<<12); 142 | 143 | // source=osc and enable clock 144 | *(clk + PWMCLK_CNTL) = 0x5A000011; 145 | 146 | // disable PWM 147 | *(pwm + PWM_CTL) = 0; 148 | 149 | // needs some time until the PWM module gets disabled, without the delay the PWM module crashs 150 | usleep(10); 151 | 152 | // filled with 0 for 20 milliseconds = 320 bits 153 | *(pwm + PWM_RNG1) = 320; 154 | 155 | // 32 bits = 2 milliseconds, init with 1 millisecond 156 | setServo(0); 157 | 158 | // start PWM1 in serializer mode 159 | *(pwm + PWM_CTL) = 3; 160 | } 161 | 162 | int main(int argc, char **argv) 163 | { 164 | // init PWM module for GPIO pin 18 with 50 Hz frequency 165 | initHardware(); 166 | 167 | // servo test, position in percent: 0 % = 1 ms, 100 % = 2 ms 168 | while (1) { 169 | setServo(0); 170 | sleep(1); 171 | setServo(25); 172 | sleep(1); 173 | setServo(50); 174 | sleep(1); 175 | setServo(75); 176 | sleep(1); 177 | setServo(100); 178 | sleep(1); 179 | } 180 | return 0; 181 | } 182 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/pwm_test/pwm.c: -------------------------------------------------------------------------------- 1 | // PWM example, based on code from http://elinux.org/RPi_Low-level_peripherals for the mmap part 2 | // and http://www.raspberrypi.org/phpBB3/viewtopic.php?t=8467&p=124620 for PWM initialization 3 | // 4 | // compile with "gcc pwm.c -o pwm", test with "./pwm" (needs to be root for /dev/mem access) 5 | // 6 | // Frank Buss, 2012 7 | 8 | // Rpi1 #define BCM2708_PERI_BASE 0x20000000 9 | #define BCM2708_PERI_BASE 0x3f000000 10 | #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */ 11 | #define PWM_BASE (BCM2708_PERI_BASE + 0x20C000) /* PWM controller */ 12 | #define CLOCK_BASE (BCM2708_PERI_BASE + 0x101000) /* CLOCK controller */ 13 | 14 | #define PWM_CTL 0 15 | #define PWM_RNG1 4 16 | #define PWM_DAT1 5 17 | 18 | #define PWMCLK_CNTL 40 19 | #define PWMCLK_DIV 41 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | #define PAGE_SIZE (4*1024) 34 | #define BLOCK_SIZE (4*1024) 35 | 36 | // I/O access 37 | volatile unsigned *gpio; 38 | volatile unsigned *pwm; 39 | volatile unsigned *clk; 40 | 41 | // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y) 42 | #define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3)) 43 | #define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3)) 44 | #define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3)) 45 | 46 | #define GPIO_SET *(gpio+7) // sets bits which are 1 ignores bits which are 0 47 | #define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0 48 | 49 | // map 4k register memory for direct access from user space and return a user space pointer to it 50 | volatile unsigned *mapRegisterMemory(int base) 51 | { 52 | static int mem_fd = 0; 53 | char *mem, *map; 54 | 55 | /* open /dev/mem */ 56 | if (!mem_fd) { 57 | if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { 58 | printf("can't open /dev/mem \n"); 59 | exit (-1); 60 | } 61 | } 62 | 63 | /* mmap register */ 64 | 65 | // Allocate MAP block 66 | if ((mem = malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) { 67 | printf("allocation error \n"); 68 | exit (-1); 69 | } 70 | 71 | // Make sure pointer is on 4K boundary 72 | if ((unsigned long)mem % PAGE_SIZE) 73 | mem += PAGE_SIZE - ((unsigned long)mem % PAGE_SIZE); 74 | 75 | // Now map it 76 | map = (char *)mmap( 77 | (caddr_t)mem, 78 | BLOCK_SIZE, 79 | PROT_READ|PROT_WRITE, 80 | MAP_SHARED|MAP_FIXED, 81 | mem_fd, 82 | base 83 | ); 84 | 85 | if ((long)map < 0) { 86 | printf("mmap error %d\n", (int)map); 87 | exit (-1); 88 | } 89 | 90 | // Always use volatile pointer! 91 | return (volatile unsigned *)map; 92 | } 93 | 94 | // set up a memory regions to access GPIO, PWM and the clock manager 95 | void setupRegisterMemoryMappings() 96 | { 97 | gpio = mapRegisterMemory(GPIO_BASE); 98 | pwm = mapRegisterMemory(PWM_BASE); 99 | clk = mapRegisterMemory(CLOCK_BASE); 100 | } 101 | 102 | void setServo(int percent) 103 | { 104 | int bitCount; 105 | unsigned int bits = 0; 106 | 107 | // 32 bits = 2 milliseconds 108 | bitCount = 16 + 16 * percent / 100; 109 | if (bitCount > 32) bitCount = 32; 110 | if (bitCount < 1) bitCount = 1; 111 | bits = 0; 112 | while (bitCount) { 113 | bits <<= 1; 114 | bits |= 1; 115 | bitCount--; 116 | } 117 | *(pwm + PWM_DAT1) = bits; 118 | } 119 | 120 | // init hardware 121 | void initHardware() 122 | { 123 | // mmap register space 124 | setupRegisterMemoryMappings(); 125 | 126 | // set PWM alternate function for GPIO18 127 | SET_GPIO_ALT(12, 0); 128 | 129 | // stop clock and waiting for busy flag doesn't work, so kill clock 130 | *(clk + PWMCLK_CNTL) = 0x5A000000 | (1 << 5); 131 | usleep(10); 132 | 133 | // set frequency 134 | // DIVI is the integer part of the divisor 135 | // the fractional part (DIVF) drops clock cycles to get the output frequency, bad for servo motors 136 | // 320 bits for one cycle of 20 milliseconds = 62.5 us per bit = 16 kHz 137 | int idiv = (int) (19200000.0f / 16000.0f); 138 | if (idiv < 1 || idiv > 0x1000) { 139 | printf("idiv out of range: %x\n", idiv); 140 | exit(-1); 141 | } 142 | *(clk + PWMCLK_DIV) = 0x5A000000 | (idiv<<12); 143 | 144 | // source=osc and enable clock 145 | *(clk + PWMCLK_CNTL) = 0x5A000011; 146 | 147 | // disable PWM 148 | *(pwm + PWM_CTL) = 0; 149 | 150 | // needs some time until the PWM module gets disabled, without the delay the PWM module crashs 151 | usleep(10); 152 | 153 | // filled with 0 for 20 milliseconds = 320 bits 154 | *(pwm + PWM_RNG1) = 320; 155 | 156 | // 32 bits = 2 milliseconds, init with 1 millisecond 157 | setServo(0); 158 | 159 | // start PWM0 in serializer mode 160 | *(pwm + PWM_CTL) = 3; 161 | } 162 | 163 | int main(int argc, char **argv) 164 | { 165 | // init PWM module for GPIO pin 18 with 50 Hz frequency 166 | initHardware(); 167 | 168 | // servo test, position in percent: 0 % = 1 ms, 100 % = 2 ms 169 | while (1) { 170 | setServo(20); 171 | sleep(1); 172 | setServo(30); 173 | sleep(1); 174 | setServo(40); 175 | sleep(1); 176 | setServo(50); 177 | sleep(1); 178 | } 179 | return 0; 180 | } 181 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/pwm_test/pwm_back.c: -------------------------------------------------------------------------------- 1 | // PWM example, based on code from http://elinux.org/RPi_Low-level_peripherals for the mmap part 2 | // and http://www.raspberrypi.org/phpBB3/viewtopic.php?t=8467&p=124620 for PWM initialization 3 | // 4 | // compile with "gcc pwm.c -o pwm", test with "./pwm" (needs to be root for /dev/mem access) 5 | // 6 | // Frank Buss, 2012 7 | 8 | // Rpi1 #define BCM2708_PERI_BASE 0x20000000 9 | #define BCM2708_PERI_BASE 0x3f000000 10 | #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */ 11 | #define PWM_BASE (BCM2708_PERI_BASE + 0x20C000) /* PWM controller */ 12 | #define CLOCK_BASE (BCM2708_PERI_BASE + 0x101000) /* CLOCK controller */ 13 | 14 | #define PWM_CTL 0 15 | #define PWM_RNG1 4 16 | #define PWM_DAT1 5 17 | 18 | #define PWMCLK_CNTL 40 19 | #define PWMCLK_DIV 41 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | #define PAGE_SIZE (4*1024) 34 | #define BLOCK_SIZE (4*1024) 35 | 36 | // I/O access 37 | volatile unsigned *gpio; 38 | volatile unsigned *pwm; 39 | volatile unsigned *clk; 40 | 41 | // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y) 42 | #define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3)) 43 | #define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3)) 44 | #define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3)) 45 | 46 | #define GPIO_SET *(gpio+7) // sets bits which are 1 ignores bits which are 0 47 | #define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0 48 | 49 | // map 4k register memory for direct access from user space and return a user space pointer to it 50 | volatile unsigned *mapRegisterMemory(int base) 51 | { 52 | static int mem_fd = 0; 53 | char *mem, *map; 54 | 55 | /* open /dev/mem */ 56 | if (!mem_fd) { 57 | if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { 58 | printf("can't open /dev/mem \n"); 59 | exit (-1); 60 | } 61 | } 62 | 63 | /* mmap register */ 64 | 65 | // Allocate MAP block 66 | if ((mem = malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) { 67 | printf("allocation error \n"); 68 | exit (-1); 69 | } 70 | 71 | // Make sure pointer is on 4K boundary 72 | if ((unsigned long)mem % PAGE_SIZE) 73 | mem += PAGE_SIZE - ((unsigned long)mem % PAGE_SIZE); 74 | 75 | // Now map it 76 | map = (char *)mmap( 77 | (caddr_t)mem, 78 | BLOCK_SIZE, 79 | PROT_READ|PROT_WRITE, 80 | MAP_SHARED|MAP_FIXED, 81 | mem_fd, 82 | base 83 | ); 84 | 85 | if ((long)map < 0) { 86 | printf("mmap error %d\n", (int)map); 87 | exit (-1); 88 | } 89 | 90 | // Always use volatile pointer! 91 | return (volatile unsigned *)map; 92 | } 93 | 94 | // set up a memory regions to access GPIO, PWM and the clock manager 95 | void setupRegisterMemoryMappings() 96 | { 97 | gpio = mapRegisterMemory(GPIO_BASE); 98 | pwm = mapRegisterMemory(PWM_BASE); 99 | clk = mapRegisterMemory(CLOCK_BASE); 100 | } 101 | 102 | void setServo(int percent) 103 | { 104 | int bitCount; 105 | unsigned int bits = 0; 106 | 107 | // 32 bits = 2 milliseconds 108 | bitCount = 16 + 16 * percent / 100; 109 | if (bitCount > 32) bitCount = 32; 110 | if (bitCount < 1) bitCount = 1; 111 | bits = 0; 112 | while (bitCount) { 113 | bits <<= 1; 114 | bits |= 1; 115 | bitCount--; 116 | } 117 | *(pwm + PWM_DAT1) = bits; 118 | } 119 | 120 | // init hardware 121 | void initHardware() 122 | { 123 | // mmap register space 124 | setupRegisterMemoryMappings(); 125 | 126 | // set PWM alternate function for GPIO18 127 | SET_GPIO_ALT(12, 0); 128 | 129 | // stop clock and waiting for busy flag doesn't work, so kill clock 130 | *(clk + PWMCLK_CNTL) = 0x5A000000 | (1 << 5); 131 | usleep(10); 132 | 133 | // set frequency 134 | // DIVI is the integer part of the divisor 135 | // the fractional part (DIVF) drops clock cycles to get the output frequency, bad for servo motors 136 | // 320 bits for one cycle of 20 milliseconds = 62.5 us per bit = 16 kHz 137 | int idiv = (int) (19200000.0f / 16000.0f); 138 | if (idiv < 1 || idiv > 0x1000) { 139 | printf("idiv out of range: %x\n", idiv); 140 | exit(-1); 141 | } 142 | *(clk + PWMCLK_DIV) = 0x5A000000 | (idiv<<12); 143 | 144 | // source=osc and enable clock 145 | *(clk + PWMCLK_CNTL) = 0x5A000011; 146 | 147 | // disable PWM 148 | *(pwm + PWM_CTL) = 0; 149 | 150 | // needs some time until the PWM module gets disabled, without the delay the PWM module crashs 151 | usleep(10); 152 | 153 | // filled with 0 for 20 milliseconds = 320 bits 154 | *(pwm + PWM_RNG1) = 320; 155 | 156 | // 32 bits = 2 milliseconds, init with 1 millisecond 157 | setServo(0); 158 | 159 | // start PWM0 in serializer mode 160 | *(pwm + PWM_CTL) = 3; 161 | } 162 | 163 | int main(int argc, char **argv) 164 | { 165 | // init PWM module for GPIO pin 18 with 50 Hz frequency 166 | initHardware(); 167 | 168 | // servo test, position in percent: 0 % = 1 ms, 100 % = 2 ms 169 | while (1) { 170 | setServo(20); 171 | sleep(1); 172 | setServo(30); 173 | sleep(1); 174 | setServo(40); 175 | sleep(1); 176 | setServo(50); 177 | sleep(1); 178 | } 179 | return 0; 180 | } 181 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/pwm_test/main.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : Temperature measurement and processing on Rpi 3 | * Date : 2015-12-03 23:52:37 4 | * Author : Xiang,Zuo 5 | * Email : xianglinks@gmail.com 6 | ******************************************************************************/ 7 | 8 | /* ---------------- Code Section ---------------- */ 9 | .text 10 | .balign 4 11 | 12 | /* determined by Application Binary Interface(ABI) 13 | * the calling of functions(both asm and c functions) in asm 14 | * use registers r0-r3 as first four arguments 15 | * and the frist four return-values use also r0-r3 16 | * more than four parameters should realized with stack */ 17 | 18 | /* import c fuctions for addr_mapping */ 19 | .extern usleep 20 | .extern sleep 21 | 22 | /* import asm functions */ 23 | .extern get_pin_level 24 | .extern set_pin_pwm 25 | 26 | 27 | /* main function */ 28 | .global main 29 | .func main 30 | main: 31 | push {r4-r11, lr} 32 | 33 | /* GPIO_BASE in r9 */ 34 | bl returnGPIOBaseAddr 35 | mov r9, r0 36 | bl set_pin_pwm 37 | 38 | /* 39 | * PWM_BASE = (BCM2835_PERI_BASE + 0x20C000) -> PWM controller 40 | * Bit Streams configured individually to output either PWM or serialised version of 32 bits 41 | * both modes clocked by clk_pwm, which is normally 100MHz, can be varied by the clock manager 42 | * PWN controller consist of two independent channels -> channel0 & channel1 43 | * two sub-modes in PWM controller: MSEN = 0(default) and MSEN = 1 44 | * hardware support PWM: BCM-pin 12: PWM0, alternate function 0(alt0) 45 | */ 46 | 47 | /* CLOCK_BASE = (BCM2835_PERI_BASE + 0x101000) -> CLOCK controller 48 | * base = 0x7e000000 clock 0x7e101000 49 | * genau infomations of address of register on BCM2835: http://elinux.org/BCM2835_registers 50 | * Page 105 -> GPIO general clock 51 | */ 52 | 53 | set_pwm_clock: 54 | /* get CLOCK_BASE in r8 */ 55 | bl returnCLKBaseAddr 56 | mov r8, r0 57 | add r3, r8, #160 /* addr CM_PWMCLK_CNTL */ 58 | /* *(clk + PWMCLK_CNTL) = 0x5A000000 | (1 << 5) */ 59 | ldr r2, =1509949472 60 | str r2, [r3, #0] 61 | mov r0, #10 62 | bl usleep 63 | 64 | set_frequency: 65 | /* set frequency 66 | DIVI is the integer part of the divisor 67 | the fractional part (DIVF) drops clock cycles to get the output frequency, bad for leds 68 | 320 bits for one cycle of 20 milliseconds = 62.5 us per bit = 16 kHz 69 | * the frequency of the source = 19200000Hz 70 | * the frequency we want = 16kHz 71 | * the idiv = 19200000Hz / 16000 = 1200 72 | */ 73 | 74 | /* *(clk + PWMCLK_DIV) = 0x5A000000 | (idiv<<12) */ 75 | add r3, r8, #164 /* addr PMCLK_DIV */ 76 | mov r2, #1200 77 | mov r2, r2, asl #12 /* idiv << 12 */ 78 | orr r2, r2, #1509949440 79 | str r2, [r3, #0] 80 | 81 | enable_clock: 82 | add r3, r8, #160 /* addr CM_PWMCLK_CNTL */ 83 | /* *(clk + PWMCLK_CNTL) = 0x5A000011;*/ 84 | ldr r2, =0x5A000011 85 | str r2, [r3, #0] 86 | 87 | disable_pwm: 88 | /* put PWM_BASE in r7 */ 89 | bl returnPWMBaseAddr 90 | mov r7, r0 91 | mov r2, #0 92 | str r2, [r7, #0] 93 | /* needs some time until the PWM module gets disabled, without the delay the PWM module crashs */ 94 | mov r0, #10 95 | bl usleep 96 | 97 | set_pwm_registers: 98 | /* RNG1 register */ 99 | add r3, r7, #16 100 | mov r2, #320 101 | str r2, [r3, #0] 102 | 103 | /* init Duty Cycle with 0 */ 104 | mov r0, #0 105 | bl setServo 106 | 107 | start_pwm_serialized_mode: 108 | mov r2, #3 109 | str r2, [r7, #0] 110 | 111 | /* main loop */ 112 | main_loop: 113 | mov r0, #10 114 | bl setServo 115 | mov r0, #1 116 | bl sleep 117 | 118 | mov r0, #20 119 | bl setServo 120 | mov r0, #1 121 | bl sleep 122 | 123 | b main_loop 124 | 125 | end_main: 126 | mov r0, #0 127 | pop {r4-r11, pc} 128 | .endfunc 129 | 130 | 131 | .global setDutyCycle 132 | .func setDutyCycle 133 | setDutyCycle: 134 | /* arg1: int percent(r0), arg2: PWM_BASE(r1) 135 | * 32 bit = 2 milliseconds 136 | * set PWM_DAT1 Register: Offset = 5 * 4 = 20 137 | */ 138 | push {lr} 139 | mov r7, r1 140 | str fp, [sp, #-4]! 141 | add fp, sp, #0 142 | sub sp, sp, #20 143 | str r0, [fp, #-16] 144 | mov r3, #0 145 | str r3, [fp, #-12] 146 | ldr r3, [fp, #-16] 147 | mov r3, r3, asl #4 148 | ldr r2, .L16 149 | smull r1, r2, r2, r3 150 | mov r2, r2, asr #5 151 | mov r3, r3, asr #31 152 | rsb r3, r3, r2 153 | add r3, r3, #16 154 | str r3, [fp, #-8] 155 | ldr r3, [fp, #-8] 156 | cmp r3, #32 157 | ble .L12 158 | mov r3, #32 159 | str r3, [fp, #-8] 160 | .L12: 161 | ldr r3, [fp, #-8] 162 | cmp r3, #0 163 | bgt .L13 164 | mov r3, #1 165 | str r3, [fp, #-8] 166 | .L13: 167 | mov r3, #0 168 | str r3, [fp, #-12] 169 | b .L14 170 | .L15: 171 | ldr r3, [fp, #-12] 172 | mov r3, r3, asl #1 173 | str r3, [fp, #-12] 174 | ldr r3, [fp, #-12] 175 | orr r3, r3, #1 176 | str r3, [fp, #-12] 177 | ldr r3, [fp, #-8] 178 | sub r3, r3, #1 179 | str r3, [fp, #-8] 180 | .L14: 181 | ldr r3, [fp, #-8] 182 | cmp r3, #0 183 | bne .L15 184 | add r3, r7, #20 185 | ldr r2, [fp, #-12] 186 | str r2, [r3, #0] 187 | add sp, fp, #0 188 | ldmfd sp!, {fp} 189 | .L17: 190 | .align 2 191 | .L16: 192 | .word 1374389535 193 | .align 2 194 | pop {pc} 195 | .endfunc 196 | 197 | /* ---------------- Data Section ---------------- */ 198 | .data 199 | .balign 4 200 | 201 | /* definations for strings */ 202 | errMsg1: .ascii "addr_mapping do not work. aborting...\n" 203 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/pwm_test/main_back.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : Temperature measurement and processing on Rpi 3 | * Date : 2015-12-03 23:52:37 4 | * Author : Xiang,Zuo 5 | * Email : xianglinks@gmail.com 6 | ******************************************************************************/ 7 | 8 | /* ---------------- Code Section ---------------- */ 9 | .text 10 | .balign 4 11 | 12 | /* determined by Application Binary Interface(ABI) 13 | * the calling of functions(both asm and c functions) in asm 14 | * use registers r0-r3 as first four arguments 15 | * and the frist four return-values use also r0-r3 16 | * more than four parameters should realized with stack */ 17 | 18 | /* import c fuctions for addr_mapping */ 19 | .extern usleep 20 | .extern sleep 21 | 22 | /* import asm functions */ 23 | .extern get_pin_level 24 | .extern set_pin_pwm 25 | 26 | 27 | /* main function */ 28 | .global main 29 | .func main 30 | main: 31 | push {r4-r11, lr} 32 | 33 | /* GPIO_BASE in r9 */ 34 | bl returnGPIOBaseAddr 35 | mov r9, r0 36 | bl set_pin_pwm 37 | 38 | /* 39 | * PWM_BASE = (BCM2835_PERI_BASE + 0x20C000) -> PWM controller 40 | * Bit Streams configured individually to output either PWM or serialised version of 32 bits 41 | * both modes clocked by clk_pwm, which is normally 100MHz, can be varied by the clock manager 42 | * PWN controller consist of two independent channels -> channel0 & channel1 43 | * two sub-modes in PWM controller: MSEN = 0(default) and MSEN = 1 44 | * hardware support PWM: BCM-pin 12: PWM0, alternate function 0(alt0) 45 | */ 46 | 47 | /* CLOCK_BASE = (BCM2835_PERI_BASE + 0x101000) -> CLOCK controller 48 | * base = 0x7e000000 clock 0x7e101000 49 | * genau infomations of address of register on BCM2835: http://elinux.org/BCM2835_registers 50 | * Page 105 -> GPIO general clock 51 | */ 52 | 53 | set_pwm_clock: 54 | /* get CLOCK_BASE in r8 */ 55 | bl returnCLKBaseAddr 56 | mov r8, r0 57 | add r3, r8, #160 /* addr CM_PWMCLK_CNTL */ 58 | /* *(clk + PWMCLK_CNTL) = 0x5A000000 | (1 << 5) */ 59 | ldr r2, =1509949472 60 | str r2, [r3, #0] 61 | mov r0, #10 62 | bl usleep 63 | 64 | set_frequency: 65 | /* set frequency 66 | DIVI is the integer part of the divisor 67 | the fractional part (DIVF) drops clock cycles to get the output frequency, bad for leds 68 | 320 bits for one cycle of 20 milliseconds = 62.5 us per bit = 16 kHz 69 | * the frequency of the source = 19200000Hz 70 | * the frequency we want = 16kHz 71 | * the idiv = 19200000Hz / 16000 = 1200 72 | */ 73 | 74 | /* *(clk + PWMCLK_DIV) = 0x5A000000 | (idiv<<12) */ 75 | add r3, r8, #164 /* addr PMCLK_DIV */ 76 | mov r2, #1200 77 | mov r2, r2, asl #12 /* idiv << 12 */ 78 | orr r2, r2, #1509949440 79 | str r2, [r3, #0] 80 | 81 | enable_clock: 82 | add r3, r8, #160 /* addr CM_PWMCLK_CNTL */ 83 | /* *(clk + PWMCLK_CNTL) = 0x5A000011;*/ 84 | ldr r2, =0x5A000011 85 | str r2, [r3, #0] 86 | 87 | disable_pwm: 88 | /* put PWM_BASE in r7 */ 89 | bl returnPWMBaseAddr 90 | mov r7, r0 91 | mov r2, #0 92 | str r2, [r7, #0] 93 | /* needs some time until the PWM module gets disabled, without the delay the PWM module crashs */ 94 | mov r0, #10 95 | bl usleep 96 | 97 | set_pwm_registers: 98 | /* RNG1 register */ 99 | add r3, r7, #16 100 | mov r2, #320 101 | str r2, [r3, #0] 102 | 103 | /* init Duty Cycle with 0 */ 104 | mov r0, #0 105 | bl setServo 106 | 107 | start_pwm_serialized_mode: 108 | mov r2, #3 109 | str r2, [r7, #0] 110 | 111 | /* main loop */ 112 | main_loop: 113 | mov r0, #10 114 | bl setServo 115 | mov r0, #1 116 | bl sleep 117 | 118 | mov r0, #20 119 | bl setServo 120 | mov r0, #1 121 | bl sleep 122 | 123 | b main_loop 124 | 125 | end_main: 126 | mov r0, #0 127 | pop {r4-r11, pc} 128 | .endfunc 129 | 130 | 131 | .global setDutyCycle 132 | .func setDutyCycle 133 | setDutyCycle: 134 | /* arg1: int percent(r0), arg2: PWM_BASE(r1) 135 | * 32 bit = 2 milliseconds 136 | * set PWM_DAT1 Register: Offset = 5 * 4 = 20 137 | */ 138 | push {lr} 139 | mov r7, r1 140 | str fp, [sp, #-4]! 141 | add fp, sp, #0 142 | sub sp, sp, #20 143 | str r0, [fp, #-16] 144 | mov r3, #0 145 | str r3, [fp, #-12] 146 | ldr r3, [fp, #-16] 147 | mov r3, r3, asl #4 148 | ldr r2, .L16 149 | smull r1, r2, r2, r3 150 | mov r2, r2, asr #5 151 | mov r3, r3, asr #31 152 | rsb r3, r3, r2 153 | add r3, r3, #16 154 | str r3, [fp, #-8] 155 | ldr r3, [fp, #-8] 156 | cmp r3, #32 157 | ble .L12 158 | mov r3, #32 159 | str r3, [fp, #-8] 160 | .L12: 161 | ldr r3, [fp, #-8] 162 | cmp r3, #0 163 | bgt .L13 164 | mov r3, #1 165 | str r3, [fp, #-8] 166 | .L13: 167 | mov r3, #0 168 | str r3, [fp, #-12] 169 | b .L14 170 | .L15: 171 | ldr r3, [fp, #-12] 172 | mov r3, r3, asl #1 173 | str r3, [fp, #-12] 174 | ldr r3, [fp, #-12] 175 | orr r3, r3, #1 176 | str r3, [fp, #-12] 177 | ldr r3, [fp, #-8] 178 | sub r3, r3, #1 179 | str r3, [fp, #-8] 180 | .L14: 181 | ldr r3, [fp, #-8] 182 | cmp r3, #0 183 | bne .L15 184 | add r3, r7, #20 185 | ldr r2, [fp, #-12] 186 | str r2, [r3, #0] 187 | add sp, fp, #0 188 | ldmfd sp!, {fp} 189 | .L17: 190 | .align 2 191 | .L16: 192 | .word 1374389535 193 | .align 2 194 | pop {pc} 195 | .endfunc 196 | 197 | /* ---------------- Data Section ---------------- */ 198 | .data 199 | .balign 4 200 | 201 | /* definations for strings */ 202 | errMsg1: .ascii "addr_mapping do not work. aborting...\n" 203 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/calc_led/gpio_functions.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : Functions for interaction with GPIO-pins on Rpi 3 | * Date : 2016-01-08 10:51:17 4 | * Author : Xiang,Zuo 5 | * Email : xianglinks@gmail.com 6 | ******************************************************************************/ 7 | 8 | /* -- Code Section -- */ 9 | .text 10 | .balign 4 11 | 12 | /* definations for BCM-pins 13 | * this number is the same as that on LK-RB-Shield 14 | * the mapping between BCM-pins, WiringPi-pins and Physical-pins 15 | * can be found on webseite: http://www.pinout.xyz/ 16 | * note: The usage of BCM-pin 19-21 can cause some unknown problem by Raspbian 17 | */ 18 | 19 | .equ yellowLed, 12 20 | .equ greenLed, 13 21 | .equ redLed, 14 22 | .equ btnPin1, 5 23 | .equ btnPin2, 15 24 | 25 | /* detailed information about GPIO-Registers 26 | * can be found on datasheet bcm2835 on page 89-105 27 | */ 28 | 29 | /* set pin's mode: manipulating GPFSEL-Register 30 | * address of register = base + 4 * ( pinNum / 10 ) 31 | * each 3 bits stand for a pin 32 | * mode position of pin = (pinNum % 10) * 3 33 | * 000 = GPIO Pin is an input 34 | * 001 = GPIO Pin is an output 35 | * 100 = GPIO Pin takes alternate function 0 36 | * 101 = GPIO Pin takes alternate function 1 37 | * 110 = GPIO Pin takes alternate function 2 38 | * 111 = GPIO Pin takes alternate function 3 39 | * 011 = GPIO Pin takes alternate function 4 40 | * 010 = GPIO Pin takes alternate function 5 */ 41 | 42 | /* set pins as input */ 43 | .global set_pin_input 44 | .func set_pin_input 45 | set_pin_input: 46 | ldr r1, [r0, #(4 * (btnPin1 / 10))] 47 | and r1, #~(7 << ((btnPin1 % 10) * 3)) /* set pin-bits to 000 */ 48 | str r1, [r0, #(4 * (btnPin1 / 10))] 49 | 50 | ldr r1, [r0, #(4 * (btnPin2 / 10))] 51 | and r1, #~(7 << ((btnPin2 % 10) * 3)) /* set pin-bits to 000 */ 52 | str r1, [r0, #(4 * (btnPin2 / 10))] 53 | 54 | bx lr 55 | .endfunc 56 | 57 | /* set pin(ledPin) as output */ 58 | .global set_pin_output 59 | .func set_pin_output 60 | set_pin_output: 61 | and r1, #~(7 << ((yellowLed % 10) * 3)) /* set pin-bits to 000 */ 62 | orr r1, #(1 << ((yellowLed % 10) * 3)) /* set pin-bits to 001 */ 63 | str r1, [r0, #(4 * (yellowLed / 10))] 64 | 65 | and r1, #~(7 << ((greenLed % 10) * 3)) /* set pin-bits to 000 */ 66 | orr r1, #(1 << ((greenLed % 10) * 3)) /* set pin-bits to 001 */ 67 | str r1, [r0, #(4 * (greenLed / 10))] 68 | 69 | and r1, #~(7 << ((redLed % 10) * 3)) /* set pin-bits to 000 */ 70 | orr r1, #(1 << ((redLed % 10) * 3)) /* set pin-bits to 001 */ 71 | str r1, [r0, #(4 * (redLed / 10))] 72 | 73 | bx lr 74 | .endfunc 75 | 76 | /* get pin volt-level: reading GPLEV-Register 77 | * address of register = base + #(51 + 4 * (pinNum / 32)) 78 | * each bit stand for volt-level of the pin(0:low, 1:high(3.3V)) 79 | * args : r0 GPIO_BASE 80 | r1 pinNum 81 | * return: r0 pin volt-level 82 | */ 83 | .global get_pin_level 84 | .func get_pin_level 85 | get_pin_level: 86 | mov r4, r1 /* backup the pinNum in r4 */ 87 | /* get the interval of pinNum */ 88 | cmp r4, #32 89 | bgt upper_get_pin_level 90 | /* if pinNum < 32 -> pinNum / 32 = 0 */ 91 | mov r2, #0 92 | mov r2, r2, LSL #4 93 | add r2, r2, #52 94 | ldr r1, [r0, r2] 95 | lsr r1, r4 96 | and r1, #1 97 | mov r0, r1 98 | b end_get_pin_level 99 | 100 | upper_get_pin_level: 101 | /* if pinNum > 32 -> pinNum / 32 = 1 */ 102 | mov r2, #1 103 | mov r2, r2, LSL #4 104 | add r2, r2, #52 105 | ldr r1, [r0, r2] 106 | lsr r1, r4 107 | and r1, #1 108 | mov r0, r1 109 | 110 | end_get_pin_level: 111 | bx lr 112 | .endfunc 113 | 114 | /* control led 115 | * set pin's output: manipulating GPSET-Register 116 | * GPSET0: pin(00-32) ; GPSET1: pin (32-53) 117 | * address of register = 4 * (7 + pinNum / 32) 118 | * each bit stand for one pin(0:low, 1:high(3.3V)) 119 | */ 120 | 121 | /* blink led */ 122 | .global blink_yellow_led 123 | .func blink_yellow_led 124 | blink_yellow_led: 125 | mov r5, lr 126 | mov r4, r0 127 | mov r1, #(1 << (yellowLed % 32)) 128 | str r1, [r4, #(4 * (7 + yellowLed / 32))] 129 | mov r0, #50 130 | bl wait 131 | mov r1, #(1 << (yellowLed % 32)) 132 | str r1, [r4, #(4 * (10 + (yellowLed / 32)))] 133 | mov pc, r5 134 | .endfunc 135 | 136 | .global blink_red_led 137 | .func blink_red_led 138 | blink_red_led: 139 | mov r5, lr 140 | mov r4, r0 141 | mov r1, #(1 << (redLed % 32)) 142 | str r1, [r4, #(4 * (7 + redLed / 32))] 143 | mov r0, #50 144 | bl wait 145 | mov r1, #(1 << (redLed % 32)) 146 | str r1, [r4, #(4 * (10 + (redLed / 32)))] 147 | mov pc, r5 148 | .endfunc 149 | 150 | .global blink_all_leds 151 | .func blink_all_leds 152 | blink_all_leds: 153 | mov r5, lr 154 | mov r4, r0 155 | mov r1, #(1 << (yellowLed % 32)) 156 | str r1, [r4, #(4 * (7 + yellowLed / 32))] 157 | mov r1, #(1 << (greenLed % 32)) 158 | str r1, [r4, #(4 * (7 + greenLed / 32))] 159 | mov r1, #(1 << (redLed % 32)) 160 | str r1, [r4, #(4 * (7 + redLed / 32))] 161 | mov r0, #200 162 | bl wait 163 | mov r1, #(1 << (yellowLed % 32)) 164 | str r1, [r4, #(4 * (10 + (yellowLed / 32)))] 165 | mov r1, #(1 << (greenLed % 32)) 166 | str r1, [r4, #(4 * (10 + (greenLed / 32)))] 167 | mov r1, #(1 << (redLed % 32)) 168 | str r1, [r4, #(4 * (10 + (redLed / 32)))] 169 | mov pc, r5 170 | .endfunc 171 | 172 | /* idle_status: all leds off */ 173 | .global set_idle 174 | .func set_idle 175 | set_idle: 176 | mov r1, #(1 << (yellowLed % 32)) 177 | str r1, [r0, #(4 * (10 + (yellowLed / 32)))] 178 | mov r1, #(1 << (greenLed % 32)) 179 | str r1, [r0, #(4 * (10 + (greenLed / 32)))] 180 | mov r1, #(1 << (redLed % 32)) 181 | str r1, [r0, #(4 * (10 + (redLed / 32)))] 182 | bx lr 183 | .endfunc 184 | 185 | /* use led as binary code to show result 186 | * args: r0: GPIO_BASE 187 | r1: sum 188 | r2: lowest pinNum 189 | condition: pinNum < 32 190 | */ 191 | .global show_sum_led 192 | .func show_sum_led 193 | show_sum_led: 194 | mov r1, r1, LSL r2 195 | str r1, [r0, #28] 196 | bx lr 197 | .endfunc 198 | 199 | /* blink led to show result 200 | * args: r0: GPIO_BASE 201 | r1: sum 202 | */ 203 | .global show_sum_blink 204 | .func show_sum_blink 205 | show_sum_blink: 206 | push {lr} 207 | mov r10, r1 208 | 209 | blink_loop: 210 | cmp r10, #0 211 | beq end_blink_loop 212 | mov r0, r9 213 | bl blink_yellow_led 214 | mov r0, #100 215 | bl wait 216 | sub r10, r10, #1 217 | b blink_loop 218 | 219 | end_blink_loop: 220 | pop {pc} 221 | .endfunc 222 | 223 | 224 | /* function to delay */ 225 | .global wait 226 | .func wait 227 | /* delay for some time (0.01 * r0) second */ 228 | wait: 229 | mov r3, r0 230 | wait_loop2: 231 | sub r3, #1 232 | mov r2, #0x3F0000 233 | wait_loop1: 234 | sub r2,#1 235 | cmp r2,#0 236 | bne wait_loop1 237 | cmp r3, #0 238 | bne wait_loop2 239 | /* end wait_loop2 */ 240 | bx lr 241 | .endfunc 242 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/led_rhythm/main.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : Different status of leds controlled by JT-Button 3 | * Date : 2015-12-14 15:48:04 4 | * Author : Xiang,Zuo 5 | * Email : xianglinks@gmail.com 6 | ******************************************************************************/ 7 | 8 | /* ---------------- Code Section ---------------- */ 9 | .text 10 | .balign 4 11 | 12 | /* determined by Application Binary Interface(ABI) 13 | * the calling of functions(both asm and c functions) in asm 14 | * use registers r0-r3 as first four arguments 15 | * and the frist four return-values use also r0-r3 16 | * more than four parameters should realized with stack */ 17 | 18 | .equ modeNum, 5 19 | 20 | /* import c fuctions */ 21 | .extern map_peripheral /* mapping a block (4096 bytes) of physical addresses to virtual space */ 22 | 23 | /* import asm functions */ 24 | .extern set_pin_input 25 | .extern set_pin_output 26 | .extern get_pin_level 27 | .extern set_idle 28 | .extern set_all_on 29 | .extern set_yellow_on 30 | .extern set_green_on 31 | .extern set_red_on 32 | 33 | /* main function */ 34 | .global main 35 | .func main 36 | main: 37 | push {r4-r11, lr} 38 | 39 | /* memory mapping using c function */ 40 | mapping_addr: 41 | bl map_peripheral 42 | mov r9, r0 /* save the GPIO_BASE in r9 */ 43 | mov r1, #-1 44 | cmp r0, r1 45 | bne gpio_settings 46 | /* when problem with mapping */ 47 | ldr r0, =errMsg1 48 | bl printf 49 | b end_main 50 | 51 | /* gpio settings: set inputs and outputs */ 52 | gpio_settings: 53 | mov r0, r9 54 | bl set_pin_output 55 | 56 | mov r0, r9 57 | bl set_pin_input 58 | 59 | /* The bit for programm-status is saved in r10 60 | * 0 -> idle_status 61 | * 1 -> all_on_status 62 | * 2 -> all led flash 63 | * 3 -> mode3(yellow to red) 64 | * 4 -> mode4(yellow to red and backwards) 65 | * this status should be changed by button-input signal */ 66 | 67 | mov r10, #0 /* init with idle_status */ 68 | 69 | /* init with idle_status: all leds off */ 70 | mov r0, r9 71 | bl set_idle 72 | 73 | /* main loop */ 74 | main_loop: 75 | 76 | /* check the status-bit in r10 */ 77 | check_status: 78 | /* jump to different status(0 to (modeNum - 1)) */ 79 | cmp r10, #0 80 | beq mode0 81 | 82 | cmp r10, #1 83 | beq mode1 84 | 85 | cmp r10, #2 86 | beq mode2 87 | 88 | cmp r10, #3 89 | beq mode3 90 | 91 | cmp r10, #4 92 | beq mode4 93 | 94 | /* mode0: (idle_status) all leds off */ 95 | mode0 : 96 | mov r0, r9 97 | bl set_idle 98 | /* check for button signal */ 99 | mov r0, r9 100 | bl get_pin_level 101 | cmp r0, #0 /* no button signal */ 102 | beq mode0 103 | bl change_mode 104 | b main_loop 105 | 106 | /* mode1: all led on */ 107 | mode1: 108 | mov r0, r9 109 | bl set_all_on 110 | /* check for button signal */ 111 | mov r0, r9 112 | bl get_pin_level 113 | cmp r0, #0 /* no button signal */ 114 | beq mode1 115 | bl change_mode 116 | b main_loop 117 | 118 | /* mode2: all led flash (0.2s) */ 119 | mode2: 120 | md2_step1: 121 | mov r0, r9 122 | bl set_all_on 123 | mov r0, #20 124 | bl wait 125 | /* check the button status */ 126 | mov r0, r9 127 | bl get_pin_level 128 | cmp r0, #0 129 | beq md2_step2 130 | bl change_mode 131 | b main_loop 132 | 133 | md2_step2: 134 | mov r0, r9 135 | bl set_idle 136 | mov r0, #20 137 | bl wait 138 | /* check the button status */ 139 | mov r0, r9 140 | bl get_pin_level 141 | cmp r0, #0 142 | beq md2_step1 143 | bl change_mode 144 | b main_loop 145 | 146 | /* mode3: from yellow to red and again (0.5s) */ 147 | mode3: 148 | md3_step1: 149 | mov r0, r9 150 | bl set_yellow_on 151 | mov r0, #50 152 | bl wait 153 | /* check the button status */ 154 | mov r0, r9 155 | bl get_pin_level 156 | cmp r0, #0 157 | beq md3_step2 158 | bl change_mode 159 | b main_loop 160 | 161 | md3_step2: 162 | mov r0, r9 163 | bl set_green_on 164 | mov r0, #50 165 | bl wait 166 | 167 | /* check the button status */ 168 | mov r0, r9 169 | bl get_pin_level 170 | cmp r0, #0 171 | beq md3_step3 172 | bl change_mode 173 | b main_loop 174 | 175 | md3_step3: 176 | mov r0, r9 177 | bl set_red_on 178 | mov r0, #50 179 | bl wait 180 | mov r0, r9 181 | bl get_pin_level 182 | cmp r0, #0 183 | beq md3_step1 184 | bl change_mode 185 | b main_loop 186 | 187 | /* mode4: from yellow to red and backwards (0.5s) */ 188 | mode4: 189 | md4_step1: 190 | mov r0, r9 191 | bl set_yellow_on 192 | mov r0, #50 193 | bl wait 194 | /* check the button status */ 195 | mov r0, r9 196 | bl get_pin_level 197 | cmp r0, #0 198 | beq md4_step2 199 | bl change_mode 200 | b main_loop 201 | 202 | md4_step2: 203 | mov r0, r9 204 | bl set_green_on 205 | mov r0, #50 206 | bl wait 207 | 208 | /* check the button status */ 209 | mov r0, r9 210 | bl get_pin_level 211 | cmp r0, #0 212 | beq md4_step3 213 | bl change_mode 214 | b main_loop 215 | 216 | md4_step3: 217 | mov r0, r9 218 | bl set_red_on 219 | mov r0, #50 220 | bl wait 221 | mov r0, r9 222 | bl get_pin_level 223 | cmp r0, #0 224 | beq md4_step4 225 | bl change_mode 226 | b main_loop 227 | 228 | md4_step4: 229 | mov r0, r9 230 | bl set_green_on 231 | mov r0, #50 232 | bl wait 233 | mov r0, r9 234 | bl get_pin_level 235 | cmp r0, #0 236 | beq md4_step1 237 | bl change_mode 238 | b main_loop 239 | 240 | end_main: 241 | mov r0, #0 242 | pop {r4-r11, pc} 243 | .endfunc /* end main function */ 244 | 245 | 246 | /* function to change mode */ 247 | /* the change of programm-status should be triggered 248 | * by falling edge of the button-input: when there is 10 */ 249 | .global change_mode 250 | .func change_mode 251 | change_mode: 252 | push {lr} 253 | check_falling_edge: 254 | mov r0, r9 255 | bl get_pin_level 256 | cmp r0, #1 257 | beq check_falling_edge 258 | /* if the falling edge is detected: 10 -> change status 259 | * change the status using XOR */ 260 | change_status: 261 | add r10, r10, #1 262 | cmp r10, #modeNum 263 | blt return 264 | /* if r10 >= modeNum(out of period), set status to null */ 265 | sub r10, r10, #modeNum 266 | return: 267 | mov r0, #20 268 | bl wait 269 | pop {pc} 270 | .endfunc 271 | 272 | /* function to delay */ 273 | .global wait 274 | .func wait 275 | /* delay for some time (0.01 * r0) second */ 276 | wait: 277 | mov r3, r0 278 | wait_loop2: 279 | sub r3, #1 280 | mov r2, #0x3F0000 281 | wait_loop1: 282 | sub r2,#1 283 | cmp r2,#0 284 | bne wait_loop1 285 | cmp r3, #0 286 | bne wait_loop2 287 | /* end main_loop */ 288 | bx lr 289 | .endfunc 290 | 291 | /* ---------------- Data Section ---------------- */ 292 | .data 293 | .balign 4 294 | 295 | /* definations for strings */ 296 | errMsg1: .ascii "memory mapping do not work. aborting...\n" 297 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/button_test/gpio_functions.S: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * About : Functions for interaction with GPIO-pins on Rpi 3 | * Date : 2015-12-14 12:26:40 4 | * Author : Xiang,Zuo 5 | * Email : xianglinks@gmail.com 6 | ******************************************************************************/ 7 | 8 | /* -- Code Section -- */ 9 | .text 10 | .balign 4 11 | 12 | .equ yellowPin, 19 13 | .equ greenPin, 20 14 | .equ redPin, 21 15 | .equ btnPin1, 14 16 | 17 | /* function to caculate 32-bit unsigned division 18 | * args : r0 contains numerator 19 | * r1 contains divisor 20 | * return: r2 contains the quotient */ 21 | .global unsigned_division 22 | .func unsigned_division 23 | unsigned_division: 24 | mov r3, r1 /* r3 <- r1 */ 25 | cmp r3, r0, LSR #1 /* update cpsr with r3 - r0 / 2 */ 26 | .Lloop2: 27 | movls r3, r3, LSL #1 /* if r3 <= 2 * r0 ( C=0 or Z=1 ) then r3 <- r3 * 2 */ 28 | cmp r3, r0, LSR #1 /* update cpsr with r3 - (r0 / 2) */ 29 | bls .Lloop2 /* branch to .Lloop2 if r3 <= 2 * r0 ( C=0 or Z=1 ) */ 30 | 31 | mov r2, #0 /* init quotient r2 <- 0 */ 32 | 33 | .Lloop3: 34 | cmp r0, r3 /* update cpsr with r0 - r3 */ 35 | subhs r0, r0, r3 /* if r0 >= r3 ( C=1 ) then r0 <- r0 - r3 */ 36 | adc r2, r2, r2 /* r2 <- r2 + r2 + C. 37 | Note that if r0 >= r3 then C=1, C=0 otherwise */ 38 | 39 | mov r3, r3, LSR #1 /* r3 <- r3 / 2 */ 40 | cmp r3, r1 /* update cpsr with r3 - r1 */ 41 | bhs .Lloop3 /* if r3 >= r1 branch to .Lloop3 */ 42 | bx lr 43 | .endfunc 44 | 45 | /* function to caculate 32-bit unsigned division 46 | * args : r0 the number to be modulated 47 | r1 the quotient of division 10 48 | * return: r0 contains the result */ 49 | .global mod_ten 50 | .func mod_ten 51 | mod_ten: 52 | mov r2, #0 53 | start_mod_ten: 54 | cmp r1, #0 55 | beq end_mod_ten 56 | add r2, r2, #10 57 | sub r1, r1, #1 58 | end_mod_ten: 59 | sub r0, r0, r2 60 | bx lr 61 | .endfunc 62 | 63 | 64 | 65 | /* note: detailed information about GPIO-Registers 66 | * can be found on datasheet bcm2835 on page 89-105 67 | */ 68 | 69 | /* set pin's mode: manipulating GPFSEL-Register 70 | * address of register = base + 4 * ( pinNum / 10 ) 71 | * each 3 bits stand for a pin 72 | * mode position of pin = (pinNum % 10) * 3 73 | * 000 = GPIO Pin is an input 74 | * 001 = GPIO Pin is an output 75 | * 100 = GPIO Pin takes alternate function 0 76 | * 101 = GPIO Pin takes alternate function 1 77 | * 110 = GPIO Pin takes alternate function 2 78 | * 111 = GPIO Pin takes alternate function 3 79 | * 011 = GPIO Pin takes alternate function 4 80 | * 010 = GPIO Pin takes alternate function 5 */ 81 | 82 | /* function to set a gpio_pin as input 83 | * args : r0 GPIO_BASE 84 | * r1 BCM-Pin Number */ 85 | .global set_pin_input 86 | .func set_pin_input 87 | set_pin_input: 88 | mov r3, r0 /* backup GPIO_BASE */ 89 | mov r4, r1 /* backup BCM-Pin Number */ 90 | 91 | mov r0, r1 92 | mov r1, #10 93 | bl unsigned_division /* calc Pin / 10, quotient in r2 */ 94 | mov r5, r2, LSL #4 /* r5 = r2 * 4 */ 95 | ldr r1, [r3, r5] 96 | mov r6, r1 /* put the value in GPFSEL in r6 */ 97 | /* calc pinNum mod 10 */ 98 | mov r0, r4 99 | mov r1, r2 /* put the quotient in mod_ten function */ 100 | bl mod_ten 101 | 102 | add r0, r0, LSL #2 /* r0 = 3 * r0 */ 103 | mov r1, #7 104 | mov r1, r1, LSL r0 /* r1 = 7 << (pinNum % 10) * 3 */ 105 | mov r2, #4294967295 106 | eor r1, r1, r2 107 | and r1, r6, r1 108 | str r1, [r3, r5] 109 | bx lr 110 | .endfunc 111 | 112 | /* set pin as output */ 113 | .global set_pin_output 114 | .func set_pin_output 115 | set_pin_output: 116 | ldr r1, [r0, #(4 * (yellowPin / 10))] 117 | and r1, #~(7 << ((yellowPin % 10) * 3)) /* set pin-bits to 000 */ 118 | orr r1, #(1 << ((yellowPin % 10) * 3)) /* set pin-bits to 001 */ 119 | str r1, [r0, #(4 * (yellowPin / 10))] 120 | ldr r1, [r0, #(4 * (greenPin / 10))] 121 | and r1, #~(7 << ((greenPin % 10) * 3)) 122 | orr r1, #(1 << ((greenPin % 10) * 3)) 123 | str r1, [r0, #(4 * (greenPin / 10))] 124 | ldr r1, [r0, #(4 * (redPin / 10))] 125 | and r1, #~(7 << ((redPin % 10) * 3)) 126 | orr r1, #(1 << ((redPin % 10) * 3)) 127 | str r1, [r0, #(4 * (redPin / 10))] 128 | bx lr 129 | .endfunc 130 | 131 | /* get pin(btnPin) volt-level: reading GPLEV-Register 132 | * address of register = base + #(51 + 4 * (pinNum / 32)) 133 | * each bit stand for volt-level of the pin(0:low, 1:high(3.3V)) 134 | */ 135 | .global get_pin_level 136 | .func get_pin_level 137 | get_pin_level: 138 | ldr r1, [r0, #(52 + 4 * (btnPin1 / 32))] 139 | lsr r1, #btnPin1 140 | and r1, #1 141 | mov r0, r1 /* r0 as return value */ 142 | bx lr 143 | .endfunc 144 | 145 | /* control led 146 | * set pin's output: manipulating GPSET-Register 147 | * GPSET0: pin(00-32) ; GPSET1: pin (32-53) 148 | * address of register = 4 * (7 + pinNum / 32) 149 | * each bit stand for one pin(0:low, 1:high(3.3V)) 150 | */ 151 | 152 | /* idle_status: all leds off */ 153 | .global set_idle 154 | .func set_idle 155 | set_idle: 156 | mov r1, #(1 << (yellowPin % 32)) 157 | str r1, [r0, #(4 * (10 + (yellowPin / 32)))] 158 | mov r1, #(1 << (greenPin % 32)) 159 | str r1, [r0, #(4 * (10 + (greenPin / 32)))] 160 | mov r1, #(1 << (redPin % 32)) 161 | str r1, [r0, #(4 * (10 + (redPin / 32)))] 162 | bx lr 163 | .endfunc 164 | 165 | /* on_status: all leds on */ 166 | .global set_all_on 167 | .func set_all_on 168 | set_all_on: 169 | mov r1, #(1 << (yellowPin % 32)) 170 | str r1, [r0, #(4 * (7 + yellowPin / 32))] 171 | mov r1, #(1 << (greenPin % 32)) 172 | str r1, [r0, #(4 * (7 + greenPin / 32))] 173 | mov r1, #(1 << (redPin % 32)) 174 | str r1, [r0, #(4 * (7 + redPin / 32))] 175 | bx lr 176 | .endfunc 177 | 178 | /* functions to set the single led on */ 179 | .global set_yellow_on 180 | .func set_yellow_on 181 | set_yellow_on: 182 | mov r1, #(1 << (yellowPin % 32)) 183 | str r1, [r0, #(4 * (7 + yellowPin / 32))] 184 | mov r1, #(1 << (greenPin % 32)) 185 | str r1, [r0, #(4 * (10 + greenPin / 32))] 186 | mov r1, #(1 << (redPin % 32)) 187 | str r1, [r0, #(4 * (10 + redPin / 32))] 188 | bx lr 189 | .endfunc 190 | 191 | .global set_green_on 192 | .func set_green_on 193 | set_green_on: 194 | mov r1, #(1 << (yellowPin % 32)) 195 | str r1, [r0, #(4 * (10 + yellowPin / 32))] 196 | mov r1, #(1 << (greenPin % 32)) 197 | str r1, [r0, #(4 * (7 + greenPin / 32))] 198 | mov r1, #(1 << (redPin % 32)) 199 | str r1, [r0, #(4 * (10 + redPin / 32))] 200 | bx lr 201 | .endfunc 202 | 203 | .global set_red_on 204 | .func set_red_on 205 | set_red_on: 206 | mov r1, #(1 << (yellowPin % 32)) 207 | str r1, [r0, #(4 * (10 + yellowPin / 32))] 208 | mov r1, #(1 << (greenPin % 32)) 209 | str r1, [r0, #(4 * (10 + greenPin / 32))] 210 | mov r1, #(1 << (redPin % 32)) 211 | str r1, [r0, #(4 * (7 + redPin / 32))] 212 | bx lr 213 | .endfunc 214 | 215 | .global set_led_off 216 | .func set_led_off 217 | set_led_off: 218 | mov r1, #(1 << (redPin% 32)) 219 | str r1, [r0, #(4*(10 + (redPin/ 32)))] 220 | bx lr 221 | .endfunc 222 | 223 | /* on_status: all leds on */ 224 | .global set_led_on 225 | .func set_led_on 226 | set_led_on: 227 | mov r1, #(1 << (redPin% 32)) 228 | str r1, [r0, #(4 * (7 + redPin/ 32))] 229 | bx lr 230 | .endfunc 231 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/pwm_led/pwm.s: -------------------------------------------------------------------------------- 1 | .arch armv6 2 | .eabi_attribute 27, 3 3 | .eabi_attribute 28, 1 4 | .fpu vfp 5 | .eabi_attribute 20, 1 6 | .eabi_attribute 21, 1 7 | .eabi_attribute 23, 3 8 | .eabi_attribute 24, 1 9 | .eabi_attribute 25, 1 10 | .eabi_attribute 26, 2 11 | .eabi_attribute 30, 6 12 | .eabi_attribute 18, 4 13 | .file "pwm.c" 14 | .comm gpio,4,4 15 | .comm pwm,4,4 16 | .comm clk,4,4 17 | .section .rodata 18 | .align 2 19 | .LC0: 20 | .ascii "/dev/mem\000" 21 | .align 2 22 | .LC1: 23 | .ascii "can't open /dev/mem \000" 24 | .align 2 25 | .LC2: 26 | .ascii "allocation error \000" 27 | .align 2 28 | .LC3: 29 | .ascii "mmap error %d\012\000" 30 | .text 31 | .align 2 32 | .global mapRegisterMemory 33 | .type mapRegisterMemory, %function 34 | mapRegisterMemory: 35 | @ args = 0, pretend = 0, frame = 16 36 | @ frame_needed = 1, uses_anonymous_args = 0 37 | stmfd sp!, {fp, lr} 38 | add fp, sp, #4 39 | sub sp, sp, #24 40 | str r0, [fp, #-16] 41 | ldr r3, .L6 42 | ldr r3, [r3, #0] 43 | cmp r3, #0 44 | bne .L2 45 | ldr r0, .L6+4 46 | ldr r1, .L6+8 47 | bl open 48 | mov r2, r0 49 | ldr r3, .L6 50 | str r2, [r3, #0] 51 | ldr r3, .L6 52 | ldr r3, [r3, #0] 53 | cmp r3, #0 54 | bge .L2 55 | ldr r0, .L6+12 56 | bl puts 57 | mvn r0, #0 58 | bl exit 59 | .L2: 60 | ldr r0, .L6+16 61 | bl malloc 62 | mov r3, r0 63 | str r3, [fp, #-8] 64 | ldr r3, [fp, #-8] 65 | cmp r3, #0 66 | bne .L3 67 | ldr r0, .L6+20 68 | bl puts 69 | mvn r0, #0 70 | bl exit 71 | .L3: 72 | ldr r3, [fp, #-8] 73 | mov r3, r3, asl #20 74 | mov r3, r3, lsr #20 75 | cmp r3, #0 76 | beq .L4 77 | ldr r3, [fp, #-8] 78 | mov r3, r3, asl #20 79 | mov r3, r3, lsr #20 80 | rsb r3, r3, #4096 81 | ldr r2, [fp, #-8] 82 | add r3, r2, r3 83 | str r3, [fp, #-8] 84 | .L4: 85 | ldr r3, .L6 86 | ldr r3, [r3, #0] 87 | str r3, [sp, #0] 88 | ldr r3, [fp, #-16] 89 | str r3, [sp, #4] 90 | ldr r0, [fp, #-8] 91 | mov r1, #4096 92 | mov r2, #3 93 | mov r3, #17 94 | bl mmap 95 | str r0, [fp, #-12] 96 | ldr r3, [fp, #-12] 97 | cmp r3, #0 98 | bge .L5 99 | ldr r2, .L6+24 100 | ldr r3, [fp, #-12] 101 | mov r0, r2 102 | mov r1, r3 103 | bl printf 104 | mvn r0, #0 105 | bl exit 106 | .L5: 107 | ldr r3, [fp, #-12] 108 | mov r0, r3 109 | sub sp, fp, #4 110 | ldmfd sp!, {fp, pc} 111 | .L7: 112 | .align 2 113 | .L6: 114 | .word mem_fd.3244 115 | .word .LC0 116 | .word 1052674 117 | .word .LC1 118 | .word 8191 119 | .word .LC2 120 | .word .LC3 121 | .size mapRegisterMemory, .-mapRegisterMemory 122 | .align 2 123 | .global setupRegisterMemoryMappings 124 | .type setupRegisterMemoryMappings, %function 125 | setupRegisterMemoryMappings: 126 | @ args = 0, pretend = 0, frame = 0 127 | @ frame_needed = 1, uses_anonymous_args = 0 128 | stmfd sp!, {fp, lr} 129 | add fp, sp, #4 130 | ldr r0, .L9 131 | bl mapRegisterMemory 132 | mov r2, r0 133 | ldr r3, .L9+4 134 | str r2, [r3, #0] 135 | ldr r0, .L9+8 136 | bl mapRegisterMemory 137 | mov r2, r0 138 | ldr r3, .L9+12 139 | str r2, [r3, #0] 140 | ldr r0, .L9+16 141 | bl mapRegisterMemory 142 | mov r2, r0 143 | ldr r3, .L9+20 144 | str r2, [r3, #0] 145 | ldmfd sp!, {fp, pc} 146 | .L10: 147 | .align 2 148 | .L9: 149 | .word 1059061760 150 | .word gpio 151 | .word 1059110912 152 | .word pwm 153 | .word 1058017280 154 | .word clk 155 | .size setupRegisterMemoryMappings, .-setupRegisterMemoryMappings 156 | .align 2 157 | .global setServo 158 | .type setServo, %function 159 | setServo: 160 | @ args = 0, pretend = 0, frame = 16 161 | @ frame_needed = 1, uses_anonymous_args = 0 162 | @ link register save eliminated. 163 | str fp, [sp, #-4]! 164 | add fp, sp, #0 165 | sub sp, sp, #20 166 | str r0, [fp, #-16] 167 | mov r3, #0 168 | str r3, [fp, #-12] 169 | ldr r3, [fp, #-16] 170 | mov r3, r3, asl #4 171 | ldr r2, .L16 172 | smull r1, r2, r2, r3 173 | mov r2, r2, asr #5 174 | mov r3, r3, asr #31 175 | rsb r3, r3, r2 176 | add r3, r3, #16 177 | str r3, [fp, #-8] 178 | ldr r3, [fp, #-8] 179 | cmp r3, #32 180 | ble .L12 181 | mov r3, #32 182 | str r3, [fp, #-8] 183 | .L12: 184 | ldr r3, [fp, #-8] 185 | cmp r3, #0 186 | bgt .L13 187 | mov r3, #1 188 | str r3, [fp, #-8] 189 | .L13: 190 | mov r3, #0 191 | str r3, [fp, #-12] 192 | b .L14 193 | .L15: 194 | ldr r3, [fp, #-12] 195 | mov r3, r3, asl #1 196 | str r3, [fp, #-12] 197 | ldr r3, [fp, #-12] 198 | orr r3, r3, #1 199 | str r3, [fp, #-12] 200 | ldr r3, [fp, #-8] 201 | sub r3, r3, #1 202 | str r3, [fp, #-8] 203 | .L14: 204 | ldr r3, [fp, #-8] 205 | cmp r3, #0 206 | bne .L15 207 | ldr r3, .L16+4 208 | ldr r3, [r3, #0] 209 | add r3, r3, #20 210 | ldr r2, [fp, #-12] 211 | str r2, [r3, #0] 212 | add sp, fp, #0 213 | ldmfd sp!, {fp} 214 | bx lr 215 | .L17: 216 | .align 2 217 | .L16: 218 | .word 1374389535 219 | .word pwm 220 | .size setServo, .-setServo 221 | .section .rodata 222 | .align 2 223 | .LC4: 224 | .ascii "idiv out of range: %x\012\000" 225 | .text 226 | .align 2 227 | .global initHardware 228 | .type initHardware, %function 229 | initHardware: 230 | @ args = 0, pretend = 0, frame = 8 231 | @ frame_needed = 1, uses_anonymous_args = 0 232 | stmfd sp!, {fp, lr} 233 | add fp, sp, #4 234 | sub sp, sp, #8 235 | bl setupRegisterMemoryMappings 236 | ldr r3, .L21 237 | ldr r3, [r3, #0] 238 | add r3, r3, #4 239 | ldr r2, .L21 240 | ldr r2, [r2, #0] 241 | add r2, r2, #4 242 | ldr r2, [r2, #0] 243 | orr r2, r2, #256 244 | str r2, [r3, #0] 245 | ldr r3, .L21+4 246 | ldr r3, [r3, #0] 247 | add r3, r3, #160 248 | ldr r2, .L21+8 249 | str r2, [r3, #0] 250 | mov r0, #10 251 | bl usleep 252 | mov r3, #1200 253 | str r3, [fp, #-8] 254 | ldr r3, [fp, #-8] 255 | cmp r3, #0 256 | ble .L19 257 | ldr r3, [fp, #-8] 258 | cmp r3, #4096 259 | ble .L20 260 | .L19: 261 | ldr r3, .L21+12 262 | mov r0, r3 263 | ldr r1, [fp, #-8] 264 | bl printf 265 | mvn r0, #0 266 | bl exit 267 | .L20: 268 | ldr r3, .L21+4 269 | ldr r3, [r3, #0] 270 | add r3, r3, #164 271 | ldr r2, [fp, #-8] 272 | mov r2, r2, asl #12 273 | orr r2, r2, #1509949440 274 | str r2, [r3, #0] 275 | ldr r3, .L21+4 276 | ldr r3, [r3, #0] 277 | add r3, r3, #160 278 | ldr r2, .L21+16 279 | str r2, [r3, #0] 280 | ldr r3, .L21+20 281 | ldr r3, [r3, #0] 282 | mov r2, #0 283 | str r2, [r3, #0] 284 | mov r0, #10 285 | bl usleep 286 | ldr r3, .L21+20 287 | ldr r3, [r3, #0] 288 | add r3, r3, #16 289 | mov r2, #320 290 | str r2, [r3, #0] 291 | mov r0, #0 292 | bl setServo 293 | ldr r3, .L21+20 294 | ldr r3, [r3, #0] 295 | mov r2, #3 296 | str r2, [r3, #0] 297 | sub sp, fp, #4 298 | ldmfd sp!, {fp, pc} 299 | .L22: 300 | .align 2 301 | .L21: 302 | .word gpio 303 | .word clk 304 | .word 1509949472 305 | .word .LC4 306 | .word 1509949457 307 | .word pwm 308 | .size initHardware, .-initHardware 309 | .align 2 310 | .global main 311 | .type main, %function 312 | main: 313 | @ args = 0, pretend = 0, frame = 8 314 | @ frame_needed = 1, uses_anonymous_args = 0 315 | stmfd sp!, {fp, lr} 316 | add fp, sp, #4 317 | sub sp, sp, #8 318 | str r0, [fp, #-8] 319 | str r1, [fp, #-12] 320 | bl initHardware 321 | .L24: 322 | mov r0, #0 323 | bl setServo 324 | mov r0, #1 325 | bl sleep 326 | mov r0, #25 327 | bl setServo 328 | mov r0, #1 329 | bl sleep 330 | mov r0, #50 331 | bl setServo 332 | mov r0, #1 333 | bl sleep 334 | mov r0, #75 335 | bl setServo 336 | mov r0, #1 337 | bl sleep 338 | mov r0, #100 339 | bl setServo 340 | mov r0, #1 341 | bl sleep 342 | b .L24 343 | .size main, .-main 344 | .local mem_fd.3244 345 | .comm mem_fd.3244,4,4 346 | .ident "GCC: (Debian 4.6.3-14+rpi1) 4.6.3" 347 | .section .note.GNU-stack,"",%progbits 348 | -------------------------------------------------------------------------------- /asm-rasp/mrt_experiment/pwm_test/pwm.s: -------------------------------------------------------------------------------- 1 | .arch armv6 2 | .eabi_attribute 27, 3 3 | .eabi_attribute 28, 1 4 | .fpu vfp 5 | .eabi_attribute 20, 1 6 | .eabi_attribute 21, 1 7 | .eabi_attribute 23, 3 8 | .eabi_attribute 24, 1 9 | .eabi_attribute 25, 1 10 | .eabi_attribute 26, 2 11 | .eabi_attribute 30, 6 12 | .eabi_attribute 18, 4 13 | .file "pwm.c" 14 | .comm gpio,4,4 15 | .comm pwm,4,4 16 | .comm clk,4,4 17 | .section .rodata 18 | .align 2 19 | .LC0: 20 | .ascii "/dev/mem\000" 21 | .align 2 22 | .LC1: 23 | .ascii "can't open /dev/mem \000" 24 | .align 2 25 | .LC2: 26 | .ascii "allocation error \000" 27 | .align 2 28 | .LC3: 29 | .ascii "mmap error %d\012\000" 30 | .text 31 | .align 2 32 | .global mapRegisterMemory 33 | .type mapRegisterMemory, %function 34 | mapRegisterMemory: 35 | @ args = 0, pretend = 0, frame = 16 36 | @ frame_needed = 1, uses_anonymous_args = 0 37 | stmfd sp!, {fp, lr} 38 | add fp, sp, #4 39 | sub sp, sp, #24 40 | str r0, [fp, #-16] 41 | ldr r3, .L6 42 | ldr r3, [r3, #0] 43 | cmp r3, #0 44 | bne .L2 45 | ldr r0, .L6+4 46 | ldr r1, .L6+8 47 | bl open 48 | mov r2, r0 49 | ldr r3, .L6 50 | str r2, [r3, #0] 51 | ldr r3, .L6 52 | ldr r3, [r3, #0] 53 | cmp r3, #0 54 | bge .L2 55 | ldr r0, .L6+12 56 | bl puts 57 | mvn r0, #0 58 | bl exit 59 | .L2: 60 | ldr r0, .L6+16 61 | bl malloc 62 | mov r3, r0 63 | str r3, [fp, #-8] 64 | ldr r3, [fp, #-8] 65 | cmp r3, #0 66 | bne .L3 67 | ldr r0, .L6+20 68 | bl puts 69 | mvn r0, #0 70 | bl exit 71 | .L3: 72 | ldr r3, [fp, #-8] 73 | mov r3, r3, asl #20 74 | mov r3, r3, lsr #20 75 | cmp r3, #0 76 | beq .L4 77 | ldr r3, [fp, #-8] 78 | mov r3, r3, asl #20 79 | mov r3, r3, lsr #20 80 | rsb r3, r3, #4096 81 | ldr r2, [fp, #-8] 82 | add r3, r2, r3 83 | str r3, [fp, #-8] 84 | .L4: 85 | ldr r3, .L6 86 | ldr r3, [r3, #0] 87 | str r3, [sp, #0] 88 | ldr r3, [fp, #-16] 89 | str r3, [sp, #4] 90 | ldr r0, [fp, #-8] 91 | mov r1, #4096 92 | mov r2, #3 93 | mov r3, #17 94 | bl mmap 95 | str r0, [fp, #-12] 96 | ldr r3, [fp, #-12] 97 | cmp r3, #0 98 | bge .L5 99 | ldr r2, .L6+24 100 | ldr r3, [fp, #-12] 101 | mov r0, r2 102 | mov r1, r3 103 | bl printf 104 | mvn r0, #0 105 | bl exit 106 | .L5: 107 | ldr r3, [fp, #-12] 108 | mov r0, r3 109 | sub sp, fp, #4 110 | ldmfd sp!, {fp, pc} 111 | .L7: 112 | .align 2 113 | .L6: 114 | .word mem_fd.3244 115 | .word .LC0 116 | .word 1052674 117 | .word .LC1 118 | .word 8191 119 | .word .LC2 120 | .word .LC3 121 | .size mapRegisterMemory, .-mapRegisterMemory 122 | .align 2 123 | .global setupRegisterMemoryMappings 124 | .type setupRegisterMemoryMappings, %function 125 | setupRegisterMemoryMappings: 126 | @ args = 0, pretend = 0, frame = 0 127 | @ frame_needed = 1, uses_anonymous_args = 0 128 | stmfd sp!, {fp, lr} 129 | add fp, sp, #4 130 | ldr r0, .L9 131 | bl mapRegisterMemory 132 | mov r2, r0 133 | ldr r3, .L9+4 134 | str r2, [r3, #0] 135 | ldr r0, .L9+8 136 | bl mapRegisterMemory 137 | mov r2, r0 138 | ldr r3, .L9+12 139 | str r2, [r3, #0] 140 | ldr r0, .L9+16 141 | bl mapRegisterMemory 142 | mov r2, r0 143 | ldr r3, .L9+20 144 | str r2, [r3, #0] 145 | ldmfd sp!, {fp, pc} 146 | .L10: 147 | .align 2 148 | .L9: 149 | .word 1059061760 150 | .word gpio 151 | .word 1059110912 152 | .word pwm 153 | .word 1058017280 154 | .word clk 155 | .size setupRegisterMemoryMappings, .-setupRegisterMemoryMappings 156 | .align 2 157 | .global setServo 158 | .type setServo, %function 159 | setServo: 160 | @ args = 0, pretend = 0, frame = 16 161 | @ frame_needed = 1, uses_anonymous_args = 0 162 | @ link register save eliminated. 163 | str fp, [sp, #-4]! 164 | add fp, sp, #0 165 | sub sp, sp, #20 166 | str r0, [fp, #-16] 167 | mov r3, #0 168 | str r3, [fp, #-12] 169 | ldr r3, [fp, #-16] 170 | mov r3, r3, asl #4 171 | ldr r2, .L16 172 | smull r1, r2, r2, r3 173 | mov r2, r2, asr #5 174 | mov r3, r3, asr #31 175 | rsb r3, r3, r2 176 | add r3, r3, #16 177 | str r3, [fp, #-8] 178 | ldr r3, [fp, #-8] 179 | cmp r3, #32 180 | ble .L12 181 | mov r3, #32 182 | str r3, [fp, #-8] 183 | .L12: 184 | ldr r3, [fp, #-8] 185 | cmp r3, #0 186 | bgt .L13 187 | mov r3, #1 188 | str r3, [fp, #-8] 189 | .L13: 190 | mov r3, #0 191 | str r3, [fp, #-12] 192 | b .L14 193 | .L15: 194 | ldr r3, [fp, #-12] 195 | mov r3, r3, asl #1 196 | str r3, [fp, #-12] 197 | ldr r3, [fp, #-12] 198 | orr r3, r3, #1 199 | str r3, [fp, #-12] 200 | ldr r3, [fp, #-8] 201 | sub r3, r3, #1 202 | str r3, [fp, #-8] 203 | .L14: 204 | ldr r3, [fp, #-8] 205 | cmp r3, #0 206 | bne .L15 207 | ldr r3, .L16+4 208 | ldr r3, [r3, #0] 209 | add r3, r3, #20 210 | ldr r2, [fp, #-12] 211 | str r2, [r3, #0] 212 | add sp, fp, #0 213 | ldmfd sp!, {fp} 214 | bx lr 215 | .L17: 216 | .align 2 217 | .L16: 218 | .word 1374389535 219 | .word pwm 220 | .size setServo, .-setServo 221 | .section .rodata 222 | .align 2 223 | .LC4: 224 | .ascii "idiv out of range: %x\012\000" 225 | .text 226 | .align 2 227 | .global initHardware 228 | .type initHardware, %function 229 | initHardware: 230 | @ args = 0, pretend = 0, frame = 8 231 | @ frame_needed = 1, uses_anonymous_args = 0 232 | stmfd sp!, {fp, lr} 233 | add fp, sp, #4 234 | sub sp, sp, #8 235 | bl setupRegisterMemoryMappings 236 | ldr r3, .L21 237 | ldr r3, [r3, #0] 238 | add r3, r3, #4 239 | ldr r2, .L21 240 | ldr r2, [r2, #0] 241 | add r2, r2, #4 242 | ldr r2, [r2, #0] 243 | orr r2, r2, #256 244 | str r2, [r3, #0] 245 | ldr r3, .L21+4 246 | ldr r3, [r3, #0] 247 | add r3, r3, #160 248 | ldr r2, .L21+8 249 | str r2, [r3, #0] 250 | mov r0, #10 251 | bl usleep 252 | mov r3, #1200 253 | str r3, [fp, #-8] 254 | ldr r3, [fp, #-8] 255 | cmp r3, #0 256 | ble .L19 257 | ldr r3, [fp, #-8] 258 | cmp r3, #4096 259 | ble .L20 260 | .L19: 261 | ldr r3, .L21+12 262 | mov r0, r3 263 | ldr r1, [fp, #-8] 264 | bl printf 265 | mvn r0, #0 266 | bl exit 267 | .L20: 268 | ldr r3, .L21+4 269 | ldr r3, [r3, #0] 270 | add r3, r3, #164 271 | ldr r2, [fp, #-8] 272 | mov r2, r2, asl #12 273 | orr r2, r2, #1509949440 274 | str r2, [r3, #0] 275 | ldr r3, .L21+4 276 | ldr r3, [r3, #0] 277 | add r3, r3, #160 278 | ldr r2, .L21+16 279 | str r2, [r3, #0] 280 | ldr r3, .L21+20 281 | ldr r3, [r3, #0] 282 | mov r2, #0 283 | str r2, [r3, #0] 284 | mov r0, #10 285 | bl usleep 286 | ldr r3, .L21+20 287 | ldr r3, [r3, #0] 288 | add r3, r3, #16 289 | mov r2, #320 290 | str r2, [r3, #0] 291 | mov r0, #0 292 | bl setServo 293 | ldr r3, .L21+20 294 | ldr r3, [r3, #0] 295 | mov r2, #3 296 | str r2, [r3, #0] 297 | sub sp, fp, #4 298 | ldmfd sp!, {fp, pc} 299 | .L22: 300 | .align 2 301 | .L21: 302 | .word gpio 303 | .word clk 304 | .word 1509949472 305 | .word .LC4 306 | .word 1509949457 307 | .word pwm 308 | .size initHardware, .-initHardware 309 | .align 2 310 | .global main 311 | .type main, %function 312 | main: 313 | @ args = 0, pretend = 0, frame = 8 314 | @ frame_needed = 1, uses_anonymous_args = 0 315 | stmfd sp!, {fp, lr} 316 | add fp, sp, #4 317 | sub sp, sp, #8 318 | str r0, [fp, #-8] 319 | str r1, [fp, #-12] 320 | bl initHardware 321 | .L24: 322 | mov r0, #0 323 | bl setServo 324 | mov r0, #1 325 | bl sleep 326 | mov r0, #25 327 | bl setServo 328 | mov r0, #1 329 | bl sleep 330 | mov r0, #50 331 | bl setServo 332 | mov r0, #1 333 | bl sleep 334 | mov r0, #75 335 | bl setServo 336 | mov r0, #1 337 | bl sleep 338 | mov r0, #100 339 | bl setServo 340 | mov r0, #1 341 | bl sleep 342 | b .L24 343 | .size main, .-main 344 | .local mem_fd.3244 345 | .comm mem_fd.3244,4,4 346 | .ident "GCC: (Debian 4.6.3-14+rpi1) 4.6.3" 347 | .section .note.GNU-stack,"",%progbits 348 | --------------------------------------------------------------------------------