X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=ssr%2FSrc%2Fmain.c;h=cb54168bf568f2a3eefd43d293dcc229cacfadeb;hb=075b0e3ec3abb970b92ae6d033c8c41f00adffc0;hp=e934fa9ce2ffd911f87a278c2e714fa4e340f977;hpb=08f734c44598ca90f47f4ab0dfed232c045dd403;p=home-hw.git diff --git a/ssr/Src/main.c b/ssr/Src/main.c index e934fa9..cb54168 100644 --- a/ssr/Src/main.c +++ b/ssr/Src/main.c @@ -56,16 +56,18 @@ PCD_HandleTypeDef hpcd_USB_FS; /* USER CODE BEGIN PV */ /* Private variables ---------------------------------------------------------*/ struct usb usb; +volatile byte led_trigger; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); -static void MX_I2C1_Init(void); -static void MX_I2C2_Init(void); +static void MX_DMA_Init(void); static void MX_USB_PCD_Init(void); static void MX_TIM4_Init(void); +static void MX_USART1_UART_Init(void); +static void MX_TIM3_Init(void); /* USER CODE BEGIN PFP */ /* Private function prototypes -----------------------------------------------*/ @@ -74,6 +76,44 @@ static void MX_TIM4_Init(void); /* USER CODE BEGIN 0 */ +static void process_packet(void) +{ + uint cmd = get_u32_be(rx_packet); + uint arg = get_u32_be(rx_packet+4); + debug_printf("<< cmd %08x %08x\n", cmd, arg); + + memset(tx_packet, 0, sizeof(tx_packet)); + put_u32_be(tx_packet, 1); + + switch (cmd) + { + case 1: + if (arg < 16) + { + if (arg & 1) + LL_GPIO_ResetOutputPin(SSR1_GPIO_Port, SSR1_Pin); + else + LL_GPIO_SetOutputPin(SSR1_GPIO_Port, SSR1_Pin); + if (arg & 2) + LL_GPIO_ResetOutputPin(SSR2_GPIO_Port, SSR2_Pin); + else + LL_GPIO_SetOutputPin(SSR2_GPIO_Port, SSR2_Pin); + if (arg & 4) + LL_GPIO_ResetOutputPin(SSR3_GPIO_Port, SSR3_Pin); + else + LL_GPIO_SetOutputPin(SSR3_GPIO_Port, SSR3_Pin); + if (arg & 8) + LL_GPIO_ResetOutputPin(SSR4_GPIO_Port, SSR4_Pin); + else + LL_GPIO_SetOutputPin(SSR4_GPIO_Port, SSR4_Pin); + put_u32_be(tx_packet, 0); + } + break; + } + + debug_printf(">> status %08x\n", get_u32_be(tx_packet)); +} + /* USER CODE END 0 */ /** @@ -106,6 +146,7 @@ int main(void) /* Initialize all configured peripherals */ MX_GPIO_Init(); + MX_DMA_Init(); // A hack to let USB host reset us LL_GPIO_InitTypeDef gpio; @@ -118,23 +159,18 @@ int main(void) LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_13); LL_mDelay(1000); - MX_I2C1_Init(); - MX_I2C2_Init(); MX_USB_PCD_Init(); MX_TIM4_Init(); + MX_USART1_UART_Init(); + MX_TIM3_Init(); /* USER CODE BEGIN 2 */ - display_init(); usb_start(&usb); - bmp_init(); LL_TIM_EnableCounter(TIM4); LL_TIM_EnableIT_UPDATE(TIM4); LL_TIM_GenerateEvent_UPDATE(TIM4); - { - byte buf[5] = { 0xff, 0xff, 10, 0xff, 0xff }; - display_buffer(buf); - } + ds_test(); /* USER CODE END 2 */ @@ -142,32 +178,34 @@ int main(void) /* USER CODE BEGIN WHILE */ while (1) { - if (rx_packet_state == 1 && !tx_packet_state) + if (led_trigger) { + led_trigger = 0; static byte led_state; if (led_state) LL_GPIO_SetOutputPin(LED_GPIO_Port, LED_Pin); else LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin); led_state ^= 1; + } - display_buffer(rx_packet); + if (rx_packet_state == 1 && !tx_packet_state) + { tx_packet_state = 1; - put_u32_be(tx_packet, adjusted_temp); - put_u32_be(tx_packet + 4, adjusted_press); - put_u32_be(tx_packet + 8, bmp_counter); + process_packet(); usb_ep_send(&usb, 0x82, tx_packet, 12); rx_packet_state = 0; usb_ep_receive(&usb, 0x01, rx_packet, 64); - bmp_request = 1; } - bmp_step(); - - // debug_printf("Counter = %d\n", cnt); - // display_counter(cnt); +#if 0 + static int cnt; + debug_printf("Counter = %d\n", cnt); + cnt++; +#endif __WFI(); + // LL_mDelay(1000); /* USER CODE END WHILE */ @@ -232,89 +270,77 @@ void SystemClock_Config(void) NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0)); } -/* I2C1 init function */ -static void MX_I2C1_Init(void) +/* TIM3 init function */ +static void MX_TIM3_Init(void) { - LL_I2C_InitTypeDef I2C_InitStruct; + LL_TIM_InitTypeDef TIM_InitStruct; + LL_TIM_OC_InitTypeDef TIM_OC_InitStruct; LL_GPIO_InitTypeDef GPIO_InitStruct; - /**I2C1 GPIO Configuration - PB6 ------> I2C1_SCL - PB7 ------> I2C1_SDA - */ - GPIO_InitStruct.Pin = LL_GPIO_PIN_6|LL_GPIO_PIN_7; - GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE; - GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH; - GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN; - LL_GPIO_Init(GPIOB, &GPIO_InitStruct); - /* Peripheral clock enable */ - LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1); + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3); - /* I2C1 interrupt Init */ - NVIC_SetPriority(I2C1_EV_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0)); - NVIC_EnableIRQ(I2C1_EV_IRQn); + /* TIM3 DMA Init */ + + /* TIM3_CH1_TRIG Init */ + LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_6, LL_DMA_DIRECTION_PERIPH_TO_MEMORY); - /**I2C Initialization - */ - I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C; - I2C_InitStruct.ClockSpeed = 400000; - I2C_InitStruct.DutyCycle = LL_I2C_DUTYCYCLE_2; - I2C_InitStruct.OwnAddress1 = 0; - I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK; - I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT; - LL_I2C_Init(I2C1, &I2C_InitStruct); + LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_6, LL_DMA_PRIORITY_VERYHIGH); - LL_I2C_SetOwnAddress2(I2C1, 0); + LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_6, LL_DMA_MODE_NORMAL); - LL_I2C_DisableOwnAddress2(I2C1); + LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_6, LL_DMA_PERIPH_NOINCREMENT); - LL_I2C_DisableGeneralCall(I2C1); + LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_6, LL_DMA_MEMORY_INCREMENT); - LL_I2C_EnableClockStretching(I2C1); + LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_6, LL_DMA_PDATAALIGN_HALFWORD); -} + LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_6, LL_DMA_MDATAALIGN_HALFWORD); -/* I2C2 init function */ -static void MX_I2C2_Init(void) -{ + /* TIM3 interrupt Init */ + NVIC_SetPriority(TIM3_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0)); + NVIC_EnableIRQ(TIM3_IRQn); - LL_I2C_InitTypeDef I2C_InitStruct; + TIM_InitStruct.Prescaler = 71; + TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP; + TIM_InitStruct.Autoreload = 0; + TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1; + LL_TIM_Init(TIM3, &TIM_InitStruct); - LL_GPIO_InitTypeDef GPIO_InitStruct; + LL_TIM_DisableARRPreload(TIM3); - /**I2C2 GPIO Configuration - PB10 ------> I2C2_SCL - PB11 ------> I2C2_SDA - */ - GPIO_InitStruct.Pin = LL_GPIO_PIN_10|LL_GPIO_PIN_11; - GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE; - GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH; - GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN; - LL_GPIO_Init(GPIOB, &GPIO_InitStruct); + LL_TIM_SetClockSource(TIM3, LL_TIM_CLOCKSOURCE_INTERNAL); - /* Peripheral clock enable */ - LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C2); + TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_FROZEN; + TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE; + TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE; + TIM_OC_InitStruct.CompareValue = 0; + TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH; + LL_TIM_OC_Init(TIM3, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct); - /**I2C Initialization - */ - I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C; - I2C_InitStruct.ClockSpeed = 400000; - I2C_InitStruct.DutyCycle = LL_I2C_DUTYCYCLE_2; - I2C_InitStruct.OwnAddress1 = 0; - I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK; - I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT; - LL_I2C_Init(I2C2, &I2C_InitStruct); + LL_TIM_OC_DisableFast(TIM3, LL_TIM_CHANNEL_CH1); - LL_I2C_SetOwnAddress2(I2C2, 0); + TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_ACTIVE; + TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE; + TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE; + LL_TIM_OC_Init(TIM3, LL_TIM_CHANNEL_CH2, &TIM_OC_InitStruct); - LL_I2C_DisableOwnAddress2(I2C2); + LL_TIM_OC_DisableFast(TIM3, LL_TIM_CHANNEL_CH2); - LL_I2C_DisableGeneralCall(I2C2); + LL_TIM_SetTriggerOutput(TIM3, LL_TIM_TRGO_RESET); - LL_I2C_EnableClockStretching(I2C2); + LL_TIM_DisableMasterSlaveMode(TIM3); + + /**TIM3 GPIO Configuration + PA7 ------> TIM3_CH2 + */ + GPIO_InitStruct.Pin = THERMO_Pin; + GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE; + GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH; + GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN; + LL_GPIO_Init(THERMO_GPIO_Port, &GPIO_InitStruct); } @@ -357,6 +383,45 @@ static void MX_TIM4_Init(void) } +/* USART1 init function */ +static void MX_USART1_UART_Init(void) +{ + + LL_USART_InitTypeDef USART_InitStruct; + + LL_GPIO_InitTypeDef GPIO_InitStruct; + + /* Peripheral clock enable */ + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1); + + /**USART1 GPIO Configuration + PA9 ------> USART1_TX + PA10 ------> USART1_RX + */ + GPIO_InitStruct.Pin = LL_GPIO_PIN_9; + GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE; + GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH; + GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL; + LL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = LL_GPIO_PIN_10; + GPIO_InitStruct.Mode = LL_GPIO_MODE_FLOATING; + LL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + USART_InitStruct.BaudRate = 115200; + USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B; + USART_InitStruct.StopBits = LL_USART_STOPBITS_1; + USART_InitStruct.Parity = LL_USART_PARITY_NONE; + USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX; + USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE; + LL_USART_Init(USART1, &USART_InitStruct); + + LL_USART_ConfigAsyncMode(USART1); + + LL_USART_Enable(USART1); + +} + /* USB init function */ static void MX_USB_PCD_Init(void) { @@ -375,6 +440,22 @@ static void MX_USB_PCD_Init(void) } +/** + * Enable DMA controller clock + */ +static void MX_DMA_Init(void) +{ + /* Init with LL driver */ + /* DMA controller clock enable */ + LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1); + + /* DMA interrupt init */ + /* DMA1_Channel6_IRQn interrupt configuration */ + NVIC_SetPriority(DMA1_Channel6_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0)); + NVIC_EnableIRQ(DMA1_Channel6_IRQn); + +} + /** Configure pins as * Analog * Input @@ -390,11 +471,14 @@ static void MX_GPIO_Init(void) /* GPIO Ports Clock Enable */ LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC); LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOD); - LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB); LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA); + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB); - /**/ - LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin); + /* LED output is OC, active low, defaults to high */ + LL_GPIO_SetOutputPin(LED_GPIO_Port, LED_Pin); + + /* SSR outputs are OC, active low, default to high */ + LL_GPIO_SetOutputPin(GPIOB, SSR3_Pin|SSR4_Pin|SSR1_Pin|SSR2_Pin); /**/ GPIO_InitStruct.Pin = LED_Pin; @@ -404,9 +488,15 @@ static void MX_GPIO_Init(void) LL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct); /**/ - GPIO_InitStruct.Pin = BMP_DONE_Pin; - GPIO_InitStruct.Mode = LL_GPIO_MODE_FLOATING; - LL_GPIO_Init(BMP_DONE_GPIO_Port, &GPIO_InitStruct); + GPIO_InitStruct.Pin = SSR3_Pin|SSR4_Pin|SSR1_Pin|SSR2_Pin; + GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT; + GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN; + LL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + // XXX: By some magic, the above initialization does not always work, + // so we repeat it here. + LL_GPIO_SetOutputPin(GPIOB, SSR3_Pin|SSR4_Pin|SSR1_Pin|SSR2_Pin); }