Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement on sem_ctl + psreg #167

Merged
merged 22 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions projects/cm_mcu/Semaphore.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,50 @@ void initSemaphores(void)
i2c5_sem = xSemaphoreCreateMutex();
i2c6_sem = xSemaphoreCreateMutex();
}

SemaphoreHandle_t getSemaphore(int number)
{
SemaphoreHandle_t s;
switch (number) {
case 1:
s = i2c1_sem;
break;
case 2:
s = i2c2_sem;
break;
case 3:
s = i2c3_sem;
break;
case 4:
s = i2c4_sem;
break;
case 5:
s = i2c5_sem;
break;
case 6:
s = i2c6_sem;
break;
default:
s = 0;
break;
}
return s;
}

#define MAX_TRIES 50
int acquireI2CSemaphore(SemaphoreHandle_t s)
{
int retval = pdTRUE;
if (s == NULL) {
return pdFAIL;
}
int tries = 0;
while (xSemaphoreTake(s, (TickType_t)10) == pdFALSE) {
++tries;
if (tries > MAX_TRIES) {
retval = pdFAIL;
break;
}
}
return retval;
}
4 changes: 4 additions & 0 deletions projects/cm_mcu/Semaphore.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ extern SemaphoreHandle_t i2c6_sem;

void initSemaphores(void);

SemaphoreHandle_t getSemaphore(int number);

int acquireI2CSemaphore(SemaphoreHandle_t s);

#endif /* PROJECTS_CM_MCU_SEMAPHORE_H_ */
46 changes: 0 additions & 46 deletions projects/cm_mcu/commands/I2CCommands.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,6 @@
#include "Semaphore.h"
#include "projdefs.h"

SemaphoreHandle_t getSemaphore(int number)
{
SemaphoreHandle_t s;
switch (number) {
case 1:
s = i2c1_sem;
break;
case 2:
s = i2c2_sem;
break;
case 3:
s = i2c3_sem;
break;
case 4:
s = i2c4_sem;
break;
case 5:
s = i2c5_sem;
break;
case 6:
s = i2c6_sem;
break;
default:
s = 0;
break;
}
return s;
}
#define MAX_TRIES 10
static int acquireI2CSemaphore(SemaphoreHandle_t s)
{
int retval = pdTRUE;
if (s == NULL) {
return pdFAIL;
}
int tries = 0;
while (xSemaphoreTake(s, (TickType_t)10) == pdFALSE) {
++tries;
if (tries > MAX_TRIES) {
retval = pdFAIL;
break;
}
}
return retval;
}

static bool isValidDevice(int device)
{
#ifdef REV1
Expand Down
10 changes: 7 additions & 3 deletions projects/cm_mcu/commands/SensorControl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1197,8 +1197,10 @@ BaseType_t psmon_reg(int argc, char **argv, char *m)
page, pm_addrs_dcdc[which].name, regAddress);

// acquire the semaphore
while (xSemaphoreTake(dcdc_args.xSem, (TickType_t)10) == pdFALSE)
;
if (acquireI2CSemaphore(dcdc_args.xSem) == pdFAIL) {
snprintf(m, SCRATCH_SIZE, "%s: could not get semaphore in time\r\n", argv[0]);
return pdFALSE;
}
uint8_t ui8page = page;
// page register
int r = apollo_pmbus_rw(&g_sMaster1, &eStatus1, false, &pm_addrs_dcdc[which], &extra_cmds[0], &ui8page);
Expand All @@ -1217,6 +1219,8 @@ BaseType_t psmon_reg(int argc, char **argv, char *m)
argv[0], vv);

// release the semaphore
xSemaphoreGive(dcdc_args.xSem);
if (xSemaphoreGetMutexHolder(dcdc_args.xSem) == xTaskGetCurrentTaskHandle()) {
xSemaphoreGive(dcdc_args.xSem);
}
return pdFALSE;
}
2 changes: 1 addition & 1 deletion projects/cm_mcu/commands/SoftwareCommands.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ BaseType_t sem_ctl(int argc, char **argv, char *m)
}
else if (strncmp(argv[2], "take", 4) == 0) {
// try to acquire the semaphore. Wait a finite amount of time.
if (xSemaphoreTake(s, pdMS_TO_TICKS(500)) == pdTRUE) {
if (xSemaphoreTake(s, pdMS_TO_TICKS(5000)) == pdTRUE) {
snprintf(m, SCRATCH_SIZE, "%s: taking semaphore %d\r\n", argv[0], number);
sem_status |= 0x1U << number;
}
Expand Down