/*! \brief main routine \param[in] none \param[out] none \retval none */ int main(void) { /* system clocks configuration */ rcu_config(); /* GPIO configuration */ gpio_config(); /* USB device configuration */ usbd_core_init(&usb_device_dev); /* NVIC configuration */ nvic_config(); /* enabled USB pull-up */ gpio_bit_set(USB_PULLUP, USB_PULLUP_PIN); /* now the usb device is connected */ usb_device_dev.status = USBD_CONNECTED; while (1) { if (USBD_CONFIGURED == usb_device_dev.status) { cdc_acm_data_receive(&usb_device_dev); if (0 != receive_length) { if (1 == packet_sent) { cdc_acm_data_send(&usb_device_dev, receive_length); receive_length = 0; } } } } }
static int rt_hw_spi5_init(void) { /* register spi bus */ { rt_err_t result; rcu_periph_clock_enable(RCU_GPIOG); rcu_periph_clock_enable(RCU_SPI5); /* SPI5_CLK(PG13), SPI5_MISO(PG12), SPI5_MOSI(PG14),SPI5_IO2(PG10) and SPI5_IO3(PG11) GPIO pin configuration */ gpio_af_set(GPIOG, GPIO_AF_5, GPIO_PIN_10|GPIO_PIN_11| GPIO_PIN_12|GPIO_PIN_13| GPIO_PIN_14); gpio_mode_set(GPIOG, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10|GPIO_PIN_11| GPIO_PIN_12|GPIO_PIN_13| GPIO_PIN_14); gpio_output_options_set(GPIOG, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ, GPIO_PIN_10|GPIO_PIN_11| GPIO_PIN_12|GPIO_PIN_13| GPIO_PIN_14); result = gd32_spi_bus_register(SPI5, SPI_BUS_NAME); if (result != RT_EOK) { return result; } } /* attach cs */ { static struct rt_spi_device spi_device; static struct gd32_spi_cs spi_cs; rt_err_t result; spi_cs.GPIOx = GPIOG; spi_cs.GPIO_Pin = GPIO_PIN_9; /* SPI5_CS(PG9) GPIO pin configuration */ gpio_mode_set(GPIOG, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_9); gpio_output_options_set(GPIOG, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9); gpio_bit_set(GPIOG,GPIO_PIN_9); result = rt_spi_bus_attach_device(&spi_device, SPI_FLASH_DEVICE_NAME, SPI_BUS_NAME, (void*)&spi_cs); if (result != RT_EOK) { return result; } } return RT_EOK; }
/*! \brief main routine will construct a MSC device \param[in] none \param[out] none \retval none */ int main(void) { /* system clocks configuration */ rcu_config(); /* GPIO configuration */ gpio_config(); /* USB device configuration */ usbd_core_init(&usb_device_dev); /* NVIC configuration */ nvic_config(); /* enabled USB pull-up */ gpio_bit_set(USB_PULLUP, USB_PULLUP_PIN); /* now the usb device is connected */ usb_device_dev.status = USBD_CONNECTED; while(usb_device_dev.status != USBD_CONFIGURED); while (1){} }
static rt_uint32_t xfer(struct rt_spi_device* device, struct rt_spi_message* message) { struct rt_spi_bus * gd32_spi_bus = (struct rt_spi_bus *)device->bus; struct gd32f4_spi *f4_spi = (struct gd32f4_spi *)gd32_spi_bus->parent.user_data; struct rt_spi_configuration * config = &device->config; struct gd32_spi_cs * gd32_spi_cs = device->parent.user_data; uint32_t spi_periph = f4_spi->spi_periph; RT_ASSERT(device != NULL); RT_ASSERT(message != NULL); /* take CS */ if(message->cs_take) { gpio_bit_reset(gd32_spi_cs->GPIOx, gd32_spi_cs->GPIO_Pin); DEBUG_PRINTF("spi take cs\n"); } { if(config->data_width <= 8) { const rt_uint8_t * send_ptr = message->send_buf; rt_uint8_t * recv_ptr = message->recv_buf; rt_uint32_t size = message->length; DEBUG_PRINTF("spi poll transfer start: %d\n", size); while(size--) { rt_uint8_t data = 0xFF; if(send_ptr != RT_NULL) { data = *send_ptr++; } // Todo: replace register read/write by gd32f4 lib //Wait until the transmit buffer is empty while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_TBE)); // Send the byte spi_i2s_data_transmit(spi_periph, data); //Wait until a data is received while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_RBNE)); // Get the received data data = spi_i2s_data_receive(spi_periph); if(recv_ptr != RT_NULL) { *recv_ptr++ = data; } } DEBUG_PRINTF("spi poll transfer finsh\n"); } else if(config->data_width <= 16) { const rt_uint16_t * send_ptr = message->send_buf; rt_uint16_t * recv_ptr = message->recv_buf; rt_uint32_t size = message->length; while(size--) { rt_uint16_t data = 0xFF; if(send_ptr != RT_NULL) { data = *send_ptr++; } //Wait until the transmit buffer is empty while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_TBE)); // Send the byte spi_i2s_data_transmit(spi_periph, data); //Wait until a data is received while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_RBNE)); // Get the received data data = spi_i2s_data_receive(spi_periph); if(recv_ptr != RT_NULL) { *recv_ptr++ = data; } } } } /* release CS */ if(message->cs_release) { gpio_bit_set(gd32_spi_cs->GPIOx, gd32_spi_cs->GPIO_Pin); DEBUG_PRINTF("spi release cs\n"); } return message->length; };