|
|
If the test [LED Blink](LED-Blink) worked, everything is set up correctly. This next tutorial will teach you how to write a simple driver for the led.
|
|
|
|
|
|
## Header file
|
|
|
|
|
|
To get started, create a folder called `led` in `src/shared/drivers`.
|
|
|
|
|
|
In it, create a file called `SimpleLed.h` and write:
|
|
|
|
|
|
```cpp
|
|
|
/* Copyright (c) 2017 Skyward Experimental Rocketry
|
|
|
* Authors: YOUR_NAME_HERE
|
|
|
*
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
*
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
* all copies or substantial portions of the Software.
|
|
|
*
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
* THE SOFTWARE.
|
|
|
*/
|
|
|
|
|
|
#ifndef SIMPLE_LED_H
|
|
|
#define SIMPLE_LED_H
|
|
|
|
|
|
/* Includes basic useful stuff, e.g. the miosix kernel*/
|
|
|
#include <Common.h>
|
|
|
|
|
|
/* Define the PD14 pin as ledPin */
|
|
|
typedef miosix::Gpio<GPIOD_BASE, 14> ledPin;
|
|
|
|
|
|
/**
|
|
|
* Describe the class here.
|
|
|
*/
|
|
|
class SimpleLed
|
|
|
{
|
|
|
public:
|
|
|
/**
|
|
|
* Class constructor: opens the led pin in input mode and sets it to OFF.
|
|
|
*/
|
|
|
SimpleLed();
|
|
|
|
|
|
/**
|
|
|
* Class destructor.
|
|
|
*/
|
|
|
~SimpleLed() {}
|
|
|
|
|
|
/**
|
|
|
* Get the state of the led.
|
|
|
*\return TRUE = ON, FALSE = OFF
|
|
|
*/
|
|
|
bool getState();
|
|
|
|
|
|
/**
|
|
|
* Switch the led to the given state
|
|
|
* \param newState the state to be reached (TRUE = ON, FALSE = OFF)
|
|
|
*/
|
|
|
void switchToState(bool newState);
|
|
|
|
|
|
protected:
|
|
|
// Nothing here
|
|
|
private:
|
|
|
bool state;
|
|
|
|
|
|
};
|
|
|
|
|
|
#endif /* SIMPLE_LED_H */
|
|
|
```
|
|
|
|
|
|
Please note a few things that are part of our [coding standards](Coding-Guidelines):
|
|
|
- Copyright at the start (write your name in the authors and the right date)
|
|
|
- `#ifndef` instruction, to prevent multiple re-inclusions
|
|
|
- `#include <Common.h>`, a bunch of useful includes :)
|
|
|
- `typedef miosix::Gpio<XXX, YYY>` is how GPIO pins are declared in Miosix: `<GPIOD_BASE, 14>` means pin `PD14` of the discovery board
|
|
|
- Comments on top of the class and before each function, with a description of the argument and/or the return value
|
|
|
|
|
|
## Cpp file
|
|
|
|
|
|
Now, create another file called `SimpleLed.cpp` in the same folder and add:
|
|
|
|
|
|
```cpp
|
|
|
/* Copyright (c) <YEAR_HERE> Skyward Experimental Rocketry
|
|
|
* Authors: <YOUR_NAME_HERE>
|
|
|
*
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
*
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
* all copies or substantial portions of the Software.
|
|
|
*
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
* THE SOFTWARE.
|
|
|
*/
|
|
|
|
|
|
#include <drivers/led/SimpleLed.h>
|
|
|
|
|
|
using namespace miosix;
|
|
|
|
|
|
SimpleLed::SimpleLed() : state(false)
|
|
|
{
|
|
|
ledPin::mode(Mode::OUTPUT);
|
|
|
}
|
|
|
|
|
|
bool SimpleLed::getState() {
|
|
|
return state;
|
|
|
}
|
|
|
|
|
|
void SimpleLed::switchToState(bool newState)
|
|
|
{
|
|
|
if (newState)
|
|
|
{
|
|
|
ledPin::high();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
ledPin::low();
|
|
|
}
|
|
|
state = newState;
|
|
|
}
|
|
|
|
|
|
```
|
|
|
As you can see, `PD14` is set as an `output` pin in the constructor and its digital value can be set to `high` or `low`.
|
|
|
You can find more information about how to use the GPIO and what pins you can use, in the [GPIO Usage](GPIO-Usage-&-Used-Pins) page.
|
|
|
|
|
|
## Write an entrypoint
|
|
|
|
|
|
To write a program that uses this driver, an entrypoint is needed as before: in `src/entrypoints` add a file called `test-led-driver.cpp` and write:
|
|
|
|
|
|
```cpp
|
|
|
#include <Common.h>
|
|
|
#include <drivers/led/SimpleLed.h>
|
|
|
|
|
|
using namespace miosix;
|
|
|
|
|
|
int main()
|
|
|
{
|
|
|
SimpleLed led;
|
|
|
|
|
|
while(1)
|
|
|
{
|
|
|
led.switchToState( !(led.getState()) );
|
|
|
miosix::Thread::sleep(200);
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
## Add everything to SBS
|
|
|
|
|
|
Just as before we have to add what we wrote to `sbs.conf`. This time though
|
|
|
we will also have to compile `SimpleLed.cpp` with our entrypoint. The way to do this is:
|
|
|
|
|
|
Add a new `srcfiles` group in `sbs.conf`, after `#srcfiles`:
|
|
|
|
|
|
```
|
|
|
[simple_led_include]
|
|
|
Type: srcfiles
|
|
|
Files: src/shared/drivers/led/SimpleLed.cpp
|
|
|
```
|
|
|
|
|
|
Now you can add the entrypoint (a.k.a *board*, because it's a software that will
|
|
|
run on a board).
|
|
|
|
|
|
```
|
|
|
[test-led-driver]
|
|
|
Type: board
|
|
|
BoardId: stm32f407vg_stm32f4discovery
|
|
|
BinName: test-led-driver
|
|
|
Include: %simple_led_include
|
|
|
Defines:
|
|
|
Main: test-led-driver
|
|
|
```
|
|
|
|
|
|
#### Note
|
|
|
As before, please note that you have to choose the correct board in order for your
|
|
|
code to run on your specific target board, if it's not a STM32F407VG Discovery.
|
|
|
Also consider that the available LEDs and the pins to which they are connected can change according to the board you use. You should check the board's datasheet or its user manual.
|
|
|
Otherwise you can use any of the available pins and connect an external LED.
|
|
|
|
|
|
## Run it!
|
|
|
Same as before: build the entrypoint with `python sbs -b test-led-driver` and [flash the binary on the board](Flashing-on-a-Target-Board). |
|
|
\ No newline at end of file |