1 //*****************************************************************************
2 //
3 // onewire.c - Driver for OneWire master module.
4 //
5 // Copyright (c) 2012-2017 Texas Instruments Incorporated.  All rights reserved.
6 // Software License Agreement
7 //
8 //   Redistribution and use in source and binary forms, with or without
9 //   modification, are permitted provided that the following conditions
10 //   are met:
11 //
12 //   Redistributions of source code must retain the above copyright
13 //   notice, this list of conditions and the following disclaimer.
14 //
15 //   Redistributions in binary form must reproduce the above copyright
16 //   notice, this list of conditions and the following disclaimer in the
17 //   documentation and/or other materials provided with the
18 //   distribution.
19 //
20 //   Neither the name of Texas Instruments Incorporated nor the names of
21 //   its contributors may be used to endorse or promote products derived
22 //   from this software without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 //
36 //*****************************************************************************
37 
38 //*****************************************************************************
39 //
40 //! \addtogroup onewire_api
41 //! @{
42 //
43 //*****************************************************************************
44 
45 #include <ti/devices/msp432e4/inc/msp432e411y.h>
46 #include "types.h"
47 #include <stdint.h>
48 #include <stdbool.h>
49 #include <stdint.h>
50 #include "inc/hw_onewire.h"
51 #include "inc/hw_sysctl.h"
52 #include "debug.h"
53 #include "interrupt.h"
54 #include "onewire.h"
55 #include "sysctl.h"
56 
57 //*****************************************************************************
58 //
59 // A bit mask for all transaction related fields in the 1-Wire control
60 // register.
61 //
62 //*****************************************************************************
63 #define ONEWIRE_TXN_MASK        (ONEWIRE_CS_OP_M | ONEWIRE_CS_SZ_M |          \
64                                  ONEWIRE_CS_BSIZE_M)
65 
66 //*****************************************************************************
67 //
68 // Left-shift value for the control register's transaction size.
69 //
70 //*****************************************************************************
71 #define ONEWIRE_TXN_SIZE_LSHIFT 3
72 
73 //*****************************************************************************
74 //
75 // Left-shift value for the control register's last byte bit size.
76 //
77 //*****************************************************************************
78 #define ONEWIRE_TXN_BSIZE_LSHIFT                                              \
79                                 16
80 
81 //*****************************************************************************
82 //
83 //! Initializes the 1-Wire module.
84 //!
85 //! \param ui32Base specifies the base address of the 1-Wire module.
86 //! \param ui32InitFlags provides the initialization flags.
87 //!
88 //! This function configures and initializes the 1-Wire interface for use.
89 //!
90 //! The \e ui32InitFlags parameter is a combination of the following:
91 //!
92 //! - \b ONEWIRE_INIT_SPD_STD - standard speed bus timings
93 //! - \b ONEWIRE_INIT_SPD_OD - overdrive speed bus timings
94 //! - \b ONEWIRE_INIT_READ_STD - standard read sampling timing
95 //! - \b ONEWIRE_INIT_READ_LATE - late read sampling timing
96 //! - \b ONEWIRE_INIT_ATR - standard answer-to-reset presence detect
97 //! - \b ONEWIRE_INIT_NO_ATR - no answer-to-reset presence detect
98 //! - \b ONEWIRE_INIT_STD_POL - normal signal polarity
99 //! - \b ONEWIRE_INIT_ALT_POL - alternate (reverse) signal polarity
100 //! - \b ONEWIRE_INIT_1_WIRE_CFG - standard 1-Wire (1 data pin) setup
101 //! - \b ONEWIRE_INIT_2_WIRE_CFG - alternate 2-Wire (2 data pin) setup
102 //!
103 //! \return None.
104 //
105 //*****************************************************************************
106 void
OneWireInit(uint32_t ui32Base,uint32_t ui32InitFlags)107 OneWireInit(uint32_t ui32Base, uint32_t ui32InitFlags)
108 {
109     //
110     // Check the arguments.
111     //
112     ASSERT(ui32Base == ONEWIRE0_BASE);
113 
114     //
115     // Initialize control register.
116     //
117     HWREG(ui32Base + ONEWIRE_O_CS) = ui32InitFlags;
118 }
119 
120 //*****************************************************************************
121 //
122 //! Issues a reset on the 1-Wire bus.
123 //!
124 //! \param ui32Base specifies the base address of the 1-Wire module.
125 //!
126 //! This function causes the 1-Wire module to generate a reset signal on the
127 //! 1-Wire bus.
128 //!
129 //! \return None.
130 //
131 //*****************************************************************************
132 void
OneWireBusReset(uint32_t ui32Base)133 OneWireBusReset(uint32_t ui32Base)
134 {
135     //
136     // Check the argument.
137     //
138     ASSERT(ui32Base == ONEWIRE0_BASE);
139 
140     //
141     // Issue a bus reset.
142     //
143     HWREG(ui32Base + ONEWIRE_O_CS) |= ONEWIRE_CS_RST;
144 }
145 
146 //*****************************************************************************
147 //
148 //! Retrieves the 1-Wire bus condition status.
149 //!
150 //! \param ui32Base specifies the base address of the 1-Wire module.
151 //!
152 //! This function returns the 1-Wire bus conditions reported by the 1-Wire
153 //! module.  These conditions could be a logical OR of any of the following:
154 //!
155 //! - \b ONEWIRE_BUS_STATUS_BUSY - A read, write, or reset is active.
156 //! - \b ONEWIRE_BUS_STATUS_NO_SLAVE - No slave presence pulses detected.
157 //! - \b ONEWIRE_BUS_STATUS_STUCK - The bus is being held low by non-master.
158 //!
159 //! \return Returns the 1-Wire bus conditions if detected else zero.
160 //
161 //*****************************************************************************
162 uint32_t
OneWireBusStatus(uint32_t ui32Base)163 OneWireBusStatus(uint32_t ui32Base)
164 {
165     //
166     // Check the argument.
167     //
168     ASSERT(ui32Base == ONEWIRE0_BASE);
169 
170     //
171     // Return the status bits from control and status register.
172     //
173     return (HWREG(ui32Base + ONEWIRE_O_CS) & (ONEWIRE_CS_BUSY |
174             ONEWIRE_CS_NOATR |
175             ONEWIRE_CS_STUCK));
176 }
177 
178 //*****************************************************************************
179 //
180 //! Retrieves data from the 1-Wire interface.
181 //!
182 //! \param ui32Base specifies the base address of the 1-Wire module.
183 //! \param pui32Data is a pointer to storage to hold the read data.
184 //!
185 //! This function reads data from the 1-Wire module once all active bus
186 //! operations are completed.  By protocol definition, bit data defaults to
187 //! a 1.  Thus if a slave did not signal any 0-bit data, this read returns
188 //! 0xffffffff.
189 //!
190 //! \return None.
191 //
192 //*****************************************************************************
193 void
OneWireDataGet(uint32_t ui32Base,uint32_t * pui32Data)194 OneWireDataGet(uint32_t ui32Base, uint32_t *pui32Data)
195 {
196     //
197     // Check the arguments.
198     //
199     ASSERT(ui32Base == ONEWIRE0_BASE);
200     ASSERT(pui32Data);
201 
202     //
203     // Wait for any active operations to complete.
204     //
205     while (HWREG(ui32Base + ONEWIRE_O_CS) & ONEWIRE_CS_BUSY)
206     {
207     }
208 
209     //
210     // Copy the data into the provided storage.
211     //
212     *pui32Data = HWREG(ui32Base + ONEWIRE_O_DATR);
213 }
214 
215 //*****************************************************************************
216 //
217 //! Retrieves data from the 1-Wire interface.
218 //!
219 //! \param ui32Base specifies the base address of the 1-Wire module.
220 //! \param pui32Data is a pointer to storage to hold the read data.
221 //!
222 //! This function reads data from the 1-Wire module if there are no active
223 //! operations on the bus.  Otherwise it returns without reading the data from
224 //! the module.
225 //!
226 //! By protocol definition, bit data defaults to a 1.  Thus if a slave did
227 //! not signal any 0-bit data, this read returns 0xffffffff.
228 //!
229 //! \return Returns \b true if a data read was performed, or \b false if the
230 //! bus was not idle and no data was read.
231 //
232 //*****************************************************************************
233 bool
OneWireDataGetNonBlocking(uint32_t ui32Base,uint32_t * pui32Data)234 OneWireDataGetNonBlocking(uint32_t ui32Base, uint32_t *pui32Data)
235 {
236     //
237     // Check the arguments.
238     //
239     ASSERT(ui32Base == ONEWIRE0_BASE);
240     ASSERT(pui32Data);
241 
242     //
243     // If the bus is busy, return without reading.
244     //
245     if (HWREG(ui32Base + ONEWIRE_O_CS) & ONEWIRE_CS_BUSY)
246     {
247         return (false);
248     }
249 
250     //
251     // Copy the data into the provided storage.
252     //
253     *pui32Data = HWREG(ui32Base + ONEWIRE_O_DATR);
254 
255     //
256     // Notify the caller data was read from the read register.
257     //
258     return (true);
259 }
260 
261 //*****************************************************************************
262 //
263 //! Clears the 1-Wire module interrupt sources.
264 //!
265 //! \param ui32Base specifies the base address of the 1-Wire module.
266 //! \param ui32IntFlags is a bit mask of the interrupt sources to be cleared.
267 //!
268 //! This function clears the specified 1-Wire interrupt sources so that they no
269 //! longer assert.  This function must be called in the interrupt handler to
270 //! keep the interrupts from being triggered again immediately upon exit.  The
271 //! \e ui32IntFlags parameter can be a logical OR of any of the following:
272 //!
273 //! - \b ONEWIRE_INT_RESET_DONE - Bus reset has just completed.
274 //! - \b ONEWIRE_INT_OP_DONE - Read or write operation completed.  If a
275 //!   combined write and read operation was set up, the interrupt signals the
276 //!   read is done.
277 //! - \b ONEWIRE_INT_NO_SLAVE - No presence detect was signaled by a slave.
278 //! - \b ONEWIRE_INT_STUCK - Bus is being held low by non-master.
279 //! - \b ONEWIRE_INT_DMA_DONE - DMA operation has completed.
280 //!
281 //! \note Because there is a write buffer in the Cortex-M processor, it may
282 //! take several clock cycles before the interrupt source is actually cleared.
283 //! Therefore, it is recommended that the interrupt source be cleared early in
284 //! the interrupt handler (as opposed to the very last action) to avoid
285 //! returning from the interrupt handler before the interrupt source is
286 //! actually cleared.  Failure to do so may result in the interrupt handler
287 //! being immediately reentered (because the interrupt controller still sees
288 //! the interrupt source asserted).
289 //!
290 //! \return None.
291 //
292 //*****************************************************************************
293 void
OneWireIntClear(uint32_t ui32Base,uint32_t ui32IntFlags)294 OneWireIntClear(uint32_t ui32Base, uint32_t ui32IntFlags)
295 {
296     //
297     // Check the argument.
298     //
299     ASSERT(ui32Base == ONEWIRE0_BASE);
300     ASSERT((ui32IntFlags & ~(ONEWIRE_IM_RST | ONEWIRE_IM_OPC | ONEWIRE_IM_DMA |
301                              ONEWIRE_IM_NOATR | ONEWIRE_IM_STUCK)) == 0);
302 
303     //
304     // Clear the requested interrupts.
305     //
306     HWREG(ui32Base + ONEWIRE_O_ICR) = ui32IntFlags;
307 }
308 
309 //*****************************************************************************
310 //
311 //! Disables individual 1-Wire module interrupt sources.
312 //!
313 //! \param ui32Base specifies the base address of the 1-Wire module.
314 //! \param ui32IntFlags is a bit mask of the interrupt sources to be disabled.
315 //!
316 //! This function disables the indicated 1-Wire interrupt sources.  The
317 //! \e ui32IntFlags parameter can be a logical OR of any of the following:
318 //!
319 //! - \b ONEWIRE_INT_RESET_DONE - Bus reset has just completed.
320 //! - \b ONEWIRE_INT_OP_DONE - Read or write operation completed.  If a
321 //!   combined write and read operation was set up, the interrupt signals the
322 //!   read is done.
323 //! - \b ONEWIRE_INT_NO_SLAVE - No presence detect was signaled by a slave.
324 //! - \b ONEWIRE_INT_STUCK - Bus is being held low by non-master.
325 //! - \b ONEWIRE_INT_DMA_DONE - DMA operation has completed
326 //!
327 //! \return None.
328 //
329 //*****************************************************************************
330 void
OneWireIntDisable(uint32_t ui32Base,uint32_t ui32IntFlags)331 OneWireIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags)
332 {
333     //
334     // Check the arguments.
335     //
336     ASSERT(ui32Base == ONEWIRE0_BASE);
337     ASSERT((ui32IntFlags & ~(ONEWIRE_IM_RST | ONEWIRE_IM_OPC | ONEWIRE_IM_DMA |
338                              ONEWIRE_IM_NOATR | ONEWIRE_IM_STUCK)) == 0);
339 
340     //
341     // Disable the requested interrupts.
342     //
343     HWREG(ui32Base + ONEWIRE_O_IM) &= ~ui32IntFlags;
344 }
345 
346 //*****************************************************************************
347 //
348 //! Enables individual 1-Wire module interrupt sources.
349 //!
350 //! \param ui32Base specifies the base address of the 1-Wire module.
351 //! \param ui32IntFlags is a bit mask of the interrupt sources to be enabled.
352 //!
353 //! This function enables the indicated 1-Wire interrupt sources.  Only the
354 //! sources that are enabled can be reflected to the processor interrupt;
355 //! disabled sources have no effect on the processor.  The \e ui32IntFlags
356 //! parameter can be a logical OR of any of the following:
357 //!
358 //! - \b ONEWIRE_INT_RESET_DONE - Bus reset has just completed.
359 //! - \b ONEWIRE_INT_OP_DONE - Read or write operation completed.  If a
360 //!   combined write and read operation was set up, the interrupt signals the
361 //!   read is done.
362 //! - \b ONEWIRE_INT_NO_SLAVE - No presence detect was signaled by a slave.
363 //! - \b ONEWIRE_INT_STUCK - Bus is being held low by non-master.
364 //! - \b ONEWIRE_INT_DMA_DONE - DMA operation has completed
365 //!
366 //! \return None.
367 //
368 //*****************************************************************************
369 void
OneWireIntEnable(uint32_t ui32Base,uint32_t ui32IntFlags)370 OneWireIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags)
371 {
372     //
373     // Check the arguments.
374     //
375     ASSERT(ui32Base == ONEWIRE0_BASE);
376     ASSERT((ui32IntFlags & ~(ONEWIRE_IM_RST | ONEWIRE_IM_OPC | ONEWIRE_IM_DMA |
377                              ONEWIRE_IM_NOATR | ONEWIRE_IM_STUCK)) == 0);
378 
379     //
380     // Enable the requested interrupts.
381     //
382     HWREG(ui32Base + ONEWIRE_O_IM) |= ui32IntFlags;
383 }
384 
385 //*****************************************************************************
386 //
387 //! Gets the current 1-Wire interrupt status.
388 //!
389 //! \param ui32Base specifies the base address of the 1-Wire module.
390 //! \param bMasked is \b false if the raw interrupt status is required or
391 //! \b true if the masked interrupt status is required.
392 //!
393 //! This function returns the interrupt status for the 1-Wire module.  Either
394 //! the raw interrupt status or the status of interrupts that are allowed to
395 //! reflect to the processor can be returned.
396 //!
397 //! \return Returns the masked or raw 1-Wire interrupt status, as a bit field
398 //! of any of the following values:
399 //!
400 //! - \b ONEWIRE_INT_RESET_DONE - Bus reset has just completed.
401 //! - \b ONEWIRE_INT_OP_DONE - Read or write operation completed.
402 //! - \b ONEWIRE_INT_NO_SLAVE - No presence detect was signaled by a slave.
403 //! - \b ONEWIRE_INT_STUCK - Bus is being held low by non-master.
404 //! - \b ONEWIRE_INT_DMA_DONE - DMA operation has completed
405 //
406 //*****************************************************************************
407 uint32_t
OneWireIntStatus(uint32_t ui32Base,bool bMasked)408 OneWireIntStatus(uint32_t ui32Base, bool bMasked)
409 {
410     //
411     // Check the argument.
412     //
413     ASSERT(ui32Base == ONEWIRE0_BASE);
414 
415     //
416     // Return either the interrupt status or the raw interrupt status as
417     // requested.
418     //
419     if (bMasked)
420     {
421         return (HWREG(ui32Base + ONEWIRE_O_MIS));
422     }
423     else
424     {
425         return (HWREG(ui32Base + ONEWIRE_O_RIS));
426     }
427 }
428 
429 //*****************************************************************************
430 //
431 //! Returns the 1-Wire controller interrupt number.
432 //!
433 //! \param ui32Base specifies the 1-Wire module base address.
434 //!
435 //! This function returns the interrupt number for the 1-Wire module with the
436 //! base address passed in the \e ui32Base parameter.
437 //!
438 //! \return Returns a 1-Wire interrupt number or 0 if the interrupt does not
439 //! exist.
440 //
441 //*****************************************************************************
442 static uint32_t
_OneWireIntNumberGet(uint32_t ui32Base)443 _OneWireIntNumberGet(uint32_t ui32Base)
444 {
445     uint32_t ui32Int;
446 
447     ASSERT(ui32Base == ONEWIRE0_BASE);
448 
449     ui32Int = INT_ONEWIRE0;
450 
451     return (ui32Int);
452 }
453 
454 //*****************************************************************************
455 //
456 //! Registers an interrupt handler for the 1-Wire module.
457 //!
458 //! \param ui32Base is the base address of the 1-Wire module.
459 //! \param pfnHandler is a pointer to the function to be called when the
460 //! 1-Wire interrupt occurs.
461 //!
462 //! This function sets the handler to be called when a 1-Wire interrupt occurs.
463 //! This function enables the global interrupt in the interrupt controller;
464 //! specific 1-Wire interrupts must be enabled via OneWireIntEnable().  If
465 //! necessary, it is the interrupt handler's responsibility to clear the
466 //! interrupt source via OneWireIntClear().
467 //!
468 //! \sa IntRegister() for important information about registering interrupt
469 //! handlers.
470 //!
471 //! \return None.
472 //
473 //*****************************************************************************
474 void
OneWireIntRegister(uint32_t ui32Base,void (* pfnHandler)(void))475 OneWireIntRegister(uint32_t ui32Base, void (*pfnHandler)(void))
476 {
477     uint32_t ui32Int;
478 
479     //
480     // Check the argument.
481     //
482     ASSERT(ui32Base == ONEWIRE0_BASE);
483     ASSERT(pfnHandler);
484 
485     //
486     // Get the actual interrupt number for the 1-Wire module.
487     //
488     ui32Int = _OneWireIntNumberGet(ui32Base);
489 
490     ASSERT(ui32Int != 0);
491 
492     //
493     // Register the interrupt handler.
494     //
495     IntRegister(ui32Int, pfnHandler);
496 
497     //
498     // Enable the 1-Wire peripheral interrupt.
499     //
500     IntEnable(ui32Int);
501 }
502 
503 //*****************************************************************************
504 //
505 //! Unregisters an interrupt handler for the 1-Wire module.
506 //!
507 //! \param ui32Base is the base address of the 1-Wire module.
508 //!
509 //! This function clears the handler to be called when an 1-Wire interrupt
510 //! occurs.  This function also masks off the interrupt in the interrupt
511 //! controller so that the interrupt handler no longer is called.
512 //!
513 //! \sa IntRegister() for important information about registering interrupt
514 //! handlers.
515 //!
516 //! \return None.
517 //
518 //*****************************************************************************
519 void
OneWireIntUnregister(uint32_t ui32Base)520 OneWireIntUnregister(uint32_t ui32Base)
521 {
522     uint32_t ui32Int;
523 
524     //
525     // Check the argument.
526     //
527     ASSERT(ui32Base == ONEWIRE0_BASE);
528 
529     //
530     // Get the actual interrupt number for the 1-Wire module.
531     //
532     ui32Int = _OneWireIntNumberGet(ui32Base);
533     ASSERT(ui32Int != 0);
534 
535     //
536     // Disable the 1-Wire peripheral interrupt.
537     //
538     IntDisable(ui32Int);
539 
540     //
541     // Unregister the interrupt handler.
542     //
543     IntUnregister(ui32Int);
544 }
545 
546 //*****************************************************************************
547 //
548 //! Disables 1-Wire DMA operations.
549 //!
550 //! \param ui32Base is the base address of the 1-Wire module.
551 //! \param ui32DMAFlags is a bit mask of the DMA features to disable.
552 //!
553 //! This function is used to disable 1-Wire DMA features that were enabled
554 //! by OneWireDMAEnable().  The specified 1-Wire DMA features are disabled.
555 //! The \e ui32DMAFlags parameter is a combination of the following:
556 //!
557 //! - \b ONEWIRE_DMA_BUS_RESET - Issue a 1-Wire bus reset before starting
558 //! - \b ONEWIRE_DMA_OP_READ - Read after each module transaction
559 //! - \b ONEWIRE_DMA_OP_MULTI_WRITE - Write after each previous write
560 //! - \b ONEWIRE_DMA_OP_MULTI_READ - Read after each previous read
561 //! - \b ONEWIRE_DMA_MODE_SG - Start DMA on enable then repeat on each
562 //!   completion
563 //! - \b ONEWIRE_DMA_OP_SZ_8 - Bus read/write of 8 bits
564 //! - \b ONEWIRE_DMA_OP_SZ_16 - Bus read/write of 16 bits
565 //! - \b ONEWIRE_DMA_OP_SZ_32 - Bus read/write of 32 bits
566 //!
567 //! \return None.
568 //
569 //*****************************************************************************
570 void
OneWireDMADisable(uint32_t ui32Base,uint32_t ui32DMAFlags)571 OneWireDMADisable(uint32_t ui32Base, uint32_t ui32DMAFlags)
572 {
573     //
574     // Check the arguments.
575     //
576     ASSERT(ui32Base == ONEWIRE0_BASE);
577     ASSERT(ui32DMAFlags > 0);
578 
579     //
580     // Clear the transaction size bits
581     //
582     HWREG(ui32Base + ONEWIRE_O_CS) = (HWREG(ui32Base + ONEWIRE_O_CS) &
583                                       ~(ONEWIRE_TXN_MASK));
584 
585     //
586     // Disable the DMA features as requested.
587     //
588     HWREG(ui32Base + ONEWIRE_O_DMA) &= ~(ui32DMAFlags & 0xff);
589 }
590 
591 //*****************************************************************************
592 //
593 //! Enables 1-Wire DMA operations.
594 //!
595 //! \param ui32Base is the base address of the 1-Wire module.
596 //! \param ui32DMAFlags is a bit mask of the DMA features to enable.
597 //!
598 //! This function enables the specified 1-Wire DMA features.  The 1-Wire module
599 //! can be configured for write operations, read operations, small write and
600 //! read operations, and scatter-gather support of mixed operations.
601 //!
602 //! The \e ui32DMAFlags parameter is a combination of the following:
603 //!
604 //! - \b ONEWIRE_DMA_BUS_RESET - Issue a 1-Wire bus reset before starting
605 //! - \b ONEWIRE_DMA_OP_READ - Read after each module transaction
606 //! - \b ONEWIRE_DMA_OP_MULTI_WRITE - Write after each previous write
607 //! - \b ONEWIRE_DMA_OP_MULTI_READ - Read after each previous read
608 //! - \b ONEWIRE_DMA_MODE_SG - Start DMA on enable then repeat on each
609 //!   completion
610 //! - \b ONEWIRE_DMA_OP_SZ_8 - Bus read/write of 8 bits
611 //! - \b ONEWIRE_DMA_OP_SZ_16 - Bus read/write of 16 bits
612 //! - \b ONEWIRE_DMA_OP_SZ_32 - Bus read/write of 32 bits
613 //!
614 //! \note The uDMA controller must be properly configured before DMA can be
615 //! used with the 1-Wire module.
616 //!
617 //! \return None.
618 //
619 //*****************************************************************************
620 void
OneWireDMAEnable(uint32_t ui32Base,uint32_t ui32DMAFlags)621 OneWireDMAEnable(uint32_t ui32Base, uint32_t ui32DMAFlags)
622 {
623     //
624     // Check the arguments.
625     //
626     ASSERT(ui32Base == ONEWIRE0_BASE);
627     ASSERT(ui32DMAFlags > 0);
628 
629     //
630     // set up the transaction size.
631     //
632     HWREG(ui32Base + ONEWIRE_O_CS) = ((HWREG(ui32Base + ONEWIRE_O_CS) &
633                                        ~(ONEWIRE_TXN_MASK)) |
634                                       (ui32DMAFlags >> 8));
635 
636     //
637     // Enable DMA with the parameters provided.
638     //
639     HWREG(ui32Base + ONEWIRE_O_DMA) = (ui32DMAFlags & 0xf);
640 
641     //
642     // If a read transaction was requested, seed the write data register.  This
643     // will trigger the DMA reads to start.  This should not be done for
644     // scatter-gather operations.
645     //
646     if ((ui32DMAFlags & (ONEWIRE_DMA_DMAOP_RDSNG | ONEWIRE_DMA_DMAOP_RDMUL)) &&
647             !(ui32DMAFlags & ONEWIRE_DMA_SG))
648     {
649         //
650         // Workaround for DMA receive trigger errata.
651         //
652         SysCtlDelay(9);
653 
654         //
655         // Write DATW to trigger DMA receive start.
656         //
657         HWREG(ui32Base + ONEWIRE_O_DATW) = 0xffffffff;
658     }
659 }
660 
661 //*****************************************************************************
662 //
663 //! Performs a 1-Wire protocol transaction on the bus.
664 //!
665 //! \param ui32Base specifies the base address of the 1-Wire module.
666 //! \param ui32OpMode sets the transaction type.
667 //! \param ui32Data is the data for a write operation.
668 //! \param ui32BitCnt specifies the number of valid bits (1-32) for the
669 //! operation.
670 //!
671 //! This function performs a 1-Wire protocol transaction, read and/or write, on
672 //! the bus.  The application should confirm the bus is idle before starting a
673 //! read or write transaction.
674 //!
675 //! The \e ui32OpMode defines the activity for the bus operations and is a
676 //! logical OR of the following:
677 //!
678 //! - \b ONEWIRE_OP_RESET - Indicates the operation should be started with
679 //!   a bus reset.
680 //! - \b ONEWIRE_OP_WRITE - A write operation
681 //! - \b ONEWIRE_OP_READ - A read operation
682 //!
683 //! \note If both a read and write operation are requested, the write will be
684 //! performed prior to the read.
685 //!
686 //! \return None.
687 //
688 //*****************************************************************************
689 void
OneWireTransaction(uint32_t ui32Base,uint32_t ui32OpMode,uint32_t ui32Data,uint32_t ui32BitCnt)690 OneWireTransaction(uint32_t ui32Base, uint32_t ui32OpMode, uint32_t ui32Data,
691                    uint32_t ui32BitCnt)
692 {
693     uint32_t ui32Transaction;
694 
695     //
696     // Check the arguments.
697     //
698     ASSERT(ui32Base == ONEWIRE0_BASE);
699     ASSERT((ui32OpMode & (ONEWIRE_OP_RESET | ONEWIRE_OP_WRITE |
700                           ONEWIRE_OP_READ)) > 0);
701     ASSERT((ui32BitCnt >= 1) && (ui32BitCnt <= 32));
702 
703     //
704     // Read the control register and clear any transaction related
705     // bit fields.
706     //
707     ui32Transaction = HWREG(ui32Base + ONEWIRE_O_CS) & ~(ONEWIRE_TXN_MASK);
708 
709     //
710     // Add the user specified operation flags.
711     //
712     ui32Transaction |= (ui32OpMode & (ONEWIRE_OP_RESET | ONEWIRE_OP_WRITE |
713                                       ONEWIRE_OP_READ));
714 
715     //
716     // set up for a read or write transaction.
717     //
718     if (ui32Transaction & (ONEWIRE_CS_OP_WR | ONEWIRE_CS_OP_RD))
719     {
720         //
721         // Configure the 1-Wire module for the transaction size.  This is
722         // specified as 1-4 bytes and the specific bit size for the last
723         // byte therein.
724         //
725         ui32Transaction |= ((((ui32BitCnt % 8) ? (ui32BitCnt / 8) + 1 :
726                               (ui32BitCnt / 8)) - 1) <<
727                             ONEWIRE_TXN_SIZE_LSHIFT);
728         ui32Transaction |= ((ui32BitCnt % 8) << ONEWIRE_TXN_BSIZE_LSHIFT);
729 
730         //
731         // Write specific setup.
732         //
733         if (ui32Transaction & ONEWIRE_CS_OP_WR)
734         {
735             //
736             // Load the data into the write register.
737             //
738             HWREG(ui32Base + ONEWIRE_O_DATW) = ui32Data;
739         }
740     }
741 
742     //
743     // Start the transaction.
744     //
745     HWREG(ui32Base + ONEWIRE_O_CS) = ui32Transaction;
746 }
747 
748 //*****************************************************************************
749 //
750 // Close the Doxygen group.
751 //! @}
752 //
753 //*****************************************************************************
754