re-organized HAL
git-svn-id: https://svn.vbchaos.nl/svn/hsb/trunk@220 05563f52-14a8-4384-a975-3d1654cca0fa
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
/// @file IODevice.c
|
||||
/// @brief Description
|
||||
// -----------------------------------------------------------------------------
|
||||
// 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
|
||||
// -----------------------------------------------------------------------------
|
||||
/// $Revision$
|
||||
/// $Author$
|
||||
/// $Date$
|
||||
// (c) 2017 Micro-Key bv
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
/// @file IODevice.c
|
||||
/// @ingroup {group_name}
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Include files
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#include "IODevice.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Type definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// File-scope variables
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
ErrorStatus IODevice_construct (struct IODevice* self, ReadFunction read, WriteFunction write)
|
||||
{
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
|
||||
self->_write = write;
|
||||
self->_read = read;
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
ErrorStatus IODevice_write(const struct IODevice* self, const char* buffer, size_t length)
|
||||
{
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
|
||||
if (self->_write != NULL)
|
||||
{
|
||||
self->_write(self, buffer, length);
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
@@ -0,0 +1,221 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
/// @file keypadMatrix.c
|
||||
/// @brief Description
|
||||
// -----------------------------------------------------------------------------
|
||||
// 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
|
||||
// -----------------------------------------------------------------------------
|
||||
/// $Revision$
|
||||
/// $Author$
|
||||
/// $Date$
|
||||
// (c) 2017 Micro-Key bv
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
/// @file keypadMatrix.c
|
||||
/// @ingroup {group_name}
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Include files
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include "FreeRTOSFixes.h"
|
||||
|
||||
#include "Logger.h"
|
||||
|
||||
#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_DEF_QUEUESIZE (32)
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Type definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// File-scope variables
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
static ErrorStatus read(const struct IODevice* self, char* buffer, size_t length, size_t* actualLength);
|
||||
static void KeypadTask(void* parameters);
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
ErrorStatus Keypad_construct(struct Keypad* self, struct KeypadParameters* parameters, int debounceTime)
|
||||
{
|
||||
int rowCounter = 0;
|
||||
int colCounter = 0;
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
|
||||
if(keypad != NULL)
|
||||
{
|
||||
IODevice_construct(&self->device, read, NULL);
|
||||
|
||||
if(returnValue == SUCCESS)
|
||||
{
|
||||
//! 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)
|
||||
{
|
||||
LOGGER_INFO("Keypad task started");
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER_ERROR("Keypad task FAILED");
|
||||
}
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
void Keypad_Destruct (const struct Keypad* self)
|
||||
{
|
||||
vTaskDelete(self->taskHandle);
|
||||
vQueueDelete(self->rxQueue);
|
||||
}
|
||||
|
||||
|
||||
ErrorStatus Keypad_getDefaultParameters(struct KeypadParameters* parameters)
|
||||
{
|
||||
ErrorStatus errorStatus = SUCCESS;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void KeypadTask(void* parameters)
|
||||
{
|
||||
int rowCounter = 0;
|
||||
int colCounter = 0;
|
||||
struct Keypad* self = (struct Keypad*) parameters;
|
||||
while (1)
|
||||
{
|
||||
// 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);
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
/// @file nhd0420.c
|
||||
/// @brief Description
|
||||
// -----------------------------------------------------------------------------
|
||||
// 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
|
||||
// -----------------------------------------------------------------------------
|
||||
/// $Revision$
|
||||
/// $Author$
|
||||
/// $Date$
|
||||
// (c) 2017 Micro-Key bv
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
/// @file nhd0420.c
|
||||
/// @ingroup {group_name}
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Include files
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "stm32f10x.h"
|
||||
|
||||
#include "nhd0420.h"
|
||||
|
||||
#include "spi.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#define NHD0420_CURSOR_OFFSET_ROW1 (0x00)
|
||||
#define NHD0420_CURSOR_OFFSET_ROW2 (0x40)
|
||||
#define NHD0420_CURSOR_OFFSET_ROW3 (0x14)
|
||||
#define NHD0420_CURSOR_OFFSET_ROW4 (0x54)
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Type definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// File-scope variables
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
static int nhd0420_cursorRowOffset[NHD0420_NUMBER_OF_ROWS] =
|
||||
{
|
||||
NHD0420_CURSOR_OFFSET_ROW1,
|
||||
NHD0420_CURSOR_OFFSET_ROW2,
|
||||
NHD0420_CURSOR_OFFSET_ROW3,
|
||||
NHD0420_CURSOR_OFFSET_ROW4
|
||||
};
|
||||
|
||||
static const struct IODevice* displayDevice = NULL;
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
ErrorStatus NHD0420_construct(const struct IODevice* const device)
|
||||
{
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
|
||||
if (displayDevice == NULL)
|
||||
{
|
||||
displayDevice = device;
|
||||
}
|
||||
else
|
||||
{
|
||||
returnValue = ERROR;
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
void NHD0420_destruct (void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
ErrorStatus NHD0420_getSpiParameters(struct SpiParameters* parameters)
|
||||
{
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
|
||||
if ((configCPU_CLOCK_HZ / 64) < NHD0420_SPI_MAX_CLK_HZ)
|
||||
{
|
||||
parameters->SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
|
||||
}
|
||||
else if ((configCPU_CLOCK_HZ / 128) < NHD0420_SPI_MAX_CLK_HZ)
|
||||
{
|
||||
parameters->SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
|
||||
}
|
||||
else if ((configCPU_CLOCK_HZ / 256) < NHD0420_SPI_MAX_CLK_HZ)
|
||||
{
|
||||
parameters->SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
|
||||
}
|
||||
else
|
||||
{
|
||||
// The CPU clock is too high. The pre-scaler has a max of 256. A clock higher than 25,6 MHz
|
||||
// results in a SPI CLK higher than 100 kHz, which is the max of the display
|
||||
returnValue = ERROR;
|
||||
}
|
||||
|
||||
if (returnValue == SUCCESS)
|
||||
{
|
||||
// SPI pre-scaler was no problem - assign the remaining parameters
|
||||
parameters->SPI_CPHA = NHD0420_SPI_CPHA;
|
||||
parameters->SPI_CPOL = NHD0420_SPI_CPOL;
|
||||
parameters->SPI_CRCPolynomial = NHD0420_SPI_CRCPolynomial;
|
||||
parameters->SPI_DataSize = NHD0420_SPI_DataSize;
|
||||
parameters->SPI_Direction = NHD0420_SPI_Direction;
|
||||
parameters->SPI_FirstBit = NHD0420_SPI_FirstBit;
|
||||
parameters->SPI_Mode = NHD0420_SPI_Mode;
|
||||
parameters->SPI_NSS = NHD0420_SPI_NSS;
|
||||
parameters->rxQueueSize = NHD0420_SPI_RX_QUEUE;
|
||||
parameters->txQueueSize = NHD0420_SPI_TX_QUEUE;
|
||||
}
|
||||
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
ErrorStatus NHD0420_setCursorToPosition(char row, char column)
|
||||
{
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
|
||||
// Setting cursor requires sending a command sequence with an additional
|
||||
// address parameter representing the line/column
|
||||
|
||||
// Each line has a dedicated offset, the column is simply added to that offset
|
||||
// Make sure to keep within boundaries to avoid glitches
|
||||
|
||||
row = row -1;
|
||||
column = column - 1;
|
||||
|
||||
// Check the coordinates to avoid glitches
|
||||
if ((row >= NHD0420_NUMBER_OF_ROWS) && (column >= NHD0420_NUMBER_OF_COLUMNS))
|
||||
{
|
||||
returnValue = ERROR;
|
||||
}
|
||||
|
||||
if (returnValue == SUCCESS)
|
||||
{
|
||||
char address = nhd0420_cursorRowOffset[(int)row] + column;
|
||||
char buffer[3] = {NHD0420_CMD_PREFIX, NHD0420_CMD_CURSOR_SET, address};
|
||||
returnValue = NHD0420_sendData(buffer, 3);
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
ErrorStatus NHD0420_setContrast(char contrast)
|
||||
{
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
|
||||
// Setting contrast requires sending a command sequence with an additional
|
||||
// parameter representing the contrast
|
||||
// Contrast values must be between NHD0420_CONTRAST_MIN and
|
||||
// NHD0420_CONTRAST_MAX. If boundaries are exceeded, this function will be
|
||||
// left with an ERROR
|
||||
|
||||
if ((contrast < NHD0420_CONTRAST_MIN) || (contrast > NHD0420_CONTRAST_MAX))
|
||||
{
|
||||
returnValue = ERROR;
|
||||
}
|
||||
|
||||
|
||||
if (returnValue == SUCCESS)
|
||||
{
|
||||
char buffer[3] = {NHD0420_CMD_PREFIX, NHD0420_CMD_SET_CONTRAST, contrast};
|
||||
returnValue = NHD0420_sendData(buffer, 3);
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
ErrorStatus NHD0420_setBacklightBrightness(char brightness)
|
||||
{
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
|
||||
// Setting backlight brightness requires sending a command sequence with an
|
||||
// additional parameter representing the brightness
|
||||
// Brightness values must be between NHD0420_BRIGHTNESS_MIN and
|
||||
// NHD0420_BRIGHTNESS_MAX. If boundaries are exceeded, this function will be
|
||||
// left with an ERROR
|
||||
|
||||
if ((brightness < NHD0420_BRIGHTNESS_MIN) || (brightness > NHD0420_BRIGHTNESS_MAX))
|
||||
{
|
||||
returnValue = ERROR;
|
||||
}
|
||||
|
||||
|
||||
if (returnValue == SUCCESS)
|
||||
{
|
||||
char buffer[3] = {NHD0420_CMD_PREFIX, NHD0420_CMD_SET_BRIGHTNESS, brightness};
|
||||
returnValue = NHD0420_sendData(buffer, 3);
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
ErrorStatus NHD0420_setRS232Baudrate(char baudrate)
|
||||
{
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
|
||||
// Setting baudrate requires sending a command sequence with an
|
||||
// additional parameter representing the baudrate
|
||||
// Baudrate values must be between NHD0420_BAUDRATE_MIN and
|
||||
// NHD0420_BAUDRATE_MAX. If boundaries are exceeded, this function will be
|
||||
// left with an ERROR
|
||||
|
||||
if ((baudrate < NHD0420_BAUDRATE_MIN) || (baudrate > NHD0420_BAUDRATE_MAX))
|
||||
{
|
||||
returnValue = ERROR;
|
||||
}
|
||||
|
||||
if (returnValue == SUCCESS)
|
||||
{
|
||||
char buffer[3] = {NHD0420_CMD_PREFIX, NHD0420_CMD_CHANGE_RS232_BR, baudrate};
|
||||
returnValue = NHD0420_sendData(buffer, 3);
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
ErrorStatus NHD0420_setI2CAddress(char address)
|
||||
{
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
|
||||
// Setting I2C requires sending a command sequence with an
|
||||
// additional parameter representing the address
|
||||
// Baudrate values must be between NHD0420_BAUDRATE_MIN and
|
||||
// NHD0420_BAUDRATE_MAX. If boundaries are exeeded, this function will be
|
||||
// left with an ERROR
|
||||
|
||||
if ((address | 0xFE) != 0xFE)
|
||||
{
|
||||
returnValue = ERROR;
|
||||
}
|
||||
|
||||
if (returnValue == SUCCESS)
|
||||
{
|
||||
char buffer[3] = {NHD0420_CMD_PREFIX, NHD0420_CMD_CHANGE_I2C_ADDRSS, address};
|
||||
returnValue = NHD0420_sendData(buffer, 3);
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------
|
||||
* NHD0420_SendCommand
|
||||
* Send a command to the display
|
||||
*
|
||||
* @param command
|
||||
*
|
||||
* @return ErrorStatus SUCCESS if initialisation was successful
|
||||
* ERROR otherwise
|
||||
*
|
||||
* @todo
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
ErrorStatus NHD0420_sendCommand(char command)
|
||||
{
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
|
||||
char buffer[NHD0420_CMD_LENGTH] = {NHD0420_CMD_PREFIX, command};
|
||||
|
||||
returnValue = IODevice_write(displayDevice, buffer, NHD0420_CMD_LENGTH);
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
ErrorStatus NHD0420_sendData(const char* buffer, size_t length)
|
||||
{
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
|
||||
returnValue = IODevice_write(displayDevice, buffer, length);
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
Reference in New Issue
Block a user