platform.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // Platform-specific functions
  2. #ifndef __PLATFORM_H__
  3. #define __PLATFORM_H__
  4. #include <stdint.h>
  5. #include "cpu_esp8266.h"
  6. #include "driver/pwm.h"
  7. #include "driver/uart.h"
  8. // Error / status codes
  9. enum
  10. {
  11. PLATFORM_ERR,
  12. PLATFORM_OK,
  13. PLATFORM_UNDERFLOW = -1
  14. };
  15. typedef uint32_t platform_task_handle_t;
  16. typedef uint32_t platform_task_param_t;
  17. // Platform initialization
  18. int platform_init(void);
  19. void platform_int_init(void);
  20. // ****************************************************************************
  21. // KEY_LED functions
  22. uint8_t platform_key_led( uint8_t level);
  23. // *****************************************************************************
  24. // GPIO subsection
  25. #define PLATFORM_GPIO_FLOAT 0
  26. #define PLATFORM_GPIO_PULLUP 1
  27. #define PLATFORM_GPIO_INT 2
  28. #define PLATFORM_GPIO_OUTPUT 1
  29. #define PLATFORM_GPIO_OPENDRAIN 3
  30. #define PLATFORM_GPIO_INPUT 0
  31. #define PLATFORM_GPIO_HIGH 1
  32. #define PLATFORM_GPIO_LOW 0
  33. typedef uint32_t (* platform_hook_function)(uint32_t bitmask);
  34. static inline int platform_gpio_exists( unsigned pin ) { return pin < NUM_GPIO; }
  35. int platform_gpio_mode( unsigned pin, unsigned mode, unsigned pull );
  36. int platform_gpio_write( unsigned pin, unsigned level );
  37. int platform_gpio_read( unsigned pin );
  38. // Note that these functions will not be compiled in unless GPIO_INTERRUPT_ENABLE and
  39. // GPIO_INTERRUPT_HOOK_ENABLE are defined.
  40. int platform_gpio_register_intr_hook(uint32_t gpio_bits, platform_hook_function hook);
  41. #define platform_gpio_unregister_intr_hook(hook) \
  42. platform_gpio_register_intr_hook(0, hook);
  43. void platform_gpio_intr_init( unsigned pin, GPIO_INT_TYPE type );
  44. void platform_gpio_init( platform_task_handle_t gpio_task );
  45. // *****************************************************************************
  46. // Timer subsection
  47. // Timer data type
  48. typedef uint32_t timer_data_type;
  49. // *****************************************************************************
  50. // CAN subsection
  51. // Maximum length for any CAN message
  52. #define PLATFORM_CAN_MAXLEN 8
  53. // eLua CAN ID types
  54. enum
  55. {
  56. ELUA_CAN_ID_STD = 0,
  57. ELUA_CAN_ID_EXT
  58. };
  59. static inline int platform_can_exists( unsigned id ) { return NUM_CAN && (id < NUM_CAN); }
  60. uint32_t platform_can_setup( unsigned id, uint32_t clock );
  61. int platform_can_send( unsigned id, uint32_t canid, uint8_t idtype, uint8_t len, const uint8_t *data );
  62. int platform_can_recv( unsigned id, uint32_t *canid, uint8_t *idtype, uint8_t *len, uint8_t *data );
  63. // *****************************************************************************
  64. // SPI subsection
  65. // There are 4 "virtual" SPI ports (SPI0...SPI3).
  66. #define PLATFORM_SPI_TOTAL 4
  67. // TODO: PLATFORM_SPI_TOTAL is not used - figure out purpose, or remove?
  68. // SPI mode
  69. #define PLATFORM_SPI_MASTER 1
  70. #define PLATFORM_SPI_SLAVE 0
  71. // SS values
  72. #define PLATFORM_SPI_SELECT_ON 1
  73. #define PLATFORM_SPI_SELECT_OFF 0
  74. // SPI enable/disable
  75. #define PLATFORM_SPI_ENABLE 1
  76. #define PLATFORM_SPI_DISABLE 0
  77. // SPI clock phase
  78. #define PLATFORM_SPI_CPHA_LOW 0
  79. #define PLATFORM_SPI_CPHA_HIGH 1
  80. // SPI clock polarity
  81. #define PLATFORM_SPI_CPOL_LOW 0
  82. #define PLATFORM_SPI_CPOL_HIGH 1
  83. // Data types
  84. typedef uint32_t spi_data_type;
  85. // The platform SPI functions
  86. static inline int platform_spi_exists( unsigned id ) { return id < NUM_SPI; }
  87. uint32_t platform_spi_setup( uint8_t id, int mode, unsigned cpol, unsigned cpha, uint32_t clock_div);
  88. int platform_spi_send( uint8_t id, uint8_t bitlen, spi_data_type data );
  89. spi_data_type platform_spi_send_recv( uint8_t id, uint8_t bitlen, spi_data_type data );
  90. void platform_spi_select( unsigned id, int is_select );
  91. int platform_spi_blkwrite( uint8_t id, size_t len, const uint8_t *data );
  92. int platform_spi_blkread( uint8_t id, size_t len, uint8_t *data );
  93. int platform_spi_transaction( uint8_t id, uint8_t cmd_bitlen, spi_data_type cmd_data,
  94. uint8_t addr_bitlen, spi_data_type addr_data,
  95. uint16_t mosi_bitlen, uint8_t dummy_bitlen, int16_t miso_bitlen );
  96. // *****************************************************************************
  97. // UART subsection
  98. // There are 3 "virtual" UART ports (UART0...UART2).
  99. #define PLATFORM_UART_TOTAL 3
  100. // TODO: PLATFORM_UART_TOTAL is not used - figure out purpose, or remove?
  101. // Note: Some CPUs (e.g. LM4F/TM4C) have more than 3 hardware UARTs
  102. // Parity
  103. enum
  104. {
  105. PLATFORM_UART_PARITY_NONE = 0,
  106. PLATFORM_UART_PARITY_EVEN = 1,
  107. PLATFORM_UART_PARITY_ODD = 2,
  108. PLATFORM_UART_PARITY_MARK = 3,
  109. PLATFORM_UART_PARITY_SPACE = 4
  110. };
  111. // Stop bits
  112. enum
  113. {
  114. PLATFORM_UART_STOPBITS_1 = 1,
  115. PLATFORM_UART_STOPBITS_2 = 2,
  116. PLATFORM_UART_STOPBITS_1_5 = 3
  117. };
  118. // Flow control types (this is a bit mask, one can specify PLATFORM_UART_FLOW_RTS | PLATFORM_UART_FLOW_CTS )
  119. #define PLATFORM_UART_FLOW_NONE 0
  120. #define PLATFORM_UART_FLOW_RTS 1
  121. #define PLATFORM_UART_FLOW_CTS 2
  122. // The platform UART functions
  123. static inline int platform_uart_exists( unsigned id ) { return id < NUM_UART; }
  124. uint32_t platform_uart_setup( unsigned id, uint32_t baud, int databits, int parity, int stopbits );
  125. int platform_uart_set_buffer( unsigned id, unsigned size );
  126. void platform_uart_send( unsigned id, uint8_t data );
  127. void platform_s_uart_send( unsigned id, uint8_t data );
  128. int platform_uart_recv( unsigned id, unsigned timer_id, timer_data_type timeout );
  129. int platform_s_uart_recv( unsigned id, timer_data_type timeout );
  130. int platform_uart_set_flow_control( unsigned id, int type );
  131. int platform_s_uart_set_flow_control( unsigned id, int type );
  132. void platform_uart_alt( int set );
  133. void platform_uart_get_config(unsigned id, uint32_t *baudp, uint32_t *databitsp, uint32_t *parityp, uint32_t *stopbitsp);
  134. // *****************************************************************************
  135. // PWM subsection
  136. // There are 16 "virtual" PWM channels (PWM0...PWM15)
  137. #define PLATFORM_PWM_TOTAL 16
  138. // TODO: PLATFORM_PWM_TOTAL is not used - figure out purpose, or remove?
  139. #define NORMAL_PWM_DEPTH PWM_DEPTH
  140. #define NORMAL_DUTY(d) (((unsigned)(d)*NORMAL_PWM_DEPTH) / PWM_DEPTH)
  141. #define DUTY(d) ((uint16_t)( ((unsigned)(d)*PWM_DEPTH) / NORMAL_PWM_DEPTH) )
  142. // The platform PWM functions
  143. void platform_pwm_init( void );
  144. static inline int platform_pwm_exists( unsigned id ) { return ((id < NUM_PWM) && (id > 0)); }
  145. uint32_t platform_pwm_setup( unsigned id, uint32_t frequency, unsigned duty );
  146. void platform_pwm_close( unsigned id );
  147. bool platform_pwm_start( unsigned id );
  148. void platform_pwm_stop( unsigned id );
  149. uint32_t platform_pwm_set_clock( unsigned id, uint32_t data );
  150. uint32_t platform_pwm_get_clock( unsigned id );
  151. uint32_t platform_pwm_set_duty( unsigned id, uint32_t data );
  152. uint32_t platform_pwm_get_duty( unsigned id );
  153. // *****************************************************************************
  154. // The platform ADC functions
  155. // Functions requiring platform-specific implementation
  156. int platform_adc_update_sequence(void);
  157. int platform_adc_start_sequence(void);
  158. void platform_adc_stop( unsigned id );
  159. uint32_t platform_adc_set_clock( unsigned id, uint32_t frequency);
  160. int platform_adc_check_timer_id( unsigned id, unsigned timer_id );
  161. // ADC Common Functions
  162. static inline int platform_adc_exists( unsigned id ) { return id < NUM_ADC; }
  163. uint32_t platform_adc_get_maxval( unsigned id );
  164. uint32_t platform_adc_set_smoothing( unsigned id, uint32_t length );
  165. void platform_adc_set_blocking( unsigned id, uint32_t mode );
  166. void platform_adc_set_freerunning( unsigned id, uint32_t mode );
  167. uint32_t platform_adc_is_done( unsigned id );
  168. void platform_adc_set_timer( unsigned id, uint32_t timer );
  169. // ****************************************************************************
  170. // OneWire functions
  171. static inline int platform_ow_exists( unsigned id ) { return ((id < NUM_OW) && (id > 0)); }
  172. // ****************************************************************************
  173. // Timer functions
  174. static inline int platform_tmr_exists( unsigned id ) { return id < NUM_TMR; }
  175. // *****************************************************************************
  176. // Sigma-Delta platform interface
  177. // ****************************************************************************
  178. // Sigma-Delta functions
  179. static inline int platform_sigma_delta_exists( unsigned id ) {return ((id < NUM_GPIO) && (id > 0)); }
  180. uint8_t platform_sigma_delta_setup( uint8_t pin );
  181. uint8_t platform_sigma_delta_close( uint8_t pin );
  182. void platform_sigma_delta_set_pwmduty( uint8_t duty );
  183. void platform_sigma_delta_set_prescale( uint8_t prescale );
  184. void platform_sigma_delta_set_target( uint8_t target );
  185. // *****************************************************************************
  186. // I2C platform interface
  187. // I2C speed
  188. enum
  189. {
  190. PLATFORM_I2C_SPEED_SLOW = 100000,
  191. PLATFORM_I2C_SPEED_FAST = 400000,
  192. PLATFORM_I2C_SPEED_FASTPLUS = 1000000
  193. };
  194. // I2C direction
  195. enum
  196. {
  197. PLATFORM_I2C_DIRECTION_TRANSMITTER,
  198. PLATFORM_I2C_DIRECTION_RECEIVER
  199. };
  200. #ifdef NUM_I2C
  201. static inline int platform_i2c_exists( unsigned id ) { return id < NUM_I2C; }
  202. #else
  203. static inline int platform_i2c_exists( unsigned id ) { return 0; }
  204. #endif
  205. uint32_t platform_i2c_setup( unsigned id, uint8_t sda, uint8_t scl, uint32_t speed );
  206. bool platform_i2c_configured( unsigned id );
  207. void platform_i2c_send_start( unsigned id );
  208. void platform_i2c_send_stop( unsigned id );
  209. int platform_i2c_send_address( unsigned id, uint16_t address, int direction );
  210. int platform_i2c_send_byte( unsigned id, uint8_t data );
  211. int platform_i2c_recv_byte( unsigned id, int ack );
  212. // *****************************************************************************
  213. // Ethernet specific functions
  214. void platform_eth_send_packet( const void* src, uint32_t size );
  215. uint32_t platform_eth_get_packet_nb( void* buf, uint32_t maxlen );
  216. void platform_eth_force_interrupt(void);
  217. uint32_t platform_eth_get_elapsed_time(void);
  218. // *****************************************************************************
  219. // Internal flash erase/write functions
  220. uint32_t platform_flash_get_first_free_block_address( uint32_t *psect );
  221. uint32_t platform_flash_get_sector_of_address( uint32_t addr );
  222. uint32_t platform_flash_write( const void *from, uint32_t toaddr, uint32_t size );
  223. uint32_t platform_flash_read( void *to, uint32_t fromaddr, uint32_t size );
  224. uint32_t platform_s_flash_write( const void *from, uint32_t toaddr, uint32_t size );
  225. uint32_t platform_s_flash_read( void *to, uint32_t fromaddr, uint32_t size );
  226. uint32_t platform_flash_get_num_sectors(void);
  227. int platform_flash_erase_sector( uint32_t sector_id );
  228. /**
  229. * Translated a mapped address to a physical flash address, based on the
  230. * current flash cache mapping, and v.v.
  231. * @param mapped_addr Address to translate (>= INTERNAL_FLASH_MAPPED_ADDRESS)
  232. * @return the corresponding physical flash address, or -1 if flash cache is
  233. * not currently active.
  234. * @see Cache_Read_Enable.
  235. */
  236. uint32_t platform_flash_mapped2phys (uint32_t mapped_addr);
  237. uint32_t platform_flash_phys2mapped (uint32_t phys_addr);
  238. uint32_t platform_flash_get_partition (uint32_t part_id, uint32_t *addr);
  239. // *****************************************************************************
  240. // Other glue
  241. int platform_ow_exists( unsigned id );
  242. int platform_gpio_exists( unsigned id );
  243. int platform_tmr_exists( unsigned id );
  244. // *****************************************************************************
  245. void* platform_print_deprecation_note( const char *msg, const char *time_frame);
  246. // Helper macros
  247. #define MOD_CHECK_ID( mod, id )\
  248. if( !platform_ ## mod ## _exists( id ) )\
  249. return luaL_error( L, #mod" %d does not exist", ( unsigned )id )
  250. #define MOD_CHECK_TIMER( id )\
  251. if( id == PLATFORM_TIMER_SYS_ID && !platform_timer_sys_available() )\
  252. return luaL_error( L, "the system timer is not available on this platform" );\
  253. if( !platform_tmr_exists( id ) )\
  254. return luaL_error( L, "timer %d does not exist", ( unsigned )id )\
  255. #define MOD_CHECK_RES_ID( mod, id, resmod, resid )\
  256. if( !platform_ ## mod ## _check_ ## resmod ## _id( id, resid ) )\
  257. return luaL_error( L, #resmod" %d not valid with " #mod " %d", ( unsigned )resid, ( unsigned )id )
  258. // *****************************************************************************
  259. // Reboot config page
  260. /*
  261. * The 4K flash page in the linker section .irom0.ptable (offset 0x10000) is used
  262. * for configuration changes that persist across reboots. This 4k page contains a
  263. * sequence of records that are written using flash NAND writing rules. See the
  264. * header app/spiffs/spiffs_nucleus.h for a discussion of how SPIFFS uses these. A
  265. * similar technique is used here.
  266. *
  267. * Each record is word aligned and the first two bytes of the record are its size
  268. * and record type. Type 0xFF means unused and type 0x00 means deleted. New
  269. * records can be added by overwriting the first unused slot. Records can be
  270. * replaced by adding the new version, then setting the type of the previous version
  271. * to deleted. This all works and can be implemented with platform_s_flash_write()
  272. * upto the 4K limit.
  273. *
  274. * If a new record cannot fit into the page then the deleted records are GCed by
  275. * copying the active records into a RAM scratch pad, erasing the page and writing
  276. * them back. Yes, this is powerfail unsafe for a few mSec, but this is no worse
  277. * than writing to SPIFFS and won't even occur in normal production use.
  278. */
  279. #define IROM_PTABLE_ATTR __attribute__((section(".irom0.ptable")))
  280. #define PLATFORM_PARTITION(n) (SYSTEM_PARTITION_CUSTOMER_BEGIN + n)
  281. #define PLATFORM_RCR_DELETED 0x0
  282. #define PLATFORM_RCR_PT 0x1
  283. #define PLATFORM_RCR_PHY_DATA 0x2
  284. #define PLATFORM_RCR_REFLASH 0x3
  285. #define PLATFORM_RCR_FREE 0xFF
  286. typedef union {
  287. uint32_t hdr;
  288. struct { uint8_t len,id; };
  289. } platform_rcr_t;
  290. uint32_t platform_rcr_read (uint8_t rec_id, void **rec);
  291. uint32_t platform_rcr_write (uint8_t rec_id, const void *rec, uint8_t size);
  292. #define PLATFORM_TASK_PRIORITY_LOW 0
  293. #define PLATFORM_TASK_PRIORITY_MEDIUM 1
  294. #define PLATFORM_TASK_PRIORITY_HIGH 2
  295. /*
  296. * Signals are a 32-bit number of the form header:14; count:16, priority:2. The header
  297. * is just a fixed fingerprint and the count is allocated serially by the task get_id()
  298. * function.
  299. */
  300. #define platform_post_low(handle,param) platform_post(PLATFORM_TASK_PRIORITY_LOW, handle, param)
  301. #define platform_post_medium(handle,param) platform_post(PLATFORM_TASK_PRIORITY_MEDIUM, handle, param)
  302. #define platform_post_high(handle,param) platform_post(PLATFORM_TASK_PRIORITY_HIGH, handle, param)
  303. typedef void (*platform_task_callback_t)(platform_task_param_t param, uint8 prio);
  304. platform_task_handle_t platform_task_get_id(platform_task_callback_t t);
  305. bool platform_post(uint8 prio, platform_task_handle_t h, platform_task_param_t par);
  306. #endif