포인터에 값을 추가로 넣을 수 있나요?

z_yon의 이미지

i2c 동작에 device id 와 reg 주소, 데이터를 포인터로 넘기는데
reg 주소를 데이터와 합치고 싶어요

i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t * data, uint8_t len)
이렇게 받아온 값을

i2c_write(uint8_t dev_id, uint8_t * tx_buf, uint8_t tx_num)
이 api에 맞춰서 넘기고 싶은데

주소값을 바로 넘기고 싶은데 레지스터 주소값을 어떻게 넘겨야하는게 좋을까요

kurishin의 이미지

후자의 API 수정이 불가능하다면 적당한 스택 크기만큼 끊어서 보내는 방법밖에 없을것으로 보입니다.

API 수정이 가능하다면 구조체로 2개 인자의 주소를 담아서 포인터를 넘기면 될것이고요.

라스코니의 이미지

우선 malloc()으로 잡히는 공간은 stack 영역이 아니라 heap 영역입니다.
심심찮게 내장형 소프트웨어 동작에서 가장 비싼 처리는 바로 특별한 이유없이 데이터를 copy or move 하는 것입니다.

아주 portable 하지는 않지만 많은 내장형/펌웨어 코드에서 사용하는 방식은 최대로 받을 수 있는 len 사이즈를 잡아 놓고 사용하는 것입니다. 간략히 나타내면 아래와 같이 할 수 있겠네요.

#define MAX_LEN 2000
 
uint8_t buf[MAX_LEN];
 
i2c_write(dev_id, reg_addr, buf+1, len); // buf+1 은 buf[1] 부터 데이터를 받아 오라는 의미
 
buf[0] = reg_addr;
 
//이제 buf[]는 reg_addr + data[len] 을 가지고 있는 byte stream이 됨

물론 이 방법은 쓰레드와 mutex를 쓸 필요가 없는 상황에서만 활용가능합니다.

익명 사용자의 이미지

API 에서 reg_addr 을 먼저 보내고 그 다음 data를 보내야 됩니다. 스택 크기가 부족하니까요.
API 내부에서는 스택 크기가 충분한가요? 동일한 실행영역이면 스택 부족으로 연결할수가 없습니다.
스트림으로 끊어서 보내야 합니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.