Updated firmware and test tool with with simple random number generator code

This commit is contained in:
Mickey Malone
2021-02-07 11:01:42 -06:00
parent b4d5453fd4
commit 8cdffc8d4c
3 changed files with 56 additions and 9 deletions

View File

@@ -2,5 +2,7 @@ add_executable(pico_rng
pico_rng.c pico_rng.c
) )
target_link_libraries(pico_rng PRIVATE pico_stdlib hardware_resets hardware_irq) target_link_libraries(pico_rng PRIVATE pico_stdlib hardware_resets hardware_irq hardware_adc)
pico_enable_stdio_uart(pico_rng 1)
pico_add_extra_outputs(pico_rng) pico_add_extra_outputs(pico_rng)

View File

@@ -8,6 +8,8 @@
// Pico // Pico
#include "pico/stdlib.h" #include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/adc.h"
// For memcpy // For memcpy
#include <string.h> #include <string.h>
@@ -538,11 +540,42 @@ void ep0_out_handler(uint8_t *buf, uint16_t len) {
// Device specific functions // Device specific functions
void ep1_out_handler(uint8_t *buf, uint16_t len) { void ep1_out_handler(uint8_t *buf, uint16_t len) {
printf("RX %d bytes from host\n", len); uint16_t new_buf[40];
char *new_buf = "Got yo data"; uint16_t adc_result;
// Send data back to host uint8_t size;
int i;
printf("RX %d bytes from host %d \n", len, *buf);
gpio_put(25, 1);
if(len != 1)
{
//TODO handle length error
size = 64;
}
else
{
size = *buf;
if(size == 0 || size > 64)
{
//TODO handle requested size error
size = 64;
}
}
memset(new_buf, 0, 40);
for(i = 1; i < (size / 2) + 4; i++)
{
adc_result = adc_read();
memcpy(&new_buf[i-1], (void*)&adc_result, sizeof(uint16_t));
}
gpio_put(25, 0);
// Send random data back to the host
struct usb_endpoint_configuration *ep = usb_get_endpoint_configuration(EP2_IN_ADDR); struct usb_endpoint_configuration *ep = usb_get_endpoint_configuration(EP2_IN_ADDR);
usb_start_transfer(ep, new_buf, 11); usb_start_transfer(ep, (char*)new_buf, size);
} }
void ep2_in_handler(uint8_t *buf, uint16_t len) { void ep2_in_handler(uint8_t *buf, uint16_t len) {
@@ -553,6 +586,16 @@ void ep2_in_handler(uint8_t *buf, uint16_t len) {
int main(void) { int main(void) {
stdio_init_all(); stdio_init_all();
// Builtin GPIO
gpio_init(25);
gpio_set_dir(25, GPIO_OUT);
// ADC
adc_init();
adc_gpio_init(26);
adc_select_input(0);
printf("USB pico rng\n"); printf("USB pico rng\n");
usb_device_init(); usb_device_init();

View File

@@ -10,6 +10,7 @@
import usb.core import usb.core
import usb.util import usb.util
import random
# find our device # find our device
dev = usb.core.find(idVendor=0x0000, idProduct=0x0004) dev = usb.core.find(idVendor=0x0000, idProduct=0x0004)
@@ -41,8 +42,9 @@ inep = usb.util.find_descriptor(
assert inep is not None assert inep is not None
assert outep is not None assert outep is not None
test_string = "Hello World!" num_bytes = random.randint(0,65)
outep.write(test_string)
from_device = inep.read(len(test_string))
print("Device Says: {}".format(''.join([chr(x) for x in from_device]))) outep.write([num_bytes], 500)
from_device = inep.read(num_bytes, 500)
print(":".join("{:02x}".format(b) for b in from_device))