OK. I got it working. The real problem here was the WirelessMode It was set to a default value of 9 (which is mixed g/b/n). My wireless network is n-only. So it was never get attached to the Access Point. Setting it to 6 (n only) got it running.
$this->bbcode_second_pass_code('', '
ifconfig ra0 up
sleep 3
#iwpriv ra0 set WirelessMode=Managed
iwpriv ra0 set AuthMode=WPA2PSK
iwpriv ra0 set EncrypType=AES
iwpriv ra0 set WPAPSK=xxx
iwpriv ra0 set SSID=xxx
iwpriv ra0 set WirelessMode=6
sleep 1
dhcpcd ra0
')
Observation:
1. The driver dat file in /etc/Wireless/RT3390STA/ is relevant. If you change the default values in that file, they will be used the next time ra0 is started.
2. Using DHCP, after the iwpriv commands were executed, iwconfig will not show the real parameters. Only after dhcpcd has started and the wireless interface is connected, then iwconfig will show all parameters.
Hope this help others looking for a solution.