Added mutex for multiple SPI devices on the same SPI bus

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Committer:
SolderSplashLabs
Date:
Thu Oct 03 21:00:00 2013 +0000
Revision:
23:fed7f64dd520
Parent:
21:fb34ac8d9af5
Child:
26:456f73ed2a75
Added separate debug messages and VT100 formatting

Who changed what in which revision?

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