|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2016 Realtek Semiconductor Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#if DEVICE_EMAC |
| 18 | + |
| 19 | +#include <stdio.h> |
| 20 | +#include "mbed_assert.h" |
| 21 | +#include "mbed_events.h" |
| 22 | + |
| 23 | +#include "CC3220SF_emac.h" |
| 24 | +#include "EMACMemoryManager.h" |
| 25 | + |
| 26 | +#include "rtos.h" |
| 27 | +#include "lwip/pbuf.h" |
| 28 | +#include "netif/etharp.h" |
| 29 | + |
| 30 | +//#include "lwip_intf.h" |
| 31 | +#include "ti/drivers/net/wifi/netcfg.h" |
| 32 | + |
| 33 | +#define CC3220_EMAC_MTU_SIZE (1500U) |
| 34 | + |
| 35 | +CC3220_EMAC::CC3220_EMAC() |
| 36 | +{ |
| 37 | + //set_callback_func((emac_callback)(&CC3220_EMAC::wlan_emac_recv), this); |
| 38 | +} |
| 39 | + |
| 40 | +uint32_t CC3220_EMAC::get_mtu_size() const |
| 41 | +{ |
| 42 | + return CC3220_EMAC_MTU_SIZE; |
| 43 | +} |
| 44 | + |
| 45 | +uint32_t CC3220_EMAC::get_align_preference() const |
| 46 | +{ |
| 47 | + return true; |
| 48 | +} |
| 49 | + |
| 50 | +void CC3220_EMAC::get_ifname(char *name, uint8_t size) const |
| 51 | +{ |
| 52 | + MBED_ASSERT(name != NULL); |
| 53 | + strncpy(name, "r0", size); |
| 54 | +} |
| 55 | + |
| 56 | +uint8_t CC3220_EMAC::get_hwaddr_size() const |
| 57 | +{ |
| 58 | + return ETH_HWADDR_LEN; |
| 59 | +} |
| 60 | + |
| 61 | +bool CC3220_EMAC::get_hwaddr(uint8_t *addr) const |
| 62 | +{ |
| 63 | + _u8 macAddressVal[SL_MAC_ADDR_LEN]; |
| 64 | + _u16 macAddressLen = SL_MAC_ADDR_LEN; |
| 65 | + _u16 ConfigOpt = 0; |
| 66 | + |
| 67 | + if (0 == sl_NetCfgGet(SL_NETCFG_MAC_ADDRESS_GET,&ConfigOpt,&macAddressLen,(_u8 *)macAddressVal)) |
| 68 | + { |
| 69 | + for (int i = 0; i < SL_MAC_ADDR_LEN; i++) { |
| 70 | + addr[i] = (unsigned char) macAddressVal[i]; |
| 71 | + } |
| 72 | + return true; |
| 73 | + } else { |
| 74 | + printf("Get HW address failed\r\n"); |
| 75 | + return false; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +void CC3220_EMAC::set_hwaddr(const uint8_t *addr) |
| 80 | +{ |
| 81 | + sl_NetCfgSet(SL_NETCFG_MAC_ADDRESS_SET,1,SL_MAC_ADDR_LEN,(_u8 *)addr); |
| 82 | + sl_Stop(0); |
| 83 | + sl_Start(NULL,NULL,NULL); |
| 84 | +} |
| 85 | + |
| 86 | +bool CC3220_EMAC::link_out(emac_mem_buf_t *buf) |
| 87 | +{ |
| 88 | +#if 0 |
| 89 | + struct eth_drv_sg *sg_list; |
| 90 | + int sg_len = 0; |
| 91 | + int tot_len; |
| 92 | + emac_mem_buf_t *p; |
| 93 | + bool ret = true; |
| 94 | + if (!rltk_wlan_running(0)) { |
| 95 | + memory_manager->free(buf); |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + sg_list = (struct eth_drv_sg *)malloc(sizeof(struct eth_drv_sg)*MAX_ETH_DRV_SG); |
| 100 | + if (sg_list == 0) { |
| 101 | + memory_manager->free(buf); |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + p = buf; |
| 106 | + tot_len = memory_manager->get_total_len(p); |
| 107 | + for (; p != NULL && sg_len < MAX_ETH_DRV_SG; p = memory_manager->get_next(p)) { |
| 108 | + sg_list[sg_len].buf = (unsigned int)(static_cast<uint8_t *>(memory_manager->get_ptr(p))); |
| 109 | + sg_list[sg_len].len = memory_manager->get_len(p); |
| 110 | + sg_len++; |
| 111 | + } |
| 112 | + if (sg_len) { |
| 113 | + if (rltk_wlan_send(0, sg_list, sg_len, tot_len) != 0) { |
| 114 | + ret = false; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + memory_manager->free(buf); |
| 119 | + free(sg_list); |
| 120 | + return ret; |
| 121 | +#endif |
| 122 | + return true; |
| 123 | +} |
| 124 | + |
| 125 | +bool CC3220_EMAC::power_up() |
| 126 | +{ |
| 127 | + sl_Start(NULL,NULL,NULL); |
| 128 | + return true; |
| 129 | +} |
| 130 | + |
| 131 | +void CC3220_EMAC::power_down() |
| 132 | +{ |
| 133 | + sl_Stop(0); |
| 134 | +} |
| 135 | + |
| 136 | +void CC3220_EMAC::set_link_input_cb(emac_link_input_cb_t input_cb) |
| 137 | +{ |
| 138 | + emac_link_input_cb = input_cb; |
| 139 | +} |
| 140 | + |
| 141 | +void CC3220_EMAC::set_link_state_cb(emac_link_state_change_cb_t state_cb) |
| 142 | +{ |
| 143 | + emac_link_state_cb = state_cb; |
| 144 | +} |
| 145 | + |
| 146 | +void CC3220_EMAC::add_multicast_group(const uint8_t *addr) |
| 147 | +{ |
| 148 | +} |
| 149 | + |
| 150 | +void CC3220_EMAC::remove_multicast_group(const uint8_t *addr) |
| 151 | +{ |
| 152 | +} |
| 153 | + |
| 154 | +void CC3220_EMAC::set_all_multicast(bool all) |
| 155 | +{ |
| 156 | +} |
| 157 | + |
| 158 | +void CC3220_EMAC::set_memory_manager(EMACMemoryManager &mem_mngr) |
| 159 | +{ |
| 160 | + memory_manager = &mem_mngr; |
| 161 | +} |
| 162 | + |
| 163 | +void CC3220_EMAC::wlan_emac_recv(void *param, struct netif *netif, uint32_t len) |
| 164 | +{ |
| 165 | +#if 0 |
| 166 | + struct eth_drv_sg sg_list[MAX_ETH_DRV_SG] = {0}; |
| 167 | + emac_mem_buf_t *buf; |
| 168 | + CC3220_EMAC *enet = static_cast<CC3220_EMAC *>(param); |
| 169 | + emac_mem_buf_t *p; |
| 170 | + int sg_len = 0; |
| 171 | + if (!rltk_wlan_running(0)) { |
| 172 | + return; |
| 173 | + } |
| 174 | + |
| 175 | + if (len > MAX_ETH_MSG || len < 0) { |
| 176 | + len = MAX_ETH_MSG; |
| 177 | + } |
| 178 | + |
| 179 | + buf = enet->memory_manager->alloc_heap(len, 0); |
| 180 | + if (buf == NULL) { |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + enet->memory_manager->set_len(buf, len); |
| 185 | + p = buf; |
| 186 | + for (; p != NULL && sg_len < MAX_ETH_DRV_SG; p = enet->memory_manager->get_next(p)) { |
| 187 | + sg_list[sg_len].buf = (unsigned int)(static_cast<uint8_t *>(enet->memory_manager->get_ptr(p))); |
| 188 | + sg_list[sg_len].len = enet->memory_manager->get_len(p); |
| 189 | + sg_len++; |
| 190 | + } |
| 191 | + |
| 192 | + rltk_wlan_recv(0, sg_list, sg_len); |
| 193 | + if (enet->emac_link_input_cb) { |
| 194 | + enet->emac_link_input_cb(buf); |
| 195 | + } |
| 196 | +#endif |
| 197 | +} |
| 198 | + |
| 199 | +void mbed_default_mac_address(char *mac) { |
| 200 | + unsigned char RTK_mac_addr[3] = {0x00, 0xE0, 0x4C}; // default Realtek mac address |
| 201 | + mac[0] = RTK_mac_addr[0]; |
| 202 | + mac[1] = RTK_mac_addr[1]; |
| 203 | + mac[2] = RTK_mac_addr[2]; |
| 204 | + mac[3] = 0x87; |
| 205 | + mac[4] = 0x00; |
| 206 | + mac[5] = 0x01; |
| 207 | + return; |
| 208 | +} |
| 209 | + |
| 210 | +void mbed_mac_address(char *mac) |
| 211 | +{ |
| 212 | + int i; |
| 213 | + _u8 macAddressVal[SL_MAC_ADDR_LEN]; |
| 214 | + _u16 macAddressLen = SL_MAC_ADDR_LEN; |
| 215 | + _u16 ConfigOpt = 0; |
| 216 | + if (0 == sl_NetCfgGet(SL_NETCFG_MAC_ADDRESS_GET,&ConfigOpt,&macAddressLen,(_u8 *)macAddressVal)) |
| 217 | + { |
| 218 | + for (i = 0; i < SL_MAC_ADDR_LEN; i++) { |
| 219 | + mac[i] = (unsigned char) macAddressVal[i]; |
| 220 | + } |
| 221 | + } else { |
| 222 | + printf("Get HW address failed\r\n"); |
| 223 | + mbed_default_mac_address(mac); |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | +void CC3220_EMAC::wlan_emac_link_change(bool up) |
| 228 | +{ |
| 229 | + if (emac_link_state_cb) { |
| 230 | + emac_link_state_cb(up); |
| 231 | + } |
| 232 | +} |
| 233 | + |
| 234 | +CC3220_EMAC &CC3220_EMAC::get_instance() { |
| 235 | + static CC3220_EMAC cc3220_emac; |
| 236 | + return cc3220_emac; |
| 237 | +} |
| 238 | +// Weak so a module can override |
| 239 | +MBED_WEAK EMAC &EMAC::get_default_instance() { |
| 240 | + return CC3220_EMAC::get_instance(); |
| 241 | +} |
| 242 | +#endif |
0 commit comments