Files
ACD528xx_Test_Project/source/main.cpp
T
Dominik Kotarski 937e762857 Changed interrupt architecture and improved joystick task
Now supports buffered interrupts on ioexpander and ignores constantly pressed buttons. Joystick task is also changed to support buffered messages.
2019-03-18 15:47:12 +01:00

260 lines
7.0 KiB
C++

/**
* @file main.cpp
* @author Jurica Resetar (Jurica@aconno.de)
* @brief Template project based on FreeRTOS and Nordic's SDK
* @version 0.1
* @date 2019-02-11
*
* @copyright Copyright (c) 2019
*
*/
#include <stdint.h>
#include <string.h>
#include "nordic_common.h"
#include "nrf.h"
#include "nrf_sdh_freertos.h"
#include "FreeRTOS.h"
#include "task.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "aconnoConfig.h"
#include "aconnoHelpers.h"
#include "aconnoTasks.h"
#include "aconnoBLE.h"
#include "aconnoTasks.h"
/**
* @brief loger task handler
*
*/
TaskHandle_t initTask;
/**
* @brief State Machine menu task handler
*
*/
TaskHandle_t menuStateMachineTask;
/**
* @brief LDR example task
*
*/
TaskHandle_t LDRTask;
/**
* @brief Joystick example task
*
*/
TaskHandle_t joystickExampleTask;
/**
* @brief rgbLedExampleTask
*
*/
TaskHandle_t rgbLedExampleTask;
/**
* @brief Handle of a task taking care of reading ioexpander over i2c in
* order to dertermine a source of the interrupt
*
*/
TaskHandle_t getInterruptSourceTask;
#if NRF_LOG_ENABLED
extern TaskHandle_t mLoggerThread;
#endif
TaskHandle_t potTask;
TaskHandle_t tempTask;
TaskHandle_t gyroTask;
TaskHandle_t bleTask;
TaskHandle_t bleImageTask;
TaskHandle_t irTask;
TaskHandle_t ledsTask;
TaskHandle_t relayTask;
SemaphoreHandle_t getInterruptSourceSemaphore;
QueueHandle_t interruptSourceQueue;
QueueHandle_t interruptSourceQueueSecondary;
/**
* @brief Object defining current state of the Master Example
*/
State *pMenuState;
AconnoI2C i2c(PIN_EXP_SDA, PIN_EXP_SCL);
AconnoSPI spi(PIN_EPD_MISO, PIN_EPD_MOSI, PIN_EPD_SCK);
AconnoSAADC adc;
AconnoGPIO ledRed(PIN_LED_RED, GPIO_DIR::GPIO_OUTPUT);
AconnoGPIO ledBlue(PIN_LED_BLUE, GPIO_DIR::GPIO_OUTPUT);
AconnoGPIO ledGreen(PIN_LED_GREEN, GPIO_DIR::GPIO_OUTPUT);
AconnoGPIO irLed(PIN_IRLED, GPIO_DIR::GPIO_OUTPUT);
AconnoGPIO sw1 (PIN_BUTTON, GPIO_DIR::GPIO_INPUT);
AconnoGPIO ld1 (PIN_LED, GPIO_DIR::GPIO_OUTPUT);
GDEP015OC1 epd(spi, PIN_EPD_CS, PIN_EPD_DC, PIN_EPD_RST, PIN_EPD_BUSY);
MCP23017 expdr(i2c, TWI_EXPANDER_ADDR, EXPDR_A_INT, GPIO_DIR::GPIO_INTERRUPT,
GPIO_PULL::OFF, GPIO_EDGE::FALLING, interruptCallback);
LSM9DS1 mems(i2c);
LDR light(adc, ADC_LIGHT);
Potentiometer pot(adc, ADC_POT);
MCP9700 ambientTemperature(adc, ADC_TEMP);
// Must be global
AconnoBLEService myService;
AconnoBLEChar myChar;
/** @brief Function for application main entry.
*/
int main(void)
{
// Initialize modules.
logInit();
clockInit();
NRF_LOG_INFO("\r\n\r\nMain started.");
if(pdPASS != xTaskCreate(logTaskFunction, "INIT", 256, NULL, 1,
&initTask))
{
APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
}
// Initialize i2c
i2c.init();
// Initialize ioexpander
initIoExpander(&expdr);
expdr.setupInterruptPin(MCP23017_CENTER, MCP23017_INTMODE::FALLING);
expdr.setupInterruptPin(MCP23017_UP, MCP23017_INTMODE::FALLING);
expdr.setupInterruptPin(MCP23017_DOWN, MCP23017_INTMODE::FALLING);
expdr.setupInterruptPin(MCP23017_RIGHT, MCP23017_INTMODE::FALLING);
expdr.setupInterruptPin(MCP23017_LEFT, MCP23017_INTMODE::FALLING);
// End ioexpander Initialize
// Turn off leds
ledRed.set();
ledBlue.set();
ledGreen.set();
ld1.set();
// Create all semaphores that will be used
// todo: extract magic numbers into xymbolic constants
getInterruptSourceSemaphore = xSemaphoreCreateCounting(
10, // max count
0); // initial count
if (getInterruptSourceSemaphore == NULL)
{
NRF_LOG_ERROR("ERROR, not enough space available to create semaphore!");
}
// Create all tasks that will be used
xTaskCreate(getInterruptSourceFunction, "GetInterruptSourceTask", 256, NULL,
2, &getInterruptSourceTask);
xTaskCreate(menuStateMachineTaskFunction, "MenuStateMachine", 256, NULL, 1,
&menuStateMachineTask);
xTaskCreate(ldrFunction, "LDR", 256, NULL, 1, &LDRTask);
xTaskCreate(potentiometerFunction, "POT", 256, NULL, 1, &potTask);
xTaskCreate(temperatureFunction, "TEMP", 256, NULL, 1, &tempTask);
xTaskCreate(gyroFunction, "GYRO", 256, NULL, 1, &gyroTask);
xTaskCreate(bleBeaconFunction, "BEACON", 256, NULL, 1, &bleTask);
xTaskCreate(infraredFunction, "INFRARED", 256, NULL, 1, &irTask);
xTaskCreate(ledsFunction, "LEDS", 256, NULL, 1, &ledsTask);
xTaskCreate(relayFunction, "RELAYS", 256, NULL, 1, &relayTask);
xTaskCreate(joystickTaskFunction, "JoystickTask", 256, NULL, 1,
&joystickExampleTask);
xTaskCreate(rgbLedTaskFunction, "rgbTask", 256, NULL, 1, &rgbLedExampleTask);
// Todo: Consider creating a static instead queue via xQueueCreateStatic()
// Create a queue capable of containing 10 unsigned long values.
// todo: extract magic numbers into a symbolic constants
interruptSourceQueue = xQueueCreate(10, sizeof(IOEXP_INT_SOURCE));
interruptSourceQueueSecondary = xQueueCreate(10, sizeof(IOEXP_INT_SOURCE));
if(interruptSourceQueue == NULL ||interruptSourceQueueSecondary == NULL)
{
NRF_LOG_ERROR("ERROR, not enough space available to create queues!");
}
// Initialize state machine
pMenuState = new Intro1State();
pMenuState->displayOnEpd();
// Activate deep sleep mode.
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
// Initialize modules.
timersInit();
applicationTimerStart();
// Initialize aconno BLE module
AconnoBLE &myBLE = *getAconnoBLEInstance();
myBLE.initBLEStack();
uint16_t myUUID = BLE_EX_CHAR_UUID;
myService.serviceInit(myUUID);
myBLE.addService(&myService);
myChar.charInit(BLE_EX_CHAR_SIZE_B, WRITE);
myChar.regOnWriteCall(imageUpdate);
myService.addChar(&myChar);
myBLE.setTXPower(4);
// Create a FreeRTOS task for the BLE stack.
// The task will run advertising_start() before entering its loop.
nrf_sdh_freertos_init(NULL, NULL);
NRF_LOG_INFO("HRS FreeRTOS example started.");
// Start FreeRTOS scheduler.
vTaskStartScheduler();
for(;;)
{
APP_ERROR_HANDLER(NRF_ERROR_FORBIDDEN);
}
}
#if NRF_LOG_ENABLED
/**@brief Thread for handling the logger.
*
* @details This thread is responsible for processing log entries if
* logs are deferred. Thread flushes all log entries and suspends. It is
* resumed by idle task hook.
*
* @param[in] arg Pointer used for passing some arbitrary information
* (context) from the osThreadCreate() call to the thread.
*/
void loggerThread(void *arg)
{
UNUSED_PARAMETER(arg);
while(1)
{
NRF_LOG_FLUSH();
vTaskSuspend(NULL); // Suspend myself
}
}
#endif //NRF_LOG_ENABLED
/**@brief A function which is hooked to idle task.
* @note Idle hook must be enabled in FreeRTOS configuration
* (configUSE_IDLE_HOOK).
*/
#ifdef __cplusplus
extern "C"
{
void vApplicationIdleHook(void)
{
#if NRF_LOG_ENABLED
vTaskResume(mLoggerThread);
#endif
}
}
#endif