• R/O
  • SSH
  • HTTPS

qrobosdk: コミット


コミットメタ情報

リビジョン1916 (tree)
日時2010-09-17 23:48:35
作者satofumi

ログメッセージ

SCI1 送信が割り込みで動作

変更サマリ

差分

--- branches/tsukuba_2010/libs/run_drive/Makefile (revision 1915)
+++ branches/tsukuba_2010/libs/run_drive/Makefile (revision 1916)
@@ -28,7 +28,7 @@
2828
2929 .PHONY : all clean html upload
3030 ######################################################################
31-run_drive.mot : run_drive.o initialize_state.o control_state.o pause_state.o clock_initialize.o imask.o communication_handler.o protocol_handler.o sci_read_write.o emergency_io_control.o timer_control.o path_follow.o position_handler.o velocity_handler.o body_handler.o wheel_velocity.o motor_velocity.o motor_pwm.o encoder_reader.o odometry_calculate.o mtu2_initialize.o
31+run_drive.mot : run_drive.o initialize_state.o control_state.o pause_state.o clock_initialize.o imask.o communication_handler.o protocol_handler.o sci_interrupt_read_write.o emergency_io_control.o timer_control.o path_follow.o position_handler.o velocity_handler.o body_handler.o wheel_velocity.o motor_velocity.o motor_pwm.o encoder_reader.o odometry_calculate.o mtu2_initialize.o ring_buffer.o
3232
3333 # DO NOT DELETE
3434
@@ -68,8 +68,8 @@
6868 initialize_state.o: emergency_io_control.h odometry_calculate.h path_follow.h
6969 initialize_state.o: wheel_velocity.h protocol_handler.h interrupt_priority.h
7070 motor_pwm.o: motor_pwm.h mtu2_initialize.h
71-motor_velocity.o: motor_velocity.h motor_t.h motor_pwm.h encoder_reader.h
72-motor_velocity.o: encoder_t.h
71+motor_velocity.o: motor_velocity.h motor_t.h robot_parameter.h motor_pwm.h
72+motor_velocity.o: encoder_reader.h encoder_t.h
7373 mtu2_initialize.o: mtu2_initialize.h
7474 odometry_calculate.o: odometry_calculate.h odometry_t.h
7575 path_follow.o: path_t.h position_handler.h
@@ -82,6 +82,7 @@
8282 protocol_handler.o: communication_handler.h
8383 run_drive.o: initialize_state.h run_t.h controller_config.h odometry_t.h
8484 run_drive.o: path_t.h wheel_t.h encoder_t.h motor_t.h control_state.h
85+sci_interrupt_read_write.o: sci_read_write.h cpu_clock.h
8586 sci_read_write.o: sci_read_write.h cpu_clock.h
8687 sci_utilities.o: sci_utilities.h sci_read_write.h
8788 std_string.o: std_string.h
--- branches/tsukuba_2010/libs/run_drive/ring_buffer.c (nonexistent)
+++ branches/tsukuba_2010/libs/run_drive/ring_buffer.c (revision 1916)
@@ -0,0 +1,136 @@
1+/*!
2+ \file
3+ \brief リングバッファ
4+
5+ \author Satofumi KAMIMURA
6+
7+ $Id$
8+*/
9+
10+#include "ring_buffer.h"
11+
12+
13+void ring_initialize(ring_buffer_t *ring, char *buffer, const int shift_length)
14+{
15+ ring->buffer = buffer;
16+ ring->buffer_size = 1 << shift_length;
17+ ring_clear(ring);
18+}
19+
20+
21+void ring_clear(ring_buffer_t *ring)
22+{
23+ ring->first = 0;
24+ ring->last = 0;
25+}
26+
27+
28+int ring_size(const ring_buffer_t *ring)
29+{
30+ int first = ring->first;
31+ int last = ring->last;
32+
33+ return (last >= first) ? last - first : ring->buffer_size - (first - last);
34+}
35+
36+
37+int ring_capacity(const ring_buffer_t *ring)
38+{
39+ return ring->buffer_size - 1;
40+}
41+
42+
43+static void byte_move(char *dest, const char *src, int n)
44+{
45+ const char *last_p = dest + n;
46+ while (dest < last_p) {
47+ *dest++ = *src++;
48+ }
49+}
50+
51+
52+int ring_write(ring_buffer_t *ring, const char *data, int size)
53+{
54+ int free_size = ring_capacity(ring) - ring_size(ring);
55+ int push_size = (size > free_size) ? free_size : size;
56+
57+ // データ配置
58+ if (ring->first <= ring->last) {
59+ // last から buffer_size 終端までに配置
60+ int left_size = 0;
61+ int to_end = ring->buffer_size - ring->last;
62+ int move_size = (to_end > push_size) ? push_size : to_end;
63+
64+ byte_move(&ring->buffer[ring->last], data, move_size);
65+ ring->last += move_size;
66+ ring->last &= (ring->buffer_size -1);
67+
68+ left_size = push_size - move_size;
69+ if (left_size > 0) {
70+ // 0 から first の前までを配置
71+ byte_move(ring->buffer, &data[move_size], left_size);
72+ ring->last = left_size;
73+ }
74+ } else {
75+ // last から first の前まで配置
76+ byte_move(&ring->buffer[ring->last], data, push_size);
77+ ring->last += push_size;
78+ }
79+ return push_size;
80+}
81+
82+
83+int ring_read(ring_buffer_t *ring, char *buffer, int size)
84+{
85+ // データ取得
86+ int current_size = ring_size(ring);
87+ int pop_size = (size > current_size) ? current_size : size;
88+
89+ if (ring->first <= ring->last) {
90+ byte_move(buffer, &ring->buffer[ring->first], pop_size);
91+ ring->first += pop_size;
92+
93+ } else {
94+ // first から buffer_size 終端までを配置
95+ int left_size = 0;
96+ int to_end = ring->buffer_size - ring->first;
97+ int move_size = (pop_size < to_end) ? pop_size : to_end;
98+ byte_move(buffer, &ring->buffer[ring->first], move_size);
99+
100+ ring->first += move_size;
101+ ring->first &= (ring->buffer_size -1);
102+
103+ left_size = pop_size - move_size;
104+ if (left_size > 0) {
105+ // 0 から last の前までを配置
106+ byte_move(&buffer[move_size], ring->buffer, left_size);
107+ ring->first += left_size;
108+ }
109+ }
110+ return pop_size;
111+}
112+
113+
114+#if 0
115+int ring_readPtr(ring_buffer_t *ring, char **ptr, int size)
116+{
117+ int diff;
118+ int readable_size;
119+ int read_size;
120+ int actual_size = ring_size(ring);
121+
122+ if (size < actual_size) {
123+ actual_size = size;
124+ }
125+
126+ *ptr = &ring->buffer[ring->first];
127+ diff = ring->last - ring->first;
128+ readable_size = (diff > 0) ? diff : ring->buffer_size - ring->first;
129+ read_size = (readable_size > actual_size) ? actual_size : readable_size;
130+
131+ ring->first += read_size;
132+ ring->first &= (ring->buffer_size -1);
133+
134+ return read_size;
135+}
136+#endif
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
--- branches/tsukuba_2010/libs/run_drive/ring_buffer.h (nonexistent)
+++ branches/tsukuba_2010/libs/run_drive/ring_buffer.h (revision 1916)
@@ -0,0 +1,88 @@
1+#ifndef RING_BUFFER_H
2+#define RING_BUFFER_H
3+
4+/*!
5+ \file
6+ \brief リングバッファ
7+
8+ \author Satofumi KAMIMURA
9+
10+ $Id$
11+*/
12+
13+
14+//! リングバッファの管理情報
15+typedef struct
16+{
17+ char *buffer; //!< バッファへのポインタ
18+ int buffer_size; //!< バッファサイズ
19+ int first; //!< バッファの先頭位置
20+ int last; //!< バッファの最終位置
21+} ring_buffer_t;
22+
23+
24+/*!
25+ \brief 初期化
26+
27+ \param[in] ring リングバッファの構造体
28+ \param[in] buffer 割り当てるバッファ
29+ \param[in] shift_length バッファサイズの 2 の乗数
30+*/
31+extern void ring_initialize(ring_buffer_t *ring,
32+ char *buffer, const int shift_length);
33+
34+
35+/*!
36+ \brief リングバッファのクリア
37+
38+ \param[in] ring リングバッファの構造体
39+*/
40+extern void ring_clear(ring_buffer_t *ring);
41+
42+
43+/*!
44+ \brief 格納データ数を返す
45+
46+ \param[in] ring リングバッファの構造体
47+*/
48+extern int ring_size(const ring_buffer_t *ring);
49+
50+
51+/*!
52+ \brief 最大の格納データ数を返す
53+
54+ \param[in] ring リングバッファの構造体
55+*/
56+extern int ring_capacity(const ring_buffer_t *ring);
57+
58+
59+/*!
60+ \brief データの格納
61+
62+ \param[in] ring リングバッファの構造体
63+ \param[in] data データ
64+ \param[in] size データサイズ
65+
66+ \return 格納したデータ数
67+*/
68+extern int ring_write(ring_buffer_t *ring, const char *data, int size);
69+
70+
71+/*!
72+ \brief データの取り出し
73+
74+ \param[in] ring リングバッファの構造体
75+ \param[out] buffer データ
76+ \param[in] size 最大のデータサイズ
77+
78+ \return 取り出したデータ数
79+*/
80+extern int ring_read(ring_buffer_t *ring, char *buffer, int size);
81+
82+
83+#if 0
84+//! 内部ポインタから直に読み出しを行う
85+extern int ring_readPtr(ring_buffer_t *ring, char **ptr, int size);
86+#endif
87+
88+#endif /* ! RING_BUFFER_H */
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
--- branches/tsukuba_2010/libs/run_drive/sci_interrupt_read_write.c (nonexistent)
+++ branches/tsukuba_2010/libs/run_drive/sci_interrupt_read_write.c (revision 1916)
@@ -0,0 +1,145 @@
1+/*!
2+ \file
3+ \brief SCI 通信 (割り込み版)
4+
5+ \author Satofumi KAMIMURA
6+
7+ $Id$
8+*/
9+
10+#include "sci_read_write.h"
11+#include "imask.h"
12+#include "cpu_clock.h"
13+#include "ring_buffer.h"
14+#include <7125S.H>
15+
16+
17+enum {
18+ BUFFER_SIZE_SHIFT = 8,
19+ BUFFER_SIZE = 1 << BUFFER_SIZE_SHIFT,
20+};
21+
22+
23+static unsigned char interrupt_priority_ = 0;
24+static ring_buffer_t read_ring_;
25+static ring_buffer_t write_ring_;
26+static char read_buffer_[BUFFER_SIZE];
27+static char write_buffer_[BUFFER_SIZE];
28+
29+
30+void sci_initialize(int priority, long baudrate)
31+{
32+ interrupt_priority_ = priority;
33+
34+ // スタンバイ解除
35+ STB.CR3.BYTE = 0xef;
36+
37+ // 通信と割り込みを禁止し、内部クロックで動作させる
38+ SCI1.SCSCR.BYTE = 0x00;
39+
40+ // 8bit, no parity, 1 stop bit
41+ SCI1.SCSMR.BYTE = 0x00;
42+ SCI1.SCSDCR.BYTE = 0xf2;
43+
44+ // set baudrate
45+ SCI1.SCBRR = (int)((P_MHz / 1024.0) * 2 * 1000000 / 64 / baudrate) - 1;
46+
47+ // 1 bit のウェイト
48+ // !!! 1 bit のウェイト数を適切に設定すること
49+ enum { OneBitWait = 1000 };
50+ volatile int i;
51+ for (i = 0; i < OneBitWait; ++i) {
52+ ;
53+ }
54+
55+ // TXD1(PA4)
56+ PFC.PACRL2.WORD &= ~0x0007;
57+ PFC.PACRL2.WORD |= 0x0001;
58+
59+ // RXD1(PA3)
60+ PFC.PACRL1.WORD &= ~0x7000;
61+ PFC.PACRL1.WORD |= 0x1000;
62+
63+ // リングバッファの初期化
64+ ring_initialize(&read_ring_, read_buffer_, BUFFER_SIZE_SHIFT);
65+ ring_initialize(&write_ring_, write_buffer_, BUFFER_SIZE_SHIFT);
66+
67+ // 割り込みの初期化
68+ //INTC.IPRL.BIT._SCI1 = priority;
69+ INTC.IPRL.WORD &= ~0x0f00;
70+ INTC.IPRL.WORD |= priority << 8;
71+ SCI1.SCSCR.BYTE |= 0x40;
72+ // !!! 以下の割り込みを許可する必要があるかは、要検討
73+ // !!! SCSCR の TIE, TEIE, MPIE で割り込みを許可
74+
75+ // 通信を許可
76+ SCI1.SCSCR.BYTE |= 0x30;
77+}
78+
79+
80+int sci_write(const char *data, int size)
81+{
82+ unsigned char current_level = get_imask_exr();
83+ int n;
84+
85+ // 送信データの格納
86+ set_imask_exr(interrupt_priority_);
87+ n = ring_write(&write_ring_, data, size);
88+
89+ if (n > 0) {
90+ // 送信割り込みの許可
91+ SCI1.SCSCR.BYTE |= 0x80;
92+ }
93+ set_imask_exr(current_level);
94+
95+ return n;
96+}
97+
98+
99+int sci_read(char *data, int max_size)
100+{
101+ int current_level = get_imask_exr();
102+ int read_size;
103+
104+ set_imask_exr(interrupt_priority_);
105+ read_size = ring_read(&read_ring_, data, max_size);
106+ set_imask_exr(current_level);
107+
108+ return read_size;
109+}
110+
111+
112+#pragma interrupt
113+void eri1(void)
114+{
115+ SCI1.SCSSR.BYTE &= ~0x38;
116+}
117+
118+
119+#pragma interrupt
120+void rxi1(void)
121+{
122+ char ch = SCI1.SCRDR;
123+ ring_write(&read_ring_, &ch, 1);
124+ SCI1.SCSSR.BYTE &= ~0x40;
125+}
126+
127+
128+#pragma interrupt
129+void txi1(void)
130+{
131+ char ch;
132+
133+ if (ring_read(&write_ring_, &ch, 1) <= 0) {
134+ SCI1.SCSCR.BYTE &= ~0x80; // 送信終了。以後の送信割り込みを禁止
135+ } else {
136+ SCI1.SCTDR = ch;
137+ SCI1.SCSSR.BYTE &= ~0x80;
138+ }
139+}
140+
141+
142+#pragma interrupt
143+void tei1(void)
144+{
145+}
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
--- branches/tsukuba_2010/libs/run_drive/sci_interrupt_read_write.h (nonexistent)
+++ branches/tsukuba_2010/libs/run_drive/sci_interrupt_read_write.h (revision 1916)
@@ -0,0 +1,28 @@
1+#ifndef QRK_SCI_INTERRUPT_READ_WRITE_H
2+#define QRK_SCI_INTERRUPT_READ_WRITE_H
3+
4+/*!
5+ \file
6+ \brief SCI 通信
7+
8+ \author Satofumi KAMIMURA
9+
10+ $Id$
11+*/
12+
13+
14+//! 初期化
15+extern void sci_initialize(int priority, long baudrate);
16+
17+
18+extern int sci_write(const char *data, int size);
19+
20+
21+/*!
22+ \brief 受信
23+
24+ !!! 割り込み版では、受信済みのデータを格納して、即座に戻る。データがなかった場合は 0 を返すようにする
25+*/
26+extern int sci_read(char *data, int max_size);
27+
28+#endif /* !QRK_SCI_INTERRUPT_READ_WRITE_H */
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
--- branches/tsukuba_2010/libs/run_drive/samples/sci_interrupt_write.c (nonexistent)
+++ branches/tsukuba_2010/libs/run_drive/samples/sci_interrupt_write.c (revision 1916)
@@ -0,0 +1,31 @@
1+/*!
2+ \example sci_interrupt_write.c 割り込み版のシリアル書き込みサンプル
3+
4+ \author Satofumi KAMIMURA
5+
6+ $Id$
7+*/
8+
9+#include "clock_initialize.h"
10+#include "imask.h"
11+#include "sci_interrupt_read_write.h"
12+#include "sci_utilities.h"
13+#include "interrupt_priority.h"
14+
15+
16+int main(void)
17+{
18+ const char message[] = "hello.\r\n";
19+
20+ imask_initialize();
21+ clock_initialize();
22+ sci_initialize(INTERRUPT_PRIORITY_COMMUNICATION, 38400);
23+ set_imask_exr(0);
24+
25+ sci_write(message, sizeof(message) - 1);
26+ sci_puts("start\r\n");
27+
28+ while (1) {
29+ ;
30+ }
31+}
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
--- branches/tsukuba_2010/libs/run_drive/samples/Makefile (revision 1915)
+++ branches/tsukuba_2010/libs/run_drive/samples/Makefile (revision 1916)
@@ -22,6 +22,7 @@
2222 motor_velocity_run.mot \
2323 encoder_print.mot \
2424 wheel_velocity_run.mot \
25+ sci_interrupt_write.mot \
2526
2627 all : $(TARGET)
2728
@@ -43,6 +44,7 @@
4344 motor_velocity_run.mot : motor_velocity_run.o $(BASE_OBJ) $(SOURCE_DIR)/timer_control.o $(SOURCE_DIR)/encoder_reader.o $(SOURCE_DIR)/motor_pwm.o $(SOURCE_DIR)/motor_velocity.o $(SOURCE_DIR)/mtu2_initialize.o
4445 pwm_output.mot : pwm_output.o $(SOURCE_DIR)/pwm_control.o
4546 wheel_velocity_run.mot : wheel_velocity_run.o $(BASE_OBJ) $(SOURCE_DIR)/wheel_velocity.o $(SOURCE_DIR)/encoder_reader.o $(SOURCE_DIR)/motor_velocity.o $(SOURCE_DIR)/mtu2_initialize.o $(SOURCE_DIR)/motor_pwm.o $(SOURCE_DIR)/timer_control.o
47+sci_interrupt_write.mot : sci_interrupt_write.o $(BASE_OBJ) $(SOURCE_DIR)/sci_interrupt_read_write.o $(SOURCE_DIR)/sci_utilities.o $(SOURCE_DIR)/ring_buffer.o
4648
4749 # DO NOT DELETE
4850
@@ -57,6 +59,9 @@
5759 motor_velocity_run.o: ../encoder_t.h
5860 sci_echoback.o: ../clock_initialize.h ../imask.h ../sci_read_write.h
5961 sci_echoback.o: ../interrupt_priority.h
62+sci_interrupt_write.o: ../clock_initialize.h ../imask.h
63+sci_interrupt_write.o: ../sci_interrupt_read_write.h ../sci_utilities.h
64+sci_interrupt_write.o: ../interrupt_priority.h
6065 sci_write.o: ../clock_initialize.h ../imask.h ../sci_read_write.h
6166 sci_write.o: ../sci_utilities.h ../interrupt_priority.h
6267 timer_1sec.o: ../clock_initialize.h ../imask.h ../sci_read_write.h
旧リポジトリブラウザで表示