Пример #1
0
/////////////////////////////////////////////////////////////////////
//功    能:复位RC522
//返    回: 成功返回MI_OK
/////////////////////////////////////////////////////////////////////
char PcdReset(void)
{
	//PORTD|=(1<<RC522RST);
	SET_RC522RST;
    delay_ns(10);
	//PORTD&=~(1<<RC522RST);
	CLR_RC522RST;
    delay_ns(10);
	//PORTD|=(1<<RC522RST);
	SET_RC522RST;
    delay_ns(10);
    WriteRawRC(CommandReg,PCD_RESETPHASE);
	WriteRawRC(CommandReg,PCD_RESETPHASE);
    delay_ns(10);
    
    WriteRawRC(ModeReg,0x3D);            //和Mifare卡通讯,CRC初始值0x6363
    WriteRawRC(TReloadRegL,30);           
    WriteRawRC(TReloadRegH,0);
    WriteRawRC(TModeReg,0x8D);
    WriteRawRC(TPrescalerReg,0x3E);
	
	WriteRawRC(TxAutoReg,0x40);//必须要
   
    return MI_OK;
}
Пример #2
0
static uint16_t enc_txrx(const uint16_t txd)
{
  pin_set_output_low(GPIOA, PORTA_ENC_CS);
  volatile uint16_t rxd __attribute__((unused)) = SPI1->DR; // be sure we've flushed the RX register
  SPI1->DR = txd | (g_parity_lookup[txd & 0x7fff] << 15);
  while (!(SPI1->SR & SPI_SR_RXNE)) { } // wait for it...
  delay_ns(1); // just burn a few cycles...
  pin_set_output_high(GPIOA, PORTA_ENC_CS);
  return SPI1->DR;
}
Пример #3
0
void adf_write_register(uint32_t data) {
    int8_t i;

    gpio_clear(ADF_CLK_PORT, ADF_CLK);
    delay_ns(20);

    gpio_clear(ADF_LE_PORT, ADF_LE);
    delay_ns(100);

    for(i=31; i>=0; i--) {
        if((data & (1<<i))>>i)
            gpio_set(ADF_DATA_PORT, ADF_DATA);
        else
            gpio_clear(ADF_DATA_PORT, ADF_DATA);
        delay_ns(20);
        gpio_set(ADF_CLK_PORT, ADF_CLK);
        delay_ns(100);
        gpio_clear(ADF_CLK_PORT, ADF_CLK);
        delay_ns(100);
    }
Пример #4
0
/*
 * Bit-bang a byte on the GPIO data line
 */
static inline void send_byte(unsigned char byte)
{
    int i;

    /* Data setup on rising clock edge */
    for (i = 7; i >= 0; i--) {
        //
        if (byte & (1 << i))
            gpio_set_value(DATA_GPIO, 1);
        else
            gpio_set_value(DATA_GPIO, 0);


        gpio_set_value(CCLK_GPIO, 1);
        delay_ns(1);

        gpio_set_value(CCLK_GPIO, 0);
        delay_ns(1);
    }
}
Пример #5
0
int main(void)
{
    // Initialize the UART for communicating via text.
    uart_init();

    printf("before");
    delay_ns(200.0);
    printf("after");

  return 0;
}
Пример #6
0
/*
 * This is a bit special SPI method.
 *    - Pulses Asserts SSEL (CS) and holds high for the duration of command.
 *    - Does not pulse SSEL between bytes.
 *    - Does not change SSEL after the write.
 */
void SPI_Write(const uint8_t *data, uint16_t len)
{
    if (len == 0)
        return;

    while (SSP_GetStatus(CL632_SPI, SSP_STAT_TXFIFO_EMPTY) == RESET);

    // Send reset.
    GPIO_ResetBits(CL632_SSEL_PORT, CL632_SSEL_PIN);
    delay_ns(100);
    GPIO_SetBits(CL632_SSEL_PORT, CL632_SSEL_PIN);
    delay_ns(100);

    while (len-- > 0) {
        SSP_SendData(CL632_SPI, *data++);
        while (SSP_GetStatus(CL632_SPI, SSP_STAT_TXFIFO_NOTFULL) == RESET);
    }

    while (SSP_GetStatus(CL632_SPI, SSP_STAT_TXFIFO_EMPTY) == RESET);

    delay_ns(100);
}
Пример #7
0
//////////////////////////////////////////////////////////////////////
//设置RC632的工作方式 
//////////////////////////////////////////////////////////////////////
char M500PcdConfigISOType(u8   type)
{
   if (type == 'A')                     //ISO14443_A
   { 
       ClearBitMask(Status2Reg,0x08);
       WriteRawRC(ModeReg,0x3D);//3F
       WriteRawRC(RxSelReg,0x86);//84
       WriteRawRC(RFCfgReg,0x7F);   //4F
   	   WriteRawRC(TReloadRegL,30);//tmoLength);// TReloadVal = 'h6a =tmoLength(dec) 
	   WriteRawRC(TReloadRegH,0);
       WriteRawRC(TModeReg,0x8D);
	   WriteRawRC(TPrescalerReg,0x3E);
	   delay_ns(1000);
       PcdAntennaOn();
   }
   else{ return 1; }
   
   return MI_OK;
}
Пример #8
0
/*
 * Clock in a byte from the GPIO line
 */
static inline void read_byte(unsigned char *byte)
{
    int i;
    bool val;
    *byte = 0;

    /* data read on falling clock edge */
    for (i = 7; i >= 0; i--) {

        gpio_set_value(CCLK_GPIO, 1);
        delay_ns(1);

        gpio_get_value(DATA_GPIO, &val);

        if (val)
            *byte |= (1 << i);

        gpio_set_value(CCLK_GPIO, 0);

    }
}
Пример #9
0
static void accel_txrx(const uint8_t start_reg, 
                       const uint8_t num_regs,
                       uint8_t *rx,
                       const uint8_t *tx)
{
  pin_set_output_low(GPIOE, PORTE_CS);
  volatile uint8_t __attribute__((unused)) rxd = SPI1->DR; // flush first!
  SPI1->DR = (tx == NULL ? 0x80 : 0x00) | start_reg; // tx = NULL during reads
  while (!(SPI1->SR & SPI_SR_RXNE)) { } // wait for it...
  rxd = SPI1->DR; // flush garbage (first inbound byte)
  for (int i = 0; i < num_regs; i++)
  {
    while (!(SPI1->SR & SPI_SR_TXE)) { }
    SPI1->DR = tx ? tx[i] : 0; // start SPI txrx
    while (!(SPI1->SR & SPI_SR_RXNE)) { } // wait for it...
    if (rx)
      rx[i] = SPI1->DR; // save the data byte that we received
    else
      SPI1->DR; // drain buffer anyway
  }
  while (SPI1->SR & SPI_SR_BSY) { }
  delay_ns(10); // burn a few cycles (TODO: is this needed? scope it sometime.)
  pin_set_output_high(GPIOE, PORTE_CS);
}
Пример #10
0
void init() {
	/* Load persistent settings, or set the default values. */
	if(persistent_load() == -1) {
		wanted_temperature = TEMP_DEF;
		wanted_humidity = HUM_DEF;
	}

	i2c_init();
	httpd_init();
	logs_init();
	pid_init();

	struct sigaction sa = (struct sigaction) {
		.sa_handler = &reload,
		.sa_flags = 0,
		.sa_restorer = NULL
	};

	sigemptyset(&(sa.sa_mask));
	sigaddset(&(sa.sa_mask), SIGUSR1);
	sigaddset(&(sa.sa_mask), SIGINT);
	sigaddset(&(sa.sa_mask), SIGTERM);

	if(sigaction(SIGUSR1, &sa, NULL) < 0) {
		fprintf(stderr, "Couldn't install signal handler.\n");
		exit(1);
	}

	sa.sa_handler = &end;
	
	if(sigaction(SIGTERM, &sa, NULL) < 0 || sigaction(SIGINT, &sa, NULL) < 0) {
		fprintf(stderr, "Couldn't install signal handler.\n");
		exit(1);
	}
}

void reload(int signal) {
	httpd_reload();
}

int main(int argc, char **argv) {
	float data[2];

	init();

	struct timespec ts;
	clock_gettime(CLOCK_MONOTONIC, &ts);
	for(;;) {
		if(!read_data(data)) {
			continue;
		}

		log_values(data[1], data[0]);
		pid_control(data[1], data[0]);
		stats();

		if(logstdout(data) == EOF) {
			break;
		}

		delay_ns(&ts, PERIOD_S, 0);
	}

	end();
}
Пример #11
0
/** Send a pulse on the ENABLE line */
void _lcd_clk()
{
	pin_up(LCD_E);
	delay_ns(420);
	pin_down(LCD_E);
}
Пример #12
0
/** Send a pulse on the ENABLE line */
void _lcd_clk()
{
	pin_high(LCD_E);
	delay_ns(450);
	pin_low(LCD_E);
}
Пример #13
0
static int gb_spi_transfer(uint32_t cportid, gb_operation_header *op_header) {
    struct gb_spi_transfer_desc *desc;
    struct gb_spi_transfer_request *request;
    struct device_spi_transfer transfer;
    uint32_t size = 0, freq = 0;
    uint8_t *write_data, *read_buf;
    bool selected = false;
    int i, op_count;
    int ret = 0;
    struct gb_spi_transfer_response *response;
    size_t expected_size;
    size_t request_size;

    request_size = gb_operation_request_size(op_header);
    if (request_size < sizeof(*request)) {
        greybus_op_response(cportid,
                            op_header,
                            GB_OP_INVALID,
                            NULL,
                            0);
        return -1;
    }
    request = (struct gb_spi_transfer_request *)
                (((uint8_t *)op_header) +
                 sizeof(gb_operation_header));

    op_count = le16_to_cpu(request->count);

    expected_size = sizeof(*request) +
                    op_count * sizeof(request->transfers[0]);
    if (request_size < expected_size) {
        greybus_op_response(cportid,
                            op_header,
                            GB_OP_INVALID,
                            NULL,
                            0);
        return -1;
    }

    write_data = (uint8_t *)&request->transfers[op_count];

    for (i = 0; i < op_count; i++) {
        desc = &request->transfers[i];
        if (desc->rdwr & SPI_XFER_READ) {
            size += le32_to_cpu(desc->len);
        }
    }

    unsigned char payload[size];
    response = (struct gb_spi_transfer_response *)payload;
    read_buf = response->data;

    /* set SPI mode */
    ret = device_spi_setmode(spi_dev, request->chip_select, request->mode);
    if (ret) {
        goto spi_err;
    }

    /* parse all transfer request from AP host side */
    for (i = 0; i < op_count; i++) {
        desc = &request->transfers[i];
        freq = le32_to_cpu(desc->speed_hz);

        /* set SPI bits-per-word */
        ret = device_spi_setbits(spi_dev, request->chip_select,
                                 desc->bits_per_word);
        if (ret) {
            goto spi_err;
        }

        /* set SPI clock */
        ret = device_spi_setfrequency(spi_dev, request->chip_select, &freq);
        if (ret) {
            goto spi_err;
        }

        /* assert chip-select pin */
        if (!selected) {
            ret = device_spi_select(spi_dev, request->chip_select);
            if (ret) {
                goto spi_err;
            }
            selected = true;
        }

        /* setup SPI transfer */
        memset(&transfer, 0, sizeof(struct device_spi_transfer));
        if (desc->rdwr & SPI_XFER_WRITE) {
            transfer.txbuffer = write_data;
        } else {
            transfer.txbuffer = NULL;
        }

        if (desc->rdwr & SPI_XFER_READ) {
            transfer.rxbuffer = read_buf;
        } else {
            transfer.rxbuffer = NULL;
        }

        transfer.nwords = le32_to_cpu(desc->len);

        /* start SPI transfer */
        ret = device_spi_exchange(spi_dev, &transfer);
        if (ret) {
            goto spi_err;
        }
        /* move to next gb_spi_transfer data buffer */
        if (desc->rdwr & SPI_XFER_WRITE) {
            write_data += le32_to_cpu(desc->len);
        }

        /* If rdwr without SPI_XFER_READ flag, not need to resize
         * read buffer
         */
        if (desc->rdwr & SPI_XFER_READ) {
            read_buf += le32_to_cpu(desc->len);
        }

        /* if cs_change enable, change the chip-select pin signal */
        if (desc->cs_change) {
            /* force deassert chip-select pin */
            ret = device_spi_deselect(spi_dev, request->chip_select);
            if (ret) {
                goto spi_err;
            }
            selected = false;
        }

        if (le16_to_cpu(desc->delay_usecs) > 0) {
            delay_ns(le16_to_cpu(desc->delay_usecs) * 1000);
        }
    }

spi_err:
    if (selected) {
        /* deassert chip-select pin */
        ret = device_spi_deselect(spi_dev, request->chip_select);
        if (ret) {
            greybus_op_response(cportid,
                            op_header,
                            GB_OP_UNKNOWN_ERROR,
                            NULL,
                            0);
            return -1;
        }
    }
    return greybus_op_response(cportid,
                              op_header,
                              GB_OP_SUCCESS,
                              (unsigned char *)response,
                              size);
}