我使用的是NXP的cortex-m3芯片LPC1768作为主控芯片,经过研究反复研究和实验,修改startup.s代码中的stack_size和heap_size的大小都无济于事,任然出现奇怪的现象,即在单步调试的时候,只能执行一次,并且这次得到的结果是正确的,然后就立即出现硬件错误,进入汇编语言的硬件错误死循环;如果全速运行,那么每次得到的结果都是0.00000,没有一次能得到正确结果,但是并没有死机,感觉应该是该sprintf %f的功能不正确而已,也不至于死机,不过偶尔确实有错误数据返回,但并没有什么规律。
基于此,在网上找了好久,终于弄明白,应该是这类微控制器不支持sprintf %f,因为这个操作太耗时,耗资源,不值得,很多微控制器都无法满足它的要求,故arm-gcc没有提供该支持或者支持得很不好,不过它也给出了凑合的解决办法,比较简单,模拟效果实现的。实现代码参考如下:
When using GCC compiler, due to internal standard C library architecture, it is strongly not recommended to use the "%f" mode in the wm_sprintf function in order to convert a float variable to a string. This leads to an ARM exception (product reset).
float float_num; uint8_t str_temp[128]; sprintf(str_temp,"%d.%03d",(uint32_t)float_num,(uint32_t)((float_num * 1000) - (uint32_t)(float_num * 1000)));//(实现三位小数转换)
与大家一起分享!
1、使用内核模块参数有两个步骤:
(1)、第一步:第一个变量来存储内核模块传过来的参数值
(2)、第二步:声明模块参数module_param(),写在加载函数的前面
2、内核模块参数的作用:控制模块不同的行为(例如控制一个驱动为串口还是并口驱动程序)
3、例子说明:#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
static char *name = "David Xie";
static int age = 30;
module_param(age, int, S_IRUGO);
module_param(name, charp, S_IRUGO);
static int hello_init(void)
{
printk(KERN_EMERG" Name:%s\n",name);
printk(KERN_EMERG" Age:%d\n",age);
return 0;
}
static void hello_exit(void)
{
printk(KERN_INFO" Module Exit\n ");
}
module_init(hello_init);
module_exit(hello_exit);
// insmod param.ko 当不跟参数的时候,Age默认值是30,所以输出30
// insmod param.ko Age=40 当指定参数为40,Age输出的是指定传过来的参数
五、带有初始化的基本类型
建议不要使用内置类型,而是使用类
比如:
不要使用int,而是使用Int
不要使用unsigned,该用Unsigned
.....
这样就不会出现所谓的垃圾值
scpp_types.h:
#ifndef __SCCP_TYPES_H__ #define __SCCP_TYPES_H__ #include <ostream> #include "scpp_assert.h" template <typename T> class TNumber { public: TNumber(const T& x =0 ) :data_(x) { } operator T() const { return data_; } TNumber &operator = (const T& x) { data_ = x; return *this; } TNumber operator ++(int) { TNumber<T> copy(*this); ++data_; return copy; } TNumber operator ++() { ++data_; return *this; } TNumber& operator += (T x) { data_ += x; return *this; } TNumber& operator -= (T x) { data_ -= x; return *this; } TNumber& operator *= (T x) { data_ *= x; return *this; } TNumber& operator /= (T x) { SCPP_ASSERT(x != 0, "Attempt to divide by 0"); data_ /= x; return *this; } T operator / (T x) { SCPP_ASSERT(x != 0, "Attempt to divide by 0"); return data_ / x; } private: T data_; }; typedef long long int64; typedef unsigned long long unsigned64; typedef TNumber<int> Int; typedef TNumber<unsigned> Unsigned; typedef TNumber<int64> Int64; typedef TNumber<unsigned64> Unsigned64; typedef TNumber<float> Float; typedef TNumber<double> Double; typedef TNumber<char> Char; class Bool { public: Bool(bool x = false) :data_(x) { } operator bool () const { return data_; } Bool& operator = (bool x) { data_ = x; return *this; } Bool& operator &= (bool x) { data_ &= x; return *this; } Bool& operator |= (bool x) { data_ |= x; return *this; } private: bool data_; }; inline std::ostream& operator << (std::ostream& os, Bool b) { if (b) { os << "True"; } else { os << "False"; } return os; }
测试代码(vs2012+win7环境):
#include "stdafx.h" #include "scpp_assert.h" #include "iostream" #include "scpp_vector.h" #include "scpp_array.h" #include "scpp_matrix.h" #include "algorithm" #include "scpp_types.h" int _tmain(int argc, _TCHAR* argv[]) { Int dataInt; Double dataDouble1(1.2); Double dataDouble2; Char c; std::cout << dataInt << std::endl; std::cout << dataDouble1 << std::endl; std::cout << dataDouble2 << std::endl; std::cout << dataInt++ << std::endl; std::cout << ++dataInt << std::endl; std::cout << c << std::endl; c = 'x'; std::cout << c << std::endl; // dataDouble1 /= dataDouble2; dataDouble1 = dataDouble1 / dataDouble2; return 0; }