AC5编译器,搭配C++11,在Keil环境中开发STM32F103C8T6FreeRTOS但是还在模块开发和测试阶段,因此还 没启动调度器 ,但是使用了heap4.c中的pvPortMalloc重载new和定义了内存分配器OSAllocator(下附源码),目的是使用STL库时用FreeRTOS托管内存Heap_Size设置为0,FreeRTOS的heap设置为8k串口发送是没有问题的,如果不是使用STL的queue,所有功能一切正常,每次接收中断都能正常触发。但是在串口中断中使用queue接受数据,只要触发一次接受中断并入队,那么这个串口中断和定时器中断都失效了,再也不会进去。
最奇怪的是之前这一套方案是可行的,但是写完定时器的代码后就不行了, 尽管不开定时器也不行 。
类成员变量定义:
class Uart_Dev
{
public:
...
private:
...
uint16_t __rx_max_size;//定义最大容量
std::queue<uint8_t, std::deque<uint8_t, OSAllocator<uint8_t>>> __rx_queue;
}
中断函数定义:
void Uart_Dev::isr_handler(uint8_t data)
{
if (__rx_queue.size() < __rx_max_size)
{
__rx_queue.push(data);
}
}
static Uart_Dev *usart1;//指向USART1外设的设备指针,创建有关USART1的对象时,会将this赋值给它
extern "C"
{
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
uint8_t data = USART_ReceiveData(USART1);
usart1->isr_handler(data);
}
}
}
template <class _TypeT>
class OSAllocator
{
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef _TypeT value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
OSAllocator() noexcept {}
OSAllocator(const OSAllocator&) noexcept {}
template <class _TypeU>
struct rebind {
typedef OSAllocator<_TypeU> other;
};
template <class _TypeU>
OSAllocator(const OSAllocator<_TypeU>&) noexcept {}
template <class _TypeU>
OSAllocator& operator=(const OSAllocator<_TypeU>&) noexcept {
return *this;
}
pointer address(reference __x) const {
return &__x;
}
const_pointer address(const_reference __x) const {
return &__x;
}
pointer allocate(size_type __n, const void* = 0) {
if (__n == 0) return nullptr;
pointer __p = static_cast<pointer>(pvPortMalloc(__n * sizeof(value_type)));
// if (!__p) throw std::bad_alloc();
return __p;
}
void deallocate(pointer __p, size_type) {
vPortFree(__p);
}
size_type max_size() const noexcept {
return size_type(-1) / sizeof(value_type);
}
template <class _Up, class... _Args>
void construct(_Up* __p, _Args... __args) {
::new((void *)__p) _Up(__args...);
}
template <class _Up>
void destroy(_Up* __p) noexcept {
__p->~_Up();
}
};
祝各位看到帖子和解决问题的大佬们身体健康,万事如意
举报
更多回帖