1/* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/******************************************************************************
17 * Copyright 2013-2014 Espressif Systems (Wuxi)
18 *
19 * FileName: user_main.c
20 *
21 * Description: entry file of user application
22 *
23 * Modification history:
24 *     2014/12/1, v1.0 create this file.
25*******************************************************************************/
26
27#include "esp_common.h"
28#include "freertos/FreeRTOS.h"
29#include "freertos/task.h"
30#include "uart.h"
31
32#include "user_config.h"
33#include "jerry_run.h"
34#include "jerry-targetjs.h"
35
36static void show_free_mem(int idx) {
37  size_t res = xPortGetFreeHeapSize();
38  printf("dbg free memory(%d): %d\r\n", idx, res);
39}
40
41static int jerry_task_init(void) {
42  DECLARE_JS_CODES;
43
44  js_entry();
45
46  /* run rest of the js files first */
47  show_free_mem(2);
48  for (int src = 1; js_codes[src].source; src++) {
49    int retcode = js_eval(js_codes[src].source, js_codes[src].length);
50    if (retcode != 0) {
51      printf("js_eval failed code(%d) [%s]\r\n", retcode, js_codes[src].name);
52      return -1;
53    }
54  }
55
56  /* run main.js */
57  int retcode = js_eval(js_codes[0].source, js_codes[0].length);
58  if (retcode != 0) {
59    printf("js_eval failed code(%d) [%s]\r\n", retcode, js_codes[0].name);
60    return -2;
61  }
62  show_free_mem(3);
63  return 0;
64}
65
66static void jerry_task(void *pvParameters) {
67  if (jerry_task_init() == 0) {
68    const portTickType xDelay = 100 / portTICK_RATE_MS;
69    uint32_t ticknow = 0;
70
71    for (;;) {
72      vTaskDelay(xDelay);
73      js_loop(ticknow);
74      if (!ticknow) {
75        show_free_mem(4);
76      }
77      ticknow++;
78    }
79  }
80  js_exit();
81}
82
83/*
84 * This is entry point for user code
85 */
86void ICACHE_FLASH_ATTR user_init(void)
87{
88  UART_SetBaudrate(UART0, BIT_RATE_115200);
89
90  show_free_mem(0);
91  wifi_softap_dhcps_stop();
92  show_free_mem(1);
93
94  PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0);    // GPIO 0
95  PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2);    // GPIO 2
96
97  xTaskCreate(jerry_task, "jerry", JERRY_STACK_SIZE, NULL, 2, NULL);
98}
99
100/*
101 * FunctionName : user_rf_cal_sector_set
102 * Description  : SDK just reserved 4 sectors, used for rf init data and Parameters.
103 *                We add this function to force users to set rf cal sector, since
104 *                we don't know which sector is free in user's application.
105 *                sector map for last several sectors : ABCCC
106 *                A : rf cal
107 *                B : rf init data
108 *                C : sdk parameters
109 * Parameters   : none
110 * Returns      : rf cal sector
111 */
112uint32 user_rf_cal_sector_set(void)
113{
114    flash_size_map size_map = system_get_flash_size_map();
115    uint32 rf_cal_sec = 0;
116
117    switch (size_map) {
118        case FLASH_SIZE_4M_MAP_256_256:
119            rf_cal_sec = 128 - 5;
120            break;
121
122        case FLASH_SIZE_8M_MAP_512_512:
123            rf_cal_sec = 256 - 5;
124            break;
125
126        case FLASH_SIZE_16M_MAP_512_512:
127        case FLASH_SIZE_16M_MAP_1024_1024:
128            rf_cal_sec = 512 - 5;
129            break;
130
131        case FLASH_SIZE_32M_MAP_512_512:
132        case FLASH_SIZE_32M_MAP_1024_1024:
133            rf_cal_sec = 1024 - 5;
134            break;
135        case FLASH_SIZE_64M_MAP_1024_1024:
136            rf_cal_sec = 2048 - 5;
137            break;
138        case FLASH_SIZE_128M_MAP_1024_1024:
139            rf_cal_sec = 4096 - 5;
140            break;
141        default:
142            rf_cal_sec = 0;
143            break;
144    }
145
146    return rf_cal_sec;
147}
148