Skip to content

Commit

Permalink
FW: Correct PPM In&Out not starting
Browse files Browse the repository at this point in the history
  • Loading branch information
dlktdr committed Aug 3, 2024
1 parent fe4e2d6 commit 4ca2371
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 62 deletions.
42 changes: 17 additions & 25 deletions firmware/src/src/PPMIn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ LOG_MODULE_REGISTER(ppmin);
static bool framestarted = false;
static bool ppminstarted = false;
static bool ppminverted = false;
static int setPin = -1;

// Used in ISR
static uint16_t isrchannels[16];
Expand Down Expand Up @@ -94,19 +93,9 @@ ISR_DIRECT_DECLARE(PPMInGPIOTE_ISR)
}

// Set pin to -1 to disable
void PpmIn_setPin(int pinNum)
void PpmIn_startStop(bool stop)
{
// Same pin, just quit

// #if defined(CONFIG_BOARD_ARDUINO_NANO_33_BLE) // Nano33, can pick D2-D12
// if (pinNum == setPin) return;
// int pin = D_TO_PIN(pinNum);
// int port = D_TO_PORT(pinNum);
// int setPin_pin = D_TO_PIN(setPin);
// int setPin_port = D_TO_PORT(setPin);
// #else

// If pin is fixed don't allow turning it off
static bool initalized = false;
if(initalized) return;
initalized = true;
Expand All @@ -118,7 +107,7 @@ void PpmIn_setPin(int pinNum)
// Stop Interrupts
uint32_t key = irq_lock();

if (pinNum < 0 && ppminstarted) { // Disable
if (stop && ppminstarted) { // Disable

// Stop Interrupt
NRF_GPIOTE->INTENCLR = PPMIN_GPIOTE_MASK;
Expand All @@ -143,12 +132,8 @@ void PpmIn_setPin(int pinNum)
GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos;

// Set pin num and started flag
setPin = pinNum;
ppminstarted = false;

} else {
setPin = pinNum;

// Disable Interrupt, Clear event
NRF_GPIOTE->INTENCLR = PPMIN_GPIOTE_MASK;
NRF_GPIOTE->EVENTS_IN[PPMIN_GPIOTE] = 0;
Expand Down Expand Up @@ -210,15 +195,15 @@ void PpmIn_setPin(int pinNum)
irq_unlock(key);
}

// TODO REMOVE ME. This function works inverted or not.
void PpmIn_setInverted(bool inv)
{
if (!ppminstarted) return;

if (ppminverted != inv) {
int op = setPin;
PpmIn_setPin(-1);
PpmIn_startStop(true);
ppminverted = inv;
PpmIn_setPin(op);
PpmIn_startStop();
}
}

Expand All @@ -231,7 +216,7 @@ void PpmIn_execute()

if (micros64() - runtime > 60000) {
if (sentconn == false) {
LOG_WRN("PPM Input Data Lost");
LOG_INF("PPM Input Data Lost");
sentconn = true;
ch_count = 0;
}
Expand Down Expand Up @@ -269,24 +254,31 @@ int PpmIn_getChannels(uint16_t *ch)
return rval;
}

int PpmIn_init() {return 0;}
int PpmIn_init() {
LOG_INF("Using pin %s", StrPinDescriptions[IO_PPMIN]);
PpmIn_startStop();
return 0;
}

#elif DT_NODE_EXISTS(DT_NODELABEL(ppm_input))
#warning "PPMIn using DT_NODELABEL(ppm_input) pin"

int PpmIn_init() {return 0;}
int PpmIn_init() {
LOG_ERR("PPM In function is not completed. We need helpers here");
return 0;
}
int PpmIn_getChannels(uint16_t *ch)
{
return 0;
}
void PpmIn_setPin(int pinNum) {}
void PpmIn_startStop(bool stop) {}
void PpmIn_execute() {}

#else

int PpmIn_init() {return -1;}
int PpmIn_getChannels(uint16_t *ch) {return 0;}
void PpmIn_setPin(int pinNum) {}
void PpmIn_startStop(bool stop) {}
void PpmIn_execute() {}

#endif
38 changes: 8 additions & 30 deletions firmware/src/src/PPMOut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ volatile bool interrupt = false;

static volatile bool ppmoutstarted = false;
static volatile bool ppmoutinverted = false;
static int setPin = -1;

// Used in ISR

// Used to read data at once, read with isr disabled
static uint16_t ch_values[16];
static int ch_count;
Expand Down Expand Up @@ -136,22 +134,10 @@ ISR_DIRECT_DECLARE(PPMTimerISR)

// Set pin to -1 to disable

void PpmOut_setPin(int pinNum)
void PpmOut_startStop(bool stop)
{
// Same pin, just quit
if (pinNum == setPin) return;

// #if defined(CONFIG_BOARD_ARDUINO_NANO_33_BLE) // Nano33, can pick D2-D12
// int pin = D_TO_PIN(pinNum);
// int port = D_TO_PORT(pinNum);
// int setPin_pin = D_TO_PIN(setPin);
// int setPin_port = D_TO_PORT(setPin);
// #else
int pin = PIN_TO_GPIN(PIN_NAME_TO_NUM(IO_PPMOUT));
int port = PIN_TO_GPORT(PIN_NAME_TO_NUM(IO_PPMOUT));
int setPin_pin = pin;
int setPin_port = port;
// #endif

if (!ppmoutstarted) {
resetChannels();
Expand Down Expand Up @@ -179,20 +165,8 @@ void PpmOut_setPin(int pinNum)
// Disable PPI
NRF_PPI->CHENCLR = PPMOUT_PPICH_MSK;

// Set current pin back to low drive , if enabled
if (setPin > 0) {
if (setPin_port == 0)
NRF_P0->PIN_CNF[setPin_pin] =
(NRF_P0->PIN_CNF[setPin_pin] & ~GPIO_PIN_CNF_DRIVE_Msk) |
GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos;
else if (setPin_port == 1)
NRF_P1->PIN_CNF[setPin_pin] =
(NRF_P1->PIN_CNF[setPin_pin] & ~GPIO_PIN_CNF_DRIVE_Msk) |
GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos;
}

// If we want to enable it....
if (pinNum > 0) {
if (stop == false) {
// Setup GPOITE[7] to toggle output on every timer capture
NRF_GPIOTE->CONFIG[PPMOUT_GPIOTE] =
(GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos) |
Expand Down Expand Up @@ -238,7 +212,6 @@ void PpmOut_setPin(int pinNum)
PPMOUT_TIMER->INTENSET = PPMOUT_TMRCOMP_CH_MSK;
}

setPin = pinNum;
irq_unlock(key);
}

Expand All @@ -259,7 +232,12 @@ void PpmOut_setChannel(int chan, uint16_t val)
}

int PpmOut_getChnCount() { return ch_count; }
int PpmOut_init() {return 0;}
int PpmOut_init()
{
LOG_INF("Using pin %s", StrPinDescriptions[IO_PPMOUT]);
PpmOut_startStop();
return 0;
}

#elif DT_NODE_EXISTS(DT_NODELABEL(ppm_output))
#include <zephyr/device.h>
Expand Down
22 changes: 17 additions & 5 deletions firmware/src/src/htmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,32 @@ void start(void)
}

// Start PPM Output
LOG_INF("Starting PPMOut");
#if defined(HAS_PPMOUT)
LOG_INF("PPMOut Starting");
if(PpmOut_init()) {
LOG_WRN("PPMOut_init failed");
LOG_WRN("PPMOut initalization failed");
}
#elif
LOG_INF("PPMOut is not supported on this board");
#endif

// Start PPM Input
LOG_INF("Starting PPMIn");
#if defined(HAS_PPMIN)
LOG_INF("PPMIn Starting");
if(PpmIn_init()) {
LOG_WRN("PPMIn_init failed");
LOG_WRN("PPMIn initalization failed");
}
#elif
LOG_INF("PPMIn is not supported on this board");
#endif

// Start External UART
LOG_INF("Starting AuxUART");
#if defined(HAS_AUXSERIAL)
LOG_INF("AuxUART Starting");
uart_init();
#elif
LOG_INF("AuxUART is not supported on this board");
#endif

// PWM Outputs - Fixed to A0-A3
#if defined(HAS_PWMOUTPUTS)
Expand Down
2 changes: 1 addition & 1 deletion firmware/src/src/include/PPMIn.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
#include <stdint.h>

int PpmIn_init();
void PpmIn_setPin(int pinNum);
void PpmIn_startStop(bool stop=false);
int PpmIn_getChannels(uint16_t *ch);
void PpmIn_execute();
2 changes: 1 addition & 1 deletion firmware/src/src/include/PPMOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <stdint.h>

int PpmOut_init();
void PpmOut_setPin(int pinNum);
void PpmOut_startStop(bool stop=false);
void PpmOut_setChannel(int chan, uint16_t val);
void PpmOut_execute();
int PpmOut_getChnCount();
Expand Down

0 comments on commit 4ca2371

Please sign in to comment.