Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
Changed GPIO pin numbering from physical to BCM
Browse files Browse the repository at this point in the history
This should ease the future replacement of the wiringPi library.
  • Loading branch information
Eleonore Mizo committed Apr 27, 2023
1 parent d3cf4e8 commit 14eef04
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ int WINAPI WinMain (::HINSTANCE instance, ::HINSTANCE prev_instance, ::LPSTR cmd
#endif

#if fstb_SYS == fstb_SYS_LINUX && ! defined (MAIN_USE_VOID)
::wiringPiSetupPhys ();
::wiringPiSetupGpio ();

::pinMode (mfx::hw::GpioPin::_nav_cancel, INPUT);
if (::digitalRead (mfx::hw::GpioPin::_nav_cancel) == LOW)
Expand Down
2 changes: 1 addition & 1 deletion src/mfx/hw/DisplayPi3Pcd8544.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace hw


// Before calling:
// ::wiringPiSetupPhys ()
// ::wiringPiSetup* ()
// ::pinMode (_pin_rst, OUTPUT);
// ::digitalWrite (_pin_rst, LOW); ::delay (100);
// ::digitalWrite (_pin_rst, HIGH); ::delay (1);
Expand Down
2 changes: 1 addition & 1 deletion src/mfx/hw/DisplayPi3St7920.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace hw


// Before calling:
// ::wiringPiSetupPhys ()
// ::wiringPiSetup* ()
// ::pinMode (_pin_rst, OUTPUT);
// ::digitalWrite (_pin_rst, LOW); ::delay (100);
// ::digitalWrite (_pin_rst, HIGH); ::delay (1);
Expand Down
23 changes: 10 additions & 13 deletions src/mfx/hw/GpioPin.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,26 @@ class GpioPin
public:

// - - - - - - - - - - - - - - - - - - - - - - - -
// Physical numbering
// BCM numbering (GPIO)

// Shared
static constexpr int _reset = 18; // GPIO 24 / phys 18
static constexpr int _reset = 24; // GPIO 24 / phys 18

// Switches
static constexpr int _nav_ok = 7; // GPIO 4 / phys 7
static constexpr int _nav_cancel = 22; // GPIO 25 / phys 22
static constexpr int _nav_ok = 4; // GPIO 4 / phys 7
static constexpr int _nav_cancel = 25; // GPIO 25 / phys 22

// LEDs
static constexpr int _led_0 = 11; // GPIO 17 / phys 11
static constexpr int _led_1 = 13; // GPIO 27 / phys 13
static constexpr int _led_2 = 15; // GPIO 22 / phys 15
static constexpr int _led_0 = 17; // GPIO 17 / phys 11
static constexpr int _led_1 = 27; // GPIO 27 / phys 13
static constexpr int _led_2 = 22; // GPIO 22 / phys 15

// Small Nokia display (never acually used)
static constexpr int _pcd8544_dc = 12; // GPIO 18 / phys 12
static constexpr int _pcd8544_cs = 16; // GPIO 23 / phys 16
static constexpr int _pcd8544_dc = 18; // GPIO 18 / phys 12
static constexpr int _pcd8544_cs = 23; // GPIO 23 / phys 16

// 128x64 display (SPI)
static constexpr int _st7920_cs = 16; // GPIO 23 / phys 16

// - - - - - - - - - - - - - - - - - - - - - - - -
// BCM numbering (GPIO)
static constexpr int _st7920_cs = 23; // GPIO 23 / phys 16

// Audio interface (I2S)
static constexpr int _snd_reset = 5; // GPIO 5 / phys 29
Expand Down
51 changes: 26 additions & 25 deletions src/mfx/hw/GpioPwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,19 @@ void GpioPwm::clear (int chn)



void GpioPwm::clear (int chn, int pin)
void GpioPwm::clear (int chn, int gpio)
{
assert (chn >= 0);
assert (chn < _nbr_dma_chn);
assert (_chn_arr [chn].get () != nullptr);

Channel & channel = *(_chn_arr [chn]);
channel.clear (pin);
channel.clear (gpio);
}



void GpioPwm::add_pulse (int chn, int pin, int start, int width)
void GpioPwm::add_pulse (int chn, int gpio, int start, int width)
{
assert (chn >= 0);
assert (chn < _nbr_dma_chn);
Expand All @@ -182,12 +182,12 @@ void GpioPwm::add_pulse (int chn, int pin, int start, int width)
const int width_spl = (width + (_granularity >> 1)) / _granularity;

Channel & channel = *(_chn_arr [chn]);
channel.add_pulse (pin, start_spl, width_spl);
channel.add_pulse (gpio, start_spl, width_spl);
}



void GpioPwm::set_pulse (int chn, int pin, int start, int width)
void GpioPwm::set_pulse (int chn, int gpio, int start, int width)
{
assert (chn >= 0);
assert (chn < _nbr_dma_chn);
Expand All @@ -197,7 +197,7 @@ void GpioPwm::set_pulse (int chn, int pin, int start, int width)
const int width_spl = (width + (_granularity >> 1)) / _granularity;

Channel & channel = *(_chn_arr [chn]);
channel.set_pulse (pin, start_spl, width_spl);
channel.set_pulse (gpio, start_spl, width_spl);
}


Expand Down Expand Up @@ -320,9 +320,10 @@ void GpioPwm::Channel::clear ()



void GpioPwm::Channel::clear (int pin)
void GpioPwm::Channel::clear (int gpio)
{
const int gpio = ::physPinToGpio (pin);
assert (gpio >= 0);

uint32_t * d_ptr = _dma.use_buf <uint32_t> ();

// Remove this gpio from all samples
Expand All @@ -331,7 +332,7 @@ void GpioPwm::Channel::clear (int pin)
d_ptr [i] &= ~(1 << gpio);
}

::digitalWrite (pin, 0);
::digitalWrite (gpio, 0);
}


Expand All @@ -344,16 +345,16 @@ void GpioPwm::Channel::clear (int pin)
// point in time, only the last added action (eg. set-to-low) will be executed on all pins.
// To create these kinds of inverted signals on two GPIOs, either offset them by 1 step, or
// use multiple DMA channels.
void GpioPwm::Channel::add_pulse (int pin, int start, int width)
void GpioPwm::Channel::add_pulse (int gpio, int start, int width)
{
assert (gpio >= 0);
assert (width <= int (_nbr_samples));
assert (start >= 0);
assert (start < int (_nbr_samples));

const int gpio = ::physPinToGpio (pin);
if (! is_gpio_ready (gpio))
{
init_gpio (pin, gpio);
init_gpio (gpio);
}

int pos = start;
Expand Down Expand Up @@ -395,16 +396,16 @@ void GpioPwm::Channel::add_pulse (int pin, int start, int width)



void GpioPwm::Channel::set_pulse (int pin, int start, int width)
void GpioPwm::Channel::set_pulse (int gpio, int start, int width)
{
assert (gpio >= 0);
assert (width <= int (_nbr_samples));
assert (start >= 0);
assert (start < int (_nbr_samples));

const int gpio = ::physPinToGpio (pin);
if (! is_gpio_ready (gpio))
{
init_gpio (pin, gpio);
init_gpio (gpio);
}

int pos = start;
Expand Down Expand Up @@ -446,7 +447,7 @@ void GpioPwm::Channel::set_pulse (int pin, int start, int width)

if (width == 0)
{
::digitalWrite (pin, 0);
::digitalWrite (gpio, 0);
}
}

Expand All @@ -467,8 +468,9 @@ void GpioPwm::Channel::set_pulse (int pin, int start, int width)
// For a given DMA channel, this function must be called with the
// same "physical" parameters (nbr_cycles, nbr_phases).
// Returns the level error.
float GpioPwm::Channel::set_multilevel (int pin, int nbr_cycles, int nbr_phases, int phase, float level)
float GpioPwm::Channel::set_multilevel (int gpio, int nbr_cycles, int nbr_phases, int phase, float level)
{
assert (gpio >= 0);
assert (nbr_cycles > 0);
assert (nbr_cycles * nbr_phases * 2 <= int (_nbr_samples));
assert (nbr_phases > 0);
Expand All @@ -477,10 +479,9 @@ float GpioPwm::Channel::set_multilevel (int pin, int nbr_cycles, int nbr_phases,
assert (level >= 0);
assert (level <= 1);

const int gpio = ::physPinToGpio (pin);
if (! is_gpio_ready (gpio))
{
init_gpio (pin, gpio);
init_gpio (gpio);
}

DmaCtrlBlock * cb_ptr = &_dma.use_cb (0);
Expand Down Expand Up @@ -577,17 +578,17 @@ float GpioPwm::Channel::set_multilevel (int pin, int nbr_cycles, int nbr_phases,

if (level == 0)
{
::digitalWrite (pin, 0);
::digitalWrite (gpio, 0);
}

return duty / float (nbr_cycles);
}



int GpioPwm::Channel::find_free_front_pos (int pin, int pos, bool up_flag, bool fwd_flag)
int GpioPwm::Channel::find_free_front_pos (int gpio, int pos, bool up_flag, bool fwd_flag)
{
const int gpio = ::physPinToGpio (pin);
assert (gpio >= 0);
const DmaCtrlBlock * cb_ptr = &_dma.use_cb (0);
const uint32_t * d_ptr = _dma.use_buf <const uint32_t> ();

Expand Down Expand Up @@ -618,10 +619,10 @@ bool GpioPwm::Channel::is_gpio_ready (int gpio)
}


void GpioPwm::Channel::init_gpio (int pin, int gpio)
void GpioPwm::Channel::init_gpio (int gpio)
{
::pinMode (pin, OUTPUT);
::digitalWrite (pin, 0);
::pinMode (gpio, OUTPUT);
::digitalWrite (gpio, 0);
_gpio_init |= 1 << gpio;
}

Expand Down
20 changes: 10 additions & 10 deletions src/mfx/hw/GpioPwm.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ class GpioPwm
virtual ~GpioPwm () = default;
int init_chn (int chn, int subcycle_time);
void clear (int chn);
void clear (int chn, int pin);
void set_pulse (int chn, int pin, int start, int width);
void add_pulse (int chn, int pin, int start, int width);
float set_multilevel (int chn, int pin, int nbr_cycles, int nbr_phases, int phase, float level);
void clear (int chn, int gpio);
void set_pulse (int chn, int gpio, int start, int width);
void add_pulse (int chn, int gpio, int start, int width);
float set_multilevel (int chn, int gpio, int nbr_cycles, int nbr_phases, int phase, float level);



Expand All @@ -127,14 +127,14 @@ class GpioPwm
virtual ~Channel ();

void clear ();
void clear (int pin);
void add_pulse (int pin, int start, int width);
void set_pulse (int pin, int start, int width);
float set_multilevel (int pin, int nbr_cycles, int nbr_phases, int phase, float level);
int find_free_front_pos (int pin, int pos, bool up_flag, bool fwd_flag);
void clear (int gpio);
void add_pulse (int gpio, int start, int width);
void set_pulse (int gpio, int start, int width);
float set_multilevel (int gpio, int nbr_cycles, int nbr_phases, int phase, float level);
int find_free_front_pos (int gpio, int pos, bool up_flag, bool fwd_flag);

static bool is_gpio_ready (int gpio);
static void init_gpio (int pin, int gpio);
static void init_gpio (int gpio);

int _index;
MmapPtr _dma_reg;
Expand Down
2 changes: 1 addition & 1 deletion src/mfx/hw/LedPi3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const int LedPi3::_gpio_pin_arr [_nbr_led] =


// Before calling:
// ::wiringPiSetupPhys ()
// ::wiringPiSetup* ()
LedPi3::LedPi3 ()
: _gpio_pwm (_pwm_resol)
, _state_arr ()
Expand Down
2 changes: 1 addition & 1 deletion src/mfx/hw/UserInputPi3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const int UserInputPi3::_pot_arr [Cst::_nbr_pot] =


// Before calling:
// ::wiringPiSetupPhys ()
// ::wiringPiSetup* ()
// ::pinMode (_pin_rst, OUTPUT);
// ::digitalWrite (_pin_rst, LOW); ::delay (100);
// ::digitalWrite (_pin_rst, HIGH); ::delay (1);
Expand Down
2 changes: 1 addition & 1 deletion src/test/TestLedSimple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int TestLedSimple::perform_test ()
mfx::hw::GpioPin::_led_2
};

::wiringPiSetupPhys ();
::wiringPiSetupGpio ();

for (int i = 0; i < nbr_leds; ++i)
{
Expand Down
2 changes: 1 addition & 1 deletion src/test/TestUserInputPi3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int TestUserInputPi3::perform_test ()

const int pin_rst = mfx::hw::GpioPin::_reset;

::wiringPiSetupPhys ();
::wiringPiSetupGpio ();
::pinMode (pin_rst, OUTPUT);
::digitalWrite (pin_rst, LOW);
::delay (100);
Expand Down

0 comments on commit 14eef04

Please sign in to comment.