C8051F32x firmware infrastructure

Sign in or create your account | Project List | Help

C8051F32x firmware infrastructure Git Source Tree

Root/f32x/c2-bitbang.c

1/*
2 * f32x/c2-bitbang.c - Basic C2 messages, bitbang
3 *
4 * Written 2008, 2010 by Werner Almesberger
5 * Copyright 2008, 2010 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13/*
14 * This file is meant to be #included by a .c file that defines C2CK and C2D,
15 * provides the gpio_* functions, etc.
16 */
17
18
19#include <stdint.h>
20#include <unistd.h>
21
22
23/* ----- Bit-level operations ---------------------------------------------- */
24
25
26static void c2_pulse(void)
27{
28    gpio_low(C2CK);
29    gpio_high(C2CK);
30}
31
32
33static void c2_send(uint32_t value, int bits)
34{
35    int i;
36
37    for (i = 0; i != bits; i++) {
38        gpio_set(C2D, (value >> i) & 1);
39        c2_pulse();
40    }
41}
42
43
44static uint32_t c2_recv(int bits)
45{
46    uint32_t v = 0;
47    int i;
48
49    for (i = 0; i != bits; i++) {
50        v |= gpio_get(C2D) << i;
51        c2_pulse();
52        }
53    return v;
54}
55
56
57/* ----- C2 Register read/write -------------------------------------------- */
58
59
60static void c2_addr_write(uint8_t addr)
61{
62    c2_pulse();
63    gpio_output(C2D);
64    c2_send(C2_ADDR_WRITE, 2);
65    c2_send(addr, 8);
66    gpio_input(C2D);
67    c2_pulse();
68}
69
70
71static uint8_t c2_addr_read(void)
72{
73    c2_pulse();
74    gpio_output(C2D);
75    c2_send(C2_ADDR_READ, 2);
76    gpio_input(C2D);
77    c2_pulse();
78    return c2_recv(8);
79}
80
81
82static void c2_data_write(uint32_t data, int bytes)
83{
84    c2_pulse();
85    gpio_output(C2D);
86    c2_send(C2_DATA_WRITE, 2);
87    c2_send(bytes-1, 2);
88    c2_send(data, 8*bytes);
89    gpio_input(C2D);
90    c2_pulse();
91    while (!c2_recv(1));
92}
93
94
95static uint32_t c2_data_read(int bytes)
96{
97    c2_pulse();
98    gpio_output(C2D);
99    c2_send(C2_DATA_READ, 2);
100    c2_send(bytes-1, 2);
101    gpio_input(C2D);
102    c2_pulse();
103    while (!c2_recv(1));
104    return c2_recv(8*bytes);
105}
106
107
108/* ----- C2 initialization ------------------------------------------------- */
109
110
111static void c2_init(void)
112{
113    gpio_input(C2D);
114    gpio_output(C2CK);
115    gpio_low(C2CK);
116    usleep(20);
117    gpio_high(C2CK);
118    usleep(2);
119}
120
121
122static void c2_reset(void)
123{
124    gpio_input(C2D);
125    gpio_low(C2CK);
126    usleep(20);
127// gpio_input(C2CK);
128    gpio_output(C2CK);
129    gpio_high(C2CK);
130}
131

Archive Download this file

Branches:
master



interactive