• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *    Copyright (c) 2016, The OpenThread Authors.
3  *    All rights reserved.
4  *
5  *    Redistribution and use in source and binary forms, with or without
6  *    modification, are permitted provided that the following conditions are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *    3. Neither the name of the copyright holder nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  *    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  *    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  *    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20  *    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  *    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  *    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  *    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  *    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  *    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /**
29  * @file
30  *   This file contains definitions for a SPI interface to the OpenThread stack.
31  */
32 
33 #ifndef NCP_SPI_HPP_
34 #define NCP_SPI_HPP_
35 
36 #include "openthread-core-config.h"
37 
38 #include "lib/spinel/spi_frame.hpp"
39 #include "ncp/ncp_base.hpp"
40 
41 namespace ot {
42 namespace Ncp {
43 
44 class NcpSpi : public NcpBase
45 {
46 public:
47     /**
48      * Initializes the object.
49      *
50      * @param[in]  aInstance  A pointer to the OpenThread instance structure.
51      */
52     explicit NcpSpi(Instance *aInstance);
53 
54 private:
55     enum
56     {
57         /**
58          * SPI tx and rx buffer size (should fit a max length frame + SPI header).
59          */
60         kSpiBufferSize = OPENTHREAD_CONFIG_NCP_SPI_BUFFER_SIZE,
61 
62         /**
63          * Size of the SPI header (in bytes).
64          */
65         kSpiHeaderSize = Spinel::SpiFrame::kHeaderSize,
66     };
67 
68     enum TxState
69     {
70         kTxStateIdle,            // No frame to send.
71         kTxStateSending,         // A frame is ready to be sent.
72         kTxStateHandlingSendDone // The frame was sent successfully, waiting to prepare the next one (if any).
73     };
74 
75     typedef uint8_t LargeFrameBuffer[kSpiBufferSize];
76     typedef uint8_t EmptyFrameBuffer[kSpiHeaderSize];
77 
78     static bool SpiTransactionComplete(void    *aContext,
79                                        uint8_t *aOutputBuf,
80                                        uint16_t aOutputLen,
81                                        uint8_t *aInputBuf,
82                                        uint16_t aInputLen,
83                                        uint16_t aTransLen);
84     bool        SpiTransactionComplete(uint8_t *aOutputBuf,
85                                        uint16_t aOutputLen,
86                                        uint8_t *aInputBuf,
87                                        uint16_t aInputLen,
88                                        uint16_t aTransLen);
89 
90     static void SpiTransactionProcess(void *aContext);
91     void        SpiTransactionProcess(void);
92 
93     static void HandleFrameAddedToTxBuffer(void                    *aContext,
94                                            Spinel::Buffer::FrameTag aFrameTag,
95                                            Spinel::Buffer::Priority aPriority,
96                                            Spinel::Buffer          *aBuffer);
97 
98     static void PrepareTxFrame(Tasklet &aTasklet);
99     void        PrepareTxFrame(void);
100     void        HandleRxFrame(void);
101     void        PrepareNextSpiSendFrame(void);
102 
103     volatile TxState mTxState;
104     volatile bool    mHandlingRxFrame;
105     volatile bool    mResetFlag;
106 
107     Tasklet mPrepareTxFrameTask;
108 
109     uint16_t         mSendFrameLength;
110     LargeFrameBuffer mSendFrame;
111     EmptyFrameBuffer mEmptySendFrameFullAccept;
112     EmptyFrameBuffer mEmptySendFrameZeroAccept;
113 
114     LargeFrameBuffer mReceiveFrame;
115     EmptyFrameBuffer mEmptyReceiveFrame;
116 };
117 
118 } // namespace Ncp
119 } // namespace ot
120 
121 #endif // NCP_SPI_HPP_
122