How-to on subset of C++ for AVR and other micros

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It may be commonplace now to use a subset of C++ for small RAM microprocessors, this subset being something like object methods and properties but not the big libraries and unbridled use of create/destroy/polymorphism.

If someone has some small-RAM (say, 4-16KB) code examples, it would be appreciated

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Here's some code I started on for the Xmega. It's incomplete and not fully tested, but I think it should work as a starting point for an example.

main.cpp

#include 
#include 
#include "Port.h"

int main (void)   
{
  static uint8_t val;

  Port foo(PORTF);
  
  foo.set_port(0xAA);
  val = foo.get_port();
  foo.setup_pin_cfg(1,PORT_OPC_PULLUP_gc,PORT_ISC_INPUT_DISABLE_gc);

  while (1);
}

Port.h

#ifndef XMEGA_PORT_CLASS_H
#define XMEGA_PORT_CLASS_H

#include 
#include 

class Port
{
  public:
    Port(PORT_t& p);
    uint8_t get_port(){return port.IN;} 
    void set_port(uint8_t p){port.OUT = p;}             
    void clear_port(volatile uint8_t& p){port.OUT = p;}
    void set_port_dir(uint8_t dir){port.DIR = dir;}    
    void set_pin_out(uint8_t pins){port.DIRSET = pins;}
    void set_pin_in(uint8_t pins){port.DIRCLR = pins;}
    void set_pin(uint8_t pins){port.OUTSET = pins;}
    void clear_pin(uint8_t pins){port.OUTCLR = pins;}
    void toggle_pin(uint8_t pins){port.OUTTGL = pins;}   
    void setup_ext_int0(const PORT_INT0LVL_t lvl, const uint8_t pin);
    void setup_ext_int1(const PORT_INT0LVL_t lvl, const uint8_t pin);
    void setup_pin_cfg(const uint8_t pin, PORT_OPC_t opc, PORT_ISC_t isc);

  private:
    PORTCFG_t& cfg;
    PORT_t& port;

};

#endif /*------------------- XMEGA_PORT_CLASS_H ---------------------*/

Port.cpp

#include "Port.h"

Port::Port(PORT_t& p) : cfg (PORTCFG), port(p)
{
  
}

void Port::setup_ext_int0(const PORT_INT0LVL_t lvl, const uint8_t pin)
{
  port.INTCTRL = lvl & 0x03;
  port.INT0MASK = pin;
}

void Port::setup_ext_int1(const PORT_INT0LVL_t lvl, const uint8_t pin)
{
  port.INTCTRL = lvl & 0x0C;
  port.INT1MASK = pin;
}

void Port::setup_pin_cfg(const uint8_t pin, PORT_OPC_t opc, PORT_ISC_t isc)
{
  switch (pin)
  {
    case 0:
      port.PIN0CTRL = (opc|isc);
      break;
    case 1:
      port.PIN1CTRL = (opc|isc);
      break;
    case 2:
      port.PIN2CTRL = (opc|isc);
      break;
    case 3:
      port.PIN3CTRL = (opc|isc);
      break;
    case 4:
      port.PIN4CTRL = (opc|isc);
      break;
    case 5:
      port.PIN5CTRL = (opc|isc);
      break;
    case 6:
      port.PIN6CTRL = (opc|isc);
      break;
    case 7:
      port.PIN7CTRL = (opc|isc);
      break;
    default:return;
  }
}