other_chip.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. 芯片通用移植指南
  2. =========================
  3. 本节主要介绍所有带 USB IP 的芯片,移植 CherryUSB 主从协议栈时的通用步骤和注意事项。在往下看之前,需要 **你准备好一个可以打印 helloworld 的基本工程** ,并且实现了 `printf` 、 `malloc`、 `free`。如果是主机,需要 **准备好可以打印 helloworld 的带 OS 的工程**。通常来说, `printf` 大家都会实现,
  4. `malloc`、 `free` 主要给主机使用, 默认 config 文件使用的就是 `malloc`、 `free`,推荐修改为 os 相关接口, 不推荐使用系统库,尤其是 keil 自带的这种。举例如下:
  5. .. code-block:: C
  6. // default config
  7. #define usb_malloc(size) malloc(size)
  8. #define usb_free(ptr) free(ptr)
  9. // freertos
  10. #define usb_malloc(size) pvPortMalloc(size)
  11. #define usb_free(ptr) vPortFree(ptr)
  12. //rtthread
  13. #define usb_malloc(size) rt_malloc(size)
  14. #define usb_free(ptr) rt_free(ptr)
  15. USB Device 移植要点
  16. -----------------------
  17. - 拷贝 CherryUSB 源码到工程目录下,并按需添加源文件和头文件路径,其中 `usbd_core.c` 和 `usb_dc_xxx.c` 为必须添加项。而 `usb_dc_xxx.c` 是芯片所对应的 USB IP dcd 部分驱动,如果不知道自己芯片属于那个 USB IP,参考 **port** 目录下的不同 USB IP 的 readme。如果使用的 USB IP 没有支持,只能自己实现了
  18. - 拷贝 `cherryusb_config_template.h` 文件到自己工程目录下,命名为 `usb_config.h`,并添加相应的目录头文件路径
  19. - 实现 `usb_dc_low_level_init` 函数(该函数主要负责 USB 时钟、引脚、中断的初始化)。该函数可以放在你想要放的任何参与编译的 c 文件中。如何进行 USB 的时钟、引脚、中断等初始化,请自行根据你使用的芯片原厂提供的源码中进行添加。
  20. - 描述符的注册、class的注册、接口的注册、端点中断的注册。不会的参考 demo 下的 template
  21. - 调用 `usbd_initialize` 并填入 `busid` 和 USB IP 的 `reg base`, `busid` 从 0 开始,不能超过 `CONFIG_USBDEV_MAX_BUS`
  22. - 在中断函数中调用 `USBD_IRQHandler`,并传入 `busid`, 如果你的 SDK 中中断入口已经存在 `USBD_IRQHandler` ,请更改 USB 协议栈中的名称
  23. - 编译使用。各个 class 如何使用,参考 demo 下的 template
  24. USB Host 移植要点
  25. -----------------------
  26. - 拷贝 CherryUSB 源码到工程目录下,并按需添加源文件和头文件路径,其中 `usbh_core.c` 、 `usb_hc_xxx.c` 以及 **osal** 目录下源文件(根据不同的 os 选择对应的源文件)为必须添加项。而 `usb_hc_xxx.c` 是芯片所对应的 USB IP dcd 部分驱动,如果不知道自己芯片属于那个 USB IP,参考 **port** 目录下的不同 USB IP 的 readme。如果使用的 USB IP 没有支持,只能自己实现了
  27. - 拷贝 `cherryusb_config_template.h` 文件到自己工程目录下,命名为 `usb_config.h`,并添加相应的目录头文件路径
  28. - 实现 `usb_hc_low_level_init` 函数(该函数主要负责 USB 时钟、引脚、中断的初始化)。该函数可以放在你想要放的任何参与编译的 c 文件中。如何进行 USB 的时钟、引脚、中断等初始化,请自行根据你使用的芯片原厂提供的源码中进行添加。
  29. - 调用 `usbh_initialize` 并填入 `busid` 和 USB IP 的 `reg base`, `busid` 从 0 开始,不能超过 `CONFIG_USBHOST_MAX_BUS`
  30. - 在中断函数中调用 `USBH_IRQHandler`,并传入 `busid`, 如果你的 SDK 中中断入口已经存在 `USBH_IRQHandler` ,请更改 USB 协议栈中的名称
  31. - 如果使用的是 GCC ,需要在链接脚本中添加如下代码(需要放在 flash 位置):
  32. .. code-block:: C
  33. // 在 ld 文件中添加如下代码
  34. . = ALIGN(4);
  35. __usbh_class_info_start__ = .;
  36. KEEP(*(.usbh_class_info))
  37. __usbh_class_info_end__ = .;
  38. GCC 举例如下:
  39. .. code-block:: C
  40. /* The program code and other data into "FLASH" Rom type memory */
  41. .text :
  42. {
  43. . = ALIGN(4);
  44. *(.text) /* .text sections (code) */
  45. *(.text*) /* .text* sections (code) */
  46. *(.glue_7) /* glue arm to thumb code */
  47. *(.glue_7t) /* glue thumb to arm code */
  48. *(.eh_frame)
  49. KEEP (*(.init))
  50. KEEP (*(.fini))
  51. . = ALIGN(4);
  52. __usbh_class_info_start__ = .;
  53. KEEP(*(.usbh_class_info))
  54. __usbh_class_info_end__ = .;
  55. . = ALIGN(4);
  56. _etext = .; /* define a global symbols at end of code */
  57. } > FLASH
  58. - Segger Embedded Studio 举例如下:
  59. .. code-block:: C
  60. define block cherryusb_usbh_class_info { section .usbh_class_info };
  61. define exported symbol __usbh_class_info_start__ = start of block cherryusb_usbh_class_info;
  62. define exported symbol __usbh_class_info_end__ = end of block cherryusb_usbh_class_info + 1;
  63. place in AXI_SRAM { block cherryusb_usbh_class_info };
  64. keep { section .usbh_class_info};
  65. - 编译使用。各个 class 如何使用,参考 demo 下的 `usb_host.c` 文件
  66. 带 cache 功能的芯片使用注意
  67. -------------------------------
  68. 协议栈以及 port 中不会对 cache 区域的 ram 进行 clean 或者 invalid,所以需要使用一块非 cache 区域的 ram 来维护。 `USB_NOCACHE_RAM_SECTION` 宏表示将变量指定到非 cache ram上,
  69. 因此,用户需要在对应的链接脚本中添加 no cache ram 的 section。默认 `USB_NOCACHE_RAM_SECTION` 定义为 `__attribute__((section(".noncacheable")))`。
  70. GCC:
  71. .. code-block:: C
  72. MEMORY
  73. {
  74. RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 256K - 64K
  75. RAM_nocache (xrw) : ORIGIN = 0x20030000, LENGTH = 64K
  76. FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 512K
  77. }
  78. ._nocache_ram :
  79. {
  80. . = ALIGN(4);
  81. *(.noncacheable)
  82. } >RAM_nocache
  83. SCT:
  84. .. code-block:: C
  85. LR_IROM1 0x08000000 0x00200000 { ; load region size_region
  86. ER_IROM1 0x08000000 0x00200000 { ; load address = execution address
  87. *.o (RESET, +First)
  88. *(InRoot$$Sections)
  89. .ANY (+RO)
  90. .ANY (+XO)
  91. }
  92. RW_IRAM2 0x24000000 0x00070000 { ; RW data
  93. .ANY (+RW +ZI)
  94. }
  95. USB_NOCACHERAM 0x24070000 0x00010000 { ; RW data
  96. *(.noncacheable)
  97. }
  98. }
  99. ICF:
  100. .. code-block:: C
  101. define region NONCACHEABLE_RAM = [from 0x1140000 size 256K];
  102. place in NONCACHEABLE_RAM { section .noncacheable, section .noncacheable.init, section .noncacheable.bss }; // Noncacheable