#include "usb_util.h"

void USB_PMAToMemory(void *mem, uint16_t offset, size_t length)
{
    uint8_t *dst = (uint8_t*)mem;
    uint8_t *pma = (uint8_t*)(USB_PMA_ADDR + 2 * offset);

    for(unsigned int i = 0; i < length; i++)
    {
        *dst++ = *pma++;
        if(i & 1)
        {
            pma += 2;
        }
    }
}

void USB_MemoryToPMA(uint16_t offset, const void *mem, size_t length)
{
    // Only words can be copied. Thus, if the length is not even, it has to be
    // incremented to ensure that the last byte is copied. Since the PMA buffer
    // always has even size, this is not a problem.
    if(length & 1)
    {
        length++;
    }

    const uint8_t *src = (const uint8_t*)mem;

    uint16_t *pma = (uint16_t*)(USB_PMA_ADDR + 2 * offset);
    for(unsigned int i = 0; i < length / 2; i++)
    {
        uint16_t tmp = src[2 * i] | (src[2 * i + 1] << 8);
        *pma++ = tmp;
        pma++;
    }
}

