Skip to content

Commit

Permalink
[TEST] Count ticks with PortIn
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterBowman committed Jun 19, 2023
1 parent ab2c869 commit 6f7a94b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 99 deletions.
124 changes: 33 additions & 91 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,122 +1,64 @@
/*
* Copyright (c) 2017-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/

#include "mbed.h"

#include "LPC17xx.h"
PortIn port(Port0, 0x0000003F);

static const unsigned FRAME_LENGTH = 20;
static const uint32_t SIGNAL_BIT = 0x10000000U; // don't use 0x80000000U as it is `osFlagsError`
volatile bool stateClock = true;
volatile bool stateData = true;

volatile unsigned int _countClock;
volatile unsigned int _countData;
volatile int clockTicks = 0;
volatile int dataTicks = 0;

volatile bool isTransmittingFrame = false;
volatile bool isStartPulse = false; // pulse high-low-high on DATA while DLCK is high
volatile bool clockState = true;
volatile bool dataState = true;
volatile unsigned int index = FRAME_LENGTH;
volatile uint32_t buffer = SIGNAL_BIT;
volatile unsigned int frames = 0;
Thread thread;

extern "C" void EINT3_IRQHandler(void)
void worker()
{
// Clear the interrupt.
LPC_GPIOINT->IO0IntClr = ~((uint32_t) 0);
int value;

if ((LPC_GPIOINT->IO0IntStatR & (1 << 0)) == (1 << 0))
while (true)
{
_countClock++;
value = port.read();

clockState = true;

if (isTransmittingFrame)
if ((value & 0x00000001) == 1)
{
buffer |= (dataState ? 1 : 0) << --index;

if (index == 0)
if (!stateClock)
{
isTransmittingFrame = false;
frames++;
// thread.flags_set(buffer);
// sem.release();
index = FRAME_LENGTH;
clockTicks++;
stateClock = true;
}
}
else if (isStartPulse)
else
{
// invalidate, we are amid a frame transmission (which is not being accounted)
isStartPulse = false;
if (stateClock)
{
stateClock = false;
}
}
}
else if ((LPC_GPIOINT->IO0IntStatF & (1 << 0)) == (1 << 0))
{
clockState = false;
}
else if ((LPC_GPIOINT->IO0IntStatR & (1 << 1)) == (1 << 1))
{
_countData++;

dataState = true;

if (!isTransmittingFrame && isStartPulse && clockState)
if ((value & 0x00000002) == 1)
{
isStartPulse = false;
isTransmittingFrame = true;
buffer = SIGNAL_BIT;
if (!stateData)
{
dataTicks++;
stateData = true;
}
}
}
else if ((LPC_GPIOINT->IO0IntStatF & (1 << 1)) == (1 << 1))
{
dataState = false;

if (!isTransmittingFrame && clockState)
else
{
isStartPulse = true;
if (stateData)
{
stateData = false;
}
}
}
}

class Counter
{
public:
Counter(unsigned int clockPin, unsigned int dataPin)
{
__disable_irq();

NVIC_DisableIRQ(EINT3_IRQn);

// Power up GPIO.
// LPC_SC->PCONP |= (1 << 15);

// GPIO P0.0 and P0.1 as input.
LPC_PINCON->PINSEL0 &= ~((1 << 3) | (1 << 2) | (1 << 1) | (1 << 0));
// LPC_PINCON->PINMODE1 &= ~(0x3 << 0);
// LPC_PINCON->PINMODE1 |= (0x3 << 0);
LPC_GPIO0->FIODIR &= ~((1 << 1) | (1 << 0));

LPC_GPIOINT->IO0IntEnR |= ((1 << 1) | (1 << 0));
LPC_GPIOINT->IO0IntEnF |= ((1 << 1) | (1 << 0));

LPC_SC->EXTINT = 1;

NVIC_EnableIRQ(EINT3_IRQn);

__enable_irq();
}
};

int main()
{
Counter counter(9, 10);
thread.start(callback(&worker));

while (true)
{
printf("Count so far: cticks=%d dticks=%d frames=%d data=0x%08X\n", _countClock, _countData, frames, buffer);
ThisThread::sleep_for(2000);
printf("cticks = %d, dticks = %d\n", clockTicks, dataTicks);
ThisThread::sleep_for(1000);
}

return 0;
}
8 changes: 0 additions & 8 deletions mbed-app.json

This file was deleted.

0 comments on commit 6f7a94b

Please sign in to comment.