esp8266.pp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. unit esp8266;
  2. interface
  3. const
  4. //unit: Hz
  5. APB_CLK_FREQ = 80*1000000;
  6. UART_CLK_FREQ = APB_CLK_FREQ;
  7. //divided by 256
  8. TIMER_CLK_FREQ = (APB_CLK_FREQ shr 8);
  9. //Peripheral device base address
  10. PERIPHS_DPORT_BASEADDR = $3ff00000;
  11. PERIPHS_GPIO_BASEADDR = $60000300;
  12. PERIPHS_TIMER_BASEDDR = $60000600;
  13. PERIPHS_RTC_BASEADDR = $60000700;
  14. PERIPHS_IO_MUX = $60000800;
  15. //Interrupt remap control registers
  16. EDGE_INT_ENABLE_REG = (PERIPHS_DPORT_BASEADDR+$04);
  17. // TM1_EDGE_INT_ENABLE() = SET_PERI_REG_MASK(EDGE_INT_ENABLE_REG, $2);
  18. // TM1_EDGE_INT_DISABLE() = CLEAR_PERI_REG_MASK(EDGE_INT_ENABLE_REG, $2);
  19. //GPIO reg
  20. // GPIO_REG_READ(reg) = READ_PERI_REG(PERIPHS_GPIO_BASEADDR + reg);
  21. // GPIO_REG_WRITE(reg, val) = WRITE_PERI_REG(PERIPHS_GPIO_BASEADDR + reg, val);
  22. GPIO_OUT_ADDRESS = $00;
  23. GPIO_OUT_W1TS_ADDRESS = $04;
  24. GPIO_OUT_W1TC_ADDRESS = $08;
  25. GPIO_ENABLE_ADDRESS = $0c;
  26. GPIO_ENABLE_W1TS_ADDRESS = $10;
  27. GPIO_ENABLE_W1TC_ADDRESS = $14;
  28. GPIO_OUT_W1TC_DATA_MASK = $0000ffff;
  29. GPIO_IN_ADDRESS = $18;
  30. GPIO_STATUS_ADDRESS = $1c;
  31. GPIO_STATUS_W1TS_ADDRESS = $20;
  32. GPIO_STATUS_W1TC_ADDRESS = $24;
  33. GPIO_STATUS_INTERRUPT_MASK = $0000ffff;
  34. GPIO_RTC_CALIB_SYNC = PERIPHS_GPIO_BASEADDR+$6c;
  35. //first write to zero, then to one to start
  36. RTC_CALIB_START = $80000000;
  37. //max 8ms
  38. RTC_PERIOD_NUM_MASK = $3ff;
  39. GPIO_RTC_CALIB_VALUE = PERIPHS_GPIO_BASEADDR+$70;
  40. //after measure, flag to one, when start from zero to one, turn to zero
  41. RTC_CALIB_RDY_S = 31;
  42. RTC_CALIB_VALUE_MASK = $fffff;
  43. GPIO_PIN0_ADDRESS = $28;
  44. GPIO_ID_PIN0 = 0;
  45. // GPIO_ID_PIN(n) = (GPIO_ID_PIN0+(n));
  46. GPIO_LAST_REGISTER_ID = 15;
  47. GPIO_ID_NONE = $ffffffff;
  48. GPIO_PIN_COUNT = 16;
  49. GPIO_PIN_CONFIG_MSB = 12;
  50. GPIO_PIN_CONFIG_LSB = 11;
  51. GPIO_PIN_CONFIG_MASK = $00001800;
  52. // GPIO_PIN_CONFIG_GET(x) = (((x) and GPIO_PIN_CONFIG_MASK) shr GPIO_PIN_CONFIG_LSB);
  53. // GPIO_PIN_CONFIG_SET(x) = (((x) shl GPIO_PIN_CONFIG_LSB) and GPIO_PIN_CONFIG_MASK);
  54. GPIO_WAKEUP_ENABLE = 1;
  55. GPIO_WAKEUP_DISABLE = (not GPIO_WAKEUP_ENABLE);
  56. GPIO_PIN_WAKEUP_ENABLE_MSB = 10;
  57. GPIO_PIN_WAKEUP_ENABLE_LSB = 10;
  58. GPIO_PIN_WAKEUP_ENABLE_MASK = $00000400;
  59. // GPIO_PIN_WAKEUP_ENABLE_GET(x) = (((x) and GPIO_PIN_WAKEUP_ENABLE_MASK) shr GPIO_PIN_WAKEUP_ENABLE_LSB);
  60. // GPIO_PIN_WAKEUP_ENABLE_SET(x) = (((x) shl GPIO_PIN_WAKEUP_ENABLE_LSB) and GPIO_PIN_WAKEUP_ENABLE_MASK);
  61. GPIO_PIN_INT_TYPE_MASK = $380;
  62. GPIO_PIN_INT_TYPE_MSB = 9;
  63. GPIO_PIN_INT_TYPE_LSB = 7;
  64. // GPIO_PIN_INT_TYPE_GET(x) = (((x) and GPIO_PIN_INT_TYPE_MASK) shr GPIO_PIN_INT_TYPE_LSB);
  65. // GPIO_PIN_INT_TYPE_SET(x) = (((x) shl GPIO_PIN_INT_TYPE_LSB) and GPIO_PIN_INT_TYPE_MASK);
  66. GPIO_PAD_DRIVER_ENABLE = 1;
  67. GPIO_PAD_DRIVER_DISABLE = (not GPIO_PAD_DRIVER_ENABLE);
  68. GPIO_PIN_PAD_DRIVER_MSB = 2;
  69. GPIO_PIN_PAD_DRIVER_LSB = 2;
  70. GPIO_PIN_PAD_DRIVER_MASK = $00000004;
  71. // GPIO_PIN_PAD_DRIVER_GET(x) = (((x) and GPIO_PIN_PAD_DRIVER_MASK) shr GPIO_PIN_PAD_DRIVER_LSB);
  72. // GPIO_PIN_PAD_DRIVER_SET(x) = (((x) shl GPIO_PIN_PAD_DRIVER_LSB) and GPIO_PIN_PAD_DRIVER_MASK);
  73. GPIO_AS_PIN_SOURCE = 0;
  74. SIGMA_AS_PIN_SOURCE = (not GPIO_AS_PIN_SOURCE);
  75. GPIO_PIN_SOURCE_MSB = 0;
  76. GPIO_PIN_SOURCE_LSB = 0;
  77. GPIO_PIN_SOURCE_MASK = $00000001;
  78. // GPIO_PIN_SOURCE_GET(x) = (((x) and GPIO_PIN_SOURCE_MASK) shr GPIO_PIN_SOURCE_LSB);
  79. // GPIO_PIN_SOURCE_SET(x) = (((x) shl GPIO_PIN_SOURCE_LSB) and GPIO_PIN_SOURCE_MASK);
  80. // TIMER reg
  81. // RTC_REG_READ(addr) = READ_PERI_REG(PERIPHS_TIMER_BASEDDR + addr);
  82. // RTC_REG_WRITE(addr, val) = WRITE_PERI_REG(PERIPHS_TIMER_BASEDDR + addr, val);
  83. // RTC_CLR_REG_MASK(reg, mask) = CLEAR_PERI_REG_MASK(PERIPHS_TIMER_BASEDDR +reg, mask);
  84. // Returns the current time according to the timer timer.
  85. // NOW() = RTC_REG_READ(FRC2_COUNT_ADDRESS);
  86. //load initial_value to timer1
  87. FRC1_LOAD_ADDRESS = $00;
  88. //timer1's counter value(count from initial_value to 0)
  89. FRC1_COUNT_ADDRESS = $04;
  90. FRC1_CTRL_ADDRESS = $08;
  91. //clear timer1's interrupt when write this address
  92. FRC1_INT_ADDRESS = $0c;
  93. FRC1_INT_CLR_MASK = $00000001;
  94. //timer2's counter value(count from initial_value to 0)
  95. FRC2_COUNT_ADDRESS = $24;
  96. //RTC reg
  97. REG_RTC_BASE = PERIPHS_RTC_BASEADDR;
  98. RTC_STORE0 = (REG_RTC_BASE + $030);
  99. RTC_STORE1 = (REG_RTC_BASE + $034);
  100. RTC_STORE2 = (REG_RTC_BASE + $038);
  101. RTC_STORE3 = (REG_RTC_BASE + $03C);
  102. RTC_GPIO_OUT = (REG_RTC_BASE + $068);
  103. RTC_GPIO_ENABLE = (REG_RTC_BASE + $074);
  104. RTC_GPIO_IN_DATA = (REG_RTC_BASE + $08C);
  105. RTC_GPIO_CONF = (REG_RTC_BASE + $090);
  106. PAD_XPD_DCDC_CONF = (REG_RTC_BASE + $0A0);
  107. //PIN Mux reg
  108. PERIPHS_IO_MUX_FUNC = $13;
  109. PERIPHS_IO_MUX_FUNC_S = 4;
  110. PERIPHS_IO_MUX_PULLUP = $80;
  111. PERIPHS_IO_MUX_PULLUP2 = $40;
  112. PERIPHS_IO_MUX_SLEEP_PULLUP = $8;
  113. PERIPHS_IO_MUX_SLEEP_PULLUP2 = $4;
  114. PERIPHS_IO_MUX_SLEEP_OE = $2;
  115. PERIPHS_IO_MUX_OE = $1;
  116. PERIPHS_IO_MUX_CONF_U = (PERIPHS_IO_MUX + $00);
  117. SPI0_CLK_EQU_SYS_CLK = $100;
  118. SPI1_CLK_EQU_SYS_CLK = $200;
  119. PERIPHS_IO_MUX_MTDI_U = (PERIPHS_IO_MUX + $04);
  120. FUNC_GPIO12 = 3;
  121. PERIPHS_IO_MUX_MTCK_U = (PERIPHS_IO_MUX + $08);
  122. FUNC_GPIO13 = 3;
  123. PERIPHS_IO_MUX_MTMS_U = (PERIPHS_IO_MUX + $0C);
  124. FUNC_GPIO14 = 3;
  125. PERIPHS_IO_MUX_MTDO_U = (PERIPHS_IO_MUX + $10);
  126. FUNC_GPIO15 = 3;
  127. FUNC_U0RTS = 4;
  128. PERIPHS_IO_MUX_U0RXD_U = (PERIPHS_IO_MUX + $14);
  129. FUNC_GPIO3 = 3;
  130. PERIPHS_IO_MUX_U0TXD_U = (PERIPHS_IO_MUX + $18);
  131. FUNC_U0TXD = 0;
  132. FUNC_GPIO1 = 3;
  133. PERIPHS_IO_MUX_SD_CLK_U = (PERIPHS_IO_MUX + $1c);
  134. FUNC_SDCLK = 0;
  135. FUNC_SPICLK = 1;
  136. PERIPHS_IO_MUX_SD_DATA0_U = (PERIPHS_IO_MUX + $20);
  137. FUNC_SDDATA0 = 0;
  138. FUNC_SPIQ = 1;
  139. FUNC_U1TXD = 4;
  140. PERIPHS_IO_MUX_SD_DATA1_U = (PERIPHS_IO_MUX + $24);
  141. FUNC_SDDATA1 = 0;
  142. FUNC_SPID = 1;
  143. FUNC_U1RXD = 4;
  144. FUNC_SDDATA1_U1RXD = 7;
  145. PERIPHS_IO_MUX_SD_DATA2_U = (PERIPHS_IO_MUX + $28);
  146. FUNC_SDDATA2 = 0;
  147. FUNC_SPIHD = 1;
  148. FUNC_GPIO9 = 3;
  149. PERIPHS_IO_MUX_SD_DATA3_U = (PERIPHS_IO_MUX + $2c);
  150. FUNC_SDDATA3 = 0;
  151. FUNC_SPIWP = 1;
  152. FUNC_GPIO10 = 3;
  153. PERIPHS_IO_MUX_SD_CMD_U = (PERIPHS_IO_MUX + $30);
  154. FUNC_SDCMD = 0;
  155. FUNC_SPICS0 = 1;
  156. PERIPHS_IO_MUX_GPIO0_U = (PERIPHS_IO_MUX + $34);
  157. FUNC_GPIO0 = 0;
  158. PERIPHS_IO_MUX_GPIO2_U = (PERIPHS_IO_MUX + $38);
  159. FUNC_GPIO2 = 0;
  160. FUNC_U1TXD_BK = 2;
  161. FUNC_U0TXD_BK = 4;
  162. PERIPHS_IO_MUX_GPIO4_U = (PERIPHS_IO_MUX + $3C);
  163. FUNC_GPIO4 = 0;
  164. PERIPHS_IO_MUX_GPIO5_U = (PERIPHS_IO_MUX + $40);
  165. FUNC_GPIO5 = 0;
  166. // PIN_PULLUP_DIS(PIN_NAME) = CLEAR_PERI_REG_MASK(PIN_NAME, PERIPHS_IO_MUX_PULLUP);
  167. // PIN_PULLUP_EN(PIN_NAME) = SET_PERI_REG_MASK(PIN_NAME, PERIPHS_IO_MUX_PULLUP);
  168. implementation
  169. var
  170. _stack_top: record end; external name '_stack_top';
  171. _data: record end; external name '_data';
  172. _edata: record end; external name '_edata';
  173. _text_start: record end; external name '_text_start';
  174. _etext: record end; external name '_etext';
  175. _bss_start: record end; external name '_bss_start';
  176. _bss_end: record end; external name '_bss_end';
  177. procedure Pascalmain; external name 'PASCALMAIN';
  178. procedure HaltProc; assembler; nostackframe; public name'_haltproc';
  179. asm
  180. .Lloop:
  181. j .Lloop
  182. end;
  183. procedure Startup;
  184. var
  185. psrc,pdst,pend: plongword;
  186. begin
  187. // Copy .text
  188. psrc:=@_etext;
  189. pdst:=@_data;
  190. pend:=@_edata;
  191. while pdst<pend do
  192. begin
  193. pdst^:=psrc^;
  194. inc(pdst);
  195. inc(psrc);
  196. end;
  197. // Clear .bss
  198. pend:=@_bss_end;
  199. while pdst<pend do
  200. begin
  201. pdst^:=0;
  202. inc(pdst);
  203. end;
  204. PascalMain;
  205. Haltproc;
  206. end;
  207. procedure LowLevelStartup; assembler; nostackframe;
  208. asm
  209. j .Lstart
  210. .align 4
  211. .Lstack_ptr:
  212. .long _stack_top
  213. .align 4
  214. .Lstart:
  215. l32r a1, .Lstack_ptr
  216. j Startup
  217. end;
  218. end.