├── chapter-6 ├── dagu.cpp ├── dagu-black.cpp ├── sns-black.cpp ├── sns.cpp └── weather.js ├── BeagleBone code examples.docx ├── chapter-5 ├── am335x-bone.dts ├── spi.js ├── adc1.cpp ├── bone_pwm_P8_13-00A0.dts ├── cape-bone-iio-00A0.dts ├── interrupt.js ├── uart1.dts ├── am33xx_pwm-00A0.dts ├── adc1-black.cpp ├── adc2.cpp ├── adc2-black.cpp ├── led1-black.cpp ├── am335x-boneblack.dts ├── led1.cpp ├── uart1-black.cpp ├── pwm.cpp ├── led3-black.cpp ├── pwm-black.cpp ├── uart1.cpp ├── led3.cpp ├── sq_wave-black.cpp ├── sq_wave.cpp ├── led2-black.cpp ├── led2.cpp ├── am335x-bone-common.dtsi └── am33xx.dtsi ├── chapter-1 └── blinkled.js ├── chapter-2 ├── irtest2.js ├── irtest.js ├── irtest1.js ├── fail.js ├── blinky.js ├── next.js └── almanzas.js ├── README.md ├── chapter-4 ├── 4Wrobot.js └── ROV.js ├── chapter-3 ├── LCD1.js ├── LCD2.js └── cpuload.js ├── sns2.cpp └── appendix ├── LCD2-black.cpp └── LCD2.cpp /chapter-6/dagu.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jadonk/bad-to-the-bone-examples/HEAD/chapter-6/dagu.cpp -------------------------------------------------------------------------------- /chapter-6/dagu-black.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jadonk/bad-to-the-bone-examples/HEAD/chapter-6/dagu-black.cpp -------------------------------------------------------------------------------- /BeagleBone code examples.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jadonk/bad-to-the-bone-examples/HEAD/BeagleBone code examples.docx -------------------------------------------------------------------------------- /chapter-5/am335x-bone.dts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/ 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 as 6 | * published by the Free Software Foundation. 7 | */ 8 | /dts-v1/; 9 | 10 | /include/ "am33xx.dtsi" 11 | 12 | /include/ "am335x-bone-common.dtsi" 13 | -------------------------------------------------------------------------------- /chapter-5/spi.js: -------------------------------------------------------------------------------- 1 | //********************************************************* 2 | 3 | var b = require('bonescript'); 4 | 5 | //Old bonescript defines 'bone' globally 6 | var pins = (typeof bone != 'undefined') ? bone : b.bone.pins; 7 | 8 | var spi_data_pin = pins.P8_13; 9 | var spi_clk_pin = pins.P8_11; 10 | 11 | b.pinMode(spi_data_pin, b.OUTPUT); 12 | b.pinMode(spi_clk_pin, b.OUTPUT); 13 | b.shiftOut(spi_data_pin, spi_clk_pin, b.LSBFIRST, 0xAC); 14 | 15 | //********************************************************* 16 | 17 | -------------------------------------------------------------------------------- /chapter-1/blinkled.js: -------------------------------------------------------------------------------- 1 | var b = require('bonescript'); 2 | 3 | //Old bonescript defines 'bone' globally 4 | var pins = (typeof bone != 'undefined') ? bone : b.bone.pins; 5 | 6 | var ledPin = pins.P8_13; 7 | var ledPin2 = pins.USR3; 8 | 9 | b.pinMode(ledPin, b.OUTPUT); 10 | b.pinMode(ledPin2, b.OUTPUT); 11 | 12 | var state = b.LOW; 13 | b.digitalWrite(ledPin, state); 14 | b.digitalWrite(ledPin2, state); 15 | 16 | setInterval(toggle, 1000); 17 | 18 | function toggle() { 19 | if(state == b.LOW) state = b.HIGH; 20 | else state = b.LOW; 21 | b.digitalWrite(ledPin, state); 22 | b.digitalWrite(ledPin2, state); 23 | } 24 | -------------------------------------------------------------------------------- /chapter-2/irtest2.js: -------------------------------------------------------------------------------- 1 | //*********************************************************************** 2 | 3 | var b = require('bonescript'); 4 | 5 | //Old bonescript defines 'bone' globally 6 | var pins = (typeof bone != 'undefined') ? bone : b.bone.pins; 7 | 8 | var ledPin = pins.P9_14; //PWM pin for LED interface 9 | var ainPin = pins.P9_39; //analog input pin for IR sensor 10 | var IR_sensor_value; 11 | 12 | b.pinMode(ledPin, b.OUTPUT); //set pin to digital output 13 | 14 | while(1) 15 | { 16 | //read analog output from IR sensor 17 | //normalized value ranges from 0..1 18 | IR_sensor_value = b.analogRead(ainPin); 19 | b.analogWrite(ledPin, IR_sensor_value); 20 | } 21 | 22 | //*********************************************************************** 23 | 24 | -------------------------------------------------------------------------------- /chapter-5/adc1.cpp: -------------------------------------------------------------------------------- 1 | //************************************************************** 2 | //adc1.cpp: the analog value on AIN0 (P9, pin 39) provided by 3 | //a LM34 temperature sensor is read continuously until 4 | //[Control][C] is used to stop the program. 5 | //************************************************************** 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #define output "out" 13 | #define input "in" 14 | 15 | int main (void) 16 | { 17 | //define file handles 18 | FILE *ifp_ain1; 19 | float ain0_value; 20 | 21 | ifp_ain1 = fopen("/sys/bus/platform/devices/tsc/ain1", "r"); 22 | if (ifp_ain1 == NULL) {printf("Unable to ain1.\n");} 23 | 24 | while(1) 25 | { 26 | fseek(ifp_ain1, 0, SEEK_SET); 27 | fscanf(ifp_ain1, "%f", &ain0_value); 28 | printf("%f\n", ain0_value); 29 | } 30 | 31 | fclose(ifp_ain1); 32 | return 1; 33 | } 34 | //************************************************************** 35 | 36 | -------------------------------------------------------------------------------- /chapter-5/bone_pwm_P8_13-00A0.dts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 CircuitCo 3 | * Copyright (C) 2013 Texas Instruments 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | */ 9 | /dts-v1/; 10 | /plugin/; 11 | 12 | / { 13 | compatible = "ti,beaglebone"; 14 | 15 | /* identification */ 16 | part-number = "bone_pwm_P8_13"; 17 | version = "00A0"; 18 | 19 | fragment@0 { 20 | target = <&am33xx_pinmux>; 21 | __overlay__ { 22 | pwm_P8_13: pinmux_pwm_P8_13_pins { 23 | pinctrl-single,pins = <0x024 0x4>; /* P8_13 (ZCZ ball T10) | MODE 4 */ 24 | }; 25 | }; 26 | }; 27 | 28 | fragment@1 { 29 | target = <&ocp>; 30 | __overlay__ { 31 | pwm_test_P8_13 { 32 | compatible = "pwm_test"; 33 | pwms = <&ehrpwm2 1 500000 1>; 34 | pwm-names = "PWM_P8_13"; 35 | pinctrl-names = "default"; 36 | pinctrl-0 = <&pwm_P8_13>; 37 | enabled = <1>; 38 | duty = <0>; 39 | status = "okay"; 40 | }; 41 | }; 42 | }; 43 | }; 44 | -------------------------------------------------------------------------------- /chapter-2/irtest.js: -------------------------------------------------------------------------------- 1 | //*********************************************************************** 2 | 3 | var b=require('bonescript'); 4 | 5 | //Old bonescript defines 'bone' globally 6 | var pins = (typeof bone != 'undefined') ? bone : b.bone.pins; 7 | 8 | var ledPin = pins.P8_13; //digital pin for LED interface 9 | var ainPin = pins.P9_39; //analog input pin for IR sensor 10 | var IR_sensor_value; 11 | 12 | b.pinMode(ledPin, b.OUTPUT); //set pin to digital output 13 | 14 | while(1) 15 | { 16 | //read analog output from IR sensor 17 | //normalized value ranges from 0..1 18 | IR_sensor_value = b.analogRead(ainPin); 19 | //assumes desired threshold at 20 | //1.25 VDC with max value of 1.75 VDC 21 | if(IR_sensor_value > 0.714) 22 | { 23 | b.digitalWrite(ledPin, b.HIGH); //turn LED on 24 | } 25 | else 26 | { 27 | b.digitalWrite(ledPin, b.LOW); //turn LED off 28 | } 29 | } 30 | 31 | //*********************************************************************** 32 | 33 | -------------------------------------------------------------------------------- /chapter-2/irtest1.js: -------------------------------------------------------------------------------- 1 | //*********************************************************************** 2 | 3 | var b=require('bonescript'); 4 | 5 | //Old bonescript defines 'bone' globally 6 | var pins = (typeof bone != 'undefined') ? bone : b.bone.pins; 7 | 8 | var ledPin = pins.P8_13; //digital pin for LED interface 9 | var ainPin = pins.P9_39; //analog input pin for IR sensor 10 | var IR_sensor_value; 11 | 12 | b.pinMode(ledPin, b.OUTPUT); //set pin to digital output 13 | 14 | while(1) 15 | { 16 | //read analog output from IR sensor 17 | //normalized value ranges from 0..1 18 | IR_sensor_value = b.analogRead(ainPin); 19 | //assumes desired threshold at 20 | //1.25 VDC with max value of 1.75 VDC 21 | if(IR_sensor_value > 0.714) 22 | { 23 | b.digitalWrite(ledPin, b.HIGH); //turn LED on 24 | } 25 | else 26 | { 27 | b.digitalWrite(ledPin, b.LOW); //turn LED off 28 | } 29 | } 30 | 31 | //*********************************************************************** 32 | 33 | -------------------------------------------------------------------------------- /chapter-5/cape-bone-iio-00A0.dts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/ 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 as 6 | * published by the Free Software Foundation. 7 | */ 8 | /dts-v1/; 9 | /plugin/; 10 | 11 | / { 12 | compatible = "ti,beaglebone", "ti,beaglebone-black"; 13 | 14 | /* identification */ 15 | part-number = "iio-test"; 16 | 17 | fragment@0 { 18 | target = <&ocp>; 19 | __overlay__ { 20 | /* avoid stupid warning */ 21 | #address-cells = <1>; 22 | #size-cells = <1>; 23 | 24 | tscadc { 25 | compatible = "ti,ti-tscadc"; 26 | reg = <0x44e0d000 0x1000>; 27 | 28 | interrupt-parent = <&intc>; 29 | interrupts = <16>; 30 | ti,hwmods = "adc_tsc"; 31 | status = "okay"; 32 | 33 | adc { 34 | ti,adc-channels = <8>; 35 | }; 36 | }; 37 | 38 | test_helper: helper { 39 | compatible = "bone-iio-helper"; 40 | vsense-name = "AIN0", "AIN1", "AIN2", "AIN3", "AIN4", "AIN5", "AIN6", "AIN7"; 41 | vsense-scale = <100 100 100 100 100 100 100 100>; 42 | status = "okay"; 43 | }; 44 | }; 45 | }; 46 | }; 47 | -------------------------------------------------------------------------------- /chapter-5/interrupt.js: -------------------------------------------------------------------------------- 1 | //************************************************************* 2 | var b = require('bonescript'); 3 | 4 | //Old bonescript defines 'bone' globally 5 | var pins = (typeof bone != 'undefined') ? bone : b.bone.pins; 6 | 7 | var greenLED = pins.P8_13; 8 | var redLED = pins.P9_14; 9 | var inputSW = pins.P8_15; 10 | var main_delay = 100; 11 | var inter_delay = 50; 12 | var state = b.LOW; 13 | 14 | b.pinMode(greenLED, b.OUTPUT); 15 | b.pinMode(redLED, b.OUTPUT); 16 | b.pinMode(inputSW, b.INPUT); 17 | b.attachInterrupt(inputSW, blink_red, b.RISING); 18 | 19 | function loop() 20 | { 21 | state = (state == b.HIGH) ? b.LOW : b.HIGH; 22 | b.digitalWrite(greenLED, state); 23 | setTimeout(loop, main_delay); 24 | } 25 | 26 | function blink_red() 27 | { 28 | b.digitalWrite(redLED, b.HIGH); 29 | setTimeout(blink_red2, inter_delay); 30 | } 31 | 32 | function blink_red2() 33 | { 34 | b.digitalWrite(redLED, b.LOW); 35 | setTimeout(blink_red3, inter_delay); 36 | } 37 | 38 | function blink_red3() 39 | { 40 | b.digitalWrite(redLED, b.HIGH); 41 | setTimeout(blink_red4, inter_delay); 42 | } 43 | 44 | function blink_red4() 45 | { 46 | b.digitalWrite(redLED, b.LOW); 47 | } 48 | //******************************************************* 49 | 50 | -------------------------------------------------------------------------------- /chapter-5/uart1.dts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 CircuitCo 3 | * Copyright (C) 2013 Texas Instruments 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | */ 9 | /dts-v1/; 10 | /plugin/; 11 | 12 | / { 13 | compatible = "ti,beaglebone", "ti,beaglebone-black"; 14 | 15 | /* identification */ 16 | part-number = "uart1"; 17 | version = "00A0"; 18 | 19 | fragment@0 { 20 | target = <&am33xx_pinmux>; 21 | __overlay__ { 22 | pinmux_serial1: pinmux_serial1_pins { 23 | pinctrl-single,pins = < 24 | 0x184 0x20 /* P9_24 (ZCZ ball D15) RX-enabled MODE 0 */ 25 | 0x180 0x20 /* P9_26 (ZCZ ball D16) RX-enabled MODE 0 */ 26 | >; 27 | }; 28 | }; 29 | }; 30 | 31 | fragment@1 { 32 | target = <&ocp>; 33 | __overlay__ { 34 | serial1_pinmux_helper { 35 | compatible = "bone-pinmux-helper"; 36 | status = "okay"; 37 | pinctrl-names = "default"; 38 | pinctrl-0 = <&pinmux_serial1>; 39 | }; 40 | }; 41 | }; 42 | 43 | fragment@2 { 44 | target = <&uart2>; /* really uart1 */ 45 | __overlay__ { 46 | status = "okay"; 47 | }; 48 | }; 49 | }; 50 | -------------------------------------------------------------------------------- /chapter-5/am33xx_pwm-00A0.dts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 CircuitCo 3 | * Copyright (C) 2013 Texas Instruments 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | */ 9 | /dts-v1/; 10 | /plugin/; 11 | 12 | / { 13 | compatible = "ti,beaglebone"; 14 | 15 | /* identification */ 16 | part-number = "test1"; 17 | version = "00A0"; 18 | 19 | fragment@0 { 20 | target = <&epwmss0>; 21 | __overlay__ { 22 | status = "okay"; 23 | }; 24 | }; 25 | 26 | fragment@1 { 27 | target = <&ehrpwm0>; 28 | __overlay__ { 29 | status = "okay"; 30 | }; 31 | }; 32 | 33 | fragment@2 { 34 | target = <&ecap0>; 35 | __overlay__ { 36 | status = "okay"; 37 | }; 38 | }; 39 | 40 | fragment@3 { 41 | target = <&epwmss1>; 42 | __overlay__ { 43 | status = "okay"; 44 | }; 45 | }; 46 | 47 | fragment@4 { 48 | target = <&ehrpwm1>; 49 | __overlay__ { 50 | status = "okay"; 51 | }; 52 | }; 53 | 54 | fragment@5 { 55 | target = <&epwmss2>; 56 | __overlay__ { 57 | status = "okay"; 58 | }; 59 | }; 60 | 61 | fragment@6 { 62 | target = <&ehrpwm2>; 63 | __overlay__ { 64 | status = "okay"; 65 | }; 66 | }; 67 | 68 | fragment@7 { 69 | target = <&ecap2>; 70 | __overlay__ { 71 | status = "okay"; 72 | }; 73 | }; 74 | }; 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Bad to the Bone 2 | =============== 3 | 4 | Code examples from 5 | 6 | [Bad to the Bone: Crafting Electronics Systems with Beaglebone and BeagleBone Black](https://www.amazon.com/dp/1627051376/ref=as_li_ss_til?tag=bloghanerhead-20&camp=0&creative=0&linkCode=as4&creativeASIN=1627051376&adid=18HMQH5WC5MSYCGY32QD&) 7 | by Steven Barrett and Jason Kridner 8 | 9 | This comprehensive book provides detailed materials for both novice and experienced programmers using all BeagleBone variants which host a powerful 32-bit, super-scalar TI Sitara ARM Cortex A8 processor. Authored by Steven F. Barrett and Jason Kridner, a seasoned ECE educator along with the founder of Beagleboard.org, respectively, the work may be used in a wide variety of projects from science fair projects to university courses and senior design projects to first prototypes of very complex systems. Beginners may access the power of the "Bone" through the user-friendly Bonescript examples. Seasoned users may take full advantage of the Bone's power using the underlying Linux-based operating system, a host of feature extension boards (Capes) and a wide variety of Linux community open source libraries. The book contains background theory on system operation coupled with many well-documented, illustrative examples. Examples for novice users are centered on motivational, fun robot projects while advanced projects follow the theme of assistive technology and image processing applications. 10 | -------------------------------------------------------------------------------- /chapter-5/adc1-black.cpp: -------------------------------------------------------------------------------- 1 | //************************************************************** 2 | //adc1.cpp: the analog value on AIN0 (P9, pin 39) provided by 3 | //a LM34 temperature sensor is read continuously until 4 | //[Control][C] is used to stop the program. 5 | // 6 | //Before the AIN values show up, you must perform: 7 | // echo cape-bone-iio > /sys/devices/bone_capemgr.*/slots 8 | // 9 | //Notes: 10 | // * The ".11" after helper can change between software revs 11 | // * The scale of values is now 0-1800 (mV) instead of 0-4095 12 | // * The AINx indexes now match the hardware documentation 13 | // instead of being off by 1 14 | //************************************************************** 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #define output "out" 22 | #define input "in" 23 | 24 | int main (void) 25 | { 26 | //define file handles 27 | FILE *ifp_ain0; 28 | float ain0_value; 29 | 30 | ifp_ain0 = fopen("/sys/module/bone_iio_helper/drivers/platform:bone-iio-helper/helper.11/AIN0", "r"); 31 | if (ifp_ain0 == NULL) {printf("Unable to AIN0.\n");} 32 | 33 | while(1) 34 | { 35 | fseek(ifp_ain0, 0, SEEK_SET); 36 | fscanf(ifp_ain0, "%f", &ain0_value); 37 | printf("%f\n", ain0_value); 38 | } 39 | 40 | fclose(ifp_ain0); 41 | return 1; 42 | } 43 | //************************************************************** 44 | 45 | -------------------------------------------------------------------------------- /chapter-2/fail.js: -------------------------------------------------------------------------------- 1 | //****************************************************************** 2 | // next.js - A pattern for calling sequential functions in node.js 3 | //****************************************************************** 4 | 5 | mysteps(done); 6 | 7 | //****************************************************************** 8 | //done 9 | //****************************************************************** 10 | 11 | function done() { 12 | console.log("done"); 13 | } 14 | 15 | //****************************************************************** 16 | //mysteps 17 | //****************************************************************** 18 | 19 | function mysteps(callback) 20 | { 21 | //Provide a list of functions to call 22 | var steps = 23 | [ 24 | function(){ console.log("i = " + i); setTimeout(next, 1000); }, //delay 1s 25 | function(){ console.log("i = " + i); setTimeout(next, 1000); }, //delay 1s 26 | function(){ console.log("i = " + i); setTimeout(next, 1000); }, //delay 1s 27 | function(){ console.log("i = " + i); setTimeout(next, 1000); }, //delay 1s 28 | function(){ console.log("i = " + i); setTimeout(next, 1000); }, //delay 1s 29 | function(){ console.log("i = " + i); setTimeout(next, 1000); }, //delay 1s 30 | function(){ callback(); } 31 | ]; 32 | 33 | //Start at 0 34 | var i = 0; 35 | 36 | process.on('SIGINT', next); 37 | console.log("i = " + i); 38 | next(); //Call the first function 39 | 40 | //Nested helper function to call the next function in 'steps' 41 | function next() 42 | { 43 | i++ 44 | steps[i-1](); 45 | } 46 | } 47 | 48 | //****************************************************************** 49 | -------------------------------------------------------------------------------- /chapter-5/adc2.cpp: -------------------------------------------------------------------------------- 1 | //************************************************************** 2 | //include files 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | //define 9 | #define output "out" 10 | #define input "in" 11 | #define ain1_in "/sys/bus/platform/devices/tsc/ain1" 12 | 13 | //function prototypes 14 | void delay_sec(float delay_value); 15 | 16 | int main (void) 17 | { 18 | //define file handles 19 | FILE *ifp_ain1; 20 | float ain0_value; 21 | float ain0_voltage; 22 | float ain0_temp; 23 | 24 | ifp_ain1 = fopen(ain1_in, "r"); 25 | if (ifp_ain1 == NULL) {printf("Unable to ain1.\n");} 26 | 27 | while(1) 28 | { 29 | fseek(ifp_ain1, 0, SEEK_SET); 30 | fscanf(ifp_ain1, "%f", &ain0_value); 31 | printf("AINO reading [of 4096]: %f\n", ain0_value); 32 | ain0_voltage = ((ain0_value/4096.0) * 1.8); 33 | printf("AINO voltage [V]: %f\n", ain0_voltage); 34 | ain0_temp = (ain0_voltage/.010); 35 | printf("AINO temperature [F]: %f\n\n", ain0_temp); 36 | delay_sec(2.0); 37 | } 38 | fclose(ifp_ain1); 39 | return 1; 40 | } 41 | 42 | //************************************************************** 43 | //function definitions 44 | //************************************************************** 45 | 46 | void delay_sec(float delay_value) 47 | { 48 | time_t now, later; 49 | 50 | now = time(NULL); 51 | later = time (NULL); 52 | 53 | while(difftime(later, now) < delay_value) 54 | { 55 | later = time(NULL); //keep checking time 56 | } 57 | } 58 | 59 | //************************************************************** 60 | 61 | -------------------------------------------------------------------------------- /chapter-5/adc2-black.cpp: -------------------------------------------------------------------------------- 1 | //************************************************************** 2 | //include files 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | //define 9 | #define output "out" 10 | #define input "in" 11 | #define ain1_in "/sys/module/bone_iio_helper/drivers/platform:bone-iio-helper/helper.11/AIN0" 12 | 13 | //function prototypes 14 | void delay_sec(float delay_value); 15 | 16 | int main (void) 17 | { 18 | //define file handles 19 | FILE *ifp_ain1; 20 | float ain0_value; 21 | float ain0_voltage; 22 | float ain0_temp; 23 | 24 | ifp_ain1 = fopen(ain1_in, "r"); 25 | if (ifp_ain1 == NULL) {printf("Unable to ain1.\n");} 26 | 27 | while(1) 28 | { 29 | fseek(ifp_ain1, 0, SEEK_SET); 30 | fscanf(ifp_ain1, "%f", &ain0_value); 31 | printf("AINO reading [of 1800]: %f\n", ain0_value); 32 | ain0_voltage = ((ain0_value/1800.0) * 1.8); 33 | printf("AINO voltage [V]: %f\n", ain0_voltage); 34 | ain0_temp = (ain0_voltage/.010); 35 | printf("AINO temperature [F]: %f\n\n", ain0_temp); 36 | delay_sec(2.0); 37 | } 38 | fclose(ifp_ain1); 39 | return 1; 40 | } 41 | 42 | //************************************************************** 43 | //function definitions 44 | //************************************************************** 45 | 46 | void delay_sec(float delay_value) 47 | { 48 | time_t now, later; 49 | 50 | now = time(NULL); 51 | later = time (NULL); 52 | 53 | while(difftime(later, now) < delay_value) 54 | { 55 | later = time(NULL); //keep checking time 56 | } 57 | } 58 | 59 | //************************************************************** 60 | 61 | -------------------------------------------------------------------------------- /chapter-5/led1-black.cpp: -------------------------------------------------------------------------------- 1 | //******************************************************************* 2 | //led1.cpp: illuminates an LED connected to expansion header P8, pin13 3 | //(GPIO1_12 designated as gpio44) 4 | //******************************************************************* 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | #define output "out" 11 | #define input "in" 12 | 13 | int main (void) 14 | { 15 | //define file handles 16 | FILE *ofp_export, *ofp_gpio44_value, *ofp_gpio44_direction; 17 | 18 | //define pin variables 19 | int pin_number = 44, logic_status = 0; 20 | char* pin_direction = output; 21 | 22 | //establish a direction and value file within export for gpio44 23 | ofp_export = fopen("/sys/class/gpio/export", "w"); 24 | if(ofp_export == NULL) {printf("Unable to open export.\n");} 25 | fseek(ofp_export, 0, SEEK_SET); 26 | fprintf(ofp_export, "%d", pin_number); 27 | fflush(ofp_export); 28 | 29 | //configure gpio44 for writing 30 | ofp_gpio44_direction = fopen("/sys/class/gpio/gpio44/direction", "w"); 31 | if(ofp_gpio44_direction == NULL) {printf("Unable to open gpio44_direction.\n");} 32 | fseek(ofp_gpio44_direction, 0, SEEK_SET); 33 | fprintf(ofp_gpio44_direction, "%s", pin_direction); 34 | fflush(ofp_gpio44_direction); 35 | 36 | //write a logic 1 to gpio44 to illuminate the LED 37 | ofp_gpio44_value = fopen("/sys/class/gpio/gpio44/value", "w"); 38 | if(ofp_gpio44_value == NULL) {printf("Unable to open gpio44_value.\n");} 39 | fseek(ofp_gpio44_value, 0, SEEK_SET); 40 | fprintf(ofp_gpio44_value, "%d", logic_status); 41 | fflush(ofp_gpio44_value); 42 | 43 | //close all files 44 | fclose(ofp_export); 45 | fclose(ofp_gpio44_direction); 46 | fclose(ofp_gpio44_value); 47 | return 1; 48 | } 49 | //******************************************************************** 50 | 51 | -------------------------------------------------------------------------------- /chapter-2/blinky.js: -------------------------------------------------------------------------------- 1 | //*********************************************************************** 2 | 3 | var b = require('bonescript'); 4 | 5 | //Old bonescript defines 'bone' globally 6 | var pins = (typeof bone != 'undefined') ? bone : b.bone.pins; 7 | 8 | var left_IR_sensor = pins.P9_39; //analog input for left IR sensor 9 | var center_IR_sensor = pins.P9_40; //analog input for center IR sensor 10 | var right_IR_sensor = pins.P9_37; //analog input for right IR sensor 11 | 12 | var left_motor_pin = pins.P9_14; //PWM pin for left motor 13 | var right_motor_pin = pins.P9_16; //PWM pin for right motor 14 | 15 | var left_sensor_value; 16 | var center_sensor_value; 17 | var right_sensor_value; 18 | 19 | b.pinMode(left_motor_pin, b.OUTPUT); //left motor pin 20 | b.pinMode(right_motor_pin, b.OUTPUT);//right motor pin 21 | 22 | while(1) 23 | { 24 | //read analog output from IR sensors 25 | //normalized value ranges from 0..1 26 | left_sensor_value = b.analogRead(left_IR_sensor); 27 | center_sensor_value = b.analogRead(center_IR_sensor); 28 | right_sensor_value = b.analogRead(right_IR_sensor); 29 | 30 | //assumes desired threshold at 31 | //1.25 VDC with max value of 1.75 VDC 32 | if((left_sensor_value > 0.714)&& 33 | (center_sensor_value <= 0.714)&& 34 | (right_sensor_value > 0.714)) 35 | { //robot continues straight ahead 36 | b.analogWrite(left_motor_pin, 0.7); 37 | b.analogWrite(right_motor_pin, 0.7); 38 | } 39 | /* 40 | else if(...) 41 | { 42 | : 43 | : 44 | : 45 | } 46 | */ 47 | } 48 | 49 | //*********************************************************************** 50 | 51 | -------------------------------------------------------------------------------- /chapter-2/next.js: -------------------------------------------------------------------------------- 1 | //****************************************************************** 2 | // next.js - A pattern for calling sequential functions in node.js 3 | //****************************************************************** 4 | 5 | catch_events(); 6 | mysteps(done); 7 | console.log("other tasks can continue"); 8 | 9 | //****************************************************************** 10 | //done 11 | //****************************************************************** 12 | 13 | function done() { 14 | console.log("done"); 15 | } 16 | 17 | //****************************************************************** 18 | //catch_events 19 | //****************************************************************** 20 | 21 | function catch_events() 22 | { 23 | process.on('SIGINT', got_interrupt); //Capture ^C key presses 24 | function got_interrupt() 25 | { 26 | console.log('got interrupt'); 27 | } 28 | } 29 | 30 | //****************************************************************** 31 | //mysteps 32 | //****************************************************************** 33 | 34 | function mysteps(callback) 35 | { 36 | //Provide a list of functions to call 37 | var steps = 38 | [ 39 | function(){ console.log("i = " + i); setTimeout(next, 1000); }, //delay 1s 40 | function(){ console.log("i = " + i); setTimeout(next, 1000); }, //delay 1s 41 | function(){ console.log("i = " + i); setTimeout(next, 1000); }, //delay 1s 42 | function(){ console.log("i = " + i); setTimeout(next, 1000); }, //delay 1s 43 | function(){ console.log("i = " + i); setTimeout(next, 1000); }, //delay 1s 44 | function(){ callback(); } 45 | ]; 46 | 47 | //Start at 0 48 | var i = 0; 49 | 50 | next(); //Call the first function 51 | 52 | //Nested helper function to call the next function in 'steps' 53 | function next() 54 | { 55 | i++; 56 | steps[i-1](); 57 | } 58 | } 59 | 60 | //****************************************************************** 61 | -------------------------------------------------------------------------------- /chapter-5/am335x-boneblack.dts: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/ 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 as 6 | * published by the Free Software Foundation. 7 | */ 8 | /dts-v1/; 9 | 10 | /include/ "am33xx.dtsi" 11 | 12 | /include/ "am335x-bone-common.dtsi" 13 | 14 | &userled_pins { 15 | pinctrl-single,pins = < 16 | 0x54 0x7 /* gpmc_a5.gpio1_21, OUTPUT | MODE7 */ 17 | 0x58 0x17 /* gpmc_a6.gpio1_22, OUTPUT_PULLUP | MODE7 */ 18 | 0x5c 0x7 /* gpmc_a7.gpio1_23, OUTPUT | MODE7 */ 19 | 0x60 0x17 /* gpmc_a8.gpio1_24, OUTPUT_PULLUP | MODE7 */ 20 | 0x00c 0x31 /* P8_6 gpmc_ad3.mmc1_dat1 PIN_INPUT_PULLUP | OMAP_MUX_MODE1 */ 21 | 0x008 0x31 /* P8_5 gpmc_ad2.mmc1_dat2 PIN_INPUT_PULLUP | OMAP_MUX_MODE1 */ 22 | 0x004 0x31 /* P8_24 gpmc_ad1.mmc1_dat1 PIN_INPUT_PULLUP | OMAP_MUX_MODE1 */ 23 | 0x000 0x31 /* P8_25 gpmc_ad0.mmc1_dat0 PIN_INPUT_PULLUP | OMAP_MUX_MODE1 */ 24 | 0x084 0x32 /* P8_20 gpmc_csn2.mmc1_cmd OMAP_MUX_MODE2 | AM33XX_PIN_INPUT_PULLUP} */ 25 | 0x080 0x32 /* P8_21 gpmc_csn1.immc1_clk OMAP_MUX_MODE2 | AM33XX_PIN_INPUT_PULLUP} */ 26 | >; 27 | }; 28 | 29 | &ldo3_reg { 30 | regulator-min-microvolt = <1800000>; 31 | regulator-max-microvolt = <1800000>; 32 | regulator-always-on; 33 | }; 34 | 35 | &mmc1 { 36 | vmmc-supply = <&vmmcsd_fixed>; 37 | }; 38 | 39 | &mmc2 { 40 | vmmc-supply = <&vmmcsd_fixed>; 41 | bus-width = <8>; 42 | ti,non-removable; 43 | status = "okay"; 44 | }; 45 | 46 | 47 | &cpu { 48 | /* 49 | * To consider voltage drop between PMIC and SoC, 50 | * tolerance value is reduced to 2% from 4% and 51 | * voltage value is increased as a precaution. 52 | */ 53 | operating-points = < 54 | /* kHz uV */ 55 | 1000000 1350000 56 | 800000 1300000 57 | 600000 1112000 58 | 300000 969000 59 | >; 60 | }; 61 | 62 | -------------------------------------------------------------------------------- /chapter-6/sns-black.cpp: -------------------------------------------------------------------------------- 1 | //********************************************************************* 2 | //sns.cpp - Speak and Spell 3 | // - prompts user for input 4 | // - prints input to screen 5 | // - provides spoken input via speech synthesis chip connected 6 | // to uart1 7 | // - configures BeagleBone uart1 for tranmission and 9600 Baud 8 | // 9 | //Requires use of the 'uart1.dts' from Chapter 5 10 | //********************************************************************* 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | int main(void) 24 | { 25 | //define file handle for uart1 26 | FILE *ofp_uart1_tx, *ofp_uart1_rx; 27 | 28 | //uart1 configuration using termios 29 | termios uart1; 30 | int fd; 31 | 32 | //open uart1 for tx/rx, not controlling device 33 | if((fd = open("/dev/ttyO1", O_RDWR | O_NOCTTY)) < 0) 34 | printf("Unable to open uart1 access.\n"); 35 | 36 | //get attributes of uart1 37 | if(tcgetattr(fd, &uart1) < 0) 38 | printf("Could not get attributes of UART1 at ttyO1\n"); 39 | 40 | //set Baud rate 41 | if(cfsetospeed(&uart1, B9600) < 0) 42 | printf("Could not set baud rate\n"); 43 | else 44 | printf("Baud rate: 9600\n"); 45 | 46 | //set attributes of uart1 47 | uart1.c_iflag = 0; 48 | uart1.c_oflag = 0; 49 | uart1.c_lflag = 0; 50 | tcsetattr(fd, TCSANOW, &uart1); 51 | 52 | char byte_out[20]; 53 | 54 | //set ASCII charater G repeatedly 55 | while(1) 56 | { 57 | printf("Enter letter, word, statement. Press [Enter].\n\n"); 58 | scanf("%s", byte_out); 59 | printf("%s\n\n\n", byte_out); 60 | write(fd, byte_out, strlen(byte_out)+1); 61 | } 62 | 63 | close(fd); 64 | } 65 | //********************************************************************* 66 | 67 | -------------------------------------------------------------------------------- /chapter-5/led1.cpp: -------------------------------------------------------------------------------- 1 | //******************************************************************* 2 | //led1.cpp: illuminates an LED connected to expansion header P8, pin3 3 | //(GPIO1_6 designated as gpio38) 4 | //******************************************************************* 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | #define output "out" 11 | #define input "in" 12 | 13 | int main (void) 14 | { 15 | //define file handles 16 | FILE *ofp_gpmc_ad6, *ofp_export, *ofp_gpio38_value, *ofp_gpio38_direction; 17 | 18 | //define pin variables 19 | int mux_mode = 0x007, pin_number = 38, logic_status = 1; 20 | char* pin_direction = output; 21 | 22 | //configure the mux mode 23 | ofp_gpmc_ad6 = fopen("/sys/kernel/debug/omap_mux/gpmc_ad6", "w"); 24 | if(ofp_gpmc_ad6 == NULL) {printf("Unable to open gpmc_ad6.\n");} 25 | fseek(ofp_gpmc_ad6, 0, SEEK_SET); 26 | fprintf(ofp_gpmc_ad6, "0x%02x", mux_mode); 27 | fflush(ofp_gpmc_ad6); 28 | 29 | //establish a direction and value file within export for gpio38 30 | ofp_export = fopen("/sys/class/gpio/export", "w"); 31 | if(ofp_export == NULL) {printf("Unable to open export.\n");} 32 | fseek(ofp_export, 0, SEEK_SET); 33 | fprintf(ofp_export, "%d", pin_number); 34 | fflush(ofp_export); 35 | 36 | //configure gpio38 for writing 37 | ofp_gpio38_direction = fopen("/sys/class/gpio/gpio38/direction", "w"); 38 | if(ofp_gpio38_direction == NULL) {printf("Unable to open gpio38_direction.\n");} 39 | fseek(ofp_gpio38_direction, 0, SEEK_SET); 40 | fprintf(ofp_gpio38_direction, "%s", pin_direction); 41 | fflush(ofp_gpio38_direction); 42 | 43 | //write a logic 1 to gpio38 to illuminate the LED 44 | ofp_gpio38_value = fopen("/sys/class/gpio/gpio38/value", "w"); 45 | if(ofp_gpio38_value == NULL) {printf("Unable to open gpio38_value.\n");} 46 | fseek(ofp_gpio38_value, 0, SEEK_SET); 47 | fprintf(ofp_gpio38_value, "%d", logic_status); 48 | fflush(ofp_gpio38_value); 49 | 50 | //close all files 51 | fclose(ofp_gpmc_ad6); 52 | fclose(ofp_export); 53 | fclose(ofp_gpio38_direction); 54 | fclose(ofp_gpio38_value); 55 | return 1; 56 | } 57 | //******************************************************************** 58 | 59 | -------------------------------------------------------------------------------- /chapter-5/uart1-black.cpp: -------------------------------------------------------------------------------- 1 | //********************************************************************* 2 | //uart1.cpp - configures BeagleBone uart1 for tranmission and 9600 Baud 3 | //and repeatedly sends the character G via uart1 tx pin (P9, 24) 4 | // 5 | //To configure the UART first do: 6 | // # dtc -O dtb -o uart1-00A0.dtbo -b 0 -@ uart1.dts 7 | // # cp uart1-00A0.dtbo /lib/firmware/uart1-00A0.dtbo 8 | // # echo uart1 > /sys/devices/bone_capemgr.8/slots 9 | // 10 | //Check that the pinmux has been configured via: 11 | // # cat /sys/kernel/debug/pinctrl/44e10800.pinmux/pinmux-pins 12 | // pin 96 (44e10980): serial1_pinmux_helper.14 (GPIO UNCLAIMED) function pinmux_serial1_pins group pinmux_serial1_pins 13 | // pin 97 (44e10984): serial1_pinmux_helper.14 (GPIO UNCLAIMED) function pinmux_serial1_pins group pinmux_serial1_pins 14 | // 15 | //********************************************************************* 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | int main(void) 28 | { 29 | //define file handle for uart1 30 | FILE *ofp_uart1_tx, *ofp_uart1_rx; 31 | 32 | //uart1 configuration using termios 33 | termios uart1; 34 | int fd; 35 | 36 | //open uart1 for tx/rx, not controlling device 37 | if((fd = open("/dev/ttyO1", O_RDWR | O_NOCTTY)) < 0) 38 | printf("Unable to open uart1 access.\n"); 39 | 40 | //get attributes of uart1 41 | if(tcgetattr(fd, &uart1) < 0) 42 | printf("Could not get attributes of UART1 at ttyO1\n"); 43 | 44 | //set Baud rate 45 | if(cfsetospeed(&uart1, B9600) < 0) 46 | printf("Could not set baud rate\n"); 47 | else 48 | printf("Baud rate: 9600\n"); 49 | 50 | //set attributes of uart1 51 | uart1.c_iflag = 0; 52 | uart1.c_oflag = 0; 53 | uart1.c_lflag = 0; 54 | tcsetattr(fd, TCSANOW, &uart1); 55 | 56 | char byte_out[] = {0x47}; 57 | 58 | //set ASCII charater G repeatedly 59 | while(1) 60 | { 61 | write(fd, byte_out, strlen(byte_out)+1); 62 | } 63 | 64 | close(fd); 65 | } 66 | 67 | //*********************************************************** 68 | 69 | -------------------------------------------------------------------------------- /chapter-5/pwm.cpp: -------------------------------------------------------------------------------- 1 | //********************************************************************** 2 | #include 3 | #include 4 | #include 5 | 6 | #define output "out" 7 | #define input "in" 8 | 9 | int main (void) 10 | { 11 | //define file handles 12 | FILE *ofp_gpmc_a2, *pwm_freq, *pwm_req, *pwm_duty, *pwm_polarity, *pwm_run; 13 | 14 | //define pin variables 15 | int mux_mode = 0x006, request = 1, freq = 100, duty = 50, polarity = 1, run =1; 16 | 17 | ofp_gpmc_a2 = fopen("/sys/kernel/debug/omap_mux/gpmc_a2", "w"); 18 | if(ofp_gpmc_a2 == NULL) {printf("Unable to open gpmc_a2.\n");} 19 | fseek(ofp_gpmc_a2, 0, SEEK_SET); 20 | fprintf(ofp_gpmc_a2, "0x%02x", mux_mode); 21 | fflush(ofp_gpmc_a2); 22 | 23 | pwm_req = fopen("/sys/class/pwm/ehrpwm.1:0/request", "w"); 24 | if(pwm_req == NULL) {printf("Unable to open pwm request.\n");} 25 | fseek(pwm_req, 0, SEEK_SET); 26 | fprintf(pwm_req, "%d", request); 27 | fflush(pwm_req); 28 | 29 | pwm_freq = fopen("/sys/class/pwm/ehrpwm.1:0/period_freq", "w"); 30 | if(pwm_freq == NULL) {printf("Unable to open pwm frequency.\n");} 31 | fseek(pwm_freq, 0, SEEK_SET); 32 | fprintf(pwm_freq, "%d", freq); 33 | fflush(pwm_freq); 34 | 35 | pwm_duty = fopen("/sys/class/pwm/ehrpwm.1:0/duty_percent", "w"); 36 | if(pwm_duty == NULL) {printf("Unable to open pwm duty cycle.\n");} 37 | fseek(pwm_duty, 0, SEEK_SET); 38 | fprintf(pwm_duty, "%d", duty); 39 | fflush(pwm_duty); 40 | 41 | pwm_polarity = fopen("/sys/class/pwm/ehrpwm.1:0/polarity", "w"); 42 | if(pwm_polarity == NULL) {printf("Unable to open pwm polarity.\n");} 43 | fseek(pwm_polarity, 0, SEEK_SET); 44 | fprintf(pwm_polarity, "%d", polarity); 45 | fflush(pwm_polarity); 46 | 47 | pwm_run = fopen("/sys/class/pwm/ehrpwm.1:0/run", "w"); 48 | if(pwm_run == NULL) {printf("Unable to open pwm run.\n");} 49 | fseek(pwm_run, 0, SEEK_SET); 50 | fprintf(pwm_run, "%d", run); 51 | fflush(pwm_run); 52 | 53 | while(1) 54 | { 55 | 56 | } 57 | 58 | fclose(ofp_gpmc_a2); 59 | fclose(pwm_req); 60 | fclose(pwm_freq); 61 | fclose(pwm_duty); 62 | fclose(pwm_polarity); 63 | fclose(pwm_run); 64 | 65 | return 1; 66 | } 67 | //********************************************************************** 68 | 69 | -------------------------------------------------------------------------------- /chapter-5/led3-black.cpp: -------------------------------------------------------------------------------- 1 | //********************************************************************* 2 | //led3.cpp: this programs toggles (flashes) an LED connected to 3 | //expansion header P8, pin 13 (GPIO1_12 designated as gpio44) at five 4 | //second intervals. 5 | //********************************************************************* 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #define output "out" 12 | #define input "in" 13 | 14 | int main (void) 15 | { 16 | //define file handles 17 | FILE *ofp_export, *ofp_gpio44_value, *ofp_gpio44_direction; 18 | 19 | //define pin variables 20 | int pin_number = 44, logic_status = 1; 21 | char* pin_direction = output; 22 | 23 | //time parameters 24 | time_t now, later; 25 | 26 | ofp_export = fopen("/sys/class/gpio/export", "w"); 27 | if(ofp_export == NULL) {printf("Unable to open export.\n");} 28 | fseek(ofp_export, 0, SEEK_SET); 29 | fprintf(ofp_export, "%d", pin_number); 30 | fflush(ofp_export); 31 | 32 | ofp_gpio44_direction = fopen("/sys/class/gpio/gpio44/direction", "w"); 33 | if(ofp_gpio44_direction == NULL) {printf("Unable to open gpio44_direction.\n");} 34 | fseek(ofp_gpio44_direction, 0, SEEK_SET); 35 | fprintf(ofp_gpio44_direction, "%s", pin_direction); 36 | fflush(ofp_gpio44_direction); 37 | 38 | ofp_gpio44_value = fopen("/sys/class/gpio/gpio44/value", "w"); 39 | if(ofp_gpio44_value == NULL) {printf("Unable to open gpio44_value.\n");} 40 | fseek(ofp_gpio44_value, 0, SEEK_SET); 41 | logic_status = 1; 42 | fprintf(ofp_gpio44_value, "%d", logic_status); 43 | fflush(ofp_gpio44_value); 44 | 45 | now = time(NULL); 46 | later = time(NULL); 47 | 48 | while(1) 49 | { 50 | while(difftime(later, now) < 5.0) 51 | { 52 | later = time(NULL); //keep checking time 53 | } 54 | if(logic_status == 1) logic_status = 0; 55 | else logic_status = 1; 56 | //write to gpio44 57 | fprintf(ofp_gpio44_value, "%d", logic_status); 58 | fflush(ofp_gpio44_value); 59 | now=time(NULL); 60 | later=time(NULL); 61 | } 62 | fclose(ofp_export); 63 | fclose(ofp_gpio44_direction); 64 | fclose(ofp_gpio44_value); 65 | return 1; 66 | } 67 | 68 | //********************************************************************* 69 | 70 | -------------------------------------------------------------------------------- /chapter-5/pwm-black.cpp: -------------------------------------------------------------------------------- 1 | //********************************************************************** 2 | //pwm.cpp 3 | // 4 | //This uses PWM to output a "fade" effect to P9_14 5 | // 6 | //Before use, you must: 7 | // echo bone_pwm_P9_14 > /sys/devices/bone_capemgr.*/slots 8 | // 9 | //Note that in /sys/devices/ocp.2/pwm_test_P9_14.12 10 | // the .2 after ocp and the .12 after pwm_test_P9_14 both might change 11 | //********************************************************************** 12 | #include 13 | #include 14 | #include 15 | 16 | #define output "out" 17 | #define input "in" 18 | 19 | int main (void) 20 | { 21 | //define file handles 22 | FILE *pwm_period, *pwm_duty, *pwm_polarity, *pwm_run; 23 | 24 | //define pin variables 25 | int period = 500000, duty = 250000, polarity = 1, run = 1; 26 | int increment = 1; 27 | 28 | pwm_period = fopen("/sys/devices/ocp.2/pwm_test_P9_14.12/period", "w"); 29 | if(pwm_period == NULL) {printf("Unable to open pwm period.\n");} 30 | fseek(pwm_period, 0, SEEK_SET); 31 | fprintf(pwm_period, "%d", period); 32 | fflush(pwm_period); 33 | 34 | pwm_duty = fopen("/sys/devices/ocp.2/pwm_test_P9_14.12/duty", "w"); 35 | if(pwm_duty == NULL) {printf("Unable to open pwm duty cycle.\n");} 36 | fseek(pwm_duty, 0, SEEK_SET); 37 | fprintf(pwm_duty, "%d", duty); 38 | fflush(pwm_duty); 39 | 40 | pwm_polarity = fopen("/sys/devices/ocp.2/pwm_test_P9_14.12/polarity", "w"); 41 | if(pwm_polarity == NULL) {printf("Unable to open pwm polarity.\n");} 42 | fseek(pwm_polarity, 0, SEEK_SET); 43 | fprintf(pwm_polarity, "%d", polarity); 44 | fflush(pwm_polarity); 45 | 46 | pwm_run = fopen("/sys/devices/ocp.2/pwm_test_P9_14.12/run", "w"); 47 | if(pwm_run == NULL) {printf("Unable to open pwm run.\n");} 48 | fseek(pwm_run, 0, SEEK_SET); 49 | fprintf(pwm_run, "%d", run); 50 | fflush(pwm_run); 51 | 52 | while(1) 53 | { 54 | if(duty >= period) increment = -1; 55 | else if(duty <= 0) increment = 1; 56 | duty += increment; 57 | fseek(pwm_duty, 0, SEEK_SET); 58 | fprintf(pwm_duty, "%d", duty); 59 | fflush(pwm_duty); 60 | } 61 | 62 | fclose(pwm_period); 63 | fclose(pwm_duty); 64 | fclose(pwm_polarity); 65 | fclose(pwm_run); 66 | 67 | return 1; 68 | } 69 | //********************************************************************** 70 | 71 | -------------------------------------------------------------------------------- /chapter-2/almanzas.js: -------------------------------------------------------------------------------- 1 | //****************************************************************** 2 | // almanzas.js 3 | //****************************************************************** 4 | 5 | var counter = 1; 6 | var driveup = 1; 7 | 8 | setTimeout(counterOrder, 1000);//Counter customer at second 1 9 | setTimeout(counterOrder, 2000);//Counter customer at second 2 10 | setTimeout(driveupOrder, 3000);//Driveup customer at second 3 11 | setTimeout(counterOrder, 4000);//Counter customer at second 4 12 | setTimeout(counterOrder, 5000);//Counter customer at second 5 13 | setTimeout(driveupOrder, 6000);//Driveup customer at second 6 14 | setTimeout(driveupOrder, 7000);//Driveup customer at second 7 15 | setTimeout(counterOrder, 8000);//Counter customer at second 8 16 | 17 | //****************************************************************** 18 | //server(task) 19 | // 20 | //The server takes the orders from the customers and gives them 21 | //to the kitchen, as well as delivering orders from the kitchen 22 | //****************************************************************** 23 | 24 | function server(customer) 25 | { 26 | console.log("Take order " + customer); 27 | kitchen(deliver); 28 | 29 | function deliver() 30 | { 31 | console.log("Deliver " + customer + " food order"); 32 | } 33 | } 34 | 35 | //****************************************************************** 36 | //kitchen(callback) 37 | // 38 | //The kitchen takes tasks and completes them at a random time 39 | //between 1 and 5 seconds 40 | //****************************************************************** 41 | 42 | function kitchen(callback) 43 | { 44 | var delay = 1000 + Math.round(Math.random() * 4000); 45 | setTimeout(callback, delay); 46 | } 47 | 48 | //****************************************************************** 49 | //counterOrder() 50 | // 51 | //Give order to server 52 | //****************************************************************** 53 | 54 | function counterOrder() 55 | { 56 | server("C" + counter); 57 | counter++; 58 | } 59 | 60 | //****************************************************************** 61 | //driveupOrder() 62 | // 63 | //Give order to server 64 | //****************************************************************** 65 | 66 | function driveupOrder() 67 | { 68 | server("A" + driveup); 69 | driveup++; 70 | } 71 | 72 | -------------------------------------------------------------------------------- /chapter-5/uart1.cpp: -------------------------------------------------------------------------------- 1 | //********************************************************************* 2 | //uart1.cpp - configures BeagleBone uart1 for tranmission and 9600 Baud 3 | //and repeatedly sends the character G via uart1 tx pin (P9, 24) 4 | //********************************************************************* 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | int main(void) 17 | { 18 | //define file handle for uart1 19 | FILE *ofp_uart1_tx, *ofp_uart1_rx; 20 | 21 | //define mux mode for uart1 tx 22 | int mux_mode_uart1_tx = 0X8000; 23 | int mux_mode_uart1_rx = 0X8020; 24 | 25 | //configure uart1 for transmission 26 | ofp_uart1_tx = fopen("/sys/kernel/debug/omap_mux/uart1_txd", "w"); 27 | if(ofp_uart1_tx == NULL) printf("Unable to open uart1 tx file.\n"); 28 | fseek(ofp_uart1_tx, 0, SEEK_SET); 29 | fprintf(ofp_uart1_tx, "0x%2x", mux_mode_uart1_tx); 30 | fflush(ofp_uart1_tx); 31 | 32 | //configure uart1 for reception 33 | ofp_uart1_rx = fopen("/sys/kernel/debug/omap_mux/uart1_rxd", "w"); 34 | if(ofp_uart1_rx == NULL) printf("Unable to open uart1 rx file.\n"); 35 | fseek(ofp_uart1_rx, 0, SEEK_SET); 36 | fprintf(ofp_uart1_rx, "0x%2x", mux_mode_uart1_rx); 37 | fflush(ofp_uart1_rx); 38 | 39 | //uart1 configuration using termios 40 | termios uart1; 41 | int fd; 42 | 43 | //open uart1 for tx/rx, not controlling device 44 | if((fd = open("/dev/ttyO1", O_RDWR | O_NOCTTY)) < 0) 45 | printf("Unable to open uart1 access.\n"); 46 | 47 | //get attributes of uart1 48 | if(tcgetattr(fd, &uart1) < 0) 49 | printf("Could not get attributes of UART1 at ttyO1\n"); 50 | 51 | //set Baud rate 52 | if(cfsetospeed(&uart1, B9600) < 0) 53 | printf("Could not set baud rate\n"); 54 | else 55 | printf("Baud rate: 9600\n"); 56 | 57 | //set attributes of uart1 58 | uart1.c_iflag = 0; 59 | uart1.c_oflag = 0; 60 | uart1.c_lflag = 0; 61 | tcsetattr(fd, TCSANOW, &uart1); 62 | 63 | char byte_out[] = {0x47}; 64 | 65 | //set ASCII charater G repeatedly 66 | while(1) 67 | { 68 | write(fd, byte_out, strlen(byte_out)+1); 69 | } 70 | 71 | close(fd); 72 | } 73 | 74 | //*********************************************************** 75 | 76 | -------------------------------------------------------------------------------- /chapter-4/4Wrobot.js: -------------------------------------------------------------------------------- 1 | //*********************************************************************** 2 | var b = require('bonescript'); 3 | 4 | 5 | //Old bonescript defines 'bone' globally 6 | var pins = (typeof bone != 'undefined') ? bone : b.bone.pins; 7 | 8 | var left_IR_sensor = pins.P9_39; //analog input for left IR sensor 9 | var center_IR_sensor = pins.P9_40; //analog input for center IR sensor 10 | var right_IR_sensor = pins.P9_37; //analog input for right IR sensor 11 | 12 | var IMU_xout = pins.P9_35; //IMU xout analog signal 13 | var IMU_yout = pins.P9_36; //IMU yout analog signal 14 | 15 | var left_motor_pin = pins.P9_14; //PWM pin for left motor 16 | var right_motor_pin = pins.P9_16; //PWM pin for right motor 17 | var left_motor_for_rev = pins.P9_13; //left motor forward/reverse 18 | var right_motor_for_rev= pins.P9_15; //right motor forward/reverse 19 | 20 | var left_sensor_value; 21 | var center_sensor_value; 22 | var right_sensor_value; 23 | 24 | 25 | b.pinMode(left_motor_pin, b.OUTPUT); //set pin to digital output 26 | b.pinMode(right_motor_pin, b.OUTPUT); //set pin to digital output 27 | b.pinMode(left_motor_for_rev, b.OUTPUT); //set pin to digital output 28 | b.pinMode(right_motor_for_rev, b.OUTPUT); //set pin to digital output 29 | 30 | while(1) 31 | { 32 | //read analog output from IR sensors 33 | //normalized value ranges from 0..1 34 | left_sensor_value = b.analogRead(left_IR_sensor); 35 | center_sensor_value = b.analogRead(center_IR_sensor); 36 | right_sensor_value = b.analogRead(right_IR_sensor); 37 | 38 | //assumes desired threshold at 39 | //1.25 VDC with max value of 1.75 VDC 40 | if((left_sensor_value > 0.714)&& 41 | (center_sensor_value <= 0.714)&& 42 | (right_sensor_value > 0.714)) 43 | { //robot straight ahead 44 | b.digitalWrite(left_motor_for_rev, b.HIGH); //left motor forward 45 | b.analogWrite(left_motor_pin, 0.7); //left motor RPM 46 | b.digitalWrite(right_motor_for_rev, b.HIGH);//right motor forward 47 | b.analogWrite(right_motor_pin, 0.7); //right motor RPM 48 | } 49 | /* 50 | else if(...) 51 | { 52 | : 53 | : 54 | : 55 | } 56 | */ 57 | } 58 | 59 | //*********************************************************************** 60 | 61 | -------------------------------------------------------------------------------- /chapter-5/led3.cpp: -------------------------------------------------------------------------------- 1 | //********************************************************************* 2 | //led3.cpp: this programs toggles (flashes) an LED connected to 3 | //expansion header P8, pin 3 (GPIO1_6 designated as gpio38) at five 4 | //second intervals. 5 | //********************************************************************* 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #define output "out" 12 | #define input "in" 13 | 14 | int main (void) 15 | { 16 | //define file handles 17 | FILE *ofp_gpm6_ad6, *ofp_export, *ofp_gpio38_value, *ofp_gpio38_direction; 18 | 19 | //define pin variables 20 | int mux_mode = 0x007, pin_number = 38, logic_status = 1; 21 | char* pin_direction = output; 22 | 23 | //time parameters 24 | time_t now, later; 25 | 26 | ofp_gpm6_ad6 = fopen("/sys/kernel/debug/omap_mux/gpmc_ad6", "w"); 27 | if(ofp_gpm6_ad6 == NULL) {printf("Unable to open gpmc_ad6.\n");} 28 | fseek(ofp_gpm6_ad6, 0, SEEK_SET); 29 | fprintf(ofp_gpm6_ad6, "0x%02x", mux_mode); 30 | fflush(ofp_gpm6_ad6); 31 | 32 | ofp_export = fopen("/sys/class/gpio/export", "w"); 33 | if(ofp_export == NULL) {printf("Unable to open export.\n");} 34 | fseek(ofp_export, 0, SEEK_SET); 35 | fprintf(ofp_export, "%d", pin_number); 36 | fflush(ofp_export); 37 | 38 | ofp_gpio38_direction = fopen("/sys/class/gpio/gpio38/direction", "w"); 39 | if(ofp_gpio38_direction == NULL) {printf("Unable to open gpio38_direction.\n");} 40 | fseek(ofp_gpio38_direction, 0, SEEK_SET); 41 | fprintf(ofp_gpio38_direction, "%s", pin_direction); 42 | fflush(ofp_gpio38_direction); 43 | 44 | ofp_gpio38_value = fopen("/sys/class/gpio/gpio38/value", "w"); 45 | if(ofp_gpio38_value == NULL) {printf("Unable to open gpio38_value.\n");} 46 | fseek(ofp_gpio38_value, 0, SEEK_SET); 47 | logic_status = 1; 48 | fprintf(ofp_gpio38_value, "%d", logic_status); 49 | fflush(ofp_gpio38_value); 50 | 51 | now = time(NULL); 52 | later = time(NULL); 53 | 54 | while(1) 55 | { 56 | while(difftime(later, now) < 5.0) 57 | { 58 | later = time(NULL); //keep checking time 59 | } 60 | if(logic_status == 1) logic_status = 0; 61 | else logic_status = 1; 62 | //write to gpio38 63 | fprintf(ofp_gpio38_value, "%d", logic_status); 64 | fflush(ofp_gpio38_value); 65 | now=time(NULL); 66 | later=time(NULL); 67 | } 68 | fclose(ofp_gpm6_ad6); 69 | fclose(ofp_export); 70 | fclose(ofp_gpio38_direction); 71 | fclose(ofp_gpio38_value); 72 | return 1; 73 | } 74 | 75 | //********************************************************************* 76 | 77 | -------------------------------------------------------------------------------- /chapter-6/sns.cpp: -------------------------------------------------------------------------------- 1 | //********************************************************************* 2 | //********************************************************************* 3 | //sns.cpp - Speak and Spell 4 | // - prompts user for input 5 | // - prints input to screen 6 | // - provides spoken input via speech synthesis chip connected 7 | // to uart1 8 | // - configures BeagleBone uart1 for tranmission and 9600 Baud 9 | // 10 | //********************************************************************* 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | int main(void) 24 | { 25 | //define file handle for uart1 26 | FILE *ofp_uart1_tx, *ofp_uart1_rx; 27 | 28 | //define mux mode for uart1 tx 29 | int mux_mode_uart1_tx = 0X8000; 30 | int mux_mode_uart1_rx = 0X8020; 31 | 32 | //configure uart1 for transmission 33 | ofp_uart1_tx = fopen("/sys/kernel/debug/omap_mux/uart1_txd", "w"); 34 | if(ofp_uart1_tx == NULL) printf("Unable to open uart1 tx file.\n"); 35 | fseek(ofp_uart1_tx, 0, SEEK_SET); 36 | fprintf(ofp_uart1_tx, "0x%2x", mux_mode_uart1_tx); 37 | fflush(ofp_uart1_tx); 38 | 39 | //configure uart1 for transmission 40 | ofp_uart1_rx = fopen("/sys/kernel/debug/omap_mux/uart1_rxd", "w"); 41 | if(ofp_uart1_rx == NULL) printf("Unable to open uart1 rx file.\n"); 42 | fseek(ofp_uart1_rx, 0, SEEK_SET); 43 | fprintf(ofp_uart1_rx, "0x%2x", mux_mode_uart1_rx); 44 | fflush(ofp_uart1_rx); 45 | 46 | //uart1 configuration using termios 47 | termios uart1; 48 | int fd; 49 | 50 | //open uart1 for tx/rx, not controlling device 51 | if((fd = open("/dev/ttyO1", O_RDWR | O_NOCTTY)) < 0) 52 | printf("Unable to open uart1 access.\n"); 53 | 54 | //get attributes of uart1 55 | if(tcgetattr(fd, &uart1) < 0) 56 | printf("Could not get attributes of UART1 at ttyO1\n"); 57 | 58 | //set Baud rate 59 | if(cfsetospeed(&uart1, B9600) < 0) 60 | printf("Could not set baud rate\n"); 61 | else 62 | printf("Baud rate: 9600\n"); 63 | 64 | //set attributes of uart1 65 | uart1.c_iflag = 0; 66 | uart1.c_oflag = 0; 67 | uart1.c_lflag = 0; 68 | tcsetattr(fd, TCSANOW, &uart1); 69 | 70 | char byte_out[20]; 71 | 72 | //set ASCII charater G repeatedly 73 | while(1) 74 | { 75 | printf("Enter letter, word, statement. Press [Enter].\n\n"); 76 | scanf("%s", byte_out); 77 | printf("%s\n\n\n", byte_out); 78 | write(fd, byte_out, strlen(byte_out)+1); 79 | } 80 | 81 | close(fd); 82 | } 83 | //********************************************************************* 84 | 85 | -------------------------------------------------------------------------------- /chapter-5/sq_wave-black.cpp: -------------------------------------------------------------------------------- 1 | //****************************************************************** 2 | //sq_wave: generates a 100 Hz, 50% duty cycle signal on header 3 | //P8, pin 13 (GPIO1_12 designated as gpio44). 4 | //****************************************************************** 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define output "out" 12 | #define input "in" 13 | 14 | void delay_us(int); 15 | 16 | int main (void) 17 | { 18 | //define file handles 19 | FILE *ofp_export, *ofp_gpio44_value, *ofp_gpio44_direction; 20 | 21 | //define pin variables 22 | int pin_number = 44, logic_status = 1; 23 | char* pin_direction = output; 24 | 25 | ofp_export = fopen("/sys/class/gpio/export", "w"); 26 | if(ofp_export == NULL) {printf("Unable to open export.\n");} 27 | fseek(ofp_export, 0, SEEK_SET); 28 | fprintf(ofp_export, "%d", pin_number); 29 | fflush(ofp_export); 30 | 31 | ofp_gpio44_direction = fopen("/sys/class/gpio/gpio44/direction", "w"); 32 | if(ofp_gpio44_direction == NULL) {printf("Unable to open gpio44_direction.\n");} 33 | fseek(ofp_gpio44_direction, 0, SEEK_SET); 34 | fprintf(ofp_gpio44_direction, "%s", pin_direction); 35 | fflush(ofp_gpio44_direction); 36 | 37 | ofp_gpio44_value = fopen("/sys/class/gpio/gpio44/value", "w"); 38 | if(ofp_gpio44_value == NULL) {printf("Unable to open gpio44_value.\n");} 39 | fseek(ofp_gpio44_value, 0, SEEK_SET); 40 | logic_status = 1; 41 | fprintf(ofp_gpio44_value, "%d", logic_status); 42 | fflush(ofp_gpio44_value); 43 | 44 | 45 | while(1) 46 | { 47 | delay_us(5000); 48 | if(logic_status == 1) logic_status = 0; 49 | else logic_status = 1; 50 | //write to gpio44 51 | fprintf(ofp_gpio44_value, "%d", logic_status); 52 | fflush(ofp_gpio44_value); 53 | } 54 | fclose(ofp_export); 55 | fclose(ofp_gpio44_direction); 56 | fclose(ofp_gpio44_value); 57 | return 1; 58 | } 59 | 60 | 61 | //****************************************************************** 62 | 63 | void delay_us(int desired_delay_us) 64 | { 65 | struct timeval tv_start; //start time hack 66 | struct timeval tv_now; //current time hack 67 | int elapsed_time_us; 68 | 69 | gettimeofday(&tv_start, NULL); 70 | elapsed_time_us = 0; 71 | 72 | while(elapsed_time_us < desired_delay_us) 73 | { 74 | gettimeofday(&tv_now, NULL); 75 | if(tv_now.tv_usec >= tv_start.tv_usec) 76 | elapsed_time_us = tv_now.tv_usec - tv_start.tv_usec; 77 | else 78 | elapsed_time_us = (1000000 - tv_start.tv_usec) + tv_now.tv_usec; 79 | //printf("start: %ld \n", tv_start.tv_usec); 80 | //printf("now: %ld \n", tv_now.tv_usec); 81 | //printf("desired: %d \n", desired_delay_ms); 82 | //printf("elapsed: %d \n\n", elapsed_time_ms); 83 | } 84 | } 85 | 86 | //****************************************************************** 87 | 88 | -------------------------------------------------------------------------------- /chapter-5/sq_wave.cpp: -------------------------------------------------------------------------------- 1 | //****************************************************************** 2 | //sq_wave: generates a 100 Hz, 50% duty cycle signal on header 3 | //P8, pin 3 (GPIO1_6 designated as gpio38). 4 | //****************************************************************** 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define output "out" 12 | #define input "in" 13 | 14 | void delay_us(int); 15 | 16 | int main (void) 17 | { 18 | //define file handles 19 | FILE *ofp_gpm6_ad6, *ofp_export, *ofp_gpio38_value, *ofp_gpio38_direction; 20 | 21 | //define pin variables 22 | int mux_mode = 0x007, pin_number = 38, logic_status = 1; 23 | char* pin_direction = output; 24 | 25 | ofp_gpm6_ad6 = fopen("/sys/kernel/debug/omap_mux/gpmc_ad6", "w"); 26 | if(ofp_gpm6_ad6 == NULL) {printf("Unable to open gpmc_ad6.\n");} 27 | fseek(ofp_gpm6_ad6, 0, SEEK_SET); 28 | fprintf(ofp_gpm6_ad6, "0x%02x", mux_mode); 29 | fflush(ofp_gpm6_ad6); 30 | 31 | ofp_export = fopen("/sys/class/gpio/export", "w"); 32 | if(ofp_export == NULL) {printf("Unable to open export.\n");} 33 | fseek(ofp_export, 0, SEEK_SET); 34 | fprintf(ofp_export, "%d", pin_number); 35 | fflush(ofp_export); 36 | 37 | ofp_gpio38_direction = fopen("/sys/class/gpio/gpio38/direction", "w"); 38 | if(ofp_gpio38_direction == NULL) {printf("Unable to open gpio38_direction.\n");} 39 | fseek(ofp_gpio38_direction, 0, SEEK_SET); 40 | fprintf(ofp_gpio38_direction, "%s", pin_direction); 41 | fflush(ofp_gpio38_direction); 42 | 43 | ofp_gpio38_value = fopen("/sys/class/gpio/gpio38/value", "w"); 44 | if(ofp_gpio38_value == NULL) {printf("Unable to open gpio38_value.\n");} 45 | fseek(ofp_gpio38_value, 0, SEEK_SET); 46 | logic_status = 1; 47 | fprintf(ofp_gpio38_value, "%d", logic_status); 48 | fflush(ofp_gpio38_value); 49 | 50 | 51 | while(1) 52 | { 53 | delay_us(5000); 54 | if(logic_status == 1) logic_status = 0; 55 | else logic_status = 1; 56 | //write to gpio38 57 | fprintf(ofp_gpio38_value, "%d", logic_status); 58 | fflush(ofp_gpio38_value); 59 | } 60 | fclose(ofp_gpm6_ad6); 61 | fclose(ofp_export); 62 | fclose(ofp_gpio38_direction); 63 | fclose(ofp_gpio38_value); 64 | return 1; 65 | } 66 | 67 | 68 | //****************************************************************** 69 | 70 | void delay_us(int desired_delay_us) 71 | { 72 | struct timeval tv_start; //start time hack 73 | struct timeval tv_now; //current time hack 74 | int elapsed_time_us; 75 | 76 | gettimeofday(&tv_start, NULL); 77 | elapsed_time_us = 0; 78 | 79 | while(elapsed_time_us < desired_delay_us) 80 | { 81 | gettimeofday(&tv_now, NULL); 82 | if(tv_now.tv_usec >= tv_start.tv_usec) 83 | elapsed_time_us = tv_now.tv_usec - tv_start.tv_usec; 84 | else 85 | elapsed_time_us = (1000000 - tv_start.tv_usec) + tv_now.tv_usec; 86 | //printf("start: %ld \n", tv_start.tv_usec); 87 | //printf("now: %ld \n", tv_now.tv_usec); 88 | //printf("desired: %d \n", desired_delay_ms); 89 | //printf("elapsed: %d \n\n", elapsed_time_ms); 90 | } 91 | } 92 | 93 | //****************************************************************** 94 | 95 | -------------------------------------------------------------------------------- /chapter-4/ROV.js: -------------------------------------------------------------------------------- 1 | //*********************************************************************** 2 | 3 | var b = require('bonescript'); 4 | 5 | //Old bonescript defines 'bone' globally 6 | var pins = (typeof bone != 'undefined') ? bone : b.bone.pins; 7 | 8 | var joystick_horz = pins.P9_39; //joystick horizontal signal 9 | var joystick_vert = pins.P9_40; //joystick vertical signal 10 | var joystick_sel = pins.P8_10; //joystick select button 11 | 12 | var left_motor_pin = pins.P9_14; //PWM pin for left thruster 13 | var right_motor_pin = pins.P9_16; //PWM pin for right thruster 14 | var vertical_motor_pin = pins.P8_13; //output pin for vert thruster 15 | 16 | var left_LED_pin = pins.P8_7; //left LED 17 | var vert_LED_pin = pins.P8_8; //vertical LED 18 | var right_LED_pin = pins.P8_9; //right LED 19 | 20 | var joystick_horizontal; 21 | var joystick_vertical; 22 | var vertical_thrust_on; 23 | 24 | b.pinMode(joystick_sel, b.INPUT); 25 | b.pinMode(vertical_motor_pin, b.OUTPUT); 26 | b.pinMode(left_LED_pin, b.OUTPUT); 27 | b.pinMode(vert_LED_pin, b.OUTPUT); 28 | b.pinMode(right_LED_pin, b.OUTPUT); 29 | 30 | //Read the joystick and react every 500 ms 31 | setInterval(readJoystickAndDrive, 500); 32 | 33 | function readJoystickAndDrive() 34 | { 35 | b.digitalWrite(vert_LED_pin, b.LOW); //de-assert vertical LED 36 | b.digitalWrite(left_LED_pin, b.LOW); //de-assert left LED 37 | b.digitalWrite(right_LED_pin, b.LOW); //de-assert right LED 38 | 39 | joystick_horizontal = b.analogRead(joystick_horz); //read horz position 40 | joystick_vertical = b.analogRead(joystick_vert); //read vert position 41 | vertical_thrust_on = b.digitalRead(joystick_sel); //vertical selected? 42 | 43 | //Determine joystick position and desired thrust PWM relative 44 | //to neutral (0.9 VDC, 0.9 VDC) position 45 | 46 | if(joystick_horizontal > 0.9) 47 | joystick_horizontal = joystick_horizontal - 0.9; 48 | else 49 | joystick_horizontal = 0.9 - joystick_horizontal; 50 | 51 | if(joystick_vertical > 0.9) 52 | joystick_vertical = joystick_vertical - 0.9; 53 | else 54 | joystick_vertical = 0.9 - joystick_vertical; 55 | 56 | //If vertical thruster asserted - dive! 57 | if(vertical_thrust_on) 58 | { 59 | b.digitalWrite(vertical_motor_pin, b.HIGH); //assert vertical thruster 60 | b.digitalWrite(vert_LED_pin, b.HIGH); //assert vertical LED 61 | } 62 | else 63 | { 64 | b.digitalWrite(vertical_motor_pin, b.LOW); //de-assert vert thruster 65 | } 66 | 67 | 68 | //Update left and right thrusters 69 | 70 | //Case 1: 71 | // - Joystick horizontal has positive value 72 | // - Joystick vertical not asserted 73 | // - Move ROV forward 74 | // - Assert right and left thrusters 75 | // - Duty cycle determined by joystick position 76 | if((joystick_horizontal > 0) && (joystick_vertical == 0)) 77 | { 78 | b.analogWrite(left_motor_pin, joystick_horizontal); 79 | b.analogWrite(right_motor_pin, joystick_horizontal); 80 | b.digitalWrite(left_LED_pin, b.HIGH); //assert left LED 81 | b.digitalWrite(right_LED_pin, b.HIGH); //assert right LED 82 | } 83 | 84 | //insert other cases 85 | /* 86 | : 87 | : 88 | : 89 | */ 90 | } 91 | 92 | //*********************************************************************** 93 | -------------------------------------------------------------------------------- /chapter-5/led2-black.cpp: -------------------------------------------------------------------------------- 1 | //********************************************************************* 2 | //led2.cpp: This program reads the logic value of a pushbutton switch 3 | //connected to expansion header P8, pin 7 (GPIO2_2 designated as gpio66). 4 | //If the switch is logic high (1) an LED connected to expansion header 5 | //P8, pin13 (GPIO1_12 designated as gpio44) is illuminated. 6 | //********************************************************************* 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #define output "out" 13 | #define input "in" 14 | 15 | int main (void) 16 | { 17 | //define file handles for gpio44 (P8, pin 13, GPIO1_12) 18 | FILE *ofp_export_44, *ofp_gpio44_value, *ofp_gpio44_direction; 19 | 20 | //define file handles for gpio66 (P8, pin 7, GPIO2_2) 21 | FILE *ofp_export_66, *ifp_gpio66_value, *ofp_gpio66_direction; 22 | 23 | //define pin variables for gpio44 24 | int pin_number_44 = 44, logic_status_44 = 1; 25 | char* pin_direction_44 = output; 26 | 27 | //define pin variables for gpio66 28 | int pin_number_66 = 66, logic_status_66; 29 | char* pin_direction_66 = input; 30 | 31 | //create direction and value file for gpio44 32 | ofp_export_44 = fopen("/sys/class/gpio/export", "w"); 33 | if(ofp_export_44 == NULL) {printf("Unable to open export.\n");} 34 | fseek(ofp_export_44, 0, SEEK_SET); 35 | fprintf(ofp_export_44, "%d", pin_number_44); 36 | fflush(ofp_export_44); 37 | 38 | //create direction and value file for gpio66 39 | ofp_export_66 = fopen("/sys/class/gpio/export", "w"); 40 | if(ofp_export_66 == NULL) {printf("Unable to open export.\n");} 41 | fseek(ofp_export_66, 0, SEEK_SET); 42 | fprintf(ofp_export_66, "%d", pin_number_66); 43 | fflush(ofp_export_66); 44 | 45 | //configure gpio44 direction 46 | ofp_gpio44_direction = fopen("/sys/class/gpio/gpio44/direction", "w"); 47 | if(ofp_gpio44_direction == NULL) {printf("Unable to open gpio44_direction.\n");} 48 | fseek(ofp_gpio44_direction, 0, SEEK_SET); 49 | fprintf(ofp_gpio44_direction, "%s", pin_direction_44); 50 | fflush(ofp_gpio44_direction); 51 | 52 | //configure gpio66 direction 53 | ofp_gpio66_direction = fopen("/sys/class/gpio/gpio66/direction", "w"); 54 | if(ofp_gpio66_direction == NULL) {printf("Unable to open gpio66_direction.\n");} 55 | fseek(ofp_gpio66_direction, 0, SEEK_SET); 56 | fprintf(ofp_gpio66_direction, "%s", pin_direction_66); 57 | fflush(ofp_gpio66_direction); 58 | 59 | //configure gpio44 value \--- initially set logic high 60 | ofp_gpio44_value = fopen("/sys/class/gpio/gpio44/value", "w"); 61 | if(ofp_gpio44_value == NULL) {printf("Unable to open gpio44_value.\n");} 62 | fseek(ofp_gpio44_value, 0, SEEK_SET); 63 | fprintf(ofp_gpio44_value, "%d", logic_status_44); 64 | fflush(ofp_gpio44_value); 65 | 66 | while(1) 67 | { 68 | //configure gpio66 value and read the gpio66 pin 69 | ifp_gpio66_value = fopen("/sys/class/gpio/gpio66/value", "r"); 70 | if(ifp_gpio66_value == NULL) {printf("Unable to open gpio66_value.\n");} 71 | fseek(ifp_gpio66_value, 0, SEEK_SET); 72 | fscanf(ifp_gpio66_value, "%d", &logic_status_66); 73 | fclose(ifp_gpio66_value); 74 | printf("%d", logic_status_66); 75 | if(logic_status_66 == 1) 76 | { 77 | //set gpio44 logic high 78 | fseek(ofp_gpio44_value, 0, SEEK_SET); 79 | logic_status_44 = 1; 80 | fprintf(ofp_gpio44_value, "%d", logic_status_44); 81 | fflush(ofp_gpio44_value); 82 | printf("High\n"); 83 | } 84 | else 85 | { 86 | //set gpio44 logic low 87 | fseek(ofp_gpio44_value, 0, SEEK_SET); 88 | logic_status_44 = 0; 89 | fprintf(ofp_gpio44_value, "%d", logic_status_44); 90 | fflush(ofp_gpio44_value); 91 | printf("Low\n"); 92 | } 93 | } 94 | 95 | //close files 96 | fclose(ofp_export_44); 97 | fclose(ofp_gpio44_direction); 98 | fclose(ofp_gpio44_value); 99 | 100 | fclose(ofp_export_66); 101 | fclose(ofp_gpio66_direction); 102 | fclose(ifp_gpio66_value); 103 | 104 | return 1; 105 | } 106 | //********************************************************************* 107 | 108 | -------------------------------------------------------------------------------- /chapter-5/led2.cpp: -------------------------------------------------------------------------------- 1 | //********************************************************************* 2 | //led2.cpp: This program reads the logic value of a pushbutton switch 3 | //connected to expansion header P8, pin 5 (GPIO1_2 designated as gpio34). 4 | //If the switch is logic high (1) an LED connected to expansion header 5 | //P8, pin3 (GPIO1_6 designated as gpio38) is illuminated. 6 | //********************************************************************* 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #define output "out" 13 | #define input "in" 14 | 15 | int main (void) 16 | { 17 | //define file handles for gpio38 (P8, pin 3, GPIO1_6) 18 | FILE *ofp_gpmc_ad6, *ofp_export_38, *ofp_gpio38_value, *ofp_gpio38_direction; 19 | 20 | //define file handles for gpio34 (P8, pin 5, GPIO1_2) 21 | FILE *ofp_gpmc_ad2, *ofp_export_34, *ifp_gpio34_value, *ofp_gpio34_direction; 22 | 23 | //define pin variables for gpio38 24 | int mux_mode_38 = 0x0007, pin_number_38 = 38, logic_status_38 = 1; 25 | char* pin_direction_38 = output; 26 | 27 | //define pin variables for gpio34 28 | int mux_mode_34 = 0x003f, pin_number_34 = 34, logic_status_34; 29 | char* pin_direction_34 = input; 30 | 31 | //gpio38 mux setting 32 | ofp_gpmc_ad6 = fopen("/sys/kernel/debug/omap_mux/gpmc_ad6", "w"); 33 | if(ofp_gpmc_ad6 == NULL) {printf("Unable to open gpmc_ad6.\n");} 34 | fseek(ofp_gpmc_ad6, 0, SEEK_SET); 35 | fprintf(ofp_gpmc_ad6, "0x%02x", mux_mode_38); 36 | fflush(ofp_gpmc_ad6); 37 | 38 | //gpio34 mux setting 39 | ofp_gpmc_ad2 = fopen("/sys/kernel/debug/omap_mux/gpmc_ad2", "w"); 40 | if(ofp_gpmc_ad2 == NULL) {printf("Unable to open gpmc_ad2.\n");} 41 | fseek(ofp_gpmc_ad2, 0, SEEK_SET); 42 | fprintf(ofp_gpmc_ad2, "0x%02x", mux_mode_34); 43 | fflush(ofp_gpmc_ad2); 44 | 45 | //create direction and value file for gpio38 46 | ofp_export_38 = fopen("/sys/class/gpio/export", "w"); 47 | if(ofp_export_38 == NULL) {printf("Unable to open export.\n");} 48 | fseek(ofp_export_38, 0, SEEK_SET); 49 | fprintf(ofp_export_38, "%d", pin_number_38); 50 | fflush(ofp_export_38); 51 | 52 | //create direction and value file for gpio34 53 | ofp_export_34 = fopen("/sys/class/gpio/export", "w"); 54 | if(ofp_export_34 == NULL) {printf("Unable to open export.\n");} 55 | fseek(ofp_export_34, 0, SEEK_SET); 56 | fprintf(ofp_export_34, "%d", pin_number_34); 57 | fflush(ofp_export_34); 58 | 59 | //configure gpio38 direction 60 | ofp_gpio38_direction = fopen("/sys/class/gpio/gpio38/direction", "w"); 61 | if(ofp_gpio38_direction == NULL) {printf("Unable to open gpio38_direction.\n");} 62 | fseek(ofp_gpio38_direction, 0, SEEK_SET); 63 | fprintf(ofp_gpio38_direction, "%s", pin_direction_38); 64 | fflush(ofp_gpio38_direction); 65 | 66 | //configure gpio34 direction 67 | ofp_gpio34_direction = fopen("/sys/class/gpio/gpio34/direction", "w"); 68 | if(ofp_gpio34_direction == NULL) {printf("Unable to open gpio34_direction.\n");} 69 | fseek(ofp_gpio34_direction, 0, SEEK_SET); 70 | fprintf(ofp_gpio34_direction, "%s", pin_direction_34); 71 | fflush(ofp_gpio34_direction); 72 | 73 | //configure gpio38 value \--- initially set logic high 74 | ofp_gpio38_value = fopen("/sys/class/gpio/gpio38/value", "w"); 75 | if(ofp_gpio38_value == NULL) {printf("Unable to open gpio38_value.\n");} 76 | fseek(ofp_gpio38_value, 0, SEEK_SET); 77 | fprintf(ofp_gpio38_value, "%d", logic_status_38); 78 | fflush(ofp_gpio38_value); 79 | 80 | while(1) 81 | { 82 | //configure gpio34 value and read the gpio34 pin 83 | ifp_gpio34_value = fopen("/sys/class/gpio/gpio34/value", "r"); 84 | if(ifp_gpio34_value == NULL) {printf("Unable to open gpio34_value.\n");} 85 | fseek(ifp_gpio34_value, 0, SEEK_SET); 86 | fscanf(ifp_gpio34_value, "%d", &logic_status_34); 87 | fclose(ifp_gpio34_value); 88 | printf("%d", logic_status_34); 89 | if(logic_status_34 == 1) 90 | { 91 | //set gpio38 logic high 92 | fseek(ofp_gpio38_value, 0, SEEK_SET); 93 | logic_status_38 = 1; 94 | fprintf(ofp_gpio38_value, "%d", logic_status_38); 95 | fflush(ofp_gpio38_value); 96 | printf("High\n"); 97 | } 98 | else 99 | { 100 | //set gpio38 logic low 101 | fseek(ofp_gpio38_value, 0, SEEK_SET); 102 | logic_status_38 = 0; 103 | fprintf(ofp_gpio38_value, "%d", logic_status_38); 104 | fflush(ofp_gpio38_value); 105 | printf("Low\n"); 106 | } 107 | } 108 | 109 | //close files 110 | fclose(ofp_gpmc_ad6); 111 | fclose(ofp_export_38); 112 | fclose(ofp_gpio38_direction); 113 | fclose(ofp_gpio38_value); 114 | 115 | fclose(ofp_gpmc_ad2); 116 | fclose(ofp_export_34); 117 | fclose(ofp_gpio34_direction); 118 | fclose(ifp_gpio34_value); 119 | 120 | return 1; 121 | } 122 | //********************************************************************* 123 | 124 | -------------------------------------------------------------------------------- /chapter-3/LCD1.js: -------------------------------------------------------------------------------- 1 | //****************************************************************** 2 | 3 | var b = require ('bonescript'); 4 | 5 | //Old bonescript defines 'bone' globally 6 | var pins = (typeof bone != 'undefined') ? bone : b.bone.pins; 7 | 8 | var LCD_RS = pins.P8_9; //LCD Register Set (RS) control 9 | var LCD_E = pins.P8_10; //LCD Enable (E) control 10 | var LCD_DB0 = pins.P8_11; //LCD Data line DB0 11 | var LCD_DB1 = pins.P8_12; //LCD Data line DB1 12 | var LCD_DB2 = pins.P8_13; //LCD Data line DB2 13 | var LCD_DB3 = pins.P8_14; //LCD Data line DB3 14 | var LCD_DB4 = pins.P8_15; //LCD Data line DB4 15 | var LCD_DB5 = pins.P8_16; //LCD Data line DB5 16 | var LCD_DB6 = pins.P8_17; //LCD Data line DB6 17 | var LCD_DB7 = pins.P8_18; //LCD Data line DB7 18 | 19 | var counter = 1; //counter to be displayed 20 | 21 | b.pinMode(LCD_RS, b.OUTPUT); //set pin to digital output 22 | b.pinMode(LCD_E, b.OUTPUT); //set pin to digital output 23 | b.pinMode(LCD_DB0, b.OUTPUT); //set pin to digital output 24 | b.pinMode(LCD_DB1, b.OUTPUT); //set pin to digital output 25 | b.pinMode(LCD_DB2, b.OUTPUT); //set pin to digital output 26 | b.pinMode(LCD_DB3, b.OUTPUT); //set pin to digital output 27 | b.pinMode(LCD_DB4, b.OUTPUT); //set pin to digital output 28 | b.pinMode(LCD_DB5, b.OUTPUT); //set pin to digital output 29 | b.pinMode(LCD_DB6, b.OUTPUT); //set pin to digital output 30 | b.pinMode(LCD_DB7, b.OUTPUT); //set pin to digital output 31 | LCD_init(LCD_update); //call LCD initialize 32 | 33 | //****************************************************************** 34 | //LCD_update 35 | //****************************************************************** 36 | 37 | function LCD_update() 38 | { 39 | LCD_putchar(counter, next); //write 'counter' value to LCD 40 | 41 | //When LCD_putchar completes, schedule the next run of it 42 | function next() 43 | { 44 | counter++; //update counter 45 | if(counter > 9) //Re-init after 9 46 | { 47 | counter = 1; 48 | LCD_init(LCD_update); 49 | } 50 | else 51 | setTimeout(LCD_update, 500); //update again in 500 ms 52 | } 53 | } 54 | 55 | //****************************************************************** 56 | //LCD_init 57 | //****************************************************************** 58 | 59 | function LCD_init(callback) 60 | { 61 | //LCD Enable (E) pin low 62 | b.digitalWrite(LCD_E, b.LOW); 63 | 64 | //Start at the beginning of the list of steps to perform 65 | var i = 0; 66 | 67 | //List of steps to perform 68 | var steps = 69 | [ 70 | function(){ setTimeout(next, 15); }, //delay 15ms 71 | function(){ LCD_putcommand(0x38, next); }, //set for 8-bit operation 72 | function(){ setTimeout(next, 5); }, //delay 5ms 73 | function(){ LCD_putcommand(0x38, next); }, //set for 8-bit operation 74 | function(){ LCD_putcommand(0x38, next); }, //set for 5 x 7 character 75 | function(){ LCD_putcommand(0x0E, next); }, //display on 76 | function(){ LCD_putcommand(0x01, next); }, //display clear 77 | function(){ LCD_putcommand(0x06, next); }, //entry mode set 78 | function(){ LCD_putcommand(0x00, next); }, //clear display, cursor at home 79 | function(){ LCD_putcommand(0x00, callback); } //clear display, cursor at home 80 | ]; 81 | 82 | next(); //Execute the first step 83 | 84 | //Function for executing the next step 85 | function next() 86 | { 87 | i++; 88 | steps[i-1](); 89 | } 90 | } 91 | 92 | //****************************************************************** 93 | //LCD_putcommand 94 | //****************************************************************** 95 | 96 | function LCD_putcommand(cmd, callback) 97 | { 98 | //parse command variable into individual bits for output 99 | //to LCD 100 | if((cmd & 0x0080)== 0x0080) b.digitalWrite(LCD_DB7, b.HIGH); 101 | else b.digitalWrite(LCD_DB7, b.LOW); 102 | if((cmd & 0x0040)== 0x0040) b.digitalWrite(LCD_DB6, b.HIGH); 103 | else b.digitalWrite(LCD_DB6, b.LOW); 104 | if((cmd & 0x0020)== 0x0020) b.digitalWrite(LCD_DB5, b.HIGH); 105 | else b.digitalWrite(LCD_DB5, b.LOW); 106 | if((cmd & 0x0010)== 0x0010) b.digitalWrite(LCD_DB4, b.HIGH); 107 | else b.digitalWrite(LCD_DB4, b.LOW); 108 | if((cmd & 0x0008)== 0x0008) b.digitalWrite(LCD_DB3, b.HIGH); 109 | else b.digitalWrite(LCD_DB3, b.LOW); 110 | if((cmd & 0x0004)== 0x0004) b.digitalWrite(LCD_DB2, b.HIGH); 111 | else b.digitalWrite(LCD_DB2, b.LOW); 112 | if((cmd & 0x0002)== 0x0002) b.digitalWrite(LCD_DB1, b.HIGH); 113 | else b.digitalWrite(LCD_DB1, b.LOW); 114 | if((cmd & 0x0001)== 0x0001) b.digitalWrite(LCD_DB0, b.HIGH); 115 | else b.digitalWrite(LCD_DB0, b.LOW); 116 | 117 | //LCD Register Set (RS) to logic zero for command input 118 | b.digitalWrite(LCD_RS, b.LOW); 119 | //LCD Enable (E) pin high 120 | b.digitalWrite(LCD_E, b.HIGH); 121 | 122 | //End the write after 1ms 123 | setTimeout(endWrite, 1); 124 | 125 | function endWrite() 126 | { 127 | //LCD Enable (E) pin low 128 | b.digitalWrite(LCD_E, b.LOW); 129 | //delay 1ms before calling 'callback' 130 | setTimeout(callback, 1); 131 | } 132 | } 133 | 134 | //****************************************************************** 135 | //LCD_putchar 136 | //****************************************************************** 137 | 138 | function LCD_putchar(chr1, callback) 139 | { 140 | //Convert chr1 variable to UNICODE (ASCII) 141 | var chr = chr1.toString().charCodeAt(0); 142 | 143 | //parse character variable into individual bits for output 144 | //to LCD 145 | if((chr & 0x0080)== 0x0080) b.digitalWrite(LCD_DB7, b.HIGH); 146 | else b.digitalWrite(LCD_DB7, b.LOW); 147 | if((chr & 0x0040)== 0x0040) b.digitalWrite(LCD_DB6, b.HIGH); 148 | else b.digitalWrite(LCD_DB6, b.LOW); 149 | if((chr & 0x0020)== 0x0020) b.digitalWrite(LCD_DB5, b.HIGH); 150 | else b.digitalWrite(LCD_DB5, b.LOW); 151 | if((chr & 0x0010)== 0x0010) b.digitalWrite(LCD_DB4, b.HIGH); 152 | else b.digitalWrite(LCD_DB4, b.LOW); 153 | if((chr & 0x0008)== 0x0008) b.digitalWrite(LCD_DB3, b.HIGH); 154 | else b.digitalWrite(LCD_DB3, b.LOW); 155 | if((chr & 0x0004)== 0x0004) b.digitalWrite(LCD_DB2, b.HIGH); 156 | else b.digitalWrite(LCD_DB2, b.LOW); 157 | if((chr & 0x0002)== 0x0002) b.digitalWrite(LCD_DB1, b.HIGH); 158 | else b.digitalWrite(LCD_DB1, b.LOW); 159 | if((chr & 0x0001)== 0x0001) b.digitalWrite(LCD_DB0, b.HIGH); 160 | else b.digitalWrite(LCD_DB0, b.LOW); 161 | 162 | //LCD Register Set (RS) to logic one for character input 163 | b.digitalWrite(LCD_RS, b.HIGH); 164 | //LCD Enable (E) pin high 165 | b.digitalWrite(LCD_E, b.HIGH); 166 | 167 | //End the write after 1ms 168 | setTimeout(endWrite, 1); 169 | 170 | function endWrite() 171 | { 172 | //LCD Enable (E) pin low and call scheduleCallback when done 173 | b.digitalWrite(LCD_E, b.LOW); 174 | //delay 1ms before calling 'callback' 175 | setTimeout(callback, 1); 176 | } 177 | } 178 | 179 | //****************************************************************** 180 | -------------------------------------------------------------------------------- /chapter-3/LCD2.js: -------------------------------------------------------------------------------- 1 | //****************************************************************** 2 | 3 | var b = require ('bonescript'); 4 | 5 | //Old bonescript defines 'bone' globally 6 | var pins = (typeof bone != 'undefined') ? bone : b.bone.pins; 7 | 8 | var LCD_RS = pins.P8_9; //LCD Register Set (RS) control 9 | var LCD_E = pins.P8_10; //LCD Enable (E) control 10 | var LCD_DB0 = pins.P8_11; //LCD Data line DB0 11 | var LCD_DB1 = pins.P8_12; //LCD Data line DB1 12 | var LCD_DB2 = pins.P8_13; //LCD Data line DB2 13 | var LCD_DB3 = pins.P8_14; //LCD Data line DB3 14 | var LCD_DB4 = pins.P8_15; //LCD Data line DB4 15 | var LCD_DB5 = pins.P8_16; //LCD Data line DB5 16 | var LCD_DB6 = pins.P8_17; //LCD Data line DB6 17 | var LCD_DB7 = pins.P8_18; //LCD Data line DB7 18 | 19 | b.pinMode(LCD_RS, b.OUTPUT); //set pin to digital output 20 | b.pinMode(LCD_E, b.OUTPUT); //set pin to digital output 21 | b.pinMode(LCD_DB0, b.OUTPUT); //set pin to digital output 22 | b.pinMode(LCD_DB1, b.OUTPUT); //set pin to digital output 23 | b.pinMode(LCD_DB2, b.OUTPUT); //set pin to digital output 24 | b.pinMode(LCD_DB3, b.OUTPUT); //set pin to digital output 25 | b.pinMode(LCD_DB4, b.OUTPUT); //set pin to digital output 26 | b.pinMode(LCD_DB5, b.OUTPUT); //set pin to digital output 27 | b.pinMode(LCD_DB6, b.OUTPUT); //set pin to digital output 28 | b.pinMode(LCD_DB7, b.OUTPUT); //set pin to digital output 29 | LCD_init(firstLine); //call LCD initialize 30 | function firstLine() 31 | { 32 | LCD_print(1, "BeagleBone", nextLine); 33 | } 34 | function nextLine() 35 | { 36 | LCD_print(2, "Bonescript"); 37 | } 38 | 39 | //****************************************************************** 40 | //LCD_print 41 | //****************************************************************** 42 | 43 | function LCD_print(line, message, callback) 44 | { 45 | var i = 0; 46 | 47 | if(line == 1) 48 | { 49 | LCD_putcommand(0x80, writeNextCharacter);//print to LCD line 1 50 | } 51 | else 52 | { 53 | LCD_putcommand(0xc0, writeNextCharacter);//print to LCD line 2 54 | } 55 | 56 | function writeNextCharacter() 57 | { 58 | //if we already printed the last character, stop and callback 59 | if(i == message.length) 60 | { 61 | if(callback) callback(); 62 | return; 63 | } 64 | 65 | //get the next character to print 66 | var chr = message.substring(i, i+1); 67 | i++; 68 | 69 | //print it using LCD_putchar and come back again when done 70 | LCD_putchar(chr, writeNextCharacter); 71 | } 72 | } 73 | 74 | //****************************************************************** 75 | //LCD_init 76 | //****************************************************************** 77 | 78 | function LCD_init(callback) 79 | { 80 | //LCD Enable (E) pin low 81 | b.digitalWrite(LCD_E, b.LOW); 82 | 83 | //Start at the beginning of the list of steps to perform 84 | var i = 0; 85 | 86 | //List of steps to perform 87 | var steps = 88 | [ 89 | function(){ setTimeout(next, 15); }, //delay 15ms 90 | function(){ LCD_putcommand(0x38, next); }, //set for 8-bit operation 91 | function(){ setTimeout(next, 5); }, //delay 5ms 92 | function(){ LCD_putcommand(0x38, next); }, //set for 8-bit operation 93 | function(){ LCD_putcommand(0x38, next); }, //set for 5 x 7 character 94 | function(){ LCD_putcommand(0x0E, next); }, //display on 95 | function(){ LCD_putcommand(0x01, next); }, //display clear 96 | function(){ LCD_putcommand(0x06, next); }, //entry mode set 97 | function(){ LCD_putcommand(0x00, next); }, //clear display, cursor at home 98 | function(){ LCD_putcommand(0x00, callback); } //clear display, cursor at home 99 | ]; 100 | 101 | next(); //Execute the first step 102 | 103 | //Function for executing the next step 104 | function next() 105 | { 106 | i++; 107 | steps[i-1](); 108 | } 109 | } 110 | 111 | //****************************************************************** 112 | //LCD_putcommand 113 | //****************************************************************** 114 | 115 | function LCD_putcommand(cmd, callback) 116 | { 117 | //parse command variable into individual bits for output 118 | //to LCD 119 | if((cmd & 0x0080)== 0x0080) b.digitalWrite(LCD_DB7, b.HIGH); 120 | else b.digitalWrite(LCD_DB7, b.LOW); 121 | if((cmd & 0x0040)== 0x0040) b.digitalWrite(LCD_DB6, b.HIGH); 122 | else b.digitalWrite(LCD_DB6, b.LOW); 123 | if((cmd & 0x0020)== 0x0020) b.digitalWrite(LCD_DB5, b.HIGH); 124 | else b.digitalWrite(LCD_DB5, b.LOW); 125 | if((cmd & 0x0010)== 0x0010) b.digitalWrite(LCD_DB4, b.HIGH); 126 | else b.digitalWrite(LCD_DB4, b.LOW); 127 | if((cmd & 0x0008)== 0x0008) b.digitalWrite(LCD_DB3, b.HIGH); 128 | else b.digitalWrite(LCD_DB3, b.LOW); 129 | if((cmd & 0x0004)== 0x0004) b.digitalWrite(LCD_DB2, b.HIGH); 130 | else b.digitalWrite(LCD_DB2, b.LOW); 131 | if((cmd & 0x0002)== 0x0002) b.digitalWrite(LCD_DB1, b.HIGH); 132 | else b.digitalWrite(LCD_DB1, b.LOW); 133 | if((cmd & 0x0001)== 0x0001) b.digitalWrite(LCD_DB0, b.HIGH); 134 | else b.digitalWrite(LCD_DB0, b.LOW); 135 | 136 | //LCD Register Set (RS) to logic zero for command input 137 | b.digitalWrite(LCD_RS, b.LOW); 138 | //LCD Enable (E) pin high 139 | b.digitalWrite(LCD_E, b.HIGH); 140 | 141 | //End the write after 1ms 142 | setTimeout(endWrite, 1); 143 | 144 | function endWrite() 145 | { 146 | //LCD Enable (E) pin low 147 | b.digitalWrite(LCD_E, b.LOW); 148 | //delay 1ms before calling 'callback' 149 | setTimeout(callback, 1); 150 | } 151 | } 152 | 153 | //****************************************************************** 154 | //LCD_putchar 155 | //****************************************************************** 156 | 157 | function LCD_putchar(chr1, callback) 158 | { 159 | //Convert chr1 variable to UNICODE (ASCII) 160 | var chr = chr1.toString().charCodeAt(0); 161 | 162 | //parse character variable into individual bits for output 163 | //to LCD 164 | if((chr & 0x0080)== 0x0080) b.digitalWrite(LCD_DB7, b.HIGH); 165 | else b.digitalWrite(LCD_DB7, b.LOW); 166 | if((chr & 0x0040)== 0x0040) b.digitalWrite(LCD_DB6, b.HIGH); 167 | else b.digitalWrite(LCD_DB6, b.LOW); 168 | if((chr & 0x0020)== 0x0020) b.digitalWrite(LCD_DB5, b.HIGH); 169 | else b.digitalWrite(LCD_DB5, b.LOW); 170 | if((chr & 0x0010)== 0x0010) b.digitalWrite(LCD_DB4, b.HIGH); 171 | else b.digitalWrite(LCD_DB4, b.LOW); 172 | if((chr & 0x0008)== 0x0008) b.digitalWrite(LCD_DB3, b.HIGH); 173 | else b.digitalWrite(LCD_DB3, b.LOW); 174 | if((chr & 0x0004)== 0x0004) b.digitalWrite(LCD_DB2, b.HIGH); 175 | else b.digitalWrite(LCD_DB2, b.LOW); 176 | if((chr & 0x0002)== 0x0002) b.digitalWrite(LCD_DB1, b.HIGH); 177 | else b.digitalWrite(LCD_DB1, b.LOW); 178 | if((chr & 0x0001)== 0x0001) b.digitalWrite(LCD_DB0, b.HIGH); 179 | else b.digitalWrite(LCD_DB0, b.LOW); 180 | 181 | //LCD Register Set (RS) to logic one for character input 182 | b.digitalWrite(LCD_RS, b.HIGH); 183 | //LCD Enable (E) pin high 184 | b.digitalWrite(LCD_E, b.HIGH); 185 | 186 | //End the write after 1ms 187 | setTimeout(endWrite, 1); 188 | 189 | function endWrite() 190 | { 191 | //LCD Enable (E) pin low and call scheduleCallback when done 192 | b.digitalWrite(LCD_E, b.LOW); 193 | //delay 1ms before calling 'callback' 194 | setTimeout(callback, 1); 195 | } 196 | } 197 | 198 | //****************************************************************** 199 | -------------------------------------------------------------------------------- /chapter-3/cpuload.js: -------------------------------------------------------------------------------- 1 | //****************************************************************** 2 | 3 | var b = require('bonescript'); 4 | var fs = require('fs'); 5 | 6 | //Old bonescript defines 'bone' globally 7 | var pins = (typeof bone != 'undefined') ? bone : b.bone.pins; 8 | 9 | var LCD_RS = pins.P8_9; //LCD Register Set (RS) control 10 | var LCD_E = pins.P8_10; //LCD Enable (E) control 11 | var LCD_DB0 = pins.P8_11; //LCD Data line DB0 12 | var LCD_DB1 = pins.P8_12; //LCD Data line DB1 13 | var LCD_DB2 = pins.P8_13; //LCD Data line DB2 14 | var LCD_DB3 = pins.P8_14; //LCD Data line DB3 15 | var LCD_DB4 = pins.P8_15; //LCD Data line DB4 16 | var LCD_DB5 = pins.P8_16; //LCD Data line DB5 17 | var LCD_DB6 = pins.P8_17; //LCD Data line DB6 18 | var LCD_DB7 = pins.P8_18; //LCD Data line DB7 19 | 20 | b.pinMode(LCD_RS, b.OUTPUT); //set pin to digital output 21 | b.pinMode(LCD_E, b.OUTPUT); //set pin to digital output 22 | b.pinMode(LCD_DB0, b.OUTPUT); //set pin to digital output 23 | b.pinMode(LCD_DB1, b.OUTPUT); //set pin to digital output 24 | b.pinMode(LCD_DB2, b.OUTPUT); //set pin to digital output 25 | b.pinMode(LCD_DB3, b.OUTPUT); //set pin to digital output 26 | b.pinMode(LCD_DB4, b.OUTPUT); //set pin to digital output 27 | b.pinMode(LCD_DB5, b.OUTPUT); //set pin to digital output 28 | b.pinMode(LCD_DB6, b.OUTPUT); //set pin to digital output 29 | b.pinMode(LCD_DB7, b.OUTPUT); //set pin to digital output 30 | LCD_init(firstLine); //call LCD initialize 31 | function firstLine() 32 | { 33 | LCD_print(1, "BeagleBone Load", doUpdate); 34 | } 35 | function doUpdate() 36 | { 37 | fs.readFile("/proc/loadavg", writeUpdate); 38 | } 39 | function writeUpdate(err, data) 40 | { 41 | LCD_print(2, data.toString().substring(0, 14), onUpdate); 42 | } 43 | function onUpdate() 44 | { 45 | setTimeout(doUpdate, 1000); 46 | } 47 | 48 | //****************************************************************** 49 | //LCD_print 50 | //****************************************************************** 51 | 52 | function LCD_print(line, message, callback) 53 | { 54 | var i = 0; 55 | 56 | if(line == 1) 57 | { 58 | LCD_putcommand(0x80, writeNextCharacter);//print to LCD line 1 59 | } 60 | else 61 | { 62 | LCD_putcommand(0xc0, writeNextCharacter);//print to LCD line 2 63 | } 64 | 65 | function writeNextCharacter() 66 | { 67 | //if we already printed the last character, stop and callback 68 | if(i == message.length) 69 | { 70 | if(callback) callback(); 71 | return; 72 | } 73 | 74 | //get the next character to print 75 | var chr = message.substring(i, i+1); 76 | i++; 77 | 78 | //print it using LCD_putchar and come back again when done 79 | LCD_putchar(chr, writeNextCharacter); 80 | } 81 | } 82 | 83 | //****************************************************************** 84 | //LCD_init 85 | //****************************************************************** 86 | 87 | function LCD_init(callback) 88 | { 89 | //LCD Enable (E) pin low 90 | b.digitalWrite(LCD_E, b.LOW); 91 | 92 | //Start at the beginning of the list of steps to perform 93 | var i = 0; 94 | 95 | //List of steps to perform 96 | var steps = 97 | [ 98 | function(){ setTimeout(next, 15); }, //delay 15ms 99 | function(){ LCD_putcommand(0x38, next); }, //set for 8-bit operation 100 | function(){ setTimeout(next, 5); }, //delay 5ms 101 | function(){ LCD_putcommand(0x38, next); }, //set for 8-bit operation 102 | function(){ LCD_putcommand(0x38, next); }, //set for 5 x 7 character 103 | function(){ LCD_putcommand(0x0E, next); }, //display on 104 | function(){ LCD_putcommand(0x01, next); }, //display clear 105 | function(){ LCD_putcommand(0x06, next); }, //entry mode set 106 | function(){ LCD_putcommand(0x00, next); }, //clear display, cursor at home 107 | function(){ LCD_putcommand(0x00, callback); } //clear display, cursor at home 108 | ]; 109 | 110 | next(); //Execute the first step 111 | 112 | //Function for executing the next step 113 | function next() 114 | { 115 | i++; 116 | steps[i-1](); 117 | } 118 | } 119 | 120 | //****************************************************************** 121 | //LCD_putcommand 122 | //****************************************************************** 123 | 124 | function LCD_putcommand(cmd, callback) 125 | { 126 | var chr = cmd; 127 | 128 | //Start at the beginning of the list of steps to perform 129 | var i = 0; 130 | 131 | //List of steps to perform 132 | var steps = 133 | [ 134 | function() { 135 | //parse character variable into individual bits for output 136 | //to LCD 137 | if((chr & 0x0080)== 0x0080) b.digitalWrite(LCD_DB7, b.HIGH, next); 138 | else b.digitalWrite(LCD_DB7, b.LOW, next); 139 | },function() { 140 | if((chr & 0x0040)== 0x0040) b.digitalWrite(LCD_DB6, b.HIGH, next); 141 | else b.digitalWrite(LCD_DB6, b.LOW, next); 142 | },function() { 143 | if((chr & 0x0020)== 0x0020) b.digitalWrite(LCD_DB5, b.HIGH, next); 144 | else b.digitalWrite(LCD_DB5, b.LOW, next); 145 | },function() { 146 | if((chr & 0x0010)== 0x0010) b.digitalWrite(LCD_DB4, b.HIGH, next); 147 | else b.digitalWrite(LCD_DB4, b.LOW, next); 148 | },function() { 149 | if((chr & 0x0008)== 0x0008) b.digitalWrite(LCD_DB3, b.HIGH, next); 150 | else b.digitalWrite(LCD_DB3, b.LOW, next); 151 | },function() { 152 | if((chr & 0x0004)== 0x0004) b.digitalWrite(LCD_DB2, b.HIGH, next); 153 | else b.digitalWrite(LCD_DB2, b.LOW, next); 154 | },function() { 155 | if((chr & 0x0002)== 0x0002) b.digitalWrite(LCD_DB1, b.HIGH, next); 156 | else b.digitalWrite(LCD_DB1, b.LOW, next); 157 | },function() { 158 | if((chr & 0x0001)== 0x0001) b.digitalWrite(LCD_DB0, b.HIGH, next); 159 | else b.digitalWrite(LCD_DB0, b.LOW, next); 160 | },function() { 161 | //LCD Register Set (RS) to logic zero for command input 162 | b.digitalWrite(LCD_RS, b.LOW, next); 163 | },function() { 164 | //LCD Enable (E) pin high 165 | b.digitalWrite(LCD_E, b.HIGH, next); 166 | },function() { 167 | //End the write after 1ms 168 | setTimeout(next, 1); 169 | },function() { 170 | //LCD Enable (E) pin low and call scheduleCallback when done 171 | b.digitalWrite(LCD_E, b.LOW, next); 172 | },function() { 173 | //delay 1ms before calling 'callback' 174 | setTimeout(callback, 1); 175 | }]; 176 | 177 | next(); //Execute the first step 178 | 179 | //Function for executing the next step 180 | function next() 181 | { 182 | i++; 183 | steps[i-1](); 184 | } 185 | } 186 | 187 | //****************************************************************** 188 | //LCD_putchar 189 | //****************************************************************** 190 | 191 | function LCD_putchar(chr1, callback) 192 | { 193 | //Convert chr1 variable to UNICODE (ASCII) 194 | var chr = chr1.toString().charCodeAt(0); 195 | 196 | //Start at the beginning of the list of steps to perform 197 | var i = 0; 198 | 199 | //List of steps to perform 200 | var steps = 201 | [ 202 | function() { 203 | //parse character variable into individual bits for output 204 | //to LCD 205 | if((chr & 0x0080)== 0x0080) b.digitalWrite(LCD_DB7, b.HIGH, next); 206 | else b.digitalWrite(LCD_DB7, b.LOW, next); 207 | },function() { 208 | if((chr & 0x0040)== 0x0040) b.digitalWrite(LCD_DB6, b.HIGH, next); 209 | else b.digitalWrite(LCD_DB6, b.LOW, next); 210 | },function() { 211 | if((chr & 0x0020)== 0x0020) b.digitalWrite(LCD_DB5, b.HIGH, next); 212 | else b.digitalWrite(LCD_DB5, b.LOW, next); 213 | },function() { 214 | if((chr & 0x0010)== 0x0010) b.digitalWrite(LCD_DB4, b.HIGH, next); 215 | else b.digitalWrite(LCD_DB4, b.LOW, next); 216 | },function() { 217 | if((chr & 0x0008)== 0x0008) b.digitalWrite(LCD_DB3, b.HIGH, next); 218 | else b.digitalWrite(LCD_DB3, b.LOW, next); 219 | },function() { 220 | if((chr & 0x0004)== 0x0004) b.digitalWrite(LCD_DB2, b.HIGH, next); 221 | else b.digitalWrite(LCD_DB2, b.LOW, next); 222 | },function() { 223 | if((chr & 0x0002)== 0x0002) b.digitalWrite(LCD_DB1, b.HIGH, next); 224 | else b.digitalWrite(LCD_DB1, b.LOW, next); 225 | },function() { 226 | if((chr & 0x0001)== 0x0001) b.digitalWrite(LCD_DB0, b.HIGH, next); 227 | else b.digitalWrite(LCD_DB0, b.LOW, next); 228 | },function() { 229 | //LCD Register Set (RS) to logic one for character input 230 | b.digitalWrite(LCD_RS, b.HIGH, next); 231 | },function() { 232 | //LCD Enable (E) pin high 233 | b.digitalWrite(LCD_E, b.HIGH, next); 234 | },function() { 235 | //End the write after 1ms 236 | setTimeout(next, 1); 237 | },function() { 238 | //LCD Enable (E) pin low and call scheduleCallback when done 239 | b.digitalWrite(LCD_E, b.LOW, next); 240 | },function() { 241 | //delay 1ms before calling 'callback' 242 | setTimeout(callback, 1); 243 | }]; 244 | 245 | next(); //Execute the first step 246 | 247 | //Function for executing the next step 248 | function next() 249 | { 250 | i++; 251 | steps[i-1](); 252 | } 253 | } 254 | 255 | //****************************************************************** 256 | -------------------------------------------------------------------------------- /sns2.cpp: -------------------------------------------------------------------------------- 1 | //********************************************************************* 2 | //sns.cpp - Speak and Spell 3 | // - prompts user for input 4 | // - prints input to screen 5 | // - configures BeagleBone P8, pin 5 for receive from SP0-512 6 | // speech chip SPEAKING pin (SP0-512, pin 17) 7 | // - configures BeagleBone uart1 for tranmission and 9600 Baud 8 | // (P9, 24) 9 | // - provides spoken input via speech synthesis chip connected 10 | // to uart1 tx pin (P9, 24) 11 | //********************************************************************* 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #define output "out" 26 | #define input "in" 27 | 28 | 29 | //function prototype 30 | void delay_us(int); 31 | void delay_sec(float delay_value); 32 | void configure_uart1(void); 33 | void configure_SP0_512(void); 34 | void SP0_512_handshake(void); 35 | void reset_SP0_512(void); 36 | void configure_P8_3_output(void); 37 | 38 | //global variables 39 | //***UART1*** 40 | //define file handle for uart1 41 | FILE *ofp_uart1_tx, *ofp_uart1_rx; 42 | 43 | //define mux mode for uart1 tx 44 | int mux_mode_uart1_tx = 0X8000; 45 | int mux_mode_uart1_rx = 0X8020; 46 | 47 | //uart1 configuration using termios 48 | termios uart1; 49 | int fd, i; 50 | char byte_out[40]; 51 | 52 | //***SP0-512*** 53 | //configure P8, pin 5 for input - connect to SP0-512 SPEAKING signal 54 | //define file handles for gpio34 (P8, pin 5, GPIO1_2) 55 | FILE *ofp_gpmc_ad2, *ofp_export_34, *ifp_gpio34_value, *ofp_gpio34_direction; 56 | 57 | //define pin variables for gpio34 58 | int mux_mode_34 = 0x003f, pin_number_34 = 34, logic_status_34; 59 | char* pin_direction_34 = input; 60 | 61 | //configure P8, pin 3 for output - connect to reset circuit on SP0-512 62 | //define file handles for gpio38 (P8, pin 3, GPIO1_6) 63 | FILE *ofp_gpmc_ad6, *ofp_export_38, *ofp_gpio38_value, *ofp_gpio38_direction; 64 | 65 | //define pin variables for gpio38 66 | int mux_mode_38 = 0x0007, pin_number_38 = 38, logic_status_38 = 0; 67 | char* pin_direction_38 = output; 68 | 69 | char cntr_cr[] = "\r"; 70 | char reset[] = "[RESET]"; 71 | 72 | int main(void) 73 | { 74 | configure_uart1(); 75 | configure_P8_3_output(); 76 | configure_SP0_512(); 77 | 78 | while(1) 79 | { 80 | printf("Enter letter, word, statement. Press [Enter].\r\n\n"); 81 | scanf("%s", byte_out); 82 | printf("%s\r\n\n", byte_out); 83 | strcat(byte_out, cntr_cr); 84 | write(fd, byte_out, strlen(byte_out)+2); 85 | SP0_512_handshake(); 86 | reset_SP0_512(); 87 | } 88 | 89 | fclose(ofp_gpmc_ad2); 90 | fclose(ofp_export_34); 91 | fclose(ofp_gpio34_direction); 92 | fclose(ifp_gpio34_value); 93 | close(fd); 94 | } 95 | 96 | //************************************************************* 97 | 98 | void SP0_512_handshake(void) 99 | { 100 | for(i=0; i<=10; i++) //wait for SPEAKING line to go logic high 101 | { 102 | delay_us(10000); 103 | } 104 | 105 | logic_status_34 = 1; 106 | 107 | while(logic_status_34 != 0) //wait for SPEAKING line to go low 108 | { 109 | //configure gpio34 value and read the gpio34 pin 110 | ifp_gpio34_value = fopen("/sys/class/gpio/gpio34/value", "r"); 111 | if(ifp_gpio34_value == NULL) {printf("Unable to open gpio34_value.\n");} 112 | fseek(ifp_gpio34_value, 0, SEEK_SET); 113 | fscanf(ifp_gpio34_value, "%d", &logic_status_34); 114 | fclose(ifp_gpio34_value); 115 | } 116 | 117 | for(i=0; i<=10; i++) //wait for SPEAKING line to go logic high 118 | { 119 | delay_us(10000); 120 | } 121 | } 122 | 123 | //************************************************************* 124 | 125 | void configure_uart1(void) 126 | { 127 | 128 | //configure uart1 for transmission 129 | ofp_uart1_tx = fopen("/sys/kernel/debug/omap_mux/uart1_txd", "w"); 130 | if(ofp_uart1_tx == NULL) printf("Unable to open uart1 tx file.\n"); 131 | fseek(ofp_uart1_tx, 0, SEEK_SET); 132 | fprintf(ofp_uart1_tx, "0x%2x", mux_mode_uart1_tx); 133 | fflush(ofp_uart1_tx); 134 | 135 | //configure uart1 for reception 136 | ofp_uart1_rx = fopen("/sys/kernel/debug/omap_mux/uart1_rxd", "w"); 137 | if(ofp_uart1_rx == NULL) printf("Unable to open uart1 rx file.\n"); 138 | fseek(ofp_uart1_rx, 0, SEEK_SET); 139 | fprintf(ofp_uart1_rx, "0x%2x", mux_mode_uart1_rx); 140 | 141 | //open uart1 for tx/rx, not controlling device 142 | if((fd = open("/dev/ttyO1", O_RDWR | O_NOCTTY)) < 0) 143 | printf("Unable to open uart1 access.\n"); 144 | 145 | //get attributes of uart1 146 | if(tcgetattr(fd, &uart1) < 0) 147 | printf("Could not get attributes of UART1 at ttyO1\n"); 148 | 149 | //set Baud rate 150 | if(cfsetospeed(&uart1, B9600) < 0) 151 | printf("Could not set baud rate\n"); 152 | else 153 | printf("Baud rate: 9600\n"); 154 | 155 | //set attributes of uart1 156 | uart1.c_iflag = 0; 157 | uart1.c_oflag = 0; 158 | uart1.c_lflag = 0; 159 | uart1.c_cflag |= CS8; 160 | tcsetattr(fd, TCSANOW, &uart1); 161 | } 162 | 163 | //*********************************************************** 164 | 165 | void configure_SP0_512(void) 166 | { 167 | //gpio34 mux setting 168 | ofp_gpmc_ad2 = fopen("/sys/kernel/debug/omap_mux/gpmc_ad2", "w"); 169 | if(ofp_gpmc_ad2 == NULL) {printf("Unable to open gpmc_ad2.\n");} 170 | fseek(ofp_gpmc_ad2, 0, SEEK_SET); 171 | fprintf(ofp_gpmc_ad2, "0x%02x", mux_mode_34); 172 | fflush(ofp_gpmc_ad2); 173 | 174 | //create direction and value file for gpio34 175 | ofp_export_34 = fopen("/sys/class/gpio/export", "w"); 176 | if(ofp_export_34 == NULL) {printf("Unable to open export.\n");} 177 | fseek(ofp_export_34, 0, SEEK_SET); 178 | fprintf(ofp_export_34, "%d", pin_number_34); 179 | fflush(ofp_export_34); 180 | 181 | //configure gpio34 direction 182 | ofp_gpio34_direction = fopen("/sys/class/gpio/gpio34/direction", "w"); 183 | if(ofp_gpio34_direction == NULL) {printf("Unable to open gpio34_direction.\n");} 184 | fseek(ofp_gpio34_direction, 0, SEEK_SET); 185 | fprintf(ofp_gpio34_direction, "%s", pin_direction_34); 186 | } 187 | 188 | //********************************************************************* 189 | 190 | void configure_P8_3_output(void) 191 | { 192 | //gpio38 mux setting 193 | ofp_gpmc_ad6 = fopen("/sys/kernel/debug/omap_mux/gpmc_ad6", "w"); 194 | if(ofp_gpmc_ad6 == NULL) {printf("Unable to open gpmc_ad6.\n");} 195 | fseek(ofp_gpmc_ad6, 0, SEEK_SET); 196 | fprintf(ofp_gpmc_ad6, "0x%02x", mux_mode_38); 197 | fflush(ofp_gpmc_ad6); 198 | 199 | //create direction and value file for gpio38 200 | ofp_export_38 = fopen("/sys/class/gpio/export", "w"); 201 | if(ofp_export_38 == NULL) {printf("Unable to open export.\n");} 202 | fseek(ofp_export_38, 0, SEEK_SET); 203 | fprintf(ofp_export_38, "%d", pin_number_38); 204 | fflush(ofp_export_38); 205 | 206 | //configure gpio38 direction 207 | ofp_gpio38_direction = fopen("/sys/class/gpio/gpio38/direction", "w"); 208 | if(ofp_gpio38_direction == NULL) {printf("Unable to open gpio38_direction.\n");} 209 | fseek(ofp_gpio38_direction, 0, SEEK_SET); 210 | fprintf(ofp_gpio38_direction, "%s", pin_direction_38); 211 | fflush(ofp_gpio38_direction); 212 | 213 | //configure gpio38 value - initially set logic low 214 | ofp_gpio38_value = fopen("/sys/class/gpio/gpio38/value", "w"); 215 | if(ofp_gpio38_value == NULL) {printf("Unable to open gpio38_value.\n");} 216 | fseek(ofp_gpio38_value, 0, SEEK_SET); 217 | fprintf(ofp_gpio38_value, "%d", logic_status_38); 218 | fflush(ofp_gpio38_value); 219 | } 220 | 221 | //************************************************************* 222 | 223 | void reset_SP0_512(void) 224 | { 225 | logic_status_38 = 1; 226 | fseek(ofp_gpio38_value, 0, SEEK_SET); 227 | fprintf(ofp_gpio38_value, "%d", logic_status_38); 228 | fflush(ofp_gpio38_value); 229 | 230 | delay_us(10000); 231 | 232 | logic_status_38 = 0; 233 | fseek(ofp_gpio38_value, 0, SEEK_SET); 234 | fprintf(ofp_gpio38_value, "%d", logic_status_38); 235 | fflush(ofp_gpio38_value); 236 | } 237 | 238 | //************************************************************** 239 | 240 | void delay_us(int desired_delay_us) 241 | { 242 | struct timeval tv_start; //start time hack 243 | struct timeval tv_now; //current time hack 244 | int elapsed_time_us; 245 | 246 | gettimeofday(&tv_start, NULL); 247 | elapsed_time_us = 0; 248 | 249 | while(elapsed_time_us < desired_delay_us) 250 | { 251 | gettimeofday(&tv_now, NULL); 252 | if(tv_now.tv_usec >= tv_start.tv_usec) 253 | elapsed_time_us = tv_now.tv_usec - tv_start.tv_usec; 254 | else 255 | elapsed_time_us = (1000000 - tv_start.tv_usec) + tv_now.tv_usec; 256 | //printf("start: %ld \n", tv_start.tv_usec); 257 | //printf("now: %ld \n", tv_now.tv_usec); 258 | //printf("desired: %d \n", desired_delay_ms); 259 | //printf("elapsed: %d \n\n", elapsed_time_ms); 260 | } 261 | } 262 | 263 | //************************************************************* 264 | //delay_sec(float delay_value) 265 | //************************************************************* 266 | 267 | void delay_sec(float delay_value) 268 | { 269 | time_t now, later; 270 | 271 | now = time(NULL); 272 | later = time(NULL); 273 | 274 | while(difftime(later,now) < delay_value) 275 | { 276 | later = time(NULL); 277 | } 278 | } 279 | 280 | //************************************************************* 281 | -------------------------------------------------------------------------------- /chapter-6/weather.js: -------------------------------------------------------------------------------- 1 | //*********************************************************************** 2 | var b = require ('bonescript'); 3 | 4 | //Old bonescript defines 'bone' globally 5 | var pins = (typeof bone != 'undefined') ? bone : b.bone.pins; 6 | 7 | //sensor pin configuration 8 | var wx_vane = pins.P9_39; //weather vane 9 | var temp_sen= pins.P9_37; //temperature sensor 10 | var wx_vane_value; 11 | var temp_sen_value; 12 | 13 | //wind direction LED 14 | var LED_N = pins.P9_11; //N: LED segment J 15 | var LED_NE = pins.P9_12; //NE: LED segment K 16 | var LED_E = pins.P9_13; //E: LED segment G2 17 | var LED_SE = pins.P9_14; //SE: LED segment L 18 | var LED_S = pins.P9_15; //S: LED segment M 19 | var LED_SW = pins.P9_16; //SW: LED segment N 20 | var LED_W = pins.P9_17; //W: LED segment G1 21 | var LED_NW = pins.P9_18; //NW: LED segment H 22 | 23 | //LCD pin configuration 24 | var LCD_RS = pins.P8_9; //LCD Register Set (RS) control 25 | var LCD_E = pins.P8_10; //LCD Enable (E) control 26 | var LCD_DB0 = pins.P8_11; //LCD Data line DB0 27 | var LCD_DB1 = pins.P8_12; //LCD Data line DB1 28 | var LCD_DB2 = pins.P8_13; //LCD Data line DB2 29 | var LCD_DB3 = pins.P8_14; //LCD Data line DB3 30 | var LCD_DB4 = pins.P8_15; //LCD Data line DB4 31 | var LCD_DB5 = pins.P8_16; //LCD Data line DB5 32 | var LCD_DB6 = pins.P8_17; //LCD Data line DB6 33 | var LCD_DB7 = pins.P8_18; //LCD Data line DB7 34 | 35 | //wind direction pins 36 | b.pinMode(LED_N, b.OUTPUT); 37 | b.pinMode(LED_NE, b.OUTPUT); 38 | b.pinMode(LED_E, b.OUTPUT); 39 | b.pinMode(LED_SE, b.OUTPUT); 40 | b.pinMode(LED_S, b.OUTPUT); 41 | b.pinMode(LED_SW, b.OUTPUT); 42 | b.pinMode(LED_W, b.OUTPUT); 43 | b.pinMode(LED_NW, b.OUTPUT); 44 | 45 | //LCD direction pins 46 | b.pinMode(LCD_RS, b.OUTPUT); //set pin to digital output 47 | b.pinMode(LCD_E, b.OUTPUT); //set pin to digital output 48 | b.pinMode(LCD_DB0, b.OUTPUT); //set pin to digital output 49 | b.pinMode(LCD_DB1, b.OUTPUT); //set pin to digital output 50 | b.pinMode(LCD_DB2, b.OUTPUT); //set pin to digital output 51 | b.pinMode(LCD_DB3, b.OUTPUT); //set pin to digital output 52 | b.pinMode(LCD_DB4, b.OUTPUT); //set pin to digital output 53 | b.pinMode(LCD_DB5, b.OUTPUT); //set pin to digital output 54 | b.pinMode(LCD_DB6, b.OUTPUT); //set pin to digital output 55 | b.pinMode(LCD_DB7, b.OUTPUT); //set pin to digital output 56 | LCD_init(); //call LCD initialize 57 | 58 | setInterval(updateWeather, 100); 59 | 60 | function updateWeather() 61 | { 62 | clear_LEDs(); 63 | 64 | //Read weather vane and temperature sensors 65 | wx_vane_value = b.analogRead(wx_vane); 66 | temp_sen_value = b.analogRead(temp_sen); 67 | 68 | //Calculate temperature 69 | 70 | //North 71 | if((wx_vane_value > 0.9375)||(wx_vane_value <= 0.0625)) 72 | { 73 | //illuminate N LED 74 | b.digitalWrite(LED_N, b.HIGH); 75 | } 76 | 77 | //Northeast 78 | else if((wx_vane_value > 0.0625)&&(wx_vane_value <= 0.1875)) 79 | { 80 | //illuminate NE LED 81 | b.digitalWrite(LED_NE, b.HIGH); 82 | } 83 | 84 | //East 85 | else if((wx_vane_value > 0.1875)&&(wx_vane_value <= 0.3125)) 86 | { 87 | //illuminate E LED 88 | b.digitalWrite(LED_E, b.HIGH); 89 | } 90 | 91 | //Southeast 92 | else if((wx_vane_value > 0.3125)&&(wx_vane_value <= 0.4375)) 93 | { 94 | //illuminate SE LED 95 | b.digitalWrite(LED_SE, b.HIGH); 96 | } 97 | 98 | //South 99 | else if((wx_vane_value > 0.4375)&&(wx_vane_value <= 0.5625)) 100 | { 101 | //illuminate S LED 102 | b.digitalWrite(LED_S, b.HIGH); 103 | } 104 | 105 | //Southwest 106 | else if((wx_vane_value > 0.5625)&&(wx_vane_value <= 0.6875)) 107 | { 108 | //illuminate SW LED 109 | b.digitalWrite(LED_SW, b.HIGH); 110 | } 111 | 112 | //West 113 | else if((wx_vane_value > 0.6875)&&(wx_vane_value <= 0.8125)) 114 | { 115 | //illuminate W LED 116 | b.digitalWrite(LED_W, b.HIGH) 117 | } 118 | 119 | //NE 120 | else 121 | { 122 | //illuminate NE LED 123 | b.digitalWrite(LED_NE, b.HIGH) 124 | } 125 | } 126 | 127 | //****************************************************************** 128 | //clear_LEDs 129 | //****************************************************************** 130 | 131 | function clear_LEDs() { 132 | //reset LEDs 133 | b.digitalWrite(LED_N, b.LOW); 134 | b.digitalWrite(LED_NE, b.LOW); 135 | b.digitalWrite(LED_E, b.LOW); 136 | b.digitalWrite(LED_SE, b.LOW); 137 | b.digitalWrite(LED_S, b.LOW); 138 | b.digitalWrite(LED_SW, b.LOW); 139 | b.digitalWrite(LED_W, b.LOW); 140 | b.digitalWrite(LED_NW, b.LOW); 141 | } 142 | 143 | //****************************************************************** 144 | //LCD_print 145 | //****************************************************************** 146 | 147 | function LCD_print(line, message, callback) 148 | { 149 | var i = 0; 150 | 151 | if(line == 1) 152 | { 153 | LCD_putcommand(0x80, writeNextCharacter);//print to LCD line 1 154 | } 155 | else 156 | { 157 | LCD_putcommand(0xc0, writeNextCharacter);//print to LCD line 2 158 | } 159 | 160 | function writeNextCharacter() 161 | { 162 | //if we already printed the last character, stop and callback 163 | if(i == message.length) 164 | { 165 | if(callback) callback(); 166 | return; 167 | } 168 | 169 | //get the next character to print 170 | var chr = message.substring(i, i+1); 171 | i++; 172 | 173 | //print it using LCD_putchar and come back again when done 174 | LCD_putchar(chr, writeNextCharacter); 175 | } 176 | } 177 | 178 | //****************************************************************** 179 | //LCD_init 180 | //****************************************************************** 181 | 182 | function LCD_init(callback) 183 | { 184 | //LCD Enable (E) pin low 185 | b.digitalWrite(LCD_E, b.LOW); 186 | 187 | //Start at the beginning of the list of steps to perform 188 | var i = 0; 189 | 190 | //List of steps to perform 191 | var steps = 192 | [ 193 | function(){ setTimeout(next, 15); }, //delay 15ms 194 | function(){ LCD_putcommand(0x38, next); }, //set for 8-bit operation 195 | function(){ setTimeout(next, 5); }, //delay 5ms 196 | function(){ LCD_putcommand(0x38, next); }, //set for 8-bit operation 197 | function(){ LCD_putcommand(0x38, next); }, //set for 5 x 7 character 198 | function(){ LCD_putcommand(0x0E, next); }, //display on 199 | function(){ LCD_putcommand(0x01, next); }, //display clear 200 | function(){ LCD_putcommand(0x06, next); }, //entry mode set 201 | function(){ LCD_putcommand(0x00, next); }, //clear display, cursor at home 202 | function(){ LCD_putcommand(0x00, callback); } //clear display, cursor at home 203 | ]; 204 | 205 | next(); //Execute the first step 206 | 207 | //Function for executing the next step 208 | function next() 209 | { 210 | i++; 211 | steps[i-1](); 212 | } 213 | } 214 | 215 | //****************************************************************** 216 | //LCD_putcommand 217 | //****************************************************************** 218 | 219 | function LCD_putcommand(cmd, callback) 220 | { 221 | //parse command variable into individual bits for output 222 | //to LCD 223 | if((cmd & 0x0080)== 0x0080) b.digitalWrite(LCD_DB7, b.HIGH); 224 | else b.digitalWrite(LCD_DB7, b.LOW); 225 | if((cmd & 0x0040)== 0x0040) b.digitalWrite(LCD_DB6, b.HIGH); 226 | else b.digitalWrite(LCD_DB6, b.LOW); 227 | if((cmd & 0x0020)== 0x0020) b.digitalWrite(LCD_DB5, b.HIGH); 228 | else b.digitalWrite(LCD_DB5, b.LOW); 229 | if((cmd & 0x0010)== 0x0010) b.digitalWrite(LCD_DB4, b.HIGH); 230 | else b.digitalWrite(LCD_DB4, b.LOW); 231 | if((cmd & 0x0008)== 0x0008) b.digitalWrite(LCD_DB3, b.HIGH); 232 | else b.digitalWrite(LCD_DB3, b.LOW); 233 | if((cmd & 0x0004)== 0x0004) b.digitalWrite(LCD_DB2, b.HIGH); 234 | else b.digitalWrite(LCD_DB2, b.LOW); 235 | if((cmd & 0x0002)== 0x0002) b.digitalWrite(LCD_DB1, b.HIGH); 236 | else b.digitalWrite(LCD_DB1, b.LOW); 237 | if((cmd & 0x0001)== 0x0001) b.digitalWrite(LCD_DB0, b.HIGH); 238 | else b.digitalWrite(LCD_DB0, b.LOW); 239 | 240 | //LCD Register Set (RS) to logic zero for command input 241 | b.digitalWrite(LCD_RS, b.LOW); 242 | //LCD Enable (E) pin high 243 | b.digitalWrite(LCD_E, b.HIGH); 244 | 245 | //End the write after 1ms 246 | setTimeout(endWrite, 1); 247 | 248 | function endWrite() 249 | { 250 | //LCD Enable (E) pin low 251 | b.digitalWrite(LCD_E, b.LOW); 252 | //delay 1ms before calling 'callback' 253 | setTimeout(callback, 1); 254 | } 255 | } 256 | 257 | //****************************************************************** 258 | //LCD_putchar 259 | //****************************************************************** 260 | 261 | function LCD_putchar(chr1, callback) 262 | { 263 | //Convert chr1 variable to UNICODE (ASCII) 264 | var chr = chr1.toString().charCodeAt(0); 265 | 266 | //parse character variable into individual bits for output 267 | //to LCD 268 | if((chr & 0x0080)== 0x0080) b.digitalWrite(LCD_DB7, b.HIGH); 269 | else b.digitalWrite(LCD_DB7, b.LOW); 270 | if((chr & 0x0040)== 0x0040) b.digitalWrite(LCD_DB6, b.HIGH); 271 | else b.digitalWrite(LCD_DB6, b.LOW); 272 | if((chr & 0x0020)== 0x0020) b.digitalWrite(LCD_DB5, b.HIGH); 273 | else b.digitalWrite(LCD_DB5, b.LOW); 274 | if((chr & 0x0010)== 0x0010) b.digitalWrite(LCD_DB4, b.HIGH); 275 | else b.digitalWrite(LCD_DB4, b.LOW); 276 | if((chr & 0x0008)== 0x0008) b.digitalWrite(LCD_DB3, b.HIGH); 277 | else b.digitalWrite(LCD_DB3, b.LOW); 278 | if((chr & 0x0004)== 0x0004) b.digitalWrite(LCD_DB2, b.HIGH); 279 | else b.digitalWrite(LCD_DB2, b.LOW); 280 | if((chr & 0x0002)== 0x0002) b.digitalWrite(LCD_DB1, b.HIGH); 281 | else b.digitalWrite(LCD_DB1, b.LOW); 282 | if((chr & 0x0001)== 0x0001) b.digitalWrite(LCD_DB0, b.HIGH); 283 | else b.digitalWrite(LCD_DB0, b.LOW); 284 | 285 | //LCD Register Set (RS) to logic one for character input 286 | b.digitalWrite(LCD_RS, b.HIGH); 287 | //LCD Enable (E) pin high 288 | b.digitalWrite(LCD_E, b.HIGH); 289 | 290 | //End the write after 1ms 291 | setTimeout(endWrite, 1); 292 | 293 | function endWrite() 294 | { 295 | //LCD Enable (E) pin low and call scheduleCallback when done 296 | b.digitalWrite(LCD_E, b.LOW); 297 | //delay 1ms before calling 'callback' 298 | setTimeout(callback, 1); 299 | } 300 | } 301 | 302 | //****************************************************************** 303 | -------------------------------------------------------------------------------- /chapter-5/am335x-bone-common.dtsi: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/ 3 | * 4 | * This program is free software; you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License version 2 as 6 | * published by the Free Software Foundation. 7 | */ 8 | 9 | /include/ "am33xx.dtsi" 10 | 11 | / { 12 | model = "TI AM335x BeagleBone"; 13 | compatible = "ti,am335x-bone", "ti,am33xx"; 14 | 15 | cpus { 16 | cpu@0 { 17 | cpu0-supply = <&dcdc2_reg>; 18 | }; 19 | }; 20 | 21 | memory { 22 | device_type = "memory"; 23 | reg = <0x80000000 0x10000000>; /* 256 MB */ 24 | }; 25 | 26 | am33xx_pinmux: pinmux@44e10800 { 27 | pinctrl-names = "default"; 28 | pinctrl-0 = <&userleds_pins>; 29 | 30 | userled_pins: pinmux_userled_pins { 31 | pinctrl-single,pins = < 32 | 0x54 0x7 /* gpmc_a5.gpio1_21, OUTPUT | MODE7 */ 33 | 0x58 0x17 /* gpmc_a6.gpio1_22, OUTPUT_PULLUP | MODE7 */ 34 | 0x5c 0x7 /* gpmc_a7.gpio1_23, OUTPUT | MODE7 */ 35 | 0x60 0x17 /* gpmc_a8.gpio1_24, OUTPUT_PULLUP | MODE7 */ 36 | >; 37 | }; 38 | i2c0_pins: pinmux_i2c0_pins { 39 | pinctrl-single,pins = < 40 | 0x188 0x70 /* i2c0_sda, SLEWCTRL_SLOW | INPUT_PULLUP | MODE0 */ 41 | 0x18c 0x70 /* i2c0_scl, SLEWCTRL_SLOW | INPUT_PULLUP | MODE0 */ 42 | >; 43 | }; 44 | i2c2_pins: pinmux_i2c2_pins { 45 | pinctrl-single,pins = < 46 | 0x178 0x73 /* uart1_ctsn.i2c2_sda, SLEWCTRL_SLOW | INPUT_PULLUP | MODE3 */ 47 | 0x17c 0x73 /* uart1_rtsn.i2c2_scl, SLEWCTRL_SLOW | INPUT_PULLUP | MODE3 */ 48 | >; 49 | }; 50 | }; 51 | 52 | ocp: ocp { 53 | uart1: serial@44e09000 { 54 | status = "okay"; 55 | }; 56 | 57 | gpio-leds { 58 | compatible = "gpio-leds"; 59 | pinctrl-names = "default"; 60 | pinctrl-0 = <&userled_pins>; 61 | 62 | led0 { 63 | label = "beaglebone:green:usr0"; 64 | gpios = <&gpio2 21 0>; 65 | linux,default-trigger = "heartbeat"; 66 | default-state = "off"; 67 | }; 68 | 69 | led1 { 70 | label = "beaglebone:green:usr1"; 71 | gpios = <&gpio2 22 0>; 72 | linux,default-trigger = "mmc0"; 73 | default-state = "off"; 74 | }; 75 | 76 | led2 { 77 | label = "beaglebone:green:usr2"; 78 | gpios = <&gpio2 23 0>; 79 | linux,default-trigger = "cpu0"; 80 | default-state = "off"; 81 | }; 82 | 83 | led3 { 84 | label = "beaglebone:green:usr3"; 85 | gpios = <&gpio2 24 0>; 86 | default-state = "off"; 87 | linux,default-trigger = "mmc1"; 88 | }; 89 | }; 90 | 91 | rtc@44e3e000 { 92 | ti,system-power-controller; 93 | }; 94 | }; 95 | 96 | bone_capemgr { 97 | compatible = "ti,bone-capemgr"; 98 | status = "okay"; 99 | 100 | eeprom = <&baseboard_eeprom>; 101 | 102 | baseboardmaps { 103 | baseboard_beaglebone: board@0 { 104 | board-name = "A335BONE"; 105 | compatible-name = "ti,beaglebone"; 106 | }; 107 | 108 | baseboard_beaglebone_black: board@1 { 109 | board-name = "A335BNLT"; 110 | compatible-name = "ti,beaglebone-black"; 111 | }; 112 | }; 113 | 114 | slots { 115 | slot@0 { 116 | eeprom = <&cape_eeprom0>; 117 | }; 118 | 119 | slot@1 { 120 | eeprom = <&cape_eeprom1>; 121 | }; 122 | 123 | slot@2 { 124 | eeprom = <&cape_eeprom2>; 125 | }; 126 | 127 | slot@3 { 128 | eeprom = <&cape_eeprom3>; 129 | }; 130 | 131 | /* Beaglebone black has it soldered on */ 132 | slot@4 { 133 | ti,cape-override; 134 | compatible = "ti,beaglebone-black"; 135 | board-name = "Bone-LT-eMMC-2G"; 136 | version = "00A0"; 137 | manufacturer = "Texas Instruments"; 138 | part-number = "BB-BONE-EMMC-2G"; 139 | }; 140 | 141 | /* geiger cape version A0 without an EEPROM */ 142 | slot@5 { 143 | ti,cape-override; 144 | compatible = "kernel-command-line", "runtime"; 145 | board-name = "Bone-Geiger"; 146 | version = "00A0"; 147 | manufacturer = "Geiger Inc."; 148 | part-number = "BB-BONE-GEIGER"; 149 | }; 150 | 151 | /* Beaglebone black has it soldered on */ 152 | slot@6 { 153 | ti,cape-override; 154 | compatible = "ti,beaglebone-black"; 155 | board-name = "Bone-Black-HDMI"; 156 | version = "00A0"; 157 | manufacturer = "Texas Instruments"; 158 | part-number = "BB-BONELT-HDMI"; 159 | }; 160 | 161 | /* Nixie cape version A0 without an EEPROM */ 162 | slot@7 { 163 | ti,cape-override; 164 | compatible = "kernel-command-line", "runtime"; 165 | board-name = "Bone-Nixie"; 166 | version = "00A0"; 167 | manufacturer = "Ranostay Industries"; 168 | part-number = "BB-BONE-NIXIE"; 169 | }; 170 | 171 | /* adafruit 1.8" TFT prototype cape */ 172 | slot@8 { 173 | ti,cape-override; 174 | compatible = "kernel-command-line", "runtime"; 175 | board-name = "Bone-TFT"; 176 | version = "00A0"; 177 | manufacturer = "Adafruit"; 178 | part-number = "BB-BONE-TFT-01"; 179 | }; 180 | 181 | /* adafruit RTC DS1307 prototype cape */ 182 | slot@9 { 183 | ti,cape-override; 184 | compatible = "kernel-command-line", "runtime"; 185 | board-name = "Bone-RTC"; 186 | version = "00A0"; 187 | manufacturer = "Adafruit"; 188 | part-number = "BB-BONE-RTC-01"; 189 | }; 190 | 191 | slot@10 { 192 | ti,cape-override; 193 | compatible = "kernel-command-line", "runtime"; 194 | board-name = "Bone-Hexy"; 195 | version = "00A0"; 196 | manufacturer = "Koen Kooi"; 197 | part-number = "BB-BONE-HEXY-01"; 198 | }; 199 | /* MRF24J40 Cape Override */ 200 | slot@11 { 201 | ti,cape-override; 202 | compatible = "kernel-command-line", "runtime"; 203 | board-name = "Bone-MRF24J40"; 204 | version = "00A0"; 205 | manufacturer = "Signal 11 Software"; 206 | part-number = "BB-BONE-MRF24J40"; 207 | }; 208 | }; 209 | 210 | /* mapping between board names and dtb objects */ 211 | capemaps { 212 | /* DVI cape */ 213 | cape@0 { 214 | /* board-name = "BeagleBone DVI-D CAPE"; */ 215 | part-number = "BB-BONE-DVID-01"; 216 | version@00A0 { 217 | version = "00A0"; 218 | dtbo = "cape-bone-dvi-00A0.dtbo"; 219 | }; 220 | version@00A1 { 221 | version = "00A1", "01"; 222 | dtbo = "cape-bone-dvi-00A1.dtbo"; 223 | }; 224 | version@00A2 { 225 | version = "00A2", "A2"; 226 | dtbo = "cape-bone-dvi-00A2.dtbo"; 227 | }; 228 | version@00A3 { 229 | version = "00A3"; 230 | dtbo = "cape-bone-dvi-00A2.dtbo"; 231 | }; 232 | }; 233 | 234 | /* beaglebone black emmc on board */ 235 | cape@1 { 236 | /* board-name = "BeagleBone 2G eMMC1 CAPE"; */ 237 | part-number = "BB-BONE-EMMC-2G"; 238 | version@00A0 { 239 | version = "00A0"; 240 | dtbo = "cape-bone-2g-emmc1.dtbo"; 241 | }; 242 | }; 243 | 244 | /* geiger cape */ 245 | cape@2 { 246 | part-number = "BB-BONE-GEIGER"; 247 | version@00A0 { 248 | version = "00A0"; 249 | dtbo = "cape-bone-geiger-00A0.dtbo"; 250 | }; 251 | }; 252 | 253 | /* LCD3 cape */ 254 | cape@3 { 255 | part-number = "BB-BONE-LCD3-01"; 256 | version@00A0 { 257 | version = "00A0"; 258 | dtbo = "cape-bone-lcd3-00A0.dtbo"; 259 | }; 260 | version@00A2 { 261 | version = "00A2"; 262 | dtbo = "cape-bone-lcd3-00A2.dtbo"; 263 | }; 264 | }; 265 | 266 | /* Weather cape */ 267 | cape@4 { 268 | part-number = "BB-BONE-WTHR-01"; 269 | version@00A0 { 270 | version = "00A0"; 271 | dtbo = "cape-bone-weather-00A0.dtbo"; 272 | }; 273 | }; 274 | 275 | /* beaglebone black hdmi on board */ 276 | cape@5 { 277 | part-number = "BB-BONELT-HDMI"; 278 | version@00A0 { 279 | version = "00A0"; 280 | dtbo = "cape-boneblack-hdmi-00A0.dtbo"; 281 | }; 282 | }; 283 | 284 | /* nixie cape */ 285 | cape@6 { 286 | part-number = "BB-BONE-NIXIE"; 287 | version@00A0 { 288 | version = "00A0"; 289 | dtbo = "cape-bone-nixie-00A0.dtbo"; 290 | }; 291 | }; 292 | cape@7 { 293 | part-number = "BB-BONE-TFT-01"; 294 | version@00A0 { 295 | version = "00A0"; 296 | dtbo = "cape-bone-adafruit-lcd-00A0.dtbo"; 297 | }; 298 | }; 299 | 300 | cape@8 { 301 | part-number = "BB-BONE-RTC-01"; 302 | version@00A0 { 303 | version = "00A0"; 304 | dtbo = "cape-bone-adafruit-rtc-00A0.dtbo"; 305 | }; 306 | }; 307 | 308 | cape@9 { 309 | part-number = "BB-BONE-HEXY-01"; 310 | version@00A0 { 311 | version = "00A0"; 312 | dtbo = "cape-bone-hexy-00A0.dtbo"; 313 | }; 314 | }; 315 | /* mrf24j40 cape */ 316 | cape@10 { 317 | part-number = "BB-BONE-MRF24J40"; 318 | version@00A0 { 319 | version = "00A0"; 320 | dtbo = "cape-bone-mrf24j40-00A0.dtbo"; 321 | }; 322 | }; 323 | /* expansion test */ 324 | cape@11 { 325 | part-number = "BB-BONE-EXPTEST"; 326 | version@00A0 { 327 | version = "00A0"; 328 | dtbo = "cape-bone-exptest-00A0.dtbo"; 329 | }; 330 | }; 331 | }; 332 | }; 333 | 334 | vmmcsd_fixed: fixedregulator@0 { 335 | compatible = "regulator-fixed"; 336 | regulator-name = "vmmcsd_fixed"; 337 | regulator-min-microvolt = <3300000>; 338 | regulator-max-microvolt = <3300000>; 339 | }; 340 | 341 | }; 342 | 343 | &i2c0 { 344 | status = "okay"; 345 | clock-frequency = <400000>; 346 | pinctrl-names = "default"; 347 | pinctrl-0 = <&i2c0_pins>; 348 | 349 | tps: tps@24 { 350 | reg = <0x24>; 351 | }; 352 | 353 | baseboard_eeprom: baseboard_eeprom@50 { 354 | compatible = "at,24c256"; 355 | reg = <0x50>; 356 | }; 357 | }; 358 | 359 | &i2c2 { 360 | status = "okay"; 361 | pinctrl-names = "default"; 362 | pinctrl-0 = <&i2c2_pins>; 363 | 364 | clock-frequency = <100000>; 365 | 366 | cape_eeprom0: cape_eeprom0@54 { 367 | compatible = "at,24c256"; 368 | reg = <0x54>; 369 | }; 370 | 371 | cape_eeprom1: cape_eeprom1@55 { 372 | compatible = "at,24c256"; 373 | reg = <0x55>; 374 | }; 375 | 376 | cape_eeprom2: cape_eeprom2@56 { 377 | compatible = "at,24c256"; 378 | reg = <0x56>; 379 | }; 380 | 381 | cape_eeprom3: cape_eeprom3@57 { 382 | compatible = "at,24c256"; 383 | reg = <0x57>; 384 | }; 385 | }; 386 | 387 | /include/ "tps65217.dtsi" 388 | 389 | &tps { 390 | ti,pmic-shutdown-controller; 391 | 392 | regulators { 393 | dcdc1_reg: regulator@0 { 394 | regulator-always-on; 395 | }; 396 | 397 | dcdc2_reg: regulator@1 { 398 | /* VDD_MPU voltage limits 0.95V - 1.26V with +/-4% tolerance */ 399 | regulator-name = "vdd_mpu"; 400 | regulator-min-microvolt = <925000>; 401 | regulator-max-microvolt = <1325000>; 402 | regulator-boot-on; 403 | regulator-always-on; 404 | }; 405 | 406 | dcdc3_reg: regulator@2 { 407 | /* VDD_CORE voltage limits 0.95V - 1.1V with +/-4% tolerance */ 408 | regulator-name = "vdd_core"; 409 | regulator-min-microvolt = <925000>; 410 | regulator-max-microvolt = <1150000>; 411 | regulator-boot-on; 412 | regulator-always-on; 413 | }; 414 | 415 | ldo1_reg: regulator@3 { 416 | regulator-always-on; 417 | }; 418 | 419 | ldo2_reg: regulator@4 { 420 | regulator-always-on; 421 | }; 422 | 423 | ldo3_reg: regulator@5 { 424 | regulator-min-microvolt = <1800000>; 425 | regulator-max-microvolt = <3300000>; 426 | regulator-always-on; 427 | }; 428 | 429 | ldo4_reg: regulator@6 { 430 | regulator-always-on; 431 | }; 432 | }; 433 | }; 434 | 435 | &cpsw_emac0 { 436 | phy_id = <&davinci_mdio>, <0>; 437 | }; 438 | 439 | &cpsw_emac1 { 440 | phy_id = <&davinci_mdio>, <1>; 441 | }; 442 | 443 | &mmc1 { 444 | status = "okay"; 445 | vmmc-supply = <&ldo3_reg>; 446 | ti,vcc-aux-disable-is-sleep; 447 | }; 448 | 449 | &edma { 450 | ti,edma-xbar-event-map = <32 12>; 451 | }; 452 | 453 | &sham { 454 | status = "okay"; 455 | }; 456 | 457 | &aes { 458 | status = "okay"; 459 | }; 460 | 461 | &usb_otg_hs { 462 | interface_type = <1>; 463 | power = <250>; 464 | status = "okay"; 465 | }; 466 | -------------------------------------------------------------------------------- /appendix/LCD2-black.cpp: -------------------------------------------------------------------------------- 1 | //****************************************************************** 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define output "out" 10 | #define input "in" 11 | 12 | //function prototypes 13 | void configure_LCD_interface(void); 14 | void LCD_putcommand(unsigned char); 15 | void LCD_putchar(unsigned char); 16 | void LCD_print(unsigned int, char *string); 17 | void delay_us(int); 18 | void LCD_init(void); 19 | FILE *setup_gpio(unsigned int pin_number, char* direction); 20 | void set_gpio_value(FILE* value_file, unsigned int logic_status); 21 | 22 | //LCD_RS: bone.P8_9 LCD Register Set (RS) control: GPIO2_5: pin-69 23 | FILE* gpio_rs_value = NULL; 24 | int pin_number_rs = 69, logic_status_rs = 1; 25 | char* pin_direction_rs = output; 26 | 27 | //LCD_E: bone.P8_10: LCD Enable En (E): GPIO2_4: pin-68 28 | FILE* gpio_e_value = NULL; 29 | int pin_number_e = 68, logic_status_e = 1; 30 | char* pin_direction_e = output; 31 | 32 | //LCD_DB0: bone.P8_11: LCD Data line DB0: GPIO1_13: pin-45 33 | FILE *gpio_db0_value = NULL; 34 | int pin_number_db0 = 45, logic_status_db0 = 1; 35 | char* pin_direction_db0 = output; 36 | 37 | //LCD_DB1: bone.P8_12: LCD Data line DB1: GPIO1_12: pin-44 38 | FILE *gpio_db1_value = NULL; 39 | int pin_number_db1 = 44, logic_status_db1 = 1; 40 | char* pin_direction_db1 = output; 41 | 42 | //LCD_DB2: bone.P8_13: LCD Data line DB2: GPIO0_23: pin-23 43 | FILE *gpio_db2_value = NULL; 44 | int pin_number_db2 = 23, logic_status_db2 = 1; 45 | char* pin_direction_db2 = output; 46 | 47 | //LCD_DB3: bone.P8_14: LCD Data line DB3: GPIO0_26: pin-26 48 | FILE *gpio_db3_value = NULL; 49 | int pin_number_db3 = 26, logic_status_db3 = 1; 50 | char* pin_direction_db3 = output; 51 | 52 | //LCD_DB4: bone.P8_15: LCD Data line DB4: GPIO1_15: pin-47 53 | FILE *gpio_db4_value = NULL; 54 | int pin_number_db4 = 47, logic_status_db4 = 1; 55 | char* pin_direction_db4 = output; 56 | 57 | //LCD_DB5: bone.P8_16: LCD Data line DB5: GPIO1_14: pin-46 58 | FILE *gpio_db5_value = NULL; 59 | int pin_number_db5 = 46, logic_status_db5 = 1; 60 | char* pin_direction_db5 = output; 61 | 62 | //LCD_DB6: bone.P8_17: LCD Data line DB6: GPIO0_27: pin-27 63 | FILE *gpio_db6_value = NULL; 64 | int pin_number_db6 = 27, logic_status_db6 = 1; 65 | char* pin_direction_db6 = output; 66 | 67 | //LCD_DB7: bone.P8_18: LCD Data line DB7: GPIO2_1: pin-65 68 | FILE *gpio_db7_value = NULL; 69 | int pin_number_db7 = 65, logic_status_db7 = 1; 70 | char* pin_direction_db7 = output; 71 | 72 | 73 | int main(void) 74 | { 75 | configure_LCD_interface(); 76 | printf("Configure LCD \n"); 77 | LCD_init(); //call LCD initialize 78 | printf("LCD initialize \n"); 79 | 80 | while(1) 81 | { 82 | LCD_print(1, "Bad to the"); 83 | printf("Bad to the\n"); 84 | delay_us(100); 85 | LCD_print(2, " Bone"); 86 | printf(" Bone\n"); 87 | delay_us(100); 88 | } 89 | return 1; 90 | } 91 | 92 | //****************************************************************** 93 | //FILE *setup_gpio(unsigned int pin_number, char* pin_direction) 94 | //****************************************************************** 95 | 96 | FILE *setup_gpio(unsigned int pin_number, char* pin_direction) 97 | { 98 | FILE *exp, *gpio_value, *gpio_direction; 99 | char gpio_direction_filename[40]; 100 | char gpio_value_filename[40]; 101 | 102 | //create direction and value file for pin 103 | exp = fopen("/sys/class/gpio/export", "w"); 104 | if(exp == NULL) {printf("Unable to open export.\n");} 105 | fseek(exp, 0, SEEK_SET); 106 | fprintf(exp, "%d", pin_number); 107 | fflush(exp); 108 | fclose(exp); 109 | 110 | //configure pin direction 111 | sprintf(gpio_direction_filename, "/sys/class/gpio/gpio%d/direction", pin_number); 112 | gpio_direction = fopen(gpio_direction_filename, "w"); 113 | if(gpio_direction == NULL) {printf("Unable to open %s.\n", gpio_direction_filename);} 114 | fseek(gpio_direction, 0, SEEK_SET); 115 | fprintf(gpio_direction, "%s", pin_direction); 116 | fflush(gpio_direction); 117 | fclose(gpio_direction); 118 | 119 | //open pin value file 120 | sprintf(gpio_value_filename, "/sys/class/gpio/gpio%d/value", pin_number); 121 | gpio_value = fopen(gpio_value_filename, "w"); 122 | if(gpio_value == NULL) {printf("Unable to open %s.\n", gpio_value_filename);} 123 | 124 | printf("Opening %d (%s) returned 0x%08x\n", pin_number, gpio_value_filename, (int)gpio_value); 125 | return(gpio_value); 126 | } 127 | 128 | //****************************************************************** 129 | //void set_gpio_value(FILE* value_file, unsigned int logic_status) 130 | //****************************************************************** 131 | void set_gpio_value(FILE* value_file, unsigned int logic_status) 132 | { 133 | 134 | printf(" %d -> 0x%08x\n", logic_status, (int)value_file); 135 | fseek(value_file, 0, SEEK_SET); 136 | fprintf(value_file, "%d", logic_status); 137 | fflush(value_file); 138 | 139 | } 140 | 141 | //****************************************************************** 142 | //void configure_LCD_interface(void) 143 | //****************************************************************** 144 | 145 | void configure_LCD_interface(void) 146 | { 147 | 148 | //Setup LCD GPIO pins 149 | gpio_rs_value = setup_gpio(pin_number_rs, pin_direction_rs); 150 | gpio_e_value = setup_gpio(pin_number_e, pin_direction_e); 151 | gpio_db0_value = setup_gpio(pin_number_db0, pin_direction_db0); 152 | gpio_db1_value = setup_gpio(pin_number_db1, pin_direction_db1); 153 | gpio_db2_value = setup_gpio(pin_number_db2, pin_direction_db2); 154 | gpio_db3_value = setup_gpio(pin_number_db3, pin_direction_db3); 155 | gpio_db4_value = setup_gpio(pin_number_db4, pin_direction_db4); 156 | gpio_db5_value = setup_gpio(pin_number_db5, pin_direction_db5); 157 | gpio_db6_value = setup_gpio(pin_number_db6, pin_direction_db6); 158 | gpio_db7_value = setup_gpio(pin_number_db7, pin_direction_db7); 159 | 160 | } 161 | 162 | //****************************************************************** 163 | //LCD_init 164 | //****************************************************************** 165 | 166 | void LCD_init(void) 167 | { 168 | delay_us(15000); //wait 15 ms 169 | LCD_putcommand(0x30); //set for 8-bit operation 170 | delay_us(5000); //delay 5 ms 171 | LCD_putcommand(0x30); //set for 8-bit operation 172 | delay_us(100); //delay 100 us 173 | LCD_putcommand(0x30); //set for 8-bit operation 174 | LCD_putcommand(0x38); //function set 175 | LCD_putcommand(0x80); //display off 176 | LCD_putcommand(0x01); //display clear 177 | LCD_putcommand(0x06); //entry mode set 178 | } 179 | 180 | //****************************************************************** 181 | //LCD_putcommand 182 | //****************************************************************** 183 | 184 | void LCD_putcommand(unsigned char cmd) 185 | { 186 | //parse command variable into individual bits for output 187 | //to LCD 188 | 189 | //configure DB7 value 190 | if((cmd & 0x0080)== 0x0080) 191 | { 192 | printf("CmDB7:1"); 193 | logic_status_db7 = 1; 194 | } 195 | else 196 | { 197 | printf(" CmDB7:0"); 198 | logic_status_db7 = 0; 199 | } 200 | set_gpio_value(gpio_db7_value, logic_status_db7); 201 | 202 | 203 | //configure DB6 value 204 | if((cmd & 0x0040)== 0x0040) 205 | { 206 | printf(" CmDB6:1"); 207 | logic_status_db6 = 1; 208 | } 209 | else 210 | { 211 | printf(" CmDB6:0"); 212 | logic_status_db6 = 0; 213 | } 214 | set_gpio_value(gpio_db6_value, logic_status_db6); 215 | 216 | 217 | //configure DB5 value 218 | if((cmd & 0x0020)== 0x0020) 219 | { 220 | printf(" CmDB5:1"); 221 | logic_status_db5 = 1; 222 | } 223 | else 224 | { 225 | printf(" CmDB5:0"); 226 | logic_status_db5 = 0; 227 | } 228 | set_gpio_value(gpio_db5_value, logic_status_db5); 229 | 230 | 231 | //configure DB4 value 232 | if((cmd & 0x0010)== 0x0010) 233 | { 234 | printf(" CmDB4:1"); 235 | logic_status_db4 = 1; 236 | } 237 | else 238 | { 239 | printf(" CmDB4:0"); 240 | logic_status_db4 = 0; 241 | } 242 | set_gpio_value(gpio_db4_value, logic_status_db4); 243 | 244 | 245 | //configure DB3 value 246 | if((cmd & 0x0008)== 0x0008) 247 | { 248 | printf(" CmDB3:1"); 249 | logic_status_db3 = 1; 250 | } 251 | else 252 | { 253 | printf(" CmDB3:0"); 254 | logic_status_db3 = 0; 255 | } 256 | set_gpio_value(gpio_db3_value, logic_status_db3); 257 | 258 | 259 | //configure DB2 value 260 | if((cmd & 0x0004)== 0x0004) 261 | { 262 | printf(" CmDB2:1"); 263 | logic_status_db2 = 1; 264 | } 265 | else 266 | { 267 | printf(" CmDB2:0"); 268 | logic_status_db2 = 0; 269 | } 270 | set_gpio_value(gpio_db2_value, logic_status_db2); 271 | 272 | 273 | //configure DB1 value 274 | if((cmd & 0x0002)== 0x0002) 275 | { 276 | printf(" CmDB1:1"); 277 | logic_status_db1 = 1; 278 | } 279 | else 280 | { 281 | printf(" CmDB1:0"); 282 | logic_status_db1 = 0; 283 | } 284 | set_gpio_value(gpio_db1_value, logic_status_db1); 285 | 286 | 287 | //configure DB0 value 288 | if((cmd & 0x0001)== 0x0001) 289 | { 290 | printf(" CmDB0:1"); 291 | logic_status_db0 = 1; 292 | } 293 | else 294 | { 295 | printf(" CmDB0:0"); 296 | logic_status_db0 = 0; 297 | } 298 | set_gpio_value(gpio_db0_value, logic_status_db0); 299 | 300 | printf("\n"); 301 | 302 | //LCD Register Set (RS) to logic zero for command input 303 | logic_status_rs = 0; 304 | set_gpio_value(gpio_rs_value, logic_status_rs); 305 | 306 | //LCD Enable (E) pin high 307 | logic_status_e = 1; 308 | set_gpio_value(gpio_e_value, logic_status_e); 309 | 310 | //delay 311 | delay_us(2); 312 | 313 | //LCD Enable (E) pin low 314 | logic_status_e = 0; 315 | set_gpio_value(gpio_e_value, logic_status_e); 316 | 317 | //delay 318 | delay_us(100); 319 | } 320 | 321 | //****************************************************************** 322 | //LCD_putchar 323 | //****************************************************************** 324 | 325 | void LCD_putchar(unsigned char chr) 326 | { 327 | //parse character variable into individual bits for output 328 | //to LCD 329 | 330 | printf("Data: %c:%d", chr, chr); 331 | chr = (int)(chr); 332 | 333 | //configure DB7 value 334 | if((chr & 0x0080)== 0x0080) 335 | { 336 | printf(" DB7:1"); 337 | logic_status_db7 = 1; 338 | } 339 | else 340 | { 341 | printf(" DB7:0"); 342 | logic_status_db7 = 0; 343 | } 344 | set_gpio_value(gpio_db7_value, logic_status_db7); 345 | 346 | 347 | //configure DB6 value 348 | if((chr & 0x0040)== 0x0040) 349 | { 350 | printf(" DB6:1"); 351 | logic_status_db6 = 1; 352 | } 353 | else 354 | { 355 | printf(" DB6:0"); 356 | logic_status_db6 = 0; 357 | } 358 | set_gpio_value(gpio_db6_value, logic_status_db6); 359 | 360 | 361 | //configure DB5 value 362 | if((chr & 0x0020)== 0x0020) 363 | { 364 | printf(" DB5:1"); 365 | logic_status_db5 = 1; 366 | } 367 | else 368 | { 369 | printf(" DB5:0"); 370 | logic_status_db5 = 0; 371 | } 372 | set_gpio_value(gpio_db5_value, logic_status_db5); 373 | 374 | 375 | //configure DB4 value 376 | if((chr & 0x0010)== 0x0010) 377 | { 378 | printf(" DB4:1"); 379 | logic_status_db4 = 1; 380 | } 381 | else 382 | { 383 | printf(" DB4:0"); 384 | logic_status_db4 = 0; 385 | } 386 | set_gpio_value(gpio_db4_value, logic_status_db4); 387 | 388 | 389 | //configure DB3 value 390 | if((chr & 0x0008)== 0x0008) 391 | { 392 | printf(" DB3:1"); 393 | logic_status_db3 = 1; 394 | } 395 | else 396 | { 397 | printf(" DB3:0"); 398 | logic_status_db3 = 0; 399 | } 400 | set_gpio_value(gpio_db3_value, logic_status_db3); 401 | 402 | 403 | //configure DB2 value 404 | if((chr & 0x0004)== 0x0004) 405 | { 406 | printf(" DB2:1"); 407 | logic_status_db2 = 1; 408 | } 409 | else 410 | { 411 | printf(" DB2:0"); 412 | logic_status_db2 = 0; 413 | } 414 | set_gpio_value(gpio_db2_value, logic_status_db2); 415 | 416 | 417 | //configure DB1 value 418 | if((chr & 0x0002)== 0x0002) 419 | { 420 | printf(" DB1:1"); 421 | logic_status_db1 = 1; 422 | } 423 | else 424 | { 425 | printf(" DB1:0"); 426 | logic_status_db1 = 0; 427 | } 428 | set_gpio_value(gpio_db1_value, logic_status_db1); 429 | 430 | //configure DB0 value 431 | if((chr & 0x0001)== 0x0001) 432 | { 433 | printf(" DB0:1\n"); 434 | logic_status_db0 = 1; 435 | } 436 | else 437 | { 438 | printf(" DB0:0\n"); 439 | logic_status_db0 = 0; 440 | } 441 | set_gpio_value(gpio_db0_value, logic_status_db0); 442 | 443 | //LCD Register Set (RS) to logic one for character input 444 | logic_status_rs = 1; 445 | set_gpio_value(gpio_rs_value, logic_status_rs); 446 | 447 | //LCD Enable (E) pin high 448 | logic_status_e = 1; 449 | set_gpio_value(gpio_e_value, logic_status_e); 450 | 451 | //delay 452 | delay_us(2); 453 | 454 | //LCD Enable (E) pin low 455 | logic_status_e = 0; 456 | set_gpio_value(gpio_e_value, logic_status_e); 457 | 458 | //delay 459 | delay_us(2); 460 | 461 | } 462 | 463 | //****************************************************************** 464 | //****************************************************************** 465 | void LCD_print(unsigned int line, char *msg) 466 | { 467 | int i = 0; 468 | 469 | if(line == 1) 470 | { 471 | LCD_putcommand(0x80); //print to LCD line 1 472 | } 473 | else 474 | { 475 | LCD_putcommand(0xc0); //print to LCD line 2 476 | } 477 | 478 | while(*(msg) != '\0') 479 | { 480 | LCD_putchar(*msg); 481 | //printf("Data: %c\n\n", *msg); 482 | msg++; 483 | } 484 | } 485 | 486 | //****************************************************************** 487 | 488 | void delay_us(int desired_delay_us) 489 | { 490 | struct timeval tv_start; //start time hack 491 | struct timeval tv_now; //current time hack 492 | int elapsed_time_us; 493 | 494 | gettimeofday(&tv_start, NULL); 495 | elapsed_time_us = 0; 496 | 497 | while(elapsed_time_us < desired_delay_us) 498 | { 499 | gettimeofday(&tv_now, NULL); 500 | if(tv_now.tv_usec >= tv_start.tv_usec) 501 | elapsed_time_us = tv_now.tv_usec - tv_start.tv_usec; 502 | else 503 | elapsed_time_us = (1000000 - tv_start.tv_usec) + tv_now.tv_usec; 504 | //printf("start: %ld \n", tv_start.tv_usec); 505 | //printf("now: %ld \n", tv_now.tv_usec); 506 | //printf("desired: %d \n", desired_delay_ms); 507 | //printf("elapsed: %d \n\n", elapsed_time_ms); 508 | } 509 | } 510 | 511 | //****************************************************************** 512 | 513 | 514 | 515 | -------------------------------------------------------------------------------- /chapter-5/am33xx.dtsi: -------------------------------------------------------------------------------- 1 | /* 2 | * Device Tree Source for AM33XX SoC 3 | * 4 | * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/ 5 | * 6 | * This file is licensed under the terms of the GNU General Public License 7 | * version 2. This program is licensed "as is" without any warranty of any 8 | * kind, whether express or implied. 9 | */ 10 | 11 | /include/ "skeleton.dtsi" 12 | 13 | / { 14 | compatible = "ti,am33xx"; 15 | interrupt-parent = <&intc>; 16 | 17 | aliases { 18 | serial0 = &uart1; 19 | serial1 = &uart2; 20 | serial2 = &uart3; 21 | serial3 = &uart4; 22 | serial4 = &uart5; 23 | serial5 = &uart6; 24 | }; 25 | 26 | cpus { 27 | cpu: cpu@0 { 28 | compatible = "arm,cortex-a8"; 29 | 30 | /* 31 | * To consider voltage drop between PMIC and SoC, 32 | * tolerance value is reduced to 2% from 4% and 33 | * voltage value is increased as a precaution. 34 | */ 35 | operating-points = < 36 | /* kHz uV */ 37 | /* ES 2.0 Nitro and Turbo OPPs" 38 | 1000000 1350000 39 | 800000 1300000 40 | */ 41 | 720000 1285000 42 | 600000 1225000 43 | 500000 1125000 44 | 275000 1125000 45 | >; 46 | voltage-tolerance = <2>; /* 2 percentage */ 47 | clock-latency = <300000>; /* From omap-cpufreq driver */ 48 | }; 49 | }; 50 | 51 | /* 52 | * The soc node represents the soc top level view. It is uses for IPs 53 | * that are not memory mapped in the MPU view or for the MPU itself. 54 | */ 55 | soc { 56 | compatible = "ti,omap-infra"; 57 | mpu { 58 | compatible = "ti,omap3-mpu"; 59 | ti,hwmods = "mpu"; 60 | }; 61 | }; 62 | 63 | am33xx_pinmux: pinmux@44e10800 { 64 | compatible = "pinctrl-single"; 65 | reg = <0x44e10800 0x0238>; 66 | #address-cells = <1>; 67 | #size-cells = <0>; 68 | pinctrl-single,register-width = <32>; 69 | pinctrl-single,function-mask = <0x7f>; 70 | }; 71 | 72 | /* 73 | * XXX: Use a flat representation of the AM33XX interconnect. 74 | * The real AM33XX interconnect network is quite complex.Since 75 | * that will not bring real advantage to represent that in DT 76 | * for the moment, just use a fake OCP bus entry to represent 77 | * the whole bus hierarchy. 78 | */ 79 | ocp { 80 | compatible = "simple-bus"; 81 | #address-cells = <1>; 82 | #size-cells = <1>; 83 | ranges; 84 | ti,hwmods = "l3_main"; 85 | 86 | intc: interrupt-controller@48200000 { 87 | compatible = "ti,omap2-intc"; 88 | interrupt-controller; 89 | #interrupt-cells = <1>; 90 | ti,intc-size = <128>; 91 | reg = <0x48200000 0x1000>; 92 | }; 93 | 94 | edma: edma@49000000 { 95 | compatible = "ti,edma3"; 96 | ti,hwmods = "tpcc", "tptc0", "tptc1", "tptc2"; 97 | reg = <0x49000000 0x10000>, 98 | <0x44e10f90 0x10>; 99 | interrupt-parent = <&intc>; 100 | interrupts = <12 13 14>; 101 | #dma-cells = <1>; 102 | dma-channels = <64>; 103 | ti,edma-regions = <4>; 104 | ti,edma-slots = <256>; 105 | ti,edma-queue-tc-map = <0 0 106 | 1 1 107 | 2 2>; 108 | ti,edma-queue-priority-map = <0 0 109 | 1 1 110 | 2 2>; 111 | ti,edma-default-queue = <0>; 112 | }; 113 | 114 | gpio1: gpio@44e07000 { 115 | compatible = "ti,omap4-gpio"; 116 | ti,hwmods = "gpio1"; 117 | gpio-controller; 118 | #gpio-cells = <2>; 119 | interrupt-controller; 120 | #interrupt-cells = <1>; 121 | reg = <0x44e07000 0x1000>; 122 | interrupts = <96>; 123 | }; 124 | 125 | gpio2: gpio@4804c000 { 126 | compatible = "ti,omap4-gpio"; 127 | ti,hwmods = "gpio2"; 128 | gpio-controller; 129 | #gpio-cells = <2>; 130 | interrupt-controller; 131 | #interrupt-cells = <1>; 132 | reg = <0x4804c000 0x1000>; 133 | interrupts = <98>; 134 | }; 135 | 136 | gpio3: gpio@481ac000 { 137 | compatible = "ti,omap4-gpio"; 138 | ti,hwmods = "gpio3"; 139 | gpio-controller; 140 | #gpio-cells = <2>; 141 | interrupt-controller; 142 | #interrupt-cells = <1>; 143 | reg = <0x481ac000 0x1000>; 144 | interrupts = <32>; 145 | }; 146 | 147 | gpio4: gpio@481ae000 { 148 | compatible = "ti,omap4-gpio"; 149 | ti,hwmods = "gpio4"; 150 | gpio-controller; 151 | #gpio-cells = <2>; 152 | interrupt-controller; 153 | #interrupt-cells = <1>; 154 | reg = <0x481ae000 0x1000>; 155 | interrupts = <62>; 156 | }; 157 | 158 | uart1: serial@44e09000 { 159 | compatible = "ti,omap3-uart"; 160 | ti,hwmods = "uart1"; 161 | clock-frequency = <48000000>; 162 | reg = <0x44e09000 0x2000>; 163 | interrupts = <72>; 164 | status = "disabled"; 165 | }; 166 | 167 | uart2: serial@48022000 { 168 | compatible = "ti,omap3-uart"; 169 | ti,hwmods = "uart2"; 170 | clock-frequency = <48000000>; 171 | reg = <0x48022000 0x2000>; 172 | interrupts = <73>; 173 | status = "disabled"; 174 | }; 175 | 176 | uart3: serial@48024000 { 177 | compatible = "ti,omap3-uart"; 178 | ti,hwmods = "uart3"; 179 | clock-frequency = <48000000>; 180 | reg = <0x48024000 0x2000>; 181 | interrupts = <74>; 182 | status = "disabled"; 183 | }; 184 | 185 | uart4: serial@481a6000 { 186 | compatible = "ti,omap3-uart"; 187 | ti,hwmods = "uart4"; 188 | clock-frequency = <48000000>; 189 | reg = <0x481a6000 0x2000>; 190 | interrupts = <44>; 191 | status = "disabled"; 192 | }; 193 | 194 | uart5: serial@481a8000 { 195 | compatible = "ti,omap3-uart"; 196 | ti,hwmods = "uart5"; 197 | clock-frequency = <48000000>; 198 | reg = <0x481a8000 0x2000>; 199 | interrupts = <45>; 200 | status = "disabled"; 201 | }; 202 | 203 | uart6: serial@481aa000 { 204 | compatible = "ti,omap3-uart"; 205 | ti,hwmods = "uart6"; 206 | clock-frequency = <48000000>; 207 | reg = <0x481aa000 0x2000>; 208 | interrupts = <46>; 209 | status = "disabled"; 210 | }; 211 | 212 | i2c0: i2c@44e0b000 { 213 | compatible = "ti,omap4-i2c"; 214 | #address-cells = <1>; 215 | #size-cells = <0>; 216 | ti,hwmods = "i2c1"; /* TODO: Fix hwmod */ 217 | reg = <0x44e0b000 0x1000>; 218 | interrupts = <70>; 219 | status = "disabled"; 220 | }; 221 | 222 | i2c1: i2c@4802a000 { 223 | compatible = "ti,omap4-i2c"; 224 | #address-cells = <1>; 225 | #size-cells = <0>; 226 | ti,hwmods = "i2c2"; /* TODO: Fix hwmod */ 227 | reg = <0x4802a000 0x1000>; 228 | interrupts = <71>; 229 | status = "disabled"; 230 | }; 231 | 232 | i2c2: i2c@4819c000 { 233 | compatible = "ti,omap4-i2c"; 234 | #address-cells = <1>; 235 | #size-cells = <0>; 236 | ti,hwmods = "i2c3"; /* TODO: Fix hwmod */ 237 | reg = <0x4819c000 0x1000>; 238 | interrupts = <30>; 239 | status = "disabled"; 240 | }; 241 | 242 | mmc1: mmc@48060000 { 243 | compatible = "ti,omap3-hsmmc"; 244 | ti,hwmods = "mmc1"; 245 | ti,dual-volt; 246 | ti,needs-special-reset; 247 | ti,needs-special-hs-handling; 248 | dmas = <&edma 24 249 | &edma 25>; 250 | dma-names = "tx", "rx"; 251 | status = "disabled"; 252 | }; 253 | 254 | mmc2: mmc@481d8000 { 255 | compatible = "ti,omap3-hsmmc"; 256 | ti,hwmods = "mmc2"; 257 | ti,needs-special-reset; 258 | ti,needs-special-hs-handling; 259 | dmas = <&edma 2 260 | &edma 3>; 261 | dma-names = "tx", "rx"; 262 | status = "disabled"; 263 | }; 264 | 265 | mmc3: mmc@47810000 { 266 | compatible = "ti,omap3-hsmmc"; 267 | ti,hwmods = "mmc3"; 268 | ti,needs-special-reset; 269 | ti,needs-special-hs-handling; 270 | status = "disabled"; 271 | }; 272 | 273 | wdt2: wdt@44e35000 { 274 | compatible = "ti,omap3-wdt"; 275 | ti,hwmods = "wd_timer2"; 276 | reg = <0x44e35000 0x1000>; 277 | interrupts = <91>; 278 | }; 279 | 280 | dcan0: d_can@481cc000 { 281 | compatible = "bosch,d_can"; 282 | ti,hwmods = "d_can0"; 283 | reg = <0x481cc000 0x2000>; 284 | interrupts = <52>; 285 | status = "disabled"; 286 | }; 287 | 288 | dcan1: d_can@481d0000 { 289 | compatible = "bosch,d_can"; 290 | ti,hwmods = "d_can1"; 291 | reg = <0x481d0000 0x2000>; 292 | interrupts = <55>; 293 | status = "disabled"; 294 | }; 295 | 296 | timer1: timer@44e31000 { 297 | compatible = "ti,omap2-timer"; 298 | reg = <0x44e31000 0x400>; 299 | interrupts = <67>; 300 | ti,hwmods = "timer1"; 301 | ti,timer-alwon; 302 | }; 303 | 304 | timer2: timer@48040000 { 305 | compatible = "ti,omap2-timer"; 306 | reg = <0x48040000 0x400>; 307 | interrupts = <68>; 308 | ti,hwmods = "timer2"; 309 | }; 310 | 311 | timer3: timer@48042000 { 312 | compatible = "ti,omap2-timer"; 313 | reg = <0x48042000 0x400>; 314 | interrupts = <69>; 315 | ti,hwmods = "timer3"; 316 | }; 317 | 318 | timer4: timer@48044000 { 319 | compatible = "ti,omap2-timer"; 320 | reg = <0x48044000 0x400>; 321 | interrupts = <92>; 322 | ti,hwmods = "timer4"; 323 | ti,timer-pwm; 324 | }; 325 | 326 | timer5: timer@48046000 { 327 | compatible = "ti,omap2-timer"; 328 | reg = <0x48046000 0x400>; 329 | interrupts = <93>; 330 | ti,hwmods = "timer5"; 331 | ti,timer-pwm; 332 | }; 333 | 334 | timer6: timer@48048000 { 335 | compatible = "ti,omap2-timer"; 336 | reg = <0x48048000 0x400>; 337 | interrupts = <94>; 338 | ti,hwmods = "timer6"; 339 | ti,timer-pwm; 340 | }; 341 | 342 | timer7: timer@4804a000 { 343 | compatible = "ti,omap2-timer"; 344 | reg = <0x4804a000 0x400>; 345 | interrupts = <95>; 346 | ti,hwmods = "timer7"; 347 | ti,timer-pwm; 348 | }; 349 | 350 | pruss: pruss@4a300000 { 351 | compatible = "ti,pruss-v2"; 352 | ti,hwmods = "pruss"; 353 | ti,deassert-hard-reset = "pruss", "pruss"; 354 | reg = <0x4a300000 0x080000>; 355 | ti,pintc-offset = <0x20000>; 356 | interrupt-parent = <&intc>; 357 | status = "disabled"; 358 | interrupts = <20 21 22 23 24 25 26 27>; 359 | }; 360 | 361 | rtc@44e3e000 { 362 | compatible = "ti,da830-rtc"; 363 | reg = <0x44e3e000 0x1000>; 364 | interrupts = <75 365 | 76>; 366 | ti,hwmods = "rtc"; 367 | }; 368 | 369 | spi0: spi@48030000 { 370 | compatible = "ti,omap4-mcspi"; 371 | #address-cells = <1>; 372 | #size-cells = <0>; 373 | reg = <0x48030000 0x400>; 374 | interrupt = <65>; 375 | ti,spi-num-cs = <2>; 376 | ti,hwmods = "spi0"; 377 | dmas = <&edma 16 378 | &edma 17 379 | &edma 18 380 | &edma 19>; 381 | dma-names = "tx0", "rx0", "tx1", "rx1"; 382 | status = "disabled"; 383 | }; 384 | 385 | spi1: spi@481a0000 { 386 | compatible = "ti,omap4-mcspi"; 387 | #address-cells = <1>; 388 | #size-cells = <0>; 389 | reg = <0x481a0000 0x400>; 390 | interrupt = <125>; 391 | ti,spi-num-cs = <2>; 392 | ti,hwmods = "spi1"; 393 | dmas = <&edma 42 394 | &edma 43 395 | &edma 44 396 | &edma 45>; 397 | dma-names = "tx0", "rx0", "tx1", "rx1"; 398 | status = "disabled"; 399 | 400 | }; 401 | 402 | nop-phy@0 { 403 | compatible = "nop-xceiv-usb"; 404 | }; 405 | 406 | nop-phy@1 { 407 | compatible = "nop-xceiv-usb"; 408 | }; 409 | 410 | usb_otg_hs: usb@47400000 { 411 | compatible = "ti,musb-am33xx"; 412 | reg = <0x47400000 0x1000 /* usbss */ 413 | 0x47401000 0x800 /* musb instance 0 */ 414 | 0x47401800 0x800>; /* musb instance 1 */ 415 | interrupts = <17 /* usbss */ 416 | 18 /* musb instance 0 */ 417 | 19>; /* musb instance 1 */ 418 | multipoint = <1>; 419 | num-eps = <16>; 420 | ram-bits = <12>; 421 | port0-mode = <3>; 422 | port1-mode = <1>; 423 | power = <250>; 424 | ti,hwmods = "usb_otg_hs"; 425 | status = "disabled"; 426 | }; 427 | 428 | mac: ethernet@4a100000 { 429 | compatible = "ti,cpsw"; 430 | ti,hwmods = "cpgmac0"; 431 | cpdma_channels = <8>; 432 | ale_entries = <1024>; 433 | bd_ram_size = <0x2000>; 434 | no_bd_ram = <0>; 435 | rx_descs = <64>; 436 | mac_control = <0x20>; 437 | slaves = <2>; 438 | cpts_active_slave = <0>; 439 | cpts_clock_mult = <0x80000000>; 440 | cpts_clock_shift = <29>; 441 | reg = <0x4a100000 0x800 442 | 0x4a101200 0x100>; 443 | #address-cells = <1>; 444 | #size-cells = <1>; 445 | interrupt-parent = <&intc>; 446 | /* 447 | * c0_rx_thresh_pend 448 | * c0_rx_pend 449 | * c0_tx_pend 450 | * c0_misc_pend 451 | */ 452 | interrupts = <40 41 42 43>; 453 | ranges; 454 | disable-napi; 455 | 456 | davinci_mdio: mdio@4a101000 { 457 | compatible = "ti,davinci_mdio"; 458 | #address-cells = <1>; 459 | #size-cells = <0>; 460 | ti,hwmods = "davinci_mdio"; 461 | bus_freq = <1000000>; 462 | reg = <0x4a101000 0x100>; 463 | }; 464 | 465 | cpsw_emac0: slave@4a100200 { 466 | /* Filled in by U-Boot */ 467 | mac-address = [ 00 00 00 00 00 00 ]; 468 | }; 469 | 470 | cpsw_emac1: slave@4a100300 { 471 | /* Filled in by U-Boot */ 472 | mac-address = [ 00 00 00 00 00 00 ]; 473 | }; 474 | }; 475 | 476 | tscadc: tscadc@44e0d000 { 477 | compatible = "ti,ti-tscadc"; 478 | reg = <0x44e0d000 0x1000>; 479 | interrupt-parent = <&intc>; 480 | interrupts = <16>; 481 | ti,hwmods = "adc_tsc"; 482 | status = "disabled"; 483 | }; 484 | 485 | lcdc: lcdc@4830e000 { 486 | compatible = "ti,am3352-lcdc", "ti,da830-lcdc"; 487 | reg = <0x4830e000 0x1000>; 488 | interrupts = <36>; 489 | status = "disabled"; 490 | ti,hwmods = "lcdc"; 491 | }; 492 | 493 | epwmss0: epwmss@48300000 { 494 | compatible = "ti,am33xx-pwmss"; 495 | reg = <0x48300000 0x10>; 496 | ti,hwmods = "epwmss0"; 497 | #address-cells = <1>; 498 | #size-cells = <1>; 499 | status = "disabled"; 500 | ranges = <0x48300100 0x48300100 0x80 /* ECAP */ 501 | 0x48300180 0x48300180 0x80 /* EQEP */ 502 | 0x48300200 0x48300200 0x80>; /* EHRPWM */ 503 | 504 | ecap0: ecap@48300100 { 505 | compatible = "ti,am33xx-ecap"; 506 | #pwm-cells = <3>; 507 | reg = <0x48300100 0x80>; 508 | ti,hwmods = "ecap0"; 509 | status = "disabled"; 510 | }; 511 | 512 | ehrpwm0: ehrpwm@48300200 { 513 | compatible = "ti,am33xx-ehrpwm"; 514 | #pwm-cells = <3>; 515 | reg = <0x48300200 0x80>; 516 | ti,hwmods = "ehrpwm0"; 517 | status = "disabled"; 518 | }; 519 | }; 520 | 521 | epwmss1: epwmss@48302000 { 522 | compatible = "ti,am33xx-pwmss"; 523 | reg = <0x48302000 0x10>; 524 | ti,hwmods = "epwmss1"; 525 | #address-cells = <1>; 526 | #size-cells = <1>; 527 | status = "disabled"; 528 | ranges = <0x48302100 0x48302100 0x80 /* ECAP */ 529 | 0x48302180 0x48302180 0x80 /* EQEP */ 530 | 0x48302200 0x48302200 0x80>; /* EHRPWM */ 531 | 532 | ecap1: ecap@48302100 { 533 | compatible = "ti,am33xx-ecap"; 534 | #pwm-cells = <3>; 535 | reg = <0x48302100 0x80>; 536 | ti,hwmods = "ecap1"; 537 | status = "disabled"; 538 | }; 539 | 540 | ehrpwm1: ehrpwm@48302200 { 541 | compatible = "ti,am33xx-ehrpwm"; 542 | #pwm-cells = <3>; 543 | reg = <0x48302200 0x80>; 544 | ti,hwmods = "ehrpwm1"; 545 | status = "disabled"; 546 | }; 547 | }; 548 | 549 | epwmss2: epwmss@48304000 { 550 | compatible = "ti,am33xx-pwmss"; 551 | reg = <0x48304000 0x10>; 552 | ti,hwmods = "epwmss2"; 553 | #address-cells = <1>; 554 | #size-cells = <1>; 555 | status = "disabled"; 556 | ranges = <0x48304100 0x48304100 0x80 /* ECAP */ 557 | 0x48304180 0x48304180 0x80 /* EQEP */ 558 | 0x48304200 0x48304200 0x80>; /* EHRPWM */ 559 | 560 | ecap2: ecap@48304100 { 561 | compatible = "ti,am33xx-ecap"; 562 | #pwm-cells = <3>; 563 | reg = <0x48304100 0x80>; 564 | ti,hwmods = "ecap2"; 565 | status = "disabled"; 566 | }; 567 | 568 | ehrpwm2: ehrpwm@48304200 { 569 | compatible = "ti,am33xx-ehrpwm"; 570 | #pwm-cells = <3>; 571 | reg = <0x48304200 0x80>; 572 | ti,hwmods = "ehrpwm2"; 573 | status = "disabled"; 574 | }; 575 | }; 576 | 577 | sham: sham@53100000 { 578 | compatible = "ti,omap4-sham"; 579 | ti,hwmods = "sham"; 580 | #address-cells = <1>; 581 | #size-cells = <0>; 582 | reg = <0x53100000 0x200>; 583 | interrupt-parent = <&intc>; 584 | interrupts = <109>; 585 | dmas = <&edma 36>; 586 | dma-names = "rx"; 587 | }; 588 | 589 | aes: aes@53500000 { 590 | compatible = "ti,omap4-aes"; 591 | ti,hwmods = "aes"; 592 | #address-cells = <1>; 593 | #size-cells = <0>; 594 | reg = <0x53500000 0xa0>; 595 | interrupt-parent = <&intc>; 596 | interrupts = <102>; 597 | dmas = <&edma 6 598 | &edma 5>; 599 | dma-names = "tx", "rx"; 600 | }; 601 | 602 | mcasp0: mcasp@48038000 { 603 | compatible = "ti,omap2-mcasp-audio"; 604 | #address-cells = <1>; 605 | #size-cells = <0>; 606 | ti,hwmods = "mcasp0"; 607 | reg = <0x48038000 0x2000>; 608 | interrupts = <80 81>; 609 | status = "disabled"; 610 | asp-chan-q = <2>; /* EVENTQ_2 */ 611 | tx-dma-offset = <0x46000000>; 612 | rx-dma-offset = <0x46000000>; 613 | dmas = <&edma 8 614 | &edma 9>; 615 | dma-names = "tx", "rx"; 616 | }; 617 | 618 | mcasp1: mcasp@4803C000 { 619 | compatible = "ti,omap2-mcasp-audio"; 620 | #address-cells = <1>; 621 | #size-cells = <0>; 622 | ti,hwmods = "mcasp1"; 623 | reg = <0x4803C000 0x2000>; 624 | interrupts = <82 83>; 625 | status = "disabled"; 626 | asp-chan-q = <2>; /* EVENTQ_2 */ 627 | tx-dma-offset = <0x46400000>; 628 | rx-dma-offset = <0x46400000>; 629 | dmas = <&edma 10 630 | &edma 11>; 631 | dma-names = "tx", "rx"; 632 | }; 633 | 634 | }; 635 | }; 636 | -------------------------------------------------------------------------------- /appendix/LCD2.cpp: -------------------------------------------------------------------------------- 1 | //****************************************************************** 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define output "out" 10 | #define input "in" 11 | 12 | //function prototypes 13 | void configure_LCD_interface(void); 14 | void LCD_putcommand(unsigned char); 15 | void LCD_putchar(unsigned char); 16 | void LCD_print(unsigned int, char *string); 17 | void delay_us(int); 18 | void LCD_init(void); 19 | 20 | //LCD_RS: bone.P8_29 LCD Register Set (RS) control: GPIO2_23: pin-87 21 | FILE *ofp_lcd_hsync, *export_87, *gpio87_value, *gpio87_direction; 22 | int mux_mode_87 = 0x8007, pin_number_87 = 87, logic_status_87 = 1; 23 | char* pin_direction_87 = output; 24 | 25 | //LCD_E: bone.P8_28: LCD Enable En (E): GPIO2_24: pin-88 26 | FILE *ofp_lcd_pclk, *export_88, *gpio88_value, *gpio88_direction; 27 | int mux_mode_88 = 0x8007, pin_number_88 = 88, logic_status_88 = 1; 28 | char* pin_direction_88 = output; 29 | 30 | //LCD_DB0: bone.P8_45: LCD Data line DB0: GPIO2_6: pin-70 31 | FILE *ofp_lcd_data0, *export_70, *gpio70_value, *gpio70_direction; 32 | int mux_mode_70 = 0x8007, pin_number_70 = 70, logic_status_70 = 1; 33 | char* pin_direction_70 = output; 34 | 35 | //LCD_DB1: bone.P8_46: LCD Data line DB1: GPIO2_7: pin-71 36 | FILE *ofp_lcd_data1, *export_71, *gpio71_value, *gpio71_direction; 37 | int mux_mode_71 = 0x8007, pin_number_71 = 71, logic_status_71 = 1; 38 | char* pin_direction_71 = output; 39 | 40 | //LCD_DB2: bone.P8_43: LCD Data line DB2: GPIO2_8: pin-72 41 | FILE *ofp_lcd_data2, *export_72, *gpio72_value, *gpio72_direction; 42 | int mux_mode_72 = 0x8007, pin_number_72 = 72, logic_status_72 = 1; 43 | char* pin_direction_72 = output; 44 | 45 | //LCD_DB3: bone.P8_44: LCD Data line DB3: GPIO2_9: pin-73 46 | FILE *ofp_lcd_data3, *export_73, *gpio73_value, *gpio73_direction; 47 | int mux_mode_73 = 0x8007, pin_number_73 = 73, logic_status_73 = 1; 48 | char* pin_direction_73 = output; 49 | 50 | //LCD_DB4: bone.P8_41: LCD Data line DB4: GPIO2_10: pin-74 51 | FILE *ofp_lcd_data4, *export_74, *gpio74_value, *gpio74_direction; 52 | int mux_mode_74 = 0x8007, pin_number_74 = 74, logic_status_74 = 1; 53 | char* pin_direction_74 = output; 54 | 55 | //LCD_DB5: bone.P8_42: LCD Data line DB5: GPIO2_11: pin-75 56 | FILE *ofp_lcd_data5, *export_75, *gpio75_value, *gpio75_direction; 57 | int mux_mode_75 = 0x8007, pin_number_75 = 75, logic_status_75 = 1; 58 | char* pin_direction_75 = output; 59 | 60 | //LCD_DB6: bone.P8_39: LCD Data line DB6: GPIO2_12: pin-76 61 | FILE *ofp_lcd_data6, *export_76, *gpio76_value, *gpio76_direction; 62 | int mux_mode_76 = 0x8007, pin_number_76 = 76, logic_status_76 = 1; 63 | char* pin_direction_76 = output; 64 | 65 | //LCD_DB7: bone.P8_40: LCD Data line DB0: GPIO2_13: pin-77 66 | FILE *ofp_lcd_data7, *export_77, *gpio77_value, *gpio77_direction; 67 | int mux_mode_77 = 0x8007, pin_number_77 = 77, logic_status_77 = 1; 68 | char* pin_direction_77 = output; 69 | 70 | 71 | int main(void) 72 | { 73 | configure_LCD_interface(); 74 | printf("Configure LCD \n"); 75 | LCD_init(); //call LCD initialize 76 | printf("LCD initialize \n"); 77 | 78 | while(1) 79 | { 80 | LCD_print(1, "G"); 81 | printf("G\n"); 82 | delay_us(100); 83 | //LCD_print(2, "Bonescript"); 84 | //printf("Bonescript\n"); 85 | delay_us(100); 86 | } 87 | return 1; 88 | } 89 | 90 | //****************************************************************** 91 | //void configure_LCD_interface(void) 92 | //****************************************************************** 93 | 94 | void configure_LCD_interface(void) 95 | { 96 | 97 | //gpio87 mux setting 98 | ofp_lcd_hsync = fopen("/sys/kernel/debug/omap_mux/lcd_hsync", "w"); 99 | if(ofp_lcd_hsync == NULL) {printf("Unable to open lcd_hsync.\n");} 100 | fseek(ofp_lcd_hsync, 0, SEEK_SET); 101 | fprintf(ofp_lcd_hsync, "0x%02x", mux_mode_87); 102 | fflush(ofp_lcd_hsync); 103 | 104 | //create direction and value file for gpio87 105 | export_87 = fopen("/sys/class/gpio/export", "w"); 106 | if(export_87 == NULL) {printf("Unable to open export.\n");} 107 | fseek(export_87, 0, SEEK_SET); 108 | fprintf(export_87, "%d", pin_number_87); 109 | fflush(export_87); 110 | 111 | //configure gpio87 direction 112 | gpio87_direction = fopen("/sys/class/gpio/gpio87/direction", "w"); 113 | if(gpio87_direction == NULL) {printf("Unable to open gpio87_direction.\n");} 114 | fseek(gpio87_direction, 0, SEEK_SET); 115 | fprintf(gpio87_direction, "%s", pin_direction_87); 116 | fflush(gpio87_direction); 117 | 118 | 119 | //LCD_E 120 | //gpio88 mux setting 121 | ofp_lcd_pclk = fopen("/sys/kernel/debug/omap_mux/lcd_pclk", "w"); 122 | if(ofp_lcd_pclk == NULL) {printf("Unable to open lcd_pclk.\n");} 123 | fseek(ofp_lcd_pclk, 0, SEEK_SET); 124 | fprintf(ofp_lcd_pclk, "0x%02x", mux_mode_88); 125 | fflush(ofp_lcd_pclk); 126 | 127 | //create direction and value file for gpio88 128 | export_88 = fopen("/sys/class/gpio/export", "w"); 129 | if(export_88 == NULL) {printf("Unable to open export.\n");} 130 | fseek(export_88, 0, SEEK_SET); 131 | fprintf(export_88, "%d", pin_number_88); 132 | fflush(export_88); 133 | 134 | //configure gpio88 direction 135 | gpio88_direction = fopen("/sys/class/gpio/gpio88/direction", "w"); 136 | if(gpio88_direction == NULL) {printf("Unable to open gpio88_direction.\n");} 137 | fseek(gpio88_direction, 0, SEEK_SET); 138 | fprintf(gpio88_direction, "%s", pin_direction_88); 139 | fflush(gpio88_direction); 140 | 141 | 142 | //LCD_DB0 143 | //gpio70 mux setting 144 | ofp_lcd_data0 = fopen("/sys/kernel/debug/omap_mux/lcd_data0", "w"); 145 | if(ofp_lcd_data0 == NULL) {printf("Unable to open lcd_data0.\n");} 146 | fseek(ofp_lcd_data0, 0, SEEK_SET); 147 | fprintf(ofp_lcd_data0, "0x%02x", mux_mode_70); 148 | fflush(ofp_lcd_data0); 149 | 150 | //create direction and value file for gpio70 151 | export_70 = fopen("/sys/class/gpio/export", "w"); 152 | if(export_70 == NULL) {printf("Unable to open export.\n");} 153 | fseek(export_70, 0, SEEK_SET); 154 | fprintf(export_70, "%d", pin_number_70); 155 | fflush(export_70); 156 | 157 | //configure gpio70 direction 158 | gpio70_direction = fopen("/sys/class/gpio/gpio70/direction", "w"); 159 | if(gpio70_direction == NULL) {printf("Unable to open gpio70_direction.\n");} 160 | fseek(gpio70_direction, 0, SEEK_SET); 161 | fprintf(gpio70_direction, "%s", pin_direction_70); 162 | fflush(gpio70_direction); 163 | 164 | 165 | //LCD_DB1 166 | //gpio71 mux setting 167 | ofp_lcd_data1 = fopen("/sys/kernel/debug/omap_mux/lcd_data1", "w"); 168 | if(ofp_lcd_data1 == NULL) {printf("Unable to open lcd_data1.\n");} 169 | fseek(ofp_lcd_data1, 0, SEEK_SET); 170 | fprintf(ofp_lcd_data1, "0x%02x", mux_mode_71); 171 | fflush(ofp_lcd_data1); 172 | 173 | //create direction and value file for gpio71 174 | export_71 = fopen("/sys/class/gpio/export", "w"); 175 | if(export_71 == NULL) {printf("Unable to open export.\n");} 176 | fseek(export_71, 0, SEEK_SET); 177 | fprintf(export_71, "%d", pin_number_71); 178 | fflush(export_71); 179 | 180 | //configure gpio71 direction 181 | gpio71_direction = fopen("/sys/class/gpio/gpio71/direction", "w"); 182 | if(gpio71_direction == NULL) {printf("Unable to open gpio71_direction.\n");} 183 | fseek(gpio71_direction, 0, SEEK_SET); 184 | fprintf(gpio71_direction, "%s", pin_direction_71); 185 | fflush(gpio71_direction); 186 | 187 | 188 | //LCD_DB2 189 | //gpio72 mux setting 190 | ofp_lcd_data2 = fopen("/sys/kernel/debug/omap_mux/lcd_data2", "w"); 191 | if(ofp_lcd_data2 == NULL) {printf("Unable to open lcd_data2.\n");} 192 | fseek(ofp_lcd_data2, 0, SEEK_SET); 193 | fprintf(ofp_lcd_data2, "0x%02x", mux_mode_72); 194 | fflush(ofp_lcd_data2); 195 | 196 | //create direction and value file for gpio72 197 | export_72 = fopen("/sys/class/gpio/export", "w"); 198 | if(export_72 == NULL) {printf("Unable to open export.\n");} 199 | fseek(export_72, 0, SEEK_SET); 200 | fprintf(export_72, "%d", pin_number_72); 201 | fflush(export_72); 202 | 203 | //configure gpio72 direction 204 | gpio72_direction = fopen("/sys/class/gpio/gpio72/direction", "w"); 205 | if(gpio72_direction == NULL) {printf("Unable to open gpio72_direction.\n");} 206 | fseek(gpio72_direction, 0, SEEK_SET); 207 | fprintf(gpio72_direction, "%s", pin_direction_72); 208 | fflush(gpio72_direction); 209 | 210 | 211 | //LCD_DB3 212 | //gpio73 mux setting 213 | ofp_lcd_data3 = fopen("/sys/kernel/debug/omap_mux/lcd_data3", "w"); 214 | if(ofp_lcd_data3 == NULL) {printf("Unable to open lcd_data3.\n");} 215 | fseek(ofp_lcd_data3, 0, SEEK_SET); 216 | fprintf(ofp_lcd_data3, "0x%02x", mux_mode_73); 217 | fflush(ofp_lcd_data3); 218 | 219 | //create direction and value file for gpio73 220 | export_73 = fopen("/sys/class/gpio/export", "w"); 221 | if(export_73 == NULL) {printf("Unable to open export.\n");} 222 | fseek(export_73, 0, SEEK_SET); 223 | fprintf(export_73, "%d", pin_number_73); 224 | fflush(export_73); 225 | 226 | //configure gpio73 direction 227 | gpio73_direction = fopen("/sys/class/gpio/gpio73/direction", "w"); 228 | if(gpio73_direction == NULL) {printf("Unable to open gpio73_direction.\n");} 229 | fseek(gpio73_direction, 0, SEEK_SET); 230 | fprintf(gpio73_direction, "%s", pin_direction_73); 231 | fflush(gpio73_direction); 232 | 233 | 234 | //LCD_DB4 235 | //gpio74 mux setting 236 | ofp_lcd_data4 = fopen("/sys/kernel/debug/omap_mux/lcd_data4", "w"); 237 | if(ofp_lcd_data4 == NULL) {printf("Unable to open lcd_data4.\n");} 238 | fseek(ofp_lcd_data4, 0, SEEK_SET); 239 | fprintf(ofp_lcd_data4, "0x%02x", mux_mode_74); 240 | fflush(ofp_lcd_data4); 241 | 242 | //create direction and value file for gpio74 243 | export_74 = fopen("/sys/class/gpio/export", "w"); 244 | if(export_74 == NULL) {printf("Unable to open export.\n");} 245 | fseek(export_74, 0, SEEK_SET); 246 | fprintf(export_74, "%d", pin_number_74); 247 | fflush(export_74); 248 | 249 | //configure gpio74 direction 250 | gpio74_direction = fopen("/sys/class/gpio/gpio74/direction", "w"); 251 | if(gpio74_direction == NULL) {printf("Unable to open gpio74_direction.\n");} 252 | fseek(gpio74_direction, 0, SEEK_SET); 253 | fprintf(gpio74_direction, "%s", pin_direction_74); 254 | fflush(gpio74_direction); 255 | 256 | 257 | //LCD_DB5 258 | //gpio75 mux setting 259 | ofp_lcd_data5 = fopen("/sys/kernel/debug/omap_mux/lcd_data5", "w"); 260 | if(ofp_lcd_data5 == NULL) {printf("Unable to open lcd_data5.\n");} 261 | fseek(ofp_lcd_data5, 0, SEEK_SET); 262 | fprintf(ofp_lcd_data5, "0x%02x", mux_mode_75); 263 | fflush(ofp_lcd_data5); 264 | 265 | //create direction and value file for gpio75 266 | export_75 = fopen("/sys/class/gpio/export", "w"); 267 | if(export_75 == NULL) {printf("Unable to open export.\n");} 268 | fseek(export_75, 0, SEEK_SET); 269 | fprintf(export_75, "%d", pin_number_75); 270 | fflush(export_75); 271 | 272 | //configure gpio75 direction 273 | gpio75_direction = fopen("/sys/class/gpio/gpio75/direction", "w"); 274 | if(gpio75_direction == NULL) {printf("Unable to open gpio75_direction.\n");} 275 | fseek(gpio75_direction, 0, SEEK_SET); 276 | fprintf(gpio75_direction, "%s", pin_direction_75); 277 | fflush(gpio75_direction); 278 | 279 | 280 | //LCD_DB6 281 | //gpio76 mux setting 282 | ofp_lcd_data6 = fopen("/sys/kernel/debug/omap_mux/lcd_data6", "w"); 283 | if(ofp_lcd_data6 == NULL) {printf("Unable to open lcd_data6.\n");} 284 | fseek(ofp_lcd_data6, 0, SEEK_SET); 285 | fprintf(ofp_lcd_data6, "0x%02x", mux_mode_76); 286 | fflush(ofp_lcd_data6); 287 | 288 | //create direction and value file for gpio76 289 | export_76 = fopen("/sys/class/gpio/export", "w"); 290 | if(export_76 == NULL) {printf("Unable to open export.\n");} 291 | fseek(export_76, 0, SEEK_SET); 292 | fprintf(export_76, "%d", pin_number_76); 293 | fflush(export_76); 294 | 295 | //configure gpio76 direction 296 | gpio76_direction = fopen("/sys/class/gpio/gpio76/direction", "w"); 297 | if(gpio76_direction == NULL) {printf("Unable to open gpio76_direction.\n");} 298 | fseek(gpio76_direction, 0, SEEK_SET); 299 | fprintf(gpio76_direction, "%s", pin_direction_76); 300 | fflush(gpio76_direction); 301 | 302 | 303 | //LCD_DB7 304 | //gpio77 mux setting 305 | ofp_lcd_data7 = fopen("/sys/kernel/debug/omap_mux/lcd_data7", "w"); 306 | if(ofp_lcd_data7 == NULL) {printf("Unable to open lcd_data7.\n");} 307 | fseek(ofp_lcd_data7, 0, SEEK_SET); 308 | fprintf(ofp_lcd_data7, "0x%02x", mux_mode_77); 309 | fflush(ofp_lcd_data7); 310 | 311 | //create direction and value file for gpio77 312 | export_77 = fopen("/sys/class/gpio/export", "w"); 313 | if(export_77 == NULL) {printf("Unable to open export.\n");} 314 | fseek(export_77, 0, SEEK_SET); 315 | fprintf(export_77, "%d", pin_number_77); 316 | fflush(export_77); 317 | 318 | //configure gpio77 direction 319 | gpio77_direction = fopen("/sys/class/gpio/gpio77/direction", "w"); 320 | if(gpio77_direction == NULL) {printf("Unable to open gpio77_direction.\n");} 321 | fseek(gpio77_direction, 0, SEEK_SET); 322 | fprintf(gpio77_direction, "%s", pin_direction_77); 323 | fflush(gpio77_direction); 324 | } 325 | 326 | //****************************************************************** 327 | //LCD_init 328 | //****************************************************************** 329 | 330 | void LCD_init(void) 331 | { 332 | delay_us(15000); //wait 15 ms 333 | LCD_putcommand(0x30); //set for 8-bit operation 334 | delay_us(5000); //delay 5 ms 335 | LCD_putcommand(0x30); //set for 8-bit operation 336 | delay_us(100); //delay 100 us 337 | LCD_putcommand(0x30); //set for 8-bit operation 338 | LCD_putcommand(0x38); //function set 339 | LCD_putcommand(0x80); //display off 340 | LCD_putcommand(0x01); //display clear 341 | LCD_putcommand(0x06); //entry mode set 342 | } 343 | 344 | //****************************************************************** 345 | //LCD_putcommand 346 | //****************************************************************** 347 | 348 | void LCD_putcommand(unsigned char cmd) 349 | { 350 | //parse command variable into individual bits for output 351 | //to LCD 352 | 353 | //configure DB7 value 354 | gpio77_value = fopen("/sys/class/gpio/gpio77/value", "w"); 355 | if(gpio77_value == NULL) {printf("Unable to open gpio77_value.\n");} 356 | fseek(gpio77_value, 0, SEEK_SET); 357 | if((cmd & 0x0080)== 0x0080) 358 | { 359 | printf("CmDB7:1"); 360 | logic_status_77 = 1; 361 | fprintf(gpio77_value, "%d", logic_status_77); 362 | } 363 | else 364 | { 365 | printf(" CmDB7:0"); 366 | logic_status_77 = 0; 367 | fprintf(gpio77_value, "%d", logic_status_77); 368 | } 369 | fflush(gpio77_value); 370 | 371 | 372 | //configure DB6 value 373 | gpio76_value = fopen("/sys/class/gpio/gpio76/value", "w"); 374 | if(gpio76_value == NULL) {printf("Unable to open gpio76_value.\n");} 375 | fseek(gpio76_value, 0, SEEK_SET); 376 | if((cmd & 0x0040)== 0x0040) 377 | { 378 | printf(" CmDB6:1"); 379 | logic_status_76 = 1; 380 | fprintf(gpio76_value, "%d", logic_status_76); 381 | } 382 | else 383 | { 384 | printf(" CmDB6:0"); 385 | logic_status_76 = 0; 386 | fprintf(gpio76_value, "%d", logic_status_76); 387 | } 388 | fflush(gpio76_value); 389 | 390 | 391 | //configure DB5 value 392 | gpio75_value = fopen("/sys/class/gpio/gpio75/value", "w"); 393 | if(gpio75_value == NULL) {printf("Unable to open gpio75_value.\n");} 394 | fseek(gpio75_value, 0, SEEK_SET); 395 | if((cmd & 0x0020)== 0x0020) 396 | { 397 | printf(" CmDB5:1"); 398 | logic_status_75 = 1; 399 | fprintf(gpio75_value, "%d", logic_status_75); 400 | } 401 | else 402 | { 403 | printf(" CmDB5:0"); 404 | logic_status_75 = 0; 405 | fprintf(gpio75_value, "%d", logic_status_75); 406 | } 407 | fflush(gpio75_value); 408 | 409 | 410 | //configure DB4 value 411 | gpio74_value = fopen("/sys/class/gpio/gpio74/value", "w"); 412 | if(gpio74_value == NULL) {printf("Unable to open gpio74_value.\n");} 413 | fseek(gpio74_value, 0, SEEK_SET); 414 | if((cmd & 0x0010)== 0x0010) 415 | { 416 | printf(" CmDB4:1"); 417 | logic_status_74 = 1; 418 | fprintf(gpio74_value, "%d", logic_status_74); 419 | } 420 | else 421 | { 422 | printf(" CmDB4:0"); 423 | logic_status_74 = 0; 424 | fprintf(gpio74_value, "%d", logic_status_74); 425 | } 426 | fflush(gpio74_value); 427 | 428 | 429 | //configure DB3 value 430 | gpio73_value = fopen("/sys/class/gpio/gpio73/value", "w"); 431 | if(gpio73_value == NULL) {printf("Unable to open gpio73_value.\n");} 432 | fseek(gpio73_value, 0, SEEK_SET); 433 | if((cmd & 0x0008)== 0x0008) 434 | { 435 | printf(" CmDB3:1"); 436 | logic_status_73 = 1; 437 | fprintf(gpio73_value, "%d", logic_status_73); 438 | } 439 | else 440 | { 441 | printf(" CmDB3:0"); 442 | logic_status_73 = 0; 443 | fprintf(gpio73_value, "%d", logic_status_73); 444 | } 445 | fflush(gpio73_value); 446 | 447 | 448 | //configure DB2 value 449 | gpio72_value = fopen("/sys/class/gpio/gpio72/value", "w"); 450 | if(gpio72_value == NULL) {printf("Unable to open gpio72_value.\n");} 451 | fseek(gpio72_value, 0, SEEK_SET); 452 | if((cmd & 0x0004)== 0x0004) 453 | { 454 | printf(" CmDB2:1"); 455 | logic_status_72 = 1; 456 | fprintf(gpio72_value, "%d", logic_status_72); 457 | } 458 | else 459 | { 460 | printf(" CmDB2:0"); 461 | logic_status_72 = 0; 462 | fprintf(gpio72_value, "%d", logic_status_72); 463 | } 464 | fflush(gpio72_value); 465 | 466 | 467 | //configure DB1 value 468 | gpio71_value = fopen("/sys/class/gpio/gpio71/value", "w"); 469 | if(gpio71_value == NULL) {printf("Unable to open gpio71_value.\n");} 470 | fseek(gpio71_value, 0, SEEK_SET); 471 | if((cmd & 0x0002)== 0x0002) 472 | { 473 | printf(" CmDB1:1"); 474 | logic_status_71 = 1; 475 | fprintf(gpio71_value, "%d", logic_status_71); 476 | } 477 | else 478 | { 479 | printf(" CmDB1:0"); 480 | logic_status_71 = 0; 481 | fprintf(gpio77_value, "%d", logic_status_71); 482 | } 483 | fflush(gpio71_value); 484 | 485 | 486 | //configure DB0 value 487 | gpio70_value = fopen("/sys/class/gpio/gpio70/value", "w"); 488 | if(gpio70_value == NULL) {printf("Unable to open gpio70_value.\n");} 489 | fseek(gpio70_value, 0, SEEK_SET); 490 | if((cmd & 0x0001)== 0x0001) 491 | { 492 | printf(" CmDB0:1\n"); 493 | logic_status_70 = 1; 494 | fprintf(gpio70_value, "%d", logic_status_70); 495 | } 496 | else 497 | { 498 | printf(" CmDB0:0\n"); 499 | logic_status_70 = 0; 500 | fprintf(gpio70_value, "%d", logic_status_70); 501 | } 502 | fflush(gpio70_value); 503 | 504 | //LCD Register Set (RS) to logic zero for command input 505 | logic_status_87 = 0; 506 | gpio87_value = fopen("/sys/class/gpio/gpio87/value", "w"); 507 | if(gpio87_value == NULL) {printf("Unable to open gpio87_value.\n");} 508 | fseek(gpio87_value, 0, SEEK_SET); 509 | fprintf(gpio87_value, "%d", logic_status_87); 510 | fflush(gpio87_value); 511 | 512 | 513 | //LCD Enable (E) pin high 514 | logic_status_88 = 1; 515 | gpio88_value = fopen("/sys/class/gpio/gpio88/value", "w"); 516 | if(gpio88_value == NULL) {printf("Unable to open gpio88_value.\n");} 517 | fseek(gpio88_value, 0, SEEK_SET); 518 | fprintf(gpio88_value, "%d", logic_status_88); 519 | fflush(gpio88_value); 520 | 521 | //delay 522 | delay_us(2); 523 | 524 | //LCD Enable (E) pin low 525 | logic_status_88 = 0; 526 | gpio88_value = fopen("/sys/class/gpio/gpio88/value", "w"); 527 | if(gpio88_value == NULL) {printf("Unable to open gpio88_value.\n");} 528 | fseek(gpio88_value, 0, SEEK_SET); 529 | fprintf(gpio88_value, "%d", logic_status_88); 530 | fflush(gpio88_value); 531 | 532 | //delay 533 | delay_us(100); 534 | } 535 | 536 | //****************************************************************** 537 | //LCD_putchar 538 | //****************************************************************** 539 | 540 | void LCD_putchar(unsigned char chr) 541 | { 542 | //parse character variable into individual bits for output 543 | //to LCD 544 | 545 | printf("Data: %c:%d", chr, chr); 546 | chr = (int)(chr); 547 | 548 | //configure DB7 value 549 | gpio77_value = fopen("/sys/class/gpio/gpio77/value", "w"); 550 | if(gpio77_value == NULL) {printf("Unable to open gpio77_value.\n");} 551 | fseek(gpio77_value, 0, SEEK_SET); 552 | if((chr & 0x0080)== 0x0080) 553 | { 554 | printf(" DB7:1"); 555 | logic_status_77 = 1; 556 | fprintf(gpio77_value, "%d", logic_status_77); 557 | } 558 | else 559 | { 560 | printf(" DB7:0"); 561 | logic_status_77 = 0; 562 | fprintf(gpio77_value, "%d", logic_status_77); 563 | } 564 | fflush(gpio77_value); 565 | 566 | 567 | //configure DB6 value 568 | gpio76_value = fopen("/sys/class/gpio/gpio76/value", "w"); 569 | if(gpio76_value == NULL) {printf("Unable to open gpio76_value.\n");} 570 | fseek(gpio76_value, 0, SEEK_SET); 571 | if((chr & 0x0040)== 0x0040) 572 | { 573 | printf(" DB6:1"); 574 | logic_status_76 = 1; 575 | fprintf(gpio76_value, "%d", logic_status_76); 576 | } 577 | else 578 | { 579 | printf(" DB6:0"); 580 | logic_status_76 = 0; 581 | fprintf(gpio76_value, "%d", logic_status_76); 582 | } 583 | fflush(gpio76_value); 584 | 585 | 586 | //configure DB5 value 587 | gpio75_value = fopen("/sys/class/gpio/gpio75/value", "w"); 588 | if(gpio75_value == NULL) {printf("Unable to open gpio75_value.\n");} 589 | fseek(gpio75_value, 0, SEEK_SET); 590 | if((chr & 0x0020)== 0x0020) 591 | { 592 | printf(" DB5:1"); 593 | logic_status_75 = 1; 594 | fprintf(gpio75_value, "%d", logic_status_75); 595 | } 596 | else 597 | { 598 | printf(" DB5:0"); 599 | logic_status_75 = 0; 600 | fprintf(gpio75_value, "%d", logic_status_75); 601 | } 602 | fflush(gpio75_value); 603 | 604 | 605 | //configure DB4 value 606 | gpio74_value = fopen("/sys/class/gpio/gpio74/value", "w"); 607 | if(gpio74_value == NULL) {printf("Unable to open gpio74_value.\n");} 608 | fseek(gpio74_value, 0, SEEK_SET); 609 | if((chr & 0x0010)== 0x0010) 610 | { 611 | printf(" DB4:1"); 612 | logic_status_74 = 1; 613 | fprintf(gpio74_value, "%d", logic_status_74); 614 | } 615 | else 616 | { 617 | printf(" DB4:0"); 618 | logic_status_74 = 0; 619 | fprintf(gpio74_value, "%d", logic_status_74); 620 | } 621 | fflush(gpio74_value); 622 | 623 | 624 | //configure DB3 value 625 | gpio73_value = fopen("/sys/class/gpio/gpio73/value", "w"); 626 | if(gpio73_value == NULL) {printf("Unable to open gpio73_value.\n");} 627 | fseek(gpio73_value, 0, SEEK_SET); 628 | if((chr & 0x0008)== 0x0008) 629 | { 630 | printf(" DB3:1"); 631 | logic_status_73 = 1; 632 | fprintf(gpio73_value, "%d", logic_status_73); 633 | } 634 | else 635 | { 636 | printf(" DB3:0"); 637 | logic_status_73 = 0; 638 | fprintf(gpio73_value, "%d", logic_status_73); 639 | } 640 | fflush(gpio73_value); 641 | 642 | 643 | //configure DB2 value 644 | gpio72_value = fopen("/sys/class/gpio/gpio72/value", "w"); 645 | if(gpio72_value == NULL) {printf("Unable to open gpio72_value.\n");} 646 | fseek(gpio72_value, 0, SEEK_SET); 647 | if((chr & 0x0004)== 0x0004) 648 | { 649 | printf(" DB2:1"); 650 | logic_status_72 = 1; 651 | fprintf(gpio72_value, "%d", logic_status_72); 652 | } 653 | else 654 | { 655 | printf(" DB2:0"); 656 | logic_status_72 = 0; 657 | fprintf(gpio72_value, "%d", logic_status_72); 658 | } 659 | fflush(gpio72_value); 660 | 661 | 662 | //configure DB1 value 663 | gpio71_value = fopen("/sys/class/gpio/gpio71/value", "w"); 664 | if(gpio71_value == NULL) {printf("Unable to open gpio71_value.\n");} 665 | fseek(gpio71_value, 0, SEEK_SET); 666 | if((chr & 0x0002)== 0x0002) 667 | { 668 | printf(" DB1:1"); 669 | logic_status_71 = 1; 670 | fprintf(gpio71_value, "%d", logic_status_71); 671 | } 672 | else 673 | { 674 | printf(" DB1:0"); 675 | logic_status_71 = 0; 676 | fprintf(gpio77_value, "%d", logic_status_71); 677 | } 678 | fflush(gpio71_value); 679 | 680 | //configure DB0 value 681 | gpio70_value = fopen("/sys/class/gpio/gpio70/value", "w"); 682 | if(gpio70_value == NULL) {printf("Unable to open gpio70_value.\n");} 683 | fseek(gpio70_value, 0, SEEK_SET); 684 | if((chr & 0x0001)== 0x0001) 685 | { 686 | printf(" DB0:1\n"); 687 | logic_status_70 = 1; 688 | fprintf(gpio70_value, "%d", logic_status_70); 689 | } 690 | else 691 | { 692 | printf(" DB0:0\n"); 693 | logic_status_70 = 0; 694 | fprintf(gpio70_value, "%d", logic_status_70); 695 | } 696 | fflush(gpio70_value); 697 | 698 | //LCD Register Set (RS) to logic one for character input 699 | logic_status_87 = 1; 700 | gpio87_value = fopen("/sys/class/gpio/gpio87/value", "w"); 701 | if(gpio87_value == NULL) {printf("Unable to open gpio87_value.\n");} 702 | fseek(gpio87_value, 0, SEEK_SET); 703 | fprintf(gpio87_value, "%d", logic_status_87); 704 | fflush(gpio87_value); 705 | 706 | 707 | //LCD Enable (E) pin high 708 | logic_status_88 = 1; 709 | gpio88_value = fopen("/sys/class/gpio/gpio88/value", "w"); 710 | if(gpio88_value == NULL) {printf("Unable to open gpio88_value.\n");} 711 | fseek(gpio88_value, 0, SEEK_SET); 712 | fprintf(gpio88_value, "%d", logic_status_88); 713 | fflush(gpio88_value); 714 | 715 | //delay 716 | delay_us(2); 717 | 718 | //LCD Enable (E) pin low 719 | logic_status_88 = 0; 720 | gpio88_value = fopen("/sys/class/gpio/gpio88/value", "w"); 721 | if(gpio88_value == NULL) {printf("Unable to open gpio88_value.\n");} 722 | fseek(gpio88_value, 0, SEEK_SET); 723 | fprintf(gpio88_value, "%d", logic_status_88); 724 | fflush(gpio88_value); 725 | 726 | //delay 727 | delay_us(2); 728 | 729 | } 730 | 731 | //****************************************************************** 732 | //****************************************************************** 733 | void LCD_print(unsigned int line, char *msg) 734 | { 735 | int i = 0; 736 | 737 | if(line == 1) 738 | { 739 | LCD_putcommand(0x80); //print to LCD line 1 740 | } 741 | else 742 | { 743 | LCD_putcommand(0xc0); //print to LCD line 2 744 | } 745 | 746 | while(*(msg) != '\0') 747 | { 748 | LCD_putchar(*msg); 749 | //printf("Data: %c\n\n", *msg); 750 | msg++; 751 | } 752 | } 753 | 754 | //****************************************************************** 755 | 756 | void delay_us(int desired_delay_us) 757 | { 758 | struct timeval tv_start; //start time hack 759 | struct timeval tv_now; //current time hack 760 | int elapsed_time_us; 761 | 762 | gettimeofday(&tv_start, NULL); 763 | elapsed_time_us = 0; 764 | 765 | while(elapsed_time_us < desired_delay_us) 766 | { 767 | gettimeofday(&tv_now, NULL); 768 | if(tv_now.tv_usec >= tv_start.tv_usec) 769 | elapsed_time_us = tv_now.tv_usec - tv_start.tv_usec; 770 | else 771 | elapsed_time_us = (1000000 - tv_start.tv_usec) + tv_now.tv_usec; 772 | //printf("start: %ld \n", tv_start.tv_usec); 773 | //printf("now: %ld \n", tv_now.tv_usec); 774 | //printf("desired: %d \n", desired_delay_ms); 775 | //printf("elapsed: %d \n\n", elapsed_time_ms); 776 | } 777 | } 778 | 779 | //****************************************************************** 780 | 781 | 782 | 783 | --------------------------------------------------------------------------------