ISSDK  1.7
IoT Sensing Software Development Kit
status.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  * of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  * list of conditions and the following disclaimer in the documentation and/or
16  * other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*! \file status.c
36  \brief Application-specific status subsystem
37 
38  Applications may change how they choose to display status information.
39  The default implementation here uses LEDs on NXP Freedom boards.
40  You may swap out implementations as long as the "Required" methods and states
41  are retained.
42 */
43 
44 #include "board.h" // KSDK HAL
45 #ifndef CPU_LPC54114J256BD64_cm4
46 #include "fsl_port.h" // KSDK Port Module Interface
47 #endif
48 #include "sensor_fusion.h" // Sensor fusion structures and functions
49 #include "drivers.h" // Common driver header file
50 #include "status.h" // Header for this .c file
51 
52 // bit-field definitions
53 #define N 0x00 // No color
54 #define R 0x04 // Red LED
55 #define G 0x02 // Green LED
56 #define B 0x01 // Blue LED
57 
58 // set RGB LED as a function of bit-field values
59 void ssSetLeds(int8_t RGB)
60 {
61  if (RGB & R)
62  LED_RED_ON();
63  else
64  LED_RED_OFF();
65  if (RGB & G)
66  LED_GREEN_ON();
67  else
68  LED_GREEN_OFF();
69  if (RGB & B)
70  LED_BLUE_ON();
71  else
72  LED_BLUE_OFF();
73 }
74 
75 // Do an immediate status update
77 {
78  pStatus->status = status;
79  pStatus->next = status;
80 
81  uint8_t blink = false;
82  uint8_t RGB = N;
83 
84  // This is where we actually change the visual indicator
85  // We are not using the blue LED because it is not available on some
86  // board combinations.
87  switch (status)
88  {
89  case INITIALIZING: // solid GREEN
90  RGB = G;
91  break;
92  case NORMAL: // blinking GREEN
93  RGB = G;
94  blink = true;
95  break;
96  case LOWPOWER: // blinking YELLOW
97  RGB = R|G;
98  blink = true;
99  break;
100  case SOFT_FAULT: // solid RED (usually momentary)
101  case HARD_FAULT: // solid RED
102  RGB = R;
103  break;
104  default: // default = off;
105  RGB = N;
106  }
107 
108  if ((!blink) | (status != pStatus->previous))
109  {
110  ssSetLeds(RGB);
111  pStatus->toggle = true;
112  }
113  else
114  {
115  if (pStatus->toggle)
116  {
117  ssSetLeds(N);
118  }
119  else
120  {
121  ssSetLeds(RGB);
122  }
123 
124  pStatus->toggle = !pStatus->toggle;
125  }
126  while (status == HARD_FAULT) ; // Never return on hard fault
127  // while (status == SOFT_FAULT) ; // DEBUG ONLY Never return on soft fault
128 }
129 
130 // Unit test for status sub-system
131 void ssTest(StatusSubsystem *pStatus)
132 {
133  switch (pStatus->status)
134  {
135  case OFF:
136  ssSetStatusNow(pStatus, INITIALIZING);
137  break;
138  case INITIALIZING:
139  ssSetStatusNow(pStatus, LOWPOWER);
140  break;
141  case LOWPOWER:
142  ssSetStatusNow(pStatus, NORMAL);
143  break;
144  case NORMAL:
146  break;
147  case RECEIVING_WIRED:
149  break;
150  case RECEIVING_WIRELESS:
151  ssSetStatusNow(pStatus, SOFT_FAULT);
152  break;
153  case SOFT_FAULT:
154  ssSetStatusNow(pStatus, HARD_FAULT);
155  break;
156  case HARD_FAULT:
157  ssSetStatusNow(pStatus, OFF);
158  break;
159  }
160 }
161 
162 // undefine these just in case some other library needs them
163 #undef N
164 #undef R
165 #undef G
166 #undef B
167 
168 
169 // queue up a status change (which will take place at the next updateStatus)
171 {
172  pStatus->next = status;
173 }
174 
175 // promote any previously queued status update
177 {
178  pStatus->previous = pStatus->status;
179  ssSetStatusNow(pStatus, pStatus->next);
180 }
181 
182 // make an immediate update to the system status
184 {
185  pStatus->next = status;
186  ssUpdateStatus(pStatus);
187 }
188 
189 /// initializeStatusSubsystem() should be called once at startup to initialize the
190 /// data structure and to put hardware into the proper state for communicating status.
192 {
193  pStatus->set = ssSetStatus;
194  pStatus->queue = ssQueueStatus;
195  pStatus->update = ssUpdateStatus;
196  pStatus->test = ssTest;
197  pStatus->previous = OFF;
198  pStatus->set(pStatus, OFF);
199  pStatus->queue(pStatus, OFF);
200  pStatus->toggle = false;
201 
202  /* Un-gate the port clocks */
203  CLOCK_EnableClock(RED_LED.clockName);
204  CLOCK_EnableClock(GREEN_LED.clockName);
205  //CLOCK_EnableClock(BLUE_LED.clockName);
206 #ifndef CPU_LPC54114J256BD64_cm4
207  // Not needed for the LPC54114 (done elsewhere)
208  // Led pin mux Configuration
210  kPORT_MuxAsGpio);
212  kPORT_MuxAsGpio);
213 #endif
214  /* set initial values */
217 }
#define LED_BLUE_ON()
Definition: board.h:130
fusion_status_t previous
Previous status state - fusion_status_t is defined in sensor_fusion.h.
Definition: status.h:50
Initializing sensors and algorithms.
void ssSetStatusNow(StatusSubsystem *pStatus, fusion_status_t status)
Definition: status.c:76
ssSetStatus_t * set
change status immediately - no delay
Definition: status.h:54
Receiving commands over wireless interface (momentary)
#define LED_RED_INIT(output)
Definition: board.h:95
void ssTest(StatusSubsystem *pStatus)
Definition: status.c:131
#define G
Definition: status.c:55
int32_t status
void initializeStatusSubsystem(StatusSubsystem *pStatus)
Definition: status.c:191
#define B
Definition: status.c:56
#define LED_GREEN_INIT(output)
Definition: board.h:110
These are the state definitions for the status subsystem.
#define LED_RED_ON()
Definition: board.h:100
void ssQueueStatus(StatusSubsystem *pStatus, fusion_status_t status)
Definition: status.c:170
StatusSubsystem() provides an object-like interface for communicating status to the user...
Definition: status.h:48
Recoverable FAULT = something went wrong, but we can keep going.
ssUpdateStatus_t * update
make pending status active/visible
Definition: status.h:56
void ssSetLeds(int8_t RGB)
Definition: status.c:59
gpioHandleKSDK_t GREEN_LED
Definition: frdm_k64f.c:214
uint8_t toggle
This implementation can change LED color and have either solid/toggle.
Definition: status.h:59
fusion_status_t status
Current status.
Definition: status.h:51
#define R
Definition: status.c:54
#define LED_BLUE_OFF()
Definition: board.h:133
#define BOARD_LED_GREEN_GPIO_PIN
Definition: board.h:90
The sensor_fusion.h file implements the top level programming interface.
#define LED_GREEN_ON()
Definition: board.h:115
Receiving commands over wired interface (momentary)
Running in reduced power mode.
void ssUpdateStatus(StatusSubsystem *pStatus)
Definition: status.c:176
Provides function prototypes for driver level interfaces.
ssSetStatus_t * queue
queue status change for next regular interval
Definition: status.h:55
#define LOGIC_LED_OFF
Definition: board.h:84
ssUpdateStatus_t * test
unit test which simply increments to next state
Definition: status.h:57
#define N
Definition: status.c:53
#define LED_RED_OFF()
Definition: board.h:103
#define BOARD_LED_RED_GPIO_PIN
Definition: board.h:87
clock_ip_name_t clockName
Definition: gpio_driver.h:82
Non-recoverable FAULT = something went very wrong.
Application-specific status subsystem.
void ssSetStatus(StatusSubsystem *pStatus, fusion_status_t status)
Definition: status.c:183
gpioHandleKSDK_t RED_LED
Definition: frdm_k64f.c:207
#define LED_GREEN_OFF()
Definition: board.h:118
#define BOARD_LED_RED_GPIO_PORT
Definition: board.h:86
fusion_status_t
Application-specific serial communications system.
Operation is Nominal.
#define BOARD_LED_GREEN_GPIO_PORT
Definition: board.h:89
fusion_status_t next
Pending status change.
Definition: status.h:52