Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PCInt class #131

Merged
merged 1 commit into from Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 14 additions & 22 deletions andino_firmware/src/encoder_driver.cpp
Expand Up @@ -70,13 +70,15 @@
#include "Arduino.h"
#include "commands.h"
#include "hw.h"
#include "pcint.h"

volatile long left_enc_pos = 0L;
volatile long right_enc_pos = 0L;
static const int8_t ENC_STATES[] = {0, 1, -1, 0, -1, 0, 0, 1, 1, 0, 0, -1, 0, -1, 1, 0}; // encoder lookup table
static const int8_t ENC_STATES[] = {0, 1, -1, 0, -1, 0, 0, 1,
1, 0, 0, -1, 0, -1, 1, 0}; // encoder lookup table

/* Interrupt routine for LEFT encoder, taking care of actual counting */
ISR(PCINT2_vect) {
void left_encoder_cb() {
static uint8_t enc_last = 0;

enc_last <<= 2; // shift previous state two places
Expand All @@ -86,7 +88,7 @@ ISR(PCINT2_vect) {
}

/* Interrupt routine for RIGHT encoder, taking care of actual counting */
ISR(PCINT1_vect) {
void right_encoder_cb() {
static uint8_t enc_last = 0;

enc_last <<= 2; // shift previous state two places
Expand All @@ -96,25 +98,15 @@ ISR(PCINT1_vect) {
}

void initEncoders() {
// set as inputs
DDRD &= ~(1 << LEFT_ENCODER_A_GPIO_PIN);
DDRD &= ~(1 << LEFT_ENCODER_B_GPIO_PIN);
DDRC &= ~(1 << RIGHT_ENCODER_A_GPIO_PIN);
DDRC &= ~(1 << RIGHT_ENCODER_B_GPIO_PIN);

// enable pull up resistors
PORTD |= (1 << LEFT_ENCODER_A_GPIO_PIN);
PORTD |= (1 << LEFT_ENCODER_B_GPIO_PIN);
PORTC |= (1 << RIGHT_ENCODER_A_GPIO_PIN);
PORTC |= (1 << RIGHT_ENCODER_B_GPIO_PIN);

// tell pin change mask to listen to left encoder pins
PCMSK2 |= (1 << LEFT_ENCODER_A_GPIO_PIN) | (1 << LEFT_ENCODER_B_GPIO_PIN);
// tell pin change mask to listen to right encoder pins
PCMSK1 |= (1 << RIGHT_ENCODER_A_GPIO_PIN) | (1 << RIGHT_ENCODER_B_GPIO_PIN);

// enable PCINT1 and PCINT2 interrupt in the general interrupt mask
PCICR |= (1 << PCIE1) | (1 << PCIE2);
pinMode(LEFT_ENCODER_A_GPIO_PIN, INPUT_PULLUP);
pinMode(LEFT_ENCODER_B_GPIO_PIN, INPUT_PULLUP);
pinMode(RIGHT_ENCODER_A_GPIO_PIN, INPUT_PULLUP);
pinMode(RIGHT_ENCODER_B_GPIO_PIN, INPUT_PULLUP);

andino::PCInt::attach_interrupt(LEFT_ENCODER_A_GPIO_PIN, left_encoder_cb);
andino::PCInt::attach_interrupt(LEFT_ENCODER_B_GPIO_PIN, left_encoder_cb);
andino::PCInt::attach_interrupt(RIGHT_ENCODER_A_GPIO_PIN, right_encoder_cb);
andino::PCInt::attach_interrupt(RIGHT_ENCODER_B_GPIO_PIN, right_encoder_cb);
}

/* Wrap the encoder reading function */
Expand Down
12 changes: 6 additions & 6 deletions andino_firmware/src/hw.h
Expand Up @@ -29,17 +29,17 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once

// PC4 (pin A4), RIGHT ENCODER PIN A
#define RIGHT_ENCODER_A_GPIO_PIN PC4
// PC4 (pin 18), RIGHT ENCODER PIN A
jballoffet marked this conversation as resolved.
Show resolved Hide resolved
#define RIGHT_ENCODER_A_GPIO_PIN 18

// PC5 (pin A5), RIGHT ENCODER PIN B
#define RIGHT_ENCODER_B_GPIO_PIN PC5
// PC5 (pin 19), RIGHT ENCODER PIN B
#define RIGHT_ENCODER_B_GPIO_PIN 19

// PD2 (pin 2), LEFT ENCODER PIN A
#define LEFT_ENCODER_A_GPIO_PIN PD2
#define LEFT_ENCODER_A_GPIO_PIN 2

// PD3 (pin 3), LEFT ENCODER PIN B
#define LEFT_ENCODER_B_GPIO_PIN PD3
#define LEFT_ENCODER_B_GPIO_PIN 3

// PD5 (pin 5), RIGHT MOTOR DRIVER BACKWARD PIN
#define RIGHT_MOTOR_BACKWARD_GPIO_PIN 5
Expand Down
79 changes: 79 additions & 0 deletions andino_firmware/src/pcint.cpp
@@ -0,0 +1,79 @@
// BSD 3-Clause License
//
// Copyright (c) 2023, Ekumen Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "pcint.h"

#include "Arduino.h"

static andino::PCInt::InterruptCallback g_callbacks[3] = {nullptr};

ISR(PCINT0_vect) {
if (g_callbacks[0] != nullptr) {
g_callbacks[0]();
}
}

ISR(PCINT1_vect) {
if (g_callbacks[1] != nullptr) {
g_callbacks[1]();
}
}

ISR(PCINT2_vect) {
if (g_callbacks[2] != nullptr) {
g_callbacks[2]();
}
}

namespace andino {

constexpr volatile uint8_t* PCInt::kPortToPCMask[];

void PCInt::attach_interrupt(uint8_t pin, InterruptCallback callback) {
uint8_t bit_mask = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);

if (port == NOT_A_PORT) {
return;
}

// Ports B, C and D values are 2, 3 and 4 correspondingly.
port -= 2;

// Set corresponding bit in the appropriate Pin Change Mask register.
*(kPortToPCMask[port]) |= bit_mask;

// Set corresponding bit in the Pin Change Interrupt Control register.
PCICR |= 0x01 << port;

// Set callback function.
g_callbacks[port] = callback;
}

} // namespace andino
56 changes: 56 additions & 0 deletions andino_firmware/src/pcint.h
@@ -0,0 +1,56 @@
// BSD 3-Clause License
//
// Copyright (c) 2023, Ekumen Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once

#include <stdint.h>

#include "Arduino.h"

namespace andino {

/// @brief This class provides a simple way to set and use Pin Change Interrupts.
class PCInt {
public:
/// @brief Interrupt callback type.
typedef void (*InterruptCallback)();

/// @brief Attaches an interrupt callback.
/// @note Only one callback per port is supported.
///
/// @param pin Pin of interest.
/// @param callback Callback function.
static void attach_interrupt(uint8_t pin, InterruptCallback callback);

private:
/// Map between ports and Pin Change Mask registers.
static constexpr volatile uint8_t* kPortToPCMask[]{&PCMSK0, &PCMSK1, &PCMSK2};
};

} // namespace andino