Added mutex for multiple SPI devices on the same SPI bus

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Committer:
vpcola
Date:
Thu Oct 16 13:39:08 2014 +0000
Revision:
47:cc9a2501e29f
Parent:
45:50ab13d8f2dc
Added mutex if used in rtos

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