esp8266.pp 7.5 KB

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