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