Programming on a Chipkit Max32, a little review

ware (based on java) are not all functioning out-of-the-box or need administrator rights. ... and equally the communication with the prototyping platform. To fix this ...
568KB taille 30 téléchargements 224 vues
Programming on a Chipkit Max32, a little review Maxime Vinzio November 2014

Contents 1 About the platform

2

2 Core Functions 2 .1 Basic includes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 .2 A task manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3 3 3

3 Sainsmart Screen and Shield 3 .1 LCD and Touchscreen . . . 3 .2 SD Slot . . . . . . . . . . . 3 .3 Shield . . . . . . . . . . . . 3 .4 Network Connection . . . .

. . . .

4 4 5 6 6

. . . .

6 7 7 7 7

5 Hardware 5 .1 Analog Write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 .2 External Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 .3 Internal Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

8 8 10 11

6 Code exportation

14

7 Conclusion

14

. . . .

4 Modification of UTFT Library 4 .0.1 Pinout . . . . . . . . . 4 .0.2 Memory usage . . . . . 4 .0.3 Compatibility . . . . . 4 .1 Utouch Calibration . . . . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

1

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

Discovering the Chipkit Max 32

Section 2

This document is a feedback of the use of the ChipKit Max32 prototyping board used with a saintsmart screen. It presents the platform principles and modifications done in order to help anyone willing to develop with it. The lack of documentation asks to be a bit sneaky and study the code adapted from the Arduino community. Special ChipKit functionalities are well documented, and in case of doubt the PIC32 documentations of Microchip seem complete and are useful1 . Please see the condensed presentation of the Chipkit MAX32 board[2].

1

About the platform

The software has been developed with an external editor from MPIDE. MPIDE is the modified Arduino IDE by Digilent for the ChipKit products. The interface and functionalities are basics, compilation macros are hidden and unmodifiable. The version I used to was mpide-0023-linux64-20140316. It was sometimes a bit buggy, but usually an unexplained bug can be solved by unplugging the ChipKit board and relaunching the software. A smarter choice would be to use MPLABX, developed for the PIC32 micro-controllers types or create a makefile for the project. Nevertheless, MPIDE is a turnkey software, for other solutions some manipulations have to be done to use the compiler and the exportation of the project to another platform risk to be more difficult. Arduino prototyping systems "like" use the combination of C and C++. C++ is chosen as programming language, object oriented languages allowing an easy interfacing for "object" control. Arduino functions are present in the basic functions. ChipKit also respects the design of Arduino’s systems and allows to use Arduino’s shields. Nevertheless, the operating voltage being different (3.3 V for ChipKit instead of 5 V for Arduino), in most of cases Arduino’s shields have to be modified. Here the ChipKit MAX32 –having a "similar" pinout– borrow in some configuration files the name of the Arduino Mega board. Finally, lot’s of code have been adapted from the Arduino project. But it is really sad to see most of basic functions provided as soft solutions instead of using the hard-designed functionalities. For example the PWM signal demonstration is done by software. Beside the instructive function, this kind of design is resource’s consuming, can not be implemented if other operations must be operated and is always frustrating. Nevertheless, the PIC32 documentation allows to write low-level functions that will be presented here (find the reference manuals of the pic32 family with the keyword "pic32" [url]). The MPIDE software can be downloaded here2 . On Linux it is easily installable by extracting it in the /aux/ directory. Nevertheless some of the libraries used by this software (based on java) are not all functioning out-of-the-box or need administrator rights. Especially for the serial management library. It allows the transfer of the compiled binary and equally the communication with the prototyping platform. To fix this a manipulation is to create a symbolic link from the system serial library (/usr/lib/librxtxSerial.so) to the software lib directory. 1 2

http://www.microchip.com/TechDoc.aspx?type=ReferenceManuals http://chipkit.net/started/install-chipkit-software/install-mpide-linux/

Page 2 of 15

Discovering the Chipkit Max 32

2

Section 2

Core Functions

A reference of basic functions can be found on Arduino’s website3 . For example, it is not recommended to have a recursive use of the min/max functions, indeed having a look in the wiring.h definition file, we can see that theses functions are defined by macros.

2 .1

Basic includes

In every program are automatically included these standard libraries: #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e

< s t d l i b . h> " w i r i n g . h" "WProgram . h"

#i f d e f __cplusplus #i n c l u d e " H a r d w a r e S e r i a l . h" #e n d i f

"Wiring.h" contains mathematical constants, main.cpp and Arduino functions prototype and macros as min(), max(). . . "WProgram.h" contains registers pointers e.g. T2CONSET : configuration register for the TIMER2. To facilitate the definitions, or use pic32 specific functions (for example, UARTConfigure(uartX, UART_ENABLE_PINS_TX_RX_ONLY);) the library is proposed. This library is mentioned here because it should be avoid, for strange reasons ChipKit redefined the type word. This can conduct to overwriting definitions and failures4 . Some of the definitions findable in this library have been written in the p32_defs.h for classic hardware use (e.g. for the output compare) (see /pathtompide/hardware/pic32/cores/pic32/p32_d

2 .2

A task manager

The task manager allows the creation and operation of background tasks. A task is a user defined function that is executed automatically at specified intervals or at a specified time. Using tasks can simplify the logic of programs that need to perform periodic operations. The scheduling times for task functions is based on the system millisecond tick counter (the core timer). This is the value returned by the millis() function. This tick counter runs continuously and gives the number of milliseconds elapsed since the system started running. Scheduling of tasks for execution is done prior to each execution of the user function loop(). In order to task scheduling works reliably, the time to execute the user loop() function should be short; preferably less than a millisecond, but no more than a few milliseconds if the timing accuracy of task scheduling needs to be precise. Example of functions : createTask () destroyTask ( ) 3 4

http://arduino.cc/en/Reference/HomePage http://ChipKit.net/forum/viewtopic.php?f=7&t=2790&sid=05b48c8be96c6af12711a1c7bf650702&start=10

Page 3 of 15

Discovering the Chipkit Max 32

Section 3

i n t main ( v o i d ) { init () ; setup () ; while (1) { _scheduleTask ( ) ; loop () ; } return 0; }

Figure 1: main.cpp getTaskId ( ) startTaskAt ( ) getTaskNextExec ( ) setTaskState () setTaskPeriod ()

If a task is too long, the task management may be broken, this can be avoided by disabling every task at the start of one and re-enabling them afterwards. For example, the UTFT button library (presented further) has a multi-thread incompatible design, if a button stays pressed, the whole system stays in a reading loop and waits for the user to release the touch-screen, the functioning of this library has to be modified to fit to a periodic operating (a periodic interrupt function based on a timer can do it). This system should not be used for a precise period sampling, we will prefer timer interrupts control for data sensoring.

3

Sainsmart Screen and Shield

Since the MCU is working in 3.3 V, the designed shield for the 5 V Arduino Mega has to be modified.

3 .1

LCD and Touchscreen

The screen is a 3.2 inches TFT LCD SainSmart 320x240 RGB based on the SSD1289, Integrated Power device, 262k color, Gate and Source Driver With RAM. The touch-screen is a resistive technology and a controller convert the analog signal (ADS7843). Standing alone they work in 3.3 V. The pinout is shown on figure 2. The screen is controllable with the pins:

Page 4 of 15

Discovering the Chipkit Max 32 E CS RS

WR RST DB0-15

Section 3

An Enable pin that enables writing to the registers, here always at high level, directly wired on the shield. The ChipSelect Pin A register select pin that controls where in the LCD’s memory you’re writing data to. You can select either the data register, which holds what goes on the screen, or an instruction register, which is where the LCD’s controller looks for instructions on what to do next. that selects reading mode or writing mode Reset Pin. 16 bits data bus, bidirectional to read or to write.

The touch-screen is controllable with the pins: DOUT BUSY DIN CS DCLK

Serial Data Output. Data is shifted on the falling edge of DCLK. This output is high impedance when CS is HIGH. Busy Output. This output is high impedance when CS is HIGH. Not compulsory. Serial Data Input. If CS is LOW, data is latched on rising edge of DCLK. Chip Select Input. Controls conversion timing and enables the serial input/output register. External Clock Input. This clock runs the SAR conversion process and synchronizes serial data I/O.

Figure 2: Sainsmart TFT LCD Pin Out This device is working with the UTFT Library (see section 4 ) and should work as well with the library TFT from Chipkit (it uses directly the PMP functionality but the writing of a touchscreen driver may be necessary)5 .

3 .2

SD Slot

The Sainsmart screen comes with a SD slot usable as extended memory pluggable to the 5 V Arduino. The shield is known as having an incorrect routing for the SD card, after verification it’s not the case. A SD card can be directly connected to the 3.3 V of the system. 5

hrefhttps://github.com/majenkotech/TFThttps://github.com/majenkotech/TFT

Page 5 of 15

Discovering the Chipkit Max 32

Section 4

Figure 3: SD connection to Arduino The resistances on the Sainsmart device have to be removed (3). Note that no voltage divider is installed on the MISO signal wire, simply being already a 3.3 V signal recognizable by the usual 5 V Arduino threshold. The SD card works fine with the SD library. The SD library being a wrapper of the SDfat, it creates a class variable SD that we have to use over the "hidden" SDfat parameters. It allows to open only one file at the time.

3 .3

Shield

The shield has been first modified by removing the voltage dividers. Furthermore the routing has to be changed, indeed some ports configurable as output can not work as it. The USBon-the-go functions used pins that can only be used as digital inputs when USB is not enabled even if the writing register of these ports exist. See page 174 in the summarised datasheet[6]. Furthermore some other hardware functionalities as the Output Compare (see further) or external interrupts can be reserved for other operations, and the pins usable are limited. The variable resistor on the shield seems to engender problems (current too high ?) if badly positioned. Try to set it if the screen does not work.

3 .4

Network Connection

Not implemented but pins reserved for a further development.

4

Modification of UTFT Library

The Universal TFT library is designed for Arduino and chipKit. It aggregates the common drivers for Arduino’s like devices. Nevertheless, it is gluttonous in memory. Thus the code has been adapted and optimized. This free software is downloadable here 6 . 6

http://www.henningkarlsen.com/electronics/library.php?id=51

Page 6 of 15

Discovering the Chipkit Max 32

4 .0.1

Section 5

Pinout

Only the RS, CS, W R and RES (and SER for serials screens) are dynamically configurable. The UTFT library defines its pinout depending on the board used for example for the Chipkit Max32 the definition files is in hardware/pic32/HW_PIC32MX795F512L.h. Indeed to get a faster usage of the inputs/outputs the set/reset registers are used hence a as fast as possible operation of the LCD module. See the code 4 showing the principle of masking. It would have been possible to do it dynamically using the Parallel Master Port hardware functionality of the Chipkit. It is a parallel 8-bit/16-bit input/output module specifically designed to communicate with a wide variety of parallel devices, such as communications peripherals, LCDs, external memory devices and microcontrollers. //PORT B c o n f i g u r a t i o n a s output TRISB&=~0x0700 ; // C l e a r and w r i t i n g LATBCLR = 0 x0700 ; LATBSET = (BYTE