From dddd4e4c3c9523c67110ad2edbb73c3333780186 Mon Sep 17 00:00:00 2001 From: sakumisu <1203593632@qq.com> Date: Wed, 23 Feb 2022 17:48:47 +0800 Subject: [PATCH] gdd memory align32 for variable in structure --- class/cdc/usbd_cdc.c | 2 +- class/msc/usbd_msc.c | 22 +++---------- common/usb_mem.h | 73 ++++++++++++++++++++++++++------------------ core/usbd_core.c | 26 ++++++++-------- 4 files changed, 63 insertions(+), 60 deletions(-) diff --git a/class/cdc/usbd_cdc.c b/class/cdc/usbd_cdc.c index 707e6ac0..11d709da 100644 --- a/class/cdc/usbd_cdc.c +++ b/class/cdc/usbd_cdc.c @@ -29,7 +29,7 @@ const char *parity_name[] = { "N", "O", "E", "M", "S" }; /* Device data structure */ struct cdc_acm_cfg_priv { /* 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 */ uint8_t line_state; /* CDC ACM serial state bitmap, DCE side */ diff --git a/class/msc/usbd_msc.c b/class/msc/usbd_msc.c index 57781e4b..0da261d7 100644 --- a/class/msc/usbd_msc.c +++ b/class/msc/usbd_msc.c @@ -50,8 +50,8 @@ enum Stage { struct usbd_msc_cfg_priv { /* state of the bulk-only state machine */ enum Stage stage; - struct CBW cbw; - struct CSW csw; + USB_MEM_ALIGN32 struct CBW cbw; + USB_MEM_ALIGN32 struct CSW csw; uint8_t sKey; /* Sense key */ uint8_t ASC; /* Additional Sense Code */ @@ -71,24 +71,12 @@ static bool memOK; static void usbd_msc_reset(void) { - usbd_msc_cfg.stage = MSC_READ_CBW; - 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)); - + memset((uint8_t *)&usbd_msc_cfg, 0, sizeof(struct usbd_msc_cfg_priv)); usbd_msc_get_cap(0, &usbd_msc_cfg.scsi_blk_nbr, &usbd_msc_cfg.scsi_blk_size); - if (usbd_msc_cfg.block_buffer) { - free(usbd_msc_cfg.block_buffer); + if (usbd_msc_cfg.block_buffer == NULL) { + 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)); } /** diff --git a/common/usb_mem.h b/common/usb_mem.h index 7f518b0c..9d9a111d 100644 --- a/common/usb_mem.h +++ b/common/usb_mem.h @@ -23,53 +23,68 @@ #ifndef _USB_MEM_H #define _USB_MEM_H -#define DCACHE_LINE_SIZE 32 -#define DCACHE_LINEMASK (DCACHE_LINE_SIZE - 1) +#ifndef CONFIG_DCACHE_LINE_SIZE +#define CONFIG_DCACHE_LINE_SIZE 32 +#endif #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_MEM_ALIGN32 __attribute__((aligned(CONFIG_DCACHE_LINE_SIZE))) #else -#define USB_MEM_ALIGN32 __attribute__((aligned(DCACHE_LINE_SIZE))) #define USB_NOCACHE_RAM_SECTION -#endif -#else #define USB_MEM_ALIGN32 -#define USB_NOCACHE_RAM_SECTION #endif -static inline void *usb_malloc(size_t size) -{ - return malloc(size); -} - -static inline void usb_free(void *ptr) -{ - free(ptr); -} +#define usb_malloc(size) malloc(size) +#define usb_free(ptr) free(ptr) #ifdef CONFIG_USB_DCACHE_ENABLE static inline void *usb_iomalloc(size_t size) { - size = (size + DCACHE_LINEMASK) & ~DCACHE_LINEMASK; - return malloc(size); -} + void *ptr; + void *align_ptr; + int uintptr_size; + size_t align_size; + uint32_t align = CONFIG_DCACHE_LINE_SIZE; -static inline void usb_iofree(void *addr) -{ - free(addr); -} -#else -static inline void *usb_iomalloc(size_t size) -{ - return malloc(size); + /* sizeof pointer */ + uintptr_size = sizeof(void *); + uintptr_size -= 1; + + /* align the alignment size to uintptr size byte */ + align = ((align + uintptr_size) & ~uintptr_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) { - 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 \ No newline at end of file diff --git a/core/usbd_core.c b/core/usbd_core.c index ce4e4abc..2e2a6cc8 100644 --- a/core/usbd_core.c +++ b/core/usbd_core.c @@ -43,9 +43,9 @@ #define USB_EP_OUT_NUM 8 #define USB_EP_IN_NUM 8 -static struct usbd_core_cfg_priv { +struct usbd_core_cfg_priv { /** Setup packet */ - struct usb_setup_packet setup; + USB_MEM_ALIGN32 struct usb_setup_packet setup; /** Pointer to data buffer */ uint8_t *ep0_data_buf; /** Remaining bytes in buffer */ @@ -57,7 +57,7 @@ static struct usbd_core_cfg_priv { /** Pointer to registered descriptors */ const uint8_t *descriptors; /* 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 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) { USB_LOG_INFO("Setup: " - "bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\r\n", - setup->bmRequestType, - setup->bRequest, - setup->wValue, - setup->wIndex, - setup->wLength); + "bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\r\n", + setup->bmRequestType, + setup->bRequest, + setup->wValue, + setup->wIndex, + 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; 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_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; 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); @@ -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", - cur_iface, cur_alt_setting); + cur_iface, cur_alt_setting); break; 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 * 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*/ usbd_core_cfg.zlp_flag = true; }