/*============================================================================ @brief ------------------------------------------------------------------------------ @note ============================================================================*/ void dev__tcm__spi__init( void ) { ret_code_t err_code = nrf_drv_spi_init(&m_spi_master, &config, NULL); printf("SPI init: %d\r\n",err_code); APP_ERROR_CHECK(err_code); }
static ret_code_t hardware_init(void) { ret_code_t err_code; nrf_gpio_cfg_output(ST7735_DC_PIN); nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG; spi_config.sck_pin = ST7735_SCK_PIN; spi_config.miso_pin = ST7735_MISO_PIN; spi_config.mosi_pin = ST7735_MOSI_PIN; spi_config.ss_pin = ST7735_SS_PIN; err_code = nrf_drv_spi_init(&spi, &spi_config, NULL, NULL); return err_code; }
void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { int i; for (i = 0; i < SPI_COUNT; ++i) { spi_info_t *p_spi_info = &m_spi_info[i]; if (!p_spi_info->initialized) { NVIC_SetVector(spi_hanlder_desc[i].IRQn, spi_hanlder_desc[i].vector); p_spi_info->sck_pin = (uint8_t)sclk; p_spi_info->mosi_pin = (mosi != NC) ? (uint8_t)mosi : NRF_DRV_SPI_PIN_NOT_USED; p_spi_info->miso_pin = (miso != NC) ? (uint8_t)miso : NRF_DRV_SPI_PIN_NOT_USED; p_spi_info->ss_pin = (ssel != NC) ? (uint8_t)ssel : NRF_DRV_SPI_PIN_NOT_USED; p_spi_info->spi_mode = (uint8_t)NRF_DRV_SPI_MODE_0; p_spi_info->frequency = NRF_DRV_SPI_FREQ_1M; // By default each SPI instance is initialized to work as a master. // Should the slave mode be used, the instance will be reconfigured // appropriately in 'spi_format'. nrf_drv_spi_config_t config; prepare_master_config(&config, p_spi_info); nrf_drv_spi_t const *p_spi = &m_instances[i].master; ret_code_t ret_code = nrf_drv_spi_init(p_spi, &config, m_master_event_handlers[i]); if (ret_code == NRF_SUCCESS) { p_spi_info->initialized = true; p_spi_info->master = true; p_spi_info->flag.busy = false; #if DEVICE_SPI_ASYNCH p_spi_info->handler = 0; #endif SPI_IDX(obj) = i; return; } } } // No available peripheral error("No available SPI peripheral\r\n"); }
/**@brief Function for initializing a SPI master driver. * * @param[in] p_instance Pointer to SPI master driver instance. * @param[in] lsb Bits order LSB if true, MSB if false. */ static void spi_master_init(nrf_drv_spi_t const * p_instance, bool lsb) { uint32_t err_code = NRF_SUCCESS; nrf_drv_spi_config_t config = { .irq_priority = APP_IRQ_PRIORITY_LOW, .orc = 0xCC, .frequency = NRF_DRV_SPI_FREQ_8M, // originally NRF_DRV_SPI_FREQ_1M .mode = NRF_DRV_SPI_MODE_0, .bit_order = (lsb ? //if lsb then LSB_FIRRST, else MSB_FIRST NRF_DRV_SPI_BIT_ORDER_LSB_FIRST : NRF_DRV_SPI_BIT_ORDER_MSB_FIRST), }; if (p_instance == &m_spi_master_0) { config.sck_pin = SPIM0_SCK_PIN; config.mosi_pin = SPIM0_MOSI_PIN; config.miso_pin = SPIM0_MISO_PIN; // SS not initialized config.ss_pin = SPIM0_SS_PIN; err_code = nrf_drv_spi_init(p_instance, &config, //spi_intan_id_read_handler); // EDIT: INTAN ID spi handler spi_master_0_event_handler); // EDIT: data collection spi handler } APP_ERROR_CHECK(err_code); } /**@brief Function for sending and receiving data. * * @param[in] p_instance Pointer to SPI master driver instance. * @param[in] p_tx_data A pointer to a buffer TX. * @param[out] p_rx_data A pointer to a buffer RX. * @param[in] len A length of the data buffers. */ static void spi_send_recv(nrf_drv_spi_t const * p_instance, uint8_t * p_tx_data, uint8_t * p_rx_data, uint16_t tx_len, uint16_t rx_len) { uint32_t err_code = nrf_drv_spi_transfer(p_instance, p_tx_data, tx_len, p_rx_data, rx_len); //APP_ERROR_CHECK(err_code); }
/**@brief Function for application main entry. Does not return. */ int main(void) { unsigned char reg = 0x00; //IC Identity Register nrf_gpio_cfg_output(30); //for the CS pin nrf_drv_gpiote_out_set(30); //This should assert the CS for the SPI peripheral m_transfer_completed = false; // Setup bsp module. bsp_configuration(); nrf_drv_spi_config_t const config = { #if (SPI0_ENABLED == 1) .sck_pin = SPIM0_SCK_PIN, .mosi_pin = SPIM0_MOSI_PIN, .miso_pin = SPIM0_MISO_PIN, .ss_pin = SPIM0_SS_PIN, #elif (SPI1_ENABLED == 1) .sck_pin = SPIM1_SCK_PIN, .mosi_pin = SPIM1_MOSI_PIN, .miso_pin = SPIM1_MISO_PIN, .ss_pin = SPIM1_SS_PIN, #elif (SPI2_ENABLED == 1) .sck_pin = SPIM2_SCK_PIN, .mosi_pin = SPIM2_MOSI_PIN, .miso_pin = SPIM2_MISO_PIN, .ss_pin = SPIM2_SS_PIN, #endif .irq_priority = APP_IRQ_PRIORITY_LOW, .orc = 0xCC, .frequency = NRF_DRV_SPI_FREQ_1M, .mode = NRF_DRV_SPI_MODE_0, .bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST, .ss_pin = NRF_DRV_SPI_PIN_NOT_USED, //added by DS - if not specified, the nrf_drv_spi_init() will set this to high, which is wrong, CS for AS3911 is active low }; ret_code_t err_code = nrf_drv_spi_init(&m_spi_master, &config, spi_master_event_handler); APP_ERROR_CHECK(err_code); while(1) { SPIWriteReg(0x18,0x27); reg=SPIReadReg(0x18); SEGGER_RTT_printf(0, " r value is %x\n", reg); } }
/**@brief Function for application main entry. Does not return. */ int main(void) { // Setup bsp module. bsp_configuration(); nrf_drv_spi_config_t const config = { #if (SPI0_ENABLED == 1) .sck_pin = SPIM0_SCK_PIN, .mosi_pin = SPIM0_MOSI_PIN, .miso_pin = SPIM0_MISO_PIN, .ss_pin = SPIM0_SS_PIN, #elif (SPI1_ENABLED == 1) .sck_pin = SPIM1_SCK_PIN, .mosi_pin = SPIM1_MOSI_PIN, .miso_pin = SPIM1_MISO_PIN, .ss_pin = SPIM1_SS_PIN, #elif (SPI2_ENABLED == 1) .sck_pin = SPIM2_SCK_PIN, .mosi_pin = SPIM2_MOSI_PIN, .miso_pin = SPIM2_MISO_PIN, .ss_pin = SPIM2_SS_PIN, #endif .irq_priority = APP_IRQ_PRIORITY_LOW, .orc = 0xCC, .frequency = NRF_DRV_SPI_FREQ_1M, .mode = NRF_DRV_SPI_MODE_0, .bit_order = NRF_DRV_SPI_BIT_ORDER_LSB_FIRST, }; ret_code_t err_code = nrf_drv_spi_init(&m_spi_master, &config, spi_master_event_handler); APP_ERROR_CHECK(err_code); for (;;) { if (m_transfer_completed) { m_transfer_completed = false; // Set buffers and start data transfer. spi_send_recv(m_tx_data, m_rx_data, TX_RX_BUF_LENGTH); } } }
/** @brief Function initialization and configuration of RTC driver instance. */ static void spi_config(max6675_config_t *max6675_config) { ret_code_t err_code; nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG; NRF_LOG_DEBUG("spi config %d %d %d\n\r",max6675_config->miso_pin,max6675_config->sck_pin,max6675_config->cs_pin); spi_config.sck_pin = max6675_config->sck_pin; spi_config.mosi_pin = 0xFF; // not used spi_config.miso_pin = max6675_config->miso_pin; spi_config.ss_pin = 0xFF; // max6675 need Conversion Time 220ms, so we set it external //spi_config.frequency = NRF_DRV_SPI_FREQ_125K; // tCP = 100ns see MAX7219/MAX7221 Datasheet TIMING CHARACTERISTICS spi_config.frequency = NRF_DRV_SPI_FREQ_8M; // tCP = 100ns see MAX7219/MAX7221 Datasheet TIMING CHARACTERISTICS spi_config.mode = NRF_DRV_SPI_MODE_0; // SCK active high, sample on leading edge of clock. spi_config.bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST; err_code = nrf_drv_spi_init(&my_spi_0,&spi_config,NULL); APP_ERROR_CHECK(err_code); }
void spi_frequency(spi_t *obj, int hz) { spi_info_t *p_spi_info = SPI_INFO(obj); nrf_drv_spi_frequency_t new_frequency = freq_translate(hz); if (p_spi_info->master) { if (p_spi_info->frequency != new_frequency) { p_spi_info->frequency = new_frequency; nrf_drv_spi_config_t config; prepare_master_config(&config, p_spi_info); nrf_drv_spi_t const *p_spi = MASTER_INST(obj); nrf_drv_spi_uninit(p_spi); (void)nrf_drv_spi_init(p_spi, &config, m_master_event_handlers[SPI_IDX(obj)]); } } // There is no need to set anything in slaves when it comes to frequency, // since slaves just synchronize with the clock provided by a master. }
void spi_format(spi_t *obj, int bits, int mode, int slave) { if (bits != 8) { error("Only 8-bits SPI is supported\r\n"); } if (mode > 3) { error("SPI format error\r\n"); } spi_info_t *p_spi_info = SPI_INFO(obj); if (slave) { nrf_drv_spis_mode_t spi_modes[4] = { NRF_DRV_SPIS_MODE_0, NRF_DRV_SPIS_MODE_1, NRF_DRV_SPIS_MODE_2, NRF_DRV_SPIS_MODE_3, }; nrf_drv_spis_mode_t new_mode = spi_modes[mode]; // If the peripheral is currently working as a master, the SDK driver // it uses needs to be switched from SPI to SPIS. if (p_spi_info->master) { nrf_drv_spi_uninit(MASTER_INST(obj)); } // I the SPI mode has to be changed, the SDK's SPIS driver needs to be // re-initialized (there is no other way to change its configuration). else if (p_spi_info->spi_mode != (uint8_t)new_mode) { nrf_drv_spis_uninit(SLAVE_INST(obj)); } else { return; } p_spi_info->spi_mode = (uint8_t)new_mode; p_spi_info->master = false; p_spi_info->flag.readable = false; // Initialize SDK's SPIS driver with the new configuration. nrf_drv_spis_config_t config; prepare_slave_config(&config, p_spi_info); (void)nrf_drv_spis_init(SLAVE_INST(obj), &config, m_slave_event_handlers[SPI_IDX(obj)]); // Prepare the slave for transfer. p_spi_info->tx_buf = NRF_DRV_SPIS_DEFAULT_ORC; nrf_drv_spis_buffers_set(SLAVE_INST(obj), (uint8_t const *)&p_spi_info->tx_buf, 1, (uint8_t *)&p_spi_info->rx_buf, 1); } else // master { nrf_drv_spi_mode_t spi_modes[4] = { NRF_DRV_SPI_MODE_0, NRF_DRV_SPI_MODE_1, NRF_DRV_SPI_MODE_2, NRF_DRV_SPI_MODE_3, }; nrf_drv_spi_mode_t new_mode = spi_modes[mode]; // If the peripheral is currently working as a slave, the SDK driver // it uses needs to be switched from SPIS to SPI. if (!p_spi_info->master) { nrf_drv_spis_uninit(SLAVE_INST(obj)); } // I the SPI mode has to be changed, the SDK's SPI driver needs to be // re-initialized (there is no other way to change its configuration). else if (p_spi_info->spi_mode != (uint8_t)new_mode) { nrf_drv_spi_uninit(MASTER_INST(obj)); } else { return; } p_spi_info->spi_mode = (uint8_t)new_mode; p_spi_info->master = true; p_spi_info->flag.busy = false; // Initialize SDK's SPI driver with the new configuration. nrf_drv_spi_config_t config; prepare_master_config(&config, p_spi_info); (void)nrf_drv_spi_init(MASTER_INST(obj), &config, m_master_event_handlers[SPI_IDX(obj)]); } }
void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { int i; // This block is only a workaround that allows to create SPI object several // times, what would be otherwise impossible in the current implementation // of mbed driver that does not call spi_free() from SPI destructor. // Once this mbed's imperfection is corrected, this block should be removed. for (i = 0; i < SPI_COUNT; ++i) { spi_info_t *p_spi_info = &m_spi_info[i]; if (p_spi_info->initialized && p_spi_info->mosi_pin == (uint8_t)mosi && p_spi_info->miso_pin == (uint8_t)miso && p_spi_info->sck_pin == (uint8_t)sclk && p_spi_info->ss_pin == (uint8_t)ssel) { // Reuse the already allocated SPI instance (instead of allocating // a new one), if it appears to be initialized with exactly the same // pin assignments. SPI_IDX(obj) = i; return; } } for (i = 0; i < SPI_COUNT; ++i) { spi_info_t *p_spi_info = &m_spi_info[i]; if (!p_spi_info->initialized) { p_spi_info->sck_pin = (uint8_t)sclk; p_spi_info->mosi_pin = (mosi != NC) ? (uint8_t)mosi : NRF_DRV_SPI_PIN_NOT_USED; p_spi_info->miso_pin = (miso != NC) ? (uint8_t)miso : NRF_DRV_SPI_PIN_NOT_USED; p_spi_info->ss_pin = (ssel != NC) ? (uint8_t)ssel : NRF_DRV_SPI_PIN_NOT_USED; p_spi_info->spi_mode = (uint8_t)NRF_DRV_SPI_MODE_0; p_spi_info->frequency = NRF_DRV_SPI_FREQ_1M; NVIC_SetVector(spi_handler_desc[i].IRQn, spi_handler_desc[i].vector); // By default each SPI instance is initialized to work as a master. // Should the slave mode be used, the instance will be reconfigured // appropriately in 'spi_format'. nrf_drv_spi_config_t config; prepare_master_config(&config, p_spi_info); nrf_drv_spi_t const *p_spi = &m_instances[i].master; ret_code_t ret_code = nrf_drv_spi_init(p_spi, &config, m_master_event_handlers[i]); if (ret_code == NRF_SUCCESS) { p_spi_info->initialized = true; p_spi_info->master = true; p_spi_info->flag.busy = false; #if DEVICE_SPI_ASYNCH p_spi_info->handler = 0; #endif SPI_IDX(obj) = i; return; } } } // No available peripheral error("No available SPI peripheral\r\n"); }
/**@brief Application main function. */ int main(void) { /* beginning of Initializing services for the Nordic Board */ uint32_t err_code; bool erase_bonds; //print start string to terminal uint8_t start_string[] = START_STRING; printf("%s",start_string); // Initialize timer. APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false); nrf_drv_gpiote_init(); uart_init(); //buttons_leds_init(&erase_bonds); ble_stack_init(); gap_params_init(); services_init(); advertising_init(); conn_params_init(); err_code = ble_advertising_start(BLE_ADV_MODE_FAST); APP_ERROR_CHECK(err_code); //More SPI Additions nrf_drv_spi_config_t const config = { #if (SPI0_ENABLED == 1) .sck_pin = SPIM0_SCK_PIN, .mosi_pin = SPIM0_MOSI_PIN, .miso_pin = SPIM0_MISO_PIN, .ss_pin = SPIM0_SS_PIN, #elif (SPI1_ENABLED == 1) .sck_pin = SPIM1_SCK_PIN, .mosi_pin = SPIM1_MOSI_PIN, .miso_pin = SPIM1_MISO_PIN, .ss_pin = SPIM1_SS_PIN, #elif (SPI2_ENABLED == 1) .sck_pin = SPIM2_SCK_PIN, .mosi_pin = SPIM2_MOSI_PIN, .miso_pin = SPIM2_MISO_PIN, .ss_pin = SPIM2_SS_PIN, #endif .irq_priority = APP_IRQ_PRIORITY_LOW, .orc = 0xCC, .frequency = NRF_DRV_SPI_FREQ_1M, .mode = NRF_DRV_SPI_MODE_0, .bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST, }; ret_code_t err_code1 = nrf_drv_spi_init(&m_spi_master, &config, spi_master_event_handler); APP_ERROR_CHECK(err_code1); /* End of Initializing services for the Nordic Board Beginning of CC1101 initializing. */ CC1101_Init(); // Enter main loop. for (;;) { // //for setting in receive mode // RecvDataPacket(); SEGGER_RTT_WriteString(0,"RX data:"); for(uint32_t i = 0; i<8;i++){ SEGGER_RTT_printf(0,"%x",m_rx_data[i]); } SEGGER_RTT_WriteString(0,"\n"); // //for sending data that was recieved from the BTLE event // if(m_transfer_completed & newData) { m_transfer_completed = false; newData = false; SendDataPacket(txt_data, strlen((char *) txt_data) + 1);//Additional byte (5+1) is header byte nrf_delay_ms(1); } } } void CC1101_Init(void){ //sequence of SS pin on/off to indicate we are going to reset the system nrf_gpio_pin_clear(SPIM0_SS_PIN); nrf_delay_ms(1); nrf_gpio_pin_set(SPIM0_SS_PIN); nrf_delay_ms(1); nrf_gpio_pin_clear(SPIM0_SS_PIN); //strobe CC1101 reset uint8_t SRES = 0x30; SpiStrobe(SRES); nrf_delay_ms(5); //calibrate CC1101 CC1101_Calibrate(); nrf_delay_ms(1); }