initial, ported from constantly failed C++ variant
This commit is contained in:
60
src/PontCoopScheduler.c
Normal file
60
src/PontCoopScheduler.c
Normal file
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* PontCoopScheduler.c
|
||||
*
|
||||
* Created on: 29.08.2016
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "PontCoopScheduler.h"
|
||||
|
||||
tTask tasks[MAX_NUM_OF_TASKS];
|
||||
|
||||
|
||||
void schInit() {
|
||||
for (uint8_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
|
||||
tasks[i].delay = 0;
|
||||
tasks[i].period = 0;
|
||||
tasks[i].run = 0;
|
||||
tasks[i].exec = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void schAdd(void (*exec)(void), uint32_t delay, uint32_t period) {
|
||||
for (uint8_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
|
||||
if (tasks[i].exec == NULL) {
|
||||
tasks[i].delay = delay;
|
||||
tasks[i].period = period;
|
||||
tasks[i].run = 0;
|
||||
tasks[i].exec = exec;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void schExec() {
|
||||
for (uint8_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
|
||||
if (tasks[i].exec != NULL && tasks[i].run > 0) {
|
||||
tasks[i].run--;
|
||||
tasks[i].exec();
|
||||
if (tasks[i].period == 0) {
|
||||
tasks[i].exec = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void schUpdate() {
|
||||
for (uint8_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
|
||||
if (tasks[i].exec != NULL) {
|
||||
if (tasks[i].delay == 0) {
|
||||
tasks[i].delay = tasks[i].period;
|
||||
tasks[i].run++;
|
||||
} else {
|
||||
tasks[i].delay--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
33
src/PontCoopScheduler.h
Normal file
33
src/PontCoopScheduler.h
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* PontCoopScheduler.h
|
||||
*
|
||||
* Created on: 29.08.2016
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#ifndef PONTCOOPSCHEDULER_H_
|
||||
#define PONTCOOPSCHEDULER_H_
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
|
||||
#define MAX_NUM_OF_TASKS 3
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint32_t delay;
|
||||
uint32_t period;
|
||||
uint8_t run;
|
||||
void (*exec)(void);
|
||||
} tTask;
|
||||
|
||||
|
||||
void schInit();
|
||||
void schAdd(void (*exec)(void), uint32_t delay, uint32_t period);
|
||||
void schExec();
|
||||
void schUpdate();
|
||||
|
||||
|
||||
#endif /* PONTCOOPSCHEDULER_H_ */
|
107
src/display.c
Normal file
107
src/display.c
Normal file
@ -0,0 +1,107 @@
|
||||
/*
|
||||
* display.c
|
||||
*
|
||||
* Created on: 29.08.2016
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
|
||||
#include <msp430g2553.h>
|
||||
#include <stdint.h>
|
||||
#include <intrinsics.h>
|
||||
|
||||
#include "display.h"
|
||||
#include "gpio.h"
|
||||
#include "time.h"
|
||||
|
||||
|
||||
|
||||
const uint16_t INIT_CYCLE_DELAY = 200;
|
||||
|
||||
typedef enum {
|
||||
ALL_ID = 10,
|
||||
H_ID,
|
||||
I_ID,
|
||||
EMPTY_ID,
|
||||
} tPatternId;
|
||||
|
||||
const tPin ALL_PATTERN[] = { SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_F, SEG_G, PINS_END };
|
||||
const tPin H_PATTERN[] = { SEG_B, SEG_C, SEG_E, SEG_F, SEG_G, PINS_END };
|
||||
const tPin I_PATTERN[] = { SEG_B, SEG_C, PINS_END };
|
||||
const tPin EMPTY_PATTERN[] = { PINS_END };
|
||||
const tPin NUMBER0[] = { SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_F, PINS_END };
|
||||
const tPin NUMBER1[] = { SEG_B, SEG_C, PINS_END };
|
||||
const tPin NUMBER2[] = { SEG_A, SEG_B, SEG_G, SEG_E, SEG_D, PINS_END };
|
||||
const tPin NUMBER3[] = { SEG_A, SEG_B, SEG_G, SEG_C, SEG_D, PINS_END };
|
||||
const tPin NUMBER4[] = { SEG_F, SEG_G, SEG_B, SEG_C, PINS_END };
|
||||
const tPin NUMBER5[] = { SEG_A, SEG_F, SEG_G, SEG_C, SEG_D, PINS_END };
|
||||
const tPin NUMBER6[] = { SEG_A, SEG_C, SEG_D, SEG_E, SEG_F, SEG_G, PINS_END };
|
||||
const tPin NUMBER7[] = { SEG_A, SEG_B, SEG_C, PINS_END };
|
||||
const tPin NUMBER8[] = { SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_F, SEG_G, PINS_END };
|
||||
const tPin NUMBER9[] = { SEG_A, SEG_B, SEG_C, SEG_D, SEG_F, SEG_G, PINS_END };
|
||||
|
||||
const tPin *NUMBERS[] = { NUMBER0, NUMBER1, NUMBER2, NUMBER3, NUMBER4,
|
||||
NUMBER5, NUMBER6, NUMBER7, NUMBER8, NUMBER9, ALL_PATTERN, H_PATTERN,
|
||||
I_PATTERN, EMPTY_PATTERN };
|
||||
|
||||
const tPin DIGITS[] = { DIGIT_0, DIGIT_1, PINS_END };
|
||||
|
||||
|
||||
uint8_t digitValues[2] = { EMPTY_ID, EMPTY_ID };
|
||||
|
||||
|
||||
|
||||
void displayInit() {
|
||||
for (tPin d = DIGIT_0; d <= DIGIT_1; d++) {
|
||||
gpioSetPin(d, LOW);
|
||||
for (tPin s = SEG_A; s <= SEG_G; s++) {
|
||||
gpioSetPin(s, HIGH);
|
||||
ms_active_delay(INIT_CYCLE_DELAY);
|
||||
gpioSetPin(s, LOW);
|
||||
}
|
||||
gpioSetPin(d, HIGH);
|
||||
}
|
||||
}
|
||||
|
||||
static void showNumber(uint8_t n) {
|
||||
const tPin *pattern = NUMBERS[10];
|
||||
do {
|
||||
gpioSetPin(*pattern, LOW);
|
||||
pattern++;
|
||||
} while (*pattern != PINS_END);
|
||||
pattern = NUMBERS[n];
|
||||
do {
|
||||
gpioSetPin(*pattern, HIGH);
|
||||
pattern++;
|
||||
} while (*pattern != PINS_END);
|
||||
}
|
||||
|
||||
void displaySetValue(uint8_t v) {
|
||||
if (v >= 100) {
|
||||
digitValues[1] = H_ID;
|
||||
digitValues[0] = I_ID;
|
||||
} else {
|
||||
digitValues[1] = v / 10;
|
||||
digitValues[0] = v - digitValues[1];
|
||||
}
|
||||
}
|
||||
|
||||
void displayExec() {
|
||||
static uint8_t activeDigit = 0;
|
||||
|
||||
activeDigit++;
|
||||
if (activeDigit == 2) {
|
||||
activeDigit = 0;
|
||||
}
|
||||
|
||||
const tPin *digit = DIGITS;
|
||||
do {
|
||||
gpioSetPin(*digit, HIGH);
|
||||
digit++;
|
||||
} while (*digit != PINS_END);
|
||||
|
||||
digit = DIGITS + activeDigit;
|
||||
gpioSetPin(*digit, LOW);
|
||||
|
||||
showNumber(digitValues[activeDigit]);
|
||||
}
|
17
src/display.h
Normal file
17
src/display.h
Normal file
@ -0,0 +1,17 @@
|
||||
/*
|
||||
* display.h
|
||||
*
|
||||
* Created on: 29.08.2016
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#ifndef DISPLAY_H_
|
||||
#define DISPLAY_H_
|
||||
|
||||
|
||||
void displayInit();
|
||||
void displayExec();
|
||||
void displaySetValue(uint8_t v);
|
||||
|
||||
|
||||
#endif /* DISPLAY_H_ */
|
49
src/gpio.c
Normal file
49
src/gpio.c
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* gpio.c
|
||||
*
|
||||
* Created on: 29.08.2016
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
|
||||
#include <msp430g2553.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define GPIO_C
|
||||
#include "gpio.h"
|
||||
#undef GPIO_C
|
||||
|
||||
|
||||
|
||||
extern tPinCfg pinCfg[];
|
||||
|
||||
|
||||
void gpioInitPins() {
|
||||
for (tPin p = PINS_FIRST; p < PINS_END; p += 1) {
|
||||
tPinCfg pin = pinCfg[p];
|
||||
if (pin.portId == PORT1) {
|
||||
P1DIR |= pin.bit;
|
||||
} else if (pin.portId == PORT2) {
|
||||
P2DIR |= pin.bit;
|
||||
}
|
||||
gpioSetPin(p, pin.defaultOut);
|
||||
}
|
||||
}
|
||||
|
||||
void gpioSetPin(tPin p, tPinState v) {
|
||||
tPinCfg pin = pinCfg[p];
|
||||
if (v == HIGH) {
|
||||
if (pin.portId == PORT1) {
|
||||
P1OUT |= pin.bit;
|
||||
} else if (pin.portId == PORT2) {
|
||||
P2OUT |= pin.bit;
|
||||
}
|
||||
} else {
|
||||
if (pin.portId == PORT1) {
|
||||
P1OUT &= ~pin.bit;
|
||||
} else if (pin.portId == PORT2) {
|
||||
P2OUT &= ~pin.bit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
56
src/gpio.h
Normal file
56
src/gpio.h
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* gpio.h
|
||||
*
|
||||
* Created on: 29.08.2016
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#ifndef GPIO_H_
|
||||
#define GPIO_H_
|
||||
|
||||
|
||||
#include <msp430g2553.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
|
||||
typedef enum {
|
||||
PORT1,
|
||||
PORT2
|
||||
} tPort;
|
||||
|
||||
typedef enum {
|
||||
LOW,
|
||||
HIGH,
|
||||
} tPinState;
|
||||
|
||||
typedef struct {
|
||||
tPort portId;
|
||||
uint16_t bit;
|
||||
tPinState defaultOut;
|
||||
} tPinCfg;
|
||||
|
||||
|
||||
typedef enum {
|
||||
PINS_FIRST,
|
||||
SEG_A = PINS_FIRST,
|
||||
SEG_B,
|
||||
SEG_C,
|
||||
SEG_D,
|
||||
SEG_E,
|
||||
SEG_F,
|
||||
SEG_G,
|
||||
DIGIT_0,
|
||||
DIGIT_1,
|
||||
TESTPIN,
|
||||
PINS_END
|
||||
} tPin;
|
||||
|
||||
void gpioInitPins();
|
||||
void gpioSetPin(tPin p, tPinState v);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /* GPIO_H_ */
|
21
src/gpioCfg.c
Normal file
21
src/gpioCfg.c
Normal file
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* gpioCfg.c
|
||||
*
|
||||
* Created on: 29.08.2016
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#include "gpio.h"
|
||||
|
||||
tPinCfg pinCfg[PINS_END] = {
|
||||
{PORT1, BIT7, LOW}, //A
|
||||
{PORT1, BIT6, LOW}, //B
|
||||
{PORT2, BIT1, LOW}, //C
|
||||
{PORT2, BIT3, LOW}, //D
|
||||
{PORT2, BIT4, LOW}, //E
|
||||
{PORT2, BIT5, LOW}, //F
|
||||
{PORT2, BIT2, LOW}, //G
|
||||
{PORT2, BIT0, HIGH}, //0
|
||||
{PORT1, BIT5, HIGH}, //1
|
||||
{PORT1, BIT7, LOW}, // TESTPIN
|
||||
};
|
49
src/main.c
Normal file
49
src/main.c
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* main.c
|
||||
*
|
||||
* Created on: 29.08.2016
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#include <msp430g2553.h>
|
||||
#include <stdint.h>
|
||||
#include <intrinsics.h>
|
||||
|
||||
#include "gpio.h"
|
||||
#include "time.h"
|
||||
#include "display.h"
|
||||
#include "PontCoopScheduler.h"
|
||||
#include "testTask.h"
|
||||
|
||||
|
||||
int main() {
|
||||
WDTCTL = WDTPW | WDTHOLD;
|
||||
|
||||
// highest possible system clock
|
||||
DCOCTL = DCO0 | DCO1 | DCO2;
|
||||
BCSCTL1 = XT2OFF | RSEL0 | RSEL1 | RSEL2 | RSEL3;
|
||||
BCSCTL2 = 0;
|
||||
BCSCTL3 = 0;
|
||||
|
||||
|
||||
gpioInitPins();
|
||||
timeInit();
|
||||
schInit();
|
||||
|
||||
// interrupts are required for delay function in displayInit();
|
||||
__enable_interrupt();
|
||||
displayInit();
|
||||
__disable_interrupt();
|
||||
|
||||
testTaskInit();
|
||||
|
||||
// schAdd(displayExec, 0, 100);
|
||||
schAdd(testTaskExec, 0, 100);
|
||||
|
||||
while (1) {
|
||||
schExec();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
26
src/testTask.c
Normal file
26
src/testTask.c
Normal file
@ -0,0 +1,26 @@
|
||||
/*
|
||||
* testTask.c
|
||||
*
|
||||
* Created on: 29.08.2016
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#include "gpio.h"
|
||||
|
||||
|
||||
void testTaskInit() {
|
||||
|
||||
}
|
||||
|
||||
void testTaskExec() {
|
||||
static uint8_t toggle = 0;
|
||||
|
||||
if (toggle == 0) {
|
||||
toggle = 1;
|
||||
gpioSetPin(TESTPIN, HIGH);
|
||||
} else {
|
||||
toggle = 0;
|
||||
gpioSetPin(TESTPIN, LOW);
|
||||
}
|
||||
}
|
||||
|
15
src/testTask.h
Normal file
15
src/testTask.h
Normal file
@ -0,0 +1,15 @@
|
||||
/*
|
||||
* testTask.h
|
||||
*
|
||||
* Created on: 29.08.2016
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#ifndef TESTTASK_H_
|
||||
#define TESTTASK_H_
|
||||
|
||||
void testTaskInit();
|
||||
void testTaskExec();
|
||||
|
||||
|
||||
#endif /* TESTTASK_H_ */
|
38
src/time.c
Normal file
38
src/time.c
Normal file
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* time.c
|
||||
*
|
||||
* Created on: 20.05.2014
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#include <msp430g2553.h>
|
||||
#include <isr_compat.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "time.h"
|
||||
#include "PontCoopScheduler.h"
|
||||
|
||||
|
||||
volatile uint32_t timestamp;
|
||||
|
||||
ISR(TIMER0_A0, TA0_ISR) {
|
||||
timestamp++;
|
||||
schUpdate();
|
||||
}
|
||||
|
||||
void timeInit() {
|
||||
timestamp = 0;
|
||||
|
||||
TACCR0 = 32;
|
||||
TACCTL0 = CCIE;
|
||||
TACTL = MC_1 | ID_0 | TASSEL_1 | TACLR;
|
||||
}
|
||||
|
||||
uint32_t getMillis() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
void ms_active_delay(uint16_t delay) {
|
||||
uint32_t start = timestamp;
|
||||
while (start + delay > timestamp);
|
||||
}
|
19
src/time.h
Normal file
19
src/time.h
Normal file
@ -0,0 +1,19 @@
|
||||
/*
|
||||
* time.h
|
||||
*
|
||||
* Created on: 20.05.2014
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#ifndef TIME_H_
|
||||
#define TIME_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void timeInit();
|
||||
uint32_t getMillis();
|
||||
void ms_active_delay(uint16_t delay);
|
||||
|
||||
|
||||
|
||||
#endif /* TIME_H_ */
|
Reference in New Issue
Block a user