rt_err_t ads7843_init(const char * name, const char * spi_device_name) { rt_spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name); if(rt_spi_device == RT_NULL) { rt_kprintf("spi device %s not found!\r\n", spi_device_name); return -RT_ENOSYS; } /* config spi */ { struct rt_spi_configuration cfg; cfg.data_width = 8; cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible: Mode 0 and Mode 3 */ cfg.max_hz = 2 * 1000 * 1000; rt_spi_configure(rt_spi_device, &cfg); } /* register device */ ads7843_device.type = RT_Device_Class_Block; ads7843_device.init = RT_NULL; ads7843_device.open = RT_NULL; ads7843_device.close = RT_NULL; ads7843_device.read = ads7843_read; ads7843_device.write = RT_NULL; ads7843_device.control = RT_NULL; /* no private */ ads7843_device.user_data = RT_NULL; rt_device_register(&ads7843_device, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE); return RT_EOK; }
rt_err_t rt_hw_nRF24L01_init(const char * spi_device_name){ RF24L01_IO_Init(); rt_spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name); if(rt_spi_device == RT_NULL) { FLASH_TRACE("spi device %s not found!\r\n", spi_device_name); return -RT_ENOSYS; } /* config spi */ { struct rt_spi_configuration cfg; cfg.data_width = 8; cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible: Mode 0 and Mode 3 */ cfg.max_hz = 9 * 1000 * 1000; /* 9M */ //cfg.max_hz = 50 * 1000 * 1000; /* 9M */ rt_spi_configure(rt_spi_device, &cfg); } // SPI_RF24L01_RW(0x5a); if(RF24L01_Check() == RT_EOK) { //这里直接启一个线程来处理相关数据。 rt_kprintf("nRF24L01 is connected !\n"); return RT_EOK; } else { rt_kprintf("nRF24L01 is not connected !\n"); return RT_ERROR ; //MCU与void RF24L01_Init(void)不正常连接 } }
rt_err_t m74hc595_init(const char *m74hc595_device_name, const char *spi_device_name) { struct rt_spi_device *rt_spi_device; rt_spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name); if (rt_spi_device == RT_NULL) { rt_kprintf("spi device %s not found!\r\n", spi_device_name); return -RT_ENOSYS; } spi_m74hc595.rt_spi_device = rt_spi_device; /* config spi */ { struct rt_spi_configuration cfg; cfg.data_width = 8; cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible Modes 0 and 3 */ cfg.max_hz = 66000000; /* Atmel RapidS Serial Interface: 66MHz Maximum Clock Frequency */ rt_spi_configure(spi_m74hc595.rt_spi_device, &cfg); } /* register device */ spi_m74hc595.m74hc595_device.type = RT_Device_Class_Block; spi_m74hc595.m74hc595_device.init = M74HC595_init; spi_m74hc595.m74hc595_device.open = M74HC595_open; spi_m74hc595.m74hc595_device.close = M74HC595_close; spi_m74hc595.m74hc595_device.control = M74HC595_control; spi_m74hc595.m74hc595_device.read = spi_read_165; spi_m74hc595.m74hc595_device.write = spi_write_595; /* no private */ spi_m74hc595.m74hc595_device.user_data = RT_NULL; rt_device_register(&spi_m74hc595.m74hc595_device, m74hc595_device_name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE); return RT_EOK; }
void rt_hw_spi3_init(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE); /* register spi bus */ { static struct stm32_spi_bus stm32_spi; GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /*!< SPI SCK pin configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12; GPIO_Init(GPIOC, &GPIO_InitStructure); /* Connect alternate function */ GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SPI3); GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_SPI3); GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_SPI3); stm32_spi_register(SPI3, &stm32_spi, "spi3"); } /* attach cs */ { static struct rt_spi_device spi_device; static struct stm32_spi_cs spi_cs; GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* spi20: PG6 */ spi_cs.GPIOx = GPIOA; spi_cs.GPIO_Pin = GPIO_Pin_15; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = spi_cs.GPIO_Pin; GPIO_SetBits(spi_cs.GPIOx, spi_cs.GPIO_Pin); GPIO_Init(spi_cs.GPIOx, &GPIO_InitStructure); rt_spi_bus_attach_device(&spi_device, "spi20", "spi3", (void*)&spi_cs); } { static struct rt_spi_device * rt_spi_device; struct rt_spi_configuration cfg; rt_spi_device = (struct rt_spi_device *)rt_device_find("spi20"); spi_user = rt_spi_device; rt_spi_device->bus->owner = rt_spi_device; cfg.data_width = 8; cfg.mode = RT_SPI_MODE_3 | RT_SPI_MSB; /* SPI Compatible: Mode 0 and Mode 3 */ cfg.max_hz = 2000000;//42 * 1000 * 1000; /* 50M */ rt_spi_configure(rt_spi_device, &cfg); } }
/** \brief sst25vfxx SPI flash init * * \param void * \return void * */ rt_err_t rt_hw_sst25vfxx_init(const char * spi_device_name) { port_init(); if (rt_sem_init(&spi2_lock, "spi2lock", 1, RT_IPC_FLAG_FIFO) != RT_EOK) { rt_kprintf("init spi2 lock semaphore failed\n"); } rt_spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name); if(rt_spi_device == RT_NULL) { FLASH_TRACE("spi device %s not found!\r\n", spi_device_name); return -RT_ENOSYS; } /* config spi */ { struct rt_spi_configuration cfg; cfg.data_width = 8; cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible: Mode 0 and Mode 3 */ cfg.max_hz = 9 * 1000 * 1000; /* 10M */ rt_spi_configure(rt_spi_device, &cfg); } spi_lock(); spi_config(); //CS_LOW(); spi_readwrite( CMD_WRDI ); //CS_HIGH(); //CS_LOW(); spi_readwrite( CMD_JEDEC_ID ); device_id = spi_readwrite(0xFF); device_id |= spi_readwrite(0xFF)<<8; device_id |= spi_readwrite(0xFF)<<16; //CS_HIGH(); if(device_id == SST25VF016) { FLASH_TRACE("FLASH TYPE : SST25VF016\r\n"); rt_kprintf("SPI Flash device_id is : 0x%X !\n",device_id); //CS_LOW(); spi_readwrite( CMD_DBSY ); //CS_HIGH(); //CS_LOW(); spi_readwrite( CMD_EWSR ); //CS_HIGH(); //CS_LOW(); spi_readwrite( CMD_WRSR ); spi_readwrite( 0 ); //CS_HIGH(); } rt_kprintf("SPI Flash device_id is : 0x%X !\n",device_id); spi_unlock(); spi_flash_device.type = RT_Device_Class_Block; spi_flash_device.init = sst25vfxx_flash_init; spi_flash_device.open = sst25vfxx_flash_open; spi_flash_device.close = sst25vfxx_flash_close; spi_flash_device.read = sst25vfxx_flash_read; spi_flash_device.write = sst25vfxx_flash_write; spi_flash_device.control = sst25vfxx_flash_control; /* no private */ spi_flash_device.user_data = RT_NULL; rt_device_register(&spi_flash_device, "spi0", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE); return RT_EOK; }