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