aboutsummaryrefslogtreecommitdiff
path: root/example-fsm-55/hiroshi-ayumi.c
blob: 8c47d54c20a6462ce66defdde22369657ae915fc (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <stdint.h>
#include <stdlib.h>
#include <chopstx.h>

#include "board.h"

#define PERIPH_BASE	0x40000000
#define APBPERIPH_BASE   PERIPH_BASE
#define APB2PERIPH_BASE	(PERIPH_BASE + 0x10000)
#define AHBPERIPH_BASE	(PERIPH_BASE + 0x20000)
#define AHB2PERIPH_BASE	(PERIPH_BASE + 0x08000000)

struct GPIO {
  volatile uint32_t MODER;
  volatile uint16_t OTYPER;
  uint16_t dummy0;
  volatile uint32_t OSPEEDR;
  volatile uint32_t PUPDR;
  volatile uint16_t IDR;
  uint16_t dummy1;
  volatile uint16_t ODR;
  uint16_t dummy2;
  volatile uint16_t BSRR;
  uint16_t dummy3;
  volatile uint32_t LCKR;
  volatile uint32_t AFR[2];
  volatile uint16_t BRR;
  uint16_t dummy4;
};
#define GPIOA_BASE	(AHB2PERIPH_BASE + 0x0000)
#define GPIOA		((struct GPIO *) GPIOA_BASE)
#define GPIOF_BASE	(AHB2PERIPH_BASE + 0x1400)
#define GPIOF		((struct GPIO *) GPIOF_BASE)

static struct GPIO *const GPIO_LED = ((struct GPIO *const) GPIO_LED_BASE);
static struct GPIO *const GPIO_OTHER = ((struct GPIO *const) GPIO_OTHER_BASE);

static chopstx_mutex_t mtx;
static chopstx_cond_t cnd0;

static uint8_t
user_button (void)
{
  return (GPIO_OTHER->IDR & 1) == 0;
}

static uint8_t l_data[5];

static void
set_led_display (uint32_t data)
{
  l_data[0] = (data >>  0) & 0x1f;
  l_data[1] = (data >>  5) & 0x1f;
  l_data[2] = (data >> 10) & 0x1f;
  l_data[3] = (data >> 15) & 0x1f;
  l_data[4] = (data >> 20) & 0x1f;
}


static void
wait_for (uint32_t usec)
{
  chopstx_usec_wait (usec);
}

static void
led_prepare_row (uint8_t col)
{
  uint16_t data = 0x1f;

  data |= ((l_data[0] & (1 << col)) ? 1 : 0) << 5;
  data |= ((l_data[1] & (1 << col)) ? 1 : 0) << 6;
  data |= ((l_data[2] & (1 << col)) ? 1 : 0) << 7;
  data |= ((l_data[3] & (1 << col)) ? 1 : 0) << 9;
  data |= ((l_data[4] & (1 << col)) ? 1 : 0) << 10;
  GPIO_LED->ODR = data;
}

static void
led_enable_column (uint8_t col)
{
  GPIO_LED->BRR = (1 << col);
}

static void *
led (void *arg)
{
  (void)arg;

  chopstx_mutex_lock (&mtx);
  chopstx_cond_wait (&cnd0, &mtx);
  chopstx_mutex_unlock (&mtx);

  while (1)
    {
      int i;

      for (i = 0; i < 5; i++)
	{
	  led_prepare_row (i);
	  led_enable_column (i);
	  wait_for (1000);
	}
    }

  return NULL;
}

#define PRIO_LED 3

extern uint8_t __process1_stack_base__, __process1_stack_size__;

const uint32_t __stackaddr_led = (uint32_t)&__process1_stack_base__;
const size_t __stacksize_led = (size_t)&__process1_stack_size__;

#define DATA55(x0,x1,x2,x3,x4) (x0<<20)|(x1<<15)|(x2<<10)|(x3<< 5)|(x4<< 0)
#define SIZE55(img) (sizeof (img) / sizeof (uint32_t))

static uint32_t image0[] = {
  DATA55 (0x00,0x00,0x00,0x00,0x00),
  DATA55 (0x00,0x01,0x00,0x00,0x00),
  DATA55 (0x01,0x03,0x01,0x01,0x00),
  DATA55 (0x02,0x06,0x02,0x02,0x01),
  DATA55 (0x05,0x0d,0x05,0x05,0x03),
  DATA55 (0x0b,0x1a,0x0a,0x0a,0x06),
  DATA55 (0x16,0x14,0x14,0x14,0x0c),
  DATA55 (0x0d,0x08,0x09,0x08,0x19),
  DATA55 (0x1b,0x11,0x12,0x10,0x13),
  DATA55 (0x17,0x03,0x04,0x00,0x07),
  DATA55 (0x0f,0x06,0x09,0x01,0x0e),
  DATA55 (0x1e,0x0c,0x12,0x02,0x1c),
  DATA55 (0x1d,0x19,0x05,0x05,0x18),
  DATA55 (0x1a,0x12,0x0a,0x0a,0x11),
  DATA55 (0x14,0x04,0x14,0x14,0x03),
  DATA55 (0x08,0x08,0x08,0x09,0x06),
  DATA55 (0x10,0x10,0x10,0x12,0x0c),
  DATA55 (0x00,0x00,0x00,0x04,0x18),
  DATA55 (0x00,0x00,0x00,0x08,0x10),
  DATA55 (0x00,0x00,0x00,0x10,0x00),
  DATA55 (0x00,0x00,0x00,0x00,0x00),
  DATA55 (0x00,0x00,0x00,0x00,0x00),
  DATA55 (0x00,0x00,0x00,0x00,0x00),
  DATA55 (0x00,0x00,0x00,0x00,0x00),
  DATA55 (0x00,0x00,0x00,0x00,0x00),
};

static uint32_t image1[] = {
  DATA55 (0x00,0x00,0x00,0x00,0x00),
  DATA55 (0x01,0x00,0x00,0x00,0x01),
  DATA55 (0x03,0x00,0x00,0x01,0x03),
  DATA55 (0x07,0x01,0x01,0x03,0x06),
  DATA55 (0x0f,0x02,0x02,0x06,0x0c),
  DATA55 (0x1f,0x05,0x05,0x0c,0x18),
  DATA55 (0x1e,0x0a,0x0a,0x18,0x11),
  DATA55 (0x1d,0x14,0x14,0x10,0x03),
  DATA55 (0x1b,0x08,0x08,0x00,0x07),
  DATA55 (0x17,0x11,0x11,0x01,0x0f),
  DATA55 (0x0e,0x02,0x02,0x02,0x1f),
  DATA55 (0x1c,0x04,0x04,0x04,0x1e),
  DATA55 (0x19,0x08,0x09,0x08,0x1d),
  DATA55 (0x13,0x10,0x13,0x10,0x1b),
  DATA55 (0x07,0x00,0x07,0x00,0x17),
  DATA55 (0x0f,0x00,0x0f,0x00,0x0f),
  DATA55 (0x1e,0x00,0x1e,0x00,0x1e),
  DATA55 (0x1c,0x00,0x1c,0x00,0x1c),
  DATA55 (0x18,0x00,0x18,0x00,0x18),
  DATA55 (0x10,0x00,0x10,0x00,0x10),
  DATA55 (0x00,0x00,0x00,0x00,0x00),
  DATA55 (0x00,0x00,0x00,0x00,0x00),
  DATA55 (0x00,0x00,0x00,0x00,0x00),
  DATA55 (0x00,0x00,0x00,0x00,0x00),
  DATA55 (0x00,0x00,0x00,0x00,0x00),
};

int
main (int argc, const char *argv[])
{
  uint8_t state = 0;

  (void)argc;
  (void)argv;

  chopstx_mutex_init (&mtx);
  chopstx_cond_init (&cnd0);

  chopstx_create (PRIO_LED, __stackaddr_led, __stacksize_led, led, NULL);

  chopstx_usec_wait (200*1000);

  chopstx_mutex_lock (&mtx);
  chopstx_cond_signal (&cnd0);
  chopstx_mutex_unlock (&mtx);

  while (1)
    {
      unsigned int i;

      if (state)
	for (i = 0; i < SIZE55 (image0); i++)
	  {
	    if (user_button ())
	      state = 0;
	    set_led_display (image0[i]);
	    wait_for (200*1000);
	  }
      else
	for (i = 0; i < SIZE55 (image1); i++)
	  {
	    if (user_button ())
	      state = 1;
	    set_led_display (image1[i]);
	    wait_for (200*1000);
	  }
    }

  return 0;
}