Skip to content

Commit 09f35b3

Browse files
bessmanAlexander Bessman
authored andcommitted
Use dynamic memory
1 parent 5cb9ab4 commit 09f35b3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+753
-853
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ target_compile_options(pslab-firmware.elf PRIVATE
1919
-mno-eds-warn)
2020

2121
target_link_options(pslab-firmware.elf PRIVATE
22-
"LINKER:-Map=pslab-firmware.map,--report-mem")
22+
"LINKER:-Map=pslab-firmware.map,--report-mem,--heap=0x6000")
2323

2424
if (LEGACY_HARDWARE)
2525
message("Building for PSLab v5")

src/bus/i2c/i2c.c

Lines changed: 71 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ uint8_t I2C_Receive(I2C_RESPONSE r) {
745745

746746

747747
enum Status I2C_command_start(
748-
uint8_t args[],
748+
uint8_t **args,
749749
uint16_t const args_size,
750750
__attribute__ ((unused)) uint8_t **rets,
751751
__attribute__ ((unused)) uint16_t *rets_size
@@ -756,21 +756,21 @@ enum Status I2C_command_start(
756756
return E_BAD_ARGSIZE;
757757
}
758758

759-
address = *args;
759+
address = **args;
760760

761761
I2C_InitializeIfNot(I2C_GetBaudRate(), I2C_DISABLE_INTERRUPTS);
762762
I2C_StartSignal();
763763
I2C_Transmit(address);
764764

765-
*rets = args;
765+
*rets = *args;
766766
(*rets)[0] = (uint8_t)I2C2STATbits.ACKSTAT;
767767
*rets_size = 1;
768768

769769
return I2C_ACKNOWLEDGE_STATUS_BIT && I2C2STATbits.BCL ? E_FAILED : E_OK;
770770
}
771771

772772
enum Status I2C_command_stop(
773-
__attribute__ ((unused)) uint8_t args[],
773+
__attribute__ ((unused)) uint8_t **args,
774774
__attribute__ ((unused)) uint16_t const args_size,
775775
__attribute__ ((unused)) uint8_t **rets,
776776
__attribute__ ((unused)) uint16_t *rets_size
@@ -780,7 +780,7 @@ enum Status I2C_command_stop(
780780
}
781781

782782
enum Status I2C_command_wait(
783-
__attribute__ ((unused)) uint8_t args[],
783+
__attribute__ ((unused)) uint8_t **args,
784784
__attribute__ ((unused)) uint16_t const args_size,
785785
__attribute__ ((unused)) uint8_t **rets,
786786
__attribute__ ((unused)) uint16_t *rets_size
@@ -790,18 +790,21 @@ enum Status I2C_command_wait(
790790
}
791791

792792
enum Status I2C_command_send(
793-
uint8_t args[],
793+
uint8_t **args,
794794
uint16_t const args_size,
795795
uint8_t **rets,
796796
uint16_t *rets_size
797797
) {
798-
if (args_size != 1) {
798+
uint8_t data = 0;
799+
800+
if (args_size != sizeof(data)) {
799801
return E_BAD_ARGSIZE;
800802
}
801803

802-
uint8_t data = *args;
804+
data = **args;
803805
I2C_Transmit(data);
804-
**rets = (uint8_t)I2C2STATbits.ACKSTAT;
806+
*rets = *args;
807+
(*rets)[0] = (uint8_t)I2C2STATbits.ACKSTAT;
805808
*rets_size = 1;
806809

807810
if (I2C_ACKNOWLEDGE_STATUS_BIT && I2C2STATbits.BCL) {
@@ -811,35 +814,40 @@ enum Status I2C_command_send(
811814
}
812815

813816
enum Status I2C_command_send_burst(
814-
uint8_t args[],
817+
uint8_t **args,
815818
uint16_t const args_size,
816819
__attribute__ ((unused)) uint8_t **rets,
817820
__attribute__ ((unused)) uint16_t *rets_size
818821
) {
819-
if (args_size != 1) {
822+
uint8_t data = 0;
823+
824+
if (args_size != sizeof(data)) {
820825
return E_BAD_ARGSIZE;
821826
}
822827

823-
uint8_t const data = *args;
828+
data = **args;
824829
I2C_Transmit(data);
825830

826831
return E_OK;
827832
}
828833

829834
enum Status I2C_command_restart(
830-
uint8_t args[],
835+
uint8_t **args,
831836
uint16_t const args_size,
832837
uint8_t **rets,
833838
uint16_t *rets_size
834839
) {
835-
if (args_size != 1) {
840+
uint8_t address = 0;
841+
842+
if (args_size != sizeof(address)) {
836843
return E_BAD_ARGSIZE;
837844
}
838845

839-
uint8_t const address = *args;
846+
address = **args;
840847
I2C_RestartSignal();
841848
I2C_Transmit(address);
842-
**rets = (uint8_t)I2C2STATbits.ACKSTAT;
849+
*rets = *args;
850+
(*rets)[0] = (uint8_t)I2C2STATbits.ACKSTAT;
843851
*rets_size = 1;
844852

845853
if (I2C_ACKNOWLEDGE_STATUS_BIT && I2C2STATbits.BCL) {
@@ -850,7 +858,7 @@ enum Status I2C_command_restart(
850858
}
851859

852860
enum Status I2C_command_read_more(
853-
__attribute__ ((unused)) uint8_t args[],
861+
__attribute__ ((unused)) uint8_t **args,
854862
__attribute__ ((unused)) uint16_t const args_size,
855863
uint8_t **rets,
856864
uint16_t *rets_size
@@ -862,7 +870,7 @@ enum Status I2C_command_read_more(
862870
}
863871

864872
enum Status I2C_command_read_end(
865-
__attribute__ ((unused)) uint8_t args[],
873+
__attribute__ ((unused)) uint8_t **args,
866874
__attribute__ ((unused)) uint16_t const args_size,
867875
uint8_t **rets,
868876
uint16_t *rets_size
@@ -874,22 +882,24 @@ enum Status I2C_command_read_end(
874882
}
875883

876884
enum Status I2C_command_config(
877-
uint8_t args[],
885+
uint8_t **args,
878886
uint16_t const args_size,
879887
__attribute__ ((unused)) uint8_t **rets,
880888
__attribute__ ((unused)) uint16_t *rets_size
881889
) {
882-
if (args_size != 2) {
890+
uint16_t baudrate = BAUD1000000;
891+
892+
if (args_size != sizeof(baudrate)) {
883893
return E_BAD_ARGSIZE;
884894
}
885895

886-
uint16_t const baud_rate = *(uint16_t const *const)args;
887-
I2C_InitializeIfNot(baud_rate, I2C_DISABLE_INTERRUPTS);
896+
baudrate = **(uint16_t **)args;
897+
I2C_InitializeIfNot(baudrate, I2C_DISABLE_INTERRUPTS);
888898
return E_OK;
889899
}
890900

891901
enum Status I2C_command_status(
892-
__attribute__ ((unused)) uint8_t args[],
902+
__attribute__ ((unused)) uint8_t **args,
893903
__attribute__ ((unused)) uint16_t const args_size,
894904
uint8_t **rets,
895905
uint16_t *rets_size
@@ -900,35 +910,38 @@ enum Status I2C_command_status(
900910
}
901911

902912
enum Status I2C_command_read_bulk(
903-
uint8_t *const args,
913+
uint8_t **args,
904914
uint16_t const args_size,
905915
uint8_t **rets,
906916
uint16_t *rets_size
907917
) {
908-
if (args_size != 3) {
918+
struct Input {
919+
uint8_t device;
920+
uint8_t address;
921+
uint8_t count;
922+
uint8_t _pad[0];
923+
} *input = NULL;
924+
925+
if (args_size != sizeof(struct Input) - sizeof(input->_pad)) {
909926
return E_BAD_ARGSIZE;
910927
}
911928

912-
uint8_t const device = args[0];
913-
uint8_t const address = args[1];
914-
uint8_t count = args[2];
929+
input = (struct Input *)args;
915930

916-
if (!count) {
917-
return E_OK;
918-
}
931+
if (!input->count) { return E_OK; }
919932

920933
// count is uint8_t and will always fit in payload buffer.
921934

922935
I2C_StartSignal();
923-
I2C_Transmit(device << 1);
924-
I2C_Transmit(address);
936+
I2C_Transmit(input->device << 1);
937+
I2C_Transmit(input->address);
925938
I2C_RestartSignal();
926-
I2C_Transmit((device << 1) | 1);
939+
I2C_Transmit((input->device << 1) | 1);
927940

928-
*rets = args; // Write output to payload buffer.
929-
*rets_size = count;
941+
*rets = *args; // Write output to payload buffer.
942+
*rets_size = input->count;
930943

931-
while (--count) {
944+
while (--input->count) {
932945
**rets++ = (I2C_Receive(I2C_RESPONSE_ACKNOWLEDGE));
933946
}
934947
**rets = I2C_Receive(I2C_RESPONSE_NEGATIVE_ACKNOWLEDGE);
@@ -938,37 +951,40 @@ enum Status I2C_command_read_bulk(
938951
}
939952

940953
enum Status I2C_command_write_bulk(
941-
uint8_t args[],
954+
uint8_t **args,
942955
uint16_t const args_size,
943956
__attribute__ ((unused)) uint8_t **rets,
944957
__attribute__ ((unused)) uint16_t *rets_size
945958
) {
946-
if (args_size < 2) {
959+
struct Input {
960+
uint8_t device;
961+
uint8_t count;
962+
uint8_t data[];
963+
} *input = NULL;
964+
965+
if (args_size < sizeof(struct Input)) {
947966
return E_BAD_ARGSIZE;
948967
}
949968

950-
uint8_t const device = args[0];
951-
uint8_t const count = args[1];
969+
input = (struct Input *)args;
952970

953-
if (args_size != 2U + count) {
971+
if (args_size != sizeof(struct Input) + input->count) {
954972
return E_BAD_ARGSIZE;
955973
}
956974

957-
uint8_t const *data = args + 2;
958-
959975
I2C_StartSignal();
960-
I2C_Transmit(device << 1);
976+
I2C_Transmit(input->device << 1);
961977

962-
for (uint8_t i = 0; i < count; i++) {
963-
I2C_Transmit(*data++);
978+
for (uint8_t i = 0; i < input->count; i++) {
979+
I2C_Transmit(input->data[i]);
964980
}
965981

966982
I2C_StopSignal();
967983
return E_OK;
968984
}
969985

970986
enum Status I2C_command_enable_smbus(
971-
__attribute__ ((unused)) uint8_t args[],
987+
__attribute__ ((unused)) uint8_t **args,
972988
__attribute__ ((unused)) uint16_t const args_size,
973989
__attribute__ ((unused)) uint8_t **rets,
974990
__attribute__ ((unused)) uint16_t *rets_size
@@ -983,7 +999,7 @@ enum Status I2C_command_enable_smbus(
983999
}
9841000

9851001
enum Status I2C_command_disable_smbus(
986-
__attribute__ ((unused)) uint8_t args[],
1002+
__attribute__ ((unused)) uint8_t **args,
9871003
__attribute__ ((unused)) uint16_t const args_size,
9881004
__attribute__ ((unused)) uint8_t **rets,
9891005
__attribute__ ((unused)) uint16_t *rets_size
@@ -998,7 +1014,7 @@ enum Status I2C_command_disable_smbus(
9981014
}
9991015

10001016
enum Status I2C_command_init(
1001-
__attribute__ ((unused)) uint8_t args[],
1017+
__attribute__ ((unused)) uint8_t **args,
10021018
__attribute__ ((unused)) uint16_t const args_size,
10031019
__attribute__ ((unused)) uint8_t **rets,
10041020
__attribute__ ((unused)) uint16_t *rets_size
@@ -1009,16 +1025,18 @@ enum Status I2C_command_init(
10091025
}
10101026

10111027
enum Status I2C_command_pull_down(
1012-
uint8_t args[],
1028+
uint8_t **args,
10131029
uint16_t const args_size,
10141030
__attribute__ ((unused)) uint8_t **rets,
10151031
__attribute__ ((unused)) uint16_t *rets_size
10161032
) {
1017-
if (args_size != 2) {
1033+
uint16_t delay = 0;
1034+
1035+
if (args_size != sizeof(delay)) {
10181036
return E_BAD_ARGSIZE;
10191037
}
10201038

1021-
uint16_t const delay = *(uint16_t const *const)args;
1039+
delay = **(uint16_t **)args;
10221040
I2C_SCL_SetDigitalOutput();
10231041
I2C_SCL_SetLow();
10241042
DELAY_us(delay);

0 commit comments

Comments
 (0)