diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/inc/Class.h b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/inc/Class.h new file mode 100644 index 0000000..fe87c53 --- /dev/null +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/inc/Class.h @@ -0,0 +1,33 @@ +/* ----------------------------------------------------------------------------- + * Class.h (c) 2013 Micro-Key bv + * ----------------------------------------------------------------------------- + * Micro-Key bv + * Industrieweg 28, 9804 TG Noordhorn + * Postbus 92, 9800 AB Zuidhorn + * The Netherlands + * Tel: +31 594 503020 + * Fax: +31 594 505825 + * Email: support@microkey.nl + * Web: www.microkey.nl + * ----------------------------------------------------------------------------- + * Description: Macro utilities to hide or protect struct members + * ----------------------------------------------------------------------------- + * $Id$ + * ----------------------------------------------------------------------------- + */ + +#ifndef _CLASS_H_ +#define _CLASS_H_ + +#ifndef CLASS_INTERNAL_INCLUDE + + #define PRIVATE(member) DONOTUSE ## member + #undef CLASS_INTERNAL_INCLUDE + +#else + + #define PRIVATE(member) member + +#endif + +#endif diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/inc/MAX5715.h b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/inc/MAX5715.h index 2b04426..7633436 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/inc/MAX5715.h +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/inc/MAX5715.h @@ -116,16 +116,21 @@ // Type definitions. // ----------------------------------------------------------------------------- +struct MAX5715; + struct MAX5715_DAC { + struct MAX5715* parent; uint8_t id; uint16_t value; + bool initialized; }; struct MAX5715 { const struct IODevice* device; struct MAX5715_DAC dac[MAX5715_NUMBER_OF_DACS]; + bool initialized; }; // ----------------------------------------------------------------------------- @@ -156,7 +161,7 @@ extern ErrorStatus MAX5715_construct(struct MAX5715* self, const struct IODevice * * @param self * - * @return ErrorStatus SUCCESS if initialisation was successful + * @return ErrorStatus SUCCESS if destruction was successful * ERROR otherwise * * @todo @@ -171,7 +176,7 @@ extern void MAX5715_destruct(struct MAX5715* self); * * @param parameters SpiParameters struct to fill * - * @return ErrorStatus SUCCESS if initialisation was successful + * @return ErrorStatus SUCCESS if function was successful * ERROR otherwise * * @todo @@ -187,7 +192,8 @@ extern ErrorStatus MAX5715_getSpiParameters(struct SpiParameters* parameters); * * @param self The MAX5715 instance * @param command The command to be sent - * @return ErrorStatus SUCCESS if initialisation was successful + * + * @return ErrorStatus SUCCESS if function was successful * ERROR otherwise * * @todo @@ -195,19 +201,38 @@ extern ErrorStatus MAX5715_getSpiParameters(struct SpiParameters* parameters); */ extern ErrorStatus MAX5715_sendCommand(const struct MAX5715* self, uint8_t command, uint16_t data); + /** ---------------------------------------------------------------------------- - * MAX5715DAC_setOutput - * Sets the output of the DAC in argument self to the value given in argument - * value + * MAX5715Channel_construct + * Constructor for one of the channels on the MAX5715 DAC * - * @param self The DAC to use - * @param value The value to set - * @return ErrorStatus SUCCESS if initialisation was successful + * @param self The channel object to initialize + * @param parent The parent object that the channel belongs + * to + * @param id The channel number + * + * @return ErrorStatus SUCCESS if construction was successful * ERROR otherwise * * @todo * ----------------------------------------------------------------------------- */ -extern ErrorStatus MAX5715DAC_setOutput(struct MAX5715_DAC self, uint16_t value); +extern ErrorStatus MAX5715Channel_construct(struct MAX5715_DAC* self, struct MAX5715* parent, size_t id); + + +/** ---------------------------------------------------------------------------- + * MAX5715Channel_setValue + * Sends value to the DAC channel in self + * + * @param self The DAC object + * @param value Value to write + * + * @return ErrorStatus SUCCESS if setting value was successful + * ERROR otherwise + * + * @todo + * ----------------------------------------------------------------------------- + */ +extern ErrorStatus MAX5715Channel_setValue(struct MAX5715_DAC* self, uint16_t value); #endif /* INC_MAX5715_H_ */ diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/inc/Observable.h b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/inc/Observable.h new file mode 100644 index 0000000..cf688eb --- /dev/null +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/inc/Observable.h @@ -0,0 +1,131 @@ +/* ----------------------------------------------------------------------------- + * Observable.h (c) 2013 Micro-Key bv + * ----------------------------------------------------------------------------- + * Micro-Key bv + * Industrieweg 28, 9804 TG Noordhorn + * Postbus 92, 9800 AB Zuidhorn + * The Netherlands + * Tel: +31 594 503020 + * Fax: +31 594 505825 + * Email: support@microkey.nl + * Web: www.microkey.nl + * ----------------------------------------------------------------------------- + * Description: Observer design pattern + * ----------------------------------------------------------------------------- + * $Id$ + * ----------------------------------------------------------------------------- + */ + +#ifndef _OBSERVABLE_H_ +#define _OBSERVABLE_H_ + +/* --------------* + * Include files * + * --------------* + */ + +#include "stm32f10x.h" + +#include "Class.h" +#include "Observer.h" + +/* -------------------------------* + * Constant and macro definitions * + * -------------------------------* + */ + +/** +* Maximal number of Observers for one Observable. +*/ +#define OBSERVABLE_MAX_OBSERVERS (32) + +/** +* Static initializer for the Observable class. +* Typical usage: struct Observable observable = OBSERVABLE_INITIALIZER; +*/ +#ifdef CLASS_INTERNAL_INCLUDE + #define OBSERVABLE_INITIALIZER { .nrOfObservers = 0, .observers = { 0, } } +#else + #define OBSERVABLE_INITIALIZER { .DONOTUSEnrOfObservers = 0, .DONOTUSEobservers = { 0, } } +#endif + +/* ------------------* + * Type definitions. * + * ------------------* + */ + +/** +* The Observable class. +*/ +struct Observable +{ + int PRIVATE(nrOfObservers); + Observer PRIVATE(observers)[OBSERVABLE_MAX_OBSERVERS]; +}; + +/* ----------------------* + * Function declarations * + * ----------------------* + */ + +/** +* Initializes the Observable class. +* This is not needed if the Observable has been statically initialized by "struct Observable observable = OBSERVABLE_INITIALIZER". +* @param self: address of the Observable struct. +* @retval none. +*/ +void Observable_initialize(struct Observable* self); + +/** +* Terminates the Observable class. All Observers are removed. +* @param self: address of the Observable struct. +* @retval none. +*/ +void Observable_terminate(struct Observable* self); + +/** +* Adds one Observer to the Observable. +* @param self: address of the Observable struct. +* @param observer: Observer to be added. +* @retval ErrorStatus: returns an error in case the maximum number of Observers have been added. +*/ +ErrorStatus Observable_addObserver(struct Observable* self, const Observer observer); + +/** +* Adds one Observer to the Observable at the front of the list. +* This ensures that this Observer is notified before Observers added by Observable_addObserver. +* @param self: address of the Observable struct. +* @param observer: Observer to be added. +* @retval ErrorStatus: returns an error in case the maximum number of Observers have been added. +*/ +ErrorStatus Observable_addObserverAtFront(struct Observable* self, const Observer observer); + +/** +* Notifies all Observers by calling the Observer callback function. The parameter void* data will be +* passed as parameter to the Observer. +* @param self: address of the Observable struct. +* @param data: void pointer data to be passed as parameter to the Observer. +* @retval ErrorStatus: returns an error in case one or more of the Observers returned an error. +*/ +ErrorStatus Observable_notifyObservers(const struct Observable* self, const void* const data); + +/** +* Deletes one specific Observer added before. If the Observer cannot be found, no action is taken. +* @param self: address of the Observable struct. +* @param observer: Observer to be deleted. +*/ +void Observable_deleteObserver(struct Observable* self, const Observer observer); + +/** +* Deletes all Observers added. +* @param self: address of the Observable struct. +*/ +void Observable_deleteObservers(struct Observable* self); + +/** +* Returns the number of Observers currently subscribed to an Observable. +* @param self: address of the Observable struct. +*/ +int Observable_nrOfObservers(struct Observable* self); + +#endif diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/inc/Observer.h b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/inc/Observer.h new file mode 100644 index 0000000..e33af83 --- /dev/null +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/inc/Observer.h @@ -0,0 +1,39 @@ +/* ----------------------------------------------------------------------------- + * Observer.h (c) 2013 Micro-Key bv + * ----------------------------------------------------------------------------- + * Micro-Key bv + * Industrieweg 28, 9804 TG Noordhorn + * Postbus 92, 9800 AB Zuidhorn + * The Netherlands + * Tel: +31 594 503020 + * Fax: +31 594 505825 + * Email: support@microkey.nl + * Web: www.microkey.nl + * ----------------------------------------------------------------------------- + * Description: Observer design pattern + * ----------------------------------------------------------------------------- + * $Id$ + * ----------------------------------------------------------------------------- + */ + +#ifndef _OBSERVER_H_ +#define _OBSERVER_H_ + +/* --------------* + * Include files * + * --------------* + */ + +#include "stm32f10x.h" + +/* ------------------* + * Type definitions. * + * ------------------* + */ + +/** +* The Observable class (which is a function pointer) +*/ +typedef ErrorStatus (*Observer)(const void* const data); + +#endif diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/src/MAX5715.c b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/src/MAX5715.c index 7aca670..677ad21 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/src/MAX5715.c +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/src/MAX5715.c @@ -63,14 +63,21 @@ ErrorStatus MAX5715_construct(struct MAX5715* self, const struct IODevice* devic { ErrorStatus returnValue = SUCCESS; int loopCounter; - if (self != NULL) + if (!self->initialized) { - self->device = device; - - for (loopCounter = 0; loopCounter < MAX5715_NUMBER_OF_DACS; loopCounter++) + if (device != NULL) { - self->dac[loopCounter].id = loopCounter; - self->dac[loopCounter].value = 0x0000; + for (loopCounter = 0; loopCounter < MAX5715_NUMBER_OF_DACS; loopCounter++) + { + self->dac[loopCounter].initialized = false; + } + + self->device = device; + self->initialized = true; + } + else + { + returnValue = ERROR; } } else @@ -137,3 +144,51 @@ ErrorStatus MAX5715_sendCommand(const struct MAX5715* self, uint8_t command, uin return IODevice_write(self->device, buffer, 3); } + + +ErrorStatus MAX5715Channel_construct(struct MAX5715_DAC* self, struct MAX5715* parent, size_t id) +{ + ErrorStatus returnValue = SUCCESS; + + if (id < MAX5715_NUMBER_OF_DACS) + { + // Check that the parent has no channel already initialized on that ID + if (!parent->dac[id].initialized) + { + if (!self->initialized) + { + parent->dac[id] = *self; + + self->id = id; + self->parent = parent; + self->value = 0; + self->initialized = true; + } + else + { + returnValue = ERROR; + } + } + else + { + returnValue = ERROR; + } + } + else + { + returnValue = ERROR; + } + return returnValue; +} + + +ErrorStatus MAX5715Channel_setValue(struct MAX5715_DAC* self, uint16_t value) +{ + ErrorStatus returnValue = SUCCESS; + + // Send data + + // Send GO + + return returnValue; +} diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/src/Observable.c b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/src/Observable.c new file mode 100644 index 0000000..43ed3d9 --- /dev/null +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/src/Observable.c @@ -0,0 +1,176 @@ +/* ----------------------------------------------------------------------------- + * Observable.c (c) 2013 Micro-Key bv + * ----------------------------------------------------------------------------- + * Micro-Key bv + * Industrieweg 28, 9804 TG Noordhorn + * Postbus 92, 9800 AB Zuidhorn + * The Netherlands + * Tel: +31 594 503020 + * Fax: +31 594 505825 + * Email: support@microkey.nl + * Web: www.microkey.nl + * ----------------------------------------------------------------------------- + * Description: Observer design pattern + * ----------------------------------------------------------------------------- + * $Id$ + * ----------------------------------------------------------------------------- + */ + +/* --------------* + * Include files * + * --------------* + */ + +#define CLASS_INTERNAL_INCLUDE +#include "Observable.h" +#include "Logger.h" +#include + +/* -------------------------------* + * Constant and macro definitions * + * -------------------------------* + */ + +#define OBSERVER_NOT_FOUND (-1) + +/* ----------------------* + * Function declarations * + * ----------------------* + */ + +// Searches for the specified observer in the observer list and returns the index on self->observers[] if found. +// If not found, the index returned is OBSERVER_NOT_FOUND. +static int indexOfObserverInList(const struct Observable* self, const Observer observer); + +/* ---------------------* + * Function definitions * + * ---------------------* + */ + +void Observable_initialize(struct Observable* self) +{ + Observable_deleteObservers(self); +} + +void Observable_terminate(struct Observable* self) +{ + Observable_deleteObservers(self); +} + +ErrorStatus Observable_addObserver(struct Observable* self, const Observer observer) +{ + ErrorStatus errorStatus = SUCCESS; + + // Only add Observer if it hasn't been added before + if(indexOfObserverInList(self, observer) == OBSERVER_NOT_FOUND) + { + if(self->nrOfObservers < (int)sizeof(self->observers) / (int)sizeof(self->observers[0])) + { + self->observers[self->nrOfObservers] = observer; + ++self->nrOfObservers; + } + else + { + LOGGER_ERROR("No space left to store a new observer"); + errorStatus = ERROR; + } + } + + return errorStatus; +} + +ErrorStatus Observable_addObserverAtFront(struct Observable* self, const Observer observer) +{ + ErrorStatus errorStatus = SUCCESS; + int observerIndex; + + // Search for this Observer in the list + observerIndex = indexOfObserverInList(self, observer); + + // Only add Observer if it hasn't been added before + if(observerIndex == OBSERVER_NOT_FOUND) + { + if(self->nrOfObservers < (int)sizeof(self->observers) / (int)sizeof(self->observers[0])) + { + // Move all entries one down + int i; + + for(i = self->nrOfObservers - 1; i >= 0; --i) + { + self->observers[i + 1] = self->observers[i]; + } + + self->observers[0] = observer; + ++self->nrOfObservers; + } + else + { + LOGGER_ERROR("No space left to store a new observer"); + errorStatus = ERROR; + } + } + + return errorStatus; +} + +ErrorStatus Observable_notifyObservers(const struct Observable* self, const void* const data) +{ + ErrorStatus errorStatus = SUCCESS; + int i; + + for(i = 0; i < self->nrOfObservers; ++i) + { + errorStatus &= self->observers[i](data); + } + + return errorStatus; +} + +void Observable_deleteObserver(struct Observable* self, const Observer observer) +{ + int observerIndex; + + // Search for this Observer in the list + observerIndex = indexOfObserverInList(self, observer); + + // If found, move all remaining entries one up + if(observerIndex != OBSERVER_NOT_FOUND) + { + int i; + + for(i = observerIndex; i < self->nrOfObservers - 1; ++i) + { + self->observers[i] = self->observers[i + 1]; + } + + --self->nrOfObservers; + } +} + +void Observable_deleteObservers(struct Observable* self) +{ + self->nrOfObservers = 0; +} + +int Observable_nrOfObservers(struct Observable* self) +{ + return self->nrOfObservers; +} + +static int indexOfObserverInList(const struct Observable* self, const Observer observer) +{ + int observerIndex = OBSERVER_NOT_FOUND; + int i; + + // Search for this Observer in the list + for(i = 0; i < self->nrOfObservers; ++i) + { + if(self->observers[i] == observer) + { + observerIndex = i; + break; + } + } + + return observerIndex; +} diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/adc.h b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/adc.h index 6b4b4ef..a892146 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/adc.h +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/adc.h @@ -67,6 +67,7 @@ struct AdcChannel uint8_t Rank; uint8_t ADC_SampleTime; T_PL_GPIO input; + bool initialized; }; struct AdcParameters @@ -93,6 +94,7 @@ struct Adc // order of convertions. E.G. channel 5 can be put first while channel 1 can be put last. // The array index stands for the RANK uint16_t channelValue[ADC_NUMBER_OF_CHANNELS]; + bool initialized; }; // ----------------------------------------------------------------------------- @@ -188,6 +190,7 @@ extern ErrorStatus ADC_setDMAStatus (struct Adc* self, FunctionalState command); * Constructor for ADC channel instance * * @param self The ADC channel instance to initialize + * @param parent The parent ADC this channel belongs to * @param parameters Additional ADC parameters * * @return ErrorStatus SUCCESS if initialisation was successful @@ -196,7 +199,7 @@ extern ErrorStatus ADC_setDMAStatus (struct Adc* self, FunctionalState command); * @todo * ----------------------------------------------------------------------------- */ -extern ErrorStatus ADCChannel_construct(struct AdcChannel* self, struct AdcChannelParameters* parameters); +extern ErrorStatus ADCChannel_construct(struct AdcChannel* self, struct Adc* parent, struct AdcChannelParameters* parameters); /** ---------------------------------------------------------------------------- diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/adc.c b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/adc.c index 624a531..04410c0 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/adc.c +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/adc.c @@ -140,7 +140,7 @@ ErrorStatus ADC_setDMAStatus (struct Adc* self, FunctionalState command) } -ErrorStatus ADCChannel_construct(struct AdcChannel* self, struct AdcChannelParameters* parameters) +ErrorStatus ADCChannel_construct(struct AdcChannel* self, struct Adc* parent, struct AdcChannelParameters* parameters) { ErrorStatus returnValue = SUCCESS; diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/main.c b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/main.c index 25df797..48d9d10 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/main.c +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/main.c @@ -89,7 +89,7 @@ static struct Display _display; struct Display* display = &_display; static struct NHD0420 nhd0420; -static struct MAX5715 max5715; +static struct MAX5715 max5715 = {.initialized = false}; // ----------------------------------------------------------------------------- // Function declarations