summaryrefslogtreecommitdiffstats
path: root/boot/max7219.c
blob: 3e065627d1eb1009cbd542b4e504fd7cd23167a7 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "project.h"

#define NCS     (GPIO7)
#define NCS_PORT  GPIOG

#define SCK   (GPIO3)
#define SCK_PORT  GPIOB

#define MOSI    (GPIO5)
#define MOSI_PORT GPIOB


static void
set (int sck, int ncs, int mosi)
{
  if (sck)
    SET (SCK);
  else
    CLEAR (SCK);


  if (ncs)
    SET (NCS);
  else
    CLEAR (NCS);


  if (mosi)
    SET (MOSI);
  else
    CLEAR (MOSI);

  //  delay_us(1);
  //delay_us(10);

}

static void
spip_send_8 (uint8_t wot)
{
  int i;

  for (i = 0; i < 8; ++i) {
    set (0, 0, wot & 0x80);
    set (1, 0, wot & 0x80);
    set (0, 0, wot & 0x80);
    wot <<= 1;
  }
}



static void
write_reg (uint8_t reg, uint8_t data)
{

  set (0, 1, 0);
  set (0, 0, 0);

  spip_send_8 (reg);
  spip_send_8 (data);

  spip_send_8 (reg);
  spip_send_8 (data);

  spip_send_8 (reg);
  spip_send_8 (data);

  set (0, 0, 0);
  set (0, 1, 0);
}


static void
write_regs (uint8_t reg, uint8_t data1, uint8_t data2, uint8_t data3)
{

  set (0, 1, 0);
  set (0, 0, 0);

  spip_send_8 (reg);
  spip_send_8 (data3);

  spip_send_8 (reg);
  spip_send_8 (data2);

  spip_send_8 (reg);
  spip_send_8 (data1);

  set (0, 0, 0);
  set (0, 1, 0);
}


void
max7219_init (void)
{
  MAP_OUTPUT_PP (SCK);
  MAP_OUTPUT_PP (NCS);
  MAP_OUTPUT_PP (MOSI);

  set (0, 1, 0);


    write_reg (0xc, 0x1); //Power up
    write_reg (0xf, 0x0); //normal mode

    write_reg (0x9, 0x0); //No decode
    write_reg (0xb, 0x7); //8 digits
    write_regs (0xa,15,15,15); //max brightness
    write_reg (1,0);
    write_reg (2,0);
    write_reg (3,0);
    write_reg (4,0);
    write_reg (5,0);
    write_regs (6,0xbe,0,0);
    write_regs (7,0x47,0,0);
    write_regs (8,0x3d,0,0);  

}