Toby's Study Blog
article thumbnail

๐Ÿš™ Bluetooth ๋ž€?

2.4GHz ์ฃผํŒŒ์ˆ˜ ๋Œ€์—ญ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ทผ๊ฑฐ๋ฆฌ ๋ฌด์„ ํ†ต์‹  ๊ธฐ์ˆ ์ด๋ฉฐ ์ „๋ ฅ ์†Œ๋ชจ๊ฐ€ ์ ๊ณ , ์—ฐ๊ฒฐ์ด ๊ฐ„ํŽธํ•œ ํŠน์ง•์„ ๊ฐ€์ง€๋Š” ํ†ต์‹ ์ด๋‹ค.
์™€์ดํŒŒ์ด์™€ ๋น„๊ตํ•˜์—ฌ ์ •๋ฆฌํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

                           ํŠน ์„ฑ                         ์™€์ดํŒŒ์ด                         ๋ธ”๋ฃจํˆฌ์Šค
                      ์ฃผํŒŒ์ˆ˜ ๋Œ€์—ญ 2.4GHz, 5GHz 2.4GHz
                  ์ตœ๋Œ€ ๋ฐ์ดํ„ฐ ์†๋„ 9.6Gbps 24Mbps
                       ์ „๋ ฅ ์†Œ๋น„ ๋†’์Œ ๋‚ฎ์Œ
                       ์—ฐ๊ฒฐ ๋ฒ”์œ„ 100m 10m
                           ๋ณด ์•ˆ ๋†’์Œ  ๋‚ฎ์Œ
                        ์‚ฌ์šฉ ์šฉ๋„ ์ธํ„ฐ๋„ท ์—ฐ๊ฒฐ, ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ ์ „์†ก ๊ทผ๊ฑฐ๋ฆฌ ์žฅ์น˜ ์—ฐ๊ฒฐ, ์†Œํ˜• ๋ฐ์ดํ„ฐ ์ „์†ก

๋ธ”๋ฃจํˆฌ์Šค ๋ชจ๋“ˆ [zs-040]

  • zs-040์€ ๋ธ”๋ฃจํˆฌ์Šค 4.0์„ ์ง€์›ํ•˜๋ฉฐ 2.4GHz ์ฃผํŒŒ์ˆ˜ ๋Œ€์—ญ, 1Mbps ์†๋„๋ฅผ ๊ฐ€์ง€๋Š” ๋ธ”๋ฃจํˆฌ์Šค ํ†ต์‹  ๋ชจ๋“ˆ์ด๋‹ค.
  • uart ๋˜ํ•œ ์ง€์›ํ•œ๋‹ค. 
  • F429zi MCU์—๋Š” ๋ธ”๋ฃจํˆฌ์Šค ๊ธฐ๋Šฅ์ด ์—†์–ด ๋ธ”๋ฃจํˆฌ์Šค ๋ชจ๋“ˆ์ด ๋”ฐ๋กœ ํ•„์š”ํ•˜๋‹ค
  • ํ•ด๋‹น ๋ชจ๋“ˆ์„ uart๋กœ ์‚ฌ์šฉํ•  ๋•Œ์™€ bluetooth๋กœ ์‚ฌ์šฉํ•  ๋•Œ ๋ชจ๋‘ rxd๋Š” gpio_in๋กœ ์„ค์ •ํ•œ PA9 ํ•€์—, txd๋Š” gpio_out๋กœ ์„ค์ •ํ•œ PA10 ํ•€์— ์—ฐ๊ฒฐํ•œ๋‹ค.
  • uart ์ผ๋•Œ๋Š” 5V์˜ ์ „์••์„ bluetooth ์ผ๋–„๋Š” 3.3V ์ „์••์„ VCC์— ์—ฐ๊ฒฐํ•œ๋‹ค,

zs 040

๐Ÿš™ ์‹ค์Šต ๋ชฉํ‘œ

  1. zs-040 ๋ชจ๋“ˆ - MCU ์—ฐ๊ฒฐ
  2. bluetooth ํ†ต์‹ ์„ ์œ„ํ•œ bluetooth.h, bluetooth.c ํŒŒ์ผ ์ž‘์„ฑ
  3. ์Šค๋งˆํŠธํฐ -- zs-040 bluetooth ์—ฐ๊ฒฐ๋จ ํ™•์ธ ๋ฐ led ์ œ์–ด, pwm ์ œ์–ด ํ™•์ธ

๐Ÿš™ ์‹ค์Šต ๊ฒฐ๊ณผ

https://github.com/Tobbyvv/stm32-project/tree/main/rtos_msg_q

zs-040 ๋ชจ๋“ˆ - MCU ์—ฐ๊ฒฐ 

 

VCC - CN8 7๋ฒˆ 3.3V (uart๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ 5V์— ์—ฐ๊ฒฐ)
GND - CN8 11๋ฒˆ GND
RXD - CN12 PA9
TXD - CN12 PA10 

bluetooth ํ†ต์‹ ์„ ์œ„ํ•œ bluetooth.h, bluetooth.c ํŒŒ์ผ ์ž‘์„ฑ

 2.1 bluetooth.h

#ifndef INC_BLUETOOTH_H_
#define INC_BLUETOOTH_H_

#ifdef __cplusplus
extern "C" {
#endif

void bt_init(void);
void bt_QPut(const void *msg_ptr, uint8_t msg_prio, uint32_t timeout);

#ifdef __cplusplus
}
#endif

#endif /* INC_BLUETOOTH_H_ */

 2.2 bluetooth.c

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>

#include "cmsis_os.h"
#include "main.h"
#include "uart.h"
#include "bluetooth.h"

// ๋ธ”๋ฃจํˆฌ์Šค ๋ฉ”์‹œ์ง€ ํ์˜ ํ•ธ๋“ค
static osMessageQueueId_t msgBufQ_handle;
// ๋ธ”๋ฃจํˆฌ์Šค ํƒœ์Šคํฌ์˜ ํ•ธ๋“ค
static osThreadId_t btTask_handle;
// ๋ธ”๋ฃจํˆฌ์Šค ํƒœ์Šคํฌ์˜ ์†์„ฑ
static const osThreadAttr_t btTask_attributes = {
  .stack_size = 256 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
static void btProc_Task(void *arg);

void bt_init(void)
{
  // ๋ธ”๋ฃจํˆฌ์Šค ํƒœ์Šคํฌ๋ฅผ ์ƒ์„ฑ
  btTask_handle = osThreadNew(btProc_Task, NULL, &btTask_attributes);
  if (btTask_handle == NULL) {
    printf("%s : %d\r\n", __func__, __LINE__);
    while (1);
  }
  // ๋ธ”๋ฃจํˆฌ์Šค ๋ฉ”์‹œ์ง€ ํ๋ฅผ ์ƒ์„ฑ
  msgBufQ_handle = osMessageQueueNew(5, sizeof(MSG_T), NULL);
  if (msgBufQ_handle == NULL) {
    printf("%s : %d\r\n", __func__, __LINE__);
    while (1);
  }
  printf("Bluetooth Initialized...\r\n");
}
// ๋ธ”๋ฃจํˆฌ์Šค ๋ฉ”์‹œ์ง€ ํ์— ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๋Š” ํ•จ์ˆ˜
void bt_QPut(const void *msg_ptr, uint8_t msg_prio, uint32_t timeout)
{
  // ๋ธ”๋ฃจํˆฌ์Šค ๋ฉ”์‹œ์ง€ ํ์— ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ธ๋‹ค.
  osMessageQueuePut(msgBufQ_handle, msg_ptr, msg_prio, timeout);
}
// ๋ธ”๋ฃจํˆฌ์Šค ์ƒํƒœ ์ฝœ๋ฐฑ ํ•จ์ˆ˜
void (*cbf)(void);
// ๋ธ”๋ฃจํˆฌ์Šค ์ƒํƒœ
uint8_t s_state = 0;
// ๋ธ”๋ฃจํˆฌ์Šค ๋ฉ”์‹œ์ง€ ๋ฒ„ํผ
char msg_buf[101];
// ๋ธ”๋ฃจํˆฌ์Šค ์ƒํƒœ S0 ํ•จ์ˆ˜
void func_s0(void)
{
  // MSG_T ๊ตฌ์กฐ์ฒด๋ฅผ ์„ ์–ธํ•œ๋‹ค.
  MSG_T qTx, qRx;

  // ๋ธ”๋ฃจํˆฌ์Šค ์ƒํƒœ๊ฐ€ S0์ด๋ฉด "BT status : S0"๋ฅผ ์ถœ๋ ฅ
  if (s_state == 0) {
    printf("BT status : S0\r\n");

    // qTx ๊ตฌ์กฐ์ฒด๋ฅผ ์„ค์ •
    qTx.id = E_MSG_UART2_TX_N_CHECK;
    strcpy(msg_buf, "AT");
    qTx.len = strlen(msg_buf);
    qTx.timeout = 1000;
    qTx.pData = (uint8_t *)msg_buf;

    // UART์— qTx ๊ตฌ์กฐ์ฒด๋ฅผ ๋ณด๋ƒ„
    uart_QPut(&qTx, 0, osWaitForever);

    // ๋ธ”๋ฃจํˆฌ์Šค ์ƒํƒœ๋ฅผ S1๋กœ ๋ณ€๊ฒฝ
    s_state++;
  } else {
    // MSG_T ๊ตฌ์กฐ์ฒด๋ฅผ ๋ฉ”์‹œ์ง€ ํ์—์„œ ๊ฐ€์ ธ์˜จ๋‹ค.
    osStatus_t sts = osMessageQueueGet(msgBufQ_handle, &qRx, NULL, osWaitForever);
    // ํƒ€์ž„์•„์›ƒ์ด ๋ฐœ์ƒํ•˜๋ฉด ๋ธ”๋ฃจํˆฌ์Šค ์ƒํƒœ๋ฅผ S0์œผ๋กœ ๋ณ€๊ฒฝ
    if (sts == osErrorTimeout) {
      s_state = 0;
  	} else if (sts == osOK) {
			switch (qRx.id) {
				case E_MSG_BT_RX : {
					if (memcmp(qRx.pData, "OK", 2) == 0) {
						printf("BT status : S1\r\n");
						cbf = func_s1;  // callback function
						s_state = 0;
						return;
					}} break;}  // switch
			s_state = 0;
		} // else if (sts == osOK) {}}
      
     // ๋ธ”๋ฃจํˆฌ์Šค ์ƒํƒœ S1 ํ•จ์ˆ˜
void func_s1(void)
{
  // MSG_T ๊ตฌ์กฐ์ฒด๋ฅผ ์„ ์–ธ
  MSG_T qTx, qRx;

  // ๋ธ”๋ฃจํˆฌ์Šค ์ƒํƒœ๊ฐ€ S1์ด๋ฉด "BT status : S1"๋ฅผ ์ถœ๋ ฅ
  if (s_state == 1) {
    printf("BT status : S1\r\n");

    // MSG_T ๊ตฌ์กฐ์ฒด๋ฅผ ๋ฉ”์‹œ์ง€ ํ์—์„œ ๊ฐ€์ ธ์˜ด
    osStatus_t sts = osMessageQueueGet(msgBufQ_handle, &qRx, NULL, 300);

    // ํƒ€์ž„์•„์›ƒ์ด ๋ฐœ์ƒํ•˜๋ฉด UART์— "AT"์„ ๋ณด๋ƒ„
    if (sts == osErrorTimeout) {
      qTx.id = E_MSG_UART2_TX_N_CHECK;
      qTx.len = strlen("AT");
      qTx.timeout = 1;
      qTx.pData = (uint8_t *)"AT";
      uart_QPut(&qTx, 0, 0);
    } else if (sts == osOK) {
      // ๋ฉ”์‹œ์ง€ ํ์—์„œ ๊ฐ€์ ธ์˜จ ๋ฉ”์‹œ์ง€์˜ ID๊ฐ€ E_MSG_BT_RX ์ด๋ฉด
      if (qRx.id == E_MSG_BT_RX) {
        // ๋ฉ”์‹œ์ง€ ํ์—์„œ ๊ฐ€์ ธ์˜จ ๋ฉ”์‹œ์ง€์˜ ๋ฐ์ดํ„ฐ๋ฅผ UART์— ๋‹ค์‹œ ๋ณด๋ƒ„
        qTx.id = E_MSG_UART2_TX;
        qTx.len = qRx.len;
        qTx.pData = qRx.pData;
        uart_QPut(&qTx, 0, 0);
      }}}}

static void btProc_Task(void *arg)
{
  // ๋ธ”๋ฃจํˆฌ์Šค ์ƒํƒœ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ func_s0๋กœ ์„ค์ •
  cbf = func_s0;.
  printf("Bluetooth Thread Start...\r\n");
  osDelay(500);
  cbf();   // ๋ธ”๋ฃจํˆฌ์Šค ์ƒํƒœ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ํ˜ธ์ถœ
  for (;;) {
    cbf();
  }}

์Šค๋งˆํŠธํฐ -- zs-040 bluetooth ์—ฐ๊ฒฐ๋จ ํ™•์ธ ๋ฐ led ์ œ์–ด, pwm ์ œ์–ด ํ™•์ธ

bluetooth ์—ฐ๊ฒฐ์€ ์„ฑ๊ณตํ•˜์˜€์œผ๋‚˜ ๋ฌด๋ฃŒ ์–ดํ”Œ์˜ terminal์„ ํ†ตํ•ด ๋ฉ”์‹œ์ง€๋ฅผ ์ „๋‹ฌํ•˜์˜€์œผ๋‚˜ s1 ํ•จ์ˆ˜๊ฐ€ ๋™์ž‘ํ•˜์ง€ ์•Š์•„
์ตœ๊ทผ์—๋Š” bluetooth 4.0์„ ๊ฑฐ์˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๊ฒƒ์œผ๋กœ ๋ณด์•„ ์–ดํ”Œ์—์„œ 4.0 ๋ฒ„์ „ ๊ฐ„์˜ ํ†ต์‹ ์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค. 
(โ€ป 4.0 ๋ฒ„์ „์— ๋งž๋Š” ๋ฉ”์‹œ์ง€ ์†ก์ˆ˜์‹ ์ด ๊ฐ€๋Šฅํ•œ bluetooth ์–ดํ”Œ apk ํŒŒ์ผ์„ ์ฐพ์•„๋ณด์•˜์œผ๋‚˜ ์ฐพ์ง€ ๋ชปํ•˜์˜€์Œ)

์—ฐ๊ฒฐ์—๋Š” ์„ฑ๊ณตํ•˜์˜€์œผ๋‚˜ s1 ํ•จ์ˆ˜ ์ž‘๋™ํ•˜์ง€ ์•Š์•„ ํ•ด๋‹น ๋ฉ”์‹œ์ง€๊ฐ€ return ๋˜์ง€ ์•Š์Œ

 

'embedded' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[protocol] CAN ํ†ต์‹  ์ด๋ก  ์ •๋ฆฌ  (0) 2023.10.14
[protocol] UDS ์ด๋ก  ์ •๋ฆฌ  (0) 2023.10.13
[stm32] RTOS ์‹ค์Šต  (0) 2023.09.18
[stm32] RTOS ์ด๋ก  ์ •๋ฆฌ  (0) 2023.09.11
[stm32] I2C ํ†ต์‹  ์‹ค์Šต  (0) 2023.09.11
profile

Toby's Study Blog

@Toby12

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!

๊ฒ€์ƒ‰ ํƒœ๊ทธ