This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Hard Fault: Unaligned memory access

Hi, I am trying to write an "event dispatcher" so it's possible to listen to types of event and send events. You pass a function pointer and it will be called when that event is sent. Though I am having issues with unaligned memory access and I am unsure what is the cause.

Here is the code for the event dispatcher. I have removed some checks to simplify the code but essentially it's a 2d array of "listener". Each listener has a callback associated with it.

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void eventDispatcher_task(void* context){
event_dispatcher_t* dispatcher = (event_dispatcher_t*)context;
while(true)
{
event_t event;
if(xQueueReceive(dispatcher->queue.handle, &event, portMAX_DELAY) == pdPASS){
// The enum value is it's idx in the array
uint16_t idx = event.type;
event_watchers_t* watcher = &dispatcher->watchers[idx];
// Send to all event listener of that type
for(uint16_t i=0; i < watcher->length; i++){
void* context = watcher->listeners[i].context;
watcher->listeners[i].callback(event->type, event->data, context);
}
}
}
}
// Create task and queue using static allocation
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


And the .h with the types definitions

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "stdint.h"
#include "FreeRTOS.h"
#include "queue.h"
#include "task.h"
#define MAX_EVENT_LISTENER (16)
#define EVENT_LISTENER_QUEUE_SIZE (16)
#define EVENT_LISTENER_STACK_SIZE (512)
#define EVENT_LISTENER_PRIORITY (3)
/**
* @breif The types of event
*/
typedef enum {
EVENT_ALL_EVENT, /** @brief Listen to all event */
EVENT_START_SESSION, /** @brief Listen to start session events */
EVENT_TYPES_COUNT, /** @brief The number of event types */
} event_type_t;
/**
* @brief The data associated with the events
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


Here is where the code is used.
Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void test_callback(event_type_t event_type, event_data_t data, void* context){
uint8_t* val = context;
(*val)++;
LOG_INFO("Event %d with data %d, context %d", event_type, data.start_session.test, *val);
}
event_dispatcher_t event_dispatcher;
int main_freertos(void)
{
// ... Some initialisation code
// Called after freertos is initialized and the scheduler is running
EventDispatcher_init(&event_dispatcher);
static uint8_t counter = 0;
// Register listener
EventDispatcher_registerListener(&event_dispatcher, EVENT_START_SESSION, test_callback, &counter);
// Send event, the callback should be called everytime
while(true){
EventDispatcher_dispatch(&event_dispatcher, EVENT_START_SESSION, (event_data_t){.start_session.test = 42});
vTaskDelay(10);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


The event is trigger three to four times and after a hard fault stops the application. Here is the message displayed by the hard fault handler.

Fullscreen
1
2
3
4
5
6
7
8
9
// Initialization logs....
<info> app: [main.c:577] Event 1 with data 42, context 1
<info> app: [main.c:577] Event 1 with data 42, context 2
<info> app: [main.c:577] Event 1 with data 42, context 3
<info> app: [main.c:577] Event 1 with data 42, context 2
<error> hardfault: HARD FAULT at 0x0002A86A
<error> hardfault: R0: 0x00000013 R1: 0x20008FF4 R2: 0x20008FF8 R3: 0x200098A0
<error> hardfault: R12: 0x20011008 LR: 0x0002A85D PSR: 0x6100000E
<error> hardfault: Cause: The processor has made an unaligned memory access.
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX



I tried aligned my struct using

__attribute__ ((aligned (4)))

on the typedef but it didn't fix the issue since I though this was the issue.

I am using Segger Embedded Studio 4.5 on Windows 10

SDK 15.3.0 on the nrf52840



I know this is a lot of information at once, but since don't know where the alignment issue can come from and no stack trace is available, I've tried to provide all the necessary info.
I don't know if it's caused by the static freertos stack allocation or when the even triggers.
Any help is appreciated and I am more than happy to provide more info if necessary

UPDATE: It seems even when not listening to an event, the issue is present. Thus the issue is necessarily in `eventDispatcher_task`, `EventDispatcher_dispatch` (though not much is done here) or `main_freertos` and the callbacks are not a problem