Files
2019-05-15 10:55:09 +02:00

463 lines
12 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
*
*/
//------------------------------ INCLUDES -------------------------------------
#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 "settings.h"
#include "basicConfig.h"
#include "arbData.h"
#include "aconnoConfig.h"
#include "aconnoHelpers.h"
#include "aconnoTasks.h"
#include "aconnoBLE.h"
#include "aconnoTasks.h"
#include "aconnoBeeper.h"
#include "slots.h"
//-------------------------------- MACROS -------------------------------------
//----------------------------- DATA TYPES ------------------------------------
//--------------------- PRIVATE FUNCTION PROTOTYPES ---------------------------
//----------------------- STATIC DATA & CONSTANTS -----------------------------
//------------------------------ GLOBAL DATA ----------------------------------
/**
* @brief loger task handler
*/
TaskHandle_t initTask;
/**
* @brief State Machine menu task handler
*/
TaskHandle_t menuStateMachineTask;
/**
* @brief LDR example task handler
*/
TaskHandle_t ldrTask;
/**
* @brief Joystick example task handler
*/
TaskHandle_t joyTask;
/**
* @brief Rgb led example task handler
*/
TaskHandle_t rgbTask;
/**
* @brief Interrupt source pooler task
*/
TaskHandle_t getInterruptSourceTask;
/**
* @brief Sound example task handler
*/
TaskHandle_t soundTask;
/**
* @brief Potentiometer example task handler
*/
TaskHandle_t potTask;
/**
* @brief Temperature example task handler
*/
TaskHandle_t tempTask;
/**
* @brief Gyroscope example task handler
*/
TaskHandle_t gyroTask;
/**
* @brief Ble beacon example task handler
*/
TaskHandle_t bleTask;
/**
* @brief Ble image example task handler
*/
TaskHandle_t bleImageTask;
/**
* @brief Infrared example task handler
*/
TaskHandle_t irTask;
/**
* @brief LEDs example task handler
*/
TaskHandle_t ledsTask;
/**
* @brief Relay example task handler
*/
TaskHandle_t relayTask;
/**
* @brief Servomotor example task handler
*/
TaskHandle_t servoTask;
#ifdef NRF52840_XXAA
/**
* @brief USB example task handler
*/
TaskHandle_t usbTask;
#endif
#if NRF_LOG_ENABLED
extern TaskHandle_t mLoggerThread;
#endif
/**
* @brief Semaphore to control the interrupt source task
*/
SemaphoreHandle_t getInterruptSourceSemaphore;
/**
* @brief A queue which buffers interrupt events
*/
QueueHandle_t interruptSourceQueue;
/**
* @brief secondary interrupt Source queue. To be used by examples that
* require interrupts
*/
QueueHandle_t interruptSourceQueueSecondary;
/**
* @brief Mutex to protect writing on the e-paper display
*/
SemaphoreHandle_t epdWriteMutex;
/**
* @brief Mutex to protect the I2C
*/
SemaphoreHandle_t i2cWriteMutex;
/**
* @brief Object defining current state of the Master Example
*/
State *pMenuState;
/**
* @brief AconnoI2C instance which is used for i2c communication
*/
AconnoI2C i2c(PIN_EXP_SDA, PIN_EXP_SCL);
/**
* @brief AconnoSPI instance which is used for SPI communication
*/
AconnoSPI spi(PIN_EPD_MISO, PIN_EPD_MOSI, PIN_EPD_SCK);
/**
* @brief E-paper display driver class instance
*/
GDEP015OC1 epd(spi, PIN_EPD_CS, PIN_EPD_DC, PIN_EPD_RST, PIN_EPD_BUSY);
/**
* @brief IO-expander driver class instance. Ioexpander is connected
* to the joystick, button2 and relays
*/
MCP23017 expdr(i2c,
TWI_EXPANDER_ADDR,
EXPDR_A_INT,
GPIO_DIR::GPIO_INTERRUPT,
GPIO_PULL::OFF,
GPIO_EDGE::FALLING,
interruptCallback);
/**
* @brief Gyro, acceleration and magnetometer sensor driver class instance
*/
LSM9DS1 mems(i2c);
/**
* @brief Successive approximation adc instance. Used by sensors whose
* analog outputs are connected directly to the chip
*/
AconnoSAADC adc;
/**
* @brief Light dependent resisor driver class instance
*/
LDR light(adc, ADC_LIGHT);
/**
* @brief Potentiometer driver class instance
*/
Potentiometer pot(adc, ADC_POT);
/**
* @brief Temperature sensor driver class instance
*/
MCP9700 ambientTemperature(adc, ADC_TEMP);
/**
* @brief Servo driver class instance
*/
Servo servo(PIN_SERVO2, PWM_INSTANCE_ID(2));
/**
* @brief Beeper helper class instance
*/
AconnoBeeper beeper(PWM_INSTANCE_ID(1), PIN_BUZZER);
/**
* @brief GPIO driver class for the red led
*/
AconnoGPIO ledRed(PIN_LED_RED, GPIO_DIR::GPIO_OUTPUT);
/**
* @brief GPIO driver class for the blue led
*/
AconnoGPIO ledBlue(PIN_LED_BLUE, GPIO_DIR::GPIO_OUTPUT);
/**
* @brief GPIO driver class for the green led
*/
AconnoGPIO ledGreen(PIN_LED_GREEN, GPIO_DIR::GPIO_OUTPUT);
/**
* @brief GPIO driver class for the Infrared led
*/
AconnoGPIO irLed(PIN_IRLED, GPIO_DIR::GPIO_OUTPUT);
/**
* @brief GPIO driver class for button1
*/
AconnoGPIO sw1(PIN_BUTTON, GPIO_DIR::GPIO_INPUT);
/**
* @brief GPIO driver class for led1
*/
AconnoGPIO ld1(PIN_LED, GPIO_DIR::GPIO_OUTPUT);
/**
* @brief Aconno BLE service class instance.
*/
AconnoBLEService myService;
/**
* @brief Aconno BLE characteristic class instance
*/
AconnoBLEChar myChar;
//--------------------- PUBLIC FUNCTIONS AND OBJECTS --------------------------
/**
* @brief Function called at bluetooth disconnect event
*/
void disconnectionCallback(void)
{
NRF_LOG_INFO("APP disconnection callback.");
AconnoBLE &myBLE = *getAconnoBLEInstance();
myBLE.startAdvertising(APP_ADV_INTERVAL, APP_ADV_DURATION);
}
/**
* @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 board
bspInit();
// Create all semaphores that will be used
getInterruptSourceSemaphore = xSemaphoreCreateBinary();
if (getInterruptSourceSemaphore == NULL)
{
NRF_LOG_ERROR("ERROR, not enough space available to create semaphore!");
}
epdWriteMutex = xSemaphoreCreateMutex();
i2cWriteMutex = xSemaphoreCreateMutex();
// Create all tasks that will be used
// getInterruptSource task needs higher priority to ensure prompt interrupt
// handling
xTaskCreate(getInterruptSourceFunction,
"GetInterruptSourceTask",
TASK_STACK_DEPTH(256),
DEFAULT_TASK_PARAMETERS,
TASK_PRIORITY(2),
&getInterruptSourceTask);
xTaskCreate(menuStateMachineTaskFunction,
"MenuStateMachine",
TASK_STACK_DEPTH(256),
DEFAULT_TASK_PARAMETERS,
TASK_PRIORITY(1),
&menuStateMachineTask);
xTaskCreate(ldrFunction,
"LDR",
TASK_STACK_DEPTH(256),
DEFAULT_TASK_PARAMETERS,
TASK_PRIORITY(1),
&ldrTask);
xTaskCreate(potentiometerFunction,
"POT",
TASK_STACK_DEPTH(256),
DEFAULT_TASK_PARAMETERS,
TASK_PRIORITY(1),
&potTask);
xTaskCreate(temperatureFunction,
"TEMP",
TASK_STACK_DEPTH(256),
DEFAULT_TASK_PARAMETERS,
TASK_PRIORITY(1),
&tempTask);
xTaskCreate(gyroFunction,
"GYRO",
TASK_STACK_DEPTH(256),
DEFAULT_TASK_PARAMETERS,
TASK_PRIORITY(1),
&gyroTask);
xTaskCreate(bleBeaconFunction,
"BEACON",
TASK_STACK_DEPTH(256),
DEFAULT_TASK_PARAMETERS,
TASK_PRIORITY(1),
&bleTask);
xTaskCreate(infraredFunction,
"INFRARED",
TASK_STACK_DEPTH(256),
DEFAULT_TASK_PARAMETERS,
TASK_PRIORITY(1),
&irTask);
xTaskCreate(ledsFunction,
"LEDS",
TASK_STACK_DEPTH(256),
DEFAULT_TASK_PARAMETERS,
TASK_PRIORITY(1),
&ledsTask);
xTaskCreate(relayFunction,
"RELAYS",
TASK_STACK_DEPTH(256),
DEFAULT_TASK_PARAMETERS,
TASK_PRIORITY(1),
&relayTask);
xTaskCreate(joyFunction,
"JOYSTICK",
TASK_STACK_DEPTH(256),
DEFAULT_TASK_PARAMETERS,
TASK_PRIORITY(1),
&joyTask);
xTaskCreate(rgbFunction,
"RGB",
TASK_STACK_DEPTH(256),
DEFAULT_TASK_PARAMETERS,
TASK_PRIORITY(1),
&rgbTask);
xTaskCreate(servoFunction,
"SERVO",
TASK_STACK_DEPTH(256),
DEFAULT_TASK_PARAMETERS,
TASK_PRIORITY(1),
&servoTask);
xTaskCreate(soundFunction,
"SOUND",
TASK_STACK_DEPTH(256),
DEFAULT_TASK_PARAMETERS,
TASK_PRIORITY(1),
&soundTask);
#ifdef NRF52840_XXAA
xTaskCreate(usbFunction,
"USB",
TASK_STACK_DEPTH(256),
DEFAULT_TASK_PARAMETERS,
TASK_PRIORITY(1),
&usbTask);
#endif
// Create the queues which will be used
interruptSourceQueue = xQueueCreate(QUEUE_DEFAULT_SIZE,
sizeof(INT_SOURCE));
interruptSourceQueueSecondary = xQueueCreate(QUEUE_DEFAULT_SIZE,
sizeof(INT_SOURCE));
if(interruptSourceQueue == NULL ||interruptSourceQueueSecondary == NULL)
{
NRF_LOG_ERROR("ERROR, not enough space available to create queues!");
}
// Initialize state machine
pMenuState = new Intro1State(State::EPD_WRITE_METHOD::FULL);
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);
myBLE.regDisconnCallback(disconnectionCallback);
myChar.charInit(BLE_EX_CHAR_SIZE_B, WRITE, false, false, NULL, 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
//--------------------------- INTERRUPT HANDLERS ------------------------------