蓝牙BLE---DA14683的SPI主机通信C源码

demo_spi.h



#include "hw_gpio.h"
#include "hw_uart.h"
#include "hw_spi.h"
#include "osal.h"
#include "resmgmt.h"

#define SPITimeOut ((unsigned int)0x0050)

void demo_spi_init(void);

void spi_write_reg(unsigned char data);
char spi_read_reg(unsigned char Addr);

 

demo_spi.c

#include "hw_spi.h"
#include "demo_spi.h"
#include <stdint.h>
#include "stdio.h"


void spi_pin_config()
{
        hw_gpio_set_pin_function(HW_GPIO_PORT_3,HW_GPIO_PIN_7,HW_GPIO_MODE_OUTPUT_PUSH_PULL,\
                HW_GPIO_FUNC_SPI_CLK);
        hw_gpio_set_pin_function(HW_GPIO_PORT_1,HW_GPIO_PIN_4,HW_GPIO_MODE_OUTPUT_PUSH_PULL,\
                HW_GPIO_FUNC_SPI_DO);
        hw_gpio_set_pin_function(HW_GPIO_PORT_1,HW_GPIO_PIN_7,HW_GPIO_MODE_INPUT_PULLUP,\
                HW_GPIO_FUNC_SPI_DI);
        hw_gpio_set_pin_function(HW_GPIO_PORT_3,HW_GPIO_PIN_5,HW_GPIO_MODE_OUTPUT_PUSH_PULL,\
                HW_GPIO_FUNC_GPIO);
}


void demo_spi_init(void)
{
        spi_config SPI1;
        spi_pin_config();
        //multiplexing pin
        //configure SPI module
        hw_spi_enable(HW_SPI1,0);
        SPI1.cs_pad.port=HW_GPIO_PORT_3;
        SPI1.cs_pad.pin=HW_GPIO_PIN_5;
        SPI1.word_mode = HW_SPI_MODE_MASTER;
        SPI1.smn_role = HW_SPI_WORD_8BIT;
        SPI1.polarity_mode = HW_SPI_POL_LOW;
        SPI1.phase_mode = HW_SPI_PHA_MODE_0;
        SPI1.xtal_freq = HW_SPI_FREQ_DIV_8;
        SPI1.fifo_mode = HW_SPI_FIFO_NONE;
        hw_spi_init(HW_SPI1,&SPI1);
}

void spi_write_reg(unsigned char data)
{
        unsigned int TIMEOUT = SPITimeOut;
        while(hw_spi_is_busy(HW_SPI1))
        {
          TIMEOUT--;
          if(TIMEOUT==0)
           {
              printf("write_err!\r\n");
              while(hw_uart_is_busy(HW_UART1));
              return ;
            }
         }
      hw_spi_fifo_write8(HW_SPI1,data);
}

char spi_read_reg(unsigned char Addr)
{
        unsigned int TIMEOUT = SPITimeOut;
        char Recchar;
        while(hw_spi_is_busy(HW_SPI1))
        {
          TIMEOUT--;
          if(TIMEOUT==0)
           {
              printf("read_err!\r\n");
              while(hw_uart_is_busy(HW_UART1));
              return 0;
            }
         }
        while (!hw_spi_get_interrupt_status(HW_SPI1)) {
        }
        Recchar = hw_spi_fifo_read8(HW_SPI1);
        hw_spi_clear_interrupt(HW_SPI1);
         return Recchar;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jim天河

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值