Thursday, March 1, 2018

PKOB device support

We can add "-J-Dpkobskde.support.same.as.pk3=true" to the IDE "default_options" in ../v3.xx/mplab_ide/etc/mplab_ide.conf, it will make the PK3 device support as the default for PKOB.

PlatformToolMeta.java
/**
* This method should only be called if the user passed the following
* option to the IDE: -J-Dpkobskde.support.same.as.pk3=true
* 
* The user is telling the IDE to treat the PKOB SKDE platform tool meta
* as if it had the same device support as PK3.
* We do this to allow APPS to test a new PKOB SKDE without having to
* change the PKOB SKDE xml deviceSupport element.
*/

Wednesday, April 26, 2017

Create Large Arrays for PIC24 or dsPIC33

With C30, in the compiler option, check "Enable large arrays".

With XC16, check "Allow arrays larger than 32K" in the Memory model.

Thursday, March 16, 2017

UART JTAG

XTAL = 20MHz

UART = 921,600

1. Setup UART
2. Test loopback
3. Test SPI
4. Test SPI loopback

=======
Program the Test Chip to Blink LED
Enter Enhanced ICSP
Exit ICSP
Send Instruction to IR, and Check IR Output
Read Device ID from DR

========
SET_ICSP_PINS arg[1] = pin states
<7 – 4> unused
<3> PGD logic level
<2> PGC logic level
<1> 1= PGD input, 0= output
<0> 1= PGC input, 0= output

========
FASTDATA 0xF..FF20.0000 to 0xF..FF20.000F

========
Ref Clk: 921,600: 1-182, 2-364

PLLMULT:  Ref:20MHz(Sys)=>1Hz
    1=>3Hz, 0=>19Hz, 2=>20Hz, 3=>6Hz, 4=>8Hz, 5=>12Hz, 6=>19Hz
    1=>3Hz, 0=>19.38Hz, 2=>20Hz, 3=>4.42Hz, 4=>5.89Hz, 5=>12Hz, 6=>17.7Hz

========
ICSP speed: 10MHz (P1,TPGC)
MTAP speed: 12MHz
ETAP  speed: 10MHz

========
JTAGEN bit: http://www.microchip.com/forums/m947541.aspx



Debug Vector



Thursday, February 9, 2017

PIC32MM support - PICkit 2

Code Memory Size
Device
Row Size
(Words)
Page Size
(Words)
Boot Flash Size
PIC32MM
64
512
0x1FC00000-0x1FC016FF (5.75KB)
PIC32MX1/2
32
256
0x1FC00000-0x1FC00BFF (3KB)
PIC32MX3/4/6/7
128
1024
0x1FC00000-0x1FC02FFF (12KB)


int progMemP32 = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem;
int bootMemP32 = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].BootFlash;

ProgramMem = (0x10000+0x1700)/4 = 17856 (including boot flash)
BootFlash = 0x1700/4= 1472

PE Command
   remove 0x03: WORD_PROGRAM
   add 0x0E: DOUBLE_WORD_PGRM

Configuration Bits: Double Word

FSIGN (0x1FC0.17E0) 0x7FFF (must)


Code Execution from RAM: A000:0800  == not required ==
Set CFGCON.EXECADDR<7:0> to 0x02
CFGCON at 0xBF80.3B00

PICkitFunctions.searchDevice
=========================

Move PE Loader to 0xA000.0200, PE to 0xA000.0300

v
PIC32BlankCheck
v
PIC32Read
v
PIC32CRC
v
PIC32Verify
v
PIC32Erase


Change the ChipEraseScript
v
PIC32Write


if the JTAGEN is programmed, the MCHP TAP controller must be selected immediately after any PROGRAM command.  This can be done by using a SendCommand(MTAP_SW_MTAP) pseudo operation.  After 400 us, the EJTAG TAP controller can be selected again by calling the SendCommand(MTAP_SW_ETAP) pseudo operation.  -- ch 16.3

ImportHex - 90%, UserID (WIP)
ExportHex

Configuration Bits - DialogConfigEdit - OK(display)

=============================

PE Loader: (swap HI/LO word, 41A3, FF20, 41A5, DEAD, 6A30, 6930...)



ChipEraseScript:
 $JT2_SENDCMD 07        //<--- SendCMD(MTAP_CMD)
 $JT2_XFERDATA8_LIT FC  //<--- Xferdata(MCHP_ERASE)
 $JT2_XFERDATA8_LIT D0  //<--- Xferdata(MCHP_DE_ASSERT_RST)
 $DELAY_LONG 4A



===== Notes: JTAG =====

MTAP
  - MTAP_SW_MTAP[5h0x04]
  - MTAP_SW_ETAP[5h0x05]
  - MTAP_IDCODE[5h0x01]
  - MTAP_COMMAND[5h0x07]
    - MCHP_STATUS
    - MCHP_ASSERT_RST
    - MCHP_DE_ASSERT_RST
    - MCHP_ERASE

EJTAG
  - ETAP_ADDRESS[5h0x08]
  - ETAP_DATA[5h0x09]
  - ETAP_CONTROL[5h0x0A]
  - ETAP_EJTAGBOOT[5h0x0C]*
  - ETAP_FASTDATA[5h0x0E]*
  # XferFastData
      ETAP_FASTDATA
  # XferInstruction
      ETAP_CONTROL
      ETAP_DATA
      ETAP_CONTROL



Sunday, December 11, 2016

USB HID Host PICkit Bridge - PIC32MX

1(OK). Check Configuration Words for CPU/Peripheral System Frequency 48MHz in HardwareProfile.h
     Osc: 20 MHz
     try 48MHz first, if not stable, try 40MHz...(TCHIP-USB-MX250F128B is 40MHz)

2(OK). Check Baud Rate: 38400 first, then 921600. in HardwareProfile.h
    #define BAUDRATE2       38400UL //57600UL
    #define BRG_DIV2        4
    #define BRGH2           1

3(OK). Modify usbTPL[] in usb_config.c,  in usb_config.h
   #define NUM_TPL_ENTRIES 1

4(OK). usb_host_local.h
#define USB_SOF_THRESHOLD_64                0x5A    //0x4A will cause frame error

5. change io_mapping
    in HardwareProfile.h
    

6(OK). modify timer in InitializeTimer()

7. Test Timer/SYSCLK setup  Blink LED

8. ERRATA Don't turn cache and ... on due to Double Write on Peripheral
    Implement DMA (done)

9. Latency of the HC-06 bluetooth module is about 40ms.  The latency of a regulator PIC18F2550 USB Serial COM Port is about 1.4ms.

10. Implement Circular Buffer (Queue) for Commands.  It can only improve the regular write command, but not read command due to the latency of the bluetooth module.

================
modify usb_config.h usb_config.c
modify usb_host_local.h

add PIC32MX270F256B support

1. Update PK2DeviceFile.xml

2. add and set breakpoint in searchDevice() in PICkitFunctions.cs
    if (familyIndex != 0x10) return false; 

3. set breakpoint in DetectDevice() in PICkitFunctions.cs


USB JTAG PIC16F1459

I have been trying to make a USB Blaster Clone to program the Altera MAX 10.  I found a Japanese website, USB-Blasterもどきの製作, showing how to do it with PIC18F14k50. Unfortunately I don't have it with me, but I have PIC16F1459 and it seems it's a good candidate for the replacement.  PIC16F1459 has an internal 48MHz(3x PLL) clock which can sync with USB Host's frame signal to provide the required USB clock(± 0.20% accuracy) without an external oscillator.

After I started the conversion, I found out the original code is written with the old Microchip USB stack, so I also have to figure out how to use the newer USB stack to do it, actually it took more time to know how the old USB stack works.  After a couple of weeks, I stopped working on it and went for some other errands.

A couple of days ago, I found there is a new dev kit for MAX 10 issued by CQ Publisher.  The author put the code on the web.  Although it only supports JTAG programming mode, it's good enough to MAX 10.  It really helped me out, because it's developed with a newer USB Stack.

Originally I was thinking maybe I can run PIC16F1459 with the 3.3V source on the MAX 10 dev board, although the datasheet says the minimum is 3.6V.  I did get the USB to work, but the IO signal is unstable.  I thought that might be caused by the breadboard design, after spent another day, I gave up with the 3.6V idea, so I just used the 5V source and used 560ohm/560ohm as the level shifter.  According to the MAX 10 document, it's better to use a lower voltage at about 2.5V for the JTAG I/O, because there is no clamping diode for overshoot protection on those pins.

After a couple of tries... I finally got it done!

People who are interested with this topic can also take at a look at the following links:

USB JTAG Adaptor
OpenOCD usb_blaster.c

I am thinking PSoC 5LP is another good candidate for doing this job, because it has several special IO(SIO) pins which can use the internal D/A converter as the Vref for a more flexible IO voltage.  Cypress provides a very affordable dev kit CY8CKIT-059 PSoC® 5LP Prototyping Kit for developers.  I will give it a try in the following weeks.




Tuesday, December 15, 2015

Bluetooth + PICkit 2c

Programming PIC18F2550 with Bootloader firmware (using PICkit2)
   test bootloader by checking the LED (button at, LED at)

Programming PIC18F2550 with PICkit2 firmware
   check PICkit2App

[Bluetooth module: AT COMMAND (NO \n\r)
  1.AT
  2.AT+NAME
  3.AT+PIN      (xxx8)
  4.AT+BAUD5    (for 19200)]

Write a simple UART program to echo the data sent from the host.

Setup Vdd, Vpp circuit
   Vpp_Feedback
   Vdd_Target_FB

   Vpp_ON
   Vpp_PUMP

   Vdd_TARGET_N
   Vdd_TARGET_P
   Vdd_TARGET_adj

Update firmware with serial port support
   programming the new firmware to PIC18F2550

XC8       
section //  linker class  // memory
text; CODE, Flash
bss0; BANK0; RAM
data0; BANK0; RAM
idata; CODE; Flash

PSECT bss0, class=BANK0
PSECT text, class=CODE










Using Executive Code Memory as the Bootloader

---- Diagnosis Phase ----

Open RIPE_XX_XX.hex in MPLAB X.

Change Base Address of Code
:020000040100f9

Export the Assembly Code

--> Interrupt Vector

PICKIT DIY

Originally working on dsPIC30F2011 with bluetooth module.

Work on PIC18LF2455, first using PICkit 2 to flash bootloader to PIC18LF2455.

RB5 - Push Button (Enter Bootloader Mode)
RC0 - Busy LED

//PICkit User Guide
PK2Error0018: Unable to enter bootloader

Description: PICkit 2 Debug Express was unable to access the PICkit 2 unit bootloader
while updating the PICkit 2 Operating System.

Suggested Actions: Disconnect the PICkit 2 unit from USB and the target board. While
holding the PICkit 2 unit push button down, plug the PICkit 2 unit back into the USB
port. The BUSY LED should flash slowly. If it does not, the bootloader may be
corrupted. Contact Microchip about the problem.
//******************
see boot.h for memory maps for remapped vectors

    #define RM_RESET_VECTOR             0x002000
    #define RM_HIGH_INTERRUPT_VECTOR    0x002008
    #define RM_LOW_INTERRUPT_VECTOR     0x002018

-------------------------------
// Write key 0x5555 at last memory location to tell bootloader FW loaded.
             if (!Pk2.BL_WriteFWLoadedKey())
             {
                 displayStatusWindow.Text = "Error loading Operating System.";
                 displayStatusWindow.BackColor = Color.Salmon;
                 return;
             }

---------------------------------
 public static bool BL_WriteFWLoadedKey()
        {
            byte[] flashWriteData = new byte[3 + 32];  // 3 address bytes plus 32 data bytes.
         
            flashWriteData[0] = 0xE0;
            flashWriteData[1] = 0x7F;
            flashWriteData[2] = 0x00;   // Address = 0x007FE0
            for (int i = 3; i < flashWriteData.Length; i++)
            {
                flashWriteData[i] = 0xFF;
            }
            flashWriteData[flashWriteData.Length - 2] = 0x55;
            flashWriteData[flashWriteData.Length - 1] = 0x55;
            return BL_WriteFlash(flashWriteData);
        }

----------------------------------------
// boot_main.c
    if (PROG_SWITCH) {         // is the push button pressed?

        // if no, does program memory location 0x7FFE contain 0x55?

        Temp = * ((rom far char *) 0x7FFE);

        if(Temp == 0x55) {

            _asm goto RM_RESET_VECTOR _endasm

        }

    } // end if (PROG_SWITCH)

// disassembly
               Line         Address         Opcode         Label         DisAssy    
               1845           0E68           AA81                          BTFSS PORTB, 5, ACCESS
               1846           0E6A           D00D                          BRA 0xE86      
               1847           0E6C           0EFE                          MOVLW 0xFE     
               1848           0E6E           6EF6                          MOVWF TBLPTRL, ACCESS
               1849           0E70           0E7F                          MOVLW 0x7F     
               1850           0E72           6EF7                          MOVWF TBLPTRH, ACCESS
               1851           0E74           6AF8                          CLRF TBLPTRU, ACCESS
               1852           0E76           0008                          TBLRD*         
               1853           0E78           50F5                          MOVF TABLAT, W, ACCESS
               1854           0E7A           6EDF                          MOVWF INDF2, ACCESS
               1855           0E7C           0E55                          MOVLW 0x55     
               1856           0E7E           5CDF                          SUBWF INDF2, W, ACCESS
               1857           0E80           E102                          BNZ 0xE86      
               1858           0E82           EF00                          GOTO 0x2000    
               1859           0E84           F010                          NOP          

// HEX file
:100E70007F0EF76EF86A0800F550DF6E550EDF5CE6

change 7F to 5F for 24Kbyte device, 5FFF(24K device), 7FFF(32K device) end of on-chip program memory. E6 is the checksum
to
:100E70005F0EF76EF86A0800F550DF6E550EDF5C06

>PIC18F2550
:100D400000430031003800460032003500350030E5
>PIC18F2455
:100D400000430031003800460032003400350035E1


// user program can start from address 1000 

--------------------
Program Linker Options

Memory Model:: ROM Ranges: default,-5FF0-5FFF
Additional Options:: Codeoffset: 0x2000
--------------------
PIC18LF25k50 Vusb3v3 tied to Vdd  (no internal 3.3V LDO regulator)
PIC18F25k50 Vusb3v3 tied to capacitor (using internal 3.3V LDO regulator)
--------------------
VDD ranges are different for PIC18LF25K50 and PIC18LF2550
notes: PIC18LF25K50 is like PIC18F2550, PIC18F25K50 is like PIC18LF2550.

-------------------

PICkit 2 firmware porting to XC8

--------- Bootloader ---------
1. Create MPLAB X project.
2. Update Compiler Include Path
3. Generate Configuration File
4. Compile each c file
     a. Change typedefs.h
     b. Comment out "pragma udata", "pragma code"
     c. Change _asm to asm()
#pragma psect usbram XX -> using @0x400...

    if (U1IEbits.UERRIE && U1IRbits.UERRIF)
    {
#if defined (DEBUG_ENABLE)
        DEBUG_PutChar('#');
        DEBUG_PutHexUINT8( U1EIR );
#endif

* * * *
porting to XC8 on halt due to unknow USB issues ... try another approach using MLA usb bootloader...

1. Copy Project
2. Change Descriptor Data
3. Change Enter Bootloader Button to RB5 as in PICkit 2
4. Change Command
5. Check BootPICxxx.h and VectorRemap.asm for Vector setup

    #define REMAPPED_APPLICATION_RESET_VECTOR       0x2000
    #define APP_SIGNATURE_ADDRESS                   0x7FFE
    #define APP_SIGNATURE_VALUE                     0x5555


======== PICkit Bootloader / Find PICkit Device==========
1. Send Command 'v', if the return data[1]=0x76, it's the bootloader
    data[6]='B',data[7]=Major_Version,data[8]=Minor_Version

2. Configuration Bits, WRTB, WRT0 is clear for Write Protection from 0x0000~0x1FFF

3.
void ReadVersion(void);   //test OK
void ReadProgMem(void);   //test OK
void WriteProgMem(void);  //test OK
void EraseProgMem(void);  //test OK
void ReadEE(void);   //?
void WriteEE(void);  //?

4. PICkit2 Firmware (Reserve the last 32 bytes block for signature 0x5555@0x7FFE

5. Download the firmware to PIC18F2550, OK.







dsPIC33EP256MC202 - PICkit2

family ID: 5
search Priority: 2
using example from dsPIC33FJ256MC510,  dsPIC33FJ256MC710, dsPIC33FJ128MC202

family ID: 1
search Priority: 0
using example from PIC24FJ64GB002

test get DeviceID

delay long: 5.46ms
delay short: 21.3us

dsPIC33EP256MC202
   TBLPAG: 0x54
   VISI: 0x0F88, VISI>>1:0x07C4
        MOV W0, TBLPAG, 8802A0
        MOV W0, VISI          887C40
        MOV #VISI, W7        20F887

  double word write cycle time: 346 FRC cycles, 54.4us ~ 60us

dsPIC33F
   TBLPAG: 0x32
   VISI: 0x0784, VISI>>1:0x03C2
        MOV W0, TBLPAG, 880190
        MOV W0, VISI,         883C20
        MOV #VISI, W7        207847

    row(64 instructions) programming time: 1.28 ms

<ICSP>
enter ICSP (OK)

get DeviceID (OK)

disable PE33 in PICkit2.ini

adjust DialogConfigEdit.cs (OK)

create simple LED blinking app running with internal oscillator. (OK)

update Script
   ScriptLength cannot be greater than 61
   [#ChipEraseScript]
   [#ConfigRdScript]   <--- need to change to byte
   [#UserIDRdScript]  <--- need to change byte to word

   [#ProgMemAddrSetScript]
        MOV ADDR_L, W6
        MOV.B ADDR_H, W0
        MOV W0, TBLPAG
        MOV #VISI,W7

   [#ProgMemRdScript]
        TBLRDL [W6],[W7]
        TBLRDH [W6++],[W7]

===========
   [#ProgMemWrPrepScript]
        MOV #0x4001, W10     //word write
        MOV W10, NVMCON
        MOV ADDR_L, W7
        MOV.B ADDR_H, W0
        MOV W0, TBLPAG

   [#ProgMemWrScript]
   [#ConfigWrScript]
   [#UserIDWrScript]

test erase script(should be OK)

test programming script(OK)
   add script buffer location (number): PROGMEM_WR2 = 30;  // not required

test configure write script (OK)

test userid write script(Pending)

add device family dsPIC33EP  FamilyID:18, FamilyType:18, SearchPriority:3 (OK)

enable PE33 in PICkit2.ini (as default)
---
load PE executive (OK)

<Enhanced ICSP>

PE: A delay of 25 ms is required between commands.

PE_DownloadAndConnect(OK)
PE_Connect (OK)
PE_Read(OK)
      add a dealy_short(1) between read_byte and read_byte_buffer
PE_BlankCheck(OK)
      PE bug: Expected Reponse[0] should be 0x1E, but returned 0x1A
PE_Write(OK) might have problem with the last block(32 instructions) with the Configuration Bits
PE_Verify(OK)

Export the script(OK)

Add CRC check (WIP)

Apply Object Oriented Design...



[get device ID script]
 $COREINST24 00 02 04  //goto 0x200
 $COREINST24 00 02 04
 $NOP24
 $NOP24
 $NOP24
 $COREINST24 00 02 04
 $NOP24
 $COREINST24 F0 0F 20  //mov 0xff, w0
 $COREINST24 90 01 88  //mov w0, tblpag
 $COREINST24 06 00 20  //mov 0x0000, w6
 $NOP24
 $NOP24
 $COREINST24 07 00 20  //mov 0x0000, w7
 $COREINST24 B6 0B BA  //mov [w6++], [w7] == mov [w6++],w0
 $NOP24
 $NOP24
 $COREINST24 20 3C 88  //mov w0, visi
 $NOP24
 $VISI24
 $NOP24
 $LOOP 0D 01

HID Bootloader - PIC18F2550 XC8

Duplicate configuration

Follow comments in main.c to 

change PIC to PIC18F2550
setup board parameters in main.c, HardwareProfile.h, usb_config.h

in usb_config.h
#define ENABLE_USB_LED_BLINK_STATUS

dsPIC33E/PIC24E Flash Programming Notes

For some devices without volatile configuration bits have special programming requirements.

Devices such as dsPIC33EP256MU806, MU810, MU814, dsPIC33EP512GP806, MC806, MU810, MU814...

See the Flash Programming Spec 70619B, Table 6-5, Table 7-1, for clock frequency > 2MHz

There is no such requirement for dsPIC33EP256MC20X