【发布时间】:2020-03-17 06:21:03
【问题描述】:
我正在尝试制作一些 UART 代码,它将在堆栈上放置一个小字符串,以便通过中断系统进行传输。这专门用于 Atmel SAM 微控制器
#define UART_BUF_LEN 16
//GPS SERIAL INSTANCE
struct usart_module usart_instance;
char buffer[UART_BUF_LEN];
uintptr_t bufferPtr = (uintptr_t)buffer;
bool transmitting(){
return (uintptr_t)buffer == bufferPtr; //If the read head is at the top of the array, then we are not transmitting.
}
bool transmit(char* c, uint len){
if(!transmitting() && len<=UART_BUF_LEN){
bufferPtr= (uintptr_t)buffer + len;//set write head to top of the len stack
while(transmitting()){
*bufferPtr = *c; //Set the value at the address of bufferPtr to the value at the address of c.
bufferPtr --; //Move the buffer closer to the head of the array
c++; //Move the head of the array down some.
}
}else{
return false;
}
bufferPtr= (uintptr_t)buffer + len; //reset the read head so that our transmit code knows where to read from.
return true;
}
这里有问题*bufferPtr = *c;。当我构建解决方案时,bufferptr 似乎不可取消引用。我收到以下错误:
一元'*'的无效类型参数(有'uintptr_t {aka unsigned int}')
我在网上看过,所有消息来源都告诉我,我必须将uintptr_t 转换回内存地址指向的本机数据类型的指针。我不确定该怎么做,因为使用类型转换(char *),表明缓冲区指针是字符指针无法编译,给我与上面相同的错误。
现在,当我将行更改为 \*(char\*)bufferPtr = \*c; 时,兔子洞会更深一层,这不会给我任何错误。这条线是什么意思?
我希望这意味着将bufferPtr类型转换为char指针的地址处的值设置为c地址处的值。这是正确的吗?
【问题讨论】:
-
uintptr_t 是“unsigned int”,所以你必须把它转换成指针
-
@NoumanTajik 在微控制器代码中,我认为它有点不同,尤其是在 8 位微控制器中,但我明白你在说什么
-
无论是 PC 还是 MCU,编译器都必须知道您指的是内存位置还是值。你上面所说的错误说同样的事情,即一元'*'的无效类型参数(有'uintptr_t {aka unsigned int}')。我认为 uintptr_t 命名有些混乱,它应该是 typedef 为 (unsigned int *) 而不是 (unsigned int)。
-
@NoumanTajik 那么它们是完全相同的数据类型吗?
-
"uintptr_t" 在你的代码中意味着 (unsigned int) NOT (unsigned int *)
标签: c pointers microcontroller avr