Code: Select all
#define BLE_CMD_MAX_QUEUED 4
#define BLE_CMD_MAX_PACKET_SIZE 64
typedef struct {
binary_semaphore_t semaphore;
uint8_t packet[BLE_CMD_MAX_PACKET_SIZE];
uint8_t packet_length;
uint8_t class_id;
uint8_t id;
} ble_cmd_t;
static MEMORYPOOL_DECL(cmd_pool, sizeof(ble_cmd_t), NULL);
static ble_cmd_t cmd_pool_data[BLE_CMD_MAX_QUEUED] __attribute__((aligned(sizeof(stkalign_t))));
static mailbox_t cmd_mailbox;
static msg_t cmd_messages[BLE_CMD_MAX_QUEUED];
void bleInit()
{
uint8_t i;
for (i=0; i < BLE_CMD_MAX_QUEUED; i++)
{
chPoolFree(&cmd_pool, &(cmd_pool_data[i]));
chBSemObjectInit(&(cmd_pool_data[i].semaphore), false);
}
chMBObjectInit(&cmd_mailbox, cmd_messages, BLE_CMD_MAX_QUEUED);
ble_cmd_t* cmd = (ble_cmd_t*)chPoolAlloc(&cmd_pool);
ble_cmd_t* cmd2 = (ble_cmd_t*)chPoolAlloc(&cmd_pool);
//at this point cmd == cmd2
}
After changing the ble_cmd_t struct definition to the below, making the semaphore the last field, cmd and cmd2 were correctly allocated and assigned two distinct addresses:
Code: Select all
typedef struct {
uint8_t packet[BLE_CMD_MAX_PACKET_SIZE];
uint8_t packet_length;
uint8_t class_id;
uint8_t id;
binary_semaphore_t semaphore;
} ble_cmd_t;
I really just can't wrap my head around why the order of the fields matter, other than some kind of memory alignment issue. Does anything in the above code look wrong or can anybody suggest reasons why I might be experiencing this behavior? Even though it seems to be working now I won't be satisfied until I can explain the behavior somehow. Thanks!