forked from arduino/ArduinoCore-renesas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOPAMP.cpp
107 lines (95 loc) · 3.99 KB
/
OPAMP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "OPAMP.h"
#include <Arduino.h>
/* Make sure this library fails to compile for unsupported boards. */
#if !defined(ARDUINO_UNOWIFIR4) && !defined(ARDUINO_MINIMA)
#error "Unsupported board for OPAMP library."
#endif
/* pin mode needed to activate OPAMP functionality */
#define OPAMP_IN_PINCFG (IOPORT_CFG_PORT_DIRECTION_INPUT | IOPORT_CFG_PERIPHERAL_PIN | IOPORT_CFG_ANALOG_ENABLE)
#define OPAMP_OUT_PINCFG (IOPORT_CFG_PORT_DIRECTION_OUTPUT | IOPORT_CFG_PERIPHERAL_PIN | IOPORT_CFG_ANALOG_ENABLE)
#define FSP_CHECK(err) do { if( (err) != FSP_SUCCESS) return false; } while(0)
// Compact structure for OPAMP channel pins
struct opamp_channel_pins_t {
bsp_io_port_pin_t plus;
bsp_io_port_pin_t minus;
bsp_io_port_pin_t output;
};
// See Renesas RA4M1 Group Datasheet
// Note: Channel 0 is the only accessible one one the Arduino Minima boards.
static const opamp_channel_pins_t opamp_channels[] = {
{BSP_IO_PORT_00_PIN_00, BSP_IO_PORT_00_PIN_01, BSP_IO_PORT_00_PIN_02}, /* CH0 */
{BSP_IO_PORT_00_PIN_13, BSP_IO_PORT_00_PIN_12, BSP_IO_PORT_00_PIN_03}, /* CH1 */
{BSP_IO_PORT_00_PIN_11, BSP_IO_PORT_00_PIN_10, BSP_IO_PORT_00_PIN_04}, /* CH2 */
{BSP_IO_PORT_00_PIN_05, BSP_IO_PORT_00_PIN_06, BSP_IO_PORT_00_PIN_07}, /* CH3 */
};
bool OpampClass::initPins(uint8_t channel_mask) {
fsp_err_t err;
ioport_instance_ctrl_t ioport_ctrl {};
// Make sure to return false if nothing was given to initialize
// or a too high channel bit is in there
if (channel_mask == 0 || channel_mask > 0b1111) {
return false;
}
// Check the 4 possible channels
for (uint8_t i = 0; i < 4; i++) {
// was this channel selected?
if (!(channel_mask & (1u << i))) {
continue;
}
opamp_channel_pins_t pins = opamp_channels[i];
err = R_IOPORT_PinCfg(&ioport_ctrl, pins.plus, OPAMP_IN_PINCFG);
FSP_CHECK(err);
err = R_IOPORT_PinCfg(&ioport_ctrl, pins.minus, OPAMP_IN_PINCFG);
FSP_CHECK(err);
err = R_IOPORT_PinCfg(&ioport_ctrl, pins.output, OPAMP_OUT_PINCFG);
FSP_CHECK(err);
}
// if we got here, none of the checks triggered an early return.
return true;
}
void OpampClass::initOpamp(OpampSpeedMode speed, uint8_t channel_mask) {
uint8_t ampmc_val = 0U;
/* setup amplifier speed mode within amplifier mode control */
/* for all boards, this is at bit position 7 with either 0 (lowspeed) or 1 (highspeed) */
ampmc_val = (uint8_t) ((uint8_t) speed << R_OPAMP_AMPMC_AMPSP_Pos) & R_OPAMP_AMPMC_AMPSP_Msk;
/* reset opamp */
R_OPAMP->AMPC = 0U;
/* write prepared mode control value */
R_OPAMP->AMPMC = ampmc_val;
/* setup activation trigger select register */
/* we only support "Software start & stop" for now, value 0. */
R_OPAMP->AMPTRS = 0;
R_OPAMP->AMPTRM = 0;
/* set the bits for the activated channels */
R_OPAMP->AMPC |= channel_mask;
/* note: we don't have to activate the charge pump (AMPCPC) because here AVCC0 > 2.7V */
/* delay for the wanted init time in microseconds */
if (speed == OPAMP_SPEED_LOWSPEED) {
delayMicroseconds(BSP_FEATURE_OPAMP_MIN_WAIT_TIME_LP_US);
} else if (speed == OPAMP_SPEED_HIGHSPEED) {
delayMicroseconds(BSP_FEATURE_OPAMP_MIN_WAIT_TIME_HS_US);
}
}
bool OpampClass::begin(OpampSpeedMode speed) {
return this->begin(1u << ARDUINO_DEFAULT_OPAMP_CHANNEL, speed);
}
bool OpampClass::begin(uint8_t channel_mask, OpampSpeedMode speed) {
if (!initPins(channel_mask)) {
return false;
}
initOpamp(speed, channel_mask);
return true;
}
bool OpampClass::isRunning(uint8_t const channel) {
return (R_OPAMP->AMPMON & (1u << channel)) != 0;
}
void OpampClass::end() {
// deactivate all channels.
R_OPAMP->AMPC = 0;
}
void OpampClass::end(uint8_t channel_mask) {
// deactivate given channels
R_OPAMP->AMPC &= ~channel_mask;
}
/* global instance */
OpampClass OPAMP;