SHARE
TWEET

Untitled

a guest Jun 20th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include "fpm.h"
  4. #define F_CPU 16000000
  5. #define BAUD 57600
  6. #define BRC ((F_CPU/16/BAUD) - 1)
  7. #define UART_BUFFER_SIZE 512
  8. #include <util/delay.h>
  9.  
  10. volatile uint8_t *UART_buffer;
  11. uint8_t available = 0;
  12. uint16_t buffer_size = 0;
  13. uint16_t UART_write_index = 0;
  14. uint16_t UART_read_index = 0;
  15. volatile uint32_t milliseconds = 0;
  16.  
  17. void setup(void);
  18. uint16_t read_packet(uint8_t *bytes, uint16_t len);
  19. void send_packet(uint8_t *bytes, uint16_t len);
  20. uint16_t avail_packet(void);
  21. uint32_t millis(void);
  22.  
  23. int main(void)
  24. {
  25.     setup();
  26.  
  27.     FPM finger;
  28.     FPM_System_Params params;
  29.     finger.address = 0xFFFFFFFF;
  30.     finger.password = 0;
  31.     finger.avail_func = avail_packet;
  32.     finger.read_func = read_packet;
  33.     finger.write_func = send_packet;
  34.  
  35.     if (fpm_begin(&finger, 1000, 2000)) {
  36.         fpm_read_params(&finger, &params);
  37.         printf("Found fingerprint sensor!rn");
  38.         printf("Capacity: %drn", params.capacity);
  39.         printf("Packet length: %drn", fpm_packet_lengths[params.packet_len]);
  40.     }
  41.     else {
  42.         printf("Did not find fingerprint sensor :(rn");
  43.         while (1);
  44.     }
  45. }
  46.  
  47. void setup(void)
  48. {
  49.     UBRR0H = (BRC >> 8); //Shifts the BRC into the high BAUD register.  
  50.     UBRR0L = BRC; //Shifts the BRC into the low BAUD register.
  51.  
  52.     UCSR0B = (1 << TXEN0); //Sets the TX enable bit.
  53.     UCSR0B = (1 << RXEN0); //Sets the RX enable bit.
  54.     UCSR0C = (1 << UCSZ00) | (1 << UCSZ01); //Sets packet size.
  55.  
  56.     UART_buffer = malloc(512 * sizeof(uint8_t));
  57. }
  58.  
  59. uint16_t read_packet(uint8_t *bytes, uint16_t len)
  60. {
  61.     *bytes = UART_buffer;
  62.     for(int i = 0; i < available; i++)
  63.         UART_buffer[i] = '';
  64.  
  65.     return 0;
  66. }
  67.  
  68. void send_packet(uint8_t *bytes, uint16_t len)
  69. {
  70.     while(len > 0)
  71.     {
  72.         UDR0 = *bytes++;
  73.         len--;
  74.     }
  75. }
  76.  
  77. uint16_t avail_packet(void)
  78. {  
  79.     return buffer_size;
  80. }
  81.  
  82. uint32_t millis(void)
  83. {
  84.     return milliseconds;
  85. }
  86.  
  87. ISR(USART_RX_vect)
  88. {
  89.     UART_buffer[UART_write_index] = UDR0;
  90.     UART_write_index++;
  91.     buffer_size++;
  92.  
  93.     if(UART_write_index > UART_BUFFER_SIZE)
  94.         UART_write_index = 0;
  95. }
  96.  
  97. /*ISR(TIMER0_COMPA_vect)
  98. {
  99.     ++milliseconds;
  100. }*/
  101.      
  102. uint8_t fpm_begin(FPM * fpm, fpm_millis_func _millis_func, fpm_delay_func _delay_func) {
  103.     millis_func = _millis_func;
  104.     delay_func = _delay_func;
  105.  
  106.     delay_func(1000);            // 500 ms at least according to datasheet
  107.  
  108.     fpm->buffer[0] = FPM_VERIFYPASSWORD;
  109.     fpm->buffer[1] = (fpm->password >> 24) & 0xff; fpm->buffer[2] = (fpm->password >> 16) & 0xff;
  110.     fpm->buffer[3] = (fpm->password >> 8) & 0xff; fpm->buffer[4] = fpm->password & 0xff;
  111.     write_packet(fpm, FPM_COMMANDPACKET, fpm->buffer, 5);
  112.  
  113.     uint8_t confirm_code = 0;
  114.     int16_t len = read_ack_get_response(fpm, &confirm_code);
  115.  
  116.     if (len < 0 || confirm_code != FPM_OK)
  117.         return 0;
  118.  
  119.     if (fpm_read_params(fpm, NULL) != FPM_OK)
  120.         return 0;
  121.  
  122.     return 1;
  123. }
  124.      
  125. static int16_t get_reply(FPM * fpm, uint8_t * replyBuf, uint16_t buflen,
  126.                         uint8_t * pktid, fpm_uart_write_func out_stream) {
  127.  
  128.     FPM_State state = FPM_STATE_READ_HEADER;
  129.  
  130.     uint16_t header = 0;
  131.     uint8_t pid = 0;
  132.     uint16_t length = 0;
  133.     uint16_t chksum = 0;
  134.     uint16_t remn = 0;
  135.  
  136.     uint32_t last_read = millis_func();
  137.     //Below is additional code we don't reach...
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top