cc3000 hostdriver with the mbed socket interface

Dependents:   cc3000_hello_world_demo cc3000_simple_socket_demo cc3000_ntp_demo cc3000_ping_demo ... more

Committer:
SolderSplashLabs
Date:
Thu Oct 03 20:22:45 2013 +0000
Revision:
21:fb34ac8d9af5
Parent:
0:615c697c33b0
Child:
23:fed7f64dd520
Added HCI Command Logging;

Who changed what in which revision?

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