51uwb.cn

 找回密码
 立即注册
查看: 5156|回复: 0
打印 上一主题 下一主题

bitcraze 代码移植到MDK(Keil)

[复制链接]

73

主题

249

帖子

6843

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
6843
跳转到指定楼层
楼主
发表于 2017-12-4 09:03:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
bitcraze  代码是在ubuntu上使用GCC 编译的,国内使用linux 开发stm32 的用户非常有限,大多使用Keil ,少量用户使用IAR

这里我尝试移植到Keil 下,移植注意事项: 使用stm32 cube STM32F07 相关的代码,因为这个库中的hal 层函数与bitcraze最为接近,不要使用单独std lib !不然问题多多!

其它问题汇总:
1  __attribute__((packed))属性
在移植过程中,会遇到如下的一个属性 __attribute__((packed)),这个要替换位__pack,编译器不同,前者适合GCC,后者用于Keil,下面摘录keil 官网
http://www.keil.com/support/man/ ... hr1359124980173.htm
  1. This type attribute is a GNU compiler extension that the ARM compiler supports.
  2. This attribute is equivalent to __packed
复制代码
上面的属性主要是是对齐功能,按照最小对齐方式对齐,例如如果一个结构体里有一个char 和 一个int,正常情况按照4字节对齐,这个结构体需要占用8个字节,而如果使用__packed修饰,按照最小对齐,那么只需要5个字节。 keil 官网有如下一个例子
  1. typedef struct
  2. {
  3.     char a;
  4.     int b;
  5. } S;
  6. #pragma pack(2)
  7. typedef struct
  8. {
  9.     char a;
  10.     int b;
  11. } SP;
  12. S var = { 0x11, 0x44444444 };
  13. SP pvar = { 0x11, 0x44444444 };
复制代码
The layout of S is:
Figure 9-1 Nonpacked structure S
        


The layout of SP is:
Figure 9-2 Packed structure SP
         

2 汇编移植
  1.     __ASM volatile ("movs r3, #0\nldr r3, [r3, #0]\nMSR msp, r3\n" : : : "r3", "sp");
复制代码

3  enumerated type mixed with another type
  查看问题代码:
  1. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, 0);
复制代码
然后看这个函数的定义
  1. void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
复制代码
GPIO_PinState是一个结构体
  1. typedef enum
  2. {
  3.   GPIO_PIN_RESET = 0U,
  4.   GPIO_PIN_SET
  5. }GPIO_PinState;
复制代码
我们现在直接赋值0 / 1 ,KEIL 在编译的时候报warning 属于正常,一个一个替换即可。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

bphero Inc.  

GMT+8, 2024-5-13 07:12 , Processed in 0.009958 second(s), 5 queries , File On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

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