Skip to content

Commit 4d571fb

Browse files
committed
Created PWM object linking pin name, channel and function (alt0, alt1, ...)
1 parent 51b77da commit 4d571fb

File tree

2 files changed

+95
-55
lines changed

2 files changed

+95
-55
lines changed

Gpio.cpp

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void Gpio::setup_pin(GpioPinIO pin) {
6161
std::uint8_t fun_static = static_cast<std::uint8_t>(pin.function);
6262

6363
bcm2835_gpio_fsel(pin_static, fun_static);
64-
LOG("setup pin:%d function:%d", pin_static, fun_static);
64+
LOG("setup io pin:%d function:%d", pin_static, fun_static);
6565

6666
// if specified set PullUp resistor
6767
if (pin.pullUp != GpioPullUp::none) {
@@ -72,38 +72,24 @@ void Gpio::setup_pin(GpioPinIO pin) {
7272
}
7373
}
7474

75-
void Gpio::setup_pin(GpioPwm pin) {
76-
std::uint8_t channel = get_pwm_channel(pin);
75+
void Gpio::setup_pin(GpioPwm pwm) {
76+
GpioPwmItem *item = pwm.item;
77+
if (item != NULL) {
78+
std::uint8_t pin = item->name;
79+
std::uint8_t channel = item->channel;
80+
std::uint8_t function = item->function;
7781

78-
if (channel >= 0 && pin.mode != GpioPwmMode::unknown) {
79-
bcm2835_pwm_set_mode(channel, static_cast<std::uint8_t>(pin.mode), 1 /* 1=enable */);
80-
}
81-
82-
if (channel >= 0 && pin.range > 0) {
83-
bcm2835_pwm_set_range(channel, pin.range);
84-
}
85-
}
82+
bcm2835_gpio_fsel(pin, function);
83+
LOG("setup pwm pin:%d function:%d", pin, function);
8684

87-
std::uint8_t Gpio::get_pwm_channel(GpioPwm pin) {
88-
std::uint8_t channel = -1;
89-
90-
switch(pin.name) {
91-
case GpioPwmName::pwm1_gpio19:
92-
case GpioPwmName::pwm1_gpio13:
93-
channel = 1;
94-
break;
95-
96-
case GpioPwmName::pwm0_gpio18:
97-
case GpioPwmName::pwm0_gpio12:
98-
channel = 0;
99-
break;
85+
if (channel >= 0 && pwm.mode != GpioPwmMode::unknown) {
86+
bcm2835_pwm_set_mode(channel, static_cast<std::uint8_t>(pwm.mode), 1 /* 1=enable */);
87+
}
10088

101-
default:
102-
channel = -1;
103-
break;
89+
if (channel >= 0 && pwm.range > 0) {
90+
bcm2835_pwm_set_range(channel, pwm.range);
91+
}
10492
}
105-
106-
return channel;
10793
}
10894

10995
void Gpio::read(std::list<GpioPinIO*> pin_list) {
@@ -117,10 +103,13 @@ void Gpio::read(std::list<GpioPinIO*> pin_list) {
117103

118104
void Gpio::read(std::list<GpioPwm*> pin_list) {
119105
for (GpioPwm *pin : pin_list) {
120-
std::uint8_t pin_static = static_cast<std::uint8_t>(pin->name);
121-
122-
pin->value = bcm2835_gpio_lev(pin_static);
123-
LOG("read PWM pin:%d value:%d", pin_static, pin->value);
106+
GpioPwmItem *item = pin->item;
107+
if (item != NULL) {
108+
pin->value = bcm2835_gpio_lev(item->name);
109+
LOG("read PWM pin:%d value:%d", item->name, pin->value);
110+
} else {
111+
LOG("read PWM error: pin not initialized");
112+
}
124113
}
125114
}
126115

@@ -151,11 +140,10 @@ void Gpio::write(GpioPinIO* pin, bool val) {
151140
}
152141

153142
void Gpio::write(GpioPwm* pin, std::uint32_t val) {
154-
std::uint8_t channel = get_pwm_channel(*pin);
155-
156-
if (channel >= 0) {
157-
bcm2835_pwm_set_data(channel, val);
158-
LOG("write PWM channel:%d value:%d", channel, val);
143+
GpioPwmItem *item = pin->item;
144+
if (item != NULL) {
145+
bcm2835_pwm_set_data(item->channel, val);
146+
LOG("write PWM channel:%d value:%d", item->channel, val);
159147
} else {
160148
std::cerr << "write PWM channel not found" << std::endl;
161149
}

Gpio.h

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ using namespace std;
3232
#define BCM2835_INIT_SPI_ERROR -2
3333
#define BCM2835_CLOSE_ERROR -3
3434

35-
// Pin names
35+
// GPIO pin names
3636
enum class GpioPinName: std::uint8_t {
3737
unknown = 0xff,
3838
gpio27 = RPI_V2_GPIO_P1_13,
@@ -45,26 +45,59 @@ enum class GpioPinName: std::uint8_t {
4545
gpio4 = RPI_V2_GPIO_P1_07
4646
};
4747

48-
// Pin names
49-
enum class GpioPwmName: std::uint8_t {
50-
unknown = 0xff,
51-
pwm1_gpio19 = RPI_V2_GPIO_P1_35, /*!< Can be PWM channel 1 in ALT FUN 5 */
52-
pwm0_gpio18 = RPI_V2_GPIO_P1_12, /*!< Can be PWM channel 0 in ALT FUN 5 */
53-
pwm1_gpio13 = RPI_V2_GPIO_P1_33, /*!< Can be PWM channel 1 in ALT FUN 0 */
54-
pwm0_gpio12 = RPI_V2_GPIO_P1_32, /*!< Can be PWM channel 0 in ALT FUN 0 */
48+
// PWM pin names
49+
enum class GpioPwmName {
50+
pwm1_gpio19,
51+
pwm0_gpio18,
52+
pwm1_gpio13,
53+
pwm0_gpio12
54+
};
55+
56+
class GpioPwmItem {
57+
private:
58+
GpioPwmItem (
59+
std::uint8_t name,
60+
std::uint8_t function,
61+
std::uint8_t channel
62+
) {
63+
this->name = name;
64+
this->function = function;
65+
this->channel = channel;
66+
}
67+
68+
public:
69+
std::uint8_t name;
70+
std::uint8_t function;
71+
std::uint8_t channel;
72+
73+
static GpioPwmItem* create(GpioPwmName pwm_name) {
74+
GpioPwmItem *item = NULL;
75+
switch(pwm_name) {
76+
case GpioPwmName::pwm1_gpio19:
77+
item = new GpioPwmItem(RPI_V2_GPIO_P1_35, BCM2835_GPIO_FSEL_ALT5, 1);
78+
break;
79+
80+
case GpioPwmName::pwm0_gpio18:
81+
item = new GpioPwmItem(RPI_V2_GPIO_P1_12, BCM2835_GPIO_FSEL_ALT5, 0);
82+
break;
83+
84+
case GpioPwmName::pwm1_gpio13:
85+
item = new GpioPwmItem(RPI_V2_GPIO_P1_33, BCM2835_GPIO_FSEL_ALT0, 1);
86+
break;
87+
88+
case GpioPwmName::pwm0_gpio12:
89+
item = new GpioPwmItem(RPI_V2_GPIO_P1_32, BCM2835_GPIO_FSEL_ALT0, 0);
90+
break;
91+
}
92+
return item;
93+
}
5594
};
5695

5796
// Functions
5897
enum class GpioFunction: std::uint8_t {
5998
unknown = 0xff,
6099
input = BCM2835_GPIO_FSEL_INPT,
61-
output = BCM2835_GPIO_FSEL_OUTP,
62-
alt0 = BCM2835_GPIO_FSEL_ALT0,
63-
alt1 = BCM2835_GPIO_FSEL_ALT1,
64-
alt2 = BCM2835_GPIO_FSEL_ALT2,
65-
alt3 = BCM2835_GPIO_FSEL_ALT3,
66-
alt4 = BCM2835_GPIO_FSEL_ALT4,
67-
alt5 = BCM2835_GPIO_FSEL_ALT5
100+
output = BCM2835_GPIO_FSEL_OUTP
68101
};
69102

70103
// Pull up resistor
@@ -112,16 +145,36 @@ class GpioPinIO : GpioPinConfigCommon
112145
bool value = false;
113146
GpioFunction function = GpioFunction::unknown;
114147
GpioPullUp pullUp = GpioPullUp::none;
148+
149+
GpioPinIO (
150+
GpioPinName name,
151+
GpioFunction function = GpioFunction::unknown,
152+
GpioPullUp pullUp = GpioPullUp::none
153+
) {
154+
this->name = name;
155+
this->function = function;
156+
this->pullUp = pullUp;
157+
}
115158
};
116159

117160
// PWM
118161
class GpioPwm : GpioPinConfigCommon
119162
{
120163
public:
121-
GpioPwmName name = GpioPwmName::unknown;
164+
GpioPwmItem *item = NULL;
122165
std::uint32_t value = 0;
123166
GpioPwmMode mode = GpioPwmMode::unknown;
124167
std::uint32_t range = 0xff;
168+
169+
GpioPwm (
170+
GpioPwmItem *item,
171+
std::uint32_t range = 0xff,
172+
GpioPwmMode mode = GpioPwmMode::unknown
173+
) {
174+
this->item = item;
175+
this->range = range;
176+
this->mode = mode;
177+
}
125178
};
126179

127180
class Gpio {
@@ -145,7 +198,6 @@ class Gpio {
145198
int init_gpio();
146199
int init_spi();
147200
int close_gpio();
148-
std::uint8_t get_pwm_channel(GpioPwm pin);
149201
};
150202

151203
#endif

0 commit comments

Comments
 (0)