272 lines
7.5 KiB
C++
272 lines
7.5 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;
|
|
TaskHandle_t servoTask;
|
|
SemaphoreHandle_t getInterruptSourceSemaphore;
|
|
QueueHandle_t interruptSourceQueue;
|
|
QueueHandle_t interruptSourceQueueSecondary;
|
|
TimerHandle_t intFlagTimer;
|
|
|
|
/**
|
|
* @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);
|
|
|
|
Servo servo(PIN_SERVO2, 2);
|
|
// 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
|
|
getInterruptSourceSemaphore = xSemaphoreCreateCounting(
|
|
INTERRUPT_SEMAPHORE_MAX_COUNT,
|
|
INTERRUPT_SEMAPHORE_INITIAL_COUNT);
|
|
if (getInterruptSourceSemaphore == NULL)
|
|
{
|
|
NRF_LOG_ERROR("ERROR, not enough space available to create semaphore!");
|
|
}
|
|
|
|
// Create all tasks that will be used
|
|
// getInterruptSource task needs higher priority to ensure prompt interrupt
|
|
// handling
|
|
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);
|
|
xTaskCreate(servoFunction, "SERVO", 256, NULL, 1, &servoTask);
|
|
|
|
// Todo: Consider creating a static instead queue via xQueueCreateStatic()
|
|
interruptSourceQueue = xQueueCreate(QUEUE_DEFAULT_SIZE,
|
|
sizeof(IOEXP_INT_SOURCE));
|
|
interruptSourceQueueSecondary = xQueueCreate(QUEUE_DEFAULT_SIZE,
|
|
sizeof(IOEXP_INT_SOURCE));
|
|
if(interruptSourceQueue == NULL ||interruptSourceQueueSecondary == NULL)
|
|
{
|
|
NRF_LOG_ERROR("ERROR, not enough space available to create queues!");
|
|
}
|
|
|
|
// todo: extract magic numbers into constants
|
|
intFlagTimer = xTimerCreate("IoexpanderInterruptTimer",
|
|
IOEXP_INTERRUPT_TIMER_PERIOD,
|
|
pdFALSE, // do not loop infinitely
|
|
(void *) 0, // timer id
|
|
intFlagTimerCallback); //timer callback
|
|
|
|
// 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
|