wadetb / tinyhci

A tiny driver for the Texas Instruments CC3000 wireless network controller
15 stars 4 forks source link

It doesn't get a DHCP address #6

Open Makuna opened 9 years ago

Makuna commented 9 years ago

My CC3000 is the latest firmware, I am using the latest Arduino IDE 1.6. I am running on a Mega2560. My setup works fine with Adafruit library.

This is connecting to a secured wifi.

I changed the EN pin to 5 (what my hardware is setup for) and I never get a DHCP address.

wlan_ioctl_set_connection_policy retuns zero wlan_connect returns zero

but the event callback that sets wifi_dhcp to 1 never happens. I turned on level 2 debug, see dump below

Source Code:

#include <Arduino.h>
#include <SPI.h>
#include <tinyhci.h>

#define WLAN_SSID      "ATF Tactical Van"           // cannot be longer than 32 characters!
#define WLAN_PASS      "ThisIsPassword"
#define WLAN_SECURITY  WLAN_SEC_WPA2
#define WLAN_TIMEOUT   60000

void wlan_callback(uint16_t event)
{
    //if (event == HCI_EVNT_WLAN_UNSOL_TCP_CLOSE_WAIT)
    //    client_socket = -1;
}

bool wifi_connect(void)
{
    int32_t result;

    result = wlan_ioctl_set_connection_policy(false, false, false);
    Serial.print(F("policy "));
    Serial.println(result);

    result = wlan_connect(WLAN_SECURITY,
            WLAN_SSID, 
            strlen(WLAN_SSID), 
            0, 
            (unsigned char*)WLAN_PASS, 
            strlen(WLAN_PASS));
    Serial.print(F("connect "));
    Serial.println(result);

    int time = millis();

    //while (!wifi_connected)
    //{
    //    if ((millis() - time) > WLAN_TIMEOUT)
    //    {
    //        Serial.println(F("Failed to connect!"));
    //        return false;
    //    }
    //}

    //time = millis();
    while (!wifi_dhcp)
    {
        if ((millis() - time) > WLAN_TIMEOUT)
        {
            Serial.println(F("Failed to get DHCP assinged address!"));
            return false;
        }
    }
    return true;
}

bool displayConnectionDetails(void)
{
    netapp_ipconfig_t ipConfig;

    netappipconfig(&ipConfig);

    Serial.print(F("\nIP Addr: ")); printIpAddress(ipConfig.ipAddr);
    Serial.print(F("\Subnet  : ")); printIpAddress(ipConfig.subnet);
    Serial.print(F("\nGateway: ")); printIpAddress(ipConfig.gateway);
    Serial.print(F("\nDHCPsrv: ")); printIpAddress(ipConfig.DHCPServer);
    Serial.print(F("\nDNSserv: ")); printIpAddress(ipConfig.DNSServer);
    Serial.print(F("\nMac    : "));
    for (int i = 0; i < sizeof(ipConfig.macAddr); i++)
    {
        Serial.print(ipConfig.macAddr[i], HEX);
        if (i < sizeof(ipConfig.macAddr) - 1)
        {
            Serial.print(F("-"));
        }
    }
    Serial.println();
    return true;
}

void printIpAddress(uint32_t ip) 
{
    Serial.print((uint8_t)(ip >> 24));
    Serial.print('.');
    Serial.print((uint8_t)(ip >> 16));
    Serial.print('.');
    Serial.print((uint8_t)(ip >> 8));
    Serial.print('.');
    Serial.print((uint8_t)(ip));
}

void setup()
{
    Serial.begin(115200);
    Serial.println(F("Coap Server Test"));

    wlan_init();
    Serial.print(F("\nAttempting to connect to "));
    Serial.println(WLAN_SSID);
    if (!wifi_connect())
    {
        return;
    }
    Serial.println(F("Connected!"));

    displayConnectionDetails();
}

void loop()
{

}

Bebug Dump:

==> wlan_init <==
==> hci_begin_first_command <==
==> hci_end_command_begin_receive <==
==> hci_irq <==
==> hci_begin_receive <==
hci_payload_size: 5
==> hci_dispatch <==
rx_type: 4
rx_event_type: 4000
rx_args_size: 1
==> hci_end_receive <==
hci_payload_size: 1
==> hci_begin_command <==
==> hci_irq <==
==> hci_end_command_begin_receive <==
==> hci_irq <==
==> hci_begin_receive <==
hci_payload_size: 9
==> hci_dispatch <==
rx_type: 4
rx_event_type: 400B
rx_args_size: 4
status: 0
hci_buffer_count: 6
hci_buffer_size: 1500
==> hci_end_receive <==
hci_payload_size: 1
==> hci_begin_command <==
==> hci_irq <==
==> hci_end_command_begin_receive <==
==> hci_irq <==
==> hci_begin_receive <==
hci_payload_size: 9
==> hci_dispatch <==
rx_type: 4
rx_event_type: 8
rx_args_size: 5
==> hci_end_receive <==
hci_payload_size: 5
==> wlan_ioctl_set_connection_policy <==
should_connect_to_open_ap: 0
should_use_fast_connect: 0
use_profiles: 0
==> hci_begin_command <==
==> hci_irq <==
==> hci_end_command_begin_receive <==
==> hci_irq <==
==> hci_begin_receive <==
hci_payload_size: 9
==> hci_dispatch <==
rx_type: 4
rx_event_type: 4
rx_args_size: 5
status: 0
result: 0
==> hci_end_receive <==
hci_payload_size: 0
policy 0
==> wlan_connect <==
sec_type: 3
ssid: ATF Tactical Van
ssid_len: 16
(char*)bssid: 
(char*)key: ThisIsPassword
key_len: 14
==> hci_begin_command <==
==> hci_irq <==
==> hci_end_command_begin_receive <==
==> hci_irq <==
==> hci_begin_receive <==
hci_payload_size: 9
==> hci_dispatch <==
rx_type: 4
rx_event_type: 1
rx_args_size: 5
status: 0
result: 0
==> hci_end_receive <==
hci_payload_size: 0
connect 0
Sweet-Peas commented 9 years ago

It would have been so much simpler if you would have posted the source code as well =)

Makuna commented 9 years ago

Updated with source code. The code was basically the server test sketch included in the project modified to my Wifi settings.

The issue maybe that interrupts are being missed due to how much work is being done within the interrupt service routine. The Adafruit code does polling of the interrupt pin/state to work around it (which seems hacky). The only reason an interrupt would be missed is that the source didn't hold it long enough (?) or interrupts were disabled when it did trigger. I suspect the latter due to how much is within it.

wadetb commented 9 years ago

Thanks for your report. It's strange that it's failing as early as the DHCP, though I've never tried tinyhci with a Mega2560 before.

Regarding the interrupt possibility, the CC3K shouldn't be able to raise another interrupt until we release the CS line in hci_end_receive(), after handling the first one. That's the last thing we do in our interrupt handler. Perhaps my assumption about the SPI protocol is wrong, but I'm not sure how anything could work robustly if that weren't true.

Does it make any difference if you disable serial logging? That code adds quite a bit of time to the ISR.

Makuna commented 9 years ago

Removing the debugging doesn't help.

I glanced around and noticed some comments about must delay by micro seconds but the were delaying by milliseconds. I changed those also and that didn't help.

Does the sketch look correct? what do you run on?