element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Legacy Personal Blogs Clean Up Part 1: Basic Ground Rules
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: jack.chaney56
  • Date Created: 8 Mar 2016 2:47 PM Date Created
  • Views 695 views
  • Likes 5 likes
  • Comments 4 comments
Related
Recommended

Clean Up Part 1: Basic Ground Rules

jack.chaney56
jack.chaney56
8 Mar 2016

Part of the clean up process takes the whole thing back to the beginning, assuming nothing, and only using baby steps.

 

Making a platform.

Nothing ever gets built without the tools. Embedded applications are the same, but the tools are not hammers and screw drivers.  The tools of embedded programming are functions that have had the bugs worked out of them. That perform particular tasks in an efficient and highly predictable manner.  These tools are able to be ported from project to project with minimal modification and only a superficial cleaning to fit the specific use.  Because embedded applications are a little bit software and a little bit hardware, often a generic platform is also carried from application to application because the tools, often, are optimized for the platform. I have worked with many platforms, and many software development environments, from assembly language on an early PC (the old 8088 with two 5.25” floppy drives), to quad core ARM systems with LAN using high level languages like C, Java, and Python. All along the way I have developed tools to perform tasks, and periodically, I have taken the tools out of the box and refit them to the new environment.

 

Hammers:

So what kinds of tools are in my tool box? Serial communication! Top priority number one most used functionality. Ported to so many machines and so many languages, I have lost count. Luckily, since back in the early days of 6800, 6502, Z80, and 8080, the communication module has remained mostly unchanged. The only change has only been what the registers were named, the function has always been the same.  This means the code has not had to change significantly, other than register naming and how interrupts are managed.  Also, writing the code in C has given me a highly portable and super clean operator for managing serial communications.

 

Next on the list is timers. A real time system needs to have a real time base. It is never known when a program is running (short of counting cycles), how much time has elapsed in a program from one function to another.  Especially if there are lots of interrupt routines, and the functions are under the control of another team of programmers. The way to manage time is to use a constant time base, either with an interrupt based counter, or with some form of real time clock hardware.  The second is usually battery backed to provide a real clock function, with date information as well.  Interrupt based counters are most often using some form of capture/compare timer functionality, with a periodic interrupt and a counter to give the number of tics and converting to hours, minutes, and seconds (and possibly day, month, year).

 

The next group of tools is mostly dependent on what is installed in the platform, but fall into categories; data collection, which can be A/D measurement or switch state, graphics, things like plot, draw, filled shapes, and axis rotation, and math functions for creation of sin, cos, log, or root values.  I also have some specialized tools for the work I generally do, like; table interpolation, and a general data stream parser.

 

When you have a handful of established tools, it is easier to look at a problem and determine which tools will be used in the solution.  With this in mind, it is a good rule that more tools is always better, because, “if the only tool you have is a hammer, all your problems look like nails” becomes the box you put yourself into.

 

Wood:

There are a number of processor chips on the market, and depending on what is needed, points the direction for which to select. Embedded programming has an established bundle of guidelines when it comes to selecting processors. Tool sets meaning; compiler, Integrated Development Environment (IDE), In Circuit Emulator (ICE), programmer (for loading code), and the most important, specification sheets. The other selection criteria is a clear migration path, so the chip manufacturer won’t stop production just before you are ready to start yours, and in case you outgrow the current chip, or the speed just isn’t there, there is a better version that will run the current code with only slight modification.

Manufacturers have development platforms which are outstanding for early code development, but often paint you in a corner for the hardware design.  Occasionally this is good, but often the function pins you had wanted to use for a specific purpose is unavailable because a platform function has forced it a certain way.

 

I am currently a fan of the Atmel line of processors, because they provide a good migration path for going larger, they are a well-established manufacturer, so they are not likely to go away in the near future (before I can retire). They have 8, 16, and 32 bit Micro Processor Units (MPUs), and have multiple core processor types.  They also have a compiler and IDE that are available from the website for free download, a large number of development platforms, and ICE capability using Joint Test Action Group (JTAG) ICE devices. The platforms are also available as education based test units from Arduino (open source hardware), that provide good basic platform with very little extra baggage, meaning most of the pins are available directly via headers.

 

Project Start:

The development platform selected is an Arduino Nano, using an in circuit AVR programmer. The Atmel 328 is a small easily managed processor with a good array of features, well suited for a learning platform. The IDE is AVR Studio from the Atmel site (only requires you to enter some information so they can send you product updates via email), not exactly “free” like beer. The next piece of ground work is programming style. Coding style is like a fingerprint. Each person has their own style and technique, usually based on years of practice. I have a particular style and because I am the writer, I get to use my style. The reader might have a different style and since if you get 20 programmers in a room you will see at least 21 programming styles, I am not going to try and change.  There will be a significant amount of source provided, particularly in the tools section.  It is probably best now to establish some programming “rules” (really more of a guideline):

 

  1. Floating point is for people, not computers, and should be avoided in real time systems wherever possible.
  2. Self documenting code is a nice theory, so comment as much as possible. And no, you won’t remember later.
  3. Real time and control systems are event driven so interrupts rule, and mainline is small.
  4. Watch for patterns, a generic function beats out cut and paste, always!
  5. Time is finite, you can’t make more of it, even if it would be handy.

 

More rules will pop up as the discussion progresses. Returning to style, here is a style point that will probably rub many of the Electrical Engineers out there the wrong way (hammer/nail issues).  I try not to use state machines for my programming. I do know what they are, and how they work and how handy they can be for real time design. But, I have seen them misused often and badly enough times, they have become a method to avoid instead of embrace.  With that in mind, here is a very effective means for making a state machine that encourages proper methodology instead of abuse.  Newer C allows use of function pointers, so this is available in C as well as C++.  Code begins here… I will explain style elements in just a bit.

 

typedef SWord (*func)(void);    // declare a general function pointer

func stateMachine[] = {         // make a list of functions one for each state
    init,                       // state 0 is exit, also initialize (return is the next state)
    state1,                     // each state operation returns a state number of the next state to run
    state2,                     // states should be enumerated so naming can be used
        …
    stateN                      // N states in the diagram
};

int main() {
    SWord state = 0;            // initialize first
                                // each state operation returns the next state
    while ((state = stateMachine[state]()) != 0);
    return state;               // should always be zero, right?
}

 

Style stuff…

SWord is used instead of int beacause, based on the system and compiler, an int can be whatever the word size for the processor happens to be. I developed a “types.h” include file with naming that always means the same thing, and make small adjustments based on the particular processor / compiler pairing.  Simply described, UWord and SWord are for 16 bit numbers, unsigned and signed versions. UByte and SBtye are 8 bit, and ULong and SLong, are 32 bits. I also keep the Holy Grail for embedded programming in my types.h file.  It is the method for accessing hardware addresses from high level C code.

 

#define REG_W(a) *((volatile UWord* const)(a))
#define ARR_W(a) ((UWord* const)(a))

 

These are the 16 bit versions of the mechanism; the first is for a register or a physical location in memory referenced by “a”, the second is for addressing an array of 16 bit locations in memory at address “a”.  By defining a structure, and using the same method, it is possible to declare a memory mapped device in the same way.  Enough for now, here is my basic types.h that will be used for the examples.

 

Please note: the lack of comments. Because this file is updated for each compiler change, it is always touched.  If my memory gets that bad, I will have to leave the business.

 

/************************************************************************************************
types.h

History: [JAC] Original Creation when the earth was cooling

Copyright(c) 2016 Chaney Firmware
************************************************************************************************/
#pragma once
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdbool.h>
#include <stdio.h>

#define VER_MAJ             0
#define VER_MIN             0
#define VER_REV             0
/************************************************************************************************
 Version number is maintained as a single 16 bit number of bit fields
 bits           Field
 12:15          MAJOR               (number between 0 and 15)
 5:11           MINOR               (number between 0 and 99)
 0:4            REVISION            (number between 0 and 31)
************************************************************************************************/
#define OFS_MAJ            12
#define OFS_MIN             5
#define OFS_REV             0

#define VERS_NO            ((VER_MAJ<<OFS_MAJ)|(VER_MIN<<OFS_MIN)|(VER_REV<<OFS_REV))

typedef unsigned char       UByte;
typedef char                SByte;
typedef unsigned short      UWord;
typedef short               SWord;
typedef unsigned long       ULong;
typedef long                SLong;

#define REG_B(a)            *((volatile UByte* const)(a))
#define REG_W(a)            *((volatile UWord* const)(a))
#define REG_L(a)            *((volatile ULong* const)(a))
#define REG_Bs(a)           *((volatile SByte* const)(a))
#define REG_Ws(a)           *((volatile SWord* const)(a))
#define REG_Ls(a)           *((volatile SLong* const)(a))

#define ARR_B(a)            ((UByte* const)(a))
#define ARR_W(a)            ((UWord* const)(a))
#define ARR_L(a)            ((ULong* const)(a))
#define ARR_Bs(a)           ((SByte* const)(a))
#define ARR_Ws(a)           ((SWord* const)(a))
#define ARR_Ls(a)           ((SLong* const)(a))

#ifdef forever
#undef forever
#endif
#define forever             for(;;)

#define bit0                (1<<0)
#define bit1                (1<<1)
#define bit2                (1<<2)
#define bit3                (1<<3)
#define bit4                (1<<4)
#define bit5                (1<<5)
#define bit6                (1<<6)
#define bit7                (1<<7)
#define bit8                (1<<8)
#define bit9                (1<<9)
#define bit10              (1<<10)
#define bit11              (1<<11)
#define bit12              (1<<12)
#define bit13              (1<<13)
#define bit14              (1<<14)
#define bit15              (1<<15)

/* end of file */

 

Installments are going to have to be divided up by function making a whole bunch of chapters for just getting the platform working. A/D conversion, UART, timers, and EEPROM management are the next sections.

  • Sign in to reply

Top Comments

  • Jan Cumps
    Jan Cumps over 9 years ago +1
    Some build tools, when integrated with version control (I've implemented it in Ant with CVS for development at work) can generate the version data for you at each build, so you don't have to do that by…
  • jack.chaney56
    jack.chaney56 over 9 years ago in reply to jw0752 +1
    Thank you John. Still noticing gaps for areas to which I would like to return. I did not do a background baseline for programming ability, I use shortcuts quite a bit that should be explained, and I should…
  • jack.chaney56
    jack.chaney56 over 9 years ago in reply to jw0752

    Thank you John.  Still noticing gaps for areas to which I would like to return.  I did not do a background baseline for programming ability, I use shortcuts quite a bit that should be explained, and I should explain why I use things like for(;;) instead of while(1). I will be presenting a few more tools before jumping into engine management. What is interesting, when I had been working on this for a while, is noticing that my engine control code was looking quite a bit like an operating system.  I say if you only have a hammer, all your problems look like nails.  The insight is, looking back on my pile of source code, my "hammer" is operating systems.  I taught the class at a junior college for several years, and became very comfortable with messaging, scheduling, task control, and device drivers.

     

    Please be one of my herders while I write, to make sure my discussions are not too obscure, or seem to present a method without proper explanation.

     

    Thank you,

    Jack

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • jw0752
    jw0752 over 9 years ago

    Hi Jack,

    I am going to enjoy reading this series.

    John

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • jack.chaney56
    jack.chaney56 over 9 years ago in reply to Jan Cumps

    Thank you so much for responding. I am hoping the stages get both looked at, and commented.  I have used many different types of version management systems (currently tortoise), and understand the check-in, check-out process on many have either an @ or % or &> type of mechanism that puts the version into the code for you.  But, I am going for something quite generic here, and also wanted to add versioning, because versioning is important. Also note the version number works out to be a 16 bit value.

     

    Jack

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 9 years ago

    Some build tools, when integrated with version control (I've implemented it in Ant with CVS for development at work)  can generate the version data for you at each build, so you don't have to do that by hand.

    You can also tag your sources with that build number, so that it's exactly recreatable later.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube