am_hal_ios.c 42.8 KB
Newer Older
L
lin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
//*****************************************************************************
//
//  am_hal_ios.c
//! @file
//!
//! @brief Functions for interfacing with the IO Slave module
//!
//! @addtogroup ios2 IO Slave (SPI/I2C)
//! @ingroup apollo2hal
//! @{
//
//*****************************************************************************

//*****************************************************************************
//
// Copyright (c) 2017, Ambiq Micro
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
L
lin 已提交
45
// This is part of revision 1.2.11 of the AmbiqSuite Development Package.
L
lin 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
//
//*****************************************************************************

#include <stdint.h>
#include <stdbool.h>
#include "am_mcu_apollo.h"

//*****************************************************************************
//
// SRAM Buffer structure
//
//*****************************************************************************
typedef struct
{
    uint8_t           *pui8Data;
    volatile uint32_t ui32WriteIndex;
    volatile uint32_t ui32ReadIndex;
    volatile uint32_t ui32Length;
L
lin 已提交
64
    uint32_t          ui32FifoInc;
L
lin 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    uint32_t          ui32Capacity;
}
am_hal_ios_buffer_t;

am_hal_ios_buffer_t g_sSRAMBuffer;

//*****************************************************************************
//
// Forward declarations of static funcitons.
//
//*****************************************************************************
static void am_hal_ios_buffer_init(am_hal_ios_buffer_t *psBuffer,
                                   void *pvArray, uint32_t ui32Bytes);

static void fifo_write(uint8_t *pui8Data, uint32_t ui32NumBytes);

//*****************************************************************************
//
// Function-like macros.
//
//*****************************************************************************
#define am_hal_ios_buffer_empty(psBuffer)                                   \
    ((psBuffer)->ui32Length == 0)

#define am_hal_ios_buffer_full(psBuffer)                                    \
    ((psBuffer)->ui32Length == (psBuffer)->ui32Capacity)

#define am_hal_ios_buffer_data_left(psBuffer)                               \
    ((psBuffer)->ui32Length)

//*****************************************************************************
//
// Global Variables
//
//*****************************************************************************
volatile uint8_t * const am_hal_ios_pui8LRAM = (uint8_t *)REG_IOSLAVE_BASEADDR;
uint8_t *g_pui8FIFOBase = (uint8_t *) REG_IOSLAVE_BASEADDR;
uint8_t *g_pui8FIFOEnd = (uint8_t *) REG_IOSLAVE_BASEADDR;
uint8_t *g_pui8FIFOPtr = (uint8_t *) REG_IOSLAVE_BASEADDR;
uint8_t g_ui32HwFifoSize = 0;
uint32_t g_ui32FifoBaseOffset = 0;

L
lin 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
//*****************************************************************************
//
// Checks to see if this processor is a Rev B2 device.
//
// This is needed to disable SHELBY-1654 workaround.
//
//*****************************************************************************
bool
isRevB2(void)
{
    //
    // Check to make sure the major rev is B and the minor rev is 2.
    //
    return ( (AM_REG(MCUCTRL, CHIPREV) & 0xFF) ==   \
        (AM_REG_MCUCTRL_CHIPREV_REVMAJ_B | (AM_REG_MCUCTRL_CHIPREV_REVMIN_REV0 + 2)) );
}

L
lin 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
//*****************************************************************************
//
//! @brief Enable the IOS in the power control block.
//!
//! This function enables the IOS module in the power control block.
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_pwrctrl_enable(void)
{
    am_hal_pwrctrl_periph_enable(AM_HAL_PWRCTRL_IOS);
}

//*****************************************************************************
//
//! @brief Disable the IOS in the power control block.
//!
//! This function disables the IOS module in the power control block.
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_pwrctrl_disable(void)
{
    am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_IOS);
}

//*****************************************************************************
//
//! @brief Enables the IOS module
//!
//! This function enables the IOSLAVE module using the IFCEN bitfield in the
//! IOSLAVE_CFG register.
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_enable(uint32_t ui32Module)
{
    AM_REGn(IOSLAVE, ui32Module, CFG) |= AM_REG_IOSLAVE_CFG_IFCEN(1);
}

//*****************************************************************************
//
//! @brief Disables the IOSLAVE module.
//!
//! This function disables the IOSLAVE module using the IFCEN bitfield in the
//! IOSLAVE_CFG register.
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_disable(uint32_t ui32Module)
{
    AM_REGn(IOSLAVE, ui32Module, CFG) &= ~(AM_REG_IOSLAVE_CFG_IFCEN(1));
}

//*****************************************************************************
//
//! @brief Configure the IOS module.
//!
//! This function reads the an \e am_hal_ios_config_t structure and uses it to
//! set up the IO Slave module. Please see the information on the configuration
//! structure for more information on the parameters that may be set by this
//! function.
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_config(am_hal_ios_config_t *psConfig)
{
    uint32_t ui32LRAMConfig;

    am_hal_pwrctrl_periph_enable(AM_HAL_PWRCTRL_IOS);

    //
    // Record the FIFO parameters for later use.
    //
    g_pui8FIFOBase = (uint8_t *)(REG_IOSLAVE_BASEADDR + psConfig->ui32FIFOBase);
    g_pui8FIFOEnd = (uint8_t *)(REG_IOSLAVE_BASEADDR + psConfig->ui32RAMBase);
    g_ui32HwFifoSize = g_pui8FIFOEnd - g_pui8FIFOBase;
    g_ui32FifoBaseOffset = psConfig->ui32FIFOBase;

    //
    // Caluclate the value for the IO Slave FIFO configuration register.
    //
    ui32LRAMConfig = AM_REG_IOSLAVE_FIFOCFG_ROBASE(psConfig->ui32ROBase >> 3);
    ui32LRAMConfig |= AM_REG_IOSLAVE_FIFOCFG_FIFOBASE(psConfig->ui32FIFOBase >> 3);
    ui32LRAMConfig |= AM_REG_IOSLAVE_FIFOCFG_FIFOMAX(psConfig->ui32RAMBase >> 3);

    //
    // Just in case, disable the IOS
    //
    am_hal_ios_disable(0);

    //
    // Write the configuration register with the user's selected interface
    // characteristics.
    //
    AM_REG(IOSLAVE, CFG) = psConfig->ui32InterfaceSelect;

    //
    // Write the FIFO configuration register to set the memory map for the LRAM.
    //
    AM_REG(IOSLAVE, FIFOCFG) = ui32LRAMConfig;

    //
    // Enable the IOS. The following configuration options can't be set while
    // the IOS is disabled.
    //
    am_hal_ios_enable(0);

    //
    // Initialize the FIFO pointer to the beginning of the FIFO section.
    //
    am_hal_ios_fifo_ptr_set(psConfig->ui32FIFOBase);

    //
    // Write the FIFO threshold register.
    //
    AM_REG(IOSLAVE, FIFOTHR) = psConfig->ui32FIFOThreshold;
}

//*****************************************************************************
//
//! @brief Set bits in the HOST side IOINTCTL register.
//!
//! This function may be used to set an interrupt bit to the host.
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_host_int_set(uint32_t ui32Interrupt)
{
    //
    // Set a bit that will cause an interrupt to the host.
    //
    AM_REG(IOSLAVE, IOINTCTL) = AM_REG_IOSLAVE_IOINTCTL_IOINTSET(ui32Interrupt);
}

//*****************************************************************************
//
//! @brief Clear bits in the HOST side IOINTCTL register.
//!
//! This function may be used to clear an interrupt bit to the host.
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_host_int_clear(uint32_t ui32Interrupt)
{
    //
    // Clear bits that will cause an interrupt to the host.
    //
    AM_REG(IOSLAVE, IOINTCTL) = AM_REG_IOSLAVE_IOINTCTL_IOINTCLR(ui32Interrupt);
}

//*****************************************************************************
//
//! @brief Get the bits in the HOST side IOINTCTL register.
//!
//! This function may be used to read the host side interrupt bits.
//!
//! @return None.
//
//*****************************************************************************
uint32_t
am_hal_ios_host_int_get(void)
{
    //
    // return the value of the bits that will cause an interrupt to the host.
    //
    return AM_BFR(IOSLAVE, IOINTCTL, IOINT);
}

//*****************************************************************************
//
//! @brief Get the enable bits in the HOST side IOINTCTL register.
//!
//! This function may be used to read the host side interrupt bits.
//!
//! @return None.
//
//*****************************************************************************
uint32_t
am_hal_ios_host_int_enable_get(void)
{
    //
    // return the value of the bits that will cause an interrupt to the host.
    //
    return AM_BFR(IOSLAVE, IOINTCTL, IOINTEN);
}

//*****************************************************************************
//
//! @brief Enable an IOS Access Interrupt.
//!
//! This function may be used to enable an interrupt to the NVIC.
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_access_int_enable(uint32_t ui32Interrupt)
{
    //
    // OR the desired interrupt into the enable register.
    //
    AM_REG(IOSLAVE, REGACCINTEN) |= ui32Interrupt;
}

//*****************************************************************************
//
//! @brief Return all enabled IOS Access Interrupts.
//!
//! This function may be used to return all enabled IOS Access interrupts.
//!
//! @return the enabled interrrupts.
//
//*****************************************************************************
uint32_t
am_hal_ios_access_int_enable_get(void)
{
    //
    // Return the enabled interrupts.
    //
    return AM_REG(IOSLAVE, REGACCINTEN);
}

//*****************************************************************************
//
//! @brief Disable an IOS Access Interrupt.
//!
//! This function may be used to disable an interrupt to the NVIC.
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_access_int_disable(uint32_t ui32Interrupt)
{
    //
    // Clear the desired bit from the interrupt enable register.
    //
    AM_REG(IOSLAVE, REGACCINTEN) &= ~(ui32Interrupt);
}

//*****************************************************************************
//
//! @brief Clear an IOS Access Interrupt.
//!
//! This function may be used to clear an interrupt to the NVIC.
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_access_int_clear(uint32_t ui32Interrupt)
{
    //
    // Use the interrupt clear register to deactivate the chosen interrupt.
    //
    AM_REG(IOSLAVE, REGACCINTCLR) = ui32Interrupt;
}

//*****************************************************************************
//
//! @brief Set an IOS Access Interrupt.
//!
//! This function may be used to set an interrupt to the NVIC.
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_access_int_set(uint32_t ui32Interrupt)
{
    //
    // Use the interrupt set register to activate the chosen interrupt.
    //
    AM_REG(IOSLAVE, REGACCINTSET) = ui32Interrupt;
}

//*****************************************************************************
//
//! @brief Check the status of an IOS Access Interrupt.
//!
//! @param bEnabledOnly - return only the enabled interrupt status.
//!
//! This function may be used to return the enabled interrupt status.
//!
//! @return the enabled interrupt status.
//
//*****************************************************************************
uint32_t
am_hal_ios_access_int_status_get(bool bEnabledOnly)
{
    if ( bEnabledOnly )
    {
        uint32_t u32RetVal = AM_REG(IOSLAVE, REGACCINTSTAT);
        return u32RetVal & AM_REG(IOSLAVE, REGACCINTEN);

    }
    else
    {
        return AM_REG(IOSLAVE, REGACCINTSTAT);
    }
}

//*****************************************************************************
//
//! @brief Enable an IOS Interrupt.
//!
//! @param ui32Interrupt - desired interrupts.
//!
//! This function may be used to enable an interrupt to the NVIC.
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_int_enable(uint32_t ui32Interrupt)
{
    //
    // OR the desired interrupt into the enable register.
    //
    AM_REG(IOSLAVE, INTEN) |= ui32Interrupt;
}

//*****************************************************************************
//
//! @brief Return all enabled IOS Interrupts.
//!
//! This function may be used to return all enabled IOS interrupts.
//!
//! @return the enabled interrrupts.
//
//*****************************************************************************
uint32_t
am_hal_ios_int_enable_get(void)
{
    //
    // Return the enabled interrupts.
    //
    return AM_REG(IOSLAVE, INTEN);
}

//*****************************************************************************
//
//! @brief Disable an IOS Interrupt.
//!
//! @param ui32Interrupt - desired interrupts.
//!
//! This function may be used to disable an interrupt to the NVIC.
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_int_disable(uint32_t ui32Interrupt)
{
    //
    // Clear the desired bit from the interrupt enable register.
    //
    AM_REG(IOSLAVE, INTEN) &= ~(ui32Interrupt);
}

//*****************************************************************************
//
//! @brief Clear an IOS Interrupt.
//!
//! @param ui32Interrupt - desired interrupts.
//!
//! This function may be used to clear an interrupt to the NVIC.
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_int_clear(uint32_t ui32Interrupt)
{
    //
    // Use the interrupt clear register to deactivate the chosen interrupt.
    //
    AM_REG(IOSLAVE, INTCLR) = ui32Interrupt;
}

//*****************************************************************************
//
//! @brief Set an IOS Interrupt.
//!
//! @param ui32Interrupt - desired interrupts.
//!
//! This function may be used to set an interrupt to the NVIC.
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_int_set(uint32_t ui32Interrupt)
{
    //
    // Use the interrupt clear register to deactivate the chosen interrupt.
    //
    AM_REG(IOSLAVE, INTSET) = ui32Interrupt;
}

//*****************************************************************************
//
//! @brief Write to the LRAM.
//!
//! @param ui32Offset - offset into the LRAM to write.
//! @param ui8Value - value to be written.
//!
//! This function writes ui8Value to offset ui32Offset inside the LRAM.
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_lram_write(uint32_t ui32Offset, uint8_t ui8Value)
{
    //
    // Write the LRAM.
    //
    am_hal_ios_pui8LRAM[ui32Offset] = ui8Value;
}

//*****************************************************************************
//
//! @brief Read from the LRAM.
//!
//! @param ui32Offset - offset into the LRAM to read.
//!
//! This function read from offset ui32Offset inside the LRAM.
//!
//! @return the value at ui32Offset.
//
//*****************************************************************************
uint8_t
am_hal_ios_lram_read(uint32_t ui32Offset)
{
    //
    // Read the LRAM.
    //
    return am_hal_ios_pui8LRAM[ui32Offset];
}

//*****************************************************************************
//
//! @brief Check the status of an IOS Interrupt.
//!
//! @param bEnabledOnly - return only the enabled interrupt status.
//!
//! This function may be used to return the enabled interrupt status.
//!
//! @return the enabled interrupt status.
//
//*****************************************************************************
uint32_t
am_hal_ios_int_status_get(bool bEnabledOnly)
{
    if ( bEnabledOnly )
    {
        uint32_t u32RetVal = AM_REG(IOSLAVE, INTSTAT);
        return u32RetVal & AM_REG(IOSLAVE, INTEN);

    }
    else
    {
        return AM_REG(IOSLAVE, INTSTAT);
    }
}

//*****************************************************************************
//
//! @brief Check the amount of space used in the FIFO
//!
//! This function returns the available data in the overall FIFO yet to be
//! read by the host. This takes into account the SRAM buffer and hardware FIFO
//!
//! @return Bytes used in the Overall FIFO.
//
//*****************************************************************************
uint32_t
am_hal_ios_fifo_space_used(void)
{
    uint32_t ui32Val;
    uint32_t ui32Primask;
    //
    // Start a critical section for thread safety.
    //
    ui32Primask = am_hal_interrupt_master_disable();
    ui32Val = g_sSRAMBuffer.ui32Length;
    ui32Val += AM_BFR(IOSLAVE, FIFOPTR, FIFOSIZ);
    //
    // End the critical section
    //
    am_hal_interrupt_master_set(ui32Primask);
    return ui32Val;
}



//*****************************************************************************
//
//! @brief Check the amount of space left in the FIFO
//!
//! This function returns the available space in the overall FIFO to accept
//! new data. This takes into account the SRAM buffer and hardware FIFO
//!
//! @return Bytes left in the Overall FIFO.
//
//*****************************************************************************
uint32_t
am_hal_ios_fifo_space_left(void)
{
    uint32_t ui32Val;
    uint32_t ui32Primask;
    //
    // Start a critical section for thread safety.
    //
    ui32Primask = am_hal_interrupt_master_disable();
    //
    // We waste one byte in HW FIFO
    //
    ui32Val = g_sSRAMBuffer.ui32Capacity + g_ui32HwFifoSize - 1;
    ui32Val -= g_sSRAMBuffer.ui32Length;
    ui32Val -= AM_BFR(IOSLAVE, FIFOPTR, FIFOSIZ);
    //
    // End the critical section
    //
    am_hal_interrupt_master_set(ui32Primask);
    return ui32Val;
}

//*****************************************************************************
//
//! @brief Check the amount of space left in the hardware FIFO
//!
//! This function reads the IOSLAVE FIFOPTR register and determines the amount
//! of space left in the IOS LRAM FIFO.
//!
//! @return Bytes left in the IOS FIFO.
//
//*****************************************************************************
static uint32_t
fifo_space_left(void)
{
    //
    // We waste one byte in HW FIFO
    //
    return ((uint32_t)g_ui32HwFifoSize- AM_BFR(IOSLAVE, FIFOPTR, FIFOSIZ) - 1);
}

//*****************************************************************************
//
// Helper function for managing IOS FIFO writes.
//
//*****************************************************************************
static void
fifo_write(uint8_t *pui8Data, uint32_t ui32NumBytes)
{
    uint8_t *pFifoPtr = g_pui8FIFOPtr;
    uint8_t *pFifoBase = g_pui8FIFOBase;
    uint8_t *pFifoEnd = g_pui8FIFOEnd;
    while ( ui32NumBytes )
    {
        //
        // Write the data to the FIFO
        //
        *pFifoPtr++ = *pui8Data++;
        ui32NumBytes--;

        //
        // Make sure to wrap the FIFO pointer if necessary.
        //
        if ( pFifoPtr == pFifoEnd )
        {
            pFifoPtr = pFifoBase;
        }
    }
    g_pui8FIFOPtr = pFifoPtr;
}

//
// Assembly code below assumes 8bit FIFOSIZ field aligned at a byte boundary
//
#if (((AM_REG_IOSLAVE_FIFOPTR_FIFOSIZ_M >> AM_REG_IOSLAVE_FIFOPTR_FIFOSIZ_S) != 0xFF) \
        || (AM_REG_IOSLAVE_FIFOPTR_FIFOSIZ_S & 0x3))
#error "FIFOSIZ not 8bit value aligned at byte offset"
#endif

//
// Byte offset of FIFOSIZ field in FIFOPTR register
//
#define BYTEOFFSET_FIFOSIZE             (AM_REG_IOSLAVE_FIFOPTR_FIFOSIZ_S >> 3)

//*****************************************************************************
//
// Helper function in assembly for implementing the ReSync
//
//*****************************************************************************
#if defined(__GNUC_STDC_INLINE__)
#if (AM_REG_IOSLAVE_FIFOPTR_FIFOSIZ_S != 8)
#error "AM_REG_IOSLAVE_FIFOPTR_FIFOSIZ_S not 8"
#endif
__attribute__((naked))
static void
internal_resync_fifoSize(uint32_t wrOffset, uint32_t maxFifoSize, uint32_t hwFifoPtrRegAddr)
{
    __asm
    (
        "   push    {r3,r4}\n\t"                  // Save r3, r4 - used by this function
        "__internal_resync_fifoSize_loop:\n\t"
        "   ldr     r4, [r2]\n\t"                 // Load FIFOPTR register in r4
        "   ubfx    r3, r4, #8, #8\n\t"           // Extract hwFifoSize to r3
        "   uxtb    r4, r4\n\t"                   // Extract rdOffset in r4
        "   subs    r4, r0, r4\n\t"               // fifoSize in r4 = wrOffset - rdOffset
        "   it      cc\n\t"                       // if (wrOffset < rdOffset)
        "   addcc   r4, r4, r1\n\t"               //     fifoSize = maxFifoSize - (rdOffset - wrOffset)
        "   cmp     r3, r4\n\t"                   // (hwFifoSize != fifoSize)
        "   beq     __internal_resync_fifosize_done\n\t"
        "   strb    r4, [r2, #1]\n\t"             // Overwrite FIFOSIZ value with fifoSize
        "   b       __internal_resync_fifoSize_loop\n\t" // Repeat the check
        "__internal_resync_fifosize_done:\n\t"
        "   pop     {r3,r4}\n\t"                  // Restore registers
        "   bx      lr\n\t"
    );
}

#elif defined(__ARMCC_VERSION)
__asm static void
internal_resync_fifoSize(uint32_t wrOffset, uint32_t maxFifoSize, uint32_t hwFifoPtrRegAddr)
{
    push    {r3, r4}                 // Save r3, r4 - used by this function
internal_resync_fifoSize_loop
    ldr     r4, [r2]                 // Load FIFOPTR register in r4
    ubfx    r3, r4, #AM_REG_IOSLAVE_FIFOPTR_FIFOSIZ_S, #8 // Extract hwFifoSize to r3
    uxtb    r4, r4                   // Extract rdOffset in r4
    subs    r4, r0, r4               // fifoSize in r4 = wrOffset - rdOffset
    it      cc                       // if (wrOffset < rdOffset),
    addcc   r4, r4, r1               //     fifoSize = maxFifoSize - (rdOffset - wrOffset)
    cmp     r3, r4                   // (hwFifoSize != fifoSize)
    beq     internal_resync_fifosize_done
    strb    r4, [r2, #1]             // Overwrite FIFOSIZ value with fifoSize
    b       internal_resync_fifoSize_loop // Repeat the check
internal_resync_fifosize_done
    pop     {r3, r4}                 // Restore registers
    bx      lr
}

#elif defined(__IAR_SYSTEMS_ICC__)
#if (AM_REG_IOSLAVE_FIFOPTR_FIFOSIZ_S != 8)
#error "AM_REG_IOSLAVE_FIFOPTR_FIFOSIZ_S not 8"
#endif
__stackless static void
internal_resync_fifoSize(uint32_t wrOffset, uint32_t maxFifoSize, uint32_t hwFifoPtrRegAddr)
{
    __asm volatile (
          "    push    {r3,r4}\n"                  // Save r3, r4 - used by this function
          "__internal_resync_fifoSize_loop:\n"
          "    ldr     r4, [r2]\n"                 // Load FIFOPTR register in r4
          "    ubfx    r3, r4, #8, #8\n"           // Extract hwFifoSize to r3
          "    uxtb    r4, r4\n"                   // Extract rdOffset in r4
          "    subs    r4, r0, r4\n"               // fifoSize in r4 = wrOffset - rdOffset
          "    it      cc\n"
          "    addcc   r4, r4, r1\n"               //     fifoSize = maxFifoSize - (rdOffset - wrOffset)
          "    cmp     r3, r4\n"                   // (fifoSize != hwFifoSize)
          "    beq     __internal_resync_fifosize_done\n"
          "    strb    r4, [r2, #1]\n"             // Overwrite FIFOSIZ value with fifoSize
          "    b       __internal_resync_fifoSize_loop\n" // Repeat the check
          "__internal_resync_fifosize_done:\n"
          "    pop     {r3,r4}\n"                  // Restore registers
          "    bx      lr\n"
          );
}

#else
static void
internal_resync_fifoSize(uint32_t wrOffset, uint32_t maxFifoSize, uint32_t hwFifoPtrRegAddr)
{
    uint32_t fifoSize;
    uint32_t hwFifoPtrReg;
    uint32_t rdOffset;
    uint32_t hwFifoSize;

    hwFifoPtrReg = AM_REGVAL(hwFifoPtrRegAddr);
    rdOffset = ((hwFifoPtrReg & AM_REG_IOSLAVE_FIFOPTR_FIFOPTR_M) >> AM_REG_IOSLAVE_FIFOPTR_FIFOPTR_S);
    hwFifoSize = (hwFifoPtrReg & AM_REG_IOSLAVE_FIFOPTR_FIFOSIZ_M) >> AM_REG_IOSLAVE_FIFOPTR_FIFOSIZ_S;
    // By wasting one byte in hardware FIFO, we're guaranteed that fifoSize does not need special handling for FULL FIFO case
    fifoSize = ((wrOffset >= rdOffset) ? (wrOffset - rdOffset) : (maxFifoSize - (rdOffset - wrOffset)));
    while ( fifoSize != hwFifoSize )
    {
        // Overwite correct FIFOSIZ
        // Need to do a Byte Write to make sure the FIFOPTR is not overwritten
        *((uint8_t *)(hwFifoPtrRegAddr + BYTEOFFSET_FIFOSIZE)) = fifoSize;
        // Read back the register and check for consistency
        hwFifoPtrReg = AM_REGVAL(hwFifoPtrRegAddr);
        rdOffset = ((hwFifoPtrReg & AM_REG_IOSLAVE_FIFOPTR_FIFOPTR_M) >> AM_REG_IOSLAVE_FIFOPTR_FIFOPTR_S);
        hwFifoSize = (hwFifoPtrReg & AM_REG_IOSLAVE_FIFOPTR_FIFOSIZ_M) >> AM_REG_IOSLAVE_FIFOPTR_FIFOSIZ_S;
        // By wasting one byte in hardware FIFO, we're guaranteed that fifoSize does not need special handling for FULL FIFO case
        fifoSize = ((wrOffset >= rdOffset) ? (wrOffset - rdOffset) : (hwFifoSize - (rdOffset - wrOffset)));
    }
}
#endif

//
// Address of the FIFOPTR register
//
#define AM_REG_IOS_FIFOPTR      (REG_IOSLAVE_BASEADDR + AM_REG_IOSLAVE_FIFOPTR_O)

// When the FIFO is being replenished by the SW, at the same time as host is
// reading from it, there is a possible race condition, where the hardware decrement
// of FIFOSIZ as a result of read gets overwritten by hardware increment due to
// write.
// This function re-sync's the FIFOSIZ to ensure such errors do not accumulate
void
resync_fifoSize(void)
{
    uint32_t ui32Primask;
    uint32_t wrOffset = (uint32_t)g_pui8FIFOPtr - (uint32_t)am_hal_ios_pui8LRAM;
    //
    // Start a critical section for thread safety.
    //
    ui32Primask = am_hal_interrupt_master_disable();
    internal_resync_fifoSize(wrOffset, g_ui32HwFifoSize, AM_REG_IOS_FIFOPTR);
    // Clear interrupts for IOS which could be spuriously triggered
    AM_REG(IOSLAVE, REGACCINTCLR) = (AM_HAL_IOS_INT_FSIZE | AM_HAL_IOS_INT_FOVFL | AM_HAL_IOS_INT_FUNDFL);
    //
    // End the critical section
    //
    am_hal_interrupt_master_set(ui32Primask);
    return;
}

//*****************************************************************************
//
//! @brief Transfer any available data from the IOS SRAM buffer to the FIFO.
//!
//! This function is meant to be called from an interrupt handler for the
//! ioslave module. It checks the IOS FIFO interrupt status for a threshold
//! event, and transfers data from an SRAM buffer into the IOS FIFO.
//!
//! @param ui32Status should be set to the ios interrupt status at the time of
//! ISR entry.
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_fifo_service(uint32_t ui32Status)
{
    uint32_t thresh;
    uint32_t freeSpace, usedSpace, chunk1, chunk2, ui32WriteIndex;

    //
    // Check for FIFO size interrupts.
    //
    if ( ui32Status & AM_HAL_IOS_INT_FSIZE )
    {
        thresh = AM_BFR(IOSLAVE, FIFOTHR, FIFOTHR);

        //
        // While the FIFO is at or below threshold Add more data
        // If Fifo level is above threshold, we're guaranteed an FSIZ interrupt
        //
        while ( g_sSRAMBuffer.ui32Length &&
                ((usedSpace = AM_BFR(IOSLAVE, FIFOPTR, FIFOSIZ)) <= thresh) )
        {
            //
            // So, we do have some data in SRAM which needs to be moved to FIFO.
            // A chunk of data is a continguous set of bytes in SRAM that can be
            //  written to FIFO. Determine the chunks of data from SRAM that can
            //  be written. Up to two chunks possible
            //
            ui32WriteIndex = g_sSRAMBuffer.ui32WriteIndex;
            chunk1 = ((ui32WriteIndex > (uint32_t)g_sSRAMBuffer.ui32ReadIndex) ?   \
                        (ui32WriteIndex - (uint32_t)g_sSRAMBuffer.ui32ReadIndex) : \
                        (g_sSRAMBuffer.ui32Capacity - (uint32_t)g_sSRAMBuffer.ui32ReadIndex));
            chunk2 = g_sSRAMBuffer.ui32Length - chunk1;
            // We waste one byte in HW FIFO
            freeSpace = g_ui32HwFifoSize - usedSpace - 1;
            // Write data in chunks
            // Determine the chunks of data from SRAM that can be written
            if ( chunk1 > freeSpace )
            {
                fifo_write((uint8_t *)(g_sSRAMBuffer.pui8Data + g_sSRAMBuffer.ui32ReadIndex), freeSpace);
                //
                // Advance the read index, wrapping if needed.
                //
                g_sSRAMBuffer.ui32ReadIndex += freeSpace;
                // No need to check for wrap as we wrote less than chunk1
                //
                // Adjust the length value to reflect the change.
                //
                g_sSRAMBuffer.ui32Length -= freeSpace;
            }
            else
            {
                fifo_write((uint8_t *)(g_sSRAMBuffer.pui8Data + g_sSRAMBuffer.ui32ReadIndex), chunk1);

                //
                // Update the read index - wrapping as needed
                //
                g_sSRAMBuffer.ui32ReadIndex += chunk1;
                g_sSRAMBuffer.ui32ReadIndex %= g_sSRAMBuffer.ui32Capacity;
                //
                // Adjust the length value to reflect the change.
                //
                g_sSRAMBuffer.ui32Length -= chunk1;
                freeSpace -= chunk1;

                if ( freeSpace && chunk2 )
                {
                    if ( chunk2 > freeSpace )
                    {
                        fifo_write((uint8_t *)(g_sSRAMBuffer.pui8Data + g_sSRAMBuffer.ui32ReadIndex), freeSpace);

                        //
                        // Advance the read index, wrapping if needed.
                        //
                        g_sSRAMBuffer.ui32ReadIndex += freeSpace;

                        // No need to check for wrap in chunk2
                        //
                        // Adjust the length value to reflect the change.
                        //
                        g_sSRAMBuffer.ui32Length -= freeSpace;
                    }
                    else
                    {
                        fifo_write((uint8_t *)(g_sSRAMBuffer.pui8Data + g_sSRAMBuffer.ui32ReadIndex), chunk2);
                        //
                        // Advance the read index, wrapping if needed.
                        //
                        g_sSRAMBuffer.ui32ReadIndex += chunk2;

                        // No need to check for wrap in chunk2
                        //
                        // Adjust the length value to reflect the change.
                        //
                        g_sSRAMBuffer.ui32Length -= chunk2;
                    }
                }
            }
L
lin 已提交
978 979 980 981
            if (!isRevB2())
            {
                resync_fifoSize();
            }
L
lin 已提交
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064

            //
            // Need to retake the FIFO space, after Threshold interrupt has been reenabled
            // Clear any spurious FSIZE interrupt that might have got raised
            //
            AM_BFW(IOSLAVE, INTCLR, FSIZE, 1);
        }
    }
}

//*****************************************************************************
//
//! @brief Writes the specified number of bytes to the IOS fifo.
//!
//! @param pui8Data is a pointer to the data to be written to the fifo.
//! @param ui32NumBytes is the number of bytes to send.
//!
//! This function will write data from the caller-provided array to the IOS
//! LRAM FIFO. If there is no space in the LRAM FIFO, the data will be copied
//! to a temporary SRAM buffer instead.
//!
//! The maximum message size for the IO Slave is 1023 bytes.
//!
//! @note In order for SRAM copy operations in the function to work correctly,
//! the \e am_hal_ios_buffer_service() function must be called in the ISR for
//! the ioslave module.
//!
//! @return Number of bytes written (could be less than ui32NumBytes, if not enough space)
//
//*****************************************************************************
uint32_t
am_hal_ios_fifo_write(uint8_t *pui8Data, uint32_t ui32NumBytes)
{
    uint32_t ui32FIFOSpace;
    uint32_t ui32SRAMSpace;
    uint32_t ui32SRAMLength;
    uint32_t ui32Primask;
    uint32_t totalBytes = ui32NumBytes;

    //
    // This operation will only work properly if an SRAM buffer has been
    // allocated. Make sure that am_hal_ios_fifo_buffer_init() has been called,
    // and the buffer pointer looks valid.
    //
    am_hal_debug_assert(g_sSRAMBuffer.pui8Data != 0);

    if ( ui32NumBytes == 0 )
    {
        return 0;
    }

    //
    // Start a critical section for thread safety.
    //
    ui32Primask = am_hal_interrupt_master_disable();

    ui32SRAMLength = g_sSRAMBuffer.ui32Length;
    //
    // End the critical section
    //
    am_hal_interrupt_master_set(ui32Primask);

    //
    // If the SRAM buffer is empty, we should just write directly to the FIFO.
    //
    if ( ui32SRAMLength == 0 )
    {
        ui32FIFOSpace = fifo_space_left();

        //
        // If the whole message fits, send it now.
        //
        if ( ui32NumBytes <= ui32FIFOSpace )
        {
            fifo_write(pui8Data, ui32NumBytes);
            ui32NumBytes = 0;
        }
        else
        {
            fifo_write(pui8Data, ui32FIFOSpace);
            ui32NumBytes -= ui32FIFOSpace;
            pui8Data += ui32FIFOSpace;
        };
L
lin 已提交
1065 1066 1067 1068
        if (!isRevB2())
        {
            resync_fifoSize();
        }
L
lin 已提交
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
    }

    //
    // If there's still data, write it to the SRAM buffer.
    //
    if ( ui32NumBytes )
    {
        uint32_t idx, writeIdx, capacity, fifoSize;
        ui32SRAMSpace = g_sSRAMBuffer.ui32Capacity - ui32SRAMLength;

        writeIdx = g_sSRAMBuffer.ui32WriteIndex;
        capacity = g_sSRAMBuffer.ui32Capacity;
        //
        // Make sure that the data will fit inside the SRAM buffer.
        //
        if ( ui32SRAMSpace > ui32NumBytes )
        {
            ui32SRAMSpace = ui32NumBytes;
        }

        //
        // If the data will fit, write it to the SRAM buffer.
        //
        for ( idx = 0; idx < ui32SRAMSpace; idx++ )
        {
            g_sSRAMBuffer.pui8Data[(idx + writeIdx) % capacity] = pui8Data[idx];
        }

        ui32NumBytes -= idx;
        //
        // Start a critical section for thread safety before updating length & wrIdx.
        //
        ui32Primask = am_hal_interrupt_master_disable();
        //
        // Advance the write index, making sure to wrap if necessary.
        //
        g_sSRAMBuffer.ui32WriteIndex = (idx + writeIdx) % capacity;

        //
        // Update the length value appropriately.
        //
        g_sSRAMBuffer.ui32Length += idx;
        //
        // End the critical section
        //
        am_hal_interrupt_master_set(ui32Primask);

        // It is possible that there is a race condition that the FIFO level has
        // gone below the threshold by the time we set the wrIdx above, and hence
        // we may never get the threshold interrupt to serve the SRAM data we
        // just wrote

        // If that is the case, explicitly generate the FSIZE interrupt from here
        fifoSize = AM_BFR(IOSLAVE, FIFOPTR, FIFOSIZ);
        if ( fifoSize <= AM_BFR(IOSLAVE, FIFOTHR, FIFOTHR) )
        {
            AM_BFW(IOSLAVE, INTSET, FSIZE, 1);
        }
    }

L
lin 已提交
1129 1130
    // Number of bytes written
    g_sSRAMBuffer.ui32FifoInc += totalBytes - ui32NumBytes;
L
lin 已提交
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
    return (totalBytes - ui32NumBytes);
}

//*****************************************************************************
//
//! @brief Writes the specified number of bytes to the IOS fifo simply.
//!
//! @param pui8Data is a pointer to the data to be written to the fifo.
//! @param ui32NumBytes is the number of bytes to send.
//!
//! This function will write data from the caller-provided array to the IOS
//! LRAM FIFO. This simple routine does not use SRAM buffering for large
L
lin 已提交
1143
//! messages. This function also updates the FIFOCTR.
L
lin 已提交
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
//!
//! The maximum message size for the IO Slave is 128 bytes.
//!
//! @note Do note call the \e am_hal_ios_buffer_service() function in the ISR for
//! the ioslave module.
//!
//! @return
//
//*****************************************************************************
void
am_hal_ios_fifo_write_simple(uint8_t *pui8Data, uint32_t ui32NumBytes)
{
    uint32_t ui32FIFOSpace;

    //
    // Check the FIFO and the SRAM buffer to see where we have space.
    //
    ui32FIFOSpace = fifo_space_left();

    //
    // If the whole message fits, send it now.
    //
    if ( ui32NumBytes <= ui32FIFOSpace )
    {
        fifo_write(pui8Data, ui32NumBytes);
L
lin 已提交
1169 1170
        // Write FIFOINC
        AM_BFW(IOSLAVE, FIFOINC, FIFOINC, ui32NumBytes);
L
lin 已提交
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
    }
    else
    {
        //
        // The message didn't fit. Try using am_hal_ios_fifo_write() instead.
        //
        am_hal_debug_assert_msg(0, "The requested IOS transfer didn't fit in"
                                   "the LRAM FIFO. Try using am_hal_ios_fifo_write().");
    }
}

//*****************************************************************************
//
//! @brief Sets the IOS FIFO pointer to the specified LRAM offset.
//!
//! @param ui32Offset is LRAM offset to set the FIFO pointer to.
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_fifo_ptr_set(uint32_t ui32Offset)
{
    uint32_t ui32Primask;

    //
    // Start a critical section for thread safety.
    //
    ui32Primask = am_hal_interrupt_master_disable();

    //
    // Set the FIFO Update bit.
    //
    AM_REG(IOSLAVE, FUPD) = 0x1;

    //
    // Change the FIFO offset.
    //
    AM_REG(IOSLAVE, FIFOPTR) = ui32Offset;

    //
    // Clear the FIFO update bit.
    //
    AM_REG(IOSLAVE, FUPD) = 0x0;

    //
    // Set the global FIFO-pointer tracking variable.
    //
    g_pui8FIFOPtr = (uint8_t *) (REG_IOSLAVE_BASEADDR + ui32Offset);

    //
    // End the critical section.
    //
    am_hal_interrupt_master_set(ui32Primask);
}

//*****************************************************************************
//
// Initialize an SRAM buffer for use with the IO Slave.
//
//*****************************************************************************
static void
am_hal_ios_buffer_init(am_hal_ios_buffer_t *psBuffer, void *pvArray,
                       uint32_t ui32Bytes)
{
    psBuffer->ui32WriteIndex = 0;
    psBuffer->ui32ReadIndex = 0;
    psBuffer->ui32Length = 0;
    psBuffer->ui32Capacity = ui32Bytes;
L
lin 已提交
1240
    psBuffer->ui32FifoInc = 0;
L
lin 已提交
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
    psBuffer->pui8Data = (uint8_t *)pvArray;
}

//*****************************************************************************
//
//! @brief Poll for all host side read activity to complete.
//!
//! Poll for all host side read activity to complete. Use this before
//! calling am_hal_ios_fifo_write_simple().
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_read_poll_complete(void)
{
    while ( AM_REG(IOSLAVE, FUPD) & AM_REG_IOSLAVE_FUPD_IOREAD_M );
}

//*****************************************************************************
//
//! @brief Initializes an SRAM buffer for the IOS FIFO.
//!
//! @param pui8Buffer is the SRAM buffer that will be used for IOS fifo data.
//! @param ui32BufferSize is the size of the SRAM buffer.
//!
//! This function provides the IOS HAL functions with working memory for
//! managing outgoing IOS FIFO transactions. It needs to be called at least
//! once before am_hal_ios_fifo_write() may be used.
//!
//! The recommended buffer size for the IOS FIFO is 1024 bytes.
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_fifo_buffer_init(uint8_t *pui8Buffer, uint32_t ui32NumBytes)
{
    //
    // Initialize the global SRAM buffer
    // Total size, which is SRAM Buffer plus the hardware FIFO needs to be
    // limited to 1023
    //
    if ( ui32NumBytes > (1023 - g_ui32HwFifoSize + 1) )
    {
        ui32NumBytes = (1023 - g_ui32HwFifoSize + 1);
    }

    am_hal_ios_buffer_init(&g_sSRAMBuffer, pui8Buffer, ui32NumBytes);

    //
    // Clear the FIFO State
    //
    AM_BFW(IOSLAVE, FIFOCTR, FIFOCTR, 0x0);
    AM_BFW(IOSLAVE, FIFOPTR, FIFOSIZ, 0x0);

    am_hal_ios_fifo_ptr_set(g_ui32FifoBaseOffset);
}

//*****************************************************************************
//
//! @brief Update the FIFOCTR to inform host of available data to read.
//!
//! This function allows the application to indicate to HAL when it is safe to
L
lin 已提交
1305 1306
//! update the FIFOCTR. This function needs to be used in conjunction with
//! am_hal_ios_fifo_write(), which itself does not update the FIFOCTR
L
lin 已提交
1307
//!
L
lin 已提交
1308
//! CAUTION:
L
lin 已提交
1309 1310 1311
//! Application needs to implement some sort of
//! synchronization with the host to make sure host is not reading FIFOCTR while
//! it is being updated by the MCU, since the FIFOCTR read over
L
lin 已提交
1312 1313 1314 1315 1316 1317
//! IO is not an atomic operation. Otherwise, some other logic could be implemented
//! by the host to detect and disregard transient values of FIFOCTR (e.g. multiple
//! reads till it gets a stable value).
//! For Pre-B2 parts, it is necessary to have this synchronization guarantee that
//! Host is not doing any READ operation - be it for FIFOCTR or FIFO itself when
//! this call is made, as otherwise the FIFOCTR value may get corrupted.
L
lin 已提交
1318 1319 1320 1321 1322 1323 1324 1325
//!
//!
//! @return None.
//
//*****************************************************************************
void
am_hal_ios_update_fifoctr(void)
{
L
lin 已提交
1326 1327 1328
    // Write FIFOINC
    AM_BFW(IOSLAVE, FIFOINC, FIFOINC, g_sSRAMBuffer.ui32FifoInc);
    g_sSRAMBuffer.ui32FifoInc = 0;
L
lin 已提交
1329 1330 1331 1332 1333 1334 1335 1336 1337
    return;
}

//*****************************************************************************
//
//  End the doxygen group
//! @}
//
//*****************************************************************************