erez i / cc3000_hostdriver_mbedsocket

Dependents:   cc3000_ping_demo_try_2

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cc3000_nvmem.cpp Source File

cc3000_nvmem.cpp

00001 /*****************************************************************************
00002 *
00003 *  C++ interface/implementation created by Martin Kojtal (0xc0170). Thanks to
00004 *  Jim Carver and Frank Vannieuwkerke for their inital cc3000 mbed port and
00005 *  provided help.
00006 *
00007 *  This version of "host driver" uses CC3000 Host Driver Implementation. Thus
00008 *  read the following copyright:
00009 *
00010 *  Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
00011 *
00012 *  Redistribution and use in source and binary forms, with or without
00013 *  modification, are permitted provided that the following conditions
00014 *  are met:
00015 *
00016 *    Redistributions of source code must retain the above copyright
00017 *    notice, this list of conditions and the following disclaimer.
00018 *
00019 *    Redistributions in binary form must reproduce the above copyright
00020 *    notice, this list of conditions and the following disclaimer in the
00021 *    documentation and/or other materials provided with the
00022 *    distribution.
00023 *
00024 *    Neither the name of Texas Instruments Incorporated nor the names of
00025 *    its contributors may be used to endorse or promote products derived
00026 *    from this software without specific prior written permission.
00027 *
00028 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00029 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00030 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00031 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00032 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00033 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00034 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00035 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00036 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00037 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00038 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00039 *
00040 *****************************************************************************/
00041 #include "cc3000.h"
00042 #include "cc3000_nvmem.h"
00043 #include "cc3000_common.h"
00044 
00045 namespace mbed_cc3000 {
00046 
00047 cc3000_nvmem::cc3000_nvmem(cc3000_hci &hci, cc3000_event &event, cc3000_simple_link &simple_link)
00048         : _hci(hci), _event(event), _simple_link(simple_link) {
00049 
00050 }
00051 
00052 cc3000_nvmem::~cc3000_nvmem() {
00053 
00054 }
00055 
00056 int32_t  cc3000_nvmem::read(uint32_t file_id, uint32_t length, uint32_t offset, uint8_t *buff) {
00057     uint8_t ucStatus = 0xFF;
00058     uint8_t *ptr;
00059     uint8_t *args;
00060 
00061     ptr = _simple_link.get_transmit_buffer();
00062     args = (ptr + HEADERS_SIZE_CMD);
00063     // Fill in HCI packet structure
00064     args = UINT32_TO_STREAM(args, file_id);
00065     args = UINT32_TO_STREAM(args, length);
00066     args = UINT32_TO_STREAM(args, offset);
00067 
00068     // Initiate HCI command
00069     _hci.command_send(HCI_CMND_NVMEM_READ, ptr, NVMEM_READ_PARAMS_LEN);
00070     _event.simplelink_wait_event(HCI_CMND_NVMEM_READ, &ucStatus);
00071 
00072     // If data is present, read it even when an error is returned.
00073     // Note: It is the users responsibility to ignore the data when an error is returned.
00074     // Wait for the data in a synchronous way.
00075     //  We assume the buffer is large enough to also store nvmem parameters.
00076     _event.simplelink_wait_data(buff, 0, 0);
00077 
00078     return(ucStatus);
00079 }
00080 
00081 int32_t  cc3000_nvmem::write(uint32_t file_id, uint32_t length, uint32_t entry_offset, uint8_t *buff) {
00082     int32_t iRes;
00083     uint8_t *ptr;
00084     uint8_t *args;
00085 
00086     iRes = EFAIL;
00087 
00088     ptr = _simple_link.get_transmit_buffer();
00089     args = (ptr + SPI_HEADER_SIZE + HCI_DATA_CMD_HEADER_SIZE);
00090 
00091     // Fill in HCI packet structure
00092     args = UINT32_TO_STREAM(args, file_id);
00093     args = UINT32_TO_STREAM(args, 12);
00094     args = UINT32_TO_STREAM(args, length);
00095     args = UINT32_TO_STREAM(args, entry_offset);
00096 
00097     memcpy((ptr + SPI_HEADER_SIZE + HCI_DATA_CMD_HEADER_SIZE +
00098                     NVMEM_WRITE_PARAMS_LEN),buff,length);
00099 
00100     // Initiate a HCI command on the data channel
00101     _hci.data_command_send(HCI_CMND_NVMEM_WRITE, ptr, NVMEM_WRITE_PARAMS_LEN, length);
00102 
00103     _event.simplelink_wait_event(HCI_EVNT_NVMEM_WRITE, &iRes);
00104 
00105     return(iRes);
00106 }
00107 
00108 uint8_t  cc3000_nvmem::set_mac_address(uint8_t *mac) {
00109     return  write(NVMEM_MAC_FILEID, MAC_ADDR_LEN, 0, mac);
00110 }
00111 
00112 uint8_t  cc3000_nvmem::get_mac_address(uint8_t *mac) {
00113     return  read(NVMEM_MAC_FILEID, MAC_ADDR_LEN, 0, mac);
00114 }
00115 
00116 uint8_t  cc3000_nvmem::write_patch(uint32_t file_id, uint32_t length, const uint8_t *data) {
00117     uint8_t     status = 0;
00118     uint16_t    offset = 0;
00119     uint8_t*      spDataPtr = (uint8_t*)data;
00120 
00121     while ((status == 0) && (length >= SP_PORTION_SIZE)) {
00122         status = write(file_id, SP_PORTION_SIZE, offset, spDataPtr);
00123         offset += SP_PORTION_SIZE;
00124         length -= SP_PORTION_SIZE;
00125         spDataPtr += SP_PORTION_SIZE;
00126     }
00127 
00128     if (status !=0) {
00129         // NVMEM error occurred
00130         return status;
00131     }
00132 
00133     if (length != 0) {
00134         // If length MOD 512 is nonzero, write the remaining bytes.
00135         status = write(file_id, length, offset, spDataPtr);
00136     }
00137 
00138     return status;
00139 }
00140 
00141 int32_t  cc3000_nvmem::create_entry(uint32_t file_id, uint32_t new_len) {
00142     uint8_t *ptr;
00143     uint8_t *args;
00144     uint16_t retval;
00145 
00146     ptr = _simple_link.get_transmit_buffer();
00147     args = (ptr + HEADERS_SIZE_CMD);
00148 
00149     // Fill in HCI packet structure
00150     args = UINT32_TO_STREAM(args, file_id);
00151     args = UINT32_TO_STREAM(args, new_len);
00152 
00153     // Initiate a HCI command
00154     _hci.command_send(HCI_CMND_NVMEM_CREATE_ENTRY,ptr, NVMEM_CREATE_PARAMS_LEN);
00155 
00156     _event.simplelink_wait_event(HCI_CMND_NVMEM_CREATE_ENTRY, &retval);
00157 
00158     return(retval);
00159 }
00160 
00161 #ifndef CC3000_TINY_DRIVER
00162 uint8_t cc3000_nvmem::read_sp_version(uint8_t* patch_ver) {
00163     uint8_t *ptr;
00164     // 1st byte is the status and the rest is the SP version
00165     uint8_t retBuf[5];
00166 
00167     ptr = _simple_link.get_transmit_buffer();
00168 
00169    // Initiate a HCI command, no args are required
00170     _hci.command_send(HCI_CMND_READ_SP_VERSION, ptr, 0);
00171     _event.simplelink_wait_event(HCI_CMND_READ_SP_VERSION, retBuf);
00172 
00173     // package ID
00174     *patch_ver = retBuf[3];
00175     // package build number
00176     *(patch_ver+1) = retBuf[4];
00177 
00178     return(retBuf[0]);
00179 }
00180 #endif
00181 
00182 } // mbed_cc3000 namespace