Added a GPIO to power on/off for external I2C sensor(s) (with LEDs)
Dependencies: UniGraphic mbed vt100
mbedSPI.cpp
00001 #include "mbed.h" 00002 #include "vt100.h" 00003 #include "afSPI.h" 00004 #include "mbedSPI.h" 00005 00006 #if defined (TARGET_KL25Z) || defined (TARGET_TEENSY3_1) 00007 #ifndef SPI0_C1 00008 #define SPI0_C1 (*(uint8_t *)0x40076000) 00009 #endif 00010 #endif 00011 /* 00012 class mbedSPI : public afSPI { 00013 public: 00014 mbedSPI(PinName mosi, PinName miso, PinName sckl, PinName cs) ; 00015 virtual void begin() ; 00016 virtual void beginSPI() ; 00017 virtual void endSPI() ; 00018 virtual void transfer(char *bytes,int len) ; 00019 private: 00020 SPI _spi ; 00021 DigitalOut _cs ; 00022 } ; 00023 */ 00024 00025 mbedSPI::mbedSPI(PinName mosi, PinName miso, PinName sckl, PinName cs) : 00026 _spi(mosi, miso, sckl), _cs(cs, 1) 00027 { 00028 _spi.format(8, 0) ; 00029 _spi.frequency(1000000) ; /* 1MHz */ 00030 00031 #if defined (TARGET_KL25Z) || defined (TARGET_TEENSY3_1) 00032 #ifndef SPI0_C1 00033 #define SPI0_C1 (*(uint8_t *)0x40076000) 00034 #endif 00035 // SPI0_C1 |= 0x01 ; /* LSB First */ 00036 // SPI0_C1 &= 0xFE ; /* MSB First */ 00037 #endif 00038 00039 } 00040 00041 void mbedSPI::begin(void) 00042 { 00043 } 00044 00045 void mbedSPI::beginSPI() 00046 { 00047 // printf("+") ; fflush(stdout) ; 00048 _cs = 0 ; 00049 SPI0_C1 |= 0x01 ; /* LSB First */ 00050 wait_us(1) ; 00051 } 00052 00053 void mbedSPI::endSPI() 00054 { 00055 _cs = 1 ; 00056 SPI0_C1 &= 0xFE ; /* MSB First */ 00057 wait_us(1) ; 00058 // printf("-") ; fflush(stdout) ; 00059 } 00060 00061 /** 00062 * on 17-Jan-2018 disable/enable irq added 00063 * before and after of each _spi.writes 00064 */ 00065 void mbedSPI::transfer(char *bytes, int len) 00066 { 00067 int i ; 00068 for (i = 0 ; i < len ; i++ ) { 00069 __disable_irq() ; // Disable Interrupts 00070 bytes[i] = _spi.write(bytes[i]) ; 00071 __enable_irq() ; // Enable Interrupts 00072 } 00073 } 00074
Generated on Wed Jul 13 2022 12:25:10 by
1.7.2
La Suno