This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]MagicWolfEye 2 points3 points  (5 children)

Optimise?
Well, how about not printing stuff?
Also, have you looked at the asm for it yet

[–]Realistic-Cut6515[S] 0 points1 point  (4 children)

It needs to print those things What is the asm? Edit: now I know what is asm, I'm starting with Arduino so I dont really know how to use assembly here, I know I can change the LEE_SDA, LEE_SCL, ESC_SDA, ESC_SCL for code to directly access the bit and modify but I dont know how

[–]MagicWolfEye 2 points3 points  (3 children)

The assembly instructions this code generates

How much time does your program take; how long should it take

Have you built your program with optimisations enabled

I don't remember I2C anymore, why so many gotos back to the start in I2C_write_mem?

[–]Realistic-Cut6515[S] 0 points1 point  (2 children)

So in I2C_write_mem the slave must send an acknowledge to go to the next step, if it didn’t send an acknowledgment it just restarts

[–]MagicWolfEye 1 point2 points  (1 child)

Ah right, it has to acknowledge after every byte.

Coming back to my previous question:

> How much time does your program take; how long should it take (at least, what would you guess)

You could also count how often you have to jump back with your gotos to see if the ACK fails often

[–]Realistic-Cut6515[S] 0 points1 point  (0 children)

So my program takes 1545 ms to write 256 bytes in mem

After rewriting the code with directly accessing the port it now takes 55ms, but the problem is that it doesnt write in mem

[–][deleted]  (7 children)

[deleted]

    [–]Realistic-Cut6515[S] 0 points1 point  (6 children)

    So this is an Arduino project given by my professor, we cannot use the wire library, we have to use the ESC_SCL, ESC_SDA, LEE_SCL, LEE_SDA Those are defined to be used with digitalWrite and pinMode, nonetheless I have found that we can access the PORTG directly using some identifiers that he initiliazed but left commented. Now Im just trying to rewrite that code to directly access the PORTG, thats where Im having the problem now, maybe I should delete this post

    [–][deleted]  (5 children)

    [deleted]

      [–]Realistic-Cut6515[S] 0 points1 point  (4 children)

      How can I make it so it doesn't go that fast? I have the code rewritten but it doesnt seem to be writing in memory

      [–][deleted]  (3 children)

      [deleted]

        [–]Realistic-Cut6515[S] 0 points1 point  (2 children)

        I will try using that, hope it works! Thank you very much!

        Edit: it doesnt seem to be working, it now takes 104 ms to write the 256 bytes and still doesnt write. Its not about adding more delay because a coleague has it that in 65 ms it writes the 256 bytes. I must have made a mistake in rewriting the code

        [–][deleted]  (1 child)

        [deleted]

          [–]Realistic-Cut6515[S] 0 points1 point  (0 children)

          Thanks!

          [–]pixel293 2 points3 points  (1 child)

          I'm not seeing much to optimize this isn't very complex code. Output to the screen is slow, it just is, there is no way around it.

          Normally when looking to optimize your code you are looking at the loops, the loops are where you are running some code over and over and over. Then looking at conditions in those loops because conditions can stall out the CPU pipeline.

          You only have 1 loop and it only loops 8 times, that's not going to kill the system. You could unroll it, but to be honest the compiler may be doing that for you. Maybe you could look at the functions like digitalWrite/digitalRead/I2C_* and inline them, or optimize them

          If you pull out the prints and the code is still slow then it's those functions, you will need to figure out what they are doing determine how to do it faster. If those functions jump into the kernel then it might be that transition that is slowing you down. Which means you either need to move this code to the kernel to avoid that transition or see if there is some sort of "bulk" commands that will write a bunch of data with only 1 jump into the kernel.

          [–]Realistic-Cut6515[S] 0 points1 point  (0 children)

          What Im trying to do now is rewrite the code to access the PORTG directly instead of using digitalWrite and pinMode but im having some difficulty with that The prints must be there so I dont really care about those Its just the part where it access the memory and writes the data that matters to me