2013-07-30

First contact with Microchip PIC24: upgrading PICkit3 firmware

Short version (for those who just need the solution)

If you get the error message that your PICkit3's firmware is out-of-date, go get MPLAB 8 (only for Windows) and let it upgrade the firmware automatically (click Programmer -> Select Programmer -> PICkit3). Then go back to MPLAB X and program your device (of course, select correct target chip). I cannot believe this solution either. But it is from Microchip's support team.

Full story

I will be team-teaching Embedded System class next semester, with another faculty member who offered this class previously, using Microchip's PIC24 architecture. However, my first contact with PIC24 is very frustrating.

Microchip released a new IDE, MPLAB X, for developing and downloading programs for their chips. Unlike previous one, it supports Linux (whereas ATMEL and Texas Instruments have done years ago). I ordered PICkit3 (the downloader or hardware programmer) and Explorer 16 (the dev kit) from Microchip in the hope to use their technical support later.

Out-of-date firmware in PICkit3

However, downloading programs using PICkit3 and MPLAB X frustrated me a lot. The error message didn't help or the information was not consistent with what happened.

When I first finished a hello-world program and tried to download, I got a self-contradictory error:
Connecting to MPLAB PICkit 3...
Firmware Suite Version.....01.22.08
Firmware type..............dsPIC33F/24F/24H

Your PICkit 3 firmware version is too old. You must have firmware version 01.22.08 or higher to use MPLAB X. Connection Failed.

What?

I tried again and it seems that MPLAB X helped me update the firmware:
Connecting to MPLAB PICkit 3...
Firmware Suite Version.....01.22.08
Firmware type..............dsPIC33F/24F/24H


Downloading Firmware...
Downloading bootloader
Bootloader download complete
Programming download...
Connection Failed.

So I tried again and but even the firmware was messed up:
Connecting to MPLAB PICkit 3...
Firmware Suite Version.....01.22.08
Firmware type..............Unknown Firmware Type


Downloading Firmware...
Downloading RS...
RS download complete
Programming download...
Connection Failed.

Since the firmware was never updated, I contacted Microchip's technical support.

Troubleshooting

After a week, they replied and gave me a very weird solution: updating the firmware using an older version (8) of MPLAB on Windows. I tried and I even got error messages on Windows.

Just when I was about to send the Pickit3 back, I plugged it back to my Linux box for last try and some miracle happened. (I guess what happened was that MPLAB 8 on Windows upgraded the firmware to a version from which MPLAB X kept upgrading.)
Connecting to MPLAB PICkit 3...
Firmware Suite Version.....01.28.90
Firmware type..............PIC18F


Downloading Firmware...
Downloading AP...
AP download complete
Programming download...
Firmware Suite Version.....01.28.92
Firmware type..............dsPIC33F/24F/24H

Target detected
Device ID Revision = 3044

The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x3ff

Programming...
Programming/Verify complete 

Conclusion

It took me 2 weeks just to run a hello-world program. I have to say that Microchip does not pave a way for fresh people to learn their stuff. Just a few example:
  • Why did they send me hardware with firmware greatly out-of-date anyway? And this cannot be solved in their latest software. Instead, I need a previous version to do that. This is a broken solution.
  • The user guide for Explorer16 freezes at its 2005 version whereas the hardware programmer and IDE have changed a lot since then. For example, I didn't know how to connect the Explorer16 with PICkit3. There are two ways and one might burn your circuit.
  • The "official" PIC24 textbook, written by a Microchip engineering, has wrong example programs. Like the very early one below:
    int main(void) {
        PORTA=0xff;  
        TRISA = 0;  
        return 0;
    }
    How can you output data to Port A before it is set as an output? The first two lines need to be swapped. You won't get the correct value even in software-emulated debugger.
  • The unreliability issue of the hardware programmer PICkit3. I will mention it in another post.

Hence, my first contact with Microchip's solution was not pleasant.

2013-07-28

multidimensional copy in MATLAB's repmat() and NumPy's tile()

repmat is a MATLAB function. A while ago, I had to translate a MATLAB script into Python and it had a lot of repmat(). However, the doc "NumPy for MATLAB Users" only covers the case to duplicate a matrix in 2D, e.g., repmat(a, m, n). How about repmat(a, [m n p])?

The answer is
numpy.array([np.tile(x, (m,n)) for i in xrange(p)])
Please see my demo below. Please also note that using nested tile, i.e.,
np.tile(np.tile(x, (2,2)), (1,3))
does not help-you still get a 2D matrix.

In [1]: import numpy as np

In [2]: x=np.array([[1,2,3],[4,5,6]])

In [3]: np.tile(x, (2,2))
Out[3]: 
array([[1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6],
       [1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6]])

In [4]: np.tile(np.tile(x, (2,2)), (1,3))
Out[4]: 
array([[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6],
       [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6]])

In [5]: np.tile(np.tile(x, (2,2)), (3,1))
Out[5]: 
array([[1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6],
       [1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6],
       [1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6],
       [1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6],
       [1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6],
       [1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6]])

In [6]: np.array([np.tile(x, (2,2)) for i in xrange(3)])
Out[6]: 
array([[[1, 2, 3, 1, 2, 3],
        [4, 5, 6, 4, 5, 6],
        [1, 2, 3, 1, 2, 3],
        [4, 5, 6, 4, 5, 6]],

       [[1, 2, 3, 1, 2, 3],
        [4, 5, 6, 4, 5, 6],
        [1, 2, 3, 1, 2, 3],
        [4, 5, 6, 4, 5, 6]],

       [[1, 2, 3, 1, 2, 3],
        [4, 5, 6, 4, 5, 6],
        [1, 2, 3, 1, 2, 3],
        [4, 5, 6, 4, 5, 6]]])


in MATLAB
>> repmat(x, [2,2,3])

ans(:,:,1) =

1     2     3     1     2     3
4     5     6     4     5     6
1     2     3     1     2     3
4     5     6     4     5     6


ans(:,:,2) =

1     2     3     1     2     3
4     5     6     4     5     6
1     2     3     1     2     3
4     5     6     4     5     6


ans(:,:,3) =

1     2     3     1     2     3
4     5     6     4     5     6
1     2     3     1     2     3
4     5     6     4     5     6

2013-07-13

Eigenvalue solving by lobpcg in Python

Scipy provides two sets of functions for solving eigenvalue problems:
  1. ARPACK-wrapped scipy.sparse.linalg.eigsh and scipy.sparse.linalg.eigs, and 
  2. LOBPCG-based scipy.sparse.linalg.lobpcg
The latter is new to me. However I notice something interesting.

First, the option largest= seems to work in the opposite way. If I set is as largest=False, I actually get largest eigenvalues. To get smallest eigenvalues, I set it as largest=True.

Second, the function requires an initial approximation to the eigenvectors, which is not easy for new users. As someone pointed out, sometimes it takes "a very large number of iterations to get the correct eigenvalues."

Nevertheless, the solver is not too bad compared with ARPACK-wrapped eigsh (for real symmetric square matrix or complex hermitian matrixes) and eigs (for non-symmetric square matrixes). For example, given two matrixes, I got the two eigenvalues for general eigenvalue problem:
  • by eigsh: [-1.0385276515935676e-16, 0.9194804029047039, 3.7579933101613547, 8.257545329015041, 10.44664798242384]
  • by lobpcg: [7.0329601669327232e-16, 0.91948040290470379, 3.7579933101613623, 8.2575453290150449, 10.446647982423839]