Wednesday, 31 May 2017

Programming the Arduino Uno without using Arduino IDE

https://github.com/RealTimeEngineers/Arduino_programming_without_Arduino_IDE


It is possible to program the Arduino Uno without the use of Arduino IDE.

Download the WinAVR tools from the below link

https://sourceforge.net/projects/winavr/files/latest/download

After installation you can find the AVR tools in C:\WinAVR-20100110\bin



Open Notepad and save the below code as blinky.c and place it in some folder say 'test_blinky'


/*
 * Blinks LED on connected to pin 13 (PB5 on Atmega328P)
 *
 * Authors : Jabez Winston C, Hariharan K
 *
 */

#define F_CPU 16e6  // Arduino Uno's ATmega328P runs at 16MHz 

#include<avr/io.h>
#include<util/delay.h>

int main()
{
DDRB|=(1<<5); //Set PB5 as output by setting 5th bit DDRB register  

 while(1)
 {
 PORTB|=(1<<5);  //Set pin 13 (PB5)- High
 _delay_ms(100); //Wait for 100 ms
 PORTB&=~(1<<5); //Set pin 13 (PB5)- Low
 _delay_ms(100); //Wait for 100ms
 }
return 0; 
//Refer Atmega328P datasheet for details regarding DDRB and PORTB
}


Inside the folder 'test_blinky' ,with SHIFT key pressed ,right click and select Open command window here


Type the following commands on command prompt

avr-gcc -O2 -mmcu=atmega328p blinky.c 
avr-objcopy -O ihex a.out a.hex

The first command compiles the program with optimization level 2 (O2) and


To flash the HEX file (a.hex) to Arduino ,use the below command
avrdude -patmega328p -carduino -PCOM25 -b115200 -D -Uflash:w:a.hex:i

Replace COM25 port by the COM port for your Arduino by looking into the device manager
-Uflash:w:a.hex:i tells the avrdude utility to (w)rite (i)ntel hex file (a.hex) to flash memory.
-b115200 tells at what baud rate avrdude should communicate with the bootloader. Here it is 115200.Changing it to some other value won't work.


To automate the 3 steps, a batch file is written which does the process of compiling,HEX file conversion and flashing the HEX file.Save the below code as build_and_flash.bat in test_blinky folder and run it.

del *.hex *.out

avr-gcc -O2 -mmcu=atmega328p blinky.c 
avr-objcopy -O ihex a.out a.hex
avrdude -patmega328p -carduino -PCOM25 -b115200 -D -Uflash:w:a.hex:i

pause


Complete work is found in this github link
https://github.com/RealTimeEngineers/Arduino_programming_without_Arduino_IDE

How was it found that it works this way ?

Go to File -> Preferences in Arduino IDE



































Enable verbose output during compilation and upload


Arduino IDE showing the commands executed in background during compilation and upload



Arduino's AVR tools can be found in C:\Program Files (x86)\Arduino\hardware\tools\avr\bin . Those tools can also be used instead of WinAVR tools.


The below batch file makes use Arduino AVR tools. Save as build_and_flash_arduino_tools.bat and make  use of it.

@echo off

::Add 'C:\Program Files (x86)\Arduino\hardware\tools\avr\bin' to environment variable PATH
set PATH=C:\Program Files (x86)\Arduino\hardware\tools\avr\bin;%PATH%
 
:: Delete all .hex and .out files 
del *.hex *.out

:: Compile 'blinky.c' for ATmega328p chip using 'avr-gcc' to generate 'a.out' 
avr-gcc -O2 -mmcu=atmega328p blinky.c 

:: Convert 'a.out' to Intel HEX file 'a.hex' using 'avr-objcopy'
avr-objcopy -O ihex a.out a.hex

:: Burn the HEX file(a.hex) to ATmega328 chip on Arduino Uno using 'avrdude'
:: Replace COM25 by the COM port detected on your PC
avrdude -C"C:\Program Files (x86)\Arduino\hardware\tools\avr\etc\avrdude.conf"  -patmega328p -carduino -PCOM25 -b115200 -D -Uflash:w:a.hex:i

::Don't close console after execution
pause

Friday, 31 March 2017

Controller Area Netrwork (CAN) in Linux

1. Install CAN-Utils

Method 1:

Using the command
sudo apt-get install can-utils




(OR)



After downloading use the following commands

unzip can-utils-master.zip
cd can-utils-master
make
sudo make install

2.Load CAN drivers (Reference: https://en.wikipedia.org/wiki/SocketCAN )

Use the following commands

sudo modprobe can
sudo modprobe vcan
sudo modprobe can_raw
sudo modprobe can_gw

To list the kernel modules
lsmod



Kernel modules can be found in /lib/modules/<kernel_version>/net/can/
(or) <KERNEL_SRC>/net/can . Kernel source can be downloaded from www.kernel.org


3.Creating virtual CAN node( Reference: https://en.wikipedia.org/wiki/SocketCAN )
Use the following commands to create a virtual CAN node vcan0

sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0

Similarly repeat the above commands to a virtual CAN node vcan1



To list the details of network interfaces ,use ifconfig
Use ifconfig vcan0 to know details of vcan0




4.Using CAN-Utils

CAN Utils contains the following tools
asc2log, bcmserver, canbusload, can-calc-bit-timing, candump, canfdtest, cangen, cangw, canlogserver, canplayer, cansend, cansniffer, isotpdump, isotprecv, isotpperf, isotpsend, isotpserver, isotpsniffer, isotptun, log2asc, log2long, slcan_attach, slcand and slcanpty

Frequently used tools are highlighted

 a.cansend and candump

To send a CAN message , using cansend there are two requirements 11 bit(0x000 to 0x7FF) /29 bit(0x00000000 to 0x1FFFFFFF) identifier and message of length ranging from 0 to 8 bytes

To send a message (0x6789) of 2 bytes with 11bit identifier (0x000) to interface vcan0,use cansend vcan0 000#6789

To send a message (0x8679) of 8 bytes with 29 bit identifier (0x0123456789ABCDEF) to interface vcan0,use cansend vcan0 0x0123456789ABCDEF#8679



Using Acceptance filtering (Refer https://discuss.cantact.io/t/using-can-utils/24 )

To filter messages whose ID is in the range  0x120 to 0x12F
Use candump vcan0,120:0x7F0
Messages are filtered based on this condition
[received_can_id] & [can_mask] == [can_id] & [can_mask]
Here can_id = 0x120 and can_mask =0x7F0
& is a bitwise operator



b.Using the CAN gateway utility (cangw):

Ensure that kernel module can_gw  is loaded.You can check it using the command lsmod

sudo cangw -F                                                            
 #deletes all gateway rules

sudo cangw -A -s vcan0 -d vcan1 -e -f 120:7F0      
 #Adds a new rule with source as vcan0 , destination as vcan1 with echo enabled and  which accepts messages of ID in the range     0x120 to 0x12F

sudo cangw -A -s vcan0 -d vcan1 -e -f 200:7F0       
#Adds a new rule with source as vcan0 destination as vcan1 with echo enabled and   which accepts messages of ID in the range  0x200 to 0x20F

cangw -L           
#Lists all gateway rules



d. using cangen
For more help and information, <utility_name> --help

Eg:       cangw --help
      cansend --help
      candump --help




Linux CAN Subsystem

For working on real CAN hardware
https://harrisonsand.com/can-on-the-raspberry-pi/