From 25fbe7b4b71566ec1b9ab9f10b827eb7e527f711 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Tue, 26 Jun 2018 15:37:32 +0200 Subject: [PATCH] Time display through USB --- Inc/app.h | 6 ++++++ Src/display.c | 32 ++++++++++++++++++++++++++++++++ Src/main.c | 37 ++++++++++++++++++++++++++++++++----- Src/stm32f1xx_it.c | 7 +------ Src/usbdev.c | 11 +++++++++++ host/test.c | 19 ++++++++++++++++--- 6 files changed, 98 insertions(+), 14 deletions(-) diff --git a/Inc/app.h b/Inc/app.h index bd81ad0..d975287 100644 --- a/Inc/app.h +++ b/Inc/app.h @@ -1,4 +1,10 @@ +// main.c + +extern byte rx_display[8]; +extern volatile byte rx_display_ready; + // display.c void display_init(void); void display_counter(uint cnt); +void display_buffer(byte *buf); diff --git a/Src/display.c b/Src/display.c index a8aecf3..2af6ad1 100644 --- a/Src/display.c +++ b/Src/display.c @@ -157,3 +157,35 @@ void display_counter(uint cnt) display_data_end(); } } + +void display_buffer(byte *buf) +{ + for (uint p=0; p<4; p++) + { + display_cmd(SSD1306_SETSTARTPAGE + p); + display_cmd(SSD1306_SETHIGHCOLUMN); + display_cmd(SSD1306_SETLOWCOLUMN); + display_data_start(); + for (uint i=0; i<5; i++) + { + uint ch = buf[i]; + if (ch <= 10) + { + for (uint j=0; j<23; j++) + { + byte x = Gentium23x32[(23*4+1)*ch + 1 + 4*j + p]; + display_data(x); + } + } + else + { + for (uint j=0; j<23; j++) + display_data(0); + } + display_data(0); + display_data(0); + display_data(0); + } + display_data_end(); + } +} diff --git a/Src/main.c b/Src/main.c index 57349e4..bb225b2 100644 --- a/Src/main.c +++ b/Src/main.c @@ -45,6 +45,8 @@ #include "usb.h" #include "app.h" +#include + /* USER CODE END Includes */ /* Private variables ---------------------------------------------------------*/ @@ -71,6 +73,8 @@ static void MX_TIM4_Init(void); /* USER CODE END PFP */ /* USER CODE BEGIN 0 */ +byte rx_display[8]; +volatile byte rx_display_ready; /* USER CODE END 0 */ @@ -128,17 +132,40 @@ int main(void) LL_TIM_EnableIT_UPDATE(TIM4); LL_TIM_GenerateEvent_UPDATE(TIM4); + { + byte buf[5] = { 0xff, 0xff, 10, 0xff, 0xff }; + display_buffer(buf); + } + /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ - int cnt = 0; while (1) { - debug_printf("Counter = %d\n", cnt); - display_counter(cnt); - LL_mDelay(1000); - cnt++; + __disable_irq(); + if (rx_display_ready) + { + byte rx[8]; + rx_display_ready = 0; + memcpy(rx, rx_display, 8); + __enable_irq(); + display_buffer(rx); + } + else + __enable_irq(); + + // debug_printf("Counter = %d\n", cnt); + // display_counter(cnt); + + 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; + + __WFI(); /* USER CODE END WHILE */ diff --git a/Src/stm32f1xx_it.c b/Src/stm32f1xx_it.c index 67ae5fe..1754251 100644 --- a/Src/stm32f1xx_it.c +++ b/Src/stm32f1xx_it.c @@ -230,12 +230,7 @@ void TIM4_IRQHandler(void) /* USER CODE BEGIN TIM4_IRQn 0 */ if (LL_TIM_IsActiveFlag_UPDATE(TIM4)) { - 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; + // FIXME LL_TIM_ClearFlag_UPDATE(TIM4); } diff --git a/Src/usbdev.c b/Src/usbdev.c index ca98cd3..9713bc8 100644 --- a/Src/usbdev.c +++ b/Src/usbdev.c @@ -3,6 +3,9 @@ #include "util.h" #include "usb.h" +#include "app.h" + +#include /*** Descriptors ***/ @@ -135,6 +138,14 @@ void usb_dev_recv_done(struct usb *usb, byte epnum) { // usb_tx_buf[0]++; // usb_ep_send(usb, 0x82, usb_tx_buf, 33); + + u32 len = usb_ep_received_size(usb, 0x01); + if (len >= 8) + { + memcpy(rx_display, usb_rx_buf, 8); + rx_display_ready = 1; + } + usb_ep_receive(usb, 0x01, usb_rx_buf, 64); } } diff --git a/host/test.c b/host/test.c index 81d428e..07b804e 100644 --- a/host/test.c +++ b/host/test.c @@ -1,5 +1,7 @@ #include #include +#include +#include #include struct libusb_context *usb_ctxt; @@ -61,16 +63,25 @@ int main(void) for (;;) { - unsigned char req[64] = { 1, 2, 3, 4 }; + time_t t = time(NULL); + struct tm *tm = localtime(&t); + + unsigned char req[8] = { + tm->tm_hour / 10, + tm->tm_hour % 10, + (tm->tm_sec % 2 ? 10 : 0xff), + tm->tm_min / 10, + tm->tm_min % 10, + }; int transferred; - if (err = libusb_bulk_transfer(devh, 0x01, req, 32, &transferred, 2000)) + if (err = libusb_bulk_transfer(devh, 0x01, req, 8, &transferred, 2000)) { fprintf(stderr, "Transfer failed: error %d\n", err); exit(1); } printf("Transferred %d bytes\n", transferred); -#if 1 +#if 0 unsigned char resp[1000]; int received; if (err = libusb_bulk_transfer(devh, 0x82, resp, 1000, &received, 2000)) @@ -80,6 +91,8 @@ int main(void) } printf("Received %d bytes [%02x]\n", received, resp[0]); #endif + + sleep(1); } return 0; -- 2.39.2