Skip to content

Commit

Permalink
[TEST] Implement a naïve edge counter
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterBowman committed Jun 19, 2023
1 parent 1fd91c3 commit b79d29d
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 113 deletions.
196 changes: 83 additions & 113 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,72 +1,65 @@
/**
* Copyright: 2023 (C) Universidad Carlos III de Madrid
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LICENSE
**/
/*
* Copyright (c) 2017-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/

#include "mbed.h"
#include "LPC17xx.h"

PinName sensorClockPin = p9;
PinName sensorDataPin = p10;
// Semaphore sem(1);
// Thread thread;

// DigitalOut led(LED1);

class SensorInterruptHandler
{
class Counter {
public:
SensorInterruptHandler(PinName clockPin, PinName dataPin, Thread & _thread)
: clock(clockPin),
data(dataPin),
clockIn(clockPin),
dataIn(dataPin),
thread(_thread),
Counter(PinName clockPin, PinName dataPin)
: _clockInterrupt(clockPin),
_dataInterrupt(dataPin),
isTransmittingFrame(false),
isStartPulse(false),
clockState(true),
dataState(true),
index(FRAME_LENGTH),
buffer(SIGNAL_BIT)
buffer(SIGNAL_BIT),
frames(0)
{
clock.fall(callback(this, &SensorInterruptHandler::onClockFallingEdge));
clock.rise(callback(this, &SensorInterruptHandler::onClockRisingEdge));

data.fall(callback(this, &SensorInterruptHandler::onDataFallingEdge));
data.rise(callback(this, &SensorInterruptHandler::onDataRisingEdge));
_clockInterrupt.rise(callback(this, &Counter::clockRise));
_clockInterrupt.fall(callback(this, &Counter::clockFall));
_dataInterrupt.rise(callback(this, &Counter::dataRise));
_dataInterrupt.fall(callback(this, &Counter::dataFall));
// sem.acquire();
}

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

private:
void onClockFallingEdge()
void clockRise()
{
clockState = false;
}
_countClock++;

void onClockRisingEdge()
{
clockState = true;

if (isTransmittingFrame)
{
index--;
// index--;

if (!dataState)
{
buffer = buffer | (1UL << index);
}
else
{
buffer = buffer | ((index % 2 == 0) << index);
}
// if (!dataState)
// {
// buffer = buffer | (1UL << index);
// }
// else
// {
// buffer = buffer | ((index % 2 == 0) << index);
// }

buffer |= (dataState ? 1 : 0) << --index;
// buffer |= (index % 2) << --index;

if (index == 0)
{
isTransmittingFrame = false;
thread.flags_set(buffer);
frames++;
// thread.flags_set(buffer);
// sem.release();
index = FRAME_LENGTH;
// led = 0;
buffer = SIGNAL_BIT;
}
}
else if (isStartPulse)
Expand All @@ -76,120 +69,97 @@ class SensorInterruptHandler
}
}

void onDataFallingEdge()
void clockFall()
{
dataState = false;
clockState = false;
}

if (!isTransmittingFrame && clockState)
{
isStartPulse = true;
}
int readClock()
{
return _countClock;
}

void onDataRisingEdge()
void dataRise()
{
_countData++;

dataState = true;

if (!isTransmittingFrame && isStartPulse && clockState)
{
isStartPulse = false;
isTransmittingFrame = true;
// led = 1;
// buffer = SIGNAL_BIT;
buffer = SIGNAL_BIT;
}
}

// buffer |= (dataState ? 1 : 0) << --index;
void dataFall()
{
dataState = false;

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

// if (index == 0)
// {
// thread.flags_set(buffer);
// index = FRAME_LENGTH;
// buffer = SIGNAL_BIT;
// }
int readData()
{
return _countData;
}

InterruptIn clock;
InterruptIn data;
DigitalIn clockIn;
DigitalIn dataIn;
Thread & thread;
uint32_t readBuffer() const
{ return buffer; }

int getFrames() const
{ return frames; }

private:
InterruptIn _clockInterrupt;
InterruptIn _dataInterrupt;

volatile int _countClock;
volatile int _countData;

volatile bool isTransmittingFrame;
volatile bool isStartPulse; // pulse high-low-high on DATA while DLCK is high
volatile bool clockState;
volatile bool dataState;
volatile int index;
volatile uint32_t buffer;
volatile int frames;
};

enum SensorChannel : uint8_t
{
VOLTAGE_LEVEL = 0,
FX, FY, FZ,
MX, MY, MZ,
CALIBRATION
};
Counter counter(p9, p10);

void sensorWorker(SensorInterruptHandler * handler)
void sensorWorker()
{
uint32_t raw;

while (true)
{
raw = ThisThread::flags_wait_all(SensorInterruptHandler::SIGNAL_BIT) & 0x000FFFFF; // 20 bits
raw = ThisThread::flags_wait_all(Counter::SIGNAL_BIT) & 0x000FFFFF; // 20 bits

uint8_t rawChannel = raw >> 16; // 4 MSB
uint16_t rawData = raw & 0x0000FFFF; // two's complement representation of a signed 16-bit number

int16_t data = static_cast<int16_t>(rawData);

printf("0x%08x 0x%08x 0x%02x\n", rawData, data, rawChannel);

// switch (rawChannel)
// {
// case VOLTAGE_LEVEL:
// printf("Voltage level: %d\n", data);
// break;
// case FX:
// printf("Fx: %d\n", data);
// break;
// case FY:
// printf("Fy: %d\n", data);
// break;
// case FZ:
// printf("Fz: %d\n", data);
// break;
// case MX:
// printf("Mx: %d\n", data);
// break;
// case MY:
// printf("My: %d\n", data);
// break;
// case MZ:
// printf("Mz: %d\n", data);
// break;
// case CALIBRATION:
// printf("Calibration: %d\n", data);
// break;
// default:
// printf("Unknown channel: %d\n", data);
// break;
// }
// printf("0x%08x 0x%08x 0x%02x\n", rawData, data, rawChannel);
printf("Count so far: %d %d 0x%08X\n", counter.readClock(), counter.readData(), counter.readBuffer());
}
}

int main()
{
Thread sensorThread;
SensorInterruptHandler sensorInterrupt(sensorClockPin, sensorDataPin, sensorThread);

sensorThread.start(callback(sensorWorker, &sensorInterrupt));
// thread.start(callback(sensorWorker));

while (true)
while (1)
{
printf("loop\n");
ThisThread::sleep_for(1000);
// printf("loop\n");
// ThisThread::sleep_for(1000);
// sem.acquire();
printf("Count so far: cticks=%d dticks=%d frames=%d data=0x%08X\n", counter.readClock(), counter.readData(), counter.getFrames(), counter.readBuffer());
ThisThread::sleep_for(2000);
// sem.acquire();
}

sensorThread.join();
}
8 changes: 8 additions & 0 deletions mbed-app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"requires": ["bare-metal"],
"target_overrides": {
"*": {
"target.c_lib": "small"
}
}
}

0 comments on commit b79d29d

Please sign in to comment.