Bluetooth UART support for the Adafruit BluefruitLE SPI, for the University of York Engineering Stage 1 project
Adafruit_BluefruitLE_SPI.h@2:8c341bac60b8, 2021-03-12 (annotated)
- Committer:
- ajp109
- Date:
- Fri Mar 12 14:33:03 2021 +0000
- Revision:
- 2:8c341bac60b8
- Parent:
- 0:a80552d32b80
Last commit before fork - all working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ajp109 | 0:a80552d32b80 | 1 | /**************************************************************************/ |
ajp109 | 0:a80552d32b80 | 2 | /*! |
ajp109 | 0:a80552d32b80 | 3 | @file Adafruit_BluefruiLE_SPI.h |
ajp109 | 0:a80552d32b80 | 4 | @author hathach, ktown (Adafruit Industries), ajp109 (University of York) |
ajp109 | 0:a80552d32b80 | 5 | |
ajp109 | 0:a80552d32b80 | 6 | @section LICENSE |
ajp109 | 0:a80552d32b80 | 7 | |
ajp109 | 0:a80552d32b80 | 8 | Software License Agreement (BSD License) |
ajp109 | 0:a80552d32b80 | 9 | |
ajp109 | 0:a80552d32b80 | 10 | Copyright (c) 2015, Adafruit Industries (adafruit.com) |
ajp109 | 0:a80552d32b80 | 11 | All rights reserved. |
ajp109 | 0:a80552d32b80 | 12 | |
ajp109 | 0:a80552d32b80 | 13 | Redistribution and use in source and binary forms, with or without |
ajp109 | 0:a80552d32b80 | 14 | modification, are permitted provided that the following conditions are met: |
ajp109 | 0:a80552d32b80 | 15 | 1. Redistributions of source code must retain the above copyright |
ajp109 | 0:a80552d32b80 | 16 | notice, this list of conditions and the following disclaimer. |
ajp109 | 0:a80552d32b80 | 17 | 2. Redistributions in binary form must reproduce the above copyright |
ajp109 | 0:a80552d32b80 | 18 | notice, this list of conditions and the following disclaimer in the |
ajp109 | 0:a80552d32b80 | 19 | documentation and/or other materials provided with the distribution. |
ajp109 | 0:a80552d32b80 | 20 | 3. Neither the name of the copyright holders nor the |
ajp109 | 0:a80552d32b80 | 21 | names of its contributors may be used to endorse or promote products |
ajp109 | 0:a80552d32b80 | 22 | derived from this software without specific prior written permission. |
ajp109 | 0:a80552d32b80 | 23 | |
ajp109 | 0:a80552d32b80 | 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
ajp109 | 0:a80552d32b80 | 25 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
ajp109 | 0:a80552d32b80 | 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
ajp109 | 0:a80552d32b80 | 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY |
ajp109 | 0:a80552d32b80 | 28 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
ajp109 | 0:a80552d32b80 | 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
ajp109 | 0:a80552d32b80 | 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
ajp109 | 0:a80552d32b80 | 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
ajp109 | 0:a80552d32b80 | 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
ajp109 | 0:a80552d32b80 | 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
ajp109 | 0:a80552d32b80 | 34 | */ |
ajp109 | 0:a80552d32b80 | 35 | /**************************************************************************/ |
ajp109 | 0:a80552d32b80 | 36 | #ifndef _ADAFRUIT_BLE_SPI_H_ |
ajp109 | 0:a80552d32b80 | 37 | #define _ADAFRUIT_BLE_SPI_H_ |
ajp109 | 0:a80552d32b80 | 38 | |
ajp109 | 0:a80552d32b80 | 39 | #include <Adafruit_BLE.h> |
ajp109 | 0:a80552d32b80 | 40 | #include "utility/Adafruit_FIFO.h" |
ajp109 | 0:a80552d32b80 | 41 | |
ajp109 | 0:a80552d32b80 | 42 | #define SPI_IGNORED_BYTE 0xFEu /**< SPI default character. Character clocked out in case of an ignored transaction. */ |
ajp109 | 0:a80552d32b80 | 43 | #define SPI_OVERREAD_BYTE 0xFFu /**< SPI over-read character. Character clocked out after an over-read of the transmit buffer. */ |
ajp109 | 0:a80552d32b80 | 44 | #define SPI_DEFAULT_DELAY_US 50 |
ajp109 | 0:a80552d32b80 | 45 | |
ajp109 | 0:a80552d32b80 | 46 | #define memclr(buffer, size) memset(buffer, 0, size) |
ajp109 | 0:a80552d32b80 | 47 | |
ajp109 | 0:a80552d32b80 | 48 | |
ajp109 | 0:a80552d32b80 | 49 | class Adafruit_BluefruitLE_SPI : public Adafruit_BLE |
ajp109 | 0:a80552d32b80 | 50 | { |
ajp109 | 0:a80552d32b80 | 51 | private: |
ajp109 | 0:a80552d32b80 | 52 | // Hardware Pin |
ajp109 | 0:a80552d32b80 | 53 | SPI m_spi; |
ajp109 | 2:8c341bac60b8 | 54 | DigitalOut m_cs; |
ajp109 | 0:a80552d32b80 | 55 | InterruptIn m_irq; |
ajp109 | 0:a80552d32b80 | 56 | DigitalOut m_rst; |
ajp109 | 0:a80552d32b80 | 57 | |
ajp109 | 0:a80552d32b80 | 58 | // TX |
ajp109 | 0:a80552d32b80 | 59 | uint8_t m_tx_buffer[SDEP_MAX_PACKETSIZE]; |
ajp109 | 0:a80552d32b80 | 60 | uint8_t m_tx_count; |
ajp109 | 0:a80552d32b80 | 61 | |
ajp109 | 0:a80552d32b80 | 62 | // RX |
ajp109 | 0:a80552d32b80 | 63 | uint8_t m_rx_buffer[BLE_BUFSIZE]; |
ajp109 | 0:a80552d32b80 | 64 | Adafruit_FIFO m_rx_fifo; |
ajp109 | 0:a80552d32b80 | 65 | |
ajp109 | 0:a80552d32b80 | 66 | bool m_mode_switch_command_enabled; |
ajp109 | 0:a80552d32b80 | 67 | |
ajp109 | 0:a80552d32b80 | 68 | // Low level transportation I/O functions |
ajp109 | 0:a80552d32b80 | 69 | bool sendInitializePattern(void); |
ajp109 | 0:a80552d32b80 | 70 | bool sendPacket(uint16_t command, const uint8_t* buffer, uint8_t count, uint8_t more_data); |
ajp109 | 0:a80552d32b80 | 71 | bool getPacket(sdepMsgResponse_t* p_response); |
ajp109 | 0:a80552d32b80 | 72 | |
ajp109 | 0:a80552d32b80 | 73 | bool getResponse(void); |
ajp109 | 0:a80552d32b80 | 74 | void simulateSwitchMode(void); |
ajp109 | 0:a80552d32b80 | 75 | // bool handleSwitchCmdInDataMode(uint8_t ch); |
ajp109 | 0:a80552d32b80 | 76 | |
ajp109 | 0:a80552d32b80 | 77 | uint8_t spixfer(uint8_t x); |
ajp109 | 0:a80552d32b80 | 78 | void spixfer(void *x, size_t len); |
ajp109 | 0:a80552d32b80 | 79 | |
ajp109 | 0:a80552d32b80 | 80 | public: |
ajp109 | 0:a80552d32b80 | 81 | // Constructor |
ajp109 | 0:a80552d32b80 | 82 | Adafruit_BluefruitLE_SPI(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName irq, PinName rst = NC); |
ajp109 | 0:a80552d32b80 | 83 | |
ajp109 | 0:a80552d32b80 | 84 | // HW initialisation |
ajp109 | 0:a80552d32b80 | 85 | bool begin(bool v = false, bool blocking = true); |
ajp109 | 0:a80552d32b80 | 86 | void end(void); |
ajp109 | 0:a80552d32b80 | 87 | |
ajp109 | 0:a80552d32b80 | 88 | bool setMode(uint8_t new_mode); |
ajp109 | 0:a80552d32b80 | 89 | void enableModeSwitchCommand(bool enabled); |
ajp109 | 0:a80552d32b80 | 90 | |
ajp109 | 0:a80552d32b80 | 91 | // Override Stream |
ajp109 | 0:a80552d32b80 | 92 | virtual ssize_t write(const void *buf, size_t size); |
ajp109 | 0:a80552d32b80 | 93 | virtual int sync(); |
ajp109 | 0:a80552d32b80 | 94 | |
ajp109 | 0:a80552d32b80 | 95 | protected: |
ajp109 | 0:a80552d32b80 | 96 | // Class Stream interface (Arduino) |
ajp109 | 0:a80552d32b80 | 97 | virtual int available(void); |
ajp109 | 0:a80552d32b80 | 98 | |
ajp109 | 0:a80552d32b80 | 99 | virtual int _putc(int c); |
ajp109 | 0:a80552d32b80 | 100 | virtual int _getc(); |
ajp109 | 0:a80552d32b80 | 101 | |
ajp109 | 0:a80552d32b80 | 102 | }; |
ajp109 | 0:a80552d32b80 | 103 | |
ajp109 | 0:a80552d32b80 | 104 | #endif |