User avatar
ValterFukuoka
Posts: 94
Joined: Sat Oct 01, 2011 11:39 pm
Location: Japan
Contact: Website

Re: Add small cpu for R/C servo controlled

Thu Sep 12, 2013 6:58 am

danjperron wrote:Well this little program to burn a small 8 pins cpu for R/C servo start to be a little more than it was intent for.
thanks again,
I don't know how many people use my small code but it will increase the universality of it.
Daniel
My point of view is that by using Python there will be a much large number of people able to understand, use and contribute back to the software...

Following in the line of the "co-controller" thinking expressed above, I believe that "little, easy and simple stuff" can help a lot of people to accomplish a lot of stuff...

Instead of thinking of the current state of burnLVP as "small code", let's think about using the small is beautiful as THE feature for its success...

Of course, the number of lines can grow, but its COMPREHENSIBILITY should remain as "beautiful" as it is today (state)... I think this is the way for more and more people to adopt it...

Valter

User avatar
ValterFukuoka
Posts: 94
Joined: Sat Oct 01, 2011 11:39 pm
Location: Japan
Contact: Website

Re: Add small cpu for R/C servo controlled

Thu Sep 12, 2013 10:39 am

{About sleep() in Python on RasPI}

Just running a small test:

Code: Select all

# sleep-test.py

from datetime import datetime
import time

def check_sleep(amount):
    start = datetime.now()
    time.sleep(amount)
    end = datetime.now()
    delta = end-start
    return delta.seconds + delta.microseconds/1000000., amount

print check_sleep(0.1)
print check_sleep(0.01)
print check_sleep(0.001)
print check_sleep(0.0001)
print check_sleep(0.00001)
print check_sleep(0.000001)
print check_sleep(0.0000001)
print check_sleep(0.00000001)
print check_sleep(0.000000001)
gives us one idea about the smallest "meaningful" time for sleep() in RasPI...

Code: Select all

(0.100462, 0.1)
(0.010309, 0.01)
(0.001502, 0.001)
(0.000604, 0.0001)
(0.000502, 1e-05)
(0.000575, 1e-06)
(0.000175, 1e-07)
(0.000382, 1e-08)
(0.000162, 1e-09)
anything below "0.0001" will not produce any meaningful result...

Is there any other option for sleep() in RasPI Python?

danjperron
Posts: 3332
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Thu Sep 12, 2013 11:26 am

Thank you Valter,


I will also change my github and add your modification, if you gave permission to do it ( I will add your name).

Maybe I will revise the code and add a argument to specify VPP (standard) or LVP mode.
Also I should check revision number and complain if the chip has errata in the LVP mode.

I have to complete one of two projects (8 mm film to HD mp4 converter) and rfid reader system . I will try to do it in between before I got the rc522 rfid reader and continue my rfid card project I'm using a apsx re-210 reader and it is expensive (works very well).

Daniel.

User avatar
ValterFukuoka
Posts: 94
Joined: Sat Oct 01, 2011 11:39 pm
Location: Japan
Contact: Website

Re: Add small cpu for R/C servo controlled

Thu Sep 12, 2013 5:34 pm

danjperron wrote:Thank you Valter,
I will also change my github and add your modification, if you gave permission to do it ( I will add your name).
Daniel.
Sure, thanks.

{BETA 2}
I did test a second version of the burnHLVP hard interface "burner"...

Previous one I was trying to use just 1 type of transistor (NPN), but seems that by using a compound NPN-PNP it works better (same design as the RPP)... so I did some tests...

Here is the scheme:
Image

Valter

User avatar
ValterFukuoka
Posts: 94
Joined: Sat Oct 01, 2011 11:39 pm
Location: Japan
Contact: Website

Re: Add small cpu for R/C servo controlled

Fri Sep 13, 2013 4:49 pm

Just added support to "export .hex file" (from MCU to file)...
Working, but still need to do tests...

Valter

User avatar
ValterFukuoka
Posts: 94
Joined: Sat Oct 01, 2011 11:39 pm
Location: Japan
Contact: Website

Re: Add small cpu for R/C servo controlled

Sun Sep 15, 2013 10:02 am

ValterFukuoka wrote:Just added support to "export .hex file" (from MCU to file)...
Working, but still need to do tests...
Valter
Ok, still testing but since others can help to improve, and for Daniel to have a look and perhaps incorporate the changes into the original...

Testing version of the code is here:
https://sites.google.com/site/kit2pic/

Look for the most recent "burnLVP_2013_AAA_nn" files...

After succeeding in more tests I will report here (and there, on the url)...

For while, I have 2 version (on my PC), the LVP and the HVP (this one I will also upload there) to keep testing more simple for me. The HVP uses the hardware mentioned on the previous posts... and the LVP, needs only PI, to work, as the original...

If you find any errors, please report to us...

Regards,
Valter

User avatar
ValterFukuoka
Posts: 94
Joined: Sat Oct 01, 2011 11:39 pm
Location: Japan
Contact: Website

Re: Add small cpu for R/C servo controlled

Sun Sep 15, 2013 10:58 am

Also, Fritzing diagram available:

Image

High resolution and Fritzing file (as well as RaspberryPI Model, from Adafruit):
https://sites.google.com/site/kit2pic/

Daniel, feel free to use it, if you want...

Valter

danjperron
Posts: 3332
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Mon Sep 16, 2013 1:49 am

Nice work Valter!

I just read your code from your webpage.

Good comments and I will work on on my code to add some arguments to the python code.

I should add hexintel16 , verify and read mode, Maybe I should also add the parameters VPP for High voltage programming.


I'm working a lot with the pic18(LF)xxK80 series. A long goal will be to implement them also. They could be program using the LVP but they use 4 blocks for the program and a bigger flash data and config file. But it should be possible since the method is the same.


Daniel

User avatar
ValterFukuoka
Posts: 94
Joined: Sat Oct 01, 2011 11:39 pm
Location: Japan
Contact: Website

Re: Add small cpu for R/C servo controlled

Mon Sep 16, 2013 9:22 am

danjperron wrote:Nice work Valter!
I just read your code from your webpage.
Good comments and I will work on on my code to add some arguments to the python code.
Daniel
Thanks.
I am trying to make it more easy for others to provide more support for more MCUs...
danjperron wrote:Nice work Valter!
I'm working a lot with the pic18(LF)xxK80 series. A long goal will be to implement them also. They could be program using the LVP but they use 4 blocks for the program and a bigger flash data and config file. But it should be possible since the method is the same.
Daniel
YES, that would be nice...I also did check some datasheet for 18F series... I'm not working on it yet... but I have interest on this...

User avatar
ValterFukuoka
Posts: 94
Joined: Sat Oct 01, 2011 11:39 pm
Location: Japan
Contact: Website

Re: Add small cpu for R/C servo controlled

Mon Sep 16, 2013 9:26 am

danjperron wrote:Nice work Valter!
Maybe I should also add the parameters VPP for High voltage programming.
Daniel
Perhaps you should think about "upgrading" the name of the program...
You can use "LVP" as the default behavior (option) with a HVP switch...
Of, course, the "burnLVP" name itself needs a "patch"...

As I am already testing it on HVP, it will be nice to incorporate all the changes into the original code, making it "LVP" by default...

User avatar
ValterFukuoka
Posts: 94
Joined: Sat Oct 01, 2011 11:39 pm
Location: Japan
Contact: Website

Re: Add small cpu for R/C servo controlled

Mon Sep 16, 2013 9:31 am

Daniel (and everybody), take a look at this...

Code: Select all

C:\Python27\pyusb-master>python pythonLVP-v0.1.py 12f1822_blink_intosc.hex
Mon, 16 Sep 2013 08:45:19 +0000
File " 12f1822_blink_intosc.hex " loaded
LVP ON
MCU : 0x1b80  : PIC12F1840 Revision  0x4
ProgramSize = 0x1000
DataSize    = 0x100
LVP OFF
LVP ON
Bulk Erase Program , Data. .... done.
LVP OFF
LVP ON
Program blank check................................Passed!
Data Blank check........Passed!
Writing Program................................Done.
Program check ................................Passed!
Writing Data........Done.
Data check ........Passed!
Writing Config..Done.
Config Check..Passed!
No Error.   All Done!
LVP OFF
Mon, 16 Sep 2013 08:52:19 +0000

C:\Python27\pyusb-master>
IT IS WORKING ON A USB-TO-SERIAL INTERFACE!!!

See the time it takes to program (a blink led)?

Mon, 16 Sep 2013 08:45:19 +0000
... (programming...)
Mon, 16 Sep 2013 08:52:19 +0000

...taking a LONG time... and see the:

C:\Python27\pyusb-master>python pythonLVP-v0.1.py 12f1822_blink_intosc.hex
...
C:\Python27\pyusb-master>

...because I am testing it on a WINDOWS PC!!!

IT IS NOT ON RASPBERRY PI!!!

I just succeeded with an upload... will be reporting more about it here...
VERY, very slow for while...
QUITE EXCITING this one... I will tell the "why" later...

Valter

User avatar
ValterFukuoka
Posts: 94
Joined: Sat Oct 01, 2011 11:39 pm
Location: Japan
Contact: Website

Re: Add small cpu for R/C servo controlled

Mon Sep 16, 2013 9:59 am

ValterFukuoka wrote: IT IS WORKING ON A USB-TO-SERIAL INTERFACE!!!
...
I just succeeded with an upload... will be reporting more about it here...
VERY, very slow for while...
QUITE EXCITING this one... I will tell the "why" later...
Valter
I just want to put a diagram about this last post...
Image

danjperron
Posts: 3332
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Tue Sep 17, 2013 3:39 am

I got the pic18F... working

At least I'm able to set the LVP mode and read the device ID.

This is really another beast compare to the pic12/16

First MCLR and PGM are separate so we need to add a line.

I add some functions to make it work

Code: Select all

def Set_LVP_PIC18():
   #held MCLR LOW
   GPIO.setup(PIC_MCLR,GPIO.OUT)
   GPIO.output(PIC_MCLR,False)

   #hdel PIC_CLK & PIC_DATA & PIC_PGM low
   GPIO.setup(PIC_CLK, GPIO.OUT)
   GPIO.output(PIC_CLK, False)
   GPIO.setup(PIC_DATA , GPIO.OUT)
   GPIO.output(PIC_DATA, False)
   GPIO.setup(PIC_PGM, GPIO.OUT)
   GPIO.output(PIC_PGM, False)

   sleep(0.001)
   #set PGM High
   GPIO.output(PIC_PGM, True)
   sleep(0.01)
   #Set MCLR High
   GPIO.output(PIC_MCLR, True)
   sleep(0.01)


def Release_LVP_PIC18():
   #set PGM Low
   GPIO.output(PIC_PGM, False)
   sleep(0.001)

   #held MCLR LOW
   GPIO.setup(PIC_MCLR,GPIO.OUT)
   GPIO.output(PIC_MCLR,False)

   #PIC_CLK & PIC_DATA has input
   GPIO.setup(PIC_CLK, GPIO.IN)
   GPIO.setup(PIC_DATA , GPIO.IN)

   sleep(0.001)
   #Set MCLR High
   GPIO.output(PIC_MCLR, True)

def LoadWordPic18(Value):
  print "LoadWordPic18(", hex(Value), ")"
  GPIO.setup(PIC_DATA,GPIO.OUT)
  for loop in range(16):
     GPIO.output(PIC_CLK,True)
     GPIO.output(PIC_DATA,(Value & 1)==1)
     pass
     GPIO.output(PIC_CLK,False)
     pass
     Value = Value >> 1;


def ReadDataPic18():
  GPIO.setup(PIC_DATA,GPIO.IN)
  for loop in range(8):
    GPIO.output(PIC_CLK,True)
    pass
    GPIO.output(PIC_CLK,False)
    pass
  Value = 0
  for loop in range(8):
    GPIO.output(PIC_CLK,True)
    pass
    if GPIO.input(PIC_DATA):
        Value =  Value + (1 << loop)
    GPIO.output(PIC_CLK,False)
    pass
  return Value

def LoadCommandPic18(Value):
  print "LoadCommandPic18(",hex(Value),")"
  GPIO.setup(PIC_DATA,GPIO.OUT)
  for loop in range(4):
     GPIO.output(PIC_CLK,True)
     GPIO.output(PIC_DATA,(Value & 8)==8)
     pass
     GPIO.output(PIC_CLK,False)
     pass
     Value = Value << 1;


def ReadMemoryPic18Next():
  LoadCommandPic18(0x1001)
  return ReadDataPic18()

def LoadCommandDataPic18(Pic18Command, Pic18Value):
  LoadCommandPic18(Pic18Command)
  LoadWordPic18(Pic18Value)


def ReadMemoryPic18(pic18_Memory):
  LoadCommandDataPic18(0,0x0E00 | ((pic18_Memory >> 16) & 0xff))
  LoadCommandDataPic18(0,0x6EF8)
  LoadCommandDataPic18(0,0x0E00 | ((pic18_Memory >> 8) & 0xff))
  LoadCommandDataPic18(0,0x6EF7)
  LoadCommandDataPic18(0,0x0E00 | (pic18_Memory & 0xff))
  LoadCommandDataPic18(0,0x6EF6)
  return ReadMemoryPic18Next()



#=============  main ==========

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)


GPIO.setup(PIC_PGM,GPIO.OUT)
GPIO.setup(PIC_MCLR,GPIO.OUT)
GPIO.setup(PIC_CLK,GPIO.IN)
GPIO.setup(PIC_DATA,GPIO.IN)

Set_LVP_PIC18()
CpuRevision = ReadMemoryPic18(0x3ffffe)
#CpuId = ReadMemoryPic18Next()
CpuId = ReadMemoryPic18(0x3fffff)
CpuId = (CpuId << 8 ) | (CpuRevision & 0xE0)
CpuRevision = CpuRevision & 0x1f

print "Cpu Id =" , hex(CpuId)
print "Revision=" , hex(CpuRevision)


Release_LVP_PIC18()
the function ReadMemoryPic18Next() doesn't increment the counter. I didn't find why but it is maybe an errata again. But ReadMemoryPic18() works.

Tomorrow I will try to program it. The Bulk erase only works with 5V on the pic18f258 so I will add adafruit converter.


Daniel

User avatar
ValterFukuoka
Posts: 94
Joined: Sat Oct 01, 2011 11:39 pm
Location: Japan
Contact: Website

Re: Add small cpu for R/C servo controlled

Tue Sep 17, 2013 4:58 am

danjperron wrote:I got the pic18F... working
At least I'm able to set the LVP mode and read the device ID.
This is really another beast compare to the pic12/16
First MCLR and PGM are separate so we need to add a line.
I add some functions to make it work
Great!
danjperron wrote: the function ReadMemoryPic18Next() doesn't increment the counter. I didn't find why but it is maybe an errata again.
Daniel
Perhaps a typo on the value (bin instead of hex):
LoadCommandPic18(0b1001)
instead of:
LoadCommandPic18(0x1001)

Nice job.

danjperron
Posts: 3332
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Tue Sep 17, 2013 10:59 am

Thanks Valter,

this is it. I will test the rest tonight. (eastern time)


Daniel

User avatar
ValterFukuoka
Posts: 94
Joined: Sat Oct 01, 2011 11:39 pm
Location: Japan
Contact: Website

Re: Add small cpu for R/C servo controlled

Tue Sep 17, 2013 9:45 pm

danjperron wrote:I got the pic18F... working
Daniel
Daniel, what pin are you using from GPIO for the "PGM" signal?

I did some test, just to be sure it works with 4550, 2550, 14K50...

They all are identified (device id) correctly...
I used this setup below:
Image

Valter

danjperron
Posts: 3332
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Tue Sep 17, 2013 10:41 pm

Hi Valter,

correct pin! pin 26

I did some work during my lunch break. Bulk erase, program code, program blank verify, program verify.

For the eerom data, I assume address 0xf00000 in the hex file .

The code program will use the multi panel method. Not sure yet about the boot block. Do I need to burn it separately. I will see.

Still need to work on the config.

I overlook the pic18f26k80 and I think it needs the magic code. I will add this familly and I will check it more carefully.

Daniel

danjperron
Posts: 3332
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Wed Sep 18, 2013 3:39 am

Hi Valter,

this is my latest source code for the pic18F... programming.

It is not complete,

The program burning sequence doesn't work.
I'm just trying to write 0 but the ProgramCheckPic18 tell me it is 0x3.

I will investigate it tomorrow.
Maybe I will try single panel writing mode instead.

I'm just trying to write 0 and but the Programcheck tell me it is 0x3. If I use my Pickit adapter , nothing has been written.

The Bulk erase, the blank check verify and the program verify work!

Code: Select all

#!/usr/bin/env python

################################
#
# burn18LVP.py  
#
#
# Program to burn pic18F...using LVP mode with a Rasberry Pi
#
#
# programmer : Daniel Perron
# Date       : Sept 17, 2013
# Version    : 0.001  
#
#
# preliminary version  *** not working ****
#
# Enhanced version of  burnLVP  for pic18F...
#
# source code:  https://github.com/danjperron/A2D_PIC_RPI
#
#


#////////////////////////////////////  MIT LICENSE ///////////////////////////////////
#	The MIT License (MIT)
#
#	Copyright (c) 2013 Daniel Perron
#
#	Permission is hereby granted, free of charge, to any person obtaining a copy of
#	this software and associated documentation files (the "Software"), to deal in
#	the Software without restriction, including without limitation the rights to
#	use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
#	the Software, and to permit persons to whom the Software is furnished to do so,
#	subject to the following conditions:
#
#	The above copyright notice and this permission notice shall be included in all
#	copies or substantial portions of the Software.
#
#	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
#	FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
#	COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
#	IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
#	CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



from time import sleep
import RPi.GPIO as GPIO
import sys, termios, atexit
from intelhex import IntelHex
from select import select   



# some constnat

Pic18PanelSize = 0x2000



#set io pin

#CLK GPIO 4 
PIC_CLK = 7

#DATA GPIO 8
PIC_DATA = 24

#MCLR GPIO 9
PIC_MCLR = 21

#PGM_GPIO 
PIC_PGM  = 26



#compatible PIC id

PIC18F242  = 0x0480
PIC18F248  = 0x0800
PIC18F252  = 0x0400
PIC18F258  = 0x0840
PIC18F442  = 0x04A0
PIC18F448  = 0x0820
PIC18F452  = 0x0420
PIC18F458  = 0x0860



# command definition
C_PIC18_INSTRUCTION   = 0b0000
C_PIC18_TABLAT 	      = 0b0010
C_PIC18_READ          = 0b1000
C_PIC18_READ_INC      = 0b1001
C_PIC18_READ_DEC      = 0b1010
C_PIC18_READ_PRE_INC  = 0b1011
C_PIC18_WRITE         = 0b1100
C_PIC18_WRITE_INC_BY2 = 0b1101
C_PIC18_WRITE_DEC_BY2 = 0b1110
C_PIC18_START_PGM     = 0b1111



def Set_LVP_PIC18():
   #held MCLR LOW
   GPIO.setup(PIC_MCLR,GPIO.OUT)
   GPIO.output(PIC_MCLR,False)

   #hdel PIC_CLK & PIC_DATA & PIC_PGM low
   GPIO.setup(PIC_CLK, GPIO.OUT)
   GPIO.output(PIC_CLK, False)
   GPIO.setup(PIC_DATA , GPIO.OUT)
   GPIO.output(PIC_DATA, False)
   GPIO.setup(PIC_PGM, GPIO.OUT)
   GPIO.output(PIC_PGM, False)

   sleep(0.001)
   #set PGM High
   GPIO.output(PIC_PGM, True)
   sleep(0.01)
   #Set MCLR High
   GPIO.output(PIC_MCLR, True)
   sleep(0.01)


def Release_LVP_PIC18():
   #set PGM Low
   GPIO.output(PIC_PGM, False)
   sleep(0.001)

   #held MCLR LOW
   GPIO.setup(PIC_MCLR,GPIO.OUT)
   GPIO.output(PIC_MCLR,False)

   #PIC_CLK & PIC_DATA has input
   GPIO.setup(PIC_CLK, GPIO.IN)
   GPIO.setup(PIC_DATA , GPIO.IN)

   sleep(0.001)
   #Set MCLR High
   GPIO.output(PIC_MCLR, True)


def LoadWordPic18(Value):
#  print "LoadWordPic18(", hex(Value), ")"
  GPIO.setup(PIC_DATA,GPIO.OUT)
  for loop in range(16):
     GPIO.output(PIC_CLK,True)
     GPIO.output(PIC_DATA,(Value & 1)==1)
     pass
     GPIO.output(PIC_CLK,False)
     pass
     Value = Value >> 1;


def ReadDataPic18():
  GPIO.setup(PIC_DATA,GPIO.IN)
  for loop in range(8):
    GPIO.output(PIC_CLK,True)
    pass
    GPIO.output(PIC_CLK,False)
    pass
  Value = 0
  for loop in range(8):
    GPIO.output(PIC_CLK,True)
    pass
    if GPIO.input(PIC_DATA):
        Value =  Value | (1 << loop)
    GPIO.output(PIC_CLK,False)
    pass
  return Value

def LoadCommandPic18(Value):
#  print "LoadCommandPic18(",hex(Value),")"
  GPIO.setup(PIC_DATA,GPIO.OUT)
  for loop in range(4):
     GPIO.output(PIC_CLK,True)
     GPIO.output(PIC_DATA,(Value & 8)==8)
     pass
     GPIO.output(PIC_CLK,False)
     pass
     Value = Value << 1;


def LoadInstruction(Instruction):
  LoadCommandPic18(C_PIC18_INSTRUCTION)
  LoadWordPic18(Instruction)


def ReadMemoryPic18Next():
  LoadCommandPic18(C_PIC18_READ_INC)
  return ReadDataPic18()


def LoadMemoryAddress(MemoryAddress):
  LoadInstruction(0x0E00 | ((MemoryAddress >> 16) & 0xff))
  LoadInstruction(0x6EF8)
  LoadInstruction(0x0E00 | ((MemoryAddress >> 8) & 0xff))
  LoadInstruction(0x6EF7)
  LoadInstruction(0x0E00 | (MemoryAddress & 0xff))
  LoadInstruction(0x6EF6)
 


def ReadMemoryPic18(MemoryAddress):
  LoadMemoryAddress(MemoryAddress)
  return ReadMemoryPic18Next()

def BulkErasePic18():
  print "Bulk Erase ",
  LoadMemoryAddress(0x3C0004)
  LoadCommandPic18(C_PIC18_WRITE)
  LoadWordPic18(0x0080)
  LoadInstruction(0x0000)
  LoadCommandPic18(C_PIC18_INSTRUCTION)
  sleep(0.1) 
  print "..... Done!"  

def WriteDataPic18(DataToBurn):
  LoadCommandPic18(C_PIC18_START_PGM)
  LoadWordPic18(DataToBurn)




def WriteAndWait():
  GPIO.setup(PIC_DATA,GPIO.OUT)
  GPIO.output(PIC_DATA, False)
  for loop in range(3):
     GPIO.output(PIC_CLK,True)
     pass
     GPIO.output(PIC_CLK,False)
     pass
  GPIO.output(PIC_CLK,True)
  sleep(0.005)
  GPIO.output(PIC_CLK,False)

def ProgramBurnPic18(pic_data, program_base, program_size):
  print "Writing Program",

  LoadInstruction(0x8EA6)
  LoadInstruction(0x8CA6)
  LoadInstruction(0x86A6)
 
  NumberOfPanel = program_size / Pic18PanelSize 

  WaitFlag= False
  for CountIdx in range(0,Pic18PanelSize,8):
    LoadMemoryAddress(0x3C0006)
    LoadCommandPic18(C_PIC18_WRITE)
    LoadWordPic18(0x0040)
    LoadInstruction(0x8EA6)
    LoadInstruction(0x9CA6)

    for PanelIdx in range(NumberOfPanel):
     AddressOffset = CountIdx + (PanelIdx * Pic18PanelSize)
     LoadMemoryAddress(AddressOffset)  
     for DataWordIdx in range(4):
       if DataWordIdx == 3 :
         if PanelIdx == (NumberOfPanel -1):
           WaitFlag= True
           LoadCommandPic18(C_PIC18_START_PGM)
         else:
           LoadCommandPic18(C_PIC18_WRITE)
       else:
         LoadCommandPic18(C_PIC18_WRITE_INC_BY2)
       PAddress = AddressOffset + (DataWordIdx * 2) + program_base;
#       LoadWordPic18(pic_data.get(PAddress) | (pic_data.get(PAddress+1) << 8))
       LoadWordPic18( 0)
       if WaitFlag:
         WriteAndWait()
         WaitFlag= False;
    if (CountIdx % 256)==0 :
      sys.stdout.write('.')
      sys.stdout.flush()
  print "Done!"




def ProgramBlankCheckPic18(program_size):
   print "Program blank check",
   LoadMemoryAddress(0)
   for l in range (program_size):
     Value = ReadMemoryPic18Next()
     if(Value != 0xff):
       print "*** CPU program at Address ", hex(l), " = ", hex(Value), " Failed!"
       return False
     if (l % 1024)==0 :
       sys.stdout.write('.')
       sys.stdout.flush()
   print "Passed!"
   return True
     
def ProgramCheckPic18(pic_data, program_base, program_size):     
   print "Program check ",
   LoadMemoryAddress(0x200)
   for l in range (0x200,program_size):
     Value = ReadMemoryPic18Next()
     if(Value != 0):
       print "Program address ", hex(l), " write  ", hex(0), " read" , hex(Value)
#     if(Value != pic_data.get(l + program_base)):
#       print "Program address ", hex(l), " write  ", hex(pic_data.get(l + program_base)), " read" , hex(Value)
       if l > 10:
         return False
     if (l % 1024)==0 :
       sys.stdout.write('.')
       sys.stdout.flush()
   print "Passed!"
   return True  

       





#=============  main ==========

if __name__ == '__main__':
  if len(sys.argv) is 2:
    HexFile = sys.argv[1]
  elif len(sys.argv) is 1:
    HexFile = ''
  else:
    print 'Usage: %s file.hex' % sys.argv[0]
    quit()


## load hex file if it exist
FileData =  IntelHex()
if len(HexFile) > 0 :
   try:
     FileData.loadhex(HexFile)
   except IOError:
     print 'Error in file "', HexFile, '"'
     quit()

PicData = FileData.todict()       
print 'File "', HexFile, '" loaded'



GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)


GPIO.setup(PIC_PGM,GPIO.OUT)
GPIO.setup(PIC_MCLR,GPIO.OUT)
GPIO.setup(PIC_CLK,GPIO.OUT)
GPIO.setup(PIC_DATA,GPIO.IN)

Set_LVP_PIC18()
CpuRevision = ReadMemoryPic18(0x3ffffe)
CpuId = ReadMemoryPic18Next()
CpuId = (CpuId << 8 ) | (CpuRevision & 0xE0)
CpuRevision = CpuRevision & 0x1f

print "Cpu Id =" , hex(CpuId)
print "Revision=" , hex(CpuRevision)

DataBase = 0xf00000
ProgramBase = 0
ProgramSize = 32768

if CpuId != 0x840 :
   print " not a pic18f258"
   quit()

BulkErasePic18()

if ProgramBlankCheckPic18(ProgramSize):
  ProgramBurnPic18(PicData,ProgramBase,ProgramSize)
  if ProgramCheckPic18(PicData,ProgramBase,ProgramSize):
    print "no Error. All Done!"

Release_LVP_PIC18()


Good night

Daniel

User avatar
ValterFukuoka
Posts: 94
Joined: Sat Oct 01, 2011 11:39 pm
Location: Japan
Contact: Website

Re: Add small cpu for R/C servo controlled

Wed Sep 18, 2013 4:16 pm

danjperron wrote: this is my latest source code for the pic18F... programming.
Daniel
Hi Daniel,

I'm trying the latest code on 2550 and 14K50...

[device id] - OK
[read] - OK

[bulk erase]
each mcu seems to have its own sequence... and, following the code you have for the F258, I created for F2550 and F14K50 (consulting respective datasheets)... BUT, both fail...

[write]
just trying to write few bytes at the beginning of flash (0x0000)... "something" happens, as "sometimes" the a portion of the values "get there, near the place" it was supposed to be burn... (lol)... STRANGE... and UNSTABLE behavior, because not every time it happens... (lol)
At least part of the procedure seems correct... but...

[voltage]
voltage seems to be OK, within the datasheet specs...

still trying... will keep reporting here...

danjperron
Posts: 3332
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Wed Sep 18, 2013 5:06 pm

Hi Valter,

I fix a small bug in my routine LoadCommandPic18
It should be LSB first.

Bulk erase does not work . The read works fine. I will have time to work more on it tonight.

Daniel

danjperron
Posts: 3332
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Thu Sep 19, 2013 2:56 am

It is Working.

I was able to figure out how to program the pic18F258.

I put the code on github https://github.com/danjperron/burnLVP

At the moment only the pic18f258 could be program. And only the program code.

I will work on the eerom data and the config later. Once this is done I will implement the

pic18Fxxxx familly. A little bit different but not much.

I correct the code and found out that I was not able to do bulk erase when the power was at 5V. The Adafruit converter versus the cpu are not playing nice together. I reduce the voltage to 4.5V , using a diode, and everything works fine.

I do have a code erase function which is not needed but it is working.

So this is good improvement. We should have a full working program at least for the
pic18Fxx2/pic18Fxx8 familly at the end of this week.

Tomorrow I will work on the eerom data and the Config.

Daniel

User avatar
ValterFukuoka
Posts: 94
Joined: Sat Oct 01, 2011 11:39 pm
Location: Japan
Contact: Website

Re: Add small cpu for R/C servo controlled

Thu Sep 19, 2013 7:26 am

danjperron wrote:It is Working.
I was able to figure out how to program the pic18F258.
I put the code on github https://github.com/danjperron/burnLVP
Daniel
Daniel, great!
I will run some test on the 18F2550/14K50 trying at least to get the "bulk erase", for this I will be trying to use 3.3v from RasPI and 5v using adapter...

[voltage levels...]
Yesterday I did a relative good look at 18F datasheet and was trying to know more about "its algorithm", I even found a working sample for Arduino here:
https://sites.google.com/site/thehighsp ... ino-pic18f
And yes, it confirm that the thinking (algorithm) is, more or less, correct... so I started to think about voltage but was unable to run it besides RasPI 3.3v... today I will try...

Valter

User avatar
ValterFukuoka
Posts: 94
Joined: Sat Oct 01, 2011 11:39 pm
Location: Japan
Contact: Website

Re: Add small cpu for R/C servo controlled

Thu Sep 19, 2013 8:52 am

(2550 and voltage)
Using the new (github) code, and the following sequence work for 2550 Bulk Erase with RasPI 3.3Volts:

Code: Select all

def BulkErasePic18F2550():
  print "Bulk Erase ",
  LoadMemoryAddress(0x3C0005)
  LoadCommandWordPic18(C_PIC18_WRITE,0x3F3F)
  LoadCode(0)  

  LoadMemoryAddress(0x3C0004)
  LoadCommandWordPic18(C_PIC18_WRITE,0x8F8F)
  LoadCode(0)  
  
  LoadCommandPic18(C_PIC18_NOP)
  sleep(0.015)
  LoadWordPic18(0)
  print "..... Done!" 
Valter

User avatar
ValterFukuoka
Posts: 94
Joined: Sat Oct 01, 2011 11:39 pm
Location: Japan
Contact: Website

Re: Add small cpu for R/C servo controlled

Thu Sep 19, 2013 9:25 am

(18F2550 and WRITE - success)

I am able to write to the Program Memory of the 18F2550, using a little "trick" within a special NOP routine (the "end" NOP)...

Code: Select all

#-----------------------------------------------------------------------------

def ProgramBurnPic18f2550():
  print "Writing Program",

#step 1 (prg data specs page 17)
  LoadCode(0x8EA6)
  LoadCode(0x9CA6)

#step 2 (prg data specs page 17)
  LoadMemoryAddress(0x0)

#step 3 (prg data specs page 17)
  LoadCommandWordPic18(C_PIC18_WRITE_INC_BY2,0x7777)
 
#step 4 (prg data specs page 17)
  LoadCommandWordPic18(C_PIC18_START_PGM,0x8888)

  myProgramNOP2550(C_PIC18_NOP)
  LoadCode(0)

# I think it is better to switch out and in from LVP here...
# ..........................................................
# ..........................................................
# ..........................................................

  print "..... Done!" 

#-----------------------------------------------------------------------------

def myProgramNOP2550(Value):
  #little "trick" on the 4th bit rise...

  GPIO.setup(PIC_DATA,GPIO.OUT)
  
  for loop in range(3):
     GPIO.output(PIC_DATA,(Value & 1)==1)
     GPIO.output(PIC_CLK,True)
     GPIO.output(PIC_CLK,False)
     Value = Value >> 1;

#bit 4 comes here...
  sleep(0.001)
  GPIO.output(PIC_CLK,True)
  sleep(0.02)
  GPIO.output(PIC_CLK,False)
  sleep(0.01)

#-----------------------------------------------------------------------------

Will do some tests using this setup... and report results...

Still using RasPI 3.3Volts...

Valter

User avatar
ValterFukuoka
Posts: 94
Joined: Sat Oct 01, 2011 11:39 pm
Location: Japan
Contact: Website

Re: Add small cpu for R/C servo controlled

Thu Sep 19, 2013 10:21 am

(18F14K50 - Bulk Erase and Write - OK)

The same algorithm that works for the 18F2550 also works for the 18F14K50... some bytes from the sequence codes are differents...

Code: Select all

#-----------------------------------------------------------------------------

def BulkErasePic18F14K50():
  print "Bulk Erase ",
  LoadMemoryAddress(0x3C0005)
  LoadCommandWordPic18(C_PIC18_WRITE,0x0F0F)
  LoadCode(0)  

  LoadMemoryAddress(0x3C0004)
  LoadCommandWordPic18(C_PIC18_WRITE,0x8F8F)
  LoadCode(0)  
  
  LoadCommandPic18(C_PIC18_NOP)
  sleep(0.015)
  LoadWordPic18(0)
  print "..... Done!" 

#-----------------------------------------------------------------------------

def ProgramBurnPic18f14k50():
  print "Writing Program",

#step 1 (prg data specs page 16)
  LoadCode(0x8EA6)
  LoadCode(0x9CA6)
  LoadCode(0x84A6)
  
#step 2 (prg data specs page 16)
  LoadMemoryAddress(0x0)

#step 3 (prg data specs page 16)
  LoadCommandWordPic18(C_PIC18_WRITE_INC_BY2,0x7777)
 
#step 4 (prg data specs page 16)
  LoadCommandWordPic18(C_PIC18_START_PGM,0x8888)

  myProgramNOP2550(C_PIC18_NOP)
  LoadCode(0)

# I think it is better to switch out and in from LVP here...
# ..........................................................
# ..........................................................
# ..........................................................

  print "..... Done!" 

#-----------------------------------------------------------------------------
The "closing" NOP is the same as the 18F2550...
Using RasPI 3.3Volts.
Valter

Return to “Automation, sensing and robotics”