summaryrefslogtreecommitdiffstats
path: root/crypto/gt22l16a1y.c
blob: b997c36708ec6591bea2bca0a86349b1140ae835 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "project.h"

#define SCLK   17
#define MOSI   18
#define NCS    19
#define MISO   20

#define READ 0x3
#define FAST_READ 0xb


static inline void d(void)
{
nrf_delay_us(1);
}

void
gt_init (void)
{
  NRF_GPIO->PIN_CNF[SCLK] =
    (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
    (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
    (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
    (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) |
    (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);

  NRF_GPIO->PIN_CNF[MOSI] =
    (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
    (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
    (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
    (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) |
    (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);


  NRF_GPIO->PIN_CNF[NCS] =
    (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
    (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
    (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
    (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) |
    (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);



  NRF_GPIO->PIN_CNF[MISO] =
    (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
    (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
    (GPIO_PIN_CNF_PULL_Pulldown << GPIO_PIN_CNF_PULL_Pos) |
    (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
    (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);


  NRF_GPIO->DIRSET = (1 << MOSI) | (1 << NCS) | (1<<SCLK);
  NRF_GPIO->DIRCLR = (1 << MISO);

  NRF_GPIO->OUTSET = (1 << NCS) | (1<<MISO);
}

void gt_read(uint32_t a,uint8_t * buf,size_t len)
{
  uint32_t c;
  uint8_t v;
  
  NRF_GPIO->OUTCLR = (1 << SCLK);
  d();
  NRF_GPIO->OUTCLR = (1 << NCS);
  NRF_GPIO->OUTSET = (1 << NCS);
  d();
  
 a |= READ << 24;

 a=0x3000000;
 a+=768+80;

  for (c=0x80000000;c;c>>=1) {
	if (c&a) 
		  NRF_GPIO->OUTSET = (1 << MOSI);
	else
		  NRF_GPIO->OUTCLR = (1 << MOSI);
  d();

  NRF_GPIO->OUTSET = (1 << SCLK);
  d();
  NRF_GPIO->OUTCLR = (1 << SCLK);
  d();
  }

  while (len--) {
	v=0;
	for (c=0x80;c;c>>=1) {
	if (NRF_GPIO->IN & (1<<MISO)) v|=c;
	


  NRF_GPIO->OUTSET = (1 << SCLK);
  d();
  NRF_GPIO->OUTCLR = (1 << SCLK);
  d();
	}
	*(buf++)=v;
  }

  d();
	
  NRF_GPIO->OUTSET = (1 << NCS);
  d();
	

}