gdd memory align32 for variable in structure

This commit is contained in:
sakumisu
2022-02-23 17:48:47 +08:00
parent c053a656bd
commit dddd4e4c3c
4 changed files with 63 additions and 60 deletions

View File

@@ -29,7 +29,7 @@ const char *parity_name[] = { "N", "O", "E", "M", "S" };
/* Device data structure */ /* Device data structure */
struct cdc_acm_cfg_priv { struct cdc_acm_cfg_priv {
/* CDC ACM line coding properties. LE order */ /* CDC ACM line coding properties. LE order */
struct cdc_line_coding line_coding; USB_MEM_ALIGN32 struct cdc_line_coding line_coding;
/* CDC ACM line state bitmap, DTE side */ /* CDC ACM line state bitmap, DTE side */
uint8_t line_state; uint8_t line_state;
/* CDC ACM serial state bitmap, DCE side */ /* CDC ACM serial state bitmap, DCE side */

View File

@@ -50,8 +50,8 @@ enum Stage {
struct usbd_msc_cfg_priv { struct usbd_msc_cfg_priv {
/* state of the bulk-only state machine */ /* state of the bulk-only state machine */
enum Stage stage; enum Stage stage;
struct CBW cbw; USB_MEM_ALIGN32 struct CBW cbw;
struct CSW csw; USB_MEM_ALIGN32 struct CSW csw;
uint8_t sKey; /* Sense key */ uint8_t sKey; /* Sense key */
uint8_t ASC; /* Additional Sense Code */ uint8_t ASC; /* Additional Sense Code */
@@ -71,24 +71,12 @@ static bool memOK;
static void usbd_msc_reset(void) static void usbd_msc_reset(void)
{ {
usbd_msc_cfg.stage = MSC_READ_CBW; memset((uint8_t *)&usbd_msc_cfg, 0, sizeof(struct usbd_msc_cfg_priv));
usbd_msc_cfg.scsi_blk_addr = 0U;
usbd_msc_cfg.scsi_blk_len = 0U;
usbd_msc_cfg.max_lun = 0;
usbd_msc_cfg.sKey = 0;
usbd_msc_cfg.ASC = 0;
usbd_msc_cfg.ASQ = 0;
(void)memset((void *)&usbd_msc_cfg.cbw, 0, sizeof(struct CBW));
(void)memset((void *)&usbd_msc_cfg.csw, 0, sizeof(struct CSW));
usbd_msc_get_cap(0, &usbd_msc_cfg.scsi_blk_nbr, &usbd_msc_cfg.scsi_blk_size); usbd_msc_get_cap(0, &usbd_msc_cfg.scsi_blk_nbr, &usbd_msc_cfg.scsi_blk_size);
if (usbd_msc_cfg.block_buffer) { if (usbd_msc_cfg.block_buffer == NULL) {
free(usbd_msc_cfg.block_buffer); usbd_msc_cfg.block_buffer = usb_iomalloc(usbd_msc_cfg.scsi_blk_size * sizeof(uint8_t));
} }
usbd_msc_cfg.block_buffer = malloc(usbd_msc_cfg.scsi_blk_size * sizeof(uint8_t));
memset(usbd_msc_cfg.block_buffer, 0, usbd_msc_cfg.scsi_blk_size * sizeof(uint8_t));
} }
/** /**

View File

@@ -23,53 +23,68 @@
#ifndef _USB_MEM_H #ifndef _USB_MEM_H
#define _USB_MEM_H #define _USB_MEM_H
#define DCACHE_LINE_SIZE 32 #ifndef CONFIG_DCACHE_LINE_SIZE
#define DCACHE_LINEMASK (DCACHE_LINE_SIZE - 1) #define CONFIG_DCACHE_LINE_SIZE 32
#endif
#ifdef CONFIG_USB_DCACHE_ENABLE #ifdef CONFIG_USB_DCACHE_ENABLE
#ifdef CONFIG_USB_NOCACHE_RAM
#define USB_MEM_ALIGN32
#define USB_NOCACHE_RAM_SECTION __attribute__((section(".nocache_ram"))) #define USB_NOCACHE_RAM_SECTION __attribute__((section(".nocache_ram")))
#define USB_MEM_ALIGN32 __attribute__((aligned(CONFIG_DCACHE_LINE_SIZE)))
#else #else
#define USB_MEM_ALIGN32 __attribute__((aligned(DCACHE_LINE_SIZE)))
#define USB_NOCACHE_RAM_SECTION #define USB_NOCACHE_RAM_SECTION
#endif
#else
#define USB_MEM_ALIGN32 #define USB_MEM_ALIGN32
#define USB_NOCACHE_RAM_SECTION
#endif #endif
static inline void *usb_malloc(size_t size) #define usb_malloc(size) malloc(size)
{ #define usb_free(ptr) free(ptr)
return malloc(size);
}
static inline void usb_free(void *ptr)
{
free(ptr);
}
#ifdef CONFIG_USB_DCACHE_ENABLE #ifdef CONFIG_USB_DCACHE_ENABLE
static inline void *usb_iomalloc(size_t size) static inline void *usb_iomalloc(size_t size)
{ {
size = (size + DCACHE_LINEMASK) & ~DCACHE_LINEMASK; void *ptr;
return malloc(size); void *align_ptr;
} int uintptr_size;
size_t align_size;
uint32_t align = CONFIG_DCACHE_LINE_SIZE;
static inline void usb_iofree(void *addr) /* sizeof pointer */
{ uintptr_size = sizeof(void *);
free(addr); uintptr_size -= 1;
}
#else /* align the alignment size to uintptr size byte */
static inline void *usb_iomalloc(size_t size) align = ((align + uintptr_size) & ~uintptr_size);
{
return malloc(size); /* get total aligned size */
align_size = ((size + uintptr_size) & ~uintptr_size) + align;
/* allocate memory block from heap */
ptr = usb_malloc(align_size);
if (ptr != NULL) {
/* the allocated memory block is aligned */
if (((uint32_t)ptr & (align - 1)) == 0) {
align_ptr = (void *)((uint32_t)ptr + align);
} else {
align_ptr = (void *)(((uint32_t)ptr + (align - 1)) & ~(align - 1));
}
/* set the pointer before alignment pointer to the real pointer */
*((uint32_t *)((uint32_t)align_ptr - sizeof(void *))) = (uint32_t)ptr;
ptr = align_ptr;
}
return ptr;
} }
static inline void usb_iofree(void *ptr) static inline void usb_iofree(void *ptr)
{ {
free(ptr); void *real_ptr;
real_ptr = (void *)*(uint32_t *)((uint32_t)ptr - sizeof(void *));
usb_free(real_ptr);
} }
#else
#define usb_iomalloc(size) usb_malloc(size)
#define usb_iofree(ptr) usb_free(ptr)
#endif #endif
#endif #endif

View File

@@ -43,9 +43,9 @@
#define USB_EP_OUT_NUM 8 #define USB_EP_OUT_NUM 8
#define USB_EP_IN_NUM 8 #define USB_EP_IN_NUM 8
static struct usbd_core_cfg_priv { struct usbd_core_cfg_priv {
/** Setup packet */ /** Setup packet */
struct usb_setup_packet setup; USB_MEM_ALIGN32 struct usb_setup_packet setup;
/** Pointer to data buffer */ /** Pointer to data buffer */
uint8_t *ep0_data_buf; uint8_t *ep0_data_buf;
/** Remaining bytes in buffer */ /** Remaining bytes in buffer */
@@ -57,7 +57,7 @@ static struct usbd_core_cfg_priv {
/** Pointer to registered descriptors */ /** Pointer to registered descriptors */
const uint8_t *descriptors; const uint8_t *descriptors;
/* Buffer used for storing standard, class and vendor request data */ /* Buffer used for storing standard, class and vendor request data */
uint8_t req_data[USB_REQUEST_BUFFER_SIZE]; USB_MEM_ALIGN32 uint8_t req_data[USB_REQUEST_BUFFER_SIZE];
#if USBD_EP_CALLBACK_SEARCH_METHOD == USBD_EP_CALLBACK_ARR_SEARCH #if USBD_EP_CALLBACK_SEARCH_METHOD == USBD_EP_CALLBACK_ARR_SEARCH
usbd_endpoint_callback in_ep_cb[USB_EP_IN_NUM]; usbd_endpoint_callback in_ep_cb[USB_EP_IN_NUM];
@@ -86,12 +86,12 @@ static struct usb_bos_descriptor *bos_desc;
static void usbd_print_setup(struct usb_setup_packet *setup) static void usbd_print_setup(struct usb_setup_packet *setup)
{ {
USB_LOG_INFO("Setup: " USB_LOG_INFO("Setup: "
"bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\r\n", "bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\r\n",
setup->bmRequestType, setup->bmRequestType,
setup->bRequest, setup->bRequest,
setup->wValue, setup->wValue,
setup->wIndex, setup->wIndex,
setup->wLength); setup->wLength);
} }
/** /**
@@ -200,7 +200,7 @@ static bool usbd_set_endpoint(const struct usb_endpoint_descriptor *ep_desc)
ep_cfg.ep_type = ep_desc->bmAttributes & USBD_EP_TYPE_MASK; ep_cfg.ep_type = ep_desc->bmAttributes & USBD_EP_TYPE_MASK;
USB_LOG_INFO("Open endpoint:0x%x type:%u mps:%u\r\n", USB_LOG_INFO("Open endpoint:0x%x type:%u mps:%u\r\n",
ep_cfg.ep_addr, ep_cfg.ep_type, ep_cfg.ep_mps); ep_cfg.ep_addr, ep_cfg.ep_type, ep_cfg.ep_mps);
usbd_ep_open(&ep_cfg); usbd_ep_open(&ep_cfg);
usbd_core_cfg.configured = true; usbd_core_cfg.configured = true;
@@ -226,7 +226,7 @@ static bool usbd_reset_endpoint(const struct usb_endpoint_descriptor *ep_desc)
ep_cfg.ep_type = ep_desc->bmAttributes & USBD_EP_TYPE_MASK; ep_cfg.ep_type = ep_desc->bmAttributes & USBD_EP_TYPE_MASK;
USB_LOG_INFO("Close endpoint:0x%x type:%u\r\n", USB_LOG_INFO("Close endpoint:0x%x type:%u\r\n",
ep_cfg.ep_addr, ep_cfg.ep_type); ep_cfg.ep_addr, ep_cfg.ep_type);
usbd_ep_close(ep_cfg.ep_addr); usbd_ep_close(ep_cfg.ep_addr);
@@ -429,7 +429,7 @@ static bool usbd_set_interface(uint8_t iface, uint8_t alt_setting)
} }
USB_LOG_DBG("Current iface %u alt setting %u", USB_LOG_DBG("Current iface %u alt setting %u",
cur_iface, cur_alt_setting); cur_iface, cur_alt_setting);
break; break;
case USB_DESCRIPTOR_TYPE_ENDPOINT: case USB_DESCRIPTOR_TYPE_ENDPOINT:
@@ -982,7 +982,7 @@ static void usbd_send_to_host(uint16_t len)
* last chunk is wMaxPacketSize long, to indicate the last * last chunk is wMaxPacketSize long, to indicate the last
* packet. * packet.
*/ */
if(!usbd_core_cfg.ep0_data_buf_residue && (len > usbd_core_cfg.ep0_data_buf_len) && !(usbd_core_cfg.ep0_data_buf_len % USB_CTRL_EP_MPS)){ if (!usbd_core_cfg.ep0_data_buf_residue && (len > usbd_core_cfg.ep0_data_buf_len) && !(usbd_core_cfg.ep0_data_buf_len % USB_CTRL_EP_MPS)) {
/* Transfers a zero-length packet next*/ /* Transfers a zero-length packet next*/
usbd_core_cfg.zlp_flag = true; usbd_core_cfg.zlp_flag = true;
} }