Skip to content
Draft
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
22 changes: 13 additions & 9 deletions multi/stm32l4-multi/stm32l4-multi.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <sys/msg.h>
#include <sys/pwman.h>
#include <posix/utils.h>
#include <assert.h>

#include <libklog.h>

Expand Down Expand Up @@ -89,7 +90,7 @@


struct {
char stack[THREADS_NO - 1][STACKSZ] __attribute__ ((aligned(8)));
char stack[THREADS_NO - 1][STACKSZ] __attribute__((aligned(8)));

unsigned int port;
} common;
Expand Down Expand Up @@ -192,17 +193,17 @@ static void handleMsg(msg_t *msg)

case spi_get:
err = spi_transaction(imsg->spi_rw.spi, spi_dir_read, imsg->spi_rw.cmd, imsg->spi_rw.addr,
imsg->spi_rw.flags, msg->o.data, NULL, msg->o.size);
imsg->spi_rw.flags, msg->o.data, NULL, msg->o.size);
break;

case spi_set:
err = spi_transaction(imsg->spi_rw.spi, spi_dir_write, imsg->spi_rw.cmd, imsg->spi_rw.addr,
imsg->spi_rw.flags, NULL, msg->i.data, msg->i.size);
imsg->spi_rw.flags, NULL, msg->i.data, msg->i.size);
break;

case spi_rw:
err = spi_transaction(imsg->spi_rw.spi, spi_dir_readwrite, imsg->spi_rw.cmd, imsg->spi_rw.addr,
imsg->spi_rw.flags, msg->o.data, msg->i.data, (msg->i.size > msg->o.size) ? msg->o.size : msg->i.size);
imsg->spi_rw.flags, msg->o.data, msg->i.data, (msg->i.size > msg->o.size) ? msg->o.size : msg->i.size);
break;

case spi_def:
Expand All @@ -211,7 +212,7 @@ static void handleMsg(msg_t *msg)

case gpio_def:
err = gpio_configPin(imsg->gpio_def.port, imsg->gpio_def.pin, imsg->gpio_def.mode,
imsg->gpio_def.af, imsg->gpio_def.otype, imsg->gpio_def.ospeed, imsg->gpio_def.pupd);
imsg->gpio_def.af, imsg->gpio_def.otype, imsg->gpio_def.ospeed, imsg->gpio_def.pupd);
break;

case gpio_get:
Expand All @@ -225,12 +226,12 @@ static void handleMsg(msg_t *msg)

case uart_def:
err = uart_configure(imsg->uart_def.uart, imsg->uart_def.bits, imsg->uart_def.parity,
imsg->uart_def.baud, imsg->uart_def.enable);
imsg->uart_def.baud, imsg->uart_def.enable);
break;

case uart_get:
err = uart_read(imsg->uart_get.uart, msg->o.data, msg->o.size,
imsg->uart_get.mode, imsg->uart_get.timeout);
imsg->uart_get.mode, imsg->uart_get.timeout);
break;

case uart_set:
Expand Down Expand Up @@ -264,8 +265,11 @@ static void handleMsg(msg_t *msg)
err = pwm_disableChannel(imsg->pwm_dischn.timer, imsg->pwm_dischn.chn);
break;
case pwm_bitseq:
err = pwm_setBitSequence(imsg->pwm_bitseq.timer, imsg->pwm_bitseq.chn, imsg->pwm_bitseq.data,
imsg->pwm_bitseq.nbits, imsg->pwm_bitseq.datasize, imsg->pwm_bitseq.flags);
assert(imsg->pwm_bitseq.bitseqTable != NULL);
for (uint8_t i = 0; i < imsg->pwm_bitseq.count; i++) {
err = pwm_setBitSequence(imsg->pwm_bitseq.bitseqTable[i].timer, imsg->pwm_bitseq.bitseqTable[i].chn, imsg->pwm_bitseq.bitseqTable[i].data,
imsg->pwm_bitseq.bitseqTable[i].nbits, imsg->pwm_bitseq.bitseqTable[i].datasize, imsg->pwm_bitseq.bitseqTable[i].flags);
}
Comment on lines +269 to +272

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This loop has a couple of issues:

  1. Compile Error: The assert on line 269 contains a typo (imsg->bitseqTable[i]). The correct member is imsg->pwm_bitseq.bitseqTable. This will prevent the code from compiling.
  2. Error Handling: The err variable is overwritten in each iteration. If pwm_setBitSequence() fails for any but the last item, the error will be lost, and the operation might be considered successful. The loop should terminate on the first error.

I suggest replacing the loop with a version that corrects the assertion and handles errors properly.

            if (imsg->pwm_bitseq.count > 0) {
                assert(imsg->pwm_bitseq.bitseqTable != NULL);
                for (uint8_t i = 0; i < imsg->pwm_bitseq.count; i++) {
                    err = pwm_setBitSequence(imsg->pwm_bitseq.bitseqTable[i].timer, imsg->pwm_bitseq.bitseqTable[i].chn, imsg->pwm_bitseq.bitseqTable[i].data,
                            imsg->pwm_bitseq.bitseqTable[i].nbits, imsg->pwm_bitseq.bitseqTable[i].datasize, imsg->pwm_bitseq.bitseqTable[i].flags);
                    if (err < 0) {
                        break;
                    }
                }
            }

break;
#endif

Expand Down
18 changes: 15 additions & 3 deletions multi/stm32l4-multi/stm32l4-multi.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,26 @@ typedef struct {
uint32_t nbits;
uint8_t datasize;
int flags;
} pwmbitseqinternal_t;

typedef struct {
pwmbitseqinternal_t *bitseqTable;
uint8_t count;
} __attribute__((packed)) pwmbitseq_t;


/* EXTI */


enum { exti_irq = 0, exti_event, exti_irqevent, exti_disabled };
enum { exti_irq = 0,
exti_event,
exti_irqevent,
exti_disabled };


enum { exti_rising = 0, exti_falling, exti_risingfalling };
enum { exti_rising = 0,
exti_falling,
exti_risingfalling };


typedef struct {
Expand All @@ -252,7 +262,9 @@ typedef struct {
/* ADC */


enum { adc1 = 0, adc2, adc3 };
enum { adc1 = 0,
adc2,
adc3 };


typedef struct {
Expand Down
Loading