platform.h 16 KB

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