Skip to content

Commit

Permalink
Merge branch 'master' into bracz-lib-clean
Browse files Browse the repository at this point in the history
* master:
  Ensures that 'make clean' empties lib directory of symlinks. (#753)
  Add an input/output GPIO type. This helps support bit banged I2C. (#752)
  Adds TinyUsb based CDC virtual Com port driver with an STM32 port. (#748)
  Limit the amount of input we read into memory in arduino sketches. (#746)
  Updates to send_datagram cmdline utility (#740)
  • Loading branch information
balazsracz committed Dec 3, 2023
2 parents 85a3f80 + 23cd9ca commit 11bd2d7
Show file tree
Hide file tree
Showing 25 changed files with 1,339 additions and 14 deletions.
5 changes: 3 additions & 2 deletions applications/send_datagram/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ int appl_main(int argc, char *argv[])
}
if (wait_for_response) {
g_datagram_can.registry()->insert(&g_node, payload[0], &printer);
g_datagram_can.registry()->insert(&g_node, payload[0] ^ 1, &printer);
}
Buffer<openlcb::GenMessage> *b;
mainBufferPool->alloc(&b);
Expand All @@ -256,11 +257,11 @@ int appl_main(int argc, char *argv[])
fprintf(stderr, "Datagram send result: %04x\n", client->result());
if (!(client->result() & DatagramClient::OK_REPLY_PENDING)) {
LOG(INFO, "Target node indicates no response pending.");
} else if (wait_for_response) {
printer.wait();
}
if (wait_for_response) {
printer.wait();
g_datagram_can.registry()->erase(&g_node, payload[0], &printer);
g_datagram_can.registry()->erase(&g_node, payload[0] ^ 1, &printer);
}
}

Expand Down
20 changes: 10 additions & 10 deletions applications/usb_can/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@
#include "utils/GridConnectHub.hxx"
#include "utils/Hub.hxx"
#include "utils/HubDevice.hxx"
#include "utils/HubDeviceSelect.hxx"
#include "utils/blinker.h"

Executor<1> g_executor("g_executor", 0, 1024);
Executor<1> g_executor(NO_THREAD{});
Service g_service(&g_executor);
CanHubFlow can_hub0(&g_service);

Expand All @@ -62,7 +63,8 @@ OVERRIDE_CONST(main_thread_stack_size, 2500);
OVERRIDE_CONST(main_thread_stack_size, 900);
#endif

OVERRIDE_CONST(gridconnect_bridge_max_incoming_packets, 10);
OVERRIDE_CONST(gridconnect_bridge_max_incoming_packets, 5);
OVERRIDE_CONST(gridconnect_port_max_incoming_packets, 2);

/** Entry point to application.
* @param argc number of command line arguments
Expand All @@ -73,19 +75,17 @@ int appl_main(int argc, char* argv[])
{
int serial_fd = ::open("/dev/serUSB0", O_RDWR); // or /dev/ser0
HASSERT(serial_fd >= 0);
create_gc_port_for_can_hub(&can_hub0, serial_fd);
::fcntl(serial_fd, F_SETFL, O_NONBLOCK);

create_gc_port_for_can_hub(&can_hub0, serial_fd, nullptr, true);

int can_fd = ::open("/dev/can0", O_RDWR);
HASSERT(can_fd >= 0);
::fcntl(can_fd, F_SETFL, O_NONBLOCK);

FdHubPort<CanHubFlow> can_hub_port(
HubDeviceSelect<CanHubFlow> can_hub_port(
&can_hub0, can_fd, EmptyNotifiable::DefaultInstance());

while(1) {
sleep(1);
resetblink(1);
sleep(1);
resetblink(0);
}
g_executor.thread_body();
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
/** \copyright
* Copyright (c) 2012, Stuart W Baker
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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.
*
* \file HwInit.cxx
* This file represents the hardware initialization for the TI Tiva MCU.
*
* @author Stuart W. Baker
* @date 5 January 2013
*/

#include <new>
#include <cstdint>

#include "freertos_drivers/st/stm32f_hal_conf.hxx"
#include "stm32f0xx_hal_rcc.h"
#include "stm32f0xx_hal_flash.h"
#include "stm32f0xx_hal_gpio.h"
#include "stm32f0xx_hal_gpio_ex.h"
#include "stm32f0xx_hal_dma.h"
#include "stm32f0xx_hal_tim.h"

#include "os/OS.hxx"
#include "Stm32Uart.hxx"
#include "Stm32Can.hxx"
#include "Stm32UsbCdc.hxx"
#include "Stm32EEPROMEmulation.hxx"
#include "hardware.hxx"

/** override stdin */
const char *STDIN_DEVICE = "/dev/ser0";

/** override stdout */
const char *STDOUT_DEVICE = "/dev/ser0";

/** override stderr */
const char *STDERR_DEVICE = "/dev/ser0";

/** UART 0 serial driver instance */
// static Stm32Uart uart0("/dev/ser0", USART1, USART1_IRQn);

/** CAN 0 CAN driver instance */
static Stm32Can can0("/dev/can0");

/** EEPROM emulation driver. The file size might be made bigger. */
static Stm32EEPROMEmulation eeprom0("/dev/eeprom", 512);

const size_t EEPROMEmulation::SECTOR_SIZE = 2048;

/** USB-Serial instance */
static Stm32UsbCdc serUSB0("/dev/serUSB0");

extern "C" {

/** Blink LED */
uint32_t blinker_pattern = 0;
static uint32_t rest_pattern = 0;

void hw_set_to_safe(void)
{
}

void resetblink(uint32_t pattern)
{
BLINKER_RAW_Pin::set(pattern ? true : false);
blinker_pattern = pattern;
/* make a timer event trigger immediately */
}

void setblink(uint32_t pattern)
{
resetblink(pattern);
}

void timer14_interrupt_handler(void)
{
//
// Clear the timer interrupt.
//
TIM14->SR = ~TIM_IT_UPDATE;

// Set output LED.
BLINKER_RAW_Pin::set(rest_pattern & 1);

// Shift and maybe reset pattern.
rest_pattern >>= 1;
if (!rest_pattern)
{
rest_pattern = blinker_pattern;
}
}

void diewith(uint32_t pattern)
{
// vPortClearInterruptMask(0x20);
asm("cpsie i\n");

resetblink(pattern);
while (1)
;
}

/** Setup the system clock */
static void clock_setup(void)
{
/* reset clock configuration to default state */
RCC->CR = RCC_CR_HSITRIM_4 | RCC_CR_HSION;
while (!(RCC->CR & RCC_CR_HSIRDY))
;

#define USE_EXTERNAL_8_MHz_CLOCK_SOURCE 0
/* configure PLL: 8 MHz * 6 = 48 MHz */
#if USE_EXTERNAL_8_MHz_CLOCK_SOURCE
RCC->CR |= RCC_CR_HSEON;
while (!(RCC->CR & RCC_CR_HSERDY))
;
RCC->CFGR = RCC_CFGR_PLLMUL6 | RCC_CFGR_PLLSRC_HSE_PREDIV | RCC_CFGR_SW_HSE;
while (!((RCC->CFGR & RCC_CFGR_SWS) == RCC_CFGR_SWS_HSE))
;
#else
RCC->CFGR = RCC_CFGR_PLLMUL6 | RCC_CFGR_PLLSRC_HSI_PREDIV | RCC_CFGR_SW_HSI;
while (!((RCC->CFGR & RCC_CFGR_SWS) == RCC_CFGR_SWS_HSI))
;
#endif
/* enable PLL */
RCC->CR |= RCC_CR_PLLON;
while (!(RCC->CR & RCC_CR_PLLRDY))
;

/* set PLL as system clock */
RCC->CFGR = (RCC->CFGR & (~RCC_CFGR_SW)) | RCC_CFGR_SW_PLL;
while (!((RCC->CFGR & RCC_CFGR_SWS) == RCC_CFGR_SWS_PLL))
;

RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
/* Select HSI48 as USB clock source */
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USB;
PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);

/*Configure the clock recovery system (CRS)********************************/

/*Enable CRS Clock*/
__HAL_RCC_CRS_CLK_ENABLE();

RCC_CRSInitTypeDef RCC_CRSInitStruct = {0};

/* Default Synchro Signal division factor (not divided) */
RCC_CRSInitStruct.Prescaler = RCC_CRS_SYNC_DIV1;

/* Set the SYNCSRC[1:0] bits according to CRS_Source value */
RCC_CRSInitStruct.Source = RCC_CRS_SYNC_SOURCE_USB;

/* HSI48 is synchronized with USB SOF at 1KHz rate */
RCC_CRSInitStruct.ReloadValue =
__HAL_RCC_CRS_RELOADVALUE_CALCULATE(48000000, 1000);
RCC_CRSInitStruct.ErrorLimitValue = RCC_CRS_ERRORLIMIT_DEFAULT;

/* Set the TRIM[5:0] to the default value*/
RCC_CRSInitStruct.HSI48CalibrationValue = 0x20;

/* Start automatic synchronization */
HAL_RCCEx_CRSConfig(&RCC_CRSInitStruct);
}

/** Initialize the processor hardware.
*/
void hw_preinit(void)
{
/* Globally disables interrupts until the FreeRTOS scheduler is up. */
asm("cpsid i\n");

/* these FLASH settings enable opertion at 48 MHz */
__HAL_FLASH_PREFETCH_BUFFER_ENABLE();
__HAL_FLASH_SET_LATENCY(FLASH_LATENCY_1);

/* setup the system clock */
clock_setup();

/* enable peripheral clocks */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_CAN1_CLK_ENABLE();
__HAL_RCC_TIM14_CLK_ENABLE();
__HAL_RCC_USB_CLK_ENABLE();

/* setup pinmux */
GPIO_InitTypeDef gpio_init;
memset(&gpio_init, 0, sizeof(gpio_init));

/* USART1 pinmux on PA9 and PA10 */
gpio_init.Mode = GPIO_MODE_AF_PP;
gpio_init.Pull = GPIO_PULLUP;
gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
gpio_init.Alternate = GPIO_AF1_USART1;
gpio_init.Pin = GPIO_PIN_9;
HAL_GPIO_Init(GPIOA, &gpio_init);
gpio_init.Pin = GPIO_PIN_10;
HAL_GPIO_Init(GPIOA, &gpio_init);

/* CAN pinmux !!OPEN-DRAIN!! on PB8 and PB9 */
gpio_init.Mode = GPIO_MODE_AF_OD;
gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
gpio_init.Pull = GPIO_PULLUP;
gpio_init.Alternate = GPIO_AF4_CAN;
gpio_init.Pin = GPIO_PIN_8;
HAL_GPIO_Init(GPIOB, &gpio_init);
gpio_init.Pin = GPIO_PIN_9;
HAL_GPIO_Init(GPIOB, &gpio_init);

// USB Pins
// Configure USB DM and DP pins. This is optional, and maintained only for
// user guidance.
gpio_init.Pin = (GPIO_PIN_11 | GPIO_PIN_12);
gpio_init.Mode = GPIO_MODE_INPUT;
gpio_init.Pull = GPIO_NOPULL;
gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &gpio_init);

GpioInit::hw_init();

/* Initializes the blinker timer. */
TIM_HandleTypeDef TimHandle;
memset(&TimHandle, 0, sizeof(TimHandle));
TimHandle.Instance = TIM14;
TimHandle.Init.Period = configCPU_CLOCK_HZ / 10000 / 8;
TimHandle.Init.Prescaler = 10000;
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
TimHandle.Init.RepetitionCounter = 0;
if (HAL_TIM_Base_Init(&TimHandle) != HAL_OK)
{
/* Initialization Error */
HASSERT(0);
}
if (HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK)
{
/* Starting Error */
HASSERT(0);
}
SetInterruptPriority(TIM14_IRQn, 0);
NVIC_EnableIRQ(TIM14_IRQn);
}

void hw_postinit(void)
{
serUSB0.hw_postinit();
}

extern void dcd_int_handler(int);

void usb_interrupt_handler(void)
{
dcd_int_handler(0);
}

} // extern "C"
Loading

0 comments on commit 11bd2d7

Please sign in to comment.