Added mutex for multiple SPI devices on the same SPI bus
Fork of cc3000_hostdriver_mbedsocket by
cc3000_hci.cpp@20:30b6ed7bf8fd, 2013-10-03 (annotated)
- Committer:
- Kojto
- Date:
- Thu Oct 03 17:15:45 2013 +0200
- Revision:
- 20:30b6ed7bf8fd
- Parent:
- 0:615c697c33b0
- Child:
- 22:d23c59fec0dc
UNIX line endings
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 20:30b6ed7bf8fd | 1 | /***************************************************************************** |
Kojto | 20:30b6ed7bf8fd | 2 | * |
Kojto | 20:30b6ed7bf8fd | 3 | * C++ interface/implementation created by Martin Kojtal (0xc0170). Thanks to |
Kojto | 20:30b6ed7bf8fd | 4 | * Jim Carver and Frank Vannieuwkerke for their inital cc3000 mbed port and |
Kojto | 20:30b6ed7bf8fd | 5 | * provided help. |
Kojto | 20:30b6ed7bf8fd | 6 | * |
Kojto | 20:30b6ed7bf8fd | 7 | * This version of "host driver" uses CC3000 Host Driver Implementation. Thus |
Kojto | 20:30b6ed7bf8fd | 8 | * read the following copyright: |
Kojto | 20:30b6ed7bf8fd | 9 | * |
Kojto | 20:30b6ed7bf8fd | 10 | * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ |
Kojto | 20:30b6ed7bf8fd | 11 | * |
Kojto | 20:30b6ed7bf8fd | 12 | * Redistribution and use in source and binary forms, with or without |
Kojto | 20:30b6ed7bf8fd | 13 | * modification, are permitted provided that the following conditions |
Kojto | 20:30b6ed7bf8fd | 14 | * are met: |
Kojto | 20:30b6ed7bf8fd | 15 | * |
Kojto | 20:30b6ed7bf8fd | 16 | * Redistributions of source code must retain the above copyright |
Kojto | 20:30b6ed7bf8fd | 17 | * notice, this list of conditions and the following disclaimer. |
Kojto | 20:30b6ed7bf8fd | 18 | * |
Kojto | 20:30b6ed7bf8fd | 19 | * Redistributions in binary form must reproduce the above copyright |
Kojto | 20:30b6ed7bf8fd | 20 | * notice, this list of conditions and the following disclaimer in the |
Kojto | 20:30b6ed7bf8fd | 21 | * documentation and/or other materials provided with the |
Kojto | 20:30b6ed7bf8fd | 22 | * distribution. |
Kojto | 20:30b6ed7bf8fd | 23 | * |
Kojto | 20:30b6ed7bf8fd | 24 | * Neither the name of Texas Instruments Incorporated nor the names of |
Kojto | 20:30b6ed7bf8fd | 25 | * its contributors may be used to endorse or promote products derived |
Kojto | 20:30b6ed7bf8fd | 26 | * from this software without specific prior written permission. |
Kojto | 20:30b6ed7bf8fd | 27 | * |
Kojto | 20:30b6ed7bf8fd | 28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
Kojto | 20:30b6ed7bf8fd | 29 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
Kojto | 20:30b6ed7bf8fd | 30 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
Kojto | 20:30b6ed7bf8fd | 31 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
Kojto | 20:30b6ed7bf8fd | 32 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
Kojto | 20:30b6ed7bf8fd | 33 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
Kojto | 20:30b6ed7bf8fd | 34 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
Kojto | 20:30b6ed7bf8fd | 35 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
Kojto | 20:30b6ed7bf8fd | 36 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
Kojto | 20:30b6ed7bf8fd | 37 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
Kojto | 20:30b6ed7bf8fd | 38 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Kojto | 20:30b6ed7bf8fd | 39 | * |
Kojto | 20:30b6ed7bf8fd | 40 | *****************************************************************************/ |
Kojto | 20:30b6ed7bf8fd | 41 | #include "cc3000.h" |
Kojto | 20:30b6ed7bf8fd | 42 | |
Kojto | 20:30b6ed7bf8fd | 43 | namespace mbed_cc3000 { |
Kojto | 20:30b6ed7bf8fd | 44 | |
Kojto | 20:30b6ed7bf8fd | 45 | cc3000_hci::cc3000_hci(cc3000_spi &spi) : _spi(spi) { |
Kojto | 20:30b6ed7bf8fd | 46 | |
Kojto | 20:30b6ed7bf8fd | 47 | } |
Kojto | 20:30b6ed7bf8fd | 48 | |
Kojto | 20:30b6ed7bf8fd | 49 | cc3000_hci::~cc3000_hci() { |
Kojto | 20:30b6ed7bf8fd | 50 | |
Kojto | 20:30b6ed7bf8fd | 51 | } |
Kojto | 20:30b6ed7bf8fd | 52 | |
Kojto | 20:30b6ed7bf8fd | 53 | uint16_t cc3000_hci::command_send(uint16_t op_code, uint8_t *buffer, uint8_t length) { |
Kojto | 20:30b6ed7bf8fd | 54 | unsigned char *stream; |
Kojto | 20:30b6ed7bf8fd | 55 | |
Kojto | 20:30b6ed7bf8fd | 56 | stream = (buffer + SPI_HEADER_SIZE); |
Kojto | 20:30b6ed7bf8fd | 57 | |
Kojto | 20:30b6ed7bf8fd | 58 | UINT8_TO_STREAM(stream, HCI_TYPE_CMND); |
Kojto | 20:30b6ed7bf8fd | 59 | stream = UINT16_TO_STREAM(stream, op_code); |
Kojto | 20:30b6ed7bf8fd | 60 | UINT8_TO_STREAM(stream, length); |
Kojto | 20:30b6ed7bf8fd | 61 | //Update the opcode of the event we will be waiting for |
Kojto | 20:30b6ed7bf8fd | 62 | _spi.write(buffer, length + SIMPLE_LINK_HCI_CMND_HEADER_SIZE); |
Kojto | 20:30b6ed7bf8fd | 63 | return(0); |
Kojto | 20:30b6ed7bf8fd | 64 | } |
Kojto | 20:30b6ed7bf8fd | 65 | |
Kojto | 20:30b6ed7bf8fd | 66 | uint32_t cc3000_hci::data_send(uint8_t op_code, uint8_t *args, uint16_t arg_length, |
Kojto | 20:30b6ed7bf8fd | 67 | uint16_t data_length, const uint8_t *tail, uint16_t tail_length) { |
Kojto | 20:30b6ed7bf8fd | 68 | unsigned char *stream; |
Kojto | 20:30b6ed7bf8fd | 69 | |
Kojto | 20:30b6ed7bf8fd | 70 | stream = ((args) + SPI_HEADER_SIZE); |
Kojto | 20:30b6ed7bf8fd | 71 | |
Kojto | 20:30b6ed7bf8fd | 72 | UINT8_TO_STREAM(stream, HCI_TYPE_DATA); |
Kojto | 20:30b6ed7bf8fd | 73 | UINT8_TO_STREAM(stream, op_code); |
Kojto | 20:30b6ed7bf8fd | 74 | UINT8_TO_STREAM(stream, arg_length); |
Kojto | 20:30b6ed7bf8fd | 75 | stream = UINT16_TO_STREAM(stream, arg_length + data_length + tail_length); |
Kojto | 20:30b6ed7bf8fd | 76 | |
Kojto | 20:30b6ed7bf8fd | 77 | // Send the packet |
Kojto | 20:30b6ed7bf8fd | 78 | _spi.write(args, SIMPLE_LINK_HCI_DATA_HEADER_SIZE + arg_length + data_length + tail_length); |
Kojto | 20:30b6ed7bf8fd | 79 | |
Kojto | 20:30b6ed7bf8fd | 80 | return 0; |
Kojto | 20:30b6ed7bf8fd | 81 | } |
Kojto | 20:30b6ed7bf8fd | 82 | |
Kojto | 20:30b6ed7bf8fd | 83 | void cc3000_hci::data_command_send(uint16_t op_code, uint8_t *buffer, uint8_t arg_length, uint16_t data_length) { |
Kojto | 20:30b6ed7bf8fd | 84 | unsigned char *stream = (buffer + SPI_HEADER_SIZE); |
Kojto | 20:30b6ed7bf8fd | 85 | |
Kojto | 20:30b6ed7bf8fd | 86 | UINT8_TO_STREAM(stream, HCI_TYPE_DATA); |
Kojto | 20:30b6ed7bf8fd | 87 | UINT8_TO_STREAM(stream, op_code); |
Kojto | 20:30b6ed7bf8fd | 88 | UINT8_TO_STREAM(stream, arg_length); |
Kojto | 20:30b6ed7bf8fd | 89 | stream = UINT16_TO_STREAM(stream, arg_length + data_length); |
Kojto | 20:30b6ed7bf8fd | 90 | |
Kojto | 20:30b6ed7bf8fd | 91 | // Send the command |
Kojto | 20:30b6ed7bf8fd | 92 | _spi.write(buffer, arg_length + data_length + SIMPLE_LINK_HCI_DATA_CMND_HEADER_SIZE); |
Kojto | 20:30b6ed7bf8fd | 93 | |
Kojto | 20:30b6ed7bf8fd | 94 | return; |
Kojto | 20:30b6ed7bf8fd | 95 | } |
Kojto | 20:30b6ed7bf8fd | 96 | |
Kojto | 20:30b6ed7bf8fd | 97 | void cc3000_hci::patch_send(uint8_t op_code, uint8_t *buffer, uint8_t *patch, uint16_t data_length) { |
Kojto | 20:30b6ed7bf8fd | 98 | unsigned short usTransLength; |
Kojto | 20:30b6ed7bf8fd | 99 | unsigned char *stream = (buffer + SPI_HEADER_SIZE); |
Kojto | 20:30b6ed7bf8fd | 100 | UINT8_TO_STREAM(stream, HCI_TYPE_PATCH); |
Kojto | 20:30b6ed7bf8fd | 101 | UINT8_TO_STREAM(stream, op_code); |
Kojto | 20:30b6ed7bf8fd | 102 | stream = UINT16_TO_STREAM(stream, data_length + SIMPLE_LINK_HCI_PATCH_HEADER_SIZE); |
Kojto | 20:30b6ed7bf8fd | 103 | if (data_length <= SL_PATCH_PORTION_SIZE) |
Kojto | 20:30b6ed7bf8fd | 104 | { |
Kojto | 20:30b6ed7bf8fd | 105 | UINT16_TO_STREAM(stream, data_length); |
Kojto | 20:30b6ed7bf8fd | 106 | stream = UINT16_TO_STREAM(stream, data_length); |
Kojto | 20:30b6ed7bf8fd | 107 | memcpy((buffer + SPI_HEADER_SIZE) + HCI_PATCH_HEADER_SIZE, patch, data_length); |
Kojto | 20:30b6ed7bf8fd | 108 | // Update the opcode of the event we will be waiting for |
Kojto | 20:30b6ed7bf8fd | 109 | _spi.write(buffer, data_length + HCI_PATCH_HEADER_SIZE); |
Kojto | 20:30b6ed7bf8fd | 110 | } |
Kojto | 20:30b6ed7bf8fd | 111 | else |
Kojto | 20:30b6ed7bf8fd | 112 | { |
Kojto | 20:30b6ed7bf8fd | 113 | |
Kojto | 20:30b6ed7bf8fd | 114 | usTransLength = (data_length/SL_PATCH_PORTION_SIZE); |
Kojto | 20:30b6ed7bf8fd | 115 | UINT16_TO_STREAM(stream, data_length + SIMPLE_LINK_HCI_PATCH_HEADER_SIZE + usTransLength*SIMPLE_LINK_HCI_PATCH_HEADER_SIZE); |
Kojto | 20:30b6ed7bf8fd | 116 | stream = UINT16_TO_STREAM(stream, SL_PATCH_PORTION_SIZE); |
Kojto | 20:30b6ed7bf8fd | 117 | memcpy(buffer + SPI_HEADER_SIZE + HCI_PATCH_HEADER_SIZE, patch, SL_PATCH_PORTION_SIZE); |
Kojto | 20:30b6ed7bf8fd | 118 | data_length -= SL_PATCH_PORTION_SIZE; |
Kojto | 20:30b6ed7bf8fd | 119 | patch += SL_PATCH_PORTION_SIZE; |
Kojto | 20:30b6ed7bf8fd | 120 | |
Kojto | 20:30b6ed7bf8fd | 121 | // Update the opcode of the event we will be waiting for |
Kojto | 20:30b6ed7bf8fd | 122 | _spi.write(buffer, SL_PATCH_PORTION_SIZE + HCI_PATCH_HEADER_SIZE); |
Kojto | 20:30b6ed7bf8fd | 123 | |
Kojto | 20:30b6ed7bf8fd | 124 | stream = (buffer + SPI_HEADER_SIZE); |
Kojto | 20:30b6ed7bf8fd | 125 | while (data_length) |
Kojto | 20:30b6ed7bf8fd | 126 | { |
Kojto | 20:30b6ed7bf8fd | 127 | if (data_length <= SL_PATCH_PORTION_SIZE) |
Kojto | 20:30b6ed7bf8fd | 128 | { |
Kojto | 20:30b6ed7bf8fd | 129 | usTransLength = data_length; |
Kojto | 20:30b6ed7bf8fd | 130 | data_length = 0; |
Kojto | 20:30b6ed7bf8fd | 131 | |
Kojto | 20:30b6ed7bf8fd | 132 | } |
Kojto | 20:30b6ed7bf8fd | 133 | else |
Kojto | 20:30b6ed7bf8fd | 134 | { |
Kojto | 20:30b6ed7bf8fd | 135 | usTransLength = SL_PATCH_PORTION_SIZE; |
Kojto | 20:30b6ed7bf8fd | 136 | data_length -= usTransLength; |
Kojto | 20:30b6ed7bf8fd | 137 | } |
Kojto | 20:30b6ed7bf8fd | 138 | |
Kojto | 20:30b6ed7bf8fd | 139 | *(unsigned short *)stream = usTransLength; |
Kojto | 20:30b6ed7bf8fd | 140 | memcpy(stream + SIMPLE_LINK_HCI_PATCH_HEADER_SIZE, patch, usTransLength); |
Kojto | 20:30b6ed7bf8fd | 141 | patch += usTransLength; |
Kojto | 20:30b6ed7bf8fd | 142 | |
Kojto | 20:30b6ed7bf8fd | 143 | // Update the opcode of the event we will be waiting for |
Kojto | 20:30b6ed7bf8fd | 144 | _spi.write((unsigned char *)stream, usTransLength + sizeof(usTransLength)); |
Kojto | 20:30b6ed7bf8fd | 145 | } |
Kojto | 20:30b6ed7bf8fd | 146 | } |
Kojto | 20:30b6ed7bf8fd | 147 | } |
Kojto | 20:30b6ed7bf8fd | 148 | |
Kojto | 20:30b6ed7bf8fd | 149 | } |