Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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, (long*)&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 + NVMEM_WRITE_PARAMS_LEN),buff,ulLength); 00156 00157 // Initiate a HCI command but it will come on data channel 00158 hci_data_command_send(HCI_CMND_NVMEM_WRITE, ptr, NVMEM_WRITE_PARAMS_LEN, ulLength); 00159 00160 SimpleLinkWaitEvent(HCI_EVNT_NVMEM_WRITE, &iRes); 00161 00162 return(iRes); 00163 } 00164 00165 00166 //***************************************************************************** 00167 // 00168 //! nvmem_set_mac_address 00169 //! 00170 //! @param mac mac address to be set 00171 //! 00172 //! @return on success 0, error otherwise. 00173 //! 00174 //! @brief Write MAC address to EEPROM. 00175 //! mac address as appears over the air (OUI first) 00176 //! 00177 //***************************************************************************** 00178 00179 unsigned char nvmem_set_mac_address(unsigned char *mac) 00180 { 00181 return nvmem_write(NVMEM_MAC_FILEID, MAC_ADDR_LEN, 0, mac); 00182 } 00183 00184 //***************************************************************************** 00185 // 00186 //! nvmem_get_mac_address 00187 //! 00188 //! @param[out] mac mac address 00189 //! 00190 //! @return on success 0, error otherwise. 00191 //! 00192 //! @brief Read MAC address from EEPROM. 00193 //! mac address as appears over the air (OUI first) 00194 //! 00195 //***************************************************************************** 00196 00197 unsigned char nvmem_get_mac_address(unsigned char *mac) 00198 { 00199 return nvmem_read(NVMEM_MAC_FILEID, MAC_ADDR_LEN, 0, mac); 00200 } 00201 00202 //***************************************************************************** 00203 // 00204 //! nvmem_write_patch 00205 //! 00206 //! @param ulFileId nvmem file id:\n 00207 //! NVMEM_WLAN_DRIVER_SP_FILEID, NVMEM_WLAN_FW_SP_FILEID, 00208 //! @param spLength number of bytes to write 00209 //! @param spData SP data to write 00210 //! 00211 //! @return on success 0, error otherwise. 00212 //! 00213 //! @brief program a patch to a specific file ID. 00214 //! The SP data is assumed to be organized in 2-dimensional. 00215 //! Each line is SP_PORTION_SIZE bytes long. Actual programming is 00216 //! applied in SP_PORTION_SIZE bytes portions. 00217 //! 00218 //***************************************************************************** 00219 00220 unsigned char nvmem_write_patch(unsigned long ulFileId, unsigned long spLength, const unsigned char *spData) 00221 { 00222 unsigned char status = 0; 00223 unsigned short offset = 0; 00224 unsigned char* spDataPtr = (unsigned char*)spData; 00225 00226 while ((status == 0) && (spLength >= SP_PORTION_SIZE)) 00227 { 00228 status = nvmem_write(ulFileId, SP_PORTION_SIZE, offset, spDataPtr); 00229 offset += SP_PORTION_SIZE; 00230 spLength -= SP_PORTION_SIZE; 00231 spDataPtr += SP_PORTION_SIZE; 00232 } 00233 00234 if (status !=0) 00235 { 00236 // NVMEM error occurred 00237 return status; 00238 } 00239 00240 if (spLength != 0) 00241 { 00242 // if reached here, a reminder is left 00243 status = nvmem_write(ulFileId, spLength, offset, spDataPtr); 00244 } 00245 00246 return status; 00247 } 00248 00249 //***************************************************************************** 00250 // 00251 //! nvmem_read_sp_version 00252 //! 00253 //! @param[out] patchVer first number indicates package ID and the second 00254 //! number indicates package build number 00255 //! 00256 //! @return on success 0, error otherwise. 00257 //! 00258 //! @brief Read patch version. read package version (WiFi FW patch, 00259 //! driver-supplicant-NS patch, bootloader patch) 00260 //! 00261 //***************************************************************************** 00262 00263 #ifndef CC3000_TINY_DRIVER 00264 unsigned char nvmem_read_sp_version(unsigned char* patchVer) 00265 { 00266 unsigned char *ptr; 00267 // 1st byte is the status and the rest is the SP version 00268 unsigned char retBuf[5]; 00269 00270 ptr = tSLInformation.pucTxCommandBuffer; 00271 00272 // Initiate a HCI command, no args are required 00273 hci_command_send(HCI_CMND_READ_SP_VERSION, ptr, 0); 00274 SimpleLinkWaitEvent(HCI_CMND_READ_SP_VERSION, (long*)retBuf); 00275 00276 // package ID 00277 *patchVer = retBuf[3]; 00278 // package build number 00279 *(patchVer+1) = retBuf[4]; 00280 00281 return(retBuf[0]); 00282 } 00283 #endif 00284 00285 //***************************************************************************** 00286 // 00287 //! nvmem_create_entry 00288 //! 00289 //! @param ulFileId nvmem file Id:\n 00290 //! * NVMEM_AES128_KEY_FILEID: 12 00291 //! * NVMEM_SHARED_MEM_FILEID: 13 00292 //! * and fileIDs 14 and 15 00293 //! @param ulNewLen entry ulLength 00294 //! 00295 //! @return on success 0, error otherwise. 00296 //! 00297 //! @brief Create new file entry and allocate space on the NVMEM. 00298 //! Applies only to user files. 00299 //! Modify the size of file. 00300 //! If the entry is unallocated - allocate it to size 00301 //! ulNewLen (marked invalid). 00302 //! If it is allocated then deallocate it first. 00303 //! To just mark the file as invalid without resizing - 00304 //! set ulNewLen=0. 00305 //! 00306 //***************************************************************************** 00307 00308 signed long 00309 nvmem_create_entry(unsigned long ulFileId, unsigned long ulNewLen) 00310 { 00311 unsigned char *ptr; 00312 unsigned char *args; 00313 unsigned short retval; 00314 00315 ptr = tSLInformation.pucTxCommandBuffer; 00316 args = (ptr + HEADERS_SIZE_CMD); 00317 00318 // Fill in HCI packet structure 00319 args = UINT32_TO_STREAM(args, ulFileId); 00320 args = UINT32_TO_STREAM(args, ulNewLen); 00321 00322 // Initiate a HCI command 00323 hci_command_send(HCI_CMND_NVMEM_CREATE_ENTRY,ptr, NVMEM_CREATE_PARAMS_LEN); 00324 00325 SimpleLinkWaitEvent(HCI_CMND_NVMEM_CREATE_ENTRY, (long*)&retval); 00326 00327 return(retval); 00328 } 00329 00330 00331 00332 //***************************************************************************** 00333 // 00334 // Close the Doxygen group. 00335 //! @} 00336 // 00337 //***************************************************************************** 00338 00339
Generated on Tue Jul 12 2022 19:26:44 by
1.7.2