Adding SDR code

pull/1/head
Luigi Cruz 2021-03-09 23:30:57 -03:00
rodzic f7308e67d1
commit 02beb36b0c
6 zmienionych plików z 286 dodań i 196 usunięć

5
.vscode/settings.json vendored 100644
Wyświetl plik

@ -0,0 +1,5 @@
{
"files.associations": {
"sstream": "c"
}
}

Wyświetl plik

@ -14,13 +14,14 @@ target_link_libraries(tcp_server
tinyusb_board
tinyusb_net
hardware_i2c
hardware_adc
hardware_dma
hardware_irq
hardware_pwm
lwip
)
target_include_directories(tcp_server PRIVATE
.
${CMAKE_SOURCE_DIR}/lib/bmp180
)
target_include_directories(tcp_server PRIVATE .)
pico_enable_stdio_usb(tcp_server 1)
pico_enable_stdio_uart(tcp_server 0)

Wyświetl plik

@ -1,8 +1,8 @@
/*
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
@ -11,21 +11,21 @@
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
*
* Author: Simon Goldschmidt
*
*/
@ -44,6 +44,7 @@
#define LWIP_TCP 1
#define ETH_PAD_SIZE 0
#define LWIP_IP_ACCEPT_UDP_PORT(p) ((p) == PP_NTOHS(67))
#define LWIP_TCP_KEEPALIVE 1
#define TCP_MSS (1500 /*mtu*/ - 20 /*iphdr*/ - 20 /*tcphhr*/)
#define TCP_SND_BUF (2 * TCP_MSS)

Wyświetl plik

@ -1,47 +1,11 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 Peter Lawrence
*
* influenced by lrndis https://github.com/fetisov/lrndis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
/*
this appears as either a RNDIS or CDC-ECM USB virtual network adapter; the OS picks its preference
RNDIS should be valid on Linux and Windows hosts, and CDC-ECM should be valid on Linux and macOS hosts
The MCU appears to the host as IP address 192.168.7.1, and provides a DHCP server, DNS server, and web server.
*/
/*
Some smartphones *may* work with this implementation as well, but likely have limited (broken) drivers,
and likely their manufacturer has not tested such functionality. Some code workarounds could be tried:
The smartphone may only have an ECM driver, but refuse to automatically pick ECM (unlike the OSes above);
try modifying ./examples/devices/net_lwip_webserver/usb_descriptors.c so that CONFIG_ID_ECM is default.
The smartphone may be artificially picky about which Ethernet MAC address to recognize; if this happens,
try changing the first byte of tud_network_mac_address[] below from 0x02 to 0x00 (clearing bit 1).
*/
// Initial TinyUSB RNDIS written by Peter Lawrence.
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/adc.h"
#include "hardware/dma.h"
#include "hardware/irq.h"
#include "hardware/pwm.h"
#include "hardware/clocks.h"
#include "bsp/board.h"
#include "tusb.h"
@ -52,11 +16,7 @@ try changing the first byte of tud_network_mac_address[] below from 0x02 to 0x00
#include "lwip/timeouts.h"
#include "lwip/api.h"
#include "lwip/sys.h"
#include "lwip/udp.h"
#include "httpd.h"
#include "bmp180.h"
#include "lwip/tcp.h"
/* lwip context */
static struct netif netif_data;
@ -75,16 +35,14 @@ static const ip_addr_t netmask = IPADDR4_INIT_BYTES(255, 255, 255, 0);
static const ip_addr_t gateway = IPADDR4_INIT_BYTES(0, 0, 0, 0);
/* database IP addresses that can be offered to the host; this must be in RAM to store assigned MAC addresses */
static dhcp_entry_t entries[] =
{
static dhcp_entry_t entries[] = {
/* mac ip address lease time */
{ {0}, IPADDR4_INIT_BYTES(192, 168, 7, 2), 24 * 60 * 60 },
{ {0}, IPADDR4_INIT_BYTES(192, 168, 7, 3), 24 * 60 * 60 },
{ {0}, IPADDR4_INIT_BYTES(192, 168, 7, 4), 24 * 60 * 60 },
};
static const dhcp_config_t dhcp_config =
{
static const dhcp_config_t dhcp_config = {
.router = IPADDR4_INIT_BYTES(0, 0, 0, 0), /* router address (if any) */
.port = 67, /* listen port */
.dns = IPADDR4_INIT_BYTES(192, 168, 7, 1), /* dns server (if any) */
@ -92,59 +50,52 @@ static const dhcp_config_t dhcp_config =
TU_ARRAY_SIZE(entries), /* num entry */
entries /* entries */
};
static err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
{
(void)netif;
static err_t linkoutput_fn(struct netif *netif, struct pbuf *p) {
(void)netif;
for (;;)
{
/* if TinyUSB isn't ready, we must signal back to lwip that there is nothing we can do */
if (!tud_ready())
return ERR_USE;
for (;;) {
/* if TinyUSB isn't ready, we must signal back to lwip that there is nothing we can do */
if (!tud_ready()) return ERR_USE;
/* if the network driver can accept another packet, we make it happen */
if (tud_network_can_xmit())
{
tud_network_xmit(p, 0 /* unused for this example */);
return ERR_OK;
/* if the network driver can accept another packet, we make it happen */
if (tud_network_can_xmit()) {
tud_network_xmit(p, 0 /* unused for this example */);
return ERR_OK;
}
/* transfer execution to TinyUSB in the hopes that it will finish transmitting the prior packet */
tud_task();
}
/* transfer execution to TinyUSB in the hopes that it will finish transmitting the prior packet */
tud_task();
}
}
static err_t output_fn(struct netif *netif, struct pbuf *p, const ip_addr_t *addr)
{
return etharp_output(netif, p, addr);
static err_t output_fn(struct netif *netif, struct pbuf *p, const ip_addr_t *addr) {
return etharp_output(netif, p, addr);
}
static err_t netif_init_cb(struct netif *netif)
{
LWIP_ASSERT("netif != NULL", (netif != NULL));
netif->mtu = CFG_TUD_NET_MTU;
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
netif->state = NULL;
netif->name[0] = 'E';
netif->name[1] = 'X';
netif->linkoutput = linkoutput_fn;
netif->output = output_fn;
return ERR_OK;
static err_t netif_init_cb(struct netif *netif) {
LWIP_ASSERT("netif != NULL", (netif != NULL));
netif->mtu = CFG_TUD_NET_MTU;
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
netif->state = NULL;
netif->name[0] = 'E';
netif->name[1] = 'X';
netif->linkoutput = linkoutput_fn;
netif->output = output_fn;
return ERR_OK;
}
static void init_lwip(void)
{
struct netif *netif = &netif_data;
static void init_lwip(void) {
struct netif *netif = &netif_data;
lwip_init();
lwip_init();
/* the lwip virtual MAC address must be different from the host's; to ensure this, we toggle the LSbit */
netif->hwaddr_len = sizeof(tud_network_mac_address);
memcpy(netif->hwaddr, tud_network_mac_address, sizeof(tud_network_mac_address));
netif->hwaddr[5] ^= 0x01;
/* the lwip virtual MAC address must be different from the host's; to ensure this, we toggle the LSbit */
netif->hwaddr_len = sizeof(tud_network_mac_address);
memcpy(netif->hwaddr, tud_network_mac_address, sizeof(tud_network_mac_address));
netif->hwaddr[5] ^= 0x01;
netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, netif_init_cb, ip_input);
netif_set_default(netif);
netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, netif_init_cb, ip_input);
netif_set_default(netif);
}
/* handle any DNS requests from dns-server */
@ -158,110 +109,242 @@ bool dns_query_proc(const char *name, ip_addr_t *addr)
return false;
}
bool tud_network_recv_cb(const uint8_t *src, uint16_t size)
{
/* this shouldn't happen, but if we get another packet before
parsing the previous, we must signal our inability to accept it */
if (received_frame) return false;
bool tud_network_recv_cb(const uint8_t *src, uint16_t size) {
/* this shouldn't happen, but if we get another packet before
parsing the previous, we must signal our inability to accept it */
if (received_frame) return false;
if (size)
{
struct pbuf *p = pbuf_alloc(PBUF_RAW, size, PBUF_POOL);
if (size) {
struct pbuf *p = pbuf_alloc(PBUF_RAW, size, PBUF_POOL);
if (p)
{
/* pbuf_alloc() has already initialized struct; all we need to do is copy the data */
memcpy(p->payload, src, size);
if (p) {
/* pbuf_alloc() has already initialized struct; all we need to do is copy the data */
memcpy(p->payload, src, size);
/* store away the pointer for service_traffic() to later handle */
received_frame = p;
/* store away the pointer for service_traffic() to later handle */
received_frame = p;
}
}
}
return true;
return true;
}
uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg)
{
struct pbuf *p = (struct pbuf *)ref;
struct pbuf *q;
uint16_t len = 0;
uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg) {
struct pbuf *p = (struct pbuf *)ref;
struct pbuf *q;
uint16_t len = 0;
(void)arg; /* unused for this example */
(void)arg; /* unused for this example */
/* traverse the "pbuf chain"; see ./lwip/src/core/pbuf.c for more info */
for(q = p; q != NULL; q = q->next)
{
memcpy(dst, (char *)q->payload, q->len);
dst += q->len;
len += q->len;
if (q->len == q->tot_len) break;
}
/* traverse the "pbuf chain"; see ./lwip/src/core/pbuf.c for more info */
for(q = p; q != NULL; q = q->next) {
memcpy(dst, (char *)q->payload, q->len);
dst += q->len;
len += q->len;
if (q->len == q->tot_len) break;
}
return len;
return len;
}
static void service_traffic(void)
{
/* handle any packet received by tud_network_recv_cb() */
if (received_frame)
{
ethernet_input(received_frame, &netif_data);
pbuf_free(received_frame);
received_frame = NULL;
tud_network_recv_renew();
}
/* handle any packet received by tud_network_recv_cb() */
if (received_frame) {
ethernet_input(received_frame, &netif_data);
pbuf_free(received_frame);
received_frame = NULL;
tud_network_recv_renew();
}
sys_check_timeouts();
sys_check_timeouts();
}
void tud_network_init_cb(void)
{
/* if the network is re-initializing and we have a leftover packet, we must do a cleanup */
if (received_frame)
{
pbuf_free(received_frame);
received_frame = NULL;
}
void tud_network_init_cb(void) {
/* if the network is re-initializing and we have a leftover packet, we must do a cleanup */
if (received_frame) {
pbuf_free(received_frame);
received_frame = NULL;
}
}
bmp_t bmp;
bool running = false;
bool connected = false;
struct tcp_pcb* client;
static void udpecho_raw_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16_t port)
{
if(p == NULL)
return;
//#define DEBUG
#define CAPTURE_CHANNEL 0
#define CAPTURE_DEPTH 500
*(char*)(p->payload + 1) = '0';
uint dma_chan_a, dma_chan_b;
uint8_t capture_buf_a[CAPTURE_DEPTH];
uint8_t capture_buf_b[CAPTURE_DEPTH];
udp_sendto(pcb, p, addr, port);
pbuf_free(p);
void dma_handler(uint8_t* buffer, int id) {
#ifdef DEBUG
char str[64];
int len = sprintf(str, "DMA IRQ %d [%d %d ... %d]\n", id, buffer[0], buffer[1], buffer[CAPTURE_DEPTH-1]);
if (connected) {
tcp_write(client, str, len, 0);
tcp_output(client);
}
#else
if (connected) {
tcp_write(client, buffer, CAPTURE_DEPTH, 0x01);
tcp_output(client);
}
#endif
}
int main(void)
{
/* initialize TinyUSB */
board_init();
tusb_init();
/* initialize lwip, dhcp-server, dns-server, and http */
init_lwip();
while (!netif_is_up(&netif_data));
while (dhserv_init(&dhcp_config) != ERR_OK);
while (dnserv_init(&ipaddr, 53, dns_query_proc) != ERR_OK);
struct udp_pcb *pcb;
pcb = udp_new();
udp_bind(pcb, IP_ADDR_ANY, 7777);
udp_recv(pcb , udpecho_raw_recv, pcb);
httpd_init();
while (1)
{
tud_task();
service_traffic();
}
return 0;
void dma_handler_a() {
dma_handler((uint8_t*)&capture_buf_a, 0);
dma_hw->ints0 = 1u << dma_chan_a;
dma_channel_set_write_addr(dma_chan_a, &capture_buf_a, false);
}
void dma_handler_b() {
dma_handler((uint8_t*)&capture_buf_b, 1);
dma_hw->ints1 = 1u << dma_chan_b;
dma_channel_set_write_addr(dma_chan_b, &capture_buf_b, false);
}
static void start_stream() {
adc_gpio_init(26 + CAPTURE_CHANNEL);
adc_init();
adc_select_input(CAPTURE_CHANNEL);
adc_fifo_setup(
true, // Write to FIFO
true, // Enable DREQ
1, // Trigger DREQ with at least one sample
false, // No ERR bit
true // Shift each sample by 8 bits
);
adc_set_clkdiv(0);
dma_channel_config dma_cfg_a, dma_cfg_b;
dma_chan_a = dma_claim_unused_channel(true);
dma_chan_b = dma_claim_unused_channel(true);
dma_cfg_a = dma_channel_get_default_config(dma_chan_a);
dma_cfg_b = dma_channel_get_default_config(dma_chan_b);
channel_config_set_transfer_data_size(&dma_cfg_a, DMA_SIZE_8);
channel_config_set_transfer_data_size(&dma_cfg_b, DMA_SIZE_8);
channel_config_set_read_increment(&dma_cfg_a, false);
channel_config_set_read_increment(&dma_cfg_b, false);
channel_config_set_write_increment(&dma_cfg_a, true);
channel_config_set_write_increment(&dma_cfg_b, true);
channel_config_set_dreq(&dma_cfg_a, DREQ_ADC);
channel_config_set_dreq(&dma_cfg_b, DREQ_ADC);
channel_config_set_chain_to(&dma_cfg_a, dma_chan_b);
channel_config_set_chain_to(&dma_cfg_b, dma_chan_a);
dma_channel_configure(dma_chan_a, &dma_cfg_a,
capture_buf_a, // dst
&adc_hw->fifo, // src
CAPTURE_DEPTH, // transfer count
true // start now
);
dma_channel_configure(dma_chan_b, &dma_cfg_b,
capture_buf_b, // dst
&adc_hw->fifo, // src
CAPTURE_DEPTH, // transfer count
false // start now
);
dma_channel_set_irq0_enabled(dma_chan_a, true);
irq_set_exclusive_handler(DMA_IRQ_0, dma_handler_a);
irq_set_enabled(DMA_IRQ_0, true);
dma_channel_set_irq1_enabled(dma_chan_b, true);
irq_set_exclusive_handler(DMA_IRQ_1, dma_handler_b);
irq_set_enabled(DMA_IRQ_1, true);
adc_run(true);
running = true;
}
static void close_conn(struct tcp_pcb *pcb){
tcp_arg(pcb, NULL);
tcp_sent(pcb, NULL);
tcp_recv(pcb, NULL);
tcp_close(pcb);
running = false;
connected = false;
}
static err_t srv_receive(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) {
if (err != ERR_OK && p != NULL)
goto exception;
tcp_recved(pcb, p->tot_len);
tcp_sent(pcb, NULL);
client = pcb;
connected = true;
exception:
pbuf_free(p);
return err;
}
static err_t srv_accept(void * arg, struct tcp_pcb * pcb, err_t err) {
tcp_setprio(pcb, TCP_PRIO_MIN);
tcp_recv(pcb, srv_receive);
tcp_err(pcb, NULL);
tcp_poll(pcb, NULL, 4);
return ERR_OK;
}
int main(void) {
// Initialize everything.
board_init();
tusb_init();
init_lwip();
// Startup lwIP stack.
while (!netif_is_up(&netif_data));
while (dhserv_init(&dhcp_config) != ERR_OK);
while (dnserv_init(&ipaddr, 53, dns_query_proc) != ERR_OK);
// Start TCP server.
struct tcp_pcb* pcb = tcp_new();
pcb->so_options |= SOF_KEEPALIVE;
pcb->keep_intvl = 75000000;
tcp_bind(pcb, IP_ADDR_ANY, 7777);
struct tcp_pcb* listen = tcp_listen(pcb);
tcp_accept(listen, srv_accept);
// Start a test signal.
gpio_set_function(0, GPIO_FUNC_PWM);
uint slice_num = pwm_gpio_to_slice_num(0);
pwm_set_wrap(slice_num, 1);
pwm_set_chan_level(slice_num, PWM_CHAN_A, 1);
uint32_t f_sys = clock_get_hz(clk_sys); // typically 125'000'000 Hz
const int top = 4095;
const int f_pwm = 80;
float scale = (top+1) * f_pwm;
float divider = f_sys / scale;
pwm_set_clkdiv(slice_num, divider);
pwm_set_enabled(slice_num, true);
// Listen to events.
while (1) {
if (connected && !running) start_stream();
tud_task();
service_traffic();
}
return 0;
}

@ -1 +1 @@
Subproject commit 1548682049251d65a1d42f9751a47dcb5c5adf13
Subproject commit 89b8071d2a99e1af25b01c6cb0380ae0216053ca

@ -1 +1 @@
Subproject commit 2d5789eca89658a7f0a01e2d6010c0f254605d72
Subproject commit fc10a97c386f65c1a44c68684fe52a56aaf50df0