Skip to content

Commit

Permalink
usb host working with crow
Browse files Browse the repository at this point in the history
  • Loading branch information
trentgill committed Jun 9, 2024
1 parent df98015 commit c06299a
Show file tree
Hide file tree
Showing 13 changed files with 661 additions and 9 deletions.
16 changes: 15 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ GIT_VERSION := $(shell git describe --tags)
CUBE=submodules/STM32CubeF7_Drivers
HALS=$(CUBE)/STM32F7xx_HAL_Driver/Src
USBD=submodules/STM32CubeF7_USB/STM32_USB_Device_Library
USBH=submodules/STM32CubeF7_USB/STM32_USB_Host_Library
WRLIB=submodules/wrLib
WRDSP=submodules/wrDsp
LUAS=submodules/lua/src
Expand Down Expand Up @@ -43,6 +44,9 @@ STM32_INCLUDES = \
-Iusbd/ \
-I$(USBD)/Class/CDC/Inc/ \
-I$(USBD)/Core/Inc/ \
-Iusbh/ \
-I$(USBH)/Class/CDC/Inc/ \
-I$(USBH)/Core/Inc/ \

OPTIMIZE = -O2

Expand Down Expand Up @@ -72,7 +76,9 @@ ifeq ($(R), 1)
#CFLAGS += -flto # broken in debug mode. provides a small LTO binary size reduction
endif

LDFLAGS = -Wl,-T,stm32_flash.ld,-flto,-gc-sections
# LDFLAGS = -Wl,-T,stm32_flash.ld,-flto,-gc-sections

LDFLAGS = -Wl,-T,stm32_flash.ld,-gc-sections
LIBS = -lm -lc -lnosys

SRC = main.c \
Expand All @@ -91,6 +97,7 @@ SRC = main.c \
$(HALS)/stm32f7xx_hal_dma2d.c \
$(HALS)/stm32f7xx_hal_pcd.c \
$(HALS)/stm32f7xx_hal_pcd_ex.c \
$(HALS)/stm32f7xx_hal_hcd.c \
$(HALS)/stm32f7xx_hal_pwr.c \
$(HALS)/stm32f7xx_hal_pwr_ex.c \
$(HALS)/stm32f7xx_hal_rng.c \
Expand All @@ -115,12 +122,19 @@ SRC = main.c \
lib/shapes.c \
lib/slopes.c \
lib/midi.c \
lib/mhost.c \
$(wildcard ll/*.c) \
$(wildcard usbd/*.c) \
$(USBD)/Core/Src/usbd_core.c \
$(USBD)/Core/Src/usbd_ctlreq.c \
$(USBD)/Core/Src/usbd_ioreq.c \
$(USBD)/Class/CDC/Src/usbd_cdc.c \
$(wildcard usbh/*.c) \
$(USBH)/Core/Src/usbh_core.c \
$(USBH)/Core/Src/usbh_ctlreq.c \
$(USBH)/Core/Src/usbh_ioreq.c \
$(USBH)/Core/Src/usbh_pipes.c \
$(USBH)/Class/CDC/Src/usbh_cdc.c \
$(WRLIB)/str_buffer.c \
$(WRLIB)/wrConvert.c \
$(WRLIB)/wrMath.c \
Expand Down
63 changes: 63 additions & 0 deletions lib/mhost.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "mhost.h"

#include <stm32f7xx.h>

// #include "usb_device.h"

void MHost_Init(void){
// __HAL_RCC_GPIOB_CLK_ENABLE();

// GPIO_InitTypeDef g;
// g.Pin = GPIO_PIN_12;
// g.Mode = GPIO_MODE_OUTPUT_PP;
// g.Pull = GPIO_NOPULL;
// g.Speed = GPIO_SPEED_FAST;
// HAL_GPIO_Init(GPIOB, &g);
// HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, 0); // immediately turn off power




// from https://github.com/Hypnotriod/midi-box-stm32
// MX_USB_DEVICE_Init();



}

void MHost_Task(void){
// MIDI_ProcessUSBData(); // see /midi_router.c
}

void MHost_Power(int status){ // enable/disable +5v to connected device
// handled by the LL usbh driver
// HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, !!status);
}

// TODO support power error flag from usb switch

/*
void USB_LP_CAN1_RX0_IRQHandler(void)
{
HAL_PCD_IRQHandler(&hpcd_USB_FS);
}
USBD_HandleTypeDef hUsbDeviceFS;
void MX_USB_DEVICE_Init(void)
{
// Init Device Library, add supported class and start the library.
if (USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS) != USBD_OK)
{
Error_Handler();
}
if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_MIDI) != USBD_OK)
{
Error_Handler();
}
if (USBD_Start(&hUsbDeviceFS) != USBD_OK)
{
Error_Handler();
}
}
*/
22 changes: 22 additions & 0 deletions lib/mhost.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

void MHost_Init(void);
void MHost_Power(int status); // enable +5v to connected device

/*
// for the low-level driver
// B12: PWR_ENABLE: set high to engage power to the connected device
// B13: PWR_nERROR: set pullup. PSU pulls low if error occurs (need IRQ)
// set PWR_ENABLE low if this occurs (TODO retry?)
// B14: USB_D-
// B15: USB_D+
#define MIDI_IN_PORTS_NUM 0x01
#define MIDI_OUT_PORTS_NUM 0x03
void USB_LP_CAN1_RX0_IRQHandler(void);
extern PCD_HandleTypeDef hpcd_USB_FS;
void MX_USB_DEVICE_Init(void);
*/
4 changes: 4 additions & 0 deletions ll/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ static void Sys_Clk_Config(void)

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);

// USE CUBE MX Clock Configuration to set all of this!
// Currently using 168MHz core clock to reduce power dissipation in +3v3 regulator
// Will need to adjust PLLQ (for USB clock) when increasing clock with a better reg.

static RCC_OscInitTypeDef osc;
osc.OscillatorType = RCC_OSCILLATORTYPE_HSE;
osc.HSEState = RCC_HSE_ON;
Expand Down
57 changes: 53 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
#include "lib/lualink.h"
// #include "lib/repl.h"
#include "usbd/usbd_cdc_interface.h" // CDC_main_init()
#include "usbh/usbh_main.h"
#include "lib/bootloader.h" // bootloader_enter(), bootloader_restart()
#include "lib/flash.h" // Flash_clear_user_script()
#include "stm32f7xx_it.h" // CPU_count;

#include "lib/midi.h"
// #include "lib/mhost.h"
#include "ll/uart.h"

static Uart uart_rx;
Expand Down Expand Up @@ -59,6 +61,10 @@ int main(void)
Caw_Init( max_timers-1 ); // use last timer
CDC_clear_buffers();

// MIDI Host
// MHost_Init();
USBHost_Init();

// i2c_hw_pullups_init(); // enable GPIO for v1.1 hardware pullups
// ii_init( II_CROW );
// Random_Init();
Expand All @@ -68,22 +74,48 @@ int main(void)
// REPL_print_script_name();
// Lua_crowbegin();

// TODO startup animation
// here we run the power sequence to spread out current spikes when enabling
// setup the leds first so we can draw a nice animation while things get going
// mostly just doing this to get a predictable state before enabling USB Host
// HAL_Delay(1000);

// Enable USB Host power
// MHost_Power(1);



uint32_t last_tick = HAL_GetTick();
int saw = 0;
int g_state = 0;
int counter = 0;
int once = 1;
while(1){
CPU_count++;

saw++;
saw &= 0xfffff;
if(saw == 0x7ffff){
saw &= 0x3ffff;
if(saw == 0x1ffff){
Debug_Pin_Set(1, g_state);
g_state ^= 1;
// Caw_printf("hi\n\r");
// Caw_printf("%i\n\r",counter++);

uint8_t midi_msg[3] = {0x90, 0x3c, 0x64};
MIDI_transmit(&midi, midi_msg, 3);

if(once){
once = 0;

// char crow_msg[32];
// sprintf(crow_msg, "output[1].volts = %i\n\r", counter++);
// sprintf(crow_msg, "^^v\n\r", counter++);
// if(counter > 10) counter = 0;
// USBHost_Send(crow_msg, strlen(crow_msg)+1);
}
// char crow_msg[64];
// snprintf(crow_msg, 64, "print(time())\n\r");
// char* crow_msg = "^^v\n\r";
char* crow_msg = "print('hi')\n\r";
USBHost_Send((unsigned char*)crow_msg, strlen(crow_msg));
}

// U_PrintNow();
Expand Down Expand Up @@ -119,5 +151,22 @@ int main(void)
// event_next(); // check/execute single event
// ii_leader_process();
Caw_send_queued();

if( USBHost_BG_Task() ){ // data is ready to be used
uint8_t* buf;
size_t len = USBHost_Get_Received(&buf);
len = len > 100 ? 100 : len;
char crow_msg[100];
/*
for(int i=0; i<len; i++){
sprintf(&crow_msg[i*3], "%2x ", buf[i]);
// we overwrite each NULL char with next set
// last call leaves trailing NULL
}
*/
// sprintf(&crow_msg[len*3], "\n\r");
snprintf(crow_msg, len, buf);
Caw_printf("C:%s\n\r", crow_msg);
}
}
}
2 changes: 1 addition & 1 deletion stm32f7xx_hal_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
/*#define HAL_WWDG_MODULE_ENABLED*/
#define HAL_CORTEX_MODULE_ENABLED
#define HAL_PCD_MODULE_ENABLED
// #define HAL_HCD_MODULE_ENABLED
#define HAL_HCD_MODULE_ENABLED
// #define HAL_DFSDM_MODULE_ENABLED
// #define HAL_DSI_MODULE_ENABLED
/*#define HAL_JPEG_MODULE_ENABLED*/
Expand Down
8 changes: 8 additions & 0 deletions usbd/usbd_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,14 @@ void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
{
USBD_LL_Suspend(hpcd->pData);

/*
if (hpcd->Init.low_power_enable)
{
// Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register.
SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
}
*/
}

/**
Expand Down
9 changes: 8 additions & 1 deletion usbd/usbd_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,24 @@
/* Common Config */
#define USBD_MAX_NUM_INTERFACES 1
#define USBD_MAX_NUM_CONFIGURATION 1
#define USBD_MAX_STR_DESC_SIZ 0x100
#define USBD_MAX_STR_DESC_SIZ 0x100 // could be 512 (0x200)
#define USBD_SELF_POWERED 1
#define USBD_DEBUG_LEVEL 0

/* Exported macro ------------------------------------------------------------*/
/* Memory management macros */
void* malloc1( size_t size ); // like calloc, but sets to ones (??!?!?!)
//#define USBD_malloc (uint32_t *)USBD_static_malloc
#define USBD_malloc malloc1
// #define USBD_free USBD_static_free
#define USBD_free free
#define USBD_memset memset
#define USBD_memcpy memcpy

/* For footprint reasons and since only one allocation is handled in the MIDI class
driver, the malloc/free is changed into a static allocation method */
// void *USBD_static_malloc(uint32_t size);
// void USBD_static_free(void *p);

/* DEBUG macros */
#if (USBD_DEBUG_LEVEL > 0U)
Expand Down
3 changes: 1 addition & 2 deletions usbd/usbd_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ void USB_CDC_DeInit(void)

extern PCD_HandleTypeDef hpcd;

void OTG_FS_IRQHandler(void)
{
void OTG_FS_IRQHandler(void){
HAL_PCD_IRQHandler(&hpcd);
}
Loading

0 comments on commit c06299a

Please sign in to comment.