Guest User

twi_master_shorts.c

a guest
May 13th, 2015
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.41 KB | None | 0 0
  1. /* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12.  
  13. #include "twi_master.h"
  14. #include "twi_master_config.h"
  15. #include <stdbool.h>
  16. #include <stdint.h>
  17. #include "nrf.h"
  18. #include "nrf_delay.h"
  19. #include "nrf_gpio.h"
  20. #include "nrf_sdm.h"
  21.  
  22. /* Max cycles approximately to wait on RXDREADY and TXDREADY event,
  23. * This is optimized way instead of using timers, this is not power aware. */
  24. #define MAX_TIMEOUT_LOOPS (20000UL) /**< MAX while loops to wait for RXD/TXD event */
  25.  
  26. static uint8_t sd_enabled;
  27.  
  28. static bool twi_master_write(uint8_t *data, uint8_t data_length, bool issue_stop_condition)
  29. {
  30. uint32_t timeout = MAX_TIMEOUT_LOOPS; /* max loops to wait for EVENTS_TXDSENT event*/
  31.  
  32. if (data_length == 0)
  33. {
  34. /* Return false for requesting data of size 0 */
  35. return false;
  36. }
  37.  
  38. NRF_TWI1->TXD = *data++;
  39. NRF_TWI1->TASKS_STARTTX = 1;
  40.  
  41. /** @snippet [TWI HW master write] */
  42. while (true)
  43. {
  44. while(NRF_TWI1->EVENTS_TXDSENT == 0 && NRF_TWI1->EVENTS_ERROR == 0 && (--timeout))
  45. {
  46. // Do nothing.
  47. }
  48.  
  49. if (timeout == 0 || NRF_TWI1->EVENTS_ERROR != 0)
  50. {
  51. // Recover the peripheral as indicated by PAN 56: "TWI: TWI module lock-up." found at
  52. // Product Anomaly Notification document found at
  53. // https://www.nordicsemi.com/eng/Products/Bluetooth-R-low-energy/nRF51822/#Downloads
  54. NRF_TWI1->EVENTS_ERROR = 0;
  55. NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
  56. NRF_TWI1->POWER = 0;
  57. nrf_delay_us(5);
  58. NRF_TWI1->POWER = 1;
  59. NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
  60.  
  61. (void)twi_master_init();
  62.  
  63. return false;
  64. }
  65. NRF_TWI1->EVENTS_TXDSENT = 0;
  66. if (--data_length == 0)
  67. {
  68. break;
  69. }
  70.  
  71. NRF_TWI1->TXD = *data++;
  72. }
  73. /** @snippet [TWI HW master write] */
  74.  
  75. if (issue_stop_condition)
  76. {
  77. NRF_TWI1->EVENTS_STOPPED = 0;
  78. NRF_TWI1->TASKS_STOP = 1;
  79. /* Wait until stop sequence is sent */
  80. while(NRF_TWI1->EVENTS_STOPPED == 0)
  81. {
  82. // Do nothing.
  83. }
  84. }
  85. return true;
  86. }
  87.  
  88.  
  89. /** @brief Function for read by twi_master.
  90. */
  91. static bool twi_master_read(uint8_t *data, uint8_t data_length, bool issue_stop_condition)
  92. {
  93. uint32_t timeout = MAX_TIMEOUT_LOOPS; /* max loops to wait for RXDREADY event*/
  94.  
  95. sd_softdevice_is_enabled(&sd_enabled);
  96.  
  97. if (data_length == 0)
  98. {
  99. /* Return false for requesting data of size 0 */
  100. return false;
  101. }
  102. else if (data_length == 1)
  103. {
  104. if(sd_enabled)
  105. {
  106. sd_ppi_channel_assign(0, &NRF_TWI1->EVENTS_BB, &NRF_TWI1->TASKS_STOP);
  107. }
  108. else
  109. {
  110. NRF_PPI->CH[0].TEP = (uint32_t)&NRF_TWI1->TASKS_STOP;
  111. }
  112. }
  113. else
  114. {
  115. if(sd_enabled)
  116. {
  117. sd_ppi_channel_assign(0, &NRF_TWI1->EVENTS_BB, &NRF_TWI1->TASKS_SUSPEND);
  118. }
  119. else
  120. {
  121. NRF_PPI->CH[0].TEP = (uint32_t)&NRF_TWI1->TASKS_SUSPEND;
  122. }
  123. }
  124.  
  125. if(sd_enabled)
  126. {
  127. sd_ppi_channel_enable_set(1 << 0);
  128. }
  129. else
  130. {
  131. NRF_PPI->CHENSET = PPI_CHENSET_CH0_Msk;
  132. }
  133. NRF_TWI1->EVENTS_RXDREADY = 0;
  134. NRF_TWI1->TASKS_STARTRX = 1;
  135.  
  136. /** @snippet [TWI HW master read] */
  137. while (true)
  138. {
  139. while(NRF_TWI1->EVENTS_RXDREADY == 0 && NRF_TWI1->EVENTS_ERROR == 0 && (--timeout))
  140. {
  141. // Do nothing.
  142. }
  143. NRF_TWI1->EVENTS_RXDREADY = 0;
  144.  
  145. if (timeout == 0 || NRF_TWI1->EVENTS_ERROR != 0)
  146. {
  147. // Recover the peripheral as indicated by PAN 56: "TWI: TWI module lock-up." found at
  148. // Product Anomaly Notification document found at
  149. // https://www.nordicsemi.com/eng/Products/Bluetooth-R-low-energy/nRF51822/#Downloads
  150. NRF_TWI1->EVENTS_ERROR = 0;
  151. NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
  152. NRF_TWI1->POWER = 0;
  153. nrf_delay_us(5);
  154. NRF_TWI1->POWER = 1;
  155. NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
  156.  
  157. (void)twi_master_init();
  158.  
  159. return false;
  160. }
  161.  
  162. *data++ = NRF_TWI1->RXD;
  163.  
  164. /* Configure PPI to stop TWI master before we get last BB event */
  165. if (--data_length == 1)
  166. {
  167. sd_softdevice_is_enabled(&sd_enabled);
  168. if(sd_enabled)
  169. {
  170. sd_ppi_channel_assign(0, &NRF_TWI1->EVENTS_BB, &NRF_TWI1->TASKS_STOP);
  171. }
  172. else
  173. {
  174. NRF_PPI->CH[0].TEP = (uint32_t)&NRF_TWI1->TASKS_STOP;
  175. }
  176. }
  177.  
  178. if (data_length == 0)
  179. {
  180. break;
  181. }
  182.  
  183. // Recover the peripheral as indicated by PAN 56: "TWI: TWI module lock-up." found at
  184. // Product Anomaly Notification document found at
  185. // https://www.nordicsemi.com/eng/Products/Bluetooth-R-low-energy/nRF51822/#Downloads
  186. nrf_delay_us(20);
  187. NRF_TWI1->TASKS_RESUME = 1;
  188. }
  189. /** @snippet [TWI HW master read] */
  190.  
  191. /* Wait until stop sequence is sent */
  192. while(NRF_TWI1->EVENTS_STOPPED == 0)
  193. {
  194. // Do nothing.
  195. }
  196. NRF_TWI1->EVENTS_STOPPED = 0;
  197.  
  198. sd_softdevice_is_enabled(&sd_enabled);
  199. if(sd_enabled)
  200. {
  201. sd_ppi_channel_enable_clr(1 << 0);
  202. }
  203. else
  204. {
  205. NRF_PPI->CHENCLR = PPI_CHENCLR_CH0_Msk;
  206. }
  207. return true;
  208. }
  209.  
  210.  
  211. /**
  212. * @brief Function for detecting stuck slaves (SDA = 0 and SCL = 1) and tries to clear the bus.
  213. *
  214. * @return
  215. * @retval false Bus is stuck.
  216. * @retval true Bus is clear.
  217. */
  218. static bool twi_master_clear_bus(void)
  219. {
  220. uint32_t twi_state;
  221. bool bus_clear;
  222. uint32_t clk_pin_config;
  223. uint32_t data_pin_config;
  224.  
  225. // Save and disable TWI hardware so software can take control over the pins.
  226. twi_state = NRF_TWI1->ENABLE;
  227. NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
  228.  
  229. clk_pin_config = \
  230. NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER];
  231. NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER] = \
  232. (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) \
  233. | (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) \
  234. | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) \
  235. | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) \
  236. | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
  237.  
  238. data_pin_config = \
  239. NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_DATA_PIN_NUMBER];
  240. NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_DATA_PIN_NUMBER] = \
  241. (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) \
  242. | (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) \
  243. | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) \
  244. | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) \
  245. | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
  246.  
  247. TWI_SDA_HIGH();
  248. TWI_SCL_HIGH();
  249. TWI_DELAY();
  250.  
  251. if ((TWI_SDA_READ() == 1) && (TWI_SCL_READ() == 1))
  252. {
  253. bus_clear = true;
  254. }
  255. else
  256. {
  257. uint_fast8_t i;
  258. bus_clear = false;
  259.  
  260. // Clock max 18 pulses worst case scenario(9 for master to send the rest of command and 9
  261. // for slave to respond) to SCL line and wait for SDA come high.
  262. for (i=18; i--;)
  263. {
  264. TWI_SCL_LOW();
  265. TWI_DELAY();
  266. TWI_SCL_HIGH();
  267. TWI_DELAY();
  268.  
  269. if (TWI_SDA_READ() == 1)
  270. {
  271. bus_clear = true;
  272. break;
  273. }
  274. }
  275. }
  276.  
  277. NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER] = clk_pin_config;
  278. NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_DATA_PIN_NUMBER] = data_pin_config;
  279.  
  280. NRF_TWI1->ENABLE = twi_state;
  281.  
  282. return bus_clear;
  283. }
  284.  
  285.  
  286. /** @brief Function for initializing the twi_master.
  287. */
  288. bool twi_master_init(void)
  289. {
  290. /* To secure correct signal levels on the pins used by the TWI
  291. master when the system is in OFF mode, and when the TWI master is
  292. disabled, these pins must be configured in the GPIO peripheral.
  293. */
  294. NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER] = \
  295. (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) \
  296. | (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) \
  297. | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) \
  298. | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) \
  299. | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
  300.  
  301. NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_DATA_PIN_NUMBER] = \
  302. (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) \
  303. | (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) \
  304. | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) \
  305. | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) \
  306. | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
  307.  
  308. NRF_TWI1->EVENTS_RXDREADY = 0;
  309. NRF_TWI1->EVENTS_TXDSENT = 0;
  310. NRF_TWI1->PSELSCL = TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER;
  311. NRF_TWI1->PSELSDA = TWI_MASTER_CONFIG_DATA_PIN_NUMBER;
  312. NRF_TWI1->FREQUENCY = TWI_FREQUENCY_FREQUENCY_K100 << TWI_FREQUENCY_FREQUENCY_Pos;
  313.  
  314. sd_softdevice_is_enabled(&sd_enabled);
  315. if(sd_enabled)
  316. {
  317. sd_ppi_channel_assign(0, &NRF_TWI1->EVENTS_BB, &NRF_TWI1->TASKS_SUSPEND);
  318. sd_ppi_channel_enable_clr(1 << 0);
  319. }
  320. else
  321. {
  322. NRF_PPI->CH[0].EEP = (uint32_t)&NRF_TWI1->EVENTS_BB;
  323. NRF_PPI->CH[0].TEP = (uint32_t)&NRF_TWI1->TASKS_SUSPEND;
  324. NRF_PPI->CHENCLR = PPI_CHENCLR_CH0_Msk;
  325. }
  326.  
  327. NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
  328.  
  329. return twi_master_clear_bus();
  330. }
  331.  
  332.  
  333. /** @brief Function for transfer by twi_master.
  334. */
  335. bool twi_master_transfer(uint8_t address,
  336. uint8_t * data,
  337. uint8_t data_length,
  338. bool issue_stop_condition)
  339. {
  340. bool transfer_succeeded = false;
  341. if (data_length > 0 && twi_master_clear_bus())
  342. {
  343. NRF_TWI1->ADDRESS = (address >> 1);
  344.  
  345. if ((address & TWI_READ_BIT))
  346. {
  347. transfer_succeeded = twi_master_read(data, data_length, issue_stop_condition);
  348. }
  349. else
  350. {
  351. transfer_succeeded = twi_master_write(data, data_length, issue_stop_condition);
  352. }
  353. }
  354. return transfer_succeeded;
  355. }
  356.  
  357. /*lint --flb "Leave library region" */
Advertisement
Add Comment
Please, Sign In to add comment