cc3000 hostdriver with the mbed socket interface

Dependents:   cc3000_hello_world_demo cc3000_simple_socket_demo cc3000_ntp_demo cc3000_ping_demo ... more

Committer:
Kojto
Date:
Thu Sep 19 07:55:14 2013 +0000
Revision:
0:615c697c33b0
Child:
20:30b6ed7bf8fd
initial commit

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 #include "cc3000_nvmem.h"
Kojto 0:615c697c33b0 43 #include "cc3000_common.h"
Kojto 0:615c697c33b0 44
Kojto 0:615c697c33b0 45 namespace mbed_cc3000 {
Kojto 0:615c697c33b0 46
Kojto 0:615c697c33b0 47 cc3000_nvmem::cc3000_nvmem(cc3000_hci &hci, cc3000_event &event, cc3000_simple_link &simple_link)
Kojto 0:615c697c33b0 48 : _hci(hci), _event(event), _simple_link(simple_link) {
Kojto 0:615c697c33b0 49
Kojto 0:615c697c33b0 50 }
Kojto 0:615c697c33b0 51
Kojto 0:615c697c33b0 52 cc3000_nvmem::~cc3000_nvmem() {
Kojto 0:615c697c33b0 53
Kojto 0:615c697c33b0 54 }
Kojto 0:615c697c33b0 55
Kojto 0:615c697c33b0 56 int32_t cc3000_nvmem::read(uint32_t file_id, uint32_t length, uint32_t offset, uint8_t *buff) {
Kojto 0:615c697c33b0 57 uint8_t ucStatus = 0xFF;
Kojto 0:615c697c33b0 58 uint8_t *ptr;
Kojto 0:615c697c33b0 59 uint8_t *args;
Kojto 0:615c697c33b0 60
Kojto 0:615c697c33b0 61 ptr = _simple_link.get_transmit_buffer();
Kojto 0:615c697c33b0 62 args = (ptr + HEADERS_SIZE_CMD);
Kojto 0:615c697c33b0 63 // Fill in HCI packet structure
Kojto 0:615c697c33b0 64 args = UINT32_TO_STREAM(args, file_id);
Kojto 0:615c697c33b0 65 args = UINT32_TO_STREAM(args, length);
Kojto 0:615c697c33b0 66 args = UINT32_TO_STREAM(args, offset);
Kojto 0:615c697c33b0 67
Kojto 0:615c697c33b0 68 // Initiate HCI command
Kojto 0:615c697c33b0 69 _hci.command_send(HCI_CMND_NVMEM_READ, ptr, NVMEM_READ_PARAMS_LEN);
Kojto 0:615c697c33b0 70 _event.simplelink_wait_event(HCI_CMND_NVMEM_READ, &ucStatus);
Kojto 0:615c697c33b0 71
Kojto 0:615c697c33b0 72 // If data is present, read it even when an error is returned.
Kojto 0:615c697c33b0 73 // Note: It is the users responsibility to ignore the data when an error is returned.
Kojto 0:615c697c33b0 74 // Wait for the data in a synchronous way.
Kojto 0:615c697c33b0 75 // We assume the buffer is large enough to also store nvmem parameters.
Kojto 0:615c697c33b0 76 _event.simplelink_wait_data(buff, 0, 0);
Kojto 0:615c697c33b0 77
Kojto 0:615c697c33b0 78 return(ucStatus);
Kojto 0:615c697c33b0 79 }
Kojto 0:615c697c33b0 80
Kojto 0:615c697c33b0 81 int32_t cc3000_nvmem::write(uint32_t file_id, uint32_t length, uint32_t entry_offset, uint8_t *buff) {
Kojto 0:615c697c33b0 82 int32_t iRes;
Kojto 0:615c697c33b0 83 uint8_t *ptr;
Kojto 0:615c697c33b0 84 uint8_t *args;
Kojto 0:615c697c33b0 85
Kojto 0:615c697c33b0 86 iRes = EFAIL;
Kojto 0:615c697c33b0 87
Kojto 0:615c697c33b0 88 ptr = _simple_link.get_transmit_buffer();
Kojto 0:615c697c33b0 89 args = (ptr + SPI_HEADER_SIZE + HCI_DATA_CMD_HEADER_SIZE);
Kojto 0:615c697c33b0 90
Kojto 0:615c697c33b0 91 // Fill in HCI packet structure
Kojto 0:615c697c33b0 92 args = UINT32_TO_STREAM(args, file_id);
Kojto 0:615c697c33b0 93 args = UINT32_TO_STREAM(args, 12);
Kojto 0:615c697c33b0 94 args = UINT32_TO_STREAM(args, length);
Kojto 0:615c697c33b0 95 args = UINT32_TO_STREAM(args, entry_offset);
Kojto 0:615c697c33b0 96
Kojto 0:615c697c33b0 97 memcpy((ptr + SPI_HEADER_SIZE + HCI_DATA_CMD_HEADER_SIZE +
Kojto 0:615c697c33b0 98 NVMEM_WRITE_PARAMS_LEN),buff,length);
Kojto 0:615c697c33b0 99
Kojto 0:615c697c33b0 100 // Initiate a HCI command on the data channel
Kojto 0:615c697c33b0 101 _hci.data_command_send(HCI_CMND_NVMEM_WRITE, ptr, NVMEM_WRITE_PARAMS_LEN, length);
Kojto 0:615c697c33b0 102
Kojto 0:615c697c33b0 103 _event.simplelink_wait_event(HCI_EVNT_NVMEM_WRITE, &iRes);
Kojto 0:615c697c33b0 104
Kojto 0:615c697c33b0 105 return(iRes);
Kojto 0:615c697c33b0 106 }
Kojto 0:615c697c33b0 107
Kojto 0:615c697c33b0 108 uint8_t cc3000_nvmem::set_mac_address(uint8_t *mac) {
Kojto 0:615c697c33b0 109 return write(NVMEM_MAC_FILEID, MAC_ADDR_LEN, 0, mac);
Kojto 0:615c697c33b0 110 }
Kojto 0:615c697c33b0 111
Kojto 0:615c697c33b0 112 uint8_t cc3000_nvmem::get_mac_address(uint8_t *mac) {
Kojto 0:615c697c33b0 113 return read(NVMEM_MAC_FILEID, MAC_ADDR_LEN, 0, mac);
Kojto 0:615c697c33b0 114 }
Kojto 0:615c697c33b0 115
Kojto 0:615c697c33b0 116 uint8_t cc3000_nvmem::write_patch(uint32_t file_id, uint32_t length, const uint8_t *data) {
Kojto 0:615c697c33b0 117 uint8_t status = 0;
Kojto 0:615c697c33b0 118 uint16_t offset = 0;
Kojto 0:615c697c33b0 119 uint8_t* spDataPtr = (uint8_t*)data;
Kojto 0:615c697c33b0 120
Kojto 0:615c697c33b0 121 while ((status == 0) && (length >= SP_PORTION_SIZE))
Kojto 0:615c697c33b0 122 {
Kojto 0:615c697c33b0 123 status = write(file_id, SP_PORTION_SIZE, offset, spDataPtr);
Kojto 0:615c697c33b0 124 offset += SP_PORTION_SIZE;
Kojto 0:615c697c33b0 125 length -= SP_PORTION_SIZE;
Kojto 0:615c697c33b0 126 spDataPtr += SP_PORTION_SIZE;
Kojto 0:615c697c33b0 127 }
Kojto 0:615c697c33b0 128
Kojto 0:615c697c33b0 129 if (status !=0)
Kojto 0:615c697c33b0 130 {
Kojto 0:615c697c33b0 131 // NVMEM error occurred
Kojto 0:615c697c33b0 132 return status;
Kojto 0:615c697c33b0 133 }
Kojto 0:615c697c33b0 134
Kojto 0:615c697c33b0 135 if (length != 0)
Kojto 0:615c697c33b0 136 {
Kojto 0:615c697c33b0 137 // If length MOD 512 is nonzero, write the remaining bytes.
Kojto 0:615c697c33b0 138 status = write(file_id, length, offset, spDataPtr);
Kojto 0:615c697c33b0 139 }
Kojto 0:615c697c33b0 140
Kojto 0:615c697c33b0 141 return status;
Kojto 0:615c697c33b0 142 }
Kojto 0:615c697c33b0 143
Kojto 0:615c697c33b0 144 int32_t cc3000_nvmem::create_entry(uint32_t file_id, uint32_t new_len) {
Kojto 0:615c697c33b0 145 uint8_t *ptr;
Kojto 0:615c697c33b0 146 uint8_t *args;
Kojto 0:615c697c33b0 147 uint16_t retval;
Kojto 0:615c697c33b0 148
Kojto 0:615c697c33b0 149 ptr = _simple_link.get_transmit_buffer();
Kojto 0:615c697c33b0 150 args = (ptr + HEADERS_SIZE_CMD);
Kojto 0:615c697c33b0 151
Kojto 0:615c697c33b0 152 // Fill in HCI packet structure
Kojto 0:615c697c33b0 153 args = UINT32_TO_STREAM(args, file_id);
Kojto 0:615c697c33b0 154 args = UINT32_TO_STREAM(args, new_len);
Kojto 0:615c697c33b0 155
Kojto 0:615c697c33b0 156 // Initiate a HCI command
Kojto 0:615c697c33b0 157 _hci.command_send(HCI_CMND_NVMEM_CREATE_ENTRY,ptr, NVMEM_CREATE_PARAMS_LEN);
Kojto 0:615c697c33b0 158
Kojto 0:615c697c33b0 159 _event.simplelink_wait_event(HCI_CMND_NVMEM_CREATE_ENTRY, &retval);
Kojto 0:615c697c33b0 160
Kojto 0:615c697c33b0 161 return(retval);
Kojto 0:615c697c33b0 162 }
Kojto 0:615c697c33b0 163
Kojto 0:615c697c33b0 164 #ifndef CC3000_TINY_DRIVER
Kojto 0:615c697c33b0 165 uint8_t cc3000_nvmem::read_sp_version(uint8_t* patch_ver) {
Kojto 0:615c697c33b0 166 uint8_t *ptr;
Kojto 0:615c697c33b0 167 // 1st byte is the status and the rest is the SP version
Kojto 0:615c697c33b0 168 uint8_t retBuf[5];
Kojto 0:615c697c33b0 169
Kojto 0:615c697c33b0 170 ptr = _simple_link.get_transmit_buffer();
Kojto 0:615c697c33b0 171
Kojto 0:615c697c33b0 172 // Initiate a HCI command, no args are required
Kojto 0:615c697c33b0 173 _hci.command_send(HCI_CMND_READ_SP_VERSION, ptr, 0);
Kojto 0:615c697c33b0 174 _event.simplelink_wait_event(HCI_CMND_READ_SP_VERSION, retBuf);
Kojto 0:615c697c33b0 175
Kojto 0:615c697c33b0 176 // package ID
Kojto 0:615c697c33b0 177 *patch_ver = retBuf[3];
Kojto 0:615c697c33b0 178 // package build number
Kojto 0:615c697c33b0 179 *(patch_ver+1) = retBuf[4];
Kojto 0:615c697c33b0 180
Kojto 0:615c697c33b0 181 return(retBuf[0]);
Kojto 0:615c697c33b0 182 }
Kojto 0:615c697c33b0 183
Kojto 0:615c697c33b0 184 #endif
Kojto 0:615c697c33b0 185
Kojto 0:615c697c33b0 186 }