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": []
}
]
}
}
222 changes: 206 additions & 16 deletions application.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,75 @@
#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;

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);

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)};

// app
void receiver(App *self, int unused) {
CANMsg msg;
CAN_RECEIVE(&can0, &msg);
Expand All @@ -27,33 +80,170 @@ 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) {
if (self->lh)
{
*dac = self->volume;
self->lh = false;
} else {
*dac = 0;
self->lh = true;
}
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);
SCI_WRITE(&sci0, "lock\n");
}


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

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

}

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);

}

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);
void enableDeadlineLD(Loader *self, int c) {
if (self->deadline == 0)
{
self->deadline = USEC(1300);
} else {
self->deadline = 0;
}
}

int main() {
INSTALL(&sci0, sci_interrupt, SCI_IRQ0);
INSTALL(&can0, can_interrupt, CAN_IRQ0);
TINYTIMBER(&app, startApp, 0);
return 0;
}
41 changes: 41 additions & 0 deletions semaphore.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "TinyTimber.h"
#include "semaphore.h"

void c_enqueue(Caller c, Caller *queue) {
Caller prev = NULL, q = *queue;
while (q) { // find last element in queue
prev = q;
q = q->next;
}
if (prev == NULL)
*queue = c; // empty queue: put ‘c’ first
else
prev->next = c; // non-empty queue: put ‘c’ last
c->next = NULL;
}

Caller c_dequeue(Caller *queue) {
Caller c = *queue;
if (c)
*queue = c->next; // remove first element in queue
return c;
}

void Wait(Semaphore *self, int c) {
Caller wakeup = (Caller) c; // type-cast back from ‘int’
if (self->value > 0) {
self->value--;
ASYNC(wakeup->obj, wakeup->meth, 0);
}
else
c_enqueue(wakeup, &self->queue);
}

void Signal(Semaphore *self, int unused) {
if (self->queue) {
Caller wakeup = c_dequeue(&self->queue);
ASYNC(wakeup->obj, wakeup->meth, 0);
}
else
self->value++;
}
28 changes: 28 additions & 0 deletions semaphore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef _SEMAPHORE_H
#define _SEMAPHORE_H

#include "TinyTimber.h"

struct call_block;
typedef struct call_block *Caller;

typedef struct call_block {
Caller next; // for use in linked lists
Object *obj;
Method meth;
} CallBlock;

#define initCallBlock() { 0, 0, 0 }

typedef struct {
Object super;
int value;
Caller queue;
} Semaphore;

void Wait(Semaphore*, int);
void Signal(Semaphore*, int);

#define initSemaphore(n) { initObject(), n, 0 }

#endif