Erick / Mbed 2 deprecated ICE-F412

Dependencies:   mbed-rtos mbed

Committer:
jmarkel44
Date:
Tue Jan 24 19:05:33 2017 +0000
Revision:
0:61364762ee0e
Port from IAR to Nucleo-F412 board

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jmarkel44 0:61364762ee0e 1 /******************************************************************************
jmarkel44 0:61364762ee0e 2 *
jmarkel44 0:61364762ee0e 3 * File: main.cpp
jmarkel44 0:61364762ee0e 4 * Desciption: Ground Zero
jmarkel44 0:61364762ee0e 5 *
jmarkel44 0:61364762ee0e 6 *****************************************************************************/
jmarkel44 0:61364762ee0e 7 #include <time.h>
jmarkel44 0:61364762ee0e 8 #include <iostream>
jmarkel44 0:61364762ee0e 9
jmarkel44 0:61364762ee0e 10 #include "global.h"
jmarkel44 0:61364762ee0e 11 #include "mbed.h"
jmarkel44 0:61364762ee0e 12 #include "rtos.h"
jmarkel44 0:61364762ee0e 13 #include "ntshell.h"
jmarkel44 0:61364762ee0e 14 #include "version.h"
jmarkel44 0:61364762ee0e 15 #include "AnalyticsLogger.h"
jmarkel44 0:61364762ee0e 16 #include "ConfigurationHandler.h"
jmarkel44 0:61364762ee0e 17 #include "ControlTask.h"
jmarkel44 0:61364762ee0e 18 #include "DataHandler.h"
jmarkel44 0:61364762ee0e 19 #include "ModbusMaster.h"
jmarkel44 0:61364762ee0e 20 #include "OutputTask.h"
jmarkel44 0:61364762ee0e 21 #include "ConfigFs.h"
jmarkel44 0:61364762ee0e 22 #include "rtc.h"
jmarkel44 0:61364762ee0e 23 #include "mfs.h"
jmarkel44 0:61364762ee0e 24
jmarkel44 0:61364762ee0e 25 // threading data
jmarkel44 0:61364762ee0e 26 osThreadId mainThreadId = NULL;
jmarkel44 0:61364762ee0e 27
jmarkel44 0:61364762ee0e 28 Thread *GLOBAL_analyticsLogger_thread = NULL;
jmarkel44 0:61364762ee0e 29 Thread *GLOBAL_modbusMaster_thread = NULL;
jmarkel44 0:61364762ee0e 30 Thread *GLOBAL_BLE_thread = NULL;
jmarkel44 0:61364762ee0e 31 Thread *GLOBAL_CDH_thread = NULL;
jmarkel44 0:61364762ee0e 32 Thread *GLOBAL_configHandler_thread = NULL;
jmarkel44 0:61364762ee0e 33 Thread *GLOBAL_controlTask_thread = NULL;
jmarkel44 0:61364762ee0e 34 Thread *GLOBAL_outputTask_thread = NULL;
jmarkel44 0:61364762ee0e 35 Thread *GLOBAL_dataHandler_thread = NULL;
jmarkel44 0:61364762ee0e 36
jmarkel44 0:61364762ee0e 37 // mailboxes
jmarkel44 0:61364762ee0e 38 Mail<ConfigMessage_t, 16> ConfigHandlerMailBox;
jmarkel44 0:61364762ee0e 39 Mail<ModbusMasterReq_t, 2> ModbusMasterMailBox;
jmarkel44 0:61364762ee0e 40 Mail<OutputControlMsg_t, 16> OutputMasterMailBox;
jmarkel44 0:61364762ee0e 41 Mail<AnalyticsLoggerReq_t, 16> AnalyticsLoggerMailBox;
jmarkel44 0:61364762ee0e 42 Mail<BLEHandlerReq_t, 1> BLEHandlerMailBox;
jmarkel44 0:61364762ee0e 43
jmarkel44 0:61364762ee0e 44 // bootup sequence signals
jmarkel44 0:61364762ee0e 45 int sig_output_continue = 0x1; // allow the output handler to start
jmarkel44 0:61364762ee0e 46 int sig_config_continue = 0x2; // allow the config handler to start
jmarkel44 0:61364762ee0e 47 int sig_control_continue = 0x3; // allow the control task to start
jmarkel44 0:61364762ee0e 48
jmarkel44 0:61364762ee0e 49 // some digitals...for fun, whilst developing
jmarkel44 0:61364762ee0e 50 DigitalOut led1(LED1);
jmarkel44 0:61364762ee0e 51 DigitalOut led2(LED2);
jmarkel44 0:61364762ee0e 52 DigitalOut led3(LED3);
jmarkel44 0:61364762ee0e 53
jmarkel44 0:61364762ee0e 54 DigitalIn rClk(D7);
jmarkel44 0:61364762ee0e 55
jmarkel44 0:61364762ee0e 56 // console communication settings
jmarkel44 0:61364762ee0e 57 Serial console(USBTX, USBRX, 115200);
jmarkel44 0:61364762ee0e 58
jmarkel44 0:61364762ee0e 59 I2C* i2c;
jmarkel44 0:61364762ee0e 60
jmarkel44 0:61364762ee0e 61 ConfigFs EEP_FileSystem;
jmarkel44 0:61364762ee0e 62 ConfigFs *GLOBAL_mdot = &EEP_FileSystem;
jmarkel44 0:61364762ee0e 63
jmarkel44 0:61364762ee0e 64 //MTSLog *GLOBAL_logger;
jmarkel44 0:61364762ee0e 65
jmarkel44 0:61364762ee0e 66 #undef TODO_ICE // move this, doesn't belong here...
jmarkel44 0:61364762ee0e 67 // store modbus register information
jmarkel44 0:61364762ee0e 68 std::map<std::string,ModbusRegister> ModbusRegisterMap;
jmarkel44 0:61364762ee0e 69 std::map<std::string,RegisterValue> RegisterValueMap;
jmarkel44 0:61364762ee0e 70 std::map<std::string,SimulateInput> SimulateInputMap;
jmarkel44 0:61364762ee0e 71
jmarkel44 0:61364762ee0e 72 // local prototypes
jmarkel44 0:61364762ee0e 73 static void banner(void);
jmarkel44 0:61364762ee0e 74
jmarkel44 0:61364762ee0e 75 /*****************************************************************************
jmarkel44 0:61364762ee0e 76 * Function: timestamp_boot_record()
jmarkel44 0:61364762ee0e 77 * Description: timestamp the boot record with current time
jmarkel44 0:61364762ee0e 78 *
jmarkel44 0:61364762ee0e 79 * @param none
jmarkel44 0:61364762ee0e 80 * @return none
jmarkel44 0:61364762ee0e 81 *****************************************************************************/
jmarkel44 0:61364762ee0e 82 static void timestamp_boot_record(void)
jmarkel44 0:61364762ee0e 83 {
jmarkel44 0:61364762ee0e 84 char time_string[80];
jmarkel44 0:61364762ee0e 85 time_t curr_sec;
jmarkel44 0:61364762ee0e 86 struct tm *ts;
jmarkel44 0:61364762ee0e 87
jmarkel44 0:61364762ee0e 88 curr_sec = time(0);
jmarkel44 0:61364762ee0e 89 ts = localtime(&curr_sec);
jmarkel44 0:61364762ee0e 90 strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", ts);
jmarkel44 0:61364762ee0e 91
jmarkel44 0:61364762ee0e 92 GLOBAL_mdot->saveUserFile("boot.time", time_string, sizeof(time_string));
jmarkel44 0:61364762ee0e 93 return;
jmarkel44 0:61364762ee0e 94 }
jmarkel44 0:61364762ee0e 95
jmarkel44 0:61364762ee0e 96
jmarkel44 0:61364762ee0e 97 /**
jmarkel44 0:61364762ee0e 98 * @brief the main software entry point
jmarkel44 0:61364762ee0e 99 *
jmarkel44 0:61364762ee0e 100 * @param none
jmarkel44 0:61364762ee0e 101 * @return 0
jmarkel44 0:61364762ee0e 102 */
jmarkel44 0:61364762ee0e 103
jmarkel44 0:61364762ee0e 104
jmarkel44 0:61364762ee0e 105
jmarkel44 0:61364762ee0e 106 int main(void)
jmarkel44 0:61364762ee0e 107 {
jmarkel44 0:61364762ee0e 108 // light 'em up! -- remove this once we go to a real board!
jmarkel44 0:61364762ee0e 109 led1 = led2 = led3 = 1;
jmarkel44 0:61364762ee0e 110
jmarkel44 0:61364762ee0e 111 struct tm rtc_time;
jmarkel44 0:61364762ee0e 112 time_t curr_sec;
jmarkel44 0:61364762ee0e 113 int year = 0;
jmarkel44 0:61364762ee0e 114
jmarkel44 0:61364762ee0e 115 mainThreadId = osThreadGetId();
jmarkel44 0:61364762ee0e 116
jmarkel44 0:61364762ee0e 117 i2c = &i2c_instance;
jmarkel44 0:61364762ee0e 118
jmarkel44 0:61364762ee0e 119 // intialize the real-time clock
jmarkel44 0:61364762ee0e 120 rtc_init();
jmarkel44 0:61364762ee0e 121
jmarkel44 0:61364762ee0e 122 rtc_get_time(&year, &rtc_time.tm_mon, &rtc_time.tm_mday, &rtc_time.tm_hour, &rtc_time.tm_min, &rtc_time.tm_sec);
jmarkel44 0:61364762ee0e 123 rtc_time.tm_mon = rtc_time.tm_mon - 1;
jmarkel44 0:61364762ee0e 124 rtc_time.tm_year = year - 1900;
jmarkel44 0:61364762ee0e 125 curr_sec = mktime( &rtc_time );
jmarkel44 0:61364762ee0e 126
jmarkel44 0:61364762ee0e 127 set_time(curr_sec);
jmarkel44 0:61364762ee0e 128
jmarkel44 0:61364762ee0e 129 timestamp_boot_record();
jmarkel44 0:61364762ee0e 130
jmarkel44 0:61364762ee0e 131 mainThreadId = osThreadGetId();
jmarkel44 0:61364762ee0e 132
jmarkel44 0:61364762ee0e 133 // display the startup banner
jmarkel44 0:61364762ee0e 134 banner();
jmarkel44 0:61364762ee0e 135
jmarkel44 0:61364762ee0e 136 // the Modbus master task
jmarkel44 0:61364762ee0e 137 Thread modbusMaster_thread(ModbusMaster, NULL, osPriorityHigh, MODBUS_MASTER_STACK_SIZE, NULL);
jmarkel44 0:61364762ee0e 138 osSignalWait(sig_output_continue, osWaitForever);
jmarkel44 0:61364762ee0e 139
jmarkel44 0:61364762ee0e 140 // the Output task
jmarkel44 0:61364762ee0e 141 Thread outputTask_thread(OutputTask, NULL, osPriorityNormal, OUTPUT_TASK_STACK_SIZE, NULL);
jmarkel44 0:61364762ee0e 142 osSignalWait(sig_config_continue, osWaitForever);
jmarkel44 0:61364762ee0e 143
jmarkel44 0:61364762ee0e 144 // the Configuration Handler task
jmarkel44 0:61364762ee0e 145 Thread configHandler_thread(ConfigurationHandler, NULL, osPriorityNormal, CONFIG_HANDLER_STACK_SIZE, NULL);
jmarkel44 0:61364762ee0e 146 osSignalWait(sig_control_continue, osWaitForever);
jmarkel44 0:61364762ee0e 147
jmarkel44 0:61364762ee0e 148 // the Control task
jmarkel44 0:61364762ee0e 149 Thread controlTask_thread(ControlTask, NULL, osPriorityNormal, CONTROL_TASK_STACK_SIZE, NULL);
jmarkel44 0:61364762ee0e 150
jmarkel44 0:61364762ee0e 151 // the Data Handler
jmarkel44 0:61364762ee0e 152 Thread dataHandler_thread(DataHandler, NULL, osPriorityNormal, DATA_HANDLER_STACK_SIZE, NULL);
jmarkel44 0:61364762ee0e 153
jmarkel44 0:61364762ee0e 154 // the Analytics Logger task
jmarkel44 0:61364762ee0e 155 Thread analyticsLoggerThread(AnalyticsLogger, NULL, osPriorityHigh, ANALYTICS_LOGGER_STACK_SIZE, NULL);
jmarkel44 0:61364762ee0e 156
jmarkel44 0:61364762ee0e 157 GLOBAL_analyticsLogger_thread = &analyticsLoggerThread;
jmarkel44 0:61364762ee0e 158 GLOBAL_modbusMaster_thread = &modbusMaster_thread;
jmarkel44 0:61364762ee0e 159 GLOBAL_configHandler_thread = &configHandler_thread;
jmarkel44 0:61364762ee0e 160 GLOBAL_controlTask_thread = &controlTask_thread;
jmarkel44 0:61364762ee0e 161 GLOBAL_outputTask_thread = &outputTask_thread;
jmarkel44 0:61364762ee0e 162 GLOBAL_dataHandler_thread = &dataHandler_thread;
jmarkel44 0:61364762ee0e 163
jmarkel44 0:61364762ee0e 164 Thread::wait(2500);
jmarkel44 0:61364762ee0e 165
jmarkel44 0:61364762ee0e 166 // start the command shell -- this will loop forever
jmarkel44 0:61364762ee0e 167 console.printf("\r\nInvoking the command shell\n\r\n");
jmarkel44 0:61364762ee0e 168 ntshell_execute(&ntshell, func_read, func_write, func_cb_ntshell);
jmarkel44 0:61364762ee0e 169 }
jmarkel44 0:61364762ee0e 170
jmarkel44 0:61364762ee0e 171 /**
jmarkel44 0:61364762ee0e 172 * @brief the bootup banner
jmarkel44 0:61364762ee0e 173 *
jmarkel44 0:61364762ee0e 174 * @param none
jmarkel44 0:61364762ee0e 175 * @return none
jmarkel44 0:61364762ee0e 176 */
jmarkel44 0:61364762ee0e 177 static void banner(void)
jmarkel44 0:61364762ee0e 178 {
jmarkel44 0:61364762ee0e 179 struct tm *ts;
jmarkel44 0:61364762ee0e 180 time_t curr_sec;
jmarkel44 0:61364762ee0e 181 char time_string[80];
jmarkel44 0:61364762ee0e 182
jmarkel44 0:61364762ee0e 183 curr_sec = time(0);
jmarkel44 0:61364762ee0e 184 ts = localtime(&curr_sec);
jmarkel44 0:61364762ee0e 185
jmarkel44 0:61364762ee0e 186 strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", ts);
jmarkel44 0:61364762ee0e 187
jmarkel44 0:61364762ee0e 188 printf("\n\n\r\nProject: ICE v%d.%d.%d (%s)\n",
jmarkel44 0:61364762ee0e 189 MAJOR_VERSION_NUMBER,
jmarkel44 0:61364762ee0e 190 MINOR_VERSION_NUMBER,
jmarkel44 0:61364762ee0e 191 PATCH_VERSION_NUMBER,
jmarkel44 0:61364762ee0e 192 PROJECT_CODE_NAME);
jmarkel44 0:61364762ee0e 193
jmarkel44 0:61364762ee0e 194 printf("\rThe Intelligent Connected Experience\n");
jmarkel44 0:61364762ee0e 195 printf("\rCopyright 2017 Nalco Water, an Ecolab Company\n");
jmarkel44 0:61364762ee0e 196
jmarkel44 0:61364762ee0e 197 #ifdef __ICCARM__
jmarkel44 0:61364762ee0e 198 printf("\rToolchain: IAR\n");
jmarkel44 0:61364762ee0e 199 #else
jmarkel44 0:61364762ee0e 200 printf("\rToolchain: MBED\n");
jmarkel44 0:61364762ee0e 201 #endif
jmarkel44 0:61364762ee0e 202
jmarkel44 0:61364762ee0e 203 printf("\r _________ _______ _______ \n");
jmarkel44 0:61364762ee0e 204 printf("\r \\__ __/( ____ \\( ____ \\ \n");
jmarkel44 0:61364762ee0e 205 printf("\r ) ( | ( \\/| ( \\/ \n");
jmarkel44 0:61364762ee0e 206 printf("\r | | | | | (__ \n");
jmarkel44 0:61364762ee0e 207 printf("\r | | | | | __) \n");
jmarkel44 0:61364762ee0e 208 printf("\r | | | | | ( \n");
jmarkel44 0:61364762ee0e 209 printf("\r ___) (___| (____/\\| (____/\\ \n");
jmarkel44 0:61364762ee0e 210 printf("\r \\_______/(_______/(_______/ \n");
jmarkel44 0:61364762ee0e 211
jmarkel44 0:61364762ee0e 212 printf("\r\nCurrent time is: %s\r\n", time_string);
jmarkel44 0:61364762ee0e 213 }