ISSDK  1.7
IoT Sensing Software Development Kit
control_lpc.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 control_lpc.c
36  \brief Defines control sub-system for LPC54114
37 
38  This file contains a USART implementation of the control subsystem. The
39  command interpreter and streaming functions are contained in two separate
40  files. So you can easily swap those out with only minor changes here.
41 */
42 #include "fsl_debug_console.h"
43 #include "board.h"
44 #include "pin_mux.h"
45 #include "fsl_usart.h"
46 #include "sensor_fusion.h"
47 #include "control.h"
48 
49 // global structures
50 uint8_t sUARTOutputBuffer[256]; // larger than the nominal 124 byte size for outgoing packets
51 
52 // direct access to sfg here is the only place in the entire library where we cannot simply
53 // pass a pointer. This is because it is needed by the UART interrupt handlers. Since this
54 // only occurs here, in a subsystem which is defined to be application dependent, that is
55 // considered acceptable.
57 
58 // Blocking function to write a single byte to a specified UART
59 void myUART_WriteByte(USART_Type *base, uint8_t data)
60 {
61  uint32_t flag = kUSART_TxFifoNotFullFlag & USART_GetStatusFlags(base);
62  while (!flag)
63  {
64  flag = kUSART_TxFifoNotFullFlag & USART_GetStatusFlags(base);
65  }
66  USART_WriteByte(base, data);
67 }
68 
69 // Blocking function pipes specified buffer to both output UARTS
70 int8_t writeControlPort(ControlSubsystem *pComm, uint8_t buffer[], uint16_t nbytes)
71 {
72  uint16_t i;
73  for (i = 0; i < nbytes; i++)
74  {
75  myUART_WriteByte(WIRED_USART, buffer[i]);
76  }
77 
78  return (0);
79 }
80 
82 {
83  uint8_t data;
84  static char iCommandBuffer_B[5] = "~~~~"; // 5 bytes long to include the unused terminating \0
85 
86  /* If new data arrived. */
87  if ((kUSART_RxFifoNotEmptyFlag | kUSART_RxError) & USART_GetStatusFlags(WIRED_USART))
88  {
89  data = USART_ReadByte(WIRED_USART);
90  DecodeCommandBytes(&sfg, iCommandBuffer_B, &data, 1);
91  }
92 }
93 
94 /// Initialize the control subsystem and all related hardware
96  ControlSubsystem *pComm ///< pointer to the control subystem structure
97 )
98 {
99  usart_config_t config;
100  if (pComm)
101  {
102  pComm->DefaultQuaternionPacketType = Q3; // default to simplest algorithm
103  pComm->QuaternionPacketType = Q3; // default to simplest algorithm
104  pComm->AngularVelocityPacketOn = true; // transmit angular velocity packet
105  pComm->DebugPacketOn = true; // transmit debug packet
106  pComm->RPCPacketOn = true; // transmit roll, pitch, compass packet
107  pComm->AltPacketOn = true; // Altitude packet
108  pComm->AccelCalPacketOn = 0;
109  pComm->write = writeControlPort;
110  pComm->stream = CreateAndSendPackets;
111 
112  // attach 12 MHz clock to FLEXCOMM0 (debug console)
113  CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
114 
115  // reset FLEXCOMM for USART
116  RESET_PeripheralReset(kFC0_RST_SHIFT_RSTn);
117 
118  //USART0_InitPins(); // defined in pin_mux.c
119  // config.baudRate_Bps = 115200U;
120  // config.parityMode = kUSART_ParityDisabled;
121  // config.stopBitCount = kUSART_OneStopBit;
122  // config.loopback = false;
123  // config.enableTx = false;
124  // config.enableRx = false;
125 
126  USART_GetDefaultConfig(&config);
127  config.baudRate_Bps = BOARD_DEBUG_UART_BAUDRATE;
128  config.enableTx = true;
129  config.enableRx = true;
130 
131  USART_Init(WIRED_USART, &config, WIRED_USART_CLK_FREQ);
132 
133  // Enable RX interrupt.
134  USART_EnableInterrupts(WIRED_USART, kUSART_RxLevelInterruptEnable | kUSART_RxErrorInterruptEnable);
135  EnableIRQ(WIRED_USART_IRQn);
136 
137  return (0);
138  }
139  else
140  {
141  return (1);
142  }
143 }
volatile quaternion_type QuaternionPacketType
quaternion type transmitted over UART
Definition: control.h:70
uint8_t data[FXLS8962_DATA_SIZE]
writePort_t * write
low level function to write a char buffer to the serial stream
Definition: control.h:76
volatile uint8_t AltPacketOn
flag to enable altitude packet
Definition: control.h:74
he ControlSubsystem encapsulates command and data streaming functions.
Definition: control.h:68
volatile uint8_t AngularVelocityPacketOn
flag to enable angular velocity packet
Definition: control.h:71
#define BOARD_DEBUG_UART_CLK_ATTACH
Definition: board.h:54
The top level fusion structure.
quaternion_type DefaultQuaternionPacketType
default quaternion transmitted at power on
Definition: control.h:69
int8_t initializeControlPort(ControlSubsystem *pComm)
Initialize the control subsystem and all related hardware.
Definition: control_lpc.c:95
void myUART_WriteByte(USART_Type *base, uint8_t data)
Definition: control_lpc.c:59
int8_t writeControlPort(ControlSubsystem *pComm, uint8_t buffer[], uint16_t nbytes)
Definition: control_lpc.c:70
void CreateAndSendPackets(SensorFusionGlobals *sfg, uint8_t *sUARTOutputBuffer)
The sensor_fusion.h file implements the top level programming interface.
volatile uint8_t RPCPacketOn
flag to enable roll, pitch, compass packet
Definition: control.h:73
#define WIRED_USART_CLK_FREQ
Definition: lpc54114.h:109
uint8_t sUARTOutputBuffer[256]
main output buffer defined in control.c
Definition: control_lpc.c:50
streamData_t * stream
function to create packets for serial stream
Definition: control.h:77
void DecodeCommandBytes(SensorFusionGlobals *sfg, char iCommandBuffer[], uint8 sUART_InputBuffer[], uint16 nbytes)
#define WIRED_USART_IRQn
Definition: lpc54114.h:111
#define BOARD_DEBUG_UART_BAUDRATE
Definition: board.h:57
Quaternion derived from 3-axis accel (tilt)
Definition: sensor_fusion.h:75
#define WIRED_USART
Definition: lpc54114.h:107
Defines control sub-system.
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.
volatile int8_t AccelCalPacketOn
variable used to coordinate accelerometer calibration
Definition: control.h:75
void WIRED_USART_IRQHandler(void)
Definition: control_lpc.c:81
volatile uint8_t DebugPacketOn
flag to enable debug packet
Definition: control.h:72