Skip to content

Commit

Permalink
Small cleanups. (#163)
Browse files Browse the repository at this point in the history
Don't hardcode PCLK frequencies.
Remove unused #includes.
Enable USB2 console only when the peripheral exist.
  • Loading branch information
sarg committed Feb 13, 2024
1 parent a6019f4 commit 8beb06b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
3 changes: 0 additions & 3 deletions src/common/microsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@

#include "ff.h"

#include "usb1cfg.h"
#include "usb2cfg.h"

#include "microsd.h"
#include "common.h"

Expand Down
7 changes: 4 additions & 3 deletions src/drv/stm32cube/bsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
#include "ch.h"
#include "osal.h"

#include "hal_lld.h"
#include "bsp.h"
#include "bsp_gpio.h"

Expand Down Expand Up @@ -66,12 +67,12 @@ uint32_t HAL_GetTick(void)

uint32_t HAL_RCC_GetPCLK1Freq(void)
{
return 42000000;
return STM32_PCLK1;
}

uint32_t HAL_RCC_GetPCLK2Freq(void)
{
return 84000000;
return STM32_PCLK2;
}

bool delay_is_expired(bool start, uint32_t wait_nb_cycles)
Expand Down Expand Up @@ -108,7 +109,7 @@ void wait_delay(uint32_t wait_nb_cycles)

uint32_t bsp_get_apb1_freq(void)
{
return 42000000;
return STM32_PCLK1;
}

void reboot_usb_dfu(void)
Expand Down
3 changes: 0 additions & 3 deletions src/hydrabus/hydrabus_sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@

#include "ff.h"

#include "usb1cfg.h"
#include "usb2cfg.h"

#include "microsd.h"
#include "hydrabus_sd.h"
#include "common.h"
Expand Down
23 changes: 19 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
#include "common.h"

#include "usb1cfg.h"

#if STM32_USB_USE_OTG2
#include "usb2cfg.h"
#endif

#include "microsd.h"
#include "hydrabus.h"
Expand All @@ -51,8 +54,10 @@ volatile int nb_console = 0;

/* USB1: Virtual serial port over USB. */
SerialUSBDriver SDU1;
#ifdef STM32_USB_USE_OTG2
/* USB2: Virtual serial port over USB. */
SerialUSBDriver SDU2;
#endif

extern t_token tl_tokens[];
extern t_token_dict tl_dict[];
Expand All @@ -61,12 +66,16 @@ extern t_token_dict tl_dict[];
t_tokenline tl_con1 __attribute__ ((section(".ram4")));
t_mode_config mode_con1 __attribute__ ((section(".ram4"))) = { .proto={ .dev_num = 0 }, .cmd={ 0 } };

#if STM32_USB_USE_OTG2
t_tokenline tl_con2 __attribute__ ((section(".ram4")));
t_mode_config mode_con2 __attribute__ ((section(".ram4"))) = { .proto={ .dev_num = 0 }, .cmd={ 0 } };
#endif

t_hydra_console consoles[] = {
{ .thread_name="console USB1", .sdu=&SDU1, .tl=&tl_con1, .mode = &mode_con1 },
{ .thread_name="console USB2", .sdu=&SDU2, .tl=&tl_con2, .mode = &mode_con2 }
{ .thread_name="console USB1", .sdu=&SDU1, .tl=&tl_con1, .mode = &mode_con1 }
#if STM32_USB_USE_OTG2
,{ .thread_name="console USB2", .sdu=&SDU2, .tl=&tl_con2, .mode = &mode_con2 }
#endif
};

THD_FUNCTION(console, arg)
Expand Down Expand Up @@ -131,7 +140,7 @@ volatile int a, b, c;

int main(void)
{
int sleep_ms, i;
uint32_t sleep_ms, i;
int local_nb_console;
#ifdef HYDRANFC
bool hydranfc_detected;
Expand Down Expand Up @@ -170,16 +179,20 @@ int main(void)
sduObjectInit(&SDU1);
sduStart(&SDU1, &serusb1cfg);

#if STM32_USB_USE_OTG2
sduObjectInit(&SDU2);
sduStart(&SDU2, &serusb2cfg);
#endif

/*
* Activates the USB1 & 2 driver and then the USB bus pull-up on D+.
* Note, a delay is inserted in order to not have to disconnect the cable
* after a reset.
*/
usbDisconnectBus(serusb1cfg.usbp);
#if STM32_USB_USE_OTG2
usbDisconnectBus(serusb2cfg.usbp);
#endif

chThdSleepMilliseconds(500);

Expand All @@ -194,8 +207,10 @@ int main(void)

usbConnectBus(serusb1cfg.usbp);

#if STM32_USB_USE_OTG2
usbStart(serusb2cfg.usbp, &usb2cfg);
usbConnectBus(serusb2cfg.usbp);
#endif

/* Wait for USB Enumeration. */
chThdSleepMilliseconds(100);
Expand All @@ -210,7 +225,7 @@ int main(void)
chRegSetThreadName("main");
while (TRUE) {
local_nb_console = 0;
for (i = 0; i < 2; i++) {
for (i = 0; i < ARRAY_SIZE(consoles); i++) {
if (!consoles[i].thread) {
if (consoles[i].sdu->config->usbp->state != USB_ACTIVE)
continue;
Expand Down

0 comments on commit 8beb06b

Please sign in to comment.