ISSDK  1.7
IoT Sensing Software Development Kit
control_lpsci.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_lpsci.c
36  \brief Defines control sub-system
37 
38  This file contains a Low power UART implementation of the control subsystem.
39  This version is targeted specificially at FRDM-KL25Z, which utilizes a low
40  power uart to drive both the OpenSDA and shield UART connections for
41  FRDM-MULT2-B Bluetooth module.
42 
43  The low power uart utilizes a slightly different interface within KSDK, hence
44  this adaptation.
45 
46  The command interpreter and streaming functions are contained in two separate
47  files. So you can easily swap those out with only minor changes here.
48 */
49 #include "fsl_debug_console.h"
50 #include "board.h"
51 #include "pin_mux.h"
52 #include "fsl_uart.h"
53 #include "fsl_lpsci.h"
54 #include "fsl_port.h"
55 #include "sensor_fusion.h"
56 #include "control.h"
57 
58 #define CONTROL_BAUDRATE 115200 ///< Baudrate to be used for serial communications
59 
60 // global structures
61 uint8_t sUARTOutputBuffer[256]; // larger than the nominal 124 byte size for outgoing packets
62 
63 // direct access to sfg here is the only place in the entire library where we cannot simply
64 // pass a pointer. This is because it is needed by the UART interrupt handlers. Since this
65 // only occurs here, in a subsystem which is defined to be application dependent, that is
66 // considered acceptable.
68 
69 // Blocking function to write a single byte to a specified UART
70 void myUART_WriteByte(UART0_Type *base, uint8_t data)
71 {
72  uint8_t flag = 0;
73  while (!flag)
74  {
75  flag = base->S1 & 0x80;
76  }
77  LPSCI_WriteByte(base, data);
78 }
79 
80 // Blocking function pipes specified buffer to both output UARTS
81 int8_t writeControlPort(ControlSubsystem *pComm, uint8_t buffer[], uint16_t nbytes)
82 {
83  uint16_t i;
84  for (i = 0; i < nbytes; i++)
85  {
86  myUART_WriteByte(CONTROL_UART, buffer[i]);
87  }
88 
89  return (0);
90 }
91 
92 // initialize BlueRadios BR-LE4.0-D2A Bluetooth module
93 // This is required for NXP FRDM-FXS-MULT2-B boards.
94 void BlueRadios_Init(void)
95 {
96  uint16_t ilen; // command string length
97 
98  // transmit "ATSRM,2,0\r" to minimize traffic from the module
99  // command "ATSRM": sets the module response mode which configures how verbose the module will be
100  // 2: response mode at to minimal
101  // 0: disconnected mode is command mode
102  // \r: carriage return escape sequence
103  strcpy((char *)sUARTOutputBuffer, "ATSRM,2,0\r");
104  ilen = strlen((char *)sUARTOutputBuffer);
105  writeControlPort(NULL, sUARTOutputBuffer, ilen);
106  return;
107 }
108 
110 {
111  uint8_t data;
112  static char iCommandBuffer_A[5] = "~~~~"; // 5 bytes long to include the unused terminating \0
113 
114  sfg.setStatus(&sfg, RECEIVING_WIRED);
115 
116  if ((kLPSCI_RxDataRegFullFlag)&LPSCI_GetStatusFlags(CONTROL_UART))
117  {
118  data = LPSCI_ReadByte(CONTROL_UART);
119  DecodeCommandBytes(&sfg, iCommandBuffer_A, &data, 1);
120  }
121 
122 }
123 
124 /// Initialize the control subsystem and all related hardware
126  ControlSubsystem *pComm ///< pointer to the control subystem structure
127 )
128 {
129  lpsci_config_t config;
130  if (pComm)
131  {
132  pComm->DefaultQuaternionPacketType = Q3; // default to simplest algorithm
133  pComm->QuaternionPacketType = Q3; // default to simplest algorithm
134  pComm->AngularVelocityPacketOn = true; // transmit angular velocity packet
135  pComm->DebugPacketOn = true; // transmit debug packet
136  pComm->RPCPacketOn = true; // transmit roll, pitch, compass packet
137  pComm->AltPacketOn = true; // Altitude packet
138  pComm->AccelCalPacketOn = 0;
139  pComm->write = writeControlPort;
140  pComm->stream = CreateAndSendPackets;
141 
142  /* Initialize WIRED UART pins below - currently duplicates code in pin_mux.c */
143  CLOCK_SetLpsci0Clock(0x1U);
144  CLOCK_EnableClock(CONTROL_UART_PORT_CLKEN);
147  LPSCI_GetDefaultConfig(&config);
148 
149  config.baudRate_Bps = CONTROL_BAUDRATE;
150  config.enableTx = true;
151  config.enableRx = true;
152  LPSCI_Init(CONTROL_UART, &config, CLOCK_GetFreq(CONTROL_UART_CLKSRC));
153  //BlueRadios_Init();
154 
155  // Enable RX interrupt.
156  LPSCI_EnableInterrupts(CONTROL_UART, kLPSCI_RxDataRegFullInterruptEnable);
157  EnableIRQ(CONTROL_UART_IRQn);
158 
159  return (0);
160  }
161  else
162  {
163  return (1);
164  }
165 }
volatile quaternion_type QuaternionPacketType
quaternion type transmitted over UART
Definition: control.h:70
#define CONTROL_UART
KSDK instance name for the debug UART.
Definition: frdm_kl25z.h:147
uint8_t data[FXLS8962_DATA_SIZE]
#define CONTROL_UART_RX_PIN
The port number associated with RX.
Definition: frdm_kl25z.h:150
#define CONTROL_UART_PORT_CLKEN
KDSK handle for the pin port clock enable.
Definition: frdm_kl25z.h:148
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
#define CONTROL_UART_TX_PIN
The port number associated with TX.
Definition: frdm_kl25z.h:151
#define CONTROL_UART_CLKSRC
KSDK instance name for the clock feeding this module.
Definition: frdm_kl25z.h:154
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
The top level fusion structure.
quaternion_type DefaultQuaternionPacketType
default quaternion transmitted at power on
Definition: control.h:69
#define CONTROL_BAUDRATE
Baudrate to be used for serial communications.
Definition: control_lpsci.c:58
#define CONTROL_UART_IRQn
KSDK interrupt vector number.
Definition: frdm_kl25z.h:155
setStatus_t * setStatus
change status indicator immediately
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
Receiving commands over wired interface (momentary)
int8_t initializeControlPort(ControlSubsystem *pComm)
Initialize the control subsystem and all related hardware.
void myUART_WriteByte(UART0_Type *base, uint8_t data)
Definition: control_lpsci.c:70
#define CONTROL_UART_PORT
KDSK handle for the pin port associated with this UART.
Definition: frdm_kl25z.h:149
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)
int8_t writeControlPort(ControlSubsystem *pComm, uint8_t buffer[], uint16_t nbytes)
Definition: control_lpsci.c:81
Quaternion derived from 3-axis accel (tilt)
Definition: sensor_fusion.h:75
#define CONTROL_UART_MUX
KDSK pin mux selector.
Definition: frdm_kl25z.h:152
Defines control sub-system.
void CONTROL_UART_IRQHandler(void)
uint8_t sUARTOutputBuffer[256]
main output buffer defined in control.c
Definition: control_lpsci.c:61
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 BlueRadios_Init(void)
Definition: control_lpsci.c:94
volatile uint8_t DebugPacketOn
flag to enable debug packet
Definition: control.h:72