• AVR Freaks

Conceptual question about saving/restoring state.

Author
Aiden.Morrison
Super Member
  • Total Posts : 729
  • Reward points : 0
  • Joined: 2005/02/25 11:18:31
  • Location: Canada
  • Status: offline
2011/04/06 09:32:26 (permalink)
0

Conceptual question about saving/restoring state.

Hi there,

I'm trying to find some ways to squeeze more efficiency out of the FAT-FS implementation I posted last month.  Basically it works great for throwing data to and fro but it's blocking during operations and spends a lot of them just waiting for the SPI bus to send/receive data.

I toyed with the idea of adapting it to be interrupt driven in the traditional sense, but felt this is a little bit beyond my abilities and patience.

What I think is needed is threading, but I'm not comfortable learning to work with an RTOS just yet, and wonder if an intermediate solution is possible.

I'm hoping someone here can advise me if this is feasible.

1) Call the FAT-FS write commands as usual.

2) When the SPI sector write starts, initiate a 16-byte (or DMA for 512 byte sector) transfer.

3) After the command to do so is given, store the state of the processor and return from the function.

4) When the SPI interrupt fires indicating the completion of the transfer, restore the previously stored state to effectively return to that point in the FAT-FS write command.

5) when done (either sending the next sector, or finished the complete write operation) save the new state and restore the context saved by the isr handler.

From my days of assembly on the 6811 context saving was something I never had to do for interrupts, but from the very brief experience I have with the PIC18 this sounds possible - am I overlooking anything fundamental here?

Thanks in advance.



#1

5 Replies Related Threads

    yuantuh
    Super Member
    • Total Posts : 204
    • Reward points : 0
    • Joined: 2009/01/30 04:39:57
    • Location: 0
    • Status: offline
    Re:Conceptual question about saving/restoring state. 2011/04/06 16:51:05 (permalink)
    0
    It is almost impossiable to achieve what you want without using RTOS. DMA is not good for none streaming data, such I2C, SPI data transaction.
    #2
    Aiden.Morrison
    Super Member
    • Total Posts : 729
    • Reward points : 0
    • Joined: 2005/02/25 11:18:31
    • Location: Canada
    • Status: offline
    Re:Conceptual question about saving/restoring state. 2011/04/06 23:40:54 (permalink)
    0
    Hi there yuantuh,

    I understand that DMA isn't useful for complex transactions, but it should be useful for the 512 byte block transfers done during the write to an SD card yes?

    Before trying to manipulate context save/restore I need to do more reading and take a look at the assembly code generated for interrupt handlers, but I'm hoping it will be possible to do this to get a simple form of 2 threads going.

    Do you have any insights on the proper sequence of instructions to preserve the processor state?

    Thanks in advance.
    #3
    yuantuh
    Super Member
    • Total Posts : 204
    • Reward points : 0
    • Joined: 2009/01/30 04:39:57
    • Location: 0
    • Status: offline
    Re:Conceptual question about saving/restoring state. 2011/04/07 16:59:49 (permalink)
    0
    Hi Aiden,

    Transfering data block has no problem using DMA, but before writing data block to SD card, the corresponding command(s) has to be sent first, which might delay some time due to neccessary dummy read involved. I wonder DMA effeciency for SPI data trasaction. However, I never use DMA for SPI, So maybe I am wrong.

    For context save/restore implemetation, I think you can read RTOS source code, say FreeRTOS, and ignore queue, semaphore, mutex to see whether you can get there.
    #4
    aschen0866
    Super Member
    • Total Posts : 4495
    • Reward points : 0
    • Joined: 2006/01/08 22:18:32
    • Location: San Diego
    • Status: offline
    Re:Conceptual question about saving/restoring state. 2011/04/07 18:12:07 (permalink)
    0
    Aiden.Morrison
    ..., but I'm hoping it will be possible to do this to get a simple form of 2 threads going.
    ...

    Perhaps the simplest approach to achieve "2 threads" as you mentioned is to use the software interrupt feature in a PIC32. Take a look PIC32 Peripheral Libraries for MPLAB C32 Compiler Section 7.5.


    #5
    Aiden.Morrison
    Super Member
    • Total Posts : 729
    • Reward points : 0
    • Joined: 2005/02/25 11:18:31
    • Location: Canada
    • Status: offline
    Re:Conceptual question about saving/restoring state. 2011/04/08 13:28:23 (permalink)
    0
    Thanks for the input all - between the ideas posted here I should be able to work something out.  I will probably end up testing an RTOS next time since that seems to be one of the better solutions to this type of problem.
    #6
    Jump to:
    © 2019 APG vNext Commercial Version 4.5