Added mutex for multiple SPI devices on the same SPI bus

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Revision:
45:50ab13d8f2dc
Parent:
44:960b73df5981
Child:
46:ca8c234997c0
--- a/cc3000.cpp	Sun Oct 13 11:46:21 2013 +0200
+++ b/cc3000.cpp	Wed Nov 06 17:56:25 2013 +0100
@@ -47,36 +47,119 @@
 static uint8_t cc3000_prefix[] = {'T', 'T', 'T'};
 cc3000 *cc3000::_inst;
 
-cc3000::cc3000(PinName cc3000_irq, PinName cc3000_en, PinName cc3000_cs, SPI cc3000_spi, IRQn_Type irq_port)
-            :  _event(_simple_link, _hci, _spi, *this), _socket(_simple_link, _hci, _event), _spi(cc3000_irq, cc3000_en, cc3000_cs, cc3000_spi, irq_port, _event, _simple_link), _hci(_spi),
-            _nvmem(_hci, _event, _simple_link), _netapp(_simple_link, _nvmem, _hci, _event), _wlan(_simple_link, _event, _spi, _hci) {
-    /* TODO - pIRQ riorities ?? */
-
+cc3000::cc3000(PinName cc3000_irq, PinName cc3000_en, PinName cc3000_cs, SPI cc3000_spi)
+             : _event(_simple_link, _hci, _spi, *this), _socket(_simple_link, _hci, _event),
+               _spi(cc3000_irq, cc3000_en, cc3000_cs, cc3000_spi, _event, _simple_link), _hci(_spi),
+               _nvmem(_hci, _event, _simple_link), _netapp(_simple_link, _nvmem, _hci, _event),
+               _wlan(_simple_link, _event, _spi, _hci) {
     _simple_link.set_tx_complete_signal(1);
-    _status.dhcp = 0;
-    _status.connected = 0;
-    _status.socket = 0;
-    _status.dhcp_configured = 0;
-    _status.smart_config_complete = 0;
-    _status.stop_smart_config = 0;
-    _status.ok_to_shut_down = 0;
-    _status.enabled = 0;
-
+    memset(&_status, 0, sizeof(_status));
     _inst = this;
 }
 
 cc3000::~cc3000() {
-
 }
 
 #if (CC3000_ETH_COMPAT == 1)
+cc3000::cc3000(PinName cc3000_irq, PinName cc3000_en, PinName cc3000_cs, SPI cc3000_spi, const char *ssid,
+               const char *phrase, Security sec, bool smart_config)
+             : _event(_simple_link, _hci, _spi, *this), _socket(_simple_link, _hci, _event),
+               _spi(cc3000_irq, cc3000_en, cc3000_cs, cc3000_spi, _event, _simple_link), _hci(_spi),
+               _nvmem(_hci, _event, _simple_link), _netapp(_simple_link, _nvmem, _hci, _event),
+               _wlan(_simple_link, _event, _spi, _hci), _sec(sec), _smart_config(smart_config) {
+    _simple_link.set_tx_complete_signal(1);
+    memset(&_status, 0, sizeof(_status));
+    strcpy((char *)_ssid, ssid);
+    strcpy((char *)_phrase, phrase);
+    _inst = this;
+}
+
 // Ethernet library compatible, functions return strings
 // Caches the ipconfig from the usync callback
-static char mac_addr[19];
+static char mac_addr[19]= "\0";
 static char ip_addr[17] = "\0";
 static char gateway[17] = "\0";
 static char networkmask[17] = "\0";
 
+void cc3000::init() {
+    _wlan.start(0);
+
+    uint32_t subnet[4] = {0};
+    uint32_t ip[4] = {0};
+    uint32_t getway[4] = {0};
+    uint32_t dns[4] = {0};
+
+    _netapp.dhcp(ip, subnet, getway, dns);
+    _wlan.stop();
+    wait(1);
+    _wlan.start(0);
+
+    _status.enabled = 1;
+    _wlan.set_event_mask(HCI_EVNT_WLAN_UNSOL_INIT | HCI_EVNT_WLAN_KEEPALIVE | HCI_EVNT_WLAN_ASYNC_PING_REPORT);
+}
+
+void cc3000::init(const char *ip, const char *mask, const char *gateway) {
+    _netapp.dhcp((uint32_t *)ip, (uint32_t *)mask, (uint32_t *)gateway, (uint32_t *)ip); //dns = ip
+    _wlan.stop();
+    wait(1);
+    _wlan.start(0);
+
+    _status.enabled = 1;
+    _wlan.set_event_mask(HCI_EVNT_WLAN_UNSOL_INIT | HCI_EVNT_WLAN_KEEPALIVE | HCI_EVNT_WLAN_ASYNC_PING_REPORT);
+}
+
+int cc3000::connect(unsigned int timeout_ms) {
+    Timer t;
+    int ret = 0;
+
+    if (_smart_config == false) {
+        _wlan.ioctl_set_connection_policy(0, 0, 0);
+    } else {
+        tUserFS user_info;
+        get_user_file_info((uint8_t *)&user_info, sizeof(user_info));
+        if (user_info.FTC == 1) {
+            _wlan.ioctl_set_connection_policy(0, 1, 1);
+        } else {
+            DBG_CC("Smart config is not set. Please run the first time configuration.");
+            return -1;
+        }
+    }
+
+    t.start();
+    while (is_connected() == false) {
+        if (strlen((const char *)_phrase) < 8) {
+            if (connect_open(_ssid)) {
+                break;
+            }
+        } else {
+#ifndef CC3000_TINY_DRIVER
+            if (connect_secure(_ssid,_phrase, _sec)) {
+                break;
+            }
+#else
+            return -1; /* secure connection not supported with TINY_DRIVER */
+#endif
+        }
+
+        if (t.read_ms() > timeout_ms) {
+            ret = -1;
+            DBG_CC("Connection to AP failed");
+            break;
+        }
+    }
+
+    while (is_dhcp_configured() == false)
+    {
+        if (t.read_ms() > timeout_ms) {
+            ret = -1;
+            DBG_CC("Connection to AP failed");
+            break;
+        }
+    }
+
+    return ret;
+}
+
 char* cc3000::getMACAddress() {
     return mac_addr;
 }
@@ -92,34 +175,39 @@
 char* cc3000::getNetworkMask() {
     return networkmask;
 }
+
+int cc3000::disconnect(void){
+    if (_wlan.disconnect()) {
+        return -1;
+    } else {
+        return 0;
+    }
+}
+
 #endif
 
 void cc3000::usync_callback(int32_t event_type, uint8_t *data, uint8_t length) {
-    if (event_type == HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE)
-    {
+    if (event_type == HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE) {
         DBG_CC("Callback : HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE");
         _status.smart_config_complete = 1;
         _status.stop_smart_config = 1;
     }
 
-    if (event_type == HCI_EVNT_WLAN_UNSOL_CONNECT)
-    {
+    if (event_type == HCI_EVNT_WLAN_UNSOL_CONNECT) {
         DBG_CC("Callback : HCI_EVNT_WLAN_UNSOL_CONNECT");
         _status.connected = 1;
         // Connect message is always followed by a DHCP message, connection is not useable until then
         _status.dhcp      = 0;
     }
 
-    if (event_type == HCI_EVNT_WLAN_UNSOL_DISCONNECT)
-    {
+    if (event_type == HCI_EVNT_WLAN_UNSOL_DISCONNECT) {
         DBG_CC("Callback : HCI_EVNT_WLAN_UNSOL_DISCONNECT");
         _status.connected = 0;
         _status.dhcp      = 0;
         _status.dhcp_configured = 0;
     }
 
-    if (event_type == HCI_EVNT_WLAN_UNSOL_DHCP)
-    {
+    if (event_type == HCI_EVNT_WLAN_UNSOL_DHCP) {
 #if (CC3000_ETH_COMPAT == 1)
         _socket.inet_ntoa_r( htonl(*((uint32_t *)(&data[NETAPP_IPCONFIG_IP_OFFSET]))), ip_addr, 17);
         _socket.inet_ntoa_r( htonl(*((uint32_t *)(&data[NETAPP_IPCONFIG_GW_OFFSET]))), gateway, 17);
@@ -135,22 +223,19 @@
         }
     }
 
-    if (event_type == HCI_EVENT_CC3000_CAN_SHUT_DOWN)
-    {
+    if (event_type == HCI_EVENT_CC3000_CAN_SHUT_DOWN) {
         // Note this means the modules is idle, so it could be shutdown..
         //DBG_CC("Callback : HCI_EVENT_CC3000_CAN_SHUT_DOWN");
         _status.ok_to_shut_down = 1;
     }
 
-    if (event_type == HCI_EVNT_WLAN_ASYNC_PING_REPORT)
-    {
+    if (event_type == HCI_EVNT_WLAN_ASYNC_PING_REPORT) {
         DBG_CC("Callback : HCI_EVNT_WLAN_ASYNC_PING_REPORT");
         memcpy(&_ping_report, data, length);
     }
 
     if (event_type == HCI_EVNT_BSD_TCP_CLOSE_WAIT) {
-        uint8_t socketnum;
-        socketnum = data[0];
+        uint8_t socketnum = data[0];
         DBG_CC("Callback : HCI_EVNT_BSD_TCP_CLOSE_WAIT - Socket : %d", socketnum);
         if (socketnum < MAX_SOCKETS) {
             _closed_sockets[socketnum] = true; /* clients socket is closed */
@@ -162,14 +247,12 @@
     _status.smart_config_complete = 0;
     _wlan.ioctl_set_connection_policy(0, 0, 0);
 
-    if (_status.connected == 1)
-    {
+    if (_status.connected == 1) {
         disconnect();
     }
 
     //Wait until CC3000 is disconected
-    while (_status.connected == 1)
-    {
+    while (_status.connected == 1) {
         wait_us(5);
         _event.hci_unsolicited_event_handler();
     }
@@ -182,8 +265,7 @@
     DBG_CC("Waiting for smartconfig to be completed");
 
     // Wait for Smart config finished
-    while (_status.smart_config_complete == 0)
-    {
+    while (_status.smart_config_complete == 0) {
         wait_ms(100);
     }
 
@@ -214,6 +296,9 @@
 }
 
 bool cc3000::connect_secure(const uint8_t *ssid, const uint8_t *key, int32_t security_mode) {
+#ifdef CC3000_TINY_DRIVER
+    return false; /* not supported*/
+#else
     uint32_t ret;
 
     //_wlan.disconnect();
@@ -225,24 +310,20 @@
       ret = false;
     }
     return ret;
+#endif
 }
 
 bool cc3000::connect_non_blocking(const uint8_t *ssid, const uint8_t *key, int32_t security_mode)
 {
-bool ret = false;
+    bool ret = false;
 
-    if (key == 0)
-    {
-        if (connect_open(ssid))
-        {
+    if (key == 0) {
+        if (connect_open(ssid)) {
             ret = true;
         }
-    }
-    else
-    {
+    } else {
     #ifndef CC3000_TINY_DRIVER
-        if (connect_secure(ssid,key,security_mode))
-        {
+        if (connect_secure(ssid,key,security_mode)) {
             ret = true;
         }
     #else
@@ -254,7 +335,7 @@
 }
 
 bool cc3000::connect_to_AP(const uint8_t *ssid, const uint8_t *key, int32_t security_mode) {
-    Timer t;  /* TODO static? */
+    Timer t;
     bool ret = true;
 
     t.start();
@@ -274,11 +355,9 @@
         }
 
         /* timeout 10 seconds */
-        if (t.read_ms() > 10000){
+        if (t.read_ms() > 10000) {
             ret = false;
-
             DBG_CC("Connection to AP failed");
-
             break;
         }
     }
@@ -289,7 +368,7 @@
 void cc3000::start(uint8_t patch) {
     _wlan.start(patch);
     _status.enabled = 1;
-    _wlan.set_event_mask(HCI_EVNT_WLAN_UNSOL_INIT | HCI_EVNT_WLAN_KEEPALIVE);
+    _wlan.set_event_mask(HCI_EVNT_WLAN_UNSOL_INIT | HCI_EVNT_WLAN_KEEPALIVE | HCI_EVNT_WLAN_ASYNC_PING_REPORT);
 }
 
 void cc3000::stop(void) {
@@ -306,10 +385,9 @@
 }
 
 bool cc3000::connect_open(const uint8_t *ssid) {
-    uint32_t ret;
-
     _wlan.disconnect();
     wait_ms(3);
+    uint32_t ret;
 #ifndef CC3000_TINY_DRIVER
     ret = _wlan.connect(0,ssid, strlen((const char *)ssid), 0, 0, 0);
 #else
@@ -329,13 +407,10 @@
 }
 
 bool cc3000::is_connected() {
-    if (( _status.connected ) && ( _status.dhcp ))
-    {
-        return( 1 );
-    }
-    else
-    {
-        return( 0 );
+    if (( _status.connected ) && ( _status.dhcp )) {
+        return 1;
+    } else {
+        return 0;
     }
 }
 
@@ -371,11 +446,10 @@
 #endif
 
 void cc3000::delete_profiles(void) {
-    tUserFS user_info;
-
     _wlan.ioctl_set_connection_policy(0, 0, 0);
     _wlan.ioctl_del_profile(255);
 
+    tUserFS user_info;
     get_user_file_info((uint8_t *)&user_info, sizeof(user_info));
     user_info.FTC = 0;
     set_user_file_info((uint8_t *)&user_info, sizeof(user_info));
@@ -385,15 +459,8 @@
     _nvmem.write( NVMEM_USER_FILE_1_FILEID, size, 0, info_file);
 }
 
-bool cc3000::disconnect(void){
-    if (_wlan.disconnect()) {
-        return false;
-    } else {
-        return true;
-    }
-}
-
 uint32_t cc3000::ping(uint32_t ip, uint8_t attempts, uint16_t timeout, uint8_t size) {
+#ifndef CC3000_TINY_DRIVER
     uint32_t reversed_ip = (ip >> 24) | ((ip >> 8) & 0xFF00) | ((ip << 8) & 0xFF0000) | (ip << 24);
 
     _ping_report.packets_received = 0;
@@ -412,6 +479,9 @@
     DBG_CC("Avg time: %d",_ping_report.avg_round_time);
 
     return _ping_report.packets_received;
+#else
+    return 0;
+#endif
 }
 
 /* Conversion between uint types and C strings */
@@ -448,5 +518,5 @@
            (*(p + offset + 1)) << 8) + (uint32_t)(*(p + offset)));
 }
 
-} /* end of mbed_cc3000 namespace */
+} // mbed_cc3000 namespace