Monday, September 3, 2018

MPLAB Code Configuration and Microchip Xpress board

As I mentioned on my Blog post about Microchip's Get Launched workshop, after some time without using the latest versions of Microchip development tools, I was playing lately with MPLAB X and MCC, and learned few tricks at the Microchip workshop about how to quickly put together a simple embedded application on MPLAB X IDE and getting it running in a low cost evaluation board.

For this particular project I used one of Microchip Xpress Evaluation Boards and MikroElektronika's 7seg display Click Board (MIKROE-1201)

This is a very simple project to show a two digit decimal counter on the seven segment displays. If you have MPLAB X and the MCC plugin properly installed it will take only few minutes to get it up and running. I will show you step by step how to make it happen.

While the Microchip Xpress Evaluation board has been initially designed to showcase the MPLAB Xpress online IDE, it can also be used with with the standalone MPLAB X on your computer but you will program it in a different way than other development boards, I will show you how.

This particular board has a QFN28 PIC16F18855 8-bit MCU with 8KB FLASH memory and 1KB RAM, and maximum clock speed of 32MHz. The Xpress board does not have an external crystal for the main clock and uses the high-precision internal oscillator. Connected to the PIC there are 5 LEDs, one push-button and a 10K potentiometer and as a bonus a EMC1001 temperature sensor. And obviously also wired to the PIC the pair of female headers for the mikroBUS™ so we can attach the seven segment display or other Click Board™.

Power is sourced from the microUSB connector, USB communication and programming is implemented using a PIC18LF25K50. The board includes a MCP1703 small LDO to step down the USB +5V to +3.3V.

The MikroElektronika 7seg Click Board™ has two standard JS-5213A common cathode single digit seven segment and decimal point displays, wired to a pair of SN74HC595 8-bit shift registers. In this board the displays are not multiplexed and driven in parallel from the MCU. The Click Board™ will take serial data to fill the registers to set the state of each output that drives all the segments and decimal points. The trick here is how to put together the stream of bits to show up the correct numbers on each digit, but don't worry here is where MCC is very helpful, the code is already there!

OK lets start putting the project together, launch MPLAB X IDE. Notice that on the top icon bar you will see MCC in blue, if you have it grayed out you must install the MCC plug-in.

Open a New Project and first select the correct target device, in our case the PIC16F188855.

Now we need to select which tool are we using to program the board. As I mentioned before since this board does not have a debugger we will select "Simulator" on the Hardware Tools tree.

Now we select the compiler. While I've both versions of XC8 I used v1.45 on this project originally before I installed v2.00. Now if you use v.200 be aware that there is a little bug, the workaround for now is to select ANSI C 90 standard.

After we complete setting up all the project properties we saved it.

This setup process creates the entire project structure and sets the correct parameters but take in account that there is not a line of code in the project yet! That's where MCC comes to play, so click on the MCC icon to make the magic happen.

Depending on how fast your PC is, it make take an instant to load the plug-in and additional menus associated with it. You will notice a new tab on the top left window labeled "Resource Management (MCC)" This tab will show various windows on the left, by default on top you will see the Project Resources and below it Device Resources.

On the Device Resources window we'll navigate to one of the last options showing "Mikro-E Clicks" open that menu tree and you will see a sub-tree with all the click boards currently supported by MCC organized by type. Then lets look at Display, and there! the LED7seg is listed as supported, double click on it.

We are not done yet since we need to tell MCC the particular configuration we have for this board and how each of the signals are assigned to the GPIO pins on the MCU.
You will see that on the main top right window now you have all the information and configuration associated with the LED7seg click board, including a picture of it and a direct link to the MikroElektronika product page.

On the bottom Output Window a new tab will appear with the Pin Manager Grid View.

Be aware that the default pin assignment may not be correct for the current Xpress board we are using, so we have some work to do. So take a look at the documentation or silkscreen on the 7seg click board and how it matches the GPIO signals on the Xpress board (RB0, RB1, etc.)

Also in the Project Resources window (top left) look for System->Pin Module and double check that your pin assignments and direction are correct. You will notice that MCC already assigned custom names for each of the signals.

After you are done just click on the Generate tab next to Project Resources.

Now if you go back to the Projects tab and look at the files tree, you will find that MCC populated the tree with all the files needed for the project. Are we there yet? Nope but close, we need to write two lines of code since what we get from MCC is the framework and an example. So look for these two files main.c and LED7Seg_example.h.

The files generated by MCC include all the code needed to drive and communicate with the 7seg click board, and includes an example about how to use this code, but the example is not yet used by our main program.

Then if you open the   file, you will find what is the function we have to call from the main program to execute the example, in this case LED7Seg_example(void) so we'll add the call on the main while loop on main.c (remember to remove the void).

And since we are calling a function that has not been pre-defined we need to include the header file also on main.c.

Save all files, and click now on the Clean and Build Main Project top Icon menu, this will compile and generate the output binary file to be programmed on the Xpress board. As you can see the process on my machine took 8 seconds !!

Now it is time to program the board. If you didn't have it previously connected to your PCB via the microUSB cable, you can do it now. You will notice that the Xpress board will present a remote USB drive on your PC, in my case XPRESS(K:). If you open a file explorer window on it you will find a README.HTM file which is just a redirect to the Xpress board product page on Microchip's website.

Then on a file explorer window find your project files, MPLAB X IDE generates various directories, one of them is dist where you find the default production image. On that directory you will see the .hex file we need, in my case based on the project name I defined is Xpress-7Seg.X.production.hex, just copy and paste that file to the remote USB drive from the Xpress board and you will see that almost instantly the PIC18 programs the PIC16 and starts to run the program. You should see a decimal counter on the LED displays going up in intervals of one second.

That's it !!

While this project is very simple, there are other more complex that can be put together using MCC, but this is a great start to have something easy running quickly so you can learn the ropes. 

I will be posting more projects soon, showing how to use other Microchip Development boards, some I designed and produced myself, and various MikroElektronika Click Boards. 

Happy Hacking


Wednesday, June 20, 2018

PocketBeagle - How to increase root filesystem size

If you are reading this, probably you already created your microSD card for the PocketBeagle which is recommended to be 4GB or bigger. But, regardless of the size of the microSD card you used the system image that got written on it has a fixed size that is most probably less than the total capacity of the card.

In my case, I used a 16GB microSD card, lets see how much is used …

debian@beaglebone:~$ df -v
Filesystem     1K-blocks    Used Available Use% Mounted on
udev              220048       0    220048   0% /dev
tmpfs              49624    5592     44032  12% /run
/dev/mmcblk0p1   3357264 1659656   1507352  53% /
tmpfs             248112       0    248112   0% /dev/shm
tmpfs               5120       0      5120   0% /run/lock
tmpfs             248112       0    248112   0% /sys/fs/cgroup
tmpfs              49620       0     49620   0% /run/user/1000

As you can see the total size of the root file system is about 3.3GB and 53% is already used !

Then, how can you increase the size to use the total available capacity on the microSD card ?

The process is similar on other BeagleBoards and actually on many Linux distributions, on the PocketBeagle is very simple since there is only one “memory block” device /dev/mmcbkl0

debian@beaglebone:~$ ls -l /dev/mmc*
brw-rw---- 1 root disk 179, 0 Mar  5 07:12 /dev/mmcblk0
brw-rw---- 1 root disk 179, 1 Mar  5 07:12 /dev/mmcblk0p1

For the next few operations you will have to become the superuser (root,) instead of having to enter each command preceded by sudo, you can just do sudo -I which will give you the shell in root mode

debian@beaglebone:~$ sudo -i

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for debian:

Notice that after entering the password now the command prompt changed from $ to #, you are now in superuser mode.

To increase the size of the partition and filesystem, first we’ll have to check the partition table and delete the current partition (don’t worry it does not delete the contents) and create a new partition with the new size.

root@beaglebone:~# fdisk /dev/mmcblk0

Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): p
Disk /dev/mmcblk0: 14.9 GiB, 15931539456 bytes, 31116288 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0736b542

Device         Boot Start     End Sectors  Size Id Type
/dev/mmcblk0p1 *     8192 6963199 6955008  3.3G 83 Linux

Command (m for help):

Take note on with sector the partition says it starts, in this case 8192.

Now we will delete the existing partition and create a new one starting at the same sector but we’ll change where it ends, you can choose a smaller number than the total available sectors but I will chose the default which will give me all the available space on the current microSD card

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-31116287, default 2048): 8192
Last sector, +sectors or +size{K,M,G,T,P} (8192-31116287, default 31116287):

Created a new partition 1 of type 'Linux' and of size 14.9 GiB.
Partition #1 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: n

Command (m for help):

Notice that it now says the size of the Linux partition is 14.9 GiB !

No need to delete the signature, but we have not written yet the new partition table on the microSD card, we can double check the new parameters printing the new partition table

Command (m for help): p

Disk /dev/mmcblk0: 14.9 GiB, 15931539456 bytes, 31116288 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0736b542

Device         Boot Start      End  Sectors  Size Id Type
/dev/mmcblk0p1       8192 31116287 31108096 14.9G 83 Linux

What we want, all the available space on the microSD card as the Linux partition, then we are ready to write the table

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy
The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

As soon as we wrote the new partition the Linux kernel detects that it has changed and we’ll have to reboot so it can load the new table

root@beaglebone:~# reboot

You will obviously lose your connection to the board if you are connected via ssh, then wait for the board to reboot then restart the connection, login and become root with sudo -i

login as: debian
debian@'s password:
Linux beaglebone 4.9.82-ti-r102 #1 SMP PREEMPT Thu Feb 22 01:16:12 UTC 2018 armv7l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Mon Mar  5 07:31:39 2018 from
debian@beaglebone:~$ sudo -i
[sudo] password for debian:

Before you do anything else we need to finish the process by extending the size of the filesystem with resize2fs, we’ll use the device that points to the first partition /dev/mmcblk0p1

root@beaglebone:~# resize2fs /dev/mmcblk0p1
resize2fs 1.43.4 (31-Jan-2017)
Filesystem at /dev/mmcblk0p1 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mmcblk0p1 is now 3888512 (4k) blocks long.

Let’s check then how much space we have now

root@beaglebone:~# df -v
Filesystem     1K-blocks    Used Available Use% Mounted on
udev              220048       0    220048   0% /dev
tmpfs              49624    4960     44664  10% /run
/dev/mmcblk0p1  15247576 1662340  12914392  12% /
tmpfs             248112       0    248112   0% /dev/shm
tmpfs               5120       0      5120   0% /run/lock
tmpfs             248112       0    248112   0% /sys/fs/cgroup
tmpfs              49620       0     49620   0% /run/user/1000

There you go, now we have all the available space on the microSD card and we have only 12% used, enough to download some updates/upgrades and packages for development.

Essentially the same process is used to increase the size of any Linux partition but you must be very careful if you have more than one partition on the table since they can’t overlap and by changing the table and resizing the filesystems you may lose the data if not done properly.

So, what’s next ? Connecting to the Internet ? why not ?

How to coming soon !


Wednesday, May 23, 2018

Raspberry Pi - ECN001 Hardware Mod

Update 5/23/18:
I published this article several years ago. No doubt many of the links and references are now outdated. Just keeping it here to preserve the history of some blog entries. 

Sunday, May 20, 2018

Update in progress ....

Many years ago I started using these Blogs to record and to share what I learned from my R&D work in technology particularly Digital Electronics or Embedded Design.

Also one of my hobbies has been what may be called "Retrocomputing" sort of having a little fun with old computers, some electronics tools and gadgets.

Due to time constraint I sort of abandoned the practice of writing new articles and updating these Blogs. Well I decided that it is time to get back and refresh this corner on the websphere reorganizing these pages and getting back into the habit to write new articles and keep this updated.

To have the content somehow organized I will keep three associated but specifically focused Blogs.

Some of these pages had some previous articles, some of them I put them in draft mode so I can check and update their content and republish.

I continue to generate a daily digital News Paper called Short Circuit that is generated automatically from various feeds I provide. I will also be updating those sources for more rich contact.

To those that have been writing me asking about old projects and when I'd be back... well this is the answer :-)

Hope you enjoy the content and feel free to provide your comments and contact me