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