DsPIC30F 5011 Development Board

From OpenCircuits
(Difference between revisions)
Jump to: navigation, search
(dsPicProgrammer (Java-based Multi-Platformed))
Line 451: Line 451:
  
 
==[[Programming Tips]]==
 
==[[Programming Tips]]==
 +
*Description on developing drivers with POSIX API
  
 
==Bootloader Development==
 
==Bootloader Development==

Revision as of 00:21, 22 August 2008

Contents

Introduction

Features of dsPIC30F5011

  • 2.5 to 5V
  • Up to 30MIPs
  • High current/sink source I/O pins: 25mA
  • DSP Instruction Set
  • Dual programming techniques: ICSP and RTSP
  • UART: up to 2 modules
  • I2C: up to 1Mbps
  • 10-bit A/D, 1.1 Msps
  • 12-bit A/D, 200 ksps
  • 44K flash (66Kb), 4Kb RAM, 1Kb EEPROM
  • No DAC
  • Pin-to-pin compatible with other dsPICs
Table 1.1 Comparison with Compatible dsPICs
dsPic Price
US$
MIPs Flash
(kB)
RAM
(kB)
EEPROM
(kB)
I/O ADC
12-bit
IC OC Motor
Ctrl
Timers QEI UART SPI I2C CAN Codec
30F5011 5.91 30 66 4 1 52 16 8 8 0 5x16bit
2x32bit
0 2 2 1 2 1
30F6011A 7.73 30 132 6 2 52 16 8 8 0 5x16bit
2x32bit
0 2 2 1 2 0
30F6012A 7.85 30 144 8 4 52 16 8 8 0 5x16bit
2x32bit
0 2 2 1 2 1
33FJ128GP206 4.62 40 128 8 0 53 18 8 8 0 9x16bit
4x32bit
0 2 2 1 0 1
33FJ128GP306 4.81 40 128 16 0 53 18 8 8 0 9x16bit
4x32bit
0 2 2 2 0 1
33FJ128GP706 5.49 40 128 16 0 53 18 8 8 0 9x16bit
4x32bit
0 2 2 2 2 1
33FJ128MC506 4.97 40 128 8 0 53 16 8 8 8 9x16bit
4x32bit
1 2 2 2 1 0
33FJ128MC706 5.38 40 128 16 0 53 16 8 8 8 9x16bit
4x32bit
1 2 2 2 1 0
33FJ256GP506 6.11 40 256 16 0 53 18 8 8 0 9x16bit
4x32bit
0 2 2 2 1 1

Web Page

Forum

References

Code Examples


Programming Methods

  • There are 2 programming methods: In-Circuit Serial Programming (ICSP) and Run-Time Self-Programming (RTSP)
  • ICSP allows the devices to be programmed after being placed in a circuit board.
  • RTSP allows the devices to be programmed when an embedded program is already in operation.

ICSP: External Programmer (ICD2)

  • Two types of ICSP are available: ICSP and Enhanced ICSP. Both of them require setting MCLR# to VIHH (9V – 13.25V).
  • Standard ICSP
    • Use external programmer (e.g. MPLAB® ICD 2, MPLAB® PM3 or PRO MATE® II) only.
    • Required low-level programming to erase, program and verify the chip.
    • Slower, because codes are serially executed.
    • Program memory can be erased using Normal-Voltage (4.5 – 5.5V) or Low-Voltage (2.5V – 4.5V).
  • Enhanced ICSP
    • Use external programmer and Programming Executive (PE).
    • PE is stored in the on-chip memory.
    • PE allows faster programming.
    • PE can be downloaded to the chip by external programmer using the standard ICSP method.
    • PE contains a small command set to erase, program and verify the chip, avoiding the need of low-level programming.

Hardware Interface

Table 2.1 Pin Used by ICSP
Pin Label Function Pin Number
MCLR# Programming Enable 7
VDD Power Supply 10, 26, 38, 57
VSS Ground 9, 25, 41, 56
PGC Serial Clock 17
PGD Serial Data 18


Table 2.2 Available Programmers in the Market
Product Name Interface with PC Interface with Device Price (US) Postage (US) Total (US)
MPLAB® ICD 2 USB or RS232 6-PIN RJ-12 connector $159.99 - -
Full Speed USB Microchip ICD2
Debugger and Programmer
USB 6-PIN ICSP connector
6-PIN RJ-12 connector
$72.00 $12.00 $84.00
Mini Microchip Compatible ICD2
Debugger and Programmer
RS232 6-PIN ICSP connector
6-PIN RJ-12 connector
$45.00 $10.00 $55.00
ICDX30 RS232 6-pin RJ-11 $51.00 $47.46 $98.46
*Clone Microchip ICD2 (Now Using) USB 6-pin flat cables $30.00 $12.00 $42.00


Table 2.3 DIY ICD 2 Programmer Circuit
Source Schematic PIC16F877A Bootloader
Patrick Touzet Yes HEX
Nebadje Yes Zip

Software Interface

  • The program can be written and compiled in an Integrated Development Environment (IDE) using either Assembly or C. The complied codes are then loaded to the device through the external programmer.


Table 2.4 Summary of IDE
Product Name Features OS Price (US$)
MPLAB® IDE Assembler Only Windows Free
MPLAB® C30 Assembler and C-Compiler Windows $895.00 (Free student version1)
Piklab 0.12.0 Assembler and C-Compiler Linux Free2
  1. Full-featured for the first 60 days. After 60 days, some code optimization functions are disabled. The compiler will continue to function after 60 days, but code size may increase.
  2. The current version supports external programmer ICD 2, but not yet tested.

RTSP: COM Port (Bootloader)

  • RTSP works in normal voltage (MCLR# no need to raise to VIHH).
  • No literature has mentioned the incorporation of Programming Executive (PE). Presumably, since Enhanced ICSP needs to set MCLR# to VIHH, RTSP cannot use PE.
  • Refer to bootloader section.


IC Requirements

Table 3.1 IC Requirements
Part No. Description Min Temp Max Temp Min Volt Max Volt Typ Cur Max Cur
dsPIC30F5011-30I/PT uP -40oC 85oC 2.5V [1] 5.5V 145mA 217mA
MAX3232ESE RS232 driver -40oC 85oC 3.0V 5.5V 0.3mA 1.0mA
DS3695N RS485 driver -40oC 85oC 4.75V 5.25V 42mA 60mA
DAC6574IDGS 10-bit Quad-DAC I2C -40oC 105oC 2.7V 5.5V 0.6mA 0.9mA
74HC14D Quad-Schmitt Trigger -40oC 125oC 2.0V 6.0V 0.02mA
Overall -40oC 85oC 4.75V 5.25V <300mA [2]
dsPIC33FJ128GP306-I/PT uP -40oC 85oC 3.0V [1] 3.6V 74mA 250mA
ADM3485EARZ RS485 driver -40oC 85oC 3.0V 3.6V 1.1mA 2.2mA
24LC256-I/SN 256kBits I2C EEPROM -40oC 85oC 2.5V 5.5V 400uA 3mA
LM3940IMP-3.3 5V-3.3V Regulator -40oC 125oC 5.0V 7.5V 10mA 250mA
  1. Minimum voltage measured is 3.3V (with 2 LEDs blinking) running at 30MHz.
  2. Measured current at 5V is 180mA (with 2 LEDs blinking only)


Development Environment

Windows

PIC setup win.JPG

  • C-Compiler, Assembler and Linker are under GNU license.
    • MPLAB C30 C Compiler (*.c -> *.s)
    • MPLAB ASM30 Assembler (*.s -> *.o)
    • MPLAB LINK30 Linker (*.o -> *.bin)
  • PA optimizer, simulator, runtime libraries, header files, include files, and linker scripts are not covered by GNU. Reference is here.
  • Microchip has integrated ASM30, LINK30, assembly header files, linker scripts in MPLAB IDE, which is free for download.
  • MPLAB C30 costs US$895. A 60-day free student version is also available. After 60-days, the optimizer is automatically disabled, while other tools can still function properly. Refer to Table 2.4.


Table 4.1 C Libraries in MPLAB C30
Library Directory
(\\Microchip\MPLAB C30)
Major functions
DSP Library
(e.g. libdsp-coff.a)
\lib
\src\dsp
\support\h
Vector, Matrix, Filter, etc.
16-Bit Peripheral Libraries
(e.g. libp30F5011-coff.a)
\lib
\src\peripheral
\support\h
ADC12, IOPort, UART, I2C, etc.
Standard C Libraries
(e.g. libc-coff.a, libm-coff.a, libpic-coff.a)
\lib
\src\libm
\include
stdio.h, time.h, float.h, math.h,
MPLAB C30 Built-in Functions none _buildin_addab, _buildin_add, _buildinmpy, etc

Linux

PIC setup linux.JPG

  • C Compiler, Assembler and Linker are under GNU license.
    • The code can be downloaded from Microchip at here.
    • Current MPLAB ASM30 Assembler: v2.04
    • Current MPLAB C30 Compiler: v2.04
  • John Steele Scott has made templates that can be readily used by Debian-based systems.
  • For v1.32, the necessary conversion to *.deb has been done already at here.
    • Download pic30-1.32-debian.tar.bz2 for Template v1.32.
    • Download pic30-binutils_1.32-1_i386.deb for the assember.
    • Download pic30-gcc_1.32-1_i386.deb for the compiler.
  • For v2.00
  • For v3.01, convert the Toolchain following instructions at here
    • Pre-install these packages: dpkg-dev, debhelper, bison, flex, sysutils, gcc-3.3, fakeroot
      • cmd: sudo apt-get install dpkg-dev debhelper bison flex sysutils gcc-3.3 fakeroot
    • Download and unzip template: pic30-3.01.tar.bz2
    • Download assembler: mplabalc30v3_01_A.tar.gz. Save under /pic30-3.01/pic30-binutils-3.01/upstream/
    • Download c-compiler: mplabc30v3_01_A.tgz. Save under /pic30-3.01/pic30-gcc-3.01/upstream/
    • Install MPLAB_C30_v3_01-StudentEdition under Windows
    • Copy directories /include, /lib, /support, and /bin/c30_device.info to pic30-3.01/pic30-support-3.01/upstream/
    • Pack pic30-binutils into deb file
      • goto /pic30-3.01/pic30-binutils-3.01/
      • type cmd: dpkg-buildpackage -rfakeroot -b
    • Pack pic30-gcc-3.01 into deb file
      • goto /pic30-3.01/pic30-gcc-3.01/
      • type cmd: dpkg-buildpackage -rfakeroot -b
    • Pack pic30-gcc-3.01 into deb file
      • goto /pic30-3.01/pic30-support-3.01/
      • type cmd: dpkg-buildpackage -rfakeroot -b
    • install pic30-binutils_3.01-1_i386.deb
      • type cmd: sudo dpkg -i pic30-binutils_3.01-1_i386.deb
    • install pic30-gcc_3.01-1_i386.deb
      • type cmd: sudo dpkg -i pic30-gcc_3.01-1_i386.deb
    • install pic30-support_3.01-1_all.deb
      • type cmd: sudo dpkg -i pic30-support_3.01-1_all.deb
  • Important Note: Only the compiler is free. The header files and library are owned by Microchip.
    • Thomas Sailer suggested to download the Student version of C30 compiler and then build the libraries without source code. A package template for Fedora system is available here.
    • Instructions for filling the upstream direction is available here.
    • Alteratively, Stephan Walter has started a project to develop C Runtime Library for dsPIC.
      • Current libraries in version 0.1.1 include: assert.h, cdefs.h, ctype.h, errno.h, inttypes.h, stdint.h, stdio.h, stdlib.h, string.h
  • Burning Program Codes to Target Board
  1. Use 'dspicprg and dspicdmp' utilities developed by Homer Reid to burn hex code (*.hex) to devices. See Reference here. Through serial port only?
  2. Use Piklab IDE. Details on file format not known.
  3. Use MPLAB IDE to burn hex code (*.hex) to devices.

Code Optimization

  • Below is a comparsion between different optimization levels for the project including drivers for 2 projects.
Table 4.2 Comparison between differnt optimization levels
Optimization Description Project 1
Code Size
(byte)
Project 1
Data Usage
(byte)
Project 2
Code Size
(byte)
Project 2
Data Usage
(byte)
O0 No optimization
Fastest Compilation
6222 (9%) 178 (4%) 26,037 (38%) 710 (17%)
O1 Optimize
Tries to reduce code size and execution time.
4473 (6%) 178 (4%) 22,290 (32%) 710 (17%)
O2 Optimize even more
Performs nearly all supported optimizations
that do not involve a space-speed trade-off.
Increases both compilation time and the
performance of the generated code.
4422 (6%) 178 (4%) 21,993 (32%) 710 (17%)
O3 Optimize yet more.
O3 turns on all optimizations specified by O2
and also turns on the inline-functions option.
4485 (6%) 178 (4%) 22,176 (32%) 710 (17%)
Os Optimize for size.
Os enables all O2 optimizations that do not
typically increase code size. It also performs
further optimizations designed to reduce code
size.
4356 (6%) 178 (4%) 21,885 (32%) 710 (17%)


Software Architecture

               +--------+--------+--------+--------+--------+
 Application   | Task 1 | Task 2 | Task 3 | Task 4 | Task 5 |
               +--------+--------+--------+--------+--------+
               |                 POSIX API                  |
               +-------------------+------------------------+
    OS         |    Coroutine      |   FreeRTOS Scheduler   |
               +-------------------+------------------------+
               |                   Drivers                  |
               +------+-----+-----+--------+-------+--------+
  Hardware     | UART | ADC | DAC | EEPROM |  PWM  | TIMERS | 
               +------+-----+-----+--------+-------+--------+
  • Currently, operating system is based on FreeRTOS incorporating coroutine developed by Simon Tatham
  • Software Drivers are to be developed to allow users at Application Level to use the hardware (e.g. ADC, DAC, UART, EEPROM etc) through the OS.
  • The interface between the drivers and the OS is based on POSIX standard (e.g. open(), write(), read(), ioctl(), usleep() etc).
  • The most up-to-date development can be found at repository freertos_posix


Programming Tips

  • Description on developing drivers with POSIX API

Bootloader Development

Concepts

  • Programming with ICSP is useful when the target board is produced in batch. The producer can download a program even when the chip is on the target board.
  • However, ICSP requires an external programmer.
  • To allow the user to change the program after production but without the need of an external programmer, bootloader becomes useful.
  • Bootloader is a small program installed via ICSP. Everytime the device is reset, the bootloader is run first. The bootloader first detects the default serial channel whether the user wishes to download a new program to the device. If so, the bootloader will pause there, and wait for the user to download the hex file from the PC. The hex file is written to the device via RTSP instructions in the bootloader. If a new download is not necessary, the bootloader redirects to the previously installed user's program.
  • The disadvantage of bootloaders is that they consume some of the memory of the device.


Table 7.1 Free bootloaders for dsPIC
Developer Source Platform User Guide Remarks
ingenia Assembly Windows pdf
  • Works for all dsPIC supporting RTSP
  • Auto baudrate detection
  • Use about 1.15% of the flash memory space (0xAFFF-0xAE00)/(0xAFFF-0x0100)
  • Development of Linux platform is underway
  • Modification of code for dsPIC30F5011 is successful
Tiny Assembly Windows Web
  • By default, only supports 601X, 601X, 401X, 2010
  • Smaller code size than ingenia, but not as easy to modify
Elektronika Hex Windows txt
  • Only works for dsPIC30F6014 serial port UART2 at baudrate 57600

dsPicBootloader

  • The bootloader developed by ingenia is open source and it has been modified (see below) to suit our development using dsPic30f5011.
  • The bootloader (hereafter called dsPicBootloader) employs the following settings:
  1. Use U2ART channel
  2. Use FRC, PLL16
  3. For 5011, the bootloader is located between 0x00AE00 to 0x00AFFE (512bytes). Refer to C:\Program Files\Ingenia\ingeniadsPICbootloader\ibl_dspiclist.xml after installing the GUI interface.

1. including p30f5011.gld and p30f5011.inc

       .include "p30f5011.inc"

2. changing the config code of UART #0x8420 -> #0x8020

       ; Uart init
       mov #0x8020, W0           ; W0 = 0x8020 -> 1000 0000 0010 0000b
       mov W0, U2MODE            ; Enable UART, AutoBaud and 8N1
       clr U2STA

3. changing the start address 0xAE00 - 0x0100 = 0AD00

         .equ CRC, W4
         .equ ACK, 0x55
         .equ NACK, 0xFF
         .equ USER_ADDRESS, 0x0100
         .equ START_ADDRESS, 0xAD00                ; Relative to 0x0100

4. using Internal FRC and PLL16

       config __FOSC, CSW_FSCM_OFF & FRC_PLL16 ;Turn off clock switching and
                                          	;fail-safe clock monitoring and
                                           	;use the Internal Clock as the
                                          	;system clock

5. disabling MCLR (optional)

       config __FBORPOR, PBOR_ON & BORV_27 & PWRT_16 & MCLR_DIS
                                           ;Set Brown-out Reset voltage and
                                           ;and set Power-up Timer to 16msecs

6. changing all the related registers of U1ART to U2ART, all U1XXX => U2XXX

       U2MODE, U2STA, U2BRG, U2RXREG, U2TXREG

7. changing all the related registers of IC1 to IC2, all IC1XXX => IC2XXX

       IC2CON, #IC2IF, #IC2IE

dsPicProgrammer (Java-based Multi-Platformed)

  • Ingenia developed a programmer (PC-side) that works only in Windows environment. The project for Linux environment is currently suspended.
  • A simple programmer (hereafter called dsPicProgrammer) written in Java based on the library developed by RXTX has been developed here. The programmer supports both Linux and Windows environments, and may be used as a substitution for the official programmer developed by ingenia.
  • The programmer has the following specification and limitations:
  1. Can be used on both Linux and Windows platforms.
  2. Adjustable baudrate (9600bps to 57600bps[1]).
  3. Support programming of dsPIC30F5011 and dsPIC33FJ128GP306[2] devices (Developers may add your devices).
  4. Protection against overwriting bootloader codes on devices.
  5. Detection if application program does not have its reset() at user's code start address.
  6. Reprogramming can be done without powering down the target board, provided the user's program is compliant to that stated below.
  7. Target board will run the user's program after programming is done.
  8. Can be used with USB-Serial Cables. Below is a list of tested cable:
Prolific PL-2303 USB to Serial Bridge Controller: Driver download
BAFO Technologies BF-810 USB Serial Adapter:Driver download

NOTE:
[1] from version 1.5.2 onwards, the baudrate is increased to 115200bps.
[2] only dsPIC30 devices are compatible with the ingenia's programmer. For dsPIC33 devices, the protocol has been modified due to the increased flash size, and the dsPIC33 bootloader can only work with dsPicProgrammer.

Special Consideration

  • The bootloader assumes that the user program starts at address 0x100. This is usually the case, but there are always exceptions.
  • To ensure that the user program always starts at address 0x100, you can create a customized linker script and customized reset() function as follows:
  • Copy and modify the file named "crt0.s" from the directory "C:\Program Files\Microchip\MPLAB C30\src\pic30" to the project directory and include it.
   .section .reset, code      //previously .section .libc, code 
  • Copy and modify the linkerscript for the device (e.g. p30f5011.gld) to the project directory and include it.
  .text __CODE_BASE :
  {
     *(.reset);              //<-insert this line here
     *(.handle);
     *(.libc) *(.libm) *(.libdsp);  /* keep together in this order */
     *(.lib*);
     *(.text);
  } >program

Communication Protocol

          +-------------------+             +-------------------+----------------+
          |  dsPicProgrammer  |             |  dsPicBootloader  | User's Program |
          +-------------------+             +-------------------+----------------+
          |       PC          |             |             Target dsPic           |
          +-------------------+             +------------------------------------+
          |     COM PORT      |=============|                 UART               |
          +-------------------+             +------------------------------------+
  • Stage 1: User's Configuation
    • Select a COM port channel
    • Select a baudrate
    • Select the user hex file
         java -jar dsPicProgrammer.jar COM1 19200 foo.hex
  • Stage 2: Resetting Target Device
    • dsPicProgrammer sends a Break character (pull UART-TX to low logic, which is normally high).
    • User's program on dsPic detects the break character and reset the chip
NOTE: The user's program is expected to have the following code in order to enable this function. Otherwise, the target board must be restarted manually.
  void _ISR _U2RXInterrupt(void)
  {
      //No Framming error
      if( U2STAbits.FERR == 0)
      {
          //Normal procedure
      }
      //Framming error
      else
      {
          if ( U2STAbits.URXDA ){
              unsigned char data;
              data = (unsigned char) U2RXREG;
              if(data == 0x00){
                  // A break char has been received: 
                  //  U2RX has been pulled to zero for more than 13 bits
                  //  This is used to reboot the pic
                  mdelay(800);    //wait for break character to clear
                  asm("reset");   //software reset
              }
          }        
      }
      _U2RXIF = 0;        //Clear the flag
  }
  • Stage 3: Entering Ingenia's Protocol
    • Transmission is conducted in 8N1, i.e. 8-bit, no parity, 1 stop-bit
    • Communication Protocol is reviewed in ingenia bootloader user's guide section 2.1.3. The following summarises the key steps on the PC side (Refer also to section 2.2.2).
  1. Autobaud rate detection: dsPicProgrammer continuously sends a character "U" [0x55] via COM port and waits for an acknowledgment character "U", [ACK] = [0x55]
  2. Version Control: dsPicProgrammer sends the command character [0x03]. On success, dsPicProgrammer receives 3 characters i) Major Version ii) Minor Version iii) Acknowledgment [0x55]
  3. Device ID Monitoring: dsPicProgrammer sends the read command character [0x01] + 24-bit address [High][Medium][Low] (0xFF0000). Then, it receives 4-byte data [High][Medium][Low][ACK]
  4. Load the user hex file and check integrity
  5. Start Programming: dsPicProgrammer issues the write command character [0x02] + 24-bit address [High][Medium][Low]+ Number of bytes [N] + [data 0] + [data 1] + ... + [data N-1] + [CRC]=(INTEL HEX8 Checksum - Sum modulo 256) and receives [ACK] or [NACK] = [0xFF]
NOTE: Writing is in row mode access (i.e. erase and write a whole row, each row has 32 instructions, or 96 bytes because each instruction has 24 bits)
  • Stage 4: Goto User's Program
    • dsPicProgrammer sends the goto user code command [0x0F]


USB-RS232 Bridge

  • As USB ports are becoming more and more common, COM ports and Parallel ports may be redundant in the next few years. This section explore the possibilities of programming the target board through a USB port.
  • There are two options:
  1. Use an external USB/RS232 adaptor, the driver will emulate a virtual COM port, such as Prolific and FDTI. Ingenia has tested its bootloader with some USB-232 manufacturers (silabs, FTDI, etc..). However, the programming failed with our Prolific adapter. Application program may use JavaComm API (javax.comm) and/or RXTX to drive the COM port.
  2. Modified the bootloader program on PC to support USB communication. e.g. using jUSB and JSR-80 (javax.usb). External circuits such as PIC18F4550 and MAX232 are required.
   |--User's App.--|-------Device Manager------|-------USB-RS232 Interface------|---dsPIC---|
  Option 1:
    +-------------+  +----------+  +----------+  +---+  +------------+  +-----+  +--------+
    | Application |--| JavaComm |--| Virtual  |==|USB|--|    FDTI    |--|RS232|==| Target |
    |   Program   |  |  RXTX    |  | COM Port |  +---+  | Circuitary |  +-----+  | Board  |
    +-------------+  +----------+  +----------+         +------------+           +--------+
  Option 2:
    +-------------+          +--------+          +---+  +------------+  +-----+  +--------+
    | Application |----------| JSR-80 |==========|USB|--| PIC18F4550 |--|RS232|==| Target |
    |   Program   |          |  jUSB  |          +---+  |   MAX232   |  +-----+  | Board  |
    +-------------+          +--------+                 +------------+           +--------+
  • Currently, when RXTX is incorporated with JavaComm API, operating systems supported include Linux, Windows, Mac OS, Solaris and other operating systems. On the other hand, jUSB and JSR-80 only works for linux.

FDTI Chipset

  • FT232RL communicates with PC via USB to provide 1 UART channel.
  • Datasheet can be downloaded here.
    • Refer to Fig. 11 (Page 19) for Bus Powered Configuration.
    • Refer to Fig. 16 (Page 24) for for UART TTL-level Receive [RXD -> 1], Transmit [TXD -> 4], Transmit Enable [CBUS2/TXDEN -> 3]. Omit Receive Enable [CBUS3/PWREN#] and use [CBUS2/TXDEN -> 2]
    • Refer to Fig. 15 (Page 23) for LED Configuration: [CBUS0/TXLED#] and [CBUS1/RXLED#]
  • Virtual COM Port Drivers can be downloaded here.


Programming the Device

Requirements

  • Hardware
  1. PC with COM port (Windows XP Installed for MPLAB)
  2. ICD2 Programmer
  3. Target Board
  4. 5V Power Supply
  • Software
  1. MPLAB IDE v7.50: v7.60 is not compatible with our current ICD2 Programmers. There are consistent "Devices cannot be founded" warnings.
  2. dsPicProgrammer (dsPicProgrammer.jar)
  3. RXTX driver: download and upzip rxtx-2.1-7-bins-r2.zip (Final)
  • Files
  1. dsPicBootloader (bl_5011.hex). Original assembly code by ingenia can be downloaded from here.
  2. Application hex file (e.g. app.hex)

Loading Bootloader (Once only)

Table 9.1 Loading Bootloader
Step Remarks
Install MPLAB IDE
  • Do NOT connect ICD 2 (via USB) to PC
  • Execute MPLAB vX.XX Install.exe
Install USB Driver
  • Follow the instruction in (C:\Program Files\Microchip\MPLAB IDE\ICD2\Drivers\Ddicd2.htm)
Select Target Chip
  • Run MPLAB IDE on PC
  • Select: Configure>Select Devices...
  • Choose dsPIC30F5011
Target <-> ICD 2
  • Use six pin cable. Beware of the pin assignments. Only pin 1 - 5 should be used.
  • Place Jumper on target board (if any). The Jumper connects target Vcc to ICD 2.
  • Do NOT power-up the target.
ICD 2 <-> PC
  • Plug-in ICD 2 to PC via USB cable
  • Power-up the target.
  • Select: Programmer>Select Programmer>MPLAB ICD 2
  • If this is the first time the ICD 2 is connected to PC, MPLAB IDE will automatically download the required OS to ICD 2, wait until it has finished
  • If you have not connected and powered up the target, you might see Warnings on invalid device IDs, and/or running self tests.
  • See results of self test if necessary: Programmer>Settings, Status Tab. Refer to ICD2 User's Guide Chapter 7.
Load Bootloader
  • Select: File>Import...
  • Select bl_5011.hex
Start Programming
  • Select: Programmer>Program
Finishing
  • Power-down the Taget
  • Select: Programmer>Select Programmer>None
  • Unplug USB cable

Loading Firmware

Java Environment Setup

  • Download and install the latest JDK or JRE
  • Download and Extract RXTX Driver
    • Available from RXTX
    • File: rxtx-2.1-7-bins-r2.zip (Final)
    • Extract the files using software such as WinRAR
      1 2 rxtx.JPG
  • Copy RXTXcomm.jar to
    • For Windows users, C:\Program Files\Java\X\lib\ext (under the latest jre, e.g. X = jre1.6.0_03)
    • For Linux users, /usr/lib/jvm/java-version/jre/lib/ext
      1 3 rxtxcomm.JPG
  • Copy rxtxSerial.dll/librxtxSerial.so to
    • For Windows users, C:\Program Files\Java\X\bin (under the lastest jre, e.g. X = jre1.6.0_03)
    • For Linux users, /usr/lib/jvm/java-version/jre/lib/[machine type] (i386 for instance)
      1 4 rxtxserial.JPG
  • Download dsPicProgrammer
    • Available from here
    • Expand the latest tag under dsPicProgrammer
    • File: dsPicProgrammer.jar
      1 5 dspicprogrammer.JPG
    • Save the file (dsPicProgrammer.jar) and your hex file (foo.hex) to your local directory (e.g. C:\dsPicProgrammer\)
      1 6 dspicprogrammer2.JPG
directly download this package

Download Firmware

  • Start a Command Prompt
  • Run dsPicProgrammer
    • Make sure your device is connected through a "Direct RS232 Cable" to PC Serial Port.
    • Change to the directory where dsPicProgrammer.jar is located and run the command:
      • [path/to/java/]java -Djava.library.path=[path/to/rxtxlib] -jar dsPicProgrammer.jar COM1 57600 foo.hex
        • for example, jre\bin\java -Djava.library.path=.\rxtx\bin -jar dspicProgrammer.jar COM1 57600 foo.hex
      • Or directly using dsPicProgrammer.bat COM1 57600 foo.hex(Windows)
      • java -jar dsPicProgrammer.jar /dev/ttyS0 57600 foo.hex (Linux)
where COM1 is your COM Port ID
57600 is communication speed [in bps]
foo.hex is your firmware new file
2 3 command.JPG
    • When the program prompt you for an input, press y:
      File:2 4 prompt.JPG
    • If communication can be established, you should see something like this:
      2 5 progress.JPG
    • Wait until programming is completed.
      2 6 complete.JPG
    • after all completed, power OFF your device, then power ON again, enjoy your new updated.

Troubleshooting

  • Invalid COM Port
    • In case of selecting an invalid COM port, you should see the error message:
      gnu.io.NoSuchPortException: Choosing COM Port Error
      3 1 com err.JPG
  • Missing firmware file
    • In case of selecting a firmware file that does not exist, you should see the error message:
      java.io.FileNotFoundException: foo1.hex <The system cannot find the file specified>
      3 2 file err.JPG
  • Missing RXTX driver
    • In case of missing the RXTX driver, you should see the error message:
      Exception in thread “main” java.lang.NoClassDefFoundError: gnu/io/UnsupportedCommOperationException
      3 3 rxtx err.JPG

Remote Access

  • At the moment, local devices (e.g. EEPROM, ADC, DAC, etc.) can only be accessed locally through POSIX functions such as open(), read(), write(), ioctl().
  • However, a client may need to access these devices on a remote server. This section reviews the background and gives some ideas on its possible implementation.

Requirements

  • A remote file access protocol, to transfer "files" (i.e. device's data) such as:
  1. File Transfer Protocol (FTP): Required files are copied from sever to client for manipulation
  2. Remote Shell (RSH): Required files are copied from sever to client for manipulation
  3. Network File System (NFS): Required files are manipulated on sever
  • An API to access files using a selected protocol, such as:
  1. lam_rfposix: A POSIX-like remote file service for Local Area Multicomputer
  2. API employed by VxWorks: VxWorks is a Unix-like real-time operating system, commonly used for embedded systems.

API Reference for VxWorks


Conversion to dsPIC33F Devices

  • This section discusses the conversion required from dsPIC30F5011 to dsPIC33FJ128GP306.
  • Refer to official document dsPIC30F to dsPIC33F Conversion Guidelines (DS70172A).
  • Note that this section does not mainly intend to introduce the new functionalities of dsPIC33F devices. It only serves the purpose to summarise the major (if not minimum) changes required to port the setup of dsPIC30 to dsPIC33 devices.

Hardware

  • dsPIC33 operates at voltage of 3.3V. A voltage regulator, such as LM3940 can be used to convert 5V supply to 3.3V.
  • A 1uF capacitor has to be placed at pin 56 (previously VSS, now VDDCORE).

Software

Configuration Bits


  • dsPIC33 can operate at 40MIPs at maximum. To configure the device using internal FRC, replace the configuration bits setting as follows:
  _FOSCSEL(FNOSC_FRCPLL);  // FRC Oscillator with PLL
  _FOSC(FCKSM_CSDCMD & OSCIOFNC_ON  & POSCMD_NONE);  
                           // Clock Switching and Fail Safe Clock Monitor is disabled
                           // OSC2 Pin Function: OSC2(RC15) as Digital IO
                           // Primary Oscillator Mode: Disabled
  _FWDT(FWDTEN_OFF);       // Watchdog Timer Enabled/disabled by user software		
  • Configure on-chip PLL at runtime as follows (at start of main function):
  _PLLDIV = 38;            // M=40: PLL Feedback Divisor bits
  CLKDIV = 0;              // N1=2: PLL VCO Output Divider Select bits
                           // N2=2: PLL Phase Detector Input Divider bits
  OSCTUN = 22;              // Tune FRC oscillator, if FRC is used; 
                            // 0: Center frequency (7.37 MHz nominal)
                            // 22: +8.25% (7.98 MHz)
  RCONbits.SWDTEN = 0;      // Disable Watch Dog Timer
  while(OSCCONbits.LOCK != 1);	// Wait for PLL to lock

UART


  • No change is required.

I2C


  • dsPIC33 supports upto 2 I2C devices. As a result, replace all I2C related registers with xxI2Cyy to xxI2C1yy. For examples:
  _SI2C1IF = 0; //Clear Slave interrupt
  _MI2C1IF = 0; //Clear Master interrupt
  _SI2C1IE = 0; //Disable Slave interrupt
  _MI2C1IE = 0; //Disable Master interrupt
  I2C1BRG = I2C_BRG; // Configure Baud rate
  I2C1CONbits.I2CEN = 1;
  ...
  etc.

ADC


  • The ADC in dsPic33 is significantly different from that in dsPic30. Specifically, ADC in dsPic33 uses DMA to buffer the adc data. Replace the open, interrupt routine, add and remove codes as follows:
  unsigned int adc_bufA[ADC_MAX_CH] __attribute__((space(dma),aligned(256)));
  unsigned int adc_bufB[ADC_MAX_CH] __attribute__((space(dma),aligned(256)));
  unsigned int* ADC16Ptr;             //Pointer to ADC register buffer, 
  unsigned char adc_ch_select = 0;    //Pointer to channel to be read from
  unsigned char adc_data_ready = 0;   //Indicate if RAM data is ready for output
  unsigned int which_dma = 0;         //indicate which adc_buf to be used
  void adc_open(void)
  {
      // Configure interrupt
      _AD1IF = 0;              //clear ADC interrupt flag
      _AD1IE = 0;              //disable adc interrupt
      AD1CHSbits.CH0NA = 0;
      // Configure analog i/o  
      _TRISB0 = 1;
      _TRISB1 = 1;    
      AD1PCFG = 0xFFFC;        //Enable AN0 (Vref+) and AN1 (Vref-)
      AD1PCFGH = 0xFFFF;       //AN16-AN31: Disabled
      // Configure scan input channels    
      AD1CSSL = 0x0003;    //0 => Skip, 1 => Scan
      AD1CSSH = 0x0000;    //Skipping AN16-AN31
      // ADCCON4:
      AD1CON4bits.DMABL = 0;    // Each buffer contains 1 word
      // ADCCON3:
      AD1CON3bits.SAMC = 1; //1TAD for sampling time
      AD1CON3bits.ADRC = 0;            //Use system clock
      AD1CON3bits.ADCS = ADC_ADCS;     //each conversion requires 14TAD
      // ADCCON2:
      AD1CON2bits.VCFG = 3;    //External Vref+, Vref-
      AD1CON2bits.CSCNA = 1;   //Scan input
      AD1CON2bits.SMPI = 1;    //2 channels are scanned
      // ADCCON1:
      AD1CON1bits.FORM = 0;        //[0:integer]; [2 fractional]; [3 siged fractional]
      AD1CON1bits.SSRC = 7;        //auto covert, using internal clock source
      AD1CON1bits.ASAM = 1;        //auto setting of SAMP bit
      AD1CON1bits.AD12B = 1;       //12-bit, 1-channel ADC operation
      AD1CON1bits.ADDMABM = 0;     // DMA buffers are built in scatter/gather mode
      AD1CON1bits.ADON = 1;        // Turn on the A/D converter
      // DMA0 Configuration:
      DMA0CONbits.AMODE = 2;      // Configure DMA for Peripheral indirect mode
      DMA0CONbits.MODE  = 2;      // Configure DMA for Continuous Ping-Pong mode
      DMA0PAD=(int)&ADC1BUF0;     
      DMA0CNT = 1;                // generate dma interrupt every 2 samples 
                                  // same as SMPI because only 1 dma buffer per channel         
      DMA0REQ = 13;               // Select ADC1 as DMA Request source
      DMA0STA = __builtin_dmaoffset(adc_bufA);     
      DMA0STB = __builtin_dmaoffset(adc_bufB);
      _DMA0IF = 0;                // Clear the DMA interrupt flag bit
      _DMA0IE = 1;                // Set the DMA interrupt enable bit
      DMA0CONbits.CHEN=1;         // Enable DMA
  }
  void _ISR _DMA0Interrupt(void)
  {
      ADC16Ptr = (which_dma == 0)? adc_bufA : adc_bufB;   //Update pointer
      adc_data_ready = 1;
      which_dma ^= 1;                                     //Next buffer to be used
      _DMA0IF = 0;                                        //Clear the DMA0 Interrupt Flag
  }
  static void adcAdd(unsigned char ch){
      unsigned int mask;
      mask = 0x0001 << ch;
      TRISB = TRISB | mask;
      AD1CSSL = AD1CSSL | mask;      
      AD1PCFG = ~AD1CSSL;
      AD1CON2bits.SMPI++;  //take one more sample per interrupt
      DMA0CNT++; 
  }
  
  static void adcRm(unsigned char ch){
      unsigned int mask;
      mask = 0x0001 << ch;
      AD1PCFG = AD1PCFG | mask;
      AD1CSSL = ~AD1PCFG;
      AD1CON2bits.SMPI--;  //take one less sample per interrupt
     DMA0CNT--; 
  }

EEPROM


  • There is no EEPROM in dsPIC33 devices. Please consider to use an external EEPROM using I2C communication.

Simple PWM


  • No change is required.

Memory Map for dsPIC33FJ128GP306

Table 11.1 Memory Location
Type Start Address End Address Size
Flash 0x000000 0x0157FF 86K[1]
+--Flash: Reset Vector 0x000000 0x000003 4
+--Flash: Interrupt Vector Table 0x000004 0x0000FF 252
+--Flash: Alternate Vector Table 0x000104 0x0001FF 252
+--Flash: User Program 0x000200 0x0157FF 85.5K
Programming Executive 0x800000 0x800FFF 4K[1]
Config Registers 0xF80000 0xF80017 24
Device ID (0xE5) 0xFF0000 0xFF0003 4

[1] Each address is 16-bit wide. Every two addresses correspond to a 24-bit instruction. Each even address contains 2 valid bytes; each odd address contains 1 valid byte plus 1 phathom byte.

Custom Linker Script to Maximize Space for Constant Data

  • Constant data declared using keyword const will be stored in the .const section in the flash memory.
  • Normally, during compilation, the linker will assign these data after the program code (.text section).
  • Since .const is accessed by auto-psv function, to maximize the space for constant data (32kb), the .const section needs to be aligned at 0x80000 boundary.
  • This requires the following change in linker script:
 __CONST_BASE = 0x8000;
 
 .text __CODE_BASE :
 {
 	*(.reset);
       *(.handle);
       *(.libc) *(.libm) *(.libdsp);  /* keep together in this order */
       *(.lib*);
       /* *(.text);		deleted to maximize space for const data */
 } >program
 
 .const __CONST_BASE :
 {
 	*(.const);
 } >program
  • If your program is large, after this change in linker script, function calls may involve large jump in the memory map (>32kB). As a result, you may need to enable the large code and large memory model during compilation. In such case, use the following options in your build path:
   -mlarge-code -mlarge-data
  • Meanwhile, functions that are defined in the standard C libraries, but are replaced with your own implementations (e.g. I/O stubs: open(), read(), write(), lseek(), ioctl() etc.) may have the following linker error:
   /usr/pic30-elf/lib//libc-elf.a(fflush.eo)(.libc+0x3c): In function '.LM11':
   : Link Error: relocation truncated to fit: PC RELATIVE BRANCH _write
   /usr/pic30-elf/lib//libc-elf.a(fclose.eo)(.libc+0x42): In function '.LM18':
   : Link Error: relocation truncated to fit: PC RELATIVE BRANCH _close 
  • To resolve the problem, you need to place the functions in the .libc section rather than in the .text section, like this:
   #define LIBC_CODE_LOC __attribute__ ( (section(".libc")))
   
   int LIBC_CODE_LOC open(const char *pathname, int flags){ ... }
   int LIBC_CODE_LOC close(int fd){ ... }
   int LIBC_CODE_LOC write(int fd, void* buf, int count) { ... }
   int LIBC_CODE_LOC read(int fd, void* buf, int count) { ... }
   int LIBC_CODE_LOC ioctl(int fd, int request, void* argp) { ... }
   int LIBC_CODE_LOC lseek(int fd, int offset, int whence) { ... }

dsPicBootloader and dsPicProgrammer

  • RTSP for dsPIC33F is different from dsPIC30F.
    • Row size changes from 32 instructions (96bytes) to 64 instructions (192 bytes)
    • Erase operation changes from 1 row to 8 rows
    • No EEPROM
  • With regards to the above changes, dsPicBootloader and dsPicProgrammer has been modified. In particular, dsPicProgrammer can be used to program both dsPic30F and dsPic33F devices (Yet, dsPic33F is no longer compatible with Ingenia's programmer). You can easily add your devices to the source code.

Downloads

Table 12.1 Related software download links for dsPicBootloader and dsPicProgrammer
Program Site 1 Site 2 Remarks
JDK website Download latest JDK
RXTX website Download rxtx-2.1-7-bins-r2.zip or later
dsPicBootloader click click (v1.3) Under "dsPicBootloader/", download bl_5011.s or bl_j128gp306.s
dsPicProgrammer click click (v1.3.5) Under "dsPicProgrammer/", dowload dsPicProgrammer.jar

Alternatively, if you want to compile yourself or modify the source code, download
all source files under "dsPicProgrammer/" plus RdFileIntelHex.java under
"IntelHexPaser/tags/0.02.00/".
You should also install RXTX on your local machine as recommended in the readme file.
Ingenia's bootloader website Download original ingenia's bootloader

ToDo

  • dspic gcc compiler for constant problem
  • add chip to stable voltage upon power failure or to detect low voltage, and generate interrupt for dsPic to execute shutdown routine (e.g. save important data to NVM, shutdown ethernet etc.)
  • program the bootloader into flash under linux platform
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox