Skip to content

Commit 8b39410

Browse files
committed
added chmemcore patch
1 parent 115fd6f commit 8b39410

File tree

6 files changed

+289
-21
lines changed

6 files changed

+289
-21
lines changed

Makefile

+4-10
Original file line numberDiff line numberDiff line change
@@ -90,30 +90,24 @@ CSRC = $(PORTSRC) \
9090
CPPSRC = $(R2PSRC)
9191

9292
ifeq ($(TEST),debug_test)
93-
CPPSRC += DebugTransport.cpp DebugPublisher.cpp DebugSubscriber.cpp \
94-
chnew.cpp main_debug_test.cpp
93+
CPPSRC += chnew.cpp main_debug_test.cpp
9594
endif
9695

9796
ifeq ($(TEST),rtcan_pub_test)
98-
CPPSRC += DebugTransport.cpp DebugPublisher.cpp DebugSubscriber.cpp \
99-
RTCANTransport.cpp RTCANPublisher.cpp RTCANSubscriber.cpp \
100-
chnew.cpp main_rtcan_pub_test.cpp
97+
CPPSRC += chnew.cpp main_rtcan_pub_test.cpp
10198
# UDEFS = -DRTCAN_ISMASTER
10299
endif
103100

104101
ifeq ($(TEST),rtcan_sub_test)
105-
CPPSRC += DebugTransport.cpp DebugPublisher.cpp DebugSubscriber.cpp \
106-
RTCANTransport.cpp RTCANPublisher.cpp RTCANSubscriber.cpp \
107-
chnew.cpp main_rtcan_sub_test.cpp
102+
CPPSRC += chnew.cpp main_rtcan_sub_test.cpp
108103
endif
109104

110105
ifeq ($(TEST),pubsub_test)
111106
CPPSRC += main_pubsub_test.cpp chnew.cpp
112107
endif
113108

114109
ifeq ($(TEST),)
115-
CPPSRC += DebugTransport.cpp DebugPublisher.cpp DebugSubscriber.cpp \
116-
main.cpp chnew.cpp
110+
CPPSRC += main.cpp chnew.cpp
117111
endif
118112

119113
# C sources to be compiled in ARM mode regardless of the global setting.

apps/pub_led/pub_led.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
#include "common.hpp"
1+
#include <r2p/Middleware.hpp>
2+
#include "LedMsg.hpp"
23

3-
using namespace r2p;
4+
#include "ch.h"
5+
#include "hal.h"
46

7+
using namespace r2p;
58

69
struct AppConfig {
710
char led_topic_name[NamingTraits<Topic>::MAX_LENGTH + 1];
@@ -14,7 +17,7 @@ const AppConfig app_config R2P_APP_CONFIG = {
1417
"LED",
1518
"LED_PUB",
1619
200,
17-
1
20+
LED2
1821
};
1922

2023

@@ -35,10 +38,10 @@ void app_main(void) {
3538
if (pub.publish(*msgp)) {
3639
was_on = !was_on;
3740
} else {
38-
palTogglePad(LED_GPIO, LED3);
41+
palTogglePad(LED_GPIO, LED4);
3942
}
4043
} else {
41-
palTogglePad(LED_GPIO, LED3);
44+
palTogglePad(LED_GPIO, LED4);
4245
}
4346
r2p::Thread::sleep(Time::ms(app_config.loop_delay_ms));
4447
}

apps/sub_led/sub_led.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
#include "common.hpp"
1+
#include <r2p/Middleware.hpp>
2+
#include "LedMsg.hpp"
3+
4+
#include "ch.h"
5+
#include "hal.h"
6+
27

38
using namespace r2p;
49

@@ -19,12 +24,10 @@ const AppConfig app_config R2P_APP_CONFIG = {
1924

2025
static bool led_callback(const LedMsg &msg) {
2126

22-
unsigned led_pin_id = led2pin(msg.id);
23-
2427
if (msg.on) {
25-
palClearPad(LED_GPIO, led_pin_id);
28+
palClearPad(LED_GPIO, msg.id);
2629
} else {
27-
palSetPad(LED_GPIO, led_pin_id);
30+
palSetPad(LED_GPIO, msg.id);
2831
}
2932
return true;
3033
}

chmemcore.c

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*
2+
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
3+
2011,2012,2013 Giovanni Di Sirio.
4+
5+
This file is part of ChibiOS/RT.
6+
7+
ChibiOS/RT is free software; you can redistribute it and/or modify
8+
it under the terms of the GNU General Public License as published by
9+
the Free Software Foundation; either version 3 of the License, or
10+
(at your option) any later version.
11+
12+
ChibiOS/RT is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU General Public License for more details.
16+
17+
You should have received a copy of the GNU General Public License
18+
along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
/**
22+
* @file chmemcore.c
23+
* @brief Core memory manager code.
24+
*
25+
* @addtogroup memcore
26+
* @details Core Memory Manager related APIs and services.
27+
* <h2>Operation mode</h2>
28+
* The core memory manager is a simplified allocator that only
29+
* allows to allocate memory blocks without the possibility to
30+
* free them.<br>
31+
* This allocator is meant as a memory blocks provider for the
32+
* other allocators such as:
33+
* - C-Runtime allocator (through a compiler specific adapter module).
34+
* - Heap allocator (see @ref heaps).
35+
* - Memory pools allocator (see @ref pools).
36+
* .
37+
* By having a centralized memory provider the various allocators
38+
* can coexist and share the main memory.<br>
39+
* This allocator, alone, is also useful for very simple
40+
* applications that just require a simple way to get memory
41+
* blocks.
42+
* @pre In order to use the core memory manager APIs the @p CH_USE_MEMCORE
43+
* option must be enabled in @p chconf.h.
44+
* @{
45+
*/
46+
47+
#include "ch.h"
48+
49+
#if CH_USE_MEMCORE || defined(__DOXYGEN__)
50+
51+
static uint8_t *nextmem;
52+
static uint8_t *endmem;
53+
static uint8_t *realendmem;
54+
55+
/**
56+
* @brief Low level memory manager initialization.
57+
*
58+
* @notapi
59+
*/
60+
void _core_init(void) {
61+
#if CH_MEMCORE_SIZE == 0
62+
extern uint8_t __heap_base__[];
63+
extern uint8_t __heap_end__[];
64+
nextmem = (uint8_t *)MEM_ALIGN_NEXT(__heap_base__);
65+
endmem = (uint8_t *)MEM_ALIGN_PREV(__heap_end__);
66+
#else
67+
static stkalign_t buffer[MEM_ALIGN_NEXT(CH_MEMCORE_SIZE)/MEM_ALIGN_SIZE];
68+
nextmem = (uint8_t *)&buffer[0];
69+
endmem = (uint8_t *)&buffer[MEM_ALIGN_NEXT(CH_MEMCORE_SIZE)/MEM_ALIGN_SIZE];
70+
#endif
71+
realendmem = endmem;
72+
}
73+
74+
/**
75+
* @brief Allocates a memory block.
76+
* @details The size of the returned block is aligned to the alignment
77+
* type so it is not possible to allocate less
78+
* than <code>MEM_ALIGN_SIZE</code>.
79+
*
80+
* @param[in] size the size of the block to be allocated
81+
* @return A pointer to the allocated memory block.
82+
* @retval NULL allocation failed, core memory exhausted.
83+
*
84+
* @api
85+
*/
86+
void *chCoreAlloc(size_t size) {
87+
void *p;
88+
89+
chSysLock();
90+
p = chCoreAllocI(size);
91+
chSysUnlock();
92+
return p;
93+
}
94+
95+
/**
96+
* @brief Allocates a memory block.
97+
* @details The size of the returned block is aligned to the alignment
98+
* type so it is not possible to allocate less than
99+
* <code>MEM_ALIGN_SIZE</code>.
100+
*
101+
* @param[in] size the size of the block to be allocated.
102+
* @return A pointer to the allocated memory block.
103+
* @retval NULL allocation failed, core memory exhausted.
104+
*
105+
* @iclass
106+
*/
107+
void *chCoreAllocI(size_t size) {
108+
void *p;
109+
110+
chDbgCheckClassI();
111+
112+
size = MEM_ALIGN_NEXT(size);
113+
if ((size_t)(endmem - nextmem) < size)
114+
return NULL;
115+
p = nextmem;
116+
nextmem += size;
117+
return p;
118+
}
119+
120+
/**
121+
* @brief Core memory status.
122+
*
123+
* @return The size, in bytes, of the free core memory.
124+
*
125+
* @api
126+
*/
127+
size_t chCoreStatus(void) {
128+
129+
return (size_t)(endmem - nextmem);
130+
}
131+
132+
133+
134+
void *chCoreReserve(size_t size) {
135+
void *p;
136+
137+
chSysLock();
138+
p = chCoreReserveI(size);
139+
chSysUnlock();
140+
return p;
141+
}
142+
143+
void *chCoreReserveI(size_t size) {
144+
145+
chDbgCheckClassI();
146+
147+
size = MEM_ALIGN_NEXT(size);
148+
if ((size_t)(endmem - nextmem) < size)
149+
return NULL;
150+
endmem -= size;
151+
return endmem;
152+
}
153+
154+
void *chCoreUnreserve(size_t size) {
155+
void *p;
156+
157+
chSysLock();
158+
p = chCoreUnreserveI(size);
159+
chSysUnlock();
160+
return p;
161+
}
162+
163+
void *chCoreUnreserveI(size_t size) {
164+
165+
chDbgCheckClassI();
166+
167+
size = MEM_ALIGN_NEXT(size);
168+
if ((size_t)(realendmem - endmem) < size)
169+
return NULL;
170+
endmem += size;
171+
return endmem;
172+
}
173+
174+
#endif /* CH_USE_MEMCORE */
175+
176+
/** @} */

chmemcore.h

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
3+
2011,2012,2013 Giovanni Di Sirio.
4+
5+
This file is part of ChibiOS/RT.
6+
7+
ChibiOS/RT is free software; you can redistribute it and/or modify
8+
it under the terms of the GNU General Public License as published by
9+
the Free Software Foundation; either version 3 of the License, or
10+
(at your option) any later version.
11+
12+
ChibiOS/RT is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU General Public License for more details.
16+
17+
You should have received a copy of the GNU General Public License
18+
along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
/**
22+
* @file chmemcore.h
23+
* @brief Core memory manager macros and structures.
24+
*
25+
* @addtogroup memcore
26+
* @{
27+
*/
28+
29+
#ifndef _CHMEMCORE_H_
30+
#define _CHMEMCORE_H_
31+
32+
/**
33+
* @brief Memory get function.
34+
* @note This type must be assignment compatible with the @p chMemAlloc()
35+
* function.
36+
*/
37+
typedef void *(*memgetfunc_t)(size_t size);
38+
39+
/**
40+
* @name Alignment support macros
41+
*/
42+
/**
43+
* @brief Alignment size constant.
44+
*/
45+
#define MEM_ALIGN_SIZE sizeof(stkalign_t)
46+
47+
/**
48+
* @brief Alignment mask constant.
49+
*/
50+
#define MEM_ALIGN_MASK (MEM_ALIGN_SIZE - 1)
51+
52+
/**
53+
* @brief Alignment helper macro.
54+
*/
55+
#define MEM_ALIGN_PREV(p) ((size_t)(p) & ~MEM_ALIGN_MASK)
56+
57+
/**
58+
* @brief Alignment helper macro.
59+
*/
60+
#define MEM_ALIGN_NEXT(p) MEM_ALIGN_PREV((size_t)(p) + MEM_ALIGN_MASK)
61+
62+
/**
63+
* @brief Returns whatever a pointer or memory size is aligned to
64+
* the type @p align_t.
65+
*/
66+
#define MEM_IS_ALIGNED(p) (((size_t)(p) & MEM_ALIGN_MASK) == 0)
67+
/** @} */
68+
69+
#if CH_USE_MEMCORE || defined(__DOXYGEN__)
70+
71+
#ifdef __cplusplus
72+
extern "C" {
73+
#endif
74+
void _core_init(void);
75+
void *chCoreAlloc(size_t size);
76+
void *chCoreAllocI(size_t size);
77+
size_t chCoreStatus(void);
78+
79+
void *chCoreReserve(size_t size);
80+
void *chCoreReserveI(size_t size);
81+
void *chCoreUnreserve(size_t size);
82+
void *chCoreUnreserveI(size_t size);
83+
84+
#ifdef __cplusplus
85+
}
86+
#endif
87+
88+
#endif /* CH_USE_MEMCORE */
89+
90+
#endif /* _CHMEMCORE_H_ */
91+
92+
/** @} */

main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <r2p/Mutex.hpp>
1212
#include <r2p/NamingTraits.hpp>
1313
#include <r2p/Bootloader.hpp>
14-
#include "DebugTransport.hpp"
14+
#include "r2p/transport/DebugTransport.hpp"
1515

1616
#include <cstdio>
1717
#include <cstdlib>

0 commit comments

Comments
 (0)