Microprocessor and peripheral 2 assignments for AUTH
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

207 lines
6.8 KiB

  1. /*!
  2. * \file onewire_uart.h
  3. * \brief
  4. * A target independent 1-wire implementation using a microprocessor's uart
  5. * for bit timing
  6. * \note
  7. * This 1-wire implementation is based on MCU UART io functionality. In order
  8. * to work it needs:
  9. * 1) A Open drain tx and floating(or pull-up) rx UART pin configuration with both pins
  10. * connected to the 1-wire bus wire
  11. * 2) A Transmit function even in blocking/polling mode
  12. * 3) A receive function even in blocking/polling mode
  13. * 4) A baudrate set function
  14. *
  15. * Copyright (C) 2016 Choutouridis Christos <cchoutou@ece.auth.gr>
  16. *
  17. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Lesser General Public License as
  19. * published by the Free Software Foundation, either version 3
  20. * of the License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Lesser General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Lesser General Public License
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. *
  30. */
  31. #ifndef __onewire_uart_h__
  32. #define __onewire_uart_h__
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. #include "driver_types.h"
  37. #include <string.h>
  38. /* ================ General Defines ====================*/
  39. typedef uint16_t (*ow_uart_rw_ft) (uint8_t); /*!< UART read-write function pointer */
  40. typedef drv_status_en (*ow_uart_br_ft) (uint32_t); /*!< UART baudrate modify function pointer */
  41. /*!
  42. * 1-Wire operational state
  43. */
  44. typedef enum {
  45. OWS_RESET = 0, /*!< 1-Wire bus is during in Reset state */
  46. OWS_OPER /*!< 1-Wire bus is during normal operation */
  47. }ow_uart_state_en;
  48. /*!
  49. * 1-Wire UART baudrate table
  50. */
  51. typedef struct {
  52. uint32_t reset; /*!< Baudrate during reset */
  53. uint32_t oper; /*!< Baudrate during normal operation */
  54. uint32_t current; /*!< The current baudrate to use as flag */
  55. }ow_uart_br_t;
  56. /*!
  57. * 1-Wire driver function callback pointers
  58. * \note
  59. * The function in this structure provided from the low level driver (usually).
  60. */
  61. typedef struct {
  62. ow_uart_rw_ft rw; /*!< Pointer UART read-write function */
  63. ow_uart_br_ft br; /*!< Pointer to UART baudrate function */
  64. }ow_uart_io_t;
  65. /*!
  66. * 1-Wire Data type
  67. */
  68. typedef struct {
  69. ow_uart_io_t io; /*!< Callback pointers and direction state */
  70. uint32_t timing; /*!< The selected timing mode */
  71. ow_uart_br_t baudrate; /*!< The current baudrate configuration */
  72. drv_status_en status; /*!< Toolbox driver status */
  73. }ow_uart_t;
  74. /*
  75. * ============ Helper Macros =============
  76. */
  77. /*!
  78. * 1-Wire speed
  79. */
  80. #define OW_UART_T_STANDARD (0)
  81. #define OW_UART_T_OVERDRIVE (1)
  82. /*!
  83. * Check if the timing is a valid \ref ow_uart_timing_en value
  84. */
  85. #define _ow_uart_is_timings(_t_) ( ((_t_) == OW_UART_T_STANDARD) || ((_t_) == OW_UART_T_OVERDRIVE) )
  86. /*
  87. * Timing Diagram
  88. * --------------------------------------------------
  89. * Based on Application Note 214 (www.maxim-ic.com)
  90. *
  91. * --- ---- - - - -------
  92. * Reset \ / \ X X X /
  93. * -------------------- - - - -
  94. * RS: | | | | | | | | | | |
  95. * bit: st 0 1 2 3 4 5 6 7 st
  96. * < ---------- 1024/174 usec ------------->
  97. *
  98. * 8 bits, no parity, 1 stop
  99. * Standard: Overdrive :
  100. * BR: 9600, BR: 57600
  101. * TX: 0xF0, TX: 0xF8
  102. * RX: 0xF0 not present RX: 0xF8 not present
  103. * less if present less if present
  104. *
  105. *
  106. * --- --------------------------------------
  107. * Write 1 \ /
  108. * ----
  109. * RS: | | | | | | | | | | |
  110. * bit: st 0 1 2 3 4 5 6 7 st
  111. * < ------------- 87/11 usec ------------->
  112. * 8 bits, no parity, 1 stop
  113. * standard: BR: 115200 Overdrive: BR: 921600
  114. * TX: 0xFF
  115. * RX: 0xFF
  116. *
  117. * --- ------
  118. * Write 0 \ /
  119. * -------------------------------------
  120. * RS: | | | | | | | | | | |
  121. * bit: st 0 1 2 3 4 5 6 7 st
  122. * < ------------- 87/11 usec ------------->
  123. * 8 bits, no parity, 1 stop
  124. * standard: BR: 115200 Overdrive: BR: 921600
  125. * TX: 0x00
  126. * RX: 0x00
  127. *
  128. * --- - - - - - - - - - - - ------
  129. * Read \ / X X X X X X X X X X X X X X X /
  130. * ---- - - - - - - - - - - -
  131. * RS: | | | | | | | | | | |
  132. * bit: st 0 1 2 3 4 5 6 7 st
  133. * < ------------- 87/11 usec ------------->
  134. * ^
  135. * |
  136. * Master sample
  137. *
  138. * 8 bits, no parity, 1 stop
  139. * standard: BR: 115200 Overdrive: BR: 921600
  140. * TX: 0xFF
  141. * RX: {1 - 0xFF, 0 - [0x00 - 0xFE] }
  142. *
  143. */
  144. #define _ow_baudrate_standard(_br_) \
  145. do { \
  146. _br_.reset = 9600; \
  147. _br_.oper = 115200; \
  148. _br_.current = 9600; \
  149. } while (0)
  150. #define _ow_baudrate_overdrive(_br_) \
  151. do { \
  152. _br_.reset = 115200; \
  153. _br_.oper = 921600; \
  154. _br_.current = 115200; \
  155. } while (0)
  156. /*
  157. * ============= PUBLIC ALCD API =============
  158. */
  159. /*
  160. * Link and Glue functions
  161. */
  162. void ow_uart_link_rw (ow_uart_t *ow, ow_uart_rw_ft tx); /*!< link driver's read-write function */
  163. void ow_uart_link_br (ow_uart_t *ow, ow_uart_br_ft br); /*!< link driver's baudrate function*/
  164. /*
  165. * Set functions
  166. */
  167. void ow_uart_set_timing (ow_uart_t *ow, uint32_t owt); /*!< set 1-wire timing mode */
  168. /*
  169. * User Functions
  170. */
  171. void ow_uart_deinit (ow_uart_t *ow); /*!< for compatibility */
  172. drv_status_en ow_uart_init (ow_uart_t *ow); /*!< for compatibility */
  173. drv_status_en
  174. ow_uart_reset (ow_uart_t *ow);
  175. uint8_t ow_uart_rx (ow_uart_t *ow);
  176. void ow_uart_tx (ow_uart_t *ow, byte_t byte);
  177. uint8_t ow_uart_rw (ow_uart_t *ow, byte_t byte);
  178. drv_status_en
  179. ow_uart_search (ow_uart_t *ow, uint8_t *romid);
  180. #ifdef __cplusplus
  181. }
  182. #endif
  183. #endif /* #ifndef __onewire_uart_h__ */