Files
kunlun/dtest/spi_test/spi_test.c
2024-09-28 14:24:04 +08:00

334 lines
7.0 KiB
C
Executable File

/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
be copied by any method or incorporated into another program without
the express written consent of Aerospace C.Power. This Information or any portion
thereof remains the property of Aerospace C.Power. The Information contained herein
is believed to be accurate and Aerospace C.Power assumes no responsibility or
liability for its use in any way and conveys no license or title under
any patent or copyright and makes no representation or warranty that this
Information is free from patent or copyright infringement.
****************************************************************************/
/* os shim includes */
#include "os_types.h"
#include "os_task.h"
#include "os_utils.h"
/* common includes */
#include "iot_io.h"
#include "iot_bitops.h"
#include "iot_pkt_api.h"
#include "iot_ipc.h"
#include "iot_plc_lib.h"
#include "iot_dbglog_api.h"
#include "iot_config.h"
/* driver includes */
#include "iot_clock.h"
#include "iot_uart.h"
#include "iot_led.h"
/* cli includes */
#include "iot_cli.h"
#include "iot_uart_h.h"
/* debug includes*/
#include "dbg_io.h"
#include "spi.h"
#include "apb.h"
#include "apb_hw.h"
#include "gpio_mtx.h"
extern int platform_init();
os_task_h test_init_handle;
#define debug_printf(s)
void print_hex(int hex)
{
char hex_buf[11];
int index, data;
hex_buf[0] = '0';
hex_buf[1] = 'x';
hex_buf[10] = '\0';
index = 9;
while(index>1)
{
data = hex&0xFF;
hex_buf[index] = (data>9) ? (data-10+'A') : (data+'0');
hex >>= 4;
index--;
}
iot_printf(hex_buf);
}
int spi_test_device=DEVICE_SPI0_MASTER;
#define SPI_READ_SIZE 256
char spi_tx_buf[SPI_READ_SIZE];
char spi_rx_buf[SPI_READ_SIZE];
xfer_buf spi_tx, spi_rx;
int do_spi_test_poll = 1;
void spi_test_poll()
{
int offset;
iot_spi_cfg_t cfg = {0};
cfg.gpio.clk = 37;
cfg.gpio.cs = 28;
cfg.gpio.miso = 40;
cfg.gpio.mosi = 39;
cfg.port = 0;
(void)iot_spi_module_init(&cfg);
/* Use default configuration */
(void)iot_spi_dev_register_detail
(spi_test_device, NULL, NULL, NULL, NULL, 0x0);
spi_tx.p_nt = NULL;
spi_tx.txbuf = spi_tx_buf;
spi_tx.rxbuf = spi_rx_buf;
spi_tx.size = 10;
os_mem_set(spi_tx_buf, 0x55, SPI_READ_SIZE);
offset = 0;
while(1)
{
spi_tx_buf[0] = (char)offset++;
(void)iot_spi_poll_transfer(spi_test_device, &spi_tx);
//offset += SPI_READ_SIZE;
}
}
static xfer_buf *int_hd;
void spi_test_intterupt_handler(int status)
{
int cnt;
if(NULL == int_hd)
return ;
if(status&SPI_RXFIFO_FULL)
{
(void)iot_spi_get_rx_data_cnt(DEVICE_SPI0_MASTER, &cnt);
if(int_hd->rxbuf)
{
cnt = iot_spi_read_data(DEVICE_SPI0_MASTER, int_hd->rxbuf+int_hd->xfer_size, cnt);
int_hd->xfer_size += cnt;
}
else
{
/* Clear fifo */
(void)iot_spi_read_data(DEVICE_SPI0_MASTER, NULL, cnt);
}
}
if(status&SPI_TXFIFO_EMPTY)
{
(void)iot_spi_get_tx_data_space_cnt(DEVICE_SPI0_MASTER, &cnt);
if(int_hd->txbuf)
{
cnt = iot_spi_write_data(DEVICE_SPI0_MASTER, int_hd->rxbuf+int_hd->xfer_size, cnt);
int_hd->xfer_size += cnt;
}
else
{
(void)iot_spi_write_data(DEVICE_SPI0_MASTER, NULL, cnt);
}
}
if(int_hd->size == int_hd->xfer_size)
int_hd = int_hd->p_nt;
}
void spi_test_interrupt(void)
{
iot_spi_cfg_t cfg = {0};
cfg.gpio.clk = 37;
cfg.gpio.cs = 28;
cfg.gpio.miso = 40;
cfg.gpio.mosi = 39;
cfg.port = 0;
(void)iot_spi_module_init(&cfg);
spi_tx.p_nt = NULL;
spi_tx.txbuf = spi_tx_buf;
spi_tx.rxbuf = NULL;
spi_tx.size = 0x04;
spi_tx.xfer_size =0;
spi_rx.p_nt = NULL;
spi_rx.rxbuf = spi_rx_buf;
spi_rx.txbuf = NULL;
spi_rx.size = SPI_READ_SIZE;
spi_rx.xfer_size =0;
int_hd = &spi_tx;
/* Use default configuration */
(void)iot_spi_dev_register_detail(spi_test_device, NULL, NULL, NULL, spi_test_intterupt_handler, \
SPI_RXFIFO_FULL|SPI_TXFIFO_EMPTY);
}
void spi_pin_config(void)
{
apb_enable(GMTX_EB_OFFSET + 32);
//apb_enable(APB_GPIO);
/* UART0 */
*(int*)0x44007008 = 0x0; /* FUNCTION 1 TXD */
*(int*)0x4400700C = 0x0; /* FUNCTION 1 RXD*/
*(int*)0x44020024 = 0x1000; /* SIG9 <- core */
/* SPI FLASH */
/*
GPIO 20 <-> CLK
GPIO 17 <-> CS
GPIO 18 <-> MISO
GPIO 19 <-> MOSI
*/
#if 1
*(int*)0x44007080 = 0x0; /* FUNCTION 1 GPIO 16 */
*(int*)0x44007084 = 0x0; /* FUNCTION 1 GPIO 17 */
*(int*)0x44007088 = 0x0; /* FUNCTION 1 GPIO 18 */
*(int*)0x4400708C = 0x0; /* FUNCTION 1 GPIO 19 */
#endif
*(int*)0x44020040 = 18; /* MISO <- GPIO18 */
*(int*)0x44020450 = 20; /* CLK -> GPIO20 */
*(int*)0x44020444 = 21; /* CS -> GPIO17 */
*(int*)0x4402044C = 22; /* MOSI -> GPIO19 */
}
void dma_spi_gpio_config()
{
gpio_mtx_enable();
gpio_sig_info_t info1 = {
4,
{
{IO_TYPE_OUT, 0, 10, 0xff, 20},
{IO_TYPE_OUT, 0, 11, 0xff, 21},
{IO_TYPE_OUT, 0, 15, 0xff, 22},
{IO_TYPE_IN, 0, 12, 16, 0xff}
}
};
gpio_module_pin_select(&info1);
gpio_module_sig_select(&info1, GPIO_MTX_MODE_MATRIX);
}
void test_init()
{
/* init common modules */
iot_bitops_init();
/* init os related modules and utilities */
os_utils_init();
/* init dbglog module */
iot_dbglog_init();
/*init uart module*/
iot_uart_init(1);
spi_pin_config();
//dma_spi_gpio_config();
if(do_spi_test_poll)
spi_test_poll();
else
spi_test_interrupt();
}
void iot_task_1(void *arg)
{
debug_printf("Entry Task 1 ..\r\n");
for(;;) {
test_init();
os_delete_task(test_init_handle);
}
}
int32_t iot_task_init()
{
/* start plc lib task */
test_init_handle = os_create_task(iot_task_1, NULL, 9);
//create the tasks;
if(test_init_handle != NULL) {
debug_printf("Task 1 created successfully .. \r\n");
}
return 0;
}
int32_t iot_task_start()
{
//start the tasks;
os_start_kernel();
return 0;
}
static int32_t iot_platform_init()
{
/*platform intialization*/
platform_init();
//resource initializations;
system_clock_init();
system_uart_init();
dbg_uart_init();
dbg_uart_stage1_init();
iot_led_init();
return 0;
}
int32_t iot_module_init(void)
{
//platform intialization;
iot_platform_init();
//create all the tasks;
iot_task_init();
debug_printf("starting...\n");
return 0;
}
int32_t iot_module_start(void)
{
int32_t res = 0;
res = iot_task_start();
return res;
}
int main(void)
{
//module init;
iot_module_init();
iot_module_start();
return 0;
}