platform.h 12 KB

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