A Port of TI's Webserver for the CC3000

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nvmem.cpp Source File

nvmem.cpp

00001 /*****************************************************************************
00002 *
00003 *  nvmem.c  - CC3000 Host Driver Implementation.
00004 *  Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
00005 *
00006 *  Redistribution and use in source and binary forms, with or without
00007 *  modification, are permitted provided that the following conditions
00008 *  are met:
00009 *
00010 *    Redistributions of source code must retain the above copyright
00011 *    notice, this list of conditions and the following disclaimer.
00012 *
00013 *    Redistributions in binary form must reproduce the above copyright
00014 *    notice, this list of conditions and the following disclaimer in the
00015 *    documentation and/or other materials provided with the   
00016 *    distribution.
00017 *
00018 *    Neither the name of Texas Instruments Incorporated nor the names of
00019 *    its contributors may be used to endorse or promote products derived
00020 *    from this software without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00025 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
00026 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
00027 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
00028 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00029 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00030 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00031 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00032 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 *
00034 *****************************************************************************/
00035 
00036 //*****************************************************************************
00037 //
00038 //! \addtogroup nvmem_api
00039 //! @{
00040 //
00041 //*****************************************************************************
00042 
00043 #include <stdio.h>
00044 #include <string.h>
00045 #include "nvmem.h"
00046 #include "hci.h"
00047 #include "socket.h"
00048 #include "evnt_handler.h"
00049 
00050 //*****************************************************************************
00051 //
00052 // Prototypes for the structures for APIs.
00053 //
00054 //*****************************************************************************
00055 
00056 #define NVMEM_READ_PARAMS_LEN   (12)
00057 #define NVMEM_CREATE_PARAMS_LEN     (8)
00058 #define NVMEM_WRITE_PARAMS_LEN  (16)
00059 
00060 //*****************************************************************************
00061 //
00062 //!  nvmem_read
00063 //!
00064 //!  @param  ulFileId   nvmem file id:\n
00065 //!                     NVMEM_NVS_FILEID, NVMEM_NVS_SHADOW_FILEID,
00066 //!                     NVMEM_WLAN_CONFIG_FILEID, NVMEM_WLAN_CONFIG_SHADOW_FILEID,
00067 //!                     NVMEM_WLAN_DRIVER_SP_FILEID, NVMEM_WLAN_FW_SP_FILEID,
00068 //!                     NVMEM_MAC_FILEID, NVMEM_FRONTEND_VARS_FILEID,
00069 //!                     NVMEM_IP_CONFIG_FILEID, NVMEM_IP_CONFIG_SHADOW_FILEID,
00070 //!                     NVMEM_BOOTLOADER_SP_FILEID, NVMEM_RM_FILEID,
00071 //!                     and user files 12-15.
00072 //!  @param  ulLength    number of bytes to read 
00073 //!  @param  ulOffset    ulOffset in file from where to read  
00074 //!  @param  buff        output buffer pointer
00075 //!
00076 //!  @return       number of bytes read, otherwise error.
00077 //!
00078 //!  @brief       Reads data from the file referred by the ulFileId parameter. 
00079 //!               Reads data from file ulOffset till length. Err if the file can't
00080 //!               be used, is invalid, or if the read is out of bounds. 
00081 //!  
00082 //*****************************************************************************
00083 
00084 signed long 
00085 nvmem_read(unsigned long ulFileId, unsigned long ulLength, unsigned long ulOffset, unsigned char *buff)
00086 {
00087     unsigned char ucStatus = 0xFF;
00088     unsigned char *ptr;
00089     unsigned char *args;
00090     
00091     ptr = tSLInformation.pucTxCommandBuffer;
00092     args = (ptr + HEADERS_SIZE_CMD);
00093     
00094     // Fill in HCI packet structure
00095     args = UINT32_TO_STREAM(args, ulFileId);
00096     args = UINT32_TO_STREAM(args, ulLength);
00097     args = UINT32_TO_STREAM(args, ulOffset);
00098     
00099     // Initiate a HCI command
00100     hci_command_send(HCI_CMND_NVMEM_READ, ptr, NVMEM_READ_PARAMS_LEN);
00101     SimpleLinkWaitEvent(HCI_CMND_NVMEM_READ, &ucStatus);
00102     
00103     // In case there is data - read it - even if an error code is returned
00104    // Note: It is the user responsibility to ignore the data in case of an error code
00105     
00106     // Wait for the data in a synchronous way. Here we assume that the buffer is 
00107     // big enough to store also parameters of nvmem
00108     
00109     SimpleLinkWaitData(buff, 0, 0);
00110     
00111     return(ucStatus);
00112 }
00113 
00114 //*****************************************************************************
00115 //
00116 //!  nvmem_write
00117 //!
00118 //!  @param  ulFileId nvmem file id:\n
00119 //!                   NVMEM_WLAN_DRIVER_SP_FILEID, NVMEM_WLAN_FW_SP_FILEID,
00120 //!                   NVMEM_MAC_FILEID, NVMEM_BOOTLOADER_SP_FILEID,
00121 //!                   and user files 12-15.
00122 //!  @param  ulLength       number of bytes to write  
00123 //!  @param  ulEntryOffset  offset in file to start write operation from 
00124 //!  @param  buff           data to write
00125 //!
00126 //!  @return       on success 0, error otherwise.
00127 //!
00128 //!  @brief       Write data to nvmem.
00129 //!               writes data to file referred by the ulFileId parameter. 
00130 //!               Writes data to file ulOffset till ulLength.The file id will be 
00131 //!               marked invalid till the write is done. The file entry doesn't
00132 //!               need to be valid - only allocated.
00133 //!  
00134 //*****************************************************************************
00135 
00136 signed long 
00137 nvmem_write(unsigned long ulFileId, unsigned long ulLength, unsigned long 
00138                         ulEntryOffset, unsigned char *buff)
00139 {
00140     long iRes;
00141     unsigned char *ptr;
00142     unsigned char *args;
00143     
00144     iRes = EFAIL;
00145     
00146     ptr = tSLInformation.pucTxCommandBuffer;
00147     args = (ptr + SPI_HEADER_SIZE + HCI_DATA_CMD_HEADER_SIZE);
00148     
00149     // Fill in HCI packet structure
00150     args = UINT32_TO_STREAM(args, ulFileId);
00151     args = UINT32_TO_STREAM(args, 12);
00152     args = UINT32_TO_STREAM(args, ulLength);
00153     args = UINT32_TO_STREAM(args, ulEntryOffset);
00154     
00155     memcpy((ptr + SPI_HEADER_SIZE + HCI_DATA_CMD_HEADER_SIZE + 
00156                     NVMEM_WRITE_PARAMS_LEN),buff,ulLength);
00157     
00158     // Initiate a HCI command but it will come on data channel
00159     hci_data_command_send(HCI_CMND_NVMEM_WRITE, ptr, NVMEM_WRITE_PARAMS_LEN,
00160                                                 ulLength);
00161     
00162     SimpleLinkWaitEvent(HCI_EVNT_NVMEM_WRITE, &iRes);
00163     
00164     return(iRes);
00165 }
00166 
00167 
00168 //*****************************************************************************
00169 //
00170 //!  nvmem_set_mac_address
00171 //!
00172 //!  @param  mac   mac address to be set
00173 //!
00174 //!  @return       on success 0, error otherwise.
00175 //!
00176 //!  @brief       Write MAC address to EEPROM. 
00177 //!               mac address as appears over the air (OUI first)
00178 //!  
00179 //*****************************************************************************
00180 
00181 unsigned char nvmem_set_mac_address(unsigned char *mac)
00182 {
00183     return  nvmem_write(NVMEM_MAC_FILEID, MAC_ADDR_LEN, 0, mac);
00184 }
00185 
00186 //*****************************************************************************
00187 //
00188 //!  nvmem_get_mac_address
00189 //!
00190 //!  @param[out]  mac   mac address  
00191 //!
00192 //!  @return       on success 0, error otherwise.
00193 //!
00194 //!  @brief       Read MAC address from EEPROM. 
00195 //!               mac address as appears over the air (OUI first)
00196 //!  
00197 //*****************************************************************************
00198 
00199 unsigned char nvmem_get_mac_address(unsigned char *mac)
00200 {
00201     return  nvmem_read(NVMEM_MAC_FILEID, MAC_ADDR_LEN, 0, mac);
00202 }
00203 
00204 //*****************************************************************************
00205 //
00206 //!  nvmem_write_patch
00207 //!
00208 //!  @param  ulFileId   nvmem file id:\n
00209 //!                     NVMEM_WLAN_DRIVER_SP_FILEID, NVMEM_WLAN_FW_SP_FILEID,
00210 //!  @param  spLength   number of bytes to write 
00211 //!  @param  spData     SP data to write
00212 //!
00213 //!  @return       on success 0, error otherwise.
00214 //!
00215 //!  @brief      program a patch to a specific file ID. 
00216 //!              The SP data is assumed to be organized in 2-dimensional.
00217 //!              Each line is SP_PORTION_SIZE bytes long. Actual programming is 
00218 //!              applied in SP_PORTION_SIZE bytes portions.
00219 //!  
00220 //*****************************************************************************
00221 
00222 unsigned char nvmem_write_patch(unsigned long ulFileId, unsigned long spLength, const unsigned char *spData)
00223 {
00224     unsigned char   status = 0;
00225     unsigned short  offset = 0;
00226     unsigned char*      spDataPtr = (unsigned char*)spData;
00227     
00228     while ((status == 0) && (spLength >= SP_PORTION_SIZE))
00229     {
00230         status = nvmem_write(ulFileId, SP_PORTION_SIZE, offset, spDataPtr);
00231         offset += SP_PORTION_SIZE;
00232         spLength -= SP_PORTION_SIZE;
00233         spDataPtr += SP_PORTION_SIZE;
00234     }
00235     
00236     if (status !=0)
00237     {
00238         // NVMEM error occurred
00239         return status;
00240     }
00241     
00242     if (spLength != 0)
00243     {
00244         // if reached here, a reminder is left
00245         status = nvmem_write(ulFileId, spLength, offset, spDataPtr);
00246     }
00247     
00248     return status;
00249 }
00250 
00251 //*****************************************************************************
00252 //
00253 //!  nvmem_read_sp_version
00254 //!
00255 //!  @param[out]  patchVer    first number indicates package ID and the second 
00256 //!                           number indicates package build number   
00257 //!
00258 //!  @return       on success  0, error otherwise.
00259 //!
00260 //!  @brief      Read patch version. read package version (WiFi FW patch, 
00261 //!              driver-supplicant-NS patch, bootloader patch)
00262 //!  
00263 //*****************************************************************************
00264 
00265 #ifndef CC3000_TINY_DRIVER
00266 unsigned char nvmem_read_sp_version(unsigned char* patchVer)
00267 {
00268     unsigned char *ptr;
00269     // 1st byte is the status and the rest is the SP version
00270     unsigned char   retBuf[5];  
00271     
00272     ptr = tSLInformation.pucTxCommandBuffer;
00273   
00274    // Initiate a HCI command, no args are required
00275     hci_command_send(HCI_CMND_READ_SP_VERSION, ptr, 0); 
00276     SimpleLinkWaitEvent(HCI_CMND_READ_SP_VERSION, retBuf);
00277     
00278     // package ID
00279     *patchVer = retBuf[3];          
00280     // package build number
00281     *(patchVer+1) = retBuf[4];      
00282     
00283     return(retBuf[0]);
00284 }
00285 #endif
00286 
00287 //*****************************************************************************
00288 //
00289 //!  nvmem_create_entry
00290 //!
00291 //!  @param       ulFileId    nvmem file Id:\n
00292 //!                           * NVMEM_AES128_KEY_FILEID: 12
00293 //!                           * NVMEM_SHARED_MEM_FILEID: 13
00294 //!                           * and fileIDs 14 and 15
00295 //!  @param       ulNewLen    entry ulLength  
00296 //!
00297 //!  @return       on success 0, error otherwise.
00298 //!
00299 //!  @brief      Create new file entry and allocate space on the NVMEM. 
00300 //!              Applies only to user files.
00301 //!              Modify the size of file.
00302 //!              If the entry is unallocated - allocate it to size 
00303 //!              ulNewLen (marked invalid).
00304 //!              If it is allocated then deallocate it first.
00305 //!              To just mark the file as invalid without resizing - 
00306 //!              set ulNewLen=0.
00307 //!  
00308 //*****************************************************************************
00309 
00310 signed long 
00311 nvmem_create_entry(unsigned long ulFileId, unsigned long ulNewLen)
00312 {
00313     unsigned char *ptr; 
00314     unsigned char *args;
00315     unsigned short retval;
00316     
00317     ptr = tSLInformation.pucTxCommandBuffer;
00318     args = (ptr + HEADERS_SIZE_CMD);
00319     
00320     // Fill in HCI packet structure
00321     args = UINT32_TO_STREAM(args, ulFileId);
00322     args = UINT32_TO_STREAM(args, ulNewLen);
00323     
00324     // Initiate a HCI command
00325     hci_command_send(HCI_CMND_NVMEM_CREATE_ENTRY,ptr, NVMEM_CREATE_PARAMS_LEN);
00326     
00327     SimpleLinkWaitEvent(HCI_CMND_NVMEM_CREATE_ENTRY, &retval);
00328     
00329     return(retval);
00330 }
00331 
00332 
00333 
00334 //*****************************************************************************
00335 //
00336 // Close the Doxygen group.
00337 //! @}
00338 //
00339 //*****************************************************************************
00340 
00341