查看: 1833|回复: 1
收起左侧

[FreeRTOS] Using FreeRTOS on RISC-V Microcontrollers

[复制链接]

  离线 

  • TA的每日心情
    奋斗
    2021-1-15 13:53
  • 签到天数: 26 天

    [LV.4]

    发表于 2020-8-16 23:11:28 | 显示全部楼层 |阅读模式

    有人预言,RISC-V或将是继Intel和Arm之后的第三大主流处理器体系。欢迎访问全球首家只专注于RISC-V单片机行业应用的中文网站

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    本帖最后由 新ちゃん 于 2020-8-21 03:42 编辑

    FreeRTOS 10.2.1版本开始,官方加入RISC-V demo,并且增加目录 source/portable/gcc/risc-v .


    正式官宣对risc-v soc的支持,改动部分可以参考以下地址:

    https://www.freertos.org/Using-FreeRTOS-on-RISC-V.html


    实际上对于risc-v soc的使用,很多用户早就可以运行于FreeRTOS上了,之前用的就是在github上搜到非官方的接口文件(port)。


    这次官方应该也是看到多个soc上运行的FreeRTOS,把它们进行了整合发布出来。


    demo目录下共有3个soc,分别是

    三家公司代表了市面上主流的商业ip


    Microchip和Microsemi 合并以后既有FPGA又有board,它这块板子上soc并不清楚使用了什么core,但从risc-v官网上看

    micorchip 开源贡献里使用的都是rocket



    国内芯片技术交流-[FreeRTOS] Using FreeRTOS on RISC-V Microcontrollersrisc-v单片机中文社区(1)


    第二个搜了一下是NXP的RV32M1,用了4个core,2个risc-v的,2个ARM的,RISC-V的core是RI5CY和zero RI5CY.


    ETH Zurich 是瑞士的苏黎世联邦理工, 而Università di Bologna是意大利的博洛尼亚大学


    国内芯片技术交流-[FreeRTOS] Using FreeRTOS on RISC-V Microcontrollersrisc-v单片机中文社区(2) 国内芯片技术交流-[FreeRTOS] Using FreeRTOS on RISC-V Microcontrollersrisc-v单片机中文社区(3)     国内芯片技术交流-[FreeRTOS] Using FreeRTOS on RISC-V Microcontrollersrisc-v单片机中文社区(4)

    第三个就是更具有代表性的risc-v官方公司sifive了,具体的信息并不清楚。


    该文档主要阐述了针对risc-v的6处修改:


    1/2,增加一个针对risc-v CPU的定义宏文件( freertos_risc_v_chip_specific_extensions.h ) ,实际当你使用某risc-v core时一定会有一个类似的定义文件:

    1. #define portasmHAS_CLINT 1
    复制代码

    a Core Local Interrupter (CLINT)   ,表示CPU是否有内部timer interrupt可用于tick;

    1. #define portasmADDITIONAL_CONTEXT_SIZE 0
    复制代码

    CPU有几个额外register当context switch时需要保存;

    1. #ifdef __ASSEMBLY__
    2. .macro portasmSAVE_ADDITIONAL_REGISTERS
    3.         /* No additional registers to save, so this macro does nothing. */
    4.         .endm

    5. /* Restore the additional registers found on the Pulpino. */
    6. .macro portasmRESTORE_ADDITIONAL_REGISTERS
    7.         /* No additional registers to restore, so this macro does nothing. */
    8.         .endm
    9. #endif
    复制代码

    汇编宏用于context switch时save和load这些额外register;

    以及其他针对该CPU的设定,例如:scr1的IPIC register地址和置位宏就可以定义在此

    1. #define                                                                     IPIC_CISV                                        0xBF0
    复制代码

    3,freertosconfig.h增加相关宏:

    如果CLINT=1,Timer地址定义在此,scr1的如下

    1. #define configCLINT_BASE_ADDRESS                0x490000
    复制代码

    4,修改了interrupt的stack使用,之前ISR使用的是task stack作为临时stack空间,新版本将独立出一个xISRStackTop空间。

    两种方法定义该空间,一是通过编译器分配数组的方式,二是ld里定义对应section symbol再赋值

    1. /* The stack used by interrupt service routines.  Set configISR_STACK_SIZE_WORDS
    2. to use a statically allocated array as the interrupt stack.  Alternative leave
    3. configISR_STACK_SIZE_WORDS undefined and update the linker script so that a
    4. linker variable names __freertos_irq_stack_top has the same value as the top
    5. of the stack used by main.  Using the linker script method will repurpose the
    6. stack that was used by main before the scheduler was started for use as the
    7. interrupt stack after the scheduler has started. */
    8. #ifdef configISR_STACK_SIZE_WORDS
    9.         static __attribute__ ((aligned(16))) StackType_t xISRStack[ configISR_STACK_SIZE_WORDS ] = { 0 };
    10.         const StackType_t xISRStackTop = ( StackType_t ) &( xISRStack[ ( configISR_STACK_SIZE_WORDS & ~portBYTE_ALIGNMENT_MASK ) - 1 ] );
    11. #else
    12.         extern const uint32_t __freertos_irq_stack_top[];
    13.         const StackType_t xISRStackTop = ( StackType_t ) __freertos_irq_stack_top;
    14. #endif
    复制代码

    5,定义 宏portasmHANDLE_INTERRUPT作为ISR外部中断的入口。

    在官方的portASM.S中采用了非vector mode 的方式使用MTVEC register,而我自己使用的是scr1 的vector mode,所以并没有去使用这个宏 portasmHANDLE_INTERRUPT。


    6,freertos_risc_v_trap_handler 标签标识exception/interrupt的入口地址,该地址要写入MTVEC中。

    对于scr1 vector mode,该地址是vector table的起始地址,每个exception和interrupt 都根据mcause * 4来作偏移。

    其中,中断包括software interrupt/timer interrupt /external interrupt,所有external interrupt共享一个入口地址,具体是哪个external interrupt要根据IPIC来判断。而ECALL指令产生的interrupt会使用exception 偏移地址11。所以相当于有两个software interrupt可用,一个被用于FreeRTOS 中 portYIELD()宏。

    1. Table 12: List of MCAUSE Exception Codes
    2. INT   EC Description
    3. 0     0 Instruction address misaligned
    4. 0     1 Instruction access fault
    5. 0     2 Illegal instruction
    6. 0     3 Breakpoint
    7. 0     4 Load address misaligned
    8. 0     5 Load access fault
    9. 0     6 Store/AMO address misaligned
    10. 0     7 Store/AMO access fault
    11. 0     10..8 Not supported
    12. 0     11 Ecall from M-mode
    13. 0     >=12 Reserved

    14. 1     2..0 Reserved
    15. 1     3 Machine Software Interrupt
    16. 1     6..4 Reserved
    17. 1     7 Machine Timer Interrupt
    18. 1     10..8 Reserved
    19. 1     11 Machine External Interrupt
    20. 1     >=12 Reserved
    复制代码




    上一篇:Cygwin综合实现RISC-V出错
    下一篇:RISC-V蜂鸟项目开发
    RISCV作者优文
    全球首家只专注于RISC-V单片机行业应用的中文网站
    回复

    使用道具 举报

      离线 

  • TA的每日心情
    奋斗
    2021-1-15 13:53
  • 签到天数: 26 天

    [LV.4]

     楼主| 发表于 2020-8-16 23:14:01 | 显示全部楼层
    本帖最后由 新ちゃん 于 2020-8-21 03:43 编辑

    官宣里定义mstauts要保存在context switch中,用ecall呼叫实现portYIELD()。和之前用的function方式实现portYIELD()相比开销较多,自测会多花费30%的时间在context switch中。主要花费在CPU要跳转到freertos_risc_v_trap_handler并且 需要一次判断是非为ecall指令,再指向ISR stack空间。而保存进stack的数量几乎相当的。
    1. /*
    2. * Unlike other ports pxPortInitialiseStack() is written in assembly code as it
    3. * needs access to the portasmADDITIONAL_CONTEXT_SIZE constant.  The prototype
    4. * for the function is as per the other ports:
    5. * StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters );
    6. *
    7. * As per the standard RISC-V ABI pxTopcOfStack is passed in in a0, pxCode in
    8. * a1, and pvParameters in a2.  The new top of stack is passed out in a0.
    9. *
    10. * RISC-V maps registers to ABI names as follows (X1 to X31 integer registers
    11. * for the 'I' profile, X1 to X15 for the 'E' profile, currently I assumed).
    12. *
    13. * Register                ABI Name        Description                                                Saver
    14. * x0                        zero                Hard-wired zero                                        -
    15. * x1                        ra                        Return address                                        Caller
    16. * x2                        sp                        Stack pointer                                        Callee
    17. * x3                        gp                        Global pointer                                        -
    18. * x4                        tp                        Thread pointer                                        -
    19. * x5-7                        t0-2                Temporaries                                                Caller
    20. * x8                        s0/fp                Saved register/Frame pointer        Callee
    21. * x9                        s1                        Saved register                                        Callee
    22. * x10-11                a0-1                Function Arguments/return values Caller
    23. * x12-17                a2-7                Function arguments                                Caller
    24. * x18-27                s2-11                Saved registers                                        Callee
    25. * x28-31                t3-6                Temporaries                                                Caller
    26. *
    27. * The RISC-V context is saved t FreeRTOS tasks in the following stack frame,
    28. * where the global and thread pointers are currently assumed to be constant so
    29. * are not saved:
    30. *
    31. * mstatus
    32. * x31
    33. * x30
    34. * x29
    35. * x28
    36. * x27
    37. * x26
    38. * x25
    39. * x24
    40. * x23
    41. * x22
    42. * x21
    43. * x20
    44. * x19
    45. * x18
    46. * x17
    47. * x16
    48. * x15
    49. * x14
    50. * x13
    51. * x12
    52. * x11
    53. * pvParameters
    54. * x9
    55. * x8
    56. * x7
    57. * x6
    58. * x5
    59. * portTASK_RETURN_ADDRESS
    60. * [chip specific registers go here]
    61. * pxCode
    62. */
    复制代码

    本篇完
    全球首家只专注于RISC-V单片机行业应用的中文网站
    高级模式
    B Color Image Link Quote Code Smilies

    本版积分规则

    关闭

    RISC-V单片机中文网上一条 /2 下一条



    版权及免责声明|RISC-V单片机中文网 |网站地图

    GMT+8, 2024-4-20 05:48 , Processed in 0.818729 second(s), 51 queries .

    快速回复 返回顶部 返回列表