diff --git a/belastingProject.syscfg b/belastingProject.syscfg index c87a831..64af893 100644 --- a/belastingProject.syscfg +++ b/belastingProject.syscfg @@ -1,65 +1,82 @@ -/** - * 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. - * @cliArgs --board "/ti/boards/CC3220S_LAUNCHXL" --product "simplelink_cc32xx_sdk@4.30.00.06" - * @versions {"data":"2020090214","timestamp":"2020090214","tool":"1.6.0+1543","templates":"2020090214"} - */ - -/** - * Import the modules used in this configuration. - */ -const DriverLib = scripting.addModule("/ti/devices/DriverLib"); -const Board = scripting.addModule("/ti/drivers/Board"); -const DMA = scripting.addModule("/ti/drivers/DMA"); -const GPIO = scripting.addModule("/ti/drivers/GPIO", {}, false); -const GPIO1 = GPIO.addInstance(); -const I2C = scripting.addModule("/ti/drivers/I2C", {}, false); -const I2C1 = I2C.addInstance(); -const PWM = scripting.addModule("/ti/drivers/PWM", {}, false); -const PWM1 = PWM.addInstance(); -const Power = scripting.addModule("/ti/drivers/Power"); -const SPI = scripting.addModule("/ti/drivers/SPI"); -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"; -GPIO1.interruptTrigger = "Falling Edge"; -GPIO1.pull = "Pull Up"; -GPIO1.mode = "Dynamic"; -GPIO1.callbackFunction = "noodstop_noodstopISR"; - -I2C1.$name = "CONFIG_I2C_0"; -I2C1.$hardware = system.deviceData.board.components.LP_I2C; -I2C1.i2c.sdaPin.$assign = "boosterpack.10"; -I2C1.i2c.sclPin.$assign = "boosterpack.9"; - -PWM1.$name = "CONFIG_PWM_0"; -PWM1.$hardware = system.deviceData.board.components.LED_RED; - -Power.ioRetentionShutdown = ["GRP_1"]; -Power.parkPins.$name = "ti_drivers_power_PowerCC32XXPins0"; - -SPI1.$name = "CONFIG_SPI_MASTER"; - -Watchdog1.$name = "CONFIG_WATCHDOG_0"; -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.4"; -I2C1.i2c.$suggestSolution = "I2C0"; -PWM1.timer.$suggestSolution = "Timer2"; -PWM1.timer.pwmPin.$suggestSolution = "boosterpack.29"; -SPI1.spi.$suggestSolution = "SPI0"; -SPI1.spi.sclkPin.$suggestSolution = "boosterpack.7"; -SPI1.spi.misoPin.$suggestSolution = "boosterpack.14"; -SPI1.spi.mosiPin.$suggestSolution = "boosterpack.15"; -SPI1.spi.dmaRxChannel.$suggestSolution = "UDMA_CH30"; -SPI1.spi.dmaTxChannel.$suggestSolution = "UDMA_CH31"; +/** + * 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. + * @cliArgs --board "/ti/boards/CC3220S_LAUNCHXL" --product "simplelink_cc32xx_sdk@4_20_00_07" + * @versions {"data":"2020052512","timestamp":"2020052512","tool":"1.5.0+1397","templates":"2020052512"} + */ + +/** + * Import the modules used in this configuration. + */ +const DriverLib = scripting.addModule("/ti/devices/DriverLib"); +const Board = scripting.addModule("/ti/drivers/Board"); +const DMA = scripting.addModule("/ti/drivers/DMA"); +const GPIO = scripting.addModule("/ti/drivers/GPIO", {}, false); +const GPIO1 = GPIO.addInstance(); +const GPIO2 = GPIO.addInstance(); +const GPIO3 = GPIO.addInstance(); +const GPIO4 = GPIO.addInstance(); +const GPIO5 = GPIO.addInstance(); +const PWM = scripting.addModule("/ti/drivers/PWM", {}, false); +const PWM1 = PWM.addInstance(); +const Power = scripting.addModule("/ti/drivers/Power"); +const SPI = scripting.addModule("/ti/drivers/SPI"); +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"; +GPIO1.interruptTrigger = "Falling Edge"; +GPIO1.pull = "Pull Up"; +GPIO1.mode = "Dynamic"; +GPIO1.callbackFunction = "noodstopISR"; + +GPIO2.$name = "CONFIG_GPIO_INIT"; +GPIO2.pull = "Pull Down"; +GPIO2.callbackFunction = "initISR"; +GPIO2.interruptTrigger = "Rising Edge"; +GPIO2.gpioPin.$assign = "boosterpack.7"; + +GPIO3.$name = "CONFIG_GPIO_START"; +GPIO3.callbackFunction = "startSysISR"; +GPIO3.interruptTrigger = "Both Edges"; +GPIO3.gpioPin.$assign = "boosterpack.4"; + +GPIO4.$name = "CONFIG_GPIO_CSMSP"; +GPIO4.mode = "Output"; +GPIO4.initialOutputState = "High"; + +GPIO5.$name = "CONFIG_GPIO_CSADC"; +GPIO5.mode = "Output"; +GPIO5.initialOutputState = "High"; + +PWM1.$name = "CONFIG_PWM_0"; +PWM1.$hardware = system.deviceData.board.components.LED_RED; + +Power.ioRetentionShutdown = ["GRP_1"]; +Power.parkPins.$name = "ti_drivers_power_PowerCC32XXPins0"; + +SPI1.$name = "CONFIG_SPI_MASTER"; + +Watchdog1.$name = "CONFIG_WATCHDOG_0"; +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"; diff --git a/pinout.md b/pinout.md new file mode 100644 index 0000000..861be9d --- /dev/null +++ b/pinout.md @@ -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 | diff --git a/src/MPPT.c b/src/MPPT.c index b6980c1..622a614 100644 --- a/src/MPPT.c +++ b/src/MPPT.c @@ -1,64 +1,77 @@ -#include -#include - -#include "global.h" -#include "MPPT.h" - -#define PWM_MIN 0.001 -#define PWM_MAX 0.9 - -uint8_t mppt_setpoint, mppt_setpointOverrite; -uint8_t *mppt_setpointP; - -PWM_Handle mppt_pwm; - -void mppt_setSetpointOverride(uint8_t vermogen){ - mppt_setpointOverrite = vermogen; - mppt_setpointP = &mppt_setpointOverrite; -} -void mppt_setSetpoint(uint8_t vermogen){ - mppt_setpoint = vermogen; -} - -void mppt_init(){ - PWM_init(); - - PWM_Params params; - PWM_Params_init(¶ms); - params.dutyUnits = PWM_DUTY_FRACTION; - params.dutyValue = 0; - params.periodUnits = PWM_PERIOD_US; - params.periodValue = 0; - mppt_pwm = PWM_open(CONFIG_PWM_0, ¶ms); - if (mppt_pwm == NULL) { - while (1); - } - - mppt_setpointP = &mppt_setpoint; -} - - -void mppt_setPWM(double d){ - if(d < PWM_MIN){ - PWM_setDuty(mppt_pwm, 0); - return; - } - if(d > PWM_MAX){ - d = 0.9; - } - PWM_setDuty(mppt_pwm, (uint32_t) ((double) PWM_DUTY_FRACTION_MAX * d)); -} - -void mppt_start(){ - PWM_start(mppt_pwm); - - double duty = 0, step = 0.01; - while(Status == WORKING){ - mppt_setPWM(duty); - duty += step; - if(duty > 1){ - step = -step; - duty += step; - } - } -} \ No newline at end of file +#include +#include + +#include "global.h" +#include "MPPT.h" +#include "spi.h" + +#define PWM_MIN 0.001 +#define PWM_MAX 0.9 + +uint8_t mppt_setpoint, mppt_setpointOverrite, mppt_vermogen; +uint8_t *mppt_setpointP; + +PWM_Handle mppt_pwm; + +void mppt_setSetpointOverride(uint8_t vermogen){ + mppt_setpointOverrite = vermogen; + mppt_setpointP = &mppt_setpointOverrite; +} +void mppt_setSetpoint(uint8_t vermogen){ + mppt_setpoint = vermogen; +} +uint8_t mppt_getVermogen(){ + return mppt_vermogen; +} + +void mppt_meetVermogen(){ + uint16_t spanning = ADC_read(ADC_CH2); + uint16_t curent = ADC_read(ADC_CH3); + //TODO: add real formula for spanning and curent + mppt_vermogen = (spanning * curent) / 100; +} + +void mppt_init(){ + PWM_init(); + + PWM_Params params; + PWM_Params_init(¶ms); + params.dutyUnits = PWM_DUTY_FRACTION; + params.dutyValue = 0; + params.periodUnits = PWM_PERIOD_US; + params.periodValue = 10; // 100 kHz + mppt_pwm = PWM_open(CONFIG_PWM_0, ¶ms); + if (mppt_pwm == NULL) { + while (1); + } + + mppt_setpointP = &mppt_setpoint; + mppt_start(); +} + +void mppt_setPWM(double d){ + if(d < PWM_MIN){ + PWM_setDuty(mppt_pwm, 0); + return; + } + if(d > PWM_MAX){ + d = PWM_MAX; + } + PWM_setDuty(mppt_pwm, (uint32_t) ((double) PWM_DUTY_FRACTION_MAX * d)); +} + +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); + } +} diff --git a/src/MPPT.h b/src/MPPT.h index 076b13d..a58c07f 100644 --- a/src/MPPT.h +++ b/src/MPPT.h @@ -1,11 +1,11 @@ -#ifndef MPPT_H -#define MPPT_H - -#include - -void mppt_init(); -void mppt_start(); -void mppt_setSetpointOverride(uint8_t vermogen); -void mppt_setSetpoint(uint8_t vermogen); - -#endif \ No newline at end of file +#ifndef MPPT_H +#define MPPT_H + +#include + +void mppt_init(); +void mppt_start(); +void mppt_setSetpointOverride(uint8_t vermogen); +void mppt_setSetpoint(uint8_t vermogen); + +#endif diff --git a/src/communicatieBeheer.c b/src/communicatie.c similarity index 56% rename from src/communicatieBeheer.c rename to src/communicatie.c index 1cce1c4..45eb798 100644 --- a/src/communicatieBeheer.c +++ b/src/communicatie.c @@ -1,93 +1,88 @@ -#include "global.h" -#include "communicatie.h" - -#define SPI_PACKET_LENGTH 4 - - -unsigned char ReciveBuffer[SPI_PACKET_LENGTH]; -unsigned char TransmitBuffer[SPI_PACKET_LENGTH]; - -uint16_t comm_snelheid; - -typedef enum { - SPIPARM_maxVermogen, - SPIPARM_maxSnelheid, - SPIPARM_maxTempratuur, - SPIPARM_setpoint, - SPIPARM_vermogen, - SPIPARM_snelheid, - SPIPARM_tempratuur, - SPIPARM_count -} SPI_Register_t; - -// communicatie spi Taak -void comm_spi(){ - // Initialiseer SPI - ISP_init(); - SPI_Params spiParams; - SPI_Params_init(&spiParams); - spiParams.frameFormat = SPI_POL0_PHA0; // mode0 - spiParams.bitRate = 1E6; // 1 MHz - SPI_Handle masterSpi = SPI_open(CONFIG_SPI_MASTER, &spiParams); - if (masterSpi == NULL) { - // Display_printf(display, 0, 0, "Error initializing master SPI\n"); - while (1); - } else { - // Display_printf(display, 0, 0, "Master SPI initialized\n"); - } - - // create transaction - SPI_Transaction spiTransaction; - spiTransaction.count = SPI_PACKET_LENGTH; - spiTransaction.txBuf = (void *) TransmitBuffer; - spiTransaction.rxBuf = (void *) ReciveBuffer; - - // start the loop - TransmitBuffer[0] = 0; - while(1){ - // zero the buffers - memset((void *) TransmitBuffer+1, 0, SPI_PACKET_LENGTH-1); - memset((void *) ReciveBuffer, 0, SPI_PACKET_LENGTH); - - // set data in the transmit buffer - ReciveBuffer[0] = TransmitBuffer[0]; - switch(TransmitBuffer[0]){ - case SPIPARM_vermogen: - TransmitBuffer[1] = mppt_getVermogen(); - break; - case SPIPARM_snelheid: - TransmitBuffer[1] = comm_snelheid; - break; - case SPIPARM_tempratuur: - TransmitBuffer[1] = noodstop_getTemptatuur(); - break; - } - - // do the transaction - //TODO: set the CS - SPI_transaction(masterSpi, &spiTransaction); - - // read the data out the recive buffer - switch (ReciveBuffer[0]){ - case SPIPARM_maxVermogen: - noodstop_setMaxVermogen(ReciveBuffer[2]); - break; - case SPIPARM_maxSnelheid: - noodstop_setMaxSnelheid(ReciveBuffer[2] + ReciveBuffer[3]*0x100); - break; - case SPIPARM_maxTempratuur: - noodstop_setMaxTemptratuur(ReciveBuffer[2]); - break; - case SPIPARM_setpoint: - mppt_setSsetpoint(ReciveBuffer[2]); - break; - } - - // increment and loop parameter - TransmitBuffer[0]++; - if(TransmitBuffer[0] == SPIPARM_count) - TransmitBuffer[0] = 0; - - usleep(5000); - } -} +#include + +#include "debug.h" + +#include "global.h" +#include "communicatie.h" +#include "spi.h" + +#define SPI_PACKET_LENGTH 4 + +SPI_Trans_t SPI_MSPTrans; +unsigned char ReciveBuffer[SPI_PACKET_LENGTH]; +unsigned char TransmitBuffer[SPI_PACKET_LENGTH]; + +uint16_t comm_snelheid; + +typedef enum { + SPIPARM_maxVermogen, + SPIPARM_maxSnelheid, + SPIPARM_maxTempratuur, + SPIPARM_setpoint, + SPIPARM_vermogen, + SPIPARM_snelheid, + SPIPARM_tempratuur, + SPIPARM_count +} SPI_Register_t; + +#define SPI_INTERVAL (1/100) / SPIPARM_count // update all params 100 time a second + +// communicatie spi Taak +void comm_init(){ + // create SPI transaction + SPI_Params_init(&SPI_MSPTrans.params); + SPI_MSPTrans.interface = CONFIG_SPI_MASTER; + SPI_MSPTrans.cs = CONFIG_GPIO_CSMSP; + SPI_ADC.params.frameFormat = SPI_POL0_PHA0; // mode 0 + SPI_MSPTrans.params.bitRate = 1E6; // 1 MHz + SPI_MSPTrans.trans.count = SPI_PACKET_LENGTH; + SPI_MSPTrans.trans.txBuf = &TransmitBuffer[0]; + SPI_MSPTrans.trans.rxBuf = &ReciveBuffer[0]; + + // start the loop + TransmitBuffer[0] = 0; + while(1){ + // zero the buffers + memset(&TransmitBuffer[0] + 1, 0, SPI_PACKET_LENGTH-1); + // memset(&ReciveBuffer[0], 0, SPI_PACKET_LENGTH); + + // set data in the transmit buffer + switch(TransmitBuffer[0]){ + case SPIPARM_vermogen: + TransmitBuffer[1] = mppt_getVermogen(); + break; + case SPIPARM_snelheid: + TransmitBuffer[1] = comm_snelheid; + break; + case SPIPARM_tempratuur: + TransmitBuffer[1] = noodstop_getTempratuur(); + break; + } + + // do the transaction + SPI_trans(&SPI_MSPTrans); + + // read the data out the receive buffer + switch (ReciveBuffer[0]){ + case SPIPARM_maxVermogen: + noodstop_setMaxVermogen(ReciveBuffer[2]); + break; + case SPIPARM_maxSnelheid: + noodstop_setMaxSnelheid(ReciveBuffer[2] + ReciveBuffer[3]*0x100); // littel endian + break; + case SPIPARM_maxTempratuur: + noodstop_setMaxTemptratuur(ReciveBuffer[2]); + break; + case SPIPARM_setpoint: + mppt_setSetpoint(ReciveBuffer[2]); + break; + } + + // increment and loop parameter + TransmitBuffer[0]++; + if(TransmitBuffer[0] == SPIPARM_count) + TransmitBuffer[0] = 0; + + usleep(SPI_INTERVAL); + } +} diff --git a/src/communicatie.h b/src/communicatie.h index 20ef385..897a346 100644 --- a/src/communicatie.h +++ b/src/communicatie.h @@ -1,70 +1,67 @@ -#ifndef COMMUNICATIE_H -#define COMMUNICATIE_H - -#include - -// enummeration for packet IDs -typedef enum { - PACKET_UPDATEBELASTING, - PACKET_NOODSTOP, - PACKET_INITPARAMS -} packetID_t; - -// packet structures -typedef struct { - packetID_t id; - bool overheeat; - bool overload; - bool overspeed; -} noodstop; - -typedef struct { - packetID_t id; - uint8_t maxVermogen; - uint16_t maxSnelhied; - uint8_t maxTemptratuur; - uint8_t vermogenSetpoint; -} initBelasting; - -typedef struct { - packetID_t id; - uint8_t maxVermogen; - uint16_t maxSnelhied; - uint8_t maxTemptratuur; -} maxWaardesBelasting; - -typedef struct { - packetID_t id; - uint8_t rendement; - uint16_t snelheid; - uint16_t stroom; - uint16_t spanning; -} dataBelasting; - -typedef struct { - packetID_t id; - uint16_t snelheidSetpoint; - uint16_t maxSpanning; - uint16_t maxStroom; -} initAandrijving; - -typedef struct { - packetID_t id; - uint8_t rendement; - uint8_t vermogen; - uint16_t stroom; - uint16_t spanning; -} dataAandrijving; - -union { - initAandrijving; - initBelasting; - maxWaardesBelasting; - dataBelasting; - initAandrijving; - dataAandrijving; -} I2CPacket_t; - -void comm_spi(); - -#endif +#ifndef COMMUNICATIE_H +#define COMMUNICATIE_H + +//// enummeration for packet IDs +//typedef enum { +// PACKET_UPDATEBELASTING, +// PACKET_NOODSTOP, +// PACKET_INITPARAMS +//} packetID_t; +// +//// packet structures +//typedef struct packet_0 { +// packetID_t id; +// bool overheeat; +// bool overload; +// bool overspeed; +//} noodstop; +// +//typedef struct packet_1 { +// packetID_t id; +// uint8_t maxVermogen; +// uint16_t maxSnelhied; +// uint8_t maxTemptratuur; +// uint8_t vermogenSetpoint; +//} initBelasting; +// +//typedef struct packet_2 { +// packetID_t id; +// uint8_t maxVermogen; +// uint16_t maxSnelhied; +// uint8_t maxTemptratuur; +//} maxWaardesBelasting; +// +//typedef struct packet_3 { +// packetID_t id; +// uint8_t rendement; +// uint16_t snelheid; +// uint16_t stroom; +// uint16_t spanning; +//} dataBelasting; +// +//typedef struct packet_4 { +// packetID_t id; +// uint16_t snelheidSetpoint; +// uint16_t maxSpanning; +// uint16_t maxStroom; +//} initAandrijving; +// +//typedef struct packet_5 { +// packetID_t id; +// uint8_t rendement; +// uint8_t vermogen; +// uint16_t stroom; +// uint16_t spanning; +//} dataAandrijving; +// +//typedef union I2CPacket_t { +// initAandrijving initA; +// initBelasting initB; +// maxWaardesBelasting maxB; +// dataAandrijving dataA; +// dataBelasting. dataB; +//} I2CPacket_t; + +void comm_init(); + +#endif diff --git a/src/debug.h b/src/debug.h new file mode 100644 index 0000000..d1d4ce8 --- /dev/null +++ b/src/debug.h @@ -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 diff --git a/src/global.h b/src/global.h index a4e8062..2c210a6 100644 --- a/src/global.h +++ b/src/global.h @@ -1,22 +1,24 @@ -#ifndef GLOBALS_H -#define GLOBALS_H - -#include -#include "ti_drivers_config.h" - -enum { - SLEEP, - INIT, - MPPT_READY, - NOODSTOP_READY, - ALL_READY, - WORKING, - OVERHEAD, - OVERLOAD, - OVERSPEED, - EXT_NOODSTOP -} Status; - -pthread_t createSimplePTread(int prio, void * fn); - -#endif +#ifndef GLOBALS_H +#define GLOBALS_H + +#include +#include "ti_drivers_config.h" + +typedef enum { + SLEEP, + INIT, + MPPT_READY, + NOODSTOP_READY, + ALL_READY, + WORKING, + OVERHEAD, + OVERLOAD, + OVERSPEED, + EXT_NOODSTOP +} Status_t; + +Status_t Status; + +pthread_t createSimplePTread(int prio, void * fn); + +#endif diff --git a/src/main.c b/src/main.c index 36c70ac..0594e0d 100644 --- a/src/main.c +++ b/src/main.c @@ -1,44 +1,115 @@ -#include -#include -#include - -#include "global.h" -#include "communicatie.h" -#include "noodstop.h" -#include "MPPT.h" - -#define ERROR(msg) return; //TODO: create error handeler - -//TODO: maybe detach pthread? -//TODO: add stacksize option -pthread_t createSimplePTread(int prio, void * fn){ - struct sched_param priParam; - priParam.sched_priority = prio; - - pthread_attr_t pAttrs; - pthread_attr_init(&pAttrs); - pthread_attr_setschedparam(&pAttrs, &priParam); - - pthread_t thread; - if( pthread_create(&thread, &pAttrs, fn, NULL) != 0 ){ - ERROR("could not create pthread") - } - - return thread; -} - -void startInit(){ - createSimplePTread(1, &comm_spi); - createSimplePTread(1, &noodstop_init); - createSimplePTread(1, &mppt_init); - //TODO: add systeembeheer -} - -int main(void) -{ - Board_init(); // initilaze board - - //TODO: add logic - - BIOS_start(); // start the BIOS -} +#include +#include +#include +#include + +#include "debug.h" + +#include "global.h" +#include "communicatie.h" +#include "noodstop.h" +#include "MPPT.h" +#include "spi.h" + +struct mainTreads { + pthread_t comm; + pthread_t noodstop; + pthread_t mppt; + pthread_t sysBeheer; +} treads; + +void * mainTask(void *arg); + +//TODO: maybe detach pthread? +//TODO: add stacksize option +pthread_t createSimplePTread(int prio, void * fn){ + struct sched_param priParam; + priParam.sched_priority = prio; + + pthread_attr_t pAttrs; + pthread_attr_init(&pAttrs); + pthread_attr_setschedparam(&pAttrs, &priParam); + + pthread_t thread; + if( pthread_create(&thread, &pAttrs, fn, NULL) != 0 ){ + ERROR("could not create thread") + return 0; + } + + return thread; +} + +void startInit(){ + 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); +} diff --git a/src/noodstop.c b/src/noodstop.c index 51be83c..892b847 100644 --- a/src/noodstop.c +++ b/src/noodstop.c @@ -1,132 +1,133 @@ -#include -#include -#include -#include - -#include "global.h" -#include "communicatie.h" -#include "MPPT.h" - -struct { - uint8_t maxTempratuur; - uint8_t maxVermogen; - uint16_t maxSnelheid; -} nood_maxwaardes; - -extern pthread_t createSimplePTread(int prio, void * fn); - -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 noodstop_activeerNoodstop(){ - // 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){ - // controleer snelheid - if(snelhied > nood_maxwaardes.maxSnelheid){ - // PANIC!! - mppt_setSetpointOverride(nood_maxwaardes.maxVermogen); - - // Wacht 0.5 seconde - usleep(500E3); - - // activeer noostop - Status = OVERSPEED; - noodstop_activeerNoodstop(); - } -} - -void noodstop_tempratuurHandle(){ - uint8_t tempratuur; - do{ - // wacht 0.1 seconde - usleep(100E3); - - // lees ADC uit - //TODO: do it! - - // bereken temperatuur - //?? Yeti wat is die formule? - tempratuur = ~0; - - // controleer temperatuur - }while(tempratuur < nood_maxwaardes.maxTempratuur); - - // vermogen override = 0W - mppt_vermogenOverride(0); - - // activeer noostop - Status = OVERHEAD; - noodstop_activeerNoodstop(); -} - -void noodstop_noodstopISR(){ - // Wacht 0.5 seconde - usleep(500000); - - // activeer noostop - Status = EXT_NOODSTOP; - noodstop_activeerNoodstop(); -} - -void noodstop_init(){ - // ADC init - //TODO: do it! - - // GPIO init - GPIO_setConfig(CONF_GPIO_NOODSTOP, GPIO_CFG_IN_PU); //TODO: check config - - memset(&nood_maxwaardes, 0xff, sizeof(nood_maxwaardes)); // set all max value to invalid ones - - // 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){ - Status = ALL_READY; - }else{ - ERROR("invalid Status"); - } - - // wacht tot status == werken - while(Status != WORKING){ - usleep(1E3); - } - - // start the treath voor het uitlezen van de temptatuur - pthread_t adcThread = createSimplePTread(1, &noodstop_tempratuurHandle); - - // 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 -} +#include +#include + +#include "debug.h" + +#include "global.h" +#include "communicatie.h" +#include "MPPT.h" +#include "spi.h" + +struct { + uint8_t maxTempratuur; + uint8_t maxVermogen; + uint16_t maxSnelheid; +} nood_maxwaardes; +pthread_t adcThread; +uint8_t temp; + +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 noodstop_activeerNoodstop(){ + // zet noodstop GPIO als uitgang en laag + GPIO_setConfig(CONF_GPIO_NOODSTOP, GPIO_CFG_OUT_LOW); //TODO: check config +} + +void noodstop_snelhied(uint16_t snelhied){ + // controleer snelheid + if(snelhied > nood_maxwaardes.maxSnelheid){ + // PANIC!! + mppt_setSetpointOverride(nood_maxwaardes.maxVermogen); + + // Wacht 0.5 seconde + usleep(500E3); + + // activeer noostop + Status = OVERSPEED; + noodstop_activeerNoodstop(); + } +} + +uint8_t noodstop_getTempratuur(){ + return temp; +} + +uint8_t noodstop_readTemp(uint8_t ch){ + uint16_t v = ADC_read(&SPI_ADC); + + //TODO: add real fomula + + return v >> 2; +} + +void noodstop_tempratuurHandle(){ + uint8_t tempB; + do{ + // wacht 0.1 seconde + usleep(100E3); + + // read tempretures + temp = noodstop_readTemp(ADC_CH0); + tempB = noodstop_readTemp(ADC_CH1); + if(temp < tempB) + temp = tempB; + + // controleer temperatuur + }while(temp < nood_maxwaardes.maxTempratuur); + + // vermogen override = 0W + mppt_setSetpointOverride(0); + + // activeer noostop + Status = OVERHEAD; + noodstop_activeerNoodstop(); +} + +/** noodstopISR() + * ISR for the external emergency stop from the button or other devides + * The ISR is created in the syscfg. + */ +void noodstopISR(){ + // Wacht 0.5 seconde + usleep(500E3); + + // activeer noostop + Status = EXT_NOODSTOP; + noodstop_activeerNoodstop(); +} + +void noodstop_init(){ + // GPIO init + GPIO_setConfig(CONF_GPIO_NOODSTOP, GPIO_CFG_IN_PU); //TODO: check config + + memset(&nood_maxwaardes, 0xff, sizeof(nood_maxwaardes)); // set all max value to invalid ones + + // 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){ + Status = ALL_READY; + }else{ + ERROR("invalid Status"); + } +} + +void noodstop_start(){ + // start the treath voor het uitlezen van de temptatuur + adcThread = createSimplePTread(2, &noodstop_tempratuurHandle); +} + +void noodstop_deinit(){ + // stop threads + pthread_exit(adcThread); +} diff --git a/src/noodstop.h b/src/noodstop.h index e75a13e..8b20de4 100644 --- a/src/noodstop.h +++ b/src/noodstop.h @@ -1,13 +1,18 @@ -#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 +#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(); +void noodstop_start(); +void noodstop_deinit(); + +uint8_t noodstop_getTempratuur(); +void noodstop_activeerNoodstop(); + +#endif diff --git a/src/spi.c b/src/spi.c new file mode 100644 index 0000000..15fd90d --- /dev/null +++ b/src/spi.c @@ -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; +} diff --git a/src/spi.h b/src/spi.h new file mode 100644 index 0000000..6280747 --- /dev/null +++ b/src/spi.h @@ -0,0 +1,36 @@ +#include +#include + +#include + +#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);