InfiniTime.git

ref: 0195ece317d15bcba7dfbd74b58bb8dbcd3bca0a

src/drivers/SpiMaster.h


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once
#include <FreeRTOS.h>
#include <cstdint>
#include <cstddef>
#include <array>
#include <atomic>
#include <task.h>

#include "BufferProvider.h"
namespace Pinetime {
  namespace Drivers {
    class SpiMaster {
      public:;
        enum class SpiModule : uint8_t {SPI0, SPI1};
        enum class BitOrder : uint8_t {Msb_Lsb, Lsb_Msb};
        enum class Modes : uint8_t {Mode0, Mode1, Mode2, Mode3};
        enum class Frequencies : uint8_t {Freq8Mhz};
        struct Parameters {
          BitOrder bitOrder;
          Modes mode;
          Frequencies Frequency;
          uint8_t pinSCK;
          uint8_t pinMOSI;
          uint8_t pinMISO;
          uint8_t pinCSN;
        };

        SpiMaster(const SpiModule spi, const Parameters& params);
        SpiMaster(const SpiMaster&) = delete;
        SpiMaster& operator=(const SpiMaster&) = delete;
        SpiMaster(SpiMaster&&) = delete;
        SpiMaster& operator=(SpiMaster&&) = delete;

        bool Init();
        bool Write(const uint8_t* data, size_t size);

        void OnStartedEvent();
        void OnEndEvent();

        void Sleep();
        void Wakeup();

      private:
        void SetupWorkaroundForFtpan58(NRF_SPIM_Type *spim, uint32_t ppi_channel, uint32_t gpiote_channel);
        void DisableWorkaroundForFtpan58(NRF_SPIM_Type *spim, uint32_t ppi_channel, uint32_t gpiote_channel);
        void PrepareTx(const volatile uint32_t bufferAddress, const volatile size_t size);

        NRF_SPIM_Type *  spiBaseAddress;
        uint8_t pinCsn;

        SpiMaster::SpiModule spi;
        SpiMaster::Parameters params;

        volatile bool busy = false;
        volatile uint32_t currentBufferAddr = 0;
        volatile size_t currentBufferSize = 0;
        volatile TaskHandle_t taskToNotify;
    };
  }
}