Seeed / eMPL_MPU
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_spi.c Source File

mbed_spi.c

00001 
00002 #include <stdio.h>
00003 #include "spi_api.h"
00004 #include "gpio_api.h"
00005 
00006 #ifdef TARGET_MCU_NRF51822
00007 #include "nrf51.h"
00008 static PinName mbed_spi_mosi;
00009 static PinName mbed_spi_miso;
00010 static PinName mbed_spi_sclk;
00011 static PinName mbed_spi_cs;
00012 static uint32_t mbed_spi_mosi_cnf;
00013 static uint32_t mbed_spi_miso_cnf;
00014 static uint32_t mbed_spi_sclk_cnf;
00015 static uint32_t mbed_spi_cs_cnf;
00016 #endif
00017 
00018 static spi_t mbed_spi_object;
00019 static gpio_t mbed_cs_object;
00020 
00021 
00022 
00023 void mbed_spi_init(PinName mosi, PinName miso, PinName sclk, PinName cs)
00024 {
00025     spi_init(&mbed_spi_object, mosi, miso, sclk, NC);
00026     spi_format(&mbed_spi_object, 8, 0, 0);
00027     spi_frequency(&mbed_spi_object, 1000000);
00028 
00029     gpio_init_out(&mbed_cs_object, cs);
00030     gpio_write(&mbed_cs_object, 1);
00031 
00032 #ifdef TARGET_MCU_NRF51822
00033     mbed_spi_mosi = mosi;
00034     mbed_spi_miso = miso;
00035     mbed_spi_sclk = sclk;
00036     mbed_spi_cs   = cs;
00037     
00038     mbed_spi_mosi_cnf = NRF_GPIO->PIN_CNF[mosi];
00039     mbed_spi_miso_cnf = NRF_GPIO->PIN_CNF[miso];
00040     mbed_spi_sclk_cnf = NRF_GPIO->PIN_CNF[sclk];
00041     mbed_spi_cs_cnf = NRF_GPIO->PIN_CNF[cs];
00042 #endif
00043 }
00044 
00045 int mbed_spi_write(unsigned char reg_addr,
00046                    unsigned char length,
00047                    unsigned char const *data)
00048 {
00049     int i;
00050 
00051     gpio_write(&mbed_cs_object, 0);
00052     spi_master_write(&mbed_spi_object, reg_addr);
00053     for (i = 0; i < length; i++) {
00054         spi_master_write(&mbed_spi_object, data[i]);
00055     }
00056     gpio_write(&mbed_cs_object, 1);
00057     return 0;
00058 }
00059 
00060 int mbed_spi_read(unsigned char reg_addr,
00061                   unsigned char length,
00062                   unsigned char *data)
00063 {
00064     int i;
00065 
00066     gpio_write(&mbed_cs_object, 0);
00067     spi_master_write(&mbed_spi_object, reg_addr | 0x80);
00068     for (i = 0; i < length; i++) {
00069         data[i] = spi_master_write(&mbed_spi_object, 0xff);
00070     }
00071 
00072     gpio_write(&mbed_cs_object, 1);
00073     return 0;
00074 }
00075 
00076 void mbed_spi_enable(void)
00077 {
00078 #if defined(TARGET_MCU_NRF51822) && !defined(DEBUG)
00079     // NRF_GPIO->PIN_CNF[mbed_spi_mosi] = mbed_spi_mosi_cnf;
00080     // NRF_GPIO->PIN_CNF[mbed_spi_miso] = mbed_spi_miso_cnf;
00081     // NRF_GPIO->PIN_CNF[mbed_spi_sclk] = mbed_spi_sclk_cnf;
00082     // NRF_GPIO->PIN_CNF[mbed_spi_cs] = mbed_spi_cs_cnf;
00083     
00084     mbed_spi_object.spi->ENABLE = 1;
00085 #endif
00086 }
00087 
00088 void mbed_spi_disable(void)
00089 {
00090 #if defined(TARGET_MCU_NRF51822) && !defined(DEBUG)
00091     mbed_spi_mosi_cnf = NRF_GPIO->PIN_CNF[mbed_spi_mosi];
00092     mbed_spi_miso_cnf = NRF_GPIO->PIN_CNF[mbed_spi_miso];
00093     mbed_spi_sclk_cnf = NRF_GPIO->PIN_CNF[mbed_spi_sclk];
00094     mbed_spi_cs_cnf = NRF_GPIO->PIN_CNF[mbed_spi_cs];
00095     
00096     mbed_spi_object.spi->ENABLE = 0;
00097     
00098     // NRF_GPIO->PIN_CNF[mbed_spi_mosi] = 2;
00099     // NRF_GPIO->PIN_CNF[mbed_spi_miso] = 2;
00100     // NRF_GPIO->PIN_CNF[mbed_spi_sclk] = 2;
00101     // NRF_GPIO->PIN_CNF[mbed_spi_cs] = 2;
00102 #endif
00103 }