|
| 1 | +#include "OPAMP.h" |
| 2 | +#include <Arduino.h> |
| 3 | + |
| 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 | + |
| 9 | +/* pin mode needed to activate OPAMP functionality */ |
| 10 | +#define OPAMP_IN_PINCFG (IOPORT_CFG_PORT_DIRECTION_INPUT | IOPORT_CFG_PERIPHERAL_PIN | IOPORT_CFG_ANALOG_ENABLE) |
| 11 | +#define OPAMP_OUT_PINCFG (IOPORT_CFG_PORT_DIRECTION_OUTPUT | IOPORT_CFG_PERIPHERAL_PIN | IOPORT_CFG_ANALOG_ENABLE) |
| 12 | +#define FSP_CHECK(err) do { if( (err) != FSP_SUCCESS) return false; } while(0) |
| 13 | + |
| 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 const channel_mask) { |
| 31 | + fsp_err_t err; |
| 32 | + ioport_instance_ctrl_t ioport_ctrl {}; |
| 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 | + } |
| 52 | + // if we got here, none of the checks triggered an early return. |
| 53 | + return true; |
| 54 | +} |
| 55 | + |
| 56 | +void OpampClass::initOpamp(OpampSpeedMode speed, uint8_t const channel_mask) { |
| 57 | + uint8_t ampmc_val = 0U; |
| 58 | + /* setup amplifier speed mode within amplifier mode control */ |
| 59 | + /* for all boards, this is at bit position 7 with either 0 (lowspeed) or 1 (highspeed) */ |
| 60 | + ampmc_val = (uint8_t) ((uint8_t) speed << R_OPAMP_AMPMC_AMPSP_Pos) & R_OPAMP_AMPMC_AMPSP_Msk; |
| 61 | + /* reset opamp */ |
| 62 | + R_OPAMP->AMPC = 0U; |
| 63 | + /* write prepared mode control value */ |
| 64 | + R_OPAMP->AMPMC = ampmc_val; |
| 65 | + /* setup activation trigger select register */ |
| 66 | + /* we only support "Software start & stop" for now, value 0. */ |
| 67 | + R_OPAMP->AMPTRS = 0; |
| 68 | + R_OPAMP->AMPTRM = 0; |
| 69 | + /* set the bits for the activated channels */ |
| 70 | + R_OPAMP->AMPC |= channel_mask; |
| 71 | + /* note: we don't have to activate the charge pump (AMPCPC) because here AVCC0 > 2.7V */ |
| 72 | + /* delay for the wanted init time in microseconds */ |
| 73 | + if (speed == OPAMP_SPEED_LOWSPEED) { |
| 74 | + delayMicroseconds(BSP_FEATURE_OPAMP_MIN_WAIT_TIME_LP_US); |
| 75 | + } else if (speed == OPAMP_SPEED_HIGHSPEED) { |
| 76 | + delayMicroseconds(BSP_FEATURE_OPAMP_MIN_WAIT_TIME_HS_US); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +bool OpampClass::begin() { |
| 81 | + return this->begin(OPAMP_SPEED_HIGHSPEED); |
| 82 | +} |
| 83 | + |
| 84 | +bool OpampClass::begin(OpampSpeedMode const speed) { |
| 85 | + |
| 86 | + return this->begin(1u << ARDUINO_UNO_R4_DEFAULT_OPAMP_CHANNEL, speed); |
| 87 | +} |
| 88 | + |
| 89 | +bool OpampClass::begin(uint8_t const channel_mask, OpampSpeedMode const speed) { |
| 90 | + |
| 91 | + if (!initPins(channel_mask)) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + initOpamp(speed, channel_mask); |
| 95 | + return true; |
| 96 | +} |
| 97 | + |
| 98 | +bool OpampClass::isRunning(uint8_t const channel) { |
| 99 | + return (R_OPAMP->AMPMON & (1u << channel)) != 0; |
| 100 | +} |
| 101 | + |
| 102 | +void OpampClass::end() { |
| 103 | + // deactivate all channels. |
| 104 | + R_OPAMP->AMPC = 0; |
| 105 | +} |
| 106 | + |
| 107 | +void OpampClass::end(uint8_t const channel_mask) { |
| 108 | + // deactivate given channels |
| 109 | + R_OPAMP->AMPC &= ~channel_mask; |
| 110 | +} |
| 111 | + |
| 112 | +/* global instance */ |
| 113 | +OpampClass OPAMP; |
0 commit comments