van i2c naar spi

This commit is contained in:
Mats van Reenen 2020-12-29 20:50:24 +01:00
parent fd389ca33b
commit bc71a50604
2 changed files with 126 additions and 134 deletions

View File

@ -11,61 +11,58 @@ typedef enum {
} packetID_t; } packetID_t;
// packet structures // packet structures
union { typedef struct {
struct { packetID_t id;
packetID_t id = PACKET_NOODSTOP; bool overheeat;
bool overheeat; bool overload;
bool overload; bool overspeed;
bool overspeed; } initAandrijving;
} initAandrijving;
struct {
packetID_t id = PACKET_INITPARAMS;
uint8_t maxVermogen;
uint16_t maxSnelhied;
uint8_t maxTemptratuur;
uint8_t vermogenSetpoint;
} initBelasting;
struct {
packetID_t id = 0x03;
uint8_t maxVermogen;
uint16_t maxSnelhied;
uint8_t maxTemptratuur;
} maxWaardesBelasting;
struct {
packetID_t id = 0x04;
uint8_t rendement;
uint16_t snelheid;
uint16_t stroom;
uint16_t spanning;
};
struct {
packetID_t id = 0x05;
uint16_t snelheidSetpoint;
uint16_t maxSpanning;
uint16_t maxStroom;
} initAandrijving;
struct {
packetID_t id = 0x06;
uint8_t rendement;
uint8_t vermogen;
uint16_t stroom;
uint16_t spanning;
};
} I2CPacket_t;
typedef struct { typedef struct {
uint8_t maxTemperatuur; packetID_t id;
uint8_t maxVermogen; uint8_t maxVermogen;
uint16_t maxSnelheid; uint16_t maxSnelhied;
} maxwaardes_t; uint8_t maxTemptratuur;
uint8_t vermogenSetpoint;
} initBelasting;
mqd_t MaxWaardes_mq; typedef struct {
packetID_t id;
uint8_t maxVermogen;
uint16_t maxSnelhied;
uint8_t maxTemptratuur;
} maxWaardesBelasting;
extern void mppt_setpoint(uint8_t vermogen); 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;
#endif #endif

View File

@ -1,102 +1,97 @@
#include <ti/drivers/I2CSlave.h> #include <ti/drivers/SPI.h>
#include "communicatie.h" #include "communicatie.h"
#define COMM_I2C_ADDRESS 0xB1 #define SPI_PACKET_LENGTH 4
extern pthread_t createSimplePTread(int prio, void * fn); extern pthread_t createSimplePTread(int prio, void * fn);
union I2CPacket_t PacketBuffer; unsigned char ReciveBuffer[SPI_PACKET_LENGTH];
unsigned char TransmitBuffer[SPI_PACKET_LENGTH];
void dataBijhouden(){ uint16_t comm_snelheid;
//BLOCK: wacht op werken
//??
while(1){ typedef enum {
//BLOCK: vermogen DUT aflezen SPIPARM_maxVermogen,
//?? SPIPARM_maxSnelheid,
SPIPARM_maxTempratuur,
SPIPARM_setpoint,
SPIPARM_vermogen,
SPIPARM_snelheid,
SPIPARM_tempratuur,
SPIPARM_count
} SPI_Register_t;
//BLOCK: Snelheid aflezen // communicatie spi Taak
//?? void comm_spi(){
// Initialiseer SPI
//BLOCK: bereken koppel ISP_init();
//?? SPI_Params spiParams;
SPI_Params_init(&spiParams);
//BLOCK: Vermogen belasting aflezen spiParams.frameFormat = SPI_POL0_PHA0; // mode0
//?? spiParams.bitRate = 1E6; // 1 MHz
SPI_Handle masterSpi = SPI_open(CONFIG_SPI_MASTER, &spiParams);
//BLOCK: Spanning belasting aflezen if (masterSpi == NULL) {
//?? // Display_printf(display, 0, 0, "Error initializing master SPI\n");
while (1);
//BLOCK: bereken rendement } else {
//?? // Display_printf(display, 0, 0, "Master SPI initialized\n");
//BLOCK: Temperatuur aflezen
//??
//BLOCK: sleep 1ms
usleep(1000);
}
}
// communicatie I2C Taak
void comm_i2c(){
//BLOCK: init start begint
//??
//BLOCK: Initialiseer I2C
I2C_init();
//TODO: findout corect driver for this
Slave_Handle handle;
I2CSlave_Params params;
I2CSlave_Params_init(&params);
params.transferMode = I2CSLAVE_MODE_BLOCKING;
params.slaveAddress = COMM_I2C_ADDRESS;
handle = I2CSlave_open(CONFIG_I2C_0, &params);
if (!handle) {
System_printf("I2CSlave did not open");
} }
//BLOCK: GPIO init // create transaction
//?? SPI_Transaction transaction;
transaction.count = SPI_PACKET_LENGTH;
//BLOCK: maak thread aan om waardes te updaten transaction.txBuf = (void *) TransmitBuffer;
createSimplePTread(1, &dataBijhouden); transaction.rxBuf = (void *) ReciveBuffer;
I2CPacket_t packet;
// start the loop
TransmitBuffer[0] = 0;
while(1){ while(1){
//BLOCK: read package // zero the buffers
//TODO: figgerout how it actualy works memset((void *) TransmitBuffer+1, 0, SPI_PACKET_LENGTH-1);
I2C_read(handle, &packet, sizeof(Packet)); memset((void *) ReciveBuffer, 0, SPI_PACKET_LENGTH);
//BLOCK: send reply // set data in the transmit buffer
//TODO: figgerout how it actualy works ReciveBuffer[0] = TransmitBuffer[0];
I2C_send(handle, &PacketBuffer, sizeof(PacketBuffer)); switch(TransmitBuffer[0]){
case SPIPARM_vermogen:
//BLOCK: identificeer package TransmitBuffer[1] = mppt_getVermogen();
switch (packet){
case PACKET_UPDATEBELASTING:
//BLOCK: Update setpoint
mppt_setpoint(packet.vermogenSetpoint);
break; break;
case SPIPARM_snelheid:
case PACKET_NOODSTOP: TransmitBuffer[1] = comm_snelheid;
//BLOCK: Update Overheat, Overspeed, overload waardes updaten
//??
break; break;
case SPIPARM_tempratuur:
case PACKET_INITPARAMS: TransmitBuffer[1] = noodstop_getTemptatuur();
//BLOCK: Verstuur data naar noodstop
maxwaardes_t p;
p.maxTemperatuur = packet.maxTemperatuur;
p.maxVermogen = packet.maxVermogen;
p.maxSnelheid = packet.maxSnelheid;
//mq_send(MaxWaardes_mq, (const char*)&p, sizeof(p), 3);
//BLOCK: Verstuur data naar MPPT
mppt_setpoint(packet.vermogenSetpoint);
break; break;
} }
// do the transaction
SPI_transaction(masterSpi, &transaction);
// read the data out the recive buffer
switch (ReciveBuffer[0]){
case SPIPARM_maxVermogen:
uint8_t *maxVermogen = &ReciveBuffer[1];
noodstop_setMaxVermogen(*maxVermogen);
break;
case SPIPARM_maxSnelheid:
uint16_t *maxSnelheid = &ReciveBuffer[1];
noodstop_setMaxSnelheid(*maxSnelheid);
break;
case SPIPARM_maxTempratuur:
uint8_t *maxTemptratuur = &ReciveBuffer[1];
noodstop_setMaxTemptratuur(*maxTemptratuur);
break;
case SPIPARM_setpoint:
uint8_t *setpoint = &ReciveBuffer[1];
mppt_setpoint(*setpoint);
break;
}
// increment and loop parameter
TransmitBuffer[0]++;
if(TransmitBuffer[0] == SPIPARM_count)
TransmitBuffer[0] = 0;
usleep(5000);
} }