Skip to content

Commit ef3472a

Browse files
committed
Rework begin(), end() to use channel masks, create isRunning() instead
1 parent 57c7f97 commit ef3472a

File tree

3 files changed

+71
-29
lines changed

3 files changed

+71
-29
lines changed

libraries/OPAMP/examples/start_opamp/start_opamp.ino

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
void setup () {
44
Serial.begin(9600);
5-
delay(2000); // serial monitor delay
5+
delay(2000); // serial monitor delay
66
// activate OPAMP
77
if (!OPAMP.begin(OPAMP_SPEED_HIGHSPEED)) {
88
Serial.println("Failed to start OPAMP!");
99
}
10-
uint8_t status = OPAMP.getStatus();
11-
if (status & (1u << 0u)) {
10+
bool isRunning = OPAMP.isRunning(0);
11+
if (isRunning) {
1212
Serial.println("OPAMP running on channel 0!");
1313
} else {
1414
Serial.println("OPAMP channel 0 is not running!");
1515
}
1616
}
1717

1818
void loop() {
19-
delay(1000); // do nothing
20-
}
19+
delay(1000); // do nothing
20+
}

libraries/OPAMP/src/OPAMP.cpp

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,54 @@
11
#include "OPAMP.h"
22
#include <Arduino.h>
33

4+
/* Make sure this library fails to compile for unsupported boards. */
5+
#if !defined(ARDUINO_UNOWIFIR4) && !defined(ARDUINO_MINIMA)
6+
#error "Unsupported board for OPAMP library."
7+
#endif
8+
49
/* pin mode needed to activate OPAMP functionality */
510
#define OPAMP_IN_PINCFG (IOPORT_CFG_PORT_DIRECTION_INPUT | IOPORT_CFG_PERIPHERAL_PIN | IOPORT_CFG_ANALOG_ENABLE)
611
#define OPAMP_OUT_PINCFG (IOPORT_CFG_PORT_DIRECTION_OUTPUT | IOPORT_CFG_PERIPHERAL_PIN | IOPORT_CFG_ANALOG_ENABLE)
712
#define FSP_CHECK(err) do { if( (err) != FSP_SUCCESS) return false; } while(0)
813

9-
bool OpampClass::initPins() {
14+
// Compact structure for OPAMP channel pins
15+
struct opamp_channel_pins_t {
16+
bsp_io_port_pin_t plus;
17+
bsp_io_port_pin_t minus;
18+
bsp_io_port_pin_t output;
19+
};
20+
21+
// See Renesas RA4M1 Group Datasheet
22+
// Note: Channel 0 is the only accessible one one the Arduino Minima boards.
23+
static const opamp_channel_pins_t opamp_channels[] = {
24+
{BSP_IO_PORT_00_PIN_00, BSP_IO_PORT_00_PIN_01, BSP_IO_PORT_00_PIN_02}, /* CH0 */
25+
{BSP_IO_PORT_00_PIN_13, BSP_IO_PORT_00_PIN_12, BSP_IO_PORT_00_PIN_03}, /* CH1 */
26+
{BSP_IO_PORT_00_PIN_11, BSP_IO_PORT_00_PIN_10, BSP_IO_PORT_00_PIN_04}, /* CH2 */
27+
{BSP_IO_PORT_00_PIN_05, BSP_IO_PORT_00_PIN_06, BSP_IO_PORT_00_PIN_07}, /* CH3 */
28+
};
29+
30+
bool OpampClass::initPins(uint8_t channel_mask) {
1031
fsp_err_t err;
1132
ioport_instance_ctrl_t ioport_ctrl {};
12-
#if defined(ARDUINO_UNOWIFIR4) || defined(ARDUINO_MINIMA)
13-
/* channel 0 pins. Only accessible ones. */
14-
err = R_IOPORT_PinCfg(&ioport_ctrl, BSP_IO_PORT_00_PIN_00, OPAMP_IN_PINCFG);
15-
FSP_CHECK(err);
16-
err = R_IOPORT_PinCfg(&ioport_ctrl, BSP_IO_PORT_00_PIN_01, OPAMP_IN_PINCFG);
17-
FSP_CHECK(err);
18-
err = R_IOPORT_PinCfg(&ioport_ctrl, BSP_IO_PORT_00_PIN_02, OPAMP_OUT_PINCFG);
19-
FSP_CHECK(err);
20-
#else
21-
#error "Unsupported board."
22-
#endif
33+
// Make sure to return false if nothing was given to initialize
34+
// or a too high channel bit is in there
35+
if (channel_mask == 0 || channel_mask > 0b1111) {
36+
return false;
37+
}
38+
// Check the 4 possible channels
39+
for(uint8_t i = 0; i < 4; i++) {
40+
// was this channel selected?
41+
if (!(channel_mask & (1u << i))) {
42+
continue;
43+
}
44+
opamp_channel_pins_t pins = opamp_channels[i];
45+
err = R_IOPORT_PinCfg(&ioport_ctrl, pins.plus, OPAMP_IN_PINCFG);
46+
FSP_CHECK(err);
47+
err = R_IOPORT_PinCfg(&ioport_ctrl, pins.minus, OPAMP_IN_PINCFG);
48+
FSP_CHECK(err);
49+
err = R_IOPORT_PinCfg(&ioport_ctrl, pins.output, OPAMP_OUT_PINCFG);
50+
FSP_CHECK(err);
51+
}
2352
// if we got here, none of the checks triggered an early return.
2453
return true;
2554
}
@@ -49,20 +78,29 @@ void OpampClass::initOpamp(OpampSpeedMode speed, uint8_t channel_mask) {
4978
}
5079

5180
bool OpampClass::begin(OpampSpeedMode speed) {
52-
if(!initPins()) {
81+
return this->begin(1u << ARDUINO_DEFAULT_OPAMP_CHANNEL, speed);
82+
}
83+
84+
bool OpampClass::begin(uint8_t channel_mask, OpampSpeedMode speed) {
85+
if(!initPins(channel_mask)) {
5386
return false;
5487
}
55-
initOpamp(speed, (1u << ARDUINO_USED_OPAMP_CHANNEL));
88+
initOpamp(speed, channel_mask);
5689
return true;
5790
}
5891

59-
uint8_t OpampClass::getStatus() {
60-
return R_OPAMP->AMPMON;
92+
bool OpampClass::isRunning(uint8_t const channel) {
93+
return (R_OPAMP->AMPMON & (1u << channel)) != 0;
6194
}
6295

6396
void OpampClass::end() {
64-
// clear the bit for the used channel
65-
R_OPAMP->AMPC &= (uint8_t) ~(1u << ARDUINO_USED_OPAMP_CHANNEL);
97+
// deactivate all channels.
98+
R_OPAMP->AMPC = 0;
99+
}
100+
101+
void OpampClass::end(uint8_t channel_mask) {
102+
// deactivate given channels
103+
R_OPAMP->AMPC &= ~channel_mask;
66104
}
67105

68106
/* global instance */

libraries/OPAMP/src/OPAMP.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ enum OpampSpeedMode {
1212

1313
/* The supported boards have 4 OPAMP channels, however, only channel 0 is accessible. */
1414
/* All other channels are connected to the LED matrix or not exposed. */
15-
#define ARDUINO_USED_OPAMP_CHANNEL 0
15+
#define ARDUINO_DEFAULT_OPAMP_CHANNEL 0
1616

1717
/**
1818
* Pin Mapping for OPAMP
@@ -32,13 +32,17 @@ class OpampClass {
3232
public:
3333
/* startup the OPAMP on channel 0 */
3434
bool begin(OpampSpeedMode speed = OPAMP_SPEED_HIGHSPEED);
35-
/* stop the OPAMP on channel 0 */
35+
/* startup the OPAMP with arbitrary channel mask */
36+
bool begin(uint8_t channel_mask, OpampSpeedMode speed = OPAMP_SPEED_HIGHSPEED);
37+
/* stop all OPAMP channels */
3638
void end();
37-
/* i-th Bit: 0 if OPAMP channel is stopped, 1 if OPAMP channel is operating */
38-
uint8_t getStatus();
39+
/* stop specific OPAMP channel(s) */
40+
void end(uint8_t channel_mask);
41+
/* returns true if the specified OPAMP channel number is running */
42+
bool isRunning(uint8_t const channel);
3943
private:
40-
/* initializes OPAMP pins */
41-
bool initPins();
44+
/* initializes OPAMP pins for given channel(s) */
45+
bool initPins(uint8_t channel_mask);
4246
/* activates OPAMP for given speed and channel(s) */
4347
void initOpamp(OpampSpeedMode speed, uint8_t channel_mask);
4448
};

0 commit comments

Comments
 (0)