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 20:56:45 2013 +0000
Revision:
22:d23c59fec0dc
Parent:
20:30b6ed7bf8fd
Child:
26:456f73ed2a75
Separate Debug defines and VT100 colour

Who changed what in which revision?

UserRevisionLine numberNew 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;
SolderSplashLabs 22:d23c59fec0dc 55
SolderSplashLabs 22:d23c59fec0dc 56 DBG_HCI_CMD("Command Sent : 0x%04X", op_code);
Kojto 20:30b6ed7bf8fd 57
Kojto 20:30b6ed7bf8fd 58 stream = (buffer + SPI_HEADER_SIZE);
Kojto 20:30b6ed7bf8fd 59
Kojto 20:30b6ed7bf8fd 60 UINT8_TO_STREAM(stream, HCI_TYPE_CMND);
Kojto 20:30b6ed7bf8fd 61 stream = UINT16_TO_STREAM(stream, op_code);
Kojto 20:30b6ed7bf8fd 62 UINT8_TO_STREAM(stream, length);
Kojto 20:30b6ed7bf8fd 63 //Update the opcode of the event we will be waiting for
Kojto 20:30b6ed7bf8fd 64 _spi.write(buffer, length + SIMPLE_LINK_HCI_CMND_HEADER_SIZE);
Kojto 20:30b6ed7bf8fd 65 return(0);
Kojto 20:30b6ed7bf8fd 66 }
Kojto 20:30b6ed7bf8fd 67
Kojto 20:30b6ed7bf8fd 68 uint32_t cc3000_hci::data_send(uint8_t op_code, uint8_t *args, uint16_t arg_length,
Kojto 20:30b6ed7bf8fd 69 uint16_t data_length, const uint8_t *tail, uint16_t tail_length) {
Kojto 20:30b6ed7bf8fd 70 unsigned char *stream;
Kojto 20:30b6ed7bf8fd 71
Kojto 20:30b6ed7bf8fd 72 stream = ((args) + SPI_HEADER_SIZE);
Kojto 20:30b6ed7bf8fd 73
Kojto 20:30b6ed7bf8fd 74 UINT8_TO_STREAM(stream, HCI_TYPE_DATA);
Kojto 20:30b6ed7bf8fd 75 UINT8_TO_STREAM(stream, op_code);
Kojto 20:30b6ed7bf8fd 76 UINT8_TO_STREAM(stream, arg_length);
Kojto 20:30b6ed7bf8fd 77 stream = UINT16_TO_STREAM(stream, arg_length + data_length + tail_length);
Kojto 20:30b6ed7bf8fd 78
Kojto 20:30b6ed7bf8fd 79 // Send the packet
Kojto 20:30b6ed7bf8fd 80 _spi.write(args, SIMPLE_LINK_HCI_DATA_HEADER_SIZE + arg_length + data_length + tail_length);
Kojto 20:30b6ed7bf8fd 81
Kojto 20:30b6ed7bf8fd 82 return 0;
Kojto 20:30b6ed7bf8fd 83 }
Kojto 20:30b6ed7bf8fd 84
Kojto 20:30b6ed7bf8fd 85 void cc3000_hci::data_command_send(uint16_t op_code, uint8_t *buffer, uint8_t arg_length, uint16_t data_length) {
Kojto 20:30b6ed7bf8fd 86 unsigned char *stream = (buffer + SPI_HEADER_SIZE);
Kojto 20:30b6ed7bf8fd 87
Kojto 20:30b6ed7bf8fd 88 UINT8_TO_STREAM(stream, HCI_TYPE_DATA);
Kojto 20:30b6ed7bf8fd 89 UINT8_TO_STREAM(stream, op_code);
Kojto 20:30b6ed7bf8fd 90 UINT8_TO_STREAM(stream, arg_length);
Kojto 20:30b6ed7bf8fd 91 stream = UINT16_TO_STREAM(stream, arg_length + data_length);
Kojto 20:30b6ed7bf8fd 92
Kojto 20:30b6ed7bf8fd 93 // Send the command
Kojto 20:30b6ed7bf8fd 94 _spi.write(buffer, arg_length + data_length + SIMPLE_LINK_HCI_DATA_CMND_HEADER_SIZE);
Kojto 20:30b6ed7bf8fd 95
Kojto 20:30b6ed7bf8fd 96 return;
Kojto 20:30b6ed7bf8fd 97 }
Kojto 20:30b6ed7bf8fd 98
Kojto 20:30b6ed7bf8fd 99 void cc3000_hci::patch_send(uint8_t op_code, uint8_t *buffer, uint8_t *patch, uint16_t data_length) {
Kojto 20:30b6ed7bf8fd 100 unsigned short usTransLength;
Kojto 20:30b6ed7bf8fd 101 unsigned char *stream = (buffer + SPI_HEADER_SIZE);
Kojto 20:30b6ed7bf8fd 102 UINT8_TO_STREAM(stream, HCI_TYPE_PATCH);
Kojto 20:30b6ed7bf8fd 103 UINT8_TO_STREAM(stream, op_code);
Kojto 20:30b6ed7bf8fd 104 stream = UINT16_TO_STREAM(stream, data_length + SIMPLE_LINK_HCI_PATCH_HEADER_SIZE);
Kojto 20:30b6ed7bf8fd 105 if (data_length <= SL_PATCH_PORTION_SIZE)
Kojto 20:30b6ed7bf8fd 106 {
Kojto 20:30b6ed7bf8fd 107 UINT16_TO_STREAM(stream, data_length);
Kojto 20:30b6ed7bf8fd 108 stream = UINT16_TO_STREAM(stream, data_length);
Kojto 20:30b6ed7bf8fd 109 memcpy((buffer + SPI_HEADER_SIZE) + HCI_PATCH_HEADER_SIZE, patch, data_length);
Kojto 20:30b6ed7bf8fd 110 // Update the opcode of the event we will be waiting for
Kojto 20:30b6ed7bf8fd 111 _spi.write(buffer, data_length + HCI_PATCH_HEADER_SIZE);
Kojto 20:30b6ed7bf8fd 112 }
Kojto 20:30b6ed7bf8fd 113 else
Kojto 20:30b6ed7bf8fd 114 {
Kojto 20:30b6ed7bf8fd 115
Kojto 20:30b6ed7bf8fd 116 usTransLength = (data_length/SL_PATCH_PORTION_SIZE);
Kojto 20:30b6ed7bf8fd 117 UINT16_TO_STREAM(stream, data_length + SIMPLE_LINK_HCI_PATCH_HEADER_SIZE + usTransLength*SIMPLE_LINK_HCI_PATCH_HEADER_SIZE);
Kojto 20:30b6ed7bf8fd 118 stream = UINT16_TO_STREAM(stream, SL_PATCH_PORTION_SIZE);
Kojto 20:30b6ed7bf8fd 119 memcpy(buffer + SPI_HEADER_SIZE + HCI_PATCH_HEADER_SIZE, patch, SL_PATCH_PORTION_SIZE);
Kojto 20:30b6ed7bf8fd 120 data_length -= SL_PATCH_PORTION_SIZE;
Kojto 20:30b6ed7bf8fd 121 patch += SL_PATCH_PORTION_SIZE;
Kojto 20:30b6ed7bf8fd 122
Kojto 20:30b6ed7bf8fd 123 // Update the opcode of the event we will be waiting for
Kojto 20:30b6ed7bf8fd 124 _spi.write(buffer, SL_PATCH_PORTION_SIZE + HCI_PATCH_HEADER_SIZE);
Kojto 20:30b6ed7bf8fd 125
Kojto 20:30b6ed7bf8fd 126 stream = (buffer + SPI_HEADER_SIZE);
Kojto 20:30b6ed7bf8fd 127 while (data_length)
Kojto 20:30b6ed7bf8fd 128 {
Kojto 20:30b6ed7bf8fd 129 if (data_length <= SL_PATCH_PORTION_SIZE)
Kojto 20:30b6ed7bf8fd 130 {
Kojto 20:30b6ed7bf8fd 131 usTransLength = data_length;
Kojto 20:30b6ed7bf8fd 132 data_length = 0;
Kojto 20:30b6ed7bf8fd 133
Kojto 20:30b6ed7bf8fd 134 }
Kojto 20:30b6ed7bf8fd 135 else
Kojto 20:30b6ed7bf8fd 136 {
Kojto 20:30b6ed7bf8fd 137 usTransLength = SL_PATCH_PORTION_SIZE;
Kojto 20:30b6ed7bf8fd 138 data_length -= usTransLength;
Kojto 20:30b6ed7bf8fd 139 }
Kojto 20:30b6ed7bf8fd 140
Kojto 20:30b6ed7bf8fd 141 *(unsigned short *)stream = usTransLength;
Kojto 20:30b6ed7bf8fd 142 memcpy(stream + SIMPLE_LINK_HCI_PATCH_HEADER_SIZE, patch, usTransLength);
Kojto 20:30b6ed7bf8fd 143 patch += usTransLength;
Kojto 20:30b6ed7bf8fd 144
Kojto 20:30b6ed7bf8fd 145 // Update the opcode of the event we will be waiting for
Kojto 20:30b6ed7bf8fd 146 _spi.write((unsigned char *)stream, usTransLength + sizeof(usTransLength));
Kojto 20:30b6ed7bf8fd 147 }
Kojto 20:30b6ed7bf8fd 148 }
Kojto 20:30b6ed7bf8fd 149 }
Kojto 20:30b6ed7bf8fd 150
Kojto 20:30b6ed7bf8fd 151 }