Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .application.c.swp
Binary file not shown.
Binary file modified Debug/RTS-Lab.elf
Binary file not shown.
604 changes: 494 additions & 110 deletions Debug/RTS-Lab.map

Large diffs are not rendered by default.

622 changes: 450 additions & 172 deletions Debug/RTS-Lab.s19

Large diffs are not rendered by default.

Binary file modified Debug/application.o
Binary file not shown.
Binary file added Debug/semaphore.o
Binary file not shown.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ OBJECTS= $(DEBUGDIR)dispatch.o \
$(DEBUGDIR)stm32f4xx_tim.o \
$(DEBUGDIR)stm32f4xx_usart.o \
$(DEBUGDIR)startup.o \
$(DEBUGDIR)application.o
$(DEBUGDIR)application.o \
$(DEBUGDIR)semaphore.o

###
### Main target
Expand Down Expand Up @@ -100,9 +101,11 @@ $(DEBUGDIR)canTinyTimber.o: canTinyTimber.c canTinyTimber.h
$(CC) -c $< -o $@ $(CCFLAGS)
$(DEBUGDIR)sciTinyTimber.o: sciTinyTimber.c sciTinyTimber.h
$(CC) -c $< -o $@ $(CCFLAGS)
$(DEBUGDIR)semaphore.o: semaphore.c semaphore.h
$(CC) -c $< -o $@ $(CCFLAGS)

# User-defined targets
$(DEBUGDIR)application.o: application.c TinyTimber.h sciTinyTimber.h canTinyTimber.h
$(DEBUGDIR)application.o: application.c TinyTimber.h sciTinyTimber.h canTinyTimber.h semaphore.h
$(CC) -c $< -o $@ $(CCFLAGS)

###
Expand Down
16 changes: 16 additions & 0 deletions RTS-Lab.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"folders": [
{
"path": "."
}
],
"settings": {
"makefile.launchConfigurations": [
{
"cwd": "c:\\Users\\DELL\\Desktop\\RTS-Lab\\Debug",
"binaryPath": "c:\\Users\\DELL\\Desktop\\RTS-Lab\\Debug\\RTS-Lab.elf",
"binaryArgs": []
}
]
}
}
278 changes: 262 additions & 16 deletions application.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,89 @@
#include "canTinyTimber.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "semaphore.h" // semaphore
#define TRUE 1
#define FALSE 0

/*
* D : enable/disable deadline
*
* M: mute/unmute
*
* up arrow: increase volume
*
* down arrow: decrease volume
*
* left arrow: decrease background load
*
* right arrow: increase background load
*/

typedef struct {
Object super;
int count;
char c;
} App;

App app = { initObject(), 0, 'X' };
typedef struct {
Object super;
CallBlock callBlock;
int period;
bool lh; // 1 or 0
int volume;
int mute;
Time deadline;
} ToneGenerator;

typedef struct { // step 2
Object super;
int background_loop_range;
Time deadline;
} Loader;

typedef struct {
Time start;
Time totalMeasuredTime;
Time largestMeasuredTime;
int timeSamples;
} WCETSampler;


int* dac = (int *)0x4000741C;

///////////////////////////////////////////////////////////
void reader(App*, int);
void receiver(App*, int);

Serial sci0 = initSerial(SCI_PORT0, &app, reader);
void tick(ToneGenerator*, int);
void upVolume(ToneGenerator*, int);
void downVolume(ToneGenerator*, int);
void mute(ToneGenerator*, int);
void enableDeadlineTG(ToneGenerator*, int);
void lockRequest(ToneGenerator*, int);
int checkMuted(ToneGenerator*, int);

void backgroundLoop(Loader*, int);
void increaseLoad(Loader*, int);
void decreaseLoad(Loader*, int);
void enableDeadlineLD(Loader*, int);

void wcetBegin(WCETSampler*);
void wcetEnd(WCETSampler*);

///////////////////////////////////////////////////////////
App app = { initObject(), 0, 'X' };
Serial sci0 = initSerial(SCI_PORT0, &app, reader);
Semaphore muteVolumeSem = initSemaphore(1); // lock the tg when is muted
Can can0 = initCan(CAN_PORT0, &app, receiver);
ToneGenerator tg = {initObject(),initCallBlock(), 500, true, 5, FALSE, USEC(100)}; // 500 USEC 650USEC 931USEC
Loader ld = {initObject(), 1000, USEC(1300)};
WCETSampler wcettg = {0,0,0,0};
WCETSampler wcetld = {0,0,0,0};

///////////////////////////////////////////////////////////
// app
void receiver(App *self, int unused) {
CANMsg msg;
CAN_RECEIVE(&can0, &msg);
Expand All @@ -27,33 +94,212 @@ void receiver(App *self, int unused) {
}

void reader(App *self, int c) {

SCI_WRITE(&sci0, "Rcv: \'");
SCI_WRITECHAR(&sci0, c);
SCI_WRITE(&sci0, "\'\n");
switch (c)
{
case 30: //up
ASYNC(&tg, lockRequest, (int)upVolume);
break;
case 31: //down
ASYNC(&tg, lockRequest, (int)downVolume);
break;
case 28: // decrease process load
ASYNC(&ld, decreaseLoad, 0);
break;
case 29: // increase process load
ASYNC(&ld, increaseLoad, 0);
break;
case 'M': // mute/unmute
if (SYNC(&tg, checkMuted, 0)) // sycn will return a value
{
ASYNC(&tg, lockRequest, (int)mute);
} else {
ASYNC(&tg, mute, 0);
}

break;
case 'D': // deadline enable/disable
ASYNC(&tg, enableDeadlineTG, 0);
ASYNC(&ld, enableDeadlineLD, 0);
break;

default:
break;
}
}

void startApp(App *self, int arg) {
CANMsg msg;

CAN_INIT(&can0);
SCI_INIT(&sci0);
SCI_INIT(&sci0); // ?
SCI_WRITE(&sci0, "Hello, hello...\n");
ASYNC(&tg, tick, 0); // follow correct paradigm
// ASYNC(&ld, backgroundLoop, 0); //

}

///////////////////////////////////////////////////////////
// tone generator
void tick(ToneGenerator *self, int c) {
wcetBegin(&wcettg);
for (size_t i = 0; i < 100; i++)
{
if (self->lh)
{
*dac = self->volume;
self->lh = false;
} else {
*dac = 0;
self->lh = true;
}
}


wcetEnd(&wcettg);
SEND(USEC(self->period), self->deadline, self, tick, c); // step 2


}

void upVolume(ToneGenerator *self, int c) {
char tempBuffer[50];
if (self->volume < 25)
{
self->volume++;
}
sprintf(tempBuffer, "volume: %d\n", self->volume);
SCI_WRITE(&sci0, tempBuffer);
ASYNC(&muteVolumeSem, Signal, 0);
}

void downVolume(ToneGenerator *self, int c) {
char tempBuffer[50];
if (self->volume > 1)
{
self->volume--;
}
sprintf(tempBuffer, "volume: %d\n", self->volume);
SCI_WRITE(&sci0, tempBuffer);
ASYNC(&muteVolumeSem, Signal, 0);
}

void mute(ToneGenerator *self, int c) {
if(!self->mute) {
self->mute = self->volume;
self->volume = 0;
SCI_WRITE(&sci0, "muted\n");
} else {
self->volume = self->mute;
self->mute = FALSE;
SCI_WRITE(&sci0, "unmuted\n");
ASYNC(&muteVolumeSem, Signal, 0); // realse lock
}

}

void enableDeadlineTG(ToneGenerator *self, int c) {
if (self->deadline == 0)
{
self->deadline = USEC(100);
SCI_WRITE(&sci0, "enable deadline\n");
} else {
self->deadline = 0;
SCI_WRITE(&sci0, "disable deadline\n");
}

}

void lockRequest(ToneGenerator* self, int c) {
self->callBlock.obj = self;
self->callBlock.meth = (Method)c;
ASYNC(&muteVolumeSem, Wait, (int)&self->callBlock);
}


int checkMuted(ToneGenerator* self, int c) {
if (!self->mute)
{
return TRUE;
} else {
return FALSE;
}
}

///////////////////////////////////////////////////////////
// loader
void backgroundLoop(Loader* self, int c) {
// wcetBegin(&wcetld);
for (size_t i = 0; i < self->background_loop_range; i++)
{

}
// wcetEnd(&wcetld);
SEND(USEC(1300),self->deadline, self, backgroundLoop, c); // step 2
}

void increaseLoad(Loader* self, int c) {
char tempBuffer[50];
self->background_loop_range += 500;
sprintf(tempBuffer, "background loop range: %d\n", self->background_loop_range);
SCI_WRITE(&sci0, tempBuffer);
}

void decreaseLoad(Loader* self, int c) {
char tempBuffer[50];
if (self->background_loop_range > 0)
{
self->background_loop_range -= 500;
}
sprintf(tempBuffer, "background loop range: %d\n", self->background_loop_range);
SCI_WRITE(&sci0, tempBuffer);

}

void enableDeadlineLD(Loader *self, int c) {
if (self->deadline == 0)
{
self->deadline = USEC(1300);
} else {
self->deadline = 0;
}
}

///////////////////////////////////////////////////////////
void wcetBegin(WCETSampler* self) {
self->start = CURRENT_OFFSET();
}

void wcetEnd(WCETSampler* self) {
Time end = CURRENT_OFFSET();
if(self->timeSamples < 500) {
self->timeSamples += 1;

Time elapsed = end - self->start;
self->totalMeasuredTime += elapsed;
if(elapsed > self->largestMeasuredTime) {
self->largestMeasuredTime = elapsed;
}
} else {
if (self->timeSamples == 500) {
const int STRLEN = 100;
char s[STRLEN];

long worst = USEC_OF(self->largestMeasuredTime);
long average = USEC_OF(self->totalMeasuredTime) / self->timeSamples;

snprintf(s, STRLEN, "Worst: %ld\nAverage: %ld\nSamples: %d", worst, average, self->timeSamples);
SCI_WRITE(&sci0, s);

msg.msgId = 1;
msg.nodeId = 1;
msg.length = 6;
msg.buff[0] = 'H';
msg.buff[1] = 'e';
msg.buff[2] = 'l';
msg.buff[3] = 'l';
msg.buff[4] = 'o';
msg.buff[5] = 0;
CAN_SEND(&can0, &msg);
self->timeSamples += 1;
}
}
}

///////////////////////////////////////////////////////////
int main() {
INSTALL(&sci0, sci_interrupt, SCI_IRQ0);
INSTALL(&can0, can_interrupt, CAN_IRQ0);
TINYTIMBER(&app, startApp, 0);
return 0;
}
Loading