Added a GPIO to power on/off for external I2C sensor(s) (with LEDs)
Dependencies: UniGraphic mbed vt100
18-Jun-2018 外部センサの電源オン・オフ機能は下位互換の為に無効になっていました。 この版で再度有効にしました。
af_utils/mbedSPI.cpp@0:846e2321c637, 2018-04-13 (annotated)
- Committer:
- Rhyme
- Date:
- Fri Apr 13 04:19:23 2018 +0000
- Revision:
- 0:846e2321c637
power to color sensor on/off test OK. Currently the function is disabled.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:846e2321c637 | 1 | #include "mbed.h" |
Rhyme | 0:846e2321c637 | 2 | #include "vt100.h" |
Rhyme | 0:846e2321c637 | 3 | #include "afSPI.h" |
Rhyme | 0:846e2321c637 | 4 | #include "mbedSPI.h" |
Rhyme | 0:846e2321c637 | 5 | |
Rhyme | 0:846e2321c637 | 6 | #if defined (TARGET_KL25Z) || defined (TARGET_TEENSY3_1) |
Rhyme | 0:846e2321c637 | 7 | #ifndef SPI0_C1 |
Rhyme | 0:846e2321c637 | 8 | #define SPI0_C1 (*(uint8_t *)0x40076000) |
Rhyme | 0:846e2321c637 | 9 | #endif |
Rhyme | 0:846e2321c637 | 10 | #endif |
Rhyme | 0:846e2321c637 | 11 | /* |
Rhyme | 0:846e2321c637 | 12 | class mbedSPI : public afSPI { |
Rhyme | 0:846e2321c637 | 13 | public: |
Rhyme | 0:846e2321c637 | 14 | mbedSPI(PinName mosi, PinName miso, PinName sckl, PinName cs) ; |
Rhyme | 0:846e2321c637 | 15 | virtual void begin() ; |
Rhyme | 0:846e2321c637 | 16 | virtual void beginSPI() ; |
Rhyme | 0:846e2321c637 | 17 | virtual void endSPI() ; |
Rhyme | 0:846e2321c637 | 18 | virtual void transfer(char *bytes,int len) ; |
Rhyme | 0:846e2321c637 | 19 | private: |
Rhyme | 0:846e2321c637 | 20 | SPI _spi ; |
Rhyme | 0:846e2321c637 | 21 | DigitalOut _cs ; |
Rhyme | 0:846e2321c637 | 22 | } ; |
Rhyme | 0:846e2321c637 | 23 | */ |
Rhyme | 0:846e2321c637 | 24 | |
Rhyme | 0:846e2321c637 | 25 | mbedSPI::mbedSPI(PinName mosi, PinName miso, PinName sckl, PinName cs) : |
Rhyme | 0:846e2321c637 | 26 | _spi(mosi, miso, sckl), _cs(cs, 1) |
Rhyme | 0:846e2321c637 | 27 | { |
Rhyme | 0:846e2321c637 | 28 | _spi.format(8, 0) ; |
Rhyme | 0:846e2321c637 | 29 | _spi.frequency(1000000) ; /* 1MHz */ |
Rhyme | 0:846e2321c637 | 30 | |
Rhyme | 0:846e2321c637 | 31 | #if defined (TARGET_KL25Z) || defined (TARGET_TEENSY3_1) |
Rhyme | 0:846e2321c637 | 32 | #ifndef SPI0_C1 |
Rhyme | 0:846e2321c637 | 33 | #define SPI0_C1 (*(uint8_t *)0x40076000) |
Rhyme | 0:846e2321c637 | 34 | #endif |
Rhyme | 0:846e2321c637 | 35 | // SPI0_C1 |= 0x01 ; /* LSB First */ |
Rhyme | 0:846e2321c637 | 36 | // SPI0_C1 &= 0xFE ; /* MSB First */ |
Rhyme | 0:846e2321c637 | 37 | #endif |
Rhyme | 0:846e2321c637 | 38 | |
Rhyme | 0:846e2321c637 | 39 | } |
Rhyme | 0:846e2321c637 | 40 | |
Rhyme | 0:846e2321c637 | 41 | void mbedSPI::begin(void) |
Rhyme | 0:846e2321c637 | 42 | { |
Rhyme | 0:846e2321c637 | 43 | } |
Rhyme | 0:846e2321c637 | 44 | |
Rhyme | 0:846e2321c637 | 45 | void mbedSPI::beginSPI() |
Rhyme | 0:846e2321c637 | 46 | { |
Rhyme | 0:846e2321c637 | 47 | // printf("+") ; fflush(stdout) ; |
Rhyme | 0:846e2321c637 | 48 | _cs = 0 ; |
Rhyme | 0:846e2321c637 | 49 | SPI0_C1 |= 0x01 ; /* LSB First */ |
Rhyme | 0:846e2321c637 | 50 | wait_us(1) ; |
Rhyme | 0:846e2321c637 | 51 | } |
Rhyme | 0:846e2321c637 | 52 | |
Rhyme | 0:846e2321c637 | 53 | void mbedSPI::endSPI() |
Rhyme | 0:846e2321c637 | 54 | { |
Rhyme | 0:846e2321c637 | 55 | _cs = 1 ; |
Rhyme | 0:846e2321c637 | 56 | SPI0_C1 &= 0xFE ; /* MSB First */ |
Rhyme | 0:846e2321c637 | 57 | wait_us(1) ; |
Rhyme | 0:846e2321c637 | 58 | // printf("-") ; fflush(stdout) ; |
Rhyme | 0:846e2321c637 | 59 | } |
Rhyme | 0:846e2321c637 | 60 | |
Rhyme | 0:846e2321c637 | 61 | /** |
Rhyme | 0:846e2321c637 | 62 | * on 17-Jan-2018 disable/enable irq added |
Rhyme | 0:846e2321c637 | 63 | * before and after of each _spi.writes |
Rhyme | 0:846e2321c637 | 64 | */ |
Rhyme | 0:846e2321c637 | 65 | void mbedSPI::transfer(char *bytes, int len) |
Rhyme | 0:846e2321c637 | 66 | { |
Rhyme | 0:846e2321c637 | 67 | int i ; |
Rhyme | 0:846e2321c637 | 68 | for (i = 0 ; i < len ; i++ ) { |
Rhyme | 0:846e2321c637 | 69 | __disable_irq() ; // Disable Interrupts |
Rhyme | 0:846e2321c637 | 70 | bytes[i] = _spi.write(bytes[i]) ; |
Rhyme | 0:846e2321c637 | 71 | __enable_irq() ; // Enable Interrupts |
Rhyme | 0:846e2321c637 | 72 | } |
Rhyme | 0:846e2321c637 | 73 | } |
Rhyme | 0:846e2321c637 | 74 |