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
Embedded and Microcontrollers
  • Technologies
  • More
Embedded and Microcontrollers
Blog Renesas RX: C++ tryout with GCC
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Embedded and Microcontrollers to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 20 Jul 2023 10:07 PM Date Created
  • Views 913 views
  • Likes 7 likes
  • Comments 5 comments
  • RoadTest
  • RX23e-a
  • gcc
  • renesas
  • e2studio
  • d2a28d6e-11d8-11ee-be56-0242ac120002
Related
Recommended

Renesas RX: C++ tryout with GCC

Jan Cumps
Jan Cumps
20 Jul 2023

When you create a new Renesas project for an RX device, you can choose C++. Let's try it out with an object oriented blinky.

For  simplicity, I'm creating one class: an output pin class called io::O. It can drive a pin high and low. You assign a pin at creation time. You can drive it by assigning true or false to it:

#include "O.h"

int main(void) {
	bool state = true;
	io::O led(GPIO_PORT_H_PIN_2);  // initialise the output pin for the LED.

    while(1) {
    	led = !led;

    	// silly wait
    	for (int i = 0; i < 10000000; i++) {};

    	led != led;

    	// silly wait
    	for (int i = 0; i < 10000000; i++) {};
    }
return 0;
}

Class O

image

header: O.h

#ifndef O_H_
#define O_H_

extern "C" {
#include "r_gpio_rx_if.h"
}

namespace io {

typedef gpio_port_pin_t pin_t;

class O {
public:
	O(pin_t pin);
	virtual ~O();
	O &operator= (O &rhs);
    O &operator!= (O &rhs);
	O &operator= (bool high);
	operator bool() const;
protected:
  pin_t pin;
};

} // namespace io

#endif /* O_H_ */

implementation: O.cpp

#include <O.h>

using namespace io;

O::O(pin_t pin): pin(pin) {
}

O::~O() {
}

O &O::operator= (bool high) {
	R_GPIO_PinWrite(this->pin, (gpio_level_t)high);
	return *this;
}

O &O::operator!= (O &rhs) {
	R_GPIO_PinWrite(this->pin, (gpio_level_t)!((bool)rhs));
	return *this;
}

O::operator bool() const {
	gpio_level_t l = R_GPIO_PinRead(this->pin);
	return (bool) l;
}

When you run this, you have your usual blinky. I tried to make the code very simple, with just a little bit of OO encapsulation. A proof that it can be done.
mbed fans may see similarities to the DigitalXXX classes. It was definitely an inspiration, although I didn't port their code. (I've done that in the past for another controller family).

The attached e² studio project is precompiled, using GCC and the rx23e-a starter kit board. It can be used with CC-RX and any RX controller.
FIT objects used: r_bsp (board startup), Config_PORT (pin setup) and r_gpio_rx (gpio API).
rx23ea_thermocouple_cpp_gcc_20230721.zip

link to all posts

  • Sign in to reply
  • DAB
    DAB over 1 year ago

    Nice post Jan.

    It is a good example of why you need to know what your compiler is doing to you, not for you.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 1 year ago in reply to Jan Cumps

    By moving the class declarfations from main to global,, the memory use redused "a lot"

    image

    I don't know why yet ...

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 1 year ago in reply to Jan Cumps

    2 lines of code that say output (line 23 and 24) should be output2. Can't edit:

    led = !output2;
    led != output2;

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 1 year ago

    For the fans, memory usage

    CPP:
    image

    C:
    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 1 year ago

    I've designed this class so that it (kind of) acts as a boolean.

    // ...
    
    io::O led(GPIO_PORT_H_PIN_2);
    
    // all of these are valid:
    
    // set led pin high or low
    led = true;
    led = false;
    
    // is the led pin high?
    bool state = led;
    
    // inverse the led pin
    led = !led;
    
    // inverse the led pin
    led != led;
    
    io::O output2(GPIO_PORT_1_PIN_1);
    
    // both set the state of led to the inverse state of output2:
    led = !output;
    led != output;
    
    // controversial? led now is the output2 pin, pin assignment changes and state taken over (my choice)
    // alternative, I can implement it so that the led takes over only the state from output2, instead of becoming output2
    led = output2;
    

    • Cancel
    • Vote Up 0 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