Sunday, March 22, 2015

Chip programming with Linux 1 - PICkit version2 clone under Linux / Freebsd / Mac OS


Description    : Installing PICkit version2 clone for Linux / Mac.


            











Compatibility : Linux kernel 2.4 or 2.6. ( Tested )
                                   FreeBSD                        ( NOT Tested
                          Mac OSX 10.4                ( NOT Tested
                          Mac OSX 10.5                ( NOT Tested )
                          OS X Mavericks 10.9.2  ( Tested )

Steps :

1. Login as root

Command  : su
                  
2. PICkit version2 clone drivers for Linux / Mac

Commands : yum install gcc-c++
                     yum install libusb1-devel
                     yum install libusb-devel


3. Download PICkit version2 clone software for Linux / Mac

URL : Pickit2 Tool for Linux

4. Change directory to download location

Command : cd /home/developer64/Downloads/

5. Extract source code

Command : tar zxvf pk2cmdv1.20LinuxMacSource.tar.gz

6. Change directory to source code folder

Command : cd pk2cmdv1.20LinuxMacSource

7. Check your Linux kernel vernal version

Command : uname -r  

My Linux kernel version is 2.6.32-504.12.2.el6.x86_64


8. Build the source code

This software comes with source code. So you can view the source code and change according to your Operating System. It is written in C++ and you can improve the source code according your requirement.

 
Command for Linux Kernel version start with 2.6 : make linux  
  
Command for Linux Kernel version start with 2.4 : make linux-old

Command for FreeBSD                                         : make freebsd

Command for Mac OS 10.4                                   : make mac104

Command for Mac OS 10.5                                   : make mac105


9. Copy relevant files to /usr/local/bin


Command : cp pk2cmd /usr/local/bin
                   cp PK2DeviceFile.dat /usr/local/bin/
                   cp PK2V023200.hex /usr/local/bin/


10. Now check your software version

Command : pk2cmd -?V

Executable Version:    1.20.00
Device File Version:   1.55.00
OS Firmware Version:   PICkit 2 not found

 

Cheers we have successfully installed. Yet to configure it. It says  PICkit 2 not found as PICkit2 device not connected with my computer

           
11. Create Driver Manager Empty Rule File

Command : echo "" > /etc/udev/rules.d/99-pickit2.rules

12. Open the file
  
Command : gedit /etc/udev/rules.d/99-pickit2.rules

13. Add bellow rule content to /etc/udev/rules.d/99-pickit2.rules and save the file
  
SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device",SYSFS{idVendor}=="04d8" , SYSFS{idProduct}=="0033", MODE="0666"


14. Now reload Linux Driver Manager Rules
  
Command : udevadm control --reload-rules

15. Now connect your PICkit2 USB device without PIC chipset

16. Check the device connectivity with your computer
  
Command : pk2cmd -?V

Result       :  Executable Version:    1.20.00
                    Device File Version:   1.55.00
                    OS Firmware Version:   2.32.00


                    Operation Succeeded

Cheers. It says Operation Succeeded

16. Mount your PIC Chipset in right direction on your device.

Refer bellow picture.

17. Now connect your PICkit2 USB device 













17. Run Auto detection command. This will identify PIC Chipset

Command : pk2cmd -P


Result       :  Auto-Detect: Found part PIC16F877A.

                    Operation Succeeded


My PIC chip is PIC16F877A



18. Erasing chipset

Command : pk2cmd -P PIC16F877A -X -E
 
Result       :  Erasing Device...

                    Operation Succeeded


19. Installing hexadecimal file in to chipset. 
      Hexadecimal file will include all execution steps of the firmware. 
      We will discuss how to write small program for LED display, Sensor  
      readings, Motor controllers and etc..

      We will be able to write firmware based on Micro C language Micro C IDE.
      Then we will convert it in to HEX file.


For the moment we will use existing HEX file PK2V023200.hex located in same place pk2cmdv1.20LinuxMacSource


Command : pk2cmd -P PIC16F877A -X -M -F PK2V023200.hex
 
Result       :  PICkit 2 Program Report
                    22-3-2015, 23:29:55
                    Device Type: PIC16F877A

                    Program Succeeded.

                    Operation Succeeded

Now it says, it wrote given Hex file in to chipset called PIC16F877A.


19. Verify Hex file against Chipset memory issues


Command : pk2cmd -P PIC16F877A -Y -F PK2V023200.hex
 
Result       :  PICkit 2 Verify Report
              22-3-2015, 23:44:49
              Device Type: PIC16F877A

              EEData Memory Errors

              Address   Good     Bad
              000000    0000EA   000000


It seems everything is OK.


20. Power ON Chipset


Command : pk2cmd -P PIC16F877A -T
 
Result       :  Operation Succeeded
Now you can see power indicator LED is working


21. Power OFF Chipset


Command : pk2cmd -P PIC16F877A
 
Result       :  Operation Succeeded
Now you can see power indicator LED is NOT working




In next post we will install Micro C IDE, C compilers dedicated to Micro C IDE. Then we will develop some hobby circuits such as LED controllers, Motor controllers, sensor readers and many more.

Thank you. 

Monday, March 9, 2015

Divide large SQL file under Linux in an Emergency Run.

Divide large SQL file under Linux in an Emergency Run.
-----------------------------------------------------------------------------------------------------

Requirement  : Think about in an emergency situation you have to run large SQL file where you are from remote desktop and you don't have GUI access.
            
Methodology : In such a situation lets divide SQL file to small size files and run them separately.

Advantage   : You don't need to wait until it runs as it will take lots of time.
                        It will save your time if SQL errors occurred in the middle of the process.
                              You can fix any SQL errors if them occurred in the middle of the process.

1. Change directory to file location

Command : cd /home/saminda_06488/Documents/DB-CCG

2. Read the size of the file in bytes

Command : du -b
                   du -b CM_CDRS_DATA_TABLE.sql

5476295390 bytes ( Around 5GB to 6 GB )
  
3. Methodology
  
Lets read entire file by 500 MB ( 50 X 1024 X 1024 bytes = 524288000 bytes ) per time
We are going to use Linux command called "head" with specifying number of bytes
Then we are sending the output to new SQL file called "1.sql"

Command : head -c 524288000 CM_CDRS_DATA_TABLE.sql > 1.sql

4. Once it finished then read the last lines of 1.sql by Linux command called "tail"

Command : tail 1.sql

It will be like this.

Insert into CCG.CM_CDRS (UCID,CLI,YEAR,MONTH,DURATION,CM_TIMESTAMP,CM_STATUS,CM_START_TIME,CALL_VDN) values ('1050761310051687','776541947','2011','07',83,to_date('07-JUL-11','DD-MON-RR'),'N','2011-07-07 20:44:47','6340');
Insert into CCG.CM_CDRS (UCID,CLI,YEAR,MONTH,DURATION,CM_TIMESTAMP,CM_STATUS,CM_START_TIME,CALL_VDN) values ('1054221310051772','771087148','2011','07',61,to_d



5. Count characters of Last incomplete SQL line. Only if you have such line

Insert into CCG.CM_CDRS (UCID,CLI,YEAR,MONTH,DURATION,CM_TIMESTAMP,CM_STATUS,CM_START_TIME,CALL_VDN) values ('1054221310051772','771087148','2011','07',61,to_d

Count is 159 characters.

6. Lets adjust the 1.sql file size by reducing 159 bytes from 524288000 bytes. That is 524287841 bytes

Command : head -c 524287841 CM_CDRS_DATA_TABLE.sql > 1.sql


7. Verify it by reading last 10 rows.

Command : tail 1.sql

Insert into CCG.CM_CDRS (UCID,CLI,YEAR,MONTH,DURATION,CM_TIMESTAMP,CM_STATUS,CM_START_TIME,CALL_VDN) values ('1056491310051831','773833438','2011','07',3,to_date('07-JUL-11','DD-MON-RR'),'N','2011-07-07 20:47:11','6341');
Insert into CCG.CM_CDRS (UCID,CLI,YEAR,MONTH,DURATION,CM_TIMESTAMP,CM_STATUS,CM_START_TIME,CALL_VDN) values ('1053431310051750','772605427','2011','07',74,to_date('07-JUL-11','DD-MON-RR'),'N','2011-07-07 20:45:50','6341');
Insert into CCG.CM_CDRS (UCID,CLI,YEAR,MONTH,DURATION,CM_TIMESTAMP,CM_STATUS,CM_START_TIME,CALL_VDN) values ('1050761310051687','776541947','2011','07',83,to_date('07-JUL-11','DD-MON-RR'),'N','2011-07-07 20:44:47','6340');



It seems it is OK.


8. Keep newly generated 1.sql in seperate place and now we are going to generate next 50 MB.

Now we have to generate new CM_CDRS_DATA_TABLE.sql without first 50 MB.
Now we have to read bytes from down to top. Here we are going to use command called "tail"

First remove first 50 MB ( 524288000 - 159 ) bytes = 524287841 bytes

Now get the remaining bytes to generate new CM_CDRS_DATA_TABLE.sql
( 5476295390 - 524287841 ) bytes = 4952007549 bytes


command : tail -c 5423866749 CM_CDRS_DATA_TABLE.sql > CM_CDRS_DATA_TABLE.sql_TMP_1


9. Now follow again 1-9 steps until you finished entire SQL file.


  
10. Once you have spitted entire 6 GB SQL file in to 500 mb, now you can run them one by one in correct sequence.