分类:
2008-10-29 17:46:13
/* Data needs to be stable on rising edge of clock */
void
generic_spi_raw_output_u8(unsigned char val)
{
#define IN_DATA_GPIO 3
#define OUT_DATA_GPIO 19
#define CLK_GPIO 12
int i, rd_val, wr_val;
for (i = 7; i >= 0; i--) {
rd_val = ar7100_reg_rd(AR9100_SPI_GPIO_OUT);
rd_val &= ~(1 << CLK_GPIO); /* lower clock */
wr_val = (((val >> i) & 1) << OUT_DATA_GPIO);
//printf(" %x", wr_val);
//wr_val = 1 << OUT_DATA_GPIO;
wr_val |= (rd_val & ~(1 << OUT_DATA_GPIO));
ar7100_reg_wr_nf(AR9100_SPI_GPIO_OUT, wr_val);
wr_val |= (1 << CLK_GPIO); /* high clock */
ar7100_reg_wr_nf(AR9100_SPI_GPIO_OUT, wr_val);
}
}
unsigned int
generic_spi_raw_input_u32(void)
{
int i, rd_val;
unsigned int ret_val = 0;
/* Configure as input for data */
rd_val = (ar7100_reg_rd(AR9100_SPI_GPIO_OE) & ~(1 << IN_DATA_GPIO));
ar7100_reg_wr_nf(AR9100_SPI_GPIO_OE, rd_val);
/* Configure GPIO as output for clock */
rd_val = (ar7100_reg_rd(AR9100_SPI_GPIO_OE) | (1 << CLK_GPIO));
ar7100_reg_wr_nf(AR9100_SPI_GPIO_OE, rd_val);
for (i = 31; i >= 0; i--) {
rd_val = ar7100_reg_rd(AR9100_SPI_GPIO_OUT);
rd_val &= ~(1 << CLK_GPIO); /* lower clock */
ar7100_reg_wr_nf(AR9100_SPI_GPIO_OUT, rd_val);
rd_val = ar7100_reg_rd(AR9100_SPI_GPIO_OUT);
rd_val |= (1 << CLK_GPIO); /* high clock */
ar7100_reg_wr_nf(AR9100_SPI_GPIO_OUT, rd_val);
rd_val = ar7100_reg_rd(AR9100_SPI_GPIO_IN);
ret_val |= ((rd_val & (1 << IN_DATA_GPIO)) >> IN_DATA_GPIO) << i;
}
rd_val = ar7100_reg_rd(AR9100_SPI_GPIO_OUT);
rd_val &= ~(1 << CLK_GPIO); /* lower clock */
ar7100_reg_wr_nf(AR9100_SPI_GPIO_OUT, rd_val);
return ret_val;
}
读时序:
clk 拉底 拉高 读入一位数据(最高位先读进来)
写时序:
clk 拉底 送出一位数据(最高位先送出) clk拉高