From 9fb4b146fcbcea6d83ac80a4ebfcbbd6d1e2e4e6 Mon Sep 17 00:00:00 2001 From: crinq Date: Wed, 6 Sep 2017 04:20:06 +0200 Subject: [PATCH] code format --- bootloader/src/main.c | 96 +- bootloader/src/stm32f4xx_it.c | 36 +- bootloader/src/system_stm32f4xx.c | 151 ++- bootloader/src/version.c | 9 +- f3dfu/Inc/stm32f3xx_hal_conf.h | 146 +-- f3dfu/Inc/stm32f3xx_it.h | 4 +- f3dfu/Inc/usb_device.h | 4 +- f3dfu/Inc/usbd_conf.h | 127 +-- f3dfu/Inc/usbd_desc.h | 28 +- f3dfu/Inc/usbd_dfu_if.h | 32 +- f3dfu/Src/main.c | 183 ++-- f3dfu/Src/stm32f3xx_hal_msp.c | 59 +- f3dfu/Src/stm32f3xx_it.c | 44 +- f3dfu/Src/system_stm32f3xx.c | 66 +- f3dfu/Src/usb_device.c | 6 +- f3dfu/Src/usbd_conf.c | 587 +++++------ f3dfu/Src/usbd_desc.c | 215 ++-- f3dfu/Src/usbd_dfu_if.c | 175 ++-- inc/hw/hw.h | 34 +- inc/main.h | 38 +- inc/old/eeprom.h | 46 +- inc/old/link.h | 4 +- inc/setup.h | 4 +- inc/sserial.h | 122 +-- inc/stm32f4xx_conf.h | 10 +- inc/stm32f4xx_it.h | 6 +- inc/usbd_cdc_if.h | 8 +- shared/angle.c | 88 +- shared/commands.c | 46 +- shared/commands.h | 10 +- shared/common.h | 118 +-- shared/comps/acim_ttc.c | 92 +- shared/comps/curpid.c | 164 +-- shared/comps/dc.c | 28 +- shared/comps/dq.c | 90 +- shared/comps/fanuc.c | 72 +- shared/comps/fault.c | 598 +++++------ shared/comps/fb_switch.c | 188 ++-- shared/comps/hal_test.c | 54 +- shared/comps/idq.c | 58 +- shared/comps/iit.c | 114 +-- shared/comps/linrev.c | 79 +- shared/comps/pe.c | 40 +- shared/comps/pid.c | 367 ++++--- shared/comps/pmsm_limits.c | 101 +- shared/comps/pmsm_ttc.c | 49 +- shared/comps/psi.c | 59 +- shared/comps/reslimit.c | 33 +- shared/comps/rev.c | 39 +- shared/comps/sim.c | 122 +-- shared/comps/spid.c | 143 ++- shared/comps/stp.c | 91 +- shared/comps/svm.c | 135 +-- shared/comps/term.c | 135 +-- shared/comps/uvw.c | 75 +- shared/comps/vel.c | 99 +- shared/comps/vel_int.c | 93 +- shared/comps/ypid.c | 171 ++-- shared/config.c | 40 +- shared/config.h | 6 +- shared/crc16.c | 45 +- shared/crc16.h | 14 +- shared/crc32.c | 45 +- shared/crc32.h | 14 +- shared/crc8.c | 45 +- shared/crc8.h | 14 +- shared/defines.h | 48 +- shared/f3hw.h | 8 +- shared/hal.c | 1510 ++++++++++++++-------------- shared/hal.h | 212 ++-- shared/hw_math.h | 2 +- shared/old/hal_old.c | 555 +++++----- shared/old/hal_old.h | 236 ++--- shared/old/hal_term.c | 270 ++--- shared/old/hal_term.h | 4 +- shared/ringbuf.c | 173 ++-- shared/ringbuf.h | 11 +- shared/version.h | 34 +- src/comps/adc.c | 227 +++-- src/comps/conf.c | 28 +- src/comps/enc_cmd.c | 287 +++--- src/comps/enc_fb.c | 216 ++-- src/comps/encm.c | 115 +-- src/comps/hv.c | 333 +++--- src/comps/hw/io4.c | 773 +++++++------- src/comps/o_fb.c | 103 +- src/comps/res.c | 257 +++-- src/comps/sserial.c | 881 ++++++++-------- src/main.c | 190 ++-- src/old/eeprom.c | 303 +++--- src/old/link.c | 724 ++++++------- src/setup.c | 282 +++--- src/stm32f4xx_it.c | 26 +- src/syscalls.c | 96 +- src/system_stm32f4xx.c | 151 ++- src/usb_cdc.c | 286 +++--- src/version.c | 9 +- stm32f103/inc/stm32f10x_conf.h | 8 +- stm32f103/inc/stm32f10x_it.h | 6 +- stm32f103/src/main.c | 861 ++++++++-------- stm32f103/src/version.c | 9 +- stm32f303/inc/adc.h | 2 +- stm32f303/inc/dac.h | 2 +- stm32f303/inc/main.h | 6 +- stm32f303/inc/opamp.h | 2 +- stm32f303/inc/stm32f3xx_hal_conf.h | 146 +-- stm32f303/inc/stm32f3xx_it.h | 4 +- stm32f303/inc/tim.h | 6 +- stm32f303/inc/usb_device.h | 4 +- stm32f303/inc/usbd_cdc_if.h | 40 +- stm32f303/inc/usbd_conf.h | 123 +-- stm32f303/inc/usbd_desc.h | 28 +- stm32f303/src/adc.c | 504 +++++----- stm32f303/src/comps/enc.c | 76 +- stm32f303/src/comps/hv.c | 77 +- stm32f303/src/comps/hvdc.c | 35 +- stm32f303/src/comps/io.c | 112 ++- stm32f303/src/comps/ls.c | 384 +++---- stm32f303/src/dac.c | 51 +- stm32f303/src/main.c | 249 +++-- stm32f303/src/opamp.c | 156 ++- stm32f303/src/stm32f3xx_hal_msp.c | 3 +- stm32f303/src/stm32f3xx_it.c | 30 +- stm32f303/src/tim.c | 150 ++- stm32f303/src/usb_device.c | 6 +- stm32f303/src/usbd_cdc_if.c | 252 +++-- stm32f303/src/usbd_conf.c | 289 +++--- stm32f303/src/usbd_desc.c | 215 ++-- stm32f303/src/version.c | 9 +- 129 files changed, 8831 insertions(+), 9378 deletions(-) mode change 100755 => 100644 inc/old/eeprom.h mode change 100755 => 100644 src/old/eeprom.c mode change 100755 => 100644 stm32f103/inc/stm32f10x_conf.h mode change 100755 => 100644 stm32f103/inc/stm32f10x_it.h diff --git a/bootloader/src/main.c b/bootloader/src/main.c index 2cba5598..7218b7a3 100644 --- a/bootloader/src/main.c +++ b/bootloader/src/main.c @@ -22,63 +22,63 @@ #include "version.h" #if __GNUC__ < 5 - #error gcc to old (< 5.0) +#error gcc to old (< 5.0) #endif #define APP_START 0x08010000 -#define APP_END 0x08100000 +#define APP_END 0x08100000 #define APP_RANGE_VALID(a, s) (!(((a) | (s)) & 3) && (a) >= APP_START && ((a) + (s)) <= APP_END) #define VERSION_INFO_OFFSET 0x188 -static volatile const struct version_info *app_info = (void*)(APP_START + VERSION_INFO_OFFSET); +static volatile const struct version_info *app_info = (void *)(APP_START + VERSION_INFO_OFFSET); -static int app_ok(void) -{ - if (!APP_RANGE_VALID(APP_START, app_info->image_size)) { - return 0; - } - CRC_ResetDR(); - uint32_t crc = CRC_CalcBlockCRC((uint32_t *) APP_START, app_info->image_size / 4); +static int app_ok(void) { + if(!APP_RANGE_VALID(APP_START, app_info->image_size)) { + return 0; + } + CRC_ResetDR(); + uint32_t crc = CRC_CalcBlockCRC((uint32_t *)APP_START, app_info->image_size / 4); - if (crc != 0) { - return 0; - } + if(crc != 0) { + return 0; + } - return 1; + return 1; } -int main(void) -{ - GPIO_InitTypeDef GPIO_InitDef; - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_CRC, ENABLE); - GPIO_InitDef.GPIO_Pin = GPIO_Pin_13; - GPIO_InitDef.GPIO_Mode = GPIO_Mode_IN; - GPIO_InitDef.GPIO_OType = GPIO_OType_PP; - GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_UP; - GPIO_InitDef.GPIO_Speed = GPIO_Speed_2MHz; - GPIO_Init(GPIOA, &GPIO_InitDef); - uint32_t pin = !GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_13); - RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA, ENABLE); // reset gpio a - RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA, DISABLE); - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, DISABLE); +int main(void) { + GPIO_InitTypeDef GPIO_InitDef; + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_CRC, ENABLE); + GPIO_InitDef.GPIO_Pin = GPIO_Pin_13; + GPIO_InitDef.GPIO_Mode = GPIO_Mode_IN; + GPIO_InitDef.GPIO_OType = GPIO_OType_PP; + GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_UP; + GPIO_InitDef.GPIO_Speed = GPIO_Speed_2MHz; + GPIO_Init(GPIOA, &GPIO_InitDef); + uint32_t pin = !GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_13); + RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA, ENABLE); // reset gpio a + RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOA, DISABLE); + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, DISABLE); - void (*SysMemBootJump)(void); - if ( (*((unsigned long *)0x2001C000) == 0xDEADBEEF) || pin || !app_ok()) {//Memory map, datasheet - *((unsigned long *)0x2001C000) = 0xCAFEFEED; //Reset bootloader trigger - __set_MSP(0x20001000); - //Point the PC to the System Memory reset vector (+4) - //AN2606 - //Table 64. Bootloader device-dependent parameters - SysMemBootJump = (void (*)(void)) (*((uint32_t *) 0x1FFF0004)); - SysMemBootJump(); - while (1); - } else { - uint32_t stack = ((const uint32_t *) APP_START)[0]; - uint32_t entry = ((const uint32_t *) APP_START)[1]; - asm volatile( - "msr msp, %0 \n\t" - "bx %1 \n\t" - : : "r" (stack), "r" (entry) - ); - while (1); - } + void (*SysMemBootJump)(void); + if((*((unsigned long *)0x2001C000) == 0xDEADBEEF) || pin || !app_ok()) { //Memory map, datasheet + *((unsigned long *)0x2001C000) = 0xCAFEFEED; //Reset bootloader trigger + __set_MSP(0x20001000); + //Point the PC to the System Memory reset vector (+4) + //AN2606 + //Table 64. Bootloader device-dependent parameters + SysMemBootJump = (void (*)(void))(*((uint32_t *)0x1FFF0004)); + SysMemBootJump(); + while(1) + ; + } else { + uint32_t stack = ((const uint32_t *)APP_START)[0]; + uint32_t entry = ((const uint32_t *)APP_START)[1]; + asm volatile( + "msr msp, %0 \n\t" + "bx %1 \n\t" + : + : "r"(stack), "r"(entry)); + while(1) + ; + } } diff --git a/bootloader/src/stm32f4xx_it.c b/bootloader/src/stm32f4xx_it.c index 635a70b7..f9126ac9 100644 --- a/bootloader/src/stm32f4xx_it.c +++ b/bootloader/src/stm32f4xx_it.c @@ -48,8 +48,7 @@ * @param None * @retval None */ -void NMI_Handler(void) -{ +void NMI_Handler(void) { } /** @@ -57,11 +56,9 @@ void NMI_Handler(void) * @param None * @retval None */ -void HardFault_Handler(void) -{ +void HardFault_Handler(void) { /* Go to infinite loop when Hard Fault exception occurs */ - while (1) - { + while(1) { } } @@ -70,11 +67,9 @@ void HardFault_Handler(void) * @param None * @retval None */ -void MemManage_Handler(void) -{ +void MemManage_Handler(void) { /* Go to infinite loop when Memory Manage exception occurs */ - while (1) - { + while(1) { } } @@ -83,11 +78,9 @@ void MemManage_Handler(void) * @param None * @retval None */ -void BusFault_Handler(void) -{ +void BusFault_Handler(void) { /* Go to infinite loop when Bus Fault exception occurs */ - while (1) - { + while(1) { } } @@ -96,11 +89,9 @@ void BusFault_Handler(void) * @param None * @retval None */ -void UsageFault_Handler(void) -{ +void UsageFault_Handler(void) { /* Go to infinite loop when Usage Fault exception occurs */ - while (1) - { + while(1) { } } @@ -109,8 +100,7 @@ void UsageFault_Handler(void) * @param None * @retval None */ -void SVC_Handler(void) -{ +void SVC_Handler(void) { } /** @@ -118,8 +108,7 @@ void SVC_Handler(void) * @param None * @retval None */ -void DebugMon_Handler(void) -{ +void DebugMon_Handler(void) { } /** @@ -127,8 +116,7 @@ void DebugMon_Handler(void) * @param None * @retval None */ -void PendSV_Handler(void) -{ +void PendSV_Handler(void) { } /** diff --git a/bootloader/src/system_stm32f4xx.c b/bootloader/src/system_stm32f4xx.c index 133f97e4..3a83d5a2 100644 --- a/bootloader/src/system_stm32f4xx.c +++ b/bootloader/src/system_stm32f4xx.c @@ -139,19 +139,19 @@ /*!< Uncomment the following line if you need to relocate your vector Table in Internal SRAM. */ /* #define VECT_TAB_SRAM */ -#define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field. - This value must be a multiple of 0x200. */ +#define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field. \ + This value must be a multiple of 0x200. */ /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */ -#define PLL_M 8 -#define PLL_N 336 +#define PLL_M 8 +#define PLL_N 336 /* SYSCLK = PLL_VCO / PLL_P */ -#define PLL_P 2 +#define PLL_P 2 /* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */ -#define PLL_Q 7 +#define PLL_Q 7 /** * @} @@ -169,9 +169,9 @@ * @{ */ - uint32_t SystemCoreClock = 168000000; +uint32_t SystemCoreClock = 168000000; - __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}; +__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}; /** * @} @@ -183,7 +183,7 @@ static void SetSysClock(void); #ifdef DATA_IN_ExtSRAM - static void SystemInit_ExtMemCtl(void); +static void SystemInit_ExtMemCtl(void); #endif /* DATA_IN_ExtSRAM */ /** @@ -201,9 +201,8 @@ static void SetSysClock(void); * @param None * @retval None */ -void SystemInit(void) -{ - SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* Set CP10 and CP11 to full access */ +void SystemInit(void) { + SCB->CPACR |= ((3UL << 10 * 2) | (3UL << 11 * 2)); /* Set CP10 and CP11 to full access */ /* Reset the RCC clock configuration to the default reset state ------------*/ /* Set HSION bit */ @@ -232,7 +231,7 @@ void SystemInit(void) AHB/APBx prescalers and Flash settings ----------------------------------*/ SetSysClock(); - /* Configure the Vector Table location add offset address ------------------*/ +/* Configure the Vector Table location add offset address ------------------*/ #ifdef VECT_TAB_SRAM SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ #else @@ -276,41 +275,36 @@ void SystemInit(void) * @param None * @retval None */ -void SystemCoreClockUpdate(void) -{ +void SystemCoreClockUpdate(void) { uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2; /* Get SYSCLK source -------------------------------------------------------*/ tmp = RCC->CFGR & RCC_CFGR_SWS; - switch (tmp) - { - case 0x00: /* HSI used as system clock source */ + switch(tmp) { + case 0x00: /* HSI used as system clock source */ SystemCoreClock = HSI_VALUE; break; - case 0x04: /* HSE used as system clock source */ + case 0x04: /* HSE used as system clock source */ SystemCoreClock = HSE_VALUE; break; - case 0x08: /* PLL P used as system clock source */ + case 0x08: /* PLL P used as system clock source */ /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N SYSCLK = PLL_VCO / PLL_P */ pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22; - pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM; + pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM; - if (pllsource != 0) - { + if(pllsource != 0) { /* HSE used as PLL clock source */ pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6); - } - else - { + } else { /* HSI used as PLL clock source */ pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6); } - pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2; - SystemCoreClock = pllvco/pllp; + pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >> 16) + 1) * 2; + SystemCoreClock = pllvco / pllp; break; default: SystemCoreClock = HSI_VALUE; @@ -331,34 +325,28 @@ void SystemCoreClockUpdate(void) * @param None * @retval None */ -static void SetSysClock(void) -{ -/******************************************************************************/ -/* PLL (clocked by HSE) used as System clock source */ -/******************************************************************************/ +static void SetSysClock(void) { + /******************************************************************************/ + /* PLL (clocked by HSE) used as System clock source */ + /******************************************************************************/ __IO uint32_t StartUpCounter = 0, HSEStatus = 0; /* Enable HSE */ RCC->CR |= ((uint32_t)RCC_CR_HSEON); /* Wait till HSE is ready and if Time out is reached exit */ - do - { + do { HSEStatus = RCC->CR & RCC_CR_HSERDY; StartUpCounter++; } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT)); - if ((RCC->CR & RCC_CR_HSERDY) != RESET) - { + if((RCC->CR & RCC_CR_HSERDY) != RESET) { HSEStatus = (uint32_t)0x01; - } - else - { + } else { HSEStatus = (uint32_t)0x00; } - if (HSEStatus == (uint32_t)0x01) - { + if(HSEStatus == (uint32_t)0x01) { /* Enable high performance mode, System frequency up to 168 MHz */ RCC->APB1ENR |= RCC_APB1ENR_PWREN; PWR->CR |= PWR_CR_VOS; @@ -373,34 +361,31 @@ static void SetSysClock(void) RCC->CFGR |= RCC_CFGR_PPRE1_DIV4; /* Configure the main PLL */ - RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) | - (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24); + RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) - 1) << 16) | + (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24); /* Enable the main PLL */ RCC->CR |= RCC_CR_PLLON; /* Wait till the main PLL is ready */ - while((RCC->CR & RCC_CR_PLLRDY) == 0) - { + while((RCC->CR & RCC_CR_PLLRDY) == 0) { } /* Configure Flash prefetch, Instruction cache, Data cache and wait state */ - FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS; + FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_5WS; /* Select the main PLL as system clock source */ - RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW)); + RCC->CFGR &= (uint32_t)((uint32_t) ~(RCC_CFGR_SW)); RCC->CFGR |= RCC_CFGR_SW_PLL; /* Wait till the main PLL is used as system clock source */ - while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL); + while((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL) + ; { } - } - else - { /* If HSE fails to start-up, the application will have wrong clock + } else { /* If HSE fails to start-up, the application will have wrong clock configuration. User can add here some code to deal with this error */ } - } /** @@ -418,10 +403,9 @@ static void SetSysClock(void) * @param None * @retval None */ -void SystemInit_ExtMemCtl(void) -{ -/*-- GPIOs Configuration -----------------------------------------------------*/ -/* +void SystemInit_ExtMemCtl(void) { + /*-- GPIOs Configuration -----------------------------------------------------*/ + /* +-------------------+--------------------+------------------+------------------+ + SRAM pins assignment + +-------------------+--------------------+------------------+------------------+ @@ -440,66 +424,66 @@ void SystemInit_ExtMemCtl(void) | | PE15 <-> FSMC_D12 | +-------------------+--------------------+ */ - /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */ - RCC->AHB1ENR = 0x00000078; + /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */ + RCC->AHB1ENR = 0x00000078; /* Connect PDx pins to FSMC Alternate function */ - GPIOD->AFR[0] = 0x00cc00cc; - GPIOD->AFR[1] = 0xcc0ccccc; + GPIOD->AFR[0] = 0x00cc00cc; + GPIOD->AFR[1] = 0xcc0ccccc; /* Configure PDx pins in Alternate function mode */ - GPIOD->MODER = 0xaaaa0a0a; + GPIOD->MODER = 0xaaaa0a0a; /* Configure PDx pins speed to 100 MHz */ GPIOD->OSPEEDR = 0xffff0f0f; /* Configure PDx pins Output type to push-pull */ - GPIOD->OTYPER = 0x00000000; + GPIOD->OTYPER = 0x00000000; /* No pull-up, pull-down for PDx pins */ - GPIOD->PUPDR = 0x00000000; + GPIOD->PUPDR = 0x00000000; /* Connect PEx pins to FSMC Alternate function */ - GPIOE->AFR[0] = 0xc00cc0cc; - GPIOE->AFR[1] = 0xcccccccc; + GPIOE->AFR[0] = 0xc00cc0cc; + GPIOE->AFR[1] = 0xcccccccc; /* Configure PEx pins in Alternate function mode */ - GPIOE->MODER = 0xaaaa828a; + GPIOE->MODER = 0xaaaa828a; /* Configure PEx pins speed to 100 MHz */ GPIOE->OSPEEDR = 0xffffc3cf; /* Configure PEx pins Output type to push-pull */ - GPIOE->OTYPER = 0x00000000; + GPIOE->OTYPER = 0x00000000; /* No pull-up, pull-down for PEx pins */ - GPIOE->PUPDR = 0x00000000; + GPIOE->PUPDR = 0x00000000; /* Connect PFx pins to FSMC Alternate function */ - GPIOF->AFR[0] = 0x00cccccc; - GPIOF->AFR[1] = 0xcccc0000; + GPIOF->AFR[0] = 0x00cccccc; + GPIOF->AFR[1] = 0xcccc0000; /* Configure PFx pins in Alternate function mode */ - GPIOF->MODER = 0xaa000aaa; + GPIOF->MODER = 0xaa000aaa; /* Configure PFx pins speed to 100 MHz */ GPIOF->OSPEEDR = 0xff000fff; /* Configure PFx pins Output type to push-pull */ - GPIOF->OTYPER = 0x00000000; + GPIOF->OTYPER = 0x00000000; /* No pull-up, pull-down for PFx pins */ - GPIOF->PUPDR = 0x00000000; + GPIOF->PUPDR = 0x00000000; /* Connect PGx pins to FSMC Alternate function */ - GPIOG->AFR[0] = 0x00cccccc; - GPIOG->AFR[1] = 0x000000c0; + GPIOG->AFR[0] = 0x00cccccc; + GPIOG->AFR[1] = 0x000000c0; /* Configure PGx pins in Alternate function mode */ - GPIOG->MODER = 0x00080aaa; + GPIOG->MODER = 0x00080aaa; /* Configure PGx pins speed to 100 MHz */ GPIOG->OSPEEDR = 0x000c0fff; /* Configure PGx pins Output type to push-pull */ - GPIOG->OTYPER = 0x00000000; + GPIOG->OTYPER = 0x00000000; /* No pull-up, pull-down for PGx pins */ - GPIOG->PUPDR = 0x00000000; + GPIOG->PUPDR = 0x00000000; -/*-- FSMC Configuration ------------------------------------------------------*/ + /*-- FSMC Configuration ------------------------------------------------------*/ /* Enable the FSMC interface clock */ - RCC->AHB3ENR = 0x00000001; + RCC->AHB3ENR = 0x00000001; /* Configure and enable Bank1_SRAM2 */ FSMC_Bank1->BTCR[2] = 0x00001015; - FSMC_Bank1->BTCR[3] = 0x00010603;//0x00010400; + FSMC_Bank1->BTCR[3] = 0x00010603; //0x00010400; FSMC_Bank1E->BWTR[2] = 0x0fffffff; -/* + /* Bank1_SRAM2 is configured as follow: p.FSMC_AddressSetupTime = 3;//0; @@ -526,7 +510,6 @@ void SystemInit_ExtMemCtl(void) FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p; FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p; */ - } #endif /* DATA_IN_ExtSRAM */ diff --git a/bootloader/src/version.c b/bootloader/src/version.c index 55c7d6bc..2d869b48 100644 --- a/bootloader/src/version.c +++ b/bootloader/src/version.c @@ -1,8 +1,7 @@ #include "version.h" volatile const struct version_info version_info = { - .product_name = "STMBL-Bootloader", - .major = 0, - .minor = 9, - .patch = 0 -}; + .product_name = "STMBL-Bootloader", + .major = 0, + .minor = 9, + .patch = 0}; diff --git a/f3dfu/Inc/stm32f3xx_hal_conf.h b/f3dfu/Inc/stm32f3xx_hal_conf.h index 5532a9ee..90c7037f 100644 --- a/f3dfu/Inc/stm32f3xx_hal_conf.h +++ b/f3dfu/Inc/stm32f3xx_hal_conf.h @@ -30,14 +30,14 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** - */ + */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __STM32F3xx_HAL_CONF_H #define __STM32F3xx_HAL_CONF_H #ifdef __cplusplus - extern "C" { +extern "C" { #endif /* Exported types ------------------------------------------------------------*/ @@ -47,8 +47,8 @@ /** * @brief This is the list of modules to be used in the HAL driver */ - -#define HAL_MODULE_ENABLED + +#define HAL_MODULE_ENABLED /*#define HAL_ADC_MODULE_ENABLED */ /*#define HAL_CAN_MODULE_ENABLED */ /*#define HAL_CEC_MODULE_ENABLED */ @@ -92,16 +92,16 @@ * This value is used by the RCC HAL module to compute the system frequency * (when HSE is used as system clock source, directly or through the PLL). */ -#if !defined (HSE_VALUE) - #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ +#if !defined(HSE_VALUE) +#define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ #endif /* HSE_VALUE */ /** * @brief In the following line adjust the External High Speed oscillator (HSE) Startup * Timeout value */ -#if !defined (HSE_STARTUP_TIMEOUT) - #define HSE_STARTUP_TIMEOUT ((uint32_t)100) /*!< Time out for HSE start up, in ms */ +#if !defined(HSE_STARTUP_TIMEOUT) +#define HSE_STARTUP_TIMEOUT ((uint32_t)100) /*!< Time out for HSE start up, in ms */ #endif /* HSE_STARTUP_TIMEOUT */ /** @@ -109,38 +109,38 @@ * This value is used by the RCC HAL module to compute the system frequency * (when HSI is used as system clock source, directly or through the PLL). */ -#if !defined (HSI_VALUE) - #define HSI_VALUE ((uint32_t)8000000) /*!< Value of the Internal oscillator in Hz*/ +#if !defined(HSI_VALUE) +#define HSI_VALUE ((uint32_t)8000000) /*!< Value of the Internal oscillator in Hz*/ #endif /* HSI_VALUE */ /** * @brief In the following line adjust the Internal High Speed oscillator (HSI) Startup * Timeout value */ -#if !defined (HSI_STARTUP_TIMEOUT) - #define HSI_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for HSI start up */ -#endif /* HSI_STARTUP_TIMEOUT */ +#if !defined(HSI_STARTUP_TIMEOUT) +#define HSI_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for HSI start up */ +#endif /* HSI_STARTUP_TIMEOUT */ /** * @brief Internal Low Speed oscillator (LSI) value. */ -#if !defined (LSI_VALUE) - #define LSI_VALUE ((uint32_t)40000) -#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz - The real value may vary depending on the variations - in voltage and temperature. */ +#if !defined(LSI_VALUE) +#define LSI_VALUE ((uint32_t)40000) +#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz \ + The real value may vary depending on the variations \ + in voltage and temperature. */ /** * @brief External Low Speed oscillator (LSE) value. */ -#if !defined (LSE_VALUE) - #define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ -#endif /* LSE_VALUE */ +#if !defined(LSE_VALUE) +#define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ +#endif /* LSE_VALUE */ /** * @brief Time out for LSE start up value in ms. */ -#if !defined (LSE_STARTUP_TIMEOUT) - #define LSE_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for LSE start up, in ms */ +#if !defined(LSE_STARTUP_TIMEOUT) +#define LSE_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for LSE start up, in ms */ #endif /* LSE_STARTUP_TIMEOUT */ /** @@ -150,8 +150,8 @@ * - External clock generated through external PLL component on EVAL 303 (based on MCO or crystal) * - External clock not generated on EVAL 373 */ -#if !defined (EXTERNAL_CLOCK_VALUE) - #define EXTERNAL_CLOCK_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz*/ +#if !defined(EXTERNAL_CLOCK_VALUE) +#define EXTERNAL_CLOCK_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz*/ #endif /* EXTERNAL_CLOCK_VALUE */ /* Tip: To avoid modifying this file each time you need to use different HSE, @@ -160,14 +160,14 @@ /* ########################### System Configuration ######################### */ /** * @brief This is the HAL system configuration section - */ + */ -#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ -#define TICK_INT_PRIORITY ((uint32_t)0) /*!< tick interrupt priority (lowest by default) */ -#define USE_RTOS 0 -#define PREFETCH_ENABLE 1 -#define INSTRUCTION_CACHE_ENABLE 0 -#define DATA_CACHE_ENABLE 0 +#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ +#define TICK_INT_PRIORITY ((uint32_t)0) /*!< tick interrupt priority (lowest by default) */ +#define USE_RTOS 0 +#define PREFETCH_ENABLE 1 +#define INSTRUCTION_CACHE_ENABLE 0 +#define DATA_CACHE_ENABLE 0 /* ########################## Assert Selection ############################## */ /** @@ -182,139 +182,139 @@ */ #ifdef HAL_RCC_MODULE_ENABLED - #include "stm32f3xx_hal_rcc.h" +#include "stm32f3xx_hal_rcc.h" #endif /* HAL_RCC_MODULE_ENABLED */ #ifdef HAL_GPIO_MODULE_ENABLED - #include "stm32f3xx_hal_gpio.h" +#include "stm32f3xx_hal_gpio.h" #endif /* HAL_GPIO_MODULE_ENABLED */ #ifdef HAL_DMA_MODULE_ENABLED - #include "stm32f3xx_hal_dma.h" +#include "stm32f3xx_hal_dma.h" #endif /* HAL_DMA_MODULE_ENABLED */ - + #ifdef HAL_CORTEX_MODULE_ENABLED - #include "stm32f3xx_hal_cortex.h" +#include "stm32f3xx_hal_cortex.h" #endif /* HAL_CORTEX_MODULE_ENABLED */ #ifdef HAL_ADC_MODULE_ENABLED - #include "stm32f3xx_hal_adc.h" +#include "stm32f3xx_hal_adc.h" #endif /* HAL_ADC_MODULE_ENABLED */ #ifdef HAL_CAN_MODULE_ENABLED - #include "stm32f3xx_hal_can.h" +#include "stm32f3xx_hal_can.h" #endif /* HAL_CAN_MODULE_ENABLED */ #ifdef HAL_CEC_MODULE_ENABLED - #include "stm32f3xx_hal_cec.h" +#include "stm32f3xx_hal_cec.h" #endif /* HAL_CEC_MODULE_ENABLED */ #ifdef HAL_COMP_MODULE_ENABLED - #include "stm32f3xx_hal_comp.h" +#include "stm32f3xx_hal_comp.h" #endif /* HAL_COMP_MODULE_ENABLED */ #ifdef HAL_CRC_MODULE_ENABLED - #include "stm32f3xx_hal_crc.h" +#include "stm32f3xx_hal_crc.h" #endif /* HAL_CRC_MODULE_ENABLED */ #ifdef HAL_DAC_MODULE_ENABLED - #include "stm32f3xx_hal_dac.h" +#include "stm32f3xx_hal_dac.h" #endif /* HAL_DAC_MODULE_ENABLED */ #ifdef HAL_FLASH_MODULE_ENABLED - #include "stm32f3xx_hal_flash.h" +#include "stm32f3xx_hal_flash.h" #endif /* HAL_FLASH_MODULE_ENABLED */ #ifdef HAL_SRAM_MODULE_ENABLED - #include "stm32f3xx_hal_sram.h" +#include "stm32f3xx_hal_sram.h" #endif /* HAL_SRAM_MODULE_ENABLED */ #ifdef HAL_NOR_MODULE_ENABLED - #include "stm32f3xx_hal_nor.h" +#include "stm32f3xx_hal_nor.h" #endif /* HAL_NOR_MODULE_ENABLED */ #ifdef HAL_NAND_MODULE_ENABLED - #include "stm32f3xx_hal_nand.h" +#include "stm32f3xx_hal_nand.h" #endif /* HAL_NAND_MODULE_ENABLED */ #ifdef HAL_PCCARD_MODULE_ENABLED - #include "stm32f3xx_hal_pccard.h" -#endif /* HAL_PCCARD_MODULE_ENABLED */ +#include "stm32f3xx_hal_pccard.h" +#endif /* HAL_PCCARD_MODULE_ENABLED */ #ifdef HAL_HRTIM_MODULE_ENABLED - #include "stm32f3xx_hal_hrtim.h" +#include "stm32f3xx_hal_hrtim.h" #endif /* HAL_HRTIM_MODULE_ENABLED */ #ifdef HAL_I2C_MODULE_ENABLED - #include "stm32f3xx_hal_i2c.h" +#include "stm32f3xx_hal_i2c.h" #endif /* HAL_I2C_MODULE_ENABLED */ #ifdef HAL_I2S_MODULE_ENABLED - #include "stm32f3xx_hal_i2s.h" +#include "stm32f3xx_hal_i2s.h" #endif /* HAL_I2S_MODULE_ENABLED */ #ifdef HAL_IRDA_MODULE_ENABLED - #include "stm32f3xx_hal_irda.h" +#include "stm32f3xx_hal_irda.h" #endif /* HAL_IRDA_MODULE_ENABLED */ #ifdef HAL_IWDG_MODULE_ENABLED - #include "stm32f3xx_hal_iwdg.h" +#include "stm32f3xx_hal_iwdg.h" #endif /* HAL_IWDG_MODULE_ENABLED */ #ifdef HAL_OPAMP_MODULE_ENABLED - #include "stm32f3xx_hal_opamp.h" +#include "stm32f3xx_hal_opamp.h" #endif /* HAL_OPAMP_MODULE_ENABLED */ #ifdef HAL_PCD_MODULE_ENABLED - #include "stm32f3xx_hal_pcd.h" +#include "stm32f3xx_hal_pcd.h" #endif /* HAL_PCD_MODULE_ENABLED */ #ifdef HAL_PWR_MODULE_ENABLED - #include "stm32f3xx_hal_pwr.h" +#include "stm32f3xx_hal_pwr.h" #endif /* HAL_PWR_MODULE_ENABLED */ #ifdef HAL_RTC_MODULE_ENABLED - #include "stm32f3xx_hal_rtc.h" +#include "stm32f3xx_hal_rtc.h" #endif /* HAL_RTC_MODULE_ENABLED */ #ifdef HAL_SDADC_MODULE_ENABLED - #include "stm32f3xx_hal_sdadc.h" +#include "stm32f3xx_hal_sdadc.h" #endif /* HAL_SDADC_MODULE_ENABLED */ #ifdef HAL_SMARTCARD_MODULE_ENABLED - #include "stm32f3xx_hal_smartcard.h" +#include "stm32f3xx_hal_smartcard.h" #endif /* HAL_SMARTCARD_MODULE_ENABLED */ #ifdef HAL_SMBUS_MODULE_ENABLED - #include "stm32f3xx_hal_smbus.h" +#include "stm32f3xx_hal_smbus.h" #endif /* HAL_SMBUS_MODULE_ENABLED */ #ifdef HAL_SPI_MODULE_ENABLED - #include "stm32f3xx_hal_spi.h" +#include "stm32f3xx_hal_spi.h" #endif /* HAL_SPI_MODULE_ENABLED */ #ifdef HAL_TIM_MODULE_ENABLED - #include "stm32f3xx_hal_tim.h" +#include "stm32f3xx_hal_tim.h" #endif /* HAL_TIM_MODULE_ENABLED */ #ifdef HAL_TSC_MODULE_ENABLED - #include "stm32f3xx_hal_tsc.h" +#include "stm32f3xx_hal_tsc.h" #endif /* HAL_TSC_MODULE_ENABLED */ #ifdef HAL_UART_MODULE_ENABLED - #include "stm32f3xx_hal_uart.h" +#include "stm32f3xx_hal_uart.h" #endif /* HAL_UART_MODULE_ENABLED */ #ifdef HAL_USART_MODULE_ENABLED - #include "stm32f3xx_hal_usart.h" +#include "stm32f3xx_hal_usart.h" #endif /* HAL_USART_MODULE_ENABLED */ #ifdef HAL_WWDG_MODULE_ENABLED - #include "stm32f3xx_hal_wwdg.h" +#include "stm32f3xx_hal_wwdg.h" #endif /* HAL_WWDG_MODULE_ENABLED */ /* Exported macro ------------------------------------------------------------*/ -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** * @brief The assert_param macro is used for function's parameters check. * @param expr: If expr is false, it calls assert_failed function @@ -323,13 +323,13 @@ * If expr is true, it returns no value. * @retval None */ - #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) +#define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) /* Exported functions ------------------------------------------------------- */ - void assert_failed(uint8_t* file, uint32_t line); +void assert_failed(uint8_t *file, uint32_t line); #else - #define assert_param(expr) ((void)0) -#endif /* USE_FULL_ASSERT */ - +#define assert_param(expr) ((void)0) +#endif /* USE_FULL_ASSERT */ + #ifdef __cplusplus } #endif diff --git a/f3dfu/Inc/stm32f3xx_it.h b/f3dfu/Inc/stm32f3xx_it.h index e30758b1..560f793b 100644 --- a/f3dfu/Inc/stm32f3xx_it.h +++ b/f3dfu/Inc/stm32f3xx_it.h @@ -36,8 +36,8 @@ #define __STM32F3xx_IT_H #ifdef __cplusplus - extern "C" { -#endif +extern "C" { +#endif /* Includes ------------------------------------------------------------------*/ /* Exported types ------------------------------------------------------------*/ diff --git a/f3dfu/Inc/usb_device.h b/f3dfu/Inc/usb_device.h index 9ff674fe..3165412a 100644 --- a/f3dfu/Inc/usb_device.h +++ b/f3dfu/Inc/usb_device.h @@ -45,7 +45,7 @@ #ifndef __usb_device_H #define __usb_device_H #ifdef __cplusplus - extern "C" { +extern "C" { #endif /* Includes ------------------------------------------------------------------*/ @@ -55,7 +55,7 @@ extern USBD_HandleTypeDef hUsbDeviceFS; -/* USB_Device init function */ +/* USB_Device init function */ void MX_USB_DEVICE_Init(void); #ifdef __cplusplus diff --git a/f3dfu/Inc/usbd_conf.h b/f3dfu/Inc/usbd_conf.h index fa721a27..fe1e2818 100644 --- a/f3dfu/Inc/usbd_conf.h +++ b/f3dfu/Inc/usbd_conf.h @@ -45,7 +45,7 @@ #ifndef __USBD_CONF__H__ #define __USBD_CONF__H__ #ifdef __cplusplus - extern "C" { +extern "C" { #endif /* Includes ------------------------------------------------------------------*/ #include @@ -58,119 +58,121 @@ /** @addtogroup USBD_OTG_DRIVER * @{ */ - + /** @defgroup USBD_CONF * @brief usb otg low level driver configuration file * @{ - */ + */ /** @defgroup USBD_CONF_Exported_Defines * @{ - */ + */ /*---------- -----------*/ -#define USBD_MAX_NUM_INTERFACES 1 +#define USBD_MAX_NUM_INTERFACES 1 /*---------- -----------*/ -#define USBD_MAX_NUM_CONFIGURATION 1 +#define USBD_MAX_NUM_CONFIGURATION 1 /*---------- -----------*/ -#define USBD_MAX_STR_DESC_SIZ 512 +#define USBD_MAX_STR_DESC_SIZ 512 /*---------- -----------*/ -#define USBD_SUPPORT_USER_STRING 1 +#define USBD_SUPPORT_USER_STRING 1 /*---------- -----------*/ -#define USBD_DEBUG_LEVEL 0 +#define USBD_DEBUG_LEVEL 0 /*---------- -----------*/ -#define USBD_SELF_POWERED 1 +#define USBD_SELF_POWERED 1 /*---------- -----------*/ -#define USBD_DFU_MAX_ITF_NUM 1 +#define USBD_DFU_MAX_ITF_NUM 1 /*---------- -----------*/ -#define USBD_DFU_XFER_SIZE 1024 +#define USBD_DFU_XFER_SIZE 1024 /*---------- -----------*/ -#define USBD_DFU_APP_DEFAULT_ADD 0x08004000 +#define USBD_DFU_APP_DEFAULT_ADD 0x08004000 /****************************************/ /* #define for FS and HS identification */ -#define DEVICE_FS 0 +#define DEVICE_FS 0 /** @defgroup USBD_Exported_Macros * @{ - */ + */ -/* Memory management macros */ -#define USBD_malloc (uint32_t *)USBD_static_malloc -#define USBD_free USBD_static_free -#define USBD_memset /* Not used */ -#define USBD_memcpy /* Not used */ +/* Memory management macros */ +#define USBD_malloc (uint32_t *)USBD_static_malloc +#define USBD_free USBD_static_free +#define USBD_memset /* Not used */ +#define USBD_memcpy /* Not used */ -#define USBD_Delay HAL_Delay +#define USBD_Delay HAL_Delay /* For footprint reasons and since only one allocation is handled in the HID 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); +void USBD_static_free(void *p); -/* DEBUG macros */ -#if (USBD_DEBUG_LEVEL > 0) -#define USBD_UsrLog(...) printf(__VA_ARGS__);\ - printf("\n"); +/* DEBUG macros */ +#if(USBD_DEBUG_LEVEL > 0) +#define USBD_UsrLog(...) \ + printf(__VA_ARGS__); \ + printf("\n"); #else -#define USBD_UsrLog(...) -#endif - - -#if (USBD_DEBUG_LEVEL > 1) - -#define USBD_ErrLog(...) printf("ERROR: ") ;\ - printf(__VA_ARGS__);\ - printf("\n"); -#else -#define USBD_ErrLog(...) -#endif - - -#if (USBD_DEBUG_LEVEL > 2) -#define USBD_DbgLog(...) printf("DEBUG : ") ;\ - printf(__VA_ARGS__);\ - printf("\n"); -#else -#define USBD_DbgLog(...) +#define USBD_UsrLog(...) #endif - + + +#if(USBD_DEBUG_LEVEL > 1) + +#define USBD_ErrLog(...) \ + printf("ERROR: "); \ + printf(__VA_ARGS__); \ + printf("\n"); +#else +#define USBD_ErrLog(...) +#endif + + +#if(USBD_DEBUG_LEVEL > 2) +#define USBD_DbgLog(...) \ + printf("DEBUG : "); \ + printf(__VA_ARGS__); \ + printf("\n"); +#else +#define USBD_DbgLog(...) +#endif + /** * @} - */ - - - + */ + + /** * @} - */ + */ /** @defgroup USBD_CONF_Exported_Types * @{ - */ + */ /** * @} - */ + */ /** @defgroup USBD_CONF_Exported_Macros * @{ - */ + */ /** * @} - */ + */ /** @defgroup USBD_CONF_Exported_Variables * @{ - */ + */ /** * @} - */ + */ /** @defgroup USBD_CONF_Exported_FunctionsPrototype * @{ - */ + */ /** * @} - */ + */ #ifdef __cplusplus } #endif @@ -179,10 +181,9 @@ void USBD_static_free(void *p); /** * @} - */ + */ /** * @} - */ + */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - diff --git a/f3dfu/Inc/usbd_desc.h b/f3dfu/Inc/usbd_desc.h index d71cbb0c..1259557f 100644 --- a/f3dfu/Inc/usbd_desc.h +++ b/f3dfu/Inc/usbd_desc.h @@ -47,7 +47,7 @@ #define __USBD_DESC__H__ #ifdef __cplusplus - extern "C" { +extern "C" { #endif /* Includes ------------------------------------------------------------------*/ #include "usbd_def.h" @@ -55,11 +55,11 @@ /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY * @{ */ - + /** @defgroup USB_DESC * @brief general defines for the usb device library file * @{ - */ + */ /** @defgroup USB_DESC_Exported_Defines * @{ @@ -67,37 +67,37 @@ /** * @} - */ + */ /** @defgroup USBD_DESC_Exported_TypesDefinitions * @{ */ /** * @} - */ + */ /** @defgroup USBD_DESC_Exported_Macros * @{ - */ + */ /** * @} - */ + */ /** @defgroup USBD_DESC_Exported_Variables * @{ - */ + */ extern USBD_DescriptorsTypeDef FS_Desc; /** * @} - */ + */ /** @defgroup USBD_DESC_Exported_FunctionsPrototype * @{ - */ - + */ + /** * @} - */ + */ #ifdef __cplusplus } #endif @@ -106,9 +106,9 @@ extern USBD_DescriptorsTypeDef FS_Desc; /** * @} - */ + */ /** * @} -*/ +*/ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/f3dfu/Inc/usbd_dfu_if.h b/f3dfu/Inc/usbd_dfu_if.h index 13541b90..7c8da38d 100644 --- a/f3dfu/Inc/usbd_dfu_if.h +++ b/f3dfu/Inc/usbd_dfu_if.h @@ -46,7 +46,7 @@ #define __USBD_DFU_IF_H #ifdef __cplusplus - extern "C" { +extern "C" { #endif /* Includes ------------------------------------------------------------------*/ #include "usbd_dfu.h" @@ -56,72 +56,72 @@ /** @addtogroup STM32_USB_DEVICE_LIBRARY * @{ */ - + /** @defgroup USBD_MEDIA * @brief header file for the usbd_dfu_if.c file * @{ - */ + */ /** @defgroup USBD_MEDIA_Exported_Defines * @{ - */ + */ /* USER CODE BEGIN EXPORTED_DEFINES */ /* USER CODE END EXPORTED_DEFINES */ /** * @} - */ + */ /** @defgroup USBD_MEDIA_Exported_Types * @{ - */ + */ /* USER CODE BEGIN EXPORTED_TYPES */ /* USER CODE END EXPORTED_TYPES */ /** * @} - */ + */ /** @defgroup USBD_MEDIA_Exported_Macros * @{ - */ + */ /* USER CODE BEGIN EXPORTED_MACRO */ /* USER CODE END EXPORTED_MACRO */ /** * @} - */ + */ /** @defgroup USBD_MEDIA_Exported_Variables * @{ - */ + */ - extern USBD_DFU_MediaTypeDef USBD_DFU_fops_FS; +extern USBD_DFU_MediaTypeDef USBD_DFU_fops_FS; /* USER CODE BEGIN EXPORTED_VARIABLES */ /* USER CODE END EXPORTED_VARIABLES */ /** * @} - */ + */ /** @defgroup USBD_MEDIA_Exported_FunctionsPrototype * @{ - */ + */ /* USER CODE BEGIN EXPORTED_FUNCTIONS */ /* USER CODE END EXPORTED_FUNCTIONS */ /** * @} - */ + */ /** * @} - */ + */ /** * @} -*/ +*/ #ifdef __cplusplus } #endif diff --git a/f3dfu/Src/main.c b/f3dfu/Src/main.c index 6b416a68..30d3fb65 100644 --- a/f3dfu/Src/main.c +++ b/f3dfu/Src/main.c @@ -77,25 +77,22 @@ static void MX_RTC_Init(void); #define APP_END 0x08020000 #define APP_RANGE_VALID(a, s) (!(((a) | (s)) & 3) && (a) >= APP_START && ((a) + (s)) <= APP_END) #define VERSION_INFO_OFFSET 0x188 -static volatile const struct version_info *app_info = (void*)(APP_START + VERSION_INFO_OFFSET); +static volatile const struct version_info *app_info = (void *)(APP_START + VERSION_INFO_OFFSET); -static int app_ok(void) -{ - if (!APP_RANGE_VALID(APP_START, app_info->image_size)) { - return 0; - } - uint32_t crc = HAL_CRC_Calculate(&hcrc, (uint32_t *) APP_START, app_info->image_size / 4); +static int app_ok(void) { + if(!APP_RANGE_VALID(APP_START, app_info->image_size)) { + return 0; + } + uint32_t crc = HAL_CRC_Calculate(&hcrc, (uint32_t *)APP_START, app_info->image_size / 4); - if (crc != 0) { - return 0; - } - return 1; + if(crc != 0) { + return 0; + } + return 1; } /* USER CODE END 0 */ -int main(void) -{ - +int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ @@ -125,113 +122,106 @@ int main(void) MX_CRC_Init(); MX_RTC_Init(); -GPIO_InitTypeDef GPIO_InitStruct; + GPIO_InitTypeDef GPIO_InitStruct; #ifdef USB_DISCONNECT_PIN - GPIO_InitStruct.Pin = USB_DISCONNECT_PIN; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Pin = USB_DISCONNECT_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(USB_DISCONNECT_PORT, &GPIO_InitStruct); HAL_GPIO_WritePin(USB_DISCONNECT_PORT, USB_DISCONNECT_PIN, GPIO_PIN_RESET); #endif #ifdef USB_CONNECT_PIN - GPIO_InitStruct.Pin = USB_CONNECT_PIN; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Pin = USB_CONNECT_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(USB_CONNECT_PORT, &GPIO_InitStruct); HAL_GPIO_WritePin(USB_CONNECT_PORT, USB_CONNECT_PIN, GPIO_PIN_SET); #endif /* USER CODE BEGIN 2 */ - if(app_ok() && RTC->BKP0R == 0x00000000){ + if(app_ok() && RTC->BKP0R == 0x00000000) { // SCB->VTOR = APP_START; /* Jump to user application */ void (*JumpToApplication)(void); - uint32_t JumpAddress = *(__IO uint32_t*) (USBD_DFU_APP_DEFAULT_ADD + 4); - JumpToApplication = (void*)JumpAddress; + uint32_t JumpAddress = *(__IO uint32_t *)(USBD_DFU_APP_DEFAULT_ADD + 4); + JumpToApplication = (void *)JumpAddress; /* Initialize user application's Stack Pointer */ - __set_MSP(*(__IO uint32_t*) USBD_DFU_APP_DEFAULT_ADD); + __set_MSP(*(__IO uint32_t *)USBD_DFU_APP_DEFAULT_ADD); #ifdef USB_DISCONNECT_PIN HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET); HAL_Delay(100); #endif JumpToApplication(); - while (1); + while(1) + ; } RTC->BKP0R = 0x00000000; /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ - while (1) - { - /* USER CODE END WHILE */ + while(1) { + /* USER CODE END WHILE */ - /* USER CODE BEGIN 3 */ - HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET); - HAL_Delay(50); - HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET); - HAL_Delay(50); + /* USER CODE BEGIN 3 */ + HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET); + HAL_Delay(50); + HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET); + HAL_Delay(50); } /* USER CODE END 3 */ - } /** System Clock Configuration */ -void SystemClock_Config(void) -{ - +void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_PeriphCLKInitTypeDef PeriphClkInit; - /**Initializes the CPU, AHB and APB busses clocks + /**Initializes the CPU, AHB and APB busses clocks */ - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE; - RCC_OscInitStruct.HSEState = RCC_HSE_ON; + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_HSE; + RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; - RCC_OscInitStruct.HSIState = RCC_HSI_ON; - RCC_OscInitStruct.LSIState = RCC_LSI_ON; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; - RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; - RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) - { + RCC_OscInitStruct.HSIState = RCC_HSI_ON; + RCC_OscInitStruct.LSIState = RCC_LSI_ON; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; + RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; + if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } - /**Initializes the CPU, AHB and APB busses clocks + /**Initializes the CPU, AHB and APB busses clocks */ - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK - |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; - RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; - RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; - if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) - { + if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } - PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB|RCC_PERIPHCLK_RTC; - PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; - PeriphClkInit.USBClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5; - if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) - { + PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB | RCC_PERIPHCLK_RTC; + PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; + PeriphClkInit.USBClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5; + if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); } - /**Configure the Systick interrupt time + /**Configure the Systick interrupt time */ - HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); + HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000); - /**Configure the Systick + /**Configure the Systick */ HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); @@ -240,40 +230,32 @@ void SystemClock_Config(void) } /* CRC init function */ -static void MX_CRC_Init(void) -{ - - hcrc.Instance = CRC; - hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE; - hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE; - hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE; +static void MX_CRC_Init(void) { + hcrc.Instance = CRC; + hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE; + hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE; + hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE; hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE; - hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_WORDS; - if (HAL_CRC_Init(&hcrc) != HAL_OK) - { + hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_WORDS; + if(HAL_CRC_Init(&hcrc) != HAL_OK) { Error_Handler(); } - } /* RTC init function */ -static void MX_RTC_Init(void) -{ - - /**Initialize RTC Only +static void MX_RTC_Init(void) { + /**Initialize RTC Only */ - hrtc.Instance = RTC; - hrtc.Init.HourFormat = RTC_HOURFORMAT_24; - hrtc.Init.AsynchPrediv = 127; - hrtc.Init.SynchPrediv = 255; - hrtc.Init.OutPut = RTC_OUTPUT_DISABLE; + hrtc.Instance = RTC; + hrtc.Init.HourFormat = RTC_HOURFORMAT_24; + hrtc.Init.AsynchPrediv = 127; + hrtc.Init.SynchPrediv = 255; + hrtc.Init.OutPut = RTC_OUTPUT_DISABLE; hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; - hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; - if (HAL_RTC_Init(&hrtc) != HAL_OK) - { + hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; + if(HAL_RTC_Init(&hrtc) != HAL_OK) { Error_Handler(); } - } /** Configure pins as @@ -283,9 +265,7 @@ static void MX_RTC_Init(void) * EVENT_OUT * EXTI */ -static void MX_GPIO_Init(void) -{ - +static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct; /* GPIO Ports Clock Enable */ @@ -296,12 +276,11 @@ static void MX_GPIO_Init(void) HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET); /*Configure GPIO pin : LED_Pin */ - GPIO_InitStruct.Pin = LED_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Pin = LED_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct); - } /* USER CODE BEGIN 4 */ @@ -313,14 +292,12 @@ static void MX_GPIO_Init(void) * @param None * @retval None */ -void Error_Handler(void) -{ +void Error_Handler(void) { /* USER CODE BEGIN Error_Handler */ /* User can add his own implementation to report the HAL error return state */ - while(1) - { + while(1) { } - /* USER CODE END Error_Handler */ + /* USER CODE END Error_Handler */ } #ifdef USE_FULL_ASSERT @@ -332,23 +309,21 @@ void Error_Handler(void) * @param line: assert_param error line source number * @retval None */ -void assert_failed(uint8_t* file, uint32_t line) -{ +void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ - } #endif /** * @} - */ + */ /** * @} -*/ +*/ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/f3dfu/Src/stm32f3xx_hal_msp.c b/f3dfu/Src/stm32f3xx_hal_msp.c index cf9ce642..fd16ec38 100644 --- a/f3dfu/Src/stm32f3xx_hal_msp.c +++ b/f3dfu/Src/stm32f3xx_hal_msp.c @@ -51,8 +51,7 @@ extern void Error_Handler(void); /** * Initializes the Global MSP. */ -void HAL_MspInit(void) -{ +void HAL_MspInit(void) { /* USER CODE BEGIN MspInit 0 */ /* USER CODE END MspInit 0 */ @@ -82,72 +81,56 @@ void HAL_MspInit(void) /* USER CODE END MspInit 1 */ } -void HAL_CRC_MspInit(CRC_HandleTypeDef* hcrc) -{ +void HAL_CRC_MspInit(CRC_HandleTypeDef *hcrc) { + if(hcrc->Instance == CRC) { + /* USER CODE BEGIN CRC_MspInit 0 */ - if(hcrc->Instance==CRC) - { - /* USER CODE BEGIN CRC_MspInit 0 */ - - /* USER CODE END CRC_MspInit 0 */ + /* USER CODE END CRC_MspInit 0 */ /* Peripheral clock enable */ __HAL_RCC_CRC_CLK_ENABLE(); - /* USER CODE BEGIN CRC_MspInit 1 */ + /* USER CODE BEGIN CRC_MspInit 1 */ - /* USER CODE END CRC_MspInit 1 */ + /* USER CODE END CRC_MspInit 1 */ } - } -void HAL_CRC_MspDeInit(CRC_HandleTypeDef* hcrc) -{ +void HAL_CRC_MspDeInit(CRC_HandleTypeDef *hcrc) { + if(hcrc->Instance == CRC) { + /* USER CODE BEGIN CRC_MspDeInit 0 */ - if(hcrc->Instance==CRC) - { - /* USER CODE BEGIN CRC_MspDeInit 0 */ - - /* USER CODE END CRC_MspDeInit 0 */ + /* USER CODE END CRC_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_CRC_CLK_DISABLE(); } /* USER CODE BEGIN CRC_MspDeInit 1 */ /* USER CODE END CRC_MspDeInit 1 */ - } -void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc) -{ +void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc) { + if(hrtc->Instance == RTC) { + /* USER CODE BEGIN RTC_MspInit 0 */ - if(hrtc->Instance==RTC) - { - /* USER CODE BEGIN RTC_MspInit 0 */ - - /* USER CODE END RTC_MspInit 0 */ + /* USER CODE END RTC_MspInit 0 */ /* Peripheral clock enable */ __HAL_RCC_RTC_ENABLE(); - /* USER CODE BEGIN RTC_MspInit 1 */ + /* USER CODE BEGIN RTC_MspInit 1 */ - /* USER CODE END RTC_MspInit 1 */ + /* USER CODE END RTC_MspInit 1 */ } - } -void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc) -{ +void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc) { + if(hrtc->Instance == RTC) { + /* USER CODE BEGIN RTC_MspDeInit 0 */ - if(hrtc->Instance==RTC) - { - /* USER CODE BEGIN RTC_MspDeInit 0 */ - - /* USER CODE END RTC_MspDeInit 0 */ + /* USER CODE END RTC_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_RTC_DISABLE(); } /* USER CODE BEGIN RTC_MspDeInit 1 */ /* USER CODE END RTC_MspDeInit 1 */ - } /* USER CODE BEGIN 1 */ diff --git a/f3dfu/Src/stm32f3xx_it.c b/f3dfu/Src/stm32f3xx_it.c index 0953dd25..caf16ede 100644 --- a/f3dfu/Src/stm32f3xx_it.c +++ b/f3dfu/Src/stm32f3xx_it.c @@ -43,14 +43,13 @@ extern PCD_HandleTypeDef hpcd_USB_FS; /******************************************************************************/ -/* Cortex-M4 Processor Interruption and Exception Handlers */ +/* Cortex-M4 Processor Interruption and Exception Handlers */ /******************************************************************************/ /** * @brief This function handles Non maskable interrupt. */ -void NMI_Handler(void) -{ +void NMI_Handler(void) { /* USER CODE BEGIN NonMaskableInt_IRQn 0 */ /* USER CODE END NonMaskableInt_IRQn 0 */ @@ -62,13 +61,11 @@ void NMI_Handler(void) /** * @brief This function handles Hard fault interrupt. */ -void HardFault_Handler(void) -{ +void HardFault_Handler(void) { /* USER CODE BEGIN HardFault_IRQn 0 */ /* USER CODE END HardFault_IRQn 0 */ - while (1) - { + while(1) { } /* USER CODE BEGIN HardFault_IRQn 1 */ @@ -78,13 +75,11 @@ void HardFault_Handler(void) /** * @brief This function handles Memory management fault. */ -void MemManage_Handler(void) -{ +void MemManage_Handler(void) { /* USER CODE BEGIN MemoryManagement_IRQn 0 */ /* USER CODE END MemoryManagement_IRQn 0 */ - while (1) - { + while(1) { } /* USER CODE BEGIN MemoryManagement_IRQn 1 */ @@ -94,13 +89,11 @@ void MemManage_Handler(void) /** * @brief This function handles Pre-fetch fault, memory access fault. */ -void BusFault_Handler(void) -{ +void BusFault_Handler(void) { /* USER CODE BEGIN BusFault_IRQn 0 */ /* USER CODE END BusFault_IRQn 0 */ - while (1) - { + while(1) { } /* USER CODE BEGIN BusFault_IRQn 1 */ @@ -110,13 +103,11 @@ void BusFault_Handler(void) /** * @brief This function handles Undefined instruction or illegal state. */ -void UsageFault_Handler(void) -{ +void UsageFault_Handler(void) { /* USER CODE BEGIN UsageFault_IRQn 0 */ /* USER CODE END UsageFault_IRQn 0 */ - while (1) - { + while(1) { } /* USER CODE BEGIN UsageFault_IRQn 1 */ @@ -126,8 +117,7 @@ void UsageFault_Handler(void) /** * @brief This function handles System service call via SWI instruction. */ -void SVC_Handler(void) -{ +void SVC_Handler(void) { /* USER CODE BEGIN SVCall_IRQn 0 */ /* USER CODE END SVCall_IRQn 0 */ @@ -139,8 +129,7 @@ void SVC_Handler(void) /** * @brief This function handles Debug monitor. */ -void DebugMon_Handler(void) -{ +void DebugMon_Handler(void) { /* USER CODE BEGIN DebugMonitor_IRQn 0 */ /* USER CODE END DebugMonitor_IRQn 0 */ @@ -152,8 +141,7 @@ void DebugMon_Handler(void) /** * @brief This function handles Pendable request for system service. */ -void PendSV_Handler(void) -{ +void PendSV_Handler(void) { /* USER CODE BEGIN PendSV_IRQn 0 */ /* USER CODE END PendSV_IRQn 0 */ @@ -165,8 +153,7 @@ void PendSV_Handler(void) /** * @brief This function handles System tick timer. */ -void SysTick_Handler(void) -{ +void SysTick_Handler(void) { /* USER CODE BEGIN SysTick_IRQn 0 */ /* USER CODE END SysTick_IRQn 0 */ @@ -187,8 +174,7 @@ void SysTick_Handler(void) /** * @brief This function handles USB low priority or CAN_RX0 interrupts. */ -void USB_LP_CAN_RX0_IRQHandler(void) -{ +void USB_LP_CAN_RX0_IRQHandler(void) { /* USER CODE BEGIN USB_LP_CAN_RX0_IRQn 0 */ /* USER CODE END USB_LP_CAN_RX0_IRQn 0 */ diff --git a/f3dfu/Src/system_stm32f3xx.c b/f3dfu/Src/system_stm32f3xx.c index 841006c5..1868a5e6 100644 --- a/f3dfu/Src/system_stm32f3xx.c +++ b/f3dfu/Src/system_stm32f3xx.c @@ -102,21 +102,21 @@ /** @addtogroup STM32F3xx_System_Private_Defines * @{ */ -#if !defined (HSE_VALUE) - #define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz. - This value can be provided and adapted by the user application. */ +#if !defined(HSE_VALUE) +#define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz. \ + This value can be provided and adapted by the user application. */ #endif /* HSE_VALUE */ -#if !defined (HSI_VALUE) - #define HSI_VALUE ((uint32_t)8000000) /*!< Default value of the Internal oscillator in Hz. - This value can be provided and adapted by the user application. */ +#if !defined(HSI_VALUE) +#define HSI_VALUE ((uint32_t)8000000) /*!< Default value of the Internal oscillator in Hz. \ + This value can be provided and adapted by the user application. */ #endif /* HSI_VALUE */ /*!< Uncomment the following line if you need to relocate your vector Table in Internal SRAM. */ /* #define VECT_TAB_SRAM */ -#define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field. - This value must be a multiple of 0x200. */ +#define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field. \ + This value must be a multiple of 0x200. */ /** * @} */ @@ -132,7 +132,7 @@ /** @addtogroup STM32F3xx_System_Private_Variables * @{ */ - /* This variable is updated in three ways: +/* This variable is updated in three ways: 1) by calling CMSIS function SystemCoreClockUpdate() 2) by calling HAL API function HAL_RCC_GetHCLKFreq() 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency @@ -167,12 +167,11 @@ const uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4}; * @param None * @retval None */ -void SystemInit(void) -{ - /* FPU settings ------------------------------------------------------------*/ - #if (__FPU_PRESENT == 1) && (__FPU_USED == 1) - SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */ - #endif +void SystemInit(void) { +/* FPU settings ------------------------------------------------------------*/ +#if(__FPU_PRESENT == 1) && (__FPU_USED == 1) + SCB->CPACR |= ((3UL << 10 * 2) | (3UL << 11 * 2)); /* set CP10 and CP11 Full Access */ +#endif /* Reset the RCC clock configuration to the default reset state ------------*/ /* Set HSION bit */ @@ -242,47 +241,39 @@ void SystemInit(void) * @param None * @retval None */ -void SystemCoreClockUpdate (void) -{ +void SystemCoreClockUpdate(void) { uint32_t tmp = 0, pllmull = 0, pllsource = 0, predivfactor = 0; /* Get SYSCLK source -------------------------------------------------------*/ tmp = RCC->CFGR & RCC_CFGR_SWS; - switch (tmp) - { - case RCC_CFGR_SWS_HSI: /* HSI used as system clock */ + switch(tmp) { + case RCC_CFGR_SWS_HSI: /* HSI used as system clock */ SystemCoreClock = HSI_VALUE; break; - case RCC_CFGR_SWS_HSE: /* HSE used as system clock */ + case RCC_CFGR_SWS_HSE: /* HSE used as system clock */ SystemCoreClock = HSE_VALUE; break; - case RCC_CFGR_SWS_PLL: /* PLL used as system clock */ + case RCC_CFGR_SWS_PLL: /* PLL used as system clock */ /* Get PLL clock source and multiplication factor ----------------------*/ - pllmull = RCC->CFGR & RCC_CFGR_PLLMUL; + pllmull = RCC->CFGR & RCC_CFGR_PLLMUL; pllsource = RCC->CFGR & RCC_CFGR_PLLSRC; - pllmull = ( pllmull >> 18) + 2; + pllmull = (pllmull >> 18) + 2; -#if defined (STM32F302xE) || defined (STM32F303xE) || defined (STM32F398xx) - predivfactor = (RCC->CFGR2 & RCC_CFGR2_PREDIV) + 1; - if (pllsource == RCC_CFGR_PLLSRC_HSE_PREDIV) - { +#if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) + predivfactor = (RCC->CFGR2 & RCC_CFGR2_PREDIV) + 1; + if(pllsource == RCC_CFGR_PLLSRC_HSE_PREDIV) { /* HSE oscillator clock selected as PREDIV1 clock entry */ SystemCoreClock = (HSE_VALUE / predivfactor) * pllmull; - } - else - { + } else { /* HSI oscillator clock selected as PREDIV1 clock entry */ SystemCoreClock = (HSI_VALUE / predivfactor) * pllmull; } -#else - if (pllsource == RCC_CFGR_PLLSRC_HSI_DIV2) - { +#else + if(pllsource == RCC_CFGR_PLLSRC_HSI_DIV2) { /* HSI oscillator clock divided by 2 selected as PLL clock entry */ SystemCoreClock = (HSI_VALUE >> 1) * pllmull; - } - else - { + } else { predivfactor = (RCC->CFGR2 & RCC_CFGR2_PREDIV) + 1; /* HSE oscillator clock selected as PREDIV1 clock entry */ SystemCoreClock = (HSE_VALUE / predivfactor) * pllmull; @@ -313,4 +304,3 @@ void SystemCoreClockUpdate (void) */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - diff --git a/f3dfu/Src/usb_device.c b/f3dfu/Src/usb_device.c index 8da88975..85864327 100644 --- a/f3dfu/Src/usb_device.c +++ b/f3dfu/Src/usb_device.c @@ -53,9 +53,8 @@ /* USB Device Core handle declaration */ USBD_HandleTypeDef hUsbDeviceFS; -/* init function */ -void MX_USB_DEVICE_Init(void) -{ +/* init function */ +void MX_USB_DEVICE_Init(void) { /* Init Device Library,Add Supported Class and Start the library*/ USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS); @@ -64,7 +63,6 @@ void MX_USB_DEVICE_Init(void) USBD_DFU_RegisterMedia(&hUsbDeviceFS, &USBD_DFU_fops_FS); USBD_Start(&hUsbDeviceFS); - } /** * @} diff --git a/f3dfu/Src/usbd_conf.c b/f3dfu/Src/usbd_conf.c index 6a0e84aa..1f2cbcad 100644 --- a/f3dfu/Src/usbd_conf.c +++ b/f3dfu/Src/usbd_conf.c @@ -68,23 +68,21 @@ void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state); *******************************************************************************/ /* MSP Init */ -void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle) -{ +void HAL_PCD_MspInit(PCD_HandleTypeDef *pcdHandle) { GPIO_InitTypeDef GPIO_InitStruct; - if(pcdHandle->Instance==USB) - { - /* USER CODE BEGIN USB_MspInit 0 */ + if(pcdHandle->Instance == USB) { + /* USER CODE BEGIN USB_MspInit 0 */ + + /* USER CODE END USB_MspInit 0 */ - /* USER CODE END USB_MspInit 0 */ - /**USB GPIO Configuration PA11 ------> USB_DM PA12 ------> USB_DP */ - GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Alternate = GPIO_AF14_USB; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); @@ -94,34 +92,32 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle) /* Peripheral interrupt init */ HAL_NVIC_SetPriority(USB_LP_CAN_RX0_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USB_LP_CAN_RX0_IRQn); - /* USER CODE BEGIN USB_MspInit 1 */ + /* USER CODE BEGIN USB_MspInit 1 */ - /* USER CODE END USB_MspInit 1 */ + /* USER CODE END USB_MspInit 1 */ } } -void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle) -{ - if(pcdHandle->Instance==USB) - { - /* USER CODE BEGIN USB_MspDeInit 0 */ +void HAL_PCD_MspDeInit(PCD_HandleTypeDef *pcdHandle) { + if(pcdHandle->Instance == USB) { + /* USER CODE BEGIN USB_MspDeInit 0 */ - /* USER CODE END USB_MspDeInit 0 */ + /* USER CODE END USB_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_USB_CLK_DISABLE(); - + /**USB GPIO Configuration PA11 ------> USB_DM PA12 ------> USB_DP */ - HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12); + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11 | GPIO_PIN_12); /* Peripheral interrupt Deinit*/ HAL_NVIC_DisableIRQ(USB_LP_CAN_RX0_IRQn); - /* USER CODE BEGIN USB_MspDeInit 1 */ + /* USER CODE BEGIN USB_MspDeInit 1 */ - /* USER CODE END USB_MspDeInit 1 */ + /* USER CODE END USB_MspDeInit 1 */ } } @@ -130,9 +126,8 @@ void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle) * @param hpcd: PCD handle * @retval None */ -void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd) -{ - USBD_LL_SetupStage((USBD_HandleTypeDef*)hpcd->pData, (uint8_t *)hpcd->Setup); +void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd) { + USBD_LL_SetupStage((USBD_HandleTypeDef *)hpcd->pData, (uint8_t *)hpcd->Setup); } /** @@ -141,9 +136,8 @@ void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd) * @param epnum: Endpoint Number * @retval None */ -void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) -{ - USBD_LL_DataOutStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff); +void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) { + USBD_LL_DataOutStage((USBD_HandleTypeDef *)hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff); } /** @@ -152,9 +146,8 @@ void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) * @param epnum: Endpoint Number * @retval None */ -void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) -{ - USBD_LL_DataInStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff); +void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) { + USBD_LL_DataInStage((USBD_HandleTypeDef *)hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff); } /** @@ -162,9 +155,8 @@ void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) * @param hpcd: PCD handle * @retval None */ -void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) -{ - USBD_LL_SOF((USBD_HandleTypeDef*)hpcd->pData); +void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) { + USBD_LL_SOF((USBD_HandleTypeDef *)hpcd->pData); } /** @@ -172,25 +164,23 @@ void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) * @param hpcd: PCD handle * @retval None */ -void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd) -{ +void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd) { USBD_SpeedTypeDef speed = USBD_SPEED_FULL; /*Set USB Current Speed*/ - switch (hpcd->Init.speed) - { - case PCD_SPEED_FULL: - speed = USBD_SPEED_FULL; - break; - - default: - speed = USBD_SPEED_FULL; - break; + switch(hpcd->Init.speed) { + case PCD_SPEED_FULL: + speed = USBD_SPEED_FULL; + break; + + default: + speed = USBD_SPEED_FULL; + break; } - USBD_LL_SetSpeed((USBD_HandleTypeDef*)hpcd->pData, speed); - + USBD_LL_SetSpeed((USBD_HandleTypeDef *)hpcd->pData, speed); + /*Reset Device*/ - USBD_LL_Reset((USBD_HandleTypeDef*)hpcd->pData); + USBD_LL_Reset((USBD_HandleTypeDef *)hpcd->pData); } /** @@ -199,14 +189,12 @@ void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd) * @param hpcd: PCD handle * @retval None */ -void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd) -{ +void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd) { /* Inform USB library that core enters in suspend Mode */ - USBD_LL_Suspend((USBD_HandleTypeDef*)hpcd->pData); + USBD_LL_Suspend((USBD_HandleTypeDef *)hpcd->pData); /*Enter in STOP mode */ /* USER CODE BEGIN 2 */ - if (hpcd->Init.low_power_enable) - { + 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)); } @@ -219,13 +207,11 @@ void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd) * @param hpcd: PCD handle * @retval None */ -void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd) -{ +void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd) { /* USER CODE BEGIN 3 */ /* USER CODE END 3 */ - USBD_LL_Resume((USBD_HandleTypeDef*)hpcd->pData); - + USBD_LL_Resume((USBD_HandleTypeDef *)hpcd->pData); } /** @@ -234,9 +220,8 @@ void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd) * @param epnum: Endpoint Number * @retval None */ -void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) -{ - USBD_LL_IsoOUTIncomplete((USBD_HandleTypeDef*)hpcd->pData, epnum); +void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) { + USBD_LL_IsoOUTIncomplete((USBD_HandleTypeDef *)hpcd->pData, epnum); } /** @@ -245,9 +230,8 @@ void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) * @param epnum: Endpoint Number * @retval None */ -void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) -{ - USBD_LL_IsoINIncomplete((USBD_HandleTypeDef*)hpcd->pData, epnum); +void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) { + USBD_LL_IsoINIncomplete((USBD_HandleTypeDef *)hpcd->pData, epnum); } /** @@ -255,9 +239,8 @@ void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) * @param hpcd: PCD handle * @retval None */ -void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd) -{ - USBD_LL_DevConnected((USBD_HandleTypeDef*)hpcd->pData); +void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd) { + USBD_LL_DevConnected((USBD_HandleTypeDef *)hpcd->pData); } /** @@ -265,9 +248,8 @@ void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd) * @param hpcd: PCD handle * @retval None */ -void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd) -{ - USBD_LL_DevDisconnected((USBD_HandleTypeDef*)hpcd->pData); +void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd) { + USBD_LL_DevDisconnected((USBD_HandleTypeDef *)hpcd->pData); } /******************************************************************************* @@ -278,27 +260,25 @@ void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd) * @param pdev: Device handle * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef *pdev) -{ +USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev) { /* Init USB_IP */ /* Link The driver to the stack */ hpcd_USB_FS.pData = pdev; - pdev->pData = &hpcd_USB_FS; + pdev->pData = &hpcd_USB_FS; - hpcd_USB_FS.Instance = USB; - hpcd_USB_FS.Init.dev_endpoints = 8; - hpcd_USB_FS.Init.speed = PCD_SPEED_FULL; - hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_64; - hpcd_USB_FS.Init.phy_itface = PCD_PHY_EMBEDDED; - hpcd_USB_FS.Init.low_power_enable = DISABLE; + hpcd_USB_FS.Instance = USB; + hpcd_USB_FS.Init.dev_endpoints = 8; + hpcd_USB_FS.Init.speed = PCD_SPEED_FULL; + hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_64; + hpcd_USB_FS.Init.phy_itface = PCD_PHY_EMBEDDED; + hpcd_USB_FS.Init.low_power_enable = DISABLE; hpcd_USB_FS.Init.battery_charging_enable = DISABLE; - if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK) - { + if(HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK) { Error_Handler(); } - HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x00 , PCD_SNG_BUF, 0x18); - HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x80 , PCD_SNG_BUF, 0x58); + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef *)pdev->pData, 0x00, PCD_SNG_BUF, 0x18); + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef *)pdev->pData, 0x80, PCD_SNG_BUF, 0x58); return USBD_OK; } @@ -307,31 +287,30 @@ USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef *pdev) * @param pdev: Device handle * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_DeInit (USBD_HandleTypeDef *pdev) -{ - HAL_StatusTypeDef hal_status = HAL_OK; +USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev) { + HAL_StatusTypeDef hal_status = HAL_OK; USBD_StatusTypeDef usb_status = USBD_OK; - + hal_status = HAL_PCD_DeInit(pdev->pData); - - switch (hal_status) { - case HAL_OK : + + switch(hal_status) { + case HAL_OK: usb_status = USBD_OK; - break; - case HAL_ERROR : + break; + case HAL_ERROR: usb_status = USBD_FAIL; - break; - case HAL_BUSY : + break; + case HAL_BUSY: usb_status = USBD_BUSY; - break; - case HAL_TIMEOUT : + break; + case HAL_TIMEOUT: usb_status = USBD_FAIL; - break; - default : + break; + default: usb_status = USBD_FAIL; - break; + break; } - return usb_status; + return usb_status; } /** @@ -339,31 +318,30 @@ USBD_StatusTypeDef USBD_LL_DeInit (USBD_HandleTypeDef *pdev) * @param pdev: Device handle * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev) -{ - HAL_StatusTypeDef hal_status = HAL_OK; +USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev) { + HAL_StatusTypeDef hal_status = HAL_OK; USBD_StatusTypeDef usb_status = USBD_OK; - + hal_status = HAL_PCD_Start(pdev->pData); - - switch (hal_status) { - case HAL_OK : + + switch(hal_status) { + case HAL_OK: usb_status = USBD_OK; - break; - case HAL_ERROR : + break; + case HAL_ERROR: usb_status = USBD_FAIL; - break; - case HAL_BUSY : + break; + case HAL_BUSY: usb_status = USBD_BUSY; - break; - case HAL_TIMEOUT : + break; + case HAL_TIMEOUT: usb_status = USBD_FAIL; - break; - default : + break; + default: usb_status = USBD_FAIL; - break; + break; } - return usb_status; + return usb_status; } /** @@ -371,31 +349,30 @@ USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev) * @param pdev: Device handle * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_Stop (USBD_HandleTypeDef *pdev) -{ - HAL_StatusTypeDef hal_status = HAL_OK; +USBD_StatusTypeDef USBD_LL_Stop(USBD_HandleTypeDef *pdev) { + HAL_StatusTypeDef hal_status = HAL_OK; USBD_StatusTypeDef usb_status = USBD_OK; - + hal_status = HAL_PCD_Stop(pdev->pData); - - switch (hal_status) { - case HAL_OK : + + switch(hal_status) { + case HAL_OK: usb_status = USBD_OK; - break; - case HAL_ERROR : + break; + case HAL_ERROR: usb_status = USBD_FAIL; - break; - case HAL_BUSY : + break; + case HAL_BUSY: usb_status = USBD_BUSY; - break; - case HAL_TIMEOUT : + break; + case HAL_TIMEOUT: usb_status = USBD_FAIL; - break; - default : + break; + default: usb_status = USBD_FAIL; - break; + break; } - return usb_status; + return usb_status; } /** @@ -406,38 +383,37 @@ USBD_StatusTypeDef USBD_LL_Stop (USBD_HandleTypeDef *pdev) * @param ep_mps: Endpoint Max Packet Size * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_OpenEP (USBD_HandleTypeDef *pdev, - uint8_t ep_addr, - uint8_t ep_type, - uint16_t ep_mps) -{ - HAL_StatusTypeDef hal_status = HAL_OK; +USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev, + uint8_t ep_addr, + uint8_t ep_type, + uint16_t ep_mps) { + HAL_StatusTypeDef hal_status = HAL_OK; USBD_StatusTypeDef usb_status = USBD_OK; - hal_status = HAL_PCD_EP_Open(pdev->pData, - ep_addr, - ep_mps, + hal_status = HAL_PCD_EP_Open(pdev->pData, + ep_addr, + ep_mps, ep_type); - - - switch (hal_status) { - case HAL_OK : + + + switch(hal_status) { + case HAL_OK: usb_status = USBD_OK; - break; - case HAL_ERROR : + break; + case HAL_ERROR: usb_status = USBD_FAIL; - break; - case HAL_BUSY : + break; + case HAL_BUSY: usb_status = USBD_BUSY; - break; - case HAL_TIMEOUT : + break; + case HAL_TIMEOUT: usb_status = USBD_FAIL; - break; - default : + break; + default: usb_status = USBD_FAIL; - break; + break; } - return usb_status; + return usb_status; } /** @@ -446,31 +422,30 @@ USBD_StatusTypeDef USBD_LL_OpenEP (USBD_HandleTypeDef *pdev, * @param ep_addr: Endpoint Number * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_CloseEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) -{ - HAL_StatusTypeDef hal_status = HAL_OK; +USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) { + HAL_StatusTypeDef hal_status = HAL_OK; USBD_StatusTypeDef usb_status = USBD_OK; - + hal_status = HAL_PCD_EP_Close(pdev->pData, ep_addr); - - switch (hal_status) { - case HAL_OK : + + switch(hal_status) { + case HAL_OK: usb_status = USBD_OK; - break; - case HAL_ERROR : + break; + case HAL_ERROR: usb_status = USBD_FAIL; - break; - case HAL_BUSY : + break; + case HAL_BUSY: usb_status = USBD_BUSY; - break; - case HAL_TIMEOUT : + break; + case HAL_TIMEOUT: usb_status = USBD_FAIL; - break; - default : + break; + default: usb_status = USBD_FAIL; - break; + break; } - return usb_status; + return usb_status; } /** @@ -479,31 +454,30 @@ USBD_StatusTypeDef USBD_LL_CloseEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) * @param ep_addr: Endpoint Number * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_FlushEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) -{ - HAL_StatusTypeDef hal_status = HAL_OK; +USBD_StatusTypeDef USBD_LL_FlushEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) { + HAL_StatusTypeDef hal_status = HAL_OK; USBD_StatusTypeDef usb_status = USBD_OK; - + hal_status = HAL_PCD_EP_Flush(pdev->pData, ep_addr); - - switch (hal_status) { - case HAL_OK : + + switch(hal_status) { + case HAL_OK: usb_status = USBD_OK; - break; - case HAL_ERROR : + break; + case HAL_ERROR: usb_status = USBD_FAIL; - break; - case HAL_BUSY : + break; + case HAL_BUSY: usb_status = USBD_BUSY; - break; - case HAL_TIMEOUT : + break; + case HAL_TIMEOUT: usb_status = USBD_FAIL; - break; - default : + break; + default: usb_status = USBD_FAIL; - break; + break; } - return usb_status; + return usb_status; } /** @@ -512,31 +486,30 @@ USBD_StatusTypeDef USBD_LL_FlushEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) * @param ep_addr: Endpoint Number * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_StallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) -{ - HAL_StatusTypeDef hal_status = HAL_OK; +USBD_StatusTypeDef USBD_LL_StallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) { + HAL_StatusTypeDef hal_status = HAL_OK; USBD_StatusTypeDef usb_status = USBD_OK; - + hal_status = HAL_PCD_EP_SetStall(pdev->pData, ep_addr); - - switch (hal_status) { - case HAL_OK : + + switch(hal_status) { + case HAL_OK: usb_status = USBD_OK; - break; - case HAL_ERROR : + break; + case HAL_ERROR: usb_status = USBD_FAIL; - break; - case HAL_BUSY : + break; + case HAL_BUSY: usb_status = USBD_BUSY; - break; - case HAL_TIMEOUT : + break; + case HAL_TIMEOUT: usb_status = USBD_FAIL; - break; - default : + break; + default: usb_status = USBD_FAIL; - break; + break; } - return usb_status; + return usb_status; } /** @@ -545,31 +518,30 @@ USBD_StatusTypeDef USBD_LL_StallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) * @param ep_addr: Endpoint Number * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_ClearStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) -{ - HAL_StatusTypeDef hal_status = HAL_OK; +USBD_StatusTypeDef USBD_LL_ClearStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) { + HAL_StatusTypeDef hal_status = HAL_OK; USBD_StatusTypeDef usb_status = USBD_OK; - - hal_status = HAL_PCD_EP_ClrStall(pdev->pData, ep_addr); - - switch (hal_status) { - case HAL_OK : + + hal_status = HAL_PCD_EP_ClrStall(pdev->pData, ep_addr); + + switch(hal_status) { + case HAL_OK: usb_status = USBD_OK; - break; - case HAL_ERROR : + break; + case HAL_ERROR: usb_status = USBD_FAIL; - break; - case HAL_BUSY : + break; + case HAL_BUSY: usb_status = USBD_BUSY; - break; - case HAL_TIMEOUT : + break; + case HAL_TIMEOUT: usb_status = USBD_FAIL; - break; - default : + break; + default: usb_status = USBD_FAIL; - break; + break; } - return usb_status; + return usb_status; } /** @@ -578,17 +550,13 @@ USBD_StatusTypeDef USBD_LL_ClearStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_a * @param ep_addr: Endpoint Number * @retval Stall (1: Yes, 0: No) */ -uint8_t USBD_LL_IsStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) -{ - PCD_HandleTypeDef *hpcd = (PCD_HandleTypeDef*) pdev->pData; - - if((ep_addr & 0x80) == 0x80) - { - return hpcd->IN_ep[ep_addr & 0x7F].is_stall; - } - else - { - return hpcd->OUT_ep[ep_addr & 0x7F].is_stall; +uint8_t USBD_LL_IsStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) { + PCD_HandleTypeDef *hpcd = (PCD_HandleTypeDef *)pdev->pData; + + if((ep_addr & 0x80) == 0x80) { + return hpcd->IN_ep[ep_addr & 0x7F].is_stall; + } else { + return hpcd->OUT_ep[ep_addr & 0x7F].is_stall; } } /** @@ -597,31 +565,30 @@ uint8_t USBD_LL_IsStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) * @param ep_addr: Endpoint Number * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_SetUSBAddress (USBD_HandleTypeDef *pdev, uint8_t dev_addr) -{ - HAL_StatusTypeDef hal_status = HAL_OK; +USBD_StatusTypeDef USBD_LL_SetUSBAddress(USBD_HandleTypeDef *pdev, uint8_t dev_addr) { + HAL_StatusTypeDef hal_status = HAL_OK; USBD_StatusTypeDef usb_status = USBD_OK; - + hal_status = HAL_PCD_SetAddress(pdev->pData, dev_addr); - - switch (hal_status) { - case HAL_OK : + + switch(hal_status) { + case HAL_OK: usb_status = USBD_OK; - break; - case HAL_ERROR : + break; + case HAL_ERROR: usb_status = USBD_FAIL; - break; - case HAL_BUSY : + break; + case HAL_BUSY: usb_status = USBD_BUSY; - break; - case HAL_TIMEOUT : + break; + case HAL_TIMEOUT: usb_status = USBD_FAIL; - break; - default : + break; + default: usb_status = USBD_FAIL; - break; + break; } - return usb_status; + return usb_status; } /** @@ -632,34 +599,33 @@ USBD_StatusTypeDef USBD_LL_SetUSBAddress (USBD_HandleTypeDef *pdev, uint8_t dev * @param size: Data size * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_Transmit (USBD_HandleTypeDef *pdev, - uint8_t ep_addr, - uint8_t *pbuf, - uint16_t size) -{ - HAL_StatusTypeDef hal_status = HAL_OK; +USBD_StatusTypeDef USBD_LL_Transmit(USBD_HandleTypeDef *pdev, + uint8_t ep_addr, + uint8_t *pbuf, + uint16_t size) { + HAL_StatusTypeDef hal_status = HAL_OK; USBD_StatusTypeDef usb_status = USBD_OK; hal_status = HAL_PCD_EP_Transmit(pdev->pData, ep_addr, pbuf, size); - - switch (hal_status) { - case HAL_OK : + + switch(hal_status) { + case HAL_OK: usb_status = USBD_OK; - break; - case HAL_ERROR : + break; + case HAL_ERROR: usb_status = USBD_FAIL; - break; - case HAL_BUSY : + break; + case HAL_BUSY: usb_status = USBD_BUSY; - break; - case HAL_TIMEOUT : + break; + case HAL_TIMEOUT: usb_status = USBD_FAIL; - break; - default : + break; + default: usb_status = USBD_FAIL; - break; + break; } - return usb_status; + return usb_status; } /** @@ -670,34 +636,33 @@ USBD_StatusTypeDef USBD_LL_Transmit (USBD_HandleTypeDef *pdev, * @param size: Data size * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, - uint8_t ep_addr, - uint8_t *pbuf, - uint16_t size) -{ - HAL_StatusTypeDef hal_status = HAL_OK; +USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, + uint8_t ep_addr, + uint8_t *pbuf, + uint16_t size) { + HAL_StatusTypeDef hal_status = HAL_OK; USBD_StatusTypeDef usb_status = USBD_OK; hal_status = HAL_PCD_EP_Receive(pdev->pData, ep_addr, pbuf, size); - - switch (hal_status) { - case HAL_OK : + + switch(hal_status) { + case HAL_OK: usb_status = USBD_OK; - break; - case HAL_ERROR : + break; + case HAL_ERROR: usb_status = USBD_FAIL; - break; - case HAL_BUSY : + break; + case HAL_BUSY: usb_status = USBD_BUSY; - break; - case HAL_TIMEOUT : + break; + case HAL_TIMEOUT: usb_status = USBD_FAIL; - break; - default : + break; + default: usb_status = USBD_FAIL; - break; + break; } - return usb_status; + return usb_status; } /** @@ -706,9 +671,8 @@ USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, * @param ep_addr: Endpoint Number * @retval Recived Data Size */ -uint32_t USBD_LL_GetRxDataSize (USBD_HandleTypeDef *pdev, uint8_t ep_addr) -{ - return HAL_PCD_EP_GetRxCount((PCD_HandleTypeDef*) pdev->pData, ep_addr); +uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr) { + return HAL_PCD_EP_GetRxCount((PCD_HandleTypeDef *)pdev->pData, ep_addr); } /** @@ -716,9 +680,8 @@ uint32_t USBD_LL_GetRxDataSize (USBD_HandleTypeDef *pdev, uint8_t ep_addr) * @param Delay: Delay in ms * @retval None */ -void USBD_LL_Delay (uint32_t Delay) -{ - HAL_Delay(Delay); +void USBD_LL_Delay(uint32_t Delay) { + HAL_Delay(Delay); } /** @@ -726,9 +689,8 @@ void USBD_LL_Delay (uint32_t Delay) * @param size: size of allocated memory * @retval None */ -void *USBD_static_malloc(uint32_t size) -{ - static uint32_t mem[(sizeof(USBD_DFU_HandleTypeDef)/4)+1];/* On 32-bit boundary */ +void *USBD_static_malloc(uint32_t size) { + static uint32_t mem[(sizeof(USBD_DFU_HandleTypeDef) / 4) + 1]; /* On 32-bit boundary */ return mem; } @@ -737,9 +699,7 @@ void *USBD_static_malloc(uint32_t size) * @param *p pointer to allocated memory address * @retval None */ -void USBD_static_free(void *p) -{ - +void USBD_static_free(void *p) { } /** @@ -748,20 +708,15 @@ void USBD_static_free(void *p) * @param state: connection state (0 : disconnected / 1: connected) * @retval None */ -void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state) -{ -/* USER CODE BEGIN 4 */ - if (state == 1) - { +void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state) { + /* USER CODE BEGIN 4 */ + if(state == 1) { /* Configure Low Connection State */ - - } - else - { + + } else { /* Configure High Connection State */ - - } -/* USER CODE END 4 */ + } + /* USER CODE END 4 */ } /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/f3dfu/Src/usbd_desc.c b/f3dfu/Src/usbd_desc.c index 0fb920ac..e4a23bdf 100644 --- a/f3dfu/Src/usbd_desc.c +++ b/f3dfu/Src/usbd_desc.c @@ -54,125 +54,125 @@ /** @defgroup USBD_DESC * @brief USBD descriptors module * @{ - */ + */ /** @defgroup USBD_DESC_Private_TypesDefinitions * @{ - */ + */ /** * @} - */ + */ /** @defgroup USBD_DESC_Private_Defines * @{ - */ -#define USBD_VID 1155 -#define USBD_LANGID_STRING 1033 -#define USBD_MANUFACTURER_STRING "STMicroelectronics" -#define USBD_PID_FS 57105 -#define USBD_PRODUCT_STRING_FS "STM32 DownLoad Firmware Update" -#define USBD_SERIALNUMBER_STRING_FS "00000000001A" -#define USBD_CONFIGURATION_STRING_FS "DFU Config" -#define USBD_INTERFACE_STRING_FS "DFU Interface" + */ +#define USBD_VID 1155 +#define USBD_LANGID_STRING 1033 +#define USBD_MANUFACTURER_STRING "STMicroelectronics" +#define USBD_PID_FS 57105 +#define USBD_PRODUCT_STRING_FS "STM32 DownLoad Firmware Update" +#define USBD_SERIALNUMBER_STRING_FS "00000000001A" +#define USBD_CONFIGURATION_STRING_FS "DFU Config" +#define USBD_INTERFACE_STRING_FS "DFU Interface" /* USER CODE BEGIN 0 */ /* USER CODE END 0*/ /** * @} - */ + */ /** @defgroup USBD_DESC_Private_Macros * @{ - */ + */ /** * @} - */ + */ /** @defgroup USBD_DESC_Private_Variables * @{ - */ -uint8_t * USBD_FS_DeviceDescriptor( USBD_SpeedTypeDef speed , uint16_t *length); -uint8_t * USBD_FS_LangIDStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length); -uint8_t * USBD_FS_ManufacturerStrDescriptor ( USBD_SpeedTypeDef speed , uint16_t *length); -uint8_t * USBD_FS_ProductStrDescriptor ( USBD_SpeedTypeDef speed , uint16_t *length); -uint8_t * USBD_FS_SerialStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length); -uint8_t * USBD_FS_ConfigStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length); -uint8_t * USBD_FS_InterfaceStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length); + */ +uint8_t *USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); +uint8_t *USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); +uint8_t *USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); +uint8_t *USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); +uint8_t *USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); +uint8_t *USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); +uint8_t *USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); #ifdef USB_SUPPORT_USER_STRING_DESC -uint8_t * USBD_FS_USRStringDesc (USBD_SpeedTypeDef speed, uint8_t idx , uint16_t *length); -#endif /* USB_SUPPORT_USER_STRING_DESC */ +uint8_t *USBD_FS_USRStringDesc(USBD_SpeedTypeDef speed, uint8_t idx, uint16_t *length); +#endif /* USB_SUPPORT_USER_STRING_DESC */ USBD_DescriptorsTypeDef FS_Desc = -{ - USBD_FS_DeviceDescriptor, - USBD_FS_LangIDStrDescriptor, - USBD_FS_ManufacturerStrDescriptor, - USBD_FS_ProductStrDescriptor, - USBD_FS_SerialStrDescriptor, - USBD_FS_ConfigStrDescriptor, - USBD_FS_InterfaceStrDescriptor, + { + USBD_FS_DeviceDescriptor, + USBD_FS_LangIDStrDescriptor, + USBD_FS_ManufacturerStrDescriptor, + USBD_FS_ProductStrDescriptor, + USBD_FS_SerialStrDescriptor, + USBD_FS_ConfigStrDescriptor, + USBD_FS_InterfaceStrDescriptor, }; -#if defined ( __ICCARM__ ) /*!< IAR Compiler */ - #pragma data_alignment=4 +#if defined(__ICCARM__) /*!< IAR Compiler */ +#pragma data_alignment = 4 #endif /* USB Standard Device Descriptor */ __ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = - { - 0x12, /*bLength */ - USB_DESC_TYPE_DEVICE, /*bDescriptorType*/ - 0x00, /* bcdUSB */ - 0x02, - 0x00, /*bDeviceClass*/ - 0x00, /*bDeviceSubClass*/ - 0x00, /*bDeviceProtocol*/ - USB_MAX_EP0_SIZE, /*bMaxPacketSize*/ - LOBYTE(USBD_VID), /*idVendor*/ - HIBYTE(USBD_VID), /*idVendor*/ - LOBYTE(USBD_PID_FS), /*idVendor*/ - HIBYTE(USBD_PID_FS), /*idVendor*/ - 0x00, /*bcdDevice rel. 2.00*/ - 0x02, - USBD_IDX_MFC_STR, /*Index of manufacturer string*/ - USBD_IDX_PRODUCT_STR, /*Index of product string*/ - USBD_IDX_SERIAL_STR, /*Index of serial number string*/ - USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/ - } ; + { + 0x12, /*bLength */ + USB_DESC_TYPE_DEVICE, /*bDescriptorType*/ + 0x00, /* bcdUSB */ + 0x02, + 0x00, /*bDeviceClass*/ + 0x00, /*bDeviceSubClass*/ + 0x00, /*bDeviceProtocol*/ + USB_MAX_EP0_SIZE, /*bMaxPacketSize*/ + LOBYTE(USBD_VID), /*idVendor*/ + HIBYTE(USBD_VID), /*idVendor*/ + LOBYTE(USBD_PID_FS), /*idVendor*/ + HIBYTE(USBD_PID_FS), /*idVendor*/ + 0x00, /*bcdDevice rel. 2.00*/ + 0x02, + USBD_IDX_MFC_STR, /*Index of manufacturer string*/ + USBD_IDX_PRODUCT_STR, /*Index of product string*/ + USBD_IDX_SERIAL_STR, /*Index of serial number string*/ + USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/ +}; /* USB_DeviceDescriptor */ -#if defined ( __ICCARM__ ) /*!< IAR Compiler */ - #pragma data_alignment=4 +#if defined(__ICCARM__) /*!< IAR Compiler */ +#pragma data_alignment = 4 #endif /* USB Standard Device Descriptor */ __ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = -{ - USB_LEN_LANGID_STR_DESC, - USB_DESC_TYPE_STRING, - LOBYTE(USBD_LANGID_STRING), - HIBYTE(USBD_LANGID_STRING), + { + USB_LEN_LANGID_STR_DESC, + USB_DESC_TYPE_STRING, + LOBYTE(USBD_LANGID_STRING), + HIBYTE(USBD_LANGID_STRING), }; -#if defined ( __ICCARM__ ) /*!< IAR Compiler */ - #pragma data_alignment=4 +#if defined(__ICCARM__) /*!< IAR Compiler */ +#pragma data_alignment = 4 #endif __ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END; /** * @} - */ + */ /** @defgroup USBD_DESC_Private_FunctionPrototypes * @{ - */ + */ /** * @} - */ + */ /** @defgroup USBD_DESC_Private_Functions * @{ - */ + */ /** * @brief USBD_FS_DeviceDescriptor @@ -181,8 +181,7 @@ __ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END; * @param length : pointer to data length variable * @retval pointer to descriptor buffer */ -uint8_t * USBD_FS_DeviceDescriptor( USBD_SpeedTypeDef speed , uint16_t *length) -{ +uint8_t *USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { *length = sizeof(USBD_FS_DeviceDesc); return USBD_FS_DeviceDesc; } @@ -194,9 +193,8 @@ uint8_t * USBD_FS_DeviceDescriptor( USBD_SpeedTypeDef speed , uint16_t *length) * @param length : pointer to data length variable * @retval pointer to descriptor buffer */ -uint8_t * USBD_FS_LangIDStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length) -{ - *length = sizeof(USBD_LangIDDesc); +uint8_t *USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { + *length = sizeof(USBD_LangIDDesc); return USBD_LangIDDesc; } @@ -207,15 +205,11 @@ uint8_t * USBD_FS_LangIDStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *leng * @param length : pointer to data length variable * @retval pointer to descriptor buffer */ -uint8_t * USBD_FS_ProductStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length) -{ - if(speed == 0) - { - USBD_GetString (USBD_PRODUCT_STRING_FS, USBD_StrDesc, length); - } - else - { - USBD_GetString (USBD_PRODUCT_STRING_FS, USBD_StrDesc, length); +uint8_t *USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { + if(speed == 0) { + USBD_GetString(USBD_PRODUCT_STRING_FS, USBD_StrDesc, length); + } else { + USBD_GetString(USBD_PRODUCT_STRING_FS, USBD_StrDesc, length); } return USBD_StrDesc; } @@ -227,9 +221,8 @@ uint8_t * USBD_FS_ProductStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *len * @param length : pointer to data length variable * @retval pointer to descriptor buffer */ -uint8_t * USBD_FS_ManufacturerStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length) -{ - USBD_GetString (USBD_MANUFACTURER_STRING, USBD_StrDesc, length); +uint8_t *USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { + USBD_GetString(USBD_MANUFACTURER_STRING, USBD_StrDesc, length); return USBD_StrDesc; } @@ -240,15 +233,11 @@ uint8_t * USBD_FS_ManufacturerStrDescriptor( USBD_SpeedTypeDef speed , uint16_t * @param length : pointer to data length variable * @retval pointer to descriptor buffer */ -uint8_t * USBD_FS_SerialStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length) -{ - if(speed == USBD_SPEED_HIGH) - { - USBD_GetString (USBD_SERIALNUMBER_STRING_FS, USBD_StrDesc, length); - } - else - { - USBD_GetString (USBD_SERIALNUMBER_STRING_FS, USBD_StrDesc, length); +uint8_t *USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { + if(speed == USBD_SPEED_HIGH) { + USBD_GetString(USBD_SERIALNUMBER_STRING_FS, USBD_StrDesc, length); + } else { + USBD_GetString(USBD_SERIALNUMBER_STRING_FS, USBD_StrDesc, length); } return USBD_StrDesc; } @@ -260,17 +249,13 @@ uint8_t * USBD_FS_SerialStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *leng * @param length : pointer to data length variable * @retval pointer to descriptor buffer */ -uint8_t * USBD_FS_ConfigStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length) -{ - if(speed == USBD_SPEED_HIGH) - { - USBD_GetString (USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length); +uint8_t *USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { + if(speed == USBD_SPEED_HIGH) { + USBD_GetString(USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length); + } else { + USBD_GetString(USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length); } - else - { - USBD_GetString (USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length); - } - return USBD_StrDesc; + return USBD_StrDesc; } /** @@ -280,28 +265,24 @@ uint8_t * USBD_FS_ConfigStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *leng * @param length : pointer to data length variable * @retval pointer to descriptor buffer */ -uint8_t * USBD_FS_InterfaceStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length) -{ - if(speed == 0) - { - USBD_GetString (USBD_INTERFACE_STRING_FS, USBD_StrDesc, length); +uint8_t *USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { + if(speed == 0) { + USBD_GetString(USBD_INTERFACE_STRING_FS, USBD_StrDesc, length); + } else { + USBD_GetString(USBD_INTERFACE_STRING_FS, USBD_StrDesc, length); } - else - { - USBD_GetString (USBD_INTERFACE_STRING_FS, USBD_StrDesc, length); - } - return USBD_StrDesc; + return USBD_StrDesc; } /** * @} - */ + */ /** * @} - */ + */ /** * @} - */ + */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/f3dfu/Src/usbd_dfu_if.c b/f3dfu/Src/usbd_dfu_if.c index d15a6e4c..7a32a153 100644 --- a/f3dfu/Src/usbd_dfu_if.c +++ b/f3dfu/Src/usbd_dfu_if.c @@ -44,9 +44,9 @@ /* Includes ------------------------------------------------------------------*/ #include "usbd_dfu_if.h" /* USER CODE BEGIN INCLUDE */ -#define FLASH_ERASE_TIME (uint16_t)50 -#define FLASH_PROGRAM_TIME (uint16_t)50 -#define USBD_DFU_APP_END_ADD 0x08020000 +#define FLASH_ERASE_TIME (uint16_t)50 +#define FLASH_PROGRAM_TIME (uint16_t)50 +#define USBD_DFU_APP_END_ADD 0x08020000 /* USER CODE END INCLUDE */ /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY @@ -55,42 +55,42 @@ /** @defgroup USBD_DFU * @brief usbd core module * @{ - */ + */ /** @defgroup USBD_DFU_Private_TypesDefinitions * @{ - */ + */ /* USER CODE BEGIN PRIVATE_TYPES */ -/* USER CODE END PRIVATE_TYPES */ +/* USER CODE END PRIVATE_TYPES */ /** * @} - */ + */ /** @defgroup USBD_DFU_Private_Defines * @{ - */ + */ //#define FLASH_DESC_STR "@Internal Flash /0x08000000/03*016Ka,01*016Kg,01*064Kg,07*128Kg,04*016Kg,01*064Kg,07*128Kg" -#define FLASH_DESC_STR "@Internal Flash /0x08004000/112*0002Kg" +#define FLASH_DESC_STR "@Internal Flash /0x08004000/112*0002Kg" //USBD_DFU_MEDIA Interface USBD_DFU_MEDIA Parameter Description: The description of the flash (used by PC tool DFuSe) Each Alternate setting string descriptor must follow this memory mapping so that the PC Host Software can decode the right mapping for the selected device: ● @: To detect that this is a special mapping descriptor (to avoid decoding standard descriptor) ● /: for separator between zones ● Maximum 8 digits per address starting by “0x” ● /: for separator between zones ● Maximum of 2 digits for the number of sectors ● *: For separator between number of sectors and sector size ● Maximum 3 digits for sector size between 0 and 999 ● 1 digit for the sector size multiplier. Valid entries are: B (byte), K (Kilo), M (Mega) ● 1 digit for the sector type as follows: – a (0x41): Readable – b (0x42): Erasable – c (0x43): Readable and Erasabled (0x44): Writeable – e (0x45): Readable and Writeable – f (0x46): Erasable and Writeable – g (0x47): Readable, Erasable and Writeable Note: If the target memory is not contiguous, the user can add the new sectors to be decoded just after a slash"/" as shown in the following example: "@Flash /0xF000/1*4Ka/0xE000/1*4Kg/0x8000/2*24Kg" //int: Found DFU: [0483:df11] ver=2200, devnum=6, cfg=1, intf=0, path="20-1", alt=0, name="@Internal Flash /0x08000000/128*0002Kg", serial="2060374D2033" //this: Found DFU: [0483:df11] ver=0200, devnum=5, cfg=1, intf=0, path="20-1", alt=0, name="@Internal Flash /0x08000000/03*016Ka,01*016Kg,01*064Kg,07*128Kg,04*016Kg,01*064Kg,07*128Kg", serial="00000000001A" //int: 2048 transfer size! /* USER CODE BEGIN PRIVATE_DEFINES */ /* USER CODE END PRIVATE_DEFINES */ - + /** * @} - */ + */ /** @defgroup USBD_DFU_Private_Macros * @{ - */ + */ /* USER CODE BEGIN PRIVATE_MACRO */ /* USER CODE END PRIVATE_MACRO */ /** * @} - */ + */ /** @defgroup USBD_AUDIO_IF_Private_Variables * @{ @@ -100,48 +100,48 @@ /** * @} - */ - + */ + /** @defgroup USBD_DFU_IF_Exported_Variables * @{ - */ - extern USBD_HandleTypeDef hUsbDeviceFS; + */ +extern USBD_HandleTypeDef hUsbDeviceFS; /* USER CODE BEGIN EXPORTED_VARIABLES */ /* USER CODE END EXPORTED_VARIABLES */ /** * @} - */ - + */ + /** @defgroup USBD_DFU_Private_FunctionPrototypes * @{ */ static uint16_t MEM_If_Init_FS(void); -static uint16_t MEM_If_Erase_FS (uint32_t Add); -static uint16_t MEM_If_Write_FS (uint8_t *src, uint8_t *dest, uint32_t Len); -static uint8_t *MEM_If_Read_FS (uint8_t *src, uint8_t *dest, uint32_t Len); +static uint16_t MEM_If_Erase_FS(uint32_t Add); +static uint16_t MEM_If_Write_FS(uint8_t *src, uint8_t *dest, uint32_t Len); +static uint8_t *MEM_If_Read_FS(uint8_t *src, uint8_t *dest, uint32_t Len); static uint16_t MEM_If_DeInit_FS(void); -static uint16_t MEM_If_GetStatus_FS (uint32_t Add, uint8_t Cmd, uint8_t *buffer); +static uint16_t MEM_If_GetStatus_FS(uint32_t Add, uint8_t Cmd, uint8_t *buffer); /* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */ /* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */ /** * @} - */ - -#if defined ( __ICCARM__ ) /*!< IAR Compiler */ - #pragma data_alignment=4 + */ + +#if defined(__ICCARM__) /*!< IAR Compiler */ +#pragma data_alignment = 4 #endif __ALIGN_BEGIN USBD_DFU_MediaTypeDef USBD_DFU_fops_FS __ALIGN_END = -{ - (uint8_t*)FLASH_DESC_STR, - MEM_If_Init_FS, - MEM_If_DeInit_FS, - MEM_If_Erase_FS, - MEM_If_Write_FS, - MEM_If_Read_FS, - MEM_If_GetStatus_FS, + { + (uint8_t *)FLASH_DESC_STR, + MEM_If_Init_FS, + MEM_If_DeInit_FS, + MEM_If_Erase_FS, + MEM_If_Write_FS, + MEM_If_Read_FS, + MEM_If_GetStatus_FS, }; /* Private functions ---------------------------------------------------------*/ @@ -151,13 +151,12 @@ __ALIGN_BEGIN USBD_DFU_MediaTypeDef USBD_DFU_fops_FS __ALIGN_END = * @param None * @retval 0 if operation is successful, MAL_FAIL else. */ -uint16_t MEM_If_Init_FS(void) -{ - /* USER CODE BEGIN 0 */ +uint16_t MEM_If_Init_FS(void) { + /* USER CODE BEGIN 0 */ /* Unlock the internal flash */ HAL_FLASH_Unlock(); return (USBD_OK); - /* USER CODE END 0 */ + /* USER CODE END 0 */ } /** @@ -166,13 +165,12 @@ uint16_t MEM_If_Init_FS(void) * @param None * @retval 0 if operation is successful, MAL_FAIL else. */ -uint16_t MEM_If_DeInit_FS(void) -{ +uint16_t MEM_If_DeInit_FS(void) { /* USER CODE BEGIN 1 */ /* Lock the internal flash */ HAL_FLASH_Lock(); return (USBD_OK); - /* USER CODE END 1 */ + /* USER CODE END 1 */ } /** @@ -181,9 +179,8 @@ uint16_t MEM_If_DeInit_FS(void) * @param Add: Address of sector to be erased. * @retval 0 if operation is successful, MAL_FAIL else. */ -uint16_t MEM_If_Erase_FS(uint32_t Add) -{ - /* USER CODE BEGIN 2 */ +uint16_t MEM_If_Erase_FS(uint32_t Add) { + /* USER CODE BEGIN 2 */ uint32_t NbOfPages = 0; uint32_t PageError = 0; /* Variable contains Flash operation status */ @@ -192,19 +189,18 @@ uint16_t MEM_If_Erase_FS(uint32_t Add) //TODO: only erase APP pages /* Get the number of sector to erase from 1st sector*/ //NbOfPages = (USBD_DFU_APP_END_ADD - USBD_DFU_APP_DEFAULT_ADD) / FLASH_PAGE_SIZE; - NbOfPages = 1; + NbOfPages = 1; eraseinitstruct.TypeErase = FLASH_TYPEERASE_PAGES; //eraseinitstruct.PageAddress = USBD_DFU_APP_DEFAULT_ADD; eraseinitstruct.PageAddress = Add; - eraseinitstruct.NbPages = NbOfPages; - status = HAL_FLASHEx_Erase(&eraseinitstruct, &PageError); + eraseinitstruct.NbPages = NbOfPages; + status = HAL_FLASHEx_Erase(&eraseinitstruct, &PageError); - if (status != HAL_OK) - { + if(status != HAL_OK) { return 1; } return 0; - /* USER CODE END 2 */ + /* USER CODE END 2 */ } /** @@ -215,31 +211,25 @@ uint16_t MEM_If_Erase_FS(uint32_t Add) * @param Len: Number of data to be written (in bytes). * @retval 0 if operation is successful, MAL_FAIL else. */ -uint16_t MEM_If_Write_FS(uint8_t *src, uint8_t *dest, uint32_t Len) -{ - /* USER CODE BEGIN 3 */ +uint16_t MEM_If_Write_FS(uint8_t *src, uint8_t *dest, uint32_t Len) { + /* USER CODE BEGIN 3 */ uint32_t i = 0; - for(i = 0; i < Len; i+=4) - { + for(i = 0; i < Len; i += 4) { /* Device voltage range supposed to be [2.7V to 3.6V], the operation will be done by byte */ - if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)(dest+i), *(uint32_t*)(src+i)) == HAL_OK) - { + if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)(dest + i), *(uint32_t *)(src + i)) == HAL_OK) { /* Check the written value */ - if(*(uint32_t *)(src + i) != *(uint32_t*)(dest+i)) - { + if(*(uint32_t *)(src + i) != *(uint32_t *)(dest + i)) { /* Flash content doesn't match SRAM content */ return 2; } - } - else - { + } else { /* Error occurred while writing data in Flash memory */ return 1; } } return 0; - /* USER CODE END 3 */ + /* USER CODE END 3 */ } /** @@ -250,20 +240,18 @@ uint16_t MEM_If_Write_FS(uint8_t *src, uint8_t *dest, uint32_t Len) * @param Len: Number of data to be read (in bytes). * @retval Pointer to the physical address where data should be read. */ -uint8_t *MEM_If_Read_FS (uint8_t *src, uint8_t *dest, uint32_t Len) -{ +uint8_t *MEM_If_Read_FS(uint8_t *src, uint8_t *dest, uint32_t Len) { /* Return a valid address to avoid HardFault */ - /* USER CODE BEGIN 4 */ - uint32_t i = 0; + /* USER CODE BEGIN 4 */ + uint32_t i = 0; uint8_t *psrc = src; - for(i = 0; i < Len; i++) - { + for(i = 0; i < Len; i++) { dest[i] = *psrc++; } /* Return a valid address to avoid HardFault */ - return (uint8_t*)(dest); - /* USER CODE END 4 */ + return (uint8_t *)(dest); + /* USER CODE END 4 */ } /** @@ -274,26 +262,24 @@ uint8_t *MEM_If_Read_FS (uint8_t *src, uint8_t *dest, uint32_t Len) * @param buffer: used for returning the time necessary for a program or an erase operation * @retval 0 if operation is successful */ -uint16_t MEM_If_GetStatus_FS (uint32_t Add, uint8_t Cmd, uint8_t *buffer) -{ - /* USER CODE BEGIN 5 */ - switch (Cmd) - { - case DFU_MEDIA_PROGRAM: - buffer[1] = (uint8_t)FLASH_PROGRAM_TIME; - buffer[2] = (uint8_t)(FLASH_PROGRAM_TIME << 8); - buffer[3] = 0; - break; - - case DFU_MEDIA_ERASE: - default: - buffer[1] = (uint8_t)FLASH_ERASE_TIME; - buffer[2] = (uint8_t)(FLASH_ERASE_TIME << 8); - buffer[3] = 0; - break; - } - return (USBD_OK); - /* USER CODE END 5 */ +uint16_t MEM_If_GetStatus_FS(uint32_t Add, uint8_t Cmd, uint8_t *buffer) { + /* USER CODE BEGIN 5 */ + switch(Cmd) { + case DFU_MEDIA_PROGRAM: + buffer[1] = (uint8_t)FLASH_PROGRAM_TIME; + buffer[2] = (uint8_t)(FLASH_PROGRAM_TIME << 8); + buffer[3] = 0; + break; + + case DFU_MEDIA_ERASE: + default: + buffer[1] = (uint8_t)FLASH_ERASE_TIME; + buffer[2] = (uint8_t)(FLASH_ERASE_TIME << 8); + buffer[3] = 0; + break; + } + return (USBD_OK); + /* USER CODE END 5 */ } /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */ @@ -301,10 +287,9 @@ uint16_t MEM_If_GetStatus_FS (uint32_t Add, uint8_t Cmd, uint8_t *buffer) /** * @} - */ + */ /** * @} - */ + */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - diff --git a/inc/hw/hw.h b/inc/hw/hw.h index 58ccd198..87ec4d21 100644 --- a/inc/hw/hw.h +++ b/inc/hw/hw.h @@ -16,14 +16,14 @@ #define COS_PIN GPIO_Pin_5 #define COS_PORT GPIOC -#define ADC_REF 3.3//analog reference voltage -#define ADC_RES 4096.0//analog resolution, 12 bit +#define ADC_REF 3.3 //analog reference voltage +#define ADC_RES 4096.0 //analog resolution, 12 bit -#define OP_R_INPUT 1000.0//opamp input -#define OP_R_FEEDBACK 3900.0//opamp feedback -#define OP_R_OUT_LOW 180.0//opamp out low -#define OP_R_OUT_HIGH 470.0//opamp out high -#define OP_REF 5.0//opamp reference voltage +#define OP_R_INPUT 1000.0 //opamp input +#define OP_R_FEEDBACK 3900.0 //opamp feedback +#define OP_R_OUT_LOW 180.0 //opamp out low +#define OP_R_OUT_HIGH 470.0 //opamp out high +#define OP_REF 5.0 //opamp reference voltage //FB UART #define FB0_UART USART3 @@ -91,7 +91,7 @@ #define FB0_ENC_TIM_RCC RCC_APB1Periph_TIM3 #define FB0_RES_TIM TIM2 -#define FB0_RES_TIM_AF GPIO_AF_TIM2 //sync to slave timer for resolver +#define FB0_RES_TIM_AF GPIO_AF_TIM2 //sync to slave timer for resolver #define FB0_RES_TIM_RCC RCC_APB1Periph_TIM2 #define TIM_MASTER TIM4 @@ -120,14 +120,14 @@ #define COS_PIN GPIO_Pin_7 #define COS_PORT GPIOA -#define ADC_REF 3.3//analog reference voltage -#define ADC_RES 4096.0//analog resolution, 12 bit +#define ADC_REF 3.3 //analog reference voltage +#define ADC_RES 4096.0 //analog resolution, 12 bit -#define OP_R_INPUT 10000.0//opamp input -#define OP_R_FEEDBACK 15000.0//opamp feedback -#define OP_R_OUT_LOW 470.0//opamp out low -#define OP_R_OUT_HIGH 22.0//opamp out high -#define OP_REF 1.83//opamp reference voltage +#define OP_R_INPUT 10000.0 //opamp input +#define OP_R_FEEDBACK 15000.0 //opamp feedback +#define OP_R_OUT_LOW 470.0 //opamp out low +#define OP_R_OUT_HIGH 22.0 //opamp out high +#define OP_REF 1.83 //opamp reference voltage //FB UART #define FB0_UART USART6 @@ -142,7 +142,7 @@ #define FB0_UART_TX_DMA_CHAN DMA_Channel_5 #define FB0_UART_TX_DMA_TCIF DMA_FLAG_TCIF6 -//v4 does not have dedicated rx pin, use pc6 as rx and tx. +//v4 does not have dedicated rx pin, use pc6 as rx and tx. //#define FB0_UART_RX_PIN GPIO_Pin_11 //#define FB0_UART_RX_PORT GPIOB //#define FB0_UART_RX_PIN_SOURCE GPIO_PinSource11 @@ -246,7 +246,7 @@ #define FB0_ENC_TIM_RCC RCC_APB1Periph_TIM4 #define FB0_RES_TIM TIM4 -#define FB0_RES_TIM_AF GPIO_AF_TIM4 //sync to slave timer for resolver +#define FB0_RES_TIM_AF GPIO_AF_TIM4 //sync to slave timer for resolver #define FB0_RES_TIM_RCC RCC_APB1Periph_TIM4 //FB1, TIM1 #define FB1_A_PIN GPIO_Pin_9 diff --git a/inc/main.h b/inc/main.h index 26e594fe..f4aef79a 100644 --- a/inc/main.h +++ b/inc/main.h @@ -2,28 +2,28 @@ #include //fault state -typedef enum{ - DISABLED = 0, - ENABLED, - PHASING, - SOFT_FAULT, - HARD_FAULT, - LED_TEST, +typedef enum { + DISABLED = 0, + ENABLED, + PHASING, + SOFT_FAULT, + HARD_FAULT, + LED_TEST, } state_t; //fault errors -typedef enum{ - NO_ERROR = 0, - CMD_ERROR, - MOT_FB_ERROR, - COM_FB_ERROR, - JOINT_FB_ERROR, - POS_ERROR, - SAT_ERROR, - HV_ERROR, - HV_TEMP_ERROR, - HV_VOLT_ERROR, - MOT_TEMP_ERROR, +typedef enum { + NO_ERROR = 0, + CMD_ERROR, + MOT_FB_ERROR, + COM_FB_ERROR, + JOINT_FB_ERROR, + POS_ERROR, + SAT_ERROR, + HV_ERROR, + HV_TEMP_ERROR, + HV_VOLT_ERROR, + MOT_TEMP_ERROR, } fault_t; void Wait(uint32_t ms); diff --git a/inc/old/eeprom.h b/inc/old/eeprom.h old mode 100755 new mode 100644 index c62a1ea9..68c89fe4 --- a/inc/old/eeprom.h +++ b/inc/old/eeprom.h @@ -18,7 +18,7 @@ * *

© COPYRIGHT 2011 STMicroelectronics

****************************************************************************** - */ + */ /* Define to prevent recursive inclusion -------------------------------------*/ //TODO: https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Java/Flat.aspx?RootFolder=%2fpublic%2fSTe2ecommunities%2fmcu%2fLists%2fSTM32Java%2fBug%20%28possible%20data%20loss%29%20in%20STM32F4xx_EEPROM_Emulation%20%28AN3969%29¤tviews=346 @@ -30,53 +30,53 @@ /* Exported constants --------------------------------------------------------*/ /* Define the size of the sectors to be used */ -#define PAGE_SIZE (uint32_t)0x4000 /* Page size = 16KByte */ +#define PAGE_SIZE (uint32_t)0x4000 /* Page size = 16KByte */ /* Device voltage range supposed to be [2.7V to 3.6V], the operation will be done by word */ -#define VOLTAGE_RANGE (uint8_t)VoltageRange_3 +#define VOLTAGE_RANGE (uint8_t) VoltageRange_3 /* EEPROM start address in Flash */ -#define EEPROM_START_ADDRESS ((uint32_t)0x08008000) /* EEPROM emulation start address: - from sector2 : after 16KByte of used - Flash memory */ +#define EEPROM_START_ADDRESS ((uint32_t)0x08008000) /* EEPROM emulation start address: \ + from sector2 : after 16KByte of used \ + Flash memory */ /* Pages 0 and 1 base and end addresses */ -#define PAGE0_BASE_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + 0x0000)) -#define PAGE0_END_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + (PAGE_SIZE - 1))) -#define PAGE0_ID FLASH_Sector_2 +#define PAGE0_BASE_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + 0x0000)) +#define PAGE0_END_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + (PAGE_SIZE - 1))) +#define PAGE0_ID FLASH_Sector_2 -#define PAGE1_BASE_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + 0x4000)) -#define PAGE1_END_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + (2 * PAGE_SIZE - 1))) -#define PAGE1_ID FLASH_Sector_3 +#define PAGE1_BASE_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + 0x4000)) +#define PAGE1_END_ADDRESS ((uint32_t)(EEPROM_START_ADDRESS + (2 * PAGE_SIZE - 1))) +#define PAGE1_ID FLASH_Sector_3 /* Used Flash pages for EEPROM emulation */ -#define PAGE0 ((uint16_t)0x0000) -#define PAGE1 ((uint16_t)0x0001) +#define PAGE0 ((uint16_t)0x0000) +#define PAGE1 ((uint16_t)0x0001) /* No valid page define */ -#define NO_VALID_PAGE ((uint16_t)0x00AB) +#define NO_VALID_PAGE ((uint16_t)0x00AB) /* Page status definitions */ -#define ERASED ((uint16_t)0xFFFF) /* Page is empty */ -#define RECEIVE_DATA ((uint16_t)0xEEEE) /* Page is marked to receive data */ -#define VALID_PAGE ((uint16_t)0x0000) /* Page containing valid data */ +#define ERASED ((uint16_t)0xFFFF) /* Page is empty */ +#define RECEIVE_DATA ((uint16_t)0xEEEE) /* Page is marked to receive data */ +#define VALID_PAGE ((uint16_t)0x0000) /* Page containing valid data */ /* Valid pages in read and write defines */ -#define READ_FROM_VALID_PAGE ((uint8_t)0x00) -#define WRITE_IN_VALID_PAGE ((uint8_t)0x01) +#define READ_FROM_VALID_PAGE ((uint8_t)0x00) +#define WRITE_IN_VALID_PAGE ((uint8_t)0x01) /* Page full define */ -#define PAGE_FULL ((uint8_t)0x80) +#define PAGE_FULL ((uint8_t)0x80) /* Variables' number */ -#define NB_OF_VAR ((uint8_t)150) +#define NB_OF_VAR ((uint8_t)150) /* Exported types ------------------------------------------------------------*/ /* Exported macro ------------------------------------------------------------*/ /* Exported functions ------------------------------------------------------- */ uint16_t EE_Init(void); -uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data); +uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t *Data); uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data); #endif /* __EEPROM_H */ diff --git a/inc/old/link.h b/inc/old/link.h index b072f517..bf465b44 100644 --- a/inc/old/link.h +++ b/inc/old/link.h @@ -4,7 +4,7 @@ #define KGCM2(a) ((a) / 10000.0) //Do not change order, append new types at the end -typedef enum{ +typedef enum { ENC = 1, RES, SINCOS, @@ -22,7 +22,7 @@ typedef enum{ SSI } protocol_t; -typedef enum{ +typedef enum { ACSYNC = 0, ACASYNC, AC2PHASE, diff --git a/inc/setup.h b/inc/setup.h index 444baedb..6b439bed 100644 --- a/inc/setup.h +++ b/inc/setup.h @@ -18,8 +18,8 @@ //sample times for F4: 3,15,28,56,84,112,144,480 #define RES_SampleTime ADC_SampleTime_3Cycles -#define ADC_ANZ 60 -#define PID_WAVES 4 +#define ADC_ANZ 60 +#define PID_WAVES 4 void setup(void); void setup_res(void); diff --git a/inc/sserial.h b/inc/sserial.h index 5b3e23d7..4b9b594c 100644 --- a/inc/sserial.h +++ b/inc/sserial.h @@ -43,9 +43,9 @@ #define LBPCardName2Cmd 0xd2 #define LBPCardName3Cmd 0xd3 -#define CT_RW 1 // 01b -#define CT_RPC 2 // 10b -#define CT_LOCAL 3 // 11b +#define CT_RW 1 // 01b +#define CT_RPC 2 // 10b +#define CT_LOCAL 3 // 11b #define MEM_SIZE 2048 @@ -53,29 +53,29 @@ #define RECORD_TYPE_MODE_DATA_RECORD 0xB0 //process data -#define DATA_TYPE_PAD 0x00 //padding, done automaticly at the end -#define DATA_TYPE_BITS 0x01 -#define DATA_TYPE_UNSIGNED 0x02 -#define DATA_TYPE_SIGNED 0x03 -#define DATA_TYPE_NONVOL_UNSIGNED 0x04 -#define DATA_TYPE_NONVOL_SIGNED 0x05 -#define DATA_TYPE_STREAM 0x06 -#define DATA_TYPE_BOOLEAN 0x07 -#define DATA_TYPE_ENCODER 0x08 -#define DATA_TYPE_FLOAT 0x10 // New for STMBL -#define DATA_TYPE_ENCODER_H 0x18 -#define DATA_TYPE_ENCODER_L 0x28 +#define DATA_TYPE_PAD 0x00 //padding, done automaticly at the end +#define DATA_TYPE_BITS 0x01 +#define DATA_TYPE_UNSIGNED 0x02 +#define DATA_TYPE_SIGNED 0x03 +#define DATA_TYPE_NONVOL_UNSIGNED 0x04 +#define DATA_TYPE_NONVOL_SIGNED 0x05 +#define DATA_TYPE_STREAM 0x06 +#define DATA_TYPE_BOOLEAN 0x07 +#define DATA_TYPE_ENCODER 0x08 +#define DATA_TYPE_FLOAT 0x10 // New for STMBL +#define DATA_TYPE_ENCODER_H 0x18 +#define DATA_TYPE_ENCODER_L 0x28 #define DATA_DIRECTION_INPUT 0x00 #define DATA_DIRECTION_BI_DIRECTIONAL 0x40 #define DATA_DIRECTION_OUTPUT 0x80 -#define MEMPTR(p) ((uint32_t)&p-(uint32_t)&memory) +#define MEMPTR(p) ((uint32_t)&p - (uint32_t)&memory) #define MEMU8(ptr) (memory.bytes[ptr]) -#define MEMU16(ptr) (memory.bytes[ptr] | memory.bytes[ptr+1]<<8) -#define MEMU32(ptr) (memory.bytes[ptr] | memory.bytes[ptr+1]<<8 | memory.bytes[ptr+2]<<16 | memory.bytes[ptr+3]<<24) -#define MEMFLOAT(ptr) ((float)({uint32_t tmp = MEMU32(ptr);*((float*)&tmp);})) +#define MEMU16(ptr) (memory.bytes[ptr] | memory.bytes[ptr + 1] << 8) +#define MEMU32(ptr) (memory.bytes[ptr] | memory.bytes[ptr + 1] << 8 | memory.bytes[ptr + 2] << 16 | memory.bytes[ptr + 3] << 24) +#define MEMFLOAT(ptr) ((float)({uint32_t tmp = MEMU32(ptr);*((float*)&tmp); })) #define NUM_BYTES(bits) (bits / 8 + (bits % 8 > 0 ? 1 : 0)) @@ -89,30 +89,34 @@ #define DATA_DIR(pd_ptr) INDIRECT_PD(pd_ptr)->data_direction #define DATA_SIZE(pd_ptr) INDIRECT_PD(pd_ptr)->data_size -#define ADD_PROCESS_VAR(args) *ptocp = add_pd args; input_bits += IS_INPUT(INDIRECT_PD(ptocp)) ? DATA_SIZE(ptocp) : 0; output_bits += IS_OUTPUT(INDIRECT_PD(ptocp)) ? DATA_SIZE(ptocp) : 0; last_pd = INDIRECT_PD(ptocp++) +#define ADD_PROCESS_VAR(args) \ + *ptocp = add_pd args; \ + input_bits += IS_INPUT(INDIRECT_PD(ptocp)) ? DATA_SIZE(ptocp) : 0; \ + output_bits += IS_OUTPUT(INDIRECT_PD(ptocp)) ? DATA_SIZE(ptocp) : 0; \ + last_pd = INDIRECT_PD(ptocp++) #define ADD_GLOBAL_VAR(args) *gtocp++ = add_pd args #define ADD_MODE(args) *gtocp++ = add_mode args -#define BITSLEFT(ptr) (8-ptr) +#define BITSLEFT(ptr) (8 - ptr) #define BOOLPIN(pin) (uint8_t)(PIN(pin) > 0.0) -typedef union{ - struct { - uint8_t ds:2;//DataSize: Specifies data size, 00b = 1 bytes, 01b = 2 bytes, 10 b= 4 bytes, 11b = 8 bytes. - uint8_t as:1;//AddressSize: 0 to specify current address, 1 to specify 2 byte address. - uint8_t ai:1;//AutoInc: 0 leaves address unchanged, 1 specifies that address is post incremented by data size in bytes. - uint8_t rid:1;//RPCIncludesData: 0 specifies that data is from stream, 1, that data is from RPC (RPC only, ignored for non RPC commands) - uint8_t wr:1;//Write: 1 to specify write, 0 to specify read - uint8_t ct:2;//CommandType: Must be 01b to specify data read/write command - // 01:rw 11:local command 10:rpc - }; - struct { - uint8_t rpc:6; - uint8_t dummy:2; - //01:rw 11: local command 10:rpc - }; - uint8_t byte; +typedef union { + struct { + uint8_t ds : 2; //DataSize: Specifies data size, 00b = 1 bytes, 01b = 2 bytes, 10 b= 4 bytes, 11b = 8 bytes. + uint8_t as : 1; //AddressSize: 0 to specify current address, 1 to specify 2 byte address. + uint8_t ai : 1; //AutoInc: 0 leaves address unchanged, 1 specifies that address is post incremented by data size in bytes. + uint8_t rid : 1; //RPCIncludesData: 0 specifies that data is from stream, 1, that data is from RPC (RPC only, ignored for non RPC commands) + uint8_t wr : 1; //Write: 1 to specify write, 0 to specify read + uint8_t ct : 2; //CommandType: Must be 01b to specify data read/write command + // 01:rw 11:local command 10:rpc + }; + struct { + uint8_t rpc : 6; + uint8_t dummy : 2; + //01:rw 11: local command 10:rpc + }; + uint8_t byte; } lbp_t; #pragma pack(1) @@ -121,36 +125,36 @@ typedef union { uint32_t unit; } unit_no_t; -typedef struct{ - uint8_t record_type;//0xa0 - uint8_t data_size; - uint8_t data_type; - uint8_t data_direction; - float param_min; - float param_max; - uint16_t data_addr; - char names; +typedef struct { + uint8_t record_type; //0xa0 + uint8_t data_size; + uint8_t data_type; + uint8_t data_direction; + float param_min; + float param_max; + uint16_t data_addr; + char names; } process_data_descriptor_t; -typedef struct{ - uint8_t record_type;//0xb0 - uint8_t index; - uint8_t type; - uint8_t unused; - char names; +typedef struct { + uint8_t record_type; //0xb0 + uint8_t index; + uint8_t type; + uint8_t unused; + char names; } mode_descriptor_t; -typedef struct{ - uint8_t input; //process data input bytes - uint8_t output; //process data output bytes - uint16_t ptocp; //pointer to process data table start - uint16_t gtocp; //pointer to mode data table start +typedef struct { + uint8_t input; //process data input bytes + uint8_t output; //process data output bytes + uint16_t ptocp; //pointer to process data table start + uint16_t gtocp; //pointer to mode data table start } discovery_rpc_t; typedef union { struct { - discovery_rpc_t discovery;//6 bytes - uint16_t foo;//padding, so heap is word aligned + discovery_rpc_t discovery; //6 bytes + uint16_t foo; //padding, so heap is word aligned uint8_t heap[MEM_SIZE - sizeof(discovery_rpc_t)]; }; diff --git a/inc/stm32f4xx_conf.h b/inc/stm32f4xx_conf.h index b81ad186..0d41d249 100644 --- a/inc/stm32f4xx_conf.h +++ b/inc/stm32f4xx_conf.h @@ -17,7 +17,7 @@ * *

© COPYRIGHT 2011 STMicroelectronics

****************************************************************************** - */ + */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __STM32F4xx_CONF_H @@ -66,7 +66,7 @@ /* #define USE_FULL_ASSERT 1 */ /* Exported macro ------------------------------------------------------------*/ -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** * @brief The assert_param macro is used for function's parameters check. @@ -76,11 +76,11 @@ * If expr is true, it returns no value. * @retval None */ - #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) +#define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) /* Exported functions ------------------------------------------------------- */ - void assert_failed(uint8_t* file, uint32_t line); +void assert_failed(uint8_t *file, uint32_t line); #else - #define assert_param(expr) ((void)0) +#define assert_param(expr) ((void)0) #endif /* USE_FULL_ASSERT */ #endif /* __STM32F4xx_CONF_H */ diff --git a/inc/stm32f4xx_it.h b/inc/stm32f4xx_it.h index 74f17c9c..4ded470e 100644 --- a/inc/stm32f4xx_it.h +++ b/inc/stm32f4xx_it.h @@ -17,15 +17,15 @@ * *

© COPYRIGHT 2011 STMicroelectronics

****************************************************************************** - */ + */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __STM32F4xx_IT_H #define __STM32F4xx_IT_H #ifdef __cplusplus - extern "C" { -#endif +extern "C" { +#endif /* Includes ------------------------------------------------------------------*/ #include "stm32f4xx.h" diff --git a/inc/usbd_cdc_if.h b/inc/usbd_cdc_if.h index 467968f8..d6dafdb9 100644 --- a/inc/usbd_cdc_if.h +++ b/inc/usbd_cdc_if.h @@ -9,23 +9,23 @@ /* These are external variables imported from CDC core to be used for IN transfer management. */ -extern uint8_t APP_Rx_Buffer[]; /* Write CDC received data in this buffer. +extern uint8_t APP_Rx_Buffer[]; /* Write CDC received data in this buffer. These data will be sent over USB IN endpoint in the CDC core functions. */ -extern uint32_t APP_Rx_ptr_in; /* Increment this pointer or roll it back to +extern uint32_t APP_Rx_ptr_in; /* Increment this pointer or roll it back to start address when writing received data in the buffer APP_Rx_Buffer. */ extern struct ringbuf usb_rx_buf; extern struct ringbuf usb_tx_buf; -uint8_t USB_CDC_is_connected(void); +uint8_t USB_CDC_is_connected(void); uint16_t USB_VCP_get_string(char *ptr); void USB_VCP_send_string(unsigned char *ptr); void usb_init(void); void cdc_init(void); -int cdc_tx(void* data, uint32_t len); +int cdc_tx(void *data, uint32_t len); int cdc_getline(char *ptr, int len); int cdc_is_connected(); void cdc_poll(); diff --git a/shared/angle.c b/shared/angle.c index e9ecef0b..863498e5 100644 --- a/shared/angle.c +++ b/shared/angle.c @@ -55,49 +55,61 @@ const float sintab[257] = { 0.989177, 0.990058, 0.990903, 0.991710, 0.992480, 0.993212, 0.993907, 0.994565, 0.995185, 0.995767, 0.996313, 0.996820, 0.997290, 0.997723, 0.998118, 0.998476, 0.998795, 0.999078, 0.999322, 0.999529, 0.999699, 0.999831, 0.999925, 0.999981, - 1.000000 -}; + 1.000000}; -void sincos_fast(float x, float *sin, float *cos){ - extern const float sintab[257]; +void sincos_fast(float x, float *sin, float *cos) { + extern const float sintab[257]; - x = x * 256 / (M_PI/2); + x = x * 256 / (M_PI / 2); - int y = (int)x; - if (y < 0) - y -= 1; + int y = (int)x; + if(y < 0) + y -= 1; - float f = x - y; - int i = y & 255; + float f = x - y; + int i = y & 255; - float s = (1-f) * sintab[i ] + f * sintab[i+1 ]; - float c = (1-f) * sintab[256-i] + f * sintab[255-i]; + float s = (1 - f) * sintab[i] + f * sintab[i + 1]; + float c = (1 - f) * sintab[256 - i] + f * sintab[255 - i]; - switch ((y >> 8) & 3) { - case 0: *sin = s; *cos = c; break; - case 1: *sin = c; *cos = -s; break; - case 2: *sin = -s; *cos = -c; break; - case 3: *sin = -c; *cos = s; break; - default: *sin = 0; *cos = 0; break; - } -} - -inline float minus(float a, float b){ - if(ABS(a - b) < M_PI){ - return(a - b); - } - else if(a > b){ - return(a - b - 2.0 * M_PI); - } - else{ - return(a - b + 2.0 * M_PI); - } -} - -inline float mod(float a){ - if(a + M_PI < 0.0f){ - return -(fmodf(-(a + M_PI), M_PI * 2.0) - M_PI); - }else{ - return fmodf(a + M_PI, M_PI * 2.0) - M_PI; + switch((y >> 8) & 3) { + case 0: + *sin = s; + *cos = c; + break; + case 1: + *sin = c; + *cos = -s; + break; + case 2: + *sin = -s; + *cos = -c; + break; + case 3: + *sin = -c; + *cos = s; + break; + default: + *sin = 0; + *cos = 0; + break; + } +} + +inline float minus(float a, float b) { + if(ABS(a - b) < M_PI) { + return (a - b); + } else if(a > b) { + return (a - b - 2.0 * M_PI); + } else { + return (a - b + 2.0 * M_PI); + } +} + +inline float mod(float a) { + if(a + M_PI < 0.0f) { + return -(fmodf(-(a + M_PI), M_PI * 2.0) - M_PI); + } else { + return fmodf(a + M_PI, M_PI * 2.0) - M_PI; } } diff --git a/shared/commands.c b/shared/commands.c index 3e9ba638..c9a5e011 100644 --- a/shared/commands.c +++ b/shared/commands.c @@ -3,31 +3,31 @@ #include #include -uint32_t call_cmd(char * s){ - char c[64]; - char a[64]; - uint32_t foo = sscanf(s, " %[a-zA-Z_0-9] %[ -~]", c, a); - switch(foo){ - case 0: - return(0); - case 1: - a[0] = 0; - case 2: - for(uint32_t i = 0; i < sizeof(cmd) / sizeof(cmd_t); i++){ - if(!strcmp(cmd[i].name, c)){ - cmd[i].ptr(a); - return(1); - } - } - default: - return(0); - } - return(0); +uint32_t call_cmd(char *s) { + char c[64]; + char a[64]; + uint32_t foo = sscanf(s, " %[a-zA-Z_0-9] %[ -~]", c, a); + switch(foo) { + case 0: + return (0); + case 1: + a[0] = 0; + case 2: + for(uint32_t i = 0; i < sizeof(cmd) / sizeof(cmd_t); i++) { + if(!strcmp(cmd[i].name, c)) { + cmd[i].ptr(a); + return (1); + } + } + default: + return (0); + } + return (0); } -void listcmd(char * ptr){ - for(uint32_t i = 0; i < sizeof(cmd) / sizeof(cmd_t); i++){ - printf("%s: %s\n",cmd[i].name, cmd[i].doc); +void listcmd(char *ptr) { + for(uint32_t i = 0; i < sizeof(cmd) / sizeof(cmd_t); i++) { + printf("%s: %s\n", cmd[i].name, cmd[i].doc); } } diff --git a/shared/commands.h b/shared/commands.h index f46bdcb5..3e226ec9 100644 --- a/shared/commands.h +++ b/shared/commands.h @@ -1,12 +1,12 @@ #pragma once #include -#define COMMAND(c,p,d) +#define COMMAND(c, p, d) -uint32_t call_cmd(char * s); +uint32_t call_cmd(char *s); -typedef struct{ +typedef struct { char name[32]; - char * doc; + char *doc; void (*ptr)(char *); -}cmd_t; +} cmd_t; diff --git a/shared/common.h b/shared/common.h index d15a559d..5eba3ab0 100644 --- a/shared/common.h +++ b/shared/common.h @@ -2,87 +2,87 @@ #include #if __GNUC__ < 5 - #error gcc to old (< 5.0) +#error gcc to old (< 5.0) #endif -#define DATABAUD 3000000 //baudrate used for communication +#define DATABAUD 3000000 //baudrate used for communication #define PWM_RES 4800 //data from f3 to f4 #pragma pack(1) -typedef struct{ - float d_fb; - float q_fb; - float dc_volt; - float pwm_volt; - float value; - uint16_t addr; - union { - uint16_t fault : 1; - uint16_t foo; - } flags; - uint32_t crc; +typedef struct { + float d_fb; + float q_fb; + float dc_volt; + float pwm_volt; + float value; + uint16_t addr; + union { + uint16_t fault : 1; + uint16_t foo; + } flags; + uint32_t crc; } packet_from_hv_t; //data from f4 to f3 #pragma pack(1) -typedef struct{ - float d_cmd; - float q_cmd; - float pos; - float vel; - float value; - uint16_t addr; - union{ - struct{ - uint16_t enable : 1; - enum packet_to_hv_cmd_type_t{ - VOLT_MODE = 0, - CURRENT_MODE, - } cmd_type : 1; - enum packet_to_hv_phase_type_t{ - PHASE_90_3PH = 0, - PHASE_90_4PH, - PHASE_120_3PH, - PHASE_180_2PH, - PHASE_180_3PH, - } phase_type : 3; +typedef struct { + float d_cmd; + float q_cmd; + float pos; + float vel; + float value; + uint16_t addr; + union { + struct { + uint16_t enable : 1; + enum packet_to_hv_cmd_type_t { + VOLT_MODE = 0, + CURRENT_MODE, + } cmd_type : 1; + enum packet_to_hv_phase_type_t { + PHASE_90_3PH = 0, + PHASE_90_4PH, + PHASE_120_3PH, + PHASE_180_2PH, + PHASE_180_3PH, + } phase_type : 3; } flags; uint16_t foo; }; - uint32_t crc; + uint32_t crc; } packet_to_hv_t; #pragma pack(1) typedef union { - struct f3_config_data_temp{ - float r; - float l; - float psi; - float cur_p; - float cur_i; - float cur_ff; - float cur_ind; - float max_y; - float max_cur; - } pins; - float data[sizeof(struct f3_config_data_temp) / 4]; + struct f3_config_data_temp { + float r; + float l; + float psi; + float cur_p; + float cur_i; + float cur_ff; + float cur_ind; + float max_y; + float max_cur; + } pins; + float data[sizeof(struct f3_config_data_temp) / 4]; } f3_config_data_t; #pragma pack(1) typedef union { - struct f3_state_data_temp{ - float u_fb; - float v_fb; - float w_fb; - float hv_temp; - float mot_temp; - float core_temp; - float fault; - float y; - } pins; - float data[sizeof(struct f3_state_data_temp) / 4]; + struct f3_state_data_temp { + float u_fb; + float v_fb; + float w_fb; + float hv_temp; + float mot_temp; + float core_temp; + float fault; + float y; + } pins; + float data[sizeof(struct f3_state_data_temp) / 4]; } f3_state_data_t; //check if structs can be send at 5kHz using DATABAUD diff --git a/shared/comps/acim_ttc.c b/shared/comps/acim_ttc.c index 7e0a71f0..44bcc5dd 100644 --- a/shared/comps/acim_ttc.c +++ b/shared/comps/acim_ttc.c @@ -6,7 +6,7 @@ HAL_COMP(acim_ttc); -HAL_PIN(mode); // 0 = u/f, 1 = mtpa +HAL_PIN(mode); // 0 = u/f, 1 = mtpa // motor values HAL_PIN(torque_n); @@ -25,60 +25,60 @@ HAL_PIN(freq); HAL_PIN(slip) HAL_PIN(pos); -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - // struct acim_ttc_ctx_t * ctx = (struct acim_ttc_ctx_t *)ctx_ptr; - struct acim_ttc_pin_ctx_t * pins = (struct acim_ttc_pin_ctx_t *)pin_ptr; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct acim_ttc_ctx_t * ctx = (struct acim_ttc_ctx_t *)ctx_ptr; + struct acim_ttc_pin_ctx_t *pins = (struct acim_ttc_pin_ctx_t *)pin_ptr; - float poles = MAX(PIN(polecount), 1.0); - float torque = PIN(torque); - float vel = PIN(vel); - float slip_n = PIN(slip_n); - float torque_n = MAX(PIN(torque_n), 0.001); - float cur_n = PIN(cur_n); + float poles = MAX(PIN(polecount), 1.0); + float torque = PIN(torque); + float vel = PIN(vel); + float slip_n = PIN(slip_n); + float torque_n = MAX(PIN(torque_n), 0.001); + float cur_n = PIN(cur_n); - float id = 0.0; - float iq = 0.0; - float freq = vel * poles / 2.0 / M_PI; - float slip = 0.0; + float id = 0.0; + float iq = 0.0; + float freq = vel * poles / 2.0 / M_PI; + float slip = 0.0; - switch((int)PIN(mode)){ - case 0: // slip control - id = cur_n / sqrtf(2.0); // constant flux - iq = cur_n / sqrtf(2.0) / torque_n * torque; - slip = slip_n / torque_n * torque; + switch((int)PIN(mode)) { + case 0: // slip control + id = cur_n / sqrtf(2.0); // constant flux + iq = cur_n / sqrtf(2.0) / torque_n * torque; + slip = slip_n / torque_n * torque; break; - case 1: // mtpa - id = 0.0; - iq = cur_n / torque_n * torque; - slip = slip_n * SIGN(torque); // constant slip - break; + case 1: // mtpa + id = 0.0; + iq = cur_n / torque_n * torque; + slip = slip_n * SIGN(torque); // constant slip + break; - default: - id = 0; - iq = 0; - slip = 0.0; - } + default: + id = 0; + iq = 0; + slip = 0.0; + } - freq += slip; + freq += slip; - PIN(id) = id; - PIN(iq) = iq; - PIN(freq) = freq; - PIN(slip) = slip; - PIN(pos) = mod(PIN(pos) + freq * period * 2.0 * M_PI); + PIN(id) = id; + PIN(iq) = iq; + PIN(freq) = freq; + PIN(slip) = slip; + PIN(pos) = mod(PIN(pos) + freq * period * 2.0 * M_PI); } hal_comp_t acim_ttc_comp_struct = { - .name = "acim_ttc", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = 0, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = 0, - .pin_count = sizeof(struct acim_ttc_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "acim_ttc", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = 0, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct acim_ttc_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/curpid.c b/shared/comps/curpid.c index 837615d7..bf982638 100644 --- a/shared/comps/curpid.c +++ b/shared/comps/curpid.c @@ -36,117 +36,117 @@ HAL_PIN(lq); // torque constant HAL_PIN(psi); -HAL_PIN(ff); // r feed forward +HAL_PIN(ff); // r feed forward HAL_PIN(kp); HAL_PIN(ki); -HAL_PIN(kind); // bemf feed forward +HAL_PIN(kind); // bemf feed forward -HAL_PIN(vel); // velocity input +HAL_PIN(vel); // velocity input // current error outputs HAL_PIN(id_error); HAL_PIN(iq_error); -struct curpid_ctx_t{ +struct curpid_ctx_t { float id_error_sum; float iq_error_sum; }; -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { // struct curpid_ctx_t * ctx = (struct curpid_ctx_t *)ctx_ptr; - struct curpid_pin_ctx_t * pins = (struct curpid_pin_ctx_t *)pin_ptr; - PIN(rd) = 0.5; - PIN(rq) = 0.5; - PIN(ld) = 0.01; - PIN(lq) = 0.01; + struct curpid_pin_ctx_t *pins = (struct curpid_pin_ctx_t *)pin_ptr; + + PIN(rd) = 0.5; + PIN(rq) = 0.5; + PIN(ld) = 0.01; + PIN(lq) = 0.01; PIN(psi) = 0.05; - PIN(kp) = 0.1; - PIN(ki) = 0.005; + PIN(kp) = 0.1; + PIN(ki) = 0.005; } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct curpid_ctx_t * ctx = (struct curpid_ctx_t *)ctx_ptr; - struct curpid_pin_ctx_t * pins = (struct curpid_pin_ctx_t *)pin_ptr; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct curpid_ctx_t *ctx = (struct curpid_ctx_t *)ctx_ptr; + struct curpid_pin_ctx_t *pins = (struct curpid_pin_ctx_t *)pin_ptr; - float rd = MAX(PIN(rd), 0.1); - float rq = MAX(PIN(rq), 0.1); - float ld = MAX(PIN(ld), 0.001); - float lq = MAX(PIN(lq), 0.001); + float rd = MAX(PIN(rd), 0.1); + float rq = MAX(PIN(rq), 0.1); + float ld = MAX(PIN(ld), 0.001); + float lq = MAX(PIN(lq), 0.001); - float ff = PIN(ff); - float kind = PIN(kind); - float kpd = ld * PIN(kp) / period / 2.0; - float kid = rd * PIN(ki) / ld; - float kpq = lq * PIN(kp) / period / 2.0; - float kiq = rq * PIN(ki) / lq; - - //TODO curpid: sqrt(di^2+qi^2) auf max_ac_cur clampen - float max_cur = MIN(PIN(max_cur), 28.0); - float idc = LIMIT(PIN(id_cmd), max_cur); - float iqc = LIMIT(PIN(iq_cmd), max_cur); - - float max_volt = PIN(pwm_volt); + float ff = PIN(ff); + float kind = PIN(kind); + float kpd = ld * PIN(kp) / period / 2.0; + float kid = rd * PIN(ki) / ld; + float kpq = lq * PIN(kp) / period / 2.0; + float kiq = rq * PIN(ki) / lq; - float id = PIN(id_fb); - float iq = PIN(iq_fb); - - float vel = PIN(vel); - float psi_d = ld * id + PIN(psi); - float psi_q = lq * iq; - float indd = vel * psi_q; - float indq = vel * psi_d; + //TODO curpid: sqrt(di^2+qi^2) auf max_ac_cur clampen + float max_cur = MIN(PIN(max_cur), 28.0); + float idc = LIMIT(PIN(id_cmd), max_cur); + float iqc = LIMIT(PIN(iq_cmd), max_cur); - float id_error = idc - id; - float iq_error = iqc - iq; + float max_volt = PIN(pwm_volt); - float ud = LIMIT(ff * rd * idc - kind * indd + kpd * id_error, max_volt); - float uq = LIMIT(ff * rq * iqc + kind * indq + kpq * iq_error, max_volt); + float id = PIN(id_fb); + float iq = PIN(iq_fb); - if(kpd * kid > 0.0 && kpq * kiq > 0.0){ - ctx->id_error_sum = LIMIT(ctx->id_error_sum + kpd * kid * id_error, max_volt - ud); - ctx->iq_error_sum = LIMIT(ctx->iq_error_sum + kpq * kiq * iq_error, max_volt - uq); - } - else{ - ctx->id_error_sum = 0.0; - ctx->iq_error_sum = 0.0; - } + float vel = PIN(vel); + float psi_d = ld * id + PIN(psi); + float psi_q = lq * iq; + float indd = vel * psi_q; + float indq = vel * psi_d; - ud += ctx->id_error_sum; - uq += ctx->iq_error_sum; + float id_error = idc - id; + float iq_error = iqc - iq; - if(PIN(cmd_mode) == VOLT_MODE){ - ud = LIMIT(PIN(id_cmd), max_volt); - uq = LIMIT(PIN(iq_cmd), max_volt); + float ud = LIMIT(ff * rd * idc - kind * indd + kpd * id_error, max_volt); + float uq = LIMIT(ff * rq * iqc + kind * indq + kpq * iq_error, max_volt); + + if(kpd * kid > 0.0 && kpq * kiq > 0.0) { + ctx->id_error_sum = LIMIT(ctx->id_error_sum + kpd * kid * id_error, max_volt - ud); + ctx->iq_error_sum = LIMIT(ctx->iq_error_sum + kpq * kiq * iq_error, max_volt - uq); + } else { ctx->id_error_sum = 0.0; ctx->iq_error_sum = 0.0; - id_error = 0.0; - iq_error = 0.0; - } + } - if(PIN(en) <= 0.0){ - ud = 0.0; - uq = 0.0; - ctx->id_error_sum = 0.0; - ctx->iq_error_sum = 0.0; - } - - PIN(ud) = ud; - PIN(uq) = uq; + ud += ctx->id_error_sum; + uq += ctx->iq_error_sum; - PIN(id_error) = id_error; - PIN(iq_error) = iq_error; + if(PIN(cmd_mode) == VOLT_MODE) { + ud = LIMIT(PIN(id_cmd), max_volt); + uq = LIMIT(PIN(iq_cmd), max_volt); + ctx->id_error_sum = 0.0; + ctx->iq_error_sum = 0.0; + id_error = 0.0; + iq_error = 0.0; + } + + if(PIN(en) <= 0.0) { + ud = 0.0; + uq = 0.0; + ctx->id_error_sum = 0.0; + ctx->iq_error_sum = 0.0; + } + + PIN(ud) = ud; + PIN(uq) = uq; + + PIN(id_error) = id_error; + PIN(iq_error) = iq_error; } hal_comp_t curpid_comp_struct = { - .name = "curpid", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct curpid_ctx_t), - .pin_count = sizeof(struct curpid_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "curpid", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct curpid_ctx_t), + .pin_count = sizeof(struct curpid_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/dc.c b/shared/comps/dc.c index 39886ae1..2c4f739d 100644 --- a/shared/comps/dc.c +++ b/shared/comps/dc.c @@ -13,25 +13,25 @@ HAL_PIN(u); HAL_PIN(v); HAL_PIN(w); -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { // struct hv_ctx_t * ctx = (struct hv_ctx_t *)ctx_ptr; - struct dc_pin_ctx_t * pins = (struct dc_pin_ctx_t *)pin_ptr; - + struct dc_pin_ctx_t *pins = (struct dc_pin_ctx_t *)pin_ptr; + PIN(u) = PIN(uq) / 2.0; PIN(v) = -PIN(uq) / 2.0; PIN(w) = 0; } hal_comp_t dc_comp_struct = { - .name = "dc", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = 0, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = 0, - .pin_count = sizeof(struct dc_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "dc", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = 0, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct dc_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/dq.c b/shared/comps/dq.c index 86616d3a..4ed6ba86 100644 --- a/shared/comps/dq.c +++ b/shared/comps/dq.c @@ -16,7 +16,7 @@ HAL_PIN(w); //rotor position HAL_PIN(pos); -HAL_PIN(polecount);//1 +HAL_PIN(polecount); //1 //a,b,gamma output HAL_PIN(a); @@ -27,77 +27,77 @@ HAL_PIN(y); HAL_PIN(d); HAL_PIN(q); -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - // struct dq_ctx_t * ctx = (struct dq_ctx_t *)ctx_ptr; - struct dq_pin_ctx_t * pins = (struct dq_pin_ctx_t *)pin_ptr; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct dq_ctx_t * ctx = (struct dq_ctx_t *)ctx_ptr; + struct dq_pin_ctx_t *pins = (struct dq_pin_ctx_t *)pin_ptr; - float u = PIN(u); - float v = PIN(v); - float w = PIN(w); - - //clarke transformation + float u = PIN(u); + float v = PIN(v); + float w = PIN(w); + + //clarke transformation float a, b, y; - switch((int)PIN(mode)){ - case PHASE_90_3PH: // 90° + switch((int)PIN(mode)) { + case PHASE_90_3PH: // 90° a = u - v; b = w - v; y = u / 3.0 + v / 3.0 + w / 3.0; - break; + break; - case PHASE_120_3PH: // 120° + case PHASE_120_3PH: // 120° a = u * 2.0 / 3.0 - v / 3.0 - w / 3.0; b = v / M_SQRT3 - w / M_SQRT3; y = u / 3.0 + v / 3.0 + w / 3.0; - break; + break; - case PHASE_180_2PH: // 180° + case PHASE_180_2PH: // 180° a = 0; b = (u - w) / 2.0; y = (u + w) / 2.0; - break; - - case PHASE_180_3PH: // 180° + break; + + case PHASE_180_3PH: // 180° a = v; b = (u - w) / 2.0; y = (u + w) / 2.0; - break; + break; default: a = 0.0; b = 0.0; y = 0.0; - } - - float p = (int)MAX(PIN(polecount), 1.0); - float pos = PIN(pos) * p; + } - float si = 0.0; - float co = 0.0; - sincos_fast(pos, &si, &co); + float p = (int)MAX(PIN(polecount), 1.0); + float pos = PIN(pos) * p; - //park transformation - float d = a * co + b * si; - float q = - a * si + b * co; + float si = 0.0; + float co = 0.0; + sincos_fast(pos, &si, &co); - PIN(a) = a; - PIN(b) = b; - PIN(y) = y; + //park transformation + float d = a * co + b * si; + float q = -a * si + b * co; - PIN(d) = d; - PIN(q) = q; + PIN(a) = a; + PIN(b) = b; + PIN(y) = y; + + PIN(d) = d; + PIN(q) = q; } hal_comp_t dq_comp_struct = { - .name = "dq", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = 0, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = 0, - .pin_count = sizeof(struct dq_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "dq", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = 0, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct dq_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/fanuc.c b/shared/comps/fanuc.c index e3b0054d..63dcec76 100644 --- a/shared/comps/fanuc.c +++ b/shared/comps/fanuc.c @@ -15,43 +15,43 @@ HAL_PIN(C8); //rotor position output HAL_PIN(pos); -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - // struct fanuc_ctx_t * ctx = (struct fanuc_ctx_t *)ctx_ptr; - struct fanuc_pin_ctx_t * pins = (struct fanuc_pin_ctx_t *)pin_ptr; - - //TODO: const... - uint8_t tab[16]; - tab[0] = 0; - tab[1] = 15; - tab[2] = 1; - tab[3] = 2; - tab[4] = 13; - tab[5] = 14; - tab[6] = 12; - tab[7] = 11; - tab[8] = 5; - tab[9] = 6; - tab[10] = 4; - tab[11] = 3; - tab[12] = 8; - tab[13] = 7; - tab[14] = 9; - tab[15] = 10; - - uint32_t t = (PIN(C1) > 0.0) + (PIN(C2) > 0.0) * 2 + (PIN(C4) > 0.0) * 4 + (PIN(C8) > 0.0) * 8; - PIN(pos) = mod((tab[t] / 15.0 * 2.0 * M_PI)); +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct fanuc_ctx_t * ctx = (struct fanuc_ctx_t *)ctx_ptr; + struct fanuc_pin_ctx_t *pins = (struct fanuc_pin_ctx_t *)pin_ptr; + + //TODO: const... + uint8_t tab[16]; + tab[0] = 0; + tab[1] = 15; + tab[2] = 1; + tab[3] = 2; + tab[4] = 13; + tab[5] = 14; + tab[6] = 12; + tab[7] = 11; + tab[8] = 5; + tab[9] = 6; + tab[10] = 4; + tab[11] = 3; + tab[12] = 8; + tab[13] = 7; + tab[14] = 9; + tab[15] = 10; + + uint32_t t = (PIN(C1) > 0.0) + (PIN(C2) > 0.0) * 2 + (PIN(C4) > 0.0) * 4 + (PIN(C8) > 0.0) * 8; + PIN(pos) = mod((tab[t] / 15.0 * 2.0 * M_PI)); } hal_comp_t fanuc_comp_struct = { - .name = "fanuc", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = 0, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = 0, - .pin_count = sizeof(struct fanuc_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "fanuc", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = 0, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct fanuc_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/fault.c b/shared/comps/fault.c index 3909d50a..b3d36bc6 100644 --- a/shared/comps/fault.c +++ b/shared/comps/fault.c @@ -66,328 +66,328 @@ HAL_PIN(print); HAL_PIN(brake_release); -struct fault_ctx_t{ - state_t state; - fault_t fault; - uint32_t phased; - float cmd_error; - float mot_fb_error; - float com_fb_error; - float joint_fb_error; - float hv_error; - float hv_temp_error; - float dc_volt_error; - float mot_temp_error; +struct fault_ctx_t { + state_t state; + fault_t fault; + uint32_t phased; + float cmd_error; + float mot_fb_error; + float com_fb_error; + float joint_fb_error; + float hv_error; + float hv_temp_error; + float dc_volt_error; + float mot_temp_error; }; -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct fault_ctx_t * ctx = (struct fault_ctx_t *)ctx_ptr; - struct fault_pin_ctx_t * pins = (struct fault_pin_ctx_t *)pin_ptr; - ctx->state = DISABLED; - ctx->fault = NO_ERROR; - ctx->phased = 0; - PIN(phase_with_brake) = 1.0; - PIN(phase_on_start) = 1.0; - PIN(min_dc_volt) = 20.0; - PIN(high_dc_volt) = 370.0; - PIN(max_dc_volt) = 390.0; - PIN(max_hv_temp) = 90.0; - PIN(max_mot_temp) = 100.0; - PIN(high_hv_temp) = 70.0; - PIN(high_mot_temp) = 80.0; - PIN(fan_hv_temp) = 60.0; - PIN(fan_mot_temp) = 60.0; +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct fault_ctx_t *ctx = (struct fault_ctx_t *)ctx_ptr; + struct fault_pin_ctx_t *pins = (struct fault_pin_ctx_t *)pin_ptr; + + ctx->state = DISABLED; + ctx->fault = NO_ERROR; + ctx->phased = 0; + PIN(phase_with_brake) = 1.0; + PIN(phase_on_start) = 1.0; + PIN(min_dc_volt) = 20.0; + PIN(high_dc_volt) = 370.0; + PIN(max_dc_volt) = 390.0; + PIN(max_hv_temp) = 90.0; + PIN(max_mot_temp) = 100.0; + PIN(high_hv_temp) = 70.0; + PIN(high_mot_temp) = 80.0; + PIN(fan_hv_temp) = 60.0; + PIN(fan_mot_temp) = 60.0; } -float err_filter(float *ctx, float max, float dens, float err){ - if(err > 0.0){ +float err_filter(float *ctx, float max, float dens, float err) { + if(err > 0.0) { *ctx += 1.0; - } - else{ + } else { *ctx -= dens; } *ctx = CLAMP(*ctx, 0.0, max); - if(*ctx < max * 0.99){ - return(0.0); + if(*ctx < max * 0.99) { + return (0.0); } - return(1.0); + return (1.0); } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct fault_ctx_t * ctx = (struct fault_ctx_t *)ctx_ptr; - struct fault_pin_ctx_t * pins = (struct fault_pin_ctx_t *)pin_ptr; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct fault_ctx_t *ctx = (struct fault_ctx_t *)ctx_ptr; + struct fault_pin_ctx_t *pins = (struct fault_pin_ctx_t *)pin_ptr; - if(PIN(phase_on_start) <= 0.0){ - ctx->phased = 1; - } - - switch(ctx->state){ - case DISABLED: - if(PIN(en) > 0.0){ - if(PIN(rephase) > 0.0){ // TODO: check phase_on_start - ctx->phased = 0; - } - if(ctx->phased == 0){ - ctx->state = PHASING; - } - else{ - ctx->state = ENABLED; - PIN(start_offset) = minus(PIN(fb), PIN(cmd)); - } - } - break; - - case ENABLED: - if(PIN(en) <= 0.0){ - ctx->state = DISABLED; - } - break; - - case PHASING: - if(RISING_EDGE(PIN(phase_ready))){ - ctx->state = ENABLED; - PIN(start_offset) = minus(PIN(fb), PIN(cmd)); - } - - if(PIN(en) <= 0.0){ - ctx->state = DISABLED; - } - break; - - case SOFT_FAULT: - if(PIN(en) <= 0.0){ - ctx->state = DISABLED; - } - break; - - case LED_TEST: - case HARD_FAULT: - break; - } + if(PIN(phase_on_start) <= 0.0) { + ctx->phased = 1; + } - if(err_filter(&(ctx->cmd_error), 5.0, 0.001, PIN(cmd_error) > 0.0)){ - ctx->fault = CMD_ERROR; - ctx->state = SOFT_FAULT; - } - - if(err_filter(&(ctx->mot_fb_error), 5.0, 0.001, PIN(mot_fb_error) > 0.0)){ - ctx->fault = MOT_FB_ERROR; - ctx->state = SOFT_FAULT; - ctx->phased = 0; - } - - if(err_filter(&(ctx->com_fb_error), 5.0, 0.001, PIN(com_fb_error) > 0.0)){ - ctx->fault = COM_FB_ERROR; - ctx->state = SOFT_FAULT; - ctx->phased = 0; - } - - if(err_filter(&(ctx->joint_fb_error), 5.0, 0.001, PIN(joint_fb_error) > 0.0)){ - ctx->fault = JOINT_FB_ERROR; - ctx->state = SOFT_FAULT; - ctx->phased = 0; - } - - if(err_filter(&(ctx->hv_error), 3.0, 0.001, PIN(hv_error) > 0.0)){ - ctx->fault = HV_ERROR; - ctx->state = SOFT_FAULT; - } - - if(ABS(PIN(pos_error)) > PIN(max_pos_error)){ - ctx->fault = POS_ERROR; - ctx->state = SOFT_FAULT; - } - - if(PIN(sat) > PIN(max_sat)){ - ctx->fault = SAT_ERROR; - ctx->state = SOFT_FAULT; - } - - if(err_filter(&(ctx->hv_temp_error), 5.0, 0.001, PIN(hv_temp) > PIN(max_hv_temp))){ - ctx->fault = HV_TEMP_ERROR; - ctx->state = SOFT_FAULT; - } - - if(err_filter(&(ctx->dc_volt_error), 5.0, 0.001, PIN(dc_volt) > PIN(max_dc_volt) || PIN(dc_volt) < PIN(min_dc_volt))){ - ctx->fault = HV_VOLT_ERROR; - ctx->state = SOFT_FAULT; - } - - if(err_filter(&(ctx->mot_temp_error), 5.0, 0.001, PIN(mot_temp) > PIN(max_mot_temp))){ - ctx->fault = MOT_TEMP_ERROR; - ctx->state = SOFT_FAULT; - } - - float scale = 1.0; - scale = MIN(scale, SCALE(PIN(hv_temp), PIN(high_hv_temp), PIN(max_hv_temp))); - scale = MIN(scale, SCALE(PIN(dc_volt), PIN(high_dc_volt), PIN(max_dc_volt))); - scale = MIN(scale, SCALE(PIN(mot_temp), PIN(high_mot_temp), PIN(max_mot_temp))); - scale = MIN(scale, SCALE(PIN(dc_cur), PIN(max_dc_cur) * 0.8, PIN(max_dc_cur))); // TODO PIN(high_dc_cur) - - PIN(dc_brake) = SCALE(PIN(dc_volt), PIN(max_dc_volt), PIN(high_dc_volt)); - - if(PIN(hv_temp) >= PIN(fan_hv_temp)){ - PIN(hv_fan) = 1.0; - } - - if(PIN(hv_temp) < PIN(fan_hv_temp) * 0.9){ - PIN(hv_fan) = 0.0; - } - - if(PIN(mot_temp) >= PIN(fan_mot_temp)){ - PIN(mot_fan) = 1.0; - } - - if(PIN(mot_temp) < PIN(fan_mot_temp) * 0.9){ - PIN(mot_fan) = 0.0; - } - - switch(ctx->state){ - case DISABLED: - PIN(phase_start) = 0.0; - PIN(mot_brake) = 0.0; - PIN(en_out) = 0.0; - PIN(en_pid) = 0.0; - ctx->fault = NO_ERROR; - // scale = 0.0; - break; - - case ENABLED: - PIN(phase_start) = 0.0; - PIN(mot_brake) = 1.0; - PIN(en_out) = 1.0; - PIN(en_pid) = 1.0; - ctx->fault = NO_ERROR; - ctx->phased = 1; - break; - - case PHASING: - PIN(phase_start) = 1.0; - PIN(mot_brake) = PIN(phase_with_brake); - ctx->fault = NO_ERROR; - PIN(en_pid) = 0.0; - PIN(en_out) = 1.0; - break; - - case SOFT_FAULT: - PIN(phase_start) = 0.0; - PIN(mot_brake) = 0.0; - PIN(en_out) = 0.0; - PIN(en_pid) = 0.0; - // scale = 0.0; - break; - - case LED_TEST: - case HARD_FAULT: - PIN(phase_start) = 0.0; - PIN(mot_brake) = 0.0; - PIN(en_out) = 0.0; - PIN(en_pid) = 0.0; - // scale = 0.0; - break; - } - - PIN(fault) = ctx->fault; - PIN(state) = ctx->state; - PIN(scale) = scale; - - if(PIN(brake_release) > 0.0){ - PIN(mot_brake) = 1.0; - } -} - - -static void nrt_func(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ -struct fault_ctx_t * ctx = (struct fault_ctx_t *)ctx_ptr; -struct fault_pin_ctx_t * pins = (struct fault_pin_ctx_t *)pin_ptr; -//TODO: fix EDGE -if(EDGE(ctx->state) || PIN(print) > 0.0){ - PIN(print) = 0.0; - switch((state_t)ctx->state){ - case DISABLED: - printf("INFO: Disabled \n"); - break; - - case ENABLED: - printf("INFO: Enabled \n"); - break; - - case PHASING: - printf("INFO: Phasing \n"); - break; - - case SOFT_FAULT: - printf("ERROR: Soft fault: "); - switch((fault_t)ctx->fault){ - case NO_ERROR: - printf("no error\n"); - break; - - case CMD_ERROR: - printf("CMD error\n"); - break; - - case MOT_FB_ERROR: - printf("mot FB error\n"); - break; - - case COM_FB_ERROR: - printf("com FB error\n"); - break; - - case JOINT_FB_ERROR: - printf("com FB error\n"); - break; - - case POS_ERROR: - printf("position error\n"); - break; - - case SAT_ERROR: - printf("saturation error\n"); - break; - - case HV_ERROR: - printf("HV error\n"); - break; - - case HV_TEMP_ERROR: - printf("HV overtemperture\n"); - break; - - case HV_VOLT_ERROR: - printf("HV volt error\n"); - break; - - case MOT_TEMP_ERROR: - printf("Motor overtemperture\n"); - break; + switch(ctx->state) { + case DISABLED: + if(PIN(en) > 0.0) { + if(PIN(rephase) > 0.0) { // TODO: check phase_on_start + ctx->phased = 0; + } + if(ctx->phased == 0) { + ctx->state = PHASING; + } else { + ctx->state = ENABLED; + PIN(start_offset) = minus(PIN(fb), PIN(cmd)); + } } break; - case HARD_FAULT: - printf("ERROR: Hard fault: \n"); + case ENABLED: + if(PIN(en) <= 0.0) { + ctx->state = DISABLED; + } break; - - default: + + case PHASING: + if(RISING_EDGE(PIN(phase_ready))) { + ctx->state = ENABLED; + PIN(start_offset) = minus(PIN(fb), PIN(cmd)); + } + + if(PIN(en) <= 0.0) { + ctx->state = DISABLED; + } break; - } + + case SOFT_FAULT: + if(PIN(en) <= 0.0) { + ctx->state = DISABLED; + } + break; + + case LED_TEST: + case HARD_FAULT: + break; + } + + if(err_filter(&(ctx->cmd_error), 5.0, 0.001, PIN(cmd_error) > 0.0)) { + ctx->fault = CMD_ERROR; + ctx->state = SOFT_FAULT; + } + + if(err_filter(&(ctx->mot_fb_error), 5.0, 0.001, PIN(mot_fb_error) > 0.0)) { + ctx->fault = MOT_FB_ERROR; + ctx->state = SOFT_FAULT; + ctx->phased = 0; + } + + if(err_filter(&(ctx->com_fb_error), 5.0, 0.001, PIN(com_fb_error) > 0.0)) { + ctx->fault = COM_FB_ERROR; + ctx->state = SOFT_FAULT; + ctx->phased = 0; + } + + if(err_filter(&(ctx->joint_fb_error), 5.0, 0.001, PIN(joint_fb_error) > 0.0)) { + ctx->fault = JOINT_FB_ERROR; + ctx->state = SOFT_FAULT; + ctx->phased = 0; + } + + if(err_filter(&(ctx->hv_error), 3.0, 0.001, PIN(hv_error) > 0.0)) { + ctx->fault = HV_ERROR; + ctx->state = SOFT_FAULT; + } + + if(ABS(PIN(pos_error)) > PIN(max_pos_error)) { + ctx->fault = POS_ERROR; + ctx->state = SOFT_FAULT; + } + + if(PIN(sat) > PIN(max_sat)) { + ctx->fault = SAT_ERROR; + ctx->state = SOFT_FAULT; + } + + if(err_filter(&(ctx->hv_temp_error), 5.0, 0.001, PIN(hv_temp) > PIN(max_hv_temp))) { + ctx->fault = HV_TEMP_ERROR; + ctx->state = SOFT_FAULT; + } + + if(err_filter(&(ctx->dc_volt_error), 5.0, 0.001, PIN(dc_volt) > PIN(max_dc_volt) || PIN(dc_volt) < PIN(min_dc_volt))) { + ctx->fault = HV_VOLT_ERROR; + ctx->state = SOFT_FAULT; + } + + if(err_filter(&(ctx->mot_temp_error), 5.0, 0.001, PIN(mot_temp) > PIN(max_mot_temp))) { + ctx->fault = MOT_TEMP_ERROR; + ctx->state = SOFT_FAULT; + } + + float scale = 1.0; + scale = MIN(scale, SCALE(PIN(hv_temp), PIN(high_hv_temp), PIN(max_hv_temp))); + scale = MIN(scale, SCALE(PIN(dc_volt), PIN(high_dc_volt), PIN(max_dc_volt))); + scale = MIN(scale, SCALE(PIN(mot_temp), PIN(high_mot_temp), PIN(max_mot_temp))); + scale = MIN(scale, SCALE(PIN(dc_cur), PIN(max_dc_cur) * 0.8, PIN(max_dc_cur))); // TODO PIN(high_dc_cur) + + PIN(dc_brake) = SCALE(PIN(dc_volt), PIN(max_dc_volt), PIN(high_dc_volt)); + + if(PIN(hv_temp) >= PIN(fan_hv_temp)) { + PIN(hv_fan) = 1.0; + } + + if(PIN(hv_temp) < PIN(fan_hv_temp) * 0.9) { + PIN(hv_fan) = 0.0; + } + + if(PIN(mot_temp) >= PIN(fan_mot_temp)) { + PIN(mot_fan) = 1.0; + } + + if(PIN(mot_temp) < PIN(fan_mot_temp) * 0.9) { + PIN(mot_fan) = 0.0; + } + + switch(ctx->state) { + case DISABLED: + PIN(phase_start) = 0.0; + PIN(mot_brake) = 0.0; + PIN(en_out) = 0.0; + PIN(en_pid) = 0.0; + ctx->fault = NO_ERROR; + // scale = 0.0; + break; + + case ENABLED: + PIN(phase_start) = 0.0; + PIN(mot_brake) = 1.0; + PIN(en_out) = 1.0; + PIN(en_pid) = 1.0; + ctx->fault = NO_ERROR; + ctx->phased = 1; + break; + + case PHASING: + PIN(phase_start) = 1.0; + PIN(mot_brake) = PIN(phase_with_brake); + ctx->fault = NO_ERROR; + PIN(en_pid) = 0.0; + PIN(en_out) = 1.0; + break; + + case SOFT_FAULT: + PIN(phase_start) = 0.0; + PIN(mot_brake) = 0.0; + PIN(en_out) = 0.0; + PIN(en_pid) = 0.0; + // scale = 0.0; + break; + + case LED_TEST: + case HARD_FAULT: + PIN(phase_start) = 0.0; + PIN(mot_brake) = 0.0; + PIN(en_out) = 0.0; + PIN(en_pid) = 0.0; + // scale = 0.0; + break; + } + + PIN(fault) = ctx->fault; + PIN(state) = ctx->state; + PIN(scale) = scale; + + if(PIN(brake_release) > 0.0) { + PIN(mot_brake) = 1.0; + } } + + +static void nrt_func(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct fault_ctx_t *ctx = (struct fault_ctx_t *)ctx_ptr; + struct fault_pin_ctx_t *pins = (struct fault_pin_ctx_t *)pin_ptr; + + //TODO: fix EDGE + if(EDGE(ctx->state) || PIN(print) > 0.0) { + PIN(print) = 0.0; + switch((state_t)ctx->state) { + case DISABLED: + printf("INFO: Disabled \n"); + break; + + case ENABLED: + printf("INFO: Enabled \n"); + break; + + case PHASING: + printf("INFO: Phasing \n"); + break; + + case SOFT_FAULT: + printf("ERROR: Soft fault: "); + switch((fault_t)ctx->fault) { + case NO_ERROR: + printf("no error\n"); + break; + + case CMD_ERROR: + printf("CMD error\n"); + break; + + case MOT_FB_ERROR: + printf("mot FB error\n"); + break; + + case COM_FB_ERROR: + printf("com FB error\n"); + break; + + case JOINT_FB_ERROR: + printf("com FB error\n"); + break; + + case POS_ERROR: + printf("position error\n"); + break; + + case SAT_ERROR: + printf("saturation error\n"); + break; + + case HV_ERROR: + printf("HV error\n"); + break; + + case HV_TEMP_ERROR: + printf("HV overtemperture\n"); + break; + + case HV_VOLT_ERROR: + printf("HV volt error\n"); + break; + + case MOT_TEMP_ERROR: + printf("Motor overtemperture\n"); + break; + } + break; + + case HARD_FAULT: + printf("ERROR: Hard fault: \n"); + break; + + default: + break; + } + } } hal_comp_t fault_comp_struct = { - .name = "fault", - .nrt = nrt_func, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct fault_ctx_t), - .pin_count = sizeof(struct fault_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "fault", + .nrt = nrt_func, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct fault_ctx_t), + .pin_count = sizeof(struct fault_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/fb_switch.c b/shared/comps/fb_switch.c index 63950e07..7628ac84 100644 --- a/shared/comps/fb_switch.c +++ b/shared/comps/fb_switch.c @@ -12,7 +12,7 @@ HAL_PIN(pos_fb); HAL_PIN(vel_fb); HAL_PIN(com_fb); HAL_PIN(joint_fb); -HAL_PIN(state); // 0 = disabled, 1 = enabled +HAL_PIN(state); // 0 = disabled, 1 = enabled HAL_PIN(cmd_pos); @@ -20,7 +20,7 @@ HAL_PIN(mot_pos); HAL_PIN(mot_abs_pos); HAL_PIN(mot_polecount); HAL_PIN(mot_offset); -HAL_PIN(mot_state); // 0 = disabled, 1 = inc, 2 = start abs, 3 = abs +HAL_PIN(mot_state); // 0 = disabled, 1 = inc, 2 = start abs, 3 = abs HAL_PIN(com_pos); HAL_PIN(com_abs_pos); @@ -43,105 +43,103 @@ HAL_PIN(current_com_pos); HAL_PIN(en); -struct fb_switch_ctx_t{ - int32_t current_com_pos; - float cmd_offset; - float com_offset; +struct fb_switch_ctx_t { + int32_t current_com_pos; + float cmd_offset; + float com_offset; }; -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct fb_switch_ctx_t * ctx = (struct fb_switch_ctx_t *)ctx_ptr; - // struct fb_switch_pin_ctx_t * pins = (struct fb_switch_pin_ctx_t *)pin_ptr; - ctx->current_com_pos = 10; - ctx->cmd_offset = 0.0; - ctx->com_offset = 0.0; +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct fb_switch_ctx_t *ctx = (struct fb_switch_ctx_t *)ctx_ptr; + // struct fb_switch_pin_ctx_t * pins = (struct fb_switch_pin_ctx_t *)pin_ptr; + + ctx->current_com_pos = 10; + ctx->cmd_offset = 0.0; + ctx->com_offset = 0.0; } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct fb_switch_ctx_t * ctx = (struct fb_switch_ctx_t *)ctx_ptr; - struct fb_switch_pin_ctx_t * pins = (struct fb_switch_pin_ctx_t *)pin_ptr; - float mot_pos = PIN(mot_pos); - PIN(pos_fb) = mod(mot_pos + ctx->cmd_offset); - PIN(vel_fb) = mot_pos; - - if(PIN(en) <= 0.0){ +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct fb_switch_ctx_t *ctx = (struct fb_switch_ctx_t *)ctx_ptr; + struct fb_switch_pin_ctx_t *pins = (struct fb_switch_pin_ctx_t *)pin_ptr; + + float mot_pos = PIN(mot_pos); + PIN(pos_fb) = mod(mot_pos + ctx->cmd_offset); + PIN(vel_fb) = mot_pos; + + if(PIN(en) <= 0.0) { + PIN(state) = 0.0; + ctx->current_com_pos = 10; + ctx->com_offset = 0.0; + ctx->cmd_offset = minus(PIN(cmd_pos), mot_pos); + PIN(pos_fb) = mod(mot_pos); + } else { + PIN(state) = 1.0; + if(PIN(joint_state) >= 2.0 && ctx->current_com_pos > 3.0) { + ctx->current_com_pos = 3; + ctx->com_offset = minus(mod((PIN(joint_abs_pos) + PIN(joint_offset)) * PIN(polecount) / PIN(mot_joint_ratio)), mot_pos * PIN(polecount) / PIN(mot_polecount)); + } + if(PIN(com_state) >= 2.0 && ctx->current_com_pos > 2.0) { + ctx->current_com_pos = 2; + ctx->com_offset = minus(mod((PIN(com_abs_pos) + PIN(com_offset)) * PIN(polecount) / PIN(com_polecount)), mot_pos * PIN(polecount) / PIN(mot_polecount)); + } + if(PIN(mot_state) >= 2.0 && ctx->current_com_pos > 1.0) { + ctx->current_com_pos = 1; + ctx->com_offset = 0.0; + } + if(ctx->current_com_pos > 4.0) { + ctx->current_com_pos = 4.0; + // TODO cauto + // ctx->com_offset = cauto PIN(state) = 0.0; - ctx->current_com_pos = 10; - ctx->com_offset = 0.0; - ctx->cmd_offset = minus(PIN(cmd_pos), mot_pos); - PIN(pos_fb) = mod(mot_pos); - } - else{ - PIN(state) = 1.0; - if(PIN(joint_state) >= 2.0 && ctx->current_com_pos > 3.0){ - ctx->current_com_pos = 3; - ctx->com_offset = minus(mod((PIN(joint_abs_pos) + PIN(joint_offset)) * PIN(polecount) / PIN(mot_joint_ratio)), mot_pos * PIN(polecount) / PIN(mot_polecount)); - } - if(PIN(com_state) >= 2.0 && ctx->current_com_pos > 2.0){ - ctx->current_com_pos = 2; - ctx->com_offset = minus(mod((PIN(com_abs_pos) + PIN(com_offset)) * PIN(polecount) / PIN(com_polecount)), mot_pos * PIN(polecount) / PIN(mot_polecount)); - } - if(PIN(mot_state) >= 2.0 && ctx->current_com_pos > 1.0){ - ctx->current_com_pos = 1; - ctx->com_offset = 0.0; - } - if(ctx->current_com_pos > 4.0){ - ctx->current_com_pos = 4.0; - // TODO cauto - // ctx->com_offset = cauto - PIN(state) = 0.0; - } - - PIN(current_com_pos) = ctx->current_com_pos; - - switch(ctx->current_com_pos){ - case 4: - PIN(com_fb) = mod((mot_pos + ctx->com_offset) * PIN(polecount) / PIN(mot_polecount)); - break; - - case 3: - if(PIN(joint_state) != 3.0){ - PIN(com_fb) = mod((mot_pos + ctx->com_offset) * PIN(polecount) / PIN(mot_polecount)); - } - else{ - PIN(com_fb) = mod((PIN(joint_abs_pos) + PIN(joint_offset)) * PIN(polecount)); - } - break; - - case 2: - if(PIN(com_state) != 3.0){ - PIN(com_fb) = mod((mot_pos + ctx->com_offset) * PIN(polecount) / PIN(mot_polecount)); - } - else{ - PIN(com_fb) = mod((PIN(com_abs_pos) + PIN(com_offset)) * PIN(polecount) / PIN(com_polecount)); - } - break; - - case 1: - if(PIN(mot_state) != 3.0){ - PIN(state) = 0.0; - } - else{ - PIN(com_fb) = mod((PIN(mot_abs_pos) + PIN(mot_offset)) * PIN(polecount) / PIN(mot_polecount)); - } - break; - - default: - PIN(state) = 0.0; - } - } + } + + PIN(current_com_pos) = ctx->current_com_pos; + + switch(ctx->current_com_pos) { + case 4: + PIN(com_fb) = mod((mot_pos + ctx->com_offset) * PIN(polecount) / PIN(mot_polecount)); + break; + + case 3: + if(PIN(joint_state) != 3.0) { + PIN(com_fb) = mod((mot_pos + ctx->com_offset) * PIN(polecount) / PIN(mot_polecount)); + } else { + PIN(com_fb) = mod((PIN(joint_abs_pos) + PIN(joint_offset)) * PIN(polecount)); + } + break; + + case 2: + if(PIN(com_state) != 3.0) { + PIN(com_fb) = mod((mot_pos + ctx->com_offset) * PIN(polecount) / PIN(mot_polecount)); + } else { + PIN(com_fb) = mod((PIN(com_abs_pos) + PIN(com_offset)) * PIN(polecount) / PIN(com_polecount)); + } + break; + + case 1: + if(PIN(mot_state) != 3.0) { + PIN(state) = 0.0; + } else { + PIN(com_fb) = mod((PIN(mot_abs_pos) + PIN(mot_offset)) * PIN(polecount) / PIN(mot_polecount)); + } + break; + + default: + PIN(state) = 0.0; + } + } } hal_comp_t fb_switch_comp_struct = { - .name = "fb_switch", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct fb_switch_ctx_t), - .pin_count = sizeof(struct fb_switch_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "fb_switch", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct fb_switch_ctx_t), + .pin_count = sizeof(struct fb_switch_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/hal_test.c b/shared/comps/hal_test.c index 32be3293..6ff9ae09 100644 --- a/shared/comps/hal_test.c +++ b/shared/comps/hal_test.c @@ -5,38 +5,38 @@ HAL_COMP(hal_test); HAL_PIN(rt_wait); HAL_PIN(frt_wait); -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - // struct rev_ctx_t * ctx = (struct rev_ctx_t *)ctx_ptr; - struct hal_test_pin_ctx_t * pins = (struct hal_test_pin_ctx_t *)pin_ptr; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct rev_ctx_t * ctx = (struct rev_ctx_t *)ctx_ptr; + struct hal_test_pin_ctx_t *pins = (struct hal_test_pin_ctx_t *)pin_ptr; - volatile uint32_t foo = 0; - (void)foo; - for(uint32_t i = 0; i < PIN(rt_wait); i++){ - foo = 1; - } + volatile uint32_t foo = 0; + (void)foo; + for(uint32_t i = 0; i < PIN(rt_wait); i++) { + foo = 1; + } } -static void frt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - // struct rev_ctx_t * ctx = (struct rev_ctx_t *)ctx_ptr; - struct hal_test_pin_ctx_t * pins = (struct hal_test_pin_ctx_t *)pin_ptr; +static void frt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct rev_ctx_t * ctx = (struct rev_ctx_t *)ctx_ptr; + struct hal_test_pin_ctx_t *pins = (struct hal_test_pin_ctx_t *)pin_ptr; - volatile uint32_t foo = 0; - (void)foo; - for(uint32_t i = 0; i < PIN(frt_wait); i++){ - foo = 1; - } + volatile uint32_t foo = 0; + (void)foo; + for(uint32_t i = 0; i < PIN(frt_wait); i++) { + foo = 1; + } } hal_comp_t hal_test_comp_struct = { - .name = "hal_test", - .nrt = 0, - .rt = rt_func, - .frt = frt_func, - .nrt_init = 0, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = 0, - .pin_count = sizeof(struct hal_test_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "hal_test", + .nrt = 0, + .rt = rt_func, + .frt = frt_func, + .nrt_init = 0, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct hal_test_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/idq.c b/shared/comps/idq.c index 8f339c7b..adb2ef33 100644 --- a/shared/comps/idq.c +++ b/shared/comps/idq.c @@ -15,7 +15,7 @@ HAL_PIN(q); //rotor position HAL_PIN(pos); -HAL_PIN(polecount);//1 +HAL_PIN(polecount); //1 //a,b output HAL_PIN(a); @@ -26,14 +26,14 @@ HAL_PIN(u); HAL_PIN(v); HAL_PIN(w); -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - // struct idq_ctx_t * ctx = (struct idq_ctx_t *)ctx_ptr; - struct idq_pin_ctx_t * pins = (struct idq_pin_ctx_t *)pin_ptr; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct idq_ctx_t * ctx = (struct idq_ctx_t *)ctx_ptr; + struct idq_pin_ctx_t *pins = (struct idq_pin_ctx_t *)pin_ptr; float d = PIN(d); float q = PIN(q); - float p = (int)MAX(PIN(polecount), 1.0); + float p = (int)MAX(PIN(polecount), 1.0); float pos = PIN(pos) * p; float si = 0.0; @@ -43,41 +43,41 @@ static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst //inverse park transformation float a = d * co - q * si; float b = d * si + q * co; - + //inverse clarke transformation float u, v, w; - switch((int)PIN(mode)){ - case PHASE_90_3PH: // 90° + switch((int)PIN(mode)) { + case PHASE_90_3PH: // 90° u = a; v = 0.0; w = b; break; - case PHASE_120_3PH: // 120° + case PHASE_120_3PH: // 120° u = a; - v = - a / 2.0 + b / 2.0 * M_SQRT3; - w = - a / 2.0 - b / 2.0 * M_SQRT3; + v = -a / 2.0 + b / 2.0 * M_SQRT3; + w = -a / 2.0 - b / 2.0 * M_SQRT3; break; - case PHASE_180_2PH: // 180° + case PHASE_180_2PH: // 180° u = b / 2.0; v = 0.0; - w = - b / 2.0; + w = -b / 2.0; break; - case PHASE_180_3PH: // 180° + case PHASE_180_3PH: // 180° u = b / 2.0; v = a; - w = - b / 2.0; + w = -b / 2.0; break; - + default: u = 0.0; v = 0.0; w = 0.0; - } - + } + PIN(a) = a; PIN(b) = b; @@ -88,15 +88,15 @@ static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst } hal_comp_t idq_comp_struct = { - .name = "idq", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = 0, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = 0, - .pin_count = sizeof(struct idq_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "idq", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = 0, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct idq_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/iit.c b/shared/comps/iit.c index ee94cc97..4ac14eb8 100644 --- a/shared/comps/iit.c +++ b/shared/comps/iit.c @@ -23,67 +23,69 @@ HAL_PIN(id); HAL_PIN(iq); HAL_PIN(flow); -struct iit_ctx_t{ - float mot_temp; +struct iit_ctx_t { + float mot_temp; }; -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct iit_ctx_t * ctx = (struct iit_ctx_t *)ctx_ptr; - struct iit_pin_ctx_t * pins = (struct iit_pin_ctx_t *)pin_ptr; - ctx->mot_temp = 25.0; - PIN(brake_r) = 50.0; - PIN(brake) = 2.0; - PIN(air_temp) = 20.0; - PIN(mot_air_res) = 0.0; - PIN(mot_cap) = 0.0; - PIN(mot_tau) = 300.0; - PIN(mot_temp) = 25.0; - PIN(mot_max_temp) = 130.0; - PIN(mot_res) = 1.0; - PIN(mot_psi) = 1.0; - PIN(mot_real_res) = 1.0; - PIN(mot_real_psi) = 1.0; - PIN(mot_cont_cur) = 2.15; - PIN(id) = 0.0; - PIN(iq) = 0.0; - PIN(flow) = 0.0; +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct iit_ctx_t *ctx = (struct iit_ctx_t *)ctx_ptr; + struct iit_pin_ctx_t *pins = (struct iit_pin_ctx_t *)pin_ptr; + ctx->mot_temp = 25.0; + PIN(brake_r) = 50.0; + PIN(brake) = 2.0; + PIN(air_temp) = 20.0; + PIN(mot_air_res) = 0.0; + PIN(mot_cap) = 0.0; + PIN(mot_tau) = 300.0; + PIN(mot_temp) = 25.0; + PIN(mot_max_temp) = 130.0; + PIN(mot_res) = 1.0; + PIN(mot_psi) = 1.0; + PIN(mot_real_res) = 1.0; + PIN(mot_real_psi) = 1.0; + PIN(mot_cont_cur) = 2.15; + PIN(id) = 0.0; + PIN(iq) = 0.0; + PIN(flow) = 0.0; } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct iit_ctx_t * ctx = (struct iit_ctx_t *)ctx_ptr; - struct iit_pin_ctx_t * pins = (struct iit_pin_ctx_t *)pin_ptr; - - float mot_real_res = PIN(mot_res) * (1 + 0.004 * (ctx->mot_temp - PIN(air_temp))); - - float mot_max_res = PIN(mot_res) * (1 + 0.004 * (PIN(mot_max_temp) - PIN(air_temp))); - float mot_air_res = (PIN(mot_max_temp) - PIN(air_temp)) / (PIN(mot_cont_cur) * PIN(mot_cont_cur) * mot_max_res);; - float mot_cap = PIN(mot_tau) / mot_air_res; - - float brake_flow = 24.0 * 24.0 / PIN(brake_r) * PIN(brake); - float flow = (PIN(id) * PIN(id) + PIN(iq) * PIN(iq)) * 3.0 / 2.0 * mot_real_res - (ctx->mot_temp - PIN(air_temp)) / mot_air_res; - - flow += brake_flow; - - ctx->mot_temp += flow / mot_cap * period; - - PIN(flow) = flow; - PIN(mot_air_res) = mot_air_res; - PIN(mot_cap) = mot_cap; - PIN(mot_temp) = ctx->mot_temp; - PIN(mot_real_res) = mot_real_res; - PIN(mot_real_psi) = PIN(mot_psi) * (1 - 0.0013 * (ctx->mot_temp - PIN(air_temp)));; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct iit_ctx_t *ctx = (struct iit_ctx_t *)ctx_ptr; + struct iit_pin_ctx_t *pins = (struct iit_pin_ctx_t *)pin_ptr; + + float mot_real_res = PIN(mot_res) * (1 + 0.004 * (ctx->mot_temp - PIN(air_temp))); + + float mot_max_res = PIN(mot_res) * (1 + 0.004 * (PIN(mot_max_temp) - PIN(air_temp))); + float mot_air_res = (PIN(mot_max_temp) - PIN(air_temp)) / (PIN(mot_cont_cur) * PIN(mot_cont_cur) * mot_max_res); + ; + float mot_cap = PIN(mot_tau) / mot_air_res; + + float brake_flow = 24.0 * 24.0 / PIN(brake_r) * PIN(brake); + float flow = (PIN(id) * PIN(id) + PIN(iq) * PIN(iq)) * 3.0 / 2.0 * mot_real_res - (ctx->mot_temp - PIN(air_temp)) / mot_air_res; + + flow += brake_flow; + + ctx->mot_temp += flow / mot_cap * period; + + PIN(flow) = flow; + PIN(mot_air_res) = mot_air_res; + PIN(mot_cap) = mot_cap; + PIN(mot_temp) = ctx->mot_temp; + PIN(mot_real_res) = mot_real_res; + PIN(mot_real_psi) = PIN(mot_psi) * (1 - 0.0013 * (ctx->mot_temp - PIN(air_temp))); + ; } hal_comp_t iit_comp_struct = { - .name = "iit", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct iit_ctx_t), - .pin_count = sizeof(struct iit_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "iit", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct iit_ctx_t), + .pin_count = sizeof(struct iit_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/linrev.c b/shared/comps/linrev.c index 6b0fe0d6..e6119ad4 100644 --- a/shared/comps/linrev.c +++ b/shared/comps/linrev.c @@ -30,57 +30,56 @@ HAL_PIN(cmd_d_out); HAL_PIN(fb_in); HAL_PIN(fb_out); -struct linrev_ctx_t{ - uint8_t lastq;//last quadrant - int32_t rev;//current multiturn +struct linrev_ctx_t { + uint8_t lastq; //last quadrant + int32_t rev; //current multiturn }; -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct linrev_ctx_t * ctx = (struct linrev_ctx_t *)ctx_ptr; - struct linrev_pin_ctx_t * pins = (struct linrev_pin_ctx_t *)pin_ptr; - - float s = PIN(scale); - if(s < 0.01){ - s = 0.01; - } +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct linrev_ctx_t *ctx = (struct linrev_ctx_t *)ctx_ptr; + struct linrev_pin_ctx_t *pins = (struct linrev_pin_ctx_t *)pin_ptr; - PIN(cmd_out) = mod((PIN(cmd_in) / s) * 2.0 * M_PI); - PIN(cmd_d_out) = PIN(cmd_d_in) / s * 2.0 * M_PI; + float s = PIN(scale); + if(s < 0.01) { + s = 0.01; + } - uint8_t q = 0; + PIN(cmd_out) = mod((PIN(cmd_in) / s) * 2.0 * M_PI); + PIN(cmd_d_out) = PIN(cmd_d_in) / s * 2.0 * M_PI; - if(PIN(fb_in) > M_PI/2.0){ - q = 2; - } - if(PIN(fb_in) < -M_PI/2.0){ - q = 3; - } + uint8_t q = 0; - if(q != 0 && q == 3 && ctx->lastq == 2){ - ctx->rev++; - } + if(PIN(fb_in) > M_PI / 2.0) { + q = 2; + } + if(PIN(fb_in) < -M_PI / 2.0) { + q = 3; + } - if(q != 0 && q == 2 && ctx->lastq == 3){ - ctx->rev--; - } + if(q != 0 && q == 3 && ctx->lastq == 2) { + ctx->rev++; + } - ctx->lastq = q; + if(q != 0 && q == 2 && ctx->lastq == 3) { + ctx->rev--; + } - PIN(fb_out) = ((PIN(fb_in) + ctx->rev * M_PI * 2.0) * s) / (2.0 * M_PI); + ctx->lastq = q; + PIN(fb_out) = ((PIN(fb_in) + ctx->rev * M_PI * 2.0) * s) / (2.0 * M_PI); } const hal_comp_t linrev_comp_struct = { - .name = "linrev", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = 0, - .hw_init = 0, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct linrev_ctx_t), - .pin_count = sizeof(struct linrev_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "linrev", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = 0, + .hw_init = 0, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct linrev_ctx_t), + .pin_count = sizeof(struct linrev_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/pe.c b/shared/comps/pe.c index fad910d9..065200c7 100644 --- a/shared/comps/pe.c +++ b/shared/comps/pe.c @@ -25,35 +25,35 @@ HAL_PIN(p_el_ac); HAL_PIN(p_m); HAL_PIN(p_t); -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { // struct pe_ctx_t * ctx = (struct pe_ctx_t *)ctx_ptr; - struct pe_pin_ctx_t * pins = (struct pe_pin_ctx_t *)pin_ptr; - + struct pe_pin_ctx_t *pins = (struct pe_pin_ctx_t *)pin_ptr; + float udc = PIN(udc); float vel = PIN(vel); - float id = PIN(id); - float iq = PIN(iq); - float t = PIN(torque); + float id = PIN(id); + float iq = PIN(iq); + float t = PIN(torque); - PIN(e_el) = PIN(cap) * udc * udc / 2.0; + PIN(e_el) = PIN(cap) * udc * udc / 2.0; PIN(e_kin) = PIN(j) * vel * vel / 2.0; PIN(p_el_dc) = udc * PIN(idc); PIN(p_el_ac) = PIN(ud) * PIN(id) + PIN(ud) * PIN(id); - PIN(p_m) = t * vel; - PIN(p_t) = 3/2 * PIN(r) * (id * id + iq * iq); + PIN(p_m) = t * vel; + PIN(p_t) = 3 / 2 * PIN(r) * (id * id + iq * iq); } hal_comp_t pe_comp_struct = { - .name = "pe", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = 0, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = 0, - .pin_count = sizeof(struct pe_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "pe", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = 0, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct pe_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/pid.c b/shared/comps/pid.c index ab8a9a69..407ada62 100644 --- a/shared/comps/pid.c +++ b/shared/comps/pid.c @@ -26,26 +26,26 @@ HAL_COMP(pid); -HAL_PIN(pos_ext_cmd); // cmd in (rad) -HAL_PIN(pos_fb); // feedback in (rad) -HAL_PIN(pos_error); // error out (rad) +HAL_PIN(pos_ext_cmd); // cmd in (rad) +HAL_PIN(pos_fb); // feedback in (rad) +HAL_PIN(pos_error); // error out (rad) -HAL_PIN(vel_ext_cmd); // cmd in (rad/s) -HAL_PIN(vel_fb); // feedback in (rad/s) -HAL_PIN(vel_cmd); // cmd out (rad/s) -HAL_PIN(vel_error); // error out (rad/s) +HAL_PIN(vel_ext_cmd); // cmd in (rad/s) +HAL_PIN(vel_fb); // feedback in (rad/s) +HAL_PIN(vel_cmd); // cmd out (rad/s) +HAL_PIN(vel_error); // error out (rad/s) -HAL_PIN(acc_ext_cmd); // cmd in (rad/s^2) -HAL_PIN(acc_cmd); // cmd out (rad/s^2) +HAL_PIN(acc_ext_cmd); // cmd in (rad/s^2) +HAL_PIN(acc_cmd); // cmd out (rad/s^2) -HAL_PIN(torque_ext_cmd); // cmd in (Nm) -HAL_PIN(torque_cmd); // cmd out (Nm) -HAL_PIN(torque_cor_cmd); // corrected cmd out (Nm) -HAL_PIN(torque_sum); +HAL_PIN(torque_ext_cmd); // cmd in (Nm) +HAL_PIN(torque_cmd); // cmd out (Nm) +HAL_PIN(torque_cor_cmd); // corrected cmd out (Nm) +HAL_PIN(torque_sum); -HAL_PIN(friction); // (Nm) -HAL_PIN(damping); // (Nm/rad/s) -HAL_PIN(load); // (Nm) +HAL_PIN(friction); // (Nm) +HAL_PIN(damping); // (Nm/rad/s) +HAL_PIN(load); // (Nm) HAL_PIN(enable); @@ -54,211 +54,208 @@ HAL_PIN(vel_en); HAL_PIN(acc_en); HAL_PIN(torque_en); -HAL_PIN(pos_p); // (1/s) +HAL_PIN(pos_p); // (1/s) -HAL_PIN(vel_p); // (1/s) +HAL_PIN(vel_p); // (1/s) HAL_PIN(vel_i); HAL_PIN(vel_g); HAL_PIN(acc_g); -HAL_PIN(g); // model limit scaling +HAL_PIN(g); // model limit scaling -HAL_PIN(j); // (kgm^2) +HAL_PIN(j); // (kgm^2) // system limits -HAL_PIN(max_vel); // (rad/s) -HAL_PIN(max_torque); // (Nm) -HAL_PIN(min_torque); // (Nm) +HAL_PIN(max_vel); // (rad/s) +HAL_PIN(max_torque); // (Nm) +HAL_PIN(min_torque); // (Nm) // user limits -HAL_PIN(max_usr_vel); // (rad/s) -HAL_PIN(max_usr_acc); // (rad/s^2) -HAL_PIN(max_usr_torque); // (Nm) +HAL_PIN(max_usr_vel); // (rad/s) +HAL_PIN(max_usr_acc); // (rad/s^2) +HAL_PIN(max_usr_torque); // (Nm) HAL_PIN(vel_sat); HAL_PIN(acc_sat); HAL_PIN(torque_sat); -HAL_PIN(saturated); // (s) +HAL_PIN(saturated); // (s) -struct pid_ctx_t{ - float sat;//saturated time - float torque_sum;//integrator +struct pid_ctx_t { + float sat; //saturated time + float torque_sum; //integrator }; -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct pid_ctx_t * ctx = (struct pid_ctx_t *)ctx_ptr; - struct pid_pin_ctx_t * pins = (struct pid_pin_ctx_t *)pin_ptr; - ctx->sat = 0.0; +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct pid_ctx_t *ctx = (struct pid_ctx_t *)ctx_ptr; + struct pid_pin_ctx_t *pins = (struct pid_pin_ctx_t *)pin_ptr; + + ctx->sat = 0.0; ctx->torque_sum = 0.0; - PIN(pos_en) = 1.0; - PIN(vel_en) = 1.0; - PIN(acc_en) = 1.0; - PIN(torque_en) = 1.0; - PIN(pos_p) = 100.0; // (1/s) - PIN(vel_p) = 2000.0; // (1/s) - PIN(vel_i) = 10.0; - PIN(vel_g) = 1.0; - PIN(acc_g) = 1.0; - PIN(g) = 0.99; // model limit scaling - + PIN(pos_en) = 1.0; + PIN(vel_en) = 1.0; + PIN(acc_en) = 1.0; + PIN(torque_en) = 1.0; + PIN(pos_p) = 100.0; // (1/s) + PIN(vel_p) = 2000.0; // (1/s) + PIN(vel_i) = 10.0; + PIN(vel_g) = 1.0; + PIN(acc_g) = 1.0; + PIN(g) = 0.99; // model limit scaling } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct pid_ctx_t * ctx = (struct pid_ctx_t *)ctx_ptr; - struct pid_pin_ctx_t * pins = (struct pid_pin_ctx_t *)pin_ptr; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct pid_ctx_t *ctx = (struct pid_ctx_t *)ctx_ptr; + struct pid_pin_ctx_t *pins = (struct pid_pin_ctx_t *)pin_ptr; - float j = MAX(PIN(j), 0.0); - float g = CLAMP(PIN(g), 0.0, 1.0); + float j = MAX(PIN(j), 0.0); + float g = CLAMP(PIN(g), 0.0, 1.0); - float torque_ext_cmd = PIN(torque_ext_cmd); - float torque_min = MAX(PIN(min_torque) * g, -PIN(max_usr_torque)); - float torque_max = MIN(PIN(max_torque) * g, PIN(max_usr_torque)); - float torque_cmd; - float torque_sat; + float torque_ext_cmd = PIN(torque_ext_cmd); + float torque_min = MAX(PIN(min_torque) * g, -PIN(max_usr_torque)); + float torque_max = MIN(PIN(max_torque) * g, PIN(max_usr_torque)); + float torque_cmd; + float torque_sat; - float acc_ext_cmd = PIN(acc_ext_cmd); - float acc_min = -PIN(max_usr_acc);//MAX(torque_min / j, -PIN(max_usr_acc)); - float acc_max = PIN(max_usr_acc);//MIN(torque_max / j, PIN(max_usr_acc)); - float acc_cmd; - float acc_sat; + float acc_ext_cmd = PIN(acc_ext_cmd); + float acc_min = -PIN(max_usr_acc); //MAX(torque_min / j, -PIN(max_usr_acc)); + float acc_max = PIN(max_usr_acc); //MIN(torque_max / j, PIN(max_usr_acc)); + float acc_cmd; + float acc_sat; - float vel_ext_cmd = PIN(vel_ext_cmd); - float vel_fb = PIN(vel_fb); - float vel_min = MAX(-PIN(max_vel) * g, -PIN(max_usr_vel));//MAX(MAX(velfb + accmin * period, -PIN(max_vel) * p) , -PIN(max_usr_vel)); - float vel_max = MIN(PIN(max_vel) * g, PIN(max_usr_vel));//MIN(MIN(velfb + accmax * period, PIN(max_vel) * p), PIN(max_usr_vel)); - float vel_cmd; - float vel_error; - float vel_sat; + float vel_ext_cmd = PIN(vel_ext_cmd); + float vel_fb = PIN(vel_fb); + float vel_min = MAX(-PIN(max_vel) * g, -PIN(max_usr_vel)); //MAX(MAX(velfb + accmin * period, -PIN(max_vel) * p) , -PIN(max_usr_vel)); + float vel_max = MIN(PIN(max_vel) * g, PIN(max_usr_vel)); //MIN(MIN(velfb + accmax * period, PIN(max_vel) * p), PIN(max_usr_vel)); + float vel_cmd; + float vel_error; + float vel_sat; - float pos_ext_cmd = PIN(pos_ext_cmd); - float pos_fb = PIN(pos_fb); - float pos_error = minus(pos_ext_cmd, pos_fb); + float pos_ext_cmd = PIN(pos_ext_cmd); + float pos_fb = PIN(pos_fb); + float pos_error = minus(pos_ext_cmd, pos_fb); - float pos_p = MAX(PIN(pos_p), 0.0); - float vel_p = MAX(PIN(vel_p), 0.0); - float vel_i = MAX(PIN(vel_i), 0.0); - float vel_g = CLAMP(PIN(vel_g), 0.0, 1.0); - float acc_g = CLAMP(PIN(acc_g), 0.0, 1.0); + float pos_p = MAX(PIN(pos_p), 0.0); + float vel_p = MAX(PIN(vel_p), 0.0); + float vel_i = MAX(PIN(vel_i), 0.0); + float vel_g = CLAMP(PIN(vel_g), 0.0, 1.0); + float acc_g = CLAMP(PIN(acc_g), 0.0, 1.0); - - float pos_en = PIN(pos_en); - float vel_en = PIN(vel_en); - float acc_en = PIN(acc_en); - float torque_en = PIN(torque_en); - if(PIN(enable) > 0.0){ - if(torque_en <= 0.0){ - pos_en = 0.0; - vel_en = 0.0; - acc_en = 0.0; - torque_ext_cmd = 0.0; - } - if(acc_en <= 0.0){ - pos_en = 0.0; - vel_en = 0.0; - acc_g = 0.0; - acc_ext_cmd = 0.0; - } - if(vel_en <= 0.0){ - pos_en = 0.0; - pos_error = 0.0; - vel_ext_cmd = 0.0; - vel_fb = 0.0; - ctx->torque_sum = 0.0; - } - if(pos_en <= 0.0){ - pos_p = 0.0; - pos_error = 0.0; - } - - // pos -> vel - vel_cmd = pos_error * pos_p; // p - //vel_cmd = CLAMP(vel_cmd, vel_fb + acc_min * period, vel_fb + acc_max * period); // adaptive loop clamping - vel_cmd = CLAMP(vel_cmd, vel_min * vel_g, vel_max * vel_g); // p clamping - vel_cmd += vel_ext_cmd; // ff - vel_sat = SAT2(vel_cmd, vel_min * 0.99, vel_max * 0.99); - vel_cmd = CLAMP(vel_cmd, vel_min, vel_max); // clamping + float pos_en = PIN(pos_en); + float vel_en = PIN(vel_en); + float acc_en = PIN(acc_en); + float torque_en = PIN(torque_en); - // vel -> acc - vel_error = vel_cmd - vel_fb; - acc_cmd = vel_error * vel_p; // p - acc_cmd = CLAMP(acc_cmd, acc_min * acc_g, acc_max * acc_g); // p clamping - acc_cmd += acc_ext_cmd; // ff - acc_sat = SAT2(acc_cmd, acc_min * 0.99, acc_max * 0.99); - acc_cmd = CLAMP(acc_cmd, acc_min, acc_max); // clamping - - // acc -> torque - torque_cmd = acc_cmd * j + torque_ext_cmd; // ff - torque_cmd = CLAMP(torque_cmd, torque_min, torque_max); // clamping - ctx->torque_sum += vel_error * period * vel_i; // i - ctx->torque_sum = CLAMP(ctx->torque_sum, torque_min - torque_cmd, torque_max - torque_cmd); // dynamic integral clamping - torque_cmd += ctx->torque_sum; - torque_sat = SAT2(torque_cmd, torque_min * 0.99, torque_max * 0.99); - - - if(vel_i == 0.0){ - ctx->torque_sum = 0.0; - } - - PIN(torque_cmd) = torque_cmd; - if(vel_fb != 0.0){ // friction ff - torque_cmd += SIGN(vel_fb) * PIN(friction); - } - else{ - torque_cmd += SIGN(torque_cmd) * PIN(friction); - } - torque_cmd += vel_fb * PIN(damping); // damping ff - torque_cmd -= PIN(load); // load ff - torque_sat += 2 * SAT2(torque_cmd, torque_min, torque_max); - torque_cmd = CLAMP(torque_cmd, torque_min, torque_max); // min/max clamping - - // sat - if(ABS(vel_sat) + ABS(acc_sat) + ABS(torque_sat) > 0.0){ - ctx->sat += period; - } - else{ - ctx->sat -= period; - } - ctx->sat = MAX(ctx->sat, 0.0); - } - else{ + if(PIN(enable) > 0.0) { + if(torque_en <= 0.0) { + pos_en = 0.0; + vel_en = 0.0; + acc_en = 0.0; + torque_ext_cmd = 0.0; + } + if(acc_en <= 0.0) { + pos_en = 0.0; + vel_en = 0.0; + acc_g = 0.0; + acc_ext_cmd = 0.0; + } + if(vel_en <= 0.0) { + pos_en = 0.0; + pos_error = 0.0; + vel_ext_cmd = 0.0; + vel_fb = 0.0; ctx->torque_sum = 0.0; + } + if(pos_en <= 0.0) { + pos_p = 0.0; pos_error = 0.0; - vel_cmd = 0.0; - vel_error = 0.0; - acc_cmd = 0.0; - torque_cmd = 0.0; - vel_sat = 0.0; - acc_sat = 0.0; - torque_sat = 0.0; - ctx->sat = 0.0; - } + } - PIN(pos_error) = pos_error; - PIN(vel_cmd) = vel_cmd; - PIN(vel_error) = vel_error; - PIN(acc_cmd) = acc_cmd; - PIN(torque_cor_cmd) = torque_cmd; - PIN(torque_sum) = ctx->torque_sum; + // pos -> vel + vel_cmd = pos_error * pos_p; // p + //vel_cmd = CLAMP(vel_cmd, vel_fb + acc_min * period, vel_fb + acc_max * period); // adaptive loop clamping + vel_cmd = CLAMP(vel_cmd, vel_min * vel_g, vel_max * vel_g); // p clamping + vel_cmd += vel_ext_cmd; // ff + vel_sat = SAT2(vel_cmd, vel_min * 0.99, vel_max * 0.99); + vel_cmd = CLAMP(vel_cmd, vel_min, vel_max); // clamping - PIN(vel_sat) = vel_sat; - PIN(acc_sat) = acc_sat; - PIN(torque_sat) = torque_sat; - PIN(saturated) = ctx->sat; + // vel -> acc + vel_error = vel_cmd - vel_fb; + acc_cmd = vel_error * vel_p; // p + acc_cmd = CLAMP(acc_cmd, acc_min * acc_g, acc_max * acc_g); // p clamping + acc_cmd += acc_ext_cmd; // ff + acc_sat = SAT2(acc_cmd, acc_min * 0.99, acc_max * 0.99); + acc_cmd = CLAMP(acc_cmd, acc_min, acc_max); // clamping + + // acc -> torque + torque_cmd = acc_cmd * j + torque_ext_cmd; // ff + torque_cmd = CLAMP(torque_cmd, torque_min, torque_max); // clamping + ctx->torque_sum += vel_error * period * vel_i; // i + ctx->torque_sum = CLAMP(ctx->torque_sum, torque_min - torque_cmd, torque_max - torque_cmd); // dynamic integral clamping + torque_cmd += ctx->torque_sum; + torque_sat = SAT2(torque_cmd, torque_min * 0.99, torque_max * 0.99); + + + if(vel_i == 0.0) { + ctx->torque_sum = 0.0; + } + + PIN(torque_cmd) = torque_cmd; + if(vel_fb != 0.0) { // friction ff + torque_cmd += SIGN(vel_fb) * PIN(friction); + } else { + torque_cmd += SIGN(torque_cmd) * PIN(friction); + } + torque_cmd += vel_fb * PIN(damping); // damping ff + torque_cmd -= PIN(load); // load ff + torque_sat += 2 * SAT2(torque_cmd, torque_min, torque_max); + torque_cmd = CLAMP(torque_cmd, torque_min, torque_max); // min/max clamping + + // sat + if(ABS(vel_sat) + ABS(acc_sat) + ABS(torque_sat) > 0.0) { + ctx->sat += period; + } else { + ctx->sat -= period; + } + ctx->sat = MAX(ctx->sat, 0.0); + } else { + ctx->torque_sum = 0.0; + pos_error = 0.0; + vel_cmd = 0.0; + vel_error = 0.0; + acc_cmd = 0.0; + torque_cmd = 0.0; + vel_sat = 0.0; + acc_sat = 0.0; + torque_sat = 0.0; + ctx->sat = 0.0; + } + + PIN(pos_error) = pos_error; + PIN(vel_cmd) = vel_cmd; + PIN(vel_error) = vel_error; + PIN(acc_cmd) = acc_cmd; + PIN(torque_cor_cmd) = torque_cmd; + PIN(torque_sum) = ctx->torque_sum; + + PIN(vel_sat) = vel_sat; + PIN(acc_sat) = acc_sat; + PIN(torque_sat) = torque_sat; + PIN(saturated) = ctx->sat; } hal_comp_t pid_comp_struct = { - .name = "pid", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct pid_ctx_t), - .pin_count = sizeof(struct pid_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "pid", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct pid_ctx_t), + .pin_count = sizeof(struct pid_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/pmsm_limits.c b/shared/comps/pmsm_limits.c index 322a9e9c..c32f32c6 100644 --- a/shared/comps/pmsm_limits.c +++ b/shared/comps/pmsm_limits.c @@ -37,68 +37,69 @@ HAL_PIN(abs_max_vel); HAL_PIN(iq); HAL_PIN(indq); -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { // struct pmsm_limits_ctx_t * ctx = (struct pmsm_limits_ctx_t *)ctx_ptr; - struct pmsm_limits_pin_ctx_t * pins = (struct pmsm_limits_pin_ctx_t *)pin_ptr; - PIN(psi) = 0.01; - PIN(r) = 1.0; - PIN(ld) = 0.001; - PIN(lq) = 0.001; + struct pmsm_limits_pin_ctx_t *pins = (struct pmsm_limits_pin_ctx_t *)pin_ptr; + + PIN(psi) = 0.01; + PIN(r) = 1.0; + PIN(ld) = 0.001; + PIN(lq) = 0.001; PIN(polecount) = 1.0; } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - // struct pmsm_limits_ctx_t * ctx = (struct pmsm_limits_ctx_t *)ctx_ptr; - struct pmsm_limits_pin_ctx_t * pins = (struct pmsm_limits_pin_ctx_t *)pin_ptr; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct pmsm_limits_ctx_t * ctx = (struct pmsm_limits_ctx_t *)ctx_ptr; + struct pmsm_limits_pin_ctx_t *pins = (struct pmsm_limits_pin_ctx_t *)pin_ptr; - float p = (int)MAX(PIN(polecount), 1.0); - //float ld = MAX(PIN(ld), 0.0001); - float lq = MAX(PIN(lq), 0.0001); - float psi = MAX(PIN(psi), 0.01); - float r = MAX(PIN(r), 0.001); - float iq = PIN(iq); // TODO id + float p = (int)MAX(PIN(polecount), 1.0); + //float ld = MAX(PIN(ld), 0.0001); + float lq = MAX(PIN(lq), 0.0001); + float psi = MAX(PIN(psi), 0.01); + float r = MAX(PIN(r), 0.001); + float iq = PIN(iq); // TODO id - float indq = PIN(indq); // TODO vel * (psi + id ld) + float indq = PIN(indq); // TODO vel * (psi + id ld) - float volt = PIN(ac_volt); - float abs_max_cur = volt / r; - float abs_max_vel = volt / psi / p; - float abs_max_torque = 3.0 / 2.0 * p * psi * abs_max_cur; + float volt = PIN(ac_volt); + float abs_max_cur = volt / r; + float abs_max_vel = volt / psi / p; + float abs_max_torque = 3.0 / 2.0 * p * psi * abs_max_cur; - float next_max_cur = iq + (volt - r * iq - indq) / lq * period * 2.0 / 3.0; - float next_min_cur = iq + (-volt - r * iq - indq) / lq * period * 2.0 / 3.0; - float next_max_torque = 3.0 / 2.0 * p * (psi * next_max_cur); - float next_min_torque = 3.0 / 2.0 * p * (psi * next_min_cur); + float next_max_cur = iq + (volt - r * iq - indq) / lq * period * 2.0 / 3.0; + float next_min_cur = iq + (-volt - r * iq - indq) / lq * period * 2.0 / 3.0; + float next_max_torque = 3.0 / 2.0 * p * (psi * next_max_cur); + float next_min_torque = 3.0 / 2.0 * p * (psi * next_min_cur); - float max_cur = (volt - indq) / r; - float min_cur = (-volt - indq) / r; - float max_torque = 3.0 / 2.0 * p * (psi * max_cur); - float min_torque = 3.0 / 2.0 * p * (psi * min_cur); + float max_cur = (volt - indq) / r; + float min_cur = (-volt - indq) / r; + float max_torque = 3.0 / 2.0 * p * (psi * max_cur); + float min_torque = 3.0 / 2.0 * p * (psi * min_cur); - PIN(abs_max_cur) = abs_max_cur; - PIN(abs_max_vel) = abs_max_vel; - PIN(abs_max_torque) = abs_max_torque; - PIN(next_max_cur) = next_max_cur; - PIN(next_min_cur) = next_min_cur; - PIN(next_max_torque) = next_max_torque; - PIN(next_min_torque) = next_min_torque; - PIN(max_cur) = max_cur; - PIN(min_cur) = min_cur; - PIN(max_torque) = max_torque; - PIN(min_torque) = min_torque; + PIN(abs_max_cur) = abs_max_cur; + PIN(abs_max_vel) = abs_max_vel; + PIN(abs_max_torque) = abs_max_torque; + PIN(next_max_cur) = next_max_cur; + PIN(next_min_cur) = next_min_cur; + PIN(next_max_torque) = next_max_torque; + PIN(next_min_torque) = next_min_torque; + PIN(max_cur) = max_cur; + PIN(min_cur) = min_cur; + PIN(max_torque) = max_torque; + PIN(min_torque) = min_torque; } hal_comp_t pmsm_limits_comp_struct = { - .name = "pmsm_limits", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = 0, - .pin_count = sizeof(struct pmsm_limits_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "pmsm_limits", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct pmsm_limits_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/pmsm_ttc.c b/shared/comps/pmsm_ttc.c index 0e1b694f..f0410338 100644 --- a/shared/comps/pmsm_ttc.c +++ b/shared/comps/pmsm_ttc.c @@ -26,37 +26,38 @@ HAL_PIN(torque); // cur cmd out HAL_PIN(cur); -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { // struct sim_ctx_t * ctx = (struct sim_ctx_t *)ctx_ptr; - struct pmsm_ttc_pin_ctx_t * pins = (struct pmsm_ttc_pin_ctx_t *)pin_ptr; + struct pmsm_ttc_pin_ctx_t *pins = (struct pmsm_ttc_pin_ctx_t *)pin_ptr; + PIN(nc) = 1.0; PIN(ne) = 1.0; } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - // struct pmsm_ttc_ctx_t * ctx = (struct pmsm_ttc_ctx_t *)ctx_ptr; - struct pmsm_ttc_pin_ctx_t * pins = (struct pmsm_ttc_pin_ctx_t *)pin_ptr; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct pmsm_ttc_ctx_t * ctx = (struct pmsm_ttc_ctx_t *)ctx_ptr; + struct pmsm_ttc_pin_ctx_t *pins = (struct pmsm_ttc_pin_ctx_t *)pin_ptr; - float p = MAX(PIN(polecount), 1.0); - float psi_m = MAX(PIN(psi), 0.01); - float torque = PIN(torque); - - float tc = PIN(ac) * sinf(PIN(pc) + PIN(pos) * PIN(nc) * p); - float te = torque * PIN(ae) * sinf(PIN(pe) + PIN(pos) * PIN(ne) * p); - PIN(t) = tc + te; - PIN(cur) = (torque + PIN(g) * (tc + te)) / 3.0 * 2.0 / p / psi_m; + float p = MAX(PIN(polecount), 1.0); + float psi_m = MAX(PIN(psi), 0.01); + float torque = PIN(torque); + + float tc = PIN(ac) * sinf(PIN(pc) + PIN(pos) * PIN(nc) * p); + float te = torque * PIN(ae) * sinf(PIN(pe) + PIN(pos) * PIN(ne) * p); + PIN(t) = tc + te; + PIN(cur) = (torque + PIN(g) * (tc + te)) / 3.0 * 2.0 / p / psi_m; } hal_comp_t pmsm_ttc_comp_struct = { - .name = "pmsm_ttc", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = 0, - .pin_count = sizeof(struct pmsm_ttc_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "pmsm_ttc", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct pmsm_ttc_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/psi.c b/shared/comps/psi.c index 339c2317..aaa8a2e3 100644 --- a/shared/comps/psi.c +++ b/shared/comps/psi.c @@ -15,42 +15,43 @@ HAL_PIN(drop); HAL_PIN(psi); HAL_PIN(max_psi); -struct psi_ctx_t{ - float max_f; +struct psi_ctx_t { + float max_f; }; //HALL_PIN(polecount) = 1.0; //HALL_PIN(drop) = 0.8; -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct psi_ctx_t * ctx = (struct psi_ctx_t *)ctx_ptr; - struct psi_pin_ctx_t * pins = (struct psi_pin_ctx_t *)pin_ptr; - float f = ABS(PIN(vel)) / 2.0 / M_PI * PIN(polecount); - float u = PIN(dc_volt) + PIN(drop); - float u2 = MAX3(PIN(u), PIN(v), PIN(w)) - MIN3(PIN(u), PIN(v), PIN(w)); - u = MIN(u, u2) / M_SQRT3; // TODO: fix - if(f > 1.0){ - PIN(psi) = u / f / 2.0 / M_PI; - if(ctx->max_f < f){ - ctx->max_f = f; - PIN(max_psi) = PIN(psi); - } - } - ctx->max_f *= 0.999999; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct psi_ctx_t *ctx = (struct psi_ctx_t *)ctx_ptr; + struct psi_pin_ctx_t *pins = (struct psi_pin_ctx_t *)pin_ptr; + + float f = ABS(PIN(vel)) / 2.0 / M_PI * PIN(polecount); + float u = PIN(dc_volt) + PIN(drop); + float u2 = MAX3(PIN(u), PIN(v), PIN(w)) - MIN3(PIN(u), PIN(v), PIN(w)); + u = MIN(u, u2) / M_SQRT3; // TODO: fix + if(f > 1.0) { + PIN(psi) = u / f / 2.0 / M_PI; + if(ctx->max_f < f) { + ctx->max_f = f; + PIN(max_psi) = PIN(psi); + } + } + ctx->max_f *= 0.999999; } const hal_comp_t psi_comp_struct = { - .name = "psi", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = 0, - .hw_init = 0, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct psi_ctx_t), - .pin_count = sizeof(struct psi_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "psi", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = 0, + .hw_init = 0, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct psi_ctx_t), + .pin_count = sizeof(struct psi_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/reslimit.c b/shared/comps/reslimit.c index 159d2aa7..df748c08 100644 --- a/shared/comps/reslimit.c +++ b/shared/comps/reslimit.c @@ -11,23 +11,24 @@ HAL_PIN(pos_out); HAL_PIN(res); -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - // struct reslimit_ctx_t * ctx = (struct reslimit_ctx_t *)ctx_ptr; - struct reslimit_pin_ctx_t * pins = (struct reslimit_pin_ctx_t *)pin_ptr; - uint32_t r = ABS(PIN(res));//TODO: div by zero - PIN(pos_out) = ((int)(PIN(pos_in) * r / 2.0 / M_PI + 0.5)) / (float)r * 2.0 * M_PI; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct reslimit_ctx_t * ctx = (struct reslimit_ctx_t *)ctx_ptr; + struct reslimit_pin_ctx_t *pins = (struct reslimit_pin_ctx_t *)pin_ptr; + + uint32_t r = ABS(PIN(res)); //TODO: div by zero + PIN(pos_out) = ((int)(PIN(pos_in) * r / 2.0 / M_PI + 0.5)) / (float)r * 2.0 * M_PI; } hal_comp_t reslimit_comp_struct = { - .name = "reslimit", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = 0, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = 0, - .pin_count = sizeof(struct reslimit_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "reslimit", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = 0, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct reslimit_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/rev.c b/shared/comps/rev.c index 04b89837..791ad33b 100644 --- a/shared/comps/rev.c +++ b/shared/comps/rev.c @@ -12,30 +12,29 @@ HAL_PIN(in_d); HAL_PIN(out_d); HAL_PIN(rev); -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - // struct rev_ctx_t * ctx = (struct rev_ctx_t *)ctx_ptr; - struct rev_pin_ctx_t * pins = (struct rev_pin_ctx_t *)pin_ptr; - - if(PIN(rev) > 0.0){ - PIN(out) = minus(0, PIN(in)); +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct rev_ctx_t * ctx = (struct rev_ctx_t *)ctx_ptr; + struct rev_pin_ctx_t *pins = (struct rev_pin_ctx_t *)pin_ptr; + + if(PIN(rev) > 0.0) { + PIN(out) = minus(0, PIN(in)); PIN(out_d) = -PIN(in_d); - } - else{ - PIN(out) = PIN(in); + } else { + PIN(out) = PIN(in); PIN(out_d) = PIN(in_d); } } hal_comp_t rev_comp_struct = { - .name = "rev", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = 0, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = 0, - .pin_count = sizeof(struct rev_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "rev", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = 0, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct rev_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/sim.c b/shared/comps/sim.c index eb1d2847..544ef536 100644 --- a/shared/comps/sim.c +++ b/shared/comps/sim.c @@ -11,87 +11,87 @@ HAL_PIN(freq); HAL_PIN(sin); HAL_PIN(msin); HAL_PIN(sin2); -HAL_PIN(msin2);//const vel, const max vel = amp +HAL_PIN(msin2); //const vel, const max vel = amp HAL_PIN(sin3); -HAL_PIN(msin3);//const max acc = amp +HAL_PIN(msin3); //const max acc = amp HAL_PIN(square); HAL_PIN(vel); HAL_PIN(res); HAL_PIN(offset); -struct sim_ctx_t{ - float time; - float amp; - float freq; - float vel; +struct sim_ctx_t { + float time; + float amp; + float freq; + float vel; }; -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { // struct sim_ctx_t * ctx = (struct sim_ctx_t *)ctx_ptr; - struct sim_pin_ctx_t * pins = (struct sim_pin_ctx_t *)pin_ptr; - PIN(amp) = 3.1; + struct sim_pin_ctx_t *pins = (struct sim_pin_ctx_t *)pin_ptr; + + PIN(amp) = 3.1; PIN(freq) = 1.0; - PIN(res) = 100000.0; + PIN(res) = 100000.0; } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct sim_ctx_t * ctx = (struct sim_ctx_t *)ctx_ptr; - struct sim_pin_ctx_t * pins = (struct sim_pin_ctx_t *)pin_ptr; - - ctx->amp = PIN(amp) * 0.001 + ctx->amp * 0.999; - ctx->freq = PIN(freq) * 0.001 + ctx->freq * 0.999; - float sin; - float sin2; - float sin3; - float amp2; - float r = MAX(PIN(res), 1); +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct sim_ctx_t *ctx = (struct sim_ctx_t *)ctx_ptr; + struct sim_pin_ctx_t *pins = (struct sim_pin_ctx_t *)pin_ptr; - if(ABS(ctx->freq) > 0.01){ - amp2 = 1 / (ctx->freq * 2.0 * M_PI); - } - else{ - amp2 = 0; - } + ctx->amp = PIN(amp) * 0.001 + ctx->amp * 0.999; + ctx->freq = PIN(freq) * 0.001 + ctx->freq * 0.999; + float sin; + float sin2; + float sin3; + float amp2; + float r = MAX(PIN(res), 1); - ctx->time += period; + if(ABS(ctx->freq) > 0.01) { + amp2 = 1 / (ctx->freq * 2.0 * M_PI); + } else { + amp2 = 0; + } - if(ABS(ctx->freq * ctx->time) >= 1.0 && ABS(ctx->freq) > 0.0){ // TODO fix - ctx->time -= 1.0 / ABS(ctx->freq); - } + ctx->time += period; - float co = 0.0; - float si = 0.0; - sincos_fast(ctx->freq * ctx->time * 2.0 * M_PI, &si, &co); + if(ABS(ctx->freq * ctx->time) >= 1.0 && ABS(ctx->freq) > 0.0) { // TODO fix + ctx->time -= 1.0 / ABS(ctx->freq); + } - sin = ctx->amp * si; - sin2 = sin * amp2; - sin3 = sin2 * amp2; + float co = 0.0; + float si = 0.0; + sincos_fast(ctx->freq * ctx->time * 2.0 * M_PI, &si, &co); - float s = sin; - float o = PIN(offset); - ctx->vel += ctx->freq * 2.0 * M_PI * period; - ctx->vel = mod(ctx->vel); + sin = ctx->amp * si; + sin2 = sin * amp2; + sin3 = sin2 * amp2; - PIN(sin) = s + o; - PIN(sin2) = sin2 + o; - PIN(sin3) = sin3 + o; - PIN(msin) = ((int)(mod(s + o) * r)) / r; - PIN(msin2) = ((int)(mod(sin2 + o) * r)) / r; - PIN(msin3) = ((int)(mod(sin3 + o) * r)) / r; - PIN(square) = (sin > 0.0) ? (ctx->amp + o) : (-ctx->amp + o); - PIN(vel) = ctx->vel;//mod(((int)(vel * r)) / r + o); + float s = sin; + float o = PIN(offset); + ctx->vel += ctx->freq * 2.0 * M_PI * period; + ctx->vel = mod(ctx->vel); + + PIN(sin) = s + o; + PIN(sin2) = sin2 + o; + PIN(sin3) = sin3 + o; + PIN(msin) = ((int)(mod(s + o) * r)) / r; + PIN(msin2) = ((int)(mod(sin2 + o) * r)) / r; + PIN(msin3) = ((int)(mod(sin3 + o) * r)) / r; + PIN(square) = (sin > 0.0) ? (ctx->amp + o) : (-ctx->amp + o); + PIN(vel) = ctx->vel; //mod(((int)(vel * r)) / r + o); } hal_comp_t sim_comp_struct = { - .name = "sim", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct sim_ctx_t), - .pin_count = sizeof(struct sim_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "sim", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct sim_ctx_t), + .pin_count = sizeof(struct sim_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/spid.c b/shared/comps/spid.c index d0283762..0abead18 100644 --- a/shared/comps/spid.c +++ b/shared/comps/spid.c @@ -7,20 +7,20 @@ HAL_COMP(spid); // input -HAL_PIN(cmd); // command -HAL_PIN(fb); // feedback -HAL_PIN(en); // enalbe +HAL_PIN(cmd); // command +HAL_PIN(fb); // feedback +HAL_PIN(en); // enalbe // gains -HAL_PIN(kp); // proportional -HAL_PIN(ki); // integator -HAL_PIN(kd); // differential -HAL_PIN(ksd); // scaled differential -HAL_PIN(kdi); // differential integrator -HAL_PIN(ksdi); // scaled differential integrator -HAL_PIN(kff0); // feedforward 0 -HAL_PIN(kff1); // feedforward 1 -HAL_PIN(offset); // 0 offset +HAL_PIN(kp); // proportional +HAL_PIN(ki); // integator +HAL_PIN(kd); // differential +HAL_PIN(ksd); // scaled differential +HAL_PIN(kdi); // differential integrator +HAL_PIN(ksdi); // scaled differential integrator +HAL_PIN(kff0); // feedforward 0 +HAL_PIN(kff1); // feedforward 1 +HAL_PIN(offset); // 0 offset HAL_PIN(min_output); HAL_PIN(max_output); @@ -31,79 +31,78 @@ HAL_PIN(output); HAL_PIN(error); HAL_PIN(sat); -struct spid_ctx_t{ - float error_sum; - float last_error; - float last_cmd; +struct spid_ctx_t { + float error_sum; + float last_error; + float last_cmd; }; -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct spid_ctx_t * ctx = (struct spid_ctx_t *)ctx_ptr; - struct spid_pin_ctx_t * pins = (struct spid_pin_ctx_t *)pin_ptr; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct spid_ctx_t *ctx = (struct spid_ctx_t *)ctx_ptr; + struct spid_pin_ctx_t *pins = (struct spid_pin_ctx_t *)pin_ptr; - float offset = PIN(offset); - float min = PIN(min_output) - offset; - float max = PIN(max_output) - offset; - float max_error = PIN(max_error); + float offset = PIN(offset); + float min = PIN(min_output) - offset; + float max = PIN(max_output) - offset; + float max_error = PIN(max_error); - float cmd = PIN(cmd); - float cmd_d = (cmd - ctx->last_cmd) / period; - float error = cmd - PIN(fb); - if(max_error > 0.0){ - error = LIMIT(error, max_error); - } - float error_d = (error - ctx->last_error) / period; + float cmd = PIN(cmd); + float cmd_d = (cmd - ctx->last_cmd) / period; + float error = cmd - PIN(fb); + if(max_error > 0.0) { + error = LIMIT(error, max_error); + } + float error_d = (error - ctx->last_error) / period; - float output = 0.0; - output += cmd * PIN(kff0); // feedforward 0 - output += cmd_d * PIN(kff1); // feedforward 1 - output += error * PIN(kp); // porportional - output += error_d * PIN(kd); // differential - if(PIN(ksd) != 0.0 && ABS(error) > (max - min) / PIN(ksd) * 0.001){ - ctx->error_sum += error_d / ABS(error) * PIN(ksd); // scalded differential - } - output = CLAMP(output, min, max); + float output = 0.0; + output += cmd * PIN(kff0); // feedforward 0 + output += cmd_d * PIN(kff1); // feedforward 1 + output += error * PIN(kp); // porportional + output += error_d * PIN(kd); // differential + if(PIN(ksd) != 0.0 && ABS(error) > (max - min) / PIN(ksd) * 0.001) { + ctx->error_sum += error_d / ABS(error) * PIN(ksd); // scalded differential + } + output = CLAMP(output, min, max); - ctx->error_sum += error * PIN(ki) * period; // integrator - ctx->error_sum += error_d * PIN(kdi) * period; // differential integrator - if(PIN(ksdi) != 0.0 && ABS(error) > (max - min) / PIN(ksdi) * 0.001){ - ctx->error_sum += error_d / ABS(error) * PIN(ksdi) * period; // scalded differential integrator - } - ctx->error_sum = CLAMP(ctx->error_sum, min - output, max - output); // dynamic anti windup + ctx->error_sum += error * PIN(ki) * period; // integrator + ctx->error_sum += error_d * PIN(kdi) * period; // differential integrator + if(PIN(ksdi) != 0.0 && ABS(error) > (max - min) / PIN(ksdi) * 0.001) { + ctx->error_sum += error_d / ABS(error) * PIN(ksdi) * period; // scalded differential integrator + } + ctx->error_sum = CLAMP(ctx->error_sum, min - output, max - output); // dynamic anti windup - output += ctx->error_sum; + output += ctx->error_sum; - if(PIN(en) <= 0.0){ - output = 0.0; - ctx->error_sum = 0.0; - } + if(PIN(en) <= 0.0) { + output = 0.0; + ctx->error_sum = 0.0; + } - if(output <= min * 0.99 || output >= max * 0.99){ - PIN(sat) += period; - } - else{ - PIN(sat) = 0.0; - } + if(output <= min * 0.99 || output >= max * 0.99) { + PIN(sat) += period; + } else { + PIN(sat) = 0.0; + } - output += offset; + output += offset; - PIN(output) = output; + PIN(output) = output; - PIN(error) = error; - ctx->last_error = error; - ctx->last_cmd = cmd; + PIN(error) = error; + ctx->last_error = error; + ctx->last_cmd = cmd; } hal_comp_t spid_comp_struct = { - .name = "spid", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = 0, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct spid_ctx_t), - .pin_count = sizeof(struct spid_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "spid", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = 0, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct spid_ctx_t), + .pin_count = sizeof(struct spid_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/stp.c b/shared/comps/stp.c index 4a011cd9..86c82080 100644 --- a/shared/comps/stp.c +++ b/shared/comps/stp.c @@ -21,64 +21,65 @@ HAL_PIN(max_acc); HAL_PIN(dtg); -struct stp_ctx_t{ - float p0; - float p; - float v0; - float pold; - float vold; +struct stp_ctx_t { + float p0; + float p; + float v0; + float pold; + float vold; }; -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct stp_ctx_t * ctx = (struct stp_ctx_t *)ctx_ptr; - struct stp_pin_ctx_t * pins = (struct stp_pin_ctx_t *)pin_ptr; - //float p = PIN(pos_in); - //float ctx->v0 = PIN(vel_in); +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct stp_ctx_t *ctx = (struct stp_ctx_t *)ctx_ptr; + struct stp_pin_ctx_t *pins = (struct stp_pin_ctx_t *)pin_ptr; - //ctx->p0 += minus(p, ctx->pold); - //ctx->pold = p; + //float p = PIN(pos_in); + //float ctx->v0 = PIN(vel_in); - float p1 = PIN(target); + //ctx->p0 += minus(p, ctx->pold); + //ctx->pold = p; - float vmax = PIN(max_vel); - float amax = MAX(PIN(max_acc), 0.1); + float p1 = PIN(target); - float vel = SIGN(p1 - ctx->p0) * amax * sqrtf(2.0 * ABS(p1 - ctx->p0) / amax); + float vmax = PIN(max_vel); + float amax = MAX(PIN(max_acc), 0.1); - vel = LIMIT(vel, vmax); - vel = CLAMP(vel, ctx->v0 - amax * period, ctx->v0 + amax * period); + float vel = SIGN(p1 - ctx->p0) * amax * sqrtf(2.0 * ABS(p1 - ctx->p0) / amax); - if(ABS(vel) > ABS(p1 - ctx->p0) / period){ - vel = (p1 - ctx->p0) / period; - } + vel = LIMIT(vel, vmax); + vel = CLAMP(vel, ctx->v0 - amax * period, ctx->v0 + amax * period); - float acc = (vel - ctx->vold) / period; - ctx->vold = vel; + if(ABS(vel) > ABS(p1 - ctx->p0) / period) { + vel = (p1 - ctx->p0) / period; + } - ctx->v0 = vel; - ctx->p0 += ctx->v0 * period; - ctx->p += ctx->p0 - ctx->pold; - ctx->pold = ctx->p0; + float acc = (vel - ctx->vold) / period; + ctx->vold = vel; - //p += vel * period; + ctx->v0 = vel; + ctx->p0 += ctx->v0 * period; + ctx->p += ctx->p0 - ctx->pold; + ctx->pold = ctx->p0; - PIN(pos_out) = mod(ctx->p); - PIN(vel_out) = vel; - PIN(acc_out) = acc; - PIN(dtg) = p1 - ctx->p0; + //p += vel * period; + + PIN(pos_out) = mod(ctx->p); + PIN(vel_out) = vel; + PIN(acc_out) = acc; + PIN(dtg) = p1 - ctx->p0; } const hal_comp_t stp_comp_struct = { - .name = "stp", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = 0, - .hw_init = 0, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct stp_ctx_t), - .pin_count = sizeof(struct stp_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "stp", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = 0, + .hw_init = 0, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct stp_ctx_t), + .pin_count = sizeof(struct stp_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/svm.c b/shared/comps/svm.c index e1631c46..81994069 100644 --- a/shared/comps/svm.c +++ b/shared/comps/svm.c @@ -30,81 +30,82 @@ HAL_PIN(enu); HAL_PIN(env); HAL_PIN(enw); -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { // struct svm_ctx_t * ctx = (struct svm_ctx_t *)ctx_ptr; - struct svm_pin_ctx_t * pins = (struct svm_pin_ctx_t *)pin_ptr; + struct svm_pin_ctx_t *pins = (struct svm_pin_ctx_t *)pin_ptr; + PIN(mode) = 2.0; + PIN(enu) = 1.0; + PIN(env) = 1.0; + PIN(enw) = 1.0; +} + +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct svm_ctx_t * ctx = (struct svm_ctx_t *)ctx_ptr; + struct svm_pin_ctx_t *pins = (struct svm_pin_ctx_t *)pin_ptr; + + float offset = 0; + float udc = PIN(udc); + + float u = PIN(u); + float v = PIN(v); + float w = PIN(w); + + switch((int)PIN(mode)) { + default: + case 0: // sine modulation + offset = (u + v + w) / 3.0 - udc / 2.0; + break; + + case 1: // space vector modulation + offset = (MIN3(u, v, w) + MAX3(u, v, w)) / 2.0 - udc / 2.0; + break; + + case 2: // flat bottom space vector modulation + offset = MIN3(u, v, w); + break; + + case 3: // flat top space vector modulation + offset = MAX3(u, v, w) - udc; + break; + } + PIN(enu) = 1.0; PIN(env) = 1.0; PIN(enw) = 1.0; -} -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - // struct svm_ctx_t * ctx = (struct svm_ctx_t *)ctx_ptr; - struct svm_pin_ctx_t * pins = (struct svm_pin_ctx_t *)pin_ptr; + switch((int)PIN(cmode)) { + case 1: // block + if(u > v && u < w) { + PIN(enu) = 0.0; + } + if(v > u && v < w) { + PIN(env) = 0.0; + } + if(w > u && w < v) { + PIN(enw) = 0.0; + } + break; - float offset = 0; - float udc = PIN(udc); - - float u = PIN(u); - float v = PIN(v); - float w = PIN(w); - - switch((int)PIN(mode)){ - default: - case 0: // sine modulation - offset = (u + v + w) / 3.0 - udc / 2.0; - break; - - case 1: // space vector modulation - offset = (MIN3(u, v, w) + MAX3(u, v, w)) / 2.0 - udc / 2.0; - break; - - case 2: // flat bottom space vector modulation - offset = MIN3(u, v, w); - break; - - case 3: // flat top space vector modulation - offset = MAX3(u, v, w) - udc; - break; - } - - PIN(enu) = 1.0; - PIN(env) = 1.0; - PIN(enw) = 1.0; - - switch((int)PIN(cmode)){ - case 1: // block - if(u > v && u < w){ - PIN(enu) = 0.0; - } - if(v > u && v < w){ - PIN(env) = 0.0; - } - if(w > u && w < v){ - PIN(enw) = 0.0; - } - break; - - default: // sine - break; - } - - PIN(su) = u - offset; - PIN(sv) = v - offset; - PIN(sw) = w - offset; + default: // sine + break; + } + + PIN(su) = u - offset; + PIN(sv) = v - offset; + PIN(sw) = w - offset; } hal_comp_t svm_comp_struct = { - .name = "svm", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = 0, - .pin_count = sizeof(struct svm_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "svm", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct svm_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/term.c b/shared/comps/term.c index 2ad84ef3..d786d910 100644 --- a/shared/comps/term.c +++ b/shared/comps/term.c @@ -16,92 +16,93 @@ HAL_PINA(gain, 8); HAL_PIN(send_step); HAL_PIN(con); -struct term_ctx_t{ - float wave_buf[TERM_BUF_SIZE][TERM_NUM_WAVES]; - uint32_t send_counter; - uint32_t write_pos; - uint32_t read_pos; +struct term_ctx_t { + float wave_buf[TERM_BUF_SIZE][TERM_NUM_WAVES]; + uint32_t send_counter; + uint32_t write_pos; + uint32_t read_pos; }; -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { // struct term_ctx_t * ctx = (struct sim_ctx_t *)ctx_ptr; - struct term_pin_ctx_t * pins = (struct term_pin_ctx_t *)pin_ptr; + struct term_pin_ctx_t *pins = (struct term_pin_ctx_t *)pin_ptr; + PIN(send_step) = 50; - for(int i = 0; i < TERM_NUM_WAVES; i++){ + for(int i = 0; i < TERM_NUM_WAVES; i++) { PINA(gain, i) = 10; } } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct term_ctx_t * ctx = (struct term_ctx_t *)ctx_ptr; - struct term_pin_ctx_t * pins = (struct term_pin_ctx_t *)pin_ptr; - - if(ctx->send_counter++ >= PIN(send_step) - 1){ - for(int i = 0; i < TERM_NUM_WAVES; i++){ - ctx->wave_buf[ctx->write_pos][i] = PINA(wave, i); - } - - ctx->write_pos++; - ctx->write_pos %= TERM_BUF_SIZE; - ctx->send_counter = 0; - } +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct term_ctx_t *ctx = (struct term_ctx_t *)ctx_ptr; + struct term_pin_ctx_t *pins = (struct term_pin_ctx_t *)pin_ptr; + + if(ctx->send_counter++ >= PIN(send_step) - 1) { + for(int i = 0; i < TERM_NUM_WAVES; i++) { + ctx->wave_buf[ctx->write_pos][i] = PINA(wave, i); + } + + ctx->write_pos++; + ctx->write_pos %= TERM_BUF_SIZE; + ctx->send_counter = 0; + } } -static void nrt_func(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct term_ctx_t * ctx = (struct term_ctx_t *)ctx_ptr; - struct term_pin_ctx_t * pins = (struct term_pin_ctx_t *)pin_ptr; +static void nrt_func(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct term_ctx_t *ctx = (struct term_ctx_t *)ctx_ptr; + struct term_pin_ctx_t *pins = (struct term_pin_ctx_t *)pin_ptr; - int tmp = 0; - unsigned char buf[TERM_NUM_WAVES + 2]; + int tmp = 0; + unsigned char buf[TERM_NUM_WAVES + 2]; - buf[0] = 255; - buf[TERM_NUM_WAVES + 1] = 0; + buf[0] = 255; + buf[TERM_NUM_WAVES + 1] = 0; - unsigned int wp = ctx->write_pos; - unsigned int bc = 0; + unsigned int wp = ctx->write_pos; + unsigned int bc = 0; - while(ctx->read_pos != wp){ - bc++; - - for(int i = 0; i < TERM_NUM_WAVES; i++){ - tmp = (int)((ctx->wave_buf[ctx->read_pos][i] + PINA(offset,i)) * PINA(gain,i) + 128); - buf[i+1] = CLAMP(tmp,1,254); - } + while(ctx->read_pos != wp) { + bc++; - ctx->read_pos++; - ctx->read_pos %= TERM_BUF_SIZE; + for(int i = 0; i < TERM_NUM_WAVES; i++) { + tmp = (int)((ctx->wave_buf[ctx->read_pos][i] + PINA(offset, i)) * PINA(gain, i) + 128); + buf[i + 1] = CLAMP(tmp, 1, 254); + } - buf[TERM_NUM_WAVES + 1] = 0; + ctx->read_pos++; + ctx->read_pos %= TERM_BUF_SIZE; - if (cdc_is_connected() && PIN(send_step) > 0) { - cdc_tx(buf,TERM_NUM_WAVES + 1); - } - } + buf[TERM_NUM_WAVES + 1] = 0; - if (cdc_is_connected()) { - PIN(con) = 1.0; - }else{ - PIN(con) = 0.0; - } - - if (cdc_is_connected()) { - char rx_buf[64]; - if (cdc_getline(rx_buf,sizeof(rx_buf))){ - hal_parse(rx_buf); - } - } + if(cdc_is_connected() && PIN(send_step) > 0) { + cdc_tx(buf, TERM_NUM_WAVES + 1); + } + } + + if(cdc_is_connected()) { + PIN(con) = 1.0; + } else { + PIN(con) = 0.0; + } + + if(cdc_is_connected()) { + char rx_buf[64]; + if(cdc_getline(rx_buf, sizeof(rx_buf))) { + hal_parse(rx_buf); + } + } } hal_comp_t term_comp_struct = { - .name = "term", - .nrt = nrt_func, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct term_ctx_t), - .pin_count = sizeof(struct term_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "term", + .nrt = nrt_func, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct term_ctx_t), + .pin_count = sizeof(struct term_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/uvw.c b/shared/comps/uvw.c index 11400ed4..26ac122c 100644 --- a/shared/comps/uvw.c +++ b/shared/comps/uvw.c @@ -24,48 +24,49 @@ HAL_PIN(p7); HAL_PIN(pos); HAL_PIN(rpos); -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { // struct uvw_ctx_t * ctx = (struct io_ctx_t *)ctx_ptr; - struct uvw_pin_ctx_t * pins = (struct uvw_pin_ctx_t *)pin_ptr; - PIN(p0) = 0;//fault - PIN(p1) = 0;//u = 0 - PIN(p2) = 2;//v = 2.094395 - PIN(p3) = 1;//u + v = 1.047198 - PIN(p4) = 4;//w = -2.094395 - PIN(p5) = 5;//u + w = -1.047198 - PIN(p6) = 3;//v + w = -3.141593 - PIN(p7) = 0;//fault + struct uvw_pin_ctx_t *pins = (struct uvw_pin_ctx_t *)pin_ptr; + + PIN(p0) = 0; //fault + PIN(p1) = 0; //u = 0 + PIN(p2) = 2; //v = 2.094395 + PIN(p3) = 1; //u + v = 1.047198 + PIN(p4) = 4; //w = -2.094395 + PIN(p5) = 5; //u + w = -1.047198 + PIN(p6) = 3; //v + w = -3.141593 + PIN(p7) = 0; //fault } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - // struct uvw_ctx_t * ctx = (struct uvw_ctx_t *)ctx_ptr; - struct uvw_pin_ctx_t * pins = (struct uvw_pin_ctx_t *)pin_ptr; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct uvw_ctx_t * ctx = (struct uvw_ctx_t *)ctx_ptr; + struct uvw_pin_ctx_t *pins = (struct uvw_pin_ctx_t *)pin_ptr; - uint32_t rpos = (PIN(u) > 0.0) * 1.0 + (PIN(v) > 0.0) * 2.0 + (PIN(w) > 0.0) * 4.0; - //TODO: make this const, fault output - uint32_t t[8]; - t[0] = PIN(p0); - t[1] = PIN(p1); - t[2] = PIN(p2); - t[3] = PIN(p3); - t[4] = PIN(p4); - t[5] = PIN(p5); - t[6] = PIN(p6); - t[7] = PIN(p7); - PIN(rpos) = rpos; - PIN(pos) = mod((float)t[rpos]/6.0 * 2.0 * M_PI); + uint32_t rpos = (PIN(u) > 0.0) * 1.0 + (PIN(v) > 0.0) * 2.0 + (PIN(w) > 0.0) * 4.0; + //TODO: make this const, fault output + uint32_t t[8]; + t[0] = PIN(p0); + t[1] = PIN(p1); + t[2] = PIN(p2); + t[3] = PIN(p3); + t[4] = PIN(p4); + t[5] = PIN(p5); + t[6] = PIN(p6); + t[7] = PIN(p7); + PIN(rpos) = rpos; + PIN(pos) = mod((float)t[rpos] / 6.0 * 2.0 * M_PI); } hal_comp_t uvw_comp_struct = { - .name = "uvw", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = 0, - .pin_count = sizeof(struct uvw_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "uvw", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct uvw_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/vel.c b/shared/comps/vel.c index e181eb1e..504d8329 100644 --- a/shared/comps/vel.c +++ b/shared/comps/vel.c @@ -21,73 +21,74 @@ HAL_PIN(vel_ff); HAL_PIN(en); HAL_PIN(pos_error); -struct vel_ctx_t{ +struct vel_ctx_t { float last_acc; float acc_sum; float vel_sum; }; -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { // struct vel_ctx_t * ctx = (struct vel_ctx_t *)ctx_ptr; - struct vel_pin_ctx_t * pins = (struct vel_pin_ctx_t *)pin_ptr; - PIN(w) = 1000.0; - PIN(d) = 0.9; - PIN(g) = 1.0; - PIN(h) = 1.0; - PIN(j) = 0.00001; + struct vel_pin_ctx_t *pins = (struct vel_pin_ctx_t *)pin_ptr; + + PIN(w) = 1000.0; + PIN(d) = 0.9; + PIN(g) = 1.0; + PIN(h) = 1.0; + PIN(j) = 0.00001; PIN(lp) = 50.0; PIN(en) = 1.0; } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct vel_ctx_t * ctx = (struct vel_ctx_t *)ctx_ptr; - struct vel_pin_ctx_t * pins = (struct vel_pin_ctx_t *)pin_ptr; - - if(PIN(en) == 0.0){ - ctx->vel_sum = PIN(pos_in); - ctx->acc_sum = 0.0; - } - float vel_ff = PIN(vel_ff) * PIN(h); - ctx->vel_sum += (ctx->acc_sum + vel_ff) * period; // ff - - float pos_error = minus(PIN(pos_in), ctx->vel_sum); - float w = PIN(w); - float d = PIN(d); - float g = PIN(g); - float lp = LP_HZ(PIN(lp)); - float j = MAX(PIN(j), 0.0000001); - float acc = g * PIN(torque) / j; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct vel_ctx_t *ctx = (struct vel_ctx_t *)ctx_ptr; + struct vel_pin_ctx_t *pins = (struct vel_pin_ctx_t *)pin_ptr; - ctx->last_acc = acc * lp + (1.0 - lp) * ctx->last_acc; + if(PIN(en) == 0.0) { + ctx->vel_sum = PIN(pos_in); + ctx->acc_sum = 0.0; + } + float vel_ff = PIN(vel_ff) * PIN(h); + ctx->vel_sum += (ctx->acc_sum + vel_ff) * period; // ff - float acc_ff = acc - ctx->last_acc; + float pos_error = minus(PIN(pos_in), ctx->vel_sum); + float w = PIN(w); + float d = PIN(d); + float g = PIN(g); + float lp = LP_HZ(PIN(lp)); + float j = MAX(PIN(j), 0.0000001); + float acc = g * PIN(torque) / j; - acc_ff += pos_error * w * w; + ctx->last_acc = acc * lp + (1.0 - lp) * ctx->last_acc; - ctx->acc_sum += acc_ff * period; + float acc_ff = acc - ctx->last_acc; - PIN(vel) = ctx->acc_sum + vel_ff; - PIN(acc) = acc_ff; + acc_ff += pos_error * w * w; - vel_ff = 2.0 * d * w * pos_error; - - ctx->vel_sum += vel_ff * period; - ctx->vel_sum = mod(ctx->vel_sum); + ctx->acc_sum += acc_ff * period; - PIN(pos_out) = ctx->vel_sum; - PIN(pos_error) = minus(PIN(pos_in), ctx->vel_sum); + PIN(vel) = ctx->acc_sum + vel_ff; + PIN(acc) = acc_ff; + + vel_ff = 2.0 * d * w * pos_error; + + ctx->vel_sum += vel_ff * period; + ctx->vel_sum = mod(ctx->vel_sum); + + PIN(pos_out) = ctx->vel_sum; + PIN(pos_error) = minus(PIN(pos_in), ctx->vel_sum); } hal_comp_t vel_comp_struct = { - .name = "vel", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct vel_ctx_t), - .pin_count = sizeof(struct vel_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "vel", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct vel_ctx_t), + .pin_count = sizeof(struct vel_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/vel_int.c b/shared/comps/vel_int.c index e21a691e..1ebe0dad 100644 --- a/shared/comps/vel_int.c +++ b/shared/comps/vel_int.c @@ -15,66 +15,65 @@ HAL_PIN(vel_out); HAL_PIN(wd); HAL_PIN(error); -struct vel_int_ctx_t{ - float pos; - float counter; +struct vel_int_ctx_t { + float pos; + float counter; }; -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct vel_int_ctx_t * ctx = (struct vel_int_ctx_t *)ctx_ptr; - struct vel_int_pin_ctx_t * pins = (struct vel_int_pin_ctx_t *)pin_ptr; - ctx->pos = 0.0; +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct vel_int_ctx_t *ctx = (struct vel_int_ctx_t *)ctx_ptr; + struct vel_int_pin_ctx_t *pins = (struct vel_int_pin_ctx_t *)pin_ptr; + + ctx->pos = 0.0; ctx->counter = 0.0; - PIN(pos_in) = 0.0; + PIN(pos_in) = 0.0; PIN(pos_out) = 0.0; - PIN(vel_in) = 0.0; + PIN(vel_in) = 0.0; PIN(vel_out) = 0.0; - PIN(wd) = 0.002; + PIN(wd) = 0.002; PIN(error) = 0.0; } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct vel_int_ctx_t * ctx = (struct vel_int_ctx_t *)ctx_ptr; - struct vel_int_pin_ctx_t * pins = (struct vel_int_pin_ctx_t *)pin_ptr; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct vel_int_ctx_t *ctx = (struct vel_int_ctx_t *)ctx_ptr; + struct vel_int_pin_ctx_t *pins = (struct vel_int_pin_ctx_t *)pin_ptr; - float p = PIN(pos_in); - float v = PIN(vel_in); + float p = PIN(pos_in); + float v = PIN(vel_in); - if(ctx->counter > PIN(wd) && v != 0.0){ - v = 0; - PIN(error) = 1.0; - } - else{ - PIN(error) = 0.0; - ctx->counter += period; - } - - if(EDGE(p)){ - ctx->counter = 0.0; - ctx->pos = p; - } - else{ - ctx->pos += v * period; - } - - ctx->pos = mod(ctx->pos); - - PIN(pos_out) = ctx->pos; - PIN(vel_out) = v; + if(ctx->counter > PIN(wd) && v != 0.0) { + v = 0; + PIN(error) = 1.0; + } else { + PIN(error) = 0.0; + ctx->counter += period; + } + + if(EDGE(p)) { + ctx->counter = 0.0; + ctx->pos = p; + } else { + ctx->pos += v * period; + } + + ctx->pos = mod(ctx->pos); + + PIN(pos_out) = ctx->pos; + PIN(vel_out) = v; } hal_comp_t vel_int_comp_struct = { - .name = "vel_int", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct vel_int_ctx_t), - .pin_count = sizeof(struct vel_int_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "vel_int", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct vel_int_ctx_t), + .pin_count = sizeof(struct vel_int_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/comps/ypid.c b/shared/comps/ypid.c index 584d5137..7cc0565b 100644 --- a/shared/comps/ypid.c +++ b/shared/comps/ypid.c @@ -26,24 +26,24 @@ HAL_COMP(ypid); -HAL_PIN(pos_ext_cmd); // cmd in (rad) -HAL_PIN(pos_fb); // feedback in (rad) -HAL_PIN(pos_error); // error out (rad) +HAL_PIN(pos_ext_cmd); // cmd in (rad) +HAL_PIN(pos_fb); // feedback in (rad) +HAL_PIN(pos_error); // error out (rad) -HAL_PIN(vel_ext_cmd); // cmd in (rad/s) -HAL_PIN(vel_fb); // feedback in (rad/s) -HAL_PIN(vel_cmd); // cmd out (rad/s) -HAL_PIN(vel_error); // error out (rad/s) -HAL_PIN(vel_min);// minimum velocity error 0.3 +HAL_PIN(vel_ext_cmd); // cmd in (rad/s) +HAL_PIN(vel_fb); // feedback in (rad/s) +HAL_PIN(vel_cmd); // cmd out (rad/s) +HAL_PIN(vel_error); // error out (rad/s) +HAL_PIN(vel_min); // minimum velocity error 0.3 HAL_PIN(enable); HAL_PIN(out); -HAL_PIN(pos_p);//10 +HAL_PIN(pos_p); //10 -HAL_PIN(vel_p);//0.5 -HAL_PIN(vel_i);//0.005 -HAL_PIN(vel_ff);//1.0 +HAL_PIN(vel_p); //0.5 +HAL_PIN(vel_i); //0.005 +HAL_PIN(vel_ff); //1.0 // system limits HAL_PIN(max_vel); @@ -54,96 +54,93 @@ HAL_PIN(vel_sat); HAL_PIN(out_sat); HAL_PIN(saturated); -struct ypid_ctx_t{ +struct ypid_ctx_t { float sat; float vel_error_sum; }; -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct ypid_ctx_t * ctx = (struct ypid_ctx_t *)ctx_ptr; - struct ypid_pin_ctx_t * pins = (struct ypid_pin_ctx_t *)pin_ptr; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct ypid_ctx_t *ctx = (struct ypid_ctx_t *)ctx_ptr; + struct ypid_pin_ctx_t *pins = (struct ypid_pin_ctx_t *)pin_ptr; - float vel_cmd; - float vel_fb = PIN(vel_fb); + float vel_cmd; + float vel_fb = PIN(vel_fb); - float vel_error; - float vel_sat; - float out_sat; + float vel_error; + float vel_sat; + float out_sat; - float pos_error = minus(PIN(pos_ext_cmd), PIN(pos_fb)); + float pos_error = minus(PIN(pos_ext_cmd), PIN(pos_fb)); - float pos_p = MAX(PIN(pos_p), 0.0); - float vel_p = MAX(PIN(vel_p), 0.0); - float vel_i = MAX(PIN(vel_i), 0.0); - - float velmin = MAX(-PIN(max_vel), vel_fb - PIN(max_acc) * period); - float velmax = MIN(PIN(max_vel), vel_fb + PIN(max_acc) * period); - - float out; + float pos_p = MAX(PIN(pos_p), 0.0); + float vel_p = MAX(PIN(vel_p), 0.0); + float vel_i = MAX(PIN(vel_i), 0.0); - if(PIN(enable) > 0.0){ - - // pos -> vel - vel_cmd = pos_p * pos_error + PIN(vel_ff) * PIN(vel_ext_cmd); - vel_sat = SAT(vel_cmd,PIN(max_vel)); - vel_cmd = CLAMP(vel_cmd, velmin, velmax); // min/max clamping + float velmin = MAX(-PIN(max_vel), vel_fb - PIN(max_acc) * period); + float velmax = MIN(PIN(max_vel), vel_fb + PIN(max_acc) * period); - // vel -> out - vel_error = vel_cmd - vel_fb; - if(ABS(vel_error) < PIN(vel_min)){ - vel_error = 0; - } - - ctx->vel_error_sum += vel_error; - out = LIMIT(vel_error * vel_p, PIN(max_out)); - if(vel_i * vel_p > 0.0f){ - ctx->vel_error_sum = CLAMP(ctx->vel_error_sum, (-PIN(max_out) - out)/(vel_i * vel_p), (PIN(max_out) - out) / (vel_i * vel_p)); - }else{ - ctx->vel_error_sum = 0; - } - out += ctx->vel_error_sum * vel_i * vel_p; - out_sat = SAT(out,PIN(max_out) * 0.99); - out = LIMIT(out, PIN(max_out)); - - // sat - if(ABS(vel_sat) + ABS(out_sat) > 0.0){ - ctx->sat += period; - } - else{ - ctx->sat = 0.0; - } - } - else{ - ctx->vel_error_sum = 0.0; - vel_cmd = 0.0; - vel_error = 0.0; - vel_sat = 0.0; - out_sat = 0.0; + float out; + + if(PIN(enable) > 0.0) { + // pos -> vel + vel_cmd = pos_p * pos_error + PIN(vel_ff) * PIN(vel_ext_cmd); + vel_sat = SAT(vel_cmd, PIN(max_vel)); + vel_cmd = CLAMP(vel_cmd, velmin, velmax); // min/max clamping + + // vel -> out + vel_error = vel_cmd - vel_fb; + if(ABS(vel_error) < PIN(vel_min)) { + vel_error = 0; + } + + ctx->vel_error_sum += vel_error; + out = LIMIT(vel_error * vel_p, PIN(max_out)); + if(vel_i * vel_p > 0.0f) { + ctx->vel_error_sum = CLAMP(ctx->vel_error_sum, (-PIN(max_out) - out) / (vel_i * vel_p), (PIN(max_out) - out) / (vel_i * vel_p)); + } else { + ctx->vel_error_sum = 0; + } + out += ctx->vel_error_sum * vel_i * vel_p; + out_sat = SAT(out, PIN(max_out) * 0.99); + out = LIMIT(out, PIN(max_out)); + + // sat + if(ABS(vel_sat) + ABS(out_sat) > 0.0) { + ctx->sat += period; + } else { ctx->sat = 0.0; - out = 0.0; - } + } + } else { + ctx->vel_error_sum = 0.0; + vel_cmd = 0.0; + vel_error = 0.0; + vel_sat = 0.0; + out_sat = 0.0; + ctx->sat = 0.0; + out = 0.0; + } - PIN(pos_error) = pos_error; - PIN(vel_cmd) = vel_cmd; - PIN(vel_error) = vel_error; - PIN(out) = out; + PIN(pos_error) = pos_error; + PIN(vel_cmd) = vel_cmd; + PIN(vel_error) = vel_error; + PIN(out) = out; - PIN(vel_sat) = vel_sat; - PIN(out_sat) = out_sat; - PIN(saturated) = ctx->sat; + PIN(vel_sat) = vel_sat; + PIN(out_sat) = out_sat; + PIN(saturated) = ctx->sat; } hal_comp_t ypid_comp_struct = { - .name = "ypid", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = 0, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct ypid_ctx_t), - .pin_count = sizeof(struct ypid_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "ypid", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = 0, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct ypid_ctx_t), + .pin_count = sizeof(struct ypid_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/shared/config.c b/shared/config.c index b08dc248..e317e03c 100644 --- a/shared/config.c +++ b/shared/config.c @@ -6,30 +6,30 @@ extern config_template_t config_templates[]; extern const uint32_t num_of_config_templates; -void link(char * ptr){ - for(int i = 0; i < num_of_config_templates; i++){ - if(strlen(ptr) == strlen(config_templates[i].name) && !strncmp(config_templates[i].name, ptr, strlen(ptr))){ - hal_parse(config_templates[i].config); - return; - } - } +void link(char *ptr) { + for(int i = 0; i < num_of_config_templates; i++) { + if(strlen(ptr) == strlen(config_templates[i].name) && !strncmp(config_templates[i].name, ptr, strlen(ptr))) { + hal_parse(config_templates[i].config); + return; + } + } } COMMAND("link", link, "load config template"); -void show_config_template(char * ptr){ - for(int i = 0; i < num_of_config_templates; i++){ - if(strlen(ptr) == strlen(config_templates[i].name) && !strncmp(config_templates[i].name, ptr, strlen(ptr))){ - printf("\nHAL config template: %s\n", config_templates[i].name); - printf("%s\n", config_templates[i].config); - return; - } - } - printf("HAL config template %s not found\n", ptr); - printf("available templates:\n"); - for(int i = 0; i < num_of_config_templates; i++){ - printf(" - %s\n", config_templates[i].name); - } +void show_config_template(char *ptr) { + for(int i = 0; i < num_of_config_templates; i++) { + if(strlen(ptr) == strlen(config_templates[i].name) && !strncmp(config_templates[i].name, ptr, strlen(ptr))) { + printf("\nHAL config template: %s\n", config_templates[i].name); + printf("%s\n", config_templates[i].config); + return; + } + } + printf("HAL config template %s not found\n", ptr); + printf("available templates:\n"); + for(int i = 0; i < num_of_config_templates; i++) { + printf(" - %s\n", config_templates[i].name); + } } COMMAND("show_config", show_config_template, "show config templates"); diff --git a/shared/config.h b/shared/config.h index e55411a5..a88a78be 100644 --- a/shared/config.h +++ b/shared/config.h @@ -1,6 +1,6 @@ #include "hal.h" typedef struct { - NAME name; - char *config; -}config_template_t; + NAME name; + char *config; +} config_template_t; diff --git a/shared/crc16.c b/shared/crc16.c index 085fb913..d014223e 100644 --- a/shared/crc16.c +++ b/shared/crc16.c @@ -13,7 +13,7 @@ * ReflectOut = True * Algorithm = table-driven *****************************************************************************/ -#include "crc16.h" /* include the header file generated with pycrc */ +#include "crc16.h" /* include the header file generated with pycrc */ #include #include @@ -52,8 +52,7 @@ static const crc16_t crc_table[256] = { 0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40, 0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41, 0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641, - 0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040 -}; + 0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040}; /** * Reflect all bits of a \a data word of \a data_len bytes. @@ -62,17 +61,16 @@ static const crc16_t crc_table[256] = { * \param data_len The width of \a data expressed in number of bits. * \return The reflected data. *****************************************************************************/ -crc16_t crc16_reflect(crc16_t data, size_t data_len) -{ - unsigned int i; - crc16_t ret; +crc16_t crc16_reflect(crc16_t data, size_t data_len) { + unsigned int i; + crc16_t ret; - ret = data & 0x01; - for (i = 1; i < data_len; i++) { - data >>= 1; - ret = (ret << 1) | (data & 0x01); - } - return ret; + ret = data & 0x01; + for(i = 1; i < data_len; i++) { + data >>= 1; + ret = (ret << 1) | (data & 0x01); + } + return ret; } @@ -84,18 +82,15 @@ crc16_t crc16_reflect(crc16_t data, size_t data_len) * \param data_len Number of bytes in the \a data buffer. * \return The updated crc value. *****************************************************************************/ -crc16_t crc16_update(crc16_t crc, const void *data, size_t data_len) -{ - const unsigned char *d = (const unsigned char *)data; - unsigned int tbl_idx; +crc16_t crc16_update(crc16_t crc, const void *data, size_t data_len) { + const unsigned char *d = (const unsigned char *)data; + unsigned int tbl_idx; - while (data_len--) { - tbl_idx = (crc ^ *d) & 0xff; - crc = (crc_table[tbl_idx] ^ (crc >> 8)) & 0xffff; + while(data_len--) { + tbl_idx = (crc ^ *d) & 0xff; + crc = (crc_table[tbl_idx] ^ (crc >> 8)) & 0xffff; - d++; - } - return crc & 0xffff; + d++; + } + return crc & 0xffff; } - - diff --git a/shared/crc16.h b/shared/crc16.h index bf59c37d..4a285e44 100644 --- a/shared/crc16.h +++ b/shared/crc16.h @@ -56,9 +56,8 @@ crc16_t crc16_reflect(crc16_t data, size_t data_len); * * \return The initial crc value. *****************************************************************************/ -static inline crc16_t crc16_init(void) -{ - return 0x0000; +static inline crc16_t crc16_init(void) { + return 0x0000; } @@ -79,14 +78,13 @@ crc16_t crc16_update(crc16_t crc, const void *data, size_t data_len); * \param crc The current crc value. * \return The final crc value. *****************************************************************************/ -static inline crc16_t crc16_finalize(crc16_t crc) -{ - return crc ^ 0x0000; +static inline crc16_t crc16_finalize(crc16_t crc) { + return crc ^ 0x0000; } #ifdef __cplusplus -} /* closing brace for extern "C" */ +} /* closing brace for extern "C" */ #endif -#endif /* __CRC16_H__ */ +#endif /* __CRC16_H__ */ diff --git a/shared/crc32.c b/shared/crc32.c index 8dbe9ddd..4d972d1b 100644 --- a/shared/crc32.c +++ b/shared/crc32.c @@ -13,7 +13,7 @@ * ReflectOut = True * Algorithm = table-driven *****************************************************************************/ -#include "crc32.h" /* include the header file generated with pycrc */ +#include "crc32.h" /* include the header file generated with pycrc */ #include #include @@ -84,8 +84,7 @@ static const crc32_t crc_table[256] = { 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, - 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d -}; + 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d}; /** * Reflect all bits of a \a data word of \a data_len bytes. @@ -94,17 +93,16 @@ static const crc32_t crc_table[256] = { * \param data_len The width of \a data expressed in number of bits. * \return The reflected data. *****************************************************************************/ -crc32_t crc32_reflect(crc32_t data, size_t data_len) -{ - unsigned int i; - crc32_t ret; +crc32_t crc32_reflect(crc32_t data, size_t data_len) { + unsigned int i; + crc32_t ret; - ret = data & 0x01; - for (i = 1; i < data_len; i++) { - data >>= 1; - ret = (ret << 1) | (data & 0x01); - } - return ret; + ret = data & 0x01; + for(i = 1; i < data_len; i++) { + data >>= 1; + ret = (ret << 1) | (data & 0x01); + } + return ret; } @@ -116,18 +114,15 @@ crc32_t crc32_reflect(crc32_t data, size_t data_len) * \param data_len Number of bytes in the \a data buffer. * \return The updated crc value. *****************************************************************************/ -crc32_t crc32_update(crc32_t crc, const void *data, size_t data_len) -{ - const unsigned char *d = (const unsigned char *)data; - unsigned int tbl_idx; +crc32_t crc32_update(crc32_t crc, const void *data, size_t data_len) { + const unsigned char *d = (const unsigned char *)data; + unsigned int tbl_idx; - while (data_len--) { - tbl_idx = (crc ^ *d) & 0xff; - crc = (crc_table[tbl_idx] ^ (crc >> 8)) & 0xffffffff; + while(data_len--) { + tbl_idx = (crc ^ *d) & 0xff; + crc = (crc_table[tbl_idx] ^ (crc >> 8)) & 0xffffffff; - d++; - } - return crc & 0xffffffff; + d++; + } + return crc & 0xffffffff; } - - diff --git a/shared/crc32.h b/shared/crc32.h index 97867ceb..1d0f6d65 100644 --- a/shared/crc32.h +++ b/shared/crc32.h @@ -56,9 +56,8 @@ crc32_t crc32_reflect(crc32_t data, size_t data_len); * * \return The initial crc value. *****************************************************************************/ -static inline crc32_t crc32_init(void) -{ - return 0xffffffff; +static inline crc32_t crc32_init(void) { + return 0xffffffff; } @@ -79,14 +78,13 @@ crc32_t crc32_update(crc32_t crc, const void *data, size_t data_len); * \param crc The current crc value. * \return The final crc value. *****************************************************************************/ -static inline crc32_t crc32_finalize(crc32_t crc) -{ - return crc ^ 0xffffffff; +static inline crc32_t crc32_finalize(crc32_t crc) { + return crc ^ 0xffffffff; } #ifdef __cplusplus -} /* closing brace for extern "C" */ +} /* closing brace for extern "C" */ #endif -#endif /* __CRC32_H__ */ +#endif /* __CRC32_H__ */ diff --git a/shared/crc8.c b/shared/crc8.c index b417a37c..c0cdd442 100644 --- a/shared/crc8.c +++ b/shared/crc8.c @@ -13,7 +13,7 @@ * ReflectOut = True * Algorithm = table-driven *****************************************************************************/ -#include "crc8.h" /* include the header file generated with pycrc */ +#include "crc8.h" /* include the header file generated with pycrc */ #include #include @@ -36,8 +36,7 @@ static const crc8_t crc_table[256] = { 0xca, 0x94, 0x76, 0x28, 0xab, 0xf5, 0x17, 0x49, 0x08, 0x56, 0xb4, 0xea, 0x69, 0x37, 0xd5, 0x8b, 0x57, 0x09, 0xeb, 0xb5, 0x36, 0x68, 0x8a, 0xd4, 0x95, 0xcb, 0x29, 0x77, 0xf4, 0xaa, 0x48, 0x16, 0xe9, 0xb7, 0x55, 0x0b, 0x88, 0xd6, 0x34, 0x6a, 0x2b, 0x75, 0x97, 0xc9, 0x4a, 0x14, 0xf6, 0xa8, - 0x74, 0x2a, 0xc8, 0x96, 0x15, 0x4b, 0xa9, 0xf7, 0xb6, 0xe8, 0x0a, 0x54, 0xd7, 0x89, 0x6b, 0x35 -}; + 0x74, 0x2a, 0xc8, 0x96, 0x15, 0x4b, 0xa9, 0xf7, 0xb6, 0xe8, 0x0a, 0x54, 0xd7, 0x89, 0x6b, 0x35}; /** * Reflect all bits of a \a data word of \a data_len bytes. @@ -46,17 +45,16 @@ static const crc8_t crc_table[256] = { * \param data_len The width of \a data expressed in number of bits. * \return The reflected data. *****************************************************************************/ -crc8_t crc8_reflect(crc8_t data, size_t data_len) -{ - unsigned int i; - crc8_t ret; +crc8_t crc8_reflect(crc8_t data, size_t data_len) { + unsigned int i; + crc8_t ret; - ret = data & 0x01; - for (i = 1; i < data_len; i++) { - data >>= 1; - ret = (ret << 1) | (data & 0x01); - } - return ret; + ret = data & 0x01; + for(i = 1; i < data_len; i++) { + data >>= 1; + ret = (ret << 1) | (data & 0x01); + } + return ret; } @@ -68,18 +66,15 @@ crc8_t crc8_reflect(crc8_t data, size_t data_len) * \param data_len Number of bytes in the \a data buffer. * \return The updated crc value. *****************************************************************************/ -crc8_t crc8_update(crc8_t crc, const void *data, size_t data_len) -{ - const unsigned char *d = (const unsigned char *)data; - unsigned int tbl_idx; +crc8_t crc8_update(crc8_t crc, const void *data, size_t data_len) { + const unsigned char *d = (const unsigned char *)data; + unsigned int tbl_idx; - while (data_len--) { - tbl_idx = (crc ^ *d); - crc = (crc_table[tbl_idx]) & 0xff; + while(data_len--) { + tbl_idx = (crc ^ *d); + crc = (crc_table[tbl_idx]) & 0xff; - d++; - } - return crc & 0xff; + d++; + } + return crc & 0xff; } - - diff --git a/shared/crc8.h b/shared/crc8.h index ecea478c..2534ad9f 100644 --- a/shared/crc8.h +++ b/shared/crc8.h @@ -56,9 +56,8 @@ crc8_t crc8_reflect(crc8_t data, size_t data_len); * * \return The initial crc value. *****************************************************************************/ -static inline crc8_t crc8_init(void) -{ - return 0x00; +static inline crc8_t crc8_init(void) { + return 0x00; } @@ -79,14 +78,13 @@ crc8_t crc8_update(crc8_t crc, const void *data, size_t data_len); * \param crc The current crc value. * \return The final crc value. *****************************************************************************/ -static inline crc8_t crc8_finalize(crc8_t crc) -{ - return crc ^ 0x00; +static inline crc8_t crc8_finalize(crc8_t crc) { + return crc ^ 0x00; } #ifdef __cplusplus -} /* closing brace for extern "C" */ +} /* closing brace for extern "C" */ #endif -#endif /* __CRC8_H__ */ +#endif /* __CRC8_H__ */ diff --git a/shared/defines.h b/shared/defines.h index b158e478..3363f037 100644 --- a/shared/defines.h +++ b/shared/defines.h @@ -27,43 +27,43 @@ extern "C" { #define NO 0 #define YES 1 -#define ABS(a) (((a) < 0.0) ? -(a) : (a)) -#define LIMIT(x, lowhigh) (((x) > (lowhigh)) ? (lowhigh) : (((x) < (-lowhigh)) ? (-lowhigh) : (x))) -#define SAT(x, lowhigh) (((x) > (lowhigh)) ? (1.0) : (((x) < (-lowhigh)) ? (-1.0) : (0.0))) -#define SAT2(x, low, high) (((x) > (high)) ? (1.0) : (((x) < (low)) ? (-1.0) : (0.0))) -#define STEP(from, to, step) (((from) < (to)) ? (MIN((from) + (step), (to))) : (MAX((from) - (step), (to)))) -#define DEG(a) ((a) * M_PI / 180.0) -#define RAD(a) ((a) * 180.0 / M_PI) +#define ABS(a) (((a) < 0.0) ? -(a) : (a)) +#define LIMIT(x, lowhigh) (((x) > (lowhigh)) ? (lowhigh) : (((x) < (-lowhigh)) ? (-lowhigh) : (x))) +#define SAT(x, lowhigh) (((x) > (lowhigh)) ? (1.0) : (((x) < (-lowhigh)) ? (-1.0) : (0.0))) +#define SAT2(x, low, high) (((x) > (high)) ? (1.0) : (((x) < (low)) ? (-1.0) : (0.0))) +#define STEP(from, to, step) (((from) < (to)) ? (MIN((from) + (step), (to))) : (MAX((from) - (step), (to)))) +#define DEG(a) ((a)*M_PI / 180.0) +#define RAD(a) ((a)*180.0 / M_PI) #define SIGN(a) (((a) < 0.0) ? (-1.0) : (((a) > 0.0) ? (1.0) : (0.0))) -#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) -#define SCALE(value, high, max) MIN(MAX(((max) - (value)) / ((max) - (high)), 0.0), 1.0) -#define MIN(a, b) (((a) < (b)) ? (a) : (b)) -#define MAX(a, b) (((a) > (b)) ? (a) : (b)) +#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) +#define SCALE(value, high, max) MIN(MAX(((max) - (value)) / ((max) - (high)), 0.0), 1.0) +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) #define MIN3(a, b, c) MIN(a, MIN(b, c)) #define MAX3(a, b, c) MAX(a, MAX(b, c)) -#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) -#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) +#define FIELD_SIZEOF(t, f) (sizeof(((t *)0)->f)) -#define STATIC_ASSERT(x) _Static_assert(x, #x) +#define STATIC_ASSERT(x) _Static_assert(x, #x) -#define STRINGIFY_(x) #x -#define STRINGIFY(x) STRINGIFY_(x) +#define STRINGIFY_(x) #x +#define STRINGIFY(x) STRINGIFY_(x) //TODO: change type to typeof() -//TODO: change __old_val__ to something more useful +//TODO: change __old_val__ to something more useful //BUG: fails with multiple comps TODO: move to ctx -#define RISING_EDGE(sig)\ -({static float __old_val__ = 0.0; uint8_t ret = (sig) > __old_val__; __old_val__ = (sig); ret;}) +#define RISING_EDGE(sig) \ + ({static float __old_val__ = 0.0; uint8_t ret = (sig) > __old_val__; __old_val__ = (sig); ret; }) -#define FALLING_EDGE(sig)\ -({static float __old_val__ = 0.0; uint8_t ret = (sig) < __old_val__; __old_val__ = (sig); ret;}) +#define FALLING_EDGE(sig) \ + ({static float __old_val__ = 0.0; uint8_t ret = (sig) < __old_val__; __old_val__ = (sig); ret; }) -#define EDGE(sig)\ -({static float __old_val__ = 0.0; uint8_t ret = (sig) != __old_val__; __old_val__ = (sig); ret;}) +#define EDGE(sig) \ + ({static float __old_val__ = 0.0; uint8_t ret = (sig) != __old_val__; __old_val__ = (sig); ret; }) -#define LP_HZ(a) (((a) <= 0.0) ? (1.0) : (1.0 / (1.0/period / ((a) * M_PI * 2.0) + 1.0))) +#define LP_HZ(a) (((a) <= 0.0) ? (1.0) : (1.0 / (1.0 / period / ((a)*M_PI * 2.0) + 1.0))) #ifdef __cplusplus } diff --git a/shared/f3hw.h b/shared/f3hw.h index dd8bcc12..559946b8 100644 --- a/shared/f3hw.h +++ b/shared/f3hw.h @@ -1,9 +1,9 @@ //stmbl -#define AREF 3.338// analog reference voltage +#define AREF 3.338 // analog reference voltage -#define VDIVUP 249000.0 * 2.0//HV div pullup R1,R12 -#define VDIVDOWN 3900.0//HV div pulldown R2,R9 -#define SHUNT 0.003//shunt +#define VDIVUP 249000.0 * 2.0 //HV div pullup R1,R12 +#define VDIVDOWN 3900.0 //HV div pulldown R2,R9 +#define SHUNT 0.003 //shunt #define SHUNT_PULLUP 15000.0 #define SHUNT_SERIE 470.0 diff --git a/shared/hal.c b/shared/hal.c index f1275b5b..dfcd7ff1 100644 --- a/shared/hal.c +++ b/shared/hal.c @@ -8,906 +8,894 @@ hal_t hal; -hal_comp_t * comp_by_name(NAME name){ - for(int i = 0; i < comp_count; i++){ - if(!strncmp(name, comps[i]->name, sizeof(NAME))){ - return(comps[i]); - } - } - return(0); +hal_comp_t *comp_by_name(NAME name) { + for(int i = 0; i < comp_count; i++) { + if(!strncmp(name, comps[i]->name, sizeof(NAME))) { + return (comps[i]); + } + } + return (0); } -volatile hal_comp_inst_t * comp_inst_by_name(NAME name, uint32_t instance){ - hal_comp_t * comp = comp_by_name(name); - if(comp){ - for(int i = 0; i < hal.comp_inst_count; i++){ - if(hal.comp_insts[i].comp == comp && instance == hal.comp_insts[i].instance){ - return(&hal.comp_insts[i]); - } +volatile hal_comp_inst_t *comp_inst_by_name(NAME name, uint32_t instance) { + hal_comp_t *comp = comp_by_name(name); + if(comp) { + for(int i = 0; i < hal.comp_inst_count; i++) { + if(hal.comp_insts[i].comp == comp && instance == hal.comp_insts[i].instance) { + return (&hal.comp_insts[i]); } - } - return(0); + } + } + return (0); } -pin_t * pin_by_name(NAME comp_name, NAME pin_name){ - uint32_t offset = 0; - for(int i = 0; i < comp_count; i++){ - if(!strncmp(comp_name, comps[i]->name, sizeof(NAME))){ - for(int j = 0; j < comps[i]->pin_count; j++){ - if(!strncmp(pin_name, pins[j + offset], sizeof(NAME))){ - return(&pins[j + offset]); - } - } +pin_t *pin_by_name(NAME comp_name, NAME pin_name) { + uint32_t offset = 0; + for(int i = 0; i < comp_count; i++) { + if(!strncmp(comp_name, comps[i]->name, sizeof(NAME))) { + for(int j = 0; j < comps[i]->pin_count; j++) { + if(!strncmp(pin_name, pins[j + offset], sizeof(NAME))) { + return (&pins[j + offset]); + } } - } - return(0); + } + } + return (0); } -volatile hal_pin_inst_t * pin_inst_by_name(NAME comp_name, uint32_t instance, NAME pin_name){ - volatile hal_comp_inst_t * comp = comp_inst_by_name(comp_name, instance); - if(comp){ - for(int i = 0; i < comp->comp->pin_count; i++){ - if(!strncmp(pin_name, comp->pins[i], sizeof(NAME))){ - return(&comp->pin_insts[i]); - } +volatile hal_pin_inst_t *pin_inst_by_name(NAME comp_name, uint32_t instance, NAME pin_name) { + volatile hal_comp_inst_t *comp = comp_inst_by_name(comp_name, instance); + if(comp) { + for(int i = 0; i < comp->comp->pin_count; i++) { + if(!strncmp(pin_name, comp->pins[i], sizeof(NAME))) { + return (&comp->pin_insts[i]); } - } - return(0); + } + } + return (0); } -pin_t * pin_by_pin_inst(volatile hal_pin_inst_t * p){ - for(int i = 0; i < hal.comp_inst_count; i++){ - for(int j = 0; j < hal.comp_insts[i].comp->pin_count; j++){ - if(&(hal.comp_insts[i].pin_insts[j]) == p){ - return(&hal.comp_insts[i].pins[j]); - } +pin_t *pin_by_pin_inst(volatile hal_pin_inst_t *p) { + for(int i = 0; i < hal.comp_inst_count; i++) { + for(int j = 0; j < hal.comp_insts[i].comp->pin_count; j++) { + if(&(hal.comp_insts[i].pin_insts[j]) == p) { + return (&hal.comp_insts[i].pins[j]); } - } - return(0); + } + } + return (0); } -volatile hal_comp_inst_t * comp_inst_by_pin_inst(volatile hal_pin_inst_t * p){ - for(int i = 0; i < hal.comp_inst_count; i++){ - for(int j = 0; j < hal.comp_insts[i].comp->pin_count; j++){ - if(&(hal.comp_insts[i].pin_insts[j]) == p){ - return(&hal.comp_insts[i]); - } +volatile hal_comp_inst_t *comp_inst_by_pin_inst(volatile hal_pin_inst_t *p) { + for(int i = 0; i < hal.comp_inst_count; i++) { + for(int j = 0; j < hal.comp_insts[i].comp->pin_count; j++) { + if(&(hal.comp_insts[i].pin_insts[j]) == p) { + return (&hal.comp_insts[i]); } - } - return(0); + } + } + return (0); } - -void hal_term_print_state(){ - switch(hal.hal_state){ - break; - case HAL_OK2: +void hal_term_print_state() { + switch(hal.hal_state) { + break; + case HAL_OK2: printf("HAL state: HAL_OK2\n"); break; - case RT_TOO_LONG: + case RT_TOO_LONG: printf("HAL state: RT_TOO_LONG\n"); break; - case FRT_TOO_LONG: + case FRT_TOO_LONG: printf("HAL state: FRT_TOO_LONG\n"); break; - case MISC_ERROR: + case MISC_ERROR: printf("HAL state: MISC_ERROR\n"); - switch(hal.error_info.error_handler){ - case HardFault: - printf("error handler: HardFault\n"); - break; - - case NMI: - printf("error handler: NMI\n"); - break; - - case MemManage: - printf("error handler: MemManage\n"); - break; - - case BusFault: - printf("error handler: BusFault\n"); - break; - - case UsageFault: - printf("error handler: UsageFault\n"); - break; + switch(hal.error_info.error_handler) { + case HardFault: + printf("error handler: HardFault\n"); + break; + + case NMI: + printf("error handler: NMI\n"); + break; + + case MemManage: + printf("error handler: MemManage\n"); + break; + + case BusFault: + printf("error handler: BusFault\n"); + break; + + case UsageFault: + printf("error handler: UsageFault\n"); + break; } printf("active rt func: %lu\n", hal.error_info.active_rt_func); printf("active frt func: %lu\n", hal.error_info.active_frt_func); printf("active nrt func: %lu\n", hal.error_info.active_nrt_func); - + break; - case MEM_ERROR: + case MEM_ERROR: printf("HAL state: MEM_ERROR\n"); break; - case CONFIG_LOAD_ERROR: + case CONFIG_LOAD_ERROR: printf("HAL state: CONFIG_LOAD_ERROR\n"); break; - case CONFIG_ERROR: + case CONFIG_ERROR: printf("HAL state: CONFIG_ERROR\n"); break; - case NAN_ERROR: + case NAN_ERROR: printf("HAL state: NAN_ERROR\n"); break; - default: + default: printf("HAL state: unkonwn error\n"); - } + } } -void hal_term_print_info(char * ptr){ - printf("######## hal info ########\n"); - printf("HAL_MAX_COMPS %lu/%i\n", hal.comp_inst_count, HAL_MAX_COMPS); - printf("HAL_MAX_PINS %lu/%i\n", hal.pin_inst_count, HAL_MAX_PINS); - printf("HAL_MAX_CTX %lu/%i\n", hal.ctx_count, HAL_MAX_CTX); - hal_term_print_state(); - - // printf("link errors %lu\n", hal.link_errors); - // printf("pin errors %lu\n", hal.pin_errors); - // printf("comp errors %lu\n", hal.comp_errors); - // printf("set errors %lu\n", hal.set_errors); - // printf("get errors %lu\n", hal.get_errors); - // printf("foo0.bar: %f\n", hal_get_pin("foo0.bar")); - // printf("error_name: %s\n",hal.error_name); - float pe = hal.rt_period; - float ct = (float)hal.rt_ticks / hal_get_systick_freq(); - float mct = (float)hal.rt_max_ticks / hal_get_systick_freq(); - float cct = 0; - float mcct = 0; - struct pin_ctx_t * pins = 0; - printf("\n### rt info ###\n"); - switch(hal.rt_state){ - case RT_STOP: +void hal_term_print_info(char *ptr) { + printf("######## hal info ########\n"); + printf("HAL_MAX_COMPS %lu/%i\n", hal.comp_inst_count, HAL_MAX_COMPS); + printf("HAL_MAX_PINS %lu/%i\n", hal.pin_inst_count, HAL_MAX_PINS); + printf("HAL_MAX_CTX %lu/%i\n", hal.ctx_count, HAL_MAX_CTX); + hal_term_print_state(); + + // printf("link errors %lu\n", hal.link_errors); + // printf("pin errors %lu\n", hal.pin_errors); + // printf("comp errors %lu\n", hal.comp_errors); + // printf("set errors %lu\n", hal.set_errors); + // printf("get errors %lu\n", hal.get_errors); + // printf("foo0.bar: %f\n", hal_get_pin("foo0.bar")); + // printf("error_name: %s\n",hal.error_name); + float pe = hal.rt_period; + float ct = (float)hal.rt_ticks / hal_get_systick_freq(); + float mct = (float)hal.rt_max_ticks / hal_get_systick_freq(); + float cct = 0; + float mcct = 0; + struct pin_ctx_t *pins = 0; + printf("\n### rt info ###\n"); + switch(hal.rt_state) { + case RT_STOP: printf("rt state: STOP\n"); break; - case RT_SLEEP: + case RT_SLEEP: printf("rt state: SLEEP\n"); break; - case RT_CALC: + case RT_CALC: printf("rt state: CALC\n"); break; - } - if(mct > 0.0){ - printf("rt_ticks: %lu(max %lu)\n", hal.rt_ticks, hal.rt_max_ticks); - if(pe > 0.0){ - printf("rt time: %f(max %f)us/%fus = %f(max %f)%% @ %fkHz\n", ct * 1000000.0, mct * 1000000.0, pe * 1000000.0, (ct / pe) * 100.0, (mct / pe) * 100.0, 1.0 / pe / 1000.0); - } - else{ - printf("rt time: %f(max %f)us\n", ct * 1000000.0, mct * 1000000.0); - } - } - printf("active rt funcs(%lu):\n", hal.rt_comp_count); - for(int j = 0; j < hal.rt_comp_count; j++){ - pins = (struct pin_ctx_t *)(hal.rt_comps[j]->pin_insts); - cct = (float)hal.rt_comps[j]->rt_ticks / hal_get_systick_freq(); - mcct = (float)hal.rt_comps[j]->rt_max_ticks / hal_get_systick_freq(); - printf("%s(%f) %f(max %f)us", hal.rt_comps[j]->comp->name, PIN(rt_prio), cct * 1000000.0, mcct * 1000000.0); - if(pe > 0.0){ - printf(" = %f(max %f)%%\n", cct / pe * 100.0, mcct / pe * 100.0); - } - else{ - printf("\n"); - } - } - - - pe = hal.frt_period; - ct = (float)hal.frt_ticks / hal_get_systick_freq(); - mct = (float)hal.frt_max_ticks / hal_get_systick_freq(); - printf("\n### frt info ###\n"); - switch(hal.frt_state){ - case RT_STOP: + } + if(mct > 0.0) { + printf("rt_ticks: %lu(max %lu)\n", hal.rt_ticks, hal.rt_max_ticks); + if(pe > 0.0) { + printf("rt time: %f(max %f)us/%fus = %f(max %f)%% @ %fkHz\n", ct * 1000000.0, mct * 1000000.0, pe * 1000000.0, (ct / pe) * 100.0, (mct / pe) * 100.0, 1.0 / pe / 1000.0); + } else { + printf("rt time: %f(max %f)us\n", ct * 1000000.0, mct * 1000000.0); + } + } + printf("active rt funcs(%lu):\n", hal.rt_comp_count); + for(int j = 0; j < hal.rt_comp_count; j++) { + pins = (struct pin_ctx_t *)(hal.rt_comps[j]->pin_insts); + cct = (float)hal.rt_comps[j]->rt_ticks / hal_get_systick_freq(); + mcct = (float)hal.rt_comps[j]->rt_max_ticks / hal_get_systick_freq(); + printf("%s(%f) %f(max %f)us", hal.rt_comps[j]->comp->name, PIN(rt_prio), cct * 1000000.0, mcct * 1000000.0); + if(pe > 0.0) { + printf(" = %f(max %f)%%\n", cct / pe * 100.0, mcct / pe * 100.0); + } else { + printf("\n"); + } + } + + + pe = hal.frt_period; + ct = (float)hal.frt_ticks / hal_get_systick_freq(); + mct = (float)hal.frt_max_ticks / hal_get_systick_freq(); + printf("\n### frt info ###\n"); + switch(hal.frt_state) { + case RT_STOP: printf("frt state: STOP\n"); break; - case RT_SLEEP: + case RT_SLEEP: printf("frt state: SLEEP\n"); break; - case RT_CALC: + case RT_CALC: printf("frt state: CALC\n"); break; - } - if(mct > 0.0){ - printf("frt_ticks: %lu(max %lu)\n", hal.frt_ticks, hal.frt_max_ticks); - if(pe > 0.0){ - printf("frt time: %f(max %f)us/%fus = %f(max %f)%% @ %fkHz\n", ct * 1000000.0, mct * 1000000.0, pe * 1000000.0, (ct / pe) * 100.0, (mct / pe) * 100.0, 1.0 / pe / 1000.0); - } - else{ - printf("frt time: %f(max %f)us\n", ct * 1000000.0, mct * 1000000.0); - } - } - printf("active frt funcs(%lu):\n", hal.frt_comp_count); - for(int j = 0; j < hal.frt_comp_count; j++){ - pins = (struct pin_ctx_t *)(hal.frt_comps[j]->pin_insts); - cct = (float)hal.frt_comps[j]->frt_ticks / hal_get_systick_freq(); - mcct = (float)hal.frt_comps[j]->frt_max_ticks / hal_get_systick_freq(); - printf("%s(%f) %f(max %f)us", hal.frt_comps[j]->comp->name, PIN(frt_prio), cct * 1000000.0, mcct * 1000000.0); - if(pe > 0.0){ - printf(" = %f(max %f)%%\n", cct / pe * 100.0, mcct / pe * 100.0); - } - else{ - printf("\n"); - } - } - - ct = (float)hal.nrt_ticks / hal_get_systick_freq(); - mct = (float)hal.nrt_max_ticks / hal_get_systick_freq(); - printf("\n### nrt info ###\n"); - if(mct > 0.0){ - printf("nrt time: %f(max %f)us\n", ct * 1000000.0, mct * 1000000.0); - } - printf("active nrt funcs(%lu):\n", hal.comp_inst_count); - for(int j = 0; j < hal.comp_inst_count; j++){ - if(hal.comp_insts[j].comp->nrt){ - pins = (struct pin_ctx_t *)(hal.comp_insts[j].pin_insts); - - cct = (float)hal.comp_insts[j].nrt_ticks / hal_get_systick_freq(); - mcct = (float)hal.comp_insts[j].nrt_max_ticks / hal_get_systick_freq(); - printf("%s %f(max %f)us\n", hal.comp_insts[j].comp->name, cct * 1000000.0, mcct * 1000000.0); - } - } + } + if(mct > 0.0) { + printf("frt_ticks: %lu(max %lu)\n", hal.frt_ticks, hal.frt_max_ticks); + if(pe > 0.0) { + printf("frt time: %f(max %f)us/%fus = %f(max %f)%% @ %fkHz\n", ct * 1000000.0, mct * 1000000.0, pe * 1000000.0, (ct / pe) * 100.0, (mct / pe) * 100.0, 1.0 / pe / 1000.0); + } else { + printf("frt time: %f(max %f)us\n", ct * 1000000.0, mct * 1000000.0); + } + } + printf("active frt funcs(%lu):\n", hal.frt_comp_count); + for(int j = 0; j < hal.frt_comp_count; j++) { + pins = (struct pin_ctx_t *)(hal.frt_comps[j]->pin_insts); + cct = (float)hal.frt_comps[j]->frt_ticks / hal_get_systick_freq(); + mcct = (float)hal.frt_comps[j]->frt_max_ticks / hal_get_systick_freq(); + printf("%s(%f) %f(max %f)us", hal.frt_comps[j]->comp->name, PIN(frt_prio), cct * 1000000.0, mcct * 1000000.0); + if(pe > 0.0) { + printf(" = %f(max %f)%%\n", cct / pe * 100.0, mcct / pe * 100.0); + } else { + printf("\n"); + } + } + + ct = (float)hal.nrt_ticks / hal_get_systick_freq(); + mct = (float)hal.nrt_max_ticks / hal_get_systick_freq(); + printf("\n### nrt info ###\n"); + if(mct > 0.0) { + printf("nrt time: %f(max %f)us\n", ct * 1000000.0, mct * 1000000.0); + } + printf("active nrt funcs(%lu):\n", hal.comp_inst_count); + for(int j = 0; j < hal.comp_inst_count; j++) { + if(hal.comp_insts[j].comp->nrt) { + pins = (struct pin_ctx_t *)(hal.comp_insts[j].pin_insts); + + cct = (float)hal.comp_insts[j].nrt_ticks / hal_get_systick_freq(); + mcct = (float)hal.comp_insts[j].nrt_max_ticks / hal_get_systick_freq(); + printf("%s %f(max %f)us\n", hal.comp_insts[j].comp->name, cct * 1000000.0, mcct * 1000000.0); + } + } } COMMAND("hal", hal_term_print_info, "print HAL stats"); -uint32_t load_comp(hal_comp_t * comp){ - if(!comp){ - printf("load_comp: not found\n"); - return(0); - } - if(hal.comp_inst_count >= HAL_MAX_COMPS - 1){ - printf("load_comp: not enough space to load comp: %s\n", comp->name); - return(0); - } - if(hal.pin_inst_count + comp->pin_count >= HAL_MAX_PINS - 1){ - printf("load_comp: not enough space to load comp pins: %s\n", comp->name); - return(0); - } - uint32_t ctx_size = ((uint32_t)ceil((comp->ctx_size / 4.0))) * 4; - if(hal.ctx_count + ctx_size >= HAL_MAX_CTX - 1){ - printf("load_comp: not enough space to load comp ctx: %s\n", comp->name); - return(0); - } - - // load comp - hal.comp_insts[hal.comp_inst_count].comp = comp; - hal.comp_insts[hal.comp_inst_count].ctx = &hal.ctxs[hal.ctx_count]; - hal.comp_insts[hal.comp_inst_count].pin_insts = &hal.pin_insts[hal.pin_inst_count]; - hal.comp_insts[hal.comp_inst_count].ctx_size = ctx_size; - hal.comp_insts[hal.comp_inst_count].state = PRE_INIT; - - uint32_t offset = 0; - for(int i = 0; i < comp_count; i++){ - if(comps[i] == comp){ - hal.comp_insts[hal.comp_inst_count].pins = &pins[offset]; - } - offset += comps[i]->pin_count; - } - hal.comp_insts[hal.comp_inst_count].instance = 0; - for(int i = 0; i < hal.comp_inst_count; i++){ - if(hal.comp_insts[i].comp == comp){ - hal.comp_insts[hal.comp_inst_count].instance++; - } - } - - - // load pins - for(int i = hal.pin_inst_count; i < hal.pin_inst_count + comp->pin_count; i++){ - hal.pin_insts[i].value = 0.0; - hal.pin_insts[i].source = &hal.pin_insts[i]; - } - hal.pin_inst_count += comp->pin_count; - - // load ctx - for(int i = hal.ctx_count; i < hal.ctx_count + ctx_size; i++){ - hal.ctxs[i] = 0; - } +uint32_t load_comp(hal_comp_t *comp) { + if(!comp) { + printf("load_comp: not found\n"); + return (0); + } + if(hal.comp_inst_count >= HAL_MAX_COMPS - 1) { + printf("load_comp: not enough space to load comp: %s\n", comp->name); + return (0); + } + if(hal.pin_inst_count + comp->pin_count >= HAL_MAX_PINS - 1) { + printf("load_comp: not enough space to load comp pins: %s\n", comp->name); + return (0); + } + uint32_t ctx_size = ((uint32_t)ceil((comp->ctx_size / 4.0))) * 4; + if(hal.ctx_count + ctx_size >= HAL_MAX_CTX - 1) { + printf("load_comp: not enough space to load comp ctx: %s\n", comp->name); + return (0); + } - hal.ctx_count += ctx_size; - - if(hal.comp_insts[hal.comp_inst_count].comp->nrt_init != 0){ - hal.comp_insts[hal.comp_inst_count].comp->nrt_init(hal.comp_insts[hal.comp_inst_count].ctx, hal.comp_insts[hal.comp_inst_count].pin_insts); - } - hal.comp_insts[hal.comp_inst_count].nrt_ticks = 0; - hal.comp_insts[hal.comp_inst_count].nrt_max_ticks = 0; - hal.comp_insts[hal.comp_inst_count].state = PRE_HW_INIT; - - hal.comp_inst_count++; - - return(1); + // load comp + hal.comp_insts[hal.comp_inst_count].comp = comp; + hal.comp_insts[hal.comp_inst_count].ctx = &hal.ctxs[hal.ctx_count]; + hal.comp_insts[hal.comp_inst_count].pin_insts = &hal.pin_insts[hal.pin_inst_count]; + hal.comp_insts[hal.comp_inst_count].ctx_size = ctx_size; + hal.comp_insts[hal.comp_inst_count].state = PRE_INIT; + + uint32_t offset = 0; + for(int i = 0; i < comp_count; i++) { + if(comps[i] == comp) { + hal.comp_insts[hal.comp_inst_count].pins = &pins[offset]; + } + offset += comps[i]->pin_count; + } + hal.comp_insts[hal.comp_inst_count].instance = 0; + for(int i = 0; i < hal.comp_inst_count; i++) { + if(hal.comp_insts[i].comp == comp) { + hal.comp_insts[hal.comp_inst_count].instance++; + } + } + + + // load pins + for(int i = hal.pin_inst_count; i < hal.pin_inst_count + comp->pin_count; i++) { + hal.pin_insts[i].value = 0.0; + hal.pin_insts[i].source = &hal.pin_insts[i]; + } + hal.pin_inst_count += comp->pin_count; + + // load ctx + for(int i = hal.ctx_count; i < hal.ctx_count + ctx_size; i++) { + hal.ctxs[i] = 0; + } + + hal.ctx_count += ctx_size; + + if(hal.comp_insts[hal.comp_inst_count].comp->nrt_init != 0) { + hal.comp_insts[hal.comp_inst_count].comp->nrt_init(hal.comp_insts[hal.comp_inst_count].ctx, hal.comp_insts[hal.comp_inst_count].pin_insts); + } + hal.comp_insts[hal.comp_inst_count].nrt_ticks = 0; + hal.comp_insts[hal.comp_inst_count].nrt_max_ticks = 0; + hal.comp_insts[hal.comp_inst_count].state = PRE_HW_INIT; + + hal.comp_inst_count++; + + return (1); } -void hal_run_rt(){ - #ifdef HAL_CALC_TIME - uint32_t hal_start = hal_get_systick_value(); - #endif - - switch(hal.rt_state){ - case RT_STOP: - return; - case RT_CALC: // call stop - hal.rt_state = RT_STOP; - hal.hal_state = RT_TOO_LONG; - hal.frt_state = RT_STOP; - return; - case RT_SLEEP: - if(hal.active_rt_func > -1){ // call stop - hal.rt_state = RT_STOP; - hal.hal_state = MISC_ERROR; - hal.frt_state = RT_STOP; - return; - } - hal.rt_state = RT_CALC; - } - - #ifdef HAL_COMP_CALC_TIME - uint32_t start = hal_get_systick_value(); - #endif +void hal_run_rt() { +#ifdef HAL_CALC_TIME + uint32_t hal_start = hal_get_systick_value(); +#endif - for(hal.active_rt_func = 0; hal.active_rt_func < hal.rt_comp_count; hal.active_rt_func++){ - hal.rt_comps[hal.active_rt_func]->comp->rt(hal.rt_period, hal.rt_comps[hal.active_rt_func]->ctx, hal.rt_comps[hal.active_rt_func]->pin_insts); - #ifdef HAL_COMP_CALC_TIME - uint32_t end_ticks = hal_get_systick_value(); - if(start < end_ticks){ - start += hal_get_systick_reload(); - } - hal.rt_comps[hal.active_rt_func]->rt_ticks = start - end_ticks; - hal.rt_comps[hal.active_rt_func]->rt_max_ticks = MAX(hal.rt_comps[hal.active_rt_func]->rt_max_ticks, hal.rt_comps[hal.active_rt_func]->rt_ticks); - start = end_ticks; - #endif - } - hal.active_rt_func = -1; - - if(hal.rt_state == RT_CALC){ - hal.rt_state = RT_SLEEP; - } - - #ifdef HAL_CALC_TIME - uint32_t hal_end = hal_get_systick_value(); - if(hal_start < hal_end){ - hal_start += hal_get_systick_reload(); + switch(hal.rt_state) { + case RT_STOP: + return; + case RT_CALC: // call stop + hal.rt_state = RT_STOP; + hal.hal_state = RT_TOO_LONG; + hal.frt_state = RT_STOP; + return; + case RT_SLEEP: + if(hal.active_rt_func > -1) { // call stop + hal.rt_state = RT_STOP; + hal.hal_state = MISC_ERROR; + hal.frt_state = RT_STOP; + return; } - hal.rt_ticks = hal_start - hal_end; - hal.rt_max_ticks = MAX(hal.rt_max_ticks, hal.rt_ticks); - #endif - - #ifdef HAL_WATCHDOG - hal_reset_watchdog(); - #endif + hal.rt_state = RT_CALC; + } + +#ifdef HAL_COMP_CALC_TIME + uint32_t start = hal_get_systick_value(); +#endif + + for(hal.active_rt_func = 0; hal.active_rt_func < hal.rt_comp_count; hal.active_rt_func++) { + hal.rt_comps[hal.active_rt_func]->comp->rt(hal.rt_period, hal.rt_comps[hal.active_rt_func]->ctx, hal.rt_comps[hal.active_rt_func]->pin_insts); +#ifdef HAL_COMP_CALC_TIME + uint32_t end_ticks = hal_get_systick_value(); + if(start < end_ticks) { + start += hal_get_systick_reload(); + } + hal.rt_comps[hal.active_rt_func]->rt_ticks = start - end_ticks; + hal.rt_comps[hal.active_rt_func]->rt_max_ticks = MAX(hal.rt_comps[hal.active_rt_func]->rt_max_ticks, hal.rt_comps[hal.active_rt_func]->rt_ticks); + start = end_ticks; +#endif + } + hal.active_rt_func = -1; + + if(hal.rt_state == RT_CALC) { + hal.rt_state = RT_SLEEP; + } + +#ifdef HAL_CALC_TIME + uint32_t hal_end = hal_get_systick_value(); + if(hal_start < hal_end) { + hal_start += hal_get_systick_reload(); + } + hal.rt_ticks = hal_start - hal_end; + hal.rt_max_ticks = MAX(hal.rt_max_ticks, hal.rt_ticks); +#endif + +#ifdef HAL_WATCHDOG + hal_reset_watchdog(); +#endif } -void hal_run_frt(){ - #ifdef HAL_CALC_TIME - uint32_t hal_start = hal_get_systick_value(); - #endif +void hal_run_frt() { +#ifdef HAL_CALC_TIME + uint32_t hal_start = hal_get_systick_value(); +#endif - switch(hal.frt_state){ - case RT_STOP: - return; - case RT_CALC: - hal.rt_state = RT_STOP; - hal.hal_state = FRT_TOO_LONG; - hal.frt_state = RT_STOP; - return; - case RT_SLEEP: - if(hal.active_frt_func > -1){ - hal.rt_state = RT_STOP; - hal.hal_state = MISC_ERROR; - hal.frt_state = RT_STOP; - return; - } - hal.frt_state = RT_CALC; - } - - #ifdef HAL_COMP_CALC_TIME - uint32_t start = hal_get_systick_value(); - #endif - - for(hal.active_frt_func = 0; hal.active_frt_func < hal.frt_comp_count; hal.active_frt_func++){ - hal.frt_comps[hal.active_frt_func]->comp->frt(hal.frt_period, hal.frt_comps[hal.active_frt_func]->ctx, hal.frt_comps[hal.active_frt_func]->pin_insts); - #ifdef HAL_COMP_CALC_TIME - uint32_t end_ticks = hal_get_systick_value(); - if(start < end_ticks){ - start += hal_get_systick_reload(); - } - hal.frt_comps[hal.active_frt_func]->frt_ticks = start - end_ticks; - hal.frt_comps[hal.active_frt_func]->frt_max_ticks = MAX(hal.frt_comps[hal.active_frt_func]->frt_max_ticks, hal.frt_comps[hal.active_frt_func]->frt_ticks); - start = end_ticks; - #endif - } - hal.active_frt_func = -1; - - if(hal.frt_state == RT_CALC){ - hal.frt_state = RT_SLEEP; - } - - #ifdef HAL_CALC_TIME - uint32_t hal_end = hal_get_systick_value(); - if(hal_start < hal_end){ - hal_start += hal_get_systick_reload(); + switch(hal.frt_state) { + case RT_STOP: + return; + case RT_CALC: + hal.rt_state = RT_STOP; + hal.hal_state = FRT_TOO_LONG; + hal.frt_state = RT_STOP; + return; + case RT_SLEEP: + if(hal.active_frt_func > -1) { + hal.rt_state = RT_STOP; + hal.hal_state = MISC_ERROR; + hal.frt_state = RT_STOP; + return; } - hal.frt_ticks = hal_start - hal_end; - hal.frt_max_ticks = MAX(hal.frt_max_ticks, hal.frt_ticks); - #endif - - #ifdef HAL_WATCHDOG - hal_reset_watchdog(); - #endif + hal.frt_state = RT_CALC; + } + +#ifdef HAL_COMP_CALC_TIME + uint32_t start = hal_get_systick_value(); +#endif + + for(hal.active_frt_func = 0; hal.active_frt_func < hal.frt_comp_count; hal.active_frt_func++) { + hal.frt_comps[hal.active_frt_func]->comp->frt(hal.frt_period, hal.frt_comps[hal.active_frt_func]->ctx, hal.frt_comps[hal.active_frt_func]->pin_insts); +#ifdef HAL_COMP_CALC_TIME + uint32_t end_ticks = hal_get_systick_value(); + if(start < end_ticks) { + start += hal_get_systick_reload(); + } + hal.frt_comps[hal.active_frt_func]->frt_ticks = start - end_ticks; + hal.frt_comps[hal.active_frt_func]->frt_max_ticks = MAX(hal.frt_comps[hal.active_frt_func]->frt_max_ticks, hal.frt_comps[hal.active_frt_func]->frt_ticks); + start = end_ticks; +#endif + } + hal.active_frt_func = -1; + + if(hal.frt_state == RT_CALC) { + hal.frt_state = RT_SLEEP; + } + +#ifdef HAL_CALC_TIME + uint32_t hal_end = hal_get_systick_value(); + if(hal_start < hal_end) { + hal_start += hal_get_systick_reload(); + } + hal.frt_ticks = hal_start - hal_end; + hal.frt_max_ticks = MAX(hal.frt_max_ticks, hal.frt_ticks); +#endif + +#ifdef HAL_WATCHDOG + hal_reset_watchdog(); +#endif } -void hal_run_nrt(){ - #ifdef HAL_CALC_TIME - uint32_t hal_start = hal_get_systick_value(); - #endif - - #ifdef HAL_COMP_CALC_TIME - uint32_t start = hal_get_systick_value(); - #endif - - for(hal.active_nrt_func = 0; hal.active_nrt_func < hal.comp_inst_count; hal.active_nrt_func++){ - if(hal.comp_insts[hal.active_nrt_func].comp->nrt != 0){ - hal.comp_insts[hal.active_nrt_func].comp->nrt(hal.comp_insts[hal.active_nrt_func].ctx, hal.comp_insts[hal.active_nrt_func].pin_insts); - #ifdef HAL_COMP_CALC_TIME - uint32_t end_ticks = hal_get_systick_value(); - if(start < end_ticks){ - start += hal_get_systick_reload(); - } - hal.comp_insts[hal.active_nrt_func].nrt_ticks = start - end_ticks; - hal.comp_insts[hal.active_nrt_func].nrt_max_ticks = MAX(hal.comp_insts[hal.active_nrt_func].nrt_max_ticks, hal.comp_insts[hal.active_nrt_func].nrt_ticks); - start = end_ticks; - #endif +void hal_run_nrt() { +#ifdef HAL_CALC_TIME + uint32_t hal_start = hal_get_systick_value(); +#endif + +#ifdef HAL_COMP_CALC_TIME + uint32_t start = hal_get_systick_value(); +#endif + + for(hal.active_nrt_func = 0; hal.active_nrt_func < hal.comp_inst_count; hal.active_nrt_func++) { + if(hal.comp_insts[hal.active_nrt_func].comp->nrt != 0) { + hal.comp_insts[hal.active_nrt_func].comp->nrt(hal.comp_insts[hal.active_nrt_func].ctx, hal.comp_insts[hal.active_nrt_func].pin_insts); +#ifdef HAL_COMP_CALC_TIME + uint32_t end_ticks = hal_get_systick_value(); + if(start < end_ticks) { + start += hal_get_systick_reload(); } - } - hal.active_nrt_func = -1; - - #ifdef HAL_CALC_TIME - uint32_t hal_end = hal_get_systick_value(); - if(hal_start < hal_end){ - hal_start += hal_get_systick_reload(); - } - hal.nrt_ticks = hal_start - hal_end; - hal.nrt_max_ticks = MAX(hal.nrt_max_ticks, hal.nrt_ticks); - #endif - - #ifdef HAL_WATCHDOG - hal_reset_watchdog(); - #endif + hal.comp_insts[hal.active_nrt_func].nrt_ticks = start - end_ticks; + hal.comp_insts[hal.active_nrt_func].nrt_max_ticks = MAX(hal.comp_insts[hal.active_nrt_func].nrt_max_ticks, hal.comp_insts[hal.active_nrt_func].nrt_ticks); + start = end_ticks; +#endif + } + } + hal.active_nrt_func = -1; + +#ifdef HAL_CALC_TIME + uint32_t hal_end = hal_get_systick_value(); + if(hal_start < hal_end) { + hal_start += hal_get_systick_reload(); + } + hal.nrt_ticks = hal_start - hal_end; + hal.nrt_max_ticks = MAX(hal.nrt_max_ticks, hal.nrt_ticks); +#endif + +#ifdef HAL_WATCHDOG + hal_reset_watchdog(); +#endif } -void hal_init_hw(){ - for(int i = 0; i < hal.comp_inst_count; i++){ - if(hal.comp_insts[i].state == PRE_HW_INIT){ - if(hal.comp_insts[i].comp->hw_init != 0){ - hal.comp_insts[i].comp->hw_init(hal.comp_insts[i].ctx, hal.comp_insts[i].pin_insts); - } - hal.comp_insts[i].state = STARTED; +void hal_init_hw() { + for(int i = 0; i < hal.comp_inst_count; i++) { + if(hal.comp_insts[i].state == PRE_HW_INIT) { + if(hal.comp_insts[i].comp->hw_init != 0) { + hal.comp_insts[i].comp->hw_init(hal.comp_insts[i].ctx, hal.comp_insts[i].pin_insts); } - } + hal.comp_insts[i].state = STARTED; + } + } } -void load(char * ptr){ - printf("load :%s:\n", ptr); - load_comp(comp_by_name(ptr)); +void load(char *ptr) { + printf("load :%s:\n", ptr); + load_comp(comp_by_name(ptr)); } COMMAND("load", load, "load comp from flash"); -void show(char * ptr){ - int j = 0; - int pin_offset = 0; - for(int i = 0; i < comp_count; i++){ - printf("%s\n", comps[i]->name); - printf("#pins: %lu\n", comps[i]->pin_count); - printf("#ctx: %lu byte\n", comps[i]->ctx_size); - for(; j < pin_offset + comps[i]->pin_count; j++){ - printf("- %s\n", pins[j]); - } - pin_offset += comps[i]->pin_count; - } +void show(char *ptr) { + int j = 0; + int pin_offset = 0; + for(int i = 0; i < comp_count; i++) { + printf("%s\n", comps[i]->name); + printf("#pins: %lu\n", comps[i]->pin_count); + printf("#ctx: %lu byte\n", comps[i]->ctx_size); + for(; j < pin_offset + comps[i]->pin_count; j++) { + printf("- %s\n", pins[j]); + } + pin_offset += comps[i]->pin_count; + } } COMMAND("show", show, "show comps in flash"); -void list(char * ptr){ - for(int i = 0; i < hal.comp_inst_count; i++){ - printf("%s%lu:\n", hal.comp_insts[i].comp->name, hal.comp_insts[i].instance); - printf("#pins: %lu\n", hal.comp_insts[i].comp->pin_count); - printf("#ctx: %lu byte\n", hal.comp_insts[i].comp->ctx_size); - printf("real #ctx: %lu byte\n", hal.comp_insts[i].ctx_size); - printf("*pins: 0x%x\n", (unsigned int)hal.comp_insts[i].pin_insts); - printf("*ctx: 0x%x\n", (unsigned int)hal.comp_insts[i].ctx); - printf("state: "); - switch(hal.comp_insts[i].state) { - case PRE_INIT: - printf("PRE_INIT\n"); - break; - case PRE_HW_INIT: - printf("PRE_HW_INIT\n"); - break; - case STARTED: - printf("STARTED\n"); - break; - default: - printf("unknown\n"); - } - for(int j = 0; j < hal.comp_insts[i].comp->pin_count; j++){ - volatile hal_comp_inst_t * comp = comp_inst_by_pin_inst(hal.comp_insts[i].pin_insts[j].source->source); - printf("- %s <= %s%lu.%s = %f\n", hal.comp_insts[i].pins[j], comp->comp->name, comp->instance, (char *)pin_by_pin_inst(hal.comp_insts[i].pin_insts[j].source->source), hal.comp_insts[i].pin_insts[j].source->source->value); - } - } +void list(char *ptr) { + for(int i = 0; i < hal.comp_inst_count; i++) { + printf("%s%lu:\n", hal.comp_insts[i].comp->name, hal.comp_insts[i].instance); + printf("#pins: %lu\n", hal.comp_insts[i].comp->pin_count); + printf("#ctx: %lu byte\n", hal.comp_insts[i].comp->ctx_size); + printf("real #ctx: %lu byte\n", hal.comp_insts[i].ctx_size); + printf("*pins: 0x%x\n", (unsigned int)hal.comp_insts[i].pin_insts); + printf("*ctx: 0x%x\n", (unsigned int)hal.comp_insts[i].ctx); + printf("state: "); + switch(hal.comp_insts[i].state) { + case PRE_INIT: + printf("PRE_INIT\n"); + break; + case PRE_HW_INIT: + printf("PRE_HW_INIT\n"); + break; + case STARTED: + printf("STARTED\n"); + break; + default: + printf("unknown\n"); + } + for(int j = 0; j < hal.comp_insts[i].comp->pin_count; j++) { + volatile hal_comp_inst_t *comp = comp_inst_by_pin_inst(hal.comp_insts[i].pin_insts[j].source->source); + printf("- %s <= %s%lu.%s = %f\n", hal.comp_insts[i].pins[j], comp->comp->name, comp->instance, (char *)pin_by_pin_inst(hal.comp_insts[i].pin_insts[j].source->source), hal.comp_insts[i].pin_insts[j].source->source->value); + } + } } COMMAND("list", list, "show comp instances"); -void sort_rt(){ - float min = INFINITY; - int min_index = -1; - float rt_prio = 0.0; - char added[HAL_MAX_COMPS]; - struct pin_ctx_t * pins; +void sort_rt() { + float min = INFINITY; + int min_index = -1; + float rt_prio = 0.0; + char added[HAL_MAX_COMPS]; + struct pin_ctx_t *pins; - for(int i = 0; i < hal.comp_inst_count; i++){ - added[i] = 0; - } + for(int i = 0; i < hal.comp_inst_count; i++) { + added[i] = 0; + } - hal.rt_comp_count = 0; - for(int i = 0; i < hal.comp_inst_count; i++){ - min = INFINITY; - min_index = -1; - for(int j = hal.comp_inst_count - 1; j >= 0; j--){ - pins = (struct pin_ctx_t *)(hal.comp_insts[j].pin_insts); - rt_prio = PIN(rt_prio); - if(rt_prio <= min && added[j] == 0 && rt_prio > 0.0 && hal.comp_insts[j].comp->rt != 0){ - min = rt_prio; - min_index = j; - } + hal.rt_comp_count = 0; + for(int i = 0; i < hal.comp_inst_count; i++) { + min = INFINITY; + min_index = -1; + for(int j = hal.comp_inst_count - 1; j >= 0; j--) { + pins = (struct pin_ctx_t *)(hal.comp_insts[j].pin_insts); + rt_prio = PIN(rt_prio); + if(rt_prio <= min && added[j] == 0 && rt_prio > 0.0 && hal.comp_insts[j].comp->rt != 0) { + min = rt_prio; + min_index = j; } - if(min_index >= 0){ - added[min_index] = 1; - hal.rt_comps[hal.rt_comp_count++] = &hal.comp_insts[min_index]; - } - } + } + if(min_index >= 0) { + added[min_index] = 1; + hal.rt_comps[hal.rt_comp_count++] = &hal.comp_insts[min_index]; + } + } } -void sort_frt(){ - float min = INFINITY; - int min_index = -1; - float frt_prio = 0.0; - char added[HAL_MAX_COMPS]; - struct pin_ctx_t * pins; +void sort_frt() { + float min = INFINITY; + int min_index = -1; + float frt_prio = 0.0; + char added[HAL_MAX_COMPS]; + struct pin_ctx_t *pins; - for(int i = 0; i < hal.comp_inst_count; i++){ - added[i] = 0; - } + for(int i = 0; i < hal.comp_inst_count; i++) { + added[i] = 0; + } - hal.frt_comp_count = 0; - for(int i = 0; i < hal.comp_inst_count; i++){ - min = INFINITY; - min_index = -1; - for(int j = hal.comp_inst_count - 1; j >= 0; j--){ - pins = (struct pin_ctx_t *)(hal.comp_insts[j].pin_insts); - frt_prio = PIN(frt_prio); - if(frt_prio <= min && added[j] == 0 && frt_prio > 0.0 && hal.comp_insts[j].comp->frt != 0){ - min = frt_prio; - min_index = j; - } + hal.frt_comp_count = 0; + for(int i = 0; i < hal.comp_inst_count; i++) { + min = INFINITY; + min_index = -1; + for(int j = hal.comp_inst_count - 1; j >= 0; j--) { + pins = (struct pin_ctx_t *)(hal.comp_insts[j].pin_insts); + frt_prio = PIN(frt_prio); + if(frt_prio <= min && added[j] == 0 && frt_prio > 0.0 && hal.comp_insts[j].comp->frt != 0) { + min = frt_prio; + min_index = j; } - if(min_index >= 0){ - added[min_index] = 1; - hal.frt_comps[hal.frt_comp_count++] = &hal.comp_insts[min_index]; - } - } + } + if(min_index >= 0) { + added[min_index] = 1; + hal.frt_comps[hal.frt_comp_count++] = &hal.comp_insts[min_index]; + } + } } -void start_rt(){ - for(int i = 0; i < hal.rt_comp_count; i++){ - if(hal.rt_comps[i]->comp->rt_start != 0){ - hal.rt_comps[i]->comp->rt_start(hal.rt_comps[i]->ctx, hal.rt_comps[i]->pin_insts); - } - hal.rt_comps[i]->rt_ticks = 0; - hal.rt_comps[i]->rt_max_ticks = 0; - } - - hal.rt_ticks = 0.0; - hal.rt_max_ticks = 0.0; - - hal.rt_state = RT_SLEEP; +void start_rt() { + for(int i = 0; i < hal.rt_comp_count; i++) { + if(hal.rt_comps[i]->comp->rt_start != 0) { + hal.rt_comps[i]->comp->rt_start(hal.rt_comps[i]->ctx, hal.rt_comps[i]->pin_insts); + } + hal.rt_comps[i]->rt_ticks = 0; + hal.rt_comps[i]->rt_max_ticks = 0; + } + + hal.rt_ticks = 0.0; + hal.rt_max_ticks = 0.0; + + hal.rt_state = RT_SLEEP; } -void start_frt(){ - for(int i = 0; i < hal.frt_comp_count; i++){ - if(hal.frt_comps[i]->comp->frt_start != 0){ - hal.frt_comps[i]->comp->frt_start(hal.frt_comps[i]->ctx, hal.frt_comps[i]->pin_insts); - } - hal.frt_comps[i]->frt_ticks = 0; - hal.frt_comps[i]->frt_max_ticks = 0; - } - - hal.frt_ticks = 0.0; - hal.frt_max_ticks = 0.0; - - hal.frt_state = RT_SLEEP; +void start_frt() { + for(int i = 0; i < hal.frt_comp_count; i++) { + if(hal.frt_comps[i]->comp->frt_start != 0) { + hal.frt_comps[i]->comp->frt_start(hal.frt_comps[i]->ctx, hal.frt_comps[i]->pin_insts); + } + hal.frt_comps[i]->frt_ticks = 0; + hal.frt_comps[i]->frt_max_ticks = 0; + } + + hal.frt_ticks = 0.0; + hal.frt_max_ticks = 0.0; + + hal.frt_state = RT_SLEEP; } -void hal_start(){ - hal.hal_state = HAL_OK2; - - sort_rt(); - sort_frt(); - hal_init_hw(); - start_rt(); - start_frt(); +void hal_start() { + hal.hal_state = HAL_OK2; + + sort_rt(); + sort_frt(); + hal_init_hw(); + start_rt(); + start_frt(); } COMMAND("start", hal_start, "start rt system"); -void stop_rt(){ - hal.rt_state = RT_STOP; - - for(int i = 0; i < hal.rt_comp_count; i++){ - if(hal.rt_comps[i]->comp->rt_stop != 0){ - hal.rt_comps[i]->comp->rt_stop(hal.rt_comps[i]->ctx, hal.rt_comps[i]->pin_insts); - } - } +void stop_rt() { + hal.rt_state = RT_STOP; + + for(int i = 0; i < hal.rt_comp_count; i++) { + if(hal.rt_comps[i]->comp->rt_stop != 0) { + hal.rt_comps[i]->comp->rt_stop(hal.rt_comps[i]->ctx, hal.rt_comps[i]->pin_insts); + } + } } -void stop_frt(){ - hal.frt_state = RT_STOP; - - for(int i = 0; i < hal.frt_comp_count; i++){ - if(hal.frt_comps[i]->comp->frt_stop != 0){ - hal.frt_comps[i]->comp->frt_stop(hal.frt_comps[i]->ctx, hal.frt_comps[i]->pin_insts); - } - } +void stop_frt() { + hal.frt_state = RT_STOP; + + for(int i = 0; i < hal.frt_comp_count; i++) { + if(hal.frt_comps[i]->comp->frt_stop != 0) { + hal.frt_comps[i]->comp->frt_stop(hal.frt_comps[i]->ctx, hal.frt_comps[i]->pin_insts); + } + } } -void hal_stop(){ - stop_rt(); - stop_frt(); +void hal_stop() { + stop_rt(); + stop_frt(); } COMMAND("stop", hal_stop, "stop rt system"); -void hal_init(float rt_period, float frt_period){ - hal.rt_state = RT_STOP; - hal.frt_state = RT_STOP; - - for(int i = 0; i < HAL_MAX_COMPS; i++){ - hal.rt_comps[i] = 0; - hal.frt_comps[i] = 0; - } - - hal.comp_inst_count = 0; - hal.rt_comp_count = 0; - hal.frt_comp_count = 0; - hal.pin_inst_count = 0; - - for(int i = 0; i < HAL_MAX_CTX; i++){ - hal.ctxs[i] = 0; - } - hal.ctx_count = 0; - - hal.active_rt_func = -1; - hal.active_frt_func = -1; - hal.active_nrt_func = -1; - - hal.rt_ticks = 0.0; - hal.rt_max_ticks = 0.0; - hal.frt_ticks = 0.0; - hal.frt_max_ticks = 0.0; - hal.nrt_ticks = 0.0; - hal.nrt_max_ticks = 0.0; - hal.rt_period = rt_period; - hal.frt_period = frt_period; - hal.nrt_ticks = 0.0; - hal.nrt_max_ticks = 0.0; +void hal_init(float rt_period, float frt_period) { + hal.rt_state = RT_STOP; + hal.frt_state = RT_STOP; + + for(int i = 0; i < HAL_MAX_COMPS; i++) { + hal.rt_comps[i] = 0; + hal.frt_comps[i] = 0; + } + + hal.comp_inst_count = 0; + hal.rt_comp_count = 0; + hal.frt_comp_count = 0; + hal.pin_inst_count = 0; + + for(int i = 0; i < HAL_MAX_CTX; i++) { + hal.ctxs[i] = 0; + } + hal.ctx_count = 0; + + hal.active_rt_func = -1; + hal.active_frt_func = -1; + hal.active_nrt_func = -1; + + hal.rt_ticks = 0.0; + hal.rt_max_ticks = 0.0; + hal.frt_ticks = 0.0; + hal.frt_max_ticks = 0.0; + hal.nrt_ticks = 0.0; + hal.nrt_max_ticks = 0.0; + hal.rt_period = rt_period; + hal.frt_period = frt_period; + hal.nrt_ticks = 0.0; + hal.nrt_max_ticks = 0.0; } -void hal_print_pin(volatile hal_pin_inst_t * p){ - pin_t * pin = pin_by_pin_inst(p); - volatile hal_comp_inst_t * comp = comp_inst_by_pin_inst(p); - - pin_t * pin2; - volatile hal_comp_inst_t * comp2; - pin_t * pin3; - volatile hal_comp_inst_t * comp3; +void hal_print_pin(volatile hal_pin_inst_t *p) { + pin_t *pin = pin_by_pin_inst(p); + volatile hal_comp_inst_t *comp = comp_inst_by_pin_inst(p); - if(p && pin && comp){ - if(p == p->source){//if pin is not linked - printf("%s%lu.%s = %f\n", (char *)comp->comp->name, comp->instance, (char *)pin, p->value); - }else if(p->source == p->source->source){//pin is single linked - pin2 = pin_by_pin_inst(p->source); - comp2 = comp_inst_by_pin_inst(p->source); - printf("%s%lu.%s <= %s%lu.%s = %f\n", (char *)comp->comp->name, comp->instance, (char *)pin, (char *)comp2->comp->name, comp2->instance, (char *)pin2, p->source->value); - } - else{//pin is double linked - pin2 = pin_by_pin_inst(p->source); - comp2 = comp_inst_by_pin_inst(p->source); - pin3 = pin_by_pin_inst(p->source->source); - comp3 = comp_inst_by_pin_inst(p->source->source); - printf("%s%lu.%s <= %s%lu.%s <= %s%lu.%s = %f\n", (char *)comp->comp->name, comp->instance, (char *)pin, (char *)comp2->comp->name, comp2->instance, (char *)pin2, (char *)comp3->comp->name, comp3->instance, (char *)pin3, p->source->source->value); - } - } + pin_t *pin2; + volatile hal_comp_inst_t *comp2; + pin_t *pin3; + volatile hal_comp_inst_t *comp3; + + if(p && pin && comp) { + if(p == p->source) { //if pin is not linked + printf("%s%lu.%s = %f\n", (char *)comp->comp->name, comp->instance, (char *)pin, p->value); + } else if(p->source == p->source->source) { //pin is single linked + pin2 = pin_by_pin_inst(p->source); + comp2 = comp_inst_by_pin_inst(p->source); + printf("%s%lu.%s <= %s%lu.%s = %f\n", (char *)comp->comp->name, comp->instance, (char *)pin, (char *)comp2->comp->name, comp2->instance, (char *)pin2, p->source->value); + } else { //pin is double linked + pin2 = pin_by_pin_inst(p->source); + comp2 = comp_inst_by_pin_inst(p->source); + pin3 = pin_by_pin_inst(p->source->source); + comp3 = comp_inst_by_pin_inst(p->source->source); + printf("%s%lu.%s <= %s%lu.%s <= %s%lu.%s = %f\n", (char *)comp->comp->name, comp->instance, (char *)pin, (char *)comp2->comp->name, comp2->instance, (char *)pin2, (char *)comp3->comp->name, comp3->instance, (char *)pin3, p->source->source->value); + } + } } -char * findline(char * ptr){ - for(int i = 0; i < 64; i++){ - if(ptr[i] == 0){ - return(0); - } - if(ptr[i] == '\n'){ - return(ptr + i + 1); - } - } - return(0); +char *findline(char *ptr) { + for(int i = 0; i < 64; i++) { + if(ptr[i] == 0) { + return (0); + } + if(ptr[i] == '\n') { + return (ptr + i + 1); + } + } + return (0); } -uint32_t hal_parse_(char * cmd); +uint32_t hal_parse_(char *cmd); -uint32_t hal_parse(char * cmd){ - do{ - hal_parse_(cmd); - cmd = findline(cmd); - }while(cmd); - return(0); +uint32_t hal_parse(char *cmd) { + do { + hal_parse_(cmd); + cmd = findline(cmd); + } while(cmd); + return (0); } -uint32_t hal_parse_(char * cmd){ - if(call_cmd(cmd)){ - return(1); - } - - int32_t foo = 0; - - char sinkc[64]; - uint32_t sinki = 0; - char sinkp[64]; - - float value = 0.0; - - char sourcec[64]; - uint32_t sourcei = 0; - char sourcep[64]; - - volatile hal_pin_inst_t * sink; - volatile hal_pin_inst_t * source; - - uint32_t found = 0; - - foo = sscanf(cmd," %[a-zA-Z_]%lu.%[a-zA-Z0-9_] = %f", sinkc, &sinki, sinkp, &value); - switch(foo){ - case 0: +uint32_t hal_parse_(char *cmd) { + if(call_cmd(cmd)) { + return (1); + } + + int32_t foo = 0; + + char sinkc[64]; + uint32_t sinki = 0; + char sinkp[64]; + + float value = 0.0; + + char sourcec[64]; + uint32_t sourcei = 0; + char sourcep[64]; + + volatile hal_pin_inst_t *sink; + volatile hal_pin_inst_t *source; + + uint32_t found = 0; + + foo = sscanf(cmd, " %[a-zA-Z_]%lu.%[a-zA-Z0-9_] = %f", sinkc, &sinki, sinkp, &value); + switch(foo) { + case 0: break; - case 1: // search comps - for(int i = 0; i < hal.comp_inst_count; i++){ - if(!strncmp(hal.comp_insts[i].comp->name, sinkc, strlen(sinkc))){ - printf("%s%lu\n", hal.comp_insts[i].comp->name, hal.comp_insts[i].instance); - found = 1; - } - } - if(!found){ - printf("not found: %s\n", cmd); - } - break; - case 2: // search comps + instance - for(int i = 0; i < hal.comp_inst_count; i++){ - if(hal.comp_insts[i].instance == sinki && !strcmp(hal.comp_insts[i].comp->name, sinkc)){ - for(int j = 0; j < hal.comp_insts[i].comp->pin_count; j++){ - //volatile hal_comp_inst_t * comp = comp_inst_by_pin_inst(hal.comp_insts[i].pin_insts[j].source->source); - //printf("%s%lu.%s <= %s%lu.%s = %f\n", hal.comp_insts[i].comp->name, hal.comp_insts[i].instance, hal.comp_insts[i].pins[j], comp->comp->name, comp->instance, (char *)pin_by_pin_inst(hal.comp_insts[i].pin_insts[j].source->source), hal.comp_insts[i].pin_insts[j].source->source->value); - hal_print_pin(&(hal.comp_insts[i].pin_insts[j])); - found = 1; - } - } - } - if(!found){ - printf("not found: %s\n", cmd); - } - break; - case 3: - foo = sscanf(cmd," %[a-zA-Z_]%lu.%[a-zA-Z0-9_] = %[a-zA-Z_]%lu.%[a-zA-Z0-9_]", sinkc, &sinki, sinkp, sourcec, &sourcei, sourcep); - if(foo == 6){ // link pins - sink = pin_inst_by_name(sinkc, sinki, sinkp); - source = pin_inst_by_name(sourcec, sourcei, sourcep); - if(sink && source){ - sink->source = source; - printf("OK %s%lu.%s <= %s%lu.%s = %f\n", sinkc, sinki, sinkp, sourcec, sourcei, sourcep, source->source->value); + case 1: // search comps + for(int i = 0; i < hal.comp_inst_count; i++) { + if(!strncmp(hal.comp_insts[i].comp->name, sinkc, strlen(sinkc))) { + printf("%s%lu\n", hal.comp_insts[i].comp->name, hal.comp_insts[i].instance); + found = 1; + } + } + if(!found) { + printf("not found: %s\n", cmd); + } + break; + case 2: // search comps + instance + for(int i = 0; i < hal.comp_inst_count; i++) { + if(hal.comp_insts[i].instance == sinki && !strcmp(hal.comp_insts[i].comp->name, sinkc)) { + for(int j = 0; j < hal.comp_insts[i].comp->pin_count; j++) { + //volatile hal_comp_inst_t * comp = comp_inst_by_pin_inst(hal.comp_insts[i].pin_insts[j].source->source); + //printf("%s%lu.%s <= %s%lu.%s = %f\n", hal.comp_insts[i].comp->name, hal.comp_insts[i].instance, hal.comp_insts[i].pins[j], comp->comp->name, comp->instance, (char *)pin_by_pin_inst(hal.comp_insts[i].pin_insts[j].source->source), hal.comp_insts[i].pin_insts[j].source->source->value); + hal_print_pin(&(hal.comp_insts[i].pin_insts[j])); + found = 1; + } + } + } + if(!found) { + printf("not found: %s\n", cmd); + } + break; + case 3: + foo = sscanf(cmd, " %[a-zA-Z_]%lu.%[a-zA-Z0-9_] = %[a-zA-Z_]%lu.%[a-zA-Z0-9_]", sinkc, &sinki, sinkp, sourcec, &sourcei, sourcep); + if(foo == 6) { // link pins + sink = pin_inst_by_name(sinkc, sinki, sinkp); + source = pin_inst_by_name(sourcec, sourcei, sourcep); + if(sink && source) { + sink->source = source; + printf("OK %s%lu.%s <= %s%lu.%s = %f\n", sinkc, sinki, sinkp, sourcec, sourcei, sourcep, source->source->value); + } else if(sink) { + printf("not found: %s%lu.%s\n", sourcec, sourcei, sourcep); + } else { + printf("not found: %s%lu.%s\n", sinkc, sinki, sinkp); + } + } else { // search comps + instance + pin + for(int i = 0; i < hal.comp_inst_count; i++) { + if(hal.comp_insts[i].instance == sinki && !strcmp(hal.comp_insts[i].comp->name, sinkc)) { + for(int j = 0; j < hal.comp_insts[i].comp->pin_count; j++) { + //volatile hal_comp_inst_t * comp = comp_inst_by_pin_inst(hal.comp_insts[i].pin_insts[j].source->source); + if(!strncmp(hal.comp_insts[i].pins[j], sinkp, strlen(sinkp))) { + hal_print_pin(&(hal.comp_insts[i].pin_insts[j])); + //printf("%s%lu.%s <= %s%lu.%s = %f\n", hal.comp_insts[i].comp->name, hal.comp_insts[i].instance, hal.comp_insts[i].pins[j], comp->comp->name, comp->instance, (char *)pin_by_pin_inst(hal.comp_insts[i].pin_insts[j].source->source), hal.comp_insts[i].pin_insts[j].source->source->value); + found = 1; + } } - else if(sink){ - printf("not found: %s%lu.%s\n", sourcec, sourcei, sourcep); - } - else{ - printf("not found: %s%lu.%s\n", sinkc, sinki, sinkp); - } - } - else{ // search comps + instance + pin - for(int i = 0; i < hal.comp_inst_count; i++){ - if(hal.comp_insts[i].instance == sinki && !strcmp(hal.comp_insts[i].comp->name, sinkc)){ - for(int j = 0; j < hal.comp_insts[i].comp->pin_count; j++){ - //volatile hal_comp_inst_t * comp = comp_inst_by_pin_inst(hal.comp_insts[i].pin_insts[j].source->source); - if(!strncmp(hal.comp_insts[i].pins[j], sinkp, strlen(sinkp))){ - hal_print_pin(&(hal.comp_insts[i].pin_insts[j])); - //printf("%s%lu.%s <= %s%lu.%s = %f\n", hal.comp_insts[i].comp->name, hal.comp_insts[i].instance, hal.comp_insts[i].pins[j], comp->comp->name, comp->instance, (char *)pin_by_pin_inst(hal.comp_insts[i].pin_insts[j].source->source), hal.comp_insts[i].pin_insts[j].source->source->value); - found = 1; - } - } - } - } - if(!found){ - printf("not found: %s\n", cmd); - } - } - break; - case 4: // set pin - sink = pin_inst_by_name(sinkc, sinki, sinkp); - if(sink){ - sink->value = value; - sink->source = sink; - printf("OK %s%lu.%s = %f\n", sinkc, sinki, sinkp, value); - } - else{ - printf("not found: %s%lu.%s\n", sinkc, sinki, sinkp); - } - break; - default: - printf("not found: %s\n", cmd); - } - return(0); + } + } + if(!found) { + printf("not found: %s\n", cmd); + } + } + break; + case 4: // set pin + sink = pin_inst_by_name(sinkc, sinki, sinkp); + if(sink) { + sink->value = value; + sink->source = sink; + printf("OK %s%lu.%s = %f\n", sinkc, sinki, sinkp, value); + } else { + printf("not found: %s%lu.%s\n", sinkc, sinki, sinkp); + } + break; + default: + printf("not found: %s\n", cmd); + } + return (0); } -void hal_error(uint32_t error_handler){ - hal.error_info.active_rt_func = hal.active_rt_func; - hal.error_info.active_frt_func = hal.active_frt_func; - hal.error_info.active_nrt_func = hal.active_nrt_func; - hal.error_info.error_handler = error_handler; - hal_stop(); - hal.hal_state = MISC_ERROR; +void hal_error(uint32_t error_handler) { + hal.error_info.active_rt_func = hal.active_rt_func; + hal.error_info.active_frt_func = hal.active_frt_func; + hal.error_info.active_nrt_func = hal.active_nrt_func; + hal.error_info.error_handler = error_handler; + hal_stop(); + hal.hal_state = MISC_ERROR; } -void fault(){ - printf("trigger fault handler\n"); - volatile uint32_t * ptr = (uint32_t*)0x08010000; - ptr[0] = 1; +void fault() { + printf("trigger fault handler\n"); + volatile uint32_t *ptr = (uint32_t *)0x08010000; + ptr[0] = 1; } COMMAND("fault", fault, "trigger fault"); -void about(){ - printf("######## software info ########\n"); - printf( +void about() { + printf("######## software info ########\n"); + printf( "%s v%i.%i.%i %s\n", version_info.product_name, version_info.major, version_info.minor, version_info.patch, - version_info.git_version - ); - printf("Branch %s\n",version_info.git_branch); - printf("Compiled %s %s ",version_info.build_date, version_info.build_time); - printf("by %s on %s\n",version_info.build_user, version_info.build_host); - printf("GCC %s\n",__VERSION__); - printf("newlib %s\n",_NEWLIB_VERSION); - // printf("CMSIS %i.%i\n",__CM4_CMSIS_VERSION_MAIN,__CM4_CMSIS_VERSION_SUB); - // printf("StdPeriph %i.%i.%i\n",__STM32F4XX_STDPERIPH_VERSION_MAIN,__STM32F4XX_STDPERIPH_VERSION_SUB1,__STM32F4XX_STDPERIPH_VERSION_SUB2); - // printf("CPU ID %lx %lx %lx\n",U_ID[0], U_ID[1], U_ID[2]); - printf("size: %lu crc:%lx\n", version_info.image_size, version_info.image_crc); - - // printf("######## Bootloader info ########\n"); - // printf( - // "%s v%i.%i.%i %s\n", - // bt_version_info->product_name, - // bt_version_info->major, - // bt_version_info->minor, - // bt_version_info->patch, - // bt_version_info->git_version - // ); - // printf("Branch %s\n",bt_version_info->git_branch); - // printf("Compiled %s %s ",bt_version_info->build_date, bt_version_info->build_time); - // printf("by %s on %s\n",bt_version_info->build_user, bt_version_info->build_host); - // - // printf("start:%p ,size:%p ,end%p \n",&_binary_obj_hv_hv_bin_start,&_binary_obj_hv_hv_bin_size,&_binary_obj_hv_hv_bin_end); + version_info.git_version); + printf("Branch %s\n", version_info.git_branch); + printf("Compiled %s %s ", version_info.build_date, version_info.build_time); + printf("by %s on %s\n", version_info.build_user, version_info.build_host); + printf("GCC %s\n", __VERSION__); + printf("newlib %s\n", _NEWLIB_VERSION); + // printf("CMSIS %i.%i\n",__CM4_CMSIS_VERSION_MAIN,__CM4_CMSIS_VERSION_SUB); + // printf("StdPeriph %i.%i.%i\n",__STM32F4XX_STDPERIPH_VERSION_MAIN,__STM32F4XX_STDPERIPH_VERSION_SUB1,__STM32F4XX_STDPERIPH_VERSION_SUB2); + // printf("CPU ID %lx %lx %lx\n",U_ID[0], U_ID[1], U_ID[2]); + printf("size: %lu crc:%lx\n", version_info.image_size, version_info.image_crc); + // printf("######## Bootloader info ########\n"); + // printf( + // "%s v%i.%i.%i %s\n", + // bt_version_info->product_name, + // bt_version_info->major, + // bt_version_info->minor, + // bt_version_info->patch, + // bt_version_info->git_version + // ); + // printf("Branch %s\n",bt_version_info->git_branch); + // printf("Compiled %s %s ",bt_version_info->build_date, bt_version_info->build_time); + // printf("by %s on %s\n",bt_version_info->build_user, bt_version_info->build_host); + // + // printf("start:%p ,size:%p ,end%p \n",&_binary_obj_hv_hv_bin_start,&_binary_obj_hv_hv_bin_size,&_binary_obj_hv_hv_bin_end); } COMMAND("about", about, "show system infos"); diff --git a/shared/hal.h b/shared/hal.h index 532f1854..6c4713f4 100644 --- a/shared/hal.h +++ b/shared/hal.h @@ -1,4 +1,4 @@ -#pragma once +#pragma once #include #include @@ -18,9 +18,9 @@ #define HAL_MAX_CTX 16384 #endif -#define HAL_COMP(name) -#define HAL_PIN(name) -#define HAL_PINA(name, index) +#define HAL_COMP(name) +#define HAL_PIN(name) +#define HAL_PINA(name, index) extern uint32_t hal_get_systick_value(); extern uint32_t hal_get_systick_reload(); @@ -32,127 +32,127 @@ typedef char NAME[32]; typedef NAME const pin_t; -typedef struct hal_pin_inst_t{ - volatile float value; - volatile struct hal_pin_inst_t * source; +typedef struct hal_pin_inst_t { + volatile float value; + volatile struct hal_pin_inst_t *source; } hal_pin_inst_t; -typedef const struct{ - NAME name; - void (*nrt)(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr); - void (*rt)(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr); - void (*frt)(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr); - - void (*nrt_init)(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr); - void (*hw_init)(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr); - void (*rt_start)(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr); - void (*frt_start)(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr); - void (*rt_stop)(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr); - void (*frt_stop)(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr); +typedef const struct { + NAME name; + void (*nrt)(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr); + void (*rt)(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr); + void (*frt)(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr); - uint32_t ctx_size; - uint32_t pin_count; + void (*nrt_init)(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr); + void (*hw_init)(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr); + void (*rt_start)(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr); + void (*frt_start)(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr); + void (*rt_stop)(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr); + void (*frt_stop)(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr); + + uint32_t ctx_size; + uint32_t pin_count; } hal_comp_t; -typedef struct hal_comp_inst_t{ - hal_comp_t * comp; - uint32_t instance; - volatile void * ctx; - volatile hal_pin_inst_t * pin_insts; - pin_t * pins; - uint32_t ctx_size; - volatile int32_t rt_ticks; - volatile int32_t frt_ticks; - volatile int32_t nrt_ticks; - - volatile int32_t rt_max_ticks; - volatile int32_t frt_max_ticks; - volatile int32_t nrt_max_ticks; - - volatile enum { - PRE_INIT, - PRE_HW_INIT, - STARTED, - } state; +typedef struct hal_comp_inst_t { + hal_comp_t *comp; + uint32_t instance; + volatile void *ctx; + volatile hal_pin_inst_t *pin_insts; + pin_t *pins; + uint32_t ctx_size; + volatile int32_t rt_ticks; + volatile int32_t frt_ticks; + volatile int32_t nrt_ticks; + + volatile int32_t rt_max_ticks; + volatile int32_t frt_max_ticks; + volatile int32_t nrt_max_ticks; + + volatile enum { + PRE_INIT, + PRE_HW_INIT, + STARTED, + } state; } hal_comp_inst_t; #define PIN(p) (pins->p.source->source->value) #define PINA(p, i) (pins->p[i].source->source->value) -typedef struct{ - volatile uint32_t active_rt_func; - volatile uint32_t active_frt_func; - volatile uint32_t active_nrt_func; - volatile enum { - HardFault, - NMI, - MemManage, - BusFault, - UsageFault, +typedef struct { + volatile uint32_t active_rt_func; + volatile uint32_t active_frt_func; + volatile uint32_t active_nrt_func; + volatile enum { + HardFault, + NMI, + MemManage, + BusFault, + UsageFault, } error_handler; } hal_error_t; -typedef struct{ - volatile enum thread_state_t{ - RT_CALC, - RT_SLEEP, - RT_STOP - } rt_state; +typedef struct { + volatile enum thread_state_t { + RT_CALC, + RT_SLEEP, + RT_STOP + } rt_state; - volatile enum thread_state_t frt_state; + volatile enum thread_state_t frt_state; - volatile enum hal_state_t{ - FRT_TOO_LONG, - RT_TOO_LONG, - MISC_ERROR, - MEM_ERROR, - CONFIG_LOAD_ERROR, - CONFIG_ERROR, - NAN_ERROR, - HAL_OK2 - } hal_state; - - volatile struct hal_comp_inst_t * rt_comps[HAL_MAX_COMPS]; - volatile struct hal_comp_inst_t * frt_comps[HAL_MAX_COMPS]; - - volatile int32_t active_rt_func; - volatile int32_t active_frt_func; - volatile int32_t active_nrt_func; - - struct hal_comp_inst_t comp_insts[HAL_MAX_COMPS]; - struct hal_pin_inst_t pin_insts[HAL_MAX_PINS]; - volatile uint8_t ctxs[HAL_MAX_CTX]; // create runtime ctx print in python based on COMP_ctx_t - uint32_t comp_inst_count; - uint32_t rt_comp_count; - uint32_t frt_comp_count; - uint32_t pin_inst_count; - uint32_t ctx_count; - - volatile uint32_t rt_ticks; - volatile uint32_t frt_ticks; - volatile uint32_t nrt_ticks; - - volatile uint32_t rt_max_ticks; - volatile uint32_t frt_max_ticks; - volatile uint32_t nrt_max_ticks; - - volatile float rt_period; - volatile float frt_period; - - hal_error_t error_info; + volatile enum hal_state_t { + FRT_TOO_LONG, + RT_TOO_LONG, + MISC_ERROR, + MEM_ERROR, + CONFIG_LOAD_ERROR, + CONFIG_ERROR, + NAN_ERROR, + HAL_OK2 + } hal_state; + + volatile struct hal_comp_inst_t *rt_comps[HAL_MAX_COMPS]; + volatile struct hal_comp_inst_t *frt_comps[HAL_MAX_COMPS]; + + volatile int32_t active_rt_func; + volatile int32_t active_frt_func; + volatile int32_t active_nrt_func; + + struct hal_comp_inst_t comp_insts[HAL_MAX_COMPS]; + struct hal_pin_inst_t pin_insts[HAL_MAX_PINS]; + volatile uint8_t ctxs[HAL_MAX_CTX]; // create runtime ctx print in python based on COMP_ctx_t + uint32_t comp_inst_count; + uint32_t rt_comp_count; + uint32_t frt_comp_count; + uint32_t pin_inst_count; + uint32_t ctx_count; + + volatile uint32_t rt_ticks; + volatile uint32_t frt_ticks; + volatile uint32_t nrt_ticks; + + volatile uint32_t rt_max_ticks; + volatile uint32_t frt_max_ticks; + volatile uint32_t nrt_max_ticks; + + volatile float rt_period; + volatile float frt_period; + + hal_error_t error_info; } hal_t; extern hal_t hal; -hal_comp_t * comp_by_name(NAME name); -volatile hal_comp_inst_t * comp_inst_by_name(NAME name, uint32_t instance); +hal_comp_t *comp_by_name(NAME name); +volatile hal_comp_inst_t *comp_inst_by_name(NAME name, uint32_t instance); uint32_t pin_offset_by_comp_name(NAME name); -pin_t * pin_by_name(NAME comp_name, NAME pin_name); -volatile hal_pin_inst_t * pin_inst_by_name(NAME comp_name, uint32_t instance, NAME pin_name); -uint32_t load_comp(hal_comp_t * comp); -pin_t * pin_by_pin_inst(volatile hal_pin_inst_t * p); -volatile hal_comp_inst_t * comp_inst_by_pin_inst(volatile hal_pin_inst_t * p); -void hal_print_pin(volatile hal_pin_inst_t * p); +pin_t *pin_by_name(NAME comp_name, NAME pin_name); +volatile hal_pin_inst_t *pin_inst_by_name(NAME comp_name, uint32_t instance, NAME pin_name); +uint32_t load_comp(hal_comp_t *comp); +pin_t *pin_by_pin_inst(volatile hal_pin_inst_t *p); +volatile hal_comp_inst_t *comp_inst_by_pin_inst(volatile hal_pin_inst_t *p); +void hal_print_pin(volatile hal_pin_inst_t *p); void hal_init(float rt_period, float frt_period); // void hal_init_nrt(); @@ -161,7 +161,7 @@ void hal_stop(); void hal_run_rt(); void hal_run_frt(); void hal_run_nrt(); -uint32_t hal_parse(char * cmd); +uint32_t hal_parse(char *cmd); void hal_error(uint32_t error_handler); #include "hal_tbl.h" diff --git a/shared/hw_math.h b/shared/hw_math.h index 7e9001ee..b0354ae7 100644 --- a/shared/hw_math.h +++ b/shared/hw_math.h @@ -83,4 +83,4 @@ SIG o----| GND */ #define V3(a, pm, pd) ((a) / (pd) * ((pm) + (pd))) -#define R3(a, vcc, pu, pm, pd) (V3(a, pm, pd) / ((vcc) - V3(a, pm, pd)) / (pu) - V3(a, pm, pd) / ((pm) + (pd))) +#define R3(a, vcc, pu, pm, pd) (V3(a, pm, pd) / ((vcc)-V3(a, pm, pd)) / (pu)-V3(a, pm, pd) / ((pm) + (pd))) diff --git a/shared/old/hal_old.c b/shared/old/hal_old.c index 067ff7ba..e9510fb1 100644 --- a/shared/old/hal_old.c +++ b/shared/old/hal_old.c @@ -26,406 +26,407 @@ hal_struct_t hal; //TODO: only works for single positive digit -char* hal_itoa(int i){ +char *hal_itoa(int i) { hal.tmp[0] = (i % 10) + '0'; hal.tmp[1] = '\0'; - return(hal.tmp); + return (hal.tmp); } -void hal_init(){ +void hal_init() { hal.comp_type_count = 0; - for(int i = 0; i < MAX_COMP_TYPES; i++){ + for(int i = 0; i < MAX_COMP_TYPES; i++) { hal.comp_types_counter[i] = 0; } hal.hal_pin_count = 0; - hal.comp_count = 0; + hal.comp_count = 0; hal.nrt_init_func_count = 0; - hal.rt_func_count = 0; - hal.nrt_func_count = 0; - hal.frt_func_count = 0; + hal.rt_func_count = 0; + hal.nrt_func_count = 0; + hal.frt_func_count = 0; hal.link_errors = 0; - hal.pin_errors = 0; - hal.set_errors = 0; - hal.get_errors = 0; + hal.pin_errors = 0; + hal.set_errors = 0; + hal.get_errors = 0; hal.comp_errors = 0; - hal.rt_state = RT_STOP; - hal.frt_state = FRT_STOP; - hal.hal_state = HAL2_OK; - hal.active_rt_func = -1; + hal.rt_state = RT_STOP; + hal.frt_state = FRT_STOP; + hal.hal_state = HAL2_OK; + hal.active_rt_func = -1; hal.active_frt_func = -1; hal.active_nrt_func = -1; - strcpy(hal.error_name,"no error"); + strcpy(hal.error_name, "no error"); } -void hal_comp_init(){ - for(int i = 0; i < hal.nrt_init_func_count; i++){ // run nrt init - hal.nrt_init[i](); - } +void hal_comp_init() { + for(int i = 0; i < hal.nrt_init_func_count; i++) { // run nrt init + hal.nrt_init[i](); + } } -inline void hal_run_nrt(float period){ // TODO NAN trap? - //run all non realtime hal functions - for(hal.active_nrt_func = 0; hal.active_nrt_func < hal.nrt_func_count; hal.active_nrt_func++){ - hal.nrt[hal.active_nrt_func](period); - } - hal.active_nrt_func = -1; +inline void hal_run_nrt(float period) { // TODO NAN trap? + //run all non realtime hal functions + for(hal.active_nrt_func = 0; hal.active_nrt_func < hal.nrt_func_count; hal.active_nrt_func++) { + hal.nrt[hal.active_nrt_func](period); + } + hal.active_nrt_func = -1; } -uint32_t get_fpscr(){ - uint32_t result; - /* Empty asm statement works as a scheduling barrier */ - __asm volatile (""); - __asm volatile ("VMRS %0, fpscr" : "=r" (result) ); - __asm volatile (""); - return(result); -} - -void set_fpscr(uint32_t fpscr){ +uint32_t get_fpscr() { + uint32_t result; /* Empty asm statement works as a scheduling barrier */ - __asm volatile (""); - __asm volatile ("VMSR fpscr, %0" : : "r" (fpscr) : "vfpcc"); - __asm volatile (""); + __asm volatile(""); + __asm volatile("VMRS %0, fpscr" + : "=r"(result)); + __asm volatile(""); + return (result); } -void hal_run_rt(float period){ - //run all realtime hal functions - for(hal.active_rt_func = 0; hal.active_rt_func < hal.rt_func_count; hal.active_rt_func++){ - hal.rt[hal.active_rt_func](period); - uint32_t fpscr = get_fpscr(); - if(fpscr & 3){ // TODO div zero not working - if(fpscr & 1){ - printf("FPU invalid operation (NaN) "); - } - else{ - printf("FPU div by zero "); - } - printf("in func: %s%u.rt\n", hal.hal_comps[find_comp_by_func((uint32_t)hal.rt[hal.active_rt_func])]->name, (unsigned int)hal.hal_comps[find_comp_by_func((uint32_t)hal.rt[hal.active_rt_func])]->instance); - hal_stop(); - return; +void set_fpscr(uint32_t fpscr) { + /* Empty asm statement works as a scheduling barrier */ + __asm volatile(""); + __asm volatile("VMSR fpscr, %0" + : + : "r"(fpscr) + : "vfpcc"); + __asm volatile(""); +} + +void hal_run_rt(float period) { + //run all realtime hal functions + for(hal.active_rt_func = 0; hal.active_rt_func < hal.rt_func_count; hal.active_rt_func++) { + hal.rt[hal.active_rt_func](period); + uint32_t fpscr = get_fpscr(); + if(fpscr & 3) { // TODO div zero not working + if(fpscr & 1) { + printf("FPU invalid operation (NaN) "); + } else { + printf("FPU div by zero "); } - } - hal.active_rt_func = -1; + printf("in func: %s%u.rt\n", hal.hal_comps[find_comp_by_func((uint32_t)hal.rt[hal.active_rt_func])]->name, (unsigned int)hal.hal_comps[find_comp_by_func((uint32_t)hal.rt[hal.active_rt_func])]->instance); + hal_stop(); + return; + } + } + hal.active_rt_func = -1; } -void hal_run_frt(float period){ - //run all fast realtime hal functions - for(hal.active_frt_func = 0; hal.active_frt_func < hal.frt_func_count; hal.active_frt_func++){ - hal.frt[hal.active_frt_func](period); - uint32_t fpscr = get_fpscr(); - if(fpscr & 0x3){ - if(fpscr & 1){ - printf("FPU invalid operation (NaN) "); - } - else{ - printf("FPU div by zero "); - } - printf("in func: %s%u.frt\n", hal.hal_comps[find_comp_by_func((uint32_t)hal.frt[hal.active_frt_func])]->name, (unsigned int)hal.hal_comps[find_comp_by_func((uint32_t)hal.frt[hal.active_frt_func])]->instance); - hal_stop(); - return; +void hal_run_frt(float period) { + //run all fast realtime hal functions + for(hal.active_frt_func = 0; hal.active_frt_func < hal.frt_func_count; hal.active_frt_func++) { + hal.frt[hal.active_frt_func](period); + uint32_t fpscr = get_fpscr(); + if(fpscr & 0x3) { + if(fpscr & 1) { + printf("FPU invalid operation (NaN) "); + } else { + printf("FPU div by zero "); } - } - hal.active_frt_func = -1; + printf("in func: %s%u.frt\n", hal.hal_comps[find_comp_by_func((uint32_t)hal.frt[hal.active_frt_func])]->name, (unsigned int)hal.hal_comps[find_comp_by_func((uint32_t)hal.frt[hal.active_frt_func])]->instance); + hal_stop(); + return; + } + } + hal.active_frt_func = -1; } -int hal_start_rt(){ - if(hal.rt_state == RT_STOP && hal.hal_state == HAL2_OK){ - hal.active_rt_func = -1; - hal.rt_state = RT_SLEEP; - return(1); - } - return(0); +int hal_start_rt() { + if(hal.rt_state == RT_STOP && hal.hal_state == HAL2_OK) { + hal.active_rt_func = -1; + hal.rt_state = RT_SLEEP; + return (1); + } + return (0); } //TODO: crashes when hal is already running? -void hal_start(){ - uint32_t fpscr = get_fpscr(); - fpscr &= (uint32_t)~0x9F; // Clear all exception flags - set_fpscr(fpscr); - - float min = INFINITY; - int min_index = -1; - float rt_prio = 0.0; - float frt_prio = 0.0; +void hal_start() { + uint32_t fpscr = get_fpscr(); + fpscr &= (uint32_t)~0x9F; // Clear all exception flags + set_fpscr(fpscr); - char added[MAX_COMPS]; + float min = INFINITY; + int min_index = -1; + float rt_prio = 0.0; + float frt_prio = 0.0; - for(int i = 0; i < MAX_COMPS; i++){ - added[i] = 0; - } - // add rt func - hal.rt_func_count = 0; - for(int i = 0; i < hal.comp_count; i++){ - min = INFINITY; - min_index = -1; - for(int j = hal.comp_count - 1; j >= 0; j--){ - rt_prio = hal.hal_pins[hal.hal_comps[j]->hal_pin_start_index + 2]->source->source->value; - if(rt_prio <= min && added[j] == 0 && rt_prio >= 0.0 && hal.hal_comps[j]->rt != 0){ - min = rt_prio; - min_index = j; - } + char added[MAX_COMPS]; + + for(int i = 0; i < MAX_COMPS; i++) { + added[i] = 0; + } + // add rt func + hal.rt_func_count = 0; + for(int i = 0; i < hal.comp_count; i++) { + min = INFINITY; + min_index = -1; + for(int j = hal.comp_count - 1; j >= 0; j--) { + rt_prio = hal.hal_pins[hal.hal_comps[j]->hal_pin_start_index + 2]->source->source->value; + if(rt_prio <= min && added[j] == 0 && rt_prio >= 0.0 && hal.hal_comps[j]->rt != 0) { + min = rt_prio; + min_index = j; } - if(min_index >= 0){ - added[min_index] = 1; - hal.rt[hal.rt_func_count++] = hal.hal_comps[min_index]->rt; - } - } + } + if(min_index >= 0) { + added[min_index] = 1; + hal.rt[hal.rt_func_count++] = hal.hal_comps[min_index]->rt; + } + } - for(int i = 0; i < hal.comp_count; i++){ - added[i] = 0; - } - // add frt func - hal.frt_func_count = 0; - for(int i = 0; i < hal.comp_count; i++){ - min = INFINITY; - min_index = -1; - for(int j = hal.comp_count - 1; j >= 0; j--){ - frt_prio = hal.hal_pins[hal.hal_comps[j]->hal_pin_start_index + 3]->source->source->value; - if(frt_prio <= min && added[j] == 0 && frt_prio >= 0.0 && hal.hal_comps[j]->frt != 0){ - min = frt_prio; - min_index = j; - } + for(int i = 0; i < hal.comp_count; i++) { + added[i] = 0; + } + // add frt func + hal.frt_func_count = 0; + for(int i = 0; i < hal.comp_count; i++) { + min = INFINITY; + min_index = -1; + for(int j = hal.comp_count - 1; j >= 0; j--) { + frt_prio = hal.hal_pins[hal.hal_comps[j]->hal_pin_start_index + 3]->source->source->value; + if(frt_prio <= min && added[j] == 0 && frt_prio >= 0.0 && hal.hal_comps[j]->frt != 0) { + min = frt_prio; + min_index = j; } - if(min_index >= 0){ - added[min_index] = 1; - hal.frt[hal.frt_func_count++] = hal.hal_comps[min_index]->frt; + } + if(min_index >= 0) { + added[min_index] = 1; + hal.frt[hal.frt_func_count++] = hal.hal_comps[min_index]->frt; + } + } + // add (de)init func + hal.rt_init_func_count = 0; + hal.rt_deinit_func_count = 0; + for(int i = 0; i < hal.comp_count; i++) { + rt_prio = hal.hal_pins[hal.hal_comps[i]->hal_pin_start_index + 2]->source->source->value; + frt_prio = hal.hal_pins[hal.hal_comps[i]->hal_pin_start_index + 3]->source->source->value; + + if(rt_prio >= 0.0 || frt_prio >= 0.0) { + if(hal.hal_comps[i]->rt_init != 0) { + hal.rt_init[hal.rt_init_func_count++] = hal.hal_comps[i]->rt_init; } - } - // add (de)init func - hal.rt_init_func_count = 0; - hal.rt_deinit_func_count = 0; - for(int i = 0; i < hal.comp_count; i++){ - rt_prio = hal.hal_pins[hal.hal_comps[i]->hal_pin_start_index + 2]->source->source->value; - frt_prio = hal.hal_pins[hal.hal_comps[i]->hal_pin_start_index + 3]->source->source->value; - - if(rt_prio >= 0.0 || frt_prio >= 0.0){ - if(hal.hal_comps[i]->rt_init != 0){ - hal.rt_init[hal.rt_init_func_count++] = hal.hal_comps[i]->rt_init; - } - if(hal.hal_comps[i]->rt_deinit != 0){ - hal.rt_deinit[hal.rt_deinit_func_count++] = hal.hal_comps[i]->rt_deinit; - } + if(hal.hal_comps[i]->rt_deinit != 0) { + hal.rt_deinit[hal.rt_deinit_func_count++] = hal.hal_comps[i]->rt_deinit; } - } - for(int i = 0; i < hal.rt_init_func_count; i++){ - hal.rt_init[i](); - } + } + } + for(int i = 0; i < hal.rt_init_func_count; i++) { + hal.rt_init[i](); + } - hal_start_rt(); + hal_start_rt(); - if(hal.frt_func_count > 0){ - hal_start_frt(); - hal_enable_frt(); - } - hal_enable_rt(); + if(hal.frt_func_count > 0) { + hal_start_frt(); + hal_enable_frt(); + } + hal_enable_rt(); } -void hal_stop(){ - hal_stop_frt(); - hal_stop_rt(); - hal_disable_frt(); - hal_disable_rt(); - hal.hal_state = HAL2_OK; - for(; hal.rt_deinit_func_count > 0; hal.rt_deinit_func_count--){ - hal.rt_deinit[hal.rt_deinit_func_count - 1](); - } +void hal_stop() { + hal_stop_frt(); + hal_stop_rt(); + hal_disable_frt(); + hal_disable_rt(); + hal.hal_state = HAL2_OK; + for(; hal.rt_deinit_func_count > 0; hal.rt_deinit_func_count--) { + hal.rt_deinit[hal.rt_deinit_func_count - 1](); + } } -int hal_start_frt(){ - if(hal.frt_state == FRT_STOP && hal.hal_state == HAL2_OK){ - hal.active_frt_func = -1; - hal.frt_state = FRT_SLEEP; - return(1); - } - return(0); +int hal_start_frt() { + if(hal.frt_state == FRT_STOP && hal.hal_state == HAL2_OK) { + hal.active_frt_func = -1; + hal.frt_state = FRT_SLEEP; + return (1); + } + return (0); } -void hal_stop_rt(){ - hal.active_rt_func = -1; - hal.rt_state = RT_STOP; - hal.active_rt_func = -1; - hal.rt_state = RT_STOP; +void hal_stop_rt() { + hal.active_rt_func = -1; + hal.rt_state = RT_STOP; + hal.active_rt_func = -1; + hal.rt_state = RT_STOP; } -void hal_stop_frt(){ - hal.active_frt_func = -1; - hal.frt_state = FRT_STOP; - hal.active_frt_func = -1; - hal.frt_state = FRT_STOP; +void hal_stop_frt() { + hal.active_frt_func = -1; + hal.frt_state = FRT_STOP; + hal.active_frt_func = -1; + hal.frt_state = FRT_STOP; } //TODO: calculate and reduce name length... -void hal_init_pin(hal_name_t name, hal_pin_t* pin, float value){ +void hal_init_pin(hal_name_t name, hal_pin_t *pin, float value) { strncpy(pin->name, hal.comp_types[hal.comp_type], HAL_NAME_LENGTH); strncat(pin->name, hal_itoa(hal.comp_types_counter[hal.comp_type]), HAL_NAME_LENGTH); strncat(pin->name, ".", HAL_NAME_LENGTH); strncat(pin->name, name, HAL_NAME_LENGTH); - pin->value = value; + pin->value = value; pin->source = pin; hal_register_pin(pin); } -int hal_register_pin(hal_pin_t* pin){ - if(hal.hal_pin_count >= MAX_HAL_PINS){ +int hal_register_pin(hal_pin_t *pin) { + if(hal.hal_pin_count >= MAX_HAL_PINS) { hal.pin_errors++; - return(0); + return (0); } - for(int i = 0; i < hal.hal_pin_count; i++){ - if(!strcmp(hal.hal_pins[i]->name, pin->name)){ + for(int i = 0; i < hal.hal_pin_count; i++) { + if(!strcmp(hal.hal_pins[i]->name, pin->name)) { hal.pin_errors++; - return(0); + return (0); } } hal.hal_pins[hal.hal_pin_count] = pin; hal.hal_pin_count++; - return(1); + return (1); } -int hal_set_pin(hal_name_t name, float value){ - for(int i = 0; i < hal.hal_pin_count; i++){ - if(!strcmp(hal.hal_pins[i]->name, name)){ - hal.hal_pins[i]->value = value; +int hal_set_pin(hal_name_t name, float value) { + for(int i = 0; i < hal.hal_pin_count; i++) { + if(!strcmp(hal.hal_pins[i]->name, name)) { + hal.hal_pins[i]->value = value; hal.hal_pins[i]->source = hal.hal_pins[i]; - return(1); + return (1); } } - strcpy(hal.error_name,name); + strcpy(hal.error_name, name); hal.set_errors++; - return(0); + return (0); } -int hal_is_pin(hal_name_t name){ - for(int i = 0; i < hal.hal_pin_count; i++){ - if(!strcmp(hal.hal_pins[i]->name, name)){ - return(1); +int hal_is_pin(hal_name_t name) { + for(int i = 0; i < hal.hal_pin_count; i++) { + if(!strcmp(hal.hal_pins[i]->name, name)) { + return (1); } } - return(0); + return (0); } -int hal_is_compname(hal_name_t name){ - for(int i = 0; i < hal.hal_pin_count; i++){ - if(!strncmp(hal.hal_pins[i]->name, name, strlen(name))){ - return(1); +int hal_is_compname(hal_name_t name) { + for(int i = 0; i < hal.hal_pin_count; i++) { + if(!strncmp(hal.hal_pins[i]->name, name, strlen(name))) { + return (1); } } - return(0); + return (0); } -float hal_get_pin(hal_name_t name){ - for(int i = 0; i < hal.hal_pin_count; i++){ - if(!strcmp(hal.hal_pins[i]->name, name)){ - return(hal.hal_pins[i]->source->source->value); +float hal_get_pin(hal_name_t name) { + for(int i = 0; i < hal.hal_pin_count; i++) { + if(!strcmp(hal.hal_pins[i]->name, name)) { + return (hal.hal_pins[i]->source->source->value); } } hal.get_errors++; - return(0.0); + return (0.0); } -hal_pin_t hal_map_pin(hal_name_t name){ - for(int i = 0; i < hal.hal_pin_count; i++){ - if(!strcmp(hal.hal_pins[i]->name, name)){ - return(*hal.hal_pins[i]); - } +hal_pin_t hal_map_pin(hal_name_t name) { + for(int i = 0; i < hal.hal_pin_count; i++) { + if(!strcmp(hal.hal_pins[i]->name, name)) { + return (*hal.hal_pins[i]); } - return(*hal.hal_pins[0]); + } + return (*hal.hal_pins[0]); } -void hal_write_pin(hal_pin_t* pin, float value){ - pin->value = value; +void hal_write_pin(hal_pin_t *pin, float value) { + pin->value = value; pin->source = pin; } -float hal_read_pin(hal_pin_t* pin){ - return(pin->source->source->value); +float hal_read_pin(hal_pin_t *pin) { + return (pin->source->source->value); } -hal_pin_t* hal_find_pin(hal_name_t name){ - for(int i = 0; i < hal.hal_pin_count; i++){ - if(!strcmp(hal.hal_pins[i]->name, name)){ - return(hal.hal_pins[i]); +hal_pin_t *hal_find_pin(hal_name_t name) { + for(int i = 0; i < hal.hal_pin_count; i++) { + if(!strcmp(hal.hal_pins[i]->name, name)) { + return (hal.hal_pins[i]); } } - return(0); + return (0); } -int hal_link_pins(hal_name_t source, hal_name_t sink){ - hal_pin_t* d; - hal_pin_t* s; +int hal_link_pins(hal_name_t source, hal_name_t sink) { + hal_pin_t *d; + hal_pin_t *s; d = hal_find_pin(source); s = hal_find_pin(sink); - if(d != 0 && s != 0){ - s->value = s->source->source->value; + if(d != 0 && s != 0) { + s->value = s->source->source->value; s->source = d; - return(1); + return (1); } - strcpy(hal.error_name,source); + strcpy(hal.error_name, source); hal.link_errors++; - return(0); + return (0); } -int hal_set_comp_type(hal_name_t name){ - for(int i = 0; i < hal.comp_type_count; i++){ - if(!strcmp(hal.comp_types[i], name)){ +int hal_set_comp_type(hal_name_t name) { + for(int i = 0; i < hal.comp_type_count; i++) { + if(!strcmp(hal.comp_types[i], name)) { hal.comp_type = i; - return(hal.comp_types_counter[hal.comp_type]++); + return (hal.comp_types_counter[hal.comp_type]++); } } - if(hal.comp_type_count < MAX_COMP_TYPES){ + if(hal.comp_type_count < MAX_COMP_TYPES) { strncpy(hal.comp_types[hal.comp_type_count], name, HAL_NAME_LENGTH); hal.comp_type = hal.comp_type_count++; - return(0); + return (0); } hal.comp_errors++; - return(-1); + return (-1); } -void hal_add_comp(hal_comp_t* comp){ - if(comp != 0 && hal.comp_count < MAX_COMPS){ - hal.hal_comps[hal.comp_count++] = comp; - comp->instance = hal.comp_types_counter[hal.comp_type]; - if(comp->nrt_init != 0){ - hal.nrt_init[hal.nrt_init_func_count++] = comp->nrt_init; - } - if(comp->nrt != 0 && hal.nrt_func_count < MAX_COMPS){ - hal.nrt[hal.nrt_func_count++] = comp->nrt; - } - } - else{ - hal.comp_errors++; - } +void hal_add_comp(hal_comp_t *comp) { + if(comp != 0 && hal.comp_count < MAX_COMPS) { + hal.hal_comps[hal.comp_count++] = comp; + comp->instance = hal.comp_types_counter[hal.comp_type]; + if(comp->nrt_init != 0) { + hal.nrt_init[hal.nrt_init_func_count++] = comp->nrt_init; + } + if(comp->nrt != 0 && hal.nrt_func_count < MAX_COMPS) { + hal.nrt[hal.nrt_func_count++] = comp->nrt; + } + } else { + hal.comp_errors++; + } } -int32_t find_comp_by_func(uint32_t p){ - for(int i = 0; i < hal.comp_count; i++){ - if(p == (uint32_t)hal.hal_comps[i]->rt){ - return(i); - } - if(p == (uint32_t)hal.hal_comps[i]->frt){ - return(i); - } - if(p == (uint32_t)hal.hal_comps[i]->nrt){ - return(i); - } - if(p == (uint32_t)hal.hal_comps[i]->frt){ - return(i); - } - if(p == (uint32_t)hal.hal_comps[i]->rt_init){ - return(i); - } - if(p == (uint32_t)hal.hal_comps[i]->rt_deinit){ - return(i); - } - if(p == (uint32_t)hal.hal_comps[i]->nrt_init){ - return(i); - } - } - return(-1); +int32_t find_comp_by_func(uint32_t p) { + for(int i = 0; i < hal.comp_count; i++) { + if(p == (uint32_t)hal.hal_comps[i]->rt) { + return (i); + } + if(p == (uint32_t)hal.hal_comps[i]->frt) { + return (i); + } + if(p == (uint32_t)hal.hal_comps[i]->nrt) { + return (i); + } + if(p == (uint32_t)hal.hal_comps[i]->frt) { + return (i); + } + if(p == (uint32_t)hal.hal_comps[i]->rt_init) { + return (i); + } + if(p == (uint32_t)hal.hal_comps[i]->rt_deinit) { + return (i); + } + if(p == (uint32_t)hal.hal_comps[i]->nrt_init) { + return (i); + } + } + return (-1); } diff --git a/shared/old/hal_old.h b/shared/old/hal_old.h index dfe0f226..58b2f002 100644 --- a/shared/old/hal_old.h +++ b/shared/old/hal_old.h @@ -30,33 +30,33 @@ typedef char hal_name_t[HAL_NAME_LENGTH]; -typedef struct hal_pin_t{ +typedef struct hal_pin_t { hal_name_t name; volatile float value; - struct hal_pin_t* source; + struct hal_pin_t *source; } hal_pin_t; -typedef struct{ - hal_name_t name; - void (*rt_init)(); - void (*rt_deinit)(); - void (*nrt_init)(); - void (*rt)(float period); - void (*frt)(float period); - void (*nrt)(float period); - int hal_pin_start_index; - int hal_pin_count; - uint32_t instance; +typedef struct { + hal_name_t name; + void (*rt_init)(); + void (*rt_deinit)(); + void (*nrt_init)(); + void (*rt)(float period); + void (*frt)(float period); + void (*nrt)(float period); + int hal_pin_start_index; + int hal_pin_count; + uint32_t instance; } hal_comp_t; -typedef struct{ +typedef struct { hal_name_t comp_types[MAX_COMP_TYPES]; int comp_types_counter[MAX_COMP_TYPES]; int comp_type_count; int comp_type; hal_name_t tmp; - hal_comp_t* hal_comps[MAX_COMPS]; + hal_comp_t *hal_comps[MAX_COMPS]; int comp_count; void (*rt_init[MAX_COMPS])(); @@ -77,16 +77,16 @@ typedef struct{ void (*frt[MAX_COMPS])(float period); int frt_func_count; - hal_pin_t* hal_pins[MAX_HAL_PINS]; + hal_pin_t *hal_pins[MAX_HAL_PINS]; int hal_pin_count; - volatile enum{ + volatile enum { RT_CALC, RT_SLEEP, RT_STOP } rt_state; - volatile enum{ + volatile enum { FRT_CALC, FRT_SLEEP, FRT_STOP @@ -138,9 +138,9 @@ void hal_stop_rt(); void hal_stop_frt(); -void hal_init_pin(hal_name_t name, hal_pin_t* pin, float value); +void hal_init_pin(hal_name_t name, hal_pin_t *pin, float value); -int hal_register_pin(hal_pin_t* pin); +int hal_register_pin(hal_pin_t *pin); int hal_set_comp_type(hal_name_t name); @@ -154,15 +154,15 @@ float hal_get_pin(hal_name_t name); hal_pin_t hal_map_pin(hal_name_t name); -void hal_write_pin(hal_pin_t* pin, float value); +void hal_write_pin(hal_pin_t *pin, float value); -float hal_read_pin(hal_pin_t* pin); +float hal_read_pin(hal_pin_t *pin); -struct hal_pin_t* hal_find_pin(hal_name_t name); +struct hal_pin_t *hal_find_pin(hal_name_t name); int hal_link_pins(hal_name_t source, hal_name_t sink); -void hal_add_comp(hal_comp_t* comp); +void hal_add_comp(hal_comp_t *comp); int32_t find_comp_by_func(uint32_t p); @@ -174,65 +174,65 @@ extern uint32_t hal_get_systick_value(); extern uint32_t hal_get_systick_reload(); extern uint32_t hal_get_systick_freq(); -char* hal_itoa(int i); +char *hal_itoa(int i); -#define HAL_COMP(type) \ -{ \ - static hal_comp_t self; \ - strncpy(self.name, #type, HAL_NAME_LENGTH); \ - self.nrt_init = 0; \ - self.rt_init = 0; \ - self.rt_deinit = 0; \ - self.rt = 0; \ - self.frt = 0; \ - self.nrt = 0; \ - self.hal_pin_start_index = hal.hal_pin_count; \ - self.hal_pin_count = 0; \ - hal_set_comp_type(self.name); \ - HAL_PIN(rt_calc_time) = 0.0; \ - HAL_PIN(frt_calc_time) = 0.0; \ - HAL_PIN(rt_prio) = -1.0; \ - HAL_PIN(frt_prio) = -1.0; +#define HAL_COMP(type) \ + { \ + static hal_comp_t self; \ + strncpy(self.name, #type, HAL_NAME_LENGTH); \ + self.nrt_init = 0; \ + self.rt_init = 0; \ + self.rt_deinit = 0; \ + self.rt = 0; \ + self.frt = 0; \ + self.nrt = 0; \ + self.hal_pin_start_index = hal.hal_pin_count; \ + self.hal_pin_count = 0; \ + hal_set_comp_type(self.name); \ + HAL_PIN(rt_calc_time) = 0.0; \ + HAL_PIN(frt_calc_time) = 0.0; \ + HAL_PIN(rt_prio) = -1.0; \ + HAL_PIN(frt_prio) = -1.0; -#define HAL_PIN(name) \ - static hal_pin_t name##_hal_pin; \ - hal_init_pin(#name, &name##_hal_pin, 0.0); \ +#define HAL_PIN(name) \ + static hal_pin_t name##_hal_pin; \ + hal_init_pin(#name, &name##_hal_pin, 0.0); \ (name##_hal_pin.value) //TODO: clamp loads of stuff, like count... check length... //TODO: more than 10 pins requires fixing hal_itoa -#define HAL_PIN_ARRAY(name,count) \ - STATIC_ASSERT(count <= 10); \ - static hal_pin_t name##_hal_pin[count]; \ - for(uint32_t i = 0; i < count; i++){ \ - char pinname[HAL_NAME_LENGTH]; \ - strncpy(pinname,#name,HAL_NAME_LENGTH); \ - strncat(pinname,hal_itoa(i),HAL_NAME_LENGTH); \ +#define HAL_PIN_ARRAY(name, count) \ + STATIC_ASSERT(count <= 10); \ + static hal_pin_t name##_hal_pin[count]; \ + for(uint32_t i = 0; i < count; i++) { \ + char pinname[HAL_NAME_LENGTH]; \ + strncpy(pinname, #name, HAL_NAME_LENGTH); \ + strncat(pinname, hal_itoa(i), HAL_NAME_LENGTH); \ hal_init_pin(pinname, &name##_hal_pin[i], 0.0); \ } -#define GLOBAL_HAL_PIN(name) \ +#define GLOBAL_HAL_PIN(name) \ volatile hal_pin_t name##_hal_pin; #define MEM(var) static var -#define PIN(name) \ +#define PIN(name) \ (name##_hal_pin.source->source->value) -#define PIN_ARRAY(name,index) \ +#define PIN_ARRAY(name, index) \ (name##_hal_pin[index].source->source->value) -#define INIT(func) \ - self.nrt_init = ({ void function(){func} function;}); +#define INIT(func) \ + self.nrt_init = ({ void function(){func} function; }); -#define RT_INIT(func) \ - self.rt_init = ({ void function(){func} function;}); +#define RT_INIT(func) \ + self.rt_init = ({ void function(){func} function; }); -#define RT_DEINIT(func) \ - self.rt_deinit = ({ void function(){func} function;}); +#define RT_DEINIT(func) \ + self.rt_deinit = ({ void function(){func} function; }); -#define RT(func) \ - self.rt = ({ void function(float period){ \ +#define RT(func) \ + self.rt = ({ void function(float period){ \ uint32_t __start_time__ = hal_get_systick_value(); \ func \ uint32_t __end_time__ = hal_get_systick_value(); \ @@ -240,10 +240,10 @@ char* hal_itoa(int i); __start_time__ += hal_get_systick_reload(); \ } \ PIN(rt_calc_time) = ((float)(__start_time__ - __end_time__)) / hal_get_systick_freq(); \ - } function;}); + } function; }); -#define FRT(func) \ - self.frt = ({ void function(float period){ \ +#define FRT(func) \ + self.frt = ({ void function(float period){ \ uint32_t __start_time__ = hal_get_systick_value(); \ func \ uint32_t __end_time__ = hal_get_systick_value(); \ @@ -251,66 +251,66 @@ char* hal_itoa(int i); __start_time__ += hal_get_systick_reload(); \ } \ PIN(frt_calc_time) = ((float)(__start_time__ - __end_time__)) / hal_get_systick_freq(); \ - } function;}); + } function; }); -#define NRT(func) \ - self.nrt = ({ void function(float period){func} function;}); +#define NRT(func) \ + self.nrt = ({ void function(float period){func} function; }); -#define HT(ht_code) \ -{ \ -inline void ht_function(){ \ -static float ht_time_count; \ -(void) ht_time_count; \ -static int jump_label_pointer = -__COUNTER__ - 2; \ -static int jump_label_pointer_old = 0; \ -(void) jump_label_pointer_old; \ -switch(jump_label_pointer){ \ -case -__COUNTER__ - 1:; \ - ht_code; \ -default: \ -goto jump_label_ht_end; \ -jump_label_ht_end: \ - break; \ -}\ -}\ -ht_function(); \ -} +#define HT(ht_code) \ + { \ + inline void ht_function() { \ + static float ht_time_count; \ + (void)ht_time_count; \ + static int jump_label_pointer = -__COUNTER__ - 2; \ + static int jump_label_pointer_old = 0; \ + (void)jump_label_pointer_old; \ + switch(jump_label_pointer) { \ + case -__COUNTER__ - 1:; \ + ht_code; \ + default: \ + goto jump_label_ht_end; \ + jump_label_ht_end: \ + break; \ + } \ + } \ + ht_function(); \ + } -#define GOTO(ht_label) \ -jump_label_pointer = (ht_label); \ -break; +#define GOTO(ht_label) \ + jump_label_pointer = (ht_label); \ + break; #define NEXT(ht_label) \ -jump_label_pointer = (ht_label); + jump_label_pointer = (ht_label); #define STATE(ht_state) \ -break; \ -case (ht_state): + break; \ + case(ht_state): -#define SLEEP(time) \ -ht_time_count = 0.0; \ -jump_label_pointer_old = jump_label_pointer; \ -case -__COUNTER__ - 2:; jump_label_pointer = -__COUNTER__ - 1; \ -if(ht_time_count < (time)){ \ - ht_time_count += period; \ - goto jump_label_ht_end; \ -} \ -jump_label_pointer = jump_label_pointer_old; +#define SLEEP(time) \ + ht_time_count = 0.0; \ + jump_label_pointer_old = jump_label_pointer; \ + case -__COUNTER__ - 2:; \ + jump_label_pointer = -__COUNTER__ - 1; \ + if(ht_time_count < (time)) { \ + ht_time_count += period; \ + goto jump_label_ht_end; \ + } \ + jump_label_pointer = jump_label_pointer_old; - //TODO: rename HAL_ENDCOMP -#define ENDCOMP \ +//TODO: rename HAL_ENDCOMP +#define ENDCOMP \ self.hal_pin_count = hal.hal_pin_count - self.hal_pin_start_index; \ - hal_add_comp(&self); \ -} + hal_add_comp(&self); \ + } -#define BLINK(N) \ -({ \ - int t = (systime / 300) % (2 * N + 2); \ - if(t < 2){ \ - t = 0; \ - } \ - else{ \ - t = t % 2; \ - } \ - t;\ -}) +#define BLINK(N) \ + ({ \ + int t = (systime / 300) % (2 * N + 2); \ + if(t < 2) { \ + t = 0; \ + } else { \ + t = t % 2; \ + } \ + t; \ + }) diff --git a/shared/old/hal_term.c b/shared/old/hal_term.c index b48313ac..7a18c8ab 100644 --- a/shared/old/hal_term.c +++ b/shared/old/hal_term.c @@ -1,178 +1,178 @@ #include #include "hal_term.h" -void hal_term_print_pin(hal_pin_t* pin){ - if(pin == pin->source){//if pin is not linked - printf("%s = %f\n", pin->name, pin->source->source->value); - }else{//pin is linked - printf("%s <= %s = %f\n", pin->name, pin->source->name, pin->source->source->value); - } +void hal_term_print_pin(hal_pin_t *pin) { + if(pin == pin->source) { //if pin is not linked + printf("%s = %f\n", pin->name, pin->source->source->value); + } else { //pin is linked + printf("%s <= %s = %f\n", pin->name, pin->source->name, pin->source->source->value); + } } -void hal_term_list(){ - for(int i = 0; i < hal.hal_pin_count; i++){ - printf("%s <= %s = %f\n", hal.hal_pins[i]->name, hal.hal_pins[i]->source->name, hal.hal_pins[i]->source->source->value); - Wait(1);//TODO: remove wait... - } +void hal_term_list() { + for(int i = 0; i < hal.hal_pin_count; i++) { + printf("%s <= %s = %f\n", hal.hal_pins[i]->name, hal.hal_pins[i]->source->name, hal.hal_pins[i]->source->source->value); + Wait(1); //TODO: remove wait... + } } -void hal_term_getconf(){ - for(int i = 0; i < hal.hal_pin_count; i++){ - char name[6]; - strncpy(name,hal.hal_pins[i]->name,5); - name[5] = '\0'; - if(!strcmp(name, "conf0")){ - printf("%s = %f\n", hal.hal_pins[i]->name, hal.hal_pins[i]->value); - } - } +void hal_term_getconf() { + for(int i = 0; i < hal.hal_pin_count; i++) { + char name[6]; + strncpy(name, hal.hal_pins[i]->name, 5); + name[5] = '\0'; + if(!strcmp(name, "conf0")) { + printf("%s = %f\n", hal.hal_pins[i]->name, hal.hal_pins[i]->value); + } + } } -void hal_term_print_state(){ - switch(hal.hal_state){ - case HAL2_OK: +void hal_term_print_state() { + switch(hal.hal_state) { + case HAL2_OK: printf("HAL state: HAL2_OK\n"); break; - case RT_TOO_LONG: + case RT_TOO_LONG: printf("HAL state: RT_TOO_LONG\n"); break; - case FRT_TOO_LONG: + case FRT_TOO_LONG: printf("HAL state: FRT_TOO_LONG\n"); break; - case MISC_ERROR: + case MISC_ERROR: printf("HAL state: MISC_ERROR\n"); break; - case MEM_ERROR: + case MEM_ERROR: printf("HAL state: MEM_ERROR\n"); break; - case CONFIG_LOAD_ERROR: + case CONFIG_LOAD_ERROR: printf("HAL state: CONFIG_LOAD_ERROR\n"); break; - case CONFIG_ERROR: + case CONFIG_ERROR: printf("HAL state: CONFIG_ERROR\n"); break; - case NAN_ERROR: + case NAN_ERROR: printf("HAL state: NAN_ERROR\n"); break; - default: + default: printf("HAL state: unkonwn error\n"); - } + } } -void hal_term_print_info(){ - printf("######## hal info ########\n"); - printf("#pins %i\n", hal.hal_pin_count); - printf("#comps %i\n", hal.comp_count); - printf("link errors %lu\n", hal.link_errors); - printf("pin errors %lu\n", hal.pin_errors); - printf("comp errors %lu\n", hal.comp_errors); - printf("set errors %lu\n", hal.set_errors); - printf("get errors %lu\n", hal.get_errors); - printf("foo0.bar: %f\n", hal_get_pin("foo0.bar")); - printf("error_name: %s\n",hal.error_name); - float pe = hal_get_pin("net0.rt_period"); - float ct = hal_get_pin("net0.rt_calc_time"); - if(pe > 0.0){ - printf("rt time: %fus/%fus = %f%% @ %fkHz\n", ct * 1000000.0, pe * 1000000.0, (ct / pe) * 100.0, 1.0 / pe / 1000.0); - //printf("=%f%%\n",(ct/pe)*100); - } - pe = hal_get_pin("net0.frt_period"); - ct = hal_get_pin("net0.frt_calc_time"); - if(pe > 0.0){ - printf("frt time: %fus/%fus = %f%% @ %fkHz\n", ct * 1000000.0, pe * 1000000.0, (ct / pe) * 100.0, 1.0 / pe / 1000.0); - //printf("=%f%%\n",(ct/pe)*100); - } - pe = hal_get_pin("net0.nrt_period"); - ct = hal_get_pin("net0.nrt_calc_time"); - if(pe > 0.0){ - printf("nrt time: %f/%fs", ct, pe); - printf("=%f%%\n",(ct/pe)*100); - } - switch(hal.rt_state){ - case RT_STOP: +void hal_term_print_info() { + printf("######## hal info ########\n"); + printf("#pins %i\n", hal.hal_pin_count); + printf("#comps %i\n", hal.comp_count); + printf("link errors %lu\n", hal.link_errors); + printf("pin errors %lu\n", hal.pin_errors); + printf("comp errors %lu\n", hal.comp_errors); + printf("set errors %lu\n", hal.set_errors); + printf("get errors %lu\n", hal.get_errors); + printf("foo0.bar: %f\n", hal_get_pin("foo0.bar")); + printf("error_name: %s\n", hal.error_name); + float pe = hal_get_pin("net0.rt_period"); + float ct = hal_get_pin("net0.rt_calc_time"); + if(pe > 0.0) { + printf("rt time: %fus/%fus = %f%% @ %fkHz\n", ct * 1000000.0, pe * 1000000.0, (ct / pe) * 100.0, 1.0 / pe / 1000.0); + //printf("=%f%%\n",(ct/pe)*100); + } + pe = hal_get_pin("net0.frt_period"); + ct = hal_get_pin("net0.frt_calc_time"); + if(pe > 0.0) { + printf("frt time: %fus/%fus = %f%% @ %fkHz\n", ct * 1000000.0, pe * 1000000.0, (ct / pe) * 100.0, 1.0 / pe / 1000.0); + //printf("=%f%%\n",(ct/pe)*100); + } + pe = hal_get_pin("net0.nrt_period"); + ct = hal_get_pin("net0.nrt_calc_time"); + if(pe > 0.0) { + printf("nrt time: %f/%fs", ct, pe); + printf("=%f%%\n", (ct / pe) * 100); + } + switch(hal.rt_state) { + case RT_STOP: printf("rt state: STOP\n"); break; - case RT_SLEEP: + case RT_SLEEP: printf("rt state: SLEEP\n"); break; - case RT_CALC: + case RT_CALC: printf("rt state: CALC\n"); break; - } - switch(hal.frt_state){ - case FRT_STOP: + } + switch(hal.frt_state) { + case FRT_STOP: printf("frt state: STOP\n"); break; - case FRT_SLEEP: + case FRT_SLEEP: printf("frt state: SLEEP\n"); break; - case FRT_CALC: + case FRT_CALC: printf("frt state: CALC\n"); break; - } - hal_term_print_state(); - uint32_t p = 0; - int32_t i = -1; - char str[HAL_NAME_LENGTH]; - printf("active rt funcs(%u):\n", hal.rt_func_count); - pe = hal_get_pin("net0.rt_period"); - for(int j = 0; j < hal.rt_func_count; j++){ - p = (uint32_t)hal.rt[j]; - i = find_comp_by_func(p); - if(i >= 0){ - sprintf(str, "%s%lu.rt_calc_time", hal.hal_comps[i]->name, hal.hal_comps[i]->instance); - ct = hal_get_pin(str); - printf("-%s%lu.rt(%f) %fus", hal.hal_comps[i]->name, hal.hal_comps[i]->instance, hal.hal_pins[hal.hal_comps[i]->hal_pin_start_index + 2]->source->source->value, ct * 1000000.0f); - if(ct > 0.0){ - printf(" = %f%%", (ct / pe) * 100.0); - } - printf("\n"); + } + hal_term_print_state(); + uint32_t p = 0; + int32_t i = -1; + char str[HAL_NAME_LENGTH]; + printf("active rt funcs(%u):\n", hal.rt_func_count); + pe = hal_get_pin("net0.rt_period"); + for(int j = 0; j < hal.rt_func_count; j++) { + p = (uint32_t)hal.rt[j]; + i = find_comp_by_func(p); + if(i >= 0) { + sprintf(str, "%s%lu.rt_calc_time", hal.hal_comps[i]->name, hal.hal_comps[i]->instance); + ct = hal_get_pin(str); + printf("-%s%lu.rt(%f) %fus", hal.hal_comps[i]->name, hal.hal_comps[i]->instance, hal.hal_pins[hal.hal_comps[i]->hal_pin_start_index + 2]->source->source->value, ct * 1000000.0f); + if(ct > 0.0) { + printf(" = %f%%", (ct / pe) * 100.0); } - } - printf("\nactive frt funcs(%u):\n", hal.frt_func_count); - pe = hal_get_pin("net0.frt_period"); - for(int j = 0; j < hal.frt_func_count; j++){ - p = (uint32_t)hal.frt[j]; - i = find_comp_by_func(p); - if(i >= 0){ - ct = hal_get_pin(str); - printf("-%s%lu.frt(%f) %fus", hal.hal_comps[i]->name, hal.hal_comps[i]->instance, hal.hal_pins[hal.hal_comps[i]->hal_pin_start_index + 3]->source->source->value, ct * 1000000.0f); - if(ct > 0.0){ - printf(" = %f%%", (ct / pe) * 100.0); - } - printf("\n"); + printf("\n"); + } + } + printf("\nactive frt funcs(%u):\n", hal.frt_func_count); + pe = hal_get_pin("net0.frt_period"); + for(int j = 0; j < hal.frt_func_count; j++) { + p = (uint32_t)hal.frt[j]; + i = find_comp_by_func(p); + if(i >= 0) { + ct = hal_get_pin(str); + printf("-%s%lu.frt(%f) %fus", hal.hal_comps[i]->name, hal.hal_comps[i]->instance, hal.hal_pins[hal.hal_comps[i]->hal_pin_start_index + 3]->source->source->value, ct * 1000000.0f); + if(ct > 0.0) { + printf(" = %f%%", (ct / pe) * 100.0); } - } - printf("\nactive rt_init funcs(%u):\n", hal.rt_init_func_count); - for(int j = 0; j < hal.rt_init_func_count; j++){ - p = (uint32_t)hal.rt_init[j]; - i = find_comp_by_func(p); - if(i >= 0){ - printf("-%s%lu.rt_init\n", hal.hal_comps[i]->name, hal.hal_comps[i]->instance); - } - } - printf("\nactive rt_deinit funcs(%u):\n", hal.rt_deinit_func_count); - for(int j = 0; j < hal.rt_deinit_func_count; j++){ - p = (uint32_t)hal.rt_deinit[j]; - i = find_comp_by_func(p); - if(i >= 0){ - printf("-%s%lu.rt_deinit\n", hal.hal_comps[i]->name, hal.hal_comps[i]->instance); - } - } - printf("\nactive nrt_init funcs(%u):\n", hal.nrt_init_func_count); - for(int j = 0; j < hal.nrt_init_func_count; j++){ - p = (uint32_t)hal.nrt_init[j]; - i = find_comp_by_func(p); - if(i >= 0){ - printf("-%s%lu.nrt_init\n", hal.hal_comps[i]->name, hal.hal_comps[i]->instance); - } - } - printf("\nactive nrt funcs(%u):\n", hal.nrt_func_count); - for(int j = 0; j < hal.nrt_func_count; j++){ - p = (uint32_t)hal.nrt[j]; - i = find_comp_by_func(p); - if(i >= 0){ - printf("-%s%lu.nrt\n", hal.hal_comps[i]->name, hal.hal_comps[i]->instance); - } - } - printf("\n"); + printf("\n"); + } + } + printf("\nactive rt_init funcs(%u):\n", hal.rt_init_func_count); + for(int j = 0; j < hal.rt_init_func_count; j++) { + p = (uint32_t)hal.rt_init[j]; + i = find_comp_by_func(p); + if(i >= 0) { + printf("-%s%lu.rt_init\n", hal.hal_comps[i]->name, hal.hal_comps[i]->instance); + } + } + printf("\nactive rt_deinit funcs(%u):\n", hal.rt_deinit_func_count); + for(int j = 0; j < hal.rt_deinit_func_count; j++) { + p = (uint32_t)hal.rt_deinit[j]; + i = find_comp_by_func(p); + if(i >= 0) { + printf("-%s%lu.rt_deinit\n", hal.hal_comps[i]->name, hal.hal_comps[i]->instance); + } + } + printf("\nactive nrt_init funcs(%u):\n", hal.nrt_init_func_count); + for(int j = 0; j < hal.nrt_init_func_count; j++) { + p = (uint32_t)hal.nrt_init[j]; + i = find_comp_by_func(p); + if(i >= 0) { + printf("-%s%lu.nrt_init\n", hal.hal_comps[i]->name, hal.hal_comps[i]->instance); + } + } + printf("\nactive nrt funcs(%u):\n", hal.nrt_func_count); + for(int j = 0; j < hal.nrt_func_count; j++) { + p = (uint32_t)hal.nrt[j]; + i = find_comp_by_func(p); + if(i >= 0) { + printf("-%s%lu.nrt\n", hal.hal_comps[i]->name, hal.hal_comps[i]->instance); + } + } + printf("\n"); } diff --git a/shared/old/hal_term.h b/shared/old/hal_term.h index 199807c4..e46be9fa 100644 --- a/shared/old/hal_term.h +++ b/shared/old/hal_term.h @@ -3,9 +3,9 @@ #include #include "hal.h" -void hal_term_print_pin(hal_pin_t* pin); +void hal_term_print_pin(hal_pin_t *pin); void hal_term_print_info(); void hal_term_list(); void hal_term_getconf(); void hal_term_print_state(); -extern void Wait(uint32_t ms);//TODO: find a way of removing wait form hal_term +extern void Wait(uint32_t ms); //TODO: find a way of removing wait form hal_term diff --git a/shared/ringbuf.c b/shared/ringbuf.c index 44fdd83f..4785ef12 100644 --- a/shared/ringbuf.c +++ b/shared/ringbuf.c @@ -7,16 +7,15 @@ * \param data pointer to data byte * \return number of bytes read (0 if buffer was empty) */ -int rb_getc(struct ringbuf *rb, char *data) -{ - if (!rb->len) - return 0; +int rb_getc(struct ringbuf *rb, char *data) { + if(!rb->len) + return 0; - *data = rb->buf[rb->pos++]; - if (rb->pos >= rb->bufsize) - rb->pos -= rb->bufsize; - rb->len--; - return 1; + *data = rb->buf[rb->pos++]; + if(rb->pos >= rb->bufsize) + rb->pos -= rb->bufsize; + rb->len--; + return 1; } @@ -27,44 +26,42 @@ int rb_getc(struct ringbuf *rb, char *data) * \param data pointer to data byte * \return number of bytes written (0 if buffer was full) */ -int rb_putc(struct ringbuf *rb, const char data) -{ - if (rb->len >= rb->bufsize) - return 0; +int rb_putc(struct ringbuf *rb, const char data) { + if(rb->len >= rb->bufsize) + return 0; - unsigned i = rb->pos + rb->len; - if (i >= rb->bufsize) - i -= rb->bufsize; + unsigned i = rb->pos + rb->len; + if(i >= rb->bufsize) + i -= rb->bufsize; - rb->buf[i] = data; - rb->len++; + rb->buf[i] = data; + rb->len++; - return 1; + return 1; } /** * Read from a buffer * */ -int rb_read(struct ringbuf *rb, void *data, int len) -{ - if (len > rb->len) - len = rb->len; +int rb_read(struct ringbuf *rb, void *data, int len) { + if(len > rb->len) + len = rb->len; - int len1 = len; - if (rb->pos + len1 >= rb->bufsize) { - int len2 = (rb->pos + len1) - rb->bufsize; - len1 -= len2; - memcpy((char*)data + len1, rb->buf, len2); - } - memcpy(data, rb->buf + rb->pos, len1); + int len1 = len; + if(rb->pos + len1 >= rb->bufsize) { + int len2 = (rb->pos + len1) - rb->bufsize; + len1 -= len2; + memcpy((char *)data + len1, rb->buf, len2); + } + memcpy(data, rb->buf + rb->pos, len1); - rb->len -= len; - rb->pos += len; - if (rb->pos > rb->bufsize) - rb->pos -= rb->bufsize; + rb->len -= len; + rb->pos += len; + if(rb->pos > rb->bufsize) + rb->pos -= rb->bufsize; - return len; + return len; } @@ -76,63 +73,61 @@ int rb_read(struct ringbuf *rb, void *data, int len) * \return number of bytes written (0 if buffer was full) * */ -int rb_write(struct ringbuf *rb, const void *data, int len) -{ - int i = 0; - char* d_ptr = (char*)data; - while(len > 0 && rb_putc(rb, *d_ptr++)){ - len--; - i++; - } - return(i); - // if (len > rb->bufsize - rb->len) - // len = rb->bufsize - rb->len; - // - // int len1 = len; - // if (rb->pos + rb->len + len1 >= rb->bufsize) { - // int len2 = (rb->pos + rb->len + len1) - rb->bufsize; - // len1 -= len2; - // memcpy(rb->buf, (char*)data + len1, len2); - // } - // - // memcpy(rb->buf + rb->pos + rb->len, data, len1); - // - // rb->len += len; - // return len; +int rb_write(struct ringbuf *rb, const void *data, int len) { + int i = 0; + char *d_ptr = (char *)data; + while(len > 0 && rb_putc(rb, *d_ptr++)) { + len--; + i++; + } + return (i); + // if (len > rb->bufsize - rb->len) + // len = rb->bufsize - rb->len; + // + // int len1 = len; + // if (rb->pos + rb->len + len1 >= rb->bufsize) { + // int len2 = (rb->pos + rb->len + len1) - rb->bufsize; + // len1 -= len2; + // memcpy(rb->buf, (char*)data + len1, len2); + // } + // + // memcpy(rb->buf + rb->pos + rb->len, data, len1); + // + // rb->len += len; + // return len; } -int rb_getline(struct ringbuf *rb, char *ptr, int len){ - int ret = 0; - char c; - if (rb->len == 0){ - return 0; - } +int rb_getline(struct ringbuf *rb, char *ptr, int len) { + int ret = 0; + char c; + if(rb->len == 0) { + return 0; + } - while(rb_getc(rb, &c) && ret < len){ - ret++; - *ptr++ = c; - if(c == '\n'){ - *--ptr = '\0'; - return ret; - } - } - rb_undo(rb, ret); - return 0; + while(rb_getc(rb, &c) && ret < len) { + ret++; + *ptr++ = c; + if(c == '\n') { + *--ptr = '\0'; + return ret; + } + } + rb_undo(rb, ret); + return 0; } -int rb_undo(struct ringbuf *rb, int len){ - if(len == 0){ - return(0); - } - if (rb->len + len > rb->bufsize){ - return(0); - } - if(rb->pos < len){ - rb->pos = rb->pos + rb->bufsize - len; - } - else{ - rb->pos = rb->pos - len; - } - rb->len = rb->len + len; - return(len); +int rb_undo(struct ringbuf *rb, int len) { + if(len == 0) { + return (0); + } + if(rb->len + len > rb->bufsize) { + return (0); + } + if(rb->pos < len) { + rb->pos = rb->pos + rb->bufsize - len; + } else { + rb->pos = rb->pos - len; + } + rb->len = rb->len + len; + return (len); } diff --git a/shared/ringbuf.h b/shared/ringbuf.h index a76356ba..b34bb648 100644 --- a/shared/ringbuf.h +++ b/shared/ringbuf.h @@ -7,13 +7,14 @@ * */ struct ringbuf { - char *buf; ///< Pointer to buffer memory - unsigned bufsize; ///< Size of buffer memory - volatile unsigned pos; ///< Current read position - volatile unsigned len; ///< Length of data in buffer + char *buf; ///< Pointer to buffer memory + unsigned bufsize; ///< Size of buffer memory + volatile unsigned pos; ///< Current read position + volatile unsigned len; ///< Length of data in buffer }; -#define RINGBUF(size) { .buf = (char[(size)]) {0}, .bufsize = (size) } +#define RINGBUF(size) \ + { .buf = (char[(size)]){0}, .bufsize = (size) } /** * Read a single byte from a buffer diff --git a/shared/version.h b/shared/version.h index 362a150e..c84ec68e 100644 --- a/shared/version.h +++ b/shared/version.h @@ -2,25 +2,25 @@ #include -struct version_info {//TODO: typedef - // set at link-time by add-version-info.py - // - uint32_t image_crc; - uint32_t image_size; +struct version_info { //TODO: typedef + // set at link-time by add-version-info.py + // + uint32_t image_crc; + uint32_t image_size; - char git_version[32]; - char git_branch[32]; - char build_user[16]; - char build_host[32]; - char build_date[16]; - char build_time[16]; + char git_version[32]; + char git_branch[32]; + char build_user[16]; + char build_host[32]; + char build_date[16]; + char build_time[16]; - // set at compile-time - //TODO: change to uint32_t - char product_name[32]; - int major; - int minor; - int patch; + // set at compile-time + //TODO: change to uint32_t + char product_name[32]; + int major; + int minor; + int patch; }; extern volatile const struct version_info version_info __attribute__((section(".version_info"))); diff --git a/src/comps/adc.c b/src/comps/adc.c index 5288cc72..f9948ad3 100644 --- a/src/comps/adc.c +++ b/src/comps/adc.c @@ -15,156 +15,155 @@ HAL_COMP(adc); -HAL_PIN(sin);//sin output -HAL_PIN(cos);//cos output -HAL_PIN(sin3);//sin output, last quater only -HAL_PIN(cos3);//cos output, last quater only -HAL_PIN(quad);//quadrant of sin/cos +HAL_PIN(sin); //sin output +HAL_PIN(cos); //cos output +HAL_PIN(sin3); //sin output, last quater only +HAL_PIN(cos3); //cos output, last quater only +HAL_PIN(quad); //quadrant of sin/cos -HAL_PIN(res_en);//flip polarity for resolvers +HAL_PIN(res_en); //flip polarity for resolvers HAL_PIN(sin_gain); HAL_PIN(cos_gain); HAL_PIN(sin_offset); HAL_PIN(cos_offset); - -HAL_PIN(send_step);//15.0; + +HAL_PIN(send_step); //15.0; HAL_PINA(offset, 8); HAL_PINA(gain, 8); -struct adc_ctx_t{ - volatile float txbuf[8][PID_WAVES * ADC_ANZ]; - uint32_t txpos; - uint32_t send_counter;//send_step counter - uint32_t send;//send buffer state +struct adc_ctx_t { + volatile float txbuf[8][PID_WAVES * ADC_ANZ]; + uint32_t txpos; + uint32_t send_counter; //send_step counter + uint32_t send; //send buffer state }; -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct adc_ctx_t * ctx = (struct adc_ctx_t *)ctx_ptr; - struct adc_pin_ctx_t * pins = (struct adc_pin_ctx_t *)pin_ptr; - PINA(gain, 0) = 200; - PINA(gain, 1) = 200; - PIN(sin_gain) = 1.0; - PIN(cos_gain) = 1.0; - ctx->txpos = 0; - ctx->send_counter = 0; //send_step counter - ctx->send = 1; //send buffer state +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct adc_ctx_t *ctx = (struct adc_ctx_t *)ctx_ptr; + struct adc_pin_ctx_t *pins = (struct adc_pin_ctx_t *)pin_ptr; + PINA(gain, 0) = 200; + PINA(gain, 1) = 200; + PIN(sin_gain) = 1.0; + PIN(cos_gain) = 1.0; + ctx->txpos = 0; + ctx->send_counter = 0; //send_step counter + ctx->send = 1; //send buffer state } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct adc_ctx_t * ctx = (struct adc_ctx_t *)ctx_ptr; - struct adc_pin_ctx_t * pins = (struct adc_pin_ctx_t *)pin_ptr; - - float si[PID_WAVES]; - float co[PID_WAVES]; - uint32_t sii; - uint32_t coi; - - float s_o = PIN(sin_offset); - float c_o = PIN(cos_offset); - float s_g = PIN(sin_gain); - float c_g = PIN(cos_gain); - float s; - float c; - - volatile uint32_t *ADC_DMA_Buffer; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct adc_ctx_t *ctx = (struct adc_ctx_t *)ctx_ptr; + struct adc_pin_ctx_t *pins = (struct adc_pin_ctx_t *)pin_ptr; - // if(DMA_GetCurrentMemoryTarget(DMA2_Stream0) == 0){ - // ADC_DMA_Buffer = ADC_DMA_Buffer1; - // } - // else{ - ADC_DMA_Buffer = ADC_DMA_Buffer0; - // } - for(int i = 0; i < PID_WAVES; i++){ - sii = 0; - coi = 0; - for(int j = 0; j < ADC_ANZ; j++){ - //ADC dual mode puts both channels in one word, right aligned. - sii += ADC_DMA_Buffer[i * ADC_ANZ + j] & 0x0000ffff; - coi += ADC_DMA_Buffer[i * ADC_ANZ + j] >> 16; - if(ctx->send == 0){ // TODO: move V_DIFF2 to nrt, this is too slow - ctx->txbuf[0][ctx->txpos] = (((i==0 || i==2) && (PIN(res_en) > 0.0))?-1.0:1.0) * V_DIFF2(ADC_DMA_Buffer[i * ADC_ANZ + j] & 0x0000ffff); - ctx->txbuf[1][ctx->txpos] = (((i==0 || i==2) && (PIN(res_en) > 0.0))?-1.0:1.0) * V_DIFF2(ADC_DMA_Buffer[i * ADC_ANZ + j] >> 16); - ctx->txpos++; - } + float si[PID_WAVES]; + float co[PID_WAVES]; + uint32_t sii; + uint32_t coi; + + float s_o = PIN(sin_offset); + float c_o = PIN(cos_offset); + float s_g = PIN(sin_gain); + float c_g = PIN(cos_gain); + float s; + float c; + + volatile uint32_t *ADC_DMA_Buffer; + + // if(DMA_GetCurrentMemoryTarget(DMA2_Stream0) == 0){ + // ADC_DMA_Buffer = ADC_DMA_Buffer1; + // } + // else{ + ADC_DMA_Buffer = ADC_DMA_Buffer0; + // } + for(int i = 0; i < PID_WAVES; i++) { + sii = 0; + coi = 0; + for(int j = 0; j < ADC_ANZ; j++) { + //ADC dual mode puts both channels in one word, right aligned. + sii += ADC_DMA_Buffer[i * ADC_ANZ + j] & 0x0000ffff; + coi += ADC_DMA_Buffer[i * ADC_ANZ + j] >> 16; + if(ctx->send == 0) { // TODO: move V_DIFF2 to nrt, this is too slow + ctx->txbuf[0][ctx->txpos] = (((i == 0 || i == 2) && (PIN(res_en) > 0.0)) ? -1.0 : 1.0) * V_DIFF2(ADC_DMA_Buffer[i * ADC_ANZ + j] & 0x0000ffff); + ctx->txbuf[1][ctx->txpos] = (((i == 0 || i == 2) && (PIN(res_en) > 0.0)) ? -1.0 : 1.0) * V_DIFF2(ADC_DMA_Buffer[i * ADC_ANZ + j] >> 16); + ctx->txpos++; } - si[i] = s_g * V_DIFF(sii) + s_o; - co[i] = c_g * V_DIFF(coi) + c_o; - } - if(ctx->send == 0){ - ctx->send = 1; - ctx->txpos = 0; - } + } + si[i] = s_g * V_DIFF(sii) + s_o; + co[i] = c_g * V_DIFF(coi) + c_o; + } + if(ctx->send == 0) { + ctx->send = 1; + ctx->txpos = 0; + } - PIN(sin3) = si[3]; - PIN(cos3) = co[3]; + PIN(sin3) = si[3]; + PIN(cos3) = co[3]; - if(PIN(res_en) > 0.0){ - s = (si[3] - si[2] + si[1] - si[0]) / 4.0; - c = (co[3] - co[2] + co[1] - co[0]) / 4.0; - } - else{ - s = (si[3] + si[2] + si[1] + si[0]) / 4.0; - c = (co[3] + co[2] + co[1] + co[0]) / 4.0; - } - - if(si[3]>=0){ - if(co[3]>0) - PIN(quad) = 1; - else - PIN(quad) = 2; - }else{ - if(co[3]>0) - PIN(quad) = 4; - else - PIN(quad) = 3; - } - PIN(sin) = s; - PIN(cos) = c; + if(PIN(res_en) > 0.0) { + s = (si[3] - si[2] + si[1] - si[0]) / 4.0; + c = (co[3] - co[2] + co[1] - co[0]) / 4.0; + } else { + s = (si[3] + si[2] + si[1] + si[0]) / 4.0; + c = (co[3] + co[2] + co[1] + co[0]) / 4.0; + } + + if(si[3] >= 0) { + if(co[3] > 0) + PIN(quad) = 1; + else + PIN(quad) = 2; + } else { + if(co[3] > 0) + PIN(quad) = 4; + else + PIN(quad) = 3; + } + PIN(sin) = s; + PIN(cos) = c; } -static void nrt_func(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct adc_ctx_t * ctx = (struct adc_ctx_t *)ctx_ptr; - struct adc_pin_ctx_t * pins = (struct adc_pin_ctx_t *)pin_ptr; - if(ctx->send_counter++ >= PIN(send_step) - 1 && PIN(send_step) > 0){ +static void nrt_func(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct adc_ctx_t *ctx = (struct adc_ctx_t *)ctx_ptr; + struct adc_pin_ctx_t *pins = (struct adc_pin_ctx_t *)pin_ptr; + if(ctx->send_counter++ >= PIN(send_step) - 1 && PIN(send_step) > 0) { int tmp = 0; uint8_t buf[TERM_NUM_WAVES + 3]; buf[0] = 255; - for(int k = 0; k < PID_WAVES*ADC_ANZ;k++){ - for(int i = 0; i < TERM_NUM_WAVES; i++){ - tmp = (ctx->txbuf[i][k] + PINA(offset,i)) * PINA(gain,i) + 128; - buf[i+1] = CLAMP(tmp,1,254); + for(int k = 0; k < PID_WAVES * ADC_ANZ; k++) { + for(int i = 0; i < TERM_NUM_WAVES; i++) { + tmp = (ctx->txbuf[i][k] + PINA(offset, i)) * PINA(gain, i) + 128; + buf[i + 1] = CLAMP(tmp, 1, 254); } buf[8 + 1] = 0; - - if (USB_CDC_is_connected()) { + + if(USB_CDC_is_connected()) { USB_VCP_send_string(buf); } } - - buf[0] = 0xfe;//trigger servoterm + + buf[0] = 0xfe; //trigger servoterm buf[1] = 0x00; - if (USB_CDC_is_connected()) { + if(USB_CDC_is_connected()) { USB_VCP_send_string(buf); } ctx->send_counter = 0; - ctx->send = 0; + ctx->send = 0; } } hal_comp_t adc_comp_struct = { - .name = "adc", - .nrt = nrt_func, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct adc_ctx_t), - .pin_count = sizeof(struct adc_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "adc", + .nrt = nrt_func, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct adc_ctx_t), + .pin_count = sizeof(struct adc_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/src/comps/conf.c b/src/comps/conf.c index fceff59b..39f2a070 100644 --- a/src/comps/conf.c +++ b/src/comps/conf.c @@ -1,5 +1,5 @@ #include -#include "main.h" // for Wait +#include "main.h" // for Wait #include "commands.h" #include "hal.h" @@ -10,7 +10,7 @@ HAL_PIN(l); HAL_PIN(j); HAL_PIN(psi); HAL_PIN(polecount); -HAL_PIN(mot_type);//ac sync,async/dc,2phase +HAL_PIN(mot_type); //ac sync,async/dc,2phase HAL_PIN(out_rev); HAL_PIN(high_mot_temp); HAL_PIN(max_mot_temp); @@ -38,7 +38,7 @@ HAL_PIN(com_fb_offset); HAL_PIN(com_fb_rev); HAL_PIN(com_fb_res); -HAL_PIN(autophase);//constant,cauto,hfi +HAL_PIN(autophase); //constant,cauto,hfi HAL_PIN(cmd_rev); HAL_PIN(cmd_res); @@ -67,15 +67,15 @@ HAL_PIN(cur_ind); HAL_PIN(max_sat); hal_comp_t conf_comp_struct = { - .name = "conf", - .nrt = 0, - .rt = 0, - .frt = 0, - .nrt_init = 0, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = 0, - .pin_count = sizeof(struct conf_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "conf", + .nrt = 0, + .rt = 0, + .frt = 0, + .nrt_init = 0, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct conf_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/src/comps/enc_cmd.c b/src/comps/enc_cmd.c index 14c8adff..23f217d1 100644 --- a/src/comps/enc_cmd.c +++ b/src/comps/enc_cmd.c @@ -13,180 +13,177 @@ HAL_PIN(pos); HAL_PIN(a); HAL_PIN(b); HAL_PIN(fault); -HAL_PIN(mode); // 0 = quad, 1 = step/dir, 2 = dir/step, 3 = up/down -HAL_PIN(remap); // 0 = cmd, 1 = fb0, 2 = fb1 +HAL_PIN(mode); // 0 = quad, 1 = step/dir, 2 = dir/step, 3 = up/down +HAL_PIN(remap); // 0 = cmd, 1 = fb0, 2 = fb1 -struct enc_cmd_ctx_t{ - int e_res; - uint32_t a_pin, b_pin, c_pin, c_en_pin, a_pin_source, b_pin_source, tim_af, tim_rcc; - GPIO_TypeDef * a_port, * b_port, * c_port, * c_en_port; - TIM_TypeDef * tim; +struct enc_cmd_ctx_t { + int e_res; + uint32_t a_pin, b_pin, c_pin, c_en_pin, a_pin_source, b_pin_source, tim_af, tim_rcc; + GPIO_TypeDef *a_port, *b_port, *c_port, *c_en_port; + TIM_TypeDef *tim; }; -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct enc_cmd_ctx_t * ctx = (struct enc_cmd_ctx_t *)ctx_ptr; - struct enc_cmd_pin_ctx_t * pins = (struct enc_cmd_pin_ctx_t *)pin_ptr; - ctx->e_res = 0; - PIN(res) = 4096.0; +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct enc_cmd_ctx_t *ctx = (struct enc_cmd_ctx_t *)ctx_ptr; + struct enc_cmd_pin_ctx_t *pins = (struct enc_cmd_pin_ctx_t *)pin_ptr; + ctx->e_res = 0; + PIN(res) = 4096.0; } -static void hw_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct enc_cmd_ctx_t * ctx = (struct enc_cmd_ctx_t *)ctx_ptr; - struct enc_cmd_pin_ctx_t * pins = (struct enc_cmd_pin_ctx_t *)pin_ptr; - - ctx->e_res = (int)PIN(res); - if(ctx->e_res < 1){ - ctx->e_res = 1; - } - - //RCC_AHB1PeriphClockCmd(FB1_|ENC0_B_IO_RCC, ENABLE); //Enable needed Clocks for IOs - GPIO_InitTypeDef GPIO_InitStruct; - GPIO_StructInit(&GPIO_InitStruct); - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; - GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz; - GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; - GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; +static void hw_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct enc_cmd_ctx_t *ctx = (struct enc_cmd_ctx_t *)ctx_ptr; + struct enc_cmd_pin_ctx_t *pins = (struct enc_cmd_pin_ctx_t *)pin_ptr; - - + ctx->e_res = (int)PIN(res); + if(ctx->e_res < 1) { + ctx->e_res = 1; + } - switch((int)PIN(remap)){ - case 0: - ctx->a_pin = CMD_A_PIN; - ctx->b_pin = CMD_B_PIN; - ctx->c_pin = CMD_C_PIN; - ctx->c_en_pin = CMD_C_EN_PIN; - ctx->a_port = CMD_A_PORT; - ctx->b_port = CMD_B_PORT; - ctx->c_port = CMD_C_PORT; - ctx->c_en_port = CMD_C_EN_PORT; - ctx->a_pin_source = CMD_A_PIN_SOURCE; - ctx->b_pin_source = CMD_B_PIN_SOURCE; - ctx->tim_af = CMD_ENC_TIM_AF; - ctx->tim_rcc = CMD_ENC_TIM_RCC; - ctx->tim = CMD_ENC_TIM; - RCC_APB1PeriphClockCmd(ctx->tim_rcc, ENABLE); + //RCC_AHB1PeriphClockCmd(FB1_|ENC0_B_IO_RCC, ENABLE); //Enable needed Clocks for IOs + GPIO_InitTypeDef GPIO_InitStruct; + GPIO_StructInit(&GPIO_InitStruct); + GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; + GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz; + GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; + GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; + + + switch((int)PIN(remap)) { + case 0: + ctx->a_pin = CMD_A_PIN; + ctx->b_pin = CMD_B_PIN; + ctx->c_pin = CMD_C_PIN; + ctx->c_en_pin = CMD_C_EN_PIN; + ctx->a_port = CMD_A_PORT; + ctx->b_port = CMD_B_PORT; + ctx->c_port = CMD_C_PORT; + ctx->c_en_port = CMD_C_EN_PORT; + ctx->a_pin_source = CMD_A_PIN_SOURCE; + ctx->b_pin_source = CMD_B_PIN_SOURCE; + ctx->tim_af = CMD_ENC_TIM_AF; + ctx->tim_rcc = CMD_ENC_TIM_RCC; + ctx->tim = CMD_ENC_TIM; + RCC_APB1PeriphClockCmd(ctx->tim_rcc, ENABLE); break; - case 1: - ctx->a_pin = FB0_A_PIN; - ctx->b_pin = FB0_B_PIN; - ctx->c_pin = FB0_Z_PIN; - ctx->c_en_pin = FB0_Z_TXEN_PIN; - ctx->a_port = FB0_A_PORT; - ctx->b_port = FB0_B_PORT; - ctx->c_port = FB0_Z_PORT; - ctx->c_en_port = FB0_Z_TXEN_PORT; - ctx->a_pin_source = FB0_A_PIN_SOURCE; - ctx->b_pin_source = FB0_B_PIN_SOURCE; - ctx->tim_af = FB0_ENC_TIM_AF; - ctx->tim_rcc = FB0_ENC_TIM_RCC; - ctx->tim = FB0_ENC_TIM; - RCC_APB1PeriphClockCmd(ctx->tim_rcc, ENABLE); + case 1: + ctx->a_pin = FB0_A_PIN; + ctx->b_pin = FB0_B_PIN; + ctx->c_pin = FB0_Z_PIN; + ctx->c_en_pin = FB0_Z_TXEN_PIN; + ctx->a_port = FB0_A_PORT; + ctx->b_port = FB0_B_PORT; + ctx->c_port = FB0_Z_PORT; + ctx->c_en_port = FB0_Z_TXEN_PORT; + ctx->a_pin_source = FB0_A_PIN_SOURCE; + ctx->b_pin_source = FB0_B_PIN_SOURCE; + ctx->tim_af = FB0_ENC_TIM_AF; + ctx->tim_rcc = FB0_ENC_TIM_RCC; + ctx->tim = FB0_ENC_TIM; + RCC_APB1PeriphClockCmd(ctx->tim_rcc, ENABLE); break; - case 2: - ctx->a_pin = FB1_A_PIN; - ctx->b_pin = FB1_B_PIN; - ctx->c_pin = FB1_Z_PIN; - ctx->c_en_pin = FB1_Z_TXEN_PIN; - ctx->a_port = FB1_A_PORT; - ctx->b_port = FB1_B_PORT; - ctx->c_port = FB1_Z_PORT; - ctx->c_en_port = FB1_Z_TXEN_PORT; - ctx->a_pin_source = FB1_A_PIN_SOURCE; - ctx->b_pin_source = FB1_B_PIN_SOURCE; - ctx->tim_af = FB1_ENC_TIM_AF; - ctx->tim_rcc = FB1_ENC_TIM_RCC; - ctx->tim = FB1_ENC_TIM; - RCC_APB2PeriphClockCmd(ctx->tim_rcc, ENABLE); + case 2: + ctx->a_pin = FB1_A_PIN; + ctx->b_pin = FB1_B_PIN; + ctx->c_pin = FB1_Z_PIN; + ctx->c_en_pin = FB1_Z_TXEN_PIN; + ctx->a_port = FB1_A_PORT; + ctx->b_port = FB1_B_PORT; + ctx->c_port = FB1_Z_PORT; + ctx->c_en_port = FB1_Z_TXEN_PORT; + ctx->a_pin_source = FB1_A_PIN_SOURCE; + ctx->b_pin_source = FB1_B_PIN_SOURCE; + ctx->tim_af = FB1_ENC_TIM_AF; + ctx->tim_rcc = FB1_ENC_TIM_RCC; + ctx->tim = FB1_ENC_TIM; + RCC_APB2PeriphClockCmd(ctx->tim_rcc, ENABLE); break; - default: + default: return; - } - GPIO_InitStruct.GPIO_Pin = ctx->a_pin; - GPIO_Init(ctx->a_port, &GPIO_InitStruct); + } + GPIO_InitStruct.GPIO_Pin = ctx->a_pin; + GPIO_Init(ctx->a_port, &GPIO_InitStruct); - GPIO_InitStruct.GPIO_Pin = ctx->b_pin; - GPIO_Init(ctx->b_port, &GPIO_InitStruct); - - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; - GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; - GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz; - GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; - GPIO_InitStruct.GPIO_Pin = ctx->c_pin; - GPIO_Init(ctx->c_port, &GPIO_InitStruct); - GPIO_InitStruct.GPIO_Pin = ctx->c_en_pin; - GPIO_Init(ctx->c_en_port, &GPIO_InitStruct); - - GPIO_SetBits(ctx->c_en_port, ctx->c_en_pin); - - //Bind pins to Timer - GPIO_PinAFConfig(ctx->a_port, ctx->a_pin_source, ctx->tim_af); - GPIO_PinAFConfig(ctx->b_port, ctx->b_pin_source, ctx->tim_af); + GPIO_InitStruct.GPIO_Pin = ctx->b_pin; + GPIO_Init(ctx->b_port, &GPIO_InitStruct); - TIM_SetAutoreload(ctx->tim, ctx->e_res * 2 - 1); - // quad - TIM_Cmd(ctx->tim, DISABLE); - TIM_EncoderInterfaceConfig(ctx->tim, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); - TIM_ICInitTypeDef TIM_ICInitStruct; - TIM_ICInitStruct.TIM_Channel = TIM_Channel_1; - TIM_ICInitStruct.TIM_ICFilter = 0x0f; //Digital filtering @ 1/32 fDTS - TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_BothEdge; //Just trigger at the rising edge, because its the clock - TIM_ICInitStruct.TIM_ICPrescaler = 1; //no prescaler, capture is done each time an edge is detected on the capture input - TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_IndirectTI; //IC1 mapped to TI1 - TIM_ICInit(ctx->tim, &TIM_ICInitStruct); + GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; + GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz; + GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_InitStruct.GPIO_Pin = ctx->c_pin; + GPIO_Init(ctx->c_port, &GPIO_InitStruct); + GPIO_InitStruct.GPIO_Pin = ctx->c_en_pin; + GPIO_Init(ctx->c_en_port, &GPIO_InitStruct); - TIM_ICInitStruct.TIM_Channel = TIM_Channel_2; - TIM_ICInitStruct.TIM_ICFilter = 0x0f; //Digital filtering @ 1/32 fDTS - TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising; //Trigger at every edge, because its the direction - TIM_ICInitStruct.TIM_ICPrescaler = 1; //no prescaler, capture is done each time an edge is detected on the capture input - TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI; //IC2 mapped to TI1 - TIM_ICInit(ctx->tim, &TIM_ICInitStruct); - TIM_Cmd(ctx->tim, ENABLE); + GPIO_SetBits(ctx->c_en_port, ctx->c_en_pin); + + //Bind pins to Timer + GPIO_PinAFConfig(ctx->a_port, ctx->a_pin_source, ctx->tim_af); + GPIO_PinAFConfig(ctx->b_port, ctx->b_pin_source, ctx->tim_af); + + TIM_SetAutoreload(ctx->tim, ctx->e_res * 2 - 1); + // quad + TIM_Cmd(ctx->tim, DISABLE); + TIM_EncoderInterfaceConfig(ctx->tim, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); + TIM_ICInitTypeDef TIM_ICInitStruct; + TIM_ICInitStruct.TIM_Channel = TIM_Channel_1; + TIM_ICInitStruct.TIM_ICFilter = 0x0f; //Digital filtering @ 1/32 fDTS + TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_BothEdge; //Just trigger at the rising edge, because its the clock + TIM_ICInitStruct.TIM_ICPrescaler = 1; //no prescaler, capture is done each time an edge is detected on the capture input + TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_IndirectTI; //IC1 mapped to TI1 + TIM_ICInit(ctx->tim, &TIM_ICInitStruct); + + TIM_ICInitStruct.TIM_Channel = TIM_Channel_2; + TIM_ICInitStruct.TIM_ICFilter = 0x0f; //Digital filtering @ 1/32 fDTS + TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising; //Trigger at every edge, because its the direction + TIM_ICInitStruct.TIM_ICPrescaler = 1; //no prescaler, capture is done each time an edge is detected on the capture input + TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI; //IC2 mapped to TI1 + TIM_ICInit(ctx->tim, &TIM_ICInitStruct); + TIM_Cmd(ctx->tim, ENABLE); } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct enc_cmd_ctx_t * ctx = (struct enc_cmd_ctx_t *)ctx_ptr; - struct enc_cmd_pin_ctx_t * pins = (struct enc_cmd_pin_ctx_t *)pin_ptr; - +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct enc_cmd_ctx_t *ctx = (struct enc_cmd_ctx_t *)ctx_ptr; + struct enc_cmd_pin_ctx_t *pins = (struct enc_cmd_pin_ctx_t *)pin_ptr; + int32_t tim = TIM_GetCounter(ctx->tim); PIN(a) = GPIO_ReadInputDataBit(ctx->a_port, ctx->a_pin); PIN(b) = GPIO_ReadInputDataBit(ctx->b_port, ctx->b_pin); - + float p = 0.0; - p = mod(tim * 2.0f * M_PI / (float)ctx->e_res); - + p = mod(tim * 2.0f * M_PI / (float)ctx->e_res); + PIN(pos) = p; - + int r = (int)PIN(res); - if(r < 1){ - r = 1; + if(r < 1) { + r = 1; } - - if(ctx->e_res != r){ + + if(ctx->e_res != r) { ctx->e_res = r; TIM_SetAutoreload(ctx->tim, ctx->e_res * 2 - 1); } - - if(PIN(fault) > 0.0){ - GPIO_SetBits(ctx->c_port, ctx->c_pin); + + if(PIN(fault) > 0.0) { + GPIO_SetBits(ctx->c_port, ctx->c_pin); + } else { + GPIO_ResetBits(ctx->c_port, ctx->c_pin); } - else{ - GPIO_ResetBits(ctx->c_port, ctx->c_pin); - } } const hal_comp_t enc_cmd_comp_struct = { - .name = "enc_cmd", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .hw_init = hw_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct enc_cmd_ctx_t), - .pin_count = sizeof(struct enc_cmd_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "enc_cmd", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .hw_init = hw_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct enc_cmd_ctx_t), + .pin_count = sizeof(struct enc_cmd_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/src/comps/enc_fb.c b/src/comps/enc_fb.c index 997dccca..37273b61 100644 --- a/src/comps/enc_fb.c +++ b/src/comps/enc_fb.c @@ -13,7 +13,7 @@ HAL_PIN(ires); HAL_PIN(pos); HAL_PIN(abspos); HAL_PIN(isabs); -HAL_PIN(index);//TODO: +HAL_PIN(index); //TODO: HAL_PIN(a); HAL_PIN(b); HAL_PIN(ipos); @@ -29,49 +29,49 @@ HAL_PIN(error_max_cnt); HAL_PIN(amp); -struct enc_fb_ctx_t{ - int e_res; - float absoffset; - float error_cnt; +struct enc_fb_ctx_t { + int e_res; + float absoffset; + float error_cnt; }; -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct enc_fb_ctx_t * ctx = (struct enc_fb_ctx_t *)ctx_ptr; - struct enc_fb_pin_ctx_t * pins = (struct enc_fb_pin_ctx_t *)pin_ptr; - ctx->e_res = 0; - ctx->absoffset = 0.0; - PIN(res) = 2048.0; - PIN(ires) = 1024.0; +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct enc_fb_ctx_t *ctx = (struct enc_fb_ctx_t *)ctx_ptr; + struct enc_fb_pin_ctx_t *pins = (struct enc_fb_pin_ctx_t *)pin_ptr; + ctx->e_res = 0; + ctx->absoffset = 0.0; + PIN(res) = 2048.0; + PIN(ires) = 1024.0; } -static void hw_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct enc_fb_ctx_t * ctx = (struct enc_fb_ctx_t *)ctx_ptr; - struct enc_fb_pin_ctx_t * pins = (struct enc_fb_pin_ctx_t *)pin_ptr; +static void hw_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct enc_fb_ctx_t *ctx = (struct enc_fb_ctx_t *)ctx_ptr; + struct enc_fb_pin_ctx_t *pins = (struct enc_fb_pin_ctx_t *)pin_ptr; GPIO_InitTypeDef GPIO_InitStructure; TIM_ICInitTypeDef TIM_ICInitStructure; - TIM_ICInitStructure.TIM_Channel = TIM_Channel_1 | TIM_Channel_2; - TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; + TIM_ICInitStructure.TIM_Channel = TIM_Channel_1 | TIM_Channel_2; + TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; - TIM_ICInitStructure.TIM_ICFilter = 0xF; + TIM_ICInitStructure.TIM_ICFilter = 0xF; TIM_ICInit(FB0_ENC_TIM, &TIM_ICInitStructure); /***************** port 1, quadrature , sin/cos or resolver *********************/ ctx->e_res = (int)PIN(res); - if(ctx->e_res < 1){ - ctx->e_res = 1; + if(ctx->e_res < 1) { + ctx->e_res = 1; } // enable clocks RCC_APB1PeriphClockCmd(FB0_ENC_TIM_RCC, ENABLE); // pin mode: af - GPIO_InitStructure.GPIO_Pin = FB0_A_PIN; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; + GPIO_InitStructure.GPIO_Pin = FB0_A_PIN; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(FB0_A_PORT, &GPIO_InitStructure); - + GPIO_InitStructure.GPIO_Pin = FB0_B_PIN; GPIO_Init(FB0_B_PORT, &GPIO_InitStructure); @@ -89,39 +89,39 @@ static void hw_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ } -static void frt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct enc_fb_ctx_t * ctx = (struct enc_fb_ctx_t *)ctx_ptr; - struct enc_fb_pin_ctx_t * pins = (struct enc_fb_pin_ctx_t *)pin_ptr; - - float p = mod(TIM_GetCounter(FB0_ENC_TIM) * 2.0f * M_PI / (float)ctx->e_res); - PIN(pos) = p; - //TODO: this gets triggered by wire saving abs encoders. add timeout? - if(RISING_EDGE(!GPIO_ReadInputDataBit(FB0_Z_PORT,FB0_Z_PIN))){ - // TODO: fix - ctx->absoffset = -p; - PIN(isabs) = 1.0; - } - PIN(index) = GPIO_ReadInputDataBit(FB0_Z_PORT,FB0_Z_PIN); - PIN(abspos) = mod(p + ctx->absoffset); +static void frt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct enc_fb_ctx_t *ctx = (struct enc_fb_ctx_t *)ctx_ptr; + struct enc_fb_pin_ctx_t *pins = (struct enc_fb_pin_ctx_t *)pin_ptr; + + float p = mod(TIM_GetCounter(FB0_ENC_TIM) * 2.0f * M_PI / (float)ctx->e_res); + PIN(pos) = p; + //TODO: this gets triggered by wire saving abs encoders. add timeout? + if(RISING_EDGE(!GPIO_ReadInputDataBit(FB0_Z_PORT, FB0_Z_PIN))) { + // TODO: fix + ctx->absoffset = -p; + PIN(isabs) = 1.0; + } + PIN(index) = GPIO_ReadInputDataBit(FB0_Z_PORT, FB0_Z_PIN); + PIN(abspos) = mod(p + ctx->absoffset); } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct enc_fb_ctx_t * ctx = (struct enc_fb_ctx_t *)ctx_ptr; - struct enc_fb_pin_ctx_t * pins = (struct enc_fb_pin_ctx_t *)pin_ptr; - - //sample timer value and timer pins together, so we can calculate the quadrant of the timer - int32_t tim = TIM_GetCounter(FB0_ENC_TIM);//TODO: interrupt here? +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct enc_fb_ctx_t *ctx = (struct enc_fb_ctx_t *)ctx_ptr; + struct enc_fb_pin_ctx_t *pins = (struct enc_fb_pin_ctx_t *)pin_ptr; + + //sample timer value and timer pins together, so we can calculate the quadrant of the timer + int32_t tim = TIM_GetCounter(FB0_ENC_TIM); //TODO: interrupt here? uint32_t scgpio = FB0_A_PORT->IDR; float p = 0.0; - int r = (int)PIN(res); - if(r < 1){ - r = 1; + int r = (int)PIN(res); + if(r < 1) { + r = 1; } float ir = PIN(ires); - if(ir < 1){ - ir = 1; + if(ir < 1) { + ir = 1; } float s = PIN(sin); @@ -132,48 +132,45 @@ static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst int q; //calculate quadrant of timer - if((scgpio & FB0_A_PIN)){//TODO: invert for v3... check: plot oquad vs quad - if(scgpio & FB0_B_PIN){ + if((scgpio & FB0_A_PIN)) { //TODO: invert for v3... check: plot oquad vs quad + if(scgpio & FB0_B_PIN) { q = 1; - } - else{ + } else { q = 2; } - }else{ - if(scgpio & FB0_B_PIN){ + } else { + if(scgpio & FB0_B_PIN) { q = 4; - } - else{ + } else { q = 3; } } //TODO: sincos stuff at speed //analog quadrant is calculated by adc component - int qdiff = PIN(quad)-q; + int qdiff = PIN(quad) - q; - switch(qdiff){ - case 1: - case -3: - tim++; - break; - case -1: - case 3: - tim--; - break; - default: - break; + switch(qdiff) { + case 1: + case -3: + tim++; + break; + case -1: + case 3: + tim--; + break; + default: + break; } - if(tim >= ctx->e_res){ - tim = 0; - } - else if(tim < 0){ - tim = ctx->e_res - 1; + if(tim >= ctx->e_res) { + tim = 0; + } else if(tim < 0) { + tim = ctx->e_res - 1; } PIN(qdiff) = qdiff; - PIN(a) = (scgpio & FB0_A_PIN) > 0;//TODO: invert for v3 + PIN(a) = (scgpio & FB0_A_PIN) > 0; //TODO: invert for v3 PIN(b) = (scgpio & FB0_B_PIN) > 0; PIN(oquad) = q; @@ -181,49 +178,46 @@ static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst p = mod(tim * 2.0f * M_PI / (float)ctx->e_res); //TODO: fix EDGE - if(a < 0.15 && !EDGE(tim)){ - ctx->error_cnt += 1.0; - } - else{ + if(a < 0.15 && !EDGE(tim)) { + ctx->error_cnt += 1.0; + } else { ctx->error_cnt -= 0.01; } - - if(ctx->error_cnt > 5.0){ - ctx->error_cnt = 5.0; - } - if(ctx->error_cnt < 0.0){ - ctx->error_cnt = 0.0; - } - - if(ctx->error_cnt < 5.0){ - PIN(error) = 0.0; - PIN(ipos) = mod(p + ((int)(ir * mod(atan2f(s, c) * 4.0 + M_PI) / M_PI)) / ir * M_PI / (float)ctx->e_res); - } - else{ - PIN(error) = 1.0; - } - PIN(error_cnt) = ctx->error_cnt; - PIN(error_max_cnt) = MAX(ctx->error_cnt, PIN(error_max_cnt)); - - if(ctx->e_res != r){ + if(ctx->error_cnt > 5.0) { + ctx->error_cnt = 5.0; + } + if(ctx->error_cnt < 0.0) { + ctx->error_cnt = 0.0; + } + + if(ctx->error_cnt < 5.0) { + PIN(error) = 0.0; + PIN(ipos) = mod(p + ((int)(ir * mod(atan2f(s, c) * 4.0 + M_PI) / M_PI)) / ir * M_PI / (float)ctx->e_res); + } else { + PIN(error) = 1.0; + } + PIN(error_cnt) = ctx->error_cnt; + PIN(error_max_cnt) = MAX(ctx->error_cnt, PIN(error_max_cnt)); + + + if(ctx->e_res != r) { ctx->e_res = r; TIM_SetAutoreload(FB0_ENC_TIM, ctx->e_res - 1); } - } const hal_comp_t enc_fb_comp_struct = { - .name = "enc_fb", - .nrt = 0, - .rt = rt_func, - .frt = frt_func, - .nrt_init = nrt_init, - .hw_init = hw_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct enc_fb_ctx_t), - .pin_count = sizeof(struct enc_fb_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "enc_fb", + .nrt = 0, + .rt = rt_func, + .frt = frt_func, + .nrt_init = nrt_init, + .hw_init = hw_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct enc_fb_ctx_t), + .pin_count = sizeof(struct enc_fb_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/src/comps/encm.c b/src/comps/encm.c index 93b28046..268a4969 100644 --- a/src/comps/encm.c +++ b/src/comps/encm.c @@ -10,15 +10,15 @@ HAL_COMP(encm); HAL_PIN(pos); HAL_PIN(error); -HAL_PIN(dma);//dma transfers left +HAL_PIN(dma); //dma transfers left -struct encm_ctx_t{ +struct encm_ctx_t { uint32_t error; uint8_t rxbuf[15]; }; -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct encm_ctx_t * ctx = (struct encm_ctx_t *)ctx_ptr; +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct encm_ctx_t *ctx = (struct encm_ctx_t *)ctx_ptr; // struct encm_pin_ctx_t * pins = (struct encm_pin_ctx_t *)pin_ptr; GPIO_InitTypeDef GPIO_InitStruct; USART_InitTypeDef USART_InitStruct; @@ -36,11 +36,11 @@ static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr) //USART TX GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6); - GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6; - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; + GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6; + GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; - GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; + GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOC, &GPIO_InitStruct); //USART RX @@ -48,47 +48,47 @@ static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr) // GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11; // GPIO_Init(GPIOB, &GPIO_InitStruct); - USART_InitStruct.USART_BaudRate = 2500000; - USART_InitStruct.USART_WordLength = USART_WordLength_8b; - USART_InitStruct.USART_StopBits = USART_StopBits_1; - USART_InitStruct.USART_Parity = USART_Parity_No; + USART_InitStruct.USART_BaudRate = 2500000; + USART_InitStruct.USART_WordLength = USART_WordLength_8b; + USART_InitStruct.USART_StopBits = USART_StopBits_1; + USART_InitStruct.USART_Parity = USART_Parity_No; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; - USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; + USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART6, &USART_InitStruct); - + /* Enable the USART */ USART_Cmd(USART6, ENABLE); - USART_HalfDuplexCmd(USART6,ENABLE); + USART_HalfDuplexCmd(USART6, ENABLE); // DMA-Disable DMA_Cmd(DMA2_Stream1, DISABLE); DMA_DeInit(DMA2_Stream1); // DMA2-Config - DMA_InitStructure.DMA_Channel = DMA_Channel_5; - DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART6->DR); - DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ctx->rxbuf; - DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; - DMA_InitStructure.DMA_BufferSize = ARRAY_SIZE(ctx->rxbuf); - DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; - DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; + DMA_InitStructure.DMA_Channel = DMA_Channel_5; + DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) & (USART6->DR); + DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ctx->rxbuf; + DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; + DMA_InitStructure.DMA_BufferSize = ARRAY_SIZE(ctx->rxbuf); + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; - DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte; - DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; - DMA_InitStructure.DMA_Priority = DMA_Priority_High; - DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; - DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; - DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; - DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; + DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte; + DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; + DMA_InitStructure.DMA_Priority = DMA_Priority_High; + DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; + DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; + DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; + DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream1, &DMA_InitStructure); USART_DMACmd(USART6, USART_DMAReq_Rx, ENABLE); } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct encm_ctx_t * ctx = (struct encm_ctx_t *)ctx_ptr; - struct encm_pin_ctx_t * pins = (struct encm_pin_ctx_t *)pin_ptr; - PIN(dma) = DMA_GetCurrDataCounter(DMA2_Stream1); +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct encm_ctx_t *ctx = (struct encm_ctx_t *)ctx_ptr; + struct encm_pin_ctx_t *pins = (struct encm_pin_ctx_t *)pin_ptr; + PIN(dma) = DMA_GetCurrDataCounter(DMA2_Stream1); // for(int i = 0; i < ARRAY_SIZE(ctx->rxbuf); i++){ // PIN_ARRAY(d,i) = ctx->rxbuf[i]; // } @@ -103,33 +103,34 @@ static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst //6: 255 turns... how many bits? //7: unknown //8: checksum: xor byte 0-8 = 0 - if((sizeof(ctx->rxbuf) - DMA_GetCurrDataCounter(DMA2_Stream1)) > 7){ + if((sizeof(ctx->rxbuf) - DMA_GetCurrDataCounter(DMA2_Stream1)) > 7) { int offset = -1; - for(int i = 0; i < 3; i++){ - if(ctx->rxbuf[i] == 0x32){ + for(int i = 0; i < 3; i++) { + if(ctx->rxbuf[i] == 0x32) { offset = i; break; } } - if(offset >= 0 && ctx->rxbuf[0 + offset] == 0x32 && ((ctx->rxbuf[0 + offset] ^ ctx->rxbuf[1 + offset] ^ ctx->rxbuf[2 + offset] ^ ctx->rxbuf[3 + offset] ^ ctx->rxbuf[4 + offset] ^ ctx->rxbuf[5 + offset] ^ ctx->rxbuf[6 + offset] ^ ctx->rxbuf[7 + offset] ^ ctx->rxbuf[8 + offset]) == 0)){ - uint32_t tpos = ((ctx->rxbuf[2 + offset] & 0x80) >> 7) + ctx->rxbuf[3 + offset]*2 + ctx->rxbuf[4 + offset]*512;// 17 bit single turn position - PIN(pos) = (tpos * M_PI * 2.0 / 131072.0) - M_PI;// convert to +-PI - PIN(error) = 0.0; - }else{ - ctx->error++;//TODO: overflow... + if(offset >= 0 && ctx->rxbuf[0 + offset] == 0x32 && ((ctx->rxbuf[0 + offset] ^ ctx->rxbuf[1 + offset] ^ ctx->rxbuf[2 + offset] ^ ctx->rxbuf[3 + offset] ^ ctx->rxbuf[4 + offset] ^ ctx->rxbuf[5 + offset] ^ ctx->rxbuf[6 + offset] ^ ctx->rxbuf[7 + offset] ^ ctx->rxbuf[8 + offset]) == 0)) { + uint32_t tpos = ((ctx->rxbuf[2 + offset] & 0x80) >> 7) + ctx->rxbuf[3 + offset] * 2 + ctx->rxbuf[4 + offset] * 512; // 17 bit single turn position + PIN(pos) = (tpos * M_PI * 2.0 / 131072.0) - M_PI; // convert to +-PI + PIN(error) = 0.0; + } else { + ctx->error++; //TODO: overflow... PIN(error) = ctx->error; } - }else{ - ctx->error++;//TODO: overflow... + } else { + ctx->error++; //TODO: overflow... PIN(error) = ctx->error; } //TODO: irq here will cause problems - GPIO_SetBits(GPIOD,GPIO_Pin_15);//tx enable + GPIO_SetBits(GPIOD, GPIO_Pin_15); //tx enable USART_SendData(USART6, 0x32); - while (USART_GetFlagStatus(USART6, USART_FLAG_TC) == RESET); - GPIO_ResetBits(GPIOD,GPIO_Pin_15);//tx disable + while(USART_GetFlagStatus(USART6, USART_FLAG_TC) == RESET) + ; + GPIO_ResetBits(GPIOD, GPIO_Pin_15); //tx disable //start rx dma DMA_Cmd(DMA2_Stream1, DISABLE); DMA_ClearFlag(DMA2_Stream1, DMA_FLAG_TCIF1); @@ -137,15 +138,15 @@ static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst } hal_comp_t encm_comp_struct = { - .name = "encm", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct encm_ctx_t), - .pin_count = sizeof(struct encm_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "encm", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct encm_ctx_t), + .pin_count = sizeof(struct encm_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/src/comps/hv.c b/src/comps/hv.c index 021e85cd..f0e82442 100644 --- a/src/comps/hv.c +++ b/src/comps/hv.c @@ -10,7 +10,7 @@ HAL_COMP(hv); -//process data from LS +//process data from LS HAL_PIN(d_cmd); HAL_PIN(q_cmd); HAL_PIN(pos); @@ -55,79 +55,79 @@ HAL_PIN(crc_error); HAL_PIN(timeout); HAL_PIN(scale); -struct hv_ctx_t{ - volatile packet_to_hv_t packet_to_hv; - volatile packet_from_hv_t packet_from_hv; - f3_config_data_t config; - f3_state_data_t state; - uint16_t addr; - uint16_t timeout; +struct hv_ctx_t { + volatile packet_to_hv_t packet_to_hv; + volatile packet_from_hv_t packet_from_hv; + f3_config_data_t config; + f3_state_data_t state; + uint16_t addr; + uint16_t timeout; }; volatile uint32_t foobar; -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct hv_ctx_t * ctx = (struct hv_ctx_t *)ctx_ptr; - struct hv_pin_ctx_t * pins = (struct hv_pin_ctx_t *)pin_ptr; - - //setup uart to f1. uses DMA to transfer to_hv struct. - GPIO_InitTypeDef GPIO_InitStruct; - USART_InitTypeDef USART_InitStruct; - DMA_InitTypeDef DMA_InitStructure; +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct hv_ctx_t *ctx = (struct hv_ctx_t *)ctx_ptr; + struct hv_pin_ctx_t *pins = (struct hv_pin_ctx_t *)pin_ptr; - UART_DRV_CLOCK_COMMAND(UART_DRV_RCC, ENABLE); + //setup uart to f1. uses DMA to transfer to_hv struct. + GPIO_InitTypeDef GPIO_InitStruct; + USART_InitTypeDef USART_InitStruct; + DMA_InitTypeDef DMA_InitStructure; - //USART TX - GPIO_PinAFConfig(UART_DRV_TX_PORT, UART_DRV_TX_PIN_SOURCE, UART_DRV_TX_AF_SOURCE); - GPIO_InitStruct.GPIO_Pin = UART_DRV_TX_PIN; - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; - GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; - GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP ; - GPIO_Init(UART_DRV_TX_PORT, &GPIO_InitStruct); + UART_DRV_CLOCK_COMMAND(UART_DRV_RCC, ENABLE); - //USART RX - GPIO_PinAFConfig(UART_DRV_RX_PORT, UART_DRV_RX_PIN_SOURCE, UART_DRV_RX_AF_SOURCE); - GPIO_InitStruct.GPIO_Pin = UART_DRV_RX_PIN; - GPIO_Init(UART_DRV_RX_PORT, &GPIO_InitStruct); + //USART TX + GPIO_PinAFConfig(UART_DRV_TX_PORT, UART_DRV_TX_PIN_SOURCE, UART_DRV_TX_AF_SOURCE); + GPIO_InitStruct.GPIO_Pin = UART_DRV_TX_PIN; + GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; + GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; + GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; + GPIO_Init(UART_DRV_TX_PORT, &GPIO_InitStruct); - USART_OverSampling8Cmd(UART_DRV,ENABLE); - USART_InitStruct.USART_BaudRate = DATABAUD; - USART_InitStruct.USART_WordLength = USART_WordLength_8b; - USART_InitStruct.USART_StopBits = USART_StopBits_1; - USART_InitStruct.USART_Parity = USART_Parity_No; - USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; - USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; - USART_Init(UART_DRV, &USART_InitStruct); + //USART RX + GPIO_PinAFConfig(UART_DRV_RX_PORT, UART_DRV_RX_PIN_SOURCE, UART_DRV_RX_AF_SOURCE); + GPIO_InitStruct.GPIO_Pin = UART_DRV_RX_PIN; + GPIO_Init(UART_DRV_RX_PORT, &GPIO_InitStruct); - /* Enable the USART */ - USART_Cmd(UART_DRV, ENABLE); + USART_OverSampling8Cmd(UART_DRV, ENABLE); + USART_InitStruct.USART_BaudRate = DATABAUD; + USART_InitStruct.USART_WordLength = USART_WordLength_8b; + USART_InitStruct.USART_StopBits = USART_StopBits_1; + USART_InitStruct.USART_Parity = USART_Parity_No; + USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; + USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; + USART_Init(UART_DRV, &USART_InitStruct); - // DMA-Disable - DMA_Cmd(UART_DRV_TX_DMA, DISABLE); - DMA_DeInit(UART_DRV_TX_DMA); + /* Enable the USART */ + USART_Cmd(UART_DRV, ENABLE); - // DMA2-Config - DMA_InitStructure.DMA_Channel = UART_DRV_TX_DMA_CHAN; - DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(UART_DRV->DR); - DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&(ctx->packet_to_hv); - DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; - DMA_InitStructure.DMA_BufferSize = sizeof(packet_to_hv_t); - DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; - DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; - DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; - DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte; - DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; - DMA_InitStructure.DMA_Priority = DMA_Priority_High; - DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; - DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; - DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; - DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; - DMA_Init(UART_DRV_TX_DMA, &DMA_InitStructure); + // DMA-Disable + DMA_Cmd(UART_DRV_TX_DMA, DISABLE); + DMA_DeInit(UART_DRV_TX_DMA); - //DMA_Cmd(UART_DRV_TX_DMA, ENABLE); + // DMA2-Config + DMA_InitStructure.DMA_Channel = UART_DRV_TX_DMA_CHAN; + DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) & (UART_DRV->DR); + DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) & (ctx->packet_to_hv); + DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; + DMA_InitStructure.DMA_BufferSize = sizeof(packet_to_hv_t); + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; + DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; + DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte; + DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; + DMA_InitStructure.DMA_Priority = DMA_Priority_High; + DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; + DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; + DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; + DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; + DMA_Init(UART_DRV_TX_DMA, &DMA_InitStructure); - USART_DMACmd(UART_DRV, USART_DMAReq_Tx, ENABLE); + //DMA_Cmd(UART_DRV_TX_DMA, ENABLE); + + USART_DMACmd(UART_DRV, USART_DMAReq_Tx, ENABLE); // DMA-Disable @@ -135,21 +135,21 @@ static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr) DMA_DeInit(UART_DRV_RX_DMA); // DMA2-Config - DMA_InitStructure.DMA_Channel = UART_DRV_RX_DMA_CHAN; - DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(UART_DRV->DR); - DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&(ctx->packet_from_hv); - DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; - DMA_InitStructure.DMA_BufferSize = sizeof(packet_from_hv_t); - DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; - DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; + DMA_InitStructure.DMA_Channel = UART_DRV_RX_DMA_CHAN; + DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) & (UART_DRV->DR); + DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) & (ctx->packet_from_hv); + DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; + DMA_InitStructure.DMA_BufferSize = sizeof(packet_from_hv_t); + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; - DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte; - DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; - DMA_InitStructure.DMA_Priority = DMA_Priority_High; - DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; - DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; - DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; - DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; + DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte; + DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; + DMA_InitStructure.DMA_Priority = DMA_Priority_High; + DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; + DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; + DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; + DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(UART_DRV_RX_DMA, &DMA_InitStructure); @@ -157,108 +157,107 @@ static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr) DMA_Cmd(UART_DRV_RX_DMA, DISABLE); DMA_ClearFlag(UART_DRV_RX_DMA, UART_DRV_RX_DMA_TCIF); DMA_Cmd(UART_DRV_RX_DMA, ENABLE); - + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, ENABLE); ctx->timeout = 0; PIN(timeout) = 0; } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct hv_ctx_t * ctx = (struct hv_ctx_t *)ctx_ptr; - struct hv_pin_ctx_t * pins = (struct hv_pin_ctx_t *)pin_ptr; - - float e = PIN(en); - float pos = PIN(pos); - float vel = PIN(vel); - - ctx->config.pins.r = PIN(r); - ctx->config.pins.l = PIN(l); - ctx->config.pins.psi = PIN(psi); - ctx->config.pins.cur_p = PIN(cur_p); - ctx->config.pins.cur_i = PIN(cur_i); - ctx->config.pins.cur_ff = PIN(cur_ff); - ctx->config.pins.cur_ind = PIN(cur_ind); - ctx->config.pins.max_y = PIN(max_y); - ctx->config.pins.max_cur = PIN(max_cur) * PIN(scale); +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct hv_ctx_t *ctx = (struct hv_ctx_t *)ctx_ptr; + struct hv_pin_ctx_t *pins = (struct hv_pin_ctx_t *)pin_ptr; - uint32_t dma_pos = DMA_GetCurrDataCounter(UART_DRV_RX_DMA); - if(dma_pos == 0){ - CRC_ResetDR(); - uint32_t crc = CRC_CalcBlockCRC((uint32_t *) &(ctx->packet_from_hv), sizeof(packet_from_hv_t) / 4 - 1); - - if(crc == ctx->packet_from_hv.crc){ - PIN(d_fb) = ctx->packet_from_hv.d_fb; - PIN(q_fb) = ctx->packet_from_hv.q_fb; - PIN(dc_volt) = ctx->packet_from_hv.dc_volt; - PIN(pwm_volt) = ctx->packet_from_hv.pwm_volt; - - uint16_t a = ctx->packet_from_hv.addr; - a = CLAMP(a, 0, sizeof(f3_state_data_t) / 4); - ctx->state.data[a] = ctx->packet_from_hv.value; - - PIN(u_fb) = ctx->state.pins.u_fb; - PIN(v_fb) = ctx->state.pins.v_fb; - PIN(w_fb) = ctx->state.pins.w_fb; - PIN(hv_temp) = ctx->state.pins.hv_temp; - PIN(mot_temp) = ctx->state.pins.mot_temp; - PIN(core_temp) = ctx->state.pins.core_temp; - PIN(fault) = ctx->state.pins.fault; - PIN(y) = ctx->state.pins.y; - PIN(com_error) = 0.0;//TODO: link to fault - ctx->timeout = 0; - }else{ - PIN(crc_error)++; - PIN(com_error) = 1.0; - } - } - - if(ctx->timeout > 5){ - PIN(timeout)++; + float e = PIN(en); + float pos = PIN(pos); + float vel = PIN(vel); + + ctx->config.pins.r = PIN(r); + ctx->config.pins.l = PIN(l); + ctx->config.pins.psi = PIN(psi); + ctx->config.pins.cur_p = PIN(cur_p); + ctx->config.pins.cur_i = PIN(cur_i); + ctx->config.pins.cur_ff = PIN(cur_ff); + ctx->config.pins.cur_ind = PIN(cur_ind); + ctx->config.pins.max_y = PIN(max_y); + ctx->config.pins.max_cur = PIN(max_cur) * PIN(scale); + + uint32_t dma_pos = DMA_GetCurrDataCounter(UART_DRV_RX_DMA); + if(dma_pos == 0) { + CRC_ResetDR(); + uint32_t crc = CRC_CalcBlockCRC((uint32_t *)&(ctx->packet_from_hv), sizeof(packet_from_hv_t) / 4 - 1); + + if(crc == ctx->packet_from_hv.crc) { + PIN(d_fb) = ctx->packet_from_hv.d_fb; + PIN(q_fb) = ctx->packet_from_hv.q_fb; + PIN(dc_volt) = ctx->packet_from_hv.dc_volt; + PIN(pwm_volt) = ctx->packet_from_hv.pwm_volt; + + uint16_t a = ctx->packet_from_hv.addr; + a = CLAMP(a, 0, sizeof(f3_state_data_t) / 4); + ctx->state.data[a] = ctx->packet_from_hv.value; + + PIN(u_fb) = ctx->state.pins.u_fb; + PIN(v_fb) = ctx->state.pins.v_fb; + PIN(w_fb) = ctx->state.pins.w_fb; + PIN(hv_temp) = ctx->state.pins.hv_temp; + PIN(mot_temp) = ctx->state.pins.mot_temp; + PIN(core_temp) = ctx->state.pins.core_temp; + PIN(fault) = ctx->state.pins.fault; + PIN(y) = ctx->state.pins.y; + PIN(com_error) = 0.0; //TODO: link to fault + ctx->timeout = 0; + } else { + PIN(crc_error) + ++; PIN(com_error) = 1.0; - } - ctx->timeout++; - - float d_cmd = PIN(d_cmd); - float q_cmd = PIN(q_cmd); - - if(PIN(rev) > 0.0){//TODO: rev DC - q_cmd *= -1.0; - } - - if(e > 0.0){ - ctx->packet_to_hv.d_cmd = d_cmd; - ctx->packet_to_hv.q_cmd = q_cmd; - ctx->packet_to_hv.flags.enable = 1; + } } - else{ - ctx->packet_to_hv.d_cmd = 0.0; - ctx->packet_to_hv.q_cmd = 0.0; + + if(ctx->timeout > 5) { + PIN(timeout) + ++; + PIN(com_error) = 1.0; + } + ctx->timeout++; + + float d_cmd = PIN(d_cmd); + float q_cmd = PIN(q_cmd); + + if(PIN(rev) > 0.0) { //TODO: rev DC + q_cmd *= -1.0; + } + + if(e > 0.0) { + ctx->packet_to_hv.d_cmd = d_cmd; + ctx->packet_to_hv.q_cmd = q_cmd; + ctx->packet_to_hv.flags.enable = 1; + } else { + ctx->packet_to_hv.d_cmd = 0.0; + ctx->packet_to_hv.q_cmd = 0.0; ctx->packet_to_hv.flags.enable = 0; } - ctx->packet_to_hv.flags.cmd_type = PIN(cmd_mode); + ctx->packet_to_hv.flags.cmd_type = PIN(cmd_mode); ctx->packet_to_hv.flags.phase_type = PIN(phase_mode); - ctx->packet_to_hv.pos = pos; - ctx->packet_to_hv.vel = vel; - ctx->packet_to_hv.addr = ctx->addr; - ctx->packet_to_hv.value = ctx->config.data[ctx->addr++]; + ctx->packet_to_hv.pos = pos; + ctx->packet_to_hv.vel = vel; + ctx->packet_to_hv.addr = ctx->addr; + ctx->packet_to_hv.value = ctx->config.data[ctx->addr++]; ctx->addr %= sizeof(f3_config_data_t) / 4; CRC_ResetDR(); - ctx->packet_to_hv.crc = CRC_CalcBlockCRC((uint32_t *) &(ctx->packet_to_hv), sizeof(packet_to_hv_t) / 4 - 1); + ctx->packet_to_hv.crc = CRC_CalcBlockCRC((uint32_t *)&(ctx->packet_to_hv), sizeof(packet_to_hv_t) / 4 - 1); PIN(uart_sr) = UART_DRV->SR; PIN(uart_dr) = UART_DRV->DR; - //start DMA RX transfer - DMA_Cmd(UART_DRV_RX_DMA, DISABLE); - DMA_ClearFlag(UART_DRV_RX_DMA, UART_DRV_RX_DMA_TCIF); - DMA_Cmd(UART_DRV_RX_DMA, ENABLE); - + //start DMA RX transfer + DMA_Cmd(UART_DRV_RX_DMA, DISABLE); + DMA_ClearFlag(UART_DRV_RX_DMA, UART_DRV_RX_DMA_TCIF); + DMA_Cmd(UART_DRV_RX_DMA, ENABLE); + //start DMA TX transfer DMA_Cmd(UART_DRV_TX_DMA, DISABLE); DMA_ClearFlag(UART_DRV_TX_DMA, UART_DRV_TX_DMA_TCIF); DMA_Cmd(UART_DRV_TX_DMA, ENABLE); - - // PIN(power) = PIN(dc_cur) * PIN(dc_volt); @@ -272,15 +271,15 @@ static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst hal_comp_t hv_comp_struct = { - .name = "hv", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct hv_ctx_t), - .pin_count = sizeof(struct hv_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "hv", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct hv_ctx_t), + .pin_count = sizeof(struct hv_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/src/comps/hw/io4.c b/src/comps/hw/io4.c index caa7e2f3..dc8b23ed 100644 --- a/src/comps/hw/io4.c +++ b/src/comps/hw/io4.c @@ -8,17 +8,16 @@ #include "main.h" volatile uint64_t systime; -#define BLINK(N) \ -({ \ - int t = (systime / 300) % (2 * N + 2); \ - if(t < 2){ \ - t = 0; \ - } \ - else{ \ - t = t % 2; \ - } \ - t;\ -}) +#define BLINK(N) \ + ({ \ + int t = (systime / 300) % (2 * N + 2); \ + if(t < 2) { \ + t = 0; \ + } else { \ + t = t % 2; \ + } \ + t; \ + }) HAL_COMP(io); @@ -84,414 +83,410 @@ HAL_PIN(fb1a); HAL_PIN(fb1b); HAL_PIN(fb1z); -HAL_PIN(fbsd);//fb shutdown +HAL_PIN(fbsd); //fb shutdown -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct io_pin_ctx_t * pins = (struct io_pin_ctx_t *)pin_ptr; - PIN(th0) = 12.0; - PIN(th1) = 12.0; - PIN(fbth0) = 2.5; - PIN(fbth1) = 2.5; +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct io_pin_ctx_t *pins = (struct io_pin_ctx_t *)pin_ptr; + PIN(th0) = 12.0; + PIN(th1) = 12.0; + PIN(fbth0) = 2.5; + PIN(fbth1) = 2.5; } -static void hw_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - // struct io_ctx_t * ctx = (struct io_ctx_t *)ctx_ptr; - struct io_pin_ctx_t * pins = (struct io_pin_ctx_t *)pin_ptr; +static void hw_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct io_ctx_t * ctx = (struct io_ctx_t *)ctx_ptr; + struct io_pin_ctx_t *pins = (struct io_pin_ctx_t *)pin_ptr; - GPIO_InitTypeDef GPIO_InitStructure; - ADC_InitTypeDef ADC_InitStructure; - - //**** ADC3 for analog input and fb temperature - //TODO: ADC calibration? - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_Init(GPIOC, &GPIO_InitStructure); - - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; - GPIO_Init(GPIOD, &GPIO_InitStructure); - - if(PIN(swd_remap) > 0){ - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; - GPIO_Init(GPIOA, &GPIO_InitStructure); - } - - RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE); - ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//data converted will be shifted to right - ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//Input voltage is converted into a 12bit number giving a maximum value of 4096 - ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //the conversion is continuous, the input data is converted more than once - ADC_InitStructure.ADC_ExternalTrigConv = TIM_MASTER_ADC;//trigger on rising edge of TIM_MASTER oc - ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; - ADC_InitStructure.ADC_NbrOfConversion = 1;//ADC_ANZ;//I think this one is clear :p - ADC_InitStructure.ADC_ScanConvMode = ENABLE;//The scan is configured in one channel - ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; - ADC_Init(ADC3, &ADC_InitStructure); - ADC_InjectedSequencerLengthConfig(ADC3, 4); - ADC_InjectedChannelConfig(ADC3, ADC_Channel_10, 1, ADC_SampleTime_144Cycles); - ADC_InjectedChannelConfig(ADC3, ADC_Channel_11, 2, ADC_SampleTime_144Cycles); - ADC_InjectedChannelConfig(ADC3, ADC_Channel_12, 3, ADC_SampleTime_144Cycles); - ADC_InjectedChannelConfig(ADC3, ADC_Channel_13, 4, ADC_SampleTime_144Cycles); - ADC_Cmd(ADC3, ENABLE); - ADC_SoftwareStartInjectedConv(ADC3); - //**** ADC3 end - - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; - - //fan - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; - GPIO_Init(GPIOE, &GPIO_InitStructure); + GPIO_InitTypeDef GPIO_InitStructure; + ADC_InitTypeDef ADC_InitStructure; - //red - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; - GPIO_Init(GPIOD, &GPIO_InitStructure); + //**** ADC3 for analog input and fb temperature + //TODO: ADC calibration? + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(GPIOC, &GPIO_InitStructure); - //yellow - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; - GPIO_Init(GPIOD, &GPIO_InitStructure); + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; + GPIO_Init(GPIOD, &GPIO_InitStructure); - //green - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; - GPIO_Init(GPIOD, &GPIO_InitStructure); - - //in1 led - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; - GPIO_Init(GPIOE, &GPIO_InitStructure); - - //in0 led - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; - GPIO_Init(GPIOE, &GPIO_InitStructure); - - //out0 - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; - GPIO_Init(GPIOE, &GPIO_InitStructure); + if(PIN(swd_remap) > 0) { + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; + GPIO_Init(GPIOA, &GPIO_InitStructure); + } - //out1 - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; - GPIO_Init(GPIOE, &GPIO_InitStructure); - - //out2 - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; - GPIO_Init(GPIOE, &GPIO_InitStructure); - - //fb0 green - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; - GPIO_Init(GPIOD, &GPIO_InitStructure); - - //fb0 yellow - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; - GPIO_Init(GPIOD, &GPIO_InitStructure); - - //fb1 green - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; - GPIO_Init(GPIOE, &GPIO_InitStructure); - - //fb1 yellow - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; - GPIO_Init(GPIOE, &GPIO_InitStructure); - - //cmd - // GPIO_InitStructure.GPIO_Pin = CMD_C_EN_PIN; - // GPIO_Init(CMD_C_EN_PORT, &GPIO_InitStructure); - // - // GPIO_InitStructure.GPIO_Pin = CMD_D_EN_PIN; - // GPIO_Init(CMD_D_EN_PORT, &GPIO_InitStructure); - - //cmd yellow - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; - GPIO_Init(GPIOD, &GPIO_InitStructure); - - //cmd green - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; - GPIO_Init(GPIOD, &GPIO_InitStructure); - - //fb 5v enable - GPIO_SetBits(GPIOC, GPIO_Pin_13); - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; - GPIO_Init(GPIOC, &GPIO_InitStructure); - - if(PIN(cmd_remap) > 0){ - // CMD EN - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; - GPIO_InitStructure.GPIO_Pin = CMD_A_EN_PIN; - GPIO_Init(CMD_A_EN_PORT, &GPIO_InitStructure); - GPIO_InitStructure.GPIO_Pin = CMD_B_EN_PIN; - GPIO_Init(CMD_B_EN_PORT, &GPIO_InitStructure); - GPIO_InitStructure.GPIO_Pin = CMD_C_EN_PIN; - GPIO_Init(CMD_C_EN_PORT, &GPIO_InitStructure); - - GPIO_SetBits(CMD_A_EN_PORT, CMD_A_EN_PIN); - GPIO_SetBits(CMD_B_EN_PORT, CMD_B_EN_PIN); - GPIO_SetBits(CMD_C_EN_PORT, CMD_C_EN_PIN); - } + RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE); + ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //data converted will be shifted to right + ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //Input voltage is converted into a 12bit number giving a maximum value of 4096 + ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //the conversion is continuous, the input data is converted more than once + ADC_InitStructure.ADC_ExternalTrigConv = TIM_MASTER_ADC; //trigger on rising edge of TIM_MASTER oc + ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; + ADC_InitStructure.ADC_NbrOfConversion = 1; //ADC_ANZ;//I think this one is clear :p + ADC_InitStructure.ADC_ScanConvMode = ENABLE; //The scan is configured in one channel + ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; + ADC_Init(ADC3, &ADC_InitStructure); + ADC_InjectedSequencerLengthConfig(ADC3, 4); + ADC_InjectedChannelConfig(ADC3, ADC_Channel_10, 1, ADC_SampleTime_144Cycles); + ADC_InjectedChannelConfig(ADC3, ADC_Channel_11, 2, ADC_SampleTime_144Cycles); + ADC_InjectedChannelConfig(ADC3, ADC_Channel_12, 3, ADC_SampleTime_144Cycles); + ADC_InjectedChannelConfig(ADC3, ADC_Channel_13, 4, ADC_SampleTime_144Cycles); + ADC_Cmd(ADC3, ENABLE); + ADC_SoftwareStartInjectedConv(ADC3); + //**** ADC3 end + + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + + //fan + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; + GPIO_Init(GPIOE, &GPIO_InitStructure); + + //red + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; + GPIO_Init(GPIOD, &GPIO_InitStructure); + + //yellow + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; + GPIO_Init(GPIOD, &GPIO_InitStructure); + + //green + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; + GPIO_Init(GPIOD, &GPIO_InitStructure); + + //in1 led + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; + GPIO_Init(GPIOE, &GPIO_InitStructure); + + //in0 led + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; + GPIO_Init(GPIOE, &GPIO_InitStructure); + + //out0 + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; + GPIO_Init(GPIOE, &GPIO_InitStructure); + + //out1 + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; + GPIO_Init(GPIOE, &GPIO_InitStructure); + + //out2 + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; + GPIO_Init(GPIOE, &GPIO_InitStructure); + + //fb0 green + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; + GPIO_Init(GPIOD, &GPIO_InitStructure); + + //fb0 yellow + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; + GPIO_Init(GPIOD, &GPIO_InitStructure); + + //fb1 green + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; + GPIO_Init(GPIOE, &GPIO_InitStructure); + + //fb1 yellow + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; + GPIO_Init(GPIOE, &GPIO_InitStructure); + + //cmd + // GPIO_InitStructure.GPIO_Pin = CMD_C_EN_PIN; + // GPIO_Init(CMD_C_EN_PORT, &GPIO_InitStructure); + // + // GPIO_InitStructure.GPIO_Pin = CMD_D_EN_PIN; + // GPIO_Init(CMD_D_EN_PORT, &GPIO_InitStructure); + + //cmd yellow + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; + GPIO_Init(GPIOD, &GPIO_InitStructure); + + //cmd green + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; + GPIO_Init(GPIOD, &GPIO_InitStructure); + + //fb 5v enable + GPIO_SetBits(GPIOC, GPIO_Pin_13); + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; + GPIO_Init(GPIOC, &GPIO_InitStructure); + + if(PIN(cmd_remap) > 0) { + // CMD EN + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_InitStructure.GPIO_Pin = CMD_A_EN_PIN; + GPIO_Init(CMD_A_EN_PORT, &GPIO_InitStructure); + GPIO_InitStructure.GPIO_Pin = CMD_B_EN_PIN; + GPIO_Init(CMD_B_EN_PORT, &GPIO_InitStructure); + GPIO_InitStructure.GPIO_Pin = CMD_C_EN_PIN; + GPIO_Init(CMD_C_EN_PORT, &GPIO_InitStructure); + + GPIO_SetBits(CMD_A_EN_PORT, CMD_A_EN_PIN); + GPIO_SetBits(CMD_B_EN_PORT, CMD_B_EN_PIN); + GPIO_SetBits(CMD_C_EN_PORT, CMD_C_EN_PIN); + } } #include "../shared/hw_math.h" #define ARES 4096.0 #define AREF 3.3 -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - // struct io_ctx_t * ctx = (struct io_ctx_t *)ctx_ptr; - struct io_pin_ctx_t * pins = (struct io_pin_ctx_t *)pin_ptr; - uint32_t red = 0; - uint32_t green = 0; - uint32_t yellow = 0; - - //TODO: unit conversion - //TODO: check if adc sample complete? - float in0 = V2(ADC2V(ADC_GetInjectedConversionValue(ADC3, ADC_InjectedChannel_1)), 3.3, 1000.0, 10000.0, 1000.0); - float in1 = V2(ADC2V(ADC_GetInjectedConversionValue(ADC3, ADC_InjectedChannel_2)), 3.3, 1000.0, 10000.0, 1000.0); - PIN(in0) = in0; - PIN(in1) = in1; - - if(in0 > PIN(th0) + 0.1){ - PIN(ind0) = 1.0; - PIN(ind0n) = 0.0; - GPIO_SetBits(GPIOE, GPIO_Pin_1); - } - else if(in0 < PIN(th0) - 0.1){ - PIN(ind0) = 0.0; - PIN(ind0n) = 1.0; - GPIO_ResetBits(GPIOE, GPIO_Pin_1); - } - - if(in1 > PIN(th1) + 0.1){ - PIN(ind1) = 1.0; - PIN(ind1n) = 0.0; - GPIO_SetBits(GPIOE, GPIO_Pin_0); - } - else if(in1 < PIN(th1) - 0.1){ - PIN(ind1) = 0.0; - PIN(ind1n) = 1.0; - GPIO_ResetBits(GPIOE, GPIO_Pin_0); - } - - - in0 = V3(ADC2V(ADC_GetInjectedConversionValue(ADC3, ADC_InjectedChannel_3)), 10000.0, 1000.0); - in1 = V3(ADC2V(ADC_GetInjectedConversionValue(ADC3, ADC_InjectedChannel_4)), 10000.0, 1000.0); - PIN(fb0) = in0; - PIN(fb1) = in1; - - ADC_SoftwareStartInjectedConv(ADC3); - - if(in0 > PIN(fbth0) + 0.1){ - PIN(fbd0) = 1.0; - PIN(fbd0n) = 0.0; - } - else if(in0 < PIN(fbth0) - 0.1){ - PIN(fbd0) = 0.0; - PIN(fbd0n) = 1.0; - } - - if(in1 > PIN(fbth1) + 0.1){ - PIN(fbd1) = 1.0; - PIN(fbd1n) = 0.0; - } - else if(in1 < PIN(fbth1) - 0.1){ - PIN(fbd1) = 0.0; - PIN(fbd1n) = 1.0; - } +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct io_ctx_t * ctx = (struct io_ctx_t *)ctx_ptr; + struct io_pin_ctx_t *pins = (struct io_pin_ctx_t *)pin_ptr; + uint32_t red = 0; + uint32_t green = 0; + uint32_t yellow = 0; - if(PIN(swd_remap) > 0){ - PIN(DIO) = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_13); - PIN(CK) = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_14); - } + //TODO: unit conversion + //TODO: check if adc sample complete? + float in0 = V2(ADC2V(ADC_GetInjectedConversionValue(ADC3, ADC_InjectedChannel_1)), 3.3, 1000.0, 10000.0, 1000.0); + float in1 = V2(ADC2V(ADC_GetInjectedConversionValue(ADC3, ADC_InjectedChannel_2)), 3.3, 1000.0, 10000.0, 1000.0); + PIN(in0) = in0; + PIN(in1) = in1; - switch((state_t)PIN(state)){ - case DISABLED: - red = 0; - green = 0; - yellow = 1; - break; - - case ENABLED: - red = 0; - green = 1; - yellow = 0; - break; - - case PHASING: - red = 0; - green = 1; - yellow = 1; - break; - - case SOFT_FAULT: - red = BLINK((int)PIN(fault)); - green = 0; - yellow = 0; - break; - - case HARD_FAULT: - red = BLINK((int)PIN(fault)); - green = BLINK((int)PIN(fault)); - yellow = BLINK((int)PIN(fault)); + if(in0 > PIN(th0) + 0.1) { + PIN(ind0) = 1.0; + PIN(ind0n) = 0.0; + GPIO_SetBits(GPIOE, GPIO_Pin_1); + } else if(in0 < PIN(th0) - 0.1) { + PIN(ind0) = 0.0; + PIN(ind0n) = 1.0; + GPIO_ResetBits(GPIOE, GPIO_Pin_1); + } + + if(in1 > PIN(th1) + 0.1) { + PIN(ind1) = 1.0; + PIN(ind1n) = 0.0; + GPIO_SetBits(GPIOE, GPIO_Pin_0); + } else if(in1 < PIN(th1) - 0.1) { + PIN(ind1) = 0.0; + PIN(ind1n) = 1.0; + GPIO_ResetBits(GPIOE, GPIO_Pin_0); + } + + + in0 = V3(ADC2V(ADC_GetInjectedConversionValue(ADC3, ADC_InjectedChannel_3)), 10000.0, 1000.0); + in1 = V3(ADC2V(ADC_GetInjectedConversionValue(ADC3, ADC_InjectedChannel_4)), 10000.0, 1000.0); + PIN(fb0) = in0; + PIN(fb1) = in1; + + ADC_SoftwareStartInjectedConv(ADC3); + + if(in0 > PIN(fbth0) + 0.1) { + PIN(fbd0) = 1.0; + PIN(fbd0n) = 0.0; + } else if(in0 < PIN(fbth0) - 0.1) { + PIN(fbd0) = 0.0; + PIN(fbd0n) = 1.0; + } + + if(in1 > PIN(fbth1) + 0.1) { + PIN(fbd1) = 1.0; + PIN(fbd1n) = 0.0; + } else if(in1 < PIN(fbth1) - 0.1) { + PIN(fbd1) = 0.0; + PIN(fbd1n) = 1.0; + } + + if(PIN(swd_remap) > 0) { + PIN(DIO) = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_13); + PIN(CK) = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_14); + } + + switch((state_t)PIN(state)) { + case DISABLED: + red = 0; + green = 0; + yellow = 1; break; - case LED_TEST: - red = 1; - green = 1; - yellow = 1; + case ENABLED: + red = 0; + green = 1; + yellow = 0; break; - } - if(red > 0) - GPIO_SetBits(GPIOD, GPIO_Pin_3); - else - GPIO_ResetBits(GPIOD, GPIO_Pin_3); + case PHASING: + red = 0; + green = 1; + yellow = 1; + break; - if(yellow > 0) - GPIO_SetBits(GPIOD, GPIO_Pin_4); - else - GPIO_ResetBits(GPIOD, GPIO_Pin_4); + case SOFT_FAULT: + red = BLINK((int)PIN(fault)); + green = 0; + yellow = 0; + break; - if(green > 0) - GPIO_SetBits(GPIOD, GPIO_Pin_5); - else - GPIO_ResetBits(GPIOD, GPIO_Pin_5); + case HARD_FAULT: + red = BLINK((int)PIN(fault)); + green = BLINK((int)PIN(fault)); + yellow = BLINK((int)PIN(fault)); + break; - if(PIN(out0) > 0) - GPIO_SetBits(GPIOE, GPIO_Pin_4); - else - GPIO_ResetBits(GPIOE, GPIO_Pin_4); + case LED_TEST: + red = 1; + green = 1; + yellow = 1; + break; + } - if(PIN(out1) > 0) - GPIO_SetBits(GPIOE, GPIO_Pin_5); - else - GPIO_ResetBits(GPIOE, GPIO_Pin_5); + if(red > 0) + GPIO_SetBits(GPIOD, GPIO_Pin_3); + else + GPIO_ResetBits(GPIOD, GPIO_Pin_3); - if(PIN(out2) > 0) - GPIO_SetBits(GPIOE, GPIO_Pin_6); - else - GPIO_ResetBits(GPIOE, GPIO_Pin_6); - - if(PIN(fb0g) > 0) - GPIO_SetBits(GPIOD, GPIO_Pin_8); - else - GPIO_ResetBits(GPIOD, GPIO_Pin_8); - - if(PIN(fb0y) > 0) - GPIO_SetBits(GPIOD, GPIO_Pin_9); - else - GPIO_ResetBits(GPIOD, GPIO_Pin_9); - - if(PIN(fb1g) > 0) - GPIO_SetBits(GPIOE, GPIO_Pin_7); - else - GPIO_ResetBits(GPIOE, GPIO_Pin_7); - - if(PIN(fb1y) > 0) - GPIO_SetBits(GPIOE, GPIO_Pin_8); - else - GPIO_ResetBits(GPIOE, GPIO_Pin_8); - - // if(PIN(cmdc_en) > 0){ - // GPIO_SetBits(CMD_C_EN_PORT, CMD_C_EN_PIN); - // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; - // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; - // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; - // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; - // GPIO_InitStructure.GPIO_Pin = CMD_C_PIN; - // GPIO_Init(CMD_C_PORT, &GPIO_InitStructure); - // if(PIN(cmdc) > 0){ - // GPIO_SetBits(CMD_C_PORT, CMD_C_PIN); - // } - // else{ - // GPIO_ResetBits(CMD_C_PORT, CMD_C_PIN); - // } - // } - // else{ - // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; - // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; - // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; - // GPIO_InitStructure.GPIO_Pin = CMD_C_PIN; - // GPIO_Init(CMD_C_PORT, &GPIO_InitStructure); - // GPIO_ResetBits(CMD_C_EN_PORT, CMD_C_EN_PIN); - // PIN(cmdc) = (CMD_C_PORT->IDR & CMD_C_PIN) > 0; - // } - // - // if(PIN(cmdd_en) > 0){ - // GPIO_SetBits(CMD_D_EN_PORT, CMD_D_EN_PIN); - // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; - // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; - // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; - // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; - // GPIO_InitStructure.GPIO_Pin = CMD_C_PIN; - // GPIO_Init(CMD_D_PORT, &GPIO_InitStructure); - // if(PIN(cmdd) > 0){ - // GPIO_SetBits(CMD_D_PORT, CMD_D_PIN); - // } - // else{ - // GPIO_ResetBits(CMD_D_PORT, CMD_D_PIN); - // } - // } - // else{ - // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; - // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; - // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; - // GPIO_InitStructure.GPIO_Pin = CMD_D_PIN; - // GPIO_Init(CMD_D_PORT, &GPIO_InitStructure); - // GPIO_ResetBits(CMD_D_EN_PORT, CMD_D_EN_PIN); - // PIN(cmdd) = (CMD_D_PORT->IDR & CMD_D_PIN) > 0; - // } - - if(PIN(cmdy) > 0) - GPIO_SetBits(GPIOD, GPIO_Pin_6); - else - GPIO_ResetBits(GPIOD, GPIO_Pin_6); - - if(PIN(cmdg) > 0) - GPIO_SetBits(GPIOD, GPIO_Pin_7); - else - GPIO_ResetBits(GPIOD, GPIO_Pin_7); + if(yellow > 0) + GPIO_SetBits(GPIOD, GPIO_Pin_4); + else + GPIO_ResetBits(GPIOD, GPIO_Pin_4); - if(PIN(fan) > 0) - GPIO_SetBits(GPIOE, GPIO_Pin_3); - else - GPIO_ResetBits(GPIOE, GPIO_Pin_3); + if(green > 0) + GPIO_SetBits(GPIOD, GPIO_Pin_5); + else + GPIO_ResetBits(GPIOD, GPIO_Pin_5); - if(PIN(fbsd) > 0) - GPIO_ResetBits(GPIOC, GPIO_Pin_13); - else - GPIO_SetBits(GPIOC, GPIO_Pin_13); + if(PIN(out0) > 0) + GPIO_SetBits(GPIOE, GPIO_Pin_4); + else + GPIO_ResetBits(GPIOE, GPIO_Pin_4); - PIN(CRX) = GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_0); - PIN(CTX) = GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_1); - PIN(C12) = GPIO_ReadInputDataBit(CMD_A_PORT, CMD_A_PIN); - PIN(C36) = GPIO_ReadInputDataBit(CMD_B_PORT, CMD_B_PIN); - PIN(C54) = GPIO_ReadInputDataBit(CMD_C_PORT, CMD_C_PIN); - - PIN(fb0a) = GPIO_ReadInputDataBit(FB0_A_PORT,FB0_A_PIN); - PIN(fb0b) = GPIO_ReadInputDataBit(FB0_B_PORT,FB0_B_PIN); - PIN(fb0z) = GPIO_ReadInputDataBit(FB0_Z_PORT,FB0_Z_PIN); + if(PIN(out1) > 0) + GPIO_SetBits(GPIOE, GPIO_Pin_5); + else + GPIO_ResetBits(GPIOE, GPIO_Pin_5); - PIN(fb1a) = GPIO_ReadInputDataBit(FB1_A_PORT,FB1_A_PIN); - PIN(fb1b) = GPIO_ReadInputDataBit(FB1_B_PORT,FB1_B_PIN); - PIN(fb1z) = GPIO_ReadInputDataBit(FB1_Z_PORT,FB1_Z_PIN); + if(PIN(out2) > 0) + GPIO_SetBits(GPIOE, GPIO_Pin_6); + else + GPIO_ResetBits(GPIOE, GPIO_Pin_6); + + if(PIN(fb0g) > 0) + GPIO_SetBits(GPIOD, GPIO_Pin_8); + else + GPIO_ResetBits(GPIOD, GPIO_Pin_8); + + if(PIN(fb0y) > 0) + GPIO_SetBits(GPIOD, GPIO_Pin_9); + else + GPIO_ResetBits(GPIOD, GPIO_Pin_9); + + if(PIN(fb1g) > 0) + GPIO_SetBits(GPIOE, GPIO_Pin_7); + else + GPIO_ResetBits(GPIOE, GPIO_Pin_7); + + if(PIN(fb1y) > 0) + GPIO_SetBits(GPIOE, GPIO_Pin_8); + else + GPIO_ResetBits(GPIOE, GPIO_Pin_8); + + // if(PIN(cmdc_en) > 0){ + // GPIO_SetBits(CMD_C_EN_PORT, CMD_C_EN_PIN); + // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; + // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; + // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + // GPIO_InitStructure.GPIO_Pin = CMD_C_PIN; + // GPIO_Init(CMD_C_PORT, &GPIO_InitStructure); + // if(PIN(cmdc) > 0){ + // GPIO_SetBits(CMD_C_PORT, CMD_C_PIN); + // } + // else{ + // GPIO_ResetBits(CMD_C_PORT, CMD_C_PIN); + // } + // } + // else{ + // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; + // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; + // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + // GPIO_InitStructure.GPIO_Pin = CMD_C_PIN; + // GPIO_Init(CMD_C_PORT, &GPIO_InitStructure); + // GPIO_ResetBits(CMD_C_EN_PORT, CMD_C_EN_PIN); + // PIN(cmdc) = (CMD_C_PORT->IDR & CMD_C_PIN) > 0; + // } + // + // if(PIN(cmdd_en) > 0){ + // GPIO_SetBits(CMD_D_EN_PORT, CMD_D_EN_PIN); + // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; + // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; + // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + // GPIO_InitStructure.GPIO_Pin = CMD_C_PIN; + // GPIO_Init(CMD_D_PORT, &GPIO_InitStructure); + // if(PIN(cmdd) > 0){ + // GPIO_SetBits(CMD_D_PORT, CMD_D_PIN); + // } + // else{ + // GPIO_ResetBits(CMD_D_PORT, CMD_D_PIN); + // } + // } + // else{ + // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; + // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; + // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + // GPIO_InitStructure.GPIO_Pin = CMD_D_PIN; + // GPIO_Init(CMD_D_PORT, &GPIO_InitStructure); + // GPIO_ResetBits(CMD_D_EN_PORT, CMD_D_EN_PIN); + // PIN(cmdd) = (CMD_D_PORT->IDR & CMD_D_PIN) > 0; + // } + + if(PIN(cmdy) > 0) + GPIO_SetBits(GPIOD, GPIO_Pin_6); + else + GPIO_ResetBits(GPIOD, GPIO_Pin_6); + + if(PIN(cmdg) > 0) + GPIO_SetBits(GPIOD, GPIO_Pin_7); + else + GPIO_ResetBits(GPIOD, GPIO_Pin_7); + + if(PIN(fan) > 0) + GPIO_SetBits(GPIOE, GPIO_Pin_3); + else + GPIO_ResetBits(GPIOE, GPIO_Pin_3); + + if(PIN(fbsd) > 0) + GPIO_ResetBits(GPIOC, GPIO_Pin_13); + else + GPIO_SetBits(GPIOC, GPIO_Pin_13); + + PIN(CRX) = GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_0); + PIN(CTX) = GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_1); + PIN(C12) = GPIO_ReadInputDataBit(CMD_A_PORT, CMD_A_PIN); + PIN(C36) = GPIO_ReadInputDataBit(CMD_B_PORT, CMD_B_PIN); + PIN(C54) = GPIO_ReadInputDataBit(CMD_C_PORT, CMD_C_PIN); + + PIN(fb0a) = GPIO_ReadInputDataBit(FB0_A_PORT, FB0_A_PIN); + PIN(fb0b) = GPIO_ReadInputDataBit(FB0_B_PORT, FB0_B_PIN); + PIN(fb0z) = GPIO_ReadInputDataBit(FB0_Z_PORT, FB0_Z_PIN); + + PIN(fb1a) = GPIO_ReadInputDataBit(FB1_A_PORT, FB1_A_PIN); + PIN(fb1b) = GPIO_ReadInputDataBit(FB1_B_PORT, FB1_B_PIN); + PIN(fb1z) = GPIO_ReadInputDataBit(FB1_Z_PORT, FB1_Z_PIN); } hal_comp_t io_comp_struct = { - .name = "io", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .hw_init = hw_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = 0, - .pin_count = sizeof(struct io_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "io", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .hw_init = hw_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct io_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/src/comps/o_fb.c b/src/comps/o_fb.c index 9e63b385..1bbb82d4 100644 --- a/src/comps/o_fb.c +++ b/src/comps/o_fb.c @@ -11,64 +11,61 @@ HAL_COMP(o_fb); HAL_PIN(a); HAL_PIN(b); -static void hw_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - // struct o_fb_ctx_t * ctx = (struct o_fb_ctx_t *)ctx_ptr; - // struct o_fb_pin_ctx_t * pins = (struct o_fb_pin_ctx_t *)pin_ptr; - - GPIO_InitTypeDef GPIO_InitStructure; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; - - GPIO_InitStructure.GPIO_Pin = FB0_A_EN_PIN; - GPIO_Init(FB0_A_EN_PORT, &GPIO_InitStructure); - - GPIO_InitStructure.GPIO_Pin = FB0_B_EN_PIN; - GPIO_Init(FB0_B_EN_PORT, &GPIO_InitStructure); - - GPIO_SetBits(FB0_A_EN_PORT, FB0_A_EN_PIN); - GPIO_SetBits(FB0_B_EN_PORT, FB0_B_EN_PIN); - - GPIO_InitStructure.GPIO_Pin = FB0_A_PIN; - GPIO_Init(FB0_A_PORT, &GPIO_InitStructure); - - GPIO_InitStructure.GPIO_Pin = FB0_B_PIN; - GPIO_Init(FB0_B_PORT, &GPIO_InitStructure); +static void hw_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct o_fb_ctx_t * ctx = (struct o_fb_ctx_t *)ctx_ptr; + // struct o_fb_pin_ctx_t * pins = (struct o_fb_pin_ctx_t *)pin_ptr; + + GPIO_InitTypeDef GPIO_InitStructure; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + + GPIO_InitStructure.GPIO_Pin = FB0_A_EN_PIN; + GPIO_Init(FB0_A_EN_PORT, &GPIO_InitStructure); + + GPIO_InitStructure.GPIO_Pin = FB0_B_EN_PIN; + GPIO_Init(FB0_B_EN_PORT, &GPIO_InitStructure); + + GPIO_SetBits(FB0_A_EN_PORT, FB0_A_EN_PIN); + GPIO_SetBits(FB0_B_EN_PORT, FB0_B_EN_PIN); + + GPIO_InitStructure.GPIO_Pin = FB0_A_PIN; + GPIO_Init(FB0_A_PORT, &GPIO_InitStructure); + + GPIO_InitStructure.GPIO_Pin = FB0_B_PIN; + GPIO_Init(FB0_B_PORT, &GPIO_InitStructure); } +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct o_fb_ctx_t * ctx = (struct o_fb_ctx_t *)ctx_ptr; + struct o_fb_pin_ctx_t *pins = (struct o_fb_pin_ctx_t *)pin_ptr; -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - // struct o_fb_ctx_t * ctx = (struct o_fb_ctx_t *)ctx_ptr; - struct o_fb_pin_ctx_t * pins = (struct o_fb_pin_ctx_t *)pin_ptr; - - if(PIN(a) > 0){ - GPIO_SetBits(FB0_A_PORT, FB0_A_PIN); - } - else{ - GPIO_ResetBits(FB0_A_PORT, FB0_A_PIN); - } - - if(PIN(b) > 0){ - GPIO_SetBits(FB0_B_PORT, FB0_B_PIN); - } - else{ - GPIO_ResetBits(FB0_B_PORT, FB0_B_PIN); - } + if(PIN(a) > 0) { + GPIO_SetBits(FB0_A_PORT, FB0_A_PIN); + } else { + GPIO_ResetBits(FB0_A_PORT, FB0_A_PIN); + } + + if(PIN(b) > 0) { + GPIO_SetBits(FB0_B_PORT, FB0_B_PIN); + } else { + GPIO_ResetBits(FB0_B_PORT, FB0_B_PIN); + } } const hal_comp_t o_fb_comp_struct = { - .name = "o_fb", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = 0, - .hw_init = hw_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = 0, - .pin_count = sizeof(struct o_fb_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "o_fb", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = 0, + .hw_init = hw_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct o_fb_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/src/comps/res.c b/src/comps/res.c index abf1fe14..42f0145f 100644 --- a/src/comps/res.c +++ b/src/comps/res.c @@ -13,7 +13,7 @@ HAL_PIN(amp); HAL_PIN(quad); HAL_PIN(poles); -HAL_PIN(vel); // TODO: vel rev, fb,cmd -> vel0,1 -> rev +HAL_PIN(vel); // TODO: vel rev, fb,cmd -> vel0,1 -> rev HAL_PIN(sin); HAL_PIN(cos); @@ -25,148 +25,147 @@ HAL_PIN(tim_oc); // TODO: in hal stop, reset adc dma -struct res_ctx_t{ - int lastq;// last quadrant - int abspos;// multiturn position +struct res_ctx_t { + int lastq; // last quadrant + int abspos; // multiturn position }; -static void hw_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct res_ctx_t * ctx = (struct res_ctx_t *)ctx_ptr; - struct res_pin_ctx_t * pins = (struct res_pin_ctx_t *)pin_ptr; - - PIN(poles) = 1.0; - PIN(error_n) = 1.0; - PIN(tim_oc) = 47.0; - - ctx->abspos = 0; - ctx->lastq = 0; - - TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; - TIM_OCInitTypeDef TIM_OCInitStructure; - GPIO_InitTypeDef GPIO_InitStructure; - - //timer init for v4, v3 uses slave timer - RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); - TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; - TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; - TIM_TimeBaseStructure.TIM_Period = 60 - 1;// 1.2e6 / 60 = 20kHz - TIM_TimeBaseStructure.TIM_Prescaler = 0; - TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; - TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); - TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_External1);// Rising edges of the selected trigger (TRGI) clock the counter - TIM_ITRxExternalClockConfig(TIM4, TIM_TS_ITR2);// clk = TIM_MASTER(TIM2) trigger out - TIM_ARRPreloadConfig(TIM4,ENABLE); - TIM_Cmd(TIM4, ENABLE); - - // resolver reference signal OC - TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; - TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; - TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; - TIM_OCInitStructure.TIM_Pulse = 0; - TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; - TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; - TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; - TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; - //ref is always OC3 - TIM_OC3Init(TIM4, &TIM_OCInitStructure); - TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable); - TIM_CtrlPWMOutputs(TIM4, ENABLE); - - //resolver ref signal generation - GPIO_InitStructure.GPIO_Pin = FB0_Z_PIN; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; - GPIO_Init(FB0_Z_PORT, &GPIO_InitStructure); - GPIO_PinAFConfig(FB0_Z_PORT, FB0_Z_PIN_SOURCE, GPIO_AF_TIM4); +static void hw_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct res_ctx_t *ctx = (struct res_ctx_t *)ctx_ptr; + struct res_pin_ctx_t *pins = (struct res_pin_ctx_t *)pin_ptr; - // TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; - // TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; - // TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; - // TIM_OCInitStructure.TIM_Pulse = 10; - // TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; - // TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; - // TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; - // TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; - // - // TIM_OC3Init(TIM2, &TIM_OCInitStructure); - // TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable); - // TIM_CtrlPWMOutputs(TIM2, ENABLE); - - //txen - GPIO_InitStructure.GPIO_Pin = FB0_Z_TXEN_PIN; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; - GPIO_Init(FB0_Z_TXEN_PORT, &GPIO_InitStructure); - GPIO_SetBits(FB0_Z_TXEN_PORT,FB0_Z_TXEN_PIN); + PIN(poles) = 1.0; + PIN(error_n) = 1.0; + PIN(tim_oc) = 47.0; + + ctx->abspos = 0; + ctx->lastq = 0; + + TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; + TIM_OCInitTypeDef TIM_OCInitStructure; + GPIO_InitTypeDef GPIO_InitStructure; + + //timer init for v4, v3 uses slave timer + RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); + TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; + TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; + TIM_TimeBaseStructure.TIM_Period = 60 - 1; // 1.2e6 / 60 = 20kHz + TIM_TimeBaseStructure.TIM_Prescaler = 0; + TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; + TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); + TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_External1); // Rising edges of the selected trigger (TRGI) clock the counter + TIM_ITRxExternalClockConfig(TIM4, TIM_TS_ITR2); // clk = TIM_MASTER(TIM2) trigger out + TIM_ARRPreloadConfig(TIM4, ENABLE); + TIM_Cmd(TIM4, ENABLE); + + // resolver reference signal OC + TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; + TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; + TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; + TIM_OCInitStructure.TIM_Pulse = 0; + TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; + TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; + TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; + TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; + //ref is always OC3 + TIM_OC3Init(TIM4, &TIM_OCInitStructure); + TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable); + TIM_CtrlPWMOutputs(TIM4, ENABLE); + + //resolver ref signal generation + GPIO_InitStructure.GPIO_Pin = FB0_Z_PIN; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_Init(FB0_Z_PORT, &GPIO_InitStructure); + GPIO_PinAFConfig(FB0_Z_PORT, FB0_Z_PIN_SOURCE, GPIO_AF_TIM4); + + // TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; + // TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; + // TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; + // TIM_OCInitStructure.TIM_Pulse = 10; + // TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; + // TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; + // TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; + // TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; + // + // TIM_OC3Init(TIM2, &TIM_OCInitStructure); + // TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable); + // TIM_CtrlPWMOutputs(TIM2, ENABLE); + + //txen + GPIO_InitStructure.GPIO_Pin = FB0_Z_TXEN_PIN; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_Init(FB0_Z_TXEN_PORT, &GPIO_InitStructure); + GPIO_SetBits(FB0_Z_TXEN_PORT, FB0_Z_TXEN_PIN); } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct res_ctx_t * ctx = (struct res_ctx_t *)ctx_ptr; - struct res_pin_ctx_t * pins = (struct res_pin_ctx_t *)pin_ptr; - //TODO: arr can change! - TIM4->CCR3 = (int)CLAMP(PIN(tim_oc), 0, TIM4->ARR - 1); - - float s = 0.0; - float c = 0.0; - float a = 0.0; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct res_ctx_t *ctx = (struct res_ctx_t *)ctx_ptr; + struct res_pin_ctx_t *pins = (struct res_pin_ctx_t *)pin_ptr; + //TODO: arr can change! + TIM4->CCR3 = (int)CLAMP(PIN(tim_oc), 0, TIM4->ARR - 1); - s = PIN(sin); - c = PIN(cos); - a = sqrtf(s * s + c * c); + float s = 0.0; + float c = 0.0; + float a = 0.0; - float p = MAX(1.0, PIN(poles)); - - float pos = atan2f(s, c); - float dpos = PIN(vel) * period / 2.0; + s = PIN(sin); + c = PIN(cos); + a = sqrtf(s * s + c * c); - if(a < 0.25){ - PIN(error) = 1.0; + float p = MAX(1.0, PIN(poles)); + + float pos = atan2f(s, c); + float dpos = PIN(vel) * period / 2.0; + + if(a < 0.25) { + PIN(error) = 1.0; PIN(error_n) = 0.0; - } - else{ - PIN(error) = 0.0; + } else { + PIN(error) = 0.0; PIN(error_n) = 1.0; - if(p == 1.0f){ - PIN(pos) = mod(pos + dpos); - }else{ - int q = PIN(quad);// current quadrant + if(p == 1.0f) { + PIN(pos) = mod(pos + dpos); + } else { + int q = PIN(quad); // current quadrant - if(ctx->lastq == 2 && q == 3) - ctx->abspos++; - if(ctx->lastq == 3 && q == 2) - ctx->abspos--; + if(ctx->lastq == 2 && q == 3) + ctx->abspos++; + if(ctx->lastq == 3 && q == 2) + ctx->abspos--; - if(ctx->abspos >= p){ - ctx->abspos = 0; - } - if(ctx->abspos <= -1){ - ctx->abspos = p - 1; - } - - ctx->lastq = q; - //TODO: clamp ctx->abspos - float absa = pos + ctx->abspos * M_PI * 2.0f; - PIN(pos) = mod(absa/p + dpos); + if(ctx->abspos >= p) { + ctx->abspos = 0; } - } - PIN(amp) = a; + if(ctx->abspos <= -1) { + ctx->abspos = p - 1; + } + + ctx->lastq = q; + //TODO: clamp ctx->abspos + float absa = pos + ctx->abspos * M_PI * 2.0f; + PIN(pos) = mod(absa / p + dpos); + } + } + PIN(amp) = a; } const hal_comp_t res_comp_struct = { - .name = "res", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = 0, - .hw_init = hw_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct res_ctx_t), - .pin_count = sizeof(struct res_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "res", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = 0, + .hw_init = hw_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct res_ctx_t), + .pin_count = sizeof(struct res_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/src/comps/sserial.c b/src/comps/sserial.c index 32a33fdd..860a66ff 100644 --- a/src/comps/sserial.c +++ b/src/comps/sserial.c @@ -36,9 +36,9 @@ HAL_COMP(sserial); // pins HAL_PIN(dump_pd_vals); HAL_PIN(error); -HAL_PIN(crc_error);//counts crc errors -HAL_PIN(connected);//connection status TODO: not stable during startup, needs link to pd -HAL_PIN(timeout);// 20khz / 1khz * 2 reads = 40 +HAL_PIN(crc_error); //counts crc errors +HAL_PIN(connected); //connection status TODO: not stable during startup, needs link to pd +HAL_PIN(timeout); // 20khz / 1khz * 2 reads = 40 HAL_PIN(pos_cmd); HAL_PIN(pos_cmd_d); @@ -57,13 +57,13 @@ HAL_PIN(out3); HAL_PIN(enable); //TODO: move to ctx -struct sserial_ctx_t{ - uint32_t foo; +struct sserial_ctx_t { + uint32_t foo; }; volatile uint8_t rxbuf[128]; volatile uint8_t txbuf[20]; -uint16_t address;//current address pointer +uint16_t address; //current address pointer int rxpos; memory_t memory; uint8_t *heap_ptr; @@ -77,98 +77,98 @@ int available; unit_no_t unit; uint8_t crc_reuest(uint8_t len) { - uint8_t crc = crc8_init(); - for(int i = rxpos; i < rxpos+len; i++){ - crc = crc8_update(crc, (void*)&(rxbuf[i%sizeof(rxbuf)]), 1); - } - crc8_finalize(crc); - return crc == rxbuf[(rxpos+len)%sizeof(rxbuf)]; + uint8_t crc = crc8_init(); + for(int i = rxpos; i < rxpos + len; i++) { + crc = crc8_update(crc, (void *)&(rxbuf[i % sizeof(rxbuf)]), 1); + } + crc8_finalize(crc); + return crc == rxbuf[(rxpos + len) % sizeof(rxbuf)]; } -uint8_t crc8( uint8_t *addr, uint8_t len) { - uint8_t crc = crc8_init(); - crc = crc8_update(crc, addr, len); - return crc8_finalize(crc); +uint8_t crc8(uint8_t *addr, uint8_t len) { + uint8_t crc = crc8_init(); + crc = crc8_update(crc, addr, len); + return crc8_finalize(crc); } -void send(uint8_t len, uint8_t docrc){ - timeout = 0; - if(docrc){ - txbuf[len] = crc8((uint8_t *)txbuf,len); - DMA_SetCurrDataCounter(DMA1_Stream4,len+1); - }else{ - DMA_SetCurrDataCounter(DMA1_Stream4,len); - } - DMA_Cmd(DMA1_Stream4, DISABLE); - DMA_ClearFlag(DMA1_Stream4, DMA_FLAG_TCIF4); - DMA_Cmd(DMA1_Stream4, ENABLE); +void send(uint8_t len, uint8_t docrc) { + timeout = 0; + if(docrc) { + txbuf[len] = crc8((uint8_t *)txbuf, len); + DMA_SetCurrDataCounter(DMA1_Stream4, len + 1); + } else { + DMA_SetCurrDataCounter(DMA1_Stream4, len); + } + DMA_Cmd(DMA1_Stream4, DISABLE); + DMA_ClearFlag(DMA1_Stream4, DMA_FLAG_TCIF4); + DMA_Cmd(DMA1_Stream4, ENABLE); } uint16_t add_pd(char *name_string, char *unit_string, uint8_t data_size_in_bits, uint8_t data_type, uint8_t data_dir, float param_min, float param_max) { - process_data_descriptor_t pdr; - pdr.record_type = RECORD_TYPE_PROCESS_DATA_RECORD; - pdr.data_size = data_size_in_bits; - pdr.data_type = data_type; - pdr.data_direction = data_dir; - pdr.param_min = param_min; - pdr.param_max = param_max; - pdr.data_addr = MEMPTR(*heap_ptr); - - heap_ptr += NUM_BYTES(data_size_in_bits); - // this aligns the heap pointer to 32bit. Not doing this causes the floats in the pd to be misaligned, which crashes the arm. - if((uint32_t)heap_ptr % 4){ - heap_ptr += 4 - (uint32_t)heap_ptr % 4; - } + process_data_descriptor_t pdr; + pdr.record_type = RECORD_TYPE_PROCESS_DATA_RECORD; + pdr.data_size = data_size_in_bits; + pdr.data_type = data_type; + pdr.data_direction = data_dir; + pdr.param_min = param_min; + pdr.param_max = param_max; + pdr.data_addr = MEMPTR(*heap_ptr); - memcpy(heap_ptr, &pdr, sizeof(process_data_descriptor_t)); - // note that we don't store the names in the struct anymore. The fixed-length struct is copied into memory, and then the nmaes go in directly behind it, so they'll read out properly + heap_ptr += NUM_BYTES(data_size_in_bits); + // this aligns the heap pointer to 32bit. Not doing this causes the floats in the pd to be misaligned, which crashes the arm. + if((uint32_t)heap_ptr % 4) { + heap_ptr += 4 - (uint32_t)heap_ptr % 4; + } - uint16_t pd_ptr = MEMPTR(*heap_ptr); // save off the ptr to return, before we modify the heap ptr + memcpy(heap_ptr, &pdr, sizeof(process_data_descriptor_t)); + // note that we don't store the names in the struct anymore. The fixed-length struct is copied into memory, and then the nmaes go in directly behind it, so they'll read out properly - heap_ptr = (uint8_t *)&(((process_data_descriptor_t *)heap_ptr)->names); + uint16_t pd_ptr = MEMPTR(*heap_ptr); // save off the ptr to return, before we modify the heap ptr - // copy the strings in after the pd - strcpy((char *)heap_ptr, unit_string); - heap_ptr += strlen(unit_string)+1; + heap_ptr = (uint8_t *)&(((process_data_descriptor_t *)heap_ptr)->names); - strcpy((char *)heap_ptr, name_string); - heap_ptr += strlen(name_string)+1; + // copy the strings in after the pd + strcpy((char *)heap_ptr, unit_string); + heap_ptr += strlen(unit_string) + 1; - // moved this up to before the pd record - /* + strcpy((char *)heap_ptr, name_string); + heap_ptr += strlen(name_string) + 1; + + // moved this up to before the pd record + /* // this aligns the heap pointer to 32bit. Not doing this causes the floats in the pd to be misaligned, which crashes the arm. if((uint32_t)heap_ptr % 4){ heap_ptr += 4 - (uint32_t)heap_ptr % 4; } */ - return pd_ptr; + return pd_ptr; } uint16_t add_mode(char *name_string, uint8_t index, uint8_t type) { - mode_descriptor_t mdr; - mdr.record_type = RECORD_TYPE_MODE_DATA_RECORD; - mdr.index = index; - mdr.type = type;//hw = 0, sw = 1 - mdr.unused = 0x00; + mode_descriptor_t mdr; + mdr.record_type = RECORD_TYPE_MODE_DATA_RECORD; + mdr.index = index; + mdr.type = type; //hw = 0, sw = 1 + mdr.unused = 0x00; - memcpy(heap_ptr, &mdr, sizeof(mode_descriptor_t)); + memcpy(heap_ptr, &mdr, sizeof(mode_descriptor_t)); - uint16_t md_ptr = MEMPTR(*heap_ptr); + uint16_t md_ptr = MEMPTR(*heap_ptr); - heap_ptr = (uint8_t *)&(((mode_descriptor_t *)heap_ptr)->names); + heap_ptr = (uint8_t *)&(((mode_descriptor_t *)heap_ptr)->names); - strcpy((char *)heap_ptr, name_string); - heap_ptr += strlen(name_string)+1; + strcpy((char *)heap_ptr, name_string); + heap_ptr += strlen(name_string) + 1; - return md_ptr; + return md_ptr; } void metadata(pd_metadata_t *pdm, process_data_descriptor_t *ptr) { - pdm->ptr = ptr; - pdm->range = ptr->data_type == DATA_TYPE_SIGNED ? MAX(ABS(ptr->param_min), ABS(ptr->param_max))*2 : ptr->param_max; - pdm->bitmax = (1<data_size)-1; + pdm->ptr = ptr; + pdm->range = ptr->data_type == DATA_TYPE_SIGNED ? MAX(ABS(ptr->param_min), ABS(ptr->param_max)) * 2 : ptr->param_max; + pdm->bitmax = (1 << ptr->data_size) - 1; } //v3 @@ -191,433 +191,446 @@ void metadata(pd_metadata_t *pdm, process_data_descriptor_t *ptr) { //TODO: lbp command 0xe6 to set mode -static void hw_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - // struct sserial_ctx_t * ctx = (struct sserial_ctx_t *)ctx_ptr; - struct sserial_pin_ctx_t * pins = (struct sserial_pin_ctx_t *)pin_ptr; +static void hw_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct sserial_ctx_t * ctx = (struct sserial_ctx_t *)ctx_ptr; + struct sserial_pin_ctx_t *pins = (struct sserial_pin_ctx_t *)pin_ptr; - PIN(timeout) = 100.0;// 20khz / 1khz * 2 reads = 40 + PIN(timeout) = 100.0; // 20khz / 1khz * 2 reads = 40 - GPIO_InitTypeDef GPIO_InitStruct; - USART_InitTypeDef USART_InitStruct; - DMA_InitTypeDef DMA_InitStructure; - RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); - RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); - //USART TX - GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_UART4); - GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; - GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; - GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; - GPIO_Init(GPIOA, &GPIO_InitStruct); + GPIO_InitTypeDef GPIO_InitStruct; + USART_InitTypeDef USART_InitStruct; + DMA_InitTypeDef DMA_InitStructure; + RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); + RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); + //USART TX + GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_UART4); + GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; + GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; + GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; + GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; + GPIO_Init(GPIOA, &GPIO_InitStruct); - //USART RX - GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); - GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10; - GPIO_Init(GPIOA, &GPIO_InitStruct); + //USART RX + GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); + GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10; + GPIO_Init(GPIOA, &GPIO_InitStruct); - USART_InitStruct.USART_BaudRate = 2500000; - USART_InitStruct.USART_WordLength = USART_WordLength_8b; - USART_InitStruct.USART_StopBits = USART_StopBits_1; - USART_InitStruct.USART_Parity = USART_Parity_No; - USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; - USART_InitStruct.USART_Mode = USART_Mode_Rx; - USART_Init(USART1, &USART_InitStruct); - USART_InitStruct.USART_Mode = USART_Mode_Tx; - USART_Init(UART4, &USART_InitStruct); - //USART_HalfDuplexCmd(USART1,ENABLE); + USART_InitStruct.USART_BaudRate = 2500000; + USART_InitStruct.USART_WordLength = USART_WordLength_8b; + USART_InitStruct.USART_StopBits = USART_StopBits_1; + USART_InitStruct.USART_Parity = USART_Parity_No; + USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; + USART_InitStruct.USART_Mode = USART_Mode_Rx; + USART_Init(USART1, &USART_InitStruct); + USART_InitStruct.USART_Mode = USART_Mode_Tx; + USART_Init(UART4, &USART_InitStruct); + //USART_HalfDuplexCmd(USART1,ENABLE); - //USART_InitStruct.USART_Mode = USART_Mode_Tx; - //USART_Init(USART1, &USART_InitStruct); + //USART_InitStruct.USART_Mode = USART_Mode_Tx; + //USART_Init(USART1, &USART_InitStruct); - //USART_Cmd(USART1, ENABLE); - USART_Cmd(USART1, ENABLE); - USART_Cmd(UART4, ENABLE); + //USART_Cmd(USART1, ENABLE); + USART_Cmd(USART1, ENABLE); + USART_Cmd(UART4, ENABLE); - //RX DMA + //RX DMA - DMA_Cmd(DMA2_Stream5, DISABLE); - DMA_DeInit(DMA2_Stream5); + DMA_Cmd(DMA2_Stream5, DISABLE); + DMA_DeInit(DMA2_Stream5); - // DMA2-Config - DMA_InitStructure.DMA_Channel = DMA_Channel_4; - DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART1->DR); - DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&rxbuf; - DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; - DMA_InitStructure.DMA_BufferSize = sizeof(rxbuf); - DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; - DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; - DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; - DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte; - DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; - DMA_InitStructure.DMA_Priority = DMA_Priority_High; - DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; - DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; - DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; - DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; - DMA_Init(DMA2_Stream5, &DMA_InitStructure); + // DMA2-Config + DMA_InitStructure.DMA_Channel = DMA_Channel_4; + DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) & (USART1->DR); + DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&rxbuf; + DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; + DMA_InitStructure.DMA_BufferSize = sizeof(rxbuf); + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; + DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; + DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte; + DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; + DMA_InitStructure.DMA_Priority = DMA_Priority_High; + DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; + DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; + DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; + DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; + DMA_Init(DMA2_Stream5, &DMA_InitStructure); - DMA_Cmd(DMA2_Stream5, ENABLE); + DMA_Cmd(DMA2_Stream5, ENABLE); - USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE); + USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE); - //TX DMA + //TX DMA - DMA_Cmd(DMA1_Stream4, DISABLE); - DMA_DeInit(DMA1_Stream4); + DMA_Cmd(DMA1_Stream4, DISABLE); + DMA_DeInit(DMA1_Stream4); - // DMA2-Config - DMA_InitStructure.DMA_Channel = DMA_Channel_4; - DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(UART4->DR); - DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&txbuf; - DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; - DMA_InitStructure.DMA_BufferSize = sizeof(txbuf); - DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; - DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; - DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; - DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte; - DMA_InitStructure.DMA_Mode = DMA_Priority_Low; - DMA_InitStructure.DMA_Priority = DMA_Priority_High; - DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; - DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; - DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; - DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; - DMA_Init(DMA1_Stream4, &DMA_InitStructure); + // DMA2-Config + DMA_InitStructure.DMA_Channel = DMA_Channel_4; + DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) & (UART4->DR); + DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&txbuf; + DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; + DMA_InitStructure.DMA_BufferSize = sizeof(txbuf); + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; + DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; + DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte; + DMA_InitStructure.DMA_Mode = DMA_Priority_Low; + DMA_InitStructure.DMA_Priority = DMA_Priority_High; + DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; + DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; + DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; + DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; + DMA_Init(DMA1_Stream4, &DMA_InitStructure); - //DMA_Cmd(DMA1_Stream7, ENABLE); + //DMA_Cmd(DMA1_Stream7, ENABLE); - USART_DMACmd(UART4, USART_DMAReq_Tx, ENABLE); - - //tx enable - GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7; - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; - GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; - GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz; - GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; - GPIO_Init(GPIOB, &GPIO_InitStruct); - - GPIO_SetBits(GPIOB, GPIO_Pin_7); - - //generate unit number from 96bit unique chip ID - unit.unit = U_ID[0] ^ U_ID[1] ^ U_ID[2]; - - - - rxpos = 0; - timeout = 1000;//make sure we start in timeout + USART_DMACmd(UART4, USART_DMAReq_Tx, ENABLE); - heap_ptr = memory.heap; + //tx enable + GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7; + GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; + GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz; + GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_Init(GPIOB, &GPIO_InitStruct); - uint16_t input_bits = 8; // this starts at 8 bits = 1 byte for the fault byte - uint16_t output_bits = 0; + GPIO_SetBits(GPIOB, GPIO_Pin_7); - // these are temp toc arrays that the macros will write pointers into. the tocs get copied to main memory after everything else is written in - uint16_t ptoc[32]; - uint16_t gtoc[32]; + //generate unit number from 96bit unique chip ID + unit.unit = U_ID[0] ^ U_ID[1] ^ U_ID[2]; - uint16_t *ptocp = ptoc; - uint16_t *gtocp = gtoc; - process_data_descriptor_t *last_pd; - ADD_PROCESS_VAR(("output_pins", "none", 4, DATA_TYPE_BITS, DATA_DIRECTION_OUTPUT, 0, 1)); metadata(&(pd_table.output_pins), last_pd); - ADD_PROCESS_VAR(("enable", "none", 1, DATA_TYPE_BOOLEAN, DATA_DIRECTION_OUTPUT, 0, 1)); metadata(&(pd_table.enable), last_pd); - ADD_PROCESS_VAR(("pos_cmd", "rad", 32, DATA_TYPE_FLOAT, DATA_DIRECTION_OUTPUT, -INFINITY, INFINITY)); metadata(&(pd_table.pos_cmd), last_pd); - - ADD_PROCESS_VAR(("input_pins", "none", 4, DATA_TYPE_BITS, DATA_DIRECTION_INPUT, -100, 100)); metadata(&(pd_table.input_pins), last_pd); - ADD_PROCESS_VAR(("fault", "none", 1, DATA_TYPE_BOOLEAN, DATA_DIRECTION_INPUT, 0, 1)); metadata(&(pd_table.fault), last_pd); - ADD_PROCESS_VAR(("pos_fb", "rad", 32, DATA_TYPE_FLOAT, DATA_DIRECTION_INPUT, -INFINITY, INFINITY)); metadata(&(pd_table.pos_fb), last_pd); - //globals and modes are not working. https://github.com/LinuxCNC/linuxcnc/blob/2957cc5ad0a463c39fb35c10a0c14909c09a5fb7/src/hal/drivers/mesa-hostmot2/sserial.c#L1516 - // - globals need write support - // - linuxcnc only supports globals of type DATA_TYPE_NONVOL_UNSIGNED or DATA_TYPE_NONVOL_SIGNED - //ADD_GLOBAL_VAR(("swr", "non", 8, DATA_TYPE_NONVOL_UNSIGNED, DATA_DIRECTION_OUTPUT, 0, 0)); + rxpos = 0; + timeout = 1000; //make sure we start in timeout - //ADD_MODE(("foo", 0, 0)); - ADD_MODE(("Position mode", 0, 1)); + heap_ptr = memory.heap; - // automatically create padding pds based on the mod remainder of input/output bits - if (input_bits % 8) ADD_PROCESS_VAR(("padding", "", 8 - (input_bits%8), DATA_TYPE_PAD, DATA_DIRECTION_INPUT, 0, 0)); - if (output_bits % 8) ADD_PROCESS_VAR(("padding", "", 8 - (output_bits%8), DATA_TYPE_PAD, DATA_DIRECTION_OUTPUT, 0, 0)); + uint16_t input_bits = 8; // this starts at 8 bits = 1 byte for the fault byte + uint16_t output_bits = 0; - // now that all the toc entries have been added, write out the tocs to memory and set up the toc pointers + // these are temp toc arrays that the macros will write pointers into. the tocs get copied to main memory after everything else is written in + uint16_t ptoc[32]; + uint16_t gtoc[32]; - //calculate bytes from bits - memory.discovery.input = input_bits >> 3; - memory.discovery.output = output_bits >> 3; + uint16_t *ptocp = ptoc; + uint16_t *gtocp = gtoc; + process_data_descriptor_t *last_pd; - memory.discovery.ptocp = MEMPTR(*heap_ptr); + ADD_PROCESS_VAR(("output_pins", "none", 4, DATA_TYPE_BITS, DATA_DIRECTION_OUTPUT, 0, 1)); + metadata(&(pd_table.output_pins), last_pd); + ADD_PROCESS_VAR(("enable", "none", 1, DATA_TYPE_BOOLEAN, DATA_DIRECTION_OUTPUT, 0, 1)); + metadata(&(pd_table.enable), last_pd); + ADD_PROCESS_VAR(("pos_cmd", "rad", 32, DATA_TYPE_FLOAT, DATA_DIRECTION_OUTPUT, -INFINITY, INFINITY)); + metadata(&(pd_table.pos_cmd), last_pd); - for(uint8_t i = 0; i < ptocp - ptoc; i++) { - *heap_ptr++ = ptoc[i] & 0x00FF; - *heap_ptr++ = (ptoc[i] & 0xFF00) >> 8; - } - // this is the ptoc end marker - *heap_ptr++ = 0x00; - *heap_ptr++ = 0x00; + ADD_PROCESS_VAR(("input_pins", "none", 4, DATA_TYPE_BITS, DATA_DIRECTION_INPUT, -100, 100)); + metadata(&(pd_table.input_pins), last_pd); + ADD_PROCESS_VAR(("fault", "none", 1, DATA_TYPE_BOOLEAN, DATA_DIRECTION_INPUT, 0, 1)); + metadata(&(pd_table.fault), last_pd); + ADD_PROCESS_VAR(("pos_fb", "rad", 32, DATA_TYPE_FLOAT, DATA_DIRECTION_INPUT, -INFINITY, INFINITY)); + metadata(&(pd_table.pos_fb), last_pd); + //globals and modes are not working. https://github.com/LinuxCNC/linuxcnc/blob/2957cc5ad0a463c39fb35c10a0c14909c09a5fb7/src/hal/drivers/mesa-hostmot2/sserial.c#L1516 + // - globals need write support + // - linuxcnc only supports globals of type DATA_TYPE_NONVOL_UNSIGNED or DATA_TYPE_NONVOL_SIGNED + //ADD_GLOBAL_VAR(("swr", "non", 8, DATA_TYPE_NONVOL_UNSIGNED, DATA_DIRECTION_OUTPUT, 0, 0)); - memory.discovery.gtocp = MEMPTR(*heap_ptr); + //ADD_MODE(("foo", 0, 0)); + ADD_MODE(("Position mode", 0, 1)); - for(uint8_t i = 0; i < gtocp - gtoc; i++) { - *heap_ptr++ = gtoc[i] & 0x00FF; - *heap_ptr++ = (gtoc[i] & 0xFF00) >> 8; - } - // this is the gtoc end marker - *heap_ptr++ = 0x00; - *heap_ptr++ = 0x00; + // automatically create padding pds based on the mod remainder of input/output bits + if(input_bits % 8) + ADD_PROCESS_VAR(("padding", "", 8 - (input_bits % 8), DATA_TYPE_PAD, DATA_DIRECTION_INPUT, 0, 0)); + if(output_bits % 8) + ADD_PROCESS_VAR(("padding", "", 8 - (output_bits % 8), DATA_TYPE_PAD, DATA_DIRECTION_OUTPUT, 0, 0)); + + // now that all the toc entries have been added, write out the tocs to memory and set up the toc pointers + + //calculate bytes from bits + memory.discovery.input = input_bits >> 3; + memory.discovery.output = output_bits >> 3; + + memory.discovery.ptocp = MEMPTR(*heap_ptr); + + for(uint8_t i = 0; i < ptocp - ptoc; i++) { + *heap_ptr++ = ptoc[i] & 0x00FF; + *heap_ptr++ = (ptoc[i] & 0xFF00) >> 8; + } + // this is the ptoc end marker + *heap_ptr++ = 0x00; + *heap_ptr++ = 0x00; + + memory.discovery.gtocp = MEMPTR(*heap_ptr); + + for(uint8_t i = 0; i < gtocp - gtoc; i++) { + *heap_ptr++ = gtoc[i] & 0x00FF; + *heap_ptr++ = (gtoc[i] & 0xFF00) >> 8; + } + // this is the gtoc end marker + *heap_ptr++ = 0x00; + *heap_ptr++ = 0x00; } void process_data_rpc(uint8_t fault, volatile uint8_t *input, volatile uint8_t *output) { - uint16_t *ptocp = (uint16_t *)(memory.bytes + memory.discovery.ptocp); - uint32_t local_rxpos = rxpos; - *(input++) = fault; - *input = 0x00; + uint16_t *ptocp = (uint16_t *)(memory.bytes + memory.discovery.ptocp); + uint32_t local_rxpos = rxpos; + *(input++) = fault; + *input = 0x00; - // data needs to be packed and unpacked based on its type and size - // input is a pointer to the data that gets sent back to the host - // need a bit pointer to keep track of partials + // data needs to be packed and unpacked based on its type and size + // input is a pointer to the data that gets sent back to the host + // need a bit pointer to keep track of partials - uint8_t output_bit_ptr = 0; - uint8_t input_bit_ptr = 0; + uint8_t output_bit_ptr = 0; + uint8_t input_bit_ptr = 0; - while(*ptocp != 0x0000) { - process_data_descriptor_t *pd = (process_data_descriptor_t *)(memory.bytes + *ptocp++); - - if (IS_INPUT(pd)) { - uint16_t data_addr = pd->data_addr; - uint8_t data_size = pd->data_size; - uint8_t data_bit_ptr = 0; - while(data_size > 0) { - uint8_t bits_to_pack = data_size < BITSLEFT(input_bit_ptr) ? data_size : BITSLEFT(input_bit_ptr); - if (BITSLEFT(data_bit_ptr) < bits_to_pack) { bits_to_pack = BITSLEFT(data_bit_ptr); } + while(*ptocp != 0x0000) { + process_data_descriptor_t *pd = (process_data_descriptor_t *)(memory.bytes + *ptocp++); - uint8_t mask = ((1<data_addr; + uint8_t data_size = pd->data_size; + uint8_t data_bit_ptr = 0; + while(data_size > 0) { + uint8_t bits_to_pack = data_size < BITSLEFT(input_bit_ptr) ? data_size : BITSLEFT(input_bit_ptr); + if(BITSLEFT(data_bit_ptr) < bits_to_pack) { + bits_to_pack = BITSLEFT(data_bit_ptr); + } - *input |= ((MEMU8(data_addr) & mask) >> data_bit_ptr) << input_bit_ptr; + uint8_t mask = ((1 << bits_to_pack) - 1) << (data_bit_ptr); - input_bit_ptr += bits_to_pack; - data_bit_ptr += bits_to_pack; - data_size -= bits_to_pack; - if((input_bit_ptr %= 8) == 0) *(++input) = 0x00; // make sure we clear the input buffer whenever we increment bytes - if((data_bit_ptr %= 8) == 0) data_addr++; - } + *input |= ((MEMU8(data_addr) & mask) >> data_bit_ptr) << input_bit_ptr; + + input_bit_ptr += bits_to_pack; + data_bit_ptr += bits_to_pack; + data_size -= bits_to_pack; + if((input_bit_ptr %= 8) == 0) + *(++input) = 0x00; // make sure we clear the input buffer whenever we increment bytes + if((data_bit_ptr %= 8) == 0) + data_addr++; } - if (IS_OUTPUT(pd)) { - uint16_t data_addr = pd->data_addr; - uint8_t data_size = pd->data_size; + } + if(IS_OUTPUT(pd)) { + uint16_t data_addr = pd->data_addr; + uint8_t data_size = pd->data_size; - uint8_t val_bits_remaining = 8; - uint8_t val = 0x00; + uint8_t val_bits_remaining = 8; + uint8_t val = 0x00; - while(data_size > 0) { - // the number of bits to unpack this iteration is the number of bits remaining in the pd, or the number of bits remaining in the output byte, - // whichever is smaller. Then, it can be even smaller if we have less room in the current val. + while(data_size > 0) { + // the number of bits to unpack this iteration is the number of bits remaining in the pd, or the number of bits remaining in the output byte, + // whichever is smaller. Then, it can be even smaller if we have less room in the current val. - uint8_t bits_to_unpack = data_size < BITSLEFT(output_bit_ptr) ? data_size : BITSLEFT(output_bit_ptr); - if (val_bits_remaining < bits_to_unpack) { bits_to_unpack = val_bits_remaining; } + uint8_t bits_to_unpack = data_size < BITSLEFT(output_bit_ptr) ? data_size : BITSLEFT(output_bit_ptr); + if(val_bits_remaining < bits_to_unpack) { + bits_to_unpack = val_bits_remaining; + } - // create a bitmask the width of the bits to read, shifted to the position in the output byte that we're pointing to - uint8_t mask = ((1<> (output_bit_ptr)) << (8-val_bits_remaining); + // val is what we get when we mask off output and then shift it to the proper place. + val = val | ((rxbuf[(local_rxpos + 1) % sizeof(rxbuf)] & mask) >> (output_bit_ptr)) << (8 - val_bits_remaining); - val_bits_remaining -= bits_to_unpack; - data_size -= bits_to_unpack; - output_bit_ptr += bits_to_unpack; - // rxpos is a ringbuf and wraps around - // note: this replaces the output argument - if((output_bit_ptr %= 8) == 0){ - local_rxpos++; - local_rxpos = local_rxpos % sizeof(rxbuf); - } - + val_bits_remaining -= bits_to_unpack; + data_size -= bits_to_unpack; + output_bit_ptr += bits_to_unpack; + // rxpos is a ringbuf and wraps around + // note: this replaces the output argument + if((output_bit_ptr %= 8) == 0) { + local_rxpos++; + local_rxpos = local_rxpos % sizeof(rxbuf); + } - if(val_bits_remaining == 0 || data_size == 0) { - MEMU8(data_addr++) = val; - val_bits_remaining = 8; - val = 0x00; - } - } - // now we've finished unpacking it and storing it in memory, but we have to fix up the high bits if it wasn't a byte-aligned datasize. - // for instance, if we receive 0xFFF in a 12 bit field, that is a negative number, but we stored it as 0x0FFF in memory. - // strategy is to set the most significant n bits of the MSB to the most significant bit of the output value, iff the pd is defined as signed. - if (SIGNED(pd) && pd->data_size % 8 != 0) { - //printf("in output fixup. data_addr %h data_size %i num_bytes %i\n", pd->data_addr, pd->data_size, NUM_BYTES(pd->data_size)); - uint8_t msb_addr = pd->data_addr + NUM_BYTES(pd->data_size) - 1; - //printf("in output fixup. MSB (at %h): %h\n", msb_addr, MEMU8(msb_addr)); - - // these two masks use data_size%8, this is the number of bits in the most significant byte, and since we tested for %8!=0 above, we know it's a partial byte - if(MEMU8(msb_addr) & 1<<(pd->data_size%8 - 1)) { // this test uses a mask that is 1 in the most significant bit position, we only need to fixup the val if it's 1 (ie negative) - // this mask is all the unused high bits set - uint8_t mask = 0xFF ^ ((1<data_size%8) - 1); - //printf("applying mask: %h\n", mask); - MEMU8(msb_addr) |= mask; - } - //printf("fixed up val: %h\n", MEMU8(msb_addr)); + if(val_bits_remaining == 0 || data_size == 0) { + MEMU8(data_addr++) = val; + val_bits_remaining = 8; + val = 0x00; + } } - } -} + // now we've finished unpacking it and storing it in memory, but we have to fix up the high bits if it wasn't a byte-aligned datasize. + // for instance, if we receive 0xFFF in a 12 bit field, that is a negative number, but we stored it as 0x0FFF in memory. + // strategy is to set the most significant n bits of the MSB to the most significant bit of the output value, iff the pd is defined as signed. + if(SIGNED(pd) && pd->data_size % 8 != 0) { + //printf("in output fixup. data_addr %h data_size %i num_bytes %i\n", pd->data_addr, pd->data_size, NUM_BYTES(pd->data_size)); + uint8_t msb_addr = pd->data_addr + NUM_BYTES(pd->data_size) - 1; + //printf("in output fixup. MSB (at %h): %h\n", msb_addr, MEMU8(msb_addr)); + + // these two masks use data_size%8, this is the number of bits in the most significant byte, and since we tested for %8!=0 above, we know it's a partial byte + if(MEMU8(msb_addr) & 1 << (pd->data_size % 8 - 1)) { // this test uses a mask that is 1 in the most significant bit position, we only need to fixup the val if it's 1 (ie negative) + // this mask is all the unused high bits set + uint8_t mask = 0xFF ^ ((1 << pd->data_size % 8) - 1); + //printf("applying mask: %h\n", mask); + MEMU8(msb_addr) |= mask; + } + + //printf("fixed up val: %h\n", MEMU8(msb_addr)); + } + } + } } //TODO: div by zero float scale_out(pd_metadata_t pd, int32_t val) { - return val * pd.range / (float)pd.bitmax; -} + return val * pd.range / (float)pd.bitmax; +} //TODO: div by zero int32_t scale_in(pd_metadata_t pd, float val) { - return CLAMP(val, pd.ptr->param_min, pd.ptr->param_max) * pd.bitmax / pd.range; + return CLAMP(val, pd.ptr->param_min, pd.ptr->param_max) * pd.bitmax / pd.range; } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - // struct res_ctx_t * ctx = (struct res_ctx_t *)ctx_ptr; - struct sserial_pin_ctx_t * pins = (struct sserial_pin_ctx_t *)pin_ptr; - // update all hal pins with values from their associated pds +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct res_ctx_t * ctx = (struct res_ctx_t *)ctx_ptr; + struct sserial_pin_ctx_t *pins = (struct sserial_pin_ctx_t *)pin_ptr; + // update all hal pins with values from their associated pds - // uint16_t foo = MEMU16(pd_table.pos_cmd.ptr->data_addr); - // scale_out(pd_table.pos_cmd, *(int16_t*)&foo); - PIN(pos_cmd) = MEMFLOAT(pd_table.pos_cmd.ptr->data_addr); - uint8_t outpins = MEMU8(pd_table.output_pins.ptr->data_addr); - PIN(out0) = outpins >> 0 & 1 ? 1.0 : 0.0; - PIN(out1) = outpins >> 1 & 1 ? 1.0 : 0.0; - PIN(out2) = outpins >> 2 & 1 ? 1.0 : 0.0; - PIN(out3) = outpins >> 3 & 1 ? 1.0 : 0.0; - uint8_t enable = MEMU8(pd_table.enable.ptr->data_addr); - PIN(enable) = enable<<0 & 1 ? 1.0 : 0.0; - //TODO: how to handle bidirectional pins properly? + // uint16_t foo = MEMU16(pd_table.pos_cmd.ptr->data_addr); + // scale_out(pd_table.pos_cmd, *(int16_t*)&foo); + PIN(pos_cmd) = MEMFLOAT(pd_table.pos_cmd.ptr->data_addr); + uint8_t outpins = MEMU8(pd_table.output_pins.ptr->data_addr); + PIN(out0) = outpins >> 0 & 1 ? 1.0 : 0.0; + PIN(out1) = outpins >> 1 & 1 ? 1.0 : 0.0; + PIN(out2) = outpins >> 2 & 1 ? 1.0 : 0.0; + PIN(out3) = outpins >> 3 & 1 ? 1.0 : 0.0; + uint8_t enable = MEMU8(pd_table.enable.ptr->data_addr); + PIN(enable) = enable << 0 & 1 ? 1.0 : 0.0; + //TODO: how to handle bidirectional pins properly? - //*((uint16_t *)&(memory.bytes[pd_table.pos_fb.ptr->data_addr])) = (uint16_t)scale_in(pd_table.pos_fb, PIN(pos_fb)); - - //uint32_t tmp = MEMU32(ptr);*((float*)&tmp);})) - - //MEMFLOAT(pd_table.pos_fb.ptr->data_addr) = PIN(pos_fb); - *((float *)&(memory.bytes[pd_table.pos_fb.ptr->data_addr])) = PIN(pos_fb); - - MEMU8(pd_table.fault.ptr->data_addr) = BOOLPIN(fault); - MEMU8(pd_table.input_pins.ptr->data_addr) = BOOLPIN(in0)<<0 | BOOLPIN(in1)<<1 | BOOLPIN(in2)<<2 | BOOLPIN(in3)<<3; + //*((uint16_t *)&(memory.bytes[pd_table.pos_fb.ptr->data_addr])) = (uint16_t)scale_in(pd_table.pos_fb, PIN(pos_fb)); + //uint32_t tmp = MEMU32(ptr);*((float*)&tmp);})) + + //MEMFLOAT(pd_table.pos_fb.ptr->data_addr) = PIN(pos_fb); + *((float *)&(memory.bytes[pd_table.pos_fb.ptr->data_addr])) = PIN(pos_fb); + + MEMU8(pd_table.fault.ptr->data_addr) = BOOLPIN(fault); + MEMU8(pd_table.input_pins.ptr->data_addr) = BOOLPIN(in0) << 0 | BOOLPIN(in1) << 1 | BOOLPIN(in2) << 2 | BOOLPIN(in3) << 3; } -static void frt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ -// struct res_ctx_t * ctx = (struct res_ctx_t *)ctx_ptr; -struct sserial_pin_ctx_t * pins = (struct sserial_pin_ctx_t *)pin_ptr; - -for(int j = 0;j<2;j++){ - //next received packet will be written to bufferpos - bufferpos = sizeof(rxbuf) - DMA_GetCurrDataCounter(DMA2_Stream5); - //how many packets we have the the rx buffer for processing - available = (bufferpos - rxpos + sizeof(rxbuf)) % sizeof(rxbuf); - - if (available >= 1) { +static void frt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct res_ctx_t * ctx = (struct res_ctx_t *)ctx_ptr; + struct sserial_pin_ctx_t *pins = (struct sserial_pin_ctx_t *)pin_ptr; + + for(int j = 0; j < 2; j++) { + //next received packet will be written to bufferpos + bufferpos = sizeof(rxbuf) - DMA_GetCurrDataCounter(DMA2_Stream5); + //how many packets we have the the rx buffer for processing + available = (bufferpos - rxpos + sizeof(rxbuf)) % sizeof(rxbuf); + + if(available >= 1) { lbp.byte = rxbuf[rxpos]; - if (lbp.ct == CT_LOCAL && lbp.wr == 0){ //local read, cmd+crc = 2b - timeout = 0; - if(available >= 2){ - switch(lbp.byte) { - case LBPCookieCMD: - txbuf[0] = LBPCookie; - break; - case LBPStatusCMD: //TODO: return status - txbuf[0] = 0x00; - break; - case LBPCardName0Cmd ... LBPCardName3Cmd: - txbuf[0] = name[lbp.byte-LBPCardName0Cmd]; - break; - default: //TODO: handle unknown command condition - txbuf[0] = 0x00; + if(lbp.ct == CT_LOCAL && lbp.wr == 0) { //local read, cmd+crc = 2b + timeout = 0; + if(available >= 2) { + switch(lbp.byte) { + case LBPCookieCMD: + txbuf[0] = LBPCookie; + break; + case LBPStatusCMD: //TODO: return status + txbuf[0] = 0x00; + break; + case LBPCardName0Cmd ... LBPCardName3Cmd: + txbuf[0] = name[lbp.byte - LBPCardName0Cmd]; + break; + default: //TODO: handle unknown command condition + txbuf[0] = 0x00; + } + send(1, 1); + rxpos += 2; + } else { + continue; + } + } else if(lbp.ct == CT_LOCAL && lbp.wr == 1) { //local write, cmd+data+crc = 3b + timeout = 0; + //0xFF and 0xFC are not followed by crc + if(rxbuf[rxpos] == 0xFF) { + // reset parser + rxpos += 1; + } else if(rxbuf[rxpos] == 0xFC) { + // todo + rxpos += 1; + } else if(available >= 3) { //writes do not expect crc in reply + txbuf[0] = 0x00; + send(1, 0); + rxpos += 3; + } else { + continue; + } + } else if(lbp.ct == CT_RPC) { //RPC TODO: check for ct should not required for rpc + timeout = 0; + if(lbp.byte == UnitNumberRPC && available >= 2) { //unit number, cmd+crc = 2b + txbuf[0] = unit.byte[0]; + txbuf[1] = unit.byte[1]; + txbuf[2] = unit.byte[2]; + txbuf[3] = unit.byte[3]; + send(4, 1); + rxpos += 2; + } else if(lbp.byte == DiscoveryRPC && available >= 2) { //discovery, cmd+crc = 2b + memcpy((void *)txbuf, ((uint8_t *)&memory.discovery), sizeof(memory.discovery)); + send(sizeof(memory.discovery), 1); + rxpos += 2; + } else if(lbp.byte == ProcessDataRPC && available >= memory.discovery.output + 2) { //process data, requires cmd+output bytes+crc + //TODO: maybe packing and unpacking can be moved to RT + process_data_rpc(0x00, txbuf, &(rxbuf[rxpos + 1])); // todo: send a proper fault byte? + send(memory.discovery.input, 1); + //uint16_t foo = MEMU16(pd_table.pos_cmd.ptr->data_addr); + //float p = scale_out(pd_table.pos_cmd, *(int16_t*)&foo); + float p = MEMFLOAT(pd_table.pos_cmd.ptr->data_addr); + PIN(pos_cmd_d) = minus(p, last_pos_cmd) * 1000.0f; //TODO: only valid for 1khz servo thread + last_pos_cmd = p; + //we cannot send the reply based on crc, as this causes timeouts + //instead we should check for errors in RT + if(!crc_reuest(memory.discovery.output + 1)) { + PIN(crc_error) + ++; + } + rxpos += memory.discovery.output + 2; + } else { + continue; + } + } else if(lbp.ct == CT_RW && lbp.wr == 0) { //read TODO: implement write. + timeout = 0; + if(available >= 2) { + if(lbp.as == 1) { //address included in command = cmd+addr+addr+crc + if(available >= 4) { + address = rxbuf[(rxpos + 1) % sizeof(rxbuf)] + (rxbuf[(rxpos + 2) % sizeof(rxbuf)] << 8); + rxpos += 4; + } else { + continue; } - send(1,1); + } else { //address not included in command = cmd+crc rxpos += 2; - }else{ - continue; - } - }else if(lbp.ct == CT_LOCAL && lbp.wr == 1){//local write, cmd+data+crc = 3b - timeout = 0; - //0xFF and 0xFC are not followed by crc - if(rxbuf[rxpos] == 0xFF){ - // reset parser - rxpos += 1; - }else if(rxbuf[rxpos] == 0xFC){ - // todo - rxpos += 1; - }else if(available >= 3){//writes do not expect crc in reply - txbuf[0] = 0x00; - send(1,0); - rxpos += 3; - }else{ - continue; - } - }else if(lbp.ct == CT_RPC){//RPC TODO: check for ct should not required for rpc - timeout = 0; - if(lbp.byte == UnitNumberRPC && available >= 2){//unit number, cmd+crc = 2b - txbuf[0] = unit.byte[0]; - txbuf[1] = unit.byte[1]; - txbuf[2] = unit.byte[2]; - txbuf[3] = unit.byte[3]; - send(4,1); - rxpos += 2; - }else if(lbp.byte == DiscoveryRPC && available >= 2){//discovery, cmd+crc = 2b - memcpy((void*)txbuf,((uint8_t*)&memory.discovery),sizeof(memory.discovery)); - send(sizeof(memory.discovery),1); - rxpos += 2; - }else if(lbp.byte == ProcessDataRPC && available >= memory.discovery.output + 2){//process data, requires cmd+output bytes+crc - //TODO: maybe packing and unpacking can be moved to RT - process_data_rpc(0x00, txbuf, &(rxbuf[rxpos+1])); // todo: send a proper fault byte? - send(memory.discovery.input,1); - //uint16_t foo = MEMU16(pd_table.pos_cmd.ptr->data_addr); - //float p = scale_out(pd_table.pos_cmd, *(int16_t*)&foo); - float p = MEMFLOAT(pd_table.pos_cmd.ptr->data_addr); - PIN(pos_cmd_d) = minus(p,last_pos_cmd)*1000.0f;//TODO: only valid for 1khz servo thread - last_pos_cmd = p; - //we cannot send the reply based on crc, as this causes timeouts - //instead we should check for errors in RT - if(!crc_reuest(memory.discovery.output + 1)){ - PIN(crc_error)++; - } - rxpos += memory.discovery.output + 2; - }else{ - continue; - } - }else if (lbp.ct == CT_RW && lbp.wr == 0){ //read TODO: implement write. - timeout = 0; - if(available >= 2){ - if (lbp.as == 1){ //address included in command = cmd+addr+addr+crc - if (available >= 4) { - address = rxbuf[(rxpos+1)%sizeof(rxbuf)] + (rxbuf[(rxpos+2)%sizeof(rxbuf)]<<8); - rxpos += 4; - }else{ - continue; - } - }else{ //address not included in command = cmd+crc - rxpos += 2; - } - //TODO: check if address is valid - memcpy((void*)txbuf,&memory.bytes[address],(1< PIN(timeout)){//TODO: clamping + timeout++; + if(timeout > PIN(timeout)) { //TODO: clamping PIN(connected) = 0; - PIN(error) = 1; - rxpos = bufferpos; - }else{ + PIN(error) = 1; + rxpos = bufferpos; + } else { PIN(connected) = 1; - PIN(error) = 0; - } - rxpos = rxpos % sizeof(rxbuf); -} + PIN(error) = 0; + } + rxpos = rxpos % sizeof(rxbuf); + } } @@ -639,16 +652,16 @@ for(int j = 0;j<2;j++){ // ); const hal_comp_t sserial_comp_struct = { -.name = "sserial", -.nrt = 0, -.rt = rt_func, -.frt = frt_func, -.nrt_init = 0, -.hw_init = hw_init, -.rt_start = 0, -.frt_start = 0, -.rt_stop = 0, -.frt_stop = 0, -.ctx_size = sizeof(struct sserial_ctx_t), -.pin_count = sizeof(struct sserial_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "sserial", + .nrt = 0, + .rt = rt_func, + .frt = frt_func, + .nrt_init = 0, + .hw_init = hw_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct sserial_ctx_t), + .pin_count = sizeof(struct sserial_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/src/main.c b/src/main.c index 427167cd..0c3732c0 100644 --- a/src/main.c +++ b/src/main.c @@ -28,155 +28,153 @@ #include "main.h" #include "commands.h" -uint32_t hal_get_systick_value(){ - return(SysTick->VAL); +uint32_t hal_get_systick_value() { + return (SysTick->VAL); } -uint32_t hal_get_systick_reload(){ - return(SysTick->LOAD); +uint32_t hal_get_systick_reload() { + return (SysTick->LOAD); } -uint32_t hal_get_systick_freq(){ - return(RCC_Clocks.HCLK_Frequency); +uint32_t hal_get_systick_freq() { + return (RCC_Clocks.HCLK_Frequency); } volatile uint64_t systime = 0; -void SysTick_Handler(void) -{ +void SysTick_Handler(void) { systime++; } //20kHz -void TIM_SLAVE_HANDLER(void){ - TIM_ClearITPendingBit(TIM_SLAVE,TIM_IT_Update); - hal_run_frt(); - if(TIM_GetITStatus(TIM_SLAVE,TIM_IT_Update) == SET){ - hal_stop(); - hal.hal_state = FRT_TOO_LONG; - } +void TIM_SLAVE_HANDLER(void) { + TIM_ClearITPendingBit(TIM_SLAVE, TIM_IT_Update); + hal_run_frt(); + if(TIM_GetITStatus(TIM_SLAVE, TIM_IT_Update) == SET) { + hal_stop(); + hal.hal_state = FRT_TOO_LONG; + } } //5 kHz interrupt for hal. at this point all ADCs have been sampled, //see setup_res() in setup.c if you are interested in the magic behind this. -void DMA2_Stream0_IRQHandler(void){ - GPIOD->BSRRL |= GPIO_Pin_1; - DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_TCIF0); - hal_run_rt(); - if(DMA_GetITStatus(DMA2_Stream0, DMA_IT_TCIF0) == SET){ - hal_stop(); - hal.hal_state = RT_TOO_LONG; - } - GPIOD->BSRRH |= GPIO_Pin_1; +void DMA2_Stream0_IRQHandler(void) { + GPIOD->BSRRL |= GPIO_Pin_1; + DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_TCIF0); + hal_run_rt(); + if(DMA_GetITStatus(DMA2_Stream0, DMA_IT_TCIF0) == SET) { + hal_stop(); + hal.hal_state = RT_TOO_LONG; + } + GPIOD->BSRRH |= GPIO_Pin_1; } -void bootloader(char * ptr){ - *((unsigned long *)0x2001C000) = 0xDEADBEEF;//set bootloader trigger +void bootloader(char *ptr) { + *((unsigned long *)0x2001C000) = 0xDEADBEEF; //set bootloader trigger NVIC_SystemReset(); } COMMAND("bootloader", bootloader, "enter bootloader"); -void nv_reset(char * ptr){ +void nv_reset(char *ptr) { NVIC_SystemReset(); } COMMAND("reset", nv_reset, "reset STMBL"); -char config[15*1024]; -const char* config_ro = (char*)0x08008000; +char config[15 * 1024]; +const char *config_ro = (char *)0x08008000; -void confcrc(char * ptr){ - uint32_t len = strnlen(config,sizeof(config)-1); - CRC_ResetDR(); - uint32_t crc = CRC_CalcBlockCRC((uint32_t *) config, len / 4); - for(int i = 0;iVTOR = (uint32_t)&g_pfnVectors; +int main(void) { + // Relocate interrupt vectors + // + extern void *g_pfnVectors; + SCB->VTOR = (uint32_t)&g_pfnVectors; - setup(); - hal_init(0.0002, 0.00005); - // hal load comps - load_comp(comp_by_name("term")); - hal_parse("flashloadconf"); - hal_parse("loadconf"); - hal_parse("start"); - - TIM_Cmd(TIM_MASTER, ENABLE); - TIM_ITConfig(TIM_SLAVE, TIM_IT_Update, ENABLE); + setup(); + hal_init(0.0002, 0.00005); + // hal load comps + load_comp(comp_by_name("term")); + hal_parse("flashloadconf"); + hal_parse("loadconf"); + hal_parse("start"); - while(1)//run non realtime stuff - { - hal_run_nrt(); - //cdc_poll(); - Wait(1); - } + TIM_Cmd(TIM_MASTER, ENABLE); + TIM_ITConfig(TIM_SLAVE, TIM_IT_Update, ENABLE); + + while(1) //run non realtime stuff + { + hal_run_nrt(); + //cdc_poll(); + Wait(1); + } } -void Wait(uint32_t ms){ - uint64_t t = systime + ms; - while(t >= systime){ - } +void Wait(uint32_t ms) { + uint64_t t = systime + ms; + while(t >= systime) { + } } diff --git a/src/old/eeprom.c b/src/old/eeprom.c old mode 100755 new mode 100644 index 3437ec42..81c6f58d --- a/src/old/eeprom.c +++ b/src/old/eeprom.c @@ -17,11 +17,11 @@ * *

© COPYRIGHT 2011 STMicroelectronics

****************************************************************************** - */ + */ /** @addtogroup EEPROM_Emulation * @{ - */ + */ /* Includes ------------------------------------------------------------------*/ #include "eeprom.h" @@ -52,84 +52,71 @@ static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data); * @retval - Flash error code: on write Flash error * - FLASH_COMPLETE: on success */ -uint16_t EE_Init(void) -{ +uint16_t EE_Init(void) { uint16_t PageStatus0 = 6, PageStatus1 = 6; - uint16_t VarIdx = 0; + uint16_t VarIdx = 0; uint16_t EepromStatus = 0, ReadStatus = 0; int16_t x = -1; - uint16_t FlashStatus; + uint16_t FlashStatus; /* Get Page0 status */ - PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS); + PageStatus0 = (*(__IO uint16_t *)PAGE0_BASE_ADDRESS); /* Get Page1 status */ - PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS); + PageStatus1 = (*(__IO uint16_t *)PAGE1_BASE_ADDRESS); /* Check for invalid header states and repair if necessary */ - switch (PageStatus0) - { + switch(PageStatus0) { case ERASED: - if (PageStatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */ - { - /* Erase Page0 */ - FlashStatus = FLASH_EraseSector(PAGE0_ID,VOLTAGE_RANGE); - /* If erase operation was failed, a Flash error code is returned */ - if (FlashStatus != FLASH_COMPLETE) - { - return FlashStatus; - } - } - else if (PageStatus1 == RECEIVE_DATA) /* Page0 erased, Page1 receive */ + if(PageStatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */ { /* Erase Page0 */ FlashStatus = FLASH_EraseSector(PAGE0_ID, VOLTAGE_RANGE); /* If erase operation was failed, a Flash error code is returned */ - if (FlashStatus != FLASH_COMPLETE) - { + if(FlashStatus != FLASH_COMPLETE) { + return FlashStatus; + } + } else if(PageStatus1 == RECEIVE_DATA) /* Page0 erased, Page1 receive */ + { + /* Erase Page0 */ + FlashStatus = FLASH_EraseSector(PAGE0_ID, VOLTAGE_RANGE); + /* If erase operation was failed, a Flash error code is returned */ + if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Mark Page1 as valid */ FlashStatus = FLASH_ProgramHalfWord(PAGE1_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ - if (FlashStatus != FLASH_COMPLETE) - { + if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } - } - else /* First EEPROM access (Page0&1 are erased) or invalid state -> format EEPROM */ + } else /* First EEPROM access (Page0&1 are erased) or invalid state -> format EEPROM */ { /* Erase both Page0 and Page1 and set Page0 as valid page */ FlashStatus = EE_Format(); /* If erase/program operation was failed, a Flash error code is returned */ - if (FlashStatus != FLASH_COMPLETE) - { + if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } } break; case RECEIVE_DATA: - if (PageStatus1 == VALID_PAGE) /* Page0 receive, Page1 valid */ + if(PageStatus1 == VALID_PAGE) /* Page0 receive, Page1 valid */ { /* Transfer data from Page1 to Page0 */ - for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) - { - if (( *(__IO uint16_t*)(PAGE0_BASE_ADDRESS + 6)) == VarIdx) - { + for(VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) { + if((*(__IO uint16_t *)(PAGE0_BASE_ADDRESS + 6)) == VarIdx) { x = VarIdx; } - if (VarIdx != x) - { + if(VarIdx != x) { /* Read the last variables' updates */ ReadStatus = EE_ReadVariable(VarIdx, &DataVar); /* In case variable corresponding to the virtual address was found */ - if (ReadStatus != 0x1) - { + if(ReadStatus != 0x1) { /* Transfer the variable to the Page0 */ EepromStatus = EE_VerifyPageFullWriteVariable(VarIdx, DataVar); /* If program operation was failed, a Flash error code is returned */ - if (EepromStatus != FLASH_COMPLETE) - { + if(EepromStatus != FLASH_COMPLETE) { return EepromStatus; } } @@ -138,89 +125,73 @@ uint16_t EE_Init(void) /* Mark Page0 as valid */ FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ - if (FlashStatus != FLASH_COMPLETE) - { + if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Erase Page1 */ FlashStatus = FLASH_EraseSector(PAGE1_ID, VOLTAGE_RANGE); /* If erase operation was failed, a Flash error code is returned */ - if (FlashStatus != FLASH_COMPLETE) - { + if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } - } - else if (PageStatus1 == ERASED) /* Page0 receive, Page1 erased */ + } else if(PageStatus1 == ERASED) /* Page0 receive, Page1 erased */ { /* Erase Page1 */ FlashStatus = FLASH_EraseSector(PAGE1_ID, VOLTAGE_RANGE); /* If erase operation was failed, a Flash error code is returned */ - if (FlashStatus != FLASH_COMPLETE) - { + if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Mark Page0 as valid */ FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ - if (FlashStatus != FLASH_COMPLETE) - { + if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } - } - else /* Invalid state -> format eeprom */ + } else /* Invalid state -> format eeprom */ { /* Erase both Page0 and Page1 and set Page0 as valid page */ FlashStatus = EE_Format(); /* If erase/program operation was failed, a Flash error code is returned */ - if (FlashStatus != FLASH_COMPLETE) - { + if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } } break; case VALID_PAGE: - if (PageStatus1 == VALID_PAGE) /* Invalid state -> format eeprom */ + if(PageStatus1 == VALID_PAGE) /* Invalid state -> format eeprom */ { /* Erase both Page0 and Page1 and set Page0 as valid page */ FlashStatus = EE_Format(); /* If erase/program operation was failed, a Flash error code is returned */ - if (FlashStatus != FLASH_COMPLETE) - { + if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } - } - else if (PageStatus1 == ERASED) /* Page0 valid, Page1 erased */ + } else if(PageStatus1 == ERASED) /* Page0 valid, Page1 erased */ { /* Erase Page1 */ FlashStatus = FLASH_EraseSector(PAGE1_ID, VOLTAGE_RANGE); /* If erase operation was failed, a Flash error code is returned */ - if (FlashStatus != FLASH_COMPLETE) - { + if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } - } - else /* Page0 valid, Page1 receive */ + } else /* Page0 valid, Page1 receive */ { /* Transfer data from Page0 to Page1 */ - for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) - { - if ((*(__IO uint16_t*)(PAGE1_BASE_ADDRESS + 6)) == VarIdx) - { + for(VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) { + if((*(__IO uint16_t *)(PAGE1_BASE_ADDRESS + 6)) == VarIdx) { x = VarIdx; } - if (VarIdx != x) - { + if(VarIdx != x) { /* Read the last variables' updates */ ReadStatus = EE_ReadVariable(VarIdx, &DataVar); /* In case variable corresponding to the virtual address was found */ - if (ReadStatus != 0x1) - { + if(ReadStatus != 0x1) { /* Transfer the variable to the Page1 */ EepromStatus = EE_VerifyPageFullWriteVariable(VarIdx, DataVar); /* If program operation was failed, a Flash error code is returned */ - if (EepromStatus != FLASH_COMPLETE) - { + if(EepromStatus != FLASH_COMPLETE) { return EepromStatus; } } @@ -229,26 +200,23 @@ uint16_t EE_Init(void) /* Mark Page1 as valid */ FlashStatus = FLASH_ProgramHalfWord(PAGE1_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ - if (FlashStatus != FLASH_COMPLETE) - { + if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Erase Page0 */ FlashStatus = FLASH_EraseSector(PAGE0_ID, VOLTAGE_RANGE); /* If erase operation was failed, a Flash error code is returned */ - if (FlashStatus != FLASH_COMPLETE) - { + if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } } break; - default: /* Any other state -> format eeprom */ + default: /* Any other state -> format eeprom */ /* Erase both Page0 and Page1 and set Page0 as valid page */ FlashStatus = EE_Format(); /* If erase/program operation was failed, a Flash error code is returned */ - if (FlashStatus != FLASH_COMPLETE) - { + if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } break; @@ -267,9 +235,8 @@ uint16_t EE_Init(void) * - 1: if the variable was not found * - NO_VALID_PAGE: if no valid page was found. */ -uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data) -{ - uint16_t ValidPage = PAGE0; +uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t *Data) { + uint16_t ValidPage = PAGE0; uint16_t AddressValue = 0x5555, ReadStatus = 1; uint32_t Address = EEPROM_START_ADDRESS, PageStartAddress = EEPROM_START_ADDRESS; @@ -277,9 +244,8 @@ uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data) ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE); /* Check if there is no valid page */ - if (ValidPage == NO_VALID_PAGE) - { - return NO_VALID_PAGE; + if(ValidPage == NO_VALID_PAGE) { + return NO_VALID_PAGE; } /* Get the valid Page start Address */ @@ -289,24 +255,20 @@ uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data) Address = (uint32_t)((EEPROM_START_ADDRESS - 2) + (uint32_t)((1 + ValidPage) * PAGE_SIZE)); /* Check each active page address starting from end */ - while (Address > (PageStartAddress + 2)) - { + while(Address > (PageStartAddress + 2)) { /* Get the current location content to be compared with virtual address */ - AddressValue = (*(__IO uint16_t*)Address); + AddressValue = (*(__IO uint16_t *)Address); /* Compare the read address with the virtual address */ - if (AddressValue == VirtAddress) - { + if(AddressValue == VirtAddress) { /* Get content of Address-2 which is variable value */ - *Data = (*(__IO uint16_t*)(Address - 2)); + *Data = (*(__IO uint16_t *)(Address - 2)); /* In case variable value is read, reset ReadStatus flag */ ReadStatus = 0; break; - } - else - { + } else { /* Next address location */ Address = Address - 4; } @@ -326,16 +288,14 @@ uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data) * - NO_VALID_PAGE: if no valid page was found * - Flash error code: on write Flash error */ -uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data) -{ +uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data) { uint16_t Status = 0; /* Write the variable virtual address and value in the EEPROM */ Status = EE_VerifyPageFullWriteVariable(VirtAddress, Data); /* In case the EEPROM active page is full */ - if (Status == PAGE_FULL) - { + if(Status == PAGE_FULL) { /* Perform Page transfer */ Status = EE_PageTransfer(VirtAddress, Data); } @@ -350,16 +310,14 @@ uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data) * @retval Status of the last operation (Flash write or erase) done during * EEPROM formating */ -static FLASH_Status EE_Format(void) -{ +static FLASH_Status EE_Format(void) { FLASH_Status FlashStatus = FLASH_COMPLETE; /* Erase Page0 */ FlashStatus = FLASH_EraseSector(PAGE0_ID, VOLTAGE_RANGE); /* If erase operation was failed, a Flash error code is returned */ - if (FlashStatus != FLASH_COMPLETE) - { + if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } @@ -367,8 +325,7 @@ static FLASH_Status EE_Format(void) FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ - if (FlashStatus != FLASH_COMPLETE) - { + if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } @@ -388,65 +345,47 @@ static FLASH_Status EE_Format(void) * @retval Valid page number (PAGE or PAGE1) or NO_VALID_PAGE in case * of no valid page was found */ -static uint16_t EE_FindValidPage(uint8_t Operation) -{ +static uint16_t EE_FindValidPage(uint8_t Operation) { uint16_t PageStatus0 = 6, PageStatus1 = 6; /* Get Page0 actual status */ - PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS); + PageStatus0 = (*(__IO uint16_t *)PAGE0_BASE_ADDRESS); /* Get Page1 actual status */ - PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS); + PageStatus1 = (*(__IO uint16_t *)PAGE1_BASE_ADDRESS); /* Write or read operation */ - switch (Operation) - { - case WRITE_IN_VALID_PAGE: /* ---- Write operation ---- */ - if (PageStatus1 == VALID_PAGE) - { + switch(Operation) { + case WRITE_IN_VALID_PAGE: /* ---- Write operation ---- */ + if(PageStatus1 == VALID_PAGE) { /* Page0 receiving data */ - if (PageStatus0 == RECEIVE_DATA) - { - return PAGE0; /* Page0 valid */ + if(PageStatus0 == RECEIVE_DATA) { + return PAGE0; /* Page0 valid */ + } else { + return PAGE1; /* Page1 valid */ } - else - { - return PAGE1; /* Page1 valid */ - } - } - else if (PageStatus0 == VALID_PAGE) - { + } else if(PageStatus0 == VALID_PAGE) { /* Page1 receiving data */ - if (PageStatus1 == RECEIVE_DATA) - { - return PAGE1; /* Page1 valid */ + if(PageStatus1 == RECEIVE_DATA) { + return PAGE1; /* Page1 valid */ + } else { + return PAGE0; /* Page0 valid */ } - else - { - return PAGE0; /* Page0 valid */ - } - } - else - { - return NO_VALID_PAGE; /* No valid Page */ + } else { + return NO_VALID_PAGE; /* No valid Page */ } - case READ_FROM_VALID_PAGE: /* ---- Read operation ---- */ - if (PageStatus0 == VALID_PAGE) - { - return PAGE0; /* Page0 valid */ - } - else if (PageStatus1 == VALID_PAGE) - { - return PAGE1; /* Page1 valid */ - } - else - { - return NO_VALID_PAGE ; /* No valid Page */ + case READ_FROM_VALID_PAGE: /* ---- Read operation ---- */ + if(PageStatus0 == VALID_PAGE) { + return PAGE0; /* Page0 valid */ + } else if(PageStatus1 == VALID_PAGE) { + return PAGE1; /* Page1 valid */ + } else { + return NO_VALID_PAGE; /* No valid Page */ } default: - return PAGE0; /* Page0 valid */ + return PAGE0; /* Page0 valid */ } } @@ -460,19 +399,17 @@ static uint16_t EE_FindValidPage(uint8_t Operation) * - NO_VALID_PAGE: if no valid page was found * - Flash error code: on write Flash error */ -static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data) -{ +static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data) { FLASH_Status FlashStatus = FLASH_COMPLETE; - uint16_t ValidPage = PAGE0; - uint32_t Address = EEPROM_START_ADDRESS, PageEndAddress = EEPROM_START_ADDRESS+PAGE_SIZE; + uint16_t ValidPage = PAGE0; + uint32_t Address = EEPROM_START_ADDRESS, PageEndAddress = EEPROM_START_ADDRESS + PAGE_SIZE; /* Get valid Page for write operation */ ValidPage = EE_FindValidPage(WRITE_IN_VALID_PAGE); /* Check if there is no valid page */ - if (ValidPage == NO_VALID_PAGE) - { - return NO_VALID_PAGE; + if(ValidPage == NO_VALID_PAGE) { + return NO_VALID_PAGE; } /* Get the valid Page start Address */ @@ -482,25 +419,20 @@ static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Da PageEndAddress = (uint32_t)((EEPROM_START_ADDRESS - 2) + (uint32_t)((1 + ValidPage) * PAGE_SIZE)); /* Check each active page address starting from begining */ - while (Address < PageEndAddress) - { + while(Address < PageEndAddress) { /* Verify if Address and Address+2 contents are 0xFFFFFFFF */ - if ((*(__IO uint32_t*)Address) == 0xFFFFFFFF) - { + if((*(__IO uint32_t *)Address) == 0xFFFFFFFF) { /* Set variable data */ FlashStatus = FLASH_ProgramHalfWord(Address, Data); /* If program operation was failed, a Flash error code is returned */ - if (FlashStatus != FLASH_COMPLETE) - { + if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Set variable virtual address */ FlashStatus = FLASH_ProgramHalfWord(Address + 2, VirtAddress); /* Return program operation status */ return FlashStatus; - } - else - { + } else { /* Next address location */ Address = Address + 4; } @@ -521,69 +453,60 @@ static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Da * - NO_VALID_PAGE: if no valid page was found * - Flash error code: on write Flash error */ -static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data) -{ +static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data) { FLASH_Status FlashStatus = FLASH_COMPLETE; - uint32_t NewPageAddress = EEPROM_START_ADDRESS; - uint16_t OldPageId=0; + uint32_t NewPageAddress = EEPROM_START_ADDRESS; + uint16_t OldPageId = 0; uint16_t ValidPage = PAGE0, VarIdx = 0; uint16_t EepromStatus = 0, ReadStatus = 0; /* Get active Page for read operation */ ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE); - if (ValidPage == PAGE1) /* Page1 valid */ + if(ValidPage == PAGE1) /* Page1 valid */ { /* New page address where variable will be moved to */ NewPageAddress = PAGE0_BASE_ADDRESS; /* Old page ID where variable will be taken from */ OldPageId = PAGE1_ID; - } - else if (ValidPage == PAGE0) /* Page0 valid */ + } else if(ValidPage == PAGE0) /* Page0 valid */ { /* New page address where variable will be moved to */ NewPageAddress = PAGE1_BASE_ADDRESS; /* Old page ID where variable will be taken from */ OldPageId = PAGE0_ID; - } - else - { - return NO_VALID_PAGE; /* No valid Page */ + } else { + return NO_VALID_PAGE; /* No valid Page */ } /* Set the new Page status to RECEIVE_DATA status */ FlashStatus = FLASH_ProgramHalfWord(NewPageAddress, RECEIVE_DATA); /* If program operation was failed, a Flash error code is returned */ - if (FlashStatus != FLASH_COMPLETE) - { + if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Write the variable passed as parameter in the new active page */ EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddress, Data); /* If program operation was failed, a Flash error code is returned */ - if (EepromStatus != FLASH_COMPLETE) - { + if(EepromStatus != FLASH_COMPLETE) { return EepromStatus; } /* Transfer process: transfer variables from old to the new active page */ - for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) - { - if (VarIdx != VirtAddress) /* Check each variable except the one passed as parameter */ + for(VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) { + if(VarIdx != VirtAddress) /* Check each variable except the one passed as parameter */ { /* Read the other last variable updates */ ReadStatus = EE_ReadVariable(VarIdx, &DataVar); /* In case variable corresponding to the virtual address was found */ - if (ReadStatus != 0x1) - { + if(ReadStatus != 0x1) { /* Transfer the variable to the new active page */ EepromStatus = EE_VerifyPageFullWriteVariable(VarIdx, DataVar); /* If program operation was failed, a Flash error code is returned */ - if (EepromStatus != FLASH_COMPLETE) - { + if(EepromStatus != FLASH_COMPLETE) { return EepromStatus; } } @@ -593,16 +516,14 @@ static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data) /* Erase the old Page: Set old Page status to ERASED status */ FlashStatus = FLASH_EraseSector(OldPageId, VOLTAGE_RANGE); /* If erase operation was failed, a Flash error code is returned */ - if (FlashStatus != FLASH_COMPLETE) - { + if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } /* Set new Page status to VALID_PAGE status */ FlashStatus = FLASH_ProgramHalfWord(NewPageAddress, VALID_PAGE); /* If program operation was failed, a Flash error code is returned */ - if (FlashStatus != FLASH_COMPLETE) - { + if(FlashStatus != FLASH_COMPLETE) { return FlashStatus; } @@ -612,6 +533,6 @@ static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data) /** * @} - */ + */ /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ diff --git a/src/old/link.c b/src/old/link.c index fa89e27e..8311ffa1 100644 --- a/src/old/link.c +++ b/src/old/link.c @@ -1,405 +1,405 @@ #include "link.h" #include "hal.h" -void link_ac(){ - hal_set_pin("t2c0.rt_prio", 9.0); - // hal_set_pin("curpid0.rt_prio", 10.0); - // hal_set_pin("pmsm0.rt_prio", 11.0); - hal_set_pin("pmsm_limits0.rt_prio", 12.0); - // hal_set_pin("idq0.rt_prio", 13.0); - // hal_set_pin("dq0.rt_prio", 14.1); - hal_set_pin("i2t0.rt_prio", 15.0); +void link_ac() { + hal_set_pin("t2c0.rt_prio", 9.0); + // hal_set_pin("curpid0.rt_prio", 10.0); + // hal_set_pin("pmsm0.rt_prio", 11.0); + hal_set_pin("pmsm_limits0.rt_prio", 12.0); + // hal_set_pin("idq0.rt_prio", 13.0); + // hal_set_pin("dq0.rt_prio", 14.1); + hal_set_pin("i2t0.rt_prio", 15.0); - // t2c - hal_link_pins("conf0.polecount", "t2c0.polecount"); - hal_link_pins("conf0.psi", "t2c0.psi"); - hal_link_pins("cauto0.pos", "t2c0.pos"); + // t2c + hal_link_pins("conf0.polecount", "t2c0.polecount"); + hal_link_pins("conf0.psi", "t2c0.psi"); + hal_link_pins("cauto0.pos", "t2c0.pos"); - hal_link_pins("t2c0.cur", "hv0.q_cmd"); - hal_link_pins("cauto0.i_d", "hv0.d_cmd"); - hal_link_pins("cauto0.pos", "hv0.pos"); - hal_link_pins("net0.fb_d", "hv0.vel"); - hal_link_pins("conf0.max_ac_cur", "hv0.max_cur"); - hal_link_pins("conf0.polecount", "hv0.polecount"); - hal_link_pins("pid0.torque_cor_cmd", "vel1.torque"); + hal_link_pins("t2c0.cur", "hv0.q_cmd"); + hal_link_pins("cauto0.i_d", "hv0.d_cmd"); + hal_link_pins("cauto0.pos", "hv0.pos"); + hal_link_pins("net0.fb_d", "hv0.vel"); + hal_link_pins("conf0.max_ac_cur", "hv0.max_cur"); + hal_link_pins("conf0.polecount", "hv0.polecount"); + hal_link_pins("pid0.torque_cor_cmd", "vel1.torque"); - hal_link_pins("conf0.r", "hv0.r"); - hal_link_pins("conf0.l", "hv0.l"); - hal_link_pins("conf0.psi", "hv0.psi"); - hal_link_pins("conf0.cur_p", "hv0.cur_p"); - hal_link_pins("conf0.cur_i", "hv0.cur_i"); - hal_link_pins("conf0.cur_ff", "hv0.cur_ff"); - hal_link_pins("conf0.cur_ind", "hv0.cur_ind"); - hal_set_pin("hv0.max_y", 1.0); + hal_link_pins("conf0.r", "hv0.r"); + hal_link_pins("conf0.l", "hv0.l"); + hal_link_pins("conf0.psi", "hv0.psi"); + hal_link_pins("conf0.cur_p", "hv0.cur_p"); + hal_link_pins("conf0.cur_i", "hv0.cur_i"); + hal_link_pins("conf0.cur_ff", "hv0.cur_ff"); + hal_link_pins("conf0.cur_ind", "hv0.cur_ind"); + hal_set_pin("hv0.max_y", 1.0); - // curpid - // hal_link_pins("cauto0.i_d", "curpid0.id_cmd"); - // hal_link_pins("t2c0.cur", "curpid0.iq_cmd"); - // hal_link_pins("hv0.dc_volt", "curpid0.dc_volt"); - // hal_link_pins("hv0.pwm_volt", "curpid0.ac_volt"); - // hal_link_pins("conf0.r", "curpid0.rd"); - // hal_link_pins("conf0.r", "curpid0.rq"); - // hal_link_pins("conf0.l", "curpid0.ld"); - // hal_link_pins("conf0.l", "curpid0.lq"); - // hal_link_pins("conf0.cur_ff", "curpid0.ff"); - // hal_link_pins("conf0.cur_p", "curpid0.kp"); - // hal_link_pins("conf0.cur_i", "curpid0.ki"); - // hal_link_pins("conf0.cur_ind", "curpid0.kind"); + // curpid + // hal_link_pins("cauto0.i_d", "curpid0.id_cmd"); + // hal_link_pins("t2c0.cur", "curpid0.iq_cmd"); + // hal_link_pins("hv0.dc_volt", "curpid0.dc_volt"); + // hal_link_pins("hv0.pwm_volt", "curpid0.ac_volt"); + // hal_link_pins("conf0.r", "curpid0.rd"); + // hal_link_pins("conf0.r", "curpid0.rq"); + // hal_link_pins("conf0.l", "curpid0.ld"); + // hal_link_pins("conf0.l", "curpid0.lq"); + // hal_link_pins("conf0.cur_ff", "curpid0.ff"); + // hal_link_pins("conf0.cur_p", "curpid0.kp"); + // hal_link_pins("conf0.cur_i", "curpid0.ki"); + // hal_link_pins("conf0.cur_ind", "curpid0.kind"); - // pmsm - // hal_link_pins("curpid0.ud", "pmsm0.ud"); - // hal_link_pins("curpid0.uq", "pmsm0.uq"); - // hal_link_pins("net0.fb_d", "pmsm0.vel"); - // hal_link_pins("conf0.r", "pmsm0.r"); - // hal_link_pins("conf0.l", "pmsm0.ld"); - // hal_link_pins("conf0.l", "pmsm0.lq"); - // hal_link_pins("conf0.psi", "pmsm0.psi"); - // hal_link_pins("pmsm0.id", "curpid0.id_fb"); - // hal_link_pins("pmsm0.iq", "curpid0.iq_fb"); - // hal_link_pins("pmsm0.indd", "curpid0.indd_fb"); - // hal_link_pins("pmsm0.indq", "curpid0.indq_fb"); - // hal_link_pins("conf0.polecount", "pmsm0.polecount"); - // hal_link_pins("pmsm0.iq", "hv0.iq"); + // pmsm + // hal_link_pins("curpid0.ud", "pmsm0.ud"); + // hal_link_pins("curpid0.uq", "pmsm0.uq"); + // hal_link_pins("net0.fb_d", "pmsm0.vel"); + // hal_link_pins("conf0.r", "pmsm0.r"); + // hal_link_pins("conf0.l", "pmsm0.ld"); + // hal_link_pins("conf0.l", "pmsm0.lq"); + // hal_link_pins("conf0.psi", "pmsm0.psi"); + // hal_link_pins("pmsm0.id", "curpid0.id_fb"); + // hal_link_pins("pmsm0.iq", "curpid0.iq_fb"); + // hal_link_pins("pmsm0.indd", "curpid0.indd_fb"); + // hal_link_pins("pmsm0.indq", "curpid0.indq_fb"); + // hal_link_pins("conf0.polecount", "pmsm0.polecount"); + // hal_link_pins("pmsm0.iq", "hv0.iq"); - // pmsm_limits - hal_link_pins("conf0.r", "pmsm_limits0.r"); - hal_link_pins("conf0.l", "pmsm_limits0.ld"); - hal_link_pins("conf0.l", "pmsm_limits0.lq"); - hal_link_pins("conf0.psi", "pmsm_limits0.psi"); - hal_link_pins("conf0.polecount", "pmsm_limits0.polecount"); - hal_link_pins("hv0.pwm_volt", "pmsm_limits0.ac_volt"); - hal_link_pins("pmsm0.indq", "pmsm_limits0.indq"); - hal_link_pins("pmsm0.iq", "pmsm_limits0.iq"); - hal_link_pins("pmsm_limits0.max_torque", "pid0.max_torque"); - hal_link_pins("pmsm_limits0.min_torque", "pid0.min_torque"); - hal_link_pins("pmsm_limits0.abs_max_vel", "pid0.max_vel"); + // pmsm_limits + hal_link_pins("conf0.r", "pmsm_limits0.r"); + hal_link_pins("conf0.l", "pmsm_limits0.ld"); + hal_link_pins("conf0.l", "pmsm_limits0.lq"); + hal_link_pins("conf0.psi", "pmsm_limits0.psi"); + hal_link_pins("conf0.polecount", "pmsm_limits0.polecount"); + hal_link_pins("hv0.pwm_volt", "pmsm_limits0.ac_volt"); + hal_link_pins("pmsm0.indq", "pmsm_limits0.indq"); + hal_link_pins("pmsm0.iq", "pmsm_limits0.iq"); + hal_link_pins("pmsm_limits0.max_torque", "pid0.max_torque"); + hal_link_pins("pmsm_limits0.min_torque", "pid0.min_torque"); + hal_link_pins("pmsm_limits0.abs_max_vel", "pid0.max_vel"); - // i2t - hal_link_pins("conf0.r", "i2t0.mot_res"); - hal_link_pins("conf0.psi", "i2t0.mot_psi"); - hal_link_pins("conf0.max_motor_temp", "i2t0.mot_max_temp"); - hal_link_pins("pmsm0.id", "i2t0.id"); - hal_link_pins("pmsm0.iq", "i2t0.iq"); - // hal_link_pins("conf0.max_ac_cur", "i2t0.mot_cont_cur"); - // hal_link_pins("i2t0.mot_temp", "fault0.motor_temp"); - hal_link_pins("fault0.mot_brake", "i2t0.brake"); + // i2t + hal_link_pins("conf0.r", "i2t0.mot_res"); + hal_link_pins("conf0.psi", "i2t0.mot_psi"); + hal_link_pins("conf0.max_motor_temp", "i2t0.mot_max_temp"); + hal_link_pins("pmsm0.id", "i2t0.id"); + hal_link_pins("pmsm0.iq", "i2t0.iq"); + // hal_link_pins("conf0.max_ac_cur", "i2t0.mot_cont_cur"); + // hal_link_pins("i2t0.mot_temp", "fault0.motor_temp"); + hal_link_pins("fault0.mot_brake", "i2t0.brake"); - hal_link_pins("curpid0.ud", "idq0.d"); - hal_link_pins("curpid0.uq", "idq0.q"); - hal_link_pins("conf0.polecount", "idq0.polecount"); - hal_link_pins("idq0.a", "hv0.a"); - hal_link_pins("idq0.b", "hv0.b"); + hal_link_pins("curpid0.ud", "idq0.d"); + hal_link_pins("curpid0.uq", "idq0.q"); + hal_link_pins("conf0.polecount", "idq0.polecount"); + hal_link_pins("idq0.a", "hv0.a"); + hal_link_pins("idq0.b", "hv0.b"); } -void link_pid(){ - hal_set_pin("fault0.rt_prio", 3.0); - hal_set_pin("rev0.rt_prio", 4.0); - hal_set_pin("rev1.rt_prio", 4.0); - hal_set_pin("vel0.rt_prio", 5.0); - hal_set_pin("vel1.rt_prio", 5.0); - hal_set_pin("stp0.rt_prio", 6.0); - hal_set_pin("cauto0.rt_prio", 7.0); - hal_set_pin("pid0.rt_prio", 8.0); - hal_set_pin("hv0.rt_prio", 14.0); - hal_set_pin("sim0.rt_prio", 15.0); - hal_set_pin("term0.rt_prio", 15.0); +void link_pid() { + hal_set_pin("fault0.rt_prio", 3.0); + hal_set_pin("rev0.rt_prio", 4.0); + hal_set_pin("rev1.rt_prio", 4.0); + hal_set_pin("vel0.rt_prio", 5.0); + hal_set_pin("vel1.rt_prio", 5.0); + hal_set_pin("stp0.rt_prio", 6.0); + hal_set_pin("cauto0.rt_prio", 7.0); + hal_set_pin("pid0.rt_prio", 8.0); + hal_set_pin("hv0.rt_prio", 14.0); + hal_set_pin("sim0.rt_prio", 15.0); + hal_set_pin("term0.rt_prio", 15.0); - // cmd - hal_link_pins("conf0.cmd_rev", "rev0.rev"); - hal_link_pins("vel0.pos_out", "net0.cmd"); - hal_link_pins("conf0.max_vel", "stp0.max_vel"); - hal_link_pins("conf0.max_acc", "stp0.max_acc"); - hal_link_pins("rev0.out", "vel0.pos_in"); - hal_link_pins("fault0.en_pid", "vel0.en"); - hal_link_pins("vel0.vel", "net0.cmd_d"); - hal_set_pin("vel0.w", 500.0); - hal_link_pins("net0.cmd","pid0.pos_ext_cmd"); - hal_link_pins("net0.cmd_d", "pid0.vel_ext_cmd"); + // cmd + hal_link_pins("conf0.cmd_rev", "rev0.rev"); + hal_link_pins("vel0.pos_out", "net0.cmd"); + hal_link_pins("conf0.max_vel", "stp0.max_vel"); + hal_link_pins("conf0.max_acc", "stp0.max_acc"); + hal_link_pins("rev0.out", "vel0.pos_in"); + hal_link_pins("fault0.en_pid", "vel0.en"); + hal_link_pins("vel0.vel", "net0.cmd_d"); + hal_set_pin("vel0.w", 500.0); + hal_link_pins("net0.cmd", "pid0.pos_ext_cmd"); + hal_link_pins("net0.cmd_d", "pid0.vel_ext_cmd"); - // fb - hal_link_pins("conf0.fb_rev", "rev1.rev"); + // fb + hal_link_pins("conf0.fb_rev", "rev1.rev"); - hal_link_pins("rev1.out", "vel1.pos_in"); - hal_link_pins("fault0.en_pid", "vel1.en"); - hal_link_pins("vel1.pos_out", "cauto0.fb_in"); + hal_link_pins("rev1.out", "vel1.pos_in"); + hal_link_pins("fault0.en_pid", "vel1.en"); + hal_link_pins("vel1.pos_out", "cauto0.fb_in"); - hal_link_pins("conf0.j", "vel1.j"); - hal_link_pins("pmsm0.torque", "vel1.torque"); - hal_link_pins("vel1.vel", "net0.fb_d"); - hal_link_pins("cauto0.fb_out", "net0.fb"); + hal_link_pins("conf0.j", "vel1.j"); + hal_link_pins("pmsm0.torque", "vel1.torque"); + hal_link_pins("vel1.vel", "net0.fb_d"); + hal_link_pins("cauto0.fb_out", "net0.fb"); - hal_link_pins("net0.fb","pid0.pos_fb"); - hal_link_pins("net0.fb_d", "pid0.vel_fb"); + hal_link_pins("net0.fb", "pid0.pos_fb"); + hal_link_pins("net0.fb_d", "pid0.vel_fb"); - // fault - hal_link_pins("conf0.max_dc_cur", "fault0.max_dc_cur"); - // hal_link_pins("conf0.max_ac_cur", "fault0.max_ac_cur"); - hal_link_pins("conf0.max_dc_volt", "fault0.max_hv_volt"); - hal_link_pins("conf0.max_hv_temp", "fault0.max_hv_temp"); - hal_link_pins("conf0.max_motor_temp", "fault0.max_mot_temp"); - hal_link_pins("conf0.max_pos_error", "fault0.max_pos_error"); - hal_link_pins("conf0.high_dc_volt", "fault0.high_hv_volt"); - hal_link_pins("conf0.high_hv_temp", "fault0.high_hv_temp"); - hal_link_pins("conf0.high_motor_temp", "fault0.high_mot_temp"); - hal_link_pins("conf0.low_dc_volt", "fault0.min_hv_volt"); - hal_link_pins("conf0.fan_hv_temp", "fault0.fan_hv_temp"); - hal_link_pins("conf0.fan_motor_temp", "fault0.fan_mot_temp"); - hal_link_pins("conf0.autophase", "fault0.phase_on_start"); - hal_link_pins("conf0.max_sat", "fault0.max_sat"); + // fault + hal_link_pins("conf0.max_dc_cur", "fault0.max_dc_cur"); + // hal_link_pins("conf0.max_ac_cur", "fault0.max_ac_cur"); + hal_link_pins("conf0.max_dc_volt", "fault0.max_hv_volt"); + hal_link_pins("conf0.max_hv_temp", "fault0.max_hv_temp"); + hal_link_pins("conf0.max_motor_temp", "fault0.max_mot_temp"); + hal_link_pins("conf0.max_pos_error", "fault0.max_pos_error"); + hal_link_pins("conf0.high_dc_volt", "fault0.high_hv_volt"); + hal_link_pins("conf0.high_hv_temp", "fault0.high_hv_temp"); + hal_link_pins("conf0.high_motor_temp", "fault0.high_mot_temp"); + hal_link_pins("conf0.low_dc_volt", "fault0.min_hv_volt"); + hal_link_pins("conf0.fan_hv_temp", "fault0.fan_hv_temp"); + hal_link_pins("conf0.fan_motor_temp", "fault0.fan_mot_temp"); + hal_link_pins("conf0.autophase", "fault0.phase_on_start"); + hal_link_pins("conf0.max_sat", "fault0.max_sat"); - hal_link_pins("fault0.phase_start", "cauto0.start"); - hal_link_pins("cauto0.ready", "fault0.phase_ready"); + hal_link_pins("fault0.phase_start", "cauto0.start"); + hal_link_pins("cauto0.ready", "fault0.phase_ready"); - hal_link_pins("pid0.pos_error", "fault0.pos_error"); - hal_link_pins("pid0.saturated", "fault0.sat"); - hal_link_pins("hv0.dc_volt", "fault0.hv_volt"); - hal_link_pins("hv0.hv_temp", "fault0.hv_temp"); - hal_link_pins("net0.motor_temp", "fault0.mot_temp"); - hal_link_pins("hv0.dc_cur", "fault0.dc_cur"); - hal_link_pins("net0.fb_error", "fault0.fb0_error"); - hal_link_pins("net0.fb_error", "fault0.fb1_error"); - hal_link_pins("net0.cmd", "fault0.cmd"); - hal_link_pins("rev1.out", "fault0.fb"); - hal_link_pins("fault0.start_offset", "cauto0.start_offset"); + hal_link_pins("pid0.pos_error", "fault0.pos_error"); + hal_link_pins("pid0.saturated", "fault0.sat"); + hal_link_pins("hv0.dc_volt", "fault0.hv_volt"); + hal_link_pins("hv0.hv_temp", "fault0.hv_temp"); + hal_link_pins("net0.motor_temp", "fault0.mot_temp"); + hal_link_pins("hv0.dc_cur", "fault0.dc_cur"); + hal_link_pins("net0.fb_error", "fault0.fb0_error"); + hal_link_pins("net0.fb_error", "fault0.fb1_error"); + hal_link_pins("net0.cmd", "fault0.cmd"); + hal_link_pins("rev1.out", "fault0.fb"); + hal_link_pins("fault0.start_offset", "cauto0.start_offset"); - hal_link_pins("fault0.scale", "curpid0.scale"); - hal_link_pins("conf0.max_ac_cur", "curpid0.max_cur"); - hal_link_pins("hv0.com_error", "fault0.hv_error"); - // hal_link_pins("hv0.hv_fault", "fault0.hv_error"); //TODO + hal_link_pins("fault0.scale", "curpid0.scale"); + hal_link_pins("conf0.max_ac_cur", "curpid0.max_cur"); + hal_link_pins("hv0.com_error", "fault0.hv_error"); + // hal_link_pins("hv0.hv_fault", "fault0.hv_error"); //TODO - hal_link_pins("fault0.mot_brake", "io0.out0"); - hal_link_pins("fault0.hv_fan", "io0.fan"); + hal_link_pins("fault0.mot_brake", "io0.out0"); + hal_link_pins("fault0.hv_fan", "io0.fan"); - hal_link_pins("fault0.en_out", "hv0.en"); - hal_link_pins("fault0.en_pid", "pid0.enable"); + hal_link_pins("fault0.en_out", "hv0.en"); + hal_link_pins("fault0.en_pid", "pid0.enable"); - hal_link_pins("net0.enable", "fault0.en"); + hal_link_pins("net0.enable", "fault0.en"); - hal_link_pins("fault0.fault", "io0.fault"); - hal_link_pins("fault0.state", "io0.state"); + hal_link_pins("fault0.fault", "io0.fault"); + hal_link_pins("fault0.state", "io0.state"); - // cauto + // cauto - hal_link_pins("conf0.fb_offset", "cauto0.mag_offset"); - hal_link_pins("conf0.phase_cur", "cauto0.cur"); - hal_link_pins("conf0.phase_time", "cauto0.time"); + hal_link_pins("conf0.fb_offset", "cauto0.mag_offset"); + hal_link_pins("conf0.phase_cur", "cauto0.cur"); + hal_link_pins("conf0.phase_time", "cauto0.time"); - // pid - hal_link_pins("conf0.j", "pid0.j"); - hal_link_pins("conf0.g", "pid0.g"); - hal_link_pins("conf0.pos_p", "pid0.pos_p"); - hal_link_pins("conf0.vel_p", "pid0.vel_p"); - hal_link_pins("conf0.vel_i", "pid0.vel_i"); - hal_link_pins("conf0.vel_g", "pid0.vel_g"); - hal_link_pins("conf0.max_vel", "pid0.max_usr_vel"); - hal_link_pins("conf0.max_acc", "pid0.max_usr_acc"); - hal_link_pins("conf0.max_force", "pid0.max_usr_torque"); - hal_link_pins("pid0.torque_cor_cmd", "t2c0.torque"); + // pid + hal_link_pins("conf0.j", "pid0.j"); + hal_link_pins("conf0.g", "pid0.g"); + hal_link_pins("conf0.pos_p", "pid0.pos_p"); + hal_link_pins("conf0.vel_p", "pid0.vel_p"); + hal_link_pins("conf0.vel_i", "pid0.vel_i"); + hal_link_pins("conf0.vel_g", "pid0.vel_g"); + hal_link_pins("conf0.max_vel", "pid0.max_usr_vel"); + hal_link_pins("conf0.max_acc", "pid0.max_usr_acc"); + hal_link_pins("conf0.max_force", "pid0.max_usr_torque"); + hal_link_pins("pid0.torque_cor_cmd", "t2c0.torque"); - // misc - hal_link_pins("conf0.out_rev", "hv0.rev"); - // hal_link_pins("cauto0.pos", "idq0.pos"); - hal_link_pins("conf0.cmd_res", "sim0.res"); + // misc + hal_link_pins("conf0.out_rev", "hv0.rev"); + // hal_link_pins("cauto0.pos", "idq0.pos"); + hal_link_pins("conf0.cmd_res", "sim0.res"); - // term - hal_link_pins("net0.cmd", "term0.wave0"); - hal_link_pins("net0.fb", "term0.wave1"); - hal_link_pins("net0.cmd_d", "term0.wave2"); - hal_link_pins("net0.fb_d", "term0.wave3"); - hal_set_pin("term0.gain0", 20.0); - hal_set_pin("term0.gain1", 20.0); - hal_set_pin("term0.gain2", 1.0); - hal_set_pin("term0.gain3", 1.0); + // term + hal_link_pins("net0.cmd", "term0.wave0"); + hal_link_pins("net0.fb", "term0.wave1"); + hal_link_pins("net0.cmd_d", "term0.wave2"); + hal_link_pins("net0.fb_d", "term0.wave3"); + hal_set_pin("term0.gain0", 20.0); + hal_set_pin("term0.gain1", 20.0); + hal_set_pin("term0.gain2", 1.0); + hal_set_pin("term0.gain3", 1.0); - hal_link_pins("conf0.sin_gain", "adc0.sin_gain"); - hal_link_pins("conf0.cos_gain", "adc0.cos_gain"); - hal_link_pins("conf0.sin_offset", "adc0.sin_offset"); - hal_link_pins("conf0.cos_offset", "adc0.cos_offset"); + hal_link_pins("conf0.sin_gain", "adc0.sin_gain"); + hal_link_pins("conf0.cos_gain", "adc0.cos_gain"); + hal_link_pins("conf0.sin_offset", "adc0.sin_offset"); + hal_link_pins("conf0.cos_offset", "adc0.cos_offset"); } -void link_simplepid(){ - hal_set_pin("ypid0.rt_prio", 8.0); - hal_link_pins("net0.cmd", "ypid0.pos_ext_cmd"); - hal_link_pins("net0.cmd_d", "ypid0.vel_ext_cmd"); - hal_link_pins("net0.fb", "ypid0.pos_fb"); - hal_link_pins("net0.fb_d", "ypid0.vel_fb"); - hal_link_pins("fault0.en_pid", "ypid0.enable"); - hal_link_pins("conf0.pos_p", "ypid0.pos_p"); - hal_link_pins("conf0.max_vel", "ypid0.max_vel"); - hal_link_pins("conf0.max_acc", "ypid0.max_acc"); - hal_link_pins("hv0.pwm_volt", "ypid0.max_out"); - hal_link_pins("ypid0.saturated", "fault0.sat"); - hal_link_pins("ypid0.pos_error", "fault0.pos_error"); - hal_link_pins("conf0.acc_p", "ypid0.vel_p");//TODO: rename config pins? - hal_link_pins("conf0.acc_pi", "ypid0.vel_i");//TODO: rename config pins? - hal_set_pin("vel1.w" , 2000.0);//TODO: change to velbuf +void link_simplepid() { + hal_set_pin("ypid0.rt_prio", 8.0); + hal_link_pins("net0.cmd", "ypid0.pos_ext_cmd"); + hal_link_pins("net0.cmd_d", "ypid0.vel_ext_cmd"); + hal_link_pins("net0.fb", "ypid0.pos_fb"); + hal_link_pins("net0.fb_d", "ypid0.vel_fb"); + hal_link_pins("fault0.en_pid", "ypid0.enable"); + hal_link_pins("conf0.pos_p", "ypid0.pos_p"); + hal_link_pins("conf0.max_vel", "ypid0.max_vel"); + hal_link_pins("conf0.max_acc", "ypid0.max_acc"); + hal_link_pins("hv0.pwm_volt", "ypid0.max_out"); + hal_link_pins("ypid0.saturated", "fault0.sat"); + hal_link_pins("ypid0.pos_error", "fault0.pos_error"); + hal_link_pins("conf0.acc_p", "ypid0.vel_p"); //TODO: rename config pins? + hal_link_pins("conf0.acc_pi", "ypid0.vel_i"); //TODO: rename config pins? + hal_set_pin("vel1.w", 2000.0); //TODO: change to velbuf } -int update_mot(){ - switch((mot_type_t)hal_get_pin("conf0.mot_type")){ - case ACSYNC: - //TODO: copy acsync stuff from link_pid here - link_ac(); - hal_link_pins("curpid0.uq", "idq0.q"); - hal_set_pin("hv0.mode", 1.0); - break; - case ACASYNC: - break; - case AC2PHASE://precise - link_ac(); - hal_set_pin("uf0.rt_prio", 3.0); - hal_link_pins("uf0.pos", "idq0.pos"); - hal_set_pin("idq0.d", 0.0); - hal_link_pins("uf0.volt", "idq0.q"); - hal_set_pin("hv0.mode", 0.0); - hal_link_pins("net0.enable", "hv0.en"); - hal_link_pins("fault0.scale", "uf0.scale"); +int update_mot() { + switch((mot_type_t)hal_get_pin("conf0.mot_type")) { + case ACSYNC: + //TODO: copy acsync stuff from link_pid here + link_ac(); + hal_link_pins("curpid0.uq", "idq0.q"); + hal_set_pin("hv0.mode", 1.0); + break; + case ACASYNC: + break; + case AC2PHASE: //precise + link_ac(); + hal_set_pin("uf0.rt_prio", 3.0); + hal_link_pins("uf0.pos", "idq0.pos"); + hal_set_pin("idq0.d", 0.0); + hal_link_pins("uf0.volt", "idq0.q"); + hal_set_pin("hv0.mode", 0.0); + hal_link_pins("net0.enable", "hv0.en"); + hal_link_pins("fault0.scale", "uf0.scale"); - hal_set_pin("freq_fb0.rt_prio", 1.0); - hal_link_pins("freq_fb0.vel", "uf0.vel_fb"); - //fb_res = 6 - //mot_type = 2 - //polecount = 1 - break; - case DC: - link_simplepid(); - hal_set_pin("hv0.mode", 2.0); - hal_link_pins("ypid0.out", "hv0.q_cmd"); - break; - default: - return -1; - } - return 0; + hal_set_pin("freq_fb0.rt_prio", 1.0); + hal_link_pins("freq_fb0.vel", "uf0.vel_fb"); + //fb_res = 6 + //mot_type = 2 + //polecount = 1 + break; + case DC: + link_simplepid(); + hal_set_pin("hv0.mode", 2.0); + hal_link_pins("ypid0.out", "hv0.q_cmd"); + break; + default: + return -1; + } + return 0; } -int update_fb(){ - hal_set_pin("adc0.rt_prio", 1.0); - hal_set_pin("enc_fb0.rt_prio", -1.0); - hal_set_pin("enc_fb0.frt_prio", -1.0); - hal_set_pin("res0.rt_prio", -1.0); - hal_set_pin("encm0.rt_prio", -1.0); - // hal_set_pin("encs0.rt_prio", -1.0); - // hal_set_pin("yaskawa0.rt_prio", -1.0); - switch((protocol_t)hal_get_pin("conf0.fb_type")){ - case ENC: - hal_link_pins("enc_fb0.pos", "rev1.in"); - hal_link_pins("conf0.fb_res", "enc_fb0.res"); - hal_set_pin("enc_fb0.rt_prio", 2.0); - hal_set_pin("enc_fb0.frt_prio", 1.0); - hal_link_pins("enc_fb0.isabs", "cauto0.isabs"); - // hal_set_pin("pderiv1.frt_prio", 2.0); - // hal_set_pin("pderiv1.rt_prio", 5.0); - break; - case RES: - hal_link_pins("res0.pos", "rev1.in"); - hal_link_pins("conf0.fb_polecount", "res0.poles"); - hal_link_pins("adc0.sin", "res0.sin"); - hal_link_pins("adc0.cos", "res0.cos"); - hal_link_pins("adc0.quad", "res0.quad"); - hal_link_pins("res0.error", "net0.fb_error"); - hal_set_pin("adc0.res_en", 1.0); - hal_set_pin("res0.rt_prio", 2.0); - break; - case SINCOS: - hal_link_pins("adc0.sin3", "enc_fb0.sin"); - hal_link_pins("adc0.cos3", "enc_fb0.cos"); - hal_link_pins("adc0.quad", "enc_fb0.quad"); - hal_link_pins("enc_fb0.ipos", "rev1.in"); - hal_link_pins("conf0.fb_res", "enc_fb0.res"); - hal_link_pins("enc_fb0.error", "net0.fb_error"); - hal_set_pin("enc_fb0.ires", 1024.0); - hal_set_pin("enc_fb0.rt_prio", 2.0); - hal_set_pin("enc_fb0.frt_prio", 1.0); - break; - case HYPER: - hal_link_pins("adc0.sin3", "enc_fb0.sin"); - hal_link_pins("adc0.cos3", "enc_fb0.cos"); - hal_link_pins("adc0.quad", "enc_fb0.quad"); - hal_link_pins("enc_fb0.ipos", "rev1.in"); - hal_link_pins("conf0.fb_res", "enc_fb0.res"); - hal_link_pins("enc_fb0.error", "net0.fb_error"); - hal_set_pin("enc_fb0.ires", 1024.0); - hal_set_pin("enc_fb0.rt_prio", 2.0); - hal_set_pin("enc_fb0.frt_prio", 1.0); - hal_set_pin("hyper0.rt_prio", 1.0); // TODO: link hyper - break; - case MITSU: - hal_link_pins("encm0.pos", "rev1.in"); - hal_link_pins("encm0.error", "net0.fb_error"); - hal_set_pin("encm0.rt_prio", 2.0); - break; - case YASKAWA: - hal_link_pins("yaskawa0.pos", "rev1.in"); - hal_set_pin("yaskawa0.rt_prio", 2.0); - break; - case SANYO: - hal_link_pins("encs0.pos", "rev1.in"); - hal_set_pin("encs0.rt_prio", 2.0); - break; - default: - return -1; - } - return 0; +int update_fb() { + hal_set_pin("adc0.rt_prio", 1.0); + hal_set_pin("enc_fb0.rt_prio", -1.0); + hal_set_pin("enc_fb0.frt_prio", -1.0); + hal_set_pin("res0.rt_prio", -1.0); + hal_set_pin("encm0.rt_prio", -1.0); + // hal_set_pin("encs0.rt_prio", -1.0); + // hal_set_pin("yaskawa0.rt_prio", -1.0); + switch((protocol_t)hal_get_pin("conf0.fb_type")) { + case ENC: + hal_link_pins("enc_fb0.pos", "rev1.in"); + hal_link_pins("conf0.fb_res", "enc_fb0.res"); + hal_set_pin("enc_fb0.rt_prio", 2.0); + hal_set_pin("enc_fb0.frt_prio", 1.0); + hal_link_pins("enc_fb0.isabs", "cauto0.isabs"); + // hal_set_pin("pderiv1.frt_prio", 2.0); + // hal_set_pin("pderiv1.rt_prio", 5.0); + break; + case RES: + hal_link_pins("res0.pos", "rev1.in"); + hal_link_pins("conf0.fb_polecount", "res0.poles"); + hal_link_pins("adc0.sin", "res0.sin"); + hal_link_pins("adc0.cos", "res0.cos"); + hal_link_pins("adc0.quad", "res0.quad"); + hal_link_pins("res0.error", "net0.fb_error"); + hal_set_pin("adc0.res_en", 1.0); + hal_set_pin("res0.rt_prio", 2.0); + break; + case SINCOS: + hal_link_pins("adc0.sin3", "enc_fb0.sin"); + hal_link_pins("adc0.cos3", "enc_fb0.cos"); + hal_link_pins("adc0.quad", "enc_fb0.quad"); + hal_link_pins("enc_fb0.ipos", "rev1.in"); + hal_link_pins("conf0.fb_res", "enc_fb0.res"); + hal_link_pins("enc_fb0.error", "net0.fb_error"); + hal_set_pin("enc_fb0.ires", 1024.0); + hal_set_pin("enc_fb0.rt_prio", 2.0); + hal_set_pin("enc_fb0.frt_prio", 1.0); + break; + case HYPER: + hal_link_pins("adc0.sin3", "enc_fb0.sin"); + hal_link_pins("adc0.cos3", "enc_fb0.cos"); + hal_link_pins("adc0.quad", "enc_fb0.quad"); + hal_link_pins("enc_fb0.ipos", "rev1.in"); + hal_link_pins("conf0.fb_res", "enc_fb0.res"); + hal_link_pins("enc_fb0.error", "net0.fb_error"); + hal_set_pin("enc_fb0.ires", 1024.0); + hal_set_pin("enc_fb0.rt_prio", 2.0); + hal_set_pin("enc_fb0.frt_prio", 1.0); + hal_set_pin("hyper0.rt_prio", 1.0); // TODO: link hyper + break; + case MITSU: + hal_link_pins("encm0.pos", "rev1.in"); + hal_link_pins("encm0.error", "net0.fb_error"); + hal_set_pin("encm0.rt_prio", 2.0); + break; + case YASKAWA: + hal_link_pins("yaskawa0.pos", "rev1.in"); + hal_set_pin("yaskawa0.rt_prio", 2.0); + break; + case SANYO: + hal_link_pins("encs0.pos", "rev1.in"); + hal_set_pin("encs0.rt_prio", 2.0); + break; + default: + return -1; + } + return 0; } -int update_cmd(){ - hal_set_pin("enc_cmd0.rt_prio", -1.0); - hal_set_pin("enc_cmd0.frt_prio", -1.0); - // hal_set_pin("step_cmd0.rt_prio", -1.0); - // hal_set_pin("step_cmd0.frt_prio", -1.0); - hal_set_pin("sserial0.rt_prio", -1.0); - hal_set_pin("sserial0.frt_prio", -1.0); - hal_set_pin("en0.rt_prio", -1.0); - hal_link_pins("conf0.fb_res", "reslimit0.res"); - switch((protocol_t)hal_get_pin("conf0.cmd_type")){ - case ENC: - hal_link_pins("enc_cmd0.pos", "rev0.in"); - hal_link_pins("conf0.cmd_res", "enc_cmd0.res"); - hal_set_pin("enc_cmd0.rt_prio", 2.0); - hal_set_pin("enc_cmd0.frt_prio", 1.0); - // hal_set_pin("pderiv0.frt_prio", 2.0); - // hal_set_pin("pderiv0.rt_prio", 5.0); - if(hal_get_pin("conf0.error_out") == 1.0){//error out using rs485 - hal_set_pin("en0.rt_prio", 15.0); - hal_link_pins("fault0.fault_not", "en0.en"); - hal_set_pin("en0.txen", 1.0); - } - break; - case STEPDIR: - hal_link_pins("step_cmd0.pos", "rev0.in"); - hal_link_pins("conf0.cmd_res", "step_cmd0.res"); - hal_set_pin("step_cmd0.rt_prio", 2.0); - hal_set_pin("step_cmd0.frt_prio", 1.0); - // hal_set_pin("pderiv0.frt_prio", 2.0); - // hal_set_pin("pderiv0.rt_prio", 5.0); - if(hal_get_pin("conf0.error_out") == 1.0){//error out using rs485 - hal_set_pin("en0.rt_prio", 15.0); - hal_link_pins("fault0.fault_not", "en0.en"); - hal_set_pin("en0.txen", 1.0); - } - break; - case SSERIAL: - hal_link_pins("sserial0.pos_cmd", "vel_int0.pos_in"); - hal_link_pins("sserial0.pos_cmd_d", "vel_int0.vel_in"); - hal_link_pins("sserial0.enable", "net0.enable"); - hal_link_pins("fault0.fault", "sserial0.fault"); - hal_link_pins("sserial0.connected", "fault0.cmd_ready"); - hal_link_pins("sserial0.out0", "fault0.brake_release"); - hal_link_pins("rev1.out", "sserial0.pos_fb"); - //this breaks cmd rev... - hal_link_pins("vel_int0.pos_out", "reslimit0.pos_in"); - hal_link_pins("reslimit0.pos_out", "net0.cmd"); - hal_link_pins("vel_int0.vel_out", "net0.cmd_d"); - hal_set_pin("vel_int0.wd", 0.002);//TODO: this depends on linuxcnc servo thread period - //TODO: handle error of vel_int - hal_set_pin("sserial0.rt_prio", 2.0); - hal_set_pin("sserial0.frt_prio", 2.0); - hal_set_pin("vel_int0.rt_prio", 2.1); - hal_set_pin("reslimit0.rt_prio", 2.2); - break; - default: - return -1; - } - return 0; +int update_cmd() { + hal_set_pin("enc_cmd0.rt_prio", -1.0); + hal_set_pin("enc_cmd0.frt_prio", -1.0); + // hal_set_pin("step_cmd0.rt_prio", -1.0); + // hal_set_pin("step_cmd0.frt_prio", -1.0); + hal_set_pin("sserial0.rt_prio", -1.0); + hal_set_pin("sserial0.frt_prio", -1.0); + hal_set_pin("en0.rt_prio", -1.0); + hal_link_pins("conf0.fb_res", "reslimit0.res"); + switch((protocol_t)hal_get_pin("conf0.cmd_type")) { + case ENC: + hal_link_pins("enc_cmd0.pos", "rev0.in"); + hal_link_pins("conf0.cmd_res", "enc_cmd0.res"); + hal_set_pin("enc_cmd0.rt_prio", 2.0); + hal_set_pin("enc_cmd0.frt_prio", 1.0); + // hal_set_pin("pderiv0.frt_prio", 2.0); + // hal_set_pin("pderiv0.rt_prio", 5.0); + if(hal_get_pin("conf0.error_out") == 1.0) { //error out using rs485 + hal_set_pin("en0.rt_prio", 15.0); + hal_link_pins("fault0.fault_not", "en0.en"); + hal_set_pin("en0.txen", 1.0); + } + break; + case STEPDIR: + hal_link_pins("step_cmd0.pos", "rev0.in"); + hal_link_pins("conf0.cmd_res", "step_cmd0.res"); + hal_set_pin("step_cmd0.rt_prio", 2.0); + hal_set_pin("step_cmd0.frt_prio", 1.0); + // hal_set_pin("pderiv0.frt_prio", 2.0); + // hal_set_pin("pderiv0.rt_prio", 5.0); + if(hal_get_pin("conf0.error_out") == 1.0) { //error out using rs485 + hal_set_pin("en0.rt_prio", 15.0); + hal_link_pins("fault0.fault_not", "en0.en"); + hal_set_pin("en0.txen", 1.0); + } + break; + case SSERIAL: + hal_link_pins("sserial0.pos_cmd", "vel_int0.pos_in"); + hal_link_pins("sserial0.pos_cmd_d", "vel_int0.vel_in"); + hal_link_pins("sserial0.enable", "net0.enable"); + hal_link_pins("fault0.fault", "sserial0.fault"); + hal_link_pins("sserial0.connected", "fault0.cmd_ready"); + hal_link_pins("sserial0.out0", "fault0.brake_release"); + hal_link_pins("rev1.out", "sserial0.pos_fb"); + //this breaks cmd rev... + hal_link_pins("vel_int0.pos_out", "reslimit0.pos_in"); + hal_link_pins("reslimit0.pos_out", "net0.cmd"); + hal_link_pins("vel_int0.vel_out", "net0.cmd_d"); + hal_set_pin("vel_int0.wd", 0.002); //TODO: this depends on linuxcnc servo thread period + //TODO: handle error of vel_int + hal_set_pin("sserial0.rt_prio", 2.0); + hal_set_pin("sserial0.frt_prio", 2.0); + hal_set_pin("vel_int0.rt_prio", 2.1); + hal_set_pin("reslimit0.rt_prio", 2.2); + break; + default: + return -1; + } + return 0; } diff --git a/src/setup.c b/src/setup.c index d6fd9eb9..79888fde 100644 --- a/src/setup.c +++ b/src/setup.c @@ -9,166 +9,166 @@ #include "setup.h" #include "usbd_cdc_if.h" -void setup(){ - //Enable clocks - //TODO: small f4 does not have GPIOE - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_DMA1 | RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_CRC, ENABLE); +void setup() { + //Enable clocks + //TODO: small f4 does not have GPIOE + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_DMA1 | RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_CRC, ENABLE); - NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); + NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); - setup_res(); - usb_init(); - - GPIO_InitTypeDef GPIO_InitStructure; - // messpin - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; - - //fan - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_0; - GPIO_Init(GPIOD, &GPIO_InitStructure); + setup_res(); + usb_init(); - // systick timer - RCC_GetClocksFreq(&RCC_Clocks); - SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000); - //systick prio + GPIO_InitTypeDef GPIO_InitStructure; + // messpin + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; - NVIC_SetPriority(SysTick_IRQn, 14); + //fan + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_0; + GPIO_Init(GPIOD, &GPIO_InitStructure); + + // systick timer + RCC_GetClocksFreq(&RCC_Clocks); + SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000); + //systick prio + + NVIC_SetPriority(SysTick_IRQn, 14); } // Setup Resolver Interface // master timer triggers ADC1,ADC2 via OC, and slave timer via TRGO at 1.2MHz // slave timer OC generates resolver reference signal at 10kHz, phase can be adjusted by oc value // DMA2 moves ADC_ANZ samples to memory, generates transfer complete interrupt at 5kHz -void setup_res(){ - TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; - TIM_OCInitTypeDef TIM_OCInitStructure; - //master timer - RCC_APB1PeriphClockCmd(TIM_MASTER_RCC, ENABLE); - TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; - TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; - TIM_TimeBaseStructure.TIM_Period = 70 - 1;// 84e6 / 70 = 1.2MHz - TIM_TimeBaseStructure.TIM_Prescaler = 0; - TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; - TIM_TimeBaseInit(TIM_MASTER, &TIM_TimeBaseStructure); - TIM_ARRPreloadConfig(TIM_MASTER,ENABLE); - TIM_SelectOutputTrigger(TIM_MASTER, TIM_TRGOSource_Update);// trigger ADC - - //oc for adc trigger - TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; - TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; - TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; - TIM_OCInitStructure.TIM_Pulse = 1; - TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; - TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; - //ADC trigger OC depends on timer - TIM_MASTER_ADC_OC_INIT(TIM_MASTER, &TIM_OCInitStructure); - TIM_MASTER_ADC_OC_PRELOAD(TIM_MASTER, TIM_OCPreload_Enable); - TIM_CtrlPWMOutputs(TIM_MASTER, ENABLE); - - //slave timer - RCC_APB1PeriphClockCmd(TIM_SLAVE_RCC, ENABLE); - TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; - TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; - TIM_TimeBaseStructure.TIM_Period = 60 - 1;// 1.2e6 / 60 = 20kHz - TIM_TimeBaseStructure.TIM_Prescaler = 0; - TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; - TIM_TimeBaseInit(TIM_SLAVE, &TIM_TimeBaseStructure); - TIM_SelectSlaveMode(TIM_SLAVE, TIM_SlaveMode_External1);//Rising edges of the selected trigger (TRGI) clock the counter - TIM_ITRxExternalClockConfig(TIM_SLAVE, TIM_SLAVE_ITR);// clk = TIM_MASTER trigger out - TIM_ARRPreloadConfig(TIM_SLAVE,ENABLE); - - TIM_Cmd(TIM_SLAVE, ENABLE); - - /* ADC clock enable */ - RCC_APB2PeriphClockCmd(SIN_ADC_RCC | COS_ADC_RCC, ENABLE); +void setup_res() { + TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; + TIM_OCInitTypeDef TIM_OCInitStructure; + //master timer + RCC_APB1PeriphClockCmd(TIM_MASTER_RCC, ENABLE); + TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; + TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; + TIM_TimeBaseStructure.TIM_Period = 70 - 1; // 84e6 / 70 = 1.2MHz + TIM_TimeBaseStructure.TIM_Prescaler = 0; + TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; + TIM_TimeBaseInit(TIM_MASTER, &TIM_TimeBaseStructure); + TIM_ARRPreloadConfig(TIM_MASTER, ENABLE); + TIM_SelectOutputTrigger(TIM_MASTER, TIM_TRGOSource_Update); // trigger ADC - //Analog pin configuration - GPIO_InitTypeDef GPIO_InitStructure; - GPIO_InitStructure.GPIO_Pin = SIN_PIN; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; - GPIO_Init(SIN_PORT,&GPIO_InitStructure); + //oc for adc trigger + TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; + TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; + TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; + TIM_OCInitStructure.TIM_Pulse = 1; + TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; + TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; + //ADC trigger OC depends on timer + TIM_MASTER_ADC_OC_INIT(TIM_MASTER, &TIM_OCInitStructure); + TIM_MASTER_ADC_OC_PRELOAD(TIM_MASTER, TIM_OCPreload_Enable); + TIM_CtrlPWMOutputs(TIM_MASTER, ENABLE); - GPIO_InitStructure.GPIO_Pin = COS_PIN; - GPIO_Init(COS_PORT,&GPIO_InitStructure); + //slave timer + RCC_APB1PeriphClockCmd(TIM_SLAVE_RCC, ENABLE); + TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; + TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; + TIM_TimeBaseStructure.TIM_Period = 60 - 1; // 1.2e6 / 60 = 20kHz + TIM_TimeBaseStructure.TIM_Prescaler = 0; + TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; + TIM_TimeBaseInit(TIM_SLAVE, &TIM_TimeBaseStructure); + TIM_SelectSlaveMode(TIM_SLAVE, TIM_SlaveMode_External1); //Rising edges of the selected trigger (TRGI) clock the counter + TIM_ITRxExternalClockConfig(TIM_SLAVE, TIM_SLAVE_ITR); // clk = TIM_MASTER trigger out + TIM_ARRPreloadConfig(TIM_SLAVE, ENABLE); - //ADC structure configuration - ADC_DeInit(); - ADC_InitTypeDef ADC_InitStructure; - ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//data converted will be shifted to right - ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//Input voltage is converted into a 12bit number giving a maximum value of 4096 - ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //the conversion is continuous, the input data is converted more than once - ADC_InitStructure.ADC_ExternalTrigConv = TIM_MASTER_ADC;//trigger on rising edge of TIM_MASTER oc - ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; - ADC_InitStructure.ADC_NbrOfConversion = 1;//ADC_ANZ;//I think this one is clear :p - ADC_InitStructure.ADC_ScanConvMode = ENABLE;//The scan is configured in one channel - ADC_Init(SIN_ADC, &ADC_InitStructure);//Initialize ADC with the previous configuration - ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; - ADC_Init(COS_ADC, &ADC_InitStructure);//Initialize ADC with the previous configuration - - ADC_CommonInitTypeDef ADC_CommonInitStructure; - ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult; - ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4; - ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2; - ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; - ADC_CommonInit(&ADC_CommonInitStructure); + TIM_Cmd(TIM_SLAVE, ENABLE); - for(int i = 1;i<=1;i++){ - ADC_RegularChannelConfig(SIN_ADC, SIN_ADC_CHAN, i, RES_SampleTime); - ADC_RegularChannelConfig(COS_ADC, COS_ADC_CHAN, i, RES_SampleTime); - } + /* ADC clock enable */ + RCC_APB2PeriphClockCmd(SIN_ADC_RCC | COS_ADC_RCC, ENABLE); - ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE); + //Analog pin configuration + GPIO_InitTypeDef GPIO_InitStructure; + GPIO_InitStructure.GPIO_Pin = SIN_PIN; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_Init(SIN_PORT, &GPIO_InitStructure); - //Enable ADC conversion - ADC_Cmd(SIN_ADC,ENABLE); - ADC_Cmd(COS_ADC,ENABLE); + GPIO_InitStructure.GPIO_Pin = COS_PIN; + GPIO_Init(COS_PORT, &GPIO_InitStructure); - // DMA-Disable - DMA_Cmd(DMA2_Stream0, DISABLE); - DMA_DeInit(DMA2_Stream0); + //ADC structure configuration + ADC_DeInit(); + ADC_InitTypeDef ADC_InitStructure; + ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //data converted will be shifted to right + ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //Input voltage is converted into a 12bit number giving a maximum value of 4096 + ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //the conversion is continuous, the input data is converted more than once + ADC_InitStructure.ADC_ExternalTrigConv = TIM_MASTER_ADC; //trigger on rising edge of TIM_MASTER oc + ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; + ADC_InitStructure.ADC_NbrOfConversion = 1; //ADC_ANZ;//I think this one is clear :p + ADC_InitStructure.ADC_ScanConvMode = ENABLE; //The scan is configured in one channel + ADC_Init(SIN_ADC, &ADC_InitStructure); //Initialize ADC with the previous configuration + ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; + ADC_Init(COS_ADC, &ADC_InitStructure); //Initialize ADC with the previous configuration - // DMA2-Config - DMA_InitTypeDef DMA_InitStructure; - DMA_InitStructure.DMA_Channel = DMA_Channel_0; - DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC->CDR; - DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC_DMA_Buffer0; - DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; - DMA_InitStructure.DMA_BufferSize = ADC_ANZ * PID_WAVES; - DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; - DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; - DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; - DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; - DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; - DMA_InitStructure.DMA_Priority = DMA_Priority_High; - DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; - DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; - DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; - DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; - //TODO: use double buffer - //DMA_DoubleBufferModeConfig(DMA2_Stream0, (uint32_t)ADC_DMA_Buffer1, DMA_Memory_0); - //DMA_DoubleBufferModeCmd(DMA2_Stream0, ENABLE); - DMA_Init(DMA2_Stream0, &DMA_InitStructure); + ADC_CommonInitTypeDef ADC_CommonInitStructure; + ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult; + ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4; + ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2; + ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; + ADC_CommonInit(&ADC_CommonInitStructure); - NVIC_InitTypeDef NVIC_InitStructure; - //HAL Fast realtime irq 20kHz - NVIC_InitStructure.NVIC_IRQChannel = TIM_SLAVE_IRQ; - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; - NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; - NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; - NVIC_Init(&NVIC_InitStructure); - - //HAL Realtime irq 5kHz - NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn; - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; - NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; - NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; - NVIC_Init(&NVIC_InitStructure); + for(int i = 1; i <= 1; i++) { + ADC_RegularChannelConfig(SIN_ADC, SIN_ADC_CHAN, i, RES_SampleTime); + ADC_RegularChannelConfig(COS_ADC, COS_ADC_CHAN, i, RES_SampleTime); + } - DMA_Cmd(DMA2_Stream0, ENABLE); + ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE); - DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE); + //Enable ADC conversion + ADC_Cmd(SIN_ADC, ENABLE); + ADC_Cmd(COS_ADC, ENABLE); + + // DMA-Disable + DMA_Cmd(DMA2_Stream0, DISABLE); + DMA_DeInit(DMA2_Stream0); + + // DMA2-Config + DMA_InitTypeDef DMA_InitStructure; + DMA_InitStructure.DMA_Channel = DMA_Channel_0; + DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC->CDR; + DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC_DMA_Buffer0; + DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; + DMA_InitStructure.DMA_BufferSize = ADC_ANZ * PID_WAVES; + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; + DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; + DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; + DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; + DMA_InitStructure.DMA_Priority = DMA_Priority_High; + DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; + DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; + DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; + DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; + //TODO: use double buffer + //DMA_DoubleBufferModeConfig(DMA2_Stream0, (uint32_t)ADC_DMA_Buffer1, DMA_Memory_0); + //DMA_DoubleBufferModeCmd(DMA2_Stream0, ENABLE); + DMA_Init(DMA2_Stream0, &DMA_InitStructure); + + NVIC_InitTypeDef NVIC_InitStructure; + //HAL Fast realtime irq 20kHz + NVIC_InitStructure.NVIC_IRQChannel = TIM_SLAVE_IRQ; + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; + NVIC_Init(&NVIC_InitStructure); + + //HAL Realtime irq 5kHz + NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn; + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; + NVIC_Init(&NVIC_InitStructure); + + DMA_Cmd(DMA2_Stream0, ENABLE); + + DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE); } diff --git a/src/stm32f4xx_it.c b/src/stm32f4xx_it.c index c007e90d..d629e6d4 100644 --- a/src/stm32f4xx_it.c +++ b/src/stm32f4xx_it.c @@ -49,9 +49,8 @@ * @param None * @retval None */ -void NMI_Handler(void) -{ - hal_error(NMI); +void NMI_Handler(void) { + hal_error(NMI); } /** @@ -59,8 +58,7 @@ void NMI_Handler(void) * @param None * @retval None */ -void HardFault_Handler(void) -{ +void HardFault_Handler(void) { /* Go to infinite loop when Hard Fault exception occurs */ hal_error(HardFault); } @@ -70,8 +68,7 @@ void HardFault_Handler(void) * @param None * @retval None */ -void MemManage_Handler(void) -{ +void MemManage_Handler(void) { /* Go to infinite loop when Memory Manage exception occurs */ hal_error(MemManage); } @@ -81,8 +78,7 @@ void MemManage_Handler(void) * @param None * @retval None */ -void BusFault_Handler(void) -{ +void BusFault_Handler(void) { /* Go to infinite loop when Bus Fault exception occurs */ hal_error(BusFault); } @@ -92,8 +88,7 @@ void BusFault_Handler(void) * @param None * @retval None */ -void UsageFault_Handler(void) -{ +void UsageFault_Handler(void) { /* Go to infinite loop when Usage Fault exception occurs */ hal_error(UsageFault); } @@ -103,8 +98,7 @@ void UsageFault_Handler(void) * @param None * @retval None */ -void SVC_Handler(void) -{ +void SVC_Handler(void) { } /** @@ -112,8 +106,7 @@ void SVC_Handler(void) * @param None * @retval None */ -void DebugMon_Handler(void) -{ +void DebugMon_Handler(void) { } /** @@ -121,8 +114,7 @@ void DebugMon_Handler(void) * @param None * @retval None */ -void PendSV_Handler(void) -{ +void PendSV_Handler(void) { } /** diff --git a/src/syscalls.c b/src/syscalls.c index 1c95fff9..ab60128a 100644 --- a/src/syscalls.c +++ b/src/syscalls.c @@ -8,85 +8,83 @@ #include "usbd_cdc_if.h" //int __errno; -size_t __malloc_margin = 256; -char *__brkval; +size_t __malloc_margin = 256; +char *__brkval; int _close(int file) { - return 0; + return 0; } int _fstat(int file, struct stat *st) { - return 0; + return 0; } int _isatty(int file) { - return 1; + return 1; } int _lseek(int file, int ptr, int dir) { - return 0; + return 0; } int _open(const char *name, int flags, int mode) { - return -1; + return -1; } /* Register name faking - works in collusion with the linker. */ -register char * stack_ptr asm ("sp"); +register char *stack_ptr asm("sp"); -void *_sbrk_r(struct _reent *r, ptrdiff_t incr) -{ - extern char end; // provided by the linker script +void *_sbrk_r(struct _reent *r, ptrdiff_t incr) { + extern char end; // provided by the linker script - if (__brkval == 0) - __brkval = &end; + if(__brkval == 0) + __brkval = &end; - if (__brkval + incr > (char*)__get_MSP() - __malloc_margin) { - r->_errno = ENOMEM; - return (void*)-1; - } + if(__brkval + incr > (char *)__get_MSP() - __malloc_margin) { + r->_errno = ENOMEM; + return (void *)-1; + } - void *ret = __brkval; - __brkval += incr; + void *ret = __brkval; + __brkval += incr; - return ret; + return ret; } -ssize_t _read(int fd, void *ptr, size_t len) -{ - (void) fd; - while (!usb_rx_buf.len); +ssize_t _read(int fd, void *ptr, size_t len) { + (void)fd; + while(!usb_rx_buf.len) + ; - if (len > usb_rx_buf.len) - len = usb_rx_buf.len; + if(len > usb_rx_buf.len) + len = usb_rx_buf.len; - char *c = (char *) ptr; - for (uint16_t i = 0; i < len; i++) - rb_getc(&usb_rx_buf, c++); + char *c = (char *)ptr; + for(uint16_t i = 0; i < len; i++) + rb_getc(&usb_rx_buf, c++); - return len; + return len; } //TODO: check if connected? -int _write(int fd, const char *ptr, int len) -{ - if(!USB_CDC_is_connected()){ - return 0; - } - char *c = (char *) ptr; - (void) fd; - int sent = 0; +int _write(int fd, const char *ptr, int len) { + if(!USB_CDC_is_connected()) { + return 0; + } + char *c = (char *)ptr; + (void)fd; + int sent = 0; - while (len--) { - // send a queued byte - copy to usb stack buffer - APP_Rx_Buffer[APP_Rx_ptr_in++] = *c; - c++; + while(len--) { + // send a queued byte - copy to usb stack buffer + APP_Rx_Buffer[APP_Rx_ptr_in++] = *c; + c++; - // To avoid buffer overflow - if (APP_Rx_ptr_in >= APP_RX_DATA_SIZE) { - APP_Rx_ptr_in = 0; - } - sent++; - } - return sent; + // To avoid buffer overflow + if(APP_Rx_ptr_in >= APP_RX_DATA_SIZE) { + APP_Rx_ptr_in = 0; + } + sent++; + } + return sent; } diff --git a/src/system_stm32f4xx.c b/src/system_stm32f4xx.c index 133f97e4..3a83d5a2 100644 --- a/src/system_stm32f4xx.c +++ b/src/system_stm32f4xx.c @@ -139,19 +139,19 @@ /*!< Uncomment the following line if you need to relocate your vector Table in Internal SRAM. */ /* #define VECT_TAB_SRAM */ -#define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field. - This value must be a multiple of 0x200. */ +#define VECT_TAB_OFFSET 0x00 /*!< Vector Table base offset field. \ + This value must be a multiple of 0x200. */ /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */ -#define PLL_M 8 -#define PLL_N 336 +#define PLL_M 8 +#define PLL_N 336 /* SYSCLK = PLL_VCO / PLL_P */ -#define PLL_P 2 +#define PLL_P 2 /* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */ -#define PLL_Q 7 +#define PLL_Q 7 /** * @} @@ -169,9 +169,9 @@ * @{ */ - uint32_t SystemCoreClock = 168000000; +uint32_t SystemCoreClock = 168000000; - __I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}; +__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}; /** * @} @@ -183,7 +183,7 @@ static void SetSysClock(void); #ifdef DATA_IN_ExtSRAM - static void SystemInit_ExtMemCtl(void); +static void SystemInit_ExtMemCtl(void); #endif /* DATA_IN_ExtSRAM */ /** @@ -201,9 +201,8 @@ static void SetSysClock(void); * @param None * @retval None */ -void SystemInit(void) -{ - SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* Set CP10 and CP11 to full access */ +void SystemInit(void) { + SCB->CPACR |= ((3UL << 10 * 2) | (3UL << 11 * 2)); /* Set CP10 and CP11 to full access */ /* Reset the RCC clock configuration to the default reset state ------------*/ /* Set HSION bit */ @@ -232,7 +231,7 @@ void SystemInit(void) AHB/APBx prescalers and Flash settings ----------------------------------*/ SetSysClock(); - /* Configure the Vector Table location add offset address ------------------*/ +/* Configure the Vector Table location add offset address ------------------*/ #ifdef VECT_TAB_SRAM SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ #else @@ -276,41 +275,36 @@ void SystemInit(void) * @param None * @retval None */ -void SystemCoreClockUpdate(void) -{ +void SystemCoreClockUpdate(void) { uint32_t tmp = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2; /* Get SYSCLK source -------------------------------------------------------*/ tmp = RCC->CFGR & RCC_CFGR_SWS; - switch (tmp) - { - case 0x00: /* HSI used as system clock source */ + switch(tmp) { + case 0x00: /* HSI used as system clock source */ SystemCoreClock = HSI_VALUE; break; - case 0x04: /* HSE used as system clock source */ + case 0x04: /* HSE used as system clock source */ SystemCoreClock = HSE_VALUE; break; - case 0x08: /* PLL P used as system clock source */ + case 0x08: /* PLL P used as system clock source */ /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N SYSCLK = PLL_VCO / PLL_P */ pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22; - pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM; + pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM; - if (pllsource != 0) - { + if(pllsource != 0) { /* HSE used as PLL clock source */ pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6); - } - else - { + } else { /* HSI used as PLL clock source */ pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6); } - pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2; - SystemCoreClock = pllvco/pllp; + pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >> 16) + 1) * 2; + SystemCoreClock = pllvco / pllp; break; default: SystemCoreClock = HSI_VALUE; @@ -331,34 +325,28 @@ void SystemCoreClockUpdate(void) * @param None * @retval None */ -static void SetSysClock(void) -{ -/******************************************************************************/ -/* PLL (clocked by HSE) used as System clock source */ -/******************************************************************************/ +static void SetSysClock(void) { + /******************************************************************************/ + /* PLL (clocked by HSE) used as System clock source */ + /******************************************************************************/ __IO uint32_t StartUpCounter = 0, HSEStatus = 0; /* Enable HSE */ RCC->CR |= ((uint32_t)RCC_CR_HSEON); /* Wait till HSE is ready and if Time out is reached exit */ - do - { + do { HSEStatus = RCC->CR & RCC_CR_HSERDY; StartUpCounter++; } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT)); - if ((RCC->CR & RCC_CR_HSERDY) != RESET) - { + if((RCC->CR & RCC_CR_HSERDY) != RESET) { HSEStatus = (uint32_t)0x01; - } - else - { + } else { HSEStatus = (uint32_t)0x00; } - if (HSEStatus == (uint32_t)0x01) - { + if(HSEStatus == (uint32_t)0x01) { /* Enable high performance mode, System frequency up to 168 MHz */ RCC->APB1ENR |= RCC_APB1ENR_PWREN; PWR->CR |= PWR_CR_VOS; @@ -373,34 +361,31 @@ static void SetSysClock(void) RCC->CFGR |= RCC_CFGR_PPRE1_DIV4; /* Configure the main PLL */ - RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) -1) << 16) | - (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24); + RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P >> 1) - 1) << 16) | + (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24); /* Enable the main PLL */ RCC->CR |= RCC_CR_PLLON; /* Wait till the main PLL is ready */ - while((RCC->CR & RCC_CR_PLLRDY) == 0) - { + while((RCC->CR & RCC_CR_PLLRDY) == 0) { } /* Configure Flash prefetch, Instruction cache, Data cache and wait state */ - FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS; + FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_LATENCY_5WS; /* Select the main PLL as system clock source */ - RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW)); + RCC->CFGR &= (uint32_t)((uint32_t) ~(RCC_CFGR_SW)); RCC->CFGR |= RCC_CFGR_SW_PLL; /* Wait till the main PLL is used as system clock source */ - while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS ) != RCC_CFGR_SWS_PLL); + while((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL) + ; { } - } - else - { /* If HSE fails to start-up, the application will have wrong clock + } else { /* If HSE fails to start-up, the application will have wrong clock configuration. User can add here some code to deal with this error */ } - } /** @@ -418,10 +403,9 @@ static void SetSysClock(void) * @param None * @retval None */ -void SystemInit_ExtMemCtl(void) -{ -/*-- GPIOs Configuration -----------------------------------------------------*/ -/* +void SystemInit_ExtMemCtl(void) { + /*-- GPIOs Configuration -----------------------------------------------------*/ + /* +-------------------+--------------------+------------------+------------------+ + SRAM pins assignment + +-------------------+--------------------+------------------+------------------+ @@ -440,66 +424,66 @@ void SystemInit_ExtMemCtl(void) | | PE15 <-> FSMC_D12 | +-------------------+--------------------+ */ - /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */ - RCC->AHB1ENR = 0x00000078; + /* Enable GPIOD, GPIOE, GPIOF and GPIOG interface clock */ + RCC->AHB1ENR = 0x00000078; /* Connect PDx pins to FSMC Alternate function */ - GPIOD->AFR[0] = 0x00cc00cc; - GPIOD->AFR[1] = 0xcc0ccccc; + GPIOD->AFR[0] = 0x00cc00cc; + GPIOD->AFR[1] = 0xcc0ccccc; /* Configure PDx pins in Alternate function mode */ - GPIOD->MODER = 0xaaaa0a0a; + GPIOD->MODER = 0xaaaa0a0a; /* Configure PDx pins speed to 100 MHz */ GPIOD->OSPEEDR = 0xffff0f0f; /* Configure PDx pins Output type to push-pull */ - GPIOD->OTYPER = 0x00000000; + GPIOD->OTYPER = 0x00000000; /* No pull-up, pull-down for PDx pins */ - GPIOD->PUPDR = 0x00000000; + GPIOD->PUPDR = 0x00000000; /* Connect PEx pins to FSMC Alternate function */ - GPIOE->AFR[0] = 0xc00cc0cc; - GPIOE->AFR[1] = 0xcccccccc; + GPIOE->AFR[0] = 0xc00cc0cc; + GPIOE->AFR[1] = 0xcccccccc; /* Configure PEx pins in Alternate function mode */ - GPIOE->MODER = 0xaaaa828a; + GPIOE->MODER = 0xaaaa828a; /* Configure PEx pins speed to 100 MHz */ GPIOE->OSPEEDR = 0xffffc3cf; /* Configure PEx pins Output type to push-pull */ - GPIOE->OTYPER = 0x00000000; + GPIOE->OTYPER = 0x00000000; /* No pull-up, pull-down for PEx pins */ - GPIOE->PUPDR = 0x00000000; + GPIOE->PUPDR = 0x00000000; /* Connect PFx pins to FSMC Alternate function */ - GPIOF->AFR[0] = 0x00cccccc; - GPIOF->AFR[1] = 0xcccc0000; + GPIOF->AFR[0] = 0x00cccccc; + GPIOF->AFR[1] = 0xcccc0000; /* Configure PFx pins in Alternate function mode */ - GPIOF->MODER = 0xaa000aaa; + GPIOF->MODER = 0xaa000aaa; /* Configure PFx pins speed to 100 MHz */ GPIOF->OSPEEDR = 0xff000fff; /* Configure PFx pins Output type to push-pull */ - GPIOF->OTYPER = 0x00000000; + GPIOF->OTYPER = 0x00000000; /* No pull-up, pull-down for PFx pins */ - GPIOF->PUPDR = 0x00000000; + GPIOF->PUPDR = 0x00000000; /* Connect PGx pins to FSMC Alternate function */ - GPIOG->AFR[0] = 0x00cccccc; - GPIOG->AFR[1] = 0x000000c0; + GPIOG->AFR[0] = 0x00cccccc; + GPIOG->AFR[1] = 0x000000c0; /* Configure PGx pins in Alternate function mode */ - GPIOG->MODER = 0x00080aaa; + GPIOG->MODER = 0x00080aaa; /* Configure PGx pins speed to 100 MHz */ GPIOG->OSPEEDR = 0x000c0fff; /* Configure PGx pins Output type to push-pull */ - GPIOG->OTYPER = 0x00000000; + GPIOG->OTYPER = 0x00000000; /* No pull-up, pull-down for PGx pins */ - GPIOG->PUPDR = 0x00000000; + GPIOG->PUPDR = 0x00000000; -/*-- FSMC Configuration ------------------------------------------------------*/ + /*-- FSMC Configuration ------------------------------------------------------*/ /* Enable the FSMC interface clock */ - RCC->AHB3ENR = 0x00000001; + RCC->AHB3ENR = 0x00000001; /* Configure and enable Bank1_SRAM2 */ FSMC_Bank1->BTCR[2] = 0x00001015; - FSMC_Bank1->BTCR[3] = 0x00010603;//0x00010400; + FSMC_Bank1->BTCR[3] = 0x00010603; //0x00010400; FSMC_Bank1E->BWTR[2] = 0x0fffffff; -/* + /* Bank1_SRAM2 is configured as follow: p.FSMC_AddressSetupTime = 3;//0; @@ -526,7 +510,6 @@ void SystemInit_ExtMemCtl(void) FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p; FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p; */ - } #endif /* DATA_IN_ExtSRAM */ diff --git a/src/usb_cdc.c b/src/usb_cdc.c index 942fd587..53c09878 100644 --- a/src/usb_cdc.c +++ b/src/usb_cdc.c @@ -7,91 +7,86 @@ #include "stm32f4xx.h" #include "stm32f4xx_conf.h" -#define RX_QUEUE_SIZE 512 +#define RX_QUEUE_SIZE 512 -struct ringbuf usb_rx_buf = { .buf = (char[RX_QUEUE_SIZE]) {0}, .bufsize = RX_QUEUE_SIZE }; +struct ringbuf usb_rx_buf = {.buf = (char[RX_QUEUE_SIZE]){0}, .bufsize = RX_QUEUE_SIZE}; -static USB_OTG_CORE_HANDLE USB_OTG_dev; +static USB_OTG_CORE_HANDLE USB_OTG_dev; -static uint16_t VCP_Init(void) -{ - return USBD_OK; +static uint16_t VCP_Init(void) { + return USBD_OK; } -static uint16_t VCP_DeInit(void) -{ - return USBD_OK; +static uint16_t VCP_DeInit(void) { + return USBD_OK; } -static uint16_t VCP_Ctrl(uint32_t Cmd, uint8_t *Buf, uint32_t Len) -{ - return USBD_OK; +static uint16_t VCP_Ctrl(uint32_t Cmd, uint8_t *Buf, uint32_t Len) { + return USBD_OK; } // this function is not called -static uint16_t VCP_DataTx(void) -{ - return USBD_OK; +static uint16_t VCP_DataTx(void) { + return USBD_OK; } uint16_t cr_count = 0; -static uint16_t VCP_DataRx(uint8_t *buf, uint32_t len) -{ - for (uint32_t i = 0; i < len; i++) { - if (buf[i] == 0x0A) - cr_count++; - rb_putc(&usb_rx_buf, buf[i]); - } - return USBD_OK; +static uint16_t VCP_DataRx(uint8_t *buf, uint32_t len) { + for(uint32_t i = 0; i < len; i++) { + if(buf[i] == 0x0A) + cr_count++; + rb_putc(&usb_rx_buf, buf[i]); + } + return USBD_OK; } -#define USB_CDC_FIRST_ASCII 32 // erstes Ascii-Zeichen -#define USB_CDC_LAST_ASCII 255 // letztes Ascii-Zeichen -uint16_t USB_VCP_get_string(char *ptr) -{ - uint16_t akt_pos = 0; - char wert = '\0'; +#define USB_CDC_FIRST_ASCII 32 // erstes Ascii-Zeichen +#define USB_CDC_LAST_ASCII 255 // letztes Ascii-Zeichen +uint16_t USB_VCP_get_string(char *ptr) { + uint16_t akt_pos = 0; + char wert = '\0'; - // test ob eine Endekennung empfangen wurde - if (cr_count == 0) return 0; + // test ob eine Endekennung empfangen wurde + if(cr_count == 0) + return 0; - if (usb_rx_buf.len == 0) return 0; + if(usb_rx_buf.len == 0) + return 0; - // kompletten String bis zur Endekennung auslesen - // (oder bis Puffer leer ist) - // es werden nur Ascii-Zeichen übergeben - do { - rb_getc(&usb_rx_buf, &wert); - if ((wert >= USB_CDC_FIRST_ASCII) && (wert <= USB_CDC_LAST_ASCII)) { - *(ptr + akt_pos) = wert; - akt_pos++; - } - } while ((usb_rx_buf.len != 0) && (wert != '\n')); + // kompletten String bis zur Endekennung auslesen + // (oder bis Puffer leer ist) + // es werden nur Ascii-Zeichen übergeben + do { + rb_getc(&usb_rx_buf, &wert); + if((wert >= USB_CDC_FIRST_ASCII) && (wert <= USB_CDC_LAST_ASCII)) { + *(ptr + akt_pos) = wert; + akt_pos++; + } + } while((usb_rx_buf.len != 0) && (wert != '\n')); - // Stringende anhängen - *(ptr + akt_pos) = '\0'; + // Stringende anhängen + *(ptr + akt_pos) = '\0'; - // eine Endekennung wurde bearbeitet - cr_count--; + // eine Endekennung wurde bearbeitet + cr_count--; - return akt_pos; + return akt_pos; } -void USB_VCP_send_string(unsigned char *ptr) -{ - while (*ptr != 0) { - // send a queued byte - copy to usb stack buffer - APP_Rx_Buffer[APP_Rx_ptr_in++] = *ptr; - ptr++; +void USB_VCP_send_string(unsigned char *ptr) { + while(*ptr != 0) { + // send a queued byte - copy to usb stack buffer + APP_Rx_Buffer[APP_Rx_ptr_in++] = *ptr; + ptr++; - // To avoid buffer overflow - if (APP_Rx_ptr_in >= APP_RX_DATA_SIZE) { - APP_Rx_ptr_in = 0; - } + // To avoid buffer overflow + if(APP_Rx_ptr_in >= APP_RX_DATA_SIZE) { + APP_Rx_ptr_in = 0; } + } } const CDC_IF_Prop_TypeDef VCP_fops = { @@ -99,155 +94,132 @@ const CDC_IF_Prop_TypeDef VCP_fops = { .pIf_DeInit = VCP_DeInit, .pIf_Ctrl = VCP_Ctrl, .pIf_DataTx = VCP_DataTx, - .pIf_DataRx = VCP_DataRx -}; + .pIf_DataRx = VCP_DataRx}; -void usb_init(void) -{ - USBD_Init(&USB_OTG_dev, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_CDC_cb, &USR_cb); +void usb_init(void) { + USBD_Init(&USB_OTG_dev, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_CDC_cb, &USR_cb); } -void OTG_FS_IRQHandler(void) -{ - USBD_OTG_ISR_Handler(&USB_OTG_dev); +void OTG_FS_IRQHandler(void) { + USBD_OTG_ISR_Handler(&USB_OTG_dev); } -void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev) -{ - // Enable peripheral clocks - // - RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; - RCC->AHB2ENR |= RCC_APB2ENR_SYSCFGEN; - RCC->AHB2ENR |= RCC_AHB2ENR_OTGFSEN; +void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev) { + // Enable peripheral clocks + // + RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; + RCC->AHB2ENR |= RCC_APB2ENR_SYSCFGEN; + RCC->AHB2ENR |= RCC_AHB2ENR_OTGFSEN; - // enable I/O compensation cell to reduce the I/O noise on power supply - SYSCFG->CMPCR = SYSCFG_CMPCR_CMP_PD; + // enable I/O compensation cell to reduce the I/O noise on power supply + SYSCFG->CMPCR = SYSCFG_CMPCR_CMP_PD; - // Configure DM and DP Pins - // - GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_OTG1_FS); - GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_OTG1_FS); + // Configure DM and DP Pins + // + GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_OTG1_FS); + GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_OTG1_FS); - GPIO_Init(GPIOA, &(GPIO_InitTypeDef) { - .GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12, - .GPIO_Speed = GPIO_Speed_50MHz, - .GPIO_Mode = GPIO_Mode_AF, - .GPIO_OType = GPIO_OType_PP, - .GPIO_PuPd = GPIO_PuPd_NOPULL - }); + GPIO_Init(GPIOA, &(GPIO_InitTypeDef){.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12, .GPIO_Speed = GPIO_Speed_50MHz, .GPIO_Mode = GPIO_Mode_AF, .GPIO_OType = GPIO_OType_PP, .GPIO_PuPd = GPIO_PuPd_NOPULL}); } -void USB_OTG_BSP_EnableInterrupt(USB_OTG_CORE_HANDLE *pdev) -{ - NVIC_Init(&(NVIC_InitTypeDef) { - .NVIC_IRQChannel = OTG_FS_IRQn, - .NVIC_IRQChannelPreemptionPriority = 15, - .NVIC_IRQChannelSubPriority = 0, - .NVIC_IRQChannelCmd = ENABLE - }); +void USB_OTG_BSP_EnableInterrupt(USB_OTG_CORE_HANDLE *pdev) { + NVIC_Init(&(NVIC_InitTypeDef){ + .NVIC_IRQChannel = OTG_FS_IRQn, + .NVIC_IRQChannelPreemptionPriority = 15, + .NVIC_IRQChannelSubPriority = 0, + .NVIC_IRQChannelCmd = ENABLE}); } -void USB_OTG_BSP_uDelay(const uint32_t usec) -{ - uint32_t count = 0; - const uint32_t utime = (120 * usec / 7); - do { - if (++count > utime) { - return ; - } +void USB_OTG_BSP_uDelay(const uint32_t usec) { + uint32_t count = 0; + const uint32_t utime = (120 * usec / 7); + do { + if(++count > utime) { + return; } - while (1); + } while(1); } -void USB_OTG_BSP_mDelay(const uint32_t msec) -{ - USB_OTG_BSP_uDelay(msec * 1000); +void USB_OTG_BSP_mDelay(const uint32_t msec) { + USB_OTG_BSP_uDelay(msec * 1000); } // USB_Usr enum { - USB_CDC_DETACHED, - USB_CDC_CONNECTED + USB_CDC_DETACHED, + USB_CDC_CONNECTED } E_USB_STAT; uint8_t usb_cdc_status = USB_CDC_DETACHED; -void USBD_USR_Init(void) -{ - usb_cdc_status = USB_CDC_DETACHED; +void USBD_USR_Init(void) { + usb_cdc_status = USB_CDC_DETACHED; } -void USBD_USR_DeviceReset(uint8_t speed) -{ - usb_cdc_status = USB_CDC_DETACHED; +void USBD_USR_DeviceReset(uint8_t speed) { + usb_cdc_status = USB_CDC_DETACHED; } -void USBD_USR_DeviceConfigured (void) -{ - usb_cdc_status = USB_CDC_CONNECTED; +void USBD_USR_DeviceConfigured(void) { + usb_cdc_status = USB_CDC_CONNECTED; } -void USBD_USR_DeviceConnected (void) -{ - usb_cdc_status = USB_CDC_DETACHED; +void USBD_USR_DeviceConnected(void) { + usb_cdc_status = USB_CDC_DETACHED; } -void USBD_USR_DeviceDisconnected (void) -{ - usb_cdc_status = USB_CDC_DETACHED; +void USBD_USR_DeviceDisconnected(void) { + usb_cdc_status = USB_CDC_DETACHED; } -void USBD_USR_DeviceSuspended(void) -{ - usb_cdc_status = USB_CDC_DETACHED; +void USBD_USR_DeviceSuspended(void) { + usb_cdc_status = USB_CDC_DETACHED; } -void USBD_USR_DeviceResumed(void) -{ - usb_cdc_status = USB_CDC_DETACHED; +void USBD_USR_DeviceResumed(void) { + usb_cdc_status = USB_CDC_DETACHED; } USBD_Usr_cb_TypeDef USR_cb = -{ - USBD_USR_Init, - USBD_USR_DeviceReset, - USBD_USR_DeviceConfigured, - USBD_USR_DeviceSuspended, - USBD_USR_DeviceResumed, - USBD_USR_DeviceConnected, - USBD_USR_DeviceDisconnected, + { + USBD_USR_Init, + USBD_USR_DeviceReset, + USBD_USR_DeviceConfigured, + USBD_USR_DeviceSuspended, + USBD_USR_DeviceResumed, + USBD_USR_DeviceConnected, + USBD_USR_DeviceDisconnected, }; -uint8_t USB_CDC_is_connected(void) -{ - return usb_cdc_status; - } +uint8_t USB_CDC_is_connected(void) { + return usb_cdc_status; +} //TODO: implement new term API -void cdc_init(void){} +void cdc_init(void) {} -int cdc_tx(void* data, uint32_t len){ - while (len--) { - // send a queued byte - copy to usb stack buffer - APP_Rx_Buffer[APP_Rx_ptr_in++] = *(uint8_t*)data; - data++; - // To avoid buffer overflow - if (APP_Rx_ptr_in >= APP_RX_DATA_SIZE) { - APP_Rx_ptr_in = 0; - } - } - return len; +int cdc_tx(void *data, uint32_t len) { + while(len--) { + // send a queued byte - copy to usb stack buffer + APP_Rx_Buffer[APP_Rx_ptr_in++] = *(uint8_t *)data; + data++; + // To avoid buffer overflow + if(APP_Rx_ptr_in >= APP_RX_DATA_SIZE) { + APP_Rx_ptr_in = 0; + } + } + return len; } -int cdc_getline(char *ptr, int len){ - return USB_VCP_get_string(ptr); +int cdc_getline(char *ptr, int len) { + return USB_VCP_get_string(ptr); } -int cdc_is_connected(){ - return USB_CDC_is_connected(); +int cdc_is_connected() { + return USB_CDC_is_connected(); } -void cdc_poll(){} +void cdc_poll() {} diff --git a/src/version.c b/src/version.c index 790fef22..861ca1d0 100644 --- a/src/version.c +++ b/src/version.c @@ -1,8 +1,7 @@ #include "version.h" volatile const struct version_info version_info = { - .product_name = "STMBL", - .major = 0, - .minor = 9, - .patch = 8 -}; + .product_name = "STMBL", + .major = 0, + .minor = 9, + .patch = 8}; diff --git a/stm32f103/inc/stm32f10x_conf.h b/stm32f103/inc/stm32f10x_conf.h old mode 100755 new mode 100644 index cbb88198..af6c3b5a --- a/stm32f103/inc/stm32f10x_conf.h +++ b/stm32f103/inc/stm32f10x_conf.h @@ -56,7 +56,7 @@ /* #define USE_FULL_ASSERT 1 */ /* Exported macro ------------------------------------------------------------*/ -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** * @brief The assert_param macro is used for function's parameters check. @@ -65,11 +65,11 @@ * that failed. If expr is true, it returns no value. * @retval None */ - #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) +#define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) /* Exported functions ------------------------------------------------------- */ - void assert_failed(uint8_t* file, uint32_t line); +void assert_failed(uint8_t *file, uint32_t line); #else - #define assert_param(expr) ((void)0) +#define assert_param(expr) ((void)0) #endif /* USE_FULL_ASSERT */ #endif /* __STM32F10x_CONF_H */ diff --git a/stm32f103/inc/stm32f10x_it.h b/stm32f103/inc/stm32f10x_it.h old mode 100755 new mode 100644 index 88902626..d25aaafb --- a/stm32f103/inc/stm32f10x_it.h +++ b/stm32f103/inc/stm32f10x_it.h @@ -17,15 +17,15 @@ * *

© COPYRIGHT 2011 STMicroelectronics

****************************************************************************** - */ + */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __STM32F10x_IT_H #define __STM32F10x_IT_H #ifdef __cplusplus - extern "C" { -#endif +extern "C" { +#endif /* Includes ------------------------------------------------------------------*/ #include "stm32f10x.h" diff --git a/stm32f103/src/main.c b/stm32f103/src/main.c index abab76d1..c39d97e2 100644 --- a/stm32f103/src/main.c +++ b/stm32f103/src/main.c @@ -4,29 +4,29 @@ #include "version.h" #include -#define ARES 4096.0// analog resolution, 12 bit -#define AREF 3.3// analog reference voltage +#define ARES 4096.0 // analog resolution, 12 bit +#define AREF 3.3 // analog reference voltage //iramx v3.1-v3.3 hardware -#define RCUR 0.0181//shunt -#define TPULLUP 10000//iramx temperature pullup +#define RCUR 0.0181 //shunt +#define TPULLUP 10000 //iramx temperature pullup #define R10 10000 #define R11 180 -#define VDIVUP 36000.0//HV div pullup R1,R12 -#define VDIVDOWN 280.0//HV div pulldown R2,R9 +#define VDIVUP 36000.0 //HV div pullup R1,R12 +#define VDIVDOWN 280.0 //HV div pulldown R2,R9 #define PWM_U TIM1->CCR1 #define PWM_V TIM1->CCR2 #define PWM_W TIM1->CCR3 -#define AMP(a) (((a) * AREF / ARES - AREF / (R10 + R11) * R11) / (RCUR * R10) * (R10 + R11)) -#define TEMP(a) (log10f((a) * AREF / ARES * TPULLUP / (AREF - a * AREF / ARES)) * (-53) + 290) +#define AMP(a) (((a)*AREF / ARES - AREF / (R10 + R11) * R11) / (RCUR * R10) * (R10 + R11)) +#define TEMP(a) (log10f((a)*AREF / ARES * TPULLUP / (AREF - a * AREF / ARES)) * (-53) + 290) #define VOLT(a) ((a) / ARES * AREF / VDIVDOWN * (VDIVUP + VDIVDOWN)) -volatile uint16_t ADCConvertedValue[100];//DMA buffer for ADC -volatile uint8_t rxbuf[50];//DMA buffer for UART RX -uint32_t rxpos = 0;//UART rx buffer position +volatile uint16_t ADCConvertedValue[100]; //DMA buffer for ADC +volatile uint8_t rxbuf[50]; //DMA buffer for UART RX +uint32_t rxpos = 0; //UART rx buffer position volatile uint32_t u_cmd = 0; volatile uint32_t v_cmd = 0; @@ -36,40 +36,40 @@ uint32_t u_error = 0; uint32_t v_error = 0; uint32_t w_error = 0; -#define TOFIXEDU32(a) ((uint32_t)((a) * 65536)) +#define TOFIXEDU32(a) ((uint32_t)((a)*65536)) #define TOFLOAT32(a) ((uint32_t)((a) / 65536)) #define TEMP_RES 32 #define TEMP_SCALE (TEMP_RES / ARES) int16_t temp_buf[TEMP_RES]; -float tempb(float i){ - unsigned int x = (int)(i * TEMP_SCALE); - float a = TOFLOAT(temp_buf[x]); - float b = TOFLOAT(temp_buf[x + 1]); - return(a + (b - a) * (i * TEMP_SCALE - x)); +float tempb(float i) { + unsigned int x = (int)(i * TEMP_SCALE); + float a = TOFLOAT(temp_buf[x]); + float b = TOFLOAT(temp_buf[x + 1]); + return (a + (b - a) * (i * TEMP_SCALE - x)); } -volatile uint32_t timeout = 99999; -volatile uint16_t temp_raw = 0; -volatile uint8_t hv_fault = 0; -volatile uint8_t hv_enabled = 0; +volatile uint32_t timeout = 99999; +volatile uint16_t temp_raw = 0; +volatile uint8_t hv_fault = 0; +volatile uint8_t hv_enabled = 0; volatile int32_t hv_fault_count = 0; float volt = 0; -float amp = 0; +float amp = 0; float temp = 0; volatile unsigned int systime = 0; -volatile float u,v,w; +volatile float u, v, w; volatile int uartsend = 0; packet_to_hv_t packet_to_hv; packet_from_hv_t packet_from_hv; int32_t datapos = -1; -TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; -TIM_OCInitTypeDef TIM_OCInitStructure; +TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; +TIM_OCInitTypeDef TIM_OCInitStructure; NVIC_InitTypeDef NVIC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStruct; @@ -77,476 +77,473 @@ ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; DMA_InitTypeDef DMA_InitStructuretx; -void Wait(unsigned int ms){//TODO: systick is not used - volatile unsigned int t = systime + ms; - while(t >= systime){ - } +void Wait(unsigned int ms) { //TODO: systick is not used + volatile unsigned int t = systime + ms; + while(t >= systime) { + } } -void SysTick_Handler(void) -{ - systime++; +void SysTick_Handler(void) { + systime++; } -void RCC_Configuration(void) -{ - RCC_ClocksTypeDef RCC_Clocks; - RCC_GetClocksFreq(&RCC_Clocks); - SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000 - 1); +void RCC_Configuration(void) { + RCC_ClocksTypeDef RCC_Clocks; + RCC_GetClocksFreq(&RCC_Clocks); + SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000 - 1); - RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); + RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); } -void hv_enable(){ - GPIO_SetBits(GPIOB,GPIO_Pin_6); - hv_enabled = 1; +void hv_enable() { + GPIO_SetBits(GPIOB, GPIO_Pin_6); + hv_enabled = 1; } -void hv_disable(){ - GPIO_ResetBits(GPIOB,GPIO_Pin_6); - hv_enabled = 0; +void hv_disable() { + GPIO_ResetBits(GPIOB, GPIO_Pin_6); + hv_enabled = 0; } -void GPIO_Configuration(void) -{ - //LED init - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; - GPIO_Init(GPIOC, &GPIO_InitStructure); +void GPIO_Configuration(void) { + //LED init + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; + GPIO_Init(GPIOC, &GPIO_InitStructure); - //Enable output - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; - GPIO_Init(GPIOB, &GPIO_InitStructure); - //Fault in - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; - GPIO_Init(GPIOB, &GPIO_InitStructure); + //Enable output + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; + GPIO_Init(GPIOB, &GPIO_InitStructure); + //Fault in + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; + GPIO_Init(GPIOB, &GPIO_InitStructure); - //PA5,6,7 sv2 - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; - GPIO_Init(GPIOA, &GPIO_InitStructure); - hv_disable(); + //PA5,6,7 sv2 + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; + GPIO_Init(GPIOA, &GPIO_InitStructure); + hv_disable(); } -void tim2_init(){ - RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); - TIM_TimeBaseStructure.TIM_Period = 480; // 72000000 / 480 = 150kHz - TIM_TimeBaseStructure.TIM_Prescaler = 0; - TIM_TimeBaseStructure.TIM_ClockDivision = 0; - TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; - TIM_TimeBaseStructure.TIM_RepetitionCounter = 1; - TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); - - TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; - TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; - TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; - TIM_OCInitStructure.TIM_Pulse = 240; - TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; - TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; - TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; - TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; +void tim2_init() { + RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); + TIM_TimeBaseStructure.TIM_Period = 480; // 72000000 / 480 = 150kHz + TIM_TimeBaseStructure.TIM_Prescaler = 0; + TIM_TimeBaseStructure.TIM_ClockDivision = 0; + TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; + TIM_TimeBaseStructure.TIM_RepetitionCounter = 1; + TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); - TIM_OC2Init(TIM2, &TIM_OCInitStructure); + TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; + TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; + TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; + TIM_OCInitStructure.TIM_Pulse = 240; + TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; + TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; + TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; + TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; - TIM_Cmd(TIM2, ENABLE); + TIM_OC2Init(TIM2, &TIM_OCInitStructure); - TIM_CtrlPWMOutputs(TIM2, ENABLE); + TIM_Cmd(TIM2, ENABLE); + + TIM_CtrlPWMOutputs(TIM2, ENABLE); } -void tim1_init(){ - RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); +void tim1_init() { + RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); - //TIM1 - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_Init(GPIOA, &GPIO_InitStructure); - //TIM1N - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_Init(GPIOB, &GPIO_InitStructure); + //TIM1 + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(GPIOA, &GPIO_InitStructure); + //TIM1N + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(GPIOB, &GPIO_InitStructure); - /* Channel 1, 2 and 3 Configuration in PWM mode */ - TIM_TimeBaseStructure.TIM_Period = PWM_RES; - TIM_TimeBaseStructure.TIM_Prescaler = 0; - TIM_TimeBaseStructure.TIM_ClockDivision = 0; - TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned3; - TIM_TimeBaseStructure.TIM_RepetitionCounter = 1; - TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); - TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); + /* Channel 1, 2 and 3 Configuration in PWM mode */ + TIM_TimeBaseStructure.TIM_Period = PWM_RES; + TIM_TimeBaseStructure.TIM_Prescaler = 0; + TIM_TimeBaseStructure.TIM_ClockDivision = 0; + TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned3; + TIM_TimeBaseStructure.TIM_RepetitionCounter = 1; + TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); + TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); - /* int NVIC setup */ - NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; - NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; - NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; + /* int NVIC setup */ + NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; + NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; - NVIC_Init(&NVIC_InitStructure); + NVIC_Init(&NVIC_InitStructure); - TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; - TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; - TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; - TIM_OCInitStructure.TIM_Pulse = 0; - TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; - TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; - TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; - TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; + TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; + TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; + TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; + TIM_OCInitStructure.TIM_Pulse = 0; + TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; + TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; + TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; + TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; - TIM_OC1Init(TIM1, &TIM_OCInitStructure); - TIM_OC2Init(TIM1, &TIM_OCInitStructure); - TIM_OC3Init(TIM1, &TIM_OCInitStructure); + TIM_OC1Init(TIM1, &TIM_OCInitStructure); + TIM_OC2Init(TIM1, &TIM_OCInitStructure); + TIM_OC3Init(TIM1, &TIM_OCInitStructure); - TIM_Cmd(TIM1, ENABLE); + TIM_Cmd(TIM1, ENABLE); - TIM_CtrlPWMOutputs(TIM1, ENABLE); + TIM_CtrlPWMOutputs(TIM1, ENABLE); } -void usart_init(){ - RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); - RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); +void usart_init() { + RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); + RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); - //DMA is configured every tx cycle - DMA_InitStructuretx.DMA_PeripheralBaseAddr = (uint32_t)&USART2->DR; - DMA_InitStructuretx.DMA_MemoryBaseAddr = (uint32_t)&packet_from_hv; - DMA_InitStructuretx.DMA_DIR = DMA_DIR_PeripheralDST; - DMA_InitStructuretx.DMA_BufferSize = sizeof(packet_from_hv_t); - DMA_InitStructuretx.DMA_PeripheralInc = DMA_PeripheralInc_Disable; - DMA_InitStructuretx.DMA_MemoryInc = DMA_MemoryInc_Enable; - DMA_InitStructuretx.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; - DMA_InitStructuretx.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; - DMA_InitStructuretx.DMA_Mode = DMA_Mode_Normal; - DMA_InitStructuretx.DMA_Priority = DMA_Priority_High; - DMA_InitStructuretx.DMA_M2M = DMA_M2M_Disable; - - //RX DMA - DMA_DeInit(DMA1_Channel6); - DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART2->DR; - DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&rxbuf; - DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; - DMA_InitStructure.DMA_BufferSize = sizeof(rxbuf); - DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; - DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; - DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; - DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; - DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; - DMA_InitStructure.DMA_Priority = DMA_Priority_High; - DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; - DMA_Init(DMA1_Channel6, &DMA_InitStructure); - DMA_Cmd(DMA1_Channel6, ENABLE); + //DMA is configured every tx cycle + DMA_InitStructuretx.DMA_PeripheralBaseAddr = (uint32_t)&USART2->DR; + DMA_InitStructuretx.DMA_MemoryBaseAddr = (uint32_t)&packet_from_hv; + DMA_InitStructuretx.DMA_DIR = DMA_DIR_PeripheralDST; + DMA_InitStructuretx.DMA_BufferSize = sizeof(packet_from_hv_t); + DMA_InitStructuretx.DMA_PeripheralInc = DMA_PeripheralInc_Disable; + DMA_InitStructuretx.DMA_MemoryInc = DMA_MemoryInc_Enable; + DMA_InitStructuretx.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; + DMA_InitStructuretx.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; + DMA_InitStructuretx.DMA_Mode = DMA_Mode_Normal; + DMA_InitStructuretx.DMA_Priority = DMA_Priority_High; + DMA_InitStructuretx.DMA_M2M = DMA_M2M_Disable; - //USART TX - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; - GPIO_Init(GPIOA, &GPIO_InitStructure); + //RX DMA + DMA_DeInit(DMA1_Channel6); + DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART2->DR; + DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&rxbuf; + DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; + DMA_InitStructure.DMA_BufferSize = sizeof(rxbuf); + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; + DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; + DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; + DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; + DMA_InitStructure.DMA_Priority = DMA_Priority_High; + DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; + DMA_Init(DMA1_Channel6, &DMA_InitStructure); + DMA_Cmd(DMA1_Channel6, ENABLE); - //USART RX - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; - GPIO_Init(GPIOA, &GPIO_InitStructure); + //USART TX + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; + GPIO_Init(GPIOA, &GPIO_InitStructure); - USART_InitStruct.USART_BaudRate = DATABAUD; - USART_InitStruct.USART_WordLength = USART_WordLength_8b; - USART_InitStruct.USART_StopBits = USART_StopBits_1; - USART_InitStruct.USART_Parity = USART_Parity_No; - USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; - USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; + //USART RX + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; + GPIO_Init(GPIOA, &GPIO_InitStructure); - USART_Init(USART2, &USART_InitStruct); - USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE); - USART_DMACmd(USART2, USART_DMAReq_Rx, ENABLE); - USART_Cmd(USART2, ENABLE); + USART_InitStruct.USART_BaudRate = DATABAUD; + USART_InitStruct.USART_WordLength = USART_WordLength_8b; + USART_InitStruct.USART_StopBits = USART_StopBits_1; + USART_InitStruct.USART_Parity = USART_Parity_No; + USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; + USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; + + USART_Init(USART2, &USART_InitStruct); + USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE); + USART_DMACmd(USART2, USART_DMAReq_Rx, ENABLE); + USART_Cmd(USART2, ENABLE); } // Setup ADC -void setup_adc(){ - RCC_ADCCLKConfig(RCC_PCLK2_Div6); // 12MHz - RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); - RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); +void setup_adc() { + RCC_ADCCLKConfig(RCC_PCLK2_Div6); // 12MHz + RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); + RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; - //PINC5 IN15 DC link - //PINC4 IN14 AMP - //PINB0 IN8 temperature - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5; - GPIO_Init(GPIOC, &GPIO_InitStructure); - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; - GPIO_Init(GPIOB, &GPIO_InitStructure); + //PINC5 IN15 DC link + //PINC4 IN14 AMP + //PINB0 IN8 temperature + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5; + GPIO_Init(GPIOC, &GPIO_InitStructure); + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; + GPIO_Init(GPIOB, &GPIO_InitStructure); - DMA_DeInit(DMA1_Channel1); - DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR; - DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADCConvertedValue; - DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; - DMA_InitStructure.DMA_BufferSize = ARRAY_SIZE(ADCConvertedValue); - DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; - DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; - DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; - DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; - DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; - DMA_InitStructure.DMA_Priority = DMA_Priority_High; - DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; - DMA_Init(DMA1_Channel1, &DMA_InitStructure); + DMA_DeInit(DMA1_Channel1); + DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR; + DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADCConvertedValue; + DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; + DMA_InitStructure.DMA_BufferSize = ARRAY_SIZE(ADCConvertedValue); + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; + DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; + DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; + DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; + DMA_InitStructure.DMA_Priority = DMA_Priority_High; + DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; + DMA_Init(DMA1_Channel1, &DMA_InitStructure); - /* Enable DMA1 channel1 */ - DMA_Cmd(DMA1_Channel1, ENABLE); + /* Enable DMA1 channel1 */ + DMA_Cmd(DMA1_Channel1, ENABLE); - /* ADC1 configuration ------------------------------------------------------*/ - ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; - ADC_InitStructure.ADC_ScanConvMode = ENABLE; - ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; - ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2; - ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; - ADC_InitStructure.ADC_NbrOfChannel = 1; - ADC_Init(ADC1, &ADC_InitStructure); - - ADC_ExternalTrigConvCmd(ADC1, ENABLE); + /* ADC1 configuration ------------------------------------------------------*/ + ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; + ADC_InitStructure.ADC_ScanConvMode = ENABLE; + ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; + ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2; + ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; + ADC_InitStructure.ADC_NbrOfChannel = 1; + ADC_Init(ADC1, &ADC_InitStructure); - ADC_TempSensorVrefintCmd(ENABLE); + ADC_ExternalTrigConvCmd(ADC1, ENABLE); - ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_13Cycles5); //amp + ADC_TempSensorVrefintCmd(ENABLE); - /* Enable ADC1 DMA */ - ADC_DMACmd(ADC1, ENABLE); + ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_13Cycles5); //amp - /* Enable ADC1 */ - ADC_Cmd(ADC1, ENABLE); + /* Enable ADC1 DMA */ + ADC_DMACmd(ADC1, ENABLE); - /* Enable ADC1 reset calibration register */ - ADC_ResetCalibration(ADC1); - /* Check the end of ADC1 reset calibration register */ - while(ADC_GetResetCalibrationStatus(ADC1)); + /* Enable ADC1 */ + ADC_Cmd(ADC1, ENABLE); - /* Start ADC1 calibration */ - ADC_StartCalibration(ADC1); - /* Check the end of ADC1 calibration */ - while(ADC_GetCalibrationStatus(ADC1)); + /* Enable ADC1 reset calibration register */ + ADC_ResetCalibration(ADC1); + /* Check the end of ADC1 reset calibration register */ + while(ADC_GetResetCalibrationStatus(ADC1)) + ; - //ADC2, injected mode for voltage and temperatue - ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; - ADC_InitStructure.ADC_ScanConvMode = ENABLE; - ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; - ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; - ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; - ADC_InitStructure.ADC_NbrOfChannel = 2; - ADC_Init(ADC2, &ADC_InitStructure); - - ADC_InjectedSequencerLengthConfig(ADC2, 2); - /* ADC1 injected channel Configuration */ - ADC_InjectedChannelConfig(ADC2, ADC_Channel_15, 1, ADC_SampleTime_71Cycles5); - ADC_InjectedChannelConfig(ADC2, ADC_Channel_8, 2, ADC_SampleTime_71Cycles5); - /* ADC1 injected external trigger configuration */ - ADC_ExternalTrigInjectedConvConfig(ADC2, ADC_ExternalTrigInjecConv_None); - ADC_Cmd(ADC2, ENABLE); - /* Enable ADC1 reset calibration register */ - ADC_ResetCalibration(ADC2); - /* Check the end of ADC1 reset calibration register */ - while(ADC_GetResetCalibrationStatus(ADC2)); + /* Start ADC1 calibration */ + ADC_StartCalibration(ADC1); + /* Check the end of ADC1 calibration */ + while(ADC_GetCalibrationStatus(ADC1)) + ; - /* Start ADC1 calibration */ - ADC_StartCalibration(ADC2); - /* Check the end of ADC1 calibration */ - while(ADC_GetCalibrationStatus(ADC2)); - ADC_SoftwareStartInjectedConvCmd(ADC2, ENABLE); + //ADC2, injected mode for voltage and temperatue + ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; + ADC_InitStructure.ADC_ScanConvMode = ENABLE; + ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; + ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; + ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; + ADC_InitStructure.ADC_NbrOfChannel = 2; + ADC_Init(ADC2, &ADC_InitStructure); + + ADC_InjectedSequencerLengthConfig(ADC2, 2); + /* ADC1 injected channel Configuration */ + ADC_InjectedChannelConfig(ADC2, ADC_Channel_15, 1, ADC_SampleTime_71Cycles5); + ADC_InjectedChannelConfig(ADC2, ADC_Channel_8, 2, ADC_SampleTime_71Cycles5); + /* ADC1 injected external trigger configuration */ + ADC_ExternalTrigInjectedConvConfig(ADC2, ADC_ExternalTrigInjecConv_None); + ADC_Cmd(ADC2, ENABLE); + /* Enable ADC1 reset calibration register */ + ADC_ResetCalibration(ADC2); + /* Check the end of ADC1 reset calibration register */ + while(ADC_GetResetCalibrationStatus(ADC2)) + ; + + /* Start ADC1 calibration */ + ADC_StartCalibration(ADC2); + /* Check the end of ADC1 calibration */ + while(ADC_GetCalibrationStatus(ADC2)) + ; + ADC_SoftwareStartInjectedConvCmd(ADC2, ENABLE); } //TIM1 update interrupt, every PWM cycle -void TIM1_UP_IRQHandler(){ - TIM_ClearITPendingBit(TIM1, TIM_IT_Update); - if(timeout > 30){//disable driver - hv_disable(); - GPIO_SetBits(GPIOC,GPIO_Pin_1);//yellow led on - GPIO_ResetBits(GPIOC,GPIO_Pin_2);//green led off - - PWM_U = 0; - PWM_V = 0; - PWM_W = 0; - - u_cmd = TOFIXEDU32(0.0); - u_error = TOFIXEDU32(0.0); - v_cmd = TOFIXEDU32(0.0); - v_error = TOFIXEDU32(0.0); - w_cmd = TOFIXEDU32(0.0); - w_error = TOFIXEDU32(0.0); - }else{ - GPIO_SetBits(GPIOC,GPIO_Pin_2);//green led on - GPIO_ResetBits(GPIOC,GPIO_Pin_1);//yellow led off - timeout ++; - - u_error += u_cmd; - uint32_t u = TOFLOAT32(u_error); - u_error = MAX(0, u_error - TOFIXEDU32(u)); - PWM_U = u; - - v_error += v_cmd; - uint32_t v = TOFLOAT32(v_error); - v_error = MAX(0, v_error - TOFIXEDU32(v)); - PWM_V = v; - - w_error += w_cmd; - uint32_t w = TOFLOAT32(w_error); - w_error = MAX(0, w_error - TOFIXEDU32(w)); - PWM_W = w; - } +void TIM1_UP_IRQHandler() { + TIM_ClearITPendingBit(TIM1, TIM_IT_Update); + if(timeout > 30) { //disable driver + hv_disable(); + GPIO_SetBits(GPIOC, GPIO_Pin_1); //yellow led on + GPIO_ResetBits(GPIOC, GPIO_Pin_2); //green led off + + PWM_U = 0; + PWM_V = 0; + PWM_W = 0; + + u_cmd = TOFIXEDU32(0.0); + u_error = TOFIXEDU32(0.0); + v_cmd = TOFIXEDU32(0.0); + v_error = TOFIXEDU32(0.0); + w_cmd = TOFIXEDU32(0.0); + w_error = TOFIXEDU32(0.0); + } else { + GPIO_SetBits(GPIOC, GPIO_Pin_2); //green led on + GPIO_ResetBits(GPIOC, GPIO_Pin_1); //yellow led off + timeout++; + + u_error += u_cmd; + uint32_t u = TOFLOAT32(u_error); + u_error = MAX(0, u_error - TOFIXEDU32(u)); + PWM_U = u; + + v_error += v_cmd; + uint32_t v = TOFLOAT32(v_error); + v_error = MAX(0, v_error - TOFIXEDU32(v)); + PWM_V = v; + + w_error += w_cmd; + uint32_t w = TOFLOAT32(w_error); + w_error = MAX(0, w_error - TOFIXEDU32(w)); + PWM_W = w; + } } -int main(void) -{ - NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); - RCC_Configuration(); - GPIO_Configuration(); +int main(void) { + NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); + RCC_Configuration(); + GPIO_Configuration(); - setup_adc(); - tim1_init(); - tim2_init(); - usart_init(); + setup_adc(); + tim1_init(); + tim2_init(); + usart_init(); - PWM_U = 0; - PWM_V = 0; - PWM_W = 0; + PWM_U = 0; + PWM_V = 0; + PWM_W = 0; - packet_from_hv.head.start = 255; - packet_from_hv.head.key = 0; + packet_from_hv.head.start = 255; + packet_from_hv.head.key = 0; - for(int i = 0; i < TEMP_RES; i++){ - temp_buf[i] = TOFIXED(TEMP(i / TEMP_SCALE)); - } + for(int i = 0; i < TEMP_RES; i++) { + temp_buf[i] = TOFIXED(TEMP(i / TEMP_SCALE)); + } - while(1){ - //next received packet will be written to bufferpos - uint32_t bufferpos = sizeof(rxbuf) - DMA_GetCurrDataCounter(DMA1_Channel6); - //how many packets we have the the rx buffer for processing - uint32_t available = (bufferpos - rxpos + sizeof(rxbuf)) % sizeof(rxbuf); - - for(int i = 0;i < available;i++){ - uint16_t buf = rxbuf[(rxpos)%sizeof(rxbuf)]; - if(buf == 255){ //start condition - datapos = 0; - ((uint8_t*)&packet_to_hv)[datapos++] = (uint8_t)buf; - uartsend = 1; - }else if(datapos >= 0 && datapos < sizeof(packet_to_hv_t)){ - ((uint8_t*)&packet_to_hv)[datapos++] = (uint8_t)buf; - } - if(datapos == sizeof(packet_to_hv_t)){//all data received - datapos = -1; - unbuff_packet((packet_header_t*)&packet_to_hv, sizeof(to_hv_t)); - if(packet_to_hv.data.enable == 1){ - hv_enable(); - }else{ - hv_disable(); - } - float ua = packet_to_hv.data.a; - float ub = packet_to_hv.data.b; + while(1) { + //next received packet will be written to bufferpos + uint32_t bufferpos = sizeof(rxbuf) - DMA_GetCurrDataCounter(DMA1_Channel6); + //how many packets we have the the rx buffer for processing + uint32_t available = (bufferpos - rxpos + sizeof(rxbuf)) % sizeof(rxbuf); - float u = 0.0; - float v = 0.0; - float w = 0.0; - - if(packet_to_hv.data.mode == 0){//a,b voltages - u = ua; // inverse clarke - v = - ua / 2.0 + ub / 2.0 * M_SQRT3; - w = - ua / 2.0 - ub / 2.0 * M_SQRT3; - }else if(packet_to_hv.data.mode == 1){//DC, a: -dclink ... +dclink - u = ua / 2.0; - v = -ua / 2.0; - w = 0; - }else if(packet_to_hv.data.mode == 2){//2phase, a,b: -dclink/2 ... +dclink/2 - u = ua; - v = 0; - w = ub; - } - - u += volt / 2.0; - v += volt / 2.0; - w += volt / 2.0; - - if(u < v){ - if(u < w){ - v -= u; - w -= u; - u = 0.0; - } - else{ - u -= w; - v -= w; - w = 0.0; - } - } - else{ - if(v < w){ - u -= v; - w -= v; - v = 0.0; - } - else{ - u -= w; - v -= w; - w = 0.0; - } - } - - u_cmd = TOFIXEDU32(CLAMP(u / volt * PWM_RES, 0, PWM_RES * 0.95)); - v_cmd = TOFIXEDU32(CLAMP(v / volt * PWM_RES, 0, PWM_RES * 0.95)); - w_cmd = TOFIXEDU32(CLAMP(w / volt * PWM_RES, 0, PWM_RES * 0.95)); - - timeout = 0; //reset timeout - } - - rxpos++; - rxpos = rxpos % sizeof(rxbuf); + for(int i = 0; i < available; i++) { + uint16_t buf = rxbuf[(rxpos) % sizeof(rxbuf)]; + if(buf == 255) { //start condition + datapos = 0; + ((uint8_t *)&packet_to_hv)[datapos++] = (uint8_t)buf; + uartsend = 1; + } else if(datapos >= 0 && datapos < sizeof(packet_to_hv_t)) { + ((uint8_t *)&packet_to_hv)[datapos++] = (uint8_t)buf; } - - if(uartsend == 1){ - DMA_DeInit(DMA1_Channel7); - DMA_Init(DMA1_Channel7, &DMA_InitStructuretx); - DMA_Cmd(DMA1_Channel7, ENABLE); - int adcbufferpos; - uint32_t cur_sum = 0; - //next received packet will be written to bufferpos - adcbufferpos = ARRAY_SIZE(ADCConvertedValue) - DMA_GetCurrDataCounter(DMA1_Channel1); - //bufferpos-1 .. bufferpos-1-samples - int samples = 30; - for(int i = 0; i < samples; i++){ - if(adcbufferpos + i >= ARRAY_SIZE(ADCConvertedValue)){ - adcbufferpos = 0; - } - cur_sum += ADCConvertedValue[adcbufferpos+i]; - } - - amp = AMP((float)cur_sum / (float)samples); - volt = VOLT(ADC_GetInjectedConversionValue(ADC2, ADC_InjectedChannel_1)); + if(datapos == sizeof(packet_to_hv_t)) { //all data received + datapos = -1; + unbuff_packet((packet_header_t *)&packet_to_hv, sizeof(to_hv_t)); + if(packet_to_hv.data.enable == 1) { + hv_enable(); + } else { + hv_disable(); + } + float ua = packet_to_hv.data.a; + float ub = packet_to_hv.data.b; - packet_from_hv.data.dc_volt = TOFIXED(volt); - packet_from_hv.data.dc_cur = TOFIXED(amp); - packet_from_hv.data.hv_temp = TOFIXED(temp); - int hv_fault_limit = 10; - if(hv_fault == 1 && hv_enabled == 1){ - if(hv_fault_count < hv_fault_limit){ - hv_fault_count++; - } - }else{ - hv_fault_count = 0; - } - if(hv_fault_count >= hv_fault_limit){ - packet_from_hv.data.hv_fault = 1; - }else{ - packet_from_hv.data.hv_fault = 0; - } + float u = 0.0; + float v = 0.0; + float w = 0.0; - buff_packet((packet_header_t*)&packet_from_hv, sizeof(from_hv_t)); - uartsend = 0; - temp_raw = ADC_GetInjectedConversionValue(ADC2, ADC_InjectedChannel_2); - if(temp_raw < ARES && temp_raw > 0){ - temp = tempb(temp_raw); - } - ADC_SoftwareStartInjectedConvCmd(ADC2, ENABLE); - } - } + if(packet_to_hv.data.mode == 0) { //a,b voltages + u = ua; // inverse clarke + v = -ua / 2.0 + ub / 2.0 * M_SQRT3; + w = -ua / 2.0 - ub / 2.0 * M_SQRT3; + } else if(packet_to_hv.data.mode == 1) { //DC, a: -dclink ... +dclink + u = ua / 2.0; + v = -ua / 2.0; + w = 0; + } else if(packet_to_hv.data.mode == 2) { //2phase, a,b: -dclink/2 ... +dclink/2 + u = ua; + v = 0; + w = ub; + } + + u += volt / 2.0; + v += volt / 2.0; + w += volt / 2.0; + + if(u < v) { + if(u < w) { + v -= u; + w -= u; + u = 0.0; + } else { + u -= w; + v -= w; + w = 0.0; + } + } else { + if(v < w) { + u -= v; + w -= v; + v = 0.0; + } else { + u -= w; + v -= w; + w = 0.0; + } + } + + u_cmd = TOFIXEDU32(CLAMP(u / volt * PWM_RES, 0, PWM_RES * 0.95)); + v_cmd = TOFIXEDU32(CLAMP(v / volt * PWM_RES, 0, PWM_RES * 0.95)); + w_cmd = TOFIXEDU32(CLAMP(w / volt * PWM_RES, 0, PWM_RES * 0.95)); + + timeout = 0; //reset timeout + } + + rxpos++; + rxpos = rxpos % sizeof(rxbuf); + } + + if(uartsend == 1) { + DMA_DeInit(DMA1_Channel7); + DMA_Init(DMA1_Channel7, &DMA_InitStructuretx); + DMA_Cmd(DMA1_Channel7, ENABLE); + int adcbufferpos; + uint32_t cur_sum = 0; + //next received packet will be written to bufferpos + adcbufferpos = ARRAY_SIZE(ADCConvertedValue) - DMA_GetCurrDataCounter(DMA1_Channel1); + //bufferpos-1 .. bufferpos-1-samples + int samples = 30; + for(int i = 0; i < samples; i++) { + if(adcbufferpos + i >= ARRAY_SIZE(ADCConvertedValue)) { + adcbufferpos = 0; + } + cur_sum += ADCConvertedValue[adcbufferpos + i]; + } + + amp = AMP((float)cur_sum / (float)samples); + volt = VOLT(ADC_GetInjectedConversionValue(ADC2, ADC_InjectedChannel_1)); + + packet_from_hv.data.dc_volt = TOFIXED(volt); + packet_from_hv.data.dc_cur = TOFIXED(amp); + packet_from_hv.data.hv_temp = TOFIXED(temp); + int hv_fault_limit = 10; + if(hv_fault == 1 && hv_enabled == 1) { + if(hv_fault_count < hv_fault_limit) { + hv_fault_count++; + } + } else { + hv_fault_count = 0; + } + if(hv_fault_count >= hv_fault_limit) { + packet_from_hv.data.hv_fault = 1; + } else { + packet_from_hv.data.hv_fault = 0; + } + + buff_packet((packet_header_t *)&packet_from_hv, sizeof(from_hv_t)); + uartsend = 0; + temp_raw = ADC_GetInjectedConversionValue(ADC2, ADC_InjectedChannel_2); + if(temp_raw < ARES && temp_raw > 0) { + temp = tempb(temp_raw); + } + ADC_SoftwareStartInjectedConvCmd(ADC2, ENABLE); + } + } } diff --git a/stm32f103/src/version.c b/stm32f103/src/version.c index a00f5ac4..7e4f2ce6 100644 --- a/stm32f103/src/version.c +++ b/stm32f103/src/version.c @@ -1,8 +1,7 @@ #include "version.h" volatile const struct version_info version_info = { - .product_name = "STMBL-hv-f103", - .major = 0, - .minor = 9, - .patch = 0 -}; + .product_name = "STMBL-hv-f103", + .major = 0, + .minor = 9, + .patch = 0}; diff --git a/stm32f303/inc/adc.h b/stm32f303/inc/adc.h index bd211266..e216970a 100644 --- a/stm32f303/inc/adc.h +++ b/stm32f303/inc/adc.h @@ -45,7 +45,7 @@ #ifndef __adc_H #define __adc_H #ifdef __cplusplus - extern "C" { +extern "C" { #endif /* Includes ------------------------------------------------------------------*/ diff --git a/stm32f303/inc/dac.h b/stm32f303/inc/dac.h index 397a0ebd..18b1c4a6 100644 --- a/stm32f303/inc/dac.h +++ b/stm32f303/inc/dac.h @@ -45,7 +45,7 @@ #ifndef __dac_H #define __dac_H #ifdef __cplusplus - extern "C" { +extern "C" { #endif /* Includes ------------------------------------------------------------------*/ diff --git a/stm32f303/inc/main.h b/stm32f303/inc/main.h index 47ba0940..3af9be31 100644 --- a/stm32f303/inc/main.h +++ b/stm32f303/inc/main.h @@ -43,7 +43,7 @@ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __MAIN_H #define __MAIN_H - /* Includes ------------------------------------------------------------------*/ +/* Includes ------------------------------------------------------------------*/ /* USER CODE BEGIN Includes */ @@ -57,11 +57,11 @@ /** * @} - */ + */ /** * @} -*/ +*/ #endif /* __MAIN_H */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stm32f303/inc/opamp.h b/stm32f303/inc/opamp.h index 91ecae2a..36e93896 100644 --- a/stm32f303/inc/opamp.h +++ b/stm32f303/inc/opamp.h @@ -45,7 +45,7 @@ #ifndef __opamp_H #define __opamp_H #ifdef __cplusplus - extern "C" { +extern "C" { #endif /* Includes ------------------------------------------------------------------*/ diff --git a/stm32f303/inc/stm32f3xx_hal_conf.h b/stm32f303/inc/stm32f3xx_hal_conf.h index a80c9009..4dbc62cc 100644 --- a/stm32f303/inc/stm32f3xx_hal_conf.h +++ b/stm32f303/inc/stm32f3xx_hal_conf.h @@ -30,14 +30,14 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** - */ + */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __STM32F3xx_HAL_CONF_H #define __STM32F3xx_HAL_CONF_H #ifdef __cplusplus - extern "C" { +extern "C" { #endif #include "main.h" @@ -48,8 +48,8 @@ /** * @brief This is the list of modules to be used in the HAL driver */ - -#define HAL_MODULE_ENABLED + +#define HAL_MODULE_ENABLED #define HAL_ADC_MODULE_ENABLED /*#define HAL_CAN_MODULE_ENABLED */ /*#define HAL_CEC_MODULE_ENABLED */ @@ -93,16 +93,16 @@ * This value is used by the RCC HAL module to compute the system frequency * (when HSE is used as system clock source, directly or through the PLL). */ -#if !defined (HSE_VALUE) - #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ +#if !defined(HSE_VALUE) +#define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ #endif /* HSE_VALUE */ /** * @brief In the following line adjust the External High Speed oscillator (HSE) Startup * Timeout value */ -#if !defined (HSE_STARTUP_TIMEOUT) - #define HSE_STARTUP_TIMEOUT ((uint32_t)100) /*!< Time out for HSE start up, in ms */ +#if !defined(HSE_STARTUP_TIMEOUT) +#define HSE_STARTUP_TIMEOUT ((uint32_t)100) /*!< Time out for HSE start up, in ms */ #endif /* HSE_STARTUP_TIMEOUT */ /** @@ -110,38 +110,38 @@ * This value is used by the RCC HAL module to compute the system frequency * (when HSI is used as system clock source, directly or through the PLL). */ -#if !defined (HSI_VALUE) - #define HSI_VALUE ((uint32_t)8000000) /*!< Value of the Internal oscillator in Hz*/ +#if !defined(HSI_VALUE) +#define HSI_VALUE ((uint32_t)8000000) /*!< Value of the Internal oscillator in Hz*/ #endif /* HSI_VALUE */ /** * @brief In the following line adjust the Internal High Speed oscillator (HSI) Startup * Timeout value */ -#if !defined (HSI_STARTUP_TIMEOUT) - #define HSI_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for HSI start up */ -#endif /* HSI_STARTUP_TIMEOUT */ +#if !defined(HSI_STARTUP_TIMEOUT) +#define HSI_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for HSI start up */ +#endif /* HSI_STARTUP_TIMEOUT */ /** * @brief Internal Low Speed oscillator (LSI) value. */ -#if !defined (LSI_VALUE) - #define LSI_VALUE ((uint32_t)40000) -#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz - The real value may vary depending on the variations - in voltage and temperature. */ +#if !defined(LSI_VALUE) +#define LSI_VALUE ((uint32_t)40000) +#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz \ + The real value may vary depending on the variations \ + in voltage and temperature. */ /** * @brief External Low Speed oscillator (LSE) value. */ -#if !defined (LSE_VALUE) - #define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ -#endif /* LSE_VALUE */ +#if !defined(LSE_VALUE) +#define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ +#endif /* LSE_VALUE */ /** * @brief Time out for LSE start up value in ms. */ -#if !defined (LSE_STARTUP_TIMEOUT) - #define LSE_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for LSE start up, in ms */ +#if !defined(LSE_STARTUP_TIMEOUT) +#define LSE_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for LSE start up, in ms */ #endif /* LSE_STARTUP_TIMEOUT */ /** @@ -151,8 +151,8 @@ * - External clock generated through external PLL component on EVAL 303 (based on MCO or crystal) * - External clock not generated on EVAL 373 */ -#if !defined (EXTERNAL_CLOCK_VALUE) - #define EXTERNAL_CLOCK_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz*/ +#if !defined(EXTERNAL_CLOCK_VALUE) +#define EXTERNAL_CLOCK_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz*/ #endif /* EXTERNAL_CLOCK_VALUE */ /* Tip: To avoid modifying this file each time you need to use different HSE, @@ -161,14 +161,14 @@ /* ########################### System Configuration ######################### */ /** * @brief This is the HAL system configuration section - */ + */ -#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ -#define TICK_INT_PRIORITY ((uint32_t)0) /*!< tick interrupt priority (lowest by default) */ -#define USE_RTOS 0 -#define PREFETCH_ENABLE 1 -#define INSTRUCTION_CACHE_ENABLE 0 -#define DATA_CACHE_ENABLE 0 +#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ +#define TICK_INT_PRIORITY ((uint32_t)0) /*!< tick interrupt priority (lowest by default) */ +#define USE_RTOS 0 +#define PREFETCH_ENABLE 1 +#define INSTRUCTION_CACHE_ENABLE 0 +#define DATA_CACHE_ENABLE 0 /* ########################## Assert Selection ############################## */ /** @@ -183,139 +183,139 @@ */ #ifdef HAL_RCC_MODULE_ENABLED - #include "stm32f3xx_hal_rcc.h" +#include "stm32f3xx_hal_rcc.h" #endif /* HAL_RCC_MODULE_ENABLED */ #ifdef HAL_GPIO_MODULE_ENABLED - #include "stm32f3xx_hal_gpio.h" +#include "stm32f3xx_hal_gpio.h" #endif /* HAL_GPIO_MODULE_ENABLED */ #ifdef HAL_DMA_MODULE_ENABLED - #include "stm32f3xx_hal_dma.h" +#include "stm32f3xx_hal_dma.h" #endif /* HAL_DMA_MODULE_ENABLED */ - + #ifdef HAL_CORTEX_MODULE_ENABLED - #include "stm32f3xx_hal_cortex.h" +#include "stm32f3xx_hal_cortex.h" #endif /* HAL_CORTEX_MODULE_ENABLED */ #ifdef HAL_ADC_MODULE_ENABLED - #include "stm32f3xx_hal_adc.h" +#include "stm32f3xx_hal_adc.h" #endif /* HAL_ADC_MODULE_ENABLED */ #ifdef HAL_CAN_MODULE_ENABLED - #include "stm32f3xx_hal_can.h" +#include "stm32f3xx_hal_can.h" #endif /* HAL_CAN_MODULE_ENABLED */ #ifdef HAL_CEC_MODULE_ENABLED - #include "stm32f3xx_hal_cec.h" +#include "stm32f3xx_hal_cec.h" #endif /* HAL_CEC_MODULE_ENABLED */ #ifdef HAL_COMP_MODULE_ENABLED - #include "stm32f3xx_hal_comp.h" +#include "stm32f3xx_hal_comp.h" #endif /* HAL_COMP_MODULE_ENABLED */ #ifdef HAL_CRC_MODULE_ENABLED - #include "stm32f3xx_hal_crc.h" +#include "stm32f3xx_hal_crc.h" #endif /* HAL_CRC_MODULE_ENABLED */ #ifdef HAL_DAC_MODULE_ENABLED - #include "stm32f3xx_hal_dac.h" +#include "stm32f3xx_hal_dac.h" #endif /* HAL_DAC_MODULE_ENABLED */ #ifdef HAL_FLASH_MODULE_ENABLED - #include "stm32f3xx_hal_flash.h" +#include "stm32f3xx_hal_flash.h" #endif /* HAL_FLASH_MODULE_ENABLED */ #ifdef HAL_SRAM_MODULE_ENABLED - #include "stm32f3xx_hal_sram.h" +#include "stm32f3xx_hal_sram.h" #endif /* HAL_SRAM_MODULE_ENABLED */ #ifdef HAL_NOR_MODULE_ENABLED - #include "stm32f3xx_hal_nor.h" +#include "stm32f3xx_hal_nor.h" #endif /* HAL_NOR_MODULE_ENABLED */ #ifdef HAL_NAND_MODULE_ENABLED - #include "stm32f3xx_hal_nand.h" +#include "stm32f3xx_hal_nand.h" #endif /* HAL_NAND_MODULE_ENABLED */ #ifdef HAL_PCCARD_MODULE_ENABLED - #include "stm32f3xx_hal_pccard.h" -#endif /* HAL_PCCARD_MODULE_ENABLED */ +#include "stm32f3xx_hal_pccard.h" +#endif /* HAL_PCCARD_MODULE_ENABLED */ #ifdef HAL_HRTIM_MODULE_ENABLED - #include "stm32f3xx_hal_hrtim.h" +#include "stm32f3xx_hal_hrtim.h" #endif /* HAL_HRTIM_MODULE_ENABLED */ #ifdef HAL_I2C_MODULE_ENABLED - #include "stm32f3xx_hal_i2c.h" +#include "stm32f3xx_hal_i2c.h" #endif /* HAL_I2C_MODULE_ENABLED */ #ifdef HAL_I2S_MODULE_ENABLED - #include "stm32f3xx_hal_i2s.h" +#include "stm32f3xx_hal_i2s.h" #endif /* HAL_I2S_MODULE_ENABLED */ #ifdef HAL_IRDA_MODULE_ENABLED - #include "stm32f3xx_hal_irda.h" +#include "stm32f3xx_hal_irda.h" #endif /* HAL_IRDA_MODULE_ENABLED */ #ifdef HAL_IWDG_MODULE_ENABLED - #include "stm32f3xx_hal_iwdg.h" +#include "stm32f3xx_hal_iwdg.h" #endif /* HAL_IWDG_MODULE_ENABLED */ #ifdef HAL_OPAMP_MODULE_ENABLED - #include "stm32f3xx_hal_opamp.h" +#include "stm32f3xx_hal_opamp.h" #endif /* HAL_OPAMP_MODULE_ENABLED */ #ifdef HAL_PCD_MODULE_ENABLED - #include "stm32f3xx_hal_pcd.h" +#include "stm32f3xx_hal_pcd.h" #endif /* HAL_PCD_MODULE_ENABLED */ #ifdef HAL_PWR_MODULE_ENABLED - #include "stm32f3xx_hal_pwr.h" +#include "stm32f3xx_hal_pwr.h" #endif /* HAL_PWR_MODULE_ENABLED */ #ifdef HAL_RTC_MODULE_ENABLED - #include "stm32f3xx_hal_rtc.h" +#include "stm32f3xx_hal_rtc.h" #endif /* HAL_RTC_MODULE_ENABLED */ #ifdef HAL_SDADC_MODULE_ENABLED - #include "stm32f3xx_hal_sdadc.h" +#include "stm32f3xx_hal_sdadc.h" #endif /* HAL_SDADC_MODULE_ENABLED */ #ifdef HAL_SMARTCARD_MODULE_ENABLED - #include "stm32f3xx_hal_smartcard.h" +#include "stm32f3xx_hal_smartcard.h" #endif /* HAL_SMARTCARD_MODULE_ENABLED */ #ifdef HAL_SMBUS_MODULE_ENABLED - #include "stm32f3xx_hal_smbus.h" +#include "stm32f3xx_hal_smbus.h" #endif /* HAL_SMBUS_MODULE_ENABLED */ #ifdef HAL_SPI_MODULE_ENABLED - #include "stm32f3xx_hal_spi.h" +#include "stm32f3xx_hal_spi.h" #endif /* HAL_SPI_MODULE_ENABLED */ #ifdef HAL_TIM_MODULE_ENABLED - #include "stm32f3xx_hal_tim.h" +#include "stm32f3xx_hal_tim.h" #endif /* HAL_TIM_MODULE_ENABLED */ #ifdef HAL_TSC_MODULE_ENABLED - #include "stm32f3xx_hal_tsc.h" +#include "stm32f3xx_hal_tsc.h" #endif /* HAL_TSC_MODULE_ENABLED */ #ifdef HAL_UART_MODULE_ENABLED - #include "stm32f3xx_hal_uart.h" +#include "stm32f3xx_hal_uart.h" #endif /* HAL_UART_MODULE_ENABLED */ #ifdef HAL_USART_MODULE_ENABLED - #include "stm32f3xx_hal_usart.h" +#include "stm32f3xx_hal_usart.h" #endif /* HAL_USART_MODULE_ENABLED */ #ifdef HAL_WWDG_MODULE_ENABLED - #include "stm32f3xx_hal_wwdg.h" +#include "stm32f3xx_hal_wwdg.h" #endif /* HAL_WWDG_MODULE_ENABLED */ /* Exported macro ------------------------------------------------------------*/ -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** * @brief The assert_param macro is used for function's parameters check. * @param expr: If expr is false, it calls assert_failed function @@ -324,13 +324,13 @@ * If expr is true, it returns no value. * @retval None */ - #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) +#define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) /* Exported functions ------------------------------------------------------- */ - void assert_failed(uint8_t* file, uint32_t line); +void assert_failed(uint8_t *file, uint32_t line); #else - #define assert_param(expr) ((void)0) -#endif /* USE_FULL_ASSERT */ - +#define assert_param(expr) ((void)0) +#endif /* USE_FULL_ASSERT */ + #ifdef __cplusplus } #endif diff --git a/stm32f303/inc/stm32f3xx_it.h b/stm32f303/inc/stm32f3xx_it.h index 5904a2bc..9ff9da4c 100644 --- a/stm32f303/inc/stm32f3xx_it.h +++ b/stm32f303/inc/stm32f3xx_it.h @@ -36,8 +36,8 @@ #define __STM32F3xx_IT_H #ifdef __cplusplus - extern "C" { -#endif +extern "C" { +#endif /* Includes ------------------------------------------------------------------*/ /* Exported types ------------------------------------------------------------*/ diff --git a/stm32f303/inc/tim.h b/stm32f303/inc/tim.h index a319d306..324df3e1 100644 --- a/stm32f303/inc/tim.h +++ b/stm32f303/inc/tim.h @@ -45,7 +45,7 @@ #ifndef __tim_H #define __tim_H #ifdef __cplusplus - extern "C" { +extern "C" { #endif /* Includes ------------------------------------------------------------------*/ @@ -65,9 +65,9 @@ extern TIM_HandleTypeDef htim8; extern void Error_Handler(void); void MX_TIM8_Init(void); - + void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); - + /* USER CODE BEGIN Prototypes */ diff --git a/stm32f303/inc/usb_device.h b/stm32f303/inc/usb_device.h index 5fe97ade..4382b371 100644 --- a/stm32f303/inc/usb_device.h +++ b/stm32f303/inc/usb_device.h @@ -45,7 +45,7 @@ #ifndef __usb_device_H #define __usb_device_H #ifdef __cplusplus - extern "C" { +extern "C" { #endif /* Includes ------------------------------------------------------------------*/ @@ -55,7 +55,7 @@ extern USBD_HandleTypeDef hUsbDeviceFS; -/* USB_Device init function */ +/* USB_Device init function */ void MX_USB_DEVICE_Init(void); #ifdef __cplusplus diff --git a/stm32f303/inc/usbd_cdc_if.h b/stm32f303/inc/usbd_cdc_if.h index 745097d6..39f0eaba 100644 --- a/stm32f303/inc/usbd_cdc_if.h +++ b/stm32f303/inc/usbd_cdc_if.h @@ -46,7 +46,7 @@ #define __USBD_CDC_IF_H #ifdef __cplusplus - extern "C" { +extern "C" { #endif /* Includes ------------------------------------------------------------------*/ #include "usbd_cdc.h" @@ -56,82 +56,82 @@ /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY * @{ */ - + /** @defgroup USBD_CDC_IF * @brief header * @{ - */ + */ /** @defgroup USBD_CDC_IF_Exported_Defines * @{ - */ + */ /* USER CODE BEGIN EXPORTED_DEFINES */ /* USER CODE END EXPORTED_DEFINES */ /** * @} - */ + */ /** @defgroup USBD_CDC_IF_Exported_Types * @{ - */ + */ /* USER CODE BEGIN EXPORTED_TYPES */ /* USER CODE END EXPORTED_TYPES */ /** * @} - */ + */ /** @defgroup USBD_CDC_IF_Exported_Macros * @{ - */ + */ /* USER CODE BEGIN EXPORTED_MACRO */ /* USER CODE END EXPORTED_MACRO */ /** * @} - */ + */ /** @defgroup USBD_AUDIO_IF_Exported_Variables * @{ - */ -extern USBD_CDC_ItfTypeDef USBD_Interface_fops_FS; + */ +extern USBD_CDC_ItfTypeDef USBD_Interface_fops_FS; /* USER CODE BEGIN EXPORTED_VARIABLES */ /* USER CODE END EXPORTED_VARIABLES */ /** * @} - */ + */ /** @defgroup USBD_CDC_IF_Exported_FunctionsPrototype * @{ - */ -uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len); + */ +uint8_t CDC_Transmit_FS(uint8_t *Buf, uint16_t Len); /* USER CODE BEGIN EXPORTED_FUNCTIONS */ //void cdc_init(void); -int cdc_tx(void* data, uint32_t len); +int cdc_tx(void *data, uint32_t len); int cdc_getline(char *ptr, int len); int cdc_is_connected(); void cdc_poll(); /* USER CODE END EXPORTED_FUNCTIONS */ /** * @} - */ + */ /** * @} - */ + */ /** * @} - */ - + */ + #ifdef __cplusplus } #endif - + #endif /* __USBD_CDC_IF_H */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stm32f303/inc/usbd_conf.h b/stm32f303/inc/usbd_conf.h index 72f062eb..d0ed7a16 100644 --- a/stm32f303/inc/usbd_conf.h +++ b/stm32f303/inc/usbd_conf.h @@ -45,7 +45,7 @@ #ifndef __USBD_CONF__H__ #define __USBD_CONF__H__ #ifdef __cplusplus - extern "C" { +extern "C" { #endif /* Includes ------------------------------------------------------------------*/ #include @@ -58,115 +58,117 @@ /** @addtogroup USBD_OTG_DRIVER * @{ */ - + /** @defgroup USBD_CONF * @brief usb otg low level driver configuration file * @{ - */ + */ /** @defgroup USBD_CONF_Exported_Defines * @{ - */ + */ /*---------- -----------*/ -#define USBD_MAX_NUM_INTERFACES 1 +#define USBD_MAX_NUM_INTERFACES 1 /*---------- -----------*/ -#define USBD_MAX_NUM_CONFIGURATION 1 +#define USBD_MAX_NUM_CONFIGURATION 1 /*---------- -----------*/ -#define USBD_MAX_STR_DESC_SIZ 512 +#define USBD_MAX_STR_DESC_SIZ 512 /*---------- -----------*/ -#define USBD_SUPPORT_USER_STRING 0 +#define USBD_SUPPORT_USER_STRING 0 /*---------- -----------*/ -#define USBD_DEBUG_LEVEL 0 +#define USBD_DEBUG_LEVEL 0 /*---------- -----------*/ -#define USBD_SELF_POWERED 1 +#define USBD_SELF_POWERED 1 /*---------- -----------*/ -#define USBD_CDC_INTERVAL 1000 +#define USBD_CDC_INTERVAL 1000 /****************************************/ /* #define for FS and HS identification */ -#define DEVICE_FS 0 +#define DEVICE_FS 0 /** @defgroup USBD_Exported_Macros * @{ - */ + */ -/* Memory management macros */ -#define USBD_malloc (uint32_t *)USBD_static_malloc -#define USBD_free USBD_static_free -#define USBD_memset /* Not used */ -#define USBD_memcpy /* Not used */ +/* Memory management macros */ +#define USBD_malloc (uint32_t *)USBD_static_malloc +#define USBD_free USBD_static_free +#define USBD_memset /* Not used */ +#define USBD_memcpy /* Not used */ -#define USBD_Delay HAL_Delay +#define USBD_Delay HAL_Delay /* For footprint reasons and since only one allocation is handled in the HID 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); +void USBD_static_free(void *p); -/* DEBUG macros */ -#if (USBD_DEBUG_LEVEL > 0) -#define USBD_UsrLog(...) printf(__VA_ARGS__);\ - printf("\n"); +/* DEBUG macros */ +#if(USBD_DEBUG_LEVEL > 0) +#define USBD_UsrLog(...) \ + printf(__VA_ARGS__); \ + printf("\n"); #else -#define USBD_UsrLog(...) -#endif - - -#if (USBD_DEBUG_LEVEL > 1) - -#define USBD_ErrLog(...) printf("ERROR: ") ;\ - printf(__VA_ARGS__);\ - printf("\n"); -#else -#define USBD_ErrLog(...) -#endif - - -#if (USBD_DEBUG_LEVEL > 2) -#define USBD_DbgLog(...) printf("DEBUG : ") ;\ - printf(__VA_ARGS__);\ - printf("\n"); -#else -#define USBD_DbgLog(...) +#define USBD_UsrLog(...) #endif - + + +#if(USBD_DEBUG_LEVEL > 1) + +#define USBD_ErrLog(...) \ + printf("ERROR: "); \ + printf(__VA_ARGS__); \ + printf("\n"); +#else +#define USBD_ErrLog(...) +#endif + + +#if(USBD_DEBUG_LEVEL > 2) +#define USBD_DbgLog(...) \ + printf("DEBUG : "); \ + printf(__VA_ARGS__); \ + printf("\n"); +#else +#define USBD_DbgLog(...) +#endif + /** * @} - */ - - - + */ + + /** * @} - */ + */ /** @defgroup USBD_CONF_Exported_Types * @{ - */ + */ /** * @} - */ + */ /** @defgroup USBD_CONF_Exported_Macros * @{ - */ + */ /** * @} - */ + */ /** @defgroup USBD_CONF_Exported_Variables * @{ - */ + */ /** * @} - */ + */ /** @defgroup USBD_CONF_Exported_FunctionsPrototype * @{ - */ + */ /** * @} - */ + */ #ifdef __cplusplus } #endif @@ -175,10 +177,9 @@ void USBD_static_free(void *p); /** * @} - */ + */ /** * @} - */ + */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - diff --git a/stm32f303/inc/usbd_desc.h b/stm32f303/inc/usbd_desc.h index b627c8c0..21b4872f 100644 --- a/stm32f303/inc/usbd_desc.h +++ b/stm32f303/inc/usbd_desc.h @@ -47,7 +47,7 @@ #define __USBD_DESC__H__ #ifdef __cplusplus - extern "C" { +extern "C" { #endif /* Includes ------------------------------------------------------------------*/ #include "usbd_def.h" @@ -55,11 +55,11 @@ /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY * @{ */ - + /** @defgroup USB_DESC * @brief general defines for the usb device library file * @{ - */ + */ /** @defgroup USB_DESC_Exported_Defines * @{ @@ -67,37 +67,37 @@ /** * @} - */ + */ /** @defgroup USBD_DESC_Exported_TypesDefinitions * @{ */ /** * @} - */ + */ /** @defgroup USBD_DESC_Exported_Macros * @{ - */ + */ /** * @} - */ + */ /** @defgroup USBD_DESC_Exported_Variables * @{ - */ + */ extern USBD_DescriptorsTypeDef FS_Desc; /** * @} - */ + */ /** @defgroup USBD_DESC_Exported_FunctionsPrototype * @{ - */ - + */ + /** * @} - */ + */ #ifdef __cplusplus } #endif @@ -106,9 +106,9 @@ extern USBD_DescriptorsTypeDef FS_Desc; /** * @} - */ + */ /** * @} -*/ +*/ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stm32f303/src/adc.c b/stm32f303/src/adc.c index 770fb626..55edf3c6 100644 --- a/stm32f303/src/adc.c +++ b/stm32f303/src/adc.c @@ -55,72 +55,64 @@ ADC_HandleTypeDef hadc3; ADC_HandleTypeDef hadc4; /* ADC1 init function */ -void MX_ADC1_Init(void) -{ +void MX_ADC1_Init(void) { ADC_ChannelConfTypeDef sConfig; - /**Common config + /**Common config */ - hadc1.Instance = ADC1; - hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; - hadc1.Init.Resolution = ADC_RESOLUTION_12B; - hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE; - hadc1.Init.ContinuousConvMode = DISABLE; + hadc1.Instance = ADC1; + hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; + hadc1.Init.Resolution = ADC_RESOLUTION_12B; + hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE; + hadc1.Init.ContinuousConvMode = DISABLE; hadc1.Init.DiscontinuousConvMode = DISABLE; - hadc1.Init.NbrOfDiscConversion = 1; - hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; - hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T8_TRGO; - hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; - hadc1.Init.NbrOfConversion = 6; + hadc1.Init.NbrOfDiscConversion = 1; + hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; + hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T8_TRGO; + hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc1.Init.NbrOfConversion = 6; hadc1.Init.DMAContinuousRequests = DISABLE; - hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; - hadc1.Init.LowPowerAutoWait = DISABLE; - hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; - if (HAL_ADC_Init(&hadc1) != HAL_OK) - { + hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + hadc1.Init.LowPowerAutoWait = DISABLE; + hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; + if(HAL_ADC_Init(&hadc1) != HAL_OK) { Error_Handler(); } - sConfig.Channel = ADC_CHANNEL_3; // pa2 = opamp1_out = iw (3) - sConfig.Rank = 1; - sConfig.SingleDiff = ADC_SINGLE_ENDED; + sConfig.Channel = ADC_CHANNEL_3; // pa2 = opamp1_out = iw (3) + sConfig.Rank = 1; + sConfig.SingleDiff = ADC_SINGLE_ENDED; sConfig.SamplingTime = ADC_SAMPLETIME_19CYCLES_5; sConfig.OffsetNumber = ADC_OFFSET_NONE; - sConfig.Offset = 0; - if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) - { + sConfig.Offset = 0; + if(HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { Error_Handler(); } sConfig.Rank = 2; - if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) - { + if(HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { Error_Handler(); } - + sConfig.Rank = 3; - if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) - { + if(HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { Error_Handler(); } - + sConfig.Rank = 4; - if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) - { + if(HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { Error_Handler(); } - + sConfig.Rank = 5; - if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) - { + if(HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { Error_Handler(); } - - sConfig.Channel = ADC_CHANNEL_4; // pa3 = uw - sConfig.Rank = 6; + + sConfig.Channel = ADC_CHANNEL_4; // pa3 = uw + sConfig.Rank = 6; sConfig.SamplingTime = ADC_SAMPLETIME_181CYCLES_5; - if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) - { + if(HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { Error_Handler(); } @@ -131,76 +123,67 @@ void MX_ADC1_Init(void) // { // Error_Handler(); // } - } /* ADC2 init function */ -void MX_ADC2_Init(void) -{ +void MX_ADC2_Init(void) { ADC_ChannelConfTypeDef sConfig; // ADC_MultiModeTypeDef multimode; - /**Common config + /**Common config */ - hadc2.Instance = ADC2; - hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; - hadc2.Init.Resolution = ADC_RESOLUTION_12B; - hadc2.Init.ScanConvMode = ADC_SCAN_ENABLE; - hadc2.Init.ContinuousConvMode = DISABLE; - hadc2.Init.DiscontinuousConvMode = DISABLE; - hadc2.Init.NbrOfDiscConversion = 1; - hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; - hadc2.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T8_TRGO; - hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT; - hadc2.Init.NbrOfConversion = 6; - hadc2.Init.DMAContinuousRequests = DISABLE; - hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV; - hadc2.Init.LowPowerAutoWait = DISABLE; - hadc2.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; - if (HAL_ADC_Init(&hadc2) != HAL_OK) - { + hadc2.Instance = ADC2; + hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; + hadc2.Init.Resolution = ADC_RESOLUTION_12B; + hadc2.Init.ScanConvMode = ADC_SCAN_ENABLE; + hadc2.Init.ContinuousConvMode = DISABLE; + hadc2.Init.DiscontinuousConvMode = DISABLE; + hadc2.Init.NbrOfDiscConversion = 1; + hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; + hadc2.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T8_TRGO; + hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc2.Init.NbrOfConversion = 6; + hadc2.Init.DMAContinuousRequests = DISABLE; + hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + hadc2.Init.LowPowerAutoWait = DISABLE; + hadc2.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; + if(HAL_ADC_Init(&hadc2) != HAL_OK) { Error_Handler(); } - sConfig.Channel = ADC_CHANNEL_3; // pa6 = opamp2_out = iu (3) - sConfig.Rank = 1; - sConfig.SingleDiff = ADC_SINGLE_ENDED; + sConfig.Channel = ADC_CHANNEL_3; // pa6 = opamp2_out = iu (3) + sConfig.Rank = 1; + sConfig.SingleDiff = ADC_SINGLE_ENDED; sConfig.SamplingTime = ADC_SAMPLETIME_19CYCLES_5; sConfig.OffsetNumber = ADC_OFFSET_NONE; - sConfig.Offset = 0; - if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) - { - Error_Handler(); - } - - sConfig.Rank = 2; - if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) - { - Error_Handler(); - } - - sConfig.Rank = 3; - if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) - { - Error_Handler(); - } - - sConfig.Rank = 4; - if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) - { - Error_Handler(); - } - - sConfig.Rank = 5; - if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) - { + sConfig.Offset = 0; + if(HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) { Error_Handler(); } - sConfig.Channel = ADC_CHANNEL_2; // pa5 = uv - sConfig.Rank = 6; + sConfig.Rank = 2; + if(HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) { + Error_Handler(); + } + + sConfig.Rank = 3; + if(HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) { + Error_Handler(); + } + + sConfig.Rank = 4; + if(HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) { + Error_Handler(); + } + + sConfig.Rank = 5; + if(HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) { + Error_Handler(); + } + + sConfig.Channel = ADC_CHANNEL_2; // pa5 = uv + sConfig.Rank = 6; sConfig.SamplingTime = ADC_SAMPLETIME_181CYCLES_5; - if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) - { + if(HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) { Error_Handler(); } @@ -213,74 +196,66 @@ void MX_ADC2_Init(void) // } } /* ADC3 init function */ -void MX_ADC3_Init(void) -{ +void MX_ADC3_Init(void) { ADC_ChannelConfTypeDef sConfig; - /**Common config + /**Common config */ - hadc3.Instance = ADC3; - hadc3.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; - hadc3.Init.Resolution = ADC_RESOLUTION_12B; - hadc3.Init.ScanConvMode = ADC_SCAN_ENABLE; - hadc3.Init.ContinuousConvMode = DISABLE; - hadc3.Init.DiscontinuousConvMode = DISABLE; - hadc3.Init.NbrOfDiscConversion = 1; - hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; - hadc3.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T8_TRGO; - hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT; - hadc3.Init.NbrOfConversion = 6; - hadc3.Init.DMAContinuousRequests = DISABLE; - hadc3.Init.EOCSelection = ADC_EOC_SINGLE_CONV; - hadc3.Init.LowPowerAutoWait = DISABLE; - hadc3.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; - if (HAL_ADC_Init(&hadc3) != HAL_OK) - { + hadc3.Instance = ADC3; + hadc3.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; + hadc3.Init.Resolution = ADC_RESOLUTION_12B; + hadc3.Init.ScanConvMode = ADC_SCAN_ENABLE; + hadc3.Init.ContinuousConvMode = DISABLE; + hadc3.Init.DiscontinuousConvMode = DISABLE; + hadc3.Init.NbrOfDiscConversion = 1; + hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; + hadc3.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T8_TRGO; + hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc3.Init.NbrOfConversion = 6; + hadc3.Init.DMAContinuousRequests = DISABLE; + hadc3.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + hadc3.Init.LowPowerAutoWait = DISABLE; + hadc3.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; + if(HAL_ADC_Init(&hadc3) != HAL_OK) { Error_Handler(); } - /**Configure Regular Channel + /**Configure Regular Channel */ - sConfig.Channel = ADC_CHANNEL_1; // pb1 = opamp3_out = iv (1) - sConfig.Rank = 1; - sConfig.SingleDiff = ADC_SINGLE_ENDED; + sConfig.Channel = ADC_CHANNEL_1; // pb1 = opamp3_out = iv (1) + sConfig.Rank = 1; + sConfig.SingleDiff = ADC_SINGLE_ENDED; sConfig.SamplingTime = ADC_SAMPLETIME_19CYCLES_5; sConfig.OffsetNumber = ADC_OFFSET_NONE; - sConfig.Offset = 0; - if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) - { + sConfig.Offset = 0; + if(HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { Error_Handler(); } sConfig.Rank = 2; - if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) - { + if(HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { Error_Handler(); } - + sConfig.Rank = 3; - if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) - { + if(HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { Error_Handler(); } - + sConfig.Rank = 4; - if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) - { + if(HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { Error_Handler(); } - + sConfig.Rank = 5; - if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) - { + if(HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { Error_Handler(); } - - sConfig.Channel = ADC_CHANNEL_5; // pb13 = uu - sConfig.Rank = 6; + + sConfig.Channel = ADC_CHANNEL_5; // pb13 = uu + sConfig.Rank = 6; sConfig.SamplingTime = ADC_SAMPLETIME_181CYCLES_5; - if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) - { + if(HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { Error_Handler(); } // @@ -293,276 +268,249 @@ void MX_ADC3_Init(void) // { // Error_Handler(); // } - } /* ADC4 init function */ -void MX_ADC4_Init(void) -{ +void MX_ADC4_Init(void) { ADC_ChannelConfTypeDef sConfig; // ADC_MultiModeTypeDef multimode; - /**Common config + /**Common config */ - hadc4.Instance = ADC4; - hadc4.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; - hadc4.Init.Resolution = ADC_RESOLUTION_12B; - hadc4.Init.ScanConvMode = ADC_SCAN_ENABLE; - hadc4.Init.ContinuousConvMode = DISABLE; - hadc4.Init.DiscontinuousConvMode = DISABLE; - hadc4.Init.NbrOfDiscConversion = 1; - hadc4.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; - hadc4.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T8_TRGO; - hadc4.Init.DataAlign = ADC_DATAALIGN_RIGHT; - hadc4.Init.NbrOfConversion = 6; - hadc4.Init.DMAContinuousRequests = DISABLE; - hadc4.Init.EOCSelection = ADC_EOC_SINGLE_CONV; - hadc4.Init.LowPowerAutoWait = DISABLE; - hadc4.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; - if (HAL_ADC_Init(&hadc4) != HAL_OK) - { + hadc4.Instance = ADC4; + hadc4.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; + hadc4.Init.Resolution = ADC_RESOLUTION_12B; + hadc4.Init.ScanConvMode = ADC_SCAN_ENABLE; + hadc4.Init.ContinuousConvMode = DISABLE; + hadc4.Init.DiscontinuousConvMode = DISABLE; + hadc4.Init.NbrOfDiscConversion = 1; + hadc4.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; + hadc4.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T8_TRGO; + hadc4.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc4.Init.NbrOfConversion = 6; + hadc4.Init.DMAContinuousRequests = DISABLE; + hadc4.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + hadc4.Init.LowPowerAutoWait = DISABLE; + hadc4.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; + if(HAL_ADC_Init(&hadc4) != HAL_OK) { Error_Handler(); } - sConfig.Channel = ADC_CHANNEL_4; // pb14 = hv_temp - sConfig.Rank = 1; - sConfig.SingleDiff = ADC_SINGLE_ENDED; + sConfig.Channel = ADC_CHANNEL_4; // pb14 = hv_temp + sConfig.Rank = 1; + sConfig.SingleDiff = ADC_SINGLE_ENDED; sConfig.SamplingTime = ADC_SAMPLETIME_19CYCLES_5; sConfig.OffsetNumber = ADC_OFFSET_NONE; - sConfig.Offset = 0; - if (HAL_ADC_ConfigChannel(&hadc4, &sConfig) != HAL_OK) - { - Error_Handler(); - } - - sConfig.Rank = 2; - if (HAL_ADC_ConfigChannel(&hadc4, &sConfig) != HAL_OK) - { - Error_Handler(); - } - - sConfig.Rank = 3; - if (HAL_ADC_ConfigChannel(&hadc4, &sConfig) != HAL_OK) - { - Error_Handler(); - } - - sConfig.Channel = ADC_CHANNEL_5; // pb15 = mot_temp - sConfig.Rank = 4; - if (HAL_ADC_ConfigChannel(&hadc4, &sConfig) != HAL_OK) - { - Error_Handler(); - } - - sConfig.Rank = 5; - if (HAL_ADC_ConfigChannel(&hadc4, &sConfig) != HAL_OK) - { + sConfig.Offset = 0; + if(HAL_ADC_ConfigChannel(&hadc4, &sConfig) != HAL_OK) { Error_Handler(); } - sConfig.Channel = ADC_CHANNEL_3; // pb12 = hv - sConfig.Rank = 6; + sConfig.Rank = 2; + if(HAL_ADC_ConfigChannel(&hadc4, &sConfig) != HAL_OK) { + Error_Handler(); + } + + sConfig.Rank = 3; + if(HAL_ADC_ConfigChannel(&hadc4, &sConfig) != HAL_OK) { + Error_Handler(); + } + + sConfig.Channel = ADC_CHANNEL_5; // pb15 = mot_temp + sConfig.Rank = 4; + if(HAL_ADC_ConfigChannel(&hadc4, &sConfig) != HAL_OK) { + Error_Handler(); + } + + sConfig.Rank = 5; + if(HAL_ADC_ConfigChannel(&hadc4, &sConfig) != HAL_OK) { + Error_Handler(); + } + + sConfig.Channel = ADC_CHANNEL_3; // pb12 = hv + sConfig.Rank = 6; sConfig.SamplingTime = ADC_SAMPLETIME_181CYCLES_5; - if (HAL_ADC_ConfigChannel(&hadc4, &sConfig) != HAL_OK) - { + if(HAL_ADC_ConfigChannel(&hadc4, &sConfig) != HAL_OK) { Error_Handler(); } } -static uint32_t HAL_RCC_ADC12_CLK_ENABLED=0; -static uint32_t HAL_RCC_ADC34_CLK_ENABLED=0; - -void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle) -{ +static uint32_t HAL_RCC_ADC12_CLK_ENABLED = 0; +static uint32_t HAL_RCC_ADC34_CLK_ENABLED = 0; +void HAL_ADC_MspInit(ADC_HandleTypeDef *adcHandle) { GPIO_InitTypeDef GPIO_InitStruct; - if(adcHandle->Instance==ADC1) - { - /* USER CODE BEGIN ADC1_MspInit 0 */ + if(adcHandle->Instance == ADC1) { + /* USER CODE BEGIN ADC1_MspInit 0 */ - /* USER CODE END ADC1_MspInit 0 */ + /* USER CODE END ADC1_MspInit 0 */ /* Peripheral clock enable */ HAL_RCC_ADC12_CLK_ENABLED++; - if(HAL_RCC_ADC12_CLK_ENABLED==1){ + if(HAL_RCC_ADC12_CLK_ENABLED == 1) { __HAL_RCC_ADC12_CLK_ENABLE(); } - + /**ADC1 GPIO Configuration PA1 ------> ADC1_IN2 PA2 ------> ADC1_IN3 PA3 ------> ADC1_IN4 */ - GPIO_InitStruct.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3; + GPIO_InitStruct.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - /* USER CODE BEGIN ADC1_MspInit 1 */ + /* USER CODE BEGIN ADC1_MspInit 1 */ - /* USER CODE END ADC1_MspInit 1 */ - } - else if(adcHandle->Instance==ADC2) - { - /* USER CODE BEGIN ADC2_MspInit 0 */ + /* USER CODE END ADC1_MspInit 1 */ + } else if(adcHandle->Instance == ADC2) { + /* USER CODE BEGIN ADC2_MspInit 0 */ - /* USER CODE END ADC2_MspInit 0 */ + /* USER CODE END ADC2_MspInit 0 */ /* Peripheral clock enable */ HAL_RCC_ADC12_CLK_ENABLED++; - if(HAL_RCC_ADC12_CLK_ENABLED==1){ + if(HAL_RCC_ADC12_CLK_ENABLED == 1) { __HAL_RCC_ADC12_CLK_ENABLE(); } - + /**ADC2 GPIO Configuration PA5 ------> ADC2_IN2 PA6 ------> ADC2_IN3 PA7 ------> ADC2_IN4 */ - GPIO_InitStruct.Pin = GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7; + GPIO_InitStruct.Pin = GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - /* USER CODE BEGIN ADC2_MspInit 1 */ + /* USER CODE BEGIN ADC2_MspInit 1 */ - /* USER CODE END ADC2_MspInit 1 */ - } - else if(adcHandle->Instance==ADC3) - { - /* USER CODE BEGIN ADC3_MspInit 0 */ + /* USER CODE END ADC2_MspInit 1 */ + } else if(adcHandle->Instance == ADC3) { + /* USER CODE BEGIN ADC3_MspInit 0 */ - /* USER CODE END ADC3_MspInit 0 */ + /* USER CODE END ADC3_MspInit 0 */ /* Peripheral clock enable */ HAL_RCC_ADC34_CLK_ENABLED++; - if(HAL_RCC_ADC34_CLK_ENABLED==1){ + if(HAL_RCC_ADC34_CLK_ENABLED == 1) { __HAL_RCC_ADC34_CLK_ENABLE(); } - + /**ADC3 GPIO Configuration PB0 ------> ADC3_IN12 PB1 ------> ADC3_IN1 PB13 ------> ADC3_IN5 */ - GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_13; + GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_13; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - /* USER CODE BEGIN ADC3_MspInit 1 */ + /* USER CODE BEGIN ADC3_MspInit 1 */ - /* USER CODE END ADC3_MspInit 1 */ - } - else if(adcHandle->Instance==ADC4) - { - /* USER CODE BEGIN ADC4_MspInit 0 */ + /* USER CODE END ADC3_MspInit 1 */ + } else if(adcHandle->Instance == ADC4) { + /* USER CODE BEGIN ADC4_MspInit 0 */ - /* USER CODE END ADC4_MspInit 0 */ + /* USER CODE END ADC4_MspInit 0 */ /* Peripheral clock enable */ HAL_RCC_ADC34_CLK_ENABLED++; - if(HAL_RCC_ADC34_CLK_ENABLED==1){ + if(HAL_RCC_ADC34_CLK_ENABLED == 1) { __HAL_RCC_ADC34_CLK_ENABLE(); } - + /**ADC4 GPIO Configuration PB12 ------> ADC4_IN3 PB14 ------> ADC4_IN4 PB15 ------> ADC4_IN5 */ - GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_14 | GPIO_PIN_15; + GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_14 | GPIO_PIN_15; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - /* USER CODE BEGIN ADC4_MspInit 1 */ + /* USER CODE BEGIN ADC4_MspInit 1 */ - /* USER CODE END ADC4_MspInit 1 */ + /* USER CODE END ADC4_MspInit 1 */ } } -void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle) -{ +void HAL_ADC_MspDeInit(ADC_HandleTypeDef *adcHandle) { + if(adcHandle->Instance == ADC1) { + /* USER CODE BEGIN ADC1_MspDeInit 0 */ - if(adcHandle->Instance==ADC1) - { - /* USER CODE BEGIN ADC1_MspDeInit 0 */ - - /* USER CODE END ADC1_MspDeInit 0 */ + /* USER CODE END ADC1_MspDeInit 0 */ /* Peripheral clock disable */ HAL_RCC_ADC12_CLK_ENABLED--; - if(HAL_RCC_ADC12_CLK_ENABLED==0){ + if(HAL_RCC_ADC12_CLK_ENABLED == 0) { __HAL_RCC_ADC12_CLK_DISABLE(); } - + /**ADC1 GPIO Configuration PA3 ------> ADC1_IN4 */ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_3); - /* USER CODE BEGIN ADC1_MspDeInit 1 */ + /* USER CODE BEGIN ADC1_MspDeInit 1 */ - /* USER CODE END ADC1_MspDeInit 1 */ - } - else if(adcHandle->Instance==ADC2) - { - /* USER CODE BEGIN ADC2_MspDeInit 0 */ + /* USER CODE END ADC1_MspDeInit 1 */ + } else if(adcHandle->Instance == ADC2) { + /* USER CODE BEGIN ADC2_MspDeInit 0 */ - /* USER CODE END ADC2_MspDeInit 0 */ + /* USER CODE END ADC2_MspDeInit 0 */ /* Peripheral clock disable */ HAL_RCC_ADC12_CLK_ENABLED--; - if(HAL_RCC_ADC12_CLK_ENABLED==0){ + if(HAL_RCC_ADC12_CLK_ENABLED == 0) { __HAL_RCC_ADC12_CLK_DISABLE(); } - + /**ADC2 GPIO Configuration PA5 ------> ADC2_IN2 */ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5); - /* USER CODE BEGIN ADC2_MspDeInit 1 */ + /* USER CODE BEGIN ADC2_MspDeInit 1 */ - /* USER CODE END ADC2_MspDeInit 1 */ - } - else if(adcHandle->Instance==ADC3) - { - /* USER CODE BEGIN ADC3_MspDeInit 0 */ + /* USER CODE END ADC2_MspDeInit 1 */ + } else if(adcHandle->Instance == ADC3) { + /* USER CODE BEGIN ADC3_MspDeInit 0 */ - /* USER CODE END ADC3_MspDeInit 0 */ + /* USER CODE END ADC3_MspDeInit 0 */ /* Peripheral clock disable */ HAL_RCC_ADC34_CLK_ENABLED--; - if(HAL_RCC_ADC34_CLK_ENABLED==0){ + if(HAL_RCC_ADC34_CLK_ENABLED == 0) { __HAL_RCC_ADC34_CLK_DISABLE(); } - + /**ADC3 GPIO Configuration PB13 ------> ADC3_IN5 */ HAL_GPIO_DeInit(GPIOB, GPIO_PIN_13); - /* USER CODE BEGIN ADC3_MspDeInit 1 */ + /* USER CODE BEGIN ADC3_MspDeInit 1 */ - /* USER CODE END ADC3_MspDeInit 1 */ - } - else if(adcHandle->Instance==ADC4) - { - /* USER CODE BEGIN ADC4_MspDeInit 0 */ + /* USER CODE END ADC3_MspDeInit 1 */ + } else if(adcHandle->Instance == ADC4) { + /* USER CODE BEGIN ADC4_MspDeInit 0 */ - /* USER CODE END ADC4_MspDeInit 0 */ + /* USER CODE END ADC4_MspDeInit 0 */ /* Peripheral clock disable */ HAL_RCC_ADC34_CLK_ENABLED--; - if(HAL_RCC_ADC34_CLK_ENABLED==0){ + if(HAL_RCC_ADC34_CLK_ENABLED == 0) { __HAL_RCC_ADC34_CLK_DISABLE(); } - + /**ADC4 GPIO Configuration PB12 ------> ADC4_IN3 PB14 ------> ADC4_IN4 PB15 ------> ADC4_IN5 */ - HAL_GPIO_DeInit(GPIOB, GPIO_PIN_12|GPIO_PIN_14|GPIO_PIN_15); + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_12 | GPIO_PIN_14 | GPIO_PIN_15); - /* USER CODE BEGIN ADC4_MspDeInit 1 */ + /* USER CODE BEGIN ADC4_MspDeInit 1 */ - /* USER CODE END ADC4_MspDeInit 1 */ + /* USER CODE END ADC4_MspDeInit 1 */ } -} +} /* USER CODE BEGIN 1 */ diff --git a/stm32f303/src/comps/enc.c b/stm32f303/src/comps/enc.c index aacd02d4..912edb2c 100644 --- a/stm32f303/src/comps/enc.c +++ b/stm32f303/src/comps/enc.c @@ -11,73 +11,73 @@ HAL_PIN(pos); HAL_PIN(a); HAL_PIN(b); - TIM_HandleTypeDef htim1; +TIM_HandleTypeDef htim1; -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { // struct enc_ctx_t * ctx = (struct enc_ctx_t *)ctx_ptr; // struct enc_pin_ctx_t * pins = (struct enc_pin_ctx_t *)pin_ptr; __HAL_RCC_TIM1_CLK_ENABLE(); - + /**TIM1 GPIO Configuration PA8 ------> TIM1_CH1 PA9 ------> TIM1_CH2 */ GPIO_InitTypeDef GPIO_InitStruct; - GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_PULLUP; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF6_TIM1; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); TIM_Encoder_InitTypeDef sConfig; TIM_MasterConfigTypeDef sMasterConfig; - - htim1.Instance = TIM1; - htim1.Init.Prescaler = 0; - htim1.Init.CounterMode = TIM_COUNTERMODE_UP; - htim1.Init.Period = 2000; - htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + + htim1.Instance = TIM1; + htim1.Init.Prescaler = 0; + htim1.Init.CounterMode = TIM_COUNTERMODE_UP; + htim1.Init.Period = 2000; + htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim1.Init.RepetitionCounter = 0; // htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; - sConfig.EncoderMode = TIM_ENCODERMODE_TI12;//TIM_ENCODERMODE_TI1?? - sConfig.IC1Polarity = TIM_ICPOLARITY_RISING; + sConfig.EncoderMode = TIM_ENCODERMODE_TI12; //TIM_ENCODERMODE_TI1?? + sConfig.IC1Polarity = TIM_ICPOLARITY_RISING; sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI; sConfig.IC1Prescaler = TIM_ICPSC_DIV1; - sConfig.IC1Filter = 0; - sConfig.IC2Polarity = TIM_ICPOLARITY_RISING; + sConfig.IC1Filter = 0; + sConfig.IC2Polarity = TIM_ICPOLARITY_RISING; sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; sConfig.IC2Prescaler = TIM_ICPSC_DIV1; - sConfig.IC2Filter = 0; + sConfig.IC2Filter = 0; HAL_TIM_Encoder_Init(&htim1, &sConfig); - - sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET; - sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig); - + HAL_TIM_Encoder_Start(&htim1, TIM_CHANNEL_1 | TIM_CHANNEL_2); - } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { // struct enc_ctx_t * ctx = (struct enc_ctx_t *)ctx_ptr; - struct enc_pin_ctx_t * pins = (struct enc_pin_ctx_t *)pin_ptr; - PIN(a) = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_8); - PIN(b) = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_9); + struct enc_pin_ctx_t *pins = (struct enc_pin_ctx_t *)pin_ptr; + + PIN(a) = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_8); + PIN(b) = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_9); PIN(pos) = mod(TIM1->CNT * 2.0f * M_PI / 2000.0f); } hal_comp_t enc_comp_struct = { - .name = "enc", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = 0, - .pin_count = sizeof(struct enc_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "enc", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct enc_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/stm32f303/src/comps/hv.c b/stm32f303/src/comps/hv.c index 5a52c7b9..6e09ad83 100644 --- a/stm32f303/src/comps/hv.c +++ b/stm32f303/src/comps/hv.c @@ -27,37 +27,37 @@ HAL_PIN(enw); //fault output HAL_PIN(fault); -HAL_PIN(min_on); // min on time [s] -HAL_PIN(min_off); // min off time [s] +HAL_PIN(min_on); // min on time [s] +HAL_PIN(min_off); // min off time [s] -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { // struct hv_ctx_t * ctx = (struct hv_ctx_t *)ctx_ptr; - struct hv_pin_ctx_t * pins = (struct hv_pin_ctx_t *)pin_ptr; - PIN(enu) = 1.0; - PIN(env) = 1.0; - PIN(enw) = 1.0; - PIN(min_on) = 0.00000035; + struct hv_pin_ctx_t *pins = (struct hv_pin_ctx_t *)pin_ptr; + + PIN(enu) = 1.0; + PIN(env) = 1.0; + PIN(enw) = 1.0; + PIN(min_on) = 0.00000035; PIN(min_off) = 0.000005; - + GPIO_InitTypeDef GPIO_InitStruct; //PA15 HV EN - GPIO_InitStruct.Pin = GPIO_PIN_15; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Pin = GPIO_PIN_15; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); //PB7 HV FAULT - GPIO_InitStruct.Pin = GPIO_PIN_7; + GPIO_InitStruct.Pin = GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { // struct hv_ctx_t * ctx = (struct hv_ctx_t *)ctx_ptr; - struct hv_pin_ctx_t * pins = (struct hv_pin_ctx_t *)pin_ptr; + struct hv_pin_ctx_t *pins = (struct hv_pin_ctx_t *)pin_ptr; float udc = MAX(PIN(udc), 0.1); //convert voltages to PWM output compare values @@ -65,35 +65,34 @@ static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst int32_t v = (int32_t)(CLAMP(PIN(v), 0.0, udc) / udc * 4800.0); int32_t w = (int32_t)(CLAMP(PIN(w), 0.0, udc) / udc * 4800.0); //convert on and off times to PWM output compare values - int32_t min_on = (int32_t)(4800.0 * 15000.0 * PIN(min_on) + 0.5); + int32_t min_on = (int32_t)(4800.0 * 15000.0 * PIN(min_on) + 0.5); int32_t min_off = (int32_t)(4800.0 * 15000.0 * PIN(min_off) + 0.5); - - if((u > 0 && u < min_on) || (v > 0 && v < min_on) || (w > 0 && w < min_on)){ + + if((u > 0 && u < min_on) || (v > 0 && v < min_on) || (w > 0 && w < min_on)) { u += min_on; v += min_on; w += min_on; } - - if((u > 4800 - min_off) || (v > 4800 - min_off) || (w > 4800 - min_off)){ + + if((u > 4800 - min_off) || (v > 4800 - min_off) || (w > 4800 - min_off)) { u -= min_off; v -= min_off; w -= min_off; } #ifdef PWM_INVERT - PWM_U = 4800-CLAMP(u, 0, 4800 - min_off); - PWM_V = 4800-CLAMP(v, 0, 4800 - min_off); - PWM_W = 4800-CLAMP(w, 0, 4800 - min_off); + PWM_U = 4800 - CLAMP(u, 0, 4800 - min_off); + PWM_V = 4800 - CLAMP(v, 0, 4800 - min_off); + PWM_W = 4800 - CLAMP(w, 0, 4800 - min_off); #else PWM_U = CLAMP(u, 0, 4800 - min_off); PWM_V = CLAMP(v, 0, 4800 - min_off); PWM_W = CLAMP(w, 0, 4800 - min_off); #endif - - if(PIN(hv_temp) < 85.0){ + + if(PIN(hv_temp) < 85.0) { HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, PIN(en) > 0 ? GPIO_PIN_SET : GPIO_PIN_RESET); - } - else{ + } else { HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET); } //TODO: check enable timing on fault pin @@ -101,15 +100,15 @@ static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst } hal_comp_t hv_comp_struct = { - .name = "hv", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = 0, - .pin_count = sizeof(struct hv_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "hv", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct hv_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/stm32f303/src/comps/hvdc.c b/stm32f303/src/comps/hvdc.c index 5f7f3dbd..a5509308 100644 --- a/stm32f303/src/comps/hvdc.c +++ b/stm32f303/src/comps/hvdc.c @@ -14,25 +14,26 @@ HAL_PIN(uq); HAL_PIN(udc); -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { // struct hv_ctx_t * ctx = (struct hv_ctx_t *)ctx_ptr; - struct hvdc_pin_ctx_t * pins = (struct hvdc_pin_ctx_t *)pin_ptr; - float udc = MAX(PIN(udc), 1.0); - int32_t dcpwm = (int32_t)(PIN(uq)/2.0/udc * 4800.0); - PWM_U = CLAMP(2400 + dcpwm , 50, 4750); - PWM_V = CLAMP(2400 - dcpwm , 50, 4750); + struct hvdc_pin_ctx_t *pins = (struct hvdc_pin_ctx_t *)pin_ptr; + + float udc = MAX(PIN(udc), 1.0); + int32_t dcpwm = (int32_t)(PIN(uq) / 2.0 / udc * 4800.0); + PWM_U = CLAMP(2400 + dcpwm, 50, 4750); + PWM_V = CLAMP(2400 - dcpwm, 50, 4750); } hal_comp_t hvdc_comp_struct = { - .name = "hvdc", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = 0, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = 0, - .pin_count = sizeof(struct hvdc_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "hvdc", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = 0, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct hvdc_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/stm32f303/src/comps/io.c b/stm32f303/src/comps/io.c index fafdd80e..fe73c7c8 100644 --- a/stm32f303/src/comps/io.c +++ b/stm32f303/src/comps/io.c @@ -26,127 +26,129 @@ HAL_PIN(mot_temp); uint32_t adc_12_buf[10]; uint32_t adc_34_buf[10]; -struct io_ctx_t{ +struct io_ctx_t { float u_offset; float v_offset; float w_offset; }; -#define ARES 4096.0// analog resolution, 12 bit +#define ARES 4096.0 // analog resolution, 12 bit #define ADC(a) ((a) / ARES * AREF) #define HV_TEMP_PULLUP 10000 -#define HV_R(a) (HV_TEMP_PULLUP / (AREF / (a) - 1)) +#define HV_R(a) (HV_TEMP_PULLUP / (AREF / (a)-1)) #define MOT_TEMP_PULLUP 10000 #define MOT_TEMP_PULLMID 51000 #define MOT_TEMP_PULLDOWN 10000 #define MOT_TEMP_REF 15.26 #define MOT_REF(a) ((a) * (MOT_TEMP_PULLMID + MOT_TEMP_PULLDOWN) / MOT_TEMP_PULLDOWN) -#define MOT_R(a) (MOT_TEMP_PULLUP / (MOT_TEMP_REF / (a) - 1)) +#define MOT_R(a) (MOT_TEMP_PULLUP / (MOT_TEMP_REF / (a)-1)) -#define ARES 4096.0// analog resolution, 12 bit +#define ARES 4096.0 // analog resolution, 12 bit #define VOLT(a) ((a) / (ARES) * (AREF) / (VDIVDOWN) * ((VDIVUP) + (VDIVDOWN))) //#define TEMP(a) (log10f((a) * (AREF) / (ARES) * (TPULLUP) / ((AREF) - (a) * (AREF) / (ARES))) * (-53.0) + 290.0) #define SHUNT_GAIN 16.0 -#define AMP(a, gain) (((a) * AREF / ARES / (gain) - AREF / (SHUNT_PULLUP + SHUNT_SERIE) * SHUNT_SERIE) / (SHUNT * SHUNT_PULLUP) * (SHUNT_PULLUP + SHUNT_SERIE)) +#define AMP(a, gain) (((a)*AREF / ARES / (gain)-AREF / (SHUNT_PULLUP + SHUNT_SERIE) * SHUNT_SERIE) / (SHUNT * SHUNT_PULLUP) * (SHUNT_PULLUP + SHUNT_SERIE)) -float r2temp(float r){ - r = r / 1000; - const int step = 10; +float r2temp(float r) { + r = r / 1000; + const int step = 10; const int start = -10; //-10..100 const float temp[] = {271.7, 158.2, 95.23, 59.07, 37.64, 24.59, 16.43, 11.21, 7.798, 5.518, 3.972, 2.902}; - for(int i = 1; i < ARRAY_SIZE(temp); i++){ - if(temp[i] < r){ + for(int i = 1; i < ARRAY_SIZE(temp); i++) { + if(temp[i] < r) { float a = temp[i - 1]; float b = temp[i]; - return(-(r - b) / (a - b) * step + i * step + start); + return (-(r - b) / (a - b) * step + i * step + start); } } - return(temp[ARRAY_SIZE(temp)] + step); + return (temp[ARRAY_SIZE(temp)] + step); } -static void nrt_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { // struct io_ctx_t * ctx = (struct io_ctx_t *)ctx_ptr; // struct io_pin_ctx_t * pins = (struct io_pin_ctx_t *)pin_ptr; GPIO_InitTypeDef GPIO_InitStruct; //PA8 LED - GPIO_InitStruct.Pin = GPIO_PIN_8; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Pin = GPIO_PIN_8; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - + DMA1_Channel1->CCR &= (uint16_t)(~DMA_CCR_EN); - DMA1_Channel1->CPAR = (uint32_t)&(ADC12_COMMON->CDR); - DMA1_Channel1->CMAR = (uint32_t)adc_12_buf; + DMA1_Channel1->CPAR = (uint32_t) & (ADC12_COMMON->CDR); + DMA1_Channel1->CMAR = (uint32_t)adc_12_buf; DMA1_Channel1->CNDTR = 6; - DMA1_Channel1->CCR = DMA_CCR_MINC | DMA_CCR_PL_0 | DMA_CCR_MSIZE_1 | DMA_CCR_PSIZE_1 | DMA_CCR_CIRC; + DMA1_Channel1->CCR = DMA_CCR_MINC | DMA_CCR_PL_0 | DMA_CCR_MSIZE_1 | DMA_CCR_PSIZE_1 | DMA_CCR_CIRC; ADC1->CFGR |= ADC_CFGR_DMAEN | ADC_CFGR_DMACFG; DMA1_Channel1->CCR |= DMA_CCR_EN; // ADC12_COMMON->CCR |= ADC12_CCR_MDMA_1; DMA2_Channel5->CCR &= (uint16_t)(~DMA_CCR_EN); - DMA2_Channel5->CPAR = (uint32_t)&(ADC34_COMMON->CDR); - DMA2_Channel5->CMAR = (uint32_t)adc_34_buf; + DMA2_Channel5->CPAR = (uint32_t) & (ADC34_COMMON->CDR); + DMA2_Channel5->CMAR = (uint32_t)adc_34_buf; DMA2_Channel5->CNDTR = 6; - DMA2_Channel5->CCR = DMA_CCR_MINC | DMA_CCR_PL_0 | DMA_CCR_MSIZE_1 | DMA_CCR_PSIZE_1 | DMA_CCR_CIRC; + DMA2_Channel5->CCR = DMA_CCR_MINC | DMA_CCR_PL_0 | DMA_CCR_MSIZE_1 | DMA_CCR_PSIZE_1 | DMA_CCR_CIRC; ADC3->CFGR |= ADC_CFGR_DMAEN | ADC_CFGR_DMACFG; DMA2_Channel5->CCR |= DMA_CCR_EN; // ADC34_COMMON->CCR |= ADC34_CCR_MDMA_1; } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct io_ctx_t * ctx = (struct io_ctx_t *)ctx_ptr; - struct io_pin_ctx_t * pins = (struct io_pin_ctx_t *)pin_ptr; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct io_ctx_t *ctx = (struct io_ctx_t *)ctx_ptr; + struct io_pin_ctx_t *pins = (struct io_pin_ctx_t *)pin_ptr; + + while(!(DMA1->ISR & DMA_ISR_TCIF1)) { + } + while(!(DMA2->ISR & DMA_ISR_TCIF5)) { + } - while(!(DMA1->ISR & DMA_ISR_TCIF1)){} - while(!(DMA2->ISR & DMA_ISR_TCIF5)){} - DMA1->IFCR = DMA_IFCR_CTCIF1; DMA2->IFCR = DMA_IFCR_CTCIF5; uint32_t a12 = adc_12_buf[0] + adc_12_buf[1] + adc_12_buf[2] + adc_12_buf[3] + adc_12_buf[4]; uint32_t a34 = adc_34_buf[0] + adc_34_buf[1] + adc_34_buf[2] + adc_34_buf[3] + adc_34_buf[4]; - - if(ctx->u_offset == 0){ + + if(ctx->u_offset == 0) { ctx->w_offset = AMP((float)(a12 & 0xFFFF) / 5.0, SHUNT_GAIN); ctx->u_offset = AMP((float)(a12 >> 16) / 5.0, SHUNT_GAIN); ctx->v_offset = AMP((float)(a34 & 0xFFFF) / 5.0, SHUNT_GAIN); } - - PIN(iw) = -AMP((float)(a12 & 0xFFFF) / 5.0, SHUNT_GAIN) + ctx->w_offset; // 1u - PIN(iu) = -AMP((float)(a12 >> 16) / 5.0, SHUNT_GAIN) + ctx->u_offset; - PIN(iv) = -AMP((float)(a34 & 0xFFFF) / 5.0, SHUNT_GAIN) + ctx->v_offset; - PIN(w) = VOLT(adc_12_buf[5] & 0xFFFF) * 0.05 + PIN(w) * 0.95; // 0.6u - PIN(v) = VOLT(adc_12_buf[5] >> 16) * 0.05 + PIN(v) * 0.95; - PIN(u) = VOLT(adc_34_buf[5] & 0xFFFF) * 0.05 + PIN(u) * 0.95; - PIN(udc) = VOLT(adc_34_buf[5] >> 16) * 0.05 + PIN(udc) * 0.95; + + PIN(iw) = -AMP((float)(a12 & 0xFFFF) / 5.0, SHUNT_GAIN) + ctx->w_offset; // 1u + PIN(iu) = -AMP((float)(a12 >> 16) / 5.0, SHUNT_GAIN) + ctx->u_offset; + PIN(iv) = -AMP((float)(a34 & 0xFFFF) / 5.0, SHUNT_GAIN) + ctx->v_offset; + PIN(w) = VOLT(adc_12_buf[5] & 0xFFFF) * 0.05 + PIN(w) * 0.95; // 0.6u + PIN(v) = VOLT(adc_12_buf[5] >> 16) * 0.05 + PIN(v) * 0.95; + PIN(u) = VOLT(adc_34_buf[5] & 0xFFFF) * 0.05 + PIN(u) * 0.95; + PIN(udc) = VOLT(adc_34_buf[5] >> 16) * 0.05 + PIN(udc) * 0.95; PIN(udc_pwm) = PIN(udc) / 2.0; - - PIN(hv_temp) = r2temp(HV_R(ADC(adc_34_buf[0] >> 16))) * 0.01 + PIN(hv_temp) * 0.99; // 5.5u - PIN(mot_temp) = MOT_R(MOT_REF(ADC(adc_34_buf[3] >> 16))); // 1.4u - HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, PIN(led) > 0 ? GPIO_PIN_SET : GPIO_PIN_RESET); // 0.1u + + PIN(hv_temp) = r2temp(HV_R(ADC(adc_34_buf[0] >> 16))) * 0.01 + PIN(hv_temp) * 0.99; // 5.5u + PIN(mot_temp) = MOT_R(MOT_REF(ADC(adc_34_buf[3] >> 16))); // 1.4u + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, PIN(led) > 0 ? GPIO_PIN_SET : GPIO_PIN_RESET); // 0.1u } hal_comp_t io_comp_struct = { - .name = "io", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .nrt_init = nrt_init, - .rt_start = 0, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct io_ctx_t), - .pin_count = sizeof(struct io_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "io", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct io_ctx_t), + .pin_count = sizeof(struct io_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/stm32f303/src/comps/ls.c b/stm32f303/src/comps/ls.c index 605065ad..c8a9f64e 100644 --- a/stm32f303/src/comps/ls.c +++ b/stm32f303/src/comps/ls.c @@ -10,7 +10,7 @@ extern CRC_HandleTypeDef hcrc; HAL_COMP(ls); -//process data from LS +//process data from LS HAL_PIN(d_cmd); HAL_PIN(q_cmd); HAL_PIN(pos); @@ -54,12 +54,12 @@ HAL_PIN(dma_pos); HAL_PIN(idle); -struct ls_ctx_t{ - uint32_t timeout; - uint32_t tx_addr; - uint8_t send; - volatile packet_to_hv_t packet_to_hv; - volatile packet_from_hv_t packet_from_hv; +struct ls_ctx_t { + uint32_t timeout; + uint32_t tx_addr; + uint8_t send; + volatile packet_to_hv_t packet_to_hv; + volatile packet_from_hv_t packet_from_hv; }; //TODO: move to ctx @@ -69,217 +69,221 @@ struct ls_ctx_t{ f3_config_data_t config; f3_state_data_t state; -static void hw_init(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct ls_ctx_t * ctx = (struct ls_ctx_t *)ctx_ptr; - // struct ls_pin_ctx_t * pins = (struct ls_pin_ctx_t *)pin_ptr; - - GPIO_InitTypeDef GPIO_InitStruct; - - /* Peripheral clock enable */ - __HAL_RCC_USART3_CLK_ENABLE(); - - UART_HandleTypeDef huart3; - huart3.Instance = USART3; - huart3.Init.BaudRate = DATABAUD; - huart3.Init.WordLength = UART_WORDLENGTH_8B; - huart3.Init.StopBits = UART_STOPBITS_1; - huart3.Init.Parity = UART_PARITY_NONE; - huart3.Init.Mode = UART_MODE_TX_RX; - huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; - huart3.Init.OverSampling = UART_OVERSAMPLING_8; - huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; - huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; - USART3->CR3 |= USART_CR3_DMAT | USART_CR3_DMAR | USART_CR3_OVRDIS; - HAL_UART_Init(&huart3); - - /**USART3 GPIO Configuration +static void hw_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct ls_ctx_t *ctx = (struct ls_ctx_t *)ctx_ptr; + // struct ls_pin_ctx_t * pins = (struct ls_pin_ctx_t *)pin_ptr; + + GPIO_InitTypeDef GPIO_InitStruct; + + /* Peripheral clock enable */ + __HAL_RCC_USART3_CLK_ENABLE(); + + UART_HandleTypeDef huart3; + huart3.Instance = USART3; + huart3.Init.BaudRate = DATABAUD; + huart3.Init.WordLength = UART_WORDLENGTH_8B; + huart3.Init.StopBits = UART_STOPBITS_1; + huart3.Init.Parity = UART_PARITY_NONE; + huart3.Init.Mode = UART_MODE_TX_RX; + huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; + huart3.Init.OverSampling = UART_OVERSAMPLING_8; + huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; + huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; + USART3->CR3 |= USART_CR3_DMAT | USART_CR3_DMAR | USART_CR3_OVRDIS; + HAL_UART_Init(&huart3); + + /**USART3 GPIO Configuration PB10 ------> USART3_TX PB11 ------> USART3_RX */ - GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_PULLUP; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; - GPIO_InitStruct.Alternate = GPIO_AF7_USART3; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - __HAL_RCC_DMA1_CLK_ENABLE(); - - //TX DMA - DMA1_Channel2->CCR &= (uint16_t)(~DMA_CCR_EN); - DMA1_Channel2->CPAR = (uint32_t)&(USART3->TDR); - DMA1_Channel2->CMAR = (uint32_t)&(ctx->packet_from_hv); - DMA1_Channel2->CNDTR = sizeof(packet_from_hv_t); - DMA1_Channel2->CCR = DMA_CCR_MINC | DMA_CCR_DIR;// | DMA_CCR_PL_0 | DMA_CCR_PL_1 - DMA1->IFCR = DMA_IFCR_CTCIF2 | DMA_IFCR_CHTIF2 | DMA_IFCR_CGIF2; - - //RX DMA - DMA1_Channel3->CCR &= (uint16_t)(~DMA_CCR_EN); - DMA1_Channel3->CPAR = (uint32_t)&(USART3->RDR); - DMA1_Channel3->CMAR = (uint32_t)&(ctx->packet_to_hv); - DMA1_Channel3->CNDTR = sizeof(packet_to_hv_t); - DMA1_Channel3->CCR = DMA_CCR_MINC;// | DMA_CCR_PL_0 | DMA_CCR_PL_1 - DMA1->IFCR = DMA_IFCR_CTCIF3 | DMA_IFCR_CHTIF3 | DMA_IFCR_CGIF3; - DMA1_Channel3->CCR |= DMA_CCR_EN; - - config.pins.r = 0.0; - config.pins.l = 0.0; - config.pins.psi = 0.0; - config.pins.cur_p = 0.0; - config.pins.cur_i = 0.0; - config.pins.cur_ff = 0.0; - config.pins.cur_ind = 0.0; - config.pins.max_y = 0.0; - config.pins.max_cur = 0.0; - - USART3->RTOR = 16; // 16 bits timeout - USART3->CR2 |= USART_CR2_RTOEN; // timeout en - USART3->ICR |= USART_ICR_RTOCF; // timeout clear flag + GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF7_USART3; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + __HAL_RCC_DMA1_CLK_ENABLE(); + + //TX DMA + DMA1_Channel2->CCR &= (uint16_t)(~DMA_CCR_EN); + DMA1_Channel2->CPAR = (uint32_t) & (USART3->TDR); + DMA1_Channel2->CMAR = (uint32_t) & (ctx->packet_from_hv); + DMA1_Channel2->CNDTR = sizeof(packet_from_hv_t); + DMA1_Channel2->CCR = DMA_CCR_MINC | DMA_CCR_DIR; // | DMA_CCR_PL_0 | DMA_CCR_PL_1 + DMA1->IFCR = DMA_IFCR_CTCIF2 | DMA_IFCR_CHTIF2 | DMA_IFCR_CGIF2; + + //RX DMA + DMA1_Channel3->CCR &= (uint16_t)(~DMA_CCR_EN); + DMA1_Channel3->CPAR = (uint32_t) & (USART3->RDR); + DMA1_Channel3->CMAR = (uint32_t) & (ctx->packet_to_hv); + DMA1_Channel3->CNDTR = sizeof(packet_to_hv_t); + DMA1_Channel3->CCR = DMA_CCR_MINC; // | DMA_CCR_PL_0 | DMA_CCR_PL_1 + DMA1->IFCR = DMA_IFCR_CTCIF3 | DMA_IFCR_CHTIF3 | DMA_IFCR_CGIF3; + DMA1_Channel3->CCR |= DMA_CCR_EN; + + config.pins.r = 0.0; + config.pins.l = 0.0; + config.pins.psi = 0.0; + config.pins.cur_p = 0.0; + config.pins.cur_i = 0.0; + config.pins.cur_ff = 0.0; + config.pins.cur_ind = 0.0; + config.pins.max_y = 0.0; + config.pins.max_cur = 0.0; + + USART3->RTOR = 16; // 16 bits timeout + USART3->CR2 |= USART_CR2_RTOEN; // timeout en + USART3->ICR |= USART_ICR_RTOCF; // timeout clear flag } -static void rt_start(volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct ls_ctx_t * ctx = (struct ls_ctx_t *)ctx_ptr; - struct ls_pin_ctx_t * pins = (struct ls_pin_ctx_t *)pin_ptr; - ctx->timeout = 0; - ctx->tx_addr = 0; - ctx->send = 0; - PIN(crc_error) = 0.0; - PIN(crc_ok) = 0.0; - PIN(timeout) = 0.0; - PIN(idle) = 0.0; +static void rt_start(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct ls_ctx_t *ctx = (struct ls_ctx_t *)ctx_ptr; + struct ls_pin_ctx_t *pins = (struct ls_pin_ctx_t *)pin_ptr; + + ctx->timeout = 0; + ctx->tx_addr = 0; + ctx->send = 0; + PIN(crc_error) = 0.0; + PIN(crc_ok) = 0.0; + PIN(timeout) = 0.0; + PIN(idle) = 0.0; } -static void rt_func(float period, volatile void * ctx_ptr, volatile hal_pin_inst_t * pin_ptr){ - struct ls_ctx_t * ctx = (struct ls_ctx_t *)ctx_ptr; - struct ls_pin_ctx_t * pins = (struct ls_pin_ctx_t *)pin_ptr; +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct ls_ctx_t *ctx = (struct ls_ctx_t *)ctx_ptr; + struct ls_pin_ctx_t *pins = (struct ls_pin_ctx_t *)pin_ptr; - uint32_t dma_pos = sizeof(packet_to_hv_t) - DMA1_Channel3->CNDTR; + uint32_t dma_pos = sizeof(packet_to_hv_t) - DMA1_Channel3->CNDTR; - if(dma_pos == sizeof(packet_to_hv_t)){ - uint32_t crc = HAL_CRC_Calculate(&hcrc, (uint32_t *) &(ctx->packet_to_hv), sizeof(packet_to_hv_t) / 4 - 1); - if(crc == ctx->packet_to_hv.crc){ - PIN(en) = ctx->packet_to_hv.flags.enable; - PIN(phase_mode) = ctx->packet_to_hv.flags.phase_type; - PIN(cmd_mode) = ctx->packet_to_hv.flags.cmd_type; - PIN(d_cmd) = ctx->packet_to_hv.d_cmd; - PIN(q_cmd) = ctx->packet_to_hv.q_cmd; - PIN(pos) = ctx->packet_to_hv.pos; - PIN(vel) = ctx->packet_to_hv.vel; - uint8_t a = ctx->packet_to_hv.addr; - a = CLAMP(a, 0, sizeof(config) / 4); - config.data[a] = ctx->packet_to_hv.value; // TODO: first enable after complete update + if(dma_pos == sizeof(packet_to_hv_t)) { + uint32_t crc = HAL_CRC_Calculate(&hcrc, (uint32_t *)&(ctx->packet_to_hv), sizeof(packet_to_hv_t) / 4 - 1); + if(crc == ctx->packet_to_hv.crc) { + PIN(en) = ctx->packet_to_hv.flags.enable; + PIN(phase_mode) = ctx->packet_to_hv.flags.phase_type; + PIN(cmd_mode) = ctx->packet_to_hv.flags.cmd_type; + PIN(d_cmd) = ctx->packet_to_hv.d_cmd; + PIN(q_cmd) = ctx->packet_to_hv.q_cmd; + PIN(pos) = ctx->packet_to_hv.pos; + PIN(vel) = ctx->packet_to_hv.vel; + uint8_t a = ctx->packet_to_hv.addr; + a = CLAMP(a, 0, sizeof(config) / 4); + config.data[a] = ctx->packet_to_hv.value; // TODO: first enable after complete update - PIN(r) = config.pins.r; - PIN(l) = config.pins.l; - PIN(psi) = config.pins.psi; - PIN(cur_p) = config.pins.cur_p; - PIN(cur_i) = config.pins.cur_i; - PIN(cur_ff) = config.pins.cur_ff; - PIN(cur_ind) = config.pins.cur_ind; - PIN(max_y) = config.pins.max_y; - PIN(max_cur) = config.pins.max_cur; - ctx->timeout = 0; - PIN(crc_ok)++; - if(ctx->send == 0){ - ctx->send = 1; - } + PIN(r) = config.pins.r; + PIN(l) = config.pins.l; + PIN(psi) = config.pins.psi; + PIN(cur_p) = config.pins.cur_p; + PIN(cur_i) = config.pins.cur_i; + PIN(cur_ff) = config.pins.cur_ff; + PIN(cur_ind) = config.pins.cur_ind; + PIN(max_y) = config.pins.max_y; + PIN(max_cur) = config.pins.max_cur; + ctx->timeout = 0; + PIN(crc_ok) + ++; + if(ctx->send == 0) { + ctx->send = 1; } - else{ - PIN(crc_error)++; - } - } - - if(USART3->ISR & USART_ISR_RTOF){ // idle line - USART3->ICR |= USART_ICR_RTOCF | USART_ICR_FECF | USART_ICR_ORECF; // timeout clear flag - GPIOA->BSRR |= GPIO_PIN_10; + } else { + PIN(crc_error) + ++; + } + } - PIN(idle)++; - if(dma_pos != sizeof(packet_to_hv_t)){ - PIN(dma_pos) = dma_pos; - } + if(USART3->ISR & USART_ISR_RTOF) { // idle line + USART3->ICR |= USART_ICR_RTOCF | USART_ICR_FECF | USART_ICR_ORECF; // timeout clear flag + GPIOA->BSRR |= GPIO_PIN_10; - // reset rx DMA - DMA1_Channel3->CCR &= (uint16_t)(~DMA_CCR_EN); - DMA1_Channel3->CNDTR = sizeof(packet_to_hv_t); - DMA1_Channel3->CCR |= DMA_CCR_EN; - dma_pos = 0; - GPIOA->BSRR |= GPIO_PIN_10 << 16; + PIN(idle) + ++; + if(dma_pos != sizeof(packet_to_hv_t)) { + PIN(dma_pos) = dma_pos; + } - //ctx->send = 1; - } + // reset rx DMA + DMA1_Channel3->CCR &= (uint16_t)(~DMA_CCR_EN); + DMA1_Channel3->CNDTR = sizeof(packet_to_hv_t); + DMA1_Channel3->CCR |= DMA_CCR_EN; + dma_pos = 0; + GPIOA->BSRR |= GPIO_PIN_10 << 16; - if(ctx->send == 2){ - ctx->send = 0; - } - if(ctx->send == 1 && dma_pos != 0){ - ctx->send = 2; - //packet_to_hv.d_cmd = -99.0; - state.pins.u_fb = PIN(u_fb); - state.pins.v_fb = PIN(v_fb); - state.pins.w_fb = PIN(w_fb); - state.pins.hv_temp = PIN(hv_temp); - state.pins.mot_temp = PIN(mot_temp); - state.pins.core_temp = PIN(core_temp); - state.pins.fault = PIN(fault); - state.pins.y = PIN(y); + //ctx->send = 1; + } - // fill tx struct - ctx->packet_from_hv.dc_volt = PIN(dc_volt); - ctx->packet_from_hv.pwm_volt = PIN(pwm_volt); - ctx->packet_from_hv.d_fb = PIN(d_fb); - ctx->packet_from_hv.q_fb = PIN(q_fb); - ctx->packet_from_hv.addr = ctx->tx_addr; - ctx->packet_from_hv.value = state.data[ctx->tx_addr++]; - ctx->tx_addr %= sizeof(state) / 4; - ctx->packet_from_hv.crc = HAL_CRC_Calculate(&hcrc, (uint32_t *) &(ctx->packet_from_hv), sizeof(packet_from_hv_t) / 4 - 1); + if(ctx->send == 2) { + ctx->send = 0; + } + if(ctx->send == 1 && dma_pos != 0) { + ctx->send = 2; + //packet_to_hv.d_cmd = -99.0; + state.pins.u_fb = PIN(u_fb); + state.pins.v_fb = PIN(v_fb); + state.pins.w_fb = PIN(w_fb); + state.pins.hv_temp = PIN(hv_temp); + state.pins.mot_temp = PIN(mot_temp); + state.pins.core_temp = PIN(core_temp); + state.pins.fault = PIN(fault); + state.pins.y = PIN(y); - // start tx DMA - DMA1_Channel2->CCR &= (uint16_t)(~DMA_CCR_EN); - DMA1_Channel2->CNDTR = sizeof(packet_from_hv_t); - DMA1_Channel2->CCR |= DMA_CCR_EN; - //ctx->send = 0; - } + // fill tx struct + ctx->packet_from_hv.dc_volt = PIN(dc_volt); + ctx->packet_from_hv.pwm_volt = PIN(pwm_volt); + ctx->packet_from_hv.d_fb = PIN(d_fb); + ctx->packet_from_hv.q_fb = PIN(q_fb); + ctx->packet_from_hv.addr = ctx->tx_addr; + ctx->packet_from_hv.value = state.data[ctx->tx_addr++]; + ctx->tx_addr %= sizeof(state) / 4; + ctx->packet_from_hv.crc = HAL_CRC_Calculate(&hcrc, (uint32_t *)&(ctx->packet_from_hv), sizeof(packet_from_hv_t) / 4 - 1); - if(ctx->timeout > 5){//disable driver - PIN(en) = 0.0; - PIN(timeout)++; - } - ctx->timeout++; + // start tx DMA + DMA1_Channel2->CCR &= (uint16_t)(~DMA_CCR_EN); + DMA1_Channel2->CNDTR = sizeof(packet_from_hv_t); + DMA1_Channel2->CCR |= DMA_CCR_EN; + //ctx->send = 0; + } - // TODO: sin = 0.5 - switch((uint16_t)PIN(phase_mode)){ - case PHASE_90_3PH: // 90° - PIN(pwm_volt) = PIN(dc_volt) / M_SQRT2 * 0.95; + if(ctx->timeout > 5) { //disable driver + PIN(en) = 0.0; + PIN(timeout) + ++; + } + ctx->timeout++; + + // TODO: sin = 0.5 + switch((uint16_t)PIN(phase_mode)) { + case PHASE_90_3PH: // 90° + PIN(pwm_volt) = PIN(dc_volt) / M_SQRT2 * 0.95; break; - case PHASE_90_4PH: // 90° - PIN(pwm_volt) = PIN(dc_volt) * 0.95; + case PHASE_90_4PH: // 90° + PIN(pwm_volt) = PIN(dc_volt) * 0.95; break; - case PHASE_120_3PH: // 120° - PIN(pwm_volt) = PIN(dc_volt) / M_SQRT3 * 0.95; + case PHASE_120_3PH: // 120° + PIN(pwm_volt) = PIN(dc_volt) / M_SQRT3 * 0.95; break; - case PHASE_180_2PH: // 180° - case PHASE_180_3PH: // 180° - PIN(pwm_volt) = PIN(dc_volt) * 0.95; + case PHASE_180_2PH: // 180° + case PHASE_180_3PH: // 180° + PIN(pwm_volt) = PIN(dc_volt) * 0.95; break; - default: - PIN(pwm_volt) = 0.0; - } + default: + PIN(pwm_volt) = 0.0; + } } hal_comp_t ls_comp_struct = { - .name = "ls", - .nrt = 0, - .rt = rt_func, - .frt = 0, - .hw_init = hw_init, - .rt_start = rt_start, - .frt_start = 0, - .rt_stop = 0, - .frt_stop = 0, - .ctx_size = sizeof(struct ls_ctx_t), - .pin_count = sizeof(struct ls_pin_ctx_t) / sizeof(struct hal_pin_inst_t), + .name = "ls", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .hw_init = hw_init, + .rt_start = rt_start, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct ls_ctx_t), + .pin_count = sizeof(struct ls_pin_ctx_t) / sizeof(struct hal_pin_inst_t), }; diff --git a/stm32f303/src/dac.c b/stm32f303/src/dac.c index 16302977..9d696055 100644 --- a/stm32f303/src/dac.c +++ b/stm32f303/src/dac.c @@ -52,76 +52,65 @@ DAC_HandleTypeDef hdac; /* DAC init function */ -void MX_DAC_Init(void) -{ +void MX_DAC_Init(void) { DAC_ChannelConfTypeDef sConfig; - /**DAC Initialization + /**DAC Initialization */ hdac.Instance = DAC; - if (HAL_DAC_Init(&hdac) != HAL_OK) - { + if(HAL_DAC_Init(&hdac) != HAL_OK) { Error_Handler(); } - /**DAC channel OUT1 config + /**DAC channel OUT1 config */ - sConfig.DAC_Trigger = DAC_TRIGGER_NONE; + sConfig.DAC_Trigger = DAC_TRIGGER_NONE; sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; - if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_1) != HAL_OK) - { + if(HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_1) != HAL_OK) { Error_Handler(); } - } -void HAL_DAC_MspInit(DAC_HandleTypeDef* dacHandle) -{ - +void HAL_DAC_MspInit(DAC_HandleTypeDef *dacHandle) { GPIO_InitTypeDef GPIO_InitStruct; - if(dacHandle->Instance==DAC) - { - /* USER CODE BEGIN DAC_MspInit 0 */ + if(dacHandle->Instance == DAC) { + /* USER CODE BEGIN DAC_MspInit 0 */ - /* USER CODE END DAC_MspInit 0 */ + /* USER CODE END DAC_MspInit 0 */ /* Peripheral clock enable */ __HAL_RCC_DAC1_CLK_ENABLE(); - + /**DAC GPIO Configuration PA4 ------> DAC_OUT1 */ - GPIO_InitStruct.Pin = GPIO_PIN_4; + GPIO_InitStruct.Pin = GPIO_PIN_4; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - /* USER CODE BEGIN DAC_MspInit 1 */ + /* USER CODE BEGIN DAC_MspInit 1 */ - /* USER CODE END DAC_MspInit 1 */ + /* USER CODE END DAC_MspInit 1 */ } } -void HAL_DAC_MspDeInit(DAC_HandleTypeDef* dacHandle) -{ +void HAL_DAC_MspDeInit(DAC_HandleTypeDef *dacHandle) { + if(dacHandle->Instance == DAC) { + /* USER CODE BEGIN DAC_MspDeInit 0 */ - if(dacHandle->Instance==DAC) - { - /* USER CODE BEGIN DAC_MspDeInit 0 */ - - /* USER CODE END DAC_MspDeInit 0 */ + /* USER CODE END DAC_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_DAC1_CLK_DISABLE(); - + /**DAC GPIO Configuration PA4 ------> DAC_OUT1 */ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_4); - } /* USER CODE BEGIN DAC_MspDeInit 1 */ /* USER CODE END DAC_MspDeInit 1 */ -} +} /* USER CODE BEGIN 1 */ diff --git a/stm32f303/src/main.c b/stm32f303/src/main.c index 3621f4e9..34c06342 100644 --- a/stm32f303/src/main.c +++ b/stm32f303/src/main.c @@ -76,16 +76,16 @@ CRC_HandleTypeDef hcrc; // // TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE); // } -uint32_t hal_get_systick_value(){ - return(SysTick->VAL); +uint32_t hal_get_systick_value() { + return (SysTick->VAL); } -uint32_t hal_get_systick_reload(){ - return(SysTick->LOAD); +uint32_t hal_get_systick_reload() { + return (SysTick->LOAD); } -uint32_t hal_get_systick_freq(){ - return(systick_freq); +uint32_t hal_get_systick_freq() { + return (systick_freq); } /* USER CODE END PV */ @@ -102,36 +102,34 @@ void Error_Handler(void); /* USER CODE BEGIN 0 */ -void TIM8_UP_IRQHandler(){ - GPIOA->BSRR |= GPIO_PIN_9; - __HAL_TIM_CLEAR_IT(&htim8, TIM_IT_UPDATE); - hal_run_rt(); - if(__HAL_TIM_GET_FLAG(&htim8, TIM_IT_UPDATE) == SET){ - hal_stop(); - hal.hal_state = RT_TOO_LONG; - } - GPIOA->BSRR |= GPIO_PIN_9 << 16; +void TIM8_UP_IRQHandler() { + GPIOA->BSRR |= GPIO_PIN_9; + __HAL_TIM_CLEAR_IT(&htim8, TIM_IT_UPDATE); + hal_run_rt(); + if(__HAL_TIM_GET_FLAG(&htim8, TIM_IT_UPDATE) == SET) { + hal_stop(); + hal.hal_state = RT_TOO_LONG; + } + GPIOA->BSRR |= GPIO_PIN_9 << 16; } -void bootloader(char * ptr){ - #ifdef USB_DISCONNECT_PIN +void bootloader(char *ptr) { +#ifdef USB_DISCONNECT_PIN HAL_GPIO_WritePin(USB_DISCONNECT_PORT, USB_DISCONNECT_PIN, GPIO_PIN_SET); HAL_Delay(100); - #endif +#endif RTC->BKP0R = 0xDEADBEEF; NVIC_SystemReset(); } COMMAND("bootloader", bootloader, "enter bootloader"); -void reset(char * ptr){ - HAL_NVIC_SystemReset(); +void reset(char *ptr) { + HAL_NVIC_SystemReset(); } COMMAND("reset", reset, "reset STMBL"); -int main(void) -{ - +int main(void) { /* USER CODE BEGIN 1 */ SCB->VTOR = 0x8004000; /* USER CODE END 1 */ @@ -149,18 +147,18 @@ int main(void) __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOF_CLK_ENABLE(); - + GPIO_InitTypeDef GPIO_InitStruct; - #ifdef USB_DISCONNECT_PIN - GPIO_InitStruct.Pin = USB_DISCONNECT_PIN; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(USB_DISCONNECT_PORT, &GPIO_InitStruct); - HAL_GPIO_WritePin(USB_DISCONNECT_PORT, USB_DISCONNECT_PIN, GPIO_PIN_RESET); - #endif - +#ifdef USB_DISCONNECT_PIN + GPIO_InitStruct.Pin = USB_DISCONNECT_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(USB_DISCONNECT_PORT, &GPIO_InitStruct); + HAL_GPIO_WritePin(USB_DISCONNECT_PORT, USB_DISCONNECT_PIN, GPIO_PIN_RESET); +#endif + MX_TIM8_Init(); MX_ADC1_Init(); MX_ADC2_Init(); @@ -172,11 +170,11 @@ int main(void) MX_OPAMP3_Init(); // MX_USART1_UART_Init(); MX_USB_DEVICE_Init(); - + #ifdef USB_CONNECT_PIN - GPIO_InitStruct.Pin = USB_CONNECT_PIN; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Pin = USB_CONNECT_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(USB_CONNECT_PORT, &GPIO_InitStruct); HAL_GPIO_WritePin(USB_CONNECT_PORT, USB_CONNECT_PIN, GPIO_PIN_SET); @@ -195,69 +193,68 @@ int main(void) HAL_OPAMP_SelfCalibrate(&hopamp1); HAL_OPAMP_SelfCalibrate(&hopamp2); HAL_OPAMP_SelfCalibrate(&hopamp3); - - hcrc.Instance = CRC; - hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE; - hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE; - hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE; + + hcrc.Instance = CRC; + hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE; + hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE; + hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE; hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE; - hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_WORDS; - + hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_WORDS; + __HAL_RCC_CRC_CLK_ENABLE(); - - if (HAL_CRC_Init(&hcrc) != HAL_OK) - { + + if(HAL_CRC_Init(&hcrc) != HAL_OK) { Error_Handler(); } - + //IO pins - GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - if (HAL_OPAMP_Start(&hopamp1) != HAL_OK){ - Error_Handler(); - } - if (HAL_OPAMP_Start(&hopamp2) != HAL_OK){ - Error_Handler(); - } - if (HAL_OPAMP_Start(&hopamp3) != HAL_OK){ - Error_Handler(); - } + if(HAL_OPAMP_Start(&hopamp1) != HAL_OK) { + Error_Handler(); + } + if(HAL_OPAMP_Start(&hopamp2) != HAL_OK) { + Error_Handler(); + } + if(HAL_OPAMP_Start(&hopamp3) != HAL_OK) { + Error_Handler(); + } htim8.Instance->CCR1 = 0; htim8.Instance->CCR2 = 0; htim8.Instance->CCR3 = 0; - + HAL_ADC_Start(&hadc1); HAL_ADC_Start(&hadc2); HAL_ADC_Start(&hadc3); HAL_ADC_Start(&hadc4); - if (HAL_TIM_Base_Start_IT(&htim8) != HAL_OK){ - Error_Handler(); + if(HAL_TIM_Base_Start_IT(&htim8) != HAL_OK) { + Error_Handler(); } #ifndef PWM_INVERT - TIM8->RCR = 1;//uptate event foo + TIM8->RCR = 1; //uptate event foo #endif - if (HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_1) != HAL_OK){ - Error_Handler(); + if(HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_1) != HAL_OK) { + Error_Handler(); } - if (HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_2) != HAL_OK){ - Error_Handler(); + if(HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_2) != HAL_OK) { + Error_Handler(); } - if (HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_3) != HAL_OK){ - Error_Handler(); + if(HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_3) != HAL_OK) { + Error_Handler(); } - if (HAL_TIMEx_PWMN_Start(&htim8, TIM_CHANNEL_1) != HAL_OK){ - Error_Handler(); + if(HAL_TIMEx_PWMN_Start(&htim8, TIM_CHANNEL_1) != HAL_OK) { + Error_Handler(); } - if (HAL_TIMEx_PWMN_Start(&htim8, TIM_CHANNEL_2) != HAL_OK){ - Error_Handler(); + if(HAL_TIMEx_PWMN_Start(&htim8, TIM_CHANNEL_2) != HAL_OK) { + Error_Handler(); } - if (HAL_TIMEx_PWMN_Start(&htim8, TIM_CHANNEL_3) != HAL_OK){ - Error_Handler(); + if(HAL_TIMEx_PWMN_Start(&htim8, TIM_CHANNEL_3) != HAL_OK) { + Error_Handler(); } hal_init(1.0 / 15000.0, 0.0); @@ -271,7 +268,7 @@ int main(void) load_comp(comp_by_name("svm")); load_comp(comp_by_name("hv")); load_comp(comp_by_name("curpid")); - + hal_parse("term0.rt_prio = 0.1"); hal_parse("ls0.rt_prio = 0.6"); hal_parse("io0.rt_prio = 1.0"); @@ -280,7 +277,7 @@ int main(void) hal_parse("idq0.rt_prio = 4.0"); hal_parse("svm0.rt_prio = 5.0"); hal_parse("hv0.rt_prio = 6.0"); - + hal_parse("term0.send_step = 100.0"); hal_parse("term0.gain0 = 10.0"); hal_parse("term0.gain1 = 10.0"); @@ -302,19 +299,19 @@ int main(void) hal_parse("dq0.pos = ls0.pos"); hal_parse("dq0.mode = ls0.phase_mode"); hal_parse("hv0.en = ls0.en"); - + //ADC TEST hal_parse("term0.wave3 = io0.udc"); hal_parse("hv0.udc = io0.udc"); hal_parse("dq0.u = io0.iu"); hal_parse("dq0.v = io0.iv"); hal_parse("dq0.w = io0.iw"); - + hal_parse("svm0.u = idq0.u"); hal_parse("svm0.v = idq0.v"); hal_parse("svm0.w = idq0.w"); hal_parse("hv0.u = svm0.su"); - + hal_parse("hv0.v = svm0.sv"); hal_parse("hv0.w = svm0.sw"); hal_parse("svm0.udc = io0.udc"); @@ -352,74 +349,64 @@ int main(void) // hal_init_nrt(); // error foo hal_start(); - - while (1) - { - hal_run_nrt(); - cdc_poll(); - HAL_Delay(1); - } + while(1) { + hal_run_nrt(); + cdc_poll(); + HAL_Delay(1); + } } /** System Clock Configuration */ -void SystemClock_Config(void) -{ - +void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_PeriphCLKInitTypeDef PeriphClkInit; - /**Initializes the CPU, AHB and APB busses clocks + /**Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; - RCC_OscInitStruct.HSEState = RCC_HSE_ON; + RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; - RCC_OscInitStruct.HSIState = RCC_HSI_ON; - RCC_OscInitStruct.LSIState = RCC_LSI_ON; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; - RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; - RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) - { + RCC_OscInitStruct.HSIState = RCC_HSI_ON; + RCC_OscInitStruct.LSIState = RCC_LSI_ON; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; + RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; + if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } - /**Initializes the CPU, AHB and APB busses clocks + /**Initializes the CPU, AHB and APB busses clocks */ - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK - |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; - RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; - RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; - if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) - { + if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } - PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB|RCC_PERIPHCLK_USART1 - |RCC_PERIPHCLK_USART3|RCC_PERIPHCLK_TIM8 - |RCC_PERIPHCLK_ADC12|RCC_PERIPHCLK_ADC34|RCC_PERIPHCLK_RTC; + PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB | RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART3 | RCC_PERIPHCLK_TIM8 | RCC_PERIPHCLK_ADC12 | RCC_PERIPHCLK_ADC34 | RCC_PERIPHCLK_RTC; PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2; PeriphClkInit.Usart3ClockSelection = RCC_USART3CLKSOURCE_SYSCLK; - PeriphClkInit.Adc12ClockSelection = RCC_ADC12PLLCLK_DIV1; - PeriphClkInit.Adc34ClockSelection = RCC_ADC34PLLCLK_DIV1; - PeriphClkInit.USBClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5; - PeriphClkInit.Tim8ClockSelection = RCC_TIM8CLK_PLLCLK; - PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; - if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) - { + PeriphClkInit.Adc12ClockSelection = RCC_ADC12PLLCLK_DIV1; + PeriphClkInit.Adc34ClockSelection = RCC_ADC34PLLCLK_DIV1; + PeriphClkInit.USBClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5; + PeriphClkInit.Tim8ClockSelection = RCC_TIM8CLK_PLLCLK; + PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; + if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); } - /**Configure the Systick interrupt time + /**Configure the Systick interrupt time */ - HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); + HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000); - /**Configure the Systick + /**Configure the Systick */ HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); @@ -430,8 +417,8 @@ void SystemClock_Config(void) /* USER CODE BEGIN 4 */ //Delay implementation for hal_term.c -void Wait(uint32_t ms){ - HAL_Delay(ms); +void Wait(uint32_t ms) { + HAL_Delay(ms); } /* USER CODE END 4 */ @@ -441,15 +428,13 @@ void Wait(uint32_t ms){ * @param None * @retval None */ -void Error_Handler(void) -{ +void Error_Handler(void) { /* USER CODE BEGIN Error_Handler */ /* User can add his own implementation to report the HAL error return state */ - while(1) - { - HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET); + while(1) { + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET); } - /* USER CODE END Error_Handler */ + /* USER CODE END Error_Handler */ } #ifdef USE_FULL_ASSERT @@ -461,23 +446,21 @@ void Error_Handler(void) * @param line: assert_param error line source number * @retval None */ -void assert_failed(uint8_t* file, uint32_t line) -{ +void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ - } #endif /** * @} - */ + */ /** * @} -*/ +*/ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stm32f303/src/opamp.c b/stm32f303/src/opamp.c index 74d7859b..a2ded570 100644 --- a/stm32f303/src/opamp.c +++ b/stm32f303/src/opamp.c @@ -54,172 +54,146 @@ OPAMP_HandleTypeDef hopamp2; OPAMP_HandleTypeDef hopamp3; /* OPAMP1 init function */ -void MX_OPAMP1_Init(void) -{ - - hopamp1.Instance = OPAMP1; - hopamp1.Init.Mode = OPAMP_PGA_MODE; - hopamp1.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO0; +void MX_OPAMP1_Init(void) { + hopamp1.Instance = OPAMP1; + hopamp1.Init.Mode = OPAMP_PGA_MODE; + hopamp1.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO0; hopamp1.Init.TimerControlledMuxmode = OPAMP_TIMERCONTROLLEDMUXMODE_DISABLE; - hopamp1.Init.PgaConnect = OPAMP_PGA_CONNECT_INVERTINGINPUT_NO; - hopamp1.Init.PgaGain = OPAMP_PGA_GAIN_16; - hopamp1.Init.UserTrimming = OPAMP_TRIMMING_FACTORY; - if (HAL_OPAMP_Init(&hopamp1) != HAL_OK) - { + hopamp1.Init.PgaConnect = OPAMP_PGA_CONNECT_INVERTINGINPUT_NO; + hopamp1.Init.PgaGain = OPAMP_PGA_GAIN_16; + hopamp1.Init.UserTrimming = OPAMP_TRIMMING_FACTORY; + if(HAL_OPAMP_Init(&hopamp1) != HAL_OK) { Error_Handler(); } - } /* OPAMP2 init function */ -void MX_OPAMP2_Init(void) -{ - - hopamp2.Instance = OPAMP2; - hopamp2.Init.Mode = OPAMP_PGA_MODE; - hopamp2.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO0; +void MX_OPAMP2_Init(void) { + hopamp2.Instance = OPAMP2; + hopamp2.Init.Mode = OPAMP_PGA_MODE; + hopamp2.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO0; hopamp2.Init.TimerControlledMuxmode = OPAMP_TIMERCONTROLLEDMUXMODE_DISABLE; - hopamp2.Init.PgaConnect = OPAMP_PGA_CONNECT_INVERTINGINPUT_NO; - hopamp2.Init.PgaGain = OPAMP_PGA_GAIN_16; - hopamp2.Init.UserTrimming = OPAMP_TRIMMING_FACTORY; - if (HAL_OPAMP_Init(&hopamp2) != HAL_OK) - { + hopamp2.Init.PgaConnect = OPAMP_PGA_CONNECT_INVERTINGINPUT_NO; + hopamp2.Init.PgaGain = OPAMP_PGA_GAIN_16; + hopamp2.Init.UserTrimming = OPAMP_TRIMMING_FACTORY; + if(HAL_OPAMP_Init(&hopamp2) != HAL_OK) { Error_Handler(); } - } /* OPAMP3 init function */ -void MX_OPAMP3_Init(void) -{ - - hopamp3.Instance = OPAMP3; - hopamp3.Init.Mode = OPAMP_PGA_MODE; - hopamp3.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO0; +void MX_OPAMP3_Init(void) { + hopamp3.Instance = OPAMP3; + hopamp3.Init.Mode = OPAMP_PGA_MODE; + hopamp3.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO0; hopamp3.Init.TimerControlledMuxmode = OPAMP_TIMERCONTROLLEDMUXMODE_DISABLE; - hopamp3.Init.PgaConnect = OPAMP_PGA_CONNECT_INVERTINGINPUT_NO; - hopamp3.Init.PgaGain = OPAMP_PGA_GAIN_16; - hopamp3.Init.UserTrimming = OPAMP_TRIMMING_FACTORY; - if (HAL_OPAMP_Init(&hopamp3) != HAL_OK) - { + hopamp3.Init.PgaConnect = OPAMP_PGA_CONNECT_INVERTINGINPUT_NO; + hopamp3.Init.PgaGain = OPAMP_PGA_GAIN_16; + hopamp3.Init.UserTrimming = OPAMP_TRIMMING_FACTORY; + if(HAL_OPAMP_Init(&hopamp3) != HAL_OK) { Error_Handler(); } - } -void HAL_OPAMP_MspInit(OPAMP_HandleTypeDef* opampHandle) -{ - +void HAL_OPAMP_MspInit(OPAMP_HandleTypeDef *opampHandle) { GPIO_InitTypeDef GPIO_InitStruct; - if(opampHandle->Instance==OPAMP1) - { - /* USER CODE BEGIN OPAMP1_MspInit 0 */ + if(opampHandle->Instance == OPAMP1) { + /* USER CODE BEGIN OPAMP1_MspInit 0 */ + + /* USER CODE END OPAMP1_MspInit 0 */ - /* USER CODE END OPAMP1_MspInit 0 */ - /**OPAMP1 GPIO Configuration PA1 ------> OPAMP1_VINP PA2 ------> OPAMP1_VOUT */ - GPIO_InitStruct.Pin = GPIO_PIN_1 | GPIO_PIN_2; + GPIO_InitStruct.Pin = GPIO_PIN_1 | GPIO_PIN_2; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - /* USER CODE BEGIN OPAMP1_MspInit 1 */ + /* USER CODE BEGIN OPAMP1_MspInit 1 */ - /* USER CODE END OPAMP1_MspInit 1 */ - } - else if(opampHandle->Instance==OPAMP2) - { - /* USER CODE BEGIN OPAMP2_MspInit 0 */ + /* USER CODE END OPAMP1_MspInit 1 */ + } else if(opampHandle->Instance == OPAMP2) { + /* USER CODE BEGIN OPAMP2_MspInit 0 */ + + /* USER CODE END OPAMP2_MspInit 0 */ - /* USER CODE END OPAMP2_MspInit 0 */ - /**OPAMP2 GPIO Configuration PA6 ------> OPAMP2_VOUT PA7 ------> OPAMP2_VINP */ - GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; + GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - /* USER CODE BEGIN OPAMP2_MspInit 1 */ + /* USER CODE BEGIN OPAMP2_MspInit 1 */ - /* USER CODE END OPAMP2_MspInit 1 */ - } - else if(opampHandle->Instance==OPAMP3) - { - /* USER CODE BEGIN OPAMP3_MspInit 0 */ + /* USER CODE END OPAMP2_MspInit 1 */ + } else if(opampHandle->Instance == OPAMP3) { + /* USER CODE BEGIN OPAMP3_MspInit 0 */ + + /* USER CODE END OPAMP3_MspInit 0 */ - /* USER CODE END OPAMP3_MspInit 0 */ - /**OPAMP3 GPIO Configuration PB0 ------> OPAMP3_VINP PB1 ------> OPAMP3_VOUT */ - GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; + GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - /* USER CODE BEGIN OPAMP3_MspInit 1 */ + /* USER CODE BEGIN OPAMP3_MspInit 1 */ - /* USER CODE END OPAMP3_MspInit 1 */ + /* USER CODE END OPAMP3_MspInit 1 */ } } -void HAL_OPAMP_MspDeInit(OPAMP_HandleTypeDef* opampHandle) -{ +void HAL_OPAMP_MspDeInit(OPAMP_HandleTypeDef *opampHandle) { + if(opampHandle->Instance == OPAMP1) { + /* USER CODE BEGIN OPAMP1_MspDeInit 0 */ - if(opampHandle->Instance==OPAMP1) - { - /* USER CODE BEGIN OPAMP1_MspDeInit 0 */ + /* USER CODE END OPAMP1_MspDeInit 0 */ - /* USER CODE END OPAMP1_MspDeInit 0 */ - /**OPAMP1 GPIO Configuration PA1 ------> OPAMP1_VINP PA2 ------> OPAMP1_VOUT */ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_1 | GPIO_PIN_2); - /* USER CODE BEGIN OPAMP1_MspDeInit 1 */ + /* USER CODE BEGIN OPAMP1_MspDeInit 1 */ - /* USER CODE END OPAMP1_MspDeInit 1 */ - } - else if(opampHandle->Instance==OPAMP2) - { - /* USER CODE BEGIN OPAMP2_MspDeInit 0 */ + /* USER CODE END OPAMP1_MspDeInit 1 */ + } else if(opampHandle->Instance == OPAMP2) { + /* USER CODE BEGIN OPAMP2_MspDeInit 0 */ + + /* USER CODE END OPAMP2_MspDeInit 0 */ - /* USER CODE END OPAMP2_MspDeInit 0 */ - /**OPAMP2 GPIO Configuration PA6 ------> OPAMP2_VOUT PA7 ------> OPAMP2_VINP */ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_6 | GPIO_PIN_7); - /* USER CODE BEGIN OPAMP2_MspDeInit 1 */ + /* USER CODE BEGIN OPAMP2_MspDeInit 1 */ - /* USER CODE END OPAMP2_MspDeInit 1 */ - } - else if(opampHandle->Instance==OPAMP3) - { - /* USER CODE BEGIN OPAMP3_MspDeInit 0 */ + /* USER CODE END OPAMP2_MspDeInit 1 */ + } else if(opampHandle->Instance == OPAMP3) { + /* USER CODE BEGIN OPAMP3_MspDeInit 0 */ + + /* USER CODE END OPAMP3_MspDeInit 0 */ - /* USER CODE END OPAMP3_MspDeInit 0 */ - /**OPAMP3 GPIO Configuration PB0 ------> OPAMP3_VINP PB1 ------> OPAMP3_VOUT */ HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0 | GPIO_PIN_1); - /* USER CODE BEGIN OPAMP3_MspDeInit 1 */ + /* USER CODE BEGIN OPAMP3_MspDeInit 1 */ - /* USER CODE END OPAMP3_MspDeInit 1 */ + /* USER CODE END OPAMP3_MspDeInit 1 */ } -} +} /* USER CODE BEGIN 1 */ diff --git a/stm32f303/src/stm32f3xx_hal_msp.c b/stm32f303/src/stm32f3xx_hal_msp.c index 2ea00e77..857c7192 100644 --- a/stm32f303/src/stm32f3xx_hal_msp.c +++ b/stm32f303/src/stm32f3xx_hal_msp.c @@ -51,8 +51,7 @@ extern void Error_Handler(void); /** * Initializes the Global MSP. */ -void HAL_MspInit(void) -{ +void HAL_MspInit(void) { /* USER CODE BEGIN MspInit 0 */ /* USER CODE END MspInit 0 */ diff --git a/stm32f303/src/stm32f3xx_it.c b/stm32f303/src/stm32f3xx_it.c index dc079a33..4390bd7f 100644 --- a/stm32f303/src/stm32f3xx_it.c +++ b/stm32f303/src/stm32f3xx_it.c @@ -43,14 +43,13 @@ /* External variables --------------------------------------------------------*/ extern PCD_HandleTypeDef hpcd_USB_FS; /******************************************************************************/ -/* Cortex-M4 Processor Interruption and Exception Handlers */ +/* Cortex-M4 Processor Interruption and Exception Handlers */ /******************************************************************************/ /** * @brief This function handles Non maskable interrupt. */ -void NMI_Handler(void) -{ +void NMI_Handler(void) { /* USER CODE BEGIN NonMaskableInt_IRQn 0 */ hal_error(NMI); /* USER CODE END NonMaskableInt_IRQn 0 */ @@ -62,8 +61,7 @@ void NMI_Handler(void) /** * @brief This function handles Hard fault interrupt. */ -void HardFault_Handler(void) -{ +void HardFault_Handler(void) { /* USER CODE BEGIN HardFault_IRQn 0 */ /* USER CODE END HardFault_IRQn 0 */ @@ -76,8 +74,7 @@ void HardFault_Handler(void) /** * @brief This function handles Memory management fault. */ -void MemManage_Handler(void) -{ +void MemManage_Handler(void) { /* USER CODE BEGIN MemoryManagement_IRQn 0 */ /* USER CODE END MemoryManagement_IRQn 0 */ @@ -90,8 +87,7 @@ void MemManage_Handler(void) /** * @brief This function handles Pre-fetch fault, memory access fault. */ -void BusFault_Handler(void) -{ +void BusFault_Handler(void) { /* USER CODE BEGIN BusFault_IRQn 0 */ /* USER CODE END BusFault_IRQn 0 */ @@ -104,8 +100,7 @@ void BusFault_Handler(void) /** * @brief This function handles Undefined instruction or illegal state. */ -void UsageFault_Handler(void) -{ +void UsageFault_Handler(void) { /* USER CODE BEGIN UsageFault_IRQn 0 */ /* USER CODE END UsageFault_IRQn 0 */ @@ -118,8 +113,7 @@ void UsageFault_Handler(void) /** * @brief This function handles Debug monitor. */ -void DebugMon_Handler(void) -{ +void DebugMon_Handler(void) { /* USER CODE BEGIN DebugMonitor_IRQn 0 */ /* USER CODE END DebugMonitor_IRQn 0 */ @@ -131,8 +125,7 @@ void DebugMon_Handler(void) /** * @brief This function handles System tick timer. */ -void SysTick_Handler(void) -{ +void SysTick_Handler(void) { /* USER CODE BEGIN SysTick_IRQn 0 */ /* USER CODE END SysTick_IRQn 0 */ @@ -153,15 +146,14 @@ void SysTick_Handler(void) /** * @brief This function handles USB low priority or CAN_RX0 interrupts. */ -void USB_LP_CAN_RX0_IRQHandler(void) -{ +void USB_LP_CAN_RX0_IRQHandler(void) { /* USER CODE BEGIN USB_LP_CAN_RX0_IRQn 0 */ -//GPIOA->BSRR |= GPIO_PIN_10; + //GPIOA->BSRR |= GPIO_PIN_10; /* USER CODE END USB_LP_CAN_RX0_IRQn 0 */ HAL_PCD_IRQHandler(&hpcd_USB_FS); /* USER CODE BEGIN USB_LP_CAN_RX0_IRQn 1 */ -//GPIOA->BSRR |= GPIO_PIN_10 << 16; + //GPIOA->BSRR |= GPIO_PIN_10 << 16; /* USER CODE END USB_LP_CAN_RX0_IRQn 1 */ } diff --git a/stm32f303/src/tim.c b/stm32f303/src/tim.c index d51b3425..476b05a7 100644 --- a/stm32f303/src/tim.c +++ b/stm32f303/src/tim.c @@ -53,116 +53,100 @@ TIM_HandleTypeDef htim8; /* TIM8 init function */ -void MX_TIM8_Init(void) -{ +void MX_TIM8_Init(void) { TIM_ClockConfigTypeDef sClockSourceConfig; TIM_MasterConfigTypeDef sMasterConfig; TIM_OC_InitTypeDef sConfigOC; TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig; - htim8.Instance = TIM8; - htim8.Init.Prescaler = 0; - htim8.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED3; - htim8.Init.Period = 4800; + htim8.Instance = TIM8; + htim8.Init.Prescaler = 0; + htim8.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED3; + htim8.Init.Period = 4800; htim8.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; #ifdef PWM_INVERT htim8.Init.RepetitionCounter = 1; #else htim8.Init.RepetitionCounter = 0; #endif - if (HAL_TIM_Base_Init(&htim8) != HAL_OK) - { + if(HAL_TIM_Base_Init(&htim8) != HAL_OK) { Error_Handler(); } sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; - if (HAL_TIM_ConfigClockSource(&htim8, &sClockSourceConfig) != HAL_OK) - { + if(HAL_TIM_ConfigClockSource(&htim8, &sClockSourceConfig) != HAL_OK) { Error_Handler(); } - if (HAL_TIM_PWM_Init(&htim8) != HAL_OK) - { + if(HAL_TIM_PWM_Init(&htim8) != HAL_OK) { Error_Handler(); } - sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; + sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET; - sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; - if (HAL_TIMEx_MasterConfigSynchronization(&htim8, &sMasterConfig) != HAL_OK) - { + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + if(HAL_TIMEx_MasterConfigSynchronization(&htim8, &sMasterConfig) != HAL_OK) { Error_Handler(); } - sConfigOC.OCMode = TIM_OCMODE_PWM1; - sConfigOC.Pulse = 0; - sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; - sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; - sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; - sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; + sConfigOC.OCMode = TIM_OCMODE_PWM1; + sConfigOC.Pulse = 0; + sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; + sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; + sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; + sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; - if (HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) - { + if(HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) { Error_Handler(); } - if (HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC, TIM_CHANNEL_2) != HAL_OK) - { + if(HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC, TIM_CHANNEL_2) != HAL_OK) { Error_Handler(); } - if (HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC, TIM_CHANNEL_3) != HAL_OK) - { + if(HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC, TIM_CHANNEL_3) != HAL_OK) { Error_Handler(); } - sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; + sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; - sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; - sBreakDeadTimeConfig.DeadTime = PWM_DEADTIME; - sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; - sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH; - sBreakDeadTimeConfig.BreakFilter = 0; - sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE; - sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH; - sBreakDeadTimeConfig.Break2Filter = 0; - sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; - if (HAL_TIMEx_ConfigBreakDeadTime(&htim8, &sBreakDeadTimeConfig) != HAL_OK) - { + sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; + sBreakDeadTimeConfig.DeadTime = PWM_DEADTIME; + sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; + sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH; + sBreakDeadTimeConfig.BreakFilter = 0; + sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE; + sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH; + sBreakDeadTimeConfig.Break2Filter = 0; + sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; + if(HAL_TIMEx_ConfigBreakDeadTime(&htim8, &sBreakDeadTimeConfig) != HAL_OK) { Error_Handler(); } HAL_TIM_MspPostInit(&htim8); - } -void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle) -{ +void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *tim_baseHandle) { + if(tim_baseHandle->Instance == TIM8) { + /* USER CODE BEGIN TIM8_MspInit 0 */ - if(tim_baseHandle->Instance==TIM8) - { - /* USER CODE BEGIN TIM8_MspInit 0 */ - - /* USER CODE END TIM8_MspInit 0 */ + /* USER CODE END TIM8_MspInit 0 */ /* Peripheral clock enable */ __HAL_RCC_TIM8_CLK_ENABLE(); HAL_NVIC_SetPriority(TIM8_UP_IRQn, 0, 0); HAL_NVIC_EnableIRQ(TIM8_UP_IRQn); - /* USER CODE BEGIN TIM8_MspInit 1 */ + /* USER CODE BEGIN TIM8_MspInit 1 */ - /* USER CODE END TIM8_MspInit 1 */ + /* USER CODE END TIM8_MspInit 1 */ } } -void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle) -{ - +void HAL_TIM_MspPostInit(TIM_HandleTypeDef *timHandle) { GPIO_InitTypeDef GPIO_InitStruct; - if(timHandle->Instance==TIM8) - { - /* USER CODE BEGIN TIM8_MspPostInit 0 */ + if(timHandle->Instance == TIM8) { + /* USER CODE BEGIN TIM8_MspPostInit 0 */ + + /* USER CODE END TIM8_MspPostInit 0 */ - /* USER CODE END TIM8_MspPostInit 0 */ - /**TIM8 GPIO Configuration PB3 ------> TIM8_CH1N PB4 ------> TIM8_CH2N @@ -171,56 +155,52 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle) PB8 ------> TIM8_CH2 PB9 ------> TIM8_CH3 */ - GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Pin = GPIO_PIN_3 | GPIO_PIN_4; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF4_TIM8; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - GPIO_InitStruct.Pin = GPIO_PIN_5; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Pin = GPIO_PIN_5; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF3_TIM8; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - GPIO_InitStruct.Pin = GPIO_PIN_6; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Pin = GPIO_PIN_6; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF5_TIM8; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF10_TIM8; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - /* USER CODE BEGIN TIM8_MspPostInit 1 */ + /* USER CODE BEGIN TIM8_MspPostInit 1 */ - /* USER CODE END TIM8_MspPostInit 1 */ + /* USER CODE END TIM8_MspPostInit 1 */ } - } -void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle) -{ +void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *tim_baseHandle) { + if(tim_baseHandle->Instance == TIM8) { + /* USER CODE BEGIN TIM8_MspDeInit 0 */ - if(tim_baseHandle->Instance==TIM8) - { - /* USER CODE BEGIN TIM8_MspDeInit 0 */ - - /* USER CODE END TIM8_MspDeInit 0 */ + /* USER CODE END TIM8_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_TIM8_CLK_DISABLE(); } /* USER CODE BEGIN TIM8_MspDeInit 1 */ /* USER CODE END TIM8_MspDeInit 1 */ -} +} /* USER CODE BEGIN 1 */ diff --git a/stm32f303/src/usb_device.c b/stm32f303/src/usb_device.c index 23bccb16..d132e2d8 100644 --- a/stm32f303/src/usb_device.c +++ b/stm32f303/src/usb_device.c @@ -53,9 +53,8 @@ /* USB Device Core handle declaration */ USBD_HandleTypeDef hUsbDeviceFS; -/* init function */ -void MX_USB_DEVICE_Init(void) -{ +/* init function */ +void MX_USB_DEVICE_Init(void) { /* Init Device Library,Add Supported Class and Start the library*/ USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS); @@ -64,7 +63,6 @@ void MX_USB_DEVICE_Init(void) USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS); USBD_Start(&hUsbDeviceFS); - } /** * @} diff --git a/stm32f303/src/usbd_cdc_if.c b/stm32f303/src/usbd_cdc_if.c index 4c7137d2..ff3afbd3 100644 --- a/stm32f303/src/usbd_cdc_if.c +++ b/stm32f303/src/usbd_cdc_if.c @@ -45,8 +45,8 @@ #include "usbd_cdc_if.h" /* USER CODE BEGIN INCLUDE */ #include "ringbuf.h" -#define RX_QUEUE_SIZE 1024 -#define TX_QUEUE_SIZE 4096 +#define RX_QUEUE_SIZE 1024 +#define TX_QUEUE_SIZE 4096 /* USER CODE END INCLUDE */ /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY @@ -56,42 +56,42 @@ /** @defgroup USBD_CDC * @brief usbd core module * @{ - */ + */ /** @defgroup USBD_CDC_Private_TypesDefinitions * @{ - */ + */ /* USER CODE BEGIN PRIVATE_TYPES */ struct ringbuf rx_buf = RINGBUF(RX_QUEUE_SIZE); struct ringbuf tx_buf = RINGBUF(TX_QUEUE_SIZE); -/* USER CODE END PRIVATE_TYPES */ +/* USER CODE END PRIVATE_TYPES */ /** * @} - */ + */ /** @defgroup USBD_CDC_Private_Defines * @{ - */ + */ /* USER CODE BEGIN PRIVATE_DEFINES */ /* Define size for the receive and transmit buffer over CDC */ /* It's up to user to redefine and/or remove those define */ -#define APP_RX_DATA_SIZE 64 //TODO: more for FS transfers -#define APP_TX_DATA_SIZE 64 +#define APP_RX_DATA_SIZE 64 //TODO: more for FS transfers +#define APP_TX_DATA_SIZE 64 /* USER CODE END PRIVATE_DEFINES */ /** * @} - */ + */ /** @defgroup USBD_CDC_Private_Macros * @{ - */ + */ /* USER CODE BEGIN PRIVATE_MACRO */ /* USER CODE END PRIVATE_MACRO */ /** * @} - */ - + */ + /** @defgroup USBD_CDC_Private_Variables * @{ */ @@ -108,41 +108,40 @@ uint8_t UserTxBufferFS[APP_TX_DATA_SIZE]; /** * @} - */ - + */ + /** @defgroup USBD_CDC_IF_Exported_Variables * @{ - */ - extern USBD_HandleTypeDef hUsbDeviceFS; + */ +extern USBD_HandleTypeDef hUsbDeviceFS; /* USER CODE BEGIN EXPORTED_VARIABLES */ /* USER CODE END EXPORTED_VARIABLES */ /** * @} - */ - + */ + /** @defgroup USBD_CDC_Private_FunctionPrototypes * @{ */ -static int8_t CDC_Init_FS (void); -static int8_t CDC_DeInit_FS (void); -static int8_t CDC_Control_FS (uint8_t cmd, uint8_t* pbuf, uint16_t length); -static int8_t CDC_Receive_FS (uint8_t* pbuf, uint32_t *Len); +static int8_t CDC_Init_FS(void); +static int8_t CDC_DeInit_FS(void); +static int8_t CDC_Control_FS(uint8_t cmd, uint8_t *pbuf, uint16_t length); +static int8_t CDC_Receive_FS(uint8_t *pbuf, uint32_t *Len); /* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */ /* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */ /** * @} - */ - -USBD_CDC_ItfTypeDef USBD_Interface_fops_FS = -{ - CDC_Init_FS, - CDC_DeInit_FS, - CDC_Control_FS, - CDC_Receive_FS -}; + */ + +USBD_CDC_ItfTypeDef USBD_Interface_fops_FS = + { + CDC_Init_FS, + CDC_DeInit_FS, + CDC_Control_FS, + CDC_Receive_FS}; /* Private functions ---------------------------------------------------------*/ /** @@ -151,14 +150,13 @@ USBD_CDC_ItfTypeDef USBD_Interface_fops_FS = * @param None * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL */ -static int8_t CDC_Init_FS(void) -{ - /* USER CODE BEGIN 3 */ +static int8_t CDC_Init_FS(void) { + /* USER CODE BEGIN 3 */ /* Set Application Buffers */ USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0); USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS); return (USBD_OK); - /* USER CODE END 3 */ + /* USER CODE END 3 */ } /** @@ -167,11 +165,10 @@ static int8_t CDC_Init_FS(void) * @param None * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL */ -static int8_t CDC_DeInit_FS(void) -{ - /* USER CODE BEGIN 4 */ +static int8_t CDC_DeInit_FS(void) { + /* USER CODE BEGIN 4 */ return (USBD_OK); - /* USER CODE END 4 */ + /* USER CODE END 4 */ } /** @@ -182,66 +179,64 @@ static int8_t CDC_DeInit_FS(void) * @param length: Number of data to be sent (in bytes) * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL */ -static int8_t CDC_Control_FS (uint8_t cmd, uint8_t* pbuf, uint16_t length) -{ +static int8_t CDC_Control_FS(uint8_t cmd, uint8_t *pbuf, uint16_t length) { /* USER CODE BEGIN 5 */ - switch (cmd) - { - case CDC_SEND_ENCAPSULATED_COMMAND: - - break; + switch(cmd) { + case CDC_SEND_ENCAPSULATED_COMMAND: - case CDC_GET_ENCAPSULATED_RESPONSE: - - break; + break; - case CDC_SET_COMM_FEATURE: - - break; + case CDC_GET_ENCAPSULATED_RESPONSE: - case CDC_GET_COMM_FEATURE: + break; - break; + case CDC_SET_COMM_FEATURE: - case CDC_CLEAR_COMM_FEATURE: + break; - break; + case CDC_GET_COMM_FEATURE: - /*******************************************************************************/ - /* Line Coding Structure */ - /*-----------------------------------------------------------------------------*/ - /* Offset | Field | Size | Value | Description */ - /* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/ - /* 4 | bCharFormat | 1 | Number | Stop bits */ - /* 0 - 1 Stop bit */ - /* 1 - 1.5 Stop bits */ - /* 2 - 2 Stop bits */ - /* 5 | bParityType | 1 | Number | Parity */ - /* 0 - None */ - /* 1 - Odd */ - /* 2 - Even */ - /* 3 - Mark */ - /* 4 - Space */ - /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */ - /*******************************************************************************/ - case CDC_SET_LINE_CODING: - - break; + break; - case CDC_GET_LINE_CODING: + case CDC_CLEAR_COMM_FEATURE: - break; + break; - case CDC_SET_CONTROL_LINE_STATE: + /*******************************************************************************/ + /* Line Coding Structure */ + /*-----------------------------------------------------------------------------*/ + /* Offset | Field | Size | Value | Description */ + /* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/ + /* 4 | bCharFormat | 1 | Number | Stop bits */ + /* 0 - 1 Stop bit */ + /* 1 - 1.5 Stop bits */ + /* 2 - 2 Stop bits */ + /* 5 | bParityType | 1 | Number | Parity */ + /* 0 - None */ + /* 1 - Odd */ + /* 2 - Even */ + /* 3 - Mark */ + /* 4 - Space */ + /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */ + /*******************************************************************************/ + case CDC_SET_LINE_CODING: - break; + break; - case CDC_SEND_BREAK: - - break; - - default: - break; + case CDC_GET_LINE_CODING: + + break; + + case CDC_SET_CONTROL_LINE_STATE: + + break; + + case CDC_SEND_BREAK: + + break; + + default: + break; } return (USBD_OK); @@ -263,14 +258,13 @@ static int8_t CDC_Control_FS (uint8_t cmd, uint8_t* pbuf, uint16_t length) * @param Len: Number of data received (in bytes) * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL */ -static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len) -{ +static int8_t CDC_Receive_FS(uint8_t *Buf, uint32_t *Len) { /* USER CODE BEGIN 6 */ rb_write(&rx_buf, Buf, *Len); USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]); USBD_CDC_ReceivePacket(&hUsbDeviceFS); return (USBD_OK); - /* USER CODE END 6 */ + /* USER CODE END 6 */ } /** @@ -284,72 +278,70 @@ static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len) * @param Len: Number of data to be send (in bytes) * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY */ -uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len) -{ +uint8_t CDC_Transmit_FS(uint8_t *Buf, uint16_t Len) { uint8_t result = USBD_OK; - /* USER CODE BEGIN 7 */ - USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData; - if (hcdc->TxState != 0){ + /* USER CODE BEGIN 7 */ + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)hUsbDeviceFS.pClassData; + if(hcdc->TxState != 0) { return USBD_BUSY; } USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len); result = USBD_CDC_TransmitPacket(&hUsbDeviceFS); - /* USER CODE END 7 */ + /* USER CODE END 7 */ return result; } /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */ -void cdc_usbtx(){ - USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData; - if (hcdc->TxState != 0){ - return; //busy - } - int len = rb_read(&tx_buf,UserTxBufferFS,sizeof(UserTxBufferFS)); - USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, len); - USBD_CDC_TransmitPacket(&hUsbDeviceFS); +void cdc_usbtx() { + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)hUsbDeviceFS.pClassData; + if(hcdc->TxState != 0) { + return; //busy + } + int len = rb_read(&tx_buf, UserTxBufferFS, sizeof(UserTxBufferFS)); + USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, len); + USBD_CDC_TransmitPacket(&hUsbDeviceFS); } -int cdc_tx(void* data, uint32_t len){ - if(cdc_is_connected()){ - int ret; - ret = rb_write(&tx_buf, data, len); - cdc_usbtx(); - return ret; - }else{ - return 0; - } +int cdc_tx(void *data, uint32_t len) { + if(cdc_is_connected()) { + int ret; + ret = rb_write(&tx_buf, data, len); + cdc_usbtx(); + return ret; + } else { + return 0; + } } -void cdc_poll(){ - if(cdc_is_connected()){ - cdc_usbtx(); - } +void cdc_poll() { + if(cdc_is_connected()) { + cdc_usbtx(); + } } -int cdc_is_connected(){ - if(hUsbDeviceFS.dev_state != USBD_STATE_CONFIGURED){ - return 0; - }else{ - return 1; - } +int cdc_is_connected() { + if(hUsbDeviceFS.dev_state != USBD_STATE_CONFIGURED) { + return 0; + } else { + return 1; + } } -int cdc_getline(char *ptr, int len){ - return rb_getline(&rx_buf, ptr, len); +int cdc_getline(char *ptr, int len) { + return rb_getline(&rx_buf, ptr, len); } -int _write(int file, char *ptr, int len){ - return cdc_tx(ptr, len); +int _write(int file, char *ptr, int len) { + return cdc_tx(ptr, len); } /* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */ /** * @} - */ + */ /** * @} - */ + */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - diff --git a/stm32f303/src/usbd_conf.c b/stm32f303/src/usbd_conf.c index c8211cdd..c90da411 100644 --- a/stm32f303/src/usbd_conf.c +++ b/stm32f303/src/usbd_conf.c @@ -68,23 +68,21 @@ void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state); *******************************************************************************/ /* MSP Init */ -void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle) -{ +void HAL_PCD_MspInit(PCD_HandleTypeDef *pcdHandle) { GPIO_InitTypeDef GPIO_InitStruct; - if(pcdHandle->Instance==USB) - { - /* USER CODE BEGIN USB_MspInit 0 */ + if(pcdHandle->Instance == USB) { + /* USER CODE BEGIN USB_MspInit 0 */ + + /* USER CODE END USB_MspInit 0 */ - /* USER CODE END USB_MspInit 0 */ - /**USB GPIO Configuration PA11 ------> USB_DM PA12 ------> USB_DP */ - GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Alternate = GPIO_AF14_USB; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); @@ -94,34 +92,32 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle) /* Peripheral interrupt init */ HAL_NVIC_SetPriority(USB_LP_CAN_RX0_IRQn, 10, 0); HAL_NVIC_EnableIRQ(USB_LP_CAN_RX0_IRQn); - /* USER CODE BEGIN USB_MspInit 1 */ + /* USER CODE BEGIN USB_MspInit 1 */ - /* USER CODE END USB_MspInit 1 */ + /* USER CODE END USB_MspInit 1 */ } } -void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle) -{ - if(pcdHandle->Instance==USB) - { - /* USER CODE BEGIN USB_MspDeInit 0 */ +void HAL_PCD_MspDeInit(PCD_HandleTypeDef *pcdHandle) { + if(pcdHandle->Instance == USB) { + /* USER CODE BEGIN USB_MspDeInit 0 */ - /* USER CODE END USB_MspDeInit 0 */ + /* USER CODE END USB_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_USB_CLK_DISABLE(); - + /**USB GPIO Configuration PA11 ------> USB_DM PA12 ------> USB_DP */ - HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12); + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11 | GPIO_PIN_12); /* Peripheral interrupt Deinit*/ HAL_NVIC_DisableIRQ(USB_LP_CAN_RX0_IRQn); - /* USER CODE BEGIN USB_MspDeInit 1 */ + /* USER CODE BEGIN USB_MspDeInit 1 */ - /* USER CODE END USB_MspDeInit 1 */ + /* USER CODE END USB_MspDeInit 1 */ } } @@ -130,9 +126,8 @@ void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle) * @param hpcd: PCD handle * @retval None */ -void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd) -{ - USBD_LL_SetupStage((USBD_HandleTypeDef*)hpcd->pData, (uint8_t *)hpcd->Setup); +void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd) { + USBD_LL_SetupStage((USBD_HandleTypeDef *)hpcd->pData, (uint8_t *)hpcd->Setup); } /** @@ -141,9 +136,8 @@ void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd) * @param epnum: Endpoint Number * @retval None */ -void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) -{ - USBD_LL_DataOutStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff); +void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) { + USBD_LL_DataOutStage((USBD_HandleTypeDef *)hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff); } /** @@ -152,9 +146,8 @@ void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) * @param epnum: Endpoint Number * @retval None */ -void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) -{ - USBD_LL_DataInStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff); +void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) { + USBD_LL_DataInStage((USBD_HandleTypeDef *)hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff); } /** @@ -162,9 +155,8 @@ void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) * @param hpcd: PCD handle * @retval None */ -void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) -{ - USBD_LL_SOF((USBD_HandleTypeDef*)hpcd->pData); +void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) { + USBD_LL_SOF((USBD_HandleTypeDef *)hpcd->pData); } /** @@ -172,25 +164,23 @@ void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) * @param hpcd: PCD handle * @retval None */ -void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd) -{ +void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd) { USBD_SpeedTypeDef speed = USBD_SPEED_FULL; /*Set USB Current Speed*/ - switch (hpcd->Init.speed) - { - case PCD_SPEED_FULL: - speed = USBD_SPEED_FULL; - break; - - default: - speed = USBD_SPEED_FULL; - break; + switch(hpcd->Init.speed) { + case PCD_SPEED_FULL: + speed = USBD_SPEED_FULL; + break; + + default: + speed = USBD_SPEED_FULL; + break; } - USBD_LL_SetSpeed((USBD_HandleTypeDef*)hpcd->pData, speed); - + USBD_LL_SetSpeed((USBD_HandleTypeDef *)hpcd->pData, speed); + /*Reset Device*/ - USBD_LL_Reset((USBD_HandleTypeDef*)hpcd->pData); + USBD_LL_Reset((USBD_HandleTypeDef *)hpcd->pData); } /** @@ -199,14 +189,12 @@ void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd) * @param hpcd: PCD handle * @retval None */ -void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd) -{ +void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd) { /* Inform USB library that core enters in suspend Mode */ - USBD_LL_Suspend((USBD_HandleTypeDef*)hpcd->pData); + USBD_LL_Suspend((USBD_HandleTypeDef *)hpcd->pData); /*Enter in STOP mode */ /* USER CODE BEGIN 2 */ - if (hpcd->Init.low_power_enable) - { + 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)); } @@ -219,13 +207,11 @@ void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd) * @param hpcd: PCD handle * @retval None */ -void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd) -{ +void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd) { /* USER CODE BEGIN 3 */ /* USER CODE END 3 */ - USBD_LL_Resume((USBD_HandleTypeDef*)hpcd->pData); - + USBD_LL_Resume((USBD_HandleTypeDef *)hpcd->pData); } /** @@ -234,9 +220,8 @@ void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd) * @param epnum: Endpoint Number * @retval None */ -void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) -{ - USBD_LL_IsoOUTIncomplete((USBD_HandleTypeDef*)hpcd->pData, epnum); +void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) { + USBD_LL_IsoOUTIncomplete((USBD_HandleTypeDef *)hpcd->pData, epnum); } /** @@ -245,9 +230,8 @@ void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) * @param epnum: Endpoint Number * @retval None */ -void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) -{ - USBD_LL_IsoINIncomplete((USBD_HandleTypeDef*)hpcd->pData, epnum); +void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) { + USBD_LL_IsoINIncomplete((USBD_HandleTypeDef *)hpcd->pData, epnum); } /** @@ -255,9 +239,8 @@ void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) * @param hpcd: PCD handle * @retval None */ -void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd) -{ - USBD_LL_DevConnected((USBD_HandleTypeDef*)hpcd->pData); +void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd) { + USBD_LL_DevConnected((USBD_HandleTypeDef *)hpcd->pData); } /** @@ -265,9 +248,8 @@ void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd) * @param hpcd: PCD handle * @retval None */ -void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd) -{ - USBD_LL_DevDisconnected((USBD_HandleTypeDef*)hpcd->pData); +void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd) { + USBD_LL_DevDisconnected((USBD_HandleTypeDef *)hpcd->pData); } /******************************************************************************* @@ -278,30 +260,28 @@ void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd) * @param pdev: Device handle * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef *pdev) -{ +USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev) { /* Init USB_IP */ /* Link The driver to the stack */ hpcd_USB_FS.pData = pdev; - pdev->pData = &hpcd_USB_FS; + pdev->pData = &hpcd_USB_FS; - hpcd_USB_FS.Instance = USB; - hpcd_USB_FS.Init.dev_endpoints = 8; - hpcd_USB_FS.Init.speed = PCD_SPEED_FULL; - hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_64; - hpcd_USB_FS.Init.phy_itface = PCD_PHY_EMBEDDED; - hpcd_USB_FS.Init.low_power_enable = DISABLE; + hpcd_USB_FS.Instance = USB; + hpcd_USB_FS.Init.dev_endpoints = 8; + hpcd_USB_FS.Init.speed = PCD_SPEED_FULL; + hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_64; + hpcd_USB_FS.Init.phy_itface = PCD_PHY_EMBEDDED; + hpcd_USB_FS.Init.low_power_enable = DISABLE; hpcd_USB_FS.Init.battery_charging_enable = DISABLE; - if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK) - { + if(HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK) { Error_Handler(); } - HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x00 , PCD_SNG_BUF, 0x18); - HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x80 , PCD_SNG_BUF, 0x58); - HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x81 , PCD_SNG_BUF, 0xC0); - HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x01 , PCD_SNG_BUF, 0x110); - HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x82 , PCD_SNG_BUF, 0x100); + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef *)pdev->pData, 0x00, PCD_SNG_BUF, 0x18); + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef *)pdev->pData, 0x80, PCD_SNG_BUF, 0x58); + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef *)pdev->pData, 0x81, PCD_SNG_BUF, 0xC0); + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef *)pdev->pData, 0x01, PCD_SNG_BUF, 0x110); + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef *)pdev->pData, 0x82, PCD_SNG_BUF, 0x100); return USBD_OK; } @@ -310,9 +290,8 @@ USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef *pdev) * @param pdev: Device handle * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_DeInit (USBD_HandleTypeDef *pdev) -{ - HAL_PCD_DeInit((PCD_HandleTypeDef*)pdev->pData); +USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev) { + HAL_PCD_DeInit((PCD_HandleTypeDef *)pdev->pData); return USBD_OK; } @@ -321,9 +300,8 @@ USBD_StatusTypeDef USBD_LL_DeInit (USBD_HandleTypeDef *pdev) * @param pdev: Device handle * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev) -{ - HAL_PCD_Start((PCD_HandleTypeDef*)pdev->pData); +USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev) { + HAL_PCD_Start((PCD_HandleTypeDef *)pdev->pData); return USBD_OK; } @@ -332,9 +310,8 @@ USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev) * @param pdev: Device handle * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_Stop (USBD_HandleTypeDef *pdev) -{ - HAL_PCD_Stop((PCD_HandleTypeDef*) pdev->pData); +USBD_StatusTypeDef USBD_LL_Stop(USBD_HandleTypeDef *pdev) { + HAL_PCD_Stop((PCD_HandleTypeDef *)pdev->pData); return USBD_OK; } @@ -346,17 +323,16 @@ USBD_StatusTypeDef USBD_LL_Stop (USBD_HandleTypeDef *pdev) * @param ep_mps: Endpoint Max Packet Size * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_OpenEP (USBD_HandleTypeDef *pdev, - uint8_t ep_addr, - uint8_t ep_type, - uint16_t ep_mps) -{ - HAL_PCD_EP_Open((PCD_HandleTypeDef*) pdev->pData, +USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev, + uint8_t ep_addr, + uint8_t ep_type, + uint16_t ep_mps) { + HAL_PCD_EP_Open((PCD_HandleTypeDef *)pdev->pData, ep_addr, ep_mps, ep_type); - - return USBD_OK; + + return USBD_OK; } /** @@ -365,9 +341,8 @@ USBD_StatusTypeDef USBD_LL_OpenEP (USBD_HandleTypeDef *pdev, * @param ep_addr: Endpoint Number * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_CloseEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) -{ - HAL_PCD_EP_Close((PCD_HandleTypeDef*) pdev->pData, ep_addr); +USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) { + HAL_PCD_EP_Close((PCD_HandleTypeDef *)pdev->pData, ep_addr); return USBD_OK; } @@ -377,9 +352,8 @@ USBD_StatusTypeDef USBD_LL_CloseEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) * @param ep_addr: Endpoint Number * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_FlushEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) -{ - HAL_PCD_EP_Flush((PCD_HandleTypeDef*) pdev->pData, ep_addr); +USBD_StatusTypeDef USBD_LL_FlushEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) { + HAL_PCD_EP_Flush((PCD_HandleTypeDef *)pdev->pData, ep_addr); return USBD_OK; } @@ -389,9 +363,8 @@ USBD_StatusTypeDef USBD_LL_FlushEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) * @param ep_addr: Endpoint Number * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_StallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) -{ - HAL_PCD_EP_SetStall((PCD_HandleTypeDef*) pdev->pData, ep_addr); +USBD_StatusTypeDef USBD_LL_StallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) { + HAL_PCD_EP_SetStall((PCD_HandleTypeDef *)pdev->pData, ep_addr); return USBD_OK; } @@ -401,10 +374,9 @@ USBD_StatusTypeDef USBD_LL_StallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) * @param ep_addr: Endpoint Number * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_ClearStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) -{ - HAL_PCD_EP_ClrStall((PCD_HandleTypeDef*) pdev->pData, ep_addr); - return USBD_OK; +USBD_StatusTypeDef USBD_LL_ClearStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) { + HAL_PCD_EP_ClrStall((PCD_HandleTypeDef *)pdev->pData, ep_addr); + return USBD_OK; } /** @@ -413,17 +385,13 @@ USBD_StatusTypeDef USBD_LL_ClearStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_a * @param ep_addr: Endpoint Number * @retval Stall (1: Yes, 0: No) */ -uint8_t USBD_LL_IsStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) -{ - PCD_HandleTypeDef *hpcd = (PCD_HandleTypeDef*) pdev->pData; - - if((ep_addr & 0x80) == 0x80) - { - return hpcd->IN_ep[ep_addr & 0x7F].is_stall; - } - else - { - return hpcd->OUT_ep[ep_addr & 0x7F].is_stall; +uint8_t USBD_LL_IsStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) { + PCD_HandleTypeDef *hpcd = (PCD_HandleTypeDef *)pdev->pData; + + if((ep_addr & 0x80) == 0x80) { + return hpcd->IN_ep[ep_addr & 0x7F].is_stall; + } else { + return hpcd->OUT_ep[ep_addr & 0x7F].is_stall; } } /** @@ -432,10 +400,9 @@ uint8_t USBD_LL_IsStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr) * @param ep_addr: Endpoint Number * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_SetUSBAddress (USBD_HandleTypeDef *pdev, uint8_t dev_addr) -{ - HAL_PCD_SetAddress((PCD_HandleTypeDef*) pdev->pData, dev_addr); - return USBD_OK; +USBD_StatusTypeDef USBD_LL_SetUSBAddress(USBD_HandleTypeDef *pdev, uint8_t dev_addr) { + HAL_PCD_SetAddress((PCD_HandleTypeDef *)pdev->pData, dev_addr); + return USBD_OK; } /** @@ -446,12 +413,11 @@ USBD_StatusTypeDef USBD_LL_SetUSBAddress (USBD_HandleTypeDef *pdev, uint8_t dev * @param size: Data size * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_Transmit (USBD_HandleTypeDef *pdev, - uint8_t ep_addr, - uint8_t *pbuf, - uint16_t size) -{ - HAL_PCD_EP_Transmit((PCD_HandleTypeDef*) pdev->pData, ep_addr, pbuf, size); +USBD_StatusTypeDef USBD_LL_Transmit(USBD_HandleTypeDef *pdev, + uint8_t ep_addr, + uint8_t *pbuf, + uint16_t size) { + HAL_PCD_EP_Transmit((PCD_HandleTypeDef *)pdev->pData, ep_addr, pbuf, size); return USBD_OK; } @@ -463,12 +429,11 @@ USBD_StatusTypeDef USBD_LL_Transmit (USBD_HandleTypeDef *pdev, * @param size: Data size * @retval USBD Status */ -USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, - uint8_t ep_addr, - uint8_t *pbuf, - uint16_t size) -{ - HAL_PCD_EP_Receive((PCD_HandleTypeDef*) pdev->pData, ep_addr, pbuf, size); +USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, + uint8_t ep_addr, + uint8_t *pbuf, + uint16_t size) { + HAL_PCD_EP_Receive((PCD_HandleTypeDef *)pdev->pData, ep_addr, pbuf, size); return USBD_OK; } @@ -478,9 +443,8 @@ USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, * @param ep_addr: Endpoint Number * @retval Recived Data Size */ -uint32_t USBD_LL_GetRxDataSize (USBD_HandleTypeDef *pdev, uint8_t ep_addr) -{ - return HAL_PCD_EP_GetRxCount((PCD_HandleTypeDef*) pdev->pData, ep_addr); +uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr) { + return HAL_PCD_EP_GetRxCount((PCD_HandleTypeDef *)pdev->pData, ep_addr); } /** @@ -488,9 +452,8 @@ uint32_t USBD_LL_GetRxDataSize (USBD_HandleTypeDef *pdev, uint8_t ep_addr) * @param Delay: Delay in ms * @retval None */ -void USBD_LL_Delay (uint32_t Delay) -{ - HAL_Delay(Delay); +void USBD_LL_Delay(uint32_t Delay) { + HAL_Delay(Delay); } /** @@ -498,9 +461,8 @@ void USBD_LL_Delay (uint32_t Delay) * @param size: size of allocated memory * @retval None */ -void *USBD_static_malloc(uint32_t size) -{ - static uint32_t mem[(sizeof(USBD_CDC_HandleTypeDef)/4)+1];/* On 32-bit boundary */ +void *USBD_static_malloc(uint32_t size) { + static uint32_t mem[(sizeof(USBD_CDC_HandleTypeDef) / 4) + 1]; /* On 32-bit boundary */ return mem; } @@ -509,9 +471,7 @@ void *USBD_static_malloc(uint32_t size) * @param *p pointer to allocated memory address * @retval None */ -void USBD_static_free(void *p) -{ - +void USBD_static_free(void *p) { } /** @@ -520,20 +480,15 @@ void USBD_static_free(void *p) * @param state: connection state (0 : disconnected / 1: connected) * @retval None */ -void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state) -{ -/* USER CODE BEGIN 4 */ - if (state == 1) - { +void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state) { + /* USER CODE BEGIN 4 */ + if(state == 1) { /* Configure Low Connection State */ - - } - else - { + + } else { /* Configure High Connection State */ - - } -/* USER CODE END 4 */ + } + /* USER CODE END 4 */ } /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stm32f303/src/usbd_desc.c b/stm32f303/src/usbd_desc.c index 119f61de..256897ff 100644 --- a/stm32f303/src/usbd_desc.c +++ b/stm32f303/src/usbd_desc.c @@ -54,125 +54,125 @@ /** @defgroup USBD_DESC * @brief USBD descriptors module * @{ - */ + */ /** @defgroup USBD_DESC_Private_TypesDefinitions * @{ - */ + */ /** * @} - */ + */ /** @defgroup USBD_DESC_Private_Defines * @{ - */ -#define USBD_VID 1155 -#define USBD_LANGID_STRING 1033 -#define USBD_MANUFACTURER_STRING "STMicroelectronics" -#define USBD_PID_FS 22336 -#define USBD_PRODUCT_STRING_FS "STM32 Virtual ComPort" -#define USBD_SERIALNUMBER_STRING_FS "00000000001A" -#define USBD_CONFIGURATION_STRING_FS "CDC Config" -#define USBD_INTERFACE_STRING_FS "CDC Interface" + */ +#define USBD_VID 1155 +#define USBD_LANGID_STRING 1033 +#define USBD_MANUFACTURER_STRING "STMicroelectronics" +#define USBD_PID_FS 22336 +#define USBD_PRODUCT_STRING_FS "STM32 Virtual ComPort" +#define USBD_SERIALNUMBER_STRING_FS "00000000001A" +#define USBD_CONFIGURATION_STRING_FS "CDC Config" +#define USBD_INTERFACE_STRING_FS "CDC Interface" /* USER CODE BEGIN 0 */ /* USER CODE END 0*/ /** * @} - */ + */ /** @defgroup USBD_DESC_Private_Macros * @{ - */ + */ /** * @} - */ + */ /** @defgroup USBD_DESC_Private_Variables * @{ - */ -uint8_t * USBD_FS_DeviceDescriptor( USBD_SpeedTypeDef speed , uint16_t *length); -uint8_t * USBD_FS_LangIDStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length); -uint8_t * USBD_FS_ManufacturerStrDescriptor ( USBD_SpeedTypeDef speed , uint16_t *length); -uint8_t * USBD_FS_ProductStrDescriptor ( USBD_SpeedTypeDef speed , uint16_t *length); -uint8_t * USBD_FS_SerialStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length); -uint8_t * USBD_FS_ConfigStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length); -uint8_t * USBD_FS_InterfaceStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length); + */ +uint8_t *USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); +uint8_t *USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); +uint8_t *USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); +uint8_t *USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); +uint8_t *USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); +uint8_t *USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); +uint8_t *USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); #ifdef USB_SUPPORT_USER_STRING_DESC -uint8_t * USBD_FS_USRStringDesc (USBD_SpeedTypeDef speed, uint8_t idx , uint16_t *length); -#endif /* USB_SUPPORT_USER_STRING_DESC */ +uint8_t *USBD_FS_USRStringDesc(USBD_SpeedTypeDef speed, uint8_t idx, uint16_t *length); +#endif /* USB_SUPPORT_USER_STRING_DESC */ USBD_DescriptorsTypeDef FS_Desc = -{ - USBD_FS_DeviceDescriptor, - USBD_FS_LangIDStrDescriptor, - USBD_FS_ManufacturerStrDescriptor, - USBD_FS_ProductStrDescriptor, - USBD_FS_SerialStrDescriptor, - USBD_FS_ConfigStrDescriptor, - USBD_FS_InterfaceStrDescriptor, + { + USBD_FS_DeviceDescriptor, + USBD_FS_LangIDStrDescriptor, + USBD_FS_ManufacturerStrDescriptor, + USBD_FS_ProductStrDescriptor, + USBD_FS_SerialStrDescriptor, + USBD_FS_ConfigStrDescriptor, + USBD_FS_InterfaceStrDescriptor, }; -#if defined ( __ICCARM__ ) /*!< IAR Compiler */ - #pragma data_alignment=4 +#if defined(__ICCARM__) /*!< IAR Compiler */ +#pragma data_alignment = 4 #endif /* USB Standard Device Descriptor */ __ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = - { - 0x12, /*bLength */ - USB_DESC_TYPE_DEVICE, /*bDescriptorType*/ - 0x00, /* bcdUSB */ - 0x02, - 0x02, /*bDeviceClass*/ - 0x02, /*bDeviceSubClass*/ - 0x00, /*bDeviceProtocol*/ - USB_MAX_EP0_SIZE, /*bMaxPacketSize*/ - LOBYTE(USBD_VID), /*idVendor*/ - HIBYTE(USBD_VID), /*idVendor*/ - LOBYTE(USBD_PID_FS), /*idVendor*/ - HIBYTE(USBD_PID_FS), /*idVendor*/ - 0x00, /*bcdDevice rel. 2.00*/ - 0x02, - USBD_IDX_MFC_STR, /*Index of manufacturer string*/ - USBD_IDX_PRODUCT_STR, /*Index of product string*/ - USBD_IDX_SERIAL_STR, /*Index of serial number string*/ - USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/ - } ; + { + 0x12, /*bLength */ + USB_DESC_TYPE_DEVICE, /*bDescriptorType*/ + 0x00, /* bcdUSB */ + 0x02, + 0x02, /*bDeviceClass*/ + 0x02, /*bDeviceSubClass*/ + 0x00, /*bDeviceProtocol*/ + USB_MAX_EP0_SIZE, /*bMaxPacketSize*/ + LOBYTE(USBD_VID), /*idVendor*/ + HIBYTE(USBD_VID), /*idVendor*/ + LOBYTE(USBD_PID_FS), /*idVendor*/ + HIBYTE(USBD_PID_FS), /*idVendor*/ + 0x00, /*bcdDevice rel. 2.00*/ + 0x02, + USBD_IDX_MFC_STR, /*Index of manufacturer string*/ + USBD_IDX_PRODUCT_STR, /*Index of product string*/ + USBD_IDX_SERIAL_STR, /*Index of serial number string*/ + USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/ +}; /* USB_DeviceDescriptor */ -#if defined ( __ICCARM__ ) /*!< IAR Compiler */ - #pragma data_alignment=4 +#if defined(__ICCARM__) /*!< IAR Compiler */ +#pragma data_alignment = 4 #endif /* USB Standard Device Descriptor */ __ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = -{ - USB_LEN_LANGID_STR_DESC, - USB_DESC_TYPE_STRING, - LOBYTE(USBD_LANGID_STRING), - HIBYTE(USBD_LANGID_STRING), + { + USB_LEN_LANGID_STR_DESC, + USB_DESC_TYPE_STRING, + LOBYTE(USBD_LANGID_STRING), + HIBYTE(USBD_LANGID_STRING), }; -#if defined ( __ICCARM__ ) /*!< IAR Compiler */ - #pragma data_alignment=4 +#if defined(__ICCARM__) /*!< IAR Compiler */ +#pragma data_alignment = 4 #endif __ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END; /** * @} - */ + */ /** @defgroup USBD_DESC_Private_FunctionPrototypes * @{ - */ + */ /** * @} - */ + */ /** @defgroup USBD_DESC_Private_Functions * @{ - */ + */ /** * @brief USBD_FS_DeviceDescriptor @@ -181,8 +181,7 @@ __ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END; * @param length : pointer to data length variable * @retval pointer to descriptor buffer */ -uint8_t * USBD_FS_DeviceDescriptor( USBD_SpeedTypeDef speed , uint16_t *length) -{ +uint8_t *USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { *length = sizeof(USBD_FS_DeviceDesc); return USBD_FS_DeviceDesc; } @@ -194,9 +193,8 @@ uint8_t * USBD_FS_DeviceDescriptor( USBD_SpeedTypeDef speed , uint16_t *length) * @param length : pointer to data length variable * @retval pointer to descriptor buffer */ -uint8_t * USBD_FS_LangIDStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length) -{ - *length = sizeof(USBD_LangIDDesc); +uint8_t *USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { + *length = sizeof(USBD_LangIDDesc); return USBD_LangIDDesc; } @@ -207,15 +205,11 @@ uint8_t * USBD_FS_LangIDStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *leng * @param length : pointer to data length variable * @retval pointer to descriptor buffer */ -uint8_t * USBD_FS_ProductStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length) -{ - if(speed == 0) - { - USBD_GetString (USBD_PRODUCT_STRING_FS, USBD_StrDesc, length); - } - else - { - USBD_GetString (USBD_PRODUCT_STRING_FS, USBD_StrDesc, length); +uint8_t *USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { + if(speed == 0) { + USBD_GetString(USBD_PRODUCT_STRING_FS, USBD_StrDesc, length); + } else { + USBD_GetString(USBD_PRODUCT_STRING_FS, USBD_StrDesc, length); } return USBD_StrDesc; } @@ -227,9 +221,8 @@ uint8_t * USBD_FS_ProductStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *len * @param length : pointer to data length variable * @retval pointer to descriptor buffer */ -uint8_t * USBD_FS_ManufacturerStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length) -{ - USBD_GetString (USBD_MANUFACTURER_STRING, USBD_StrDesc, length); +uint8_t *USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { + USBD_GetString(USBD_MANUFACTURER_STRING, USBD_StrDesc, length); return USBD_StrDesc; } @@ -240,15 +233,11 @@ uint8_t * USBD_FS_ManufacturerStrDescriptor( USBD_SpeedTypeDef speed , uint16_t * @param length : pointer to data length variable * @retval pointer to descriptor buffer */ -uint8_t * USBD_FS_SerialStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length) -{ - if(speed == USBD_SPEED_HIGH) - { - USBD_GetString (USBD_SERIALNUMBER_STRING_FS, USBD_StrDesc, length); - } - else - { - USBD_GetString (USBD_SERIALNUMBER_STRING_FS, USBD_StrDesc, length); +uint8_t *USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { + if(speed == USBD_SPEED_HIGH) { + USBD_GetString(USBD_SERIALNUMBER_STRING_FS, USBD_StrDesc, length); + } else { + USBD_GetString(USBD_SERIALNUMBER_STRING_FS, USBD_StrDesc, length); } return USBD_StrDesc; } @@ -260,17 +249,13 @@ uint8_t * USBD_FS_SerialStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *leng * @param length : pointer to data length variable * @retval pointer to descriptor buffer */ -uint8_t * USBD_FS_ConfigStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length) -{ - if(speed == USBD_SPEED_HIGH) - { - USBD_GetString (USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length); +uint8_t *USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { + if(speed == USBD_SPEED_HIGH) { + USBD_GetString(USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length); + } else { + USBD_GetString(USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length); } - else - { - USBD_GetString (USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length); - } - return USBD_StrDesc; + return USBD_StrDesc; } /** @@ -280,28 +265,24 @@ uint8_t * USBD_FS_ConfigStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *leng * @param length : pointer to data length variable * @retval pointer to descriptor buffer */ -uint8_t * USBD_FS_InterfaceStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length) -{ - if(speed == 0) - { - USBD_GetString (USBD_INTERFACE_STRING_FS, USBD_StrDesc, length); +uint8_t *USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { + if(speed == 0) { + USBD_GetString(USBD_INTERFACE_STRING_FS, USBD_StrDesc, length); + } else { + USBD_GetString(USBD_INTERFACE_STRING_FS, USBD_StrDesc, length); } - else - { - USBD_GetString (USBD_INTERFACE_STRING_FS, USBD_StrDesc, length); - } - return USBD_StrDesc; + return USBD_StrDesc; } /** * @} - */ + */ /** * @} - */ + */ /** * @} - */ + */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stm32f303/src/version.c b/stm32f303/src/version.c index a48ef9b0..72773cc6 100644 --- a/stm32f303/src/version.c +++ b/stm32f303/src/version.c @@ -1,8 +1,7 @@ #include "version.h" volatile const struct version_info version_info = { - .product_name = "STMBL-hv-f303", - .major = 0, - .minor = 9, - .patch = 0 -}; + .product_name = "STMBL-hv-f303", + .major = 0, + .minor = 9, + .patch = 0};