initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:49:25 2016 +0000
Revision:
1:9d3b497333c0
Parent:
0:638edba3adf6
use mbed lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:638edba3adf6 1
yihui 0:638edba3adf6 2 #include <stdio.h>
yihui 0:638edba3adf6 3 #include "spi_api.h"
yihui 0:638edba3adf6 4 #include "gpio_api.h"
yihui 0:638edba3adf6 5
yihui 0:638edba3adf6 6 #ifdef TARGET_MCU_NRF51822
yihui 0:638edba3adf6 7 #include "nrf51.h"
yihui 0:638edba3adf6 8 static PinName mbed_spi_mosi;
yihui 0:638edba3adf6 9 static PinName mbed_spi_miso;
yihui 0:638edba3adf6 10 static PinName mbed_spi_sclk;
yihui 0:638edba3adf6 11 static PinName mbed_spi_cs;
yihui 0:638edba3adf6 12 static uint32_t mbed_spi_mosi_cnf;
yihui 0:638edba3adf6 13 static uint32_t mbed_spi_miso_cnf;
yihui 0:638edba3adf6 14 static uint32_t mbed_spi_sclk_cnf;
yihui 0:638edba3adf6 15 static uint32_t mbed_spi_cs_cnf;
yihui 0:638edba3adf6 16 #endif
yihui 0:638edba3adf6 17
yihui 0:638edba3adf6 18 static spi_t mbed_spi_object;
yihui 0:638edba3adf6 19 static gpio_t mbed_cs_object;
yihui 0:638edba3adf6 20
yihui 0:638edba3adf6 21
yihui 0:638edba3adf6 22
yihui 0:638edba3adf6 23 void mbed_spi_init(PinName mosi, PinName miso, PinName sclk, PinName cs)
yihui 0:638edba3adf6 24 {
yihui 0:638edba3adf6 25 spi_init(&mbed_spi_object, mosi, miso, sclk, NC);
yihui 0:638edba3adf6 26 spi_format(&mbed_spi_object, 8, 0, 0);
yihui 0:638edba3adf6 27 spi_frequency(&mbed_spi_object, 1000000);
yihui 0:638edba3adf6 28
yihui 0:638edba3adf6 29 gpio_init_out(&mbed_cs_object, cs);
yihui 0:638edba3adf6 30 gpio_write(&mbed_cs_object, 1);
yihui 0:638edba3adf6 31
yihui 0:638edba3adf6 32 #ifdef TARGET_MCU_NRF51822
yihui 0:638edba3adf6 33 mbed_spi_mosi = mosi;
yihui 0:638edba3adf6 34 mbed_spi_miso = miso;
yihui 0:638edba3adf6 35 mbed_spi_sclk = sclk;
yihui 0:638edba3adf6 36 mbed_spi_cs = cs;
yihui 0:638edba3adf6 37
yihui 0:638edba3adf6 38 mbed_spi_mosi_cnf = NRF_GPIO->PIN_CNF[mosi];
yihui 0:638edba3adf6 39 mbed_spi_miso_cnf = NRF_GPIO->PIN_CNF[miso];
yihui 0:638edba3adf6 40 mbed_spi_sclk_cnf = NRF_GPIO->PIN_CNF[sclk];
yihui 0:638edba3adf6 41 mbed_spi_cs_cnf = NRF_GPIO->PIN_CNF[cs];
yihui 0:638edba3adf6 42 #endif
yihui 0:638edba3adf6 43 }
yihui 0:638edba3adf6 44
yihui 0:638edba3adf6 45 int mbed_spi_write(unsigned char reg_addr,
yihui 0:638edba3adf6 46 unsigned char length,
yihui 0:638edba3adf6 47 unsigned char const *data)
yihui 0:638edba3adf6 48 {
yihui 0:638edba3adf6 49 int i;
yihui 0:638edba3adf6 50
yihui 0:638edba3adf6 51 gpio_write(&mbed_cs_object, 0);
yihui 0:638edba3adf6 52 spi_master_write(&mbed_spi_object, reg_addr);
yihui 0:638edba3adf6 53 for (i = 0; i < length; i++) {
yihui 0:638edba3adf6 54 spi_master_write(&mbed_spi_object, data[i]);
yihui 0:638edba3adf6 55 }
yihui 0:638edba3adf6 56 gpio_write(&mbed_cs_object, 1);
yihui 0:638edba3adf6 57 return 0;
yihui 0:638edba3adf6 58 }
yihui 0:638edba3adf6 59
yihui 0:638edba3adf6 60 int mbed_spi_read(unsigned char reg_addr,
yihui 0:638edba3adf6 61 unsigned char length,
yihui 0:638edba3adf6 62 unsigned char *data)
yihui 0:638edba3adf6 63 {
yihui 0:638edba3adf6 64 int i;
yihui 0:638edba3adf6 65
yihui 0:638edba3adf6 66 gpio_write(&mbed_cs_object, 0);
yihui 0:638edba3adf6 67 spi_master_write(&mbed_spi_object, reg_addr | 0x80);
yihui 0:638edba3adf6 68 for (i = 0; i < length; i++) {
yihui 0:638edba3adf6 69 data[i] = spi_master_write(&mbed_spi_object, 0xff);
yihui 0:638edba3adf6 70 }
yihui 0:638edba3adf6 71
yihui 0:638edba3adf6 72 gpio_write(&mbed_cs_object, 1);
yihui 0:638edba3adf6 73 return 0;
yihui 0:638edba3adf6 74 }
yihui 0:638edba3adf6 75
yihui 0:638edba3adf6 76 void mbed_spi_enable(void)
yihui 0:638edba3adf6 77 {
yihui 0:638edba3adf6 78 #if defined(TARGET_MCU_NRF51822) && !defined(DEBUG)
yihui 0:638edba3adf6 79 // NRF_GPIO->PIN_CNF[mbed_spi_mosi] = mbed_spi_mosi_cnf;
yihui 0:638edba3adf6 80 // NRF_GPIO->PIN_CNF[mbed_spi_miso] = mbed_spi_miso_cnf;
yihui 0:638edba3adf6 81 // NRF_GPIO->PIN_CNF[mbed_spi_sclk] = mbed_spi_sclk_cnf;
yihui 0:638edba3adf6 82 // NRF_GPIO->PIN_CNF[mbed_spi_cs] = mbed_spi_cs_cnf;
yihui 0:638edba3adf6 83
yihui 0:638edba3adf6 84 mbed_spi_object.spi->ENABLE = 1;
yihui 0:638edba3adf6 85 #endif
yihui 0:638edba3adf6 86 }
yihui 0:638edba3adf6 87
yihui 0:638edba3adf6 88 void mbed_spi_disable(void)
yihui 0:638edba3adf6 89 {
yihui 0:638edba3adf6 90 #if defined(TARGET_MCU_NRF51822) && !defined(DEBUG)
yihui 0:638edba3adf6 91 mbed_spi_mosi_cnf = NRF_GPIO->PIN_CNF[mbed_spi_mosi];
yihui 0:638edba3adf6 92 mbed_spi_miso_cnf = NRF_GPIO->PIN_CNF[mbed_spi_miso];
yihui 0:638edba3adf6 93 mbed_spi_sclk_cnf = NRF_GPIO->PIN_CNF[mbed_spi_sclk];
yihui 0:638edba3adf6 94 mbed_spi_cs_cnf = NRF_GPIO->PIN_CNF[mbed_spi_cs];
yihui 0:638edba3adf6 95
yihui 0:638edba3adf6 96 mbed_spi_object.spi->ENABLE = 0;
yihui 0:638edba3adf6 97
yihui 0:638edba3adf6 98 // NRF_GPIO->PIN_CNF[mbed_spi_mosi] = 2;
yihui 0:638edba3adf6 99 // NRF_GPIO->PIN_CNF[mbed_spi_miso] = 2;
yihui 0:638edba3adf6 100 // NRF_GPIO->PIN_CNF[mbed_spi_sclk] = 2;
yihui 0:638edba3adf6 101 // NRF_GPIO->PIN_CNF[mbed_spi_cs] = 2;
yihui 0:638edba3adf6 102 #endif
yihui 0:638edba3adf6 103 }