test

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Wed Jun 21 17:46:44 2017 +0100
Revision:
168:e84263d55307
Child:
175:b96e65c34a4d
This updates the lib to the mbed lib v 145

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 168:e84263d55307 1 /* mbed Microcontroller Library
AnnaBridge 168:e84263d55307 2 * Copyright (c) 2016 Realtek Semiconductor Corp.
AnnaBridge 168:e84263d55307 3 *
AnnaBridge 168:e84263d55307 4 * Licensed under the Apache License, Version 2.0 (the "License");
AnnaBridge 168:e84263d55307 5 * you may not use this file except in compliance with the License.
AnnaBridge 168:e84263d55307 6 * You may obtain a copy of the License at
AnnaBridge 168:e84263d55307 7 *
AnnaBridge 168:e84263d55307 8 * http://www.apache.org/licenses/LICENSE-2.0
AnnaBridge 168:e84263d55307 9 *
AnnaBridge 168:e84263d55307 10 * Unless required by applicable law or agreed to in writing, software
AnnaBridge 168:e84263d55307 11 * distributed under the License is distributed on an "AS IS" BASIS,
AnnaBridge 168:e84263d55307 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AnnaBridge 168:e84263d55307 13 * See the License for the specific language governing permissions and
AnnaBridge 168:e84263d55307 14 * limitations under the License.
AnnaBridge 168:e84263d55307 15 */
AnnaBridge 168:e84263d55307 16
AnnaBridge 168:e84263d55307 17 #if DEVICE_EMAC
AnnaBridge 168:e84263d55307 18
AnnaBridge 168:e84263d55307 19 #include <stdio.h>
AnnaBridge 168:e84263d55307 20 #include "mbed_assert.h"
AnnaBridge 168:e84263d55307 21 #include "mbed_events.h"
AnnaBridge 168:e84263d55307 22
AnnaBridge 168:e84263d55307 23 #include "emac_api.h"
AnnaBridge 168:e84263d55307 24 #include "rtos.h"
AnnaBridge 168:e84263d55307 25
AnnaBridge 168:e84263d55307 26 #include "lwip/pbuf.h"
AnnaBridge 168:e84263d55307 27 #include "netif/etharp.h"
AnnaBridge 168:e84263d55307 28
AnnaBridge 168:e84263d55307 29 #include "lwip_intf.h"
AnnaBridge 168:e84263d55307 30 #include "wifi_constants.h"
AnnaBridge 168:e84263d55307 31 #include "wifi_conf.h"
AnnaBridge 168:e84263d55307 32
AnnaBridge 168:e84263d55307 33 #define RTW_EMAC_MTU_SIZE (1500U)
AnnaBridge 168:e84263d55307 34
AnnaBridge 168:e84263d55307 35 static emac_interface_t *_emac;
AnnaBridge 168:e84263d55307 36 static emac_link_input_fn link_input_cb;
AnnaBridge 168:e84263d55307 37 static emac_link_state_change_fn link_state_cb;
AnnaBridge 168:e84263d55307 38 static void *link_input_data;
AnnaBridge 168:e84263d55307 39 static void *link_state_data;
AnnaBridge 168:e84263d55307 40
AnnaBridge 168:e84263d55307 41 static uint32_t wlan_get_mtu_size(emac_interface_t *emac)
AnnaBridge 168:e84263d55307 42 {
AnnaBridge 168:e84263d55307 43 return RTW_EMAC_MTU_SIZE;
AnnaBridge 168:e84263d55307 44 }
AnnaBridge 168:e84263d55307 45
AnnaBridge 168:e84263d55307 46 static void wlan_get_ifname(emac_interface_t *emac, char *name, uint8_t size)
AnnaBridge 168:e84263d55307 47 {
AnnaBridge 168:e84263d55307 48 MBED_ASSERT(name != NULL);
AnnaBridge 168:e84263d55307 49 strncpy(name, "r0", size);
AnnaBridge 168:e84263d55307 50 }
AnnaBridge 168:e84263d55307 51
AnnaBridge 168:e84263d55307 52 static uint8_t wlan_get_hwaddr_size(emac_interface_t *emac)
AnnaBridge 168:e84263d55307 53 {
AnnaBridge 168:e84263d55307 54 return ETHARP_HWADDR_LEN;
AnnaBridge 168:e84263d55307 55 }
AnnaBridge 168:e84263d55307 56
AnnaBridge 168:e84263d55307 57 static void wlan_get_hwaddr(emac_interface_t *emac, uint8_t *addr)
AnnaBridge 168:e84263d55307 58 {
AnnaBridge 168:e84263d55307 59 char mac[20];
AnnaBridge 168:e84263d55307 60 if(RTW_SUCCESS == wifi_get_mac_address(mac))
AnnaBridge 168:e84263d55307 61 {
AnnaBridge 168:e84263d55307 62 if (sscanf(mac, "%x:%x:%x:%x:%x:%x", &addr[0], &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6)
AnnaBridge 168:e84263d55307 63 printf("Get HW address failed\r\n");
AnnaBridge 168:e84263d55307 64 }else{
AnnaBridge 168:e84263d55307 65 printf("Get HW address failed\r\n");
AnnaBridge 168:e84263d55307 66 }
AnnaBridge 168:e84263d55307 67 }
AnnaBridge 168:e84263d55307 68
AnnaBridge 168:e84263d55307 69 static void wlan_set_hwaddr(emac_interface_t *emac, uint8_t *addr)
AnnaBridge 168:e84263d55307 70 {
AnnaBridge 168:e84263d55307 71
AnnaBridge 168:e84263d55307 72 }
AnnaBridge 168:e84263d55307 73
AnnaBridge 168:e84263d55307 74
AnnaBridge 168:e84263d55307 75 static bool wlan_link_out(emac_interface_t *emac, emac_stack_mem_t *buf)
AnnaBridge 168:e84263d55307 76 {
AnnaBridge 168:e84263d55307 77 struct eth_drv_sg * sg_list=0;
AnnaBridge 168:e84263d55307 78 int sg_len = 0;
AnnaBridge 168:e84263d55307 79 int tot_len;
AnnaBridge 168:e84263d55307 80 struct pbuf *p;
AnnaBridge 168:e84263d55307 81 bool ret = true;
AnnaBridge 168:e84263d55307 82
AnnaBridge 168:e84263d55307 83 if (!rltk_wlan_running(0)) {
AnnaBridge 168:e84263d55307 84 return false;
AnnaBridge 168:e84263d55307 85 }
AnnaBridge 168:e84263d55307 86
AnnaBridge 168:e84263d55307 87 sg_list = (struct eth_drv_sg *)malloc(sizeof(struct eth_drv_sg)*MAX_ETH_DRV_SG);
AnnaBridge 168:e84263d55307 88 if(sg_list == 0){//malloc fail
AnnaBridge 168:e84263d55307 89 return false;
AnnaBridge 168:e84263d55307 90 }
AnnaBridge 168:e84263d55307 91 emac_stack_mem_ref(emac, buf);
AnnaBridge 168:e84263d55307 92
AnnaBridge 168:e84263d55307 93 p = (struct pbuf *)buf;
AnnaBridge 168:e84263d55307 94 tot_len = p->tot_len;
AnnaBridge 168:e84263d55307 95 for (; p != NULL && sg_len < MAX_ETH_DRV_SG; p = p->next) {
AnnaBridge 168:e84263d55307 96 sg_list[sg_len].buf = (uint32_t) p->payload;
AnnaBridge 168:e84263d55307 97 sg_list[sg_len].len = p->len;
AnnaBridge 168:e84263d55307 98 sg_len++;
AnnaBridge 168:e84263d55307 99 }
AnnaBridge 168:e84263d55307 100
AnnaBridge 168:e84263d55307 101 if (sg_len) {
AnnaBridge 168:e84263d55307 102 if (rltk_wlan_send(0, sg_list, sg_len, tot_len) != 0) {
AnnaBridge 168:e84263d55307 103 ret = false;
AnnaBridge 168:e84263d55307 104 }
AnnaBridge 168:e84263d55307 105 }
AnnaBridge 168:e84263d55307 106
AnnaBridge 168:e84263d55307 107 emac_stack_mem_free(emac, buf);
AnnaBridge 168:e84263d55307 108 free(sg_list);
AnnaBridge 168:e84263d55307 109 return ret;
AnnaBridge 168:e84263d55307 110 }
AnnaBridge 168:e84263d55307 111
AnnaBridge 168:e84263d55307 112 static bool wlan_power_up(emac_interface_t *emac)
AnnaBridge 168:e84263d55307 113 {
AnnaBridge 168:e84263d55307 114 wifi_on(RTW_MODE_STA);
AnnaBridge 168:e84263d55307 115 wait_ms(1000);
AnnaBridge 168:e84263d55307 116 return true;
AnnaBridge 168:e84263d55307 117 }
AnnaBridge 168:e84263d55307 118
AnnaBridge 168:e84263d55307 119 static void wlan_power_down(emac_interface_t *emac)
AnnaBridge 168:e84263d55307 120 {
AnnaBridge 168:e84263d55307 121 wifi_off();
AnnaBridge 168:e84263d55307 122 }
AnnaBridge 168:e84263d55307 123
AnnaBridge 168:e84263d55307 124 static void wlan_set_link_input_cb(emac_interface_t *emac, emac_link_input_fn cb, void *data)
AnnaBridge 168:e84263d55307 125 {
AnnaBridge 168:e84263d55307 126 link_input_cb = cb;
AnnaBridge 168:e84263d55307 127 link_input_data = data;
AnnaBridge 168:e84263d55307 128 }
AnnaBridge 168:e84263d55307 129
AnnaBridge 168:e84263d55307 130 static void wlan_set_link_state_cb(emac_interface_t *emac, emac_link_state_change_fn cb, void *data)
AnnaBridge 168:e84263d55307 131 {
AnnaBridge 168:e84263d55307 132 link_state_cb = cb;
AnnaBridge 168:e84263d55307 133 link_state_data = data;
AnnaBridge 168:e84263d55307 134 }
AnnaBridge 168:e84263d55307 135
AnnaBridge 168:e84263d55307 136 void wlan_emac_recv(struct netif *netif, int len)
AnnaBridge 168:e84263d55307 137 {
AnnaBridge 168:e84263d55307 138 struct eth_drv_sg sg_list[MAX_ETH_DRV_SG];
AnnaBridge 168:e84263d55307 139 emac_stack_mem_t *buf;
AnnaBridge 168:e84263d55307 140 struct pbuf *p;
AnnaBridge 168:e84263d55307 141 int sg_len = 0;
AnnaBridge 168:e84263d55307 142
AnnaBridge 168:e84263d55307 143 if (!rltk_wlan_running(0)) {
AnnaBridge 168:e84263d55307 144 return;
AnnaBridge 168:e84263d55307 145 }
AnnaBridge 168:e84263d55307 146
AnnaBridge 168:e84263d55307 147 if (len > MAX_ETH_MSG || len < 0) {
AnnaBridge 168:e84263d55307 148 len = MAX_ETH_MSG;
AnnaBridge 168:e84263d55307 149 }
AnnaBridge 168:e84263d55307 150
AnnaBridge 168:e84263d55307 151 buf = emac_stack_mem_alloc(NULL, len, 0);
AnnaBridge 168:e84263d55307 152 if (buf == NULL) {
AnnaBridge 168:e84263d55307 153 return;
AnnaBridge 168:e84263d55307 154 }
AnnaBridge 168:e84263d55307 155
AnnaBridge 168:e84263d55307 156 p = (struct pbuf *)buf;
AnnaBridge 168:e84263d55307 157 for (; p != NULL && sg_len < MAX_ETH_DRV_SG; p = p->next) {
AnnaBridge 168:e84263d55307 158 sg_list[sg_len].buf = (uint32_t) p->payload;
AnnaBridge 168:e84263d55307 159 sg_list[sg_len].len = p->len;
AnnaBridge 168:e84263d55307 160 sg_len++;
AnnaBridge 168:e84263d55307 161 }
AnnaBridge 168:e84263d55307 162 rltk_wlan_recv(0, sg_list, sg_len);
AnnaBridge 168:e84263d55307 163
AnnaBridge 168:e84263d55307 164 if (link_input_cb) {
AnnaBridge 168:e84263d55307 165 link_input_cb(link_input_data, buf);
AnnaBridge 168:e84263d55307 166 }
AnnaBridge 168:e84263d55307 167 return;
AnnaBridge 168:e84263d55307 168 }
AnnaBridge 168:e84263d55307 169
AnnaBridge 168:e84263d55307 170 const emac_interface_ops_t wlan_emac_interface = {
AnnaBridge 168:e84263d55307 171 .get_mtu_size = wlan_get_mtu_size,
AnnaBridge 168:e84263d55307 172 .get_ifname = wlan_get_ifname,
AnnaBridge 168:e84263d55307 173 .get_hwaddr_size = wlan_get_hwaddr_size,
AnnaBridge 168:e84263d55307 174 .get_hwaddr = wlan_get_hwaddr,
AnnaBridge 168:e84263d55307 175 .set_hwaddr = wlan_set_hwaddr,
AnnaBridge 168:e84263d55307 176 .link_out = wlan_link_out,
AnnaBridge 168:e84263d55307 177 .power_up = wlan_power_up,
AnnaBridge 168:e84263d55307 178 .power_down = wlan_power_down,
AnnaBridge 168:e84263d55307 179 .set_link_input_cb = wlan_set_link_input_cb,
AnnaBridge 168:e84263d55307 180 .set_link_state_cb = wlan_set_link_state_cb
AnnaBridge 168:e84263d55307 181 };
AnnaBridge 168:e84263d55307 182
AnnaBridge 168:e84263d55307 183 void mbed_default_mac_address(char *mac) {
AnnaBridge 168:e84263d55307 184 unsigned char RTK_mac_addr[3] = {0x00, 0xE0, 0x4C}; // default Realtek mac address
AnnaBridge 168:e84263d55307 185
AnnaBridge 168:e84263d55307 186 mac[0] = RTK_mac_addr[0];
AnnaBridge 168:e84263d55307 187 mac[1] = RTK_mac_addr[1];
AnnaBridge 168:e84263d55307 188 mac[2] = RTK_mac_addr[2];
AnnaBridge 168:e84263d55307 189 mac[3] = 0x87;
AnnaBridge 168:e84263d55307 190 mac[4] = 0x00;
AnnaBridge 168:e84263d55307 191 mac[5] = 0x01;
AnnaBridge 168:e84263d55307 192
AnnaBridge 168:e84263d55307 193 return;
AnnaBridge 168:e84263d55307 194 }
AnnaBridge 168:e84263d55307 195
AnnaBridge 168:e84263d55307 196 void mbed_mac_address(char *mac)
AnnaBridge 168:e84263d55307 197 {
AnnaBridge 168:e84263d55307 198 char hwaddr[20];
AnnaBridge 168:e84263d55307 199 if(RTW_SUCCESS == wifi_get_mac_address(hwaddr))
AnnaBridge 168:e84263d55307 200 {
AnnaBridge 168:e84263d55307 201 if (sscanf(hwaddr, "%x:%x:%x:%x:%x:%x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) != 6)
AnnaBridge 168:e84263d55307 202 printf("Get HW address failed\r\n");
AnnaBridge 168:e84263d55307 203 }else{
AnnaBridge 168:e84263d55307 204 printf("Get HW address failed\r\n");
AnnaBridge 168:e84263d55307 205 mbed_default_mac_address(mac);
AnnaBridge 168:e84263d55307 206 }
AnnaBridge 168:e84263d55307 207 }
AnnaBridge 168:e84263d55307 208
AnnaBridge 168:e84263d55307 209 void wlan_emac_link_change(bool up)
AnnaBridge 168:e84263d55307 210 {
AnnaBridge 168:e84263d55307 211 if (link_state_cb) {
AnnaBridge 168:e84263d55307 212 link_state_cb(link_state_data, up);
AnnaBridge 168:e84263d55307 213 }
AnnaBridge 168:e84263d55307 214 }
AnnaBridge 168:e84263d55307 215
AnnaBridge 168:e84263d55307 216 emac_interface_t *wlan_emac_init_interface()
AnnaBridge 168:e84263d55307 217 {
AnnaBridge 168:e84263d55307 218
AnnaBridge 168:e84263d55307 219 if (_emac == NULL) {
AnnaBridge 168:e84263d55307 220 _emac = new emac_interface_t();
AnnaBridge 168:e84263d55307 221 if (_emac == NULL) {//new emac_interface_t fail
AnnaBridge 168:e84263d55307 222 printf("emac initialization failed\r\n");
AnnaBridge 168:e84263d55307 223 return NULL;
AnnaBridge 168:e84263d55307 224 }
AnnaBridge 168:e84263d55307 225 _emac->hw = NULL;
AnnaBridge 168:e84263d55307 226 memcpy((void*)&_emac->ops, &wlan_emac_interface, sizeof(wlan_emac_interface));
AnnaBridge 168:e84263d55307 227 }
AnnaBridge 168:e84263d55307 228 return _emac;
AnnaBridge 168:e84263d55307 229 }
AnnaBridge 168:e84263d55307 230 #endif