big update

This commit is contained in:
Mats van Reenen 2021-01-21 21:56:54 +01:00
parent dfd9b3ba2f
commit 7769a07681
13 changed files with 745 additions and 514 deletions

View File

@ -1,65 +1,82 @@
/** /**
* These arguments were used when this file was generated. They will be automatically applied on subsequent loads * These arguments were used when this file was generated. They will be automatically applied on subsequent loads
* via the GUI or CLI. Run CLI with '--help' for additional information on how to override these arguments. * via the GUI or CLI. Run CLI with '--help' for additional information on how to override these arguments.
* @cliArgs --board "/ti/boards/CC3220S_LAUNCHXL" --product "simplelink_cc32xx_sdk@4.30.00.06" * @cliArgs --board "/ti/boards/CC3220S_LAUNCHXL" --product "simplelink_cc32xx_sdk@4_20_00_07"
* @versions {"data":"2020090214","timestamp":"2020090214","tool":"1.6.0+1543","templates":"2020090214"} * @versions {"data":"2020052512","timestamp":"2020052512","tool":"1.5.0+1397","templates":"2020052512"}
*/ */
/** /**
* Import the modules used in this configuration. * Import the modules used in this configuration.
*/ */
const DriverLib = scripting.addModule("/ti/devices/DriverLib"); const DriverLib = scripting.addModule("/ti/devices/DriverLib");
const Board = scripting.addModule("/ti/drivers/Board"); const Board = scripting.addModule("/ti/drivers/Board");
const DMA = scripting.addModule("/ti/drivers/DMA"); const DMA = scripting.addModule("/ti/drivers/DMA");
const GPIO = scripting.addModule("/ti/drivers/GPIO", {}, false); const GPIO = scripting.addModule("/ti/drivers/GPIO", {}, false);
const GPIO1 = GPIO.addInstance(); const GPIO1 = GPIO.addInstance();
const I2C = scripting.addModule("/ti/drivers/I2C", {}, false); const GPIO2 = GPIO.addInstance();
const I2C1 = I2C.addInstance(); const GPIO3 = GPIO.addInstance();
const PWM = scripting.addModule("/ti/drivers/PWM", {}, false); const GPIO4 = GPIO.addInstance();
const PWM1 = PWM.addInstance(); const GPIO5 = GPIO.addInstance();
const Power = scripting.addModule("/ti/drivers/Power"); const PWM = scripting.addModule("/ti/drivers/PWM", {}, false);
const SPI = scripting.addModule("/ti/drivers/SPI"); const PWM1 = PWM.addInstance();
const SPI1 = SPI.addInstance(); const Power = scripting.addModule("/ti/drivers/Power");
const Watchdog = scripting.addModule("/ti/drivers/Watchdog"); const SPI = scripting.addModule("/ti/drivers/SPI");
const Watchdog1 = Watchdog.addInstance(); const SPI1 = SPI.addInstance();
const Watchdog = scripting.addModule("/ti/drivers/Watchdog");
/** const Watchdog1 = Watchdog.addInstance();
* Write custom configuration values to the imported modules.
*/ /**
GPIO1.$name = "CONF_GPIO_NOODSTOP"; * Write custom configuration values to the imported modules.
GPIO1.interruptTrigger = "Falling Edge"; */
GPIO1.pull = "Pull Up"; GPIO1.$name = "CONF_GPIO_NOODSTOP";
GPIO1.mode = "Dynamic"; GPIO1.interruptTrigger = "Falling Edge";
GPIO1.callbackFunction = "noodstop_noodstopISR"; GPIO1.pull = "Pull Up";
GPIO1.mode = "Dynamic";
I2C1.$name = "CONFIG_I2C_0"; GPIO1.callbackFunction = "noodstopISR";
I2C1.$hardware = system.deviceData.board.components.LP_I2C;
I2C1.i2c.sdaPin.$assign = "boosterpack.10"; GPIO2.$name = "CONFIG_GPIO_INIT";
I2C1.i2c.sclPin.$assign = "boosterpack.9"; GPIO2.pull = "Pull Down";
GPIO2.callbackFunction = "initISR";
PWM1.$name = "CONFIG_PWM_0"; GPIO2.interruptTrigger = "Rising Edge";
PWM1.$hardware = system.deviceData.board.components.LED_RED; GPIO2.gpioPin.$assign = "boosterpack.7";
Power.ioRetentionShutdown = ["GRP_1"]; GPIO3.$name = "CONFIG_GPIO_START";
Power.parkPins.$name = "ti_drivers_power_PowerCC32XXPins0"; GPIO3.callbackFunction = "startSysISR";
GPIO3.interruptTrigger = "Both Edges";
SPI1.$name = "CONFIG_SPI_MASTER"; GPIO3.gpioPin.$assign = "boosterpack.4";
Watchdog1.$name = "CONFIG_WATCHDOG_0"; GPIO4.$name = "CONFIG_GPIO_CSMSP";
Watchdog1.watchdog.$assign = "WATCHDOG0"; GPIO4.mode = "Output";
GPIO4.initialOutputState = "High";
/**
* Pinmux solution for unlocked pins/peripherals. This ensures that minor changes to the automatic solver in a future GPIO5.$name = "CONFIG_GPIO_CSADC";
* version of the tool will not impact the pinmux you originally saw. These lines can be completely deleted in order to GPIO5.mode = "Output";
* re-solve from scratch. GPIO5.initialOutputState = "High";
*/
GPIO1.gpioPin.$suggestSolution = "boosterpack.4"; PWM1.$name = "CONFIG_PWM_0";
I2C1.i2c.$suggestSolution = "I2C0"; PWM1.$hardware = system.deviceData.board.components.LED_RED;
PWM1.timer.$suggestSolution = "Timer2";
PWM1.timer.pwmPin.$suggestSolution = "boosterpack.29"; Power.ioRetentionShutdown = ["GRP_1"];
SPI1.spi.$suggestSolution = "SPI0"; Power.parkPins.$name = "ti_drivers_power_PowerCC32XXPins0";
SPI1.spi.sclkPin.$suggestSolution = "boosterpack.7";
SPI1.spi.misoPin.$suggestSolution = "boosterpack.14"; SPI1.$name = "CONFIG_SPI_MASTER";
SPI1.spi.mosiPin.$suggestSolution = "boosterpack.15";
SPI1.spi.dmaRxChannel.$suggestSolution = "UDMA_CH30"; Watchdog1.$name = "CONFIG_WATCHDOG_0";
SPI1.spi.dmaTxChannel.$suggestSolution = "UDMA_CH31"; Watchdog1.watchdog.$assign = "WATCHDOG0";
/**
* Pinmux solution for unlocked pins/peripherals. This ensures that minor changes to the automatic solver in a future
* version of the tool will not impact the pinmux you originally saw. These lines can be completely deleted in order to
* re-solve from scratch.
*/
GPIO1.gpioPin.$suggestSolution = "boosterpack.18";
GPIO4.gpioPin.$suggestSolution = "boosterpack.19";
GPIO5.gpioPin.$suggestSolution = "boosterpack.13";
PWM1.timer.$suggestSolution = "Timer2";
PWM1.timer.pwmPin.$suggestSolution = "boosterpack.29";
SPI1.spi.$suggestSolution = "SPI0";
SPI1.spi.sclkPin.$suggestSolution = "boosterpack.17";
SPI1.spi.misoPin.$suggestSolution = "boosterpack.14";
SPI1.spi.mosiPin.$suggestSolution = "boosterpack.15";
SPI1.spi.dmaRxChannel.$suggestSolution = "UDMA_CH30";
SPI1.spi.dmaTxChannel.$suggestSolution = "UDMA_CH31";

25
pinout.md Normal file
View File

@ -0,0 +1,25 @@
# pinout
## GPIO
| lable | pin |
|------------|----:|
| start init | P05 |
| start sys | P03 |
| noodstop | P08 |
## SPI
| lable | pin |
|--------|----:|
| CSK | P45 |
| MISO | P06 |
| MOSI | P07 |
| CS MSP | P18 |
| CS ADC | P21 |
## Other
| lable | pin |
|-------|----:|
| PWM | P64 |

View File

@ -1,64 +1,77 @@
#include <stdint.h> #include <stdint.h>
#include <ti/drivers/PWM.h> #include <ti/drivers/PWM.h>
#include "global.h" #include "global.h"
#include "MPPT.h" #include "MPPT.h"
#include "spi.h"
#define PWM_MIN 0.001
#define PWM_MAX 0.9 #define PWM_MIN 0.001
#define PWM_MAX 0.9
uint8_t mppt_setpoint, mppt_setpointOverrite;
uint8_t *mppt_setpointP; uint8_t mppt_setpoint, mppt_setpointOverrite, mppt_vermogen;
uint8_t *mppt_setpointP;
PWM_Handle mppt_pwm;
PWM_Handle mppt_pwm;
void mppt_setSetpointOverride(uint8_t vermogen){
mppt_setpointOverrite = vermogen; void mppt_setSetpointOverride(uint8_t vermogen){
mppt_setpointP = &mppt_setpointOverrite; mppt_setpointOverrite = vermogen;
} mppt_setpointP = &mppt_setpointOverrite;
void mppt_setSetpoint(uint8_t vermogen){ }
mppt_setpoint = vermogen; void mppt_setSetpoint(uint8_t vermogen){
} mppt_setpoint = vermogen;
}
void mppt_init(){ uint8_t mppt_getVermogen(){
PWM_init(); return mppt_vermogen;
}
PWM_Params params;
PWM_Params_init(&params); void mppt_meetVermogen(){
params.dutyUnits = PWM_DUTY_FRACTION; uint16_t spanning = ADC_read(ADC_CH2);
params.dutyValue = 0; uint16_t curent = ADC_read(ADC_CH3);
params.periodUnits = PWM_PERIOD_US; //TODO: add real formula for spanning and curent
params.periodValue = 0; mppt_vermogen = (spanning * curent) / 100;
mppt_pwm = PWM_open(CONFIG_PWM_0, &params); }
if (mppt_pwm == NULL) {
while (1); void mppt_init(){
} PWM_init();
mppt_setpointP = &mppt_setpoint; PWM_Params params;
} PWM_Params_init(&params);
params.dutyUnits = PWM_DUTY_FRACTION;
params.dutyValue = 0;
void mppt_setPWM(double d){ params.periodUnits = PWM_PERIOD_US;
if(d < PWM_MIN){ params.periodValue = 10; // 100 kHz
PWM_setDuty(mppt_pwm, 0); mppt_pwm = PWM_open(CONFIG_PWM_0, &params);
return; if (mppt_pwm == NULL) {
} while (1);
if(d > PWM_MAX){ }
d = 0.9;
} mppt_setpointP = &mppt_setpoint;
PWM_setDuty(mppt_pwm, (uint32_t) ((double) PWM_DUTY_FRACTION_MAX * d)); mppt_start();
} }
void mppt_start(){ void mppt_setPWM(double d){
PWM_start(mppt_pwm); if(d < PWM_MIN){
PWM_setDuty(mppt_pwm, 0);
double duty = 0, step = 0.01; return;
while(Status == WORKING){ }
mppt_setPWM(duty); if(d > PWM_MAX){
duty += step; d = PWM_MAX;
if(duty > 1){ }
step = -step; PWM_setDuty(mppt_pwm, (uint32_t) ((double) PWM_DUTY_FRACTION_MAX * d));
duty += step; }
}
} void mppt_start(){
} PWM_start(mppt_pwm);
//TODO: implement the mppt algaritme
double duty = 0, step = 0.01;
while(1){ //(Status == WORKING){
mppt_setPWM(duty);
duty += step;
if(duty > 1 || duty < 0){
step = -step;
duty += step;
}
usleep(100);
}
}

View File

@ -1,11 +1,11 @@
#ifndef MPPT_H #ifndef MPPT_H
#define MPPT_H #define MPPT_H
#include <stdint.h> #include <stdint.h>
void mppt_init(); void mppt_init();
void mppt_start(); void mppt_start();
void mppt_setSetpointOverride(uint8_t vermogen); void mppt_setSetpointOverride(uint8_t vermogen);
void mppt_setSetpoint(uint8_t vermogen); void mppt_setSetpoint(uint8_t vermogen);
#endif #endif

View File

@ -1,93 +1,88 @@
#include "global.h" #include <ti/drivers/SPI.h>
#include "communicatie.h"
#include "debug.h"
#define SPI_PACKET_LENGTH 4
#include "global.h"
#include "communicatie.h"
unsigned char ReciveBuffer[SPI_PACKET_LENGTH]; #include "spi.h"
unsigned char TransmitBuffer[SPI_PACKET_LENGTH];
#define SPI_PACKET_LENGTH 4
uint16_t comm_snelheid;
SPI_Trans_t SPI_MSPTrans;
typedef enum { unsigned char ReciveBuffer[SPI_PACKET_LENGTH];
SPIPARM_maxVermogen, unsigned char TransmitBuffer[SPI_PACKET_LENGTH];
SPIPARM_maxSnelheid,
SPIPARM_maxTempratuur, uint16_t comm_snelheid;
SPIPARM_setpoint,
SPIPARM_vermogen, typedef enum {
SPIPARM_snelheid, SPIPARM_maxVermogen,
SPIPARM_tempratuur, SPIPARM_maxSnelheid,
SPIPARM_count SPIPARM_maxTempratuur,
} SPI_Register_t; SPIPARM_setpoint,
SPIPARM_vermogen,
// communicatie spi Taak SPIPARM_snelheid,
void comm_spi(){ SPIPARM_tempratuur,
// Initialiseer SPI SPIPARM_count
ISP_init(); } SPI_Register_t;
SPI_Params spiParams;
SPI_Params_init(&spiParams); #define SPI_INTERVAL (1/100) / SPIPARM_count // update all params 100 time a second
spiParams.frameFormat = SPI_POL0_PHA0; // mode0
spiParams.bitRate = 1E6; // 1 MHz // communicatie spi Taak
SPI_Handle masterSpi = SPI_open(CONFIG_SPI_MASTER, &spiParams); void comm_init(){
if (masterSpi == NULL) { // create SPI transaction
// Display_printf(display, 0, 0, "Error initializing master SPI\n"); SPI_Params_init(&SPI_MSPTrans.params);
while (1); SPI_MSPTrans.interface = CONFIG_SPI_MASTER;
} else { SPI_MSPTrans.cs = CONFIG_GPIO_CSMSP;
// Display_printf(display, 0, 0, "Master SPI initialized\n"); SPI_ADC.params.frameFormat = SPI_POL0_PHA0; // mode 0
} SPI_MSPTrans.params.bitRate = 1E6; // 1 MHz
SPI_MSPTrans.trans.count = SPI_PACKET_LENGTH;
// create transaction SPI_MSPTrans.trans.txBuf = &TransmitBuffer[0];
SPI_Transaction spiTransaction; SPI_MSPTrans.trans.rxBuf = &ReciveBuffer[0];
spiTransaction.count = SPI_PACKET_LENGTH;
spiTransaction.txBuf = (void *) TransmitBuffer; // start the loop
spiTransaction.rxBuf = (void *) ReciveBuffer; TransmitBuffer[0] = 0;
while(1){
// start the loop // zero the buffers
TransmitBuffer[0] = 0; memset(&TransmitBuffer[0] + 1, 0, SPI_PACKET_LENGTH-1);
while(1){ // memset(&ReciveBuffer[0], 0, SPI_PACKET_LENGTH);
// zero the buffers
memset((void *) TransmitBuffer+1, 0, SPI_PACKET_LENGTH-1); // set data in the transmit buffer
memset((void *) ReciveBuffer, 0, SPI_PACKET_LENGTH); switch(TransmitBuffer[0]){
case SPIPARM_vermogen:
// set data in the transmit buffer TransmitBuffer[1] = mppt_getVermogen();
ReciveBuffer[0] = TransmitBuffer[0]; break;
switch(TransmitBuffer[0]){ case SPIPARM_snelheid:
case SPIPARM_vermogen: TransmitBuffer[1] = comm_snelheid;
TransmitBuffer[1] = mppt_getVermogen(); break;
break; case SPIPARM_tempratuur:
case SPIPARM_snelheid: TransmitBuffer[1] = noodstop_getTempratuur();
TransmitBuffer[1] = comm_snelheid; break;
break; }
case SPIPARM_tempratuur:
TransmitBuffer[1] = noodstop_getTemptatuur(); // do the transaction
break; SPI_trans(&SPI_MSPTrans);
}
// read the data out the receive buffer
// do the transaction switch (ReciveBuffer[0]){
//TODO: set the CS case SPIPARM_maxVermogen:
SPI_transaction(masterSpi, &spiTransaction); noodstop_setMaxVermogen(ReciveBuffer[2]);
break;
// read the data out the recive buffer case SPIPARM_maxSnelheid:
switch (ReciveBuffer[0]){ noodstop_setMaxSnelheid(ReciveBuffer[2] + ReciveBuffer[3]*0x100); // littel endian
case SPIPARM_maxVermogen: break;
noodstop_setMaxVermogen(ReciveBuffer[2]); case SPIPARM_maxTempratuur:
break; noodstop_setMaxTemptratuur(ReciveBuffer[2]);
case SPIPARM_maxSnelheid: break;
noodstop_setMaxSnelheid(ReciveBuffer[2] + ReciveBuffer[3]*0x100); case SPIPARM_setpoint:
break; mppt_setSetpoint(ReciveBuffer[2]);
case SPIPARM_maxTempratuur: break;
noodstop_setMaxTemptratuur(ReciveBuffer[2]); }
break;
case SPIPARM_setpoint: // increment and loop parameter
mppt_setSsetpoint(ReciveBuffer[2]); TransmitBuffer[0]++;
break; if(TransmitBuffer[0] == SPIPARM_count)
} TransmitBuffer[0] = 0;
// increment and loop parameter usleep(SPI_INTERVAL);
TransmitBuffer[0]++; }
if(TransmitBuffer[0] == SPIPARM_count) }
TransmitBuffer[0] = 0;
usleep(5000);
}
}

View File

@ -1,70 +1,67 @@
#ifndef COMMUNICATIE_H #ifndef COMMUNICATIE_H
#define COMMUNICATIE_H #define COMMUNICATIE_H
#include <ti/drivers/SPI.h> //// enummeration for packet IDs
//typedef enum {
// enummeration for packet IDs // PACKET_UPDATEBELASTING,
typedef enum { // PACKET_NOODSTOP,
PACKET_UPDATEBELASTING, // PACKET_INITPARAMS
PACKET_NOODSTOP, //} packetID_t;
PACKET_INITPARAMS //
} packetID_t; //// packet structures
//typedef struct packet_0 {
// packet structures // packetID_t id;
typedef struct { // bool overheeat;
packetID_t id; // bool overload;
bool overheeat; // bool overspeed;
bool overload; //} noodstop;
bool overspeed; //
} noodstop; //typedef struct packet_1 {
// packetID_t id;
typedef struct { // uint8_t maxVermogen;
packetID_t id; // uint16_t maxSnelhied;
uint8_t maxVermogen; // uint8_t maxTemptratuur;
uint16_t maxSnelhied; // uint8_t vermogenSetpoint;
uint8_t maxTemptratuur; //} initBelasting;
uint8_t vermogenSetpoint; //
} initBelasting; //typedef struct packet_2 {
// packetID_t id;
typedef struct { // uint8_t maxVermogen;
packetID_t id; // uint16_t maxSnelhied;
uint8_t maxVermogen; // uint8_t maxTemptratuur;
uint16_t maxSnelhied; //} maxWaardesBelasting;
uint8_t maxTemptratuur; //
} maxWaardesBelasting; //typedef struct packet_3 {
// packetID_t id;
typedef struct { // uint8_t rendement;
packetID_t id; // uint16_t snelheid;
uint8_t rendement; // uint16_t stroom;
uint16_t snelheid; // uint16_t spanning;
uint16_t stroom; //} dataBelasting;
uint16_t spanning; //
} dataBelasting; //typedef struct packet_4 {
// packetID_t id;
typedef struct { // uint16_t snelheidSetpoint;
packetID_t id; // uint16_t maxSpanning;
uint16_t snelheidSetpoint; // uint16_t maxStroom;
uint16_t maxSpanning; //} initAandrijving;
uint16_t maxStroom; //
} initAandrijving; //typedef struct packet_5 {
// packetID_t id;
typedef struct { // uint8_t rendement;
packetID_t id; // uint8_t vermogen;
uint8_t rendement; // uint16_t stroom;
uint8_t vermogen; // uint16_t spanning;
uint16_t stroom; //} dataAandrijving;
uint16_t spanning; //
} dataAandrijving; //typedef union I2CPacket_t {
// initAandrijving initA;
union { // initBelasting initB;
initAandrijving; // maxWaardesBelasting maxB;
initBelasting; // dataAandrijving dataA;
maxWaardesBelasting; // dataBelasting. dataB;
dataBelasting; //} I2CPacket_t;
initAandrijving;
dataAandrijving; void comm_init();
} I2CPacket_t;
#endif
void comm_spi();
#endif

11
src/debug.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef DEBUG_H
#define DEBUG_H
#define LOG(msg) ;
#define ERROR(msg) LOG("ERROR: " msg)
#define WARN(msg) LOG("WARN: " msg)
#define DEBUG(msg) LOG("DEBUG: " msg)
#define INFO(msg) LOG("INFO: " msg)
#endif

View File

@ -1,22 +1,24 @@
#ifndef GLOBALS_H #ifndef GLOBALS_H
#define GLOBALS_H #define GLOBALS_H
#include <pthread.h> #include <pthread.h>
#include "ti_drivers_config.h" #include "ti_drivers_config.h"
enum { typedef enum {
SLEEP, SLEEP,
INIT, INIT,
MPPT_READY, MPPT_READY,
NOODSTOP_READY, NOODSTOP_READY,
ALL_READY, ALL_READY,
WORKING, WORKING,
OVERHEAD, OVERHEAD,
OVERLOAD, OVERLOAD,
OVERSPEED, OVERSPEED,
EXT_NOODSTOP EXT_NOODSTOP
} Status; } Status_t;
pthread_t createSimplePTread(int prio, void * fn); Status_t Status;
#endif pthread_t createSimplePTread(int prio, void * fn);
#endif

View File

@ -1,44 +1,115 @@
#include <stdint.h> #include <stdint.h>
#include <pthread.h> #include <pthread.h>
#include <ti/sysbios/BIOS.h> #include <ti/drivers/GPIO.h>
#include <ti/sysbios/BIOS.h>
#include "global.h"
#include "communicatie.h" #include "debug.h"
#include "noodstop.h"
#include "MPPT.h" #include "global.h"
#include "communicatie.h"
#define ERROR(msg) return; //TODO: create error handeler #include "noodstop.h"
#include "MPPT.h"
//TODO: maybe detach pthread? #include "spi.h"
//TODO: add stacksize option
pthread_t createSimplePTread(int prio, void * fn){ struct mainTreads {
struct sched_param priParam; pthread_t comm;
priParam.sched_priority = prio; pthread_t noodstop;
pthread_t mppt;
pthread_attr_t pAttrs; pthread_t sysBeheer;
pthread_attr_init(&pAttrs); } treads;
pthread_attr_setschedparam(&pAttrs, &priParam);
void * mainTask(void *arg);
pthread_t thread;
if( pthread_create(&thread, &pAttrs, fn, NULL) != 0 ){ //TODO: maybe detach pthread?
ERROR("could not create pthread") //TODO: add stacksize option
} pthread_t createSimplePTread(int prio, void * fn){
struct sched_param priParam;
return thread; priParam.sched_priority = prio;
}
pthread_attr_t pAttrs;
void startInit(){ pthread_attr_init(&pAttrs);
createSimplePTread(1, &comm_spi); pthread_attr_setschedparam(&pAttrs, &priParam);
createSimplePTread(1, &noodstop_init);
createSimplePTread(1, &mppt_init); pthread_t thread;
//TODO: add systeembeheer if( pthread_create(&thread, &pAttrs, fn, NULL) != 0 ){
} ERROR("could not create thread")
return 0;
int main(void) }
{
Board_init(); // initilaze board return thread;
}
//TODO: add logic
void startInit(){
BIOS_start(); // start the BIOS SPI_Init();
} treads.comm = createSimplePTread(3, &comm_init);
treads.noodstop = createSimplePTread(1, &noodstop_init);
treads.mppt = createSimplePTread(1, &mppt_init);
//TODO: add systeembeheer
}
void startSys(){
treads.mppt = createSimplePTread(2, &mppt_start);
treads.noodstop = createSimplePTread(4, &noodstop_start);
}
void stopSys(){
// pthread_exit(treads.comm);
pthread_exit(treads.noodstop);
pthread_exit(treads.mppt);
treads.noodstop = createSimplePTread(3, &noodstop_deinit);
}
void initISR(){
if(Status == WORKING || Status == INIT)
return;
Status = INIT;
treads.sysBeheer = createSimplePTread(1, mainTask);
}
void startSysISR(){
if(GPIO_read(CONFIG_GPIO_START)){
if(Status != ALL_READY){
Status = EXT_NOODSTOP;
noodstop_activeerNoodstop();
stopSys();
return;
}else{
Status = WORKING;
}
}else{
Status = SLEEP;
}
}
void * mainTask(void *arg){
Status_t lastState;
while(1){
switch (Status){
case SLEEP:
stopSys();
break;
case INIT:
startInit();
break;
case WORKING:
startSys();
break;
case OVERHEAD:
case OVERLOAD:
case OVERSPEED:
case EXT_NOODSTOP:
stopSys();
return;
}
lastState = Status;
while(Status == lastState)
usleep(10);
}
}
int main(void){
Status = SLEEP;
Board_init(); // initialise board
treads.sysBeheer = createSimplePTread(1, mainTask);
BIOS_start(); // start the BIOS
while(1)
usleep(1E6);
}

View File

@ -1,132 +1,133 @@
#include <ti/drivers/GPIO.h> #include <pthread.h>
#include <ti/drivers/ADC.h> #include <stdint.h>
#include <ti/drivers/SPI.h>
#include <pthread.h> #include "debug.h"
#include "global.h" #include "global.h"
#include "communicatie.h" #include "communicatie.h"
#include "MPPT.h" #include "MPPT.h"
#include "spi.h"
struct {
uint8_t maxTempratuur; struct {
uint8_t maxVermogen; uint8_t maxTempratuur;
uint16_t maxSnelheid; uint8_t maxVermogen;
} nood_maxwaardes; uint16_t maxSnelheid;
} nood_maxwaardes;
extern pthread_t createSimplePTread(int prio, void * fn); pthread_t adcThread;
uint8_t temp;
void noodstop_setMaxVermogen(uint8_t value){
nood_maxwaardes.maxVermogen = value; void noodstop_setMaxVermogen(uint8_t value){
} nood_maxwaardes.maxVermogen = value;
void noodstop_setMaxSnelheid(uint16_t value){ }
nood_maxwaardes.maxSnelheid = value; void noodstop_setMaxSnelheid(uint16_t value){
} nood_maxwaardes.maxSnelheid = value;
void noodstop_setMaxTemptratuur(uint8_t value){ }
nood_maxwaardes.maxTempratuur = value; void noodstop_setMaxTemptratuur(uint8_t value){
} nood_maxwaardes.maxTempratuur = value;
}
void noodstop_activeerNoodstop(){
// zet noodstop GPIO als uitgang en laag void noodstop_activeerNoodstop(){
GPIO_setConfig(CONF_GPIO_NOODSTOP, GPIO_CFG_OUT_LOW); //TODO: check config // zet noodstop GPIO als uitgang en laag
GPIO_setConfig(CONF_GPIO_NOODSTOP, GPIO_CFG_OUT_LOW); //TODO: check config
// set status }
}
void noodstop_snelhied(uint16_t snelhied){
void noodstop_snelhied(uint16_t snelhied){ // controleer snelheid
// controleer snelheid if(snelhied > nood_maxwaardes.maxSnelheid){
if(snelhied > nood_maxwaardes.maxSnelheid){ // PANIC!!
// PANIC!! mppt_setSetpointOverride(nood_maxwaardes.maxVermogen);
mppt_setSetpointOverride(nood_maxwaardes.maxVermogen);
// Wacht 0.5 seconde
// Wacht 0.5 seconde usleep(500E3);
usleep(500E3);
// activeer noostop
// activeer noostop Status = OVERSPEED;
Status = OVERSPEED; noodstop_activeerNoodstop();
noodstop_activeerNoodstop(); }
} }
}
uint8_t noodstop_getTempratuur(){
void noodstop_tempratuurHandle(){ return temp;
uint8_t tempratuur; }
do{
// wacht 0.1 seconde uint8_t noodstop_readTemp(uint8_t ch){
usleep(100E3); uint16_t v = ADC_read(&SPI_ADC);
// lees ADC uit //TODO: add real fomula
//TODO: do it!
return v >> 2;
// bereken temperatuur }
//?? Yeti wat is die formule?
tempratuur = ~0; void noodstop_tempratuurHandle(){
uint8_t tempB;
// controleer temperatuur do{
}while(tempratuur < nood_maxwaardes.maxTempratuur); // wacht 0.1 seconde
usleep(100E3);
// vermogen override = 0W
mppt_vermogenOverride(0); // read tempretures
temp = noodstop_readTemp(ADC_CH0);
// activeer noostop tempB = noodstop_readTemp(ADC_CH1);
Status = OVERHEAD; if(temp < tempB)
noodstop_activeerNoodstop(); temp = tempB;
}
// controleer temperatuur
void noodstop_noodstopISR(){ }while(temp < nood_maxwaardes.maxTempratuur);
// Wacht 0.5 seconde
usleep(500000); // vermogen override = 0W
mppt_setSetpointOverride(0);
// activeer noostop
Status = EXT_NOODSTOP; // activeer noostop
noodstop_activeerNoodstop(); Status = OVERHEAD;
} noodstop_activeerNoodstop();
}
void noodstop_init(){
// ADC init /** noodstopISR()
//TODO: do it! * ISR for the external emergency stop from the button or other devides
* The ISR is created in the syscfg.
// GPIO init */
GPIO_setConfig(CONF_GPIO_NOODSTOP, GPIO_CFG_IN_PU); //TODO: check config void noodstopISR(){
// Wacht 0.5 seconde
memset(&nood_maxwaardes, 0xff, sizeof(nood_maxwaardes)); // set all max value to invalid ones usleep(500E3);
// controleer max waardes // activeer noostop
while(1){ Status = EXT_NOODSTOP;
if( noodstop_activeerNoodstop();
nood_maxwaardes.maxSnelheid <= 60000 }
&& nood_maxwaardes.maxTempratuur >= 60 && nood_maxwaardes.maxTempratuur <= 120
&& nood_maxwaardes.maxVermogen <= 250 void noodstop_init(){
){ // GPIO init
break; // stop the loop values are valid GPIO_setConfig(CONF_GPIO_NOODSTOP, GPIO_CFG_IN_PU); //TODO: check config
}
usleep(100E3); memset(&nood_maxwaardes, 0xff, sizeof(nood_maxwaardes)); // set all max value to invalid ones
}
// controleer max waardes
// Update status while(1){
if(Status == INIT){ if(
Status = NOODSTOP_READY; nood_maxwaardes.maxSnelheid <= 60000
}else if(Status == MPPT_READY){ && nood_maxwaardes.maxTempratuur >= 60 && nood_maxwaardes.maxTempratuur <= 120
Status = ALL_READY; && nood_maxwaardes.maxVermogen <= 250
}else{ ){
ERROR("invalid Status"); break; // stop the loop values are valid
} }
usleep(100E3);
// wacht tot status == werken }
while(Status != WORKING){
usleep(1E3); // Update status
} if(Status == INIT){
Status = NOODSTOP_READY;
// start the treath voor het uitlezen van de temptatuur }else if(Status == MPPT_READY){
pthread_t adcThread = createSimplePTread(1, &noodstop_tempratuurHandle); Status = ALL_READY;
}else{
// Maak interrupt voor noodstop signaal ERROR("invalid Status");
//TODO: do it! }
}
// wait until it stops
while(Status = WORKING){ void noodstop_start(){
usleep(5E3); // start the treath voor het uitlezen van de temptatuur
} adcThread = createSimplePTread(2, &noodstop_tempratuurHandle);
}
// stop threadhs
Task_delete(adcThread); void noodstop_deinit(){
//TODO: deinit ADC // stop threads
} pthread_exit(adcThread);
}

View File

@ -1,13 +1,18 @@
#ifndef NOODSTOP_H #ifndef NOODSTOP_H
#define NOODSTOP_H #define NOODSTOP_H
#include <stdint.h> #include <stdint.h>
void noodstop_setMaxVermogen(uint8_t value); void noodstop_setMaxVermogen(uint8_t value);
void noodstop_setMaxSnelheid(uint16_t value); void noodstop_setMaxSnelheid(uint16_t value);
void noodstop_setMaxTemptratuur(uint8_t value); void noodstop_setMaxTemptratuur(uint8_t value);
void noodstop_snelhied(uint16_t snelhied);
void noodstop_snelhied(uint16_t snelhied);
void noodstop_init(); void noodstop_init();
void noodstop_start();
#endif void noodstop_deinit();
uint8_t noodstop_getTempratuur();
void noodstop_activeerNoodstop();
#endif

58
src/spi.c Normal file
View File

@ -0,0 +1,58 @@
#include "spi.h"
#include "global.h"
#include "debug.h"
uint8_t ADC_TxBuf[4];
uint8_t ADC_RxBuf[4];
void SPI_Init(){
SPI_init();
// init ADC
SPI_Params_init(&SPI_ADC.params);
SPI_ADC.interface = CONFIG_SPI_MASTER;
SPI_ADC.cs = CONFIG_GPIO_CSMSP;
SPI_ADC.params.frameFormat = SPI_POL0_PHA0; // mode 0
SPI_ADC.params.bitRate = 1E6; // 1 MHz
SPI_ADC.trans.count = 4;
SPI_ADC.trans.txBuf = &ADC_TxBuf;
SPI_ADC.trans.rxBuf = &ADC_RxBuf;
ADC_TxBuf[0] = 1; // set start bit
}
uint16_t ADC_read(uint8_t ch){
while(SPI_Handaler != NULL)
usleep(10);
ADC_TxBuf[1] = ch; // set channel
SPI_trans(&SPI_ADC);
return (ADC_TxBuf[2] & 0b11) * 0x100 | ADC_TxBuf[3];
}
int SPI_trans(SPI_Trans_t *trans){
SPI_Handle spih;
// wait until last transation is done
while(SPI_Handaler != NULL)
usleep(10);
SPI_Handaler = &spih;
// open spi connection with paramaters of this one
spih = SPI_open(CONFIG_SPI_MASTER, &trans->params);
if (spih == NULL) {
ERROR("initialising SPI for communicating with the MSP\n");
return 1;
}
// do the transaction
GPIO_write(trans->cs, 0);
SPI_transfer(spih, &trans->trans);
GPIO_write(trans->cs, 1);
// close it so its can be used next time
SPI_close(spih);
SPI_Handaler = NULL;
return 0;
}

36
src/spi.h Normal file
View File

@ -0,0 +1,36 @@
#include <ti/drivers/SPI.h>
#include <ti/drivers/GPIO.h>
#include <stdint.h>
#define ADC_SINGELCH 1 << 7
#define ADC_CH0 ADC_SINGELCH | 0 << 4
#define ADC_CH1 ADC_SINGELCH | 1 << 4
#define ADC_CH2 ADC_SINGELCH | 2 << 4
#define ADC_CH3 ADC_SINGELCH | 3 << 4
#define ADC_CH4 ADC_SINGELCH | 4 << 4
#define ADC_CH5 ADC_SINGELCH | 5 << 4
#define ADC_CH6 ADC_SINGELCH | 6 << 4
#define ADC_CH7 ADC_SINGELCH | 7 << 4
#define ADC_CH01 0 << 4
#define ADC_CH10 1 << 4
#define ADC_CH23 2 << 4
#define ADC_CH32 3 << 4
#define ADC_CH45 4 << 4
#define ADC_CH54 5 << 4
#define ADC_CH67 6 << 4
#define ADC_CH76 7 << 4
typedef struct SPI_Trans_t {
SPI_Transaction trans;
uint_least8_t interface;
SPI_Params params;
uint_least8_t cs;
} SPI_Trans_t;
SPI_Handle *SPI_Handaler;
SPI_Trans_t SPI_ADC;
void SPI_Init();
uint16_t ADC_read(uint8_t ch);
int SPI_trans(SPI_Trans_t *trans);