diff --git a/src/communicatie.h b/src/communicatie.h index 930d374..4251e58 100644 --- a/src/communicatie.h +++ b/src/communicatie.h @@ -1,7 +1,7 @@ #ifndef COMMUNICATIE_H #define COMMUNICATIE_H -#include +#include // enummeration for packet IDs typedef enum { @@ -65,4 +65,6 @@ union { dataAandrijving; } I2CPacket_t; +void comm_spi(); + #endif \ No newline at end of file diff --git a/src/communicatieBeheer.c b/src/communicatieBeheer.c index ec7ddaa..ae1a59d 100644 --- a/src/communicatieBeheer.c +++ b/src/communicatieBeheer.c @@ -1,5 +1,3 @@ -#include - #include "communicatie.h" #define SPI_PACKET_LENGTH 4 @@ -30,7 +28,7 @@ void comm_spi(){ SPI_Params_init(&spiParams); spiParams.frameFormat = SPI_POL0_PHA0; // mode0 spiParams.bitRate = 1E6; // 1 MHz - SPI_Handle masterSpi = SPI_open(CONFIG_SPI_MASTER, &spiParams); + masterSpi = SPI_open(CONFIG_SPI_MASTER, &spiParams); if (masterSpi == NULL) { // Display_printf(display, 0, 0, "Error initializing master SPI\n"); while (1); @@ -66,6 +64,7 @@ void comm_spi(){ } // do the transaction + //TODO: set the CS SPI_transaction(masterSpi, &transaction); // read the data out the recive buffer diff --git a/src/global.h b/src/global.h index 3d7062e..d5aaf41 100644 --- a/src/global.h +++ b/src/global.h @@ -1,6 +1,8 @@ #ifndef GLOBALS_H #define GLOBALS_H +#include "ti_drivers_config.h" + enum { SLEEP, INIT, diff --git a/src/main.c b/src/main.c index 8277baa..09e9e32 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,11 @@ #include #include #include -#include "ti_drivers_config.h" + +#include "global.h" +#include "communicatie.h" +#include "noodstop.h" +#include "MPPT.h" #define ERROR(msg) return; //TODO: create error handeler @@ -23,11 +27,17 @@ pthread_t createSimplePTread(int prio, void * fn){ return thread; } +void startInit(){ + createSimplePTread(1, &comm_spi); + createSimplePTread(1, &noodstop_init); + //TODO: add systeembeheer and mppt +} + int main(void) { Board_init(); // initilaze board - + //TODO: add logic BIOS_start(); // start the BIOS } diff --git a/src/noodstop.c b/src/noodstop.c index ebc1577..e812575 100644 --- a/src/noodstop.c +++ b/src/noodstop.c @@ -1,79 +1,107 @@ -#include #include #include +#include +#include #include "global.h" #include "communicatie.h" #include "MPPT.h" -struct maxwaardes_t nood_maxwaardes; +struct { + uint8_t maxTempratuur; + uint8_t maxVermogen; + uint16_t maxSnelheid; +} nood_maxwaardes; extern pthread_t createSimplePTread(int prio, void * fn); -void nood_activeerNoodstop(){ - //BLOCK: zet noodstop GPIO als uitgang - //TODO: do it! - - //BLOCK: noodstop GPIO = laag - //TODO: do it! +void noodstop_setMaxVermogen(uint8_t value){ + nood_maxwaardes.maxVermogen = value; +} +void noodstop_setMaxSnelheid(uint16_t value){ + nood_maxwaardes.maxSnelheid = value; +} +void noodstop_setMaxTemptratuur(uint8_t value){ + nood_maxwaardes.maxTempratuur = value; } -void snelhied(uint16_t snelhied){ - //TODO: whoosh. +void noodstop_activeerNoodstop(){ + // zet noodstop GPIO als uitgang en laag + GPIO_setConfig(CONF_GPIO_NOODSTOP, GPIO_CFG_OUT_LOW); //TODO: check config - //BLOCK: controleer snelheid + // set status +} + +void noodstop_snelhied(uint16_t snelhied){ + // controleer snelheid if(snelhied > nood_maxwaardes.maxSnelheid){ - //BLOCK: vermogen overwrite = max vermogen + // PANIC!! mppt_vermogenOverride(nood_maxwaardes.maxVermogen); - //BLOCK: Wacht 0.5 seconde - usleep(500000); + // Wacht 0.5 seconde + usleep(500E3); - //BLOCK: activeer noostop - nood_activeerNoodstop(); + // activeer noostop + Status = OVERSPEED; + noodstop_activeerNoodstop(); } } -void nood_tempratuurHandle(){ +void noodstop_tempratuurHandle(){ + uint8_t tempratuur; do{ - //BLOCK: wacht 0.1 seconde - usleep(100000); + // wacht 0.1 seconde + usleep(100E3); - //BLOCK: lees ADC uit + // lees ADC uit //TODO: do it! - //BLOCK: bereken temperatuur + // bereken temperatuur //?? Yeti wat is die formule? + tempratuur = ~0; - //BLOCK: controleer temperatuur - }while(tempratuur < nood_maxwaardes.maxTemptratuur); + // controleer temperatuur + }while(tempratuur < nood_maxwaardes.maxTempratuur); - //BLOCK: vermogen override = 0W + // vermogen override = 0W mppt_vermogenOverride(0); - //BLOCK: activeer noostop - nood_activeerNoodstop(); + // activeer noostop + Status = OVERHEAD; + noodstop_activeerNoodstop(); } -void nood_noodstopISR(){ - //BLOCK: Wacht 0.5 seconde +void noodstop_noodstopISR(){ + // Wacht 0.5 seconde usleep(500000); - //BLOCK: activeer noostop - nood_activeerNoodstop(); + // activeer noostop + Status = EXT_NOODSTOP; + noodstop_activeerNoodstop(); } -void nood_init(){ - //BLOCK: ADC init +void noodstop_init(){ + // ADC init //TODO: do it! - //BLOCK: GPIO init - //TODO: do it! + // GPIO init + GPIO_setConfig(CONF_GPIO_NOODSTOP, GPIO_CFG_IN_PU); //TODO: check config - //BLOCK: wacht op Max waardes - mq_receive(MaxWaardes_mq, (char*)&nood_maxwaardes, sizeof(nood_maxwaardes), NULL); + memset(&nood_maxwaardes, 0xff, sizeof(nood_maxwaardes)); // set all max value to invalid ones - //BLOCK: Update status + // controleer max waardes + while(1){ + if( + nood_maxwaardes.maxSnelheid <= 60000 + && nood_maxwaardes.maxTempratuur >= 60 && nood_maxwaardes.maxTempratuur <= 120 + && nood_maxwaardes.maxVermogen <= 250 + ){ + break; // stop the loop values are valid + } + usleep(100E3); + } + + // Update status if(Status == INIT){ Status = NOODSTOP_READY; }else if(Status == MPPT_READY){ @@ -82,14 +110,23 @@ void nood_init(){ ERROR("invalid Status"); } - //BLOCK: wacht tot status == werken + // wacht tot status == werken while(Status != WORKING){ - usleep(1000); + usleep(1E3); } - //BLOCK: maak tread voor ADC - createSimplePTread(1, &nood_tempratuurHandle); + // start the treath voor het uitlezen van de temptatuur + pthread_t adcThread = createSimplePTread(1, &noodstop_tempratuurHandle); - //BLOCK: Maak interrupt voor noodstop signaal + // Maak interrupt voor noodstop signaal //TODO: do it! + + // wait until it stops + while(Status = WORKING){ + usleep(5E3); + } + + // stop threadhs + Task_delete(adcThread); + //TODO: deinit ADC } diff --git a/src/noodstop.h b/src/noodstop.h new file mode 100644 index 0000000..e75a13e --- /dev/null +++ b/src/noodstop.h @@ -0,0 +1,13 @@ +#ifndef NOODSTOP_H +#define NOODSTOP_H + +#include + +void noodstop_setMaxVermogen(uint8_t value); +void noodstop_setMaxSnelheid(uint16_t value); +void noodstop_setMaxTemptratuur(uint8_t value); + +void noodstop_snelhied(uint16_t snelhied); +void noodstop_init(); + +#endif \ No newline at end of file