The port of the MBED DigitalOut is complete.
In this blog you can see the internals of this class and how to create a blinky.
Blinky
#include "includes.h" // this is the include that replaces MBED.h for ported applications. // start Hercules HAL includes #include "HL_gio.h" // specific for Hercules. It's not part of the MBED port. // end Hercules HAL Includes DigitalOut led( LED1 ); // initialise the output pin for the LED. This is an MBED ported class. int main(/* your main loop of your blinky program */) { gioInit(); // initialise the device. This example is for Hercules. It's not part of the MBED port. while(1) { led = !led; // blink/toggle wait_ms(500); // a function I wrote that loops the number of miliseconds given as parameter. } }
The core of this example is:
DigitalOut led( LED1 ); while(1) { led = !led; // blink/toggle wait_ms(500); // }
DigitalOut Header
/* * DigatalOut.h * * Created on: 11 nov. 2019 * Author: jancu */ /* mbed Microcontroller Library * Copyright (c) 2006-2019 ARM Limited * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef CPP_IMPLEMENTATION_DEPENDENT_CLIENT_PLATFORM_DIGITALOUT_H_ #define CPP_IMPLEMENTATION_DEPENDENT_CLIENT_PLATFORM_DIGITALOUT_H_ #include "hard_includes.h" class DigitalOut { public: DigitalOut(PinName pin); virtual ~DigitalOut(); DigitalOut &operator= (int value); DigitalOut &operator= (DigitalOut &rhs); operator int(); protected: PinDefinition pinDef; }; #endif /* CPP_IMPLEMENTATION_DEPENDENT_CLIENT_PLATFORM_DIGITALOUT_H_ */
DigitalOut Source
/* mbed Microcontroller Library * Copyright (c) 2006-2019 ARM Limited * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include <DigitalOut.h> #include "HL_gio.h" DigitalOut::DigitalOut(PinName pin) { // hercules specific= The first 32 bits are the port address, lower 32 bits the pin this->pinDef.pin = pin & 0xffffffff; // the lower 32 bits are the pinnummer this->pinDef.port = ((pin & 0Xffffffff00000000) >> 32); // if your controller uses APIs to set the pin direction, you'd do that here. // I'm using the Hercules HALCoGen utility to set pin directions, so they are in the right state // when the pin will be used first. } DigitalOut::~DigitalOut() { } DigitalOut &DigitalOut::operator= (int value) { gioSetBit((gioPORT_t *)(this->pinDef.port), this->pinDef.pin, value); return *this; } DigitalOut::operator int() { return gioGetBit((gioPORT_t *)(this->pinDef.port), this->pinDef.pin); }
Device Specific Declarations Header
This file does not contain logic for your program, but definitions for the DigitalOut (and other) classes that are specific for your board.
You see the declarations of the pins (PinName), the values for pin high and low (PinDirection) and a device specific structure PinDefinition that's used in several classes to contain the effective pin info.
I called the file client_board.h. In my final library this file is automatically included.
/* * client_board.h * * Created on: 11 nov. 2019 * Author: jancu */ /* mbed Microcontroller Library * Copyright (c) 2006-2019 ARM Limited * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef CPP_IMPLEMENTATION_DEPENDENT_CLIENT_PLATFORM_CLIENT_BOARD_H_ #define CPP_IMPLEMENTATION_DEPENDENT_CLIENT_PLATFORM_CLIENT_BOARD_H_ // hercules specific: A GPIO pin has a port address 32 bit and a pin number 32 bit. // To stay compatible with the MBED interface, I kept the pin as single 64 bit number // with the port in the MSB and pin in the LSB // this is not efficient for a 32 bit controller. Feel free to modify typedef uint64_t PinName; const PinName LED1 = 0xFFF7BC5400000006; // gioPORTB 6 -- LED2 on launchpad const PinName LED2 = 0xFFF7BC5400000007; // gioPORTB 7 -- LED3 on launchpad // todo: other pins const PinName NC = 0xffffffffffffffff; typedef enum { PIN_INPUT, PIN_OUTPUT } PinDirection; typedef struct { uint32_t port; uint32_t pin; }PinDefinition; #endif /* CPP_IMPLEMENTATION_DEPENDENT_CLIENT_PLATFORM_CLIENT_BOARD_H_ */
Highlights
For the firmware developer, using the classes is easy. Some people like these abstractions, other don't.
Simple code to declare a pin, and to change it:
- Declare a gpio output pin:
DigitalOut led( LED1 );
Hercules C equivalent:
// not needed
- Set status:
led = 1; led = 0;
Hercules C equivalent:
gioSetBit(gioPortA, 6, 1); gioSetBit(gioPortA, 6, 0);
- Toggle status:
led = !led;
Hercules C equivalent:
gioToggleBit(gioPortA, 6);
- Get status:
int status = led;
Hercules C equivalent:
int status = gioGetBit(gioPortA, 6);
Comments and critique on the port are welcome below.
Top Comments