EVB-2/IS_EVB-2/src/main.cpp
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright 2014-2019 Inertial Sense, Inc. - http://inertialsense.com
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
7 
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9 
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT, IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #include <asf.h>
14 #include <string>
15 #include <stream_buffer.h>
16 
17 #include "sd_mmc_mem.h"
18 #include "wifi.h"
19 #include "xbee.h"
20 #include "globals.h"
21 #include "communications.h"
22 #include "user_interface.h"
23 #include "sd_card_logger.h"
24 #include "control_law.h"
25 
26 #undef printf
27 #define printf(...)
28 #define printf_mutex(...)
29 
30 // RTOS Task Configuration
31 #define TASK_COMM_PERIOD_MS 1
32 #define TASK_LOGGER_PERIOD_MS 1
33 #define TASK_WIFI_PERIOD_MS 10
34 #define TASK_MAINT_PERIOD_MS 10
35 #define TASK_MAINT_SLOW_SEC_PERIOD_MS 1000
36 
37 // #define TASK_COMM_STACK_SIZE (4096/sizeof(portSTACK_TYPE))
38 #define TASK_COMM_STACK_SIZE (8192/sizeof(portSTACK_TYPE))
39 #define TASK_MAINT_STACK_SIZE (4096/sizeof(portSTACK_TYPE))
40 // #define TASK_LOGGER_STACK_SIZE (4096/sizeof(portSTACK_TYPE))
41 #define TASK_LOGGER_STACK_SIZE (8192/sizeof(portSTACK_TYPE))
42 #define TASK_WIFI_STACK_SIZE (2048/sizeof(portSTACK_TYPE))
43 
44 #define TASK_COMM_PRIORITY (tskIDLE_PRIORITY + 4) // Highest
45 #define TASK_LOGGER_PRIORITY (tskIDLE_PRIORITY + 3)
46 #define TASK_WIFI_PRIORITY (tskIDLE_PRIORITY + 2)
47 #define TASK_MAINT_PRIORITY (tskIDLE_PRIORITY + 1)
48 
49 
50 #include "CAN.h"
51 
52 static void vTaskComm(void *pvParameters)
53 {
54  UNUSED(pvParameters);
56 
57  static is_comm_instance_t comm;
58  static uint8_t comm_buffer[PKT_BUF_SIZE];
59  is_comm_init(&comm, comm_buffer, PKT_BUF_SIZE);
60 
61 #ifdef CONF_BOARD_CAN_TEST
62  //if(/*g_can_test == CAN_TEST_MASTER - or something like that*/ 1)
63  //mcan_test_master();
64  if(/*g_can_test == CAN_TEST_SLAVE - or something like that*/ 1)
66 #endif
67 
68  // Start USB CDC after everything is running
69 #ifdef USB_PORT_NUM
70  serInit(USB_PORT_NUM, 0, NULL, 0);
71 #endif
72 
73  vTaskDelay(200);
75  while(1)
76  {
77  vTaskDelay(task->periodMs);
78 
80 
81 #ifdef ENABLE_WDT
82  // Feed Watchdog to prevent reset
84 #endif
85 
86  // Turn off communications LEDs - first thing after vTaskDelay()
89 
90  // Forward data between communications ports
91  step_com_bridge(comm);
92  velocity_control(comm);
93 
94  // Read buttons and update LEDs
96 
98  // Suggested USER CODE Section
99  // Update period: 1ms (Adjust by changing TASK_COMM_PERIOD_MS)
100  // Priority: high
101  //
102  // Ensure code added here does not run longer than 1ms. Consider
103  // adding code to vTaskMaint if it runs longer than 1ms and does not
104  // require a high priority.
105 
106  // Add code here...
108  }
109 }
110 
111 
115 static void vTaskLogger(void *pvParameters)
116 {
117  UNUSED(pvParameters);
119 
120  static is_comm_instance_t comm;
121  static uint8_t comm_buffer[PKT_BUF_SIZE];
122  is_comm_init(&comm, comm_buffer, PKT_BUF_SIZE);
123 
124  uINS_stream_stop_all(comm);
125 
126  vTaskDelay(200);
127  LED_LOG_OFF();
128  vTaskDelay(800);
129 
130 #if STREAM_INS_FOR_TIME_SYNC // Stream INS message on startup. Necessary to update EVB RTC for correct data log date and time.
131  //uINS0_stream_stop_all(comm);
132  //uINS0_stream_enable_std(comm);
133 #endif
134 
135  cISLogger logger;
136 
137  for (;;)
138  {
139  vTaskDelay(task->periodMs);
140 
141  step_logger_control(logger, comm);
142 
143  // Ready uINS data from com task. Log to file.
144  log_uINS_data(logger, comm);
145 
146  // Mount/unmount SD card
148 
149  update_led_log();
150  }
151 }
152 
153 
157 static void vTaskMaint(void *pvParameters)
158 {
159  UNUSED(pvParameters);
161  uint32_t m2sPeriodMs = 0;
162 
163  for (;;)
164  {
165  vTaskDelay(task->periodMs);
166 
168  // Fast Maintenance - 10ms period
169 
171  // Suggested USER CODE Section
172  // Update period: 10ms (Adjust by changing TASK_MAINT_PERIOD_MS)
173  // Priority: low
174  //
175  // Consider adding code to vTaskComm if it needs to run faster than every
176  // 10ms or requires a higher priority.
177 
178  // Add code here...
180 
181 
182 
184  // Slow Maintenance - 1000ms period
186  {
187  continue;
188  }
189  m2sPeriodMs = 0;
190 
191  // Sync local time from uINS
193 
195 
196  // Update RTOS stats
197  if (g_enRtosStats)
198  {
200  }
201 
203  { // uINS bootloader mode enabled
205  { // Automatically disable uINS after 3 minutes
207  }
208  }
209 
211  // Suggested USER CODE Section
212  // Update period: 1000ms (Adjust by changing TASK_MAINT_SUB_TASK_PERIOD_MS)
213  // Priority: low
214  //
215  // Consider adding code to vTaskComm if it needs to run faster than every
216  // 10ms or requires a higher priority.
217 
218  // Add code here...
220  }
221 }
222 
223 
224 int main(void)
225 {
226  //XDMAC channel interrupt enables do not get cleared by a software reset. Clear them before they cause issues.
227  XDMAC->XDMAC_GID = 0xFFFFFFFF;
228  for(int i=0;i<XDMACCHID_NUMBER;i++)
229  XDMAC->XDMAC_CHID[i].XDMAC_CID = 0xFFFFFFFF;
230 
231  // Force USB to disconnect. Helps to make sure the USB port starts up correctly when debugging.
232  udc_stop();
233 
234  // Initialize the SAM system
235  board_init();
236 
237  // Init globals and flash parameters
238  globals_init();
239  nvr_init();
240 
241  // Hold config while resetting
243  {
245  }
246 
247  // Init hardware I/O, SD card logger, and communications
248  board_IO_config();
249  init_control();
252 
253  // Create RTOS tasks
256 #ifdef CONF_BOARD_SPI_ATWINC_WIFI // ATWINC WIFI
258 #endif
260  strncpy(g_rtos.task[EVB_TASK_IDLE].name, "IDLE", MAX_TASK_NAME_LEN);
261  strncpy(g_rtos.task[EVB_TASK_TIMER].name, "TIMER", MAX_TASK_NAME_LEN);
263 
264 #ifdef ENABLE_WDT
265  // Setup Watchdog
266  uint32_t timeout_value = wdt_get_timeout_value(1000000, BOARD_FREQ_SLCK_XTAL); //Timeout in us, configured for 1 second.
267  wdt_init(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT, timeout_value, timeout_value);
268 #endif
269 
270  // Start the scheduler
271  printf("Starting FreeRTOS\n\r");
273 
274  // Will only get here if there was insufficient memory to create the idle task.
275  return 0;
276 }
void uINS_stream_stop_all(is_comm_instance_t &comm)
void init_control(void)
Definition: control_law.cpp:20
void vTaskStartScheduler(TaskHandle_t *idleTaskHandle, TaskHandle_t *timerTaskHandle) PRIVILEGED_FUNCTION
Definition: tasks.c:1901
void wdt_restart(Wdt *p_wdt)
Restart the watchdog timer.
Definition: wdt.c:129
#define LED_LOG_OFF()
Definition: user_board.h:270
#define LED_OFF(led)
Definition: user_board.h:235
uint32_t time_msec(void)
Definition: d_time.c:95
#define UNUSED(v)
Marking v as a unused parameter or value.
Definition: compiler.h:86
void board_init(void)
This function initializes the board target resources.
Definition: init.c:556
void sd_card_maintenance(void)
static void vTaskLogger(void *pvParameters)
RTOS logger task.
int serInit(int serialNum, uint32_t baudRate, sam_usart_opt_t *options, uint32_t *overrunStatus)
Initialize serial port with specific USART/UART and DMA settings. If not NULL, the overrun status wil...
Definition: d_usartDMA.c:1560
evb_rtos_info_t g_rtos
Definition: globals.c:27
#define printf(...)
void globals_init(void)
Definition: globals.c:38
#define XDMAC
(XDMAC ) Base Address
Definition: same70j19.h:520
bool g_enRtosStats
Definition: globals.c:35
#define PKT_BUF_SIZE
Definition: ISComm.h:98
void board_IO_config(void)
Definition: init.c:393
void mcan_test_slave(void)
Definition: CAN.cpp:215
uint32_t wdt_get_timeout_value(uint32_t ul_us, uint32_t ul_sclk)
Get counter value or permitted range value of watchdog timer from the desired timeout period (in us)...
Definition: wdt.c:88
rtos_task_t task[UINS_RTOS_NUM_TASKS]
Definition: data_sets.h:3335
#define NULL
Definition: nm_bsp.h:52
#define LED_INS_TXD_PIN
Definition: user_board.h:280
void udc_stop(void)
Stop the USB Device stack.
Definition: udc.c:358
void communications_init(void)
#define TASK_WIFI_PRIORITY
#define WDT
(WDT ) Base Address
Definition: same70j19.h:535
void step_user_interface()
void velocity_control(is_comm_instance_t &comm)
Definition: control_law.cpp:31
uint32_t handle
Definition: data_sets.h:3319
static bool ioport_get_pin_level(ioport_pin_t pin)
Get current value of an IOPORT pin, which has been configured as an input.
Definition: ioport.h:301
void nvr_slow_maintenance(void)
Definition: globals.c:311
#define TASK_COMM_STACK_SIZE
#define EVB2_CB_PRESET_DEFAULT
Definition: data_sets.h:3114
int main(void)
void nvr_init(void)
Definition: globals.c:290
void refresh_CFG_LED(void)
Definition: init.c:377
uint8_t cbPreset
Definition: data_sets.h:3020
#define TASK_LOGGER_PRIORITY
#define TASK_MAINT_SLOW_SEC_PERIOD_MS
uint32_t g_uInsBootloaderEnableTimeMs
Definition: globals.c:34
#define WDT_MR_WDDBGHLT
(WDT_MR) Watchdog Debug Halt
uint32_t g_comm_time_ms
Definition: globals.c:32
#define TASK_MAINT_PERIOD_MS
void * TaskHandle_t
Definition: task.h:62
static void vTaskMaint(void *pvParameters)
RTOS maintenance task.
#define BUTTON_CFG_PIN
Definition: user_board.h:289
void rtos_monitor(int numRtosTasks)
Definition: rtos.c:64
#define TASK_COMM_PERIOD_MS
#define USB_PORT_NUM
Definition: user_board.h:192
void is_comm_init(is_comm_instance_t *instance, uint8_t *buffer, int bufferSize)
Definition: ISComm.c:185
#define MAX_TASK_NAME_LEN
Definition: data_sets.h:3286
uint32_t periodMs
Definition: data_sets.h:3301
evb_flash_cfg_t * g_flashCfg
Definition: globals.c:22
void update_led_log(void)
#define LED_INS_RXD_PIN
Definition: user_board.h:279
#define TASK_LOGGER_PERIOD_MS
void vTaskWiFi(void *pvParameters)
Definition: wifi.c:395
void step_logger_control(cISLogger &logger, is_comm_instance_t &comm)
void sd_card_logger_init(void)
bool g_loggerEnabled
Definition: globals.c:33
#define TASK_LOGGER_STACK_SIZE
void step_com_bridge(is_comm_instance_t &comm)
#define XDMACCHID_NUMBER
Xdmac hardware registers.
void vTaskDelay(const TickType_t xTicksToDelay) PRIVILEGED_FUNCTION
void time_sync_from_uINS(void)
#define TASK_WIFI_PERIOD_MS
static void vTaskComm(void *pvParameters)
#define TASK_WIFI_STACK_SIZE
Autogenerated API include file for the Atmel Software Framework (ASF)
CTRL_ACCESS interface for common SD/MMC stack.
void wdt_init(Wdt *p_wdt, uint32_t ul_mode, uint16_t us_counter, uint16_t us_delta)
Initialize watchdog timer with the given mode.
Definition: wdt.c:110
#define TASK_COMM_PRIORITY
#define WDT_MR_WDRSTEN
(WDT_MR) Watchdog Reset Enable
char name[MAX_TASK_NAME_LEN]
Definition: data_sets.h:3292
#define TASK_MAINT_STACK_SIZE
#define BOARD_FREQ_SLCK_XTAL
Definition: same70/osc.h:57
int createTask(int index, pdTASK_CODE pxTaskCode, const char *const pcName, unsigned short usStackDepth, void *pvParameters, unsigned portBASE_TYPE uxPriority, portTickType xTimeIncrement)
Definition: rtos.c:30
void log_uINS_data(cISLogger &logger, is_comm_instance_t &comm)
#define TASK_MAINT_PRIORITY


inertial_sense_ros
Author(s):
autogenerated on Sat Sep 19 2020 03:19:04