platform.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // Platform-specific functions
  2. #ifndef __PLATFORM_H__
  3. #define __PLATFORM_H__
  4. #include "cpu_esp8266.h"
  5. #include "c_types.h"
  6. #include "driver/pwm.h"
  7. #include "driver/uart.h"
  8. #include "task/task.h"
  9. // Error / status codes
  10. enum
  11. {
  12. PLATFORM_ERR,
  13. PLATFORM_OK,
  14. PLATFORM_UNDERFLOW = -1
  15. };
  16. // Platform initialization
  17. int platform_init(void);
  18. void platform_int_init(void);
  19. // ****************************************************************************
  20. // KEY_LED functions
  21. uint8_t platform_key_led( uint8_t level);
  22. // *****************************************************************************
  23. // GPIO subsection
  24. #define PLATFORM_GPIO_FLOAT 0
  25. #define PLATFORM_GPIO_PULLUP 1
  26. #define PLATFORM_GPIO_INT 2
  27. #define PLATFORM_GPIO_OUTPUT 1
  28. #define PLATFORM_GPIO_OPENDRAIN 3
  29. #define PLATFORM_GPIO_INPUT 0
  30. #define PLATFORM_GPIO_HIGH 1
  31. #define PLATFORM_GPIO_LOW 0
  32. typedef uint32_t (* platform_hook_function)(uint32_t bitmask);
  33. static inline int platform_gpio_exists( unsigned pin ) { return pin < NUM_GPIO; }
  34. int platform_gpio_mode( unsigned pin, unsigned mode, unsigned pull );
  35. int platform_gpio_write( unsigned pin, unsigned level );
  36. int platform_gpio_read( unsigned pin );
  37. // Note that these functions will not be compiled in unless GPIO_INTERRUPT_ENABLE and
  38. // GPIO_INTERRUPT_HOOK_ENABLE are defined.
  39. int platform_gpio_register_intr_hook(uint32_t gpio_bits, platform_hook_function hook);
  40. #define platform_gpio_unregister_intr_hook(hook) \
  41. platform_gpio_register_intr_hook(0, hook);
  42. void platform_gpio_intr_init( unsigned pin, GPIO_INT_TYPE type );
  43. void platform_gpio_init( task_handle_t gpio_task );
  44. // *****************************************************************************
  45. // Timer subsection
  46. // Timer data type
  47. typedef uint32_t timer_data_type;
  48. // *****************************************************************************
  49. // CAN subsection
  50. // Maximum length for any CAN message
  51. #define PLATFORM_CAN_MAXLEN 8
  52. // eLua CAN ID types
  53. enum
  54. {
  55. ELUA_CAN_ID_STD = 0,
  56. ELUA_CAN_ID_EXT
  57. };
  58. static inline int platform_can_exists( unsigned id ) { return NUM_CAN && (id < NUM_CAN); }
  59. uint32_t platform_can_setup( unsigned id, uint32_t clock );
  60. int platform_can_send( unsigned id, uint32_t canid, uint8_t idtype, uint8_t len, const uint8_t *data );
  61. int platform_can_recv( unsigned id, uint32_t *canid, uint8_t *idtype, uint8_t *len, uint8_t *data );
  62. // *****************************************************************************
  63. // SPI subsection
  64. // There are 4 "virtual" SPI ports (SPI0...SPI3).
  65. #define PLATFORM_SPI_TOTAL 4
  66. // TODO: PLATFORM_SPI_TOTAL is not used - figure out purpose, or remove?
  67. // SPI mode
  68. #define PLATFORM_SPI_MASTER 1
  69. #define PLATFORM_SPI_SLAVE 0
  70. // SS values
  71. #define PLATFORM_SPI_SELECT_ON 1
  72. #define PLATFORM_SPI_SELECT_OFF 0
  73. // SPI enable/disable
  74. #define PLATFORM_SPI_ENABLE 1
  75. #define PLATFORM_SPI_DISABLE 0
  76. // SPI clock phase
  77. #define PLATFORM_SPI_CPHA_LOW 0
  78. #define PLATFORM_SPI_CPHA_HIGH 1
  79. // SPI clock polarity
  80. #define PLATFORM_SPI_CPOL_LOW 0
  81. #define PLATFORM_SPI_CPOL_HIGH 1
  82. // Data types
  83. typedef uint32_t spi_data_type;
  84. // The platform SPI functions
  85. static inline int platform_spi_exists( unsigned id ) { return id < NUM_SPI; }
  86. uint32_t platform_spi_setup( uint8_t id, int mode, unsigned cpol, unsigned cpha, uint32_t clock_div);
  87. int platform_spi_send( uint8_t id, uint8_t bitlen, spi_data_type data );
  88. spi_data_type platform_spi_send_recv( uint8_t id, uint8_t bitlen, spi_data_type data );
  89. void platform_spi_select( unsigned id, int is_select );
  90. int platform_spi_blkwrite( uint8_t id, size_t len, const uint8_t *data );
  91. int platform_spi_blkread( uint8_t id, size_t len, uint8_t *data );
  92. int platform_spi_transaction( uint8_t id, uint8_t cmd_bitlen, spi_data_type cmd_data,
  93. uint8_t addr_bitlen, spi_data_type addr_data,
  94. uint16_t mosi_bitlen, uint8_t dummy_bitlen, int16_t miso_bitlen );
  95. // *****************************************************************************
  96. // UART subsection
  97. // There are 3 "virtual" UART ports (UART0...UART2).
  98. #define PLATFORM_UART_TOTAL 3
  99. // TODO: PLATFORM_UART_TOTAL is not used - figure out purpose, or remove?
  100. // Note: Some CPUs (e.g. LM4F/TM4C) have more than 3 hardware UARTs
  101. // Parity
  102. enum
  103. {
  104. PLATFORM_UART_PARITY_NONE = 0,
  105. PLATFORM_UART_PARITY_EVEN = 1,
  106. PLATFORM_UART_PARITY_ODD = 2,
  107. PLATFORM_UART_PARITY_MARK = 3,
  108. PLATFORM_UART_PARITY_SPACE = 4
  109. };
  110. // Stop bits
  111. enum
  112. {
  113. PLATFORM_UART_STOPBITS_1 = 1,
  114. PLATFORM_UART_STOPBITS_2 = 2,
  115. PLATFORM_UART_STOPBITS_1_5 = 3
  116. };
  117. // Flow control types (this is a bit mask, one can specify PLATFORM_UART_FLOW_RTS | PLATFORM_UART_FLOW_CTS )
  118. #define PLATFORM_UART_FLOW_NONE 0
  119. #define PLATFORM_UART_FLOW_RTS 1
  120. #define PLATFORM_UART_FLOW_CTS 2
  121. // The platform UART functions
  122. static inline int platform_uart_exists( unsigned id ) { return id < NUM_UART; }
  123. uint32_t platform_uart_setup( unsigned id, uint32_t baud, int databits, int parity, int stopbits );
  124. int platform_uart_set_buffer( unsigned id, unsigned size );
  125. void platform_uart_send( unsigned id, uint8_t data );
  126. void platform_s_uart_send( unsigned id, uint8_t data );
  127. int platform_uart_recv( unsigned id, unsigned timer_id, timer_data_type timeout );
  128. int platform_s_uart_recv( unsigned id, timer_data_type timeout );
  129. int platform_uart_set_flow_control( unsigned id, int type );
  130. int platform_s_uart_set_flow_control( unsigned id, int type );
  131. void platform_uart_alt( int set );
  132. void platform_uart_get_config(unsigned id, uint32_t *baudp, uint32_t *databitsp, uint32_t *parityp, uint32_t *stopbitsp);
  133. // *****************************************************************************
  134. // PWM subsection
  135. // There are 16 "virtual" PWM channels (PWM0...PWM15)
  136. #define PLATFORM_PWM_TOTAL 16
  137. // TODO: PLATFORM_PWM_TOTAL is not used - figure out purpose, or remove?
  138. #define NORMAL_PWM_DEPTH PWM_DEPTH
  139. #define NORMAL_DUTY(d) (((unsigned)(d)*NORMAL_PWM_DEPTH) / PWM_DEPTH)
  140. #define DUTY(d) ((uint16_t)( ((unsigned)(d)*PWM_DEPTH) / NORMAL_PWM_DEPTH) )
  141. // The platform PWM functions
  142. void platform_pwm_init( void );
  143. static inline int platform_pwm_exists( unsigned id ) { return ((id < NUM_PWM) && (id > 0)); }
  144. uint32_t platform_pwm_setup( unsigned id, uint32_t frequency, unsigned duty );
  145. void platform_pwm_close( unsigned id );
  146. bool platform_pwm_start( unsigned id );
  147. void platform_pwm_stop( unsigned id );
  148. uint32_t platform_pwm_set_clock( unsigned id, uint32_t data );
  149. uint32_t platform_pwm_get_clock( unsigned id );
  150. uint32_t platform_pwm_set_duty( unsigned id, uint32_t data );
  151. uint32_t platform_pwm_get_duty( unsigned id );
  152. // *****************************************************************************
  153. // The platform ADC functions
  154. // Functions requiring platform-specific implementation
  155. int platform_adc_update_sequence(void);
  156. int platform_adc_start_sequence(void);
  157. void platform_adc_stop( unsigned id );
  158. uint32_t platform_adc_set_clock( unsigned id, uint32_t frequency);
  159. int platform_adc_check_timer_id( unsigned id, unsigned timer_id );
  160. // ADC Common Functions
  161. static inline int platform_adc_exists( unsigned id ) { return id < NUM_ADC; }
  162. uint32_t platform_adc_get_maxval( unsigned id );
  163. uint32_t platform_adc_set_smoothing( unsigned id, uint32_t length );
  164. void platform_adc_set_blocking( unsigned id, uint32_t mode );
  165. void platform_adc_set_freerunning( unsigned id, uint32_t mode );
  166. uint32_t platform_adc_is_done( unsigned id );
  167. void platform_adc_set_timer( unsigned id, uint32_t timer );
  168. // ****************************************************************************
  169. // OneWire functions
  170. static inline int platform_ow_exists( unsigned id ) { return ((id < NUM_OW) && (id > 0)); }
  171. // ****************************************************************************
  172. // Timer functions
  173. static inline int platform_tmr_exists( unsigned id ) { return id < NUM_TMR; }
  174. // *****************************************************************************
  175. // Sigma-Delta platform interface
  176. // ****************************************************************************
  177. // Sigma-Delta functions
  178. static inline int platform_sigma_delta_exists( unsigned id ) {return ((id < NUM_GPIO) && (id > 0)); }
  179. uint8_t platform_sigma_delta_setup( uint8_t pin );
  180. uint8_t platform_sigma_delta_close( uint8_t pin );
  181. void platform_sigma_delta_set_pwmduty( uint8_t duty );
  182. void platform_sigma_delta_set_prescale( uint8_t prescale );
  183. void platform_sigma_delta_set_target( uint8_t target );
  184. // *****************************************************************************
  185. // I2C platform interface
  186. // I2C speed
  187. enum
  188. {
  189. PLATFORM_I2C_SPEED_SLOW = 100000,
  190. PLATFORM_I2C_SPEED_FAST = 400000
  191. };
  192. // I2C direction
  193. enum
  194. {
  195. PLATFORM_I2C_DIRECTION_TRANSMITTER,
  196. PLATFORM_I2C_DIRECTION_RECEIVER
  197. };
  198. #ifdef NUM_I2C
  199. static inline int platform_i2c_exists( unsigned id ) { return id < NUM_I2C; }
  200. #else
  201. static inline int platform_i2c_exists( unsigned id ) { return 0; }
  202. #endif
  203. uint32_t platform_i2c_setup( unsigned id, uint8_t sda, uint8_t scl, uint32_t speed );
  204. void platform_i2c_send_start( unsigned id );
  205. void platform_i2c_send_stop( unsigned id );
  206. int platform_i2c_send_address( unsigned id, uint16_t address, int direction );
  207. int platform_i2c_send_byte( unsigned id, uint8_t data );
  208. int platform_i2c_recv_byte( unsigned id, int ack );
  209. // *****************************************************************************
  210. // Ethernet specific functions
  211. void platform_eth_send_packet( const void* src, uint32_t size );
  212. uint32_t platform_eth_get_packet_nb( void* buf, uint32_t maxlen );
  213. void platform_eth_force_interrupt(void);
  214. uint32_t platform_eth_get_elapsed_time(void);
  215. // *****************************************************************************
  216. // Internal flash erase/write functions
  217. uint32_t platform_flash_get_first_free_block_address( uint32_t *psect );
  218. uint32_t platform_flash_get_sector_of_address( uint32_t addr );
  219. uint32_t platform_flash_write( const void *from, uint32_t toaddr, uint32_t size );
  220. uint32_t platform_flash_read( void *to, uint32_t fromaddr, uint32_t size );
  221. uint32_t platform_s_flash_write( const void *from, uint32_t toaddr, uint32_t size );
  222. uint32_t platform_s_flash_read( void *to, uint32_t fromaddr, uint32_t size );
  223. uint32_t platform_flash_get_num_sectors(void);
  224. int platform_flash_erase_sector( uint32_t sector_id );
  225. /**
  226. * Translated a mapped address to a physical flash address, based on the
  227. * current flash cache mapping.
  228. * @param mapped_addr Address to translate (>= INTERNAL_FLASH_MAPPED_ADDRESS)
  229. * @return the corresponding physical flash address, or -1 if flash cache is
  230. * not currently active.
  231. * @see Cache_Read_Enable.
  232. */
  233. uint32_t platform_flash_mapped2phys (uint32_t mapped_addr);
  234. // *****************************************************************************
  235. // Allocator support
  236. void* platform_get_first_free_ram( unsigned id );
  237. void* platform_get_last_free_ram( unsigned id );
  238. // *****************************************************************************
  239. // Other glue
  240. int platform_ow_exists( unsigned id );
  241. int platform_gpio_exists( unsigned id );
  242. int platform_tmr_exists( unsigned id );
  243. // *****************************************************************************
  244. void* platform_print_deprecation_note( const char *msg, const char *time_frame);
  245. // Helper macros
  246. #define MOD_CHECK_ID( mod, id )\
  247. if( !platform_ ## mod ## _exists( id ) )\
  248. return luaL_error( L, #mod" %d does not exist", ( unsigned )id )
  249. #define MOD_CHECK_TIMER( id )\
  250. if( id == PLATFORM_TIMER_SYS_ID && !platform_timer_sys_available() )\
  251. return luaL_error( L, "the system timer is not available on this platform" );\
  252. if( !platform_tmr_exists( id ) )\
  253. return luaL_error( L, "timer %d does not exist", ( unsigned )id )\
  254. #define MOD_CHECK_RES_ID( mod, id, resmod, resid )\
  255. if( !platform_ ## mod ## _check_ ## resmod ## _id( id, resid ) )\
  256. return luaL_error( L, #resmod" %d not valid with " #mod " %d", ( unsigned )resid, ( unsigned )id )
  257. #endif