Updates on the IODevice structure.

Display and Logger fully functional.
Keypad task completed - yet to be tested

git-svn-id: https://svn.vbchaos.nl/svn/hsb/trunk@219 05563f52-14a8-4384-a975-3d1654cca0fa
This commit is contained in:
mmi
2017-09-28 14:30:36 +00:00
parent d54c2c497c
commit 83cce4ba74
21 changed files with 615 additions and 232 deletions

View File

@@ -29,7 +29,9 @@ keypadMatrix.o \
vpath %.o $(OBJDIR)
vpath %.c $(SRCDIR)
vpath %.c \
$(SRCDIR) \
$(ROOTDIR)/hsb-mrts/src
all: $(LIBRARY_NAME)

View File

@@ -32,7 +32,12 @@
// Include files
// -----------------------------------------------------------------------------
#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"
#include "platform.h"
#include "IODevice.h"
#include "stm32f10x_exti.h"
@@ -47,6 +52,17 @@
// Type definitions.
// -----------------------------------------------------------------------------
struct KeypadQueueItem
{
char byte;
};
typedef enum
{
RELEASED = 0,
PRESSED = (!RELEASED)
}Keypad_KeyState;
struct keypadElement
{
T_PL_GPIO gpio;
@@ -55,8 +71,19 @@ struct keypadElement
struct Keypad
{
struct IODevice device;
struct keypadElement row[KEYPAD_NUMBER_OF_ROWS];
struct keypadElement column[KEYPAD_NUMBER_OF_COLUMNS];
Keypad_KeyState lastState[KEYPAD_NUMBER_OF_ROWS][KEYPAD_NUMBER_OF_COLUMNS];
xTaskHandle taskHandle;
SemaphoreHandle_t scanSemaphore;
xQueueHandle rxQueue;
int waitToDebounce_ms;
};
struct KeypadParameters
{
int rxQueueSize;
};
// -----------------------------------------------------------------------------
@@ -67,17 +94,48 @@ struct Keypad
* Keypad_construct
* contructor for the Keypad driver
*
* @param self Keypad object to initialize
* @param parameters Parameters to use for initialisation
* @param debounceTime debounce time for the keypad to use
*
* @return ErrorStatus SUCCESS if initialisation was successful
* ERROR otherwise
*
* @todo
* -----------------------------------------------------------------------------
*/
extern ErrorStatus Keypad_construct(void);
extern ErrorStatus Keypad_construct(struct Keypad* self, struct KeypadParameters* parameters, int debounceTime);
/** ----------------------------------------------------------------------------
* Keypad_destruct
* destructor for the Keypad driver
*
* @param self Keypad object to destruct
*
* @return ErrorStatus SUCCESS if initialisation was successful
* ERROR otherwise
*
* @todo
* -----------------------------------------------------------------------------
*/
extern void Keypad_destruct (const struct Keypad* self);
/** ----------------------------------------------------------------------------
* Keypad_getDefaultParameters
* Returns default parameters for a keypad
*
* @param parameters Keypad parameters struct that will be
* filled with default values
*
* @return ErrorStatus SUCCESS if initialisation was successful
* ERROR otherwise
*
* @todo
* -----------------------------------------------------------------------------
*/
extern ErrorStatus Keypad_getDefaultParameters(struct KeypadParameters* parameters);
extern ErrorStatus Keypad_logModuleInfo(void);
#endif /* KEYPAD_INC_KEYPADMATRIX_H_ */

View File

@@ -27,8 +27,6 @@
#include <stdbool.h>
#include <stdio.h>
#include "FreeRTOS.h"
#include "task.h"
#include "FreeRTOSFixes.h"
#include "Logger.h"
@@ -36,12 +34,15 @@
#include "keypadMatrix.h"
#include "platform.h"
#include "stm32f10x_it.h"
// -----------------------------------------------------------------------------
// Constant and macro definitions
// -----------------------------------------------------------------------------
#define KEYPAD_STACK_SIZE (512)
#define KEYPAD_TASK_PRIORITY (3)
#define KEYPAD_STACK_SIZE (512)
#define KEYPAD_TASK_PRIORITY (3)
#define KEYPAD_DEF_QUEUESIZE (32)
// -----------------------------------------------------------------------------
// Type definitions
@@ -52,13 +53,13 @@
// File-scope variables
// -----------------------------------------------------------------------------
static bool initialized = false;
static xTaskHandle keypadTaskHandle = NULL;
// -----------------------------------------------------------------------------
// Function declarations
// -----------------------------------------------------------------------------
static ErrorStatus read(const struct IODevice* self, char* buffer, size_t length, size_t* actualLength);
static void KeypadTask(void* parameters);
// -----------------------------------------------------------------------------
@@ -66,31 +67,63 @@ static void KeypadTask(void* parameters);
// -----------------------------------------------------------------------------
ErrorStatus Keypad_construct(void)
ErrorStatus Keypad_construct(struct Keypad* self, struct KeypadParameters* parameters, int debounceTime)
{
int rowCounter = 0;
int colCounter = 0;
ErrorStatus returnValue = SUCCESS;
BaseType_t taskCreateReturn;
if(!initialized)
if(keypad != NULL)
{
IODevice_construct(&self->device, read, NULL);
if(returnValue == SUCCESS)
{
taskCreateReturn = xTaskCreate(KeypadTask, (const char*)"keypadTask", KEYPAD_STACK_SIZE, NULL, KEYPAD_TASK_PRIORITY, &keypadTaskHandle);
if(taskCreateReturn != pdPASS)
//! Create semaphore to synchronize with Keypad/EXTI interrupt handler
vSemaphoreCreateBinary(self->scanSemaphore);
}
self->waitToDebounce_ms = debounceTime;
// Initialize memory to keep track of state changes per key
for (rowCounter = 0; rowCounter < KEYPAD_NUMBER_OF_ROWS; rowCounter++)
{
for (colCounter = 0; colCounter < KEYPAD_NUMBER_OF_COLUMNS; colCounter++)
{
self->lastState[rowCounter][colCounter] = RELEASED;
}
}
//! Create a new FREERTOS queue to handle data from Keypad input to app
self->rxQueue = xQueueCreate(parameters->rxQueueSize, sizeof(struct KeypadQueueItem));
if (self->rxQueue == 0)
{
//! Queue identifier is 0 -> error
returnValue = ERROR; //! Set error flag
}
if(returnValue == SUCCESS)
{
xTaskCreate(KeypadTask, (const char*)"keypadTask", KEYPAD_STACK_SIZE, keypad, KEYPAD_TASK_PRIORITY, self->taskHandle);
}
if(returnValue == SUCCESS)
{
//! take txSemaphore
if (xSemaphoreTake(self->scanSemaphore, 0) == pdFALSE)
{
//! An error has occurred
returnValue = ERROR;
}
}
if(returnValue == SUCCESS)
{
initialized = true;
LOGGER_INFO("Keypad task started");
// GPIO_SetBits(kRow1->rowGpio.GPIO_Typedef, kRow1->rowGpio.GPIO_InitStruct.GPIO_Pin);
}
else
{
LOGGER_ERROR("Keypad task FAILED with code %x", (unsigned int)taskCreateReturn);
LOGGER_ERROR("Keypad task FAILED");
}
}
@@ -98,36 +131,91 @@ ErrorStatus Keypad_construct(void)
}
ErrorStatus Keypad_logModuleInfo(void)
void Keypad_Destruct (const struct Keypad* self)
{
vTaskDelete(self->taskHandle);
vQueueDelete(self->rxQueue);
}
ErrorStatus Keypad_getDefaultParameters(struct KeypadParameters* parameters)
{
ErrorStatus errorStatus = SUCCESS;
OS_logTaskInfo(keypadTaskHandle);
parameters->rxQueueSize = KEYPAD_DEF_QUEUESIZE;
return errorStatus;
}
static ErrorStatus read(const struct IODevice* self, char* buffer, size_t length, size_t* actualLength)
{
ErrorStatus errorStatus = SUCCESS;
*actualLength = 1;
return errorStatus;
}
void Keypad_Destruct (void)
{
initialized = false;
}
static void KeypadTask(void* parameters)
{
int rowCounter = 0;
int colCounter = 0;
struct Keypad* self = (struct Keypad*) parameters;
while (1)
{
vTaskDelay(1000);
// Wait for an interrupt to occur on one of the keypad columns
xSemaphoreTake(self->scanSemaphore, portMAX_DELAY);
// Debounce the keypad and wait for debounceTime prior to do anything
vTaskDelay(self->waitToDebounce_ms);
LOGGER_DEBUG("ROW1: %d, ROW2: %d, ROW3: %d, ROW4: %d - Col1: %d, Col2: %d, Col3: %d, Col4: %d",
GPIO_ReadOutputDataBit(keypad->row[0].gpio.GPIO_Typedef, keypad->row[0].gpio.GPIO_InitStruct.GPIO_Pin),
GPIO_ReadOutputDataBit(keypad->row[1].gpio.GPIO_Typedef, keypad->row[1].gpio.GPIO_InitStruct.GPIO_Pin),
GPIO_ReadOutputDataBit(keypad->row[2].gpio.GPIO_Typedef, keypad->row[2].gpio.GPIO_InitStruct.GPIO_Pin),
GPIO_ReadOutputDataBit(keypad->row[3].gpio.GPIO_Typedef, keypad->row[3].gpio.GPIO_InitStruct.GPIO_Pin),
GPIO_ReadInputDataBit(keypad->column[0].gpio.GPIO_Typedef, keypad->column[0].gpio.GPIO_InitStruct.GPIO_Pin),
GPIO_ReadInputDataBit(keypad->column[1].gpio.GPIO_Typedef, keypad->column[1].gpio.GPIO_InitStruct.GPIO_Pin),
GPIO_ReadInputDataBit(keypad->column[2].gpio.GPIO_Typedef, keypad->column[2].gpio.GPIO_InitStruct.GPIO_Pin),
GPIO_ReadInputDataBit(keypad->column[3].gpio.GPIO_Typedef, keypad->column[3].gpio.GPIO_InitStruct.GPIO_Pin)
);
// Set all row outputs
for (rowCounter = 0; rowCounter < KEYPAD_NUMBER_OF_ROWS; rowCounter++)
{
GPIO_SetBits(self->row[rowCounter].gpio.GPIO_Typedef, self->row[rowCounter].gpio.GPIO_InitStruct.GPIO_Pin);
}
// Scan through each row individually by resetting it (output level low) and check all column levels
for (rowCounter = 0; rowCounter < KEYPAD_NUMBER_OF_ROWS; rowCounter++)
{
GPIO_ResetBits(self->row[rowCounter].gpio.GPIO_Typedef, self->row[rowCounter].gpio.GPIO_InitStruct.GPIO_Pin);
for (colCounter = 0; colCounter < KEYPAD_NUMBER_OF_COLUMNS; colCounter++)
{
if (GPIO_ReadInputDataBit(self->column[colCounter].gpio.GPIO_Typedef, self->column[colCounter].gpio.GPIO_InitStruct.GPIO_Pin) == (uint8_t)Bit_SET)
{
if (self->lastState[rowCounter][colCounter] == PRESSED)
{
// Key has been released
}
else
{
// nothing changed
}
}
else
{
if (self->lastState[rowCounter][colCounter] == RELEASED)
{
// Key has been pressed
}
else
{
// nothing changed
}
}
}
GPIO_SetBits(self->row[rowCounter].gpio.GPIO_Typedef, self->row[rowCounter].gpio.GPIO_InitStruct.GPIO_Pin);
}
// Reset all row outputs and return to IRQ status
for (rowCounter = 0; rowCounter < KEYPAD_NUMBER_OF_ROWS; rowCounter++)
{
GPIO_ResetBits(self->row[rowCounter].gpio.GPIO_Typedef, self->row[rowCounter].gpio.GPIO_InitStruct.GPIO_Pin);
}
IRQ_setKeypadEXTI(self, ENABLE);
}
}