一、配置SPI引脚
本例中SPI只连接了一个设备,即国产128kB EEPROM上海贝岭BL25CMIA。
NSS脚采用软件控制。
其它引脚通过查看手册可知其连接的SPI外设为SPI1。
/* SPI port definition for master */
#define SPI_NSS_PORT (GPIO_PORT_B)
#define SPI_NSS_PIN (GPIO_PIN_12)
#define SPI_SCK_PORT (GPIO_PORT_B)
#define SPI_SCK_PIN (GPIO_PIN_13)
#define SPI_SCK_FUNC (GPIO_FUNC_40_SPI1_SCK)
#define SPI_MOSI_PORT (GPIO_PORT_B)
#define SPI_MOSI_PIN (GPIO_PIN_15)
#define SPI_MOSI_FUNC (GPIO_FUNC_41_SPI1_MOSI)
#define SPI_MISO_PORT (GPIO_PORT_B)
#define SPI_MISO_PIN (GPIO_PIN_14)
#define SPI_MISO_FUNC (GPIO_FUNC_42_SPI1_MISO)
stc_gpio_init_t stcGpioInit;
/* Port configurate */
(void)GPIO_StructInit(&stcGpioInit);
/* High driving capacity for output pin. */
stcGpioInit.u16PinDir = PIN_DIR_OUT;
stcGpioInit.u16PinDrv = PIN_DRV_HIGH;
stcGpioInit.u16PinState = PIN_STATE_SET;
(void)GPIO_Init(SPI_NSS_PORT, SPI_NSS_PIN, &stcGpioInit);
(void)GPIO_StructInit(&stcGpioInit);
stcGpioInit.u16PinDrv = PIN_DRV_HIGH;
(void)GPIO_Init(SPI_SCK_PORT, SPI_SCK_PIN, &stcGpioInit);
(void)GPIO_Init(SPI_MOSI_PORT, SPI_MOSI_PIN, &stcGpioInit);
/* CMOS input for input pin */
stcGpioInit.u16PinDrv = PIN_DRV_LOW;
stcGpioInit.u16PinIType = PIN_ITYPE_CMOS;
(void)GPIO_Init(SPI_MISO_PORT, SPI_MISO_PIN, &stcGpioInit);
/* Configure SPI Port function for master */
GPIO_SetFunc(SPI_SCK_PORT, SPI_SCK_PIN, SPI_SCK_FUNC, PIN_SUBFUNC_DISABLE);
GPIO_SetFunc(SPI_MOSI_PORT, SPI_MOSI_PIN, SPI_MOSI_FUNC, PIN_SUBFUNC_DISABLE);
GPIO_SetFunc(SPI_MISO_PORT, SPI_MISO_PIN, SPI_MISO_FUNC, PIN_SUBFUNC_DISABLE);
|