main.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "usart.h"
  23. #include "gpio.h"
  24. #ifdef USING_PERF_COUNTER
  25. #include "perf_counter.h"
  26. #endif
  27. #include "PikaVM.h"
  28. /* Private includes ----------------------------------------------------------*/
  29. /* USER CODE BEGIN Includes */
  30. #include "pikaScript.h"
  31. /* USER CODE END Includes */
  32. /* Private typedef -----------------------------------------------------------*/
  33. /* USER CODE BEGIN PTD */
  34. /* USER CODE END PTD */
  35. /* Private define ------------------------------------------------------------*/
  36. /* USER CODE BEGIN PD */
  37. /* USER CODE END PD */
  38. /* Private macro -------------------------------------------------------------*/
  39. /* USER CODE BEGIN PM */
  40. /* USER CODE END PM */
  41. /* Private variables ---------------------------------------------------------*/
  42. /* USER CODE BEGIN PV */
  43. /* USER CODE END PV */
  44. /* Private function prototypes -----------------------------------------------*/
  45. void SystemClock_Config(void);
  46. /* USER CODE BEGIN PFP */
  47. /* USER CODE END PFP */
  48. /* Private user code ---------------------------------------------------------*/
  49. /* USER CODE BEGIN 0 */
  50. char __platform_getchar(void) {
  51. uint8_t ch;
  52. while(1){
  53. if(HAL_OK == HAL_UART_Receive(&huart1, &ch, 1, 0)){
  54. return ch;
  55. }
  56. }
  57. }
  58. static void prime_number_100_c(void) {
  59. volatile int num = 0;
  60. /* run */
  61. for (volatile int i = 2; i < 100; i++) {
  62. volatile int is_prime = 1;
  63. for (volatile int j = 2; j < i; j++) {
  64. if (i % j == 0) {
  65. is_prime = 0;
  66. break;
  67. }
  68. }
  69. if (is_prime) {
  70. num = num + i;
  71. }
  72. }
  73. if ( num != 1060){
  74. printf("[error] in benchmark prime_number_100_c \r\n");
  75. }
  76. }
  77. /* USER CODE END 0 */
  78. /**
  79. * @brief The application entry point.
  80. * @retval int
  81. */
  82. int main(void)
  83. {
  84. /* USER CODE BEGIN 1 */
  85. /* USER CODE END 1 */
  86. /* MCU Configuration--------------------------------------------------------*/
  87. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  88. HAL_Init();
  89. /* USER CODE BEGIN Init */
  90. /* USER CODE END Init */
  91. /* Configure the system clock */
  92. SystemClock_Config();
  93. /* USER CODE BEGIN SysInit */
  94. /* USER CODE END SysInit */
  95. /* Initialize all configured peripherals */
  96. MX_GPIO_Init();
  97. MX_USART1_UART_Init();
  98. /* USER CODE BEGIN 2 */
  99. /* USER CODE END 2 */
  100. /* Infinite loop */
  101. /* USER CODE BEGIN WHILE */
  102. /* user input buff */
  103. PikaObj* pikaMain = NULL;
  104. /* run unit test */
  105. pikaMain = pikaScriptInit();
  106. #ifdef USING_PERF_COUNTER
  107. /* benchmark */
  108. uint64_t nCycleUsed_c,nCycleUsed_pika = 0;
  109. printf("[----------benchmark----------]\r\n");
  110. __cycleof__("", {
  111. nCycleUsed_c = _;
  112. printf("[prime_number_100_c] Cycle Used %lld\r\n", _);
  113. }) {
  114. prime_number_100_c();
  115. }
  116. /* create pikaMain root obj */
  117. pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
  118. /* clang-format off */
  119. PIKA_PYTHON(
  120. num = 0
  121. i = 2
  122. for i in range(2,100):
  123. j=2
  124. is_prime = 1
  125. for j in range(2,i):
  126. if i%j==0 :
  127. is_prime = 0
  128. break
  129. if is_prime:
  130. num = num + i
  131. )
  132. /* clang-format on */
  133. const uint8_t bytes[] = {
  134. 0xb4, 0x00, 0x00, 0x00, /* instruct array size */
  135. 0x00, 0x85, 0x01, 0x00, 0x00, 0x04, 0x03, 0x00, 0x00, 0x85, 0x07, 0x00,
  136. 0x00, 0x04, 0x09, 0x00, 0x20, 0x85, 0x07, 0x00, 0x20, 0x05, 0x0b, 0x00,
  137. 0x10, 0x02, 0x0f, 0x00, 0x00, 0x02, 0x15, 0x00, 0x00, 0x04, 0x1a, 0x00,
  138. 0x00, 0x82, 0x1e, 0x00, 0x00, 0x04, 0x09, 0x00, 0x00, 0x0d, 0x09, 0x00,
  139. 0x00, 0x07, 0x07, 0x00, 0x01, 0x85, 0x07, 0x00, 0x01, 0x04, 0x2b, 0x00,
  140. 0x01, 0x85, 0x2d, 0x00, 0x01, 0x04, 0x2f, 0x00, 0x21, 0x85, 0x07, 0x00,
  141. 0x21, 0x01, 0x09, 0x00, 0x11, 0x02, 0x0f, 0x00, 0x01, 0x02, 0x15, 0x00,
  142. 0x01, 0x04, 0x38, 0x00, 0x01, 0x82, 0x3c, 0x00, 0x01, 0x04, 0x2b, 0x00,
  143. 0x01, 0x0d, 0x2b, 0x00, 0x01, 0x07, 0x07, 0x00, 0x22, 0x81, 0x09, 0x00,
  144. 0x22, 0x01, 0x2b, 0x00, 0x12, 0x08, 0x49, 0x00, 0x12, 0x05, 0x01, 0x00,
  145. 0x02, 0x08, 0x4b, 0x00, 0x02, 0x07, 0x2d, 0x00, 0x03, 0x85, 0x01, 0x00,
  146. 0x03, 0x04, 0x2f, 0x00, 0x03, 0x8e, 0x00, 0x00, 0x01, 0x86, 0x4e, 0x00,
  147. 0x01, 0x8c, 0x38, 0x00, 0x01, 0x81, 0x2f, 0x00, 0x01, 0x07, 0x2d, 0x00,
  148. 0x12, 0x81, 0x03, 0x00, 0x12, 0x01, 0x09, 0x00, 0x02, 0x08, 0x51, 0x00,
  149. 0x02, 0x04, 0x03, 0x00, 0x00, 0x86, 0x4e, 0x00, 0x00, 0x8c, 0x1a, 0x00,
  150. /* instruct array */
  151. 0x53, 0x00, 0x00, 0x00, /* const pool size */
  152. 0x00, 0x30, 0x00, 0x6e, 0x75, 0x6d, 0x00, 0x32, 0x00, 0x69, 0x00, 0x31, 0x30,
  153. 0x30, 0x00, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x00, 0x69, 0x74, 0x65, 0x72,
  154. 0x00, 0x24, 0x6c, 0x30, 0x00, 0x24, 0x6c, 0x30, 0x2e, 0x5f, 0x5f, 0x6e,
  155. 0x65, 0x78, 0x74, 0x5f, 0x5f, 0x00, 0x6a, 0x00, 0x31, 0x00, 0x69, 0x73,
  156. 0x5f, 0x70, 0x72, 0x69, 0x6d, 0x65, 0x00, 0x24, 0x6c, 0x31, 0x00, 0x24,
  157. 0x6c, 0x31, 0x2e, 0x5f, 0x5f, 0x6e, 0x65, 0x78, 0x74, 0x5f, 0x5f, 0x00,
  158. 0x25, 0x00, 0x3d, 0x3d, 0x00, 0x2d, 0x31, 0x00, 0x2b, 0x00, /* const pool */
  159. };
  160. __cycleof__("", {
  161. nCycleUsed_pika = _;
  162. printf("[prime_number_100_pika] Cycle Used %lld\r\n", _);
  163. }) {
  164. /* run pika ByteCode */
  165. pikaVM_runByteCode(pikaMain, (uint8_t*)bytes);
  166. }
  167. int num = obj_getInt(pikaMain, "num");
  168. if ( num != 1060){
  169. printf("[error] in benchmark prime_number_100_pika \r\n");
  170. }
  171. /* free the pikaMain obj */
  172. obj_deinit(pikaMain);
  173. uint64_t benchmark_result = ((double) nCycleUsed_c / (double)nCycleUsed_pika) * 100 * 100000;
  174. printf("\r\n[------benchmark finished ---------]\r\n");
  175. printf("benchmakr result :%lld\r\n", benchmark_result);
  176. #endif
  177. pikaScriptShell(pikaMain);
  178. while (1)
  179. {
  180. }
  181. /* USER CODE END 3 */
  182. }
  183. /**
  184. * @brief System Clock Configuration
  185. * @retval None
  186. */
  187. void SystemClock_Config(void)
  188. {
  189. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  190. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  191. /** Initializes the RCC Oscillators according to the specified parameters
  192. * in the RCC_OscInitTypeDef structure.
  193. */
  194. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  195. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  196. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  197. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  198. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  199. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  200. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  201. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  202. {
  203. Error_Handler();
  204. }
  205. /** Initializes the CPU, AHB and APB buses clocks
  206. */
  207. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  208. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  209. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  210. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  211. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  212. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  213. {
  214. Error_Handler();
  215. }
  216. }
  217. /* USER CODE BEGIN 4 */
  218. /* USER CODE END 4 */
  219. /**
  220. * @brief This function is executed in case of error occurrence.
  221. * @retval None
  222. */
  223. void Error_Handler(void)
  224. {
  225. /* USER CODE BEGIN Error_Handler_Debug */
  226. /* User can add his own implementation to report the HAL error return state */
  227. __disable_irq();
  228. while (1)
  229. {
  230. }
  231. /* USER CODE END Error_Handler_Debug */
  232. }
  233. #ifdef USE_FULL_ASSERT
  234. /**
  235. * @brief Reports the name of the source file and the source line number
  236. * where the assert_param error has occurred.
  237. * @param file: pointer to the source file name
  238. * @param line: assert_param error line source number
  239. * @retval None
  240. */
  241. void assert_failed(uint8_t *file, uint32_t line)
  242. {
  243. /* USER CODE BEGIN 6 */
  244. /* User can add his own implementation to report the file name and line number,
  245. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  246. /* USER CODE END 6 */
  247. }
  248. #endif /* USE_FULL_ASSERT */
  249. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/