| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE.md file. | |
| 4 | |
| 5 #include <stdlib.h> | |
| 6 | |
| 7 #include <cmsis_os.h> | |
| 8 extern "C" { | |
| 9 #include <lcd_log.h> | |
| 10 } | |
| 11 #include <stm32746g_discovery.h> | |
| 12 #include <stm32746g_discovery_lcd.h> | |
| 13 | |
| 14 #include "include/fletch_api.h" | |
| 15 #include "include/static_ffi.h" | |
| 16 | |
| 17 #include "platforms/stm/disco_fletch/src/fletch_entry.h" | |
| 18 #include "platforms/stm/disco_fletch/src/page_allocator.h" | |
| 19 #include "platforms/stm/disco_fletch/src/uart.h" | |
| 20 #include "src/shared/utils.h" | |
| 21 | |
| 22 extern unsigned char _binary_snapshot_start; | |
| 23 extern unsigned char _binary_snapshot_end; | |
| 24 extern unsigned char _binary_snapshot_size; | |
| 25 | |
| 26 extern PageAllocator* page_allocator; | |
| 27 | |
| 28 Uart* uart; | |
| 29 | |
| 30 extern "C" size_t UartRead(uint8_t* buffer, size_t count) { | |
| 31 return uart->Read(buffer, count); | |
| 32 } | |
| 33 | |
| 34 extern "C" size_t UartWrite(uint8_t* buffer, size_t count) { | |
| 35 return uart->Write(buffer, count); | |
| 36 } | |
| 37 | |
| 38 extern "C" void LCDDrawLine( | |
| 39 uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2) { | |
| 40 // BSP_LCD_DrawLine takes uint16_t arguments. | |
| 41 BSP_LCD_DrawLine(x1, y1, x2, y2); | |
| 42 } | |
| 43 | |
| 44 // Implementation of write used from syscalls.c to redirect all printf | |
| 45 // calls to the print interceptors. | |
| 46 extern "C" int Write(int file, char *ptr, int len) { | |
| 47 for (int i = 0; i < len; i++) { | |
| 48 if (file == 2) { | |
| 49 fletch::Print::Error("%c", *ptr++); | |
| 50 } else { | |
| 51 fletch::Print::Out("%c", *ptr++); | |
| 52 } | |
| 53 } | |
| 54 return len; | |
| 55 } | |
| 56 | |
| 57 FLETCH_EXPORT_TABLE_BEGIN | |
| 58 FLETCH_EXPORT_TABLE_ENTRY("uart_read", UartRead) | |
| 59 FLETCH_EXPORT_TABLE_ENTRY("uart_write", UartWrite) | |
| 60 FLETCH_EXPORT_TABLE_ENTRY("lcd_height", BSP_LCD_GetYSize) | |
| 61 FLETCH_EXPORT_TABLE_ENTRY("lcd_width", BSP_LCD_GetXSize) | |
| 62 FLETCH_EXPORT_TABLE_ENTRY("lcd_clear", BSP_LCD_Clear) | |
| 63 FLETCH_EXPORT_TABLE_ENTRY("lcd_draw_line", LCDDrawLine) | |
| 64 FLETCH_EXPORT_TABLE_ENTRY("lcd_set_foreground_color", BSP_LCD_SetTextColor) | |
| 65 FLETCH_EXPORT_TABLE_ENTRY("lcd_set_background_color", BSP_LCD_SetBackColor) | |
| 66 FLETCH_EXPORT_TABLE_ENTRY("lcd_display_string", BSP_LCD_DisplayStringAt) | |
| 67 FLETCH_EXPORT_TABLE_END | |
| 68 | |
| 69 // Run fletch on the linked in snapshot. | |
| 70 void StartFletch(void const * argument) { | |
| 71 fletch::Print::Out("Setup fletch\n"); | |
| 72 FletchSetup(); | |
| 73 fletch::Print::Out("Reading fletch snapshot\n"); | |
| 74 unsigned char *snapshot = &_binary_snapshot_start; | |
| 75 int snapshot_size = reinterpret_cast<int>(&_binary_snapshot_size); | |
| 76 FletchProgram program = FletchLoadSnapshot(snapshot, snapshot_size); | |
| 77 fletch::Print::Out("Run fletch program\n"); | |
| 78 FletchRunMain(program); | |
| 79 fletch::Print::Out("Fletch program exited\n"); | |
| 80 } | |
| 81 | |
| 82 void UartPrintIntercepter(const char* message, int out, void* data) { | |
| 83 int len = strlen(message); | |
| 84 for (int i = 0; i < len; i++) { | |
| 85 if (message[i] == '\n') { | |
| 86 uart->Write(reinterpret_cast<const uint8_t*>("\r"), 1); | |
| 87 } | |
| 88 uart->Write(reinterpret_cast<const uint8_t*>(message + i), 1); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 // LCDLogPutchar is defined by the STM LCD log utility | |
| 93 // (Utilities/Log/lcd_log.c) by means of the macro definitions of | |
| 94 // LCD_LOG_PUTCHAR in lcd_log_conf.h. | |
| 95 extern "C" int LCDLogPutchar(int ch); | |
| 96 void LCDPrintIntercepter(const char* message, int out, void* data) { | |
| 97 int len = strlen(message); | |
| 98 if (out == 3) { | |
| 99 LCD_LineColor = LCD_COLOR_RED; | |
| 100 } else { | |
| 101 LCD_LineColor = LCD_COLOR_BLACK; | |
| 102 } | |
| 103 for (int i = 0; i < len; i++) { | |
| 104 LCDLogPutchar(message[i]); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 // Main task entry point from FreeRTOS. | |
| 109 void FletchEntry(void const * argument) { | |
| 110 // Add an arena of the 8Mb of external memory. | |
| 111 uint32_t ext_mem_arena = | |
| 112 page_allocator->AddArena("ExtMem", 0xc0000000, 0x800000); | |
| 113 | |
| 114 // Initialize the LCD. | |
| 115 size_t fb_bytes = (RK043FN48H_WIDTH * RK043FN48H_HEIGHT * 2); | |
| 116 size_t fb_pages = page_allocator->PagesForBytes(fb_bytes); | |
| 117 void* fb = page_allocator->AllocatePages(fb_pages, ext_mem_arena); | |
| 118 BSP_LCD_Init(); | |
| 119 BSP_LCD_LayerDefaultInit(1, reinterpret_cast<uint32_t>(fb)); | |
| 120 BSP_LCD_SelectLayer(1); | |
| 121 BSP_LCD_SetFont(&LCD_DEFAULT_FONT); | |
| 122 | |
| 123 // Initialize LCD Log module. | |
| 124 LCD_LOG_Init(); | |
| 125 LCD_LOG_SetHeader(reinterpret_cast<uint8_t*>(const_cast<char*>("Fletch"))); | |
| 126 LCD_LOG_SetFooter(reinterpret_cast<uint8_t*>(const_cast<char*>( | |
| 127 "STM32746G-Discovery"))); | |
| 128 FletchRegisterPrintInterceptor(LCDPrintIntercepter, NULL); | |
| 129 | |
| 130 // For now always start the UART. | |
| 131 uart = new Uart(); | |
| 132 uart->Start(); | |
| 133 | |
| 134 FletchRegisterPrintInterceptor(UartPrintIntercepter, NULL); | |
| 135 | |
| 136 // Always disable standard out, as this will cause infinite | |
| 137 // recursion in the syscalls.c handling of write. | |
| 138 fletch::Print::DisableStandardOutput(); | |
| 139 | |
| 140 StartFletch(argument); | |
| 141 | |
| 142 // No more to do right now. | |
| 143 for (;;) { | |
| 144 osDelay(1); | |
| 145 } | |
| 146 } | |
| OLD | NEW |